blob: 0ed3189ee2c92fc297243cd68e6dae14b3954d4a [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 "output.h" // panic
Gleb Natapov89acfa32010-05-10 11:36:37 +030020#include "virtio-ring.h"
21#include "virtio-pci.h"
22
Kevin O'Connordc15c8d2013-03-08 19:28:06 -050023#define BUG() do { \
24 panic("BUG: failure at %d/%s()!\n", __LINE__, __func__); \
Gleb Natapov89acfa32010-05-10 11:36:37 +030025 } while (0)
26#define BUG_ON(condition) do { if (condition) BUG(); } while (0)
27
28/*
29 * vring_more_used
30 *
31 * is there some used buffers ?
32 *
33 */
34
35int vring_more_used(struct vring_virtqueue *vq)
36{
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020037 struct vring_used *used = vq->vring.used;
38 int more = vq->last_used_idx != used->idx;
Michael S. Tsirkin0f3783b2010-05-20 16:36:32 +030039 /* Make sure ring reads are done after idx read above. */
40 smp_rmb();
41 return more;
Gleb Natapov89acfa32010-05-10 11:36:37 +030042}
43
44/*
45 * vring_free
46 *
47 * put at the begin of the free list the current desc[head]
48 */
49
50void vring_detach(struct vring_virtqueue *vq, unsigned int head)
51{
52 struct vring *vr = &vq->vring;
Kevin O'Connor782f9be2012-05-24 23:56:03 -040053 struct vring_desc *desc = GET_LOWFLAT(vr->desc);
Gleb Natapov89acfa32010-05-10 11:36:37 +030054 unsigned int i;
55
56 /* find end of given descriptor */
57
58 i = head;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020059 while (desc[i].flags & VRING_DESC_F_NEXT)
60 i = desc[i].next;
Gleb Natapov89acfa32010-05-10 11:36:37 +030061
62 /* link it with free list and point to it */
63
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020064 desc[i].next = vq->free_head;
65 vq->free_head = head;
Gleb Natapov89acfa32010-05-10 11:36:37 +030066}
67
68/*
69 * vring_get_buf
70 *
71 * get a buffer from the used list
72 *
73 */
74
75int vring_get_buf(struct vring_virtqueue *vq, unsigned int *len)
76{
77 struct vring *vr = &vq->vring;
78 struct vring_used_elem *elem;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020079 struct vring_used *used = vq->vring.used;
Gleb Natapov89acfa32010-05-10 11:36:37 +030080 u32 id;
81 int ret;
82
83// BUG_ON(!vring_more_used(vq));
84
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020085 elem = &used->ring[vq->last_used_idx % vr->num];
86 id = elem->id;
Gleb Natapov89acfa32010-05-10 11:36:37 +030087 if (len != NULL)
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020088 *len = elem->len;
Gleb Natapov89acfa32010-05-10 11:36:37 +030089
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020090 ret = vq->vdata[id];
Gleb Natapov89acfa32010-05-10 11:36:37 +030091
92 vring_detach(vq, id);
93
Gerd Hoffmann1ffda842015-07-01 14:45:58 +020094 vq->last_used_idx = vq->last_used_idx + 1;
Gleb Natapov89acfa32010-05-10 11:36:37 +030095
96 return ret;
97}
98
99void vring_add_buf(struct vring_virtqueue *vq,
100 struct vring_list list[],
101 unsigned int out, unsigned int in,
102 int index, int num_added)
103{
104 struct vring *vr = &vq->vring;
105 int i, av, head, prev;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200106 struct vring_desc *desc = vr->desc;
107 struct vring_avail *avail = vr->avail;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300108
109 BUG_ON(out + in == 0);
110
111 prev = 0;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200112 head = vq->free_head;
113 for (i = head; out; i = desc[i].next, out--) {
114 desc[i].flags = VRING_DESC_F_NEXT;
115 desc[i].addr = (u64)virt_to_phys(list->addr);
116 desc[i].len = list->length;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300117 prev = i;
118 list++;
119 }
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200120 for ( ; in; i = desc[i].next, in--) {
121 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
122 desc[i].addr = (u64)virt_to_phys(list->addr);
123 desc[i].len = list->length;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300124 prev = i;
125 list++;
126 }
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200127 desc[prev].flags = desc[prev].flags & ~VRING_DESC_F_NEXT;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300128
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200129 vq->free_head = i;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300130
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200131 vq->vdata[head] = index;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300132
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200133 av = (avail->idx + num_added) % vr->num;
134 avail->ring[av] = head;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300135}
136
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +0200137void vring_kick(struct vp_device *vp, struct vring_virtqueue *vq, int num_added)
Gleb Natapov89acfa32010-05-10 11:36:37 +0300138{
139 struct vring *vr = &vq->vring;
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200140 struct vring_avail *avail = vr->avail;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300141
Michael S. Tsirkin0f3783b2010-05-20 16:36:32 +0300142 /* Make sure idx update is done after ring write. */
143 smp_wmb();
Gerd Hoffmann1ffda842015-07-01 14:45:58 +0200144 avail->idx = avail->idx + num_added;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300145
Gerd Hoffmann2e68fb12015-06-26 08:19:28 +0200146 vp_notify(vp, vq);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300147}