pciebus-howto.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. include:: <isonum.txt>
  3. ===========================================
  4. The PCI Express Port Bus Driver Guide HOWTO
  5. ===========================================
  6. :Author: Tom L Nguyen [email protected] 11/03/2004
  7. :Copyright: |copy| 2004 Intel Corporation
  8. About this guide
  9. ================
  10. This guide describes the basics of the PCI Express Port Bus driver
  11. and provides information on how to enable the service drivers to
  12. register/unregister with the PCI Express Port Bus Driver.
  13. What is the PCI Express Port Bus Driver
  14. =======================================
  15. A PCI Express Port is a logical PCI-PCI Bridge structure. There
  16. are two types of PCI Express Port: the Root Port and the Switch
  17. Port. The Root Port originates a PCI Express link from a PCI Express
  18. Root Complex and the Switch Port connects PCI Express links to
  19. internal logical PCI buses. The Switch Port, which has its secondary
  20. bus representing the switch's internal routing logic, is called the
  21. switch's Upstream Port. The switch's Downstream Port is bridging from
  22. switch's internal routing bus to a bus representing the downstream
  23. PCI Express link from the PCI Express Switch.
  24. A PCI Express Port can provide up to four distinct functions,
  25. referred to in this document as services, depending on its port type.
  26. PCI Express Port's services include native hotplug support (HP),
  27. power management event support (PME), advanced error reporting
  28. support (AER), and virtual channel support (VC). These services may
  29. be handled by a single complex driver or be individually distributed
  30. and handled by corresponding service drivers.
  31. Why use the PCI Express Port Bus Driver?
  32. ========================================
  33. In existing Linux kernels, the Linux Device Driver Model allows a
  34. physical device to be handled by only a single driver. The PCI
  35. Express Port is a PCI-PCI Bridge device with multiple distinct
  36. services. To maintain a clean and simple solution each service
  37. may have its own software service driver. In this case several
  38. service drivers will compete for a single PCI-PCI Bridge device.
  39. For example, if the PCI Express Root Port native hotplug service
  40. driver is loaded first, it claims a PCI-PCI Bridge Root Port. The
  41. kernel therefore does not load other service drivers for that Root
  42. Port. In other words, it is impossible to have multiple service
  43. drivers load and run on a PCI-PCI Bridge device simultaneously
  44. using the current driver model.
  45. To enable multiple service drivers running simultaneously requires
  46. having a PCI Express Port Bus driver, which manages all populated
  47. PCI Express Ports and distributes all provided service requests
  48. to the corresponding service drivers as required. Some key
  49. advantages of using the PCI Express Port Bus driver are listed below:
  50. - Allow multiple service drivers to run simultaneously on
  51. a PCI-PCI Bridge Port device.
  52. - Allow service drivers implemented in an independent
  53. staged approach.
  54. - Allow one service driver to run on multiple PCI-PCI Bridge
  55. Port devices.
  56. - Manage and distribute resources of a PCI-PCI Bridge Port
  57. device to requested service drivers.
  58. Configuring the PCI Express Port Bus Driver vs. Service Drivers
  59. ===============================================================
  60. Including the PCI Express Port Bus Driver Support into the Kernel
  61. -----------------------------------------------------------------
  62. Including the PCI Express Port Bus driver depends on whether the PCI
  63. Express support is included in the kernel config. The kernel will
  64. automatically include the PCI Express Port Bus driver as a kernel
  65. driver when the PCI Express support is enabled in the kernel.
  66. Enabling Service Driver Support
  67. -------------------------------
  68. PCI device drivers are implemented based on Linux Device Driver Model.
  69. All service drivers are PCI device drivers. As discussed above, it is
  70. impossible to load any service driver once the kernel has loaded the
  71. PCI Express Port Bus Driver. To meet the PCI Express Port Bus Driver
  72. Model requires some minimal changes on existing service drivers that
  73. imposes no impact on the functionality of existing service drivers.
  74. A service driver is required to use the two APIs shown below to
  75. register its service with the PCI Express Port Bus driver (see
  76. section 5.2.1 & 5.2.2). It is important that a service driver
  77. initializes the pcie_port_service_driver data structure, included in
  78. header file /include/linux/pcieport_if.h, before calling these APIs.
  79. Failure to do so will result an identity mismatch, which prevents
  80. the PCI Express Port Bus driver from loading a service driver.
  81. pcie_port_service_register
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. ::
  84. int pcie_port_service_register(struct pcie_port_service_driver *new)
  85. This API replaces the Linux Driver Model's pci_register_driver API. A
  86. service driver should always calls pcie_port_service_register at
  87. module init. Note that after service driver being loaded, calls
  88. such as pci_enable_device(dev) and pci_set_master(dev) are no longer
  89. necessary since these calls are executed by the PCI Port Bus driver.
  90. pcie_port_service_unregister
  91. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. ::
  93. void pcie_port_service_unregister(struct pcie_port_service_driver *new)
  94. pcie_port_service_unregister replaces the Linux Driver Model's
  95. pci_unregister_driver. It's always called by service driver when a
  96. module exits.
  97. Sample Code
  98. ~~~~~~~~~~~
  99. Below is sample service driver code to initialize the port service
  100. driver data structure.
  101. ::
  102. static struct pcie_port_service_id service_id[] = { {
  103. .vendor = PCI_ANY_ID,
  104. .device = PCI_ANY_ID,
  105. .port_type = PCIE_RC_PORT,
  106. .service_type = PCIE_PORT_SERVICE_AER,
  107. }, { /* end: all zeroes */ }
  108. };
  109. static struct pcie_port_service_driver root_aerdrv = {
  110. .name = (char *)device_name,
  111. .id_table = &service_id[0],
  112. .probe = aerdrv_load,
  113. .remove = aerdrv_unload,
  114. .suspend = aerdrv_suspend,
  115. .resume = aerdrv_resume,
  116. };
  117. Below is a sample code for registering/unregistering a service
  118. driver.
  119. ::
  120. static int __init aerdrv_service_init(void)
  121. {
  122. int retval = 0;
  123. retval = pcie_port_service_register(&root_aerdrv);
  124. if (!retval) {
  125. /*
  126. * FIX ME
  127. */
  128. }
  129. return retval;
  130. }
  131. static void __exit aerdrv_service_exit(void)
  132. {
  133. pcie_port_service_unregister(&root_aerdrv);
  134. }
  135. module_init(aerdrv_service_init);
  136. module_exit(aerdrv_service_exit);
  137. Possible Resource Conflicts
  138. ===========================
  139. Since all service drivers of a PCI-PCI Bridge Port device are
  140. allowed to run simultaneously, below lists a few of possible resource
  141. conflicts with proposed solutions.
  142. MSI and MSI-X Vector Resource
  143. -----------------------------
  144. Once MSI or MSI-X interrupts are enabled on a device, it stays in this
  145. mode until they are disabled again. Since service drivers of the same
  146. PCI-PCI Bridge port share the same physical device, if an individual
  147. service driver enables or disables MSI/MSI-X mode it may result
  148. unpredictable behavior.
  149. To avoid this situation all service drivers are not permitted to
  150. switch interrupt mode on its device. The PCI Express Port Bus driver
  151. is responsible for determining the interrupt mode and this should be
  152. transparent to service drivers. Service drivers need to know only
  153. the vector IRQ assigned to the field irq of struct pcie_device, which
  154. is passed in when the PCI Express Port Bus driver probes each service
  155. driver. Service drivers should use (struct pcie_device*)dev->irq to
  156. call request_irq/free_irq. In addition, the interrupt mode is stored
  157. in the field interrupt_mode of struct pcie_device.
  158. PCI Memory/IO Mapped Regions
  159. ----------------------------
  160. Service drivers for PCI Express Power Management (PME), Advanced
  161. Error Reporting (AER), Hot-Plug (HP) and Virtual Channel (VC) access
  162. PCI configuration space on the PCI Express port. In all cases the
  163. registers accessed are independent of each other. This patch assumes
  164. that all service drivers will be well behaved and not overwrite
  165. other service driver's configuration settings.
  166. PCI Config Registers
  167. --------------------
  168. Each service driver runs its PCI config operations on its own
  169. capability structure except the PCI Express capability structure, in
  170. which Root Control register and Device Control register are shared
  171. between PME and AER. This patch assumes that all service drivers
  172. will be well behaved and not overwrite other service driver's
  173. configuration settings.