pci.c 73 KB

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