pci.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  1. /*
  2. * PCI Bus Services, see include/linux/pci.h for further explanation.
  3. *
  4. * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  5. * David Mosberger-Tang
  6. *
  7. * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/delay.h>
  11. #include <linux/init.h>
  12. #include <linux/pci.h>
  13. #include <linux/pm.h>
  14. #include <linux/module.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/string.h>
  17. #include <linux/log2.h>
  18. #include <linux/pci-aspm.h>
  19. #include <linux/pm_wakeup.h>
  20. #include <linux/interrupt.h>
  21. #include <asm/dma.h> /* isa_dma_bridge_buggy */
  22. #include "pci.h"
  23. unsigned int pci_pm_d3_delay = PCI_PM_D3_WAIT;
  24. #ifdef CONFIG_PCI_DOMAINS
  25. int pci_domains_supported = 1;
  26. #endif
  27. #define DEFAULT_CARDBUS_IO_SIZE (256)
  28. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  29. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  30. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  31. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  32. /**
  33. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  34. * @bus: pointer to PCI bus structure to search
  35. *
  36. * Given a PCI bus, returns the highest PCI bus number present in the set
  37. * including the given PCI bus and its list of child PCI buses.
  38. */
  39. unsigned char pci_bus_max_busnr(struct pci_bus* bus)
  40. {
  41. struct list_head *tmp;
  42. unsigned char max, n;
  43. max = bus->subordinate;
  44. list_for_each(tmp, &bus->children) {
  45. n = pci_bus_max_busnr(pci_bus_b(tmp));
  46. if(n > max)
  47. max = n;
  48. }
  49. return max;
  50. }
  51. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  52. #ifdef CONFIG_HAS_IOMEM
  53. void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
  54. {
  55. /*
  56. * Make sure the BAR is actually a memory resource, not an IO resource
  57. */
  58. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  59. WARN_ON(1);
  60. return NULL;
  61. }
  62. return ioremap_nocache(pci_resource_start(pdev, bar),
  63. pci_resource_len(pdev, bar));
  64. }
  65. EXPORT_SYMBOL_GPL(pci_ioremap_bar);
  66. #endif
  67. #if 0
  68. /**
  69. * pci_max_busnr - returns maximum PCI bus number
  70. *
  71. * Returns the highest PCI bus number present in the system global list of
  72. * PCI buses.
  73. */
  74. unsigned char __devinit
  75. pci_max_busnr(void)
  76. {
  77. struct pci_bus *bus = NULL;
  78. unsigned char max, n;
  79. max = 0;
  80. while ((bus = pci_find_next_bus(bus)) != NULL) {
  81. n = pci_bus_max_busnr(bus);
  82. if(n > max)
  83. max = n;
  84. }
  85. return max;
  86. }
  87. #endif /* 0 */
  88. #define PCI_FIND_CAP_TTL 48
  89. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  90. u8 pos, int cap, int *ttl)
  91. {
  92. u8 id;
  93. while ((*ttl)--) {
  94. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  95. if (pos < 0x40)
  96. break;
  97. pos &= ~3;
  98. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  99. &id);
  100. if (id == 0xff)
  101. break;
  102. if (id == cap)
  103. return pos;
  104. pos += PCI_CAP_LIST_NEXT;
  105. }
  106. return 0;
  107. }
  108. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  109. u8 pos, int cap)
  110. {
  111. int ttl = PCI_FIND_CAP_TTL;
  112. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  113. }
  114. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  115. {
  116. return __pci_find_next_cap(dev->bus, dev->devfn,
  117. pos + PCI_CAP_LIST_NEXT, cap);
  118. }
  119. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  120. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  121. unsigned int devfn, u8 hdr_type)
  122. {
  123. u16 status;
  124. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  125. if (!(status & PCI_STATUS_CAP_LIST))
  126. return 0;
  127. switch (hdr_type) {
  128. case PCI_HEADER_TYPE_NORMAL:
  129. case PCI_HEADER_TYPE_BRIDGE:
  130. return PCI_CAPABILITY_LIST;
  131. case PCI_HEADER_TYPE_CARDBUS:
  132. return PCI_CB_CAPABILITY_LIST;
  133. default:
  134. return 0;
  135. }
  136. return 0;
  137. }
  138. /**
  139. * pci_find_capability - query for devices' capabilities
  140. * @dev: PCI device to query
  141. * @cap: capability code
  142. *
  143. * Tell if a device supports a given PCI capability.
  144. * Returns the address of the requested capability structure within the
  145. * device's PCI configuration space or 0 in case the device does not
  146. * support it. Possible values for @cap:
  147. *
  148. * %PCI_CAP_ID_PM Power Management
  149. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  150. * %PCI_CAP_ID_VPD Vital Product Data
  151. * %PCI_CAP_ID_SLOTID Slot Identification
  152. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  153. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  154. * %PCI_CAP_ID_PCIX PCI-X
  155. * %PCI_CAP_ID_EXP PCI Express
  156. */
  157. int pci_find_capability(struct pci_dev *dev, int cap)
  158. {
  159. int pos;
  160. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  161. if (pos)
  162. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  163. return pos;
  164. }
  165. /**
  166. * pci_bus_find_capability - query for devices' capabilities
  167. * @bus: the PCI bus to query
  168. * @devfn: PCI device to query
  169. * @cap: capability code
  170. *
  171. * Like pci_find_capability() but works for pci devices that do not have a
  172. * pci_dev structure set up yet.
  173. *
  174. * Returns the address of the requested capability structure within the
  175. * device's PCI configuration space or 0 in case the device does not
  176. * support it.
  177. */
  178. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  179. {
  180. int pos;
  181. u8 hdr_type;
  182. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  183. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  184. if (pos)
  185. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  186. return pos;
  187. }
  188. /**
  189. * pci_find_ext_capability - Find an extended capability
  190. * @dev: PCI device to query
  191. * @cap: capability code
  192. *
  193. * Returns the address of the requested extended capability structure
  194. * within the device's PCI configuration space or 0 if the device does
  195. * not support it. Possible values for @cap:
  196. *
  197. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  198. * %PCI_EXT_CAP_ID_VC Virtual Channel
  199. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  200. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  201. */
  202. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  203. {
  204. u32 header;
  205. int ttl;
  206. int pos = PCI_CFG_SPACE_SIZE;
  207. /* minimum 8 bytes per capability */
  208. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  209. if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
  210. return 0;
  211. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  212. return 0;
  213. /*
  214. * If we have no capabilities, this is indicated by cap ID,
  215. * cap version and next pointer all being 0.
  216. */
  217. if (header == 0)
  218. return 0;
  219. while (ttl-- > 0) {
  220. if (PCI_EXT_CAP_ID(header) == cap)
  221. return pos;
  222. pos = PCI_EXT_CAP_NEXT(header);
  223. if (pos < PCI_CFG_SPACE_SIZE)
  224. break;
  225. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  226. break;
  227. }
  228. return 0;
  229. }
  230. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  231. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  232. {
  233. int rc, ttl = PCI_FIND_CAP_TTL;
  234. u8 cap, mask;
  235. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  236. mask = HT_3BIT_CAP_MASK;
  237. else
  238. mask = HT_5BIT_CAP_MASK;
  239. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  240. PCI_CAP_ID_HT, &ttl);
  241. while (pos) {
  242. rc = pci_read_config_byte(dev, pos + 3, &cap);
  243. if (rc != PCIBIOS_SUCCESSFUL)
  244. return 0;
  245. if ((cap & mask) == ht_cap)
  246. return pos;
  247. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  248. pos + PCI_CAP_LIST_NEXT,
  249. PCI_CAP_ID_HT, &ttl);
  250. }
  251. return 0;
  252. }
  253. /**
  254. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  255. * @dev: PCI device to query
  256. * @pos: Position from which to continue searching
  257. * @ht_cap: Hypertransport capability code
  258. *
  259. * To be used in conjunction with pci_find_ht_capability() to search for
  260. * all capabilities matching @ht_cap. @pos should always be a value returned
  261. * from pci_find_ht_capability().
  262. *
  263. * NB. To be 100% safe against broken PCI devices, the caller should take
  264. * steps to avoid an infinite loop.
  265. */
  266. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  267. {
  268. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  269. }
  270. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  271. /**
  272. * pci_find_ht_capability - query a device's Hypertransport capabilities
  273. * @dev: PCI device to query
  274. * @ht_cap: Hypertransport capability code
  275. *
  276. * Tell if a device supports a given Hypertransport capability.
  277. * Returns an address within the device's PCI configuration space
  278. * or 0 in case the device does not support the request capability.
  279. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  280. * which has a Hypertransport capability matching @ht_cap.
  281. */
  282. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  283. {
  284. int pos;
  285. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  286. if (pos)
  287. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  288. return pos;
  289. }
  290. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  291. /**
  292. * pci_find_parent_resource - return resource region of parent bus of given region
  293. * @dev: PCI device structure contains resources to be searched
  294. * @res: child resource record for which parent is sought
  295. *
  296. * For given resource region of given device, return the resource
  297. * region of parent bus the given region is contained in or where
  298. * it should be allocated from.
  299. */
  300. struct resource *
  301. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  302. {
  303. const struct pci_bus *bus = dev->bus;
  304. int i;
  305. struct resource *best = NULL;
  306. for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  307. struct resource *r = bus->resource[i];
  308. if (!r)
  309. continue;
  310. if (res->start && !(res->start >= r->start && res->end <= r->end))
  311. continue; /* Not contained */
  312. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  313. continue; /* Wrong type */
  314. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  315. return r; /* Exact match */
  316. if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
  317. best = r; /* Approximating prefetchable by non-prefetchable */
  318. }
  319. return best;
  320. }
  321. /**
  322. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  323. * @dev: PCI device to have its BARs restored
  324. *
  325. * Restore the BAR values for a given device, so as to make it
  326. * accessible by its driver.
  327. */
  328. static void
  329. pci_restore_bars(struct pci_dev *dev)
  330. {
  331. int i;
  332. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
  333. pci_update_resource(dev, i);
  334. }
  335. static struct pci_platform_pm_ops *pci_platform_pm;
  336. int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
  337. {
  338. if (!ops->is_manageable || !ops->set_state || !ops->choose_state
  339. || !ops->sleep_wake || !ops->can_wakeup)
  340. return -EINVAL;
  341. pci_platform_pm = ops;
  342. return 0;
  343. }
  344. static inline bool platform_pci_power_manageable(struct pci_dev *dev)
  345. {
  346. return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
  347. }
  348. static inline int platform_pci_set_power_state(struct pci_dev *dev,
  349. pci_power_t t)
  350. {
  351. return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
  352. }
  353. static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
  354. {
  355. return pci_platform_pm ?
  356. pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
  357. }
  358. static inline bool platform_pci_can_wakeup(struct pci_dev *dev)
  359. {
  360. return pci_platform_pm ? pci_platform_pm->can_wakeup(dev) : false;
  361. }
  362. static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
  363. {
  364. return pci_platform_pm ?
  365. pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
  366. }
  367. /**
  368. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  369. * given PCI device
  370. * @dev: PCI device to handle.
  371. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  372. * @wait: If 'true', wait for the device to change its power state
  373. *
  374. * RETURN VALUE:
  375. * -EINVAL if the requested state is invalid.
  376. * -EIO if device does not support PCI PM or its PM capabilities register has a
  377. * wrong version, or device doesn't support the requested state.
  378. * 0 if device already is in the requested state.
  379. * 0 if device's power state has been successfully changed.
  380. */
  381. static int
  382. pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state, bool wait)
  383. {
  384. u16 pmcsr;
  385. bool need_restore = false;
  386. if (!dev->pm_cap)
  387. return -EIO;
  388. if (state < PCI_D0 || state > PCI_D3hot)
  389. return -EINVAL;
  390. /* Validate current state:
  391. * Can enter D0 from any state, but if we can only go deeper
  392. * to sleep if we're already in a low power state
  393. */
  394. if (dev->current_state == state) {
  395. /* we're already there */
  396. return 0;
  397. } else if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  398. && dev->current_state > state) {
  399. dev_err(&dev->dev, "invalid power transition "
  400. "(from state %d to %d)\n", dev->current_state, state);
  401. return -EINVAL;
  402. }
  403. /* check if this device supports the desired state */
  404. if ((state == PCI_D1 && !dev->d1_support)
  405. || (state == PCI_D2 && !dev->d2_support))
  406. return -EIO;
  407. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  408. /* If we're (effectively) in D3, force entire word to 0.
  409. * This doesn't affect PME_Status, disables PME_En, and
  410. * sets PowerState to 0.
  411. */
  412. switch (dev->current_state) {
  413. case PCI_D0:
  414. case PCI_D1:
  415. case PCI_D2:
  416. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  417. pmcsr |= state;
  418. break;
  419. case PCI_UNKNOWN: /* Boot-up */
  420. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  421. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET)) {
  422. need_restore = true;
  423. wait = true;
  424. }
  425. /* Fall-through: force to D0 */
  426. default:
  427. pmcsr = 0;
  428. break;
  429. }
  430. /* enter specified state */
  431. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  432. if (!wait)
  433. return 0;
  434. /* Mandatory power management transition delays */
  435. /* see PCI PM 1.1 5.6.1 table 18 */
  436. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  437. msleep(pci_pm_d3_delay);
  438. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  439. udelay(PCI_PM_D2_DELAY);
  440. dev->current_state = state;
  441. /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  442. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  443. * from D3hot to D0 _may_ perform an internal reset, thereby
  444. * going to "D0 Uninitialized" rather than "D0 Initialized".
  445. * For example, at least some versions of the 3c905B and the
  446. * 3c556B exhibit this behaviour.
  447. *
  448. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  449. * devices in a D3hot state at boot. Consequently, we need to
  450. * restore at least the BARs so that the device will be
  451. * accessible to its driver.
  452. */
  453. if (need_restore)
  454. pci_restore_bars(dev);
  455. if (wait && dev->bus->self)
  456. pcie_aspm_pm_state_change(dev->bus->self);
  457. return 0;
  458. }
  459. /**
  460. * pci_update_current_state - Read PCI power state of given device from its
  461. * PCI PM registers and cache it
  462. * @dev: PCI device to handle.
  463. * @state: State to cache in case the device doesn't have the PM capability
  464. */
  465. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  466. {
  467. if (dev->pm_cap) {
  468. u16 pmcsr;
  469. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  470. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  471. } else {
  472. dev->current_state = state;
  473. }
  474. }
  475. /**
  476. * pci_set_power_state - Set the power state of a PCI device
  477. * @dev: PCI device to handle.
  478. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  479. *
  480. * Transition a device to a new power state, using the platform formware and/or
  481. * the device's PCI PM registers.
  482. *
  483. * RETURN VALUE:
  484. * -EINVAL if the requested state is invalid.
  485. * -EIO if device does not support PCI PM or its PM capabilities register has a
  486. * wrong version, or device doesn't support the requested state.
  487. * 0 if device already is in the requested state.
  488. * 0 if device's power state has been successfully changed.
  489. */
  490. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  491. {
  492. int error;
  493. /* bound the state we're entering */
  494. if (state > PCI_D3hot)
  495. state = PCI_D3hot;
  496. else if (state < PCI_D0)
  497. state = PCI_D0;
  498. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  499. /*
  500. * If the device or the parent bridge do not support PCI PM,
  501. * ignore the request if we're doing anything other than putting
  502. * it into D0 (which would only happen on boot).
  503. */
  504. return 0;
  505. if (state == PCI_D0 && platform_pci_power_manageable(dev)) {
  506. /*
  507. * Allow the platform to change the state, for example via ACPI
  508. * _PR0, _PS0 and some such, but do not trust it.
  509. */
  510. int ret = platform_pci_set_power_state(dev, PCI_D0);
  511. if (!ret)
  512. pci_update_current_state(dev, PCI_D0);
  513. }
  514. /* This device is quirked not to be put into D3, so
  515. don't put it in D3 */
  516. if (state == PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  517. return 0;
  518. error = pci_raw_set_power_state(dev, state, true);
  519. if (state > PCI_D0 && platform_pci_power_manageable(dev)) {
  520. /* Allow the platform to finalize the transition */
  521. int ret = platform_pci_set_power_state(dev, state);
  522. if (!ret) {
  523. pci_update_current_state(dev, state);
  524. error = 0;
  525. }
  526. }
  527. return error;
  528. }
  529. /**
  530. * pci_choose_state - Choose the power state of a PCI device
  531. * @dev: PCI device to be suspended
  532. * @state: target sleep state for the whole system. This is the value
  533. * that is passed to suspend() function.
  534. *
  535. * Returns PCI power state suitable for given device and given system
  536. * message.
  537. */
  538. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  539. {
  540. pci_power_t ret;
  541. if (!pci_find_capability(dev, PCI_CAP_ID_PM))
  542. return PCI_D0;
  543. ret = platform_pci_choose_state(dev);
  544. if (ret != PCI_POWER_ERROR)
  545. return ret;
  546. switch (state.event) {
  547. case PM_EVENT_ON:
  548. return PCI_D0;
  549. case PM_EVENT_FREEZE:
  550. case PM_EVENT_PRETHAW:
  551. /* REVISIT both freeze and pre-thaw "should" use D0 */
  552. case PM_EVENT_SUSPEND:
  553. case PM_EVENT_HIBERNATE:
  554. return PCI_D3hot;
  555. default:
  556. dev_info(&dev->dev, "unrecognized suspend event %d\n",
  557. state.event);
  558. BUG();
  559. }
  560. return PCI_D0;
  561. }
  562. EXPORT_SYMBOL(pci_choose_state);
  563. static int pci_save_pcie_state(struct pci_dev *dev)
  564. {
  565. int pos, i = 0;
  566. struct pci_cap_saved_state *save_state;
  567. u16 *cap;
  568. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  569. if (pos <= 0)
  570. return 0;
  571. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  572. if (!save_state) {
  573. dev_err(&dev->dev, "buffer not found in %s\n", __FUNCTION__);
  574. return -ENOMEM;
  575. }
  576. cap = (u16 *)&save_state->data[0];
  577. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]);
  578. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
  579. pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
  580. pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
  581. return 0;
  582. }
  583. static void pci_restore_pcie_state(struct pci_dev *dev)
  584. {
  585. int i = 0, pos;
  586. struct pci_cap_saved_state *save_state;
  587. u16 *cap;
  588. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  589. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  590. if (!save_state || pos <= 0)
  591. return;
  592. cap = (u16 *)&save_state->data[0];
  593. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]);
  594. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]);
  595. pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]);
  596. pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);
  597. }
  598. static int pci_save_pcix_state(struct pci_dev *dev)
  599. {
  600. int pos;
  601. struct pci_cap_saved_state *save_state;
  602. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  603. if (pos <= 0)
  604. return 0;
  605. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  606. if (!save_state) {
  607. dev_err(&dev->dev, "buffer not found in %s\n", __FUNCTION__);
  608. return -ENOMEM;
  609. }
  610. pci_read_config_word(dev, pos + PCI_X_CMD, (u16 *)save_state->data);
  611. return 0;
  612. }
  613. static void pci_restore_pcix_state(struct pci_dev *dev)
  614. {
  615. int i = 0, pos;
  616. struct pci_cap_saved_state *save_state;
  617. u16 *cap;
  618. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  619. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  620. if (!save_state || pos <= 0)
  621. return;
  622. cap = (u16 *)&save_state->data[0];
  623. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  624. }
  625. /**
  626. * pci_save_state - save the PCI configuration space of a device before suspending
  627. * @dev: - PCI device that we're dealing with
  628. */
  629. int
  630. pci_save_state(struct pci_dev *dev)
  631. {
  632. int i;
  633. /* XXX: 100% dword access ok here? */
  634. for (i = 0; i < 16; i++)
  635. pci_read_config_dword(dev, i * 4,&dev->saved_config_space[i]);
  636. dev->state_saved = true;
  637. if ((i = pci_save_pcie_state(dev)) != 0)
  638. return i;
  639. if ((i = pci_save_pcix_state(dev)) != 0)
  640. return i;
  641. return 0;
  642. }
  643. /**
  644. * pci_restore_state - Restore the saved state of a PCI device
  645. * @dev: - PCI device that we're dealing with
  646. */
  647. int
  648. pci_restore_state(struct pci_dev *dev)
  649. {
  650. int i;
  651. u32 val;
  652. /* PCI Express register must be restored first */
  653. pci_restore_pcie_state(dev);
  654. /*
  655. * The Base Address register should be programmed before the command
  656. * register(s)
  657. */
  658. for (i = 15; i >= 0; i--) {
  659. pci_read_config_dword(dev, i * 4, &val);
  660. if (val != dev->saved_config_space[i]) {
  661. dev_printk(KERN_DEBUG, &dev->dev, "restoring config "
  662. "space at offset %#x (was %#x, writing %#x)\n",
  663. i, val, (int)dev->saved_config_space[i]);
  664. pci_write_config_dword(dev,i * 4,
  665. dev->saved_config_space[i]);
  666. }
  667. }
  668. pci_restore_pcix_state(dev);
  669. pci_restore_msi_state(dev);
  670. return 0;
  671. }
  672. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  673. {
  674. int err;
  675. err = pci_set_power_state(dev, PCI_D0);
  676. if (err < 0 && err != -EIO)
  677. return err;
  678. err = pcibios_enable_device(dev, bars);
  679. if (err < 0)
  680. return err;
  681. pci_fixup_device(pci_fixup_enable, dev);
  682. return 0;
  683. }
  684. /**
  685. * pci_reenable_device - Resume abandoned device
  686. * @dev: PCI device to be resumed
  687. *
  688. * Note this function is a backend of pci_default_resume and is not supposed
  689. * to be called by normal code, write proper resume handler and use it instead.
  690. */
  691. int pci_reenable_device(struct pci_dev *dev)
  692. {
  693. if (atomic_read(&dev->enable_cnt))
  694. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  695. return 0;
  696. }
  697. static int __pci_enable_device_flags(struct pci_dev *dev,
  698. resource_size_t flags)
  699. {
  700. int err;
  701. int i, bars = 0;
  702. if (atomic_add_return(1, &dev->enable_cnt) > 1)
  703. return 0; /* already enabled */
  704. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  705. if (dev->resource[i].flags & flags)
  706. bars |= (1 << i);
  707. err = do_pci_enable_device(dev, bars);
  708. if (err < 0)
  709. atomic_dec(&dev->enable_cnt);
  710. return err;
  711. }
  712. /**
  713. * pci_enable_device_io - Initialize a device for use with IO space
  714. * @dev: PCI device to be initialized
  715. *
  716. * Initialize device before it's used by a driver. Ask low-level code
  717. * to enable I/O resources. Wake up the device if it was suspended.
  718. * Beware, this function can fail.
  719. */
  720. int pci_enable_device_io(struct pci_dev *dev)
  721. {
  722. return __pci_enable_device_flags(dev, IORESOURCE_IO);
  723. }
  724. /**
  725. * pci_enable_device_mem - Initialize a device for use with Memory space
  726. * @dev: PCI device to be initialized
  727. *
  728. * Initialize device before it's used by a driver. Ask low-level code
  729. * to enable Memory resources. Wake up the device if it was suspended.
  730. * Beware, this function can fail.
  731. */
  732. int pci_enable_device_mem(struct pci_dev *dev)
  733. {
  734. return __pci_enable_device_flags(dev, IORESOURCE_MEM);
  735. }
  736. /**
  737. * pci_enable_device - Initialize device before it's used by a driver.
  738. * @dev: PCI device to be initialized
  739. *
  740. * Initialize device before it's used by a driver. Ask low-level code
  741. * to enable I/O and memory. Wake up the device if it was suspended.
  742. * Beware, this function can fail.
  743. *
  744. * Note we don't actually enable the device many times if we call
  745. * this function repeatedly (we just increment the count).
  746. */
  747. int pci_enable_device(struct pci_dev *dev)
  748. {
  749. return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  750. }
  751. /*
  752. * Managed PCI resources. This manages device on/off, intx/msi/msix
  753. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  754. * there's no need to track it separately. pci_devres is initialized
  755. * when a device is enabled using managed PCI device enable interface.
  756. */
  757. struct pci_devres {
  758. unsigned int enabled:1;
  759. unsigned int pinned:1;
  760. unsigned int orig_intx:1;
  761. unsigned int restore_intx:1;
  762. u32 region_mask;
  763. };
  764. static void pcim_release(struct device *gendev, void *res)
  765. {
  766. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  767. struct pci_devres *this = res;
  768. int i;
  769. if (dev->msi_enabled)
  770. pci_disable_msi(dev);
  771. if (dev->msix_enabled)
  772. pci_disable_msix(dev);
  773. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  774. if (this->region_mask & (1 << i))
  775. pci_release_region(dev, i);
  776. if (this->restore_intx)
  777. pci_intx(dev, this->orig_intx);
  778. if (this->enabled && !this->pinned)
  779. pci_disable_device(dev);
  780. }
  781. static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
  782. {
  783. struct pci_devres *dr, *new_dr;
  784. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  785. if (dr)
  786. return dr;
  787. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  788. if (!new_dr)
  789. return NULL;
  790. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  791. }
  792. static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
  793. {
  794. if (pci_is_managed(pdev))
  795. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  796. return NULL;
  797. }
  798. /**
  799. * pcim_enable_device - Managed pci_enable_device()
  800. * @pdev: PCI device to be initialized
  801. *
  802. * Managed pci_enable_device().
  803. */
  804. int pcim_enable_device(struct pci_dev *pdev)
  805. {
  806. struct pci_devres *dr;
  807. int rc;
  808. dr = get_pci_dr(pdev);
  809. if (unlikely(!dr))
  810. return -ENOMEM;
  811. if (dr->enabled)
  812. return 0;
  813. rc = pci_enable_device(pdev);
  814. if (!rc) {
  815. pdev->is_managed = 1;
  816. dr->enabled = 1;
  817. }
  818. return rc;
  819. }
  820. /**
  821. * pcim_pin_device - Pin managed PCI device
  822. * @pdev: PCI device to pin
  823. *
  824. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  825. * driver detach. @pdev must have been enabled with
  826. * pcim_enable_device().
  827. */
  828. void pcim_pin_device(struct pci_dev *pdev)
  829. {
  830. struct pci_devres *dr;
  831. dr = find_pci_dr(pdev);
  832. WARN_ON(!dr || !dr->enabled);
  833. if (dr)
  834. dr->pinned = 1;
  835. }
  836. /**
  837. * pcibios_disable_device - disable arch specific PCI resources for device dev
  838. * @dev: the PCI device to disable
  839. *
  840. * Disables architecture specific PCI resources for the device. This
  841. * is the default implementation. Architecture implementations can
  842. * override this.
  843. */
  844. void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
  845. static void do_pci_disable_device(struct pci_dev *dev)
  846. {
  847. u16 pci_command;
  848. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  849. if (pci_command & PCI_COMMAND_MASTER) {
  850. pci_command &= ~PCI_COMMAND_MASTER;
  851. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  852. }
  853. pcibios_disable_device(dev);
  854. }
  855. /**
  856. * pci_disable_enabled_device - Disable device without updating enable_cnt
  857. * @dev: PCI device to disable
  858. *
  859. * NOTE: This function is a backend of PCI power management routines and is
  860. * not supposed to be called drivers.
  861. */
  862. void pci_disable_enabled_device(struct pci_dev *dev)
  863. {
  864. if (atomic_read(&dev->enable_cnt))
  865. do_pci_disable_device(dev);
  866. }
  867. /**
  868. * pci_disable_device - Disable PCI device after use
  869. * @dev: PCI device to be disabled
  870. *
  871. * Signal to the system that the PCI device is not in use by the system
  872. * anymore. This only involves disabling PCI bus-mastering, if active.
  873. *
  874. * Note we don't actually disable the device until all callers of
  875. * pci_device_enable() have called pci_device_disable().
  876. */
  877. void
  878. pci_disable_device(struct pci_dev *dev)
  879. {
  880. struct pci_devres *dr;
  881. dr = find_pci_dr(dev);
  882. if (dr)
  883. dr->enabled = 0;
  884. if (atomic_sub_return(1, &dev->enable_cnt) != 0)
  885. return;
  886. do_pci_disable_device(dev);
  887. dev->is_busmaster = 0;
  888. }
  889. /**
  890. * pcibios_set_pcie_reset_state - set reset state for device dev
  891. * @dev: the PCI-E device reset
  892. * @state: Reset state to enter into
  893. *
  894. *
  895. * Sets the PCI-E reset state for the device. This is the default
  896. * implementation. Architecture implementations can override this.
  897. */
  898. int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev,
  899. enum pcie_reset_state state)
  900. {
  901. return -EINVAL;
  902. }
  903. /**
  904. * pci_set_pcie_reset_state - set reset state for device dev
  905. * @dev: the PCI-E device reset
  906. * @state: Reset state to enter into
  907. *
  908. *
  909. * Sets the PCI reset state for the device.
  910. */
  911. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  912. {
  913. return pcibios_set_pcie_reset_state(dev, state);
  914. }
  915. /**
  916. * pci_pme_capable - check the capability of PCI device to generate PME#
  917. * @dev: PCI device to handle.
  918. * @state: PCI state from which device will issue PME#.
  919. */
  920. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  921. {
  922. if (!dev->pm_cap)
  923. return false;
  924. return !!(dev->pme_support & (1 << state));
  925. }
  926. /**
  927. * pci_pme_active - enable or disable PCI device's PME# function
  928. * @dev: PCI device to handle.
  929. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  930. *
  931. * The caller must verify that the device is capable of generating PME# before
  932. * calling this function with @enable equal to 'true'.
  933. */
  934. void pci_pme_active(struct pci_dev *dev, bool enable)
  935. {
  936. u16 pmcsr;
  937. if (!dev->pm_cap)
  938. return;
  939. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  940. /* Clear PME_Status by writing 1 to it and enable PME# */
  941. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  942. if (!enable)
  943. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  944. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  945. dev_printk(KERN_INFO, &dev->dev, "PME# %s\n",
  946. enable ? "enabled" : "disabled");
  947. }
  948. /**
  949. * pci_enable_wake - enable PCI device as wakeup event source
  950. * @dev: PCI device affected
  951. * @state: PCI state from which device will issue wakeup events
  952. * @enable: True to enable event generation; false to disable
  953. *
  954. * This enables the device as a wakeup event source, or disables it.
  955. * When such events involves platform-specific hooks, those hooks are
  956. * called automatically by this routine.
  957. *
  958. * Devices with legacy power management (no standard PCI PM capabilities)
  959. * always require such platform hooks.
  960. *
  961. * RETURN VALUE:
  962. * 0 is returned on success
  963. * -EINVAL is returned if device is not supposed to wake up the system
  964. * Error code depending on the platform is returned if both the platform and
  965. * the native mechanism fail to enable the generation of wake-up events
  966. */
  967. int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
  968. {
  969. int error = 0;
  970. bool pme_done = false;
  971. if (enable && !device_may_wakeup(&dev->dev))
  972. return -EINVAL;
  973. /*
  974. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  975. * Anderson we should be doing PME# wake enable followed by ACPI wake
  976. * enable. To disable wake-up we call the platform first, for symmetry.
  977. */
  978. if (!enable && platform_pci_can_wakeup(dev))
  979. error = platform_pci_sleep_wake(dev, false);
  980. if (!enable || pci_pme_capable(dev, state)) {
  981. pci_pme_active(dev, enable);
  982. pme_done = true;
  983. }
  984. if (enable && platform_pci_can_wakeup(dev))
  985. error = platform_pci_sleep_wake(dev, true);
  986. return pme_done ? 0 : error;
  987. }
  988. /**
  989. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  990. * @dev: PCI device to prepare
  991. * @enable: True to enable wake-up event generation; false to disable
  992. *
  993. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  994. * and this function allows them to set that up cleanly - pci_enable_wake()
  995. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  996. * ordering constraints.
  997. *
  998. * This function only returns error code if the device is not capable of
  999. * generating PME# from both D3_hot and D3_cold, and the platform is unable to
  1000. * enable wake-up power for it.
  1001. */
  1002. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  1003. {
  1004. return pci_pme_capable(dev, PCI_D3cold) ?
  1005. pci_enable_wake(dev, PCI_D3cold, enable) :
  1006. pci_enable_wake(dev, PCI_D3hot, enable);
  1007. }
  1008. /**
  1009. * pci_target_state - find an appropriate low power state for a given PCI dev
  1010. * @dev: PCI device
  1011. *
  1012. * Use underlying platform code to find a supported low power state for @dev.
  1013. * If the platform can't manage @dev, return the deepest state from which it
  1014. * can generate wake events, based on any available PME info.
  1015. */
  1016. pci_power_t pci_target_state(struct pci_dev *dev)
  1017. {
  1018. pci_power_t target_state = PCI_D3hot;
  1019. if (platform_pci_power_manageable(dev)) {
  1020. /*
  1021. * Call the platform to choose the target state of the device
  1022. * and enable wake-up from this state if supported.
  1023. */
  1024. pci_power_t state = platform_pci_choose_state(dev);
  1025. switch (state) {
  1026. case PCI_POWER_ERROR:
  1027. case PCI_UNKNOWN:
  1028. break;
  1029. case PCI_D1:
  1030. case PCI_D2:
  1031. if (pci_no_d1d2(dev))
  1032. break;
  1033. default:
  1034. target_state = state;
  1035. }
  1036. } else if (device_may_wakeup(&dev->dev)) {
  1037. /*
  1038. * Find the deepest state from which the device can generate
  1039. * wake-up events, make it the target state and enable device
  1040. * to generate PME#.
  1041. */
  1042. if (!dev->pm_cap)
  1043. return PCI_POWER_ERROR;
  1044. if (dev->pme_support) {
  1045. while (target_state
  1046. && !(dev->pme_support & (1 << target_state)))
  1047. target_state--;
  1048. }
  1049. }
  1050. return target_state;
  1051. }
  1052. /**
  1053. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1054. * @dev: Device to handle.
  1055. *
  1056. * Choose the power state appropriate for the device depending on whether
  1057. * it can wake up the system and/or is power manageable by the platform
  1058. * (PCI_D3hot is the default) and put the device into that state.
  1059. */
  1060. int pci_prepare_to_sleep(struct pci_dev *dev)
  1061. {
  1062. pci_power_t target_state = pci_target_state(dev);
  1063. int error;
  1064. if (target_state == PCI_POWER_ERROR)
  1065. return -EIO;
  1066. pci_enable_wake(dev, target_state, true);
  1067. error = pci_set_power_state(dev, target_state);
  1068. if (error)
  1069. pci_enable_wake(dev, target_state, false);
  1070. return error;
  1071. }
  1072. /**
  1073. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1074. * @dev: Device to handle.
  1075. *
  1076. * Disable device's sytem wake-up capability and put it into D0.
  1077. */
  1078. int pci_back_from_sleep(struct pci_dev *dev)
  1079. {
  1080. pci_enable_wake(dev, PCI_D0, false);
  1081. return pci_set_power_state(dev, PCI_D0);
  1082. }
  1083. /**
  1084. * pci_pm_init - Initialize PM functions of given PCI device
  1085. * @dev: PCI device to handle.
  1086. */
  1087. void pci_pm_init(struct pci_dev *dev)
  1088. {
  1089. int pm;
  1090. u16 pmc;
  1091. dev->pm_cap = 0;
  1092. /* find PCI PM capability in list */
  1093. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  1094. if (!pm)
  1095. return;
  1096. /* Check device's ability to generate PME# */
  1097. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  1098. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  1099. dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
  1100. pmc & PCI_PM_CAP_VER_MASK);
  1101. return;
  1102. }
  1103. dev->pm_cap = pm;
  1104. dev->d1_support = false;
  1105. dev->d2_support = false;
  1106. if (!pci_no_d1d2(dev)) {
  1107. if (pmc & PCI_PM_CAP_D1)
  1108. dev->d1_support = true;
  1109. if (pmc & PCI_PM_CAP_D2)
  1110. dev->d2_support = true;
  1111. if (dev->d1_support || dev->d2_support)
  1112. dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
  1113. dev->d1_support ? " D1" : "",
  1114. dev->d2_support ? " D2" : "");
  1115. }
  1116. pmc &= PCI_PM_CAP_PME_MASK;
  1117. if (pmc) {
  1118. dev_info(&dev->dev, "PME# supported from%s%s%s%s%s\n",
  1119. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  1120. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  1121. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  1122. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  1123. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  1124. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  1125. /*
  1126. * Make device's PM flags reflect the wake-up capability, but
  1127. * let the user space enable it to wake up the system as needed.
  1128. */
  1129. device_set_wakeup_capable(&dev->dev, true);
  1130. device_set_wakeup_enable(&dev->dev, false);
  1131. /* Disable the PME# generation functionality */
  1132. pci_pme_active(dev, false);
  1133. } else {
  1134. dev->pme_support = 0;
  1135. }
  1136. }
  1137. /**
  1138. * platform_pci_wakeup_init - init platform wakeup if present
  1139. * @dev: PCI device
  1140. *
  1141. * Some devices don't have PCI PM caps but can still generate wakeup
  1142. * events through platform methods (like ACPI events). If @dev supports
  1143. * platform wakeup events, set the device flag to indicate as much. This
  1144. * may be redundant if the device also supports PCI PM caps, but double
  1145. * initialization should be safe in that case.
  1146. */
  1147. void platform_pci_wakeup_init(struct pci_dev *dev)
  1148. {
  1149. if (!platform_pci_can_wakeup(dev))
  1150. return;
  1151. device_set_wakeup_capable(&dev->dev, true);
  1152. device_set_wakeup_enable(&dev->dev, false);
  1153. platform_pci_sleep_wake(dev, false);
  1154. }
  1155. /**
  1156. * pci_add_save_buffer - allocate buffer for saving given capability registers
  1157. * @dev: the PCI device
  1158. * @cap: the capability to allocate the buffer for
  1159. * @size: requested size of the buffer
  1160. */
  1161. static int pci_add_cap_save_buffer(
  1162. struct pci_dev *dev, char cap, unsigned int size)
  1163. {
  1164. int pos;
  1165. struct pci_cap_saved_state *save_state;
  1166. pos = pci_find_capability(dev, cap);
  1167. if (pos <= 0)
  1168. return 0;
  1169. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  1170. if (!save_state)
  1171. return -ENOMEM;
  1172. save_state->cap_nr = cap;
  1173. pci_add_saved_cap(dev, save_state);
  1174. return 0;
  1175. }
  1176. /**
  1177. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  1178. * @dev: the PCI device
  1179. */
  1180. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  1181. {
  1182. int error;
  1183. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP, 4 * sizeof(u16));
  1184. if (error)
  1185. dev_err(&dev->dev,
  1186. "unable to preallocate PCI Express save buffer\n");
  1187. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  1188. if (error)
  1189. dev_err(&dev->dev,
  1190. "unable to preallocate PCI-X save buffer\n");
  1191. }
  1192. /**
  1193. * pci_restore_standard_config - restore standard config registers of PCI device
  1194. * @dev: PCI device to handle
  1195. *
  1196. * This function assumes that the device's configuration space is accessible.
  1197. * If the device needs to be powered up, the function will wait for it to
  1198. * change the state.
  1199. */
  1200. int pci_restore_standard_config(struct pci_dev *dev)
  1201. {
  1202. pci_power_t prev_state;
  1203. int error;
  1204. pci_update_current_state(dev, PCI_D0);
  1205. prev_state = dev->current_state;
  1206. if (prev_state == PCI_D0)
  1207. goto Restore;
  1208. error = pci_raw_set_power_state(dev, PCI_D0, false);
  1209. if (error)
  1210. return error;
  1211. /*
  1212. * This assumes that we won't get a bus in B2 or B3 from the BIOS, but
  1213. * we've made this assumption forever and it appears to be universally
  1214. * satisfied.
  1215. */
  1216. switch(prev_state) {
  1217. case PCI_D3cold:
  1218. case PCI_D3hot:
  1219. mdelay(pci_pm_d3_delay);
  1220. break;
  1221. case PCI_D2:
  1222. udelay(PCI_PM_D2_DELAY);
  1223. break;
  1224. }
  1225. pci_update_current_state(dev, PCI_D0);
  1226. Restore:
  1227. return dev->state_saved ? pci_restore_state(dev) : 0;
  1228. }
  1229. /**
  1230. * pci_enable_ari - enable ARI forwarding if hardware support it
  1231. * @dev: the PCI device
  1232. */
  1233. void pci_enable_ari(struct pci_dev *dev)
  1234. {
  1235. int pos;
  1236. u32 cap;
  1237. u16 ctrl;
  1238. struct pci_dev *bridge;
  1239. if (!dev->is_pcie || dev->devfn)
  1240. return;
  1241. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  1242. if (!pos)
  1243. return;
  1244. bridge = dev->bus->self;
  1245. if (!bridge || !bridge->is_pcie)
  1246. return;
  1247. pos = pci_find_capability(bridge, PCI_CAP_ID_EXP);
  1248. if (!pos)
  1249. return;
  1250. pci_read_config_dword(bridge, pos + PCI_EXP_DEVCAP2, &cap);
  1251. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  1252. return;
  1253. pci_read_config_word(bridge, pos + PCI_EXP_DEVCTL2, &ctrl);
  1254. ctrl |= PCI_EXP_DEVCTL2_ARI;
  1255. pci_write_config_word(bridge, pos + PCI_EXP_DEVCTL2, ctrl);
  1256. bridge->ari_enabled = 1;
  1257. }
  1258. /**
  1259. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  1260. * @dev: the PCI device
  1261. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1262. *
  1263. * Perform INTx swizzling for a device behind one level of bridge. This is
  1264. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  1265. * behind bridges on add-in cards.
  1266. */
  1267. u8 pci_swizzle_interrupt_pin(struct pci_dev *dev, u8 pin)
  1268. {
  1269. return (((pin - 1) + PCI_SLOT(dev->devfn)) % 4) + 1;
  1270. }
  1271. int
  1272. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  1273. {
  1274. u8 pin;
  1275. pin = dev->pin;
  1276. if (!pin)
  1277. return -1;
  1278. while (dev->bus->self) {
  1279. pin = pci_swizzle_interrupt_pin(dev, pin);
  1280. dev = dev->bus->self;
  1281. }
  1282. *bridge = dev;
  1283. return pin;
  1284. }
  1285. /**
  1286. * pci_common_swizzle - swizzle INTx all the way to root bridge
  1287. * @dev: the PCI device
  1288. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1289. *
  1290. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  1291. * bridges all the way up to a PCI root bus.
  1292. */
  1293. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  1294. {
  1295. u8 pin = *pinp;
  1296. while (dev->bus->self) {
  1297. pin = pci_swizzle_interrupt_pin(dev, pin);
  1298. dev = dev->bus->self;
  1299. }
  1300. *pinp = pin;
  1301. return PCI_SLOT(dev->devfn);
  1302. }
  1303. /**
  1304. * pci_release_region - Release a PCI bar
  1305. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  1306. * @bar: BAR to release
  1307. *
  1308. * Releases the PCI I/O and memory resources previously reserved by a
  1309. * successful call to pci_request_region. Call this function only
  1310. * after all use of the PCI regions has ceased.
  1311. */
  1312. void pci_release_region(struct pci_dev *pdev, int bar)
  1313. {
  1314. struct pci_devres *dr;
  1315. if (pci_resource_len(pdev, bar) == 0)
  1316. return;
  1317. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  1318. release_region(pci_resource_start(pdev, bar),
  1319. pci_resource_len(pdev, bar));
  1320. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  1321. release_mem_region(pci_resource_start(pdev, bar),
  1322. pci_resource_len(pdev, bar));
  1323. dr = find_pci_dr(pdev);
  1324. if (dr)
  1325. dr->region_mask &= ~(1 << bar);
  1326. }
  1327. /**
  1328. * __pci_request_region - Reserved PCI I/O and memory resource
  1329. * @pdev: PCI device whose resources are to be reserved
  1330. * @bar: BAR to be reserved
  1331. * @res_name: Name to be associated with resource.
  1332. * @exclusive: whether the region access is exclusive or not
  1333. *
  1334. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1335. * being reserved by owner @res_name. Do not access any
  1336. * address inside the PCI regions unless this call returns
  1337. * successfully.
  1338. *
  1339. * If @exclusive is set, then the region is marked so that userspace
  1340. * is explicitly not allowed to map the resource via /dev/mem or
  1341. * sysfs MMIO access.
  1342. *
  1343. * Returns 0 on success, or %EBUSY on error. A warning
  1344. * message is also printed on failure.
  1345. */
  1346. static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
  1347. int exclusive)
  1348. {
  1349. struct pci_devres *dr;
  1350. if (pci_resource_len(pdev, bar) == 0)
  1351. return 0;
  1352. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  1353. if (!request_region(pci_resource_start(pdev, bar),
  1354. pci_resource_len(pdev, bar), res_name))
  1355. goto err_out;
  1356. }
  1357. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  1358. if (!__request_mem_region(pci_resource_start(pdev, bar),
  1359. pci_resource_len(pdev, bar), res_name,
  1360. exclusive))
  1361. goto err_out;
  1362. }
  1363. dr = find_pci_dr(pdev);
  1364. if (dr)
  1365. dr->region_mask |= 1 << bar;
  1366. return 0;
  1367. err_out:
  1368. dev_warn(&pdev->dev, "BAR %d: can't reserve %s region %pR\n",
  1369. bar,
  1370. pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
  1371. &pdev->resource[bar]);
  1372. return -EBUSY;
  1373. }
  1374. /**
  1375. * pci_request_region - Reserve PCI I/O and memory resource
  1376. * @pdev: PCI device whose resources are to be reserved
  1377. * @bar: BAR to be reserved
  1378. * @res_name: Name to be associated with resource
  1379. *
  1380. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  1381. * being reserved by owner @res_name. Do not access any
  1382. * address inside the PCI regions unless this call returns
  1383. * successfully.
  1384. *
  1385. * Returns 0 on success, or %EBUSY on error. A warning
  1386. * message is also printed on failure.
  1387. */
  1388. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  1389. {
  1390. return __pci_request_region(pdev, bar, res_name, 0);
  1391. }
  1392. /**
  1393. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  1394. * @pdev: PCI device whose resources are to be reserved
  1395. * @bar: BAR to be reserved
  1396. * @res_name: Name to be associated with resource.
  1397. *
  1398. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1399. * being reserved by owner @res_name. Do not access any
  1400. * address inside the PCI regions unless this call returns
  1401. * successfully.
  1402. *
  1403. * Returns 0 on success, or %EBUSY on error. A warning
  1404. * message is also printed on failure.
  1405. *
  1406. * The key difference that _exclusive makes it that userspace is
  1407. * explicitly not allowed to map the resource via /dev/mem or
  1408. * sysfs.
  1409. */
  1410. int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
  1411. {
  1412. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  1413. }
  1414. /**
  1415. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  1416. * @pdev: PCI device whose resources were previously reserved
  1417. * @bars: Bitmask of BARs to be released
  1418. *
  1419. * Release selected PCI I/O and memory resources previously reserved.
  1420. * Call this function only after all use of the PCI regions has ceased.
  1421. */
  1422. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  1423. {
  1424. int i;
  1425. for (i = 0; i < 6; i++)
  1426. if (bars & (1 << i))
  1427. pci_release_region(pdev, i);
  1428. }
  1429. int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1430. const char *res_name, int excl)
  1431. {
  1432. int i;
  1433. for (i = 0; i < 6; i++)
  1434. if (bars & (1 << i))
  1435. if (__pci_request_region(pdev, i, res_name, excl))
  1436. goto err_out;
  1437. return 0;
  1438. err_out:
  1439. while(--i >= 0)
  1440. if (bars & (1 << i))
  1441. pci_release_region(pdev, i);
  1442. return -EBUSY;
  1443. }
  1444. /**
  1445. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  1446. * @pdev: PCI device whose resources are to be reserved
  1447. * @bars: Bitmask of BARs to be requested
  1448. * @res_name: Name to be associated with resource
  1449. */
  1450. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1451. const char *res_name)
  1452. {
  1453. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  1454. }
  1455. int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
  1456. int bars, const char *res_name)
  1457. {
  1458. return __pci_request_selected_regions(pdev, bars, res_name,
  1459. IORESOURCE_EXCLUSIVE);
  1460. }
  1461. /**
  1462. * pci_release_regions - Release reserved PCI I/O and memory resources
  1463. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  1464. *
  1465. * Releases all PCI I/O and memory resources previously reserved by a
  1466. * successful call to pci_request_regions. Call this function only
  1467. * after all use of the PCI regions has ceased.
  1468. */
  1469. void pci_release_regions(struct pci_dev *pdev)
  1470. {
  1471. pci_release_selected_regions(pdev, (1 << 6) - 1);
  1472. }
  1473. /**
  1474. * pci_request_regions - Reserved PCI I/O and memory resources
  1475. * @pdev: PCI device whose resources are to be reserved
  1476. * @res_name: Name to be associated with resource.
  1477. *
  1478. * Mark all PCI regions associated with PCI device @pdev as
  1479. * being reserved by owner @res_name. Do not access any
  1480. * address inside the PCI regions unless this call returns
  1481. * successfully.
  1482. *
  1483. * Returns 0 on success, or %EBUSY on error. A warning
  1484. * message is also printed on failure.
  1485. */
  1486. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  1487. {
  1488. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  1489. }
  1490. /**
  1491. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  1492. * @pdev: PCI device whose resources are to be reserved
  1493. * @res_name: Name to be associated with resource.
  1494. *
  1495. * Mark all PCI regions associated with PCI device @pdev as
  1496. * being reserved by owner @res_name. Do not access any
  1497. * address inside the PCI regions unless this call returns
  1498. * successfully.
  1499. *
  1500. * pci_request_regions_exclusive() will mark the region so that
  1501. * /dev/mem and the sysfs MMIO access will not be allowed.
  1502. *
  1503. * Returns 0 on success, or %EBUSY on error. A warning
  1504. * message is also printed on failure.
  1505. */
  1506. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  1507. {
  1508. return pci_request_selected_regions_exclusive(pdev,
  1509. ((1 << 6) - 1), res_name);
  1510. }
  1511. static void __pci_set_master(struct pci_dev *dev, bool enable)
  1512. {
  1513. u16 old_cmd, cmd;
  1514. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  1515. if (enable)
  1516. cmd = old_cmd | PCI_COMMAND_MASTER;
  1517. else
  1518. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  1519. if (cmd != old_cmd) {
  1520. dev_dbg(&dev->dev, "%s bus mastering\n",
  1521. enable ? "enabling" : "disabling");
  1522. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1523. }
  1524. dev->is_busmaster = enable;
  1525. }
  1526. /**
  1527. * pci_set_master - enables bus-mastering for device dev
  1528. * @dev: the PCI device to enable
  1529. *
  1530. * Enables bus-mastering on the device and calls pcibios_set_master()
  1531. * to do the needed arch specific settings.
  1532. */
  1533. void pci_set_master(struct pci_dev *dev)
  1534. {
  1535. __pci_set_master(dev, true);
  1536. pcibios_set_master(dev);
  1537. }
  1538. /**
  1539. * pci_clear_master - disables bus-mastering for device dev
  1540. * @dev: the PCI device to disable
  1541. */
  1542. void pci_clear_master(struct pci_dev *dev)
  1543. {
  1544. __pci_set_master(dev, false);
  1545. }
  1546. #ifdef PCI_DISABLE_MWI
  1547. int pci_set_mwi(struct pci_dev *dev)
  1548. {
  1549. return 0;
  1550. }
  1551. int pci_try_set_mwi(struct pci_dev *dev)
  1552. {
  1553. return 0;
  1554. }
  1555. void pci_clear_mwi(struct pci_dev *dev)
  1556. {
  1557. }
  1558. #else
  1559. #ifndef PCI_CACHE_LINE_BYTES
  1560. #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES
  1561. #endif
  1562. /* This can be overridden by arch code. */
  1563. /* Don't forget this is measured in 32-bit words, not bytes */
  1564. u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4;
  1565. /**
  1566. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  1567. * @dev: the PCI device for which MWI is to be enabled
  1568. *
  1569. * Helper function for pci_set_mwi.
  1570. * Originally copied from drivers/net/acenic.c.
  1571. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  1572. *
  1573. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1574. */
  1575. static int
  1576. pci_set_cacheline_size(struct pci_dev *dev)
  1577. {
  1578. u8 cacheline_size;
  1579. if (!pci_cache_line_size)
  1580. return -EINVAL; /* The system doesn't support MWI. */
  1581. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  1582. equal to or multiple of the right value. */
  1583. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1584. if (cacheline_size >= pci_cache_line_size &&
  1585. (cacheline_size % pci_cache_line_size) == 0)
  1586. return 0;
  1587. /* Write the correct value. */
  1588. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  1589. /* Read it back. */
  1590. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1591. if (cacheline_size == pci_cache_line_size)
  1592. return 0;
  1593. dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
  1594. "supported\n", pci_cache_line_size << 2);
  1595. return -EINVAL;
  1596. }
  1597. /**
  1598. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  1599. * @dev: the PCI device for which MWI is enabled
  1600. *
  1601. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1602. *
  1603. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1604. */
  1605. int
  1606. pci_set_mwi(struct pci_dev *dev)
  1607. {
  1608. int rc;
  1609. u16 cmd;
  1610. rc = pci_set_cacheline_size(dev);
  1611. if (rc)
  1612. return rc;
  1613. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1614. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  1615. dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
  1616. cmd |= PCI_COMMAND_INVALIDATE;
  1617. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1618. }
  1619. return 0;
  1620. }
  1621. /**
  1622. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  1623. * @dev: the PCI device for which MWI is enabled
  1624. *
  1625. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1626. * Callers are not required to check the return value.
  1627. *
  1628. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1629. */
  1630. int pci_try_set_mwi(struct pci_dev *dev)
  1631. {
  1632. int rc = pci_set_mwi(dev);
  1633. return rc;
  1634. }
  1635. /**
  1636. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  1637. * @dev: the PCI device to disable
  1638. *
  1639. * Disables PCI Memory-Write-Invalidate transaction on the device
  1640. */
  1641. void
  1642. pci_clear_mwi(struct pci_dev *dev)
  1643. {
  1644. u16 cmd;
  1645. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1646. if (cmd & PCI_COMMAND_INVALIDATE) {
  1647. cmd &= ~PCI_COMMAND_INVALIDATE;
  1648. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1649. }
  1650. }
  1651. #endif /* ! PCI_DISABLE_MWI */
  1652. /**
  1653. * pci_intx - enables/disables PCI INTx for device dev
  1654. * @pdev: the PCI device to operate on
  1655. * @enable: boolean: whether to enable or disable PCI INTx
  1656. *
  1657. * Enables/disables PCI INTx for device dev
  1658. */
  1659. void
  1660. pci_intx(struct pci_dev *pdev, int enable)
  1661. {
  1662. u16 pci_command, new;
  1663. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  1664. if (enable) {
  1665. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  1666. } else {
  1667. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  1668. }
  1669. if (new != pci_command) {
  1670. struct pci_devres *dr;
  1671. pci_write_config_word(pdev, PCI_COMMAND, new);
  1672. dr = find_pci_dr(pdev);
  1673. if (dr && !dr->restore_intx) {
  1674. dr->restore_intx = 1;
  1675. dr->orig_intx = !enable;
  1676. }
  1677. }
  1678. }
  1679. /**
  1680. * pci_msi_off - disables any msi or msix capabilities
  1681. * @dev: the PCI device to operate on
  1682. *
  1683. * If you want to use msi see pci_enable_msi and friends.
  1684. * This is a lower level primitive that allows us to disable
  1685. * msi operation at the device level.
  1686. */
  1687. void pci_msi_off(struct pci_dev *dev)
  1688. {
  1689. int pos;
  1690. u16 control;
  1691. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1692. if (pos) {
  1693. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  1694. control &= ~PCI_MSI_FLAGS_ENABLE;
  1695. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  1696. }
  1697. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1698. if (pos) {
  1699. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  1700. control &= ~PCI_MSIX_FLAGS_ENABLE;
  1701. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  1702. }
  1703. }
  1704. #ifndef HAVE_ARCH_PCI_SET_DMA_MASK
  1705. /*
  1706. * These can be overridden by arch-specific implementations
  1707. */
  1708. int
  1709. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  1710. {
  1711. if (!pci_dma_supported(dev, mask))
  1712. return -EIO;
  1713. dev->dma_mask = mask;
  1714. return 0;
  1715. }
  1716. int
  1717. pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  1718. {
  1719. if (!pci_dma_supported(dev, mask))
  1720. return -EIO;
  1721. dev->dev.coherent_dma_mask = mask;
  1722. return 0;
  1723. }
  1724. #endif
  1725. #ifndef HAVE_ARCH_PCI_SET_DMA_MAX_SEGMENT_SIZE
  1726. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  1727. {
  1728. return dma_set_max_seg_size(&dev->dev, size);
  1729. }
  1730. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  1731. #endif
  1732. #ifndef HAVE_ARCH_PCI_SET_DMA_SEGMENT_BOUNDARY
  1733. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  1734. {
  1735. return dma_set_seg_boundary(&dev->dev, mask);
  1736. }
  1737. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  1738. #endif
  1739. static int __pcie_flr(struct pci_dev *dev, int probe)
  1740. {
  1741. u16 status;
  1742. u32 cap;
  1743. int exppos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1744. if (!exppos)
  1745. return -ENOTTY;
  1746. pci_read_config_dword(dev, exppos + PCI_EXP_DEVCAP, &cap);
  1747. if (!(cap & PCI_EXP_DEVCAP_FLR))
  1748. return -ENOTTY;
  1749. if (probe)
  1750. return 0;
  1751. pci_block_user_cfg_access(dev);
  1752. /* Wait for Transaction Pending bit clean */
  1753. msleep(100);
  1754. pci_read_config_word(dev, exppos + PCI_EXP_DEVSTA, &status);
  1755. if (status & PCI_EXP_DEVSTA_TRPND) {
  1756. dev_info(&dev->dev, "Busy after 100ms while trying to reset; "
  1757. "sleeping for 1 second\n");
  1758. ssleep(1);
  1759. pci_read_config_word(dev, exppos + PCI_EXP_DEVSTA, &status);
  1760. if (status & PCI_EXP_DEVSTA_TRPND)
  1761. dev_info(&dev->dev, "Still busy after 1s; "
  1762. "proceeding with reset anyway\n");
  1763. }
  1764. pci_write_config_word(dev, exppos + PCI_EXP_DEVCTL,
  1765. PCI_EXP_DEVCTL_BCR_FLR);
  1766. mdelay(100);
  1767. pci_unblock_user_cfg_access(dev);
  1768. return 0;
  1769. }
  1770. static int __pci_af_flr(struct pci_dev *dev, int probe)
  1771. {
  1772. int cappos = pci_find_capability(dev, PCI_CAP_ID_AF);
  1773. u8 status;
  1774. u8 cap;
  1775. if (!cappos)
  1776. return -ENOTTY;
  1777. pci_read_config_byte(dev, cappos + PCI_AF_CAP, &cap);
  1778. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  1779. return -ENOTTY;
  1780. if (probe)
  1781. return 0;
  1782. pci_block_user_cfg_access(dev);
  1783. /* Wait for Transaction Pending bit clean */
  1784. msleep(100);
  1785. pci_read_config_byte(dev, cappos + PCI_AF_STATUS, &status);
  1786. if (status & PCI_AF_STATUS_TP) {
  1787. dev_info(&dev->dev, "Busy after 100ms while trying to"
  1788. " reset; sleeping for 1 second\n");
  1789. ssleep(1);
  1790. pci_read_config_byte(dev,
  1791. cappos + PCI_AF_STATUS, &status);
  1792. if (status & PCI_AF_STATUS_TP)
  1793. dev_info(&dev->dev, "Still busy after 1s; "
  1794. "proceeding with reset anyway\n");
  1795. }
  1796. pci_write_config_byte(dev, cappos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  1797. mdelay(100);
  1798. pci_unblock_user_cfg_access(dev);
  1799. return 0;
  1800. }
  1801. static int __pci_reset_function(struct pci_dev *pdev, int probe)
  1802. {
  1803. int res;
  1804. res = __pcie_flr(pdev, probe);
  1805. if (res != -ENOTTY)
  1806. return res;
  1807. res = __pci_af_flr(pdev, probe);
  1808. if (res != -ENOTTY)
  1809. return res;
  1810. return res;
  1811. }
  1812. /**
  1813. * pci_execute_reset_function() - Reset a PCI device function
  1814. * @dev: Device function to reset
  1815. *
  1816. * Some devices allow an individual function to be reset without affecting
  1817. * other functions in the same device. The PCI device must be responsive
  1818. * to PCI config space in order to use this function.
  1819. *
  1820. * The device function is presumed to be unused when this function is called.
  1821. * Resetting the device will make the contents of PCI configuration space
  1822. * random, so any caller of this must be prepared to reinitialise the
  1823. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  1824. * etc.
  1825. *
  1826. * Returns 0 if the device function was successfully reset or -ENOTTY if the
  1827. * device doesn't support resetting a single function.
  1828. */
  1829. int pci_execute_reset_function(struct pci_dev *dev)
  1830. {
  1831. return __pci_reset_function(dev, 0);
  1832. }
  1833. EXPORT_SYMBOL_GPL(pci_execute_reset_function);
  1834. /**
  1835. * pci_reset_function() - quiesce and reset a PCI device function
  1836. * @dev: Device function to reset
  1837. *
  1838. * Some devices allow an individual function to be reset without affecting
  1839. * other functions in the same device. The PCI device must be responsive
  1840. * to PCI config space in order to use this function.
  1841. *
  1842. * This function does not just reset the PCI portion of a device, but
  1843. * clears all the state associated with the device. This function differs
  1844. * from pci_execute_reset_function in that it saves and restores device state
  1845. * over the reset.
  1846. *
  1847. * Returns 0 if the device function was successfully reset or -ENOTTY if the
  1848. * device doesn't support resetting a single function.
  1849. */
  1850. int pci_reset_function(struct pci_dev *dev)
  1851. {
  1852. int r = __pci_reset_function(dev, 1);
  1853. if (r < 0)
  1854. return r;
  1855. if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0)
  1856. disable_irq(dev->irq);
  1857. pci_save_state(dev);
  1858. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  1859. r = pci_execute_reset_function(dev);
  1860. pci_restore_state(dev);
  1861. if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0)
  1862. enable_irq(dev->irq);
  1863. return r;
  1864. }
  1865. EXPORT_SYMBOL_GPL(pci_reset_function);
  1866. /**
  1867. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  1868. * @dev: PCI device to query
  1869. *
  1870. * Returns mmrbc: maximum designed memory read count in bytes
  1871. * or appropriate error value.
  1872. */
  1873. int pcix_get_max_mmrbc(struct pci_dev *dev)
  1874. {
  1875. int err, cap;
  1876. u32 stat;
  1877. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1878. if (!cap)
  1879. return -EINVAL;
  1880. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1881. if (err)
  1882. return -EINVAL;
  1883. return (stat & PCI_X_STATUS_MAX_READ) >> 12;
  1884. }
  1885. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  1886. /**
  1887. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  1888. * @dev: PCI device to query
  1889. *
  1890. * Returns mmrbc: maximum memory read count in bytes
  1891. * or appropriate error value.
  1892. */
  1893. int pcix_get_mmrbc(struct pci_dev *dev)
  1894. {
  1895. int ret, cap;
  1896. u32 cmd;
  1897. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1898. if (!cap)
  1899. return -EINVAL;
  1900. ret = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1901. if (!ret)
  1902. ret = 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  1903. return ret;
  1904. }
  1905. EXPORT_SYMBOL(pcix_get_mmrbc);
  1906. /**
  1907. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  1908. * @dev: PCI device to query
  1909. * @mmrbc: maximum memory read count in bytes
  1910. * valid values are 512, 1024, 2048, 4096
  1911. *
  1912. * If possible sets maximum memory read byte count, some bridges have erratas
  1913. * that prevent this.
  1914. */
  1915. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  1916. {
  1917. int cap, err = -EINVAL;
  1918. u32 stat, cmd, v, o;
  1919. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  1920. goto out;
  1921. v = ffs(mmrbc) - 10;
  1922. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1923. if (!cap)
  1924. goto out;
  1925. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1926. if (err)
  1927. goto out;
  1928. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  1929. return -E2BIG;
  1930. err = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1931. if (err)
  1932. goto out;
  1933. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  1934. if (o != v) {
  1935. if (v > o && dev->bus &&
  1936. (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  1937. return -EIO;
  1938. cmd &= ~PCI_X_CMD_MAX_READ;
  1939. cmd |= v << 2;
  1940. err = pci_write_config_dword(dev, cap + PCI_X_CMD, cmd);
  1941. }
  1942. out:
  1943. return err;
  1944. }
  1945. EXPORT_SYMBOL(pcix_set_mmrbc);
  1946. /**
  1947. * pcie_get_readrq - get PCI Express read request size
  1948. * @dev: PCI device to query
  1949. *
  1950. * Returns maximum memory read request in bytes
  1951. * or appropriate error value.
  1952. */
  1953. int pcie_get_readrq(struct pci_dev *dev)
  1954. {
  1955. int ret, cap;
  1956. u16 ctl;
  1957. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1958. if (!cap)
  1959. return -EINVAL;
  1960. ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1961. if (!ret)
  1962. ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  1963. return ret;
  1964. }
  1965. EXPORT_SYMBOL(pcie_get_readrq);
  1966. /**
  1967. * pcie_set_readrq - set PCI Express maximum memory read request
  1968. * @dev: PCI device to query
  1969. * @rq: maximum memory read count in bytes
  1970. * valid values are 128, 256, 512, 1024, 2048, 4096
  1971. *
  1972. * If possible sets maximum read byte count
  1973. */
  1974. int pcie_set_readrq(struct pci_dev *dev, int rq)
  1975. {
  1976. int cap, err = -EINVAL;
  1977. u16 ctl, v;
  1978. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  1979. goto out;
  1980. v = (ffs(rq) - 8) << 12;
  1981. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1982. if (!cap)
  1983. goto out;
  1984. err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1985. if (err)
  1986. goto out;
  1987. if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
  1988. ctl &= ~PCI_EXP_DEVCTL_READRQ;
  1989. ctl |= v;
  1990. err = pci_write_config_dword(dev, cap + PCI_EXP_DEVCTL, ctl);
  1991. }
  1992. out:
  1993. return err;
  1994. }
  1995. EXPORT_SYMBOL(pcie_set_readrq);
  1996. /**
  1997. * pci_select_bars - Make BAR mask from the type of resource
  1998. * @dev: the PCI device for which BAR mask is made
  1999. * @flags: resource type mask to be selected
  2000. *
  2001. * This helper routine makes bar mask from the type of resource.
  2002. */
  2003. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  2004. {
  2005. int i, bars = 0;
  2006. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  2007. if (pci_resource_flags(dev, i) & flags)
  2008. bars |= (1 << i);
  2009. return bars;
  2010. }
  2011. /**
  2012. * pci_resource_bar - get position of the BAR associated with a resource
  2013. * @dev: the PCI device
  2014. * @resno: the resource number
  2015. * @type: the BAR type to be filled in
  2016. *
  2017. * Returns BAR position in config space, or 0 if the BAR is invalid.
  2018. */
  2019. int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
  2020. {
  2021. if (resno < PCI_ROM_RESOURCE) {
  2022. *type = pci_bar_unknown;
  2023. return PCI_BASE_ADDRESS_0 + 4 * resno;
  2024. } else if (resno == PCI_ROM_RESOURCE) {
  2025. *type = pci_bar_mem32;
  2026. return dev->rom_base_reg;
  2027. }
  2028. dev_err(&dev->dev, "BAR: invalid resource #%d\n", resno);
  2029. return 0;
  2030. }
  2031. static void __devinit pci_no_domains(void)
  2032. {
  2033. #ifdef CONFIG_PCI_DOMAINS
  2034. pci_domains_supported = 0;
  2035. #endif
  2036. }
  2037. /**
  2038. * pci_ext_cfg_enabled - can we access extended PCI config space?
  2039. * @dev: The PCI device of the root bridge.
  2040. *
  2041. * Returns 1 if we can access PCI extended config space (offsets
  2042. * greater than 0xff). This is the default implementation. Architecture
  2043. * implementations can override this.
  2044. */
  2045. int __attribute__ ((weak)) pci_ext_cfg_avail(struct pci_dev *dev)
  2046. {
  2047. return 1;
  2048. }
  2049. static int __devinit pci_init(void)
  2050. {
  2051. struct pci_dev *dev = NULL;
  2052. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  2053. pci_fixup_device(pci_fixup_final, dev);
  2054. }
  2055. return 0;
  2056. }
  2057. static int __init pci_setup(char *str)
  2058. {
  2059. while (str) {
  2060. char *k = strchr(str, ',');
  2061. if (k)
  2062. *k++ = 0;
  2063. if (*str && (str = pcibios_setup(str)) && *str) {
  2064. if (!strcmp(str, "nomsi")) {
  2065. pci_no_msi();
  2066. } else if (!strcmp(str, "noaer")) {
  2067. pci_no_aer();
  2068. } else if (!strcmp(str, "nodomains")) {
  2069. pci_no_domains();
  2070. } else if (!strncmp(str, "cbiosize=", 9)) {
  2071. pci_cardbus_io_size = memparse(str + 9, &str);
  2072. } else if (!strncmp(str, "cbmemsize=", 10)) {
  2073. pci_cardbus_mem_size = memparse(str + 10, &str);
  2074. } else {
  2075. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  2076. str);
  2077. }
  2078. }
  2079. str = k;
  2080. }
  2081. return 0;
  2082. }
  2083. early_param("pci", pci_setup);
  2084. device_initcall(pci_init);
  2085. EXPORT_SYMBOL(pci_reenable_device);
  2086. EXPORT_SYMBOL(pci_enable_device_io);
  2087. EXPORT_SYMBOL(pci_enable_device_mem);
  2088. EXPORT_SYMBOL(pci_enable_device);
  2089. EXPORT_SYMBOL(pcim_enable_device);
  2090. EXPORT_SYMBOL(pcim_pin_device);
  2091. EXPORT_SYMBOL(pci_disable_device);
  2092. EXPORT_SYMBOL(pci_find_capability);
  2093. EXPORT_SYMBOL(pci_bus_find_capability);
  2094. EXPORT_SYMBOL(pci_release_regions);
  2095. EXPORT_SYMBOL(pci_request_regions);
  2096. EXPORT_SYMBOL(pci_request_regions_exclusive);
  2097. EXPORT_SYMBOL(pci_release_region);
  2098. EXPORT_SYMBOL(pci_request_region);
  2099. EXPORT_SYMBOL(pci_request_region_exclusive);
  2100. EXPORT_SYMBOL(pci_release_selected_regions);
  2101. EXPORT_SYMBOL(pci_request_selected_regions);
  2102. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  2103. EXPORT_SYMBOL(pci_set_master);
  2104. EXPORT_SYMBOL(pci_clear_master);
  2105. EXPORT_SYMBOL(pci_set_mwi);
  2106. EXPORT_SYMBOL(pci_try_set_mwi);
  2107. EXPORT_SYMBOL(pci_clear_mwi);
  2108. EXPORT_SYMBOL_GPL(pci_intx);
  2109. EXPORT_SYMBOL(pci_set_dma_mask);
  2110. EXPORT_SYMBOL(pci_set_consistent_dma_mask);
  2111. EXPORT_SYMBOL(pci_assign_resource);
  2112. EXPORT_SYMBOL(pci_find_parent_resource);
  2113. EXPORT_SYMBOL(pci_select_bars);
  2114. EXPORT_SYMBOL(pci_set_power_state);
  2115. EXPORT_SYMBOL(pci_save_state);
  2116. EXPORT_SYMBOL(pci_restore_state);
  2117. EXPORT_SYMBOL(pci_pme_capable);
  2118. EXPORT_SYMBOL(pci_pme_active);
  2119. EXPORT_SYMBOL(pci_enable_wake);
  2120. EXPORT_SYMBOL(pci_wake_from_d3);
  2121. EXPORT_SYMBOL(pci_target_state);
  2122. EXPORT_SYMBOL(pci_prepare_to_sleep);
  2123. EXPORT_SYMBOL(pci_back_from_sleep);
  2124. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);