pci.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411
  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 <linux/pm_wakeup.h>
  20. #include <linux/interrupt.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. #ifdef CONFIG_HAS_IOMEM
  53. void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
  54. {
  55. /*
  56. * Make sure the BAR is actually a memory resource, not an IO resource
  57. */
  58. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  59. WARN_ON(1);
  60. return NULL;
  61. }
  62. return ioremap_nocache(pci_resource_start(pdev, bar),
  63. pci_resource_len(pdev, bar));
  64. }
  65. EXPORT_SYMBOL_GPL(pci_ioremap_bar);
  66. #endif
  67. #if 0
  68. /**
  69. * pci_max_busnr - returns maximum PCI bus number
  70. *
  71. * Returns the highest PCI bus number present in the system global list of
  72. * PCI buses.
  73. */
  74. unsigned char __devinit
  75. pci_max_busnr(void)
  76. {
  77. struct pci_bus *bus = NULL;
  78. unsigned char max, n;
  79. max = 0;
  80. while ((bus = pci_find_next_bus(bus)) != NULL) {
  81. n = pci_bus_max_busnr(bus);
  82. if(n > max)
  83. max = n;
  84. }
  85. return max;
  86. }
  87. #endif /* 0 */
  88. #define PCI_FIND_CAP_TTL 48
  89. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  90. u8 pos, int cap, int *ttl)
  91. {
  92. u8 id;
  93. while ((*ttl)--) {
  94. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  95. if (pos < 0x40)
  96. break;
  97. pos &= ~3;
  98. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  99. &id);
  100. if (id == 0xff)
  101. break;
  102. if (id == cap)
  103. return pos;
  104. pos += PCI_CAP_LIST_NEXT;
  105. }
  106. return 0;
  107. }
  108. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  109. u8 pos, int cap)
  110. {
  111. int ttl = PCI_FIND_CAP_TTL;
  112. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  113. }
  114. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  115. {
  116. return __pci_find_next_cap(dev->bus, dev->devfn,
  117. pos + PCI_CAP_LIST_NEXT, cap);
  118. }
  119. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  120. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  121. unsigned int devfn, u8 hdr_type)
  122. {
  123. u16 status;
  124. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  125. if (!(status & PCI_STATUS_CAP_LIST))
  126. return 0;
  127. switch (hdr_type) {
  128. case PCI_HEADER_TYPE_NORMAL:
  129. case PCI_HEADER_TYPE_BRIDGE:
  130. return PCI_CAPABILITY_LIST;
  131. case PCI_HEADER_TYPE_CARDBUS:
  132. return PCI_CB_CAPABILITY_LIST;
  133. default:
  134. return 0;
  135. }
  136. return 0;
  137. }
  138. /**
  139. * pci_find_capability - query for devices' capabilities
  140. * @dev: PCI device to query
  141. * @cap: capability code
  142. *
  143. * Tell if a device supports a given PCI capability.
  144. * Returns the address of the requested capability structure within the
  145. * device's PCI configuration space or 0 in case the device does not
  146. * support it. Possible values for @cap:
  147. *
  148. * %PCI_CAP_ID_PM Power Management
  149. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  150. * %PCI_CAP_ID_VPD Vital Product Data
  151. * %PCI_CAP_ID_SLOTID Slot Identification
  152. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  153. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  154. * %PCI_CAP_ID_PCIX PCI-X
  155. * %PCI_CAP_ID_EXP PCI Express
  156. */
  157. int pci_find_capability(struct pci_dev *dev, int cap)
  158. {
  159. int pos;
  160. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  161. if (pos)
  162. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  163. return pos;
  164. }
  165. /**
  166. * pci_bus_find_capability - query for devices' capabilities
  167. * @bus: the PCI bus to query
  168. * @devfn: PCI device to query
  169. * @cap: capability code
  170. *
  171. * Like pci_find_capability() but works for pci devices that do not have a
  172. * pci_dev structure set up yet.
  173. *
  174. * Returns the address of the requested capability structure within the
  175. * device's PCI configuration space or 0 in case the device does not
  176. * support it.
  177. */
  178. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  179. {
  180. int pos;
  181. u8 hdr_type;
  182. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  183. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  184. if (pos)
  185. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  186. return pos;
  187. }
  188. /**
  189. * pci_find_ext_capability - Find an extended capability
  190. * @dev: PCI device to query
  191. * @cap: capability code
  192. *
  193. * Returns the address of the requested extended capability structure
  194. * within the device's PCI configuration space or 0 if the device does
  195. * not support it. Possible values for @cap:
  196. *
  197. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  198. * %PCI_EXT_CAP_ID_VC Virtual Channel
  199. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  200. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  201. */
  202. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  203. {
  204. u32 header;
  205. int ttl;
  206. int pos = PCI_CFG_SPACE_SIZE;
  207. /* minimum 8 bytes per capability */
  208. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  209. if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
  210. return 0;
  211. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  212. return 0;
  213. /*
  214. * If we have no capabilities, this is indicated by cap ID,
  215. * cap version and next pointer all being 0.
  216. */
  217. if (header == 0)
  218. return 0;
  219. while (ttl-- > 0) {
  220. if (PCI_EXT_CAP_ID(header) == cap)
  221. return pos;
  222. pos = PCI_EXT_CAP_NEXT(header);
  223. if (pos < PCI_CFG_SPACE_SIZE)
  224. break;
  225. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  226. break;
  227. }
  228. return 0;
  229. }
  230. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  231. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  232. {
  233. int rc, ttl = PCI_FIND_CAP_TTL;
  234. u8 cap, mask;
  235. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  236. mask = HT_3BIT_CAP_MASK;
  237. else
  238. mask = HT_5BIT_CAP_MASK;
  239. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  240. PCI_CAP_ID_HT, &ttl);
  241. while (pos) {
  242. rc = pci_read_config_byte(dev, pos + 3, &cap);
  243. if (rc != PCIBIOS_SUCCESSFUL)
  244. return 0;
  245. if ((cap & mask) == ht_cap)
  246. return pos;
  247. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  248. pos + PCI_CAP_LIST_NEXT,
  249. PCI_CAP_ID_HT, &ttl);
  250. }
  251. return 0;
  252. }
  253. /**
  254. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  255. * @dev: PCI device to query
  256. * @pos: Position from which to continue searching
  257. * @ht_cap: Hypertransport capability code
  258. *
  259. * To be used in conjunction with pci_find_ht_capability() to search for
  260. * all capabilities matching @ht_cap. @pos should always be a value returned
  261. * from pci_find_ht_capability().
  262. *
  263. * NB. To be 100% safe against broken PCI devices, the caller should take
  264. * steps to avoid an infinite loop.
  265. */
  266. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  267. {
  268. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  269. }
  270. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  271. /**
  272. * pci_find_ht_capability - query a device's Hypertransport capabilities
  273. * @dev: PCI device to query
  274. * @ht_cap: Hypertransport capability code
  275. *
  276. * Tell if a device supports a given Hypertransport capability.
  277. * Returns an address within the device's PCI configuration space
  278. * or 0 in case the device does not support the request capability.
  279. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  280. * which has a Hypertransport capability matching @ht_cap.
  281. */
  282. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  283. {
  284. int pos;
  285. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  286. if (pos)
  287. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  288. return pos;
  289. }
  290. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  291. /**
  292. * pci_find_parent_resource - return resource region of parent bus of given region
  293. * @dev: PCI device structure contains resources to be searched
  294. * @res: child resource record for which parent is sought
  295. *
  296. * For given resource region of given device, return the resource
  297. * region of parent bus the given region is contained in or where
  298. * it should be allocated from.
  299. */
  300. struct resource *
  301. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  302. {
  303. const struct pci_bus *bus = dev->bus;
  304. int i;
  305. struct resource *best = NULL;
  306. for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  307. struct resource *r = bus->resource[i];
  308. if (!r)
  309. continue;
  310. if (res->start && !(res->start >= r->start && res->end <= r->end))
  311. continue; /* Not contained */
  312. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  313. continue; /* Wrong type */
  314. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  315. return r; /* Exact match */
  316. if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
  317. best = r; /* Approximating prefetchable by non-prefetchable */
  318. }
  319. return best;
  320. }
  321. /**
  322. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  323. * @dev: PCI device to have its BARs restored
  324. *
  325. * Restore the BAR values for a given device, so as to make it
  326. * accessible by its driver.
  327. */
  328. static void
  329. pci_restore_bars(struct pci_dev *dev)
  330. {
  331. int i;
  332. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
  333. pci_update_resource(dev, i);
  334. }
  335. static struct pci_platform_pm_ops *pci_platform_pm;
  336. int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
  337. {
  338. if (!ops->is_manageable || !ops->set_state || !ops->choose_state
  339. || !ops->sleep_wake || !ops->can_wakeup)
  340. return -EINVAL;
  341. pci_platform_pm = ops;
  342. return 0;
  343. }
  344. static inline bool platform_pci_power_manageable(struct pci_dev *dev)
  345. {
  346. return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
  347. }
  348. static inline int platform_pci_set_power_state(struct pci_dev *dev,
  349. pci_power_t t)
  350. {
  351. return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
  352. }
  353. static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
  354. {
  355. return pci_platform_pm ?
  356. pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
  357. }
  358. static inline bool platform_pci_can_wakeup(struct pci_dev *dev)
  359. {
  360. return pci_platform_pm ? pci_platform_pm->can_wakeup(dev) : false;
  361. }
  362. static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
  363. {
  364. return pci_platform_pm ?
  365. pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
  366. }
  367. /**
  368. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  369. * given PCI device
  370. * @dev: PCI device to handle.
  371. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  372. *
  373. * RETURN VALUE:
  374. * -EINVAL if the requested state is invalid.
  375. * -EIO if device does not support PCI PM or its PM capabilities register has a
  376. * wrong version, or device doesn't support the requested state.
  377. * 0 if device already is in the requested state.
  378. * 0 if device's power state has been successfully changed.
  379. */
  380. static int
  381. pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
  382. {
  383. u16 pmcsr;
  384. bool need_restore = false;
  385. if (!dev->pm_cap)
  386. return -EIO;
  387. if (state < PCI_D0 || state > PCI_D3hot)
  388. return -EINVAL;
  389. /* Validate current state:
  390. * Can enter D0 from any state, but if we can only go deeper
  391. * to sleep if we're already in a low power state
  392. */
  393. if (dev->current_state == state) {
  394. /* we're already there */
  395. return 0;
  396. } else if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  397. && dev->current_state > state) {
  398. dev_err(&dev->dev, "invalid power transition "
  399. "(from state %d to %d)\n", dev->current_state, state);
  400. return -EINVAL;
  401. }
  402. /* check if this device supports the desired state */
  403. if ((state == PCI_D1 && !dev->d1_support)
  404. || (state == PCI_D2 && !dev->d2_support))
  405. return -EIO;
  406. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  407. /* If we're (effectively) in D3, force entire word to 0.
  408. * This doesn't affect PME_Status, disables PME_En, and
  409. * sets PowerState to 0.
  410. */
  411. switch (dev->current_state) {
  412. case PCI_D0:
  413. case PCI_D1:
  414. case PCI_D2:
  415. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  416. pmcsr |= state;
  417. break;
  418. case PCI_UNKNOWN: /* Boot-up */
  419. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  420. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  421. need_restore = true;
  422. /* Fall-through: force to D0 */
  423. default:
  424. pmcsr = 0;
  425. break;
  426. }
  427. /* enter specified state */
  428. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  429. /* Mandatory power management transition delays */
  430. /* see PCI PM 1.1 5.6.1 table 18 */
  431. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  432. msleep(pci_pm_d3_delay);
  433. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  434. udelay(200);
  435. dev->current_state = state;
  436. /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  437. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  438. * from D3hot to D0 _may_ perform an internal reset, thereby
  439. * going to "D0 Uninitialized" rather than "D0 Initialized".
  440. * For example, at least some versions of the 3c905B and the
  441. * 3c556B exhibit this behaviour.
  442. *
  443. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  444. * devices in a D3hot state at boot. Consequently, we need to
  445. * restore at least the BARs so that the device will be
  446. * accessible to its driver.
  447. */
  448. if (need_restore)
  449. pci_restore_bars(dev);
  450. if (dev->bus->self)
  451. pcie_aspm_pm_state_change(dev->bus->self);
  452. return 0;
  453. }
  454. /**
  455. * pci_update_current_state - Read PCI power state of given device from its
  456. * PCI PM registers and cache it
  457. * @dev: PCI device to handle.
  458. * @state: State to cache in case the device doesn't have the PM capability
  459. */
  460. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  461. {
  462. if (dev->pm_cap) {
  463. u16 pmcsr;
  464. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  465. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  466. } else {
  467. dev->current_state = state;
  468. }
  469. }
  470. /**
  471. * pci_set_power_state - Set the power state of a PCI device
  472. * @dev: PCI device to handle.
  473. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  474. *
  475. * Transition a device to a new power state, using the platform formware and/or
  476. * the device's PCI PM registers.
  477. *
  478. * RETURN VALUE:
  479. * -EINVAL if the requested state is invalid.
  480. * -EIO if device does not support PCI PM or its PM capabilities register has a
  481. * wrong version, or device doesn't support the requested state.
  482. * 0 if device already is in the requested state.
  483. * 0 if device's power state has been successfully changed.
  484. */
  485. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  486. {
  487. int error;
  488. /* bound the state we're entering */
  489. if (state > PCI_D3hot)
  490. state = PCI_D3hot;
  491. else if (state < PCI_D0)
  492. state = PCI_D0;
  493. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  494. /*
  495. * If the device or the parent bridge do not support PCI PM,
  496. * ignore the request if we're doing anything other than putting
  497. * it into D0 (which would only happen on boot).
  498. */
  499. return 0;
  500. if (state == PCI_D0 && platform_pci_power_manageable(dev)) {
  501. /*
  502. * Allow the platform to change the state, for example via ACPI
  503. * _PR0, _PS0 and some such, but do not trust it.
  504. */
  505. int ret = platform_pci_set_power_state(dev, PCI_D0);
  506. if (!ret)
  507. pci_update_current_state(dev, PCI_D0);
  508. }
  509. /* This device is quirked not to be put into D3, so
  510. don't put it in D3 */
  511. if (state == PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  512. return 0;
  513. error = pci_raw_set_power_state(dev, state);
  514. if (state > PCI_D0 && platform_pci_power_manageable(dev)) {
  515. /* Allow the platform to finalize the transition */
  516. int ret = platform_pci_set_power_state(dev, state);
  517. if (!ret) {
  518. pci_update_current_state(dev, state);
  519. error = 0;
  520. }
  521. }
  522. return error;
  523. }
  524. /**
  525. * pci_choose_state - Choose the power state of a PCI device
  526. * @dev: PCI device to be suspended
  527. * @state: target sleep state for the whole system. This is the value
  528. * that is passed to suspend() function.
  529. *
  530. * Returns PCI power state suitable for given device and given system
  531. * message.
  532. */
  533. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  534. {
  535. pci_power_t ret;
  536. if (!pci_find_capability(dev, PCI_CAP_ID_PM))
  537. return PCI_D0;
  538. ret = platform_pci_choose_state(dev);
  539. if (ret != PCI_POWER_ERROR)
  540. return ret;
  541. switch (state.event) {
  542. case PM_EVENT_ON:
  543. return PCI_D0;
  544. case PM_EVENT_FREEZE:
  545. case PM_EVENT_PRETHAW:
  546. /* REVISIT both freeze and pre-thaw "should" use D0 */
  547. case PM_EVENT_SUSPEND:
  548. case PM_EVENT_HIBERNATE:
  549. return PCI_D3hot;
  550. default:
  551. dev_info(&dev->dev, "unrecognized suspend event %d\n",
  552. state.event);
  553. BUG();
  554. }
  555. return PCI_D0;
  556. }
  557. EXPORT_SYMBOL(pci_choose_state);
  558. static int pci_save_pcie_state(struct pci_dev *dev)
  559. {
  560. int pos, i = 0;
  561. struct pci_cap_saved_state *save_state;
  562. u16 *cap;
  563. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  564. if (pos <= 0)
  565. return 0;
  566. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  567. if (!save_state) {
  568. dev_err(&dev->dev, "buffer not found in %s\n", __FUNCTION__);
  569. return -ENOMEM;
  570. }
  571. cap = (u16 *)&save_state->data[0];
  572. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]);
  573. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
  574. pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
  575. pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
  576. return 0;
  577. }
  578. static void pci_restore_pcie_state(struct pci_dev *dev)
  579. {
  580. int i = 0, pos;
  581. struct pci_cap_saved_state *save_state;
  582. u16 *cap;
  583. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  584. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  585. if (!save_state || pos <= 0)
  586. return;
  587. cap = (u16 *)&save_state->data[0];
  588. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]);
  589. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]);
  590. pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]);
  591. pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);
  592. }
  593. static int pci_save_pcix_state(struct pci_dev *dev)
  594. {
  595. int pos;
  596. struct pci_cap_saved_state *save_state;
  597. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  598. if (pos <= 0)
  599. return 0;
  600. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  601. if (!save_state) {
  602. dev_err(&dev->dev, "buffer not found in %s\n", __FUNCTION__);
  603. return -ENOMEM;
  604. }
  605. pci_read_config_word(dev, pos + PCI_X_CMD, (u16 *)save_state->data);
  606. return 0;
  607. }
  608. static void pci_restore_pcix_state(struct pci_dev *dev)
  609. {
  610. int i = 0, pos;
  611. struct pci_cap_saved_state *save_state;
  612. u16 *cap;
  613. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  614. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  615. if (!save_state || pos <= 0)
  616. return;
  617. cap = (u16 *)&save_state->data[0];
  618. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  619. }
  620. /**
  621. * pci_save_state - save the PCI configuration space of a device before suspending
  622. * @dev: - PCI device that we're dealing with
  623. */
  624. int
  625. pci_save_state(struct pci_dev *dev)
  626. {
  627. int i;
  628. /* XXX: 100% dword access ok here? */
  629. for (i = 0; i < 16; i++)
  630. pci_read_config_dword(dev, i * 4,&dev->saved_config_space[i]);
  631. if ((i = pci_save_pcie_state(dev)) != 0)
  632. return i;
  633. if ((i = pci_save_pcix_state(dev)) != 0)
  634. return i;
  635. return 0;
  636. }
  637. /**
  638. * pci_restore_state - Restore the saved state of a PCI device
  639. * @dev: - PCI device that we're dealing with
  640. */
  641. int
  642. pci_restore_state(struct pci_dev *dev)
  643. {
  644. int i;
  645. u32 val;
  646. /* PCI Express register must be restored first */
  647. pci_restore_pcie_state(dev);
  648. /*
  649. * The Base Address register should be programmed before the command
  650. * register(s)
  651. */
  652. for (i = 15; i >= 0; i--) {
  653. pci_read_config_dword(dev, i * 4, &val);
  654. if (val != dev->saved_config_space[i]) {
  655. dev_printk(KERN_DEBUG, &dev->dev, "restoring config "
  656. "space at offset %#x (was %#x, writing %#x)\n",
  657. i, val, (int)dev->saved_config_space[i]);
  658. pci_write_config_dword(dev,i * 4,
  659. dev->saved_config_space[i]);
  660. }
  661. }
  662. pci_restore_pcix_state(dev);
  663. pci_restore_msi_state(dev);
  664. return 0;
  665. }
  666. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  667. {
  668. int err;
  669. err = pci_set_power_state(dev, PCI_D0);
  670. if (err < 0 && err != -EIO)
  671. return err;
  672. err = pcibios_enable_device(dev, bars);
  673. if (err < 0)
  674. return err;
  675. pci_fixup_device(pci_fixup_enable, dev);
  676. return 0;
  677. }
  678. /**
  679. * pci_reenable_device - Resume abandoned device
  680. * @dev: PCI device to be resumed
  681. *
  682. * Note this function is a backend of pci_default_resume and is not supposed
  683. * to be called by normal code, write proper resume handler and use it instead.
  684. */
  685. int pci_reenable_device(struct pci_dev *dev)
  686. {
  687. if (atomic_read(&dev->enable_cnt))
  688. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  689. return 0;
  690. }
  691. static int __pci_enable_device_flags(struct pci_dev *dev,
  692. resource_size_t flags)
  693. {
  694. int err;
  695. int i, bars = 0;
  696. if (atomic_add_return(1, &dev->enable_cnt) > 1)
  697. return 0; /* already enabled */
  698. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  699. if (dev->resource[i].flags & flags)
  700. bars |= (1 << i);
  701. err = do_pci_enable_device(dev, bars);
  702. if (err < 0)
  703. atomic_dec(&dev->enable_cnt);
  704. return err;
  705. }
  706. /**
  707. * pci_enable_device_io - Initialize a device for use with IO space
  708. * @dev: PCI device to be initialized
  709. *
  710. * Initialize device before it's used by a driver. Ask low-level code
  711. * to enable I/O resources. Wake up the device if it was suspended.
  712. * Beware, this function can fail.
  713. */
  714. int pci_enable_device_io(struct pci_dev *dev)
  715. {
  716. return __pci_enable_device_flags(dev, IORESOURCE_IO);
  717. }
  718. /**
  719. * pci_enable_device_mem - Initialize a device for use with Memory space
  720. * @dev: PCI device to be initialized
  721. *
  722. * Initialize device before it's used by a driver. Ask low-level code
  723. * to enable Memory resources. Wake up the device if it was suspended.
  724. * Beware, this function can fail.
  725. */
  726. int pci_enable_device_mem(struct pci_dev *dev)
  727. {
  728. return __pci_enable_device_flags(dev, IORESOURCE_MEM);
  729. }
  730. /**
  731. * pci_enable_device - Initialize device before it's used by a driver.
  732. * @dev: PCI device to be initialized
  733. *
  734. * Initialize device before it's used by a driver. Ask low-level code
  735. * to enable I/O and memory. Wake up the device if it was suspended.
  736. * Beware, this function can fail.
  737. *
  738. * Note we don't actually enable the device many times if we call
  739. * this function repeatedly (we just increment the count).
  740. */
  741. int pci_enable_device(struct pci_dev *dev)
  742. {
  743. return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  744. }
  745. /*
  746. * Managed PCI resources. This manages device on/off, intx/msi/msix
  747. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  748. * there's no need to track it separately. pci_devres is initialized
  749. * when a device is enabled using managed PCI device enable interface.
  750. */
  751. struct pci_devres {
  752. unsigned int enabled:1;
  753. unsigned int pinned:1;
  754. unsigned int orig_intx:1;
  755. unsigned int restore_intx:1;
  756. u32 region_mask;
  757. };
  758. static void pcim_release(struct device *gendev, void *res)
  759. {
  760. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  761. struct pci_devres *this = res;
  762. int i;
  763. if (dev->msi_enabled)
  764. pci_disable_msi(dev);
  765. if (dev->msix_enabled)
  766. pci_disable_msix(dev);
  767. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  768. if (this->region_mask & (1 << i))
  769. pci_release_region(dev, i);
  770. if (this->restore_intx)
  771. pci_intx(dev, this->orig_intx);
  772. if (this->enabled && !this->pinned)
  773. pci_disable_device(dev);
  774. }
  775. static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
  776. {
  777. struct pci_devres *dr, *new_dr;
  778. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  779. if (dr)
  780. return dr;
  781. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  782. if (!new_dr)
  783. return NULL;
  784. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  785. }
  786. static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
  787. {
  788. if (pci_is_managed(pdev))
  789. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  790. return NULL;
  791. }
  792. /**
  793. * pcim_enable_device - Managed pci_enable_device()
  794. * @pdev: PCI device to be initialized
  795. *
  796. * Managed pci_enable_device().
  797. */
  798. int pcim_enable_device(struct pci_dev *pdev)
  799. {
  800. struct pci_devres *dr;
  801. int rc;
  802. dr = get_pci_dr(pdev);
  803. if (unlikely(!dr))
  804. return -ENOMEM;
  805. if (dr->enabled)
  806. return 0;
  807. rc = pci_enable_device(pdev);
  808. if (!rc) {
  809. pdev->is_managed = 1;
  810. dr->enabled = 1;
  811. }
  812. return rc;
  813. }
  814. /**
  815. * pcim_pin_device - Pin managed PCI device
  816. * @pdev: PCI device to pin
  817. *
  818. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  819. * driver detach. @pdev must have been enabled with
  820. * pcim_enable_device().
  821. */
  822. void pcim_pin_device(struct pci_dev *pdev)
  823. {
  824. struct pci_devres *dr;
  825. dr = find_pci_dr(pdev);
  826. WARN_ON(!dr || !dr->enabled);
  827. if (dr)
  828. dr->pinned = 1;
  829. }
  830. /**
  831. * pcibios_disable_device - disable arch specific PCI resources for device dev
  832. * @dev: the PCI device to disable
  833. *
  834. * Disables architecture specific PCI resources for the device. This
  835. * is the default implementation. Architecture implementations can
  836. * override this.
  837. */
  838. void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
  839. static void do_pci_disable_device(struct pci_dev *dev)
  840. {
  841. u16 pci_command;
  842. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  843. if (pci_command & PCI_COMMAND_MASTER) {
  844. pci_command &= ~PCI_COMMAND_MASTER;
  845. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  846. }
  847. pcibios_disable_device(dev);
  848. }
  849. /**
  850. * pci_disable_enabled_device - Disable device without updating enable_cnt
  851. * @dev: PCI device to disable
  852. *
  853. * NOTE: This function is a backend of PCI power management routines and is
  854. * not supposed to be called drivers.
  855. */
  856. void pci_disable_enabled_device(struct pci_dev *dev)
  857. {
  858. if (atomic_read(&dev->enable_cnt))
  859. do_pci_disable_device(dev);
  860. }
  861. /**
  862. * pci_disable_device - Disable PCI device after use
  863. * @dev: PCI device to be disabled
  864. *
  865. * Signal to the system that the PCI device is not in use by the system
  866. * anymore. This only involves disabling PCI bus-mastering, if active.
  867. *
  868. * Note we don't actually disable the device until all callers of
  869. * pci_device_enable() have called pci_device_disable().
  870. */
  871. void
  872. pci_disable_device(struct pci_dev *dev)
  873. {
  874. struct pci_devres *dr;
  875. dr = find_pci_dr(dev);
  876. if (dr)
  877. dr->enabled = 0;
  878. if (atomic_sub_return(1, &dev->enable_cnt) != 0)
  879. return;
  880. do_pci_disable_device(dev);
  881. dev->is_busmaster = 0;
  882. }
  883. /**
  884. * pcibios_set_pcie_reset_state - set reset state for device dev
  885. * @dev: the PCI-E device reset
  886. * @state: Reset state to enter into
  887. *
  888. *
  889. * Sets the PCI-E reset state for the device. This is the default
  890. * implementation. Architecture implementations can override this.
  891. */
  892. int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev,
  893. enum pcie_reset_state state)
  894. {
  895. return -EINVAL;
  896. }
  897. /**
  898. * pci_set_pcie_reset_state - set reset state for device dev
  899. * @dev: the PCI-E device reset
  900. * @state: Reset state to enter into
  901. *
  902. *
  903. * Sets the PCI reset state for the device.
  904. */
  905. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  906. {
  907. return pcibios_set_pcie_reset_state(dev, state);
  908. }
  909. /**
  910. * pci_pme_capable - check the capability of PCI device to generate PME#
  911. * @dev: PCI device to handle.
  912. * @state: PCI state from which device will issue PME#.
  913. */
  914. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  915. {
  916. if (!dev->pm_cap)
  917. return false;
  918. return !!(dev->pme_support & (1 << state));
  919. }
  920. /**
  921. * pci_pme_active - enable or disable PCI device's PME# function
  922. * @dev: PCI device to handle.
  923. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  924. *
  925. * The caller must verify that the device is capable of generating PME# before
  926. * calling this function with @enable equal to 'true'.
  927. */
  928. void pci_pme_active(struct pci_dev *dev, bool enable)
  929. {
  930. u16 pmcsr;
  931. if (!dev->pm_cap)
  932. return;
  933. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  934. /* Clear PME_Status by writing 1 to it and enable PME# */
  935. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  936. if (!enable)
  937. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  938. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  939. dev_printk(KERN_INFO, &dev->dev, "PME# %s\n",
  940. enable ? "enabled" : "disabled");
  941. }
  942. /**
  943. * pci_enable_wake - enable PCI device as wakeup event source
  944. * @dev: PCI device affected
  945. * @state: PCI state from which device will issue wakeup events
  946. * @enable: True to enable event generation; false to disable
  947. *
  948. * This enables the device as a wakeup event source, or disables it.
  949. * When such events involves platform-specific hooks, those hooks are
  950. * called automatically by this routine.
  951. *
  952. * Devices with legacy power management (no standard PCI PM capabilities)
  953. * always require such platform hooks.
  954. *
  955. * RETURN VALUE:
  956. * 0 is returned on success
  957. * -EINVAL is returned if device is not supposed to wake up the system
  958. * Error code depending on the platform is returned if both the platform and
  959. * the native mechanism fail to enable the generation of wake-up events
  960. */
  961. int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
  962. {
  963. int error = 0;
  964. bool pme_done = false;
  965. if (enable && !device_may_wakeup(&dev->dev))
  966. return -EINVAL;
  967. /*
  968. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  969. * Anderson we should be doing PME# wake enable followed by ACPI wake
  970. * enable. To disable wake-up we call the platform first, for symmetry.
  971. */
  972. if (!enable && platform_pci_can_wakeup(dev))
  973. error = platform_pci_sleep_wake(dev, false);
  974. if (!enable || pci_pme_capable(dev, state)) {
  975. pci_pme_active(dev, enable);
  976. pme_done = true;
  977. }
  978. if (enable && platform_pci_can_wakeup(dev))
  979. error = platform_pci_sleep_wake(dev, true);
  980. return pme_done ? 0 : error;
  981. }
  982. /**
  983. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  984. * @dev: PCI device to prepare
  985. * @enable: True to enable wake-up event generation; false to disable
  986. *
  987. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  988. * and this function allows them to set that up cleanly - pci_enable_wake()
  989. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  990. * ordering constraints.
  991. *
  992. * This function only returns error code if the device is not capable of
  993. * generating PME# from both D3_hot and D3_cold, and the platform is unable to
  994. * enable wake-up power for it.
  995. */
  996. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  997. {
  998. return pci_pme_capable(dev, PCI_D3cold) ?
  999. pci_enable_wake(dev, PCI_D3cold, enable) :
  1000. pci_enable_wake(dev, PCI_D3hot, enable);
  1001. }
  1002. /**
  1003. * pci_target_state - find an appropriate low power state for a given PCI dev
  1004. * @dev: PCI device
  1005. *
  1006. * Use underlying platform code to find a supported low power state for @dev.
  1007. * If the platform can't manage @dev, return the deepest state from which it
  1008. * can generate wake events, based on any available PME info.
  1009. */
  1010. pci_power_t pci_target_state(struct pci_dev *dev)
  1011. {
  1012. pci_power_t target_state = PCI_D3hot;
  1013. if (platform_pci_power_manageable(dev)) {
  1014. /*
  1015. * Call the platform to choose the target state of the device
  1016. * and enable wake-up from this state if supported.
  1017. */
  1018. pci_power_t state = platform_pci_choose_state(dev);
  1019. switch (state) {
  1020. case PCI_POWER_ERROR:
  1021. case PCI_UNKNOWN:
  1022. break;
  1023. case PCI_D1:
  1024. case PCI_D2:
  1025. if (pci_no_d1d2(dev))
  1026. break;
  1027. default:
  1028. target_state = state;
  1029. }
  1030. } else if (device_may_wakeup(&dev->dev)) {
  1031. /*
  1032. * Find the deepest state from which the device can generate
  1033. * wake-up events, make it the target state and enable device
  1034. * to generate PME#.
  1035. */
  1036. if (!dev->pm_cap)
  1037. return PCI_POWER_ERROR;
  1038. if (dev->pme_support) {
  1039. while (target_state
  1040. && !(dev->pme_support & (1 << target_state)))
  1041. target_state--;
  1042. }
  1043. }
  1044. return target_state;
  1045. }
  1046. /**
  1047. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1048. * @dev: Device to handle.
  1049. *
  1050. * Choose the power state appropriate for the device depending on whether
  1051. * it can wake up the system and/or is power manageable by the platform
  1052. * (PCI_D3hot is the default) and put the device into that state.
  1053. */
  1054. int pci_prepare_to_sleep(struct pci_dev *dev)
  1055. {
  1056. pci_power_t target_state = pci_target_state(dev);
  1057. int error;
  1058. if (target_state == PCI_POWER_ERROR)
  1059. return -EIO;
  1060. pci_enable_wake(dev, target_state, true);
  1061. error = pci_set_power_state(dev, target_state);
  1062. if (error)
  1063. pci_enable_wake(dev, target_state, false);
  1064. return error;
  1065. }
  1066. /**
  1067. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1068. * @dev: Device to handle.
  1069. *
  1070. * Disable device's sytem wake-up capability and put it into D0.
  1071. */
  1072. int pci_back_from_sleep(struct pci_dev *dev)
  1073. {
  1074. pci_enable_wake(dev, PCI_D0, false);
  1075. return pci_set_power_state(dev, PCI_D0);
  1076. }
  1077. /**
  1078. * pci_pm_init - Initialize PM functions of given PCI device
  1079. * @dev: PCI device to handle.
  1080. */
  1081. void pci_pm_init(struct pci_dev *dev)
  1082. {
  1083. int pm;
  1084. u16 pmc;
  1085. dev->pm_cap = 0;
  1086. /* find PCI PM capability in list */
  1087. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  1088. if (!pm)
  1089. goto Exit;
  1090. /* Check device's ability to generate PME# */
  1091. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  1092. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  1093. dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
  1094. pmc & PCI_PM_CAP_VER_MASK);
  1095. goto Exit;
  1096. }
  1097. dev->pm_cap = pm;
  1098. dev->d1_support = false;
  1099. dev->d2_support = false;
  1100. if (!pci_no_d1d2(dev)) {
  1101. if (pmc & PCI_PM_CAP_D1)
  1102. dev->d1_support = true;
  1103. if (pmc & PCI_PM_CAP_D2)
  1104. dev->d2_support = true;
  1105. if (dev->d1_support || dev->d2_support)
  1106. dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
  1107. dev->d1_support ? " D1" : "",
  1108. dev->d2_support ? " D2" : "");
  1109. }
  1110. pmc &= PCI_PM_CAP_PME_MASK;
  1111. if (pmc) {
  1112. dev_info(&dev->dev, "PME# supported from%s%s%s%s%s\n",
  1113. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  1114. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  1115. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  1116. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  1117. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  1118. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  1119. /*
  1120. * Make device's PM flags reflect the wake-up capability, but
  1121. * let the user space enable it to wake up the system as needed.
  1122. */
  1123. device_set_wakeup_capable(&dev->dev, true);
  1124. device_set_wakeup_enable(&dev->dev, false);
  1125. /* Disable the PME# generation functionality */
  1126. pci_pme_active(dev, false);
  1127. } else {
  1128. dev->pme_support = 0;
  1129. }
  1130. Exit:
  1131. pci_update_current_state(dev, PCI_D0);
  1132. }
  1133. /**
  1134. * platform_pci_wakeup_init - init platform wakeup if present
  1135. * @dev: PCI device
  1136. *
  1137. * Some devices don't have PCI PM caps but can still generate wakeup
  1138. * events through platform methods (like ACPI events). If @dev supports
  1139. * platform wakeup events, set the device flag to indicate as much. This
  1140. * may be redundant if the device also supports PCI PM caps, but double
  1141. * initialization should be safe in that case.
  1142. */
  1143. void platform_pci_wakeup_init(struct pci_dev *dev)
  1144. {
  1145. if (!platform_pci_can_wakeup(dev))
  1146. return;
  1147. device_set_wakeup_capable(&dev->dev, true);
  1148. device_set_wakeup_enable(&dev->dev, false);
  1149. platform_pci_sleep_wake(dev, false);
  1150. }
  1151. /**
  1152. * pci_add_save_buffer - allocate buffer for saving given capability registers
  1153. * @dev: the PCI device
  1154. * @cap: the capability to allocate the buffer for
  1155. * @size: requested size of the buffer
  1156. */
  1157. static int pci_add_cap_save_buffer(
  1158. struct pci_dev *dev, char cap, unsigned int size)
  1159. {
  1160. int pos;
  1161. struct pci_cap_saved_state *save_state;
  1162. pos = pci_find_capability(dev, cap);
  1163. if (pos <= 0)
  1164. return 0;
  1165. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  1166. if (!save_state)
  1167. return -ENOMEM;
  1168. save_state->cap_nr = cap;
  1169. pci_add_saved_cap(dev, save_state);
  1170. return 0;
  1171. }
  1172. /**
  1173. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  1174. * @dev: the PCI device
  1175. */
  1176. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  1177. {
  1178. int error;
  1179. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP, 4 * sizeof(u16));
  1180. if (error)
  1181. dev_err(&dev->dev,
  1182. "unable to preallocate PCI Express save buffer\n");
  1183. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  1184. if (error)
  1185. dev_err(&dev->dev,
  1186. "unable to preallocate PCI-X save buffer\n");
  1187. }
  1188. /**
  1189. * pci_enable_ari - enable ARI forwarding if hardware support it
  1190. * @dev: the PCI device
  1191. */
  1192. void pci_enable_ari(struct pci_dev *dev)
  1193. {
  1194. int pos;
  1195. u32 cap;
  1196. u16 ctrl;
  1197. struct pci_dev *bridge;
  1198. if (!dev->is_pcie || dev->devfn)
  1199. return;
  1200. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  1201. if (!pos)
  1202. return;
  1203. bridge = dev->bus->self;
  1204. if (!bridge || !bridge->is_pcie)
  1205. return;
  1206. pos = pci_find_capability(bridge, PCI_CAP_ID_EXP);
  1207. if (!pos)
  1208. return;
  1209. pci_read_config_dword(bridge, pos + PCI_EXP_DEVCAP2, &cap);
  1210. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  1211. return;
  1212. pci_read_config_word(bridge, pos + PCI_EXP_DEVCTL2, &ctrl);
  1213. ctrl |= PCI_EXP_DEVCTL2_ARI;
  1214. pci_write_config_word(bridge, pos + PCI_EXP_DEVCTL2, ctrl);
  1215. bridge->ari_enabled = 1;
  1216. }
  1217. /**
  1218. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  1219. * @dev: the PCI device
  1220. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1221. *
  1222. * Perform INTx swizzling for a device behind one level of bridge. This is
  1223. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  1224. * behind bridges on add-in cards.
  1225. */
  1226. u8 pci_swizzle_interrupt_pin(struct pci_dev *dev, u8 pin)
  1227. {
  1228. return (((pin - 1) + PCI_SLOT(dev->devfn)) % 4) + 1;
  1229. }
  1230. int
  1231. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  1232. {
  1233. u8 pin;
  1234. pin = dev->pin;
  1235. if (!pin)
  1236. return -1;
  1237. while (dev->bus->self) {
  1238. pin = pci_swizzle_interrupt_pin(dev, pin);
  1239. dev = dev->bus->self;
  1240. }
  1241. *bridge = dev;
  1242. return pin;
  1243. }
  1244. /**
  1245. * pci_common_swizzle - swizzle INTx all the way to root bridge
  1246. * @dev: the PCI device
  1247. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1248. *
  1249. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  1250. * bridges all the way up to a PCI root bus.
  1251. */
  1252. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  1253. {
  1254. u8 pin = *pinp;
  1255. while (dev->bus->self) {
  1256. pin = pci_swizzle_interrupt_pin(dev, pin);
  1257. dev = dev->bus->self;
  1258. }
  1259. *pinp = pin;
  1260. return PCI_SLOT(dev->devfn);
  1261. }
  1262. /**
  1263. * pci_release_region - Release a PCI bar
  1264. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  1265. * @bar: BAR to release
  1266. *
  1267. * Releases the PCI I/O and memory resources previously reserved by a
  1268. * successful call to pci_request_region. Call this function only
  1269. * after all use of the PCI regions has ceased.
  1270. */
  1271. void pci_release_region(struct pci_dev *pdev, int bar)
  1272. {
  1273. struct pci_devres *dr;
  1274. if (pci_resource_len(pdev, bar) == 0)
  1275. return;
  1276. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  1277. release_region(pci_resource_start(pdev, bar),
  1278. pci_resource_len(pdev, bar));
  1279. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  1280. release_mem_region(pci_resource_start(pdev, bar),
  1281. pci_resource_len(pdev, bar));
  1282. dr = find_pci_dr(pdev);
  1283. if (dr)
  1284. dr->region_mask &= ~(1 << bar);
  1285. }
  1286. /**
  1287. * pci_request_region - Reserved PCI I/O and memory resource
  1288. * @pdev: PCI device whose resources are to be reserved
  1289. * @bar: BAR to be reserved
  1290. * @res_name: Name to be associated with resource.
  1291. *
  1292. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1293. * being reserved by owner @res_name. Do not access any
  1294. * address inside the PCI regions unless this call returns
  1295. * successfully.
  1296. *
  1297. * Returns 0 on success, or %EBUSY on error. A warning
  1298. * message is also printed on failure.
  1299. */
  1300. static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
  1301. int exclusive)
  1302. {
  1303. struct pci_devres *dr;
  1304. if (pci_resource_len(pdev, bar) == 0)
  1305. return 0;
  1306. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  1307. if (!request_region(pci_resource_start(pdev, bar),
  1308. pci_resource_len(pdev, bar), res_name))
  1309. goto err_out;
  1310. }
  1311. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  1312. if (!__request_mem_region(pci_resource_start(pdev, bar),
  1313. pci_resource_len(pdev, bar), res_name,
  1314. exclusive))
  1315. goto err_out;
  1316. }
  1317. dr = find_pci_dr(pdev);
  1318. if (dr)
  1319. dr->region_mask |= 1 << bar;
  1320. return 0;
  1321. err_out:
  1322. dev_warn(&pdev->dev, "BAR %d: can't reserve %s region %pR\n",
  1323. bar,
  1324. pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
  1325. &pdev->resource[bar]);
  1326. return -EBUSY;
  1327. }
  1328. /**
  1329. * pci_request_region - Reserved PCI I/O and memory resource
  1330. * @pdev: PCI device whose resources are to be reserved
  1331. * @bar: BAR to be reserved
  1332. * @res_name: Name to be associated with resource.
  1333. *
  1334. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1335. * being reserved by owner @res_name. Do not access any
  1336. * address inside the PCI regions unless this call returns
  1337. * successfully.
  1338. *
  1339. * Returns 0 on success, or %EBUSY on error. A warning
  1340. * message is also printed on failure.
  1341. */
  1342. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  1343. {
  1344. return __pci_request_region(pdev, bar, res_name, 0);
  1345. }
  1346. /**
  1347. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  1348. * @pdev: PCI device whose resources are to be reserved
  1349. * @bar: BAR to be reserved
  1350. * @res_name: Name to be associated with resource.
  1351. *
  1352. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1353. * being reserved by owner @res_name. Do not access any
  1354. * address inside the PCI regions unless this call returns
  1355. * successfully.
  1356. *
  1357. * Returns 0 on success, or %EBUSY on error. A warning
  1358. * message is also printed on failure.
  1359. *
  1360. * The key difference that _exclusive makes it that userspace is
  1361. * explicitly not allowed to map the resource via /dev/mem or
  1362. * sysfs.
  1363. */
  1364. int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
  1365. {
  1366. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  1367. }
  1368. /**
  1369. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  1370. * @pdev: PCI device whose resources were previously reserved
  1371. * @bars: Bitmask of BARs to be released
  1372. *
  1373. * Release selected PCI I/O and memory resources previously reserved.
  1374. * Call this function only after all use of the PCI regions has ceased.
  1375. */
  1376. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  1377. {
  1378. int i;
  1379. for (i = 0; i < 6; i++)
  1380. if (bars & (1 << i))
  1381. pci_release_region(pdev, i);
  1382. }
  1383. int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1384. const char *res_name, int excl)
  1385. {
  1386. int i;
  1387. for (i = 0; i < 6; i++)
  1388. if (bars & (1 << i))
  1389. if (__pci_request_region(pdev, i, res_name, excl))
  1390. goto err_out;
  1391. return 0;
  1392. err_out:
  1393. while(--i >= 0)
  1394. if (bars & (1 << i))
  1395. pci_release_region(pdev, i);
  1396. return -EBUSY;
  1397. }
  1398. /**
  1399. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  1400. * @pdev: PCI device whose resources are to be reserved
  1401. * @bars: Bitmask of BARs to be requested
  1402. * @res_name: Name to be associated with resource
  1403. */
  1404. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1405. const char *res_name)
  1406. {
  1407. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  1408. }
  1409. int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
  1410. int bars, const char *res_name)
  1411. {
  1412. return __pci_request_selected_regions(pdev, bars, res_name,
  1413. IORESOURCE_EXCLUSIVE);
  1414. }
  1415. /**
  1416. * pci_release_regions - Release reserved PCI I/O and memory resources
  1417. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  1418. *
  1419. * Releases all PCI I/O and memory resources previously reserved by a
  1420. * successful call to pci_request_regions. Call this function only
  1421. * after all use of the PCI regions has ceased.
  1422. */
  1423. void pci_release_regions(struct pci_dev *pdev)
  1424. {
  1425. pci_release_selected_regions(pdev, (1 << 6) - 1);
  1426. }
  1427. /**
  1428. * pci_request_regions - Reserved PCI I/O and memory resources
  1429. * @pdev: PCI device whose resources are to be reserved
  1430. * @res_name: Name to be associated with resource.
  1431. *
  1432. * Mark all PCI regions associated with PCI device @pdev as
  1433. * being reserved by owner @res_name. Do not access any
  1434. * address inside the PCI regions unless this call returns
  1435. * successfully.
  1436. *
  1437. * Returns 0 on success, or %EBUSY on error. A warning
  1438. * message is also printed on failure.
  1439. */
  1440. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  1441. {
  1442. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  1443. }
  1444. /**
  1445. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  1446. * @pdev: PCI device whose resources are to be reserved
  1447. * @res_name: Name to be associated with resource.
  1448. *
  1449. * Mark all PCI regions associated with PCI device @pdev as
  1450. * being reserved by owner @res_name. Do not access any
  1451. * address inside the PCI regions unless this call returns
  1452. * successfully.
  1453. *
  1454. * pci_request_regions_exclusive() will mark the region so that
  1455. * /dev/mem and the sysfs MMIO access will not be allowed.
  1456. *
  1457. * Returns 0 on success, or %EBUSY on error. A warning
  1458. * message is also printed on failure.
  1459. */
  1460. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  1461. {
  1462. return pci_request_selected_regions_exclusive(pdev,
  1463. ((1 << 6) - 1), res_name);
  1464. }
  1465. static void __pci_set_master(struct pci_dev *dev, bool enable)
  1466. {
  1467. u16 old_cmd, cmd;
  1468. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  1469. if (enable)
  1470. cmd = old_cmd | PCI_COMMAND_MASTER;
  1471. else
  1472. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  1473. if (cmd != old_cmd) {
  1474. dev_dbg(&dev->dev, "%s bus mastering\n",
  1475. enable ? "enabling" : "disabling");
  1476. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1477. }
  1478. dev->is_busmaster = enable;
  1479. }
  1480. /**
  1481. * pci_set_master - enables bus-mastering for device dev
  1482. * @dev: the PCI device to enable
  1483. *
  1484. * Enables bus-mastering on the device and calls pcibios_set_master()
  1485. * to do the needed arch specific settings.
  1486. */
  1487. void pci_set_master(struct pci_dev *dev)
  1488. {
  1489. __pci_set_master(dev, true);
  1490. pcibios_set_master(dev);
  1491. }
  1492. /**
  1493. * pci_clear_master - disables bus-mastering for device dev
  1494. * @dev: the PCI device to disable
  1495. */
  1496. void pci_clear_master(struct pci_dev *dev)
  1497. {
  1498. __pci_set_master(dev, false);
  1499. }
  1500. #ifdef PCI_DISABLE_MWI
  1501. int pci_set_mwi(struct pci_dev *dev)
  1502. {
  1503. return 0;
  1504. }
  1505. int pci_try_set_mwi(struct pci_dev *dev)
  1506. {
  1507. return 0;
  1508. }
  1509. void pci_clear_mwi(struct pci_dev *dev)
  1510. {
  1511. }
  1512. #else
  1513. #ifndef PCI_CACHE_LINE_BYTES
  1514. #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES
  1515. #endif
  1516. /* This can be overridden by arch code. */
  1517. /* Don't forget this is measured in 32-bit words, not bytes */
  1518. u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4;
  1519. /**
  1520. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  1521. * @dev: the PCI device for which MWI is to be enabled
  1522. *
  1523. * Helper function for pci_set_mwi.
  1524. * Originally copied from drivers/net/acenic.c.
  1525. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  1526. *
  1527. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1528. */
  1529. static int
  1530. pci_set_cacheline_size(struct pci_dev *dev)
  1531. {
  1532. u8 cacheline_size;
  1533. if (!pci_cache_line_size)
  1534. return -EINVAL; /* The system doesn't support MWI. */
  1535. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  1536. equal to or multiple of the right value. */
  1537. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1538. if (cacheline_size >= pci_cache_line_size &&
  1539. (cacheline_size % pci_cache_line_size) == 0)
  1540. return 0;
  1541. /* Write the correct value. */
  1542. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  1543. /* Read it back. */
  1544. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1545. if (cacheline_size == pci_cache_line_size)
  1546. return 0;
  1547. dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
  1548. "supported\n", pci_cache_line_size << 2);
  1549. return -EINVAL;
  1550. }
  1551. /**
  1552. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  1553. * @dev: the PCI device for which MWI is enabled
  1554. *
  1555. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1556. *
  1557. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1558. */
  1559. int
  1560. pci_set_mwi(struct pci_dev *dev)
  1561. {
  1562. int rc;
  1563. u16 cmd;
  1564. rc = pci_set_cacheline_size(dev);
  1565. if (rc)
  1566. return rc;
  1567. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1568. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  1569. dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
  1570. cmd |= PCI_COMMAND_INVALIDATE;
  1571. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1572. }
  1573. return 0;
  1574. }
  1575. /**
  1576. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  1577. * @dev: the PCI device for which MWI is enabled
  1578. *
  1579. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1580. * Callers are not required to check the return value.
  1581. *
  1582. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1583. */
  1584. int pci_try_set_mwi(struct pci_dev *dev)
  1585. {
  1586. int rc = pci_set_mwi(dev);
  1587. return rc;
  1588. }
  1589. /**
  1590. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  1591. * @dev: the PCI device to disable
  1592. *
  1593. * Disables PCI Memory-Write-Invalidate transaction on the device
  1594. */
  1595. void
  1596. pci_clear_mwi(struct pci_dev *dev)
  1597. {
  1598. u16 cmd;
  1599. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1600. if (cmd & PCI_COMMAND_INVALIDATE) {
  1601. cmd &= ~PCI_COMMAND_INVALIDATE;
  1602. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1603. }
  1604. }
  1605. #endif /* ! PCI_DISABLE_MWI */
  1606. /**
  1607. * pci_intx - enables/disables PCI INTx for device dev
  1608. * @pdev: the PCI device to operate on
  1609. * @enable: boolean: whether to enable or disable PCI INTx
  1610. *
  1611. * Enables/disables PCI INTx for device dev
  1612. */
  1613. void
  1614. pci_intx(struct pci_dev *pdev, int enable)
  1615. {
  1616. u16 pci_command, new;
  1617. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  1618. if (enable) {
  1619. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  1620. } else {
  1621. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  1622. }
  1623. if (new != pci_command) {
  1624. struct pci_devres *dr;
  1625. pci_write_config_word(pdev, PCI_COMMAND, new);
  1626. dr = find_pci_dr(pdev);
  1627. if (dr && !dr->restore_intx) {
  1628. dr->restore_intx = 1;
  1629. dr->orig_intx = !enable;
  1630. }
  1631. }
  1632. }
  1633. /**
  1634. * pci_msi_off - disables any msi or msix capabilities
  1635. * @dev: the PCI device to operate on
  1636. *
  1637. * If you want to use msi see pci_enable_msi and friends.
  1638. * This is a lower level primitive that allows us to disable
  1639. * msi operation at the device level.
  1640. */
  1641. void pci_msi_off(struct pci_dev *dev)
  1642. {
  1643. int pos;
  1644. u16 control;
  1645. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1646. if (pos) {
  1647. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  1648. control &= ~PCI_MSI_FLAGS_ENABLE;
  1649. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  1650. }
  1651. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1652. if (pos) {
  1653. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  1654. control &= ~PCI_MSIX_FLAGS_ENABLE;
  1655. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  1656. }
  1657. }
  1658. #ifndef HAVE_ARCH_PCI_SET_DMA_MASK
  1659. /*
  1660. * These can be overridden by arch-specific implementations
  1661. */
  1662. int
  1663. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  1664. {
  1665. if (!pci_dma_supported(dev, mask))
  1666. return -EIO;
  1667. dev->dma_mask = mask;
  1668. return 0;
  1669. }
  1670. int
  1671. pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  1672. {
  1673. if (!pci_dma_supported(dev, mask))
  1674. return -EIO;
  1675. dev->dev.coherent_dma_mask = mask;
  1676. return 0;
  1677. }
  1678. #endif
  1679. #ifndef HAVE_ARCH_PCI_SET_DMA_MAX_SEGMENT_SIZE
  1680. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  1681. {
  1682. return dma_set_max_seg_size(&dev->dev, size);
  1683. }
  1684. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  1685. #endif
  1686. #ifndef HAVE_ARCH_PCI_SET_DMA_SEGMENT_BOUNDARY
  1687. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  1688. {
  1689. return dma_set_seg_boundary(&dev->dev, mask);
  1690. }
  1691. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  1692. #endif
  1693. static int __pcie_flr(struct pci_dev *dev, int probe)
  1694. {
  1695. u16 status;
  1696. u32 cap;
  1697. int exppos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1698. if (!exppos)
  1699. return -ENOTTY;
  1700. pci_read_config_dword(dev, exppos + PCI_EXP_DEVCAP, &cap);
  1701. if (!(cap & PCI_EXP_DEVCAP_FLR))
  1702. return -ENOTTY;
  1703. if (probe)
  1704. return 0;
  1705. pci_block_user_cfg_access(dev);
  1706. /* Wait for Transaction Pending bit clean */
  1707. msleep(100);
  1708. pci_read_config_word(dev, exppos + PCI_EXP_DEVSTA, &status);
  1709. if (status & PCI_EXP_DEVSTA_TRPND) {
  1710. dev_info(&dev->dev, "Busy after 100ms while trying to reset; "
  1711. "sleeping for 1 second\n");
  1712. ssleep(1);
  1713. pci_read_config_word(dev, exppos + PCI_EXP_DEVSTA, &status);
  1714. if (status & PCI_EXP_DEVSTA_TRPND)
  1715. dev_info(&dev->dev, "Still busy after 1s; "
  1716. "proceeding with reset anyway\n");
  1717. }
  1718. pci_write_config_word(dev, exppos + PCI_EXP_DEVCTL,
  1719. PCI_EXP_DEVCTL_BCR_FLR);
  1720. mdelay(100);
  1721. pci_unblock_user_cfg_access(dev);
  1722. return 0;
  1723. }
  1724. static int __pci_af_flr(struct pci_dev *dev, int probe)
  1725. {
  1726. int cappos = pci_find_capability(dev, PCI_CAP_ID_AF);
  1727. u8 status;
  1728. u8 cap;
  1729. if (!cappos)
  1730. return -ENOTTY;
  1731. pci_read_config_byte(dev, cappos + PCI_AF_CAP, &cap);
  1732. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  1733. return -ENOTTY;
  1734. if (probe)
  1735. return 0;
  1736. pci_block_user_cfg_access(dev);
  1737. /* Wait for Transaction Pending bit clean */
  1738. msleep(100);
  1739. pci_read_config_byte(dev, cappos + PCI_AF_STATUS, &status);
  1740. if (status & PCI_AF_STATUS_TP) {
  1741. dev_info(&dev->dev, "Busy after 100ms while trying to"
  1742. " reset; sleeping for 1 second\n");
  1743. ssleep(1);
  1744. pci_read_config_byte(dev,
  1745. cappos + PCI_AF_STATUS, &status);
  1746. if (status & PCI_AF_STATUS_TP)
  1747. dev_info(&dev->dev, "Still busy after 1s; "
  1748. "proceeding with reset anyway\n");
  1749. }
  1750. pci_write_config_byte(dev, cappos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  1751. mdelay(100);
  1752. pci_unblock_user_cfg_access(dev);
  1753. return 0;
  1754. }
  1755. static int __pci_reset_function(struct pci_dev *pdev, int probe)
  1756. {
  1757. int res;
  1758. res = __pcie_flr(pdev, probe);
  1759. if (res != -ENOTTY)
  1760. return res;
  1761. res = __pci_af_flr(pdev, probe);
  1762. if (res != -ENOTTY)
  1763. return res;
  1764. return res;
  1765. }
  1766. /**
  1767. * pci_execute_reset_function() - Reset a PCI device function
  1768. * @dev: Device function to reset
  1769. *
  1770. * Some devices allow an individual function to be reset without affecting
  1771. * other functions in the same device. The PCI device must be responsive
  1772. * to PCI config space in order to use this function.
  1773. *
  1774. * The device function is presumed to be unused when this function is called.
  1775. * Resetting the device will make the contents of PCI configuration space
  1776. * random, so any caller of this must be prepared to reinitialise the
  1777. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  1778. * etc.
  1779. *
  1780. * Returns 0 if the device function was successfully reset or -ENOTTY if the
  1781. * device doesn't support resetting a single function.
  1782. */
  1783. int pci_execute_reset_function(struct pci_dev *dev)
  1784. {
  1785. return __pci_reset_function(dev, 0);
  1786. }
  1787. EXPORT_SYMBOL_GPL(pci_execute_reset_function);
  1788. /**
  1789. * pci_reset_function() - quiesce and reset a PCI device function
  1790. * @dev: Device function to reset
  1791. *
  1792. * Some devices allow an individual function to be reset without affecting
  1793. * other functions in the same device. The PCI device must be responsive
  1794. * to PCI config space in order to use this function.
  1795. *
  1796. * This function does not just reset the PCI portion of a device, but
  1797. * clears all the state associated with the device. This function differs
  1798. * from pci_execute_reset_function in that it saves and restores device state
  1799. * over the reset.
  1800. *
  1801. * Returns 0 if the device function was successfully reset or -ENOTTY if the
  1802. * device doesn't support resetting a single function.
  1803. */
  1804. int pci_reset_function(struct pci_dev *dev)
  1805. {
  1806. int r = __pci_reset_function(dev, 1);
  1807. if (r < 0)
  1808. return r;
  1809. if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0)
  1810. disable_irq(dev->irq);
  1811. pci_save_state(dev);
  1812. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  1813. r = pci_execute_reset_function(dev);
  1814. pci_restore_state(dev);
  1815. if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0)
  1816. enable_irq(dev->irq);
  1817. return r;
  1818. }
  1819. EXPORT_SYMBOL_GPL(pci_reset_function);
  1820. /**
  1821. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  1822. * @dev: PCI device to query
  1823. *
  1824. * Returns mmrbc: maximum designed memory read count in bytes
  1825. * or appropriate error value.
  1826. */
  1827. int pcix_get_max_mmrbc(struct pci_dev *dev)
  1828. {
  1829. int err, cap;
  1830. u32 stat;
  1831. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1832. if (!cap)
  1833. return -EINVAL;
  1834. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1835. if (err)
  1836. return -EINVAL;
  1837. return (stat & PCI_X_STATUS_MAX_READ) >> 12;
  1838. }
  1839. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  1840. /**
  1841. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  1842. * @dev: PCI device to query
  1843. *
  1844. * Returns mmrbc: maximum memory read count in bytes
  1845. * or appropriate error value.
  1846. */
  1847. int pcix_get_mmrbc(struct pci_dev *dev)
  1848. {
  1849. int ret, cap;
  1850. u32 cmd;
  1851. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1852. if (!cap)
  1853. return -EINVAL;
  1854. ret = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1855. if (!ret)
  1856. ret = 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  1857. return ret;
  1858. }
  1859. EXPORT_SYMBOL(pcix_get_mmrbc);
  1860. /**
  1861. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  1862. * @dev: PCI device to query
  1863. * @mmrbc: maximum memory read count in bytes
  1864. * valid values are 512, 1024, 2048, 4096
  1865. *
  1866. * If possible sets maximum memory read byte count, some bridges have erratas
  1867. * that prevent this.
  1868. */
  1869. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  1870. {
  1871. int cap, err = -EINVAL;
  1872. u32 stat, cmd, v, o;
  1873. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  1874. goto out;
  1875. v = ffs(mmrbc) - 10;
  1876. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1877. if (!cap)
  1878. goto out;
  1879. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1880. if (err)
  1881. goto out;
  1882. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  1883. return -E2BIG;
  1884. err = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1885. if (err)
  1886. goto out;
  1887. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  1888. if (o != v) {
  1889. if (v > o && dev->bus &&
  1890. (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  1891. return -EIO;
  1892. cmd &= ~PCI_X_CMD_MAX_READ;
  1893. cmd |= v << 2;
  1894. err = pci_write_config_dword(dev, cap + PCI_X_CMD, cmd);
  1895. }
  1896. out:
  1897. return err;
  1898. }
  1899. EXPORT_SYMBOL(pcix_set_mmrbc);
  1900. /**
  1901. * pcie_get_readrq - get PCI Express read request size
  1902. * @dev: PCI device to query
  1903. *
  1904. * Returns maximum memory read request in bytes
  1905. * or appropriate error value.
  1906. */
  1907. int pcie_get_readrq(struct pci_dev *dev)
  1908. {
  1909. int ret, cap;
  1910. u16 ctl;
  1911. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1912. if (!cap)
  1913. return -EINVAL;
  1914. ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1915. if (!ret)
  1916. ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  1917. return ret;
  1918. }
  1919. EXPORT_SYMBOL(pcie_get_readrq);
  1920. /**
  1921. * pcie_set_readrq - set PCI Express maximum memory read request
  1922. * @dev: PCI device to query
  1923. * @rq: maximum memory read count in bytes
  1924. * valid values are 128, 256, 512, 1024, 2048, 4096
  1925. *
  1926. * If possible sets maximum read byte count
  1927. */
  1928. int pcie_set_readrq(struct pci_dev *dev, int rq)
  1929. {
  1930. int cap, err = -EINVAL;
  1931. u16 ctl, v;
  1932. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  1933. goto out;
  1934. v = (ffs(rq) - 8) << 12;
  1935. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1936. if (!cap)
  1937. goto out;
  1938. err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1939. if (err)
  1940. goto out;
  1941. if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
  1942. ctl &= ~PCI_EXP_DEVCTL_READRQ;
  1943. ctl |= v;
  1944. err = pci_write_config_dword(dev, cap + PCI_EXP_DEVCTL, ctl);
  1945. }
  1946. out:
  1947. return err;
  1948. }
  1949. EXPORT_SYMBOL(pcie_set_readrq);
  1950. /**
  1951. * pci_select_bars - Make BAR mask from the type of resource
  1952. * @dev: the PCI device for which BAR mask is made
  1953. * @flags: resource type mask to be selected
  1954. *
  1955. * This helper routine makes bar mask from the type of resource.
  1956. */
  1957. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  1958. {
  1959. int i, bars = 0;
  1960. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  1961. if (pci_resource_flags(dev, i) & flags)
  1962. bars |= (1 << i);
  1963. return bars;
  1964. }
  1965. /**
  1966. * pci_resource_bar - get position of the BAR associated with a resource
  1967. * @dev: the PCI device
  1968. * @resno: the resource number
  1969. * @type: the BAR type to be filled in
  1970. *
  1971. * Returns BAR position in config space, or 0 if the BAR is invalid.
  1972. */
  1973. int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
  1974. {
  1975. if (resno < PCI_ROM_RESOURCE) {
  1976. *type = pci_bar_unknown;
  1977. return PCI_BASE_ADDRESS_0 + 4 * resno;
  1978. } else if (resno == PCI_ROM_RESOURCE) {
  1979. *type = pci_bar_mem32;
  1980. return dev->rom_base_reg;
  1981. }
  1982. dev_err(&dev->dev, "BAR: invalid resource #%d\n", resno);
  1983. return 0;
  1984. }
  1985. static void __devinit pci_no_domains(void)
  1986. {
  1987. #ifdef CONFIG_PCI_DOMAINS
  1988. pci_domains_supported = 0;
  1989. #endif
  1990. }
  1991. /**
  1992. * pci_ext_cfg_enabled - can we access extended PCI config space?
  1993. * @dev: The PCI device of the root bridge.
  1994. *
  1995. * Returns 1 if we can access PCI extended config space (offsets
  1996. * greater than 0xff). This is the default implementation. Architecture
  1997. * implementations can override this.
  1998. */
  1999. int __attribute__ ((weak)) pci_ext_cfg_avail(struct pci_dev *dev)
  2000. {
  2001. return 1;
  2002. }
  2003. static int __devinit pci_init(void)
  2004. {
  2005. struct pci_dev *dev = NULL;
  2006. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  2007. pci_fixup_device(pci_fixup_final, dev);
  2008. }
  2009. return 0;
  2010. }
  2011. static int __init pci_setup(char *str)
  2012. {
  2013. while (str) {
  2014. char *k = strchr(str, ',');
  2015. if (k)
  2016. *k++ = 0;
  2017. if (*str && (str = pcibios_setup(str)) && *str) {
  2018. if (!strcmp(str, "nomsi")) {
  2019. pci_no_msi();
  2020. } else if (!strcmp(str, "noaer")) {
  2021. pci_no_aer();
  2022. } else if (!strcmp(str, "nodomains")) {
  2023. pci_no_domains();
  2024. } else if (!strncmp(str, "cbiosize=", 9)) {
  2025. pci_cardbus_io_size = memparse(str + 9, &str);
  2026. } else if (!strncmp(str, "cbmemsize=", 10)) {
  2027. pci_cardbus_mem_size = memparse(str + 10, &str);
  2028. } else {
  2029. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  2030. str);
  2031. }
  2032. }
  2033. str = k;
  2034. }
  2035. return 0;
  2036. }
  2037. early_param("pci", pci_setup);
  2038. device_initcall(pci_init);
  2039. EXPORT_SYMBOL(pci_reenable_device);
  2040. EXPORT_SYMBOL(pci_enable_device_io);
  2041. EXPORT_SYMBOL(pci_enable_device_mem);
  2042. EXPORT_SYMBOL(pci_enable_device);
  2043. EXPORT_SYMBOL(pcim_enable_device);
  2044. EXPORT_SYMBOL(pcim_pin_device);
  2045. EXPORT_SYMBOL(pci_disable_device);
  2046. EXPORT_SYMBOL(pci_find_capability);
  2047. EXPORT_SYMBOL(pci_bus_find_capability);
  2048. EXPORT_SYMBOL(pci_release_regions);
  2049. EXPORT_SYMBOL(pci_request_regions);
  2050. EXPORT_SYMBOL(pci_request_regions_exclusive);
  2051. EXPORT_SYMBOL(pci_release_region);
  2052. EXPORT_SYMBOL(pci_request_region);
  2053. EXPORT_SYMBOL(pci_request_region_exclusive);
  2054. EXPORT_SYMBOL(pci_release_selected_regions);
  2055. EXPORT_SYMBOL(pci_request_selected_regions);
  2056. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  2057. EXPORT_SYMBOL(pci_set_master);
  2058. EXPORT_SYMBOL(pci_clear_master);
  2059. EXPORT_SYMBOL(pci_set_mwi);
  2060. EXPORT_SYMBOL(pci_try_set_mwi);
  2061. EXPORT_SYMBOL(pci_clear_mwi);
  2062. EXPORT_SYMBOL_GPL(pci_intx);
  2063. EXPORT_SYMBOL(pci_set_dma_mask);
  2064. EXPORT_SYMBOL(pci_set_consistent_dma_mask);
  2065. EXPORT_SYMBOL(pci_assign_resource);
  2066. EXPORT_SYMBOL(pci_find_parent_resource);
  2067. EXPORT_SYMBOL(pci_select_bars);
  2068. EXPORT_SYMBOL(pci_set_power_state);
  2069. EXPORT_SYMBOL(pci_save_state);
  2070. EXPORT_SYMBOL(pci_restore_state);
  2071. EXPORT_SYMBOL(pci_pme_capable);
  2072. EXPORT_SYMBOL(pci_pme_active);
  2073. EXPORT_SYMBOL(pci_enable_wake);
  2074. EXPORT_SYMBOL(pci_wake_from_d3);
  2075. EXPORT_SYMBOL(pci_target_state);
  2076. EXPORT_SYMBOL(pci_prepare_to_sleep);
  2077. EXPORT_SYMBOL(pci_back_from_sleep);
  2078. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);