pci.c 50 KB

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