dt-bindings: arm: rockchip: remove reference to fennec board
[sfrench/cifs-2.6.git] / drivers / pci / pcie / portdrv_core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Purpose:     PCI Express Port Bus Driver's Core Functions
4  *
5  * Copyright (C) 2004 Intel
6  * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7  */
8
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/delay.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/aer.h>
19
20 #include "../pci.h"
21 #include "portdrv.h"
22
23 struct portdrv_service_data {
24         struct pcie_port_service_driver *drv;
25         struct device *dev;
26         u32 service;
27 };
28
29 /**
30  * release_pcie_device - free PCI Express port service device structure
31  * @dev: Port service device to release
32  *
33  * Invoked automatically when device is being removed in response to
34  * device_unregister(dev).  Release all resources being claimed.
35  */
36 static void release_pcie_device(struct device *dev)
37 {
38         kfree(to_pcie_device(dev));
39 }
40
41 /*
42  * Fill in *pme, *aer, *dpc with the relevant Interrupt Message Numbers if
43  * services are enabled in "mask".  Return the number of MSI/MSI-X vectors
44  * required to accommodate the largest Message Number.
45  */
46 static int pcie_message_numbers(struct pci_dev *dev, int mask,
47                                 u32 *pme, u32 *aer, u32 *dpc)
48 {
49         u32 nvec = 0, pos;
50         u16 reg16;
51
52         /*
53          * The Interrupt Message Number indicates which vector is used, i.e.,
54          * the MSI-X table entry or the MSI offset between the base Message
55          * Data and the generated interrupt message.  See PCIe r3.1, sec
56          * 7.8.2, 7.10.10, 7.31.2.
57          */
58
59         if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
60                     PCIE_PORT_SERVICE_BWNOTIF)) {
61                 pcie_capability_read_word(dev, PCI_EXP_FLAGS, &reg16);
62                 *pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
63                 nvec = *pme + 1;
64         }
65
66 #ifdef CONFIG_PCIEAER
67         if (mask & PCIE_PORT_SERVICE_AER) {
68                 u32 reg32;
69
70                 pos = dev->aer_cap;
71                 if (pos) {
72                         pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
73                                               &reg32);
74                         *aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
75                         nvec = max(nvec, *aer + 1);
76                 }
77         }
78 #endif
79
80         if (mask & PCIE_PORT_SERVICE_DPC) {
81                 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
82                 if (pos) {
83                         pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
84                                              &reg16);
85                         *dpc = reg16 & PCI_EXP_DPC_IRQ;
86                         nvec = max(nvec, *dpc + 1);
87                 }
88         }
89
90         return nvec;
91 }
92
93 /**
94  * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
95  * for given port
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()
99  *
100  * Return value: 0 on success, error code on failure
101  */
102 static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
103 {
104         int nr_entries, nvec, pcie_irq;
105         u32 pme = 0, aer = 0, dpc = 0;
106
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);
110         if (nr_entries < 0)
111                 return nr_entries;
112
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);
117                 return -EIO;
118         }
119
120         /*
121          * If we allocated more than we need, free them and reallocate fewer.
122          *
123          * Reallocating may change the specific vectors we get, so
124          * pci_irq_vector() must be done *after* the reallocation.
125          *
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.
130          */
131         if (nvec != nr_entries) {
132                 pci_free_irq_vectors(dev);
133
134                 nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
135                                 PCI_IRQ_MSIX | PCI_IRQ_MSI);
136                 if (nr_entries < 0)
137                         return nr_entries;
138         }
139
140         /* PME, hotplug and bandwidth notification share an MSI/MSI-X vector */
141         if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
142                     PCIE_PORT_SERVICE_BWNOTIF)) {
143                 pcie_irq = pci_irq_vector(dev, pme);
144                 irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pcie_irq;
145                 irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pcie_irq;
146                 irqs[PCIE_PORT_SERVICE_BWNOTIF_SHIFT] = pcie_irq;
147         }
148
149         if (mask & PCIE_PORT_SERVICE_AER)
150                 irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, aer);
151
152         if (mask & PCIE_PORT_SERVICE_DPC)
153                 irqs[PCIE_PORT_SERVICE_DPC_SHIFT] = pci_irq_vector(dev, dpc);
154
155         return 0;
156 }
157
158 /**
159  * pcie_init_service_irqs - initialize irqs for PCI Express port services
160  * @dev: PCI Express port to handle
161  * @irqs: Array of irqs to populate
162  * @mask: Bitmask of port capabilities returned by get_port_device_capability()
163  *
164  * Return value: Interrupt mode associated with the port
165  */
166 static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
167 {
168         int ret, i;
169
170         for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
171                 irqs[i] = -1;
172
173         /*
174          * If we support PME but can't use MSI/MSI-X for it, we have to
175          * fall back to INTx or other interrupts, e.g., a system shared
176          * interrupt.
177          */
178         if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
179                 goto legacy_irq;
180
181         /* Try to use MSI-X or MSI if supported */
182         if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
183                 return 0;
184
185 legacy_irq:
186         /* fall back to legacy IRQ */
187         ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
188         if (ret < 0)
189                 return -ENODEV;
190
191         for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
192                 irqs[i] = pci_irq_vector(dev, 0);
193
194         return 0;
195 }
196
197 /**
198  * get_port_device_capability - discover capabilities of a PCI Express port
199  * @dev: PCI Express port to examine
200  *
201  * The capabilities are read from the port's PCI Express configuration registers
202  * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
203  * 7.9 - 7.11.
204  *
205  * Return value: Bitmask of discovered port capabilities
206  */
207 static int get_port_device_capability(struct pci_dev *dev)
208 {
209         struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
210         int services = 0;
211
212         if (dev->is_hotplug_bridge &&
213             (pcie_ports_native || host->native_pcie_hotplug)) {
214                 services |= PCIE_PORT_SERVICE_HP;
215
216                 /*
217                  * Disable hot-plug interrupts in case they have been enabled
218                  * by the BIOS and the hot-plug service driver is not loaded.
219                  */
220                 pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
221                           PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
222         }
223
224 #ifdef CONFIG_PCIEAER
225         if (dev->aer_cap && pci_aer_available() &&
226             (pcie_ports_native || host->native_aer)) {
227                 services |= PCIE_PORT_SERVICE_AER;
228
229                 /*
230                  * Disable AER on this port in case it's been enabled by the
231                  * BIOS (the AER service driver will enable it when necessary).
232                  */
233                 pci_disable_pcie_error_reporting(dev);
234         }
235 #endif
236
237         /*
238          * Root ports are capable of generating PME too.  Root Complex
239          * Event Collectors can also generate PMEs, but we don't handle
240          * those yet.
241          */
242         if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT &&
243             (pcie_ports_native || host->native_pme)) {
244                 services |= PCIE_PORT_SERVICE_PME;
245
246                 /*
247                  * Disable PME interrupt on this port in case it's been enabled
248                  * by the BIOS (the PME service driver will enable it when
249                  * necessary).
250                  */
251                 pcie_pme_interrupt_enable(dev, false);
252         }
253
254         if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC) &&
255             pci_aer_available() && services & PCIE_PORT_SERVICE_AER)
256                 services |= PCIE_PORT_SERVICE_DPC;
257
258         if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
259             pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
260                 services |= PCIE_PORT_SERVICE_BWNOTIF;
261
262         return services;
263 }
264
265 /**
266  * pcie_device_init - allocate and initialize PCI Express port service device
267  * @pdev: PCI Express port to associate the service device with
268  * @service: Type of service to associate with the service device
269  * @irq: Interrupt vector to associate with the service device
270  */
271 static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
272 {
273         int retval;
274         struct pcie_device *pcie;
275         struct device *device;
276
277         pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
278         if (!pcie)
279                 return -ENOMEM;
280         pcie->port = pdev;
281         pcie->irq = irq;
282         pcie->service = service;
283
284         /* Initialize generic device interface */
285         device = &pcie->device;
286         device->bus = &pcie_port_bus_type;
287         device->release = release_pcie_device;  /* callback to free pcie dev */
288         dev_set_name(device, "%s:pcie%03x",
289                      pci_name(pdev),
290                      get_descriptor_id(pci_pcie_type(pdev), service));
291         device->parent = &pdev->dev;
292         device_enable_async_suspend(device);
293
294         retval = device_register(device);
295         if (retval) {
296                 put_device(device);
297                 return retval;
298         }
299
300         pm_runtime_no_callbacks(device);
301
302         return 0;
303 }
304
305 /**
306  * pcie_port_device_register - register PCI Express port
307  * @dev: PCI Express port to register
308  *
309  * Allocate the port extension structure and register services associated with
310  * the port.
311  */
312 int pcie_port_device_register(struct pci_dev *dev)
313 {
314         int status, capabilities, i, nr_service;
315         int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
316
317         /* Enable PCI Express port device */
318         status = pci_enable_device(dev);
319         if (status)
320                 return status;
321
322         /* Get and check PCI Express port services */
323         capabilities = get_port_device_capability(dev);
324         if (!capabilities)
325                 return 0;
326
327         pci_set_master(dev);
328         /*
329          * Initialize service irqs. Don't use service devices that
330          * require interrupts if there is no way to generate them.
331          * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
332          * that can be used in the absence of irqs.  Allow them to determine
333          * if that is to be used.
334          */
335         status = pcie_init_service_irqs(dev, irqs, capabilities);
336         if (status) {
337                 capabilities &= PCIE_PORT_SERVICE_HP;
338                 if (!capabilities)
339                         goto error_disable;
340         }
341
342         /* Allocate child services if any */
343         status = -ENODEV;
344         nr_service = 0;
345         for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
346                 int service = 1 << i;
347                 if (!(capabilities & service))
348                         continue;
349                 if (!pcie_device_init(dev, service, irqs[i]))
350                         nr_service++;
351         }
352         if (!nr_service)
353                 goto error_cleanup_irqs;
354
355         return 0;
356
357 error_cleanup_irqs:
358         pci_free_irq_vectors(dev);
359 error_disable:
360         pci_disable_device(dev);
361         return status;
362 }
363
364 #ifdef CONFIG_PM
365 typedef int (*pcie_pm_callback_t)(struct pcie_device *);
366
367 static int pm_iter(struct device *dev, void *data)
368 {
369         struct pcie_port_service_driver *service_driver;
370         size_t offset = *(size_t *)data;
371         pcie_pm_callback_t cb;
372
373         if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
374                 service_driver = to_service_driver(dev->driver);
375                 cb = *(pcie_pm_callback_t *)((void *)service_driver + offset);
376                 if (cb)
377                         return cb(to_pcie_device(dev));
378         }
379         return 0;
380 }
381
382 static int get_downstream_delay(struct pci_bus *bus)
383 {
384         struct pci_dev *pdev;
385         int min_delay = 100;
386         int max_delay = 0;
387
388         list_for_each_entry(pdev, &bus->devices, bus_list) {
389                 if (!pdev->imm_ready)
390                         min_delay = 0;
391                 else if (pdev->d3cold_delay < min_delay)
392                         min_delay = pdev->d3cold_delay;
393                 if (pdev->d3cold_delay > max_delay)
394                         max_delay = pdev->d3cold_delay;
395         }
396
397         return max(min_delay, max_delay);
398 }
399
400 /*
401  * wait_for_downstream_link - Wait for downstream link to establish
402  * @pdev: PCIe port whose downstream link is waited
403  *
404  * Handle delays according to PCIe 4.0 section 6.6.1 before configuration
405  * access to the downstream component is permitted.
406  *
407  * This blocks PCI core resume of the hierarchy below this port until the
408  * link is trained. Should be called before resuming port services to
409  * prevent pciehp from starting to tear-down the hierarchy too soon.
410  */
411 static void wait_for_downstream_link(struct pci_dev *pdev)
412 {
413         int delay;
414
415         if (pci_pcie_type(pdev) != PCI_EXP_TYPE_ROOT_PORT &&
416             pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM)
417                 return;
418
419         if (pci_dev_is_disconnected(pdev))
420                 return;
421
422         if (!pdev->subordinate || list_empty(&pdev->subordinate->devices) ||
423             !pdev->bridge_d3)
424                 return;
425
426         delay = get_downstream_delay(pdev->subordinate);
427         if (!delay)
428                 return;
429
430         dev_dbg(&pdev->dev, "waiting downstream link for %d ms\n", delay);
431
432         /*
433          * If downstream port does not support speeds greater than 5 GT/s
434          * need to wait 100ms. For higher speeds (gen3) we need to wait
435          * first for the data link layer to become active.
436          */
437         if (pcie_get_speed_cap(pdev) <= PCIE_SPEED_5_0GT)
438                 msleep(delay);
439         else
440                 pcie_wait_for_link_delay(pdev, true, delay);
441 }
442
443 /**
444  * pcie_port_device_suspend - suspend port services associated with a PCIe port
445  * @dev: PCI Express port to handle
446  */
447 int pcie_port_device_suspend(struct device *dev)
448 {
449         size_t off = offsetof(struct pcie_port_service_driver, suspend);
450         return device_for_each_child(dev, &off, pm_iter);
451 }
452
453 int pcie_port_device_resume_noirq(struct device *dev)
454 {
455         size_t off = offsetof(struct pcie_port_service_driver, resume_noirq);
456
457         wait_for_downstream_link(to_pci_dev(dev));
458         return device_for_each_child(dev, &off, pm_iter);
459 }
460
461 /**
462  * pcie_port_device_resume - resume port services associated with a PCIe port
463  * @dev: PCI Express port to handle
464  */
465 int pcie_port_device_resume(struct device *dev)
466 {
467         size_t off = offsetof(struct pcie_port_service_driver, resume);
468         return device_for_each_child(dev, &off, pm_iter);
469 }
470
471 /**
472  * pcie_port_device_runtime_suspend - runtime suspend port services
473  * @dev: PCI Express port to handle
474  */
475 int pcie_port_device_runtime_suspend(struct device *dev)
476 {
477         size_t off = offsetof(struct pcie_port_service_driver, runtime_suspend);
478         return device_for_each_child(dev, &off, pm_iter);
479 }
480
481 /**
482  * pcie_port_device_runtime_resume - runtime resume port services
483  * @dev: PCI Express port to handle
484  */
485 int pcie_port_device_runtime_resume(struct device *dev)
486 {
487         size_t off = offsetof(struct pcie_port_service_driver, runtime_resume);
488
489         wait_for_downstream_link(to_pci_dev(dev));
490         return device_for_each_child(dev, &off, pm_iter);
491 }
492 #endif /* PM */
493
494 static int remove_iter(struct device *dev, void *data)
495 {
496         if (dev->bus == &pcie_port_bus_type)
497                 device_unregister(dev);
498         return 0;
499 }
500
501 static int find_service_iter(struct device *device, void *data)
502 {
503         struct pcie_port_service_driver *service_driver;
504         struct portdrv_service_data *pdrvs;
505         u32 service;
506
507         pdrvs = (struct portdrv_service_data *) data;
508         service = pdrvs->service;
509
510         if (device->bus == &pcie_port_bus_type && device->driver) {
511                 service_driver = to_service_driver(device->driver);
512                 if (service_driver->service == service) {
513                         pdrvs->drv = service_driver;
514                         pdrvs->dev = device;
515                         return 1;
516                 }
517         }
518
519         return 0;
520 }
521
522 /**
523  * pcie_port_find_service - find the service driver
524  * @dev: PCI Express port the service is associated with
525  * @service: Service to find
526  *
527  * Find PCI Express port service driver associated with given service
528  */
529 struct pcie_port_service_driver *pcie_port_find_service(struct pci_dev *dev,
530                                                         u32 service)
531 {
532         struct pcie_port_service_driver *drv;
533         struct portdrv_service_data pdrvs;
534
535         pdrvs.drv = NULL;
536         pdrvs.service = service;
537         device_for_each_child(&dev->dev, &pdrvs, find_service_iter);
538
539         drv = pdrvs.drv;
540         return drv;
541 }
542
543 /**
544  * pcie_port_find_device - find the struct device
545  * @dev: PCI Express port the service is associated with
546  * @service: For the service to find
547  *
548  * Find the struct device associated with given service on a pci_dev
549  */
550 struct device *pcie_port_find_device(struct pci_dev *dev,
551                                       u32 service)
552 {
553         struct device *device;
554         struct portdrv_service_data pdrvs;
555
556         pdrvs.dev = NULL;
557         pdrvs.service = service;
558         device_for_each_child(&dev->dev, &pdrvs, find_service_iter);
559
560         device = pdrvs.dev;
561         return device;
562 }
563 EXPORT_SYMBOL_GPL(pcie_port_find_device);
564
565 /**
566  * pcie_port_device_remove - unregister PCI Express port service devices
567  * @dev: PCI Express port the service devices to unregister are associated with
568  *
569  * Remove PCI Express port service devices associated with given port and
570  * disable MSI-X or MSI for the port.
571  */
572 void pcie_port_device_remove(struct pci_dev *dev)
573 {
574         device_for_each_child(&dev->dev, NULL, remove_iter);
575         pci_free_irq_vectors(dev);
576         pci_disable_device(dev);
577 }
578
579 /**
580  * pcie_port_probe_service - probe driver for given PCI Express port service
581  * @dev: PCI Express port service device to probe against
582  *
583  * If PCI Express port service driver is registered with
584  * pcie_port_service_register(), this function will be called by the driver core
585  * whenever match is found between the driver and a port service device.
586  */
587 static int pcie_port_probe_service(struct device *dev)
588 {
589         struct pcie_device *pciedev;
590         struct pcie_port_service_driver *driver;
591         int status;
592
593         if (!dev || !dev->driver)
594                 return -ENODEV;
595
596         driver = to_service_driver(dev->driver);
597         if (!driver || !driver->probe)
598                 return -ENODEV;
599
600         pciedev = to_pcie_device(dev);
601         status = driver->probe(pciedev);
602         if (status)
603                 return status;
604
605         get_device(dev);
606         return 0;
607 }
608
609 /**
610  * pcie_port_remove_service - detach driver from given PCI Express port service
611  * @dev: PCI Express port service device to handle
612  *
613  * If PCI Express port service driver is registered with
614  * pcie_port_service_register(), this function will be called by the driver core
615  * when device_unregister() is called for the port service device associated
616  * with the driver.
617  */
618 static int pcie_port_remove_service(struct device *dev)
619 {
620         struct pcie_device *pciedev;
621         struct pcie_port_service_driver *driver;
622
623         if (!dev || !dev->driver)
624                 return 0;
625
626         pciedev = to_pcie_device(dev);
627         driver = to_service_driver(dev->driver);
628         if (driver && driver->remove) {
629                 driver->remove(pciedev);
630                 put_device(dev);
631         }
632         return 0;
633 }
634
635 /**
636  * pcie_port_shutdown_service - shut down given PCI Express port service
637  * @dev: PCI Express port service device to handle
638  *
639  * If PCI Express port service driver is registered with
640  * pcie_port_service_register(), this function will be called by the driver core
641  * when device_shutdown() is called for the port service device associated
642  * with the driver.
643  */
644 static void pcie_port_shutdown_service(struct device *dev) {}
645
646 /**
647  * pcie_port_service_register - register PCI Express port service driver
648  * @new: PCI Express port service driver to register
649  */
650 int pcie_port_service_register(struct pcie_port_service_driver *new)
651 {
652         if (pcie_ports_disabled)
653                 return -ENODEV;
654
655         new->driver.name = new->name;
656         new->driver.bus = &pcie_port_bus_type;
657         new->driver.probe = pcie_port_probe_service;
658         new->driver.remove = pcie_port_remove_service;
659         new->driver.shutdown = pcie_port_shutdown_service;
660
661         return driver_register(&new->driver);
662 }
663 EXPORT_SYMBOL(pcie_port_service_register);
664
665 /**
666  * pcie_port_service_unregister - unregister PCI Express port service driver
667  * @drv: PCI Express port service driver to unregister
668  */
669 void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
670 {
671         driver_unregister(&drv->driver);
672 }
673 EXPORT_SYMBOL(pcie_port_service_unregister);