Blame view

drivers/rpmsg/virtio_rpmsg_bus.c 30.2 KB
c3bab8246   Suman Anna   rpmsg: virtio_rpm...
1
  // SPDX-License-Identifier: GPL-2.0
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
2
3
4
5
6
7
8
9
  /*
   * Virtio-based remote processor messaging bus
   *
   * Copyright (C) 2011 Texas Instruments, Inc.
   * Copyright (C) 2011 Google, Inc.
   *
   * Ohad Ben-Cohen <ohad@wizery.com>
   * Brian Swetland <swetland@google.com>
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
10
11
12
   */
  
  #define pr_fmt(fmt) "%s: " fmt, __func__
6de1c9332   Loic Pallardy   rpmsg: virtio: ch...
13
14
15
  #include <linux/dma-mapping.h>
  #include <linux/idr.h>
  #include <linux/jiffies.h>
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
16
17
  #include <linux/kernel.h>
  #include <linux/module.h>
6de1c9332   Loic Pallardy   rpmsg: virtio: ch...
18
19
20
  #include <linux/mutex.h>
  #include <linux/of_device.h>
  #include <linux/rpmsg.h>
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
21
  #include <linux/scatterlist.h>
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
22
  #include <linux/slab.h>
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
23
  #include <linux/sched.h>
6de1c9332   Loic Pallardy   rpmsg: virtio: ch...
24
  #include <linux/virtio.h>
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
25
  #include <linux/virtio_byteorder.h>
6de1c9332   Loic Pallardy   rpmsg: virtio: ch...
26
27
  #include <linux/virtio_ids.h>
  #include <linux/virtio_config.h>
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
28
  #include <linux/wait.h>
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
29

8b881c07c   Bjorn Andersson   rpmsg: Move helpe...
30
  #include "rpmsg_internal.h"
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
31
32
33
34
35
36
37
  /**
   * struct virtproc_info - virtual remote processor state
   * @vdev:	the virtio device
   * @rvq:	rx virtqueue
   * @svq:	tx virtqueue
   * @rbufs:	kernel address of rx buffers
   * @sbufs:	kernel address of tx buffers
b1b989144   Suman Anna   rpmsg: use less b...
38
   * @num_bufs:	total number of buffers for rx and tx
f93848f9e   Loic Pallardy   rpmsg: virtio_rpm...
39
   * @buf_size:   size of one rx or tx buffer
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
   * @last_sbuf:	index of last tx buffer used
   * @bufs_dma:	dma base addr of the buffers
   * @tx_lock:	protects svq, sbufs and sleepers, to allow concurrent senders.
   *		sending a message might require waking up a dozing remote
   *		processor, which involves sleeping, hence the mutex.
   * @endpoints:	idr of local endpoints, allows fast retrieval
   * @endpoints_lock: lock of the endpoints set
   * @sendq:	wait queue of sending contexts waiting for a tx buffers
   * @sleepers:	number of senders that are waiting for a tx buffer
   * @ns_ept:	the bus's name service endpoint
   *
   * This structure stores the rpmsg state of a given virtio remote processor
   * device (there might be several virtio proc devices for each physical
   * remote processor).
   */
  struct virtproc_info {
  	struct virtio_device *vdev;
  	struct virtqueue *rvq, *svq;
  	void *rbufs, *sbufs;
b1b989144   Suman Anna   rpmsg: use less b...
59
  	unsigned int num_bufs;
f93848f9e   Loic Pallardy   rpmsg: virtio_rpm...
60
  	unsigned int buf_size;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
61
62
63
64
65
66
67
68
69
  	int last_sbuf;
  	dma_addr_t bufs_dma;
  	struct mutex tx_lock;
  	struct idr endpoints;
  	struct mutex endpoints_lock;
  	wait_queue_head_t sendq;
  	atomic_t sleepers;
  	struct rpmsg_endpoint *ns_ept;
  };
e88dae5da   Bjorn Andersson   rpmsg: Move virti...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  /* The feature bitmap for virtio rpmsg */
  #define VIRTIO_RPMSG_F_NS	0 /* RP supports name service notifications */
  
  /**
   * struct rpmsg_hdr - common header for all rpmsg messages
   * @src: source address
   * @dst: destination address
   * @reserved: reserved for future use
   * @len: length of payload (in bytes)
   * @flags: message flags
   * @data: @len bytes of message payload data
   *
   * Every message sent(/received) on the rpmsg bus begins with this header.
   */
  struct rpmsg_hdr {
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
85
86
87
88
89
  	__virtio32 src;
  	__virtio32 dst;
  	__virtio32 reserved;
  	__virtio16 len;
  	__virtio16 flags;
4f05fc33b   Gustavo A. R. Silva   rpmsg: Replace ze...
90
  	u8 data[];
e88dae5da   Bjorn Andersson   rpmsg: Move virti...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  } __packed;
  
  /**
   * struct rpmsg_ns_msg - dynamic name service announcement message
   * @name: name of remote service that is published
   * @addr: address of remote service that is published
   * @flags: indicates whether service is created or destroyed
   *
   * This message is sent across to publish a new service, or announce
   * about its removal. When we receive these messages, an appropriate
   * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
   * or ->remove() handler of the appropriate rpmsg driver will be invoked
   * (if/as-soon-as one is registered).
   */
  struct rpmsg_ns_msg {
  	char name[RPMSG_NAME_SIZE];
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
107
108
  	__virtio32 addr;
  	__virtio32 flags;
e88dae5da   Bjorn Andersson   rpmsg: Move virti...
109
110
111
112
113
114
115
116
117
118
119
120
  } __packed;
  
  /**
   * enum rpmsg_ns_flags - dynamic name service announcement flags
   *
   * @RPMSG_NS_CREATE: a new remote service was just created
   * @RPMSG_NS_DESTROY: a known remote service was just destroyed
   */
  enum rpmsg_ns_flags {
  	RPMSG_NS_CREATE		= 0,
  	RPMSG_NS_DESTROY	= 1,
  };
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
121
  /**
6c09ea0b0   Arnaud Pouliquen   rpmsg: virtio: fi...
122
123
124
125
126
127
   * struct virtio_rpmsg_channel - rpmsg channel descriptor
   * @rpdev: the rpmsg channel device
   * @vrp: the virtio remote processor device this channel belongs to
   *
   * This structure stores the channel that links the rpmsg device to the virtio
   * remote processor device.
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
128
129
130
131
132
133
134
135
136
   */
  struct virtio_rpmsg_channel {
  	struct rpmsg_device rpdev;
  
  	struct virtproc_info *vrp;
  };
  
  #define to_virtio_rpmsg_channel(_rpdev) \
  	container_of(_rpdev, struct virtio_rpmsg_channel, rpdev)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
