virtio_pci_common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <[email protected]>
  15. * Rusty Russell <[email protected]>
  16. * Michael S. Tsirkin <[email protected]>
  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/virtio_pci_legacy.h>
  28. #include <linux/virtio_pci_modern.h>
  29. #include <linux/highmem.h>
  30. #include <linux/spinlock.h>
  31. struct virtio_pci_vq_info {
  32. /* the actual virtqueue */
  33. struct virtqueue *vq;
  34. /* the list node for the virtqueues list */
  35. struct list_head node;
  36. /* MSI-X vector (or none) */
  37. unsigned int msix_vector;
  38. };
  39. /* Our device structure */
  40. struct virtio_pci_device {
  41. struct virtio_device vdev;
  42. struct pci_dev *pci_dev;
  43. struct virtio_pci_legacy_device ldev;
  44. struct virtio_pci_modern_device mdev;
  45. bool is_legacy;
  46. /* Where to read and clear interrupt */
  47. u8 __iomem *isr;
  48. /* a list of queues so we can dispatch IRQs */
  49. spinlock_t lock;
  50. struct list_head virtqueues;
  51. /* array of all queues for house-keeping */
  52. struct virtio_pci_vq_info **vqs;
  53. /* MSI-X support */
  54. int msix_enabled;
  55. int intx_enabled;
  56. cpumask_var_t *msix_affinity_masks;
  57. /* Name strings for interrupts. This size should be enough,
  58. * and I'm too lazy to allocate each name separately. */
  59. char (*msix_names)[256];
  60. /* Number of available vectors */
  61. unsigned int msix_vectors;
  62. /* Vectors allocated, excluding per-vq vectors if any */
  63. unsigned int msix_used_vectors;
  64. /* Whether we have vector per vq */
  65. bool per_vq_vectors;
  66. struct virtqueue *(*setup_vq)(struct virtio_pci_device *vp_dev,
  67. struct virtio_pci_vq_info *info,
  68. unsigned int idx,
  69. void (*callback)(struct virtqueue *vq),
  70. const char *name,
  71. bool ctx,
  72. u16 msix_vec);
  73. void (*del_vq)(struct virtio_pci_vq_info *info);
  74. u16 (*config_vector)(struct virtio_pci_device *vp_dev, u16 vector);
  75. };
  76. /* Constants for MSI-X */
  77. /* Use first vector for configuration changes, second and the rest for
  78. * virtqueues Thus, we need at least 2 vectors for MSI. */
  79. enum {
  80. VP_MSIX_CONFIG_VECTOR = 0,
  81. VP_MSIX_VQ_VECTOR = 1,
  82. };
  83. /* Convert a generic virtio device to our structure */
  84. static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
  85. {
  86. return container_of(vdev, struct virtio_pci_device, vdev);
  87. }
  88. /* wait for pending irq handlers */
  89. void vp_synchronize_vectors(struct virtio_device *vdev);
  90. /* the notify function used when creating a virt queue */
  91. bool vp_notify(struct virtqueue *vq);
  92. /* the config->del_vqs() implementation */
  93. void vp_del_vqs(struct virtio_device *vdev);
  94. /* the config->find_vqs() implementation */
  95. int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  96. struct virtqueue *vqs[], vq_callback_t *callbacks[],
  97. const char * const names[], const bool *ctx,
  98. struct irq_affinity *desc);
  99. const char *vp_bus_name(struct virtio_device *vdev);
  100. /* Setup the affinity for a virtqueue:
  101. * - force the affinity for per vq vector
  102. * - OR over all affinities for shared MSI
  103. * - ignore the affinity request if we're using INTX
  104. */
  105. int vp_set_vq_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask);
  106. const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index);
  107. #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
  108. int virtio_pci_legacy_probe(struct virtio_pci_device *);
  109. void virtio_pci_legacy_remove(struct virtio_pci_device *);
  110. #else
  111. static inline int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
  112. {
  113. return -ENODEV;
  114. }
  115. static inline void virtio_pci_legacy_remove(struct virtio_pci_device *vp_dev)
  116. {
  117. }
  118. #endif
  119. int virtio_pci_modern_probe(struct virtio_pci_device *);
  120. void virtio_pci_modern_remove(struct virtio_pci_device *);
  121. #endif