pci.c 115 KB

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