pci.c 50 KB

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