pci.c 130 KB

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