pci.c 24 KB

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