137
  /*
b1b989144   Suman Anna   rpmsg: use less b...
138
139
140
   * We're allocating buffers of 512 bytes each for communications. The
   * number of buffers will be computed from the number of buffers supported
   * by the vring, upto a maximum of 512 buffers (256 in each direction).
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
141
142
143
144
   *
   * Each buffer will have 16 bytes for the msg header and 496 bytes for
   * the payload.
   *
b1b989144   Suman Anna   rpmsg: use less b...
145
   * This will utilize a maximum total space of 256KB for the buffers.
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
146
147
148
149
150
151
152
153
154
   *
   * We might also want to add support for user-provided buffers in time.
   * This will allow bigger buffer size flexibility, and can also be used
   * to achieve zero-copy messaging.
   *
   * Note that these numbers are purely a decision of this driver - we
   * can change this without changing anything in the firmware of the remote
   * processor.
   */
b1b989144   Suman Anna   rpmsg: use less b...
155
  #define MAX_RPMSG_NUM_BUFS	(512)
f93848f9e   Loic Pallardy   rpmsg: virtio_rpm...
156
  #define MAX_RPMSG_BUF_SIZE	(512)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
157
158
159
160
161
162
163
164
165
166
  
  /*
   * Local addresses are dynamically allocated on-demand.
   * We do not dynamically assign addresses from the low 1024 range,
   * in order to reserve that address range for predefined services.
   */
  #define RPMSG_RESERVED_ADDRESSES	(1024)
  
  /* Address 53 is reserved for advertising remote services */
  #define RPMSG_NS_ADDR			(53)
8a228ecfe   Bjorn Andersson   rpmsg: Indirectio...
167
168
169
170
171
172
173
174
175
176
177
  static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
  static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
  static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
  			       u32 dst);
  static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
  					u32 dst, void *data, int len);
  static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
  static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
  				  int len, u32 dst);
  static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
  					   u32 dst, void *data, int len);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
178

8a228ecfe   Bjorn Andersson   rpmsg: Indirectio...
179
180
181
182
183
184
185
186
187
  static const struct rpmsg_endpoint_ops virtio_endpoint_ops = {
  	.destroy_ept = virtio_rpmsg_destroy_ept,
  	.send = virtio_rpmsg_send,
  	.sendto = virtio_rpmsg_sendto,
  	.send_offchannel = virtio_rpmsg_send_offchannel,
  	.trysend = virtio_rpmsg_trysend,
  	.trysendto = virtio_rpmsg_trysendto,
  	.trysend_offchannel = virtio_rpmsg_trysend_offchannel,
  };
5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
188
  /**
9dd87c2af   Loic Pallardy   rpmsg: virtio_rpm...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
   * rpmsg_sg_init - initialize scatterlist according to cpu address location
   * @sg: scatterlist to fill
   * @cpu_addr: virtual address of the buffer
   * @len: buffer length
   *
   * An internal function filling scatterlist according to virtual address
   * location (in vmalloc or in kernel).
   */
  static void
  rpmsg_sg_init(struct scatterlist *sg, void *cpu_addr, unsigned int len)
  {
  	if (is_vmalloc_addr(cpu_addr)) {
  		sg_init_table(sg, 1);
  		sg_set_page(sg, vmalloc_to_page(cpu_addr), len,
  			    offset_in_page(cpu_addr));
  	} else {
  		WARN_ON(!virt_addr_valid(cpu_addr));
  		sg_init_one(sg, cpu_addr, len);
  	}
  }
  
  /**
5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
   * __ept_release() - deallocate an rpmsg endpoint
   * @kref: the ept's reference count
   *
   * This function deallocates an ept, and is invoked when its @kref refcount
   * drops to zero.
   *
   * Never invoke this function directly!
   */
  static void __ept_release(struct kref *kref)
  {
  	struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
  						  refcount);
  	/*
  	 * At this point no one holds a reference to ept anymore,
  	 * so we can directly free it
  	 */
  	kfree(ept);
  }
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
229
230
  /* for more info, see below documentation of rpmsg_create_ept() */
  static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
92e1de51b   Bjorn Andersson   rpmsg: Clean up r...
231
  						 struct rpmsg_device *rpdev,
0963679c0   Anna, Suman   rpmsg: align code...
232
233
  						 rpmsg_rx_cb_t cb,
  						 void *priv, u32 addr)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
