[2d4faf7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file VIRTIO support
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | #include "virtio-pci.h"
|
---|
| 33 |
|
---|
| 34 | #include <as.h>
|
---|
| 35 | #include <align.h>
|
---|
| 36 | #include <macros.h>
|
---|
| 37 |
|
---|
| 38 | #include <ddf/log.h>
|
---|
[05882233] | 39 | #include <barrier.h>
|
---|
[c1ba24a] | 40 |
|
---|
[1e472ee] | 41 | /** Allocate DMA buffers
|
---|
| 42 | *
|
---|
| 43 | * @param buffers[in] Number of buffers to allocate.
|
---|
| 44 | * @param size[in] Size of each buffer.
|
---|
| 45 | * @param write[in] True if the buffers are writable by the driver, false
|
---|
| 46 | * otherwise.
|
---|
| 47 | * @param buf[out] Output array holding virtual addresses of the allocated
|
---|
| 48 | * buffers.
|
---|
| 49 | * @param buf_p[out] Output array holding physical addresses of the allocated
|
---|
| 50 | * buffers.
|
---|
| 51 | *
|
---|
[d4ceba0] | 52 | * The buffers can be deallocated by virtio_teardown_dma_bufs().
|
---|
[1e472ee] | 53 | *
|
---|
| 54 | * @return EOK on success or error code.
|
---|
| 55 | */
|
---|
| 56 | errno_t virtio_setup_dma_bufs(unsigned int buffers, size_t size,
|
---|
| 57 | bool write, void *buf[], uintptr_t buf_p[])
|
---|
| 58 | {
|
---|
| 59 | /*
|
---|
| 60 | * Allocate all buffers at once in one large chunk.
|
---|
| 61 | */
|
---|
| 62 | void *virt = AS_AREA_ANY;
|
---|
| 63 | uintptr_t phys;
|
---|
| 64 | errno_t rc = dmamem_map_anonymous(buffers * size, 0,
|
---|
[13eecc4] | 65 | write ? AS_AREA_WRITE | AS_AREA_READ : AS_AREA_READ, 0, &phys,
|
---|
| 66 | &virt);
|
---|
[1e472ee] | 67 | if (rc != EOK)
|
---|
| 68 | return rc;
|
---|
| 69 |
|
---|
| 70 | ddf_msg(LVL_NOTE, "DMA buffers: %p-%p", virt, virt + buffers * size);
|
---|
| 71 |
|
---|
| 72 | /*
|
---|
| 73 | * Calculate addresses of the individual buffers for easy access.
|
---|
| 74 | */
|
---|
| 75 | for (unsigned i = 0; i < buffers; i++) {
|
---|
| 76 | buf[i] = virt + i * size;
|
---|
| 77 | buf_p[i] = phys + i * size;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | return EOK;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Deallocate DMA buffers
|
---|
| 84 | *
|
---|
| 85 | * @param buf[in] Array holding the virtual addresses of the DMA buffers
|
---|
[d4ceba0] | 86 | * previously allocated by virtio_setup_dma_bufs().
|
---|
[1e472ee] | 87 | */
|
---|
[8d2289c] | 88 | void virtio_teardown_dma_bufs(void *buf[])
|
---|
[1e472ee] | 89 | {
|
---|
| 90 | if (buf[0]) {
|
---|
| 91 | dmamem_unmap_anonymous(buf[0]);
|
---|
| 92 | buf[0] = NULL;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[3d135e9] | 96 | void virtio_virtq_desc_set(virtio_dev_t *vdev, uint16_t num, uint16_t descno,
|
---|
[c1ba24a] | 97 | uint64_t addr, uint32_t len, uint16_t flags, uint16_t next)
|
---|
| 98 | {
|
---|
| 99 | virtq_desc_t *d = &vdev->queues[num].desc[descno];
|
---|
[9af56b6] | 100 | pio_write_le64(&d->addr, addr);
|
---|
| 101 | pio_write_le32(&d->len, len);
|
---|
| 102 | pio_write_le16(&d->flags, flags);
|
---|
| 103 | pio_write_le16(&d->next, next);
|
---|
[c1ba24a] | 104 | }
|
---|
| 105 |
|
---|
[3d135e9] | 106 | uint16_t virtio_virtq_desc_get_next(virtio_dev_t *vdev, uint16_t num,
|
---|
| 107 | uint16_t descno)
|
---|
| 108 | {
|
---|
| 109 | virtq_desc_t *d = &vdev->queues[num].desc[descno];
|
---|
| 110 | if (!(pio_read_le16(&d->flags) & VIRTQ_DESC_F_NEXT))
|
---|
| 111 | return (uint16_t) -1U;
|
---|
| 112 | return pio_read_le16(&d->next);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[331d024] | 115 | /** Create free descriptor list from the unused VIRTIO descriptors
|
---|
| 116 | *
|
---|
| 117 | * @param vdev[in] VIRTIO device for which the free list will be created.
|
---|
| 118 | * @param num[in] Index of the virtqueue for which the free list will be
|
---|
| 119 | * created.
|
---|
| 120 | * @param size[in] Number of descriptors on the free list. The free list will
|
---|
| 121 | * contain descriptors starting from 0 to \a size - 1.
|
---|
| 122 | * @param head[out] Variable that will hold the VIRTIO descriptor at the head
|
---|
| 123 | * of the free list.
|
---|
| 124 | */
|
---|
| 125 | void virtio_create_desc_free_list(virtio_dev_t *vdev, uint16_t num,
|
---|
| 126 | uint16_t size, uint16_t *head)
|
---|
| 127 | {
|
---|
| 128 | for (unsigned i = 0; i < size; i++) {
|
---|
| 129 | virtio_virtq_desc_set(vdev, num, i, 0, 0,
|
---|
[8591b31] | 130 | VIRTQ_DESC_F_NEXT, (i + 1 == size) ? 0xffffu : i + 1);
|
---|
[331d024] | 131 | }
|
---|
| 132 | *head = 0;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /** Allocate a descriptor from the free list
|
---|
| 136 | *
|
---|
| 137 | * @param vdev[in] VIRTIO device with the free list.
|
---|
| 138 | * @param num[in] Index of the virtqueue with free list.
|
---|
| 139 | * @param head[in,out] Head of the free list.
|
---|
| 140 | *
|
---|
| 141 | * @return Allocated descriptor or 0xFFFF if the list is empty.
|
---|
| 142 | */
|
---|
| 143 | uint16_t virtio_alloc_desc(virtio_dev_t *vdev, uint16_t num, uint16_t *head)
|
---|
| 144 | {
|
---|
| 145 | virtq_t *q = &vdev->queues[num];
|
---|
| 146 | fibril_mutex_lock(&q->lock);
|
---|
| 147 | uint16_t descno = *head;
|
---|
| 148 | if (descno != (uint16_t) -1U)
|
---|
| 149 | *head = virtio_virtq_desc_get_next(vdev, num, descno);
|
---|
| 150 | fibril_mutex_unlock(&q->lock);
|
---|
| 151 | return descno;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Free a descriptor into the free list
|
---|
| 155 | *
|
---|
| 156 | * @param vdev[in] VIRTIO device with the free list.
|
---|
| 157 | * @param num[in] Index of the virtqueue with free list.
|
---|
| 158 | * @param head[in,out] Head of the free list.
|
---|
| 159 | * @param descno[in] The freed descriptor.
|
---|
| 160 | */
|
---|
| 161 | void virtio_free_desc(virtio_dev_t *vdev, uint16_t num, uint16_t *head,
|
---|
| 162 | uint16_t descno)
|
---|
| 163 | {
|
---|
| 164 | virtq_t *q = &vdev->queues[num];
|
---|
| 165 | fibril_mutex_lock(&q->lock);
|
---|
| 166 | virtio_virtq_desc_set(vdev, num, descno, 0, 0, VIRTQ_DESC_F_NEXT,
|
---|
| 167 | *head);
|
---|
| 168 | *head = descno;
|
---|
| 169 | fibril_mutex_unlock(&q->lock);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[c1ba24a] | 172 | void virtio_virtq_produce_available(virtio_dev_t *vdev, uint16_t num,
|
---|
| 173 | uint16_t descno)
|
---|
| 174 | {
|
---|
| 175 | virtq_t *q = &vdev->queues[num];
|
---|
| 176 |
|
---|
[417aaafb] | 177 | fibril_mutex_lock(&q->lock);
|
---|
[9af56b6] | 178 | uint16_t idx = pio_read_le16(&q->avail->idx);
|
---|
[9afd2a8] | 179 | pio_write_le16(&q->avail->ring[idx % q->queue_size], descno);
|
---|
[c1ba24a] | 180 | write_barrier();
|
---|
[9afd2a8] | 181 | pio_write_le16(&q->avail->idx, idx + 1);
|
---|
[c1ba24a] | 182 | write_barrier();
|
---|
[9af56b6] | 183 | pio_write_le16(q->notify, num);
|
---|
[417aaafb] | 184 | fibril_mutex_unlock(&q->lock);
|
---|
[c1ba24a] | 185 | }
|
---|
[2d4faf7] | 186 |
|
---|
[b8ef198b] | 187 | bool virtio_virtq_consume_used(virtio_dev_t *vdev, uint16_t num,
|
---|
| 188 | uint16_t *descno, uint32_t *len)
|
---|
| 189 | {
|
---|
| 190 | virtq_t *q = &vdev->queues[num];
|
---|
| 191 |
|
---|
[417aaafb] | 192 | fibril_mutex_lock(&q->lock);
|
---|
[b8ef198b] | 193 | uint16_t last_idx = q->used_last_idx % q->queue_size;
|
---|
[417aaafb] | 194 | if (last_idx == (pio_read_le16(&q->used->idx) % q->queue_size)) {
|
---|
| 195 | fibril_mutex_unlock(&q->lock);
|
---|
[b8ef198b] | 196 | return false;
|
---|
[417aaafb] | 197 | }
|
---|
[b8ef198b] | 198 |
|
---|
| 199 | *descno = (uint16_t) pio_read_le32(&q->used->ring[last_idx].id);
|
---|
| 200 | *len = pio_read_le32(&q->used->ring[last_idx].len);
|
---|
| 201 |
|
---|
| 202 | q->used_last_idx++;
|
---|
[417aaafb] | 203 | fibril_mutex_unlock(&q->lock);
|
---|
[b8ef198b] | 204 |
|
---|
| 205 | return true;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[0180c67] | 208 | errno_t virtio_virtq_setup(virtio_dev_t *vdev, uint16_t num, uint16_t size)
|
---|
[2d4faf7] | 209 | {
|
---|
| 210 | virtq_t *q = &vdev->queues[num];
|
---|
| 211 | virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
|
---|
| 212 |
|
---|
| 213 | /* Program the queue of our interest */
|
---|
[9af56b6] | 214 | pio_write_le16(&cfg->queue_select, num);
|
---|
[2d4faf7] | 215 |
|
---|
| 216 | /* Trim the size of the queue as needed */
|
---|
[ee0c03a] | 217 | if (size > pio_read_16(&cfg->queue_size)) {
|
---|
| 218 | ddf_msg(LVL_ERROR, "Virtq %u: not enough descriptors", num);
|
---|
| 219 | return ENOMEM;
|
---|
| 220 | }
|
---|
[9af56b6] | 221 | pio_write_le16(&cfg->queue_size, size);
|
---|
[ee0c03a] | 222 | ddf_msg(LVL_NOTE, "Virtq %u: %u descriptors", num, (unsigned) size);
|
---|
[2d4faf7] | 223 |
|
---|
| 224 | size_t avail_offset = 0;
|
---|
| 225 | size_t used_offset = 0;
|
---|
| 226 |
|
---|
| 227 | /*
|
---|
| 228 | * Compute the size of the needed DMA memory and also the offsets of
|
---|
| 229 | * the individual components
|
---|
| 230 | */
|
---|
| 231 | size_t mem_size = sizeof(virtq_desc_t[size]);
|
---|
| 232 | mem_size = ALIGN_UP(mem_size, _Alignof(virtq_avail_t));
|
---|
| 233 | avail_offset = mem_size;
|
---|
| 234 | mem_size += sizeof(virtq_avail_t) + sizeof(ioport16_t[size]) +
|
---|
| 235 | sizeof(ioport16_t);
|
---|
| 236 | mem_size = ALIGN_UP(mem_size, _Alignof(virtq_used_t));
|
---|
| 237 | used_offset = mem_size;
|
---|
| 238 | mem_size += sizeof(virtq_used_t) + sizeof(virtq_used_elem_t[size]) +
|
---|
| 239 | sizeof(ioport16_t);
|
---|
| 240 |
|
---|
| 241 | /*
|
---|
[0180c67] | 242 | * Allocate DMA memory for the virtqueues
|
---|
[2d4faf7] | 243 | */
|
---|
| 244 | q->virt = AS_AREA_ANY;
|
---|
[5b5c286] | 245 | errno_t rc = dmamem_map_anonymous(mem_size, 0,
|
---|
[2d4faf7] | 246 | AS_AREA_READ | AS_AREA_WRITE, 0, &q->phys, &q->virt);
|
---|
| 247 | if (rc != EOK) {
|
---|
| 248 | q->virt = NULL;
|
---|
| 249 | return rc;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[417aaafb] | 252 | fibril_mutex_initialize(&q->lock);
|
---|
| 253 |
|
---|
[2d4faf7] | 254 | q->size = mem_size;
|
---|
| 255 | q->queue_size = size;
|
---|
| 256 | q->desc = q->virt;
|
---|
| 257 | q->avail = q->virt + avail_offset;
|
---|
| 258 | q->used = q->virt + used_offset;
|
---|
[b8ef198b] | 259 | q->used_last_idx = 0;
|
---|
[2d4faf7] | 260 |
|
---|
| 261 | memset(q->virt, 0, q->size);
|
---|
| 262 |
|
---|
| 263 | /*
|
---|
| 264 | * Write the configured addresses to device's common config
|
---|
| 265 | */
|
---|
[9af56b6] | 266 | pio_write_le64(&cfg->queue_desc, q->phys);
|
---|
| 267 | pio_write_le64(&cfg->queue_avail, q->phys + avail_offset);
|
---|
| 268 | pio_write_le64(&cfg->queue_used, q->phys + used_offset);
|
---|
[2d4faf7] | 269 |
|
---|
| 270 | ddf_msg(LVL_NOTE, "DMA memory for virtq %d: virt=%p, phys=%p, size=%zu",
|
---|
| 271 | num, q->virt, (void *) q->phys, q->size);
|
---|
| 272 |
|
---|
[eda41a9e] | 273 | /* Determine virtq's notification address */
|
---|
| 274 | q->notify = vdev->notify_base +
|
---|
[9af56b6] | 275 | pio_read_le16(&cfg->queue_notif_off) * vdev->notify_off_multiplier;
|
---|
[eda41a9e] | 276 |
|
---|
| 277 | ddf_msg(LVL_NOTE, "notification register: %p", q->notify);
|
---|
| 278 |
|
---|
[cede6f8] | 279 | /* Enable the queue */
|
---|
| 280 | pio_write_le16(&cfg->queue_enable, 1);
|
---|
| 281 | ddf_msg(LVL_NOTE, "virtq %d set", num);
|
---|
| 282 |
|
---|
[2d4faf7] | 283 | return rc;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | void virtio_virtq_teardown(virtio_dev_t *vdev, uint16_t num)
|
---|
| 287 | {
|
---|
[cede6f8] | 288 | virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
|
---|
| 289 |
|
---|
| 290 | /* Disable the queue */
|
---|
| 291 | pio_write_le16(&cfg->queue_enable, 0);
|
---|
| 292 |
|
---|
[2d4faf7] | 293 | virtq_t *q = &vdev->queues[num];
|
---|
| 294 | if (q->size)
|
---|
| 295 | dmamem_unmap_anonymous(q->virt);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | /**
|
---|
| 299 | * Perform device initialization as described in section 3.1.1 of the
|
---|
| 300 | * specification, steps 1 - 6.
|
---|
| 301 | */
|
---|
| 302 | errno_t virtio_device_setup_start(virtio_dev_t *vdev, uint32_t features)
|
---|
| 303 | {
|
---|
| 304 | virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
|
---|
| 305 |
|
---|
| 306 | /* 1. Reset the device */
|
---|
| 307 | uint8_t status = VIRTIO_DEV_STATUS_RESET;
|
---|
| 308 | pio_write_8(&cfg->device_status, status);
|
---|
| 309 |
|
---|
| 310 | /* 2. Acknowledge we found the device */
|
---|
| 311 | status |= VIRTIO_DEV_STATUS_ACKNOWLEDGE;
|
---|
| 312 | pio_write_8(&cfg->device_status, status);
|
---|
| 313 |
|
---|
| 314 | /* 3. We know how to drive the device */
|
---|
| 315 | status |= VIRTIO_DEV_STATUS_DRIVER;
|
---|
| 316 | pio_write_8(&cfg->device_status, status);
|
---|
| 317 |
|
---|
| 318 | /* 4. Read the offered feature flags */
|
---|
[9af56b6] | 319 | pio_write_le32(&cfg->device_feature_select, VIRTIO_FEATURES_0_31);
|
---|
| 320 | uint32_t device_features = pio_read_le32(&cfg->device_feature);
|
---|
[2d4faf7] | 321 |
|
---|
[d80fa05] | 322 | uint32_t reserved_features = VIRTIO_F_VERSION_1;
|
---|
| 323 | pio_write_le32(&cfg->device_feature_select, VIRTIO_FEATURES_32_63);
|
---|
| 324 | uint32_t device_reserved_features = pio_read_le32(&cfg->device_feature);
|
---|
| 325 |
|
---|
| 326 | ddf_msg(LVL_NOTE, "offered features %x, reserved features %x",
|
---|
| 327 | device_features, device_reserved_features);
|
---|
[2d4faf7] | 328 |
|
---|
[3f1d4d5] | 329 | if (features != (features & device_features))
|
---|
[2d4faf7] | 330 | return ENOTSUP;
|
---|
[3f1d4d5] | 331 | features &= device_features;
|
---|
[2d4faf7] | 332 |
|
---|
[d80fa05] | 333 | if (reserved_features != (reserved_features & device_reserved_features))
|
---|
| 334 | return ENOTSUP;
|
---|
| 335 | reserved_features &= device_reserved_features;
|
---|
| 336 |
|
---|
[2d4faf7] | 337 | /* 4. Write the accepted feature flags */
|
---|
[9af56b6] | 338 | pio_write_le32(&cfg->driver_feature_select, VIRTIO_FEATURES_0_31);
|
---|
| 339 | pio_write_le32(&cfg->driver_feature, features);
|
---|
[d80fa05] | 340 | pio_write_le32(&cfg->driver_feature_select, VIRTIO_FEATURES_32_63);
|
---|
| 341 | pio_write_le32(&cfg->driver_feature, reserved_features);
|
---|
[2d4faf7] | 342 |
|
---|
[d80fa05] | 343 | ddf_msg(LVL_NOTE, "accepted features %x, reserved features %x",
|
---|
| 344 | features, reserved_features);
|
---|
[2d4faf7] | 345 |
|
---|
| 346 | /* 5. Set FEATURES_OK */
|
---|
| 347 | status |= VIRTIO_DEV_STATUS_FEATURES_OK;
|
---|
| 348 | pio_write_8(&cfg->device_status, status);
|
---|
| 349 |
|
---|
[eda41a9e] | 350 | /* 6. Test if the device supports our feature subset */
|
---|
[2d4faf7] | 351 | status = pio_read_8(&cfg->device_status);
|
---|
| 352 | if (!(status & VIRTIO_DEV_STATUS_FEATURES_OK))
|
---|
| 353 | return ENOTSUP;
|
---|
| 354 |
|
---|
| 355 | return EOK;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | /**
|
---|
| 359 | * Perform device initialization as described in section 3.1.1 of the
|
---|
| 360 | * specification, step 8 (go live).
|
---|
| 361 | */
|
---|
| 362 | void virtio_device_setup_finalize(virtio_dev_t *vdev)
|
---|
| 363 | {
|
---|
| 364 | virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
|
---|
| 365 |
|
---|
| 366 | /* 8. Go live */
|
---|
| 367 | uint8_t status = pio_read_8(&cfg->device_status);
|
---|
| 368 | pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_DRIVER_OK);
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | void virtio_device_setup_fail(virtio_dev_t *vdev)
|
---|
| 372 | {
|
---|
| 373 | virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
|
---|
| 374 |
|
---|
| 375 | uint8_t status = pio_read_8(&cfg->device_status);
|
---|
| 376 | pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_FAILED);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | /** @}
|
---|
| 380 | */
|
---|