pci.c 70 KB

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