pci.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727
  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. For devices with ARI enabled, the slot
  1311. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  1312. * the PCI Express Base Specification, Revision 2.1)
  1313. */
  1314. u8 pci_swizzle_interrupt_pin(struct pci_dev *dev, u8 pin)
  1315. {
  1316. int slot;
  1317. if (pci_ari_enabled(dev->bus))
  1318. slot = 0;
  1319. else
  1320. slot = PCI_SLOT(dev->devfn);
  1321. return (((pin - 1) + slot) % 4) + 1;
  1322. }
  1323. int
  1324. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  1325. {
  1326. u8 pin;
  1327. pin = dev->pin;
  1328. if (!pin)
  1329. return -1;
  1330. while (!pci_is_root_bus(dev->bus)) {
  1331. pin = pci_swizzle_interrupt_pin(dev, pin);
  1332. dev = dev->bus->self;
  1333. }
  1334. *bridge = dev;
  1335. return pin;
  1336. }
  1337. /**
  1338. * pci_common_swizzle - swizzle INTx all the way to root bridge
  1339. * @dev: the PCI device
  1340. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1341. *
  1342. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  1343. * bridges all the way up to a PCI root bus.
  1344. */
  1345. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  1346. {
  1347. u8 pin = *pinp;
  1348. while (!pci_is_root_bus(dev->bus)) {
  1349. pin = pci_swizzle_interrupt_pin(dev, pin);
  1350. dev = dev->bus->self;
  1351. }
  1352. *pinp = pin;
  1353. return PCI_SLOT(dev->devfn);
  1354. }
  1355. /**
  1356. * pci_release_region - Release a PCI bar
  1357. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  1358. * @bar: BAR to release
  1359. *
  1360. * Releases the PCI I/O and memory resources previously reserved by a
  1361. * successful call to pci_request_region. Call this function only
  1362. * after all use of the PCI regions has ceased.
  1363. */
  1364. void pci_release_region(struct pci_dev *pdev, int bar)
  1365. {
  1366. struct pci_devres *dr;
  1367. if (pci_resource_len(pdev, bar) == 0)
  1368. return;
  1369. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  1370. release_region(pci_resource_start(pdev, bar),
  1371. pci_resource_len(pdev, bar));
  1372. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  1373. release_mem_region(pci_resource_start(pdev, bar),
  1374. pci_resource_len(pdev, bar));
  1375. dr = find_pci_dr(pdev);
  1376. if (dr)
  1377. dr->region_mask &= ~(1 << bar);
  1378. }
  1379. /**
  1380. * __pci_request_region - Reserved PCI I/O and memory resource
  1381. * @pdev: PCI device whose resources are to be reserved
  1382. * @bar: BAR to be reserved
  1383. * @res_name: Name to be associated with resource.
  1384. * @exclusive: whether the region access is exclusive or not
  1385. *
  1386. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1387. * being reserved by owner @res_name. Do not access any
  1388. * address inside the PCI regions unless this call returns
  1389. * successfully.
  1390. *
  1391. * If @exclusive is set, then the region is marked so that userspace
  1392. * is explicitly not allowed to map the resource via /dev/mem or
  1393. * sysfs MMIO access.
  1394. *
  1395. * Returns 0 on success, or %EBUSY on error. A warning
  1396. * message is also printed on failure.
  1397. */
  1398. static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
  1399. int exclusive)
  1400. {
  1401. struct pci_devres *dr;
  1402. if (pci_resource_len(pdev, bar) == 0)
  1403. return 0;
  1404. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  1405. if (!request_region(pci_resource_start(pdev, bar),
  1406. pci_resource_len(pdev, bar), res_name))
  1407. goto err_out;
  1408. }
  1409. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  1410. if (!__request_mem_region(pci_resource_start(pdev, bar),
  1411. pci_resource_len(pdev, bar), res_name,
  1412. exclusive))
  1413. goto err_out;
  1414. }
  1415. dr = find_pci_dr(pdev);
  1416. if (dr)
  1417. dr->region_mask |= 1 << bar;
  1418. return 0;
  1419. err_out:
  1420. dev_warn(&pdev->dev, "BAR %d: can't reserve %s region %pR\n",
  1421. bar,
  1422. pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
  1423. &pdev->resource[bar]);
  1424. return -EBUSY;
  1425. }
  1426. /**
  1427. * pci_request_region - Reserve PCI I/O and memory resource
  1428. * @pdev: PCI device whose resources are to be reserved
  1429. * @bar: BAR to be reserved
  1430. * @res_name: Name to be associated with resource
  1431. *
  1432. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  1433. * being reserved by owner @res_name. Do not access any
  1434. * address inside the PCI regions unless this call returns
  1435. * successfully.
  1436. *
  1437. * Returns 0 on success, or %EBUSY on error. A warning
  1438. * message is also printed on failure.
  1439. */
  1440. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  1441. {
  1442. return __pci_request_region(pdev, bar, res_name, 0);
  1443. }
  1444. /**
  1445. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  1446. * @pdev: PCI device whose resources are to be reserved
  1447. * @bar: BAR to be reserved
  1448. * @res_name: Name to be associated with resource.
  1449. *
  1450. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1451. * being reserved by owner @res_name. Do not access any
  1452. * address inside the PCI regions unless this call returns
  1453. * successfully.
  1454. *
  1455. * Returns 0 on success, or %EBUSY on error. A warning
  1456. * message is also printed on failure.
  1457. *
  1458. * The key difference that _exclusive makes it that userspace is
  1459. * explicitly not allowed to map the resource via /dev/mem or
  1460. * sysfs.
  1461. */
  1462. int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
  1463. {
  1464. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  1465. }
  1466. /**
  1467. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  1468. * @pdev: PCI device whose resources were previously reserved
  1469. * @bars: Bitmask of BARs to be released
  1470. *
  1471. * Release selected PCI I/O and memory resources previously reserved.
  1472. * Call this function only after all use of the PCI regions has ceased.
  1473. */
  1474. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  1475. {
  1476. int i;
  1477. for (i = 0; i < 6; i++)
  1478. if (bars & (1 << i))
  1479. pci_release_region(pdev, i);
  1480. }
  1481. int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1482. const char *res_name, int excl)
  1483. {
  1484. int i;
  1485. for (i = 0; i < 6; i++)
  1486. if (bars & (1 << i))
  1487. if (__pci_request_region(pdev, i, res_name, excl))
  1488. goto err_out;
  1489. return 0;
  1490. err_out:
  1491. while(--i >= 0)
  1492. if (bars & (1 << i))
  1493. pci_release_region(pdev, i);
  1494. return -EBUSY;
  1495. }
  1496. /**
  1497. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  1498. * @pdev: PCI device whose resources are to be reserved
  1499. * @bars: Bitmask of BARs to be requested
  1500. * @res_name: Name to be associated with resource
  1501. */
  1502. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1503. const char *res_name)
  1504. {
  1505. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  1506. }
  1507. int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
  1508. int bars, const char *res_name)
  1509. {
  1510. return __pci_request_selected_regions(pdev, bars, res_name,
  1511. IORESOURCE_EXCLUSIVE);
  1512. }
  1513. /**
  1514. * pci_release_regions - Release reserved PCI I/O and memory resources
  1515. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  1516. *
  1517. * Releases all PCI I/O and memory resources previously reserved by a
  1518. * successful call to pci_request_regions. Call this function only
  1519. * after all use of the PCI regions has ceased.
  1520. */
  1521. void pci_release_regions(struct pci_dev *pdev)
  1522. {
  1523. pci_release_selected_regions(pdev, (1 << 6) - 1);
  1524. }
  1525. /**
  1526. * pci_request_regions - Reserved PCI I/O and memory resources
  1527. * @pdev: PCI device whose resources are to be reserved
  1528. * @res_name: Name to be associated with resource.
  1529. *
  1530. * Mark all PCI regions associated with PCI device @pdev as
  1531. * being reserved by owner @res_name. Do not access any
  1532. * address inside the PCI regions unless this call returns
  1533. * successfully.
  1534. *
  1535. * Returns 0 on success, or %EBUSY on error. A warning
  1536. * message is also printed on failure.
  1537. */
  1538. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  1539. {
  1540. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  1541. }
  1542. /**
  1543. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  1544. * @pdev: PCI device whose resources are to be reserved
  1545. * @res_name: Name to be associated with resource.
  1546. *
  1547. * Mark all PCI regions associated with PCI device @pdev as
  1548. * being reserved by owner @res_name. Do not access any
  1549. * address inside the PCI regions unless this call returns
  1550. * successfully.
  1551. *
  1552. * pci_request_regions_exclusive() will mark the region so that
  1553. * /dev/mem and the sysfs MMIO access will not be allowed.
  1554. *
  1555. * Returns 0 on success, or %EBUSY on error. A warning
  1556. * message is also printed on failure.
  1557. */
  1558. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  1559. {
  1560. return pci_request_selected_regions_exclusive(pdev,
  1561. ((1 << 6) - 1), res_name);
  1562. }
  1563. static void __pci_set_master(struct pci_dev *dev, bool enable)
  1564. {
  1565. u16 old_cmd, cmd;
  1566. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  1567. if (enable)
  1568. cmd = old_cmd | PCI_COMMAND_MASTER;
  1569. else
  1570. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  1571. if (cmd != old_cmd) {
  1572. dev_dbg(&dev->dev, "%s bus mastering\n",
  1573. enable ? "enabling" : "disabling");
  1574. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1575. }
  1576. dev->is_busmaster = enable;
  1577. }
  1578. /**
  1579. * pci_set_master - enables bus-mastering for device dev
  1580. * @dev: the PCI device to enable
  1581. *
  1582. * Enables bus-mastering on the device and calls pcibios_set_master()
  1583. * to do the needed arch specific settings.
  1584. */
  1585. void pci_set_master(struct pci_dev *dev)
  1586. {
  1587. __pci_set_master(dev, true);
  1588. pcibios_set_master(dev);
  1589. }
  1590. /**
  1591. * pci_clear_master - disables bus-mastering for device dev
  1592. * @dev: the PCI device to disable
  1593. */
  1594. void pci_clear_master(struct pci_dev *dev)
  1595. {
  1596. __pci_set_master(dev, false);
  1597. }
  1598. #ifdef PCI_DISABLE_MWI
  1599. int pci_set_mwi(struct pci_dev *dev)
  1600. {
  1601. return 0;
  1602. }
  1603. int pci_try_set_mwi(struct pci_dev *dev)
  1604. {
  1605. return 0;
  1606. }
  1607. void pci_clear_mwi(struct pci_dev *dev)
  1608. {
  1609. }
  1610. #else
  1611. #ifndef PCI_CACHE_LINE_BYTES
  1612. #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES
  1613. #endif
  1614. /* This can be overridden by arch code. */
  1615. /* Don't forget this is measured in 32-bit words, not bytes */
  1616. u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4;
  1617. /**
  1618. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  1619. * @dev: the PCI device for which MWI is to be enabled
  1620. *
  1621. * Helper function for pci_set_mwi.
  1622. * Originally copied from drivers/net/acenic.c.
  1623. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  1624. *
  1625. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1626. */
  1627. static int
  1628. pci_set_cacheline_size(struct pci_dev *dev)
  1629. {
  1630. u8 cacheline_size;
  1631. if (!pci_cache_line_size)
  1632. return -EINVAL; /* The system doesn't support MWI. */
  1633. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  1634. equal to or multiple of the right value. */
  1635. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1636. if (cacheline_size >= pci_cache_line_size &&
  1637. (cacheline_size % pci_cache_line_size) == 0)
  1638. return 0;
  1639. /* Write the correct value. */
  1640. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  1641. /* Read it back. */
  1642. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1643. if (cacheline_size == pci_cache_line_size)
  1644. return 0;
  1645. dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
  1646. "supported\n", pci_cache_line_size << 2);
  1647. return -EINVAL;
  1648. }
  1649. /**
  1650. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  1651. * @dev: the PCI device for which MWI is enabled
  1652. *
  1653. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1654. *
  1655. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1656. */
  1657. int
  1658. pci_set_mwi(struct pci_dev *dev)
  1659. {
  1660. int rc;
  1661. u16 cmd;
  1662. rc = pci_set_cacheline_size(dev);
  1663. if (rc)
  1664. return rc;
  1665. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1666. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  1667. dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
  1668. cmd |= PCI_COMMAND_INVALIDATE;
  1669. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1670. }
  1671. return 0;
  1672. }
  1673. /**
  1674. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  1675. * @dev: the PCI device for which MWI is enabled
  1676. *
  1677. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1678. * Callers are not required to check the return value.
  1679. *
  1680. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1681. */
  1682. int pci_try_set_mwi(struct pci_dev *dev)
  1683. {
  1684. int rc = pci_set_mwi(dev);
  1685. return rc;
  1686. }
  1687. /**
  1688. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  1689. * @dev: the PCI device to disable
  1690. *
  1691. * Disables PCI Memory-Write-Invalidate transaction on the device
  1692. */
  1693. void
  1694. pci_clear_mwi(struct pci_dev *dev)
  1695. {
  1696. u16 cmd;
  1697. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1698. if (cmd & PCI_COMMAND_INVALIDATE) {
  1699. cmd &= ~PCI_COMMAND_INVALIDATE;
  1700. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1701. }
  1702. }
  1703. #endif /* ! PCI_DISABLE_MWI */
  1704. /**
  1705. * pci_intx - enables/disables PCI INTx for device dev
  1706. * @pdev: the PCI device to operate on
  1707. * @enable: boolean: whether to enable or disable PCI INTx
  1708. *
  1709. * Enables/disables PCI INTx for device dev
  1710. */
  1711. void
  1712. pci_intx(struct pci_dev *pdev, int enable)
  1713. {
  1714. u16 pci_command, new;
  1715. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  1716. if (enable) {
  1717. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  1718. } else {
  1719. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  1720. }
  1721. if (new != pci_command) {
  1722. struct pci_devres *dr;
  1723. pci_write_config_word(pdev, PCI_COMMAND, new);
  1724. dr = find_pci_dr(pdev);
  1725. if (dr && !dr->restore_intx) {
  1726. dr->restore_intx = 1;
  1727. dr->orig_intx = !enable;
  1728. }
  1729. }
  1730. }
  1731. /**
  1732. * pci_msi_off - disables any msi or msix capabilities
  1733. * @dev: the PCI device to operate on
  1734. *
  1735. * If you want to use msi see pci_enable_msi and friends.
  1736. * This is a lower level primitive that allows us to disable
  1737. * msi operation at the device level.
  1738. */
  1739. void pci_msi_off(struct pci_dev *dev)
  1740. {
  1741. int pos;
  1742. u16 control;
  1743. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1744. if (pos) {
  1745. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  1746. control &= ~PCI_MSI_FLAGS_ENABLE;
  1747. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  1748. }
  1749. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1750. if (pos) {
  1751. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  1752. control &= ~PCI_MSIX_FLAGS_ENABLE;
  1753. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  1754. }
  1755. }
  1756. #ifndef HAVE_ARCH_PCI_SET_DMA_MASK
  1757. /*
  1758. * These can be overridden by arch-specific implementations
  1759. */
  1760. int
  1761. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  1762. {
  1763. if (!pci_dma_supported(dev, mask))
  1764. return -EIO;
  1765. dev->dma_mask = mask;
  1766. return 0;
  1767. }
  1768. int
  1769. pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  1770. {
  1771. if (!pci_dma_supported(dev, mask))
  1772. return -EIO;
  1773. dev->dev.coherent_dma_mask = mask;
  1774. return 0;
  1775. }
  1776. #endif
  1777. #ifndef HAVE_ARCH_PCI_SET_DMA_MAX_SEGMENT_SIZE
  1778. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  1779. {
  1780. return dma_set_max_seg_size(&dev->dev, size);
  1781. }
  1782. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  1783. #endif
  1784. #ifndef HAVE_ARCH_PCI_SET_DMA_SEGMENT_BOUNDARY
  1785. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  1786. {
  1787. return dma_set_seg_boundary(&dev->dev, mask);
  1788. }
  1789. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  1790. #endif
  1791. static int pcie_flr(struct pci_dev *dev, int probe)
  1792. {
  1793. int i;
  1794. int pos;
  1795. u32 cap;
  1796. u16 status;
  1797. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1798. if (!pos)
  1799. return -ENOTTY;
  1800. pci_read_config_dword(dev, pos + PCI_EXP_DEVCAP, &cap);
  1801. if (!(cap & PCI_EXP_DEVCAP_FLR))
  1802. return -ENOTTY;
  1803. if (probe)
  1804. return 0;
  1805. /* Wait for Transaction Pending bit clean */
  1806. for (i = 0; i < 4; i++) {
  1807. if (i)
  1808. msleep((1 << (i - 1)) * 100);
  1809. pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &status);
  1810. if (!(status & PCI_EXP_DEVSTA_TRPND))
  1811. goto clear;
  1812. }
  1813. dev_err(&dev->dev, "transaction is not cleared; "
  1814. "proceeding with reset anyway\n");
  1815. clear:
  1816. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL,
  1817. PCI_EXP_DEVCTL_BCR_FLR);
  1818. msleep(100);
  1819. return 0;
  1820. }
  1821. static int pci_af_flr(struct pci_dev *dev, int probe)
  1822. {
  1823. int i;
  1824. int pos;
  1825. u8 cap;
  1826. u8 status;
  1827. pos = pci_find_capability(dev, PCI_CAP_ID_AF);
  1828. if (!pos)
  1829. return -ENOTTY;
  1830. pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
  1831. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  1832. return -ENOTTY;
  1833. if (probe)
  1834. return 0;
  1835. /* Wait for Transaction Pending bit clean */
  1836. for (i = 0; i < 4; i++) {
  1837. if (i)
  1838. msleep((1 << (i - 1)) * 100);
  1839. pci_read_config_byte(dev, pos + PCI_AF_STATUS, &status);
  1840. if (!(status & PCI_AF_STATUS_TP))
  1841. goto clear;
  1842. }
  1843. dev_err(&dev->dev, "transaction is not cleared; "
  1844. "proceeding with reset anyway\n");
  1845. clear:
  1846. pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  1847. msleep(100);
  1848. return 0;
  1849. }
  1850. static int pci_pm_reset(struct pci_dev *dev, int probe)
  1851. {
  1852. u16 csr;
  1853. if (!dev->pm_cap)
  1854. return -ENOTTY;
  1855. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
  1856. if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
  1857. return -ENOTTY;
  1858. if (probe)
  1859. return 0;
  1860. if (dev->current_state != PCI_D0)
  1861. return -EINVAL;
  1862. csr &= ~PCI_PM_CTRL_STATE_MASK;
  1863. csr |= PCI_D3hot;
  1864. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  1865. msleep(pci_pm_d3_delay);
  1866. csr &= ~PCI_PM_CTRL_STATE_MASK;
  1867. csr |= PCI_D0;
  1868. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  1869. msleep(pci_pm_d3_delay);
  1870. return 0;
  1871. }
  1872. static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
  1873. {
  1874. u16 ctrl;
  1875. struct pci_dev *pdev;
  1876. if (pci_is_root_bus(dev->bus) || dev->subordinate || !dev->bus->self)
  1877. return -ENOTTY;
  1878. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  1879. if (pdev != dev)
  1880. return -ENOTTY;
  1881. if (probe)
  1882. return 0;
  1883. pci_read_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, &ctrl);
  1884. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  1885. pci_write_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, ctrl);
  1886. msleep(100);
  1887. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  1888. pci_write_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, ctrl);
  1889. msleep(100);
  1890. return 0;
  1891. }
  1892. static int pci_dev_reset(struct pci_dev *dev, int probe)
  1893. {
  1894. int rc;
  1895. might_sleep();
  1896. if (!probe) {
  1897. pci_block_user_cfg_access(dev);
  1898. /* block PM suspend, driver probe, etc. */
  1899. down(&dev->dev.sem);
  1900. }
  1901. rc = pcie_flr(dev, probe);
  1902. if (rc != -ENOTTY)
  1903. goto done;
  1904. rc = pci_af_flr(dev, probe);
  1905. if (rc != -ENOTTY)
  1906. goto done;
  1907. rc = pci_pm_reset(dev, probe);
  1908. if (rc != -ENOTTY)
  1909. goto done;
  1910. rc = pci_parent_bus_reset(dev, probe);
  1911. done:
  1912. if (!probe) {
  1913. up(&dev->dev.sem);
  1914. pci_unblock_user_cfg_access(dev);
  1915. }
  1916. return rc;
  1917. }
  1918. /**
  1919. * __pci_reset_function - reset a PCI device function
  1920. * @dev: PCI device to reset
  1921. *
  1922. * Some devices allow an individual function to be reset without affecting
  1923. * other functions in the same device. The PCI device must be responsive
  1924. * to PCI config space in order to use this function.
  1925. *
  1926. * The device function is presumed to be unused when this function is called.
  1927. * Resetting the device will make the contents of PCI configuration space
  1928. * random, so any caller of this must be prepared to reinitialise the
  1929. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  1930. * etc.
  1931. *
  1932. * Returns 0 if the device function was successfully reset or negative if the
  1933. * device doesn't support resetting a single function.
  1934. */
  1935. int __pci_reset_function(struct pci_dev *dev)
  1936. {
  1937. return pci_dev_reset(dev, 0);
  1938. }
  1939. EXPORT_SYMBOL_GPL(__pci_reset_function);
  1940. /**
  1941. * pci_reset_function - quiesce and reset a PCI device function
  1942. * @dev: PCI device to reset
  1943. *
  1944. * Some devices allow an individual function to be reset without affecting
  1945. * other functions in the same device. The PCI device must be responsive
  1946. * to PCI config space in order to use this function.
  1947. *
  1948. * This function does not just reset the PCI portion of a device, but
  1949. * clears all the state associated with the device. This function differs
  1950. * from __pci_reset_function in that it saves and restores device state
  1951. * over the reset.
  1952. *
  1953. * Returns 0 if the device function was successfully reset or negative if the
  1954. * device doesn't support resetting a single function.
  1955. */
  1956. int pci_reset_function(struct pci_dev *dev)
  1957. {
  1958. int rc;
  1959. rc = pci_dev_reset(dev, 1);
  1960. if (rc)
  1961. return rc;
  1962. pci_save_state(dev);
  1963. /*
  1964. * both INTx and MSI are disabled after the Interrupt Disable bit
  1965. * is set and the Bus Master bit is cleared.
  1966. */
  1967. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  1968. rc = pci_dev_reset(dev, 0);
  1969. pci_restore_state(dev);
  1970. return rc;
  1971. }
  1972. EXPORT_SYMBOL_GPL(pci_reset_function);
  1973. /**
  1974. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  1975. * @dev: PCI device to query
  1976. *
  1977. * Returns mmrbc: maximum designed memory read count in bytes
  1978. * or appropriate error value.
  1979. */
  1980. int pcix_get_max_mmrbc(struct pci_dev *dev)
  1981. {
  1982. int err, cap;
  1983. u32 stat;
  1984. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1985. if (!cap)
  1986. return -EINVAL;
  1987. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1988. if (err)
  1989. return -EINVAL;
  1990. return (stat & PCI_X_STATUS_MAX_READ) >> 12;
  1991. }
  1992. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  1993. /**
  1994. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  1995. * @dev: PCI device to query
  1996. *
  1997. * Returns mmrbc: maximum memory read count in bytes
  1998. * or appropriate error value.
  1999. */
  2000. int pcix_get_mmrbc(struct pci_dev *dev)
  2001. {
  2002. int ret, cap;
  2003. u32 cmd;
  2004. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  2005. if (!cap)
  2006. return -EINVAL;
  2007. ret = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  2008. if (!ret)
  2009. ret = 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  2010. return ret;
  2011. }
  2012. EXPORT_SYMBOL(pcix_get_mmrbc);
  2013. /**
  2014. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  2015. * @dev: PCI device to query
  2016. * @mmrbc: maximum memory read count in bytes
  2017. * valid values are 512, 1024, 2048, 4096
  2018. *
  2019. * If possible sets maximum memory read byte count, some bridges have erratas
  2020. * that prevent this.
  2021. */
  2022. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  2023. {
  2024. int cap, err = -EINVAL;
  2025. u32 stat, cmd, v, o;
  2026. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  2027. goto out;
  2028. v = ffs(mmrbc) - 10;
  2029. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  2030. if (!cap)
  2031. goto out;
  2032. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  2033. if (err)
  2034. goto out;
  2035. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  2036. return -E2BIG;
  2037. err = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  2038. if (err)
  2039. goto out;
  2040. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  2041. if (o != v) {
  2042. if (v > o && dev->bus &&
  2043. (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  2044. return -EIO;
  2045. cmd &= ~PCI_X_CMD_MAX_READ;
  2046. cmd |= v << 2;
  2047. err = pci_write_config_dword(dev, cap + PCI_X_CMD, cmd);
  2048. }
  2049. out:
  2050. return err;
  2051. }
  2052. EXPORT_SYMBOL(pcix_set_mmrbc);
  2053. /**
  2054. * pcie_get_readrq - get PCI Express read request size
  2055. * @dev: PCI device to query
  2056. *
  2057. * Returns maximum memory read request in bytes
  2058. * or appropriate error value.
  2059. */
  2060. int pcie_get_readrq(struct pci_dev *dev)
  2061. {
  2062. int ret, cap;
  2063. u16 ctl;
  2064. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  2065. if (!cap)
  2066. return -EINVAL;
  2067. ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  2068. if (!ret)
  2069. ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  2070. return ret;
  2071. }
  2072. EXPORT_SYMBOL(pcie_get_readrq);
  2073. /**
  2074. * pcie_set_readrq - set PCI Express maximum memory read request
  2075. * @dev: PCI device to query
  2076. * @rq: maximum memory read count in bytes
  2077. * valid values are 128, 256, 512, 1024, 2048, 4096
  2078. *
  2079. * If possible sets maximum read byte count
  2080. */
  2081. int pcie_set_readrq(struct pci_dev *dev, int rq)
  2082. {
  2083. int cap, err = -EINVAL;
  2084. u16 ctl, v;
  2085. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  2086. goto out;
  2087. v = (ffs(rq) - 8) << 12;
  2088. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  2089. if (!cap)
  2090. goto out;
  2091. err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  2092. if (err)
  2093. goto out;
  2094. if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
  2095. ctl &= ~PCI_EXP_DEVCTL_READRQ;
  2096. ctl |= v;
  2097. err = pci_write_config_dword(dev, cap + PCI_EXP_DEVCTL, ctl);
  2098. }
  2099. out:
  2100. return err;
  2101. }
  2102. EXPORT_SYMBOL(pcie_set_readrq);
  2103. /**
  2104. * pci_select_bars - Make BAR mask from the type of resource
  2105. * @dev: the PCI device for which BAR mask is made
  2106. * @flags: resource type mask to be selected
  2107. *
  2108. * This helper routine makes bar mask from the type of resource.
  2109. */
  2110. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  2111. {
  2112. int i, bars = 0;
  2113. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  2114. if (pci_resource_flags(dev, i) & flags)
  2115. bars |= (1 << i);
  2116. return bars;
  2117. }
  2118. /**
  2119. * pci_resource_bar - get position of the BAR associated with a resource
  2120. * @dev: the PCI device
  2121. * @resno: the resource number
  2122. * @type: the BAR type to be filled in
  2123. *
  2124. * Returns BAR position in config space, or 0 if the BAR is invalid.
  2125. */
  2126. int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
  2127. {
  2128. int reg;
  2129. if (resno < PCI_ROM_RESOURCE) {
  2130. *type = pci_bar_unknown;
  2131. return PCI_BASE_ADDRESS_0 + 4 * resno;
  2132. } else if (resno == PCI_ROM_RESOURCE) {
  2133. *type = pci_bar_mem32;
  2134. return dev->rom_base_reg;
  2135. } else if (resno < PCI_BRIDGE_RESOURCES) {
  2136. /* device specific resource */
  2137. reg = pci_iov_resource_bar(dev, resno, type);
  2138. if (reg)
  2139. return reg;
  2140. }
  2141. dev_err(&dev->dev, "BAR: invalid resource #%d\n", resno);
  2142. return 0;
  2143. }
  2144. #define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
  2145. static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
  2146. spinlock_t resource_alignment_lock = SPIN_LOCK_UNLOCKED;
  2147. /**
  2148. * pci_specified_resource_alignment - get resource alignment specified by user.
  2149. * @dev: the PCI device to get
  2150. *
  2151. * RETURNS: Resource alignment if it is specified.
  2152. * Zero if it is not specified.
  2153. */
  2154. resource_size_t pci_specified_resource_alignment(struct pci_dev *dev)
  2155. {
  2156. int seg, bus, slot, func, align_order, count;
  2157. resource_size_t align = 0;
  2158. char *p;
  2159. spin_lock(&resource_alignment_lock);
  2160. p = resource_alignment_param;
  2161. while (*p) {
  2162. count = 0;
  2163. if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
  2164. p[count] == '@') {
  2165. p += count + 1;
  2166. } else {
  2167. align_order = -1;
  2168. }
  2169. if (sscanf(p, "%x:%x:%x.%x%n",
  2170. &seg, &bus, &slot, &func, &count) != 4) {
  2171. seg = 0;
  2172. if (sscanf(p, "%x:%x.%x%n",
  2173. &bus, &slot, &func, &count) != 3) {
  2174. /* Invalid format */
  2175. printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
  2176. p);
  2177. break;
  2178. }
  2179. }
  2180. p += count;
  2181. if (seg == pci_domain_nr(dev->bus) &&
  2182. bus == dev->bus->number &&
  2183. slot == PCI_SLOT(dev->devfn) &&
  2184. func == PCI_FUNC(dev->devfn)) {
  2185. if (align_order == -1) {
  2186. align = PAGE_SIZE;
  2187. } else {
  2188. align = 1 << align_order;
  2189. }
  2190. /* Found */
  2191. break;
  2192. }
  2193. if (*p != ';' && *p != ',') {
  2194. /* End of param or invalid format */
  2195. break;
  2196. }
  2197. p++;
  2198. }
  2199. spin_unlock(&resource_alignment_lock);
  2200. return align;
  2201. }
  2202. /**
  2203. * pci_is_reassigndev - check if specified PCI is target device to reassign
  2204. * @dev: the PCI device to check
  2205. *
  2206. * RETURNS: non-zero for PCI device is a target device to reassign,
  2207. * or zero is not.
  2208. */
  2209. int pci_is_reassigndev(struct pci_dev *dev)
  2210. {
  2211. return (pci_specified_resource_alignment(dev) != 0);
  2212. }
  2213. ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
  2214. {
  2215. if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
  2216. count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
  2217. spin_lock(&resource_alignment_lock);
  2218. strncpy(resource_alignment_param, buf, count);
  2219. resource_alignment_param[count] = '\0';
  2220. spin_unlock(&resource_alignment_lock);
  2221. return count;
  2222. }
  2223. ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
  2224. {
  2225. size_t count;
  2226. spin_lock(&resource_alignment_lock);
  2227. count = snprintf(buf, size, "%s", resource_alignment_param);
  2228. spin_unlock(&resource_alignment_lock);
  2229. return count;
  2230. }
  2231. static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
  2232. {
  2233. return pci_get_resource_alignment_param(buf, PAGE_SIZE);
  2234. }
  2235. static ssize_t pci_resource_alignment_store(struct bus_type *bus,
  2236. const char *buf, size_t count)
  2237. {
  2238. return pci_set_resource_alignment_param(buf, count);
  2239. }
  2240. BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
  2241. pci_resource_alignment_store);
  2242. static int __init pci_resource_alignment_sysfs_init(void)
  2243. {
  2244. return bus_create_file(&pci_bus_type,
  2245. &bus_attr_resource_alignment);
  2246. }
  2247. late_initcall(pci_resource_alignment_sysfs_init);
  2248. static void __devinit pci_no_domains(void)
  2249. {
  2250. #ifdef CONFIG_PCI_DOMAINS
  2251. pci_domains_supported = 0;
  2252. #endif
  2253. }
  2254. /**
  2255. * pci_ext_cfg_enabled - can we access extended PCI config space?
  2256. * @dev: The PCI device of the root bridge.
  2257. *
  2258. * Returns 1 if we can access PCI extended config space (offsets
  2259. * greater than 0xff). This is the default implementation. Architecture
  2260. * implementations can override this.
  2261. */
  2262. int __attribute__ ((weak)) pci_ext_cfg_avail(struct pci_dev *dev)
  2263. {
  2264. return 1;
  2265. }
  2266. static int __devinit pci_init(void)
  2267. {
  2268. struct pci_dev *dev = NULL;
  2269. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  2270. pci_fixup_device(pci_fixup_final, dev);
  2271. }
  2272. return 0;
  2273. }
  2274. static int __init pci_setup(char *str)
  2275. {
  2276. while (str) {
  2277. char *k = strchr(str, ',');
  2278. if (k)
  2279. *k++ = 0;
  2280. if (*str && (str = pcibios_setup(str)) && *str) {
  2281. if (!strcmp(str, "nomsi")) {
  2282. pci_no_msi();
  2283. } else if (!strcmp(str, "noaer")) {
  2284. pci_no_aer();
  2285. } else if (!strcmp(str, "nodomains")) {
  2286. pci_no_domains();
  2287. } else if (!strncmp(str, "cbiosize=", 9)) {
  2288. pci_cardbus_io_size = memparse(str + 9, &str);
  2289. } else if (!strncmp(str, "cbmemsize=", 10)) {
  2290. pci_cardbus_mem_size = memparse(str + 10, &str);
  2291. } else if (!strncmp(str, "resource_alignment=", 19)) {
  2292. pci_set_resource_alignment_param(str + 19,
  2293. strlen(str + 19));
  2294. } else if (!strncmp(str, "ecrc=", 5)) {
  2295. pcie_ecrc_get_policy(str + 5);
  2296. } else {
  2297. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  2298. str);
  2299. }
  2300. }
  2301. str = k;
  2302. }
  2303. return 0;
  2304. }
  2305. early_param("pci", pci_setup);
  2306. device_initcall(pci_init);
  2307. EXPORT_SYMBOL(pci_reenable_device);
  2308. EXPORT_SYMBOL(pci_enable_device_io);
  2309. EXPORT_SYMBOL(pci_enable_device_mem);
  2310. EXPORT_SYMBOL(pci_enable_device);
  2311. EXPORT_SYMBOL(pcim_enable_device);
  2312. EXPORT_SYMBOL(pcim_pin_device);
  2313. EXPORT_SYMBOL(pci_disable_device);
  2314. EXPORT_SYMBOL(pci_find_capability);
  2315. EXPORT_SYMBOL(pci_bus_find_capability);
  2316. EXPORT_SYMBOL(pci_release_regions);
  2317. EXPORT_SYMBOL(pci_request_regions);
  2318. EXPORT_SYMBOL(pci_request_regions_exclusive);
  2319. EXPORT_SYMBOL(pci_release_region);
  2320. EXPORT_SYMBOL(pci_request_region);
  2321. EXPORT_SYMBOL(pci_request_region_exclusive);
  2322. EXPORT_SYMBOL(pci_release_selected_regions);
  2323. EXPORT_SYMBOL(pci_request_selected_regions);
  2324. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  2325. EXPORT_SYMBOL(pci_set_master);
  2326. EXPORT_SYMBOL(pci_clear_master);
  2327. EXPORT_SYMBOL(pci_set_mwi);
  2328. EXPORT_SYMBOL(pci_try_set_mwi);
  2329. EXPORT_SYMBOL(pci_clear_mwi);
  2330. EXPORT_SYMBOL_GPL(pci_intx);
  2331. EXPORT_SYMBOL(pci_set_dma_mask);
  2332. EXPORT_SYMBOL(pci_set_consistent_dma_mask);
  2333. EXPORT_SYMBOL(pci_assign_resource);
  2334. EXPORT_SYMBOL(pci_find_parent_resource);
  2335. EXPORT_SYMBOL(pci_select_bars);
  2336. EXPORT_SYMBOL(pci_set_power_state);
  2337. EXPORT_SYMBOL(pci_save_state);
  2338. EXPORT_SYMBOL(pci_restore_state);
  2339. EXPORT_SYMBOL(pci_pme_capable);
  2340. EXPORT_SYMBOL(pci_pme_active);
  2341. EXPORT_SYMBOL(pci_enable_wake);
  2342. EXPORT_SYMBOL(pci_wake_from_d3);
  2343. EXPORT_SYMBOL(pci_target_state);
  2344. EXPORT_SYMBOL(pci_prepare_to_sleep);
  2345. EXPORT_SYMBOL(pci_back_from_sleep);
  2346. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);