pci.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. #ifndef QEMU_PCI_H
  2. #define QEMU_PCI_H
  3. #include "qemu-common.h"
  4. #include "qdev.h"
  5. #include "memory.h"
  6. #include "dma.h"
  7. /* PCI includes legacy ISA access. */
  8. #include "isa.h"
  9. #include "pcie.h"
  10. /* PCI bus */
  11. #define PCI_DEVFN(slot, func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))
  12. #define PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f)
  13. #define PCI_FUNC(devfn) ((devfn) & 0x07)
  14. #define PCI_SLOT_MAX 32
  15. #define PCI_FUNC_MAX 8
  16. /* Class, Vendor and Device IDs from Linux's pci_ids.h */
  17. #include "pci_ids.h"
  18. /* QEMU-specific Vendor and Device ID definitions */
  19. /* IBM (0x1014) */
  20. #define PCI_DEVICE_ID_IBM_440GX 0x027f
  21. #define PCI_DEVICE_ID_IBM_OPENPIC2 0xffff
  22. /* Hitachi (0x1054) */
  23. #define PCI_VENDOR_ID_HITACHI 0x1054
  24. #define PCI_DEVICE_ID_HITACHI_SH7751R 0x350e
  25. /* Apple (0x106b) */
  26. #define PCI_DEVICE_ID_APPLE_343S1201 0x0010
  27. #define PCI_DEVICE_ID_APPLE_UNI_N_I_PCI 0x001e
  28. #define PCI_DEVICE_ID_APPLE_UNI_N_PCI 0x001f
  29. #define PCI_DEVICE_ID_APPLE_UNI_N_KEYL 0x0022
  30. #define PCI_DEVICE_ID_APPLE_IPID_USB 0x003f
  31. /* Realtek (0x10ec) */
  32. #define PCI_DEVICE_ID_REALTEK_8029 0x8029
  33. /* Xilinx (0x10ee) */
  34. #define PCI_DEVICE_ID_XILINX_XC2VP30 0x0300
  35. /* Marvell (0x11ab) */
  36. #define PCI_DEVICE_ID_MARVELL_GT6412X 0x4620
  37. /* QEMU/Bochs VGA (0x1234) */
  38. #define PCI_VENDOR_ID_QEMU 0x1234
  39. #define PCI_DEVICE_ID_QEMU_VGA 0x1111
  40. /* VMWare (0x15ad) */
  41. #define PCI_VENDOR_ID_VMWARE 0x15ad
  42. #define PCI_DEVICE_ID_VMWARE_SVGA2 0x0405
  43. #define PCI_DEVICE_ID_VMWARE_SVGA 0x0710
  44. #define PCI_DEVICE_ID_VMWARE_NET 0x0720
  45. #define PCI_DEVICE_ID_VMWARE_SCSI 0x0730
  46. #define PCI_DEVICE_ID_VMWARE_IDE 0x1729
  47. /* Intel (0x8086) */
  48. #define PCI_DEVICE_ID_INTEL_82551IT 0x1209
  49. #define PCI_DEVICE_ID_INTEL_82557 0x1229
  50. #define PCI_DEVICE_ID_INTEL_82801IR 0x2922
  51. /* Red Hat / Qumranet (for QEMU) -- see pci-ids.txt */
  52. #define PCI_VENDOR_ID_REDHAT_QUMRANET 0x1af4
  53. #define PCI_SUBVENDOR_ID_REDHAT_QUMRANET 0x1af4
  54. #define PCI_SUBDEVICE_ID_QEMU 0x1100
  55. #define PCI_DEVICE_ID_VIRTIO_NET 0x1000
  56. #define PCI_DEVICE_ID_VIRTIO_BLOCK 0x1001
  57. #define PCI_DEVICE_ID_VIRTIO_BALLOON 0x1002
  58. #define PCI_DEVICE_ID_VIRTIO_CONSOLE 0x1003
  59. #define PCI_DEVICE_ID_VIRTIO_SCSI 0x1004
  60. #define FMT_PCIBUS PRIx64
  61. typedef void PCIConfigWriteFunc(PCIDevice *pci_dev,
  62. uint32_t address, uint32_t data, int len);
  63. typedef uint32_t PCIConfigReadFunc(PCIDevice *pci_dev,
  64. uint32_t address, int len);
  65. typedef void PCIMapIORegionFunc(PCIDevice *pci_dev, int region_num,
  66. pcibus_t addr, pcibus_t size, int type);
  67. typedef void PCIUnregisterFunc(PCIDevice *pci_dev);
  68. typedef struct PCIIORegion {
  69. pcibus_t addr; /* current PCI mapping address. -1 means not mapped */
  70. #define PCI_BAR_UNMAPPED (~(pcibus_t)0)
  71. pcibus_t size;
  72. uint8_t type;
  73. MemoryRegion *memory;
  74. MemoryRegion *address_space;
  75. } PCIIORegion;
  76. #define PCI_ROM_SLOT 6
  77. #define PCI_NUM_REGIONS 7
  78. #include "pci_regs.h"
  79. /* PCI HEADER_TYPE */
  80. #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
  81. /* Size of the standard PCI config header */
  82. #define PCI_CONFIG_HEADER_SIZE 0x40
  83. /* Size of the standard PCI config space */
  84. #define PCI_CONFIG_SPACE_SIZE 0x100
  85. /* Size of the standart PCIe config space: 4KB */
  86. #define PCIE_CONFIG_SPACE_SIZE 0x1000
  87. #define PCI_NUM_PINS 4 /* A-D */
  88. /* Bits in cap_present field. */
  89. enum {
  90. QEMU_PCI_CAP_MSI = 0x1,
  91. QEMU_PCI_CAP_MSIX = 0x2,
  92. QEMU_PCI_CAP_EXPRESS = 0x4,
  93. /* multifunction capable device */
  94. #define QEMU_PCI_CAP_MULTIFUNCTION_BITNR 3
  95. QEMU_PCI_CAP_MULTIFUNCTION = (1 << QEMU_PCI_CAP_MULTIFUNCTION_BITNR),
  96. /* command register SERR bit enabled */
  97. #define QEMU_PCI_CAP_SERR_BITNR 4
  98. QEMU_PCI_CAP_SERR = (1 << QEMU_PCI_CAP_SERR_BITNR),
  99. /* Standard hot plug controller. */
  100. #define QEMU_PCI_SHPC_BITNR 5
  101. QEMU_PCI_CAP_SHPC = (1 << QEMU_PCI_SHPC_BITNR),
  102. #define QEMU_PCI_SLOTID_BITNR 6
  103. QEMU_PCI_CAP_SLOTID = (1 << QEMU_PCI_SLOTID_BITNR),
  104. };
  105. #define TYPE_PCI_DEVICE "pci-device"
  106. #define PCI_DEVICE(obj) \
  107. OBJECT_CHECK(PCIDevice, (obj), TYPE_PCI_DEVICE)
  108. #define PCI_DEVICE_CLASS(klass) \
  109. OBJECT_CLASS_CHECK(PCIDeviceClass, (klass), TYPE_PCI_DEVICE)
  110. #define PCI_DEVICE_GET_CLASS(obj) \
  111. OBJECT_GET_CLASS(PCIDeviceClass, (obj), TYPE_PCI_DEVICE)
  112. typedef struct PCIINTxRoute {
  113. enum {
  114. PCI_INTX_ENABLED,
  115. PCI_INTX_INVERTED,
  116. PCI_INTX_DISABLED,
  117. } mode;
  118. int irq;
  119. } PCIINTxRoute;
  120. typedef struct PCIDeviceClass {
  121. DeviceClass parent_class;
  122. int (*init)(PCIDevice *dev);
  123. PCIUnregisterFunc *exit;
  124. PCIConfigReadFunc *config_read;
  125. PCIConfigWriteFunc *config_write;
  126. uint16_t vendor_id;
  127. uint16_t device_id;
  128. uint8_t revision;
  129. uint16_t class_id;
  130. uint16_t subsystem_vendor_id; /* only for header type = 0 */
  131. uint16_t subsystem_id; /* only for header type = 0 */
  132. /*
  133. * pci-to-pci bridge or normal device.
  134. * This doesn't mean pci host switch.
  135. * When card bus bridge is supported, this would be enhanced.
  136. */
  137. int is_bridge;
  138. /* pcie stuff */
  139. int is_express; /* is this device pci express? */
  140. /* device isn't hot-pluggable */
  141. int no_hotplug;
  142. /* rom bar */
  143. const char *romfile;
  144. } PCIDeviceClass;
  145. typedef void (*PCIINTxRoutingNotifier)(PCIDevice *dev);
  146. typedef int (*MSIVectorUseNotifier)(PCIDevice *dev, unsigned int vector,
  147. MSIMessage msg);
  148. typedef void (*MSIVectorReleaseNotifier)(PCIDevice *dev, unsigned int vector);
  149. struct PCIDevice {
  150. DeviceState qdev;
  151. /* PCI config space */
  152. uint8_t *config;
  153. /* Used to enable config checks on load. Note that writable bits are
  154. * never checked even if set in cmask. */
  155. uint8_t *cmask;
  156. /* Used to implement R/W bytes */
  157. uint8_t *wmask;
  158. /* Used to implement RW1C(Write 1 to Clear) bytes */
  159. uint8_t *w1cmask;
  160. /* Used to allocate config space for capabilities. */
  161. uint8_t *used;
  162. /* the following fields are read only */
  163. PCIBus *bus;
  164. int32_t devfn;
  165. char name[64];
  166. PCIIORegion io_regions[PCI_NUM_REGIONS];
  167. DMAContext *dma;
  168. /* do not access the following fields */
  169. PCIConfigReadFunc *config_read;
  170. PCIConfigWriteFunc *config_write;
  171. /* IRQ objects for the INTA-INTD pins. */
  172. qemu_irq *irq;
  173. /* Current IRQ levels. Used internally by the generic PCI code. */
  174. uint8_t irq_state;
  175. /* Capability bits */
  176. uint32_t cap_present;
  177. /* Offset of MSI-X capability in config space */
  178. uint8_t msix_cap;
  179. /* MSI-X entries */
  180. int msix_entries_nr;
  181. /* Space to store MSIX table & pending bit array */
  182. uint8_t *msix_table;
  183. uint8_t *msix_pba;
  184. /* MemoryRegion container for msix exclusive BAR setup */
  185. MemoryRegion msix_exclusive_bar;
  186. /* Memory Regions for MSIX table and pending bit entries. */
  187. MemoryRegion msix_table_mmio;
  188. MemoryRegion msix_pba_mmio;
  189. /* Reference-count for entries actually in use by driver. */
  190. unsigned *msix_entry_used;
  191. /* MSIX function mask set or MSIX disabled */
  192. bool msix_function_masked;
  193. /* Version id needed for VMState */
  194. int32_t version_id;
  195. /* Offset of MSI capability in config space */
  196. uint8_t msi_cap;
  197. /* PCI Express */
  198. PCIExpressDevice exp;
  199. /* SHPC */
  200. SHPCDevice *shpc;
  201. /* Location of option rom */
  202. char *romfile;
  203. bool has_rom;
  204. MemoryRegion rom;
  205. uint32_t rom_bar;
  206. /* INTx routing notifier */
  207. PCIINTxRoutingNotifier intx_routing_notifier;
  208. /* MSI-X notifiers */
  209. MSIVectorUseNotifier msix_vector_use_notifier;
  210. MSIVectorReleaseNotifier msix_vector_release_notifier;
  211. };
  212. void pci_register_bar(PCIDevice *pci_dev, int region_num,
  213. uint8_t attr, MemoryRegion *memory);
  214. pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num);
  215. int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
  216. uint8_t offset, uint8_t size);
  217. void pci_del_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
  218. uint8_t pci_find_capability(PCIDevice *pci_dev, uint8_t cap_id);
  219. uint32_t pci_default_read_config(PCIDevice *d,
  220. uint32_t address, int len);
  221. void pci_default_write_config(PCIDevice *d,
  222. uint32_t address, uint32_t val, int len);
  223. void pci_device_save(PCIDevice *s, QEMUFile *f);
  224. int pci_device_load(PCIDevice *s, QEMUFile *f);
  225. MemoryRegion *pci_address_space(PCIDevice *dev);
  226. MemoryRegion *pci_address_space_io(PCIDevice *dev);
  227. typedef void (*pci_set_irq_fn)(void *opaque, int irq_num, int level);
  228. typedef int (*pci_map_irq_fn)(PCIDevice *pci_dev, int irq_num);
  229. typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque, int pin);
  230. typedef enum {
  231. PCI_HOTPLUG_DISABLED,
  232. PCI_HOTPLUG_ENABLED,
  233. PCI_COLDPLUG_ENABLED,
  234. } PCIHotplugState;
  235. typedef int (*pci_hotplug_fn)(DeviceState *qdev, PCIDevice *pci_dev,
  236. PCIHotplugState state);
  237. void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
  238. const char *name,
  239. MemoryRegion *address_space_mem,
  240. MemoryRegion *address_space_io,
  241. uint8_t devfn_min);
  242. PCIBus *pci_bus_new(DeviceState *parent, const char *name,
  243. MemoryRegion *address_space_mem,
  244. MemoryRegion *address_space_io,
  245. uint8_t devfn_min);
  246. void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  247. void *irq_opaque, int nirq);
  248. int pci_bus_get_irq_level(PCIBus *bus, int irq_num);
  249. void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *dev);
  250. PCIBus *pci_register_bus(DeviceState *parent, const char *name,
  251. pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  252. void *irq_opaque,
  253. MemoryRegion *address_space_mem,
  254. MemoryRegion *address_space_io,
  255. uint8_t devfn_min, int nirq);
  256. void pci_bus_set_route_irq_fn(PCIBus *, pci_route_irq_fn);
  257. PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin);
  258. void pci_bus_fire_intx_routing_notifier(PCIBus *bus);
  259. void pci_device_set_intx_routing_notifier(PCIDevice *dev,
  260. PCIINTxRoutingNotifier notifier);
  261. void pci_device_reset(PCIDevice *dev);
  262. void pci_bus_reset(PCIBus *bus);
  263. PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
  264. const char *default_devaddr);
  265. PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
  266. const char *default_devaddr);
  267. int pci_bus_num(PCIBus *s);
  268. void pci_for_each_device(PCIBus *bus, int bus_num,
  269. void (*fn)(PCIBus *bus, PCIDevice *d, void *opaque),
  270. void *opaque);
  271. PCIBus *pci_find_root_bus(int domain);
  272. int pci_find_domain(const PCIBus *bus);
  273. PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn);
  274. int pci_qdev_find_device(const char *id, PCIDevice **pdev);
  275. PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr);
  276. int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
  277. unsigned *slotp);
  278. void pci_device_deassert_intx(PCIDevice *dev);
  279. typedef DMAContext *(*PCIDMAContextFunc)(PCIBus *, void *, int);
  280. void pci_setup_iommu(PCIBus *bus, PCIDMAContextFunc fn, void *opaque);
  281. static inline void
  282. pci_set_byte(uint8_t *config, uint8_t val)
  283. {
  284. *config = val;
  285. }
  286. static inline uint8_t
  287. pci_get_byte(const uint8_t *config)
  288. {
  289. return *config;
  290. }
  291. static inline void
  292. pci_set_word(uint8_t *config, uint16_t val)
  293. {
  294. cpu_to_le16wu((uint16_t *)config, val);
  295. }
  296. static inline uint16_t
  297. pci_get_word(const uint8_t *config)
  298. {
  299. return le16_to_cpupu((const uint16_t *)config);
  300. }
  301. static inline void
  302. pci_set_long(uint8_t *config, uint32_t val)
  303. {
  304. cpu_to_le32wu((uint32_t *)config, val);
  305. }
  306. static inline uint32_t
  307. pci_get_long(const uint8_t *config)
  308. {
  309. return le32_to_cpupu((const uint32_t *)config);
  310. }
  311. static inline void
  312. pci_set_quad(uint8_t *config, uint64_t val)
  313. {
  314. cpu_to_le64w((uint64_t *)config, val);
  315. }
  316. static inline uint64_t
  317. pci_get_quad(const uint8_t *config)
  318. {
  319. return le64_to_cpup((const uint64_t *)config);
  320. }
  321. static inline void
  322. pci_config_set_vendor_id(uint8_t *pci_config, uint16_t val)
  323. {
  324. pci_set_word(&pci_config[PCI_VENDOR_ID], val);
  325. }
  326. static inline void
  327. pci_config_set_device_id(uint8_t *pci_config, uint16_t val)
  328. {
  329. pci_set_word(&pci_config[PCI_DEVICE_ID], val);
  330. }
  331. static inline void
  332. pci_config_set_revision(uint8_t *pci_config, uint8_t val)
  333. {
  334. pci_set_byte(&pci_config[PCI_REVISION_ID], val);
  335. }
  336. static inline void
  337. pci_config_set_class(uint8_t *pci_config, uint16_t val)
  338. {
  339. pci_set_word(&pci_config[PCI_CLASS_DEVICE], val);
  340. }
  341. static inline void
  342. pci_config_set_prog_interface(uint8_t *pci_config, uint8_t val)
  343. {
  344. pci_set_byte(&pci_config[PCI_CLASS_PROG], val);
  345. }
  346. static inline void
  347. pci_config_set_interrupt_pin(uint8_t *pci_config, uint8_t val)
  348. {
  349. pci_set_byte(&pci_config[PCI_INTERRUPT_PIN], val);
  350. }
  351. /*
  352. * helper functions to do bit mask operation on configuration space.
  353. * Just to set bit, use test-and-set and discard returned value.
  354. * Just to clear bit, use test-and-clear and discard returned value.
  355. * NOTE: They aren't atomic.
  356. */
  357. static inline uint8_t
  358. pci_byte_test_and_clear_mask(uint8_t *config, uint8_t mask)
  359. {
  360. uint8_t val = pci_get_byte(config);
  361. pci_set_byte(config, val & ~mask);
  362. return val & mask;
  363. }
  364. static inline uint8_t
  365. pci_byte_test_and_set_mask(uint8_t *config, uint8_t mask)
  366. {
  367. uint8_t val = pci_get_byte(config);
  368. pci_set_byte(config, val | mask);
  369. return val & mask;
  370. }
  371. static inline uint16_t
  372. pci_word_test_and_clear_mask(uint8_t *config, uint16_t mask)
  373. {
  374. uint16_t val = pci_get_word(config);
  375. pci_set_word(config, val & ~mask);
  376. return val & mask;
  377. }
  378. static inline uint16_t
  379. pci_word_test_and_set_mask(uint8_t *config, uint16_t mask)
  380. {
  381. uint16_t val = pci_get_word(config);
  382. pci_set_word(config, val | mask);
  383. return val & mask;
  384. }
  385. static inline uint32_t
  386. pci_long_test_and_clear_mask(uint8_t *config, uint32_t mask)
  387. {
  388. uint32_t val = pci_get_long(config);
  389. pci_set_long(config, val & ~mask);
  390. return val & mask;
  391. }
  392. static inline uint32_t
  393. pci_long_test_and_set_mask(uint8_t *config, uint32_t mask)
  394. {
  395. uint32_t val = pci_get_long(config);
  396. pci_set_long(config, val | mask);
  397. return val & mask;
  398. }
  399. static inline uint64_t
  400. pci_quad_test_and_clear_mask(uint8_t *config, uint64_t mask)
  401. {
  402. uint64_t val = pci_get_quad(config);
  403. pci_set_quad(config, val & ~mask);
  404. return val & mask;
  405. }
  406. static inline uint64_t
  407. pci_quad_test_and_set_mask(uint8_t *config, uint64_t mask)
  408. {
  409. uint64_t val = pci_get_quad(config);
  410. pci_set_quad(config, val | mask);
  411. return val & mask;
  412. }
  413. /* Access a register specified by a mask */
  414. static inline void
  415. pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)
  416. {
  417. uint8_t val = pci_get_byte(config);
  418. uint8_t rval = reg << (ffs(mask) - 1);
  419. pci_set_byte(config, (~mask & val) | (mask & rval));
  420. }
  421. static inline uint8_t
  422. pci_get_byte_by_mask(uint8_t *config, uint8_t mask)
  423. {
  424. uint8_t val = pci_get_byte(config);
  425. return (val & mask) >> (ffs(mask) - 1);
  426. }
  427. static inline void
  428. pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg)
  429. {
  430. uint16_t val = pci_get_word(config);
  431. uint16_t rval = reg << (ffs(mask) - 1);
  432. pci_set_word(config, (~mask & val) | (mask & rval));
  433. }
  434. static inline uint16_t
  435. pci_get_word_by_mask(uint8_t *config, uint16_t mask)
  436. {
  437. uint16_t val = pci_get_word(config);
  438. return (val & mask) >> (ffs(mask) - 1);
  439. }
  440. static inline void
  441. pci_set_long_by_mask(uint8_t *config, uint32_t mask, uint32_t reg)
  442. {
  443. uint32_t val = pci_get_long(config);
  444. uint32_t rval = reg << (ffs(mask) - 1);
  445. pci_set_long(config, (~mask & val) | (mask & rval));
  446. }
  447. static inline uint32_t
  448. pci_get_long_by_mask(uint8_t *config, uint32_t mask)
  449. {
  450. uint32_t val = pci_get_long(config);
  451. return (val & mask) >> (ffs(mask) - 1);
  452. }
  453. static inline void
  454. pci_set_quad_by_mask(uint8_t *config, uint64_t mask, uint64_t reg)
  455. {
  456. uint64_t val = pci_get_quad(config);
  457. uint64_t rval = reg << (ffs(mask) - 1);
  458. pci_set_quad(config, (~mask & val) | (mask & rval));
  459. }
  460. static inline uint64_t
  461. pci_get_quad_by_mask(uint8_t *config, uint64_t mask)
  462. {
  463. uint64_t val = pci_get_quad(config);
  464. return (val & mask) >> (ffs(mask) - 1);
  465. }
  466. PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
  467. const char *name);
  468. PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
  469. bool multifunction,
  470. const char *name);
  471. PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name);
  472. PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name);
  473. static inline int pci_is_express(const PCIDevice *d)
  474. {
  475. return d->cap_present & QEMU_PCI_CAP_EXPRESS;
  476. }
  477. static inline uint32_t pci_config_size(const PCIDevice *d)
  478. {
  479. return pci_is_express(d) ? PCIE_CONFIG_SPACE_SIZE : PCI_CONFIG_SPACE_SIZE;
  480. }
  481. /* DMA access functions */
  482. static inline DMAContext *pci_dma_context(PCIDevice *dev)
  483. {
  484. return dev->dma;
  485. }
  486. static inline int pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
  487. void *buf, dma_addr_t len, DMADirection dir)
  488. {
  489. dma_memory_rw(pci_dma_context(dev), addr, buf, len, dir);
  490. return 0;
  491. }
  492. static inline int pci_dma_read(PCIDevice *dev, dma_addr_t addr,
  493. void *buf, dma_addr_t len)
  494. {
  495. return pci_dma_rw(dev, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
  496. }
  497. static inline int pci_dma_write(PCIDevice *dev, dma_addr_t addr,
  498. const void *buf, dma_addr_t len)
  499. {
  500. return pci_dma_rw(dev, addr, (void *) buf, len, DMA_DIRECTION_FROM_DEVICE);
  501. }
  502. #define PCI_DMA_DEFINE_LDST(_l, _s, _bits) \
  503. static inline uint##_bits##_t ld##_l##_pci_dma(PCIDevice *dev, \
  504. dma_addr_t addr) \
  505. { \
  506. return ld##_l##_dma(pci_dma_context(dev), addr); \
  507. } \
  508. static inline void st##_s##_pci_dma(PCIDevice *dev, \
  509. dma_addr_t addr, uint##_bits##_t val) \
  510. { \
  511. st##_s##_dma(pci_dma_context(dev), addr, val); \
  512. }
  513. PCI_DMA_DEFINE_LDST(ub, b, 8);
  514. PCI_DMA_DEFINE_LDST(uw_le, w_le, 16)
  515. PCI_DMA_DEFINE_LDST(l_le, l_le, 32);
  516. PCI_DMA_DEFINE_LDST(q_le, q_le, 64);
  517. PCI_DMA_DEFINE_LDST(uw_be, w_be, 16)
  518. PCI_DMA_DEFINE_LDST(l_be, l_be, 32);
  519. PCI_DMA_DEFINE_LDST(q_be, q_be, 64);
  520. #undef PCI_DMA_DEFINE_LDST
  521. static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr,
  522. dma_addr_t *plen, DMADirection dir)
  523. {
  524. void *buf;
  525. buf = dma_memory_map(pci_dma_context(dev), addr, plen, dir);
  526. return buf;
  527. }
  528. static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,
  529. DMADirection dir, dma_addr_t access_len)
  530. {
  531. dma_memory_unmap(pci_dma_context(dev), buffer, len, dir, access_len);
  532. }
  533. static inline void pci_dma_sglist_init(QEMUSGList *qsg, PCIDevice *dev,
  534. int alloc_hint)
  535. {
  536. qemu_sglist_init(qsg, alloc_hint, pci_dma_context(dev));
  537. }
  538. extern const VMStateDescription vmstate_pci_device;
  539. #define VMSTATE_PCI_DEVICE(_field, _state) { \
  540. .name = (stringify(_field)), \
  541. .size = sizeof(PCIDevice), \
  542. .vmsd = &vmstate_pci_device, \
  543. .flags = VMS_STRUCT, \
  544. .offset = vmstate_offset_value(_state, _field, PCIDevice), \
  545. }
  546. #define VMSTATE_PCI_DEVICE_POINTER(_field, _state) { \
  547. .name = (stringify(_field)), \
  548. .size = sizeof(PCIDevice), \
  549. .vmsd = &vmstate_pci_device, \
  550. .flags = VMS_STRUCT|VMS_POINTER, \
  551. .offset = vmstate_offset_pointer(_state, _field, PCIDevice), \
  552. }
  553. #endif