pci.c 68 KB

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