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>
22 struct portdrv_service_data {
23 struct pcie_port_service_driver *drv;
29 * release_pcie_device - free PCI Express port service device structure
30 * @dev: Port service device to release
32 * Invoked automatically when device is being removed in response to
33 * device_unregister(dev). Release all resources being claimed.
35 static void release_pcie_device(struct device *dev)
37 kfree(to_pcie_device(dev));
41 * Fill in *pme, *aer, *dpc with the relevant Interrupt Message Numbers if
42 * services are enabled in "mask". Return the number of MSI/MSI-X vectors
43 * required to accommodate the largest Message Number.
45 static int pcie_message_numbers(struct pci_dev *dev, int mask,
46 u32 *pme, u32 *aer, u32 *dpc)
52 * The Interrupt Message Number indicates which vector is used, i.e.,
53 * the MSI-X table entry or the MSI offset between the base Message
54 * Data and the generated interrupt message. See PCIe r3.1, sec
55 * 7.8.2, 7.10.10, 7.31.2.
58 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
59 PCIE_PORT_SERVICE_BWNOTIF)) {
60 pcie_capability_read_word(dev, PCI_EXP_FLAGS, ®16);
61 *pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
66 if (mask & PCIE_PORT_SERVICE_AER) {
71 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
73 *aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
74 nvec = max(nvec, *aer + 1);
79 if (mask & PCIE_PORT_SERVICE_DPC) {
80 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
82 pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
84 *dpc = reg16 & PCI_EXP_DPC_IRQ;
85 nvec = max(nvec, *dpc + 1);
93 * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
95 * @dev: PCI Express port to handle
96 * @irqs: Array of interrupt vectors to populate
97 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
99 * Return value: 0 on success, error code on failure
101 static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
103 int nr_entries, nvec, pcie_irq;
104 u32 pme = 0, aer = 0, dpc = 0;
106 /* Allocate the maximum possible number of MSI/MSI-X vectors */
107 nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSI_ENTRIES,
108 PCI_IRQ_MSIX | PCI_IRQ_MSI);
112 /* See how many and which Interrupt Message Numbers we actually use */
113 nvec = pcie_message_numbers(dev, mask, &pme, &aer, &dpc);
114 if (nvec > nr_entries) {
115 pci_free_irq_vectors(dev);
120 * If we allocated more than we need, free them and reallocate fewer.
122 * Reallocating may change the specific vectors we get, so
123 * pci_irq_vector() must be done *after* the reallocation.
125 * If we're using MSI, hardware is *allowed* to change the Interrupt
126 * Message Numbers when we free and reallocate the vectors, but we
127 * assume it won't because we allocate enough vectors for the
128 * biggest Message Number we found.
130 if (nvec != nr_entries) {
131 pci_free_irq_vectors(dev);
133 nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
134 PCI_IRQ_MSIX | PCI_IRQ_MSI);
139 /* PME, hotplug and bandwidth notification share an MSI/MSI-X vector */
140 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
141 PCIE_PORT_SERVICE_BWNOTIF)) {
142 pcie_irq = pci_irq_vector(dev, pme);
143 irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pcie_irq;
144 irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pcie_irq;
145 irqs[PCIE_PORT_SERVICE_BWNOTIF_SHIFT] = pcie_irq;
148 if (mask & PCIE_PORT_SERVICE_AER)
149 irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, aer);
151 if (mask & PCIE_PORT_SERVICE_DPC)
152 irqs[PCIE_PORT_SERVICE_DPC_SHIFT] = pci_irq_vector(dev, dpc);
158 * pcie_init_service_irqs - initialize irqs for PCI Express port services
159 * @dev: PCI Express port to handle
160 * @irqs: Array of irqs to populate
161 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
163 * Return value: Interrupt mode associated with the port
165 static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
169 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
173 * If we support PME but can't use MSI/MSI-X for it, we have to
174 * fall back to INTx or other interrupts, e.g., a system shared
177 if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
180 /* Try to use MSI-X or MSI if supported */
181 if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
185 /* fall back to legacy IRQ */
186 ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
190 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
191 irqs[i] = pci_irq_vector(dev, 0);
197 * get_port_device_capability - discover capabilities of a PCI Express port
198 * @dev: PCI Express port to examine
200 * The capabilities are read from the port's PCI Express configuration registers
201 * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
204 * Return value: Bitmask of discovered port capabilities
206 static int get_port_device_capability(struct pci_dev *dev)
208 struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
211 if (dev->is_hotplug_bridge &&
212 (pcie_ports_native || host->native_pcie_hotplug)) {
213 services |= PCIE_PORT_SERVICE_HP;
216 * Disable hot-plug interrupts in case they have been enabled
217 * by the BIOS and the hot-plug service driver is not loaded.
219 pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
220 PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
223 #ifdef CONFIG_PCIEAER
224 if (dev->aer_cap && pci_aer_available() &&
225 (pcie_ports_native || host->native_aer)) {
226 services |= PCIE_PORT_SERVICE_AER;
229 * Disable AER on this port in case it's been enabled by the
230 * BIOS (the AER service driver will enable it when necessary).
232 pci_disable_pcie_error_reporting(dev);
237 * Root ports are capable of generating PME too. Root Complex
238 * Event Collectors can also generate PMEs, but we don't handle
241 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT &&
242 (pcie_ports_native || host->native_pme)) {
243 services |= PCIE_PORT_SERVICE_PME;
246 * Disable PME interrupt on this port in case it's been enabled
247 * by the BIOS (the PME service driver will enable it when
250 pcie_pme_interrupt_enable(dev, false);
254 * With dpc-native, allow Linux to use DPC even if it doesn't have
255 * permission to use AER.
257 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC) &&
258 pci_aer_available() &&
259 (pcie_ports_dpc_native || (services & PCIE_PORT_SERVICE_AER)))
260 services |= PCIE_PORT_SERVICE_DPC;
262 if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
263 pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
266 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &linkcap);
267 if (linkcap & PCI_EXP_LNKCAP_LBNC)
268 services |= PCIE_PORT_SERVICE_BWNOTIF;
275 * pcie_device_init - allocate and initialize PCI Express port service device
276 * @pdev: PCI Express port to associate the service device with
277 * @service: Type of service to associate with the service device
278 * @irq: Interrupt vector to associate with the service device
280 static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
283 struct pcie_device *pcie;
284 struct device *device;
286 pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
291 pcie->service = service;
293 /* Initialize generic device interface */
294 device = &pcie->device;
295 device->bus = &pcie_port_bus_type;
296 device->release = release_pcie_device; /* callback to free pcie dev */
297 dev_set_name(device, "%s:pcie%03x",
299 get_descriptor_id(pci_pcie_type(pdev), service));
300 device->parent = &pdev->dev;
301 device_enable_async_suspend(device);
303 retval = device_register(device);
309 pm_runtime_no_callbacks(device);
315 * pcie_port_device_register - register PCI Express port
316 * @dev: PCI Express port to register
318 * Allocate the port extension structure and register services associated with
321 int pcie_port_device_register(struct pci_dev *dev)
323 int status, capabilities, i, nr_service;
324 int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
326 /* Enable PCI Express port device */
327 status = pci_enable_device(dev);
331 /* Get and check PCI Express port services */
332 capabilities = get_port_device_capability(dev);
338 * Initialize service irqs. Don't use service devices that
339 * require interrupts if there is no way to generate them.
340 * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
341 * that can be used in the absence of irqs. Allow them to determine
342 * if that is to be used.
344 status = pcie_init_service_irqs(dev, irqs, capabilities);
346 capabilities &= PCIE_PORT_SERVICE_HP;
351 /* Allocate child services if any */
354 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
355 int service = 1 << i;
356 if (!(capabilities & service))
358 if (!pcie_device_init(dev, service, irqs[i]))
362 goto error_cleanup_irqs;
367 pci_free_irq_vectors(dev);
369 pci_disable_device(dev);
374 typedef int (*pcie_pm_callback_t)(struct pcie_device *);
376 static int pm_iter(struct device *dev, void *data)
378 struct pcie_port_service_driver *service_driver;
379 size_t offset = *(size_t *)data;
380 pcie_pm_callback_t cb;
382 if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
383 service_driver = to_service_driver(dev->driver);
384 cb = *(pcie_pm_callback_t *)((void *)service_driver + offset);
386 return cb(to_pcie_device(dev));
392 * pcie_port_device_suspend - suspend port services associated with a PCIe port
393 * @dev: PCI Express port to handle
395 int pcie_port_device_suspend(struct device *dev)
397 size_t off = offsetof(struct pcie_port_service_driver, suspend);
398 return device_for_each_child(dev, &off, pm_iter);
401 int pcie_port_device_resume_noirq(struct device *dev)
403 size_t off = offsetof(struct pcie_port_service_driver, resume_noirq);
404 return device_for_each_child(dev, &off, pm_iter);
408 * pcie_port_device_resume - resume port services associated with a PCIe port
409 * @dev: PCI Express port to handle
411 int pcie_port_device_resume(struct device *dev)
413 size_t off = offsetof(struct pcie_port_service_driver, resume);
414 return device_for_each_child(dev, &off, pm_iter);
418 * pcie_port_device_runtime_suspend - runtime suspend port services
419 * @dev: PCI Express port to handle
421 int pcie_port_device_runtime_suspend(struct device *dev)
423 size_t off = offsetof(struct pcie_port_service_driver, runtime_suspend);
424 return device_for_each_child(dev, &off, pm_iter);
428 * pcie_port_device_runtime_resume - runtime resume port services
429 * @dev: PCI Express port to handle
431 int pcie_port_device_runtime_resume(struct device *dev)
433 size_t off = offsetof(struct pcie_port_service_driver, runtime_resume);
434 return device_for_each_child(dev, &off, pm_iter);
438 static int remove_iter(struct device *dev, void *data)
440 if (dev->bus == &pcie_port_bus_type)
441 device_unregister(dev);
445 static int find_service_iter(struct device *device, void *data)
447 struct pcie_port_service_driver *service_driver;
448 struct portdrv_service_data *pdrvs;
451 pdrvs = (struct portdrv_service_data *) data;
452 service = pdrvs->service;
454 if (device->bus == &pcie_port_bus_type && device->driver) {
455 service_driver = to_service_driver(device->driver);
456 if (service_driver->service == service) {
457 pdrvs->drv = service_driver;
467 * pcie_port_find_device - find the struct device
468 * @dev: PCI Express port the service is associated with
469 * @service: For the service to find
471 * Find the struct device associated with given service on a pci_dev
473 struct device *pcie_port_find_device(struct pci_dev *dev,
476 struct device *device;
477 struct portdrv_service_data pdrvs;
480 pdrvs.service = service;
481 device_for_each_child(&dev->dev, &pdrvs, find_service_iter);
486 EXPORT_SYMBOL_GPL(pcie_port_find_device);
489 * pcie_port_device_remove - unregister PCI Express port service devices
490 * @dev: PCI Express port the service devices to unregister are associated with
492 * Remove PCI Express port service devices associated with given port and
493 * disable MSI-X or MSI for the port.
495 void pcie_port_device_remove(struct pci_dev *dev)
497 device_for_each_child(&dev->dev, NULL, remove_iter);
498 pci_free_irq_vectors(dev);
499 pci_disable_device(dev);
503 * pcie_port_probe_service - probe driver for given PCI Express port service
504 * @dev: PCI Express port service device to probe against
506 * If PCI Express port service driver is registered with
507 * pcie_port_service_register(), this function will be called by the driver core
508 * whenever match is found between the driver and a port service device.
510 static int pcie_port_probe_service(struct device *dev)
512 struct pcie_device *pciedev;
513 struct pcie_port_service_driver *driver;
516 if (!dev || !dev->driver)
519 driver = to_service_driver(dev->driver);
520 if (!driver || !driver->probe)
523 pciedev = to_pcie_device(dev);
524 status = driver->probe(pciedev);
533 * pcie_port_remove_service - detach driver from given PCI Express port service
534 * @dev: PCI Express port service device to handle
536 * If PCI Express port service driver is registered with
537 * pcie_port_service_register(), this function will be called by the driver core
538 * when device_unregister() is called for the port service device associated
541 static int pcie_port_remove_service(struct device *dev)
543 struct pcie_device *pciedev;
544 struct pcie_port_service_driver *driver;
546 if (!dev || !dev->driver)
549 pciedev = to_pcie_device(dev);
550 driver = to_service_driver(dev->driver);
551 if (driver && driver->remove) {
552 driver->remove(pciedev);
559 * pcie_port_shutdown_service - shut down given PCI Express port service
560 * @dev: PCI Express port service device to handle
562 * If PCI Express port service driver is registered with
563 * pcie_port_service_register(), this function will be called by the driver core
564 * when device_shutdown() is called for the port service device associated
567 static void pcie_port_shutdown_service(struct device *dev) {}
570 * pcie_port_service_register - register PCI Express port service driver
571 * @new: PCI Express port service driver to register
573 int pcie_port_service_register(struct pcie_port_service_driver *new)
575 if (pcie_ports_disabled)
578 new->driver.name = new->name;
579 new->driver.bus = &pcie_port_bus_type;
580 new->driver.probe = pcie_port_probe_service;
581 new->driver.remove = pcie_port_remove_service;
582 new->driver.shutdown = pcie_port_shutdown_service;
584 return driver_register(&new->driver);
586 EXPORT_SYMBOL(pcie_port_service_register);
589 * pcie_port_service_unregister - unregister PCI Express port service driver
590 * @drv: PCI Express port service driver to unregister
592 void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
594 driver_unregister(&drv->driver);
596 EXPORT_SYMBOL(pcie_port_service_unregister);