pci.c 126 KB

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