aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/ABI/testing/sysfs-bus-rpmsg75
-rw-r--r--Documentation/rpmsg.txt293
-rw-r--r--drivers/Kconfig2
-rw-r--r--drivers/Makefile1
-rw-r--r--drivers/rpmsg/Kconfig5
-rw-r--r--drivers/rpmsg/Makefile1
-rw-r--r--drivers/rpmsg/virtio_rpmsg_bus.c1026
-rw-r--r--include/linux/mod_devicetable.h9
-rw-r--r--include/linux/rpmsg.h326
-rw-r--r--include/linux/virtio_ids.h1
10 files changed, 1739 insertions, 0 deletions
diff --git a/Documentation/ABI/testing/sysfs-bus-rpmsg b/Documentation/ABI/testing/sysfs-bus-rpmsg
new file mode 100644
index 000000000000..189e419a5a2d
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-rpmsg
@@ -0,0 +1,75 @@
1What: /sys/bus/rpmsg/devices/.../name
2Date: June 2011
3KernelVersion: 3.3
4Contact: Ohad Ben-Cohen <ohad@wizery.com>
5Description:
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
13What: /sys/bus/rpmsg/devices/.../src
14Date: June 2011
15KernelVersion: 3.3
16Contact: Ohad Ben-Cohen <ohad@wizery.com>
17Description:
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
31What: /sys/bus/rpmsg/devices/.../dst
32Date: June 2011
33KernelVersion: 3.3
34Contact: Ohad Ben-Cohen <ohad@wizery.com>
35Description:
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
53What: /sys/bus/rpmsg/devices/.../announce
54Date: June 2011
55KernelVersion: 3.3
56Contact: Ohad Ben-Cohen <ohad@wizery.com>
57Description:
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).
diff --git a/Documentation/rpmsg.txt b/Documentation/rpmsg.txt
new file mode 100644
index 000000000000..409d9f964c5b
--- /dev/null
+++ b/Documentation/rpmsg.txt
@@ -0,0 +1,293 @@
1Remote Processor Messaging (rpmsg) Framework
2
3Note: this document describes the rpmsg bus and how to write rpmsg drivers.
4To learn how to add rpmsg support for new platforms, check out remoteproc.txt
5(also a resident of Documentation/).
6
71. Introduction
8
9Modern SoCs typically employ heterogeneous remote processor devices in
10asymmetric multiprocessing (AMP) configurations, which may be running
11different instances of operating system, whether it's Linux or any other
12flavor of real-time OS.
13
14OMAP4, for example, has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP.
15Typically, the dual cortex-A9 is running Linux in a SMP configuration,
16and each of the other three cores (two M3 cores and a DSP) is running
17its own instance of RTOS in an AMP configuration.
18
19Typically AMP remote processors employ dedicated DSP codecs and multimedia
20hardware accelerators, and therefore are often used to offload CPU-intensive
21multimedia tasks from the main application processor.
22
23These remote processors could also be used to control latency-sensitive
24sensors, drive random hardware blocks, or just perform background tasks
25while the main CPU is idling.
26
27Users of those remote processors can either be userland apps (e.g. multimedia
28frameworks talking with remote OMX components) or kernel drivers (controlling
29hardware accessible only by the remote processor, reserving kernel-controlled
30resources on behalf of the remote processor, etc..).
31
32Rpmsg is a virtio-based messaging bus that allows kernel drivers to communicate
33with remote processors available on the system. In turn, drivers could then
34expose appropriate user space interfaces, if needed.
35
36When writing a driver that exposes rpmsg communication to userland, please
37keep in mind that remote processors might have direct access to the
38system's physical memory and other sensitive hardware resources (e.g. on
39OMAP4, remote cores and hardware accelerators may have direct access to the
40physical memory, gpio banks, dma controllers, i2c bus, gptimers, mailbox
41devices, hwspinlocks, etc..). Moreover, those remote processors might be
42running RTOS where every task can access the entire memory/devices exposed
43to the processor. To minimize the risks of rogue (or buggy) userland code
44exploiting remote bugs, and by that taking over the system, it is often
45desired to limit userland to specific rpmsg channels (see definition below)
46it can send messages on, and if possible, minimize how much control
47it has over the content of the messages.
48
49Every rpmsg device is a communication channel with a remote processor (thus
50rpmsg devices are called channels). Channels are identified by a textual name
51and have a local ("source") rpmsg address, and remote ("destination") rpmsg
52address.
53
54When a driver starts listening on a channel, its rx callback is bound with
55a unique rpmsg local address (a 32-bit integer). This way when inbound messages
56arrive, the rpmsg core dispatches them to the appropriate driver according
57to their destination address (this is done by invoking the driver's rx handler
58with the payload of the inbound message).
59
60
612. 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
2063. Typical usage
207
208The following is a simple rpmsg driver, that sends an "hello!" message
209on probe(), and whenever it receives an incoming message, it dumps its
210content to the console.
211
212#include <linux/kernel.h>
213#include <linux/module.h>
214#include <linux/rpmsg.h>
215
216static 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
223static 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
239static void __devexit rpmsg_sample_remove(struct rpmsg_channel *rpdev)
240{
241 dev_info(&rpdev->dev, "rpmsg sample client driver is removed\n");
242}
243
244static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = {
245 { .name = "rpmsg-client-sample" },
246 { },
247};
248MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_sample_id_table);
249
250static 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
259static int __init init(void)
260{
261 return register_rpmsg_driver(&rpmsg_sample_client);
262}
263module_init(init);
264
265static void __exit fini(void)
266{
267 unregister_rpmsg_driver(&rpmsg_sample_client);
268}
269module_exit(fini);
270
271Note: a similar sample which can be built and loaded can be found
272in samples/rpmsg/.
273
2744. Allocations of rpmsg channels:
275
276At this point we only support dynamic allocations of rpmsg channels.
277
278This is possible only with remote processors that have the VIRTIO_RPMSG_F_NS
279virtio device feature set. This feature bit means that the remote
280processor supports dynamic name service announcement messages.
281
282When this feature is enabled, creation of rpmsg devices (i.e. channels)
283is completely dynamic: the remote processor announces the existence of a
284remote rpmsg service by sending a name service message (which contains
285the name and rpmsg addr of the remote service, see struct rpmsg_ns_msg).
286
287This message is then handled by the rpmsg bus, which in turn dynamically
288creates and registers an rpmsg channel (which represents the remote service).
289If/when a relevant rpmsg driver is registered, it will be immediately probed
290by the bus, and can then start sending messages to the remote service.
291
292The plan is also to add static creation of rpmsg channels via the virtio
293config space, but it's not implemented yet.
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 27b34bf41d4c..516faf6d88ba 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -134,6 +134,8 @@ source "drivers/iommu/Kconfig"
134 134
135source "drivers/remoteproc/Kconfig" 135source "drivers/remoteproc/Kconfig"
136 136
137source "drivers/rpmsg/Kconfig"
138
137source "drivers/virt/Kconfig" 139source "drivers/virt/Kconfig"
138 140
139source "drivers/devfreq/Kconfig" 141source "drivers/devfreq/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index f1019b714f27..3fdc17709d36 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -127,6 +127,7 @@ obj-$(CONFIG_HWSPINLOCK) += hwspinlock/
127obj-$(CONFIG_NFC) += nfc/ 127obj-$(CONFIG_NFC) += nfc/
128obj-$(CONFIG_IOMMU_SUPPORT) += iommu/ 128obj-$(CONFIG_IOMMU_SUPPORT) += iommu/
129obj-$(CONFIG_REMOTEPROC) += remoteproc/ 129obj-$(CONFIG_REMOTEPROC) += remoteproc/
130obj-$(CONFIG_RPMSG) += rpmsg/
130 131
131# Virtualization drivers 132# Virtualization drivers
132obj-$(CONFIG_VIRT_DRIVERS) += virt/ 133obj-$(CONFIG_VIRT_DRIVERS) += virt/
diff --git a/drivers/rpmsg/Kconfig b/drivers/rpmsg/Kconfig
new file mode 100644
index 000000000000..811fede35bd8
--- /dev/null
+++ b/drivers/rpmsg/Kconfig
@@ -0,0 +1,5 @@
1# RPMSG always gets selected by whoever wants it
2config RPMSG
3 tristate
4 select VIRTIO
5 select VIRTIO_RING
diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile
new file mode 100644
index 000000000000..7617fcb8259f
--- /dev/null
+++ b/drivers/rpmsg/Makefile
@@ -0,0 +1 @@
obj-$(CONFIG_RPMSG) += virtio_rpmsg_bus.o
diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
new file mode 100644
index 000000000000..257683e7fe8a
--- /dev/null
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -0,0 +1,1026 @@
1/*
2 * Virtio-based 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/virtio.h>
25#include <linux/virtio_ids.h>
26#include <linux/virtio_config.h>
27#include <linux/scatterlist.h>
28#include <linux/dma-mapping.h>
29#include <linux/slab.h>
30#include <linux/idr.h>
31#include <linux/jiffies.h>
32#include <linux/sched.h>
33#include <linux/wait.h>
34#include <linux/rpmsg.h>
35#include <linux/mutex.h>
36
37/**
38 * struct virtproc_info - virtual remote processor state
39 * @vdev: the virtio device
40 * @rvq: rx virtqueue
41 * @svq: tx virtqueue
42 * @rbufs: kernel address of rx buffers
43 * @sbufs: kernel address of tx buffers
44 * @last_sbuf: index of last tx buffer used
45 * @bufs_dma: dma base addr of the buffers
46 * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders.
47 * sending a message might require waking up a dozing remote
48 * processor, which involves sleeping, hence the mutex.
49 * @endpoints: idr of local endpoints, allows fast retrieval
50 * @endpoints_lock: lock of the endpoints set
51 * @sendq: wait queue of sending contexts waiting for a tx buffers
52 * @sleepers: number of senders that are waiting for a tx buffer
53 * @ns_ept: the bus's name service endpoint
54 *
55 * This structure stores the rpmsg state of a given virtio remote processor
56 * device (there might be several virtio proc devices for each physical
57 * remote processor).
58 */
59struct virtproc_info {
60 struct virtio_device *vdev;
61 struct virtqueue *rvq, *svq;
62 void *rbufs, *sbufs;
63 int last_sbuf;
64 dma_addr_t bufs_dma;
65 struct mutex tx_lock;
66 struct idr endpoints;
67 struct mutex endpoints_lock;
68 wait_queue_head_t sendq;
69 atomic_t sleepers;
70 struct rpmsg_endpoint *ns_ept;
71};
72
73/**
74 * struct rpmsg_channel_info - internal channel info representation
75 * @name: name of service
76 * @src: local address
77 * @dst: destination address
78 */
79struct rpmsg_channel_info {
80 char name[RPMSG_NAME_SIZE];
81 u32 src;
82 u32 dst;
83};
84
85#define to_rpmsg_channel(d) container_of(d, struct rpmsg_channel, dev)
86#define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
87
88/*
89 * We're allocating 512 buffers of 512 bytes for communications, and then
90 * using the first 256 buffers for RX, and the last 256 buffers for TX.
91 *
92 * Each buffer will have 16 bytes for the msg header and 496 bytes for
93 * the payload.
94 *
95 * This will require a total space of 256KB for the buffers.
96 *
97 * We might also want to add support for user-provided buffers in time.
98 * This will allow bigger buffer size flexibility, and can also be used
99 * to achieve zero-copy messaging.
100 *
101 * Note that these numbers are purely a decision of this driver - we
102 * can change this without changing anything in the firmware of the remote
103 * processor.
104 */
105#define RPMSG_NUM_BUFS (512)
106#define RPMSG_BUF_SIZE (512)
107#define RPMSG_TOTAL_BUF_SPACE (RPMSG_NUM_BUFS * RPMSG_BUF_SIZE)
108
109/*
110 * Local addresses are dynamically allocated on-demand.
111 * We do not dynamically assign addresses from the low 1024 range,
112 * in order to reserve that address range for predefined services.
113 */
114#define RPMSG_RESERVED_ADDRESSES (1024)
115
116/* Address 53 is reserved for advertising remote services */
117#define RPMSG_NS_ADDR (53)
118
119/* sysfs show configuration fields */
120#define rpmsg_show_attr(field, path, format_string) \
121static ssize_t \
122field##_show(struct device *dev, \
123 struct device_attribute *attr, char *buf) \
124{ \
125 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \
126 \
127 return sprintf(buf, format_string, rpdev->path); \
128}
129
130/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
131rpmsg_show_attr(name, id.name, "%s\n");
132rpmsg_show_attr(src, src, "0x%x\n");
133rpmsg_show_attr(dst, dst, "0x%x\n");
134rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
135
136/*
137 * Unique (and free running) index for rpmsg devices.
138 *
139 * Yeah, we're not recycling those numbers (yet?). will be easy
140 * to change if/when we want to.
141 */
142static unsigned int rpmsg_dev_index;
143
144static ssize_t modalias_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
146{
147 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
148
149 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
150}
151
152static struct device_attribute rpmsg_dev_attrs[] = {
153 __ATTR_RO(name),
154 __ATTR_RO(modalias),
155 __ATTR_RO(dst),
156 __ATTR_RO(src),
157 __ATTR_RO(announce),
158 __ATTR_NULL
159};
160
161/* rpmsg devices and drivers are matched using the service name */
162static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev,
163 const struct rpmsg_device_id *id)
164{
165 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
166}
167
168/* match rpmsg channel and rpmsg driver */
169static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
170{
171 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
172 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
173 const struct rpmsg_device_id *ids = rpdrv->id_table;
174 unsigned int i;
175
176 for (i = 0; ids[i].name[0]; i++)
177 if (rpmsg_id_match(rpdev, &ids[i]))
178 return 1;
179
180 return 0;
181}
182
183static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
184{
185 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
186
187 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
188 rpdev->id.name);
189}
190
191/* for more info, see below documentation of rpmsg_create_ept() */
192static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
193 struct rpmsg_channel *rpdev, rpmsg_rx_cb_t cb,
194 void *priv, u32 addr)
195{
196 int err, tmpaddr, request;
197 struct rpmsg_endpoint *ept;
198 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
199
200 if (!idr_pre_get(&vrp->endpoints, GFP_KERNEL))
201 return NULL;
202
203 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
204 if (!ept) {
205 dev_err(dev, "failed to kzalloc a new ept\n");
206 return NULL;
207 }
208
209 ept->rpdev = rpdev;
210 ept->cb = cb;
211 ept->priv = priv;
212
213 /* do we need to allocate a local address ? */
214 request = addr == RPMSG_ADDR_ANY ? RPMSG_RESERVED_ADDRESSES : addr;
215
216 mutex_lock(&vrp->endpoints_lock);
217
218 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
219 err = idr_get_new_above(&vrp->endpoints, ept, request, &tmpaddr);
220 if (err) {
221 dev_err(dev, "idr_get_new_above failed: %d\n", err);
222 goto free_ept;
223 }
224
225 /* make sure the user's address request is fulfilled, if relevant */
226 if (addr != RPMSG_ADDR_ANY && tmpaddr != addr) {
227 dev_err(dev, "address 0x%x already in use\n", addr);
228 goto rem_idr;
229 }
230
231 ept->addr = tmpaddr;
232
233 mutex_unlock(&vrp->endpoints_lock);
234
235 return ept;
236
237rem_idr:
238 idr_remove(&vrp->endpoints, request);
239free_ept:
240 mutex_unlock(&vrp->endpoints_lock);
241 kfree(ept);
242 return NULL;
243}
244
245/**
246 * rpmsg_create_ept() - create a new rpmsg_endpoint
247 * @rpdev: rpmsg channel device
248 * @cb: rx callback handler
249 * @priv: private data for the driver's use
250 * @addr: local rpmsg address to bind with @cb
251 *
252 * Every rpmsg address in the system is bound to an rx callback (so when
253 * inbound messages arrive, they are dispatched by the rpmsg bus using the
254 * appropriate callback handler) by means of an rpmsg_endpoint struct.
255 *
256 * This function allows drivers to create such an endpoint, and by that,
257 * bind a callback, and possibly some private data too, to an rpmsg address
258 * (either one that is known in advance, or one that will be dynamically
259 * assigned for them).
260 *
261 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
262 * is already created for them when they are probed by the rpmsg bus
263 * (using the rx callback provided when they registered to the rpmsg bus).
264 *
265 * So things should just work for simple drivers: they already have an
266 * endpoint, their rx callback is bound to their rpmsg address, and when
267 * relevant inbound messages arrive (i.e. messages which their dst address
268 * equals to the src address of their rpmsg channel), the driver's handler
269 * is invoked to process it.
270 *
271 * That said, more complicated drivers might do need to allocate
272 * additional rpmsg addresses, and bind them to different rx callbacks.
273 * To accomplish that, those drivers need to call this function.
274 *
275 * Drivers should provide their @rpdev channel (so the new endpoint would belong
276 * to the same remote processor their channel belongs to), an rx callback
277 * function, an optional private data (which is provided back when the
278 * rx callback is invoked), and an address they want to bind with the
279 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
280 * dynamically assign them an available rpmsg address (drivers should have
281 * a very good reason why not to always use RPMSG_ADDR_ANY here).
282 *
283 * Returns a pointer to the endpoint on success, or NULL on error.
284 */
285struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
286 rpmsg_rx_cb_t cb, void *priv, u32 addr)
287{
288 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
289}
290EXPORT_SYMBOL(rpmsg_create_ept);
291
292/**
293 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
294 * @ept: endpoing to destroy
295 *
296 * Should be used by drivers to destroy an rpmsg endpoint previously
297 * created with rpmsg_create_ept().
298 */
299void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
300{
301 struct virtproc_info *vrp = ept->rpdev->vrp;
302
303 mutex_lock(&vrp->endpoints_lock);
304 idr_remove(&vrp->endpoints, ept->addr);
305 mutex_unlock(&vrp->endpoints_lock);
306
307 kfree(ept);
308}
309EXPORT_SYMBOL(rpmsg_destroy_ept);
310
311/*
312 * when an rpmsg driver is probed with a channel, we seamlessly create
313 * it an endpoint, binding its rx callback to a unique local rpmsg
314 * address.
315 *
316 * if we need to, we also announce about this channel to the remote
317 * processor (needed in case the driver is exposing an rpmsg service).
318 */
319static int rpmsg_dev_probe(struct device *dev)
320{
321 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
322 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
323 struct virtproc_info *vrp = rpdev->vrp;
324 struct rpmsg_endpoint *ept;
325 int err;
326
327 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
328 if (!ept) {
329 dev_err(dev, "failed to create endpoint\n");
330 err = -ENOMEM;
331 goto out;
332 }
333
334 rpdev->ept = ept;
335 rpdev->src = ept->addr;
336
337 err = rpdrv->probe(rpdev);
338 if (err) {
339 dev_err(dev, "%s: failed: %d\n", __func__, err);
340 rpmsg_destroy_ept(ept);
341 goto out;
342 }
343
344 /* need to tell remote processor's name service about this channel ? */
345 if (rpdev->announce &&
346 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
347 struct rpmsg_ns_msg nsm;
348
349 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
350 nsm.addr = rpdev->src;
351 nsm.flags = RPMSG_NS_CREATE;
352
353 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
354 if (err)
355 dev_err(dev, "failed to announce service %d\n", err);
356 }
357
358out:
359 return err;
360}
361
362static int rpmsg_dev_remove(struct device *dev)
363{
364 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
365 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
366 struct virtproc_info *vrp = rpdev->vrp;
367 int err = 0;
368
369 /* tell remote processor's name service we're removing this channel */
370 if (rpdev->announce &&
371 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
372 struct rpmsg_ns_msg nsm;
373
374 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
375 nsm.addr = rpdev->src;
376 nsm.flags = RPMSG_NS_DESTROY;
377
378 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
379 if (err)
380 dev_err(dev, "failed to announce service %d\n", err);
381 }
382
383 rpdrv->remove(rpdev);
384
385 rpmsg_destroy_ept(rpdev->ept);
386
387 return err;
388}
389
390static struct bus_type rpmsg_bus = {
391 .name = "rpmsg",
392 .match = rpmsg_dev_match,
393 .dev_attrs = rpmsg_dev_attrs,
394 .uevent = rpmsg_uevent,
395 .probe = rpmsg_dev_probe,
396 .remove = rpmsg_dev_remove,
397};
398
399/**
400 * register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
401 * @rpdrv: pointer to a struct rpmsg_driver
402 *
403 * Returns 0 on success, and an appropriate error value on failure.
404 */
405int register_rpmsg_driver(struct rpmsg_driver *rpdrv)
406{
407 rpdrv->drv.bus = &rpmsg_bus;
408 return driver_register(&rpdrv->drv);
409}
410EXPORT_SYMBOL(register_rpmsg_driver);
411
412/**
413 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
414 * @rpdrv: pointer to a struct rpmsg_driver
415 *
416 * Returns 0 on success, and an appropriate error value on failure.
417 */
418void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
419{
420 driver_unregister(&rpdrv->drv);
421}
422EXPORT_SYMBOL(unregister_rpmsg_driver);
423
424static void rpmsg_release_device(struct device *dev)
425{
426 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
427
428 kfree(rpdev);
429}
430
431/*
432 * match an rpmsg channel with a channel info struct.
433 * this is used to make sure we're not creating rpmsg devices for channels
434 * that already exist.
435 */
436static int rpmsg_channel_match(struct device *dev, void *data)
437{
438 struct rpmsg_channel_info *chinfo = data;
439 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
440
441 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
442 return 0;
443
444 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
445 return 0;
446
447 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
448 return 0;
449
450 /* found a match ! */
451 return 1;
452}
453
454/*
455 * create an rpmsg channel using its name and address info.
456 * this function will be used to create both static and dynamic
457 * channels.
458 */
459static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
460 struct rpmsg_channel_info *chinfo)
461{
462 struct rpmsg_channel *rpdev;
463 struct device *tmp, *dev = &vrp->vdev->dev;
464 int ret;
465
466 /* make sure a similar channel doesn't already exist */
467 tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
468 if (tmp) {
469 /* decrement the matched device's refcount back */
470 put_device(tmp);
471 dev_err(dev, "channel %s:%x:%x already exist\n",
472 chinfo->name, chinfo->src, chinfo->dst);
473 return NULL;
474 }
475
476 rpdev = kzalloc(sizeof(struct rpmsg_channel), GFP_KERNEL);
477 if (!rpdev) {
478 pr_err("kzalloc failed\n");
479 return NULL;
480 }
481
482 rpdev->vrp = vrp;
483 rpdev->src = chinfo->src;
484 rpdev->dst = chinfo->dst;
485
486 /*
487 * rpmsg server channels has predefined local address (for now),
488 * and their existence needs to be announced remotely
489 */
490 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY ? true : false;
491
492 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
493
494 /* very simple device indexing plumbing which is enough for now */
495 dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
496
497 rpdev->dev.parent = &vrp->vdev->dev;
498 rpdev->dev.bus = &rpmsg_bus;
499 rpdev->dev.release = rpmsg_release_device;
500
501 ret = device_register(&rpdev->dev);
502 if (ret) {
503 dev_err(dev, "device_register failed: %d\n", ret);
504 put_device(&rpdev->dev);
505 return NULL;
506 }
507
508 return rpdev;
509}
510
511/*
512 * find an existing channel using its name + address properties,
513 * and destroy it
514 */
515static int rpmsg_destroy_channel(struct virtproc_info *vrp,
516 struct rpmsg_channel_info *chinfo)
517{
518 struct virtio_device *vdev = vrp->vdev;
519 struct device *dev;
520
521 dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
522 if (!dev)
523 return -EINVAL;
524
525 device_unregister(dev);
526
527 put_device(dev);
528
529 return 0;
530}
531
532/* super simple buffer "allocator" that is just enough for now */
533static void *get_a_tx_buf(struct virtproc_info *vrp)
534{
535 unsigned int len;
536 void *ret;
537
538 /* support multiple concurrent senders */
539 mutex_lock(&vrp->tx_lock);
540
541 /*
542 * either pick the next unused tx buffer
543 * (half of our buffers are used for sending messages)
544 */
545 if (vrp->last_sbuf < RPMSG_NUM_BUFS / 2)
546 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
547 /* or recycle a used one */
548 else
549 ret = virtqueue_get_buf(vrp->svq, &len);
550
551 mutex_unlock(&vrp->tx_lock);
552
553 return ret;
554}
555
556/**
557 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
558 * @vrp: virtual remote processor state
559 *
560 * This function is called before a sender is blocked, waiting for
561 * a tx buffer to become available.
562 *
563 * If we already have blocking senders, this function merely increases
564 * the "sleepers" reference count, and exits.
565 *
566 * Otherwise, if this is the first sender to block, we also enable
567 * virtio's tx callbacks, so we'd be immediately notified when a tx
568 * buffer is consumed (we rely on virtio's tx callback in order
569 * to wake up sleeping senders as soon as a tx buffer is used by the
570 * remote processor).
571 */
572static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
573{
574 /* support multiple concurrent senders */
575 mutex_lock(&vrp->tx_lock);
576
577 /* are we the first sleeping context waiting for tx buffers ? */
578 if (atomic_inc_return(&vrp->sleepers) == 1)
579 /* enable "tx-complete" interrupts before dozing off */
580 virtqueue_enable_cb(vrp->svq);
581
582 mutex_unlock(&vrp->tx_lock);
583}
584
585/**
586 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
587 * @vrp: virtual remote processor state
588 *
589 * This function is called after a sender, that waited for a tx buffer
590 * to become available, is unblocked.
591 *
592 * If we still have blocking senders, this function merely decreases
593 * the "sleepers" reference count, and exits.
594 *
595 * Otherwise, if there are no more blocking senders, we also disable
596 * virtio's tx callbacks, to avoid the overhead incurred with handling
597 * those (now redundant) interrupts.
598 */
599static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
600{
601 /* support multiple concurrent senders */
602 mutex_lock(&vrp->tx_lock);
603
604 /* are we the last sleeping context waiting for tx buffers ? */
605 if (atomic_dec_and_test(&vrp->sleepers))
606 /* disable "tx-complete" interrupts */
607 virtqueue_disable_cb(vrp->svq);
608
609 mutex_unlock(&vrp->tx_lock);
610}
611
612/**
613 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
614 * @rpdev: the rpmsg channel
615 * @src: source address
616 * @dst: destination address
617 * @data: payload of message
618 * @len: length of payload
619 * @wait: indicates whether caller should block in case no TX buffers available
620 *
621 * This function is the base implementation for all of the rpmsg sending API.
622 *
623 * It will send @data of length @len to @dst, and say it's from @src. The
624 * message will be sent to the remote processor which the @rpdev channel
625 * belongs to.
626 *
627 * The message is sent using one of the TX buffers that are available for
628 * communication with this remote processor.
629 *
630 * If @wait is true, the caller will be blocked until either a TX buffer is
631 * available, or 15 seconds elapses (we don't want callers to
632 * sleep indefinitely due to misbehaving remote processors), and in that
633 * case -ERESTARTSYS is returned. The number '15' itself was picked
634 * arbitrarily; there's little point in asking drivers to provide a timeout
635 * value themselves.
636 *
637 * Otherwise, if @wait is false, and there are no TX buffers available,
638 * the function will immediately fail, and -ENOMEM will be returned.
639 *
640 * Normally drivers shouldn't use this function directly; instead, drivers
641 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
642 * (see include/linux/rpmsg.h).
643 *
644 * Returns 0 on success and an appropriate error value on failure.
645 */
646int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
647 void *data, int len, bool wait)
648{
649 struct virtproc_info *vrp = rpdev->vrp;
650 struct device *dev = &rpdev->dev;
651 struct scatterlist sg;
652 struct rpmsg_hdr *msg;
653 int err;
654
655 /* bcasting isn't allowed */
656 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
657 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
658 return -EINVAL;
659 }
660
661 /*
662 * We currently use fixed-sized buffers, and therefore the payload
663 * length is limited.
664 *
665 * One of the possible improvements here is either to support
666 * user-provided buffers (and then we can also support zero-copy
667 * messaging), or to improve the buffer allocator, to support
668 * variable-length buffer sizes.
669 */
670 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
671 dev_err(dev, "message is too big (%d)\n", len);
672 return -EMSGSIZE;
673 }
674
675 /* grab a buffer */
676 msg = get_a_tx_buf(vrp);
677 if (!msg && !wait)
678 return -ENOMEM;
679
680 /* no free buffer ? wait for one (but bail after 15 seconds) */
681 while (!msg) {
682 /* enable "tx-complete" interrupts, if not already enabled */
683 rpmsg_upref_sleepers(vrp);
684
685 /*
686 * sleep until a free buffer is available or 15 secs elapse.
687 * the timeout period is not configurable because there's
688 * little point in asking drivers to specify that.
689 * if later this happens to be required, it'd be easy to add.
690 */
691 err = wait_event_interruptible_timeout(vrp->sendq,
692 (msg = get_a_tx_buf(vrp)),
693 msecs_to_jiffies(15000));
694
695 /* disable "tx-complete" interrupts if we're the last sleeper */
696 rpmsg_downref_sleepers(vrp);
697
698 /* timeout ? */
699 if (!err) {
700 dev_err(dev, "timeout waiting for a tx buffer\n");
701 return -ERESTARTSYS;
702 }
703 }
704
705 msg->len = len;
706 msg->flags = 0;
707 msg->src = src;
708 msg->dst = dst;
709 msg->reserved = 0;
710 memcpy(msg->data, data, len);
711
712 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
713 msg->src, msg->dst, msg->len,
714 msg->flags, msg->reserved);
715 print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
716 msg, sizeof(*msg) + msg->len, true);
717
718 sg_init_one(&sg, msg, sizeof(*msg) + len);
719
720 mutex_lock(&vrp->tx_lock);
721
722 /* add message to the remote processor's virtqueue */
723 err = virtqueue_add_buf_gfp(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL);
724 if (err < 0) {
725 /*
726 * need to reclaim the buffer here, otherwise it's lost
727 * (memory won't leak, but rpmsg won't use it again for TX).
728 * this will wait for a buffer management overhaul.
729 */
730 dev_err(dev, "virtqueue_add_buf_gfp failed: %d\n", err);
731 goto out;
732 }
733
734 /* tell the remote processor it has a pending message to read */
735 virtqueue_kick(vrp->svq);
736
737 err = 0;
738out:
739 mutex_unlock(&vrp->tx_lock);
740 return err;
741}
742EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
743
744/* called when an rx buffer is used, and it's time to digest a message */
745static void rpmsg_recv_done(struct virtqueue *rvq)
746{
747 struct rpmsg_hdr *msg;
748 unsigned int len;
749 struct rpmsg_endpoint *ept;
750 struct scatterlist sg;
751 struct virtproc_info *vrp = rvq->vdev->priv;
752 struct device *dev = &rvq->vdev->dev;
753 int err;
754
755 msg = virtqueue_get_buf(rvq, &len);
756 if (!msg) {
757 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
758 return;
759 }
760
761 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
762 msg->src, msg->dst, msg->len,
763 msg->flags, msg->reserved);
764 print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
765 msg, sizeof(*msg) + msg->len, true);
766
767 /* use the dst addr to fetch the callback of the appropriate user */
768 mutex_lock(&vrp->endpoints_lock);
769 ept = idr_find(&vrp->endpoints, msg->dst);
770 mutex_unlock(&vrp->endpoints_lock);
771
772 if (ept && ept->cb)
773 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv, msg->src);
774 else
775 dev_warn(dev, "msg received with no recepient\n");
776
777 sg_init_one(&sg, msg, sizeof(*msg) + len);
778
779 /* add the buffer back to the remote processor's virtqueue */
780 err = virtqueue_add_buf_gfp(vrp->rvq, &sg, 0, 1, msg, GFP_KERNEL);
781 if (err < 0) {
782 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
783 return;
784 }
785
786 /* tell the remote processor we added another available rx buffer */
787 virtqueue_kick(vrp->rvq);
788}
789
790/*
791 * This is invoked whenever the remote processor completed processing
792 * a TX msg we just sent it, and the buffer is put back to the used ring.
793 *
794 * Normally, though, we suppress this "tx complete" interrupt in order to
795 * avoid the incurred overhead.
796 */
797static void rpmsg_xmit_done(struct virtqueue *svq)
798{
799 struct virtproc_info *vrp = svq->vdev->priv;
800
801 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
802
803 /* wake up potential senders that are waiting for a tx buffer */
804 wake_up_interruptible(&vrp->sendq);
805}
806
807/* invoked when a name service announcement arrives */
808static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
809 void *priv, u32 src)
810{
811 struct rpmsg_ns_msg *msg = data;
812 struct rpmsg_channel *newch;
813 struct rpmsg_channel_info chinfo;
814 struct virtproc_info *vrp = priv;
815 struct device *dev = &vrp->vdev->dev;
816 int ret;
817
818 print_hex_dump(KERN_DEBUG, "NS announcement: ",
819 DUMP_PREFIX_NONE, 16, 1,
820 data, len, true);
821
822 if (len != sizeof(*msg)) {
823 dev_err(dev, "malformed ns msg (%d)\n", len);
824 return;
825 }
826
827 /*
828 * the name service ept does _not_ belong to a real rpmsg channel,
829 * and is handled by the rpmsg bus itself.
830 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
831 * in somehow.
832 */
833 if (rpdev) {
834 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
835 return;
836 }
837
838 /* don't trust the remote processor for null terminating the name */
839 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
840
841 dev_info(dev, "%sing channel %s addr 0x%x\n",
842 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
843 msg->name, msg->addr);
844
845 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
846 chinfo.src = RPMSG_ADDR_ANY;
847 chinfo.dst = msg->addr;
848
849 if (msg->flags & RPMSG_NS_DESTROY) {
850 ret = rpmsg_destroy_channel(vrp, &chinfo);
851 if (ret)
852 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
853 } else {
854 newch = rpmsg_create_channel(vrp, &chinfo);
855 if (!newch)
856 dev_err(dev, "rpmsg_create_channel failed\n");
857 }
858}
859
860static int rpmsg_probe(struct virtio_device *vdev)
861{
862 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
863 const char *names[] = { "input", "output" };
864 struct virtqueue *vqs[2];
865 struct virtproc_info *vrp;
866 void *bufs_va;
867 int err = 0, i;
868
869 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
870 if (!vrp)
871 return -ENOMEM;
872
873 vrp->vdev = vdev;
874
875 idr_init(&vrp->endpoints);
876 mutex_init(&vrp->endpoints_lock);
877 mutex_init(&vrp->tx_lock);
878 init_waitqueue_head(&vrp->sendq);
879
880 /* We expect two virtqueues, rx and tx (and in this order) */
881 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
882 if (err)
883 goto free_vrp;
884
885 vrp->rvq = vqs[0];
886 vrp->svq = vqs[1];
887
888 /* allocate coherent memory for the buffers */
889 bufs_va = dma_alloc_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
890 &vrp->bufs_dma, GFP_KERNEL);
891 if (!bufs_va)
892 goto vqs_del;
893
894 dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%x\n", bufs_va,
895 vrp->bufs_dma);
896
897 /* half of the buffers is dedicated for RX */
898 vrp->rbufs = bufs_va;
899
900 /* and half is dedicated for TX */
901 vrp->sbufs = bufs_va + RPMSG_TOTAL_BUF_SPACE / 2;
902
903 /* set up the receive buffers */
904 for (i = 0; i < RPMSG_NUM_BUFS / 2; i++) {
905 struct scatterlist sg;
906 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
907
908 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
909
910 err = virtqueue_add_buf_gfp(vrp->rvq, &sg, 0, 1, cpu_addr,
911 GFP_KERNEL);
912 WARN_ON(err < 0); /* sanity check; this can't really happen */
913 }
914
915 /* suppress "tx-complete" interrupts */
916 virtqueue_disable_cb(vrp->svq);
917
918 vdev->priv = vrp;
919
920 /* if supported by the remote processor, enable the name service */
921 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
922 /* a dedicated endpoint handles the name service msgs */
923 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
924 vrp, RPMSG_NS_ADDR);
925 if (!vrp->ns_ept) {
926 dev_err(&vdev->dev, "failed to create the ns ept\n");
927 err = -ENOMEM;
928 goto free_coherent;
929 }
930 }
931
932 /* tell the remote processor it can start sending messages */
933 virtqueue_kick(vrp->rvq);
934
935 dev_info(&vdev->dev, "rpmsg host is online\n");
936
937 return 0;
938
939free_coherent:
940 dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE, bufs_va,
941 vrp->bufs_dma);
942vqs_del:
943 vdev->config->del_vqs(vrp->vdev);
944free_vrp:
945 kfree(vrp);
946 return err;
947}
948
949static int rpmsg_remove_device(struct device *dev, void *data)
950{
951 device_unregister(dev);
952
953 return 0;
954}
955
956static void __devexit rpmsg_remove(struct virtio_device *vdev)
957{
958 struct virtproc_info *vrp = vdev->priv;
959 int ret;
960
961 vdev->config->reset(vdev);
962
963 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
964 if (ret)
965 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
966
967 idr_remove_all(&vrp->endpoints);
968 idr_destroy(&vrp->endpoints);
969
970 vdev->config->del_vqs(vrp->vdev);
971
972 dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
973 vrp->rbufs, vrp->bufs_dma);
974
975 kfree(vrp);
976}
977
978static struct virtio_device_id id_table[] = {
979 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
980 { 0 },
981};
982
983static unsigned int features[] = {
984 VIRTIO_RPMSG_F_NS,
985};
986
987static struct virtio_driver virtio_ipc_driver = {
988 .feature_table = features,
989 .feature_table_size = ARRAY_SIZE(features),
990 .driver.name = KBUILD_MODNAME,
991 .driver.owner = THIS_MODULE,
992 .id_table = id_table,
993 .probe = rpmsg_probe,
994 .remove = __devexit_p(rpmsg_remove),
995};
996
997static int __init rpmsg_init(void)
998{
999 int ret;
1000
1001 ret = bus_register(&rpmsg_bus);
1002 if (ret) {
1003 pr_err("failed to register rpmsg bus: %d\n", ret);
1004 return ret;
1005 }
1006
1007 ret = register_virtio_driver(&virtio_ipc_driver);
1008 if (ret) {
1009 pr_err("failed to register virtio driver: %d\n", ret);
1010 bus_unregister(&rpmsg_bus);
1011 }
1012
1013 return ret;
1014}
1015module_init(rpmsg_init);
1016
1017static void __exit rpmsg_fini(void)
1018{
1019 unregister_virtio_driver(&virtio_ipc_driver);
1020 bus_unregister(&rpmsg_bus);
1021}
1022module_exit(rpmsg_fini);
1023
1024MODULE_DEVICE_TABLE(virtio, id_table);
1025MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1026MODULE_LICENSE("GPL v2");
diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
index b29e7f6f8fa5..92aef8aaef1a 100644
--- a/include/linux/mod_devicetable.h
+++ b/include/linux/mod_devicetable.h
@@ -414,6 +414,15 @@ struct hv_vmbus_device_id {
414 __attribute__((aligned(sizeof(kernel_ulong_t)))); 414 __attribute__((aligned(sizeof(kernel_ulong_t))));
415}; 415};
416 416
417/* rpmsg */
418
419#define RPMSG_NAME_SIZE 32
420#define RPMSG_DEVICE_MODALIAS_FMT "rpmsg:%s"
421
422struct rpmsg_device_id {
423 char name[RPMSG_NAME_SIZE];
424};
425
417/* i2c */ 426/* i2c */
418 427
419#define I2C_NAME_SIZE 20 428#define I2C_NAME_SIZE 20
diff --git a/include/linux/rpmsg.h b/include/linux/rpmsg.h
new file mode 100644
index 000000000000..a8e50e44203c
--- /dev/null
+++ b/include/linux/rpmsg.h
@@ -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 */
56struct 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 */
77struct 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 */
89enum rpmsg_ns_flags {
90 RPMSG_NS_CREATE = 0,
91 RPMSG_NS_DESTROY = 1,
92};
93
94#define RPMSG_ADDR_ANY 0xFFFFFFFF
95
96struct 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 */
108struct 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
118typedef 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 */
141struct 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 */
156struct 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
164int register_rpmsg_device(struct rpmsg_channel *dev);
165void unregister_rpmsg_device(struct rpmsg_channel *dev);
166int register_rpmsg_driver(struct rpmsg_driver *drv);
167void unregister_rpmsg_driver(struct rpmsg_driver *drv);
168void rpmsg_destroy_ept(struct rpmsg_endpoint *);
169struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *,
170 rpmsg_rx_cb_t cb, void *priv, u32 addr);
171int
172rpmsg_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 */
191static 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 */
216static inline
217int 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 */
244static inline
245int 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 */
267static inline
268int 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 */
292static inline
293int 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 */
319static inline
320int 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 */
diff --git a/include/linux/virtio_ids.h b/include/linux/virtio_ids.h
index 85bb0bb66ffc..b37c5212265e 100644
--- a/include/linux/virtio_ids.h
+++ b/include/linux/virtio_ids.h
@@ -34,6 +34,7 @@
34#define VIRTIO_ID_CONSOLE 3 /* virtio console */ 34#define VIRTIO_ID_CONSOLE 3 /* virtio console */
35#define VIRTIO_ID_RNG 4 /* virtio ring */ 35#define VIRTIO_ID_RNG 4 /* virtio ring */
36#define VIRTIO_ID_BALLOON 5 /* virtio balloon */ 36#define VIRTIO_ID_BALLOON 5 /* virtio balloon */
37#define VIRTIO_ID_RPMSG 7 /* virtio remote processor messaging */
37#define VIRTIO_ID_9P 9 /* 9p virtio console */ 38#define VIRTIO_ID_9P 9 /* 9p virtio console */
38 39
39#endif /* _LINUX_VIRTIO_IDS_H */ 40#endif /* _LINUX_VIRTIO_IDS_H */