pci.c 31 KB

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