]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/virtio/virtio_ring.c
virtio: harsher barriers for rpmsg.
[linux-beck.git] / drivers / virtio / virtio_ring.c
1 /* Virtio ring implementation.
2  *
3  *  Copyright 2007 Rusty Russell IBM Corporation
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 #include <linux/virtio.h>
20 #include <linux/virtio_ring.h>
21 #include <linux/virtio_config.h>
22 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25
26 /* virtio guest is communicating with a virtual "device" that actually runs on
27  * a host processor.  Memory barriers are used to control SMP effects. */
28 #ifdef CONFIG_SMP
29 /* Where possible, use SMP barriers which are more lightweight than mandatory
30  * barriers, because mandatory barriers control MMIO effects on accesses
31  * through relaxed memory I/O windows (which virtio-pci does not use). */
32 #define virtio_mb(vq) \
33         do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0)
34 #define virtio_rmb(vq) \
35         do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
36 #define virtio_wmb(vq) \
37         do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
38 #else
39 /* We must force memory ordering even if guest is UP since host could be
40  * running on another CPU, but SMP barriers are defined to barrier() in that
41  * configuration. So fall back to mandatory barriers instead. */
42 #define virtio_mb(vq) mb()
43 #define virtio_rmb(vq) rmb()
44 #define virtio_wmb(vq) wmb()
45 #endif
46
47 #ifdef DEBUG
48 /* For development, we want to crash whenever the ring is screwed. */
49 #define BAD_RING(_vq, fmt, args...)                             \
50         do {                                                    \
51                 dev_err(&(_vq)->vq.vdev->dev,                   \
52                         "%s:"fmt, (_vq)->vq.name, ##args);      \
53                 BUG();                                          \
54         } while (0)
55 /* Caller is supposed to guarantee no reentry. */
56 #define START_USE(_vq)                                          \
57         do {                                                    \
58                 if ((_vq)->in_use)                              \
59                         panic("%s:in_use = %i\n",               \
60                               (_vq)->vq.name, (_vq)->in_use);   \
61                 (_vq)->in_use = __LINE__;                       \
62         } while (0)
63 #define END_USE(_vq) \
64         do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
65 #else
66 #define BAD_RING(_vq, fmt, args...)                             \
67         do {                                                    \
68                 dev_err(&_vq->vq.vdev->dev,                     \
69                         "%s:"fmt, (_vq)->vq.name, ##args);      \
70                 (_vq)->broken = true;                           \
71         } while (0)
72 #define START_USE(vq)
73 #define END_USE(vq)
74 #endif
75
76 struct vring_virtqueue
77 {
78         struct virtqueue vq;
79
80         /* Actual memory layout for this queue */
81         struct vring vring;
82
83         /* Can we use weak barriers? */
84         bool weak_barriers;
85
86         /* Other side has made a mess, don't try any more. */
87         bool broken;
88
89         /* Host supports indirect buffers */
90         bool indirect;
91
92         /* Host publishes avail event idx */
93         bool event;
94
95         /* Number of free buffers */
96         unsigned int num_free;
97         /* Head of free buffer list. */
98         unsigned int free_head;
99         /* Number we've added since last sync. */
100         unsigned int num_added;
101
102         /* Last used index we've seen. */
103         u16 last_used_idx;
104
105         /* How to notify other side. FIXME: commonalize hcalls! */
106         void (*notify)(struct virtqueue *vq);
107
108 #ifdef DEBUG
109         /* They're supposed to lock for us. */
110         unsigned int in_use;
111 #endif
112
113         /* Tokens for callbacks. */
114         void *data[];
115 };
116
117 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
118
119 /* Set up an indirect table of descriptors and add it to the queue. */
120 static int vring_add_indirect(struct vring_virtqueue *vq,
121                               struct scatterlist sg[],
122                               unsigned int out,
123                               unsigned int in,
124                               gfp_t gfp)
125 {
126         struct vring_desc *desc;
127         unsigned head;
128         int i;
129
130         desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
131         if (!desc)
132                 return -ENOMEM;
133
134         /* Transfer entries from the sg list into the indirect page */
135         for (i = 0; i < out; i++) {
136                 desc[i].flags = VRING_DESC_F_NEXT;
137                 desc[i].addr = sg_phys(sg);
138                 desc[i].len = sg->length;
139                 desc[i].next = i+1;
140                 sg++;
141         }
142         for (; i < (out + in); i++) {
143                 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
144                 desc[i].addr = sg_phys(sg);
145                 desc[i].len = sg->length;
146                 desc[i].next = i+1;
147                 sg++;
148         }
149
150         /* Last one doesn't continue. */
151         desc[i-1].flags &= ~VRING_DESC_F_NEXT;
152         desc[i-1].next = 0;
153
154         /* We're about to use a buffer */
155         vq->num_free--;
156
157         /* Use a single buffer which doesn't continue */
158         head = vq->free_head;
159         vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
160         vq->vring.desc[head].addr = virt_to_phys(desc);
161         vq->vring.desc[head].len = i * sizeof(struct vring_desc);
162
163         /* Update free pointer */
164         vq->free_head = vq->vring.desc[head].next;
165
166         return head;
167 }
168
169 int virtqueue_add_buf_gfp(struct virtqueue *_vq,
170                           struct scatterlist sg[],
171                           unsigned int out,
172                           unsigned int in,
173                           void *data,
174                           gfp_t gfp)
175 {
176         struct vring_virtqueue *vq = to_vvq(_vq);
177         unsigned int i, avail, uninitialized_var(prev);
178         int head;
179
180         START_USE(vq);
181
182         BUG_ON(data == NULL);
183
184         /* If the host supports indirect descriptor tables, and we have multiple
185          * buffers, then go indirect. FIXME: tune this threshold */
186         if (vq->indirect && (out + in) > 1 && vq->num_free) {
187                 head = vring_add_indirect(vq, sg, out, in, gfp);
188                 if (likely(head >= 0))
189                         goto add_head;
190         }
191
192         BUG_ON(out + in > vq->vring.num);
193         BUG_ON(out + in == 0);
194
195         if (vq->num_free < out + in) {
196                 pr_debug("Can't add buf len %i - avail = %i\n",
197                          out + in, vq->num_free);
198                 /* FIXME: for historical reasons, we force a notify here if
199                  * there are outgoing parts to the buffer.  Presumably the
200                  * host should service the ring ASAP. */
201                 if (out)
202                         vq->notify(&vq->vq);
203                 END_USE(vq);
204                 return -ENOSPC;
205         }
206
207         /* We're about to use some buffers from the free list. */
208         vq->num_free -= out + in;
209
210         head = vq->free_head;
211         for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
212                 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
213                 vq->vring.desc[i].addr = sg_phys(sg);
214                 vq->vring.desc[i].len = sg->length;
215                 prev = i;
216                 sg++;
217         }
218         for (; in; i = vq->vring.desc[i].next, in--) {
219                 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
220                 vq->vring.desc[i].addr = sg_phys(sg);
221                 vq->vring.desc[i].len = sg->length;
222                 prev = i;
223                 sg++;
224         }
225         /* Last one doesn't continue. */
226         vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
227
228         /* Update free pointer */
229         vq->free_head = i;
230
231 add_head:
232         /* Set token. */
233         vq->data[head] = data;
234
235         /* Put entry in available array (but don't update avail->idx until they
236          * do sync).  FIXME: avoid modulus here? */
237         avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
238         vq->vring.avail->ring[avail] = head;
239
240         pr_debug("Added buffer head %i to %p\n", head, vq);
241         END_USE(vq);
242
243         return vq->num_free;
244 }
245 EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp);
246
247 void virtqueue_kick(struct virtqueue *_vq)
248 {
249         struct vring_virtqueue *vq = to_vvq(_vq);
250         u16 new, old;
251         START_USE(vq);
252         /* Descriptors and available array need to be set before we expose the
253          * new available array entries. */
254         virtio_wmb(vq);
255
256         old = vq->vring.avail->idx;
257         new = vq->vring.avail->idx = old + vq->num_added;
258         vq->num_added = 0;
259
260         /* Need to update avail index before checking if we should notify */
261         virtio_mb(vq);
262
263         if (vq->event ?
264             vring_need_event(vring_avail_event(&vq->vring), new, old) :
265             !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
266                 /* Prod other side to tell it about changes. */
267                 vq->notify(&vq->vq);
268
269         END_USE(vq);
270 }
271 EXPORT_SYMBOL_GPL(virtqueue_kick);
272
273 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
274 {
275         unsigned int i;
276
277         /* Clear data ptr. */
278         vq->data[head] = NULL;
279
280         /* Put back on free list: find end */
281         i = head;
282
283         /* Free the indirect table */
284         if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
285                 kfree(phys_to_virt(vq->vring.desc[i].addr));
286
287         while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
288                 i = vq->vring.desc[i].next;
289                 vq->num_free++;
290         }
291
292         vq->vring.desc[i].next = vq->free_head;
293         vq->free_head = head;
294         /* Plus final descriptor */
295         vq->num_free++;
296 }
297
298 static inline bool more_used(const struct vring_virtqueue *vq)
299 {
300         return vq->last_used_idx != vq->vring.used->idx;
301 }
302
303 void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
304 {
305         struct vring_virtqueue *vq = to_vvq(_vq);
306         void *ret;
307         unsigned int i;
308
309         START_USE(vq);
310
311         if (unlikely(vq->broken)) {
312                 END_USE(vq);
313                 return NULL;
314         }
315
316         if (!more_used(vq)) {
317                 pr_debug("No more buffers in queue\n");
318                 END_USE(vq);
319                 return NULL;
320         }
321
322         /* Only get used array entries after they have been exposed by host. */
323         virtio_rmb(vq);
324
325         i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
326         *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
327
328         if (unlikely(i >= vq->vring.num)) {
329                 BAD_RING(vq, "id %u out of range\n", i);
330                 return NULL;
331         }
332         if (unlikely(!vq->data[i])) {
333                 BAD_RING(vq, "id %u is not a head!\n", i);
334                 return NULL;
335         }
336
337         /* detach_buf clears data, so grab it now. */
338         ret = vq->data[i];
339         detach_buf(vq, i);
340         vq->last_used_idx++;
341         /* If we expect an interrupt for the next entry, tell host
342          * by writing event index and flush out the write before
343          * the read in the next get_buf call. */
344         if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
345                 vring_used_event(&vq->vring) = vq->last_used_idx;
346                 virtio_mb(vq);
347         }
348
349         END_USE(vq);
350         return ret;
351 }
352 EXPORT_SYMBOL_GPL(virtqueue_get_buf);
353
354 void virtqueue_disable_cb(struct virtqueue *_vq)
355 {
356         struct vring_virtqueue *vq = to_vvq(_vq);
357
358         vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
359 }
360 EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
361
362 bool virtqueue_enable_cb(struct virtqueue *_vq)
363 {
364         struct vring_virtqueue *vq = to_vvq(_vq);
365
366         START_USE(vq);
367
368         /* We optimistically turn back on interrupts, then check if there was
369          * more to do. */
370         /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
371          * either clear the flags bit or point the event index at the next
372          * entry. Always do both to keep code simple. */
373         vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
374         vring_used_event(&vq->vring) = vq->last_used_idx;
375         virtio_mb(vq);
376         if (unlikely(more_used(vq))) {
377                 END_USE(vq);
378                 return false;
379         }
380
381         END_USE(vq);
382         return true;
383 }
384 EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
385
386 bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
387 {
388         struct vring_virtqueue *vq = to_vvq(_vq);
389         u16 bufs;
390
391         START_USE(vq);
392
393         /* We optimistically turn back on interrupts, then check if there was
394          * more to do. */
395         /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
396          * either clear the flags bit or point the event index at the next
397          * entry. Always do both to keep code simple. */
398         vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
399         /* TODO: tune this threshold */
400         bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
401         vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
402         virtio_mb(vq);
403         if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
404                 END_USE(vq);
405                 return false;
406         }
407
408         END_USE(vq);
409         return true;
410 }
411 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
412
413 void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
414 {
415         struct vring_virtqueue *vq = to_vvq(_vq);
416         unsigned int i;
417         void *buf;
418
419         START_USE(vq);
420
421         for (i = 0; i < vq->vring.num; i++) {
422                 if (!vq->data[i])
423                         continue;
424                 /* detach_buf clears data, so grab it now. */
425                 buf = vq->data[i];
426                 detach_buf(vq, i);
427                 vq->vring.avail->idx--;
428                 END_USE(vq);
429                 return buf;
430         }
431         /* That should have freed everything. */
432         BUG_ON(vq->num_free != vq->vring.num);
433
434         END_USE(vq);
435         return NULL;
436 }
437 EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
438
439 irqreturn_t vring_interrupt(int irq, void *_vq)
440 {
441         struct vring_virtqueue *vq = to_vvq(_vq);
442
443         if (!more_used(vq)) {
444                 pr_debug("virtqueue interrupt with no work for %p\n", vq);
445                 return IRQ_NONE;
446         }
447
448         if (unlikely(vq->broken))
449                 return IRQ_HANDLED;
450
451         pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
452         if (vq->vq.callback)
453                 vq->vq.callback(&vq->vq);
454
455         return IRQ_HANDLED;
456 }
457 EXPORT_SYMBOL_GPL(vring_interrupt);
458
459 struct virtqueue *vring_new_virtqueue(unsigned int num,
460                                       unsigned int vring_align,
461                                       struct virtio_device *vdev,
462                                       bool weak_barriers,
463                                       void *pages,
464                                       void (*notify)(struct virtqueue *),
465                                       void (*callback)(struct virtqueue *),
466                                       const char *name)
467 {
468         struct vring_virtqueue *vq;
469         unsigned int i;
470
471         /* We assume num is a power of 2. */
472         if (num & (num - 1)) {
473                 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
474                 return NULL;
475         }
476
477         vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
478         if (!vq)
479                 return NULL;
480
481         vring_init(&vq->vring, num, pages, vring_align);
482         vq->vq.callback = callback;
483         vq->vq.vdev = vdev;
484         vq->vq.name = name;
485         vq->notify = notify;
486         vq->weak_barriers = weak_barriers;
487         vq->broken = false;
488         vq->last_used_idx = 0;
489         vq->num_added = 0;
490         list_add_tail(&vq->vq.list, &vdev->vqs);
491 #ifdef DEBUG
492         vq->in_use = false;
493 #endif
494
495         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
496         vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
497
498         /* No callback?  Tell other side not to bother us. */
499         if (!callback)
500                 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
501
502         /* Put everything in free lists. */
503         vq->num_free = num;
504         vq->free_head = 0;
505         for (i = 0; i < num-1; i++) {
506                 vq->vring.desc[i].next = i+1;
507                 vq->data[i] = NULL;
508         }
509         vq->data[i] = NULL;
510
511         return &vq->vq;
512 }
513 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
514
515 void vring_del_virtqueue(struct virtqueue *vq)
516 {
517         list_del(&vq->list);
518         kfree(to_vvq(vq));
519 }
520 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
521
522 /* Manipulates transport-specific feature bits. */
523 void vring_transport_features(struct virtio_device *vdev)
524 {
525         unsigned int i;
526
527         for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
528                 switch (i) {
529                 case VIRTIO_RING_F_INDIRECT_DESC:
530                         break;
531                 case VIRTIO_RING_F_EVENT_IDX:
532                         break;
533                 default:
534                         /* We don't understand this bit. */
535                         clear_bit(i, vdev->features);
536                 }
537         }
538 }
539 EXPORT_SYMBOL_GPL(vring_transport_features);
540
541 /* return the size of the vring within the virtqueue */
542 unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
543 {
544
545         struct vring_virtqueue *vq = to_vvq(_vq);
546
547         return vq->vring.num;
548 }
549 EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
550
551 MODULE_LICENSE("GPL");