pci.c 24 KB

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