pci.c 136 KB

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