pci.c 81 KB

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