234
  {
d0ffce779   Tejun Heo   rpmsg: convert to...
235
  	int id_min, id_max, id;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
236
237
  	struct rpmsg_endpoint *ept;
  	struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
238
  	ept = kzalloc(sizeof(*ept), GFP_KERNEL);
a8bb3fd90   Anna, Suman   rpmsg: remove poi...
239
  	if (!ept)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
240
  		return NULL;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
241

5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
242
  	kref_init(&ept->refcount);
15fd943af   Ohad Ben-Cohen   rpmsg: make sure ...
243
  	mutex_init(&ept->cb_lock);
5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
244

bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
245
246
247
  	ept->rpdev = rpdev;
  	ept->cb = cb;
  	ept->priv = priv;
8a228ecfe   Bjorn Andersson   rpmsg: Indirectio...
248
  	ept->ops = &virtio_endpoint_ops;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
249
250
  
  	/* do we need to allocate a local address ? */
d0ffce779   Tejun Heo   rpmsg: convert to...
251
252
253
254
255
256
257
  	if (addr == RPMSG_ADDR_ANY) {
  		id_min = RPMSG_RESERVED_ADDRESSES;
  		id_max = 0;
  	} else {
  		id_min = addr;
  		id_max = addr + 1;
  	}
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
258
259
260
261
  
  	mutex_lock(&vrp->endpoints_lock);
  
  	/* bind the endpoint to an rpmsg address (and allocate one if needed) */
d0ffce779   Tejun Heo   rpmsg: convert to...
262
263
264
265
  	id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
  	if (id < 0) {
  		dev_err(dev, "idr_alloc failed: %d
  ", id);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
266
267
  		goto free_ept;
  	}
d0ffce779   Tejun Heo   rpmsg: convert to...
268
  	ept->addr = id;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
269
270
271
272
  
  	mutex_unlock(&vrp->endpoints_lock);
  
  	return ept;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
273
274
  free_ept:
  	mutex_unlock(&vrp->endpoints_lock);
5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
275
  	kref_put(&ept->refcount, __ept_release);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
276
277
  	return NULL;
  }
36b72c7dc   Bjorn Andersson   rpmsg: Introduce ...
278
279
280
281
282
  static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev,
  						      rpmsg_rx_cb_t cb,
  						      void *priv,
  						      struct rpmsg_channel_info chinfo)
  {
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
283
284
285
  	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  
  	return __rpmsg_create_ept(vch->vrp, rpdev, cb, priv, chinfo.src);
36b72c7dc   Bjorn Andersson   rpmsg: Introduce ...
286
  }
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
287
  /**
fa2d7795b   Ohad Ben-Cohen   rpmsg: fix name s...
288
289
   * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
   * @vrp: virtproc which owns this ept
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
290
291
   * @ept: endpoing to destroy
   *
fa2d7795b   Ohad Ben-Cohen   rpmsg: fix name s...
292
293
294
295
   * An internal function which destroy an ept without assuming it is
   * bound to an rpmsg channel. This is needed for handling the internal
   * name service endpoint, which isn't bound to an rpmsg channel.
   * See also __rpmsg_create_ept().
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
296
   */
fa2d7795b   Ohad Ben-Cohen   rpmsg: fix name s...
297
298
  static void
  __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
299
  {
15fd943af   Ohad Ben-Cohen   rpmsg: make sure ...
300
  	/* make sure new inbound messages can't find this ept anymore */
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
301
302
303
  	mutex_lock(&vrp->endpoints_lock);
  	idr_remove(&vrp->endpoints, ept->addr);
  	mutex_unlock(&vrp->endpoints_lock);
15fd943af   Ohad Ben-Cohen   rpmsg: make sure ...
304
305
306
307
  	/* make sure in-flight inbound messages won't invoke cb anymore */
  	mutex_lock(&ept->cb_lock);
  	ept->cb = NULL;
  	mutex_unlock(&ept->cb_lock);
5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
308
  	kref_put(&ept->refcount, __ept_release);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
309
  }
fa2d7795b   Ohad Ben-Cohen   rpmsg: fix name s...
310

8a228ecfe   Bjorn Andersson   rpmsg: Indirectio...
311
312
  static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
  {
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
313
314
315
  	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(ept->rpdev);
  
  	__rpmsg_destroy_ept(vch->vrp, ept);
8a228ecfe   Bjorn Andersson   rpmsg: Indirectio...
316
  }
