pci.c 67 KB

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