pci.c 42 KB

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