pci.c 63 KB

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