xref: /openbmc/linux/drivers/soc/qcom/rmtfs_mem.c (revision fb960bd2)
1 /*
2  * Copyright (c) 2017 Linaro Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/cdev.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/of.h>
20 #include <linux/of_reserved_mem.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24 #include <linux/io.h>
25 #include <linux/qcom_scm.h>
26 
27 #define QCOM_RMTFS_MEM_DEV_MAX	(MINORMASK + 1)
28 
29 static dev_t qcom_rmtfs_mem_major;
30 
31 struct qcom_rmtfs_mem {
32 	struct device dev;
33 	struct cdev cdev;
34 
35 	void *base;
36 	phys_addr_t addr;
37 	phys_addr_t size;
38 
39 	unsigned int client_id;
40 };
41 
42 static ssize_t qcom_rmtfs_mem_show(struct device *dev,
43 			      struct device_attribute *attr,
44 			      char *buf);
45 
46 static DEVICE_ATTR(phys_addr, 0400, qcom_rmtfs_mem_show, NULL);
47 static DEVICE_ATTR(size, 0400, qcom_rmtfs_mem_show, NULL);
48 static DEVICE_ATTR(client_id, 0400, qcom_rmtfs_mem_show, NULL);
49 
50 static ssize_t qcom_rmtfs_mem_show(struct device *dev,
51 			      struct device_attribute *attr,
52 			      char *buf)
53 {
54 	struct qcom_rmtfs_mem *rmtfs_mem = container_of(dev,
55 							struct qcom_rmtfs_mem,
56 							dev);
57 
58 	if (attr == &dev_attr_phys_addr)
59 		return sprintf(buf, "%pa\n", &rmtfs_mem->addr);
60 	if (attr == &dev_attr_size)
61 		return sprintf(buf, "%pa\n", &rmtfs_mem->size);
62 	if (attr == &dev_attr_client_id)
63 		return sprintf(buf, "%d\n", rmtfs_mem->client_id);
64 
65 	return -EINVAL;
66 }
67 
68 static struct attribute *qcom_rmtfs_mem_attrs[] = {
69 	&dev_attr_phys_addr.attr,
70 	&dev_attr_size.attr,
71 	&dev_attr_client_id.attr,
72 	NULL
73 };
74 ATTRIBUTE_GROUPS(qcom_rmtfs_mem);
75 
76 static int qcom_rmtfs_mem_open(struct inode *inode, struct file *filp)
77 {
78 	struct qcom_rmtfs_mem *rmtfs_mem = container_of(inode->i_cdev,
79 							struct qcom_rmtfs_mem,
80 							cdev);
81 
82 	get_device(&rmtfs_mem->dev);
83 	filp->private_data = rmtfs_mem;
84 
85 	return 0;
86 }
87 static ssize_t qcom_rmtfs_mem_read(struct file *filp,
88 			      char __user *buf, size_t count, loff_t *f_pos)
89 {
90 	struct qcom_rmtfs_mem *rmtfs_mem = filp->private_data;
91 
92 	if (*f_pos >= rmtfs_mem->size)
93 		return 0;
94 
95 	if (*f_pos + count >= rmtfs_mem->size)
96 		count = rmtfs_mem->size - *f_pos;
97 
98 	if (copy_to_user(buf, rmtfs_mem->base + *f_pos, count))
99 		return -EFAULT;
100 
101 	*f_pos += count;
102 	return count;
103 }
104 
105 static ssize_t qcom_rmtfs_mem_write(struct file *filp,
106 			       const char __user *buf, size_t count,
107 			       loff_t *f_pos)
108 {
109 	struct qcom_rmtfs_mem *rmtfs_mem = filp->private_data;
110 
111 	if (*f_pos >= rmtfs_mem->size)
112 		return 0;
113 
114 	if (*f_pos + count >= rmtfs_mem->size)
115 		count = rmtfs_mem->size - *f_pos;
116 
117 	if (copy_from_user(rmtfs_mem->base + *f_pos, buf, count))
118 		return -EFAULT;
119 
120 	*f_pos += count;
121 	return count;
122 }
123 
124 static int qcom_rmtfs_mem_release(struct inode *inode, struct file *filp)
125 {
126 	struct qcom_rmtfs_mem *rmtfs_mem = filp->private_data;
127 
128 	put_device(&rmtfs_mem->dev);
129 
130 	return 0;
131 }
132 
133 static const struct file_operations qcom_rmtfs_mem_fops = {
134 	.owner = THIS_MODULE,
135 	.open = qcom_rmtfs_mem_open,
136 	.read = qcom_rmtfs_mem_read,
137 	.write = qcom_rmtfs_mem_write,
138 	.release = qcom_rmtfs_mem_release,
139 	.llseek = default_llseek,
140 };
141 
142 static void qcom_rmtfs_mem_release_device(struct device *dev)
143 {
144 	struct qcom_rmtfs_mem *rmtfs_mem = container_of(dev,
145 							struct qcom_rmtfs_mem,
146 							dev);
147 
148 	kfree(rmtfs_mem);
149 }
150 
151 static int qcom_rmtfs_mem_probe(struct platform_device *pdev)
152 {
153 	struct device_node *node = pdev->dev.of_node;
154 	struct reserved_mem *rmem;
155 	struct qcom_rmtfs_mem *rmtfs_mem;
156 	u32 client_id;
157 	int ret;
158 
159 	rmem = of_reserved_mem_lookup(node);
160 	if (!rmem) {
161 		dev_err(&pdev->dev, "failed to acquire memory region\n");
162 		return -EINVAL;
163 	}
164 
165 	ret = of_property_read_u32(node, "qcom,client-id", &client_id);
166 	if (ret) {
167 		dev_err(&pdev->dev, "failed to parse \"qcom,client-id\"\n");
168 		return ret;
169 
170 	}
171 
172 	rmtfs_mem = kzalloc(sizeof(*rmtfs_mem), GFP_KERNEL);
173 	if (!rmtfs_mem)
174 		return -ENOMEM;
175 
176 	rmtfs_mem->addr = rmem->base;
177 	rmtfs_mem->client_id = client_id;
178 	rmtfs_mem->size = rmem->size;
179 
180 	device_initialize(&rmtfs_mem->dev);
181 	rmtfs_mem->dev.parent = &pdev->dev;
182 	rmtfs_mem->dev.groups = qcom_rmtfs_mem_groups;
183 
184 	rmtfs_mem->base = devm_memremap(&rmtfs_mem->dev, rmtfs_mem->addr,
185 					rmtfs_mem->size, MEMREMAP_WC);
186 	if (IS_ERR(rmtfs_mem->base)) {
187 		dev_err(&pdev->dev, "failed to remap rmtfs_mem region\n");
188 		ret = PTR_ERR(rmtfs_mem->base);
189 		goto put_device;
190 	}
191 
192 	cdev_init(&rmtfs_mem->cdev, &qcom_rmtfs_mem_fops);
193 	rmtfs_mem->cdev.owner = THIS_MODULE;
194 
195 	dev_set_name(&rmtfs_mem->dev, "qcom_rmtfs_mem%d", client_id);
196 	rmtfs_mem->dev.id = client_id;
197 	rmtfs_mem->dev.devt = MKDEV(MAJOR(qcom_rmtfs_mem_major), client_id);
198 
199 	ret = cdev_device_add(&rmtfs_mem->cdev, &rmtfs_mem->dev);
200 	if (ret) {
201 		dev_err(&pdev->dev, "failed to add cdev: %d\n", ret);
202 		goto put_device;
203 	}
204 
205 	rmtfs_mem->dev.release = qcom_rmtfs_mem_release_device;
206 
207 	dev_set_drvdata(&pdev->dev, rmtfs_mem);
208 
209 	return 0;
210 
211 put_device:
212 	put_device(&rmtfs_mem->dev);
213 
214 	return ret;
215 }
216 
217 static int qcom_rmtfs_mem_remove(struct platform_device *pdev)
218 {
219 	struct qcom_rmtfs_mem *rmtfs_mem = dev_get_drvdata(&pdev->dev);
220 
221 	cdev_device_del(&rmtfs_mem->cdev, &rmtfs_mem->dev);
222 	put_device(&rmtfs_mem->dev);
223 
224 	return 0;
225 }
226 
227 static const struct of_device_id qcom_rmtfs_mem_of_match[] = {
228 	{ .compatible = "qcom,rmtfs-mem" },
229 	{}
230 };
231 MODULE_DEVICE_TABLE(of, qcom_rmtfs_mem_of_match);
232 
233 static struct platform_driver qcom_rmtfs_mem_driver = {
234 	.probe = qcom_rmtfs_mem_probe,
235 	.remove = qcom_rmtfs_mem_remove,
236 	.driver  = {
237 		.name  = "qcom_rmtfs_mem",
238 		.of_match_table = qcom_rmtfs_mem_of_match,
239 	},
240 };
241 
242 static int qcom_rmtfs_mem_init(void)
243 {
244 	int ret;
245 
246 	ret = alloc_chrdev_region(&qcom_rmtfs_mem_major, 0,
247 				  QCOM_RMTFS_MEM_DEV_MAX, "qcom_rmtfs_mem");
248 	if (ret < 0) {
249 		pr_err("qcom_rmtfs_mem: failed to allocate char dev region\n");
250 		return ret;
251 	}
252 
253 	ret = platform_driver_register(&qcom_rmtfs_mem_driver);
254 	if (ret < 0) {
255 		pr_err("qcom_rmtfs_mem: failed to register rmtfs_mem driver\n");
256 		unregister_chrdev_region(qcom_rmtfs_mem_major,
257 					 QCOM_RMTFS_MEM_DEV_MAX);
258 	}
259 
260 	return ret;
261 }
262 module_init(qcom_rmtfs_mem_init);
263 
264 static void qcom_rmtfs_mem_exit(void)
265 {
266 	platform_driver_unregister(&qcom_rmtfs_mem_driver);
267 	unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
268 }
269 module_exit(qcom_rmtfs_mem_exit);
270