pci.c 119 KB

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