]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/rpmsg/rpmsg_core.c
efi: print unrecognized CPER section
[mirror_ubuntu-zesty-kernel.git] / drivers / rpmsg / rpmsg_core.c
1 /*
2 * remote processor messaging bus
3 *
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
6 *
7 * Ohad Ben-Cohen <ohad@wizery.com>
8 * Brian Swetland <swetland@google.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20 #define pr_fmt(fmt) "%s: " fmt, __func__
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/rpmsg.h>
25 #include <linux/of_device.h>
26 #include <linux/slab.h>
27
28 #include "rpmsg_internal.h"
29
30 /**
31 * rpmsg_create_ept() - create a new rpmsg_endpoint
32 * @rpdev: rpmsg channel device
33 * @cb: rx callback handler
34 * @priv: private data for the driver's use
35 * @chinfo: channel_info with the local rpmsg address to bind with @cb
36 *
37 * Every rpmsg address in the system is bound to an rx callback (so when
38 * inbound messages arrive, they are dispatched by the rpmsg bus using the
39 * appropriate callback handler) by means of an rpmsg_endpoint struct.
40 *
41 * This function allows drivers to create such an endpoint, and by that,
42 * bind a callback, and possibly some private data too, to an rpmsg address
43 * (either one that is known in advance, or one that will be dynamically
44 * assigned for them).
45 *
46 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
47 * is already created for them when they are probed by the rpmsg bus
48 * (using the rx callback provided when they registered to the rpmsg bus).
49 *
50 * So things should just work for simple drivers: they already have an
51 * endpoint, their rx callback is bound to their rpmsg address, and when
52 * relevant inbound messages arrive (i.e. messages which their dst address
53 * equals to the src address of their rpmsg channel), the driver's handler
54 * is invoked to process it.
55 *
56 * That said, more complicated drivers might do need to allocate
57 * additional rpmsg addresses, and bind them to different rx callbacks.
58 * To accomplish that, those drivers need to call this function.
59 *
60 * Drivers should provide their @rpdev channel (so the new endpoint would belong
61 * to the same remote processor their channel belongs to), an rx callback
62 * function, an optional private data (which is provided back when the
63 * rx callback is invoked), and an address they want to bind with the
64 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
65 * dynamically assign them an available rpmsg address (drivers should have
66 * a very good reason why not to always use RPMSG_ADDR_ANY here).
67 *
68 * Returns a pointer to the endpoint on success, or NULL on error.
69 */
70 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
71 rpmsg_rx_cb_t cb, void *priv,
72 struct rpmsg_channel_info chinfo)
73 {
74 if (WARN_ON(!rpdev))
75 return ERR_PTR(-EINVAL);
76
77 return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
78 }
79 EXPORT_SYMBOL(rpmsg_create_ept);
80
81 /**
82 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
83 * @ept: endpoing to destroy
84 *
85 * Should be used by drivers to destroy an rpmsg endpoint previously
86 * created with rpmsg_create_ept(). As with other types of "free" NULL
87 * is a valid parameter.
88 */
89 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
90 {
91 if (ept)
92 ept->ops->destroy_ept(ept);
93 }
94 EXPORT_SYMBOL(rpmsg_destroy_ept);
95
96 /**
97 * rpmsg_send() - send a message across to the remote processor
98 * @ept: the rpmsg endpoint
99 * @data: payload of message
100 * @len: length of payload
101 *
102 * This function sends @data of length @len on the @ept endpoint.
103 * The message will be sent to the remote processor which the @ept
104 * endpoint belongs to, using @ept's address and its associated rpmsg
105 * device destination addresses.
106 * In case there are no TX buffers available, the function will block until
107 * one becomes available, or a timeout of 15 seconds elapses. When the latter
108 * happens, -ERESTARTSYS is returned.
109 *
110 * Can only be called from process context (for now).
111 *
112 * Returns 0 on success and an appropriate error value on failure.
113 */
114 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
115 {
116 if (WARN_ON(!ept))
117 return -EINVAL;
118 if (!ept->ops->send)
119 return -ENXIO;
120
121 return ept->ops->send(ept, data, len);
122 }
123 EXPORT_SYMBOL(rpmsg_send);
124
125 /**
126 * rpmsg_sendto() - send a message across to the remote processor, specify dst
127 * @ept: the rpmsg endpoint
128 * @data: payload of message
129 * @len: length of payload
130 * @dst: destination address
131 *
132 * This function sends @data of length @len to the remote @dst address.
133 * The message will be sent to the remote processor which the @ept
134 * endpoint belongs to, using @ept's address as source.
135 * In case there are no TX buffers available, the function will block until
136 * one becomes available, or a timeout of 15 seconds elapses. When the latter
137 * happens, -ERESTARTSYS is returned.
138 *
139 * Can only be called from process context (for now).
140 *
141 * Returns 0 on success and an appropriate error value on failure.
142 */
143 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
144 {
145 if (WARN_ON(!ept))
146 return -EINVAL;
147 if (!ept->ops->sendto)
148 return -ENXIO;
149
150 return ept->ops->sendto(ept, data, len, dst);
151 }
152 EXPORT_SYMBOL(rpmsg_sendto);
153
154 /**
155 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
156 * @ept: the rpmsg endpoint
157 * @src: source address
158 * @dst: destination address
159 * @data: payload of message
160 * @len: length of payload
161 *
162 * This function sends @data of length @len to the remote @dst address,
163 * and uses @src as the source address.
164 * The message will be sent to the remote processor which the @ept
165 * endpoint belongs to.
166 * In case there are no TX buffers available, the function will block until
167 * one becomes available, or a timeout of 15 seconds elapses. When the latter
168 * happens, -ERESTARTSYS is returned.
169 *
170 * Can only be called from process context (for now).
171 *
172 * Returns 0 on success and an appropriate error value on failure.
173 */
174 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
175 void *data, int len)
176 {
177 if (WARN_ON(!ept))
178 return -EINVAL;
179 if (!ept->ops->send_offchannel)
180 return -ENXIO;
181
182 return ept->ops->send_offchannel(ept, src, dst, data, len);
183 }
184 EXPORT_SYMBOL(rpmsg_send_offchannel);
185
186 /**
187 * rpmsg_send() - send a message across to the remote processor
188 * @ept: the rpmsg endpoint
189 * @data: payload of message
190 * @len: length of payload
191 *
192 * This function sends @data of length @len on the @ept endpoint.
193 * The message will be sent to the remote processor which the @ept
194 * endpoint belongs to, using @ept's address as source and its associated
195 * rpdev's address as destination.
196 * In case there are no TX buffers available, the function will immediately
197 * return -ENOMEM without waiting until one becomes available.
198 *
199 * Can only be called from process context (for now).
200 *
201 * Returns 0 on success and an appropriate error value on failure.
202 */
203 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
204 {
205 if (WARN_ON(!ept))
206 return -EINVAL;
207 if (!ept->ops->trysend)
208 return -ENXIO;
209
210 return ept->ops->trysend(ept, data, len);
211 }
212 EXPORT_SYMBOL(rpmsg_trysend);
213
214 /**
215 * rpmsg_sendto() - send a message across to the remote processor, specify dst
216 * @ept: the rpmsg endpoint
217 * @data: payload of message
218 * @len: length of payload
219 * @dst: destination address
220 *
221 * This function sends @data of length @len to the remote @dst address.
222 * The message will be sent to the remote processor which the @ept
223 * endpoint belongs to, using @ept's address as source.
224 * In case there are no TX buffers available, the function will immediately
225 * return -ENOMEM without waiting until one becomes available.
226 *
227 * Can only be called from process context (for now).
228 *
229 * Returns 0 on success and an appropriate error value on failure.
230 */
231 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
232 {
233 if (WARN_ON(!ept))
234 return -EINVAL;
235 if (!ept->ops->trysendto)
236 return -ENXIO;
237
238 return ept->ops->trysendto(ept, data, len, dst);
239 }
240 EXPORT_SYMBOL(rpmsg_trysendto);
241
242 /**
243 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
244 * @ept: the rpmsg endpoint
245 * @src: source address
246 * @dst: destination address
247 * @data: payload of message
248 * @len: length of payload
249 *
250 * This function sends @data of length @len to the remote @dst address,
251 * and uses @src as the source address.
252 * The message will be sent to the remote processor which the @ept
253 * endpoint belongs to.
254 * In case there are no TX buffers available, the function will immediately
255 * return -ENOMEM without waiting until one becomes available.
256 *
257 * Can only be called from process context (for now).
258 *
259 * Returns 0 on success and an appropriate error value on failure.
260 */
261 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
262 void *data, int len)
263 {
264 if (WARN_ON(!ept))
265 return -EINVAL;
266 if (!ept->ops->trysend_offchannel)
267 return -ENXIO;
268
269 return ept->ops->trysend_offchannel(ept, src, dst, data, len);
270 }
271 EXPORT_SYMBOL(rpmsg_trysend_offchannel);
272
273 /*
274 * match an rpmsg channel with a channel info struct.
275 * this is used to make sure we're not creating rpmsg devices for channels
276 * that already exist.
277 */
278 static int rpmsg_device_match(struct device *dev, void *data)
279 {
280 struct rpmsg_channel_info *chinfo = data;
281 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
282
283 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
284 return 0;
285
286 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
287 return 0;
288
289 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
290 return 0;
291
292 /* found a match ! */
293 return 1;
294 }
295
296 struct device *rpmsg_find_device(struct device *parent,
297 struct rpmsg_channel_info *chinfo)
298 {
299 return device_find_child(parent, chinfo, rpmsg_device_match);
300
301 }
302 EXPORT_SYMBOL(rpmsg_find_device);
303
304 /* sysfs show configuration fields */
305 #define rpmsg_show_attr(field, path, format_string) \
306 static ssize_t \
307 field##_show(struct device *dev, \
308 struct device_attribute *attr, char *buf) \
309 { \
310 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
311 \
312 return sprintf(buf, format_string, rpdev->path); \
313 }
314
315 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
316 rpmsg_show_attr(name, id.name, "%s\n");
317 rpmsg_show_attr(src, src, "0x%x\n");
318 rpmsg_show_attr(dst, dst, "0x%x\n");
319 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
320
321 static ssize_t modalias_show(struct device *dev,
322 struct device_attribute *attr, char *buf)
323 {
324 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
325
326 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
327 }
328
329 static struct device_attribute rpmsg_dev_attrs[] = {
330 __ATTR_RO(name),
331 __ATTR_RO(modalias),
332 __ATTR_RO(dst),
333 __ATTR_RO(src),
334 __ATTR_RO(announce),
335 __ATTR_NULL
336 };
337
338 /* rpmsg devices and drivers are matched using the service name */
339 static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
340 const struct rpmsg_device_id *id)
341 {
342 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
343 }
344
345 /* match rpmsg channel and rpmsg driver */
346 static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
347 {
348 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
349 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
350 const struct rpmsg_device_id *ids = rpdrv->id_table;
351 unsigned int i;
352
353 if (rpdev->driver_override)
354 return !strcmp(rpdev->driver_override, drv->name);
355
356 if (ids)
357 for (i = 0; ids[i].name[0]; i++)
358 if (rpmsg_id_match(rpdev, &ids[i]))
359 return 1;
360
361 return of_driver_match_device(dev, drv);
362 }
363
364 static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
365 {
366 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
367
368 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
369 rpdev->id.name);
370 }
371
372 /*
373 * when an rpmsg driver is probed with a channel, we seamlessly create
374 * it an endpoint, binding its rx callback to a unique local rpmsg
375 * address.
376 *
377 * if we need to, we also announce about this channel to the remote
378 * processor (needed in case the driver is exposing an rpmsg service).
379 */
380 static int rpmsg_dev_probe(struct device *dev)
381 {
382 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
383 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
384 struct rpmsg_channel_info chinfo = {};
385 struct rpmsg_endpoint *ept = NULL;
386 int err;
387
388 if (rpdrv->callback) {
389 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
390 chinfo.src = rpdev->src;
391 chinfo.dst = RPMSG_ADDR_ANY;
392
393 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
394 if (!ept) {
395 dev_err(dev, "failed to create endpoint\n");
396 err = -ENOMEM;
397 goto out;
398 }
399
400 rpdev->ept = ept;
401 rpdev->src = ept->addr;
402 }
403
404 err = rpdrv->probe(rpdev);
405 if (err) {
406 dev_err(dev, "%s: failed: %d\n", __func__, err);
407 if (ept)
408 rpmsg_destroy_ept(ept);
409 goto out;
410 }
411
412 if (rpdev->ops->announce_create)
413 err = rpdev->ops->announce_create(rpdev);
414 out:
415 return err;
416 }
417
418 static int rpmsg_dev_remove(struct device *dev)
419 {
420 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
421 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
422 int err = 0;
423
424 if (rpdev->ops->announce_destroy)
425 err = rpdev->ops->announce_destroy(rpdev);
426
427 rpdrv->remove(rpdev);
428
429 if (rpdev->ept)
430 rpmsg_destroy_ept(rpdev->ept);
431
432 return err;
433 }
434
435 static struct bus_type rpmsg_bus = {
436 .name = "rpmsg",
437 .match = rpmsg_dev_match,
438 .dev_attrs = rpmsg_dev_attrs,
439 .uevent = rpmsg_uevent,
440 .probe = rpmsg_dev_probe,
441 .remove = rpmsg_dev_remove,
442 };
443
444 static void rpmsg_release_device(struct device *dev)
445 {
446 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
447
448 kfree(rpdev);
449 }
450
451 int rpmsg_register_device(struct rpmsg_device *rpdev)
452 {
453 struct device *dev = &rpdev->dev;
454 int ret;
455
456 dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
457 rpdev->id.name, rpdev->src, rpdev->dst);
458
459 rpdev->dev.bus = &rpmsg_bus;
460 rpdev->dev.release = rpmsg_release_device;
461
462 ret = device_register(&rpdev->dev);
463 if (ret) {
464 dev_err(dev, "device_register failed: %d\n", ret);
465 put_device(&rpdev->dev);
466 }
467
468 return ret;
469 }
470 EXPORT_SYMBOL(rpmsg_register_device);
471
472 /*
473 * find an existing channel using its name + address properties,
474 * and destroy it
475 */
476 int rpmsg_unregister_device(struct device *parent,
477 struct rpmsg_channel_info *chinfo)
478 {
479 struct device *dev;
480
481 dev = rpmsg_find_device(parent, chinfo);
482 if (!dev)
483 return -EINVAL;
484
485 device_unregister(dev);
486
487 put_device(dev);
488
489 return 0;
490 }
491 EXPORT_SYMBOL(rpmsg_unregister_device);
492
493 /**
494 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
495 * @rpdrv: pointer to a struct rpmsg_driver
496 * @owner: owning module/driver
497 *
498 * Returns 0 on success, and an appropriate error value on failure.
499 */
500 int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
501 {
502 rpdrv->drv.bus = &rpmsg_bus;
503 rpdrv->drv.owner = owner;
504 return driver_register(&rpdrv->drv);
505 }
506 EXPORT_SYMBOL(__register_rpmsg_driver);
507
508 /**
509 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
510 * @rpdrv: pointer to a struct rpmsg_driver
511 *
512 * Returns 0 on success, and an appropriate error value on failure.
513 */
514 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
515 {
516 driver_unregister(&rpdrv->drv);
517 }
518 EXPORT_SYMBOL(unregister_rpmsg_driver);
519
520
521 static int __init rpmsg_init(void)
522 {
523 int ret;
524
525 ret = bus_register(&rpmsg_bus);
526 if (ret)
527 pr_err("failed to register rpmsg bus: %d\n", ret);
528
529 return ret;
530 }
531 postcore_initcall(rpmsg_init);
532
533 static void __exit rpmsg_fini(void)
534 {
535 bus_unregister(&rpmsg_bus);
536 }
537 module_exit(rpmsg_fini);
538
539 MODULE_DESCRIPTION("remote processor messaging bus");
540 MODULE_LICENSE("GPL v2");