1 /* 2 * libqos virtio driver 3 * 4 * Copyright (c) 2014 Marc Marí 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 */ 9 10 #include <glib.h> 11 #include "libqtest.h" 12 #include "libqos/virtio.h" 13 14 uint8_t qvirtio_config_readb(const QVirtioBus *bus, QVirtioDevice *d, 15 void *addr) 16 { 17 return bus->config_readb(d, addr); 18 } 19 20 uint16_t qvirtio_config_readw(const QVirtioBus *bus, QVirtioDevice *d, 21 void *addr) 22 { 23 return bus->config_readw(d, addr); 24 } 25 26 uint32_t qvirtio_config_readl(const QVirtioBus *bus, QVirtioDevice *d, 27 void *addr) 28 { 29 return bus->config_readl(d, addr); 30 } 31 32 uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d, 33 void *addr) 34 { 35 return bus->config_readq(d, addr); 36 } 37 38 uint32_t qvirtio_get_features(const QVirtioBus *bus, QVirtioDevice *d) 39 { 40 return bus->get_features(d); 41 } 42 43 void qvirtio_set_features(const QVirtioBus *bus, QVirtioDevice *d, 44 uint32_t features) 45 { 46 bus->set_features(d, features); 47 } 48 49 QVirtQueue *qvirtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d, 50 QGuestAllocator *alloc, uint16_t index) 51 { 52 return bus->virtqueue_setup(d, alloc, index); 53 } 54 55 void qvirtio_reset(const QVirtioBus *bus, QVirtioDevice *d) 56 { 57 bus->set_status(d, QVIRTIO_RESET); 58 g_assert_cmphex(bus->get_status(d), ==, QVIRTIO_RESET); 59 } 60 61 void qvirtio_set_acknowledge(const QVirtioBus *bus, QVirtioDevice *d) 62 { 63 bus->set_status(d, bus->get_status(d) | QVIRTIO_ACKNOWLEDGE); 64 g_assert_cmphex(bus->get_status(d), ==, QVIRTIO_ACKNOWLEDGE); 65 } 66 67 void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d) 68 { 69 bus->set_status(d, bus->get_status(d) | QVIRTIO_DRIVER); 70 g_assert_cmphex(bus->get_status(d), ==, 71 QVIRTIO_DRIVER | QVIRTIO_ACKNOWLEDGE); 72 } 73 74 void qvirtio_set_driver_ok(const QVirtioBus *bus, QVirtioDevice *d) 75 { 76 bus->set_status(d, bus->get_status(d) | QVIRTIO_DRIVER_OK); 77 g_assert_cmphex(bus->get_status(d), ==, 78 QVIRTIO_DRIVER_OK | QVIRTIO_DRIVER | QVIRTIO_ACKNOWLEDGE); 79 } 80 81 bool qvirtio_wait_queue_isr(const QVirtioBus *bus, QVirtioDevice *d, 82 QVirtQueue *vq, uint64_t timeout) 83 { 84 do { 85 clock_step(100); 86 if (bus->get_queue_isr_status(d, vq)) { 87 break; /* It has ended */ 88 } 89 } while (--timeout); 90 91 return timeout != 0; 92 } 93 94 /* Wait for the status byte at given guest memory address to be set 95 * 96 * The virtqueue interrupt must not be raised, making this useful for testing 97 * event_index functionality. 98 */ 99 uint8_t qvirtio_wait_status_byte_no_isr(const QVirtioBus *bus, 100 QVirtioDevice *d, 101 QVirtQueue *vq, 102 uint64_t addr, 103 gint64 timeout_us) 104 { 105 gint64 start_time = g_get_monotonic_time(); 106 uint8_t val; 107 108 while ((val = readb(addr)) == 0xff) { 109 clock_step(100); 110 g_assert(!bus->get_queue_isr_status(d, vq)); 111 g_assert(g_get_monotonic_time() - start_time <= timeout_us); 112 } 113 return val; 114 } 115 116 bool qvirtio_wait_config_isr(const QVirtioBus *bus, QVirtioDevice *d, 117 uint64_t timeout) 118 { 119 do { 120 clock_step(100); 121 if (bus->get_config_isr_status(d)) { 122 break; /* It has ended */ 123 } 124 } while (--timeout); 125 126 return timeout != 0; 127 } 128 129 void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr) 130 { 131 int i; 132 133 vq->desc = addr; 134 vq->avail = vq->desc + vq->size*sizeof(QVRingDesc); 135 vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size) 136 + vq->align - 1) & ~(vq->align - 1)); 137 138 for (i = 0; i < vq->size - 1; i++) { 139 /* vq->desc[i].addr */ 140 writew(vq->desc + (16 * i), 0); 141 /* vq->desc[i].next */ 142 writew(vq->desc + (16 * i) + 14, i + 1); 143 } 144 145 /* vq->avail->flags */ 146 writew(vq->avail, 0); 147 /* vq->avail->idx */ 148 writew(vq->avail + 2, 0); 149 /* vq->avail->used_event */ 150 writew(vq->avail + 4 + (2 * vq->size), 0); 151 152 /* vq->used->flags */ 153 writew(vq->used, 0); 154 /* vq->used->avail_event */ 155 writew(vq->used+2+(sizeof(struct QVRingUsedElem)*vq->size), 0); 156 } 157 158 QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d, 159 QGuestAllocator *alloc, uint16_t elem) 160 { 161 int i; 162 QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect)); 163 164 indirect->index = 0; 165 indirect->elem = elem; 166 indirect->desc = guest_alloc(alloc, sizeof(QVRingDesc)*elem); 167 168 for (i = 0; i < elem - 1; ++i) { 169 /* indirect->desc[i].addr */ 170 writeq(indirect->desc + (16 * i), 0); 171 /* indirect->desc[i].flags */ 172 writew(indirect->desc + (16 * i) + 12, QVRING_DESC_F_NEXT); 173 /* indirect->desc[i].next */ 174 writew(indirect->desc + (16 * i) + 14, i + 1); 175 } 176 177 return indirect; 178 } 179 180 void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data, 181 uint32_t len, bool write) 182 { 183 uint16_t flags; 184 185 g_assert_cmpint(indirect->index, <, indirect->elem); 186 187 flags = readw(indirect->desc + (16 * indirect->index) + 12); 188 189 if (write) { 190 flags |= QVRING_DESC_F_WRITE; 191 } 192 193 /* indirect->desc[indirect->index].addr */ 194 writeq(indirect->desc + (16 * indirect->index), data); 195 /* indirect->desc[indirect->index].len */ 196 writel(indirect->desc + (16 * indirect->index) + 8, len); 197 /* indirect->desc[indirect->index].flags */ 198 writew(indirect->desc + (16 * indirect->index) + 12, flags); 199 200 indirect->index++; 201 } 202 203 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write, 204 bool next) 205 { 206 uint16_t flags = 0; 207 vq->num_free--; 208 209 if (write) { 210 flags |= QVRING_DESC_F_WRITE; 211 } 212 213 if (next) { 214 flags |= QVRING_DESC_F_NEXT; 215 } 216 217 /* vq->desc[vq->free_head].addr */ 218 writeq(vq->desc + (16 * vq->free_head), data); 219 /* vq->desc[vq->free_head].len */ 220 writel(vq->desc + (16 * vq->free_head) + 8, len); 221 /* vq->desc[vq->free_head].flags */ 222 writew(vq->desc + (16 * vq->free_head) + 12, flags); 223 224 return vq->free_head++; /* Return and increase, in this order */ 225 } 226 227 uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect) 228 { 229 g_assert(vq->indirect); 230 g_assert_cmpint(vq->size, >=, indirect->elem); 231 g_assert_cmpint(indirect->index, ==, indirect->elem); 232 233 vq->num_free--; 234 235 /* vq->desc[vq->free_head].addr */ 236 writeq(vq->desc + (16 * vq->free_head), indirect->desc); 237 /* vq->desc[vq->free_head].len */ 238 writel(vq->desc + (16 * vq->free_head) + 8, 239 sizeof(QVRingDesc) * indirect->elem); 240 /* vq->desc[vq->free_head].flags */ 241 writew(vq->desc + (16 * vq->free_head) + 12, QVRING_DESC_F_INDIRECT); 242 243 return vq->free_head++; /* Return and increase, in this order */ 244 } 245 246 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq, 247 uint32_t free_head) 248 { 249 /* vq->avail->idx */ 250 uint16_t idx = readl(vq->avail + 2); 251 /* vq->used->flags */ 252 uint16_t flags; 253 /* vq->used->avail_event */ 254 uint16_t avail_event; 255 256 /* vq->avail->ring[idx % vq->size] */ 257 writel(vq->avail + 4 + (2 * (idx % vq->size)), free_head); 258 /* vq->avail->idx */ 259 writel(vq->avail + 2, idx + 1); 260 261 /* Must read after idx is updated */ 262 flags = readw(vq->avail); 263 avail_event = readw(vq->used + 4 + 264 (sizeof(struct QVRingUsedElem) * vq->size)); 265 266 /* < 1 because we add elements to avail queue one by one */ 267 if ((flags & QVRING_USED_F_NO_NOTIFY) == 0 && 268 (!vq->event || (uint16_t)(idx-avail_event) < 1)) { 269 bus->virtqueue_kick(d, vq); 270 } 271 } 272 273 void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx) 274 { 275 g_assert(vq->event); 276 277 /* vq->avail->used_event */ 278 writew(vq->avail + 4 + (2 * vq->size), idx); 279 } 280