pci.c 69 KB

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