virtio_pci_common.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _DRIVERS_VIRTIO_VIRTIO_PCI_COMMON_H
  3. #define _DRIVERS_VIRTIO_VIRTIO_PCI_COMMON_H
  4. /*
  5. * Virtio PCI driver - APIs for common functionality for all device versions
  6. *
  7. * This module allows virtio devices to be used over a virtual PCI device.
  8. * This can be used with QEMU based VMMs like KVM or Xen.
  9. *
  10. * Copyright IBM Corp. 2007
  11. * Copyright Red Hat, Inc. 2014
  12. *
  13. * Authors:
  14. * Anthony Liguori <aliguori@us.ibm.com>
  15. * Rusty Russell <rusty@rustcorp.com.au>
  16. * Michael S. Tsirkin <mst@redhat.com>
  17. */
  18. #include <linux/module.h>
  19. #include <linux/list.h>
  20. #include <linux/pci.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/virtio.h>
  24. #include <linux/virtio_config.h>
  25. #include <linux/virtio_ring.h>
  26. #include <linux/virtio_pci.h>
  27. #include <linux/highmem.h>
  28. #include <linux/spinlock.h>
  29. struct virtio_pci_vq_info {
  30. /* the actual virtqueue */
  31. struct virtqueue *vq;
  32. /* the list node for the virtqueues list */
  33. struct list_head node;
  34. /* MSI-X vector (or none) */
  35. unsigned msix_vector;
  36. };
  37. /* Our device structure */
  38. struct virtio_pci_device {
  39. struct virtio_device vdev;
  40. struct pci_dev *pci_dev;
  41. /* In legacy mode, these two point to within ->legacy. */
  42. /* Where to read and clear interrupt */
  43. u8 __iomem *isr;
  44. /* Modern only fields */
  45. /* The IO mapping for the PCI config space (non-legacy mode) */
  46. struct virtio_pci_common_cfg __iomem *common;
  47. /* Device-specific data (non-legacy mode) */
  48. void __iomem *device;
  49. /* Base of vq notifications (non-legacy mode). */
  50. void __iomem *notify_base;
  51. /* So we can sanity-check accesses. */
  52. size_t notify_len;
  53. size_t device_len;
  54. /* Capability for when we need to map notifications per-vq. */
  55. int notify_map_cap;
  56. /* Multiply queue_notify_off by this value. (non-legacy mode). */
  57. u32 notify_offset_multiplier;
  58. int modern_bars;
  59. /* Legacy only field */
  60. /* the IO mapping for the PCI config space */
  61. void __iomem *ioaddr;
  62. /* a list of queues so we can dispatch IRQs */
  63. spinlock_t lock;
  64. struct list_head virtqueues;
  65. /* array of all queues for house-keeping */
  66. struct virtio_pci_vq_info **vqs;
  67. /* MSI-X support */
  68. int msix_enabled;
  69. int intx_enabled;
  70. cpumask_var_t *msix_affinity_masks;
  71. /* Name strings for interrupts. This size should be enough,
  72. * and I'm too lazy to allocate each name separately. */
  73. char (*msix_names)[256];
  74. /* Number of available vectors */
  75. unsigned msix_vectors;
  76. /* Vectors allocated, excluding per-vq vectors if any */
  77. unsigned msix_used_vectors;
  78. /* Whether we have vector per vq */
  79. bool per_vq_vectors;
  80. struct virtqueue *(*setup_vq)(struct virtio_pci_device *vp_dev,
  81. struct virtio_pci_vq_info *info,
  82. unsigned idx,
  83. void (*callback)(struct virtqueue *vq),
  84. const char *name,
  85. bool ctx,
  86. u16 msix_vec);
  87. void (*del_vq)(struct virtio_pci_vq_info *info);
  88. u16 (*config_vector)(struct virtio_pci_device *vp_dev, u16 vector);
  89. };
  90. /* Constants for MSI-X */
  91. /* Use first vector for configuration changes, second and the rest for
  92. * virtqueues Thus, we need at least 2 vectors for MSI. */
  93. enum {
  94. VP_MSIX_CONFIG_VECTOR = 0,
  95. VP_MSIX_VQ_VECTOR = 1,
  96. };
  97. /* Convert a generic virtio device to our structure */
  98. static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
  99. {
  100. return container_of(vdev, struct virtio_pci_device, vdev);
  101. }
  102. /* wait for pending irq handlers */
  103. void vp_synchronize_vectors(struct virtio_device *vdev);
  104. /* the notify function used when creating a virt queue */
  105. bool vp_notify(struct virtqueue *vq);
  106. /* the config->del_vqs() implementation */
  107. void vp_del_vqs(struct virtio_device *vdev);
  108. /* the config->find_vqs() implementation */
  109. int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  110. struct virtqueue *vqs[], vq_callback_t *callbacks[],
  111. const char * const names[], const bool *ctx,
  112. struct irq_affinity *desc);
  113. const char *vp_bus_name(struct virtio_device *vdev);
  114. /* Setup the affinity for a virtqueue:
  115. * - force the affinity for per vq vector
  116. * - OR over all affinities for shared MSI
  117. * - ignore the affinity request if we're using INTX
  118. */
  119. int vp_set_vq_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask);
  120. const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index);
  121. #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
  122. int virtio_pci_legacy_probe(struct virtio_pci_device *);
  123. void virtio_pci_legacy_remove(struct virtio_pci_device *);
  124. #else
  125. static inline int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
  126. {
  127. return -ENODEV;
  128. }
  129. static inline void virtio_pci_legacy_remove(struct virtio_pci_device *vp_dev)
  130. {
  131. }
  132. #endif
  133. int virtio_pci_modern_probe(struct virtio_pci_device *);
  134. void virtio_pci_modern_remove(struct virtio_pci_device *);
  135. #endif