36b72c7dc   Bjorn Andersson   rpmsg: Introduce ...
317
318
  static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
  {
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
319
320
  	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  	struct virtproc_info *vrp = vch->vrp;
36b72c7dc   Bjorn Andersson   rpmsg: Introduce ...
321
322
  	struct device *dev = &rpdev->dev;
  	int err = 0;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
323
  	/* need to tell remote processor's name service about this channel ? */
b2599ebff   Henri Roosen   rpmsg: virtio_rpm...
324
  	if (rpdev->announce && rpdev->ept &&
0963679c0   Anna, Suman   rpmsg: align code...
325
  	    virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
326
327
328
  		struct rpmsg_ns_msg nsm;
  
  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
329
330
  		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
  		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
331

2a48d7322   Bjorn Andersson   rpmsg: rpmsg_send...
332
  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
333
334
335
336
  		if (err)
  			dev_err(dev, "failed to announce service %d
  ", err);
  	}
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
337
338
  	return err;
  }
36b72c7dc   Bjorn Andersson   rpmsg: Introduce ...
339
  static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
340
  {
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
341
342
  	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  	struct virtproc_info *vrp = vch->vrp;
36b72c7dc   Bjorn Andersson   rpmsg: Introduce ...
343
  	struct device *dev = &rpdev->dev;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
344
345
346
  	int err = 0;
  
  	/* tell remote processor's name service we're removing this channel */
b2599ebff   Henri Roosen   rpmsg: virtio_rpm...
347
  	if (rpdev->announce && rpdev->ept &&
0963679c0   Anna, Suman   rpmsg: align code...
348
  	    virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
349
350
351
  		struct rpmsg_ns_msg nsm;
  
  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
352
353
  		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
  		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
354

2a48d7322   Bjorn Andersson   rpmsg: rpmsg_send...
355
  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
356
357
358
359
  		if (err)
  			dev_err(dev, "failed to announce service %d
  ", err);
  	}
36b72c7dc   Bjorn Andersson   rpmsg: Introduce ...
360
361
  	return err;
  }
36b72c7dc   Bjorn Andersson   rpmsg: Introduce ...
362
363
364
365
366
  static const struct rpmsg_device_ops virtio_rpmsg_ops = {
  	.create_ept = virtio_rpmsg_create_ept,
  	.announce_create = virtio_rpmsg_announce_create,
  	.announce_destroy = virtio_rpmsg_announce_destroy,
  };
b0b03b811   Bjorn Andersson   rpmsg: Release rp...
367
368
369
370
371
372
373
  static void virtio_rpmsg_release_device(struct device *dev)
  {
  	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  
  	kfree(vch);
  }
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
374
375
376
377
378
  /*
   * create an rpmsg channel using its name and address info.
   * this function will be used to create both static and dynamic
   * channels.
   */
92e1de51b   Bjorn Andersson   rpmsg: Clean up r...
379
380
  static struct rpmsg_device *rpmsg_create_channel(struct virtproc_info *vrp,
  						 struct rpmsg_channel_info *chinfo)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
381
  {
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
382
  	struct virtio_rpmsg_channel *vch;
92e1de51b   Bjorn Andersson   rpmsg: Clean up r...
383
  	struct rpmsg_device *rpdev;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
384
385
386
387
  	struct device *tmp, *dev = &vrp->vdev->dev;
  	int ret;
  
  	/* make sure a similar channel doesn't already exist */
8b881c07c   Bjorn Andersson   rpmsg: Move helpe...
388
  	tmp = rpmsg_find_device(dev, chinfo);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
389
390
391
392
393
394
395
396
  	if (tmp) {
  		/* decrement the matched device's refcount back */
  		put_device(tmp);
  		dev_err(dev, "channel %s:%x:%x already exist
  ",
  				chinfo->name, chinfo->src, chinfo->dst);
  		return NULL;
  	}
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
397
398
  	vch = kzalloc(sizeof(*vch), GFP_KERNEL);
  	if (!vch)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
399
  		return NULL;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
400

3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
401
402
  	/* Link the channel to our vrp */
  	vch->vrp = vrp;
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
403
404
  	/* Assign public information to the rpmsg_device */
  	rpdev = &vch->rpdev;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
405
406
  	rpdev->src = chinfo->src;
  	rpdev->dst = chinfo->dst;
36b72c7dc   Bjorn Andersson   rpmsg: Introduce ...
407
  	rpdev->ops = &virtio_rpmsg_ops;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
408
409
410
411
412
  
  	/*
  	 * rpmsg server channels has predefined local address (for now),
  	 * and their existence needs to be announced remotely
  	 */
c8ced113c   Andrew F. Davis   rpmsg: remove unn...
413
  	rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
414
415
  
  	strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
6eed598a0   Bjorn Andersson   rpmsg: Split off ...
416
  	rpdev->dev.parent = &vrp->vdev->dev;
b0b03b811   Bjorn Andersson   rpmsg: Release rp...
417
  	rpdev->dev.release = virtio_rpmsg_release_device;
6eed598a0   Bjorn Andersson   rpmsg: Split off ...
418
419
420
421
422
423
  	ret = rpmsg_register_device(rpdev);
  	if (ret)
  		return NULL;
  
  	return rpdev;
  }
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
424
425
426
427
428
429
430
431
432
433
434
435
436
  /* super simple buffer "allocator" that is just enough for now */
  static void *get_a_tx_buf(struct virtproc_info *vrp)
  {
  	unsigned int len;
  	void *ret;
  
  	/* support multiple concurrent senders */
  	mutex_lock(&vrp->tx_lock);
  
  	/*
  	 * either pick the next unused tx buffer
  	 * (half of our buffers are used for sending messages)
  	 */
b1b989144   Suman Anna   rpmsg: use less b...
437
  	if (vrp->last_sbuf < vrp->num_bufs / 2)
f93848f9e   Loic Pallardy   rpmsg: virtio_rpm...
438
  		ret = vrp->sbufs + vrp->buf_size * vrp->last_sbuf++;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
  	/* or recycle a used one */
  	else
  		ret = virtqueue_get_buf(vrp->svq, &len);
  
  	mutex_unlock(&vrp->tx_lock);
  
  	return ret;
  }
  
  /**
   * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
   * @vrp: virtual remote processor state
   *
   * This function is called before a sender is blocked, waiting for
   * a tx buffer to become available.
   *
   * If we already have blocking senders, this function merely increases
   * the "sleepers" reference count, and exits.
   *
   * Otherwise, if this is the first sender to block, we also enable
   * virtio's tx callbacks, so we'd be immediately notified when a tx
   * buffer is consumed (we rely on virtio's tx callback in order
   * to wake up sleeping senders as soon as a tx buffer is used by the
   * remote processor).
   */
  static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
  {
  	/* support multiple concurrent senders */
  	mutex_lock(&vrp->tx_lock);
  
  	/* are we the first sleeping context waiting for tx buffers ? */
  	if (atomic_inc_return(&vrp->sleepers) == 1)
  		/* enable "tx-complete" interrupts before dozing off */
  		virtqueue_enable_cb(vrp->svq);
  
  	mutex_unlock(&vrp->tx_lock);
  }
  
  /**
   * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
   * @vrp: virtual remote processor state
   *
   * This function is called after a sender, that waited for a tx buffer
   * to become available, is unblocked.
   *
   * If we still have blocking senders, this function merely decreases
   * the "sleepers" reference count, and exits.
   *
   * Otherwise, if there are no more blocking senders, we also disable
   * virtio's tx callbacks, to avoid the overhead incurred with handling
   * those (now redundant) interrupts.
   */
  static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
  {
  	/* support multiple concurrent senders */
  	mutex_lock(&vrp->tx_lock);
  
  	/* are we the last sleeping context waiting for tx buffers ? */
  	if (atomic_dec_and_test(&vrp->sleepers))
  		/* disable "tx-complete" interrupts */
  		virtqueue_disable_cb(vrp->svq);
  
  	mutex_unlock(&vrp->tx_lock);
  }
  
  /**
   * rpmsg_send_offchannel_raw() - send a message across to the remote processor
   * @rpdev: the rpmsg channel
   * @src: source address
   * @dst: destination address
   * @data: payload of message
   * @len: length of payload
   * @wait: indicates whether caller should block in case no TX buffers available
   *
   * This function is the base implementation for all of the rpmsg sending API.
   *
   * It will send @data of length @len to @dst, and say it's from @src. The
   * message will be sent to the remote processor which the @rpdev channel
   * belongs to.
   *
   * The message is sent using one of the TX buffers that are available for
   * communication with this remote processor.
   *
   * If @wait is true, the caller will be blocked until either a TX buffer is
   * available, or 15 seconds elapses (we don't want callers to
   * sleep indefinitely due to misbehaving remote processors), and in that
   * case -ERESTARTSYS is returned. The number '15' itself was picked
   * arbitrarily; there's little point in asking drivers to provide a timeout
   * value themselves.
   *
   * Otherwise, if @wait is false, and there are no TX buffers available,
   * the function will immediately fail, and -ENOMEM will be returned.
   *
   * Normally drivers shouldn't use this function directly; instead, drivers
   * should use the appropriate rpmsg_{try}send{to, _offchannel} API
   * (see include/linux/rpmsg.h).
   *
   * Returns 0 on success and an appropriate error value on failure.
   */
8a228ecfe   Bjorn Andersson   rpmsg: Indirectio...
538
539
540
  static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
  				     u32 src, u32 dst,
  				     void *data, int len, bool wait)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
541
  {
3bf950ff2   Bjorn Andersson   rpmsg: virtio: Hi...
542
543
  	struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  	struct virtproc_info *vrp = vch->vrp;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  	struct device *dev = &rpdev->dev;
  	struct scatterlist sg;
  	struct rpmsg_hdr *msg;
  	int err;
  
  	/* bcasting isn't allowed */
  	if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
  		dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)
  ", src, dst);
  		return -EINVAL;
  	}
  
  	/*
  	 * We currently use fixed-sized buffers, and therefore the payload
  	 * length is limited.
  	 *
  	 * One of the possible improvements here is either to support
  	 * user-provided buffers (and then we can also support zero-copy
  	 * messaging), or to improve the buffer allocator, to support
  	 * variable-length buffer sizes.
  	 */
