pci.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  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/module.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/string.h>
  17. #include <linux/log2.h>
  18. #include <linux/pci-aspm.h>
  19. #include <linux/pm_wakeup.h>
  20. #include <linux/interrupt.h>
  21. #include <asm/dma.h> /* isa_dma_bridge_buggy */
  22. #include <linux/device.h>
  23. #include <asm/setup.h>
  24. #include "pci.h"
  25. const char *pci_power_names[] = {
  26. "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
  27. };
  28. EXPORT_SYMBOL_GPL(pci_power_names);
  29. unsigned int pci_pm_d3_delay = PCI_PM_D3_WAIT;
  30. #ifdef CONFIG_PCI_DOMAINS
  31. int pci_domains_supported = 1;
  32. #endif
  33. #define DEFAULT_CARDBUS_IO_SIZE (256)
  34. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  35. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  36. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  37. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  38. #define DEFAULT_HOTPLUG_IO_SIZE (256)
  39. #define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
  40. /* pci=hpmemsize=nnM,hpiosize=nn can override this */
  41. unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
  42. unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
  43. /**
  44. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  45. * @bus: pointer to PCI bus structure to search
  46. *
  47. * Given a PCI bus, returns the highest PCI bus number present in the set
  48. * including the given PCI bus and its list of child PCI buses.
  49. */
  50. unsigned char pci_bus_max_busnr(struct pci_bus* bus)
  51. {
  52. struct list_head *tmp;
  53. unsigned char max, n;
  54. max = bus->subordinate;
  55. list_for_each(tmp, &bus->children) {
  56. n = pci_bus_max_busnr(pci_bus_b(tmp));
  57. if(n > max)
  58. max = n;
  59. }
  60. return max;
  61. }
  62. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  63. #ifdef CONFIG_HAS_IOMEM
  64. void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
  65. {
  66. /*
  67. * Make sure the BAR is actually a memory resource, not an IO resource
  68. */
  69. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  70. WARN_ON(1);
  71. return NULL;
  72. }
  73. return ioremap_nocache(pci_resource_start(pdev, bar),
  74. pci_resource_len(pdev, bar));
  75. }
  76. EXPORT_SYMBOL_GPL(pci_ioremap_bar);
  77. #endif
  78. #if 0
  79. /**
  80. * pci_max_busnr - returns maximum PCI bus number
  81. *
  82. * Returns the highest PCI bus number present in the system global list of
  83. * PCI buses.
  84. */
  85. unsigned char __devinit
  86. pci_max_busnr(void)
  87. {
  88. struct pci_bus *bus = NULL;
  89. unsigned char max, n;
  90. max = 0;
  91. while ((bus = pci_find_next_bus(bus)) != NULL) {
  92. n = pci_bus_max_busnr(bus);
  93. if(n > max)
  94. max = n;
  95. }
  96. return max;
  97. }
  98. #endif /* 0 */
  99. #define PCI_FIND_CAP_TTL 48
  100. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  101. u8 pos, int cap, int *ttl)
  102. {
  103. u8 id;
  104. while ((*ttl)--) {
  105. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  106. if (pos < 0x40)
  107. break;
  108. pos &= ~3;
  109. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  110. &id);
  111. if (id == 0xff)
  112. break;
  113. if (id == cap)
  114. return pos;
  115. pos += PCI_CAP_LIST_NEXT;
  116. }
  117. return 0;
  118. }
  119. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  120. u8 pos, int cap)
  121. {
  122. int ttl = PCI_FIND_CAP_TTL;
  123. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  124. }
  125. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  126. {
  127. return __pci_find_next_cap(dev->bus, dev->devfn,
  128. pos + PCI_CAP_LIST_NEXT, cap);
  129. }
  130. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  131. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  132. unsigned int devfn, u8 hdr_type)
  133. {
  134. u16 status;
  135. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  136. if (!(status & PCI_STATUS_CAP_LIST))
  137. return 0;
  138. switch (hdr_type) {
  139. case PCI_HEADER_TYPE_NORMAL:
  140. case PCI_HEADER_TYPE_BRIDGE:
  141. return PCI_CAPABILITY_LIST;
  142. case PCI_HEADER_TYPE_CARDBUS:
  143. return PCI_CB_CAPABILITY_LIST;
  144. default:
  145. return 0;
  146. }
  147. return 0;
  148. }
  149. /**
  150. * pci_find_capability - query for devices' capabilities
  151. * @dev: PCI device to query
  152. * @cap: capability code
  153. *
  154. * Tell if a device supports a given PCI capability.
  155. * Returns the address of the requested capability structure within the
  156. * device's PCI configuration space or 0 in case the device does not
  157. * support it. Possible values for @cap:
  158. *
  159. * %PCI_CAP_ID_PM Power Management
  160. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  161. * %PCI_CAP_ID_VPD Vital Product Data
  162. * %PCI_CAP_ID_SLOTID Slot Identification
  163. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  164. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  165. * %PCI_CAP_ID_PCIX PCI-X
  166. * %PCI_CAP_ID_EXP PCI Express
  167. */
  168. int pci_find_capability(struct pci_dev *dev, int cap)
  169. {
  170. int pos;
  171. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  172. if (pos)
  173. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  174. return pos;
  175. }
  176. /**
  177. * pci_bus_find_capability - query for devices' capabilities
  178. * @bus: the PCI bus to query
  179. * @devfn: PCI device to query
  180. * @cap: capability code
  181. *
  182. * Like pci_find_capability() but works for pci devices that do not have a
  183. * pci_dev structure set up yet.
  184. *
  185. * Returns the address of the requested capability structure within the
  186. * device's PCI configuration space or 0 in case the device does not
  187. * support it.
  188. */
  189. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  190. {
  191. int pos;
  192. u8 hdr_type;
  193. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  194. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  195. if (pos)
  196. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  197. return pos;
  198. }
  199. /**
  200. * pci_find_ext_capability - Find an extended capability
  201. * @dev: PCI device to query
  202. * @cap: capability code
  203. *
  204. * Returns the address of the requested extended capability structure
  205. * within the device's PCI configuration space or 0 if the device does
  206. * not support it. Possible values for @cap:
  207. *
  208. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  209. * %PCI_EXT_CAP_ID_VC Virtual Channel
  210. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  211. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  212. */
  213. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  214. {
  215. u32 header;
  216. int ttl;
  217. int pos = PCI_CFG_SPACE_SIZE;
  218. /* minimum 8 bytes per capability */
  219. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  220. if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
  221. return 0;
  222. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  223. return 0;
  224. /*
  225. * If we have no capabilities, this is indicated by cap ID,
  226. * cap version and next pointer all being 0.
  227. */
  228. if (header == 0)
  229. return 0;
  230. while (ttl-- > 0) {
  231. if (PCI_EXT_CAP_ID(header) == cap)
  232. return pos;
  233. pos = PCI_EXT_CAP_NEXT(header);
  234. if (pos < PCI_CFG_SPACE_SIZE)
  235. break;
  236. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  237. break;
  238. }
  239. return 0;
  240. }
  241. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  242. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  243. {
  244. int rc, ttl = PCI_FIND_CAP_TTL;
  245. u8 cap, mask;
  246. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  247. mask = HT_3BIT_CAP_MASK;
  248. else
  249. mask = HT_5BIT_CAP_MASK;
  250. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  251. PCI_CAP_ID_HT, &ttl);
  252. while (pos) {
  253. rc = pci_read_config_byte(dev, pos + 3, &cap);
  254. if (rc != PCIBIOS_SUCCESSFUL)
  255. return 0;
  256. if ((cap & mask) == ht_cap)
  257. return pos;
  258. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  259. pos + PCI_CAP_LIST_NEXT,
  260. PCI_CAP_ID_HT, &ttl);
  261. }
  262. return 0;
  263. }
  264. /**
  265. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  266. * @dev: PCI device to query
  267. * @pos: Position from which to continue searching
  268. * @ht_cap: Hypertransport capability code
  269. *
  270. * To be used in conjunction with pci_find_ht_capability() to search for
  271. * all capabilities matching @ht_cap. @pos should always be a value returned
  272. * from pci_find_ht_capability().
  273. *
  274. * NB. To be 100% safe against broken PCI devices, the caller should take
  275. * steps to avoid an infinite loop.
  276. */
  277. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  278. {
  279. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  280. }
  281. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  282. /**
  283. * pci_find_ht_capability - query a device's Hypertransport capabilities
  284. * @dev: PCI device to query
  285. * @ht_cap: Hypertransport capability code
  286. *
  287. * Tell if a device supports a given Hypertransport capability.
  288. * Returns an address within the device's PCI configuration space
  289. * or 0 in case the device does not support the request capability.
  290. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  291. * which has a Hypertransport capability matching @ht_cap.
  292. */
  293. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  294. {
  295. int pos;
  296. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  297. if (pos)
  298. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  299. return pos;
  300. }
  301. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  302. /**
  303. * pci_find_parent_resource - return resource region of parent bus of given region
  304. * @dev: PCI device structure contains resources to be searched
  305. * @res: child resource record for which parent is sought
  306. *
  307. * For given resource region of given device, return the resource
  308. * region of parent bus the given region is contained in or where
  309. * it should be allocated from.
  310. */
  311. struct resource *
  312. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  313. {
  314. const struct pci_bus *bus = dev->bus;
  315. int i;
  316. struct resource *best = NULL;
  317. for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  318. struct resource *r = bus->resource[i];
  319. if (!r)
  320. continue;
  321. if (res->start && !(res->start >= r->start && res->end <= r->end))
  322. continue; /* Not contained */
  323. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  324. continue; /* Wrong type */
  325. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  326. return r; /* Exact match */
  327. if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
  328. best = r; /* Approximating prefetchable by non-prefetchable */
  329. }
  330. return best;
  331. }
  332. /**
  333. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  334. * @dev: PCI device to have its BARs restored
  335. *
  336. * Restore the BAR values for a given device, so as to make it
  337. * accessible by its driver.
  338. */
  339. static void
  340. pci_restore_bars(struct pci_dev *dev)
  341. {
  342. int i;
  343. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
  344. pci_update_resource(dev, i);
  345. }
  346. static struct pci_platform_pm_ops *pci_platform_pm;
  347. int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
  348. {
  349. if (!ops->is_manageable || !ops->set_state || !ops->choose_state
  350. || !ops->sleep_wake || !ops->can_wakeup)
  351. return -EINVAL;
  352. pci_platform_pm = ops;
  353. return 0;
  354. }
  355. static inline bool platform_pci_power_manageable(struct pci_dev *dev)
  356. {
  357. return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
  358. }
  359. static inline int platform_pci_set_power_state(struct pci_dev *dev,
  360. pci_power_t t)
  361. {
  362. return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
  363. }
  364. static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
  365. {
  366. return pci_platform_pm ?
  367. pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
  368. }
  369. static inline bool platform_pci_can_wakeup(struct pci_dev *dev)
  370. {
  371. return pci_platform_pm ? pci_platform_pm->can_wakeup(dev) : false;
  372. }
  373. static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
  374. {
  375. return pci_platform_pm ?
  376. pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
  377. }
  378. /**
  379. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  380. * given PCI device
  381. * @dev: PCI device to handle.
  382. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  383. *
  384. * RETURN VALUE:
  385. * -EINVAL if the requested state is invalid.
  386. * -EIO if device does not support PCI PM or its PM capabilities register has a
  387. * wrong version, or device doesn't support the requested state.
  388. * 0 if device already is in the requested state.
  389. * 0 if device's power state has been successfully changed.
  390. */
  391. static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
  392. {
  393. u16 pmcsr;
  394. bool need_restore = false;
  395. /* Check if we're already there */
  396. if (dev->current_state == state)
  397. return 0;
  398. if (!dev->pm_cap)
  399. return -EIO;
  400. if (state < PCI_D0 || state > PCI_D3hot)
  401. return -EINVAL;
  402. /* Validate current state:
  403. * Can enter D0 from any state, but if we can only go deeper
  404. * to sleep if we're already in a low power state
  405. */
  406. if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  407. && dev->current_state > state) {
  408. dev_err(&dev->dev, "invalid power transition "
  409. "(from state %d to %d)\n", dev->current_state, state);
  410. return -EINVAL;
  411. }
  412. /* check if this device supports the desired state */
  413. if ((state == PCI_D1 && !dev->d1_support)
  414. || (state == PCI_D2 && !dev->d2_support))
  415. return -EIO;
  416. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  417. /* If we're (effectively) in D3, force entire word to 0.
  418. * This doesn't affect PME_Status, disables PME_En, and
  419. * sets PowerState to 0.
  420. */
  421. switch (dev->current_state) {
  422. case PCI_D0:
  423. case PCI_D1:
  424. case PCI_D2:
  425. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  426. pmcsr |= state;
  427. break;
  428. case PCI_D3hot:
  429. case PCI_D3cold:
  430. case PCI_UNKNOWN: /* Boot-up */
  431. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  432. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  433. need_restore = true;
  434. /* Fall-through: force to D0 */
  435. default:
  436. pmcsr = 0;
  437. break;
  438. }
  439. /* enter specified state */
  440. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  441. /* Mandatory power management transition delays */
  442. /* see PCI PM 1.1 5.6.1 table 18 */
  443. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  444. msleep(pci_pm_d3_delay);
  445. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  446. udelay(PCI_PM_D2_DELAY);
  447. dev->current_state = state;
  448. /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  449. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  450. * from D3hot to D0 _may_ perform an internal reset, thereby
  451. * going to "D0 Uninitialized" rather than "D0 Initialized".
  452. * For example, at least some versions of the 3c905B and the
  453. * 3c556B exhibit this behaviour.
  454. *
  455. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  456. * devices in a D3hot state at boot. Consequently, we need to
  457. * restore at least the BARs so that the device will be
  458. * accessible to its driver.
  459. */
  460. if (need_restore)
  461. pci_restore_bars(dev);
  462. if (dev->bus->self)
  463. pcie_aspm_pm_state_change(dev->bus->self);
  464. return 0;
  465. }
  466. /**
  467. * pci_update_current_state - Read PCI power state of given device from its
  468. * PCI PM registers and cache it
  469. * @dev: PCI device to handle.
  470. * @state: State to cache in case the device doesn't have the PM capability
  471. */
  472. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  473. {
  474. if (dev->pm_cap) {
  475. u16 pmcsr;
  476. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  477. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  478. } else {
  479. dev->current_state = state;
  480. }
  481. }
  482. /**
  483. * pci_platform_power_transition - Use platform to change device power state
  484. * @dev: PCI device to handle.
  485. * @state: State to put the device into.
  486. */
  487. static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
  488. {
  489. int error;
  490. if (platform_pci_power_manageable(dev)) {
  491. error = platform_pci_set_power_state(dev, state);
  492. if (!error)
  493. pci_update_current_state(dev, state);
  494. } else {
  495. error = -ENODEV;
  496. /* Fall back to PCI_D0 if native PM is not supported */
  497. if (!dev->pm_cap)
  498. dev->current_state = PCI_D0;
  499. }
  500. return error;
  501. }
  502. /**
  503. * __pci_start_power_transition - Start power transition of a PCI device
  504. * @dev: PCI device to handle.
  505. * @state: State to put the device into.
  506. */
  507. static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
  508. {
  509. if (state == PCI_D0)
  510. pci_platform_power_transition(dev, PCI_D0);
  511. }
  512. /**
  513. * __pci_complete_power_transition - Complete power transition of a PCI device
  514. * @dev: PCI device to handle.
  515. * @state: State to put the device into.
  516. *
  517. * This function should not be called directly by device drivers.
  518. */
  519. int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
  520. {
  521. return state > PCI_D0 ?
  522. pci_platform_power_transition(dev, state) : -EINVAL;
  523. }
  524. EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
  525. /**
  526. * pci_set_power_state - Set the power state of a PCI device
  527. * @dev: PCI device to handle.
  528. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  529. *
  530. * Transition a device to a new power state, using the platform firmware and/or
  531. * the device's PCI PM registers.
  532. *
  533. * RETURN VALUE:
  534. * -EINVAL if the requested state is invalid.
  535. * -EIO if device does not support PCI PM or its PM capabilities register has a
  536. * wrong version, or device doesn't support the requested state.
  537. * 0 if device already is in the requested state.
  538. * 0 if device's power state has been successfully changed.
  539. */
  540. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  541. {
  542. int error;
  543. /* bound the state we're entering */
  544. if (state > PCI_D3hot)
  545. state = PCI_D3hot;
  546. else if (state < PCI_D0)
  547. state = PCI_D0;
  548. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  549. /*
  550. * If the device or the parent bridge do not support PCI PM,
  551. * ignore the request if we're doing anything other than putting
  552. * it into D0 (which would only happen on boot).
  553. */
  554. return 0;
  555. /* Check if we're already there */
  556. if (dev->current_state == state)
  557. return 0;
  558. __pci_start_power_transition(dev, state);
  559. /* This device is quirked not to be put into D3, so
  560. don't put it in D3 */
  561. if (state == PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  562. return 0;
  563. error = pci_raw_set_power_state(dev, state);
  564. if (!__pci_complete_power_transition(dev, state))
  565. error = 0;
  566. return error;
  567. }
  568. /**
  569. * pci_choose_state - Choose the power state of a PCI device
  570. * @dev: PCI device to be suspended
  571. * @state: target sleep state for the whole system. This is the value
  572. * that is passed to suspend() function.
  573. *
  574. * Returns PCI power state suitable for given device and given system
  575. * message.
  576. */
  577. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  578. {
  579. pci_power_t ret;
  580. if (!pci_find_capability(dev, PCI_CAP_ID_PM))
  581. return PCI_D0;
  582. ret = platform_pci_choose_state(dev);
  583. if (ret != PCI_POWER_ERROR)
  584. return ret;
  585. switch (state.event) {
  586. case PM_EVENT_ON:
  587. return PCI_D0;
  588. case PM_EVENT_FREEZE:
  589. case PM_EVENT_PRETHAW:
  590. /* REVISIT both freeze and pre-thaw "should" use D0 */
  591. case PM_EVENT_SUSPEND:
  592. case PM_EVENT_HIBERNATE:
  593. return PCI_D3hot;
  594. default:
  595. dev_info(&dev->dev, "unrecognized suspend event %d\n",
  596. state.event);
  597. BUG();
  598. }
  599. return PCI_D0;
  600. }
  601. EXPORT_SYMBOL(pci_choose_state);
  602. #define PCI_EXP_SAVE_REGS 7
  603. #define pcie_cap_has_devctl(type, flags) 1
  604. #define pcie_cap_has_lnkctl(type, flags) \
  605. ((flags & PCI_EXP_FLAGS_VERS) > 1 || \
  606. (type == PCI_EXP_TYPE_ROOT_PORT || \
  607. type == PCI_EXP_TYPE_ENDPOINT || \
  608. type == PCI_EXP_TYPE_LEG_END))
  609. #define pcie_cap_has_sltctl(type, flags) \
  610. ((flags & PCI_EXP_FLAGS_VERS) > 1 || \
  611. ((type == PCI_EXP_TYPE_ROOT_PORT) || \
  612. (type == PCI_EXP_TYPE_DOWNSTREAM && \
  613. (flags & PCI_EXP_FLAGS_SLOT))))
  614. #define pcie_cap_has_rtctl(type, flags) \
  615. ((flags & PCI_EXP_FLAGS_VERS) > 1 || \
  616. (type == PCI_EXP_TYPE_ROOT_PORT || \
  617. type == PCI_EXP_TYPE_RC_EC))
  618. #define pcie_cap_has_devctl2(type, flags) \
  619. ((flags & PCI_EXP_FLAGS_VERS) > 1)
  620. #define pcie_cap_has_lnkctl2(type, flags) \
  621. ((flags & PCI_EXP_FLAGS_VERS) > 1)
  622. #define pcie_cap_has_sltctl2(type, flags) \
  623. ((flags & PCI_EXP_FLAGS_VERS) > 1)
  624. static int pci_save_pcie_state(struct pci_dev *dev)
  625. {
  626. int pos, i = 0;
  627. struct pci_cap_saved_state *save_state;
  628. u16 *cap;
  629. u16 flags;
  630. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  631. if (pos <= 0)
  632. return 0;
  633. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  634. if (!save_state) {
  635. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  636. return -ENOMEM;
  637. }
  638. cap = (u16 *)&save_state->data[0];
  639. pci_read_config_word(dev, pos + PCI_EXP_FLAGS, &flags);
  640. if (pcie_cap_has_devctl(dev->pcie_type, flags))
  641. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]);
  642. if (pcie_cap_has_lnkctl(dev->pcie_type, flags))
  643. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
  644. if (pcie_cap_has_sltctl(dev->pcie_type, flags))
  645. pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
  646. if (pcie_cap_has_rtctl(dev->pcie_type, flags))
  647. pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
  648. if (pcie_cap_has_devctl2(dev->pcie_type, flags))
  649. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &cap[i++]);
  650. if (pcie_cap_has_lnkctl2(dev->pcie_type, flags))
  651. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL2, &cap[i++]);
  652. if (pcie_cap_has_sltctl2(dev->pcie_type, flags))
  653. pci_read_config_word(dev, pos + PCI_EXP_SLTCTL2, &cap[i++]);
  654. return 0;
  655. }
  656. static void pci_restore_pcie_state(struct pci_dev *dev)
  657. {
  658. int i = 0, pos;
  659. struct pci_cap_saved_state *save_state;
  660. u16 *cap;
  661. u16 flags;
  662. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  663. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  664. if (!save_state || pos <= 0)
  665. return;
  666. cap = (u16 *)&save_state->data[0];
  667. pci_read_config_word(dev, pos + PCI_EXP_FLAGS, &flags);
  668. if (pcie_cap_has_devctl(dev->pcie_type, flags))
  669. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]);
  670. if (pcie_cap_has_lnkctl(dev->pcie_type, flags))
  671. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]);
  672. if (pcie_cap_has_sltctl(dev->pcie_type, flags))
  673. pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]);
  674. if (pcie_cap_has_rtctl(dev->pcie_type, flags))
  675. pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);
  676. if (pcie_cap_has_devctl2(dev->pcie_type, flags))
  677. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, cap[i++]);
  678. if (pcie_cap_has_lnkctl2(dev->pcie_type, flags))
  679. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL2, cap[i++]);
  680. if (pcie_cap_has_sltctl2(dev->pcie_type, flags))
  681. pci_write_config_word(dev, pos + PCI_EXP_SLTCTL2, cap[i++]);
  682. }
  683. static int pci_save_pcix_state(struct pci_dev *dev)
  684. {
  685. int pos;
  686. struct pci_cap_saved_state *save_state;
  687. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  688. if (pos <= 0)
  689. return 0;
  690. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  691. if (!save_state) {
  692. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  693. return -ENOMEM;
  694. }
  695. pci_read_config_word(dev, pos + PCI_X_CMD, (u16 *)save_state->data);
  696. return 0;
  697. }
  698. static void pci_restore_pcix_state(struct pci_dev *dev)
  699. {
  700. int i = 0, pos;
  701. struct pci_cap_saved_state *save_state;
  702. u16 *cap;
  703. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  704. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  705. if (!save_state || pos <= 0)
  706. return;
  707. cap = (u16 *)&save_state->data[0];
  708. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  709. }
  710. /**
  711. * pci_save_state - save the PCI configuration space of a device before suspending
  712. * @dev: - PCI device that we're dealing with
  713. */
  714. int
  715. pci_save_state(struct pci_dev *dev)
  716. {
  717. int i;
  718. /* XXX: 100% dword access ok here? */
  719. for (i = 0; i < 16; i++)
  720. pci_read_config_dword(dev, i * 4,&dev->saved_config_space[i]);
  721. dev->state_saved = true;
  722. if ((i = pci_save_pcie_state(dev)) != 0)
  723. return i;
  724. if ((i = pci_save_pcix_state(dev)) != 0)
  725. return i;
  726. return 0;
  727. }
  728. /**
  729. * pci_restore_state - Restore the saved state of a PCI device
  730. * @dev: - PCI device that we're dealing with
  731. */
  732. int
  733. pci_restore_state(struct pci_dev *dev)
  734. {
  735. int i;
  736. u32 val;
  737. if (!dev->state_saved)
  738. return 0;
  739. /* PCI Express register must be restored first */
  740. pci_restore_pcie_state(dev);
  741. /*
  742. * The Base Address register should be programmed before the command
  743. * register(s)
  744. */
  745. for (i = 15; i >= 0; i--) {
  746. pci_read_config_dword(dev, i * 4, &val);
  747. if (val != dev->saved_config_space[i]) {
  748. dev_printk(KERN_DEBUG, &dev->dev, "restoring config "
  749. "space at offset %#x (was %#x, writing %#x)\n",
  750. i, val, (int)dev->saved_config_space[i]);
  751. pci_write_config_dword(dev,i * 4,
  752. dev->saved_config_space[i]);
  753. }
  754. }
  755. pci_restore_pcix_state(dev);
  756. pci_restore_msi_state(dev);
  757. pci_restore_iov_state(dev);
  758. return 0;
  759. }
  760. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  761. {
  762. int err;
  763. err = pci_set_power_state(dev, PCI_D0);
  764. if (err < 0 && err != -EIO)
  765. return err;
  766. err = pcibios_enable_device(dev, bars);
  767. if (err < 0)
  768. return err;
  769. pci_fixup_device(pci_fixup_enable, dev);
  770. return 0;
  771. }
  772. /**
  773. * pci_reenable_device - Resume abandoned device
  774. * @dev: PCI device to be resumed
  775. *
  776. * Note this function is a backend of pci_default_resume and is not supposed
  777. * to be called by normal code, write proper resume handler and use it instead.
  778. */
  779. int pci_reenable_device(struct pci_dev *dev)
  780. {
  781. if (pci_is_enabled(dev))
  782. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  783. return 0;
  784. }
  785. static int __pci_enable_device_flags(struct pci_dev *dev,
  786. resource_size_t flags)
  787. {
  788. int err;
  789. int i, bars = 0;
  790. if (atomic_add_return(1, &dev->enable_cnt) > 1)
  791. return 0; /* already enabled */
  792. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  793. if (dev->resource[i].flags & flags)
  794. bars |= (1 << i);
  795. err = do_pci_enable_device(dev, bars);
  796. if (err < 0)
  797. atomic_dec(&dev->enable_cnt);
  798. return err;
  799. }
  800. /**
  801. * pci_enable_device_io - Initialize a device for use with IO space
  802. * @dev: PCI device to be initialized
  803. *
  804. * Initialize device before it's used by a driver. Ask low-level code
  805. * to enable I/O resources. Wake up the device if it was suspended.
  806. * Beware, this function can fail.
  807. */
  808. int pci_enable_device_io(struct pci_dev *dev)
  809. {
  810. return __pci_enable_device_flags(dev, IORESOURCE_IO);
  811. }
  812. /**
  813. * pci_enable_device_mem - Initialize a device for use with Memory space
  814. * @dev: PCI device to be initialized
  815. *
  816. * Initialize device before it's used by a driver. Ask low-level code
  817. * to enable Memory resources. Wake up the device if it was suspended.
  818. * Beware, this function can fail.
  819. */
  820. int pci_enable_device_mem(struct pci_dev *dev)
  821. {
  822. return __pci_enable_device_flags(dev, IORESOURCE_MEM);
  823. }
  824. /**
  825. * pci_enable_device - Initialize device before it's used by a driver.
  826. * @dev: PCI device to be initialized
  827. *
  828. * Initialize device before it's used by a driver. Ask low-level code
  829. * to enable I/O and memory. Wake up the device if it was suspended.
  830. * Beware, this function can fail.
  831. *
  832. * Note we don't actually enable the device many times if we call
  833. * this function repeatedly (we just increment the count).
  834. */
  835. int pci_enable_device(struct pci_dev *dev)
  836. {
  837. return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  838. }
  839. /*
  840. * Managed PCI resources. This manages device on/off, intx/msi/msix
  841. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  842. * there's no need to track it separately. pci_devres is initialized
  843. * when a device is enabled using managed PCI device enable interface.
  844. */
  845. struct pci_devres {
  846. unsigned int enabled:1;
  847. unsigned int pinned:1;
  848. unsigned int orig_intx:1;
  849. unsigned int restore_intx:1;
  850. u32 region_mask;
  851. };
  852. static void pcim_release(struct device *gendev, void *res)
  853. {
  854. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  855. struct pci_devres *this = res;
  856. int i;
  857. if (dev->msi_enabled)
  858. pci_disable_msi(dev);
  859. if (dev->msix_enabled)
  860. pci_disable_msix(dev);
  861. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  862. if (this->region_mask & (1 << i))
  863. pci_release_region(dev, i);
  864. if (this->restore_intx)
  865. pci_intx(dev, this->orig_intx);
  866. if (this->enabled && !this->pinned)
  867. pci_disable_device(dev);
  868. }
  869. static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
  870. {
  871. struct pci_devres *dr, *new_dr;
  872. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  873. if (dr)
  874. return dr;
  875. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  876. if (!new_dr)
  877. return NULL;
  878. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  879. }
  880. static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
  881. {
  882. if (pci_is_managed(pdev))
  883. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  884. return NULL;
  885. }
  886. /**
  887. * pcim_enable_device - Managed pci_enable_device()
  888. * @pdev: PCI device to be initialized
  889. *
  890. * Managed pci_enable_device().
  891. */
  892. int pcim_enable_device(struct pci_dev *pdev)
  893. {
  894. struct pci_devres *dr;
  895. int rc;
  896. dr = get_pci_dr(pdev);
  897. if (unlikely(!dr))
  898. return -ENOMEM;
  899. if (dr->enabled)
  900. return 0;
  901. rc = pci_enable_device(pdev);
  902. if (!rc) {
  903. pdev->is_managed = 1;
  904. dr->enabled = 1;
  905. }
  906. return rc;
  907. }
  908. /**
  909. * pcim_pin_device - Pin managed PCI device
  910. * @pdev: PCI device to pin
  911. *
  912. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  913. * driver detach. @pdev must have been enabled with
  914. * pcim_enable_device().
  915. */
  916. void pcim_pin_device(struct pci_dev *pdev)
  917. {
  918. struct pci_devres *dr;
  919. dr = find_pci_dr(pdev);
  920. WARN_ON(!dr || !dr->enabled);
  921. if (dr)
  922. dr->pinned = 1;
  923. }
  924. /**
  925. * pcibios_disable_device - disable arch specific PCI resources for device dev
  926. * @dev: the PCI device to disable
  927. *
  928. * Disables architecture specific PCI resources for the device. This
  929. * is the default implementation. Architecture implementations can
  930. * override this.
  931. */
  932. void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
  933. static void do_pci_disable_device(struct pci_dev *dev)
  934. {
  935. u16 pci_command;
  936. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  937. if (pci_command & PCI_COMMAND_MASTER) {
  938. pci_command &= ~PCI_COMMAND_MASTER;
  939. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  940. }
  941. pcibios_disable_device(dev);
  942. }
  943. /**
  944. * pci_disable_enabled_device - Disable device without updating enable_cnt
  945. * @dev: PCI device to disable
  946. *
  947. * NOTE: This function is a backend of PCI power management routines and is
  948. * not supposed to be called drivers.
  949. */
  950. void pci_disable_enabled_device(struct pci_dev *dev)
  951. {
  952. if (pci_is_enabled(dev))
  953. do_pci_disable_device(dev);
  954. }
  955. /**
  956. * pci_disable_device - Disable PCI device after use
  957. * @dev: PCI device to be disabled
  958. *
  959. * Signal to the system that the PCI device is not in use by the system
  960. * anymore. This only involves disabling PCI bus-mastering, if active.
  961. *
  962. * Note we don't actually disable the device until all callers of
  963. * pci_device_enable() have called pci_device_disable().
  964. */
  965. void
  966. pci_disable_device(struct pci_dev *dev)
  967. {
  968. struct pci_devres *dr;
  969. dr = find_pci_dr(dev);
  970. if (dr)
  971. dr->enabled = 0;
  972. if (atomic_sub_return(1, &dev->enable_cnt) != 0)
  973. return;
  974. do_pci_disable_device(dev);
  975. dev->is_busmaster = 0;
  976. }
  977. /**
  978. * pcibios_set_pcie_reset_state - set reset state for device dev
  979. * @dev: the PCI-E device reset
  980. * @state: Reset state to enter into
  981. *
  982. *
  983. * Sets the PCI-E reset state for the device. This is the default
  984. * implementation. Architecture implementations can override this.
  985. */
  986. int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev,
  987. enum pcie_reset_state state)
  988. {
  989. return -EINVAL;
  990. }
  991. /**
  992. * pci_set_pcie_reset_state - set reset state for device dev
  993. * @dev: the PCI-E device reset
  994. * @state: Reset state to enter into
  995. *
  996. *
  997. * Sets the PCI reset state for the device.
  998. */
  999. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  1000. {
  1001. return pcibios_set_pcie_reset_state(dev, state);
  1002. }
  1003. /**
  1004. * pci_pme_capable - check the capability of PCI device to generate PME#
  1005. * @dev: PCI device to handle.
  1006. * @state: PCI state from which device will issue PME#.
  1007. */
  1008. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  1009. {
  1010. if (!dev->pm_cap)
  1011. return false;
  1012. return !!(dev->pme_support & (1 << state));
  1013. }
  1014. /**
  1015. * pci_pme_active - enable or disable PCI device's PME# function
  1016. * @dev: PCI device to handle.
  1017. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  1018. *
  1019. * The caller must verify that the device is capable of generating PME# before
  1020. * calling this function with @enable equal to 'true'.
  1021. */
  1022. void pci_pme_active(struct pci_dev *dev, bool enable)
  1023. {
  1024. u16 pmcsr;
  1025. if (!dev->pm_cap)
  1026. return;
  1027. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1028. /* Clear PME_Status by writing 1 to it and enable PME# */
  1029. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  1030. if (!enable)
  1031. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1032. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  1033. dev_printk(KERN_INFO, &dev->dev, "PME# %s\n",
  1034. enable ? "enabled" : "disabled");
  1035. }
  1036. /**
  1037. * pci_enable_wake - enable PCI device as wakeup event source
  1038. * @dev: PCI device affected
  1039. * @state: PCI state from which device will issue wakeup events
  1040. * @enable: True to enable event generation; false to disable
  1041. *
  1042. * This enables the device as a wakeup event source, or disables it.
  1043. * When such events involves platform-specific hooks, those hooks are
  1044. * called automatically by this routine.
  1045. *
  1046. * Devices with legacy power management (no standard PCI PM capabilities)
  1047. * always require such platform hooks.
  1048. *
  1049. * RETURN VALUE:
  1050. * 0 is returned on success
  1051. * -EINVAL is returned if device is not supposed to wake up the system
  1052. * Error code depending on the platform is returned if both the platform and
  1053. * the native mechanism fail to enable the generation of wake-up events
  1054. */
  1055. int pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable)
  1056. {
  1057. int ret = 0;
  1058. if (enable && !device_may_wakeup(&dev->dev))
  1059. return -EINVAL;
  1060. /*
  1061. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  1062. * Anderson we should be doing PME# wake enable followed by ACPI wake
  1063. * enable. To disable wake-up we call the platform first, for symmetry.
  1064. */
  1065. if (enable) {
  1066. int error;
  1067. if (pci_pme_capable(dev, state))
  1068. pci_pme_active(dev, true);
  1069. else
  1070. ret = 1;
  1071. error = platform_pci_sleep_wake(dev, true);
  1072. if (ret)
  1073. ret = error;
  1074. } else {
  1075. platform_pci_sleep_wake(dev, false);
  1076. pci_pme_active(dev, false);
  1077. }
  1078. return ret;
  1079. }
  1080. /**
  1081. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  1082. * @dev: PCI device to prepare
  1083. * @enable: True to enable wake-up event generation; false to disable
  1084. *
  1085. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  1086. * and this function allows them to set that up cleanly - pci_enable_wake()
  1087. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  1088. * ordering constraints.
  1089. *
  1090. * This function only returns error code if the device is not capable of
  1091. * generating PME# from both D3_hot and D3_cold, and the platform is unable to
  1092. * enable wake-up power for it.
  1093. */
  1094. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  1095. {
  1096. return pci_pme_capable(dev, PCI_D3cold) ?
  1097. pci_enable_wake(dev, PCI_D3cold, enable) :
  1098. pci_enable_wake(dev, PCI_D3hot, enable);
  1099. }
  1100. /**
  1101. * pci_target_state - find an appropriate low power state for a given PCI dev
  1102. * @dev: PCI device
  1103. *
  1104. * Use underlying platform code to find a supported low power state for @dev.
  1105. * If the platform can't manage @dev, return the deepest state from which it
  1106. * can generate wake events, based on any available PME info.
  1107. */
  1108. pci_power_t pci_target_state(struct pci_dev *dev)
  1109. {
  1110. pci_power_t target_state = PCI_D3hot;
  1111. if (platform_pci_power_manageable(dev)) {
  1112. /*
  1113. * Call the platform to choose the target state of the device
  1114. * and enable wake-up from this state if supported.
  1115. */
  1116. pci_power_t state = platform_pci_choose_state(dev);
  1117. switch (state) {
  1118. case PCI_POWER_ERROR:
  1119. case PCI_UNKNOWN:
  1120. break;
  1121. case PCI_D1:
  1122. case PCI_D2:
  1123. if (pci_no_d1d2(dev))
  1124. break;
  1125. default:
  1126. target_state = state;
  1127. }
  1128. } else if (!dev->pm_cap) {
  1129. target_state = PCI_D0;
  1130. } else if (device_may_wakeup(&dev->dev)) {
  1131. /*
  1132. * Find the deepest state from which the device can generate
  1133. * wake-up events, make it the target state and enable device
  1134. * to generate PME#.
  1135. */
  1136. if (dev->pme_support) {
  1137. while (target_state
  1138. && !(dev->pme_support & (1 << target_state)))
  1139. target_state--;
  1140. }
  1141. }
  1142. return target_state;
  1143. }
  1144. /**
  1145. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1146. * @dev: Device to handle.
  1147. *
  1148. * Choose the power state appropriate for the device depending on whether
  1149. * it can wake up the system and/or is power manageable by the platform
  1150. * (PCI_D3hot is the default) and put the device into that state.
  1151. */
  1152. int pci_prepare_to_sleep(struct pci_dev *dev)
  1153. {
  1154. pci_power_t target_state = pci_target_state(dev);
  1155. int error;
  1156. if (target_state == PCI_POWER_ERROR)
  1157. return -EIO;
  1158. pci_enable_wake(dev, target_state, device_may_wakeup(&dev->dev));
  1159. error = pci_set_power_state(dev, target_state);
  1160. if (error)
  1161. pci_enable_wake(dev, target_state, false);
  1162. return error;
  1163. }
  1164. /**
  1165. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1166. * @dev: Device to handle.
  1167. *
  1168. * Disable device's sytem wake-up capability and put it into D0.
  1169. */
  1170. int pci_back_from_sleep(struct pci_dev *dev)
  1171. {
  1172. pci_enable_wake(dev, PCI_D0, false);
  1173. return pci_set_power_state(dev, PCI_D0);
  1174. }
  1175. /**
  1176. * pci_pm_init - Initialize PM functions of given PCI device
  1177. * @dev: PCI device to handle.
  1178. */
  1179. void pci_pm_init(struct pci_dev *dev)
  1180. {
  1181. int pm;
  1182. u16 pmc;
  1183. dev->pm_cap = 0;
  1184. /* find PCI PM capability in list */
  1185. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  1186. if (!pm)
  1187. return;
  1188. /* Check device's ability to generate PME# */
  1189. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  1190. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  1191. dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
  1192. pmc & PCI_PM_CAP_VER_MASK);
  1193. return;
  1194. }
  1195. dev->pm_cap = pm;
  1196. dev->d1_support = false;
  1197. dev->d2_support = false;
  1198. if (!pci_no_d1d2(dev)) {
  1199. if (pmc & PCI_PM_CAP_D1)
  1200. dev->d1_support = true;
  1201. if (pmc & PCI_PM_CAP_D2)
  1202. dev->d2_support = true;
  1203. if (dev->d1_support || dev->d2_support)
  1204. dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
  1205. dev->d1_support ? " D1" : "",
  1206. dev->d2_support ? " D2" : "");
  1207. }
  1208. pmc &= PCI_PM_CAP_PME_MASK;
  1209. if (pmc) {
  1210. dev_info(&dev->dev, "PME# supported from%s%s%s%s%s\n",
  1211. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  1212. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  1213. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  1214. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  1215. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  1216. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  1217. /*
  1218. * Make device's PM flags reflect the wake-up capability, but
  1219. * let the user space enable it to wake up the system as needed.
  1220. */
  1221. device_set_wakeup_capable(&dev->dev, true);
  1222. device_set_wakeup_enable(&dev->dev, false);
  1223. /* Disable the PME# generation functionality */
  1224. pci_pme_active(dev, false);
  1225. } else {
  1226. dev->pme_support = 0;
  1227. }
  1228. }
  1229. /**
  1230. * platform_pci_wakeup_init - init platform wakeup if present
  1231. * @dev: PCI device
  1232. *
  1233. * Some devices don't have PCI PM caps but can still generate wakeup
  1234. * events through platform methods (like ACPI events). If @dev supports
  1235. * platform wakeup events, set the device flag to indicate as much. This
  1236. * may be redundant if the device also supports PCI PM caps, but double
  1237. * initialization should be safe in that case.
  1238. */
  1239. void platform_pci_wakeup_init(struct pci_dev *dev)
  1240. {
  1241. if (!platform_pci_can_wakeup(dev))
  1242. return;
  1243. device_set_wakeup_capable(&dev->dev, true);
  1244. device_set_wakeup_enable(&dev->dev, false);
  1245. platform_pci_sleep_wake(dev, false);
  1246. }
  1247. /**
  1248. * pci_add_save_buffer - allocate buffer for saving given capability registers
  1249. * @dev: the PCI device
  1250. * @cap: the capability to allocate the buffer for
  1251. * @size: requested size of the buffer
  1252. */
  1253. static int pci_add_cap_save_buffer(
  1254. struct pci_dev *dev, char cap, unsigned int size)
  1255. {
  1256. int pos;
  1257. struct pci_cap_saved_state *save_state;
  1258. pos = pci_find_capability(dev, cap);
  1259. if (pos <= 0)
  1260. return 0;
  1261. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  1262. if (!save_state)
  1263. return -ENOMEM;
  1264. save_state->cap_nr = cap;
  1265. pci_add_saved_cap(dev, save_state);
  1266. return 0;
  1267. }
  1268. /**
  1269. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  1270. * @dev: the PCI device
  1271. */
  1272. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  1273. {
  1274. int error;
  1275. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
  1276. PCI_EXP_SAVE_REGS * sizeof(u16));
  1277. if (error)
  1278. dev_err(&dev->dev,
  1279. "unable to preallocate PCI Express save buffer\n");
  1280. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  1281. if (error)
  1282. dev_err(&dev->dev,
  1283. "unable to preallocate PCI-X save buffer\n");
  1284. }
  1285. /**
  1286. * pci_enable_ari - enable ARI forwarding if hardware support it
  1287. * @dev: the PCI device
  1288. */
  1289. void pci_enable_ari(struct pci_dev *dev)
  1290. {
  1291. int pos;
  1292. u32 cap;
  1293. u16 ctrl;
  1294. struct pci_dev *bridge;
  1295. if (!dev->is_pcie || dev->devfn)
  1296. return;
  1297. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  1298. if (!pos)
  1299. return;
  1300. bridge = dev->bus->self;
  1301. if (!bridge || !bridge->is_pcie)
  1302. return;
  1303. pos = pci_find_capability(bridge, PCI_CAP_ID_EXP);
  1304. if (!pos)
  1305. return;
  1306. pci_read_config_dword(bridge, pos + PCI_EXP_DEVCAP2, &cap);
  1307. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  1308. return;
  1309. pci_read_config_word(bridge, pos + PCI_EXP_DEVCTL2, &ctrl);
  1310. ctrl |= PCI_EXP_DEVCTL2_ARI;
  1311. pci_write_config_word(bridge, pos + PCI_EXP_DEVCTL2, ctrl);
  1312. bridge->ari_enabled = 1;
  1313. }
  1314. /**
  1315. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  1316. * @dev: the PCI device
  1317. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1318. *
  1319. * Perform INTx swizzling for a device behind one level of bridge. This is
  1320. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  1321. * behind bridges on add-in cards. For devices with ARI enabled, the slot
  1322. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  1323. * the PCI Express Base Specification, Revision 2.1)
  1324. */
  1325. u8 pci_swizzle_interrupt_pin(struct pci_dev *dev, u8 pin)
  1326. {
  1327. int slot;
  1328. if (pci_ari_enabled(dev->bus))
  1329. slot = 0;
  1330. else
  1331. slot = PCI_SLOT(dev->devfn);
  1332. return (((pin - 1) + slot) % 4) + 1;
  1333. }
  1334. int
  1335. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  1336. {
  1337. u8 pin;
  1338. pin = dev->pin;
  1339. if (!pin)
  1340. return -1;
  1341. while (!pci_is_root_bus(dev->bus)) {
  1342. pin = pci_swizzle_interrupt_pin(dev, pin);
  1343. dev = dev->bus->self;
  1344. }
  1345. *bridge = dev;
  1346. return pin;
  1347. }
  1348. /**
  1349. * pci_common_swizzle - swizzle INTx all the way to root bridge
  1350. * @dev: the PCI device
  1351. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1352. *
  1353. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  1354. * bridges all the way up to a PCI root bus.
  1355. */
  1356. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  1357. {
  1358. u8 pin = *pinp;
  1359. while (!pci_is_root_bus(dev->bus)) {
  1360. pin = pci_swizzle_interrupt_pin(dev, pin);
  1361. dev = dev->bus->self;
  1362. }
  1363. *pinp = pin;
  1364. return PCI_SLOT(dev->devfn);
  1365. }
  1366. /**
  1367. * pci_release_region - Release a PCI bar
  1368. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  1369. * @bar: BAR to release
  1370. *
  1371. * Releases the PCI I/O and memory resources previously reserved by a
  1372. * successful call to pci_request_region. Call this function only
  1373. * after all use of the PCI regions has ceased.
  1374. */
  1375. void pci_release_region(struct pci_dev *pdev, int bar)
  1376. {
  1377. struct pci_devres *dr;
  1378. if (pci_resource_len(pdev, bar) == 0)
  1379. return;
  1380. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  1381. release_region(pci_resource_start(pdev, bar),
  1382. pci_resource_len(pdev, bar));
  1383. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  1384. release_mem_region(pci_resource_start(pdev, bar),
  1385. pci_resource_len(pdev, bar));
  1386. dr = find_pci_dr(pdev);
  1387. if (dr)
  1388. dr->region_mask &= ~(1 << bar);
  1389. }
  1390. /**
  1391. * __pci_request_region - Reserved PCI I/O and memory resource
  1392. * @pdev: PCI device whose resources are to be reserved
  1393. * @bar: BAR to be reserved
  1394. * @res_name: Name to be associated with resource.
  1395. * @exclusive: whether the region access is exclusive or not
  1396. *
  1397. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1398. * being reserved by owner @res_name. Do not access any
  1399. * address inside the PCI regions unless this call returns
  1400. * successfully.
  1401. *
  1402. * If @exclusive is set, then the region is marked so that userspace
  1403. * is explicitly not allowed to map the resource via /dev/mem or
  1404. * sysfs MMIO access.
  1405. *
  1406. * Returns 0 on success, or %EBUSY on error. A warning
  1407. * message is also printed on failure.
  1408. */
  1409. static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
  1410. int exclusive)
  1411. {
  1412. struct pci_devres *dr;
  1413. if (pci_resource_len(pdev, bar) == 0)
  1414. return 0;
  1415. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  1416. if (!request_region(pci_resource_start(pdev, bar),
  1417. pci_resource_len(pdev, bar), res_name))
  1418. goto err_out;
  1419. }
  1420. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  1421. if (!__request_mem_region(pci_resource_start(pdev, bar),
  1422. pci_resource_len(pdev, bar), res_name,
  1423. exclusive))
  1424. goto err_out;
  1425. }
  1426. dr = find_pci_dr(pdev);
  1427. if (dr)
  1428. dr->region_mask |= 1 << bar;
  1429. return 0;
  1430. err_out:
  1431. dev_warn(&pdev->dev, "BAR %d: can't reserve %s region %pR\n",
  1432. bar,
  1433. pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
  1434. &pdev->resource[bar]);
  1435. return -EBUSY;
  1436. }
  1437. /**
  1438. * pci_request_region - Reserve PCI I/O and memory resource
  1439. * @pdev: PCI device whose resources are to be reserved
  1440. * @bar: BAR to be reserved
  1441. * @res_name: Name to be associated with resource
  1442. *
  1443. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  1444. * being reserved by owner @res_name. Do not access any
  1445. * address inside the PCI regions unless this call returns
  1446. * successfully.
  1447. *
  1448. * Returns 0 on success, or %EBUSY on error. A warning
  1449. * message is also printed on failure.
  1450. */
  1451. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  1452. {
  1453. return __pci_request_region(pdev, bar, res_name, 0);
  1454. }
  1455. /**
  1456. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  1457. * @pdev: PCI device whose resources are to be reserved
  1458. * @bar: BAR to be reserved
  1459. * @res_name: Name to be associated with resource.
  1460. *
  1461. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1462. * being reserved by owner @res_name. Do not access any
  1463. * address inside the PCI regions unless this call returns
  1464. * successfully.
  1465. *
  1466. * Returns 0 on success, or %EBUSY on error. A warning
  1467. * message is also printed on failure.
  1468. *
  1469. * The key difference that _exclusive makes it that userspace is
  1470. * explicitly not allowed to map the resource via /dev/mem or
  1471. * sysfs.
  1472. */
  1473. int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
  1474. {
  1475. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  1476. }
  1477. /**
  1478. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  1479. * @pdev: PCI device whose resources were previously reserved
  1480. * @bars: Bitmask of BARs to be released
  1481. *
  1482. * Release selected PCI I/O and memory resources previously reserved.
  1483. * Call this function only after all use of the PCI regions has ceased.
  1484. */
  1485. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  1486. {
  1487. int i;
  1488. for (i = 0; i < 6; i++)
  1489. if (bars & (1 << i))
  1490. pci_release_region(pdev, i);
  1491. }
  1492. int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1493. const char *res_name, int excl)
  1494. {
  1495. int i;
  1496. for (i = 0; i < 6; i++)
  1497. if (bars & (1 << i))
  1498. if (__pci_request_region(pdev, i, res_name, excl))
  1499. goto err_out;
  1500. return 0;
  1501. err_out:
  1502. while(--i >= 0)
  1503. if (bars & (1 << i))
  1504. pci_release_region(pdev, i);
  1505. return -EBUSY;
  1506. }
  1507. /**
  1508. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  1509. * @pdev: PCI device whose resources are to be reserved
  1510. * @bars: Bitmask of BARs to be requested
  1511. * @res_name: Name to be associated with resource
  1512. */
  1513. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1514. const char *res_name)
  1515. {
  1516. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  1517. }
  1518. int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
  1519. int bars, const char *res_name)
  1520. {
  1521. return __pci_request_selected_regions(pdev, bars, res_name,
  1522. IORESOURCE_EXCLUSIVE);
  1523. }
  1524. /**
  1525. * pci_release_regions - Release reserved PCI I/O and memory resources
  1526. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  1527. *
  1528. * Releases all PCI I/O and memory resources previously reserved by a
  1529. * successful call to pci_request_regions. Call this function only
  1530. * after all use of the PCI regions has ceased.
  1531. */
  1532. void pci_release_regions(struct pci_dev *pdev)
  1533. {
  1534. pci_release_selected_regions(pdev, (1 << 6) - 1);
  1535. }
  1536. /**
  1537. * pci_request_regions - Reserved PCI I/O and memory resources
  1538. * @pdev: PCI device whose resources are to be reserved
  1539. * @res_name: Name to be associated with resource.
  1540. *
  1541. * Mark all PCI regions associated with PCI device @pdev as
  1542. * being reserved by owner @res_name. Do not access any
  1543. * address inside the PCI regions unless this call returns
  1544. * successfully.
  1545. *
  1546. * Returns 0 on success, or %EBUSY on error. A warning
  1547. * message is also printed on failure.
  1548. */
  1549. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  1550. {
  1551. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  1552. }
  1553. /**
  1554. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  1555. * @pdev: PCI device whose resources are to be reserved
  1556. * @res_name: Name to be associated with resource.
  1557. *
  1558. * Mark all PCI regions associated with PCI device @pdev as
  1559. * being reserved by owner @res_name. Do not access any
  1560. * address inside the PCI regions unless this call returns
  1561. * successfully.
  1562. *
  1563. * pci_request_regions_exclusive() will mark the region so that
  1564. * /dev/mem and the sysfs MMIO access will not be allowed.
  1565. *
  1566. * Returns 0 on success, or %EBUSY on error. A warning
  1567. * message is also printed on failure.
  1568. */
  1569. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  1570. {
  1571. return pci_request_selected_regions_exclusive(pdev,
  1572. ((1 << 6) - 1), res_name);
  1573. }
  1574. static void __pci_set_master(struct pci_dev *dev, bool enable)
  1575. {
  1576. u16 old_cmd, cmd;
  1577. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  1578. if (enable)
  1579. cmd = old_cmd | PCI_COMMAND_MASTER;
  1580. else
  1581. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  1582. if (cmd != old_cmd) {
  1583. dev_dbg(&dev->dev, "%s bus mastering\n",
  1584. enable ? "enabling" : "disabling");
  1585. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1586. }
  1587. dev->is_busmaster = enable;
  1588. }
  1589. /**
  1590. * pci_set_master - enables bus-mastering for device dev
  1591. * @dev: the PCI device to enable
  1592. *
  1593. * Enables bus-mastering on the device and calls pcibios_set_master()
  1594. * to do the needed arch specific settings.
  1595. */
  1596. void pci_set_master(struct pci_dev *dev)
  1597. {
  1598. __pci_set_master(dev, true);
  1599. pcibios_set_master(dev);
  1600. }
  1601. /**
  1602. * pci_clear_master - disables bus-mastering for device dev
  1603. * @dev: the PCI device to disable
  1604. */
  1605. void pci_clear_master(struct pci_dev *dev)
  1606. {
  1607. __pci_set_master(dev, false);
  1608. }
  1609. #ifdef PCI_DISABLE_MWI
  1610. int pci_set_mwi(struct pci_dev *dev)
  1611. {
  1612. return 0;
  1613. }
  1614. int pci_try_set_mwi(struct pci_dev *dev)
  1615. {
  1616. return 0;
  1617. }
  1618. void pci_clear_mwi(struct pci_dev *dev)
  1619. {
  1620. }
  1621. #else
  1622. #ifndef PCI_CACHE_LINE_BYTES
  1623. #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES
  1624. #endif
  1625. /* This can be overridden by arch code. */
  1626. /* Don't forget this is measured in 32-bit words, not bytes */
  1627. u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4;
  1628. /**
  1629. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  1630. * @dev: the PCI device for which MWI is to be enabled
  1631. *
  1632. * Helper function for pci_set_mwi.
  1633. * Originally copied from drivers/net/acenic.c.
  1634. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  1635. *
  1636. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1637. */
  1638. static int
  1639. pci_set_cacheline_size(struct pci_dev *dev)
  1640. {
  1641. u8 cacheline_size;
  1642. if (!pci_cache_line_size)
  1643. return -EINVAL; /* The system doesn't support MWI. */
  1644. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  1645. equal to or multiple of the right value. */
  1646. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1647. if (cacheline_size >= pci_cache_line_size &&
  1648. (cacheline_size % pci_cache_line_size) == 0)
  1649. return 0;
  1650. /* Write the correct value. */
  1651. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  1652. /* Read it back. */
  1653. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1654. if (cacheline_size == pci_cache_line_size)
  1655. return 0;
  1656. dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
  1657. "supported\n", pci_cache_line_size << 2);
  1658. return -EINVAL;
  1659. }
  1660. /**
  1661. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  1662. * @dev: the PCI device for which MWI is enabled
  1663. *
  1664. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1665. *
  1666. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1667. */
  1668. int
  1669. pci_set_mwi(struct pci_dev *dev)
  1670. {
  1671. int rc;
  1672. u16 cmd;
  1673. rc = pci_set_cacheline_size(dev);
  1674. if (rc)
  1675. return rc;
  1676. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1677. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  1678. dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
  1679. cmd |= PCI_COMMAND_INVALIDATE;
  1680. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1681. }
  1682. return 0;
  1683. }
  1684. /**
  1685. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  1686. * @dev: the PCI device for which MWI is enabled
  1687. *
  1688. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1689. * Callers are not required to check the return value.
  1690. *
  1691. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1692. */
  1693. int pci_try_set_mwi(struct pci_dev *dev)
  1694. {
  1695. int rc = pci_set_mwi(dev);
  1696. return rc;
  1697. }
  1698. /**
  1699. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  1700. * @dev: the PCI device to disable
  1701. *
  1702. * Disables PCI Memory-Write-Invalidate transaction on the device
  1703. */
  1704. void
  1705. pci_clear_mwi(struct pci_dev *dev)
  1706. {
  1707. u16 cmd;
  1708. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1709. if (cmd & PCI_COMMAND_INVALIDATE) {
  1710. cmd &= ~PCI_COMMAND_INVALIDATE;
  1711. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1712. }
  1713. }
  1714. #endif /* ! PCI_DISABLE_MWI */
  1715. /**
  1716. * pci_intx - enables/disables PCI INTx for device dev
  1717. * @pdev: the PCI device to operate on
  1718. * @enable: boolean: whether to enable or disable PCI INTx
  1719. *
  1720. * Enables/disables PCI INTx for device dev
  1721. */
  1722. void
  1723. pci_intx(struct pci_dev *pdev, int enable)
  1724. {
  1725. u16 pci_command, new;
  1726. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  1727. if (enable) {
  1728. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  1729. } else {
  1730. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  1731. }
  1732. if (new != pci_command) {
  1733. struct pci_devres *dr;
  1734. pci_write_config_word(pdev, PCI_COMMAND, new);
  1735. dr = find_pci_dr(pdev);
  1736. if (dr && !dr->restore_intx) {
  1737. dr->restore_intx = 1;
  1738. dr->orig_intx = !enable;
  1739. }
  1740. }
  1741. }
  1742. /**
  1743. * pci_msi_off - disables any msi or msix capabilities
  1744. * @dev: the PCI device to operate on
  1745. *
  1746. * If you want to use msi see pci_enable_msi and friends.
  1747. * This is a lower level primitive that allows us to disable
  1748. * msi operation at the device level.
  1749. */
  1750. void pci_msi_off(struct pci_dev *dev)
  1751. {
  1752. int pos;
  1753. u16 control;
  1754. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1755. if (pos) {
  1756. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  1757. control &= ~PCI_MSI_FLAGS_ENABLE;
  1758. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  1759. }
  1760. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1761. if (pos) {
  1762. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  1763. control &= ~PCI_MSIX_FLAGS_ENABLE;
  1764. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  1765. }
  1766. }
  1767. #ifndef HAVE_ARCH_PCI_SET_DMA_MASK
  1768. /*
  1769. * These can be overridden by arch-specific implementations
  1770. */
  1771. int
  1772. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  1773. {
  1774. if (!pci_dma_supported(dev, mask))
  1775. return -EIO;
  1776. dev->dma_mask = mask;
  1777. return 0;
  1778. }
  1779. int
  1780. pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  1781. {
  1782. if (!pci_dma_supported(dev, mask))
  1783. return -EIO;
  1784. dev->dev.coherent_dma_mask = mask;
  1785. return 0;
  1786. }
  1787. #endif
  1788. #ifndef HAVE_ARCH_PCI_SET_DMA_MAX_SEGMENT_SIZE
  1789. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  1790. {
  1791. return dma_set_max_seg_size(&dev->dev, size);
  1792. }
  1793. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  1794. #endif
  1795. #ifndef HAVE_ARCH_PCI_SET_DMA_SEGMENT_BOUNDARY
  1796. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  1797. {
  1798. return dma_set_seg_boundary(&dev->dev, mask);
  1799. }
  1800. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  1801. #endif
  1802. static int pcie_flr(struct pci_dev *dev, int probe)
  1803. {
  1804. int i;
  1805. int pos;
  1806. u32 cap;
  1807. u16 status;
  1808. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1809. if (!pos)
  1810. return -ENOTTY;
  1811. pci_read_config_dword(dev, pos + PCI_EXP_DEVCAP, &cap);
  1812. if (!(cap & PCI_EXP_DEVCAP_FLR))
  1813. return -ENOTTY;
  1814. if (probe)
  1815. return 0;
  1816. /* Wait for Transaction Pending bit clean */
  1817. for (i = 0; i < 4; i++) {
  1818. if (i)
  1819. msleep((1 << (i - 1)) * 100);
  1820. pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &status);
  1821. if (!(status & PCI_EXP_DEVSTA_TRPND))
  1822. goto clear;
  1823. }
  1824. dev_err(&dev->dev, "transaction is not cleared; "
  1825. "proceeding with reset anyway\n");
  1826. clear:
  1827. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL,
  1828. PCI_EXP_DEVCTL_BCR_FLR);
  1829. msleep(100);
  1830. return 0;
  1831. }
  1832. static int pci_af_flr(struct pci_dev *dev, int probe)
  1833. {
  1834. int i;
  1835. int pos;
  1836. u8 cap;
  1837. u8 status;
  1838. pos = pci_find_capability(dev, PCI_CAP_ID_AF);
  1839. if (!pos)
  1840. return -ENOTTY;
  1841. pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
  1842. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  1843. return -ENOTTY;
  1844. if (probe)
  1845. return 0;
  1846. /* Wait for Transaction Pending bit clean */
  1847. for (i = 0; i < 4; i++) {
  1848. if (i)
  1849. msleep((1 << (i - 1)) * 100);
  1850. pci_read_config_byte(dev, pos + PCI_AF_STATUS, &status);
  1851. if (!(status & PCI_AF_STATUS_TP))
  1852. goto clear;
  1853. }
  1854. dev_err(&dev->dev, "transaction is not cleared; "
  1855. "proceeding with reset anyway\n");
  1856. clear:
  1857. pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  1858. msleep(100);
  1859. return 0;
  1860. }
  1861. static int pci_pm_reset(struct pci_dev *dev, int probe)
  1862. {
  1863. u16 csr;
  1864. if (!dev->pm_cap)
  1865. return -ENOTTY;
  1866. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
  1867. if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
  1868. return -ENOTTY;
  1869. if (probe)
  1870. return 0;
  1871. if (dev->current_state != PCI_D0)
  1872. return -EINVAL;
  1873. csr &= ~PCI_PM_CTRL_STATE_MASK;
  1874. csr |= PCI_D3hot;
  1875. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  1876. msleep(pci_pm_d3_delay);
  1877. csr &= ~PCI_PM_CTRL_STATE_MASK;
  1878. csr |= PCI_D0;
  1879. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  1880. msleep(pci_pm_d3_delay);
  1881. return 0;
  1882. }
  1883. static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
  1884. {
  1885. u16 ctrl;
  1886. struct pci_dev *pdev;
  1887. if (pci_is_root_bus(dev->bus) || dev->subordinate || !dev->bus->self)
  1888. return -ENOTTY;
  1889. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  1890. if (pdev != dev)
  1891. return -ENOTTY;
  1892. if (probe)
  1893. return 0;
  1894. pci_read_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, &ctrl);
  1895. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  1896. pci_write_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, ctrl);
  1897. msleep(100);
  1898. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  1899. pci_write_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, ctrl);
  1900. msleep(100);
  1901. return 0;
  1902. }
  1903. static int pci_dev_reset(struct pci_dev *dev, int probe)
  1904. {
  1905. int rc;
  1906. might_sleep();
  1907. if (!probe) {
  1908. pci_block_user_cfg_access(dev);
  1909. /* block PM suspend, driver probe, etc. */
  1910. down(&dev->dev.sem);
  1911. }
  1912. rc = pcie_flr(dev, probe);
  1913. if (rc != -ENOTTY)
  1914. goto done;
  1915. rc = pci_af_flr(dev, probe);
  1916. if (rc != -ENOTTY)
  1917. goto done;
  1918. rc = pci_pm_reset(dev, probe);
  1919. if (rc != -ENOTTY)
  1920. goto done;
  1921. rc = pci_parent_bus_reset(dev, probe);
  1922. done:
  1923. if (!probe) {
  1924. up(&dev->dev.sem);
  1925. pci_unblock_user_cfg_access(dev);
  1926. }
  1927. return rc;
  1928. }
  1929. /**
  1930. * __pci_reset_function - reset a PCI device function
  1931. * @dev: PCI device to reset
  1932. *
  1933. * Some devices allow an individual function to be reset without affecting
  1934. * other functions in the same device. The PCI device must be responsive
  1935. * to PCI config space in order to use this function.
  1936. *
  1937. * The device function is presumed to be unused when this function is called.
  1938. * Resetting the device will make the contents of PCI configuration space
  1939. * random, so any caller of this must be prepared to reinitialise the
  1940. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  1941. * etc.
  1942. *
  1943. * Returns 0 if the device function was successfully reset or negative if the
  1944. * device doesn't support resetting a single function.
  1945. */
  1946. int __pci_reset_function(struct pci_dev *dev)
  1947. {
  1948. return pci_dev_reset(dev, 0);
  1949. }
  1950. EXPORT_SYMBOL_GPL(__pci_reset_function);
  1951. /**
  1952. * pci_probe_reset_function - check whether the device can be safely reset
  1953. * @dev: PCI device to reset
  1954. *
  1955. * Some devices allow an individual function to be reset without affecting
  1956. * other functions in the same device. The PCI device must be responsive
  1957. * to PCI config space in order to use this function.
  1958. *
  1959. * Returns 0 if the device function can be reset or negative if the
  1960. * device doesn't support resetting a single function.
  1961. */
  1962. int pci_probe_reset_function(struct pci_dev *dev)
  1963. {
  1964. return pci_dev_reset(dev, 1);
  1965. }
  1966. /**
  1967. * pci_reset_function - quiesce and reset a PCI device function
  1968. * @dev: PCI device to reset
  1969. *
  1970. * Some devices allow an individual function to be reset without affecting
  1971. * other functions in the same device. The PCI device must be responsive
  1972. * to PCI config space in order to use this function.
  1973. *
  1974. * This function does not just reset the PCI portion of a device, but
  1975. * clears all the state associated with the device. This function differs
  1976. * from __pci_reset_function in that it saves and restores device state
  1977. * over the reset.
  1978. *
  1979. * Returns 0 if the device function was successfully reset or negative if the
  1980. * device doesn't support resetting a single function.
  1981. */
  1982. int pci_reset_function(struct pci_dev *dev)
  1983. {
  1984. int rc;
  1985. rc = pci_dev_reset(dev, 1);
  1986. if (rc)
  1987. return rc;
  1988. pci_save_state(dev);
  1989. /*
  1990. * both INTx and MSI are disabled after the Interrupt Disable bit
  1991. * is set and the Bus Master bit is cleared.
  1992. */
  1993. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  1994. rc = pci_dev_reset(dev, 0);
  1995. pci_restore_state(dev);
  1996. return rc;
  1997. }
  1998. EXPORT_SYMBOL_GPL(pci_reset_function);
  1999. /**
  2000. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  2001. * @dev: PCI device to query
  2002. *
  2003. * Returns mmrbc: maximum designed memory read count in bytes
  2004. * or appropriate error value.
  2005. */
  2006. int pcix_get_max_mmrbc(struct pci_dev *dev)
  2007. {
  2008. int err, cap;
  2009. u32 stat;
  2010. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  2011. if (!cap)
  2012. return -EINVAL;
  2013. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  2014. if (err)
  2015. return -EINVAL;
  2016. return (stat & PCI_X_STATUS_MAX_READ) >> 12;
  2017. }
  2018. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  2019. /**
  2020. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  2021. * @dev: PCI device to query
  2022. *
  2023. * Returns mmrbc: maximum memory read count in bytes
  2024. * or appropriate error value.
  2025. */
  2026. int pcix_get_mmrbc(struct pci_dev *dev)
  2027. {
  2028. int ret, cap;
  2029. u32 cmd;
  2030. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  2031. if (!cap)
  2032. return -EINVAL;
  2033. ret = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  2034. if (!ret)
  2035. ret = 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  2036. return ret;
  2037. }
  2038. EXPORT_SYMBOL(pcix_get_mmrbc);
  2039. /**
  2040. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  2041. * @dev: PCI device to query
  2042. * @mmrbc: maximum memory read count in bytes
  2043. * valid values are 512, 1024, 2048, 4096
  2044. *
  2045. * If possible sets maximum memory read byte count, some bridges have erratas
  2046. * that prevent this.
  2047. */
  2048. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  2049. {
  2050. int cap, err = -EINVAL;
  2051. u32 stat, cmd, v, o;
  2052. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  2053. goto out;
  2054. v = ffs(mmrbc) - 10;
  2055. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  2056. if (!cap)
  2057. goto out;
  2058. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  2059. if (err)
  2060. goto out;
  2061. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  2062. return -E2BIG;
  2063. err = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  2064. if (err)
  2065. goto out;
  2066. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  2067. if (o != v) {
  2068. if (v > o && dev->bus &&
  2069. (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  2070. return -EIO;
  2071. cmd &= ~PCI_X_CMD_MAX_READ;
  2072. cmd |= v << 2;
  2073. err = pci_write_config_dword(dev, cap + PCI_X_CMD, cmd);
  2074. }
  2075. out:
  2076. return err;
  2077. }
  2078. EXPORT_SYMBOL(pcix_set_mmrbc);
  2079. /**
  2080. * pcie_get_readrq - get PCI Express read request size
  2081. * @dev: PCI device to query
  2082. *
  2083. * Returns maximum memory read request in bytes
  2084. * or appropriate error value.
  2085. */
  2086. int pcie_get_readrq(struct pci_dev *dev)
  2087. {
  2088. int ret, cap;
  2089. u16 ctl;
  2090. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  2091. if (!cap)
  2092. return -EINVAL;
  2093. ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  2094. if (!ret)
  2095. ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  2096. return ret;
  2097. }
  2098. EXPORT_SYMBOL(pcie_get_readrq);
  2099. /**
  2100. * pcie_set_readrq - set PCI Express maximum memory read request
  2101. * @dev: PCI device to query
  2102. * @rq: maximum memory read count in bytes
  2103. * valid values are 128, 256, 512, 1024, 2048, 4096
  2104. *
  2105. * If possible sets maximum read byte count
  2106. */
  2107. int pcie_set_readrq(struct pci_dev *dev, int rq)
  2108. {
  2109. int cap, err = -EINVAL;
  2110. u16 ctl, v;
  2111. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  2112. goto out;
  2113. v = (ffs(rq) - 8) << 12;
  2114. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  2115. if (!cap)
  2116. goto out;
  2117. err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  2118. if (err)
  2119. goto out;
  2120. if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
  2121. ctl &= ~PCI_EXP_DEVCTL_READRQ;
  2122. ctl |= v;
  2123. err = pci_write_config_dword(dev, cap + PCI_EXP_DEVCTL, ctl);
  2124. }
  2125. out:
  2126. return err;
  2127. }
  2128. EXPORT_SYMBOL(pcie_set_readrq);
  2129. /**
  2130. * pci_select_bars - Make BAR mask from the type of resource
  2131. * @dev: the PCI device for which BAR mask is made
  2132. * @flags: resource type mask to be selected
  2133. *
  2134. * This helper routine makes bar mask from the type of resource.
  2135. */
  2136. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  2137. {
  2138. int i, bars = 0;
  2139. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  2140. if (pci_resource_flags(dev, i) & flags)
  2141. bars |= (1 << i);
  2142. return bars;
  2143. }
  2144. /**
  2145. * pci_resource_bar - get position of the BAR associated with a resource
  2146. * @dev: the PCI device
  2147. * @resno: the resource number
  2148. * @type: the BAR type to be filled in
  2149. *
  2150. * Returns BAR position in config space, or 0 if the BAR is invalid.
  2151. */
  2152. int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
  2153. {
  2154. int reg;
  2155. if (resno < PCI_ROM_RESOURCE) {
  2156. *type = pci_bar_unknown;
  2157. return PCI_BASE_ADDRESS_0 + 4 * resno;
  2158. } else if (resno == PCI_ROM_RESOURCE) {
  2159. *type = pci_bar_mem32;
  2160. return dev->rom_base_reg;
  2161. } else if (resno < PCI_BRIDGE_RESOURCES) {
  2162. /* device specific resource */
  2163. reg = pci_iov_resource_bar(dev, resno, type);
  2164. if (reg)
  2165. return reg;
  2166. }
  2167. dev_err(&dev->dev, "BAR: invalid resource #%d\n", resno);
  2168. return 0;
  2169. }
  2170. /**
  2171. * pci_set_vga_state - set VGA decode state on device and parents if requested
  2172. * @dev the PCI device
  2173. * @decode - true = enable decoding, false = disable decoding
  2174. * @command_bits PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
  2175. * @change_bridge - traverse ancestors and change bridges
  2176. */
  2177. int pci_set_vga_state(struct pci_dev *dev, bool decode,
  2178. unsigned int command_bits, bool change_bridge)
  2179. {
  2180. struct pci_bus *bus;
  2181. struct pci_dev *bridge;
  2182. u16 cmd;
  2183. WARN_ON(command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY));
  2184. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2185. if (decode == true)
  2186. cmd |= command_bits;
  2187. else
  2188. cmd &= ~command_bits;
  2189. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2190. if (change_bridge == false)
  2191. return 0;
  2192. bus = dev->bus;
  2193. while (bus) {
  2194. bridge = bus->self;
  2195. if (bridge) {
  2196. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
  2197. &cmd);
  2198. if (decode == true)
  2199. cmd |= PCI_BRIDGE_CTL_VGA;
  2200. else
  2201. cmd &= ~PCI_BRIDGE_CTL_VGA;
  2202. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
  2203. cmd);
  2204. }
  2205. bus = bus->parent;
  2206. }
  2207. return 0;
  2208. }
  2209. #define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
  2210. static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
  2211. spinlock_t resource_alignment_lock = SPIN_LOCK_UNLOCKED;
  2212. /**
  2213. * pci_specified_resource_alignment - get resource alignment specified by user.
  2214. * @dev: the PCI device to get
  2215. *
  2216. * RETURNS: Resource alignment if it is specified.
  2217. * Zero if it is not specified.
  2218. */
  2219. resource_size_t pci_specified_resource_alignment(struct pci_dev *dev)
  2220. {
  2221. int seg, bus, slot, func, align_order, count;
  2222. resource_size_t align = 0;
  2223. char *p;
  2224. spin_lock(&resource_alignment_lock);
  2225. p = resource_alignment_param;
  2226. while (*p) {
  2227. count = 0;
  2228. if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
  2229. p[count] == '@') {
  2230. p += count + 1;
  2231. } else {
  2232. align_order = -1;
  2233. }
  2234. if (sscanf(p, "%x:%x:%x.%x%n",
  2235. &seg, &bus, &slot, &func, &count) != 4) {
  2236. seg = 0;
  2237. if (sscanf(p, "%x:%x.%x%n",
  2238. &bus, &slot, &func, &count) != 3) {
  2239. /* Invalid format */
  2240. printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
  2241. p);
  2242. break;
  2243. }
  2244. }
  2245. p += count;
  2246. if (seg == pci_domain_nr(dev->bus) &&
  2247. bus == dev->bus->number &&
  2248. slot == PCI_SLOT(dev->devfn) &&
  2249. func == PCI_FUNC(dev->devfn)) {
  2250. if (align_order == -1) {
  2251. align = PAGE_SIZE;
  2252. } else {
  2253. align = 1 << align_order;
  2254. }
  2255. /* Found */
  2256. break;
  2257. }
  2258. if (*p != ';' && *p != ',') {
  2259. /* End of param or invalid format */
  2260. break;
  2261. }
  2262. p++;
  2263. }
  2264. spin_unlock(&resource_alignment_lock);
  2265. return align;
  2266. }
  2267. /**
  2268. * pci_is_reassigndev - check if specified PCI is target device to reassign
  2269. * @dev: the PCI device to check
  2270. *
  2271. * RETURNS: non-zero for PCI device is a target device to reassign,
  2272. * or zero is not.
  2273. */
  2274. int pci_is_reassigndev(struct pci_dev *dev)
  2275. {
  2276. return (pci_specified_resource_alignment(dev) != 0);
  2277. }
  2278. ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
  2279. {
  2280. if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
  2281. count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
  2282. spin_lock(&resource_alignment_lock);
  2283. strncpy(resource_alignment_param, buf, count);
  2284. resource_alignment_param[count] = '\0';
  2285. spin_unlock(&resource_alignment_lock);
  2286. return count;
  2287. }
  2288. ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
  2289. {
  2290. size_t count;
  2291. spin_lock(&resource_alignment_lock);
  2292. count = snprintf(buf, size, "%s", resource_alignment_param);
  2293. spin_unlock(&resource_alignment_lock);
  2294. return count;
  2295. }
  2296. static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
  2297. {
  2298. return pci_get_resource_alignment_param(buf, PAGE_SIZE);
  2299. }
  2300. static ssize_t pci_resource_alignment_store(struct bus_type *bus,
  2301. const char *buf, size_t count)
  2302. {
  2303. return pci_set_resource_alignment_param(buf, count);
  2304. }
  2305. BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
  2306. pci_resource_alignment_store);
  2307. static int __init pci_resource_alignment_sysfs_init(void)
  2308. {
  2309. return bus_create_file(&pci_bus_type,
  2310. &bus_attr_resource_alignment);
  2311. }
  2312. late_initcall(pci_resource_alignment_sysfs_init);
  2313. static void __devinit pci_no_domains(void)
  2314. {
  2315. #ifdef CONFIG_PCI_DOMAINS
  2316. pci_domains_supported = 0;
  2317. #endif
  2318. }
  2319. /**
  2320. * pci_ext_cfg_enabled - can we access extended PCI config space?
  2321. * @dev: The PCI device of the root bridge.
  2322. *
  2323. * Returns 1 if we can access PCI extended config space (offsets
  2324. * greater than 0xff). This is the default implementation. Architecture
  2325. * implementations can override this.
  2326. */
  2327. int __attribute__ ((weak)) pci_ext_cfg_avail(struct pci_dev *dev)
  2328. {
  2329. return 1;
  2330. }
  2331. static int __devinit pci_init(void)
  2332. {
  2333. struct pci_dev *dev = NULL;
  2334. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  2335. pci_fixup_device(pci_fixup_final, dev);
  2336. }
  2337. return 0;
  2338. }
  2339. static int __init pci_setup(char *str)
  2340. {
  2341. while (str) {
  2342. char *k = strchr(str, ',');
  2343. if (k)
  2344. *k++ = 0;
  2345. if (*str && (str = pcibios_setup(str)) && *str) {
  2346. if (!strcmp(str, "nomsi")) {
  2347. pci_no_msi();
  2348. } else if (!strcmp(str, "noaer")) {
  2349. pci_no_aer();
  2350. } else if (!strcmp(str, "nodomains")) {
  2351. pci_no_domains();
  2352. } else if (!strncmp(str, "cbiosize=", 9)) {
  2353. pci_cardbus_io_size = memparse(str + 9, &str);
  2354. } else if (!strncmp(str, "cbmemsize=", 10)) {
  2355. pci_cardbus_mem_size = memparse(str + 10, &str);
  2356. } else if (!strncmp(str, "resource_alignment=", 19)) {
  2357. pci_set_resource_alignment_param(str + 19,
  2358. strlen(str + 19));
  2359. } else if (!strncmp(str, "ecrc=", 5)) {
  2360. pcie_ecrc_get_policy(str + 5);
  2361. } else if (!strncmp(str, "hpiosize=", 9)) {
  2362. pci_hotplug_io_size = memparse(str + 9, &str);
  2363. } else if (!strncmp(str, "hpmemsize=", 10)) {
  2364. pci_hotplug_mem_size = memparse(str + 10, &str);
  2365. } else {
  2366. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  2367. str);
  2368. }
  2369. }
  2370. str = k;
  2371. }
  2372. return 0;
  2373. }
  2374. early_param("pci", pci_setup);
  2375. device_initcall(pci_init);
  2376. EXPORT_SYMBOL(pci_reenable_device);
  2377. EXPORT_SYMBOL(pci_enable_device_io);
  2378. EXPORT_SYMBOL(pci_enable_device_mem);
  2379. EXPORT_SYMBOL(pci_enable_device);
  2380. EXPORT_SYMBOL(pcim_enable_device);
  2381. EXPORT_SYMBOL(pcim_pin_device);
  2382. EXPORT_SYMBOL(pci_disable_device);
  2383. EXPORT_SYMBOL(pci_find_capability);
  2384. EXPORT_SYMBOL(pci_bus_find_capability);
  2385. EXPORT_SYMBOL(pci_release_regions);
  2386. EXPORT_SYMBOL(pci_request_regions);
  2387. EXPORT_SYMBOL(pci_request_regions_exclusive);
  2388. EXPORT_SYMBOL(pci_release_region);
  2389. EXPORT_SYMBOL(pci_request_region);
  2390. EXPORT_SYMBOL(pci_request_region_exclusive);
  2391. EXPORT_SYMBOL(pci_release_selected_regions);
  2392. EXPORT_SYMBOL(pci_request_selected_regions);
  2393. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  2394. EXPORT_SYMBOL(pci_set_master);
  2395. EXPORT_SYMBOL(pci_clear_master);
  2396. EXPORT_SYMBOL(pci_set_mwi);
  2397. EXPORT_SYMBOL(pci_try_set_mwi);
  2398. EXPORT_SYMBOL(pci_clear_mwi);
  2399. EXPORT_SYMBOL_GPL(pci_intx);
  2400. EXPORT_SYMBOL(pci_set_dma_mask);
  2401. EXPORT_SYMBOL(pci_set_consistent_dma_mask);
  2402. EXPORT_SYMBOL(pci_assign_resource);
  2403. EXPORT_SYMBOL(pci_find_parent_resource);
  2404. EXPORT_SYMBOL(pci_select_bars);
  2405. EXPORT_SYMBOL(pci_set_power_state);
  2406. EXPORT_SYMBOL(pci_save_state);
  2407. EXPORT_SYMBOL(pci_restore_state);
  2408. EXPORT_SYMBOL(pci_pme_capable);
  2409. EXPORT_SYMBOL(pci_pme_active);
  2410. EXPORT_SYMBOL(pci_enable_wake);
  2411. EXPORT_SYMBOL(pci_wake_from_d3);
  2412. EXPORT_SYMBOL(pci_target_state);
  2413. EXPORT_SYMBOL(pci_prepare_to_sleep);
  2414. EXPORT_SYMBOL(pci_back_from_sleep);
  2415. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);