pci.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. How To Write Linux PCI Drivers
  2. by Martin Mares <mj@ucw.cz> on 07-Feb-2000
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. The world of PCI is vast and it's full of (mostly unpleasant) surprises.
  5. Different PCI devices have different requirements and different bugs --
  6. because of this, the PCI support layer in Linux kernel is not as trivial
  7. as one would wish. This short pamphlet tries to help all potential driver
  8. authors find their way through the deep forests of PCI handling.
  9. 0. Structure of PCI drivers
  10. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. There exist two kinds of PCI drivers: new-style ones (which leave most of
  12. probing for devices to the PCI layer and support online insertion and removal
  13. of devices [thus supporting PCI, hot-pluggable PCI and CardBus in a single
  14. driver]) and old-style ones which just do all the probing themselves. Unless
  15. you have a very good reason to do so, please don't use the old way of probing
  16. in any new code. After the driver finds the devices it wishes to operate
  17. on (either the old or the new way), it needs to perform the following steps:
  18. Enable the device
  19. Access device configuration space
  20. Discover resources (addresses and IRQ numbers) provided by the device
  21. Allocate these resources
  22. Communicate with the device
  23. Disable the device
  24. Most of these topics are covered by the following sections, for the rest
  25. look at <linux/pci.h>, it's hopefully well commented.
  26. If the PCI subsystem is not configured (CONFIG_PCI is not set), most of
  27. the functions described below are defined as inline functions either completely
  28. empty or just returning an appropriate error codes to avoid lots of ifdefs
  29. in the drivers.
  30. 1. New-style drivers
  31. ~~~~~~~~~~~~~~~~~~~~
  32. The new-style drivers just call pci_register_driver during their initialization
  33. with a pointer to a structure describing the driver (struct pci_driver) which
  34. contains:
  35. name Name of the driver
  36. id_table Pointer to table of device ID's the driver is
  37. interested in. Most drivers should export this
  38. table using MODULE_DEVICE_TABLE(pci,...).
  39. probe Pointer to a probing function which gets called (during
  40. execution of pci_register_driver for already existing
  41. devices or later if a new device gets inserted) for all
  42. PCI devices which match the ID table and are not handled
  43. by the other drivers yet. This function gets passed a
  44. pointer to the pci_dev structure representing the device
  45. and also which entry in the ID table did the device
  46. match. It returns zero when the driver has accepted the
  47. device or an error code (negative number) otherwise.
  48. This function always gets called from process context,
  49. so it can sleep.
  50. remove Pointer to a function which gets called whenever a
  51. device being handled by this driver is removed (either
  52. during deregistration of the driver or when it's
  53. manually pulled out of a hot-pluggable slot). This
  54. function always gets called from process context, so it
  55. can sleep.
  56. save_state Save a device's state before it's suspend.
  57. suspend Put device into low power state.
  58. resume Wake device from low power state.
  59. enable_wake Enable device to generate wake events from a low power
  60. state.
  61. (Please see Documentation/power/pci.txt for descriptions
  62. of PCI Power Management and the related functions)
  63. The ID table is an array of struct pci_device_id ending with a all-zero entry.
  64. Each entry consists of:
  65. vendor, device Vendor and device ID to match (or PCI_ANY_ID)
  66. subvendor, Subsystem vendor and device ID to match (or PCI_ANY_ID)
  67. subdevice
  68. class, Device class to match. The class_mask tells which bits
  69. class_mask of the class are honored during the comparison.
  70. driver_data Data private to the driver.
  71. Most drivers don't need to use the driver_data field. Best practice
  72. for use of driver_data is to use it as an index into a static list of
  73. equivalent device types, not to use it as a pointer.
  74. Have a table entry {PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID}
  75. to have probe() called for every PCI device known to the system.
  76. New PCI IDs may be added to a device driver at runtime by writing
  77. to the file /sys/bus/pci/drivers/{driver}/new_id. When added, the
  78. driver will probe for all devices it can support.
  79. echo "vendor device subvendor subdevice class class_mask driver_data" > \
  80. /sys/bus/pci/drivers/{driver}/new_id
  81. where all fields are passed in as hexadecimal values (no leading 0x).
  82. Users need pass only as many fields as necessary; vendor, device,
  83. subvendor, and subdevice fields default to PCI_ANY_ID (FFFFFFFF),
  84. class and classmask fields default to 0, and driver_data defaults to
  85. 0UL. Device drivers must initialize use_driver_data in the dynids struct
  86. in their pci_driver struct prior to calling pci_register_driver in order
  87. for the driver_data field to get passed to the driver. Otherwise, only a
  88. 0 is passed in that field.
  89. When the driver exits, it just calls pci_unregister_driver() and the PCI layer
  90. automatically calls the remove hook for all devices handled by the driver.
  91. Please mark the initialization and cleanup functions where appropriate
  92. (the corresponding macros are defined in <linux/init.h>):
  93. __init Initialization code. Thrown away after the driver
  94. initializes.
  95. __exit Exit code. Ignored for non-modular drivers.
  96. __devinit Device initialization code. Identical to __init if
  97. the kernel is not compiled with CONFIG_HOTPLUG, normal
  98. function otherwise.
  99. __devexit The same for __exit.
  100. Tips:
  101. The module_init()/module_exit() functions (and all initialization
  102. functions called only from these) should be marked __init/exit.
  103. The struct pci_driver shouldn't be marked with any of these tags.
  104. The ID table array should be marked __devinitdata.
  105. The probe() and remove() functions (and all initialization
  106. functions called only from these) should be marked __devinit/exit.
  107. If you are sure the driver is not a hotplug driver then use only
  108. __init/exit __initdata/exitdata.
  109. Pointers to functions marked as __devexit must be created using
  110. __devexit_p(function_name). That will generate the function
  111. name or NULL if the __devexit function will be discarded.
  112. 2. How to find PCI devices manually (the old style)
  113. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. PCI drivers not using the pci_register_driver() interface search
  115. for PCI devices manually using the following constructs:
  116. Searching by vendor and device ID:
  117. struct pci_dev *dev = NULL;
  118. while (dev = pci_get_device(VENDOR_ID, DEVICE_ID, dev))
  119. configure_device(dev);
  120. Searching by class ID (iterate in a similar way):
  121. pci_get_class(CLASS_ID, dev)
  122. Searching by both vendor/device and subsystem vendor/device ID:
  123. pci_get_subsys(VENDOR_ID, DEVICE_ID, SUBSYS_VENDOR_ID, SUBSYS_DEVICE_ID, dev).
  124. You can use the constant PCI_ANY_ID as a wildcard replacement for
  125. VENDOR_ID or DEVICE_ID. This allows searching for any device from a
  126. specific vendor, for example.
  127. These functions are hotplug-safe. They increment the reference count on
  128. the pci_dev that they return. You must eventually (possibly at module unload)
  129. decrement the reference count on these devices by calling pci_dev_put().
  130. 3. Enabling and disabling devices
  131. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. Before you do anything with the device you've found, you need to enable
  133. it by calling pci_enable_device() which enables I/O and memory regions of
  134. the device, allocates an IRQ if necessary, assigns missing resources if
  135. needed and wakes up the device if it was in suspended state. Please note
  136. that this function can fail.
  137. If you want to use the device in bus mastering mode, call pci_set_master()
  138. which enables the bus master bit in PCI_COMMAND register and also fixes
  139. the latency timer value if it's set to something bogus by the BIOS.
  140. If you want to use the PCI Memory-Write-Invalidate transaction,
  141. call pci_set_mwi(). This enables the PCI_COMMAND bit for Mem-Wr-Inval
  142. and also ensures that the cache line size register is set correctly.
  143. Make sure to check the return value of pci_set_mwi(), not all architectures
  144. may support Memory-Write-Invalidate.
  145. If your driver decides to stop using the device (e.g., there was an
  146. error while setting it up or the driver module is being unloaded), it
  147. should call pci_disable_device() to deallocate any IRQ resources, disable
  148. PCI bus-mastering, etc. You should not do anything with the device after
  149. calling pci_disable_device().
  150. 4. How to access PCI config space
  151. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  152. You can use pci_(read|write)_config_(byte|word|dword) to access the config
  153. space of a device represented by struct pci_dev *. All these functions return 0
  154. when successful or an error code (PCIBIOS_...) which can be translated to a text
  155. string by pcibios_strerror. Most drivers expect that accesses to valid PCI
  156. devices don't fail.
  157. If you don't have a struct pci_dev available, you can call
  158. pci_bus_(read|write)_config_(byte|word|dword) to access a given device
  159. and function on that bus.
  160. If you access fields in the standard portion of the config header, please
  161. use symbolic names of locations and bits declared in <linux/pci.h>.
  162. If you need to access Extended PCI Capability registers, just call
  163. pci_find_capability() for the particular capability and it will find the
  164. corresponding register block for you.
  165. 5. Addresses and interrupts
  166. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  167. Memory and port addresses and interrupt numbers should NOT be read from the
  168. config space. You should use the values in the pci_dev structure as they might
  169. have been remapped by the kernel.
  170. See Documentation/IO-mapping.txt for how to access device memory.
  171. The device driver needs to call pci_request_region() to make sure
  172. no other device is already using the same resource. The driver is expected
  173. to determine MMIO and IO Port resource availability _before_ calling
  174. pci_enable_device(). Conversely, drivers should call pci_release_region()
  175. _after_ calling pci_disable_device(). The idea is to prevent two devices
  176. colliding on the same address range.
  177. Generic flavors of pci_request_region() are request_mem_region()
  178. (for MMIO ranges) and request_region() (for IO Port ranges).
  179. Use these for address resources that are not described by "normal" PCI
  180. interfaces (e.g. BAR).
  181. All interrupt handlers should be registered with IRQF_SHARED and use the devid
  182. to map IRQs to devices (remember that all PCI interrupts are shared).
  183. 6. Other interesting functions
  184. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  185. pci_find_slot() Find pci_dev corresponding to given bus and
  186. slot numbers.
  187. pci_set_power_state() Set PCI Power Management state (0=D0 ... 3=D3)
  188. pci_find_capability() Find specified capability in device's capability
  189. list.
  190. pci_module_init() Inline helper function for ensuring correct
  191. pci_driver initialization and error handling.
  192. pci_resource_start() Returns bus start address for a given PCI region
  193. pci_resource_end() Returns bus end address for a given PCI region
  194. pci_resource_len() Returns the byte length of a PCI region
  195. pci_set_drvdata() Set private driver data pointer for a pci_dev
  196. pci_get_drvdata() Return private driver data pointer for a pci_dev
  197. pci_set_mwi() Enable Memory-Write-Invalidate transactions.
  198. pci_clear_mwi() Disable Memory-Write-Invalidate transactions.
  199. 7. Miscellaneous hints
  200. ~~~~~~~~~~~~~~~~~~~~~~
  201. When displaying PCI slot names to the user (for example when a driver wants
  202. to tell the user what card has it found), please use pci_name(pci_dev)
  203. for this purpose.
  204. Always refer to the PCI devices by a pointer to the pci_dev structure.
  205. All PCI layer functions use this identification and it's the only
  206. reasonable one. Don't use bus/slot/function numbers except for very
  207. special purposes -- on systems with multiple primary buses their semantics
  208. can be pretty complex.
  209. If you're going to use PCI bus mastering DMA, take a look at
  210. Documentation/DMA-mapping.txt.
  211. Don't try to turn on Fast Back to Back writes in your driver. All devices
  212. on the bus need to be capable of doing it, so this is something which needs
  213. to be handled by platform and generic code, not individual drivers.
  214. 8. Vendor and device identifications
  215. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  216. For the future, let's avoid adding device ids to include/linux/pci_ids.h.
  217. PCI_VENDOR_ID_xxx for vendors, and a hex constant for device ids.
  218. Rationale: PCI_VENDOR_ID_xxx constants are re-used, but device ids are not.
  219. Further, device ids are arbitrary hex numbers, normally used only in a
  220. single location, the pci_device_id table.
  221. 9. Obsolete functions
  222. ~~~~~~~~~~~~~~~~~~~~~
  223. There are several functions which you might come across when trying to
  224. port an old driver to the new PCI interface. They are no longer present
  225. in the kernel as they aren't compatible with hotplug or PCI domains or
  226. having sane locking.
  227. pci_find_device() Superseded by pci_get_device()
  228. pci_find_subsys() Superseded by pci_get_subsys()
  229. pci_find_slot() Superseded by pci_get_slot()