pci.c 163 KB

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