pci.c 24 KB

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