pci-driver.c 30 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 "pci.h"
  21. struct pci_dynid {
  22. struct list_head node;
  23. struct pci_device_id id;
  24. };
  25. /**
  26. * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
  27. * @drv: target pci driver
  28. * @vendor: PCI vendor ID
  29. * @device: PCI device ID
  30. * @subvendor: PCI subvendor ID
  31. * @subdevice: PCI subdevice ID
  32. * @class: PCI class
  33. * @class_mask: PCI class mask
  34. * @driver_data: private driver data
  35. *
  36. * Adds a new dynamic pci device ID to this driver and causes the
  37. * driver to probe for all devices again. @drv must have been
  38. * registered prior to calling this function.
  39. *
  40. * CONTEXT:
  41. * Does GFP_KERNEL allocation.
  42. *
  43. * RETURNS:
  44. * 0 on success, -errno on failure.
  45. */
  46. int pci_add_dynid(struct pci_driver *drv,
  47. unsigned int vendor, unsigned int device,
  48. unsigned int subvendor, unsigned int subdevice,
  49. unsigned int class, unsigned int class_mask,
  50. unsigned long driver_data)
  51. {
  52. struct pci_dynid *dynid;
  53. int retval;
  54. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  55. if (!dynid)
  56. return -ENOMEM;
  57. dynid->id.vendor = vendor;
  58. dynid->id.device = device;
  59. dynid->id.subvendor = subvendor;
  60. dynid->id.subdevice = subdevice;
  61. dynid->id.class = class;
  62. dynid->id.class_mask = class_mask;
  63. dynid->id.driver_data = driver_data;
  64. spin_lock(&drv->dynids.lock);
  65. list_add_tail(&dynid->node, &drv->dynids.list);
  66. spin_unlock(&drv->dynids.lock);
  67. get_driver(&drv->driver);
  68. retval = driver_attach(&drv->driver);
  69. put_driver(&drv->driver);
  70. return retval;
  71. }
  72. static void pci_free_dynids(struct pci_driver *drv)
  73. {
  74. struct pci_dynid *dynid, *n;
  75. spin_lock(&drv->dynids.lock);
  76. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  77. list_del(&dynid->node);
  78. kfree(dynid);
  79. }
  80. spin_unlock(&drv->dynids.lock);
  81. }
  82. /*
  83. * Dynamic device ID manipulation via sysfs is disabled for !CONFIG_HOTPLUG
  84. */
  85. #ifdef CONFIG_HOTPLUG
  86. /**
  87. * store_new_id - sysfs frontend to pci_add_dynid()
  88. * @driver: target device driver
  89. * @buf: buffer for scanning device ID data
  90. * @count: input size
  91. *
  92. * Allow PCI IDs to be added to an existing driver via sysfs.
  93. */
  94. static ssize_t
  95. store_new_id(struct device_driver *driver, const char *buf, size_t count)
  96. {
  97. struct pci_driver *pdrv = to_pci_driver(driver);
  98. const struct pci_device_id *ids = pdrv->id_table;
  99. __u32 vendor, device, subvendor=PCI_ANY_ID,
  100. subdevice=PCI_ANY_ID, class=0, class_mask=0;
  101. unsigned long driver_data=0;
  102. int fields=0;
  103. int retval;
  104. fields = sscanf(buf, "%x %x %x %x %x %x %lx",
  105. &vendor, &device, &subvendor, &subdevice,
  106. &class, &class_mask, &driver_data);
  107. if (fields < 2)
  108. return -EINVAL;
  109. /* Only accept driver_data values that match an existing id_table
  110. entry */
  111. if (ids) {
  112. retval = -EINVAL;
  113. while (ids->vendor || ids->subvendor || ids->class_mask) {
  114. if (driver_data == ids->driver_data) {
  115. retval = 0;
  116. break;
  117. }
  118. ids++;
  119. }
  120. if (retval) /* No match */
  121. return retval;
  122. }
  123. retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
  124. class, class_mask, driver_data);
  125. if (retval)
  126. return retval;
  127. return count;
  128. }
  129. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  130. /**
  131. * store_remove_id - remove a PCI device ID from this driver
  132. * @driver: target device driver
  133. * @buf: buffer for scanning device ID data
  134. * @count: input size
  135. *
  136. * Removes a dynamic pci device ID to this driver.
  137. */
  138. static ssize_t
  139. store_remove_id(struct device_driver *driver, const char *buf, size_t count)
  140. {
  141. struct pci_dynid *dynid, *n;
  142. struct pci_driver *pdrv = to_pci_driver(driver);
  143. __u32 vendor, device, subvendor = PCI_ANY_ID,
  144. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  145. int fields = 0;
  146. int retval = -ENODEV;
  147. fields = sscanf(buf, "%x %x %x %x %x %x",
  148. &vendor, &device, &subvendor, &subdevice,
  149. &class, &class_mask);
  150. if (fields < 2)
  151. return -EINVAL;
  152. spin_lock(&pdrv->dynids.lock);
  153. list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
  154. struct pci_device_id *id = &dynid->id;
  155. if ((id->vendor == vendor) &&
  156. (id->device == device) &&
  157. (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
  158. (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
  159. !((id->class ^ class) & class_mask)) {
  160. list_del(&dynid->node);
  161. kfree(dynid);
  162. retval = 0;
  163. break;
  164. }
  165. }
  166. spin_unlock(&pdrv->dynids.lock);
  167. if (retval)
  168. return retval;
  169. return count;
  170. }
  171. static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
  172. static int
  173. pci_create_newid_file(struct pci_driver *drv)
  174. {
  175. int error = 0;
  176. if (drv->probe != NULL)
  177. error = driver_create_file(&drv->driver, &driver_attr_new_id);
  178. return error;
  179. }
  180. static void pci_remove_newid_file(struct pci_driver *drv)
  181. {
  182. driver_remove_file(&drv->driver, &driver_attr_new_id);
  183. }
  184. static int
  185. pci_create_removeid_file(struct pci_driver *drv)
  186. {
  187. int error = 0;
  188. if (drv->probe != NULL)
  189. error = driver_create_file(&drv->driver,&driver_attr_remove_id);
  190. return error;
  191. }
  192. static void pci_remove_removeid_file(struct pci_driver *drv)
  193. {
  194. driver_remove_file(&drv->driver, &driver_attr_remove_id);
  195. }
  196. #else /* !CONFIG_HOTPLUG */
  197. static inline int pci_create_newid_file(struct pci_driver *drv)
  198. {
  199. return 0;
  200. }
  201. static inline void pci_remove_newid_file(struct pci_driver *drv) {}
  202. static inline int pci_create_removeid_file(struct pci_driver *drv)
  203. {
  204. return 0;
  205. }
  206. static inline void pci_remove_removeid_file(struct pci_driver *drv) {}
  207. #endif
  208. /**
  209. * pci_match_id - See if a pci device matches a given pci_id table
  210. * @ids: array of PCI device id structures to search in
  211. * @dev: the PCI device structure to match against.
  212. *
  213. * Used by a driver to check whether a PCI device present in the
  214. * system is in its list of supported devices. Returns the matching
  215. * pci_device_id structure or %NULL if there is no match.
  216. *
  217. * Deprecated, don't use this as it will not catch any dynamic ids
  218. * that a driver might want to check for.
  219. */
  220. const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
  221. struct pci_dev *dev)
  222. {
  223. if (ids) {
  224. while (ids->vendor || ids->subvendor || ids->class_mask) {
  225. if (pci_match_one_device(ids, dev))
  226. return ids;
  227. ids++;
  228. }
  229. }
  230. return NULL;
  231. }
  232. /**
  233. * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
  234. * @drv: the PCI driver to match against
  235. * @dev: the PCI device structure to match against
  236. *
  237. * Used by a driver to check whether a PCI device present in the
  238. * system is in its list of supported devices. Returns the matching
  239. * pci_device_id structure or %NULL if there is no match.
  240. */
  241. static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
  242. struct pci_dev *dev)
  243. {
  244. struct pci_dynid *dynid;
  245. /* Look at the dynamic ids first, before the static ones */
  246. spin_lock(&drv->dynids.lock);
  247. list_for_each_entry(dynid, &drv->dynids.list, node) {
  248. if (pci_match_one_device(&dynid->id, dev)) {
  249. spin_unlock(&drv->dynids.lock);
  250. return &dynid->id;
  251. }
  252. }
  253. spin_unlock(&drv->dynids.lock);
  254. return pci_match_id(drv->id_table, dev);
  255. }
  256. struct drv_dev_and_id {
  257. struct pci_driver *drv;
  258. struct pci_dev *dev;
  259. const struct pci_device_id *id;
  260. };
  261. static long local_pci_probe(void *_ddi)
  262. {
  263. struct drv_dev_and_id *ddi = _ddi;
  264. struct device *dev = &ddi->dev->dev;
  265. int rc;
  266. /* Unbound PCI devices are always set to disabled and suspended.
  267. * During probe, the device is set to enabled and active and the
  268. * usage count is incremented. If the driver supports runtime PM,
  269. * it should call pm_runtime_put_noidle() in its probe routine and
  270. * pm_runtime_get_noresume() in its remove routine.
  271. */
  272. pm_runtime_get_noresume(dev);
  273. pm_runtime_set_active(dev);
  274. pm_runtime_enable(dev);
  275. rc = ddi->drv->probe(ddi->dev, ddi->id);
  276. if (rc) {
  277. pm_runtime_disable(dev);
  278. pm_runtime_set_suspended(dev);
  279. pm_runtime_put_noidle(dev);
  280. }
  281. return rc;
  282. }
  283. static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
  284. const struct pci_device_id *id)
  285. {
  286. int error, node;
  287. struct drv_dev_and_id ddi = { drv, dev, id };
  288. /* Execute driver initialization on node where the device's
  289. bus is attached to. This way the driver likely allocates
  290. its local memory on the right node without any need to
  291. change it. */
  292. node = dev_to_node(&dev->dev);
  293. if (node >= 0) {
  294. int cpu;
  295. get_online_cpus();
  296. cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
  297. if (cpu < nr_cpu_ids)
  298. error = work_on_cpu(cpu, local_pci_probe, &ddi);
  299. else
  300. error = local_pci_probe(&ddi);
  301. put_online_cpus();
  302. } else
  303. error = local_pci_probe(&ddi);
  304. return error;
  305. }
  306. /**
  307. * __pci_device_probe - check if a driver wants to claim a specific PCI device
  308. * @drv: driver to call to check if it wants the PCI device
  309. * @pci_dev: PCI device being probed
  310. *
  311. * returns 0 on success, else error.
  312. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  313. */
  314. static int
  315. __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  316. {
  317. const struct pci_device_id *id;
  318. int error = 0;
  319. if (!pci_dev->driver && drv->probe) {
  320. error = -ENODEV;
  321. id = pci_match_device(drv, pci_dev);
  322. if (id)
  323. error = pci_call_probe(drv, pci_dev, id);
  324. if (error >= 0) {
  325. pci_dev->driver = drv;
  326. error = 0;
  327. }
  328. }
  329. return error;
  330. }
  331. static int pci_device_probe(struct device * dev)
  332. {
  333. int error = 0;
  334. struct pci_driver *drv;
  335. struct pci_dev *pci_dev;
  336. drv = to_pci_driver(dev->driver);
  337. pci_dev = to_pci_dev(dev);
  338. pci_dev_get(pci_dev);
  339. error = __pci_device_probe(drv, pci_dev);
  340. if (error)
  341. pci_dev_put(pci_dev);
  342. return error;
  343. }
  344. static int pci_device_remove(struct device * dev)
  345. {
  346. struct pci_dev * pci_dev = to_pci_dev(dev);
  347. struct pci_driver * drv = pci_dev->driver;
  348. if (drv) {
  349. if (drv->remove) {
  350. pm_runtime_get_sync(dev);
  351. drv->remove(pci_dev);
  352. pm_runtime_put_noidle(dev);
  353. }
  354. pci_dev->driver = NULL;
  355. }
  356. /* Undo the runtime PM settings in local_pci_probe() */
  357. pm_runtime_disable(dev);
  358. pm_runtime_set_suspended(dev);
  359. pm_runtime_put_noidle(dev);
  360. /*
  361. * If the device is still on, set the power state as "unknown",
  362. * since it might change by the next time we load the driver.
  363. */
  364. if (pci_dev->current_state == PCI_D0)
  365. pci_dev->current_state = PCI_UNKNOWN;
  366. /*
  367. * We would love to complain here if pci_dev->is_enabled is set, that
  368. * the driver should have called pci_disable_device(), but the
  369. * unfortunate fact is there are too many odd BIOS and bridge setups
  370. * that don't like drivers doing that all of the time.
  371. * Oh well, we can dream of sane hardware when we sleep, no matter how
  372. * horrible the crap we have to deal with is when we are awake...
  373. */
  374. pci_dev_put(pci_dev);
  375. return 0;
  376. }
  377. static void pci_device_shutdown(struct device *dev)
  378. {
  379. struct pci_dev *pci_dev = to_pci_dev(dev);
  380. struct pci_driver *drv = pci_dev->driver;
  381. if (drv && drv->shutdown)
  382. drv->shutdown(pci_dev);
  383. pci_msi_shutdown(pci_dev);
  384. pci_msix_shutdown(pci_dev);
  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_ON(ret && drv->driver.pm);
  524. return ret;
  525. }
  526. /* New power management framework */
  527. static int pci_pm_prepare(struct device *dev)
  528. {
  529. struct device_driver *drv = dev->driver;
  530. int error = 0;
  531. /*
  532. * PCI devices suspended at run time need to be resumed at this
  533. * point, because in general it is necessary to reconfigure them for
  534. * system suspend. Namely, if the device is supposed to wake up the
  535. * system from the sleep state, we may need to reconfigure it for this
  536. * purpose. In turn, if the device is not supposed to wake up the
  537. * system from the sleep state, we'll have to prevent it from signaling
  538. * wake-up.
  539. */
  540. pm_runtime_get_sync(dev);
  541. if (drv && drv->pm && drv->pm->prepare)
  542. error = drv->pm->prepare(dev);
  543. return error;
  544. }
  545. static void pci_pm_complete(struct device *dev)
  546. {
  547. struct device_driver *drv = dev->driver;
  548. if (drv && drv->pm && drv->pm->complete)
  549. drv->pm->complete(dev);
  550. pm_runtime_put_sync(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. if (pm->suspend) {
  568. pci_power_t prev = pci_dev->current_state;
  569. int error;
  570. error = pm->suspend(dev);
  571. suspend_report_result(pm->suspend, error);
  572. if (error)
  573. return error;
  574. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  575. && pci_dev->current_state != PCI_UNKNOWN) {
  576. WARN_ONCE(pci_dev->current_state != prev,
  577. "PCI PM: State of device not saved by %pF\n",
  578. pm->suspend);
  579. }
  580. }
  581. Fixup:
  582. pci_fixup_device(pci_fixup_suspend, pci_dev);
  583. return 0;
  584. }
  585. static int pci_pm_suspend_noirq(struct device *dev)
  586. {
  587. struct pci_dev *pci_dev = to_pci_dev(dev);
  588. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  589. if (pci_has_legacy_pm_support(pci_dev))
  590. return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
  591. if (!pm) {
  592. pci_save_state(pci_dev);
  593. return 0;
  594. }
  595. if (pm->suspend_noirq) {
  596. pci_power_t prev = pci_dev->current_state;
  597. int error;
  598. error = pm->suspend_noirq(dev);
  599. suspend_report_result(pm->suspend_noirq, error);
  600. if (error)
  601. return error;
  602. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  603. && pci_dev->current_state != PCI_UNKNOWN) {
  604. WARN_ONCE(pci_dev->current_state != prev,
  605. "PCI PM: State of device not saved by %pF\n",
  606. pm->suspend_noirq);
  607. return 0;
  608. }
  609. }
  610. if (!pci_dev->state_saved) {
  611. pci_save_state(pci_dev);
  612. if (!pci_is_bridge(pci_dev))
  613. pci_prepare_to_sleep(pci_dev);
  614. }
  615. pci_pm_set_unknown_state(pci_dev);
  616. return 0;
  617. }
  618. static int pci_pm_resume_noirq(struct device *dev)
  619. {
  620. struct pci_dev *pci_dev = to_pci_dev(dev);
  621. struct device_driver *drv = dev->driver;
  622. int error = 0;
  623. pci_pm_default_resume_early(pci_dev);
  624. if (pci_has_legacy_pm_support(pci_dev))
  625. return pci_legacy_resume_early(dev);
  626. if (drv && drv->pm && drv->pm->resume_noirq)
  627. error = drv->pm->resume_noirq(dev);
  628. return error;
  629. }
  630. static int pci_pm_resume(struct device *dev)
  631. {
  632. struct pci_dev *pci_dev = to_pci_dev(dev);
  633. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  634. int error = 0;
  635. /*
  636. * This is necessary for the suspend error path in which resume is
  637. * called without restoring the standard config registers of the device.
  638. */
  639. if (pci_dev->state_saved)
  640. pci_restore_standard_config(pci_dev);
  641. if (pci_has_legacy_pm_support(pci_dev))
  642. return pci_legacy_resume(dev);
  643. pci_pm_default_resume(pci_dev);
  644. if (pm) {
  645. if (pm->resume)
  646. error = pm->resume(dev);
  647. } else {
  648. pci_pm_reenable_device(pci_dev);
  649. }
  650. return error;
  651. }
  652. #else /* !CONFIG_SUSPEND */
  653. #define pci_pm_suspend NULL
  654. #define pci_pm_suspend_noirq NULL
  655. #define pci_pm_resume NULL
  656. #define pci_pm_resume_noirq NULL
  657. #endif /* !CONFIG_SUSPEND */
  658. #ifdef CONFIG_HIBERNATE_CALLBACKS
  659. static int pci_pm_freeze(struct device *dev)
  660. {
  661. struct pci_dev *pci_dev = to_pci_dev(dev);
  662. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  663. if (pci_has_legacy_pm_support(pci_dev))
  664. return pci_legacy_suspend(dev, PMSG_FREEZE);
  665. if (!pm) {
  666. pci_pm_default_suspend(pci_dev);
  667. return 0;
  668. }
  669. if (pm->freeze) {
  670. int error;
  671. error = pm->freeze(dev);
  672. suspend_report_result(pm->freeze, error);
  673. if (error)
  674. return error;
  675. }
  676. return 0;
  677. }
  678. static int pci_pm_freeze_noirq(struct device *dev)
  679. {
  680. struct pci_dev *pci_dev = to_pci_dev(dev);
  681. struct device_driver *drv = dev->driver;
  682. if (pci_has_legacy_pm_support(pci_dev))
  683. return pci_legacy_suspend_late(dev, PMSG_FREEZE);
  684. if (drv && drv->pm && drv->pm->freeze_noirq) {
  685. int error;
  686. error = drv->pm->freeze_noirq(dev);
  687. suspend_report_result(drv->pm->freeze_noirq, error);
  688. if (error)
  689. return error;
  690. }
  691. if (!pci_dev->state_saved)
  692. pci_save_state(pci_dev);
  693. pci_pm_set_unknown_state(pci_dev);
  694. return 0;
  695. }
  696. static int pci_pm_thaw_noirq(struct device *dev)
  697. {
  698. struct pci_dev *pci_dev = to_pci_dev(dev);
  699. struct device_driver *drv = dev->driver;
  700. int error = 0;
  701. if (pci_has_legacy_pm_support(pci_dev))
  702. return pci_legacy_resume_early(dev);
  703. pci_update_current_state(pci_dev, PCI_D0);
  704. if (drv && drv->pm && drv->pm->thaw_noirq)
  705. error = drv->pm->thaw_noirq(dev);
  706. return error;
  707. }
  708. static int pci_pm_thaw(struct device *dev)
  709. {
  710. struct pci_dev *pci_dev = to_pci_dev(dev);
  711. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  712. int error = 0;
  713. if (pci_has_legacy_pm_support(pci_dev))
  714. return pci_legacy_resume(dev);
  715. if (pm) {
  716. if (pm->thaw)
  717. error = pm->thaw(dev);
  718. } else {
  719. pci_pm_reenable_device(pci_dev);
  720. }
  721. pci_dev->state_saved = false;
  722. return error;
  723. }
  724. static int pci_pm_poweroff(struct device *dev)
  725. {
  726. struct pci_dev *pci_dev = to_pci_dev(dev);
  727. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  728. if (pci_has_legacy_pm_support(pci_dev))
  729. return pci_legacy_suspend(dev, PMSG_HIBERNATE);
  730. if (!pm) {
  731. pci_pm_default_suspend(pci_dev);
  732. goto Fixup;
  733. }
  734. if (pm->poweroff) {
  735. int error;
  736. error = pm->poweroff(dev);
  737. suspend_report_result(pm->poweroff, error);
  738. if (error)
  739. return error;
  740. }
  741. Fixup:
  742. pci_fixup_device(pci_fixup_suspend, pci_dev);
  743. return 0;
  744. }
  745. static int pci_pm_poweroff_noirq(struct device *dev)
  746. {
  747. struct pci_dev *pci_dev = to_pci_dev(dev);
  748. struct device_driver *drv = dev->driver;
  749. if (pci_has_legacy_pm_support(to_pci_dev(dev)))
  750. return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
  751. if (!drv || !drv->pm)
  752. return 0;
  753. if (drv->pm->poweroff_noirq) {
  754. int error;
  755. error = drv->pm->poweroff_noirq(dev);
  756. suspend_report_result(drv->pm->poweroff_noirq, error);
  757. if (error)
  758. return error;
  759. }
  760. if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
  761. pci_prepare_to_sleep(pci_dev);
  762. return 0;
  763. }
  764. static int pci_pm_restore_noirq(struct device *dev)
  765. {
  766. struct pci_dev *pci_dev = to_pci_dev(dev);
  767. struct device_driver *drv = dev->driver;
  768. int error = 0;
  769. pci_pm_default_resume_early(pci_dev);
  770. if (pci_has_legacy_pm_support(pci_dev))
  771. return pci_legacy_resume_early(dev);
  772. if (drv && drv->pm && drv->pm->restore_noirq)
  773. error = drv->pm->restore_noirq(dev);
  774. return error;
  775. }
  776. static int pci_pm_restore(struct device *dev)
  777. {
  778. struct pci_dev *pci_dev = to_pci_dev(dev);
  779. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  780. int error = 0;
  781. /*
  782. * This is necessary for the hibernation error path in which restore is
  783. * called without restoring the standard config registers of the device.
  784. */
  785. if (pci_dev->state_saved)
  786. pci_restore_standard_config(pci_dev);
  787. if (pci_has_legacy_pm_support(pci_dev))
  788. return pci_legacy_resume(dev);
  789. pci_pm_default_resume(pci_dev);
  790. if (pm) {
  791. if (pm->restore)
  792. error = pm->restore(dev);
  793. } else {
  794. pci_pm_reenable_device(pci_dev);
  795. }
  796. return error;
  797. }
  798. #else /* !CONFIG_HIBERNATE_CALLBACKS */
  799. #define pci_pm_freeze NULL
  800. #define pci_pm_freeze_noirq NULL
  801. #define pci_pm_thaw NULL
  802. #define pci_pm_thaw_noirq NULL
  803. #define pci_pm_poweroff NULL
  804. #define pci_pm_poweroff_noirq NULL
  805. #define pci_pm_restore NULL
  806. #define pci_pm_restore_noirq NULL
  807. #endif /* !CONFIG_HIBERNATE_CALLBACKS */
  808. #ifdef CONFIG_PM_RUNTIME
  809. static int pci_pm_runtime_suspend(struct device *dev)
  810. {
  811. struct pci_dev *pci_dev = to_pci_dev(dev);
  812. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  813. pci_power_t prev = pci_dev->current_state;
  814. int error;
  815. if (!pm || !pm->runtime_suspend)
  816. return -ENOSYS;
  817. error = pm->runtime_suspend(dev);
  818. suspend_report_result(pm->runtime_suspend, error);
  819. if (error)
  820. return error;
  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. struct pci_dev *pci_dev = to_pci_dev(dev);
  837. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  838. if (!pm || !pm->runtime_resume)
  839. return -ENOSYS;
  840. pci_pm_default_resume_early(pci_dev);
  841. __pci_enable_wake(pci_dev, PCI_D0, true, false);
  842. pci_fixup_device(pci_fixup_resume, pci_dev);
  843. return pm->runtime_resume(dev);
  844. }
  845. static int pci_pm_runtime_idle(struct device *dev)
  846. {
  847. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  848. if (!pm)
  849. return -ENOSYS;
  850. if (pm->runtime_idle) {
  851. int ret = pm->runtime_idle(dev);
  852. if (ret)
  853. return ret;
  854. }
  855. pm_runtime_suspend(dev);
  856. return 0;
  857. }
  858. #else /* !CONFIG_PM_RUNTIME */
  859. #define pci_pm_runtime_suspend NULL
  860. #define pci_pm_runtime_resume NULL
  861. #define pci_pm_runtime_idle NULL
  862. #endif /* !CONFIG_PM_RUNTIME */
  863. #ifdef CONFIG_PM
  864. const struct dev_pm_ops pci_dev_pm_ops = {
  865. .prepare = pci_pm_prepare,
  866. .complete = pci_pm_complete,
  867. .suspend = pci_pm_suspend,
  868. .resume = pci_pm_resume,
  869. .freeze = pci_pm_freeze,
  870. .thaw = pci_pm_thaw,
  871. .poweroff = pci_pm_poweroff,
  872. .restore = pci_pm_restore,
  873. .suspend_noirq = pci_pm_suspend_noirq,
  874. .resume_noirq = pci_pm_resume_noirq,
  875. .freeze_noirq = pci_pm_freeze_noirq,
  876. .thaw_noirq = pci_pm_thaw_noirq,
  877. .poweroff_noirq = pci_pm_poweroff_noirq,
  878. .restore_noirq = pci_pm_restore_noirq,
  879. .runtime_suspend = pci_pm_runtime_suspend,
  880. .runtime_resume = pci_pm_runtime_resume,
  881. .runtime_idle = pci_pm_runtime_idle,
  882. };
  883. #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
  884. #else /* !COMFIG_PM_OPS */
  885. #define PCI_PM_OPS_PTR NULL
  886. #endif /* !COMFIG_PM_OPS */
  887. /**
  888. * __pci_register_driver - register a new pci driver
  889. * @drv: the driver structure to register
  890. * @owner: owner module of drv
  891. * @mod_name: module name string
  892. *
  893. * Adds the driver structure to the list of registered drivers.
  894. * Returns a negative value on error, otherwise 0.
  895. * If no error occurred, the driver remains registered even if
  896. * no device was claimed during registration.
  897. */
  898. int __pci_register_driver(struct pci_driver *drv, struct module *owner,
  899. const char *mod_name)
  900. {
  901. int error;
  902. /* initialize common driver fields */
  903. drv->driver.name = drv->name;
  904. drv->driver.bus = &pci_bus_type;
  905. drv->driver.owner = owner;
  906. drv->driver.mod_name = mod_name;
  907. spin_lock_init(&drv->dynids.lock);
  908. INIT_LIST_HEAD(&drv->dynids.list);
  909. /* register with core */
  910. error = driver_register(&drv->driver);
  911. if (error)
  912. goto out;
  913. error = pci_create_newid_file(drv);
  914. if (error)
  915. goto out_newid;
  916. error = pci_create_removeid_file(drv);
  917. if (error)
  918. goto out_removeid;
  919. out:
  920. return error;
  921. out_removeid:
  922. pci_remove_newid_file(drv);
  923. out_newid:
  924. driver_unregister(&drv->driver);
  925. goto out;
  926. }
  927. /**
  928. * pci_unregister_driver - unregister a pci driver
  929. * @drv: the driver structure to unregister
  930. *
  931. * Deletes the driver structure from the list of registered PCI drivers,
  932. * gives it a chance to clean up by calling its remove() function for
  933. * each device it was responsible for, and marks those devices as
  934. * driverless.
  935. */
  936. void
  937. pci_unregister_driver(struct pci_driver *drv)
  938. {
  939. pci_remove_removeid_file(drv);
  940. pci_remove_newid_file(drv);
  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. .pm = PCI_PM_OPS_PTR,
  1032. };
  1033. static int __init pci_driver_init(void)
  1034. {
  1035. return bus_register(&pci_bus_type);
  1036. }
  1037. postcore_initcall(pci_driver_init);
  1038. EXPORT_SYMBOL_GPL(pci_add_dynid);
  1039. EXPORT_SYMBOL(pci_match_id);
  1040. EXPORT_SYMBOL(__pci_register_driver);
  1041. EXPORT_SYMBOL(pci_unregister_driver);
  1042. EXPORT_SYMBOL(pci_dev_driver);
  1043. EXPORT_SYMBOL(pci_bus_type);
  1044. EXPORT_SYMBOL(pci_dev_get);
  1045. EXPORT_SYMBOL(pci_dev_put);