pci-driver.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /*
  2. * drivers/pci/pci-driver.c
  3. *
  4. * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
  5. * (C) Copyright 2007 Novell Inc.
  6. *
  7. * Released under the GPL v2 only.
  8. *
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/device.h>
  14. #include <linux/mempolicy.h>
  15. #include <linux/string.h>
  16. #include <linux/slab.h>
  17. #include <linux/sched.h>
  18. #include <linux/cpu.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/suspend.h>
  21. #include "pci.h"
  22. struct pci_dynid {
  23. struct list_head node;
  24. struct pci_device_id id;
  25. };
  26. /**
  27. * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
  28. * @drv: target pci driver
  29. * @vendor: PCI vendor ID
  30. * @device: PCI device ID
  31. * @subvendor: PCI subvendor ID
  32. * @subdevice: PCI subdevice ID
  33. * @class: PCI class
  34. * @class_mask: PCI class mask
  35. * @driver_data: private driver data
  36. *
  37. * Adds a new dynamic pci device ID to this driver and causes the
  38. * driver to probe for all devices again. @drv must have been
  39. * registered prior to calling this function.
  40. *
  41. * CONTEXT:
  42. * Does GFP_KERNEL allocation.
  43. *
  44. * RETURNS:
  45. * 0 on success, -errno on failure.
  46. */
  47. int pci_add_dynid(struct pci_driver *drv,
  48. unsigned int vendor, unsigned int device,
  49. unsigned int subvendor, unsigned int subdevice,
  50. unsigned int class, unsigned int class_mask,
  51. unsigned long driver_data)
  52. {
  53. struct pci_dynid *dynid;
  54. int retval;
  55. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  56. if (!dynid)
  57. return -ENOMEM;
  58. dynid->id.vendor = vendor;
  59. dynid->id.device = device;
  60. dynid->id.subvendor = subvendor;
  61. dynid->id.subdevice = subdevice;
  62. dynid->id.class = class;
  63. dynid->id.class_mask = class_mask;
  64. dynid->id.driver_data = driver_data;
  65. spin_lock(&drv->dynids.lock);
  66. list_add_tail(&dynid->node, &drv->dynids.list);
  67. spin_unlock(&drv->dynids.lock);
  68. get_driver(&drv->driver);
  69. retval = driver_attach(&drv->driver);
  70. put_driver(&drv->driver);
  71. return retval;
  72. }
  73. static void pci_free_dynids(struct pci_driver *drv)
  74. {
  75. struct pci_dynid *dynid, *n;
  76. spin_lock(&drv->dynids.lock);
  77. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  78. list_del(&dynid->node);
  79. kfree(dynid);
  80. }
  81. spin_unlock(&drv->dynids.lock);
  82. }
  83. /*
  84. * Dynamic device ID manipulation via sysfs is disabled for !CONFIG_HOTPLUG
  85. */
  86. #ifdef CONFIG_HOTPLUG
  87. /**
  88. * store_new_id - sysfs frontend to pci_add_dynid()
  89. * @driver: target device driver
  90. * @buf: buffer for scanning device ID data
  91. * @count: input size
  92. *
  93. * Allow PCI IDs to be added to an existing driver via sysfs.
  94. */
  95. static ssize_t
  96. store_new_id(struct device_driver *driver, const char *buf, size_t count)
  97. {
  98. struct pci_driver *pdrv = to_pci_driver(driver);
  99. const struct pci_device_id *ids = pdrv->id_table;
  100. __u32 vendor, device, subvendor=PCI_ANY_ID,
  101. subdevice=PCI_ANY_ID, class=0, class_mask=0;
  102. unsigned long driver_data=0;
  103. int fields=0;
  104. int retval;
  105. fields = sscanf(buf, "%x %x %x %x %x %x %lx",
  106. &vendor, &device, &subvendor, &subdevice,
  107. &class, &class_mask, &driver_data);
  108. if (fields < 2)
  109. return -EINVAL;
  110. /* Only accept driver_data values that match an existing id_table
  111. entry */
  112. if (ids) {
  113. retval = -EINVAL;
  114. while (ids->vendor || ids->subvendor || ids->class_mask) {
  115. if (driver_data == ids->driver_data) {
  116. retval = 0;
  117. break;
  118. }
  119. ids++;
  120. }
  121. if (retval) /* No match */
  122. return retval;
  123. }
  124. retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
  125. class, class_mask, driver_data);
  126. if (retval)
  127. return retval;
  128. return count;
  129. }
  130. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  131. /**
  132. * store_remove_id - remove a PCI device ID from this driver
  133. * @driver: target device driver
  134. * @buf: buffer for scanning device ID data
  135. * @count: input size
  136. *
  137. * Removes a dynamic pci device ID to this driver.
  138. */
  139. static ssize_t
  140. store_remove_id(struct device_driver *driver, const char *buf, size_t count)
  141. {
  142. struct pci_dynid *dynid, *n;
  143. struct pci_driver *pdrv = to_pci_driver(driver);
  144. __u32 vendor, device, subvendor = PCI_ANY_ID,
  145. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  146. int fields = 0;
  147. int retval = -ENODEV;
  148. fields = sscanf(buf, "%x %x %x %x %x %x",
  149. &vendor, &device, &subvendor, &subdevice,
  150. &class, &class_mask);
  151. if (fields < 2)
  152. return -EINVAL;
  153. spin_lock(&pdrv->dynids.lock);
  154. list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
  155. struct pci_device_id *id = &dynid->id;
  156. if ((id->vendor == vendor) &&
  157. (id->device == device) &&
  158. (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
  159. (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
  160. !((id->class ^ class) & class_mask)) {
  161. list_del(&dynid->node);
  162. kfree(dynid);
  163. retval = 0;
  164. break;
  165. }
  166. }
  167. spin_unlock(&pdrv->dynids.lock);
  168. if (retval)
  169. return retval;
  170. return count;
  171. }
  172. static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
  173. static int
  174. pci_create_newid_file(struct pci_driver *drv)
  175. {
  176. int error = 0;
  177. if (drv->probe != NULL)
  178. error = driver_create_file(&drv->driver, &driver_attr_new_id);
  179. return error;
  180. }
  181. static void pci_remove_newid_file(struct pci_driver *drv)
  182. {
  183. driver_remove_file(&drv->driver, &driver_attr_new_id);
  184. }
  185. static int
  186. pci_create_removeid_file(struct pci_driver *drv)
  187. {
  188. int error = 0;
  189. if (drv->probe != NULL)
  190. error = driver_create_file(&drv->driver,&driver_attr_remove_id);
  191. return error;
  192. }
  193. static void pci_remove_removeid_file(struct pci_driver *drv)
  194. {
  195. driver_remove_file(&drv->driver, &driver_attr_remove_id);
  196. }
  197. #else /* !CONFIG_HOTPLUG */
  198. static inline int pci_create_newid_file(struct pci_driver *drv)
  199. {
  200. return 0;
  201. }
  202. static inline void pci_remove_newid_file(struct pci_driver *drv) {}
  203. static inline int pci_create_removeid_file(struct pci_driver *drv)
  204. {
  205. return 0;
  206. }
  207. static inline void pci_remove_removeid_file(struct pci_driver *drv) {}
  208. #endif
  209. /**
  210. * pci_match_id - See if a pci device matches a given pci_id table
  211. * @ids: array of PCI device id structures to search in
  212. * @dev: the PCI device structure to match against.
  213. *
  214. * Used by a driver to check whether a PCI device present in the
  215. * system is in its list of supported devices. Returns the matching
  216. * pci_device_id structure or %NULL if there is no match.
  217. *
  218. * Deprecated, don't use this as it will not catch any dynamic ids
  219. * that a driver might want to check for.
  220. */
  221. const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
  222. struct pci_dev *dev)
  223. {
  224. if (ids) {
  225. while (ids->vendor || ids->subvendor || ids->class_mask) {
  226. if (pci_match_one_device(ids, dev))
  227. return ids;
  228. ids++;
  229. }
  230. }
  231. return NULL;
  232. }
  233. /**
  234. * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
  235. * @drv: the PCI driver to match against
  236. * @dev: the PCI device structure to match against
  237. *
  238. * Used by a driver to check whether a PCI device present in the
  239. * system is in its list of supported devices. Returns the matching
  240. * pci_device_id structure or %NULL if there is no match.
  241. */
  242. static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
  243. struct pci_dev *dev)
  244. {
  245. struct pci_dynid *dynid;
  246. /* Look at the dynamic ids first, before the static ones */
  247. spin_lock(&drv->dynids.lock);
  248. list_for_each_entry(dynid, &drv->dynids.list, node) {
  249. if (pci_match_one_device(&dynid->id, dev)) {
  250. spin_unlock(&drv->dynids.lock);
  251. return &dynid->id;
  252. }
  253. }
  254. spin_unlock(&drv->dynids.lock);
  255. return pci_match_id(drv->id_table, dev);
  256. }
  257. struct drv_dev_and_id {
  258. struct pci_driver *drv;
  259. struct pci_dev *dev;
  260. const struct pci_device_id *id;
  261. };
  262. static long local_pci_probe(void *_ddi)
  263. {
  264. struct drv_dev_and_id *ddi = _ddi;
  265. struct device *dev = &ddi->dev->dev;
  266. int rc;
  267. /* Unbound PCI devices are always set to disabled and suspended.
  268. * During probe, the device is set to enabled and active and the
  269. * usage count is incremented. If the driver supports runtime PM,
  270. * it should call pm_runtime_put_noidle() in its probe routine and
  271. * pm_runtime_get_noresume() in its remove routine.
  272. */
  273. pm_runtime_get_noresume(dev);
  274. pm_runtime_set_active(dev);
  275. pm_runtime_enable(dev);
  276. rc = ddi->drv->probe(ddi->dev, ddi->id);
  277. if (rc) {
  278. pm_runtime_disable(dev);
  279. pm_runtime_set_suspended(dev);
  280. pm_runtime_put_noidle(dev);
  281. }
  282. return rc;
  283. }
  284. static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
  285. const struct pci_device_id *id)
  286. {
  287. int error, node;
  288. struct drv_dev_and_id ddi = { drv, dev, id };
  289. /* Execute driver initialization on node where the device's
  290. bus is attached to. This way the driver likely allocates
  291. its local memory on the right node without any need to
  292. change it. */
  293. node = dev_to_node(&dev->dev);
  294. if (node >= 0) {
  295. int cpu;
  296. get_online_cpus();
  297. cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
  298. if (cpu < nr_cpu_ids)
  299. error = work_on_cpu(cpu, local_pci_probe, &ddi);
  300. else
  301. error = local_pci_probe(&ddi);
  302. put_online_cpus();
  303. } else
  304. error = local_pci_probe(&ddi);
  305. return error;
  306. }
  307. /**
  308. * __pci_device_probe - check if a driver wants to claim a specific PCI device
  309. * @drv: driver to call to check if it wants the PCI device
  310. * @pci_dev: PCI device being probed
  311. *
  312. * returns 0 on success, else error.
  313. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  314. */
  315. static int
  316. __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  317. {
  318. const struct pci_device_id *id;
  319. int error = 0;
  320. if (!pci_dev->driver && drv->probe) {
  321. error = -ENODEV;
  322. id = pci_match_device(drv, pci_dev);
  323. if (id)
  324. error = pci_call_probe(drv, pci_dev, id);
  325. if (error >= 0) {
  326. pci_dev->driver = drv;
  327. error = 0;
  328. }
  329. }
  330. return error;
  331. }
  332. static int pci_device_probe(struct device * dev)
  333. {
  334. int error = 0;
  335. struct pci_driver *drv;
  336. struct pci_dev *pci_dev;
  337. drv = to_pci_driver(dev->driver);
  338. pci_dev = to_pci_dev(dev);
  339. pci_dev_get(pci_dev);
  340. error = __pci_device_probe(drv, pci_dev);
  341. if (error)
  342. pci_dev_put(pci_dev);
  343. return error;
  344. }
  345. static int pci_device_remove(struct device * dev)
  346. {
  347. struct pci_dev * pci_dev = to_pci_dev(dev);
  348. struct pci_driver * drv = pci_dev->driver;
  349. if (drv) {
  350. if (drv->remove) {
  351. pm_runtime_get_sync(dev);
  352. drv->remove(pci_dev);
  353. pm_runtime_put_noidle(dev);
  354. }
  355. pci_dev->driver = NULL;
  356. }
  357. /* Undo the runtime PM settings in local_pci_probe() */
  358. pm_runtime_disable(dev);
  359. pm_runtime_set_suspended(dev);
  360. pm_runtime_put_noidle(dev);
  361. /*
  362. * If the device is still on, set the power state as "unknown",
  363. * since it might change by the next time we load the driver.
  364. */
  365. if (pci_dev->current_state == PCI_D0)
  366. pci_dev->current_state = PCI_UNKNOWN;
  367. /*
  368. * We would love to complain here if pci_dev->is_enabled is set, that
  369. * the driver should have called pci_disable_device(), but the
  370. * unfortunate fact is there are too many odd BIOS and bridge setups
  371. * that don't like drivers doing that all of the time.
  372. * Oh well, we can dream of sane hardware when we sleep, no matter how
  373. * horrible the crap we have to deal with is when we are awake...
  374. */
  375. pci_dev_put(pci_dev);
  376. return 0;
  377. }
  378. static void pci_device_shutdown(struct device *dev)
  379. {
  380. struct pci_dev *pci_dev = to_pci_dev(dev);
  381. struct pci_driver *drv = pci_dev->driver;
  382. if (drv && drv->shutdown)
  383. drv->shutdown(pci_dev);
  384. pci_msi_shutdown(pci_dev);
  385. pci_msix_shutdown(pci_dev);
  386. }
  387. #ifdef CONFIG_PM
  388. /* Auxiliary functions used for system resume and run-time resume. */
  389. /**
  390. * pci_restore_standard_config - restore standard config registers of PCI device
  391. * @pci_dev: PCI device to handle
  392. */
  393. static int pci_restore_standard_config(struct pci_dev *pci_dev)
  394. {
  395. pci_update_current_state(pci_dev, PCI_UNKNOWN);
  396. if (pci_dev->current_state != PCI_D0) {
  397. int error = pci_set_power_state(pci_dev, PCI_D0);
  398. if (error)
  399. return error;
  400. }
  401. pci_restore_state(pci_dev);
  402. return 0;
  403. }
  404. static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
  405. {
  406. pci_restore_standard_config(pci_dev);
  407. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  408. }
  409. #endif
  410. #ifdef CONFIG_PM_SLEEP
  411. /*
  412. * Default "suspend" method for devices that have no driver provided suspend,
  413. * or not even a driver at all (second part).
  414. */
  415. static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
  416. {
  417. /*
  418. * mark its power state as "unknown", since we don't know if
  419. * e.g. the BIOS will change its device state when we suspend.
  420. */
  421. if (pci_dev->current_state == PCI_D0)
  422. pci_dev->current_state = PCI_UNKNOWN;
  423. }
  424. /*
  425. * Default "resume" method for devices that have no driver provided resume,
  426. * or not even a driver at all (second part).
  427. */
  428. static int pci_pm_reenable_device(struct pci_dev *pci_dev)
  429. {
  430. int retval;
  431. /* if the device was enabled before suspend, reenable */
  432. retval = pci_reenable_device(pci_dev);
  433. /*
  434. * if the device was busmaster before the suspend, make it busmaster
  435. * again
  436. */
  437. if (pci_dev->is_busmaster)
  438. pci_set_master(pci_dev);
  439. return retval;
  440. }
  441. static int pci_legacy_suspend(struct device *dev, pm_message_t state)
  442. {
  443. struct pci_dev * pci_dev = to_pci_dev(dev);
  444. struct pci_driver * drv = pci_dev->driver;
  445. if (drv && drv->suspend) {
  446. pci_power_t prev = pci_dev->current_state;
  447. int error;
  448. error = drv->suspend(pci_dev, state);
  449. suspend_report_result(drv->suspend, error);
  450. if (error)
  451. return error;
  452. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  453. && pci_dev->current_state != PCI_UNKNOWN) {
  454. WARN_ONCE(pci_dev->current_state != prev,
  455. "PCI PM: Device state not saved by %pF\n",
  456. drv->suspend);
  457. }
  458. }
  459. pci_fixup_device(pci_fixup_suspend, pci_dev);
  460. return 0;
  461. }
  462. static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
  463. {
  464. struct pci_dev * pci_dev = to_pci_dev(dev);
  465. struct pci_driver * drv = pci_dev->driver;
  466. if (drv && drv->suspend_late) {
  467. pci_power_t prev = pci_dev->current_state;
  468. int error;
  469. error = drv->suspend_late(pci_dev, state);
  470. suspend_report_result(drv->suspend_late, error);
  471. if (error)
  472. return error;
  473. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  474. && pci_dev->current_state != PCI_UNKNOWN) {
  475. WARN_ONCE(pci_dev->current_state != prev,
  476. "PCI PM: Device state not saved by %pF\n",
  477. drv->suspend_late);
  478. return 0;
  479. }
  480. }
  481. if (!pci_dev->state_saved)
  482. pci_save_state(pci_dev);
  483. pci_pm_set_unknown_state(pci_dev);
  484. return 0;
  485. }
  486. static int pci_legacy_resume_early(struct device *dev)
  487. {
  488. struct pci_dev * pci_dev = to_pci_dev(dev);
  489. struct pci_driver * drv = pci_dev->driver;
  490. return drv && drv->resume_early ?
  491. drv->resume_early(pci_dev) : 0;
  492. }
  493. static int pci_legacy_resume(struct device *dev)
  494. {
  495. struct pci_dev * pci_dev = to_pci_dev(dev);
  496. struct pci_driver * drv = pci_dev->driver;
  497. pci_fixup_device(pci_fixup_resume, pci_dev);
  498. return drv && drv->resume ?
  499. drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
  500. }
  501. /* Auxiliary functions used by the new power management framework */
  502. static void pci_pm_default_resume(struct pci_dev *pci_dev)
  503. {
  504. pci_fixup_device(pci_fixup_resume, pci_dev);
  505. if (!pci_is_bridge(pci_dev))
  506. pci_enable_wake(pci_dev, PCI_D0, false);
  507. }
  508. static void pci_pm_default_suspend(struct pci_dev *pci_dev)
  509. {
  510. /* Disable non-bridge devices without PM support */
  511. if (!pci_is_bridge(pci_dev))
  512. pci_disable_enabled_device(pci_dev);
  513. }
  514. static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
  515. {
  516. struct pci_driver *drv = pci_dev->driver;
  517. bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
  518. || drv->resume_early);
  519. /*
  520. * Legacy PM support is used by default, so warn if the new framework is
  521. * supported as well. Drivers are supposed to support either the
  522. * former, or the latter, but not both at the same time.
  523. */
  524. WARN_ON(ret && drv->driver.pm);
  525. return ret;
  526. }
  527. /* New power management framework */
  528. static int pci_pm_prepare(struct device *dev)
  529. {
  530. struct device_driver *drv = dev->driver;
  531. int error = 0;
  532. /*
  533. * If a PCI device configured to wake up the system from sleep states
  534. * has been suspended at run time and there's a resume request pending
  535. * for it, this is equivalent to the device signaling wakeup, so the
  536. * system suspend operation should be aborted.
  537. */
  538. pm_runtime_get_noresume(dev);
  539. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  540. pm_wakeup_event(dev, 0);
  541. if (pm_wakeup_pending()) {
  542. pm_runtime_put_sync(dev);
  543. return -EBUSY;
  544. }
  545. /*
  546. * PCI devices suspended at run time need to be resumed at this
  547. * point, because in general it is necessary to reconfigure them for
  548. * system suspend. Namely, if the device is supposed to wake up the
  549. * system from the sleep state, we may need to reconfigure it for this
  550. * purpose. In turn, if the device is not supposed to wake up the
  551. * system from the sleep state, we'll have to prevent it from signaling
  552. * wake-up.
  553. */
  554. pm_runtime_resume(dev);
  555. if (drv && drv->pm && drv->pm->prepare)
  556. error = drv->pm->prepare(dev);
  557. return error;
  558. }
  559. static void pci_pm_complete(struct device *dev)
  560. {
  561. struct device_driver *drv = dev->driver;
  562. if (drv && drv->pm && drv->pm->complete)
  563. drv->pm->complete(dev);
  564. pm_runtime_put_sync(dev);
  565. }
  566. #else /* !CONFIG_PM_SLEEP */
  567. #define pci_pm_prepare NULL
  568. #define pci_pm_complete NULL
  569. #endif /* !CONFIG_PM_SLEEP */
  570. #ifdef CONFIG_SUSPEND
  571. static int pci_pm_suspend(struct device *dev)
  572. {
  573. struct pci_dev *pci_dev = to_pci_dev(dev);
  574. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  575. if (pci_has_legacy_pm_support(pci_dev))
  576. return pci_legacy_suspend(dev, PMSG_SUSPEND);
  577. if (!pm) {
  578. pci_pm_default_suspend(pci_dev);
  579. goto Fixup;
  580. }
  581. if (pm->suspend) {
  582. pci_power_t prev = pci_dev->current_state;
  583. int error;
  584. error = pm->suspend(dev);
  585. suspend_report_result(pm->suspend, error);
  586. if (error)
  587. return error;
  588. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  589. && pci_dev->current_state != PCI_UNKNOWN) {
  590. WARN_ONCE(pci_dev->current_state != prev,
  591. "PCI PM: State of device not saved by %pF\n",
  592. pm->suspend);
  593. }
  594. }
  595. Fixup:
  596. pci_fixup_device(pci_fixup_suspend, pci_dev);
  597. return 0;
  598. }
  599. static int pci_pm_suspend_noirq(struct device *dev)
  600. {
  601. struct pci_dev *pci_dev = to_pci_dev(dev);
  602. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  603. if (pci_has_legacy_pm_support(pci_dev))
  604. return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
  605. if (!pm) {
  606. pci_save_state(pci_dev);
  607. return 0;
  608. }
  609. if (pm->suspend_noirq) {
  610. pci_power_t prev = pci_dev->current_state;
  611. int error;
  612. error = pm->suspend_noirq(dev);
  613. suspend_report_result(pm->suspend_noirq, error);
  614. if (error)
  615. return error;
  616. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  617. && pci_dev->current_state != PCI_UNKNOWN) {
  618. WARN_ONCE(pci_dev->current_state != prev,
  619. "PCI PM: State of device not saved by %pF\n",
  620. pm->suspend_noirq);
  621. return 0;
  622. }
  623. }
  624. if (!pci_dev->state_saved) {
  625. pci_save_state(pci_dev);
  626. if (!pci_is_bridge(pci_dev))
  627. pci_prepare_to_sleep(pci_dev);
  628. }
  629. pci_pm_set_unknown_state(pci_dev);
  630. return 0;
  631. }
  632. static int pci_pm_resume_noirq(struct device *dev)
  633. {
  634. struct pci_dev *pci_dev = to_pci_dev(dev);
  635. struct device_driver *drv = dev->driver;
  636. int error = 0;
  637. pci_pm_default_resume_early(pci_dev);
  638. if (pci_has_legacy_pm_support(pci_dev))
  639. return pci_legacy_resume_early(dev);
  640. if (drv && drv->pm && drv->pm->resume_noirq)
  641. error = drv->pm->resume_noirq(dev);
  642. return error;
  643. }
  644. static int pci_pm_resume(struct device *dev)
  645. {
  646. struct pci_dev *pci_dev = to_pci_dev(dev);
  647. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  648. int error = 0;
  649. /*
  650. * This is necessary for the suspend error path in which resume is
  651. * called without restoring the standard config registers of the device.
  652. */
  653. if (pci_dev->state_saved)
  654. pci_restore_standard_config(pci_dev);
  655. if (pci_has_legacy_pm_support(pci_dev))
  656. return pci_legacy_resume(dev);
  657. pci_pm_default_resume(pci_dev);
  658. if (pm) {
  659. if (pm->resume)
  660. error = pm->resume(dev);
  661. } else {
  662. pci_pm_reenable_device(pci_dev);
  663. }
  664. return error;
  665. }
  666. #else /* !CONFIG_SUSPEND */
  667. #define pci_pm_suspend NULL
  668. #define pci_pm_suspend_noirq NULL
  669. #define pci_pm_resume NULL
  670. #define pci_pm_resume_noirq NULL
  671. #endif /* !CONFIG_SUSPEND */
  672. #ifdef CONFIG_HIBERNATE_CALLBACKS
  673. static int pci_pm_freeze(struct device *dev)
  674. {
  675. struct pci_dev *pci_dev = to_pci_dev(dev);
  676. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  677. if (pci_has_legacy_pm_support(pci_dev))
  678. return pci_legacy_suspend(dev, PMSG_FREEZE);
  679. if (!pm) {
  680. pci_pm_default_suspend(pci_dev);
  681. return 0;
  682. }
  683. if (pm->freeze) {
  684. int error;
  685. error = pm->freeze(dev);
  686. suspend_report_result(pm->freeze, error);
  687. if (error)
  688. return error;
  689. }
  690. return 0;
  691. }
  692. static int pci_pm_freeze_noirq(struct device *dev)
  693. {
  694. struct pci_dev *pci_dev = to_pci_dev(dev);
  695. struct device_driver *drv = dev->driver;
  696. if (pci_has_legacy_pm_support(pci_dev))
  697. return pci_legacy_suspend_late(dev, PMSG_FREEZE);
  698. if (drv && drv->pm && drv->pm->freeze_noirq) {
  699. int error;
  700. error = drv->pm->freeze_noirq(dev);
  701. suspend_report_result(drv->pm->freeze_noirq, error);
  702. if (error)
  703. return error;
  704. }
  705. if (!pci_dev->state_saved)
  706. pci_save_state(pci_dev);
  707. pci_pm_set_unknown_state(pci_dev);
  708. return 0;
  709. }
  710. static int pci_pm_thaw_noirq(struct device *dev)
  711. {
  712. struct pci_dev *pci_dev = to_pci_dev(dev);
  713. struct device_driver *drv = dev->driver;
  714. int error = 0;
  715. if (pci_has_legacy_pm_support(pci_dev))
  716. return pci_legacy_resume_early(dev);
  717. pci_update_current_state(pci_dev, PCI_D0);
  718. if (drv && drv->pm && drv->pm->thaw_noirq)
  719. error = drv->pm->thaw_noirq(dev);
  720. return error;
  721. }
  722. static int pci_pm_thaw(struct device *dev)
  723. {
  724. struct pci_dev *pci_dev = to_pci_dev(dev);
  725. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  726. int error = 0;
  727. if (pci_has_legacy_pm_support(pci_dev))
  728. return pci_legacy_resume(dev);
  729. if (pm) {
  730. if (pm->thaw)
  731. error = pm->thaw(dev);
  732. } else {
  733. pci_pm_reenable_device(pci_dev);
  734. }
  735. pci_dev->state_saved = false;
  736. return error;
  737. }
  738. static int pci_pm_poweroff(struct device *dev)
  739. {
  740. struct pci_dev *pci_dev = to_pci_dev(dev);
  741. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  742. if (pci_has_legacy_pm_support(pci_dev))
  743. return pci_legacy_suspend(dev, PMSG_HIBERNATE);
  744. if (!pm) {
  745. pci_pm_default_suspend(pci_dev);
  746. goto Fixup;
  747. }
  748. if (pm->poweroff) {
  749. int error;
  750. error = pm->poweroff(dev);
  751. suspend_report_result(pm->poweroff, error);
  752. if (error)
  753. return error;
  754. }
  755. Fixup:
  756. pci_fixup_device(pci_fixup_suspend, pci_dev);
  757. return 0;
  758. }
  759. static int pci_pm_poweroff_noirq(struct device *dev)
  760. {
  761. struct pci_dev *pci_dev = to_pci_dev(dev);
  762. struct device_driver *drv = dev->driver;
  763. if (pci_has_legacy_pm_support(to_pci_dev(dev)))
  764. return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
  765. if (!drv || !drv->pm)
  766. return 0;
  767. if (drv->pm->poweroff_noirq) {
  768. int error;
  769. error = drv->pm->poweroff_noirq(dev);
  770. suspend_report_result(drv->pm->poweroff_noirq, error);
  771. if (error)
  772. return error;
  773. }
  774. if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
  775. pci_prepare_to_sleep(pci_dev);
  776. return 0;
  777. }
  778. static int pci_pm_restore_noirq(struct device *dev)
  779. {
  780. struct pci_dev *pci_dev = to_pci_dev(dev);
  781. struct device_driver *drv = dev->driver;
  782. int error = 0;
  783. pci_pm_default_resume_early(pci_dev);
  784. if (pci_has_legacy_pm_support(pci_dev))
  785. return pci_legacy_resume_early(dev);
  786. if (drv && drv->pm && drv->pm->restore_noirq)
  787. error = drv->pm->restore_noirq(dev);
  788. return error;
  789. }
  790. static int pci_pm_restore(struct device *dev)
  791. {
  792. struct pci_dev *pci_dev = to_pci_dev(dev);
  793. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  794. int error = 0;
  795. /*
  796. * This is necessary for the hibernation error path in which restore is
  797. * called without restoring the standard config registers of the device.
  798. */
  799. if (pci_dev->state_saved)
  800. pci_restore_standard_config(pci_dev);
  801. if (pci_has_legacy_pm_support(pci_dev))
  802. return pci_legacy_resume(dev);
  803. pci_pm_default_resume(pci_dev);
  804. if (pm) {
  805. if (pm->restore)
  806. error = pm->restore(dev);
  807. } else {
  808. pci_pm_reenable_device(pci_dev);
  809. }
  810. return error;
  811. }
  812. #else /* !CONFIG_HIBERNATE_CALLBACKS */
  813. #define pci_pm_freeze NULL
  814. #define pci_pm_freeze_noirq NULL
  815. #define pci_pm_thaw NULL
  816. #define pci_pm_thaw_noirq NULL
  817. #define pci_pm_poweroff NULL
  818. #define pci_pm_poweroff_noirq NULL
  819. #define pci_pm_restore NULL
  820. #define pci_pm_restore_noirq NULL
  821. #endif /* !CONFIG_HIBERNATE_CALLBACKS */
  822. #ifdef CONFIG_PM_RUNTIME
  823. static int pci_pm_runtime_suspend(struct device *dev)
  824. {
  825. struct pci_dev *pci_dev = to_pci_dev(dev);
  826. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  827. pci_power_t prev = pci_dev->current_state;
  828. int error;
  829. if (!pm || !pm->runtime_suspend)
  830. return -ENOSYS;
  831. error = pm->runtime_suspend(dev);
  832. suspend_report_result(pm->runtime_suspend, error);
  833. if (error)
  834. return error;
  835. pci_fixup_device(pci_fixup_suspend, pci_dev);
  836. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  837. && pci_dev->current_state != PCI_UNKNOWN) {
  838. WARN_ONCE(pci_dev->current_state != prev,
  839. "PCI PM: State of device not saved by %pF\n",
  840. pm->runtime_suspend);
  841. return 0;
  842. }
  843. if (!pci_dev->state_saved)
  844. pci_save_state(pci_dev);
  845. pci_finish_runtime_suspend(pci_dev);
  846. return 0;
  847. }
  848. static int pci_pm_runtime_resume(struct device *dev)
  849. {
  850. struct pci_dev *pci_dev = to_pci_dev(dev);
  851. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  852. if (!pm || !pm->runtime_resume)
  853. return -ENOSYS;
  854. pci_pm_default_resume_early(pci_dev);
  855. __pci_enable_wake(pci_dev, PCI_D0, true, false);
  856. pci_fixup_device(pci_fixup_resume, pci_dev);
  857. return pm->runtime_resume(dev);
  858. }
  859. static int pci_pm_runtime_idle(struct device *dev)
  860. {
  861. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  862. if (!pm)
  863. return -ENOSYS;
  864. if (pm->runtime_idle) {
  865. int ret = pm->runtime_idle(dev);
  866. if (ret)
  867. return ret;
  868. }
  869. pm_runtime_suspend(dev);
  870. return 0;
  871. }
  872. #else /* !CONFIG_PM_RUNTIME */
  873. #define pci_pm_runtime_suspend NULL
  874. #define pci_pm_runtime_resume NULL
  875. #define pci_pm_runtime_idle NULL
  876. #endif /* !CONFIG_PM_RUNTIME */
  877. #ifdef CONFIG_PM
  878. const struct dev_pm_ops pci_dev_pm_ops = {
  879. .prepare = pci_pm_prepare,
  880. .complete = pci_pm_complete,
  881. .suspend = pci_pm_suspend,
  882. .resume = pci_pm_resume,
  883. .freeze = pci_pm_freeze,
  884. .thaw = pci_pm_thaw,
  885. .poweroff = pci_pm_poweroff,
  886. .restore = pci_pm_restore,
  887. .suspend_noirq = pci_pm_suspend_noirq,
  888. .resume_noirq = pci_pm_resume_noirq,
  889. .freeze_noirq = pci_pm_freeze_noirq,
  890. .thaw_noirq = pci_pm_thaw_noirq,
  891. .poweroff_noirq = pci_pm_poweroff_noirq,
  892. .restore_noirq = pci_pm_restore_noirq,
  893. .runtime_suspend = pci_pm_runtime_suspend,
  894. .runtime_resume = pci_pm_runtime_resume,
  895. .runtime_idle = pci_pm_runtime_idle,
  896. };
  897. #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
  898. #else /* !COMFIG_PM_OPS */
  899. #define PCI_PM_OPS_PTR NULL
  900. #endif /* !COMFIG_PM_OPS */
  901. /**
  902. * __pci_register_driver - register a new pci driver
  903. * @drv: the driver structure to register
  904. * @owner: owner module of drv
  905. * @mod_name: module name string
  906. *
  907. * Adds the driver structure to the list of registered drivers.
  908. * Returns a negative value on error, otherwise 0.
  909. * If no error occurred, the driver remains registered even if
  910. * no device was claimed during registration.
  911. */
  912. int __pci_register_driver(struct pci_driver *drv, struct module *owner,
  913. const char *mod_name)
  914. {
  915. int error;
  916. /* initialize common driver fields */
  917. drv->driver.name = drv->name;
  918. drv->driver.bus = &pci_bus_type;
  919. drv->driver.owner = owner;
  920. drv->driver.mod_name = mod_name;
  921. spin_lock_init(&drv->dynids.lock);
  922. INIT_LIST_HEAD(&drv->dynids.list);
  923. /* register with core */
  924. error = driver_register(&drv->driver);
  925. if (error)
  926. goto out;
  927. error = pci_create_newid_file(drv);
  928. if (error)
  929. goto out_newid;
  930. error = pci_create_removeid_file(drv);
  931. if (error)
  932. goto out_removeid;
  933. out:
  934. return error;
  935. out_removeid:
  936. pci_remove_newid_file(drv);
  937. out_newid:
  938. driver_unregister(&drv->driver);
  939. goto out;
  940. }
  941. /**
  942. * pci_unregister_driver - unregister a pci driver
  943. * @drv: the driver structure to unregister
  944. *
  945. * Deletes the driver structure from the list of registered PCI drivers,
  946. * gives it a chance to clean up by calling its remove() function for
  947. * each device it was responsible for, and marks those devices as
  948. * driverless.
  949. */
  950. void
  951. pci_unregister_driver(struct pci_driver *drv)
  952. {
  953. pci_remove_removeid_file(drv);
  954. pci_remove_newid_file(drv);
  955. driver_unregister(&drv->driver);
  956. pci_free_dynids(drv);
  957. }
  958. static struct pci_driver pci_compat_driver = {
  959. .name = "compat"
  960. };
  961. /**
  962. * pci_dev_driver - get the pci_driver of a device
  963. * @dev: the device to query
  964. *
  965. * Returns the appropriate pci_driver structure or %NULL if there is no
  966. * registered driver for the device.
  967. */
  968. struct pci_driver *
  969. pci_dev_driver(const struct pci_dev *dev)
  970. {
  971. if (dev->driver)
  972. return dev->driver;
  973. else {
  974. int i;
  975. for(i=0; i<=PCI_ROM_RESOURCE; i++)
  976. if (dev->resource[i].flags & IORESOURCE_BUSY)
  977. return &pci_compat_driver;
  978. }
  979. return NULL;
  980. }
  981. /**
  982. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  983. * @dev: the PCI device structure to match against
  984. * @drv: the device driver to search for matching PCI device id structures
  985. *
  986. * Used by a driver to check whether a PCI device present in the
  987. * system is in its list of supported devices. Returns the matching
  988. * pci_device_id structure or %NULL if there is no match.
  989. */
  990. static int pci_bus_match(struct device *dev, struct device_driver *drv)
  991. {
  992. struct pci_dev *pci_dev = to_pci_dev(dev);
  993. struct pci_driver *pci_drv = to_pci_driver(drv);
  994. const struct pci_device_id *found_id;
  995. found_id = pci_match_device(pci_drv, pci_dev);
  996. if (found_id)
  997. return 1;
  998. return 0;
  999. }
  1000. /**
  1001. * pci_dev_get - increments the reference count of the pci device structure
  1002. * @dev: the device being referenced
  1003. *
  1004. * Each live reference to a device should be refcounted.
  1005. *
  1006. * Drivers for PCI devices should normally record such references in
  1007. * their probe() methods, when they bind to a device, and release
  1008. * them by calling pci_dev_put(), in their disconnect() methods.
  1009. *
  1010. * A pointer to the device with the incremented reference counter is returned.
  1011. */
  1012. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  1013. {
  1014. if (dev)
  1015. get_device(&dev->dev);
  1016. return dev;
  1017. }
  1018. /**
  1019. * pci_dev_put - release a use of the pci device structure
  1020. * @dev: device that's been disconnected
  1021. *
  1022. * Must be called when a user of a device is finished with it. When the last
  1023. * user of the device calls this function, the memory of the device is freed.
  1024. */
  1025. void pci_dev_put(struct pci_dev *dev)
  1026. {
  1027. if (dev)
  1028. put_device(&dev->dev);
  1029. }
  1030. #ifndef CONFIG_HOTPLUG
  1031. int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
  1032. {
  1033. return -ENODEV;
  1034. }
  1035. #endif
  1036. struct bus_type pci_bus_type = {
  1037. .name = "pci",
  1038. .match = pci_bus_match,
  1039. .uevent = pci_uevent,
  1040. .probe = pci_device_probe,
  1041. .remove = pci_device_remove,
  1042. .shutdown = pci_device_shutdown,
  1043. .dev_attrs = pci_dev_attrs,
  1044. .bus_attrs = pci_bus_attrs,
  1045. .pm = PCI_PM_OPS_PTR,
  1046. };
  1047. static int __init pci_driver_init(void)
  1048. {
  1049. return bus_register(&pci_bus_type);
  1050. }
  1051. postcore_initcall(pci_driver_init);
  1052. EXPORT_SYMBOL_GPL(pci_add_dynid);
  1053. EXPORT_SYMBOL(pci_match_id);
  1054. EXPORT_SYMBOL(__pci_register_driver);
  1055. EXPORT_SYMBOL(pci_unregister_driver);
  1056. EXPORT_SYMBOL(pci_dev_driver);
  1057. EXPORT_SYMBOL(pci_bus_type);
  1058. EXPORT_SYMBOL(pci_dev_get);
  1059. EXPORT_SYMBOL(pci_dev_put);