2 * Virtio PCI driver - modern (virtio 1.0) device support
4 * This module allows virtio devices to be used over a virtual PCI device.
5 * This can be used with QEMU based VMMs like KVM or Xen.
7 * Copyright IBM Corp. 2007
8 * Copyright Red Hat, Inc. 2014
15 * This work is licensed under the terms of the GNU GPL, version 2 or later.
16 * See the COPYING file in the top-level directory.
20 #include <linux/delay.h>
21 #define VIRTIO_PCI_NO_LEGACY
22 #include "virtio_pci_common.h"
25 * Type-safe wrappers for io accesses.
26 * Use these to enforce at compile time the following spec requirement:
28 * The driver MUST access each field using the “natural” access
29 * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses
30 * for 16-bit fields and 8-bit accesses for 8-bit fields.
32 static inline u8 vp_ioread8(u8 __iomem *addr)
36 static inline u16 vp_ioread16 (__le16 __iomem *addr)
38 return ioread16(addr);
41 static inline u32 vp_ioread32(__le32 __iomem *addr)
43 return ioread32(addr);
46 static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
48 iowrite8(value, addr);
51 static inline void vp_iowrite16(u16 value, __le16 __iomem *addr)
53 iowrite16(value, addr);
56 static inline void vp_iowrite32(u32 value, __le32 __iomem *addr)
58 iowrite32(value, addr);
61 static void vp_iowrite64_twopart(u64 val,
62 __le32 __iomem *lo, __le32 __iomem *hi)
64 vp_iowrite32((u32)val, lo);
65 vp_iowrite32(val >> 32, hi);
68 static void __iomem *map_capability(struct pci_dev *dev, int off,
78 pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
81 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
83 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
86 if (length <= start) {
88 "virtio_pci: bad capability len %u (>%u expected)\n",
93 if (length - start < minlen) {
95 "virtio_pci: bad capability len %u (>=%zu expected)\n",
102 if (start + offset < offset) {
104 "virtio_pci: map wrap-around %u+%u\n",
111 if (offset & (align - 1)) {
113 "virtio_pci: offset %u not aligned to %u\n",
124 if (minlen + offset < minlen ||
125 minlen + offset > pci_resource_len(dev, bar)) {
127 "virtio_pci: map virtio %zu@%u "
128 "out of range on bar %i length %lu\n",
130 bar, (unsigned long)pci_resource_len(dev, bar));
134 p = pci_iomap_range(dev, bar, offset, length);
137 "virtio_pci: unable to map virtio %u@%u on bar %i\n",
138 length, offset, bar);
142 /* virtio config->get_features() implementation */
143 static u64 vp_get_features(struct virtio_device *vdev)
145 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
148 vp_iowrite32(0, &vp_dev->common->device_feature_select);
149 features = vp_ioread32(&vp_dev->common->device_feature);
150 vp_iowrite32(1, &vp_dev->common->device_feature_select);
151 features |= ((u64)vp_ioread32(&vp_dev->common->device_feature) << 32);
156 /* virtio config->finalize_features() implementation */
157 static int vp_finalize_features(struct virtio_device *vdev)
159 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
161 /* Give virtio_ring a chance to accept features. */
162 vring_transport_features(vdev);
164 if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
165 dev_err(&vdev->dev, "virtio: device uses modern interface "
166 "but does not have VIRTIO_F_VERSION_1\n");
170 vp_iowrite32(0, &vp_dev->common->guest_feature_select);
171 vp_iowrite32((u32)vdev->features, &vp_dev->common->guest_feature);
172 vp_iowrite32(1, &vp_dev->common->guest_feature_select);
173 vp_iowrite32(vdev->features >> 32, &vp_dev->common->guest_feature);
178 /* virtio config->get() implementation */
179 static void vp_get(struct virtio_device *vdev, unsigned offset,
180 void *buf, unsigned len)
182 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
187 BUG_ON(offset + len > vp_dev->device_len);
191 b = ioread8(vp_dev->device + offset);
192 memcpy(buf, &b, sizeof b);
195 w = cpu_to_le16(ioread16(vp_dev->device + offset));
196 memcpy(buf, &w, sizeof w);
199 l = cpu_to_le32(ioread32(vp_dev->device + offset));
200 memcpy(buf, &l, sizeof l);
203 l = cpu_to_le32(ioread32(vp_dev->device + offset));
204 memcpy(buf, &l, sizeof l);
205 l = cpu_to_le32(ioread32(vp_dev->device + offset + sizeof l));
206 memcpy(buf + sizeof l, &l, sizeof l);
213 /* the config->set() implementation. it's symmetric to the config->get()
215 static void vp_set(struct virtio_device *vdev, unsigned offset,
216 const void *buf, unsigned len)
218 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
223 BUG_ON(offset + len > vp_dev->device_len);
227 memcpy(&b, buf, sizeof b);
228 iowrite8(b, vp_dev->device + offset);
231 memcpy(&w, buf, sizeof w);
232 iowrite16(le16_to_cpu(w), vp_dev->device + offset);
235 memcpy(&l, buf, sizeof l);
236 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
239 memcpy(&l, buf, sizeof l);
240 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
241 memcpy(&l, buf + sizeof l, sizeof l);
242 iowrite32(le32_to_cpu(l), vp_dev->device + offset + sizeof l);
249 static u32 vp_generation(struct virtio_device *vdev)
251 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
252 return vp_ioread8(&vp_dev->common->config_generation);
255 /* config->{get,set}_status() implementations */
256 static u8 vp_get_status(struct virtio_device *vdev)
258 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
259 return vp_ioread8(&vp_dev->common->device_status);
262 static void vp_set_status(struct virtio_device *vdev, u8 status)
264 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
265 /* We should never be setting status to 0. */
267 vp_iowrite8(status, &vp_dev->common->device_status);
270 static void vp_reset(struct virtio_device *vdev)
272 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
273 /* 0 status means a reset. */
274 vp_iowrite8(0, &vp_dev->common->device_status);
275 /* After writing 0 to device_status, the driver MUST wait for a read of
276 * device_status to return 0 before reinitializing the device.
277 * This will flush out the status write, and flush in device writes,
278 * including MSI-X interrupts, if any.
280 while (vp_ioread8(&vp_dev->common->device_status))
282 /* Flush pending VQ/configuration callbacks. */
283 vp_synchronize_vectors(vdev);
286 static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
288 /* Setup the vector used for configuration events */
289 vp_iowrite16(vector, &vp_dev->common->msix_config);
290 /* Verify we had enough resources to assign the vector */
291 /* Will also flush the write out to device */
292 return vp_ioread16(&vp_dev->common->msix_config);
295 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
297 void (*callback)(struct virtqueue *vq),
301 struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
302 struct virtqueue *vq;
306 if (index >= vp_ioread16(&cfg->num_queues))
307 return ERR_PTR(-ENOENT);
309 /* Select the queue we're interested in */
310 vp_iowrite16(index, &cfg->queue_select);
312 /* Check if queue is either not available or already active. */
313 num = vp_ioread16(&cfg->queue_size);
314 if (!num || vp_ioread16(&cfg->queue_enable))
315 return ERR_PTR(-ENOENT);
317 if (num & (num - 1)) {
318 dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
319 return ERR_PTR(-EINVAL);
322 /* get offset of notification word for this vq */
323 off = vp_ioread16(&cfg->queue_notify_off);
325 /* create the vring */
326 vq = vring_create_virtqueue(index, num,
327 SMP_CACHE_BYTES, &vp_dev->vdev,
328 true, true, vp_notify, callback, name);
330 return ERR_PTR(-ENOMEM);
332 /* activate the queue */
333 vp_iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size);
334 vp_iowrite64_twopart(virtqueue_get_desc_addr(vq),
335 &cfg->queue_desc_lo, &cfg->queue_desc_hi);
336 vp_iowrite64_twopart(virtqueue_get_avail_addr(vq),
337 &cfg->queue_avail_lo, &cfg->queue_avail_hi);
338 vp_iowrite64_twopart(virtqueue_get_used_addr(vq),
339 &cfg->queue_used_lo, &cfg->queue_used_hi);
341 if (vp_dev->notify_base) {
342 /* offset should not wrap */
343 if ((u64)off * vp_dev->notify_offset_multiplier + 2
344 > vp_dev->notify_len) {
345 dev_warn(&vp_dev->pci_dev->dev,
346 "bad notification offset %u (x %u) "
347 "for queue %u > %zd",
348 off, vp_dev->notify_offset_multiplier,
349 index, vp_dev->notify_len);
353 vq->priv = (void __force *)vp_dev->notify_base +
354 off * vp_dev->notify_offset_multiplier;
356 vq->priv = (void __force *)map_capability(vp_dev->pci_dev,
357 vp_dev->notify_map_cap, 2, 2,
358 off * vp_dev->notify_offset_multiplier, 2,
367 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
368 vp_iowrite16(msix_vec, &cfg->queue_msix_vector);
369 msix_vec = vp_ioread16(&cfg->queue_msix_vector);
370 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
372 goto err_assign_vector;
379 if (!vp_dev->notify_base)
380 pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
382 vring_del_virtqueue(vq);
386 static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
387 struct virtqueue *vqs[], vq_callback_t *callbacks[],
388 const char * const names[], struct irq_affinity *desc)
390 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
391 struct virtqueue *vq;
392 int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, desc);
397 /* Select and activate all queues. Has to be done last: once we do
398 * this, there's no way to go back except reset.
400 list_for_each_entry(vq, &vdev->vqs, list) {
401 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
402 vp_iowrite16(1, &vp_dev->common->queue_enable);
408 static void del_vq(struct virtqueue *vq)
410 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
412 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
414 if (vp_dev->pci_dev->msix_enabled) {
415 vp_iowrite16(VIRTIO_MSI_NO_VECTOR,
416 &vp_dev->common->queue_msix_vector);
417 /* Flush the write out to device */
418 vp_ioread16(&vp_dev->common->queue_msix_vector);
421 if (!vp_dev->notify_base)
422 pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
424 vring_del_virtqueue(vq);
427 static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
430 .generation = vp_generation,
431 .get_status = vp_get_status,
432 .set_status = vp_set_status,
434 .find_vqs = vp_modern_find_vqs,
435 .del_vqs = vp_del_vqs,
436 .get_features = vp_get_features,
437 .finalize_features = vp_finalize_features,
438 .bus_name = vp_bus_name,
439 .set_vq_affinity = vp_set_vq_affinity,
440 .get_vq_affinity = vp_get_vq_affinity,
443 static const struct virtio_config_ops virtio_pci_config_ops = {
446 .generation = vp_generation,
447 .get_status = vp_get_status,
448 .set_status = vp_set_status,
450 .find_vqs = vp_modern_find_vqs,
451 .del_vqs = vp_del_vqs,
452 .get_features = vp_get_features,
453 .finalize_features = vp_finalize_features,
454 .bus_name = vp_bus_name,
455 .set_vq_affinity = vp_set_vq_affinity,
456 .get_vq_affinity = vp_get_vq_affinity,
460 * virtio_pci_find_capability - walk capabilities to find device info.
461 * @dev: the pci device
462 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
463 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
465 * Returns offset of the capability, or 0.
467 static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
468 u32 ioresource_types, int *bars)
472 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
474 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
476 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
479 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
483 /* Ignore structures with reserved BAR values */
487 if (type == cfg_type) {
488 if (pci_resource_len(dev, bar) &&
489 pci_resource_flags(dev, bar) & ioresource_types) {
498 /* This is part of the ABI. Don't screw with it. */
499 static inline void check_offsets(void)
501 /* Note: disk space was harmed in compilation of this function. */
502 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
503 offsetof(struct virtio_pci_cap, cap_vndr));
504 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
505 offsetof(struct virtio_pci_cap, cap_next));
506 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
507 offsetof(struct virtio_pci_cap, cap_len));
508 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
509 offsetof(struct virtio_pci_cap, cfg_type));
510 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
511 offsetof(struct virtio_pci_cap, bar));
512 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
513 offsetof(struct virtio_pci_cap, offset));
514 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
515 offsetof(struct virtio_pci_cap, length));
516 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
517 offsetof(struct virtio_pci_notify_cap,
518 notify_off_multiplier));
519 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
520 offsetof(struct virtio_pci_common_cfg,
521 device_feature_select));
522 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
523 offsetof(struct virtio_pci_common_cfg, device_feature));
524 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
525 offsetof(struct virtio_pci_common_cfg,
526 guest_feature_select));
527 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
528 offsetof(struct virtio_pci_common_cfg, guest_feature));
529 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
530 offsetof(struct virtio_pci_common_cfg, msix_config));
531 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
532 offsetof(struct virtio_pci_common_cfg, num_queues));
533 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
534 offsetof(struct virtio_pci_common_cfg, device_status));
535 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
536 offsetof(struct virtio_pci_common_cfg, config_generation));
537 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
538 offsetof(struct virtio_pci_common_cfg, queue_select));
539 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
540 offsetof(struct virtio_pci_common_cfg, queue_size));
541 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
542 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
543 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
544 offsetof(struct virtio_pci_common_cfg, queue_enable));
545 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
546 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
547 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
548 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
549 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
550 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
551 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
552 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
553 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
554 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
555 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
556 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
557 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
558 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
561 /* the PCI probing function */
562 int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
564 struct pci_dev *pci_dev = vp_dev->pci_dev;
565 int err, common, isr, notify, device;
571 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
572 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
575 if (pci_dev->device < 0x1040) {
576 /* Transitional devices: use the PCI subsystem device id as
577 * virtio device id, same as legacy driver always did.
579 vp_dev->vdev.id.device = pci_dev->subsystem_device;
581 /* Modern devices: simply use PCI device id, but start from 0x1040. */
582 vp_dev->vdev.id.device = pci_dev->device - 0x1040;
584 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
586 /* check for a common config: if not, use legacy mode (bar 0). */
587 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
588 IORESOURCE_IO | IORESOURCE_MEM,
589 &vp_dev->modern_bars);
591 dev_info(&pci_dev->dev,
592 "virtio_pci: leaving for legacy driver\n");
596 /* If common is there, these should be too... */
597 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
598 IORESOURCE_IO | IORESOURCE_MEM,
599 &vp_dev->modern_bars);
600 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
601 IORESOURCE_IO | IORESOURCE_MEM,
602 &vp_dev->modern_bars);
603 if (!isr || !notify) {
604 dev_err(&pci_dev->dev,
605 "virtio_pci: missing capabilities %i/%i/%i\n",
606 common, isr, notify);
610 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
612 err = dma_set_mask_and_coherent(&pci_dev->dev,
615 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
617 /* Device capability is only mandatory for devices that have
618 * device-specific configuration.
620 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
621 IORESOURCE_IO | IORESOURCE_MEM,
622 &vp_dev->modern_bars);
624 err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
625 "virtio-pci-modern");
630 vp_dev->common = map_capability(pci_dev, common,
631 sizeof(struct virtio_pci_common_cfg), 4,
632 0, sizeof(struct virtio_pci_common_cfg),
636 vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
642 /* Read notify_off_multiplier from config space. */
643 pci_read_config_dword(pci_dev,
644 notify + offsetof(struct virtio_pci_notify_cap,
645 notify_off_multiplier),
646 &vp_dev->notify_offset_multiplier);
647 /* Read notify length and offset from config space. */
648 pci_read_config_dword(pci_dev,
649 notify + offsetof(struct virtio_pci_notify_cap,
653 pci_read_config_dword(pci_dev,
654 notify + offsetof(struct virtio_pci_notify_cap,
658 /* We don't know how many VQs we'll map, ahead of the time.
659 * If notify length is small, map it all now.
660 * Otherwise, map each VQ individually later.
662 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
663 vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
665 &vp_dev->notify_len);
666 if (!vp_dev->notify_base)
669 vp_dev->notify_map_cap = notify;
672 /* Again, we don't know how much we should map, but PAGE_SIZE
673 * is more than enough for all existing devices.
676 vp_dev->device = map_capability(pci_dev, device, 0, 4,
678 &vp_dev->device_len);
682 vp_dev->vdev.config = &virtio_pci_config_ops;
684 vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
687 vp_dev->config_vector = vp_config_vector;
688 vp_dev->setup_vq = setup_vq;
689 vp_dev->del_vq = del_vq;
694 if (vp_dev->notify_base)
695 pci_iounmap(pci_dev, vp_dev->notify_base);
697 pci_iounmap(pci_dev, vp_dev->isr);
699 pci_iounmap(pci_dev, vp_dev->common);
704 void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
706 struct pci_dev *pci_dev = vp_dev->pci_dev;
709 pci_iounmap(pci_dev, vp_dev->device);
710 if (vp_dev->notify_base)
711 pci_iounmap(pci_dev, vp_dev->notify_base);
712 pci_iounmap(pci_dev, vp_dev->isr);
713 pci_iounmap(pci_dev, vp_dev->common);
714 pci_release_selected_regions(pci_dev, vp_dev->modern_bars);