blob: 7205a0acd55b62efa923fd48de62af45bcecfe1f [file] [log] [blame]
Gleb Natapov89acfa32010-05-10 11:36:37 +03001/* virtio-pci.c - virtio ring management
2 *
3 * (c) Copyright 2008 Bull S.A.S.
4 *
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
6 *
7 * some parts from Linux Virtio Ring
8 *
9 * Copyright Rusty Russell IBM Corporation 2007
10 *
11 * Adopted for Seabios: Gleb Natapov <gleb@redhat.com>
12 *
13 * This work is licensed under the terms of the GNU LGPLv3
14 * See the COPYING file in the top-level directory.
15 *
16 *
17 */
18
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040019#include "biosvar.h" // GET_GLOBAL
20#include "output.h" // panic
Gleb Natapov89acfa32010-05-10 11:36:37 +030021#include "virtio-ring.h"
22#include "virtio-pci.h"
23
Kevin O'Connordc15c8d2013-03-08 19:28:06 -050024#define BUG() do { \
25 panic("BUG: failure at %d/%s()!\n", __LINE__, __func__); \
Gleb Natapov89acfa32010-05-10 11:36:37 +030026 } while (0)
27#define BUG_ON(condition) do { if (condition) BUG(); } while (0)
28
29/*
30 * vring_more_used
31 *
32 * is there some used buffers ?
33 *
34 */
35
36int vring_more_used(struct vring_virtqueue *vq)
37{
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020038 struct vring_used *used = vq->vring.used;
39 int more = vq->last_used_idx != used->idx;
Michael S. Tsirkin0f3783b2010-05-20 16:36:32 +030040 /* Make sure ring reads are done after idx read above. */
41 smp_rmb();
42 return more;
Gleb Natapov89acfa32010-05-10 11:36:37 +030043}
44
45/*
46 * vring_free
47 *
48 * put at the begin of the free list the current desc[head]
49 */
50
51void vring_detach(struct vring_virtqueue *vq, unsigned int head)
52{
53 struct vring *vr = &vq->vring;
Kevin O'Connor782f9be2012-05-24 23:56:03 -040054 struct vring_desc *desc = GET_LOWFLAT(vr->desc);
Gleb Natapov89acfa32010-05-10 11:36:37 +030055 unsigned int i;
56
57 /* find end of given descriptor */
58
59 i = head;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020060 while (desc[i].flags & VRING_DESC_F_NEXT)
61 i = desc[i].next;
Gleb Natapov89acfa32010-05-10 11:36:37 +030062
63 /* link it with free list and point to it */
64
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020065 desc[i].next = vq->free_head;
66 vq->free_head = head;
Gleb Natapov89acfa32010-05-10 11:36:37 +030067}
68
69/*
70 * vring_get_buf
71 *
72 * get a buffer from the used list
73 *
74 */
75
76int vring_get_buf(struct vring_virtqueue *vq, unsigned int *len)
77{
78 struct vring *vr = &vq->vring;
79 struct vring_used_elem *elem;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020080 struct vring_used *used = vq->vring.used;
Gleb Natapov89acfa32010-05-10 11:36:37 +030081 u32 id;
82 int ret;
83
84// BUG_ON(!vring_more_used(vq));
85
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020086 elem = &used->ring[vq->last_used_idx % vr->num];
87 id = elem->id;
Gleb Natapov89acfa32010-05-10 11:36:37 +030088 if (len != NULL)
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020089 *len = elem->len;
Gleb Natapov89acfa32010-05-10 11:36:37 +030090
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020091 ret = vq->vdata[id];
Gleb Natapov89acfa32010-05-10 11:36:37 +030092
93 vring_detach(vq, id);
94
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020095 vq->last_used_idx = vq->last_used_idx + 1;
Gleb Natapov89acfa32010-05-10 11:36:37 +030096
97 return ret;
98}
99
100void vring_add_buf(struct vring_virtqueue *vq,
101 struct vring_list list[],
102 unsigned int out, unsigned int in,
103 int index, int num_added)
104{
105 struct vring *vr = &vq->vring;
106 int i, av, head, prev;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200107 struct vring_desc *desc = vr->desc;
108 struct vring_avail *avail = vr->avail;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300109
110 BUG_ON(out + in == 0);
111
112 prev = 0;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200113 head = vq->free_head;
114 for (i = head; out; i = desc[i].next, out--) {
115 desc[i].flags = VRING_DESC_F_NEXT;
116 desc[i].addr = (u64)virt_to_phys(list->addr);
117 desc[i].len = list->length;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300118 prev = i;
119 list++;
120 }
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200121 for ( ; in; i = desc[i].next, in--) {
122 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
123 desc[i].addr = (u64)virt_to_phys(list->addr);
124 desc[i].len = list->length;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300125 prev = i;
126 list++;
127 }
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200128 desc[prev].flags = desc[prev].flags & ~VRING_DESC_F_NEXT;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300129
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200130 vq->free_head = i;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300131
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200132 vq->vdata[head] = index;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300133
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200134 av = (avail->idx + num_added) % vr->num;
135 avail->ring[av] = head;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300136}
137
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +0200138void vring_kick(struct vp_device *vp, struct vring_virtqueue *vq, int num_added)
Gleb Natapov89acfa32010-05-10 11:36:37 +0300139{
140 struct vring *vr = &vq->vring;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200141 struct vring_avail *avail = vr->avail;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300142
Michael S. Tsirkin0f3783b2010-05-20 16:36:32 +0300143 /* Make sure idx update is done after ring write. */
144 smp_wmb();
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200145 avail->idx = avail->idx + num_added;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300146
Gerd Hoffmann2e68fb12015-06-26 08:19:28 +0200147 vp_notify(vp, vq);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300148}