pci.c 37 KB

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