xref: /openbmc/linux/drivers/rpmsg/mtk_rpmsg.c (revision 3d820cd4)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright 2019 Google LLC.
4 
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/of.h>
8 #include <linux/platform_device.h>
9 #include <linux/remoteproc.h>
10 #include <linux/rpmsg/mtk_rpmsg.h>
11 #include <linux/slab.h>
12 #include <linux/workqueue.h>
13 
14 #include "rpmsg_internal.h"
15 
16 struct mtk_rpmsg_rproc_subdev {
17 	struct platform_device *pdev;
18 	struct mtk_rpmsg_info *info;
19 	struct rpmsg_endpoint *ns_ept;
20 	struct rproc_subdev subdev;
21 
22 	struct work_struct register_work;
23 	struct list_head channels;
24 	struct mutex channels_lock;
25 };
26 
27 #define to_mtk_subdev(d) container_of(d, struct mtk_rpmsg_rproc_subdev, subdev)
28 
29 struct mtk_rpmsg_channel_info {
30 	struct rpmsg_channel_info info;
31 	bool registered;
32 	struct list_head list;
33 };
34 
35 /**
36  * struct rpmsg_ns_msg - dynamic name service announcement message
37  * @name: name of remote service that is published
38  * @addr: address of remote service that is published
39  *
40  * This message is sent across to publish a new service. When we receive these
41  * messages, an appropriate rpmsg channel (i.e device) is created. In turn, the
42  * ->probe() handler of the appropriate rpmsg driver will be invoked
43  *  (if/as-soon-as one is registered).
44  */
45 struct rpmsg_ns_msg {
46 	char name[RPMSG_NAME_SIZE];
47 	u32 addr;
48 } __packed;
49 
50 struct mtk_rpmsg_device {
51 	struct rpmsg_device rpdev;
52 	struct mtk_rpmsg_rproc_subdev *mtk_subdev;
53 };
54 
55 struct mtk_rpmsg_endpoint {
56 	struct rpmsg_endpoint ept;
57 	struct mtk_rpmsg_rproc_subdev *mtk_subdev;
58 };
59 
60 #define to_mtk_rpmsg_device(r) container_of(r, struct mtk_rpmsg_device, rpdev)
61 #define to_mtk_rpmsg_endpoint(r) container_of(r, struct mtk_rpmsg_endpoint, ept)
62 
63 static const struct rpmsg_endpoint_ops mtk_rpmsg_endpoint_ops;
64 
65 static void __mtk_ept_release(struct kref *kref)
66 {
67 	struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
68 						  refcount);
69 	kfree(to_mtk_rpmsg_endpoint(ept));
70 }
71 
72 static void mtk_rpmsg_ipi_handler(void *data, unsigned int len, void *priv)
73 {
74 	struct mtk_rpmsg_endpoint *mept = priv;
75 	struct rpmsg_endpoint *ept = &mept->ept;
76 	int ret;
77 
78 	ret = (*ept->cb)(ept->rpdev, data, len, ept->priv, ept->addr);
79 	if (ret)
80 		dev_warn(&ept->rpdev->dev, "rpmsg handler return error = %d",
81 			 ret);
82 }
83 
84 static struct rpmsg_endpoint *
85 __mtk_create_ept(struct mtk_rpmsg_rproc_subdev *mtk_subdev,
86 		 struct rpmsg_device *rpdev, rpmsg_rx_cb_t cb, void *priv,
87 		 u32 id)
88 {
89 	struct mtk_rpmsg_endpoint *mept;
90 	struct rpmsg_endpoint *ept;
91 	struct platform_device *pdev = mtk_subdev->pdev;
92 	int ret;
93 
94 	mept = kzalloc(sizeof(*mept), GFP_KERNEL);
95 	if (!mept)
96 		return NULL;
97 	mept->mtk_subdev = mtk_subdev;
98 
99 	ept = &mept->ept;
100 	kref_init(&ept->refcount);
101 
102 	ept->rpdev = rpdev;
103 	ept->cb = cb;
104 	ept->priv = priv;
105 	ept->ops = &mtk_rpmsg_endpoint_ops;
106 	ept->addr = id;
107 
108 	ret = mtk_subdev->info->register_ipi(pdev, id, mtk_rpmsg_ipi_handler,
109 					     mept);
110 	if (ret) {
111 		dev_err(&pdev->dev, "IPI register failed, id = %d", id);
112 		kref_put(&ept->refcount, __mtk_ept_release);
113 		return NULL;
114 	}
115 
116 	return ept;
117 }
118 
119 static struct rpmsg_endpoint *
120 mtk_rpmsg_create_ept(struct rpmsg_device *rpdev, rpmsg_rx_cb_t cb, void *priv,
121 		     struct rpmsg_channel_info chinfo)
122 {
123 	struct mtk_rpmsg_rproc_subdev *mtk_subdev =
124 		to_mtk_rpmsg_device(rpdev)->mtk_subdev;
125 
126 	return __mtk_create_ept(mtk_subdev, rpdev, cb, priv, chinfo.src);
127 }
128 
129 static void mtk_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
130 {
131 	struct mtk_rpmsg_rproc_subdev *mtk_subdev =
132 		to_mtk_rpmsg_endpoint(ept)->mtk_subdev;
133 
134 	mtk_subdev->info->unregister_ipi(mtk_subdev->pdev, ept->addr);
135 	kref_put(&ept->refcount, __mtk_ept_release);
136 }
137 
138 static int mtk_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
139 {
140 	struct mtk_rpmsg_rproc_subdev *mtk_subdev =
141 		to_mtk_rpmsg_endpoint(ept)->mtk_subdev;
142 
143 	return mtk_subdev->info->send_ipi(mtk_subdev->pdev, ept->addr, data,
144 					  len, 0);
145 }
146 
147 static int mtk_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
148 {
149 	struct mtk_rpmsg_rproc_subdev *mtk_subdev =
150 		to_mtk_rpmsg_endpoint(ept)->mtk_subdev;
151 
152 	/*
153 	 * TODO: This currently is same as mtk_rpmsg_send, and wait until SCP
154 	 * received the last command.
155 	 */
156 	return mtk_subdev->info->send_ipi(mtk_subdev->pdev, ept->addr, data,
157 					  len, 0);
158 }
159 
160 static const struct rpmsg_endpoint_ops mtk_rpmsg_endpoint_ops = {
161 	.destroy_ept = mtk_rpmsg_destroy_ept,
162 	.send = mtk_rpmsg_send,
163 	.trysend = mtk_rpmsg_trysend,
164 };
165 
166 static void mtk_rpmsg_release_device(struct device *dev)
167 {
168 	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
169 	struct mtk_rpmsg_device *mdev = to_mtk_rpmsg_device(rpdev);
170 
171 	kfree(mdev);
172 }
173 
174 static const struct rpmsg_device_ops mtk_rpmsg_device_ops = {
175 	.create_ept = mtk_rpmsg_create_ept,
176 };
177 
178 static struct device_node *
179 mtk_rpmsg_match_device_subnode(struct device_node *node, const char *channel)
180 {
181 	struct device_node *child;
182 	const char *name;
183 	int ret;
184 
185 	for_each_available_child_of_node(node, child) {
186 		ret = of_property_read_string(child, "mtk,rpmsg-name", &name);
187 		if (ret)
188 			continue;
189 
190 		if (strcmp(name, channel) == 0)
191 			return child;
192 	}
193 
194 	return NULL;
195 }
196 
197 static int mtk_rpmsg_register_device(struct mtk_rpmsg_rproc_subdev *mtk_subdev,
198 				     struct rpmsg_channel_info *info)
199 {
200 	struct rpmsg_device *rpdev;
201 	struct mtk_rpmsg_device *mdev;
202 	struct platform_device *pdev = mtk_subdev->pdev;
203 	int ret;
204 
205 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
206 	if (!mdev)
207 		return -ENOMEM;
208 
209 	mdev->mtk_subdev = mtk_subdev;
210 
211 	rpdev = &mdev->rpdev;
212 	rpdev->ops = &mtk_rpmsg_device_ops;
213 	rpdev->src = info->src;
214 	rpdev->dst = info->dst;
215 	strscpy(rpdev->id.name, info->name, RPMSG_NAME_SIZE);
216 
217 	rpdev->dev.of_node =
218 		mtk_rpmsg_match_device_subnode(pdev->dev.of_node, info->name);
219 	rpdev->dev.parent = &pdev->dev;
220 	rpdev->dev.release = mtk_rpmsg_release_device;
221 
222 	ret = rpmsg_register_device(rpdev);
223 	if (ret) {
224 		kfree(mdev);
225 		return ret;
226 	}
227 
228 	return 0;
229 }
230 
231 static void mtk_register_device_work_function(struct work_struct *register_work)
232 {
233 	struct mtk_rpmsg_rproc_subdev *subdev = container_of(
234 		register_work, struct mtk_rpmsg_rproc_subdev, register_work);
235 	struct platform_device *pdev = subdev->pdev;
236 	struct mtk_rpmsg_channel_info *info;
237 	int ret;
238 
239 	mutex_lock(&subdev->channels_lock);
240 	list_for_each_entry(info, &subdev->channels, list) {
241 		if (info->registered)
242 			continue;
243 
244 		ret = mtk_rpmsg_register_device(subdev, &info->info);
245 		if (ret) {
246 			dev_err(&pdev->dev, "Can't create rpmsg_device\n");
247 			continue;
248 		}
249 
250 		info->registered = true;
251 	}
252 	mutex_unlock(&subdev->channels_lock);
253 }
254 
255 static int mtk_rpmsg_create_device(struct mtk_rpmsg_rproc_subdev *mtk_subdev,
256 				   char *name, u32 addr)
257 {
258 	struct mtk_rpmsg_channel_info *info;
259 
260 	info = kzalloc(sizeof(*info), GFP_KERNEL);
261 	if (!info)
262 		return -ENOMEM;
263 
264 	strscpy(info->info.name, name, RPMSG_NAME_SIZE);
265 	info->info.src = addr;
266 	info->info.dst = RPMSG_ADDR_ANY;
267 	mutex_lock(&mtk_subdev->channels_lock);
268 	list_add(&info->list, &mtk_subdev->channels);
269 	mutex_unlock(&mtk_subdev->channels_lock);
270 
271 	schedule_work(&mtk_subdev->register_work);
272 	return 0;
273 }
274 
275 static int mtk_rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
276 			   void *priv, u32 src)
277 {
278 	struct rpmsg_ns_msg *msg = data;
279 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = priv;
280 	struct device *dev = &mtk_subdev->pdev->dev;
281 
282 	int ret;
283 
284 	if (len != sizeof(*msg)) {
285 		dev_err(dev, "malformed ns msg (%d)\n", len);
286 		return -EINVAL;
287 	}
288 
289 	/*
290 	 * the name service ept does _not_ belong to a real rpmsg channel,
291 	 * and is handled by the rpmsg bus itself.
292 	 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
293 	 * in somehow.
294 	 */
295 	if (rpdev) {
296 		dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
297 		return -EINVAL;
298 	}
299 
300 	/* don't trust the remote processor for null terminating the name */
301 	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
302 
303 	dev_info(dev, "creating channel %s addr 0x%x\n", msg->name, msg->addr);
304 
305 	ret = mtk_rpmsg_create_device(mtk_subdev, msg->name, msg->addr);
306 	if (ret) {
307 		dev_err(dev, "create rpmsg device failed\n");
308 		return ret;
309 	}
310 
311 	return 0;
312 }
313 
314 static int mtk_rpmsg_prepare(struct rproc_subdev *subdev)
315 {
316 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
317 
318 	/* a dedicated endpoint handles the name service msgs */
319 	if (mtk_subdev->info->ns_ipi_id >= 0) {
320 		mtk_subdev->ns_ept =
321 			__mtk_create_ept(mtk_subdev, NULL, mtk_rpmsg_ns_cb,
322 					 mtk_subdev,
323 					 mtk_subdev->info->ns_ipi_id);
324 		if (!mtk_subdev->ns_ept) {
325 			dev_err(&mtk_subdev->pdev->dev,
326 				"failed to create name service endpoint\n");
327 			return -ENOMEM;
328 		}
329 	}
330 
331 	return 0;
332 }
333 
334 static void mtk_rpmsg_unprepare(struct rproc_subdev *subdev)
335 {
336 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
337 
338 	if (mtk_subdev->ns_ept) {
339 		mtk_rpmsg_destroy_ept(mtk_subdev->ns_ept);
340 		mtk_subdev->ns_ept = NULL;
341 	}
342 }
343 
344 static void mtk_rpmsg_stop(struct rproc_subdev *subdev, bool crashed)
345 {
346 	struct mtk_rpmsg_channel_info *info, *next;
347 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
348 	struct device *dev = &mtk_subdev->pdev->dev;
349 
350 	/*
351 	 * Destroy the name service endpoint here, to avoid new channel being
352 	 * created after the rpmsg_unregister_device loop below.
353 	 */
354 	if (mtk_subdev->ns_ept) {
355 		mtk_rpmsg_destroy_ept(mtk_subdev->ns_ept);
356 		mtk_subdev->ns_ept = NULL;
357 	}
358 
359 	cancel_work_sync(&mtk_subdev->register_work);
360 
361 	mutex_lock(&mtk_subdev->channels_lock);
362 	list_for_each_entry(info, &mtk_subdev->channels, list) {
363 		if (!info->registered)
364 			continue;
365 		if (rpmsg_unregister_device(dev, &info->info)) {
366 			dev_warn(
367 				dev,
368 				"rpmsg_unregister_device failed for %s.%d.%d\n",
369 				info->info.name, info->info.src,
370 				info->info.dst);
371 		}
372 	}
373 
374 	list_for_each_entry_safe(info, next,
375 				 &mtk_subdev->channels, list) {
376 		list_del(&info->list);
377 		kfree(info);
378 	}
379 	mutex_unlock(&mtk_subdev->channels_lock);
380 }
381 
382 struct rproc_subdev *
383 mtk_rpmsg_create_rproc_subdev(struct platform_device *pdev,
384 			      struct mtk_rpmsg_info *info)
385 {
386 	struct mtk_rpmsg_rproc_subdev *mtk_subdev;
387 
388 	mtk_subdev = kzalloc(sizeof(*mtk_subdev), GFP_KERNEL);
389 	if (!mtk_subdev)
390 		return NULL;
391 
392 	mtk_subdev->pdev = pdev;
393 	mtk_subdev->subdev.prepare = mtk_rpmsg_prepare;
394 	mtk_subdev->subdev.stop = mtk_rpmsg_stop;
395 	mtk_subdev->subdev.unprepare = mtk_rpmsg_unprepare;
396 	mtk_subdev->info = info;
397 	INIT_LIST_HEAD(&mtk_subdev->channels);
398 	INIT_WORK(&mtk_subdev->register_work,
399 		  mtk_register_device_work_function);
400 	mutex_init(&mtk_subdev->channels_lock);
401 
402 	return &mtk_subdev->subdev;
403 }
404 EXPORT_SYMBOL_GPL(mtk_rpmsg_create_rproc_subdev);
405 
406 void mtk_rpmsg_destroy_rproc_subdev(struct rproc_subdev *subdev)
407 {
408 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
409 
410 	kfree(mtk_subdev);
411 }
412 EXPORT_SYMBOL_GPL(mtk_rpmsg_destroy_rproc_subdev);
413 
414 MODULE_LICENSE("GPL v2");
415 MODULE_DESCRIPTION("MediaTek scp rpmsg driver");
416