pci-driver.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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. #include <linux/pci.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/device.h>
  10. #include <linux/mempolicy.h>
  11. #include <linux/string.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/cpu.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/suspend.h>
  17. #include <linux/kexec.h>
  18. #include "pci.h"
  19. struct pci_dynid {
  20. struct list_head node;
  21. struct pci_device_id id;
  22. };
  23. /**
  24. * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
  25. * @drv: target pci driver
  26. * @vendor: PCI vendor ID
  27. * @device: PCI device ID
  28. * @subvendor: PCI subvendor ID
  29. * @subdevice: PCI subdevice ID
  30. * @class: PCI class
  31. * @class_mask: PCI class mask
  32. * @driver_data: private driver data
  33. *
  34. * Adds a new dynamic pci device ID to this driver and causes the
  35. * driver to probe for all devices again. @drv must have been
  36. * registered prior to calling this function.
  37. *
  38. * CONTEXT:
  39. * Does GFP_KERNEL allocation.
  40. *
  41. * RETURNS:
  42. * 0 on success, -errno on failure.
  43. */
  44. int pci_add_dynid(struct pci_driver *drv,
  45. unsigned int vendor, unsigned int device,
  46. unsigned int subvendor, unsigned int subdevice,
  47. unsigned int class, unsigned int class_mask,
  48. unsigned long driver_data)
  49. {
  50. struct pci_dynid *dynid;
  51. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  52. if (!dynid)
  53. return -ENOMEM;
  54. dynid->id.vendor = vendor;
  55. dynid->id.device = device;
  56. dynid->id.subvendor = subvendor;
  57. dynid->id.subdevice = subdevice;
  58. dynid->id.class = class;
  59. dynid->id.class_mask = class_mask;
  60. dynid->id.driver_data = driver_data;
  61. spin_lock(&drv->dynids.lock);
  62. list_add_tail(&dynid->node, &drv->dynids.list);
  63. spin_unlock(&drv->dynids.lock);
  64. return driver_attach(&drv->driver);
  65. }
  66. EXPORT_SYMBOL_GPL(pci_add_dynid);
  67. static void pci_free_dynids(struct pci_driver *drv)
  68. {
  69. struct pci_dynid *dynid, *n;
  70. spin_lock(&drv->dynids.lock);
  71. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  72. list_del(&dynid->node);
  73. kfree(dynid);
  74. }
  75. spin_unlock(&drv->dynids.lock);
  76. }
  77. /**
  78. * store_new_id - sysfs frontend to pci_add_dynid()
  79. * @driver: target device driver
  80. * @buf: buffer for scanning device ID data
  81. * @count: input size
  82. *
  83. * Allow PCI IDs to be added to an existing driver via sysfs.
  84. */
  85. static ssize_t new_id_store(struct device_driver *driver, const char *buf,
  86. size_t count)
  87. {
  88. struct pci_driver *pdrv = to_pci_driver(driver);
  89. const struct pci_device_id *ids = pdrv->id_table;
  90. __u32 vendor, device, subvendor = PCI_ANY_ID,
  91. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  92. unsigned long driver_data = 0;
  93. int fields = 0;
  94. int retval = 0;
  95. fields = sscanf(buf, "%x %x %x %x %x %x %lx",
  96. &vendor, &device, &subvendor, &subdevice,
  97. &class, &class_mask, &driver_data);
  98. if (fields < 2)
  99. return -EINVAL;
  100. if (fields != 7) {
  101. struct pci_dev *pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
  102. if (!pdev)
  103. return -ENOMEM;
  104. pdev->vendor = vendor;
  105. pdev->device = device;
  106. pdev->subsystem_vendor = subvendor;
  107. pdev->subsystem_device = subdevice;
  108. pdev->class = class;
  109. if (pci_match_id(pdrv->id_table, pdev))
  110. retval = -EEXIST;
  111. kfree(pdev);
  112. if (retval)
  113. return retval;
  114. }
  115. /* Only accept driver_data values that match an existing id_table
  116. entry */
  117. if (ids) {
  118. retval = -EINVAL;
  119. while (ids->vendor || ids->subvendor || ids->class_mask) {
  120. if (driver_data == ids->driver_data) {
  121. retval = 0;
  122. break;
  123. }
  124. ids++;
  125. }
  126. if (retval) /* No match */
  127. return retval;
  128. }
  129. retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
  130. class, class_mask, driver_data);
  131. if (retval)
  132. return retval;
  133. return count;
  134. }
  135. static DRIVER_ATTR_WO(new_id);
  136. /**
  137. * store_remove_id - remove a PCI device ID from this driver
  138. * @driver: target device driver
  139. * @buf: buffer for scanning device ID data
  140. * @count: input size
  141. *
  142. * Removes a dynamic pci device ID to this driver.
  143. */
  144. static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
  145. size_t count)
  146. {
  147. struct pci_dynid *dynid, *n;
  148. struct pci_driver *pdrv = to_pci_driver(driver);
  149. __u32 vendor, device, subvendor = PCI_ANY_ID,
  150. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  151. int fields = 0;
  152. size_t retval = -ENODEV;
  153. fields = sscanf(buf, "%x %x %x %x %x %x",
  154. &vendor, &device, &subvendor, &subdevice,
  155. &class, &class_mask);
  156. if (fields < 2)
  157. return -EINVAL;
  158. spin_lock(&pdrv->dynids.lock);
  159. list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
  160. struct pci_device_id *id = &dynid->id;
  161. if ((id->vendor == vendor) &&
  162. (id->device == device) &&
  163. (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
  164. (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
  165. !((id->class ^ class) & class_mask)) {
  166. list_del(&dynid->node);
  167. kfree(dynid);
  168. retval = count;
  169. break;
  170. }
  171. }
  172. spin_unlock(&pdrv->dynids.lock);
  173. return retval;
  174. }
  175. static DRIVER_ATTR_WO(remove_id);
  176. static struct attribute *pci_drv_attrs[] = {
  177. &driver_attr_new_id.attr,
  178. &driver_attr_remove_id.attr,
  179. NULL,
  180. };
  181. ATTRIBUTE_GROUPS(pci_drv);
  182. /**
  183. * pci_match_id - See if a pci device matches a given pci_id table
  184. * @ids: array of PCI device id structures to search in
  185. * @dev: the PCI device structure to match against.
  186. *
  187. * Used by a driver to check whether a PCI device present in the
  188. * system is in its list of supported devices. Returns the matching
  189. * pci_device_id structure or %NULL if there is no match.
  190. *
  191. * Deprecated, don't use this as it will not catch any dynamic ids
  192. * that a driver might want to check for.
  193. */
  194. const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
  195. struct pci_dev *dev)
  196. {
  197. if (ids) {
  198. while (ids->vendor || ids->subvendor || ids->class_mask) {
  199. if (pci_match_one_device(ids, dev))
  200. return ids;
  201. ids++;
  202. }
  203. }
  204. return NULL;
  205. }
  206. EXPORT_SYMBOL(pci_match_id);
  207. static const struct pci_device_id pci_device_id_any = {
  208. .vendor = PCI_ANY_ID,
  209. .device = PCI_ANY_ID,
  210. .subvendor = PCI_ANY_ID,
  211. .subdevice = PCI_ANY_ID,
  212. };
  213. /**
  214. * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
  215. * @drv: the PCI driver to match against
  216. * @dev: the PCI device structure to match against
  217. *
  218. * Used by a driver to check whether a PCI device present in the
  219. * system is in its list of supported devices. Returns the matching
  220. * pci_device_id structure or %NULL if there is no match.
  221. */
  222. static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
  223. struct pci_dev *dev)
  224. {
  225. struct pci_dynid *dynid;
  226. const struct pci_device_id *found_id = NULL;
  227. /* When driver_override is set, only bind to the matching driver */
  228. if (dev->driver_override && strcmp(dev->driver_override, drv->name))
  229. return NULL;
  230. /* Look at the dynamic ids first, before the static ones */
  231. spin_lock(&drv->dynids.lock);
  232. list_for_each_entry(dynid, &drv->dynids.list, node) {
  233. if (pci_match_one_device(&dynid->id, dev)) {
  234. found_id = &dynid->id;
  235. break;
  236. }
  237. }
  238. spin_unlock(&drv->dynids.lock);
  239. if (!found_id)
  240. found_id = pci_match_id(drv->id_table, dev);
  241. /* driver_override will always match, send a dummy id */
  242. if (!found_id && dev->driver_override)
  243. found_id = &pci_device_id_any;
  244. return found_id;
  245. }
  246. struct drv_dev_and_id {
  247. struct pci_driver *drv;
  248. struct pci_dev *dev;
  249. const struct pci_device_id *id;
  250. };
  251. static long local_pci_probe(void *_ddi)
  252. {
  253. struct drv_dev_and_id *ddi = _ddi;
  254. struct pci_dev *pci_dev = ddi->dev;
  255. struct pci_driver *pci_drv = ddi->drv;
  256. struct device *dev = &pci_dev->dev;
  257. int rc;
  258. /*
  259. * Unbound PCI devices are always put in D0, regardless of
  260. * runtime PM status. During probe, the device is set to
  261. * active and the usage count is incremented. If the driver
  262. * supports runtime PM, it should call pm_runtime_put_noidle(),
  263. * or any other runtime PM helper function decrementing the usage
  264. * count, in its probe routine and pm_runtime_get_noresume() in
  265. * its remove routine.
  266. */
  267. pm_runtime_get_sync(dev);
  268. pci_dev->driver = pci_drv;
  269. rc = pci_drv->probe(pci_dev, ddi->id);
  270. if (!rc)
  271. return rc;
  272. if (rc < 0) {
  273. pci_dev->driver = NULL;
  274. pm_runtime_put_sync(dev);
  275. return rc;
  276. }
  277. /*
  278. * Probe function should return < 0 for failure, 0 for success
  279. * Treat values > 0 as success, but warn.
  280. */
  281. dev_warn(dev, "Driver probe function unexpectedly returned %d\n", rc);
  282. return 0;
  283. }
  284. static bool pci_physfn_is_probed(struct pci_dev *dev)
  285. {
  286. #ifdef CONFIG_PCI_IOV
  287. return dev->is_virtfn && dev->physfn->is_probed;
  288. #else
  289. return false;
  290. #endif
  291. }
  292. static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
  293. const struct pci_device_id *id)
  294. {
  295. int error, node, cpu;
  296. struct drv_dev_and_id ddi = { drv, dev, id };
  297. /*
  298. * Execute driver initialization on node where the device is
  299. * attached. This way the driver likely allocates its local memory
  300. * on the right node.
  301. */
  302. node = dev_to_node(&dev->dev);
  303. dev->is_probed = 1;
  304. cpu_hotplug_disable();
  305. /*
  306. * Prevent nesting work_on_cpu() for the case where a Virtual Function
  307. * device is probed from work_on_cpu() of the Physical device.
  308. */
  309. if (node < 0 || node >= MAX_NUMNODES || !node_online(node) ||
  310. pci_physfn_is_probed(dev))
  311. cpu = nr_cpu_ids;
  312. else
  313. cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
  314. if (cpu < nr_cpu_ids)
  315. error = work_on_cpu(cpu, local_pci_probe, &ddi);
  316. else
  317. error = local_pci_probe(&ddi);
  318. dev->is_probed = 0;
  319. cpu_hotplug_enable();
  320. return error;
  321. }
  322. /**
  323. * __pci_device_probe - check if a driver wants to claim a specific PCI device
  324. * @drv: driver to call to check if it wants the PCI device
  325. * @pci_dev: PCI device being probed
  326. *
  327. * returns 0 on success, else error.
  328. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  329. */
  330. static int __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  331. {
  332. const struct pci_device_id *id;
  333. int error = 0;
  334. if (!pci_dev->driver && drv->probe) {
  335. error = -ENODEV;
  336. id = pci_match_device(drv, pci_dev);
  337. if (id)
  338. error = pci_call_probe(drv, pci_dev, id);
  339. }
  340. return error;
  341. }
  342. int __weak pcibios_alloc_irq(struct pci_dev *dev)
  343. {
  344. return 0;
  345. }
  346. void __weak pcibios_free_irq(struct pci_dev *dev)
  347. {
  348. }
  349. #ifdef CONFIG_PCI_IOV
  350. static inline bool pci_device_can_probe(struct pci_dev *pdev)
  351. {
  352. return (!pdev->is_virtfn || pdev->physfn->sriov->drivers_autoprobe);
  353. }
  354. #else
  355. static inline bool pci_device_can_probe(struct pci_dev *pdev)
  356. {
  357. return true;
  358. }
  359. #endif
  360. static int pci_device_probe(struct device *dev)
  361. {
  362. int error;
  363. struct pci_dev *pci_dev = to_pci_dev(dev);
  364. struct pci_driver *drv = to_pci_driver(dev->driver);
  365. pci_assign_irq(pci_dev);
  366. error = pcibios_alloc_irq(pci_dev);
  367. if (error < 0)
  368. return error;
  369. pci_dev_get(pci_dev);
  370. if (pci_device_can_probe(pci_dev)) {
  371. error = __pci_device_probe(drv, pci_dev);
  372. if (error) {
  373. pcibios_free_irq(pci_dev);
  374. pci_dev_put(pci_dev);
  375. }
  376. }
  377. return error;
  378. }
  379. static int pci_device_remove(struct device *dev)
  380. {
  381. struct pci_dev *pci_dev = to_pci_dev(dev);
  382. struct pci_driver *drv = pci_dev->driver;
  383. if (drv) {
  384. if (drv->remove) {
  385. pm_runtime_get_sync(dev);
  386. drv->remove(pci_dev);
  387. pm_runtime_put_noidle(dev);
  388. }
  389. pcibios_free_irq(pci_dev);
  390. pci_dev->driver = NULL;
  391. }
  392. /* Undo the runtime PM settings in local_pci_probe() */
  393. pm_runtime_put_sync(dev);
  394. /*
  395. * If the device is still on, set the power state as "unknown",
  396. * since it might change by the next time we load the driver.
  397. */
  398. if (pci_dev->current_state == PCI_D0)
  399. pci_dev->current_state = PCI_UNKNOWN;
  400. /*
  401. * We would love to complain here if pci_dev->is_enabled is set, that
  402. * the driver should have called pci_disable_device(), but the
  403. * unfortunate fact is there are too many odd BIOS and bridge setups
  404. * that don't like drivers doing that all of the time.
  405. * Oh well, we can dream of sane hardware when we sleep, no matter how
  406. * horrible the crap we have to deal with is when we are awake...
  407. */
  408. pci_dev_put(pci_dev);
  409. return 0;
  410. }
  411. static void pci_device_shutdown(struct device *dev)
  412. {
  413. struct pci_dev *pci_dev = to_pci_dev(dev);
  414. struct pci_driver *drv = pci_dev->driver;
  415. pm_runtime_resume(dev);
  416. if (drv && drv->shutdown)
  417. drv->shutdown(pci_dev);
  418. /*
  419. * If this is a kexec reboot, turn off Bus Master bit on the
  420. * device to tell it to not continue to do DMA. Don't touch
  421. * devices in D3cold or unknown states.
  422. * If it is not a kexec reboot, firmware will hit the PCI
  423. * devices with big hammer and stop their DMA any way.
  424. */
  425. if (kexec_in_progress && (pci_dev->current_state <= PCI_D3hot))
  426. pci_clear_master(pci_dev);
  427. }
  428. #ifdef CONFIG_PM
  429. /* Auxiliary functions used for system resume and run-time resume. */
  430. /**
  431. * pci_restore_standard_config - restore standard config registers of PCI device
  432. * @pci_dev: PCI device to handle
  433. */
  434. static int pci_restore_standard_config(struct pci_dev *pci_dev)
  435. {
  436. pci_update_current_state(pci_dev, PCI_UNKNOWN);
  437. if (pci_dev->current_state != PCI_D0) {
  438. int error = pci_set_power_state(pci_dev, PCI_D0);
  439. if (error)
  440. return error;
  441. }
  442. pci_restore_state(pci_dev);
  443. pci_pme_restore(pci_dev);
  444. return 0;
  445. }
  446. #endif
  447. #ifdef CONFIG_PM_SLEEP
  448. static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
  449. {
  450. pci_power_up(pci_dev);
  451. pci_restore_state(pci_dev);
  452. pci_pme_restore(pci_dev);
  453. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  454. }
  455. /*
  456. * Default "suspend" method for devices that have no driver provided suspend,
  457. * or not even a driver at all (second part).
  458. */
  459. static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
  460. {
  461. /*
  462. * mark its power state as "unknown", since we don't know if
  463. * e.g. the BIOS will change its device state when we suspend.
  464. */
  465. if (pci_dev->current_state == PCI_D0)
  466. pci_dev->current_state = PCI_UNKNOWN;
  467. }
  468. /*
  469. * Default "resume" method for devices that have no driver provided resume,
  470. * or not even a driver at all (second part).
  471. */
  472. static int pci_pm_reenable_device(struct pci_dev *pci_dev)
  473. {
  474. int retval;
  475. /* if the device was enabled before suspend, reenable */
  476. retval = pci_reenable_device(pci_dev);
  477. /*
  478. * if the device was busmaster before the suspend, make it busmaster
  479. * again
  480. */
  481. if (pci_dev->is_busmaster)
  482. pci_set_master(pci_dev);
  483. return retval;
  484. }
  485. static int pci_legacy_suspend(struct device *dev, pm_message_t state)
  486. {
  487. struct pci_dev *pci_dev = to_pci_dev(dev);
  488. struct pci_driver *drv = pci_dev->driver;
  489. if (drv && drv->suspend) {
  490. pci_power_t prev = pci_dev->current_state;
  491. int error;
  492. error = drv->suspend(pci_dev, state);
  493. suspend_report_result(drv->suspend, error);
  494. if (error)
  495. return error;
  496. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  497. && pci_dev->current_state != PCI_UNKNOWN) {
  498. WARN_ONCE(pci_dev->current_state != prev,
  499. "PCI PM: Device state not saved by %pF\n",
  500. drv->suspend);
  501. }
  502. }
  503. pci_fixup_device(pci_fixup_suspend, pci_dev);
  504. return 0;
  505. }
  506. static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
  507. {
  508. struct pci_dev *pci_dev = to_pci_dev(dev);
  509. struct pci_driver *drv = pci_dev->driver;
  510. if (drv && drv->suspend_late) {
  511. pci_power_t prev = pci_dev->current_state;
  512. int error;
  513. error = drv->suspend_late(pci_dev, state);
  514. suspend_report_result(drv->suspend_late, error);
  515. if (error)
  516. return error;
  517. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  518. && pci_dev->current_state != PCI_UNKNOWN) {
  519. WARN_ONCE(pci_dev->current_state != prev,
  520. "PCI PM: Device state not saved by %pF\n",
  521. drv->suspend_late);
  522. goto Fixup;
  523. }
  524. }
  525. if (!pci_dev->state_saved)
  526. pci_save_state(pci_dev);
  527. pci_pm_set_unknown_state(pci_dev);
  528. Fixup:
  529. pci_fixup_device(pci_fixup_suspend_late, pci_dev);
  530. return 0;
  531. }
  532. static int pci_legacy_resume_early(struct device *dev)
  533. {
  534. struct pci_dev *pci_dev = to_pci_dev(dev);
  535. struct pci_driver *drv = pci_dev->driver;
  536. return drv && drv->resume_early ?
  537. drv->resume_early(pci_dev) : 0;
  538. }
  539. static int pci_legacy_resume(struct device *dev)
  540. {
  541. struct pci_dev *pci_dev = to_pci_dev(dev);
  542. struct pci_driver *drv = pci_dev->driver;
  543. pci_fixup_device(pci_fixup_resume, pci_dev);
  544. return drv && drv->resume ?
  545. drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
  546. }
  547. /* Auxiliary functions used by the new power management framework */
  548. static void pci_pm_default_resume(struct pci_dev *pci_dev)
  549. {
  550. pci_fixup_device(pci_fixup_resume, pci_dev);
  551. pci_enable_wake(pci_dev, PCI_D0, false);
  552. }
  553. static void pci_pm_default_suspend(struct pci_dev *pci_dev)
  554. {
  555. /* Disable non-bridge devices without PM support */
  556. if (!pci_has_subordinate(pci_dev))
  557. pci_disable_enabled_device(pci_dev);
  558. }
  559. static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
  560. {
  561. struct pci_driver *drv = pci_dev->driver;
  562. bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
  563. || drv->resume_early);
  564. /*
  565. * Legacy PM support is used by default, so warn if the new framework is
  566. * supported as well. Drivers are supposed to support either the
  567. * former, or the latter, but not both at the same time.
  568. */
  569. WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n",
  570. drv->name, pci_dev->vendor, pci_dev->device);
  571. return ret;
  572. }
  573. /* New power management framework */
  574. static int pci_pm_prepare(struct device *dev)
  575. {
  576. struct device_driver *drv = dev->driver;
  577. if (drv && drv->pm && drv->pm->prepare) {
  578. int error = drv->pm->prepare(dev);
  579. if (error < 0)
  580. return error;
  581. if (!error && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
  582. return 0;
  583. }
  584. return pci_dev_keep_suspended(to_pci_dev(dev));
  585. }
  586. static void pci_pm_complete(struct device *dev)
  587. {
  588. struct pci_dev *pci_dev = to_pci_dev(dev);
  589. pci_dev_complete_resume(pci_dev);
  590. pm_generic_complete(dev);
  591. /* Resume device if platform firmware has put it in reset-power-on */
  592. if (pm_runtime_suspended(dev) && pm_resume_via_firmware()) {
  593. pci_power_t pre_sleep_state = pci_dev->current_state;
  594. pci_update_current_state(pci_dev, pci_dev->current_state);
  595. if (pci_dev->current_state < pre_sleep_state)
  596. pm_request_resume(dev);
  597. }
  598. }
  599. #else /* !CONFIG_PM_SLEEP */
  600. #define pci_pm_prepare NULL
  601. #define pci_pm_complete NULL
  602. #endif /* !CONFIG_PM_SLEEP */
  603. #ifdef CONFIG_SUSPEND
  604. static int pci_pm_suspend(struct device *dev)
  605. {
  606. struct pci_dev *pci_dev = to_pci_dev(dev);
  607. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  608. if (pci_has_legacy_pm_support(pci_dev))
  609. return pci_legacy_suspend(dev, PMSG_SUSPEND);
  610. if (!pm) {
  611. pci_pm_default_suspend(pci_dev);
  612. return 0;
  613. }
  614. /*
  615. * PCI devices suspended at run time may need to be resumed at this
  616. * point, because in general it may be necessary to reconfigure them for
  617. * system suspend. Namely, if the device is expected to wake up the
  618. * system from the sleep state, it may have to be reconfigured for this
  619. * purpose, or if the device is not expected to wake up the system from
  620. * the sleep state, it should be prevented from signaling wakeup events
  621. * going forward.
  622. *
  623. * Also if the driver of the device does not indicate that its system
  624. * suspend callbacks can cope with runtime-suspended devices, it is
  625. * better to resume the device from runtime suspend here.
  626. */
  627. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  628. !pci_dev_keep_suspended(pci_dev))
  629. pm_runtime_resume(dev);
  630. pci_dev->state_saved = false;
  631. if (pm->suspend) {
  632. pci_power_t prev = pci_dev->current_state;
  633. int error;
  634. error = pm->suspend(dev);
  635. suspend_report_result(pm->suspend, error);
  636. if (error)
  637. return error;
  638. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  639. && pci_dev->current_state != PCI_UNKNOWN) {
  640. WARN_ONCE(pci_dev->current_state != prev,
  641. "PCI PM: State of device not saved by %pF\n",
  642. pm->suspend);
  643. }
  644. }
  645. return 0;
  646. }
  647. static int pci_pm_suspend_late(struct device *dev)
  648. {
  649. if (dev_pm_smart_suspend_and_suspended(dev))
  650. return 0;
  651. pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
  652. return pm_generic_suspend_late(dev);
  653. }
  654. static int pci_pm_suspend_noirq(struct device *dev)
  655. {
  656. struct pci_dev *pci_dev = to_pci_dev(dev);
  657. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  658. if (dev_pm_smart_suspend_and_suspended(dev)) {
  659. dev->power.may_skip_resume = true;
  660. return 0;
  661. }
  662. if (pci_has_legacy_pm_support(pci_dev))
  663. return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
  664. if (!pm) {
  665. pci_save_state(pci_dev);
  666. goto Fixup;
  667. }
  668. if (pm->suspend_noirq) {
  669. pci_power_t prev = pci_dev->current_state;
  670. int error;
  671. error = pm->suspend_noirq(dev);
  672. suspend_report_result(pm->suspend_noirq, error);
  673. if (error)
  674. return error;
  675. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  676. && pci_dev->current_state != PCI_UNKNOWN) {
  677. WARN_ONCE(pci_dev->current_state != prev,
  678. "PCI PM: State of device not saved by %pF\n",
  679. pm->suspend_noirq);
  680. goto Fixup;
  681. }
  682. }
  683. if (!pci_dev->state_saved) {
  684. pci_save_state(pci_dev);
  685. if (pci_power_manageable(pci_dev))
  686. pci_prepare_to_sleep(pci_dev);
  687. }
  688. dev_dbg(dev, "PCI PM: Suspend power state: %s\n",
  689. pci_power_name(pci_dev->current_state));
  690. pci_pm_set_unknown_state(pci_dev);
  691. /*
  692. * Some BIOSes from ASUS have a bug: If a USB EHCI host controller's
  693. * PCI COMMAND register isn't 0, the BIOS assumes that the controller
  694. * hasn't been quiesced and tries to turn it off. If the controller
  695. * is already in D3, this can hang or cause memory corruption.
  696. *
  697. * Since the value of the COMMAND register doesn't matter once the
  698. * device has been suspended, we can safely set it to 0 here.
  699. */
  700. if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  701. pci_write_config_word(pci_dev, PCI_COMMAND, 0);
  702. Fixup:
  703. pci_fixup_device(pci_fixup_suspend_late, pci_dev);
  704. /*
  705. * If the target system sleep state is suspend-to-idle, it is sufficient
  706. * to check whether or not the device's wakeup settings are good for
  707. * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
  708. * pci_pm_complete() to take care of fixing up the device's state
  709. * anyway, if need be.
  710. */
  711. dev->power.may_skip_resume = device_may_wakeup(dev) ||
  712. !device_can_wakeup(dev);
  713. return 0;
  714. }
  715. static int pci_pm_resume_noirq(struct device *dev)
  716. {
  717. struct pci_dev *pci_dev = to_pci_dev(dev);
  718. struct device_driver *drv = dev->driver;
  719. int error = 0;
  720. if (dev_pm_may_skip_resume(dev))
  721. return 0;
  722. /*
  723. * Devices with DPM_FLAG_SMART_SUSPEND may be left in runtime suspend
  724. * during system suspend, so update their runtime PM status to "active"
  725. * as they are going to be put into D0 shortly.
  726. */
  727. if (dev_pm_smart_suspend_and_suspended(dev))
  728. pm_runtime_set_active(dev);
  729. pci_pm_default_resume_early(pci_dev);
  730. if (pci_has_legacy_pm_support(pci_dev))
  731. return pci_legacy_resume_early(dev);
  732. if (drv && drv->pm && drv->pm->resume_noirq)
  733. error = drv->pm->resume_noirq(dev);
  734. return error;
  735. }
  736. static int pci_pm_resume(struct device *dev)
  737. {
  738. struct pci_dev *pci_dev = to_pci_dev(dev);
  739. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  740. int error = 0;
  741. /*
  742. * This is necessary for the suspend error path in which resume is
  743. * called without restoring the standard config registers of the device.
  744. */
  745. if (pci_dev->state_saved)
  746. pci_restore_standard_config(pci_dev);
  747. if (pci_has_legacy_pm_support(pci_dev))
  748. return pci_legacy_resume(dev);
  749. pci_pm_default_resume(pci_dev);
  750. if (pm) {
  751. if (pm->resume)
  752. error = pm->resume(dev);
  753. } else {
  754. pci_pm_reenable_device(pci_dev);
  755. }
  756. return error;
  757. }
  758. #else /* !CONFIG_SUSPEND */
  759. #define pci_pm_suspend NULL
  760. #define pci_pm_suspend_late NULL
  761. #define pci_pm_suspend_noirq NULL
  762. #define pci_pm_resume NULL
  763. #define pci_pm_resume_noirq NULL
  764. #endif /* !CONFIG_SUSPEND */
  765. #ifdef CONFIG_HIBERNATE_CALLBACKS
  766. /*
  767. * pcibios_pm_ops - provide arch-specific hooks when a PCI device is doing
  768. * a hibernate transition
  769. */
  770. struct dev_pm_ops __weak pcibios_pm_ops;
  771. static int pci_pm_freeze(struct device *dev)
  772. {
  773. struct pci_dev *pci_dev = to_pci_dev(dev);
  774. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  775. if (pci_has_legacy_pm_support(pci_dev))
  776. return pci_legacy_suspend(dev, PMSG_FREEZE);
  777. if (!pm) {
  778. pci_pm_default_suspend(pci_dev);
  779. return 0;
  780. }
  781. /*
  782. * This used to be done in pci_pm_prepare() for all devices and some
  783. * drivers may depend on it, so do it here. Ideally, runtime-suspended
  784. * devices should not be touched during freeze/thaw transitions,
  785. * however.
  786. */
  787. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND))
  788. pm_runtime_resume(dev);
  789. pci_dev->state_saved = false;
  790. if (pm->freeze) {
  791. int error;
  792. error = pm->freeze(dev);
  793. suspend_report_result(pm->freeze, error);
  794. if (error)
  795. return error;
  796. }
  797. return 0;
  798. }
  799. static int pci_pm_freeze_late(struct device *dev)
  800. {
  801. if (dev_pm_smart_suspend_and_suspended(dev))
  802. return 0;
  803. return pm_generic_freeze_late(dev);
  804. }
  805. static int pci_pm_freeze_noirq(struct device *dev)
  806. {
  807. struct pci_dev *pci_dev = to_pci_dev(dev);
  808. struct device_driver *drv = dev->driver;
  809. if (dev_pm_smart_suspend_and_suspended(dev))
  810. return 0;
  811. if (pci_has_legacy_pm_support(pci_dev))
  812. return pci_legacy_suspend_late(dev, PMSG_FREEZE);
  813. if (drv && drv->pm && drv->pm->freeze_noirq) {
  814. int error;
  815. error = drv->pm->freeze_noirq(dev);
  816. suspend_report_result(drv->pm->freeze_noirq, error);
  817. if (error)
  818. return error;
  819. }
  820. if (!pci_dev->state_saved)
  821. pci_save_state(pci_dev);
  822. pci_pm_set_unknown_state(pci_dev);
  823. if (pcibios_pm_ops.freeze_noirq)
  824. return pcibios_pm_ops.freeze_noirq(dev);
  825. return 0;
  826. }
  827. static int pci_pm_thaw_noirq(struct device *dev)
  828. {
  829. struct pci_dev *pci_dev = to_pci_dev(dev);
  830. struct device_driver *drv = dev->driver;
  831. int error = 0;
  832. /*
  833. * If the device is in runtime suspend, the code below may not work
  834. * correctly with it, so skip that code and make the PM core skip all of
  835. * the subsequent "thaw" callbacks for the device.
  836. */
  837. if (dev_pm_smart_suspend_and_suspended(dev)) {
  838. dev_pm_skip_next_resume_phases(dev);
  839. return 0;
  840. }
  841. if (pcibios_pm_ops.thaw_noirq) {
  842. error = pcibios_pm_ops.thaw_noirq(dev);
  843. if (error)
  844. return error;
  845. }
  846. if (pci_has_legacy_pm_support(pci_dev))
  847. return pci_legacy_resume_early(dev);
  848. /*
  849. * pci_restore_state() requires the device to be in D0 (because of MSI
  850. * restoration among other things), so force it into D0 in case the
  851. * driver's "freeze" callbacks put it into a low-power state directly.
  852. */
  853. pci_set_power_state(pci_dev, PCI_D0);
  854. pci_restore_state(pci_dev);
  855. if (drv && drv->pm && drv->pm->thaw_noirq)
  856. error = drv->pm->thaw_noirq(dev);
  857. return error;
  858. }
  859. static int pci_pm_thaw(struct device *dev)
  860. {
  861. struct pci_dev *pci_dev = to_pci_dev(dev);
  862. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  863. int error = 0;
  864. if (pci_has_legacy_pm_support(pci_dev))
  865. return pci_legacy_resume(dev);
  866. if (pm) {
  867. if (pm->thaw)
  868. error = pm->thaw(dev);
  869. } else {
  870. pci_pm_reenable_device(pci_dev);
  871. }
  872. pci_dev->state_saved = false;
  873. return error;
  874. }
  875. static int pci_pm_poweroff(struct device *dev)
  876. {
  877. struct pci_dev *pci_dev = to_pci_dev(dev);
  878. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  879. if (pci_has_legacy_pm_support(pci_dev))
  880. return pci_legacy_suspend(dev, PMSG_HIBERNATE);
  881. if (!pm) {
  882. pci_pm_default_suspend(pci_dev);
  883. return 0;
  884. }
  885. /* The reason to do that is the same as in pci_pm_suspend(). */
  886. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  887. !pci_dev_keep_suspended(pci_dev))
  888. pm_runtime_resume(dev);
  889. pci_dev->state_saved = false;
  890. if (pm->poweroff) {
  891. int error;
  892. error = pm->poweroff(dev);
  893. suspend_report_result(pm->poweroff, error);
  894. if (error)
  895. return error;
  896. }
  897. return 0;
  898. }
  899. static int pci_pm_poweroff_late(struct device *dev)
  900. {
  901. if (dev_pm_smart_suspend_and_suspended(dev))
  902. return 0;
  903. pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
  904. return pm_generic_poweroff_late(dev);
  905. }
  906. static int pci_pm_poweroff_noirq(struct device *dev)
  907. {
  908. struct pci_dev *pci_dev = to_pci_dev(dev);
  909. struct device_driver *drv = dev->driver;
  910. if (dev_pm_smart_suspend_and_suspended(dev))
  911. return 0;
  912. if (pci_has_legacy_pm_support(to_pci_dev(dev)))
  913. return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
  914. if (!drv || !drv->pm) {
  915. pci_fixup_device(pci_fixup_suspend_late, pci_dev);
  916. return 0;
  917. }
  918. if (drv->pm->poweroff_noirq) {
  919. int error;
  920. error = drv->pm->poweroff_noirq(dev);
  921. suspend_report_result(drv->pm->poweroff_noirq, error);
  922. if (error)
  923. return error;
  924. }
  925. if (!pci_dev->state_saved && !pci_has_subordinate(pci_dev))
  926. pci_prepare_to_sleep(pci_dev);
  927. /*
  928. * The reason for doing this here is the same as for the analogous code
  929. * in pci_pm_suspend_noirq().
  930. */
  931. if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  932. pci_write_config_word(pci_dev, PCI_COMMAND, 0);
  933. pci_fixup_device(pci_fixup_suspend_late, pci_dev);
  934. if (pcibios_pm_ops.poweroff_noirq)
  935. return pcibios_pm_ops.poweroff_noirq(dev);
  936. return 0;
  937. }
  938. static int pci_pm_restore_noirq(struct device *dev)
  939. {
  940. struct pci_dev *pci_dev = to_pci_dev(dev);
  941. struct device_driver *drv = dev->driver;
  942. int error = 0;
  943. /* This is analogous to the pci_pm_resume_noirq() case. */
  944. if (dev_pm_smart_suspend_and_suspended(dev))
  945. pm_runtime_set_active(dev);
  946. if (pcibios_pm_ops.restore_noirq) {
  947. error = pcibios_pm_ops.restore_noirq(dev);
  948. if (error)
  949. return error;
  950. }
  951. pci_pm_default_resume_early(pci_dev);
  952. if (pci_has_legacy_pm_support(pci_dev))
  953. return pci_legacy_resume_early(dev);
  954. if (drv && drv->pm && drv->pm->restore_noirq)
  955. error = drv->pm->restore_noirq(dev);
  956. return error;
  957. }
  958. static int pci_pm_restore(struct device *dev)
  959. {
  960. struct pci_dev *pci_dev = to_pci_dev(dev);
  961. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  962. int error = 0;
  963. /*
  964. * This is necessary for the hibernation error path in which restore is
  965. * called without restoring the standard config registers of the device.
  966. */
  967. if (pci_dev->state_saved)
  968. pci_restore_standard_config(pci_dev);
  969. if (pci_has_legacy_pm_support(pci_dev))
  970. return pci_legacy_resume(dev);
  971. pci_pm_default_resume(pci_dev);
  972. if (pm) {
  973. if (pm->restore)
  974. error = pm->restore(dev);
  975. } else {
  976. pci_pm_reenable_device(pci_dev);
  977. }
  978. return error;
  979. }
  980. #else /* !CONFIG_HIBERNATE_CALLBACKS */
  981. #define pci_pm_freeze NULL
  982. #define pci_pm_freeze_late NULL
  983. #define pci_pm_freeze_noirq NULL
  984. #define pci_pm_thaw NULL
  985. #define pci_pm_thaw_noirq NULL
  986. #define pci_pm_poweroff NULL
  987. #define pci_pm_poweroff_late NULL
  988. #define pci_pm_poweroff_noirq NULL
  989. #define pci_pm_restore NULL
  990. #define pci_pm_restore_noirq NULL
  991. #endif /* !CONFIG_HIBERNATE_CALLBACKS */
  992. #ifdef CONFIG_PM
  993. static int pci_pm_runtime_suspend(struct device *dev)
  994. {
  995. struct pci_dev *pci_dev = to_pci_dev(dev);
  996. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  997. pci_power_t prev = pci_dev->current_state;
  998. int error;
  999. /*
  1000. * If pci_dev->driver is not set (unbound), the device should
  1001. * always remain in D0 regardless of the runtime PM status
  1002. */
  1003. if (!pci_dev->driver)
  1004. return 0;
  1005. if (!pm || !pm->runtime_suspend)
  1006. return -ENOSYS;
  1007. pci_dev->state_saved = false;
  1008. error = pm->runtime_suspend(dev);
  1009. if (error) {
  1010. /*
  1011. * -EBUSY and -EAGAIN is used to request the runtime PM core
  1012. * to schedule a new suspend, so log the event only with debug
  1013. * log level.
  1014. */
  1015. if (error == -EBUSY || error == -EAGAIN)
  1016. dev_dbg(dev, "can't suspend now (%pf returned %d)\n",
  1017. pm->runtime_suspend, error);
  1018. else
  1019. dev_err(dev, "can't suspend (%pf returned %d)\n",
  1020. pm->runtime_suspend, error);
  1021. return error;
  1022. }
  1023. pci_fixup_device(pci_fixup_suspend, pci_dev);
  1024. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  1025. && pci_dev->current_state != PCI_UNKNOWN) {
  1026. WARN_ONCE(pci_dev->current_state != prev,
  1027. "PCI PM: State of device not saved by %pF\n",
  1028. pm->runtime_suspend);
  1029. return 0;
  1030. }
  1031. if (!pci_dev->state_saved) {
  1032. pci_save_state(pci_dev);
  1033. pci_finish_runtime_suspend(pci_dev);
  1034. }
  1035. return 0;
  1036. }
  1037. static int pci_pm_runtime_resume(struct device *dev)
  1038. {
  1039. int rc;
  1040. struct pci_dev *pci_dev = to_pci_dev(dev);
  1041. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  1042. /*
  1043. * If pci_dev->driver is not set (unbound), the device should
  1044. * always remain in D0 regardless of the runtime PM status
  1045. */
  1046. if (!pci_dev->driver)
  1047. return 0;
  1048. if (!pm || !pm->runtime_resume)
  1049. return -ENOSYS;
  1050. pci_restore_standard_config(pci_dev);
  1051. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  1052. pci_enable_wake(pci_dev, PCI_D0, false);
  1053. pci_fixup_device(pci_fixup_resume, pci_dev);
  1054. rc = pm->runtime_resume(dev);
  1055. pci_dev->runtime_d3cold = false;
  1056. return rc;
  1057. }
  1058. static int pci_pm_runtime_idle(struct device *dev)
  1059. {
  1060. struct pci_dev *pci_dev = to_pci_dev(dev);
  1061. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  1062. int ret = 0;
  1063. /*
  1064. * If pci_dev->driver is not set (unbound), the device should
  1065. * always remain in D0 regardless of the runtime PM status
  1066. */
  1067. if (!pci_dev->driver)
  1068. return 0;
  1069. if (!pm)
  1070. return -ENOSYS;
  1071. if (pm->runtime_idle)
  1072. ret = pm->runtime_idle(dev);
  1073. return ret;
  1074. }
  1075. static const struct dev_pm_ops pci_dev_pm_ops = {
  1076. .prepare = pci_pm_prepare,
  1077. .complete = pci_pm_complete,
  1078. .suspend = pci_pm_suspend,
  1079. .suspend_late = pci_pm_suspend_late,
  1080. .resume = pci_pm_resume,
  1081. .freeze = pci_pm_freeze,
  1082. .freeze_late = pci_pm_freeze_late,
  1083. .thaw = pci_pm_thaw,
  1084. .poweroff = pci_pm_poweroff,
  1085. .poweroff_late = pci_pm_poweroff_late,
  1086. .restore = pci_pm_restore,
  1087. .suspend_noirq = pci_pm_suspend_noirq,
  1088. .resume_noirq = pci_pm_resume_noirq,
  1089. .freeze_noirq = pci_pm_freeze_noirq,
  1090. .thaw_noirq = pci_pm_thaw_noirq,
  1091. .poweroff_noirq = pci_pm_poweroff_noirq,
  1092. .restore_noirq = pci_pm_restore_noirq,
  1093. .runtime_suspend = pci_pm_runtime_suspend,
  1094. .runtime_resume = pci_pm_runtime_resume,
  1095. .runtime_idle = pci_pm_runtime_idle,
  1096. };
  1097. #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
  1098. #else /* !CONFIG_PM */
  1099. #define pci_pm_runtime_suspend NULL
  1100. #define pci_pm_runtime_resume NULL
  1101. #define pci_pm_runtime_idle NULL
  1102. #define PCI_PM_OPS_PTR NULL
  1103. #endif /* !CONFIG_PM */
  1104. /**
  1105. * __pci_register_driver - register a new pci driver
  1106. * @drv: the driver structure to register
  1107. * @owner: owner module of drv
  1108. * @mod_name: module name string
  1109. *
  1110. * Adds the driver structure to the list of registered drivers.
  1111. * Returns a negative value on error, otherwise 0.
  1112. * If no error occurred, the driver remains registered even if
  1113. * no device was claimed during registration.
  1114. */
  1115. int __pci_register_driver(struct pci_driver *drv, struct module *owner,
  1116. const char *mod_name)
  1117. {
  1118. /* initialize common driver fields */
  1119. drv->driver.name = drv->name;
  1120. drv->driver.bus = &pci_bus_type;
  1121. drv->driver.owner = owner;
  1122. drv->driver.mod_name = mod_name;
  1123. drv->driver.groups = drv->groups;
  1124. spin_lock_init(&drv->dynids.lock);
  1125. INIT_LIST_HEAD(&drv->dynids.list);
  1126. /* register with core */
  1127. return driver_register(&drv->driver);
  1128. }
  1129. EXPORT_SYMBOL(__pci_register_driver);
  1130. /**
  1131. * pci_unregister_driver - unregister a pci driver
  1132. * @drv: the driver structure to unregister
  1133. *
  1134. * Deletes the driver structure from the list of registered PCI drivers,
  1135. * gives it a chance to clean up by calling its remove() function for
  1136. * each device it was responsible for, and marks those devices as
  1137. * driverless.
  1138. */
  1139. void pci_unregister_driver(struct pci_driver *drv)
  1140. {
  1141. driver_unregister(&drv->driver);
  1142. pci_free_dynids(drv);
  1143. }
  1144. EXPORT_SYMBOL(pci_unregister_driver);
  1145. static struct pci_driver pci_compat_driver = {
  1146. .name = "compat"
  1147. };
  1148. /**
  1149. * pci_dev_driver - get the pci_driver of a device
  1150. * @dev: the device to query
  1151. *
  1152. * Returns the appropriate pci_driver structure or %NULL if there is no
  1153. * registered driver for the device.
  1154. */
  1155. struct pci_driver *pci_dev_driver(const struct pci_dev *dev)
  1156. {
  1157. if (dev->driver)
  1158. return dev->driver;
  1159. else {
  1160. int i;
  1161. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  1162. if (dev->resource[i].flags & IORESOURCE_BUSY)
  1163. return &pci_compat_driver;
  1164. }
  1165. return NULL;
  1166. }
  1167. EXPORT_SYMBOL(pci_dev_driver);
  1168. /**
  1169. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  1170. * @dev: the PCI device structure to match against
  1171. * @drv: the device driver to search for matching PCI device id structures
  1172. *
  1173. * Used by a driver to check whether a PCI device present in the
  1174. * system is in its list of supported devices. Returns the matching
  1175. * pci_device_id structure or %NULL if there is no match.
  1176. */
  1177. static int pci_bus_match(struct device *dev, struct device_driver *drv)
  1178. {
  1179. struct pci_dev *pci_dev = to_pci_dev(dev);
  1180. struct pci_driver *pci_drv;
  1181. const struct pci_device_id *found_id;
  1182. if (!pci_dev->match_driver)
  1183. return 0;
  1184. pci_drv = to_pci_driver(drv);
  1185. found_id = pci_match_device(pci_drv, pci_dev);
  1186. if (found_id)
  1187. return 1;
  1188. return 0;
  1189. }
  1190. /**
  1191. * pci_dev_get - increments the reference count of the pci device structure
  1192. * @dev: the device being referenced
  1193. *
  1194. * Each live reference to a device should be refcounted.
  1195. *
  1196. * Drivers for PCI devices should normally record such references in
  1197. * their probe() methods, when they bind to a device, and release
  1198. * them by calling pci_dev_put(), in their disconnect() methods.
  1199. *
  1200. * A pointer to the device with the incremented reference counter is returned.
  1201. */
  1202. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  1203. {
  1204. if (dev)
  1205. get_device(&dev->dev);
  1206. return dev;
  1207. }
  1208. EXPORT_SYMBOL(pci_dev_get);
  1209. /**
  1210. * pci_dev_put - release a use of the pci device structure
  1211. * @dev: device that's been disconnected
  1212. *
  1213. * Must be called when a user of a device is finished with it. When the last
  1214. * user of the device calls this function, the memory of the device is freed.
  1215. */
  1216. void pci_dev_put(struct pci_dev *dev)
  1217. {
  1218. if (dev)
  1219. put_device(&dev->dev);
  1220. }
  1221. EXPORT_SYMBOL(pci_dev_put);
  1222. static int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
  1223. {
  1224. struct pci_dev *pdev;
  1225. if (!dev)
  1226. return -ENODEV;
  1227. pdev = to_pci_dev(dev);
  1228. if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class))
  1229. return -ENOMEM;
  1230. if (add_uevent_var(env, "PCI_ID=%04X:%04X", pdev->vendor, pdev->device))
  1231. return -ENOMEM;
  1232. if (add_uevent_var(env, "PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor,
  1233. pdev->subsystem_device))
  1234. return -ENOMEM;
  1235. if (add_uevent_var(env, "PCI_SLOT_NAME=%s", pci_name(pdev)))
  1236. return -ENOMEM;
  1237. if (add_uevent_var(env, "MODALIAS=pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X",
  1238. pdev->vendor, pdev->device,
  1239. pdev->subsystem_vendor, pdev->subsystem_device,
  1240. (u8)(pdev->class >> 16), (u8)(pdev->class >> 8),
  1241. (u8)(pdev->class)))
  1242. return -ENOMEM;
  1243. return 0;
  1244. }
  1245. static int pci_bus_num_vf(struct device *dev)
  1246. {
  1247. return pci_num_vf(to_pci_dev(dev));
  1248. }
  1249. struct bus_type pci_bus_type = {
  1250. .name = "pci",
  1251. .match = pci_bus_match,
  1252. .uevent = pci_uevent,
  1253. .probe = pci_device_probe,
  1254. .remove = pci_device_remove,
  1255. .shutdown = pci_device_shutdown,
  1256. .dev_groups = pci_dev_groups,
  1257. .bus_groups = pci_bus_groups,
  1258. .drv_groups = pci_drv_groups,
  1259. .pm = PCI_PM_OPS_PTR,
  1260. .num_vf = pci_bus_num_vf,
  1261. .force_dma = true,
  1262. };
  1263. EXPORT_SYMBOL(pci_bus_type);
  1264. static int __init pci_driver_init(void)
  1265. {
  1266. return bus_register(&pci_bus_type);
  1267. }
  1268. postcore_initcall(pci_driver_init);