blob: 97e0b34876b2ab04d01a5e9a9ab8cf1165ef5593 [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{
Kevin O'Connor782f9be2012-05-24 23:56:03 -040038 struct vring_used *used = GET_LOWFLAT(vq->vring.used);
39 int more = GET_LOWFLAT(vq->last_used_idx) != GET_LOWFLAT(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;
Kevin O'Connor782f9be2012-05-24 23:56:03 -040060 while (GET_LOWFLAT(desc[i].flags) & VRING_DESC_F_NEXT)
61 i = GET_LOWFLAT(desc[i].next);
Gleb Natapov89acfa32010-05-10 11:36:37 +030062
63 /* link it with free list and point to it */
64
Kevin O'Connor782f9be2012-05-24 23:56:03 -040065 SET_LOWFLAT(desc[i].next, GET_LOWFLAT(vq->free_head));
66 SET_LOWFLAT(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;
Kevin O'Connor782f9be2012-05-24 23:56:03 -040080 struct vring_used *used = GET_LOWFLAT(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
Kevin O'Connor782f9be2012-05-24 23:56:03 -040086 elem = &used->ring[GET_LOWFLAT(vq->last_used_idx) % GET_LOWFLAT(vr->num)];
87 id = GET_LOWFLAT(elem->id);
Gleb Natapov89acfa32010-05-10 11:36:37 +030088 if (len != NULL)
Kevin O'Connor782f9be2012-05-24 23:56:03 -040089 *len = GET_LOWFLAT(elem->len);
Gleb Natapov89acfa32010-05-10 11:36:37 +030090
Kevin O'Connor782f9be2012-05-24 23:56:03 -040091 ret = GET_LOWFLAT(vq->vdata[id]);
Gleb Natapov89acfa32010-05-10 11:36:37 +030092
93 vring_detach(vq, id);
94
Kevin O'Connor782f9be2012-05-24 23:56:03 -040095 SET_LOWFLAT(vq->last_used_idx, GET_LOWFLAT(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;
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400107 struct vring_desc *desc = GET_LOWFLAT(vr->desc);
108 struct vring_avail *avail = GET_LOWFLAT(vr->avail);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300109
110 BUG_ON(out + in == 0);
111
112 prev = 0;
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400113 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);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300118 prev = i;
119 list++;
120 }
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400121 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);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300125 prev = i;
126 list++;
127 }
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400128 SET_LOWFLAT(desc[prev].flags,
129 GET_LOWFLAT(desc[prev].flags) & ~VRING_DESC_F_NEXT);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300130
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400131 SET_LOWFLAT(vq->free_head, i);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300132
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400133 SET_LOWFLAT(vq->vdata[head], index);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300134
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400135 av = (GET_LOWFLAT(avail->idx) + num_added) % GET_LOWFLAT(vr->num);
136 SET_LOWFLAT(avail->ring[av], head);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300137}
138
139void vring_kick(unsigned int ioaddr, struct vring_virtqueue *vq, int num_added)
140{
141 struct vring *vr = &vq->vring;
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400142 struct vring_avail *avail = GET_LOWFLAT(vr->avail);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300143
Michael S. Tsirkin0f3783b2010-05-20 16:36:32 +0300144 /* Make sure idx update is done after ring write. */
145 smp_wmb();
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400146 SET_LOWFLAT(avail->idx, GET_LOWFLAT(avail->idx) + num_added);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300147
Kevin O'Connor782f9be2012-05-24 23:56:03 -0400148 vp_notify(ioaddr, GET_LOWFLAT(vq->queue_index));
Gleb Natapov89acfa32010-05-10 11:36:37 +0300149}