Skip to content

Commit bcabbcc

Browse files
committedFeb 8, 2012
rpmsg: add virtio-based remote processor messaging bus
Add a virtio-based inter-processor communication bus, which enables kernel drivers to communicate with entities, running on remote processors, over shared memory using a simple messaging protocol. Every pair of AMP processors share two vrings, which are used to send and receive the messages over shared memory. The header of every message sent on the rpmsg bus contains src and dst addresses, which make it possible to multiplex several rpmsg channels on the same vring. Every rpmsg channel is a device on this bus. When a channel is added, and an appropriate rpmsg driver is found and probed, it is also assigned a local rpmsg address, which is then bound to the driver's callback. When inbound messages carry the local address of a bound driver, its callback is invoked by the bus. This patch provides a kernel interface only; user space interfaces will be later exposed by kernel users of this rpmsg bus. Designed with Brian Swetland <swetland@google.com>. Signed-off-by: Ohad Ben-Cohen <ohad@wizery.com> Acked-by: Rusty Russell <rusty@rustcorp.com.au> (virtio_ids.h) Cc: Brian Swetland <swetland@google.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Grant Likely <grant.likely@secretlab.ca> Cc: Tony Lindgren <tony@atomide.com> Cc: Russell King <linux@arm.linux.org.uk> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Greg KH <greg@kroah.com> Cc: Stephen Boyd <sboyd@codeaurora.org>
1 parent 34ed5a3 commit bcabbcc

File tree

10 files changed

+1739
-0
lines changed

10 files changed

+1739
-0
lines changed
 
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
What: /sys/bus/rpmsg/devices/.../name
2+
Date: June 2011
3+
KernelVersion: 3.3
4+
Contact: Ohad Ben-Cohen <ohad@wizery.com>
5+
Description:
6+
Every rpmsg device is a communication channel with a remote
7+
processor. Channels are identified with a (textual) name,
8+
which is maximum 32 bytes long (defined as RPMSG_NAME_SIZE in
9+
rpmsg.h).
10+
11+
This sysfs entry contains the name of this channel.
12+
13+
What: /sys/bus/rpmsg/devices/.../src
14+
Date: June 2011
15+
KernelVersion: 3.3
16+
Contact: Ohad Ben-Cohen <ohad@wizery.com>
17+
Description:
18+
Every rpmsg device is a communication channel with a remote
19+
processor. Channels have a local ("source") rpmsg address,
20+
and remote ("destination") rpmsg address. When an entity
21+
starts listening on one end of a channel, it assigns it with
22+
a unique rpmsg address (a 32 bits integer). This way when
23+
inbound messages arrive to this address, the rpmsg core
24+
dispatches them to the listening entity (a kernel driver).
25+
26+
This sysfs entry contains the src (local) rpmsg address
27+
of this channel. If it contains 0xffffffff, then an address
28+
wasn't assigned (can happen if no driver exists for this
29+
channel).
30+
31+
What: /sys/bus/rpmsg/devices/.../dst
32+
Date: June 2011
33+
KernelVersion: 3.3
34+
Contact: Ohad Ben-Cohen <ohad@wizery.com>
35+
Description:
36+
Every rpmsg device is a communication channel with a remote
37+
processor. Channels have a local ("source") rpmsg address,
38+
and remote ("destination") rpmsg address. When an entity
39+
starts listening on one end of a channel, it assigns it with
40+
a unique rpmsg address (a 32 bits integer). This way when
41+
inbound messages arrive to this address, the rpmsg core
42+
dispatches them to the listening entity.
43+
44+
This sysfs entry contains the dst (remote) rpmsg address
45+
of this channel. If it contains 0xffffffff, then an address
46+
wasn't assigned (can happen if the kernel driver that
47+
is attached to this channel is exposing a service to the
48+
remote processor. This make it a local rpmsg server,
49+
and it is listening for inbound messages that may be sent
50+
from any remote rpmsg client; it is not bound to a single
51+
remote entity).
52+
53+
What: /sys/bus/rpmsg/devices/.../announce
54+
Date: June 2011
55+
KernelVersion: 3.3
56+
Contact: Ohad Ben-Cohen <ohad@wizery.com>
57+
Description:
58+
Every rpmsg device is a communication channel with a remote
59+
processor. Channels are identified by a textual name (see
60+
/sys/bus/rpmsg/devices/.../name above) and have a local
61+
("source") rpmsg address, and remote ("destination") rpmsg
62+
address.
63+
64+
A channel is first created when an entity, whether local
65+
or remote, starts listening on it for messages (and is thus
66+
called an rpmsg server).
67+
68+
When that happens, a "name service" announcement is sent
69+
to the other processor, in order to let it know about the
70+
creation of the channel (this way remote clients know they
71+
can start sending messages).
72+
73+
This sysfs entry tells us whether the channel is a local
74+
server channel that is announced (values are either
75+
true or false).

