pci.c 43 KB

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