pci.c 144 KB

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