pci.c 70 KB

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