pci.c 45 KB

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