x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / pci / pcie / portdrv_core.c
blobef3bad4ad010129b32752cb2e8719c778b58fd63
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * File: portdrv_core.c
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)
8 */
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/pm.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/pcieport_if.h>
19 #include <linux/aer.h>
21 #include "../pci.h"
22 #include "portdrv.h"
24 bool pciehp_msi_disabled;
26 static int __init pciehp_setup(char *str)
28 if (!strncmp(str, "nomsi", 5))
29 pciehp_msi_disabled = true;
31 return 1;
33 __setup("pcie_hp=", pciehp_setup);
35 /**
36 * release_pcie_device - free PCI Express port service device structure
37 * @dev: Port service device to release
39 * Invoked automatically when device is being removed in response to
40 * device_unregister(dev). Release all resources being claimed.
42 static void release_pcie_device(struct device *dev)
44 kfree(to_pcie_device(dev));
48 * Fill in *pme, *aer, *dpc with the relevant Interrupt Message Numbers if
49 * services are enabled in "mask". Return the number of MSI/MSI-X vectors
50 * required to accommodate the largest Message Number.
52 static int pcie_message_numbers(struct pci_dev *dev, int mask,
53 u32 *pme, u32 *aer, u32 *dpc)
55 u32 nvec = 0, pos, reg32;
56 u16 reg16;
59 * The Interrupt Message Number indicates which vector is used, i.e.,
60 * the MSI-X table entry or the MSI offset between the base Message
61 * Data and the generated interrupt message. See PCIe r3.1, sec
62 * 7.8.2, 7.10.10, 7.31.2.
65 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
66 pcie_capability_read_word(dev, PCI_EXP_FLAGS, &reg16);
67 *pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
68 nvec = *pme + 1;
71 if (mask & PCIE_PORT_SERVICE_AER) {
72 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
73 if (pos) {
74 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
75 &reg32);
76 *aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
77 nvec = max(nvec, *aer + 1);
81 if (mask & PCIE_PORT_SERVICE_DPC) {
82 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
83 if (pos) {
84 pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
85 &reg16);
86 *dpc = reg16 & PCI_EXP_DPC_IRQ;
87 nvec = max(nvec, *dpc + 1);
91 return nvec;
94 /**
95 * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
96 * for given port
97 * @dev: PCI Express port to handle
98 * @irqs: Array of interrupt vectors to populate
99 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
101 * Return value: 0 on success, error code on failure
103 static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
105 int nr_entries, nvec;
106 u32 pme = 0, aer = 0, dpc = 0;
108 /* Allocate the maximum possible number of MSI/MSI-X vectors */
109 nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSI_ENTRIES,
110 PCI_IRQ_MSIX | PCI_IRQ_MSI);
111 if (nr_entries < 0)
112 return nr_entries;
114 /* See how many and which Interrupt Message Numbers we actually use */
115 nvec = pcie_message_numbers(dev, mask, &pme, &aer, &dpc);
116 if (nvec > nr_entries) {
117 pci_free_irq_vectors(dev);
118 return -EIO;
122 * If we allocated more than we need, free them and reallocate fewer.
124 * Reallocating may change the specific vectors we get, so
125 * pci_irq_vector() must be done *after* the reallocation.
127 * If we're using MSI, hardware is *allowed* to change the Interrupt
128 * Message Numbers when we free and reallocate the vectors, but we
129 * assume it won't because we allocate enough vectors for the
130 * biggest Message Number we found.
132 if (nvec != nr_entries) {
133 pci_free_irq_vectors(dev);
135 nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
136 PCI_IRQ_MSIX | PCI_IRQ_MSI);
137 if (nr_entries < 0)
138 return nr_entries;
141 /* PME and hotplug share an MSI/MSI-X vector */
142 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
143 irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, pme);
144 irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, pme);
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);
153 return 0;
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)
166 int ret, i;
168 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
169 irqs[i] = -1;
172 * If we support PME or hotplug, but we can't use MSI/MSI-X for
173 * them, we have to fall back to INTx or other interrupts, e.g., a
174 * system shared interrupt.
176 if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
177 goto legacy_irq;
179 if ((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())
180 goto legacy_irq;
182 /* Try to use MSI-X or MSI if supported */
183 if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
184 return 0;
186 legacy_irq:
187 /* fall back to legacy IRQ */
188 ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
189 if (ret < 0)
190 return -ENODEV;
192 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
193 if (i != PCIE_PORT_SERVICE_VC_SHIFT)
194 irqs[i] = pci_irq_vector(dev, 0);
197 return 0;
201 * get_port_device_capability - discover capabilities of a PCI Express port
202 * @dev: PCI Express port to examine
204 * The capabilities are read from the port's PCI Express configuration registers
205 * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
206 * 7.9 - 7.11.
208 * Return value: Bitmask of discovered port capabilities
210 static int get_port_device_capability(struct pci_dev *dev)
212 int services = 0;
213 int cap_mask = 0;
215 if (pcie_ports_disabled)
216 return 0;
218 cap_mask = PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP
219 | PCIE_PORT_SERVICE_VC;
220 if (pci_aer_available())
221 cap_mask |= PCIE_PORT_SERVICE_AER | PCIE_PORT_SERVICE_DPC;
223 if (pcie_ports_auto)
224 pcie_port_platform_notify(dev, &cap_mask);
226 /* Hot-Plug Capable */
227 if ((cap_mask & PCIE_PORT_SERVICE_HP) && dev->is_hotplug_bridge) {
228 services |= PCIE_PORT_SERVICE_HP;
230 * Disable hot-plug interrupts in case they have been enabled
231 * by the BIOS and the hot-plug service driver is not loaded.
233 pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
234 PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
236 /* AER capable */
237 if ((cap_mask & PCIE_PORT_SERVICE_AER)
238 && pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)) {
239 services |= PCIE_PORT_SERVICE_AER;
241 * Disable AER on this port in case it's been enabled by the
242 * BIOS (the AER service driver will enable it when necessary).
244 pci_disable_pcie_error_reporting(dev);
246 /* VC support */
247 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VC))
248 services |= PCIE_PORT_SERVICE_VC;
249 /* Root ports are capable of generating PME too */
250 if ((cap_mask & PCIE_PORT_SERVICE_PME)
251 && pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
252 services |= PCIE_PORT_SERVICE_PME;
254 * Disable PME interrupt on this port in case it's been enabled
255 * by the BIOS (the PME service driver will enable it when
256 * necessary).
258 pcie_pme_interrupt_enable(dev, false);
260 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC))
261 services |= PCIE_PORT_SERVICE_DPC;
263 return services;
267 * pcie_device_init - allocate and initialize PCI Express port service device
268 * @pdev: PCI Express port to associate the service device with
269 * @service: Type of service to associate with the service device
270 * @irq: Interrupt vector to associate with the service device
272 static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
274 int retval;
275 struct pcie_device *pcie;
276 struct device *device;
278 pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
279 if (!pcie)
280 return -ENOMEM;
281 pcie->port = pdev;
282 pcie->irq = irq;
283 pcie->service = service;
285 /* Initialize generic device interface */
286 device = &pcie->device;
287 device->bus = &pcie_port_bus_type;
288 device->release = release_pcie_device; /* callback to free pcie dev */
289 dev_set_name(device, "%s:pcie%03x",
290 pci_name(pdev),
291 get_descriptor_id(pci_pcie_type(pdev), service));
292 device->parent = &pdev->dev;
293 device_enable_async_suspend(device);
295 retval = device_register(device);
296 if (retval) {
297 put_device(device);
298 return retval;
301 pm_runtime_no_callbacks(device);
303 return 0;
307 * pcie_port_device_register - register PCI Express port
308 * @dev: PCI Express port to register
310 * Allocate the port extension structure and register services associated with
311 * the port.
313 int pcie_port_device_register(struct pci_dev *dev)
315 int status, capabilities, i, nr_service;
316 int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
318 /* Enable PCI Express port device */
319 status = pci_enable_device(dev);
320 if (status)
321 return status;
323 /* Get and check PCI Express port services */
324 capabilities = get_port_device_capability(dev);
325 if (!capabilities)
326 return 0;
328 pci_set_master(dev);
330 * Initialize service irqs. Don't use service devices that
331 * require interrupts if there is no way to generate them.
332 * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
333 * that can be used in the absence of irqs. Allow them to determine
334 * if that is to be used.
336 status = pcie_init_service_irqs(dev, irqs, capabilities);
337 if (status) {
338 capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP;
339 if (!capabilities)
340 goto error_disable;
343 /* Allocate child services if any */
344 status = -ENODEV;
345 nr_service = 0;
346 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
347 int service = 1 << i;
348 if (!(capabilities & service))
349 continue;
350 if (!pcie_device_init(dev, service, irqs[i]))
351 nr_service++;
353 if (!nr_service)
354 goto error_cleanup_irqs;
356 return 0;
358 error_cleanup_irqs:
359 pci_free_irq_vectors(dev);
360 error_disable:
361 pci_disable_device(dev);
362 return status;
365 #ifdef CONFIG_PM
366 static int suspend_iter(struct device *dev, void *data)
368 struct pcie_port_service_driver *service_driver;
370 if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
371 service_driver = to_service_driver(dev->driver);
372 if (service_driver->suspend)
373 service_driver->suspend(to_pcie_device(dev));
375 return 0;
379 * pcie_port_device_suspend - suspend port services associated with a PCIe port
380 * @dev: PCI Express port to handle
382 int pcie_port_device_suspend(struct device *dev)
384 return device_for_each_child(dev, NULL, suspend_iter);
387 static int resume_iter(struct device *dev, void *data)
389 struct pcie_port_service_driver *service_driver;
391 if ((dev->bus == &pcie_port_bus_type) &&
392 (dev->driver)) {
393 service_driver = to_service_driver(dev->driver);
394 if (service_driver->resume)
395 service_driver->resume(to_pcie_device(dev));
397 return 0;
401 * pcie_port_device_resume - resume port services associated with a PCIe port
402 * @dev: PCI Express port to handle
404 int pcie_port_device_resume(struct device *dev)
406 return device_for_each_child(dev, NULL, resume_iter);
408 #endif /* PM */
410 static int remove_iter(struct device *dev, void *data)
412 if (dev->bus == &pcie_port_bus_type)
413 device_unregister(dev);
414 return 0;
418 * pcie_port_device_remove - unregister PCI Express port service devices
419 * @dev: PCI Express port the service devices to unregister are associated with
421 * Remove PCI Express port service devices associated with given port and
422 * disable MSI-X or MSI for the port.
424 void pcie_port_device_remove(struct pci_dev *dev)
426 device_for_each_child(&dev->dev, NULL, remove_iter);
427 pci_free_irq_vectors(dev);
428 pci_disable_device(dev);
432 * pcie_port_probe_service - probe driver for given PCI Express port service
433 * @dev: PCI Express port service device to probe against
435 * If PCI Express port service driver is registered with
436 * pcie_port_service_register(), this function will be called by the driver core
437 * whenever match is found between the driver and a port service device.
439 static int pcie_port_probe_service(struct device *dev)
441 struct pcie_device *pciedev;
442 struct pcie_port_service_driver *driver;
443 int status;
445 if (!dev || !dev->driver)
446 return -ENODEV;
448 driver = to_service_driver(dev->driver);
449 if (!driver || !driver->probe)
450 return -ENODEV;
452 pciedev = to_pcie_device(dev);
453 status = driver->probe(pciedev);
454 if (status)
455 return status;
457 get_device(dev);
458 return 0;
462 * pcie_port_remove_service - detach driver from given PCI Express port service
463 * @dev: PCI Express port service device to handle
465 * If PCI Express port service driver is registered with
466 * pcie_port_service_register(), this function will be called by the driver core
467 * when device_unregister() is called for the port service device associated
468 * with the driver.
470 static int pcie_port_remove_service(struct device *dev)
472 struct pcie_device *pciedev;
473 struct pcie_port_service_driver *driver;
475 if (!dev || !dev->driver)
476 return 0;
478 pciedev = to_pcie_device(dev);
479 driver = to_service_driver(dev->driver);
480 if (driver && driver->remove) {
481 driver->remove(pciedev);
482 put_device(dev);
484 return 0;
488 * pcie_port_shutdown_service - shut down given PCI Express port service
489 * @dev: PCI Express port service device to handle
491 * If PCI Express port service driver is registered with
492 * pcie_port_service_register(), this function will be called by the driver core
493 * when device_shutdown() is called for the port service device associated
494 * with the driver.
496 static void pcie_port_shutdown_service(struct device *dev) {}
499 * pcie_port_service_register - register PCI Express port service driver
500 * @new: PCI Express port service driver to register
502 int pcie_port_service_register(struct pcie_port_service_driver *new)
504 if (pcie_ports_disabled)
505 return -ENODEV;
507 new->driver.name = new->name;
508 new->driver.bus = &pcie_port_bus_type;
509 new->driver.probe = pcie_port_probe_service;
510 new->driver.remove = pcie_port_remove_service;
511 new->driver.shutdown = pcie_port_shutdown_service;
513 return driver_register(&new->driver);
515 EXPORT_SYMBOL(pcie_port_service_register);
518 * pcie_port_service_unregister - unregister PCI Express port service driver
519 * @drv: PCI Express port service driver to unregister
521 void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
523 driver_unregister(&drv->driver);
525 EXPORT_SYMBOL(pcie_port_service_unregister);