1 // SPDX-License-Identifier: GPL-2.0
3 * Purpose: PCI Express Port Bus Driver's Core Functions
5 * Copyright (C) 2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/string.h>
16 #include <linux/slab.h>
17 #include <linux/aer.h>
23 * release_pcie_device - free PCI Express port service device structure
24 * @dev: Port service device to release
26 * Invoked automatically when device is being removed in response to
27 * device_unregister(dev). Release all resources being claimed.
29 static void release_pcie_device(struct device *dev)
31 kfree(to_pcie_device(dev));
35 * Fill in *pme, *aer, *dpc with the relevant Interrupt Message Numbers if
36 * services are enabled in "mask". Return the number of MSI/MSI-X vectors
37 * required to accommodate the largest Message Number.
39 static int pcie_message_numbers(struct pci_dev *dev, int mask,
40 u32 *pme, u32 *aer, u32 *dpc)
46 * The Interrupt Message Number indicates which vector is used, i.e.,
47 * the MSI-X table entry or the MSI offset between the base Message
48 * Data and the generated interrupt message. See PCIe r3.1, sec
49 * 7.8.2, 7.10.10, 7.31.2.
52 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
53 pcie_capability_read_word(dev, PCI_EXP_FLAGS, ®16);
54 *pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
59 if (mask & PCIE_PORT_SERVICE_AER) {
64 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
66 *aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
67 nvec = max(nvec, *aer + 1);
72 if (mask & PCIE_PORT_SERVICE_DPC) {
73 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
75 pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
77 *dpc = reg16 & PCI_EXP_DPC_IRQ;
78 nvec = max(nvec, *dpc + 1);
86 * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
88 * @dev: PCI Express port to handle
89 * @irqs: Array of interrupt vectors to populate
90 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
92 * Return value: 0 on success, error code on failure
94 static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
97 u32 pme = 0, aer = 0, dpc = 0;
99 /* Allocate the maximum possible number of MSI/MSI-X vectors */
100 nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSI_ENTRIES,
101 PCI_IRQ_MSIX | PCI_IRQ_MSI);
105 /* See how many and which Interrupt Message Numbers we actually use */
106 nvec = pcie_message_numbers(dev, mask, &pme, &aer, &dpc);
107 if (nvec > nr_entries) {
108 pci_free_irq_vectors(dev);
113 * If we allocated more than we need, free them and reallocate fewer.
115 * Reallocating may change the specific vectors we get, so
116 * pci_irq_vector() must be done *after* the reallocation.
118 * If we're using MSI, hardware is *allowed* to change the Interrupt
119 * Message Numbers when we free and reallocate the vectors, but we
120 * assume it won't because we allocate enough vectors for the
121 * biggest Message Number we found.
123 if (nvec != nr_entries) {
124 pci_free_irq_vectors(dev);
126 nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
127 PCI_IRQ_MSIX | PCI_IRQ_MSI);
132 /* PME and hotplug share an MSI/MSI-X vector */
133 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
134 irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, pme);
135 irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, pme);
138 if (mask & PCIE_PORT_SERVICE_AER)
139 irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, aer);
141 if (mask & PCIE_PORT_SERVICE_DPC)
142 irqs[PCIE_PORT_SERVICE_DPC_SHIFT] = pci_irq_vector(dev, dpc);
148 * pcie_init_service_irqs - initialize irqs for PCI Express port services
149 * @dev: PCI Express port to handle
150 * @irqs: Array of irqs to populate
151 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
153 * Return value: Interrupt mode associated with the port
155 static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
159 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
163 * If we support PME but can't use MSI/MSI-X for it, we have to
164 * fall back to INTx or other interrupts, e.g., a system shared
167 if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
170 /* Try to use MSI-X or MSI if supported */
171 if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
175 /* fall back to legacy IRQ */
176 ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
180 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
181 irqs[i] = pci_irq_vector(dev, 0);
187 * get_port_device_capability - discover capabilities of a PCI Express port
188 * @dev: PCI Express port to examine
190 * The capabilities are read from the port's PCI Express configuration registers
191 * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
194 * Return value: Bitmask of discovered port capabilities
196 static int get_port_device_capability(struct pci_dev *dev)
198 struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
201 if (dev->is_hotplug_bridge &&
202 (pcie_ports_native || host->native_hotplug)) {
203 services |= PCIE_PORT_SERVICE_HP;
206 * Disable hot-plug interrupts in case they have been enabled
207 * by the BIOS and the hot-plug service driver is not loaded.
209 pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
210 PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
213 #ifdef CONFIG_PCIEAER
214 if (dev->aer_cap && pci_aer_available() &&
215 (pcie_ports_native || host->native_aer)) {
216 services |= PCIE_PORT_SERVICE_AER;
219 * Disable AER on this port in case it's been enabled by the
220 * BIOS (the AER service driver will enable it when necessary).
222 pci_disable_pcie_error_reporting(dev);
227 * Root ports are capable of generating PME too. Root Complex
228 * Event Collectors can also generate PMEs, but we don't handle
231 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT &&
232 (pcie_ports_native || host->native_pme)) {
233 services |= PCIE_PORT_SERVICE_PME;
236 * Disable PME interrupt on this port in case it's been enabled
237 * by the BIOS (the PME service driver will enable it when
240 pcie_pme_interrupt_enable(dev, false);
243 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC) &&
244 pci_aer_available() && services & PCIE_PORT_SERVICE_AER)
245 services |= PCIE_PORT_SERVICE_DPC;
251 * pcie_device_init - allocate and initialize PCI Express port service device
252 * @pdev: PCI Express port to associate the service device with
253 * @service: Type of service to associate with the service device
254 * @irq: Interrupt vector to associate with the service device
256 static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
259 struct pcie_device *pcie;
260 struct device *device;
262 pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
267 pcie->service = service;
269 /* Initialize generic device interface */
270 device = &pcie->device;
271 device->bus = &pcie_port_bus_type;
272 device->release = release_pcie_device; /* callback to free pcie dev */
273 dev_set_name(device, "%s:pcie%03x",
275 get_descriptor_id(pci_pcie_type(pdev), service));
276 device->parent = &pdev->dev;
277 device_enable_async_suspend(device);
279 retval = device_register(device);
285 pm_runtime_no_callbacks(device);
291 * pcie_port_device_register - register PCI Express port
292 * @dev: PCI Express port to register
294 * Allocate the port extension structure and register services associated with
297 int pcie_port_device_register(struct pci_dev *dev)
299 int status, capabilities, i, nr_service;
300 int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
302 /* Enable PCI Express port device */
303 status = pci_enable_device(dev);
307 /* Get and check PCI Express port services */
308 capabilities = get_port_device_capability(dev);
314 * Initialize service irqs. Don't use service devices that
315 * require interrupts if there is no way to generate them.
316 * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
317 * that can be used in the absence of irqs. Allow them to determine
318 * if that is to be used.
320 status = pcie_init_service_irqs(dev, irqs, capabilities);
322 capabilities &= PCIE_PORT_SERVICE_HP;
327 /* Allocate child services if any */
330 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
331 int service = 1 << i;
332 if (!(capabilities & service))
334 if (!pcie_device_init(dev, service, irqs[i]))
338 goto error_cleanup_irqs;
343 pci_free_irq_vectors(dev);
345 pci_disable_device(dev);
350 static int suspend_iter(struct device *dev, void *data)
352 struct pcie_port_service_driver *service_driver;
354 if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
355 service_driver = to_service_driver(dev->driver);
356 if (service_driver->suspend)
357 service_driver->suspend(to_pcie_device(dev));
363 * pcie_port_device_suspend - suspend port services associated with a PCIe port
364 * @dev: PCI Express port to handle
366 int pcie_port_device_suspend(struct device *dev)
368 return device_for_each_child(dev, NULL, suspend_iter);
371 static int resume_iter(struct device *dev, void *data)
373 struct pcie_port_service_driver *service_driver;
375 if ((dev->bus == &pcie_port_bus_type) &&
377 service_driver = to_service_driver(dev->driver);
378 if (service_driver->resume)
379 service_driver->resume(to_pcie_device(dev));
385 * pcie_port_device_resume - resume port services associated with a PCIe port
386 * @dev: PCI Express port to handle
388 int pcie_port_device_resume(struct device *dev)
390 return device_for_each_child(dev, NULL, resume_iter);
394 static int remove_iter(struct device *dev, void *data)
396 if (dev->bus == &pcie_port_bus_type)
397 device_unregister(dev);
402 * pcie_port_device_remove - unregister PCI Express port service devices
403 * @dev: PCI Express port the service devices to unregister are associated with
405 * Remove PCI Express port service devices associated with given port and
406 * disable MSI-X or MSI for the port.
408 void pcie_port_device_remove(struct pci_dev *dev)
410 device_for_each_child(&dev->dev, NULL, remove_iter);
411 pci_free_irq_vectors(dev);
412 pci_disable_device(dev);
416 * pcie_port_probe_service - probe driver for given PCI Express port service
417 * @dev: PCI Express port service device to probe against
419 * If PCI Express port service driver is registered with
420 * pcie_port_service_register(), this function will be called by the driver core
421 * whenever match is found between the driver and a port service device.
423 static int pcie_port_probe_service(struct device *dev)
425 struct pcie_device *pciedev;
426 struct pcie_port_service_driver *driver;
429 if (!dev || !dev->driver)
432 driver = to_service_driver(dev->driver);
433 if (!driver || !driver->probe)
436 pciedev = to_pcie_device(dev);
437 status = driver->probe(pciedev);
446 * pcie_port_remove_service - detach driver from given PCI Express port service
447 * @dev: PCI Express port service device to handle
449 * If PCI Express port service driver is registered with
450 * pcie_port_service_register(), this function will be called by the driver core
451 * when device_unregister() is called for the port service device associated
454 static int pcie_port_remove_service(struct device *dev)
456 struct pcie_device *pciedev;
457 struct pcie_port_service_driver *driver;
459 if (!dev || !dev->driver)
462 pciedev = to_pcie_device(dev);
463 driver = to_service_driver(dev->driver);
464 if (driver && driver->remove) {
465 driver->remove(pciedev);
472 * pcie_port_shutdown_service - shut down given PCI Express port service
473 * @dev: PCI Express port service device to handle
475 * If PCI Express port service driver is registered with
476 * pcie_port_service_register(), this function will be called by the driver core
477 * when device_shutdown() is called for the port service device associated
480 static void pcie_port_shutdown_service(struct device *dev) {}
483 * pcie_port_service_register - register PCI Express port service driver
484 * @new: PCI Express port service driver to register
486 int pcie_port_service_register(struct pcie_port_service_driver *new)
488 if (pcie_ports_disabled)
491 new->driver.name = new->name;
492 new->driver.bus = &pcie_port_bus_type;
493 new->driver.probe = pcie_port_probe_service;
494 new->driver.remove = pcie_port_remove_service;
495 new->driver.shutdown = pcie_port_shutdown_service;
497 return driver_register(&new->driver);
499 EXPORT_SYMBOL(pcie_port_service_register);
502 * pcie_port_service_unregister - unregister PCI Express port service driver
503 * @drv: PCI Express port service driver to unregister
505 void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
507 driver_unregister(&drv->driver);
509 EXPORT_SYMBOL(pcie_port_service_unregister);