2
0

pci.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #ifndef QEMU_PCI_H
  2. #define QEMU_PCI_H
  3. #include "qemu-common.h"
  4. #include "qobject.h"
  5. #include "qdev.h"
  6. /* PCI includes legacy ISA access. */
  7. #include "isa.h"
  8. #include "pcie.h"
  9. /* PCI bus */
  10. #define PCI_DEVFN(slot, func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))
  11. #define PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f)
  12. #define PCI_FUNC(devfn) ((devfn) & 0x07)
  13. #define PCI_SLOT_MAX 32
  14. #define PCI_FUNC_MAX 8
  15. /* Class, Vendor and Device IDs from Linux's pci_ids.h */
  16. #include "pci_ids.h"
  17. /* QEMU-specific Vendor and Device ID definitions */
  18. /* IBM (0x1014) */
  19. #define PCI_DEVICE_ID_IBM_440GX 0x027f
  20. #define PCI_DEVICE_ID_IBM_OPENPIC2 0xffff
  21. /* Hitachi (0x1054) */
  22. #define PCI_VENDOR_ID_HITACHI 0x1054
  23. #define PCI_DEVICE_ID_HITACHI_SH7751R 0x350e
  24. /* Apple (0x106b) */
  25. #define PCI_DEVICE_ID_APPLE_343S1201 0x0010
  26. #define PCI_DEVICE_ID_APPLE_UNI_N_I_PCI 0x001e
  27. #define PCI_DEVICE_ID_APPLE_UNI_N_PCI 0x001f
  28. #define PCI_DEVICE_ID_APPLE_UNI_N_KEYL 0x0022
  29. #define PCI_DEVICE_ID_APPLE_IPID_USB 0x003f
  30. /* Realtek (0x10ec) */
  31. #define PCI_DEVICE_ID_REALTEK_8029 0x8029
  32. /* Xilinx (0x10ee) */
  33. #define PCI_DEVICE_ID_XILINX_XC2VP30 0x0300
  34. /* Marvell (0x11ab) */
  35. #define PCI_DEVICE_ID_MARVELL_GT6412X 0x4620
  36. /* QEMU/Bochs VGA (0x1234) */
  37. #define PCI_VENDOR_ID_QEMU 0x1234
  38. #define PCI_DEVICE_ID_QEMU_VGA 0x1111
  39. /* VMWare (0x15ad) */
  40. #define PCI_VENDOR_ID_VMWARE 0x15ad
  41. #define PCI_DEVICE_ID_VMWARE_SVGA2 0x0405
  42. #define PCI_DEVICE_ID_VMWARE_SVGA 0x0710
  43. #define PCI_DEVICE_ID_VMWARE_NET 0x0720
  44. #define PCI_DEVICE_ID_VMWARE_SCSI 0x0730
  45. #define PCI_DEVICE_ID_VMWARE_IDE 0x1729
  46. /* Intel (0x8086) */
  47. #define PCI_DEVICE_ID_INTEL_82551IT 0x1209
  48. #define PCI_DEVICE_ID_INTEL_82557 0x1229
  49. #define PCI_DEVICE_ID_INTEL_82801IR 0x2922
  50. /* Red Hat / Qumranet (for QEMU) -- see pci-ids.txt */
  51. #define PCI_VENDOR_ID_REDHAT_QUMRANET 0x1af4
  52. #define PCI_SUBVENDOR_ID_REDHAT_QUMRANET 0x1af4
  53. #define PCI_SUBDEVICE_ID_QEMU 0x1100
  54. #define PCI_DEVICE_ID_VIRTIO_NET 0x1000
  55. #define PCI_DEVICE_ID_VIRTIO_BLOCK 0x1001
  56. #define PCI_DEVICE_ID_VIRTIO_BALLOON 0x1002
  57. #define PCI_DEVICE_ID_VIRTIO_CONSOLE 0x1003
  58. #define FMT_PCIBUS PRIx64
  59. typedef void PCIConfigWriteFunc(PCIDevice *pci_dev,
  60. uint32_t address, uint32_t data, int len);
  61. typedef uint32_t PCIConfigReadFunc(PCIDevice *pci_dev,
  62. uint32_t address, int len);
  63. typedef void PCIMapIORegionFunc(PCIDevice *pci_dev, int region_num,
  64. pcibus_t addr, pcibus_t size, int type);
  65. typedef int PCIUnregisterFunc(PCIDevice *pci_dev);
  66. typedef struct PCIIORegion {
  67. pcibus_t addr; /* current PCI mapping address. -1 means not mapped */
  68. #define PCI_BAR_UNMAPPED (~(pcibus_t)0)
  69. pcibus_t size;
  70. pcibus_t filtered_size;
  71. uint8_t type;
  72. PCIMapIORegionFunc *map_func;
  73. ram_addr_t ram_addr;
  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. int msix_mmio_index;
  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. /* Version id needed for VMState */
  139. int32_t version_id;
  140. /* Offset of MSI capability in config space */
  141. uint8_t msi_cap;
  142. /* PCI Express */
  143. PCIExpressDevice exp;
  144. /* Location of option rom */
  145. char *romfile;
  146. ram_addr_t rom_offset;
  147. uint32_t rom_bar;
  148. };
  149. PCIDevice *pci_register_device(PCIBus *bus, const char *name,
  150. int instance_size, int devfn,
  151. PCIConfigReadFunc *config_read,
  152. PCIConfigWriteFunc *config_write);
  153. void pci_register_bar(PCIDevice *pci_dev, int region_num,
  154. pcibus_t size, uint8_t type,
  155. PCIMapIORegionFunc *map_func);
  156. void pci_register_bar_simple(PCIDevice *pci_dev, int region_num,
  157. pcibus_t size, uint8_t attr, ram_addr_t ram_addr);
  158. int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
  159. uint8_t offset, uint8_t size);
  160. void pci_del_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
  161. void pci_reserve_capability(PCIDevice *pci_dev, uint8_t offset, uint8_t 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. typedef void (*pci_set_irq_fn)(void *opaque, int irq_num, int level);
  170. typedef int (*pci_map_irq_fn)(PCIDevice *pci_dev, int irq_num);
  171. typedef enum {
  172. PCI_HOTPLUG_DISABLED,
  173. PCI_HOTPLUG_ENABLED,
  174. PCI_COLDPLUG_ENABLED,
  175. } PCIHotplugState;
  176. typedef int (*pci_hotplug_fn)(DeviceState *qdev, PCIDevice *pci_dev,
  177. PCIHotplugState state);
  178. void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
  179. const char *name, uint8_t devfn_min);
  180. PCIBus *pci_bus_new(DeviceState *parent, const char *name, uint8_t devfn_min);
  181. void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  182. void *irq_opaque, int nirq);
  183. int pci_bus_get_irq_level(PCIBus *bus, int irq_num);
  184. void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *dev);
  185. PCIBus *pci_register_bus(DeviceState *parent, const char *name,
  186. pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  187. void *irq_opaque, uint8_t devfn_min, int nirq);
  188. void pci_device_reset(PCIDevice *dev);
  189. void pci_bus_reset(PCIBus *bus);
  190. void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base);
  191. PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
  192. const char *default_devaddr);
  193. PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
  194. const char *default_devaddr);
  195. int pci_bus_num(PCIBus *s);
  196. void pci_for_each_device(PCIBus *bus, int bus_num, void (*fn)(PCIBus *bus, PCIDevice *d));
  197. PCIBus *pci_find_root_bus(int domain);
  198. int pci_find_domain(const PCIBus *bus);
  199. PCIBus *pci_find_bus(PCIBus *bus, int bus_num);
  200. PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn);
  201. int pci_qdev_find_device(const char *id, PCIDevice **pdev);
  202. PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr);
  203. int pci_parse_devaddr(const char *addr, int *domp, int *busp,
  204. unsigned int *slotp, unsigned int *funcp);
  205. int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
  206. unsigned *slotp);
  207. void do_pci_info_print(Monitor *mon, const QObject *data);
  208. void do_pci_info(Monitor *mon, QObject **ret_data);
  209. void pci_bridge_update_mappings(PCIBus *b);
  210. void pci_device_deassert_intx(PCIDevice *dev);
  211. static inline void
  212. pci_set_byte(uint8_t *config, uint8_t val)
  213. {
  214. *config = val;
  215. }
  216. static inline uint8_t
  217. pci_get_byte(const uint8_t *config)
  218. {
  219. return *config;
  220. }
  221. static inline void
  222. pci_set_word(uint8_t *config, uint16_t val)
  223. {
  224. cpu_to_le16wu((uint16_t *)config, val);
  225. }
  226. static inline uint16_t
  227. pci_get_word(const uint8_t *config)
  228. {
  229. return le16_to_cpupu((const uint16_t *)config);
  230. }
  231. static inline void
  232. pci_set_long(uint8_t *config, uint32_t val)
  233. {
  234. cpu_to_le32wu((uint32_t *)config, val);
  235. }
  236. static inline uint32_t
  237. pci_get_long(const uint8_t *config)
  238. {
  239. return le32_to_cpupu((const uint32_t *)config);
  240. }
  241. static inline void
  242. pci_set_quad(uint8_t *config, uint64_t val)
  243. {
  244. cpu_to_le64w((uint64_t *)config, val);
  245. }
  246. static inline uint64_t
  247. pci_get_quad(const uint8_t *config)
  248. {
  249. return le64_to_cpup((const uint64_t *)config);
  250. }
  251. static inline void
  252. pci_config_set_vendor_id(uint8_t *pci_config, uint16_t val)
  253. {
  254. pci_set_word(&pci_config[PCI_VENDOR_ID], val);
  255. }
  256. static inline void
  257. pci_config_set_device_id(uint8_t *pci_config, uint16_t val)
  258. {
  259. pci_set_word(&pci_config[PCI_DEVICE_ID], val);
  260. }
  261. static inline void
  262. pci_config_set_revision(uint8_t *pci_config, uint8_t val)
  263. {
  264. pci_set_byte(&pci_config[PCI_REVISION_ID], val);
  265. }
  266. static inline void
  267. pci_config_set_class(uint8_t *pci_config, uint16_t val)
  268. {
  269. pci_set_word(&pci_config[PCI_CLASS_DEVICE], val);
  270. }
  271. static inline void
  272. pci_config_set_prog_interface(uint8_t *pci_config, uint8_t val)
  273. {
  274. pci_set_byte(&pci_config[PCI_CLASS_PROG], val);
  275. }
  276. static inline void
  277. pci_config_set_interrupt_pin(uint8_t *pci_config, uint8_t val)
  278. {
  279. pci_set_byte(&pci_config[PCI_INTERRUPT_PIN], val);
  280. }
  281. /*
  282. * helper functions to do bit mask operation on configuration space.
  283. * Just to set bit, use test-and-set and discard returned value.
  284. * Just to clear bit, use test-and-clear and discard returned value.
  285. * NOTE: They aren't atomic.
  286. */
  287. static inline uint8_t
  288. pci_byte_test_and_clear_mask(uint8_t *config, uint8_t mask)
  289. {
  290. uint8_t val = pci_get_byte(config);
  291. pci_set_byte(config, val & ~mask);
  292. return val & mask;
  293. }
  294. static inline uint8_t
  295. pci_byte_test_and_set_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 uint16_t
  302. pci_word_test_and_clear_mask(uint8_t *config, uint16_t mask)
  303. {
  304. uint16_t val = pci_get_word(config);
  305. pci_set_word(config, val & ~mask);
  306. return val & mask;
  307. }
  308. static inline uint16_t
  309. pci_word_test_and_set_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 uint32_t
  316. pci_long_test_and_clear_mask(uint8_t *config, uint32_t mask)
  317. {
  318. uint32_t val = pci_get_long(config);
  319. pci_set_long(config, val & ~mask);
  320. return val & mask;
  321. }
  322. static inline uint32_t
  323. pci_long_test_and_set_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 uint64_t
  330. pci_quad_test_and_clear_mask(uint8_t *config, uint64_t mask)
  331. {
  332. uint64_t val = pci_get_quad(config);
  333. pci_set_quad(config, val & ~mask);
  334. return val & mask;
  335. }
  336. static inline uint64_t
  337. pci_quad_test_and_set_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. typedef int (*pci_qdev_initfn)(PCIDevice *dev);
  344. typedef struct {
  345. DeviceInfo qdev;
  346. pci_qdev_initfn init;
  347. PCIUnregisterFunc *exit;
  348. PCIConfigReadFunc *config_read;
  349. PCIConfigWriteFunc *config_write;
  350. uint16_t vendor_id;
  351. uint16_t device_id;
  352. uint8_t revision;
  353. uint16_t class_id;
  354. uint16_t subsystem_vendor_id; /* only for header type = 0 */
  355. uint16_t subsystem_id; /* only for header type = 0 */
  356. /*
  357. * pci-to-pci bridge or normal device.
  358. * This doesn't mean pci host switch.
  359. * When card bus bridge is supported, this would be enhanced.
  360. */
  361. int is_bridge;
  362. /* pcie stuff */
  363. int is_express; /* is this device pci express? */
  364. /* device isn't hot-pluggable */
  365. int no_hotplug;
  366. /* rom bar */
  367. const char *romfile;
  368. } PCIDeviceInfo;
  369. void pci_qdev_register(PCIDeviceInfo *info);
  370. void pci_qdev_register_many(PCIDeviceInfo *info);
  371. PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
  372. const char *name);
  373. PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
  374. bool multifunction,
  375. const char *name);
  376. PCIDevice *pci_try_create_multifunction(PCIBus *bus, int devfn,
  377. bool multifunction,
  378. const char *name);
  379. PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name);
  380. PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name);
  381. PCIDevice *pci_try_create(PCIBus *bus, int devfn, const char *name);
  382. static inline int pci_is_express(const PCIDevice *d)
  383. {
  384. return d->cap_present & QEMU_PCI_CAP_EXPRESS;
  385. }
  386. static inline uint32_t pci_config_size(const PCIDevice *d)
  387. {
  388. return pci_is_express(d) ? PCIE_CONFIG_SPACE_SIZE : PCI_CONFIG_SPACE_SIZE;
  389. }
  390. #endif