xref: /linux/Documentation/staging/rpmsg.rst (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce) !
1============================================
2Remote Processor Messaging (rpmsg) Framework
3============================================
4
5.. note::
6
7  This document describes the rpmsg bus and how to write rpmsg drivers.
8  To learn how to add rpmsg support for new platforms, check out remoteproc.txt
9  (also a resident of Documentation/).
10
11Introduction
12============
13
14Modern SoCs typically employ heterogeneous remote processor devices in
15asymmetric multiprocessing (AMP) configurations, which may be running
16different instances of operating system, whether it's Linux or any other
17flavor of real-time OS.
18
19OMAP4, for example, has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP.
20Typically, the dual cortex-A9 is running Linux in a SMP configuration,
21and each of the other three cores (two M3 cores and a DSP) is running
22its own instance of RTOS in an AMP configuration.
23
24Typically AMP remote processors employ dedicated DSP codecs and multimedia
25hardware accelerators, and therefore are often used to offload CPU-intensive
26multimedia tasks from the main application processor.
27
28These remote processors could also be used to control latency-sensitive
29sensors, drive random hardware blocks, or just perform background tasks
30while the main CPU is idling.
31
32Users of those remote processors can either be userland apps (e.g. multimedia
33frameworks talking with remote OMX components) or kernel drivers (controlling
34hardware accessible only by the remote processor, reserving kernel-controlled
35resources on behalf of the remote processor, etc..).
36
37Rpmsg is a virtio-based messaging bus that allows kernel drivers to communicate
38with remote processors available on the system. In turn, drivers could then
39expose appropriate user space interfaces, if needed.
40
41When writing a driver that exposes rpmsg communication to userland, please
42keep in mind that remote processors might have direct access to the
43system's physical memory and other sensitive hardware resources (e.g. on
44OMAP4, remote cores and hardware accelerators may have direct access to the
45physical memory, gpio banks, dma controllers, i2c bus, gptimers, mailbox
46devices, hwspinlocks, etc..). Moreover, those remote processors might be
47running RTOS where every task can access the entire memory/devices exposed
48to the processor. To minimize the risks of rogue (or buggy) userland code
49exploiting remote bugs, and by that taking over the system, it is often
50desired to limit userland to specific rpmsg channels (see definition below)
51it can send messages on, and if possible, minimize how much control
52it has over the content of the messages.
53
54Every rpmsg device is a communication channel with a remote processor (thus
55rpmsg devices are called channels). Channels are identified by a textual name
56and have a local ("source") rpmsg address, and remote ("destination") rpmsg
57address.
58
59When a driver starts listening on a channel, its rx callback is bound with
60a unique rpmsg local address (a 32-bit integer). This way when inbound messages
61arrive, the rpmsg core dispatches them to the appropriate driver according
62to their destination address (this is done by invoking the driver's rx handler
63with the payload of the inbound message).
64
65
66User API
67========
68
69::
70
71  int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
72
73sends a message across to the remote processor from the given endpoint.
74The caller should specify the endpoint, the data it wants to send,
75and its length (in bytes). The message will be sent on the specified
76endpoint's channel, i.e. its source and destination address fields will be
77respectively set to the endpoint's src address and its parent channel
78dst addresses.
79
80In case there are no TX buffers available, the function will block until
81one becomes available (i.e. until the remote processor consumes
82a tx buffer and puts it back on virtio's used descriptor ring),
83or a timeout of 15 seconds elapses. When the latter happens,
84-ERESTARTSYS is returned.
85
86The function can only be called from a process context (for now).
87Returns 0 on success and an appropriate error value on failure.
88
89::
90
91  int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
92
93sends a message across to the remote processor from a given endpoint,
94to a destination address provided by the caller.
95
96The caller should specify the endpoint, the data it wants to send,
97its length (in bytes), and an explicit destination address.
98
99The message will then be sent to the remote processor to which the
100endpoints's channel belongs, using the endpoints's src address,
101and the user-provided dst address (thus the channel's dst address
102will be ignored).
103
104In case there are no TX buffers available, the function will block until
105one becomes available (i.e. until the remote processor consumes
106a tx buffer and puts it back on virtio's used descriptor ring),
107or a timeout of 15 seconds elapses. When the latter happens,
108-ERESTARTSYS is returned.
109
110The function can only be called from a process context (for now).
111Returns 0 on success and an appropriate error value on failure.
112
113::
114
115  int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
116
117sends a message across to the remote processor from a given endpoint.
118The caller should specify the endpoint, the data it wants to send,
119and its length (in bytes). The message will be sent on the specified
120endpoint's channel, i.e. its source and destination address fields will be
121respectively set to the endpoint's src address and its parent channel
122dst addresses.
123
124In case there are no TX buffers available, the function will immediately
125return -ENOMEM without waiting until one becomes available.
126
127The function can only be called from a process context (for now).
128Returns 0 on success and an appropriate error value on failure.
129
130::
131
132  int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
133
134
135sends a message across to the remote processor from a given endpoint,
136to a destination address provided by the user.
137
138The user should specify the channel, the data it wants to send,
139its length (in bytes), and an explicit destination address.
140
141The message will then be sent to the remote processor to which the
142channel belongs, using the channel's src address, and the user-provided
143dst address (thus the channel's dst address will be ignored).
144
145In case there are no TX buffers available, the function will immediately
146return -ENOMEM without waiting until one becomes available.
147
148The function can only be called from a process context (for now).
149Returns 0 on success and an appropriate error value on failure.
150
151::
152
153  struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
154					  rpmsg_rx_cb_t cb, void *priv,
155					  struct rpmsg_channel_info chinfo);
156
157every rpmsg address in the system is bound to an rx callback (so when
158inbound messages arrive, they are dispatched by the rpmsg bus using the
159appropriate callback handler) by means of an rpmsg_endpoint struct.
160
161This function allows drivers to create such an endpoint, and by that,
162bind a callback, and possibly some private data too, to an rpmsg address
163(either one that is known in advance, or one that will be dynamically
164assigned for them).
165
166Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
167is already created for them when they are probed by the rpmsg bus
168(using the rx callback they provide when they registered to the rpmsg bus).
169
170So things should just work for simple drivers: they already have an
171endpoint, their rx callback is bound to their rpmsg address, and when
172relevant inbound messages arrive (i.e. messages which their dst address
173equals to the src address of their rpmsg channel), the driver's handler
174is invoked to process it.
175
176That said, more complicated drivers might do need to allocate
177additional rpmsg addresses, and bind them to different rx callbacks.
178To accomplish that, those drivers need to call this function.
179Drivers should provide their channel (so the new endpoint would bind
180to the same remote processor their channel belongs to), an rx callback
181function, an optional private data (which is provided back when the
182rx callback is invoked), and an address they want to bind with the
183callback. If addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
184dynamically assign them an available rpmsg address (drivers should have
185a very good reason why not to always use RPMSG_ADDR_ANY here).
186
187Returns a pointer to the endpoint on success, or NULL on error.
188
189::
190
191  void rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
192
193
194destroys an existing rpmsg endpoint. user should provide a pointer
195to an rpmsg endpoint that was previously created with rpmsg_create_ept().
196
197::
198
199  int register_rpmsg_driver(struct rpmsg_driver *rpdrv);
200
201
202registers an rpmsg driver with the rpmsg bus. user should provide
203a pointer to an rpmsg_driver struct, which contains the driver's
204->probe() and ->remove() functions, an rx callback, and an id_table
205specifying the names of the channels this driver is interested to
206be probed with.
207
208::
209
210  void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv);
211
212
213unregisters an rpmsg driver from the rpmsg bus. user should provide
214a pointer to a previously-registered rpmsg_driver struct.
215Returns 0 on success, and an appropriate error value on failure.
216
217
218Typical usage
219=============
220
221The following is a simple rpmsg driver, that sends an "hello!" message
222on probe(), and whenever it receives an incoming message, it dumps its
223content to the console.
224
225::
226
227  #include <linux/kernel.h>
228  #include <linux/module.h>
229  #include <linux/rpmsg.h>
230
231  static void rpmsg_sample_cb(struct rpmsg_channel *rpdev, void *data, int len,
232						void *priv, u32 src)
233  {
234	print_hex_dump(KERN_INFO, "incoming message:", DUMP_PREFIX_NONE,
235						16, 1, data, len, true);
236  }
237
238  static int rpmsg_sample_probe(struct rpmsg_channel *rpdev)
239  {
240	int err;
241
242	dev_info(&rpdev->dev, "chnl: 0x%x -> 0x%x\n", rpdev->src, rpdev->dst);
243
244	/* send a message on our channel */
245	err = rpmsg_send(rpdev->ept, "hello!", 6);
246	if (err) {
247		pr_err("rpmsg_send failed: %d\n", err);
248		return err;
249	}
250
251	return 0;
252  }
253
254  static void rpmsg_sample_remove(struct rpmsg_channel *rpdev)
255  {
256	dev_info(&rpdev->dev, "rpmsg sample client driver is removed\n");
257  }
258
259  static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = {
260	{ .name	= "rpmsg-client-sample" },
261	{ },
262  };
263  MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_sample_id_table);
264
265  static struct rpmsg_driver rpmsg_sample_client = {
266	.drv.name	= KBUILD_MODNAME,
267	.id_table	= rpmsg_driver_sample_id_table,
268	.probe		= rpmsg_sample_probe,
269	.callback	= rpmsg_sample_cb,
270	.remove		= rpmsg_sample_remove,
271  };
272  module_rpmsg_driver(rpmsg_sample_client);
273
274.. note::
275
276   a similar sample which can be built and loaded can be found
277   in samples/rpmsg/.
278
279Allocations of rpmsg channels
280=============================
281
282At this point we only support dynamic allocations of rpmsg channels.
283
284This is possible only with remote processors that have the VIRTIO_RPMSG_F_NS
285virtio device feature set. This feature bit means that the remote
286processor supports dynamic name service announcement messages.
287
288When this feature is enabled, creation of rpmsg devices (i.e. channels)
289is completely dynamic: the remote processor announces the existence of a
290remote rpmsg service by sending a name service message (which contains
291the name and rpmsg addr of the remote service, see struct rpmsg_ns_msg).
292
293This message is then handled by the rpmsg bus, which in turn dynamically
294creates and registers an rpmsg channel (which represents the remote service).
295If/when a relevant rpmsg driver is registered, it will be immediately probed
296by the bus, and can then start sending messages to the remote service.
297
298The plan is also to add static creation of rpmsg channels via the virtio
299config space, but it's not implemented yet.
300