xref: /openbmc/linux/drivers/pci/pcie/portdrv.c (revision b694e3c604e999343258c49e574abd7be012e726)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Purpose:	PCI Express Port Bus Driver
4  *
5  * Copyright (C) 2004 Intel
6  * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7  */
8 
9 #include <linux/dmi.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/pm.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/string.h>
18 #include <linux/slab.h>
19 #include <linux/aer.h>
20 
21 #include "../pci.h"
22 #include "portdrv.h"
23 
24 /*
25  * The PCIe Capability Interrupt Message Number (PCIe r3.1, sec 7.8.2) must
26  * be one of the first 32 MSI-X entries.  Per PCI r3.0, sec 6.8.3.1, MSI
27  * supports a maximum of 32 vectors per function.
28  */
29 #define PCIE_PORT_MAX_MSI_ENTRIES	32
30 
31 #define get_descriptor_id(type, service) (((type - 4) << 8) | service)
32 
33 struct portdrv_service_data {
34 	struct pcie_port_service_driver *drv;
35 	struct device *dev;
36 	u32 service;
37 };
38 
39 /**
40  * release_pcie_device - free PCI Express port service device structure
41  * @dev: Port service device to release
42  *
43  * Invoked automatically when device is being removed in response to
44  * device_unregister(dev).  Release all resources being claimed.
45  */
release_pcie_device(struct device * dev)46 static void release_pcie_device(struct device *dev)
47 {
48 	kfree(to_pcie_device(dev));
49 }
50 
51 /*
52  * Fill in *pme, *aer, *dpc with the relevant Interrupt Message Numbers if
53  * services are enabled in "mask".  Return the number of MSI/MSI-X vectors
54  * required to accommodate the largest Message Number.
55  */
pcie_message_numbers(struct pci_dev * dev,int mask,u32 * pme,u32 * aer,u32 * dpc)56 static int pcie_message_numbers(struct pci_dev *dev, int mask,
57 				u32 *pme, u32 *aer, u32 *dpc)
58 {
59 	u32 nvec = 0, pos;
60 	u16 reg16;
61 
62 	/*
63 	 * The Interrupt Message Number indicates which vector is used, i.e.,
64 	 * the MSI-X table entry or the MSI offset between the base Message
65 	 * Data and the generated interrupt message.  See PCIe r3.1, sec
66 	 * 7.8.2, 7.10.10, 7.31.2.
67 	 */
68 
69 	if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
70 		    PCIE_PORT_SERVICE_BWNOTIF)) {
71 		pcie_capability_read_word(dev, PCI_EXP_FLAGS, &reg16);
72 		*pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
73 		nvec = *pme + 1;
74 	}
75 
76 #ifdef CONFIG_PCIEAER
77 	if (mask & PCIE_PORT_SERVICE_AER) {
78 		u32 reg32;
79 
80 		pos = dev->aer_cap;
81 		if (pos) {
82 			pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
83 					      &reg32);
84 			*aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
85 			nvec = max(nvec, *aer + 1);
86 		}
87 	}
88 #endif
89 
90 	if (mask & PCIE_PORT_SERVICE_DPC) {
91 		pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
92 		if (pos) {
93 			pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
94 					     &reg16);
95 			*dpc = reg16 & PCI_EXP_DPC_IRQ;
96 			nvec = max(nvec, *dpc + 1);
97 		}
98 	}
99 
100 	return nvec;
101 }
102 
103 /**
104  * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
105  * for given port
106  * @dev: PCI Express port to handle
107  * @irqs: Array of interrupt vectors to populate
108  * @mask: Bitmask of port capabilities returned by get_port_device_capability()
109  *
110  * Return value: 0 on success, error code on failure
111  */
pcie_port_enable_irq_vec(struct pci_dev * dev,int * irqs,int mask)112 static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
113 {
114 	int nr_entries, nvec, pcie_irq;
115 	u32 pme = 0, aer = 0, dpc = 0;
116 
117 	/* Allocate the maximum possible number of MSI/MSI-X vectors */
118 	nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSI_ENTRIES,
119 			PCI_IRQ_MSIX | PCI_IRQ_MSI);
120 	if (nr_entries < 0)
121 		return nr_entries;
122 
123 	/* See how many and which Interrupt Message Numbers we actually use */
124 	nvec = pcie_message_numbers(dev, mask, &pme, &aer, &dpc);
125 	if (nvec > nr_entries) {
126 		pci_free_irq_vectors(dev);
127 		return -EIO;
128 	}
129 
130 	/*
131 	 * If we allocated more than we need, free them and reallocate fewer.
132 	 *
133 	 * Reallocating may change the specific vectors we get, so
134 	 * pci_irq_vector() must be done *after* the reallocation.
135 	 *
136 	 * If we're using MSI, hardware is *allowed* to change the Interrupt
137 	 * Message Numbers when we free and reallocate the vectors, but we
138 	 * assume it won't because we allocate enough vectors for the
139 	 * biggest Message Number we found.
140 	 */
141 	if (nvec != nr_entries) {
142 		pci_free_irq_vectors(dev);
143 
144 		nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
145 				PCI_IRQ_MSIX | PCI_IRQ_MSI);
146 		if (nr_entries < 0)
147 			return nr_entries;
148 	}
149 
150 	/* PME, hotplug and bandwidth notification share an MSI/MSI-X vector */
151 	if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
152 		    PCIE_PORT_SERVICE_BWNOTIF)) {
153 		pcie_irq = pci_irq_vector(dev, pme);
154 		irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pcie_irq;
155 		irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pcie_irq;
156 		irqs[PCIE_PORT_SERVICE_BWNOTIF_SHIFT] = pcie_irq;
157 	}
158 
159 	if (mask & PCIE_PORT_SERVICE_AER)
160 		irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, aer);
161 
162 	if (mask & PCIE_PORT_SERVICE_DPC)
163 		irqs[PCIE_PORT_SERVICE_DPC_SHIFT] = pci_irq_vector(dev, dpc);
164 
165 	return 0;
166 }
167 
168 /**
169  * pcie_init_service_irqs - initialize irqs for PCI Express port services
170  * @dev: PCI Express port to handle
171  * @irqs: Array of irqs to populate
172  * @mask: Bitmask of port capabilities returned by get_port_device_capability()
173  *
174  * Return value: Interrupt mode associated with the port
175  */
pcie_init_service_irqs(struct pci_dev * dev,int * irqs,int mask)176 static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
177 {
178 	int ret, i;
179 
180 	for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
181 		irqs[i] = -1;
182 
183 	/*
184 	 * If we support PME but can't use MSI/MSI-X for it, we have to
185 	 * fall back to INTx or other interrupts, e.g., a system shared
186 	 * interrupt.
187 	 */
188 	if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
189 		goto legacy_irq;
190 
191 	/* Try to use MSI-X or MSI if supported */
192 	if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
193 		return 0;
194 
195 legacy_irq:
196 	/* fall back to legacy IRQ */
197 	ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
198 	if (ret < 0)
199 		return -ENODEV;
200 
201 	for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
202 		irqs[i] = pci_irq_vector(dev, 0);
203 
204 	return 0;
205 }
206 
207 /**
208  * get_port_device_capability - discover capabilities of a PCI Express port
209  * @dev: PCI Express port to examine
210  *
211  * The capabilities are read from the port's PCI Express configuration registers
212  * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
213  * 7.9 - 7.11.
214  *
215  * Return value: Bitmask of discovered port capabilities
216  */
get_port_device_capability(struct pci_dev * dev)217 static int get_port_device_capability(struct pci_dev *dev)
218 {
219 	struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
220 	int services = 0;
221 
222 	if (dev->is_hotplug_bridge &&
223 	    (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT ||
224 	     pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM) &&
225 	    (pcie_ports_native || host->native_pcie_hotplug)) {
226 		services |= PCIE_PORT_SERVICE_HP;
227 
228 		/*
229 		 * Disable hot-plug interrupts in case they have been enabled
230 		 * by the BIOS and the hot-plug service driver won't be loaded
231 		 * to handle them.
232 		 */
233 		if (!IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
234 			pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
235 				PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
236 	}
237 
238 #ifdef CONFIG_PCIEAER
239 	if ((pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT ||
240              pci_pcie_type(dev) == PCI_EXP_TYPE_RC_EC) &&
241 	    dev->aer_cap && pci_aer_available() &&
242 	    (pcie_ports_native || host->native_aer))
243 		services |= PCIE_PORT_SERVICE_AER;
244 #endif
245 
246 	/* Root Ports and Root Complex Event Collectors may generate PMEs */
247 	if ((pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT ||
248 	     pci_pcie_type(dev) == PCI_EXP_TYPE_RC_EC) &&
249 	    (pcie_ports_native || host->native_pme)) {
250 		services |= PCIE_PORT_SERVICE_PME;
251 
252 		/*
253 		 * Disable PME interrupt on this port in case it's been enabled
254 		 * by the BIOS (the PME service driver will enable it when
255 		 * necessary).
256 		 */
257 		pcie_pme_interrupt_enable(dev, false);
258 	}
259 
260 	/*
261 	 * With dpc-native, allow Linux to use DPC even if it doesn't have
262 	 * permission to use AER.
263 	 */
264 	if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC) &&
265 	    pci_aer_available() &&
266 	    (pcie_ports_dpc_native || (services & PCIE_PORT_SERVICE_AER)))
267 		services |= PCIE_PORT_SERVICE_DPC;
268 
269 	if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
270 	    pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
271 		u32 linkcap;
272 
273 		pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &linkcap);
274 		if (linkcap & PCI_EXP_LNKCAP_LBNC)
275 			services |= PCIE_PORT_SERVICE_BWNOTIF;
276 	}
277 
278 	return services;
279 }
280 
281 /**
282  * pcie_device_init - allocate and initialize PCI Express port service device
283  * @pdev: PCI Express port to associate the service device with
284  * @service: Type of service to associate with the service device
285  * @irq: Interrupt vector to associate with the service device
286  */
pcie_device_init(struct pci_dev * pdev,int service,int irq)287 static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
288 {
289 	int retval;
290 	struct pcie_device *pcie;
291 	struct device *device;
292 
293 	pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
294 	if (!pcie)
295 		return -ENOMEM;
296 	pcie->port = pdev;
297 	pcie->irq = irq;
298 	pcie->service = service;
299 
300 	/* Initialize generic device interface */
301 	device = &pcie->device;
302 	device->bus = &pcie_port_bus_type;
303 	device->release = release_pcie_device;	/* callback to free pcie dev */
304 	dev_set_name(device, "%s:pcie%03x",
305 		     pci_name(pdev),
306 		     get_descriptor_id(pci_pcie_type(pdev), service));
307 	device->parent = &pdev->dev;
308 	device_enable_async_suspend(device);
309 
310 	retval = device_register(device);
311 	if (retval) {
312 		put_device(device);
313 		return retval;
314 	}
315 
316 	pm_runtime_no_callbacks(device);
317 
318 	return 0;
319 }
320 
321 /**
322  * pcie_port_device_register - register PCI Express port
323  * @dev: PCI Express port to register
324  *
325  * Allocate the port extension structure and register services associated with
326  * the port.
327  */
pcie_port_device_register(struct pci_dev * dev)328 static int pcie_port_device_register(struct pci_dev *dev)
329 {
330 	int status, capabilities, i, nr_service;
331 	int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
332 
333 	/* Enable PCI Express port device */
334 	status = pci_enable_device(dev);
335 	if (status)
336 		return status;
337 
338 	/* Get and check PCI Express port services */
339 	capabilities = get_port_device_capability(dev);
340 	if (!capabilities)
341 		return 0;
342 
343 	pci_set_master(dev);
344 	/*
345 	 * Initialize service irqs. Don't use service devices that
346 	 * require interrupts if there is no way to generate them.
347 	 * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
348 	 * that can be used in the absence of irqs.  Allow them to determine
349 	 * if that is to be used.
350 	 */
351 	status = pcie_init_service_irqs(dev, irqs, capabilities);
352 	if (status) {
353 		capabilities &= PCIE_PORT_SERVICE_HP;
354 		if (!capabilities)
355 			goto error_disable;
356 	}
357 
358 	/* Allocate child services if any */
359 	status = -ENODEV;
360 	nr_service = 0;
361 	for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
362 		int service = 1 << i;
363 		if (!(capabilities & service))
364 			continue;
365 		if (!pcie_device_init(dev, service, irqs[i]))
366 			nr_service++;
367 	}
368 	if (!nr_service)
369 		goto error_cleanup_irqs;
370 
371 	return 0;
372 
373 error_cleanup_irqs:
374 	pci_free_irq_vectors(dev);
375 error_disable:
376 	pci_disable_device(dev);
377 	return status;
378 }
379 
380 typedef int (*pcie_callback_t)(struct pcie_device *);
381 
pcie_port_device_iter(struct device * dev,void * data)382 static int pcie_port_device_iter(struct device *dev, void *data)
383 {
384 	struct pcie_port_service_driver *service_driver;
385 	size_t offset = *(size_t *)data;
386 	pcie_callback_t cb;
387 
388 	if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
389 		service_driver = to_service_driver(dev->driver);
390 		cb = *(pcie_callback_t *)((void *)service_driver + offset);
391 		if (cb)
392 			return cb(to_pcie_device(dev));
393 	}
394 	return 0;
395 }
396 
397 #ifdef CONFIG_PM
398 /**
399  * pcie_port_device_suspend - suspend port services associated with a PCIe port
400  * @dev: PCI Express port to handle
401  */
pcie_port_device_suspend(struct device * dev)402 static int pcie_port_device_suspend(struct device *dev)
403 {
404 	size_t off = offsetof(struct pcie_port_service_driver, suspend);
405 	return device_for_each_child(dev, &off, pcie_port_device_iter);
406 }
407 
pcie_port_device_resume_noirq(struct device * dev)408 static int pcie_port_device_resume_noirq(struct device *dev)
409 {
410 	size_t off = offsetof(struct pcie_port_service_driver, resume_noirq);
411 	return device_for_each_child(dev, &off, pcie_port_device_iter);
412 }
413 
414 /**
415  * pcie_port_device_resume - resume port services associated with a PCIe port
416  * @dev: PCI Express port to handle
417  */
pcie_port_device_resume(struct device * dev)418 static int pcie_port_device_resume(struct device *dev)
419 {
420 	size_t off = offsetof(struct pcie_port_service_driver, resume);
421 	return device_for_each_child(dev, &off, pcie_port_device_iter);
422 }
423 
424 /**
425  * pcie_port_device_runtime_suspend - runtime suspend port services
426  * @dev: PCI Express port to handle
427  */
pcie_port_device_runtime_suspend(struct device * dev)428 static int pcie_port_device_runtime_suspend(struct device *dev)
429 {
430 	size_t off = offsetof(struct pcie_port_service_driver, runtime_suspend);
431 	return device_for_each_child(dev, &off, pcie_port_device_iter);
432 }
433 
434 /**
435  * pcie_port_device_runtime_resume - runtime resume port services
436  * @dev: PCI Express port to handle
437  */
pcie_port_device_runtime_resume(struct device * dev)438 static int pcie_port_device_runtime_resume(struct device *dev)
439 {
440 	size_t off = offsetof(struct pcie_port_service_driver, runtime_resume);
441 	return device_for_each_child(dev, &off, pcie_port_device_iter);
442 }
443 #endif /* PM */
444 
remove_iter(struct device * dev,void * data)445 static int remove_iter(struct device *dev, void *data)
446 {
447 	if (dev->bus == &pcie_port_bus_type)
448 		device_unregister(dev);
449 	return 0;
450 }
451 
find_service_iter(struct device * device,void * data)452 static int find_service_iter(struct device *device, void *data)
453 {
454 	struct pcie_port_service_driver *service_driver;
455 	struct portdrv_service_data *pdrvs;
456 	u32 service;
457 
458 	pdrvs = (struct portdrv_service_data *) data;
459 	service = pdrvs->service;
460 
461 	if (device->bus == &pcie_port_bus_type && device->driver) {
462 		service_driver = to_service_driver(device->driver);
463 		if (service_driver->service == service) {
464 			pdrvs->drv = service_driver;
465 			pdrvs->dev = device;
466 			return 1;
467 		}
468 	}
469 
470 	return 0;
471 }
472 
473 /**
474  * pcie_port_find_device - find the struct device
475  * @dev: PCI Express port the service is associated with
476  * @service: For the service to find
477  *
478  * Find the struct device associated with given service on a pci_dev
479  */
pcie_port_find_device(struct pci_dev * dev,u32 service)480 struct device *pcie_port_find_device(struct pci_dev *dev,
481 				      u32 service)
482 {
483 	struct device *device;
484 	struct portdrv_service_data pdrvs;
485 
486 	pdrvs.dev = NULL;
487 	pdrvs.service = service;
488 	device_for_each_child(&dev->dev, &pdrvs, find_service_iter);
489 
490 	device = pdrvs.dev;
491 	return device;
492 }
493 EXPORT_SYMBOL_GPL(pcie_port_find_device);
494 
495 /**
496  * pcie_port_device_remove - unregister PCI Express port service devices
497  * @dev: PCI Express port the service devices to unregister are associated with
498  *
499  * Remove PCI Express port service devices associated with given port and
500  * disable MSI-X or MSI for the port.
501  */
pcie_port_device_remove(struct pci_dev * dev)502 static void pcie_port_device_remove(struct pci_dev *dev)
503 {
504 	device_for_each_child(&dev->dev, NULL, remove_iter);
505 	pci_free_irq_vectors(dev);
506 }
507 
508 /**
509  * pcie_port_probe_service - probe driver for given PCI Express port service
510  * @dev: PCI Express port service device to probe against
511  *
512  * If PCI Express port service driver is registered with
513  * pcie_port_service_register(), this function will be called by the driver core
514  * whenever match is found between the driver and a port service device.
515  */
pcie_port_probe_service(struct device * dev)516 static int pcie_port_probe_service(struct device *dev)
517 {
518 	struct pcie_device *pciedev;
519 	struct pcie_port_service_driver *driver;
520 	int status;
521 
522 	if (!dev || !dev->driver)
523 		return -ENODEV;
524 
525 	driver = to_service_driver(dev->driver);
526 	if (!driver || !driver->probe)
527 		return -ENODEV;
528 
529 	pciedev = to_pcie_device(dev);
530 	status = driver->probe(pciedev);
531 	if (status)
532 		return status;
533 
534 	get_device(dev);
535 	return 0;
536 }
537 
538 /**
539  * pcie_port_remove_service - detach driver from given PCI Express port service
540  * @dev: PCI Express port service device to handle
541  *
542  * If PCI Express port service driver is registered with
543  * pcie_port_service_register(), this function will be called by the driver core
544  * when device_unregister() is called for the port service device associated
545  * with the driver.
546  */
pcie_port_remove_service(struct device * dev)547 static int pcie_port_remove_service(struct device *dev)
548 {
549 	struct pcie_device *pciedev;
550 	struct pcie_port_service_driver *driver;
551 
552 	if (!dev || !dev->driver)
553 		return 0;
554 
555 	pciedev = to_pcie_device(dev);
556 	driver = to_service_driver(dev->driver);
557 	if (driver && driver->remove) {
558 		driver->remove(pciedev);
559 		put_device(dev);
560 	}
561 	return 0;
562 }
563 
564 /**
565  * pcie_port_shutdown_service - shut down given PCI Express port service
566  * @dev: PCI Express port service device to handle
567  *
568  * If PCI Express port service driver is registered with
569  * pcie_port_service_register(), this function will be called by the driver core
570  * when device_shutdown() is called for the port service device associated
571  * with the driver.
572  */
pcie_port_shutdown_service(struct device * dev)573 static void pcie_port_shutdown_service(struct device *dev) {}
574 
575 /**
576  * pcie_port_service_register - register PCI Express port service driver
577  * @new: PCI Express port service driver to register
578  */
pcie_port_service_register(struct pcie_port_service_driver * new)579 int pcie_port_service_register(struct pcie_port_service_driver *new)
580 {
581 	if (pcie_ports_disabled)
582 		return -ENODEV;
583 
584 	new->driver.name = new->name;
585 	new->driver.bus = &pcie_port_bus_type;
586 	new->driver.probe = pcie_port_probe_service;
587 	new->driver.remove = pcie_port_remove_service;
588 	new->driver.shutdown = pcie_port_shutdown_service;
589 
590 	return driver_register(&new->driver);
591 }
592 
593 /**
594  * pcie_port_service_unregister - unregister PCI Express port service driver
595  * @drv: PCI Express port service driver to unregister
596  */
pcie_port_service_unregister(struct pcie_port_service_driver * drv)597 void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
598 {
599 	driver_unregister(&drv->driver);
600 }
601 
602 /* If this switch is set, PCIe port native services should not be enabled. */
603 bool pcie_ports_disabled;
604 
605 /*
606  * If the user specified "pcie_ports=native", use the PCIe services regardless
607  * of whether the platform has given us permission.  On ACPI systems, this
608  * means we ignore _OSC.
609  */
610 bool pcie_ports_native;
611 
612 /*
613  * If the user specified "pcie_ports=dpc-native", use the Linux DPC PCIe
614  * service even if the platform hasn't given us permission.
615  */
616 bool pcie_ports_dpc_native;
617 
pcie_port_setup(char * str)618 static int __init pcie_port_setup(char *str)
619 {
620 	if (!strncmp(str, "compat", 6))
621 		pcie_ports_disabled = true;
622 	else if (!strncmp(str, "native", 6))
623 		pcie_ports_native = true;
624 	else if (!strncmp(str, "dpc-native", 10))
625 		pcie_ports_dpc_native = true;
626 
627 	return 1;
628 }
629 __setup("pcie_ports=", pcie_port_setup);
630 
631 /* global data */
632 
633 #ifdef CONFIG_PM
pcie_port_runtime_suspend(struct device * dev)634 static int pcie_port_runtime_suspend(struct device *dev)
635 {
636 	if (!to_pci_dev(dev)->bridge_d3)
637 		return -EBUSY;
638 
639 	return pcie_port_device_runtime_suspend(dev);
640 }
641 
pcie_port_runtime_idle(struct device * dev)642 static int pcie_port_runtime_idle(struct device *dev)
643 {
644 	/*
645 	 * Assume the PCI core has set bridge_d3 whenever it thinks the port
646 	 * should be good to go to D3.  Everything else, including moving
647 	 * the port to D3, is handled by the PCI core.
648 	 */
649 	return to_pci_dev(dev)->bridge_d3 ? 0 : -EBUSY;
650 }
651 
652 static const struct dev_pm_ops pcie_portdrv_pm_ops = {
653 	.suspend	= pcie_port_device_suspend,
654 	.resume_noirq	= pcie_port_device_resume_noirq,
655 	.resume		= pcie_port_device_resume,
656 	.freeze		= pcie_port_device_suspend,
657 	.thaw		= pcie_port_device_resume,
658 	.poweroff	= pcie_port_device_suspend,
659 	.restore_noirq	= pcie_port_device_resume_noirq,
660 	.restore	= pcie_port_device_resume,
661 	.runtime_suspend = pcie_port_runtime_suspend,
662 	.runtime_resume	= pcie_port_device_runtime_resume,
663 	.runtime_idle	= pcie_port_runtime_idle,
664 };
665 
666 #define PCIE_PORTDRV_PM_OPS	(&pcie_portdrv_pm_ops)
667 
668 #else /* !PM */
669 
670 #define PCIE_PORTDRV_PM_OPS	NULL
671 #endif /* !PM */
672 
673 /*
674  * pcie_portdrv_probe - Probe PCI-Express port devices
675  * @dev: PCI-Express port device being probed
676  *
677  * If detected invokes the pcie_port_device_register() method for
678  * this port device.
679  *
680  */
pcie_portdrv_probe(struct pci_dev * dev,const struct pci_device_id * id)681 static int pcie_portdrv_probe(struct pci_dev *dev,
682 					const struct pci_device_id *id)
683 {
684 	int type = pci_pcie_type(dev);
685 	int status;
686 
687 	if (!pci_is_pcie(dev) ||
688 	    ((type != PCI_EXP_TYPE_ROOT_PORT) &&
689 	     (type != PCI_EXP_TYPE_UPSTREAM) &&
690 	     (type != PCI_EXP_TYPE_DOWNSTREAM) &&
691 	     (type != PCI_EXP_TYPE_RC_EC)))
692 		return -ENODEV;
693 
694 	if (type == PCI_EXP_TYPE_RC_EC)
695 		pcie_link_rcec(dev);
696 
697 	status = pcie_port_device_register(dev);
698 	if (status)
699 		return status;
700 
701 	pci_save_state(dev);
702 
703 	dev_pm_set_driver_flags(&dev->dev, DPM_FLAG_NO_DIRECT_COMPLETE |
704 					   DPM_FLAG_SMART_SUSPEND);
705 
706 	if (pci_bridge_d3_possible(dev)) {
707 		/*
708 		 * Keep the port resumed 100ms to make sure things like
709 		 * config space accesses from userspace (lspci) will not
710 		 * cause the port to repeatedly suspend and resume.
711 		 */
712 		pm_runtime_set_autosuspend_delay(&dev->dev, 100);
713 		pm_runtime_use_autosuspend(&dev->dev);
714 		pm_runtime_mark_last_busy(&dev->dev);
715 		pm_runtime_put_autosuspend(&dev->dev);
716 		pm_runtime_allow(&dev->dev);
717 	}
718 
719 	return 0;
720 }
721 
pcie_portdrv_remove(struct pci_dev * dev)722 static void pcie_portdrv_remove(struct pci_dev *dev)
723 {
724 	if (pci_bridge_d3_possible(dev)) {
725 		pm_runtime_forbid(&dev->dev);
726 		pm_runtime_get_noresume(&dev->dev);
727 		pm_runtime_dont_use_autosuspend(&dev->dev);
728 	}
729 
730 	pcie_port_device_remove(dev);
731 
732 	pci_disable_device(dev);
733 }
734 
pcie_portdrv_shutdown(struct pci_dev * dev)735 static void pcie_portdrv_shutdown(struct pci_dev *dev)
736 {
737 	if (pci_bridge_d3_possible(dev)) {
738 		pm_runtime_forbid(&dev->dev);
739 		pm_runtime_get_noresume(&dev->dev);
740 		pm_runtime_dont_use_autosuspend(&dev->dev);
741 	}
742 
743 	pcie_port_device_remove(dev);
744 }
745 
pcie_portdrv_error_detected(struct pci_dev * dev,pci_channel_state_t error)746 static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
747 					pci_channel_state_t error)
748 {
749 	if (error == pci_channel_io_frozen)
750 		return PCI_ERS_RESULT_NEED_RESET;
751 	return PCI_ERS_RESULT_CAN_RECOVER;
752 }
753 
pcie_portdrv_slot_reset(struct pci_dev * dev)754 static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
755 {
756 	size_t off = offsetof(struct pcie_port_service_driver, slot_reset);
757 	device_for_each_child(&dev->dev, &off, pcie_port_device_iter);
758 
759 	pci_restore_state(dev);
760 	pci_save_state(dev);
761 	return PCI_ERS_RESULT_RECOVERED;
762 }
763 
pcie_portdrv_mmio_enabled(struct pci_dev * dev)764 static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
765 {
766 	return PCI_ERS_RESULT_RECOVERED;
767 }
768 
769 /*
770  * LINUX Device Driver Model
771  */
772 static const struct pci_device_id port_pci_ids[] = {
773 	/* handle any PCI-Express port */
774 	{ PCI_DEVICE_CLASS(PCI_CLASS_BRIDGE_PCI_NORMAL, ~0) },
775 	/* subtractive decode PCI-to-PCI bridge, class type is 060401h */
776 	{ PCI_DEVICE_CLASS(PCI_CLASS_BRIDGE_PCI_SUBTRACTIVE, ~0) },
777 	/* handle any Root Complex Event Collector */
778 	{ PCI_DEVICE_CLASS(((PCI_CLASS_SYSTEM_RCEC << 8) | 0x00), ~0) },
779 	{ },
780 };
781 
782 static const struct pci_error_handlers pcie_portdrv_err_handler = {
783 	.error_detected = pcie_portdrv_error_detected,
784 	.slot_reset = pcie_portdrv_slot_reset,
785 	.mmio_enabled = pcie_portdrv_mmio_enabled,
786 };
787 
788 static struct pci_driver pcie_portdriver = {
789 	.name		= "pcieport",
790 	.id_table	= &port_pci_ids[0],
791 
792 	.probe		= pcie_portdrv_probe,
793 	.remove		= pcie_portdrv_remove,
794 	.shutdown	= pcie_portdrv_shutdown,
795 
796 	.err_handler	= &pcie_portdrv_err_handler,
797 
798 	.driver_managed_dma = true,
799 
800 	.driver.pm	= PCIE_PORTDRV_PM_OPS,
801 };
802 
dmi_pcie_pme_disable_msi(const struct dmi_system_id * d)803 static int __init dmi_pcie_pme_disable_msi(const struct dmi_system_id *d)
804 {
805 	pr_notice("%s detected: will not use MSI for PCIe PME signaling\n",
806 		  d->ident);
807 	pcie_pme_disable_msi();
808 	return 0;
809 }
810 
811 static const struct dmi_system_id pcie_portdrv_dmi_table[] __initconst = {
812 	/*
813 	 * Boxes that should not use MSI for PCIe PME signaling.
814 	 */
815 	{
816 	 .callback = dmi_pcie_pme_disable_msi,
817 	 .ident = "MSI Wind U-100",
818 	 .matches = {
819 		     DMI_MATCH(DMI_SYS_VENDOR,
820 				"MICRO-STAR INTERNATIONAL CO., LTD"),
821 		     DMI_MATCH(DMI_PRODUCT_NAME, "U-100"),
822 		     },
823 	 },
824 	 {}
825 };
826 
pcie_init_services(void)827 static void __init pcie_init_services(void)
828 {
829 	pcie_aer_init();
830 	pcie_pme_init();
831 	pcie_dpc_init();
832 	pcie_hp_init();
833 }
834 
pcie_portdrv_init(void)835 static int __init pcie_portdrv_init(void)
836 {
837 	if (pcie_ports_disabled)
838 		return -EACCES;
839 
840 	pcie_init_services();
841 	dmi_check_system(pcie_portdrv_dmi_table);
842 
843 	return pci_register_driver(&pcie_portdriver);
844 }
845 device_initcall(pcie_portdrv_init);
846