pci.c 117 KB

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