| 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>
|
|---|
| 39 |
|
|---|
| 40 | errno_t virtio_virtq_setup(virtio_dev_t *vdev, uint16_t num, uint16_t size)
|
|---|
| 41 | {
|
|---|
| 42 | virtq_t *q = &vdev->queues[num];
|
|---|
| 43 | virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
|
|---|
| 44 |
|
|---|
| 45 | /* Program the queue of our interest */
|
|---|
| 46 | pio_write_16(&cfg->queue_select, num);
|
|---|
| 47 |
|
|---|
| 48 | /* Trim the size of the queue as needed */
|
|---|
| 49 | if (size > pio_read_16(&cfg->queue_size)) {
|
|---|
| 50 | ddf_msg(LVL_ERROR, "Virtq %u: not enough descriptors", num);
|
|---|
| 51 | return ENOMEM;
|
|---|
| 52 | }
|
|---|
| 53 | pio_write_16(&cfg->queue_size, size);
|
|---|
| 54 | ddf_msg(LVL_NOTE, "Virtq %u: %u descriptors", num, (unsigned) size);
|
|---|
| 55 |
|
|---|
| 56 | size_t avail_offset = 0;
|
|---|
| 57 | size_t used_offset = 0;
|
|---|
| 58 |
|
|---|
| 59 | /*
|
|---|
| 60 | * Compute the size of the needed DMA memory and also the offsets of
|
|---|
| 61 | * the individual components
|
|---|
| 62 | */
|
|---|
| 63 | size_t mem_size = sizeof(virtq_desc_t[size]);
|
|---|
| 64 | mem_size = ALIGN_UP(mem_size, _Alignof(virtq_avail_t));
|
|---|
| 65 | avail_offset = mem_size;
|
|---|
| 66 | mem_size += sizeof(virtq_avail_t) + sizeof(ioport16_t[size]) +
|
|---|
| 67 | sizeof(ioport16_t);
|
|---|
| 68 | mem_size = ALIGN_UP(mem_size, _Alignof(virtq_used_t));
|
|---|
| 69 | used_offset = mem_size;
|
|---|
| 70 | mem_size += sizeof(virtq_used_t) + sizeof(virtq_used_elem_t[size]) +
|
|---|
| 71 | sizeof(ioport16_t);
|
|---|
| 72 |
|
|---|
| 73 | /*
|
|---|
| 74 | * Allocate DMA memory for the virtqueues
|
|---|
| 75 | */
|
|---|
| 76 | q->virt = AS_AREA_ANY;
|
|---|
| 77 | errno_t rc = dmamem_map_anonymous(mem_size, 0,
|
|---|
| 78 | AS_AREA_READ | AS_AREA_WRITE, 0, &q->phys, &q->virt);
|
|---|
| 79 | if (rc != EOK) {
|
|---|
| 80 | q->virt = NULL;
|
|---|
| 81 | return rc;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | q->size = mem_size;
|
|---|
| 85 | q->queue_size = size;
|
|---|
| 86 | q->desc = q->virt;
|
|---|
| 87 | q->avail = q->virt + avail_offset;
|
|---|
| 88 | q->used = q->virt + used_offset;
|
|---|
| 89 |
|
|---|
| 90 | memset(q->virt, 0, q->size);
|
|---|
| 91 |
|
|---|
| 92 | /*
|
|---|
| 93 | * Initialize the descriptor table
|
|---|
| 94 | */
|
|---|
| 95 | for (unsigned i = 0; i < size; i++) {
|
|---|
| 96 | q->desc[i].addr = 0;
|
|---|
| 97 | q->desc[i].len = 0;
|
|---|
| 98 | q->desc[i].flags = 0;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /*
|
|---|
| 102 | * Write the configured addresses to device's common config
|
|---|
| 103 | */
|
|---|
| 104 | pio_write_64(&cfg->queue_desc, q->phys);
|
|---|
| 105 | pio_write_64(&cfg->queue_avail, q->phys + avail_offset);
|
|---|
| 106 | pio_write_64(&cfg->queue_used, q->phys + used_offset);
|
|---|
| 107 |
|
|---|
| 108 | ddf_msg(LVL_NOTE, "DMA memory for virtq %d: virt=%p, phys=%p, size=%zu",
|
|---|
| 109 | num, q->virt, (void *) q->phys, q->size);
|
|---|
| 110 |
|
|---|
| 111 | /* Determine virtq's notification address */
|
|---|
| 112 | q->notify = vdev->notify_base +
|
|---|
| 113 | pio_read_16(&cfg->queue_notif_off) * vdev->notify_off_multiplier;
|
|---|
| 114 |
|
|---|
| 115 | ddf_msg(LVL_NOTE, "notification register: %p", q->notify);
|
|---|
| 116 |
|
|---|
| 117 | return rc;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | void virtio_virtq_teardown(virtio_dev_t *vdev, uint16_t num)
|
|---|
| 121 | {
|
|---|
| 122 | virtq_t *q = &vdev->queues[num];
|
|---|
| 123 | if (q->size)
|
|---|
| 124 | dmamem_unmap_anonymous(q->virt);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * Perform device initialization as described in section 3.1.1 of the
|
|---|
| 129 | * specification, steps 1 - 6.
|
|---|
| 130 | */
|
|---|
| 131 | errno_t virtio_device_setup_start(virtio_dev_t *vdev, uint32_t features)
|
|---|
| 132 | {
|
|---|
| 133 | virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
|
|---|
| 134 |
|
|---|
| 135 | /* 1. Reset the device */
|
|---|
| 136 | uint8_t status = VIRTIO_DEV_STATUS_RESET;
|
|---|
| 137 | pio_write_8(&cfg->device_status, status);
|
|---|
| 138 |
|
|---|
| 139 | /* 2. Acknowledge we found the device */
|
|---|
| 140 | status |= VIRTIO_DEV_STATUS_ACKNOWLEDGE;
|
|---|
| 141 | pio_write_8(&cfg->device_status, status);
|
|---|
| 142 |
|
|---|
| 143 | /* 3. We know how to drive the device */
|
|---|
| 144 | status |= VIRTIO_DEV_STATUS_DRIVER;
|
|---|
| 145 | pio_write_8(&cfg->device_status, status);
|
|---|
| 146 |
|
|---|
| 147 | /* 4. Read the offered feature flags */
|
|---|
| 148 | pio_write_32(&cfg->device_feature_select, VIRTIO_FEATURES_0_31);
|
|---|
| 149 | uint32_t device_features = pio_read_32(&cfg->device_feature);
|
|---|
| 150 |
|
|---|
| 151 | ddf_msg(LVL_NOTE, "offered features %x", device_features);
|
|---|
| 152 | features &= device_features;
|
|---|
| 153 |
|
|---|
| 154 | if (!features)
|
|---|
| 155 | return ENOTSUP;
|
|---|
| 156 |
|
|---|
| 157 | /* 4. Write the accepted feature flags */
|
|---|
| 158 | pio_write_32(&cfg->driver_feature_select, VIRTIO_FEATURES_0_31);
|
|---|
| 159 | pio_write_32(&cfg->driver_feature, features);
|
|---|
| 160 |
|
|---|
| 161 | ddf_msg(LVL_NOTE, "accepted features %x", features);
|
|---|
| 162 |
|
|---|
| 163 | /* 5. Set FEATURES_OK */
|
|---|
| 164 | status |= VIRTIO_DEV_STATUS_FEATURES_OK;
|
|---|
| 165 | pio_write_8(&cfg->device_status, status);
|
|---|
| 166 |
|
|---|
| 167 | /* 6. Test if the device supports our feature subset */
|
|---|
| 168 | status = pio_read_8(&cfg->device_status);
|
|---|
| 169 | if (!(status & VIRTIO_DEV_STATUS_FEATURES_OK))
|
|---|
| 170 | return ENOTSUP;
|
|---|
| 171 |
|
|---|
| 172 | return EOK;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Perform device initialization as described in section 3.1.1 of the
|
|---|
| 177 | * specification, step 8 (go live).
|
|---|
| 178 | */
|
|---|
| 179 | void virtio_device_setup_finalize(virtio_dev_t *vdev)
|
|---|
| 180 | {
|
|---|
| 181 | virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
|
|---|
| 182 |
|
|---|
| 183 | /* 8. Go live */
|
|---|
| 184 | uint8_t status = pio_read_8(&cfg->device_status);
|
|---|
| 185 | pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_DRIVER_OK);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | void virtio_device_setup_fail(virtio_dev_t *vdev)
|
|---|
| 189 | {
|
|---|
| 190 | virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
|
|---|
| 191 |
|
|---|
| 192 | uint8_t status = pio_read_8(&cfg->device_status);
|
|---|
| 193 | pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_FAILED);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /** @}
|
|---|
| 197 | */
|
|---|