pci.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189
  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. static inline bool platform_pci_bridge_d3(struct pci_dev *dev)
  683. {
  684. return pci_platform_pm ? pci_platform_pm->bridge_d3(dev) : false;
  685. }
  686. /**
  687. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  688. * given PCI device
  689. * @dev: PCI device to handle.
  690. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  691. *
  692. * RETURN VALUE:
  693. * -EINVAL if the requested state is invalid.
  694. * -EIO if device does not support PCI PM or its PM capabilities register has a
  695. * wrong version, or device doesn't support the requested state.
  696. * 0 if device already is in the requested state.
  697. * 0 if device's power state has been successfully changed.
  698. */
  699. static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
  700. {
  701. u16 pmcsr;
  702. bool need_restore = false;
  703. /* Check if we're already there */
  704. if (dev->current_state == state)
  705. return 0;
  706. if (!dev->pm_cap)
  707. return -EIO;
  708. if (state < PCI_D0 || state > PCI_D3hot)
  709. return -EINVAL;
  710. /* Validate current state:
  711. * Can enter D0 from any state, but if we can only go deeper
  712. * to sleep if we're already in a low power state
  713. */
  714. if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  715. && dev->current_state > state) {
  716. pci_err(dev, "invalid power transition (from state %d to %d)\n",
  717. dev->current_state, state);
  718. return -EINVAL;
  719. }
  720. /* check if this device supports the desired state */
  721. if ((state == PCI_D1 && !dev->d1_support)
  722. || (state == PCI_D2 && !dev->d2_support))
  723. return -EIO;
  724. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  725. /* If we're (effectively) in D3, force entire word to 0.
  726. * This doesn't affect PME_Status, disables PME_En, and
  727. * sets PowerState to 0.
  728. */
  729. switch (dev->current_state) {
  730. case PCI_D0:
  731. case PCI_D1:
  732. case PCI_D2:
  733. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  734. pmcsr |= state;
  735. break;
  736. case PCI_D3hot:
  737. case PCI_D3cold:
  738. case PCI_UNKNOWN: /* Boot-up */
  739. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  740. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  741. need_restore = true;
  742. /* Fall-through: force to D0 */
  743. default:
  744. pmcsr = 0;
  745. break;
  746. }
  747. /* enter specified state */
  748. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  749. /* Mandatory power management transition delays */
  750. /* see PCI PM 1.1 5.6.1 table 18 */
  751. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  752. pci_dev_d3_sleep(dev);
  753. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  754. udelay(PCI_PM_D2_DELAY);
  755. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  756. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  757. if (dev->current_state != state && printk_ratelimit())
  758. pci_info(dev, "Refused to change power state, currently in D%d\n",
  759. dev->current_state);
  760. /*
  761. * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  762. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  763. * from D3hot to D0 _may_ perform an internal reset, thereby
  764. * going to "D0 Uninitialized" rather than "D0 Initialized".
  765. * For example, at least some versions of the 3c905B and the
  766. * 3c556B exhibit this behaviour.
  767. *
  768. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  769. * devices in a D3hot state at boot. Consequently, we need to
  770. * restore at least the BARs so that the device will be
  771. * accessible to its driver.
  772. */
  773. if (need_restore)
  774. pci_restore_bars(dev);
  775. if (dev->bus->self)
  776. pcie_aspm_pm_state_change(dev->bus->self);
  777. return 0;
  778. }
  779. /**
  780. * pci_update_current_state - Read power state of given device and cache it
  781. * @dev: PCI device to handle.
  782. * @state: State to cache in case the device doesn't have the PM capability
  783. *
  784. * The power state is read from the PMCSR register, which however is
  785. * inaccessible in D3cold. The platform firmware is therefore queried first
  786. * to detect accessibility of the register. In case the platform firmware
  787. * reports an incorrect state or the device isn't power manageable by the
  788. * platform at all, we try to detect D3cold by testing accessibility of the
  789. * vendor ID in config space.
  790. */
  791. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  792. {
  793. if (platform_pci_get_power_state(dev) == PCI_D3cold ||
  794. !pci_device_is_present(dev)) {
  795. dev->current_state = PCI_D3cold;
  796. } else if (dev->pm_cap) {
  797. u16 pmcsr;
  798. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  799. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  800. } else {
  801. dev->current_state = state;
  802. }
  803. }
  804. /**
  805. * pci_power_up - Put the given device into D0 forcibly
  806. * @dev: PCI device to power up
  807. */
  808. void pci_power_up(struct pci_dev *dev)
  809. {
  810. if (platform_pci_power_manageable(dev))
  811. platform_pci_set_power_state(dev, PCI_D0);
  812. pci_raw_set_power_state(dev, PCI_D0);
  813. pci_update_current_state(dev, PCI_D0);
  814. }
  815. /**
  816. * pci_platform_power_transition - Use platform to change device power state
  817. * @dev: PCI device to handle.
  818. * @state: State to put the device into.
  819. */
  820. static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
  821. {
  822. int error;
  823. if (platform_pci_power_manageable(dev)) {
  824. error = platform_pci_set_power_state(dev, state);
  825. if (!error)
  826. pci_update_current_state(dev, state);
  827. } else
  828. error = -ENODEV;
  829. if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
  830. dev->current_state = PCI_D0;
  831. return error;
  832. }
  833. /**
  834. * pci_wakeup - Wake up a PCI device
  835. * @pci_dev: Device to handle.
  836. * @ign: ignored parameter
  837. */
  838. static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
  839. {
  840. pci_wakeup_event(pci_dev);
  841. pm_request_resume(&pci_dev->dev);
  842. return 0;
  843. }
  844. /**
  845. * pci_wakeup_bus - Walk given bus and wake up devices on it
  846. * @bus: Top bus of the subtree to walk.
  847. */
  848. void pci_wakeup_bus(struct pci_bus *bus)
  849. {
  850. if (bus)
  851. pci_walk_bus(bus, pci_wakeup, NULL);
  852. }
  853. /**
  854. * __pci_start_power_transition - Start power transition of a PCI device
  855. * @dev: PCI device to handle.
  856. * @state: State to put the device into.
  857. */
  858. static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
  859. {
  860. if (state == PCI_D0) {
  861. pci_platform_power_transition(dev, PCI_D0);
  862. /*
  863. * Mandatory power management transition delays, see
  864. * PCI Express Base Specification Revision 2.0 Section
  865. * 6.6.1: Conventional Reset. Do not delay for
  866. * devices powered on/off by corresponding bridge,
  867. * because have already delayed for the bridge.
  868. */
  869. if (dev->runtime_d3cold) {
  870. if (dev->d3cold_delay)
  871. msleep(dev->d3cold_delay);
  872. /*
  873. * When powering on a bridge from D3cold, the
  874. * whole hierarchy may be powered on into
  875. * D0uninitialized state, resume them to give
  876. * them a chance to suspend again
  877. */
  878. pci_wakeup_bus(dev->subordinate);
  879. }
  880. }
  881. }
  882. /**
  883. * __pci_dev_set_current_state - Set current state of a PCI device
  884. * @dev: Device to handle
  885. * @data: pointer to state to be set
  886. */
  887. static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
  888. {
  889. pci_power_t state = *(pci_power_t *)data;
  890. dev->current_state = state;
  891. return 0;
  892. }
  893. /**
  894. * pci_bus_set_current_state - Walk given bus and set current state of devices
  895. * @bus: Top bus of the subtree to walk.
  896. * @state: state to be set
  897. */
  898. void pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
  899. {
  900. if (bus)
  901. pci_walk_bus(bus, __pci_dev_set_current_state, &state);
  902. }
  903. /**
  904. * __pci_complete_power_transition - Complete power transition of a PCI device
  905. * @dev: PCI device to handle.
  906. * @state: State to put the device into.
  907. *
  908. * This function should not be called directly by device drivers.
  909. */
  910. int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
  911. {
  912. int ret;
  913. if (state <= PCI_D0)
  914. return -EINVAL;
  915. ret = pci_platform_power_transition(dev, state);
  916. /* Power off the bridge may power off the whole hierarchy */
  917. if (!ret && state == PCI_D3cold)
  918. pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
  919. return ret;
  920. }
  921. EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
  922. /**
  923. * pci_set_power_state - Set the power state of a PCI device
  924. * @dev: PCI device to handle.
  925. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  926. *
  927. * Transition a device to a new power state, using the platform firmware and/or
  928. * the device's PCI PM registers.
  929. *
  930. * RETURN VALUE:
  931. * -EINVAL if the requested state is invalid.
  932. * -EIO if device does not support PCI PM or its PM capabilities register has a
  933. * wrong version, or device doesn't support the requested state.
  934. * 0 if the transition is to D1 or D2 but D1 and D2 are not supported.
  935. * 0 if device already is in the requested state.
  936. * 0 if the transition is to D3 but D3 is not supported.
  937. * 0 if device's power state has been successfully changed.
  938. */
  939. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  940. {
  941. int error;
  942. /* bound the state we're entering */
  943. if (state > PCI_D3cold)
  944. state = PCI_D3cold;
  945. else if (state < PCI_D0)
  946. state = PCI_D0;
  947. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  948. /*
  949. * If the device or the parent bridge do not support PCI PM,
  950. * ignore the request if we're doing anything other than putting
  951. * it into D0 (which would only happen on boot).
  952. */
  953. return 0;
  954. /* Check if we're already there */
  955. if (dev->current_state == state)
  956. return 0;
  957. __pci_start_power_transition(dev, state);
  958. /* This device is quirked not to be put into D3, so
  959. don't put it in D3 */
  960. if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  961. return 0;
  962. /*
  963. * To put device in D3cold, we put device into D3hot in native
  964. * way, then put device into D3cold with platform ops
  965. */
  966. error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
  967. PCI_D3hot : state);
  968. if (!__pci_complete_power_transition(dev, state))
  969. error = 0;
  970. return error;
  971. }
  972. EXPORT_SYMBOL(pci_set_power_state);
  973. /**
  974. * pci_choose_state - Choose the power state of a PCI device
  975. * @dev: PCI device to be suspended
  976. * @state: target sleep state for the whole system. This is the value
  977. * that is passed to suspend() function.
  978. *
  979. * Returns PCI power state suitable for given device and given system
  980. * message.
  981. */
  982. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  983. {
  984. pci_power_t ret;
  985. if (!dev->pm_cap)
  986. return PCI_D0;
  987. ret = platform_pci_choose_state(dev);
  988. if (ret != PCI_POWER_ERROR)
  989. return ret;
  990. switch (state.event) {
  991. case PM_EVENT_ON:
  992. return PCI_D0;
  993. case PM_EVENT_FREEZE:
  994. case PM_EVENT_PRETHAW:
  995. /* REVISIT both freeze and pre-thaw "should" use D0 */
  996. case PM_EVENT_SUSPEND:
  997. case PM_EVENT_HIBERNATE:
  998. return PCI_D3hot;
  999. default:
  1000. pci_info(dev, "unrecognized suspend event %d\n",
  1001. state.event);
  1002. BUG();
  1003. }
  1004. return PCI_D0;
  1005. }
  1006. EXPORT_SYMBOL(pci_choose_state);
  1007. #define PCI_EXP_SAVE_REGS 7
  1008. static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
  1009. u16 cap, bool extended)
  1010. {
  1011. struct pci_cap_saved_state *tmp;
  1012. hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
  1013. if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
  1014. return tmp;
  1015. }
  1016. return NULL;
  1017. }
  1018. struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
  1019. {
  1020. return _pci_find_saved_cap(dev, cap, false);
  1021. }
  1022. struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
  1023. {
  1024. return _pci_find_saved_cap(dev, cap, true);
  1025. }
  1026. static int pci_save_pcie_state(struct pci_dev *dev)
  1027. {
  1028. int i = 0;
  1029. struct pci_cap_saved_state *save_state;
  1030. u16 *cap;
  1031. if (!pci_is_pcie(dev))
  1032. return 0;
  1033. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  1034. if (!save_state) {
  1035. pci_err(dev, "buffer not found in %s\n", __func__);
  1036. return -ENOMEM;
  1037. }
  1038. cap = (u16 *)&save_state->cap.data[0];
  1039. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
  1040. pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
  1041. pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
  1042. pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
  1043. pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
  1044. pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
  1045. pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
  1046. return 0;
  1047. }
  1048. static void pci_restore_pcie_state(struct pci_dev *dev)
  1049. {
  1050. int i = 0;
  1051. struct pci_cap_saved_state *save_state;
  1052. u16 *cap;
  1053. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  1054. if (!save_state)
  1055. return;
  1056. cap = (u16 *)&save_state->cap.data[0];
  1057. pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
  1058. pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
  1059. pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
  1060. pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
  1061. pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
  1062. pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
  1063. pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
  1064. }
  1065. static int pci_save_pcix_state(struct pci_dev *dev)
  1066. {
  1067. int pos;
  1068. struct pci_cap_saved_state *save_state;
  1069. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1070. if (!pos)
  1071. return 0;
  1072. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  1073. if (!save_state) {
  1074. pci_err(dev, "buffer not found in %s\n", __func__);
  1075. return -ENOMEM;
  1076. }
  1077. pci_read_config_word(dev, pos + PCI_X_CMD,
  1078. (u16 *)save_state->cap.data);
  1079. return 0;
  1080. }
  1081. static void pci_restore_pcix_state(struct pci_dev *dev)
  1082. {
  1083. int i = 0, pos;
  1084. struct pci_cap_saved_state *save_state;
  1085. u16 *cap;
  1086. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  1087. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1088. if (!save_state || !pos)
  1089. return;
  1090. cap = (u16 *)&save_state->cap.data[0];
  1091. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  1092. }
  1093. /**
  1094. * pci_save_state - save the PCI configuration space of a device before suspending
  1095. * @dev: - PCI device that we're dealing with
  1096. */
  1097. int pci_save_state(struct pci_dev *dev)
  1098. {
  1099. int i;
  1100. /* XXX: 100% dword access ok here? */
  1101. for (i = 0; i < 16; i++)
  1102. pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
  1103. dev->state_saved = true;
  1104. i = pci_save_pcie_state(dev);
  1105. if (i != 0)
  1106. return i;
  1107. i = pci_save_pcix_state(dev);
  1108. if (i != 0)
  1109. return i;
  1110. pci_save_dpc_state(dev);
  1111. return pci_save_vc_state(dev);
  1112. }
  1113. EXPORT_SYMBOL(pci_save_state);
  1114. static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
  1115. u32 saved_val, int retry)
  1116. {
  1117. u32 val;
  1118. pci_read_config_dword(pdev, offset, &val);
  1119. if (val == saved_val)
  1120. return;
  1121. for (;;) {
  1122. pci_dbg(pdev, "restoring config space at offset %#x (was %#x, writing %#x)\n",
  1123. offset, val, saved_val);
  1124. pci_write_config_dword(pdev, offset, saved_val);
  1125. if (retry-- <= 0)
  1126. return;
  1127. pci_read_config_dword(pdev, offset, &val);
  1128. if (val == saved_val)
  1129. return;
  1130. mdelay(1);
  1131. }
  1132. }
  1133. static void pci_restore_config_space_range(struct pci_dev *pdev,
  1134. int start, int end, int retry)
  1135. {
  1136. int index;
  1137. for (index = end; index >= start; index--)
  1138. pci_restore_config_dword(pdev, 4 * index,
  1139. pdev->saved_config_space[index],
  1140. retry);
  1141. }
  1142. static void pci_restore_config_space(struct pci_dev *pdev)
  1143. {
  1144. if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  1145. pci_restore_config_space_range(pdev, 10, 15, 0);
  1146. /* Restore BARs before the command register. */
  1147. pci_restore_config_space_range(pdev, 4, 9, 10);
  1148. pci_restore_config_space_range(pdev, 0, 3, 0);
  1149. } else {
  1150. pci_restore_config_space_range(pdev, 0, 15, 0);
  1151. }
  1152. }
  1153. static void pci_restore_rebar_state(struct pci_dev *pdev)
  1154. {
  1155. unsigned int pos, nbars, i;
  1156. u32 ctrl;
  1157. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
  1158. if (!pos)
  1159. return;
  1160. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  1161. nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
  1162. PCI_REBAR_CTRL_NBAR_SHIFT;
  1163. for (i = 0; i < nbars; i++, pos += 8) {
  1164. struct resource *res;
  1165. int bar_idx, size;
  1166. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  1167. bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
  1168. res = pdev->resource + bar_idx;
  1169. size = order_base_2((resource_size(res) >> 20) | 1) - 1;
  1170. ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
  1171. ctrl |= size << PCI_REBAR_CTRL_BAR_SHIFT;
  1172. pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
  1173. }
  1174. }
  1175. /**
  1176. * pci_restore_state - Restore the saved state of a PCI device
  1177. * @dev: - PCI device that we're dealing with
  1178. */
  1179. void pci_restore_state(struct pci_dev *dev)
  1180. {
  1181. if (!dev->state_saved)
  1182. return;
  1183. /* PCI Express register must be restored first */
  1184. pci_restore_pcie_state(dev);
  1185. pci_restore_pasid_state(dev);
  1186. pci_restore_pri_state(dev);
  1187. pci_restore_ats_state(dev);
  1188. pci_restore_vc_state(dev);
  1189. pci_restore_rebar_state(dev);
  1190. pci_restore_dpc_state(dev);
  1191. pci_cleanup_aer_error_status_regs(dev);
  1192. pci_restore_config_space(dev);
  1193. pci_restore_pcix_state(dev);
  1194. pci_restore_msi_state(dev);
  1195. /* Restore ACS and IOV configuration state */
  1196. pci_enable_acs(dev);
  1197. pci_restore_iov_state(dev);
  1198. dev->state_saved = false;
  1199. }
  1200. EXPORT_SYMBOL(pci_restore_state);
  1201. struct pci_saved_state {
  1202. u32 config_space[16];
  1203. struct pci_cap_saved_data cap[0];
  1204. };
  1205. /**
  1206. * pci_store_saved_state - Allocate and return an opaque struct containing
  1207. * the device saved state.
  1208. * @dev: PCI device that we're dealing with
  1209. *
  1210. * Return NULL if no state or error.
  1211. */
  1212. struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
  1213. {
  1214. struct pci_saved_state *state;
  1215. struct pci_cap_saved_state *tmp;
  1216. struct pci_cap_saved_data *cap;
  1217. size_t size;
  1218. if (!dev->state_saved)
  1219. return NULL;
  1220. size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
  1221. hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
  1222. size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  1223. state = kzalloc(size, GFP_KERNEL);
  1224. if (!state)
  1225. return NULL;
  1226. memcpy(state->config_space, dev->saved_config_space,
  1227. sizeof(state->config_space));
  1228. cap = state->cap;
  1229. hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
  1230. size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  1231. memcpy(cap, &tmp->cap, len);
  1232. cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
  1233. }
  1234. /* Empty cap_save terminates list */
  1235. return state;
  1236. }
  1237. EXPORT_SYMBOL_GPL(pci_store_saved_state);
  1238. /**
  1239. * pci_load_saved_state - Reload the provided save state into struct pci_dev.
  1240. * @dev: PCI device that we're dealing with
  1241. * @state: Saved state returned from pci_store_saved_state()
  1242. */
  1243. int pci_load_saved_state(struct pci_dev *dev,
  1244. struct pci_saved_state *state)
  1245. {
  1246. struct pci_cap_saved_data *cap;
  1247. dev->state_saved = false;
  1248. if (!state)
  1249. return 0;
  1250. memcpy(dev->saved_config_space, state->config_space,
  1251. sizeof(state->config_space));
  1252. cap = state->cap;
  1253. while (cap->size) {
  1254. struct pci_cap_saved_state *tmp;
  1255. tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
  1256. if (!tmp || tmp->cap.size != cap->size)
  1257. return -EINVAL;
  1258. memcpy(tmp->cap.data, cap->data, tmp->cap.size);
  1259. cap = (struct pci_cap_saved_data *)((u8 *)cap +
  1260. sizeof(struct pci_cap_saved_data) + cap->size);
  1261. }
  1262. dev->state_saved = true;
  1263. return 0;
  1264. }
  1265. EXPORT_SYMBOL_GPL(pci_load_saved_state);
  1266. /**
  1267. * pci_load_and_free_saved_state - Reload the save state pointed to by state,
  1268. * and free the memory allocated for it.
  1269. * @dev: PCI device that we're dealing with
  1270. * @state: Pointer to saved state returned from pci_store_saved_state()
  1271. */
  1272. int pci_load_and_free_saved_state(struct pci_dev *dev,
  1273. struct pci_saved_state **state)
  1274. {
  1275. int ret = pci_load_saved_state(dev, *state);
  1276. kfree(*state);
  1277. *state = NULL;
  1278. return ret;
  1279. }
  1280. EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
  1281. int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
  1282. {
  1283. return pci_enable_resources(dev, bars);
  1284. }
  1285. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  1286. {
  1287. int err;
  1288. struct pci_dev *bridge;
  1289. u16 cmd;
  1290. u8 pin;
  1291. err = pci_set_power_state(dev, PCI_D0);
  1292. if (err < 0 && err != -EIO)
  1293. return err;
  1294. bridge = pci_upstream_bridge(dev);
  1295. if (bridge)
  1296. pcie_aspm_powersave_config_link(bridge);
  1297. err = pcibios_enable_device(dev, bars);
  1298. if (err < 0)
  1299. return err;
  1300. pci_fixup_device(pci_fixup_enable, dev);
  1301. if (dev->msi_enabled || dev->msix_enabled)
  1302. return 0;
  1303. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  1304. if (pin) {
  1305. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1306. if (cmd & PCI_COMMAND_INTX_DISABLE)
  1307. pci_write_config_word(dev, PCI_COMMAND,
  1308. cmd & ~PCI_COMMAND_INTX_DISABLE);
  1309. }
  1310. return 0;
  1311. }
  1312. /**
  1313. * pci_reenable_device - Resume abandoned device
  1314. * @dev: PCI device to be resumed
  1315. *
  1316. * Note this function is a backend of pci_default_resume and is not supposed
  1317. * to be called by normal code, write proper resume handler and use it instead.
  1318. */
  1319. int pci_reenable_device(struct pci_dev *dev)
  1320. {
  1321. if (pci_is_enabled(dev))
  1322. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  1323. return 0;
  1324. }
  1325. EXPORT_SYMBOL(pci_reenable_device);
  1326. static void pci_enable_bridge(struct pci_dev *dev)
  1327. {
  1328. struct pci_dev *bridge;
  1329. int retval;
  1330. bridge = pci_upstream_bridge(dev);
  1331. if (bridge)
  1332. pci_enable_bridge(bridge);
  1333. if (pci_is_enabled(dev)) {
  1334. if (!dev->is_busmaster)
  1335. pci_set_master(dev);
  1336. return;
  1337. }
  1338. retval = pci_enable_device(dev);
  1339. if (retval)
  1340. pci_err(dev, "Error enabling bridge (%d), continuing\n",
  1341. retval);
  1342. pci_set_master(dev);
  1343. }
  1344. static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
  1345. {
  1346. struct pci_dev *bridge;
  1347. int err;
  1348. int i, bars = 0;
  1349. /*
  1350. * Power state could be unknown at this point, either due to a fresh
  1351. * boot or a device removal call. So get the current power state
  1352. * so that things like MSI message writing will behave as expected
  1353. * (e.g. if the device really is in D0 at enable time).
  1354. */
  1355. if (dev->pm_cap) {
  1356. u16 pmcsr;
  1357. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1358. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  1359. }
  1360. if (atomic_inc_return(&dev->enable_cnt) > 1)
  1361. return 0; /* already enabled */
  1362. bridge = pci_upstream_bridge(dev);
  1363. if (bridge)
  1364. pci_enable_bridge(bridge);
  1365. /* only skip sriov related */
  1366. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  1367. if (dev->resource[i].flags & flags)
  1368. bars |= (1 << i);
  1369. for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
  1370. if (dev->resource[i].flags & flags)
  1371. bars |= (1 << i);
  1372. err = do_pci_enable_device(dev, bars);
  1373. if (err < 0)
  1374. atomic_dec(&dev->enable_cnt);
  1375. return err;
  1376. }
  1377. /**
  1378. * pci_enable_device_io - Initialize a device for use with IO space
  1379. * @dev: PCI device to be initialized
  1380. *
  1381. * Initialize device before it's used by a driver. Ask low-level code
  1382. * to enable I/O resources. Wake up the device if it was suspended.
  1383. * Beware, this function can fail.
  1384. */
  1385. int pci_enable_device_io(struct pci_dev *dev)
  1386. {
  1387. return pci_enable_device_flags(dev, IORESOURCE_IO);
  1388. }
  1389. EXPORT_SYMBOL(pci_enable_device_io);
  1390. /**
  1391. * pci_enable_device_mem - Initialize a device for use with Memory space
  1392. * @dev: PCI device to be initialized
  1393. *
  1394. * Initialize device before it's used by a driver. Ask low-level code
  1395. * to enable Memory resources. Wake up the device if it was suspended.
  1396. * Beware, this function can fail.
  1397. */
  1398. int pci_enable_device_mem(struct pci_dev *dev)
  1399. {
  1400. return pci_enable_device_flags(dev, IORESOURCE_MEM);
  1401. }
  1402. EXPORT_SYMBOL(pci_enable_device_mem);
  1403. /**
  1404. * pci_enable_device - Initialize device before it's used by a driver.
  1405. * @dev: PCI device to be initialized
  1406. *
  1407. * Initialize device before it's used by a driver. Ask low-level code
  1408. * to enable I/O and memory. Wake up the device if it was suspended.
  1409. * Beware, this function can fail.
  1410. *
  1411. * Note we don't actually enable the device many times if we call
  1412. * this function repeatedly (we just increment the count).
  1413. */
  1414. int pci_enable_device(struct pci_dev *dev)
  1415. {
  1416. return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  1417. }
  1418. EXPORT_SYMBOL(pci_enable_device);
  1419. /*
  1420. * Managed PCI resources. This manages device on/off, intx/msi/msix
  1421. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  1422. * there's no need to track it separately. pci_devres is initialized
  1423. * when a device is enabled using managed PCI device enable interface.
  1424. */
  1425. struct pci_devres {
  1426. unsigned int enabled:1;
  1427. unsigned int pinned:1;
  1428. unsigned int orig_intx:1;
  1429. unsigned int restore_intx:1;
  1430. unsigned int mwi:1;
  1431. u32 region_mask;
  1432. };
  1433. static void pcim_release(struct device *gendev, void *res)
  1434. {
  1435. struct pci_dev *dev = to_pci_dev(gendev);
  1436. struct pci_devres *this = res;
  1437. int i;
  1438. if (dev->msi_enabled)
  1439. pci_disable_msi(dev);
  1440. if (dev->msix_enabled)
  1441. pci_disable_msix(dev);
  1442. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  1443. if (this->region_mask & (1 << i))
  1444. pci_release_region(dev, i);
  1445. if (this->mwi)
  1446. pci_clear_mwi(dev);
  1447. if (this->restore_intx)
  1448. pci_intx(dev, this->orig_intx);
  1449. if (this->enabled && !this->pinned)
  1450. pci_disable_device(dev);
  1451. }
  1452. static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
  1453. {
  1454. struct pci_devres *dr, *new_dr;
  1455. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1456. if (dr)
  1457. return dr;
  1458. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  1459. if (!new_dr)
  1460. return NULL;
  1461. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  1462. }
  1463. static struct pci_devres *find_pci_dr(struct pci_dev *pdev)
  1464. {
  1465. if (pci_is_managed(pdev))
  1466. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1467. return NULL;
  1468. }
  1469. /**
  1470. * pcim_enable_device - Managed pci_enable_device()
  1471. * @pdev: PCI device to be initialized
  1472. *
  1473. * Managed pci_enable_device().
  1474. */
  1475. int pcim_enable_device(struct pci_dev *pdev)
  1476. {
  1477. struct pci_devres *dr;
  1478. int rc;
  1479. dr = get_pci_dr(pdev);
  1480. if (unlikely(!dr))
  1481. return -ENOMEM;
  1482. if (dr->enabled)
  1483. return 0;
  1484. rc = pci_enable_device(pdev);
  1485. if (!rc) {
  1486. pdev->is_managed = 1;
  1487. dr->enabled = 1;
  1488. }
  1489. return rc;
  1490. }
  1491. EXPORT_SYMBOL(pcim_enable_device);
  1492. /**
  1493. * pcim_pin_device - Pin managed PCI device
  1494. * @pdev: PCI device to pin
  1495. *
  1496. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  1497. * driver detach. @pdev must have been enabled with
  1498. * pcim_enable_device().
  1499. */
  1500. void pcim_pin_device(struct pci_dev *pdev)
  1501. {
  1502. struct pci_devres *dr;
  1503. dr = find_pci_dr(pdev);
  1504. WARN_ON(!dr || !dr->enabled);
  1505. if (dr)
  1506. dr->pinned = 1;
  1507. }
  1508. EXPORT_SYMBOL(pcim_pin_device);
  1509. /*
  1510. * pcibios_add_device - provide arch specific hooks when adding device dev
  1511. * @dev: the PCI device being added
  1512. *
  1513. * Permits the platform to provide architecture specific functionality when
  1514. * devices are added. This is the default implementation. Architecture
  1515. * implementations can override this.
  1516. */
  1517. int __weak pcibios_add_device(struct pci_dev *dev)
  1518. {
  1519. return 0;
  1520. }
  1521. /**
  1522. * pcibios_release_device - provide arch specific hooks when releasing device dev
  1523. * @dev: the PCI device being released
  1524. *
  1525. * Permits the platform to provide architecture specific functionality when
  1526. * devices are released. This is the default implementation. Architecture
  1527. * implementations can override this.
  1528. */
  1529. void __weak pcibios_release_device(struct pci_dev *dev) {}
  1530. /**
  1531. * pcibios_disable_device - disable arch specific PCI resources for device dev
  1532. * @dev: the PCI device to disable
  1533. *
  1534. * Disables architecture specific PCI resources for the device. This
  1535. * is the default implementation. Architecture implementations can
  1536. * override this.
  1537. */
  1538. void __weak pcibios_disable_device(struct pci_dev *dev) {}
  1539. /**
  1540. * pcibios_penalize_isa_irq - penalize an ISA IRQ
  1541. * @irq: ISA IRQ to penalize
  1542. * @active: IRQ active or not
  1543. *
  1544. * Permits the platform to provide architecture-specific functionality when
  1545. * penalizing ISA IRQs. This is the default implementation. Architecture
  1546. * implementations can override this.
  1547. */
  1548. void __weak pcibios_penalize_isa_irq(int irq, int active) {}
  1549. static void do_pci_disable_device(struct pci_dev *dev)
  1550. {
  1551. u16 pci_command;
  1552. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  1553. if (pci_command & PCI_COMMAND_MASTER) {
  1554. pci_command &= ~PCI_COMMAND_MASTER;
  1555. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  1556. }
  1557. pcibios_disable_device(dev);
  1558. }
  1559. /**
  1560. * pci_disable_enabled_device - Disable device without updating enable_cnt
  1561. * @dev: PCI device to disable
  1562. *
  1563. * NOTE: This function is a backend of PCI power management routines and is
  1564. * not supposed to be called drivers.
  1565. */
  1566. void pci_disable_enabled_device(struct pci_dev *dev)
  1567. {
  1568. if (pci_is_enabled(dev))
  1569. do_pci_disable_device(dev);
  1570. }
  1571. /**
  1572. * pci_disable_device - Disable PCI device after use
  1573. * @dev: PCI device to be disabled
  1574. *
  1575. * Signal to the system that the PCI device is not in use by the system
  1576. * anymore. This only involves disabling PCI bus-mastering, if active.
  1577. *
  1578. * Note we don't actually disable the device until all callers of
  1579. * pci_enable_device() have called pci_disable_device().
  1580. */
  1581. void pci_disable_device(struct pci_dev *dev)
  1582. {
  1583. struct pci_devres *dr;
  1584. dr = find_pci_dr(dev);
  1585. if (dr)
  1586. dr->enabled = 0;
  1587. dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
  1588. "disabling already-disabled device");
  1589. if (atomic_dec_return(&dev->enable_cnt) != 0)
  1590. return;
  1591. do_pci_disable_device(dev);
  1592. dev->is_busmaster = 0;
  1593. }
  1594. EXPORT_SYMBOL(pci_disable_device);
  1595. /**
  1596. * pcibios_set_pcie_reset_state - set reset state for device dev
  1597. * @dev: the PCIe device reset
  1598. * @state: Reset state to enter into
  1599. *
  1600. *
  1601. * Sets the PCIe reset state for the device. This is the default
  1602. * implementation. Architecture implementations can override this.
  1603. */
  1604. int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
  1605. enum pcie_reset_state state)
  1606. {
  1607. return -EINVAL;
  1608. }
  1609. /**
  1610. * pci_set_pcie_reset_state - set reset state for device dev
  1611. * @dev: the PCIe device reset
  1612. * @state: Reset state to enter into
  1613. *
  1614. *
  1615. * Sets the PCI reset state for the device.
  1616. */
  1617. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  1618. {
  1619. return pcibios_set_pcie_reset_state(dev, state);
  1620. }
  1621. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);
  1622. /**
  1623. * pcie_clear_root_pme_status - Clear root port PME interrupt status.
  1624. * @dev: PCIe root port or event collector.
  1625. */
  1626. void pcie_clear_root_pme_status(struct pci_dev *dev)
  1627. {
  1628. pcie_capability_set_dword(dev, PCI_EXP_RTSTA, PCI_EXP_RTSTA_PME);
  1629. }
  1630. /**
  1631. * pci_check_pme_status - Check if given device has generated PME.
  1632. * @dev: Device to check.
  1633. *
  1634. * Check the PME status of the device and if set, clear it and clear PME enable
  1635. * (if set). Return 'true' if PME status and PME enable were both set or
  1636. * 'false' otherwise.
  1637. */
  1638. bool pci_check_pme_status(struct pci_dev *dev)
  1639. {
  1640. int pmcsr_pos;
  1641. u16 pmcsr;
  1642. bool ret = false;
  1643. if (!dev->pm_cap)
  1644. return false;
  1645. pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
  1646. pci_read_config_word(dev, pmcsr_pos, &pmcsr);
  1647. if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
  1648. return false;
  1649. /* Clear PME status. */
  1650. pmcsr |= PCI_PM_CTRL_PME_STATUS;
  1651. if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
  1652. /* Disable PME to avoid interrupt flood. */
  1653. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1654. ret = true;
  1655. }
  1656. pci_write_config_word(dev, pmcsr_pos, pmcsr);
  1657. return ret;
  1658. }
  1659. /**
  1660. * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
  1661. * @dev: Device to handle.
  1662. * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
  1663. *
  1664. * Check if @dev has generated PME and queue a resume request for it in that
  1665. * case.
  1666. */
  1667. static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
  1668. {
  1669. if (pme_poll_reset && dev->pme_poll)
  1670. dev->pme_poll = false;
  1671. if (pci_check_pme_status(dev)) {
  1672. pci_wakeup_event(dev);
  1673. pm_request_resume(&dev->dev);
  1674. }
  1675. return 0;
  1676. }
  1677. /**
  1678. * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
  1679. * @bus: Top bus of the subtree to walk.
  1680. */
  1681. void pci_pme_wakeup_bus(struct pci_bus *bus)
  1682. {
  1683. if (bus)
  1684. pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
  1685. }
  1686. /**
  1687. * pci_pme_capable - check the capability of PCI device to generate PME#
  1688. * @dev: PCI device to handle.
  1689. * @state: PCI state from which device will issue PME#.
  1690. */
  1691. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  1692. {
  1693. if (!dev->pm_cap)
  1694. return false;
  1695. return !!(dev->pme_support & (1 << state));
  1696. }
  1697. EXPORT_SYMBOL(pci_pme_capable);
  1698. static void pci_pme_list_scan(struct work_struct *work)
  1699. {
  1700. struct pci_pme_device *pme_dev, *n;
  1701. mutex_lock(&pci_pme_list_mutex);
  1702. list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
  1703. if (pme_dev->dev->pme_poll) {
  1704. struct pci_dev *bridge;
  1705. bridge = pme_dev->dev->bus->self;
  1706. /*
  1707. * If bridge is in low power state, the
  1708. * configuration space of subordinate devices
  1709. * may be not accessible
  1710. */
  1711. if (bridge && bridge->current_state != PCI_D0)
  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 that are not power-manageable directly only signal
  1841. * wakeup on behalf of subordinate devices which is set up
  1842. * elsewhere, so skip them. However, bridges that are
  1843. * power-manageable may signal wakeup for themselves (for example,
  1844. * on a hotplug event) and they need to be covered here.
  1845. */
  1846. if (!pci_power_manageable(dev))
  1847. return 0;
  1848. /* Don't do the same thing twice in a row for one device. */
  1849. if (!!enable == !!dev->wakeup_prepared)
  1850. return 0;
  1851. /*
  1852. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  1853. * Anderson we should be doing PME# wake enable followed by ACPI wake
  1854. * enable. To disable wake-up we call the platform first, for symmetry.
  1855. */
  1856. if (enable) {
  1857. int error;
  1858. if (pci_pme_capable(dev, state))
  1859. pci_pme_active(dev, true);
  1860. else
  1861. ret = 1;
  1862. error = platform_pci_set_wakeup(dev, true);
  1863. if (ret)
  1864. ret = error;
  1865. if (!ret)
  1866. dev->wakeup_prepared = true;
  1867. } else {
  1868. platform_pci_set_wakeup(dev, false);
  1869. pci_pme_active(dev, false);
  1870. dev->wakeup_prepared = false;
  1871. }
  1872. return ret;
  1873. }
  1874. /**
  1875. * pci_enable_wake - change wakeup settings for a PCI device
  1876. * @pci_dev: Target device
  1877. * @state: PCI state from which device will issue wakeup events
  1878. * @enable: Whether or not to enable event generation
  1879. *
  1880. * If @enable is set, check device_may_wakeup() for the device before calling
  1881. * __pci_enable_wake() for it.
  1882. */
  1883. int pci_enable_wake(struct pci_dev *pci_dev, pci_power_t state, bool enable)
  1884. {
  1885. if (enable && !device_may_wakeup(&pci_dev->dev))
  1886. return -EINVAL;
  1887. return __pci_enable_wake(pci_dev, state, enable);
  1888. }
  1889. EXPORT_SYMBOL(pci_enable_wake);
  1890. /**
  1891. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  1892. * @dev: PCI device to prepare
  1893. * @enable: True to enable wake-up event generation; false to disable
  1894. *
  1895. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  1896. * and this function allows them to set that up cleanly - pci_enable_wake()
  1897. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  1898. * ordering constraints.
  1899. *
  1900. * This function only returns error code if the device is not allowed to wake
  1901. * up the system from sleep or it is not capable of generating PME# from both
  1902. * D3_hot and D3_cold and the platform is unable to enable wake-up power for it.
  1903. */
  1904. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  1905. {
  1906. return pci_pme_capable(dev, PCI_D3cold) ?
  1907. pci_enable_wake(dev, PCI_D3cold, enable) :
  1908. pci_enable_wake(dev, PCI_D3hot, enable);
  1909. }
  1910. EXPORT_SYMBOL(pci_wake_from_d3);
  1911. /**
  1912. * pci_target_state - find an appropriate low power state for a given PCI dev
  1913. * @dev: PCI device
  1914. * @wakeup: Whether or not wakeup functionality will be enabled for the device.
  1915. *
  1916. * Use underlying platform code to find a supported low power state for @dev.
  1917. * If the platform can't manage @dev, return the deepest state from which it
  1918. * can generate wake events, based on any available PME info.
  1919. */
  1920. static pci_power_t pci_target_state(struct pci_dev *dev, bool wakeup)
  1921. {
  1922. pci_power_t target_state = PCI_D3hot;
  1923. if (platform_pci_power_manageable(dev)) {
  1924. /*
  1925. * Call the platform to find the target state for the device.
  1926. */
  1927. pci_power_t state = platform_pci_choose_state(dev);
  1928. switch (state) {
  1929. case PCI_POWER_ERROR:
  1930. case PCI_UNKNOWN:
  1931. break;
  1932. case PCI_D1:
  1933. case PCI_D2:
  1934. if (pci_no_d1d2(dev))
  1935. break;
  1936. /* else: fall through */
  1937. default:
  1938. target_state = state;
  1939. }
  1940. return target_state;
  1941. }
  1942. if (!dev->pm_cap)
  1943. target_state = PCI_D0;
  1944. /*
  1945. * If the device is in D3cold even though it's not power-manageable by
  1946. * the platform, it may have been powered down by non-standard means.
  1947. * Best to let it slumber.
  1948. */
  1949. if (dev->current_state == PCI_D3cold)
  1950. target_state = PCI_D3cold;
  1951. if (wakeup) {
  1952. /*
  1953. * Find the deepest state from which the device can generate
  1954. * PME#.
  1955. */
  1956. if (dev->pme_support) {
  1957. while (target_state
  1958. && !(dev->pme_support & (1 << target_state)))
  1959. target_state--;
  1960. }
  1961. }
  1962. return target_state;
  1963. }
  1964. /**
  1965. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1966. * @dev: Device to handle.
  1967. *
  1968. * Choose the power state appropriate for the device depending on whether
  1969. * it can wake up the system and/or is power manageable by the platform
  1970. * (PCI_D3hot is the default) and put the device into that state.
  1971. */
  1972. int pci_prepare_to_sleep(struct pci_dev *dev)
  1973. {
  1974. bool wakeup = device_may_wakeup(&dev->dev);
  1975. pci_power_t target_state = pci_target_state(dev, wakeup);
  1976. int error;
  1977. if (target_state == PCI_POWER_ERROR)
  1978. return -EIO;
  1979. pci_enable_wake(dev, target_state, wakeup);
  1980. error = pci_set_power_state(dev, target_state);
  1981. if (error)
  1982. pci_enable_wake(dev, target_state, false);
  1983. return error;
  1984. }
  1985. EXPORT_SYMBOL(pci_prepare_to_sleep);
  1986. /**
  1987. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1988. * @dev: Device to handle.
  1989. *
  1990. * Disable device's system wake-up capability and put it into D0.
  1991. */
  1992. int pci_back_from_sleep(struct pci_dev *dev)
  1993. {
  1994. pci_enable_wake(dev, PCI_D0, false);
  1995. return pci_set_power_state(dev, PCI_D0);
  1996. }
  1997. EXPORT_SYMBOL(pci_back_from_sleep);
  1998. /**
  1999. * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
  2000. * @dev: PCI device being suspended.
  2001. *
  2002. * Prepare @dev to generate wake-up events at run time and put it into a low
  2003. * power state.
  2004. */
  2005. int pci_finish_runtime_suspend(struct pci_dev *dev)
  2006. {
  2007. pci_power_t target_state;
  2008. int error;
  2009. target_state = pci_target_state(dev, device_can_wakeup(&dev->dev));
  2010. if (target_state == PCI_POWER_ERROR)
  2011. return -EIO;
  2012. dev->runtime_d3cold = target_state == PCI_D3cold;
  2013. __pci_enable_wake(dev, target_state, pci_dev_run_wake(dev));
  2014. error = pci_set_power_state(dev, target_state);
  2015. if (error) {
  2016. pci_enable_wake(dev, target_state, false);
  2017. dev->runtime_d3cold = false;
  2018. }
  2019. return error;
  2020. }
  2021. /**
  2022. * pci_dev_run_wake - Check if device can generate run-time wake-up events.
  2023. * @dev: Device to check.
  2024. *
  2025. * Return true if the device itself is capable of generating wake-up events
  2026. * (through the platform or using the native PCIe PME) or if the device supports
  2027. * PME and one of its upstream bridges can generate wake-up events.
  2028. */
  2029. bool pci_dev_run_wake(struct pci_dev *dev)
  2030. {
  2031. struct pci_bus *bus = dev->bus;
  2032. if (!dev->pme_support)
  2033. return false;
  2034. /* PME-capable in principle, but not from the target power state */
  2035. if (!pci_pme_capable(dev, pci_target_state(dev, true)))
  2036. return false;
  2037. if (device_can_wakeup(&dev->dev))
  2038. return true;
  2039. while (bus->parent) {
  2040. struct pci_dev *bridge = bus->self;
  2041. if (device_can_wakeup(&bridge->dev))
  2042. return true;
  2043. bus = bus->parent;
  2044. }
  2045. /* We have reached the root bus. */
  2046. if (bus->bridge)
  2047. return device_can_wakeup(bus->bridge);
  2048. return false;
  2049. }
  2050. EXPORT_SYMBOL_GPL(pci_dev_run_wake);
  2051. /**
  2052. * pci_dev_keep_suspended - Check if the device can stay in the suspended state.
  2053. * @pci_dev: Device to check.
  2054. *
  2055. * Return 'true' if the device is runtime-suspended, it doesn't have to be
  2056. * reconfigured due to wakeup settings difference between system and runtime
  2057. * suspend and the current power state of it is suitable for the upcoming
  2058. * (system) transition.
  2059. *
  2060. * If the device is not configured for system wakeup, disable PME for it before
  2061. * returning 'true' to prevent it from waking up the system unnecessarily.
  2062. */
  2063. bool pci_dev_keep_suspended(struct pci_dev *pci_dev)
  2064. {
  2065. struct device *dev = &pci_dev->dev;
  2066. bool wakeup = device_may_wakeup(dev);
  2067. if (!pm_runtime_suspended(dev)
  2068. || pci_target_state(pci_dev, wakeup) != pci_dev->current_state
  2069. || platform_pci_need_resume(pci_dev))
  2070. return false;
  2071. /*
  2072. * At this point the device is good to go unless it's been configured
  2073. * to generate PME at the runtime suspend time, but it is not supposed
  2074. * to wake up the system. In that case, simply disable PME for it
  2075. * (it will have to be re-enabled on exit from system resume).
  2076. *
  2077. * If the device's power state is D3cold and the platform check above
  2078. * hasn't triggered, the device's configuration is suitable and we don't
  2079. * need to manipulate it at all.
  2080. */
  2081. spin_lock_irq(&dev->power.lock);
  2082. if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold &&
  2083. !wakeup)
  2084. __pci_pme_active(pci_dev, false);
  2085. spin_unlock_irq(&dev->power.lock);
  2086. return true;
  2087. }
  2088. /**
  2089. * pci_dev_complete_resume - Finalize resume from system sleep for a device.
  2090. * @pci_dev: Device to handle.
  2091. *
  2092. * If the device is runtime suspended and wakeup-capable, enable PME for it as
  2093. * it might have been disabled during the prepare phase of system suspend if
  2094. * the device was not configured for system wakeup.
  2095. */
  2096. void pci_dev_complete_resume(struct pci_dev *pci_dev)
  2097. {
  2098. struct device *dev = &pci_dev->dev;
  2099. if (!pci_dev_run_wake(pci_dev))
  2100. return;
  2101. spin_lock_irq(&dev->power.lock);
  2102. if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold)
  2103. __pci_pme_active(pci_dev, true);
  2104. spin_unlock_irq(&dev->power.lock);
  2105. }
  2106. void pci_config_pm_runtime_get(struct pci_dev *pdev)
  2107. {
  2108. struct device *dev = &pdev->dev;
  2109. struct device *parent = dev->parent;
  2110. if (parent)
  2111. pm_runtime_get_sync(parent);
  2112. pm_runtime_get_noresume(dev);
  2113. /*
  2114. * pdev->current_state is set to PCI_D3cold during suspending,
  2115. * so wait until suspending completes
  2116. */
  2117. pm_runtime_barrier(dev);
  2118. /*
  2119. * Only need to resume devices in D3cold, because config
  2120. * registers are still accessible for devices suspended but
  2121. * not in D3cold.
  2122. */
  2123. if (pdev->current_state == PCI_D3cold)
  2124. pm_runtime_resume(dev);
  2125. }
  2126. void pci_config_pm_runtime_put(struct pci_dev *pdev)
  2127. {
  2128. struct device *dev = &pdev->dev;
  2129. struct device *parent = dev->parent;
  2130. pm_runtime_put(dev);
  2131. if (parent)
  2132. pm_runtime_put_sync(parent);
  2133. }
  2134. /**
  2135. * pci_bridge_d3_possible - Is it possible to put the bridge into D3
  2136. * @bridge: Bridge to check
  2137. *
  2138. * This function checks if it is possible to move the bridge to D3.
  2139. * Currently we only allow D3 for recent enough PCIe ports and Thunderbolt.
  2140. */
  2141. bool pci_bridge_d3_possible(struct pci_dev *bridge)
  2142. {
  2143. if (!pci_is_pcie(bridge))
  2144. return false;
  2145. switch (pci_pcie_type(bridge)) {
  2146. case PCI_EXP_TYPE_ROOT_PORT:
  2147. case PCI_EXP_TYPE_UPSTREAM:
  2148. case PCI_EXP_TYPE_DOWNSTREAM:
  2149. if (pci_bridge_d3_disable)
  2150. return false;
  2151. /*
  2152. * Hotplug ports handled by firmware in System Management Mode
  2153. * may not be put into D3 by the OS (Thunderbolt on non-Macs).
  2154. */
  2155. if (bridge->is_hotplug_bridge && !pciehp_is_native(bridge))
  2156. return false;
  2157. if (pci_bridge_d3_force)
  2158. return true;
  2159. /* Even the oldest 2010 Thunderbolt controller supports D3. */
  2160. if (bridge->is_thunderbolt)
  2161. return true;
  2162. /* Platform might know better if the bridge supports D3 */
  2163. if (platform_pci_bridge_d3(bridge))
  2164. return true;
  2165. /*
  2166. * Hotplug ports handled natively by the OS were not validated
  2167. * by vendors for runtime D3 at least until 2018 because there
  2168. * was no OS support.
  2169. */
  2170. if (bridge->is_hotplug_bridge)
  2171. return false;
  2172. /*
  2173. * It should be safe to put PCIe ports from 2015 or newer
  2174. * to D3.
  2175. */
  2176. if (dmi_get_bios_year() >= 2015)
  2177. return true;
  2178. break;
  2179. }
  2180. return false;
  2181. }
  2182. static int pci_dev_check_d3cold(struct pci_dev *dev, void *data)
  2183. {
  2184. bool *d3cold_ok = data;
  2185. if (/* The device needs to be allowed to go D3cold ... */
  2186. dev->no_d3cold || !dev->d3cold_allowed ||
  2187. /* ... and if it is wakeup capable to do so from D3cold. */
  2188. (device_may_wakeup(&dev->dev) &&
  2189. !pci_pme_capable(dev, PCI_D3cold)) ||
  2190. /* If it is a bridge it must be allowed to go to D3. */
  2191. !pci_power_manageable(dev))
  2192. *d3cold_ok = false;
  2193. return !*d3cold_ok;
  2194. }
  2195. /*
  2196. * pci_bridge_d3_update - Update bridge D3 capabilities
  2197. * @dev: PCI device which is changed
  2198. *
  2199. * Update upstream bridge PM capabilities accordingly depending on if the
  2200. * device PM configuration was changed or the device is being removed. The
  2201. * change is also propagated upstream.
  2202. */
  2203. void pci_bridge_d3_update(struct pci_dev *dev)
  2204. {
  2205. bool remove = !device_is_registered(&dev->dev);
  2206. struct pci_dev *bridge;
  2207. bool d3cold_ok = true;
  2208. bridge = pci_upstream_bridge(dev);
  2209. if (!bridge || !pci_bridge_d3_possible(bridge))
  2210. return;
  2211. /*
  2212. * If D3 is currently allowed for the bridge, removing one of its
  2213. * children won't change that.
  2214. */
  2215. if (remove && bridge->bridge_d3)
  2216. return;
  2217. /*
  2218. * If D3 is currently allowed for the bridge and a child is added or
  2219. * changed, disallowance of D3 can only be caused by that child, so
  2220. * we only need to check that single device, not any of its siblings.
  2221. *
  2222. * If D3 is currently not allowed for the bridge, checking the device
  2223. * first may allow us to skip checking its siblings.
  2224. */
  2225. if (!remove)
  2226. pci_dev_check_d3cold(dev, &d3cold_ok);
  2227. /*
  2228. * If D3 is currently not allowed for the bridge, this may be caused
  2229. * either by the device being changed/removed or any of its siblings,
  2230. * so we need to go through all children to find out if one of them
  2231. * continues to block D3.
  2232. */
  2233. if (d3cold_ok && !bridge->bridge_d3)
  2234. pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold,
  2235. &d3cold_ok);
  2236. if (bridge->bridge_d3 != d3cold_ok) {
  2237. bridge->bridge_d3 = d3cold_ok;
  2238. /* Propagate change to upstream bridges */
  2239. pci_bridge_d3_update(bridge);
  2240. }
  2241. }
  2242. /**
  2243. * pci_d3cold_enable - Enable D3cold for device
  2244. * @dev: PCI device to handle
  2245. *
  2246. * This function can be used in drivers to enable D3cold from the device
  2247. * they handle. It also updates upstream PCI bridge PM capabilities
  2248. * accordingly.
  2249. */
  2250. void pci_d3cold_enable(struct pci_dev *dev)
  2251. {
  2252. if (dev->no_d3cold) {
  2253. dev->no_d3cold = false;
  2254. pci_bridge_d3_update(dev);
  2255. }
  2256. }
  2257. EXPORT_SYMBOL_GPL(pci_d3cold_enable);
  2258. /**
  2259. * pci_d3cold_disable - Disable D3cold for device
  2260. * @dev: PCI device to handle
  2261. *
  2262. * This function can be used in drivers to disable D3cold from the device
  2263. * they handle. It also updates upstream PCI bridge PM capabilities
  2264. * accordingly.
  2265. */
  2266. void pci_d3cold_disable(struct pci_dev *dev)
  2267. {
  2268. if (!dev->no_d3cold) {
  2269. dev->no_d3cold = true;
  2270. pci_bridge_d3_update(dev);
  2271. }
  2272. }
  2273. EXPORT_SYMBOL_GPL(pci_d3cold_disable);
  2274. /**
  2275. * pci_pm_init - Initialize PM functions of given PCI device
  2276. * @dev: PCI device to handle.
  2277. */
  2278. void pci_pm_init(struct pci_dev *dev)
  2279. {
  2280. int pm;
  2281. u16 pmc;
  2282. pm_runtime_forbid(&dev->dev);
  2283. pm_runtime_set_active(&dev->dev);
  2284. pm_runtime_enable(&dev->dev);
  2285. device_enable_async_suspend(&dev->dev);
  2286. dev->wakeup_prepared = false;
  2287. dev->pm_cap = 0;
  2288. dev->pme_support = 0;
  2289. /* find PCI PM capability in list */
  2290. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  2291. if (!pm)
  2292. return;
  2293. /* Check device's ability to generate PME# */
  2294. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  2295. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  2296. pci_err(dev, "unsupported PM cap regs version (%u)\n",
  2297. pmc & PCI_PM_CAP_VER_MASK);
  2298. return;
  2299. }
  2300. dev->pm_cap = pm;
  2301. dev->d3_delay = PCI_PM_D3_WAIT;
  2302. dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
  2303. dev->bridge_d3 = pci_bridge_d3_possible(dev);
  2304. dev->d3cold_allowed = true;
  2305. dev->d1_support = false;
  2306. dev->d2_support = false;
  2307. if (!pci_no_d1d2(dev)) {
  2308. if (pmc & PCI_PM_CAP_D1)
  2309. dev->d1_support = true;
  2310. if (pmc & PCI_PM_CAP_D2)
  2311. dev->d2_support = true;
  2312. if (dev->d1_support || dev->d2_support)
  2313. pci_printk(KERN_DEBUG, dev, "supports%s%s\n",
  2314. dev->d1_support ? " D1" : "",
  2315. dev->d2_support ? " D2" : "");
  2316. }
  2317. pmc &= PCI_PM_CAP_PME_MASK;
  2318. if (pmc) {
  2319. pci_printk(KERN_DEBUG, dev, "PME# supported from%s%s%s%s%s\n",
  2320. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  2321. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  2322. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  2323. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  2324. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  2325. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  2326. dev->pme_poll = true;
  2327. /*
  2328. * Make device's PM flags reflect the wake-up capability, but
  2329. * let the user space enable it to wake up the system as needed.
  2330. */
  2331. device_set_wakeup_capable(&dev->dev, true);
  2332. /* Disable the PME# generation functionality */
  2333. pci_pme_active(dev, false);
  2334. }
  2335. }
  2336. static unsigned long pci_ea_flags(struct pci_dev *dev, u8 prop)
  2337. {
  2338. unsigned long flags = IORESOURCE_PCI_FIXED | IORESOURCE_PCI_EA_BEI;
  2339. switch (prop) {
  2340. case PCI_EA_P_MEM:
  2341. case PCI_EA_P_VF_MEM:
  2342. flags |= IORESOURCE_MEM;
  2343. break;
  2344. case PCI_EA_P_MEM_PREFETCH:
  2345. case PCI_EA_P_VF_MEM_PREFETCH:
  2346. flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  2347. break;
  2348. case PCI_EA_P_IO:
  2349. flags |= IORESOURCE_IO;
  2350. break;
  2351. default:
  2352. return 0;
  2353. }
  2354. return flags;
  2355. }
  2356. static struct resource *pci_ea_get_resource(struct pci_dev *dev, u8 bei,
  2357. u8 prop)
  2358. {
  2359. if (bei <= PCI_EA_BEI_BAR5 && prop <= PCI_EA_P_IO)
  2360. return &dev->resource[bei];
  2361. #ifdef CONFIG_PCI_IOV
  2362. else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5 &&
  2363. (prop == PCI_EA_P_VF_MEM || prop == PCI_EA_P_VF_MEM_PREFETCH))
  2364. return &dev->resource[PCI_IOV_RESOURCES +
  2365. bei - PCI_EA_BEI_VF_BAR0];
  2366. #endif
  2367. else if (bei == PCI_EA_BEI_ROM)
  2368. return &dev->resource[PCI_ROM_RESOURCE];
  2369. else
  2370. return NULL;
  2371. }
  2372. /* Read an Enhanced Allocation (EA) entry */
  2373. static int pci_ea_read(struct pci_dev *dev, int offset)
  2374. {
  2375. struct resource *res;
  2376. int ent_size, ent_offset = offset;
  2377. resource_size_t start, end;
  2378. unsigned long flags;
  2379. u32 dw0, bei, base, max_offset;
  2380. u8 prop;
  2381. bool support_64 = (sizeof(resource_size_t) >= 8);
  2382. pci_read_config_dword(dev, ent_offset, &dw0);
  2383. ent_offset += 4;
  2384. /* Entry size field indicates DWORDs after 1st */
  2385. ent_size = ((dw0 & PCI_EA_ES) + 1) << 2;
  2386. if (!(dw0 & PCI_EA_ENABLE)) /* Entry not enabled */
  2387. goto out;
  2388. bei = (dw0 & PCI_EA_BEI) >> 4;
  2389. prop = (dw0 & PCI_EA_PP) >> 8;
  2390. /*
  2391. * If the Property is in the reserved range, try the Secondary
  2392. * Property instead.
  2393. */
  2394. if (prop > PCI_EA_P_BRIDGE_IO && prop < PCI_EA_P_MEM_RESERVED)
  2395. prop = (dw0 & PCI_EA_SP) >> 16;
  2396. if (prop > PCI_EA_P_BRIDGE_IO)
  2397. goto out;
  2398. res = pci_ea_get_resource(dev, bei, prop);
  2399. if (!res) {
  2400. pci_err(dev, "Unsupported EA entry BEI: %u\n", bei);
  2401. goto out;
  2402. }
  2403. flags = pci_ea_flags(dev, prop);
  2404. if (!flags) {
  2405. pci_err(dev, "Unsupported EA properties: %#x\n", prop);
  2406. goto out;
  2407. }
  2408. /* Read Base */
  2409. pci_read_config_dword(dev, ent_offset, &base);
  2410. start = (base & PCI_EA_FIELD_MASK);
  2411. ent_offset += 4;
  2412. /* Read MaxOffset */
  2413. pci_read_config_dword(dev, ent_offset, &max_offset);
  2414. ent_offset += 4;
  2415. /* Read Base MSBs (if 64-bit entry) */
  2416. if (base & PCI_EA_IS_64) {
  2417. u32 base_upper;
  2418. pci_read_config_dword(dev, ent_offset, &base_upper);
  2419. ent_offset += 4;
  2420. flags |= IORESOURCE_MEM_64;
  2421. /* entry starts above 32-bit boundary, can't use */
  2422. if (!support_64 && base_upper)
  2423. goto out;
  2424. if (support_64)
  2425. start |= ((u64)base_upper << 32);
  2426. }
  2427. end = start + (max_offset | 0x03);
  2428. /* Read MaxOffset MSBs (if 64-bit entry) */
  2429. if (max_offset & PCI_EA_IS_64) {
  2430. u32 max_offset_upper;
  2431. pci_read_config_dword(dev, ent_offset, &max_offset_upper);
  2432. ent_offset += 4;
  2433. flags |= IORESOURCE_MEM_64;
  2434. /* entry too big, can't use */
  2435. if (!support_64 && max_offset_upper)
  2436. goto out;
  2437. if (support_64)
  2438. end += ((u64)max_offset_upper << 32);
  2439. }
  2440. if (end < start) {
  2441. pci_err(dev, "EA Entry crosses address boundary\n");
  2442. goto out;
  2443. }
  2444. if (ent_size != ent_offset - offset) {
  2445. pci_err(dev, "EA Entry Size (%d) does not match length read (%d)\n",
  2446. ent_size, ent_offset - offset);
  2447. goto out;
  2448. }
  2449. res->name = pci_name(dev);
  2450. res->start = start;
  2451. res->end = end;
  2452. res->flags = flags;
  2453. if (bei <= PCI_EA_BEI_BAR5)
  2454. pci_printk(KERN_DEBUG, dev, "BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
  2455. bei, res, prop);
  2456. else if (bei == PCI_EA_BEI_ROM)
  2457. pci_printk(KERN_DEBUG, dev, "ROM: %pR (from Enhanced Allocation, properties %#02x)\n",
  2458. res, prop);
  2459. else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5)
  2460. pci_printk(KERN_DEBUG, dev, "VF BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
  2461. bei - PCI_EA_BEI_VF_BAR0, res, prop);
  2462. else
  2463. pci_printk(KERN_DEBUG, dev, "BEI %d res: %pR (from Enhanced Allocation, properties %#02x)\n",
  2464. bei, res, prop);
  2465. out:
  2466. return offset + ent_size;
  2467. }
  2468. /* Enhanced Allocation Initialization */
  2469. void pci_ea_init(struct pci_dev *dev)
  2470. {
  2471. int ea;
  2472. u8 num_ent;
  2473. int offset;
  2474. int i;
  2475. /* find PCI EA capability in list */
  2476. ea = pci_find_capability(dev, PCI_CAP_ID_EA);
  2477. if (!ea)
  2478. return;
  2479. /* determine the number of entries */
  2480. pci_bus_read_config_byte(dev->bus, dev->devfn, ea + PCI_EA_NUM_ENT,
  2481. &num_ent);
  2482. num_ent &= PCI_EA_NUM_ENT_MASK;
  2483. offset = ea + PCI_EA_FIRST_ENT;
  2484. /* Skip DWORD 2 for type 1 functions */
  2485. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  2486. offset += 4;
  2487. /* parse each EA entry */
  2488. for (i = 0; i < num_ent; ++i)
  2489. offset = pci_ea_read(dev, offset);
  2490. }
  2491. static void pci_add_saved_cap(struct pci_dev *pci_dev,
  2492. struct pci_cap_saved_state *new_cap)
  2493. {
  2494. hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
  2495. }
  2496. /**
  2497. * _pci_add_cap_save_buffer - allocate buffer for saving given
  2498. * capability registers
  2499. * @dev: the PCI device
  2500. * @cap: the capability to allocate the buffer for
  2501. * @extended: Standard or Extended capability ID
  2502. * @size: requested size of the buffer
  2503. */
  2504. static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
  2505. bool extended, unsigned int size)
  2506. {
  2507. int pos;
  2508. struct pci_cap_saved_state *save_state;
  2509. if (extended)
  2510. pos = pci_find_ext_capability(dev, cap);
  2511. else
  2512. pos = pci_find_capability(dev, cap);
  2513. if (!pos)
  2514. return 0;
  2515. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  2516. if (!save_state)
  2517. return -ENOMEM;
  2518. save_state->cap.cap_nr = cap;
  2519. save_state->cap.cap_extended = extended;
  2520. save_state->cap.size = size;
  2521. pci_add_saved_cap(dev, save_state);
  2522. return 0;
  2523. }
  2524. int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
  2525. {
  2526. return _pci_add_cap_save_buffer(dev, cap, false, size);
  2527. }
  2528. int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
  2529. {
  2530. return _pci_add_cap_save_buffer(dev, cap, true, size);
  2531. }
  2532. /**
  2533. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  2534. * @dev: the PCI device
  2535. */
  2536. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  2537. {
  2538. int error;
  2539. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
  2540. PCI_EXP_SAVE_REGS * sizeof(u16));
  2541. if (error)
  2542. pci_err(dev, "unable to preallocate PCI Express save buffer\n");
  2543. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  2544. if (error)
  2545. pci_err(dev, "unable to preallocate PCI-X save buffer\n");
  2546. pci_allocate_vc_save_buffers(dev);
  2547. }
  2548. void pci_free_cap_save_buffers(struct pci_dev *dev)
  2549. {
  2550. struct pci_cap_saved_state *tmp;
  2551. struct hlist_node *n;
  2552. hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
  2553. kfree(tmp);
  2554. }
  2555. /**
  2556. * pci_configure_ari - enable or disable ARI forwarding
  2557. * @dev: the PCI device
  2558. *
  2559. * If @dev and its upstream bridge both support ARI, enable ARI in the
  2560. * bridge. Otherwise, disable ARI in the bridge.
  2561. */
  2562. void pci_configure_ari(struct pci_dev *dev)
  2563. {
  2564. u32 cap;
  2565. struct pci_dev *bridge;
  2566. if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
  2567. return;
  2568. bridge = dev->bus->self;
  2569. if (!bridge)
  2570. return;
  2571. pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
  2572. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  2573. return;
  2574. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
  2575. pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
  2576. PCI_EXP_DEVCTL2_ARI);
  2577. bridge->ari_enabled = 1;
  2578. } else {
  2579. pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
  2580. PCI_EXP_DEVCTL2_ARI);
  2581. bridge->ari_enabled = 0;
  2582. }
  2583. }
  2584. static int pci_acs_enable;
  2585. /**
  2586. * pci_request_acs - ask for ACS to be enabled if supported
  2587. */
  2588. void pci_request_acs(void)
  2589. {
  2590. pci_acs_enable = 1;
  2591. }
  2592. static const char *disable_acs_redir_param;
  2593. /**
  2594. * pci_disable_acs_redir - disable ACS redirect capabilities
  2595. * @dev: the PCI device
  2596. *
  2597. * For only devices specified in the disable_acs_redir parameter.
  2598. */
  2599. static void pci_disable_acs_redir(struct pci_dev *dev)
  2600. {
  2601. int ret = 0;
  2602. const char *p;
  2603. int pos;
  2604. u16 ctrl;
  2605. if (!disable_acs_redir_param)
  2606. return;
  2607. p = disable_acs_redir_param;
  2608. while (*p) {
  2609. ret = pci_dev_str_match(dev, p, &p);
  2610. if (ret < 0) {
  2611. pr_info_once("PCI: Can't parse disable_acs_redir parameter: %s\n",
  2612. disable_acs_redir_param);
  2613. break;
  2614. } else if (ret == 1) {
  2615. /* Found a match */
  2616. break;
  2617. }
  2618. if (*p != ';' && *p != ',') {
  2619. /* End of param or invalid format */
  2620. break;
  2621. }
  2622. p++;
  2623. }
  2624. if (ret != 1)
  2625. return;
  2626. if (!pci_dev_specific_disable_acs_redir(dev))
  2627. return;
  2628. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  2629. if (!pos) {
  2630. pci_warn(dev, "cannot disable ACS redirect for this hardware as it does not have ACS capabilities\n");
  2631. return;
  2632. }
  2633. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  2634. /* P2P Request & Completion Redirect */
  2635. ctrl &= ~(PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_EC);
  2636. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  2637. pci_info(dev, "disabled ACS redirect\n");
  2638. }
  2639. /**
  2640. * pci_std_enable_acs - enable ACS on devices using standard ACS capabilites
  2641. * @dev: the PCI device
  2642. */
  2643. static void pci_std_enable_acs(struct pci_dev *dev)
  2644. {
  2645. int pos;
  2646. u16 cap;
  2647. u16 ctrl;
  2648. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  2649. if (!pos)
  2650. return;
  2651. pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
  2652. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  2653. /* Source Validation */
  2654. ctrl |= (cap & PCI_ACS_SV);
  2655. /* P2P Request Redirect */
  2656. ctrl |= (cap & PCI_ACS_RR);
  2657. /* P2P Completion Redirect */
  2658. ctrl |= (cap & PCI_ACS_CR);
  2659. /* Upstream Forwarding */
  2660. ctrl |= (cap & PCI_ACS_UF);
  2661. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  2662. }
  2663. /**
  2664. * pci_enable_acs - enable ACS if hardware support it
  2665. * @dev: the PCI device
  2666. */
  2667. void pci_enable_acs(struct pci_dev *dev)
  2668. {
  2669. if (!pci_acs_enable)
  2670. goto disable_acs_redir;
  2671. if (!pci_dev_specific_enable_acs(dev))
  2672. goto disable_acs_redir;
  2673. pci_std_enable_acs(dev);
  2674. disable_acs_redir:
  2675. /*
  2676. * Note: pci_disable_acs_redir() must be called even if ACS was not
  2677. * enabled by the kernel because it may have been enabled by
  2678. * platform firmware. So if we are told to disable it, we should
  2679. * always disable it after setting the kernel's default
  2680. * preferences.
  2681. */
  2682. pci_disable_acs_redir(dev);
  2683. }
  2684. static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
  2685. {
  2686. int pos;
  2687. u16 cap, ctrl;
  2688. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
  2689. if (!pos)
  2690. return false;
  2691. /*
  2692. * Except for egress control, capabilities are either required
  2693. * or only required if controllable. Features missing from the
  2694. * capability field can therefore be assumed as hard-wired enabled.
  2695. */
  2696. pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
  2697. acs_flags &= (cap | PCI_ACS_EC);
  2698. pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
  2699. return (ctrl & acs_flags) == acs_flags;
  2700. }
  2701. /**
  2702. * pci_acs_enabled - test ACS against required flags for a given device
  2703. * @pdev: device to test
  2704. * @acs_flags: required PCI ACS flags
  2705. *
  2706. * Return true if the device supports the provided flags. Automatically
  2707. * filters out flags that are not implemented on multifunction devices.
  2708. *
  2709. * Note that this interface checks the effective ACS capabilities of the
  2710. * device rather than the actual capabilities. For instance, most single
  2711. * function endpoints are not required to support ACS because they have no
  2712. * opportunity for peer-to-peer access. We therefore return 'true'
  2713. * regardless of whether the device exposes an ACS capability. This makes
  2714. * it much easier for callers of this function to ignore the actual type
  2715. * or topology of the device when testing ACS support.
  2716. */
  2717. bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
  2718. {
  2719. int ret;
  2720. ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
  2721. if (ret >= 0)
  2722. return ret > 0;
  2723. /*
  2724. * Conventional PCI and PCI-X devices never support ACS, either
  2725. * effectively or actually. The shared bus topology implies that
  2726. * any device on the bus can receive or snoop DMA.
  2727. */
  2728. if (!pci_is_pcie(pdev))
  2729. return false;
  2730. switch (pci_pcie_type(pdev)) {
  2731. /*
  2732. * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
  2733. * but since their primary interface is PCI/X, we conservatively
  2734. * handle them as we would a non-PCIe device.
  2735. */
  2736. case PCI_EXP_TYPE_PCIE_BRIDGE:
  2737. /*
  2738. * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
  2739. * applicable... must never implement an ACS Extended Capability...".
  2740. * This seems arbitrary, but we take a conservative interpretation
  2741. * of this statement.
  2742. */
  2743. case PCI_EXP_TYPE_PCI_BRIDGE:
  2744. case PCI_EXP_TYPE_RC_EC:
  2745. return false;
  2746. /*
  2747. * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
  2748. * implement ACS in order to indicate their peer-to-peer capabilities,
  2749. * regardless of whether they are single- or multi-function devices.
  2750. */
  2751. case PCI_EXP_TYPE_DOWNSTREAM:
  2752. case PCI_EXP_TYPE_ROOT_PORT:
  2753. return pci_acs_flags_enabled(pdev, acs_flags);
  2754. /*
  2755. * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
  2756. * implemented by the remaining PCIe types to indicate peer-to-peer
  2757. * capabilities, but only when they are part of a multifunction
  2758. * device. The footnote for section 6.12 indicates the specific
  2759. * PCIe types included here.
  2760. */
  2761. case PCI_EXP_TYPE_ENDPOINT:
  2762. case PCI_EXP_TYPE_UPSTREAM:
  2763. case PCI_EXP_TYPE_LEG_END:
  2764. case PCI_EXP_TYPE_RC_END:
  2765. if (!pdev->multifunction)
  2766. break;
  2767. return pci_acs_flags_enabled(pdev, acs_flags);
  2768. }
  2769. /*
  2770. * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable
  2771. * to single function devices with the exception of downstream ports.
  2772. */
  2773. return true;
  2774. }
  2775. /**
  2776. * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
  2777. * @start: starting downstream device
  2778. * @end: ending upstream device or NULL to search to the root bus
  2779. * @acs_flags: required flags
  2780. *
  2781. * Walk up a device tree from start to end testing PCI ACS support. If
  2782. * any step along the way does not support the required flags, return false.
  2783. */
  2784. bool pci_acs_path_enabled(struct pci_dev *start,
  2785. struct pci_dev *end, u16 acs_flags)
  2786. {
  2787. struct pci_dev *pdev, *parent = start;
  2788. do {
  2789. pdev = parent;
  2790. if (!pci_acs_enabled(pdev, acs_flags))
  2791. return false;
  2792. if (pci_is_root_bus(pdev->bus))
  2793. return (end == NULL);
  2794. parent = pdev->bus->self;
  2795. } while (pdev != end);
  2796. return true;
  2797. }
  2798. /**
  2799. * pci_rebar_find_pos - find position of resize ctrl reg for BAR
  2800. * @pdev: PCI device
  2801. * @bar: BAR to find
  2802. *
  2803. * Helper to find the position of the ctrl register for a BAR.
  2804. * Returns -ENOTSUPP if resizable BARs are not supported at all.
  2805. * Returns -ENOENT if no ctrl register for the BAR could be found.
  2806. */
  2807. static int pci_rebar_find_pos(struct pci_dev *pdev, int bar)
  2808. {
  2809. unsigned int pos, nbars, i;
  2810. u32 ctrl;
  2811. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
  2812. if (!pos)
  2813. return -ENOTSUPP;
  2814. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2815. nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
  2816. PCI_REBAR_CTRL_NBAR_SHIFT;
  2817. for (i = 0; i < nbars; i++, pos += 8) {
  2818. int bar_idx;
  2819. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2820. bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
  2821. if (bar_idx == bar)
  2822. return pos;
  2823. }
  2824. return -ENOENT;
  2825. }
  2826. /**
  2827. * pci_rebar_get_possible_sizes - get possible sizes for BAR
  2828. * @pdev: PCI device
  2829. * @bar: BAR to query
  2830. *
  2831. * Get the possible sizes of a resizable BAR as bitmask defined in the spec
  2832. * (bit 0=1MB, bit 19=512GB). Returns 0 if BAR isn't resizable.
  2833. */
  2834. u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar)
  2835. {
  2836. int pos;
  2837. u32 cap;
  2838. pos = pci_rebar_find_pos(pdev, bar);
  2839. if (pos < 0)
  2840. return 0;
  2841. pci_read_config_dword(pdev, pos + PCI_REBAR_CAP, &cap);
  2842. return (cap & PCI_REBAR_CAP_SIZES) >> 4;
  2843. }
  2844. /**
  2845. * pci_rebar_get_current_size - get the current size of a BAR
  2846. * @pdev: PCI device
  2847. * @bar: BAR to set size to
  2848. *
  2849. * Read the size of a BAR from the resizable BAR config.
  2850. * Returns size if found or negative error code.
  2851. */
  2852. int pci_rebar_get_current_size(struct pci_dev *pdev, int bar)
  2853. {
  2854. int pos;
  2855. u32 ctrl;
  2856. pos = pci_rebar_find_pos(pdev, bar);
  2857. if (pos < 0)
  2858. return pos;
  2859. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2860. return (ctrl & PCI_REBAR_CTRL_BAR_SIZE) >> PCI_REBAR_CTRL_BAR_SHIFT;
  2861. }
  2862. /**
  2863. * pci_rebar_set_size - set a new size for a BAR
  2864. * @pdev: PCI device
  2865. * @bar: BAR to set size to
  2866. * @size: new size as defined in the spec (0=1MB, 19=512GB)
  2867. *
  2868. * Set the new size of a BAR as defined in the spec.
  2869. * Returns zero if resizing was successful, error code otherwise.
  2870. */
  2871. int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size)
  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. ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
  2880. ctrl |= size << PCI_REBAR_CTRL_BAR_SHIFT;
  2881. pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
  2882. return 0;
  2883. }
  2884. /**
  2885. * pci_enable_atomic_ops_to_root - enable AtomicOp requests to root port
  2886. * @dev: the PCI device
  2887. * @cap_mask: mask of desired AtomicOp sizes, including one or more of:
  2888. * PCI_EXP_DEVCAP2_ATOMIC_COMP32
  2889. * PCI_EXP_DEVCAP2_ATOMIC_COMP64
  2890. * PCI_EXP_DEVCAP2_ATOMIC_COMP128
  2891. *
  2892. * Return 0 if all upstream bridges support AtomicOp routing, egress
  2893. * blocking is disabled on all upstream ports, and the root port supports
  2894. * the requested completion capabilities (32-bit, 64-bit and/or 128-bit
  2895. * AtomicOp completion), or negative otherwise.
  2896. */
  2897. int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask)
  2898. {
  2899. struct pci_bus *bus = dev->bus;
  2900. struct pci_dev *bridge;
  2901. u32 cap, ctl2;
  2902. if (!pci_is_pcie(dev))
  2903. return -EINVAL;
  2904. /*
  2905. * Per PCIe r4.0, sec 6.15, endpoints and root ports may be
  2906. * AtomicOp requesters. For now, we only support endpoints as
  2907. * requesters and root ports as completers. No endpoints as
  2908. * completers, and no peer-to-peer.
  2909. */
  2910. switch (pci_pcie_type(dev)) {
  2911. case PCI_EXP_TYPE_ENDPOINT:
  2912. case PCI_EXP_TYPE_LEG_END:
  2913. case PCI_EXP_TYPE_RC_END:
  2914. break;
  2915. default:
  2916. return -EINVAL;
  2917. }
  2918. while (bus->parent) {
  2919. bridge = bus->self;
  2920. pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
  2921. switch (pci_pcie_type(bridge)) {
  2922. /* Ensure switch ports support AtomicOp routing */
  2923. case PCI_EXP_TYPE_UPSTREAM:
  2924. case PCI_EXP_TYPE_DOWNSTREAM:
  2925. if (!(cap & PCI_EXP_DEVCAP2_ATOMIC_ROUTE))
  2926. return -EINVAL;
  2927. break;
  2928. /* Ensure root port supports all the sizes we care about */
  2929. case PCI_EXP_TYPE_ROOT_PORT:
  2930. if ((cap & cap_mask) != cap_mask)
  2931. return -EINVAL;
  2932. break;
  2933. }
  2934. /* Ensure upstream ports don't block AtomicOps on egress */
  2935. if (!bridge->has_secondary_link) {
  2936. pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2,
  2937. &ctl2);
  2938. if (ctl2 & PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK)
  2939. return -EINVAL;
  2940. }
  2941. bus = bus->parent;
  2942. }
  2943. pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
  2944. PCI_EXP_DEVCTL2_ATOMIC_REQ);
  2945. return 0;
  2946. }
  2947. EXPORT_SYMBOL(pci_enable_atomic_ops_to_root);
  2948. /**
  2949. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  2950. * @dev: the PCI device
  2951. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
  2952. *
  2953. * Perform INTx swizzling for a device behind one level of bridge. This is
  2954. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  2955. * behind bridges on add-in cards. For devices with ARI enabled, the slot
  2956. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  2957. * the PCI Express Base Specification, Revision 2.1)
  2958. */
  2959. u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
  2960. {
  2961. int slot;
  2962. if (pci_ari_enabled(dev->bus))
  2963. slot = 0;
  2964. else
  2965. slot = PCI_SLOT(dev->devfn);
  2966. return (((pin - 1) + slot) % 4) + 1;
  2967. }
  2968. int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  2969. {
  2970. u8 pin;
  2971. pin = dev->pin;
  2972. if (!pin)
  2973. return -1;
  2974. while (!pci_is_root_bus(dev->bus)) {
  2975. pin = pci_swizzle_interrupt_pin(dev, pin);
  2976. dev = dev->bus->self;
  2977. }
  2978. *bridge = dev;
  2979. return pin;
  2980. }
  2981. /**
  2982. * pci_common_swizzle - swizzle INTx all the way to root bridge
  2983. * @dev: the PCI device
  2984. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  2985. *
  2986. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  2987. * bridges all the way up to a PCI root bus.
  2988. */
  2989. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  2990. {
  2991. u8 pin = *pinp;
  2992. while (!pci_is_root_bus(dev->bus)) {
  2993. pin = pci_swizzle_interrupt_pin(dev, pin);
  2994. dev = dev->bus->self;
  2995. }
  2996. *pinp = pin;
  2997. return PCI_SLOT(dev->devfn);
  2998. }
  2999. EXPORT_SYMBOL_GPL(pci_common_swizzle);
  3000. /**
  3001. * pci_release_region - Release a PCI bar
  3002. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  3003. * @bar: BAR to release
  3004. *
  3005. * Releases the PCI I/O and memory resources previously reserved by a
  3006. * successful call to pci_request_region. Call this function only
  3007. * after all use of the PCI regions has ceased.
  3008. */
  3009. void pci_release_region(struct pci_dev *pdev, int bar)
  3010. {
  3011. struct pci_devres *dr;
  3012. if (pci_resource_len(pdev, bar) == 0)
  3013. return;
  3014. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  3015. release_region(pci_resource_start(pdev, bar),
  3016. pci_resource_len(pdev, bar));
  3017. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  3018. release_mem_region(pci_resource_start(pdev, bar),
  3019. pci_resource_len(pdev, bar));
  3020. dr = find_pci_dr(pdev);
  3021. if (dr)
  3022. dr->region_mask &= ~(1 << bar);
  3023. }
  3024. EXPORT_SYMBOL(pci_release_region);
  3025. /**
  3026. * __pci_request_region - Reserved PCI I/O and memory resource
  3027. * @pdev: PCI device whose resources are to be reserved
  3028. * @bar: BAR to be reserved
  3029. * @res_name: Name to be associated with resource.
  3030. * @exclusive: whether the region access is exclusive or not
  3031. *
  3032. * Mark the PCI region associated with PCI device @pdev BR @bar as
  3033. * being reserved by owner @res_name. Do not access any
  3034. * address inside the PCI regions unless this call returns
  3035. * successfully.
  3036. *
  3037. * If @exclusive is set, then the region is marked so that userspace
  3038. * is explicitly not allowed to map the resource via /dev/mem or
  3039. * sysfs MMIO access.
  3040. *
  3041. * Returns 0 on success, or %EBUSY on error. A warning
  3042. * message is also printed on failure.
  3043. */
  3044. static int __pci_request_region(struct pci_dev *pdev, int bar,
  3045. const char *res_name, int exclusive)
  3046. {
  3047. struct pci_devres *dr;
  3048. if (pci_resource_len(pdev, bar) == 0)
  3049. return 0;
  3050. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  3051. if (!request_region(pci_resource_start(pdev, bar),
  3052. pci_resource_len(pdev, bar), res_name))
  3053. goto err_out;
  3054. } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  3055. if (!__request_mem_region(pci_resource_start(pdev, bar),
  3056. pci_resource_len(pdev, bar), res_name,
  3057. exclusive))
  3058. goto err_out;
  3059. }
  3060. dr = find_pci_dr(pdev);
  3061. if (dr)
  3062. dr->region_mask |= 1 << bar;
  3063. return 0;
  3064. err_out:
  3065. pci_warn(pdev, "BAR %d: can't reserve %pR\n", bar,
  3066. &pdev->resource[bar]);
  3067. return -EBUSY;
  3068. }
  3069. /**
  3070. * pci_request_region - Reserve PCI I/O and memory resource
  3071. * @pdev: PCI device whose resources are to be reserved
  3072. * @bar: BAR to be reserved
  3073. * @res_name: Name to be associated with resource
  3074. *
  3075. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  3076. * being reserved by owner @res_name. Do not access any
  3077. * address inside the PCI regions unless this call returns
  3078. * successfully.
  3079. *
  3080. * Returns 0 on success, or %EBUSY on error. A warning
  3081. * message is also printed on failure.
  3082. */
  3083. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  3084. {
  3085. return __pci_request_region(pdev, bar, res_name, 0);
  3086. }
  3087. EXPORT_SYMBOL(pci_request_region);
  3088. /**
  3089. * pci_request_region_exclusive - Reserved 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 BR @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. * The key difference that _exclusive makes it that userspace is
  3103. * explicitly not allowed to map the resource via /dev/mem or
  3104. * sysfs.
  3105. */
  3106. int pci_request_region_exclusive(struct pci_dev *pdev, int bar,
  3107. const char *res_name)
  3108. {
  3109. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  3110. }
  3111. EXPORT_SYMBOL(pci_request_region_exclusive);
  3112. /**
  3113. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  3114. * @pdev: PCI device whose resources were previously reserved
  3115. * @bars: Bitmask of BARs to be released
  3116. *
  3117. * Release selected PCI I/O and memory resources previously reserved.
  3118. * Call this function only after all use of the PCI regions has ceased.
  3119. */
  3120. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  3121. {
  3122. int i;
  3123. for (i = 0; i < 6; i++)
  3124. if (bars & (1 << i))
  3125. pci_release_region(pdev, i);
  3126. }
  3127. EXPORT_SYMBOL(pci_release_selected_regions);
  3128. static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  3129. const char *res_name, int excl)
  3130. {
  3131. int i;
  3132. for (i = 0; i < 6; i++)
  3133. if (bars & (1 << i))
  3134. if (__pci_request_region(pdev, i, res_name, excl))
  3135. goto err_out;
  3136. return 0;
  3137. err_out:
  3138. while (--i >= 0)
  3139. if (bars & (1 << i))
  3140. pci_release_region(pdev, i);
  3141. return -EBUSY;
  3142. }
  3143. /**
  3144. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  3145. * @pdev: PCI device whose resources are to be reserved
  3146. * @bars: Bitmask of BARs to be requested
  3147. * @res_name: Name to be associated with resource
  3148. */
  3149. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  3150. const char *res_name)
  3151. {
  3152. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  3153. }
  3154. EXPORT_SYMBOL(pci_request_selected_regions);
  3155. int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars,
  3156. const char *res_name)
  3157. {
  3158. return __pci_request_selected_regions(pdev, bars, res_name,
  3159. IORESOURCE_EXCLUSIVE);
  3160. }
  3161. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  3162. /**
  3163. * pci_release_regions - Release reserved PCI I/O and memory resources
  3164. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  3165. *
  3166. * Releases all PCI I/O and memory resources previously reserved by a
  3167. * successful call to pci_request_regions. Call this function only
  3168. * after all use of the PCI regions has ceased.
  3169. */
  3170. void pci_release_regions(struct pci_dev *pdev)
  3171. {
  3172. pci_release_selected_regions(pdev, (1 << 6) - 1);
  3173. }
  3174. EXPORT_SYMBOL(pci_release_regions);
  3175. /**
  3176. * pci_request_regions - Reserved PCI I/O and memory resources
  3177. * @pdev: PCI device whose resources are to be reserved
  3178. * @res_name: Name to be associated with resource.
  3179. *
  3180. * Mark all PCI regions associated with PCI device @pdev as
  3181. * being reserved by owner @res_name. Do not access any
  3182. * address inside the PCI regions unless this call returns
  3183. * successfully.
  3184. *
  3185. * Returns 0 on success, or %EBUSY on error. A warning
  3186. * message is also printed on failure.
  3187. */
  3188. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  3189. {
  3190. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  3191. }
  3192. EXPORT_SYMBOL(pci_request_regions);
  3193. /**
  3194. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  3195. * @pdev: PCI device whose resources are to be reserved
  3196. * @res_name: Name to be associated with resource.
  3197. *
  3198. * Mark all PCI regions associated with PCI device @pdev as
  3199. * being reserved by owner @res_name. Do not access any
  3200. * address inside the PCI regions unless this call returns
  3201. * successfully.
  3202. *
  3203. * pci_request_regions_exclusive() will mark the region so that
  3204. * /dev/mem and the sysfs MMIO access will not be allowed.
  3205. *
  3206. * Returns 0 on success, or %EBUSY on error. A warning
  3207. * message is also printed on failure.
  3208. */
  3209. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  3210. {
  3211. return pci_request_selected_regions_exclusive(pdev,
  3212. ((1 << 6) - 1), res_name);
  3213. }
  3214. EXPORT_SYMBOL(pci_request_regions_exclusive);
  3215. /*
  3216. * Record the PCI IO range (expressed as CPU physical address + size).
  3217. * Return a negative value if an error has occured, zero otherwise
  3218. */
  3219. int pci_register_io_range(struct fwnode_handle *fwnode, phys_addr_t addr,
  3220. resource_size_t size)
  3221. {
  3222. int ret = 0;
  3223. #ifdef PCI_IOBASE
  3224. struct logic_pio_hwaddr *range;
  3225. if (!size || addr + size < addr)
  3226. return -EINVAL;
  3227. range = kzalloc(sizeof(*range), GFP_ATOMIC);
  3228. if (!range)
  3229. return -ENOMEM;
  3230. range->fwnode = fwnode;
  3231. range->size = size;
  3232. range->hw_start = addr;
  3233. range->flags = LOGIC_PIO_CPU_MMIO;
  3234. ret = logic_pio_register_range(range);
  3235. if (ret)
  3236. kfree(range);
  3237. #endif
  3238. return ret;
  3239. }
  3240. phys_addr_t pci_pio_to_address(unsigned long pio)
  3241. {
  3242. phys_addr_t address = (phys_addr_t)OF_BAD_ADDR;
  3243. #ifdef PCI_IOBASE
  3244. if (pio >= MMIO_UPPER_LIMIT)
  3245. return address;
  3246. address = logic_pio_to_hwaddr(pio);
  3247. #endif
  3248. return address;
  3249. }
  3250. unsigned long __weak pci_address_to_pio(phys_addr_t address)
  3251. {
  3252. #ifdef PCI_IOBASE
  3253. return logic_pio_trans_cpuaddr(address);
  3254. #else
  3255. if (address > IO_SPACE_LIMIT)
  3256. return (unsigned long)-1;
  3257. return (unsigned long) address;
  3258. #endif
  3259. }
  3260. /**
  3261. * pci_remap_iospace - Remap the memory mapped I/O space
  3262. * @res: Resource describing the I/O space
  3263. * @phys_addr: physical address of range to be mapped
  3264. *
  3265. * Remap the memory mapped I/O space described by the @res
  3266. * and the CPU physical address @phys_addr into virtual address space.
  3267. * Only architectures that have memory mapped IO functions defined
  3268. * (and the PCI_IOBASE value defined) should call this function.
  3269. */
  3270. int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
  3271. {
  3272. #if defined(PCI_IOBASE) && defined(CONFIG_MMU)
  3273. unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
  3274. if (!(res->flags & IORESOURCE_IO))
  3275. return -EINVAL;
  3276. if (res->end > IO_SPACE_LIMIT)
  3277. return -EINVAL;
  3278. return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr,
  3279. pgprot_device(PAGE_KERNEL));
  3280. #else
  3281. /* this architecture does not have memory mapped I/O space,
  3282. so this function should never be called */
  3283. WARN_ONCE(1, "This architecture does not support memory mapped I/O\n");
  3284. return -ENODEV;
  3285. #endif
  3286. }
  3287. EXPORT_SYMBOL(pci_remap_iospace);
  3288. /**
  3289. * pci_unmap_iospace - Unmap the memory mapped I/O space
  3290. * @res: resource to be unmapped
  3291. *
  3292. * Unmap the CPU virtual address @res from virtual address space.
  3293. * Only architectures that have memory mapped IO functions defined
  3294. * (and the PCI_IOBASE value defined) should call this function.
  3295. */
  3296. void pci_unmap_iospace(struct resource *res)
  3297. {
  3298. #if defined(PCI_IOBASE) && defined(CONFIG_MMU)
  3299. unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
  3300. unmap_kernel_range(vaddr, resource_size(res));
  3301. #endif
  3302. }
  3303. EXPORT_SYMBOL(pci_unmap_iospace);
  3304. static void devm_pci_unmap_iospace(struct device *dev, void *ptr)
  3305. {
  3306. struct resource **res = ptr;
  3307. pci_unmap_iospace(*res);
  3308. }
  3309. /**
  3310. * devm_pci_remap_iospace - Managed pci_remap_iospace()
  3311. * @dev: Generic device to remap IO address for
  3312. * @res: Resource describing the I/O space
  3313. * @phys_addr: physical address of range to be mapped
  3314. *
  3315. * Managed pci_remap_iospace(). Map is automatically unmapped on driver
  3316. * detach.
  3317. */
  3318. int devm_pci_remap_iospace(struct device *dev, const struct resource *res,
  3319. phys_addr_t phys_addr)
  3320. {
  3321. const struct resource **ptr;
  3322. int error;
  3323. ptr = devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL);
  3324. if (!ptr)
  3325. return -ENOMEM;
  3326. error = pci_remap_iospace(res, phys_addr);
  3327. if (error) {
  3328. devres_free(ptr);
  3329. } else {
  3330. *ptr = res;
  3331. devres_add(dev, ptr);
  3332. }
  3333. return error;
  3334. }
  3335. EXPORT_SYMBOL(devm_pci_remap_iospace);
  3336. /**
  3337. * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
  3338. * @dev: Generic device to remap IO address for
  3339. * @offset: Resource address to map
  3340. * @size: Size of map
  3341. *
  3342. * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver
  3343. * detach.
  3344. */
  3345. void __iomem *devm_pci_remap_cfgspace(struct device *dev,
  3346. resource_size_t offset,
  3347. resource_size_t size)
  3348. {
  3349. void __iomem **ptr, *addr;
  3350. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  3351. if (!ptr)
  3352. return NULL;
  3353. addr = pci_remap_cfgspace(offset, size);
  3354. if (addr) {
  3355. *ptr = addr;
  3356. devres_add(dev, ptr);
  3357. } else
  3358. devres_free(ptr);
  3359. return addr;
  3360. }
  3361. EXPORT_SYMBOL(devm_pci_remap_cfgspace);
  3362. /**
  3363. * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
  3364. * @dev: generic device to handle the resource for
  3365. * @res: configuration space resource to be handled
  3366. *
  3367. * Checks that a resource is a valid memory region, requests the memory
  3368. * region and ioremaps with pci_remap_cfgspace() API that ensures the
  3369. * proper PCI configuration space memory attributes are guaranteed.
  3370. *
  3371. * All operations are managed and will be undone on driver detach.
  3372. *
  3373. * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
  3374. * on failure. Usage example::
  3375. *
  3376. * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3377. * base = devm_pci_remap_cfg_resource(&pdev->dev, res);
  3378. * if (IS_ERR(base))
  3379. * return PTR_ERR(base);
  3380. */
  3381. void __iomem *devm_pci_remap_cfg_resource(struct device *dev,
  3382. struct resource *res)
  3383. {
  3384. resource_size_t size;
  3385. const char *name;
  3386. void __iomem *dest_ptr;
  3387. BUG_ON(!dev);
  3388. if (!res || resource_type(res) != IORESOURCE_MEM) {
  3389. dev_err(dev, "invalid resource\n");
  3390. return IOMEM_ERR_PTR(-EINVAL);
  3391. }
  3392. size = resource_size(res);
  3393. name = res->name ?: dev_name(dev);
  3394. if (!devm_request_mem_region(dev, res->start, size, name)) {
  3395. dev_err(dev, "can't request region for resource %pR\n", res);
  3396. return IOMEM_ERR_PTR(-EBUSY);
  3397. }
  3398. dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
  3399. if (!dest_ptr) {
  3400. dev_err(dev, "ioremap failed for resource %pR\n", res);
  3401. devm_release_mem_region(dev, res->start, size);
  3402. dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
  3403. }
  3404. return dest_ptr;
  3405. }
  3406. EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
  3407. static void __pci_set_master(struct pci_dev *dev, bool enable)
  3408. {
  3409. u16 old_cmd, cmd;
  3410. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  3411. if (enable)
  3412. cmd = old_cmd | PCI_COMMAND_MASTER;
  3413. else
  3414. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  3415. if (cmd != old_cmd) {
  3416. pci_dbg(dev, "%s bus mastering\n",
  3417. enable ? "enabling" : "disabling");
  3418. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3419. }
  3420. dev->is_busmaster = enable;
  3421. }
  3422. /**
  3423. * pcibios_setup - process "pci=" kernel boot arguments
  3424. * @str: string used to pass in "pci=" kernel boot arguments
  3425. *
  3426. * Process kernel boot arguments. This is the default implementation.
  3427. * Architecture specific implementations can override this as necessary.
  3428. */
  3429. char * __weak __init pcibios_setup(char *str)
  3430. {
  3431. return str;
  3432. }
  3433. /**
  3434. * pcibios_set_master - enable PCI bus-mastering for device dev
  3435. * @dev: the PCI device to enable
  3436. *
  3437. * Enables PCI bus-mastering for the device. This is the default
  3438. * implementation. Architecture specific implementations can override
  3439. * this if necessary.
  3440. */
  3441. void __weak pcibios_set_master(struct pci_dev *dev)
  3442. {
  3443. u8 lat;
  3444. /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
  3445. if (pci_is_pcie(dev))
  3446. return;
  3447. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  3448. if (lat < 16)
  3449. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  3450. else if (lat > pcibios_max_latency)
  3451. lat = pcibios_max_latency;
  3452. else
  3453. return;
  3454. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  3455. }
  3456. /**
  3457. * pci_set_master - enables bus-mastering for device dev
  3458. * @dev: the PCI device to enable
  3459. *
  3460. * Enables bus-mastering on the device and calls pcibios_set_master()
  3461. * to do the needed arch specific settings.
  3462. */
  3463. void pci_set_master(struct pci_dev *dev)
  3464. {
  3465. __pci_set_master(dev, true);
  3466. pcibios_set_master(dev);
  3467. }
  3468. EXPORT_SYMBOL(pci_set_master);
  3469. /**
  3470. * pci_clear_master - disables bus-mastering for device dev
  3471. * @dev: the PCI device to disable
  3472. */
  3473. void pci_clear_master(struct pci_dev *dev)
  3474. {
  3475. __pci_set_master(dev, false);
  3476. }
  3477. EXPORT_SYMBOL(pci_clear_master);
  3478. /**
  3479. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  3480. * @dev: the PCI device for which MWI is to be enabled
  3481. *
  3482. * Helper function for pci_set_mwi.
  3483. * Originally copied from drivers/net/acenic.c.
  3484. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  3485. *
  3486. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3487. */
  3488. int pci_set_cacheline_size(struct pci_dev *dev)
  3489. {
  3490. u8 cacheline_size;
  3491. if (!pci_cache_line_size)
  3492. return -EINVAL;
  3493. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  3494. equal to or multiple of the right value. */
  3495. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  3496. if (cacheline_size >= pci_cache_line_size &&
  3497. (cacheline_size % pci_cache_line_size) == 0)
  3498. return 0;
  3499. /* Write the correct value. */
  3500. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  3501. /* Read it back. */
  3502. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  3503. if (cacheline_size == pci_cache_line_size)
  3504. return 0;
  3505. pci_printk(KERN_DEBUG, dev, "cache line size of %d is not supported\n",
  3506. pci_cache_line_size << 2);
  3507. return -EINVAL;
  3508. }
  3509. EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
  3510. /**
  3511. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  3512. * @dev: the PCI device for which MWI is enabled
  3513. *
  3514. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  3515. *
  3516. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3517. */
  3518. int pci_set_mwi(struct pci_dev *dev)
  3519. {
  3520. #ifdef PCI_DISABLE_MWI
  3521. return 0;
  3522. #else
  3523. int rc;
  3524. u16 cmd;
  3525. rc = pci_set_cacheline_size(dev);
  3526. if (rc)
  3527. return rc;
  3528. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  3529. if (!(cmd & PCI_COMMAND_INVALIDATE)) {
  3530. pci_dbg(dev, "enabling Mem-Wr-Inval\n");
  3531. cmd |= PCI_COMMAND_INVALIDATE;
  3532. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3533. }
  3534. return 0;
  3535. #endif
  3536. }
  3537. EXPORT_SYMBOL(pci_set_mwi);
  3538. /**
  3539. * pcim_set_mwi - a device-managed pci_set_mwi()
  3540. * @dev: the PCI device for which MWI is enabled
  3541. *
  3542. * Managed pci_set_mwi().
  3543. *
  3544. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3545. */
  3546. int pcim_set_mwi(struct pci_dev *dev)
  3547. {
  3548. struct pci_devres *dr;
  3549. dr = find_pci_dr(dev);
  3550. if (!dr)
  3551. return -ENOMEM;
  3552. dr->mwi = 1;
  3553. return pci_set_mwi(dev);
  3554. }
  3555. EXPORT_SYMBOL(pcim_set_mwi);
  3556. /**
  3557. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  3558. * @dev: the PCI device for which MWI is enabled
  3559. *
  3560. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  3561. * Callers are not required to check the return value.
  3562. *
  3563. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3564. */
  3565. int pci_try_set_mwi(struct pci_dev *dev)
  3566. {
  3567. #ifdef PCI_DISABLE_MWI
  3568. return 0;
  3569. #else
  3570. return pci_set_mwi(dev);
  3571. #endif
  3572. }
  3573. EXPORT_SYMBOL(pci_try_set_mwi);
  3574. /**
  3575. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  3576. * @dev: the PCI device to disable
  3577. *
  3578. * Disables PCI Memory-Write-Invalidate transaction on the device
  3579. */
  3580. void pci_clear_mwi(struct pci_dev *dev)
  3581. {
  3582. #ifndef PCI_DISABLE_MWI
  3583. u16 cmd;
  3584. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  3585. if (cmd & PCI_COMMAND_INVALIDATE) {
  3586. cmd &= ~PCI_COMMAND_INVALIDATE;
  3587. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3588. }
  3589. #endif
  3590. }
  3591. EXPORT_SYMBOL(pci_clear_mwi);
  3592. /**
  3593. * pci_intx - enables/disables PCI INTx for device dev
  3594. * @pdev: the PCI device to operate on
  3595. * @enable: boolean: whether to enable or disable PCI INTx
  3596. *
  3597. * Enables/disables PCI INTx for device dev
  3598. */
  3599. void pci_intx(struct pci_dev *pdev, int enable)
  3600. {
  3601. u16 pci_command, new;
  3602. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  3603. if (enable)
  3604. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  3605. else
  3606. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  3607. if (new != pci_command) {
  3608. struct pci_devres *dr;
  3609. pci_write_config_word(pdev, PCI_COMMAND, new);
  3610. dr = find_pci_dr(pdev);
  3611. if (dr && !dr->restore_intx) {
  3612. dr->restore_intx = 1;
  3613. dr->orig_intx = !enable;
  3614. }
  3615. }
  3616. }
  3617. EXPORT_SYMBOL_GPL(pci_intx);
  3618. static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
  3619. {
  3620. struct pci_bus *bus = dev->bus;
  3621. bool mask_updated = true;
  3622. u32 cmd_status_dword;
  3623. u16 origcmd, newcmd;
  3624. unsigned long flags;
  3625. bool irq_pending;
  3626. /*
  3627. * We do a single dword read to retrieve both command and status.
  3628. * Document assumptions that make this possible.
  3629. */
  3630. BUILD_BUG_ON(PCI_COMMAND % 4);
  3631. BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
  3632. raw_spin_lock_irqsave(&pci_lock, flags);
  3633. bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
  3634. irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
  3635. /*
  3636. * Check interrupt status register to see whether our device
  3637. * triggered the interrupt (when masking) or the next IRQ is
  3638. * already pending (when unmasking).
  3639. */
  3640. if (mask != irq_pending) {
  3641. mask_updated = false;
  3642. goto done;
  3643. }
  3644. origcmd = cmd_status_dword;
  3645. newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
  3646. if (mask)
  3647. newcmd |= PCI_COMMAND_INTX_DISABLE;
  3648. if (newcmd != origcmd)
  3649. bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
  3650. done:
  3651. raw_spin_unlock_irqrestore(&pci_lock, flags);
  3652. return mask_updated;
  3653. }
  3654. /**
  3655. * pci_check_and_mask_intx - mask INTx on pending interrupt
  3656. * @dev: the PCI device to operate on
  3657. *
  3658. * Check if the device dev has its INTx line asserted, mask it and
  3659. * return true in that case. False is returned if no interrupt was
  3660. * pending.
  3661. */
  3662. bool pci_check_and_mask_intx(struct pci_dev *dev)
  3663. {
  3664. return pci_check_and_set_intx_mask(dev, true);
  3665. }
  3666. EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
  3667. /**
  3668. * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
  3669. * @dev: the PCI device to operate on
  3670. *
  3671. * Check if the device dev has its INTx line asserted, unmask it if not
  3672. * and return true. False is returned and the mask remains active if
  3673. * there was still an interrupt pending.
  3674. */
  3675. bool pci_check_and_unmask_intx(struct pci_dev *dev)
  3676. {
  3677. return pci_check_and_set_intx_mask(dev, false);
  3678. }
  3679. EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
  3680. /**
  3681. * pci_wait_for_pending_transaction - waits for pending transaction
  3682. * @dev: the PCI device to operate on
  3683. *
  3684. * Return 0 if transaction is pending 1 otherwise.
  3685. */
  3686. int pci_wait_for_pending_transaction(struct pci_dev *dev)
  3687. {
  3688. if (!pci_is_pcie(dev))
  3689. return 1;
  3690. return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA,
  3691. PCI_EXP_DEVSTA_TRPND);
  3692. }
  3693. EXPORT_SYMBOL(pci_wait_for_pending_transaction);
  3694. static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout)
  3695. {
  3696. int delay = 1;
  3697. u32 id;
  3698. /*
  3699. * After reset, the device should not silently discard config
  3700. * requests, but it may still indicate that it needs more time by
  3701. * responding to them with CRS completions. The Root Port will
  3702. * generally synthesize ~0 data to complete the read (except when
  3703. * CRS SV is enabled and the read was for the Vendor ID; in that
  3704. * case it synthesizes 0x0001 data).
  3705. *
  3706. * Wait for the device to return a non-CRS completion. Read the
  3707. * Command register instead of Vendor ID so we don't have to
  3708. * contend with the CRS SV value.
  3709. */
  3710. pci_read_config_dword(dev, PCI_COMMAND, &id);
  3711. while (id == ~0) {
  3712. if (delay > timeout) {
  3713. pci_warn(dev, "not ready %dms after %s; giving up\n",
  3714. delay - 1, reset_type);
  3715. return -ENOTTY;
  3716. }
  3717. if (delay > 1000)
  3718. pci_info(dev, "not ready %dms after %s; waiting\n",
  3719. delay - 1, reset_type);
  3720. msleep(delay);
  3721. delay *= 2;
  3722. pci_read_config_dword(dev, PCI_COMMAND, &id);
  3723. }
  3724. if (delay > 1000)
  3725. pci_info(dev, "ready %dms after %s\n", delay - 1,
  3726. reset_type);
  3727. return 0;
  3728. }
  3729. /**
  3730. * pcie_has_flr - check if a device supports function level resets
  3731. * @dev: device to check
  3732. *
  3733. * Returns true if the device advertises support for PCIe function level
  3734. * resets.
  3735. */
  3736. bool pcie_has_flr(struct pci_dev *dev)
  3737. {
  3738. u32 cap;
  3739. if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
  3740. return false;
  3741. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
  3742. return cap & PCI_EXP_DEVCAP_FLR;
  3743. }
  3744. EXPORT_SYMBOL_GPL(pcie_has_flr);
  3745. /**
  3746. * pcie_flr - initiate a PCIe function level reset
  3747. * @dev: device to reset
  3748. *
  3749. * Initiate a function level reset on @dev. The caller should ensure the
  3750. * device supports FLR before calling this function, e.g. by using the
  3751. * pcie_has_flr() helper.
  3752. */
  3753. int pcie_flr(struct pci_dev *dev)
  3754. {
  3755. if (!pci_wait_for_pending_transaction(dev))
  3756. pci_err(dev, "timed out waiting for pending transaction; performing function level reset anyway\n");
  3757. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
  3758. /*
  3759. * Per PCIe r4.0, sec 6.6.2, a device must complete an FLR within
  3760. * 100ms, but may silently discard requests while the FLR is in
  3761. * progress. Wait 100ms before trying to access the device.
  3762. */
  3763. msleep(100);
  3764. return pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS);
  3765. }
  3766. EXPORT_SYMBOL_GPL(pcie_flr);
  3767. static int pci_af_flr(struct pci_dev *dev, int probe)
  3768. {
  3769. int pos;
  3770. u8 cap;
  3771. pos = pci_find_capability(dev, PCI_CAP_ID_AF);
  3772. if (!pos)
  3773. return -ENOTTY;
  3774. if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
  3775. return -ENOTTY;
  3776. pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
  3777. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  3778. return -ENOTTY;
  3779. if (probe)
  3780. return 0;
  3781. /*
  3782. * Wait for Transaction Pending bit to clear. A word-aligned test
  3783. * is used, so we use the conrol offset rather than status and shift
  3784. * the test bit to match.
  3785. */
  3786. if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL,
  3787. PCI_AF_STATUS_TP << 8))
  3788. pci_err(dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n");
  3789. pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  3790. /*
  3791. * Per Advanced Capabilities for Conventional PCI ECN, 13 April 2006,
  3792. * updated 27 July 2006; a device must complete an FLR within
  3793. * 100ms, but may silently discard requests while the FLR is in
  3794. * progress. Wait 100ms before trying to access the device.
  3795. */
  3796. msleep(100);
  3797. return pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS);
  3798. }
  3799. /**
  3800. * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
  3801. * @dev: Device to reset.
  3802. * @probe: If set, only check if the device can be reset this way.
  3803. *
  3804. * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
  3805. * unset, it will be reinitialized internally when going from PCI_D3hot to
  3806. * PCI_D0. If that's the case and the device is not in a low-power state
  3807. * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
  3808. *
  3809. * NOTE: This causes the caller to sleep for twice the device power transition
  3810. * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
  3811. * by default (i.e. unless the @dev's d3_delay field has a different value).
  3812. * Moreover, only devices in D0 can be reset by this function.
  3813. */
  3814. static int pci_pm_reset(struct pci_dev *dev, int probe)
  3815. {
  3816. u16 csr;
  3817. if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET)
  3818. return -ENOTTY;
  3819. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
  3820. if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
  3821. return -ENOTTY;
  3822. if (probe)
  3823. return 0;
  3824. if (dev->current_state != PCI_D0)
  3825. return -EINVAL;
  3826. csr &= ~PCI_PM_CTRL_STATE_MASK;
  3827. csr |= PCI_D3hot;
  3828. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  3829. pci_dev_d3_sleep(dev);
  3830. csr &= ~PCI_PM_CTRL_STATE_MASK;
  3831. csr |= PCI_D0;
  3832. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  3833. pci_dev_d3_sleep(dev);
  3834. return pci_dev_wait(dev, "PM D3->D0", PCIE_RESET_READY_POLL_MS);
  3835. }
  3836. /**
  3837. * pcie_wait_for_link - Wait until link is active or inactive
  3838. * @pdev: Bridge device
  3839. * @active: waiting for active or inactive?
  3840. *
  3841. * Use this to wait till link becomes active or inactive.
  3842. */
  3843. bool pcie_wait_for_link(struct pci_dev *pdev, bool active)
  3844. {
  3845. int timeout = 1000;
  3846. bool ret;
  3847. u16 lnk_status;
  3848. /*
  3849. * Some controllers might not implement link active reporting. In this
  3850. * case, we wait for 1000 + 100 ms.
  3851. */
  3852. if (!pdev->link_active_reporting) {
  3853. msleep(1100);
  3854. return true;
  3855. }
  3856. /*
  3857. * PCIe r4.0 sec 6.6.1, a component must enter LTSSM Detect within 20ms,
  3858. * after which we should expect an link active if the reset was
  3859. * successful. If so, software must wait a minimum 100ms before sending
  3860. * configuration requests to devices downstream this port.
  3861. *
  3862. * If the link fails to activate, either the device was physically
  3863. * removed or the link is permanently failed.
  3864. */
  3865. if (active)
  3866. msleep(20);
  3867. for (;;) {
  3868. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  3869. ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
  3870. if (ret == active)
  3871. break;
  3872. if (timeout <= 0)
  3873. break;
  3874. msleep(10);
  3875. timeout -= 10;
  3876. }
  3877. if (active && ret)
  3878. msleep(100);
  3879. else if (ret != active)
  3880. pci_info(pdev, "Data Link Layer Link Active not %s in 1000 msec\n",
  3881. active ? "set" : "cleared");
  3882. return ret == active;
  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->owner))
  3940. return rc;
  3941. if (hotplug->ops->reset_slot)
  3942. rc = hotplug->ops->reset_slot(hotplug, probe);
  3943. module_put(hotplug->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. /* Save and disable devices from the top of the tree down */
  4302. static void pci_bus_save_and_disable(struct pci_bus *bus)
  4303. {
  4304. struct pci_dev *dev;
  4305. list_for_each_entry(dev, &bus->devices, bus_list) {
  4306. pci_dev_lock(dev);
  4307. pci_dev_save_and_disable(dev);
  4308. pci_dev_unlock(dev);
  4309. if (dev->subordinate)
  4310. pci_bus_save_and_disable(dev->subordinate);
  4311. }
  4312. }
  4313. /*
  4314. * Restore devices from top of the tree down - parent bridges need to be
  4315. * restored before we can get to subordinate devices.
  4316. */
  4317. static void pci_bus_restore(struct pci_bus *bus)
  4318. {
  4319. struct pci_dev *dev;
  4320. list_for_each_entry(dev, &bus->devices, bus_list) {
  4321. pci_dev_lock(dev);
  4322. pci_dev_restore(dev);
  4323. pci_dev_unlock(dev);
  4324. if (dev->subordinate)
  4325. pci_bus_restore(dev->subordinate);
  4326. }
  4327. }
  4328. /* Save and disable devices from the top of the tree down */
  4329. static void pci_slot_save_and_disable(struct pci_slot *slot)
  4330. {
  4331. struct pci_dev *dev;
  4332. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4333. if (!dev->slot || dev->slot != slot)
  4334. continue;
  4335. pci_dev_save_and_disable(dev);
  4336. if (dev->subordinate)
  4337. pci_bus_save_and_disable(dev->subordinate);
  4338. }
  4339. }
  4340. /*
  4341. * Restore devices from top of the tree down - parent bridges need to be
  4342. * restored before we can get to subordinate devices.
  4343. */
  4344. static void pci_slot_restore(struct pci_slot *slot)
  4345. {
  4346. struct pci_dev *dev;
  4347. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4348. if (!dev->slot || dev->slot != slot)
  4349. continue;
  4350. pci_dev_lock(dev);
  4351. pci_dev_restore(dev);
  4352. pci_dev_unlock(dev);
  4353. if (dev->subordinate)
  4354. pci_bus_restore(dev->subordinate);
  4355. }
  4356. }
  4357. static int pci_slot_reset(struct pci_slot *slot, int probe)
  4358. {
  4359. int rc;
  4360. if (!slot || !pci_slot_resetable(slot))
  4361. return -ENOTTY;
  4362. if (!probe)
  4363. pci_slot_lock(slot);
  4364. might_sleep();
  4365. rc = pci_reset_hotplug_slot(slot->hotplug, probe);
  4366. if (!probe)
  4367. pci_slot_unlock(slot);
  4368. return rc;
  4369. }
  4370. /**
  4371. * pci_probe_reset_slot - probe whether a PCI slot can be reset
  4372. * @slot: PCI slot to probe
  4373. *
  4374. * Return 0 if slot can be reset, negative if a slot reset is not supported.
  4375. */
  4376. int pci_probe_reset_slot(struct pci_slot *slot)
  4377. {
  4378. return pci_slot_reset(slot, 1);
  4379. }
  4380. EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
  4381. /**
  4382. * __pci_reset_slot - Try to reset a PCI slot
  4383. * @slot: PCI slot to reset
  4384. *
  4385. * A PCI bus may host multiple slots, each slot may support a reset mechanism
  4386. * independent of other slots. For instance, some slots may support slot power
  4387. * control. In the case of a 1:1 bus to slot architecture, this function may
  4388. * wrap the bus reset to avoid spurious slot related events such as hotplug.
  4389. * Generally a slot reset should be attempted before a bus reset. All of the
  4390. * function of the slot and any subordinate buses behind the slot are reset
  4391. * through this function. PCI config space of all devices in the slot and
  4392. * behind the slot is saved before and restored after reset.
  4393. *
  4394. * Same as above except return -EAGAIN if the slot cannot be locked
  4395. */
  4396. static int __pci_reset_slot(struct pci_slot *slot)
  4397. {
  4398. int rc;
  4399. rc = pci_slot_reset(slot, 1);
  4400. if (rc)
  4401. return rc;
  4402. pci_slot_save_and_disable(slot);
  4403. if (pci_slot_trylock(slot)) {
  4404. might_sleep();
  4405. rc = pci_reset_hotplug_slot(slot->hotplug, 0);
  4406. pci_slot_unlock(slot);
  4407. } else
  4408. rc = -EAGAIN;
  4409. pci_slot_restore(slot);
  4410. return rc;
  4411. }
  4412. static int pci_bus_reset(struct pci_bus *bus, int probe)
  4413. {
  4414. int ret;
  4415. if (!bus->self || !pci_bus_resetable(bus))
  4416. return -ENOTTY;
  4417. if (probe)
  4418. return 0;
  4419. pci_bus_lock(bus);
  4420. might_sleep();
  4421. ret = pci_bridge_secondary_bus_reset(bus->self);
  4422. pci_bus_unlock(bus);
  4423. return ret;
  4424. }
  4425. /**
  4426. * pci_bus_error_reset - reset the bridge's subordinate bus
  4427. * @bridge: The parent device that connects to the bus to reset
  4428. *
  4429. * This function will first try to reset the slots on this bus if the method is
  4430. * available. If slot reset fails or is not available, this will fall back to a
  4431. * secondary bus reset.
  4432. */
  4433. int pci_bus_error_reset(struct pci_dev *bridge)
  4434. {
  4435. struct pci_bus *bus = bridge->subordinate;
  4436. struct pci_slot *slot;
  4437. if (!bus)
  4438. return -ENOTTY;
  4439. mutex_lock(&pci_slot_mutex);
  4440. if (list_empty(&bus->slots))
  4441. goto bus_reset;
  4442. list_for_each_entry(slot, &bus->slots, list)
  4443. if (pci_probe_reset_slot(slot))
  4444. goto bus_reset;
  4445. list_for_each_entry(slot, &bus->slots, list)
  4446. if (pci_slot_reset(slot, 0))
  4447. goto bus_reset;
  4448. mutex_unlock(&pci_slot_mutex);
  4449. return 0;
  4450. bus_reset:
  4451. mutex_unlock(&pci_slot_mutex);
  4452. return pci_bus_reset(bridge->subordinate, 0);
  4453. }
  4454. /**
  4455. * pci_probe_reset_bus - probe whether a PCI bus can be reset
  4456. * @bus: PCI bus to probe
  4457. *
  4458. * Return 0 if bus can be reset, negative if a bus reset is not supported.
  4459. */
  4460. int pci_probe_reset_bus(struct pci_bus *bus)
  4461. {
  4462. return pci_bus_reset(bus, 1);
  4463. }
  4464. EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
  4465. /**
  4466. * __pci_reset_bus - Try to reset a PCI bus
  4467. * @bus: top level PCI bus to reset
  4468. *
  4469. * Same as above except return -EAGAIN if the bus cannot be locked
  4470. */
  4471. static int __pci_reset_bus(struct pci_bus *bus)
  4472. {
  4473. int rc;
  4474. rc = pci_bus_reset(bus, 1);
  4475. if (rc)
  4476. return rc;
  4477. pci_bus_save_and_disable(bus);
  4478. if (pci_bus_trylock(bus)) {
  4479. might_sleep();
  4480. rc = pci_bridge_secondary_bus_reset(bus->self);
  4481. pci_bus_unlock(bus);
  4482. } else
  4483. rc = -EAGAIN;
  4484. pci_bus_restore(bus);
  4485. return rc;
  4486. }
  4487. /**
  4488. * pci_reset_bus - Try to reset a PCI bus
  4489. * @pdev: top level PCI device to reset via slot/bus
  4490. *
  4491. * Same as above except return -EAGAIN if the bus cannot be locked
  4492. */
  4493. int pci_reset_bus(struct pci_dev *pdev)
  4494. {
  4495. return (!pci_probe_reset_slot(pdev->slot)) ?
  4496. __pci_reset_slot(pdev->slot) : __pci_reset_bus(pdev->bus);
  4497. }
  4498. EXPORT_SYMBOL_GPL(pci_reset_bus);
  4499. /**
  4500. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  4501. * @dev: PCI device to query
  4502. *
  4503. * Returns mmrbc: maximum designed memory read count in bytes
  4504. * or appropriate error value.
  4505. */
  4506. int pcix_get_max_mmrbc(struct pci_dev *dev)
  4507. {
  4508. int cap;
  4509. u32 stat;
  4510. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  4511. if (!cap)
  4512. return -EINVAL;
  4513. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  4514. return -EINVAL;
  4515. return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
  4516. }
  4517. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  4518. /**
  4519. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  4520. * @dev: PCI device to query
  4521. *
  4522. * Returns mmrbc: maximum memory read count in bytes
  4523. * or appropriate error value.
  4524. */
  4525. int pcix_get_mmrbc(struct pci_dev *dev)
  4526. {
  4527. int cap;
  4528. u16 cmd;
  4529. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  4530. if (!cap)
  4531. return -EINVAL;
  4532. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  4533. return -EINVAL;
  4534. return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  4535. }
  4536. EXPORT_SYMBOL(pcix_get_mmrbc);
  4537. /**
  4538. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  4539. * @dev: PCI device to query
  4540. * @mmrbc: maximum memory read count in bytes
  4541. * valid values are 512, 1024, 2048, 4096
  4542. *
  4543. * If possible sets maximum memory read byte count, some bridges have erratas
  4544. * that prevent this.
  4545. */
  4546. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  4547. {
  4548. int cap;
  4549. u32 stat, v, o;
  4550. u16 cmd;
  4551. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  4552. return -EINVAL;
  4553. v = ffs(mmrbc) - 10;
  4554. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  4555. if (!cap)
  4556. return -EINVAL;
  4557. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  4558. return -EINVAL;
  4559. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  4560. return -E2BIG;
  4561. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  4562. return -EINVAL;
  4563. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  4564. if (o != v) {
  4565. if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  4566. return -EIO;
  4567. cmd &= ~PCI_X_CMD_MAX_READ;
  4568. cmd |= v << 2;
  4569. if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
  4570. return -EIO;
  4571. }
  4572. return 0;
  4573. }
  4574. EXPORT_SYMBOL(pcix_set_mmrbc);
  4575. /**
  4576. * pcie_get_readrq - get PCI Express read request size
  4577. * @dev: PCI device to query
  4578. *
  4579. * Returns maximum memory read request in bytes
  4580. * or appropriate error value.
  4581. */
  4582. int pcie_get_readrq(struct pci_dev *dev)
  4583. {
  4584. u16 ctl;
  4585. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  4586. return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  4587. }
  4588. EXPORT_SYMBOL(pcie_get_readrq);
  4589. /**
  4590. * pcie_set_readrq - set PCI Express maximum memory read request
  4591. * @dev: PCI device to query
  4592. * @rq: maximum memory read count in bytes
  4593. * valid values are 128, 256, 512, 1024, 2048, 4096
  4594. *
  4595. * If possible sets maximum memory read request in bytes
  4596. */
  4597. int pcie_set_readrq(struct pci_dev *dev, int rq)
  4598. {
  4599. u16 v;
  4600. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  4601. return -EINVAL;
  4602. /*
  4603. * If using the "performance" PCIe config, we clamp the
  4604. * read rq size to the max packet size to prevent the
  4605. * host bridge generating requests larger than we can
  4606. * cope with
  4607. */
  4608. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  4609. int mps = pcie_get_mps(dev);
  4610. if (mps < rq)
  4611. rq = mps;
  4612. }
  4613. v = (ffs(rq) - 8) << 12;
  4614. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  4615. PCI_EXP_DEVCTL_READRQ, v);
  4616. }
  4617. EXPORT_SYMBOL(pcie_set_readrq);
  4618. /**
  4619. * pcie_get_mps - get PCI Express maximum payload size
  4620. * @dev: PCI device to query
  4621. *
  4622. * Returns maximum payload size in bytes
  4623. */
  4624. int pcie_get_mps(struct pci_dev *dev)
  4625. {
  4626. u16 ctl;
  4627. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  4628. return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
  4629. }
  4630. EXPORT_SYMBOL(pcie_get_mps);
  4631. /**
  4632. * pcie_set_mps - set PCI Express maximum payload size
  4633. * @dev: PCI device to query
  4634. * @mps: maximum payload size in bytes
  4635. * valid values are 128, 256, 512, 1024, 2048, 4096
  4636. *
  4637. * If possible sets maximum payload size
  4638. */
  4639. int pcie_set_mps(struct pci_dev *dev, int mps)
  4640. {
  4641. u16 v;
  4642. if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
  4643. return -EINVAL;
  4644. v = ffs(mps) - 8;
  4645. if (v > dev->pcie_mpss)
  4646. return -EINVAL;
  4647. v <<= 5;
  4648. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  4649. PCI_EXP_DEVCTL_PAYLOAD, v);
  4650. }
  4651. EXPORT_SYMBOL(pcie_set_mps);
  4652. /**
  4653. * pcie_bandwidth_available - determine minimum link settings of a PCIe
  4654. * device and its bandwidth limitation
  4655. * @dev: PCI device to query
  4656. * @limiting_dev: storage for device causing the bandwidth limitation
  4657. * @speed: storage for speed of limiting device
  4658. * @width: storage for width of limiting device
  4659. *
  4660. * Walk up the PCI device chain and find the point where the minimum
  4661. * bandwidth is available. Return the bandwidth available there and (if
  4662. * limiting_dev, speed, and width pointers are supplied) information about
  4663. * that point. The bandwidth returned is in Mb/s, i.e., megabits/second of
  4664. * raw bandwidth.
  4665. */
  4666. u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
  4667. enum pci_bus_speed *speed,
  4668. enum pcie_link_width *width)
  4669. {
  4670. u16 lnksta;
  4671. enum pci_bus_speed next_speed;
  4672. enum pcie_link_width next_width;
  4673. u32 bw, next_bw;
  4674. if (speed)
  4675. *speed = PCI_SPEED_UNKNOWN;
  4676. if (width)
  4677. *width = PCIE_LNK_WIDTH_UNKNOWN;
  4678. bw = 0;
  4679. while (dev) {
  4680. pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
  4681. next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
  4682. next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
  4683. PCI_EXP_LNKSTA_NLW_SHIFT;
  4684. next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
  4685. /* Check if current device limits the total bandwidth */
  4686. if (!bw || next_bw <= bw) {
  4687. bw = next_bw;
  4688. if (limiting_dev)
  4689. *limiting_dev = dev;
  4690. if (speed)
  4691. *speed = next_speed;
  4692. if (width)
  4693. *width = next_width;
  4694. }
  4695. dev = pci_upstream_bridge(dev);
  4696. }
  4697. return bw;
  4698. }
  4699. EXPORT_SYMBOL(pcie_bandwidth_available);
  4700. /**
  4701. * pcie_get_speed_cap - query for the PCI device's link speed capability
  4702. * @dev: PCI device to query
  4703. *
  4704. * Query the PCI device speed capability. Return the maximum link speed
  4705. * supported by the device.
  4706. */
  4707. enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev)
  4708. {
  4709. u32 lnkcap2, lnkcap;
  4710. /*
  4711. * PCIe r4.0 sec 7.5.3.18 recommends using the Supported Link
  4712. * Speeds Vector in Link Capabilities 2 when supported, falling
  4713. * back to Max Link Speed in Link Capabilities otherwise.
  4714. */
  4715. pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2);
  4716. if (lnkcap2) { /* PCIe r3.0-compliant */
  4717. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_16_0GB)
  4718. return PCIE_SPEED_16_0GT;
  4719. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
  4720. return PCIE_SPEED_8_0GT;
  4721. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
  4722. return PCIE_SPEED_5_0GT;
  4723. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
  4724. return PCIE_SPEED_2_5GT;
  4725. return PCI_SPEED_UNKNOWN;
  4726. }
  4727. pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
  4728. if (lnkcap) {
  4729. if (lnkcap & PCI_EXP_LNKCAP_SLS_16_0GB)
  4730. return PCIE_SPEED_16_0GT;
  4731. else if (lnkcap & PCI_EXP_LNKCAP_SLS_8_0GB)
  4732. return PCIE_SPEED_8_0GT;
  4733. else if (lnkcap & PCI_EXP_LNKCAP_SLS_5_0GB)
  4734. return PCIE_SPEED_5_0GT;
  4735. else if (lnkcap & PCI_EXP_LNKCAP_SLS_2_5GB)
  4736. return PCIE_SPEED_2_5GT;
  4737. }
  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. } else {
  4987. align_order = -1;
  4988. }
  4989. ret = pci_dev_str_match(dev, p, &p);
  4990. if (ret == 1) {
  4991. *resize = true;
  4992. if (align_order == -1)
  4993. align = PAGE_SIZE;
  4994. else
  4995. align = 1 << align_order;
  4996. break;
  4997. } else if (ret < 0) {
  4998. pr_err("PCI: Can't parse resource_alignment parameter: %s\n",
  4999. p);
  5000. break;
  5001. }
  5002. if (*p != ';' && *p != ',') {
  5003. /* End of param or invalid format */
  5004. break;
  5005. }
  5006. p++;
  5007. }
  5008. out:
  5009. spin_unlock(&resource_alignment_lock);
  5010. return align;
  5011. }
  5012. static void pci_request_resource_alignment(struct pci_dev *dev, int bar,
  5013. resource_size_t align, bool resize)
  5014. {
  5015. struct resource *r = &dev->resource[bar];
  5016. resource_size_t size;
  5017. if (!(r->flags & IORESOURCE_MEM))
  5018. return;
  5019. if (r->flags & IORESOURCE_PCI_FIXED) {
  5020. pci_info(dev, "BAR%d %pR: ignoring requested alignment %#llx\n",
  5021. bar, r, (unsigned long long)align);
  5022. return;
  5023. }
  5024. size = resource_size(r);
  5025. if (size >= align)
  5026. return;
  5027. /*
  5028. * Increase the alignment of the resource. There are two ways we
  5029. * can do this:
  5030. *
  5031. * 1) Increase the size of the resource. BARs are aligned on their
  5032. * size, so when we reallocate space for this resource, we'll
  5033. * allocate it with the larger alignment. This also prevents
  5034. * assignment of any other BARs inside the alignment region, so
  5035. * if we're requesting page alignment, this means no other BARs
  5036. * will share the page.
  5037. *
  5038. * The disadvantage is that this makes the resource larger than
  5039. * the hardware BAR, which may break drivers that compute things
  5040. * based on the resource size, e.g., to find registers at a
  5041. * fixed offset before the end of the BAR.
  5042. *
  5043. * 2) Retain the resource size, but use IORESOURCE_STARTALIGN and
  5044. * set r->start to the desired alignment. By itself this
  5045. * doesn't prevent other BARs being put inside the alignment
  5046. * region, but if we realign *every* resource of every device in
  5047. * the system, none of them will share an alignment region.
  5048. *
  5049. * When the user has requested alignment for only some devices via
  5050. * the "pci=resource_alignment" argument, "resize" is true and we
  5051. * use the first method. Otherwise we assume we're aligning all
  5052. * devices and we use the second.
  5053. */
  5054. pci_info(dev, "BAR%d %pR: requesting alignment to %#llx\n",
  5055. bar, r, (unsigned long long)align);
  5056. if (resize) {
  5057. r->start = 0;
  5058. r->end = align - 1;
  5059. } else {
  5060. r->flags &= ~IORESOURCE_SIZEALIGN;
  5061. r->flags |= IORESOURCE_STARTALIGN;
  5062. r->start = align;
  5063. r->end = r->start + size - 1;
  5064. }
  5065. r->flags |= IORESOURCE_UNSET;
  5066. }
  5067. /*
  5068. * This function disables memory decoding and releases memory resources
  5069. * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
  5070. * It also rounds up size to specified alignment.
  5071. * Later on, the kernel will assign page-aligned memory resource back
  5072. * to the device.
  5073. */
  5074. void pci_reassigndev_resource_alignment(struct pci_dev *dev)
  5075. {
  5076. int i;
  5077. struct resource *r;
  5078. resource_size_t align;
  5079. u16 command;
  5080. bool resize = false;
  5081. /*
  5082. * VF BARs are read-only zero according to SR-IOV spec r1.1, sec
  5083. * 3.4.1.11. Their resources are allocated from the space
  5084. * described by the VF BARx register in the PF's SR-IOV capability.
  5085. * We can't influence their alignment here.
  5086. */
  5087. if (dev->is_virtfn)
  5088. return;
  5089. /* check if specified PCI is target device to reassign */
  5090. align = pci_specified_resource_alignment(dev, &resize);
  5091. if (!align)
  5092. return;
  5093. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
  5094. (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
  5095. pci_warn(dev, "Can't reassign resources to host bridge\n");
  5096. return;
  5097. }
  5098. pci_read_config_word(dev, PCI_COMMAND, &command);
  5099. command &= ~PCI_COMMAND_MEMORY;
  5100. pci_write_config_word(dev, PCI_COMMAND, command);
  5101. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  5102. pci_request_resource_alignment(dev, i, align, resize);
  5103. /*
  5104. * Need to disable bridge's resource window,
  5105. * to enable the kernel to reassign new resource
  5106. * window later on.
  5107. */
  5108. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  5109. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  5110. for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
  5111. r = &dev->resource[i];
  5112. if (!(r->flags & IORESOURCE_MEM))
  5113. continue;
  5114. r->flags |= IORESOURCE_UNSET;
  5115. r->end = resource_size(r) - 1;
  5116. r->start = 0;
  5117. }
  5118. pci_disable_bridge_window(dev);
  5119. }
  5120. }
  5121. static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
  5122. {
  5123. if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
  5124. count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
  5125. spin_lock(&resource_alignment_lock);
  5126. strncpy(resource_alignment_param, buf, count);
  5127. resource_alignment_param[count] = '\0';
  5128. spin_unlock(&resource_alignment_lock);
  5129. return count;
  5130. }
  5131. static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
  5132. {
  5133. size_t count;
  5134. spin_lock(&resource_alignment_lock);
  5135. count = snprintf(buf, size, "%s", resource_alignment_param);
  5136. spin_unlock(&resource_alignment_lock);
  5137. return count;
  5138. }
  5139. static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
  5140. {
  5141. return pci_get_resource_alignment_param(buf, PAGE_SIZE);
  5142. }
  5143. static ssize_t pci_resource_alignment_store(struct bus_type *bus,
  5144. const char *buf, size_t count)
  5145. {
  5146. return pci_set_resource_alignment_param(buf, count);
  5147. }
  5148. static BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
  5149. pci_resource_alignment_store);
  5150. static int __init pci_resource_alignment_sysfs_init(void)
  5151. {
  5152. return bus_create_file(&pci_bus_type,
  5153. &bus_attr_resource_alignment);
  5154. }
  5155. late_initcall(pci_resource_alignment_sysfs_init);
  5156. static void pci_no_domains(void)
  5157. {
  5158. #ifdef CONFIG_PCI_DOMAINS
  5159. pci_domains_supported = 0;
  5160. #endif
  5161. }
  5162. #ifdef CONFIG_PCI_DOMAINS_GENERIC
  5163. static atomic_t __domain_nr = ATOMIC_INIT(-1);
  5164. static int pci_get_new_domain_nr(void)
  5165. {
  5166. return atomic_inc_return(&__domain_nr);
  5167. }
  5168. static int of_pci_bus_find_domain_nr(struct device *parent)
  5169. {
  5170. static int use_dt_domains = -1;
  5171. int domain = -1;
  5172. if (parent)
  5173. domain = of_get_pci_domain_nr(parent->of_node);
  5174. /*
  5175. * Check DT domain and use_dt_domains values.
  5176. *
  5177. * If DT domain property is valid (domain >= 0) and
  5178. * use_dt_domains != 0, the DT assignment is valid since this means
  5179. * we have not previously allocated a domain number by using
  5180. * pci_get_new_domain_nr(); we should also update use_dt_domains to
  5181. * 1, to indicate that we have just assigned a domain number from
  5182. * DT.
  5183. *
  5184. * If DT domain property value is not valid (ie domain < 0), and we
  5185. * have not previously assigned a domain number from DT
  5186. * (use_dt_domains != 1) we should assign a domain number by
  5187. * using the:
  5188. *
  5189. * pci_get_new_domain_nr()
  5190. *
  5191. * API and update the use_dt_domains value to keep track of method we
  5192. * are using to assign domain numbers (use_dt_domains = 0).
  5193. *
  5194. * All other combinations imply we have a platform that is trying
  5195. * to mix domain numbers obtained from DT and pci_get_new_domain_nr(),
  5196. * which is a recipe for domain mishandling and it is prevented by
  5197. * invalidating the domain value (domain = -1) and printing a
  5198. * corresponding error.
  5199. */
  5200. if (domain >= 0 && use_dt_domains) {
  5201. use_dt_domains = 1;
  5202. } else if (domain < 0 && use_dt_domains != 1) {
  5203. use_dt_domains = 0;
  5204. domain = pci_get_new_domain_nr();
  5205. } else {
  5206. if (parent)
  5207. pr_err("Node %pOF has ", parent->of_node);
  5208. pr_err("Inconsistent \"linux,pci-domain\" property in DT\n");
  5209. domain = -1;
  5210. }
  5211. return domain;
  5212. }
  5213. int pci_bus_find_domain_nr(struct pci_bus *bus, struct device *parent)
  5214. {
  5215. return acpi_disabled ? of_pci_bus_find_domain_nr(parent) :
  5216. acpi_pci_bus_find_domain_nr(bus);
  5217. }
  5218. #endif
  5219. /**
  5220. * pci_ext_cfg_avail - can we access extended PCI config space?
  5221. *
  5222. * Returns 1 if we can access PCI extended config space (offsets
  5223. * greater than 0xff). This is the default implementation. Architecture
  5224. * implementations can override this.
  5225. */
  5226. int __weak pci_ext_cfg_avail(void)
  5227. {
  5228. return 1;
  5229. }
  5230. void __weak pci_fixup_cardbus(struct pci_bus *bus)
  5231. {
  5232. }
  5233. EXPORT_SYMBOL(pci_fixup_cardbus);
  5234. static int __init pci_setup(char *str)
  5235. {
  5236. while (str) {
  5237. char *k = strchr(str, ',');
  5238. if (k)
  5239. *k++ = 0;
  5240. if (*str && (str = pcibios_setup(str)) && *str) {
  5241. if (!strcmp(str, "nomsi")) {
  5242. pci_no_msi();
  5243. } else if (!strncmp(str, "noats", 5)) {
  5244. pr_info("PCIe: ATS is disabled\n");
  5245. pcie_ats_disabled = true;
  5246. } else if (!strcmp(str, "noaer")) {
  5247. pci_no_aer();
  5248. } else if (!strcmp(str, "earlydump")) {
  5249. pci_early_dump = true;
  5250. } else if (!strncmp(str, "realloc=", 8)) {
  5251. pci_realloc_get_opt(str + 8);
  5252. } else if (!strncmp(str, "realloc", 7)) {
  5253. pci_realloc_get_opt("on");
  5254. } else if (!strcmp(str, "nodomains")) {
  5255. pci_no_domains();
  5256. } else if (!strncmp(str, "noari", 5)) {
  5257. pcie_ari_disabled = true;
  5258. } else if (!strncmp(str, "cbiosize=", 9)) {
  5259. pci_cardbus_io_size = memparse(str + 9, &str);
  5260. } else if (!strncmp(str, "cbmemsize=", 10)) {
  5261. pci_cardbus_mem_size = memparse(str + 10, &str);
  5262. } else if (!strncmp(str, "resource_alignment=", 19)) {
  5263. pci_set_resource_alignment_param(str + 19,
  5264. strlen(str + 19));
  5265. } else if (!strncmp(str, "ecrc=", 5)) {
  5266. pcie_ecrc_get_policy(str + 5);
  5267. } else if (!strncmp(str, "hpiosize=", 9)) {
  5268. pci_hotplug_io_size = memparse(str + 9, &str);
  5269. } else if (!strncmp(str, "hpmemsize=", 10)) {
  5270. pci_hotplug_mem_size = memparse(str + 10, &str);
  5271. } else if (!strncmp(str, "hpbussize=", 10)) {
  5272. pci_hotplug_bus_size =
  5273. simple_strtoul(str + 10, &str, 0);
  5274. if (pci_hotplug_bus_size > 0xff)
  5275. pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
  5276. } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
  5277. pcie_bus_config = PCIE_BUS_TUNE_OFF;
  5278. } else if (!strncmp(str, "pcie_bus_safe", 13)) {
  5279. pcie_bus_config = PCIE_BUS_SAFE;
  5280. } else if (!strncmp(str, "pcie_bus_perf", 13)) {
  5281. pcie_bus_config = PCIE_BUS_PERFORMANCE;
  5282. } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
  5283. pcie_bus_config = PCIE_BUS_PEER2PEER;
  5284. } else if (!strncmp(str, "pcie_scan_all", 13)) {
  5285. pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
  5286. } else if (!strncmp(str, "disable_acs_redir=", 18)) {
  5287. disable_acs_redir_param = str + 18;
  5288. } else {
  5289. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  5290. str);
  5291. }
  5292. }
  5293. str = k;
  5294. }
  5295. return 0;
  5296. }
  5297. early_param("pci", pci_setup);