xref: /openbmc/linux/drivers/soc/qcom/rmtfs_mem.c (revision 7b374a2f)
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 
qcom_rmtfs_mem_show(struct device * dev,struct device_attribute * attr,char * buf)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 
qcom_rmtfs_mem_open(struct inode * inode,struct file * filp)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 }
qcom_rmtfs_mem_read(struct file * filp,char __user * buf,size_t count,loff_t * f_pos)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 
qcom_rmtfs_mem_write(struct file * filp,const char __user * buf,size_t count,loff_t * f_pos)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 
qcom_rmtfs_mem_release(struct inode * inode,struct file * filp)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 
qcom_rmtfs_mem_mmap(struct file * filep,struct vm_area_struct * vma)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 
qcom_rmtfs_mem_release_device(struct device * dev)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 
qcom_rmtfs_mem_probe(struct platform_device * pdev)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 		ret = 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 
qcom_rmtfs_mem_remove(struct platform_device * pdev)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 
qcom_rmtfs_mem_init(void)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 
qcom_rmtfs_mem_exit(void)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