pci.c 67 KB

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