pci.c 148 KB

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