‎Documentation/rpmsg.txt

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

‎drivers/Kconfig

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ source "drivers/iommu/Kconfig"
134134

135135
source "drivers/remoteproc/Kconfig"
136136

137+
source "drivers/rpmsg/Kconfig"
138+
137139
source "drivers/virt/Kconfig"
138140

139141
source "drivers/devfreq/Kconfig"

‎drivers/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ obj-$(CONFIG_HWSPINLOCK) += hwspinlock/
127127
obj-$(CONFIG_NFC) += nfc/
128128
obj-$(CONFIG_IOMMU_SUPPORT) += iommu/
129129
obj-$(CONFIG_REMOTEPROC) += remoteproc/
130+
obj-$(CONFIG_RPMSG) += rpmsg/
130131

131132
# Virtualization drivers
132133
obj-$(CONFIG_VIRT_DRIVERS) += virt/

‎drivers/rpmsg/Kconfig

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# RPMSG always gets selected by whoever wants it
2+
config RPMSG
3+
tristate
4+
select VIRTIO
5+
select VIRTIO_RING

‎drivers/rpmsg/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
obj-$(CONFIG_RPMSG) += virtio_rpmsg_bus.o

‎drivers/rpmsg/virtio_rpmsg_bus.c

+1,026
Large diffs are not rendered by default.

‎include/linux/mod_devicetable.h

+9
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,15 @@ struct hv_vmbus_device_id {
414414
__attribute__((aligned(sizeof(kernel_ulong_t))));
415415
};
416416

417+
/* rpmsg */
418+
419+
#define RPMSG_NAME_SIZE 32
420+
#define RPMSG_DEVICE_MODALIAS_FMT "rpmsg:%s"
421+
422+
struct rpmsg_device_id {
423+
char name[RPMSG_NAME_SIZE];
424+
};
425+
417426
/* i2c */
418427

419428
#define I2C_NAME_SIZE 20

‎include/linux/rpmsg.h