f93848f9e   Loic Pallardy   rpmsg: virtio_rpm...
565
  	if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) {
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
  		dev_err(dev, "message is too big (%d)
  ", len);
  		return -EMSGSIZE;
  	}
  
  	/* grab a buffer */
  	msg = get_a_tx_buf(vrp);
  	if (!msg && !wait)
  		return -ENOMEM;
  
  	/* no free buffer ? wait for one (but bail after 15 seconds) */
  	while (!msg) {
  		/* enable "tx-complete" interrupts, if not already enabled */
  		rpmsg_upref_sleepers(vrp);
  
  		/*
  		 * sleep until a free buffer is available or 15 secs elapse.
  		 * the timeout period is not configurable because there's
  		 * little point in asking drivers to specify that.
  		 * if later this happens to be required, it'd be easy to add.
  		 */
  		err = wait_event_interruptible_timeout(vrp->sendq,
  					(msg = get_a_tx_buf(vrp)),
  					msecs_to_jiffies(15000));
  
  		/* disable "tx-complete" interrupts if we're the last sleeper */
  		rpmsg_downref_sleepers(vrp);
  
  		/* timeout ? */
  		if (!err) {
  			dev_err(dev, "timeout waiting for a tx buffer
  ");
  			return -ERESTARTSYS;
  		}
  	}
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
601
  	msg->len = cpu_to_virtio16(vrp->vdev, len);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
602
  	msg->flags = 0;
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
603
604
  	msg->src = cpu_to_virtio32(vrp->vdev, src);
  	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
605
606
607
608
609
  	msg->reserved = 0;
  	memcpy(msg->data, data, len);
  
  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d
  ",
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
610
  		src, dst, len, msg->flags, msg->reserved);
211e3a93e   Anna, Suman   rpmsg: use dynami...
611
612
  #if defined(CONFIG_DYNAMIC_DEBUG)
  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
613
  			 msg, sizeof(*msg) + len, true);
211e3a93e   Anna, Suman   rpmsg: use dynami...
614
  #endif
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
615

9dd87c2af   Loic Pallardy   rpmsg: virtio_rpm...
616
  	rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
617
618
619
620
  
  	mutex_lock(&vrp->tx_lock);
  
  	/* add message to the remote processor's virtqueue */
cee51d69a   Rusty Russell   virtio_rpmsg_bus:...
621
  	err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
57e1a3734   Rusty Russell   virtio: rpmsg: ma...
622
  	if (err) {
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
623
624
625
626
627
  		/*
  		 * need to reclaim the buffer here, otherwise it's lost
  		 * (memory won't leak, but rpmsg won't use it again for TX).
  		 * this will wait for a buffer management overhaul.
  		 */
cee51d69a   Rusty Russell   virtio_rpmsg_bus:...
628
629
  		dev_err(dev, "virtqueue_add_outbuf failed: %d
  ", err);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
630
631
632
633
634
  		goto out;
  	}
  
  	/* tell the remote processor it has a pending message to read */
  	virtqueue_kick(vrp->svq);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
635
636
637
638
  out:
  	mutex_unlock(&vrp->tx_lock);
  	return err;
  }
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
639

8a228ecfe   Bjorn Andersson   rpmsg: Indirectio...
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
  static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
  {
  	struct rpmsg_device *rpdev = ept->rpdev;
  	u32 src = ept->addr, dst = rpdev->dst;
  
  	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  }
  
  static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
  			       u32 dst)
  {
  	struct rpmsg_device *rpdev = ept->rpdev;
  	u32 src = ept->addr;
  
  	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  }
  
  static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
  					u32 dst, void *data, int len)
  {
  	struct rpmsg_device *rpdev = ept->rpdev;
  
  	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  }
  
  static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  {
  	struct rpmsg_device *rpdev = ept->rpdev;
  	u32 src = ept->addr, dst = rpdev->dst;
  
  	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  }
  
  static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
  				  int len, u32 dst)
  {
  	struct rpmsg_device *rpdev = ept->rpdev;
  	u32 src = ept->addr;
  
  	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  }
  
  static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
  					   u32 dst, void *data, int len)
  {
  	struct rpmsg_device *rpdev = ept->rpdev;
  
  	return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  }
