xref: /openbmc/linux/drivers/soc/qcom/rmtfs_mem.c (revision 017cdefe)
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 num_vmids, vmid[NUM_MAX_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 < 0) {
232 		dev_err(&pdev->dev, "failed to count qcom,vmid elements: %d\n", ret);
233 		goto remove_cdev;
234 	} else if (num_vmids > NUM_MAX_VMIDS) {
235 		dev_warn(&pdev->dev,
236 			 "too many VMIDs (%d) specified! Only mapping first %d entries\n",
237 			 num_vmids, NUM_MAX_VMIDS);
238 		num_vmids = NUM_MAX_VMIDS;
239 	}
240 
241 	ret = of_property_read_u32_array(node, "qcom,vmid", vmid, num_vmids);
242 	if (ret < 0 && ret != -EINVAL) {
243 		dev_err(&pdev->dev, "failed to parse qcom,vmid\n");
244 		goto remove_cdev;
245 	} else if (!ret) {
246 		if (!qcom_scm_is_available()) {
247 			ret = -EPROBE_DEFER;
248 			goto remove_cdev;
249 		}
250 
251 		perms[0].vmid = QCOM_SCM_VMID_HLOS;
252 		perms[0].perm = QCOM_SCM_PERM_RW;
253 
254 		for (i = 0; i < num_vmids; i++) {
255 			perms[i + 1].vmid = vmid[i];
256 			perms[i + 1].perm = QCOM_SCM_PERM_RW;
257 		}
258 
259 		rmtfs_mem->perms = BIT(QCOM_SCM_VMID_HLOS);
260 		ret = qcom_scm_assign_mem(rmtfs_mem->addr, rmtfs_mem->size,
261 					  &rmtfs_mem->perms, perms, num_vmids + 1);
262 		if (ret < 0) {
263 			dev_err(&pdev->dev, "assign memory failed\n");
264 			goto remove_cdev;
265 		}
266 	}
267 
268 	dev_set_drvdata(&pdev->dev, rmtfs_mem);
269 
270 	return 0;
271 
272 remove_cdev:
273 	cdev_device_del(&rmtfs_mem->cdev, &rmtfs_mem->dev);
274 put_device:
275 	put_device(&rmtfs_mem->dev);
276 
277 	return ret;
278 }
279 
280 static int qcom_rmtfs_mem_remove(struct platform_device *pdev)
281 {
282 	struct qcom_rmtfs_mem *rmtfs_mem = dev_get_drvdata(&pdev->dev);
283 	struct qcom_scm_vmperm perm;
284 
285 	if (rmtfs_mem->perms) {
286 		perm.vmid = QCOM_SCM_VMID_HLOS;
287 		perm.perm = QCOM_SCM_PERM_RW;
288 
289 		qcom_scm_assign_mem(rmtfs_mem->addr, rmtfs_mem->size,
290 				    &rmtfs_mem->perms, &perm, 1);
291 	}
292 
293 	cdev_device_del(&rmtfs_mem->cdev, &rmtfs_mem->dev);
294 	put_device(&rmtfs_mem->dev);
295 
296 	return 0;
297 }
298 
299 static const struct of_device_id qcom_rmtfs_mem_of_match[] = {
300 	{ .compatible = "qcom,rmtfs-mem" },
301 	{}
302 };
303 MODULE_DEVICE_TABLE(of, qcom_rmtfs_mem_of_match);
304 
305 static struct platform_driver qcom_rmtfs_mem_driver = {
306 	.probe = qcom_rmtfs_mem_probe,
307 	.remove = qcom_rmtfs_mem_remove,
308 	.driver  = {
309 		.name  = "qcom_rmtfs_mem",
310 		.of_match_table = qcom_rmtfs_mem_of_match,
311 	},
312 };
313 
314 static int __init qcom_rmtfs_mem_init(void)
315 {
316 	int ret;
317 
318 	ret = class_register(&rmtfs_class);
319 	if (ret)
320 		return ret;
321 
322 	ret = alloc_chrdev_region(&qcom_rmtfs_mem_major, 0,
323 				  QCOM_RMTFS_MEM_DEV_MAX, "qcom_rmtfs_mem");
324 	if (ret < 0) {
325 		pr_err("qcom_rmtfs_mem: failed to allocate char dev region\n");
326 		goto unregister_class;
327 	}
328 
329 	ret = platform_driver_register(&qcom_rmtfs_mem_driver);
330 	if (ret < 0) {
331 		pr_err("qcom_rmtfs_mem: failed to register rmtfs_mem driver\n");
332 		goto unregister_chrdev;
333 	}
334 
335 	return 0;
336 
337 unregister_chrdev:
338 	unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
339 unregister_class:
340 	class_unregister(&rmtfs_class);
341 	return ret;
342 }
343 module_init(qcom_rmtfs_mem_init);
344 
345 static void __exit qcom_rmtfs_mem_exit(void)
346 {
347 	platform_driver_unregister(&qcom_rmtfs_mem_driver);
348 	unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
349 	class_unregister(&rmtfs_class);
350 }
351 module_exit(qcom_rmtfs_mem_exit);
352 
353 MODULE_AUTHOR("Linaro Ltd");
354 MODULE_DESCRIPTION("Qualcomm Remote Filesystem memory driver");
355 MODULE_LICENSE("GPL v2");
356