pci-driver.c 31 KB

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