pci.c 76 KB

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