pci-driver.c 31 KB

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