pci.c 31 KB

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