pci-driver.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
  4. * (C) Copyright 2007 Novell Inc.
  5. */
  6. #include <linux/pci.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/device.h>
  10. #include <linux/mempolicy.h>
  11. #include <linux/string.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/isolation.h>
  15. #include <linux/cpu.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/suspend.h>
  18. #include <linux/kexec.h>
  19. #include <linux/of_device.h>
  20. #include <linux/acpi.h>
  21. #include <linux/dma-map-ops.h>
  22. #include "pci.h"
  23. #include "pcie/portdrv.h"
  24. struct pci_dynid {
  25. struct list_head node;
  26. struct pci_device_id id;
  27. };
  28. /**
  29. * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
  30. * @drv: target pci driver
  31. * @vendor: PCI vendor ID
  32. * @device: PCI device ID
  33. * @subvendor: PCI subvendor ID
  34. * @subdevice: PCI subdevice ID
  35. * @class: PCI class
  36. * @class_mask: PCI class mask
  37. * @driver_data: private driver data
  38. *
  39. * Adds a new dynamic pci device ID to this driver and causes the
  40. * driver to probe for all devices again. @drv must have been
  41. * registered prior to calling this function.
  42. *
  43. * CONTEXT:
  44. * Does GFP_KERNEL allocation.
  45. *
  46. * RETURNS:
  47. * 0 on success, -errno on failure.
  48. */
  49. int pci_add_dynid(struct pci_driver *drv,
  50. unsigned int vendor, unsigned int device,
  51. unsigned int subvendor, unsigned int subdevice,
  52. unsigned int class, unsigned int class_mask,
  53. unsigned long driver_data)
  54. {
  55. struct pci_dynid *dynid;
  56. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  57. if (!dynid)
  58. return -ENOMEM;
  59. dynid->id.vendor = vendor;
  60. dynid->id.device = device;
  61. dynid->id.subvendor = subvendor;
  62. dynid->id.subdevice = subdevice;
  63. dynid->id.class = class;
  64. dynid->id.class_mask = class_mask;
  65. dynid->id.driver_data = driver_data;
  66. spin_lock(&drv->dynids.lock);
  67. list_add_tail(&dynid->node, &drv->dynids.list);
  68. spin_unlock(&drv->dynids.lock);
  69. return driver_attach(&drv->driver);
  70. }
  71. EXPORT_SYMBOL_GPL(pci_add_dynid);
  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. * store_new_id - sysfs frontend to pci_add_dynid()
  84. * @driver: target device driver
  85. * @buf: buffer for scanning device ID data
  86. * @count: input size
  87. *
  88. * Allow PCI IDs to be added to an existing driver via sysfs.
  89. */
  90. static ssize_t new_id_store(struct device_driver *driver, const char *buf,
  91. size_t count)
  92. {
  93. struct pci_driver *pdrv = to_pci_driver(driver);
  94. const struct pci_device_id *ids = pdrv->id_table;
  95. u32 vendor, device, subvendor = PCI_ANY_ID,
  96. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  97. unsigned long driver_data = 0;
  98. int fields = 0;
  99. int retval = 0;
  100. fields = sscanf(buf, "%x %x %x %x %x %x %lx",
  101. &vendor, &device, &subvendor, &subdevice,
  102. &class, &class_mask, &driver_data);
  103. if (fields < 2)
  104. return -EINVAL;
  105. if (fields != 7) {
  106. struct pci_dev *pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
  107. if (!pdev)
  108. return -ENOMEM;
  109. pdev->vendor = vendor;
  110. pdev->device = device;
  111. pdev->subsystem_vendor = subvendor;
  112. pdev->subsystem_device = subdevice;
  113. pdev->class = class;
  114. if (pci_match_id(pdrv->id_table, pdev))
  115. retval = -EEXIST;
  116. kfree(pdev);
  117. if (retval)
  118. return retval;
  119. }
  120. /* Only accept driver_data values that match an existing id_table
  121. entry */
  122. if (ids) {
  123. retval = -EINVAL;
  124. while (ids->vendor || ids->subvendor || ids->class_mask) {
  125. if (driver_data == ids->driver_data) {
  126. retval = 0;
  127. break;
  128. }
  129. ids++;
  130. }
  131. if (retval) /* No match */
  132. return retval;
  133. }
  134. retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
  135. class, class_mask, driver_data);
  136. if (retval)
  137. return retval;
  138. return count;
  139. }
  140. static DRIVER_ATTR_WO(new_id);
  141. /**
  142. * store_remove_id - remove a PCI device ID from this driver
  143. * @driver: target device driver
  144. * @buf: buffer for scanning device ID data
  145. * @count: input size
  146. *
  147. * Removes a dynamic pci device ID to this driver.
  148. */
  149. static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
  150. size_t count)
  151. {
  152. struct pci_dynid *dynid, *n;
  153. struct pci_driver *pdrv = to_pci_driver(driver);
  154. u32 vendor, device, subvendor = PCI_ANY_ID,
  155. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  156. int fields = 0;
  157. size_t retval = -ENODEV;
  158. fields = sscanf(buf, "%x %x %x %x %x %x",
  159. &vendor, &device, &subvendor, &subdevice,
  160. &class, &class_mask);
  161. if (fields < 2)
  162. return -EINVAL;
  163. spin_lock(&pdrv->dynids.lock);
  164. list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
  165. struct pci_device_id *id = &dynid->id;
  166. if ((id->vendor == vendor) &&
  167. (id->device == device) &&
  168. (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
  169. (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
  170. !((id->class ^ class) & class_mask)) {
  171. list_del(&dynid->node);
  172. kfree(dynid);
  173. retval = count;
  174. break;
  175. }
  176. }
  177. spin_unlock(&pdrv->dynids.lock);
  178. return retval;
  179. }
  180. static DRIVER_ATTR_WO(remove_id);
  181. static struct attribute *pci_drv_attrs[] = {
  182. &driver_attr_new_id.attr,
  183. &driver_attr_remove_id.attr,
  184. NULL,
  185. };
  186. ATTRIBUTE_GROUPS(pci_drv);
  187. /**
  188. * pci_match_id - See if a pci device matches a given pci_id table
  189. * @ids: array of PCI device id structures to search in
  190. * @dev: the PCI device structure to match against.
  191. *
  192. * Used by a driver to check whether a PCI device present in the
  193. * system is in its list of supported devices. Returns the matching
  194. * pci_device_id structure or %NULL if there is no match.
  195. *
  196. * Deprecated, don't use this as it will not catch any dynamic ids
  197. * that a driver might want to check for.
  198. */
  199. const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
  200. struct pci_dev *dev)
  201. {
  202. if (ids) {
  203. while (ids->vendor || ids->subvendor || ids->class_mask) {
  204. if (pci_match_one_device(ids, dev))
  205. return ids;
  206. ids++;
  207. }
  208. }
  209. return NULL;
  210. }
  211. EXPORT_SYMBOL(pci_match_id);
  212. static const struct pci_device_id pci_device_id_any = {
  213. .vendor = PCI_ANY_ID,
  214. .device = PCI_ANY_ID,
  215. .subvendor = PCI_ANY_ID,
  216. .subdevice = PCI_ANY_ID,
  217. };
  218. /**
  219. * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
  220. * @drv: the PCI driver to match against
  221. * @dev: the PCI device structure to match against
  222. *
  223. * Used by a driver to check whether a PCI device present in the
  224. * system is in its list of supported devices. Returns the matching
  225. * pci_device_id structure or %NULL if there is no match.
  226. */
  227. static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
  228. struct pci_dev *dev)
  229. {
  230. struct pci_dynid *dynid;
  231. const struct pci_device_id *found_id = NULL;
  232. /* When driver_override is set, only bind to the matching driver */
  233. if (dev->driver_override && strcmp(dev->driver_override, drv->name))
  234. return NULL;
  235. /* Look at the dynamic ids first, before the static ones */
  236. spin_lock(&drv->dynids.lock);
  237. list_for_each_entry(dynid, &drv->dynids.list, node) {
  238. if (pci_match_one_device(&dynid->id, dev)) {
  239. found_id = &dynid->id;
  240. break;
  241. }
  242. }
  243. spin_unlock(&drv->dynids.lock);
  244. if (!found_id)
  245. found_id = pci_match_id(drv->id_table, dev);
  246. /* driver_override will always match, send a dummy id */
  247. if (!found_id && dev->driver_override)
  248. found_id = &pci_device_id_any;
  249. return found_id;
  250. }
  251. struct drv_dev_and_id {
  252. struct pci_driver *drv;
  253. struct pci_dev *dev;
  254. const struct pci_device_id *id;
  255. };
  256. static long local_pci_probe(void *_ddi)
  257. {
  258. struct drv_dev_and_id *ddi = _ddi;
  259. struct pci_dev *pci_dev = ddi->dev;
  260. struct pci_driver *pci_drv = ddi->drv;
  261. struct device *dev = &pci_dev->dev;
  262. int rc;
  263. /*
  264. * Unbound PCI devices are always put in D0, regardless of
  265. * runtime PM status. During probe, the device is set to
  266. * active and the usage count is incremented. If the driver
  267. * supports runtime PM, it should call pm_runtime_put_noidle(),
  268. * or any other runtime PM helper function decrementing the usage
  269. * count, in its probe routine and pm_runtime_get_noresume() in
  270. * its remove routine.
  271. */
  272. pm_runtime_get_sync(dev);
  273. pci_dev->driver = pci_drv;
  274. rc = pci_drv->probe(pci_dev, ddi->id);
  275. if (!rc)
  276. return rc;
  277. if (rc < 0) {
  278. pci_dev->driver = NULL;
  279. pm_runtime_put_sync(dev);
  280. return rc;
  281. }
  282. /*
  283. * Probe function should return < 0 for failure, 0 for success
  284. * Treat values > 0 as success, but warn.
  285. */
  286. pci_warn(pci_dev, "Driver probe function unexpectedly returned %d\n",
  287. rc);
  288. return 0;
  289. }
  290. static bool pci_physfn_is_probed(struct pci_dev *dev)
  291. {
  292. #ifdef CONFIG_PCI_IOV
  293. return dev->is_virtfn && dev->physfn->is_probed;
  294. #else
  295. return false;
  296. #endif
  297. }
  298. static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
  299. const struct pci_device_id *id)
  300. {
  301. int error, node, cpu;
  302. int hk_flags = HK_FLAG_DOMAIN | HK_FLAG_WQ;
  303. struct drv_dev_and_id ddi = { drv, dev, id };
  304. /*
  305. * Execute driver initialization on node where the device is
  306. * attached. This way the driver likely allocates its local memory
  307. * on the right node.
  308. */
  309. node = dev_to_node(&dev->dev);
  310. dev->is_probed = 1;
  311. cpu_hotplug_disable();
  312. /*
  313. * Prevent nesting work_on_cpu() for the case where a Virtual Function
  314. * device is probed from work_on_cpu() of the Physical device.
  315. */
  316. if (node < 0 || node >= MAX_NUMNODES || !node_online(node) ||
  317. pci_physfn_is_probed(dev))
  318. cpu = nr_cpu_ids;
  319. else
  320. cpu = cpumask_any_and(cpumask_of_node(node),
  321. housekeeping_cpumask(hk_flags));
  322. if (cpu < nr_cpu_ids)
  323. error = work_on_cpu(cpu, local_pci_probe, &ddi);
  324. else
  325. error = local_pci_probe(&ddi);
  326. dev->is_probed = 0;
  327. cpu_hotplug_enable();
  328. return error;
  329. }
  330. /**
  331. * __pci_device_probe - check if a driver wants to claim a specific PCI device
  332. * @drv: driver to call to check if it wants the PCI device
  333. * @pci_dev: PCI device being probed
  334. *
  335. * returns 0 on success, else error.
  336. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  337. */
  338. static int __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  339. {
  340. const struct pci_device_id *id;
  341. int error = 0;
  342. if (!pci_dev->driver && drv->probe) {
  343. error = -ENODEV;
  344. id = pci_match_device(drv, pci_dev);
  345. if (id)
  346. error = pci_call_probe(drv, pci_dev, id);
  347. }
  348. return error;
  349. }
  350. int __weak pcibios_alloc_irq(struct pci_dev *dev)
  351. {
  352. return 0;
  353. }
  354. void __weak pcibios_free_irq(struct pci_dev *dev)
  355. {
  356. }
  357. #ifdef CONFIG_PCI_IOV
  358. static inline bool pci_device_can_probe(struct pci_dev *pdev)
  359. {
  360. return (!pdev->is_virtfn || pdev->physfn->sriov->drivers_autoprobe ||
  361. pdev->driver_override);
  362. }
  363. #else
  364. static inline bool pci_device_can_probe(struct pci_dev *pdev)
  365. {
  366. return true;
  367. }
  368. #endif
  369. static int pci_device_probe(struct device *dev)
  370. {
  371. int error;
  372. struct pci_dev *pci_dev = to_pci_dev(dev);
  373. struct pci_driver *drv = to_pci_driver(dev->driver);
  374. if (!pci_device_can_probe(pci_dev))
  375. return -ENODEV;
  376. pci_assign_irq(pci_dev);
  377. error = pcibios_alloc_irq(pci_dev);
  378. if (error < 0)
  379. return error;
  380. pci_dev_get(pci_dev);
  381. error = __pci_device_probe(drv, pci_dev);
  382. if (error) {
  383. pcibios_free_irq(pci_dev);
  384. pci_dev_put(pci_dev);
  385. }
  386. return error;
  387. }
  388. static int pci_device_remove(struct device *dev)
  389. {
  390. struct pci_dev *pci_dev = to_pci_dev(dev);
  391. struct pci_driver *drv = pci_dev->driver;
  392. if (drv) {
  393. if (drv->remove) {
  394. pm_runtime_get_sync(dev);
  395. drv->remove(pci_dev);
  396. pm_runtime_put_noidle(dev);
  397. }
  398. pcibios_free_irq(pci_dev);
  399. pci_dev->driver = NULL;
  400. pci_iov_remove(pci_dev);
  401. }
  402. /* Undo the runtime PM settings in local_pci_probe() */
  403. pm_runtime_put_sync(dev);
  404. /*
  405. * If the device is still on, set the power state as "unknown",
  406. * since it might change by the next time we load the driver.
  407. */
  408. if (pci_dev->current_state == PCI_D0)
  409. pci_dev->current_state = PCI_UNKNOWN;
  410. /*
  411. * We would love to complain here if pci_dev->is_enabled is set, that
  412. * the driver should have called pci_disable_device(), but the
  413. * unfortunate fact is there are too many odd BIOS and bridge setups
  414. * that don't like drivers doing that all of the time.
  415. * Oh well, we can dream of sane hardware when we sleep, no matter how
  416. * horrible the crap we have to deal with is when we are awake...
  417. */
  418. pci_dev_put(pci_dev);
  419. return 0;
  420. }
  421. static void pci_device_shutdown(struct device *dev)
  422. {
  423. struct pci_dev *pci_dev = to_pci_dev(dev);
  424. struct pci_driver *drv = pci_dev->driver;
  425. pm_runtime_resume(dev);
  426. if (drv && drv->shutdown)
  427. drv->shutdown(pci_dev);
  428. /*
  429. * If this is a kexec reboot, turn off Bus Master bit on the
  430. * device to tell it to not continue to do DMA. Don't touch
  431. * devices in D3cold or unknown states.
  432. * If it is not a kexec reboot, firmware will hit the PCI
  433. * devices with big hammer and stop their DMA any way.
  434. */
  435. if (kexec_in_progress && (pci_dev->current_state <= PCI_D3hot))
  436. pci_clear_master(pci_dev);
  437. }
  438. #ifdef CONFIG_PM
  439. /* Auxiliary functions used for system resume and run-time resume. */
  440. /**
  441. * pci_restore_standard_config - restore standard config registers of PCI device
  442. * @pci_dev: PCI device to handle
  443. */
  444. static int pci_restore_standard_config(struct pci_dev *pci_dev)
  445. {
  446. pci_update_current_state(pci_dev, PCI_UNKNOWN);
  447. if (pci_dev->current_state != PCI_D0) {
  448. int error = pci_set_power_state(pci_dev, PCI_D0);
  449. if (error)
  450. return error;
  451. }
  452. pci_restore_state(pci_dev);
  453. pci_pme_restore(pci_dev);
  454. return 0;
  455. }
  456. static void pci_pm_default_resume(struct pci_dev *pci_dev)
  457. {
  458. pci_fixup_device(pci_fixup_resume, pci_dev);
  459. pci_enable_wake(pci_dev, PCI_D0, false);
  460. }
  461. #endif
  462. #ifdef CONFIG_PM_SLEEP
  463. static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
  464. {
  465. pci_power_up(pci_dev);
  466. pci_update_current_state(pci_dev, PCI_D0);
  467. pci_restore_state(pci_dev);
  468. pci_pme_restore(pci_dev);
  469. }
  470. /*
  471. * Default "suspend" method for devices that have no driver provided suspend,
  472. * or not even a driver at all (second part).
  473. */
  474. static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
  475. {
  476. /*
  477. * mark its power state as "unknown", since we don't know if
  478. * e.g. the BIOS will change its device state when we suspend.
  479. */
  480. if (pci_dev->current_state == PCI_D0)
  481. pci_dev->current_state = PCI_UNKNOWN;
  482. }
  483. /*
  484. * Default "resume" method for devices that have no driver provided resume,
  485. * or not even a driver at all (second part).
  486. */
  487. static int pci_pm_reenable_device(struct pci_dev *pci_dev)
  488. {
  489. int retval;
  490. /* if the device was enabled before suspend, reenable */
  491. retval = pci_reenable_device(pci_dev);
  492. /*
  493. * if the device was busmaster before the suspend, make it busmaster
  494. * again
  495. */
  496. if (pci_dev->is_busmaster)
  497. pci_set_master(pci_dev);
  498. return retval;
  499. }
  500. static int pci_legacy_suspend(struct device *dev, pm_message_t state)
  501. {
  502. struct pci_dev *pci_dev = to_pci_dev(dev);
  503. struct pci_driver *drv = pci_dev->driver;
  504. if (drv && drv->suspend) {
  505. pci_power_t prev = pci_dev->current_state;
  506. int error;
  507. error = drv->suspend(pci_dev, state);
  508. suspend_report_result(drv->suspend, error);
  509. if (error)
  510. return error;
  511. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  512. && pci_dev->current_state != PCI_UNKNOWN) {
  513. pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
  514. "PCI PM: Device state not saved by %pS\n",
  515. drv->suspend);
  516. }
  517. }
  518. pci_fixup_device(pci_fixup_suspend, pci_dev);
  519. return 0;
  520. }
  521. static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
  522. {
  523. struct pci_dev *pci_dev = to_pci_dev(dev);
  524. if (!pci_dev->state_saved)
  525. pci_save_state(pci_dev);
  526. pci_pm_set_unknown_state(pci_dev);
  527. pci_fixup_device(pci_fixup_suspend_late, pci_dev);
  528. return 0;
  529. }
  530. static int pci_legacy_resume(struct device *dev)
  531. {
  532. struct pci_dev *pci_dev = to_pci_dev(dev);
  533. struct pci_driver *drv = pci_dev->driver;
  534. pci_fixup_device(pci_fixup_resume, pci_dev);
  535. return drv && drv->resume ?
  536. drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
  537. }
  538. /* Auxiliary functions used by the new power management framework */
  539. static void pci_pm_default_suspend(struct pci_dev *pci_dev)
  540. {
  541. /* Disable non-bridge devices without PM support */
  542. if (!pci_has_subordinate(pci_dev))
  543. pci_disable_enabled_device(pci_dev);
  544. }
  545. static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
  546. {
  547. struct pci_driver *drv = pci_dev->driver;
  548. bool ret = drv && (drv->suspend || drv->resume);
  549. /*
  550. * Legacy PM support is used by default, so warn if the new framework is
  551. * supported as well. Drivers are supposed to support either the
  552. * former, or the latter, but not both at the same time.
  553. */
  554. pci_WARN(pci_dev, ret && drv->driver.pm, "device %04x:%04x\n",
  555. pci_dev->vendor, pci_dev->device);
  556. return ret;
  557. }
  558. /* New power management framework */
  559. static int pci_pm_prepare(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 (pm && pm->prepare) {
  564. int error = pm->prepare(dev);
  565. if (error < 0)
  566. return error;
  567. if (!error && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
  568. return 0;
  569. }
  570. if (pci_dev_need_resume(pci_dev))
  571. return 0;
  572. /*
  573. * The PME setting needs to be adjusted here in case the direct-complete
  574. * optimization is used with respect to this device.
  575. */
  576. pci_dev_adjust_pme(pci_dev);
  577. return 1;
  578. }
  579. static void pci_pm_complete(struct device *dev)
  580. {
  581. struct pci_dev *pci_dev = to_pci_dev(dev);
  582. pci_dev_complete_resume(pci_dev);
  583. pm_generic_complete(dev);
  584. /* Resume device if platform firmware has put it in reset-power-on */
  585. if (pm_runtime_suspended(dev) && pm_resume_via_firmware()) {
  586. pci_power_t pre_sleep_state = pci_dev->current_state;
  587. pci_refresh_power_state(pci_dev);
  588. /*
  589. * On platforms with ACPI this check may also trigger for
  590. * devices sharing power resources if one of those power
  591. * resources has been activated as a result of a change of the
  592. * power state of another device sharing it. However, in that
  593. * case it is also better to resume the device, in general.
  594. */
  595. if (pci_dev->current_state < pre_sleep_state)
  596. pm_request_resume(dev);
  597. }
  598. }
  599. #else /* !CONFIG_PM_SLEEP */
  600. #define pci_pm_prepare NULL
  601. #define pci_pm_complete NULL
  602. #endif /* !CONFIG_PM_SLEEP */
  603. #ifdef CONFIG_SUSPEND
  604. static void pcie_pme_root_status_cleanup(struct pci_dev *pci_dev)
  605. {
  606. /*
  607. * Some BIOSes forget to clear Root PME Status bits after system
  608. * wakeup, which breaks ACPI-based runtime wakeup on PCI Express.
  609. * Clear those bits now just in case (shouldn't hurt).
  610. */
  611. if (pci_is_pcie(pci_dev) &&
  612. (pci_pcie_type(pci_dev) == PCI_EXP_TYPE_ROOT_PORT ||
  613. pci_pcie_type(pci_dev) == PCI_EXP_TYPE_RC_EC))
  614. pcie_clear_root_pme_status(pci_dev);
  615. }
  616. static int pci_pm_suspend(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. pci_dev->skip_bus_pm = false;
  621. if (pci_has_legacy_pm_support(pci_dev))
  622. return pci_legacy_suspend(dev, PMSG_SUSPEND);
  623. if (!pm) {
  624. pci_pm_default_suspend(pci_dev);
  625. return 0;
  626. }
  627. /*
  628. * PCI devices suspended at run time may need to be resumed at this
  629. * point, because in general it may be necessary to reconfigure them for
  630. * system suspend. Namely, if the device is expected to wake up the
  631. * system from the sleep state, it may have to be reconfigured for this
  632. * purpose, or if the device is not expected to wake up the system from
  633. * the sleep state, it should be prevented from signaling wakeup events
  634. * going forward.
  635. *
  636. * Also if the driver of the device does not indicate that its system
  637. * suspend callbacks can cope with runtime-suspended devices, it is
  638. * better to resume the device from runtime suspend here.
  639. */
  640. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  641. pci_dev_need_resume(pci_dev)) {
  642. pm_runtime_resume(dev);
  643. pci_dev->state_saved = false;
  644. } else {
  645. pci_dev_adjust_pme(pci_dev);
  646. }
  647. if (pm->suspend) {
  648. pci_power_t prev = pci_dev->current_state;
  649. int error;
  650. error = pm->suspend(dev);
  651. suspend_report_result(pm->suspend, error);
  652. if (error)
  653. return error;
  654. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  655. && pci_dev->current_state != PCI_UNKNOWN) {
  656. pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
  657. "PCI PM: State of device not saved by %pS\n",
  658. pm->suspend);
  659. }
  660. }
  661. return 0;
  662. }
  663. static int pci_pm_suspend_late(struct device *dev)
  664. {
  665. if (dev_pm_skip_suspend(dev))
  666. return 0;
  667. pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
  668. return pm_generic_suspend_late(dev);
  669. }
  670. static int pci_pm_suspend_noirq(struct device *dev)
  671. {
  672. struct pci_dev *pci_dev = to_pci_dev(dev);
  673. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  674. if (dev_pm_skip_suspend(dev))
  675. return 0;
  676. if (pci_has_legacy_pm_support(pci_dev))
  677. return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
  678. if (!pm) {
  679. pci_save_state(pci_dev);
  680. goto Fixup;
  681. }
  682. if (pm->suspend_noirq) {
  683. pci_power_t prev = pci_dev->current_state;
  684. int error;
  685. error = pm->suspend_noirq(dev);
  686. suspend_report_result(pm->suspend_noirq, error);
  687. if (error)
  688. return error;
  689. if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
  690. && pci_dev->current_state != PCI_UNKNOWN) {
  691. pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
  692. "PCI PM: State of device not saved by %pS\n",
  693. pm->suspend_noirq);
  694. goto Fixup;
  695. }
  696. }
  697. if (pci_dev->skip_bus_pm) {
  698. /*
  699. * Either the device is a bridge with a child in D0 below it, or
  700. * the function is running for the second time in a row without
  701. * going through full resume, which is possible only during
  702. * suspend-to-idle in a spurious wakeup case. The device should
  703. * be in D0 at this point, but if it is a bridge, it may be
  704. * necessary to save its state.
  705. */
  706. if (!pci_dev->state_saved)
  707. pci_save_state(pci_dev);
  708. } else if (!pci_dev->state_saved) {
  709. pci_save_state(pci_dev);
  710. if (pci_power_manageable(pci_dev))
  711. pci_prepare_to_sleep(pci_dev);
  712. }
  713. pci_dbg(pci_dev, "PCI PM: Suspend power state: %s\n",
  714. pci_power_name(pci_dev->current_state));
  715. if (pci_dev->current_state == PCI_D0) {
  716. pci_dev->skip_bus_pm = true;
  717. /*
  718. * Per PCI PM r1.2, table 6-1, a bridge must be in D0 if any
  719. * downstream device is in D0, so avoid changing the power state
  720. * of the parent bridge by setting the skip_bus_pm flag for it.
  721. */
  722. if (pci_dev->bus->self)
  723. pci_dev->bus->self->skip_bus_pm = true;
  724. }
  725. if (pci_dev->skip_bus_pm && pm_suspend_no_platform()) {
  726. pci_dbg(pci_dev, "PCI PM: Skipped\n");
  727. goto Fixup;
  728. }
  729. pci_pm_set_unknown_state(pci_dev);
  730. /*
  731. * Some BIOSes from ASUS have a bug: If a USB EHCI host controller's
  732. * PCI COMMAND register isn't 0, the BIOS assumes that the controller
  733. * hasn't been quiesced and tries to turn it off. If the controller
  734. * is already in D3, this can hang or cause memory corruption.
  735. *
  736. * Since the value of the COMMAND register doesn't matter once the
  737. * device has been suspended, we can safely set it to 0 here.
  738. */
  739. if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  740. pci_write_config_word(pci_dev, PCI_COMMAND, 0);
  741. Fixup:
  742. pci_fixup_device(pci_fixup_suspend_late, pci_dev);
  743. /*
  744. * If the target system sleep state is suspend-to-idle, it is sufficient
  745. * to check whether or not the device's wakeup settings are good for
  746. * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
  747. * pci_pm_complete() to take care of fixing up the device's state
  748. * anyway, if need be.
  749. */
  750. if (device_can_wakeup(dev) && !device_may_wakeup(dev))
  751. dev->power.may_skip_resume = false;
  752. return 0;
  753. }
  754. static int pci_pm_resume_noirq(struct device *dev)
  755. {
  756. struct pci_dev *pci_dev = to_pci_dev(dev);
  757. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  758. pci_power_t prev_state = pci_dev->current_state;
  759. bool skip_bus_pm = pci_dev->skip_bus_pm;
  760. if (dev_pm_skip_resume(dev))
  761. return 0;
  762. /*
  763. * In the suspend-to-idle case, devices left in D0 during suspend will
  764. * stay in D0, so it is not necessary to restore or update their
  765. * configuration here and attempting to put them into D0 again is
  766. * pointless, so avoid doing that.
  767. */
  768. if (!(skip_bus_pm && pm_suspend_no_platform()))
  769. pci_pm_default_resume_early(pci_dev);
  770. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  771. pcie_pme_root_status_cleanup(pci_dev);
  772. if (!skip_bus_pm && prev_state == PCI_D3cold)
  773. pci_bridge_wait_for_secondary_bus(pci_dev);
  774. if (pci_has_legacy_pm_support(pci_dev))
  775. return 0;
  776. if (pm && pm->resume_noirq)
  777. return pm->resume_noirq(dev);
  778. return 0;
  779. }
  780. static int pci_pm_resume_early(struct device *dev)
  781. {
  782. if (dev_pm_skip_resume(dev))
  783. return 0;
  784. return pm_generic_resume_early(dev);
  785. }
  786. static int pci_pm_resume(struct device *dev)
  787. {
  788. struct pci_dev *pci_dev = to_pci_dev(dev);
  789. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  790. /*
  791. * This is necessary for the suspend error path in which resume is
  792. * called without restoring the standard config registers of the device.
  793. */
  794. if (pci_dev->state_saved)
  795. pci_restore_standard_config(pci_dev);
  796. if (pci_has_legacy_pm_support(pci_dev))
  797. return pci_legacy_resume(dev);
  798. pci_pm_default_resume(pci_dev);
  799. if (pm) {
  800. if (pm->resume)
  801. return pm->resume(dev);
  802. } else {
  803. pci_pm_reenable_device(pci_dev);
  804. }
  805. return 0;
  806. }
  807. #else /* !CONFIG_SUSPEND */
  808. #define pci_pm_suspend NULL
  809. #define pci_pm_suspend_late NULL
  810. #define pci_pm_suspend_noirq NULL
  811. #define pci_pm_resume NULL
  812. #define pci_pm_resume_early NULL
  813. #define pci_pm_resume_noirq NULL
  814. #endif /* !CONFIG_SUSPEND */
  815. #ifdef CONFIG_HIBERNATE_CALLBACKS
  816. static int pci_pm_freeze(struct device *dev)
  817. {
  818. struct pci_dev *pci_dev = to_pci_dev(dev);
  819. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  820. if (pci_has_legacy_pm_support(pci_dev))
  821. return pci_legacy_suspend(dev, PMSG_FREEZE);
  822. if (!pm) {
  823. pci_pm_default_suspend(pci_dev);
  824. return 0;
  825. }
  826. /*
  827. * Resume all runtime-suspended devices before creating a snapshot
  828. * image of system memory, because the restore kernel generally cannot
  829. * be expected to always handle them consistently and they need to be
  830. * put into the runtime-active metastate during system resume anyway,
  831. * so it is better to ensure that the state saved in the image will be
  832. * always consistent with that.
  833. */
  834. pm_runtime_resume(dev);
  835. pci_dev->state_saved = false;
  836. if (pm->freeze) {
  837. int error;
  838. error = pm->freeze(dev);
  839. suspend_report_result(pm->freeze, error);
  840. if (error)
  841. return error;
  842. }
  843. return 0;
  844. }
  845. static int pci_pm_freeze_noirq(struct device *dev)
  846. {
  847. struct pci_dev *pci_dev = to_pci_dev(dev);
  848. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  849. if (pci_has_legacy_pm_support(pci_dev))
  850. return pci_legacy_suspend_late(dev, PMSG_FREEZE);
  851. if (pm && pm->freeze_noirq) {
  852. int error;
  853. error = pm->freeze_noirq(dev);
  854. suspend_report_result(pm->freeze_noirq, error);
  855. if (error)
  856. return error;
  857. }
  858. if (!pci_dev->state_saved)
  859. pci_save_state(pci_dev);
  860. pci_pm_set_unknown_state(pci_dev);
  861. return 0;
  862. }
  863. static int pci_pm_thaw_noirq(struct device *dev)
  864. {
  865. struct pci_dev *pci_dev = to_pci_dev(dev);
  866. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  867. /*
  868. * The pm->thaw_noirq() callback assumes the device has been
  869. * returned to D0 and its config state has been restored.
  870. *
  871. * In addition, pci_restore_state() restores MSI-X state in MMIO
  872. * space, which requires the device to be in D0, so return it to D0
  873. * in case the driver's "freeze" callbacks put it into a low-power
  874. * state.
  875. */
  876. pci_set_power_state(pci_dev, PCI_D0);
  877. pci_restore_state(pci_dev);
  878. if (pci_has_legacy_pm_support(pci_dev))
  879. return 0;
  880. if (pm && pm->thaw_noirq)
  881. return pm->thaw_noirq(dev);
  882. return 0;
  883. }
  884. static int pci_pm_thaw(struct device *dev)
  885. {
  886. struct pci_dev *pci_dev = to_pci_dev(dev);
  887. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  888. int error = 0;
  889. if (pci_has_legacy_pm_support(pci_dev))
  890. return pci_legacy_resume(dev);
  891. if (pm) {
  892. if (pm->thaw)
  893. error = pm->thaw(dev);
  894. } else {
  895. pci_pm_reenable_device(pci_dev);
  896. }
  897. pci_dev->state_saved = false;
  898. return error;
  899. }
  900. static int pci_pm_poweroff(struct device *dev)
  901. {
  902. struct pci_dev *pci_dev = to_pci_dev(dev);
  903. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  904. if (pci_has_legacy_pm_support(pci_dev))
  905. return pci_legacy_suspend(dev, PMSG_HIBERNATE);
  906. if (!pm) {
  907. pci_pm_default_suspend(pci_dev);
  908. return 0;
  909. }
  910. /* The reason to do that is the same as in pci_pm_suspend(). */
  911. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  912. pci_dev_need_resume(pci_dev)) {
  913. pm_runtime_resume(dev);
  914. pci_dev->state_saved = false;
  915. } else {
  916. pci_dev_adjust_pme(pci_dev);
  917. }
  918. if (pm->poweroff) {
  919. int error;
  920. error = pm->poweroff(dev);
  921. suspend_report_result(pm->poweroff, error);
  922. if (error)
  923. return error;
  924. }
  925. return 0;
  926. }
  927. static int pci_pm_poweroff_late(struct device *dev)
  928. {
  929. if (dev_pm_skip_suspend(dev))
  930. return 0;
  931. pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
  932. return pm_generic_poweroff_late(dev);
  933. }
  934. static int pci_pm_poweroff_noirq(struct device *dev)
  935. {
  936. struct pci_dev *pci_dev = to_pci_dev(dev);
  937. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  938. if (dev_pm_skip_suspend(dev))
  939. return 0;
  940. if (pci_has_legacy_pm_support(pci_dev))
  941. return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
  942. if (!pm) {
  943. pci_fixup_device(pci_fixup_suspend_late, pci_dev);
  944. return 0;
  945. }
  946. if (pm->poweroff_noirq) {
  947. int error;
  948. error = pm->poweroff_noirq(dev);
  949. suspend_report_result(pm->poweroff_noirq, error);
  950. if (error)
  951. return error;
  952. }
  953. if (!pci_dev->state_saved && !pci_has_subordinate(pci_dev))
  954. pci_prepare_to_sleep(pci_dev);
  955. /*
  956. * The reason for doing this here is the same as for the analogous code
  957. * in pci_pm_suspend_noirq().
  958. */
  959. if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  960. pci_write_config_word(pci_dev, PCI_COMMAND, 0);
  961. pci_fixup_device(pci_fixup_suspend_late, pci_dev);
  962. return 0;
  963. }
  964. static int pci_pm_restore_noirq(struct device *dev)
  965. {
  966. struct pci_dev *pci_dev = to_pci_dev(dev);
  967. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  968. pci_pm_default_resume_early(pci_dev);
  969. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  970. if (pci_has_legacy_pm_support(pci_dev))
  971. return 0;
  972. if (pm && pm->restore_noirq)
  973. return pm->restore_noirq(dev);
  974. return 0;
  975. }
  976. static int pci_pm_restore(struct device *dev)
  977. {
  978. struct pci_dev *pci_dev = to_pci_dev(dev);
  979. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  980. /*
  981. * This is necessary for the hibernation error path in which restore is
  982. * called without restoring the standard config registers of the device.
  983. */
  984. if (pci_dev->state_saved)
  985. pci_restore_standard_config(pci_dev);
  986. if (pci_has_legacy_pm_support(pci_dev))
  987. return pci_legacy_resume(dev);
  988. pci_pm_default_resume(pci_dev);
  989. if (pm) {
  990. if (pm->restore)
  991. return pm->restore(dev);
  992. } else {
  993. pci_pm_reenable_device(pci_dev);
  994. }
  995. return 0;
  996. }
  997. #else /* !CONFIG_HIBERNATE_CALLBACKS */
  998. #define pci_pm_freeze NULL
  999. #define pci_pm_freeze_noirq NULL
  1000. #define pci_pm_thaw NULL
  1001. #define pci_pm_thaw_noirq NULL
  1002. #define pci_pm_poweroff NULL
  1003. #define pci_pm_poweroff_late NULL
  1004. #define pci_pm_poweroff_noirq NULL
  1005. #define pci_pm_restore NULL
  1006. #define pci_pm_restore_noirq NULL
  1007. #endif /* !CONFIG_HIBERNATE_CALLBACKS */
  1008. #ifdef CONFIG_PM
  1009. static int pci_pm_runtime_suspend(struct device *dev)
  1010. {
  1011. struct pci_dev *pci_dev = to_pci_dev(dev);
  1012. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  1013. pci_power_t prev = pci_dev->current_state;
  1014. int error;
  1015. /*
  1016. * If pci_dev->driver is not set (unbound), we leave the device in D0,
  1017. * but it may go to D3cold when the bridge above it runtime suspends.
  1018. * Save its config space in case that happens.
  1019. */
  1020. if (!pci_dev->driver) {
  1021. pci_save_state(pci_dev);
  1022. return 0;
  1023. }
  1024. pci_dev->state_saved = false;
  1025. if (pm && pm->runtime_suspend) {
  1026. error = pm->runtime_suspend(dev);
  1027. /*
  1028. * -EBUSY and -EAGAIN is used to request the runtime PM core
  1029. * to schedule a new suspend, so log the event only with debug
  1030. * log level.
  1031. */
  1032. if (error == -EBUSY || error == -EAGAIN) {
  1033. pci_dbg(pci_dev, "can't suspend now (%ps returned %d)\n",
  1034. pm->runtime_suspend, error);
  1035. return error;
  1036. } else if (error) {
  1037. pci_err(pci_dev, "can't suspend (%ps returned %d)\n",
  1038. pm->runtime_suspend, error);
  1039. return error;
  1040. }
  1041. }
  1042. pci_fixup_device(pci_fixup_suspend, pci_dev);
  1043. if (pm && pm->runtime_suspend
  1044. && !pci_dev->state_saved && pci_dev->current_state != PCI_D0
  1045. && pci_dev->current_state != PCI_UNKNOWN) {
  1046. pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
  1047. "PCI PM: State of device not saved by %pS\n",
  1048. pm->runtime_suspend);
  1049. return 0;
  1050. }
  1051. if (!pci_dev->state_saved) {
  1052. pci_save_state(pci_dev);
  1053. pci_finish_runtime_suspend(pci_dev);
  1054. }
  1055. return 0;
  1056. }
  1057. static int pci_pm_runtime_resume(struct device *dev)
  1058. {
  1059. struct pci_dev *pci_dev = to_pci_dev(dev);
  1060. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  1061. pci_power_t prev_state = pci_dev->current_state;
  1062. int error = 0;
  1063. /*
  1064. * Restoring config space is necessary even if the device is not bound
  1065. * to a driver because although we left it in D0, it may have gone to
  1066. * D3cold when the bridge above it runtime suspended.
  1067. */
  1068. pci_restore_standard_config(pci_dev);
  1069. if (!pci_dev->driver)
  1070. return 0;
  1071. pci_fixup_device(pci_fixup_resume_early, pci_dev);
  1072. pci_pm_default_resume(pci_dev);
  1073. if (prev_state == PCI_D3cold)
  1074. pci_bridge_wait_for_secondary_bus(pci_dev);
  1075. if (pm && pm->runtime_resume)
  1076. error = pm->runtime_resume(dev);
  1077. pci_dev->runtime_d3cold = false;
  1078. return error;
  1079. }
  1080. static int pci_pm_runtime_idle(struct device *dev)
  1081. {
  1082. struct pci_dev *pci_dev = to_pci_dev(dev);
  1083. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  1084. /*
  1085. * If pci_dev->driver is not set (unbound), the device should
  1086. * always remain in D0 regardless of the runtime PM status
  1087. */
  1088. if (!pci_dev->driver)
  1089. return 0;
  1090. if (!pm)
  1091. return -ENOSYS;
  1092. if (pm->runtime_idle)
  1093. return pm->runtime_idle(dev);
  1094. return 0;
  1095. }
  1096. static const struct dev_pm_ops pci_dev_pm_ops = {
  1097. .prepare = pci_pm_prepare,
  1098. .complete = pci_pm_complete,
  1099. .suspend = pci_pm_suspend,
  1100. .suspend_late = pci_pm_suspend_late,
  1101. .resume = pci_pm_resume,
  1102. .resume_early = pci_pm_resume_early,
  1103. .freeze = pci_pm_freeze,
  1104. .thaw = pci_pm_thaw,
  1105. .poweroff = pci_pm_poweroff,
  1106. .poweroff_late = pci_pm_poweroff_late,
  1107. .restore = pci_pm_restore,
  1108. .suspend_noirq = pci_pm_suspend_noirq,
  1109. .resume_noirq = pci_pm_resume_noirq,
  1110. .freeze_noirq = pci_pm_freeze_noirq,
  1111. .thaw_noirq = pci_pm_thaw_noirq,
  1112. .poweroff_noirq = pci_pm_poweroff_noirq,
  1113. .restore_noirq = pci_pm_restore_noirq,
  1114. .runtime_suspend = pci_pm_runtime_suspend,
  1115. .runtime_resume = pci_pm_runtime_resume,
  1116. .runtime_idle = pci_pm_runtime_idle,
  1117. };
  1118. #define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
  1119. #else /* !CONFIG_PM */
  1120. #define pci_pm_runtime_suspend NULL
  1121. #define pci_pm_runtime_resume NULL
  1122. #define pci_pm_runtime_idle NULL
  1123. #define PCI_PM_OPS_PTR NULL
  1124. #endif /* !CONFIG_PM */
  1125. /**
  1126. * __pci_register_driver - register a new pci driver
  1127. * @drv: the driver structure to register
  1128. * @owner: owner module of drv
  1129. * @mod_name: module name string
  1130. *
  1131. * Adds the driver structure to the list of registered drivers.
  1132. * Returns a negative value on error, otherwise 0.
  1133. * If no error occurred, the driver remains registered even if
  1134. * no device was claimed during registration.
  1135. */
  1136. int __pci_register_driver(struct pci_driver *drv, struct module *owner,
  1137. const char *mod_name)
  1138. {
  1139. /* initialize common driver fields */
  1140. drv->driver.name = drv->name;
  1141. drv->driver.bus = &pci_bus_type;
  1142. drv->driver.owner = owner;
  1143. drv->driver.mod_name = mod_name;
  1144. drv->driver.groups = drv->groups;
  1145. spin_lock_init(&drv->dynids.lock);
  1146. INIT_LIST_HEAD(&drv->dynids.list);
  1147. /* register with core */
  1148. return driver_register(&drv->driver);
  1149. }
  1150. EXPORT_SYMBOL(__pci_register_driver);
  1151. /**
  1152. * pci_unregister_driver - unregister a pci driver
  1153. * @drv: the driver structure to unregister
  1154. *
  1155. * Deletes the driver structure from the list of registered PCI drivers,
  1156. * gives it a chance to clean up by calling its remove() function for
  1157. * each device it was responsible for, and marks those devices as
  1158. * driverless.
  1159. */
  1160. void pci_unregister_driver(struct pci_driver *drv)
  1161. {
  1162. driver_unregister(&drv->driver);
  1163. pci_free_dynids(drv);
  1164. }
  1165. EXPORT_SYMBOL(pci_unregister_driver);
  1166. static struct pci_driver pci_compat_driver = {
  1167. .name = "compat"
  1168. };
  1169. /**
  1170. * pci_dev_driver - get the pci_driver of a device
  1171. * @dev: the device to query
  1172. *
  1173. * Returns the appropriate pci_driver structure or %NULL if there is no
  1174. * registered driver for the device.
  1175. */
  1176. struct pci_driver *pci_dev_driver(const struct pci_dev *dev)
  1177. {
  1178. if (dev->driver)
  1179. return dev->driver;
  1180. else {
  1181. int i;
  1182. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  1183. if (dev->resource[i].flags & IORESOURCE_BUSY)
  1184. return &pci_compat_driver;
  1185. }
  1186. return NULL;
  1187. }
  1188. EXPORT_SYMBOL(pci_dev_driver);
  1189. /**
  1190. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  1191. * @dev: the PCI device structure to match against
  1192. * @drv: the device driver to search for matching PCI device id structures
  1193. *
  1194. * Used by a driver to check whether a PCI device present in the
  1195. * system is in its list of supported devices. Returns the matching
  1196. * pci_device_id structure or %NULL if there is no match.
  1197. */
  1198. static int pci_bus_match(struct device *dev, struct device_driver *drv)
  1199. {
  1200. struct pci_dev *pci_dev = to_pci_dev(dev);
  1201. struct pci_driver *pci_drv;
  1202. const struct pci_device_id *found_id;
  1203. if (!pci_dev->match_driver)
  1204. return 0;
  1205. pci_drv = to_pci_driver(drv);
  1206. found_id = pci_match_device(pci_drv, pci_dev);
  1207. if (found_id)
  1208. return 1;
  1209. return 0;
  1210. }
  1211. /**
  1212. * pci_dev_get - increments the reference count of the pci device structure
  1213. * @dev: the device being referenced
  1214. *
  1215. * Each live reference to a device should be refcounted.
  1216. *
  1217. * Drivers for PCI devices should normally record such references in
  1218. * their probe() methods, when they bind to a device, and release
  1219. * them by calling pci_dev_put(), in their disconnect() methods.
  1220. *
  1221. * A pointer to the device with the incremented reference counter is returned.
  1222. */
  1223. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  1224. {
  1225. if (dev)
  1226. get_device(&dev->dev);
  1227. return dev;
  1228. }
  1229. EXPORT_SYMBOL(pci_dev_get);
  1230. /**
  1231. * pci_dev_put - release a use of the pci device structure
  1232. * @dev: device that's been disconnected
  1233. *
  1234. * Must be called when a user of a device is finished with it. When the last
  1235. * user of the device calls this function, the memory of the device is freed.
  1236. */
  1237. void pci_dev_put(struct pci_dev *dev)
  1238. {
  1239. if (dev)
  1240. put_device(&dev->dev);
  1241. }
  1242. EXPORT_SYMBOL(pci_dev_put);
  1243. static int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
  1244. {
  1245. struct pci_dev *pdev;
  1246. if (!dev)
  1247. return -ENODEV;
  1248. pdev = to_pci_dev(dev);
  1249. if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class))
  1250. return -ENOMEM;
  1251. if (add_uevent_var(env, "PCI_ID=%04X:%04X", pdev->vendor, pdev->device))
  1252. return -ENOMEM;
  1253. if (add_uevent_var(env, "PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor,
  1254. pdev->subsystem_device))
  1255. return -ENOMEM;
  1256. if (add_uevent_var(env, "PCI_SLOT_NAME=%s", pci_name(pdev)))
  1257. return -ENOMEM;
  1258. if (add_uevent_var(env, "MODALIAS=pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X",
  1259. pdev->vendor, pdev->device,
  1260. pdev->subsystem_vendor, pdev->subsystem_device,
  1261. (u8)(pdev->class >> 16), (u8)(pdev->class >> 8),
  1262. (u8)(pdev->class)))
  1263. return -ENOMEM;
  1264. return 0;
  1265. }
  1266. #if defined(CONFIG_PCIEPORTBUS) || defined(CONFIG_EEH)
  1267. /**
  1268. * pci_uevent_ers - emit a uevent during recovery path of PCI device
  1269. * @pdev: PCI device undergoing error recovery
  1270. * @err_type: type of error event
  1271. */
  1272. void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type)
  1273. {
  1274. int idx = 0;
  1275. char *envp[3];
  1276. switch (err_type) {
  1277. case PCI_ERS_RESULT_NONE:
  1278. case PCI_ERS_RESULT_CAN_RECOVER:
  1279. envp[idx++] = "ERROR_EVENT=BEGIN_RECOVERY";
  1280. envp[idx++] = "DEVICE_ONLINE=0";
  1281. break;
  1282. case PCI_ERS_RESULT_RECOVERED:
  1283. envp[idx++] = "ERROR_EVENT=SUCCESSFUL_RECOVERY";
  1284. envp[idx++] = "DEVICE_ONLINE=1";
  1285. break;
  1286. case PCI_ERS_RESULT_DISCONNECT:
  1287. envp[idx++] = "ERROR_EVENT=FAILED_RECOVERY";
  1288. envp[idx++] = "DEVICE_ONLINE=0";
  1289. break;
  1290. default:
  1291. break;
  1292. }
  1293. if (idx > 0) {
  1294. envp[idx++] = NULL;
  1295. kobject_uevent_env(&pdev->dev.kobj, KOBJ_CHANGE, envp);
  1296. }
  1297. }
  1298. #endif
  1299. static int pci_bus_num_vf(struct device *dev)
  1300. {
  1301. return pci_num_vf(to_pci_dev(dev));
  1302. }
  1303. /**
  1304. * pci_dma_configure - Setup DMA configuration
  1305. * @dev: ptr to dev structure
  1306. *
  1307. * Function to update PCI devices's DMA configuration using the same
  1308. * info from the OF node or ACPI node of host bridge's parent (if any).
  1309. */
  1310. static int pci_dma_configure(struct device *dev)
  1311. {
  1312. struct device *bridge;
  1313. int ret = 0;
  1314. bridge = pci_get_host_bridge_device(to_pci_dev(dev));
  1315. if (IS_ENABLED(CONFIG_OF) && bridge->parent &&
  1316. bridge->parent->of_node) {
  1317. ret = of_dma_configure(dev, bridge->parent->of_node, true);
  1318. } else if (has_acpi_companion(bridge)) {
  1319. struct acpi_device *adev = to_acpi_device_node(bridge->fwnode);
  1320. ret = acpi_dma_configure(dev, acpi_get_dma_attr(adev));
  1321. }
  1322. pci_put_host_bridge_device(bridge);
  1323. return ret;
  1324. }
  1325. struct bus_type pci_bus_type = {
  1326. .name = "pci",
  1327. .match = pci_bus_match,
  1328. .uevent = pci_uevent,
  1329. .probe = pci_device_probe,
  1330. .remove = pci_device_remove,
  1331. .shutdown = pci_device_shutdown,
  1332. .dev_groups = pci_dev_groups,
  1333. .bus_groups = pci_bus_groups,
  1334. .drv_groups = pci_drv_groups,
  1335. .pm = PCI_PM_OPS_PTR,
  1336. .num_vf = pci_bus_num_vf,
  1337. .dma_configure = pci_dma_configure,
  1338. };
  1339. EXPORT_SYMBOL(pci_bus_type);
  1340. #ifdef CONFIG_PCIEPORTBUS
  1341. static int pcie_port_bus_match(struct device *dev, struct device_driver *drv)
  1342. {
  1343. struct pcie_device *pciedev;
  1344. struct pcie_port_service_driver *driver;
  1345. if (drv->bus != &pcie_port_bus_type || dev->bus != &pcie_port_bus_type)
  1346. return 0;
  1347. pciedev = to_pcie_device(dev);
  1348. driver = to_service_driver(drv);
  1349. if (driver->service != pciedev->service)
  1350. return 0;
  1351. if (driver->port_type != PCIE_ANY_PORT &&
  1352. driver->port_type != pci_pcie_type(pciedev->port))
  1353. return 0;
  1354. return 1;
  1355. }
  1356. struct bus_type pcie_port_bus_type = {
  1357. .name = "pci_express",
  1358. .match = pcie_port_bus_match,
  1359. };
  1360. EXPORT_SYMBOL_GPL(pcie_port_bus_type);
  1361. #endif
  1362. static int __init pci_driver_init(void)
  1363. {
  1364. int ret;
  1365. ret = bus_register(&pci_bus_type);
  1366. if (ret)
  1367. return ret;
  1368. #ifdef CONFIG_PCIEPORTBUS
  1369. ret = bus_register(&pcie_port_bus_type);
  1370. if (ret)
  1371. return ret;
  1372. #endif
  1373. dma_debug_add_bus(&pci_bus_type);
  1374. return 0;
  1375. }
  1376. postcore_initcall(pci_driver_init);