pci.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. /* PCI HEADER_TYPE */
  140. #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
  141. /* Size of the standard PCI config header */
  142. #define PCI_CONFIG_HEADER_SIZE 0x40
  143. /* Size of the standard PCI config space */
  144. #define PCI_CONFIG_SPACE_SIZE 0x100
  145. /* Size of the standard PCIe config space: 4KB */
  146. #define PCIE_CONFIG_SPACE_SIZE 0x1000
  147. #define PCI_NUM_PINS 4 /* A-D */
  148. /* Bits in cap_present field. */
  149. enum {
  150. QEMU_PCI_CAP_MSI = 0x1,
  151. QEMU_PCI_CAP_MSIX = 0x2,
  152. QEMU_PCI_CAP_EXPRESS = 0x4,
  153. /* multifunction capable device */
  154. #define QEMU_PCI_CAP_MULTIFUNCTION_BITNR 3
  155. QEMU_PCI_CAP_MULTIFUNCTION = (1 << QEMU_PCI_CAP_MULTIFUNCTION_BITNR),
  156. /* command register SERR bit enabled - unused since QEMU v5.0 */
  157. #define QEMU_PCI_CAP_SERR_BITNR 4
  158. QEMU_PCI_CAP_SERR = (1 << QEMU_PCI_CAP_SERR_BITNR),
  159. /* Standard hot plug controller. */
  160. #define QEMU_PCI_SHPC_BITNR 5
  161. QEMU_PCI_CAP_SHPC = (1 << QEMU_PCI_SHPC_BITNR),
  162. #define QEMU_PCI_SLOTID_BITNR 6
  163. QEMU_PCI_CAP_SLOTID = (1 << QEMU_PCI_SLOTID_BITNR),
  164. /* PCI Express capability - Power Controller Present */
  165. #define QEMU_PCIE_SLTCAP_PCP_BITNR 7
  166. QEMU_PCIE_SLTCAP_PCP = (1 << QEMU_PCIE_SLTCAP_PCP_BITNR),
  167. /* Link active status in endpoint capability is always set */
  168. #define QEMU_PCIE_LNKSTA_DLLLA_BITNR 8
  169. QEMU_PCIE_LNKSTA_DLLLA = (1 << QEMU_PCIE_LNKSTA_DLLLA_BITNR),
  170. #define QEMU_PCIE_EXTCAP_INIT_BITNR 9
  171. QEMU_PCIE_EXTCAP_INIT = (1 << QEMU_PCIE_EXTCAP_INIT_BITNR),
  172. #define QEMU_PCIE_CXL_BITNR 10
  173. QEMU_PCIE_CAP_CXL = (1 << QEMU_PCIE_CXL_BITNR),
  174. };
  175. typedef struct PCIINTxRoute {
  176. enum {
  177. PCI_INTX_ENABLED,
  178. PCI_INTX_INVERTED,
  179. PCI_INTX_DISABLED,
  180. } mode;
  181. int irq;
  182. } PCIINTxRoute;
  183. typedef void (*PCIINTxRoutingNotifier)(PCIDevice *dev);
  184. typedef int (*MSIVectorUseNotifier)(PCIDevice *dev, unsigned int vector,
  185. MSIMessage msg);
  186. typedef void (*MSIVectorReleaseNotifier)(PCIDevice *dev, unsigned int vector);
  187. typedef void (*MSIVectorPollNotifier)(PCIDevice *dev,
  188. unsigned int vector_start,
  189. unsigned int vector_end);
  190. void pci_register_bar(PCIDevice *pci_dev, int region_num,
  191. uint8_t attr, MemoryRegion *memory);
  192. void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
  193. MemoryRegion *io_lo, MemoryRegion *io_hi);
  194. void pci_unregister_vga(PCIDevice *pci_dev);
  195. pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num);
  196. int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
  197. uint8_t offset, uint8_t size,
  198. Error **errp);
  199. void pci_del_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
  200. uint8_t pci_find_capability(PCIDevice *pci_dev, uint8_t cap_id);
  201. uint32_t pci_default_read_config(PCIDevice *d,
  202. uint32_t address, int len);
  203. void pci_default_write_config(PCIDevice *d,
  204. uint32_t address, uint32_t val, int len);
  205. void pci_device_save(PCIDevice *s, QEMUFile *f);
  206. int pci_device_load(PCIDevice *s, QEMUFile *f);
  207. MemoryRegion *pci_address_space(PCIDevice *dev);
  208. MemoryRegion *pci_address_space_io(PCIDevice *dev);
  209. /*
  210. * Should not normally be used by devices. For use by sPAPR target
  211. * where QEMU emulates firmware.
  212. */
  213. int pci_bar(PCIDevice *d, int reg);
  214. typedef void (*pci_set_irq_fn)(void *opaque, int irq_num, int level);
  215. typedef int (*pci_map_irq_fn)(PCIDevice *pci_dev, int irq_num);
  216. typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque, int pin);
  217. #define TYPE_PCI_BUS "PCI"
  218. OBJECT_DECLARE_TYPE(PCIBus, PCIBusClass, PCI_BUS)
  219. #define TYPE_PCIE_BUS "PCIE"
  220. #define TYPE_CXL_BUS "CXL"
  221. typedef void (*pci_bus_dev_fn)(PCIBus *b, PCIDevice *d, void *opaque);
  222. typedef void (*pci_bus_fn)(PCIBus *b, void *opaque);
  223. typedef void *(*pci_bus_ret_fn)(PCIBus *b, void *opaque);
  224. bool pci_bus_is_express(PCIBus *bus);
  225. void pci_root_bus_init(PCIBus *bus, size_t bus_size, DeviceState *parent,
  226. const char *name,
  227. MemoryRegion *address_space_mem,
  228. MemoryRegion *address_space_io,
  229. uint8_t devfn_min, const char *typename);
  230. PCIBus *pci_root_bus_new(DeviceState *parent, const char *name,
  231. MemoryRegion *address_space_mem,
  232. MemoryRegion *address_space_io,
  233. uint8_t devfn_min, const char *typename);
  234. void pci_root_bus_cleanup(PCIBus *bus);
  235. void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  236. void *irq_opaque, int nirq);
  237. void pci_bus_irqs_cleanup(PCIBus *bus);
  238. int pci_bus_get_irq_level(PCIBus *bus, int irq_num);
  239. /* 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD */
  240. static inline int pci_swizzle(int slot, int pin)
  241. {
  242. return (slot + pin) % PCI_NUM_PINS;
  243. }
  244. int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin);
  245. PCIBus *pci_register_root_bus(DeviceState *parent, const char *name,
  246. pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  247. void *irq_opaque,
  248. MemoryRegion *address_space_mem,
  249. MemoryRegion *address_space_io,
  250. uint8_t devfn_min, int nirq,
  251. const char *typename);
  252. void pci_unregister_root_bus(PCIBus *bus);
  253. void pci_bus_set_route_irq_fn(PCIBus *, pci_route_irq_fn);
  254. PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin);
  255. bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new);
  256. void pci_bus_fire_intx_routing_notifier(PCIBus *bus);
  257. void pci_device_set_intx_routing_notifier(PCIDevice *dev,
  258. PCIINTxRoutingNotifier notifier);
  259. void pci_device_reset(PCIDevice *dev);
  260. PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
  261. const char *default_model,
  262. const char *default_devaddr);
  263. PCIDevice *pci_vga_init(PCIBus *bus);
  264. static inline PCIBus *pci_get_bus(const PCIDevice *dev)
  265. {
  266. return PCI_BUS(qdev_get_parent_bus(DEVICE(dev)));
  267. }
  268. int pci_bus_num(PCIBus *s);
  269. void pci_bus_range(PCIBus *bus, int *min_bus, int *max_bus);
  270. static inline int pci_dev_bus_num(const PCIDevice *dev)
  271. {
  272. return pci_bus_num(pci_get_bus(dev));
  273. }
  274. int pci_bus_numa_node(PCIBus *bus);
  275. void pci_for_each_device(PCIBus *bus, int bus_num,
  276. pci_bus_dev_fn fn,
  277. void *opaque);
  278. void pci_for_each_device_reverse(PCIBus *bus, int bus_num,
  279. pci_bus_dev_fn fn,
  280. void *opaque);
  281. void pci_for_each_device_under_bus(PCIBus *bus,
  282. pci_bus_dev_fn fn, void *opaque);
  283. void pci_for_each_device_under_bus_reverse(PCIBus *bus,
  284. pci_bus_dev_fn fn,
  285. void *opaque);
  286. void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin,
  287. pci_bus_fn end, void *parent_state);
  288. PCIDevice *pci_get_function_0(PCIDevice *pci_dev);
  289. /* Use this wrapper when specific scan order is not required. */
  290. static inline
  291. void pci_for_each_bus(PCIBus *bus, pci_bus_fn fn, void *opaque)
  292. {
  293. pci_for_each_bus_depth_first(bus, NULL, fn, opaque);
  294. }
  295. PCIBus *pci_device_root_bus(const PCIDevice *d);
  296. const char *pci_root_bus_path(PCIDevice *dev);
  297. bool pci_bus_bypass_iommu(PCIBus *bus);
  298. PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn);
  299. int pci_qdev_find_device(const char *id, PCIDevice **pdev);
  300. void pci_bus_get_w64_range(PCIBus *bus, Range *range);
  301. void pci_device_deassert_intx(PCIDevice *dev);
  302. typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);
  303. AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
  304. void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque);
  305. pcibus_t pci_bar_address(PCIDevice *d,
  306. int reg, uint8_t type, pcibus_t size);
  307. static inline void
  308. pci_set_byte(uint8_t *config, uint8_t val)
  309. {
  310. *config = val;
  311. }
  312. static inline uint8_t
  313. pci_get_byte(const uint8_t *config)
  314. {
  315. return *config;
  316. }
  317. static inline void
  318. pci_set_word(uint8_t *config, uint16_t val)
  319. {
  320. stw_le_p(config, val);
  321. }
  322. static inline uint16_t
  323. pci_get_word(const uint8_t *config)
  324. {
  325. return lduw_le_p(config);
  326. }
  327. static inline void
  328. pci_set_long(uint8_t *config, uint32_t val)
  329. {
  330. stl_le_p(config, val);
  331. }
  332. static inline uint32_t
  333. pci_get_long(const uint8_t *config)
  334. {
  335. return ldl_le_p(config);
  336. }
  337. /*
  338. * PCI capabilities and/or their fields
  339. * are generally DWORD aligned only so
  340. * mechanism used by pci_set/get_quad()
  341. * must be tolerant to unaligned pointers
  342. *
  343. */
  344. static inline void
  345. pci_set_quad(uint8_t *config, uint64_t val)
  346. {
  347. stq_le_p(config, val);
  348. }
  349. static inline uint64_t
  350. pci_get_quad(const uint8_t *config)
  351. {
  352. return ldq_le_p(config);
  353. }
  354. static inline void
  355. pci_config_set_vendor_id(uint8_t *pci_config, uint16_t val)
  356. {
  357. pci_set_word(&pci_config[PCI_VENDOR_ID], val);
  358. }
  359. static inline void
  360. pci_config_set_device_id(uint8_t *pci_config, uint16_t val)
  361. {
  362. pci_set_word(&pci_config[PCI_DEVICE_ID], val);
  363. }
  364. static inline void
  365. pci_config_set_revision(uint8_t *pci_config, uint8_t val)
  366. {
  367. pci_set_byte(&pci_config[PCI_REVISION_ID], val);
  368. }
  369. static inline void
  370. pci_config_set_class(uint8_t *pci_config, uint16_t val)
  371. {
  372. pci_set_word(&pci_config[PCI_CLASS_DEVICE], val);
  373. }
  374. static inline void
  375. pci_config_set_prog_interface(uint8_t *pci_config, uint8_t val)
  376. {
  377. pci_set_byte(&pci_config[PCI_CLASS_PROG], val);
  378. }
  379. static inline void
  380. pci_config_set_interrupt_pin(uint8_t *pci_config, uint8_t val)
  381. {
  382. pci_set_byte(&pci_config[PCI_INTERRUPT_PIN], val);
  383. }
  384. /*
  385. * helper functions to do bit mask operation on configuration space.
  386. * Just to set bit, use test-and-set and discard returned value.
  387. * Just to clear bit, use test-and-clear and discard returned value.
  388. * NOTE: They aren't atomic.
  389. */
  390. static inline uint8_t
  391. pci_byte_test_and_clear_mask(uint8_t *config, uint8_t mask)
  392. {
  393. uint8_t val = pci_get_byte(config);
  394. pci_set_byte(config, val & ~mask);
  395. return val & mask;
  396. }
  397. static inline uint8_t
  398. pci_byte_test_and_set_mask(uint8_t *config, uint8_t mask)
  399. {
  400. uint8_t val = pci_get_byte(config);
  401. pci_set_byte(config, val | mask);
  402. return val & mask;
  403. }
  404. static inline uint16_t
  405. pci_word_test_and_clear_mask(uint8_t *config, uint16_t mask)
  406. {
  407. uint16_t val = pci_get_word(config);
  408. pci_set_word(config, val & ~mask);
  409. return val & mask;
  410. }
  411. static inline uint16_t
  412. pci_word_test_and_set_mask(uint8_t *config, uint16_t mask)
  413. {
  414. uint16_t val = pci_get_word(config);
  415. pci_set_word(config, val | mask);
  416. return val & mask;
  417. }
  418. static inline uint32_t
  419. pci_long_test_and_clear_mask(uint8_t *config, uint32_t mask)
  420. {
  421. uint32_t val = pci_get_long(config);
  422. pci_set_long(config, val & ~mask);
  423. return val & mask;
  424. }
  425. static inline uint32_t
  426. pci_long_test_and_set_mask(uint8_t *config, uint32_t mask)
  427. {
  428. uint32_t val = pci_get_long(config);
  429. pci_set_long(config, val | mask);
  430. return val & mask;
  431. }
  432. static inline uint64_t
  433. pci_quad_test_and_clear_mask(uint8_t *config, uint64_t mask)
  434. {
  435. uint64_t val = pci_get_quad(config);
  436. pci_set_quad(config, val & ~mask);
  437. return val & mask;
  438. }
  439. static inline uint64_t
  440. pci_quad_test_and_set_mask(uint8_t *config, uint64_t mask)
  441. {
  442. uint64_t val = pci_get_quad(config);
  443. pci_set_quad(config, val | mask);
  444. return val & mask;
  445. }
  446. /* Access a register specified by a mask */
  447. static inline void
  448. pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)
  449. {
  450. uint8_t val = pci_get_byte(config);
  451. uint8_t rval;
  452. assert(mask);
  453. rval = reg << ctz32(mask);
  454. pci_set_byte(config, (~mask & val) | (mask & rval));
  455. }
  456. static inline void
  457. pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg)
  458. {
  459. uint16_t val = pci_get_word(config);
  460. uint16_t rval;
  461. assert(mask);
  462. rval = reg << ctz32(mask);
  463. pci_set_word(config, (~mask & val) | (mask & rval));
  464. }
  465. static inline void
  466. pci_set_long_by_mask(uint8_t *config, uint32_t mask, uint32_t reg)
  467. {
  468. uint32_t val = pci_get_long(config);
  469. uint32_t rval;
  470. assert(mask);
  471. rval = reg << ctz32(mask);
  472. pci_set_long(config, (~mask & val) | (mask & rval));
  473. }
  474. static inline void
  475. pci_set_quad_by_mask(uint8_t *config, uint64_t mask, uint64_t reg)
  476. {
  477. uint64_t val = pci_get_quad(config);
  478. uint64_t rval;
  479. assert(mask);
  480. rval = reg << ctz32(mask);
  481. pci_set_quad(config, (~mask & val) | (mask & rval));
  482. }
  483. PCIDevice *pci_new_multifunction(int devfn, bool multifunction,
  484. const char *name);
  485. PCIDevice *pci_new(int devfn, const char *name);
  486. bool pci_realize_and_unref(PCIDevice *dev, PCIBus *bus, Error **errp);
  487. PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
  488. bool multifunction,
  489. const char *name);
  490. PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name);
  491. void lsi53c8xx_handle_legacy_cmdline(DeviceState *lsi_dev);
  492. qemu_irq pci_allocate_irq(PCIDevice *pci_dev);
  493. void pci_set_irq(PCIDevice *pci_dev, int level);
  494. static inline void pci_irq_assert(PCIDevice *pci_dev)
  495. {
  496. pci_set_irq(pci_dev, 1);
  497. }
  498. static inline void pci_irq_deassert(PCIDevice *pci_dev)
  499. {
  500. pci_set_irq(pci_dev, 0);
  501. }
  502. /*
  503. * FIXME: PCI does not work this way.
  504. * All the callers to this method should be fixed.
  505. */
  506. static inline void pci_irq_pulse(PCIDevice *pci_dev)
  507. {
  508. pci_irq_assert(pci_dev);
  509. pci_irq_deassert(pci_dev);
  510. }
  511. MSIMessage pci_get_msi_message(PCIDevice *dev, int vector);
  512. void pci_set_power(PCIDevice *pci_dev, bool state);
  513. #endif