projects/linux·blob uaccess-cleanups · refs

636 lines · 17.2 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 * remote processor messaging bus
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2011 Google, Inc.
7 *
8 * Ohad Ben-Cohen <[email protected]>
9 * Brian Swetland <[email protected]>
10 */
11
12#define pr_fmt(fmt) "%s: " fmt, __func__
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/rpmsg.h>
17#include <linux/of_device.h>
18#include <linux/pm_domain.h>
19#include <linux/slab.h>
20
21#include "rpmsg_internal.h"
22
23const struct class rpmsg_class = {
24 .name = "rpmsg",
25};
26EXPORT_SYMBOL(rpmsg_class);
27
28/**
29 * rpmsg_create_channel() - create a new rpmsg channel
30 * using its name and address info.
31 * @rpdev: rpmsg device
32 * @chinfo: channel_info to bind
33 *
34 * Return: a pointer to the new rpmsg device on success, or NULL on error.
35 */
36struct rpmsg_device *rpmsg_create_channel(struct rpmsg_device *rpdev,
37 struct rpmsg_channel_info *chinfo)
38{
39 if (WARN_ON(!rpdev))
40 return NULL;
41 if (!rpdev->ops || !rpdev->ops->create_channel) {
42 dev_err(&rpdev->dev, "no create_channel ops found\n");
43 return NULL;
44 }
45
46 return rpdev->ops->create_channel(rpdev, chinfo);
47}
48EXPORT_SYMBOL(rpmsg_create_channel);
49
50/**
51 * rpmsg_release_channel() - release a rpmsg channel
52 * using its name and address info.
53 * @rpdev: rpmsg device
54 * @chinfo: channel_info to bind
55 *
56 * Return: 0 on success or an appropriate error value.
57 */
58int rpmsg_release_channel(struct rpmsg_device *rpdev,
59 struct rpmsg_channel_info *chinfo)
60{
61 if (WARN_ON(!rpdev))
62 return -EINVAL;
63 if (!rpdev->ops || !rpdev->ops->release_channel) {
64 dev_err(&rpdev->dev, "no release_channel ops found\n");
65 return -ENXIO;
66 }
67
68 return rpdev->ops->release_channel(rpdev, chinfo);
69}
70EXPORT_SYMBOL(rpmsg_release_channel);
71
72/**
73 * rpmsg_create_ept() - create a new rpmsg_endpoint
74 * @rpdev: rpmsg channel device
75 * @cb: rx callback handler
76 * @priv: private data for the driver's use
77 * @chinfo: channel_info with the local rpmsg address to bind with @cb
78 *
79 * Every rpmsg address in the system is bound to an rx callback (so when
80 * inbound messages arrive, they are dispatched by the rpmsg bus using the
81 * appropriate callback handler) by means of an rpmsg_endpoint struct.
82 *
83 * This function allows drivers to create such an endpoint, and by that,
84 * bind a callback, and possibly some private data too, to an rpmsg address
85 * (either one that is known in advance, or one that will be dynamically
86 * assigned for them).
87 *
88 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
89 * is already created for them when they are probed by the rpmsg bus
90 * (using the rx callback provided when they registered to the rpmsg bus).
91 *
92 * So things should just work for simple drivers: they already have an
93 * endpoint, their rx callback is bound to their rpmsg address, and when
94 * relevant inbound messages arrive (i.e. messages which their dst address
95 * equals to the src address of their rpmsg channel), the driver's handler
96 * is invoked to process it.
97 *
98 * That said, more complicated drivers might need to allocate
99 * additional rpmsg addresses, and bind them to different rx callbacks.
100 * To accomplish that, those drivers need to call this function.
101 *
102 * Drivers should provide their @rpdev channel (so the new endpoint would belong
103 * to the same remote processor their channel belongs to), an rx callback
104 * function, an optional private data (which is provided back when the
105 * rx callback is invoked), and an address they want to bind with the
106 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
107 * dynamically assign them an available rpmsg address (drivers should have
108 * a very good reason why not to always use RPMSG_ADDR_ANY here).
109 *
110 * Return: a pointer to the endpoint on success, or NULL on error.
111 */
112struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
113 rpmsg_rx_cb_t cb, void *priv,
114 struct rpmsg_channel_info chinfo)
115{
116 if (WARN_ON(!rpdev))
117 return NULL;
118
119 return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
120}
121EXPORT_SYMBOL(rpmsg_create_ept);
122
123/**
124 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
125 * @ept: endpoing to destroy
126 *
127 * Should be used by drivers to destroy an rpmsg endpoint previously
128 * created with rpmsg_create_ept(). As with other types of "free" NULL
129 * is a valid parameter.
130 */
131void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
132{
133 if (ept && ept->ops)
134 ept->ops->destroy_ept(ept);
135}
136EXPORT_SYMBOL(rpmsg_destroy_ept);
137
138/**
139 * rpmsg_send() - send a message across to the remote processor
140 * @ept: the rpmsg endpoint
141 * @data: payload of message
142 * @len: length of payload
143 *
144 * This function sends @data of length @len on the @ept endpoint.
145 * The message will be sent to the remote processor which the @ept
146 * endpoint belongs to, using @ept's address and its associated rpmsg
147 * device destination addresses.
148 * In case there are no TX buffers available, the function will block until
149 * one becomes available, or a timeout of 15 seconds elapses. When the latter
150 * happens, -ERESTARTSYS is returned.
151 *
152 * Can only be called from process context (for now).
153 *
154 * Return: 0 on success and an appropriate error value on failure.
155 */
156int rpmsg_send(struct rpmsg_endpoint *ept, const void *data, int len)
157{
158 if (WARN_ON(!ept))
159 return -EINVAL;
160 if (!ept->ops->send)
161 return -ENXIO;
162
163 return ept->ops->send(ept, data, len);
164}
165EXPORT_SYMBOL(rpmsg_send);
166
167/**
168 * rpmsg_sendto() - send a message across to the remote processor, specify dst
169 * @ept: the rpmsg endpoint
170 * @data: payload of message
171 * @len: length of payload
172 * @dst: destination address
173 *
174 * This function sends @data of length @len to the remote @dst address.
175 * The message will be sent to the remote processor which the @ept
176 * endpoint belongs to, using @ept's address as source.
177 * In case there are no TX buffers available, the function will block until
178 * one becomes available, or a timeout of 15 seconds elapses. When the latter
179 * happens, -ERESTARTSYS is returned.
180 *
181 * Can only be called from process context (for now).
182 *
183 * Return: 0 on success and an appropriate error value on failure.
184 */
185int rpmsg_sendto(struct rpmsg_endpoint *ept, const void *data, int len, u32 dst)
186{
187 if (WARN_ON(!ept))
188 return -EINVAL;
189 if (!ept->ops->sendto)
190 return -ENXIO;
191
192 return ept->ops->sendto(ept, data, len, dst);
193}
194EXPORT_SYMBOL(rpmsg_sendto);
195
196/**
197 * rpmsg_trysend() - send a message across to the remote processor
198 * @ept: the rpmsg endpoint
199 * @data: payload of message
200 * @len: length of payload
201 *
202 * This function sends @data of length @len on the @ept endpoint.
203 * The message will be sent to the remote processor which the @ept
204 * endpoint belongs to, using @ept's address as source and its associated
205 * rpdev's address as destination.
206 * In case there are no TX buffers available, the function will immediately
207 * return -ENOMEM without waiting until one becomes available.
208 *
209 * Can only be called from process context (for now).
210 *
211 * Return: 0 on success and an appropriate error value on failure.
212 */
213int rpmsg_trysend(struct rpmsg_endpoint *ept, const void *data, int len)
214{
215 if (WARN_ON(!ept))
216 return -EINVAL;
217 if (!ept->ops->trysend)
218 return -ENXIO;
219
220 return ept->ops->trysend(ept, data, len);
221}
222EXPORT_SYMBOL(rpmsg_trysend);
223
224/**
225 * rpmsg_trysendto() - send a message across to the remote processor, specify dst
226 * @ept: the rpmsg endpoint
227 * @data: payload of message
228 * @len: length of payload
229 * @dst: destination address
230 *
231 * This function sends @data of length @len to the remote @dst address.
232 * The message will be sent to the remote processor which the @ept
233 * endpoint belongs to, using @ept's address as source.
234 * In case there are no TX buffers available, the function will immediately
235 * return -ENOMEM without waiting until one becomes available.
236 *
237 * Can only be called from process context (for now).
238 *
239 * Return: 0 on success and an appropriate error value on failure.
240 */
241int rpmsg_trysendto(struct rpmsg_endpoint *ept, const void *data, int len, u32 dst)
242{
243 if (WARN_ON(!ept))
244 return -EINVAL;
245 if (!ept->ops->trysendto)
246 return -ENXIO;
247
248 return ept->ops->trysendto(ept, data, len, dst);
249}
250EXPORT_SYMBOL(rpmsg_trysendto);
251
252/**
253 * rpmsg_poll() - poll the endpoint's send buffers
254 * @ept: the rpmsg endpoint
255 * @filp: file for poll_wait()
256 * @wait: poll_table for poll_wait()
257 *
258 * Return: mask representing the current state of the endpoint's send buffers
259 */
260__poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
261 poll_table *wait)
262{
263 if (WARN_ON(!ept))
264 return 0;
265 if (!ept->ops->poll)
266 return 0;
267
268 return ept->ops->poll(ept, filp, wait);
269}
270EXPORT_SYMBOL(rpmsg_poll);
271
272/**
273 * rpmsg_set_flow_control() - request remote to pause/resume transmission
274 * @ept: the rpmsg endpoint
275 * @pause: pause transmission
276 * @dst: destination address of the endpoint
277 *
278 * Return: 0 on success and an appropriate error value on failure.
279 */
280int rpmsg_set_flow_control(struct rpmsg_endpoint *ept, bool pause, u32 dst)
281{
282 if (WARN_ON(!ept))
283 return -EINVAL;
284 if (!ept->ops->set_flow_control)
285 return -EOPNOTSUPP;
286
287 return ept->ops->set_flow_control(ept, pause, dst);
288}
289EXPORT_SYMBOL_GPL(rpmsg_set_flow_control);
290
291/**
292 * rpmsg_get_mtu() - get maximum transmission buffer size for sending message.
293 * @ept: the rpmsg endpoint
294 *
295 * This function returns maximum buffer size available for a single outgoing message.
296 *
297 * Return: the maximum transmission size on success and an appropriate error
298 * value on failure.
299 */
300
301ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept)
302{
303 if (WARN_ON(!ept))
304 return -EINVAL;
305 if (!ept->ops->get_mtu)
306 return -ENOTSUPP;
307
308 return ept->ops->get_mtu(ept);
309}
310EXPORT_SYMBOL(rpmsg_get_mtu);
311
312/*
313 * match a rpmsg channel with a channel info struct.
314 * this is used to make sure we're not creating rpmsg devices for channels
315 * that already exist.
316 */
317static int rpmsg_device_match(struct device *dev, const void *data)
318{
319 const struct rpmsg_channel_info *chinfo = data;
320 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
321
322 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
323 return 0;
324
325 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
326 return 0;
327
328 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
329 return 0;
330
331 /* found a match ! */
332 return 1;
333}
334
335struct device *rpmsg_find_device(struct device *parent,
336 struct rpmsg_channel_info *chinfo)
337{
338 return device_find_child(parent, chinfo, rpmsg_device_match);
339
340}
341EXPORT_SYMBOL(rpmsg_find_device);
342
343/* sysfs show configuration fields */
344#define rpmsg_show_attr(field, path, format_string) \
345static ssize_t \
346field##_show(struct device *dev, \
347 struct device_attribute *attr, char *buf) \
348{ \
349 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
350 \
351 return sysfs_emit(buf, format_string, rpdev->path); \
352} \
353static DEVICE_ATTR_RO(field);
354
355/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
356rpmsg_show_attr(name, id.name, "%s\n");
357rpmsg_show_attr(src, src, "0x%x\n");
358rpmsg_show_attr(dst, dst, "0x%x\n");
359rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
360
361static ssize_t modalias_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
364 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
365 ssize_t len;
366
367 len = of_device_modalias(dev, buf, PAGE_SIZE);
368 if (len != -ENODEV)
369 return len;
370
371 return sysfs_emit(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
372}
373static DEVICE_ATTR_RO(modalias);
374
375static struct attribute *rpmsg_dev_attrs[] = {
376 &dev_attr_name.attr,
377 &dev_attr_modalias.attr,
378 &dev_attr_dst.attr,
379 &dev_attr_src.attr,
380 &dev_attr_announce.attr,
381 NULL,
382};
383ATTRIBUTE_GROUPS(rpmsg_dev);
384
385/* rpmsg devices and drivers are matched using the service name */
386static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
387 const struct rpmsg_device_id *id)
388{
389 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
390}
391
392/* match rpmsg channel and rpmsg driver */
393static int rpmsg_dev_match(struct device *dev, const struct device_driver *drv)
394{
395 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
396 const struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
397 const struct rpmsg_device_id *ids = rpdrv->id_table;
398 unsigned int i;
399 int ret;
400
401 ret = device_match_driver_override(dev, drv);
402 if (ret >= 0)
403 return ret;
404
405 if (ids)
406 for (i = 0; ids[i].name[0]; i++)
407 if (rpmsg_id_match(rpdev, &ids[i])) {
408 rpdev->id.driver_data = ids[i].driver_data;
409 return 1;
410 }
411
412 return of_driver_match_device(dev, drv);
413}
414
415static int rpmsg_uevent(const struct device *dev, struct kobj_uevent_env *env)
416{
417 const struct rpmsg_device *rpdev = to_rpmsg_device(dev);
418 int ret;
419
420 ret = of_device_uevent_modalias(dev, env);
421 if (ret != -ENODEV)
422 return ret;
423
424 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
425 rpdev->id.name);
426}
427
428/*
429 * when an rpmsg driver is probed with a channel, we seamlessly create
430 * it an endpoint, binding its rx callback to a unique local rpmsg
431 * address.
432 *
433 * if we need to, we also announce about this channel to the remote
434 * processor (needed in case the driver is exposing an rpmsg service).
435 */
436static int rpmsg_dev_probe(struct device *dev)
437{
438 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
439 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
440 struct rpmsg_channel_info chinfo = {};
441 struct rpmsg_endpoint *ept = NULL;
442 int err;
443
444 err = dev_pm_domain_attach(dev, PD_FLAG_ATTACH_POWER_ON |
445 PD_FLAG_DETACH_POWER_OFF);
446 if (err)
447 goto out;
448
449 if (rpdrv->callback) {
450 strscpy(chinfo.name, rpdev->id.name, sizeof(chinfo.name));
451 chinfo.src = rpdev->src;
452 chinfo.dst = RPMSG_ADDR_ANY;
453
454 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
455 if (!ept) {
456 dev_err(dev, "failed to create endpoint\n");
457 err = -ENOMEM;
458 goto out;
459 }
460
461 rpdev->ept = ept;
462 rpdev->src = ept->addr;
463
464 ept->flow_cb = rpdrv->flowcontrol;
465 }
466
467 err = rpdrv->probe(rpdev);
468 if (err) {
469 dev_err(dev, "%s: failed: %d\n", __func__, err);
470 goto destroy_ept;
471 }
472
473 if (ept && rpdev->ops->announce_create) {
474 err = rpdev->ops->announce_create(rpdev);
475 if (err) {
476 dev_err(dev, "failed to announce creation\n");
477 goto remove_rpdev;
478 }
479 }
480
481 return 0;
482
483remove_rpdev:
484 if (rpdrv->remove)
485 rpdrv->remove(rpdev);
486destroy_ept:
487 if (ept)
488 rpmsg_destroy_ept(ept);
489out:
490 return err;
491}
492
493static void rpmsg_dev_remove(struct device *dev)
494{
495 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
496 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
497
498 if (rpdev->ops->announce_destroy)
499 rpdev->ops->announce_destroy(rpdev);
500
501 if (rpdrv->remove)
502 rpdrv->remove(rpdev);
503
504 if (rpdev->ept)
505 rpmsg_destroy_ept(rpdev->ept);
506}
507
508static const struct bus_type rpmsg_bus = {
509 .name = "rpmsg",
510 .match = rpmsg_dev_match,
511 .dev_groups = rpmsg_dev_groups,
512 .driver_override = true,
513 .uevent = rpmsg_uevent,
514 .probe = rpmsg_dev_probe,
515 .remove = rpmsg_dev_remove,
516};
517
518/*
519 * A helper for registering rpmsg device with driver override and name.
520 * Drivers should not be using it, but instead rpmsg_register_device().
521 */
522int rpmsg_register_device_override(struct rpmsg_device *rpdev,
523 const char *driver_override)
524{
525 struct device *dev = &rpdev->dev;
526 int ret;
527
528 if (driver_override)
529 strscpy_pad(rpdev->id.name, driver_override, RPMSG_NAME_SIZE);
530
531 dev_set_name(dev, "%s.%s.%d.%d", dev_name(dev->parent),
532 rpdev->id.name, rpdev->src, rpdev->dst);
533
534 dev->bus = &rpmsg_bus;
535
536 device_initialize(dev);
537 if (driver_override) {
538 ret = device_set_driver_override(dev, driver_override);
539 if (ret) {
540 dev_err(dev, "device_set_driver_override() failed: %d\n", ret);
541 put_device(dev);
542 return ret;
543 }
544 }
545
546 ret = device_add(dev);
547 if (ret) {
548 dev_err(dev, "device_add failed: %d\n", ret);
549 put_device(dev);
550 }
551
552 return ret;
553}
554EXPORT_SYMBOL(rpmsg_register_device_override);
555
556int rpmsg_register_device(struct rpmsg_device *rpdev)
557{
558 return rpmsg_register_device_override(rpdev, NULL);
559}
560EXPORT_SYMBOL(rpmsg_register_device);
561
562/*
563 * find an existing channel using its name + address properties,
564 * and destroy it
565 */
566int rpmsg_unregister_device(struct device *parent,
567 struct rpmsg_channel_info *chinfo)
568{
569 struct device *dev;
570
571 dev = rpmsg_find_device(parent, chinfo);
572 if (!dev)
573 return -EINVAL;
574
575 device_unregister(dev);
576
577 put_device(dev);
578
579 return 0;
580}
581EXPORT_SYMBOL(rpmsg_unregister_device);
582
583/**
584 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
585 * @rpdrv: pointer to a struct rpmsg_driver
586 * @owner: owning module/driver
587 *
588 * Return: 0 on success, and an appropriate error value on failure.
589 */
590int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
591{
592 rpdrv->drv.bus = &rpmsg_bus;
593 rpdrv->drv.owner = owner;
594 return driver_register(&rpdrv->drv);
595}
596EXPORT_SYMBOL(__register_rpmsg_driver);
597
598/**
599 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
600 * @rpdrv: pointer to a struct rpmsg_driver
601 */
602void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
603{
604 driver_unregister(&rpdrv->drv);
605}
606EXPORT_SYMBOL(unregister_rpmsg_driver);
607
608
609static int __init rpmsg_init(void)
610{
611 int ret;
612
613 ret = class_register(&rpmsg_class);
614 if (ret) {
615 pr_err("failed to register rpmsg class\n");
616 return ret;
617 }
618
619 ret = bus_register(&rpmsg_bus);
620 if (ret) {
621 pr_err("failed to register rpmsg bus: %d\n", ret);
622 class_unregister(&rpmsg_class);
623 }
624 return ret;
625}
626postcore_initcall(rpmsg_init);
627
628static void __exit rpmsg_fini(void)
629{
630 bus_unregister(&rpmsg_bus);
631 class_unregister(&rpmsg_class);
632}
633module_exit(rpmsg_fini);
634
635MODULE_DESCRIPTION("remote processor messaging bus");
636MODULE_LICENSE("GPL v2");