1aa7d6a5d   Robert Tivy   rpmsg: process _a...
689
690
  static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
  			     struct rpmsg_hdr *msg, unsigned int len)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
691
  {
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
692
693
  	struct rpmsg_endpoint *ept;
  	struct scatterlist sg;
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
694
  	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
695
  	int err;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
696
697
  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d
  ",
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
698
699
700
701
  		virtio32_to_cpu(vrp->vdev, msg->src),
  		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
  		virtio16_to_cpu(vrp->vdev, msg->flags),
  		virtio32_to_cpu(vrp->vdev, msg->reserved));
211e3a93e   Anna, Suman   rpmsg: use dynami...
702
703
  #if defined(CONFIG_DYNAMIC_DEBUG)
  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
704
  			 msg, sizeof(*msg) + msg_len, true);
211e3a93e   Anna, Suman   rpmsg: use dynami...
705
  #endif
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
706

9648224e5   Ohad Ben-Cohen   rpmsg: validate i...
707
708
709
710
  	/*
  	 * We currently use fixed-sized buffers, so trivially sanitize
  	 * the reported payload length.
  	 */
f93848f9e   Loic Pallardy   rpmsg: virtio_rpm...
711
  	if (len > vrp->buf_size ||
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
712
713
714
  	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
  		dev_warn(dev, "inbound msg too big: (%d, %d)
  ", len, msg_len);
1aa7d6a5d   Robert Tivy   rpmsg: process _a...
715
  		return -EINVAL;
9648224e5   Ohad Ben-Cohen   rpmsg: validate i...
716
  	}
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
717
718
  	/* use the dst addr to fetch the callback of the appropriate user */
  	mutex_lock(&vrp->endpoints_lock);
5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
719

111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
720
  	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
721
722
723
724
  
  	/* let's make sure no one deallocates ept while we use it */
  	if (ept)
  		kref_get(&ept->refcount);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
725
  	mutex_unlock(&vrp->endpoints_lock);
15fd943af   Ohad Ben-Cohen   rpmsg: make sure ...
726
727
728
  	if (ept) {
  		/* make sure ept->cb doesn't go away while we use it */
  		mutex_lock(&ept->cb_lock);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
729

15fd943af   Ohad Ben-Cohen   rpmsg: make sure ...
730
  		if (ept->cb)
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
731
732
  			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
  				virtio32_to_cpu(vrp->vdev, msg->src));
15fd943af   Ohad Ben-Cohen   rpmsg: make sure ...
733
734
735
736
  
  		mutex_unlock(&ept->cb_lock);
  
  		/* farewell, ept, we don't need you anymore */
5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
737
  		kref_put(&ept->refcount, __ept_release);
15fd943af   Ohad Ben-Cohen   rpmsg: make sure ...
738
  	} else
