pci.c 74 KB

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