2
0

virtio-pci.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Virtio PCI Bindings
  3. *
  4. * Copyright IBM, Corp. 2007
  5. * Copyright (c) 2009 CodeSourcery
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. * Paul Brook <paul@codesourcery.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2. See
  12. * the COPYING file in the top-level directory.
  13. */
  14. #ifndef QEMU_VIRTIO_PCI_H
  15. #define QEMU_VIRTIO_PCI_H
  16. #include "hw/pci/msi.h"
  17. #include "hw/virtio/virtio-bus.h"
  18. typedef struct VirtIOPCIProxy VirtIOPCIProxy;
  19. /* virtio-pci-bus */
  20. typedef struct VirtioBusState VirtioPCIBusState;
  21. typedef struct VirtioBusClass VirtioPCIBusClass;
  22. #define TYPE_VIRTIO_PCI_BUS "virtio-pci-bus"
  23. #define VIRTIO_PCI_BUS(obj) \
  24. OBJECT_CHECK(VirtioPCIBusState, (obj), TYPE_VIRTIO_PCI_BUS)
  25. #define VIRTIO_PCI_BUS_GET_CLASS(obj) \
  26. OBJECT_GET_CLASS(VirtioPCIBusClass, obj, TYPE_VIRTIO_PCI_BUS)
  27. #define VIRTIO_PCI_BUS_CLASS(klass) \
  28. OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
  29. enum {
  30. VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT,
  31. VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT,
  32. VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT,
  33. VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT,
  34. VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT,
  35. VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT,
  36. VIRTIO_PCI_FLAG_ATS_BIT,
  37. VIRTIO_PCI_FLAG_INIT_DEVERR_BIT,
  38. VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT,
  39. VIRTIO_PCI_FLAG_INIT_PM_BIT,
  40. VIRTIO_PCI_FLAG_INIT_FLR_BIT,
  41. };
  42. /* Need to activate work-arounds for buggy guests at vmstate load. */
  43. #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
  44. (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
  45. /* Performance improves when virtqueue kick processing is decoupled from the
  46. * vcpu thread using ioeventfd for some devices. */
  47. #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
  48. /* virtio version flags */
  49. #define VIRTIO_PCI_FLAG_DISABLE_PCIE (1 << VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT)
  50. /* migrate extra state */
  51. #define VIRTIO_PCI_FLAG_MIGRATE_EXTRA (1 << VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT)
  52. /* have pio notification for modern device ? */
  53. #define VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY \
  54. (1 << VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT)
  55. /* page per vq flag to be used by split drivers within guests */
  56. #define VIRTIO_PCI_FLAG_PAGE_PER_VQ \
  57. (1 << VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT)
  58. /* address space translation service */
  59. #define VIRTIO_PCI_FLAG_ATS (1 << VIRTIO_PCI_FLAG_ATS_BIT)
  60. /* Init error enabling flags */
  61. #define VIRTIO_PCI_FLAG_INIT_DEVERR (1 << VIRTIO_PCI_FLAG_INIT_DEVERR_BIT)
  62. /* Init Link Control register */
  63. #define VIRTIO_PCI_FLAG_INIT_LNKCTL (1 << VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT)
  64. /* Init Power Management */
  65. #define VIRTIO_PCI_FLAG_INIT_PM (1 << VIRTIO_PCI_FLAG_INIT_PM_BIT)
  66. /* Init Function Level Reset capability */
  67. #define VIRTIO_PCI_FLAG_INIT_FLR (1 << VIRTIO_PCI_FLAG_INIT_FLR_BIT)
  68. typedef struct {
  69. MSIMessage msg;
  70. int virq;
  71. unsigned int users;
  72. } VirtIOIRQFD;
  73. /*
  74. * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
  75. */
  76. #define TYPE_VIRTIO_PCI "virtio-pci"
  77. #define VIRTIO_PCI_GET_CLASS(obj) \
  78. OBJECT_GET_CLASS(VirtioPCIClass, obj, TYPE_VIRTIO_PCI)
  79. #define VIRTIO_PCI_CLASS(klass) \
  80. OBJECT_CLASS_CHECK(VirtioPCIClass, klass, TYPE_VIRTIO_PCI)
  81. #define VIRTIO_PCI(obj) \
  82. OBJECT_CHECK(VirtIOPCIProxy, (obj), TYPE_VIRTIO_PCI)
  83. typedef struct VirtioPCIClass {
  84. PCIDeviceClass parent_class;
  85. DeviceRealize parent_dc_realize;
  86. void (*realize)(VirtIOPCIProxy *vpci_dev, Error **errp);
  87. } VirtioPCIClass;
  88. typedef struct VirtIOPCIRegion {
  89. MemoryRegion mr;
  90. uint32_t offset;
  91. uint32_t size;
  92. uint32_t type;
  93. } VirtIOPCIRegion;
  94. typedef struct VirtIOPCIQueue {
  95. uint16_t num;
  96. bool enabled;
  97. uint32_t desc[2];
  98. uint32_t avail[2];
  99. uint32_t used[2];
  100. } VirtIOPCIQueue;
  101. struct VirtIOPCIProxy {
  102. PCIDevice pci_dev;
  103. MemoryRegion bar;
  104. union {
  105. struct {
  106. VirtIOPCIRegion common;
  107. VirtIOPCIRegion isr;
  108. VirtIOPCIRegion device;
  109. VirtIOPCIRegion notify;
  110. VirtIOPCIRegion notify_pio;
  111. };
  112. VirtIOPCIRegion regs[5];
  113. };
  114. MemoryRegion modern_bar;
  115. MemoryRegion io_bar;
  116. uint32_t legacy_io_bar_idx;
  117. uint32_t msix_bar_idx;
  118. uint32_t modern_io_bar_idx;
  119. uint32_t modern_mem_bar_idx;
  120. int config_cap;
  121. uint32_t flags;
  122. bool disable_modern;
  123. bool ignore_backend_features;
  124. OnOffAuto disable_legacy;
  125. uint32_t class_code;
  126. uint32_t nvectors;
  127. uint32_t dfselect;
  128. uint32_t gfselect;
  129. uint32_t guest_features[2];
  130. VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX];
  131. VirtIOIRQFD *vector_irqfd;
  132. int nvqs_with_notifiers;
  133. VirtioBusState bus;
  134. };
  135. static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy)
  136. {
  137. return !proxy->disable_modern;
  138. }
  139. static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy)
  140. {
  141. return proxy->disable_legacy == ON_OFF_AUTO_OFF;
  142. }
  143. static inline void virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy)
  144. {
  145. proxy->disable_modern = false;
  146. proxy->disable_legacy = ON_OFF_AUTO_ON;
  147. }
  148. static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy)
  149. {
  150. proxy->disable_modern = true;
  151. }
  152. /*
  153. * virtio-input-pci: This extends VirtioPCIProxy.
  154. */
  155. #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci"
  156. /* Virtio ABI version, if we increment this, we break the guest driver. */
  157. #define VIRTIO_PCI_ABI_VERSION 0
  158. /* Input for virtio_pci_types_register() */
  159. typedef struct VirtioPCIDeviceTypeInfo {
  160. /*
  161. * Common base class for the subclasses below.
  162. *
  163. * Required only if transitional_name or non_transitional_name is set.
  164. *
  165. * We need a separate base type instead of making all types
  166. * inherit from generic_name for two reasons:
  167. * 1) generic_name implements INTERFACE_PCIE_DEVICE, but
  168. * transitional_name does not.
  169. * 2) generic_name has the "disable-legacy" and "disable-modern"
  170. * properties, transitional_name and non_transitional name don't.
  171. */
  172. const char *base_name;
  173. /*
  174. * Generic device type. Optional.
  175. *
  176. * Supports both transitional and non-transitional modes,
  177. * using the disable-legacy and disable-modern properties.
  178. * If disable-legacy=auto, (non-)transitional mode is selected
  179. * depending on the bus where the device is plugged.
  180. *
  181. * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE,
  182. * but PCI Express is supported only in non-transitional mode.
  183. *
  184. * The only type implemented by QEMU 3.1 and older.
  185. */
  186. const char *generic_name;
  187. /*
  188. * The transitional device type. Optional.
  189. *
  190. * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE.
  191. */
  192. const char *transitional_name;
  193. /*
  194. * The non-transitional device type. Optional.
  195. *
  196. * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only.
  197. */
  198. const char *non_transitional_name;
  199. /* Parent type. If NULL, TYPE_VIRTIO_PCI is used */
  200. const char *parent;
  201. /* Same as TypeInfo fields: */
  202. size_t instance_size;
  203. size_t class_size;
  204. void (*instance_init)(Object *obj);
  205. void (*class_init)(ObjectClass *klass, void *data);
  206. InterfaceInfo *interfaces;
  207. } VirtioPCIDeviceTypeInfo;
  208. /* Register virtio-pci type(s). @t must be static. */
  209. void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t);
  210. #endif