Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> |
| 4 | * |
| 5 | * VirtIO PCI bus transport driver |
| 6 | * Ported from Linux drivers/virtio/virtio_pci*.c |
| 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> |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 12 | #include <virtio_types.h> |
| 13 | #include <virtio.h> |
| 14 | #include <virtio_ring.h> |
| 15 | #include <dm/device.h> |
Simon Glass | eb41d8a | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 16 | #include <linux/bug.h> |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 17 | #include <linux/compat.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 20 | #include <linux/io.h> |
| 21 | #include "virtio_pci.h" |
| 22 | |
| 23 | #define VIRTIO_PCI_DRV_NAME "virtio-pci.m" |
| 24 | |
| 25 | /* PCI device ID in the range 0x1040 to 0x107f */ |
| 26 | #define VIRTIO_PCI_VENDOR_ID 0x1af4 |
| 27 | #define VIRTIO_PCI_DEVICE_ID00 0x1040 |
| 28 | #define VIRTIO_PCI_DEVICE_ID01 0x1041 |
| 29 | #define VIRTIO_PCI_DEVICE_ID02 0x1042 |
| 30 | #define VIRTIO_PCI_DEVICE_ID03 0x1043 |
| 31 | #define VIRTIO_PCI_DEVICE_ID04 0x1044 |
| 32 | #define VIRTIO_PCI_DEVICE_ID05 0x1045 |
| 33 | #define VIRTIO_PCI_DEVICE_ID06 0x1046 |
| 34 | #define VIRTIO_PCI_DEVICE_ID07 0x1047 |
| 35 | #define VIRTIO_PCI_DEVICE_ID08 0x1048 |
| 36 | #define VIRTIO_PCI_DEVICE_ID09 0x1049 |
| 37 | #define VIRTIO_PCI_DEVICE_ID0A 0x104a |
| 38 | #define VIRTIO_PCI_DEVICE_ID0B 0x104b |
| 39 | #define VIRTIO_PCI_DEVICE_ID0C 0x104c |
| 40 | #define VIRTIO_PCI_DEVICE_ID0D 0x104d |
| 41 | #define VIRTIO_PCI_DEVICE_ID0E 0x104e |
| 42 | #define VIRTIO_PCI_DEVICE_ID0F 0x104f |
| 43 | #define VIRTIO_PCI_DEVICE_ID10 0x1050 |
| 44 | #define VIRTIO_PCI_DEVICE_ID11 0x1051 |
| 45 | #define VIRTIO_PCI_DEVICE_ID12 0x1052 |
| 46 | #define VIRTIO_PCI_DEVICE_ID13 0x1053 |
| 47 | #define VIRTIO_PCI_DEVICE_ID14 0x1054 |
| 48 | #define VIRTIO_PCI_DEVICE_ID15 0x1055 |
| 49 | #define VIRTIO_PCI_DEVICE_ID16 0x1056 |
| 50 | #define VIRTIO_PCI_DEVICE_ID17 0x1057 |
| 51 | #define VIRTIO_PCI_DEVICE_ID18 0x1058 |
| 52 | #define VIRTIO_PCI_DEVICE_ID19 0x1059 |
| 53 | #define VIRTIO_PCI_DEVICE_ID1A 0x105a |
| 54 | #define VIRTIO_PCI_DEVICE_ID1B 0x105b |
| 55 | #define VIRTIO_PCI_DEVICE_ID1C 0x105c |
| 56 | #define VIRTIO_PCI_DEVICE_ID1D 0x105d |
| 57 | #define VIRTIO_PCI_DEVICE_ID1E 0x105e |
| 58 | #define VIRTIO_PCI_DEVICE_ID1F 0x105f |
| 59 | #define VIRTIO_PCI_DEVICE_ID20 0x1060 |
| 60 | #define VIRTIO_PCI_DEVICE_ID21 0x1061 |
| 61 | #define VIRTIO_PCI_DEVICE_ID22 0x1062 |
| 62 | #define VIRTIO_PCI_DEVICE_ID23 0x1063 |
| 63 | #define VIRTIO_PCI_DEVICE_ID24 0x1064 |
| 64 | #define VIRTIO_PCI_DEVICE_ID25 0x1065 |
| 65 | #define VIRTIO_PCI_DEVICE_ID26 0x1066 |
| 66 | #define VIRTIO_PCI_DEVICE_ID27 0x1067 |
| 67 | #define VIRTIO_PCI_DEVICE_ID28 0x1068 |
| 68 | #define VIRTIO_PCI_DEVICE_ID29 0x1069 |
| 69 | #define VIRTIO_PCI_DEVICE_ID2A 0x106a |
| 70 | #define VIRTIO_PCI_DEVICE_ID2B 0x106b |
| 71 | #define VIRTIO_PCI_DEVICE_ID2C 0x106c |
| 72 | #define VIRTIO_PCI_DEVICE_ID2D 0x106d |
| 73 | #define VIRTIO_PCI_DEVICE_ID2E 0x106e |
| 74 | #define VIRTIO_PCI_DEVICE_ID2F 0x106f |
| 75 | #define VIRTIO_PCI_DEVICE_ID30 0x1070 |
| 76 | #define VIRTIO_PCI_DEVICE_ID31 0x1071 |
| 77 | #define VIRTIO_PCI_DEVICE_ID32 0x1072 |
| 78 | #define VIRTIO_PCI_DEVICE_ID33 0x1073 |
| 79 | #define VIRTIO_PCI_DEVICE_ID34 0x1074 |
| 80 | #define VIRTIO_PCI_DEVICE_ID35 0x1075 |
| 81 | #define VIRTIO_PCI_DEVICE_ID36 0x1076 |
| 82 | #define VIRTIO_PCI_DEVICE_ID37 0x1077 |
| 83 | #define VIRTIO_PCI_DEVICE_ID38 0x1078 |
| 84 | #define VIRTIO_PCI_DEVICE_ID39 0x1079 |
| 85 | #define VIRTIO_PCI_DEVICE_ID3A 0x107a |
| 86 | #define VIRTIO_PCI_DEVICE_ID3B 0x107b |
| 87 | #define VIRTIO_PCI_DEVICE_ID3C 0x107c |
| 88 | #define VIRTIO_PCI_DEVICE_ID3D 0x107d |
| 89 | #define VIRTIO_PCI_DEVICE_ID3E 0x107e |
| 90 | #define VIRTIO_PCI_DEVICE_ID3F 0x107f |
| 91 | |
| 92 | /** |
| 93 | * virtio pci transport driver private data |
| 94 | * |
| 95 | * @common: pci transport device common register block base |
| 96 | * @notify_base: pci transport device notify register block base |
| 97 | * @device: pci transport device device-specific register block base |
| 98 | * @device_len: pci transport device device-specific register block length |
| 99 | * @notify_offset_multiplier: multiply queue_notify_off by this value |
| 100 | */ |
| 101 | struct virtio_pci_priv { |
| 102 | struct virtio_pci_common_cfg __iomem *common; |
| 103 | void __iomem *notify_base; |
| 104 | void __iomem *device; |
| 105 | u32 device_len; |
| 106 | u32 notify_offset_multiplier; |
| 107 | }; |
| 108 | |
| 109 | static int virtio_pci_get_config(struct udevice *udev, unsigned int offset, |
| 110 | void *buf, unsigned int len) |
| 111 | { |
| 112 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 113 | u8 b; |
| 114 | __le16 w; |
| 115 | __le32 l; |
| 116 | |
Andrew Scull | 94b28b9 | 2022-04-21 16:11:01 +0000 | [diff] [blame^] | 117 | if (!priv->device) |
| 118 | return -ENOSYS; |
| 119 | |
| 120 | if (offset + len > priv->device_len) |
| 121 | return -EINVAL; |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 122 | |
| 123 | switch (len) { |
| 124 | case 1: |
| 125 | b = ioread8(priv->device + offset); |
| 126 | memcpy(buf, &b, sizeof(b)); |
| 127 | break; |
| 128 | case 2: |
| 129 | w = cpu_to_le16(ioread16(priv->device + offset)); |
| 130 | memcpy(buf, &w, sizeof(w)); |
| 131 | break; |
| 132 | case 4: |
| 133 | l = cpu_to_le32(ioread32(priv->device + offset)); |
| 134 | memcpy(buf, &l, sizeof(l)); |
| 135 | break; |
| 136 | case 8: |
| 137 | l = cpu_to_le32(ioread32(priv->device + offset)); |
| 138 | memcpy(buf, &l, sizeof(l)); |
| 139 | l = cpu_to_le32(ioread32(priv->device + offset + sizeof(l))); |
| 140 | memcpy(buf + sizeof(l), &l, sizeof(l)); |
| 141 | break; |
| 142 | default: |
Andrew Scull | 94b28b9 | 2022-04-21 16:11:01 +0000 | [diff] [blame^] | 143 | return -EINVAL; |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int virtio_pci_set_config(struct udevice *udev, unsigned int offset, |
| 150 | const void *buf, unsigned int len) |
| 151 | { |
| 152 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 153 | u8 b; |
| 154 | __le16 w; |
| 155 | __le32 l; |
| 156 | |
Andrew Scull | 94b28b9 | 2022-04-21 16:11:01 +0000 | [diff] [blame^] | 157 | if (!priv->device) |
| 158 | return -ENOSYS; |
| 159 | |
| 160 | if (offset + len > priv->device_len) |
| 161 | return -EINVAL; |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 162 | |
| 163 | switch (len) { |
| 164 | case 1: |
| 165 | memcpy(&b, buf, sizeof(b)); |
| 166 | iowrite8(b, priv->device + offset); |
| 167 | break; |
| 168 | case 2: |
| 169 | memcpy(&w, buf, sizeof(w)); |
| 170 | iowrite16(le16_to_cpu(w), priv->device + offset); |
| 171 | break; |
| 172 | case 4: |
| 173 | memcpy(&l, buf, sizeof(l)); |
| 174 | iowrite32(le32_to_cpu(l), priv->device + offset); |
| 175 | break; |
| 176 | case 8: |
| 177 | memcpy(&l, buf, sizeof(l)); |
| 178 | iowrite32(le32_to_cpu(l), priv->device + offset); |
| 179 | memcpy(&l, buf + sizeof(l), sizeof(l)); |
| 180 | iowrite32(le32_to_cpu(l), priv->device + offset + sizeof(l)); |
| 181 | break; |
| 182 | default: |
Andrew Scull | 94b28b9 | 2022-04-21 16:11:01 +0000 | [diff] [blame^] | 183 | return -EINVAL; |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int virtio_pci_generation(struct udevice *udev, u32 *counter) |
| 190 | { |
| 191 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 192 | |
| 193 | *counter = ioread8(&priv->common->config_generation); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static int virtio_pci_get_status(struct udevice *udev, u8 *status) |
| 199 | { |
| 200 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 201 | |
| 202 | *status = ioread8(&priv->common->device_status); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int virtio_pci_set_status(struct udevice *udev, u8 status) |
| 208 | { |
| 209 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 210 | |
| 211 | /* We should never be setting status to 0 */ |
| 212 | WARN_ON(status == 0); |
| 213 | |
| 214 | iowrite8(status, &priv->common->device_status); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int virtio_pci_reset(struct udevice *udev) |
| 220 | { |
| 221 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 222 | |
| 223 | /* 0 status means a reset */ |
| 224 | iowrite8(0, &priv->common->device_status); |
| 225 | |
| 226 | /* |
| 227 | * After writing 0 to device_status, the driver MUST wait for a read |
| 228 | * of device_status to return 0 before reinitializing the device. |
| 229 | * This will flush out the status write, and flush in device writes, |
| 230 | * including MSI-X interrupts, if any. |
| 231 | */ |
| 232 | while (ioread8(&priv->common->device_status)) |
| 233 | udelay(1000); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int virtio_pci_get_features(struct udevice *udev, u64 *features) |
| 239 | { |
| 240 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 241 | |
| 242 | iowrite32(0, &priv->common->device_feature_select); |
| 243 | *features = ioread32(&priv->common->device_feature); |
| 244 | iowrite32(1, &priv->common->device_feature_select); |
| 245 | *features |= ((u64)ioread32(&priv->common->device_feature) << 32); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static int virtio_pci_set_features(struct udevice *udev) |
| 251 | { |
| 252 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 253 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); |
| 254 | |
| 255 | if (!__virtio_test_bit(udev, VIRTIO_F_VERSION_1)) { |
| 256 | debug("virtio: device uses modern interface but does not have VIRTIO_F_VERSION_1\n"); |
| 257 | return -EINVAL; |
| 258 | } |
| 259 | |
| 260 | iowrite32(0, &priv->common->guest_feature_select); |
| 261 | iowrite32((u32)uc_priv->features, &priv->common->guest_feature); |
| 262 | iowrite32(1, &priv->common->guest_feature_select); |
| 263 | iowrite32(uc_priv->features >> 32, &priv->common->guest_feature); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static struct virtqueue *virtio_pci_setup_vq(struct udevice *udev, |
| 269 | unsigned int index) |
| 270 | { |
| 271 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 272 | struct virtio_pci_common_cfg __iomem *cfg = priv->common; |
| 273 | struct virtqueue *vq; |
| 274 | u16 num; |
| 275 | u64 addr; |
| 276 | int err; |
| 277 | |
| 278 | if (index >= ioread16(&cfg->num_queues)) |
| 279 | return ERR_PTR(-ENOENT); |
| 280 | |
| 281 | /* Select the queue we're interested in */ |
| 282 | iowrite16(index, &cfg->queue_select); |
| 283 | |
| 284 | /* Check if queue is either not available or already active */ |
| 285 | num = ioread16(&cfg->queue_size); |
| 286 | if (!num || ioread16(&cfg->queue_enable)) |
| 287 | return ERR_PTR(-ENOENT); |
| 288 | |
| 289 | if (num & (num - 1)) { |
| 290 | printf("(%s): bad queue size %u", udev->name, num); |
| 291 | return ERR_PTR(-EINVAL); |
| 292 | } |
| 293 | |
| 294 | /* Create the vring */ |
| 295 | vq = vring_create_virtqueue(index, num, VIRTIO_PCI_VRING_ALIGN, udev); |
| 296 | if (!vq) { |
| 297 | err = -ENOMEM; |
| 298 | goto error_available; |
| 299 | } |
| 300 | |
| 301 | /* Activate the queue */ |
| 302 | iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size); |
| 303 | |
| 304 | addr = virtqueue_get_desc_addr(vq); |
| 305 | iowrite32((u32)addr, &cfg->queue_desc_lo); |
| 306 | iowrite32(addr >> 32, &cfg->queue_desc_hi); |
| 307 | |
| 308 | addr = virtqueue_get_avail_addr(vq); |
| 309 | iowrite32((u32)addr, &cfg->queue_avail_lo); |
| 310 | iowrite32(addr >> 32, &cfg->queue_avail_hi); |
| 311 | |
| 312 | addr = virtqueue_get_used_addr(vq); |
| 313 | iowrite32((u32)addr, &cfg->queue_used_lo); |
| 314 | iowrite32(addr >> 32, &cfg->queue_used_hi); |
| 315 | |
| 316 | iowrite16(1, &cfg->queue_enable); |
| 317 | |
| 318 | return vq; |
| 319 | |
| 320 | error_available: |
| 321 | return ERR_PTR(err); |
| 322 | } |
| 323 | |
| 324 | static void virtio_pci_del_vq(struct virtqueue *vq) |
| 325 | { |
| 326 | struct virtio_pci_priv *priv = dev_get_priv(vq->vdev); |
| 327 | unsigned int index = vq->index; |
| 328 | |
| 329 | iowrite16(index, &priv->common->queue_select); |
| 330 | |
| 331 | /* Select and deactivate the queue */ |
| 332 | iowrite16(0, &priv->common->queue_enable); |
| 333 | |
| 334 | vring_del_virtqueue(vq); |
| 335 | } |
| 336 | |
| 337 | static int virtio_pci_del_vqs(struct udevice *udev) |
| 338 | { |
| 339 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); |
| 340 | struct virtqueue *vq, *n; |
| 341 | |
| 342 | list_for_each_entry_safe(vq, n, &uc_priv->vqs, list) |
| 343 | virtio_pci_del_vq(vq); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int virtio_pci_find_vqs(struct udevice *udev, unsigned int nvqs, |
| 349 | struct virtqueue *vqs[]) |
| 350 | { |
| 351 | int i; |
| 352 | |
| 353 | for (i = 0; i < nvqs; ++i) { |
| 354 | vqs[i] = virtio_pci_setup_vq(udev, i); |
| 355 | if (IS_ERR(vqs[i])) { |
| 356 | virtio_pci_del_vqs(udev); |
| 357 | return PTR_ERR(vqs[i]); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int virtio_pci_notify(struct udevice *udev, struct virtqueue *vq) |
| 365 | { |
| 366 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 367 | u16 off; |
| 368 | |
| 369 | /* Select the queue we're interested in */ |
| 370 | iowrite16(vq->index, &priv->common->queue_select); |
| 371 | |
| 372 | /* get offset of notification word for this vq */ |
| 373 | off = ioread16(&priv->common->queue_notify_off); |
| 374 | |
| 375 | /* |
| 376 | * We write the queue's selector into the notification register |
| 377 | * to signal the other end |
| 378 | */ |
| 379 | iowrite16(vq->index, |
| 380 | priv->notify_base + off * priv->notify_offset_multiplier); |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * virtio_pci_find_capability - walk capabilities to find device info |
| 387 | * |
| 388 | * @udev: the transport device |
| 389 | * @cfg_type: the VIRTIO_PCI_CAP_* value we seek |
| 390 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 391 | * Return: offset of the configuration structure |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 392 | */ |
| 393 | static int virtio_pci_find_capability(struct udevice *udev, u8 cfg_type) |
| 394 | { |
| 395 | int pos; |
| 396 | int offset; |
| 397 | u8 type, bar; |
| 398 | |
| 399 | for (pos = dm_pci_find_capability(udev, PCI_CAP_ID_VNDR); |
| 400 | pos > 0; |
| 401 | pos = dm_pci_find_next_capability(udev, pos, PCI_CAP_ID_VNDR)) { |
| 402 | offset = pos + offsetof(struct virtio_pci_cap, cfg_type); |
| 403 | dm_pci_read_config8(udev, offset, &type); |
| 404 | offset = pos + offsetof(struct virtio_pci_cap, bar); |
| 405 | dm_pci_read_config8(udev, offset, &bar); |
| 406 | |
| 407 | /* Ignore structures with reserved BAR values */ |
| 408 | if (bar > 0x5) |
| 409 | continue; |
| 410 | |
| 411 | if (type == cfg_type) |
| 412 | return pos; |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * virtio_pci_map_capability - map base address of the capability |
| 420 | * |
| 421 | * @udev: the transport device |
| 422 | * @off: offset of the configuration structure |
| 423 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 424 | * Return: base address of the capability |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 425 | */ |
| 426 | static void __iomem *virtio_pci_map_capability(struct udevice *udev, int off) |
| 427 | { |
| 428 | u8 bar; |
| 429 | u32 offset; |
| 430 | ulong base; |
| 431 | void __iomem *p; |
| 432 | |
| 433 | if (!off) |
| 434 | return NULL; |
| 435 | |
| 436 | offset = off + offsetof(struct virtio_pci_cap, bar); |
| 437 | dm_pci_read_config8(udev, offset, &bar); |
| 438 | offset = off + offsetof(struct virtio_pci_cap, offset); |
| 439 | dm_pci_read_config32(udev, offset, &offset); |
| 440 | |
| 441 | /* |
| 442 | * TODO: adding 64-bit BAR support |
| 443 | * |
| 444 | * Per spec, the BAR is permitted to be either 32-bit or 64-bit. |
| 445 | * For simplicity, only read the BAR address as 32-bit. |
| 446 | */ |
| 447 | base = dm_pci_read_bar32(udev, bar); |
| 448 | p = (void __iomem *)base + offset; |
| 449 | |
| 450 | return p; |
| 451 | } |
| 452 | |
| 453 | static int virtio_pci_bind(struct udevice *udev) |
| 454 | { |
| 455 | static int num_devs; |
| 456 | char name[20]; |
| 457 | |
| 458 | /* Create a unique device name */ |
| 459 | sprintf(name, "%s#%u", VIRTIO_PCI_DRV_NAME, num_devs++); |
| 460 | device_set_name(udev, name); |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static int virtio_pci_probe(struct udevice *udev) |
| 466 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 467 | struct pci_child_plat *pplat = dev_get_parent_plat(udev); |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 468 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); |
| 469 | struct virtio_pci_priv *priv = dev_get_priv(udev); |
| 470 | u16 subvendor; |
| 471 | u8 revision; |
| 472 | int common, notify, device; |
| 473 | int offset; |
| 474 | |
| 475 | /* We only own devices >= 0x1040 and <= 0x107f: leave the rest. */ |
| 476 | if (pplat->device < 0x1040 || pplat->device > 0x107f) |
| 477 | return -ENODEV; |
| 478 | |
| 479 | /* Transitional devices must not have a PCI revision ID of 0 */ |
| 480 | dm_pci_read_config8(udev, PCI_REVISION_ID, &revision); |
| 481 | |
| 482 | /* Modern devices: simply use PCI device id, but start from 0x1040. */ |
| 483 | uc_priv->device = pplat->device - 0x1040; |
| 484 | dm_pci_read_config16(udev, PCI_SUBSYSTEM_VENDOR_ID, &subvendor); |
| 485 | uc_priv->vendor = subvendor; |
| 486 | |
| 487 | /* Check for a common config: if not, use legacy mode (bar 0) */ |
| 488 | common = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_COMMON_CFG); |
| 489 | if (!common) { |
| 490 | printf("(%s): leaving for legacy driver\n", udev->name); |
| 491 | return -ENODEV; |
| 492 | } |
| 493 | |
| 494 | /* If common is there, notify should be too */ |
| 495 | notify = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_NOTIFY_CFG); |
| 496 | if (!notify) { |
| 497 | printf("(%s): missing capabilities %i/%i\n", udev->name, |
| 498 | common, notify); |
| 499 | return -EINVAL; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Device capability is only mandatory for devices that have |
| 504 | * device-specific configuration. |
| 505 | */ |
| 506 | device = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_DEVICE_CFG); |
| 507 | if (device) { |
Andrew Scull | da03cdf | 2022-04-21 16:11:00 +0000 | [diff] [blame] | 508 | offset = device + offsetof(struct virtio_pci_cap, length); |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 509 | dm_pci_read_config32(udev, offset, &priv->device_len); |
| 510 | } |
| 511 | |
| 512 | /* Map configuration structures */ |
| 513 | priv->common = virtio_pci_map_capability(udev, common); |
| 514 | priv->notify_base = virtio_pci_map_capability(udev, notify); |
| 515 | priv->device = virtio_pci_map_capability(udev, device); |
| 516 | debug("(%p): common @ %p, notify base @ %p, device @ %p\n", |
| 517 | udev, priv->common, priv->notify_base, priv->device); |
| 518 | |
| 519 | /* Read notify_off_multiplier from config space */ |
| 520 | offset = notify + offsetof(struct virtio_pci_notify_cap, |
| 521 | notify_off_multiplier); |
| 522 | dm_pci_read_config32(udev, offset, &priv->notify_offset_multiplier); |
| 523 | |
| 524 | debug("(%s): device (%d) vendor (%08x) version (%d)\n", udev->name, |
| 525 | uc_priv->device, uc_priv->vendor, revision); |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static const struct dm_virtio_ops virtio_pci_ops = { |
| 531 | .get_config = virtio_pci_get_config, |
| 532 | .set_config = virtio_pci_set_config, |
| 533 | .generation = virtio_pci_generation, |
| 534 | .get_status = virtio_pci_get_status, |
| 535 | .set_status = virtio_pci_set_status, |
| 536 | .reset = virtio_pci_reset, |
| 537 | .get_features = virtio_pci_get_features, |
| 538 | .set_features = virtio_pci_set_features, |
| 539 | .find_vqs = virtio_pci_find_vqs, |
| 540 | .del_vqs = virtio_pci_del_vqs, |
| 541 | .notify = virtio_pci_notify, |
| 542 | }; |
| 543 | |
| 544 | U_BOOT_DRIVER(virtio_pci_modern) = { |
| 545 | .name = VIRTIO_PCI_DRV_NAME, |
| 546 | .id = UCLASS_VIRTIO, |
| 547 | .ops = &virtio_pci_ops, |
| 548 | .bind = virtio_pci_bind, |
| 549 | .probe = virtio_pci_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 550 | .priv_auto = sizeof(struct virtio_pci_priv), |
Bin Meng | 550435e | 2018-10-15 02:21:23 -0700 | [diff] [blame] | 551 | }; |
| 552 | |
| 553 | static struct pci_device_id virtio_pci_supported[] = { |
| 554 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID00) }, |
| 555 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID01) }, |
| 556 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID02) }, |
| 557 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID03) }, |
| 558 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID04) }, |
| 559 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID05) }, |
| 560 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID06) }, |
| 561 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID07) }, |
| 562 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID08) }, |
| 563 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID09) }, |
| 564 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0A) }, |
| 565 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0B) }, |
| 566 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0C) }, |
| 567 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0D) }, |
| 568 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0E) }, |
| 569 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0F) }, |
| 570 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID10) }, |
| 571 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID11) }, |
| 572 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID12) }, |
| 573 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID13) }, |
| 574 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID14) }, |
| 575 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID15) }, |
| 576 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID16) }, |
| 577 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID17) }, |
| 578 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID18) }, |
| 579 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID19) }, |
| 580 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1A) }, |
| 581 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1B) }, |
| 582 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1C) }, |
| 583 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1D) }, |
| 584 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1E) }, |
| 585 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1F) }, |
| 586 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID20) }, |
| 587 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID21) }, |
| 588 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID22) }, |
| 589 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID23) }, |
| 590 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID24) }, |
| 591 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID25) }, |
| 592 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID26) }, |
| 593 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID27) }, |
| 594 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID28) }, |
| 595 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID29) }, |
| 596 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2A) }, |
| 597 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2B) }, |
| 598 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2C) }, |
| 599 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2D) }, |
| 600 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2E) }, |
| 601 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2F) }, |
| 602 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID30) }, |
| 603 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID31) }, |
| 604 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID32) }, |
| 605 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID33) }, |
| 606 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID34) }, |
| 607 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID35) }, |
| 608 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID36) }, |
| 609 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID37) }, |
| 610 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID38) }, |
| 611 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID39) }, |
| 612 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3A) }, |
| 613 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3B) }, |
| 614 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3C) }, |
| 615 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3D) }, |
| 616 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3E) }, |
| 617 | { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3F) }, |
| 618 | {}, |
| 619 | }; |
| 620 | |
| 621 | U_BOOT_PCI_DEVICE(virtio_pci_modern, virtio_pci_supported); |