pci.h 21 KB

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