pci.c 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Bus Services, see include/linux/pci.h for further explanation.
  4. *
  5. * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  6. * David Mosberger-Tang
  7. *
  8. * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/dmi.h>
  14. #include <linux/init.h>
  15. #include <linux/of.h>
  16. #include <linux/of_pci.h>
  17. #include <linux/pci.h>
  18. #include <linux/pm.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/string.h>
  23. #include <linux/log2.h>
  24. #include <linux/logic_pio.h>
  25. #include <linux/pm_wakeup.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/device.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/pci_hotplug.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/pci-ats.h>
  32. #include <asm/setup.h>
  33. #include <asm/dma.h>
  34. #include <linux/aer.h>
  35. #include "pci.h"
  36. const char *pci_power_names[] = {
  37. "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
  38. };
  39. EXPORT_SYMBOL_GPL(pci_power_names);
  40. int isa_dma_bridge_buggy;
  41. EXPORT_SYMBOL(isa_dma_bridge_buggy);
  42. int pci_pci_problems;
  43. EXPORT_SYMBOL(pci_pci_problems);
  44. unsigned int pci_pm_d3_delay;
  45. static void pci_pme_list_scan(struct work_struct *work);
  46. static LIST_HEAD(pci_pme_list);
  47. static DEFINE_MUTEX(pci_pme_list_mutex);
  48. static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
  49. struct pci_pme_device {
  50. struct list_head list;
  51. struct pci_dev *dev;
  52. };
  53. #define PME_TIMEOUT 1000 /* How long between PME checks */
  54. static void pci_dev_d3_sleep(struct pci_dev *dev)
  55. {
  56. unsigned int delay = dev->d3_delay;
  57. if (delay < pci_pm_d3_delay)
  58. delay = pci_pm_d3_delay;
  59. if (delay)
  60. msleep(delay);
  61. }
  62. #ifdef CONFIG_PCI_DOMAINS
  63. int pci_domains_supported = 1;
  64. #endif
  65. #define DEFAULT_CARDBUS_IO_SIZE (256)
  66. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  67. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  68. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  69. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  70. #define DEFAULT_HOTPLUG_IO_SIZE (256)
  71. #define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
  72. /* pci=hpmemsize=nnM,hpiosize=nn can override this */
  73. unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
  74. unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
  75. #define DEFAULT_HOTPLUG_BUS_SIZE 1
  76. unsigned long pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
  77. enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_DEFAULT;
  78. /*
  79. * The default CLS is used if arch didn't set CLS explicitly and not
  80. * all pci devices agree on the same value. Arch can override either
  81. * the dfl or actual value as it sees fit. Don't forget this is
  82. * measured in 32-bit words, not bytes.
  83. */
  84. u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
  85. u8 pci_cache_line_size;
  86. /*
  87. * If we set up a device for bus mastering, we need to check the latency
  88. * timer as certain BIOSes forget to set it properly.
  89. */
  90. unsigned int pcibios_max_latency = 255;
  91. /* If set, the PCIe ARI capability will not be used. */
  92. static bool pcie_ari_disabled;
  93. /* If set, the PCIe ATS capability will not be used. */
  94. static bool pcie_ats_disabled;
  95. /* If set, the PCI config space of each device is printed during boot. */
  96. bool pci_early_dump;
  97. bool pci_ats_disabled(void)
  98. {
  99. return pcie_ats_disabled;
  100. }
  101. /* Disable bridge_d3 for all PCIe ports */
  102. static bool pci_bridge_d3_disable;
  103. /* Force bridge_d3 for all PCIe ports */
  104. static bool pci_bridge_d3_force;
  105. static int __init pcie_port_pm_setup(char *str)
  106. {
  107. if (!strcmp(str, "off"))
  108. pci_bridge_d3_disable = true;
  109. else if (!strcmp(str, "force"))
  110. pci_bridge_d3_force = true;
  111. return 1;
  112. }
  113. __setup("pcie_port_pm=", pcie_port_pm_setup);
  114. /* Time to wait after a reset for device to become responsive */
  115. #define PCIE_RESET_READY_POLL_MS 60000
  116. /**
  117. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  118. * @bus: pointer to PCI bus structure to search
  119. *
  120. * Given a PCI bus, returns the highest PCI bus number present in the set
  121. * including the given PCI bus and its list of child PCI buses.
  122. */
  123. unsigned char pci_bus_max_busnr(struct pci_bus *bus)
  124. {
  125. struct pci_bus *tmp;
  126. unsigned char max, n;
  127. max = bus->busn_res.end;
  128. list_for_each_entry(tmp, &bus->children, node) {
  129. n = pci_bus_max_busnr(tmp);
  130. if (n > max)
  131. max = n;
  132. }
  133. return max;
  134. }
  135. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  136. #ifdef CONFIG_HAS_IOMEM
  137. void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
  138. {
  139. struct resource *res = &pdev->resource[bar];
  140. /*
  141. * Make sure the BAR is actually a memory resource, not an IO resource
  142. */
  143. if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) {
  144. pci_warn(pdev, "can't ioremap BAR %d: %pR\n", bar, res);
  145. return NULL;
  146. }
  147. return ioremap_nocache(res->start, resource_size(res));
  148. }
  149. EXPORT_SYMBOL_GPL(pci_ioremap_bar);
  150. void __iomem *pci_ioremap_wc_bar(struct pci_dev *pdev, int bar)
  151. {
  152. /*
  153. * Make sure the BAR is actually a memory resource, not an IO resource
  154. */
  155. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  156. WARN_ON(1);
  157. return NULL;
  158. }
  159. return ioremap_wc(pci_resource_start(pdev, bar),
  160. pci_resource_len(pdev, bar));
  161. }
  162. EXPORT_SYMBOL_GPL(pci_ioremap_wc_bar);
  163. #endif
  164. /**
  165. * pci_dev_str_match_path - test if a path string matches a device
  166. * @dev: the PCI device to test
  167. * @p: string to match the device against
  168. * @endptr: pointer to the string after the match
  169. *
  170. * Test if a string (typically from a kernel parameter) formatted as a
  171. * path of device/function addresses matches a PCI device. The string must
  172. * be of the form:
  173. *
  174. * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
  175. *
  176. * A path for a device can be obtained using 'lspci -t'. Using a path
  177. * is more robust against bus renumbering than using only a single bus,
  178. * device and function address.
  179. *
  180. * Returns 1 if the string matches the device, 0 if it does not and
  181. * a negative error code if it fails to parse the string.
  182. */
  183. static int pci_dev_str_match_path(struct pci_dev *dev, const char *path,
  184. const char **endptr)
  185. {
  186. int ret;
  187. int seg, bus, slot, func;
  188. char *wpath, *p;
  189. char end;
  190. *endptr = strchrnul(path, ';');
  191. wpath = kmemdup_nul(path, *endptr - path, GFP_KERNEL);
  192. if (!wpath)
  193. return -ENOMEM;
  194. while (1) {
  195. p = strrchr(wpath, '/');
  196. if (!p)
  197. break;
  198. ret = sscanf(p, "/%x.%x%c", &slot, &func, &end);
  199. if (ret != 2) {
  200. ret = -EINVAL;
  201. goto free_and_exit;
  202. }
  203. if (dev->devfn != PCI_DEVFN(slot, func)) {
  204. ret = 0;
  205. goto free_and_exit;
  206. }
  207. /*
  208. * Note: we don't need to get a reference to the upstream
  209. * bridge because we hold a reference to the top level
  210. * device which should hold a reference to the bridge,
  211. * and so on.
  212. */
  213. dev = pci_upstream_bridge(dev);
  214. if (!dev) {
  215. ret = 0;
  216. goto free_and_exit;
  217. }
  218. *p = 0;
  219. }
  220. ret = sscanf(wpath, "%x:%x:%x.%x%c", &seg, &bus, &slot,
  221. &func, &end);
  222. if (ret != 4) {
  223. seg = 0;
  224. ret = sscanf(wpath, "%x:%x.%x%c", &bus, &slot, &func, &end);
  225. if (ret != 3) {
  226. ret = -EINVAL;
  227. goto free_and_exit;
  228. }
  229. }
  230. ret = (seg == pci_domain_nr(dev->bus) &&
  231. bus == dev->bus->number &&
  232. dev->devfn == PCI_DEVFN(slot, func));
  233. free_and_exit:
  234. kfree(wpath);
  235. return ret;
  236. }
  237. /**
  238. * pci_dev_str_match - test if a string matches a device
  239. * @dev: the PCI device to test
  240. * @p: string to match the device against
  241. * @endptr: pointer to the string after the match
  242. *
  243. * Test if a string (typically from a kernel parameter) matches a specified
  244. * PCI device. The string may be of one of the following formats:
  245. *
  246. * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
  247. * pci:<vendor>:<device>[:<subvendor>:<subdevice>]
  248. *
  249. * The first format specifies a PCI bus/device/function address which
  250. * may change if new hardware is inserted, if motherboard firmware changes,
  251. * or due to changes caused in kernel parameters. If the domain is
  252. * left unspecified, it is taken to be 0. In order to be robust against
  253. * bus renumbering issues, a path of PCI device/function numbers may be used
  254. * to address the specific device. The path for a device can be determined
  255. * through the use of 'lspci -t'.
  256. *
  257. * The second format matches devices using IDs in the configuration
  258. * space which may match multiple devices in the system. A value of 0
  259. * for any field will match all devices. (Note: this differs from
  260. * in-kernel code that uses PCI_ANY_ID which is ~0; this is for
  261. * legacy reasons and convenience so users don't have to specify
  262. * FFFFFFFFs on the command line.)
  263. *
  264. * Returns 1 if the string matches the device, 0 if it does not and
  265. * a negative error code if the string cannot be parsed.
  266. */
  267. static int pci_dev_str_match(struct pci_dev *dev, const char *p,
  268. const char **endptr)
  269. {
  270. int ret;
  271. int count;
  272. unsigned short vendor, device, subsystem_vendor, subsystem_device;
  273. if (strncmp(p, "pci:", 4) == 0) {
  274. /* PCI vendor/device (subvendor/subdevice) IDs are specified */
  275. p += 4;
  276. ret = sscanf(p, "%hx:%hx:%hx:%hx%n", &vendor, &device,
  277. &subsystem_vendor, &subsystem_device, &count);
  278. if (ret != 4) {
  279. ret = sscanf(p, "%hx:%hx%n", &vendor, &device, &count);
  280. if (ret != 2)
  281. return -EINVAL;
  282. subsystem_vendor = 0;
  283. subsystem_device = 0;
  284. }
  285. p += count;
  286. if ((!vendor || vendor == dev->vendor) &&
  287. (!device || device == dev->device) &&
  288. (!subsystem_vendor ||
  289. subsystem_vendor == dev->subsystem_vendor) &&
  290. (!subsystem_device ||
  291. subsystem_device == dev->subsystem_device))
  292. goto found;
  293. } else {
  294. /*
  295. * PCI Bus, Device, Function IDs are specified
  296. * (optionally, may include a path of devfns following it)
  297. */
  298. ret = pci_dev_str_match_path(dev, p, &p);
  299. if (ret < 0)
  300. return ret;
  301. else if (ret)
  302. goto found;
  303. }
  304. *endptr = p;
  305. return 0;
  306. found:
  307. *endptr = p;
  308. return 1;
  309. }
  310. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  311. u8 pos, int cap, int *ttl)
  312. {
  313. u8 id;
  314. u16 ent;
  315. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  316. while ((*ttl)--) {
  317. if (pos < 0x40)
  318. break;
  319. pos &= ~3;
  320. pci_bus_read_config_word(bus, devfn, pos, &ent);
  321. id = ent & 0xff;
  322. if (id == 0xff)
  323. break;
  324. if (id == cap)
  325. return pos;
  326. pos = (ent >> 8);
  327. }
  328. return 0;
  329. }
  330. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  331. u8 pos, int cap)
  332. {
  333. int ttl = PCI_FIND_CAP_TTL;
  334. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  335. }
  336. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  337. {
  338. return __pci_find_next_cap(dev->bus, dev->devfn,
  339. pos + PCI_CAP_LIST_NEXT, cap);
  340. }
  341. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  342. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  343. unsigned int devfn, u8 hdr_type)
  344. {
  345. u16 status;
  346. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  347. if (!(status & PCI_STATUS_CAP_LIST))
  348. return 0;
  349. switch (hdr_type) {
  350. case PCI_HEADER_TYPE_NORMAL:
  351. case PCI_HEADER_TYPE_BRIDGE:
  352. return PCI_CAPABILITY_LIST;
  353. case PCI_HEADER_TYPE_CARDBUS:
  354. return PCI_CB_CAPABILITY_LIST;
  355. }
  356. return 0;
  357. }
  358. /**
  359. * pci_find_capability - query for devices' capabilities
  360. * @dev: PCI device to query
  361. * @cap: capability code
  362. *
  363. * Tell if a device supports a given PCI capability.
  364. * Returns the address of the requested capability structure within the
  365. * device's PCI configuration space or 0 in case the device does not
  366. * support it. Possible values for @cap:
  367. *
  368. * %PCI_CAP_ID_PM Power Management
  369. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  370. * %PCI_CAP_ID_VPD Vital Product Data
  371. * %PCI_CAP_ID_SLOTID Slot Identification
  372. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  373. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  374. * %PCI_CAP_ID_PCIX PCI-X
  375. * %PCI_CAP_ID_EXP PCI Express
  376. */
  377. int pci_find_capability(struct pci_dev *dev, int cap)
  378. {
  379. int pos;
  380. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  381. if (pos)
  382. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  383. return pos;
  384. }
  385. EXPORT_SYMBOL(pci_find_capability);
  386. /**
  387. * pci_bus_find_capability - query for devices' capabilities
  388. * @bus: the PCI bus to query
  389. * @devfn: PCI device to query
  390. * @cap: capability code
  391. *
  392. * Like pci_find_capability() but works for pci devices that do not have a
  393. * pci_dev structure set up yet.
  394. *
  395. * Returns the address of the requested capability structure within the
  396. * device's PCI configuration space or 0 in case the device does not
  397. * support it.
  398. */
  399. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  400. {
  401. int pos;
  402. u8 hdr_type;
  403. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  404. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  405. if (pos)
  406. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  407. return pos;
  408. }
  409. EXPORT_SYMBOL(pci_bus_find_capability);
  410. /**
  411. * pci_find_next_ext_capability - Find an extended capability
  412. * @dev: PCI device to query
  413. * @start: address at which to start looking (0 to start at beginning of list)
  414. * @cap: capability code
  415. *
  416. * Returns the address of the next matching extended capability structure
  417. * within the device's PCI configuration space or 0 if the device does
  418. * not support it. Some capabilities can occur several times, e.g., the
  419. * vendor-specific capability, and this provides a way to find them all.
  420. */
  421. int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
  422. {
  423. u32 header;
  424. int ttl;
  425. int pos = PCI_CFG_SPACE_SIZE;
  426. /* minimum 8 bytes per capability */
  427. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  428. if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
  429. return 0;
  430. if (start)
  431. pos = start;
  432. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  433. return 0;
  434. /*
  435. * If we have no capabilities, this is indicated by cap ID,
  436. * cap version and next pointer all being 0.
  437. */
  438. if (header == 0)
  439. return 0;
  440. while (ttl-- > 0) {
  441. if (PCI_EXT_CAP_ID(header) == cap && pos != start)
  442. return pos;
  443. pos = PCI_EXT_CAP_NEXT(header);
  444. if (pos < PCI_CFG_SPACE_SIZE)
  445. break;
  446. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  447. break;
  448. }
  449. return 0;
  450. }
  451. EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
  452. /**
  453. * pci_find_ext_capability - Find an extended capability
  454. * @dev: PCI device to query
  455. * @cap: capability code
  456. *
  457. * Returns the address of the requested extended capability structure
  458. * within the device's PCI configuration space or 0 if the device does
  459. * not support it. Possible values for @cap:
  460. *
  461. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  462. * %PCI_EXT_CAP_ID_VC Virtual Channel
  463. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  464. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  465. */
  466. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  467. {
  468. return pci_find_next_ext_capability(dev, 0, cap);
  469. }
  470. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  471. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  472. {
  473. int rc, ttl = PCI_FIND_CAP_TTL;
  474. u8 cap, mask;
  475. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  476. mask = HT_3BIT_CAP_MASK;
  477. else
  478. mask = HT_5BIT_CAP_MASK;
  479. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  480. PCI_CAP_ID_HT, &ttl);
  481. while (pos) {
  482. rc = pci_read_config_byte(dev, pos + 3, &cap);
  483. if (rc != PCIBIOS_SUCCESSFUL)
  484. return 0;
  485. if ((cap & mask) == ht_cap)
  486. return pos;
  487. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  488. pos + PCI_CAP_LIST_NEXT,
  489. PCI_CAP_ID_HT, &ttl);
  490. }
  491. return 0;
  492. }
  493. /**
  494. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  495. * @dev: PCI device to query
  496. * @pos: Position from which to continue searching
  497. * @ht_cap: Hypertransport capability code
  498. *
  499. * To be used in conjunction with pci_find_ht_capability() to search for
  500. * all capabilities matching @ht_cap. @pos should always be a value returned
  501. * from pci_find_ht_capability().
  502. *
  503. * NB. To be 100% safe against broken PCI devices, the caller should take
  504. * steps to avoid an infinite loop.
  505. */
  506. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  507. {
  508. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  509. }
  510. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  511. /**
  512. * pci_find_ht_capability - query a device's Hypertransport capabilities
  513. * @dev: PCI device to query
  514. * @ht_cap: Hypertransport capability code
  515. *
  516. * Tell if a device supports a given Hypertransport capability.
  517. * Returns an address within the device's PCI configuration space
  518. * or 0 in case the device does not support the request capability.
  519. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  520. * which has a Hypertransport capability matching @ht_cap.
  521. */
  522. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  523. {
  524. int pos;
  525. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  526. if (pos)
  527. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  528. return pos;
  529. }
  530. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  531. /**
  532. * pci_find_parent_resource - return resource region of parent bus of given region
  533. * @dev: PCI device structure contains resources to be searched
  534. * @res: child resource record for which parent is sought
  535. *
  536. * For given resource region of given device, return the resource
  537. * region of parent bus the given region is contained in.
  538. */
  539. struct resource *pci_find_parent_resource(const struct pci_dev *dev,
  540. struct resource *res)
  541. {
  542. const struct pci_bus *bus = dev->bus;
  543. struct resource *r;
  544. int i;
  545. pci_bus_for_each_resource(bus, r, i) {
  546. if (!r)
  547. continue;
  548. if (resource_contains(r, res)) {
  549. /*
  550. * If the window is prefetchable but the BAR is
  551. * not, the allocator made a mistake.
  552. */
  553. if (r->flags & IORESOURCE_PREFETCH &&
  554. !(res->flags & IORESOURCE_PREFETCH))
  555. return NULL;
  556. /*
  557. * If we're below a transparent bridge, there may
  558. * be both a positively-decoded aperture and a
  559. * subtractively-decoded region that contain the BAR.
  560. * We want the positively-decoded one, so this depends
  561. * on pci_bus_for_each_resource() giving us those
  562. * first.
  563. */
  564. return r;
  565. }
  566. }
  567. return NULL;
  568. }
  569. EXPORT_SYMBOL(pci_find_parent_resource);
  570. /**
  571. * pci_find_resource - Return matching PCI device resource
  572. * @dev: PCI device to query
  573. * @res: Resource to look for
  574. *
  575. * Goes over standard PCI resources (BARs) and checks if the given resource
  576. * is partially or fully contained in any of them. In that case the
  577. * matching resource is returned, %NULL otherwise.
  578. */
  579. struct resource *pci_find_resource(struct pci_dev *dev, struct resource *res)
  580. {
  581. int i;
  582. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  583. struct resource *r = &dev->resource[i];
  584. if (r->start && resource_contains(r, res))
  585. return r;
  586. }
  587. return NULL;
  588. }
  589. EXPORT_SYMBOL(pci_find_resource);
  590. /**
  591. * pci_find_pcie_root_port - return PCIe Root Port
  592. * @dev: PCI device to query
  593. *
  594. * Traverse up the parent chain and return the PCIe Root Port PCI Device
  595. * for a given PCI Device.
  596. */
  597. struct pci_dev *pci_find_pcie_root_port(struct pci_dev *dev)
  598. {
  599. struct pci_dev *bridge, *highest_pcie_bridge = dev;
  600. bridge = pci_upstream_bridge(dev);
  601. while (bridge && pci_is_pcie(bridge)) {
  602. highest_pcie_bridge = bridge;
  603. bridge = pci_upstream_bridge(bridge);
  604. }
  605. if (pci_pcie_type(highest_pcie_bridge) != PCI_EXP_TYPE_ROOT_PORT)
  606. return NULL;
  607. return highest_pcie_bridge;
  608. }
  609. EXPORT_SYMBOL(pci_find_pcie_root_port);
  610. /**
  611. * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
  612. * @dev: the PCI device to operate on
  613. * @pos: config space offset of status word
  614. * @mask: mask of bit(s) to care about in status word
  615. *
  616. * Return 1 when mask bit(s) in status word clear, 0 otherwise.
  617. */
  618. int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
  619. {
  620. int i;
  621. /* Wait for Transaction Pending bit clean */
  622. for (i = 0; i < 4; i++) {
  623. u16 status;
  624. if (i)
  625. msleep((1 << (i - 1)) * 100);
  626. pci_read_config_word(dev, pos, &status);
  627. if (!(status & mask))
  628. return 1;
  629. }
  630. return 0;
  631. }
  632. /**
  633. * pci_restore_bars - restore a device's BAR values (e.g. after wake-up)
  634. * @dev: PCI device to have its BARs restored
  635. *
  636. * Restore the BAR values for a given device, so as to make it
  637. * accessible by its driver.
  638. */
  639. static void pci_restore_bars(struct pci_dev *dev)
  640. {
  641. int i;
  642. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
  643. pci_update_resource(dev, i);
  644. }
  645. static const struct pci_platform_pm_ops *pci_platform_pm;
  646. int pci_set_platform_pm(const struct pci_platform_pm_ops *ops)
  647. {
  648. if (!ops->is_manageable || !ops->set_state || !ops->get_state ||
  649. !ops->choose_state || !ops->set_wakeup || !ops->need_resume)
  650. return -EINVAL;
  651. pci_platform_pm = ops;
  652. return 0;
  653. }
  654. static inline bool platform_pci_power_manageable(struct pci_dev *dev)
  655. {
  656. return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
  657. }
  658. static inline int platform_pci_set_power_state(struct pci_dev *dev,
  659. pci_power_t t)
  660. {
  661. return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
  662. }
  663. static inline pci_power_t platform_pci_get_power_state(struct pci_dev *dev)
  664. {
  665. return pci_platform_pm ? pci_platform_pm->get_state(dev) : PCI_UNKNOWN;
  666. }
  667. static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
  668. {
  669. return pci_platform_pm ?
  670. pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
  671. }
  672. static inline int platform_pci_set_wakeup(struct pci_dev *dev, bool enable)
  673. {
  674. return pci_platform_pm ?
  675. pci_platform_pm->set_wakeup(dev, enable) : -ENODEV;
  676. }
  677. static inline bool platform_pci_need_resume(struct pci_dev *dev)
  678. {
  679. return pci_platform_pm ? pci_platform_pm->need_resume(dev) : false;
  680. }
  681. /**
  682. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  683. * given PCI device
  684. * @dev: PCI device to handle.
  685. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  686. *
  687. * RETURN VALUE:
  688. * -EINVAL if the requested state is invalid.
  689. * -EIO if device does not support PCI PM or its PM capabilities register has a
  690. * wrong version, or device doesn't support the requested state.
  691. * 0 if device already is in the requested state.
  692. * 0 if device's power state has been successfully changed.
  693. */
  694. static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
  695. {
  696. u16 pmcsr;
  697. bool need_restore = false;
  698. /* Check if we're already there */
  699. if (dev->current_state == state)
  700. return 0;
  701. if (!dev->pm_cap)
  702. return -EIO;
  703. if (state < PCI_D0 || state > PCI_D3hot)
  704. return -EINVAL;
  705. /* Validate current state:
  706. * Can enter D0 from any state, but if we can only go deeper
  707. * to sleep if we're already in a low power state
  708. */
  709. if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  710. && dev->current_state > state) {
  711. pci_err(dev, "invalid power transition (from state %d to %d)\n",
  712. dev->current_state, state);
  713. return -EINVAL;
  714. }
  715. /* check if this device supports the desired state */
  716. if ((state == PCI_D1 && !dev->d1_support)
  717. || (state == PCI_D2 && !dev->d2_support))
  718. return -EIO;
  719. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  720. /* If we're (effectively) in D3, force entire word to 0.
  721. * This doesn't affect PME_Status, disables PME_En, and
  722. * sets PowerState to 0.
  723. */
  724. switch (dev->current_state) {
  725. case PCI_D0:
  726. case PCI_D1:
  727. case PCI_D2:
  728. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  729. pmcsr |= state;
  730. break;
  731. case PCI_D3hot:
  732. case PCI_D3cold:
  733. case PCI_UNKNOWN: /* Boot-up */
  734. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  735. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  736. need_restore = true;
  737. /* Fall-through: force to D0 */
  738. default:
  739. pmcsr = 0;
  740. break;
  741. }
  742. /* enter specified state */
  743. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  744. /* Mandatory power management transition delays */
  745. /* see PCI PM 1.1 5.6.1 table 18 */
  746. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  747. pci_dev_d3_sleep(dev);
  748. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  749. udelay(PCI_PM_D2_DELAY);
  750. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  751. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  752. if (dev->current_state != state && printk_ratelimit())
  753. pci_info(dev, "Refused to change power state, currently in D%d\n",
  754. dev->current_state);
  755. /*
  756. * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  757. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  758. * from D3hot to D0 _may_ perform an internal reset, thereby
  759. * going to "D0 Uninitialized" rather than "D0 Initialized".
  760. * For example, at least some versions of the 3c905B and the
  761. * 3c556B exhibit this behaviour.
  762. *
  763. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  764. * devices in a D3hot state at boot. Consequently, we need to
  765. * restore at least the BARs so that the device will be
  766. * accessible to its driver.
  767. */
  768. if (need_restore)
  769. pci_restore_bars(dev);
  770. if (dev->bus->self)
  771. pcie_aspm_pm_state_change(dev->bus->self);
  772. return 0;
  773. }
  774. /**
  775. * pci_update_current_state - Read power state of given device and cache it
  776. * @dev: PCI device to handle.
  777. * @state: State to cache in case the device doesn't have the PM capability
  778. *
  779. * The power state is read from the PMCSR register, which however is
  780. * inaccessible in D3cold. The platform firmware is therefore queried first
  781. * to detect accessibility of the register. In case the platform firmware
  782. * reports an incorrect state or the device isn't power manageable by the
  783. * platform at all, we try to detect D3cold by testing accessibility of the
  784. * vendor ID in config space.
  785. */
  786. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  787. {
  788. if (platform_pci_get_power_state(dev) == PCI_D3cold ||
  789. !pci_device_is_present(dev)) {
  790. dev->current_state = PCI_D3cold;
  791. } else if (dev->pm_cap) {
  792. u16 pmcsr;
  793. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  794. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  795. } else {
  796. dev->current_state = state;
  797. }
  798. }
  799. /**
  800. * pci_power_up - Put the given device into D0 forcibly
  801. * @dev: PCI device to power up
  802. */
  803. void pci_power_up(struct pci_dev *dev)
  804. {
  805. if (platform_pci_power_manageable(dev))
  806. platform_pci_set_power_state(dev, PCI_D0);
  807. pci_raw_set_power_state(dev, PCI_D0);
  808. pci_update_current_state(dev, PCI_D0);
  809. }
  810. /**
  811. * pci_platform_power_transition - Use platform to change device power state
  812. * @dev: PCI device to handle.
  813. * @state: State to put the device into.
  814. */
  815. static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
  816. {
  817. int error;
  818. if (platform_pci_power_manageable(dev)) {
  819. error = platform_pci_set_power_state(dev, state);
  820. if (!error)
  821. pci_update_current_state(dev, state);
  822. } else
  823. error = -ENODEV;
  824. if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
  825. dev->current_state = PCI_D0;
  826. return error;
  827. }
  828. /**
  829. * pci_wakeup - Wake up a PCI device
  830. * @pci_dev: Device to handle.
  831. * @ign: ignored parameter
  832. */
  833. static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
  834. {
  835. pci_wakeup_event(pci_dev);
  836. pm_request_resume(&pci_dev->dev);
  837. return 0;
  838. }
  839. /**
  840. * pci_wakeup_bus - Walk given bus and wake up devices on it
  841. * @bus: Top bus of the subtree to walk.
  842. */
  843. void pci_wakeup_bus(struct pci_bus *bus)
  844. {
  845. if (bus)
  846. pci_walk_bus(bus, pci_wakeup, NULL);
  847. }
  848. /**
  849. * __pci_start_power_transition - Start power transition of a PCI device
  850. * @dev: PCI device to handle.
  851. * @state: State to put the device into.
  852. */
  853. static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
  854. {
  855. if (state == PCI_D0) {
  856. pci_platform_power_transition(dev, PCI_D0);
  857. /*
  858. * Mandatory power management transition delays, see
  859. * PCI Express Base Specification Revision 2.0 Section
  860. * 6.6.1: Conventional Reset. Do not delay for
  861. * devices powered on/off by corresponding bridge,
  862. * because have already delayed for the bridge.
  863. */
  864. if (dev->runtime_d3cold) {
  865. if (dev->d3cold_delay)
  866. msleep(dev->d3cold_delay);
  867. /*
  868. * When powering on a bridge from D3cold, the
  869. * whole hierarchy may be powered on into
  870. * D0uninitialized state, resume them to give
  871. * them a chance to suspend again
  872. */
  873. pci_wakeup_bus(dev->subordinate);
  874. }
  875. }
  876. }
  877. /**
  878. * __pci_dev_set_current_state - Set current state of a PCI device
  879. * @dev: Device to handle
  880. * @data: pointer to state to be set
  881. */
  882. static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
  883. {
  884. pci_power_t state = *(pci_power_t *)data;
  885. dev->current_state = state;
  886. return 0;
  887. }
  888. /**
  889. * pci_bus_set_current_state - Walk given bus and set current state of devices
  890. * @bus: Top bus of the subtree to walk.
  891. * @state: state to be set
  892. */
  893. void pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
  894. {
  895. if (bus)
  896. pci_walk_bus(bus, __pci_dev_set_current_state, &state);
  897. }
  898. /**
  899. * __pci_complete_power_transition - Complete power transition of a PCI device
  900. * @dev: PCI device to handle.
  901. * @state: State to put the device into.
  902. *
  903. * This function should not be called directly by device drivers.
  904. */
  905. int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
  906. {
  907. int ret;
  908. if (state <= PCI_D0)
  909. return -EINVAL;
  910. ret = pci_platform_power_transition(dev, state);
  911. /* Power off the bridge may power off the whole hierarchy */
  912. if (!ret && state == PCI_D3cold)
  913. pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
  914. return ret;
  915. }
  916. EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
  917. /**
  918. * pci_set_power_state - Set the power state of a PCI device
  919. * @dev: PCI device to handle.
  920. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  921. *
  922. * Transition a device to a new power state, using the platform firmware and/or
  923. * the device's PCI PM registers.
  924. *
  925. * RETURN VALUE:
  926. * -EINVAL if the requested state is invalid.
  927. * -EIO if device does not support PCI PM or its PM capabilities register has a
  928. * wrong version, or device doesn't support the requested state.
  929. * 0 if the transition is to D1 or D2 but D1 and D2 are not supported.
  930. * 0 if device already is in the requested state.
  931. * 0 if the transition is to D3 but D3 is not supported.
  932. * 0 if device's power state has been successfully changed.
  933. */
  934. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  935. {
  936. int error;
  937. /* bound the state we're entering */
  938. if (state > PCI_D3cold)
  939. state = PCI_D3cold;
  940. else if (state < PCI_D0)
  941. state = PCI_D0;
  942. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  943. /*
  944. * If the device or the parent bridge do not support PCI PM,
  945. * ignore the request if we're doing anything other than putting
  946. * it into D0 (which would only happen on boot).
  947. */
  948. return 0;
  949. /* Check if we're already there */
  950. if (dev->current_state == state)
  951. return 0;
  952. __pci_start_power_transition(dev, state);
  953. /* This device is quirked not to be put into D3, so
  954. don't put it in D3 */
  955. if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  956. return 0;
  957. /*
  958. * To put device in D3cold, we put device into D3hot in native
  959. * way, then put device into D3cold with platform ops
  960. */
  961. error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
  962. PCI_D3hot : state);
  963. if (!__pci_complete_power_transition(dev, state))
  964. error = 0;
  965. return error;
  966. }
  967. EXPORT_SYMBOL(pci_set_power_state);
  968. /**
  969. * pci_choose_state - Choose the power state of a PCI device
  970. * @dev: PCI device to be suspended
  971. * @state: target sleep state for the whole system. This is the value
  972. * that is passed to suspend() function.
  973. *
  974. * Returns PCI power state suitable for given device and given system
  975. * message.
  976. */
  977. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  978. {
  979. pci_power_t ret;
  980. if (!dev->pm_cap)
  981. return PCI_D0;
  982. ret = platform_pci_choose_state(dev);
  983. if (ret != PCI_POWER_ERROR)
  984. return ret;
  985. switch (state.event) {
  986. case PM_EVENT_ON:
  987. return PCI_D0;
  988. case PM_EVENT_FREEZE:
  989. case PM_EVENT_PRETHAW:
  990. /* REVISIT both freeze and pre-thaw "should" use D0 */
  991. case PM_EVENT_SUSPEND:
  992. case PM_EVENT_HIBERNATE:
  993. return PCI_D3hot;
  994. default:
  995. pci_info(dev, "unrecognized suspend event %d\n",
  996. state.event);
  997. BUG();
  998. }
  999. return PCI_D0;
  1000. }
  1001. EXPORT_SYMBOL(pci_choose_state);
  1002. #define PCI_EXP_SAVE_REGS 7
  1003. static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
  1004. u16 cap, bool extended)
  1005. {
  1006. struct pci_cap_saved_state *tmp;
  1007. hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
  1008. if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
  1009. return tmp;
  1010. }
  1011. return NULL;
  1012. }
  1013. struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
  1014. {
  1015. return _pci_find_saved_cap(dev, cap, false);
  1016. }
  1017. struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
  1018. {
  1019. return _pci_find_saved_cap(dev, cap, true);
  1020. }
  1021. static int pci_save_pcie_state(struct pci_dev *dev)
  1022. {
  1023. int i = 0;
  1024. struct pci_cap_saved_state *save_state;
  1025. u16 *cap;
  1026. if (!pci_is_pcie(dev))
  1027. return 0;
  1028. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  1029. if (!save_state) {
  1030. pci_err(dev, "buffer not found in %s\n", __func__);
  1031. return -ENOMEM;
  1032. }
  1033. cap = (u16 *)&save_state->cap.data[0];
  1034. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
  1035. pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
  1036. pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
  1037. pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
  1038. pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
  1039. pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
  1040. pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
  1041. return 0;
  1042. }
  1043. static void pci_restore_pcie_state(struct pci_dev *dev)
  1044. {
  1045. int i = 0;
  1046. struct pci_cap_saved_state *save_state;
  1047. u16 *cap;
  1048. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  1049. if (!save_state)
  1050. return;
  1051. cap = (u16 *)&save_state->cap.data[0];
  1052. pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
  1053. pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
  1054. pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
  1055. pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
  1056. pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
  1057. pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
  1058. pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
  1059. }
  1060. static int pci_save_pcix_state(struct pci_dev *dev)
  1061. {
  1062. int pos;
  1063. struct pci_cap_saved_state *save_state;
  1064. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1065. if (!pos)
  1066. return 0;
  1067. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  1068. if (!save_state) {
  1069. pci_err(dev, "buffer not found in %s\n", __func__);
  1070. return -ENOMEM;
  1071. }
  1072. pci_read_config_word(dev, pos + PCI_X_CMD,
  1073. (u16 *)save_state->cap.data);
  1074. return 0;
  1075. }
  1076. static void pci_restore_pcix_state(struct pci_dev *dev)
  1077. {
  1078. int i = 0, pos;
  1079. struct pci_cap_saved_state *save_state;
  1080. u16 *cap;
  1081. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  1082. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1083. if (!save_state || !pos)
  1084. return;
  1085. cap = (u16 *)&save_state->cap.data[0];
  1086. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  1087. }
  1088. /**
  1089. * pci_save_state - save the PCI configuration space of a device before suspending
  1090. * @dev: - PCI device that we're dealing with
  1091. */
  1092. int pci_save_state(struct pci_dev *dev)
  1093. {
  1094. int i;
  1095. /* XXX: 100% dword access ok here? */
  1096. for (i = 0; i < 16; i++)
  1097. pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
  1098. dev->state_saved = true;
  1099. i = pci_save_pcie_state(dev);
  1100. if (i != 0)
  1101. return i;
  1102. i = pci_save_pcix_state(dev);
  1103. if (i != 0)
  1104. return i;
  1105. return pci_save_vc_state(dev);
  1106. }
  1107. EXPORT_SYMBOL(pci_save_state);
  1108. static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
  1109. u32 saved_val, int retry)
  1110. {
  1111. u32 val;
  1112. pci_read_config_dword(pdev, offset, &val);
  1113. if (val == saved_val)
  1114. return;
  1115. for (;;) {
  1116. pci_dbg(pdev, "restoring config space at offset %#x (was %#x, writing %#x)\n",
  1117. offset, val, saved_val);
  1118. pci_write_config_dword(pdev, offset, saved_val);
  1119. if (retry-- <= 0)
  1120. return;
  1121. pci_read_config_dword(pdev, offset, &val);
  1122. if (val == saved_val)
  1123. return;
  1124. mdelay(1);
  1125. }
  1126. }
  1127. static void pci_restore_config_space_range(struct pci_dev *pdev,
  1128. int start, int end, int retry)
  1129. {
  1130. int index;
  1131. for (index = end; index >= start; index--)
  1132. pci_restore_config_dword(pdev, 4 * index,
  1133. pdev->saved_config_space[index],
  1134. retry);
  1135. }
  1136. static void pci_restore_config_space(struct pci_dev *pdev)
  1137. {
  1138. if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  1139. pci_restore_config_space_range(pdev, 10, 15, 0);
  1140. /* Restore BARs before the command register. */
  1141. pci_restore_config_space_range(pdev, 4, 9, 10);
  1142. pci_restore_config_space_range(pdev, 0, 3, 0);
  1143. } else {
  1144. pci_restore_config_space_range(pdev, 0, 15, 0);
  1145. }
  1146. }
  1147. static void pci_restore_rebar_state(struct pci_dev *pdev)
  1148. {
  1149. unsigned int pos, nbars, i;
  1150. u32 ctrl;
  1151. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
  1152. if (!pos)
  1153. return;
  1154. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  1155. nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
  1156. PCI_REBAR_CTRL_NBAR_SHIFT;
  1157. for (i = 0; i < nbars; i++, pos += 8) {
  1158. struct resource *res;
  1159. int bar_idx, size;
  1160. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  1161. bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
  1162. res = pdev->resource + bar_idx;
  1163. size = order_base_2((resource_size(res) >> 20) | 1) - 1;
  1164. ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
  1165. ctrl |= size << PCI_REBAR_CTRL_BAR_SHIFT;
  1166. pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
  1167. }
  1168. }
  1169. /**
  1170. * pci_restore_state - Restore the saved state of a PCI device
  1171. * @dev: - PCI device that we're dealing with
  1172. */
  1173. void pci_restore_state(struct pci_dev *dev)
  1174. {
  1175. if (!dev->state_saved)
  1176. return;
  1177. /* PCI Express register must be restored first */
  1178. pci_restore_pcie_state(dev);
  1179. pci_restore_pasid_state(dev);
  1180. pci_restore_pri_state(dev);
  1181. pci_restore_ats_state(dev);
  1182. pci_restore_vc_state(dev);
  1183. pci_restore_rebar_state(dev);
  1184. pci_cleanup_aer_error_status_regs(dev);
  1185. pci_restore_config_space(dev);
  1186. pci_restore_pcix_state(dev);
  1187. pci_restore_msi_state(dev);
  1188. /* Restore ACS and IOV configuration state */
  1189. pci_enable_acs(dev);
  1190. pci_restore_iov_state(dev);
  1191. dev->state_saved = false;
  1192. }
  1193. EXPORT_SYMBOL(pci_restore_state);
  1194. struct pci_saved_state {
  1195. u32 config_space[16];
  1196. struct pci_cap_saved_data cap[0];
  1197. };
  1198. /**
  1199. * pci_store_saved_state - Allocate and return an opaque struct containing
  1200. * the device saved state.
  1201. * @dev: PCI device that we're dealing with
  1202. *
  1203. * Return NULL if no state or error.
  1204. */
  1205. struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
  1206. {
  1207. struct pci_saved_state *state;
  1208. struct pci_cap_saved_state *tmp;
  1209. struct pci_cap_saved_data *cap;
  1210. size_t size;
  1211. if (!dev->state_saved)
  1212. return NULL;
  1213. size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
  1214. hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
  1215. size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  1216. state = kzalloc(size, GFP_KERNEL);
  1217. if (!state)
  1218. return NULL;
  1219. memcpy(state->config_space, dev->saved_config_space,
  1220. sizeof(state->config_space));
  1221. cap = state->cap;
  1222. hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
  1223. size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  1224. memcpy(cap, &tmp->cap, len);
  1225. cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
  1226. }
  1227. /* Empty cap_save terminates list */
  1228. return state;
  1229. }
  1230. EXPORT_SYMBOL_GPL(pci_store_saved_state);
  1231. /**
  1232. * pci_load_saved_state - Reload the provided save state into struct pci_dev.
  1233. * @dev: PCI device that we're dealing with
  1234. * @state: Saved state returned from pci_store_saved_state()
  1235. */
  1236. int pci_load_saved_state(struct pci_dev *dev,
  1237. struct pci_saved_state *state)
  1238. {
  1239. struct pci_cap_saved_data *cap;
  1240. dev->state_saved = false;
  1241. if (!state)
  1242. return 0;
  1243. memcpy(dev->saved_config_space, state->config_space,
  1244. sizeof(state->config_space));
  1245. cap = state->cap;
  1246. while (cap->size) {
  1247. struct pci_cap_saved_state *tmp;
  1248. tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
  1249. if (!tmp || tmp->cap.size != cap->size)
  1250. return -EINVAL;
  1251. memcpy(tmp->cap.data, cap->data, tmp->cap.size);
  1252. cap = (struct pci_cap_saved_data *)((u8 *)cap +
  1253. sizeof(struct pci_cap_saved_data) + cap->size);
  1254. }
  1255. dev->state_saved = true;
  1256. return 0;
  1257. }
  1258. EXPORT_SYMBOL_GPL(pci_load_saved_state);
  1259. /**
  1260. * pci_load_and_free_saved_state - Reload the save state pointed to by state,
  1261. * and free the memory allocated for it.
  1262. * @dev: PCI device that we're dealing with
  1263. * @state: Pointer to saved state returned from pci_store_saved_state()
  1264. */
  1265. int pci_load_and_free_saved_state(struct pci_dev *dev,
  1266. struct pci_saved_state **state)
  1267. {
  1268. int ret = pci_load_saved_state(dev, *state);
  1269. kfree(*state);
  1270. *state = NULL;
  1271. return ret;
  1272. }
  1273. EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
  1274. int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
  1275. {
  1276. return pci_enable_resources(dev, bars);
  1277. }
  1278. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  1279. {
  1280. int err;
  1281. struct pci_dev *bridge;
  1282. u16 cmd;
  1283. u8 pin;
  1284. err = pci_set_power_state(dev, PCI_D0);
  1285. if (err < 0 && err != -EIO)
  1286. return err;
  1287. bridge = pci_upstream_bridge(dev);
  1288. if (bridge)
  1289. pcie_aspm_powersave_config_link(bridge);
  1290. err = pcibios_enable_device(dev, bars);
  1291. if (err < 0)
  1292. return err;
  1293. pci_fixup_device(pci_fixup_enable, dev);
  1294. if (dev->msi_enabled || dev->msix_enabled)
  1295. return 0;
  1296. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  1297. if (pin) {
  1298. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1299. if (cmd & PCI_COMMAND_INTX_DISABLE)
  1300. pci_write_config_word(dev, PCI_COMMAND,
  1301. cmd & ~PCI_COMMAND_INTX_DISABLE);
  1302. }
  1303. return 0;
  1304. }
  1305. /**
  1306. * pci_reenable_device - Resume abandoned device
  1307. * @dev: PCI device to be resumed
  1308. *
  1309. * Note this function is a backend of pci_default_resume and is not supposed
  1310. * to be called by normal code, write proper resume handler and use it instead.
  1311. */
  1312. int pci_reenable_device(struct pci_dev *dev)
  1313. {
  1314. if (pci_is_enabled(dev))
  1315. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  1316. return 0;
  1317. }
  1318. EXPORT_SYMBOL(pci_reenable_device);
  1319. static void pci_enable_bridge(struct pci_dev *dev)
  1320. {
  1321. struct pci_dev *bridge;
  1322. int retval;
  1323. bridge = pci_upstream_bridge(dev);
  1324. if (bridge)
  1325. pci_enable_bridge(bridge);
  1326. if (pci_is_enabled(dev)) {
  1327. if (!dev->is_busmaster)
  1328. pci_set_master(dev);
  1329. return;
  1330. }
  1331. retval = pci_enable_device(dev);
  1332. if (retval)
  1333. pci_err(dev, "Error enabling bridge (%d), continuing\n",
  1334. retval);
  1335. pci_set_master(dev);
  1336. }
  1337. static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
  1338. {
  1339. struct pci_dev *bridge;
  1340. int err;
  1341. int i, bars = 0;
  1342. /*
  1343. * Power state could be unknown at this point, either due to a fresh
  1344. * boot or a device removal call. So get the current power state
  1345. * so that things like MSI message writing will behave as expected
  1346. * (e.g. if the device really is in D0 at enable time).
  1347. */
  1348. if (dev->pm_cap) {
  1349. u16 pmcsr;
  1350. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1351. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  1352. }
  1353. if (atomic_inc_return(&dev->enable_cnt) > 1)
  1354. return 0; /* already enabled */
  1355. bridge = pci_upstream_bridge(dev);
  1356. if (bridge)
  1357. pci_enable_bridge(bridge);
  1358. /* only skip sriov related */
  1359. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  1360. if (dev->resource[i].flags & flags)
  1361. bars |= (1 << i);
  1362. for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
  1363. if (dev->resource[i].flags & flags)
  1364. bars |= (1 << i);
  1365. err = do_pci_enable_device(dev, bars);
  1366. if (err < 0)
  1367. atomic_dec(&dev->enable_cnt);
  1368. return err;
  1369. }
  1370. /**
  1371. * pci_enable_device_io - Initialize a device for use with IO space
  1372. * @dev: PCI device to be initialized
  1373. *
  1374. * Initialize device before it's used by a driver. Ask low-level code
  1375. * to enable I/O resources. Wake up the device if it was suspended.
  1376. * Beware, this function can fail.
  1377. */
  1378. int pci_enable_device_io(struct pci_dev *dev)
  1379. {
  1380. return pci_enable_device_flags(dev, IORESOURCE_IO);
  1381. }
  1382. EXPORT_SYMBOL(pci_enable_device_io);
  1383. /**
  1384. * pci_enable_device_mem - Initialize a device for use with Memory space
  1385. * @dev: PCI device to be initialized
  1386. *
  1387. * Initialize device before it's used by a driver. Ask low-level code
  1388. * to enable Memory resources. Wake up the device if it was suspended.
  1389. * Beware, this function can fail.
  1390. */
  1391. int pci_enable_device_mem(struct pci_dev *dev)
  1392. {
  1393. return pci_enable_device_flags(dev, IORESOURCE_MEM);
  1394. }
  1395. EXPORT_SYMBOL(pci_enable_device_mem);
  1396. /**
  1397. * pci_enable_device - Initialize device before it's used by a driver.
  1398. * @dev: PCI device to be initialized
  1399. *
  1400. * Initialize device before it's used by a driver. Ask low-level code
  1401. * to enable I/O and memory. Wake up the device if it was suspended.
  1402. * Beware, this function can fail.
  1403. *
  1404. * Note we don't actually enable the device many times if we call
  1405. * this function repeatedly (we just increment the count).
  1406. */
  1407. int pci_enable_device(struct pci_dev *dev)
  1408. {
  1409. return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  1410. }
  1411. EXPORT_SYMBOL(pci_enable_device);
  1412. /*
  1413. * Managed PCI resources. This manages device on/off, intx/msi/msix
  1414. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  1415. * there's no need to track it separately. pci_devres is initialized
  1416. * when a device is enabled using managed PCI device enable interface.
  1417. */
  1418. struct pci_devres {
  1419. unsigned int enabled:1;
  1420. unsigned int pinned:1;
  1421. unsigned int orig_intx:1;
  1422. unsigned int restore_intx:1;
  1423. unsigned int mwi:1;
  1424. u32 region_mask;
  1425. };
  1426. static void pcim_release(struct device *gendev, void *res)
  1427. {
  1428. struct pci_dev *dev = to_pci_dev(gendev);
  1429. struct pci_devres *this = res;
  1430. int i;
  1431. if (dev->msi_enabled)
  1432. pci_disable_msi(dev);
  1433. if (dev->msix_enabled)
  1434. pci_disable_msix(dev);
  1435. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  1436. if (this->region_mask & (1 << i))
  1437. pci_release_region(dev, i);
  1438. if (this->mwi)
  1439. pci_clear_mwi(dev);
  1440. if (this->restore_intx)
  1441. pci_intx(dev, this->orig_intx);
  1442. if (this->enabled && !this->pinned)
  1443. pci_disable_device(dev);
  1444. }
  1445. static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
  1446. {
  1447. struct pci_devres *dr, *new_dr;
  1448. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1449. if (dr)
  1450. return dr;
  1451. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  1452. if (!new_dr)
  1453. return NULL;
  1454. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  1455. }
  1456. static struct pci_devres *find_pci_dr(struct pci_dev *pdev)
  1457. {
  1458. if (pci_is_managed(pdev))
  1459. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1460. return NULL;
  1461. }
  1462. /**
  1463. * pcim_enable_device - Managed pci_enable_device()
  1464. * @pdev: PCI device to be initialized
  1465. *
  1466. * Managed pci_enable_device().
  1467. */
  1468. int pcim_enable_device(struct pci_dev *pdev)
  1469. {
  1470. struct pci_devres *dr;
  1471. int rc;
  1472. dr = get_pci_dr(pdev);
  1473. if (unlikely(!dr))
  1474. return -ENOMEM;
  1475. if (dr->enabled)
  1476. return 0;
  1477. rc = pci_enable_device(pdev);
  1478. if (!rc) {
  1479. pdev->is_managed = 1;
  1480. dr->enabled = 1;
  1481. }
  1482. return rc;
  1483. }
  1484. EXPORT_SYMBOL(pcim_enable_device);
  1485. /**
  1486. * pcim_pin_device - Pin managed PCI device
  1487. * @pdev: PCI device to pin
  1488. *
  1489. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  1490. * driver detach. @pdev must have been enabled with
  1491. * pcim_enable_device().
  1492. */
  1493. void pcim_pin_device(struct pci_dev *pdev)
  1494. {
  1495. struct pci_devres *dr;
  1496. dr = find_pci_dr(pdev);
  1497. WARN_ON(!dr || !dr->enabled);
  1498. if (dr)
  1499. dr->pinned = 1;
  1500. }
  1501. EXPORT_SYMBOL(pcim_pin_device);
  1502. /*
  1503. * pcibios_add_device - provide arch specific hooks when adding device dev
  1504. * @dev: the PCI device being added
  1505. *
  1506. * Permits the platform to provide architecture specific functionality when
  1507. * devices are added. This is the default implementation. Architecture
  1508. * implementations can override this.
  1509. */
  1510. int __weak pcibios_add_device(struct pci_dev *dev)
  1511. {
  1512. return 0;
  1513. }
  1514. /**
  1515. * pcibios_release_device - provide arch specific hooks when releasing device dev
  1516. * @dev: the PCI device being released
  1517. *
  1518. * Permits the platform to provide architecture specific functionality when
  1519. * devices are released. This is the default implementation. Architecture
  1520. * implementations can override this.
  1521. */
  1522. void __weak pcibios_release_device(struct pci_dev *dev) {}
  1523. /**
  1524. * pcibios_disable_device - disable arch specific PCI resources for device dev
  1525. * @dev: the PCI device to disable
  1526. *
  1527. * Disables architecture specific PCI resources for the device. This
  1528. * is the default implementation. Architecture implementations can
  1529. * override this.
  1530. */
  1531. void __weak pcibios_disable_device(struct pci_dev *dev) {}
  1532. /**
  1533. * pcibios_penalize_isa_irq - penalize an ISA IRQ
  1534. * @irq: ISA IRQ to penalize
  1535. * @active: IRQ active or not
  1536. *
  1537. * Permits the platform to provide architecture-specific functionality when
  1538. * penalizing ISA IRQs. This is the default implementation. Architecture
  1539. * implementations can override this.
  1540. */
  1541. void __weak pcibios_penalize_isa_irq(int irq, int active) {}
  1542. static void do_pci_disable_device(struct pci_dev *dev)
  1543. {
  1544. u16 pci_command;
  1545. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  1546. if (pci_command & PCI_COMMAND_MASTER) {
  1547. pci_command &= ~PCI_COMMAND_MASTER;
  1548. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  1549. }
  1550. pcibios_disable_device(dev);
  1551. }
  1552. /**
  1553. * pci_disable_enabled_device - Disable device without updating enable_cnt
  1554. * @dev: PCI device to disable
  1555. *
  1556. * NOTE: This function is a backend of PCI power management routines and is
  1557. * not supposed to be called drivers.
  1558. */
  1559. void pci_disable_enabled_device(struct pci_dev *dev)
  1560. {
  1561. if (pci_is_enabled(dev))
  1562. do_pci_disable_device(dev);
  1563. }
  1564. /**
  1565. * pci_disable_device - Disable PCI device after use
  1566. * @dev: PCI device to be disabled
  1567. *
  1568. * Signal to the system that the PCI device is not in use by the system
  1569. * anymore. This only involves disabling PCI bus-mastering, if active.
  1570. *
  1571. * Note we don't actually disable the device until all callers of
  1572. * pci_enable_device() have called pci_disable_device().
  1573. */
  1574. void pci_disable_device(struct pci_dev *dev)
  1575. {
  1576. struct pci_devres *dr;
  1577. dr = find_pci_dr(dev);
  1578. if (dr)
  1579. dr->enabled = 0;
  1580. dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
  1581. "disabling already-disabled device");
  1582. if (atomic_dec_return(&dev->enable_cnt) != 0)
  1583. return;
  1584. do_pci_disable_device(dev);
  1585. dev->is_busmaster = 0;
  1586. }
  1587. EXPORT_SYMBOL(pci_disable_device);
  1588. /**
  1589. * pcibios_set_pcie_reset_state - set reset state for device dev
  1590. * @dev: the PCIe device reset
  1591. * @state: Reset state to enter into
  1592. *
  1593. *
  1594. * Sets the PCIe reset state for the device. This is the default
  1595. * implementation. Architecture implementations can override this.
  1596. */
  1597. int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
  1598. enum pcie_reset_state state)
  1599. {
  1600. return -EINVAL;
  1601. }
  1602. /**
  1603. * pci_set_pcie_reset_state - set reset state for device dev
  1604. * @dev: the PCIe device reset
  1605. * @state: Reset state to enter into
  1606. *
  1607. *
  1608. * Sets the PCI reset state for the device.
  1609. */
  1610. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  1611. {
  1612. return pcibios_set_pcie_reset_state(dev, state);
  1613. }
  1614. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);
  1615. /**
  1616. * pcie_clear_root_pme_status - Clear root port PME interrupt status.
  1617. * @dev: PCIe root port or event collector.
  1618. */
  1619. void pcie_clear_root_pme_status(struct pci_dev *dev)
  1620. {
  1621. pcie_capability_set_dword(dev, PCI_EXP_RTSTA, PCI_EXP_RTSTA_PME);
  1622. }
  1623. /**
  1624. * pci_check_pme_status - Check if given device has generated PME.
  1625. * @dev: Device to check.
  1626. *
  1627. * Check the PME status of the device and if set, clear it and clear PME enable
  1628. * (if set). Return 'true' if PME status and PME enable were both set or
  1629. * 'false' otherwise.
  1630. */
  1631. bool pci_check_pme_status(struct pci_dev *dev)
  1632. {
  1633. int pmcsr_pos;
  1634. u16 pmcsr;
  1635. bool ret = false;
  1636. if (!dev->pm_cap)
  1637. return false;
  1638. pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
  1639. pci_read_config_word(dev, pmcsr_pos, &pmcsr);
  1640. if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
  1641. return false;
  1642. /* Clear PME status. */
  1643. pmcsr |= PCI_PM_CTRL_PME_STATUS;
  1644. if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
  1645. /* Disable PME to avoid interrupt flood. */
  1646. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1647. ret = true;
  1648. }
  1649. pci_write_config_word(dev, pmcsr_pos, pmcsr);
  1650. return ret;
  1651. }
  1652. /**
  1653. * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
  1654. * @dev: Device to handle.
  1655. * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
  1656. *
  1657. * Check if @dev has generated PME and queue a resume request for it in that
  1658. * case.
  1659. */
  1660. static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
  1661. {
  1662. if (pme_poll_reset && dev->pme_poll)
  1663. dev->pme_poll = false;
  1664. if (pci_check_pme_status(dev)) {
  1665. pci_wakeup_event(dev);
  1666. pm_request_resume(&dev->dev);
  1667. }
  1668. return 0;
  1669. }
  1670. /**
  1671. * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
  1672. * @bus: Top bus of the subtree to walk.
  1673. */
  1674. void pci_pme_wakeup_bus(struct pci_bus *bus)
  1675. {
  1676. if (bus)
  1677. pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
  1678. }
  1679. /**
  1680. * pci_pme_capable - check the capability of PCI device to generate PME#
  1681. * @dev: PCI device to handle.
  1682. * @state: PCI state from which device will issue PME#.
  1683. */
  1684. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  1685. {
  1686. if (!dev->pm_cap)
  1687. return false;
  1688. return !!(dev->pme_support & (1 << state));
  1689. }
  1690. EXPORT_SYMBOL(pci_pme_capable);
  1691. static void pci_pme_list_scan(struct work_struct *work)
  1692. {
  1693. struct pci_pme_device *pme_dev, *n;
  1694. mutex_lock(&pci_pme_list_mutex);
  1695. list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
  1696. if (pme_dev->dev->pme_poll) {
  1697. struct pci_dev *bridge;
  1698. bridge = pme_dev->dev->bus->self;
  1699. /*
  1700. * If bridge is in low power state, the
  1701. * configuration space of subordinate devices
  1702. * may be not accessible
  1703. */
  1704. if (bridge && bridge->current_state != PCI_D0)
  1705. continue;
  1706. pci_pme_wakeup(pme_dev->dev, NULL);
  1707. } else {
  1708. list_del(&pme_dev->list);
  1709. kfree(pme_dev);
  1710. }
  1711. }
  1712. if (!list_empty(&pci_pme_list))
  1713. queue_delayed_work(system_freezable_wq, &pci_pme_work,
  1714. msecs_to_jiffies(PME_TIMEOUT));
  1715. mutex_unlock(&pci_pme_list_mutex);
  1716. }
  1717. static void __pci_pme_active(struct pci_dev *dev, bool enable)
  1718. {
  1719. u16 pmcsr;
  1720. if (!dev->pme_support)
  1721. return;
  1722. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1723. /* Clear PME_Status by writing 1 to it and enable PME# */
  1724. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  1725. if (!enable)
  1726. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1727. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  1728. }
  1729. /**
  1730. * pci_pme_restore - Restore PME configuration after config space restore.
  1731. * @dev: PCI device to update.
  1732. */
  1733. void pci_pme_restore(struct pci_dev *dev)
  1734. {
  1735. u16 pmcsr;
  1736. if (!dev->pme_support)
  1737. return;
  1738. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1739. if (dev->wakeup_prepared) {
  1740. pmcsr |= PCI_PM_CTRL_PME_ENABLE;
  1741. pmcsr &= ~PCI_PM_CTRL_PME_STATUS;
  1742. } else {
  1743. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1744. pmcsr |= PCI_PM_CTRL_PME_STATUS;
  1745. }
  1746. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  1747. }
  1748. /**
  1749. * pci_pme_active - enable or disable PCI device's PME# function
  1750. * @dev: PCI device to handle.
  1751. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  1752. *
  1753. * The caller must verify that the device is capable of generating PME# before
  1754. * calling this function with @enable equal to 'true'.
  1755. */
  1756. void pci_pme_active(struct pci_dev *dev, bool enable)
  1757. {
  1758. __pci_pme_active(dev, enable);
  1759. /*
  1760. * PCI (as opposed to PCIe) PME requires that the device have
  1761. * its PME# line hooked up correctly. Not all hardware vendors
  1762. * do this, so the PME never gets delivered and the device
  1763. * remains asleep. The easiest way around this is to
  1764. * periodically walk the list of suspended devices and check
  1765. * whether any have their PME flag set. The assumption is that
  1766. * we'll wake up often enough anyway that this won't be a huge
  1767. * hit, and the power savings from the devices will still be a
  1768. * win.
  1769. *
  1770. * Although PCIe uses in-band PME message instead of PME# line
  1771. * to report PME, PME does not work for some PCIe devices in
  1772. * reality. For example, there are devices that set their PME
  1773. * status bits, but don't really bother to send a PME message;
  1774. * there are PCI Express Root Ports that don't bother to
  1775. * trigger interrupts when they receive PME messages from the
  1776. * devices below. So PME poll is used for PCIe devices too.
  1777. */
  1778. if (dev->pme_poll) {
  1779. struct pci_pme_device *pme_dev;
  1780. if (enable) {
  1781. pme_dev = kmalloc(sizeof(struct pci_pme_device),
  1782. GFP_KERNEL);
  1783. if (!pme_dev) {
  1784. pci_warn(dev, "can't enable PME#\n");
  1785. return;
  1786. }
  1787. pme_dev->dev = dev;
  1788. mutex_lock(&pci_pme_list_mutex);
  1789. list_add(&pme_dev->list, &pci_pme_list);
  1790. if (list_is_singular(&pci_pme_list))
  1791. queue_delayed_work(system_freezable_wq,
  1792. &pci_pme_work,
  1793. msecs_to_jiffies(PME_TIMEOUT));
  1794. mutex_unlock(&pci_pme_list_mutex);
  1795. } else {
  1796. mutex_lock(&pci_pme_list_mutex);
  1797. list_for_each_entry(pme_dev, &pci_pme_list, list) {
  1798. if (pme_dev->dev == dev) {
  1799. list_del(&pme_dev->list);
  1800. kfree(pme_dev);
  1801. break;
  1802. }
  1803. }
  1804. mutex_unlock(&pci_pme_list_mutex);
  1805. }
  1806. }
  1807. pci_dbg(dev, "PME# %s\n", enable ? "enabled" : "disabled");
  1808. }
  1809. EXPORT_SYMBOL(pci_pme_active);
  1810. /**
  1811. * __pci_enable_wake - enable PCI device as wakeup event source
  1812. * @dev: PCI device affected
  1813. * @state: PCI state from which device will issue wakeup events
  1814. * @enable: True to enable event generation; false to disable
  1815. *
  1816. * This enables the device as a wakeup event source, or disables it.
  1817. * When such events involves platform-specific hooks, those hooks are
  1818. * called automatically by this routine.
  1819. *
  1820. * Devices with legacy power management (no standard PCI PM capabilities)
  1821. * always require such platform hooks.
  1822. *
  1823. * RETURN VALUE:
  1824. * 0 is returned on success
  1825. * -EINVAL is returned if device is not supposed to wake up the system
  1826. * Error code depending on the platform is returned if both the platform and
  1827. * the native mechanism fail to enable the generation of wake-up events
  1828. */
  1829. static int __pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable)
  1830. {
  1831. int ret = 0;
  1832. /*
  1833. * Bridges can only signal wakeup on behalf of subordinate devices,
  1834. * but that is set up elsewhere, so skip them.
  1835. */
  1836. if (pci_has_subordinate(dev))
  1837. return 0;
  1838. /* Don't do the same thing twice in a row for one device. */
  1839. if (!!enable == !!dev->wakeup_prepared)
  1840. return 0;
  1841. /*
  1842. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  1843. * Anderson we should be doing PME# wake enable followed by ACPI wake
  1844. * enable. To disable wake-up we call the platform first, for symmetry.
  1845. */
  1846. if (enable) {
  1847. int error;
  1848. if (pci_pme_capable(dev, state))
  1849. pci_pme_active(dev, true);
  1850. else
  1851. ret = 1;
  1852. error = platform_pci_set_wakeup(dev, true);
  1853. if (ret)
  1854. ret = error;
  1855. if (!ret)
  1856. dev->wakeup_prepared = true;
  1857. } else {
  1858. platform_pci_set_wakeup(dev, false);
  1859. pci_pme_active(dev, false);
  1860. dev->wakeup_prepared = false;
  1861. }
  1862. return ret;
  1863. }
  1864. /**
  1865. * pci_enable_wake - change wakeup settings for a PCI device
  1866. * @pci_dev: Target device
  1867. * @state: PCI state from which device will issue wakeup events
  1868. * @enable: Whether or not to enable event generation
  1869. *
  1870. * If @enable is set, check device_may_wakeup() for the device before calling
  1871. * __pci_enable_wake() for it.
  1872. */
  1873. int pci_enable_wake(struct pci_dev *pci_dev, pci_power_t state, bool enable)
  1874. {
  1875. if (enable && !device_may_wakeup(&pci_dev->dev))
  1876. return -EINVAL;
  1877. return __pci_enable_wake(pci_dev, state, enable);
  1878. }
  1879. EXPORT_SYMBOL(pci_enable_wake);
  1880. /**
  1881. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  1882. * @dev: PCI device to prepare
  1883. * @enable: True to enable wake-up event generation; false to disable
  1884. *
  1885. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  1886. * and this function allows them to set that up cleanly - pci_enable_wake()
  1887. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  1888. * ordering constraints.
  1889. *
  1890. * This function only returns error code if the device is not allowed to wake
  1891. * up the system from sleep or it is not capable of generating PME# from both
  1892. * D3_hot and D3_cold and the platform is unable to enable wake-up power for it.
  1893. */
  1894. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  1895. {
  1896. return pci_pme_capable(dev, PCI_D3cold) ?
  1897. pci_enable_wake(dev, PCI_D3cold, enable) :
  1898. pci_enable_wake(dev, PCI_D3hot, enable);
  1899. }
  1900. EXPORT_SYMBOL(pci_wake_from_d3);
  1901. /**
  1902. * pci_target_state - find an appropriate low power state for a given PCI dev
  1903. * @dev: PCI device
  1904. * @wakeup: Whether or not wakeup functionality will be enabled for the device.
  1905. *
  1906. * Use underlying platform code to find a supported low power state for @dev.
  1907. * If the platform can't manage @dev, return the deepest state from which it
  1908. * can generate wake events, based on any available PME info.
  1909. */
  1910. static pci_power_t pci_target_state(struct pci_dev *dev, bool wakeup)
  1911. {
  1912. pci_power_t target_state = PCI_D3hot;
  1913. if (platform_pci_power_manageable(dev)) {
  1914. /*
  1915. * Call the platform to find the target state for the device.
  1916. */
  1917. pci_power_t state = platform_pci_choose_state(dev);
  1918. switch (state) {
  1919. case PCI_POWER_ERROR:
  1920. case PCI_UNKNOWN:
  1921. break;
  1922. case PCI_D1:
  1923. case PCI_D2:
  1924. if (pci_no_d1d2(dev))
  1925. break;
  1926. /* else: fall through */
  1927. default:
  1928. target_state = state;
  1929. }
  1930. return target_state;
  1931. }
  1932. if (!dev->pm_cap)
  1933. target_state = PCI_D0;
  1934. /*
  1935. * If the device is in D3cold even though it's not power-manageable by
  1936. * the platform, it may have been powered down by non-standard means.
  1937. * Best to let it slumber.
  1938. */
  1939. if (dev->current_state == PCI_D3cold)
  1940. target_state = PCI_D3cold;
  1941. if (wakeup) {
  1942. /*
  1943. * Find the deepest state from which the device can generate
  1944. * PME#.
  1945. */
  1946. if (dev->pme_support) {
  1947. while (target_state
  1948. && !(dev->pme_support & (1 << target_state)))
  1949. target_state--;
  1950. }
  1951. }
  1952. return target_state;
  1953. }
  1954. /**
  1955. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1956. * @dev: Device to handle.
  1957. *
  1958. * Choose the power state appropriate for the device depending on whether
  1959. * it can wake up the system and/or is power manageable by the platform
  1960. * (PCI_D3hot is the default) and put the device into that state.
  1961. */
  1962. int pci_prepare_to_sleep(struct pci_dev *dev)
  1963. {
  1964. bool wakeup = device_may_wakeup(&dev->dev);
  1965. pci_power_t target_state = pci_target_state(dev, wakeup);
  1966. int error;
  1967. if (target_state == PCI_POWER_ERROR)
  1968. return -EIO;
  1969. pci_enable_wake(dev, target_state, wakeup);
  1970. error = pci_set_power_state(dev, target_state);
  1971. if (error)
  1972. pci_enable_wake(dev, target_state, false);
  1973. return error;
  1974. }
  1975. EXPORT_SYMBOL(pci_prepare_to_sleep);
  1976. /**
  1977. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1978. * @dev: Device to handle.
  1979. *
  1980. * Disable device's system wake-up capability and put it into D0.
  1981. */
  1982. int pci_back_from_sleep(struct pci_dev *dev)
  1983. {
  1984. pci_enable_wake(dev, PCI_D0, false);
  1985. return pci_set_power_state(dev, PCI_D0);
  1986. }
  1987. EXPORT_SYMBOL(pci_back_from_sleep);
  1988. /**
  1989. * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
  1990. * @dev: PCI device being suspended.
  1991. *
  1992. * Prepare @dev to generate wake-up events at run time and put it into a low
  1993. * power state.
  1994. */
  1995. int pci_finish_runtime_suspend(struct pci_dev *dev)
  1996. {
  1997. pci_power_t target_state;
  1998. int error;
  1999. target_state = pci_target_state(dev, device_can_wakeup(&dev->dev));
  2000. if (target_state == PCI_POWER_ERROR)
  2001. return -EIO;
  2002. dev->runtime_d3cold = target_state == PCI_D3cold;
  2003. __pci_enable_wake(dev, target_state, pci_dev_run_wake(dev));
  2004. error = pci_set_power_state(dev, target_state);
  2005. if (error) {
  2006. pci_enable_wake(dev, target_state, false);
  2007. dev->runtime_d3cold = false;
  2008. }
  2009. return error;
  2010. }
  2011. /**
  2012. * pci_dev_run_wake - Check if device can generate run-time wake-up events.
  2013. * @dev: Device to check.
  2014. *
  2015. * Return true if the device itself is capable of generating wake-up events
  2016. * (through the platform or using the native PCIe PME) or if the device supports
  2017. * PME and one of its upstream bridges can generate wake-up events.
  2018. */
  2019. bool pci_dev_run_wake(struct pci_dev *dev)
  2020. {
  2021. struct pci_bus *bus = dev->bus;
  2022. if (!dev->pme_support)
  2023. return false;
  2024. /* PME-capable in principle, but not from the target power state */
  2025. if (!pci_pme_capable(dev, pci_target_state(dev, true)))
  2026. return false;
  2027. if (device_can_wakeup(&dev->dev))
  2028. return true;
  2029. while (bus->parent) {
  2030. struct pci_dev *bridge = bus->self;
  2031. if (device_can_wakeup(&bridge->dev))
  2032. return true;
  2033. bus = bus->parent;
  2034. }
  2035. /* We have reached the root bus. */
  2036. if (bus->bridge)
  2037. return device_can_wakeup(bus->bridge);
  2038. return false;
  2039. }
  2040. EXPORT_SYMBOL_GPL(pci_dev_run_wake);
  2041. /**
  2042. * pci_dev_keep_suspended - Check if the device can stay in the suspended state.
  2043. * @pci_dev: Device to check.
  2044. *
  2045. * Return 'true' if the device is runtime-suspended, it doesn't have to be
  2046. * reconfigured due to wakeup settings difference between system and runtime
  2047. * suspend and the current power state of it is suitable for the upcoming
  2048. * (system) transition.
  2049. *
  2050. * If the device is not configured for system wakeup, disable PME for it before
  2051. * returning 'true' to prevent it from waking up the system unnecessarily.
  2052. */
  2053. bool pci_dev_keep_suspended(struct pci_dev *pci_dev)
  2054. {
  2055. struct device *dev = &pci_dev->dev;
  2056. bool wakeup = device_may_wakeup(dev);
  2057. if (!pm_runtime_suspended(dev)
  2058. || pci_target_state(pci_dev, wakeup) != pci_dev->current_state
  2059. || platform_pci_need_resume(pci_dev))
  2060. return false;
  2061. /*
  2062. * At this point the device is good to go unless it's been configured
  2063. * to generate PME at the runtime suspend time, but it is not supposed
  2064. * to wake up the system. In that case, simply disable PME for it
  2065. * (it will have to be re-enabled on exit from system resume).
  2066. *
  2067. * If the device's power state is D3cold and the platform check above
  2068. * hasn't triggered, the device's configuration is suitable and we don't
  2069. * need to manipulate it at all.
  2070. */
  2071. spin_lock_irq(&dev->power.lock);
  2072. if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold &&
  2073. !wakeup)
  2074. __pci_pme_active(pci_dev, false);
  2075. spin_unlock_irq(&dev->power.lock);
  2076. return true;
  2077. }
  2078. /**
  2079. * pci_dev_complete_resume - Finalize resume from system sleep for a device.
  2080. * @pci_dev: Device to handle.
  2081. *
  2082. * If the device is runtime suspended and wakeup-capable, enable PME for it as
  2083. * it might have been disabled during the prepare phase of system suspend if
  2084. * the device was not configured for system wakeup.
  2085. */
  2086. void pci_dev_complete_resume(struct pci_dev *pci_dev)
  2087. {
  2088. struct device *dev = &pci_dev->dev;
  2089. if (!pci_dev_run_wake(pci_dev))
  2090. return;
  2091. spin_lock_irq(&dev->power.lock);
  2092. if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold)
  2093. __pci_pme_active(pci_dev, true);
  2094. spin_unlock_irq(&dev->power.lock);
  2095. }
  2096. void pci_config_pm_runtime_get(struct pci_dev *pdev)
  2097. {
  2098. struct device *dev = &pdev->dev;
  2099. struct device *parent = dev->parent;
  2100. if (parent)
  2101. pm_runtime_get_sync(parent);
  2102. pm_runtime_get_noresume(dev);
  2103. /*
  2104. * pdev->current_state is set to PCI_D3cold during suspending,
  2105. * so wait until suspending completes
  2106. */
  2107. pm_runtime_barrier(dev);
  2108. /*
  2109. * Only need to resume devices in D3cold, because config
  2110. * registers are still accessible for devices suspended but
  2111. * not in D3cold.
  2112. */
  2113. if (pdev->current_state == PCI_D3cold)
  2114. pm_runtime_resume(dev);
  2115. }
  2116. void pci_config_pm_runtime_put(struct pci_dev *pdev)
  2117. {
  2118. struct device *dev = &pdev->dev;
  2119. struct device *parent = dev->parent;
  2120. pm_runtime_put(dev);
  2121. if (parent)
  2122. pm_runtime_put_sync(parent);
  2123. }
  2124. /**
  2125. * pci_bridge_d3_possible - Is it possible to put the bridge into D3
  2126. * @bridge: Bridge to check
  2127. *
  2128. * This function checks if it is possible to move the bridge to D3.
  2129. * Currently we only allow D3 for recent enough PCIe ports and Thunderbolt.
  2130. */
  2131. bool pci_bridge_d3_possible(struct pci_dev *bridge)
  2132. {
  2133. if (!pci_is_pcie(bridge))
  2134. return false;
  2135. switch (pci_pcie_type(bridge)) {
  2136. case PCI_EXP_TYPE_ROOT_PORT:
  2137. case PCI_EXP_TYPE_UPSTREAM:
  2138. case PCI_EXP_TYPE_DOWNSTREAM:
  2139. if (pci_bridge_d3_disable)
  2140. return false;
  2141. /*
  2142. * Hotplug ports handled by firmware in System Management Mode
  2143. * may not be put into D3 by the OS (Thunderbolt on non-Macs).
  2144. */
  2145. if (bridge->is_hotplug_bridge && !pciehp_is_native(bridge))
  2146. return false;
  2147. if (pci_bridge_d3_force)
  2148. return true;
  2149. /* Even the oldest 2010 Thunderbolt controller supports D3. */
  2150. if (bridge->is_thunderbolt)
  2151. return true;
  2152. /*
  2153. * Hotplug ports handled natively by the OS were not validated
  2154. * by vendors for runtime D3 at least until 2018 because there
  2155. * was no OS support.
  2156. */
  2157. if (bridge->is_hotplug_bridge)
  2158. return false;
  2159. /*
  2160. * It should be safe to put PCIe ports from 2015 or newer
  2161. * to D3.
  2162. */
  2163. if (dmi_get_bios_year() >= 2015)
  2164. return true;
  2165. break;
  2166. }
  2167. return false;
  2168. }
  2169. static int pci_dev_check_d3cold(struct pci_dev *dev, void *data)
  2170. {
  2171. bool *d3cold_ok = data;
  2172. if (/* The device needs to be allowed to go D3cold ... */
  2173. dev->no_d3cold || !dev->d3cold_allowed ||
  2174. /* ... and if it is wakeup capable to do so from D3cold. */
  2175. (device_may_wakeup(&dev->dev) &&
  2176. !pci_pme_capable(dev, PCI_D3cold)) ||
  2177. /* If it is a bridge it must be allowed to go to D3. */
  2178. !pci_power_manageable(dev))
  2179. *d3cold_ok = false;
  2180. return !*d3cold_ok;
  2181. }
  2182. /*
  2183. * pci_bridge_d3_update - Update bridge D3 capabilities
  2184. * @dev: PCI device which is changed
  2185. *
  2186. * Update upstream bridge PM capabilities accordingly depending on if the
  2187. * device PM configuration was changed or the device is being removed. The
  2188. * change is also propagated upstream.
  2189. */
  2190. void pci_bridge_d3_update(struct pci_dev *dev)
  2191. {
  2192. bool remove = !device_is_registered(&dev->dev);
  2193. struct pci_dev *bridge;
  2194. bool d3cold_ok = true;
  2195. bridge = pci_upstream_bridge(dev);
  2196. if (!bridge || !pci_bridge_d3_possible(bridge))
  2197. return;
  2198. /*
  2199. * If D3 is currently allowed for the bridge, removing one of its
  2200. * children won't change that.
  2201. */
  2202. if (remove && bridge->bridge_d3)
  2203. return;
  2204. /*
  2205. * If D3 is currently allowed for the bridge and a child is added or
  2206. * changed, disallowance of D3 can only be caused by that child, so
  2207. * we only need to check that single device, not any of its siblings.
  2208. *
  2209. * If D3 is currently not allowed for the bridge, checking the device
  2210. * first may allow us to skip checking its siblings.
  2211. */
  2212. if (!remove)
  2213. pci_dev_check_d3cold(dev, &d3cold_ok);
  2214. /*
  2215. * If D3 is currently not allowed for the bridge, this may be caused
  2216. * either by the device being changed/removed or any of its siblings,
  2217. * so we need to go through all children to find out if one of them
  2218. * continues to block D3.
  2219. */
  2220. if (d3cold_ok && !bridge->bridge_d3)
  2221. pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold,
  2222. &d3cold_ok);
  2223. if (bridge->bridge_d3 != d3cold_ok) {
  2224. bridge->bridge_d3 = d3cold_ok;
  2225. /* Propagate change to upstream bridges */
  2226. pci_bridge_d3_update(bridge);
  2227. }
  2228. }
  2229. /**
  2230. * pci_d3cold_enable - Enable D3cold for device
  2231. * @dev: PCI device to handle
  2232. *
  2233. * This function can be used in drivers to enable D3cold from the device
  2234. * they handle. It also updates upstream PCI bridge PM capabilities
  2235. * accordingly.
  2236. */
  2237. void pci_d3cold_enable(struct pci_dev *dev)
  2238. {
  2239. if (dev->no_d3cold) {
  2240. dev->no_d3cold = false;
  2241. pci_bridge_d3_update(dev);
  2242. }
  2243. }
  2244. EXPORT_SYMBOL_GPL(pci_d3cold_enable);
  2245. /**
  2246. * pci_d3cold_disable - Disable D3cold for device
  2247. * @dev: PCI device to handle
  2248. *
  2249. * This function can be used in drivers to disable D3cold from the device
  2250. * they handle. It also updates upstream PCI bridge PM capabilities
  2251. * accordingly.
  2252. */
  2253. void pci_d3cold_disable(struct pci_dev *dev)
  2254. {
  2255. if (!dev->no_d3cold) {
  2256. dev->no_d3cold = true;
  2257. pci_bridge_d3_update(dev);
  2258. }
  2259. }
  2260. EXPORT_SYMBOL_GPL(pci_d3cold_disable);
  2261. /**
  2262. * pci_pm_init - Initialize PM functions of given PCI device
  2263. * @dev: PCI device to handle.
  2264. */
  2265. void pci_pm_init(struct pci_dev *dev)
  2266. {
  2267. int pm;
  2268. u16 pmc;
  2269. pm_runtime_forbid(&dev->dev);
  2270. pm_runtime_set_active(&dev->dev);
  2271. pm_runtime_enable(&dev->dev);
  2272. device_enable_async_suspend(&dev->dev);
  2273. dev->wakeup_prepared = false;
  2274. dev->pm_cap = 0;
  2275. dev->pme_support = 0;
  2276. /* find PCI PM capability in list */
  2277. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  2278. if (!pm)
  2279. return;
  2280. /* Check device's ability to generate PME# */
  2281. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  2282. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  2283. pci_err(dev, "unsupported PM cap regs version (%u)\n",
  2284. pmc & PCI_PM_CAP_VER_MASK);
  2285. return;
  2286. }
  2287. dev->pm_cap = pm;
  2288. dev->d3_delay = PCI_PM_D3_WAIT;
  2289. dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
  2290. dev->bridge_d3 = pci_bridge_d3_possible(dev);
  2291. dev->d3cold_allowed = true;
  2292. dev->d1_support = false;
  2293. dev->d2_support = false;
  2294. if (!pci_no_d1d2(dev)) {
  2295. if (pmc & PCI_PM_CAP_D1)
  2296. dev->d1_support = true;
  2297. if (pmc & PCI_PM_CAP_D2)
  2298. dev->d2_support = true;
  2299. if (dev->d1_support || dev->d2_support)
  2300. pci_printk(KERN_DEBUG, dev, "supports%s%s\n",
  2301. dev->d1_support ? " D1" : "",
  2302. dev->d2_support ? " D2" : "");
  2303. }
  2304. pmc &= PCI_PM_CAP_PME_MASK;
  2305. if (pmc) {
  2306. pci_printk(KERN_DEBUG, dev, "PME# supported from%s%s%s%s%s\n",
  2307. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  2308. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  2309. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  2310. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  2311. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  2312. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  2313. dev->pme_poll = true;
  2314. /*
  2315. * Make device's PM flags reflect the wake-up capability, but
  2316. * let the user space enable it to wake up the system as needed.
  2317. */
  2318. device_set_wakeup_capable(&dev->dev, true);
  2319. /* Disable the PME# generation functionality */
  2320. pci_pme_active(dev, false);
  2321. }
  2322. }
  2323. static unsigned long pci_ea_flags(struct pci_dev *dev, u8 prop)
  2324. {
  2325. unsigned long flags = IORESOURCE_PCI_FIXED | IORESOURCE_PCI_EA_BEI;
  2326. switch (prop) {
  2327. case PCI_EA_P_MEM:
  2328. case PCI_EA_P_VF_MEM:
  2329. flags |= IORESOURCE_MEM;
  2330. break;
  2331. case PCI_EA_P_MEM_PREFETCH:
  2332. case PCI_EA_P_VF_MEM_PREFETCH:
  2333. flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  2334. break;
  2335. case PCI_EA_P_IO:
  2336. flags |= IORESOURCE_IO;
  2337. break;
  2338. default:
  2339. return 0;
  2340. }
  2341. return flags;
  2342. }
  2343. static struct resource *pci_ea_get_resource(struct pci_dev *dev, u8 bei,
  2344. u8 prop)
  2345. {
  2346. if (bei <= PCI_EA_BEI_BAR5 && prop <= PCI_EA_P_IO)
  2347. return &dev->resource[bei];
  2348. #ifdef CONFIG_PCI_IOV
  2349. else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5 &&
  2350. (prop == PCI_EA_P_VF_MEM || prop == PCI_EA_P_VF_MEM_PREFETCH))
  2351. return &dev->resource[PCI_IOV_RESOURCES +
  2352. bei - PCI_EA_BEI_VF_BAR0];
  2353. #endif
  2354. else if (bei == PCI_EA_BEI_ROM)
  2355. return &dev->resource[PCI_ROM_RESOURCE];
  2356. else
  2357. return NULL;
  2358. }
  2359. /* Read an Enhanced Allocation (EA) entry */
  2360. static int pci_ea_read(struct pci_dev *dev, int offset)
  2361. {
  2362. struct resource *res;
  2363. int ent_size, ent_offset = offset;
  2364. resource_size_t start, end;
  2365. unsigned long flags;
  2366. u32 dw0, bei, base, max_offset;
  2367. u8 prop;
  2368. bool support_64 = (sizeof(resource_size_t) >= 8);
  2369. pci_read_config_dword(dev, ent_offset, &dw0);
  2370. ent_offset += 4;
  2371. /* Entry size field indicates DWORDs after 1st */
  2372. ent_size = ((dw0 & PCI_EA_ES) + 1) << 2;
  2373. if (!(dw0 & PCI_EA_ENABLE)) /* Entry not enabled */
  2374. goto out;
  2375. bei = (dw0 & PCI_EA_BEI) >> 4;
  2376. prop = (dw0 & PCI_EA_PP) >> 8;
  2377. /*
  2378. * If the Property is in the reserved range, try the Secondary
  2379. * Property instead.
  2380. */
  2381. if (prop > PCI_EA_P_BRIDGE_IO && prop < PCI_EA_P_MEM_RESERVED)
  2382. prop = (dw0 & PCI_EA_SP) >> 16;
  2383. if (prop > PCI_EA_P_BRIDGE_IO)
  2384. goto out;
  2385. res = pci_ea_get_resource(dev, bei, prop);
  2386. if (!res) {
  2387. pci_err(dev, "Unsupported EA entry BEI: %u\n", bei);
  2388. goto out;
  2389. }
  2390. flags = pci_ea_flags(dev, prop);
  2391. if (!flags) {
  2392. pci_err(dev, "Unsupported EA properties: %#x\n", prop);
  2393. goto out;
  2394. }
  2395. /* Read Base */
  2396. pci_read_config_dword(dev, ent_offset, &base);
  2397. start = (base & PCI_EA_FIELD_MASK);
  2398. ent_offset += 4;
  2399. /* Read MaxOffset */
  2400. pci_read_config_dword(dev, ent_offset, &max_offset);
  2401. ent_offset += 4;
  2402. /* Read Base MSBs (if 64-bit entry) */
  2403. if (base & PCI_EA_IS_64) {
  2404. u32 base_upper;
  2405. pci_read_config_dword(dev, ent_offset, &base_upper);
  2406. ent_offset += 4;
  2407. flags |= IORESOURCE_MEM_64;
  2408. /* entry starts above 32-bit boundary, can't use */
  2409. if (!support_64 && base_upper)
  2410. goto out;
  2411. if (support_64)
  2412. start |= ((u64)base_upper << 32);
  2413. }
  2414. end = start + (max_offset | 0x03);
  2415. /* Read MaxOffset MSBs (if 64-bit entry) */
  2416. if (max_offset & PCI_EA_IS_64) {
  2417. u32 max_offset_upper;
  2418. pci_read_config_dword(dev, ent_offset, &max_offset_upper);
  2419. ent_offset += 4;
  2420. flags |= IORESOURCE_MEM_64;
  2421. /* entry too big, can't use */
  2422. if (!support_64 && max_offset_upper)
  2423. goto out;
  2424. if (support_64)
  2425. end += ((u64)max_offset_upper << 32);
  2426. }
  2427. if (end < start) {
  2428. pci_err(dev, "EA Entry crosses address boundary\n");
  2429. goto out;
  2430. }
  2431. if (ent_size != ent_offset - offset) {
  2432. pci_err(dev, "EA Entry Size (%d) does not match length read (%d)\n",
  2433. ent_size, ent_offset - offset);
  2434. goto out;
  2435. }
  2436. res->name = pci_name(dev);
  2437. res->start = start;
  2438. res->end = end;
  2439. res->flags = flags;
  2440. if (bei <= PCI_EA_BEI_BAR5)
  2441. pci_printk(KERN_DEBUG, dev, "BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
  2442. bei, res, prop);
  2443. else if (bei == PCI_EA_BEI_ROM)
  2444. pci_printk(KERN_DEBUG, dev, "ROM: %pR (from Enhanced Allocation, properties %#02x)\n",
  2445. res, prop);
  2446. else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5)
  2447. pci_printk(KERN_DEBUG, dev, "VF BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
  2448. bei - PCI_EA_BEI_VF_BAR0, res, prop);
  2449. else
  2450. pci_printk(KERN_DEBUG, dev, "BEI %d res: %pR (from Enhanced Allocation, properties %#02x)\n",
  2451. bei, res, prop);
  2452. out:
  2453. return offset + ent_size;
  2454. }
  2455. /* Enhanced Allocation Initialization */
  2456. void pci_ea_init(struct pci_dev *dev)
  2457. {
  2458. int ea;
  2459. u8 num_ent;
  2460. int offset;
  2461. int i;
  2462. /* find PCI EA capability in list */
  2463. ea = pci_find_capability(dev, PCI_CAP_ID_EA);
  2464. if (!ea)
  2465. return;
  2466. /* determine the number of entries */
  2467. pci_bus_read_config_byte(dev->bus, dev->devfn, ea + PCI_EA_NUM_ENT,
  2468. &num_ent);
  2469. num_ent &= PCI_EA_NUM_ENT_MASK;
  2470. offset = ea + PCI_EA_FIRST_ENT;
  2471. /* Skip DWORD 2 for type 1 functions */
  2472. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  2473. offset += 4;
  2474. /* parse each EA entry */
  2475. for (i = 0; i < num_ent; ++i)
  2476. offset = pci_ea_read(dev, offset);
  2477. }
  2478. static void pci_add_saved_cap(struct pci_dev *pci_dev,
  2479. struct pci_cap_saved_state *new_cap)
  2480. {
  2481. hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
  2482. }
  2483. /**
  2484. * _pci_add_cap_save_buffer - allocate buffer for saving given
  2485. * capability registers
  2486. * @dev: the PCI device
  2487. * @cap: the capability to allocate the buffer for
  2488. * @extended: Standard or Extended capability ID
  2489. * @size: requested size of the buffer
  2490. */
  2491. static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
  2492. bool extended, unsigned int size)
  2493. {
  2494. int pos;
  2495. struct pci_cap_saved_state *save_state;
  2496. if (extended)
  2497. pos = pci_find_ext_capability(dev, cap);
  2498. else
  2499. pos = pci_find_capability(dev, cap);
  2500. if (!pos)
  2501. return 0;
  2502. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  2503. if (!save_state)
  2504. return -ENOMEM;
  2505. save_state->cap.cap_nr = cap;
  2506. save_state->cap.cap_extended = extended;
  2507. save_state->cap.size = size;
  2508. pci_add_saved_cap(dev, save_state);
  2509. return 0;
  2510. }
  2511. int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
  2512. {
  2513. return _pci_add_cap_save_buffer(dev, cap, false, size);
  2514. }
  2515. int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
  2516. {
  2517. return _pci_add_cap_save_buffer(dev, cap, true, size);
  2518. }
  2519. /**
  2520. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  2521. * @dev: the PCI device
  2522. */
  2523. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  2524. {
  2525. int error;
  2526. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
  2527. PCI_EXP_SAVE_REGS * sizeof(u16));
  2528. if (error)
  2529. pci_err(dev, "unable to preallocate PCI Express save buffer\n");
  2530. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  2531. if (error)
  2532. pci_err(dev, "unable to preallocate PCI-X save buffer\n");
  2533. pci_allocate_vc_save_buffers(dev);
  2534. }
  2535. void pci_free_cap_save_buffers(struct pci_dev *dev)
  2536. {
  2537. struct pci_cap_saved_state *tmp;
  2538. struct hlist_node *n;
  2539. hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
  2540. kfree(tmp);
  2541. }
  2542. /**
  2543. * pci_configure_ari - enable or disable ARI forwarding
  2544. * @dev: the PCI device
  2545. *
  2546. * If @dev and its upstream bridge both support ARI, enable ARI in the
  2547. * bridge. Otherwise, disable ARI in the bridge.
  2548. */
  2549. void pci_configure_ari(struct pci_dev *dev)
  2550. {
  2551. u32 cap;
  2552. struct pci_dev *bridge;
  2553. if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
  2554. return;
  2555. bridge = dev->bus->self;
  2556. if (!bridge)
  2557. return;
  2558. pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
  2559. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  2560. return;
  2561. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
  2562. pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
  2563. PCI_EXP_DEVCTL2_ARI);
  2564. bridge->ari_enabled = 1;
  2565. } else {
  2566. pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
  2567. PCI_EXP_DEVCTL2_ARI);
  2568. bridge->ari_enabled = 0;
  2569. }
  2570. }
  2571. static int pci_acs_enable;
  2572. /**
  2573. * pci_request_acs - ask for ACS to be enabled if supported
  2574. */
  2575. void pci_request_acs(void)
  2576. {
  2577. pci_acs_enable = 1;
  2578. }
  2579. static const char *disable_acs_redir_param;
  2580. /**
  2581. * pci_disable_acs_redir - disable ACS redirect capabilities
  2582. * @dev: the PCI device
  2583. *
  2584. * For only devices specified in the disable_acs_redir parameter.
  2585. */
  2586. static void pci_disable_acs_redir(struct pci_dev *dev)
  2587. {
  2588. int ret = 0;
  2589. const char *p;
  2590. int pos;
  2591. u16 ctrl;
  2592. if (!disable_acs_redir_param)
  2593. return;
  2594. p = disable_acs_redir_param;
  2595. while (*p) {
  2596. ret = pci_dev_str_match(dev, p, &p);
  2597. if (ret < 0) {
  2598. pr_info_once("PCI: Can't parse disable_acs_redir parameter: %s\n",
  2599. disable_acs_redir_param);
  2600. break;
  2601. } else if (ret == 1) {
  2602. /* Found a match */
  2603. break;
  2604. }
  2605. if (*p != ';' && *p != ',') {
  2606. /* End of param or invalid format */
  2607. break;
  2608. }
  2609. p++;
  2610. }
  2611. if (ret != 1)
  2612. return;
  2613. if (!pci_dev_specific_disable_acs_redir(dev))
  2614. return;
  2615. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  2616. if (!pos) {
  2617. pci_warn(dev, "cannot disable ACS redirect for this hardware as it does not have ACS capabilities\n");
  2618. return;
  2619. }
  2620. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  2621. /* P2P Request & Completion Redirect */
  2622. ctrl &= ~(PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_EC);
  2623. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  2624. pci_info(dev, "disabled ACS redirect\n");
  2625. }
  2626. /**
  2627. * pci_std_enable_acs - enable ACS on devices using standard ACS capabilites
  2628. * @dev: the PCI device
  2629. */
  2630. static void pci_std_enable_acs(struct pci_dev *dev)
  2631. {
  2632. int pos;
  2633. u16 cap;
  2634. u16 ctrl;
  2635. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  2636. if (!pos)
  2637. return;
  2638. pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
  2639. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  2640. /* Source Validation */
  2641. ctrl |= (cap & PCI_ACS_SV);
  2642. /* P2P Request Redirect */
  2643. ctrl |= (cap & PCI_ACS_RR);
  2644. /* P2P Completion Redirect */
  2645. ctrl |= (cap & PCI_ACS_CR);
  2646. /* Upstream Forwarding */
  2647. ctrl |= (cap & PCI_ACS_UF);
  2648. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  2649. }
  2650. /**
  2651. * pci_enable_acs - enable ACS if hardware support it
  2652. * @dev: the PCI device
  2653. */
  2654. void pci_enable_acs(struct pci_dev *dev)
  2655. {
  2656. if (!pci_acs_enable)
  2657. goto disable_acs_redir;
  2658. if (!pci_dev_specific_enable_acs(dev))
  2659. goto disable_acs_redir;
  2660. pci_std_enable_acs(dev);
  2661. disable_acs_redir:
  2662. /*
  2663. * Note: pci_disable_acs_redir() must be called even if ACS was not
  2664. * enabled by the kernel because it may have been enabled by
  2665. * platform firmware. So if we are told to disable it, we should
  2666. * always disable it after setting the kernel's default
  2667. * preferences.
  2668. */
  2669. pci_disable_acs_redir(dev);
  2670. }
  2671. static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
  2672. {
  2673. int pos;
  2674. u16 cap, ctrl;
  2675. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
  2676. if (!pos)
  2677. return false;
  2678. /*
  2679. * Except for egress control, capabilities are either required
  2680. * or only required if controllable. Features missing from the
  2681. * capability field can therefore be assumed as hard-wired enabled.
  2682. */
  2683. pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
  2684. acs_flags &= (cap | PCI_ACS_EC);
  2685. pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
  2686. return (ctrl & acs_flags) == acs_flags;
  2687. }
  2688. /**
  2689. * pci_acs_enabled - test ACS against required flags for a given device
  2690. * @pdev: device to test
  2691. * @acs_flags: required PCI ACS flags
  2692. *
  2693. * Return true if the device supports the provided flags. Automatically
  2694. * filters out flags that are not implemented on multifunction devices.
  2695. *
  2696. * Note that this interface checks the effective ACS capabilities of the
  2697. * device rather than the actual capabilities. For instance, most single
  2698. * function endpoints are not required to support ACS because they have no
  2699. * opportunity for peer-to-peer access. We therefore return 'true'
  2700. * regardless of whether the device exposes an ACS capability. This makes
  2701. * it much easier for callers of this function to ignore the actual type
  2702. * or topology of the device when testing ACS support.
  2703. */
  2704. bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
  2705. {
  2706. int ret;
  2707. ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
  2708. if (ret >= 0)
  2709. return ret > 0;
  2710. /*
  2711. * Conventional PCI and PCI-X devices never support ACS, either
  2712. * effectively or actually. The shared bus topology implies that
  2713. * any device on the bus can receive or snoop DMA.
  2714. */
  2715. if (!pci_is_pcie(pdev))
  2716. return false;
  2717. switch (pci_pcie_type(pdev)) {
  2718. /*
  2719. * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
  2720. * but since their primary interface is PCI/X, we conservatively
  2721. * handle them as we would a non-PCIe device.
  2722. */
  2723. case PCI_EXP_TYPE_PCIE_BRIDGE:
  2724. /*
  2725. * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
  2726. * applicable... must never implement an ACS Extended Capability...".
  2727. * This seems arbitrary, but we take a conservative interpretation
  2728. * of this statement.
  2729. */
  2730. case PCI_EXP_TYPE_PCI_BRIDGE:
  2731. case PCI_EXP_TYPE_RC_EC:
  2732. return false;
  2733. /*
  2734. * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
  2735. * implement ACS in order to indicate their peer-to-peer capabilities,
  2736. * regardless of whether they are single- or multi-function devices.
  2737. */
  2738. case PCI_EXP_TYPE_DOWNSTREAM:
  2739. case PCI_EXP_TYPE_ROOT_PORT:
  2740. return pci_acs_flags_enabled(pdev, acs_flags);
  2741. /*
  2742. * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
  2743. * implemented by the remaining PCIe types to indicate peer-to-peer
  2744. * capabilities, but only when they are part of a multifunction
  2745. * device. The footnote for section 6.12 indicates the specific
  2746. * PCIe types included here.
  2747. */
  2748. case PCI_EXP_TYPE_ENDPOINT:
  2749. case PCI_EXP_TYPE_UPSTREAM:
  2750. case PCI_EXP_TYPE_LEG_END:
  2751. case PCI_EXP_TYPE_RC_END:
  2752. if (!pdev->multifunction)
  2753. break;
  2754. return pci_acs_flags_enabled(pdev, acs_flags);
  2755. }
  2756. /*
  2757. * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable
  2758. * to single function devices with the exception of downstream ports.
  2759. */
  2760. return true;
  2761. }
  2762. /**
  2763. * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
  2764. * @start: starting downstream device
  2765. * @end: ending upstream device or NULL to search to the root bus
  2766. * @acs_flags: required flags
  2767. *
  2768. * Walk up a device tree from start to end testing PCI ACS support. If
  2769. * any step along the way does not support the required flags, return false.
  2770. */
  2771. bool pci_acs_path_enabled(struct pci_dev *start,
  2772. struct pci_dev *end, u16 acs_flags)
  2773. {
  2774. struct pci_dev *pdev, *parent = start;
  2775. do {
  2776. pdev = parent;
  2777. if (!pci_acs_enabled(pdev, acs_flags))
  2778. return false;
  2779. if (pci_is_root_bus(pdev->bus))
  2780. return (end == NULL);
  2781. parent = pdev->bus->self;
  2782. } while (pdev != end);
  2783. return true;
  2784. }
  2785. /**
  2786. * pci_rebar_find_pos - find position of resize ctrl reg for BAR
  2787. * @pdev: PCI device
  2788. * @bar: BAR to find
  2789. *
  2790. * Helper to find the position of the ctrl register for a BAR.
  2791. * Returns -ENOTSUPP if resizable BARs are not supported at all.
  2792. * Returns -ENOENT if no ctrl register for the BAR could be found.
  2793. */
  2794. static int pci_rebar_find_pos(struct pci_dev *pdev, int bar)
  2795. {
  2796. unsigned int pos, nbars, i;
  2797. u32 ctrl;
  2798. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
  2799. if (!pos)
  2800. return -ENOTSUPP;
  2801. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2802. nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
  2803. PCI_REBAR_CTRL_NBAR_SHIFT;
  2804. for (i = 0; i < nbars; i++, pos += 8) {
  2805. int bar_idx;
  2806. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2807. bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
  2808. if (bar_idx == bar)
  2809. return pos;
  2810. }
  2811. return -ENOENT;
  2812. }
  2813. /**
  2814. * pci_rebar_get_possible_sizes - get possible sizes for BAR
  2815. * @pdev: PCI device
  2816. * @bar: BAR to query
  2817. *
  2818. * Get the possible sizes of a resizable BAR as bitmask defined in the spec
  2819. * (bit 0=1MB, bit 19=512GB). Returns 0 if BAR isn't resizable.
  2820. */
  2821. u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar)
  2822. {
  2823. int pos;
  2824. u32 cap;
  2825. pos = pci_rebar_find_pos(pdev, bar);
  2826. if (pos < 0)
  2827. return 0;
  2828. pci_read_config_dword(pdev, pos + PCI_REBAR_CAP, &cap);
  2829. return (cap & PCI_REBAR_CAP_SIZES) >> 4;
  2830. }
  2831. /**
  2832. * pci_rebar_get_current_size - get the current size of a BAR
  2833. * @pdev: PCI device
  2834. * @bar: BAR to set size to
  2835. *
  2836. * Read the size of a BAR from the resizable BAR config.
  2837. * Returns size if found or negative error code.
  2838. */
  2839. int pci_rebar_get_current_size(struct pci_dev *pdev, int bar)
  2840. {
  2841. int pos;
  2842. u32 ctrl;
  2843. pos = pci_rebar_find_pos(pdev, bar);
  2844. if (pos < 0)
  2845. return pos;
  2846. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2847. return (ctrl & PCI_REBAR_CTRL_BAR_SIZE) >> PCI_REBAR_CTRL_BAR_SHIFT;
  2848. }
  2849. /**
  2850. * pci_rebar_set_size - set a new size for a BAR
  2851. * @pdev: PCI device
  2852. * @bar: BAR to set size to
  2853. * @size: new size as defined in the spec (0=1MB, 19=512GB)
  2854. *
  2855. * Set the new size of a BAR as defined in the spec.
  2856. * Returns zero if resizing was successful, error code otherwise.
  2857. */
  2858. int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size)
  2859. {
  2860. int pos;
  2861. u32 ctrl;
  2862. pos = pci_rebar_find_pos(pdev, bar);
  2863. if (pos < 0)
  2864. return pos;
  2865. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2866. ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
  2867. ctrl |= size << PCI_REBAR_CTRL_BAR_SHIFT;
  2868. pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
  2869. return 0;
  2870. }
  2871. /**
  2872. * pci_enable_atomic_ops_to_root - enable AtomicOp requests to root port
  2873. * @dev: the PCI device
  2874. * @cap_mask: mask of desired AtomicOp sizes, including one or more of:
  2875. * PCI_EXP_DEVCAP2_ATOMIC_COMP32
  2876. * PCI_EXP_DEVCAP2_ATOMIC_COMP64
  2877. * PCI_EXP_DEVCAP2_ATOMIC_COMP128
  2878. *
  2879. * Return 0 if all upstream bridges support AtomicOp routing, egress
  2880. * blocking is disabled on all upstream ports, and the root port supports
  2881. * the requested completion capabilities (32-bit, 64-bit and/or 128-bit
  2882. * AtomicOp completion), or negative otherwise.
  2883. */
  2884. int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask)
  2885. {
  2886. struct pci_bus *bus = dev->bus;
  2887. struct pci_dev *bridge;
  2888. u32 cap, ctl2;
  2889. if (!pci_is_pcie(dev))
  2890. return -EINVAL;
  2891. /*
  2892. * Per PCIe r4.0, sec 6.15, endpoints and root ports may be
  2893. * AtomicOp requesters. For now, we only support endpoints as
  2894. * requesters and root ports as completers. No endpoints as
  2895. * completers, and no peer-to-peer.
  2896. */
  2897. switch (pci_pcie_type(dev)) {
  2898. case PCI_EXP_TYPE_ENDPOINT:
  2899. case PCI_EXP_TYPE_LEG_END:
  2900. case PCI_EXP_TYPE_RC_END:
  2901. break;
  2902. default:
  2903. return -EINVAL;
  2904. }
  2905. while (bus->parent) {
  2906. bridge = bus->self;
  2907. pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
  2908. switch (pci_pcie_type(bridge)) {
  2909. /* Ensure switch ports support AtomicOp routing */
  2910. case PCI_EXP_TYPE_UPSTREAM:
  2911. case PCI_EXP_TYPE_DOWNSTREAM:
  2912. if (!(cap & PCI_EXP_DEVCAP2_ATOMIC_ROUTE))
  2913. return -EINVAL;
  2914. break;
  2915. /* Ensure root port supports all the sizes we care about */
  2916. case PCI_EXP_TYPE_ROOT_PORT:
  2917. if ((cap & cap_mask) != cap_mask)
  2918. return -EINVAL;
  2919. break;
  2920. }
  2921. /* Ensure upstream ports don't block AtomicOps on egress */
  2922. if (!bridge->has_secondary_link) {
  2923. pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2,
  2924. &ctl2);
  2925. if (ctl2 & PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK)
  2926. return -EINVAL;
  2927. }
  2928. bus = bus->parent;
  2929. }
  2930. pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
  2931. PCI_EXP_DEVCTL2_ATOMIC_REQ);
  2932. return 0;
  2933. }
  2934. EXPORT_SYMBOL(pci_enable_atomic_ops_to_root);
  2935. /**
  2936. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  2937. * @dev: the PCI device
  2938. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
  2939. *
  2940. * Perform INTx swizzling for a device behind one level of bridge. This is
  2941. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  2942. * behind bridges on add-in cards. For devices with ARI enabled, the slot
  2943. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  2944. * the PCI Express Base Specification, Revision 2.1)
  2945. */
  2946. u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
  2947. {
  2948. int slot;
  2949. if (pci_ari_enabled(dev->bus))
  2950. slot = 0;
  2951. else
  2952. slot = PCI_SLOT(dev->devfn);
  2953. return (((pin - 1) + slot) % 4) + 1;
  2954. }
  2955. int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  2956. {
  2957. u8 pin;
  2958. pin = dev->pin;
  2959. if (!pin)
  2960. return -1;
  2961. while (!pci_is_root_bus(dev->bus)) {
  2962. pin = pci_swizzle_interrupt_pin(dev, pin);
  2963. dev = dev->bus->self;
  2964. }
  2965. *bridge = dev;
  2966. return pin;
  2967. }
  2968. /**
  2969. * pci_common_swizzle - swizzle INTx all the way to root bridge
  2970. * @dev: the PCI device
  2971. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  2972. *
  2973. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  2974. * bridges all the way up to a PCI root bus.
  2975. */
  2976. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  2977. {
  2978. u8 pin = *pinp;
  2979. while (!pci_is_root_bus(dev->bus)) {
  2980. pin = pci_swizzle_interrupt_pin(dev, pin);
  2981. dev = dev->bus->self;
  2982. }
  2983. *pinp = pin;
  2984. return PCI_SLOT(dev->devfn);
  2985. }
  2986. EXPORT_SYMBOL_GPL(pci_common_swizzle);
  2987. /**
  2988. * pci_release_region - Release a PCI bar
  2989. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  2990. * @bar: BAR to release
  2991. *
  2992. * Releases the PCI I/O and memory resources previously reserved by a
  2993. * successful call to pci_request_region. Call this function only
  2994. * after all use of the PCI regions has ceased.
  2995. */
  2996. void pci_release_region(struct pci_dev *pdev, int bar)
  2997. {
  2998. struct pci_devres *dr;
  2999. if (pci_resource_len(pdev, bar) == 0)
  3000. return;
  3001. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  3002. release_region(pci_resource_start(pdev, bar),
  3003. pci_resource_len(pdev, bar));
  3004. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  3005. release_mem_region(pci_resource_start(pdev, bar),
  3006. pci_resource_len(pdev, bar));
  3007. dr = find_pci_dr(pdev);
  3008. if (dr)
  3009. dr->region_mask &= ~(1 << bar);
  3010. }
  3011. EXPORT_SYMBOL(pci_release_region);
  3012. /**
  3013. * __pci_request_region - Reserved PCI I/O and memory resource
  3014. * @pdev: PCI device whose resources are to be reserved
  3015. * @bar: BAR to be reserved
  3016. * @res_name: Name to be associated with resource.
  3017. * @exclusive: whether the region access is exclusive or not
  3018. *
  3019. * Mark the PCI region associated with PCI device @pdev BR @bar as
  3020. * being reserved by owner @res_name. Do not access any
  3021. * address inside the PCI regions unless this call returns
  3022. * successfully.
  3023. *
  3024. * If @exclusive is set, then the region is marked so that userspace
  3025. * is explicitly not allowed to map the resource via /dev/mem or
  3026. * sysfs MMIO access.
  3027. *
  3028. * Returns 0 on success, or %EBUSY on error. A warning
  3029. * message is also printed on failure.
  3030. */
  3031. static int __pci_request_region(struct pci_dev *pdev, int bar,
  3032. const char *res_name, int exclusive)
  3033. {
  3034. struct pci_devres *dr;
  3035. if (pci_resource_len(pdev, bar) == 0)
  3036. return 0;
  3037. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  3038. if (!request_region(pci_resource_start(pdev, bar),
  3039. pci_resource_len(pdev, bar), res_name))
  3040. goto err_out;
  3041. } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  3042. if (!__request_mem_region(pci_resource_start(pdev, bar),
  3043. pci_resource_len(pdev, bar), res_name,
  3044. exclusive))
  3045. goto err_out;
  3046. }
  3047. dr = find_pci_dr(pdev);
  3048. if (dr)
  3049. dr->region_mask |= 1 << bar;
  3050. return 0;
  3051. err_out:
  3052. pci_warn(pdev, "BAR %d: can't reserve %pR\n", bar,
  3053. &pdev->resource[bar]);
  3054. return -EBUSY;
  3055. }
  3056. /**
  3057. * pci_request_region - Reserve PCI I/O and memory resource
  3058. * @pdev: PCI device whose resources are to be reserved
  3059. * @bar: BAR to be reserved
  3060. * @res_name: Name to be associated with resource
  3061. *
  3062. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  3063. * being reserved by owner @res_name. Do not access any
  3064. * address inside the PCI regions unless this call returns
  3065. * successfully.
  3066. *
  3067. * Returns 0 on success, or %EBUSY on error. A warning
  3068. * message is also printed on failure.
  3069. */
  3070. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  3071. {
  3072. return __pci_request_region(pdev, bar, res_name, 0);
  3073. }
  3074. EXPORT_SYMBOL(pci_request_region);
  3075. /**
  3076. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  3077. * @pdev: PCI device whose resources are to be reserved
  3078. * @bar: BAR to be reserved
  3079. * @res_name: Name to be associated with resource.
  3080. *
  3081. * Mark the PCI region associated with PCI device @pdev BR @bar as
  3082. * being reserved by owner @res_name. Do not access any
  3083. * address inside the PCI regions unless this call returns
  3084. * successfully.
  3085. *
  3086. * Returns 0 on success, or %EBUSY on error. A warning
  3087. * message is also printed on failure.
  3088. *
  3089. * The key difference that _exclusive makes it that userspace is
  3090. * explicitly not allowed to map the resource via /dev/mem or
  3091. * sysfs.
  3092. */
  3093. int pci_request_region_exclusive(struct pci_dev *pdev, int bar,
  3094. const char *res_name)
  3095. {
  3096. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  3097. }
  3098. EXPORT_SYMBOL(pci_request_region_exclusive);
  3099. /**
  3100. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  3101. * @pdev: PCI device whose resources were previously reserved
  3102. * @bars: Bitmask of BARs to be released
  3103. *
  3104. * Release selected PCI I/O and memory resources previously reserved.
  3105. * Call this function only after all use of the PCI regions has ceased.
  3106. */
  3107. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  3108. {
  3109. int i;
  3110. for (i = 0; i < 6; i++)
  3111. if (bars & (1 << i))
  3112. pci_release_region(pdev, i);
  3113. }
  3114. EXPORT_SYMBOL(pci_release_selected_regions);
  3115. static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  3116. const char *res_name, int excl)
  3117. {
  3118. int i;
  3119. for (i = 0; i < 6; i++)
  3120. if (bars & (1 << i))
  3121. if (__pci_request_region(pdev, i, res_name, excl))
  3122. goto err_out;
  3123. return 0;
  3124. err_out:
  3125. while (--i >= 0)
  3126. if (bars & (1 << i))
  3127. pci_release_region(pdev, i);
  3128. return -EBUSY;
  3129. }
  3130. /**
  3131. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  3132. * @pdev: PCI device whose resources are to be reserved
  3133. * @bars: Bitmask of BARs to be requested
  3134. * @res_name: Name to be associated with resource
  3135. */
  3136. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  3137. const char *res_name)
  3138. {
  3139. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  3140. }
  3141. EXPORT_SYMBOL(pci_request_selected_regions);
  3142. int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars,
  3143. const char *res_name)
  3144. {
  3145. return __pci_request_selected_regions(pdev, bars, res_name,
  3146. IORESOURCE_EXCLUSIVE);
  3147. }
  3148. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  3149. /**
  3150. * pci_release_regions - Release reserved PCI I/O and memory resources
  3151. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  3152. *
  3153. * Releases all PCI I/O and memory resources previously reserved by a
  3154. * successful call to pci_request_regions. Call this function only
  3155. * after all use of the PCI regions has ceased.
  3156. */
  3157. void pci_release_regions(struct pci_dev *pdev)
  3158. {
  3159. pci_release_selected_regions(pdev, (1 << 6) - 1);
  3160. }
  3161. EXPORT_SYMBOL(pci_release_regions);
  3162. /**
  3163. * pci_request_regions - Reserved PCI I/O and memory resources
  3164. * @pdev: PCI device whose resources are to be reserved
  3165. * @res_name: Name to be associated with resource.
  3166. *
  3167. * Mark all PCI regions associated with PCI device @pdev as
  3168. * being reserved by owner @res_name. Do not access any
  3169. * address inside the PCI regions unless this call returns
  3170. * successfully.
  3171. *
  3172. * Returns 0 on success, or %EBUSY on error. A warning
  3173. * message is also printed on failure.
  3174. */
  3175. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  3176. {
  3177. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  3178. }
  3179. EXPORT_SYMBOL(pci_request_regions);
  3180. /**
  3181. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  3182. * @pdev: PCI device whose resources are to be reserved
  3183. * @res_name: Name to be associated with resource.
  3184. *
  3185. * Mark all PCI regions associated with PCI device @pdev as
  3186. * being reserved by owner @res_name. Do not access any
  3187. * address inside the PCI regions unless this call returns
  3188. * successfully.
  3189. *
  3190. * pci_request_regions_exclusive() will mark the region so that
  3191. * /dev/mem and the sysfs MMIO access will not be allowed.
  3192. *
  3193. * Returns 0 on success, or %EBUSY on error. A warning
  3194. * message is also printed on failure.
  3195. */
  3196. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  3197. {
  3198. return pci_request_selected_regions_exclusive(pdev,
  3199. ((1 << 6) - 1), res_name);
  3200. }
  3201. EXPORT_SYMBOL(pci_request_regions_exclusive);
  3202. /*
  3203. * Record the PCI IO range (expressed as CPU physical address + size).
  3204. * Return a negative value if an error has occured, zero otherwise
  3205. */
  3206. int pci_register_io_range(struct fwnode_handle *fwnode, phys_addr_t addr,
  3207. resource_size_t size)
  3208. {
  3209. int ret = 0;
  3210. #ifdef PCI_IOBASE
  3211. struct logic_pio_hwaddr *range;
  3212. if (!size || addr + size < addr)
  3213. return -EINVAL;
  3214. range = kzalloc(sizeof(*range), GFP_ATOMIC);
  3215. if (!range)
  3216. return -ENOMEM;
  3217. range->fwnode = fwnode;
  3218. range->size = size;
  3219. range->hw_start = addr;
  3220. range->flags = LOGIC_PIO_CPU_MMIO;
  3221. ret = logic_pio_register_range(range);
  3222. if (ret)
  3223. kfree(range);
  3224. #endif
  3225. return ret;
  3226. }
  3227. phys_addr_t pci_pio_to_address(unsigned long pio)
  3228. {
  3229. phys_addr_t address = (phys_addr_t)OF_BAD_ADDR;
  3230. #ifdef PCI_IOBASE
  3231. if (pio >= MMIO_UPPER_LIMIT)
  3232. return address;
  3233. address = logic_pio_to_hwaddr(pio);
  3234. #endif
  3235. return address;
  3236. }
  3237. unsigned long __weak pci_address_to_pio(phys_addr_t address)
  3238. {
  3239. #ifdef PCI_IOBASE
  3240. return logic_pio_trans_cpuaddr(address);
  3241. #else
  3242. if (address > IO_SPACE_LIMIT)
  3243. return (unsigned long)-1;
  3244. return (unsigned long) address;
  3245. #endif
  3246. }
  3247. /**
  3248. * pci_remap_iospace - Remap the memory mapped I/O space
  3249. * @res: Resource describing the I/O space
  3250. * @phys_addr: physical address of range to be mapped
  3251. *
  3252. * Remap the memory mapped I/O space described by the @res
  3253. * and the CPU physical address @phys_addr into virtual address space.
  3254. * Only architectures that have memory mapped IO functions defined
  3255. * (and the PCI_IOBASE value defined) should call this function.
  3256. */
  3257. int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
  3258. {
  3259. #if defined(PCI_IOBASE) && defined(CONFIG_MMU)
  3260. unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
  3261. if (!(res->flags & IORESOURCE_IO))
  3262. return -EINVAL;
  3263. if (res->end > IO_SPACE_LIMIT)
  3264. return -EINVAL;
  3265. return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr,
  3266. pgprot_device(PAGE_KERNEL));
  3267. #else
  3268. /* this architecture does not have memory mapped I/O space,
  3269. so this function should never be called */
  3270. WARN_ONCE(1, "This architecture does not support memory mapped I/O\n");
  3271. return -ENODEV;
  3272. #endif
  3273. }
  3274. EXPORT_SYMBOL(pci_remap_iospace);
  3275. /**
  3276. * pci_unmap_iospace - Unmap the memory mapped I/O space
  3277. * @res: resource to be unmapped
  3278. *
  3279. * Unmap the CPU virtual address @res from virtual address space.
  3280. * Only architectures that have memory mapped IO functions defined
  3281. * (and the PCI_IOBASE value defined) should call this function.
  3282. */
  3283. void pci_unmap_iospace(struct resource *res)
  3284. {
  3285. #if defined(PCI_IOBASE) && defined(CONFIG_MMU)
  3286. unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
  3287. unmap_kernel_range(vaddr, resource_size(res));
  3288. #endif
  3289. }
  3290. EXPORT_SYMBOL(pci_unmap_iospace);
  3291. static void devm_pci_unmap_iospace(struct device *dev, void *ptr)
  3292. {
  3293. struct resource **res = ptr;
  3294. pci_unmap_iospace(*res);
  3295. }
  3296. /**
  3297. * devm_pci_remap_iospace - Managed pci_remap_iospace()
  3298. * @dev: Generic device to remap IO address for
  3299. * @res: Resource describing the I/O space
  3300. * @phys_addr: physical address of range to be mapped
  3301. *
  3302. * Managed pci_remap_iospace(). Map is automatically unmapped on driver
  3303. * detach.
  3304. */
  3305. int devm_pci_remap_iospace(struct device *dev, const struct resource *res,
  3306. phys_addr_t phys_addr)
  3307. {
  3308. const struct resource **ptr;
  3309. int error;
  3310. ptr = devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL);
  3311. if (!ptr)
  3312. return -ENOMEM;
  3313. error = pci_remap_iospace(res, phys_addr);
  3314. if (error) {
  3315. devres_free(ptr);
  3316. } else {
  3317. *ptr = res;
  3318. devres_add(dev, ptr);
  3319. }
  3320. return error;
  3321. }
  3322. EXPORT_SYMBOL(devm_pci_remap_iospace);
  3323. /**
  3324. * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
  3325. * @dev: Generic device to remap IO address for
  3326. * @offset: Resource address to map
  3327. * @size: Size of map
  3328. *
  3329. * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver
  3330. * detach.
  3331. */
  3332. void __iomem *devm_pci_remap_cfgspace(struct device *dev,
  3333. resource_size_t offset,
  3334. resource_size_t size)
  3335. {
  3336. void __iomem **ptr, *addr;
  3337. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  3338. if (!ptr)
  3339. return NULL;
  3340. addr = pci_remap_cfgspace(offset, size);
  3341. if (addr) {
  3342. *ptr = addr;
  3343. devres_add(dev, ptr);
  3344. } else
  3345. devres_free(ptr);
  3346. return addr;
  3347. }
  3348. EXPORT_SYMBOL(devm_pci_remap_cfgspace);
  3349. /**
  3350. * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
  3351. * @dev: generic device to handle the resource for
  3352. * @res: configuration space resource to be handled
  3353. *
  3354. * Checks that a resource is a valid memory region, requests the memory
  3355. * region and ioremaps with pci_remap_cfgspace() API that ensures the
  3356. * proper PCI configuration space memory attributes are guaranteed.
  3357. *
  3358. * All operations are managed and will be undone on driver detach.
  3359. *
  3360. * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
  3361. * on failure. Usage example::
  3362. *
  3363. * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3364. * base = devm_pci_remap_cfg_resource(&pdev->dev, res);
  3365. * if (IS_ERR(base))
  3366. * return PTR_ERR(base);
  3367. */
  3368. void __iomem *devm_pci_remap_cfg_resource(struct device *dev,
  3369. struct resource *res)
  3370. {
  3371. resource_size_t size;
  3372. const char *name;
  3373. void __iomem *dest_ptr;
  3374. BUG_ON(!dev);
  3375. if (!res || resource_type(res) != IORESOURCE_MEM) {
  3376. dev_err(dev, "invalid resource\n");
  3377. return IOMEM_ERR_PTR(-EINVAL);
  3378. }
  3379. size = resource_size(res);
  3380. name = res->name ?: dev_name(dev);
  3381. if (!devm_request_mem_region(dev, res->start, size, name)) {
  3382. dev_err(dev, "can't request region for resource %pR\n", res);
  3383. return IOMEM_ERR_PTR(-EBUSY);
  3384. }
  3385. dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
  3386. if (!dest_ptr) {
  3387. dev_err(dev, "ioremap failed for resource %pR\n", res);
  3388. devm_release_mem_region(dev, res->start, size);
  3389. dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
  3390. }
  3391. return dest_ptr;
  3392. }
  3393. EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
  3394. static void __pci_set_master(struct pci_dev *dev, bool enable)
  3395. {
  3396. u16 old_cmd, cmd;
  3397. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  3398. if (enable)
  3399. cmd = old_cmd | PCI_COMMAND_MASTER;
  3400. else
  3401. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  3402. if (cmd != old_cmd) {
  3403. pci_dbg(dev, "%s bus mastering\n",
  3404. enable ? "enabling" : "disabling");
  3405. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3406. }
  3407. dev->is_busmaster = enable;
  3408. }
  3409. /**
  3410. * pcibios_setup - process "pci=" kernel boot arguments
  3411. * @str: string used to pass in "pci=" kernel boot arguments
  3412. *
  3413. * Process kernel boot arguments. This is the default implementation.
  3414. * Architecture specific implementations can override this as necessary.
  3415. */
  3416. char * __weak __init pcibios_setup(char *str)
  3417. {
  3418. return str;
  3419. }
  3420. /**
  3421. * pcibios_set_master - enable PCI bus-mastering for device dev
  3422. * @dev: the PCI device to enable
  3423. *
  3424. * Enables PCI bus-mastering for the device. This is the default
  3425. * implementation. Architecture specific implementations can override
  3426. * this if necessary.
  3427. */
  3428. void __weak pcibios_set_master(struct pci_dev *dev)
  3429. {
  3430. u8 lat;
  3431. /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
  3432. if (pci_is_pcie(dev))
  3433. return;
  3434. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  3435. if (lat < 16)
  3436. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  3437. else if (lat > pcibios_max_latency)
  3438. lat = pcibios_max_latency;
  3439. else
  3440. return;
  3441. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  3442. }
  3443. /**
  3444. * pci_set_master - enables bus-mastering for device dev
  3445. * @dev: the PCI device to enable
  3446. *
  3447. * Enables bus-mastering on the device and calls pcibios_set_master()
  3448. * to do the needed arch specific settings.
  3449. */
  3450. void pci_set_master(struct pci_dev *dev)
  3451. {
  3452. __pci_set_master(dev, true);
  3453. pcibios_set_master(dev);
  3454. }
  3455. EXPORT_SYMBOL(pci_set_master);
  3456. /**
  3457. * pci_clear_master - disables bus-mastering for device dev
  3458. * @dev: the PCI device to disable
  3459. */
  3460. void pci_clear_master(struct pci_dev *dev)
  3461. {
  3462. __pci_set_master(dev, false);
  3463. }
  3464. EXPORT_SYMBOL(pci_clear_master);
  3465. /**
  3466. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  3467. * @dev: the PCI device for which MWI is to be enabled
  3468. *
  3469. * Helper function for pci_set_mwi.
  3470. * Originally copied from drivers/net/acenic.c.
  3471. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  3472. *
  3473. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3474. */
  3475. int pci_set_cacheline_size(struct pci_dev *dev)
  3476. {
  3477. u8 cacheline_size;
  3478. if (!pci_cache_line_size)
  3479. return -EINVAL;
  3480. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  3481. equal to or multiple of the right value. */
  3482. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  3483. if (cacheline_size >= pci_cache_line_size &&
  3484. (cacheline_size % pci_cache_line_size) == 0)
  3485. return 0;
  3486. /* Write the correct value. */
  3487. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  3488. /* Read it back. */
  3489. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  3490. if (cacheline_size == pci_cache_line_size)
  3491. return 0;
  3492. pci_printk(KERN_DEBUG, dev, "cache line size of %d is not supported\n",
  3493. pci_cache_line_size << 2);
  3494. return -EINVAL;
  3495. }
  3496. EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
  3497. /**
  3498. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  3499. * @dev: the PCI device for which MWI is enabled
  3500. *
  3501. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  3502. *
  3503. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3504. */
  3505. int pci_set_mwi(struct pci_dev *dev)
  3506. {
  3507. #ifdef PCI_DISABLE_MWI
  3508. return 0;
  3509. #else
  3510. int rc;
  3511. u16 cmd;
  3512. rc = pci_set_cacheline_size(dev);
  3513. if (rc)
  3514. return rc;
  3515. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  3516. if (!(cmd & PCI_COMMAND_INVALIDATE)) {
  3517. pci_dbg(dev, "enabling Mem-Wr-Inval\n");
  3518. cmd |= PCI_COMMAND_INVALIDATE;
  3519. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3520. }
  3521. return 0;
  3522. #endif
  3523. }
  3524. EXPORT_SYMBOL(pci_set_mwi);
  3525. /**
  3526. * pcim_set_mwi - a device-managed pci_set_mwi()
  3527. * @dev: the PCI device for which MWI is enabled
  3528. *
  3529. * Managed pci_set_mwi().
  3530. *
  3531. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3532. */
  3533. int pcim_set_mwi(struct pci_dev *dev)
  3534. {
  3535. struct pci_devres *dr;
  3536. dr = find_pci_dr(dev);
  3537. if (!dr)
  3538. return -ENOMEM;
  3539. dr->mwi = 1;
  3540. return pci_set_mwi(dev);
  3541. }
  3542. EXPORT_SYMBOL(pcim_set_mwi);
  3543. /**
  3544. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  3545. * @dev: the PCI device for which MWI is enabled
  3546. *
  3547. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  3548. * Callers are not required to check the return value.
  3549. *
  3550. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3551. */
  3552. int pci_try_set_mwi(struct pci_dev *dev)
  3553. {
  3554. #ifdef PCI_DISABLE_MWI
  3555. return 0;
  3556. #else
  3557. return pci_set_mwi(dev);
  3558. #endif
  3559. }
  3560. EXPORT_SYMBOL(pci_try_set_mwi);
  3561. /**
  3562. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  3563. * @dev: the PCI device to disable
  3564. *
  3565. * Disables PCI Memory-Write-Invalidate transaction on the device
  3566. */
  3567. void pci_clear_mwi(struct pci_dev *dev)
  3568. {
  3569. #ifndef PCI_DISABLE_MWI
  3570. u16 cmd;
  3571. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  3572. if (cmd & PCI_COMMAND_INVALIDATE) {
  3573. cmd &= ~PCI_COMMAND_INVALIDATE;
  3574. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3575. }
  3576. #endif
  3577. }
  3578. EXPORT_SYMBOL(pci_clear_mwi);
  3579. /**
  3580. * pci_intx - enables/disables PCI INTx for device dev
  3581. * @pdev: the PCI device to operate on
  3582. * @enable: boolean: whether to enable or disable PCI INTx
  3583. *
  3584. * Enables/disables PCI INTx for device dev
  3585. */
  3586. void pci_intx(struct pci_dev *pdev, int enable)
  3587. {
  3588. u16 pci_command, new;
  3589. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  3590. if (enable)
  3591. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  3592. else
  3593. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  3594. if (new != pci_command) {
  3595. struct pci_devres *dr;
  3596. pci_write_config_word(pdev, PCI_COMMAND, new);
  3597. dr = find_pci_dr(pdev);
  3598. if (dr && !dr->restore_intx) {
  3599. dr->restore_intx = 1;
  3600. dr->orig_intx = !enable;
  3601. }
  3602. }
  3603. }
  3604. EXPORT_SYMBOL_GPL(pci_intx);
  3605. static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
  3606. {
  3607. struct pci_bus *bus = dev->bus;
  3608. bool mask_updated = true;
  3609. u32 cmd_status_dword;
  3610. u16 origcmd, newcmd;
  3611. unsigned long flags;
  3612. bool irq_pending;
  3613. /*
  3614. * We do a single dword read to retrieve both command and status.
  3615. * Document assumptions that make this possible.
  3616. */
  3617. BUILD_BUG_ON(PCI_COMMAND % 4);
  3618. BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
  3619. raw_spin_lock_irqsave(&pci_lock, flags);
  3620. bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
  3621. irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
  3622. /*
  3623. * Check interrupt status register to see whether our device
  3624. * triggered the interrupt (when masking) or the next IRQ is
  3625. * already pending (when unmasking).
  3626. */
  3627. if (mask != irq_pending) {
  3628. mask_updated = false;
  3629. goto done;
  3630. }
  3631. origcmd = cmd_status_dword;
  3632. newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
  3633. if (mask)
  3634. newcmd |= PCI_COMMAND_INTX_DISABLE;
  3635. if (newcmd != origcmd)
  3636. bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
  3637. done:
  3638. raw_spin_unlock_irqrestore(&pci_lock, flags);
  3639. return mask_updated;
  3640. }
  3641. /**
  3642. * pci_check_and_mask_intx - mask INTx on pending interrupt
  3643. * @dev: the PCI device to operate on
  3644. *
  3645. * Check if the device dev has its INTx line asserted, mask it and
  3646. * return true in that case. False is returned if no interrupt was
  3647. * pending.
  3648. */
  3649. bool pci_check_and_mask_intx(struct pci_dev *dev)
  3650. {
  3651. return pci_check_and_set_intx_mask(dev, true);
  3652. }
  3653. EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
  3654. /**
  3655. * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
  3656. * @dev: the PCI device to operate on
  3657. *
  3658. * Check if the device dev has its INTx line asserted, unmask it if not
  3659. * and return true. False is returned and the mask remains active if
  3660. * there was still an interrupt pending.
  3661. */
  3662. bool pci_check_and_unmask_intx(struct pci_dev *dev)
  3663. {
  3664. return pci_check_and_set_intx_mask(dev, false);
  3665. }
  3666. EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
  3667. /**
  3668. * pci_wait_for_pending_transaction - waits for pending transaction
  3669. * @dev: the PCI device to operate on
  3670. *
  3671. * Return 0 if transaction is pending 1 otherwise.
  3672. */
  3673. int pci_wait_for_pending_transaction(struct pci_dev *dev)
  3674. {
  3675. if (!pci_is_pcie(dev))
  3676. return 1;
  3677. return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA,
  3678. PCI_EXP_DEVSTA_TRPND);
  3679. }
  3680. EXPORT_SYMBOL(pci_wait_for_pending_transaction);
  3681. static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout)
  3682. {
  3683. int delay = 1;
  3684. u32 id;
  3685. /*
  3686. * After reset, the device should not silently discard config
  3687. * requests, but it may still indicate that it needs more time by
  3688. * responding to them with CRS completions. The Root Port will
  3689. * generally synthesize ~0 data to complete the read (except when
  3690. * CRS SV is enabled and the read was for the Vendor ID; in that
  3691. * case it synthesizes 0x0001 data).
  3692. *
  3693. * Wait for the device to return a non-CRS completion. Read the
  3694. * Command register instead of Vendor ID so we don't have to
  3695. * contend with the CRS SV value.
  3696. */
  3697. pci_read_config_dword(dev, PCI_COMMAND, &id);
  3698. while (id == ~0) {
  3699. if (delay > timeout) {
  3700. pci_warn(dev, "not ready %dms after %s; giving up\n",
  3701. delay - 1, reset_type);
  3702. return -ENOTTY;
  3703. }
  3704. if (delay > 1000)
  3705. pci_info(dev, "not ready %dms after %s; waiting\n",
  3706. delay - 1, reset_type);
  3707. msleep(delay);
  3708. delay *= 2;
  3709. pci_read_config_dword(dev, PCI_COMMAND, &id);
  3710. }
  3711. if (delay > 1000)
  3712. pci_info(dev, "ready %dms after %s\n", delay - 1,
  3713. reset_type);
  3714. return 0;
  3715. }
  3716. /**
  3717. * pcie_has_flr - check if a device supports function level resets
  3718. * @dev: device to check
  3719. *
  3720. * Returns true if the device advertises support for PCIe function level
  3721. * resets.
  3722. */
  3723. bool pcie_has_flr(struct pci_dev *dev)
  3724. {
  3725. u32 cap;
  3726. if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
  3727. return false;
  3728. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
  3729. return cap & PCI_EXP_DEVCAP_FLR;
  3730. }
  3731. EXPORT_SYMBOL_GPL(pcie_has_flr);
  3732. /**
  3733. * pcie_flr - initiate a PCIe function level reset
  3734. * @dev: device to reset
  3735. *
  3736. * Initiate a function level reset on @dev. The caller should ensure the
  3737. * device supports FLR before calling this function, e.g. by using the
  3738. * pcie_has_flr() helper.
  3739. */
  3740. int pcie_flr(struct pci_dev *dev)
  3741. {
  3742. if (!pci_wait_for_pending_transaction(dev))
  3743. pci_err(dev, "timed out waiting for pending transaction; performing function level reset anyway\n");
  3744. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
  3745. /*
  3746. * Per PCIe r4.0, sec 6.6.2, a device must complete an FLR within
  3747. * 100ms, but may silently discard requests while the FLR is in
  3748. * progress. Wait 100ms before trying to access the device.
  3749. */
  3750. msleep(100);
  3751. return pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS);
  3752. }
  3753. EXPORT_SYMBOL_GPL(pcie_flr);
  3754. static int pci_af_flr(struct pci_dev *dev, int probe)
  3755. {
  3756. int pos;
  3757. u8 cap;
  3758. pos = pci_find_capability(dev, PCI_CAP_ID_AF);
  3759. if (!pos)
  3760. return -ENOTTY;
  3761. if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
  3762. return -ENOTTY;
  3763. pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
  3764. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  3765. return -ENOTTY;
  3766. if (probe)
  3767. return 0;
  3768. /*
  3769. * Wait for Transaction Pending bit to clear. A word-aligned test
  3770. * is used, so we use the conrol offset rather than status and shift
  3771. * the test bit to match.
  3772. */
  3773. if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL,
  3774. PCI_AF_STATUS_TP << 8))
  3775. pci_err(dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n");
  3776. pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  3777. /*
  3778. * Per Advanced Capabilities for Conventional PCI ECN, 13 April 2006,
  3779. * updated 27 July 2006; a device must complete an FLR within
  3780. * 100ms, but may silently discard requests while the FLR is in
  3781. * progress. Wait 100ms before trying to access the device.
  3782. */
  3783. msleep(100);
  3784. return pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS);
  3785. }
  3786. /**
  3787. * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
  3788. * @dev: Device to reset.
  3789. * @probe: If set, only check if the device can be reset this way.
  3790. *
  3791. * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
  3792. * unset, it will be reinitialized internally when going from PCI_D3hot to
  3793. * PCI_D0. If that's the case and the device is not in a low-power state
  3794. * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
  3795. *
  3796. * NOTE: This causes the caller to sleep for twice the device power transition
  3797. * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
  3798. * by default (i.e. unless the @dev's d3_delay field has a different value).
  3799. * Moreover, only devices in D0 can be reset by this function.
  3800. */
  3801. static int pci_pm_reset(struct pci_dev *dev, int probe)
  3802. {
  3803. u16 csr;
  3804. if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET)
  3805. return -ENOTTY;
  3806. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
  3807. if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
  3808. return -ENOTTY;
  3809. if (probe)
  3810. return 0;
  3811. if (dev->current_state != PCI_D0)
  3812. return -EINVAL;
  3813. csr &= ~PCI_PM_CTRL_STATE_MASK;
  3814. csr |= PCI_D3hot;
  3815. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  3816. pci_dev_d3_sleep(dev);
  3817. csr &= ~PCI_PM_CTRL_STATE_MASK;
  3818. csr |= PCI_D0;
  3819. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  3820. pci_dev_d3_sleep(dev);
  3821. return pci_dev_wait(dev, "PM D3->D0", PCIE_RESET_READY_POLL_MS);
  3822. }
  3823. /**
  3824. * pcie_wait_for_link - Wait until link is active or inactive
  3825. * @pdev: Bridge device
  3826. * @active: waiting for active or inactive?
  3827. *
  3828. * Use this to wait till link becomes active or inactive.
  3829. */
  3830. bool pcie_wait_for_link(struct pci_dev *pdev, bool active)
  3831. {
  3832. int timeout = 1000;
  3833. bool ret;
  3834. u16 lnk_status;
  3835. for (;;) {
  3836. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  3837. ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
  3838. if (ret == active)
  3839. return true;
  3840. if (timeout <= 0)
  3841. break;
  3842. msleep(10);
  3843. timeout -= 10;
  3844. }
  3845. pci_info(pdev, "Data Link Layer Link Active not %s in 1000 msec\n",
  3846. active ? "set" : "cleared");
  3847. return false;
  3848. }
  3849. void pci_reset_secondary_bus(struct pci_dev *dev)
  3850. {
  3851. u16 ctrl;
  3852. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
  3853. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  3854. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  3855. /*
  3856. * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double
  3857. * this to 2ms to ensure that we meet the minimum requirement.
  3858. */
  3859. msleep(2);
  3860. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  3861. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  3862. /*
  3863. * Trhfa for conventional PCI is 2^25 clock cycles.
  3864. * Assuming a minimum 33MHz clock this results in a 1s
  3865. * delay before we can consider subordinate devices to
  3866. * be re-initialized. PCIe has some ways to shorten this,
  3867. * but we don't make use of them yet.
  3868. */
  3869. ssleep(1);
  3870. }
  3871. void __weak pcibios_reset_secondary_bus(struct pci_dev *dev)
  3872. {
  3873. pci_reset_secondary_bus(dev);
  3874. }
  3875. /**
  3876. * pci_bridge_secondary_bus_reset - Reset the secondary bus on a PCI bridge.
  3877. * @dev: Bridge device
  3878. *
  3879. * Use the bridge control register to assert reset on the secondary bus.
  3880. * Devices on the secondary bus are left in power-on state.
  3881. */
  3882. int pci_bridge_secondary_bus_reset(struct pci_dev *dev)
  3883. {
  3884. pcibios_reset_secondary_bus(dev);
  3885. return pci_dev_wait(dev, "bus reset", PCIE_RESET_READY_POLL_MS);
  3886. }
  3887. EXPORT_SYMBOL_GPL(pci_bridge_secondary_bus_reset);
  3888. static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
  3889. {
  3890. struct pci_dev *pdev;
  3891. if (pci_is_root_bus(dev->bus) || dev->subordinate ||
  3892. !dev->bus->self || dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
  3893. return -ENOTTY;
  3894. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  3895. if (pdev != dev)
  3896. return -ENOTTY;
  3897. if (probe)
  3898. return 0;
  3899. return pci_bridge_secondary_bus_reset(dev->bus->self);
  3900. }
  3901. static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, int probe)
  3902. {
  3903. int rc = -ENOTTY;
  3904. if (!hotplug || !try_module_get(hotplug->ops->owner))
  3905. return rc;
  3906. if (hotplug->ops->reset_slot)
  3907. rc = hotplug->ops->reset_slot(hotplug, probe);
  3908. module_put(hotplug->ops->owner);
  3909. return rc;
  3910. }
  3911. static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe)
  3912. {
  3913. struct pci_dev *pdev;
  3914. if (dev->subordinate || !dev->slot ||
  3915. dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
  3916. return -ENOTTY;
  3917. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  3918. if (pdev != dev && pdev->slot == dev->slot)
  3919. return -ENOTTY;
  3920. return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
  3921. }
  3922. static void pci_dev_lock(struct pci_dev *dev)
  3923. {
  3924. pci_cfg_access_lock(dev);
  3925. /* block PM suspend, driver probe, etc. */
  3926. device_lock(&dev->dev);
  3927. }
  3928. /* Return 1 on successful lock, 0 on contention */
  3929. static int pci_dev_trylock(struct pci_dev *dev)
  3930. {
  3931. if (pci_cfg_access_trylock(dev)) {
  3932. if (device_trylock(&dev->dev))
  3933. return 1;
  3934. pci_cfg_access_unlock(dev);
  3935. }
  3936. return 0;
  3937. }
  3938. static void pci_dev_unlock(struct pci_dev *dev)
  3939. {
  3940. device_unlock(&dev->dev);
  3941. pci_cfg_access_unlock(dev);
  3942. }
  3943. static void pci_dev_save_and_disable(struct pci_dev *dev)
  3944. {
  3945. const struct pci_error_handlers *err_handler =
  3946. dev->driver ? dev->driver->err_handler : NULL;
  3947. /*
  3948. * dev->driver->err_handler->reset_prepare() is protected against
  3949. * races with ->remove() by the device lock, which must be held by
  3950. * the caller.
  3951. */
  3952. if (err_handler && err_handler->reset_prepare)
  3953. err_handler->reset_prepare(dev);
  3954. /*
  3955. * Wake-up device prior to save. PM registers default to D0 after
  3956. * reset and a simple register restore doesn't reliably return
  3957. * to a non-D0 state anyway.
  3958. */
  3959. pci_set_power_state(dev, PCI_D0);
  3960. pci_save_state(dev);
  3961. /*
  3962. * Disable the device by clearing the Command register, except for
  3963. * INTx-disable which is set. This not only disables MMIO and I/O port
  3964. * BARs, but also prevents the device from being Bus Master, preventing
  3965. * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
  3966. * compliant devices, INTx-disable prevents legacy interrupts.
  3967. */
  3968. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  3969. }
  3970. static void pci_dev_restore(struct pci_dev *dev)
  3971. {
  3972. const struct pci_error_handlers *err_handler =
  3973. dev->driver ? dev->driver->err_handler : NULL;
  3974. pci_restore_state(dev);
  3975. /*
  3976. * dev->driver->err_handler->reset_done() is protected against
  3977. * races with ->remove() by the device lock, which must be held by
  3978. * the caller.
  3979. */
  3980. if (err_handler && err_handler->reset_done)
  3981. err_handler->reset_done(dev);
  3982. }
  3983. /**
  3984. * __pci_reset_function_locked - reset a PCI device function while holding
  3985. * the @dev mutex lock.
  3986. * @dev: PCI device to reset
  3987. *
  3988. * Some devices allow an individual function to be reset without affecting
  3989. * other functions in the same device. The PCI device must be responsive
  3990. * to PCI config space in order to use this function.
  3991. *
  3992. * The device function is presumed to be unused and the caller is holding
  3993. * the device mutex lock when this function is called.
  3994. * Resetting the device will make the contents of PCI configuration space
  3995. * random, so any caller of this must be prepared to reinitialise the
  3996. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  3997. * etc.
  3998. *
  3999. * Returns 0 if the device function was successfully reset or negative if the
  4000. * device doesn't support resetting a single function.
  4001. */
  4002. int __pci_reset_function_locked(struct pci_dev *dev)
  4003. {
  4004. int rc;
  4005. might_sleep();
  4006. /*
  4007. * A reset method returns -ENOTTY if it doesn't support this device
  4008. * and we should try the next method.
  4009. *
  4010. * If it returns 0 (success), we're finished. If it returns any
  4011. * other error, we're also finished: this indicates that further
  4012. * reset mechanisms might be broken on the device.
  4013. */
  4014. rc = pci_dev_specific_reset(dev, 0);
  4015. if (rc != -ENOTTY)
  4016. return rc;
  4017. if (pcie_has_flr(dev)) {
  4018. rc = pcie_flr(dev);
  4019. if (rc != -ENOTTY)
  4020. return rc;
  4021. }
  4022. rc = pci_af_flr(dev, 0);
  4023. if (rc != -ENOTTY)
  4024. return rc;
  4025. rc = pci_pm_reset(dev, 0);
  4026. if (rc != -ENOTTY)
  4027. return rc;
  4028. rc = pci_dev_reset_slot_function(dev, 0);
  4029. if (rc != -ENOTTY)
  4030. return rc;
  4031. return pci_parent_bus_reset(dev, 0);
  4032. }
  4033. EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
  4034. /**
  4035. * pci_probe_reset_function - check whether the device can be safely reset
  4036. * @dev: PCI device to reset
  4037. *
  4038. * Some devices allow an individual function to be reset without affecting
  4039. * other functions in the same device. The PCI device must be responsive
  4040. * to PCI config space in order to use this function.
  4041. *
  4042. * Returns 0 if the device function can be reset or negative if the
  4043. * device doesn't support resetting a single function.
  4044. */
  4045. int pci_probe_reset_function(struct pci_dev *dev)
  4046. {
  4047. int rc;
  4048. might_sleep();
  4049. rc = pci_dev_specific_reset(dev, 1);
  4050. if (rc != -ENOTTY)
  4051. return rc;
  4052. if (pcie_has_flr(dev))
  4053. return 0;
  4054. rc = pci_af_flr(dev, 1);
  4055. if (rc != -ENOTTY)
  4056. return rc;
  4057. rc = pci_pm_reset(dev, 1);
  4058. if (rc != -ENOTTY)
  4059. return rc;
  4060. rc = pci_dev_reset_slot_function(dev, 1);
  4061. if (rc != -ENOTTY)
  4062. return rc;
  4063. return pci_parent_bus_reset(dev, 1);
  4064. }
  4065. /**
  4066. * pci_reset_function - quiesce and reset a PCI device function
  4067. * @dev: PCI device to reset
  4068. *
  4069. * Some devices allow an individual function to be reset without affecting
  4070. * other functions in the same device. The PCI device must be responsive
  4071. * to PCI config space in order to use this function.
  4072. *
  4073. * This function does not just reset the PCI portion of a device, but
  4074. * clears all the state associated with the device. This function differs
  4075. * from __pci_reset_function_locked() in that it saves and restores device state
  4076. * over the reset and takes the PCI device lock.
  4077. *
  4078. * Returns 0 if the device function was successfully reset or negative if the
  4079. * device doesn't support resetting a single function.
  4080. */
  4081. int pci_reset_function(struct pci_dev *dev)
  4082. {
  4083. int rc;
  4084. if (!dev->reset_fn)
  4085. return -ENOTTY;
  4086. pci_dev_lock(dev);
  4087. pci_dev_save_and_disable(dev);
  4088. rc = __pci_reset_function_locked(dev);
  4089. pci_dev_restore(dev);
  4090. pci_dev_unlock(dev);
  4091. return rc;
  4092. }
  4093. EXPORT_SYMBOL_GPL(pci_reset_function);
  4094. /**
  4095. * pci_reset_function_locked - quiesce and reset a PCI device function
  4096. * @dev: PCI device to reset
  4097. *
  4098. * Some devices allow an individual function to be reset without affecting
  4099. * other functions in the same device. The PCI device must be responsive
  4100. * to PCI config space in order to use this function.
  4101. *
  4102. * This function does not just reset the PCI portion of a device, but
  4103. * clears all the state associated with the device. This function differs
  4104. * from __pci_reset_function_locked() in that it saves and restores device state
  4105. * over the reset. It also differs from pci_reset_function() in that it
  4106. * requires the PCI device lock to be held.
  4107. *
  4108. * Returns 0 if the device function was successfully reset or negative if the
  4109. * device doesn't support resetting a single function.
  4110. */
  4111. int pci_reset_function_locked(struct pci_dev *dev)
  4112. {
  4113. int rc;
  4114. if (!dev->reset_fn)
  4115. return -ENOTTY;
  4116. pci_dev_save_and_disable(dev);
  4117. rc = __pci_reset_function_locked(dev);
  4118. pci_dev_restore(dev);
  4119. return rc;
  4120. }
  4121. EXPORT_SYMBOL_GPL(pci_reset_function_locked);
  4122. /**
  4123. * pci_try_reset_function - quiesce and reset a PCI device function
  4124. * @dev: PCI device to reset
  4125. *
  4126. * Same as above, except return -EAGAIN if unable to lock device.
  4127. */
  4128. int pci_try_reset_function(struct pci_dev *dev)
  4129. {
  4130. int rc;
  4131. if (!dev->reset_fn)
  4132. return -ENOTTY;
  4133. if (!pci_dev_trylock(dev))
  4134. return -EAGAIN;
  4135. pci_dev_save_and_disable(dev);
  4136. rc = __pci_reset_function_locked(dev);
  4137. pci_dev_restore(dev);
  4138. pci_dev_unlock(dev);
  4139. return rc;
  4140. }
  4141. EXPORT_SYMBOL_GPL(pci_try_reset_function);
  4142. /* Do any devices on or below this bus prevent a bus reset? */
  4143. static bool pci_bus_resetable(struct pci_bus *bus)
  4144. {
  4145. struct pci_dev *dev;
  4146. if (bus->self && (bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
  4147. return false;
  4148. list_for_each_entry(dev, &bus->devices, bus_list) {
  4149. if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
  4150. (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
  4151. return false;
  4152. }
  4153. return true;
  4154. }
  4155. /* Lock devices from the top of the tree down */
  4156. static void pci_bus_lock(struct pci_bus *bus)
  4157. {
  4158. struct pci_dev *dev;
  4159. list_for_each_entry(dev, &bus->devices, bus_list) {
  4160. pci_dev_lock(dev);
  4161. if (dev->subordinate)
  4162. pci_bus_lock(dev->subordinate);
  4163. }
  4164. }
  4165. /* Unlock devices from the bottom of the tree up */
  4166. static void pci_bus_unlock(struct pci_bus *bus)
  4167. {
  4168. struct pci_dev *dev;
  4169. list_for_each_entry(dev, &bus->devices, bus_list) {
  4170. if (dev->subordinate)
  4171. pci_bus_unlock(dev->subordinate);
  4172. pci_dev_unlock(dev);
  4173. }
  4174. }
  4175. /* Return 1 on successful lock, 0 on contention */
  4176. static int pci_bus_trylock(struct pci_bus *bus)
  4177. {
  4178. struct pci_dev *dev;
  4179. list_for_each_entry(dev, &bus->devices, bus_list) {
  4180. if (!pci_dev_trylock(dev))
  4181. goto unlock;
  4182. if (dev->subordinate) {
  4183. if (!pci_bus_trylock(dev->subordinate)) {
  4184. pci_dev_unlock(dev);
  4185. goto unlock;
  4186. }
  4187. }
  4188. }
  4189. return 1;
  4190. unlock:
  4191. list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
  4192. if (dev->subordinate)
  4193. pci_bus_unlock(dev->subordinate);
  4194. pci_dev_unlock(dev);
  4195. }
  4196. return 0;
  4197. }
  4198. /* Do any devices on or below this slot prevent a bus reset? */
  4199. static bool pci_slot_resetable(struct pci_slot *slot)
  4200. {
  4201. struct pci_dev *dev;
  4202. if (slot->bus->self &&
  4203. (slot->bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
  4204. return false;
  4205. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4206. if (!dev->slot || dev->slot != slot)
  4207. continue;
  4208. if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
  4209. (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
  4210. return false;
  4211. }
  4212. return true;
  4213. }
  4214. /* Lock devices from the top of the tree down */
  4215. static void pci_slot_lock(struct pci_slot *slot)
  4216. {
  4217. struct pci_dev *dev;
  4218. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4219. if (!dev->slot || dev->slot != slot)
  4220. continue;
  4221. pci_dev_lock(dev);
  4222. if (dev->subordinate)
  4223. pci_bus_lock(dev->subordinate);
  4224. }
  4225. }
  4226. /* Unlock devices from the bottom of the tree up */
  4227. static void pci_slot_unlock(struct pci_slot *slot)
  4228. {
  4229. struct pci_dev *dev;
  4230. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4231. if (!dev->slot || dev->slot != slot)
  4232. continue;
  4233. if (dev->subordinate)
  4234. pci_bus_unlock(dev->subordinate);
  4235. pci_dev_unlock(dev);
  4236. }
  4237. }
  4238. /* Return 1 on successful lock, 0 on contention */
  4239. static int pci_slot_trylock(struct pci_slot *slot)
  4240. {
  4241. struct pci_dev *dev;
  4242. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4243. if (!dev->slot || dev->slot != slot)
  4244. continue;
  4245. if (!pci_dev_trylock(dev))
  4246. goto unlock;
  4247. if (dev->subordinate) {
  4248. if (!pci_bus_trylock(dev->subordinate)) {
  4249. pci_dev_unlock(dev);
  4250. goto unlock;
  4251. }
  4252. }
  4253. }
  4254. return 1;
  4255. unlock:
  4256. list_for_each_entry_continue_reverse(dev,
  4257. &slot->bus->devices, bus_list) {
  4258. if (!dev->slot || dev->slot != slot)
  4259. continue;
  4260. if (dev->subordinate)
  4261. pci_bus_unlock(dev->subordinate);
  4262. pci_dev_unlock(dev);
  4263. }
  4264. return 0;
  4265. }
  4266. /* Save and disable devices from the top of the tree down */
  4267. static void pci_bus_save_and_disable(struct pci_bus *bus)
  4268. {
  4269. struct pci_dev *dev;
  4270. list_for_each_entry(dev, &bus->devices, bus_list) {
  4271. pci_dev_lock(dev);
  4272. pci_dev_save_and_disable(dev);
  4273. pci_dev_unlock(dev);
  4274. if (dev->subordinate)
  4275. pci_bus_save_and_disable(dev->subordinate);
  4276. }
  4277. }
  4278. /*
  4279. * Restore devices from top of the tree down - parent bridges need to be
  4280. * restored before we can get to subordinate devices.
  4281. */
  4282. static void pci_bus_restore(struct pci_bus *bus)
  4283. {
  4284. struct pci_dev *dev;
  4285. list_for_each_entry(dev, &bus->devices, bus_list) {
  4286. pci_dev_lock(dev);
  4287. pci_dev_restore(dev);
  4288. pci_dev_unlock(dev);
  4289. if (dev->subordinate)
  4290. pci_bus_restore(dev->subordinate);
  4291. }
  4292. }
  4293. /* Save and disable devices from the top of the tree down */
  4294. static void pci_slot_save_and_disable(struct pci_slot *slot)
  4295. {
  4296. struct pci_dev *dev;
  4297. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4298. if (!dev->slot || dev->slot != slot)
  4299. continue;
  4300. pci_dev_save_and_disable(dev);
  4301. if (dev->subordinate)
  4302. pci_bus_save_and_disable(dev->subordinate);
  4303. }
  4304. }
  4305. /*
  4306. * Restore devices from top of the tree down - parent bridges need to be
  4307. * restored before we can get to subordinate devices.
  4308. */
  4309. static void pci_slot_restore(struct pci_slot *slot)
  4310. {
  4311. struct pci_dev *dev;
  4312. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4313. if (!dev->slot || dev->slot != slot)
  4314. continue;
  4315. pci_dev_lock(dev);
  4316. pci_dev_restore(dev);
  4317. pci_dev_unlock(dev);
  4318. if (dev->subordinate)
  4319. pci_bus_restore(dev->subordinate);
  4320. }
  4321. }
  4322. static int pci_slot_reset(struct pci_slot *slot, int probe)
  4323. {
  4324. int rc;
  4325. if (!slot || !pci_slot_resetable(slot))
  4326. return -ENOTTY;
  4327. if (!probe)
  4328. pci_slot_lock(slot);
  4329. might_sleep();
  4330. rc = pci_reset_hotplug_slot(slot->hotplug, probe);
  4331. if (!probe)
  4332. pci_slot_unlock(slot);
  4333. return rc;
  4334. }
  4335. /**
  4336. * pci_probe_reset_slot - probe whether a PCI slot can be reset
  4337. * @slot: PCI slot to probe
  4338. *
  4339. * Return 0 if slot can be reset, negative if a slot reset is not supported.
  4340. */
  4341. int pci_probe_reset_slot(struct pci_slot *slot)
  4342. {
  4343. return pci_slot_reset(slot, 1);
  4344. }
  4345. EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
  4346. /**
  4347. * __pci_reset_slot - Try to reset a PCI slot
  4348. * @slot: PCI slot to reset
  4349. *
  4350. * A PCI bus may host multiple slots, each slot may support a reset mechanism
  4351. * independent of other slots. For instance, some slots may support slot power
  4352. * control. In the case of a 1:1 bus to slot architecture, this function may
  4353. * wrap the bus reset to avoid spurious slot related events such as hotplug.
  4354. * Generally a slot reset should be attempted before a bus reset. All of the
  4355. * function of the slot and any subordinate buses behind the slot are reset
  4356. * through this function. PCI config space of all devices in the slot and
  4357. * behind the slot is saved before and restored after reset.
  4358. *
  4359. * Same as above except return -EAGAIN if the slot cannot be locked
  4360. */
  4361. static int __pci_reset_slot(struct pci_slot *slot)
  4362. {
  4363. int rc;
  4364. rc = pci_slot_reset(slot, 1);
  4365. if (rc)
  4366. return rc;
  4367. pci_slot_save_and_disable(slot);
  4368. if (pci_slot_trylock(slot)) {
  4369. might_sleep();
  4370. rc = pci_reset_hotplug_slot(slot->hotplug, 0);
  4371. pci_slot_unlock(slot);
  4372. } else
  4373. rc = -EAGAIN;
  4374. pci_slot_restore(slot);
  4375. return rc;
  4376. }
  4377. static int pci_bus_reset(struct pci_bus *bus, int probe)
  4378. {
  4379. int ret;
  4380. if (!bus->self || !pci_bus_resetable(bus))
  4381. return -ENOTTY;
  4382. if (probe)
  4383. return 0;
  4384. pci_bus_lock(bus);
  4385. might_sleep();
  4386. ret = pci_bridge_secondary_bus_reset(bus->self);
  4387. pci_bus_unlock(bus);
  4388. return ret;
  4389. }
  4390. /**
  4391. * pci_probe_reset_bus - probe whether a PCI bus can be reset
  4392. * @bus: PCI bus to probe
  4393. *
  4394. * Return 0 if bus can be reset, negative if a bus reset is not supported.
  4395. */
  4396. int pci_probe_reset_bus(struct pci_bus *bus)
  4397. {
  4398. return pci_bus_reset(bus, 1);
  4399. }
  4400. EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
  4401. /**
  4402. * __pci_reset_bus - Try to reset a PCI bus
  4403. * @bus: top level PCI bus to reset
  4404. *
  4405. * Same as above except return -EAGAIN if the bus cannot be locked
  4406. */
  4407. static int __pci_reset_bus(struct pci_bus *bus)
  4408. {
  4409. int rc;
  4410. rc = pci_bus_reset(bus, 1);
  4411. if (rc)
  4412. return rc;
  4413. pci_bus_save_and_disable(bus);
  4414. if (pci_bus_trylock(bus)) {
  4415. might_sleep();
  4416. rc = pci_bridge_secondary_bus_reset(bus->self);
  4417. pci_bus_unlock(bus);
  4418. } else
  4419. rc = -EAGAIN;
  4420. pci_bus_restore(bus);
  4421. return rc;
  4422. }
  4423. /**
  4424. * pci_reset_bus - Try to reset a PCI bus
  4425. * @pdev: top level PCI device to reset via slot/bus
  4426. *
  4427. * Same as above except return -EAGAIN if the bus cannot be locked
  4428. */
  4429. int pci_reset_bus(struct pci_dev *pdev)
  4430. {
  4431. return (!pci_probe_reset_slot(pdev->slot)) ?
  4432. __pci_reset_slot(pdev->slot) : __pci_reset_bus(pdev->bus);
  4433. }
  4434. EXPORT_SYMBOL_GPL(pci_reset_bus);
  4435. /**
  4436. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  4437. * @dev: PCI device to query
  4438. *
  4439. * Returns mmrbc: maximum designed memory read count in bytes
  4440. * or appropriate error value.
  4441. */
  4442. int pcix_get_max_mmrbc(struct pci_dev *dev)
  4443. {
  4444. int cap;
  4445. u32 stat;
  4446. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  4447. if (!cap)
  4448. return -EINVAL;
  4449. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  4450. return -EINVAL;
  4451. return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
  4452. }
  4453. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  4454. /**
  4455. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  4456. * @dev: PCI device to query
  4457. *
  4458. * Returns mmrbc: maximum memory read count in bytes
  4459. * or appropriate error value.
  4460. */
  4461. int pcix_get_mmrbc(struct pci_dev *dev)
  4462. {
  4463. int cap;
  4464. u16 cmd;
  4465. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  4466. if (!cap)
  4467. return -EINVAL;
  4468. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  4469. return -EINVAL;
  4470. return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  4471. }
  4472. EXPORT_SYMBOL(pcix_get_mmrbc);
  4473. /**
  4474. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  4475. * @dev: PCI device to query
  4476. * @mmrbc: maximum memory read count in bytes
  4477. * valid values are 512, 1024, 2048, 4096
  4478. *
  4479. * If possible sets maximum memory read byte count, some bridges have erratas
  4480. * that prevent this.
  4481. */
  4482. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  4483. {
  4484. int cap;
  4485. u32 stat, v, o;
  4486. u16 cmd;
  4487. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  4488. return -EINVAL;
  4489. v = ffs(mmrbc) - 10;
  4490. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  4491. if (!cap)
  4492. return -EINVAL;
  4493. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  4494. return -EINVAL;
  4495. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  4496. return -E2BIG;
  4497. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  4498. return -EINVAL;
  4499. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  4500. if (o != v) {
  4501. if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  4502. return -EIO;
  4503. cmd &= ~PCI_X_CMD_MAX_READ;
  4504. cmd |= v << 2;
  4505. if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
  4506. return -EIO;
  4507. }
  4508. return 0;
  4509. }
  4510. EXPORT_SYMBOL(pcix_set_mmrbc);
  4511. /**
  4512. * pcie_get_readrq - get PCI Express read request size
  4513. * @dev: PCI device to query
  4514. *
  4515. * Returns maximum memory read request in bytes
  4516. * or appropriate error value.
  4517. */
  4518. int pcie_get_readrq(struct pci_dev *dev)
  4519. {
  4520. u16 ctl;
  4521. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  4522. return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  4523. }
  4524. EXPORT_SYMBOL(pcie_get_readrq);
  4525. /**
  4526. * pcie_set_readrq - set PCI Express maximum memory read request
  4527. * @dev: PCI device to query
  4528. * @rq: maximum memory read count in bytes
  4529. * valid values are 128, 256, 512, 1024, 2048, 4096
  4530. *
  4531. * If possible sets maximum memory read request in bytes
  4532. */
  4533. int pcie_set_readrq(struct pci_dev *dev, int rq)
  4534. {
  4535. u16 v;
  4536. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  4537. return -EINVAL;
  4538. /*
  4539. * If using the "performance" PCIe config, we clamp the
  4540. * read rq size to the max packet size to prevent the
  4541. * host bridge generating requests larger than we can
  4542. * cope with
  4543. */
  4544. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  4545. int mps = pcie_get_mps(dev);
  4546. if (mps < rq)
  4547. rq = mps;
  4548. }
  4549. v = (ffs(rq) - 8) << 12;
  4550. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  4551. PCI_EXP_DEVCTL_READRQ, v);
  4552. }
  4553. EXPORT_SYMBOL(pcie_set_readrq);
  4554. /**
  4555. * pcie_get_mps - get PCI Express maximum payload size
  4556. * @dev: PCI device to query
  4557. *
  4558. * Returns maximum payload size in bytes
  4559. */
  4560. int pcie_get_mps(struct pci_dev *dev)
  4561. {
  4562. u16 ctl;
  4563. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  4564. return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
  4565. }
  4566. EXPORT_SYMBOL(pcie_get_mps);
  4567. /**
  4568. * pcie_set_mps - set PCI Express maximum payload size
  4569. * @dev: PCI device to query
  4570. * @mps: maximum payload size in bytes
  4571. * valid values are 128, 256, 512, 1024, 2048, 4096
  4572. *
  4573. * If possible sets maximum payload size
  4574. */
  4575. int pcie_set_mps(struct pci_dev *dev, int mps)
  4576. {
  4577. u16 v;
  4578. if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
  4579. return -EINVAL;
  4580. v = ffs(mps) - 8;
  4581. if (v > dev->pcie_mpss)
  4582. return -EINVAL;
  4583. v <<= 5;
  4584. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  4585. PCI_EXP_DEVCTL_PAYLOAD, v);
  4586. }
  4587. EXPORT_SYMBOL(pcie_set_mps);
  4588. /**
  4589. * pcie_bandwidth_available - determine minimum link settings of a PCIe
  4590. * device and its bandwidth limitation
  4591. * @dev: PCI device to query
  4592. * @limiting_dev: storage for device causing the bandwidth limitation
  4593. * @speed: storage for speed of limiting device
  4594. * @width: storage for width of limiting device
  4595. *
  4596. * Walk up the PCI device chain and find the point where the minimum
  4597. * bandwidth is available. Return the bandwidth available there and (if
  4598. * limiting_dev, speed, and width pointers are supplied) information about
  4599. * that point. The bandwidth returned is in Mb/s, i.e., megabits/second of
  4600. * raw bandwidth.
  4601. */
  4602. u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
  4603. enum pci_bus_speed *speed,
  4604. enum pcie_link_width *width)
  4605. {
  4606. u16 lnksta;
  4607. enum pci_bus_speed next_speed;
  4608. enum pcie_link_width next_width;
  4609. u32 bw, next_bw;
  4610. if (speed)
  4611. *speed = PCI_SPEED_UNKNOWN;
  4612. if (width)
  4613. *width = PCIE_LNK_WIDTH_UNKNOWN;
  4614. bw = 0;
  4615. while (dev) {
  4616. pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
  4617. next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
  4618. next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
  4619. PCI_EXP_LNKSTA_NLW_SHIFT;
  4620. next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
  4621. /* Check if current device limits the total bandwidth */
  4622. if (!bw || next_bw <= bw) {
  4623. bw = next_bw;
  4624. if (limiting_dev)
  4625. *limiting_dev = dev;
  4626. if (speed)
  4627. *speed = next_speed;
  4628. if (width)
  4629. *width = next_width;
  4630. }
  4631. dev = pci_upstream_bridge(dev);
  4632. }
  4633. return bw;
  4634. }
  4635. EXPORT_SYMBOL(pcie_bandwidth_available);
  4636. /**
  4637. * pcie_get_speed_cap - query for the PCI device's link speed capability
  4638. * @dev: PCI device to query
  4639. *
  4640. * Query the PCI device speed capability. Return the maximum link speed
  4641. * supported by the device.
  4642. */
  4643. enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev)
  4644. {
  4645. u32 lnkcap2, lnkcap;
  4646. /*
  4647. * PCIe r4.0 sec 7.5.3.18 recommends using the Supported Link
  4648. * Speeds Vector in Link Capabilities 2 when supported, falling
  4649. * back to Max Link Speed in Link Capabilities otherwise.
  4650. */
  4651. pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2);
  4652. if (lnkcap2) { /* PCIe r3.0-compliant */
  4653. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_16_0GB)
  4654. return PCIE_SPEED_16_0GT;
  4655. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
  4656. return PCIE_SPEED_8_0GT;
  4657. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
  4658. return PCIE_SPEED_5_0GT;
  4659. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
  4660. return PCIE_SPEED_2_5GT;
  4661. return PCI_SPEED_UNKNOWN;
  4662. }
  4663. pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
  4664. if (lnkcap) {
  4665. if (lnkcap & PCI_EXP_LNKCAP_SLS_16_0GB)
  4666. return PCIE_SPEED_16_0GT;
  4667. else if (lnkcap & PCI_EXP_LNKCAP_SLS_8_0GB)
  4668. return PCIE_SPEED_8_0GT;
  4669. else if (lnkcap & PCI_EXP_LNKCAP_SLS_5_0GB)
  4670. return PCIE_SPEED_5_0GT;
  4671. else if (lnkcap & PCI_EXP_LNKCAP_SLS_2_5GB)
  4672. return PCIE_SPEED_2_5GT;
  4673. }
  4674. return PCI_SPEED_UNKNOWN;
  4675. }
  4676. EXPORT_SYMBOL(pcie_get_speed_cap);
  4677. /**
  4678. * pcie_get_width_cap - query for the PCI device's link width capability
  4679. * @dev: PCI device to query
  4680. *
  4681. * Query the PCI device width capability. Return the maximum link width
  4682. * supported by the device.
  4683. */
  4684. enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev)
  4685. {
  4686. u32 lnkcap;
  4687. pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
  4688. if (lnkcap)
  4689. return (lnkcap & PCI_EXP_LNKCAP_MLW) >> 4;
  4690. return PCIE_LNK_WIDTH_UNKNOWN;
  4691. }
  4692. EXPORT_SYMBOL(pcie_get_width_cap);
  4693. /**
  4694. * pcie_bandwidth_capable - calculate a PCI device's link bandwidth capability
  4695. * @dev: PCI device
  4696. * @speed: storage for link speed
  4697. * @width: storage for link width
  4698. *
  4699. * Calculate a PCI device's link bandwidth by querying for its link speed
  4700. * and width, multiplying them, and applying encoding overhead. The result
  4701. * is in Mb/s, i.e., megabits/second of raw bandwidth.
  4702. */
  4703. u32 pcie_bandwidth_capable(struct pci_dev *dev, enum pci_bus_speed *speed,
  4704. enum pcie_link_width *width)
  4705. {
  4706. *speed = pcie_get_speed_cap(dev);
  4707. *width = pcie_get_width_cap(dev);
  4708. if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
  4709. return 0;
  4710. return *width * PCIE_SPEED2MBS_ENC(*speed);
  4711. }
  4712. /**
  4713. * __pcie_print_link_status - Report the PCI device's link speed and width
  4714. * @dev: PCI device to query
  4715. * @verbose: Print info even when enough bandwidth is available
  4716. *
  4717. * If the available bandwidth at the device is less than the device is
  4718. * capable of, report the device's maximum possible bandwidth and the
  4719. * upstream link that limits its performance. If @verbose, always print
  4720. * the available bandwidth, even if the device isn't constrained.
  4721. */
  4722. void __pcie_print_link_status(struct pci_dev *dev, bool verbose)
  4723. {
  4724. enum pcie_link_width width, width_cap;
  4725. enum pci_bus_speed speed, speed_cap;
  4726. struct pci_dev *limiting_dev = NULL;
  4727. u32 bw_avail, bw_cap;
  4728. bw_cap = pcie_bandwidth_capable(dev, &speed_cap, &width_cap);
  4729. bw_avail = pcie_bandwidth_available(dev, &limiting_dev, &speed, &width);
  4730. if (bw_avail >= bw_cap && verbose)
  4731. pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth (%s x%d link)\n",
  4732. bw_cap / 1000, bw_cap % 1000,
  4733. PCIE_SPEED2STR(speed_cap), width_cap);
  4734. else if (bw_avail < bw_cap)
  4735. pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth, limited by %s x%d link at %s (capable of %u.%03u Gb/s with %s x%d link)\n",
  4736. bw_avail / 1000, bw_avail % 1000,
  4737. PCIE_SPEED2STR(speed), width,
  4738. limiting_dev ? pci_name(limiting_dev) : "<unknown>",
  4739. bw_cap / 1000, bw_cap % 1000,
  4740. PCIE_SPEED2STR(speed_cap), width_cap);
  4741. }
  4742. /**
  4743. * pcie_print_link_status - Report the PCI device's link speed and width
  4744. * @dev: PCI device to query
  4745. *
  4746. * Report the available bandwidth at the device.
  4747. */
  4748. void pcie_print_link_status(struct pci_dev *dev)
  4749. {
  4750. __pcie_print_link_status(dev, true);
  4751. }
  4752. EXPORT_SYMBOL(pcie_print_link_status);
  4753. /**
  4754. * pci_select_bars - Make BAR mask from the type of resource
  4755. * @dev: the PCI device for which BAR mask is made
  4756. * @flags: resource type mask to be selected
  4757. *
  4758. * This helper routine makes bar mask from the type of resource.
  4759. */
  4760. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  4761. {
  4762. int i, bars = 0;
  4763. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  4764. if (pci_resource_flags(dev, i) & flags)
  4765. bars |= (1 << i);
  4766. return bars;
  4767. }
  4768. EXPORT_SYMBOL(pci_select_bars);
  4769. /* Some architectures require additional programming to enable VGA */
  4770. static arch_set_vga_state_t arch_set_vga_state;
  4771. void __init pci_register_set_vga_state(arch_set_vga_state_t func)
  4772. {
  4773. arch_set_vga_state = func; /* NULL disables */
  4774. }
  4775. static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
  4776. unsigned int command_bits, u32 flags)
  4777. {
  4778. if (arch_set_vga_state)
  4779. return arch_set_vga_state(dev, decode, command_bits,
  4780. flags);
  4781. return 0;
  4782. }
  4783. /**
  4784. * pci_set_vga_state - set VGA decode state on device and parents if requested
  4785. * @dev: the PCI device
  4786. * @decode: true = enable decoding, false = disable decoding
  4787. * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
  4788. * @flags: traverse ancestors and change bridges
  4789. * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
  4790. */
  4791. int pci_set_vga_state(struct pci_dev *dev, bool decode,
  4792. unsigned int command_bits, u32 flags)
  4793. {
  4794. struct pci_bus *bus;
  4795. struct pci_dev *bridge;
  4796. u16 cmd;
  4797. int rc;
  4798. WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
  4799. /* ARCH specific VGA enables */
  4800. rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
  4801. if (rc)
  4802. return rc;
  4803. if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
  4804. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  4805. if (decode == true)
  4806. cmd |= command_bits;
  4807. else
  4808. cmd &= ~command_bits;
  4809. pci_write_config_word(dev, PCI_COMMAND, cmd);
  4810. }
  4811. if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
  4812. return 0;
  4813. bus = dev->bus;
  4814. while (bus) {
  4815. bridge = bus->self;
  4816. if (bridge) {
  4817. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
  4818. &cmd);
  4819. if (decode == true)
  4820. cmd |= PCI_BRIDGE_CTL_VGA;
  4821. else
  4822. cmd &= ~PCI_BRIDGE_CTL_VGA;
  4823. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
  4824. cmd);
  4825. }
  4826. bus = bus->parent;
  4827. }
  4828. return 0;
  4829. }
  4830. /**
  4831. * pci_add_dma_alias - Add a DMA devfn alias for a device
  4832. * @dev: the PCI device for which alias is added
  4833. * @devfn: alias slot and function
  4834. *
  4835. * This helper encodes an 8-bit devfn as a bit number in dma_alias_mask
  4836. * which is used to program permissible bus-devfn source addresses for DMA
  4837. * requests in an IOMMU. These aliases factor into IOMMU group creation
  4838. * and are useful for devices generating DMA requests beyond or different
  4839. * from their logical bus-devfn. Examples include device quirks where the
  4840. * device simply uses the wrong devfn, as well as non-transparent bridges
  4841. * where the alias may be a proxy for devices in another domain.
  4842. *
  4843. * IOMMU group creation is performed during device discovery or addition,
  4844. * prior to any potential DMA mapping and therefore prior to driver probing
  4845. * (especially for userspace assigned devices where IOMMU group definition
  4846. * cannot be left as a userspace activity). DMA aliases should therefore
  4847. * be configured via quirks, such as the PCI fixup header quirk.
  4848. */
  4849. void pci_add_dma_alias(struct pci_dev *dev, u8 devfn)
  4850. {
  4851. if (!dev->dma_alias_mask)
  4852. dev->dma_alias_mask = kcalloc(BITS_TO_LONGS(U8_MAX),
  4853. sizeof(long), GFP_KERNEL);
  4854. if (!dev->dma_alias_mask) {
  4855. pci_warn(dev, "Unable to allocate DMA alias mask\n");
  4856. return;
  4857. }
  4858. set_bit(devfn, dev->dma_alias_mask);
  4859. pci_info(dev, "Enabling fixed DMA alias to %02x.%d\n",
  4860. PCI_SLOT(devfn), PCI_FUNC(devfn));
  4861. }
  4862. bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2)
  4863. {
  4864. return (dev1->dma_alias_mask &&
  4865. test_bit(dev2->devfn, dev1->dma_alias_mask)) ||
  4866. (dev2->dma_alias_mask &&
  4867. test_bit(dev1->devfn, dev2->dma_alias_mask));
  4868. }
  4869. bool pci_device_is_present(struct pci_dev *pdev)
  4870. {
  4871. u32 v;
  4872. if (pci_dev_is_disconnected(pdev))
  4873. return false;
  4874. return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0);
  4875. }
  4876. EXPORT_SYMBOL_GPL(pci_device_is_present);
  4877. void pci_ignore_hotplug(struct pci_dev *dev)
  4878. {
  4879. struct pci_dev *bridge = dev->bus->self;
  4880. dev->ignore_hotplug = 1;
  4881. /* Propagate the "ignore hotplug" setting to the parent bridge. */
  4882. if (bridge)
  4883. bridge->ignore_hotplug = 1;
  4884. }
  4885. EXPORT_SYMBOL_GPL(pci_ignore_hotplug);
  4886. resource_size_t __weak pcibios_default_alignment(void)
  4887. {
  4888. return 0;
  4889. }
  4890. #define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
  4891. static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
  4892. static DEFINE_SPINLOCK(resource_alignment_lock);
  4893. /**
  4894. * pci_specified_resource_alignment - get resource alignment specified by user.
  4895. * @dev: the PCI device to get
  4896. * @resize: whether or not to change resources' size when reassigning alignment
  4897. *
  4898. * RETURNS: Resource alignment if it is specified.
  4899. * Zero if it is not specified.
  4900. */
  4901. static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev,
  4902. bool *resize)
  4903. {
  4904. int align_order, count;
  4905. resource_size_t align = pcibios_default_alignment();
  4906. const char *p;
  4907. int ret;
  4908. spin_lock(&resource_alignment_lock);
  4909. p = resource_alignment_param;
  4910. if (!*p && !align)
  4911. goto out;
  4912. if (pci_has_flag(PCI_PROBE_ONLY)) {
  4913. align = 0;
  4914. pr_info_once("PCI: Ignoring requested alignments (PCI_PROBE_ONLY)\n");
  4915. goto out;
  4916. }
  4917. while (*p) {
  4918. count = 0;
  4919. if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
  4920. p[count] == '@') {
  4921. p += count + 1;
  4922. } else {
  4923. align_order = -1;
  4924. }
  4925. ret = pci_dev_str_match(dev, p, &p);
  4926. if (ret == 1) {
  4927. *resize = true;
  4928. if (align_order == -1)
  4929. align = PAGE_SIZE;
  4930. else
  4931. align = 1 << align_order;
  4932. break;
  4933. } else if (ret < 0) {
  4934. pr_err("PCI: Can't parse resource_alignment parameter: %s\n",
  4935. p);
  4936. break;
  4937. }
  4938. if (*p != ';' && *p != ',') {
  4939. /* End of param or invalid format */
  4940. break;
  4941. }
  4942. p++;
  4943. }
  4944. out:
  4945. spin_unlock(&resource_alignment_lock);
  4946. return align;
  4947. }
  4948. static void pci_request_resource_alignment(struct pci_dev *dev, int bar,
  4949. resource_size_t align, bool resize)
  4950. {
  4951. struct resource *r = &dev->resource[bar];
  4952. resource_size_t size;
  4953. if (!(r->flags & IORESOURCE_MEM))
  4954. return;
  4955. if (r->flags & IORESOURCE_PCI_FIXED) {
  4956. pci_info(dev, "BAR%d %pR: ignoring requested alignment %#llx\n",
  4957. bar, r, (unsigned long long)align);
  4958. return;
  4959. }
  4960. size = resource_size(r);
  4961. if (size >= align)
  4962. return;
  4963. /*
  4964. * Increase the alignment of the resource. There are two ways we
  4965. * can do this:
  4966. *
  4967. * 1) Increase the size of the resource. BARs are aligned on their
  4968. * size, so when we reallocate space for this resource, we'll
  4969. * allocate it with the larger alignment. This also prevents
  4970. * assignment of any other BARs inside the alignment region, so
  4971. * if we're requesting page alignment, this means no other BARs
  4972. * will share the page.
  4973. *
  4974. * The disadvantage is that this makes the resource larger than
  4975. * the hardware BAR, which may break drivers that compute things
  4976. * based on the resource size, e.g., to find registers at a
  4977. * fixed offset before the end of the BAR.
  4978. *
  4979. * 2) Retain the resource size, but use IORESOURCE_STARTALIGN and
  4980. * set r->start to the desired alignment. By itself this
  4981. * doesn't prevent other BARs being put inside the alignment
  4982. * region, but if we realign *every* resource of every device in
  4983. * the system, none of them will share an alignment region.
  4984. *
  4985. * When the user has requested alignment for only some devices via
  4986. * the "pci=resource_alignment" argument, "resize" is true and we
  4987. * use the first method. Otherwise we assume we're aligning all
  4988. * devices and we use the second.
  4989. */
  4990. pci_info(dev, "BAR%d %pR: requesting alignment to %#llx\n",
  4991. bar, r, (unsigned long long)align);
  4992. if (resize) {
  4993. r->start = 0;
  4994. r->end = align - 1;
  4995. } else {
  4996. r->flags &= ~IORESOURCE_SIZEALIGN;
  4997. r->flags |= IORESOURCE_STARTALIGN;
  4998. r->start = align;
  4999. r->end = r->start + size - 1;
  5000. }
  5001. r->flags |= IORESOURCE_UNSET;
  5002. }
  5003. /*
  5004. * This function disables memory decoding and releases memory resources
  5005. * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
  5006. * It also rounds up size to specified alignment.
  5007. * Later on, the kernel will assign page-aligned memory resource back
  5008. * to the device.
  5009. */
  5010. void pci_reassigndev_resource_alignment(struct pci_dev *dev)
  5011. {
  5012. int i;
  5013. struct resource *r;
  5014. resource_size_t align;
  5015. u16 command;
  5016. bool resize = false;
  5017. /*
  5018. * VF BARs are read-only zero according to SR-IOV spec r1.1, sec
  5019. * 3.4.1.11. Their resources are allocated from the space
  5020. * described by the VF BARx register in the PF's SR-IOV capability.
  5021. * We can't influence their alignment here.
  5022. */
  5023. if (dev->is_virtfn)
  5024. return;
  5025. /* check if specified PCI is target device to reassign */
  5026. align = pci_specified_resource_alignment(dev, &resize);
  5027. if (!align)
  5028. return;
  5029. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
  5030. (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
  5031. pci_warn(dev, "Can't reassign resources to host bridge\n");
  5032. return;
  5033. }
  5034. pci_read_config_word(dev, PCI_COMMAND, &command);
  5035. command &= ~PCI_COMMAND_MEMORY;
  5036. pci_write_config_word(dev, PCI_COMMAND, command);
  5037. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  5038. pci_request_resource_alignment(dev, i, align, resize);
  5039. /*
  5040. * Need to disable bridge's resource window,
  5041. * to enable the kernel to reassign new resource
  5042. * window later on.
  5043. */
  5044. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  5045. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  5046. for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
  5047. r = &dev->resource[i];
  5048. if (!(r->flags & IORESOURCE_MEM))
  5049. continue;
  5050. r->flags |= IORESOURCE_UNSET;
  5051. r->end = resource_size(r) - 1;
  5052. r->start = 0;
  5053. }
  5054. pci_disable_bridge_window(dev);
  5055. }
  5056. }
  5057. static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
  5058. {
  5059. if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
  5060. count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
  5061. spin_lock(&resource_alignment_lock);
  5062. strncpy(resource_alignment_param, buf, count);
  5063. resource_alignment_param[count] = '\0';
  5064. spin_unlock(&resource_alignment_lock);
  5065. return count;
  5066. }
  5067. static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
  5068. {
  5069. size_t count;
  5070. spin_lock(&resource_alignment_lock);
  5071. count = snprintf(buf, size, "%s", resource_alignment_param);
  5072. spin_unlock(&resource_alignment_lock);
  5073. return count;
  5074. }
  5075. static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
  5076. {
  5077. return pci_get_resource_alignment_param(buf, PAGE_SIZE);
  5078. }
  5079. static ssize_t pci_resource_alignment_store(struct bus_type *bus,
  5080. const char *buf, size_t count)
  5081. {
  5082. return pci_set_resource_alignment_param(buf, count);
  5083. }
  5084. static BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
  5085. pci_resource_alignment_store);
  5086. static int __init pci_resource_alignment_sysfs_init(void)
  5087. {
  5088. return bus_create_file(&pci_bus_type,
  5089. &bus_attr_resource_alignment);
  5090. }
  5091. late_initcall(pci_resource_alignment_sysfs_init);
  5092. static void pci_no_domains(void)
  5093. {
  5094. #ifdef CONFIG_PCI_DOMAINS
  5095. pci_domains_supported = 0;
  5096. #endif
  5097. }
  5098. #ifdef CONFIG_PCI_DOMAINS_GENERIC
  5099. static atomic_t __domain_nr = ATOMIC_INIT(-1);
  5100. static int pci_get_new_domain_nr(void)
  5101. {
  5102. return atomic_inc_return(&__domain_nr);
  5103. }
  5104. static int of_pci_bus_find_domain_nr(struct device *parent)
  5105. {
  5106. static int use_dt_domains = -1;
  5107. int domain = -1;
  5108. if (parent)
  5109. domain = of_get_pci_domain_nr(parent->of_node);
  5110. /*
  5111. * Check DT domain and use_dt_domains values.
  5112. *
  5113. * If DT domain property is valid (domain >= 0) and
  5114. * use_dt_domains != 0, the DT assignment is valid since this means
  5115. * we have not previously allocated a domain number by using
  5116. * pci_get_new_domain_nr(); we should also update use_dt_domains to
  5117. * 1, to indicate that we have just assigned a domain number from
  5118. * DT.
  5119. *
  5120. * If DT domain property value is not valid (ie domain < 0), and we
  5121. * have not previously assigned a domain number from DT
  5122. * (use_dt_domains != 1) we should assign a domain number by
  5123. * using the:
  5124. *
  5125. * pci_get_new_domain_nr()
  5126. *
  5127. * API and update the use_dt_domains value to keep track of method we
  5128. * are using to assign domain numbers (use_dt_domains = 0).
  5129. *
  5130. * All other combinations imply we have a platform that is trying
  5131. * to mix domain numbers obtained from DT and pci_get_new_domain_nr(),
  5132. * which is a recipe for domain mishandling and it is prevented by
  5133. * invalidating the domain value (domain = -1) and printing a
  5134. * corresponding error.
  5135. */
  5136. if (domain >= 0 && use_dt_domains) {
  5137. use_dt_domains = 1;
  5138. } else if (domain < 0 && use_dt_domains != 1) {
  5139. use_dt_domains = 0;
  5140. domain = pci_get_new_domain_nr();
  5141. } else {
  5142. if (parent)
  5143. pr_err("Node %pOF has ", parent->of_node);
  5144. pr_err("Inconsistent \"linux,pci-domain\" property in DT\n");
  5145. domain = -1;
  5146. }
  5147. return domain;
  5148. }
  5149. int pci_bus_find_domain_nr(struct pci_bus *bus, struct device *parent)
  5150. {
  5151. return acpi_disabled ? of_pci_bus_find_domain_nr(parent) :
  5152. acpi_pci_bus_find_domain_nr(bus);
  5153. }
  5154. #endif
  5155. /**
  5156. * pci_ext_cfg_avail - can we access extended PCI config space?
  5157. *
  5158. * Returns 1 if we can access PCI extended config space (offsets
  5159. * greater than 0xff). This is the default implementation. Architecture
  5160. * implementations can override this.
  5161. */
  5162. int __weak pci_ext_cfg_avail(void)
  5163. {
  5164. return 1;
  5165. }
  5166. void __weak pci_fixup_cardbus(struct pci_bus *bus)
  5167. {
  5168. }
  5169. EXPORT_SYMBOL(pci_fixup_cardbus);
  5170. static int __init pci_setup(char *str)
  5171. {
  5172. while (str) {
  5173. char *k = strchr(str, ',');
  5174. if (k)
  5175. *k++ = 0;
  5176. if (*str && (str = pcibios_setup(str)) && *str) {
  5177. if (!strcmp(str, "nomsi")) {
  5178. pci_no_msi();
  5179. } else if (!strncmp(str, "noats", 5)) {
  5180. pr_info("PCIe: ATS is disabled\n");
  5181. pcie_ats_disabled = true;
  5182. } else if (!strcmp(str, "noaer")) {
  5183. pci_no_aer();
  5184. } else if (!strcmp(str, "earlydump")) {
  5185. pci_early_dump = true;
  5186. } else if (!strncmp(str, "realloc=", 8)) {
  5187. pci_realloc_get_opt(str + 8);
  5188. } else if (!strncmp(str, "realloc", 7)) {
  5189. pci_realloc_get_opt("on");
  5190. } else if (!strcmp(str, "nodomains")) {
  5191. pci_no_domains();
  5192. } else if (!strncmp(str, "noari", 5)) {
  5193. pcie_ari_disabled = true;
  5194. } else if (!strncmp(str, "cbiosize=", 9)) {
  5195. pci_cardbus_io_size = memparse(str + 9, &str);
  5196. } else if (!strncmp(str, "cbmemsize=", 10)) {
  5197. pci_cardbus_mem_size = memparse(str + 10, &str);
  5198. } else if (!strncmp(str, "resource_alignment=", 19)) {
  5199. pci_set_resource_alignment_param(str + 19,
  5200. strlen(str + 19));
  5201. } else if (!strncmp(str, "ecrc=", 5)) {
  5202. pcie_ecrc_get_policy(str + 5);
  5203. } else if (!strncmp(str, "hpiosize=", 9)) {
  5204. pci_hotplug_io_size = memparse(str + 9, &str);
  5205. } else if (!strncmp(str, "hpmemsize=", 10)) {
  5206. pci_hotplug_mem_size = memparse(str + 10, &str);
  5207. } else if (!strncmp(str, "hpbussize=", 10)) {
  5208. pci_hotplug_bus_size =
  5209. simple_strtoul(str + 10, &str, 0);
  5210. if (pci_hotplug_bus_size > 0xff)
  5211. pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
  5212. } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
  5213. pcie_bus_config = PCIE_BUS_TUNE_OFF;
  5214. } else if (!strncmp(str, "pcie_bus_safe", 13)) {
  5215. pcie_bus_config = PCIE_BUS_SAFE;
  5216. } else if (!strncmp(str, "pcie_bus_perf", 13)) {
  5217. pcie_bus_config = PCIE_BUS_PERFORMANCE;
  5218. } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
  5219. pcie_bus_config = PCIE_BUS_PEER2PEER;
  5220. } else if (!strncmp(str, "pcie_scan_all", 13)) {
  5221. pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
  5222. } else if (!strncmp(str, "disable_acs_redir=", 18)) {
  5223. disable_acs_redir_param = str + 18;
  5224. } else {
  5225. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  5226. str);
  5227. }
  5228. }
  5229. str = k;
  5230. }
  5231. return 0;
  5232. }
  5233. early_param("pci", pci_setup);