pci.c 119 KB

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