+326
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
/*
2+
* Remote processor messaging
3+
*
4+
* Copyright (C) 2011 Texas Instruments, Inc.
5+
* Copyright (C) 2011 Google, Inc.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
* * Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
* * Neither the name Texas Instruments nor the names of its
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
#ifndef _LINUX_RPMSG_H
36+
#define _LINUX_RPMSG_H
37+
38+
#include <linux/types.h>
39+
#include <linux/device.h>
40+
#include <linux/mod_devicetable.h>
41+
42+
/* The feature bitmap for virtio rpmsg */
43+
#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
44+
45+
/**
46+
* struct rpmsg_hdr - common header for all rpmsg messages
47+
* @src: source address
48+
* @dst: destination address
49+
* @reserved: reserved for future use
50+
* @len: length of payload (in bytes)
51+
* @flags: message flags
52+
* @data: @len bytes of message payload data
53+
*
54+
* Every message sent(/received) on the rpmsg bus begins with this header.
55+
*/
56+
struct rpmsg_hdr {
57+
u32 src;
58+
u32 dst;
59+
u32 reserved;
60+
u16 len;
61+
u16 flags;
62+
u8 data[0];
63+
} __packed;
64+
65+
/**
66+
* struct rpmsg_ns_msg - dynamic name service announcement message
67+
* @name: name of remote service that is published
68+
* @addr: address of remote service that is published
69+
* @flags: indicates whether service is created or destroyed
70+
*
71+
* This message is sent across to publish a new service, or announce
72+
* about its removal. When we receive these messages, an appropriate
73+
* rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
74+
* or ->remove() handler of the appropriate rpmsg driver will be invoked
75+
* (if/as-soon-as one is registered).
76+
*/
77+
struct rpmsg_ns_msg {
78+
char name[RPMSG_NAME_SIZE];
79+
u32 addr;
80+
u32 flags;
81+
} __packed;
82+
83+
/**
84+
* enum rpmsg_ns_flags - dynamic name service announcement flags
85+
*
86+
* @RPMSG_NS_CREATE: a new remote service was just created
87+
* @RPMSG_NS_DESTROY: a known remote service was just destroyed
88+
*/
89+
enum rpmsg_ns_flags {
90+
RPMSG_NS_CREATE = 0,
91+
RPMSG_NS_DESTROY = 1,
92+
};
93+
94+
#define RPMSG_ADDR_ANY 0xFFFFFFFF
95+
96+
struct virtproc_info;
97+
98+
/**
99+
* rpmsg_channel - devices that belong to the rpmsg bus are called channels
100+
* @vrp: the remote processor this channel belongs to
101+
* @dev: the device struct
102+
* @id: device id (used to match between rpmsg drivers and devices)
103+
* @src: local address
104+
* @dst: destination address
105+
* @ept: the rpmsg endpoint of this channel
106+
* @announce: if set, rpmsg will announce the creation/removal of this channel
107+
*/
108+
struct rpmsg_channel {
109+
struct virtproc_info *vrp;
110+
struct device dev;
111+
struct rpmsg_device_id id;
112+
u32 src;
113+
u32 dst;
114+
struct rpmsg_endpoint *ept;
115+
bool announce;
116+
};
117+
118+
typedef void (*rpmsg_rx_cb_t)(struct rpmsg_channel *, void *, int, void *, u32);
119+
120+
/**
121+
* struct rpmsg_endpoint - binds a local rpmsg address to its user
122+
* @rpdev: rpmsg channel device
123+
* @cb: rx callback handler
124+
* @addr: local rpmsg address
125+
* @priv: private data for the driver's use
126+
*
127+
* In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
128+
* it binds an rpmsg address with an rx callback handler.
129+
*
130+
* Simple rpmsg drivers shouldn't use this struct directly, because
131+
* things just work: every rpmsg driver provides an rx callback upon
132+
* registering to the bus, and that callback is then bound to its rpmsg
133+
* address when the driver is probed. When relevant inbound messages arrive
134+
* (i.e. messages which their dst address equals to the src address of
135+
* the rpmsg channel), the driver's handler is invoked to process it.
136+
*
137+
* More complicated drivers though, that do need to allocate additional rpmsg
138+
* addresses, and bind them to different rx callbacks, must explicitly
139+
* create additional endpoints by themselves (see rpmsg_create_ept()).
140+
*/
141+
struct rpmsg_endpoint {
142+
struct rpmsg_channel *rpdev;
143+
rpmsg_rx_cb_t cb;
144+
u32 addr;
145+
void *priv;
146+
};
147+
148+
/**
149+
* struct rpmsg_driver - rpmsg driver struct
150+
* @drv: underlying device driver
151+
* @id_table: rpmsg ids serviced by this driver
152+
* @probe: invoked when a matching rpmsg channel (i.e. device) is found
153+
* @remove: invoked when the rpmsg channel is removed
154+
* @callback: invoked when an inbound message is received on the channel
155+
*/
156+
struct rpmsg_driver {
157+
struct device_driver drv;
158+
const struct rpmsg_device_id *id_table;
159+
int (*probe)(struct rpmsg_channel *dev);
160+
void (*remove)(struct rpmsg_channel *dev);
161+
void (*callback)(struct rpmsg_channel *, void *, int, void *, u32);
162+
};
163+
164+
int register_rpmsg_device(struct rpmsg_channel *dev);
165+
void unregister_rpmsg_device(struct rpmsg_channel *dev);
166+
int register_rpmsg_driver(struct rpmsg_driver *drv);
167+
void unregister_rpmsg_driver(struct rpmsg_driver *drv);
168+
void rpmsg_destroy_ept(struct rpmsg_endpoint *);
169+
struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *,
170+
rpmsg_rx_cb_t cb, void *priv, u32 addr);
171+
int
172+
rpmsg_send_offchannel_raw(struct rpmsg_channel *, u32, u32, void *, int, bool);
173+
174+
/**
175+
* rpmsg_send() - send a message across to the remote processor
176+
* @rpdev: the rpmsg channel
177+
* @data: payload of message
178+
* @len: length of payload
179+
*
180+
* This function sends @data of length @len on the @rpdev channel.
181+
* The message will be sent to the remote processor which the @rpdev
182+
* channel belongs to, using @rpdev's source and destination addresses.
183+
* In case there are no TX buffers available, the function will block until
184+
* one becomes available, or a timeout of 15 seconds elapses. When the latter
185+
* happens, -ERESTARTSYS is returned.
186+
*
187+
* Can only be called from process context (for now).
188+
*
189+
* Returns 0 on success and an appropriate error value on failure.
190+
*/
191+
static inline int rpmsg_send(struct rpmsg_channel *rpdev, void *data, int len)
192+
{
193+
u32 src = rpdev->src, dst = rpdev->dst;
194+
195+
return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
196+
}
197+
198+
/**
199+
* rpmsg_sendto() - send a message across to the remote processor, specify dst
200+
* @rpdev: the rpmsg channel
201+
* @data: payload of message
202+
* @len: length of payload
203+
* @dst: destination address
204+
*
205+
* This function sends @data of length @len to the remote @dst address.
206+
* The message will be sent to the remote processor which the @rpdev
207+
* channel belongs to, using @rpdev's source address.
208+
* In case there are no TX buffers available, the function will block until
209+
* one becomes available, or a timeout of 15 seconds elapses. When the latter
210+
* happens, -ERESTARTSYS is returned.
211+
*
212+
* Can only be called from process context (for now).
213+
*
214+
* Returns 0 on success and an appropriate error value on failure.
215+
*/
216+
static inline
217+
int rpmsg_sendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst)
218+
{
219+
u32 src = rpdev->src;
220+
221+
return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
222+
}
223+
224+
/**
225+
* rpmsg_send_offchannel() - send a message using explicit src/dst addresses
226+
* @rpdev: the rpmsg channel
227+
* @src: source address
228+
* @dst: destination address
229+
* @data: payload of message
230+
* @len: length of payload
231+
*
232+
* This function sends @data of length @len to the remote @dst address,
233+
* and uses @src as the source address.
234+
* The message will be sent to the remote processor which the @rpdev
235+
* channel belongs to.
236+
* In case there are no TX buffers available, the function will block until
237+
* one becomes available, or a timeout of 15 seconds elapses. When the latter
238+
* happens, -ERESTARTSYS is returned.
239+
*
240+
* Can only be called from process context (for now).
241+
*
242+
* Returns 0 on success and an appropriate error value on failure.
243+
*/
244+
static inline
245+
int rpmsg_send_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
246+
void *data, int len)
247+
{
248+
return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
249+
}
250+
251+
/**
252+
* rpmsg_send() - send a message across to the remote processor
253+
* @rpdev: the rpmsg channel
254+
* @data: payload of message
255+
* @len: length of payload
256+
*
257+
* This function sends @data of length @len on the @rpdev channel.
258+
* The message will be sent to the remote processor which the @rpdev
259+
* channel belongs to, using @rpdev's source and destination addresses.
260+
* In case there are no TX buffers available, the function will immediately
261+
* return -ENOMEM without waiting until one becomes available.
262+
*
263+
* Can only be called from process context (for now).
264+
*
265+
* Returns 0 on success and an appropriate error value on failure.
266+
*/
267+
static inline
268+
int rpmsg_trysend(struct rpmsg_channel *rpdev, void *data, int len)
269+
{
270+
u32 src = rpdev->src, dst = rpdev->dst;
271+
272+
return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
273+
}
274+
275+
/**
276+
* rpmsg_sendto() - send a message across to the remote processor, specify dst
277+
* @rpdev: the rpmsg channel
278+
* @data: payload of message
279+
* @len: length of payload
280+
* @dst: destination address
281+
*
282+
* This function sends @data of length @len to the remote @dst address.
283+
* The message will be sent to the remote processor which the @rpdev
284+
* channel belongs to, using @rpdev's source address.
285+
* In case there are no TX buffers available, the function will immediately
286+
* return -ENOMEM without waiting until one becomes available.
287+
*
288+
* Can only be called from process context (for now).
289+
*
290+
* Returns 0 on success and an appropriate error value on failure.
291+
*/
292+
static inline
293+
int rpmsg_trysendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst)
294+
{
295+
u32 src = rpdev->src;
296+
297+
return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
298+
}
299+
300+
/**
301+
* rpmsg_send_offchannel() - send a message using explicit src/dst addresses
302+
* @rpdev: the rpmsg channel
303+
* @src: source address
304+
* @dst: destination address
305+
* @data: payload of message
306+
* @len: length of payload
307+
*
308+
* This function sends @data of length @len to the remote @dst address,
309+
* and uses @src as the source address.
310+
* The message will be sent to the remote processor which the @rpdev
311+
* channel belongs to.
312+
* In case there are no TX buffers available, the function will immediately
313+
* return -ENOMEM without waiting until one becomes available.
314+
*
315+
* Can only be called from process context (for now).
316+
*
317+
* Returns 0 on success and an appropriate error value on failure.
318+
*/
319+
static inline
320+
int rpmsg_trysend_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
321+
void *data, int len)
322+
{
323+
return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
324+
}
325+
326+
#endif /* _LINUX_RPMSG_H */

‎include/linux/virtio_ids.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#define VIRTIO_ID_CONSOLE 3 /* virtio console */
3535
#define VIRTIO_ID_RNG 4 /* virtio ring */
3636
#define VIRTIO_ID_BALLOON 5 /* virtio balloon */
37+
#define VIRTIO_ID_RPMSG 7 /* virtio remote processor messaging */
3738
#define VIRTIO_ID_9P 9 /* 9p virtio console */
3839

3940
#endif /* _LINUX_VIRTIO_IDS_H */

0 commit comments

Comments
 (0)
Please sign in to comment.