pci-driver.c 37 KB

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