pci.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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 <asm/dma.h> /* isa_dma_bridge_buggy */
  20. #include "pci.h"
  21. unsigned int pci_pm_d3_delay = 10;
  22. #ifdef CONFIG_PCI_DOMAINS
  23. int pci_domains_supported = 1;
  24. #endif
  25. #define DEFAULT_CARDBUS_IO_SIZE (256)
  26. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  27. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  28. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  29. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  30. /**
  31. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  32. * @bus: pointer to PCI bus structure to search
  33. *
  34. * Given a PCI bus, returns the highest PCI bus number present in the set
  35. * including the given PCI bus and its list of child PCI buses.
  36. */
  37. unsigned char pci_bus_max_busnr(struct pci_bus* bus)
  38. {
  39. struct list_head *tmp;
  40. unsigned char max, n;
  41. max = bus->subordinate;
  42. list_for_each(tmp, &bus->children) {
  43. n = pci_bus_max_busnr(pci_bus_b(tmp));
  44. if(n > max)
  45. max = n;
  46. }
  47. return max;
  48. }
  49. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  50. #if 0
  51. /**
  52. * pci_max_busnr - returns maximum PCI bus number
  53. *
  54. * Returns the highest PCI bus number present in the system global list of
  55. * PCI buses.
  56. */
  57. unsigned char __devinit
  58. pci_max_busnr(void)
  59. {
  60. struct pci_bus *bus = NULL;
  61. unsigned char max, n;
  62. max = 0;
  63. while ((bus = pci_find_next_bus(bus)) != NULL) {
  64. n = pci_bus_max_busnr(bus);
  65. if(n > max)
  66. max = n;
  67. }
  68. return max;
  69. }
  70. #endif /* 0 */
  71. #define PCI_FIND_CAP_TTL 48
  72. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  73. u8 pos, int cap, int *ttl)
  74. {
  75. u8 id;
  76. while ((*ttl)--) {
  77. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  78. if (pos < 0x40)
  79. break;
  80. pos &= ~3;
  81. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  82. &id);
  83. if (id == 0xff)
  84. break;
  85. if (id == cap)
  86. return pos;
  87. pos += PCI_CAP_LIST_NEXT;
  88. }
  89. return 0;
  90. }
  91. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  92. u8 pos, int cap)
  93. {
  94. int ttl = PCI_FIND_CAP_TTL;
  95. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  96. }
  97. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  98. {
  99. return __pci_find_next_cap(dev->bus, dev->devfn,
  100. pos + PCI_CAP_LIST_NEXT, cap);
  101. }
  102. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  103. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  104. unsigned int devfn, u8 hdr_type)
  105. {
  106. u16 status;
  107. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  108. if (!(status & PCI_STATUS_CAP_LIST))
  109. return 0;
  110. switch (hdr_type) {
  111. case PCI_HEADER_TYPE_NORMAL:
  112. case PCI_HEADER_TYPE_BRIDGE:
  113. return PCI_CAPABILITY_LIST;
  114. case PCI_HEADER_TYPE_CARDBUS:
  115. return PCI_CB_CAPABILITY_LIST;
  116. default:
  117. return 0;
  118. }
  119. return 0;
  120. }
  121. /**
  122. * pci_find_capability - query for devices' capabilities
  123. * @dev: PCI device to query
  124. * @cap: capability code
  125. *
  126. * Tell if a device supports a given PCI capability.
  127. * Returns the address of the requested capability structure within the
  128. * device's PCI configuration space or 0 in case the device does not
  129. * support it. Possible values for @cap:
  130. *
  131. * %PCI_CAP_ID_PM Power Management
  132. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  133. * %PCI_CAP_ID_VPD Vital Product Data
  134. * %PCI_CAP_ID_SLOTID Slot Identification
  135. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  136. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  137. * %PCI_CAP_ID_PCIX PCI-X
  138. * %PCI_CAP_ID_EXP PCI Express
  139. */
  140. int pci_find_capability(struct pci_dev *dev, int cap)
  141. {
  142. int pos;
  143. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  144. if (pos)
  145. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  146. return pos;
  147. }
  148. /**
  149. * pci_bus_find_capability - query for devices' capabilities
  150. * @bus: the PCI bus to query
  151. * @devfn: PCI device to query
  152. * @cap: capability code
  153. *
  154. * Like pci_find_capability() but works for pci devices that do not have a
  155. * pci_dev structure set up yet.
  156. *
  157. * Returns the address of the requested capability structure within the
  158. * device's PCI configuration space or 0 in case the device does not
  159. * support it.
  160. */
  161. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  162. {
  163. int pos;
  164. u8 hdr_type;
  165. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  166. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  167. if (pos)
  168. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  169. return pos;
  170. }
  171. /**
  172. * pci_find_ext_capability - Find an extended capability
  173. * @dev: PCI device to query
  174. * @cap: capability code
  175. *
  176. * Returns the address of the requested extended capability structure
  177. * within the device's PCI configuration space or 0 if the device does
  178. * not support it. Possible values for @cap:
  179. *
  180. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  181. * %PCI_EXT_CAP_ID_VC Virtual Channel
  182. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  183. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  184. */
  185. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  186. {
  187. u32 header;
  188. int ttl = 480; /* 3840 bytes, minimum 8 bytes per capability */
  189. int pos = 0x100;
  190. if (dev->cfg_size <= 256)
  191. return 0;
  192. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  193. return 0;
  194. /*
  195. * If we have no capabilities, this is indicated by cap ID,
  196. * cap version and next pointer all being 0.
  197. */
  198. if (header == 0)
  199. return 0;
  200. while (ttl-- > 0) {
  201. if (PCI_EXT_CAP_ID(header) == cap)
  202. return pos;
  203. pos = PCI_EXT_CAP_NEXT(header);
  204. if (pos < 0x100)
  205. break;
  206. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  207. break;
  208. }
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  212. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  213. {
  214. int rc, ttl = PCI_FIND_CAP_TTL;
  215. u8 cap, mask;
  216. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  217. mask = HT_3BIT_CAP_MASK;
  218. else
  219. mask = HT_5BIT_CAP_MASK;
  220. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  221. PCI_CAP_ID_HT, &ttl);
  222. while (pos) {
  223. rc = pci_read_config_byte(dev, pos + 3, &cap);
  224. if (rc != PCIBIOS_SUCCESSFUL)
  225. return 0;
  226. if ((cap & mask) == ht_cap)
  227. return pos;
  228. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  229. pos + PCI_CAP_LIST_NEXT,
  230. PCI_CAP_ID_HT, &ttl);
  231. }
  232. return 0;
  233. }
  234. /**
  235. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  236. * @dev: PCI device to query
  237. * @pos: Position from which to continue searching
  238. * @ht_cap: Hypertransport capability code
  239. *
  240. * To be used in conjunction with pci_find_ht_capability() to search for
  241. * all capabilities matching @ht_cap. @pos should always be a value returned
  242. * from pci_find_ht_capability().
  243. *
  244. * NB. To be 100% safe against broken PCI devices, the caller should take
  245. * steps to avoid an infinite loop.
  246. */
  247. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  248. {
  249. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  250. }
  251. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  252. /**
  253. * pci_find_ht_capability - query a device's Hypertransport capabilities
  254. * @dev: PCI device to query
  255. * @ht_cap: Hypertransport capability code
  256. *
  257. * Tell if a device supports a given Hypertransport capability.
  258. * Returns an address within the device's PCI configuration space
  259. * or 0 in case the device does not support the request capability.
  260. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  261. * which has a Hypertransport capability matching @ht_cap.
  262. */
  263. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  264. {
  265. int pos;
  266. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  267. if (pos)
  268. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  269. return pos;
  270. }
  271. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  272. /**
  273. * pci_find_parent_resource - return resource region of parent bus of given region
  274. * @dev: PCI device structure contains resources to be searched
  275. * @res: child resource record for which parent is sought
  276. *
  277. * For given resource region of given device, return the resource
  278. * region of parent bus the given region is contained in or where
  279. * it should be allocated from.
  280. */
  281. struct resource *
  282. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  283. {
  284. const struct pci_bus *bus = dev->bus;
  285. int i;
  286. struct resource *best = NULL;
  287. for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  288. struct resource *r = bus->resource[i];
  289. if (!r)
  290. continue;
  291. if (res->start && !(res->start >= r->start && res->end <= r->end))
  292. continue; /* Not contained */
  293. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  294. continue; /* Wrong type */
  295. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  296. return r; /* Exact match */
  297. if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
  298. best = r; /* Approximating prefetchable by non-prefetchable */
  299. }
  300. return best;
  301. }
  302. /**
  303. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  304. * @dev: PCI device to have its BARs restored
  305. *
  306. * Restore the BAR values for a given device, so as to make it
  307. * accessible by its driver.
  308. */
  309. static void
  310. pci_restore_bars(struct pci_dev *dev)
  311. {
  312. int i, numres;
  313. switch (dev->hdr_type) {
  314. case PCI_HEADER_TYPE_NORMAL:
  315. numres = 6;
  316. break;
  317. case PCI_HEADER_TYPE_BRIDGE:
  318. numres = 2;
  319. break;
  320. case PCI_HEADER_TYPE_CARDBUS:
  321. numres = 1;
  322. break;
  323. default:
  324. /* Should never get here, but just in case... */
  325. return;
  326. }
  327. for (i = 0; i < numres; i ++)
  328. pci_update_resource(dev, &dev->resource[i], i);
  329. }
  330. int (*platform_pci_set_power_state)(struct pci_dev *dev, pci_power_t t);
  331. /**
  332. * pci_set_power_state - Set the power state of a PCI device
  333. * @dev: PCI device to be suspended
  334. * @state: PCI power state (D0, D1, D2, D3hot, D3cold) we're entering
  335. *
  336. * Transition a device to a new power state, using the Power Management
  337. * Capabilities in the device's config space.
  338. *
  339. * RETURN VALUE:
  340. * -EINVAL if trying to enter a lower state than we're already in.
  341. * 0 if we're already in the requested state.
  342. * -EIO if device does not support PCI PM.
  343. * 0 if we can successfully change the power state.
  344. */
  345. int
  346. pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  347. {
  348. int pm, need_restore = 0;
  349. u16 pmcsr, pmc;
  350. /* bound the state we're entering */
  351. if (state > PCI_D3hot)
  352. state = PCI_D3hot;
  353. /*
  354. * If the device or the parent bridge can't support PCI PM, ignore
  355. * the request if we're doing anything besides putting it into D0
  356. * (which would only happen on boot).
  357. */
  358. if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  359. return 0;
  360. /* find PCI PM capability in list */
  361. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  362. /* abort if the device doesn't support PM capabilities */
  363. if (!pm)
  364. return -EIO;
  365. /* Validate current state:
  366. * Can enter D0 from any state, but if we can only go deeper
  367. * to sleep if we're already in a low power state
  368. */
  369. if (state != PCI_D0 && dev->current_state > state) {
  370. printk(KERN_ERR "%s(): %s: state=%d, current state=%d\n",
  371. __func__, pci_name(dev), state, dev->current_state);
  372. return -EINVAL;
  373. } else if (dev->current_state == state)
  374. return 0; /* we're already there */
  375. pci_read_config_word(dev,pm + PCI_PM_PMC,&pmc);
  376. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  377. printk(KERN_DEBUG
  378. "PCI: %s has unsupported PM cap regs version (%u)\n",
  379. pci_name(dev), pmc & PCI_PM_CAP_VER_MASK);
  380. return -EIO;
  381. }
  382. /* check if this device supports the desired state */
  383. if (state == PCI_D1 && !(pmc & PCI_PM_CAP_D1))
  384. return -EIO;
  385. else if (state == PCI_D2 && !(pmc & PCI_PM_CAP_D2))
  386. return -EIO;
  387. pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
  388. /* If we're (effectively) in D3, force entire word to 0.
  389. * This doesn't affect PME_Status, disables PME_En, and
  390. * sets PowerState to 0.
  391. */
  392. switch (dev->current_state) {
  393. case PCI_D0:
  394. case PCI_D1:
  395. case PCI_D2:
  396. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  397. pmcsr |= state;
  398. break;
  399. case PCI_UNKNOWN: /* Boot-up */
  400. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  401. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  402. need_restore = 1;
  403. /* Fall-through: force to D0 */
  404. default:
  405. pmcsr = 0;
  406. break;
  407. }
  408. /* enter specified state */
  409. pci_write_config_word(dev, pm + PCI_PM_CTRL, pmcsr);
  410. /* Mandatory power management transition delays */
  411. /* see PCI PM 1.1 5.6.1 table 18 */
  412. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  413. msleep(pci_pm_d3_delay);
  414. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  415. udelay(200);
  416. /*
  417. * Give firmware a chance to be called, such as ACPI _PRx, _PSx
  418. * Firmware method after native method ?
  419. */
  420. if (platform_pci_set_power_state)
  421. platform_pci_set_power_state(dev, state);
  422. dev->current_state = state;
  423. /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  424. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  425. * from D3hot to D0 _may_ perform an internal reset, thereby
  426. * going to "D0 Uninitialized" rather than "D0 Initialized".
  427. * For example, at least some versions of the 3c905B and the
  428. * 3c556B exhibit this behaviour.
  429. *
  430. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  431. * devices in a D3hot state at boot. Consequently, we need to
  432. * restore at least the BARs so that the device will be
  433. * accessible to its driver.
  434. */
  435. if (need_restore)
  436. pci_restore_bars(dev);
  437. if (dev->bus->self)
  438. pcie_aspm_pm_state_change(dev->bus->self);
  439. return 0;
  440. }
  441. pci_power_t (*platform_pci_choose_state)(struct pci_dev *dev, pm_message_t state);
  442. /**
  443. * pci_choose_state - Choose the power state of a PCI device
  444. * @dev: PCI device to be suspended
  445. * @state: target sleep state for the whole system. This is the value
  446. * that is passed to suspend() function.
  447. *
  448. * Returns PCI power state suitable for given device and given system
  449. * message.
  450. */
  451. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  452. {
  453. pci_power_t ret;
  454. if (!pci_find_capability(dev, PCI_CAP_ID_PM))
  455. return PCI_D0;
  456. if (platform_pci_choose_state) {
  457. ret = platform_pci_choose_state(dev, state);
  458. if (ret != PCI_POWER_ERROR)
  459. return ret;
  460. }
  461. switch (state.event) {
  462. case PM_EVENT_ON:
  463. return PCI_D0;
  464. case PM_EVENT_FREEZE:
  465. case PM_EVENT_PRETHAW:
  466. /* REVISIT both freeze and pre-thaw "should" use D0 */
  467. case PM_EVENT_SUSPEND:
  468. case PM_EVENT_HIBERNATE:
  469. return PCI_D3hot;
  470. default:
  471. printk("Unrecognized suspend event %d\n", state.event);
  472. BUG();
  473. }
  474. return PCI_D0;
  475. }
  476. EXPORT_SYMBOL(pci_choose_state);
  477. static int pci_save_pcie_state(struct pci_dev *dev)
  478. {
  479. int pos, i = 0;
  480. struct pci_cap_saved_state *save_state;
  481. u16 *cap;
  482. int found = 0;
  483. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  484. if (pos <= 0)
  485. return 0;
  486. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  487. if (!save_state)
  488. save_state = kzalloc(sizeof(*save_state) + sizeof(u16) * 4, GFP_KERNEL);
  489. else
  490. found = 1;
  491. if (!save_state) {
  492. dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n");
  493. return -ENOMEM;
  494. }
  495. cap = (u16 *)&save_state->data[0];
  496. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]);
  497. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
  498. pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
  499. pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
  500. save_state->cap_nr = PCI_CAP_ID_EXP;
  501. if (!found)
  502. pci_add_saved_cap(dev, save_state);
  503. return 0;
  504. }
  505. static void pci_restore_pcie_state(struct pci_dev *dev)
  506. {
  507. int i = 0, pos;
  508. struct pci_cap_saved_state *save_state;
  509. u16 *cap;
  510. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  511. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  512. if (!save_state || pos <= 0)
  513. return;
  514. cap = (u16 *)&save_state->data[0];
  515. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]);
  516. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]);
  517. pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]);
  518. pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);
  519. }
  520. static int pci_save_pcix_state(struct pci_dev *dev)
  521. {
  522. int pos, i = 0;
  523. struct pci_cap_saved_state *save_state;
  524. u16 *cap;
  525. int found = 0;
  526. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  527. if (pos <= 0)
  528. return 0;
  529. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  530. if (!save_state)
  531. save_state = kzalloc(sizeof(*save_state) + sizeof(u16), GFP_KERNEL);
  532. else
  533. found = 1;
  534. if (!save_state) {
  535. dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n");
  536. return -ENOMEM;
  537. }
  538. cap = (u16 *)&save_state->data[0];
  539. pci_read_config_word(dev, pos + PCI_X_CMD, &cap[i++]);
  540. save_state->cap_nr = PCI_CAP_ID_PCIX;
  541. if (!found)
  542. pci_add_saved_cap(dev, save_state);
  543. return 0;
  544. }
  545. static void pci_restore_pcix_state(struct pci_dev *dev)
  546. {
  547. int i = 0, pos;
  548. struct pci_cap_saved_state *save_state;
  549. u16 *cap;
  550. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  551. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  552. if (!save_state || pos <= 0)
  553. return;
  554. cap = (u16 *)&save_state->data[0];
  555. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  556. }
  557. /**
  558. * pci_save_state - save the PCI configuration space of a device before suspending
  559. * @dev: - PCI device that we're dealing with
  560. */
  561. int
  562. pci_save_state(struct pci_dev *dev)
  563. {
  564. int i;
  565. /* XXX: 100% dword access ok here? */
  566. for (i = 0; i < 16; i++)
  567. pci_read_config_dword(dev, i * 4,&dev->saved_config_space[i]);
  568. if ((i = pci_save_pcie_state(dev)) != 0)
  569. return i;
  570. if ((i = pci_save_pcix_state(dev)) != 0)
  571. return i;
  572. return 0;
  573. }
  574. /**
  575. * pci_restore_state - Restore the saved state of a PCI device
  576. * @dev: - PCI device that we're dealing with
  577. */
  578. int
  579. pci_restore_state(struct pci_dev *dev)
  580. {
  581. int i;
  582. u32 val;
  583. /* PCI Express register must be restored first */
  584. pci_restore_pcie_state(dev);
  585. /*
  586. * The Base Address register should be programmed before the command
  587. * register(s)
  588. */
  589. for (i = 15; i >= 0; i--) {
  590. pci_read_config_dword(dev, i * 4, &val);
  591. if (val != dev->saved_config_space[i]) {
  592. printk(KERN_DEBUG "PM: Writing back config space on "
  593. "device %s at offset %x (was %x, writing %x)\n",
  594. pci_name(dev), i,
  595. val, (int)dev->saved_config_space[i]);
  596. pci_write_config_dword(dev,i * 4,
  597. dev->saved_config_space[i]);
  598. }
  599. }
  600. pci_restore_pcix_state(dev);
  601. pci_restore_msi_state(dev);
  602. return 0;
  603. }
  604. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  605. {
  606. int err;
  607. err = pci_set_power_state(dev, PCI_D0);
  608. if (err < 0 && err != -EIO)
  609. return err;
  610. err = pcibios_enable_device(dev, bars);
  611. if (err < 0)
  612. return err;
  613. pci_fixup_device(pci_fixup_enable, dev);
  614. return 0;
  615. }
  616. /**
  617. * pci_reenable_device - Resume abandoned device
  618. * @dev: PCI device to be resumed
  619. *
  620. * Note this function is a backend of pci_default_resume and is not supposed
  621. * to be called by normal code, write proper resume handler and use it instead.
  622. */
  623. int pci_reenable_device(struct pci_dev *dev)
  624. {
  625. if (atomic_read(&dev->enable_cnt))
  626. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  627. return 0;
  628. }
  629. static int __pci_enable_device_flags(struct pci_dev *dev,
  630. resource_size_t flags)
  631. {
  632. int err;
  633. int i, bars = 0;
  634. if (atomic_add_return(1, &dev->enable_cnt) > 1)
  635. return 0; /* already enabled */
  636. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  637. if (dev->resource[i].flags & flags)
  638. bars |= (1 << i);
  639. err = do_pci_enable_device(dev, bars);
  640. if (err < 0)
  641. atomic_dec(&dev->enable_cnt);
  642. return err;
  643. }
  644. /**
  645. * pci_enable_device_io - Initialize a device for use with IO space
  646. * @dev: PCI device to be initialized
  647. *
  648. * Initialize device before it's used by a driver. Ask low-level code
  649. * to enable I/O resources. Wake up the device if it was suspended.
  650. * Beware, this function can fail.
  651. */
  652. int pci_enable_device_io(struct pci_dev *dev)
  653. {
  654. return __pci_enable_device_flags(dev, IORESOURCE_IO);
  655. }
  656. /**
  657. * pci_enable_device_mem - Initialize a device for use with Memory space
  658. * @dev: PCI device to be initialized
  659. *
  660. * Initialize device before it's used by a driver. Ask low-level code
  661. * to enable Memory resources. Wake up the device if it was suspended.
  662. * Beware, this function can fail.
  663. */
  664. int pci_enable_device_mem(struct pci_dev *dev)
  665. {
  666. return __pci_enable_device_flags(dev, IORESOURCE_MEM);
  667. }
  668. /**
  669. * pci_enable_device - Initialize device before it's used by a driver.
  670. * @dev: PCI device to be initialized
  671. *
  672. * Initialize device before it's used by a driver. Ask low-level code
  673. * to enable I/O and memory. Wake up the device if it was suspended.
  674. * Beware, this function can fail.
  675. *
  676. * Note we don't actually enable the device many times if we call
  677. * this function repeatedly (we just increment the count).
  678. */
  679. int pci_enable_device(struct pci_dev *dev)
  680. {
  681. return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  682. }
  683. /*
  684. * Managed PCI resources. This manages device on/off, intx/msi/msix
  685. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  686. * there's no need to track it separately. pci_devres is initialized
  687. * when a device is enabled using managed PCI device enable interface.
  688. */
  689. struct pci_devres {
  690. unsigned int enabled:1;
  691. unsigned int pinned:1;
  692. unsigned int orig_intx:1;
  693. unsigned int restore_intx:1;
  694. u32 region_mask;
  695. };
  696. static void pcim_release(struct device *gendev, void *res)
  697. {
  698. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  699. struct pci_devres *this = res;
  700. int i;
  701. if (dev->msi_enabled)
  702. pci_disable_msi(dev);
  703. if (dev->msix_enabled)
  704. pci_disable_msix(dev);
  705. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  706. if (this->region_mask & (1 << i))
  707. pci_release_region(dev, i);
  708. if (this->restore_intx)
  709. pci_intx(dev, this->orig_intx);
  710. if (this->enabled && !this->pinned)
  711. pci_disable_device(dev);
  712. }
  713. static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
  714. {
  715. struct pci_devres *dr, *new_dr;
  716. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  717. if (dr)
  718. return dr;
  719. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  720. if (!new_dr)
  721. return NULL;
  722. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  723. }
  724. static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
  725. {
  726. if (pci_is_managed(pdev))
  727. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  728. return NULL;
  729. }
  730. /**
  731. * pcim_enable_device - Managed pci_enable_device()
  732. * @pdev: PCI device to be initialized
  733. *
  734. * Managed pci_enable_device().
  735. */
  736. int pcim_enable_device(struct pci_dev *pdev)
  737. {
  738. struct pci_devres *dr;
  739. int rc;
  740. dr = get_pci_dr(pdev);
  741. if (unlikely(!dr))
  742. return -ENOMEM;
  743. if (dr->enabled)
  744. return 0;
  745. rc = pci_enable_device(pdev);
  746. if (!rc) {
  747. pdev->is_managed = 1;
  748. dr->enabled = 1;
  749. }
  750. return rc;
  751. }
  752. /**
  753. * pcim_pin_device - Pin managed PCI device
  754. * @pdev: PCI device to pin
  755. *
  756. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  757. * driver detach. @pdev must have been enabled with
  758. * pcim_enable_device().
  759. */
  760. void pcim_pin_device(struct pci_dev *pdev)
  761. {
  762. struct pci_devres *dr;
  763. dr = find_pci_dr(pdev);
  764. WARN_ON(!dr || !dr->enabled);
  765. if (dr)
  766. dr->pinned = 1;
  767. }
  768. /**
  769. * pcibios_disable_device - disable arch specific PCI resources for device dev
  770. * @dev: the PCI device to disable
  771. *
  772. * Disables architecture specific PCI resources for the device. This
  773. * is the default implementation. Architecture implementations can
  774. * override this.
  775. */
  776. void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
  777. /**
  778. * pci_disable_device - Disable PCI device after use
  779. * @dev: PCI device to be disabled
  780. *
  781. * Signal to the system that the PCI device is not in use by the system
  782. * anymore. This only involves disabling PCI bus-mastering, if active.
  783. *
  784. * Note we don't actually disable the device until all callers of
  785. * pci_device_enable() have called pci_device_disable().
  786. */
  787. void
  788. pci_disable_device(struct pci_dev *dev)
  789. {
  790. struct pci_devres *dr;
  791. u16 pci_command;
  792. dr = find_pci_dr(dev);
  793. if (dr)
  794. dr->enabled = 0;
  795. if (atomic_sub_return(1, &dev->enable_cnt) != 0)
  796. return;
  797. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  798. if (pci_command & PCI_COMMAND_MASTER) {
  799. pci_command &= ~PCI_COMMAND_MASTER;
  800. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  801. }
  802. dev->is_busmaster = 0;
  803. pcibios_disable_device(dev);
  804. }
  805. /**
  806. * pcibios_set_pcie_reset_state - set reset state for device dev
  807. * @dev: the PCI-E device reset
  808. * @state: Reset state to enter into
  809. *
  810. *
  811. * Sets the PCI-E reset state for the device. This is the default
  812. * implementation. Architecture implementations can override this.
  813. */
  814. int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev,
  815. enum pcie_reset_state state)
  816. {
  817. return -EINVAL;
  818. }
  819. /**
  820. * pci_set_pcie_reset_state - set reset state for device dev
  821. * @dev: the PCI-E device reset
  822. * @state: Reset state to enter into
  823. *
  824. *
  825. * Sets the PCI reset state for the device.
  826. */
  827. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  828. {
  829. return pcibios_set_pcie_reset_state(dev, state);
  830. }
  831. /**
  832. * pci_enable_wake - enable PCI device as wakeup event source
  833. * @dev: PCI device affected
  834. * @state: PCI state from which device will issue wakeup events
  835. * @enable: True to enable event generation; false to disable
  836. *
  837. * This enables the device as a wakeup event source, or disables it.
  838. * When such events involves platform-specific hooks, those hooks are
  839. * called automatically by this routine.
  840. *
  841. * Devices with legacy power management (no standard PCI PM capabilities)
  842. * always require such platform hooks. Depending on the platform, devices
  843. * supporting the standard PCI PME# signal may require such platform hooks;
  844. * they always update bits in config space to allow PME# generation.
  845. *
  846. * -EIO is returned if the device can't ever be a wakeup event source.
  847. * -EINVAL is returned if the device can't generate wakeup events from
  848. * the specified PCI state. Returns zero if the operation is successful.
  849. */
  850. int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
  851. {
  852. int pm;
  853. int status;
  854. u16 value;
  855. /* Note that drivers should verify device_may_wakeup(&dev->dev)
  856. * before calling this function. Platform code should report
  857. * errors when drivers try to enable wakeup on devices that
  858. * can't issue wakeups, or on which wakeups were disabled by
  859. * userspace updating the /sys/devices.../power/wakeup file.
  860. */
  861. status = call_platform_enable_wakeup(&dev->dev, enable);
  862. /* find PCI PM capability in list */
  863. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  864. /* If device doesn't support PM Capabilities, but caller wants to
  865. * disable wake events, it's a NOP. Otherwise fail unless the
  866. * platform hooks handled this legacy device already.
  867. */
  868. if (!pm)
  869. return enable ? status : 0;
  870. /* Check device's ability to generate PME# */
  871. pci_read_config_word(dev,pm+PCI_PM_PMC,&value);
  872. value &= PCI_PM_CAP_PME_MASK;
  873. value >>= ffs(PCI_PM_CAP_PME_MASK) - 1; /* First bit of mask */
  874. /* Check if it can generate PME# from requested state. */
  875. if (!value || !(value & (1 << state))) {
  876. /* if it can't, revert what the platform hook changed,
  877. * always reporting the base "EINVAL, can't PME#" error
  878. */
  879. if (enable)
  880. call_platform_enable_wakeup(&dev->dev, 0);
  881. return enable ? -EINVAL : 0;
  882. }
  883. pci_read_config_word(dev, pm + PCI_PM_CTRL, &value);
  884. /* Clear PME_Status by writing 1 to it and enable PME# */
  885. value |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  886. if (!enable)
  887. value &= ~PCI_PM_CTRL_PME_ENABLE;
  888. pci_write_config_word(dev, pm + PCI_PM_CTRL, value);
  889. return 0;
  890. }
  891. int
  892. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  893. {
  894. u8 pin;
  895. pin = dev->pin;
  896. if (!pin)
  897. return -1;
  898. pin--;
  899. while (dev->bus->self) {
  900. pin = (pin + PCI_SLOT(dev->devfn)) % 4;
  901. dev = dev->bus->self;
  902. }
  903. *bridge = dev;
  904. return pin;
  905. }
  906. /**
  907. * pci_release_region - Release a PCI bar
  908. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  909. * @bar: BAR to release
  910. *
  911. * Releases the PCI I/O and memory resources previously reserved by a
  912. * successful call to pci_request_region. Call this function only
  913. * after all use of the PCI regions has ceased.
  914. */
  915. void pci_release_region(struct pci_dev *pdev, int bar)
  916. {
  917. struct pci_devres *dr;
  918. if (pci_resource_len(pdev, bar) == 0)
  919. return;
  920. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  921. release_region(pci_resource_start(pdev, bar),
  922. pci_resource_len(pdev, bar));
  923. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  924. release_mem_region(pci_resource_start(pdev, bar),
  925. pci_resource_len(pdev, bar));
  926. dr = find_pci_dr(pdev);
  927. if (dr)
  928. dr->region_mask &= ~(1 << bar);
  929. }
  930. /**
  931. * pci_request_region - Reserved PCI I/O and memory resource
  932. * @pdev: PCI device whose resources are to be reserved
  933. * @bar: BAR to be reserved
  934. * @res_name: Name to be associated with resource.
  935. *
  936. * Mark the PCI region associated with PCI device @pdev BR @bar as
  937. * being reserved by owner @res_name. Do not access any
  938. * address inside the PCI regions unless this call returns
  939. * successfully.
  940. *
  941. * Returns 0 on success, or %EBUSY on error. A warning
  942. * message is also printed on failure.
  943. */
  944. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  945. {
  946. struct pci_devres *dr;
  947. if (pci_resource_len(pdev, bar) == 0)
  948. return 0;
  949. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  950. if (!request_region(pci_resource_start(pdev, bar),
  951. pci_resource_len(pdev, bar), res_name))
  952. goto err_out;
  953. }
  954. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  955. if (!request_mem_region(pci_resource_start(pdev, bar),
  956. pci_resource_len(pdev, bar), res_name))
  957. goto err_out;
  958. }
  959. dr = find_pci_dr(pdev);
  960. if (dr)
  961. dr->region_mask |= 1 << bar;
  962. return 0;
  963. err_out:
  964. printk (KERN_WARNING "PCI: Unable to reserve %s region #%d:%llx@%llx "
  965. "for device %s\n",
  966. pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
  967. bar + 1, /* PCI BAR # */
  968. (unsigned long long)pci_resource_len(pdev, bar),
  969. (unsigned long long)pci_resource_start(pdev, bar),
  970. pci_name(pdev));
  971. return -EBUSY;
  972. }
  973. /**
  974. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  975. * @pdev: PCI device whose resources were previously reserved
  976. * @bars: Bitmask of BARs to be released
  977. *
  978. * Release selected PCI I/O and memory resources previously reserved.
  979. * Call this function only after all use of the PCI regions has ceased.
  980. */
  981. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  982. {
  983. int i;
  984. for (i = 0; i < 6; i++)
  985. if (bars & (1 << i))
  986. pci_release_region(pdev, i);
  987. }
  988. /**
  989. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  990. * @pdev: PCI device whose resources are to be reserved
  991. * @bars: Bitmask of BARs to be requested
  992. * @res_name: Name to be associated with resource
  993. */
  994. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  995. const char *res_name)
  996. {
  997. int i;
  998. for (i = 0; i < 6; i++)
  999. if (bars & (1 << i))
  1000. if(pci_request_region(pdev, i, res_name))
  1001. goto err_out;
  1002. return 0;
  1003. err_out:
  1004. while(--i >= 0)
  1005. if (bars & (1 << i))
  1006. pci_release_region(pdev, i);
  1007. return -EBUSY;
  1008. }
  1009. /**
  1010. * pci_release_regions - Release reserved PCI I/O and memory resources
  1011. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  1012. *
  1013. * Releases all PCI I/O and memory resources previously reserved by a
  1014. * successful call to pci_request_regions. Call this function only
  1015. * after all use of the PCI regions has ceased.
  1016. */
  1017. void pci_release_regions(struct pci_dev *pdev)
  1018. {
  1019. pci_release_selected_regions(pdev, (1 << 6) - 1);
  1020. }
  1021. /**
  1022. * pci_request_regions - Reserved PCI I/O and memory resources
  1023. * @pdev: PCI device whose resources are to be reserved
  1024. * @res_name: Name to be associated with resource.
  1025. *
  1026. * Mark all PCI regions associated with PCI device @pdev as
  1027. * being reserved by owner @res_name. Do not access any
  1028. * address inside the PCI regions unless this call returns
  1029. * successfully.
  1030. *
  1031. * Returns 0 on success, or %EBUSY on error. A warning
  1032. * message is also printed on failure.
  1033. */
  1034. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  1035. {
  1036. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  1037. }
  1038. /**
  1039. * pci_set_master - enables bus-mastering for device dev
  1040. * @dev: the PCI device to enable
  1041. *
  1042. * Enables bus-mastering on the device and calls pcibios_set_master()
  1043. * to do the needed arch specific settings.
  1044. */
  1045. void
  1046. pci_set_master(struct pci_dev *dev)
  1047. {
  1048. u16 cmd;
  1049. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1050. if (! (cmd & PCI_COMMAND_MASTER)) {
  1051. pr_debug("PCI: Enabling bus mastering for device %s\n", pci_name(dev));
  1052. cmd |= PCI_COMMAND_MASTER;
  1053. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1054. }
  1055. dev->is_busmaster = 1;
  1056. pcibios_set_master(dev);
  1057. }
  1058. #ifdef PCI_DISABLE_MWI
  1059. int pci_set_mwi(struct pci_dev *dev)
  1060. {
  1061. return 0;
  1062. }
  1063. int pci_try_set_mwi(struct pci_dev *dev)
  1064. {
  1065. return 0;
  1066. }
  1067. void pci_clear_mwi(struct pci_dev *dev)
  1068. {
  1069. }
  1070. #else
  1071. #ifndef PCI_CACHE_LINE_BYTES
  1072. #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES
  1073. #endif
  1074. /* This can be overridden by arch code. */
  1075. /* Don't forget this is measured in 32-bit words, not bytes */
  1076. u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4;
  1077. /**
  1078. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  1079. * @dev: the PCI device for which MWI is to be enabled
  1080. *
  1081. * Helper function for pci_set_mwi.
  1082. * Originally copied from drivers/net/acenic.c.
  1083. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  1084. *
  1085. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1086. */
  1087. static int
  1088. pci_set_cacheline_size(struct pci_dev *dev)
  1089. {
  1090. u8 cacheline_size;
  1091. if (!pci_cache_line_size)
  1092. return -EINVAL; /* The system doesn't support MWI. */
  1093. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  1094. equal to or multiple of the right value. */
  1095. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1096. if (cacheline_size >= pci_cache_line_size &&
  1097. (cacheline_size % pci_cache_line_size) == 0)
  1098. return 0;
  1099. /* Write the correct value. */
  1100. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  1101. /* Read it back. */
  1102. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1103. if (cacheline_size == pci_cache_line_size)
  1104. return 0;
  1105. printk(KERN_DEBUG "PCI: cache line size of %d is not supported "
  1106. "by device %s\n", pci_cache_line_size << 2, pci_name(dev));
  1107. return -EINVAL;
  1108. }
  1109. /**
  1110. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  1111. * @dev: the PCI device for which MWI is enabled
  1112. *
  1113. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1114. *
  1115. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1116. */
  1117. int
  1118. pci_set_mwi(struct pci_dev *dev)
  1119. {
  1120. int rc;
  1121. u16 cmd;
  1122. rc = pci_set_cacheline_size(dev);
  1123. if (rc)
  1124. return rc;
  1125. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1126. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  1127. pr_debug("PCI: Enabling Mem-Wr-Inval for device %s\n",
  1128. pci_name(dev));
  1129. cmd |= PCI_COMMAND_INVALIDATE;
  1130. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1131. }
  1132. return 0;
  1133. }
  1134. /**
  1135. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  1136. * @dev: the PCI device for which MWI is enabled
  1137. *
  1138. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1139. * Callers are not required to check the return value.
  1140. *
  1141. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1142. */
  1143. int pci_try_set_mwi(struct pci_dev *dev)
  1144. {
  1145. int rc = pci_set_mwi(dev);
  1146. return rc;
  1147. }
  1148. /**
  1149. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  1150. * @dev: the PCI device to disable
  1151. *
  1152. * Disables PCI Memory-Write-Invalidate transaction on the device
  1153. */
  1154. void
  1155. pci_clear_mwi(struct pci_dev *dev)
  1156. {
  1157. u16 cmd;
  1158. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1159. if (cmd & PCI_COMMAND_INVALIDATE) {
  1160. cmd &= ~PCI_COMMAND_INVALIDATE;
  1161. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1162. }
  1163. }
  1164. #endif /* ! PCI_DISABLE_MWI */
  1165. /**
  1166. * pci_intx - enables/disables PCI INTx for device dev
  1167. * @pdev: the PCI device to operate on
  1168. * @enable: boolean: whether to enable or disable PCI INTx
  1169. *
  1170. * Enables/disables PCI INTx for device dev
  1171. */
  1172. void
  1173. pci_intx(struct pci_dev *pdev, int enable)
  1174. {
  1175. u16 pci_command, new;
  1176. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  1177. if (enable) {
  1178. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  1179. } else {
  1180. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  1181. }
  1182. if (new != pci_command) {
  1183. struct pci_devres *dr;
  1184. pci_write_config_word(pdev, PCI_COMMAND, new);
  1185. dr = find_pci_dr(pdev);
  1186. if (dr && !dr->restore_intx) {
  1187. dr->restore_intx = 1;
  1188. dr->orig_intx = !enable;
  1189. }
  1190. }
  1191. }
  1192. /**
  1193. * pci_msi_off - disables any msi or msix capabilities
  1194. * @dev: the PCI device to operate on
  1195. *
  1196. * If you want to use msi see pci_enable_msi and friends.
  1197. * This is a lower level primitive that allows us to disable
  1198. * msi operation at the device level.
  1199. */
  1200. void pci_msi_off(struct pci_dev *dev)
  1201. {
  1202. int pos;
  1203. u16 control;
  1204. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1205. if (pos) {
  1206. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  1207. control &= ~PCI_MSI_FLAGS_ENABLE;
  1208. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  1209. }
  1210. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1211. if (pos) {
  1212. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  1213. control &= ~PCI_MSIX_FLAGS_ENABLE;
  1214. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  1215. }
  1216. }
  1217. #ifndef HAVE_ARCH_PCI_SET_DMA_MASK
  1218. /*
  1219. * These can be overridden by arch-specific implementations
  1220. */
  1221. int
  1222. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  1223. {
  1224. if (!pci_dma_supported(dev, mask))
  1225. return -EIO;
  1226. dev->dma_mask = mask;
  1227. return 0;
  1228. }
  1229. int
  1230. pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  1231. {
  1232. if (!pci_dma_supported(dev, mask))
  1233. return -EIO;
  1234. dev->dev.coherent_dma_mask = mask;
  1235. return 0;
  1236. }
  1237. #endif
  1238. #ifndef HAVE_ARCH_PCI_SET_DMA_MAX_SEGMENT_SIZE
  1239. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  1240. {
  1241. return dma_set_max_seg_size(&dev->dev, size);
  1242. }
  1243. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  1244. #endif
  1245. #ifndef HAVE_ARCH_PCI_SET_DMA_SEGMENT_BOUNDARY
  1246. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  1247. {
  1248. return dma_set_seg_boundary(&dev->dev, mask);
  1249. }
  1250. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  1251. #endif
  1252. /**
  1253. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  1254. * @dev: PCI device to query
  1255. *
  1256. * Returns mmrbc: maximum designed memory read count in bytes
  1257. * or appropriate error value.
  1258. */
  1259. int pcix_get_max_mmrbc(struct pci_dev *dev)
  1260. {
  1261. int err, cap;
  1262. u32 stat;
  1263. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1264. if (!cap)
  1265. return -EINVAL;
  1266. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1267. if (err)
  1268. return -EINVAL;
  1269. return (stat & PCI_X_STATUS_MAX_READ) >> 12;
  1270. }
  1271. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  1272. /**
  1273. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  1274. * @dev: PCI device to query
  1275. *
  1276. * Returns mmrbc: maximum memory read count in bytes
  1277. * or appropriate error value.
  1278. */
  1279. int pcix_get_mmrbc(struct pci_dev *dev)
  1280. {
  1281. int ret, cap;
  1282. u32 cmd;
  1283. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1284. if (!cap)
  1285. return -EINVAL;
  1286. ret = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1287. if (!ret)
  1288. ret = 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  1289. return ret;
  1290. }
  1291. EXPORT_SYMBOL(pcix_get_mmrbc);
  1292. /**
  1293. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  1294. * @dev: PCI device to query
  1295. * @mmrbc: maximum memory read count in bytes
  1296. * valid values are 512, 1024, 2048, 4096
  1297. *
  1298. * If possible sets maximum memory read byte count, some bridges have erratas
  1299. * that prevent this.
  1300. */
  1301. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  1302. {
  1303. int cap, err = -EINVAL;
  1304. u32 stat, cmd, v, o;
  1305. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  1306. goto out;
  1307. v = ffs(mmrbc) - 10;
  1308. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1309. if (!cap)
  1310. goto out;
  1311. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1312. if (err)
  1313. goto out;
  1314. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  1315. return -E2BIG;
  1316. err = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1317. if (err)
  1318. goto out;
  1319. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  1320. if (o != v) {
  1321. if (v > o && dev->bus &&
  1322. (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  1323. return -EIO;
  1324. cmd &= ~PCI_X_CMD_MAX_READ;
  1325. cmd |= v << 2;
  1326. err = pci_write_config_dword(dev, cap + PCI_X_CMD, cmd);
  1327. }
  1328. out:
  1329. return err;
  1330. }
  1331. EXPORT_SYMBOL(pcix_set_mmrbc);
  1332. /**
  1333. * pcie_get_readrq - get PCI Express read request size
  1334. * @dev: PCI device to query
  1335. *
  1336. * Returns maximum memory read request in bytes
  1337. * or appropriate error value.
  1338. */
  1339. int pcie_get_readrq(struct pci_dev *dev)
  1340. {
  1341. int ret, cap;
  1342. u16 ctl;
  1343. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1344. if (!cap)
  1345. return -EINVAL;
  1346. ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1347. if (!ret)
  1348. ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  1349. return ret;
  1350. }
  1351. EXPORT_SYMBOL(pcie_get_readrq);
  1352. /**
  1353. * pcie_set_readrq - set PCI Express maximum memory read request
  1354. * @dev: PCI device to query
  1355. * @rq: maximum memory read count in bytes
  1356. * valid values are 128, 256, 512, 1024, 2048, 4096
  1357. *
  1358. * If possible sets maximum read byte count
  1359. */
  1360. int pcie_set_readrq(struct pci_dev *dev, int rq)
  1361. {
  1362. int cap, err = -EINVAL;
  1363. u16 ctl, v;
  1364. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  1365. goto out;
  1366. v = (ffs(rq) - 8) << 12;
  1367. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1368. if (!cap)
  1369. goto out;
  1370. err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1371. if (err)
  1372. goto out;
  1373. if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
  1374. ctl &= ~PCI_EXP_DEVCTL_READRQ;
  1375. ctl |= v;
  1376. err = pci_write_config_dword(dev, cap + PCI_EXP_DEVCTL, ctl);
  1377. }
  1378. out:
  1379. return err;
  1380. }
  1381. EXPORT_SYMBOL(pcie_set_readrq);
  1382. /**
  1383. * pci_select_bars - Make BAR mask from the type of resource
  1384. * @dev: the PCI device for which BAR mask is made
  1385. * @flags: resource type mask to be selected
  1386. *
  1387. * This helper routine makes bar mask from the type of resource.
  1388. */
  1389. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  1390. {
  1391. int i, bars = 0;
  1392. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  1393. if (pci_resource_flags(dev, i) & flags)
  1394. bars |= (1 << i);
  1395. return bars;
  1396. }
  1397. static void __devinit pci_no_domains(void)
  1398. {
  1399. #ifdef CONFIG_PCI_DOMAINS
  1400. pci_domains_supported = 0;
  1401. #endif
  1402. }
  1403. static int __devinit pci_init(void)
  1404. {
  1405. struct pci_dev *dev = NULL;
  1406. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1407. pci_fixup_device(pci_fixup_final, dev);
  1408. }
  1409. return 0;
  1410. }
  1411. static int __devinit pci_setup(char *str)
  1412. {
  1413. while (str) {
  1414. char *k = strchr(str, ',');
  1415. if (k)
  1416. *k++ = 0;
  1417. if (*str && (str = pcibios_setup(str)) && *str) {
  1418. if (!strcmp(str, "nomsi")) {
  1419. pci_no_msi();
  1420. } else if (!strcmp(str, "noaer")) {
  1421. pci_no_aer();
  1422. } else if (!strcmp(str, "nodomains")) {
  1423. pci_no_domains();
  1424. } else if (!strncmp(str, "cbiosize=", 9)) {
  1425. pci_cardbus_io_size = memparse(str + 9, &str);
  1426. } else if (!strncmp(str, "cbmemsize=", 10)) {
  1427. pci_cardbus_mem_size = memparse(str + 10, &str);
  1428. } else {
  1429. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  1430. str);
  1431. }
  1432. }
  1433. str = k;
  1434. }
  1435. return 0;
  1436. }
  1437. early_param("pci", pci_setup);
  1438. device_initcall(pci_init);
  1439. EXPORT_SYMBOL(pci_reenable_device);
  1440. EXPORT_SYMBOL(pci_enable_device_io);
  1441. EXPORT_SYMBOL(pci_enable_device_mem);
  1442. EXPORT_SYMBOL(pci_enable_device);
  1443. EXPORT_SYMBOL(pcim_enable_device);
  1444. EXPORT_SYMBOL(pcim_pin_device);
  1445. EXPORT_SYMBOL(pci_disable_device);
  1446. EXPORT_SYMBOL(pci_find_capability);
  1447. EXPORT_SYMBOL(pci_bus_find_capability);
  1448. EXPORT_SYMBOL(pci_release_regions);
  1449. EXPORT_SYMBOL(pci_request_regions);
  1450. EXPORT_SYMBOL(pci_release_region);
  1451. EXPORT_SYMBOL(pci_request_region);
  1452. EXPORT_SYMBOL(pci_release_selected_regions);
  1453. EXPORT_SYMBOL(pci_request_selected_regions);
  1454. EXPORT_SYMBOL(pci_set_master);
  1455. EXPORT_SYMBOL(pci_set_mwi);
  1456. EXPORT_SYMBOL(pci_try_set_mwi);
  1457. EXPORT_SYMBOL(pci_clear_mwi);
  1458. EXPORT_SYMBOL_GPL(pci_intx);
  1459. EXPORT_SYMBOL(pci_set_dma_mask);
  1460. EXPORT_SYMBOL(pci_set_consistent_dma_mask);
  1461. EXPORT_SYMBOL(pci_assign_resource);
  1462. EXPORT_SYMBOL(pci_find_parent_resource);
  1463. EXPORT_SYMBOL(pci_select_bars);
  1464. EXPORT_SYMBOL(pci_set_power_state);
  1465. EXPORT_SYMBOL(pci_save_state);
  1466. EXPORT_SYMBOL(pci_restore_state);
  1467. EXPORT_SYMBOL(pci_enable_wake);
  1468. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);