pci.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428
  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 <linux/pci_hotplug.h>
  25. #include <asm-generic/pci-bridge.h>
  26. #include <asm/setup.h>
  27. #include "pci.h"
  28. const char *pci_power_names[] = {
  29. "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
  30. };
  31. EXPORT_SYMBOL_GPL(pci_power_names);
  32. int isa_dma_bridge_buggy;
  33. EXPORT_SYMBOL(isa_dma_bridge_buggy);
  34. int pci_pci_problems;
  35. EXPORT_SYMBOL(pci_pci_problems);
  36. unsigned int pci_pm_d3_delay;
  37. static void pci_pme_list_scan(struct work_struct *work);
  38. static LIST_HEAD(pci_pme_list);
  39. static DEFINE_MUTEX(pci_pme_list_mutex);
  40. static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
  41. struct pci_pme_device {
  42. struct list_head list;
  43. struct pci_dev *dev;
  44. };
  45. #define PME_TIMEOUT 1000 /* How long between PME checks */
  46. static void pci_dev_d3_sleep(struct pci_dev *dev)
  47. {
  48. unsigned int delay = dev->d3_delay;
  49. if (delay < pci_pm_d3_delay)
  50. delay = pci_pm_d3_delay;
  51. msleep(delay);
  52. }
  53. #ifdef CONFIG_PCI_DOMAINS
  54. int pci_domains_supported = 1;
  55. #endif
  56. #define DEFAULT_CARDBUS_IO_SIZE (256)
  57. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  58. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  59. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  60. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  61. #define DEFAULT_HOTPLUG_IO_SIZE (256)
  62. #define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
  63. /* pci=hpmemsize=nnM,hpiosize=nn can override this */
  64. unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
  65. unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
  66. enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_TUNE_OFF;
  67. /*
  68. * The default CLS is used if arch didn't set CLS explicitly and not
  69. * all pci devices agree on the same value. Arch can override either
  70. * the dfl or actual value as it sees fit. Don't forget this is
  71. * measured in 32-bit words, not bytes.
  72. */
  73. u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
  74. u8 pci_cache_line_size;
  75. /*
  76. * If we set up a device for bus mastering, we need to check the latency
  77. * timer as certain BIOSes forget to set it properly.
  78. */
  79. unsigned int pcibios_max_latency = 255;
  80. /* If set, the PCIe ARI capability will not be used. */
  81. static bool pcie_ari_disabled;
  82. /**
  83. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  84. * @bus: pointer to PCI bus structure to search
  85. *
  86. * Given a PCI bus, returns the highest PCI bus number present in the set
  87. * including the given PCI bus and its list of child PCI buses.
  88. */
  89. unsigned char pci_bus_max_busnr(struct pci_bus* bus)
  90. {
  91. struct list_head *tmp;
  92. unsigned char max, n;
  93. max = bus->busn_res.end;
  94. list_for_each(tmp, &bus->children) {
  95. n = pci_bus_max_busnr(pci_bus_b(tmp));
  96. if(n > max)
  97. max = n;
  98. }
  99. return max;
  100. }
  101. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  102. #ifdef CONFIG_HAS_IOMEM
  103. void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
  104. {
  105. /*
  106. * Make sure the BAR is actually a memory resource, not an IO resource
  107. */
  108. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  109. WARN_ON(1);
  110. return NULL;
  111. }
  112. return ioremap_nocache(pci_resource_start(pdev, bar),
  113. pci_resource_len(pdev, bar));
  114. }
  115. EXPORT_SYMBOL_GPL(pci_ioremap_bar);
  116. #endif
  117. #define PCI_FIND_CAP_TTL 48
  118. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  119. u8 pos, int cap, int *ttl)
  120. {
  121. u8 id;
  122. while ((*ttl)--) {
  123. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  124. if (pos < 0x40)
  125. break;
  126. pos &= ~3;
  127. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  128. &id);
  129. if (id == 0xff)
  130. break;
  131. if (id == cap)
  132. return pos;
  133. pos += PCI_CAP_LIST_NEXT;
  134. }
  135. return 0;
  136. }
  137. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  138. u8 pos, int cap)
  139. {
  140. int ttl = PCI_FIND_CAP_TTL;
  141. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  142. }
  143. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  144. {
  145. return __pci_find_next_cap(dev->bus, dev->devfn,
  146. pos + PCI_CAP_LIST_NEXT, cap);
  147. }
  148. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  149. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  150. unsigned int devfn, u8 hdr_type)
  151. {
  152. u16 status;
  153. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  154. if (!(status & PCI_STATUS_CAP_LIST))
  155. return 0;
  156. switch (hdr_type) {
  157. case PCI_HEADER_TYPE_NORMAL:
  158. case PCI_HEADER_TYPE_BRIDGE:
  159. return PCI_CAPABILITY_LIST;
  160. case PCI_HEADER_TYPE_CARDBUS:
  161. return PCI_CB_CAPABILITY_LIST;
  162. default:
  163. return 0;
  164. }
  165. return 0;
  166. }
  167. /**
  168. * pci_find_capability - query for devices' capabilities
  169. * @dev: PCI device to query
  170. * @cap: capability code
  171. *
  172. * Tell if a device supports a given PCI capability.
  173. * Returns the address of the requested capability structure within the
  174. * device's PCI configuration space or 0 in case the device does not
  175. * support it. Possible values for @cap:
  176. *
  177. * %PCI_CAP_ID_PM Power Management
  178. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  179. * %PCI_CAP_ID_VPD Vital Product Data
  180. * %PCI_CAP_ID_SLOTID Slot Identification
  181. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  182. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  183. * %PCI_CAP_ID_PCIX PCI-X
  184. * %PCI_CAP_ID_EXP PCI Express
  185. */
  186. int pci_find_capability(struct pci_dev *dev, int cap)
  187. {
  188. int pos;
  189. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  190. if (pos)
  191. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  192. return pos;
  193. }
  194. /**
  195. * pci_bus_find_capability - query for devices' capabilities
  196. * @bus: the PCI bus to query
  197. * @devfn: PCI device to query
  198. * @cap: capability code
  199. *
  200. * Like pci_find_capability() but works for pci devices that do not have a
  201. * pci_dev structure set up yet.
  202. *
  203. * Returns the address of the requested capability structure within the
  204. * device's PCI configuration space or 0 in case the device does not
  205. * support it.
  206. */
  207. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  208. {
  209. int pos;
  210. u8 hdr_type;
  211. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  212. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  213. if (pos)
  214. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  215. return pos;
  216. }
  217. /**
  218. * pci_find_next_ext_capability - Find an extended capability
  219. * @dev: PCI device to query
  220. * @start: address at which to start looking (0 to start at beginning of list)
  221. * @cap: capability code
  222. *
  223. * Returns the address of the next matching extended capability structure
  224. * within the device's PCI configuration space or 0 if the device does
  225. * not support it. Some capabilities can occur several times, e.g., the
  226. * vendor-specific capability, and this provides a way to find them all.
  227. */
  228. int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
  229. {
  230. u32 header;
  231. int ttl;
  232. int pos = PCI_CFG_SPACE_SIZE;
  233. /* minimum 8 bytes per capability */
  234. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  235. if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
  236. return 0;
  237. if (start)
  238. pos = start;
  239. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  240. return 0;
  241. /*
  242. * If we have no capabilities, this is indicated by cap ID,
  243. * cap version and next pointer all being 0.
  244. */
  245. if (header == 0)
  246. return 0;
  247. while (ttl-- > 0) {
  248. if (PCI_EXT_CAP_ID(header) == cap && pos != start)
  249. return pos;
  250. pos = PCI_EXT_CAP_NEXT(header);
  251. if (pos < PCI_CFG_SPACE_SIZE)
  252. break;
  253. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  254. break;
  255. }
  256. return 0;
  257. }
  258. EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
  259. /**
  260. * pci_find_ext_capability - Find an extended capability
  261. * @dev: PCI device to query
  262. * @cap: capability code
  263. *
  264. * Returns the address of the requested extended capability structure
  265. * within the device's PCI configuration space or 0 if the device does
  266. * not support it. Possible values for @cap:
  267. *
  268. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  269. * %PCI_EXT_CAP_ID_VC Virtual Channel
  270. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  271. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  272. */
  273. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  274. {
  275. return pci_find_next_ext_capability(dev, 0, cap);
  276. }
  277. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  278. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  279. {
  280. int rc, ttl = PCI_FIND_CAP_TTL;
  281. u8 cap, mask;
  282. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  283. mask = HT_3BIT_CAP_MASK;
  284. else
  285. mask = HT_5BIT_CAP_MASK;
  286. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  287. PCI_CAP_ID_HT, &ttl);
  288. while (pos) {
  289. rc = pci_read_config_byte(dev, pos + 3, &cap);
  290. if (rc != PCIBIOS_SUCCESSFUL)
  291. return 0;
  292. if ((cap & mask) == ht_cap)
  293. return pos;
  294. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  295. pos + PCI_CAP_LIST_NEXT,
  296. PCI_CAP_ID_HT, &ttl);
  297. }
  298. return 0;
  299. }
  300. /**
  301. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  302. * @dev: PCI device to query
  303. * @pos: Position from which to continue searching
  304. * @ht_cap: Hypertransport capability code
  305. *
  306. * To be used in conjunction with pci_find_ht_capability() to search for
  307. * all capabilities matching @ht_cap. @pos should always be a value returned
  308. * from pci_find_ht_capability().
  309. *
  310. * NB. To be 100% safe against broken PCI devices, the caller should take
  311. * steps to avoid an infinite loop.
  312. */
  313. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  314. {
  315. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  316. }
  317. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  318. /**
  319. * pci_find_ht_capability - query a device's Hypertransport capabilities
  320. * @dev: PCI device to query
  321. * @ht_cap: Hypertransport capability code
  322. *
  323. * Tell if a device supports a given Hypertransport capability.
  324. * Returns an address within the device's PCI configuration space
  325. * or 0 in case the device does not support the request capability.
  326. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  327. * which has a Hypertransport capability matching @ht_cap.
  328. */
  329. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  330. {
  331. int pos;
  332. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  333. if (pos)
  334. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  335. return pos;
  336. }
  337. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  338. /**
  339. * pci_find_parent_resource - return resource region of parent bus of given region
  340. * @dev: PCI device structure contains resources to be searched
  341. * @res: child resource record for which parent is sought
  342. *
  343. * For given resource region of given device, return the resource
  344. * region of parent bus the given region is contained in or where
  345. * it should be allocated from.
  346. */
  347. struct resource *
  348. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  349. {
  350. const struct pci_bus *bus = dev->bus;
  351. int i;
  352. struct resource *best = NULL, *r;
  353. pci_bus_for_each_resource(bus, r, i) {
  354. if (!r)
  355. continue;
  356. if (res->start && !(res->start >= r->start && res->end <= r->end))
  357. continue; /* Not contained */
  358. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  359. continue; /* Wrong type */
  360. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  361. return r; /* Exact match */
  362. /* We can't insert a non-prefetch resource inside a prefetchable parent .. */
  363. if (r->flags & IORESOURCE_PREFETCH)
  364. continue;
  365. /* .. but we can put a prefetchable resource inside a non-prefetchable one */
  366. if (!best)
  367. best = r;
  368. }
  369. return best;
  370. }
  371. /**
  372. * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
  373. * @dev: the PCI device to operate on
  374. * @pos: config space offset of status word
  375. * @mask: mask of bit(s) to care about in status word
  376. *
  377. * Return 1 when mask bit(s) in status word clear, 0 otherwise.
  378. */
  379. int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
  380. {
  381. int i;
  382. /* Wait for Transaction Pending bit clean */
  383. for (i = 0; i < 4; i++) {
  384. u16 status;
  385. if (i)
  386. msleep((1 << (i - 1)) * 100);
  387. pci_read_config_word(dev, pos, &status);
  388. if (!(status & mask))
  389. return 1;
  390. }
  391. return 0;
  392. }
  393. /**
  394. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  395. * @dev: PCI device to have its BARs restored
  396. *
  397. * Restore the BAR values for a given device, so as to make it
  398. * accessible by its driver.
  399. */
  400. static void
  401. pci_restore_bars(struct pci_dev *dev)
  402. {
  403. int i;
  404. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
  405. pci_update_resource(dev, i);
  406. }
  407. static struct pci_platform_pm_ops *pci_platform_pm;
  408. int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
  409. {
  410. if (!ops->is_manageable || !ops->set_state || !ops->choose_state
  411. || !ops->sleep_wake)
  412. return -EINVAL;
  413. pci_platform_pm = ops;
  414. return 0;
  415. }
  416. static inline bool platform_pci_power_manageable(struct pci_dev *dev)
  417. {
  418. return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
  419. }
  420. static inline int platform_pci_set_power_state(struct pci_dev *dev,
  421. pci_power_t t)
  422. {
  423. return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
  424. }
  425. static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
  426. {
  427. return pci_platform_pm ?
  428. pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
  429. }
  430. static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
  431. {
  432. return pci_platform_pm ?
  433. pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
  434. }
  435. static inline int platform_pci_run_wake(struct pci_dev *dev, bool enable)
  436. {
  437. return pci_platform_pm ?
  438. pci_platform_pm->run_wake(dev, enable) : -ENODEV;
  439. }
  440. /**
  441. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  442. * given PCI device
  443. * @dev: PCI device to handle.
  444. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  445. *
  446. * RETURN VALUE:
  447. * -EINVAL if the requested state is invalid.
  448. * -EIO if device does not support PCI PM or its PM capabilities register has a
  449. * wrong version, or device doesn't support the requested state.
  450. * 0 if device already is in the requested state.
  451. * 0 if device's power state has been successfully changed.
  452. */
  453. static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
  454. {
  455. u16 pmcsr;
  456. bool need_restore = false;
  457. /* Check if we're already there */
  458. if (dev->current_state == state)
  459. return 0;
  460. if (!dev->pm_cap)
  461. return -EIO;
  462. if (state < PCI_D0 || state > PCI_D3hot)
  463. return -EINVAL;
  464. /* Validate current state:
  465. * Can enter D0 from any state, but if we can only go deeper
  466. * to sleep if we're already in a low power state
  467. */
  468. if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  469. && dev->current_state > state) {
  470. dev_err(&dev->dev, "invalid power transition "
  471. "(from state %d to %d)\n", dev->current_state, state);
  472. return -EINVAL;
  473. }
  474. /* check if this device supports the desired state */
  475. if ((state == PCI_D1 && !dev->d1_support)
  476. || (state == PCI_D2 && !dev->d2_support))
  477. return -EIO;
  478. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  479. /* If we're (effectively) in D3, force entire word to 0.
  480. * This doesn't affect PME_Status, disables PME_En, and
  481. * sets PowerState to 0.
  482. */
  483. switch (dev->current_state) {
  484. case PCI_D0:
  485. case PCI_D1:
  486. case PCI_D2:
  487. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  488. pmcsr |= state;
  489. break;
  490. case PCI_D3hot:
  491. case PCI_D3cold:
  492. case PCI_UNKNOWN: /* Boot-up */
  493. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  494. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  495. need_restore = true;
  496. /* Fall-through: force to D0 */
  497. default:
  498. pmcsr = 0;
  499. break;
  500. }
  501. /* enter specified state */
  502. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  503. /* Mandatory power management transition delays */
  504. /* see PCI PM 1.1 5.6.1 table 18 */
  505. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  506. pci_dev_d3_sleep(dev);
  507. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  508. udelay(PCI_PM_D2_DELAY);
  509. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  510. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  511. if (dev->current_state != state && printk_ratelimit())
  512. dev_info(&dev->dev, "Refused to change power state, "
  513. "currently in D%d\n", dev->current_state);
  514. /*
  515. * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  516. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  517. * from D3hot to D0 _may_ perform an internal reset, thereby
  518. * going to "D0 Uninitialized" rather than "D0 Initialized".
  519. * For example, at least some versions of the 3c905B and the
  520. * 3c556B exhibit this behaviour.
  521. *
  522. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  523. * devices in a D3hot state at boot. Consequently, we need to
  524. * restore at least the BARs so that the device will be
  525. * accessible to its driver.
  526. */
  527. if (need_restore)
  528. pci_restore_bars(dev);
  529. if (dev->bus->self)
  530. pcie_aspm_pm_state_change(dev->bus->self);
  531. return 0;
  532. }
  533. /**
  534. * pci_update_current_state - Read PCI power state of given device from its
  535. * PCI PM registers and cache it
  536. * @dev: PCI device to handle.
  537. * @state: State to cache in case the device doesn't have the PM capability
  538. */
  539. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  540. {
  541. if (dev->pm_cap) {
  542. u16 pmcsr;
  543. /*
  544. * Configuration space is not accessible for device in
  545. * D3cold, so just keep or set D3cold for safety
  546. */
  547. if (dev->current_state == PCI_D3cold)
  548. return;
  549. if (state == PCI_D3cold) {
  550. dev->current_state = PCI_D3cold;
  551. return;
  552. }
  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_power_up - Put the given device into D0 forcibly
  561. * @dev: PCI device to power up
  562. */
  563. void pci_power_up(struct pci_dev *dev)
  564. {
  565. if (platform_pci_power_manageable(dev))
  566. platform_pci_set_power_state(dev, PCI_D0);
  567. pci_raw_set_power_state(dev, PCI_D0);
  568. pci_update_current_state(dev, PCI_D0);
  569. }
  570. /**
  571. * pci_platform_power_transition - Use platform to change device power state
  572. * @dev: PCI device to handle.
  573. * @state: State to put the device into.
  574. */
  575. static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
  576. {
  577. int error;
  578. if (platform_pci_power_manageable(dev)) {
  579. error = platform_pci_set_power_state(dev, state);
  580. if (!error)
  581. pci_update_current_state(dev, state);
  582. } else
  583. error = -ENODEV;
  584. if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
  585. dev->current_state = PCI_D0;
  586. return error;
  587. }
  588. /**
  589. * pci_wakeup - Wake up a PCI device
  590. * @pci_dev: Device to handle.
  591. * @ign: ignored parameter
  592. */
  593. static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
  594. {
  595. pci_wakeup_event(pci_dev);
  596. pm_request_resume(&pci_dev->dev);
  597. return 0;
  598. }
  599. /**
  600. * pci_wakeup_bus - Walk given bus and wake up devices on it
  601. * @bus: Top bus of the subtree to walk.
  602. */
  603. static void pci_wakeup_bus(struct pci_bus *bus)
  604. {
  605. if (bus)
  606. pci_walk_bus(bus, pci_wakeup, NULL);
  607. }
  608. /**
  609. * __pci_start_power_transition - Start power transition of a PCI device
  610. * @dev: PCI device to handle.
  611. * @state: State to put the device into.
  612. */
  613. static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
  614. {
  615. if (state == PCI_D0) {
  616. pci_platform_power_transition(dev, PCI_D0);
  617. /*
  618. * Mandatory power management transition delays, see
  619. * PCI Express Base Specification Revision 2.0 Section
  620. * 6.6.1: Conventional Reset. Do not delay for
  621. * devices powered on/off by corresponding bridge,
  622. * because have already delayed for the bridge.
  623. */
  624. if (dev->runtime_d3cold) {
  625. msleep(dev->d3cold_delay);
  626. /*
  627. * When powering on a bridge from D3cold, the
  628. * whole hierarchy may be powered on into
  629. * D0uninitialized state, resume them to give
  630. * them a chance to suspend again
  631. */
  632. pci_wakeup_bus(dev->subordinate);
  633. }
  634. }
  635. }
  636. /**
  637. * __pci_dev_set_current_state - Set current state of a PCI device
  638. * @dev: Device to handle
  639. * @data: pointer to state to be set
  640. */
  641. static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
  642. {
  643. pci_power_t state = *(pci_power_t *)data;
  644. dev->current_state = state;
  645. return 0;
  646. }
  647. /**
  648. * __pci_bus_set_current_state - Walk given bus and set current state of devices
  649. * @bus: Top bus of the subtree to walk.
  650. * @state: state to be set
  651. */
  652. static void __pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
  653. {
  654. if (bus)
  655. pci_walk_bus(bus, __pci_dev_set_current_state, &state);
  656. }
  657. /**
  658. * __pci_complete_power_transition - Complete power transition of a PCI device
  659. * @dev: PCI device to handle.
  660. * @state: State to put the device into.
  661. *
  662. * This function should not be called directly by device drivers.
  663. */
  664. int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
  665. {
  666. int ret;
  667. if (state <= PCI_D0)
  668. return -EINVAL;
  669. ret = pci_platform_power_transition(dev, state);
  670. /* Power off the bridge may power off the whole hierarchy */
  671. if (!ret && state == PCI_D3cold)
  672. __pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
  673. return ret;
  674. }
  675. EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
  676. /**
  677. * pci_set_power_state - Set the power state of a PCI device
  678. * @dev: PCI device to handle.
  679. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  680. *
  681. * Transition a device to a new power state, using the platform firmware and/or
  682. * the device's PCI PM registers.
  683. *
  684. * RETURN VALUE:
  685. * -EINVAL if the requested state is invalid.
  686. * -EIO if device does not support PCI PM or its PM capabilities register has a
  687. * wrong version, or device doesn't support the requested state.
  688. * 0 if device already is in the requested state.
  689. * 0 if device's power state has been successfully changed.
  690. */
  691. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  692. {
  693. int error;
  694. /* bound the state we're entering */
  695. if (state > PCI_D3cold)
  696. state = PCI_D3cold;
  697. else if (state < PCI_D0)
  698. state = PCI_D0;
  699. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  700. /*
  701. * If the device or the parent bridge do not support PCI PM,
  702. * ignore the request if we're doing anything other than putting
  703. * it into D0 (which would only happen on boot).
  704. */
  705. return 0;
  706. /* Check if we're already there */
  707. if (dev->current_state == state)
  708. return 0;
  709. __pci_start_power_transition(dev, state);
  710. /* This device is quirked not to be put into D3, so
  711. don't put it in D3 */
  712. if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  713. return 0;
  714. /*
  715. * To put device in D3cold, we put device into D3hot in native
  716. * way, then put device into D3cold with platform ops
  717. */
  718. error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
  719. PCI_D3hot : state);
  720. if (!__pci_complete_power_transition(dev, state))
  721. error = 0;
  722. /*
  723. * When aspm_policy is "powersave" this call ensures
  724. * that ASPM is configured.
  725. */
  726. if (!error && dev->bus->self)
  727. pcie_aspm_powersave_config_link(dev->bus->self);
  728. return error;
  729. }
  730. /**
  731. * pci_choose_state - Choose the power state of a PCI device
  732. * @dev: PCI device to be suspended
  733. * @state: target sleep state for the whole system. This is the value
  734. * that is passed to suspend() function.
  735. *
  736. * Returns PCI power state suitable for given device and given system
  737. * message.
  738. */
  739. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  740. {
  741. pci_power_t ret;
  742. if (!dev->pm_cap)
  743. return PCI_D0;
  744. ret = platform_pci_choose_state(dev);
  745. if (ret != PCI_POWER_ERROR)
  746. return ret;
  747. switch (state.event) {
  748. case PM_EVENT_ON:
  749. return PCI_D0;
  750. case PM_EVENT_FREEZE:
  751. case PM_EVENT_PRETHAW:
  752. /* REVISIT both freeze and pre-thaw "should" use D0 */
  753. case PM_EVENT_SUSPEND:
  754. case PM_EVENT_HIBERNATE:
  755. return PCI_D3hot;
  756. default:
  757. dev_info(&dev->dev, "unrecognized suspend event %d\n",
  758. state.event);
  759. BUG();
  760. }
  761. return PCI_D0;
  762. }
  763. EXPORT_SYMBOL(pci_choose_state);
  764. #define PCI_EXP_SAVE_REGS 7
  765. static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
  766. u16 cap, bool extended)
  767. {
  768. struct pci_cap_saved_state *tmp;
  769. hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
  770. if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
  771. return tmp;
  772. }
  773. return NULL;
  774. }
  775. struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
  776. {
  777. return _pci_find_saved_cap(dev, cap, false);
  778. }
  779. struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
  780. {
  781. return _pci_find_saved_cap(dev, cap, true);
  782. }
  783. static int pci_save_pcie_state(struct pci_dev *dev)
  784. {
  785. int i = 0;
  786. struct pci_cap_saved_state *save_state;
  787. u16 *cap;
  788. if (!pci_is_pcie(dev))
  789. return 0;
  790. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  791. if (!save_state) {
  792. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  793. return -ENOMEM;
  794. }
  795. cap = (u16 *)&save_state->cap.data[0];
  796. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
  797. pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
  798. pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
  799. pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
  800. pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
  801. pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
  802. pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
  803. return 0;
  804. }
  805. static void pci_restore_pcie_state(struct pci_dev *dev)
  806. {
  807. int i = 0;
  808. struct pci_cap_saved_state *save_state;
  809. u16 *cap;
  810. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  811. if (!save_state)
  812. return;
  813. cap = (u16 *)&save_state->cap.data[0];
  814. pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
  815. pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
  816. pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
  817. pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
  818. pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
  819. pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
  820. pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
  821. }
  822. static int pci_save_pcix_state(struct pci_dev *dev)
  823. {
  824. int pos;
  825. struct pci_cap_saved_state *save_state;
  826. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  827. if (pos <= 0)
  828. return 0;
  829. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  830. if (!save_state) {
  831. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  832. return -ENOMEM;
  833. }
  834. pci_read_config_word(dev, pos + PCI_X_CMD,
  835. (u16 *)save_state->cap.data);
  836. return 0;
  837. }
  838. static void pci_restore_pcix_state(struct pci_dev *dev)
  839. {
  840. int i = 0, pos;
  841. struct pci_cap_saved_state *save_state;
  842. u16 *cap;
  843. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  844. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  845. if (!save_state || pos <= 0)
  846. return;
  847. cap = (u16 *)&save_state->cap.data[0];
  848. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  849. }
  850. /**
  851. * pci_save_state - save the PCI configuration space of a device before suspending
  852. * @dev: - PCI device that we're dealing with
  853. */
  854. int
  855. pci_save_state(struct pci_dev *dev)
  856. {
  857. int i;
  858. /* XXX: 100% dword access ok here? */
  859. for (i = 0; i < 16; i++)
  860. pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
  861. dev->state_saved = true;
  862. if ((i = pci_save_pcie_state(dev)) != 0)
  863. return i;
  864. if ((i = pci_save_pcix_state(dev)) != 0)
  865. return i;
  866. if ((i = pci_save_vc_state(dev)) != 0)
  867. return i;
  868. return 0;
  869. }
  870. static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
  871. u32 saved_val, int retry)
  872. {
  873. u32 val;
  874. pci_read_config_dword(pdev, offset, &val);
  875. if (val == saved_val)
  876. return;
  877. for (;;) {
  878. dev_dbg(&pdev->dev, "restoring config space at offset "
  879. "%#x (was %#x, writing %#x)\n", offset, val, saved_val);
  880. pci_write_config_dword(pdev, offset, saved_val);
  881. if (retry-- <= 0)
  882. return;
  883. pci_read_config_dword(pdev, offset, &val);
  884. if (val == saved_val)
  885. return;
  886. mdelay(1);
  887. }
  888. }
  889. static void pci_restore_config_space_range(struct pci_dev *pdev,
  890. int start, int end, int retry)
  891. {
  892. int index;
  893. for (index = end; index >= start; index--)
  894. pci_restore_config_dword(pdev, 4 * index,
  895. pdev->saved_config_space[index],
  896. retry);
  897. }
  898. static void pci_restore_config_space(struct pci_dev *pdev)
  899. {
  900. if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  901. pci_restore_config_space_range(pdev, 10, 15, 0);
  902. /* Restore BARs before the command register. */
  903. pci_restore_config_space_range(pdev, 4, 9, 10);
  904. pci_restore_config_space_range(pdev, 0, 3, 0);
  905. } else {
  906. pci_restore_config_space_range(pdev, 0, 15, 0);
  907. }
  908. }
  909. /**
  910. * pci_restore_state - Restore the saved state of a PCI device
  911. * @dev: - PCI device that we're dealing with
  912. */
  913. void pci_restore_state(struct pci_dev *dev)
  914. {
  915. if (!dev->state_saved)
  916. return;
  917. /* PCI Express register must be restored first */
  918. pci_restore_pcie_state(dev);
  919. pci_restore_ats_state(dev);
  920. pci_restore_vc_state(dev);
  921. pci_restore_config_space(dev);
  922. pci_restore_pcix_state(dev);
  923. pci_restore_msi_state(dev);
  924. pci_restore_iov_state(dev);
  925. dev->state_saved = false;
  926. }
  927. struct pci_saved_state {
  928. u32 config_space[16];
  929. struct pci_cap_saved_data cap[0];
  930. };
  931. /**
  932. * pci_store_saved_state - Allocate and return an opaque struct containing
  933. * the device saved state.
  934. * @dev: PCI device that we're dealing with
  935. *
  936. * Return NULL if no state or error.
  937. */
  938. struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
  939. {
  940. struct pci_saved_state *state;
  941. struct pci_cap_saved_state *tmp;
  942. struct pci_cap_saved_data *cap;
  943. size_t size;
  944. if (!dev->state_saved)
  945. return NULL;
  946. size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
  947. hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
  948. size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  949. state = kzalloc(size, GFP_KERNEL);
  950. if (!state)
  951. return NULL;
  952. memcpy(state->config_space, dev->saved_config_space,
  953. sizeof(state->config_space));
  954. cap = state->cap;
  955. hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
  956. size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  957. memcpy(cap, &tmp->cap, len);
  958. cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
  959. }
  960. /* Empty cap_save terminates list */
  961. return state;
  962. }
  963. EXPORT_SYMBOL_GPL(pci_store_saved_state);
  964. /**
  965. * pci_load_saved_state - Reload the provided save state into struct pci_dev.
  966. * @dev: PCI device that we're dealing with
  967. * @state: Saved state returned from pci_store_saved_state()
  968. */
  969. static int pci_load_saved_state(struct pci_dev *dev,
  970. struct pci_saved_state *state)
  971. {
  972. struct pci_cap_saved_data *cap;
  973. dev->state_saved = false;
  974. if (!state)
  975. return 0;
  976. memcpy(dev->saved_config_space, state->config_space,
  977. sizeof(state->config_space));
  978. cap = state->cap;
  979. while (cap->size) {
  980. struct pci_cap_saved_state *tmp;
  981. tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
  982. if (!tmp || tmp->cap.size != cap->size)
  983. return -EINVAL;
  984. memcpy(tmp->cap.data, cap->data, tmp->cap.size);
  985. cap = (struct pci_cap_saved_data *)((u8 *)cap +
  986. sizeof(struct pci_cap_saved_data) + cap->size);
  987. }
  988. dev->state_saved = true;
  989. return 0;
  990. }
  991. /**
  992. * pci_load_and_free_saved_state - Reload the save state pointed to by state,
  993. * and free the memory allocated for it.
  994. * @dev: PCI device that we're dealing with
  995. * @state: Pointer to saved state returned from pci_store_saved_state()
  996. */
  997. int pci_load_and_free_saved_state(struct pci_dev *dev,
  998. struct pci_saved_state **state)
  999. {
  1000. int ret = pci_load_saved_state(dev, *state);
  1001. kfree(*state);
  1002. *state = NULL;
  1003. return ret;
  1004. }
  1005. EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
  1006. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  1007. {
  1008. int err;
  1009. u16 cmd;
  1010. u8 pin;
  1011. err = pci_set_power_state(dev, PCI_D0);
  1012. if (err < 0 && err != -EIO)
  1013. return err;
  1014. err = pcibios_enable_device(dev, bars);
  1015. if (err < 0)
  1016. return err;
  1017. pci_fixup_device(pci_fixup_enable, dev);
  1018. if (dev->msi_enabled || dev->msix_enabled)
  1019. return 0;
  1020. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  1021. if (pin) {
  1022. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1023. if (cmd & PCI_COMMAND_INTX_DISABLE)
  1024. pci_write_config_word(dev, PCI_COMMAND,
  1025. cmd & ~PCI_COMMAND_INTX_DISABLE);
  1026. }
  1027. return 0;
  1028. }
  1029. /**
  1030. * pci_reenable_device - Resume abandoned device
  1031. * @dev: PCI device to be resumed
  1032. *
  1033. * Note this function is a backend of pci_default_resume and is not supposed
  1034. * to be called by normal code, write proper resume handler and use it instead.
  1035. */
  1036. int pci_reenable_device(struct pci_dev *dev)
  1037. {
  1038. if (pci_is_enabled(dev))
  1039. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  1040. return 0;
  1041. }
  1042. static void pci_enable_bridge(struct pci_dev *dev)
  1043. {
  1044. struct pci_dev *bridge;
  1045. int retval;
  1046. bridge = pci_upstream_bridge(dev);
  1047. if (bridge)
  1048. pci_enable_bridge(bridge);
  1049. if (pci_is_enabled(dev)) {
  1050. if (!dev->is_busmaster)
  1051. pci_set_master(dev);
  1052. return;
  1053. }
  1054. retval = pci_enable_device(dev);
  1055. if (retval)
  1056. dev_err(&dev->dev, "Error enabling bridge (%d), continuing\n",
  1057. retval);
  1058. pci_set_master(dev);
  1059. }
  1060. static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
  1061. {
  1062. struct pci_dev *bridge;
  1063. int err;
  1064. int i, bars = 0;
  1065. /*
  1066. * Power state could be unknown at this point, either due to a fresh
  1067. * boot or a device removal call. So get the current power state
  1068. * so that things like MSI message writing will behave as expected
  1069. * (e.g. if the device really is in D0 at enable time).
  1070. */
  1071. if (dev->pm_cap) {
  1072. u16 pmcsr;
  1073. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1074. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  1075. }
  1076. if (atomic_inc_return(&dev->enable_cnt) > 1)
  1077. return 0; /* already enabled */
  1078. bridge = pci_upstream_bridge(dev);
  1079. if (bridge)
  1080. pci_enable_bridge(bridge);
  1081. /* only skip sriov related */
  1082. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  1083. if (dev->resource[i].flags & flags)
  1084. bars |= (1 << i);
  1085. for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
  1086. if (dev->resource[i].flags & flags)
  1087. bars |= (1 << i);
  1088. err = do_pci_enable_device(dev, bars);
  1089. if (err < 0)
  1090. atomic_dec(&dev->enable_cnt);
  1091. return err;
  1092. }
  1093. /**
  1094. * pci_enable_device_io - Initialize a device for use with IO space
  1095. * @dev: PCI device to be initialized
  1096. *
  1097. * Initialize device before it's used by a driver. Ask low-level code
  1098. * to enable I/O resources. Wake up the device if it was suspended.
  1099. * Beware, this function can fail.
  1100. */
  1101. int pci_enable_device_io(struct pci_dev *dev)
  1102. {
  1103. return pci_enable_device_flags(dev, IORESOURCE_IO);
  1104. }
  1105. /**
  1106. * pci_enable_device_mem - Initialize a device for use with Memory space
  1107. * @dev: PCI device to be initialized
  1108. *
  1109. * Initialize device before it's used by a driver. Ask low-level code
  1110. * to enable Memory resources. Wake up the device if it was suspended.
  1111. * Beware, this function can fail.
  1112. */
  1113. int pci_enable_device_mem(struct pci_dev *dev)
  1114. {
  1115. return pci_enable_device_flags(dev, IORESOURCE_MEM);
  1116. }
  1117. /**
  1118. * pci_enable_device - Initialize device before it's used by a driver.
  1119. * @dev: PCI device to be initialized
  1120. *
  1121. * Initialize device before it's used by a driver. Ask low-level code
  1122. * to enable I/O and memory. Wake up the device if it was suspended.
  1123. * Beware, this function can fail.
  1124. *
  1125. * Note we don't actually enable the device many times if we call
  1126. * this function repeatedly (we just increment the count).
  1127. */
  1128. int pci_enable_device(struct pci_dev *dev)
  1129. {
  1130. return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  1131. }
  1132. /*
  1133. * Managed PCI resources. This manages device on/off, intx/msi/msix
  1134. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  1135. * there's no need to track it separately. pci_devres is initialized
  1136. * when a device is enabled using managed PCI device enable interface.
  1137. */
  1138. struct pci_devres {
  1139. unsigned int enabled:1;
  1140. unsigned int pinned:1;
  1141. unsigned int orig_intx:1;
  1142. unsigned int restore_intx:1;
  1143. u32 region_mask;
  1144. };
  1145. static void pcim_release(struct device *gendev, void *res)
  1146. {
  1147. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  1148. struct pci_devres *this = res;
  1149. int i;
  1150. if (dev->msi_enabled)
  1151. pci_disable_msi(dev);
  1152. if (dev->msix_enabled)
  1153. pci_disable_msix(dev);
  1154. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  1155. if (this->region_mask & (1 << i))
  1156. pci_release_region(dev, i);
  1157. if (this->restore_intx)
  1158. pci_intx(dev, this->orig_intx);
  1159. if (this->enabled && !this->pinned)
  1160. pci_disable_device(dev);
  1161. }
  1162. static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
  1163. {
  1164. struct pci_devres *dr, *new_dr;
  1165. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1166. if (dr)
  1167. return dr;
  1168. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  1169. if (!new_dr)
  1170. return NULL;
  1171. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  1172. }
  1173. static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
  1174. {
  1175. if (pci_is_managed(pdev))
  1176. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1177. return NULL;
  1178. }
  1179. /**
  1180. * pcim_enable_device - Managed pci_enable_device()
  1181. * @pdev: PCI device to be initialized
  1182. *
  1183. * Managed pci_enable_device().
  1184. */
  1185. int pcim_enable_device(struct pci_dev *pdev)
  1186. {
  1187. struct pci_devres *dr;
  1188. int rc;
  1189. dr = get_pci_dr(pdev);
  1190. if (unlikely(!dr))
  1191. return -ENOMEM;
  1192. if (dr->enabled)
  1193. return 0;
  1194. rc = pci_enable_device(pdev);
  1195. if (!rc) {
  1196. pdev->is_managed = 1;
  1197. dr->enabled = 1;
  1198. }
  1199. return rc;
  1200. }
  1201. /**
  1202. * pcim_pin_device - Pin managed PCI device
  1203. * @pdev: PCI device to pin
  1204. *
  1205. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  1206. * driver detach. @pdev must have been enabled with
  1207. * pcim_enable_device().
  1208. */
  1209. void pcim_pin_device(struct pci_dev *pdev)
  1210. {
  1211. struct pci_devres *dr;
  1212. dr = find_pci_dr(pdev);
  1213. WARN_ON(!dr || !dr->enabled);
  1214. if (dr)
  1215. dr->pinned = 1;
  1216. }
  1217. /*
  1218. * pcibios_add_device - provide arch specific hooks when adding device dev
  1219. * @dev: the PCI device being added
  1220. *
  1221. * Permits the platform to provide architecture specific functionality when
  1222. * devices are added. This is the default implementation. Architecture
  1223. * implementations can override this.
  1224. */
  1225. int __weak pcibios_add_device (struct pci_dev *dev)
  1226. {
  1227. return 0;
  1228. }
  1229. /**
  1230. * pcibios_release_device - provide arch specific hooks when releasing device dev
  1231. * @dev: the PCI device being released
  1232. *
  1233. * Permits the platform to provide architecture specific functionality when
  1234. * devices are released. This is the default implementation. Architecture
  1235. * implementations can override this.
  1236. */
  1237. void __weak pcibios_release_device(struct pci_dev *dev) {}
  1238. /**
  1239. * pcibios_disable_device - disable arch specific PCI resources for device dev
  1240. * @dev: the PCI device to disable
  1241. *
  1242. * Disables architecture specific PCI resources for the device. This
  1243. * is the default implementation. Architecture implementations can
  1244. * override this.
  1245. */
  1246. void __weak pcibios_disable_device (struct pci_dev *dev) {}
  1247. static void do_pci_disable_device(struct pci_dev *dev)
  1248. {
  1249. u16 pci_command;
  1250. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  1251. if (pci_command & PCI_COMMAND_MASTER) {
  1252. pci_command &= ~PCI_COMMAND_MASTER;
  1253. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  1254. }
  1255. pcibios_disable_device(dev);
  1256. }
  1257. /**
  1258. * pci_disable_enabled_device - Disable device without updating enable_cnt
  1259. * @dev: PCI device to disable
  1260. *
  1261. * NOTE: This function is a backend of PCI power management routines and is
  1262. * not supposed to be called drivers.
  1263. */
  1264. void pci_disable_enabled_device(struct pci_dev *dev)
  1265. {
  1266. if (pci_is_enabled(dev))
  1267. do_pci_disable_device(dev);
  1268. }
  1269. /**
  1270. * pci_disable_device - Disable PCI device after use
  1271. * @dev: PCI device to be disabled
  1272. *
  1273. * Signal to the system that the PCI device is not in use by the system
  1274. * anymore. This only involves disabling PCI bus-mastering, if active.
  1275. *
  1276. * Note we don't actually disable the device until all callers of
  1277. * pci_enable_device() have called pci_disable_device().
  1278. */
  1279. void
  1280. pci_disable_device(struct pci_dev *dev)
  1281. {
  1282. struct pci_devres *dr;
  1283. dr = find_pci_dr(dev);
  1284. if (dr)
  1285. dr->enabled = 0;
  1286. dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
  1287. "disabling already-disabled device");
  1288. if (atomic_dec_return(&dev->enable_cnt) != 0)
  1289. return;
  1290. do_pci_disable_device(dev);
  1291. dev->is_busmaster = 0;
  1292. }
  1293. /**
  1294. * pcibios_set_pcie_reset_state - set reset state for device dev
  1295. * @dev: the PCIe device reset
  1296. * @state: Reset state to enter into
  1297. *
  1298. *
  1299. * Sets the PCIe reset state for the device. This is the default
  1300. * implementation. Architecture implementations can override this.
  1301. */
  1302. int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
  1303. enum pcie_reset_state state)
  1304. {
  1305. return -EINVAL;
  1306. }
  1307. /**
  1308. * pci_set_pcie_reset_state - set reset state for device dev
  1309. * @dev: the PCIe device reset
  1310. * @state: Reset state to enter into
  1311. *
  1312. *
  1313. * Sets the PCI reset state for the device.
  1314. */
  1315. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  1316. {
  1317. return pcibios_set_pcie_reset_state(dev, state);
  1318. }
  1319. /**
  1320. * pci_check_pme_status - Check if given device has generated PME.
  1321. * @dev: Device to check.
  1322. *
  1323. * Check the PME status of the device and if set, clear it and clear PME enable
  1324. * (if set). Return 'true' if PME status and PME enable were both set or
  1325. * 'false' otherwise.
  1326. */
  1327. bool pci_check_pme_status(struct pci_dev *dev)
  1328. {
  1329. int pmcsr_pos;
  1330. u16 pmcsr;
  1331. bool ret = false;
  1332. if (!dev->pm_cap)
  1333. return false;
  1334. pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
  1335. pci_read_config_word(dev, pmcsr_pos, &pmcsr);
  1336. if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
  1337. return false;
  1338. /* Clear PME status. */
  1339. pmcsr |= PCI_PM_CTRL_PME_STATUS;
  1340. if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
  1341. /* Disable PME to avoid interrupt flood. */
  1342. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1343. ret = true;
  1344. }
  1345. pci_write_config_word(dev, pmcsr_pos, pmcsr);
  1346. return ret;
  1347. }
  1348. /**
  1349. * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
  1350. * @dev: Device to handle.
  1351. * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
  1352. *
  1353. * Check if @dev has generated PME and queue a resume request for it in that
  1354. * case.
  1355. */
  1356. static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
  1357. {
  1358. if (pme_poll_reset && dev->pme_poll)
  1359. dev->pme_poll = false;
  1360. if (pci_check_pme_status(dev)) {
  1361. pci_wakeup_event(dev);
  1362. pm_request_resume(&dev->dev);
  1363. }
  1364. return 0;
  1365. }
  1366. /**
  1367. * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
  1368. * @bus: Top bus of the subtree to walk.
  1369. */
  1370. void pci_pme_wakeup_bus(struct pci_bus *bus)
  1371. {
  1372. if (bus)
  1373. pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
  1374. }
  1375. /**
  1376. * pci_pme_capable - check the capability of PCI device to generate PME#
  1377. * @dev: PCI device to handle.
  1378. * @state: PCI state from which device will issue PME#.
  1379. */
  1380. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  1381. {
  1382. if (!dev->pm_cap)
  1383. return false;
  1384. return !!(dev->pme_support & (1 << state));
  1385. }
  1386. static void pci_pme_list_scan(struct work_struct *work)
  1387. {
  1388. struct pci_pme_device *pme_dev, *n;
  1389. mutex_lock(&pci_pme_list_mutex);
  1390. if (!list_empty(&pci_pme_list)) {
  1391. list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
  1392. if (pme_dev->dev->pme_poll) {
  1393. struct pci_dev *bridge;
  1394. bridge = pme_dev->dev->bus->self;
  1395. /*
  1396. * If bridge is in low power state, the
  1397. * configuration space of subordinate devices
  1398. * may be not accessible
  1399. */
  1400. if (bridge && bridge->current_state != PCI_D0)
  1401. continue;
  1402. pci_pme_wakeup(pme_dev->dev, NULL);
  1403. } else {
  1404. list_del(&pme_dev->list);
  1405. kfree(pme_dev);
  1406. }
  1407. }
  1408. if (!list_empty(&pci_pme_list))
  1409. schedule_delayed_work(&pci_pme_work,
  1410. msecs_to_jiffies(PME_TIMEOUT));
  1411. }
  1412. mutex_unlock(&pci_pme_list_mutex);
  1413. }
  1414. /**
  1415. * pci_pme_active - enable or disable PCI device's PME# function
  1416. * @dev: PCI device to handle.
  1417. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  1418. *
  1419. * The caller must verify that the device is capable of generating PME# before
  1420. * calling this function with @enable equal to 'true'.
  1421. */
  1422. void pci_pme_active(struct pci_dev *dev, bool enable)
  1423. {
  1424. u16 pmcsr;
  1425. if (!dev->pme_support)
  1426. return;
  1427. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1428. /* Clear PME_Status by writing 1 to it and enable PME# */
  1429. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  1430. if (!enable)
  1431. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1432. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  1433. /*
  1434. * PCI (as opposed to PCIe) PME requires that the device have
  1435. * its PME# line hooked up correctly. Not all hardware vendors
  1436. * do this, so the PME never gets delivered and the device
  1437. * remains asleep. The easiest way around this is to
  1438. * periodically walk the list of suspended devices and check
  1439. * whether any have their PME flag set. The assumption is that
  1440. * we'll wake up often enough anyway that this won't be a huge
  1441. * hit, and the power savings from the devices will still be a
  1442. * win.
  1443. *
  1444. * Although PCIe uses in-band PME message instead of PME# line
  1445. * to report PME, PME does not work for some PCIe devices in
  1446. * reality. For example, there are devices that set their PME
  1447. * status bits, but don't really bother to send a PME message;
  1448. * there are PCI Express Root Ports that don't bother to
  1449. * trigger interrupts when they receive PME messages from the
  1450. * devices below. So PME poll is used for PCIe devices too.
  1451. */
  1452. if (dev->pme_poll) {
  1453. struct pci_pme_device *pme_dev;
  1454. if (enable) {
  1455. pme_dev = kmalloc(sizeof(struct pci_pme_device),
  1456. GFP_KERNEL);
  1457. if (!pme_dev) {
  1458. dev_warn(&dev->dev, "can't enable PME#\n");
  1459. return;
  1460. }
  1461. pme_dev->dev = dev;
  1462. mutex_lock(&pci_pme_list_mutex);
  1463. list_add(&pme_dev->list, &pci_pme_list);
  1464. if (list_is_singular(&pci_pme_list))
  1465. schedule_delayed_work(&pci_pme_work,
  1466. msecs_to_jiffies(PME_TIMEOUT));
  1467. mutex_unlock(&pci_pme_list_mutex);
  1468. } else {
  1469. mutex_lock(&pci_pme_list_mutex);
  1470. list_for_each_entry(pme_dev, &pci_pme_list, list) {
  1471. if (pme_dev->dev == dev) {
  1472. list_del(&pme_dev->list);
  1473. kfree(pme_dev);
  1474. break;
  1475. }
  1476. }
  1477. mutex_unlock(&pci_pme_list_mutex);
  1478. }
  1479. }
  1480. dev_dbg(&dev->dev, "PME# %s\n", enable ? "enabled" : "disabled");
  1481. }
  1482. /**
  1483. * __pci_enable_wake - enable PCI device as wakeup event source
  1484. * @dev: PCI device affected
  1485. * @state: PCI state from which device will issue wakeup events
  1486. * @runtime: True if the events are to be generated at run time
  1487. * @enable: True to enable event generation; false to disable
  1488. *
  1489. * This enables the device as a wakeup event source, or disables it.
  1490. * When such events involves platform-specific hooks, those hooks are
  1491. * called automatically by this routine.
  1492. *
  1493. * Devices with legacy power management (no standard PCI PM capabilities)
  1494. * always require such platform hooks.
  1495. *
  1496. * RETURN VALUE:
  1497. * 0 is returned on success
  1498. * -EINVAL is returned if device is not supposed to wake up the system
  1499. * Error code depending on the platform is returned if both the platform and
  1500. * the native mechanism fail to enable the generation of wake-up events
  1501. */
  1502. int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
  1503. bool runtime, bool enable)
  1504. {
  1505. int ret = 0;
  1506. if (enable && !runtime && !device_may_wakeup(&dev->dev))
  1507. return -EINVAL;
  1508. /* Don't do the same thing twice in a row for one device. */
  1509. if (!!enable == !!dev->wakeup_prepared)
  1510. return 0;
  1511. /*
  1512. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  1513. * Anderson we should be doing PME# wake enable followed by ACPI wake
  1514. * enable. To disable wake-up we call the platform first, for symmetry.
  1515. */
  1516. if (enable) {
  1517. int error;
  1518. if (pci_pme_capable(dev, state))
  1519. pci_pme_active(dev, true);
  1520. else
  1521. ret = 1;
  1522. error = runtime ? platform_pci_run_wake(dev, true) :
  1523. platform_pci_sleep_wake(dev, true);
  1524. if (ret)
  1525. ret = error;
  1526. if (!ret)
  1527. dev->wakeup_prepared = true;
  1528. } else {
  1529. if (runtime)
  1530. platform_pci_run_wake(dev, false);
  1531. else
  1532. platform_pci_sleep_wake(dev, false);
  1533. pci_pme_active(dev, false);
  1534. dev->wakeup_prepared = false;
  1535. }
  1536. return ret;
  1537. }
  1538. EXPORT_SYMBOL(__pci_enable_wake);
  1539. /**
  1540. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  1541. * @dev: PCI device to prepare
  1542. * @enable: True to enable wake-up event generation; false to disable
  1543. *
  1544. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  1545. * and this function allows them to set that up cleanly - pci_enable_wake()
  1546. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  1547. * ordering constraints.
  1548. *
  1549. * This function only returns error code if the device is not capable of
  1550. * generating PME# from both D3_hot and D3_cold, and the platform is unable to
  1551. * enable wake-up power for it.
  1552. */
  1553. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  1554. {
  1555. return pci_pme_capable(dev, PCI_D3cold) ?
  1556. pci_enable_wake(dev, PCI_D3cold, enable) :
  1557. pci_enable_wake(dev, PCI_D3hot, enable);
  1558. }
  1559. /**
  1560. * pci_target_state - find an appropriate low power state for a given PCI dev
  1561. * @dev: PCI device
  1562. *
  1563. * Use underlying platform code to find a supported low power state for @dev.
  1564. * If the platform can't manage @dev, return the deepest state from which it
  1565. * can generate wake events, based on any available PME info.
  1566. */
  1567. static pci_power_t pci_target_state(struct pci_dev *dev)
  1568. {
  1569. pci_power_t target_state = PCI_D3hot;
  1570. if (platform_pci_power_manageable(dev)) {
  1571. /*
  1572. * Call the platform to choose the target state of the device
  1573. * and enable wake-up from this state if supported.
  1574. */
  1575. pci_power_t state = platform_pci_choose_state(dev);
  1576. switch (state) {
  1577. case PCI_POWER_ERROR:
  1578. case PCI_UNKNOWN:
  1579. break;
  1580. case PCI_D1:
  1581. case PCI_D2:
  1582. if (pci_no_d1d2(dev))
  1583. break;
  1584. default:
  1585. target_state = state;
  1586. }
  1587. } else if (!dev->pm_cap) {
  1588. target_state = PCI_D0;
  1589. } else if (device_may_wakeup(&dev->dev)) {
  1590. /*
  1591. * Find the deepest state from which the device can generate
  1592. * wake-up events, make it the target state and enable device
  1593. * to generate PME#.
  1594. */
  1595. if (dev->pme_support) {
  1596. while (target_state
  1597. && !(dev->pme_support & (1 << target_state)))
  1598. target_state--;
  1599. }
  1600. }
  1601. return target_state;
  1602. }
  1603. /**
  1604. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1605. * @dev: Device to handle.
  1606. *
  1607. * Choose the power state appropriate for the device depending on whether
  1608. * it can wake up the system and/or is power manageable by the platform
  1609. * (PCI_D3hot is the default) and put the device into that state.
  1610. */
  1611. int pci_prepare_to_sleep(struct pci_dev *dev)
  1612. {
  1613. pci_power_t target_state = pci_target_state(dev);
  1614. int error;
  1615. if (target_state == PCI_POWER_ERROR)
  1616. return -EIO;
  1617. /* D3cold during system suspend/hibernate is not supported */
  1618. if (target_state > PCI_D3hot)
  1619. target_state = PCI_D3hot;
  1620. pci_enable_wake(dev, target_state, device_may_wakeup(&dev->dev));
  1621. error = pci_set_power_state(dev, target_state);
  1622. if (error)
  1623. pci_enable_wake(dev, target_state, false);
  1624. return error;
  1625. }
  1626. /**
  1627. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1628. * @dev: Device to handle.
  1629. *
  1630. * Disable device's system wake-up capability and put it into D0.
  1631. */
  1632. int pci_back_from_sleep(struct pci_dev *dev)
  1633. {
  1634. pci_enable_wake(dev, PCI_D0, false);
  1635. return pci_set_power_state(dev, PCI_D0);
  1636. }
  1637. /**
  1638. * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
  1639. * @dev: PCI device being suspended.
  1640. *
  1641. * Prepare @dev to generate wake-up events at run time and put it into a low
  1642. * power state.
  1643. */
  1644. int pci_finish_runtime_suspend(struct pci_dev *dev)
  1645. {
  1646. pci_power_t target_state = pci_target_state(dev);
  1647. int error;
  1648. if (target_state == PCI_POWER_ERROR)
  1649. return -EIO;
  1650. dev->runtime_d3cold = target_state == PCI_D3cold;
  1651. __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev));
  1652. error = pci_set_power_state(dev, target_state);
  1653. if (error) {
  1654. __pci_enable_wake(dev, target_state, true, false);
  1655. dev->runtime_d3cold = false;
  1656. }
  1657. return error;
  1658. }
  1659. /**
  1660. * pci_dev_run_wake - Check if device can generate run-time wake-up events.
  1661. * @dev: Device to check.
  1662. *
  1663. * Return true if the device itself is capable of generating wake-up events
  1664. * (through the platform or using the native PCIe PME) or if the device supports
  1665. * PME and one of its upstream bridges can generate wake-up events.
  1666. */
  1667. bool pci_dev_run_wake(struct pci_dev *dev)
  1668. {
  1669. struct pci_bus *bus = dev->bus;
  1670. if (device_run_wake(&dev->dev))
  1671. return true;
  1672. if (!dev->pme_support)
  1673. return false;
  1674. while (bus->parent) {
  1675. struct pci_dev *bridge = bus->self;
  1676. if (device_run_wake(&bridge->dev))
  1677. return true;
  1678. bus = bus->parent;
  1679. }
  1680. /* We have reached the root bus. */
  1681. if (bus->bridge)
  1682. return device_run_wake(bus->bridge);
  1683. return false;
  1684. }
  1685. EXPORT_SYMBOL_GPL(pci_dev_run_wake);
  1686. void pci_config_pm_runtime_get(struct pci_dev *pdev)
  1687. {
  1688. struct device *dev = &pdev->dev;
  1689. struct device *parent = dev->parent;
  1690. if (parent)
  1691. pm_runtime_get_sync(parent);
  1692. pm_runtime_get_noresume(dev);
  1693. /*
  1694. * pdev->current_state is set to PCI_D3cold during suspending,
  1695. * so wait until suspending completes
  1696. */
  1697. pm_runtime_barrier(dev);
  1698. /*
  1699. * Only need to resume devices in D3cold, because config
  1700. * registers are still accessible for devices suspended but
  1701. * not in D3cold.
  1702. */
  1703. if (pdev->current_state == PCI_D3cold)
  1704. pm_runtime_resume(dev);
  1705. }
  1706. void pci_config_pm_runtime_put(struct pci_dev *pdev)
  1707. {
  1708. struct device *dev = &pdev->dev;
  1709. struct device *parent = dev->parent;
  1710. pm_runtime_put(dev);
  1711. if (parent)
  1712. pm_runtime_put_sync(parent);
  1713. }
  1714. /**
  1715. * pci_pm_init - Initialize PM functions of given PCI device
  1716. * @dev: PCI device to handle.
  1717. */
  1718. void pci_pm_init(struct pci_dev *dev)
  1719. {
  1720. int pm;
  1721. u16 pmc;
  1722. pm_runtime_forbid(&dev->dev);
  1723. pm_runtime_set_active(&dev->dev);
  1724. pm_runtime_enable(&dev->dev);
  1725. device_enable_async_suspend(&dev->dev);
  1726. dev->wakeup_prepared = false;
  1727. dev->pm_cap = 0;
  1728. dev->pme_support = 0;
  1729. /* find PCI PM capability in list */
  1730. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  1731. if (!pm)
  1732. return;
  1733. /* Check device's ability to generate PME# */
  1734. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  1735. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  1736. dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
  1737. pmc & PCI_PM_CAP_VER_MASK);
  1738. return;
  1739. }
  1740. dev->pm_cap = pm;
  1741. dev->d3_delay = PCI_PM_D3_WAIT;
  1742. dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
  1743. dev->d3cold_allowed = true;
  1744. dev->d1_support = false;
  1745. dev->d2_support = false;
  1746. if (!pci_no_d1d2(dev)) {
  1747. if (pmc & PCI_PM_CAP_D1)
  1748. dev->d1_support = true;
  1749. if (pmc & PCI_PM_CAP_D2)
  1750. dev->d2_support = true;
  1751. if (dev->d1_support || dev->d2_support)
  1752. dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
  1753. dev->d1_support ? " D1" : "",
  1754. dev->d2_support ? " D2" : "");
  1755. }
  1756. pmc &= PCI_PM_CAP_PME_MASK;
  1757. if (pmc) {
  1758. dev_printk(KERN_DEBUG, &dev->dev,
  1759. "PME# supported from%s%s%s%s%s\n",
  1760. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  1761. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  1762. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  1763. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  1764. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  1765. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  1766. dev->pme_poll = true;
  1767. /*
  1768. * Make device's PM flags reflect the wake-up capability, but
  1769. * let the user space enable it to wake up the system as needed.
  1770. */
  1771. device_set_wakeup_capable(&dev->dev, true);
  1772. /* Disable the PME# generation functionality */
  1773. pci_pme_active(dev, false);
  1774. }
  1775. }
  1776. static void pci_add_saved_cap(struct pci_dev *pci_dev,
  1777. struct pci_cap_saved_state *new_cap)
  1778. {
  1779. hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
  1780. }
  1781. /**
  1782. * _pci_add_cap_save_buffer - allocate buffer for saving given
  1783. * capability registers
  1784. * @dev: the PCI device
  1785. * @cap: the capability to allocate the buffer for
  1786. * @extended: Standard or Extended capability ID
  1787. * @size: requested size of the buffer
  1788. */
  1789. static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
  1790. bool extended, unsigned int size)
  1791. {
  1792. int pos;
  1793. struct pci_cap_saved_state *save_state;
  1794. if (extended)
  1795. pos = pci_find_ext_capability(dev, cap);
  1796. else
  1797. pos = pci_find_capability(dev, cap);
  1798. if (pos <= 0)
  1799. return 0;
  1800. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  1801. if (!save_state)
  1802. return -ENOMEM;
  1803. save_state->cap.cap_nr = cap;
  1804. save_state->cap.cap_extended = extended;
  1805. save_state->cap.size = size;
  1806. pci_add_saved_cap(dev, save_state);
  1807. return 0;
  1808. }
  1809. int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
  1810. {
  1811. return _pci_add_cap_save_buffer(dev, cap, false, size);
  1812. }
  1813. int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
  1814. {
  1815. return _pci_add_cap_save_buffer(dev, cap, true, size);
  1816. }
  1817. /**
  1818. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  1819. * @dev: the PCI device
  1820. */
  1821. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  1822. {
  1823. int error;
  1824. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
  1825. PCI_EXP_SAVE_REGS * sizeof(u16));
  1826. if (error)
  1827. dev_err(&dev->dev,
  1828. "unable to preallocate PCI Express save buffer\n");
  1829. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  1830. if (error)
  1831. dev_err(&dev->dev,
  1832. "unable to preallocate PCI-X save buffer\n");
  1833. pci_allocate_vc_save_buffers(dev);
  1834. }
  1835. void pci_free_cap_save_buffers(struct pci_dev *dev)
  1836. {
  1837. struct pci_cap_saved_state *tmp;
  1838. struct hlist_node *n;
  1839. hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
  1840. kfree(tmp);
  1841. }
  1842. /**
  1843. * pci_configure_ari - enable or disable ARI forwarding
  1844. * @dev: the PCI device
  1845. *
  1846. * If @dev and its upstream bridge both support ARI, enable ARI in the
  1847. * bridge. Otherwise, disable ARI in the bridge.
  1848. */
  1849. void pci_configure_ari(struct pci_dev *dev)
  1850. {
  1851. u32 cap;
  1852. struct pci_dev *bridge;
  1853. if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
  1854. return;
  1855. bridge = dev->bus->self;
  1856. if (!bridge)
  1857. return;
  1858. pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
  1859. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  1860. return;
  1861. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
  1862. pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
  1863. PCI_EXP_DEVCTL2_ARI);
  1864. bridge->ari_enabled = 1;
  1865. } else {
  1866. pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
  1867. PCI_EXP_DEVCTL2_ARI);
  1868. bridge->ari_enabled = 0;
  1869. }
  1870. }
  1871. static int pci_acs_enable;
  1872. /**
  1873. * pci_request_acs - ask for ACS to be enabled if supported
  1874. */
  1875. void pci_request_acs(void)
  1876. {
  1877. pci_acs_enable = 1;
  1878. }
  1879. /**
  1880. * pci_enable_acs - enable ACS if hardware support it
  1881. * @dev: the PCI device
  1882. */
  1883. void pci_enable_acs(struct pci_dev *dev)
  1884. {
  1885. int pos;
  1886. u16 cap;
  1887. u16 ctrl;
  1888. if (!pci_acs_enable)
  1889. return;
  1890. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  1891. if (!pos)
  1892. return;
  1893. pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
  1894. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  1895. /* Source Validation */
  1896. ctrl |= (cap & PCI_ACS_SV);
  1897. /* P2P Request Redirect */
  1898. ctrl |= (cap & PCI_ACS_RR);
  1899. /* P2P Completion Redirect */
  1900. ctrl |= (cap & PCI_ACS_CR);
  1901. /* Upstream Forwarding */
  1902. ctrl |= (cap & PCI_ACS_UF);
  1903. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  1904. }
  1905. static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
  1906. {
  1907. int pos;
  1908. u16 cap, ctrl;
  1909. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
  1910. if (!pos)
  1911. return false;
  1912. /*
  1913. * Except for egress control, capabilities are either required
  1914. * or only required if controllable. Features missing from the
  1915. * capability field can therefore be assumed as hard-wired enabled.
  1916. */
  1917. pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
  1918. acs_flags &= (cap | PCI_ACS_EC);
  1919. pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
  1920. return (ctrl & acs_flags) == acs_flags;
  1921. }
  1922. /**
  1923. * pci_acs_enabled - test ACS against required flags for a given device
  1924. * @pdev: device to test
  1925. * @acs_flags: required PCI ACS flags
  1926. *
  1927. * Return true if the device supports the provided flags. Automatically
  1928. * filters out flags that are not implemented on multifunction devices.
  1929. *
  1930. * Note that this interface checks the effective ACS capabilities of the
  1931. * device rather than the actual capabilities. For instance, most single
  1932. * function endpoints are not required to support ACS because they have no
  1933. * opportunity for peer-to-peer access. We therefore return 'true'
  1934. * regardless of whether the device exposes an ACS capability. This makes
  1935. * it much easier for callers of this function to ignore the actual type
  1936. * or topology of the device when testing ACS support.
  1937. */
  1938. bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
  1939. {
  1940. int ret;
  1941. ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
  1942. if (ret >= 0)
  1943. return ret > 0;
  1944. /*
  1945. * Conventional PCI and PCI-X devices never support ACS, either
  1946. * effectively or actually. The shared bus topology implies that
  1947. * any device on the bus can receive or snoop DMA.
  1948. */
  1949. if (!pci_is_pcie(pdev))
  1950. return false;
  1951. switch (pci_pcie_type(pdev)) {
  1952. /*
  1953. * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
  1954. * but since their primary interface is PCI/X, we conservatively
  1955. * handle them as we would a non-PCIe device.
  1956. */
  1957. case PCI_EXP_TYPE_PCIE_BRIDGE:
  1958. /*
  1959. * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
  1960. * applicable... must never implement an ACS Extended Capability...".
  1961. * This seems arbitrary, but we take a conservative interpretation
  1962. * of this statement.
  1963. */
  1964. case PCI_EXP_TYPE_PCI_BRIDGE:
  1965. case PCI_EXP_TYPE_RC_EC:
  1966. return false;
  1967. /*
  1968. * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
  1969. * implement ACS in order to indicate their peer-to-peer capabilities,
  1970. * regardless of whether they are single- or multi-function devices.
  1971. */
  1972. case PCI_EXP_TYPE_DOWNSTREAM:
  1973. case PCI_EXP_TYPE_ROOT_PORT:
  1974. return pci_acs_flags_enabled(pdev, acs_flags);
  1975. /*
  1976. * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
  1977. * implemented by the remaining PCIe types to indicate peer-to-peer
  1978. * capabilities, but only when they are part of a multifunction
  1979. * device. The footnote for section 6.12 indicates the specific
  1980. * PCIe types included here.
  1981. */
  1982. case PCI_EXP_TYPE_ENDPOINT:
  1983. case PCI_EXP_TYPE_UPSTREAM:
  1984. case PCI_EXP_TYPE_LEG_END:
  1985. case PCI_EXP_TYPE_RC_END:
  1986. if (!pdev->multifunction)
  1987. break;
  1988. return pci_acs_flags_enabled(pdev, acs_flags);
  1989. }
  1990. /*
  1991. * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable
  1992. * to single function devices with the exception of downstream ports.
  1993. */
  1994. return true;
  1995. }
  1996. /**
  1997. * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
  1998. * @start: starting downstream device
  1999. * @end: ending upstream device or NULL to search to the root bus
  2000. * @acs_flags: required flags
  2001. *
  2002. * Walk up a device tree from start to end testing PCI ACS support. If
  2003. * any step along the way does not support the required flags, return false.
  2004. */
  2005. bool pci_acs_path_enabled(struct pci_dev *start,
  2006. struct pci_dev *end, u16 acs_flags)
  2007. {
  2008. struct pci_dev *pdev, *parent = start;
  2009. do {
  2010. pdev = parent;
  2011. if (!pci_acs_enabled(pdev, acs_flags))
  2012. return false;
  2013. if (pci_is_root_bus(pdev->bus))
  2014. return (end == NULL);
  2015. parent = pdev->bus->self;
  2016. } while (pdev != end);
  2017. return true;
  2018. }
  2019. /**
  2020. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  2021. * @dev: the PCI device
  2022. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
  2023. *
  2024. * Perform INTx swizzling for a device behind one level of bridge. This is
  2025. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  2026. * behind bridges on add-in cards. For devices with ARI enabled, the slot
  2027. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  2028. * the PCI Express Base Specification, Revision 2.1)
  2029. */
  2030. u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
  2031. {
  2032. int slot;
  2033. if (pci_ari_enabled(dev->bus))
  2034. slot = 0;
  2035. else
  2036. slot = PCI_SLOT(dev->devfn);
  2037. return (((pin - 1) + slot) % 4) + 1;
  2038. }
  2039. int
  2040. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  2041. {
  2042. u8 pin;
  2043. pin = dev->pin;
  2044. if (!pin)
  2045. return -1;
  2046. while (!pci_is_root_bus(dev->bus)) {
  2047. pin = pci_swizzle_interrupt_pin(dev, pin);
  2048. dev = dev->bus->self;
  2049. }
  2050. *bridge = dev;
  2051. return pin;
  2052. }
  2053. /**
  2054. * pci_common_swizzle - swizzle INTx all the way to root bridge
  2055. * @dev: the PCI device
  2056. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  2057. *
  2058. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  2059. * bridges all the way up to a PCI root bus.
  2060. */
  2061. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  2062. {
  2063. u8 pin = *pinp;
  2064. while (!pci_is_root_bus(dev->bus)) {
  2065. pin = pci_swizzle_interrupt_pin(dev, pin);
  2066. dev = dev->bus->self;
  2067. }
  2068. *pinp = pin;
  2069. return PCI_SLOT(dev->devfn);
  2070. }
  2071. /**
  2072. * pci_release_region - Release a PCI bar
  2073. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  2074. * @bar: BAR to release
  2075. *
  2076. * Releases the PCI I/O and memory resources previously reserved by a
  2077. * successful call to pci_request_region. Call this function only
  2078. * after all use of the PCI regions has ceased.
  2079. */
  2080. void pci_release_region(struct pci_dev *pdev, int bar)
  2081. {
  2082. struct pci_devres *dr;
  2083. if (pci_resource_len(pdev, bar) == 0)
  2084. return;
  2085. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  2086. release_region(pci_resource_start(pdev, bar),
  2087. pci_resource_len(pdev, bar));
  2088. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  2089. release_mem_region(pci_resource_start(pdev, bar),
  2090. pci_resource_len(pdev, bar));
  2091. dr = find_pci_dr(pdev);
  2092. if (dr)
  2093. dr->region_mask &= ~(1 << bar);
  2094. }
  2095. /**
  2096. * __pci_request_region - Reserved PCI I/O and memory resource
  2097. * @pdev: PCI device whose resources are to be reserved
  2098. * @bar: BAR to be reserved
  2099. * @res_name: Name to be associated with resource.
  2100. * @exclusive: whether the region access is exclusive or not
  2101. *
  2102. * Mark the PCI region associated with PCI device @pdev BR @bar as
  2103. * being reserved by owner @res_name. Do not access any
  2104. * address inside the PCI regions unless this call returns
  2105. * successfully.
  2106. *
  2107. * If @exclusive is set, then the region is marked so that userspace
  2108. * is explicitly not allowed to map the resource via /dev/mem or
  2109. * sysfs MMIO access.
  2110. *
  2111. * Returns 0 on success, or %EBUSY on error. A warning
  2112. * message is also printed on failure.
  2113. */
  2114. static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
  2115. int exclusive)
  2116. {
  2117. struct pci_devres *dr;
  2118. if (pci_resource_len(pdev, bar) == 0)
  2119. return 0;
  2120. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  2121. if (!request_region(pci_resource_start(pdev, bar),
  2122. pci_resource_len(pdev, bar), res_name))
  2123. goto err_out;
  2124. }
  2125. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  2126. if (!__request_mem_region(pci_resource_start(pdev, bar),
  2127. pci_resource_len(pdev, bar), res_name,
  2128. exclusive))
  2129. goto err_out;
  2130. }
  2131. dr = find_pci_dr(pdev);
  2132. if (dr)
  2133. dr->region_mask |= 1 << bar;
  2134. return 0;
  2135. err_out:
  2136. dev_warn(&pdev->dev, "BAR %d: can't reserve %pR\n", bar,
  2137. &pdev->resource[bar]);
  2138. return -EBUSY;
  2139. }
  2140. /**
  2141. * pci_request_region - Reserve PCI I/O and memory resource
  2142. * @pdev: PCI device whose resources are to be reserved
  2143. * @bar: BAR to be reserved
  2144. * @res_name: Name to be associated with resource
  2145. *
  2146. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  2147. * being reserved by owner @res_name. Do not access any
  2148. * address inside the PCI regions unless this call returns
  2149. * successfully.
  2150. *
  2151. * Returns 0 on success, or %EBUSY on error. A warning
  2152. * message is also printed on failure.
  2153. */
  2154. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  2155. {
  2156. return __pci_request_region(pdev, bar, res_name, 0);
  2157. }
  2158. /**
  2159. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  2160. * @pdev: PCI device whose resources are to be reserved
  2161. * @bar: BAR to be reserved
  2162. * @res_name: Name to be associated with resource.
  2163. *
  2164. * Mark the PCI region associated with PCI device @pdev BR @bar as
  2165. * being reserved by owner @res_name. Do not access any
  2166. * address inside the PCI regions unless this call returns
  2167. * successfully.
  2168. *
  2169. * Returns 0 on success, or %EBUSY on error. A warning
  2170. * message is also printed on failure.
  2171. *
  2172. * The key difference that _exclusive makes it that userspace is
  2173. * explicitly not allowed to map the resource via /dev/mem or
  2174. * sysfs.
  2175. */
  2176. int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
  2177. {
  2178. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  2179. }
  2180. /**
  2181. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  2182. * @pdev: PCI device whose resources were previously reserved
  2183. * @bars: Bitmask of BARs to be released
  2184. *
  2185. * Release selected PCI I/O and memory resources previously reserved.
  2186. * Call this function only after all use of the PCI regions has ceased.
  2187. */
  2188. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  2189. {
  2190. int i;
  2191. for (i = 0; i < 6; i++)
  2192. if (bars & (1 << i))
  2193. pci_release_region(pdev, i);
  2194. }
  2195. static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  2196. const char *res_name, int excl)
  2197. {
  2198. int i;
  2199. for (i = 0; i < 6; i++)
  2200. if (bars & (1 << i))
  2201. if (__pci_request_region(pdev, i, res_name, excl))
  2202. goto err_out;
  2203. return 0;
  2204. err_out:
  2205. while(--i >= 0)
  2206. if (bars & (1 << i))
  2207. pci_release_region(pdev, i);
  2208. return -EBUSY;
  2209. }
  2210. /**
  2211. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  2212. * @pdev: PCI device whose resources are to be reserved
  2213. * @bars: Bitmask of BARs to be requested
  2214. * @res_name: Name to be associated with resource
  2215. */
  2216. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  2217. const char *res_name)
  2218. {
  2219. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  2220. }
  2221. int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
  2222. int bars, const char *res_name)
  2223. {
  2224. return __pci_request_selected_regions(pdev, bars, res_name,
  2225. IORESOURCE_EXCLUSIVE);
  2226. }
  2227. /**
  2228. * pci_release_regions - Release reserved PCI I/O and memory resources
  2229. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  2230. *
  2231. * Releases all PCI I/O and memory resources previously reserved by a
  2232. * successful call to pci_request_regions. Call this function only
  2233. * after all use of the PCI regions has ceased.
  2234. */
  2235. void pci_release_regions(struct pci_dev *pdev)
  2236. {
  2237. pci_release_selected_regions(pdev, (1 << 6) - 1);
  2238. }
  2239. /**
  2240. * pci_request_regions - Reserved PCI I/O and memory resources
  2241. * @pdev: PCI device whose resources are to be reserved
  2242. * @res_name: Name to be associated with resource.
  2243. *
  2244. * Mark all PCI regions associated with PCI device @pdev as
  2245. * being reserved by owner @res_name. Do not access any
  2246. * address inside the PCI regions unless this call returns
  2247. * successfully.
  2248. *
  2249. * Returns 0 on success, or %EBUSY on error. A warning
  2250. * message is also printed on failure.
  2251. */
  2252. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  2253. {
  2254. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  2255. }
  2256. /**
  2257. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  2258. * @pdev: PCI device whose resources are to be reserved
  2259. * @res_name: Name to be associated with resource.
  2260. *
  2261. * Mark all PCI regions associated with PCI device @pdev as
  2262. * being reserved by owner @res_name. Do not access any
  2263. * address inside the PCI regions unless this call returns
  2264. * successfully.
  2265. *
  2266. * pci_request_regions_exclusive() will mark the region so that
  2267. * /dev/mem and the sysfs MMIO access will not be allowed.
  2268. *
  2269. * Returns 0 on success, or %EBUSY on error. A warning
  2270. * message is also printed on failure.
  2271. */
  2272. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  2273. {
  2274. return pci_request_selected_regions_exclusive(pdev,
  2275. ((1 << 6) - 1), res_name);
  2276. }
  2277. static void __pci_set_master(struct pci_dev *dev, bool enable)
  2278. {
  2279. u16 old_cmd, cmd;
  2280. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  2281. if (enable)
  2282. cmd = old_cmd | PCI_COMMAND_MASTER;
  2283. else
  2284. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  2285. if (cmd != old_cmd) {
  2286. dev_dbg(&dev->dev, "%s bus mastering\n",
  2287. enable ? "enabling" : "disabling");
  2288. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2289. }
  2290. dev->is_busmaster = enable;
  2291. }
  2292. /**
  2293. * pcibios_setup - process "pci=" kernel boot arguments
  2294. * @str: string used to pass in "pci=" kernel boot arguments
  2295. *
  2296. * Process kernel boot arguments. This is the default implementation.
  2297. * Architecture specific implementations can override this as necessary.
  2298. */
  2299. char * __weak __init pcibios_setup(char *str)
  2300. {
  2301. return str;
  2302. }
  2303. /**
  2304. * pcibios_set_master - enable PCI bus-mastering for device dev
  2305. * @dev: the PCI device to enable
  2306. *
  2307. * Enables PCI bus-mastering for the device. This is the default
  2308. * implementation. Architecture specific implementations can override
  2309. * this if necessary.
  2310. */
  2311. void __weak pcibios_set_master(struct pci_dev *dev)
  2312. {
  2313. u8 lat;
  2314. /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
  2315. if (pci_is_pcie(dev))
  2316. return;
  2317. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  2318. if (lat < 16)
  2319. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  2320. else if (lat > pcibios_max_latency)
  2321. lat = pcibios_max_latency;
  2322. else
  2323. return;
  2324. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  2325. }
  2326. /**
  2327. * pci_set_master - enables bus-mastering for device dev
  2328. * @dev: the PCI device to enable
  2329. *
  2330. * Enables bus-mastering on the device and calls pcibios_set_master()
  2331. * to do the needed arch specific settings.
  2332. */
  2333. void pci_set_master(struct pci_dev *dev)
  2334. {
  2335. __pci_set_master(dev, true);
  2336. pcibios_set_master(dev);
  2337. }
  2338. /**
  2339. * pci_clear_master - disables bus-mastering for device dev
  2340. * @dev: the PCI device to disable
  2341. */
  2342. void pci_clear_master(struct pci_dev *dev)
  2343. {
  2344. __pci_set_master(dev, false);
  2345. }
  2346. /**
  2347. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  2348. * @dev: the PCI device for which MWI is to be enabled
  2349. *
  2350. * Helper function for pci_set_mwi.
  2351. * Originally copied from drivers/net/acenic.c.
  2352. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  2353. *
  2354. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2355. */
  2356. int pci_set_cacheline_size(struct pci_dev *dev)
  2357. {
  2358. u8 cacheline_size;
  2359. if (!pci_cache_line_size)
  2360. return -EINVAL;
  2361. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  2362. equal to or multiple of the right value. */
  2363. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  2364. if (cacheline_size >= pci_cache_line_size &&
  2365. (cacheline_size % pci_cache_line_size) == 0)
  2366. return 0;
  2367. /* Write the correct value. */
  2368. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  2369. /* Read it back. */
  2370. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  2371. if (cacheline_size == pci_cache_line_size)
  2372. return 0;
  2373. dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
  2374. "supported\n", pci_cache_line_size << 2);
  2375. return -EINVAL;
  2376. }
  2377. EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
  2378. #ifdef PCI_DISABLE_MWI
  2379. int pci_set_mwi(struct pci_dev *dev)
  2380. {
  2381. return 0;
  2382. }
  2383. int pci_try_set_mwi(struct pci_dev *dev)
  2384. {
  2385. return 0;
  2386. }
  2387. void pci_clear_mwi(struct pci_dev *dev)
  2388. {
  2389. }
  2390. #else
  2391. /**
  2392. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  2393. * @dev: the PCI device for which MWI is enabled
  2394. *
  2395. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  2396. *
  2397. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2398. */
  2399. int
  2400. pci_set_mwi(struct pci_dev *dev)
  2401. {
  2402. int rc;
  2403. u16 cmd;
  2404. rc = pci_set_cacheline_size(dev);
  2405. if (rc)
  2406. return rc;
  2407. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2408. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  2409. dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
  2410. cmd |= PCI_COMMAND_INVALIDATE;
  2411. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2412. }
  2413. return 0;
  2414. }
  2415. /**
  2416. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  2417. * @dev: the PCI device for which MWI is enabled
  2418. *
  2419. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  2420. * Callers are not required to check the return value.
  2421. *
  2422. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2423. */
  2424. int pci_try_set_mwi(struct pci_dev *dev)
  2425. {
  2426. int rc = pci_set_mwi(dev);
  2427. return rc;
  2428. }
  2429. /**
  2430. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  2431. * @dev: the PCI device to disable
  2432. *
  2433. * Disables PCI Memory-Write-Invalidate transaction on the device
  2434. */
  2435. void
  2436. pci_clear_mwi(struct pci_dev *dev)
  2437. {
  2438. u16 cmd;
  2439. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2440. if (cmd & PCI_COMMAND_INVALIDATE) {
  2441. cmd &= ~PCI_COMMAND_INVALIDATE;
  2442. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2443. }
  2444. }
  2445. #endif /* ! PCI_DISABLE_MWI */
  2446. /**
  2447. * pci_intx - enables/disables PCI INTx for device dev
  2448. * @pdev: the PCI device to operate on
  2449. * @enable: boolean: whether to enable or disable PCI INTx
  2450. *
  2451. * Enables/disables PCI INTx for device dev
  2452. */
  2453. void
  2454. pci_intx(struct pci_dev *pdev, int enable)
  2455. {
  2456. u16 pci_command, new;
  2457. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  2458. if (enable) {
  2459. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  2460. } else {
  2461. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  2462. }
  2463. if (new != pci_command) {
  2464. struct pci_devres *dr;
  2465. pci_write_config_word(pdev, PCI_COMMAND, new);
  2466. dr = find_pci_dr(pdev);
  2467. if (dr && !dr->restore_intx) {
  2468. dr->restore_intx = 1;
  2469. dr->orig_intx = !enable;
  2470. }
  2471. }
  2472. }
  2473. /**
  2474. * pci_intx_mask_supported - probe for INTx masking support
  2475. * @dev: the PCI device to operate on
  2476. *
  2477. * Check if the device dev support INTx masking via the config space
  2478. * command word.
  2479. */
  2480. bool pci_intx_mask_supported(struct pci_dev *dev)
  2481. {
  2482. bool mask_supported = false;
  2483. u16 orig, new;
  2484. if (dev->broken_intx_masking)
  2485. return false;
  2486. pci_cfg_access_lock(dev);
  2487. pci_read_config_word(dev, PCI_COMMAND, &orig);
  2488. pci_write_config_word(dev, PCI_COMMAND,
  2489. orig ^ PCI_COMMAND_INTX_DISABLE);
  2490. pci_read_config_word(dev, PCI_COMMAND, &new);
  2491. /*
  2492. * There's no way to protect against hardware bugs or detect them
  2493. * reliably, but as long as we know what the value should be, let's
  2494. * go ahead and check it.
  2495. */
  2496. if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
  2497. dev_err(&dev->dev, "Command register changed from "
  2498. "0x%x to 0x%x: driver or hardware bug?\n", orig, new);
  2499. } else if ((new ^ orig) & PCI_COMMAND_INTX_DISABLE) {
  2500. mask_supported = true;
  2501. pci_write_config_word(dev, PCI_COMMAND, orig);
  2502. }
  2503. pci_cfg_access_unlock(dev);
  2504. return mask_supported;
  2505. }
  2506. EXPORT_SYMBOL_GPL(pci_intx_mask_supported);
  2507. static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
  2508. {
  2509. struct pci_bus *bus = dev->bus;
  2510. bool mask_updated = true;
  2511. u32 cmd_status_dword;
  2512. u16 origcmd, newcmd;
  2513. unsigned long flags;
  2514. bool irq_pending;
  2515. /*
  2516. * We do a single dword read to retrieve both command and status.
  2517. * Document assumptions that make this possible.
  2518. */
  2519. BUILD_BUG_ON(PCI_COMMAND % 4);
  2520. BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
  2521. raw_spin_lock_irqsave(&pci_lock, flags);
  2522. bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
  2523. irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
  2524. /*
  2525. * Check interrupt status register to see whether our device
  2526. * triggered the interrupt (when masking) or the next IRQ is
  2527. * already pending (when unmasking).
  2528. */
  2529. if (mask != irq_pending) {
  2530. mask_updated = false;
  2531. goto done;
  2532. }
  2533. origcmd = cmd_status_dword;
  2534. newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
  2535. if (mask)
  2536. newcmd |= PCI_COMMAND_INTX_DISABLE;
  2537. if (newcmd != origcmd)
  2538. bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
  2539. done:
  2540. raw_spin_unlock_irqrestore(&pci_lock, flags);
  2541. return mask_updated;
  2542. }
  2543. /**
  2544. * pci_check_and_mask_intx - mask INTx on pending interrupt
  2545. * @dev: the PCI device to operate on
  2546. *
  2547. * Check if the device dev has its INTx line asserted, mask it and
  2548. * return true in that case. False is returned if not interrupt was
  2549. * pending.
  2550. */
  2551. bool pci_check_and_mask_intx(struct pci_dev *dev)
  2552. {
  2553. return pci_check_and_set_intx_mask(dev, true);
  2554. }
  2555. EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
  2556. /**
  2557. * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
  2558. * @dev: the PCI device to operate on
  2559. *
  2560. * Check if the device dev has its INTx line asserted, unmask it if not
  2561. * and return true. False is returned and the mask remains active if
  2562. * there was still an interrupt pending.
  2563. */
  2564. bool pci_check_and_unmask_intx(struct pci_dev *dev)
  2565. {
  2566. return pci_check_and_set_intx_mask(dev, false);
  2567. }
  2568. EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
  2569. /**
  2570. * pci_msi_off - disables any MSI or MSI-X capabilities
  2571. * @dev: the PCI device to operate on
  2572. *
  2573. * If you want to use MSI, see pci_enable_msi() and friends.
  2574. * This is a lower-level primitive that allows us to disable
  2575. * MSI operation at the device level.
  2576. */
  2577. void pci_msi_off(struct pci_dev *dev)
  2578. {
  2579. int pos;
  2580. u16 control;
  2581. /*
  2582. * This looks like it could go in msi.c, but we need it even when
  2583. * CONFIG_PCI_MSI=n. For the same reason, we can't use
  2584. * dev->msi_cap or dev->msix_cap here.
  2585. */
  2586. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  2587. if (pos) {
  2588. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  2589. control &= ~PCI_MSI_FLAGS_ENABLE;
  2590. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  2591. }
  2592. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  2593. if (pos) {
  2594. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  2595. control &= ~PCI_MSIX_FLAGS_ENABLE;
  2596. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  2597. }
  2598. }
  2599. EXPORT_SYMBOL_GPL(pci_msi_off);
  2600. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  2601. {
  2602. return dma_set_max_seg_size(&dev->dev, size);
  2603. }
  2604. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  2605. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  2606. {
  2607. return dma_set_seg_boundary(&dev->dev, mask);
  2608. }
  2609. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  2610. /**
  2611. * pci_wait_for_pending_transaction - waits for pending transaction
  2612. * @dev: the PCI device to operate on
  2613. *
  2614. * Return 0 if transaction is pending 1 otherwise.
  2615. */
  2616. int pci_wait_for_pending_transaction(struct pci_dev *dev)
  2617. {
  2618. if (!pci_is_pcie(dev))
  2619. return 1;
  2620. return pci_wait_for_pending(dev, PCI_EXP_DEVSTA, PCI_EXP_DEVSTA_TRPND);
  2621. }
  2622. EXPORT_SYMBOL(pci_wait_for_pending_transaction);
  2623. static int pcie_flr(struct pci_dev *dev, int probe)
  2624. {
  2625. u32 cap;
  2626. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
  2627. if (!(cap & PCI_EXP_DEVCAP_FLR))
  2628. return -ENOTTY;
  2629. if (probe)
  2630. return 0;
  2631. if (!pci_wait_for_pending_transaction(dev))
  2632. dev_err(&dev->dev, "transaction is not cleared; proceeding with reset anyway\n");
  2633. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
  2634. msleep(100);
  2635. return 0;
  2636. }
  2637. static int pci_af_flr(struct pci_dev *dev, int probe)
  2638. {
  2639. int pos;
  2640. u8 cap;
  2641. pos = pci_find_capability(dev, PCI_CAP_ID_AF);
  2642. if (!pos)
  2643. return -ENOTTY;
  2644. pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
  2645. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  2646. return -ENOTTY;
  2647. if (probe)
  2648. return 0;
  2649. /* Wait for Transaction Pending bit clean */
  2650. if (pci_wait_for_pending(dev, PCI_AF_STATUS, PCI_AF_STATUS_TP))
  2651. goto clear;
  2652. dev_err(&dev->dev, "transaction is not cleared; "
  2653. "proceeding with reset anyway\n");
  2654. clear:
  2655. pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  2656. msleep(100);
  2657. return 0;
  2658. }
  2659. /**
  2660. * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
  2661. * @dev: Device to reset.
  2662. * @probe: If set, only check if the device can be reset this way.
  2663. *
  2664. * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
  2665. * unset, it will be reinitialized internally when going from PCI_D3hot to
  2666. * PCI_D0. If that's the case and the device is not in a low-power state
  2667. * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
  2668. *
  2669. * NOTE: This causes the caller to sleep for twice the device power transition
  2670. * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
  2671. * by default (i.e. unless the @dev's d3_delay field has a different value).
  2672. * Moreover, only devices in D0 can be reset by this function.
  2673. */
  2674. static int pci_pm_reset(struct pci_dev *dev, int probe)
  2675. {
  2676. u16 csr;
  2677. if (!dev->pm_cap)
  2678. return -ENOTTY;
  2679. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
  2680. if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
  2681. return -ENOTTY;
  2682. if (probe)
  2683. return 0;
  2684. if (dev->current_state != PCI_D0)
  2685. return -EINVAL;
  2686. csr &= ~PCI_PM_CTRL_STATE_MASK;
  2687. csr |= PCI_D3hot;
  2688. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  2689. pci_dev_d3_sleep(dev);
  2690. csr &= ~PCI_PM_CTRL_STATE_MASK;
  2691. csr |= PCI_D0;
  2692. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  2693. pci_dev_d3_sleep(dev);
  2694. return 0;
  2695. }
  2696. /**
  2697. * pci_reset_bridge_secondary_bus - Reset the secondary bus on a PCI bridge.
  2698. * @dev: Bridge device
  2699. *
  2700. * Use the bridge control register to assert reset on the secondary bus.
  2701. * Devices on the secondary bus are left in power-on state.
  2702. */
  2703. void pci_reset_bridge_secondary_bus(struct pci_dev *dev)
  2704. {
  2705. u16 ctrl;
  2706. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
  2707. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  2708. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  2709. /*
  2710. * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double
  2711. * this to 2ms to ensure that we meet the minimum requirement.
  2712. */
  2713. msleep(2);
  2714. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  2715. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  2716. /*
  2717. * Trhfa for conventional PCI is 2^25 clock cycles.
  2718. * Assuming a minimum 33MHz clock this results in a 1s
  2719. * delay before we can consider subordinate devices to
  2720. * be re-initialized. PCIe has some ways to shorten this,
  2721. * but we don't make use of them yet.
  2722. */
  2723. ssleep(1);
  2724. }
  2725. EXPORT_SYMBOL_GPL(pci_reset_bridge_secondary_bus);
  2726. static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
  2727. {
  2728. struct pci_dev *pdev;
  2729. if (pci_is_root_bus(dev->bus) || dev->subordinate || !dev->bus->self)
  2730. return -ENOTTY;
  2731. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  2732. if (pdev != dev)
  2733. return -ENOTTY;
  2734. if (probe)
  2735. return 0;
  2736. pci_reset_bridge_secondary_bus(dev->bus->self);
  2737. return 0;
  2738. }
  2739. static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, int probe)
  2740. {
  2741. int rc = -ENOTTY;
  2742. if (!hotplug || !try_module_get(hotplug->ops->owner))
  2743. return rc;
  2744. if (hotplug->ops->reset_slot)
  2745. rc = hotplug->ops->reset_slot(hotplug, probe);
  2746. module_put(hotplug->ops->owner);
  2747. return rc;
  2748. }
  2749. static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe)
  2750. {
  2751. struct pci_dev *pdev;
  2752. if (dev->subordinate || !dev->slot)
  2753. return -ENOTTY;
  2754. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  2755. if (pdev != dev && pdev->slot == dev->slot)
  2756. return -ENOTTY;
  2757. return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
  2758. }
  2759. static int __pci_dev_reset(struct pci_dev *dev, int probe)
  2760. {
  2761. int rc;
  2762. might_sleep();
  2763. rc = pci_dev_specific_reset(dev, probe);
  2764. if (rc != -ENOTTY)
  2765. goto done;
  2766. rc = pcie_flr(dev, probe);
  2767. if (rc != -ENOTTY)
  2768. goto done;
  2769. rc = pci_af_flr(dev, probe);
  2770. if (rc != -ENOTTY)
  2771. goto done;
  2772. rc = pci_pm_reset(dev, probe);
  2773. if (rc != -ENOTTY)
  2774. goto done;
  2775. rc = pci_dev_reset_slot_function(dev, probe);
  2776. if (rc != -ENOTTY)
  2777. goto done;
  2778. rc = pci_parent_bus_reset(dev, probe);
  2779. done:
  2780. return rc;
  2781. }
  2782. static void pci_dev_lock(struct pci_dev *dev)
  2783. {
  2784. pci_cfg_access_lock(dev);
  2785. /* block PM suspend, driver probe, etc. */
  2786. device_lock(&dev->dev);
  2787. }
  2788. /* Return 1 on successful lock, 0 on contention */
  2789. static int pci_dev_trylock(struct pci_dev *dev)
  2790. {
  2791. if (pci_cfg_access_trylock(dev)) {
  2792. if (device_trylock(&dev->dev))
  2793. return 1;
  2794. pci_cfg_access_unlock(dev);
  2795. }
  2796. return 0;
  2797. }
  2798. static void pci_dev_unlock(struct pci_dev *dev)
  2799. {
  2800. device_unlock(&dev->dev);
  2801. pci_cfg_access_unlock(dev);
  2802. }
  2803. static void pci_dev_save_and_disable(struct pci_dev *dev)
  2804. {
  2805. /*
  2806. * Wake-up device prior to save. PM registers default to D0 after
  2807. * reset and a simple register restore doesn't reliably return
  2808. * to a non-D0 state anyway.
  2809. */
  2810. pci_set_power_state(dev, PCI_D0);
  2811. pci_save_state(dev);
  2812. /*
  2813. * Disable the device by clearing the Command register, except for
  2814. * INTx-disable which is set. This not only disables MMIO and I/O port
  2815. * BARs, but also prevents the device from being Bus Master, preventing
  2816. * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
  2817. * compliant devices, INTx-disable prevents legacy interrupts.
  2818. */
  2819. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  2820. }
  2821. static void pci_dev_restore(struct pci_dev *dev)
  2822. {
  2823. pci_restore_state(dev);
  2824. }
  2825. static int pci_dev_reset(struct pci_dev *dev, int probe)
  2826. {
  2827. int rc;
  2828. if (!probe)
  2829. pci_dev_lock(dev);
  2830. rc = __pci_dev_reset(dev, probe);
  2831. if (!probe)
  2832. pci_dev_unlock(dev);
  2833. return rc;
  2834. }
  2835. /**
  2836. * __pci_reset_function - reset a PCI device function
  2837. * @dev: PCI device to reset
  2838. *
  2839. * Some devices allow an individual function to be reset without affecting
  2840. * other functions in the same device. The PCI device must be responsive
  2841. * to PCI config space in order to use this function.
  2842. *
  2843. * The device function is presumed to be unused when this function is called.
  2844. * Resetting the device will make the contents of PCI configuration space
  2845. * random, so any caller of this must be prepared to reinitialise the
  2846. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  2847. * etc.
  2848. *
  2849. * Returns 0 if the device function was successfully reset or negative if the
  2850. * device doesn't support resetting a single function.
  2851. */
  2852. int __pci_reset_function(struct pci_dev *dev)
  2853. {
  2854. return pci_dev_reset(dev, 0);
  2855. }
  2856. EXPORT_SYMBOL_GPL(__pci_reset_function);
  2857. /**
  2858. * __pci_reset_function_locked - reset a PCI device function while holding
  2859. * the @dev mutex lock.
  2860. * @dev: PCI device to reset
  2861. *
  2862. * Some devices allow an individual function to be reset without affecting
  2863. * other functions in the same device. The PCI device must be responsive
  2864. * to PCI config space in order to use this function.
  2865. *
  2866. * The device function is presumed to be unused and the caller is holding
  2867. * the device mutex lock when this function is called.
  2868. * Resetting the device will make the contents of PCI configuration space
  2869. * random, so any caller of this must be prepared to reinitialise the
  2870. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  2871. * etc.
  2872. *
  2873. * Returns 0 if the device function was successfully reset or negative if the
  2874. * device doesn't support resetting a single function.
  2875. */
  2876. int __pci_reset_function_locked(struct pci_dev *dev)
  2877. {
  2878. return __pci_dev_reset(dev, 0);
  2879. }
  2880. EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
  2881. /**
  2882. * pci_probe_reset_function - check whether the device can be safely reset
  2883. * @dev: PCI device to reset
  2884. *
  2885. * Some devices allow an individual function to be reset without affecting
  2886. * other functions in the same device. The PCI device must be responsive
  2887. * to PCI config space in order to use this function.
  2888. *
  2889. * Returns 0 if the device function can be reset or negative if the
  2890. * device doesn't support resetting a single function.
  2891. */
  2892. int pci_probe_reset_function(struct pci_dev *dev)
  2893. {
  2894. return pci_dev_reset(dev, 1);
  2895. }
  2896. /**
  2897. * pci_reset_function - quiesce and reset a PCI device function
  2898. * @dev: PCI device to reset
  2899. *
  2900. * Some devices allow an individual function to be reset without affecting
  2901. * other functions in the same device. The PCI device must be responsive
  2902. * to PCI config space in order to use this function.
  2903. *
  2904. * This function does not just reset the PCI portion of a device, but
  2905. * clears all the state associated with the device. This function differs
  2906. * from __pci_reset_function in that it saves and restores device state
  2907. * over the reset.
  2908. *
  2909. * Returns 0 if the device function was successfully reset or negative if the
  2910. * device doesn't support resetting a single function.
  2911. */
  2912. int pci_reset_function(struct pci_dev *dev)
  2913. {
  2914. int rc;
  2915. rc = pci_dev_reset(dev, 1);
  2916. if (rc)
  2917. return rc;
  2918. pci_dev_save_and_disable(dev);
  2919. rc = pci_dev_reset(dev, 0);
  2920. pci_dev_restore(dev);
  2921. return rc;
  2922. }
  2923. EXPORT_SYMBOL_GPL(pci_reset_function);
  2924. /**
  2925. * pci_try_reset_function - quiesce and reset a PCI device function
  2926. * @dev: PCI device to reset
  2927. *
  2928. * Same as above, except return -EAGAIN if unable to lock device.
  2929. */
  2930. int pci_try_reset_function(struct pci_dev *dev)
  2931. {
  2932. int rc;
  2933. rc = pci_dev_reset(dev, 1);
  2934. if (rc)
  2935. return rc;
  2936. pci_dev_save_and_disable(dev);
  2937. if (pci_dev_trylock(dev)) {
  2938. rc = __pci_dev_reset(dev, 0);
  2939. pci_dev_unlock(dev);
  2940. } else
  2941. rc = -EAGAIN;
  2942. pci_dev_restore(dev);
  2943. return rc;
  2944. }
  2945. EXPORT_SYMBOL_GPL(pci_try_reset_function);
  2946. /* Lock devices from the top of the tree down */
  2947. static void pci_bus_lock(struct pci_bus *bus)
  2948. {
  2949. struct pci_dev *dev;
  2950. list_for_each_entry(dev, &bus->devices, bus_list) {
  2951. pci_dev_lock(dev);
  2952. if (dev->subordinate)
  2953. pci_bus_lock(dev->subordinate);
  2954. }
  2955. }
  2956. /* Unlock devices from the bottom of the tree up */
  2957. static void pci_bus_unlock(struct pci_bus *bus)
  2958. {
  2959. struct pci_dev *dev;
  2960. list_for_each_entry(dev, &bus->devices, bus_list) {
  2961. if (dev->subordinate)
  2962. pci_bus_unlock(dev->subordinate);
  2963. pci_dev_unlock(dev);
  2964. }
  2965. }
  2966. /* Return 1 on successful lock, 0 on contention */
  2967. static int pci_bus_trylock(struct pci_bus *bus)
  2968. {
  2969. struct pci_dev *dev;
  2970. list_for_each_entry(dev, &bus->devices, bus_list) {
  2971. if (!pci_dev_trylock(dev))
  2972. goto unlock;
  2973. if (dev->subordinate) {
  2974. if (!pci_bus_trylock(dev->subordinate)) {
  2975. pci_dev_unlock(dev);
  2976. goto unlock;
  2977. }
  2978. }
  2979. }
  2980. return 1;
  2981. unlock:
  2982. list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
  2983. if (dev->subordinate)
  2984. pci_bus_unlock(dev->subordinate);
  2985. pci_dev_unlock(dev);
  2986. }
  2987. return 0;
  2988. }
  2989. /* Lock devices from the top of the tree down */
  2990. static void pci_slot_lock(struct pci_slot *slot)
  2991. {
  2992. struct pci_dev *dev;
  2993. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  2994. if (!dev->slot || dev->slot != slot)
  2995. continue;
  2996. pci_dev_lock(dev);
  2997. if (dev->subordinate)
  2998. pci_bus_lock(dev->subordinate);
  2999. }
  3000. }
  3001. /* Unlock devices from the bottom of the tree up */
  3002. static void pci_slot_unlock(struct pci_slot *slot)
  3003. {
  3004. struct pci_dev *dev;
  3005. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3006. if (!dev->slot || dev->slot != slot)
  3007. continue;
  3008. if (dev->subordinate)
  3009. pci_bus_unlock(dev->subordinate);
  3010. pci_dev_unlock(dev);
  3011. }
  3012. }
  3013. /* Return 1 on successful lock, 0 on contention */
  3014. static int pci_slot_trylock(struct pci_slot *slot)
  3015. {
  3016. struct pci_dev *dev;
  3017. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3018. if (!dev->slot || dev->slot != slot)
  3019. continue;
  3020. if (!pci_dev_trylock(dev))
  3021. goto unlock;
  3022. if (dev->subordinate) {
  3023. if (!pci_bus_trylock(dev->subordinate)) {
  3024. pci_dev_unlock(dev);
  3025. goto unlock;
  3026. }
  3027. }
  3028. }
  3029. return 1;
  3030. unlock:
  3031. list_for_each_entry_continue_reverse(dev,
  3032. &slot->bus->devices, bus_list) {
  3033. if (!dev->slot || dev->slot != slot)
  3034. continue;
  3035. if (dev->subordinate)
  3036. pci_bus_unlock(dev->subordinate);
  3037. pci_dev_unlock(dev);
  3038. }
  3039. return 0;
  3040. }
  3041. /* Save and disable devices from the top of the tree down */
  3042. static void pci_bus_save_and_disable(struct pci_bus *bus)
  3043. {
  3044. struct pci_dev *dev;
  3045. list_for_each_entry(dev, &bus->devices, bus_list) {
  3046. pci_dev_save_and_disable(dev);
  3047. if (dev->subordinate)
  3048. pci_bus_save_and_disable(dev->subordinate);
  3049. }
  3050. }
  3051. /*
  3052. * Restore devices from top of the tree down - parent bridges need to be
  3053. * restored before we can get to subordinate devices.
  3054. */
  3055. static void pci_bus_restore(struct pci_bus *bus)
  3056. {
  3057. struct pci_dev *dev;
  3058. list_for_each_entry(dev, &bus->devices, bus_list) {
  3059. pci_dev_restore(dev);
  3060. if (dev->subordinate)
  3061. pci_bus_restore(dev->subordinate);
  3062. }
  3063. }
  3064. /* Save and disable devices from the top of the tree down */
  3065. static void pci_slot_save_and_disable(struct pci_slot *slot)
  3066. {
  3067. struct pci_dev *dev;
  3068. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3069. if (!dev->slot || dev->slot != slot)
  3070. continue;
  3071. pci_dev_save_and_disable(dev);
  3072. if (dev->subordinate)
  3073. pci_bus_save_and_disable(dev->subordinate);
  3074. }
  3075. }
  3076. /*
  3077. * Restore devices from top of the tree down - parent bridges need to be
  3078. * restored before we can get to subordinate devices.
  3079. */
  3080. static void pci_slot_restore(struct pci_slot *slot)
  3081. {
  3082. struct pci_dev *dev;
  3083. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3084. if (!dev->slot || dev->slot != slot)
  3085. continue;
  3086. pci_dev_restore(dev);
  3087. if (dev->subordinate)
  3088. pci_bus_restore(dev->subordinate);
  3089. }
  3090. }
  3091. static int pci_slot_reset(struct pci_slot *slot, int probe)
  3092. {
  3093. int rc;
  3094. if (!slot)
  3095. return -ENOTTY;
  3096. if (!probe)
  3097. pci_slot_lock(slot);
  3098. might_sleep();
  3099. rc = pci_reset_hotplug_slot(slot->hotplug, probe);
  3100. if (!probe)
  3101. pci_slot_unlock(slot);
  3102. return rc;
  3103. }
  3104. /**
  3105. * pci_probe_reset_slot - probe whether a PCI slot can be reset
  3106. * @slot: PCI slot to probe
  3107. *
  3108. * Return 0 if slot can be reset, negative if a slot reset is not supported.
  3109. */
  3110. int pci_probe_reset_slot(struct pci_slot *slot)
  3111. {
  3112. return pci_slot_reset(slot, 1);
  3113. }
  3114. EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
  3115. /**
  3116. * pci_reset_slot - reset a PCI slot
  3117. * @slot: PCI slot to reset
  3118. *
  3119. * A PCI bus may host multiple slots, each slot may support a reset mechanism
  3120. * independent of other slots. For instance, some slots may support slot power
  3121. * control. In the case of a 1:1 bus to slot architecture, this function may
  3122. * wrap the bus reset to avoid spurious slot related events such as hotplug.
  3123. * Generally a slot reset should be attempted before a bus reset. All of the
  3124. * function of the slot and any subordinate buses behind the slot are reset
  3125. * through this function. PCI config space of all devices in the slot and
  3126. * behind the slot is saved before and restored after reset.
  3127. *
  3128. * Return 0 on success, non-zero on error.
  3129. */
  3130. int pci_reset_slot(struct pci_slot *slot)
  3131. {
  3132. int rc;
  3133. rc = pci_slot_reset(slot, 1);
  3134. if (rc)
  3135. return rc;
  3136. pci_slot_save_and_disable(slot);
  3137. rc = pci_slot_reset(slot, 0);
  3138. pci_slot_restore(slot);
  3139. return rc;
  3140. }
  3141. EXPORT_SYMBOL_GPL(pci_reset_slot);
  3142. /**
  3143. * pci_try_reset_slot - Try to reset a PCI slot
  3144. * @slot: PCI slot to reset
  3145. *
  3146. * Same as above except return -EAGAIN if the slot cannot be locked
  3147. */
  3148. int pci_try_reset_slot(struct pci_slot *slot)
  3149. {
  3150. int rc;
  3151. rc = pci_slot_reset(slot, 1);
  3152. if (rc)
  3153. return rc;
  3154. pci_slot_save_and_disable(slot);
  3155. if (pci_slot_trylock(slot)) {
  3156. might_sleep();
  3157. rc = pci_reset_hotplug_slot(slot->hotplug, 0);
  3158. pci_slot_unlock(slot);
  3159. } else
  3160. rc = -EAGAIN;
  3161. pci_slot_restore(slot);
  3162. return rc;
  3163. }
  3164. EXPORT_SYMBOL_GPL(pci_try_reset_slot);
  3165. static int pci_bus_reset(struct pci_bus *bus, int probe)
  3166. {
  3167. if (!bus->self)
  3168. return -ENOTTY;
  3169. if (probe)
  3170. return 0;
  3171. pci_bus_lock(bus);
  3172. might_sleep();
  3173. pci_reset_bridge_secondary_bus(bus->self);
  3174. pci_bus_unlock(bus);
  3175. return 0;
  3176. }
  3177. /**
  3178. * pci_probe_reset_bus - probe whether a PCI bus can be reset
  3179. * @bus: PCI bus to probe
  3180. *
  3181. * Return 0 if bus can be reset, negative if a bus reset is not supported.
  3182. */
  3183. int pci_probe_reset_bus(struct pci_bus *bus)
  3184. {
  3185. return pci_bus_reset(bus, 1);
  3186. }
  3187. EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
  3188. /**
  3189. * pci_reset_bus - reset a PCI bus
  3190. * @bus: top level PCI bus to reset
  3191. *
  3192. * Do a bus reset on the given bus and any subordinate buses, saving
  3193. * and restoring state of all devices.
  3194. *
  3195. * Return 0 on success, non-zero on error.
  3196. */
  3197. int pci_reset_bus(struct pci_bus *bus)
  3198. {
  3199. int rc;
  3200. rc = pci_bus_reset(bus, 1);
  3201. if (rc)
  3202. return rc;
  3203. pci_bus_save_and_disable(bus);
  3204. rc = pci_bus_reset(bus, 0);
  3205. pci_bus_restore(bus);
  3206. return rc;
  3207. }
  3208. EXPORT_SYMBOL_GPL(pci_reset_bus);
  3209. /**
  3210. * pci_try_reset_bus - Try to reset a PCI bus
  3211. * @bus: top level PCI bus to reset
  3212. *
  3213. * Same as above except return -EAGAIN if the bus cannot be locked
  3214. */
  3215. int pci_try_reset_bus(struct pci_bus *bus)
  3216. {
  3217. int rc;
  3218. rc = pci_bus_reset(bus, 1);
  3219. if (rc)
  3220. return rc;
  3221. pci_bus_save_and_disable(bus);
  3222. if (pci_bus_trylock(bus)) {
  3223. might_sleep();
  3224. pci_reset_bridge_secondary_bus(bus->self);
  3225. pci_bus_unlock(bus);
  3226. } else
  3227. rc = -EAGAIN;
  3228. pci_bus_restore(bus);
  3229. return rc;
  3230. }
  3231. EXPORT_SYMBOL_GPL(pci_try_reset_bus);
  3232. /**
  3233. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  3234. * @dev: PCI device to query
  3235. *
  3236. * Returns mmrbc: maximum designed memory read count in bytes
  3237. * or appropriate error value.
  3238. */
  3239. int pcix_get_max_mmrbc(struct pci_dev *dev)
  3240. {
  3241. int cap;
  3242. u32 stat;
  3243. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  3244. if (!cap)
  3245. return -EINVAL;
  3246. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  3247. return -EINVAL;
  3248. return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
  3249. }
  3250. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  3251. /**
  3252. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  3253. * @dev: PCI device to query
  3254. *
  3255. * Returns mmrbc: maximum memory read count in bytes
  3256. * or appropriate error value.
  3257. */
  3258. int pcix_get_mmrbc(struct pci_dev *dev)
  3259. {
  3260. int cap;
  3261. u16 cmd;
  3262. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  3263. if (!cap)
  3264. return -EINVAL;
  3265. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  3266. return -EINVAL;
  3267. return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  3268. }
  3269. EXPORT_SYMBOL(pcix_get_mmrbc);
  3270. /**
  3271. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  3272. * @dev: PCI device to query
  3273. * @mmrbc: maximum memory read count in bytes
  3274. * valid values are 512, 1024, 2048, 4096
  3275. *
  3276. * If possible sets maximum memory read byte count, some bridges have erratas
  3277. * that prevent this.
  3278. */
  3279. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  3280. {
  3281. int cap;
  3282. u32 stat, v, o;
  3283. u16 cmd;
  3284. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  3285. return -EINVAL;
  3286. v = ffs(mmrbc) - 10;
  3287. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  3288. if (!cap)
  3289. return -EINVAL;
  3290. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  3291. return -EINVAL;
  3292. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  3293. return -E2BIG;
  3294. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  3295. return -EINVAL;
  3296. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  3297. if (o != v) {
  3298. if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  3299. return -EIO;
  3300. cmd &= ~PCI_X_CMD_MAX_READ;
  3301. cmd |= v << 2;
  3302. if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
  3303. return -EIO;
  3304. }
  3305. return 0;
  3306. }
  3307. EXPORT_SYMBOL(pcix_set_mmrbc);
  3308. /**
  3309. * pcie_get_readrq - get PCI Express read request size
  3310. * @dev: PCI device to query
  3311. *
  3312. * Returns maximum memory read request in bytes
  3313. * or appropriate error value.
  3314. */
  3315. int pcie_get_readrq(struct pci_dev *dev)
  3316. {
  3317. u16 ctl;
  3318. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  3319. return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  3320. }
  3321. EXPORT_SYMBOL(pcie_get_readrq);
  3322. /**
  3323. * pcie_set_readrq - set PCI Express maximum memory read request
  3324. * @dev: PCI device to query
  3325. * @rq: maximum memory read count in bytes
  3326. * valid values are 128, 256, 512, 1024, 2048, 4096
  3327. *
  3328. * If possible sets maximum memory read request in bytes
  3329. */
  3330. int pcie_set_readrq(struct pci_dev *dev, int rq)
  3331. {
  3332. u16 v;
  3333. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  3334. return -EINVAL;
  3335. /*
  3336. * If using the "performance" PCIe config, we clamp the
  3337. * read rq size to the max packet size to prevent the
  3338. * host bridge generating requests larger than we can
  3339. * cope with
  3340. */
  3341. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  3342. int mps = pcie_get_mps(dev);
  3343. if (mps < rq)
  3344. rq = mps;
  3345. }
  3346. v = (ffs(rq) - 8) << 12;
  3347. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  3348. PCI_EXP_DEVCTL_READRQ, v);
  3349. }
  3350. EXPORT_SYMBOL(pcie_set_readrq);
  3351. /**
  3352. * pcie_get_mps - get PCI Express maximum payload size
  3353. * @dev: PCI device to query
  3354. *
  3355. * Returns maximum payload size in bytes
  3356. */
  3357. int pcie_get_mps(struct pci_dev *dev)
  3358. {
  3359. u16 ctl;
  3360. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  3361. return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
  3362. }
  3363. EXPORT_SYMBOL(pcie_get_mps);
  3364. /**
  3365. * pcie_set_mps - set PCI Express maximum payload size
  3366. * @dev: PCI device to query
  3367. * @mps: maximum payload size in bytes
  3368. * valid values are 128, 256, 512, 1024, 2048, 4096
  3369. *
  3370. * If possible sets maximum payload size
  3371. */
  3372. int pcie_set_mps(struct pci_dev *dev, int mps)
  3373. {
  3374. u16 v;
  3375. if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
  3376. return -EINVAL;
  3377. v = ffs(mps) - 8;
  3378. if (v > dev->pcie_mpss)
  3379. return -EINVAL;
  3380. v <<= 5;
  3381. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  3382. PCI_EXP_DEVCTL_PAYLOAD, v);
  3383. }
  3384. EXPORT_SYMBOL(pcie_set_mps);
  3385. /**
  3386. * pcie_get_minimum_link - determine minimum link settings of a PCI device
  3387. * @dev: PCI device to query
  3388. * @speed: storage for minimum speed
  3389. * @width: storage for minimum width
  3390. *
  3391. * This function will walk up the PCI device chain and determine the minimum
  3392. * link width and speed of the device.
  3393. */
  3394. int pcie_get_minimum_link(struct pci_dev *dev, enum pci_bus_speed *speed,
  3395. enum pcie_link_width *width)
  3396. {
  3397. int ret;
  3398. *speed = PCI_SPEED_UNKNOWN;
  3399. *width = PCIE_LNK_WIDTH_UNKNOWN;
  3400. while (dev) {
  3401. u16 lnksta;
  3402. enum pci_bus_speed next_speed;
  3403. enum pcie_link_width next_width;
  3404. ret = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
  3405. if (ret)
  3406. return ret;
  3407. next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
  3408. next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
  3409. PCI_EXP_LNKSTA_NLW_SHIFT;
  3410. if (next_speed < *speed)
  3411. *speed = next_speed;
  3412. if (next_width < *width)
  3413. *width = next_width;
  3414. dev = dev->bus->self;
  3415. }
  3416. return 0;
  3417. }
  3418. EXPORT_SYMBOL(pcie_get_minimum_link);
  3419. /**
  3420. * pci_select_bars - Make BAR mask from the type of resource
  3421. * @dev: the PCI device for which BAR mask is made
  3422. * @flags: resource type mask to be selected
  3423. *
  3424. * This helper routine makes bar mask from the type of resource.
  3425. */
  3426. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  3427. {
  3428. int i, bars = 0;
  3429. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  3430. if (pci_resource_flags(dev, i) & flags)
  3431. bars |= (1 << i);
  3432. return bars;
  3433. }
  3434. /**
  3435. * pci_resource_bar - get position of the BAR associated with a resource
  3436. * @dev: the PCI device
  3437. * @resno: the resource number
  3438. * @type: the BAR type to be filled in
  3439. *
  3440. * Returns BAR position in config space, or 0 if the BAR is invalid.
  3441. */
  3442. int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
  3443. {
  3444. int reg;
  3445. if (resno < PCI_ROM_RESOURCE) {
  3446. *type = pci_bar_unknown;
  3447. return PCI_BASE_ADDRESS_0 + 4 * resno;
  3448. } else if (resno == PCI_ROM_RESOURCE) {
  3449. *type = pci_bar_mem32;
  3450. return dev->rom_base_reg;
  3451. } else if (resno < PCI_BRIDGE_RESOURCES) {
  3452. /* device specific resource */
  3453. reg = pci_iov_resource_bar(dev, resno, type);
  3454. if (reg)
  3455. return reg;
  3456. }
  3457. dev_err(&dev->dev, "BAR %d: invalid resource\n", resno);
  3458. return 0;
  3459. }
  3460. /* Some architectures require additional programming to enable VGA */
  3461. static arch_set_vga_state_t arch_set_vga_state;
  3462. void __init pci_register_set_vga_state(arch_set_vga_state_t func)
  3463. {
  3464. arch_set_vga_state = func; /* NULL disables */
  3465. }
  3466. static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
  3467. unsigned int command_bits, u32 flags)
  3468. {
  3469. if (arch_set_vga_state)
  3470. return arch_set_vga_state(dev, decode, command_bits,
  3471. flags);
  3472. return 0;
  3473. }
  3474. /**
  3475. * pci_set_vga_state - set VGA decode state on device and parents if requested
  3476. * @dev: the PCI device
  3477. * @decode: true = enable decoding, false = disable decoding
  3478. * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
  3479. * @flags: traverse ancestors and change bridges
  3480. * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
  3481. */
  3482. int pci_set_vga_state(struct pci_dev *dev, bool decode,
  3483. unsigned int command_bits, u32 flags)
  3484. {
  3485. struct pci_bus *bus;
  3486. struct pci_dev *bridge;
  3487. u16 cmd;
  3488. int rc;
  3489. WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) & (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
  3490. /* ARCH specific VGA enables */
  3491. rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
  3492. if (rc)
  3493. return rc;
  3494. if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
  3495. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  3496. if (decode == true)
  3497. cmd |= command_bits;
  3498. else
  3499. cmd &= ~command_bits;
  3500. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3501. }
  3502. if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
  3503. return 0;
  3504. bus = dev->bus;
  3505. while (bus) {
  3506. bridge = bus->self;
  3507. if (bridge) {
  3508. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
  3509. &cmd);
  3510. if (decode == true)
  3511. cmd |= PCI_BRIDGE_CTL_VGA;
  3512. else
  3513. cmd &= ~PCI_BRIDGE_CTL_VGA;
  3514. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
  3515. cmd);
  3516. }
  3517. bus = bus->parent;
  3518. }
  3519. return 0;
  3520. }
  3521. bool pci_device_is_present(struct pci_dev *pdev)
  3522. {
  3523. u32 v;
  3524. return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0);
  3525. }
  3526. EXPORT_SYMBOL_GPL(pci_device_is_present);
  3527. #define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
  3528. static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
  3529. static DEFINE_SPINLOCK(resource_alignment_lock);
  3530. /**
  3531. * pci_specified_resource_alignment - get resource alignment specified by user.
  3532. * @dev: the PCI device to get
  3533. *
  3534. * RETURNS: Resource alignment if it is specified.
  3535. * Zero if it is not specified.
  3536. */
  3537. static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev)
  3538. {
  3539. int seg, bus, slot, func, align_order, count;
  3540. resource_size_t align = 0;
  3541. char *p;
  3542. spin_lock(&resource_alignment_lock);
  3543. p = resource_alignment_param;
  3544. while (*p) {
  3545. count = 0;
  3546. if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
  3547. p[count] == '@') {
  3548. p += count + 1;
  3549. } else {
  3550. align_order = -1;
  3551. }
  3552. if (sscanf(p, "%x:%x:%x.%x%n",
  3553. &seg, &bus, &slot, &func, &count) != 4) {
  3554. seg = 0;
  3555. if (sscanf(p, "%x:%x.%x%n",
  3556. &bus, &slot, &func, &count) != 3) {
  3557. /* Invalid format */
  3558. printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
  3559. p);
  3560. break;
  3561. }
  3562. }
  3563. p += count;
  3564. if (seg == pci_domain_nr(dev->bus) &&
  3565. bus == dev->bus->number &&
  3566. slot == PCI_SLOT(dev->devfn) &&
  3567. func == PCI_FUNC(dev->devfn)) {
  3568. if (align_order == -1) {
  3569. align = PAGE_SIZE;
  3570. } else {
  3571. align = 1 << align_order;
  3572. }
  3573. /* Found */
  3574. break;
  3575. }
  3576. if (*p != ';' && *p != ',') {
  3577. /* End of param or invalid format */
  3578. break;
  3579. }
  3580. p++;
  3581. }
  3582. spin_unlock(&resource_alignment_lock);
  3583. return align;
  3584. }
  3585. /*
  3586. * This function disables memory decoding and releases memory resources
  3587. * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
  3588. * It also rounds up size to specified alignment.
  3589. * Later on, the kernel will assign page-aligned memory resource back
  3590. * to the device.
  3591. */
  3592. void pci_reassigndev_resource_alignment(struct pci_dev *dev)
  3593. {
  3594. int i;
  3595. struct resource *r;
  3596. resource_size_t align, size;
  3597. u16 command;
  3598. /* check if specified PCI is target device to reassign */
  3599. align = pci_specified_resource_alignment(dev);
  3600. if (!align)
  3601. return;
  3602. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
  3603. (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
  3604. dev_warn(&dev->dev,
  3605. "Can't reassign resources to host bridge.\n");
  3606. return;
  3607. }
  3608. dev_info(&dev->dev,
  3609. "Disabling memory decoding and releasing memory resources.\n");
  3610. pci_read_config_word(dev, PCI_COMMAND, &command);
  3611. command &= ~PCI_COMMAND_MEMORY;
  3612. pci_write_config_word(dev, PCI_COMMAND, command);
  3613. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
  3614. r = &dev->resource[i];
  3615. if (!(r->flags & IORESOURCE_MEM))
  3616. continue;
  3617. size = resource_size(r);
  3618. if (size < align) {
  3619. size = align;
  3620. dev_info(&dev->dev,
  3621. "Rounding up size of resource #%d to %#llx.\n",
  3622. i, (unsigned long long)size);
  3623. }
  3624. r->end = size - 1;
  3625. r->start = 0;
  3626. }
  3627. /* Need to disable bridge's resource window,
  3628. * to enable the kernel to reassign new resource
  3629. * window later on.
  3630. */
  3631. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  3632. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  3633. for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
  3634. r = &dev->resource[i];
  3635. if (!(r->flags & IORESOURCE_MEM))
  3636. continue;
  3637. r->end = resource_size(r) - 1;
  3638. r->start = 0;
  3639. }
  3640. pci_disable_bridge_window(dev);
  3641. }
  3642. }
  3643. static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
  3644. {
  3645. if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
  3646. count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
  3647. spin_lock(&resource_alignment_lock);
  3648. strncpy(resource_alignment_param, buf, count);
  3649. resource_alignment_param[count] = '\0';
  3650. spin_unlock(&resource_alignment_lock);
  3651. return count;
  3652. }
  3653. static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
  3654. {
  3655. size_t count;
  3656. spin_lock(&resource_alignment_lock);
  3657. count = snprintf(buf, size, "%s", resource_alignment_param);
  3658. spin_unlock(&resource_alignment_lock);
  3659. return count;
  3660. }
  3661. static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
  3662. {
  3663. return pci_get_resource_alignment_param(buf, PAGE_SIZE);
  3664. }
  3665. static ssize_t pci_resource_alignment_store(struct bus_type *bus,
  3666. const char *buf, size_t count)
  3667. {
  3668. return pci_set_resource_alignment_param(buf, count);
  3669. }
  3670. BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
  3671. pci_resource_alignment_store);
  3672. static int __init pci_resource_alignment_sysfs_init(void)
  3673. {
  3674. return bus_create_file(&pci_bus_type,
  3675. &bus_attr_resource_alignment);
  3676. }
  3677. late_initcall(pci_resource_alignment_sysfs_init);
  3678. static void pci_no_domains(void)
  3679. {
  3680. #ifdef CONFIG_PCI_DOMAINS
  3681. pci_domains_supported = 0;
  3682. #endif
  3683. }
  3684. /**
  3685. * pci_ext_cfg_avail - can we access extended PCI config space?
  3686. *
  3687. * Returns 1 if we can access PCI extended config space (offsets
  3688. * greater than 0xff). This is the default implementation. Architecture
  3689. * implementations can override this.
  3690. */
  3691. int __weak pci_ext_cfg_avail(void)
  3692. {
  3693. return 1;
  3694. }
  3695. void __weak pci_fixup_cardbus(struct pci_bus *bus)
  3696. {
  3697. }
  3698. EXPORT_SYMBOL(pci_fixup_cardbus);
  3699. static int __init pci_setup(char *str)
  3700. {
  3701. while (str) {
  3702. char *k = strchr(str, ',');
  3703. if (k)
  3704. *k++ = 0;
  3705. if (*str && (str = pcibios_setup(str)) && *str) {
  3706. if (!strcmp(str, "nomsi")) {
  3707. pci_no_msi();
  3708. } else if (!strcmp(str, "noaer")) {
  3709. pci_no_aer();
  3710. } else if (!strncmp(str, "realloc=", 8)) {
  3711. pci_realloc_get_opt(str + 8);
  3712. } else if (!strncmp(str, "realloc", 7)) {
  3713. pci_realloc_get_opt("on");
  3714. } else if (!strcmp(str, "nodomains")) {
  3715. pci_no_domains();
  3716. } else if (!strncmp(str, "noari", 5)) {
  3717. pcie_ari_disabled = true;
  3718. } else if (!strncmp(str, "cbiosize=", 9)) {
  3719. pci_cardbus_io_size = memparse(str + 9, &str);
  3720. } else if (!strncmp(str, "cbmemsize=", 10)) {
  3721. pci_cardbus_mem_size = memparse(str + 10, &str);
  3722. } else if (!strncmp(str, "resource_alignment=", 19)) {
  3723. pci_set_resource_alignment_param(str + 19,
  3724. strlen(str + 19));
  3725. } else if (!strncmp(str, "ecrc=", 5)) {
  3726. pcie_ecrc_get_policy(str + 5);
  3727. } else if (!strncmp(str, "hpiosize=", 9)) {
  3728. pci_hotplug_io_size = memparse(str + 9, &str);
  3729. } else if (!strncmp(str, "hpmemsize=", 10)) {
  3730. pci_hotplug_mem_size = memparse(str + 10, &str);
  3731. } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
  3732. pcie_bus_config = PCIE_BUS_TUNE_OFF;
  3733. } else if (!strncmp(str, "pcie_bus_safe", 13)) {
  3734. pcie_bus_config = PCIE_BUS_SAFE;
  3735. } else if (!strncmp(str, "pcie_bus_perf", 13)) {
  3736. pcie_bus_config = PCIE_BUS_PERFORMANCE;
  3737. } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
  3738. pcie_bus_config = PCIE_BUS_PEER2PEER;
  3739. } else if (!strncmp(str, "pcie_scan_all", 13)) {
  3740. pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
  3741. } else {
  3742. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  3743. str);
  3744. }
  3745. }
  3746. str = k;
  3747. }
  3748. return 0;
  3749. }
  3750. early_param("pci", pci_setup);
  3751. EXPORT_SYMBOL(pci_reenable_device);
  3752. EXPORT_SYMBOL(pci_enable_device_io);
  3753. EXPORT_SYMBOL(pci_enable_device_mem);
  3754. EXPORT_SYMBOL(pci_enable_device);
  3755. EXPORT_SYMBOL(pcim_enable_device);
  3756. EXPORT_SYMBOL(pcim_pin_device);
  3757. EXPORT_SYMBOL(pci_disable_device);
  3758. EXPORT_SYMBOL(pci_find_capability);
  3759. EXPORT_SYMBOL(pci_bus_find_capability);
  3760. EXPORT_SYMBOL(pci_release_regions);
  3761. EXPORT_SYMBOL(pci_request_regions);
  3762. EXPORT_SYMBOL(pci_request_regions_exclusive);
  3763. EXPORT_SYMBOL(pci_release_region);
  3764. EXPORT_SYMBOL(pci_request_region);
  3765. EXPORT_SYMBOL(pci_request_region_exclusive);
  3766. EXPORT_SYMBOL(pci_release_selected_regions);
  3767. EXPORT_SYMBOL(pci_request_selected_regions);
  3768. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  3769. EXPORT_SYMBOL(pci_set_master);
  3770. EXPORT_SYMBOL(pci_clear_master);
  3771. EXPORT_SYMBOL(pci_set_mwi);
  3772. EXPORT_SYMBOL(pci_try_set_mwi);
  3773. EXPORT_SYMBOL(pci_clear_mwi);
  3774. EXPORT_SYMBOL_GPL(pci_intx);
  3775. EXPORT_SYMBOL(pci_assign_resource);
  3776. EXPORT_SYMBOL(pci_find_parent_resource);
  3777. EXPORT_SYMBOL(pci_select_bars);
  3778. EXPORT_SYMBOL(pci_set_power_state);
  3779. EXPORT_SYMBOL(pci_save_state);
  3780. EXPORT_SYMBOL(pci_restore_state);
  3781. EXPORT_SYMBOL(pci_pme_capable);
  3782. EXPORT_SYMBOL(pci_pme_active);
  3783. EXPORT_SYMBOL(pci_wake_from_d3);
  3784. EXPORT_SYMBOL(pci_prepare_to_sleep);
  3785. EXPORT_SYMBOL(pci_back_from_sleep);
  3786. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);