virtio_pci_common.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/virtio_pci_legacy.h>
  28. #include <linux/virtio_pci_modern.h>
  29. #include <linux/highmem.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/mutex.h>
  32. struct virtio_pci_vq_info {
  33. /* the actual virtqueue */
  34. struct virtqueue *vq;
  35. /* the list node for the virtqueues or slow_virtqueues list */
  36. struct list_head node;
  37. /* MSI-X vector (or none) */
  38. unsigned int msix_vector;
  39. };
  40. struct virtio_pci_admin_vq {
  41. /* Virtqueue info associated with this admin queue. */
  42. struct virtio_pci_vq_info *info;
  43. /* Protects virtqueue access. */
  44. spinlock_t lock;
  45. u64 supported_cmds;
  46. /* Name of the admin queue: avq.$vq_index. */
  47. char name[10];
  48. u16 vq_index;
  49. };
  50. /* Our device structure */
  51. struct virtio_pci_device {
  52. struct virtio_device vdev;
  53. struct pci_dev *pci_dev;
  54. union {
  55. struct virtio_pci_legacy_device ldev;
  56. struct virtio_pci_modern_device mdev;
  57. };
  58. bool is_legacy;
  59. /* Where to read and clear interrupt */
  60. u8 __iomem *isr;
  61. /* Lists of queues and potentially slow path queues
  62. * so we can dispatch IRQs.
  63. */
  64. spinlock_t lock;
  65. struct list_head virtqueues;
  66. struct list_head slow_virtqueues;
  67. /* Array of all virtqueues reported in the
  68. * PCI common config num_queues field
  69. */
  70. struct virtio_pci_vq_info **vqs;
  71. struct virtio_pci_admin_vq admin_vq;
  72. /* MSI-X support */
  73. int msix_enabled;
  74. int intx_enabled;
  75. cpumask_var_t *msix_affinity_masks;
  76. /* Name strings for interrupts. This size should be enough,
  77. * and I'm too lazy to allocate each name separately. */
  78. char (*msix_names)[256];
  79. /* Number of available vectors */
  80. unsigned int msix_vectors;
  81. /* Vectors allocated, excluding per-vq vectors if any */
  82. unsigned int msix_used_vectors;
  83. /* Whether we have vector per vq */
  84. bool per_vq_vectors;
  85. struct virtqueue *(*setup_vq)(struct virtio_pci_device *vp_dev,
  86. struct virtio_pci_vq_info *info,
  87. unsigned int idx,
  88. void (*callback)(struct virtqueue *vq),
  89. const char *name,
  90. bool ctx,
  91. u16 msix_vec);
  92. void (*del_vq)(struct virtio_pci_vq_info *info);
  93. u16 (*config_vector)(struct virtio_pci_device *vp_dev, u16 vector);
  94. int (*avq_index)(struct virtio_device *vdev, u16 *index, u16 *num);
  95. };
  96. /* Constants for MSI-X */
  97. /* Use first vector for configuration changes, second and the rest for
  98. * virtqueues Thus, we need at least 2 vectors for MSI. */
  99. enum {
  100. VP_MSIX_CONFIG_VECTOR = 0,
  101. VP_MSIX_VQ_VECTOR = 1,
  102. };
  103. /* Convert a generic virtio device to our structure */
  104. static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
  105. {
  106. return container_of(vdev, struct virtio_pci_device, vdev);
  107. }
  108. /* wait for pending irq handlers */
  109. void vp_synchronize_vectors(struct virtio_device *vdev);
  110. /* the notify function used when creating a virt queue */
  111. bool vp_notify(struct virtqueue *vq);
  112. /* the config->del_vqs() implementation */
  113. void vp_del_vqs(struct virtio_device *vdev);
  114. /* the config->find_vqs() implementation */
  115. int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  116. struct virtqueue *vqs[], struct virtqueue_info vqs_info[],
  117. struct irq_affinity *desc);
  118. const char *vp_bus_name(struct virtio_device *vdev);
  119. /* Setup the affinity for a virtqueue:
  120. * - force the affinity for per vq vector
  121. * - OR over all affinities for shared MSI
  122. * - ignore the affinity request if we're using INTX
  123. */
  124. int vp_set_vq_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask);
  125. const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index);
  126. #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
  127. int virtio_pci_legacy_probe(struct virtio_pci_device *);
  128. void virtio_pci_legacy_remove(struct virtio_pci_device *);
  129. #else
  130. static inline int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
  131. {
  132. return -ENODEV;
  133. }
  134. static inline void virtio_pci_legacy_remove(struct virtio_pci_device *vp_dev)
  135. {
  136. }
  137. #endif
  138. int virtio_pci_modern_probe(struct virtio_pci_device *);
  139. void virtio_pci_modern_remove(struct virtio_pci_device *);
  140. struct virtio_device *virtio_pci_vf_get_pf_dev(struct pci_dev *pdev);
  141. #define VIRTIO_LEGACY_ADMIN_CMD_BITMAP \
  142. (BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE) | \
  143. BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ) | \
  144. BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE) | \
  145. BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ) | \
  146. BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO))
  147. /* Unlike modern drivers which support hardware virtio devices, legacy drivers
  148. * assume software-based devices: e.g. they don't use proper memory barriers
  149. * on ARM, use big endian on PPC, etc. X86 drivers are mostly ok though, more
  150. * or less by chance. For now, only support legacy IO on X86.
  151. */
  152. #ifdef CONFIG_VIRTIO_PCI_ADMIN_LEGACY
  153. #define VIRTIO_ADMIN_CMD_BITMAP VIRTIO_LEGACY_ADMIN_CMD_BITMAP
  154. #else
  155. #define VIRTIO_ADMIN_CMD_BITMAP 0
  156. #endif
  157. bool vp_is_avq(struct virtio_device *vdev, unsigned int index);
  158. void vp_modern_avq_done(struct virtqueue *vq);
  159. int vp_modern_admin_cmd_exec(struct virtio_device *vdev,
  160. struct virtio_admin_cmd *cmd);
  161. #endif