pci.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. /*
  2. * $Id: pci.c,v 1.91 1999/01/21 13:34:01 davem Exp $
  3. *
  4. * PCI Bus Services, see include/linux/pci.h for further explanation.
  5. *
  6. * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  7. * David Mosberger-Tang
  8. *
  9. * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/pci.h>
  15. #include <linux/module.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/string.h>
  18. #include <asm/dma.h> /* isa_dma_bridge_buggy */
  19. #include "pci.h"
  20. unsigned int pci_pm_d3_delay = 10;
  21. /**
  22. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  23. * @bus: pointer to PCI bus structure to search
  24. *
  25. * Given a PCI bus, returns the highest PCI bus number present in the set
  26. * including the given PCI bus and its list of child PCI buses.
  27. */
  28. unsigned char __devinit
  29. pci_bus_max_busnr(struct pci_bus* bus)
  30. {
  31. struct list_head *tmp;
  32. unsigned char max, n;
  33. max = bus->subordinate;
  34. list_for_each(tmp, &bus->children) {
  35. n = pci_bus_max_busnr(pci_bus_b(tmp));
  36. if(n > max)
  37. max = n;
  38. }
  39. return max;
  40. }
  41. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  42. #if 0
  43. /**
  44. * pci_max_busnr - returns maximum PCI bus number
  45. *
  46. * Returns the highest PCI bus number present in the system global list of
  47. * PCI buses.
  48. */
  49. unsigned char __devinit
  50. pci_max_busnr(void)
  51. {
  52. struct pci_bus *bus = NULL;
  53. unsigned char max, n;
  54. max = 0;
  55. while ((bus = pci_find_next_bus(bus)) != NULL) {
  56. n = pci_bus_max_busnr(bus);
  57. if(n > max)
  58. max = n;
  59. }
  60. return max;
  61. }
  62. #endif /* 0 */
  63. #define PCI_FIND_CAP_TTL 48
  64. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  65. u8 pos, int cap, int *ttl)
  66. {
  67. u8 id;
  68. while ((*ttl)--) {
  69. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  70. if (pos < 0x40)
  71. break;
  72. pos &= ~3;
  73. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  74. &id);
  75. if (id == 0xff)
  76. break;
  77. if (id == cap)
  78. return pos;
  79. pos += PCI_CAP_LIST_NEXT;
  80. }
  81. return 0;
  82. }
  83. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  84. u8 pos, int cap)
  85. {
  86. int ttl = PCI_FIND_CAP_TTL;
  87. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  88. }
  89. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  90. {
  91. return __pci_find_next_cap(dev->bus, dev->devfn,
  92. pos + PCI_CAP_LIST_NEXT, cap);
  93. }
  94. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  95. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  96. unsigned int devfn, u8 hdr_type)
  97. {
  98. u16 status;
  99. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  100. if (!(status & PCI_STATUS_CAP_LIST))
  101. return 0;
  102. switch (hdr_type) {
  103. case PCI_HEADER_TYPE_NORMAL:
  104. case PCI_HEADER_TYPE_BRIDGE:
  105. return PCI_CAPABILITY_LIST;
  106. case PCI_HEADER_TYPE_CARDBUS:
  107. return PCI_CB_CAPABILITY_LIST;
  108. default:
  109. return 0;
  110. }
  111. return 0;
  112. }
  113. /**
  114. * pci_find_capability - query for devices' capabilities
  115. * @dev: PCI device to query
  116. * @cap: capability code
  117. *
  118. * Tell if a device supports a given PCI capability.
  119. * Returns the address of the requested capability structure within the
  120. * device's PCI configuration space or 0 in case the device does not
  121. * support it. Possible values for @cap:
  122. *
  123. * %PCI_CAP_ID_PM Power Management
  124. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  125. * %PCI_CAP_ID_VPD Vital Product Data
  126. * %PCI_CAP_ID_SLOTID Slot Identification
  127. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  128. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  129. * %PCI_CAP_ID_PCIX PCI-X
  130. * %PCI_CAP_ID_EXP PCI Express
  131. */
  132. int pci_find_capability(struct pci_dev *dev, int cap)
  133. {
  134. int pos;
  135. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  136. if (pos)
  137. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  138. return pos;
  139. }
  140. /**
  141. * pci_bus_find_capability - query for devices' capabilities
  142. * @bus: the PCI bus to query
  143. * @devfn: PCI device to query
  144. * @cap: capability code
  145. *
  146. * Like pci_find_capability() but works for pci devices that do not have a
  147. * pci_dev structure set up yet.
  148. *
  149. * Returns the address of the requested capability structure within the
  150. * device's PCI configuration space or 0 in case the device does not
  151. * support it.
  152. */
  153. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  154. {
  155. int pos;
  156. u8 hdr_type;
  157. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  158. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  159. if (pos)
  160. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  161. return pos;
  162. }
  163. /**
  164. * pci_find_ext_capability - Find an extended capability
  165. * @dev: PCI device to query
  166. * @cap: capability code
  167. *
  168. * Returns the address of the requested extended capability structure
  169. * within the device's PCI configuration space or 0 if the device does
  170. * not support it. Possible values for @cap:
  171. *
  172. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  173. * %PCI_EXT_CAP_ID_VC Virtual Channel
  174. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  175. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  176. */
  177. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  178. {
  179. u32 header;
  180. int ttl = 480; /* 3840 bytes, minimum 8 bytes per capability */
  181. int pos = 0x100;
  182. if (dev->cfg_size <= 256)
  183. return 0;
  184. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  185. return 0;
  186. /*
  187. * If we have no capabilities, this is indicated by cap ID,
  188. * cap version and next pointer all being 0.
  189. */
  190. if (header == 0)
  191. return 0;
  192. while (ttl-- > 0) {
  193. if (PCI_EXT_CAP_ID(header) == cap)
  194. return pos;
  195. pos = PCI_EXT_CAP_NEXT(header);
  196. if (pos < 0x100)
  197. break;
  198. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  199. break;
  200. }
  201. return 0;
  202. }
  203. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  204. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  205. {
  206. int rc, ttl = PCI_FIND_CAP_TTL;
  207. u8 cap, mask;
  208. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  209. mask = HT_3BIT_CAP_MASK;
  210. else
  211. mask = HT_5BIT_CAP_MASK;
  212. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  213. PCI_CAP_ID_HT, &ttl);
  214. while (pos) {
  215. rc = pci_read_config_byte(dev, pos + 3, &cap);
  216. if (rc != PCIBIOS_SUCCESSFUL)
  217. return 0;
  218. if ((cap & mask) == ht_cap)
  219. return pos;
  220. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  221. PCI_CAP_ID_HT, &ttl);
  222. }
  223. return 0;
  224. }
  225. /**
  226. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  227. * @dev: PCI device to query
  228. * @pos: Position from which to continue searching
  229. * @ht_cap: Hypertransport capability code
  230. *
  231. * To be used in conjunction with pci_find_ht_capability() to search for
  232. * all capabilities matching @ht_cap. @pos should always be a value returned
  233. * from pci_find_ht_capability().
  234. *
  235. * NB. To be 100% safe against broken PCI devices, the caller should take
  236. * steps to avoid an infinite loop.
  237. */
  238. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  239. {
  240. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  241. }
  242. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  243. /**
  244. * pci_find_ht_capability - query a device's Hypertransport capabilities
  245. * @dev: PCI device to query
  246. * @ht_cap: Hypertransport capability code
  247. *
  248. * Tell if a device supports a given Hypertransport capability.
  249. * Returns an address within the device's PCI configuration space
  250. * or 0 in case the device does not support the request capability.
  251. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  252. * which has a Hypertransport capability matching @ht_cap.
  253. */
  254. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  255. {
  256. int pos;
  257. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  258. if (pos)
  259. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  260. return pos;
  261. }
  262. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  263. /**
  264. * pci_find_parent_resource - return resource region of parent bus of given region
  265. * @dev: PCI device structure contains resources to be searched
  266. * @res: child resource record for which parent is sought
  267. *
  268. * For given resource region of given device, return the resource
  269. * region of parent bus the given region is contained in or where
  270. * it should be allocated from.
  271. */
  272. struct resource *
  273. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  274. {
  275. const struct pci_bus *bus = dev->bus;
  276. int i;
  277. struct resource *best = NULL;
  278. for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  279. struct resource *r = bus->resource[i];
  280. if (!r)
  281. continue;
  282. if (res->start && !(res->start >= r->start && res->end <= r->end))
  283. continue; /* Not contained */
  284. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  285. continue; /* Wrong type */
  286. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  287. return r; /* Exact match */
  288. if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
  289. best = r; /* Approximating prefetchable by non-prefetchable */
  290. }
  291. return best;
  292. }
  293. /**
  294. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  295. * @dev: PCI device to have its BARs restored
  296. *
  297. * Restore the BAR values for a given device, so as to make it
  298. * accessible by its driver.
  299. */
  300. void
  301. pci_restore_bars(struct pci_dev *dev)
  302. {
  303. int i, numres;
  304. switch (dev->hdr_type) {
  305. case PCI_HEADER_TYPE_NORMAL:
  306. numres = 6;
  307. break;
  308. case PCI_HEADER_TYPE_BRIDGE:
  309. numres = 2;
  310. break;
  311. case PCI_HEADER_TYPE_CARDBUS:
  312. numres = 1;
  313. break;
  314. default:
  315. /* Should never get here, but just in case... */
  316. return;
  317. }
  318. for (i = 0; i < numres; i ++)
  319. pci_update_resource(dev, &dev->resource[i], i);
  320. }
  321. int (*platform_pci_set_power_state)(struct pci_dev *dev, pci_power_t t);
  322. /**
  323. * pci_set_power_state - Set the power state of a PCI device
  324. * @dev: PCI device to be suspended
  325. * @state: PCI power state (D0, D1, D2, D3hot, D3cold) we're entering
  326. *
  327. * Transition a device to a new power state, using the Power Management
  328. * Capabilities in the device's config space.
  329. *
  330. * RETURN VALUE:
  331. * -EINVAL if trying to enter a lower state than we're already in.
  332. * 0 if we're already in the requested state.
  333. * -EIO if device does not support PCI PM.
  334. * 0 if we can successfully change the power state.
  335. */
  336. int
  337. pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  338. {
  339. int pm, need_restore = 0;
  340. u16 pmcsr, pmc;
  341. /* bound the state we're entering */
  342. if (state > PCI_D3hot)
  343. state = PCI_D3hot;
  344. /* Validate current state:
  345. * Can enter D0 from any state, but if we can only go deeper
  346. * to sleep if we're already in a low power state
  347. */
  348. if (state != PCI_D0 && dev->current_state > state) {
  349. printk(KERN_ERR "%s(): %s: state=%d, current state=%d\n",
  350. __FUNCTION__, pci_name(dev), state, dev->current_state);
  351. return -EINVAL;
  352. } else if (dev->current_state == state)
  353. return 0; /* we're already there */
  354. /*
  355. * If the device or the parent bridge can't support PCI PM, ignore
  356. * the request if we're doing anything besides putting it into D0
  357. * (which would only happen on boot).
  358. */
  359. if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  360. return 0;
  361. /* find PCI PM capability in list */
  362. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  363. /* abort if the device doesn't support PM capabilities */
  364. if (!pm)
  365. return -EIO;
  366. pci_read_config_word(dev,pm + PCI_PM_PMC,&pmc);
  367. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  368. printk(KERN_DEBUG
  369. "PCI: %s has unsupported PM cap regs version (%u)\n",
  370. pci_name(dev), pmc & PCI_PM_CAP_VER_MASK);
  371. return -EIO;
  372. }
  373. /* check if this device supports the desired state */
  374. if (state == PCI_D1 && !(pmc & PCI_PM_CAP_D1))
  375. return -EIO;
  376. else if (state == PCI_D2 && !(pmc & PCI_PM_CAP_D2))
  377. return -EIO;
  378. pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
  379. /* If we're (effectively) in D3, force entire word to 0.
  380. * This doesn't affect PME_Status, disables PME_En, and
  381. * sets PowerState to 0.
  382. */
  383. switch (dev->current_state) {
  384. case PCI_D0:
  385. case PCI_D1:
  386. case PCI_D2:
  387. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  388. pmcsr |= state;
  389. break;
  390. case PCI_UNKNOWN: /* Boot-up */
  391. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  392. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  393. need_restore = 1;
  394. /* Fall-through: force to D0 */
  395. default:
  396. pmcsr = 0;
  397. break;
  398. }
  399. /* enter specified state */
  400. pci_write_config_word(dev, pm + PCI_PM_CTRL, pmcsr);
  401. /* Mandatory power management transition delays */
  402. /* see PCI PM 1.1 5.6.1 table 18 */
  403. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  404. msleep(pci_pm_d3_delay);
  405. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  406. udelay(200);
  407. /*
  408. * Give firmware a chance to be called, such as ACPI _PRx, _PSx
  409. * Firmware method after native method ?
  410. */
  411. if (platform_pci_set_power_state)
  412. platform_pci_set_power_state(dev, state);
  413. dev->current_state = state;
  414. /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  415. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  416. * from D3hot to D0 _may_ perform an internal reset, thereby
  417. * going to "D0 Uninitialized" rather than "D0 Initialized".
  418. * For example, at least some versions of the 3c905B and the
  419. * 3c556B exhibit this behaviour.
  420. *
  421. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  422. * devices in a D3hot state at boot. Consequently, we need to
  423. * restore at least the BARs so that the device will be
  424. * accessible to its driver.
  425. */
  426. if (need_restore)
  427. pci_restore_bars(dev);
  428. return 0;
  429. }
  430. int (*platform_pci_choose_state)(struct pci_dev *dev, pm_message_t state);
  431. /**
  432. * pci_choose_state - Choose the power state of a PCI device
  433. * @dev: PCI device to be suspended
  434. * @state: target sleep state for the whole system. This is the value
  435. * that is passed to suspend() function.
  436. *
  437. * Returns PCI power state suitable for given device and given system
  438. * message.
  439. */
  440. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  441. {
  442. int ret;
  443. if (!pci_find_capability(dev, PCI_CAP_ID_PM))
  444. return PCI_D0;
  445. if (platform_pci_choose_state) {
  446. ret = platform_pci_choose_state(dev, state);
  447. if (ret >= 0)
  448. state.event = ret;
  449. }
  450. switch (state.event) {
  451. case PM_EVENT_ON:
  452. return PCI_D0;
  453. case PM_EVENT_FREEZE:
  454. case PM_EVENT_PRETHAW:
  455. /* REVISIT both freeze and pre-thaw "should" use D0 */
  456. case PM_EVENT_SUSPEND:
  457. return PCI_D3hot;
  458. default:
  459. printk("Unrecognized suspend event %d\n", state.event);
  460. BUG();
  461. }
  462. return PCI_D0;
  463. }
  464. EXPORT_SYMBOL(pci_choose_state);
  465. static int pci_save_pcie_state(struct pci_dev *dev)
  466. {
  467. int pos, i = 0;
  468. struct pci_cap_saved_state *save_state;
  469. u16 *cap;
  470. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  471. if (pos <= 0)
  472. return 0;
  473. save_state = kzalloc(sizeof(*save_state) + sizeof(u16) * 4, GFP_KERNEL);
  474. if (!save_state) {
  475. dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n");
  476. return -ENOMEM;
  477. }
  478. cap = (u16 *)&save_state->data[0];
  479. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]);
  480. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
  481. pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
  482. pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
  483. pci_add_saved_cap(dev, save_state);
  484. return 0;
  485. }
  486. static void pci_restore_pcie_state(struct pci_dev *dev)
  487. {
  488. int i = 0, pos;
  489. struct pci_cap_saved_state *save_state;
  490. u16 *cap;
  491. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  492. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  493. if (!save_state || pos <= 0)
  494. return;
  495. cap = (u16 *)&save_state->data[0];
  496. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]);
  497. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]);
  498. pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]);
  499. pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);
  500. pci_remove_saved_cap(save_state);
  501. kfree(save_state);
  502. }
  503. static int pci_save_pcix_state(struct pci_dev *dev)
  504. {
  505. int pos, i = 0;
  506. struct pci_cap_saved_state *save_state;
  507. u16 *cap;
  508. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  509. if (pos <= 0)
  510. return 0;
  511. save_state = kzalloc(sizeof(*save_state) + sizeof(u16), GFP_KERNEL);
  512. if (!save_state) {
  513. dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n");
  514. return -ENOMEM;
  515. }
  516. cap = (u16 *)&save_state->data[0];
  517. pci_read_config_word(dev, pos + PCI_X_CMD, &cap[i++]);
  518. pci_add_saved_cap(dev, save_state);
  519. return 0;
  520. }
  521. static void pci_restore_pcix_state(struct pci_dev *dev)
  522. {
  523. int i = 0, pos;
  524. struct pci_cap_saved_state *save_state;
  525. u16 *cap;
  526. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  527. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  528. if (!save_state || pos <= 0)
  529. return;
  530. cap = (u16 *)&save_state->data[0];
  531. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  532. pci_remove_saved_cap(save_state);
  533. kfree(save_state);
  534. }
  535. /**
  536. * pci_save_state - save the PCI configuration space of a device before suspending
  537. * @dev: - PCI device that we're dealing with
  538. */
  539. int
  540. pci_save_state(struct pci_dev *dev)
  541. {
  542. int i;
  543. /* XXX: 100% dword access ok here? */
  544. for (i = 0; i < 16; i++)
  545. pci_read_config_dword(dev, i * 4,&dev->saved_config_space[i]);
  546. if ((i = pci_save_msi_state(dev)) != 0)
  547. return i;
  548. if ((i = pci_save_msix_state(dev)) != 0)
  549. return i;
  550. if ((i = pci_save_pcie_state(dev)) != 0)
  551. return i;
  552. if ((i = pci_save_pcix_state(dev)) != 0)
  553. return i;
  554. return 0;
  555. }
  556. /**
  557. * pci_restore_state - Restore the saved state of a PCI device
  558. * @dev: - PCI device that we're dealing with
  559. */
  560. int
  561. pci_restore_state(struct pci_dev *dev)
  562. {
  563. int i;
  564. int val;
  565. /* PCI Express register must be restored first */
  566. pci_restore_pcie_state(dev);
  567. /*
  568. * The Base Address register should be programmed before the command
  569. * register(s)
  570. */
  571. for (i = 15; i >= 0; i--) {
  572. pci_read_config_dword(dev, i * 4, &val);
  573. if (val != dev->saved_config_space[i]) {
  574. printk(KERN_DEBUG "PM: Writing back config space on "
  575. "device %s at offset %x (was %x, writing %x)\n",
  576. pci_name(dev), i,
  577. val, (int)dev->saved_config_space[i]);
  578. pci_write_config_dword(dev,i * 4,
  579. dev->saved_config_space[i]);
  580. }
  581. }
  582. pci_restore_pcix_state(dev);
  583. pci_restore_msi_state(dev);
  584. pci_restore_msix_state(dev);
  585. return 0;
  586. }
  587. /**
  588. * pci_enable_device_bars - Initialize some of a device for use
  589. * @dev: PCI device to be initialized
  590. * @bars: bitmask of BAR's that must be configured
  591. *
  592. * Initialize device before it's used by a driver. Ask low-level code
  593. * to enable selected I/O and memory resources. Wake up the device if it
  594. * was suspended. Beware, this function can fail.
  595. */
  596. int
  597. pci_enable_device_bars(struct pci_dev *dev, int bars)
  598. {
  599. int err;
  600. err = pci_set_power_state(dev, PCI_D0);
  601. if (err < 0 && err != -EIO)
  602. return err;
  603. err = pcibios_enable_device(dev, bars);
  604. if (err < 0)
  605. return err;
  606. return 0;
  607. }
  608. /**
  609. * __pci_enable_device - Initialize device before it's used by a driver.
  610. * @dev: PCI device to be initialized
  611. *
  612. * Initialize device before it's used by a driver. Ask low-level code
  613. * to enable I/O and memory. Wake up the device if it was suspended.
  614. * Beware, this function can fail.
  615. *
  616. * Note this function is a backend and is not supposed to be called by
  617. * normal code, use pci_enable_device() instead.
  618. */
  619. int
  620. __pci_enable_device(struct pci_dev *dev)
  621. {
  622. int err;
  623. err = pci_enable_device_bars(dev, (1 << PCI_NUM_RESOURCES) - 1);
  624. if (err)
  625. return err;
  626. pci_fixup_device(pci_fixup_enable, dev);
  627. return 0;
  628. }
  629. /**
  630. * pci_enable_device - Initialize device before it's used by a driver.
  631. * @dev: PCI device to be initialized
  632. *
  633. * Initialize device before it's used by a driver. Ask low-level code
  634. * to enable I/O and memory. Wake up the device if it was suspended.
  635. * Beware, this function can fail.
  636. *
  637. * Note we don't actually enable the device many times if we call
  638. * this function repeatedly (we just increment the count).
  639. */
  640. int pci_enable_device(struct pci_dev *dev)
  641. {
  642. int result;
  643. if (atomic_add_return(1, &dev->enable_cnt) > 1)
  644. return 0; /* already enabled */
  645. result = __pci_enable_device(dev);
  646. if (result < 0)
  647. atomic_dec(&dev->enable_cnt);
  648. return result;
  649. }
  650. /**
  651. * pcibios_disable_device - disable arch specific PCI resources for device dev
  652. * @dev: the PCI device to disable
  653. *
  654. * Disables architecture specific PCI resources for the device. This
  655. * is the default implementation. Architecture implementations can
  656. * override this.
  657. */
  658. void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
  659. /**
  660. * pci_disable_device - Disable PCI device after use
  661. * @dev: PCI device to be disabled
  662. *
  663. * Signal to the system that the PCI device is not in use by the system
  664. * anymore. This only involves disabling PCI bus-mastering, if active.
  665. *
  666. * Note we don't actually disable the device until all callers of
  667. * pci_device_enable() have called pci_device_disable().
  668. */
  669. void
  670. pci_disable_device(struct pci_dev *dev)
  671. {
  672. u16 pci_command;
  673. if (atomic_sub_return(1, &dev->enable_cnt) != 0)
  674. return;
  675. if (dev->msi_enabled)
  676. disable_msi_mode(dev, pci_find_capability(dev, PCI_CAP_ID_MSI),
  677. PCI_CAP_ID_MSI);
  678. if (dev->msix_enabled)
  679. disable_msi_mode(dev, pci_find_capability(dev, PCI_CAP_ID_MSI),
  680. PCI_CAP_ID_MSIX);
  681. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  682. if (pci_command & PCI_COMMAND_MASTER) {
  683. pci_command &= ~PCI_COMMAND_MASTER;
  684. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  685. }
  686. dev->is_busmaster = 0;
  687. pcibios_disable_device(dev);
  688. }
  689. /**
  690. * pci_enable_wake - enable device to generate PME# when suspended
  691. * @dev: - PCI device to operate on
  692. * @state: - Current state of device.
  693. * @enable: - Flag to enable or disable generation
  694. *
  695. * Set the bits in the device's PM Capabilities to generate PME# when
  696. * the system is suspended.
  697. *
  698. * -EIO is returned if device doesn't have PM Capabilities.
  699. * -EINVAL is returned if device supports it, but can't generate wake events.
  700. * 0 if operation is successful.
  701. *
  702. */
  703. int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
  704. {
  705. int pm;
  706. u16 value;
  707. /* find PCI PM capability in list */
  708. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  709. /* If device doesn't support PM Capabilities, but request is to disable
  710. * wake events, it's a nop; otherwise fail */
  711. if (!pm)
  712. return enable ? -EIO : 0;
  713. /* Check device's ability to generate PME# */
  714. pci_read_config_word(dev,pm+PCI_PM_PMC,&value);
  715. value &= PCI_PM_CAP_PME_MASK;
  716. value >>= ffs(PCI_PM_CAP_PME_MASK) - 1; /* First bit of mask */
  717. /* Check if it can generate PME# from requested state. */
  718. if (!value || !(value & (1 << state)))
  719. return enable ? -EINVAL : 0;
  720. pci_read_config_word(dev, pm + PCI_PM_CTRL, &value);
  721. /* Clear PME_Status by writing 1 to it and enable PME# */
  722. value |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  723. if (!enable)
  724. value &= ~PCI_PM_CTRL_PME_ENABLE;
  725. pci_write_config_word(dev, pm + PCI_PM_CTRL, value);
  726. return 0;
  727. }
  728. int
  729. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  730. {
  731. u8 pin;
  732. pin = dev->pin;
  733. if (!pin)
  734. return -1;
  735. pin--;
  736. while (dev->bus->self) {
  737. pin = (pin + PCI_SLOT(dev->devfn)) % 4;
  738. dev = dev->bus->self;
  739. }
  740. *bridge = dev;
  741. return pin;
  742. }
  743. /**
  744. * pci_release_region - Release a PCI bar
  745. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  746. * @bar: BAR to release
  747. *
  748. * Releases the PCI I/O and memory resources previously reserved by a
  749. * successful call to pci_request_region. Call this function only
  750. * after all use of the PCI regions has ceased.
  751. */
  752. void pci_release_region(struct pci_dev *pdev, int bar)
  753. {
  754. if (pci_resource_len(pdev, bar) == 0)
  755. return;
  756. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  757. release_region(pci_resource_start(pdev, bar),
  758. pci_resource_len(pdev, bar));
  759. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  760. release_mem_region(pci_resource_start(pdev, bar),
  761. pci_resource_len(pdev, bar));
  762. }
  763. /**
  764. * pci_request_region - Reserved PCI I/O and memory resource
  765. * @pdev: PCI device whose resources are to be reserved
  766. * @bar: BAR to be reserved
  767. * @res_name: Name to be associated with resource.
  768. *
  769. * Mark the PCI region associated with PCI device @pdev BR @bar as
  770. * being reserved by owner @res_name. Do not access any
  771. * address inside the PCI regions unless this call returns
  772. * successfully.
  773. *
  774. * Returns 0 on success, or %EBUSY on error. A warning
  775. * message is also printed on failure.
  776. */
  777. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  778. {
  779. if (pci_resource_len(pdev, bar) == 0)
  780. return 0;
  781. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  782. if (!request_region(pci_resource_start(pdev, bar),
  783. pci_resource_len(pdev, bar), res_name))
  784. goto err_out;
  785. }
  786. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  787. if (!request_mem_region(pci_resource_start(pdev, bar),
  788. pci_resource_len(pdev, bar), res_name))
  789. goto err_out;
  790. }
  791. return 0;
  792. err_out:
  793. printk (KERN_WARNING "PCI: Unable to reserve %s region #%d:%llx@%llx "
  794. "for device %s\n",
  795. pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
  796. bar + 1, /* PCI BAR # */
  797. (unsigned long long)pci_resource_len(pdev, bar),
  798. (unsigned long long)pci_resource_start(pdev, bar),
  799. pci_name(pdev));
  800. return -EBUSY;
  801. }
  802. /**
  803. * pci_release_regions - Release reserved PCI I/O and memory resources
  804. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  805. *
  806. * Releases all PCI I/O and memory resources previously reserved by a
  807. * successful call to pci_request_regions. Call this function only
  808. * after all use of the PCI regions has ceased.
  809. */
  810. void pci_release_regions(struct pci_dev *pdev)
  811. {
  812. int i;
  813. for (i = 0; i < 6; i++)
  814. pci_release_region(pdev, i);
  815. }
  816. /**
  817. * pci_request_regions - Reserved PCI I/O and memory resources
  818. * @pdev: PCI device whose resources are to be reserved
  819. * @res_name: Name to be associated with resource.
  820. *
  821. * Mark all PCI regions associated with PCI device @pdev as
  822. * being reserved by owner @res_name. Do not access any
  823. * address inside the PCI regions unless this call returns
  824. * successfully.
  825. *
  826. * Returns 0 on success, or %EBUSY on error. A warning
  827. * message is also printed on failure.
  828. */
  829. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  830. {
  831. int i;
  832. for (i = 0; i < 6; i++)
  833. if(pci_request_region(pdev, i, res_name))
  834. goto err_out;
  835. return 0;
  836. err_out:
  837. while(--i >= 0)
  838. pci_release_region(pdev, i);
  839. return -EBUSY;
  840. }
  841. /**
  842. * pci_set_master - enables bus-mastering for device dev
  843. * @dev: the PCI device to enable
  844. *
  845. * Enables bus-mastering on the device and calls pcibios_set_master()
  846. * to do the needed arch specific settings.
  847. */
  848. void
  849. pci_set_master(struct pci_dev *dev)
  850. {
  851. u16 cmd;
  852. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  853. if (! (cmd & PCI_COMMAND_MASTER)) {
  854. pr_debug("PCI: Enabling bus mastering for device %s\n", pci_name(dev));
  855. cmd |= PCI_COMMAND_MASTER;
  856. pci_write_config_word(dev, PCI_COMMAND, cmd);
  857. }
  858. dev->is_busmaster = 1;
  859. pcibios_set_master(dev);
  860. }
  861. #ifdef PCI_DISABLE_MWI
  862. int pci_set_mwi(struct pci_dev *dev)
  863. {
  864. return 0;
  865. }
  866. void pci_clear_mwi(struct pci_dev *dev)
  867. {
  868. }
  869. #else
  870. #ifndef PCI_CACHE_LINE_BYTES
  871. #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES
  872. #endif
  873. /* This can be overridden by arch code. */
  874. /* Don't forget this is measured in 32-bit words, not bytes */
  875. u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4;
  876. /**
  877. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  878. * @dev: the PCI device for which MWI is to be enabled
  879. *
  880. * Helper function for pci_set_mwi.
  881. * Originally copied from drivers/net/acenic.c.
  882. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  883. *
  884. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  885. */
  886. static int
  887. pci_set_cacheline_size(struct pci_dev *dev)
  888. {
  889. u8 cacheline_size;
  890. if (!pci_cache_line_size)
  891. return -EINVAL; /* The system doesn't support MWI. */
  892. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  893. equal to or multiple of the right value. */
  894. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  895. if (cacheline_size >= pci_cache_line_size &&
  896. (cacheline_size % pci_cache_line_size) == 0)
  897. return 0;
  898. /* Write the correct value. */
  899. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  900. /* Read it back. */
  901. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  902. if (cacheline_size == pci_cache_line_size)
  903. return 0;
  904. printk(KERN_DEBUG "PCI: cache line size of %d is not supported "
  905. "by device %s\n", pci_cache_line_size << 2, pci_name(dev));
  906. return -EINVAL;
  907. }
  908. /**
  909. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  910. * @dev: the PCI device for which MWI is enabled
  911. *
  912. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND,
  913. * and then calls @pcibios_set_mwi to do the needed arch specific
  914. * operations or a generic mwi-prep function.
  915. *
  916. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  917. */
  918. int
  919. pci_set_mwi(struct pci_dev *dev)
  920. {
  921. int rc;
  922. u16 cmd;
  923. rc = pci_set_cacheline_size(dev);
  924. if (rc)
  925. return rc;
  926. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  927. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  928. pr_debug("PCI: Enabling Mem-Wr-Inval for device %s\n", pci_name(dev));
  929. cmd |= PCI_COMMAND_INVALIDATE;
  930. pci_write_config_word(dev, PCI_COMMAND, cmd);
  931. }
  932. return 0;
  933. }
  934. /**
  935. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  936. * @dev: the PCI device to disable
  937. *
  938. * Disables PCI Memory-Write-Invalidate transaction on the device
  939. */
  940. void
  941. pci_clear_mwi(struct pci_dev *dev)
  942. {
  943. u16 cmd;
  944. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  945. if (cmd & PCI_COMMAND_INVALIDATE) {
  946. cmd &= ~PCI_COMMAND_INVALIDATE;
  947. pci_write_config_word(dev, PCI_COMMAND, cmd);
  948. }
  949. }
  950. #endif /* ! PCI_DISABLE_MWI */
  951. /**
  952. * pci_intx - enables/disables PCI INTx for device dev
  953. * @pdev: the PCI device to operate on
  954. * @enable: boolean: whether to enable or disable PCI INTx
  955. *
  956. * Enables/disables PCI INTx for device dev
  957. */
  958. void
  959. pci_intx(struct pci_dev *pdev, int enable)
  960. {
  961. u16 pci_command, new;
  962. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  963. if (enable) {
  964. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  965. } else {
  966. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  967. }
  968. if (new != pci_command) {
  969. pci_write_config_word(pdev, PCI_COMMAND, new);
  970. }
  971. }
  972. #ifndef HAVE_ARCH_PCI_SET_DMA_MASK
  973. /*
  974. * These can be overridden by arch-specific implementations
  975. */
  976. int
  977. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  978. {
  979. if (!pci_dma_supported(dev, mask))
  980. return -EIO;
  981. dev->dma_mask = mask;
  982. return 0;
  983. }
  984. int
  985. pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  986. {
  987. if (!pci_dma_supported(dev, mask))
  988. return -EIO;
  989. dev->dev.coherent_dma_mask = mask;
  990. return 0;
  991. }
  992. #endif
  993. static int __devinit pci_init(void)
  994. {
  995. struct pci_dev *dev = NULL;
  996. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  997. pci_fixup_device(pci_fixup_final, dev);
  998. }
  999. return 0;
  1000. }
  1001. static int __devinit pci_setup(char *str)
  1002. {
  1003. while (str) {
  1004. char *k = strchr(str, ',');
  1005. if (k)
  1006. *k++ = 0;
  1007. if (*str && (str = pcibios_setup(str)) && *str) {
  1008. if (!strcmp(str, "nomsi")) {
  1009. pci_no_msi();
  1010. } else {
  1011. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  1012. str);
  1013. }
  1014. }
  1015. str = k;
  1016. }
  1017. return 0;
  1018. }
  1019. early_param("pci", pci_setup);
  1020. device_initcall(pci_init);
  1021. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  1022. /* FIXME: Some boxes have multiple ISA bridges! */
  1023. struct pci_dev *isa_bridge;
  1024. EXPORT_SYMBOL(isa_bridge);
  1025. #endif
  1026. EXPORT_SYMBOL_GPL(pci_restore_bars);
  1027. EXPORT_SYMBOL(pci_enable_device_bars);
  1028. EXPORT_SYMBOL(pci_enable_device);
  1029. EXPORT_SYMBOL(pci_disable_device);
  1030. EXPORT_SYMBOL(pci_find_capability);
  1031. EXPORT_SYMBOL(pci_bus_find_capability);
  1032. EXPORT_SYMBOL(pci_release_regions);
  1033. EXPORT_SYMBOL(pci_request_regions);
  1034. EXPORT_SYMBOL(pci_release_region);
  1035. EXPORT_SYMBOL(pci_request_region);
  1036. EXPORT_SYMBOL(pci_set_master);
  1037. EXPORT_SYMBOL(pci_set_mwi);
  1038. EXPORT_SYMBOL(pci_clear_mwi);
  1039. EXPORT_SYMBOL_GPL(pci_intx);
  1040. EXPORT_SYMBOL(pci_set_dma_mask);
  1041. EXPORT_SYMBOL(pci_set_consistent_dma_mask);
  1042. EXPORT_SYMBOL(pci_assign_resource);
  1043. EXPORT_SYMBOL(pci_find_parent_resource);
  1044. EXPORT_SYMBOL(pci_set_power_state);
  1045. EXPORT_SYMBOL(pci_save_state);
  1046. EXPORT_SYMBOL(pci_restore_state);
  1047. EXPORT_SYMBOL(pci_enable_wake);
  1048. /* Quirk info */
  1049. EXPORT_SYMBOL(isa_dma_bridge_buggy);
  1050. EXPORT_SYMBOL(pci_pci_problems);