18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci============================== 48c2ecf20Sopenharmony_ciHow To Write Linux PCI Drivers 58c2ecf20Sopenharmony_ci============================== 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci:Authors: - Martin Mares <mj@ucw.cz> 88c2ecf20Sopenharmony_ci - Grant Grundler <grundler@parisc-linux.org> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ciThe world of PCI is vast and full of (mostly unpleasant) surprises. 118c2ecf20Sopenharmony_ciSince each CPU architecture implements different chip-sets and PCI devices 128c2ecf20Sopenharmony_cihave different requirements (erm, "features"), the result is the PCI support 138c2ecf20Sopenharmony_ciin the Linux kernel is not as trivial as one would wish. This short paper 148c2ecf20Sopenharmony_citries to introduce all potential driver authors to Linux APIs for 158c2ecf20Sopenharmony_ciPCI device drivers. 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ciA more complete resource is the third edition of "Linux Device Drivers" 188c2ecf20Sopenharmony_ciby Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman. 198c2ecf20Sopenharmony_ciLDD3 is available for free (under Creative Commons License) from: 208c2ecf20Sopenharmony_cihttps://lwn.net/Kernel/LDD3/. 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ciHowever, keep in mind that all documents are subject to "bit rot". 238c2ecf20Sopenharmony_ciRefer to the source code if things are not working as described here. 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ciPlease send questions/comments/patches about Linux PCI API to the 268c2ecf20Sopenharmony_ci"Linux PCI" <linux-pci@atrey.karlin.mff.cuni.cz> mailing list. 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ciStructure of PCI drivers 308c2ecf20Sopenharmony_ci======================== 318c2ecf20Sopenharmony_ciPCI drivers "discover" PCI devices in a system via pci_register_driver(). 328c2ecf20Sopenharmony_ciActually, it's the other way around. When the PCI generic code discovers 338c2ecf20Sopenharmony_cia new device, the driver with a matching "description" will be notified. 348c2ecf20Sopenharmony_ciDetails on this below. 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cipci_register_driver() leaves most of the probing for devices to 378c2ecf20Sopenharmony_cithe PCI layer and supports online insertion/removal of devices [thus 388c2ecf20Sopenharmony_cisupporting hot-pluggable PCI, CardBus, and Express-Card in a single driver]. 398c2ecf20Sopenharmony_cipci_register_driver() call requires passing in a table of function 408c2ecf20Sopenharmony_cipointers and thus dictates the high level structure of a driver. 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ciOnce the driver knows about a PCI device and takes ownership, the 438c2ecf20Sopenharmony_cidriver generally needs to perform the following initialization: 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci - Enable the device 468c2ecf20Sopenharmony_ci - Request MMIO/IOP resources 478c2ecf20Sopenharmony_ci - Set the DMA mask size (for both coherent and streaming DMA) 488c2ecf20Sopenharmony_ci - Allocate and initialize shared control data (pci_allocate_coherent()) 498c2ecf20Sopenharmony_ci - Access device configuration space (if needed) 508c2ecf20Sopenharmony_ci - Register IRQ handler (request_irq()) 518c2ecf20Sopenharmony_ci - Initialize non-PCI (i.e. LAN/SCSI/etc parts of the chip) 528c2ecf20Sopenharmony_ci - Enable DMA/processing engines 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ciWhen done using the device, and perhaps the module needs to be unloaded, 558c2ecf20Sopenharmony_cithe driver needs to take the follow steps: 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci - Disable the device from generating IRQs 588c2ecf20Sopenharmony_ci - Release the IRQ (free_irq()) 598c2ecf20Sopenharmony_ci - Stop all DMA activity 608c2ecf20Sopenharmony_ci - Release DMA buffers (both streaming and coherent) 618c2ecf20Sopenharmony_ci - Unregister from other subsystems (e.g. scsi or netdev) 628c2ecf20Sopenharmony_ci - Release MMIO/IOP resources 638c2ecf20Sopenharmony_ci - Disable the device 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ciMost of these topics are covered in the following sections. 668c2ecf20Sopenharmony_ciFor the rest look at LDD3 or <linux/pci.h> . 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ciIf the PCI subsystem is not configured (CONFIG_PCI is not set), most of 698c2ecf20Sopenharmony_cithe PCI functions described below are defined as inline functions either 708c2ecf20Sopenharmony_cicompletely empty or just returning an appropriate error codes to avoid 718c2ecf20Sopenharmony_cilots of ifdefs in the drivers. 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cipci_register_driver() call 758c2ecf20Sopenharmony_ci========================== 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ciPCI device drivers call ``pci_register_driver()`` during their 788c2ecf20Sopenharmony_ciinitialization with a pointer to a structure describing the driver 798c2ecf20Sopenharmony_ci(``struct pci_driver``): 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci.. kernel-doc:: include/linux/pci.h 828c2ecf20Sopenharmony_ci :functions: pci_driver 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ciThe ID table is an array of ``struct pci_device_id`` entries ending with an 858c2ecf20Sopenharmony_ciall-zero entry. Definitions with static const are generally preferred. 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci.. kernel-doc:: include/linux/mod_devicetable.h 888c2ecf20Sopenharmony_ci :functions: pci_device_id 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ciMost drivers only need ``PCI_DEVICE()`` or ``PCI_DEVICE_CLASS()`` to set up 918c2ecf20Sopenharmony_cia pci_device_id table. 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ciNew PCI IDs may be added to a device driver pci_ids table at runtime 948c2ecf20Sopenharmony_cias shown below:: 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci echo "vendor device subvendor subdevice class class_mask driver_data" > \ 978c2ecf20Sopenharmony_ci /sys/bus/pci/drivers/{driver}/new_id 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ciAll fields are passed in as hexadecimal values (no leading 0x). 1008c2ecf20Sopenharmony_ciThe vendor and device fields are mandatory, the others are optional. Users 1018c2ecf20Sopenharmony_cineed pass only as many optional fields as necessary: 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci - subvendor and subdevice fields default to PCI_ANY_ID (FFFFFFFF) 1048c2ecf20Sopenharmony_ci - class and classmask fields default to 0 1058c2ecf20Sopenharmony_ci - driver_data defaults to 0UL. 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ciNote that driver_data must match the value used by any of the pci_device_id 1088c2ecf20Sopenharmony_cientries defined in the driver. This makes the driver_data field mandatory 1098c2ecf20Sopenharmony_ciif all the pci_device_id entries have a non-zero driver_data value. 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ciOnce added, the driver probe routine will be invoked for any unclaimed 1128c2ecf20Sopenharmony_ciPCI devices listed in its (newly updated) pci_ids list. 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ciWhen the driver exits, it just calls pci_unregister_driver() and the PCI layer 1158c2ecf20Sopenharmony_ciautomatically calls the remove hook for all devices handled by the driver. 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci"Attributes" for driver functions/data 1198c2ecf20Sopenharmony_ci-------------------------------------- 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ciPlease mark the initialization and cleanup functions where appropriate 1228c2ecf20Sopenharmony_ci(the corresponding macros are defined in <linux/init.h>): 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci ====== ================================================= 1258c2ecf20Sopenharmony_ci __init Initialization code. Thrown away after the driver 1268c2ecf20Sopenharmony_ci initializes. 1278c2ecf20Sopenharmony_ci __exit Exit code. Ignored for non-modular drivers. 1288c2ecf20Sopenharmony_ci ====== ================================================= 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ciTips on when/where to use the above attributes: 1318c2ecf20Sopenharmony_ci - The module_init()/module_exit() functions (and all 1328c2ecf20Sopenharmony_ci initialization functions called _only_ from these) 1338c2ecf20Sopenharmony_ci should be marked __init/__exit. 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci - Do not mark the struct pci_driver. 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci - Do NOT mark a function if you are not sure which mark to use. 1388c2ecf20Sopenharmony_ci Better to not mark the function than mark the function wrong. 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ciHow to find PCI devices manually 1428c2ecf20Sopenharmony_ci================================ 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ciPCI drivers should have a really good reason for not using the 1458c2ecf20Sopenharmony_cipci_register_driver() interface to search for PCI devices. 1468c2ecf20Sopenharmony_ciThe main reason PCI devices are controlled by multiple drivers 1478c2ecf20Sopenharmony_ciis because one PCI device implements several different HW services. 1488c2ecf20Sopenharmony_ciE.g. combined serial/parallel port/floppy controller. 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ciA manual search may be performed using the following constructs: 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ciSearching by vendor and device ID:: 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci struct pci_dev *dev = NULL; 1558c2ecf20Sopenharmony_ci while (dev = pci_get_device(VENDOR_ID, DEVICE_ID, dev)) 1568c2ecf20Sopenharmony_ci configure_device(dev); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ciSearching by class ID (iterate in a similar way):: 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci pci_get_class(CLASS_ID, dev) 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ciSearching by both vendor/device and subsystem vendor/device ID:: 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci pci_get_subsys(VENDOR_ID,DEVICE_ID, SUBSYS_VENDOR_ID, SUBSYS_DEVICE_ID, dev). 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ciYou can use the constant PCI_ANY_ID as a wildcard replacement for 1678c2ecf20Sopenharmony_ciVENDOR_ID or DEVICE_ID. This allows searching for any device from a 1688c2ecf20Sopenharmony_cispecific vendor, for example. 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ciThese functions are hotplug-safe. They increment the reference count on 1718c2ecf20Sopenharmony_cithe pci_dev that they return. You must eventually (possibly at module unload) 1728c2ecf20Sopenharmony_cidecrement the reference count on these devices by calling pci_dev_put(). 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ciDevice Initialization Steps 1768c2ecf20Sopenharmony_ci=========================== 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ciAs noted in the introduction, most PCI drivers need the following steps 1798c2ecf20Sopenharmony_cifor device initialization: 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci - Enable the device 1828c2ecf20Sopenharmony_ci - Request MMIO/IOP resources 1838c2ecf20Sopenharmony_ci - Set the DMA mask size (for both coherent and streaming DMA) 1848c2ecf20Sopenharmony_ci - Allocate and initialize shared control data (pci_allocate_coherent()) 1858c2ecf20Sopenharmony_ci - Access device configuration space (if needed) 1868c2ecf20Sopenharmony_ci - Register IRQ handler (request_irq()) 1878c2ecf20Sopenharmony_ci - Initialize non-PCI (i.e. LAN/SCSI/etc parts of the chip) 1888c2ecf20Sopenharmony_ci - Enable DMA/processing engines. 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ciThe driver can access PCI config space registers at any time. 1918c2ecf20Sopenharmony_ci(Well, almost. When running BIST, config space can go away...but 1928c2ecf20Sopenharmony_cithat will just result in a PCI Bus Master Abort and config reads 1938c2ecf20Sopenharmony_ciwill return garbage). 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ciEnable the PCI device 1978c2ecf20Sopenharmony_ci--------------------- 1988c2ecf20Sopenharmony_ciBefore touching any device registers, the driver needs to enable 1998c2ecf20Sopenharmony_cithe PCI device by calling pci_enable_device(). This will: 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci - wake up the device if it was in suspended state, 2028c2ecf20Sopenharmony_ci - allocate I/O and memory regions of the device (if BIOS did not), 2038c2ecf20Sopenharmony_ci - allocate an IRQ (if BIOS did not). 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci.. note:: 2068c2ecf20Sopenharmony_ci pci_enable_device() can fail! Check the return value. 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci.. warning:: 2098c2ecf20Sopenharmony_ci OS BUG: we don't check resource allocations before enabling those 2108c2ecf20Sopenharmony_ci resources. The sequence would make more sense if we called 2118c2ecf20Sopenharmony_ci pci_request_resources() before calling pci_enable_device(). 2128c2ecf20Sopenharmony_ci Currently, the device drivers can't detect the bug when two 2138c2ecf20Sopenharmony_ci devices have been allocated the same range. This is not a common 2148c2ecf20Sopenharmony_ci problem and unlikely to get fixed soon. 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci This has been discussed before but not changed as of 2.6.19: 2178c2ecf20Sopenharmony_ci https://lore.kernel.org/r/20060302180025.GC28895@flint.arm.linux.org.uk/ 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cipci_set_master() will enable DMA by setting the bus master bit 2218c2ecf20Sopenharmony_ciin the PCI_COMMAND register. It also fixes the latency timer value if 2228c2ecf20Sopenharmony_ciit's set to something bogus by the BIOS. pci_clear_master() will 2238c2ecf20Sopenharmony_cidisable DMA by clearing the bus master bit. 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ciIf the PCI device can use the PCI Memory-Write-Invalidate transaction, 2268c2ecf20Sopenharmony_cicall pci_set_mwi(). This enables the PCI_COMMAND bit for Mem-Wr-Inval 2278c2ecf20Sopenharmony_ciand also ensures that the cache line size register is set correctly. 2288c2ecf20Sopenharmony_ciCheck the return value of pci_set_mwi() as not all architectures 2298c2ecf20Sopenharmony_cior chip-sets may support Memory-Write-Invalidate. Alternatively, 2308c2ecf20Sopenharmony_ciif Mem-Wr-Inval would be nice to have but is not required, call 2318c2ecf20Sopenharmony_cipci_try_set_mwi() to have the system do its best effort at enabling 2328c2ecf20Sopenharmony_ciMem-Wr-Inval. 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ciRequest MMIO/IOP resources 2368c2ecf20Sopenharmony_ci-------------------------- 2378c2ecf20Sopenharmony_ciMemory (MMIO), and I/O port addresses should NOT be read directly 2388c2ecf20Sopenharmony_cifrom the PCI device config space. Use the values in the pci_dev structure 2398c2ecf20Sopenharmony_cias the PCI "bus address" might have been remapped to a "host physical" 2408c2ecf20Sopenharmony_ciaddress by the arch/chip-set specific kernel support. 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ciSee Documentation/driver-api/io-mapping.rst for how to access device registers 2438c2ecf20Sopenharmony_cior device memory. 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ciThe device driver needs to call pci_request_region() to verify 2468c2ecf20Sopenharmony_cino other device is already using the same address resource. 2478c2ecf20Sopenharmony_ciConversely, drivers should call pci_release_region() AFTER 2488c2ecf20Sopenharmony_cicalling pci_disable_device(). 2498c2ecf20Sopenharmony_ciThe idea is to prevent two devices colliding on the same address range. 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci.. tip:: 2528c2ecf20Sopenharmony_ci See OS BUG comment above. Currently (2.6.19), The driver can only 2538c2ecf20Sopenharmony_ci determine MMIO and IO Port resource availability _after_ calling 2548c2ecf20Sopenharmony_ci pci_enable_device(). 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ciGeneric flavors of pci_request_region() are request_mem_region() 2578c2ecf20Sopenharmony_ci(for MMIO ranges) and request_region() (for IO Port ranges). 2588c2ecf20Sopenharmony_ciUse these for address resources that are not described by "normal" PCI 2598c2ecf20Sopenharmony_ciBARs. 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ciAlso see pci_request_selected_regions() below. 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ciSet the DMA mask size 2658c2ecf20Sopenharmony_ci--------------------- 2668c2ecf20Sopenharmony_ci.. note:: 2678c2ecf20Sopenharmony_ci If anything below doesn't make sense, please refer to 2688c2ecf20Sopenharmony_ci :doc:`/core-api/dma-api`. This section is just a reminder that 2698c2ecf20Sopenharmony_ci drivers need to indicate DMA capabilities of the device and is not 2708c2ecf20Sopenharmony_ci an authoritative source for DMA interfaces. 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ciWhile all drivers should explicitly indicate the DMA capability 2738c2ecf20Sopenharmony_ci(e.g. 32 or 64 bit) of the PCI bus master, devices with more than 2748c2ecf20Sopenharmony_ci32-bit bus master capability for streaming data need the driver 2758c2ecf20Sopenharmony_cito "register" this capability by calling pci_set_dma_mask() with 2768c2ecf20Sopenharmony_ciappropriate parameters. In general this allows more efficient DMA 2778c2ecf20Sopenharmony_cion systems where System RAM exists above 4G _physical_ address. 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ciDrivers for all PCI-X and PCIe compliant devices must call 2808c2ecf20Sopenharmony_cipci_set_dma_mask() as they are 64-bit DMA devices. 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ciSimilarly, drivers must also "register" this capability if the device 2838c2ecf20Sopenharmony_cican directly address "consistent memory" in System RAM above 4G physical 2848c2ecf20Sopenharmony_ciaddress by calling pci_set_consistent_dma_mask(). 2858c2ecf20Sopenharmony_ciAgain, this includes drivers for all PCI-X and PCIe compliant devices. 2868c2ecf20Sopenharmony_ciMany 64-bit "PCI" devices (before PCI-X) and some PCI-X devices are 2878c2ecf20Sopenharmony_ci64-bit DMA capable for payload ("streaming") data but not control 2888c2ecf20Sopenharmony_ci("consistent") data. 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ciSetup shared control data 2928c2ecf20Sopenharmony_ci------------------------- 2938c2ecf20Sopenharmony_ciOnce the DMA masks are set, the driver can allocate "consistent" (a.k.a. shared) 2948c2ecf20Sopenharmony_cimemory. See :doc:`/core-api/dma-api` for a full description of 2958c2ecf20Sopenharmony_cithe DMA APIs. This section is just a reminder that it needs to be done 2968c2ecf20Sopenharmony_cibefore enabling DMA on the device. 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ciInitialize device registers 3008c2ecf20Sopenharmony_ci--------------------------- 3018c2ecf20Sopenharmony_ciSome drivers will need specific "capability" fields programmed 3028c2ecf20Sopenharmony_cior other "vendor specific" register initialized or reset. 3038c2ecf20Sopenharmony_ciE.g. clearing pending interrupts. 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ciRegister IRQ handler 3078c2ecf20Sopenharmony_ci-------------------- 3088c2ecf20Sopenharmony_ciWhile calling request_irq() is the last step described here, 3098c2ecf20Sopenharmony_cithis is often just another intermediate step to initialize a device. 3108c2ecf20Sopenharmony_ciThis step can often be deferred until the device is opened for use. 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ciAll interrupt handlers for IRQ lines should be registered with IRQF_SHARED 3138c2ecf20Sopenharmony_ciand use the devid to map IRQs to devices (remember that all PCI IRQ lines 3148c2ecf20Sopenharmony_cican be shared). 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_cirequest_irq() will associate an interrupt handler and device handle 3178c2ecf20Sopenharmony_ciwith an interrupt number. Historically interrupt numbers represent 3188c2ecf20Sopenharmony_ciIRQ lines which run from the PCI device to the Interrupt controller. 3198c2ecf20Sopenharmony_ciWith MSI and MSI-X (more below) the interrupt number is a CPU "vector". 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_cirequest_irq() also enables the interrupt. Make sure the device is 3228c2ecf20Sopenharmony_ciquiesced and does not have any interrupts pending before registering 3238c2ecf20Sopenharmony_cithe interrupt handler. 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ciMSI and MSI-X are PCI capabilities. Both are "Message Signaled Interrupts" 3268c2ecf20Sopenharmony_ciwhich deliver interrupts to the CPU via a DMA write to a Local APIC. 3278c2ecf20Sopenharmony_ciThe fundamental difference between MSI and MSI-X is how multiple 3288c2ecf20Sopenharmony_ci"vectors" get allocated. MSI requires contiguous blocks of vectors 3298c2ecf20Sopenharmony_ciwhile MSI-X can allocate several individual ones. 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ciMSI capability can be enabled by calling pci_alloc_irq_vectors() with the 3328c2ecf20Sopenharmony_ciPCI_IRQ_MSI and/or PCI_IRQ_MSIX flags before calling request_irq(). This 3338c2ecf20Sopenharmony_cicauses the PCI support to program CPU vector data into the PCI device 3348c2ecf20Sopenharmony_cicapability registers. Many architectures, chip-sets, or BIOSes do NOT 3358c2ecf20Sopenharmony_cisupport MSI or MSI-X and a call to pci_alloc_irq_vectors with just 3368c2ecf20Sopenharmony_cithe PCI_IRQ_MSI and PCI_IRQ_MSIX flags will fail, so try to always 3378c2ecf20Sopenharmony_cispecify PCI_IRQ_LEGACY as well. 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ciDrivers that have different interrupt handlers for MSI/MSI-X and 3408c2ecf20Sopenharmony_cilegacy INTx should chose the right one based on the msi_enabled 3418c2ecf20Sopenharmony_ciand msix_enabled flags in the pci_dev structure after calling 3428c2ecf20Sopenharmony_cipci_alloc_irq_vectors. 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ciThere are (at least) two really good reasons for using MSI: 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci1) MSI is an exclusive interrupt vector by definition. 3478c2ecf20Sopenharmony_ci This means the interrupt handler doesn't have to verify 3488c2ecf20Sopenharmony_ci its device caused the interrupt. 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci2) MSI avoids DMA/IRQ race conditions. DMA to host memory is guaranteed 3518c2ecf20Sopenharmony_ci to be visible to the host CPU(s) when the MSI is delivered. This 3528c2ecf20Sopenharmony_ci is important for both data coherency and avoiding stale control data. 3538c2ecf20Sopenharmony_ci This guarantee allows the driver to omit MMIO reads to flush 3548c2ecf20Sopenharmony_ci the DMA stream. 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ciSee drivers/infiniband/hw/mthca/ or drivers/net/tg3.c for examples 3578c2ecf20Sopenharmony_ciof MSI/MSI-X usage. 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ciPCI device shutdown 3618c2ecf20Sopenharmony_ci=================== 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ciWhen a PCI device driver is being unloaded, most of the following 3648c2ecf20Sopenharmony_cisteps need to be performed: 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci - Disable the device from generating IRQs 3678c2ecf20Sopenharmony_ci - Release the IRQ (free_irq()) 3688c2ecf20Sopenharmony_ci - Stop all DMA activity 3698c2ecf20Sopenharmony_ci - Release DMA buffers (both streaming and consistent) 3708c2ecf20Sopenharmony_ci - Unregister from other subsystems (e.g. scsi or netdev) 3718c2ecf20Sopenharmony_ci - Disable device from responding to MMIO/IO Port addresses 3728c2ecf20Sopenharmony_ci - Release MMIO/IO Port resource(s) 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ciStop IRQs on the device 3768c2ecf20Sopenharmony_ci----------------------- 3778c2ecf20Sopenharmony_ciHow to do this is chip/device specific. If it's not done, it opens 3788c2ecf20Sopenharmony_cithe possibility of a "screaming interrupt" if (and only if) 3798c2ecf20Sopenharmony_cithe IRQ is shared with another device. 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ciWhen the shared IRQ handler is "unhooked", the remaining devices 3828c2ecf20Sopenharmony_ciusing the same IRQ line will still need the IRQ enabled. Thus if the 3838c2ecf20Sopenharmony_ci"unhooked" device asserts IRQ line, the system will respond assuming 3848c2ecf20Sopenharmony_ciit was one of the remaining devices asserted the IRQ line. Since none 3858c2ecf20Sopenharmony_ciof the other devices will handle the IRQ, the system will "hang" until 3868c2ecf20Sopenharmony_ciit decides the IRQ isn't going to get handled and masks the IRQ (100,000 3878c2ecf20Sopenharmony_ciiterations later). Once the shared IRQ is masked, the remaining devices 3888c2ecf20Sopenharmony_ciwill stop functioning properly. Not a nice situation. 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ciThis is another reason to use MSI or MSI-X if it's available. 3918c2ecf20Sopenharmony_ciMSI and MSI-X are defined to be exclusive interrupts and thus 3928c2ecf20Sopenharmony_ciare not susceptible to the "screaming interrupt" problem. 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ciRelease the IRQ 3968c2ecf20Sopenharmony_ci--------------- 3978c2ecf20Sopenharmony_ciOnce the device is quiesced (no more IRQs), one can call free_irq(). 3988c2ecf20Sopenharmony_ciThis function will return control once any pending IRQs are handled, 3998c2ecf20Sopenharmony_ci"unhook" the drivers IRQ handler from that IRQ, and finally release 4008c2ecf20Sopenharmony_cithe IRQ if no one else is using it. 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ciStop all DMA activity 4048c2ecf20Sopenharmony_ci--------------------- 4058c2ecf20Sopenharmony_ciIt's extremely important to stop all DMA operations BEFORE attempting 4068c2ecf20Sopenharmony_cito deallocate DMA control data. Failure to do so can result in memory 4078c2ecf20Sopenharmony_cicorruption, hangs, and on some chip-sets a hard crash. 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ciStopping DMA after stopping the IRQs can avoid races where the 4108c2ecf20Sopenharmony_ciIRQ handler might restart DMA engines. 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ciWhile this step sounds obvious and trivial, several "mature" drivers 4138c2ecf20Sopenharmony_cididn't get this step right in the past. 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ciRelease DMA buffers 4178c2ecf20Sopenharmony_ci------------------- 4188c2ecf20Sopenharmony_ciOnce DMA is stopped, clean up streaming DMA first. 4198c2ecf20Sopenharmony_ciI.e. unmap data buffers and return buffers to "upstream" 4208c2ecf20Sopenharmony_ciowners if there is one. 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ciThen clean up "consistent" buffers which contain the control data. 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ciSee :doc:`/core-api/dma-api` for details on unmapping interfaces. 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ciUnregister from other subsystems 4288c2ecf20Sopenharmony_ci-------------------------------- 4298c2ecf20Sopenharmony_ciMost low level PCI device drivers support some other subsystem 4308c2ecf20Sopenharmony_cilike USB, ALSA, SCSI, NetDev, Infiniband, etc. Make sure your 4318c2ecf20Sopenharmony_cidriver isn't losing resources from that other subsystem. 4328c2ecf20Sopenharmony_ciIf this happens, typically the symptom is an Oops (panic) when 4338c2ecf20Sopenharmony_cithe subsystem attempts to call into a driver that has been unloaded. 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ciDisable Device from responding to MMIO/IO Port addresses 4378c2ecf20Sopenharmony_ci-------------------------------------------------------- 4388c2ecf20Sopenharmony_ciio_unmap() MMIO or IO Port resources and then call pci_disable_device(). 4398c2ecf20Sopenharmony_ciThis is the symmetric opposite of pci_enable_device(). 4408c2ecf20Sopenharmony_ciDo not access device registers after calling pci_disable_device(). 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ciRelease MMIO/IO Port Resource(s) 4448c2ecf20Sopenharmony_ci-------------------------------- 4458c2ecf20Sopenharmony_ciCall pci_release_region() to mark the MMIO or IO Port range as available. 4468c2ecf20Sopenharmony_ciFailure to do so usually results in the inability to reload the driver. 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ciHow to access PCI config space 4508c2ecf20Sopenharmony_ci============================== 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ciYou can use `pci_(read|write)_config_(byte|word|dword)` to access the config 4538c2ecf20Sopenharmony_cispace of a device represented by `struct pci_dev *`. All these functions return 4548c2ecf20Sopenharmony_ci0 when successful or an error code (`PCIBIOS_...`) which can be translated to a 4558c2ecf20Sopenharmony_citext string by pcibios_strerror. Most drivers expect that accesses to valid PCI 4568c2ecf20Sopenharmony_cidevices don't fail. 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ciIf you don't have a struct pci_dev available, you can call 4598c2ecf20Sopenharmony_ci`pci_bus_(read|write)_config_(byte|word|dword)` to access a given device 4608c2ecf20Sopenharmony_ciand function on that bus. 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ciIf you access fields in the standard portion of the config header, please 4638c2ecf20Sopenharmony_ciuse symbolic names of locations and bits declared in <linux/pci.h>. 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ciIf you need to access Extended PCI Capability registers, just call 4668c2ecf20Sopenharmony_cipci_find_capability() for the particular capability and it will find the 4678c2ecf20Sopenharmony_cicorresponding register block for you. 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ciOther interesting functions 4718c2ecf20Sopenharmony_ci=========================== 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci============================= ================================================ 4748c2ecf20Sopenharmony_cipci_get_domain_bus_and_slot() Find pci_dev corresponding to given domain, 4758c2ecf20Sopenharmony_ci bus and slot and number. If the device is 4768c2ecf20Sopenharmony_ci found, its reference count is increased. 4778c2ecf20Sopenharmony_cipci_set_power_state() Set PCI Power Management state (0=D0 ... 3=D3) 4788c2ecf20Sopenharmony_cipci_find_capability() Find specified capability in device's capability 4798c2ecf20Sopenharmony_ci list. 4808c2ecf20Sopenharmony_cipci_resource_start() Returns bus start address for a given PCI region 4818c2ecf20Sopenharmony_cipci_resource_end() Returns bus end address for a given PCI region 4828c2ecf20Sopenharmony_cipci_resource_len() Returns the byte length of a PCI region 4838c2ecf20Sopenharmony_cipci_set_drvdata() Set private driver data pointer for a pci_dev 4848c2ecf20Sopenharmony_cipci_get_drvdata() Return private driver data pointer for a pci_dev 4858c2ecf20Sopenharmony_cipci_set_mwi() Enable Memory-Write-Invalidate transactions. 4868c2ecf20Sopenharmony_cipci_clear_mwi() Disable Memory-Write-Invalidate transactions. 4878c2ecf20Sopenharmony_ci============================= ================================================ 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ciMiscellaneous hints 4918c2ecf20Sopenharmony_ci=================== 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ciWhen displaying PCI device names to the user (for example when a driver wants 4948c2ecf20Sopenharmony_cito tell the user what card has it found), please use pci_name(pci_dev). 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ciAlways refer to the PCI devices by a pointer to the pci_dev structure. 4978c2ecf20Sopenharmony_ciAll PCI layer functions use this identification and it's the only 4988c2ecf20Sopenharmony_cireasonable one. Don't use bus/slot/function numbers except for very 4998c2ecf20Sopenharmony_cispecial purposes -- on systems with multiple primary buses their semantics 5008c2ecf20Sopenharmony_cican be pretty complex. 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ciDon't try to turn on Fast Back to Back writes in your driver. All devices 5038c2ecf20Sopenharmony_cion the bus need to be capable of doing it, so this is something which needs 5048c2ecf20Sopenharmony_cito be handled by platform and generic code, not individual drivers. 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ciVendor and device identifications 5088c2ecf20Sopenharmony_ci================================= 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ciDo not add new device or vendor IDs to include/linux/pci_ids.h unless they 5118c2ecf20Sopenharmony_ciare shared across multiple drivers. You can add private definitions in 5128c2ecf20Sopenharmony_ciyour driver if they're helpful, or just use plain hex constants. 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ciThe device IDs are arbitrary hex numbers (vendor controlled) and normally used 5158c2ecf20Sopenharmony_cionly in a single location, the pci_device_id table. 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ciPlease DO submit new vendor/device IDs to https://pci-ids.ucw.cz/. 5188c2ecf20Sopenharmony_ciThere's a mirror of the pci.ids file at https://github.com/pciutils/pciids. 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ciObsolete functions 5228c2ecf20Sopenharmony_ci================== 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ciThere are several functions which you might come across when trying to 5258c2ecf20Sopenharmony_ciport an old driver to the new PCI interface. They are no longer present 5268c2ecf20Sopenharmony_ciin the kernel as they aren't compatible with hotplug or PCI domains or 5278c2ecf20Sopenharmony_cihaving sane locking. 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci================= =========================================== 5308c2ecf20Sopenharmony_cipci_find_device() Superseded by pci_get_device() 5318c2ecf20Sopenharmony_cipci_find_subsys() Superseded by pci_get_subsys() 5328c2ecf20Sopenharmony_cipci_find_slot() Superseded by pci_get_domain_bus_and_slot() 5338c2ecf20Sopenharmony_cipci_get_slot() Superseded by pci_get_domain_bus_and_slot() 5348c2ecf20Sopenharmony_ci================= =========================================== 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ciThe alternative is the traditional PCI device driver that walks PCI 5378c2ecf20Sopenharmony_cidevice lists. This is still possible but discouraged. 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ciMMIO Space and "Write Posting" 5418c2ecf20Sopenharmony_ci============================== 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ciConverting a driver from using I/O Port space to using MMIO space 5448c2ecf20Sopenharmony_cioften requires some additional changes. Specifically, "write posting" 5458c2ecf20Sopenharmony_cineeds to be handled. Many drivers (e.g. tg3, acenic, sym53c8xx_2) 5468c2ecf20Sopenharmony_cialready do this. I/O Port space guarantees write transactions reach the PCI 5478c2ecf20Sopenharmony_cidevice before the CPU can continue. Writes to MMIO space allow the CPU 5488c2ecf20Sopenharmony_cito continue before the transaction reaches the PCI device. HW weenies 5498c2ecf20Sopenharmony_cicall this "Write Posting" because the write completion is "posted" to 5508c2ecf20Sopenharmony_cithe CPU before the transaction has reached its destination. 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ciThus, timing sensitive code should add readl() where the CPU is 5538c2ecf20Sopenharmony_ciexpected to wait before doing other work. The classic "bit banging" 5548c2ecf20Sopenharmony_cisequence works fine for I/O Port space:: 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci for (i = 8; --i; val >>= 1) { 5578c2ecf20Sopenharmony_ci outb(val & 1, ioport_reg); /* write bit */ 5588c2ecf20Sopenharmony_ci udelay(10); 5598c2ecf20Sopenharmony_ci } 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ciThe same sequence for MMIO space should be:: 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci for (i = 8; --i; val >>= 1) { 5648c2ecf20Sopenharmony_ci writeb(val & 1, mmio_reg); /* write bit */ 5658c2ecf20Sopenharmony_ci readb(safe_mmio_reg); /* flush posted write */ 5668c2ecf20Sopenharmony_ci udelay(10); 5678c2ecf20Sopenharmony_ci } 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ciIt is important that "safe_mmio_reg" not have any side effects that 5708c2ecf20Sopenharmony_ciinterferes with the correct operation of the device. 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ciAnother case to watch out for is when resetting a PCI device. Use PCI 5738c2ecf20Sopenharmony_ciConfiguration space reads to flush the writel(). This will gracefully 5748c2ecf20Sopenharmony_cihandle the PCI master abort on all platforms if the PCI device is 5758c2ecf20Sopenharmony_ciexpected to not respond to a readl(). Most x86 platforms will allow 5768c2ecf20Sopenharmony_ciMMIO reads to master abort (a.k.a. "Soft Fail") and return garbage 5778c2ecf20Sopenharmony_ci(e.g. ~0). But many RISC platforms will crash (a.k.a."Hard Fail"). 578