xref: /OK3568_Linux_fs/kernel/drivers/rpmsg/virtio_rpmsg_bus.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Virtio-based remote processor messaging bus
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2011 Texas Instruments, Inc.
6*4882a593Smuzhiyun  * Copyright (C) 2011 Google, Inc.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Ohad Ben-Cohen <ohad@wizery.com>
9*4882a593Smuzhiyun  * Brian Swetland <swetland@google.com>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define pr_fmt(fmt) "%s: " fmt, __func__
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/dma-mapping.h>
15*4882a593Smuzhiyun #include <linux/idr.h>
16*4882a593Smuzhiyun #include <linux/jiffies.h>
17*4882a593Smuzhiyun #include <linux/kernel.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/mutex.h>
20*4882a593Smuzhiyun #include <linux/of_device.h>
21*4882a593Smuzhiyun #include <linux/rpmsg.h>
22*4882a593Smuzhiyun #include <linux/scatterlist.h>
23*4882a593Smuzhiyun #include <linux/slab.h>
24*4882a593Smuzhiyun #include <linux/sched.h>
25*4882a593Smuzhiyun #include <linux/virtio.h>
26*4882a593Smuzhiyun #include <linux/virtio_byteorder.h>
27*4882a593Smuzhiyun #include <linux/virtio_ids.h>
28*4882a593Smuzhiyun #include <linux/virtio_config.h>
29*4882a593Smuzhiyun #include <linux/wait.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include "rpmsg_internal.h"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun  * struct virtproc_info - virtual remote processor state
35*4882a593Smuzhiyun  * @vdev:	the virtio device
36*4882a593Smuzhiyun  * @rvq:	rx virtqueue
37*4882a593Smuzhiyun  * @svq:	tx virtqueue
38*4882a593Smuzhiyun  * @rbufs:	kernel address of rx buffers
39*4882a593Smuzhiyun  * @sbufs:	kernel address of tx buffers
40*4882a593Smuzhiyun  * @num_bufs:	total number of buffers for rx and tx
41*4882a593Smuzhiyun  * @buf_size:   size of one rx or tx buffer
42*4882a593Smuzhiyun  * @last_sbuf:	index of last tx buffer used
43*4882a593Smuzhiyun  * @bufs_dma:	dma base addr of the buffers
44*4882a593Smuzhiyun  * @tx_lock:	protects svq, sbufs and sleepers, to allow concurrent senders.
45*4882a593Smuzhiyun  *		sending a message might require waking up a dozing remote
46*4882a593Smuzhiyun  *		processor, which involves sleeping, hence the mutex.
47*4882a593Smuzhiyun  * @endpoints:	idr of local endpoints, allows fast retrieval
48*4882a593Smuzhiyun  * @endpoints_lock: lock of the endpoints set
49*4882a593Smuzhiyun  * @sendq:	wait queue of sending contexts waiting for a tx buffers
50*4882a593Smuzhiyun  * @sleepers:	number of senders that are waiting for a tx buffer
51*4882a593Smuzhiyun  * @ns_ept:	the bus's name service endpoint
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * This structure stores the rpmsg state of a given virtio remote processor
54*4882a593Smuzhiyun  * device (there might be several virtio proc devices for each physical
55*4882a593Smuzhiyun  * remote processor).
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun struct virtproc_info {
58*4882a593Smuzhiyun 	struct virtio_device *vdev;
59*4882a593Smuzhiyun 	struct virtqueue *rvq, *svq;
60*4882a593Smuzhiyun 	void *rbufs, *sbufs;
61*4882a593Smuzhiyun 	unsigned int num_bufs;
62*4882a593Smuzhiyun 	unsigned int buf_size;
63*4882a593Smuzhiyun 	int last_sbuf;
64*4882a593Smuzhiyun 	dma_addr_t bufs_dma;
65*4882a593Smuzhiyun 	struct mutex tx_lock;
66*4882a593Smuzhiyun 	struct idr endpoints;
67*4882a593Smuzhiyun 	struct mutex endpoints_lock;
68*4882a593Smuzhiyun 	wait_queue_head_t sendq;
69*4882a593Smuzhiyun 	atomic_t sleepers;
70*4882a593Smuzhiyun 	struct rpmsg_endpoint *ns_ept;
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /* The feature bitmap for virtio rpmsg */
74*4882a593Smuzhiyun #define VIRTIO_RPMSG_F_NS	0 /* RP supports name service notifications */
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /**
77*4882a593Smuzhiyun  * struct rpmsg_hdr - common header for all rpmsg messages
78*4882a593Smuzhiyun  * @src: source address
79*4882a593Smuzhiyun  * @dst: destination address
80*4882a593Smuzhiyun  * @reserved: reserved for future use
81*4882a593Smuzhiyun  * @len: length of payload (in bytes)
82*4882a593Smuzhiyun  * @flags: message flags
83*4882a593Smuzhiyun  * @data: @len bytes of message payload data
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * Every message sent(/received) on the rpmsg bus begins with this header.
86*4882a593Smuzhiyun  */
87*4882a593Smuzhiyun struct rpmsg_hdr {
88*4882a593Smuzhiyun 	__virtio32 src;
89*4882a593Smuzhiyun 	__virtio32 dst;
90*4882a593Smuzhiyun 	__virtio32 reserved;
91*4882a593Smuzhiyun 	__virtio16 len;
92*4882a593Smuzhiyun 	__virtio16 flags;
93*4882a593Smuzhiyun 	u8 data[];
94*4882a593Smuzhiyun } __packed;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /**
97*4882a593Smuzhiyun  * struct rpmsg_ns_msg - dynamic name service announcement message
98*4882a593Smuzhiyun  * @name: name of remote service that is published
99*4882a593Smuzhiyun  * @addr: address of remote service that is published
100*4882a593Smuzhiyun  * @flags: indicates whether service is created or destroyed
101*4882a593Smuzhiyun  *
102*4882a593Smuzhiyun  * This message is sent across to publish a new service, or announce
103*4882a593Smuzhiyun  * about its removal. When we receive these messages, an appropriate
104*4882a593Smuzhiyun  * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
105*4882a593Smuzhiyun  * or ->remove() handler of the appropriate rpmsg driver will be invoked
106*4882a593Smuzhiyun  * (if/as-soon-as one is registered).
107*4882a593Smuzhiyun  */
108*4882a593Smuzhiyun struct rpmsg_ns_msg {
109*4882a593Smuzhiyun 	char name[RPMSG_NAME_SIZE];
110*4882a593Smuzhiyun 	__virtio32 addr;
111*4882a593Smuzhiyun 	__virtio32 flags;
112*4882a593Smuzhiyun } __packed;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun  * enum rpmsg_ns_flags - dynamic name service announcement flags
116*4882a593Smuzhiyun  *
117*4882a593Smuzhiyun  * @RPMSG_NS_CREATE: a new remote service was just created
118*4882a593Smuzhiyun  * @RPMSG_NS_DESTROY: a known remote service was just destroyed
119*4882a593Smuzhiyun  */
120*4882a593Smuzhiyun enum rpmsg_ns_flags {
121*4882a593Smuzhiyun 	RPMSG_NS_CREATE		= 0,
122*4882a593Smuzhiyun 	RPMSG_NS_DESTROY	= 1,
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /**
126*4882a593Smuzhiyun  * struct virtio_rpmsg_channel - rpmsg channel descriptor
127*4882a593Smuzhiyun  * @rpdev: the rpmsg channel device
128*4882a593Smuzhiyun  * @vrp: the virtio remote processor device this channel belongs to
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * This structure stores the channel that links the rpmsg device to the virtio
131*4882a593Smuzhiyun  * remote processor device.
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun struct virtio_rpmsg_channel {
134*4882a593Smuzhiyun 	struct rpmsg_device rpdev;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	struct virtproc_info *vrp;
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun #define to_virtio_rpmsg_channel(_rpdev) \
140*4882a593Smuzhiyun 	container_of(_rpdev, struct virtio_rpmsg_channel, rpdev)
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun  * We're allocating buffers of 512 bytes each for communications. The
144*4882a593Smuzhiyun  * number of buffers will be computed from the number of buffers supported
145*4882a593Smuzhiyun  * by the vring, upto a maximum of 512 buffers (256 in each direction).
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * Each buffer will have 16 bytes for the msg header and 496 bytes for
148*4882a593Smuzhiyun  * the payload.
149*4882a593Smuzhiyun  *
150*4882a593Smuzhiyun  * This will utilize a maximum total space of 256KB for the buffers.
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * We might also want to add support for user-provided buffers in time.
153*4882a593Smuzhiyun  * This will allow bigger buffer size flexibility, and can also be used
154*4882a593Smuzhiyun  * to achieve zero-copy messaging.
155*4882a593Smuzhiyun  *
156*4882a593Smuzhiyun  * Note that these numbers are purely a decision of this driver - we
157*4882a593Smuzhiyun  * can change this without changing anything in the firmware of the remote
158*4882a593Smuzhiyun  * processor.
159*4882a593Smuzhiyun  */
160*4882a593Smuzhiyun #define MAX_RPMSG_NUM_BUFS	(512)
161*4882a593Smuzhiyun #define MAX_RPMSG_BUF_SIZE	(512)
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun  * Local addresses are dynamically allocated on-demand.
165*4882a593Smuzhiyun  * We do not dynamically assign addresses from the low 1024 range,
166*4882a593Smuzhiyun  * in order to reserve that address range for predefined services.
167*4882a593Smuzhiyun  */
168*4882a593Smuzhiyun #define RPMSG_RESERVED_ADDRESSES	(1024)
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /* Address 53 is reserved for advertising remote services */
171*4882a593Smuzhiyun #define RPMSG_NS_ADDR			(53)
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
174*4882a593Smuzhiyun static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
175*4882a593Smuzhiyun static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
176*4882a593Smuzhiyun 			       u32 dst);
177*4882a593Smuzhiyun static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
178*4882a593Smuzhiyun 					u32 dst, void *data, int len);
179*4882a593Smuzhiyun static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
180*4882a593Smuzhiyun static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
181*4882a593Smuzhiyun 				  int len, u32 dst);
182*4882a593Smuzhiyun static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
183*4882a593Smuzhiyun 					   u32 dst, void *data, int len);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun static const struct rpmsg_endpoint_ops virtio_endpoint_ops = {
186*4882a593Smuzhiyun 	.destroy_ept = virtio_rpmsg_destroy_ept,
187*4882a593Smuzhiyun 	.send = virtio_rpmsg_send,
188*4882a593Smuzhiyun 	.sendto = virtio_rpmsg_sendto,
189*4882a593Smuzhiyun 	.send_offchannel = virtio_rpmsg_send_offchannel,
190*4882a593Smuzhiyun 	.trysend = virtio_rpmsg_trysend,
191*4882a593Smuzhiyun 	.trysendto = virtio_rpmsg_trysendto,
192*4882a593Smuzhiyun 	.trysend_offchannel = virtio_rpmsg_trysend_offchannel,
193*4882a593Smuzhiyun };
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /**
196*4882a593Smuzhiyun  * rpmsg_sg_init - initialize scatterlist according to cpu address location
197*4882a593Smuzhiyun  * @sg: scatterlist to fill
198*4882a593Smuzhiyun  * @cpu_addr: virtual address of the buffer
199*4882a593Smuzhiyun  * @len: buffer length
200*4882a593Smuzhiyun  *
201*4882a593Smuzhiyun  * An internal function filling scatterlist according to virtual address
202*4882a593Smuzhiyun  * location (in vmalloc or in kernel).
203*4882a593Smuzhiyun  */
204*4882a593Smuzhiyun static void
rpmsg_sg_init(struct scatterlist * sg,void * cpu_addr,unsigned int len)205*4882a593Smuzhiyun rpmsg_sg_init(struct scatterlist *sg, void *cpu_addr, unsigned int len)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun 	if (is_vmalloc_addr(cpu_addr)) {
208*4882a593Smuzhiyun 		sg_init_table(sg, 1);
209*4882a593Smuzhiyun 		sg_set_page(sg, vmalloc_to_page(cpu_addr), len,
210*4882a593Smuzhiyun 			    offset_in_page(cpu_addr));
211*4882a593Smuzhiyun 	} else {
212*4882a593Smuzhiyun 		WARN_ON(!virt_addr_valid(cpu_addr));
213*4882a593Smuzhiyun 		sg_init_one(sg, cpu_addr, len);
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /**
218*4882a593Smuzhiyun  * __ept_release() - deallocate an rpmsg endpoint
219*4882a593Smuzhiyun  * @kref: the ept's reference count
220*4882a593Smuzhiyun  *
221*4882a593Smuzhiyun  * This function deallocates an ept, and is invoked when its @kref refcount
222*4882a593Smuzhiyun  * drops to zero.
223*4882a593Smuzhiyun  *
224*4882a593Smuzhiyun  * Never invoke this function directly!
225*4882a593Smuzhiyun  */
__ept_release(struct kref * kref)226*4882a593Smuzhiyun static void __ept_release(struct kref *kref)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
229*4882a593Smuzhiyun 						  refcount);
230*4882a593Smuzhiyun 	/*
231*4882a593Smuzhiyun 	 * At this point no one holds a reference to ept anymore,
232*4882a593Smuzhiyun 	 * so we can directly free it
233*4882a593Smuzhiyun 	 */
234*4882a593Smuzhiyun 	kfree(ept);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun /* for more info, see below documentation of rpmsg_create_ept() */
__rpmsg_create_ept(struct virtproc_info * vrp,struct rpmsg_device * rpdev,rpmsg_rx_cb_t cb,void * priv,u32 addr)238*4882a593Smuzhiyun static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
239*4882a593Smuzhiyun 						 struct rpmsg_device *rpdev,
240*4882a593Smuzhiyun 						 rpmsg_rx_cb_t cb,
241*4882a593Smuzhiyun 						 void *priv, u32 addr)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	int id_min, id_max, id;
244*4882a593Smuzhiyun 	struct rpmsg_endpoint *ept;
245*4882a593Smuzhiyun 	struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	ept = kzalloc(sizeof(*ept), GFP_KERNEL);
248*4882a593Smuzhiyun 	if (!ept)
249*4882a593Smuzhiyun 		return NULL;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	kref_init(&ept->refcount);
252*4882a593Smuzhiyun 	mutex_init(&ept->cb_lock);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	ept->rpdev = rpdev;
255*4882a593Smuzhiyun 	ept->cb = cb;
256*4882a593Smuzhiyun 	ept->priv = priv;
257*4882a593Smuzhiyun 	ept->ops = &virtio_endpoint_ops;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	/* do we need to allocate a local address ? */
260*4882a593Smuzhiyun 	if (addr == RPMSG_ADDR_ANY) {
261*4882a593Smuzhiyun 		id_min = RPMSG_RESERVED_ADDRESSES;
262*4882a593Smuzhiyun 		id_max = 0;
263*4882a593Smuzhiyun 	} else {
264*4882a593Smuzhiyun 		id_min = addr;
265*4882a593Smuzhiyun 		id_max = addr + 1;
266*4882a593Smuzhiyun 	}
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	mutex_lock(&vrp->endpoints_lock);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/* bind the endpoint to an rpmsg address (and allocate one if needed) */
271*4882a593Smuzhiyun 	id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
272*4882a593Smuzhiyun 	if (id < 0) {
273*4882a593Smuzhiyun 		dev_err(dev, "idr_alloc failed: %d\n", id);
274*4882a593Smuzhiyun 		goto free_ept;
275*4882a593Smuzhiyun 	}
276*4882a593Smuzhiyun 	ept->addr = id;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	mutex_unlock(&vrp->endpoints_lock);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	return ept;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun free_ept:
283*4882a593Smuzhiyun 	mutex_unlock(&vrp->endpoints_lock);
284*4882a593Smuzhiyun 	kref_put(&ept->refcount, __ept_release);
285*4882a593Smuzhiyun 	return NULL;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
virtio_rpmsg_create_ept(struct rpmsg_device * rpdev,rpmsg_rx_cb_t cb,void * priv,struct rpmsg_channel_info chinfo)288*4882a593Smuzhiyun static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev,
289*4882a593Smuzhiyun 						      rpmsg_rx_cb_t cb,
290*4882a593Smuzhiyun 						      void *priv,
291*4882a593Smuzhiyun 						      struct rpmsg_channel_info chinfo)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	return __rpmsg_create_ept(vch->vrp, rpdev, cb, priv, chinfo.src);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun /**
299*4882a593Smuzhiyun  * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
300*4882a593Smuzhiyun  * @vrp: virtproc which owns this ept
301*4882a593Smuzhiyun  * @ept: endpoing to destroy
302*4882a593Smuzhiyun  *
303*4882a593Smuzhiyun  * An internal function which destroy an ept without assuming it is
304*4882a593Smuzhiyun  * bound to an rpmsg channel. This is needed for handling the internal
305*4882a593Smuzhiyun  * name service endpoint, which isn't bound to an rpmsg channel.
306*4882a593Smuzhiyun  * See also __rpmsg_create_ept().
307*4882a593Smuzhiyun  */
308*4882a593Smuzhiyun static void
__rpmsg_destroy_ept(struct virtproc_info * vrp,struct rpmsg_endpoint * ept)309*4882a593Smuzhiyun __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	/* make sure new inbound messages can't find this ept anymore */
312*4882a593Smuzhiyun 	mutex_lock(&vrp->endpoints_lock);
313*4882a593Smuzhiyun 	idr_remove(&vrp->endpoints, ept->addr);
314*4882a593Smuzhiyun 	mutex_unlock(&vrp->endpoints_lock);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	/* make sure in-flight inbound messages won't invoke cb anymore */
317*4882a593Smuzhiyun 	mutex_lock(&ept->cb_lock);
318*4882a593Smuzhiyun 	ept->cb = NULL;
319*4882a593Smuzhiyun 	mutex_unlock(&ept->cb_lock);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	kref_put(&ept->refcount, __ept_release);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
virtio_rpmsg_destroy_ept(struct rpmsg_endpoint * ept)324*4882a593Smuzhiyun static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(ept->rpdev);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	__rpmsg_destroy_ept(vch->vrp, ept);
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
virtio_rpmsg_announce_create(struct rpmsg_device * rpdev)331*4882a593Smuzhiyun static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
334*4882a593Smuzhiyun 	struct virtproc_info *vrp = vch->vrp;
335*4882a593Smuzhiyun 	struct device *dev = &rpdev->dev;
336*4882a593Smuzhiyun 	int err = 0;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	/* need to tell remote processor's name service about this channel ? */
339*4882a593Smuzhiyun 	if (rpdev->announce && rpdev->ept &&
340*4882a593Smuzhiyun 	    virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
341*4882a593Smuzhiyun 		struct rpmsg_ns_msg nsm;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
344*4882a593Smuzhiyun 		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
345*4882a593Smuzhiyun 		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
348*4882a593Smuzhiyun 		if (err)
349*4882a593Smuzhiyun 			dev_err(dev, "failed to announce service %d\n", err);
350*4882a593Smuzhiyun 	}
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	return err;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun 
virtio_rpmsg_announce_destroy(struct rpmsg_device * rpdev)355*4882a593Smuzhiyun static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
358*4882a593Smuzhiyun 	struct virtproc_info *vrp = vch->vrp;
359*4882a593Smuzhiyun 	struct device *dev = &rpdev->dev;
360*4882a593Smuzhiyun 	int err = 0;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	/* tell remote processor's name service we're removing this channel */
363*4882a593Smuzhiyun 	if (rpdev->announce && rpdev->ept &&
364*4882a593Smuzhiyun 	    virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
365*4882a593Smuzhiyun 		struct rpmsg_ns_msg nsm;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
368*4882a593Smuzhiyun 		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
369*4882a593Smuzhiyun 		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
372*4882a593Smuzhiyun 		if (err)
373*4882a593Smuzhiyun 			dev_err(dev, "failed to announce service %d\n", err);
374*4882a593Smuzhiyun 	}
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	return err;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun static const struct rpmsg_device_ops virtio_rpmsg_ops = {
380*4882a593Smuzhiyun 	.create_ept = virtio_rpmsg_create_ept,
381*4882a593Smuzhiyun 	.announce_create = virtio_rpmsg_announce_create,
382*4882a593Smuzhiyun 	.announce_destroy = virtio_rpmsg_announce_destroy,
383*4882a593Smuzhiyun };
384*4882a593Smuzhiyun 
virtio_rpmsg_release_device(struct device * dev)385*4882a593Smuzhiyun static void virtio_rpmsg_release_device(struct device *dev)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun 	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
388*4882a593Smuzhiyun 	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	kfree(vch);
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /*
394*4882a593Smuzhiyun  * create an rpmsg channel using its name and address info.
395*4882a593Smuzhiyun  * this function will be used to create both static and dynamic
396*4882a593Smuzhiyun  * channels.
397*4882a593Smuzhiyun  */
rpmsg_create_channel(struct virtproc_info * vrp,struct rpmsg_channel_info * chinfo)398*4882a593Smuzhiyun static struct rpmsg_device *rpmsg_create_channel(struct virtproc_info *vrp,
399*4882a593Smuzhiyun 						 struct rpmsg_channel_info *chinfo)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	struct virtio_rpmsg_channel *vch;
402*4882a593Smuzhiyun 	struct rpmsg_device *rpdev;
403*4882a593Smuzhiyun 	struct device *tmp, *dev = &vrp->vdev->dev;
404*4882a593Smuzhiyun 	int ret;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	/* make sure a similar channel doesn't already exist */
407*4882a593Smuzhiyun 	tmp = rpmsg_find_device(dev, chinfo);
408*4882a593Smuzhiyun 	if (tmp) {
409*4882a593Smuzhiyun 		/* decrement the matched device's refcount back */
410*4882a593Smuzhiyun 		put_device(tmp);
411*4882a593Smuzhiyun 		dev_err(dev, "channel %s:%x:%x already exist\n",
412*4882a593Smuzhiyun 				chinfo->name, chinfo->src, chinfo->dst);
413*4882a593Smuzhiyun 		return NULL;
414*4882a593Smuzhiyun 	}
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	vch = kzalloc(sizeof(*vch), GFP_KERNEL);
417*4882a593Smuzhiyun 	if (!vch)
418*4882a593Smuzhiyun 		return NULL;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	/* Link the channel to our vrp */
421*4882a593Smuzhiyun 	vch->vrp = vrp;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	/* Assign public information to the rpmsg_device */
424*4882a593Smuzhiyun 	rpdev = &vch->rpdev;
425*4882a593Smuzhiyun 	rpdev->src = chinfo->src;
426*4882a593Smuzhiyun 	rpdev->dst = chinfo->dst;
427*4882a593Smuzhiyun 	rpdev->ops = &virtio_rpmsg_ops;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	/*
430*4882a593Smuzhiyun 	 * rpmsg server channels has predefined local address (for now),
431*4882a593Smuzhiyun 	 * and their existence needs to be announced remotely
432*4882a593Smuzhiyun 	 */
433*4882a593Smuzhiyun 	rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	rpdev->dev.parent = &vrp->vdev->dev;
438*4882a593Smuzhiyun 	rpdev->dev.release = virtio_rpmsg_release_device;
439*4882a593Smuzhiyun 	ret = rpmsg_register_device(rpdev);
440*4882a593Smuzhiyun 	if (ret)
441*4882a593Smuzhiyun 		return NULL;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	return rpdev;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun /* super simple buffer "allocator" that is just enough for now */
get_a_tx_buf(struct virtproc_info * vrp)447*4882a593Smuzhiyun static void *get_a_tx_buf(struct virtproc_info *vrp)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun 	unsigned int len;
450*4882a593Smuzhiyun 	void *ret;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	/* support multiple concurrent senders */
453*4882a593Smuzhiyun 	mutex_lock(&vrp->tx_lock);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	/*
456*4882a593Smuzhiyun 	 * either pick the next unused tx buffer
457*4882a593Smuzhiyun 	 * (half of our buffers are used for sending messages)
458*4882a593Smuzhiyun 	 */
459*4882a593Smuzhiyun 	if (vrp->last_sbuf < vrp->num_bufs / 2)
460*4882a593Smuzhiyun 		ret = vrp->sbufs + vrp->buf_size * vrp->last_sbuf++;
461*4882a593Smuzhiyun 	/* or recycle a used one */
462*4882a593Smuzhiyun 	else
463*4882a593Smuzhiyun 		ret = virtqueue_get_buf(vrp->svq, &len);
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	mutex_unlock(&vrp->tx_lock);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	return ret;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun /**
471*4882a593Smuzhiyun  * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
472*4882a593Smuzhiyun  * @vrp: virtual remote processor state
473*4882a593Smuzhiyun  *
474*4882a593Smuzhiyun  * This function is called before a sender is blocked, waiting for
475*4882a593Smuzhiyun  * a tx buffer to become available.
476*4882a593Smuzhiyun  *
477*4882a593Smuzhiyun  * If we already have blocking senders, this function merely increases
478*4882a593Smuzhiyun  * the "sleepers" reference count, and exits.
479*4882a593Smuzhiyun  *
480*4882a593Smuzhiyun  * Otherwise, if this is the first sender to block, we also enable
481*4882a593Smuzhiyun  * virtio's tx callbacks, so we'd be immediately notified when a tx
482*4882a593Smuzhiyun  * buffer is consumed (we rely on virtio's tx callback in order
483*4882a593Smuzhiyun  * to wake up sleeping senders as soon as a tx buffer is used by the
484*4882a593Smuzhiyun  * remote processor).
485*4882a593Smuzhiyun  */
rpmsg_upref_sleepers(struct virtproc_info * vrp)486*4882a593Smuzhiyun static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun 	/* support multiple concurrent senders */
489*4882a593Smuzhiyun 	mutex_lock(&vrp->tx_lock);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/* are we the first sleeping context waiting for tx buffers ? */
492*4882a593Smuzhiyun 	if (atomic_inc_return(&vrp->sleepers) == 1)
493*4882a593Smuzhiyun 		/* enable "tx-complete" interrupts before dozing off */
494*4882a593Smuzhiyun 		virtqueue_enable_cb(vrp->svq);
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	mutex_unlock(&vrp->tx_lock);
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun /**
500*4882a593Smuzhiyun  * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
501*4882a593Smuzhiyun  * @vrp: virtual remote processor state
502*4882a593Smuzhiyun  *
503*4882a593Smuzhiyun  * This function is called after a sender, that waited for a tx buffer
504*4882a593Smuzhiyun  * to become available, is unblocked.
505*4882a593Smuzhiyun  *
506*4882a593Smuzhiyun  * If we still have blocking senders, this function merely decreases
507*4882a593Smuzhiyun  * the "sleepers" reference count, and exits.
508*4882a593Smuzhiyun  *
509*4882a593Smuzhiyun  * Otherwise, if there are no more blocking senders, we also disable
510*4882a593Smuzhiyun  * virtio's tx callbacks, to avoid the overhead incurred with handling
511*4882a593Smuzhiyun  * those (now redundant) interrupts.
512*4882a593Smuzhiyun  */
rpmsg_downref_sleepers(struct virtproc_info * vrp)513*4882a593Smuzhiyun static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	/* support multiple concurrent senders */
516*4882a593Smuzhiyun 	mutex_lock(&vrp->tx_lock);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/* are we the last sleeping context waiting for tx buffers ? */
519*4882a593Smuzhiyun 	if (atomic_dec_and_test(&vrp->sleepers))
520*4882a593Smuzhiyun 		/* disable "tx-complete" interrupts */
521*4882a593Smuzhiyun 		virtqueue_disable_cb(vrp->svq);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	mutex_unlock(&vrp->tx_lock);
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun /**
527*4882a593Smuzhiyun  * rpmsg_send_offchannel_raw() - send a message across to the remote processor
528*4882a593Smuzhiyun  * @rpdev: the rpmsg channel
529*4882a593Smuzhiyun  * @src: source address
530*4882a593Smuzhiyun  * @dst: destination address
531*4882a593Smuzhiyun  * @data: payload of message
532*4882a593Smuzhiyun  * @len: length of payload
533*4882a593Smuzhiyun  * @wait: indicates whether caller should block in case no TX buffers available
534*4882a593Smuzhiyun  *
535*4882a593Smuzhiyun  * This function is the base implementation for all of the rpmsg sending API.
536*4882a593Smuzhiyun  *
537*4882a593Smuzhiyun  * It will send @data of length @len to @dst, and say it's from @src. The
538*4882a593Smuzhiyun  * message will be sent to the remote processor which the @rpdev channel
539*4882a593Smuzhiyun  * belongs to.
540*4882a593Smuzhiyun  *
541*4882a593Smuzhiyun  * The message is sent using one of the TX buffers that are available for
542*4882a593Smuzhiyun  * communication with this remote processor.
543*4882a593Smuzhiyun  *
544*4882a593Smuzhiyun  * If @wait is true, the caller will be blocked until either a TX buffer is
545*4882a593Smuzhiyun  * available, or 15 seconds elapses (we don't want callers to
546*4882a593Smuzhiyun  * sleep indefinitely due to misbehaving remote processors), and in that
547*4882a593Smuzhiyun  * case -ERESTARTSYS is returned. The number '15' itself was picked
548*4882a593Smuzhiyun  * arbitrarily; there's little point in asking drivers to provide a timeout
549*4882a593Smuzhiyun  * value themselves.
550*4882a593Smuzhiyun  *
551*4882a593Smuzhiyun  * Otherwise, if @wait is false, and there are no TX buffers available,
552*4882a593Smuzhiyun  * the function will immediately fail, and -ENOMEM will be returned.
553*4882a593Smuzhiyun  *
554*4882a593Smuzhiyun  * Normally drivers shouldn't use this function directly; instead, drivers
555*4882a593Smuzhiyun  * should use the appropriate rpmsg_{try}send{to, _offchannel} API
556*4882a593Smuzhiyun  * (see include/linux/rpmsg.h).
557*4882a593Smuzhiyun  *
558*4882a593Smuzhiyun  * Returns 0 on success and an appropriate error value on failure.
559*4882a593Smuzhiyun  */
rpmsg_send_offchannel_raw(struct rpmsg_device * rpdev,u32 src,u32 dst,void * data,int len,bool wait)560*4882a593Smuzhiyun static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
561*4882a593Smuzhiyun 				     u32 src, u32 dst,
562*4882a593Smuzhiyun 				     void *data, int len, bool wait)
563*4882a593Smuzhiyun {
564*4882a593Smuzhiyun 	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
565*4882a593Smuzhiyun 	struct virtproc_info *vrp = vch->vrp;
566*4882a593Smuzhiyun 	struct device *dev = &rpdev->dev;
567*4882a593Smuzhiyun 	struct scatterlist sg;
568*4882a593Smuzhiyun 	struct rpmsg_hdr *msg;
569*4882a593Smuzhiyun 	int err;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	/* bcasting isn't allowed */
572*4882a593Smuzhiyun 	if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
573*4882a593Smuzhiyun 		dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
574*4882a593Smuzhiyun 		return -EINVAL;
575*4882a593Smuzhiyun 	}
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	/*
578*4882a593Smuzhiyun 	 * We currently use fixed-sized buffers, and therefore the payload
579*4882a593Smuzhiyun 	 * length is limited.
580*4882a593Smuzhiyun 	 *
581*4882a593Smuzhiyun 	 * One of the possible improvements here is either to support
582*4882a593Smuzhiyun 	 * user-provided buffers (and then we can also support zero-copy
583*4882a593Smuzhiyun 	 * messaging), or to improve the buffer allocator, to support
584*4882a593Smuzhiyun 	 * variable-length buffer sizes.
585*4882a593Smuzhiyun 	 */
586*4882a593Smuzhiyun 	if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) {
587*4882a593Smuzhiyun 		dev_err(dev, "message is too big (%d)\n", len);
588*4882a593Smuzhiyun 		return -EMSGSIZE;
589*4882a593Smuzhiyun 	}
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	/* grab a buffer */
592*4882a593Smuzhiyun 	msg = get_a_tx_buf(vrp);
593*4882a593Smuzhiyun 	if (!msg && !wait)
594*4882a593Smuzhiyun 		return -ENOMEM;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	/* no free buffer ? wait for one (but bail after 15 seconds) */
597*4882a593Smuzhiyun 	while (!msg) {
598*4882a593Smuzhiyun 		/* enable "tx-complete" interrupts, if not already enabled */
599*4882a593Smuzhiyun 		rpmsg_upref_sleepers(vrp);
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 		/*
602*4882a593Smuzhiyun 		 * sleep until a free buffer is available or 15 secs elapse.
603*4882a593Smuzhiyun 		 * the timeout period is not configurable because there's
604*4882a593Smuzhiyun 		 * little point in asking drivers to specify that.
605*4882a593Smuzhiyun 		 * if later this happens to be required, it'd be easy to add.
606*4882a593Smuzhiyun 		 */
607*4882a593Smuzhiyun 		err = wait_event_interruptible_timeout(vrp->sendq,
608*4882a593Smuzhiyun 					(msg = get_a_tx_buf(vrp)),
609*4882a593Smuzhiyun 					msecs_to_jiffies(15000));
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 		/* disable "tx-complete" interrupts if we're the last sleeper */
612*4882a593Smuzhiyun 		rpmsg_downref_sleepers(vrp);
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 		/* timeout ? */
615*4882a593Smuzhiyun 		if (!err) {
616*4882a593Smuzhiyun 			dev_err(dev, "timeout waiting for a tx buffer\n");
617*4882a593Smuzhiyun 			return -ERESTARTSYS;
618*4882a593Smuzhiyun 		}
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	msg->len = cpu_to_virtio16(vrp->vdev, len);
622*4882a593Smuzhiyun 	msg->flags = 0;
623*4882a593Smuzhiyun 	msg->src = cpu_to_virtio32(vrp->vdev, src);
624*4882a593Smuzhiyun 	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
625*4882a593Smuzhiyun 	msg->reserved = 0;
626*4882a593Smuzhiyun 	memcpy(msg->data, data, len);
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
629*4882a593Smuzhiyun 		src, dst, len, msg->flags, msg->reserved);
630*4882a593Smuzhiyun #if defined(CONFIG_DYNAMIC_DEBUG)
631*4882a593Smuzhiyun 	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
632*4882a593Smuzhiyun 			 msg, sizeof(*msg) + len, true);
633*4882a593Smuzhiyun #endif
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	mutex_lock(&vrp->tx_lock);
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	/* add message to the remote processor's virtqueue */
640*4882a593Smuzhiyun 	err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
641*4882a593Smuzhiyun 	if (err) {
642*4882a593Smuzhiyun 		/*
643*4882a593Smuzhiyun 		 * need to reclaim the buffer here, otherwise it's lost
644*4882a593Smuzhiyun 		 * (memory won't leak, but rpmsg won't use it again for TX).
645*4882a593Smuzhiyun 		 * this will wait for a buffer management overhaul.
646*4882a593Smuzhiyun 		 */
647*4882a593Smuzhiyun 		dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
648*4882a593Smuzhiyun 		goto out;
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	/* tell the remote processor it has a pending message to read */
652*4882a593Smuzhiyun 	virtqueue_kick(vrp->svq);
653*4882a593Smuzhiyun out:
654*4882a593Smuzhiyun 	mutex_unlock(&vrp->tx_lock);
655*4882a593Smuzhiyun 	return err;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun 
virtio_rpmsg_send(struct rpmsg_endpoint * ept,void * data,int len)658*4882a593Smuzhiyun static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	struct rpmsg_device *rpdev = ept->rpdev;
661*4882a593Smuzhiyun 	u32 src = ept->addr, dst = rpdev->dst;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun 
virtio_rpmsg_sendto(struct rpmsg_endpoint * ept,void * data,int len,u32 dst)666*4882a593Smuzhiyun static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
667*4882a593Smuzhiyun 			       u32 dst)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun 	struct rpmsg_device *rpdev = ept->rpdev;
670*4882a593Smuzhiyun 	u32 src = ept->addr;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun 
virtio_rpmsg_send_offchannel(struct rpmsg_endpoint * ept,u32 src,u32 dst,void * data,int len)675*4882a593Smuzhiyun static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
676*4882a593Smuzhiyun 					u32 dst, void *data, int len)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun 	struct rpmsg_device *rpdev = ept->rpdev;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun 
virtio_rpmsg_trysend(struct rpmsg_endpoint * ept,void * data,int len)683*4882a593Smuzhiyun static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	struct rpmsg_device *rpdev = ept->rpdev;
686*4882a593Smuzhiyun 	u32 src = ept->addr, dst = rpdev->dst;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun 
virtio_rpmsg_trysendto(struct rpmsg_endpoint * ept,void * data,int len,u32 dst)691*4882a593Smuzhiyun static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
692*4882a593Smuzhiyun 				  int len, u32 dst)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun 	struct rpmsg_device *rpdev = ept->rpdev;
695*4882a593Smuzhiyun 	u32 src = ept->addr;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun 
virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint * ept,u32 src,u32 dst,void * data,int len)700*4882a593Smuzhiyun static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
701*4882a593Smuzhiyun 					   u32 dst, void *data, int len)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	struct rpmsg_device *rpdev = ept->rpdev;
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun 
rpmsg_recv_single(struct virtproc_info * vrp,struct device * dev,struct rpmsg_hdr * msg,unsigned int len)708*4882a593Smuzhiyun static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
709*4882a593Smuzhiyun 			     struct rpmsg_hdr *msg, unsigned int len)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun 	struct rpmsg_endpoint *ept;
712*4882a593Smuzhiyun 	struct scatterlist sg;
713*4882a593Smuzhiyun 	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
714*4882a593Smuzhiyun 	int err;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
717*4882a593Smuzhiyun 		virtio32_to_cpu(vrp->vdev, msg->src),
718*4882a593Smuzhiyun 		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
719*4882a593Smuzhiyun 		virtio16_to_cpu(vrp->vdev, msg->flags),
720*4882a593Smuzhiyun 		virtio32_to_cpu(vrp->vdev, msg->reserved));
721*4882a593Smuzhiyun #if defined(CONFIG_DYNAMIC_DEBUG)
722*4882a593Smuzhiyun 	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
723*4882a593Smuzhiyun 			 msg, sizeof(*msg) + msg_len, true);
724*4882a593Smuzhiyun #endif
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	/*
727*4882a593Smuzhiyun 	 * We currently use fixed-sized buffers, so trivially sanitize
728*4882a593Smuzhiyun 	 * the reported payload length.
729*4882a593Smuzhiyun 	 */
730*4882a593Smuzhiyun 	if (len > vrp->buf_size ||
731*4882a593Smuzhiyun 	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
732*4882a593Smuzhiyun 		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
733*4882a593Smuzhiyun 		return -EINVAL;
734*4882a593Smuzhiyun 	}
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	/* use the dst addr to fetch the callback of the appropriate user */
737*4882a593Smuzhiyun 	mutex_lock(&vrp->endpoints_lock);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	/* let's make sure no one deallocates ept while we use it */
742*4882a593Smuzhiyun 	if (ept)
743*4882a593Smuzhiyun 		kref_get(&ept->refcount);
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	mutex_unlock(&vrp->endpoints_lock);
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	if (ept) {
748*4882a593Smuzhiyun 		/* make sure ept->cb doesn't go away while we use it */
749*4882a593Smuzhiyun 		mutex_lock(&ept->cb_lock);
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 		if (ept->cb)
752*4882a593Smuzhiyun 			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
753*4882a593Smuzhiyun 				virtio32_to_cpu(vrp->vdev, msg->src));
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 		mutex_unlock(&ept->cb_lock);
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 		/* farewell, ept, we don't need you anymore */
758*4882a593Smuzhiyun 		kref_put(&ept->refcount, __ept_release);
759*4882a593Smuzhiyun 	} else
760*4882a593Smuzhiyun 		dev_warn(dev, "msg received with no recipient\n");
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	/* publish the real size of the buffer */
763*4882a593Smuzhiyun 	rpmsg_sg_init(&sg, msg, vrp->buf_size);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	/* add the buffer back to the remote processor's virtqueue */
766*4882a593Smuzhiyun 	err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
767*4882a593Smuzhiyun 	if (err < 0) {
768*4882a593Smuzhiyun 		dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
769*4882a593Smuzhiyun 		return err;
770*4882a593Smuzhiyun 	}
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	return 0;
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun /* called when an rx buffer is used, and it's time to digest a message */
rpmsg_recv_done(struct virtqueue * rvq)776*4882a593Smuzhiyun static void rpmsg_recv_done(struct virtqueue *rvq)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun 	struct virtproc_info *vrp = rvq->vdev->priv;
779*4882a593Smuzhiyun 	struct device *dev = &rvq->vdev->dev;
780*4882a593Smuzhiyun 	struct rpmsg_hdr *msg;
781*4882a593Smuzhiyun 	unsigned int len, msgs_received = 0;
782*4882a593Smuzhiyun 	int err;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	msg = virtqueue_get_buf(rvq, &len);
785*4882a593Smuzhiyun 	if (!msg) {
786*4882a593Smuzhiyun 		dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
787*4882a593Smuzhiyun 		return;
788*4882a593Smuzhiyun 	}
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	while (msg) {
791*4882a593Smuzhiyun 		err = rpmsg_recv_single(vrp, dev, msg, len);
792*4882a593Smuzhiyun 		if (err)
793*4882a593Smuzhiyun 			break;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 		msgs_received++;
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 		msg = virtqueue_get_buf(rvq, &len);
798*4882a593Smuzhiyun 	}
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	dev_dbg(dev, "Received %u messages\n", msgs_received);
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	/* tell the remote processor we added another available rx buffer */
803*4882a593Smuzhiyun 	if (msgs_received)
804*4882a593Smuzhiyun 		virtqueue_kick(vrp->rvq);
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun /*
808*4882a593Smuzhiyun  * This is invoked whenever the remote processor completed processing
809*4882a593Smuzhiyun  * a TX msg we just sent it, and the buffer is put back to the used ring.
810*4882a593Smuzhiyun  *
811*4882a593Smuzhiyun  * Normally, though, we suppress this "tx complete" interrupt in order to
812*4882a593Smuzhiyun  * avoid the incurred overhead.
813*4882a593Smuzhiyun  */
rpmsg_xmit_done(struct virtqueue * svq)814*4882a593Smuzhiyun static void rpmsg_xmit_done(struct virtqueue *svq)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun 	struct virtproc_info *vrp = svq->vdev->priv;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	dev_dbg(&svq->vdev->dev, "%s\n", __func__);
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	/* wake up potential senders that are waiting for a tx buffer */
821*4882a593Smuzhiyun 	wake_up_interruptible(&vrp->sendq);
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun /* invoked when a name service announcement arrives */
rpmsg_ns_cb(struct rpmsg_device * rpdev,void * data,int len,void * priv,u32 src)825*4882a593Smuzhiyun static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
826*4882a593Smuzhiyun 		       void *priv, u32 src)
827*4882a593Smuzhiyun {
828*4882a593Smuzhiyun 	struct rpmsg_ns_msg *msg = data;
829*4882a593Smuzhiyun 	struct rpmsg_device *newch;
830*4882a593Smuzhiyun 	struct rpmsg_channel_info chinfo;
831*4882a593Smuzhiyun 	struct virtproc_info *vrp = priv;
832*4882a593Smuzhiyun 	struct device *dev = &vrp->vdev->dev;
833*4882a593Smuzhiyun 	int ret;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun #if defined(CONFIG_DYNAMIC_DEBUG)
836*4882a593Smuzhiyun 	dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
837*4882a593Smuzhiyun 			 data, len, true);
838*4882a593Smuzhiyun #endif
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	if (len != sizeof(*msg)) {
841*4882a593Smuzhiyun 		dev_err(dev, "malformed ns msg (%d)\n", len);
842*4882a593Smuzhiyun 		return -EINVAL;
843*4882a593Smuzhiyun 	}
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	/*
846*4882a593Smuzhiyun 	 * the name service ept does _not_ belong to a real rpmsg channel,
847*4882a593Smuzhiyun 	 * and is handled by the rpmsg bus itself.
848*4882a593Smuzhiyun 	 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
849*4882a593Smuzhiyun 	 * in somehow.
850*4882a593Smuzhiyun 	 */
851*4882a593Smuzhiyun 	if (rpdev) {
852*4882a593Smuzhiyun 		dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
853*4882a593Smuzhiyun 		return -EINVAL;
854*4882a593Smuzhiyun 	}
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	/* don't trust the remote processor for null terminating the name */
857*4882a593Smuzhiyun 	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
860*4882a593Smuzhiyun 	chinfo.src = RPMSG_ADDR_ANY;
861*4882a593Smuzhiyun 	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	dev_info(dev, "%sing channel %s addr 0x%x\n",
864*4882a593Smuzhiyun 		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
865*4882a593Smuzhiyun 		 "destroy" : "creat", msg->name, chinfo.dst);
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
868*4882a593Smuzhiyun 		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
869*4882a593Smuzhiyun 		if (ret)
870*4882a593Smuzhiyun 			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
871*4882a593Smuzhiyun 	} else {
872*4882a593Smuzhiyun 		newch = rpmsg_create_channel(vrp, &chinfo);
873*4882a593Smuzhiyun 		if (!newch)
874*4882a593Smuzhiyun 			dev_err(dev, "rpmsg_create_channel failed\n");
875*4882a593Smuzhiyun 	}
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	return 0;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun 
rpmsg_probe(struct virtio_device * vdev)880*4882a593Smuzhiyun static int rpmsg_probe(struct virtio_device *vdev)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun 	vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
883*4882a593Smuzhiyun 	static const char * const names[] = { "input", "output" };
884*4882a593Smuzhiyun 	struct virtqueue *vqs[2];
885*4882a593Smuzhiyun 	struct virtproc_info *vrp;
886*4882a593Smuzhiyun 	void *bufs_va;
887*4882a593Smuzhiyun 	int err = 0, i;
888*4882a593Smuzhiyun 	size_t total_buf_space;
889*4882a593Smuzhiyun 	bool notify;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
892*4882a593Smuzhiyun 	if (!vrp)
893*4882a593Smuzhiyun 		return -ENOMEM;
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	vrp->vdev = vdev;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	idr_init(&vrp->endpoints);
898*4882a593Smuzhiyun 	mutex_init(&vrp->endpoints_lock);
899*4882a593Smuzhiyun 	mutex_init(&vrp->tx_lock);
900*4882a593Smuzhiyun 	init_waitqueue_head(&vrp->sendq);
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	/* We expect two virtqueues, rx and tx (and in this order) */
903*4882a593Smuzhiyun 	err = virtio_find_vqs(vdev, 2, vqs, vq_cbs, names, NULL);
904*4882a593Smuzhiyun 	if (err)
905*4882a593Smuzhiyun 		goto free_vrp;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	vrp->rvq = vqs[0];
908*4882a593Smuzhiyun 	vrp->svq = vqs[1];
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	/* we expect symmetric tx/rx vrings */
911*4882a593Smuzhiyun 	WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
912*4882a593Smuzhiyun 		virtqueue_get_vring_size(vrp->svq));
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	/* we need less buffers if vrings are small */
915*4882a593Smuzhiyun 	if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
916*4882a593Smuzhiyun 		vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
917*4882a593Smuzhiyun 	else
918*4882a593Smuzhiyun 		vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	vrp->buf_size = MAX_RPMSG_BUF_SIZE;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	total_buf_space = vrp->num_bufs * vrp->buf_size;
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	/* allocate coherent memory for the buffers */
925*4882a593Smuzhiyun 	bufs_va = dma_alloc_coherent(vdev->dev.parent,
926*4882a593Smuzhiyun 				     total_buf_space, &vrp->bufs_dma,
927*4882a593Smuzhiyun 				     GFP_KERNEL);
928*4882a593Smuzhiyun 	if (!bufs_va) {
929*4882a593Smuzhiyun 		err = -ENOMEM;
930*4882a593Smuzhiyun 		goto vqs_del;
931*4882a593Smuzhiyun 	}
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	dev_dbg(&vdev->dev, "buffers: va %pK, dma %pad\n",
934*4882a593Smuzhiyun 		bufs_va, &vrp->bufs_dma);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	/* half of the buffers is dedicated for RX */
937*4882a593Smuzhiyun 	vrp->rbufs = bufs_va;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	/* and half is dedicated for TX */
940*4882a593Smuzhiyun 	vrp->sbufs = bufs_va + total_buf_space / 2;
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	/* set up the receive buffers */
943*4882a593Smuzhiyun 	for (i = 0; i < vrp->num_bufs / 2; i++) {
944*4882a593Smuzhiyun 		struct scatterlist sg;
945*4882a593Smuzhiyun 		void *cpu_addr = vrp->rbufs + i * vrp->buf_size;
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 		rpmsg_sg_init(&sg, cpu_addr, vrp->buf_size);
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 		err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
950*4882a593Smuzhiyun 					  GFP_KERNEL);
951*4882a593Smuzhiyun 		WARN_ON(err); /* sanity check; this can't really happen */
952*4882a593Smuzhiyun 	}
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	/* suppress "tx-complete" interrupts */
955*4882a593Smuzhiyun 	virtqueue_disable_cb(vrp->svq);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	vdev->priv = vrp;
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	/* if supported by the remote processor, enable the name service */
960*4882a593Smuzhiyun 	if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
961*4882a593Smuzhiyun 		/* a dedicated endpoint handles the name service msgs */
962*4882a593Smuzhiyun 		vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
963*4882a593Smuzhiyun 						vrp, RPMSG_NS_ADDR);
964*4882a593Smuzhiyun 		if (!vrp->ns_ept) {
965*4882a593Smuzhiyun 			dev_err(&vdev->dev, "failed to create the ns ept\n");
966*4882a593Smuzhiyun 			err = -ENOMEM;
967*4882a593Smuzhiyun 			goto free_coherent;
968*4882a593Smuzhiyun 		}
969*4882a593Smuzhiyun 	}
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	/*
972*4882a593Smuzhiyun 	 * Prepare to kick but don't notify yet - we can't do this before
973*4882a593Smuzhiyun 	 * device is ready.
974*4882a593Smuzhiyun 	 */
975*4882a593Smuzhiyun 	notify = virtqueue_kick_prepare(vrp->rvq);
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	/* From this point on, we can notify and get callbacks. */
978*4882a593Smuzhiyun 	virtio_device_ready(vdev);
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	/* tell the remote processor it can start sending messages */
981*4882a593Smuzhiyun 	/*
982*4882a593Smuzhiyun 	 * this might be concurrent with callbacks, but we are only
983*4882a593Smuzhiyun 	 * doing notify, not a full kick here, so that's ok.
984*4882a593Smuzhiyun 	 */
985*4882a593Smuzhiyun 	if (notify)
986*4882a593Smuzhiyun 		virtqueue_notify(vrp->rvq);
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	dev_info(&vdev->dev, "rpmsg host is online\n");
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 	return 0;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun free_coherent:
993*4882a593Smuzhiyun 	dma_free_coherent(vdev->dev.parent, total_buf_space,
994*4882a593Smuzhiyun 			  bufs_va, vrp->bufs_dma);
995*4882a593Smuzhiyun vqs_del:
996*4882a593Smuzhiyun 	vdev->config->del_vqs(vrp->vdev);
997*4882a593Smuzhiyun free_vrp:
998*4882a593Smuzhiyun 	kfree(vrp);
999*4882a593Smuzhiyun 	return err;
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun 
rpmsg_remove_device(struct device * dev,void * data)1002*4882a593Smuzhiyun static int rpmsg_remove_device(struct device *dev, void *data)
1003*4882a593Smuzhiyun {
1004*4882a593Smuzhiyun 	device_unregister(dev);
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	return 0;
1007*4882a593Smuzhiyun }
1008*4882a593Smuzhiyun 
rpmsg_remove(struct virtio_device * vdev)1009*4882a593Smuzhiyun static void rpmsg_remove(struct virtio_device *vdev)
1010*4882a593Smuzhiyun {
1011*4882a593Smuzhiyun 	struct virtproc_info *vrp = vdev->priv;
1012*4882a593Smuzhiyun 	size_t total_buf_space = vrp->num_bufs * vrp->buf_size;
1013*4882a593Smuzhiyun 	int ret;
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 	vdev->config->reset(vdev);
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 	ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1018*4882a593Smuzhiyun 	if (ret)
1019*4882a593Smuzhiyun 		dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	if (vrp->ns_ept)
1022*4882a593Smuzhiyun 		__rpmsg_destroy_ept(vrp, vrp->ns_ept);
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	idr_destroy(&vrp->endpoints);
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun 	vdev->config->del_vqs(vrp->vdev);
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 	dma_free_coherent(vdev->dev.parent, total_buf_space,
1029*4882a593Smuzhiyun 			  vrp->rbufs, vrp->bufs_dma);
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	kfree(vrp);
1032*4882a593Smuzhiyun }
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun static struct virtio_device_id id_table[] = {
1035*4882a593Smuzhiyun 	{ VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1036*4882a593Smuzhiyun 	{ 0 },
1037*4882a593Smuzhiyun };
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun static unsigned int features[] = {
1040*4882a593Smuzhiyun 	VIRTIO_RPMSG_F_NS,
1041*4882a593Smuzhiyun };
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun static struct virtio_driver virtio_ipc_driver = {
1044*4882a593Smuzhiyun 	.feature_table	= features,
1045*4882a593Smuzhiyun 	.feature_table_size = ARRAY_SIZE(features),
1046*4882a593Smuzhiyun 	.driver.name	= KBUILD_MODNAME,
1047*4882a593Smuzhiyun 	.driver.owner	= THIS_MODULE,
1048*4882a593Smuzhiyun 	.id_table	= id_table,
1049*4882a593Smuzhiyun 	.probe		= rpmsg_probe,
1050*4882a593Smuzhiyun 	.remove		= rpmsg_remove,
1051*4882a593Smuzhiyun };
1052*4882a593Smuzhiyun 
rpmsg_init(void)1053*4882a593Smuzhiyun static int __init rpmsg_init(void)
1054*4882a593Smuzhiyun {
1055*4882a593Smuzhiyun 	int ret;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	ret = register_virtio_driver(&virtio_ipc_driver);
1058*4882a593Smuzhiyun 	if (ret)
1059*4882a593Smuzhiyun 		pr_err("failed to register virtio driver: %d\n", ret);
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	return ret;
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun subsys_initcall(rpmsg_init);
1064*4882a593Smuzhiyun 
rpmsg_fini(void)1065*4882a593Smuzhiyun static void __exit rpmsg_fini(void)
1066*4882a593Smuzhiyun {
1067*4882a593Smuzhiyun 	unregister_virtio_driver(&virtio_ipc_driver);
1068*4882a593Smuzhiyun }
1069*4882a593Smuzhiyun module_exit(rpmsg_fini);
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun MODULE_DEVICE_TABLE(virtio, id_table);
1072*4882a593Smuzhiyun MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1073*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1074