xref: /openbmc/linux/drivers/soc/qcom/rmtfs_mem.c (revision 2bad466c)
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 	unsigned int 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 	.owner          = THIS_MODULE,
130 	.name           = "rmtfs",
131 };
132 
133 static int qcom_rmtfs_mem_mmap(struct file *filep, struct vm_area_struct *vma)
134 {
135 	struct qcom_rmtfs_mem *rmtfs_mem = filep->private_data;
136 
137 	if (vma->vm_end - vma->vm_start > rmtfs_mem->size) {
138 		dev_dbg(&rmtfs_mem->dev,
139 			"vm_end[%lu] - vm_start[%lu] [%lu] > mem->size[%pa]\n",
140 			vma->vm_end, vma->vm_start,
141 			(vma->vm_end - vma->vm_start), &rmtfs_mem->size);
142 		return -EINVAL;
143 	}
144 
145 	vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
146 	return remap_pfn_range(vma,
147 			       vma->vm_start,
148 			       rmtfs_mem->addr >> PAGE_SHIFT,
149 			       vma->vm_end - vma->vm_start,
150 			       vma->vm_page_prot);
151 }
152 
153 static const struct file_operations qcom_rmtfs_mem_fops = {
154 	.owner = THIS_MODULE,
155 	.open = qcom_rmtfs_mem_open,
156 	.read = qcom_rmtfs_mem_read,
157 	.write = qcom_rmtfs_mem_write,
158 	.release = qcom_rmtfs_mem_release,
159 	.llseek = default_llseek,
160 	.mmap = qcom_rmtfs_mem_mmap,
161 };
162 
163 static void qcom_rmtfs_mem_release_device(struct device *dev)
164 {
165 	struct qcom_rmtfs_mem *rmtfs_mem = container_of(dev,
166 							struct qcom_rmtfs_mem,
167 							dev);
168 
169 	kfree(rmtfs_mem);
170 }
171 
172 static int qcom_rmtfs_mem_probe(struct platform_device *pdev)
173 {
174 	struct device_node *node = pdev->dev.of_node;
175 	struct qcom_scm_vmperm perms[NUM_MAX_VMIDS + 1];
176 	struct reserved_mem *rmem;
177 	struct qcom_rmtfs_mem *rmtfs_mem;
178 	u32 client_id;
179 	u32 vmid[NUM_MAX_VMIDS];
180 	int num_vmids;
181 	int ret, i;
182 
183 	rmem = of_reserved_mem_lookup(node);
184 	if (!rmem) {
185 		dev_err(&pdev->dev, "failed to acquire memory region\n");
186 		return -EINVAL;
187 	}
188 
189 	ret = of_property_read_u32(node, "qcom,client-id", &client_id);
190 	if (ret) {
191 		dev_err(&pdev->dev, "failed to parse \"qcom,client-id\"\n");
192 		return ret;
193 
194 	}
195 
196 	rmtfs_mem = kzalloc(sizeof(*rmtfs_mem), GFP_KERNEL);
197 	if (!rmtfs_mem)
198 		return -ENOMEM;
199 
200 	rmtfs_mem->addr = rmem->base;
201 	rmtfs_mem->client_id = client_id;
202 	rmtfs_mem->size = rmem->size;
203 
204 	device_initialize(&rmtfs_mem->dev);
205 	rmtfs_mem->dev.parent = &pdev->dev;
206 	rmtfs_mem->dev.groups = qcom_rmtfs_mem_groups;
207 	rmtfs_mem->dev.release = qcom_rmtfs_mem_release_device;
208 
209 	rmtfs_mem->base = devm_memremap(&rmtfs_mem->dev, rmtfs_mem->addr,
210 					rmtfs_mem->size, MEMREMAP_WC);
211 	if (IS_ERR(rmtfs_mem->base)) {
212 		dev_err(&pdev->dev, "failed to remap rmtfs_mem region\n");
213 		ret = PTR_ERR(rmtfs_mem->base);
214 		goto put_device;
215 	}
216 
217 	cdev_init(&rmtfs_mem->cdev, &qcom_rmtfs_mem_fops);
218 	rmtfs_mem->cdev.owner = THIS_MODULE;
219 
220 	dev_set_name(&rmtfs_mem->dev, "qcom_rmtfs_mem%d", client_id);
221 	rmtfs_mem->dev.id = client_id;
222 	rmtfs_mem->dev.class = &rmtfs_class;
223 	rmtfs_mem->dev.devt = MKDEV(MAJOR(qcom_rmtfs_mem_major), client_id);
224 
225 	ret = cdev_device_add(&rmtfs_mem->cdev, &rmtfs_mem->dev);
226 	if (ret) {
227 		dev_err(&pdev->dev, "failed to add cdev: %d\n", ret);
228 		goto put_device;
229 	}
230 
231 	num_vmids = of_property_count_u32_elems(node, "qcom,vmid");
232 	if (num_vmids == -EINVAL) {
233 		/* qcom,vmid is optional */
234 		num_vmids = 0;
235 	} else if (num_vmids < 0) {
236 		dev_err(&pdev->dev, "failed to count qcom,vmid elements: %d\n", num_vmids);
237 		goto remove_cdev;
238 	} else if (num_vmids > NUM_MAX_VMIDS) {
239 		dev_warn(&pdev->dev,
240 			 "too many VMIDs (%d) specified! Only mapping first %d entries\n",
241 			 num_vmids, NUM_MAX_VMIDS);
242 		num_vmids = NUM_MAX_VMIDS;
243 	}
244 
245 	ret = of_property_read_u32_array(node, "qcom,vmid", vmid, num_vmids);
246 	if (ret < 0 && ret != -EINVAL) {
247 		dev_err(&pdev->dev, "failed to parse qcom,vmid\n");
248 		goto remove_cdev;
249 	} else if (!ret) {
250 		if (!qcom_scm_is_available()) {
251 			ret = -EPROBE_DEFER;
252 			goto remove_cdev;
253 		}
254 
255 		perms[0].vmid = QCOM_SCM_VMID_HLOS;
256 		perms[0].perm = QCOM_SCM_PERM_RW;
257 
258 		for (i = 0; i < num_vmids; i++) {
259 			perms[i + 1].vmid = vmid[i];
260 			perms[i + 1].perm = QCOM_SCM_PERM_RW;
261 		}
262 
263 		rmtfs_mem->perms = BIT(QCOM_SCM_VMID_HLOS);
264 		ret = qcom_scm_assign_mem(rmtfs_mem->addr, rmtfs_mem->size,
265 					  &rmtfs_mem->perms, perms, num_vmids + 1);
266 		if (ret < 0) {
267 			dev_err(&pdev->dev, "assign memory failed\n");
268 			goto remove_cdev;
269 		}
270 	}
271 
272 	dev_set_drvdata(&pdev->dev, rmtfs_mem);
273 
274 	return 0;
275 
276 remove_cdev:
277 	cdev_device_del(&rmtfs_mem->cdev, &rmtfs_mem->dev);
278 put_device:
279 	put_device(&rmtfs_mem->dev);
280 
281 	return ret;
282 }
283 
284 static int qcom_rmtfs_mem_remove(struct platform_device *pdev)
285 {
286 	struct qcom_rmtfs_mem *rmtfs_mem = dev_get_drvdata(&pdev->dev);
287 	struct qcom_scm_vmperm perm;
288 
289 	if (rmtfs_mem->perms) {
290 		perm.vmid = QCOM_SCM_VMID_HLOS;
291 		perm.perm = QCOM_SCM_PERM_RW;
292 
293 		qcom_scm_assign_mem(rmtfs_mem->addr, rmtfs_mem->size,
294 				    &rmtfs_mem->perms, &perm, 1);
295 	}
296 
297 	cdev_device_del(&rmtfs_mem->cdev, &rmtfs_mem->dev);
298 	put_device(&rmtfs_mem->dev);
299 
300 	return 0;
301 }
302 
303 static const struct of_device_id qcom_rmtfs_mem_of_match[] = {
304 	{ .compatible = "qcom,rmtfs-mem" },
305 	{}
306 };
307 MODULE_DEVICE_TABLE(of, qcom_rmtfs_mem_of_match);
308 
309 static struct platform_driver qcom_rmtfs_mem_driver = {
310 	.probe = qcom_rmtfs_mem_probe,
311 	.remove = qcom_rmtfs_mem_remove,
312 	.driver  = {
313 		.name  = "qcom_rmtfs_mem",
314 		.of_match_table = qcom_rmtfs_mem_of_match,
315 	},
316 };
317 
318 static int __init qcom_rmtfs_mem_init(void)
319 {
320 	int ret;
321 
322 	ret = class_register(&rmtfs_class);
323 	if (ret)
324 		return ret;
325 
326 	ret = alloc_chrdev_region(&qcom_rmtfs_mem_major, 0,
327 				  QCOM_RMTFS_MEM_DEV_MAX, "qcom_rmtfs_mem");
328 	if (ret < 0) {
329 		pr_err("qcom_rmtfs_mem: failed to allocate char dev region\n");
330 		goto unregister_class;
331 	}
332 
333 	ret = platform_driver_register(&qcom_rmtfs_mem_driver);
334 	if (ret < 0) {
335 		pr_err("qcom_rmtfs_mem: failed to register rmtfs_mem driver\n");
336 		goto unregister_chrdev;
337 	}
338 
339 	return 0;
340 
341 unregister_chrdev:
342 	unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
343 unregister_class:
344 	class_unregister(&rmtfs_class);
345 	return ret;
346 }
347 module_init(qcom_rmtfs_mem_init);
348 
349 static void __exit qcom_rmtfs_mem_exit(void)
350 {
351 	platform_driver_unregister(&qcom_rmtfs_mem_driver);
352 	unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
353 	class_unregister(&rmtfs_class);
354 }
355 module_exit(qcom_rmtfs_mem_exit);
356 
357 MODULE_AUTHOR("Linaro Ltd");
358 MODULE_DESCRIPTION("Qualcomm Remote Filesystem memory driver");
359 MODULE_LICENSE("GPL v2");
360