8a168ca70   Masanari Iida   treewide: Fix typ...
739
740
  		dev_warn(dev, "msg received with no recipient
  ");
5a081caa0   Ohad Ben-Cohen   rpmsg: avoid prem...
741

f1d9e9c76   Ohad Ben-Cohen   rpmsg: fix publis...
742
  	/* publish the real size of the buffer */
9dd87c2af   Loic Pallardy   rpmsg: virtio_rpm...
743
  	rpmsg_sg_init(&sg, msg, vrp->buf_size);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
744
745
  
  	/* add the buffer back to the remote processor's virtqueue */
cee51d69a   Rusty Russell   virtio_rpmsg_bus:...
746
  	err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
747
748
749
  	if (err < 0) {
  		dev_err(dev, "failed to add a virtqueue buffer: %d
  ", err);
1aa7d6a5d   Robert Tivy   rpmsg: process _a...
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
  		return err;
  	}
  
  	return 0;
  }
  
  /* called when an rx buffer is used, and it's time to digest a message */
  static void rpmsg_recv_done(struct virtqueue *rvq)
  {
  	struct virtproc_info *vrp = rvq->vdev->priv;
  	struct device *dev = &rvq->vdev->dev;
  	struct rpmsg_hdr *msg;
  	unsigned int len, msgs_received = 0;
  	int err;
  
  	msg = virtqueue_get_buf(rvq, &len);
  	if (!msg) {
  		dev_err(dev, "uhm, incoming signal, but no used buffer ?
  ");
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
769
770
  		return;
  	}
1aa7d6a5d   Robert Tivy   rpmsg: process _a...
771
772
773
774
775
776
777
778
  	while (msg) {
  		err = rpmsg_recv_single(vrp, dev, msg, len);
  		if (err)
  			break;
  
  		msgs_received++;
  
  		msg = virtqueue_get_buf(rvq, &len);
6c49fbe36   Lee Jones   rpmsg: virtio_rpm...
779
  	}
1aa7d6a5d   Robert Tivy   rpmsg: process _a...
780
781
782
  
  	dev_dbg(dev, "Received %u messages
  ", msgs_received);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
783
  	/* tell the remote processor we added another available rx buffer */
1aa7d6a5d   Robert Tivy   rpmsg: process _a...
784
785
  	if (msgs_received)
  		virtqueue_kick(vrp->rvq);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
  }
  
  /*
   * This is invoked whenever the remote processor completed processing
   * a TX msg we just sent it, and the buffer is put back to the used ring.
   *
   * Normally, though, we suppress this "tx complete" interrupt in order to
   * avoid the incurred overhead.
   */
  static void rpmsg_xmit_done(struct virtqueue *svq)
  {
  	struct virtproc_info *vrp = svq->vdev->priv;
  
  	dev_dbg(&svq->vdev->dev, "%s
  ", __func__);
  
  	/* wake up potential senders that are waiting for a tx buffer */
  	wake_up_interruptible(&vrp->sendq);
  }
  
  /* invoked when a name service announcement arrives */
4b83c52a2   Bjorn Andersson   rpmsg: Allow call...
807
808
  static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
  		       void *priv, u32 src)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
809
810
  {
  	struct rpmsg_ns_msg *msg = data;
92e1de51b   Bjorn Andersson   rpmsg: Clean up r...
811
  	struct rpmsg_device *newch;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
812
813
814
815
  	struct rpmsg_channel_info chinfo;
  	struct virtproc_info *vrp = priv;
  	struct device *dev = &vrp->vdev->dev;
  	int ret;
211e3a93e   Anna, Suman   rpmsg: use dynami...
816
817
818
819
  #if defined(CONFIG_DYNAMIC_DEBUG)
  	dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
  			 data, len, true);
  #endif
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
820
821
822
823
  
  	if (len != sizeof(*msg)) {
  		dev_err(dev, "malformed ns msg (%d)
  ", len);
4b83c52a2   Bjorn Andersson   rpmsg: Allow call...
824
  		return -EINVAL;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
825
826
827
828
829
830
831
832
833
834
835
  	}
  
  	/*
  	 * the name service ept does _not_ belong to a real rpmsg channel,
  	 * and is handled by the rpmsg bus itself.
  	 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
  	 * in somehow.
  	 */
  	if (rpdev) {
  		dev_err(dev, "anomaly: ns ept has an rpdev handle
  ");
4b83c52a2   Bjorn Andersson   rpmsg: Allow call...
836
  		return -EINVAL;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
837
838
839
840
  	}
  
  	/* don't trust the remote processor for null terminating the name */
  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
841
842
  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
  	chinfo.src = RPMSG_ADDR_ANY;
111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
843
844
845
846
847
848
  	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
  
  	dev_info(dev, "%sing channel %s addr 0x%x
  ",
  		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
  		 "destroy" : "creat", msg->name, chinfo.dst);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
849

111d10897   Guennadi Liakhovetski   rpmsg: virtio: ad...
850
  	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
5e619b486   Bjorn Andersson   rpmsg: Split rpms...
851
  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
852
853
854
855
856
857
858
859
860
  		if (ret)
  			dev_err(dev, "rpmsg_destroy_channel failed: %d
  ", ret);
  	} else {
  		newch = rpmsg_create_channel(vrp, &chinfo);
  		if (!newch)
  			dev_err(dev, "rpmsg_create_channel failed
  ");
  	}
4b83c52a2   Bjorn Andersson   rpmsg: Allow call...
861
862
  
  	return 0;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
863
864
865
866
867
  }
  
  static int rpmsg_probe(struct virtio_device *vdev)
  {
  	vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
f7ad26ff9   Stefan Hajnoczi   virtio: make find...
868
  	static const char * const names[] = { "input", "output" };
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
869
870
871
872
  	struct virtqueue *vqs[2];
  	struct virtproc_info *vrp;
  	void *bufs_va;
  	int err = 0, i;
b1b989144   Suman Anna   rpmsg: use less b...
873
  	size_t total_buf_space;
71e4b8bf0   Michael S. Tsirkin   virtio_rpmsg: set...
874
  	bool notify;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
875
876
877
878
879
880
881
882
883
884
885
886
887
  
  	vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
  	if (!vrp)
  		return -ENOMEM;
  
  	vrp->vdev = vdev;
  
  	idr_init(&vrp->endpoints);
  	mutex_init(&vrp->endpoints_lock);
  	mutex_init(&vrp->tx_lock);
  	init_waitqueue_head(&vrp->sendq);
  
  	/* We expect two virtqueues, rx and tx (and in this order) */
9b2bbdb22   Michael S. Tsirkin   virtio: wrap find...
888
  	err = virtio_find_vqs(vdev, 2, vqs, vq_cbs, names, NULL);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
889
890
891
892
893
  	if (err)
  		goto free_vrp;
  
  	vrp->rvq = vqs[0];
  	vrp->svq = vqs[1];
b1b989144   Suman Anna   rpmsg: use less b...
894
895
896
897
898
899
900
901
902
  	/* we expect symmetric tx/rx vrings */
  	WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
  		virtqueue_get_vring_size(vrp->svq));
  
  	/* we need less buffers if vrings are small */
  	if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
  		vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
  	else
  		vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
f93848f9e   Loic Pallardy   rpmsg: virtio_rpm...
903
904
905
  	vrp->buf_size = MAX_RPMSG_BUF_SIZE;
  
  	total_buf_space = vrp->num_bufs * vrp->buf_size;
b1b989144   Suman Anna   rpmsg: use less b...
906

bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
907
  	/* allocate coherent memory for the buffers */
d999b622f   Loic Pallardy   rpmsg: virtio: al...
908
  	bufs_va = dma_alloc_coherent(vdev->dev.parent,
b1b989144   Suman Anna   rpmsg: use less b...
909
910
  				     total_buf_space, &vrp->bufs_dma,
  				     GFP_KERNEL);
3119b487e   Wei Yongjun   drivers/rpmsg/vir...
911
912
  	if (!bufs_va) {
  		err = -ENOMEM;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
913
  		goto vqs_del;
3119b487e   Wei Yongjun   drivers/rpmsg/vir...
914
  	}
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
915

de4064af7   Suman Anna   rpmsg: virtio_rpm...
916
917
  	dev_dbg(&vdev->dev, "buffers: va %pK, dma %pad
  ",
8d95b322b   Anna, Suman   rpmsg: use proper...
918
  		bufs_va, &vrp->bufs_dma);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
919
920
921
922
923
  
  	/* half of the buffers is dedicated for RX */
  	vrp->rbufs = bufs_va;
  
  	/* and half is dedicated for TX */
b1b989144   Suman Anna   rpmsg: use less b...
924
  	vrp->sbufs = bufs_va + total_buf_space / 2;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
925
926
  
  	/* set up the receive buffers */
b1b989144   Suman Anna   rpmsg: use less b...
927
  	for (i = 0; i < vrp->num_bufs / 2; i++) {
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
928
  		struct scatterlist sg;
f93848f9e   Loic Pallardy   rpmsg: virtio_rpm...
929
  		void *cpu_addr = vrp->rbufs + i * vrp->buf_size;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
930

9dd87c2af   Loic Pallardy   rpmsg: virtio_rpm...
931
  		rpmsg_sg_init(&sg, cpu_addr, vrp->buf_size);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
932

cee51d69a   Rusty Russell   virtio_rpmsg_bus:...
933
  		err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
0963679c0   Anna, Suman   rpmsg: align code...
934
  					  GFP_KERNEL);
57e1a3734   Rusty Russell   virtio: rpmsg: ma...
935
  		WARN_ON(err); /* sanity check; this can't really happen */
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
  	}
  
  	/* suppress "tx-complete" interrupts */
  	virtqueue_disable_cb(vrp->svq);
  
  	vdev->priv = vrp;
  
  	/* if supported by the remote processor, enable the name service */
  	if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
  		/* a dedicated endpoint handles the name service msgs */
  		vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
  						vrp, RPMSG_NS_ADDR);
  		if (!vrp->ns_ept) {
  			dev_err(&vdev->dev, "failed to create the ns ept
  ");
  			err = -ENOMEM;
  			goto free_coherent;
  		}
  	}
71e4b8bf0   Michael S. Tsirkin   virtio_rpmsg: set...
955
956
957
958
959
960
961
962
  	/*
  	 * Prepare to kick but don't notify yet - we can't do this before
  	 * device is ready.
  	 */
  	notify = virtqueue_kick_prepare(vrp->rvq);
  
  	/* From this point on, we can notify and get callbacks. */
  	virtio_device_ready(vdev);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
963
  	/* tell the remote processor it can start sending messages */
71e4b8bf0   Michael S. Tsirkin   virtio_rpmsg: set...
964
965
966
967
968
969
  	/*
  	 * this might be concurrent with callbacks, but we are only
  	 * doing notify, not a full kick here, so that's ok.
  	 */
  	if (notify)
  		virtqueue_notify(vrp->rvq);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
970
971
972
973
974
975
976
  
  	dev_info(&vdev->dev, "rpmsg host is online
  ");
  
  	return 0;
  
  free_coherent:
d999b622f   Loic Pallardy   rpmsg: virtio: al...
977
  	dma_free_coherent(vdev->dev.parent, total_buf_space,
b1b989144   Suman Anna   rpmsg: use less b...
978
  			  bufs_va, vrp->bufs_dma);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
979
980
981
982
983
984
985
986
987
988
989
990
991
  vqs_del:
  	vdev->config->del_vqs(vrp->vdev);
  free_vrp:
  	kfree(vrp);
  	return err;
  }
  
  static int rpmsg_remove_device(struct device *dev, void *data)
  {
  	device_unregister(dev);
  
  	return 0;
  }
0fe763c57   Greg Kroah-Hartman   Drivers: misc: re...
992
  static void rpmsg_remove(struct virtio_device *vdev)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
993
994
  {
  	struct virtproc_info *vrp = vdev->priv;
f93848f9e   Loic Pallardy   rpmsg: virtio_rpm...
995
  	size_t total_buf_space = vrp->num_bufs * vrp->buf_size;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
996
997
998
999
1000
1001
1002
1003
  	int ret;
  
  	vdev->config->reset(vdev);
  
  	ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
  	if (ret)
  		dev_warn(&vdev->dev, "can't remove rpmsg device: %d
  ", ret);
fa2d7795b   Ohad Ben-Cohen   rpmsg: fix name s...
1004
1005
  	if (vrp->ns_ept)
  		__rpmsg_destroy_ept(vrp, vrp->ns_ept);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
1006
1007
1008
  	idr_destroy(&vrp->endpoints);
  
  	vdev->config->del_vqs(vrp->vdev);
d999b622f   Loic Pallardy   rpmsg: virtio: al...
1009
  	dma_free_coherent(vdev->dev.parent, total_buf_space,
b1b989144   Suman Anna   rpmsg: use less b...
1010
  			  vrp->rbufs, vrp->bufs_dma);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
  
  	kfree(vrp);
  }
  
  static struct virtio_device_id id_table[] = {
  	{ VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
  	{ 0 },
  };
  
  static unsigned int features[] = {
  	VIRTIO_RPMSG_F_NS,
  };
  
  static struct virtio_driver virtio_ipc_driver = {
  	.feature_table	= features,
  	.feature_table_size = ARRAY_SIZE(features),
  	.driver.name	= KBUILD_MODNAME,
  	.driver.owner	= THIS_MODULE,
  	.id_table	= id_table,
  	.probe		= rpmsg_probe,
0fe763c57   Greg Kroah-Hartman   Drivers: misc: re...
1031
  	.remove		= rpmsg_remove,
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
1032
1033
1034
1035
1036
  };
  
  static int __init rpmsg_init(void)
  {
  	int ret;
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
1037
  	ret = register_virtio_driver(&virtio_ipc_driver);
5e619b486   Bjorn Andersson   rpmsg: Split rpms...
1038
  	if (ret)
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
1039
1040
  		pr_err("failed to register virtio driver: %d
  ", ret);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
1041
1042
1043
  
  	return ret;
  }
963425261   Federico Fuga   rpmsg: fix depend...
1044
  subsys_initcall(rpmsg_init);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
1045
1046
1047
1048
  
  static void __exit rpmsg_fini(void)
  {
  	unregister_virtio_driver(&virtio_ipc_driver);
bcabbccab   Ohad Ben-Cohen   rpmsg: add virtio...
1049
1050
1051
1052
1053
1054
  }
  module_exit(rpmsg_fini);
  
  MODULE_DEVICE_TABLE(virtio, id_table);
  MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
  MODULE_LICENSE("GPL v2");