pci-driver.c 30 KB

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