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 = vq->vring.used;
39 int more = vq->last_used_idx != 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 (desc[i].flags & VRING_DESC_F_NEXT)
63 /* link it with free list and point to it */
65 desc[i].next = vq->free_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 = vq->vring.used;
84 // BUG_ON(!vring_more_used(vq));
86 elem = &used->ring[vq->last_used_idx % vr->num];
95 vq->last_used_idx = 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 = vr->desc;
108 struct vring_avail *avail = vr->avail;
110 BUG_ON(out + in == 0);
113 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;
121 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;
128 desc[prev].flags = desc[prev].flags & ~VRING_DESC_F_NEXT;
132 vq->vdata[head] = index;
134 av = (avail->idx + num_added) % vr->num;
135 avail->ring[av] = head;
138 void vring_kick(struct vp_device *vp, struct vring_virtqueue *vq, int num_added)
140 struct vring *vr = &vq->vring;
141 struct vring_avail *avail = vr->avail;
143 /* Make sure idx update is done after ring write. */
145 avail->idx = avail->idx + num_added;