Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi> |
| 4 | * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> |
| 5 | * |
| 6 | * virtio ring implementation |
| 7 | */ |
| 8 | |
Will Deacon | 1eb6e62 | 2023-03-29 22:24:59 +0800 | [diff] [blame] | 9 | #include <bouncebuf.h> |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <virtio_types.h> |
| 15 | #include <virtio.h> |
| 16 | #include <virtio_ring.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 17 | #include <linux/bug.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <linux/compat.h> |
Will Deacon | 0edbe5c | 2023-03-29 22:24:57 +0800 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | |
| 21 | static void *virtio_alloc_pages(struct udevice *vdev, u32 npages) |
| 22 | { |
| 23 | return memalign(PAGE_SIZE, npages * PAGE_SIZE); |
| 24 | } |
| 25 | |
| 26 | static void virtio_free_pages(struct udevice *vdev, void *ptr, u32 npages) |
| 27 | { |
| 28 | free(ptr); |
| 29 | } |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 30 | |
Will Deacon | 21da252 | 2023-03-29 22:25:00 +0800 | [diff] [blame] | 31 | static int __bb_force_page_align(struct bounce_buffer *state) |
| 32 | { |
| 33 | const ulong align_mask = PAGE_SIZE - 1; |
| 34 | |
| 35 | if ((ulong)state->user_buffer & align_mask) |
| 36 | return 0; |
| 37 | |
| 38 | if (state->len != state->len_aligned) |
| 39 | return 0; |
| 40 | |
| 41 | return 1; |
| 42 | } |
| 43 | |
Andrew Scull | 83d15d3 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 44 | static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i, |
| 45 | struct virtio_sg *sg, u16 flags) |
| 46 | { |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 47 | struct vring_desc_shadow *desc_shadow = &vq->vring_desc_shadow[i]; |
Andrew Scull | 83d15d3 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 48 | struct vring_desc *desc = &vq->vring.desc[i]; |
Will Deacon | 21da252 | 2023-03-29 22:25:00 +0800 | [diff] [blame] | 49 | void *addr; |
| 50 | |
| 51 | if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && vq->vring.bouncebufs) { |
| 52 | struct bounce_buffer *bb = &vq->vring.bouncebufs[i]; |
| 53 | unsigned int bbflags; |
| 54 | int ret; |
| 55 | |
| 56 | if (flags & VRING_DESC_F_WRITE) |
| 57 | bbflags = GEN_BB_WRITE; |
| 58 | else |
| 59 | bbflags = GEN_BB_READ; |
| 60 | |
| 61 | ret = bounce_buffer_start_extalign(bb, sg->addr, sg->length, |
| 62 | bbflags, PAGE_SIZE, |
| 63 | __bb_force_page_align); |
| 64 | if (ret) { |
| 65 | debug("%s: failed to allocate bounce buffer (length 0x%zx)\n", |
| 66 | vq->vdev->name, sg->length); |
| 67 | } |
| 68 | |
| 69 | addr = bb->bounce_buffer; |
| 70 | } else { |
| 71 | addr = sg->addr; |
| 72 | } |
Andrew Scull | 83d15d3 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 73 | |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 74 | /* Update the shadow descriptor. */ |
Will Deacon | 21da252 | 2023-03-29 22:25:00 +0800 | [diff] [blame] | 75 | desc_shadow->addr = (u64)(uintptr_t)addr; |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 76 | desc_shadow->len = sg->length; |
| 77 | desc_shadow->flags = flags; |
Andrew Scull | 83d15d3 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 78 | |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 79 | /* Update the shared descriptor to match the shadow. */ |
| 80 | desc->addr = cpu_to_virtio64(vq->vdev, desc_shadow->addr); |
| 81 | desc->len = cpu_to_virtio32(vq->vdev, desc_shadow->len); |
| 82 | desc->flags = cpu_to_virtio16(vq->vdev, desc_shadow->flags); |
| 83 | desc->next = cpu_to_virtio16(vq->vdev, desc_shadow->next); |
| 84 | |
| 85 | return desc_shadow->next; |
Andrew Scull | 83d15d3 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Will Deacon | 63ee31d | 2023-03-29 22:24:58 +0800 | [diff] [blame] | 88 | static void virtqueue_detach_desc(struct virtqueue *vq, unsigned int idx) |
| 89 | { |
Will Deacon | 21da252 | 2023-03-29 22:25:00 +0800 | [diff] [blame] | 90 | struct vring_desc *desc = &vq->vring.desc[idx]; |
| 91 | struct bounce_buffer *bb; |
| 92 | |
| 93 | if (!IS_ENABLED(CONFIG_BOUNCE_BUFFER) || !vq->vring.bouncebufs) |
| 94 | return; |
| 95 | |
| 96 | bb = &vq->vring.bouncebufs[idx]; |
| 97 | bounce_buffer_stop(bb); |
| 98 | desc->addr = cpu_to_virtio64(vq->vdev, (u64)(uintptr_t)bb->user_buffer); |
Will Deacon | 63ee31d | 2023-03-29 22:24:58 +0800 | [diff] [blame] | 99 | } |
| 100 | |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 101 | int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[], |
| 102 | unsigned int out_sgs, unsigned int in_sgs) |
| 103 | { |
| 104 | struct vring_desc *desc; |
Andrew Scull | b3f3644 | 2022-05-16 10:41:29 +0000 | [diff] [blame] | 105 | unsigned int descs_used = out_sgs + in_sgs; |
| 106 | unsigned int i, n, avail, uninitialized_var(prev); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 107 | int head; |
| 108 | |
Andrew Scull | b3f3644 | 2022-05-16 10:41:29 +0000 | [diff] [blame] | 109 | WARN_ON(descs_used == 0); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 110 | |
| 111 | head = vq->free_head; |
| 112 | |
| 113 | desc = vq->vring.desc; |
| 114 | i = head; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 115 | |
| 116 | if (vq->num_free < descs_used) { |
| 117 | debug("Can't add buf len %i - avail = %i\n", |
| 118 | descs_used, vq->num_free); |
| 119 | /* |
| 120 | * FIXME: for historical reasons, we force a notify here if |
| 121 | * there are outgoing parts to the buffer. Presumably the |
| 122 | * host should service the ring ASAP. |
| 123 | */ |
| 124 | if (out_sgs) |
| 125 | virtio_notify(vq->vdev, vq); |
| 126 | return -ENOSPC; |
| 127 | } |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 128 | |
Andrew Scull | 83d15d3 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 129 | for (n = 0; n < descs_used; n++) { |
| 130 | u16 flags = VRING_DESC_F_NEXT; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 131 | |
Andrew Scull | 83d15d3 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 132 | if (n >= out_sgs) |
| 133 | flags |= VRING_DESC_F_WRITE; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 134 | prev = i; |
Andrew Scull | 83d15d3 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 135 | i = virtqueue_attach_desc(vq, i, sgs[n], flags); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 136 | } |
| 137 | /* Last one doesn't continue */ |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 138 | vq->vring_desc_shadow[prev].flags &= ~VRING_DESC_F_NEXT; |
| 139 | desc[prev].flags = cpu_to_virtio16(vq->vdev, vq->vring_desc_shadow[prev].flags); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 140 | |
| 141 | /* We're using some buffers from the free list. */ |
| 142 | vq->num_free -= descs_used; |
| 143 | |
| 144 | /* Update free pointer */ |
| 145 | vq->free_head = i; |
| 146 | |
Andrew Scull | c282bdb | 2022-05-16 10:41:32 +0000 | [diff] [blame] | 147 | /* Mark the descriptor as the head of a chain. */ |
| 148 | vq->vring_desc_shadow[head].chain_head = true; |
| 149 | |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 150 | /* |
| 151 | * Put entry in available array (but don't update avail->idx |
| 152 | * until they do sync). |
| 153 | */ |
| 154 | avail = vq->avail_idx_shadow & (vq->vring.num - 1); |
| 155 | vq->vring.avail->ring[avail] = cpu_to_virtio16(vq->vdev, head); |
| 156 | |
| 157 | /* |
| 158 | * Descriptors and available array need to be set before we expose the |
| 159 | * new available array entries. |
| 160 | */ |
| 161 | virtio_wmb(); |
| 162 | vq->avail_idx_shadow++; |
| 163 | vq->vring.avail->idx = cpu_to_virtio16(vq->vdev, vq->avail_idx_shadow); |
| 164 | vq->num_added++; |
| 165 | |
| 166 | /* |
| 167 | * This is very unlikely, but theoretically possible. |
| 168 | * Kick just in case. |
| 169 | */ |
| 170 | if (unlikely(vq->num_added == (1 << 16) - 1)) |
| 171 | virtqueue_kick(vq); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static bool virtqueue_kick_prepare(struct virtqueue *vq) |
| 177 | { |
| 178 | u16 new, old; |
| 179 | bool needs_kick; |
| 180 | |
| 181 | /* |
| 182 | * We need to expose available array entries before checking |
| 183 | * avail event. |
| 184 | */ |
| 185 | virtio_mb(); |
| 186 | |
| 187 | old = vq->avail_idx_shadow - vq->num_added; |
| 188 | new = vq->avail_idx_shadow; |
| 189 | vq->num_added = 0; |
| 190 | |
| 191 | if (vq->event) { |
| 192 | needs_kick = vring_need_event(virtio16_to_cpu(vq->vdev, |
| 193 | vring_avail_event(&vq->vring)), new, old); |
| 194 | } else { |
| 195 | needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(vq->vdev, |
| 196 | VRING_USED_F_NO_NOTIFY)); |
| 197 | } |
| 198 | |
| 199 | return needs_kick; |
| 200 | } |
| 201 | |
| 202 | void virtqueue_kick(struct virtqueue *vq) |
| 203 | { |
| 204 | if (virtqueue_kick_prepare(vq)) |
| 205 | virtio_notify(vq->vdev, vq); |
| 206 | } |
| 207 | |
| 208 | static void detach_buf(struct virtqueue *vq, unsigned int head) |
| 209 | { |
| 210 | unsigned int i; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 211 | |
Andrew Scull | c282bdb | 2022-05-16 10:41:32 +0000 | [diff] [blame] | 212 | /* Unmark the descriptor as the head of a chain. */ |
| 213 | vq->vring_desc_shadow[head].chain_head = false; |
| 214 | |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 215 | /* Put back on free list: unmap first-level descriptors and find end */ |
| 216 | i = head; |
| 217 | |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 218 | while (vq->vring_desc_shadow[i].flags & VRING_DESC_F_NEXT) { |
Will Deacon | 63ee31d | 2023-03-29 22:24:58 +0800 | [diff] [blame] | 219 | virtqueue_detach_desc(vq, i); |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 220 | i = vq->vring_desc_shadow[i].next; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 221 | vq->num_free++; |
| 222 | } |
| 223 | |
Will Deacon | 63ee31d | 2023-03-29 22:24:58 +0800 | [diff] [blame] | 224 | virtqueue_detach_desc(vq, i); |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 225 | vq->vring_desc_shadow[i].next = vq->free_head; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 226 | vq->free_head = head; |
| 227 | |
| 228 | /* Plus final descriptor */ |
| 229 | vq->num_free++; |
| 230 | } |
| 231 | |
| 232 | static inline bool more_used(const struct virtqueue *vq) |
| 233 | { |
| 234 | return vq->last_used_idx != virtio16_to_cpu(vq->vdev, |
| 235 | vq->vring.used->idx); |
| 236 | } |
| 237 | |
| 238 | void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len) |
| 239 | { |
| 240 | unsigned int i; |
| 241 | u16 last_used; |
| 242 | |
| 243 | if (!more_used(vq)) { |
| 244 | debug("(%s.%d): No more buffers in queue\n", |
| 245 | vq->vdev->name, vq->index); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | /* Only get used array entries after they have been exposed by host */ |
| 250 | virtio_rmb(); |
| 251 | |
| 252 | last_used = (vq->last_used_idx & (vq->vring.num - 1)); |
| 253 | i = virtio32_to_cpu(vq->vdev, vq->vring.used->ring[last_used].id); |
| 254 | if (len) { |
| 255 | *len = virtio32_to_cpu(vq->vdev, |
| 256 | vq->vring.used->ring[last_used].len); |
| 257 | debug("(%s.%d): last used idx %u with len %u\n", |
| 258 | vq->vdev->name, vq->index, i, *len); |
| 259 | } |
| 260 | |
| 261 | if (unlikely(i >= vq->vring.num)) { |
| 262 | printf("(%s.%d): id %u out of range\n", |
| 263 | vq->vdev->name, vq->index, i); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
Andrew Scull | c282bdb | 2022-05-16 10:41:32 +0000 | [diff] [blame] | 267 | if (unlikely(!vq->vring_desc_shadow[i].chain_head)) { |
| 268 | printf("(%s.%d): id %u is not a head\n", |
| 269 | vq->vdev->name, vq->index, i); |
| 270 | return NULL; |
| 271 | } |
| 272 | |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 273 | detach_buf(vq, i); |
| 274 | vq->last_used_idx++; |
| 275 | /* |
| 276 | * If we expect an interrupt for the next entry, tell host |
| 277 | * by writing event index and flush out the write before |
| 278 | * the read in the next get_buf call. |
| 279 | */ |
| 280 | if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) |
| 281 | virtio_store_mb(&vring_used_event(&vq->vring), |
| 282 | cpu_to_virtio16(vq->vdev, vq->last_used_idx)); |
| 283 | |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 284 | return (void *)(uintptr_t)vq->vring_desc_shadow[i].addr; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static struct virtqueue *__vring_new_virtqueue(unsigned int index, |
| 288 | struct vring vring, |
| 289 | struct udevice *udev) |
| 290 | { |
| 291 | unsigned int i; |
| 292 | struct virtqueue *vq; |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 293 | struct vring_desc_shadow *vring_desc_shadow; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 294 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); |
| 295 | struct udevice *vdev = uc_priv->vdev; |
| 296 | |
| 297 | vq = malloc(sizeof(*vq)); |
| 298 | if (!vq) |
| 299 | return NULL; |
| 300 | |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 301 | vring_desc_shadow = calloc(vring.num, sizeof(struct vring_desc_shadow)); |
| 302 | if (!vring_desc_shadow) { |
| 303 | free(vq); |
| 304 | return NULL; |
| 305 | } |
| 306 | |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 307 | vq->vdev = vdev; |
| 308 | vq->index = index; |
| 309 | vq->num_free = vring.num; |
| 310 | vq->vring = vring; |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 311 | vq->vring_desc_shadow = vring_desc_shadow; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 312 | vq->last_used_idx = 0; |
| 313 | vq->avail_flags_shadow = 0; |
| 314 | vq->avail_idx_shadow = 0; |
| 315 | vq->num_added = 0; |
| 316 | list_add_tail(&vq->list, &uc_priv->vqs); |
| 317 | |
| 318 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); |
| 319 | |
| 320 | /* Tell other side not to bother us */ |
| 321 | vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; |
| 322 | if (!vq->event) |
| 323 | vq->vring.avail->flags = cpu_to_virtio16(vdev, |
| 324 | vq->avail_flags_shadow); |
| 325 | |
| 326 | /* Put everything in free lists */ |
| 327 | vq->free_head = 0; |
| 328 | for (i = 0; i < vring.num - 1; i++) |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 329 | vq->vring_desc_shadow[i].next = i + 1; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 330 | |
| 331 | return vq; |
| 332 | } |
| 333 | |
| 334 | struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, |
| 335 | unsigned int vring_align, |
| 336 | struct udevice *udev) |
| 337 | { |
Will Deacon | 0edbe5c | 2023-03-29 22:24:57 +0800 | [diff] [blame] | 338 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); |
| 339 | struct udevice *vdev = uc_priv->vdev; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 340 | struct virtqueue *vq; |
| 341 | void *queue = NULL; |
Will Deacon | 1eb6e62 | 2023-03-29 22:24:59 +0800 | [diff] [blame] | 342 | struct bounce_buffer *bbs = NULL; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 343 | struct vring vring; |
| 344 | |
| 345 | /* We assume num is a power of 2 */ |
| 346 | if (num & (num - 1)) { |
| 347 | printf("Bad virtqueue length %u\n", num); |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | /* TODO: allocate each queue chunk individually */ |
| 352 | for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { |
Will Deacon | 0edbe5c | 2023-03-29 22:24:57 +0800 | [diff] [blame] | 353 | size_t sz = vring_size(num, vring_align); |
| 354 | |
| 355 | queue = virtio_alloc_pages(vdev, DIV_ROUND_UP(sz, PAGE_SIZE)); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 356 | if (queue) |
| 357 | break; |
| 358 | } |
| 359 | |
| 360 | if (!num) |
| 361 | return NULL; |
| 362 | |
| 363 | if (!queue) { |
| 364 | /* Try to get a single page. You are my only hope! */ |
Will Deacon | 0edbe5c | 2023-03-29 22:24:57 +0800 | [diff] [blame] | 365 | queue = virtio_alloc_pages(vdev, 1); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 366 | } |
| 367 | if (!queue) |
| 368 | return NULL; |
| 369 | |
| 370 | memset(queue, 0, vring_size(num, vring_align)); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 371 | |
Will Deacon | 1eb6e62 | 2023-03-29 22:24:59 +0800 | [diff] [blame] | 372 | if (virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) { |
| 373 | bbs = calloc(num, sizeof(*bbs)); |
| 374 | if (!bbs) |
| 375 | goto err_free_queue; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 376 | } |
Will Deacon | 1eb6e62 | 2023-03-29 22:24:59 +0800 | [diff] [blame] | 377 | |
| 378 | vring_init(&vring, num, queue, vring_align, bbs); |
| 379 | |
| 380 | vq = __vring_new_virtqueue(index, vring, udev); |
| 381 | if (!vq) |
| 382 | goto err_free_bbs; |
| 383 | |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 384 | debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name, |
| 385 | queue, vq, num); |
| 386 | |
| 387 | return vq; |
Will Deacon | 1eb6e62 | 2023-03-29 22:24:59 +0800 | [diff] [blame] | 388 | |
| 389 | err_free_bbs: |
| 390 | free(bbs); |
| 391 | err_free_queue: |
| 392 | virtio_free_pages(vdev, queue, DIV_ROUND_UP(vring.size, PAGE_SIZE)); |
| 393 | return NULL; |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void vring_del_virtqueue(struct virtqueue *vq) |
| 397 | { |
Will Deacon | 0edbe5c | 2023-03-29 22:24:57 +0800 | [diff] [blame] | 398 | virtio_free_pages(vq->vdev, vq->vring.desc, |
| 399 | DIV_ROUND_UP(vq->vring.size, PAGE_SIZE)); |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 400 | free(vq->vring_desc_shadow); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 401 | list_del(&vq->list); |
Will Deacon | 1eb6e62 | 2023-03-29 22:24:59 +0800 | [diff] [blame] | 402 | free(vq->vring.bouncebufs); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 403 | free(vq); |
| 404 | } |
| 405 | |
| 406 | unsigned int virtqueue_get_vring_size(struct virtqueue *vq) |
| 407 | { |
| 408 | return vq->vring.num; |
| 409 | } |
| 410 | |
| 411 | ulong virtqueue_get_desc_addr(struct virtqueue *vq) |
| 412 | { |
| 413 | return (ulong)vq->vring.desc; |
| 414 | } |
| 415 | |
| 416 | ulong virtqueue_get_avail_addr(struct virtqueue *vq) |
| 417 | { |
| 418 | return (ulong)vq->vring.desc + |
| 419 | ((char *)vq->vring.avail - (char *)vq->vring.desc); |
| 420 | } |
| 421 | |
| 422 | ulong virtqueue_get_used_addr(struct virtqueue *vq) |
| 423 | { |
| 424 | return (ulong)vq->vring.desc + |
| 425 | ((char *)vq->vring.used - (char *)vq->vring.desc); |
| 426 | } |
| 427 | |
| 428 | bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx) |
| 429 | { |
| 430 | virtio_mb(); |
| 431 | |
| 432 | return last_used_idx != virtio16_to_cpu(vq->vdev, vq->vring.used->idx); |
| 433 | } |
| 434 | |
| 435 | void virtqueue_dump(struct virtqueue *vq) |
| 436 | { |
| 437 | unsigned int i; |
| 438 | |
| 439 | printf("virtqueue %p for dev %s:\n", vq, vq->vdev->name); |
| 440 | printf("\tindex %u, phys addr %p num %u\n", |
| 441 | vq->index, vq->vring.desc, vq->vring.num); |
| 442 | printf("\tfree_head %u, num_added %u, num_free %u\n", |
| 443 | vq->free_head, vq->num_added, vq->num_free); |
| 444 | printf("\tlast_used_idx %u, avail_flags_shadow %u, avail_idx_shadow %u\n", |
| 445 | vq->last_used_idx, vq->avail_flags_shadow, vq->avail_idx_shadow); |
| 446 | |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 447 | printf("Shadow descriptor dump:\n"); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 448 | for (i = 0; i < vq->vring.num; i++) { |
Andrew Scull | 594d2a6 | 2022-05-16 10:41:31 +0000 | [diff] [blame] | 449 | struct vring_desc_shadow *desc = &vq->vring_desc_shadow[i]; |
| 450 | |
| 451 | printf("\tdesc_shadow[%u] = { 0x%llx, len %u, flags %u, next %u }\n", |
| 452 | i, desc->addr, desc->len, desc->flags, desc->next); |
Tuomas Tynkkynen | d58269f | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | printf("Avail ring dump:\n"); |
| 456 | printf("\tflags %u, idx %u\n", |
| 457 | vq->vring.avail->flags, vq->vring.avail->idx); |
| 458 | for (i = 0; i < vq->vring.num; i++) { |
| 459 | printf("\tavail[%u] = %u\n", |
| 460 | i, vq->vring.avail->ring[i]); |
| 461 | } |
| 462 | |
| 463 | printf("Used ring dump:\n"); |
| 464 | printf("\tflags %u, idx %u\n", |
| 465 | vq->vring.used->flags, vq->vring.used->idx); |
| 466 | for (i = 0; i < vq->vring.num; i++) { |
| 467 | printf("\tused[%u] = { %u, %u }\n", i, |
| 468 | vq->vring.used->ring[i].id, vq->vring.used->ring[i].len); |
| 469 | } |
| 470 | } |