2
0

pci.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 FMT_PCIBUS PRIx64
  60. typedef void PCIConfigWriteFunc(PCIDevice *pci_dev,
  61. uint32_t address, uint32_t data, int len);
  62. typedef uint32_t PCIConfigReadFunc(PCIDevice *pci_dev,
  63. uint32_t address, int len);
  64. typedef void PCIMapIORegionFunc(PCIDevice *pci_dev, int region_num,
  65. pcibus_t addr, pcibus_t size, int type);
  66. typedef int PCIUnregisterFunc(PCIDevice *pci_dev);
  67. typedef struct PCIIORegion {
  68. pcibus_t addr; /* current PCI mapping address. -1 means not mapped */
  69. #define PCI_BAR_UNMAPPED (~(pcibus_t)0)
  70. pcibus_t size;
  71. uint8_t type;
  72. MemoryRegion *memory;
  73. MemoryRegion *address_space;
  74. } PCIIORegion;
  75. #define PCI_ROM_SLOT 6
  76. #define PCI_NUM_REGIONS 7
  77. #include "pci_regs.h"
  78. /* PCI HEADER_TYPE */
  79. #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
  80. /* Size of the standard PCI config header */
  81. #define PCI_CONFIG_HEADER_SIZE 0x40
  82. /* Size of the standard PCI config space */
  83. #define PCI_CONFIG_SPACE_SIZE 0x100
  84. /* Size of the standart PCIe config space: 4KB */
  85. #define PCIE_CONFIG_SPACE_SIZE 0x1000
  86. #define PCI_NUM_PINS 4 /* A-D */
  87. /* Bits in cap_present field. */
  88. enum {
  89. QEMU_PCI_CAP_MSI = 0x1,
  90. QEMU_PCI_CAP_MSIX = 0x2,
  91. QEMU_PCI_CAP_EXPRESS = 0x4,
  92. /* multifunction capable device */
  93. #define QEMU_PCI_CAP_MULTIFUNCTION_BITNR 3
  94. QEMU_PCI_CAP_MULTIFUNCTION = (1 << QEMU_PCI_CAP_MULTIFUNCTION_BITNR),
  95. /* command register SERR bit enabled */
  96. #define QEMU_PCI_CAP_SERR_BITNR 4
  97. QEMU_PCI_CAP_SERR = (1 << QEMU_PCI_CAP_SERR_BITNR),
  98. };
  99. struct PCIDevice {
  100. DeviceState qdev;
  101. /* PCI config space */
  102. uint8_t *config;
  103. /* Used to enable config checks on load. Note that writable bits are
  104. * never checked even if set in cmask. */
  105. uint8_t *cmask;
  106. /* Used to implement R/W bytes */
  107. uint8_t *wmask;
  108. /* Used to implement RW1C(Write 1 to Clear) bytes */
  109. uint8_t *w1cmask;
  110. /* Used to allocate config space for capabilities. */
  111. uint8_t *used;
  112. /* the following fields are read only */
  113. PCIBus *bus;
  114. uint32_t devfn;
  115. char name[64];
  116. PCIIORegion io_regions[PCI_NUM_REGIONS];
  117. /* do not access the following fields */
  118. PCIConfigReadFunc *config_read;
  119. PCIConfigWriteFunc *config_write;
  120. /* IRQ objects for the INTA-INTD pins. */
  121. qemu_irq *irq;
  122. /* Current IRQ levels. Used internally by the generic PCI code. */
  123. uint8_t irq_state;
  124. /* Capability bits */
  125. uint32_t cap_present;
  126. /* Offset of MSI-X capability in config space */
  127. uint8_t msix_cap;
  128. /* MSI-X entries */
  129. int msix_entries_nr;
  130. /* Space to store MSIX table */
  131. uint8_t *msix_table_page;
  132. /* MMIO index used to map MSIX table and pending bit entries. */
  133. MemoryRegion msix_mmio;
  134. /* Reference-count for entries actually in use by driver. */
  135. unsigned *msix_entry_used;
  136. /* Region including the MSI-X table */
  137. uint32_t msix_bar_size;
  138. /* MSIX function mask set or MSIX disabled */
  139. bool msix_function_masked;
  140. /* Version id needed for VMState */
  141. int32_t version_id;
  142. /* Offset of MSI capability in config space */
  143. uint8_t msi_cap;
  144. /* PCI Express */
  145. PCIExpressDevice exp;
  146. /* Location of option rom */
  147. char *romfile;
  148. bool has_rom;
  149. MemoryRegion rom;
  150. uint32_t rom_bar;
  151. };
  152. PCIDevice *pci_register_device(PCIBus *bus, const char *name,
  153. int instance_size, int devfn,
  154. PCIConfigReadFunc *config_read,
  155. PCIConfigWriteFunc *config_write);
  156. void pci_register_bar(PCIDevice *pci_dev, int region_num,
  157. uint8_t attr, MemoryRegion *memory);
  158. pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num);
  159. int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
  160. uint8_t offset, uint8_t size);
  161. void pci_del_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
  162. uint8_t pci_find_capability(PCIDevice *pci_dev, uint8_t cap_id);
  163. uint32_t pci_default_read_config(PCIDevice *d,
  164. uint32_t address, int len);
  165. void pci_default_write_config(PCIDevice *d,
  166. uint32_t address, uint32_t val, int len);
  167. void pci_device_save(PCIDevice *s, QEMUFile *f);
  168. int pci_device_load(PCIDevice *s, QEMUFile *f);
  169. MemoryRegion *pci_address_space(PCIDevice *dev);
  170. MemoryRegion *pci_address_space_io(PCIDevice *dev);
  171. typedef void (*pci_set_irq_fn)(void *opaque, int irq_num, int level);
  172. typedef int (*pci_map_irq_fn)(PCIDevice *pci_dev, int irq_num);
  173. typedef enum {
  174. PCI_HOTPLUG_DISABLED,
  175. PCI_HOTPLUG_ENABLED,
  176. PCI_COLDPLUG_ENABLED,
  177. } PCIHotplugState;
  178. typedef int (*pci_hotplug_fn)(DeviceState *qdev, PCIDevice *pci_dev,
  179. PCIHotplugState state);
  180. void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
  181. const char *name,
  182. MemoryRegion *address_space_mem,
  183. MemoryRegion *address_space_io,
  184. uint8_t devfn_min);
  185. PCIBus *pci_bus_new(DeviceState *parent, const char *name,
  186. MemoryRegion *address_space_mem,
  187. MemoryRegion *address_space_io,
  188. uint8_t devfn_min);
  189. void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  190. void *irq_opaque, int nirq);
  191. int pci_bus_get_irq_level(PCIBus *bus, int irq_num);
  192. void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *dev);
  193. PCIBus *pci_register_bus(DeviceState *parent, const char *name,
  194. pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  195. void *irq_opaque,
  196. MemoryRegion *address_space_mem,
  197. MemoryRegion *address_space_io,
  198. uint8_t devfn_min, int nirq);
  199. void pci_device_reset(PCIDevice *dev);
  200. void pci_bus_reset(PCIBus *bus);
  201. PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
  202. const char *default_devaddr);
  203. PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
  204. const char *default_devaddr);
  205. int pci_bus_num(PCIBus *s);
  206. void pci_for_each_device(PCIBus *bus, int bus_num, void (*fn)(PCIBus *bus, PCIDevice *d));
  207. PCIBus *pci_find_root_bus(int domain);
  208. int pci_find_domain(const PCIBus *bus);
  209. PCIBus *pci_find_bus(PCIBus *bus, int bus_num);
  210. PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn);
  211. int pci_qdev_find_device(const char *id, PCIDevice **pdev);
  212. PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr);
  213. int pci_parse_devaddr(const char *addr, int *domp, int *busp,
  214. unsigned int *slotp, unsigned int *funcp);
  215. int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
  216. unsigned *slotp);
  217. void pci_device_deassert_intx(PCIDevice *dev);
  218. static inline void
  219. pci_set_byte(uint8_t *config, uint8_t val)
  220. {
  221. *config = val;
  222. }
  223. static inline uint8_t
  224. pci_get_byte(const uint8_t *config)
  225. {
  226. return *config;
  227. }
  228. static inline void
  229. pci_set_word(uint8_t *config, uint16_t val)
  230. {
  231. cpu_to_le16wu((uint16_t *)config, val);
  232. }
  233. static inline uint16_t
  234. pci_get_word(const uint8_t *config)
  235. {
  236. return le16_to_cpupu((const uint16_t *)config);
  237. }
  238. static inline void
  239. pci_set_long(uint8_t *config, uint32_t val)
  240. {
  241. cpu_to_le32wu((uint32_t *)config, val);
  242. }
  243. static inline uint32_t
  244. pci_get_long(const uint8_t *config)
  245. {
  246. return le32_to_cpupu((const uint32_t *)config);
  247. }
  248. static inline void
  249. pci_set_quad(uint8_t *config, uint64_t val)
  250. {
  251. cpu_to_le64w((uint64_t *)config, val);
  252. }
  253. static inline uint64_t
  254. pci_get_quad(const uint8_t *config)
  255. {
  256. return le64_to_cpup((const uint64_t *)config);
  257. }
  258. static inline void
  259. pci_config_set_vendor_id(uint8_t *pci_config, uint16_t val)
  260. {
  261. pci_set_word(&pci_config[PCI_VENDOR_ID], val);
  262. }
  263. static inline void
  264. pci_config_set_device_id(uint8_t *pci_config, uint16_t val)
  265. {
  266. pci_set_word(&pci_config[PCI_DEVICE_ID], val);
  267. }
  268. static inline void
  269. pci_config_set_revision(uint8_t *pci_config, uint8_t val)
  270. {
  271. pci_set_byte(&pci_config[PCI_REVISION_ID], val);
  272. }
  273. static inline void
  274. pci_config_set_class(uint8_t *pci_config, uint16_t val)
  275. {
  276. pci_set_word(&pci_config[PCI_CLASS_DEVICE], val);
  277. }
  278. static inline void
  279. pci_config_set_prog_interface(uint8_t *pci_config, uint8_t val)
  280. {
  281. pci_set_byte(&pci_config[PCI_CLASS_PROG], val);
  282. }
  283. static inline void
  284. pci_config_set_interrupt_pin(uint8_t *pci_config, uint8_t val)
  285. {
  286. pci_set_byte(&pci_config[PCI_INTERRUPT_PIN], val);
  287. }
  288. /*
  289. * helper functions to do bit mask operation on configuration space.
  290. * Just to set bit, use test-and-set and discard returned value.
  291. * Just to clear bit, use test-and-clear and discard returned value.
  292. * NOTE: They aren't atomic.
  293. */
  294. static inline uint8_t
  295. pci_byte_test_and_clear_mask(uint8_t *config, uint8_t mask)
  296. {
  297. uint8_t val = pci_get_byte(config);
  298. pci_set_byte(config, val & ~mask);
  299. return val & mask;
  300. }
  301. static inline uint8_t
  302. pci_byte_test_and_set_mask(uint8_t *config, uint8_t mask)
  303. {
  304. uint8_t val = pci_get_byte(config);
  305. pci_set_byte(config, val | mask);
  306. return val & mask;
  307. }
  308. static inline uint16_t
  309. pci_word_test_and_clear_mask(uint8_t *config, uint16_t mask)
  310. {
  311. uint16_t val = pci_get_word(config);
  312. pci_set_word(config, val & ~mask);
  313. return val & mask;
  314. }
  315. static inline uint16_t
  316. pci_word_test_and_set_mask(uint8_t *config, uint16_t mask)
  317. {
  318. uint16_t val = pci_get_word(config);
  319. pci_set_word(config, val | mask);
  320. return val & mask;
  321. }
  322. static inline uint32_t
  323. pci_long_test_and_clear_mask(uint8_t *config, uint32_t mask)
  324. {
  325. uint32_t val = pci_get_long(config);
  326. pci_set_long(config, val & ~mask);
  327. return val & mask;
  328. }
  329. static inline uint32_t
  330. pci_long_test_and_set_mask(uint8_t *config, uint32_t mask)
  331. {
  332. uint32_t val = pci_get_long(config);
  333. pci_set_long(config, val | mask);
  334. return val & mask;
  335. }
  336. static inline uint64_t
  337. pci_quad_test_and_clear_mask(uint8_t *config, uint64_t mask)
  338. {
  339. uint64_t val = pci_get_quad(config);
  340. pci_set_quad(config, val & ~mask);
  341. return val & mask;
  342. }
  343. static inline uint64_t
  344. pci_quad_test_and_set_mask(uint8_t *config, uint64_t mask)
  345. {
  346. uint64_t val = pci_get_quad(config);
  347. pci_set_quad(config, val | mask);
  348. return val & mask;
  349. }
  350. typedef int (*pci_qdev_initfn)(PCIDevice *dev);
  351. typedef struct {
  352. DeviceInfo qdev;
  353. pci_qdev_initfn init;
  354. PCIUnregisterFunc *exit;
  355. PCIConfigReadFunc *config_read;
  356. PCIConfigWriteFunc *config_write;
  357. uint16_t vendor_id;
  358. uint16_t device_id;
  359. uint8_t revision;
  360. uint16_t class_id;
  361. uint16_t subsystem_vendor_id; /* only for header type = 0 */
  362. uint16_t subsystem_id; /* only for header type = 0 */
  363. /*
  364. * pci-to-pci bridge or normal device.
  365. * This doesn't mean pci host switch.
  366. * When card bus bridge is supported, this would be enhanced.
  367. */
  368. int is_bridge;
  369. /* pcie stuff */
  370. int is_express; /* is this device pci express? */
  371. /* device isn't hot-pluggable */
  372. int no_hotplug;
  373. /* rom bar */
  374. const char *romfile;
  375. } PCIDeviceInfo;
  376. void pci_qdev_register(PCIDeviceInfo *info);
  377. void pci_qdev_register_many(PCIDeviceInfo *info);
  378. PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
  379. const char *name);
  380. PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
  381. bool multifunction,
  382. const char *name);
  383. PCIDevice *pci_try_create_multifunction(PCIBus *bus, int devfn,
  384. bool multifunction,
  385. const char *name);
  386. PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name);
  387. PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name);
  388. PCIDevice *pci_try_create(PCIBus *bus, int devfn, const char *name);
  389. static inline int pci_is_express(const PCIDevice *d)
  390. {
  391. return d->cap_present & QEMU_PCI_CAP_EXPRESS;
  392. }
  393. static inline uint32_t pci_config_size(const PCIDevice *d)
  394. {
  395. return pci_is_express(d) ? PCIE_CONFIG_SPACE_SIZE : PCI_CONFIG_SPACE_SIZE;
  396. }
  397. /* DMA access functions */
  398. static inline int pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
  399. void *buf, dma_addr_t len, DMADirection dir)
  400. {
  401. cpu_physical_memory_rw(addr, buf, len, dir == DMA_DIRECTION_FROM_DEVICE);
  402. return 0;
  403. }
  404. static inline int pci_dma_read(PCIDevice *dev, dma_addr_t addr,
  405. void *buf, dma_addr_t len)
  406. {
  407. return pci_dma_rw(dev, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
  408. }
  409. static inline int pci_dma_write(PCIDevice *dev, dma_addr_t addr,
  410. const void *buf, dma_addr_t len)
  411. {
  412. return pci_dma_rw(dev, addr, (void *) buf, len, DMA_DIRECTION_FROM_DEVICE);
  413. }
  414. #define PCI_DMA_DEFINE_LDST(_l, _s, _bits) \
  415. static inline uint##_bits##_t ld##_l##_pci_dma(PCIDevice *dev, \
  416. dma_addr_t addr) \
  417. { \
  418. return ld##_l##_phys(addr); \
  419. } \
  420. static inline void st##_s##_pci_dma(PCIDevice *dev, \
  421. dma_addr_t addr, uint##_bits##_t val) \
  422. { \
  423. st##_s##_phys(addr, val); \
  424. }
  425. PCI_DMA_DEFINE_LDST(ub, b, 8);
  426. PCI_DMA_DEFINE_LDST(uw_le, w_le, 16)
  427. PCI_DMA_DEFINE_LDST(l_le, l_le, 32);
  428. PCI_DMA_DEFINE_LDST(q_le, q_le, 64);
  429. PCI_DMA_DEFINE_LDST(uw_be, w_be, 16)
  430. PCI_DMA_DEFINE_LDST(l_be, l_be, 32);
  431. PCI_DMA_DEFINE_LDST(q_be, q_be, 64);
  432. #undef PCI_DMA_DEFINE_LDST
  433. static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr,
  434. dma_addr_t *plen, DMADirection dir)
  435. {
  436. target_phys_addr_t len = *plen;
  437. void *buf;
  438. buf = cpu_physical_memory_map(addr, &len, dir == DMA_DIRECTION_FROM_DEVICE);
  439. *plen = len;
  440. return buf;
  441. }
  442. static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,
  443. DMADirection dir, dma_addr_t access_len)
  444. {
  445. cpu_physical_memory_unmap(buffer, len, dir == DMA_DIRECTION_FROM_DEVICE,
  446. access_len);
  447. }
  448. static inline void pci_dma_sglist_init(QEMUSGList *qsg, PCIDevice *dev,
  449. int alloc_hint)
  450. {
  451. qemu_sglist_init(qsg, alloc_hint);
  452. }
  453. #endif