pci-driver.c 44 KB

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