pci.c 153 KB

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