Support for booting from virtio disks
[seabios.git] / src / virtio-ring.c
1 /* 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
19 #include "virtio-ring.h"
20 #include "virtio-pci.h"
21
22 #define BUG() do {                                      \
23         dprintf(1, "BUG: failure at %s:%d/%s()!\n",     \
24                 __FILE__, __LINE__, __FUNCTION__);      \
25                 while(1);                               \
26         } 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
36 int vring_more_used(struct vring_virtqueue *vq)
37 {
38     struct vring_used *used = GET_FLATPTR(vq->vring.used);
39     wmb();
40     return GET_FLATPTR(vq->last_used_idx) != GET_FLATPTR(used->idx);
41 }
42
43 /*
44  * vring_free
45  *
46  * put at the begin of the free list the current desc[head]
47  */
48
49 void vring_detach(struct vring_virtqueue *vq, unsigned int head)
50 {
51     struct vring *vr = &vq->vring;
52     struct vring_desc *desc = GET_FLATPTR(vr->desc);
53     unsigned int i;
54
55     /* find end of given descriptor */
56
57     i = head;
58     while (GET_FLATPTR(desc[i].flags) & VRING_DESC_F_NEXT)
59         i = GET_FLATPTR(desc[i].next);
60
61     /* link it with free list and point to it */
62
63     SET_FLATPTR(desc[i].next, GET_FLATPTR(vq->free_head));
64     wmb();
65     SET_FLATPTR(vq->free_head, head);
66 }
67
68 /*
69  * vring_get_buf
70  *
71  * get a buffer from the used list
72  *
73  */
74
75 int vring_get_buf(struct vring_virtqueue *vq, unsigned int *len)
76 {
77     struct vring *vr = &vq->vring;
78     struct vring_used_elem *elem;
79     struct vring_used *used = GET_FLATPTR(vq->vring.used);
80     u32 id;
81     int ret;
82
83 //    BUG_ON(!vring_more_used(vq));
84
85     elem = &used->ring[GET_FLATPTR(vq->last_used_idx) % GET_FLATPTR(vr->num)];
86     wmb();
87     id = GET_FLATPTR(elem->id);
88     if (len != NULL)
89         *len = GET_FLATPTR(elem->len);
90
91     ret = GET_FLATPTR(vq->vdata[id]);
92
93     vring_detach(vq, id);
94
95     SET_FLATPTR(vq->last_used_idx, GET_FLATPTR(vq->last_used_idx) + 1);
96
97     return ret;
98 }
99
100 void 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;
107     struct vring_desc *desc = GET_FLATPTR(vr->desc);
108     struct vring_avail *avail = GET_FLATPTR(vr->avail);
109
110     BUG_ON(out + in == 0);
111
112     prev = 0;
113     head = GET_FLATPTR(vq->free_head);
114     for (i = head; out; i = GET_FLATPTR(desc[i].next), out--) {
115         SET_FLATPTR(desc[i].flags, VRING_DESC_F_NEXT);
116         SET_FLATPTR(desc[i].addr, (u64)virt_to_phys(list->addr));
117         SET_FLATPTR(desc[i].len, list->length);
118         prev = i;
119         list++;
120     }
121     for ( ; in; i = GET_FLATPTR(desc[i].next), in--) {
122         SET_FLATPTR(desc[i].flags, VRING_DESC_F_NEXT|VRING_DESC_F_WRITE);
123         SET_FLATPTR(desc[i].addr, (u64)virt_to_phys(list->addr));
124         SET_FLATPTR(desc[i].len, list->length);
125         prev = i;
126         list++;
127     }
128     SET_FLATPTR(desc[prev].flags,
129                 GET_FLATPTR(desc[prev].flags) & ~VRING_DESC_F_NEXT);
130
131     SET_FLATPTR(vq->free_head, i);
132
133     SET_FLATPTR(vq->vdata[head], index);
134
135     av = (GET_FLATPTR(avail->idx) + num_added) % GET_FLATPTR(vr->num);
136     SET_FLATPTR(avail->ring[av], head);
137     wmb();
138 }
139
140 void vring_kick(unsigned int ioaddr, struct vring_virtqueue *vq, int num_added)
141 {
142     struct vring *vr = &vq->vring;
143     struct vring_avail *avail = GET_FLATPTR(vr->avail);
144     struct vring_used *used = GET_FLATPTR(vq->vring.used);
145
146     wmb();
147     SET_FLATPTR(avail->idx, GET_FLATPTR(avail->idx) + num_added);
148
149     mb();
150     if (!(GET_FLATPTR(used->flags) & VRING_USED_F_NO_NOTIFY))
151         vp_notify(ioaddr, GET_FLATPTR(vq->queue_index));
152 }