1 // SPDX-License-Identifier: GPL-2.0
4 * Purpose: PCI Express Port Bus Driver's Core Functions
6 * Copyright (C) 2004 Intel
7 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/aer.h>
23 bool pciehp_msi_disabled;
25 static int __init pciehp_setup(char *str)
27 if (!strncmp(str, "nomsi", 5))
28 pciehp_msi_disabled = true;
32 __setup("pcie_hp=", pciehp_setup);
35 * release_pcie_device - free PCI Express port service device structure
36 * @dev: Port service device to release
38 * Invoked automatically when device is being removed in response to
39 * device_unregister(dev). Release all resources being claimed.
41 static void release_pcie_device(struct device *dev)
43 kfree(to_pcie_device(dev));
47 * Fill in *pme, *aer, *dpc with the relevant Interrupt Message Numbers if
48 * services are enabled in "mask". Return the number of MSI/MSI-X vectors
49 * required to accommodate the largest Message Number.
51 static int pcie_message_numbers(struct pci_dev *dev, int mask,
52 u32 *pme, u32 *aer, u32 *dpc)
54 u32 nvec = 0, pos, reg32;
58 * The Interrupt Message Number indicates which vector is used, i.e.,
59 * the MSI-X table entry or the MSI offset between the base Message
60 * Data and the generated interrupt message. See PCIe r3.1, sec
61 * 7.8.2, 7.10.10, 7.31.2.
64 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
65 pcie_capability_read_word(dev, PCI_EXP_FLAGS, ®16);
66 *pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
70 if (mask & PCIE_PORT_SERVICE_AER) {
71 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
73 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
75 *aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
76 nvec = max(nvec, *aer + 1);
80 if (mask & PCIE_PORT_SERVICE_DPC) {
81 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
83 pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
85 *dpc = reg16 & PCI_EXP_DPC_IRQ;
86 nvec = max(nvec, *dpc + 1);
94 * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
96 * @dev: PCI Express port to handle
97 * @irqs: Array of interrupt vectors to populate
98 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
100 * Return value: 0 on success, error code on failure
102 static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
104 int nr_entries, nvec;
105 u32 pme = 0, aer = 0, dpc = 0;
107 /* Allocate the maximum possible number of MSI/MSI-X vectors */
108 nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSI_ENTRIES,
109 PCI_IRQ_MSIX | PCI_IRQ_MSI);
113 /* See how many and which Interrupt Message Numbers we actually use */
114 nvec = pcie_message_numbers(dev, mask, &pme, &aer, &dpc);
115 if (nvec > nr_entries) {
116 pci_free_irq_vectors(dev);
121 * If we allocated more than we need, free them and reallocate fewer.
123 * Reallocating may change the specific vectors we get, so
124 * pci_irq_vector() must be done *after* the reallocation.
126 * If we're using MSI, hardware is *allowed* to change the Interrupt
127 * Message Numbers when we free and reallocate the vectors, but we
128 * assume it won't because we allocate enough vectors for the
129 * biggest Message Number we found.
131 if (nvec != nr_entries) {
132 pci_free_irq_vectors(dev);
134 nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
135 PCI_IRQ_MSIX | PCI_IRQ_MSI);
140 /* PME and hotplug share an MSI/MSI-X vector */
141 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
142 irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, pme);
143 irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, pme);
146 if (mask & PCIE_PORT_SERVICE_AER)
147 irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, aer);
149 if (mask & PCIE_PORT_SERVICE_DPC)
150 irqs[PCIE_PORT_SERVICE_DPC_SHIFT] = pci_irq_vector(dev, dpc);
156 * pcie_init_service_irqs - initialize irqs for PCI Express port services
157 * @dev: PCI Express port to handle
158 * @irqs: Array of irqs to populate
159 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
161 * Return value: Interrupt mode associated with the port
163 static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
167 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
171 * If we support PME or hotplug, but we can't use MSI/MSI-X for
172 * them, we have to fall back to INTx or other interrupts, e.g., a
173 * system shared interrupt.
175 if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
178 if ((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())
181 /* Try to use MSI-X or MSI if supported */
182 if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
186 /* fall back to legacy IRQ */
187 ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
191 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
192 if (i != PCIE_PORT_SERVICE_VC_SHIFT)
193 irqs[i] = pci_irq_vector(dev, 0);
200 * get_port_device_capability - discover capabilities of a PCI Express port
201 * @dev: PCI Express port to examine
203 * The capabilities are read from the port's PCI Express configuration registers
204 * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
207 * Return value: Bitmask of discovered port capabilities
209 static int get_port_device_capability(struct pci_dev *dev)
214 cap_mask = PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP
215 | PCIE_PORT_SERVICE_VC;
216 if (pci_aer_available())
217 cap_mask |= PCIE_PORT_SERVICE_AER | PCIE_PORT_SERVICE_DPC;
220 pcie_port_platform_notify(dev, &cap_mask);
222 /* Hot-Plug Capable */
223 if ((cap_mask & PCIE_PORT_SERVICE_HP) && dev->is_hotplug_bridge) {
224 services |= PCIE_PORT_SERVICE_HP;
226 * Disable hot-plug interrupts in case they have been enabled
227 * by the BIOS and the hot-plug service driver is not loaded.
229 pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
230 PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
233 if ((cap_mask & PCIE_PORT_SERVICE_AER)
234 && pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)) {
235 services |= PCIE_PORT_SERVICE_AER;
237 * Disable AER on this port in case it's been enabled by the
238 * BIOS (the AER service driver will enable it when necessary).
240 pci_disable_pcie_error_reporting(dev);
243 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VC))
244 services |= PCIE_PORT_SERVICE_VC;
245 /* Root ports are capable of generating PME too */
246 if ((cap_mask & PCIE_PORT_SERVICE_PME)
247 && pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
248 services |= PCIE_PORT_SERVICE_PME;
250 * Disable PME interrupt on this port in case it's been enabled
251 * by the BIOS (the PME service driver will enable it when
254 pcie_pme_interrupt_enable(dev, false);
256 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC))
257 services |= PCIE_PORT_SERVICE_DPC;
263 * pcie_device_init - allocate and initialize PCI Express port service device
264 * @pdev: PCI Express port to associate the service device with
265 * @service: Type of service to associate with the service device
266 * @irq: Interrupt vector to associate with the service device
268 static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
271 struct pcie_device *pcie;
272 struct device *device;
274 pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
279 pcie->service = service;
281 /* Initialize generic device interface */
282 device = &pcie->device;
283 device->bus = &pcie_port_bus_type;
284 device->release = release_pcie_device; /* callback to free pcie dev */
285 dev_set_name(device, "%s:pcie%03x",
287 get_descriptor_id(pci_pcie_type(pdev), service));
288 device->parent = &pdev->dev;
289 device_enable_async_suspend(device);
291 retval = device_register(device);
297 pm_runtime_no_callbacks(device);
303 * pcie_port_device_register - register PCI Express port
304 * @dev: PCI Express port to register
306 * Allocate the port extension structure and register services associated with
309 int pcie_port_device_register(struct pci_dev *dev)
311 int status, capabilities, i, nr_service;
312 int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
314 /* Enable PCI Express port device */
315 status = pci_enable_device(dev);
319 /* Get and check PCI Express port services */
320 capabilities = get_port_device_capability(dev);
326 * Initialize service irqs. Don't use service devices that
327 * require interrupts if there is no way to generate them.
328 * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
329 * that can be used in the absence of irqs. Allow them to determine
330 * if that is to be used.
332 status = pcie_init_service_irqs(dev, irqs, capabilities);
334 capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP;
339 /* Allocate child services if any */
342 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
343 int service = 1 << i;
344 if (!(capabilities & service))
346 if (!pcie_device_init(dev, service, irqs[i]))
350 goto error_cleanup_irqs;
355 pci_free_irq_vectors(dev);
357 pci_disable_device(dev);
362 static int suspend_iter(struct device *dev, void *data)
364 struct pcie_port_service_driver *service_driver;
366 if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
367 service_driver = to_service_driver(dev->driver);
368 if (service_driver->suspend)
369 service_driver->suspend(to_pcie_device(dev));
375 * pcie_port_device_suspend - suspend port services associated with a PCIe port
376 * @dev: PCI Express port to handle
378 int pcie_port_device_suspend(struct device *dev)
380 return device_for_each_child(dev, NULL, suspend_iter);
383 static int resume_iter(struct device *dev, void *data)
385 struct pcie_port_service_driver *service_driver;
387 if ((dev->bus == &pcie_port_bus_type) &&
389 service_driver = to_service_driver(dev->driver);
390 if (service_driver->resume)
391 service_driver->resume(to_pcie_device(dev));
397 * pcie_port_device_resume - resume port services associated with a PCIe port
398 * @dev: PCI Express port to handle
400 int pcie_port_device_resume(struct device *dev)
402 return device_for_each_child(dev, NULL, resume_iter);
406 static int remove_iter(struct device *dev, void *data)
408 if (dev->bus == &pcie_port_bus_type)
409 device_unregister(dev);
414 * pcie_port_device_remove - unregister PCI Express port service devices
415 * @dev: PCI Express port the service devices to unregister are associated with
417 * Remove PCI Express port service devices associated with given port and
418 * disable MSI-X or MSI for the port.
420 void pcie_port_device_remove(struct pci_dev *dev)
422 device_for_each_child(&dev->dev, NULL, remove_iter);
423 pci_free_irq_vectors(dev);
424 pci_disable_device(dev);
428 * pcie_port_probe_service - probe driver for given PCI Express port service
429 * @dev: PCI Express port service device to probe against
431 * If PCI Express port service driver is registered with
432 * pcie_port_service_register(), this function will be called by the driver core
433 * whenever match is found between the driver and a port service device.
435 static int pcie_port_probe_service(struct device *dev)
437 struct pcie_device *pciedev;
438 struct pcie_port_service_driver *driver;
441 if (!dev || !dev->driver)
444 driver = to_service_driver(dev->driver);
445 if (!driver || !driver->probe)
448 pciedev = to_pcie_device(dev);
449 status = driver->probe(pciedev);
458 * pcie_port_remove_service - detach driver from given PCI Express port service
459 * @dev: PCI Express port service device to handle
461 * If PCI Express port service driver is registered with
462 * pcie_port_service_register(), this function will be called by the driver core
463 * when device_unregister() is called for the port service device associated
466 static int pcie_port_remove_service(struct device *dev)
468 struct pcie_device *pciedev;
469 struct pcie_port_service_driver *driver;
471 if (!dev || !dev->driver)
474 pciedev = to_pcie_device(dev);
475 driver = to_service_driver(dev->driver);
476 if (driver && driver->remove) {
477 driver->remove(pciedev);
484 * pcie_port_shutdown_service - shut down given PCI Express port service
485 * @dev: PCI Express port service device to handle
487 * If PCI Express port service driver is registered with
488 * pcie_port_service_register(), this function will be called by the driver core
489 * when device_shutdown() is called for the port service device associated
492 static void pcie_port_shutdown_service(struct device *dev) {}
495 * pcie_port_service_register - register PCI Express port service driver
496 * @new: PCI Express port service driver to register
498 int pcie_port_service_register(struct pcie_port_service_driver *new)
500 if (pcie_ports_disabled)
503 new->driver.name = new->name;
504 new->driver.bus = &pcie_port_bus_type;
505 new->driver.probe = pcie_port_probe_service;
506 new->driver.remove = pcie_port_remove_service;
507 new->driver.shutdown = pcie_port_shutdown_service;
509 return driver_register(&new->driver);
511 EXPORT_SYMBOL(pcie_port_service_register);
514 * pcie_port_service_unregister - unregister PCI Express port service driver
515 * @drv: PCI Express port service driver to unregister
517 void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
519 driver_unregister(&drv->driver);
521 EXPORT_SYMBOL(pcie_port_service_unregister);