1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
4 * (C) Copyright 2007 Novell Inc.
5 */
6
7 #include <linux/pci.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/device.h>
11 #include <linux/mempolicy.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/sched/isolation.h>
16 #include <linux/cpu.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/suspend.h>
19 #include <linux/kexec.h>
20 #include <linux/of_device.h>
21 #include <linux/acpi.h>
22 #include <linux/dma-map-ops.h>
23 #include <linux/iommu.h>
24 #include "pci.h"
25 #include "pcie/portdrv.h"
26
27 struct pci_dynid {
28 struct list_head node;
29 struct pci_device_id id;
30 };
31
32 /**
33 * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
34 * @drv: target pci driver
35 * @vendor: PCI vendor ID
36 * @device: PCI device ID
37 * @subvendor: PCI subvendor ID
38 * @subdevice: PCI subdevice ID
39 * @class: PCI class
40 * @class_mask: PCI class mask
41 * @driver_data: private driver data
42 *
43 * Adds a new dynamic pci device ID to this driver and causes the
44 * driver to probe for all devices again. @drv must have been
45 * registered prior to calling this function.
46 *
47 * CONTEXT:
48 * Does GFP_KERNEL allocation.
49 *
50 * RETURNS:
51 * 0 on success, -errno on failure.
52 */
pci_add_dynid(struct pci_driver * drv,unsigned int vendor,unsigned int device,unsigned int subvendor,unsigned int subdevice,unsigned int class,unsigned int class_mask,unsigned long driver_data)53 int pci_add_dynid(struct pci_driver *drv,
54 unsigned int vendor, unsigned int device,
55 unsigned int subvendor, unsigned int subdevice,
56 unsigned int class, unsigned int class_mask,
57 unsigned long driver_data)
58 {
59 struct pci_dynid *dynid;
60
61 dynid = kzalloc_obj(*dynid);
62 if (!dynid)
63 return -ENOMEM;
64
65 dynid->id.vendor = vendor;
66 dynid->id.device = device;
67 dynid->id.subvendor = subvendor;
68 dynid->id.subdevice = subdevice;
69 dynid->id.class = class;
70 dynid->id.class_mask = class_mask;
71 dynid->id.driver_data = driver_data;
72
73 spin_lock(&drv->dynids.lock);
74 list_add_tail(&dynid->node, &drv->dynids.list);
75 spin_unlock(&drv->dynids.lock);
76
77 return driver_attach(&drv->driver);
78 }
79 EXPORT_SYMBOL_GPL(pci_add_dynid);
80
pci_free_dynids(struct pci_driver * drv)81 static void pci_free_dynids(struct pci_driver *drv)
82 {
83 struct pci_dynid *dynid, *n;
84
85 spin_lock(&drv->dynids.lock);
86 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
87 list_del(&dynid->node);
88 kfree(dynid);
89 }
90 spin_unlock(&drv->dynids.lock);
91 }
92
93 /**
94 * pci_match_id - See if a PCI device matches a given pci_id table
95 * @ids: array of PCI device ID structures to search in
96 * @dev: the PCI device structure to match against.
97 *
98 * Used by a driver to check whether a PCI device is in its list of
99 * supported devices. Returns the matching pci_device_id structure or
100 * %NULL if there is no match.
101 *
102 * Deprecated; don't use this as it will not catch any dynamic IDs
103 * that a driver might want to check for.
104 */
pci_match_id(const struct pci_device_id * ids,struct pci_dev * dev)105 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
106 struct pci_dev *dev)
107 {
108 if (ids) {
109 while (ids->vendor || ids->subvendor || ids->class_mask) {
110 if (pci_match_one_device(ids, dev))
111 return ids;
112 ids++;
113 }
114 }
115 return NULL;
116 }
117 EXPORT_SYMBOL(pci_match_id);
118
119 static const struct pci_device_id pci_device_id_any = {
120 .vendor = PCI_ANY_ID,
121 .device = PCI_ANY_ID,
122 .subvendor = PCI_ANY_ID,
123 .subdevice = PCI_ANY_ID,
124 };
125
126 /**
127 * pci_match_device - See if a device matches a driver's list of IDs
128 * @drv: the PCI driver to match against
129 * @dev: the PCI device structure to match against
130 *
131 * Used by a driver to check whether a PCI device is in its list of
132 * supported devices or in the dynids list, which may have been augmented
133 * via the sysfs "new_id" file. Returns the matching pci_device_id
134 * structure or %NULL if there is no match.
135 */
pci_match_device(struct pci_driver * drv,struct pci_dev * dev)136 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
137 struct pci_dev *dev)
138 {
139 struct pci_dynid *dynid;
140 const struct pci_device_id *found_id = NULL, *ids;
141 int ret;
142
143 /* When driver_override is set, only bind to the matching driver */
144 ret = device_match_driver_override(&dev->dev, &drv->driver);
145 if (ret == 0)
146 return NULL;
147
148 /* Look at the dynamic ids first, before the static ones */
149 spin_lock(&drv->dynids.lock);
150 list_for_each_entry(dynid, &drv->dynids.list, node) {
151 if (pci_match_one_device(&dynid->id, dev)) {
152 found_id = &dynid->id;
153 break;
154 }
155 }
156 spin_unlock(&drv->dynids.lock);
157
158 if (found_id)
159 return found_id;
160
161 for (ids = drv->id_table; (found_id = pci_match_id(ids, dev));
162 ids = found_id + 1) {
163 /*
164 * The match table is split based on driver_override.
165 * In case override_only was set, enforce driver_override
166 * matching.
167 */
168 if (found_id->override_only) {
169 if (ret > 0)
170 return found_id;
171 } else {
172 return found_id;
173 }
174 }
175
176 /* driver_override will always match, send a dummy id */
177 if (ret > 0)
178 return &pci_device_id_any;
179 return NULL;
180 }
181
182 /**
183 * new_id_store - sysfs frontend to pci_add_dynid()
184 * @driver: target device driver
185 * @buf: buffer for scanning device ID data
186 * @count: input size
187 *
188 * Allow PCI IDs to be added to an existing driver via sysfs.
189 */
new_id_store(struct device_driver * driver,const char * buf,size_t count)190 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
191 size_t count)
192 {
193 struct pci_driver *pdrv = to_pci_driver(driver);
194 const struct pci_device_id *ids = pdrv->id_table;
195 u32 vendor, device, subvendor = PCI_ANY_ID,
196 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
197 unsigned long driver_data = 0;
198 int fields;
199 int retval = 0;
200
201 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
202 &vendor, &device, &subvendor, &subdevice,
203 &class, &class_mask, &driver_data);
204 if (fields < 2)
205 return -EINVAL;
206
207 if (fields != 7) {
208 struct pci_dev *pdev = kzalloc_obj(*pdev);
209 if (!pdev)
210 return -ENOMEM;
211
212 pdev->vendor = vendor;
213 pdev->device = device;
214 pdev->subsystem_vendor = subvendor;
215 pdev->subsystem_device = subdevice;
216 pdev->class = class;
217
218 if (pci_match_device(pdrv, pdev))
219 retval = -EEXIST;
220
221 kfree(pdev);
222
223 if (retval)
224 return retval;
225 }
226
227 /* Only accept driver_data values that match an existing id_table
228 entry */
229 if (ids) {
230 retval = -EINVAL;
231 while (ids->vendor || ids->subvendor || ids->class_mask) {
232 if (driver_data == ids->driver_data) {
233 retval = 0;
234 break;
235 }
236 ids++;
237 }
238 if (retval) /* No match */
239 return retval;
240 }
241
242 retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
243 class, class_mask, driver_data);
244 if (retval)
245 return retval;
246 return count;
247 }
248 static DRIVER_ATTR_WO(new_id);
249
250 /**
251 * remove_id_store - remove a PCI device ID from this driver
252 * @driver: target device driver
253 * @buf: buffer for scanning device ID data
254 * @count: input size
255 *
256 * Removes a dynamic pci device ID to this driver.
257 */
remove_id_store(struct device_driver * driver,const char * buf,size_t count)258 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
259 size_t count)
260 {
261 struct pci_dynid *dynid, *n;
262 struct pci_driver *pdrv = to_pci_driver(driver);
263 u32 vendor, device, subvendor = PCI_ANY_ID,
264 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
265 int fields;
266 size_t retval = -ENODEV;
267
268 fields = sscanf(buf, "%x %x %x %x %x %x",
269 &vendor, &device, &subvendor, &subdevice,
270 &class, &class_mask);
271 if (fields < 2)
272 return -EINVAL;
273
274 spin_lock(&pdrv->dynids.lock);
275 list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
276 struct pci_device_id *id = &dynid->id;
277 if ((id->vendor == vendor) &&
278 (id->device == device) &&
279 (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
280 (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
281 !((id->class ^ class) & class_mask)) {
282 list_del(&dynid->node);
283 kfree(dynid);
284 retval = count;
285 break;
286 }
287 }
288 spin_unlock(&pdrv->dynids.lock);
289
290 return retval;
291 }
292 static DRIVER_ATTR_WO(remove_id);
293
294 static struct attribute *pci_drv_attrs[] = {
295 &driver_attr_new_id.attr,
296 &driver_attr_remove_id.attr,
297 NULL,
298 };
299 ATTRIBUTE_GROUPS(pci_drv);
300
301 struct drv_dev_and_id {
302 struct pci_driver *drv;
303 struct pci_dev *dev;
304 const struct pci_device_id *id;
305 };
306
local_pci_probe(struct drv_dev_and_id * ddi)307 static int local_pci_probe(struct drv_dev_and_id *ddi)
308 {
309 struct pci_dev *pci_dev = ddi->dev;
310 struct pci_driver *pci_drv = ddi->drv;
311 struct device *dev = &pci_dev->dev;
312 int rc;
313
314 /*
315 * Unbound PCI devices are always put in D0, regardless of
316 * runtime PM status. During probe, the device is set to
317 * active and the usage count is incremented. If the driver
318 * supports runtime PM, it should call pm_runtime_put_noidle(),
319 * or any other runtime PM helper function decrementing the usage
320 * count, in its probe routine and pm_runtime_get_noresume() in
321 * its remove routine.
322 */
323 pm_runtime_get_sync(dev);
324 pci_dev->driver = pci_drv;
325 rc = pci_drv->probe(pci_dev, ddi->id);
326 if (!rc)
327 return rc;
328 if (rc < 0) {
329 pci_dev->driver = NULL;
330 pm_runtime_put_sync(dev);
331 return rc;
332 }
333 /*
334 * Probe function should return < 0 for failure, 0 for success
335 * Treat values > 0 as success, but warn.
336 */
337 pci_warn(pci_dev, "Driver probe function unexpectedly returned %d\n",
338 rc);
339 return 0;
340 }
341
342 static struct workqueue_struct *pci_probe_wq;
343
344 struct pci_probe_arg {
345 struct drv_dev_and_id *ddi;
346 struct work_struct work;
347 int ret;
348 };
349
local_pci_probe_callback(struct work_struct * work)350 static void local_pci_probe_callback(struct work_struct *work)
351 {
352 struct pci_probe_arg *arg = container_of(work, struct pci_probe_arg, work);
353
354 arg->ret = local_pci_probe(arg->ddi);
355 }
356
pci_physfn_is_probed(struct pci_dev * dev)357 static bool pci_physfn_is_probed(struct pci_dev *dev)
358 {
359 #ifdef CONFIG_PCI_IOV
360 return dev->is_virtfn && dev->physfn->is_probed;
361 #else
362 return false;
363 #endif
364 }
365
pci_call_probe(struct pci_driver * drv,struct pci_dev * dev,const struct pci_device_id * id)366 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
367 const struct pci_device_id *id)
368 {
369 int error, node, cpu;
370 struct drv_dev_and_id ddi = { drv, dev, id };
371
372 /*
373 * Execute driver initialization on node where the device is
374 * attached. This way the driver likely allocates its local memory
375 * on the right node.
376 */
377 node = dev_to_node(&dev->dev);
378 dev->is_probed = 1;
379
380 cpu_hotplug_disable();
381 /*
382 * Prevent nesting work_on_cpu() for the case where a Virtual Function
383 * device is probed from work_on_cpu() of the Physical device.
384 */
385 if (node < 0 || node >= MAX_NUMNODES || !node_online(node) ||
386 pci_physfn_is_probed(dev)) {
387 error = local_pci_probe(&ddi);
388 } else {
389 struct pci_probe_arg arg = { .ddi = &ddi };
390
391 INIT_WORK_ONSTACK(&arg.work, local_pci_probe_callback);
392 /*
393 * The target election and the enqueue of the work must be within
394 * the same RCU read side section so that when the workqueue pool
395 * is flushed after a housekeeping cpumask update, further readers
396 * are guaranteed to queue the probing work to the appropriate
397 * targets.
398 */
399 rcu_read_lock();
400 cpu = cpumask_any_and(cpumask_of_node(node),
401 housekeeping_cpumask(HK_TYPE_DOMAIN));
402
403 if (cpu < nr_cpu_ids) {
404 struct workqueue_struct *wq = pci_probe_wq;
405
406 if (WARN_ON_ONCE(!wq))
407 wq = system_percpu_wq;
408 queue_work_on(cpu, wq, &arg.work);
409 rcu_read_unlock();
410 flush_work(&arg.work);
411 error = arg.ret;
412 } else {
413 rcu_read_unlock();
414 error = local_pci_probe(&ddi);
415 }
416
417 destroy_work_on_stack(&arg.work);
418 }
419
420 dev->is_probed = 0;
421 cpu_hotplug_enable();
422 return error;
423 }
424
pci_probe_flush_workqueue(void)425 void pci_probe_flush_workqueue(void)
426 {
427 flush_workqueue(pci_probe_wq);
428 }
429
430 /**
431 * __pci_device_probe - check if a driver wants to claim a specific PCI device
432 * @drv: driver to call to check if it wants the PCI device
433 * @pci_dev: PCI device being probed
434 *
435 * returns 0 on success, else error.
436 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
437 */
__pci_device_probe(struct pci_driver * drv,struct pci_dev * pci_dev)438 static int __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
439 {
440 const struct pci_device_id *id;
441 int error = 0;
442
443 if (drv->probe) {
444 error = -ENODEV;
445
446 id = pci_match_device(drv, pci_dev);
447 if (id)
448 error = pci_call_probe(drv, pci_dev, id);
449 }
450 return error;
451 }
452
453 #ifdef CONFIG_PCI_IOV
pci_device_can_probe(struct pci_dev * pdev)454 static inline bool pci_device_can_probe(struct pci_dev *pdev)
455 {
456 return (!pdev->is_virtfn || pdev->physfn->sriov->drivers_autoprobe ||
457 device_has_driver_override(&pdev->dev));
458 }
459 #else
pci_device_can_probe(struct pci_dev * pdev)460 static inline bool pci_device_can_probe(struct pci_dev *pdev)
461 {
462 return true;
463 }
464 #endif
465
pci_device_probe(struct device * dev)466 static int pci_device_probe(struct device *dev)
467 {
468 int error;
469 struct pci_dev *pci_dev = to_pci_dev(dev);
470 struct pci_driver *drv = to_pci_driver(dev->driver);
471
472 if (!pci_device_can_probe(pci_dev))
473 return -ENODEV;
474
475 pci_assign_irq(pci_dev);
476
477 error = pcibios_alloc_irq(pci_dev);
478 if (error < 0)
479 return error;
480
481 pci_dev_get(pci_dev);
482 error = __pci_device_probe(drv, pci_dev);
483 if (error) {
484 pcibios_free_irq(pci_dev);
485 pci_dev_put(pci_dev);
486 }
487
488 return error;
489 }
490
pci_device_remove(struct device * dev)491 static void pci_device_remove(struct device *dev)
492 {
493 struct pci_dev *pci_dev = to_pci_dev(dev);
494 struct pci_driver *drv = pci_dev->driver;
495
496 if (drv->remove) {
497 pm_runtime_get_sync(dev);
498 /*
499 * If the driver provides a .runtime_idle() callback and it has
500 * started to run already, it may continue to run in parallel
501 * with the code below, so wait until all of the runtime PM
502 * activity has completed.
503 */
504 pm_runtime_barrier(dev);
505 drv->remove(pci_dev);
506 pm_runtime_put_noidle(dev);
507 }
508 pcibios_free_irq(pci_dev);
509 pci_dev->driver = NULL;
510 pci_iov_remove(pci_dev);
511
512 /* Undo the runtime PM settings in local_pci_probe() */
513 pm_runtime_put_sync(dev);
514
515 /*
516 * If the device is still on, set the power state as "unknown",
517 * since it might change by the next time we load the driver.
518 */
519 if (pci_dev->current_state == PCI_D0)
520 pci_dev->current_state = PCI_UNKNOWN;
521
522 /*
523 * We would love to complain here if pci_dev->is_enabled is set, that
524 * the driver should have called pci_disable_device(), but the
525 * unfortunate fact is there are too many odd BIOS and bridge setups
526 * that don't like drivers doing that all of the time.
527 * Oh well, we can dream of sane hardware when we sleep, no matter how
528 * horrible the crap we have to deal with is when we are awake...
529 */
530
531 pci_dev_put(pci_dev);
532 }
533
pci_device_shutdown(struct device * dev)534 static void pci_device_shutdown(struct device *dev)
535 {
536 struct pci_dev *pci_dev = to_pci_dev(dev);
537 struct pci_driver *drv = pci_dev->driver;
538
539 pm_runtime_resume(dev);
540
541 if (drv && drv->shutdown)
542 drv->shutdown(pci_dev);
543
544 /*
545 * If this is a kexec reboot, turn off Bus Master bit on the
546 * device to tell it to not continue to do DMA. Don't touch
547 * devices in D3cold or unknown states.
548 * If it is not a kexec reboot, firmware will hit the PCI
549 * devices with big hammer and stop their DMA any way.
550 */
551 if (kexec_in_progress && (pci_dev->current_state <= PCI_D3hot))
552 pci_clear_master(pci_dev);
553 }
554
555 #ifdef CONFIG_PM_SLEEP
556
557 /* Auxiliary functions used for system resume */
558
559 /**
560 * pci_restore_standard_config - restore standard config registers of PCI device
561 * @pci_dev: PCI device to handle
562 */
pci_restore_standard_config(struct pci_dev * pci_dev)563 static int pci_restore_standard_config(struct pci_dev *pci_dev)
564 {
565 pci_update_current_state(pci_dev, PCI_UNKNOWN);
566
567 if (pci_dev->current_state != PCI_D0) {
568 int error = pci_set_power_state(pci_dev, PCI_D0);
569 if (error)
570 return error;
571 }
572
573 pci_restore_state(pci_dev);
574 pci_pme_restore(pci_dev);
575 return 0;
576 }
577 #endif /* CONFIG_PM_SLEEP */
578
579 #ifdef CONFIG_PM
580
581 /* Auxiliary functions used for system resume and run-time resume */
582
pci_pm_default_resume(struct pci_dev * pci_dev)583 static void pci_pm_default_resume(struct pci_dev *pci_dev)
584 {
585 pci_fixup_device(pci_fixup_resume, pci_dev);
586 pci_enable_wake(pci_dev, PCI_D0, false);
587 }
588
pci_pm_default_resume_early(struct pci_dev * pci_dev)589 static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
590 {
591 pci_pm_power_up_and_verify_state(pci_dev);
592 pci_restore_state(pci_dev);
593 pci_pme_restore(pci_dev);
594 }
595
pci_pm_bridge_power_up_actions(struct pci_dev * pci_dev)596 static void pci_pm_bridge_power_up_actions(struct pci_dev *pci_dev)
597 {
598 int ret;
599
600 ret = pci_bridge_wait_for_secondary_bus(pci_dev, "resume");
601 if (ret) {
602 /*
603 * The downstream link failed to come up, so mark the
604 * devices below as disconnected to make sure we don't
605 * attempt to resume them.
606 */
607 pci_walk_bus(pci_dev->subordinate, pci_dev_set_disconnected,
608 NULL);
609 return;
610 }
611
612 /*
613 * When powering on a bridge from D3cold, the whole hierarchy may be
614 * powered on into D0uninitialized state, resume them to give them a
615 * chance to suspend again
616 */
617 pci_resume_bus(pci_dev->subordinate);
618 }
619
620 #endif /* CONFIG_PM */
621
622 #ifdef CONFIG_PM_SLEEP
623
624 /*
625 * Default "suspend" method for devices that have no driver provided suspend,
626 * or not even a driver at all (second part).
627 */
pci_pm_set_unknown_state(struct pci_dev * pci_dev)628 static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
629 {
630 /*
631 * mark its power state as "unknown", since we don't know if
632 * e.g. the BIOS will change its device state when we suspend.
633 */
634 if (pci_dev->current_state == PCI_D0)
635 pci_dev->current_state = PCI_UNKNOWN;
636 }
637
638 /*
639 * Default "resume" method for devices that have no driver provided resume,
640 * or not even a driver at all (second part).
641 */
pci_pm_reenable_device(struct pci_dev * pci_dev)642 static int pci_pm_reenable_device(struct pci_dev *pci_dev)
643 {
644 int retval;
645
646 /* if the device was enabled before suspend, re-enable */
647 retval = pci_reenable_device(pci_dev);
648 /*
649 * if the device was busmaster before the suspend, make it busmaster
650 * again
651 */
652 if (pci_dev->is_busmaster)
653 pci_set_master(pci_dev);
654
655 return retval;
656 }
657
pci_legacy_suspend(struct device * dev,pm_message_t state)658 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
659 {
660 struct pci_dev *pci_dev = to_pci_dev(dev);
661 struct pci_driver *drv = pci_dev->driver;
662
663 pci_dev->state_saved = false;
664
665 if (drv && drv->suspend) {
666 pci_power_t prev = pci_dev->current_state;
667 int error;
668
669 error = drv->suspend(pci_dev, state);
670 suspend_report_result(dev, drv->suspend, error);
671 if (error)
672 return error;
673
674 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
675 && pci_dev->current_state != PCI_UNKNOWN) {
676 pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
677 "PCI PM: Device state not saved by %pS\n",
678 drv->suspend);
679 }
680 }
681
682 pci_fixup_device(pci_fixup_suspend, pci_dev);
683
684 return 0;
685 }
686
pci_legacy_suspend_late(struct device * dev)687 static int pci_legacy_suspend_late(struct device *dev)
688 {
689 struct pci_dev *pci_dev = to_pci_dev(dev);
690
691 if (!pci_dev->state_saved)
692 pci_save_state(pci_dev);
693
694 pci_pm_set_unknown_state(pci_dev);
695
696 pci_fixup_device(pci_fixup_suspend_late, pci_dev);
697
698 return 0;
699 }
700
pci_legacy_resume(struct device * dev)701 static int pci_legacy_resume(struct device *dev)
702 {
703 struct pci_dev *pci_dev = to_pci_dev(dev);
704 struct pci_driver *drv = pci_dev->driver;
705
706 pci_fixup_device(pci_fixup_resume, pci_dev);
707
708 return drv && drv->resume ?
709 drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
710 }
711
712 /* Auxiliary functions used by the new power management framework */
713
pci_pm_default_suspend(struct pci_dev * pci_dev)714 static void pci_pm_default_suspend(struct pci_dev *pci_dev)
715 {
716 /* Disable non-bridge devices without PM support */
717 if (!pci_has_subordinate(pci_dev))
718 pci_disable_enabled_device(pci_dev);
719 }
720
pci_has_legacy_pm_support(struct pci_dev * pci_dev)721 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
722 {
723 struct pci_driver *drv = pci_dev->driver;
724 bool ret = drv && (drv->suspend || drv->resume);
725
726 /*
727 * Legacy PM support is used by default, so warn if the new framework is
728 * supported as well. Drivers are supposed to support either the
729 * former, or the latter, but not both at the same time.
730 */
731 pci_WARN(pci_dev, ret && drv->driver.pm, "device %04x:%04x\n",
732 pci_dev->vendor, pci_dev->device);
733
734 return ret;
735 }
736
737 /* New power management framework */
738
pci_pm_prepare(struct device * dev)739 static int pci_pm_prepare(struct device *dev)
740 {
741 struct pci_dev *pci_dev = to_pci_dev(dev);
742 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
743
744 dev_pm_set_strict_midlayer(dev, true);
745
746 if (pm && pm->prepare) {
747 int error = pm->prepare(dev);
748 if (error < 0)
749 return error;
750
751 if (!error && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
752 return 0;
753 }
754 if (pci_dev_need_resume(pci_dev))
755 return 0;
756
757 /*
758 * The PME setting needs to be adjusted here in case the direct-complete
759 * optimization is used with respect to this device.
760 */
761 pci_dev_adjust_pme(pci_dev);
762 return 1;
763 }
764
pci_pm_complete(struct device * dev)765 static void pci_pm_complete(struct device *dev)
766 {
767 struct pci_dev *pci_dev = to_pci_dev(dev);
768
769 pci_dev_complete_resume(pci_dev);
770 pm_generic_complete(dev);
771
772 /* Resume device if platform firmware has put it in reset-power-on */
773 if (pm_runtime_suspended(dev) && pm_resume_via_firmware()) {
774 pci_power_t pre_sleep_state = pci_dev->current_state;
775
776 pci_refresh_power_state(pci_dev);
777 /*
778 * On platforms with ACPI this check may also trigger for
779 * devices sharing power resources if one of those power
780 * resources has been activated as a result of a change of the
781 * power state of another device sharing it. However, in that
782 * case it is also better to resume the device, in general.
783 */
784 if (pci_dev->current_state < pre_sleep_state)
785 pm_request_resume(dev);
786 }
787
788 dev_pm_set_strict_midlayer(dev, false);
789 }
790
791 #else /* !CONFIG_PM_SLEEP */
792
793 #define pci_pm_prepare NULL
794 #define pci_pm_complete NULL
795
796 #endif /* !CONFIG_PM_SLEEP */
797
798 #ifdef CONFIG_SUSPEND
pcie_pme_root_status_cleanup(struct pci_dev * pci_dev)799 static void pcie_pme_root_status_cleanup(struct pci_dev *pci_dev)
800 {
801 /*
802 * Some BIOSes forget to clear Root PME Status bits after system
803 * wakeup, which breaks ACPI-based runtime wakeup on PCI Express.
804 * Clear those bits now just in case (shouldn't hurt).
805 */
806 if (pci_is_pcie(pci_dev) &&
807 (pci_pcie_type(pci_dev) == PCI_EXP_TYPE_ROOT_PORT ||
808 pci_pcie_type(pci_dev) == PCI_EXP_TYPE_RC_EC))
809 pcie_clear_root_pme_status(pci_dev);
810 }
811
pci_pm_suspend(struct device * dev)812 static int pci_pm_suspend(struct device *dev)
813 {
814 struct pci_dev *pci_dev = to_pci_dev(dev);
815 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
816
817 pci_dev->skip_bus_pm = false;
818
819 /*
820 * Disabling PTM allows some systems, e.g., Intel mobile chips
821 * since Coffee Lake, to enter a lower-power PM state.
822 */
823 pci_suspend_ptm(pci_dev);
824
825 if (pci_has_legacy_pm_support(pci_dev))
826 return pci_legacy_suspend(dev, PMSG_SUSPEND);
827
828 if (!pm) {
829 pci_pm_default_suspend(pci_dev);
830 return 0;
831 }
832
833 /*
834 * PCI devices suspended at run time may need to be resumed at this
835 * point, because in general it may be necessary to reconfigure them for
836 * system suspend. Namely, if the device is expected to wake up the
837 * system from the sleep state, it may have to be reconfigured for this
838 * purpose, or if the device is not expected to wake up the system from
839 * the sleep state, it should be prevented from signaling wakeup events
840 * going forward.
841 *
842 * Also if the driver of the device does not indicate that its system
843 * suspend callbacks can cope with runtime-suspended devices, it is
844 * better to resume the device from runtime suspend here.
845 */
846 if (!dev_pm_smart_suspend(dev) || pci_dev_need_resume(pci_dev)) {
847 pm_runtime_resume(dev);
848 pci_dev->state_saved = false;
849 } else {
850 pci_dev_adjust_pme(pci_dev);
851 }
852
853 if (pm->suspend) {
854 pci_power_t prev = pci_dev->current_state;
855 int error;
856
857 error = pm->suspend(dev);
858 suspend_report_result(dev, pm->suspend, error);
859 if (error)
860 return error;
861
862 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
863 && pci_dev->current_state != PCI_UNKNOWN) {
864 pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
865 "PCI PM: State of device not saved by %pS\n",
866 pm->suspend);
867 }
868 }
869
870 return 0;
871 }
872
pci_pm_suspend_late(struct device * dev)873 static int pci_pm_suspend_late(struct device *dev)
874 {
875 if (dev_pm_skip_suspend(dev))
876 return 0;
877
878 pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
879
880 return pm_generic_suspend_late(dev);
881 }
882
pci_pm_suspend_noirq(struct device * dev)883 static int pci_pm_suspend_noirq(struct device *dev)
884 {
885 struct pci_dev *pci_dev = to_pci_dev(dev);
886 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
887
888 if (dev_pm_skip_suspend(dev))
889 return 0;
890
891 if (pci_has_legacy_pm_support(pci_dev))
892 return pci_legacy_suspend_late(dev);
893
894 if (!pm) {
895 pci_save_state(pci_dev);
896 goto Fixup;
897 }
898
899 if (pm->suspend_noirq) {
900 pci_power_t prev = pci_dev->current_state;
901 int error;
902
903 error = pm->suspend_noirq(dev);
904 suspend_report_result(dev, pm->suspend_noirq, error);
905 if (error)
906 return error;
907
908 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
909 && pci_dev->current_state != PCI_UNKNOWN) {
910 pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
911 "PCI PM: State of device not saved by %pS\n",
912 pm->suspend_noirq);
913 goto Fixup;
914 }
915 }
916
917 if (!pci_dev->state_saved) {
918 pci_save_state(pci_dev);
919
920 /*
921 * If the device is a bridge with a child in D0 below it,
922 * it needs to stay in D0, so check skip_bus_pm to avoid
923 * putting it into a low-power state in that case.
924 */
925 if (!pci_dev->skip_bus_pm && pci_power_manageable(pci_dev))
926 pci_prepare_to_sleep(pci_dev);
927 }
928
929 pci_dbg(pci_dev, "PCI PM: Suspend power state: %s\n",
930 pci_power_name(pci_dev->current_state));
931
932 if (pci_dev->current_state == PCI_D0) {
933 pci_dev->skip_bus_pm = true;
934 /*
935 * Per PCI PM r1.2, table 6-1, a bridge must be in D0 if any
936 * downstream device is in D0, so avoid changing the power state
937 * of the parent bridge by setting the skip_bus_pm flag for it.
938 */
939 if (pci_dev->bus->self)
940 pci_dev->bus->self->skip_bus_pm = true;
941 }
942
943 if (pci_dev->skip_bus_pm && pm_suspend_no_platform()) {
944 pci_dbg(pci_dev, "PCI PM: Skipped\n");
945 goto Fixup;
946 }
947
948 pci_pm_set_unknown_state(pci_dev);
949
950 /*
951 * Some BIOSes from ASUS have a bug: If a USB EHCI host controller's
952 * PCI COMMAND register isn't 0, the BIOS assumes that the controller
953 * hasn't been quiesced and tries to turn it off. If the controller
954 * is already in D3, this can hang or cause memory corruption.
955 *
956 * Since the value of the COMMAND register doesn't matter once the
957 * device has been suspended, we can safely set it to 0 here.
958 */
959 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
960 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
961
962 Fixup:
963 pci_fixup_device(pci_fixup_suspend_late, pci_dev);
964
965 /*
966 * If the target system sleep state is suspend-to-idle, it is sufficient
967 * to check whether or not the device's wakeup settings are good for
968 * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
969 * pci_pm_complete() to take care of fixing up the device's state
970 * anyway, if need be.
971 */
972 if (device_can_wakeup(dev) && !device_may_wakeup(dev))
973 dev->power.may_skip_resume = false;
974
975 return 0;
976 }
977
pci_pm_resume_noirq(struct device * dev)978 static int pci_pm_resume_noirq(struct device *dev)
979 {
980 struct pci_dev *pci_dev = to_pci_dev(dev);
981 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
982 pci_power_t prev_state = pci_dev->current_state;
983 bool skip_bus_pm = pci_dev->skip_bus_pm;
984
985 if (dev_pm_skip_resume(dev))
986 return 0;
987
988 /*
989 * In the suspend-to-idle case, devices left in D0 during suspend will
990 * stay in D0, so it is not necessary to restore or update their
991 * configuration here and attempting to put them into D0 again is
992 * pointless, so avoid doing that.
993 */
994 if (!(skip_bus_pm && pm_suspend_no_platform()))
995 pci_pm_default_resume_early(pci_dev);
996
997 pci_fixup_device(pci_fixup_resume_early, pci_dev);
998 pcie_pme_root_status_cleanup(pci_dev);
999
1000 if (!skip_bus_pm && prev_state == PCI_D3cold)
1001 pci_pm_bridge_power_up_actions(pci_dev);
1002
1003 if (pci_has_legacy_pm_support(pci_dev))
1004 return 0;
1005
1006 if (pm && pm->resume_noirq)
1007 return pm->resume_noirq(dev);
1008
1009 return 0;
1010 }
1011
pci_pm_resume_early(struct device * dev)1012 static int pci_pm_resume_early(struct device *dev)
1013 {
1014 if (dev_pm_skip_resume(dev))
1015 return 0;
1016
1017 return pm_generic_resume_early(dev);
1018 }
1019
pci_pm_resume(struct device * dev)1020 static int pci_pm_resume(struct device *dev)
1021 {
1022 struct pci_dev *pci_dev = to_pci_dev(dev);
1023 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1024
1025 /*
1026 * This is necessary for the suspend error path in which resume is
1027 * called without restoring the standard config registers of the device.
1028 */
1029 if (pci_dev->state_saved)
1030 pci_restore_standard_config(pci_dev);
1031
1032 pci_resume_ptm(pci_dev);
1033
1034 if (pci_has_legacy_pm_support(pci_dev))
1035 return pci_legacy_resume(dev);
1036
1037 pci_pm_default_resume(pci_dev);
1038
1039 if (pm) {
1040 if (pm->resume)
1041 return pm->resume(dev);
1042 } else {
1043 pci_pm_reenable_device(pci_dev);
1044 }
1045
1046 return 0;
1047 }
1048
1049 #else /* !CONFIG_SUSPEND */
1050
1051 #define pci_pm_suspend NULL
1052 #define pci_pm_suspend_late NULL
1053 #define pci_pm_suspend_noirq NULL
1054 #define pci_pm_resume NULL
1055 #define pci_pm_resume_early NULL
1056 #define pci_pm_resume_noirq NULL
1057
1058 #endif /* !CONFIG_SUSPEND */
1059
1060 #ifdef CONFIG_HIBERNATE_CALLBACKS
1061
pci_pm_freeze(struct device * dev)1062 static int pci_pm_freeze(struct device *dev)
1063 {
1064 struct pci_dev *pci_dev = to_pci_dev(dev);
1065 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1066
1067 if (pci_has_legacy_pm_support(pci_dev))
1068 return pci_legacy_suspend(dev, PMSG_FREEZE);
1069
1070 if (!pm) {
1071 pci_pm_default_suspend(pci_dev);
1072 if (!pm_runtime_suspended(dev))
1073 pci_dev->state_saved = false;
1074 return 0;
1075 }
1076
1077 /*
1078 * Resume all runtime-suspended devices before creating a snapshot
1079 * image of system memory, because the restore kernel generally cannot
1080 * be expected to always handle them consistently and they need to be
1081 * put into the runtime-active metastate during system resume anyway,
1082 * so it is better to ensure that the state saved in the image will be
1083 * always consistent with that.
1084 */
1085 pm_runtime_resume(dev);
1086 pci_dev->state_saved = false;
1087
1088 if (pm->freeze) {
1089 int error;
1090
1091 error = pm->freeze(dev);
1092 suspend_report_result(dev, pm->freeze, error);
1093 if (error)
1094 return error;
1095 }
1096
1097 return 0;
1098 }
1099
pci_pm_freeze_noirq(struct device * dev)1100 static int pci_pm_freeze_noirq(struct device *dev)
1101 {
1102 struct pci_dev *pci_dev = to_pci_dev(dev);
1103 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1104
1105 if (pci_has_legacy_pm_support(pci_dev))
1106 return pci_legacy_suspend_late(dev);
1107
1108 if (pm && pm->freeze_noirq) {
1109 int error;
1110
1111 error = pm->freeze_noirq(dev);
1112 suspend_report_result(dev, pm->freeze_noirq, error);
1113 if (error)
1114 return error;
1115 }
1116
1117 if (!pci_dev->state_saved)
1118 pci_save_state(pci_dev);
1119
1120 pci_pm_set_unknown_state(pci_dev);
1121
1122 return 0;
1123 }
1124
pci_pm_thaw_noirq(struct device * dev)1125 static int pci_pm_thaw_noirq(struct device *dev)
1126 {
1127 struct pci_dev *pci_dev = to_pci_dev(dev);
1128 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1129
1130 /*
1131 * The pm->thaw_noirq() callback assumes the device has been
1132 * returned to D0 and its config state has been restored.
1133 *
1134 * In addition, pci_restore_state() restores MSI-X state in MMIO
1135 * space, which requires the device to be in D0, so return it to D0
1136 * in case the driver's "freeze" callbacks put it into a low-power
1137 * state.
1138 */
1139 pci_pm_power_up_and_verify_state(pci_dev);
1140 pci_restore_state(pci_dev);
1141
1142 if (pci_has_legacy_pm_support(pci_dev))
1143 return 0;
1144
1145 if (pm && pm->thaw_noirq)
1146 return pm->thaw_noirq(dev);
1147
1148 return 0;
1149 }
1150
pci_pm_thaw(struct device * dev)1151 static int pci_pm_thaw(struct device *dev)
1152 {
1153 struct pci_dev *pci_dev = to_pci_dev(dev);
1154 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1155 int error = 0;
1156
1157 if (pci_has_legacy_pm_support(pci_dev))
1158 return pci_legacy_resume(dev);
1159
1160 if (pm) {
1161 if (pm->thaw)
1162 error = pm->thaw(dev);
1163 } else {
1164 pci_pm_reenable_device(pci_dev);
1165 }
1166
1167 return error;
1168 }
1169
pci_pm_poweroff(struct device * dev)1170 static int pci_pm_poweroff(struct device *dev)
1171 {
1172 struct pci_dev *pci_dev = to_pci_dev(dev);
1173 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1174
1175 if (pci_has_legacy_pm_support(pci_dev))
1176 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
1177
1178 if (!pm) {
1179 pci_pm_default_suspend(pci_dev);
1180 return 0;
1181 }
1182
1183 /* The reason to do that is the same as in pci_pm_suspend(). */
1184 if (!dev_pm_smart_suspend(dev) || pci_dev_need_resume(pci_dev)) {
1185 pm_runtime_resume(dev);
1186 pci_dev->state_saved = false;
1187 } else {
1188 pci_dev_adjust_pme(pci_dev);
1189 }
1190
1191 if (pm->poweroff) {
1192 int error;
1193
1194 error = pm->poweroff(dev);
1195 suspend_report_result(dev, pm->poweroff, error);
1196 if (error)
1197 return error;
1198 }
1199
1200 return 0;
1201 }
1202
pci_pm_poweroff_late(struct device * dev)1203 static int pci_pm_poweroff_late(struct device *dev)
1204 {
1205 if (dev_pm_skip_suspend(dev))
1206 return 0;
1207
1208 pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
1209
1210 return pm_generic_poweroff_late(dev);
1211 }
1212
pci_pm_poweroff_noirq(struct device * dev)1213 static int pci_pm_poweroff_noirq(struct device *dev)
1214 {
1215 struct pci_dev *pci_dev = to_pci_dev(dev);
1216 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1217
1218 if (dev_pm_skip_suspend(dev))
1219 return 0;
1220
1221 if (pci_has_legacy_pm_support(pci_dev))
1222 return pci_legacy_suspend_late(dev);
1223
1224 if (!pm) {
1225 pci_fixup_device(pci_fixup_suspend_late, pci_dev);
1226 return 0;
1227 }
1228
1229 if (pm->poweroff_noirq) {
1230 int error;
1231
1232 error = pm->poweroff_noirq(dev);
1233 suspend_report_result(dev, pm->poweroff_noirq, error);
1234 if (error)
1235 return error;
1236 }
1237
1238 if (!pci_dev->state_saved && !pci_has_subordinate(pci_dev))
1239 pci_prepare_to_sleep(pci_dev);
1240
1241 /*
1242 * The reason for doing this here is the same as for the analogous code
1243 * in pci_pm_suspend_noirq().
1244 */
1245 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
1246 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
1247
1248 pci_fixup_device(pci_fixup_suspend_late, pci_dev);
1249
1250 return 0;
1251 }
1252
pci_pm_restore_noirq(struct device * dev)1253 static int pci_pm_restore_noirq(struct device *dev)
1254 {
1255 struct pci_dev *pci_dev = to_pci_dev(dev);
1256 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1257
1258 pci_pm_default_resume_early(pci_dev);
1259 pci_fixup_device(pci_fixup_resume_early, pci_dev);
1260
1261 if (pci_has_legacy_pm_support(pci_dev))
1262 return 0;
1263
1264 if (pm && pm->restore_noirq)
1265 return pm->restore_noirq(dev);
1266
1267 return 0;
1268 }
1269
pci_pm_restore(struct device * dev)1270 static int pci_pm_restore(struct device *dev)
1271 {
1272 struct pci_dev *pci_dev = to_pci_dev(dev);
1273 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1274
1275 /*
1276 * This is necessary for the hibernation error path in which restore is
1277 * called without restoring the standard config registers of the device.
1278 */
1279 if (pci_dev->state_saved)
1280 pci_restore_standard_config(pci_dev);
1281
1282 if (pci_has_legacy_pm_support(pci_dev))
1283 return pci_legacy_resume(dev);
1284
1285 pci_pm_default_resume(pci_dev);
1286
1287 if (pm) {
1288 if (pm->restore)
1289 return pm->restore(dev);
1290 } else {
1291 pci_pm_reenable_device(pci_dev);
1292 }
1293
1294 return 0;
1295 }
1296
1297 #else /* !CONFIG_HIBERNATE_CALLBACKS */
1298
1299 #define pci_pm_freeze NULL
1300 #define pci_pm_freeze_noirq NULL
1301 #define pci_pm_thaw NULL
1302 #define pci_pm_thaw_noirq NULL
1303 #define pci_pm_poweroff NULL
1304 #define pci_pm_poweroff_late NULL
1305 #define pci_pm_poweroff_noirq NULL
1306 #define pci_pm_restore NULL
1307 #define pci_pm_restore_noirq NULL
1308
1309 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
1310
1311 #ifdef CONFIG_PM
1312
pci_pm_runtime_suspend(struct device * dev)1313 static int pci_pm_runtime_suspend(struct device *dev)
1314 {
1315 struct pci_dev *pci_dev = to_pci_dev(dev);
1316 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1317 pci_power_t prev = pci_dev->current_state;
1318 int error;
1319
1320 pci_suspend_ptm(pci_dev);
1321
1322 /*
1323 * If pci_dev->driver is not set (unbound), we leave the device in D0,
1324 * but it may go to D3cold when the bridge above it runtime suspends.
1325 * Save its config space in case that happens.
1326 */
1327 if (!pci_dev->driver) {
1328 pci_save_state(pci_dev);
1329 return 0;
1330 }
1331
1332 pci_dev->state_saved = false;
1333 if (pm && pm->runtime_suspend) {
1334 error = pm->runtime_suspend(dev);
1335 /*
1336 * -EBUSY and -EAGAIN is used to request the runtime PM core
1337 * to schedule a new suspend, so log the event only with debug
1338 * log level.
1339 */
1340 if (error == -EBUSY || error == -EAGAIN) {
1341 pci_dbg(pci_dev, "can't suspend now (%ps returned %d)\n",
1342 pm->runtime_suspend, error);
1343 return error;
1344 } else if (error) {
1345 pci_err(pci_dev, "can't suspend (%ps returned %d)\n",
1346 pm->runtime_suspend, error);
1347 return error;
1348 }
1349 }
1350
1351 pci_fixup_device(pci_fixup_suspend, pci_dev);
1352
1353 if (pm && pm->runtime_suspend
1354 && !pci_dev->state_saved && pci_dev->current_state != PCI_D0
1355 && pci_dev->current_state != PCI_UNKNOWN) {
1356 pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
1357 "PCI PM: State of device not saved by %pS\n",
1358 pm->runtime_suspend);
1359 return 0;
1360 }
1361
1362 if (!pci_dev->state_saved) {
1363 pci_save_state(pci_dev);
1364 pci_finish_runtime_suspend(pci_dev);
1365 }
1366
1367 return 0;
1368 }
1369
pci_pm_runtime_resume(struct device * dev)1370 static int pci_pm_runtime_resume(struct device *dev)
1371 {
1372 struct pci_dev *pci_dev = to_pci_dev(dev);
1373 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1374 pci_power_t prev_state = pci_dev->current_state;
1375 int error = 0;
1376
1377 /*
1378 * Restoring config space is necessary even if the device is not bound
1379 * to a driver because although we left it in D0, it may have gone to
1380 * D3cold when the bridge above it runtime suspended.
1381 */
1382 pci_pm_default_resume_early(pci_dev);
1383 pci_resume_ptm(pci_dev);
1384
1385 if (!pci_dev->driver)
1386 return 0;
1387
1388 pci_fixup_device(pci_fixup_resume_early, pci_dev);
1389 pci_pm_default_resume(pci_dev);
1390
1391 if (prev_state == PCI_D3cold)
1392 pci_pm_bridge_power_up_actions(pci_dev);
1393
1394 if (pm && pm->runtime_resume)
1395 error = pm->runtime_resume(dev);
1396
1397 return error;
1398 }
1399
pci_pm_runtime_idle(struct device * dev)1400 static int pci_pm_runtime_idle(struct device *dev)
1401 {
1402 struct pci_dev *pci_dev = to_pci_dev(dev);
1403 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1404
1405 /*
1406 * If pci_dev->driver is not set (unbound), the device should
1407 * always remain in D0 regardless of the runtime PM status
1408 */
1409 if (!pci_dev->driver)
1410 return 0;
1411
1412 if (pm && pm->runtime_idle)
1413 return pm->runtime_idle(dev);
1414
1415 return 0;
1416 }
1417
1418 static const struct dev_pm_ops pci_dev_pm_ops = {
1419 .prepare = pci_pm_prepare,
1420 .complete = pci_pm_complete,
1421 .suspend = pci_pm_suspend,
1422 .suspend_late = pci_pm_suspend_late,
1423 .resume = pci_pm_resume,
1424 .resume_early = pci_pm_resume_early,
1425 .freeze = pci_pm_freeze,
1426 .thaw = pci_pm_thaw,
1427 .poweroff = pci_pm_poweroff,
1428 .poweroff_late = pci_pm_poweroff_late,
1429 .restore = pci_pm_restore,
1430 .suspend_noirq = pci_pm_suspend_noirq,
1431 .resume_noirq = pci_pm_resume_noirq,
1432 .freeze_noirq = pci_pm_freeze_noirq,
1433 .thaw_noirq = pci_pm_thaw_noirq,
1434 .poweroff_noirq = pci_pm_poweroff_noirq,
1435 .restore_noirq = pci_pm_restore_noirq,
1436 .runtime_suspend = pci_pm_runtime_suspend,
1437 .runtime_resume = pci_pm_runtime_resume,
1438 .runtime_idle = pci_pm_runtime_idle,
1439 };
1440
1441 #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
1442
1443 #else /* !CONFIG_PM */
1444
1445 #define pci_pm_runtime_suspend NULL
1446 #define pci_pm_runtime_resume NULL
1447 #define pci_pm_runtime_idle NULL
1448
1449 #define PCI_PM_OPS_PTR NULL
1450
1451 #endif /* !CONFIG_PM */
1452
1453 /**
1454 * __pci_register_driver - register a new pci driver
1455 * @drv: the driver structure to register
1456 * @owner: owner module of drv
1457 * @mod_name: module name string
1458 *
1459 * Adds the driver structure to the list of registered drivers.
1460 * Returns a negative value on error, otherwise 0.
1461 * If no error occurred, the driver remains registered even if
1462 * no device was claimed during registration.
1463 */
__pci_register_driver(struct pci_driver * drv,struct module * owner,const char * mod_name)1464 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1465 const char *mod_name)
1466 {
1467 /* initialize common driver fields */
1468 drv->driver.name = drv->name;
1469 drv->driver.bus = &pci_bus_type;
1470 drv->driver.owner = owner;
1471 drv->driver.mod_name = mod_name;
1472 drv->driver.groups = drv->groups;
1473 drv->driver.dev_groups = drv->dev_groups;
1474
1475 spin_lock_init(&drv->dynids.lock);
1476 INIT_LIST_HEAD(&drv->dynids.list);
1477
1478 /* register with core */
1479 return driver_register(&drv->driver);
1480 }
1481 EXPORT_SYMBOL(__pci_register_driver);
1482
1483 /**
1484 * pci_unregister_driver - unregister a pci driver
1485 * @drv: the driver structure to unregister
1486 *
1487 * Deletes the driver structure from the list of registered PCI drivers,
1488 * gives it a chance to clean up by calling its remove() function for
1489 * each device it was responsible for, and marks those devices as
1490 * driverless.
1491 */
1492
pci_unregister_driver(struct pci_driver * drv)1493 void pci_unregister_driver(struct pci_driver *drv)
1494 {
1495 driver_unregister(&drv->driver);
1496 pci_free_dynids(drv);
1497 }
1498 EXPORT_SYMBOL(pci_unregister_driver);
1499
1500 static struct pci_driver pci_compat_driver = {
1501 .name = "compat"
1502 };
1503
1504 /**
1505 * pci_dev_driver - get the pci_driver of a device
1506 * @dev: the device to query
1507 *
1508 * Returns the appropriate pci_driver structure or %NULL if there is no
1509 * registered driver for the device.
1510 */
pci_dev_driver(const struct pci_dev * dev)1511 struct pci_driver *pci_dev_driver(const struct pci_dev *dev)
1512 {
1513 int i;
1514
1515 if (dev->driver)
1516 return dev->driver;
1517
1518 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1519 if (dev->resource[i].flags & IORESOURCE_BUSY)
1520 return &pci_compat_driver;
1521
1522 return NULL;
1523 }
1524 EXPORT_SYMBOL(pci_dev_driver);
1525
1526 /**
1527 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
1528 * @dev: the PCI device structure to match against
1529 * @drv: the device driver to search for matching PCI device id structures
1530 *
1531 * Used by a driver to check whether a PCI device present in the
1532 * system is in its list of supported devices. Returns the matching
1533 * pci_device_id structure or %NULL if there is no match.
1534 */
pci_bus_match(struct device * dev,const struct device_driver * drv)1535 static int pci_bus_match(struct device *dev, const struct device_driver *drv)
1536 {
1537 struct pci_dev *pci_dev = to_pci_dev(dev);
1538 struct pci_driver *pci_drv;
1539 const struct pci_device_id *found_id;
1540
1541 if (pci_dev_binding_disallowed(pci_dev))
1542 return 0;
1543
1544 pci_drv = (struct pci_driver *)to_pci_driver(drv);
1545 found_id = pci_match_device(pci_drv, pci_dev);
1546 if (found_id)
1547 return 1;
1548
1549 return 0;
1550 }
1551
1552 /**
1553 * pci_dev_get - increments the reference count of the pci device structure
1554 * @dev: the device being referenced
1555 *
1556 * Each live reference to a device should be refcounted.
1557 *
1558 * Drivers for PCI devices should normally record such references in
1559 * their probe() methods, when they bind to a device, and release
1560 * them by calling pci_dev_put(), in their disconnect() methods.
1561 *
1562 * A pointer to the device with the incremented reference counter is returned.
1563 */
pci_dev_get(struct pci_dev * dev)1564 struct pci_dev *pci_dev_get(struct pci_dev *dev)
1565 {
1566 if (dev)
1567 get_device(&dev->dev);
1568 return dev;
1569 }
1570 EXPORT_SYMBOL(pci_dev_get);
1571
1572 /**
1573 * pci_dev_put - release a use of the pci device structure
1574 * @dev: device that's been disconnected
1575 *
1576 * Must be called when a user of a device is finished with it. When the last
1577 * user of the device calls this function, the memory of the device is freed.
1578 */
pci_dev_put(struct pci_dev * dev)1579 void pci_dev_put(struct pci_dev *dev)
1580 {
1581 if (dev)
1582 put_device(&dev->dev);
1583 }
1584 EXPORT_SYMBOL(pci_dev_put);
1585
pci_uevent(const struct device * dev,struct kobj_uevent_env * env)1586 static int pci_uevent(const struct device *dev, struct kobj_uevent_env *env)
1587 {
1588 const struct pci_dev *pdev;
1589
1590 if (!dev)
1591 return -ENODEV;
1592
1593 pdev = to_pci_dev(dev);
1594
1595 if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class))
1596 return -ENOMEM;
1597
1598 if (add_uevent_var(env, "PCI_ID=%04X:%04X", pdev->vendor, pdev->device))
1599 return -ENOMEM;
1600
1601 if (add_uevent_var(env, "PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor,
1602 pdev->subsystem_device))
1603 return -ENOMEM;
1604
1605 if (add_uevent_var(env, "PCI_SLOT_NAME=%s", pci_name(pdev)))
1606 return -ENOMEM;
1607
1608 if (add_uevent_var(env, "MODALIAS=pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X",
1609 pdev->vendor, pdev->device,
1610 pdev->subsystem_vendor, pdev->subsystem_device,
1611 (u8)(pdev->class >> 16), (u8)(pdev->class >> 8),
1612 (u8)(pdev->class)))
1613 return -ENOMEM;
1614
1615 return 0;
1616 }
1617
1618 #if defined(CONFIG_PCIEAER) || defined(CONFIG_EEH) || defined(CONFIG_S390)
1619 /**
1620 * pci_uevent_ers - emit a uevent during recovery path of PCI device
1621 * @pdev: PCI device undergoing error recovery
1622 * @err_type: type of error event
1623 */
pci_uevent_ers(struct pci_dev * pdev,enum pci_ers_result err_type)1624 void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type)
1625 {
1626 int idx = 0;
1627 char *envp[3];
1628
1629 switch (err_type) {
1630 case PCI_ERS_RESULT_NONE:
1631 case PCI_ERS_RESULT_CAN_RECOVER:
1632 case PCI_ERS_RESULT_NEED_RESET:
1633 envp[idx++] = "ERROR_EVENT=BEGIN_RECOVERY";
1634 envp[idx++] = "DEVICE_ONLINE=0";
1635 break;
1636 case PCI_ERS_RESULT_RECOVERED:
1637 envp[idx++] = "ERROR_EVENT=SUCCESSFUL_RECOVERY";
1638 envp[idx++] = "DEVICE_ONLINE=1";
1639 break;
1640 case PCI_ERS_RESULT_DISCONNECT:
1641 envp[idx++] = "ERROR_EVENT=FAILED_RECOVERY";
1642 envp[idx++] = "DEVICE_ONLINE=0";
1643 break;
1644 default:
1645 break;
1646 }
1647
1648 if (idx > 0) {
1649 envp[idx++] = NULL;
1650 kobject_uevent_env(&pdev->dev.kobj, KOBJ_CHANGE, envp);
1651 }
1652 }
1653 #endif
1654
pci_bus_num_vf(struct device * dev)1655 static int pci_bus_num_vf(struct device *dev)
1656 {
1657 return pci_num_vf(to_pci_dev(dev));
1658 }
1659
1660 /**
1661 * pci_dma_configure - Setup DMA configuration
1662 * @dev: ptr to dev structure
1663 *
1664 * Function to update PCI devices's DMA configuration using the same
1665 * info from the OF node or ACPI node of host bridge's parent (if any).
1666 */
pci_dma_configure(struct device * dev)1667 static int pci_dma_configure(struct device *dev)
1668 {
1669 const struct device_driver *drv = READ_ONCE(dev->driver);
1670 struct device *bridge;
1671 int ret = 0;
1672
1673 bridge = pci_get_host_bridge_device(to_pci_dev(dev));
1674
1675 if (IS_ENABLED(CONFIG_OF) && bridge->parent &&
1676 bridge->parent->of_node) {
1677 ret = of_dma_configure(dev, bridge->parent->of_node, true);
1678 } else if (has_acpi_companion(bridge)) {
1679 struct acpi_device *adev = to_acpi_device_node(bridge->fwnode);
1680
1681 ret = acpi_dma_configure(dev, acpi_get_dma_attr(adev));
1682 }
1683
1684 /*
1685 * Attempt to enable ACS regardless of capability because some Root
1686 * Ports (e.g. those quirked with *_intel_pch_acs_*) do not have
1687 * the standard ACS capability but still support ACS via those
1688 * quirks.
1689 */
1690 pci_enable_acs(to_pci_dev(dev));
1691
1692 pci_put_host_bridge_device(bridge);
1693
1694 /* @drv may not be valid when we're called from the IOMMU layer */
1695 if (!ret && drv && !to_pci_driver(drv)->driver_managed_dma) {
1696 ret = iommu_device_use_default_domain(dev);
1697 if (ret)
1698 arch_teardown_dma_ops(dev);
1699 }
1700
1701 return ret;
1702 }
1703
pci_dma_cleanup(struct device * dev)1704 static void pci_dma_cleanup(struct device *dev)
1705 {
1706 struct pci_driver *driver = to_pci_driver(dev->driver);
1707
1708 if (!driver->driver_managed_dma)
1709 iommu_device_unuse_default_domain(dev);
1710 }
1711
1712 /*
1713 * pci_device_irq_get_affinity - get IRQ affinity mask for device
1714 * @dev: ptr to dev structure
1715 * @irq_vec: interrupt vector number
1716 *
1717 * Return the CPU affinity mask for @dev and @irq_vec.
1718 */
pci_device_irq_get_affinity(struct device * dev,unsigned int irq_vec)1719 static const struct cpumask *pci_device_irq_get_affinity(struct device *dev,
1720 unsigned int irq_vec)
1721 {
1722 return pci_irq_get_affinity(to_pci_dev(dev), irq_vec);
1723 }
1724
1725 const struct bus_type pci_bus_type = {
1726 .name = "pci",
1727 .driver_override = true,
1728 .match = pci_bus_match,
1729 .uevent = pci_uevent,
1730 .probe = pci_device_probe,
1731 .remove = pci_device_remove,
1732 .shutdown = pci_device_shutdown,
1733 .irq_get_affinity = pci_device_irq_get_affinity,
1734 .dev_groups = pci_dev_groups,
1735 .bus_groups = pci_bus_groups,
1736 .drv_groups = pci_drv_groups,
1737 .pm = PCI_PM_OPS_PTR,
1738 .num_vf = pci_bus_num_vf,
1739 .dma_configure = pci_dma_configure,
1740 .dma_cleanup = pci_dma_cleanup,
1741 };
1742 EXPORT_SYMBOL(pci_bus_type);
1743
pci_driver_init(void)1744 static int __init pci_driver_init(void)
1745 {
1746 int ret;
1747
1748 pci_probe_wq = alloc_workqueue("sync_wq", WQ_PERCPU, 0);
1749 if (!pci_probe_wq)
1750 return -ENOMEM;
1751
1752 ret = bus_register(&pci_bus_type);
1753 if (ret)
1754 return ret;
1755
1756 #ifdef CONFIG_PCIEPORTBUS
1757 ret = bus_register(&pcie_port_bus_type);
1758 if (ret)
1759 return ret;
1760 #endif
1761 dma_debug_add_bus(&pci_bus_type);
1762 return 0;
1763 }
1764 postcore_initcall(pci_driver_init);
1765