pci.c 104 KB

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