virtio_pci_common.h 6.3 KB

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