pci.c 27 KB

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