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