pciaccess.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * (C) Copyright IBM Corporation 2006
  3. * Copyright 2009 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * on the rights to use, copy, modify, merge, publish, distribute, sub
  10. * license, and/or sell copies of the Software, and to permit persons to whom
  11. * the Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  20. * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. /*
  26. * Copyright (c) 2007 Paulo R. Zanoni, Tiago Vignatti
  27. *
  28. * Permission is hereby granted, free of charge, to any person
  29. * obtaining a copy of this software and associated documentation
  30. * files (the "Software"), to deal in the Software without
  31. * restriction, including without limitation the rights to use,
  32. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  33. * copies of the Software, and to permit persons to whom the
  34. * Software is furnished to do so, subject to the following
  35. * conditions:
  36. *
  37. * The above copyright notice and this permission notice shall be
  38. * included in all copies or substantial portions of the Software.
  39. *
  40. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  41. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  42. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  43. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  44. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  45. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  46. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  47. * OTHER DEALINGS IN THE SOFTWARE.
  48. *
  49. */
  50. /**
  51. * \file pciaccess.h
  52. *
  53. * \author Ian Romanick <idr@us.ibm.com>
  54. */
  55. #ifndef PCIACCESS_H
  56. #define PCIACCESS_H
  57. #include <inttypes.h>
  58. #if (__GNUC__ >= 3) || (__SUNPRO_C >= 0x5130)
  59. #define __deprecated __attribute__((deprecated))
  60. #else
  61. #define __deprecated
  62. #endif
  63. typedef uint64_t pciaddr_t;
  64. struct pci_device;
  65. struct pci_device_iterator;
  66. struct pci_id_match;
  67. struct pci_slot_match;
  68. #ifdef __cplusplus
  69. extern "C" {
  70. #endif
  71. int pci_device_has_kernel_driver(struct pci_device *dev);
  72. int pci_device_is_boot_vga(struct pci_device *dev);
  73. int pci_device_read_rom(struct pci_device *dev, void *buffer);
  74. int __deprecated pci_device_map_region(struct pci_device *dev,
  75. unsigned region, int write_enable);
  76. int __deprecated pci_device_unmap_region(struct pci_device *dev,
  77. unsigned region);
  78. int pci_device_map_range(struct pci_device *dev, pciaddr_t base,
  79. pciaddr_t size, unsigned map_flags, void **addr);
  80. int pci_device_unmap_range(struct pci_device *dev, void *memory,
  81. pciaddr_t size);
  82. int __deprecated pci_device_map_memory_range(struct pci_device *dev,
  83. pciaddr_t base, pciaddr_t size, int write_enable, void **addr);
  84. int __deprecated pci_device_unmap_memory_range(struct pci_device *dev,
  85. void *memory, pciaddr_t size);
  86. int pci_device_probe(struct pci_device *dev);
  87. const struct pci_agp_info *pci_device_get_agp_info(struct pci_device *dev);
  88. const struct pci_bridge_info *pci_device_get_bridge_info(
  89. struct pci_device *dev);
  90. const struct pci_pcmcia_bridge_info *pci_device_get_pcmcia_bridge_info(
  91. struct pci_device *dev);
  92. int pci_device_get_bridge_buses(struct pci_device *dev, int *primary_bus,
  93. int *secondary_bus, int *subordinate_bus);
  94. int pci_system_init(void);
  95. void pci_system_init_dev_mem(int fd);
  96. void pci_system_cleanup(void);
  97. struct pci_device_iterator *pci_slot_match_iterator_create(
  98. const struct pci_slot_match *match);
  99. struct pci_device_iterator *pci_id_match_iterator_create(
  100. const struct pci_id_match *match);
  101. void pci_iterator_destroy(struct pci_device_iterator *iter);
  102. struct pci_device *pci_device_next(struct pci_device_iterator *iter);
  103. struct pci_device *pci_device_find_by_slot(uint32_t domain, uint32_t bus,
  104. uint32_t dev, uint32_t func);
  105. struct pci_device *pci_device_get_parent_bridge(struct pci_device *dev);
  106. void pci_get_strings(const struct pci_id_match *m,
  107. const char **device_name, const char **vendor_name,
  108. const char **subdevice_name, const char **subvendor_name);
  109. const char *pci_device_get_device_name(const struct pci_device *dev);
  110. const char *pci_device_get_subdevice_name(const struct pci_device *dev);
  111. const char *pci_device_get_vendor_name(const struct pci_device *dev);
  112. const char *pci_device_get_subvendor_name(const struct pci_device *dev);
  113. void pci_device_enable(struct pci_device *dev);
  114. int pci_device_cfg_read (struct pci_device *dev, void *data,
  115. pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read);
  116. int pci_device_cfg_read_u8 (struct pci_device *dev, uint8_t *data,
  117. pciaddr_t offset);
  118. int pci_device_cfg_read_u16(struct pci_device *dev, uint16_t *data,
  119. pciaddr_t offset);
  120. int pci_device_cfg_read_u32(struct pci_device *dev, uint32_t *data,
  121. pciaddr_t offset);
  122. int pci_device_cfg_write (struct pci_device *dev, const void *data,
  123. pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written);
  124. int pci_device_cfg_write_u8 (struct pci_device *dev, uint8_t data,
  125. pciaddr_t offset);
  126. int pci_device_cfg_write_u16(struct pci_device *dev, uint16_t data,
  127. pciaddr_t offset);
  128. int pci_device_cfg_write_u32(struct pci_device *dev, uint32_t data,
  129. pciaddr_t offset);
  130. int pci_device_cfg_write_bits(struct pci_device *dev, uint32_t mask,
  131. uint32_t data, pciaddr_t offset);
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. /**
  136. * \name Mapping flags passed to \c pci_device_map_range
  137. */
  138. /*@{*/
  139. #define PCI_DEV_MAP_FLAG_WRITABLE (1U<<0)
  140. #define PCI_DEV_MAP_FLAG_WRITE_COMBINE (1U<<1)
  141. #define PCI_DEV_MAP_FLAG_CACHABLE (1U<<2)
  142. /*@}*/
  143. #define PCI_MATCH_ANY (~0U)
  144. /**
  145. * Compare two PCI ID values (either vendor or device). This is used
  146. * internally to compare the fields of \c pci_id_match to the fields of
  147. * \c pci_device.
  148. */
  149. #define PCI_ID_COMPARE(a, b) \
  150. (((a) == PCI_MATCH_ANY) || ((a) == (b)))
  151. /**
  152. */
  153. struct pci_id_match {
  154. /**
  155. * \name Device / vendor matching controls
  156. *
  157. * Control the search based on the device, vendor, subdevice, or subvendor
  158. * IDs. Setting any of these fields to \c PCI_MATCH_ANY will cause the
  159. * field to not be used in the comparison.
  160. */
  161. /*@{*/
  162. uint32_t vendor_id;
  163. uint32_t device_id;
  164. uint32_t subvendor_id;
  165. uint32_t subdevice_id;
  166. /*@}*/
  167. /**
  168. * \name Device class matching controls
  169. *
  170. */
  171. /*@{*/
  172. uint32_t device_class;
  173. uint32_t device_class_mask;
  174. /*@}*/
  175. intptr_t match_data;
  176. };
  177. /**
  178. */
  179. struct pci_slot_match {
  180. /**
  181. * \name Device slot matching controls
  182. *
  183. * Control the search based on the domain, bus, slot, and function of
  184. * the device. Setting any of these fields to \c PCI_MATCH_ANY will cause
  185. * the field to not be used in the comparison.
  186. */
  187. /*@{*/
  188. uint32_t domain;
  189. uint32_t bus;
  190. uint32_t dev;
  191. uint32_t func;
  192. /*@}*/
  193. intptr_t match_data;
  194. };
  195. /**
  196. * BAR descriptor for a PCI device.
  197. */
  198. struct pci_mem_region {
  199. /**
  200. * When the region is mapped, this is the pointer to the memory.
  201. *
  202. * This field is \b only set when the deprecated \c pci_device_map_region
  203. * interface is used. Use \c pci_device_map_range instead.
  204. *
  205. * \deprecated
  206. */
  207. void *memory;
  208. /**
  209. * Base physical address of the region within its bus / domain.
  210. *
  211. * \warning
  212. * This address is really only useful to other devices in the same
  213. * domain. It's probably \b not the address applications will ever
  214. * use.
  215. *
  216. * \warning
  217. * Most (all?) platform back-ends leave this field unset.
  218. */
  219. pciaddr_t bus_addr;
  220. /**
  221. * Base physical address of the region from the CPU's point of view.
  222. *
  223. * This address is typically passed to \c pci_device_map_range to create
  224. * a mapping of the region to the CPU's virtual address space.
  225. */
  226. pciaddr_t base_addr;
  227. /**
  228. * Size, in bytes, of the region.
  229. */
  230. pciaddr_t size;
  231. /**
  232. * Is the region I/O ports or memory?
  233. */
  234. unsigned is_IO:1;
  235. /**
  236. * Is the memory region prefetchable?
  237. *
  238. * \note
  239. * This can only be set if \c is_IO is not set.
  240. */
  241. unsigned is_prefetchable:1;
  242. /**
  243. * Is the memory at a 64-bit address?
  244. *
  245. * \note
  246. * This can only be set if \c is_IO is not set.
  247. */
  248. unsigned is_64:1;
  249. };
  250. /**
  251. * PCI device.
  252. *
  253. * Contains all of the information about a particular PCI device.
  254. */
  255. struct pci_device {
  256. /**
  257. * \name Device bus identification.
  258. *
  259. * Complete bus identification, including domain, of the device. On
  260. * platforms that do not support PCI domains (e.g., 32-bit x86 hardware),
  261. * the domain will always be zero.
  262. */
  263. /*@{*/
  264. uint16_t domain;
  265. uint8_t bus;
  266. uint8_t dev;
  267. uint8_t func;
  268. /*@}*/
  269. /**
  270. * \name Vendor / device ID
  271. *
  272. * The vendor ID, device ID, and sub-IDs for the device.
  273. */
  274. /*@{*/
  275. uint16_t vendor_id;
  276. uint16_t device_id;
  277. uint16_t subvendor_id;
  278. uint16_t subdevice_id;
  279. /*@}*/
  280. /**
  281. * Device's class, subclass, and programming interface packed into a
  282. * single 32-bit value. The class is at bits [23:16], subclass is at
  283. * bits [15:8], and programming interface is at [7:0].
  284. */
  285. uint32_t device_class;
  286. /**
  287. * Device revision number, as read from the configuration header.
  288. */
  289. uint8_t revision;
  290. /**
  291. * BAR descriptors for the device.
  292. */
  293. struct pci_mem_region regions[6];
  294. /**
  295. * Size, in bytes, of the device's expansion ROM.
  296. */
  297. pciaddr_t rom_size;
  298. /**
  299. * IRQ associated with the device. If there is no IRQ, this value will
  300. * be -1.
  301. */
  302. int irq;
  303. /**
  304. * Storage for user data. Users of the library can store arbitrary
  305. * data in this pointer. The library will not use it for any purpose.
  306. * It is the user's responsability to free this memory before destroying
  307. * the \c pci_device structure.
  308. */
  309. intptr_t user_data;
  310. /**
  311. * Used by the VGA arbiter. Type of resource decoded by the device and
  312. * the file descriptor (/dev/vga_arbiter). */
  313. int vgaarb_rsrc;
  314. };
  315. /**
  316. * Description of the AGP capability of the device.
  317. *
  318. * \sa pci_device_get_agp_info
  319. */
  320. struct pci_agp_info {
  321. /**
  322. * Offset of the AGP registers in the devices configuration register
  323. * space. This is generally used so that the offset of the AGP command
  324. * register can be determined.
  325. */
  326. unsigned config_offset;
  327. /**
  328. * \name AGP major / minor version.
  329. */
  330. /*@{*/
  331. uint8_t major_version;
  332. uint8_t minor_version;
  333. /*@}*/
  334. /**
  335. * Logical OR of the supported AGP rates. For example, a value of 0x07
  336. * means that the device can support 1x, 2x, and 4x. A value of 0x0c
  337. * means that the device can support 8x and 4x.
  338. */
  339. uint8_t rates;
  340. unsigned int fast_writes:1; /**< Are fast-writes supported? */
  341. unsigned int addr64:1;
  342. unsigned int htrans:1;
  343. unsigned int gart64:1;
  344. unsigned int coherent:1;
  345. unsigned int sideband:1; /**< Is side-band addressing supported? */
  346. unsigned int isochronus:1;
  347. uint8_t async_req_size;
  348. uint8_t calibration_cycle_timing;
  349. uint8_t max_requests;
  350. };
  351. /**
  352. * Description of a PCI-to-PCI bridge device.
  353. *
  354. * \sa pci_device_get_bridge_info
  355. */
  356. struct pci_bridge_info {
  357. uint8_t primary_bus;
  358. uint8_t secondary_bus;
  359. uint8_t subordinate_bus;
  360. uint8_t secondary_latency_timer;
  361. uint8_t io_type;
  362. uint8_t mem_type;
  363. uint8_t prefetch_mem_type;
  364. uint16_t secondary_status;
  365. uint16_t bridge_control;
  366. uint32_t io_base;
  367. uint32_t io_limit;
  368. uint32_t mem_base;
  369. uint32_t mem_limit;
  370. uint64_t prefetch_mem_base;
  371. uint64_t prefetch_mem_limit;
  372. };
  373. /**
  374. * Description of a PCI-to-PCMCIA bridge device.
  375. *
  376. * \sa pci_device_get_pcmcia_bridge_info
  377. */
  378. struct pci_pcmcia_bridge_info {
  379. uint8_t primary_bus;
  380. uint8_t card_bus;
  381. uint8_t subordinate_bus;
  382. uint8_t cardbus_latency_timer;
  383. uint16_t secondary_status;
  384. uint16_t bridge_control;
  385. struct {
  386. uint32_t base;
  387. uint32_t limit;
  388. } io[2];
  389. struct {
  390. uint32_t base;
  391. uint32_t limit;
  392. } mem[2];
  393. };
  394. /**
  395. * VGA Arbiter definitions, functions and related.
  396. */
  397. /* Legacy VGA regions */
  398. #define VGA_ARB_RSRC_NONE 0x00
  399. #define VGA_ARB_RSRC_LEGACY_IO 0x01
  400. #define VGA_ARB_RSRC_LEGACY_MEM 0x02
  401. /* Non-legacy access */
  402. #define VGA_ARB_RSRC_NORMAL_IO 0x04
  403. #define VGA_ARB_RSRC_NORMAL_MEM 0x08
  404. int pci_device_vgaarb_init (void);
  405. void pci_device_vgaarb_fini (void);
  406. int pci_device_vgaarb_set_target (struct pci_device *dev);
  407. /* use the targetted device */
  408. int pci_device_vgaarb_decodes (int new_vga_rsrc);
  409. int pci_device_vgaarb_lock (void);
  410. int pci_device_vgaarb_trylock (void);
  411. int pci_device_vgaarb_unlock (void);
  412. /* return the current device count + resource decodes for the device */
  413. int pci_device_vgaarb_get_info (struct pci_device *dev, int *vga_count, int *rsrc_decodes);
  414. /*
  415. * I/O space access.
  416. */
  417. struct pci_io_handle;
  418. struct pci_io_handle *pci_device_open_io(struct pci_device *dev, pciaddr_t base,
  419. pciaddr_t size);
  420. struct pci_io_handle *pci_legacy_open_io(struct pci_device *dev, pciaddr_t base,
  421. pciaddr_t size);
  422. void pci_device_close_io(struct pci_device *dev, struct pci_io_handle *handle);
  423. uint32_t pci_io_read32(struct pci_io_handle *handle, uint32_t reg);
  424. uint16_t pci_io_read16(struct pci_io_handle *handle, uint32_t reg);
  425. uint8_t pci_io_read8(struct pci_io_handle *handle, uint32_t reg);
  426. void pci_io_write32(struct pci_io_handle *handle, uint32_t reg, uint32_t data);
  427. void pci_io_write16(struct pci_io_handle *handle, uint32_t reg, uint16_t data);
  428. void pci_io_write8(struct pci_io_handle *handle, uint32_t reg, uint8_t data);
  429. /*
  430. * Legacy memory access
  431. */
  432. int pci_device_map_legacy(struct pci_device *dev, pciaddr_t base,
  433. pciaddr_t size, unsigned map_flags, void **addr);
  434. int pci_device_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size);
  435. #endif /* PCIACCESS_H */