pci.c 22 KB

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