pci.c 73 KB

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