pci-driver.c 32 KB

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