pci.c 67 KB

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