| 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 <ddf/driver.h>
|
|---|
| 35 | #include <ddf/log.h>
|
|---|
| 36 | #include <pci_dev_iface.h>
|
|---|
| 37 |
|
|---|
| 38 | static bool check_bar(virtio_dev_t *vdev, uint8_t bar, uint32_t offset,
|
|---|
| 39 | uint32_t length)
|
|---|
| 40 | {
|
|---|
| 41 | /* We must ignore the capability if bar is greater than 5 */
|
|---|
| 42 | if (bar >= PCI_BAR_COUNT)
|
|---|
| 43 | return false;
|
|---|
| 44 |
|
|---|
| 45 | /* This is not a mapped BAR */
|
|---|
| 46 | if (!vdev->bar[bar].mapped)
|
|---|
| 47 | return false;
|
|---|
| 48 |
|
|---|
| 49 | uintptr_t start = (uintptr_t) vdev->bar[bar].mapped_base;
|
|---|
| 50 | if (start + offset < start)
|
|---|
| 51 | return false;
|
|---|
| 52 | if (start + offset > start + vdev->bar[bar].mapped_size)
|
|---|
| 53 | return false;
|
|---|
| 54 | if (start + offset + length < start + offset)
|
|---|
| 55 | return false;
|
|---|
| 56 | if (start + offset + length > start + vdev->bar[bar].mapped_size)
|
|---|
| 57 | return false;
|
|---|
| 58 |
|
|---|
| 59 | return true;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | static void virtio_pci_common_cfg(virtio_dev_t *vdev, uint8_t bar,
|
|---|
| 63 | uint32_t offset, uint32_t length)
|
|---|
| 64 | {
|
|---|
| 65 | if (vdev->common_cfg)
|
|---|
| 66 | return;
|
|---|
| 67 |
|
|---|
| 68 | if (!check_bar(vdev, bar, offset, length))
|
|---|
| 69 | return;
|
|---|
| 70 |
|
|---|
| 71 | vdev->common_cfg = vdev->bar[bar].mapped_base + offset;
|
|---|
| 72 |
|
|---|
| 73 | ddf_msg(LVL_NOTE, "common_cfg=%p", vdev->common_cfg);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | static void virtio_pci_notify_cfg(virtio_dev_t *vdev, uint8_t bar,
|
|---|
| 77 | uint32_t offset, uint32_t length, uint32_t multiplier)
|
|---|
| 78 | {
|
|---|
| 79 | if (vdev->notify_base)
|
|---|
| 80 | return;
|
|---|
| 81 |
|
|---|
| 82 | if (!check_bar(vdev, bar, offset, length))
|
|---|
| 83 | return;
|
|---|
| 84 |
|
|---|
| 85 | vdev->notify_base = vdev->bar[bar].mapped_base + offset;
|
|---|
| 86 | vdev->notify_off_multiplier = multiplier;
|
|---|
| 87 |
|
|---|
| 88 | ddf_msg(LVL_NOTE, "notify_base=%p, off_multiplier=%u",
|
|---|
| 89 | vdev->notify_base, vdev->notify_off_multiplier);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | static void virtio_pci_isr_cfg(virtio_dev_t *vdev, uint8_t bar, uint32_t offset,
|
|---|
| 93 | uint32_t length)
|
|---|
| 94 | {
|
|---|
| 95 | if (vdev->isr)
|
|---|
| 96 | return;
|
|---|
| 97 |
|
|---|
| 98 | if (!check_bar(vdev, bar, offset, length))
|
|---|
| 99 | return;
|
|---|
| 100 |
|
|---|
| 101 | vdev->isr = vdev->bar[bar].mapped_base + offset;
|
|---|
| 102 | vdev->isr_phys = vdev->bar[bar].phys_base + offset;
|
|---|
| 103 |
|
|---|
| 104 | ddf_msg(LVL_NOTE, "isr=%p (phys=%#" PRIxn ")", vdev->isr,
|
|---|
| 105 | vdev->isr_phys);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | static void virtio_pci_device_cfg(virtio_dev_t *vdev, uint8_t bar,
|
|---|
| 109 | uint32_t offset, uint32_t length)
|
|---|
| 110 | {
|
|---|
| 111 | if (vdev->device_cfg)
|
|---|
| 112 | return;
|
|---|
| 113 |
|
|---|
| 114 | if (!check_bar(vdev, bar, offset, length))
|
|---|
| 115 | return;
|
|---|
| 116 |
|
|---|
| 117 | vdev->device_cfg = vdev->bar[bar].mapped_base + offset;
|
|---|
| 118 |
|
|---|
| 119 | ddf_msg(LVL_NOTE, "device_cfg=%p", vdev->device_cfg);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | static errno_t enable_resources(async_sess_t *pci_sess, virtio_dev_t *vdev)
|
|---|
| 123 | {
|
|---|
| 124 | pio_window_t pio_window;
|
|---|
| 125 | errno_t rc = pio_window_get(pci_sess, &pio_window);
|
|---|
| 126 | if (rc != EOK)
|
|---|
| 127 | return rc;
|
|---|
| 128 |
|
|---|
| 129 | hw_resource_list_t hw_res;
|
|---|
| 130 | rc = hw_res_get_resource_list(pci_sess, &hw_res);
|
|---|
| 131 | if (rc != EOK)
|
|---|
| 132 | return rc;
|
|---|
| 133 |
|
|---|
| 134 | /*
|
|---|
| 135 | * Enable resources and reconstruct the mapping between BAR and resource
|
|---|
| 136 | * indices. We are going to need this later when the VIRTIO PCI
|
|---|
| 137 | * capabilities refer to specific BARs.
|
|---|
| 138 | *
|
|---|
| 139 | * XXX: The mapping should probably be provided by the PCI driver
|
|---|
| 140 | * itself.
|
|---|
| 141 | */
|
|---|
| 142 | for (unsigned i = 0, j = 0; i < PCI_BAR_COUNT && j < hw_res.count;
|
|---|
| 143 | i++) {
|
|---|
| 144 | /* Detect and skip unused BARs */
|
|---|
| 145 | uint32_t bar;
|
|---|
| 146 | rc = pci_config_space_read_32(pci_sess,
|
|---|
| 147 | PCI_BAR0 + i * sizeof(uint32_t), &bar);
|
|---|
| 148 | if (rc != EOK)
|
|---|
| 149 | return rc;
|
|---|
| 150 | if (!bar)
|
|---|
| 151 | continue;
|
|---|
| 152 |
|
|---|
| 153 | hw_resource_t *res = &hw_res.resources[j];
|
|---|
| 154 | rc = pio_enable_resource(&pio_window, res,
|
|---|
| 155 | &vdev->bar[i].mapped_base, &vdev->bar[i].phys_base,
|
|---|
| 156 | &vdev->bar[i].mapped_size);
|
|---|
| 157 | if (rc == EOK)
|
|---|
| 158 | vdev->bar[i].mapped = true;
|
|---|
| 159 | j++;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | return rc;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | static errno_t disable_resources(virtio_dev_t *vdev)
|
|---|
| 166 | {
|
|---|
| 167 | for (unsigned i = 0; i < PCI_BAR_COUNT; i++) {
|
|---|
| 168 | if (vdev->bar[i].mapped) {
|
|---|
| 169 | errno_t rc = pio_disable(vdev->bar[i].mapped_base,
|
|---|
| 170 | vdev->bar[i].mapped_size);
|
|---|
| 171 | if (rc != EOK)
|
|---|
| 172 | return rc;
|
|---|
| 173 | vdev->bar[i].mapped = false;
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | return EOK;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | errno_t virtio_pci_dev_initialize(ddf_dev_t *dev, virtio_dev_t *vdev)
|
|---|
| 181 | {
|
|---|
| 182 | memset(vdev, 0, sizeof(virtio_dev_t));
|
|---|
| 183 |
|
|---|
| 184 | async_sess_t *pci_sess = ddf_dev_parent_sess_get(dev);
|
|---|
| 185 | if (!pci_sess)
|
|---|
| 186 | return ENOENT;
|
|---|
| 187 |
|
|---|
| 188 | errno_t rc = enable_resources(pci_sess, vdev);
|
|---|
| 189 | if (rc != EOK)
|
|---|
| 190 | goto error;
|
|---|
| 191 |
|
|---|
| 192 | /*
|
|---|
| 193 | * Find the VIRTIO PCI Capabilities
|
|---|
| 194 | */
|
|---|
| 195 | uint8_t c;
|
|---|
| 196 | uint8_t cap_vndr;
|
|---|
| 197 | for (rc = pci_config_space_cap_first(pci_sess, &c, &cap_vndr);
|
|---|
| 198 | (rc == EOK) && c;
|
|---|
| 199 | rc = pci_config_space_cap_next(pci_sess, &c, &cap_vndr)) {
|
|---|
| 200 | if (cap_vndr != PCI_CAP_VENDORSPECID)
|
|---|
| 201 | continue;
|
|---|
| 202 |
|
|---|
| 203 | uint8_t cap_len;
|
|---|
| 204 | rc = pci_config_space_read_8(pci_sess,
|
|---|
| 205 | VIRTIO_PCI_CAP_CAP_LEN(c), &cap_len);
|
|---|
| 206 | if (rc != EOK)
|
|---|
| 207 | goto error;
|
|---|
| 208 |
|
|---|
| 209 | if (cap_len < VIRTIO_PCI_CAP_END(0)) {
|
|---|
| 210 | rc = EINVAL;
|
|---|
| 211 | goto error;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | uint8_t cfg_type;
|
|---|
| 215 | rc = pci_config_space_read_8(pci_sess,
|
|---|
| 216 | VIRTIO_PCI_CAP_CFG_TYPE(c), &cfg_type);
|
|---|
| 217 | if (rc != EOK)
|
|---|
| 218 | goto error;
|
|---|
| 219 |
|
|---|
| 220 | uint8_t bar;
|
|---|
| 221 | rc = pci_config_space_read_8(pci_sess, VIRTIO_PCI_CAP_BAR(c),
|
|---|
| 222 | &bar);
|
|---|
| 223 | if (rc != EOK)
|
|---|
| 224 | goto error;
|
|---|
| 225 |
|
|---|
| 226 | uint32_t offset;
|
|---|
| 227 | rc = pci_config_space_read_32(pci_sess,
|
|---|
| 228 | VIRTIO_PCI_CAP_OFFSET(c), &offset);
|
|---|
| 229 | if (rc != EOK)
|
|---|
| 230 | goto error;
|
|---|
| 231 |
|
|---|
| 232 | uint32_t length;
|
|---|
| 233 | rc = pci_config_space_read_32(pci_sess,
|
|---|
| 234 | VIRTIO_PCI_CAP_LENGTH(c), &length);
|
|---|
| 235 | if (rc != EOK)
|
|---|
| 236 | goto error;
|
|---|
| 237 |
|
|---|
| 238 | uint32_t multiplier;
|
|---|
| 239 | switch (cfg_type) {
|
|---|
| 240 | case VIRTIO_PCI_CAP_COMMON_CFG:
|
|---|
| 241 | virtio_pci_common_cfg(vdev, bar, offset, length);
|
|---|
| 242 | break;
|
|---|
| 243 | case VIRTIO_PCI_CAP_NOTIFY_CFG:
|
|---|
| 244 | if (cap_len < VIRTIO_PCI_CAP_END(sizeof(uint32_t))) {
|
|---|
| 245 | rc = EINVAL;
|
|---|
| 246 | goto error;
|
|---|
| 247 | }
|
|---|
| 248 | rc = pci_config_space_read_32(pci_sess,
|
|---|
| 249 | VIRTIO_PCI_CAP_END(c), &multiplier);
|
|---|
| 250 | if (rc != EOK)
|
|---|
| 251 | goto error;
|
|---|
| 252 | virtio_pci_notify_cfg(vdev, bar, offset, length,
|
|---|
| 253 | multiplier);
|
|---|
| 254 | break;
|
|---|
| 255 | case VIRTIO_PCI_CAP_ISR_CFG:
|
|---|
| 256 | virtio_pci_isr_cfg(vdev, bar, offset, length);
|
|---|
| 257 | break;
|
|---|
| 258 | case VIRTIO_PCI_CAP_DEVICE_CFG:
|
|---|
| 259 | virtio_pci_device_cfg(vdev, bar, offset, length);
|
|---|
| 260 | break;
|
|---|
| 261 | case VIRTIO_PCI_CAP_PCI_CFG:
|
|---|
| 262 | break;
|
|---|
| 263 | default:
|
|---|
| 264 | break;
|
|---|
| 265 | }
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /* Check that the configuration is complete */
|
|---|
| 269 | if (!vdev->common_cfg || !vdev->notify_base || !vdev->isr ||
|
|---|
| 270 | !vdev->device_cfg) {
|
|---|
| 271 | rc = EINVAL;
|
|---|
| 272 | goto error;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | return rc;
|
|---|
| 276 |
|
|---|
| 277 | error:
|
|---|
| 278 | (void) disable_resources(vdev);
|
|---|
| 279 | return rc;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | errno_t virtio_pci_dev_cleanup(virtio_dev_t *vdev)
|
|---|
| 283 | {
|
|---|
| 284 | if (vdev->queues) {
|
|---|
| 285 | for (unsigned i = 0;
|
|---|
| 286 | i < pio_read_le16(&vdev->common_cfg->num_queues); i++)
|
|---|
| 287 | virtio_virtq_teardown(vdev, i);
|
|---|
| 288 | free(vdev->queues);
|
|---|
| 289 | }
|
|---|
| 290 | return disable_resources(vdev);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /** @}
|
|---|
| 294 | */
|
|---|