pci.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. #ifndef QEMU_PCI_H
  2. #define QEMU_PCI_H
  3. #include "exec/memory.h"
  4. #include "sysemu/dma.h"
  5. /* PCI includes legacy ISA access. */
  6. #include "hw/isa/isa.h"
  7. extern bool pci_available;
  8. /* PCI bus */
  9. #define PCI_DEVFN(slot, func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))
  10. #define PCI_BUS_NUM(x) (((x) >> 8) & 0xff)
  11. #define PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f)
  12. #define PCI_FUNC(devfn) ((devfn) & 0x07)
  13. #define PCI_BUILD_BDF(bus, devfn) ((bus << 8) | (devfn))
  14. #define PCI_BDF_TO_DEVFN(x) ((x) & 0xff)
  15. #define PCI_BUS_MAX 256
  16. #define PCI_DEVFN_MAX 256
  17. #define PCI_SLOT_MAX 32
  18. #define PCI_FUNC_MAX 8
  19. /* Class, Vendor and Device IDs from Linux's pci_ids.h */
  20. #include "hw/pci/pci_ids.h"
  21. /* QEMU-specific Vendor and Device ID definitions */
  22. /* IBM (0x1014) */
  23. #define PCI_DEVICE_ID_IBM_440GX 0x027f
  24. #define PCI_DEVICE_ID_IBM_OPENPIC2 0xffff
  25. /* Hitachi (0x1054) */
  26. #define PCI_VENDOR_ID_HITACHI 0x1054
  27. #define PCI_DEVICE_ID_HITACHI_SH7751R 0x350e
  28. /* Apple (0x106b) */
  29. #define PCI_DEVICE_ID_APPLE_343S1201 0x0010
  30. #define PCI_DEVICE_ID_APPLE_UNI_N_I_PCI 0x001e
  31. #define PCI_DEVICE_ID_APPLE_UNI_N_PCI 0x001f
  32. #define PCI_DEVICE_ID_APPLE_UNI_N_KEYL 0x0022
  33. #define PCI_DEVICE_ID_APPLE_IPID_USB 0x003f
  34. /* Realtek (0x10ec) */
  35. #define PCI_DEVICE_ID_REALTEK_8029 0x8029
  36. /* Xilinx (0x10ee) */
  37. #define PCI_DEVICE_ID_XILINX_XC2VP30 0x0300
  38. /* Marvell (0x11ab) */
  39. #define PCI_DEVICE_ID_MARVELL_GT6412X 0x4620
  40. /* QEMU/Bochs VGA (0x1234) */
  41. #define PCI_VENDOR_ID_QEMU 0x1234
  42. #define PCI_DEVICE_ID_QEMU_VGA 0x1111
  43. #define PCI_DEVICE_ID_QEMU_IPMI 0x1112
  44. /* VMWare (0x15ad) */
  45. #define PCI_VENDOR_ID_VMWARE 0x15ad
  46. #define PCI_DEVICE_ID_VMWARE_SVGA2 0x0405
  47. #define PCI_DEVICE_ID_VMWARE_SVGA 0x0710
  48. #define PCI_DEVICE_ID_VMWARE_NET 0x0720
  49. #define PCI_DEVICE_ID_VMWARE_SCSI 0x0730
  50. #define PCI_DEVICE_ID_VMWARE_PVSCSI 0x07C0
  51. #define PCI_DEVICE_ID_VMWARE_IDE 0x1729
  52. #define PCI_DEVICE_ID_VMWARE_VMXNET3 0x07B0
  53. /* Intel (0x8086) */
  54. #define PCI_DEVICE_ID_INTEL_82551IT 0x1209
  55. #define PCI_DEVICE_ID_INTEL_82557 0x1229
  56. #define PCI_DEVICE_ID_INTEL_82801IR 0x2922
  57. /* Red Hat / Qumranet (for QEMU) -- see pci-ids.txt */
  58. #define PCI_VENDOR_ID_REDHAT_QUMRANET 0x1af4
  59. #define PCI_SUBVENDOR_ID_REDHAT_QUMRANET 0x1af4
  60. #define PCI_SUBDEVICE_ID_QEMU 0x1100
  61. /* legacy virtio-pci devices */
  62. #define PCI_DEVICE_ID_VIRTIO_NET 0x1000
  63. #define PCI_DEVICE_ID_VIRTIO_BLOCK 0x1001
  64. #define PCI_DEVICE_ID_VIRTIO_BALLOON 0x1002
  65. #define PCI_DEVICE_ID_VIRTIO_CONSOLE 0x1003
  66. #define PCI_DEVICE_ID_VIRTIO_SCSI 0x1004
  67. #define PCI_DEVICE_ID_VIRTIO_RNG 0x1005
  68. #define PCI_DEVICE_ID_VIRTIO_9P 0x1009
  69. #define PCI_DEVICE_ID_VIRTIO_VSOCK 0x1012
  70. /*
  71. * modern virtio-pci devices get their id assigned automatically,
  72. * there is no need to add #defines here. It gets calculated as
  73. *
  74. * PCI_DEVICE_ID = PCI_DEVICE_ID_VIRTIO_10_BASE +
  75. * virtio_bus_get_vdev_id(bus)
  76. */
  77. #define PCI_DEVICE_ID_VIRTIO_10_BASE 0x1040
  78. #define PCI_VENDOR_ID_REDHAT 0x1b36
  79. #define PCI_DEVICE_ID_REDHAT_BRIDGE 0x0001
  80. #define PCI_DEVICE_ID_REDHAT_SERIAL 0x0002
  81. #define PCI_DEVICE_ID_REDHAT_SERIAL2 0x0003
  82. #define PCI_DEVICE_ID_REDHAT_SERIAL4 0x0004
  83. #define PCI_DEVICE_ID_REDHAT_TEST 0x0005
  84. #define PCI_DEVICE_ID_REDHAT_ROCKER 0x0006
  85. #define PCI_DEVICE_ID_REDHAT_SDHCI 0x0007
  86. #define PCI_DEVICE_ID_REDHAT_PCIE_HOST 0x0008
  87. #define PCI_DEVICE_ID_REDHAT_PXB 0x0009
  88. #define PCI_DEVICE_ID_REDHAT_BRIDGE_SEAT 0x000a
  89. #define PCI_DEVICE_ID_REDHAT_PXB_PCIE 0x000b
  90. #define PCI_DEVICE_ID_REDHAT_PCIE_RP 0x000c
  91. #define PCI_DEVICE_ID_REDHAT_XHCI 0x000d
  92. #define PCI_DEVICE_ID_REDHAT_PCIE_BRIDGE 0x000e
  93. #define PCI_DEVICE_ID_REDHAT_MDPY 0x000f
  94. #define PCI_DEVICE_ID_REDHAT_NVME 0x0010
  95. #define PCI_DEVICE_ID_REDHAT_PVPANIC 0x0011
  96. #define PCI_DEVICE_ID_REDHAT_ACPI_ERST 0x0012
  97. #define PCI_DEVICE_ID_REDHAT_QXL 0x0100
  98. #define FMT_PCIBUS PRIx64
  99. typedef uint64_t pcibus_t;
  100. struct PCIHostDeviceAddress {
  101. unsigned int domain;
  102. unsigned int bus;
  103. unsigned int slot;
  104. unsigned int function;
  105. };
  106. typedef void PCIConfigWriteFunc(PCIDevice *pci_dev,
  107. uint32_t address, uint32_t data, int len);
  108. typedef uint32_t PCIConfigReadFunc(PCIDevice *pci_dev,
  109. uint32_t address, int len);
  110. typedef void PCIMapIORegionFunc(PCIDevice *pci_dev, int region_num,
  111. pcibus_t addr, pcibus_t size, int type);
  112. typedef void PCIUnregisterFunc(PCIDevice *pci_dev);
  113. typedef void MSITriggerFunc(PCIDevice *dev, MSIMessage msg);
  114. typedef MSIMessage MSIPrepareMessageFunc(PCIDevice *dev, unsigned vector);
  115. typedef MSIMessage MSIxPrepareMessageFunc(PCIDevice *dev, unsigned vector);
  116. typedef struct PCIIORegion {
  117. pcibus_t addr; /* current PCI mapping address. -1 means not mapped */
  118. #define PCI_BAR_UNMAPPED (~(pcibus_t)0)
  119. pcibus_t size;
  120. uint8_t type;
  121. MemoryRegion *memory;
  122. MemoryRegion *address_space;
  123. } PCIIORegion;
  124. #define PCI_ROM_SLOT 6
  125. #define PCI_NUM_REGIONS 7
  126. enum {
  127. QEMU_PCI_VGA_MEM,
  128. QEMU_PCI_VGA_IO_LO,
  129. QEMU_PCI_VGA_IO_HI,
  130. QEMU_PCI_VGA_NUM_REGIONS,
  131. };
  132. #define QEMU_PCI_VGA_MEM_BASE 0xa0000
  133. #define QEMU_PCI_VGA_MEM_SIZE 0x20000
  134. #define QEMU_PCI_VGA_IO_LO_BASE 0x3b0
  135. #define QEMU_PCI_VGA_IO_LO_SIZE 0xc
  136. #define QEMU_PCI_VGA_IO_HI_BASE 0x3c0
  137. #define QEMU_PCI_VGA_IO_HI_SIZE 0x20
  138. #include "hw/pci/pci_regs.h"
  139. #include "hw/pci/pcie.h"
  140. /* PCI HEADER_TYPE */
  141. #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
  142. /* Size of the standard PCI config header */
  143. #define PCI_CONFIG_HEADER_SIZE 0x40
  144. /* Size of the standard PCI config space */
  145. #define PCI_CONFIG_SPACE_SIZE 0x100
  146. /* Size of the standard PCIe config space: 4KB */
  147. #define PCIE_CONFIG_SPACE_SIZE 0x1000
  148. #define PCI_NUM_PINS 4 /* A-D */
  149. /* Bits in cap_present field. */
  150. enum {
  151. QEMU_PCI_CAP_MSI = 0x1,
  152. QEMU_PCI_CAP_MSIX = 0x2,
  153. QEMU_PCI_CAP_EXPRESS = 0x4,
  154. /* multifunction capable device */
  155. #define QEMU_PCI_CAP_MULTIFUNCTION_BITNR 3
  156. QEMU_PCI_CAP_MULTIFUNCTION = (1 << QEMU_PCI_CAP_MULTIFUNCTION_BITNR),
  157. /* command register SERR bit enabled - unused since QEMU v5.0 */
  158. #define QEMU_PCI_CAP_SERR_BITNR 4
  159. QEMU_PCI_CAP_SERR = (1 << QEMU_PCI_CAP_SERR_BITNR),
  160. /* Standard hot plug controller. */
  161. #define QEMU_PCI_SHPC_BITNR 5
  162. QEMU_PCI_CAP_SHPC = (1 << QEMU_PCI_SHPC_BITNR),
  163. #define QEMU_PCI_SLOTID_BITNR 6
  164. QEMU_PCI_CAP_SLOTID = (1 << QEMU_PCI_SLOTID_BITNR),
  165. /* PCI Express capability - Power Controller Present */
  166. #define QEMU_PCIE_SLTCAP_PCP_BITNR 7
  167. QEMU_PCIE_SLTCAP_PCP = (1 << QEMU_PCIE_SLTCAP_PCP_BITNR),
  168. /* Link active status in endpoint capability is always set */
  169. #define QEMU_PCIE_LNKSTA_DLLLA_BITNR 8
  170. QEMU_PCIE_LNKSTA_DLLLA = (1 << QEMU_PCIE_LNKSTA_DLLLA_BITNR),
  171. #define QEMU_PCIE_EXTCAP_INIT_BITNR 9
  172. QEMU_PCIE_EXTCAP_INIT = (1 << QEMU_PCIE_EXTCAP_INIT_BITNR),
  173. #define QEMU_PCIE_CXL_BITNR 10
  174. QEMU_PCIE_CAP_CXL = (1 << QEMU_PCIE_CXL_BITNR),
  175. };
  176. #define TYPE_PCI_DEVICE "pci-device"
  177. typedef struct PCIDeviceClass PCIDeviceClass;
  178. DECLARE_OBJ_CHECKERS(PCIDevice, PCIDeviceClass,
  179. PCI_DEVICE, TYPE_PCI_DEVICE)
  180. /*
  181. * Implemented by devices that can be plugged on CXL buses. In the spec, this is
  182. * actually a "CXL Component, but we name it device to match the PCI naming.
  183. */
  184. #define INTERFACE_CXL_DEVICE "cxl-device"
  185. /* Implemented by devices that can be plugged on PCI Express buses */
  186. #define INTERFACE_PCIE_DEVICE "pci-express-device"
  187. /* Implemented by devices that can be plugged on Conventional PCI buses */
  188. #define INTERFACE_CONVENTIONAL_PCI_DEVICE "conventional-pci-device"
  189. typedef struct PCIINTxRoute {
  190. enum {
  191. PCI_INTX_ENABLED,
  192. PCI_INTX_INVERTED,
  193. PCI_INTX_DISABLED,
  194. } mode;
  195. int irq;
  196. } PCIINTxRoute;
  197. struct PCIDeviceClass {
  198. DeviceClass parent_class;
  199. void (*realize)(PCIDevice *dev, Error **errp);
  200. PCIUnregisterFunc *exit;
  201. PCIConfigReadFunc *config_read;
  202. PCIConfigWriteFunc *config_write;
  203. uint16_t vendor_id;
  204. uint16_t device_id;
  205. uint8_t revision;
  206. uint16_t class_id;
  207. uint16_t subsystem_vendor_id; /* only for header type = 0 */
  208. uint16_t subsystem_id; /* only for header type = 0 */
  209. const char *romfile; /* rom bar */
  210. };
  211. typedef void (*PCIINTxRoutingNotifier)(PCIDevice *dev);
  212. typedef int (*MSIVectorUseNotifier)(PCIDevice *dev, unsigned int vector,
  213. MSIMessage msg);
  214. typedef void (*MSIVectorReleaseNotifier)(PCIDevice *dev, unsigned int vector);
  215. typedef void (*MSIVectorPollNotifier)(PCIDevice *dev,
  216. unsigned int vector_start,
  217. unsigned int vector_end);
  218. enum PCIReqIDType {
  219. PCI_REQ_ID_INVALID = 0,
  220. PCI_REQ_ID_BDF,
  221. PCI_REQ_ID_SECONDARY_BUS,
  222. PCI_REQ_ID_MAX,
  223. };
  224. typedef enum PCIReqIDType PCIReqIDType;
  225. struct PCIReqIDCache {
  226. PCIDevice *dev;
  227. PCIReqIDType type;
  228. };
  229. typedef struct PCIReqIDCache PCIReqIDCache;
  230. struct PCIDevice {
  231. DeviceState qdev;
  232. bool partially_hotplugged;
  233. bool has_power;
  234. /* PCI config space */
  235. uint8_t *config;
  236. /*
  237. * Used to enable config checks on load. Note that writable bits are
  238. * never checked even if set in cmask.
  239. */
  240. uint8_t *cmask;
  241. /* Used to implement R/W bytes */
  242. uint8_t *wmask;
  243. /* Used to implement RW1C(Write 1 to Clear) bytes */
  244. uint8_t *w1cmask;
  245. /* Used to allocate config space for capabilities. */
  246. uint8_t *used;
  247. /* the following fields are read only */
  248. int32_t devfn;
  249. /*
  250. * Cached device to fetch requester ID from, to avoid the PCI tree
  251. * walking every time we invoke PCI request (e.g., MSI). For
  252. * conventional PCI root complex, this field is meaningless.
  253. */
  254. PCIReqIDCache requester_id_cache;
  255. char name[64];
  256. PCIIORegion io_regions[PCI_NUM_REGIONS];
  257. AddressSpace bus_master_as;
  258. MemoryRegion bus_master_container_region;
  259. MemoryRegion bus_master_enable_region;
  260. /* do not access the following fields */
  261. PCIConfigReadFunc *config_read;
  262. PCIConfigWriteFunc *config_write;
  263. /* Legacy PCI VGA regions */
  264. MemoryRegion *vga_regions[QEMU_PCI_VGA_NUM_REGIONS];
  265. bool has_vga;
  266. /* Current IRQ levels. Used internally by the generic PCI code. */
  267. uint8_t irq_state;
  268. /* Capability bits */
  269. uint32_t cap_present;
  270. /* Offset of MSI-X capability in config space */
  271. uint8_t msix_cap;
  272. /* MSI-X entries */
  273. int msix_entries_nr;
  274. /* Space to store MSIX table & pending bit array */
  275. uint8_t *msix_table;
  276. uint8_t *msix_pba;
  277. /* May be used by INTx or MSI during interrupt notification */
  278. void *irq_opaque;
  279. MSITriggerFunc *msi_trigger;
  280. MSIPrepareMessageFunc *msi_prepare_message;
  281. MSIxPrepareMessageFunc *msix_prepare_message;
  282. /* MemoryRegion container for msix exclusive BAR setup */
  283. MemoryRegion msix_exclusive_bar;
  284. /* Memory Regions for MSIX table and pending bit entries. */
  285. MemoryRegion msix_table_mmio;
  286. MemoryRegion msix_pba_mmio;
  287. /* Reference-count for entries actually in use by driver. */
  288. unsigned *msix_entry_used;
  289. /* MSIX function mask set or MSIX disabled */
  290. bool msix_function_masked;
  291. /* Version id needed for VMState */
  292. int32_t version_id;
  293. /* Offset of MSI capability in config space */
  294. uint8_t msi_cap;
  295. /* PCI Express */
  296. PCIExpressDevice exp;
  297. /* SHPC */
  298. SHPCDevice *shpc;
  299. /* Location of option rom */
  300. char *romfile;
  301. uint32_t romsize;
  302. bool has_rom;
  303. MemoryRegion rom;
  304. uint32_t rom_bar;
  305. /* INTx routing notifier */
  306. PCIINTxRoutingNotifier intx_routing_notifier;
  307. /* MSI-X notifiers */
  308. MSIVectorUseNotifier msix_vector_use_notifier;
  309. MSIVectorReleaseNotifier msix_vector_release_notifier;
  310. MSIVectorPollNotifier msix_vector_poll_notifier;
  311. /* ID of standby device in net_failover pair */
  312. char *failover_pair_id;
  313. uint32_t acpi_index;
  314. };
  315. void pci_register_bar(PCIDevice *pci_dev, int region_num,
  316. uint8_t attr, MemoryRegion *memory);
  317. void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
  318. MemoryRegion *io_lo, MemoryRegion *io_hi);
  319. void pci_unregister_vga(PCIDevice *pci_dev);
  320. pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num);
  321. int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
  322. uint8_t offset, uint8_t size,
  323. Error **errp);
  324. void pci_del_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
  325. uint8_t pci_find_capability(PCIDevice *pci_dev, uint8_t cap_id);
  326. uint32_t pci_default_read_config(PCIDevice *d,
  327. uint32_t address, int len);
  328. void pci_default_write_config(PCIDevice *d,
  329. uint32_t address, uint32_t val, int len);
  330. void pci_device_save(PCIDevice *s, QEMUFile *f);
  331. int pci_device_load(PCIDevice *s, QEMUFile *f);
  332. MemoryRegion *pci_address_space(PCIDevice *dev);
  333. MemoryRegion *pci_address_space_io(PCIDevice *dev);
  334. /*
  335. * Should not normally be used by devices. For use by sPAPR target
  336. * where QEMU emulates firmware.
  337. */
  338. int pci_bar(PCIDevice *d, int reg);
  339. typedef void (*pci_set_irq_fn)(void *opaque, int irq_num, int level);
  340. typedef int (*pci_map_irq_fn)(PCIDevice *pci_dev, int irq_num);
  341. typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque, int pin);
  342. #define TYPE_PCI_BUS "PCI"
  343. OBJECT_DECLARE_TYPE(PCIBus, PCIBusClass, PCI_BUS)
  344. #define TYPE_PCIE_BUS "PCIE"
  345. #define TYPE_CXL_BUS "CXL"
  346. typedef void (*pci_bus_dev_fn)(PCIBus *b, PCIDevice *d, void *opaque);
  347. typedef void (*pci_bus_fn)(PCIBus *b, void *opaque);
  348. typedef void *(*pci_bus_ret_fn)(PCIBus *b, void *opaque);
  349. bool pci_bus_is_express(PCIBus *bus);
  350. void pci_root_bus_init(PCIBus *bus, size_t bus_size, DeviceState *parent,
  351. const char *name,
  352. MemoryRegion *address_space_mem,
  353. MemoryRegion *address_space_io,
  354. uint8_t devfn_min, const char *typename);
  355. PCIBus *pci_root_bus_new(DeviceState *parent, const char *name,
  356. MemoryRegion *address_space_mem,
  357. MemoryRegion *address_space_io,
  358. uint8_t devfn_min, const char *typename);
  359. void pci_root_bus_cleanup(PCIBus *bus);
  360. void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  361. void *irq_opaque, int nirq);
  362. void pci_bus_irqs_cleanup(PCIBus *bus);
  363. int pci_bus_get_irq_level(PCIBus *bus, int irq_num);
  364. /* 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD */
  365. static inline int pci_swizzle(int slot, int pin)
  366. {
  367. return (slot + pin) % PCI_NUM_PINS;
  368. }
  369. int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin);
  370. PCIBus *pci_register_root_bus(DeviceState *parent, const char *name,
  371. pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  372. void *irq_opaque,
  373. MemoryRegion *address_space_mem,
  374. MemoryRegion *address_space_io,
  375. uint8_t devfn_min, int nirq,
  376. const char *typename);
  377. void pci_unregister_root_bus(PCIBus *bus);
  378. void pci_bus_set_route_irq_fn(PCIBus *, pci_route_irq_fn);
  379. PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin);
  380. bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new);
  381. void pci_bus_fire_intx_routing_notifier(PCIBus *bus);
  382. void pci_device_set_intx_routing_notifier(PCIDevice *dev,
  383. PCIINTxRoutingNotifier notifier);
  384. void pci_device_reset(PCIDevice *dev);
  385. PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
  386. const char *default_model,
  387. const char *default_devaddr);
  388. PCIDevice *pci_vga_init(PCIBus *bus);
  389. static inline PCIBus *pci_get_bus(const PCIDevice *dev)
  390. {
  391. return PCI_BUS(qdev_get_parent_bus(DEVICE(dev)));
  392. }
  393. int pci_bus_num(PCIBus *s);
  394. void pci_bus_range(PCIBus *bus, int *min_bus, int *max_bus);
  395. static inline int pci_dev_bus_num(const PCIDevice *dev)
  396. {
  397. return pci_bus_num(pci_get_bus(dev));
  398. }
  399. int pci_bus_numa_node(PCIBus *bus);
  400. void pci_for_each_device(PCIBus *bus, int bus_num,
  401. pci_bus_dev_fn fn,
  402. void *opaque);
  403. void pci_for_each_device_reverse(PCIBus *bus, int bus_num,
  404. pci_bus_dev_fn fn,
  405. void *opaque);
  406. void pci_for_each_device_under_bus(PCIBus *bus,
  407. pci_bus_dev_fn fn, void *opaque);
  408. void pci_for_each_device_under_bus_reverse(PCIBus *bus,
  409. pci_bus_dev_fn fn,
  410. void *opaque);
  411. void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin,
  412. pci_bus_fn end, void *parent_state);
  413. PCIDevice *pci_get_function_0(PCIDevice *pci_dev);
  414. /* Use this wrapper when specific scan order is not required. */
  415. static inline
  416. void pci_for_each_bus(PCIBus *bus, pci_bus_fn fn, void *opaque)
  417. {
  418. pci_for_each_bus_depth_first(bus, NULL, fn, opaque);
  419. }
  420. PCIBus *pci_device_root_bus(const PCIDevice *d);
  421. const char *pci_root_bus_path(PCIDevice *dev);
  422. bool pci_bus_bypass_iommu(PCIBus *bus);
  423. PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn);
  424. int pci_qdev_find_device(const char *id, PCIDevice **pdev);
  425. void pci_bus_get_w64_range(PCIBus *bus, Range *range);
  426. void pci_device_deassert_intx(PCIDevice *dev);
  427. typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);
  428. AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
  429. void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque);
  430. pcibus_t pci_bar_address(PCIDevice *d,
  431. int reg, uint8_t type, pcibus_t size);
  432. static inline void
  433. pci_set_byte(uint8_t *config, uint8_t val)
  434. {
  435. *config = val;
  436. }
  437. static inline uint8_t
  438. pci_get_byte(const uint8_t *config)
  439. {
  440. return *config;
  441. }
  442. static inline void
  443. pci_set_word(uint8_t *config, uint16_t val)
  444. {
  445. stw_le_p(config, val);
  446. }
  447. static inline uint16_t
  448. pci_get_word(const uint8_t *config)
  449. {
  450. return lduw_le_p(config);
  451. }
  452. static inline void
  453. pci_set_long(uint8_t *config, uint32_t val)
  454. {
  455. stl_le_p(config, val);
  456. }
  457. static inline uint32_t
  458. pci_get_long(const uint8_t *config)
  459. {
  460. return ldl_le_p(config);
  461. }
  462. /*
  463. * PCI capabilities and/or their fields
  464. * are generally DWORD aligned only so
  465. * mechanism used by pci_set/get_quad()
  466. * must be tolerant to unaligned pointers
  467. *
  468. */
  469. static inline void
  470. pci_set_quad(uint8_t *config, uint64_t val)
  471. {
  472. stq_le_p(config, val);
  473. }
  474. static inline uint64_t
  475. pci_get_quad(const uint8_t *config)
  476. {
  477. return ldq_le_p(config);
  478. }
  479. static inline void
  480. pci_config_set_vendor_id(uint8_t *pci_config, uint16_t val)
  481. {
  482. pci_set_word(&pci_config[PCI_VENDOR_ID], val);
  483. }
  484. static inline void
  485. pci_config_set_device_id(uint8_t *pci_config, uint16_t val)
  486. {
  487. pci_set_word(&pci_config[PCI_DEVICE_ID], val);
  488. }
  489. static inline void
  490. pci_config_set_revision(uint8_t *pci_config, uint8_t val)
  491. {
  492. pci_set_byte(&pci_config[PCI_REVISION_ID], val);
  493. }
  494. static inline void
  495. pci_config_set_class(uint8_t *pci_config, uint16_t val)
  496. {
  497. pci_set_word(&pci_config[PCI_CLASS_DEVICE], val);
  498. }
  499. static inline void
  500. pci_config_set_prog_interface(uint8_t *pci_config, uint8_t val)
  501. {
  502. pci_set_byte(&pci_config[PCI_CLASS_PROG], val);
  503. }
  504. static inline void
  505. pci_config_set_interrupt_pin(uint8_t *pci_config, uint8_t val)
  506. {
  507. pci_set_byte(&pci_config[PCI_INTERRUPT_PIN], val);
  508. }
  509. /*
  510. * helper functions to do bit mask operation on configuration space.
  511. * Just to set bit, use test-and-set and discard returned value.
  512. * Just to clear bit, use test-and-clear and discard returned value.
  513. * NOTE: They aren't atomic.
  514. */
  515. static inline uint8_t
  516. pci_byte_test_and_clear_mask(uint8_t *config, uint8_t mask)
  517. {
  518. uint8_t val = pci_get_byte(config);
  519. pci_set_byte(config, val & ~mask);
  520. return val & mask;
  521. }
  522. static inline uint8_t
  523. pci_byte_test_and_set_mask(uint8_t *config, uint8_t mask)
  524. {
  525. uint8_t val = pci_get_byte(config);
  526. pci_set_byte(config, val | mask);
  527. return val & mask;
  528. }
  529. static inline uint16_t
  530. pci_word_test_and_clear_mask(uint8_t *config, uint16_t mask)
  531. {
  532. uint16_t val = pci_get_word(config);
  533. pci_set_word(config, val & ~mask);
  534. return val & mask;
  535. }
  536. static inline uint16_t
  537. pci_word_test_and_set_mask(uint8_t *config, uint16_t mask)
  538. {
  539. uint16_t val = pci_get_word(config);
  540. pci_set_word(config, val | mask);
  541. return val & mask;
  542. }
  543. static inline uint32_t
  544. pci_long_test_and_clear_mask(uint8_t *config, uint32_t mask)
  545. {
  546. uint32_t val = pci_get_long(config);
  547. pci_set_long(config, val & ~mask);
  548. return val & mask;
  549. }
  550. static inline uint32_t
  551. pci_long_test_and_set_mask(uint8_t *config, uint32_t mask)
  552. {
  553. uint32_t val = pci_get_long(config);
  554. pci_set_long(config, val | mask);
  555. return val & mask;
  556. }
  557. static inline uint64_t
  558. pci_quad_test_and_clear_mask(uint8_t *config, uint64_t mask)
  559. {
  560. uint64_t val = pci_get_quad(config);
  561. pci_set_quad(config, val & ~mask);
  562. return val & mask;
  563. }
  564. static inline uint64_t
  565. pci_quad_test_and_set_mask(uint8_t *config, uint64_t mask)
  566. {
  567. uint64_t val = pci_get_quad(config);
  568. pci_set_quad(config, val | mask);
  569. return val & mask;
  570. }
  571. /* Access a register specified by a mask */
  572. static inline void
  573. pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)
  574. {
  575. uint8_t val = pci_get_byte(config);
  576. uint8_t rval;
  577. assert(mask);
  578. rval = reg << ctz32(mask);
  579. pci_set_byte(config, (~mask & val) | (mask & rval));
  580. }
  581. static inline void
  582. pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg)
  583. {
  584. uint16_t val = pci_get_word(config);
  585. uint16_t rval;
  586. assert(mask);
  587. rval = reg << ctz32(mask);
  588. pci_set_word(config, (~mask & val) | (mask & rval));
  589. }
  590. static inline void
  591. pci_set_long_by_mask(uint8_t *config, uint32_t mask, uint32_t reg)
  592. {
  593. uint32_t val = pci_get_long(config);
  594. uint32_t rval;
  595. assert(mask);
  596. rval = reg << ctz32(mask);
  597. pci_set_long(config, (~mask & val) | (mask & rval));
  598. }
  599. static inline void
  600. pci_set_quad_by_mask(uint8_t *config, uint64_t mask, uint64_t reg)
  601. {
  602. uint64_t val = pci_get_quad(config);
  603. uint64_t rval;
  604. assert(mask);
  605. rval = reg << ctz32(mask);
  606. pci_set_quad(config, (~mask & val) | (mask & rval));
  607. }
  608. PCIDevice *pci_new_multifunction(int devfn, bool multifunction,
  609. const char *name);
  610. PCIDevice *pci_new(int devfn, const char *name);
  611. bool pci_realize_and_unref(PCIDevice *dev, PCIBus *bus, Error **errp);
  612. PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
  613. bool multifunction,
  614. const char *name);
  615. PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name);
  616. void lsi53c8xx_handle_legacy_cmdline(DeviceState *lsi_dev);
  617. qemu_irq pci_allocate_irq(PCIDevice *pci_dev);
  618. void pci_set_irq(PCIDevice *pci_dev, int level);
  619. static inline int pci_intx(PCIDevice *pci_dev)
  620. {
  621. return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
  622. }
  623. static inline void pci_irq_assert(PCIDevice *pci_dev)
  624. {
  625. pci_set_irq(pci_dev, 1);
  626. }
  627. static inline void pci_irq_deassert(PCIDevice *pci_dev)
  628. {
  629. pci_set_irq(pci_dev, 0);
  630. }
  631. /*
  632. * FIXME: PCI does not work this way.
  633. * All the callers to this method should be fixed.
  634. */
  635. static inline void pci_irq_pulse(PCIDevice *pci_dev)
  636. {
  637. pci_irq_assert(pci_dev);
  638. pci_irq_deassert(pci_dev);
  639. }
  640. static inline int pci_is_cxl(const PCIDevice *d)
  641. {
  642. return d->cap_present & QEMU_PCIE_CAP_CXL;
  643. }
  644. static inline int pci_is_express(const PCIDevice *d)
  645. {
  646. return d->cap_present & QEMU_PCI_CAP_EXPRESS;
  647. }
  648. static inline int pci_is_express_downstream_port(const PCIDevice *d)
  649. {
  650. uint8_t type;
  651. if (!pci_is_express(d) || !d->exp.exp_cap) {
  652. return 0;
  653. }
  654. type = pcie_cap_get_type(d);
  655. return type == PCI_EXP_TYPE_DOWNSTREAM || type == PCI_EXP_TYPE_ROOT_PORT;
  656. }
  657. static inline int pci_is_vf(const PCIDevice *d)
  658. {
  659. return d->exp.sriov_vf.pf != NULL;
  660. }
  661. static inline uint32_t pci_config_size(const PCIDevice *d)
  662. {
  663. return pci_is_express(d) ? PCIE_CONFIG_SPACE_SIZE : PCI_CONFIG_SPACE_SIZE;
  664. }
  665. static inline uint16_t pci_get_bdf(PCIDevice *dev)
  666. {
  667. return PCI_BUILD_BDF(pci_bus_num(pci_get_bus(dev)), dev->devfn);
  668. }
  669. uint16_t pci_requester_id(PCIDevice *dev);
  670. /* DMA access functions */
  671. static inline AddressSpace *pci_get_address_space(PCIDevice *dev)
  672. {
  673. return &dev->bus_master_as;
  674. }
  675. /**
  676. * pci_dma_rw: Read from or write to an address space from PCI device.
  677. *
  678. * Return a MemTxResult indicating whether the operation succeeded
  679. * or failed (eg unassigned memory, device rejected the transaction,
  680. * IOMMU fault).
  681. *
  682. * @dev: #PCIDevice doing the memory access
  683. * @addr: address within the #PCIDevice address space
  684. * @buf: buffer with the data transferred
  685. * @len: the number of bytes to read or write
  686. * @dir: indicates the transfer direction
  687. */
  688. static inline MemTxResult pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
  689. void *buf, dma_addr_t len,
  690. DMADirection dir, MemTxAttrs attrs)
  691. {
  692. return dma_memory_rw(pci_get_address_space(dev), addr, buf, len,
  693. dir, attrs);
  694. }
  695. /**
  696. * pci_dma_read: Read from an address space from PCI device.
  697. *
  698. * Return a MemTxResult indicating whether the operation succeeded
  699. * or failed (eg unassigned memory, device rejected the transaction,
  700. * IOMMU fault). Called within RCU critical section.
  701. *
  702. * @dev: #PCIDevice doing the memory access
  703. * @addr: address within the #PCIDevice address space
  704. * @buf: buffer with the data transferred
  705. * @len: length of the data transferred
  706. */
  707. static inline MemTxResult pci_dma_read(PCIDevice *dev, dma_addr_t addr,
  708. void *buf, dma_addr_t len)
  709. {
  710. return pci_dma_rw(dev, addr, buf, len,
  711. DMA_DIRECTION_TO_DEVICE, MEMTXATTRS_UNSPECIFIED);
  712. }
  713. /**
  714. * pci_dma_write: Write to address space from PCI device.
  715. *
  716. * Return a MemTxResult indicating whether the operation succeeded
  717. * or failed (eg unassigned memory, device rejected the transaction,
  718. * IOMMU fault).
  719. *
  720. * @dev: #PCIDevice doing the memory access
  721. * @addr: address within the #PCIDevice address space
  722. * @buf: buffer with the data transferred
  723. * @len: the number of bytes to write
  724. */
  725. static inline MemTxResult pci_dma_write(PCIDevice *dev, dma_addr_t addr,
  726. const void *buf, dma_addr_t len)
  727. {
  728. return pci_dma_rw(dev, addr, (void *) buf, len,
  729. DMA_DIRECTION_FROM_DEVICE, MEMTXATTRS_UNSPECIFIED);
  730. }
  731. #define PCI_DMA_DEFINE_LDST(_l, _s, _bits) \
  732. static inline MemTxResult ld##_l##_pci_dma(PCIDevice *dev, \
  733. dma_addr_t addr, \
  734. uint##_bits##_t *val, \
  735. MemTxAttrs attrs) \
  736. { \
  737. return ld##_l##_dma(pci_get_address_space(dev), addr, val, attrs); \
  738. } \
  739. static inline MemTxResult st##_s##_pci_dma(PCIDevice *dev, \
  740. dma_addr_t addr, \
  741. uint##_bits##_t val, \
  742. MemTxAttrs attrs) \
  743. { \
  744. return st##_s##_dma(pci_get_address_space(dev), addr, val, attrs); \
  745. }
  746. PCI_DMA_DEFINE_LDST(ub, b, 8);
  747. PCI_DMA_DEFINE_LDST(uw_le, w_le, 16)
  748. PCI_DMA_DEFINE_LDST(l_le, l_le, 32);
  749. PCI_DMA_DEFINE_LDST(q_le, q_le, 64);
  750. PCI_DMA_DEFINE_LDST(uw_be, w_be, 16)
  751. PCI_DMA_DEFINE_LDST(l_be, l_be, 32);
  752. PCI_DMA_DEFINE_LDST(q_be, q_be, 64);
  753. #undef PCI_DMA_DEFINE_LDST
  754. /**
  755. * pci_dma_map: Map device PCI address space range into host virtual address
  756. * @dev: #PCIDevice to be accessed
  757. * @addr: address within that device's address space
  758. * @plen: pointer to length of buffer; updated on return to indicate
  759. * if only a subset of the requested range has been mapped
  760. * @dir: indicates the transfer direction
  761. *
  762. * Return: A host pointer, or %NULL if the resources needed to
  763. * perform the mapping are exhausted (in that case *@plen
  764. * is set to zero).
  765. */
  766. static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr,
  767. dma_addr_t *plen, DMADirection dir)
  768. {
  769. return dma_memory_map(pci_get_address_space(dev), addr, plen, dir,
  770. MEMTXATTRS_UNSPECIFIED);
  771. }
  772. static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,
  773. DMADirection dir, dma_addr_t access_len)
  774. {
  775. dma_memory_unmap(pci_get_address_space(dev), buffer, len, dir, access_len);
  776. }
  777. static inline void pci_dma_sglist_init(QEMUSGList *qsg, PCIDevice *dev,
  778. int alloc_hint)
  779. {
  780. qemu_sglist_init(qsg, DEVICE(dev), alloc_hint, pci_get_address_space(dev));
  781. }
  782. extern const VMStateDescription vmstate_pci_device;
  783. #define VMSTATE_PCI_DEVICE(_field, _state) { \
  784. .name = (stringify(_field)), \
  785. .size = sizeof(PCIDevice), \
  786. .vmsd = &vmstate_pci_device, \
  787. .flags = VMS_STRUCT, \
  788. .offset = vmstate_offset_value(_state, _field, PCIDevice), \
  789. }
  790. #define VMSTATE_PCI_DEVICE_POINTER(_field, _state) { \
  791. .name = (stringify(_field)), \
  792. .size = sizeof(PCIDevice), \
  793. .vmsd = &vmstate_pci_device, \
  794. .flags = VMS_STRUCT | VMS_POINTER, \
  795. .offset = vmstate_offset_pointer(_state, _field, PCIDevice), \
  796. }
  797. MSIMessage pci_get_msi_message(PCIDevice *dev, int vector);
  798. void pci_set_power(PCIDevice *pci_dev, bool state);
  799. #endif