pci.c 154 KB

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