Tuomas Tynkkynen | c011641 | 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 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <virtio_types.h> |
| 14 | #include <virtio.h> |
| 15 | #include <virtio_ring.h> |
Simon Glass | eb41d8a | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 16 | #include <linux/bug.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <linux/compat.h> |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 18 | |
Andrew Scull | b095297 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 19 | static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i, |
| 20 | struct virtio_sg *sg, u16 flags) |
| 21 | { |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 22 | struct vring_desc_shadow *desc_shadow = &vq->vring_desc_shadow[i]; |
Andrew Scull | b095297 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 23 | struct vring_desc *desc = &vq->vring.desc[i]; |
| 24 | |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 25 | /* Update the shadow descriptor. */ |
| 26 | desc_shadow->addr = (u64)(uintptr_t)sg->addr; |
| 27 | desc_shadow->len = sg->length; |
| 28 | desc_shadow->flags = flags; |
Andrew Scull | b095297 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 29 | |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 30 | /* Update the shared descriptor to match the shadow. */ |
| 31 | desc->addr = cpu_to_virtio64(vq->vdev, desc_shadow->addr); |
| 32 | desc->len = cpu_to_virtio32(vq->vdev, desc_shadow->len); |
| 33 | desc->flags = cpu_to_virtio16(vq->vdev, desc_shadow->flags); |
| 34 | desc->next = cpu_to_virtio16(vq->vdev, desc_shadow->next); |
| 35 | |
| 36 | return desc_shadow->next; |
Andrew Scull | b095297 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 39 | int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[], |
| 40 | unsigned int out_sgs, unsigned int in_sgs) |
| 41 | { |
| 42 | struct vring_desc *desc; |
Andrew Scull | 68f8bf2 | 2022-05-16 10:41:29 +0000 | [diff] [blame] | 43 | unsigned int descs_used = out_sgs + in_sgs; |
| 44 | unsigned int i, n, avail, uninitialized_var(prev); |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 45 | int head; |
| 46 | |
Andrew Scull | 68f8bf2 | 2022-05-16 10:41:29 +0000 | [diff] [blame] | 47 | WARN_ON(descs_used == 0); |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 48 | |
| 49 | head = vq->free_head; |
| 50 | |
| 51 | desc = vq->vring.desc; |
| 52 | i = head; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 53 | |
| 54 | if (vq->num_free < descs_used) { |
| 55 | debug("Can't add buf len %i - avail = %i\n", |
| 56 | descs_used, vq->num_free); |
| 57 | /* |
| 58 | * FIXME: for historical reasons, we force a notify here if |
| 59 | * there are outgoing parts to the buffer. Presumably the |
| 60 | * host should service the ring ASAP. |
| 61 | */ |
| 62 | if (out_sgs) |
| 63 | virtio_notify(vq->vdev, vq); |
| 64 | return -ENOSPC; |
| 65 | } |
| 66 | |
Andrew Scull | b095297 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 67 | for (n = 0; n < descs_used; n++) { |
| 68 | u16 flags = VRING_DESC_F_NEXT; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 69 | |
Andrew Scull | b095297 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 70 | if (n >= out_sgs) |
| 71 | flags |= VRING_DESC_F_WRITE; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 72 | prev = i; |
Andrew Scull | b095297 | 2022-05-16 10:41:30 +0000 | [diff] [blame] | 73 | i = virtqueue_attach_desc(vq, i, sgs[n], flags); |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 74 | } |
| 75 | /* Last one doesn't continue */ |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 76 | vq->vring_desc_shadow[prev].flags &= ~VRING_DESC_F_NEXT; |
| 77 | desc[prev].flags = cpu_to_virtio16(vq->vdev, vq->vring_desc_shadow[prev].flags); |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 78 | |
| 79 | /* We're using some buffers from the free list. */ |
| 80 | vq->num_free -= descs_used; |
| 81 | |
| 82 | /* Update free pointer */ |
| 83 | vq->free_head = i; |
| 84 | |
| 85 | /* |
| 86 | * Put entry in available array (but don't update avail->idx |
| 87 | * until they do sync). |
| 88 | */ |
| 89 | avail = vq->avail_idx_shadow & (vq->vring.num - 1); |
| 90 | vq->vring.avail->ring[avail] = cpu_to_virtio16(vq->vdev, head); |
| 91 | |
| 92 | /* |
| 93 | * Descriptors and available array need to be set before we expose the |
| 94 | * new available array entries. |
| 95 | */ |
| 96 | virtio_wmb(); |
| 97 | vq->avail_idx_shadow++; |
| 98 | vq->vring.avail->idx = cpu_to_virtio16(vq->vdev, vq->avail_idx_shadow); |
| 99 | vq->num_added++; |
| 100 | |
| 101 | /* |
| 102 | * This is very unlikely, but theoretically possible. |
| 103 | * Kick just in case. |
| 104 | */ |
| 105 | if (unlikely(vq->num_added == (1 << 16) - 1)) |
| 106 | virtqueue_kick(vq); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static bool virtqueue_kick_prepare(struct virtqueue *vq) |
| 112 | { |
| 113 | u16 new, old; |
| 114 | bool needs_kick; |
| 115 | |
| 116 | /* |
| 117 | * We need to expose available array entries before checking |
| 118 | * avail event. |
| 119 | */ |
| 120 | virtio_mb(); |
| 121 | |
| 122 | old = vq->avail_idx_shadow - vq->num_added; |
| 123 | new = vq->avail_idx_shadow; |
| 124 | vq->num_added = 0; |
| 125 | |
| 126 | if (vq->event) { |
| 127 | needs_kick = vring_need_event(virtio16_to_cpu(vq->vdev, |
| 128 | vring_avail_event(&vq->vring)), new, old); |
| 129 | } else { |
| 130 | needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(vq->vdev, |
| 131 | VRING_USED_F_NO_NOTIFY)); |
| 132 | } |
| 133 | |
| 134 | return needs_kick; |
| 135 | } |
| 136 | |
| 137 | void virtqueue_kick(struct virtqueue *vq) |
| 138 | { |
| 139 | if (virtqueue_kick_prepare(vq)) |
| 140 | virtio_notify(vq->vdev, vq); |
| 141 | } |
| 142 | |
| 143 | static void detach_buf(struct virtqueue *vq, unsigned int head) |
| 144 | { |
| 145 | unsigned int i; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 146 | |
| 147 | /* Put back on free list: unmap first-level descriptors and find end */ |
| 148 | i = head; |
| 149 | |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 150 | while (vq->vring_desc_shadow[i].flags & VRING_DESC_F_NEXT) { |
| 151 | i = vq->vring_desc_shadow[i].next; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 152 | vq->num_free++; |
| 153 | } |
| 154 | |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 155 | vq->vring_desc_shadow[i].next = vq->free_head; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 156 | vq->free_head = head; |
| 157 | |
| 158 | /* Plus final descriptor */ |
| 159 | vq->num_free++; |
| 160 | } |
| 161 | |
| 162 | static inline bool more_used(const struct virtqueue *vq) |
| 163 | { |
| 164 | return vq->last_used_idx != virtio16_to_cpu(vq->vdev, |
| 165 | vq->vring.used->idx); |
| 166 | } |
| 167 | |
| 168 | void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len) |
| 169 | { |
| 170 | unsigned int i; |
| 171 | u16 last_used; |
| 172 | |
| 173 | if (!more_used(vq)) { |
| 174 | debug("(%s.%d): No more buffers in queue\n", |
| 175 | vq->vdev->name, vq->index); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | /* Only get used array entries after they have been exposed by host */ |
| 180 | virtio_rmb(); |
| 181 | |
| 182 | last_used = (vq->last_used_idx & (vq->vring.num - 1)); |
| 183 | i = virtio32_to_cpu(vq->vdev, vq->vring.used->ring[last_used].id); |
| 184 | if (len) { |
| 185 | *len = virtio32_to_cpu(vq->vdev, |
| 186 | vq->vring.used->ring[last_used].len); |
| 187 | debug("(%s.%d): last used idx %u with len %u\n", |
| 188 | vq->vdev->name, vq->index, i, *len); |
| 189 | } |
| 190 | |
| 191 | if (unlikely(i >= vq->vring.num)) { |
| 192 | printf("(%s.%d): id %u out of range\n", |
| 193 | vq->vdev->name, vq->index, i); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | detach_buf(vq, i); |
| 198 | vq->last_used_idx++; |
| 199 | /* |
| 200 | * If we expect an interrupt for the next entry, tell host |
| 201 | * by writing event index and flush out the write before |
| 202 | * the read in the next get_buf call. |
| 203 | */ |
| 204 | if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) |
| 205 | virtio_store_mb(&vring_used_event(&vq->vring), |
| 206 | cpu_to_virtio16(vq->vdev, vq->last_used_idx)); |
| 207 | |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 208 | return (void *)(uintptr_t)vq->vring_desc_shadow[i].addr; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static struct virtqueue *__vring_new_virtqueue(unsigned int index, |
| 212 | struct vring vring, |
| 213 | struct udevice *udev) |
| 214 | { |
| 215 | unsigned int i; |
| 216 | struct virtqueue *vq; |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 217 | struct vring_desc_shadow *vring_desc_shadow; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 218 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); |
| 219 | struct udevice *vdev = uc_priv->vdev; |
| 220 | |
| 221 | vq = malloc(sizeof(*vq)); |
| 222 | if (!vq) |
| 223 | return NULL; |
| 224 | |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 225 | vring_desc_shadow = calloc(vring.num, sizeof(struct vring_desc_shadow)); |
| 226 | if (!vring_desc_shadow) { |
| 227 | free(vq); |
| 228 | return NULL; |
| 229 | } |
| 230 | |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 231 | vq->vdev = vdev; |
| 232 | vq->index = index; |
| 233 | vq->num_free = vring.num; |
| 234 | vq->vring = vring; |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 235 | vq->vring_desc_shadow = vring_desc_shadow; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 236 | vq->last_used_idx = 0; |
| 237 | vq->avail_flags_shadow = 0; |
| 238 | vq->avail_idx_shadow = 0; |
| 239 | vq->num_added = 0; |
| 240 | list_add_tail(&vq->list, &uc_priv->vqs); |
| 241 | |
| 242 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); |
| 243 | |
| 244 | /* Tell other side not to bother us */ |
| 245 | vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; |
| 246 | if (!vq->event) |
| 247 | vq->vring.avail->flags = cpu_to_virtio16(vdev, |
| 248 | vq->avail_flags_shadow); |
| 249 | |
| 250 | /* Put everything in free lists */ |
| 251 | vq->free_head = 0; |
| 252 | for (i = 0; i < vring.num - 1; i++) |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 253 | vq->vring_desc_shadow[i].next = i + 1; |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 254 | |
| 255 | return vq; |
| 256 | } |
| 257 | |
| 258 | struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, |
| 259 | unsigned int vring_align, |
| 260 | struct udevice *udev) |
| 261 | { |
| 262 | struct virtqueue *vq; |
| 263 | void *queue = NULL; |
| 264 | struct vring vring; |
| 265 | |
| 266 | /* We assume num is a power of 2 */ |
| 267 | if (num & (num - 1)) { |
| 268 | printf("Bad virtqueue length %u\n", num); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | /* TODO: allocate each queue chunk individually */ |
| 273 | for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { |
| 274 | queue = memalign(PAGE_SIZE, vring_size(num, vring_align)); |
| 275 | if (queue) |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | if (!num) |
| 280 | return NULL; |
| 281 | |
| 282 | if (!queue) { |
| 283 | /* Try to get a single page. You are my only hope! */ |
| 284 | queue = memalign(PAGE_SIZE, vring_size(num, vring_align)); |
| 285 | } |
| 286 | if (!queue) |
| 287 | return NULL; |
| 288 | |
| 289 | memset(queue, 0, vring_size(num, vring_align)); |
| 290 | vring_init(&vring, num, queue, vring_align); |
| 291 | |
| 292 | vq = __vring_new_virtqueue(index, vring, udev); |
| 293 | if (!vq) { |
| 294 | free(queue); |
| 295 | return NULL; |
| 296 | } |
| 297 | debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name, |
| 298 | queue, vq, num); |
| 299 | |
| 300 | return vq; |
| 301 | } |
| 302 | |
| 303 | void vring_del_virtqueue(struct virtqueue *vq) |
| 304 | { |
| 305 | free(vq->vring.desc); |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 306 | free(vq->vring_desc_shadow); |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 307 | list_del(&vq->list); |
| 308 | free(vq); |
| 309 | } |
| 310 | |
| 311 | unsigned int virtqueue_get_vring_size(struct virtqueue *vq) |
| 312 | { |
| 313 | return vq->vring.num; |
| 314 | } |
| 315 | |
| 316 | ulong virtqueue_get_desc_addr(struct virtqueue *vq) |
| 317 | { |
| 318 | return (ulong)vq->vring.desc; |
| 319 | } |
| 320 | |
| 321 | ulong virtqueue_get_avail_addr(struct virtqueue *vq) |
| 322 | { |
| 323 | return (ulong)vq->vring.desc + |
| 324 | ((char *)vq->vring.avail - (char *)vq->vring.desc); |
| 325 | } |
| 326 | |
| 327 | ulong virtqueue_get_used_addr(struct virtqueue *vq) |
| 328 | { |
| 329 | return (ulong)vq->vring.desc + |
| 330 | ((char *)vq->vring.used - (char *)vq->vring.desc); |
| 331 | } |
| 332 | |
| 333 | bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx) |
| 334 | { |
| 335 | virtio_mb(); |
| 336 | |
| 337 | return last_used_idx != virtio16_to_cpu(vq->vdev, vq->vring.used->idx); |
| 338 | } |
| 339 | |
| 340 | void virtqueue_dump(struct virtqueue *vq) |
| 341 | { |
| 342 | unsigned int i; |
| 343 | |
| 344 | printf("virtqueue %p for dev %s:\n", vq, vq->vdev->name); |
| 345 | printf("\tindex %u, phys addr %p num %u\n", |
| 346 | vq->index, vq->vring.desc, vq->vring.num); |
| 347 | printf("\tfree_head %u, num_added %u, num_free %u\n", |
| 348 | vq->free_head, vq->num_added, vq->num_free); |
| 349 | printf("\tlast_used_idx %u, avail_flags_shadow %u, avail_idx_shadow %u\n", |
| 350 | vq->last_used_idx, vq->avail_flags_shadow, vq->avail_idx_shadow); |
| 351 | |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 352 | printf("Shadow descriptor dump:\n"); |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 353 | for (i = 0; i < vq->vring.num; i++) { |
Andrew Scull | 10a1453 | 2022-05-16 10:41:31 +0000 | [diff] [blame^] | 354 | struct vring_desc_shadow *desc = &vq->vring_desc_shadow[i]; |
| 355 | |
| 356 | printf("\tdesc_shadow[%u] = { 0x%llx, len %u, flags %u, next %u }\n", |
| 357 | i, desc->addr, desc->len, desc->flags, desc->next); |
Tuomas Tynkkynen | c011641 | 2018-10-15 02:21:01 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | printf("Avail ring dump:\n"); |
| 361 | printf("\tflags %u, idx %u\n", |
| 362 | vq->vring.avail->flags, vq->vring.avail->idx); |
| 363 | for (i = 0; i < vq->vring.num; i++) { |
| 364 | printf("\tavail[%u] = %u\n", |
| 365 | i, vq->vring.avail->ring[i]); |
| 366 | } |
| 367 | |
| 368 | printf("Used ring dump:\n"); |
| 369 | printf("\tflags %u, idx %u\n", |
| 370 | vq->vring.used->flags, vq->vring.used->idx); |
| 371 | for (i = 0; i < vq->vring.num; i++) { |
| 372 | printf("\tused[%u] = { %u, %u }\n", i, |
| 373 | vq->vring.used->ring[i].id, vq->vring.used->ring[i].len); |
| 374 | } |
| 375 | } |