pci.c 54 KB

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