pci.c 63 KB

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