pci-driver.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. return ddi->drv->probe(ddi->dev, ddi->id);
  265. }
  266. static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
  267. const struct pci_device_id *id)
  268. {
  269. int error, node;
  270. struct drv_dev_and_id ddi = { drv, dev, id };
  271. /* Execute driver initialization on node where the device's
  272. bus is attached to. This way the driver likely allocates
  273. its local memory on the right node without any need to
  274. change it. */
  275. node = dev_to_node(&dev->dev);
  276. if (node >= 0) {
  277. int cpu;
  278. get_online_cpus();
  279. cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
  280. if (cpu < nr_cpu_ids)
  281. error = work_on_cpu(cpu, local_pci_probe, &ddi);
  282. else
  283. error = local_pci_probe(&ddi);
  284. put_online_cpus();
  285. } else
  286. error = local_pci_probe(&ddi);
  287. return error;
  288. }
  289. /**
  290. * __pci_device_probe()
  291. * @drv: driver to call to check if it wants the PCI device
  292. * @pci_dev: PCI device being probed
  293. *
  294. * returns 0 on success, else error.
  295. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  296. */
  297. static int
  298. __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  299. {
  300. const struct pci_device_id *id;
  301. int error = 0;
  302. if (!pci_dev->driver && drv->probe) {
  303. error = -ENODEV;
  304. id = pci_match_device(drv, pci_dev);
  305. if (id)
  306. error = pci_call_probe(drv, pci_dev, id);
  307. if (error >= 0) {
  308. pci_dev->driver = drv;
  309. error = 0;
  310. }
  311. }
  312. return error;
  313. }
  314. static int pci_device_probe(struct device * dev)
  315. {
  316. int error = 0;
  317. struct pci_driver *drv;
  318. struct pci_dev *pci_dev;
  319. drv = to_pci_driver(dev->driver);
  320. pci_dev = to_pci_dev(dev);
  321. pci_dev_get(pci_dev);
  322. error = __pci_device_probe(drv, pci_dev);
  323. if (error)
  324. pci_dev_put(pci_dev);
  325. return error;
  326. }
  327. static int pci_device_remove(struct device * dev)
  328. {
  329. struct pci_dev * pci_dev = to_pci_dev(dev);
  330. struct pci_driver * drv = pci_dev->driver;
  331. if (drv) {
  332. if (drv->remove)
  333. drv->remove(pci_dev);
  334. pci_dev->driver = NULL;
  335. }
  336. /*
  337. * If the device is still on, set the power state as "unknown",
  338. * since it might change by the next time we load the driver.
  339. */
  340. if (pci_dev->current_state == PCI_D0)
  341. pci_dev->current_state = PCI_UNKNOWN;
  342. /*
  343. * We would love to complain here if pci_dev->is_enabled is set, that
  344. * the driver should have called pci_disable_device(), but the
  345. * unfortunate fact is there are too many odd BIOS and bridge setups
  346. * that don't like drivers doing that all of the time.
  347. * Oh well, we can dream of sane hardware when we sleep, no matter how
  348. * horrible the crap we have to deal with is when we are awake...
  349. */
  350. pci_dev_put(pci_dev);
  351. return 0;
  352. }
  353. static void pci_device_shutdown(struct device *dev)
  354. {
  355. struct pci_dev *pci_dev = to_pci_dev(dev);
  356. struct pci_driver *drv = pci_dev->driver;
  357. if (drv && drv->shutdown)
  358. drv->shutdown(pci_dev);
  359. pci_msi_shutdown(pci_dev);
  360. pci_msix_shutdown(pci_dev);
  361. }
  362. #ifdef CONFIG_PM_OPS
  363. /* Auxiliary functions used for system resume and run-time resume. */
  364. /**
  365. * pci_restore_standard_config - restore standard config registers of PCI device
  366. * @pci_dev: PCI device to handle
  367. */
  368. static int pci_restore_standard_config(struct pci_dev *pci_dev)
  369. {
  370. pci_update_current_state(pci_dev, PCI_UNKNOWN);
  371. if (pci_dev->current_state != PCI_D0) {
  372. int error = pci_set_power_state(pci_dev, PCI_D0);
  373. if (error)
  374. return error;
  375. }
  376. return pci_restore_state(pci_dev);
  377. }
  378. static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
  379. {
  380. pci_restore_standard_config(pci_dev);
  381. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  382. }
  383. #endif
  384. #ifdef CONFIG_PM_SLEEP
  385. /*
  386. * Default "suspend" method for devices that have no driver provided suspend,
  387. * or not even a driver at all (second part).
  388. */
  389. static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
  390. {
  391. /*
  392. * mark its power state as "unknown", since we don't know if
  393. * e.g. the BIOS will change its device state when we suspend.
  394. */
  395. if (pci_dev->current_state == PCI_D0)
  396. pci_dev->current_state = PCI_UNKNOWN;
  397. }
  398. /*
  399. * Default "resume" method for devices that have no driver provided resume,
  400. * or not even a driver at all (second part).
  401. */
  402. static int pci_pm_reenable_device(struct pci_dev *pci_dev)
  403. {
  404. int retval;
  405. /* if the device was enabled before suspend, reenable */
  406. retval = pci_reenable_device(pci_dev);
  407. /*
  408. * if the device was busmaster before the suspend, make it busmaster
  409. * again
  410. */
  411. if (pci_dev->is_busmaster)
  412. pci_set_master(pci_dev);
  413. return retval;
  414. }
  415. static int pci_legacy_suspend(struct device *dev, pm_message_t state)
  416. {
  417. struct pci_dev * pci_dev = to_pci_dev(dev);
  418. struct pci_driver * drv = pci_dev->driver;
  419. if (drv && drv->suspend) {
  420. pci_power_t prev = pci_dev->current_state;
  421. int error;
  422. error = drv->suspend(pci_dev, state);
  423. suspend_report_result(drv->suspend, error);
  424. if (error)
  425. return error;
  426. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  427. && pci_dev->current_state != PCI_UNKNOWN) {
  428. WARN_ONCE(pci_dev->current_state != prev,
  429. "PCI PM: Device state not saved by %pF\n",
  430. drv->suspend);
  431. }
  432. }
  433. pci_fixup_device(pci_fixup_suspend, pci_dev);
  434. return 0;
  435. }
  436. static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
  437. {
  438. struct pci_dev * pci_dev = to_pci_dev(dev);
  439. struct pci_driver * drv = pci_dev->driver;
  440. if (drv && drv->suspend_late) {
  441. pci_power_t prev = pci_dev->current_state;
  442. int error;
  443. error = drv->suspend_late(pci_dev, state);
  444. suspend_report_result(drv->suspend_late, error);
  445. if (error)
  446. return error;
  447. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  448. && pci_dev->current_state != PCI_UNKNOWN) {
  449. WARN_ONCE(pci_dev->current_state != prev,
  450. "PCI PM: Device state not saved by %pF\n",
  451. drv->suspend_late);
  452. return 0;
  453. }
  454. }
  455. if (!pci_dev->state_saved)
  456. pci_save_state(pci_dev);
  457. pci_pm_set_unknown_state(pci_dev);
  458. return 0;
  459. }
  460. static int pci_legacy_resume_early(struct device *dev)
  461. {
  462. struct pci_dev * pci_dev = to_pci_dev(dev);
  463. struct pci_driver * drv = pci_dev->driver;
  464. return drv && drv->resume_early ?
  465. drv->resume_early(pci_dev) : 0;
  466. }
  467. static int pci_legacy_resume(struct device *dev)
  468. {
  469. struct pci_dev * pci_dev = to_pci_dev(dev);
  470. struct pci_driver * drv = pci_dev->driver;
  471. pci_fixup_device(pci_fixup_resume, pci_dev);
  472. return drv && drv->resume ?
  473. drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
  474. }
  475. /* Auxiliary functions used by the new power management framework */
  476. static void pci_pm_default_resume(struct pci_dev *pci_dev)
  477. {
  478. pci_fixup_device(pci_fixup_resume, pci_dev);
  479. if (!pci_is_bridge(pci_dev))
  480. pci_enable_wake(pci_dev, PCI_D0, false);
  481. }
  482. static void pci_pm_default_suspend(struct pci_dev *pci_dev)
  483. {
  484. /* Disable non-bridge devices without PM support */
  485. if (!pci_is_bridge(pci_dev))
  486. pci_disable_enabled_device(pci_dev);
  487. }
  488. static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
  489. {
  490. struct pci_driver *drv = pci_dev->driver;
  491. bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
  492. || drv->resume_early);
  493. /*
  494. * Legacy PM support is used by default, so warn if the new framework is
  495. * supported as well. Drivers are supposed to support either the
  496. * former, or the latter, but not both at the same time.
  497. */
  498. WARN_ON(ret && drv->driver.pm);
  499. return ret;
  500. }
  501. /* New power management framework */
  502. static int pci_pm_prepare(struct device *dev)
  503. {
  504. struct device_driver *drv = dev->driver;
  505. int error = 0;
  506. /*
  507. * PCI devices suspended at run time need to be resumed at this
  508. * point, because in general it is necessary to reconfigure them for
  509. * system suspend. Namely, if the device is supposed to wake up the
  510. * system from the sleep state, we may need to reconfigure it for this
  511. * purpose. In turn, if the device is not supposed to wake up the
  512. * system from the sleep state, we'll have to prevent it from signaling
  513. * wake-up.
  514. */
  515. pm_runtime_resume(dev);
  516. if (drv && drv->pm && drv->pm->prepare)
  517. error = drv->pm->prepare(dev);
  518. return error;
  519. }
  520. static void pci_pm_complete(struct device *dev)
  521. {
  522. struct device_driver *drv = dev->driver;
  523. if (drv && drv->pm && drv->pm->complete)
  524. drv->pm->complete(dev);
  525. }
  526. #else /* !CONFIG_PM_SLEEP */
  527. #define pci_pm_prepare NULL
  528. #define pci_pm_complete NULL
  529. #endif /* !CONFIG_PM_SLEEP */
  530. #ifdef CONFIG_SUSPEND
  531. static int pci_pm_suspend(struct device *dev)
  532. {
  533. struct pci_dev *pci_dev = to_pci_dev(dev);
  534. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  535. if (pci_has_legacy_pm_support(pci_dev))
  536. return pci_legacy_suspend(dev, PMSG_SUSPEND);
  537. if (!pm) {
  538. pci_pm_default_suspend(pci_dev);
  539. goto Fixup;
  540. }
  541. if (pm->suspend) {
  542. pci_power_t prev = pci_dev->current_state;
  543. int error;
  544. error = pm->suspend(dev);
  545. suspend_report_result(pm->suspend, error);
  546. if (error)
  547. return error;
  548. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  549. && pci_dev->current_state != PCI_UNKNOWN) {
  550. WARN_ONCE(pci_dev->current_state != prev,
  551. "PCI PM: State of device not saved by %pF\n",
  552. pm->suspend);
  553. }
  554. }
  555. Fixup:
  556. pci_fixup_device(pci_fixup_suspend, pci_dev);
  557. return 0;
  558. }
  559. static int pci_pm_suspend_noirq(struct device *dev)
  560. {
  561. struct pci_dev *pci_dev = to_pci_dev(dev);
  562. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  563. if (pci_has_legacy_pm_support(pci_dev))
  564. return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
  565. if (!pm) {
  566. pci_save_state(pci_dev);
  567. return 0;
  568. }
  569. if (pm->suspend_noirq) {
  570. pci_power_t prev = pci_dev->current_state;
  571. int error;
  572. error = pm->suspend_noirq(dev);
  573. suspend_report_result(pm->suspend_noirq, error);
  574. if (error)
  575. return error;
  576. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  577. && pci_dev->current_state != PCI_UNKNOWN) {
  578. WARN_ONCE(pci_dev->current_state != prev,
  579. "PCI PM: State of device not saved by %pF\n",
  580. pm->suspend_noirq);
  581. return 0;
  582. }
  583. }
  584. if (!pci_dev->state_saved) {
  585. pci_save_state(pci_dev);
  586. if (!pci_is_bridge(pci_dev))
  587. pci_prepare_to_sleep(pci_dev);
  588. }
  589. pci_pm_set_unknown_state(pci_dev);
  590. return 0;
  591. }
  592. static int pci_pm_resume_noirq(struct device *dev)
  593. {
  594. struct pci_dev *pci_dev = to_pci_dev(dev);
  595. struct device_driver *drv = dev->driver;
  596. int error = 0;
  597. pci_pm_default_resume_early(pci_dev);
  598. if (pci_has_legacy_pm_support(pci_dev))
  599. return pci_legacy_resume_early(dev);
  600. if (drv && drv->pm && drv->pm->resume_noirq)
  601. error = drv->pm->resume_noirq(dev);
  602. return error;
  603. }
  604. static int pci_pm_resume(struct device *dev)
  605. {
  606. struct pci_dev *pci_dev = to_pci_dev(dev);
  607. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  608. int error = 0;
  609. /*
  610. * This is necessary for the suspend error path in which resume is
  611. * called without restoring the standard config registers of the device.
  612. */
  613. if (pci_dev->state_saved)
  614. pci_restore_standard_config(pci_dev);
  615. if (pci_has_legacy_pm_support(pci_dev))
  616. return pci_legacy_resume(dev);
  617. pci_pm_default_resume(pci_dev);
  618. if (pm) {
  619. if (pm->resume)
  620. error = pm->resume(dev);
  621. } else {
  622. pci_pm_reenable_device(pci_dev);
  623. }
  624. return error;
  625. }
  626. #else /* !CONFIG_SUSPEND */
  627. #define pci_pm_suspend NULL
  628. #define pci_pm_suspend_noirq NULL
  629. #define pci_pm_resume NULL
  630. #define pci_pm_resume_noirq NULL
  631. #endif /* !CONFIG_SUSPEND */
  632. #ifdef CONFIG_HIBERNATION
  633. static int pci_pm_freeze(struct device *dev)
  634. {
  635. struct pci_dev *pci_dev = to_pci_dev(dev);
  636. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  637. if (pci_has_legacy_pm_support(pci_dev))
  638. return pci_legacy_suspend(dev, PMSG_FREEZE);
  639. if (!pm) {
  640. pci_pm_default_suspend(pci_dev);
  641. return 0;
  642. }
  643. if (pm->freeze) {
  644. int error;
  645. error = pm->freeze(dev);
  646. suspend_report_result(pm->freeze, error);
  647. if (error)
  648. return error;
  649. }
  650. return 0;
  651. }
  652. static int pci_pm_freeze_noirq(struct device *dev)
  653. {
  654. struct pci_dev *pci_dev = to_pci_dev(dev);
  655. struct device_driver *drv = dev->driver;
  656. if (pci_has_legacy_pm_support(pci_dev))
  657. return pci_legacy_suspend_late(dev, PMSG_FREEZE);
  658. if (drv && drv->pm && drv->pm->freeze_noirq) {
  659. int error;
  660. error = drv->pm->freeze_noirq(dev);
  661. suspend_report_result(drv->pm->freeze_noirq, error);
  662. if (error)
  663. return error;
  664. }
  665. if (!pci_dev->state_saved)
  666. pci_save_state(pci_dev);
  667. pci_pm_set_unknown_state(pci_dev);
  668. return 0;
  669. }
  670. static int pci_pm_thaw_noirq(struct device *dev)
  671. {
  672. struct pci_dev *pci_dev = to_pci_dev(dev);
  673. struct device_driver *drv = dev->driver;
  674. int error = 0;
  675. if (pci_has_legacy_pm_support(pci_dev))
  676. return pci_legacy_resume_early(dev);
  677. pci_update_current_state(pci_dev, PCI_D0);
  678. if (drv && drv->pm && drv->pm->thaw_noirq)
  679. error = drv->pm->thaw_noirq(dev);
  680. return error;
  681. }
  682. static int pci_pm_thaw(struct device *dev)
  683. {
  684. struct pci_dev *pci_dev = to_pci_dev(dev);
  685. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  686. int error = 0;
  687. if (pci_has_legacy_pm_support(pci_dev))
  688. return pci_legacy_resume(dev);
  689. if (pm) {
  690. if (pm->thaw)
  691. error = pm->thaw(dev);
  692. } else {
  693. pci_pm_reenable_device(pci_dev);
  694. }
  695. pci_dev->state_saved = false;
  696. return error;
  697. }
  698. static int pci_pm_poweroff(struct device *dev)
  699. {
  700. struct pci_dev *pci_dev = to_pci_dev(dev);
  701. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  702. if (pci_has_legacy_pm_support(pci_dev))
  703. return pci_legacy_suspend(dev, PMSG_HIBERNATE);
  704. if (!pm) {
  705. pci_pm_default_suspend(pci_dev);
  706. goto Fixup;
  707. }
  708. if (pm->poweroff) {
  709. int error;
  710. error = pm->poweroff(dev);
  711. suspend_report_result(pm->poweroff, error);
  712. if (error)
  713. return error;
  714. }
  715. Fixup:
  716. pci_fixup_device(pci_fixup_suspend, pci_dev);
  717. return 0;
  718. }
  719. static int pci_pm_poweroff_noirq(struct device *dev)
  720. {
  721. struct pci_dev *pci_dev = to_pci_dev(dev);
  722. struct device_driver *drv = dev->driver;
  723. if (pci_has_legacy_pm_support(to_pci_dev(dev)))
  724. return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
  725. if (!drv || !drv->pm)
  726. return 0;
  727. if (drv->pm->poweroff_noirq) {
  728. int error;
  729. error = drv->pm->poweroff_noirq(dev);
  730. suspend_report_result(drv->pm->poweroff_noirq, error);
  731. if (error)
  732. return error;
  733. }
  734. if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
  735. pci_prepare_to_sleep(pci_dev);
  736. return 0;
  737. }
  738. static int pci_pm_restore_noirq(struct device *dev)
  739. {
  740. struct pci_dev *pci_dev = to_pci_dev(dev);
  741. struct device_driver *drv = dev->driver;
  742. int error = 0;
  743. pci_pm_default_resume_early(pci_dev);
  744. if (pci_has_legacy_pm_support(pci_dev))
  745. return pci_legacy_resume_early(dev);
  746. if (drv && drv->pm && drv->pm->restore_noirq)
  747. error = drv->pm->restore_noirq(dev);
  748. return error;
  749. }
  750. static int pci_pm_restore(struct device *dev)
  751. {
  752. struct pci_dev *pci_dev = to_pci_dev(dev);
  753. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  754. int error = 0;
  755. /*
  756. * This is necessary for the hibernation error path in which restore is
  757. * called without restoring the standard config registers of the device.
  758. */
  759. if (pci_dev->state_saved)
  760. pci_restore_standard_config(pci_dev);
  761. if (pci_has_legacy_pm_support(pci_dev))
  762. return pci_legacy_resume(dev);
  763. pci_pm_default_resume(pci_dev);
  764. if (pm) {
  765. if (pm->restore)
  766. error = pm->restore(dev);
  767. } else {
  768. pci_pm_reenable_device(pci_dev);
  769. }
  770. return error;
  771. }
  772. #else /* !CONFIG_HIBERNATION */
  773. #define pci_pm_freeze NULL
  774. #define pci_pm_freeze_noirq NULL
  775. #define pci_pm_thaw NULL
  776. #define pci_pm_thaw_noirq NULL
  777. #define pci_pm_poweroff NULL
  778. #define pci_pm_poweroff_noirq NULL
  779. #define pci_pm_restore NULL
  780. #define pci_pm_restore_noirq NULL
  781. #endif /* !CONFIG_HIBERNATION */
  782. #ifdef CONFIG_PM_RUNTIME
  783. static int pci_pm_runtime_suspend(struct device *dev)
  784. {
  785. struct pci_dev *pci_dev = to_pci_dev(dev);
  786. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  787. pci_power_t prev = pci_dev->current_state;
  788. int error;
  789. if (!pm || !pm->runtime_suspend)
  790. return -ENOSYS;
  791. error = pm->runtime_suspend(dev);
  792. suspend_report_result(pm->runtime_suspend, error);
  793. if (error)
  794. return error;
  795. pci_fixup_device(pci_fixup_suspend, pci_dev);
  796. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  797. && pci_dev->current_state != PCI_UNKNOWN) {
  798. WARN_ONCE(pci_dev->current_state != prev,
  799. "PCI PM: State of device not saved by %pF\n",
  800. pm->runtime_suspend);
  801. return 0;
  802. }
  803. if (!pci_dev->state_saved)
  804. pci_save_state(pci_dev);
  805. pci_finish_runtime_suspend(pci_dev);
  806. return 0;
  807. }
  808. static int pci_pm_runtime_resume(struct device *dev)
  809. {
  810. struct pci_dev *pci_dev = to_pci_dev(dev);
  811. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  812. if (!pm || !pm->runtime_resume)
  813. return -ENOSYS;
  814. pci_pm_default_resume_early(pci_dev);
  815. __pci_enable_wake(pci_dev, PCI_D0, true, false);
  816. pci_fixup_device(pci_fixup_resume, pci_dev);
  817. return pm->runtime_resume(dev);
  818. }
  819. static int pci_pm_runtime_idle(struct device *dev)
  820. {
  821. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  822. if (!pm)
  823. return -ENOSYS;
  824. if (pm->runtime_idle) {
  825. int ret = pm->runtime_idle(dev);
  826. if (ret)
  827. return ret;
  828. }
  829. pm_runtime_suspend(dev);
  830. return 0;
  831. }
  832. #else /* !CONFIG_PM_RUNTIME */
  833. #define pci_pm_runtime_suspend NULL
  834. #define pci_pm_runtime_resume NULL
  835. #define pci_pm_runtime_idle NULL
  836. #endif /* !CONFIG_PM_RUNTIME */
  837. #ifdef CONFIG_PM_OPS
  838. const struct dev_pm_ops pci_dev_pm_ops = {
  839. .prepare = pci_pm_prepare,
  840. .complete = pci_pm_complete,
  841. .suspend = pci_pm_suspend,
  842. .resume = pci_pm_resume,
  843. .freeze = pci_pm_freeze,
  844. .thaw = pci_pm_thaw,
  845. .poweroff = pci_pm_poweroff,
  846. .restore = pci_pm_restore,
  847. .suspend_noirq = pci_pm_suspend_noirq,
  848. .resume_noirq = pci_pm_resume_noirq,
  849. .freeze_noirq = pci_pm_freeze_noirq,
  850. .thaw_noirq = pci_pm_thaw_noirq,
  851. .poweroff_noirq = pci_pm_poweroff_noirq,
  852. .restore_noirq = pci_pm_restore_noirq,
  853. .runtime_suspend = pci_pm_runtime_suspend,
  854. .runtime_resume = pci_pm_runtime_resume,
  855. .runtime_idle = pci_pm_runtime_idle,
  856. };
  857. #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
  858. #else /* !COMFIG_PM_OPS */
  859. #define PCI_PM_OPS_PTR NULL
  860. #endif /* !COMFIG_PM_OPS */
  861. /**
  862. * __pci_register_driver - register a new pci driver
  863. * @drv: the driver structure to register
  864. * @owner: owner module of drv
  865. * @mod_name: module name string
  866. *
  867. * Adds the driver structure to the list of registered drivers.
  868. * Returns a negative value on error, otherwise 0.
  869. * If no error occurred, the driver remains registered even if
  870. * no device was claimed during registration.
  871. */
  872. int __pci_register_driver(struct pci_driver *drv, struct module *owner,
  873. const char *mod_name)
  874. {
  875. int error;
  876. /* initialize common driver fields */
  877. drv->driver.name = drv->name;
  878. drv->driver.bus = &pci_bus_type;
  879. drv->driver.owner = owner;
  880. drv->driver.mod_name = mod_name;
  881. spin_lock_init(&drv->dynids.lock);
  882. INIT_LIST_HEAD(&drv->dynids.list);
  883. /* register with core */
  884. error = driver_register(&drv->driver);
  885. if (error)
  886. goto out;
  887. error = pci_create_newid_file(drv);
  888. if (error)
  889. goto out_newid;
  890. error = pci_create_removeid_file(drv);
  891. if (error)
  892. goto out_removeid;
  893. out:
  894. return error;
  895. out_removeid:
  896. pci_remove_newid_file(drv);
  897. out_newid:
  898. driver_unregister(&drv->driver);
  899. goto out;
  900. }
  901. /**
  902. * pci_unregister_driver - unregister a pci driver
  903. * @drv: the driver structure to unregister
  904. *
  905. * Deletes the driver structure from the list of registered PCI drivers,
  906. * gives it a chance to clean up by calling its remove() function for
  907. * each device it was responsible for, and marks those devices as
  908. * driverless.
  909. */
  910. void
  911. pci_unregister_driver(struct pci_driver *drv)
  912. {
  913. pci_remove_removeid_file(drv);
  914. pci_remove_newid_file(drv);
  915. driver_unregister(&drv->driver);
  916. pci_free_dynids(drv);
  917. }
  918. static struct pci_driver pci_compat_driver = {
  919. .name = "compat"
  920. };
  921. /**
  922. * pci_dev_driver - get the pci_driver of a device
  923. * @dev: the device to query
  924. *
  925. * Returns the appropriate pci_driver structure or %NULL if there is no
  926. * registered driver for the device.
  927. */
  928. struct pci_driver *
  929. pci_dev_driver(const struct pci_dev *dev)
  930. {
  931. if (dev->driver)
  932. return dev->driver;
  933. else {
  934. int i;
  935. for(i=0; i<=PCI_ROM_RESOURCE; i++)
  936. if (dev->resource[i].flags & IORESOURCE_BUSY)
  937. return &pci_compat_driver;
  938. }
  939. return NULL;
  940. }
  941. /**
  942. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  943. * @dev: the PCI device structure to match against
  944. * @drv: the device driver to search for matching PCI device id structures
  945. *
  946. * Used by a driver to check whether a PCI device present in the
  947. * system is in its list of supported devices. Returns the matching
  948. * pci_device_id structure or %NULL if there is no match.
  949. */
  950. static int pci_bus_match(struct device *dev, struct device_driver *drv)
  951. {
  952. struct pci_dev *pci_dev = to_pci_dev(dev);
  953. struct pci_driver *pci_drv = to_pci_driver(drv);
  954. const struct pci_device_id *found_id;
  955. found_id = pci_match_device(pci_drv, pci_dev);
  956. if (found_id)
  957. return 1;
  958. return 0;
  959. }
  960. /**
  961. * pci_dev_get - increments the reference count of the pci device structure
  962. * @dev: the device being referenced
  963. *
  964. * Each live reference to a device should be refcounted.
  965. *
  966. * Drivers for PCI devices should normally record such references in
  967. * their probe() methods, when they bind to a device, and release
  968. * them by calling pci_dev_put(), in their disconnect() methods.
  969. *
  970. * A pointer to the device with the incremented reference counter is returned.
  971. */
  972. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  973. {
  974. if (dev)
  975. get_device(&dev->dev);
  976. return dev;
  977. }
  978. /**
  979. * pci_dev_put - release a use of the pci device structure
  980. * @dev: device that's been disconnected
  981. *
  982. * Must be called when a user of a device is finished with it. When the last
  983. * user of the device calls this function, the memory of the device is freed.
  984. */
  985. void pci_dev_put(struct pci_dev *dev)
  986. {
  987. if (dev)
  988. put_device(&dev->dev);
  989. }
  990. #ifndef CONFIG_HOTPLUG
  991. int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
  992. {
  993. return -ENODEV;
  994. }
  995. #endif
  996. struct bus_type pci_bus_type = {
  997. .name = "pci",
  998. .match = pci_bus_match,
  999. .uevent = pci_uevent,
  1000. .probe = pci_device_probe,
  1001. .remove = pci_device_remove,
  1002. .shutdown = pci_device_shutdown,
  1003. .dev_attrs = pci_dev_attrs,
  1004. .bus_attrs = pci_bus_attrs,
  1005. .pm = PCI_PM_OPS_PTR,
  1006. };
  1007. static int __init pci_driver_init(void)
  1008. {
  1009. return bus_register(&pci_bus_type);
  1010. }
  1011. postcore_initcall(pci_driver_init);
  1012. EXPORT_SYMBOL_GPL(pci_add_dynid);
  1013. EXPORT_SYMBOL(pci_match_id);
  1014. EXPORT_SYMBOL(__pci_register_driver);
  1015. EXPORT_SYMBOL(pci_unregister_driver);
  1016. EXPORT_SYMBOL(pci_dev_driver);
  1017. EXPORT_SYMBOL(pci_bus_type);
  1018. EXPORT_SYMBOL(pci_dev_get);
  1019. EXPORT_SYMBOL(pci_dev_put);