xref: /linux/drivers/soc/qcom/rmtfs_mem.c (revision 5e3992fe72748ed3892be876f09d4d990548b7af)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017 Linaro Ltd.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/cdev.h>
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/of.h>
12 #include <linux/of_reserved_mem.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <linux/io.h>
17 #include <linux/firmware/qcom/qcom_scm.h>
18 
19 #define QCOM_RMTFS_MEM_DEV_MAX	(MINORMASK + 1)
20 #define NUM_MAX_VMIDS		2
21 
22 static dev_t qcom_rmtfs_mem_major;
23 
24 struct qcom_rmtfs_mem {
25 	struct device dev;
26 	struct cdev cdev;
27 
28 	void *base;
29 	phys_addr_t addr;
30 	phys_addr_t size;
31 
32 	unsigned int client_id;
33 
34 	u64 perms;
35 };
36 
37 static ssize_t qcom_rmtfs_mem_show(struct device *dev,
38 			      struct device_attribute *attr,
39 			      char *buf);
40 
41 static DEVICE_ATTR(phys_addr, 0444, qcom_rmtfs_mem_show, NULL);
42 static DEVICE_ATTR(size, 0444, qcom_rmtfs_mem_show, NULL);
43 static DEVICE_ATTR(client_id, 0444, qcom_rmtfs_mem_show, NULL);
44 
45 static ssize_t qcom_rmtfs_mem_show(struct device *dev,
46 			      struct device_attribute *attr,
47 			      char *buf)
48 {
49 	struct qcom_rmtfs_mem *rmtfs_mem = container_of(dev,
50 							struct qcom_rmtfs_mem,
51 							dev);
52 
53 	if (attr == &dev_attr_phys_addr)
54 		return sprintf(buf, "%pa\n", &rmtfs_mem->addr);
55 	if (attr == &dev_attr_size)
56 		return sprintf(buf, "%pa\n", &rmtfs_mem->size);
57 	if (attr == &dev_attr_client_id)
58 		return sprintf(buf, "%d\n", rmtfs_mem->client_id);
59 
60 	return -EINVAL;
61 }
62 
63 static struct attribute *qcom_rmtfs_mem_attrs[] = {
64 	&dev_attr_phys_addr.attr,
65 	&dev_attr_size.attr,
66 	&dev_attr_client_id.attr,
67 	NULL
68 };
69 ATTRIBUTE_GROUPS(qcom_rmtfs_mem);
70 
71 static int qcom_rmtfs_mem_open(struct inode *inode, struct file *filp)
72 {
73 	struct qcom_rmtfs_mem *rmtfs_mem = container_of(inode->i_cdev,
74 							struct qcom_rmtfs_mem,
75 							cdev);
76 
77 	get_device(&rmtfs_mem->dev);
78 	filp->private_data = rmtfs_mem;
79 
80 	return 0;
81 }
82 static ssize_t qcom_rmtfs_mem_read(struct file *filp,
83 			      char __user *buf, size_t count, loff_t *f_pos)
84 {
85 	struct qcom_rmtfs_mem *rmtfs_mem = filp->private_data;
86 
87 	if (*f_pos >= rmtfs_mem->size)
88 		return 0;
89 
90 	if (*f_pos + count >= rmtfs_mem->size)
91 		count = rmtfs_mem->size - *f_pos;
92 
93 	if (copy_to_user(buf, rmtfs_mem->base + *f_pos, count))
94 		return -EFAULT;
95 
96 	*f_pos += count;
97 	return count;
98 }
99 
100 static ssize_t qcom_rmtfs_mem_write(struct file *filp,
101 			       const char __user *buf, size_t count,
102 			       loff_t *f_pos)
103 {
104 	struct qcom_rmtfs_mem *rmtfs_mem = filp->private_data;
105 
106 	if (*f_pos >= rmtfs_mem->size)
107 		return 0;
108 
109 	if (*f_pos + count >= rmtfs_mem->size)
110 		count = rmtfs_mem->size - *f_pos;
111 
112 	if (copy_from_user(rmtfs_mem->base + *f_pos, buf, count))
113 		return -EFAULT;
114 
115 	*f_pos += count;
116 	return count;
117 }
118 
119 static int qcom_rmtfs_mem_release(struct inode *inode, struct file *filp)
120 {
121 	struct qcom_rmtfs_mem *rmtfs_mem = filp->private_data;
122 
123 	put_device(&rmtfs_mem->dev);
124 
125 	return 0;
126 }
127 
128 static struct class rmtfs_class = {
129 	.name           = "rmtfs",
130 };
131 
132 static int qcom_rmtfs_mem_mmap(struct file *filep, struct vm_area_struct *vma)
133 {
134 	struct qcom_rmtfs_mem *rmtfs_mem = filep->private_data;
135 
136 	if (vma->vm_end - vma->vm_start > rmtfs_mem->size) {
137 		dev_dbg(&rmtfs_mem->dev,
138 			"vm_end[%lu] - vm_start[%lu] [%lu] > mem->size[%pa]\n",
139 			vma->vm_end, vma->vm_start,
140 			(vma->vm_end - vma->vm_start), &rmtfs_mem->size);
141 		return -EINVAL;
142 	}
143 
144 	vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
145 	return remap_pfn_range(vma,
146 			       vma->vm_start,
147 			       rmtfs_mem->addr >> PAGE_SHIFT,
148 			       vma->vm_end - vma->vm_start,
149 			       vma->vm_page_prot);
150 }
151 
152 static const struct file_operations qcom_rmtfs_mem_fops = {
153 	.owner = THIS_MODULE,
154 	.open = qcom_rmtfs_mem_open,
155 	.read = qcom_rmtfs_mem_read,
156 	.write = qcom_rmtfs_mem_write,
157 	.release = qcom_rmtfs_mem_release,
158 	.llseek = default_llseek,
159 	.mmap = qcom_rmtfs_mem_mmap,
160 };
161 
162 static void qcom_rmtfs_mem_release_device(struct device *dev)
163 {
164 	struct qcom_rmtfs_mem *rmtfs_mem = container_of(dev,
165 							struct qcom_rmtfs_mem,
166 							dev);
167 
168 	kfree(rmtfs_mem);
169 }
170 
171 static int qcom_rmtfs_mem_probe(struct platform_device *pdev)
172 {
173 	struct device_node *node = pdev->dev.of_node;
174 	struct qcom_scm_vmperm perms[NUM_MAX_VMIDS + 1];
175 	struct reserved_mem *rmem;
176 	struct qcom_rmtfs_mem *rmtfs_mem;
177 	u32 client_id;
178 	u32 vmid[NUM_MAX_VMIDS];
179 	int num_vmids;
180 	int ret, i;
181 
182 	rmem = of_reserved_mem_lookup(node);
183 	if (!rmem) {
184 		dev_err(&pdev->dev, "failed to acquire memory region\n");
185 		return -EINVAL;
186 	}
187 
188 	ret = of_property_read_u32(node, "qcom,client-id", &client_id);
189 	if (ret) {
190 		dev_err(&pdev->dev, "failed to parse \"qcom,client-id\"\n");
191 		return ret;
192 
193 	}
194 
195 	rmtfs_mem = kzalloc(sizeof(*rmtfs_mem), GFP_KERNEL);
196 	if (!rmtfs_mem)
197 		return -ENOMEM;
198 
199 	rmtfs_mem->addr = rmem->base;
200 	rmtfs_mem->client_id = client_id;
201 	rmtfs_mem->size = rmem->size;
202 
203 	device_initialize(&rmtfs_mem->dev);
204 	rmtfs_mem->dev.parent = &pdev->dev;
205 	rmtfs_mem->dev.groups = qcom_rmtfs_mem_groups;
206 	rmtfs_mem->dev.release = qcom_rmtfs_mem_release_device;
207 
208 	rmtfs_mem->base = devm_memremap(&rmtfs_mem->dev, rmtfs_mem->addr,
209 					rmtfs_mem->size, MEMREMAP_WC);
210 	if (IS_ERR(rmtfs_mem->base)) {
211 		dev_err(&pdev->dev, "failed to remap rmtfs_mem region\n");
212 		ret = PTR_ERR(rmtfs_mem->base);
213 		goto put_device;
214 	}
215 
216 	cdev_init(&rmtfs_mem->cdev, &qcom_rmtfs_mem_fops);
217 	rmtfs_mem->cdev.owner = THIS_MODULE;
218 
219 	dev_set_name(&rmtfs_mem->dev, "qcom_rmtfs_mem%d", client_id);
220 	rmtfs_mem->dev.id = client_id;
221 	rmtfs_mem->dev.class = &rmtfs_class;
222 	rmtfs_mem->dev.devt = MKDEV(MAJOR(qcom_rmtfs_mem_major), client_id);
223 
224 	ret = cdev_device_add(&rmtfs_mem->cdev, &rmtfs_mem->dev);
225 	if (ret) {
226 		dev_err(&pdev->dev, "failed to add cdev: %d\n", ret);
227 		goto put_device;
228 	}
229 
230 	num_vmids = of_property_count_u32_elems(node, "qcom,vmid");
231 	if (num_vmids == -EINVAL) {
232 		/* qcom,vmid is optional */
233 		num_vmids = 0;
234 	} else if (num_vmids < 0) {
235 		dev_err(&pdev->dev, "failed to count qcom,vmid elements: %d\n", num_vmids);
236 		goto remove_cdev;
237 	} else if (num_vmids > NUM_MAX_VMIDS) {
238 		dev_warn(&pdev->dev,
239 			 "too many VMIDs (%d) specified! Only mapping first %d entries\n",
240 			 num_vmids, NUM_MAX_VMIDS);
241 		num_vmids = NUM_MAX_VMIDS;
242 	}
243 
244 	ret = of_property_read_u32_array(node, "qcom,vmid", vmid, num_vmids);
245 	if (ret < 0 && ret != -EINVAL) {
246 		dev_err(&pdev->dev, "failed to parse qcom,vmid\n");
247 		goto remove_cdev;
248 	} else if (!ret) {
249 		if (!qcom_scm_is_available()) {
250 			ret = -EPROBE_DEFER;
251 			goto remove_cdev;
252 		}
253 
254 		perms[0].vmid = QCOM_SCM_VMID_HLOS;
255 		perms[0].perm = QCOM_SCM_PERM_RW;
256 
257 		for (i = 0; i < num_vmids; i++) {
258 			perms[i + 1].vmid = vmid[i];
259 			perms[i + 1].perm = QCOM_SCM_PERM_RW;
260 		}
261 
262 		rmtfs_mem->perms = BIT(QCOM_SCM_VMID_HLOS);
263 		ret = qcom_scm_assign_mem(rmtfs_mem->addr, rmtfs_mem->size,
264 					  &rmtfs_mem->perms, perms, num_vmids + 1);
265 		if (ret < 0) {
266 			dev_err(&pdev->dev, "assign memory failed\n");
267 			goto remove_cdev;
268 		}
269 	}
270 
271 	dev_set_drvdata(&pdev->dev, rmtfs_mem);
272 
273 	return 0;
274 
275 remove_cdev:
276 	cdev_device_del(&rmtfs_mem->cdev, &rmtfs_mem->dev);
277 put_device:
278 	put_device(&rmtfs_mem->dev);
279 
280 	return ret;
281 }
282 
283 static int qcom_rmtfs_mem_remove(struct platform_device *pdev)
284 {
285 	struct qcom_rmtfs_mem *rmtfs_mem = dev_get_drvdata(&pdev->dev);
286 	struct qcom_scm_vmperm perm;
287 
288 	if (rmtfs_mem->perms) {
289 		perm.vmid = QCOM_SCM_VMID_HLOS;
290 		perm.perm = QCOM_SCM_PERM_RW;
291 
292 		qcom_scm_assign_mem(rmtfs_mem->addr, rmtfs_mem->size,
293 				    &rmtfs_mem->perms, &perm, 1);
294 	}
295 
296 	cdev_device_del(&rmtfs_mem->cdev, &rmtfs_mem->dev);
297 	put_device(&rmtfs_mem->dev);
298 
299 	return 0;
300 }
301 
302 static const struct of_device_id qcom_rmtfs_mem_of_match[] = {
303 	{ .compatible = "qcom,rmtfs-mem" },
304 	{}
305 };
306 MODULE_DEVICE_TABLE(of, qcom_rmtfs_mem_of_match);
307 
308 static struct platform_driver qcom_rmtfs_mem_driver = {
309 	.probe = qcom_rmtfs_mem_probe,
310 	.remove = qcom_rmtfs_mem_remove,
311 	.driver  = {
312 		.name  = "qcom_rmtfs_mem",
313 		.of_match_table = qcom_rmtfs_mem_of_match,
314 	},
315 };
316 
317 static int __init qcom_rmtfs_mem_init(void)
318 {
319 	int ret;
320 
321 	ret = class_register(&rmtfs_class);
322 	if (ret)
323 		return ret;
324 
325 	ret = alloc_chrdev_region(&qcom_rmtfs_mem_major, 0,
326 				  QCOM_RMTFS_MEM_DEV_MAX, "qcom_rmtfs_mem");
327 	if (ret < 0) {
328 		pr_err("qcom_rmtfs_mem: failed to allocate char dev region\n");
329 		goto unregister_class;
330 	}
331 
332 	ret = platform_driver_register(&qcom_rmtfs_mem_driver);
333 	if (ret < 0) {
334 		pr_err("qcom_rmtfs_mem: failed to register rmtfs_mem driver\n");
335 		goto unregister_chrdev;
336 	}
337 
338 	return 0;
339 
340 unregister_chrdev:
341 	unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
342 unregister_class:
343 	class_unregister(&rmtfs_class);
344 	return ret;
345 }
346 module_init(qcom_rmtfs_mem_init);
347 
348 static void __exit qcom_rmtfs_mem_exit(void)
349 {
350 	platform_driver_unregister(&qcom_rmtfs_mem_driver);
351 	unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
352 	class_unregister(&rmtfs_class);
353 }
354 module_exit(qcom_rmtfs_mem_exit);
355 
356 MODULE_AUTHOR("Linaro Ltd");
357 MODULE_DESCRIPTION("Qualcomm Remote Filesystem memory driver");
358 MODULE_LICENSE("GPL v2");
359