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_capability_read_word(dev
, PCI_EXP_FLAGS
, ®16
);
60 *pme
= (reg16
& PCI_EXP_FLAGS_IRQ
) >> 9;
65 if (mask
& PCIE_PORT_SERVICE_AER
) {
70 pci_read_config_dword(dev
, pos
+ PCI_ERR_ROOT_STATUS
,
72 *aer
= (reg32
& PCI_ERR_ROOT_AER_IRQ
) >> 27;
73 nvec
= max(nvec
, *aer
+ 1);
78 if (mask
& PCIE_PORT_SERVICE_DPC
) {
79 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_DPC
);
81 pci_read_config_word(dev
, pos
+ PCI_EXP_DPC_CAP
,
83 *dpc
= reg16
& PCI_EXP_DPC_IRQ
;
84 nvec
= max(nvec
, *dpc
+ 1);
92 * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
94 * @dev: PCI Express port to handle
95 * @irqs: Array of interrupt vectors to populate
96 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
98 * Return value: 0 on success, error code on failure
100 static int pcie_port_enable_irq_vec(struct pci_dev
*dev
, int *irqs
, int mask
)
102 int nr_entries
, nvec
, pcie_irq
;
103 u32 pme
= 0, aer
= 0, dpc
= 0;
105 /* Allocate the maximum possible number of MSI/MSI-X vectors */
106 nr_entries
= pci_alloc_irq_vectors(dev
, 1, PCIE_PORT_MAX_MSI_ENTRIES
,
107 PCI_IRQ_MSIX
| PCI_IRQ_MSI
);
111 /* See how many and which Interrupt Message Numbers we actually use */
112 nvec
= pcie_message_numbers(dev
, mask
, &pme
, &aer
, &dpc
);
113 if (nvec
> nr_entries
) {
114 pci_free_irq_vectors(dev
);
119 * If we allocated more than we need, free them and reallocate fewer.
121 * Reallocating may change the specific vectors we get, so
122 * pci_irq_vector() must be done *after* the reallocation.
124 * If we're using MSI, hardware is *allowed* to change the Interrupt
125 * Message Numbers when we free and reallocate the vectors, but we
126 * assume it won't because we allocate enough vectors for the
127 * biggest Message Number we found.
129 if (nvec
!= nr_entries
) {
130 pci_free_irq_vectors(dev
);
132 nr_entries
= pci_alloc_irq_vectors(dev
, nvec
, nvec
,
133 PCI_IRQ_MSIX
| PCI_IRQ_MSI
);
138 /* PME, hotplug and bandwidth notification share an MSI/MSI-X vector */
139 if (mask
& (PCIE_PORT_SERVICE_PME
| PCIE_PORT_SERVICE_HP
|
140 PCIE_PORT_SERVICE_BWNOTIF
)) {
141 pcie_irq
= pci_irq_vector(dev
, pme
);
142 irqs
[PCIE_PORT_SERVICE_PME_SHIFT
] = pcie_irq
;
143 irqs
[PCIE_PORT_SERVICE_HP_SHIFT
] = pcie_irq
;
144 irqs
[PCIE_PORT_SERVICE_BWNOTIF_SHIFT
] = pcie_irq
;
147 if (mask
& PCIE_PORT_SERVICE_AER
)
148 irqs
[PCIE_PORT_SERVICE_AER_SHIFT
] = pci_irq_vector(dev
, aer
);
150 if (mask
& PCIE_PORT_SERVICE_DPC
)
151 irqs
[PCIE_PORT_SERVICE_DPC_SHIFT
] = pci_irq_vector(dev
, dpc
);
157 * pcie_init_service_irqs - initialize irqs for PCI Express port services
158 * @dev: PCI Express port to handle
159 * @irqs: Array of irqs to populate
160 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
162 * Return value: Interrupt mode associated with the port
164 static int pcie_init_service_irqs(struct pci_dev
*dev
, int *irqs
, int mask
)
168 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++)
172 * If we support PME but can't use MSI/MSI-X for it, we have to
173 * fall back to INTx or other interrupts, e.g., a system shared
176 if ((mask
& PCIE_PORT_SERVICE_PME
) && pcie_pme_no_msi())
179 /* Try to use MSI-X or MSI if supported */
180 if (pcie_port_enable_irq_vec(dev
, irqs
, mask
) == 0)
184 /* fall back to legacy IRQ */
185 ret
= pci_alloc_irq_vectors(dev
, 1, 1, PCI_IRQ_LEGACY
);
189 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++)
190 irqs
[i
] = pci_irq_vector(dev
, 0);
196 * get_port_device_capability - discover capabilities of a PCI Express port
197 * @dev: PCI Express port to examine
199 * The capabilities are read from the port's PCI Express configuration registers
200 * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
203 * Return value: Bitmask of discovered port capabilities
205 static int get_port_device_capability(struct pci_dev
*dev
)
207 struct pci_host_bridge
*host
= pci_find_host_bridge(dev
->bus
);
210 if (dev
->is_hotplug_bridge
&&
211 (pcie_ports_native
|| host
->native_pcie_hotplug
)) {
212 services
|= PCIE_PORT_SERVICE_HP
;
215 * Disable hot-plug interrupts in case they have been enabled
216 * by the BIOS and the hot-plug service driver is not loaded.
218 pcie_capability_clear_word(dev
, PCI_EXP_SLTCTL
,
219 PCI_EXP_SLTCTL_CCIE
| PCI_EXP_SLTCTL_HPIE
);
222 #ifdef CONFIG_PCIEAER
223 if (dev
->aer_cap
&& pci_aer_available() &&
224 (pcie_ports_native
|| host
->native_aer
)) {
225 services
|= PCIE_PORT_SERVICE_AER
;
228 * Disable AER on this port in case it's been enabled by the
229 * BIOS (the AER service driver will enable it when necessary).
231 pci_disable_pcie_error_reporting(dev
);
236 * Root ports are capable of generating PME too. Root Complex
237 * Event Collectors can also generate PMEs, but we don't handle
240 if (pci_pcie_type(dev
) == PCI_EXP_TYPE_ROOT_PORT
&&
241 (pcie_ports_native
|| host
->native_pme
)) {
242 services
|= PCIE_PORT_SERVICE_PME
;
245 * Disable PME interrupt on this port in case it's been enabled
246 * by the BIOS (the PME service driver will enable it when
249 pcie_pme_interrupt_enable(dev
, false);
252 if (pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_DPC
) &&
253 pci_aer_available() && services
& PCIE_PORT_SERVICE_AER
)
254 services
|= PCIE_PORT_SERVICE_DPC
;
256 if (pci_pcie_type(dev
) == PCI_EXP_TYPE_DOWNSTREAM
||
257 pci_pcie_type(dev
) == PCI_EXP_TYPE_ROOT_PORT
)
258 services
|= PCIE_PORT_SERVICE_BWNOTIF
;
264 * pcie_device_init - allocate and initialize PCI Express port service device
265 * @pdev: PCI Express port to associate the service device with
266 * @service: Type of service to associate with the service device
267 * @irq: Interrupt vector to associate with the service device
269 static int pcie_device_init(struct pci_dev
*pdev
, int service
, int irq
)
272 struct pcie_device
*pcie
;
273 struct device
*device
;
275 pcie
= kzalloc(sizeof(*pcie
), GFP_KERNEL
);
280 pcie
->service
= service
;
282 /* Initialize generic device interface */
283 device
= &pcie
->device
;
284 device
->bus
= &pcie_port_bus_type
;
285 device
->release
= release_pcie_device
; /* callback to free pcie dev */
286 dev_set_name(device
, "%s:pcie%03x",
288 get_descriptor_id(pci_pcie_type(pdev
), service
));
289 device
->parent
= &pdev
->dev
;
290 device_enable_async_suspend(device
);
292 retval
= device_register(device
);
298 pm_runtime_no_callbacks(device
);
304 * pcie_port_device_register - register PCI Express port
305 * @dev: PCI Express port to register
307 * Allocate the port extension structure and register services associated with
310 int pcie_port_device_register(struct pci_dev
*dev
)
312 int status
, capabilities
, i
, nr_service
;
313 int irqs
[PCIE_PORT_DEVICE_MAXSERVICES
];
315 /* Enable PCI Express port device */
316 status
= pci_enable_device(dev
);
320 /* Get and check PCI Express port services */
321 capabilities
= get_port_device_capability(dev
);
327 * Initialize service irqs. Don't use service devices that
328 * require interrupts if there is no way to generate them.
329 * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
330 * that can be used in the absence of irqs. Allow them to determine
331 * if that is to be used.
333 status
= pcie_init_service_irqs(dev
, irqs
, capabilities
);
335 capabilities
&= PCIE_PORT_SERVICE_HP
;
340 /* Allocate child services if any */
343 for (i
= 0; i
< PCIE_PORT_DEVICE_MAXSERVICES
; i
++) {
344 int service
= 1 << i
;
345 if (!(capabilities
& service
))
347 if (!pcie_device_init(dev
, service
, irqs
[i
]))
351 goto error_cleanup_irqs
;
356 pci_free_irq_vectors(dev
);
358 pci_disable_device(dev
);
363 typedef int (*pcie_pm_callback_t
)(struct pcie_device
*);
365 static int pm_iter(struct device
*dev
, void *data
)
367 struct pcie_port_service_driver
*service_driver
;
368 size_t offset
= *(size_t *)data
;
369 pcie_pm_callback_t cb
;
371 if ((dev
->bus
== &pcie_port_bus_type
) && dev
->driver
) {
372 service_driver
= to_service_driver(dev
->driver
);
373 cb
= *(pcie_pm_callback_t
*)((void *)service_driver
+ offset
);
375 return cb(to_pcie_device(dev
));
381 * pcie_port_device_suspend - suspend port services associated with a PCIe port
382 * @dev: PCI Express port to handle
384 int pcie_port_device_suspend(struct device
*dev
)
386 size_t off
= offsetof(struct pcie_port_service_driver
, suspend
);
387 return device_for_each_child(dev
, &off
, pm_iter
);
390 int pcie_port_device_resume_noirq(struct device
*dev
)
392 size_t off
= offsetof(struct pcie_port_service_driver
, resume_noirq
);
393 return device_for_each_child(dev
, &off
, pm_iter
);
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 size_t off
= offsetof(struct pcie_port_service_driver
, resume
);
403 return device_for_each_child(dev
, &off
, pm_iter
);
407 * pcie_port_device_runtime_suspend - runtime suspend port services
408 * @dev: PCI Express port to handle
410 int pcie_port_device_runtime_suspend(struct device
*dev
)
412 size_t off
= offsetof(struct pcie_port_service_driver
, runtime_suspend
);
413 return device_for_each_child(dev
, &off
, pm_iter
);
417 * pcie_port_device_runtime_resume - runtime resume port services
418 * @dev: PCI Express port to handle
420 int pcie_port_device_runtime_resume(struct device
*dev
)
422 size_t off
= offsetof(struct pcie_port_service_driver
, runtime_resume
);
423 return device_for_each_child(dev
, &off
, pm_iter
);
427 static int remove_iter(struct device
*dev
, void *data
)
429 if (dev
->bus
== &pcie_port_bus_type
)
430 device_unregister(dev
);
434 static int find_service_iter(struct device
*device
, void *data
)
436 struct pcie_port_service_driver
*service_driver
;
437 struct portdrv_service_data
*pdrvs
;
440 pdrvs
= (struct portdrv_service_data
*) data
;
441 service
= pdrvs
->service
;
443 if (device
->bus
== &pcie_port_bus_type
&& device
->driver
) {
444 service_driver
= to_service_driver(device
->driver
);
445 if (service_driver
->service
== service
) {
446 pdrvs
->drv
= service_driver
;
456 * pcie_port_find_service - find the service driver
457 * @dev: PCI Express port the service is associated with
458 * @service: Service to find
460 * Find PCI Express port service driver associated with given service
462 struct pcie_port_service_driver
*pcie_port_find_service(struct pci_dev
*dev
,
465 struct pcie_port_service_driver
*drv
;
466 struct portdrv_service_data pdrvs
;
469 pdrvs
.service
= service
;
470 device_for_each_child(&dev
->dev
, &pdrvs
, find_service_iter
);
477 * pcie_port_find_device - find the struct device
478 * @dev: PCI Express port the service is associated with
479 * @service: For the service to find
481 * Find the struct device associated with given service on a pci_dev
483 struct device
*pcie_port_find_device(struct pci_dev
*dev
,
486 struct device
*device
;
487 struct portdrv_service_data pdrvs
;
490 pdrvs
.service
= service
;
491 device_for_each_child(&dev
->dev
, &pdrvs
, find_service_iter
);
496 EXPORT_SYMBOL_GPL(pcie_port_find_device
);
499 * pcie_port_device_remove - unregister PCI Express port service devices
500 * @dev: PCI Express port the service devices to unregister are associated with
502 * Remove PCI Express port service devices associated with given port and
503 * disable MSI-X or MSI for the port.
505 void pcie_port_device_remove(struct pci_dev
*dev
)
507 device_for_each_child(&dev
->dev
, NULL
, remove_iter
);
508 pci_free_irq_vectors(dev
);
509 pci_disable_device(dev
);
513 * pcie_port_probe_service - probe driver for given PCI Express port service
514 * @dev: PCI Express port service device to probe against
516 * If PCI Express port service driver is registered with
517 * pcie_port_service_register(), this function will be called by the driver core
518 * whenever match is found between the driver and a port service device.
520 static int pcie_port_probe_service(struct device
*dev
)
522 struct pcie_device
*pciedev
;
523 struct pcie_port_service_driver
*driver
;
526 if (!dev
|| !dev
->driver
)
529 driver
= to_service_driver(dev
->driver
);
530 if (!driver
|| !driver
->probe
)
533 pciedev
= to_pcie_device(dev
);
534 status
= driver
->probe(pciedev
);
543 * pcie_port_remove_service - detach driver from given PCI Express port service
544 * @dev: PCI Express port service device to handle
546 * If PCI Express port service driver is registered with
547 * pcie_port_service_register(), this function will be called by the driver core
548 * when device_unregister() is called for the port service device associated
551 static int pcie_port_remove_service(struct device
*dev
)
553 struct pcie_device
*pciedev
;
554 struct pcie_port_service_driver
*driver
;
556 if (!dev
|| !dev
->driver
)
559 pciedev
= to_pcie_device(dev
);
560 driver
= to_service_driver(dev
->driver
);
561 if (driver
&& driver
->remove
) {
562 driver
->remove(pciedev
);
569 * pcie_port_shutdown_service - shut down given PCI Express port service
570 * @dev: PCI Express port service device to handle
572 * If PCI Express port service driver is registered with
573 * pcie_port_service_register(), this function will be called by the driver core
574 * when device_shutdown() is called for the port service device associated
577 static void pcie_port_shutdown_service(struct device
*dev
) {}
580 * pcie_port_service_register - register PCI Express port service driver
581 * @new: PCI Express port service driver to register
583 int pcie_port_service_register(struct pcie_port_service_driver
*new)
585 if (pcie_ports_disabled
)
588 new->driver
.name
= new->name
;
589 new->driver
.bus
= &pcie_port_bus_type
;
590 new->driver
.probe
= pcie_port_probe_service
;
591 new->driver
.remove
= pcie_port_remove_service
;
592 new->driver
.shutdown
= pcie_port_shutdown_service
;
594 return driver_register(&new->driver
);
596 EXPORT_SYMBOL(pcie_port_service_register
);
599 * pcie_port_service_unregister - unregister PCI Express port service driver
600 * @drv: PCI Express port service driver to unregister
602 void pcie_port_service_unregister(struct pcie_port_service_driver
*drv
)
604 driver_unregister(&drv
->driver
);
606 EXPORT_SYMBOL(pcie_port_service_unregister
);