Something went wrong. Try again.
(I'm so sorry!) Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Something went wrong. Try again.
17 kB · 402 lines
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403.. SPDX-License-Identifier: GPL-2.0
===================================Writing PCI Host Controller Drivers===================================
:Author: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
Introduction============
A PCI Host Controller driver controls PCI Root Complex (RC) hardware. TheRoot Complex hardware comprises a single PCI Host Bridge and one or moreRoot Port or Root Complex Integrated Endpoint (RCiEP) devices::
+------------------+ | CPU | +------------------+ | +--------------------------------------------+ | | Root | | +------------------+ Complex | | | Host Bridge | | | +------------------+ | | | | | Bus 0 | | | +------------|----------+ | | | | | | | +----------+ +----------+ +-------+ | | | Root | | Root | | RCiEP | | | | Port | | Port | +-------+ | | +----------+ +----------+ | | | | | | Bus 1 | Bus 2 | | | | | | +-------|-------------|----------------------+ | | +-----------+ +-----------+ | Endpoint | | Endpoint | +-----------+ +-----------+
Host Bridge: Used to connect CPU(s) to the PCI hierarchy.
Root Port: Virtual PCI-PCI bridge connecting the Host Bridge to a PCI bus.
RCiEP: Embedded PCIe Endpoint inside Root Complex connected to the HostBridge.
Endpoint: PCIe device connected to a Root Port through a Link.
Enumeration===========
The Host Bridge device is not discoverable, so it is typically enumeratedwith the help of firmware interfaces like ACPI or Devicetree. But the RootPort and RCiEP devices are discoverable through the standard enumerationprocess defined in the PCIe spec.
A Host Controller driver usually configures both Host Bridge and RootPort(s) based on platform requirements. In the case of ACPI on standardizedplatforms (e.g. x86), no platform-specific host controller driver isrequired as the firmware configures the Root Complex before OS boot andexposes the resource information through ACPI tables. For more info, referto :doc:`../acpi-info`.
For Devicetree platforms, a dedicated host controller driver is oftenrequired because the Root Complex hardware typically needs vendor-specificinitialization like PHY, clocks, power domains, and there is no standardmechanism equivalent to ACPI/MCFG to convey resource information to the OS.So on these platforms, Root Complex hardware is enumerated throughDevicetree nodes as below::
pcie@10000000 { compatible = "vendor,soc-pcie"; reg = <0x0 0x10000000 0x0 0x1000>, <0x0 0x10001000 0x0 0x1000>; reg-names = "cfg", "app"; device_type = "pci"; bus-range = <0x00 0xff>; linux,pci-domain = <0>; num-lanes = <4>;
#address-cells = <3>; #size-cells = <2>;
ranges = <0x01000000 0x0 0x00000000 0x0 0x20000000 0x0 0x00100000>, <0x02000000 0x0 0x20100000 0x0 0x20100000 0x0 0x1ff00000>; dma-ranges = <0x02000000 0x0 0x0 0x0 0x0 0x0 0x80000000>;
clocks = <&clkc PCIE_CORE_CLK>, <&clkc PCIE_AUX_CLK>; clock-names = "core", "aux"; resets = <&reset PCIE_RESET>; power-domains = <&power PCIE_PD>;
#interrupt-cells = <1>; interrupt-map-mask = <0 0 0 0x7>; interrupt-map = <0 0 0 1 &gic 0 0 GIC_SPI 100 IRQ_TYPE_LEVEL_HIGH>, <0 0 0 2 &gic 0 0 GIC_SPI 101 IRQ_TYPE_LEVEL_HIGH>, <0 0 0 3 &gic 0 0 GIC_SPI 102 IRQ_TYPE_LEVEL_HIGH>, <0 0 0 4 &gic 0 0 GIC_SPI 103 IRQ_TYPE_LEVEL_HIGH>; interrupts = <GIC_SPI 104 IRQ_TYPE_LEVEL_HIGH>; interrupt-names = "msi";
pcie@0 { compatible = "pciclass,0604"; device_type = "pci"; reg = <0x0 0x0 0x0 0x0 0x0>; bus-range = <0x01 0xff>;
#address-cells = <3>; #size-cells = <2>; ranges;
phys = <&pcie_phy>; reset-gpios = <&gpio 10 GPIO_ACTIVE_LOW>; wake-gpios = <&gpio 11 GPIO_ACTIVE_LOW>; }; };
Note the presence of two nodes in the above example. The ``pcie@10000000``node represents a PCI Host Bridge device, and ``pcie@0`` represents asingle Root Port device. The Host Bridge node should contain propertiesassociated with the Host Bridge device such as ranges, interrupts, clocks,power-domains etc... and the Root Port node should contain port-specificproperties such as phys, reset-gpios, wake-gpios etc...
NOTE: Legacy Devicetrees used a single node to describe both Host Bridgeand Root Port devices, but that design is now deprecated.
Driver Design=============
Prerequisites-------------
Before starting to write a new Host Controller driver, check if any of theexisting drivers can be reused. For example, if the Root Complex supportsthe Enhanced Configuration Access Mechanism (ECAM) and the bootloader hasconfigured the ECAM mapping before OS boot, the ``CONFIG_PCI_HOST_GENERIC``driver can be used.
If the Root Complex hardware (IP) is from IP vendors such as Synopsys orCadence, the existing ``CONFIG_PCIE_DW_PLAT_HOST`` and``CONFIG_PCIE_CADENCE_PLAT_HOST`` drivers can be reused. If not, then checkif any of the existing glue drivers available for these IPs could bereused.
If the Root Complex hardware is designed in-house by the SoC vendor, checkif there is an existing driver from the vendor for their previousgeneration Root Complex hardware. Often, the existing driver can bereused with minimal modifications.
Only if the Root Complex doesn't satisfy above prerequisites should a newHost Controller driver be written.
Probe-----
During the Host Controller driver probe(), it initializes the Root Complexhardware and registers the Host Bridge with the PCI core. The typical stepsare described below.
Initialize Resources~~~~~~~~~~~~~~~~~~~~
At the start of the probe(), initialize Host Bridge-specific resources suchas clocks, PHYs, regulators, and resets. These resources are described inthe Host Bridge Devicetree node and should be brought up before accessingthe controller hardware.
NOTE: Use the devm_*() managed APIs wherever possible so the resources arereleased automatically on probe failure and on driver detach.
Configuration Space Access~~~~~~~~~~~~~~~~~~~~~~~~~~
The PCI core accesses the Configuration Space of the enumerated devicesthrough the callbacks provided by the driver in struct pci_ops. Thesecallbacks abstract how the Root Complex generates a Configuration Requestfor a given Bus, Device and Function number.
If the Root Complex supports ECAM, the generic accessors can be reused byusing pci_ecam_map_bus() along with pci_generic_config_read() andpci_generic_config_write(). Such drivers can often be built on top ofpci_host_common_probe() without providing any custom accessors.
Setup Address Translation~~~~~~~~~~~~~~~~~~~~~~~~~
The Host Bridge translates accesses between the CPU address domain and thePCI address domain in both directions:
- Outbound: CPU addresses are translated to PCI bus addresses for the Memory and I/O accesses initiated by the CPU towards the downstream devices. These windows are derived from the ``ranges`` property of the Host Bridge Devicetree node.
- Inbound: PCI bus addresses are translated to system memory addresses for the accesses (such as DMA) initiated by the downstream devices. These windows are derived from the ``dma-ranges`` property.
The PCI core parses ``ranges`` and ``dma-ranges`` into the Host Bridgeresource lists, and the driver programs one translation window per entry.Note that the CPU address and the PCI bus address of a window may differ,so the offset between them has to be accounted for while programming thewindows.
NOTE: If the hardware supports ECAM, it is strongly recommended to use ECAMfor the Configuration Space so a translation window need not bereprogrammed for every Configuration access.
Interrupt Handling~~~~~~~~~~~~~~~~~~
Downstream devices can signal interrupts either through INTx or throughMessage Signaled Interrupts (MSI/MSI-X). The driver has to enable themechanisms supported by the Root Complex.
INTx interrupts are conveyed to the Root Complex through the Assert_INTxand Deassert_INTx messages and are then reported as system interrupts. Thedriver typically creates an IRQ domain for the four interrupts (INTA toINTD) and demultiplexes an incoming interrupt to the corresponding virtualIRQ.
An MSI/MSI-X is signaled by the downstream device as a Memory Write to aRoot Complex-specific address. There are two ways to handle them:
- If the Root Complex integrates its own MSI controller, the driver has to create an MSI IRQ domain, program the MSI target address and demultiplex the incoming MSIs to the corresponding virtual IRQs. MSI-X is handled through the same domain.
- If the MSIs are handled by an external interrupt controller (such as the GIC ITS), the Root Complex Devicetree node needs to have an ``msi-parent`` property and the driver need not implement an MSI controller.
Powering up the Slot/Endpoint~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Power ON any slots or Endpoints connected to the bus with the help of thePWRCTRL subsystem APIs such as pci_pwrctrl_create_devices() andpci_pwrctrl_power_on_devices(). Note that this requires defining thesupplies in the Root Port or Endpoint Devicetree node.
Link Training~~~~~~~~~~~~~
Once the resources are initialized, the driver has to initiate Linktraining by enabling the LTSSM (Link Training and Status State Machine) ofthe Root Port. If a PERST# signal is present, it should be deasserted tobring the downstream device out of fundamental reset before enabling theLTSSM.
Before PERST# is deasserted, the driver must satisfy the power sequencingdelays defined by the PCI Express Card Electromechanical (CEM)Specification. The power supplies must be stable for at least T_PVPERL(``PCIE_T_PVPERL_MS``, 100 ms) and the reference clock must be stable forat least T_PERST-CLK (``PCIE_T_PERST_CLK_US``, 100 us) before PERST# isdeasserted.
After the LTSSM is enabled, the driver should wait (with a timeout) for theLTSSM to reach the L0 state, indicating that the Link is up.
Once the Link is up, the PCI Express Base Specification (ConventionalReset) requires software to wait for at least ``PCIE_RESET_CONFIG_WAIT_MS``(100 ms) before sending the first Configuration Request to the downstreamdevice. For a Link operating up to 5.0 GT/s, this delay is counted from theexit of the Conventional Reset (PERST# deassertion), while for a Linkoperating above 5.0 GT/s it is counted from the completion of Linktraining. The driver should honor this delay before the bus is scanned.
NOTE: A failure to establish the Link should NOT be treated as a probefailure unless the Root Port is not Hotplug capable. If the Root Port isHotplug capable, then the driver should still register the Host Bridge andscan the bus, so that the downstream device can be discovered later whenthe Link comes up.
Register the Host Bridge~~~~~~~~~~~~~~~~~~~~~~~~
Finally, allocate the Host Bridge device with devm_pci_alloc_host_bridge(),assign the Configuration Space accessors (struct pci_ops) to it, and startthe bus scan by calling pci_host_probe(). This is the last step of theprobe(). pci_host_probe() creates the Root bus for the Host Bridge andscans/enumerates all the Root Port, RCiEP and Endpoint devices connected tothe bus.
If the Root Complex IP is from a known IP vendor, the IP specific helpersshould be reused for the above operations wherever applicable.
Power Management----------------
A Host Controller driver participates in both runtime and system-wide powermanagement. In both cases, the driver is responsible for the power state ofthe Root Complex hardware, while the PCI core manages the power state ofthe enumerated devices.
Runtime PM~~~~~~~~~~
Runtime PM allows the Root Complex hardware to be powered down when it isidle. The driver typically enables runtime PM with pm_runtime_enable() andtakes a reference with pm_runtime_get_sync() during probe(), so that thecontroller stays powered while it is in use. The reference is dropped inremove().
If the Root Complex can be powered down when idle, the driver implementsthe runtime_suspend and runtime_resume callbacks to disable and enable thecontroller resources such as the clocks, PHYs, and power domain. Thesecallbacks should manage only the controller resources and must not touchthe state of the enumerated devices, which is handled by the PCI core.
System PM~~~~~~~~~
During system suspend and resume, the driver has to save and restore thestate of the Root Complex and put the Link into a low power state.
These operations are performed in the _noirq() PM callbacks (for example,using NOIRQ_SYSTEM_SLEEP_PM_OPS()), because the controller resources suchas the clocks and PHY are shared by all the child devices. Suspending themearlier would break the child devices whose own suspend callbacks may stillaccess their Configuration Space.
In the suspend callback, the driver should:
- Broadcast a PME_Turn_Off message and wait for the PME_TO_Ack, so that the Link can transition to the L2/L3 state.- Stop the LTSSM and disable the controller resources such as the clocks, PHY and power domain.- Save any controller state that is not retained across the low power state.- Power off the downstream devices using pci_pwrctrl_power_off_devices().
In the resume callback, the driver should reverse the above by enabling thecontroller resources, restoring the saved state, re-initializing the RootComplex and re-establishing the Link as done during probe().
NOTE: If the Link is in the ASPM L1 (or L1 substates) state, some driverskeep the Link in L1 across suspend for a faster resume, instead oftransitioning it to L2/L3. This is a driver policy decision based on theplatform and the devices connected.
Shutdown--------
The shutdown() callback is invoked during system reboot or whentransitioning to a new kernel through kexec. Its purpose is to quiesce theRoot Complex so that the downstream devices cannot corrupt the memory orinterrupt the new kernel.
The driver should:
- Disable the interrupts (INTx and MSI) reported by the Root Complex so that no spurious interrupt is delivered to the new kernel.- Broadcast a PME_Turn_Off message and stop the LTSSM to bring the Link down so that any in-flight DMA from the downstream devices is stopped before the reset.- Power down the controller resources.
Unlike remove(), shutdown() does not need to tear down the software statesuch as the Root bus, since the system is going down anyway.
NOTE: shutdown() is optional. It is mainly required on platforms where thedownstream devices could perform DMA or raise interrupts during thetransition to reboot or kexec.
Remove------
remove() is called when the driver is detached, and it should undoeverything done in probe() in the reverse order.
The first step is to remove the enumerated devices and the Root bus, bycalling pci_stop_root_bus() followed by pci_remove_root_bus() under thepci_lock_rescan_remove() lock. This detaches all the child devices beforethe controller resources are released.
After the bus is removed, the driver should:
- Disable the interrupts reported by the Root Complex.- Stop the LTSSM to bring the Link down.- Power down the PHY and disable the clocks, regulators and resets.- Drop the runtime PM reference with pm_runtime_put_sync() and disable runtime PM with pm_runtime_disable().
Resources allocated through the devm_*() APIs are released automaticallyafter remove() returns and need not be freed explicitly.
NOTE: A Host Controller driver is encouraged to be built as a loadablemodule, but it should not be removed at runtime if it implements its ownIRQ domains such as MSI or INTx controllers. The IRQ mappings created forsuch domains can persist even after the interrupts are released and cannotbe disposed of safely, so tearing down the IRQ domains on removal isfragile. Such drivers should therefore prevent their removal. See thefollowing thread for more details:https://lore.kernel.org/linux-pci/87k085xekg.wl-maz@kernel.org/