1 /* virtio-pci.c - virtio ring management
3 * (c) Copyright 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
7 * some parts from Linux Virtio Ring
9 * Copyright Rusty Russell IBM Corporation 2007
11 * Adopted for Seabios: Gleb Natapov <gleb@redhat.com>
13 * This work is licensed under the terms of the GNU LGPLv3
14 * See the COPYING file in the top-level directory.
19 #include "biosvar.h" // GET_GLOBAL
20 #include "output.h" // panic
21 #include "virtio-ring.h"
22 #include "virtio-pci.h"
25 panic("BUG: failure at %d/%s()!\n", __LINE__, __func__); \
27 #define BUG_ON(condition) do { if (condition) BUG(); } while (0)
32 * is there some used buffers ?
36 int vring_more_used(struct vring_virtqueue *vq)
38 struct vring_used *used = GET_LOWFLAT(vq->vring.used);
39 int more = GET_LOWFLAT(vq->last_used_idx) != GET_LOWFLAT(used->idx);
40 /* Make sure ring reads are done after idx read above. */
48 * put at the begin of the free list the current desc[head]
51 void vring_detach(struct vring_virtqueue *vq, unsigned int head)
53 struct vring *vr = &vq->vring;
54 struct vring_desc *desc = GET_LOWFLAT(vr->desc);
57 /* find end of given descriptor */
60 while (GET_LOWFLAT(desc[i].flags) & VRING_DESC_F_NEXT)
61 i = GET_LOWFLAT(desc[i].next);
63 /* link it with free list and point to it */
65 SET_LOWFLAT(desc[i].next, GET_LOWFLAT(vq->free_head));
66 SET_LOWFLAT(vq->free_head, head);
72 * get a buffer from the used list
76 int vring_get_buf(struct vring_virtqueue *vq, unsigned int *len)
78 struct vring *vr = &vq->vring;
79 struct vring_used_elem *elem;
80 struct vring_used *used = GET_LOWFLAT(vq->vring.used);
84 // BUG_ON(!vring_more_used(vq));
86 elem = &used->ring[GET_LOWFLAT(vq->last_used_idx) % GET_LOWFLAT(vr->num)];
87 id = GET_LOWFLAT(elem->id);
89 *len = GET_LOWFLAT(elem->len);
91 ret = GET_LOWFLAT(vq->vdata[id]);
95 SET_LOWFLAT(vq->last_used_idx, GET_LOWFLAT(vq->last_used_idx) + 1);
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)
105 struct vring *vr = &vq->vring;
106 int i, av, head, prev;
107 struct vring_desc *desc = GET_LOWFLAT(vr->desc);
108 struct vring_avail *avail = GET_LOWFLAT(vr->avail);
110 BUG_ON(out + in == 0);
113 head = GET_LOWFLAT(vq->free_head);
114 for (i = head; out; i = GET_LOWFLAT(desc[i].next), out--) {
115 SET_LOWFLAT(desc[i].flags, VRING_DESC_F_NEXT);
116 SET_LOWFLAT(desc[i].addr, (u64)virt_to_phys(list->addr));
117 SET_LOWFLAT(desc[i].len, list->length);
121 for ( ; in; i = GET_LOWFLAT(desc[i].next), in--) {
122 SET_LOWFLAT(desc[i].flags, VRING_DESC_F_NEXT|VRING_DESC_F_WRITE);
123 SET_LOWFLAT(desc[i].addr, (u64)virt_to_phys(list->addr));
124 SET_LOWFLAT(desc[i].len, list->length);
128 SET_LOWFLAT(desc[prev].flags,
129 GET_LOWFLAT(desc[prev].flags) & ~VRING_DESC_F_NEXT);
131 SET_LOWFLAT(vq->free_head, i);
133 SET_LOWFLAT(vq->vdata[head], index);
135 av = (GET_LOWFLAT(avail->idx) + num_added) % GET_LOWFLAT(vr->num);
136 SET_LOWFLAT(avail->ring[av], head);
139 void vring_kick(unsigned int ioaddr, struct vring_virtqueue *vq, int num_added)
141 struct vring *vr = &vq->vring;
142 struct vring_avail *avail = GET_LOWFLAT(vr->avail);
144 /* Make sure idx update is done after ring write. */
146 SET_LOWFLAT(avail->idx, GET_LOWFLAT(avail->idx) + num_added);
148 vp_notify(ioaddr, GET_LOWFLAT(vq->queue_index));