1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright 2019 Google LLC.
5 #include <linux/kernel.h>
6 #include <linux/module.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>
14 #include "rpmsg_internal.h"
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;
22 struct work_struct register_work;
23 struct list_head channels;
24 struct mutex channels_lock;
27 #define to_mtk_subdev(d) container_of(d, struct mtk_rpmsg_rproc_subdev, subdev)
29 struct mtk_rpmsg_channel_info {
30 struct rpmsg_channel_info info;
32 struct list_head list;
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
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).
46 char name[RPMSG_NAME_SIZE];
50 struct mtk_rpmsg_device {
51 struct rpmsg_device rpdev;
52 struct mtk_rpmsg_rproc_subdev *mtk_subdev;
55 struct mtk_rpmsg_endpoint {
56 struct rpmsg_endpoint ept;
57 struct mtk_rpmsg_rproc_subdev *mtk_subdev;
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)
63 static const struct rpmsg_endpoint_ops mtk_rpmsg_endpoint_ops;
65 static void __mtk_ept_release(struct kref *kref)
67 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
69 kfree(to_mtk_rpmsg_endpoint(ept));
72 static void mtk_rpmsg_ipi_handler(void *data, unsigned int len, void *priv)
74 struct mtk_rpmsg_endpoint *mept = priv;
75 struct rpmsg_endpoint *ept = &mept->ept;
78 ret = (*ept->cb)(ept->rpdev, data, len, ept->priv, ept->addr);
80 dev_warn(&ept->rpdev->dev, "rpmsg handler return error = %d",
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,
89 struct mtk_rpmsg_endpoint *mept;
90 struct rpmsg_endpoint *ept;
91 struct platform_device *pdev = mtk_subdev->pdev;
94 mept = kzalloc(sizeof(*mept), GFP_KERNEL);
97 mept->mtk_subdev = mtk_subdev;
100 kref_init(&ept->refcount);
105 ept->ops = &mtk_rpmsg_endpoint_ops;
108 ret = mtk_subdev->info->register_ipi(pdev, id, mtk_rpmsg_ipi_handler,
111 dev_err(&pdev->dev, "IPI register failed, id = %d", id);
112 kref_put(&ept->refcount, __mtk_ept_release);
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)
123 struct mtk_rpmsg_rproc_subdev *mtk_subdev =
124 to_mtk_rpmsg_device(rpdev)->mtk_subdev;
126 return __mtk_create_ept(mtk_subdev, rpdev, cb, priv, chinfo.src);
129 static void mtk_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
131 struct mtk_rpmsg_rproc_subdev *mtk_subdev =
132 to_mtk_rpmsg_endpoint(ept)->mtk_subdev;
134 mtk_subdev->info->unregister_ipi(mtk_subdev->pdev, ept->addr);
135 kref_put(&ept->refcount, __mtk_ept_release);
138 static int mtk_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
140 struct mtk_rpmsg_rproc_subdev *mtk_subdev =
141 to_mtk_rpmsg_endpoint(ept)->mtk_subdev;
143 return mtk_subdev->info->send_ipi(mtk_subdev->pdev, ept->addr, data,
147 static int mtk_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
149 struct mtk_rpmsg_rproc_subdev *mtk_subdev =
150 to_mtk_rpmsg_endpoint(ept)->mtk_subdev;
153 * TODO: This currently is same as mtk_rpmsg_send, and wait until SCP
154 * received the last command.
156 return mtk_subdev->info->send_ipi(mtk_subdev->pdev, ept->addr, data,
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,
166 static void mtk_rpmsg_release_device(struct device *dev)
168 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
169 struct mtk_rpmsg_device *mdev = to_mtk_rpmsg_device(rpdev);
174 static const struct rpmsg_device_ops mtk_rpmsg_device_ops = {
175 .create_ept = mtk_rpmsg_create_ept,
178 static struct device_node *
179 mtk_rpmsg_match_device_subnode(struct device_node *node, const char *channel)
181 struct device_node *child;
185 for_each_available_child_of_node(node, child) {
186 ret = of_property_read_string(child, "mediatek,rpmsg-name", &name);
190 if (strcmp(name, channel) == 0)
197 static int mtk_rpmsg_register_device(struct mtk_rpmsg_rproc_subdev *mtk_subdev,
198 struct rpmsg_channel_info *info)
200 struct rpmsg_device *rpdev;
201 struct mtk_rpmsg_device *mdev;
202 struct platform_device *pdev = mtk_subdev->pdev;
204 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
208 mdev->mtk_subdev = mtk_subdev;
210 rpdev = &mdev->rpdev;
211 rpdev->ops = &mtk_rpmsg_device_ops;
212 rpdev->src = info->src;
213 rpdev->dst = info->dst;
214 strscpy(rpdev->id.name, info->name, RPMSG_NAME_SIZE);
217 mtk_rpmsg_match_device_subnode(pdev->dev.of_node, info->name);
218 rpdev->dev.parent = &pdev->dev;
219 rpdev->dev.release = mtk_rpmsg_release_device;
221 return rpmsg_register_device(rpdev);
224 static void mtk_register_device_work_function(struct work_struct *register_work)
226 struct mtk_rpmsg_rproc_subdev *subdev = container_of(
227 register_work, struct mtk_rpmsg_rproc_subdev, register_work);
228 struct platform_device *pdev = subdev->pdev;
229 struct mtk_rpmsg_channel_info *info;
232 mutex_lock(&subdev->channels_lock);
233 list_for_each_entry(info, &subdev->channels, list) {
234 if (info->registered)
237 mutex_unlock(&subdev->channels_lock);
238 ret = mtk_rpmsg_register_device(subdev, &info->info);
239 mutex_lock(&subdev->channels_lock);
241 dev_err(&pdev->dev, "Can't create rpmsg_device\n");
245 info->registered = true;
247 mutex_unlock(&subdev->channels_lock);
250 static int mtk_rpmsg_create_device(struct mtk_rpmsg_rproc_subdev *mtk_subdev,
251 char *name, u32 addr)
253 struct mtk_rpmsg_channel_info *info;
255 info = kzalloc(sizeof(*info), GFP_KERNEL);
259 strscpy(info->info.name, name, RPMSG_NAME_SIZE);
260 info->info.src = addr;
261 info->info.dst = RPMSG_ADDR_ANY;
262 mutex_lock(&mtk_subdev->channels_lock);
263 list_add(&info->list, &mtk_subdev->channels);
264 mutex_unlock(&mtk_subdev->channels_lock);
266 schedule_work(&mtk_subdev->register_work);
270 static int mtk_rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
273 struct rpmsg_ns_msg *msg = data;
274 struct mtk_rpmsg_rproc_subdev *mtk_subdev = priv;
275 struct device *dev = &mtk_subdev->pdev->dev;
279 if (len != sizeof(*msg)) {
280 dev_err(dev, "malformed ns msg (%d)\n", len);
285 * the name service ept does _not_ belong to a real rpmsg channel,
286 * and is handled by the rpmsg bus itself.
287 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
291 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
295 /* don't trust the remote processor for null terminating the name */
296 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
298 dev_info(dev, "creating channel %s addr 0x%x\n", msg->name, msg->addr);
300 ret = mtk_rpmsg_create_device(mtk_subdev, msg->name, msg->addr);
302 dev_err(dev, "create rpmsg device failed\n");
309 static int mtk_rpmsg_prepare(struct rproc_subdev *subdev)
311 struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
313 /* a dedicated endpoint handles the name service msgs */
314 if (mtk_subdev->info->ns_ipi_id >= 0) {
316 __mtk_create_ept(mtk_subdev, NULL, mtk_rpmsg_ns_cb,
318 mtk_subdev->info->ns_ipi_id);
319 if (!mtk_subdev->ns_ept) {
320 dev_err(&mtk_subdev->pdev->dev,
321 "failed to create name service endpoint\n");
329 static void mtk_rpmsg_unprepare(struct rproc_subdev *subdev)
331 struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
333 if (mtk_subdev->ns_ept) {
334 mtk_rpmsg_destroy_ept(mtk_subdev->ns_ept);
335 mtk_subdev->ns_ept = NULL;
339 static void mtk_rpmsg_stop(struct rproc_subdev *subdev, bool crashed)
341 struct mtk_rpmsg_channel_info *info, *next;
342 struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
343 struct device *dev = &mtk_subdev->pdev->dev;
346 * Destroy the name service endpoint here, to avoid new channel being
347 * created after the rpmsg_unregister_device loop below.
349 if (mtk_subdev->ns_ept) {
350 mtk_rpmsg_destroy_ept(mtk_subdev->ns_ept);
351 mtk_subdev->ns_ept = NULL;
354 cancel_work_sync(&mtk_subdev->register_work);
356 mutex_lock(&mtk_subdev->channels_lock);
357 list_for_each_entry(info, &mtk_subdev->channels, list) {
358 if (!info->registered)
360 if (rpmsg_unregister_device(dev, &info->info)) {
363 "rpmsg_unregister_device failed for %s.%d.%d\n",
364 info->info.name, info->info.src,
369 list_for_each_entry_safe(info, next,
370 &mtk_subdev->channels, list) {
371 list_del(&info->list);
374 mutex_unlock(&mtk_subdev->channels_lock);
377 struct rproc_subdev *
378 mtk_rpmsg_create_rproc_subdev(struct platform_device *pdev,
379 struct mtk_rpmsg_info *info)
381 struct mtk_rpmsg_rproc_subdev *mtk_subdev;
383 mtk_subdev = kzalloc(sizeof(*mtk_subdev), GFP_KERNEL);
387 mtk_subdev->pdev = pdev;
388 mtk_subdev->subdev.prepare = mtk_rpmsg_prepare;
389 mtk_subdev->subdev.stop = mtk_rpmsg_stop;
390 mtk_subdev->subdev.unprepare = mtk_rpmsg_unprepare;
391 mtk_subdev->info = info;
392 INIT_LIST_HEAD(&mtk_subdev->channels);
393 INIT_WORK(&mtk_subdev->register_work,
394 mtk_register_device_work_function);
395 mutex_init(&mtk_subdev->channels_lock);
397 return &mtk_subdev->subdev;
399 EXPORT_SYMBOL_GPL(mtk_rpmsg_create_rproc_subdev);
401 void mtk_rpmsg_destroy_rproc_subdev(struct rproc_subdev *subdev)
403 struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
407 EXPORT_SYMBOL_GPL(mtk_rpmsg_destroy_rproc_subdev);
409 MODULE_LICENSE("GPL v2");
410 MODULE_DESCRIPTION("MediaTek scp rpmsg driver");