pci.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647
  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/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/string.h>
  18. #include <linux/log2.h>
  19. #include <linux/pci-aspm.h>
  20. #include <linux/pm_wakeup.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <asm/setup.h>
  25. #include "pci.h"
  26. const char *pci_power_names[] = {
  27. "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
  28. };
  29. EXPORT_SYMBOL_GPL(pci_power_names);
  30. int isa_dma_bridge_buggy;
  31. EXPORT_SYMBOL(isa_dma_bridge_buggy);
  32. int pci_pci_problems;
  33. EXPORT_SYMBOL(pci_pci_problems);
  34. unsigned int pci_pm_d3_delay;
  35. static void pci_pme_list_scan(struct work_struct *work);
  36. static LIST_HEAD(pci_pme_list);
  37. static DEFINE_MUTEX(pci_pme_list_mutex);
  38. static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
  39. struct pci_pme_device {
  40. struct list_head list;
  41. struct pci_dev *dev;
  42. };
  43. #define PME_TIMEOUT 1000 /* How long between PME checks */
  44. static void pci_dev_d3_sleep(struct pci_dev *dev)
  45. {
  46. unsigned int delay = dev->d3_delay;
  47. if (delay < pci_pm_d3_delay)
  48. delay = pci_pm_d3_delay;
  49. msleep(delay);
  50. }
  51. #ifdef CONFIG_PCI_DOMAINS
  52. int pci_domains_supported = 1;
  53. #endif
  54. #define DEFAULT_CARDBUS_IO_SIZE (256)
  55. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  56. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  57. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  58. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  59. #define DEFAULT_HOTPLUG_IO_SIZE (256)
  60. #define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
  61. /* pci=hpmemsize=nnM,hpiosize=nn can override this */
  62. unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
  63. unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
  64. enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_TUNE_OFF;
  65. /*
  66. * The default CLS is used if arch didn't set CLS explicitly and not
  67. * all pci devices agree on the same value. Arch can override either
  68. * the dfl or actual value as it sees fit. Don't forget this is
  69. * measured in 32-bit words, not bytes.
  70. */
  71. u8 pci_dfl_cache_line_size __devinitdata = L1_CACHE_BYTES >> 2;
  72. u8 pci_cache_line_size;
  73. /**
  74. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  75. * @bus: pointer to PCI bus structure to search
  76. *
  77. * Given a PCI bus, returns the highest PCI bus number present in the set
  78. * including the given PCI bus and its list of child PCI buses.
  79. */
  80. unsigned char pci_bus_max_busnr(struct pci_bus* bus)
  81. {
  82. struct list_head *tmp;
  83. unsigned char max, n;
  84. max = bus->subordinate;
  85. list_for_each(tmp, &bus->children) {
  86. n = pci_bus_max_busnr(pci_bus_b(tmp));
  87. if(n > max)
  88. max = n;
  89. }
  90. return max;
  91. }
  92. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  93. #ifdef CONFIG_HAS_IOMEM
  94. void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
  95. {
  96. /*
  97. * Make sure the BAR is actually a memory resource, not an IO resource
  98. */
  99. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  100. WARN_ON(1);
  101. return NULL;
  102. }
  103. return ioremap_nocache(pci_resource_start(pdev, bar),
  104. pci_resource_len(pdev, bar));
  105. }
  106. EXPORT_SYMBOL_GPL(pci_ioremap_bar);
  107. #endif
  108. #if 0
  109. /**
  110. * pci_max_busnr - returns maximum PCI bus number
  111. *
  112. * Returns the highest PCI bus number present in the system global list of
  113. * PCI buses.
  114. */
  115. unsigned char __devinit
  116. pci_max_busnr(void)
  117. {
  118. struct pci_bus *bus = NULL;
  119. unsigned char max, n;
  120. max = 0;
  121. while ((bus = pci_find_next_bus(bus)) != NULL) {
  122. n = pci_bus_max_busnr(bus);
  123. if(n > max)
  124. max = n;
  125. }
  126. return max;
  127. }
  128. #endif /* 0 */
  129. #define PCI_FIND_CAP_TTL 48
  130. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  131. u8 pos, int cap, int *ttl)
  132. {
  133. u8 id;
  134. while ((*ttl)--) {
  135. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  136. if (pos < 0x40)
  137. break;
  138. pos &= ~3;
  139. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  140. &id);
  141. if (id == 0xff)
  142. break;
  143. if (id == cap)
  144. return pos;
  145. pos += PCI_CAP_LIST_NEXT;
  146. }
  147. return 0;
  148. }
  149. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  150. u8 pos, int cap)
  151. {
  152. int ttl = PCI_FIND_CAP_TTL;
  153. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  154. }
  155. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  156. {
  157. return __pci_find_next_cap(dev->bus, dev->devfn,
  158. pos + PCI_CAP_LIST_NEXT, cap);
  159. }
  160. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  161. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  162. unsigned int devfn, u8 hdr_type)
  163. {
  164. u16 status;
  165. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  166. if (!(status & PCI_STATUS_CAP_LIST))
  167. return 0;
  168. switch (hdr_type) {
  169. case PCI_HEADER_TYPE_NORMAL:
  170. case PCI_HEADER_TYPE_BRIDGE:
  171. return PCI_CAPABILITY_LIST;
  172. case PCI_HEADER_TYPE_CARDBUS:
  173. return PCI_CB_CAPABILITY_LIST;
  174. default:
  175. return 0;
  176. }
  177. return 0;
  178. }
  179. /**
  180. * pci_find_capability - query for devices' capabilities
  181. * @dev: PCI device to query
  182. * @cap: capability code
  183. *
  184. * Tell if a device supports a given PCI capability.
  185. * Returns the address of the requested capability structure within the
  186. * device's PCI configuration space or 0 in case the device does not
  187. * support it. Possible values for @cap:
  188. *
  189. * %PCI_CAP_ID_PM Power Management
  190. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  191. * %PCI_CAP_ID_VPD Vital Product Data
  192. * %PCI_CAP_ID_SLOTID Slot Identification
  193. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  194. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  195. * %PCI_CAP_ID_PCIX PCI-X
  196. * %PCI_CAP_ID_EXP PCI Express
  197. */
  198. int pci_find_capability(struct pci_dev *dev, int cap)
  199. {
  200. int pos;
  201. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  202. if (pos)
  203. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  204. return pos;
  205. }
  206. /**
  207. * pci_bus_find_capability - query for devices' capabilities
  208. * @bus: the PCI bus to query
  209. * @devfn: PCI device to query
  210. * @cap: capability code
  211. *
  212. * Like pci_find_capability() but works for pci devices that do not have a
  213. * pci_dev structure set up yet.
  214. *
  215. * Returns the address of the requested capability structure within the
  216. * device's PCI configuration space or 0 in case the device does not
  217. * support it.
  218. */
  219. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  220. {
  221. int pos;
  222. u8 hdr_type;
  223. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  224. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  225. if (pos)
  226. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  227. return pos;
  228. }
  229. /**
  230. * pci_find_ext_capability - Find an extended capability
  231. * @dev: PCI device to query
  232. * @cap: capability code
  233. *
  234. * Returns the address of the requested extended capability structure
  235. * within the device's PCI configuration space or 0 if the device does
  236. * not support it. Possible values for @cap:
  237. *
  238. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  239. * %PCI_EXT_CAP_ID_VC Virtual Channel
  240. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  241. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  242. */
  243. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  244. {
  245. u32 header;
  246. int ttl;
  247. int pos = PCI_CFG_SPACE_SIZE;
  248. /* minimum 8 bytes per capability */
  249. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  250. if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
  251. return 0;
  252. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  253. return 0;
  254. /*
  255. * If we have no capabilities, this is indicated by cap ID,
  256. * cap version and next pointer all being 0.
  257. */
  258. if (header == 0)
  259. return 0;
  260. while (ttl-- > 0) {
  261. if (PCI_EXT_CAP_ID(header) == cap)
  262. return pos;
  263. pos = PCI_EXT_CAP_NEXT(header);
  264. if (pos < PCI_CFG_SPACE_SIZE)
  265. break;
  266. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  267. break;
  268. }
  269. return 0;
  270. }
  271. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  272. /**
  273. * pci_bus_find_ext_capability - find an extended capability
  274. * @bus: the PCI bus to query
  275. * @devfn: PCI device to query
  276. * @cap: capability code
  277. *
  278. * Like pci_find_ext_capability() but works for pci devices that do not have a
  279. * pci_dev structure set up yet.
  280. *
  281. * Returns the address of the requested capability structure within the
  282. * device's PCI configuration space or 0 in case the device does not
  283. * support it.
  284. */
  285. int pci_bus_find_ext_capability(struct pci_bus *bus, unsigned int devfn,
  286. int cap)
  287. {
  288. u32 header;
  289. int ttl;
  290. int pos = PCI_CFG_SPACE_SIZE;
  291. /* minimum 8 bytes per capability */
  292. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  293. if (!pci_bus_read_config_dword(bus, devfn, pos, &header))
  294. return 0;
  295. if (header == 0xffffffff || header == 0)
  296. return 0;
  297. while (ttl-- > 0) {
  298. if (PCI_EXT_CAP_ID(header) == cap)
  299. return pos;
  300. pos = PCI_EXT_CAP_NEXT(header);
  301. if (pos < PCI_CFG_SPACE_SIZE)
  302. break;
  303. if (!pci_bus_read_config_dword(bus, devfn, pos, &header))
  304. break;
  305. }
  306. return 0;
  307. }
  308. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  309. {
  310. int rc, ttl = PCI_FIND_CAP_TTL;
  311. u8 cap, mask;
  312. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  313. mask = HT_3BIT_CAP_MASK;
  314. else
  315. mask = HT_5BIT_CAP_MASK;
  316. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  317. PCI_CAP_ID_HT, &ttl);
  318. while (pos) {
  319. rc = pci_read_config_byte(dev, pos + 3, &cap);
  320. if (rc != PCIBIOS_SUCCESSFUL)
  321. return 0;
  322. if ((cap & mask) == ht_cap)
  323. return pos;
  324. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  325. pos + PCI_CAP_LIST_NEXT,
  326. PCI_CAP_ID_HT, &ttl);
  327. }
  328. return 0;
  329. }
  330. /**
  331. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  332. * @dev: PCI device to query
  333. * @pos: Position from which to continue searching
  334. * @ht_cap: Hypertransport capability code
  335. *
  336. * To be used in conjunction with pci_find_ht_capability() to search for
  337. * all capabilities matching @ht_cap. @pos should always be a value returned
  338. * from pci_find_ht_capability().
  339. *
  340. * NB. To be 100% safe against broken PCI devices, the caller should take
  341. * steps to avoid an infinite loop.
  342. */
  343. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  344. {
  345. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  346. }
  347. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  348. /**
  349. * pci_find_ht_capability - query a device's Hypertransport capabilities
  350. * @dev: PCI device to query
  351. * @ht_cap: Hypertransport capability code
  352. *
  353. * Tell if a device supports a given Hypertransport capability.
  354. * Returns an address within the device's PCI configuration space
  355. * or 0 in case the device does not support the request capability.
  356. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  357. * which has a Hypertransport capability matching @ht_cap.
  358. */
  359. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  360. {
  361. int pos;
  362. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  363. if (pos)
  364. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  365. return pos;
  366. }
  367. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  368. /**
  369. * pci_find_parent_resource - return resource region of parent bus of given region
  370. * @dev: PCI device structure contains resources to be searched
  371. * @res: child resource record for which parent is sought
  372. *
  373. * For given resource region of given device, return the resource
  374. * region of parent bus the given region is contained in or where
  375. * it should be allocated from.
  376. */
  377. struct resource *
  378. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  379. {
  380. const struct pci_bus *bus = dev->bus;
  381. int i;
  382. struct resource *best = NULL, *r;
  383. pci_bus_for_each_resource(bus, r, i) {
  384. if (!r)
  385. continue;
  386. if (res->start && !(res->start >= r->start && res->end <= r->end))
  387. continue; /* Not contained */
  388. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  389. continue; /* Wrong type */
  390. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  391. return r; /* Exact match */
  392. /* We can't insert a non-prefetch resource inside a prefetchable parent .. */
  393. if (r->flags & IORESOURCE_PREFETCH)
  394. continue;
  395. /* .. but we can put a prefetchable resource inside a non-prefetchable one */
  396. if (!best)
  397. best = r;
  398. }
  399. return best;
  400. }
  401. /**
  402. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  403. * @dev: PCI device to have its BARs restored
  404. *
  405. * Restore the BAR values for a given device, so as to make it
  406. * accessible by its driver.
  407. */
  408. static void
  409. pci_restore_bars(struct pci_dev *dev)
  410. {
  411. int i;
  412. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
  413. pci_update_resource(dev, i);
  414. }
  415. static struct pci_platform_pm_ops *pci_platform_pm;
  416. int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
  417. {
  418. if (!ops->is_manageable || !ops->set_state || !ops->choose_state
  419. || !ops->sleep_wake || !ops->can_wakeup)
  420. return -EINVAL;
  421. pci_platform_pm = ops;
  422. return 0;
  423. }
  424. static inline bool platform_pci_power_manageable(struct pci_dev *dev)
  425. {
  426. return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
  427. }
  428. static inline int platform_pci_set_power_state(struct pci_dev *dev,
  429. pci_power_t t)
  430. {
  431. return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
  432. }
  433. static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
  434. {
  435. return pci_platform_pm ?
  436. pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
  437. }
  438. static inline bool platform_pci_can_wakeup(struct pci_dev *dev)
  439. {
  440. return pci_platform_pm ? pci_platform_pm->can_wakeup(dev) : false;
  441. }
  442. static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
  443. {
  444. return pci_platform_pm ?
  445. pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
  446. }
  447. static inline int platform_pci_run_wake(struct pci_dev *dev, bool enable)
  448. {
  449. return pci_platform_pm ?
  450. pci_platform_pm->run_wake(dev, enable) : -ENODEV;
  451. }
  452. /**
  453. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  454. * given PCI device
  455. * @dev: PCI device to handle.
  456. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  457. *
  458. * RETURN VALUE:
  459. * -EINVAL if the requested state is invalid.
  460. * -EIO if device does not support PCI PM or its PM capabilities register has a
  461. * wrong version, or device doesn't support the requested state.
  462. * 0 if device already is in the requested state.
  463. * 0 if device's power state has been successfully changed.
  464. */
  465. static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
  466. {
  467. u16 pmcsr;
  468. bool need_restore = false;
  469. /* Check if we're already there */
  470. if (dev->current_state == state)
  471. return 0;
  472. if (!dev->pm_cap)
  473. return -EIO;
  474. if (state < PCI_D0 || state > PCI_D3hot)
  475. return -EINVAL;
  476. /* Validate current state:
  477. * Can enter D0 from any state, but if we can only go deeper
  478. * to sleep if we're already in a low power state
  479. */
  480. if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  481. && dev->current_state > state) {
  482. dev_err(&dev->dev, "invalid power transition "
  483. "(from state %d to %d)\n", dev->current_state, state);
  484. return -EINVAL;
  485. }
  486. /* check if this device supports the desired state */
  487. if ((state == PCI_D1 && !dev->d1_support)
  488. || (state == PCI_D2 && !dev->d2_support))
  489. return -EIO;
  490. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  491. /* If we're (effectively) in D3, force entire word to 0.
  492. * This doesn't affect PME_Status, disables PME_En, and
  493. * sets PowerState to 0.
  494. */
  495. switch (dev->current_state) {
  496. case PCI_D0:
  497. case PCI_D1:
  498. case PCI_D2:
  499. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  500. pmcsr |= state;
  501. break;
  502. case PCI_D3hot:
  503. case PCI_D3cold:
  504. case PCI_UNKNOWN: /* Boot-up */
  505. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  506. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  507. need_restore = true;
  508. /* Fall-through: force to D0 */
  509. default:
  510. pmcsr = 0;
  511. break;
  512. }
  513. /* enter specified state */
  514. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  515. /* Mandatory power management transition delays */
  516. /* see PCI PM 1.1 5.6.1 table 18 */
  517. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  518. pci_dev_d3_sleep(dev);
  519. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  520. udelay(PCI_PM_D2_DELAY);
  521. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  522. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  523. if (dev->current_state != state && printk_ratelimit())
  524. dev_info(&dev->dev, "Refused to change power state, "
  525. "currently in D%d\n", dev->current_state);
  526. /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  527. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  528. * from D3hot to D0 _may_ perform an internal reset, thereby
  529. * going to "D0 Uninitialized" rather than "D0 Initialized".
  530. * For example, at least some versions of the 3c905B and the
  531. * 3c556B exhibit this behaviour.
  532. *
  533. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  534. * devices in a D3hot state at boot. Consequently, we need to
  535. * restore at least the BARs so that the device will be
  536. * accessible to its driver.
  537. */
  538. if (need_restore)
  539. pci_restore_bars(dev);
  540. if (dev->bus->self)
  541. pcie_aspm_pm_state_change(dev->bus->self);
  542. return 0;
  543. }
  544. /**
  545. * pci_update_current_state - Read PCI power state of given device from its
  546. * PCI PM registers and cache it
  547. * @dev: PCI device to handle.
  548. * @state: State to cache in case the device doesn't have the PM capability
  549. */
  550. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  551. {
  552. if (dev->pm_cap) {
  553. u16 pmcsr;
  554. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  555. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  556. } else {
  557. dev->current_state = state;
  558. }
  559. }
  560. /**
  561. * pci_platform_power_transition - Use platform to change device power state
  562. * @dev: PCI device to handle.
  563. * @state: State to put the device into.
  564. */
  565. static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
  566. {
  567. int error;
  568. if (platform_pci_power_manageable(dev)) {
  569. error = platform_pci_set_power_state(dev, state);
  570. if (!error)
  571. pci_update_current_state(dev, state);
  572. /* Fall back to PCI_D0 if native PM is not supported */
  573. if (!dev->pm_cap)
  574. dev->current_state = PCI_D0;
  575. } else {
  576. error = -ENODEV;
  577. /* Fall back to PCI_D0 if native PM is not supported */
  578. if (!dev->pm_cap)
  579. dev->current_state = PCI_D0;
  580. }
  581. return error;
  582. }
  583. /**
  584. * __pci_start_power_transition - Start power transition of a PCI device
  585. * @dev: PCI device to handle.
  586. * @state: State to put the device into.
  587. */
  588. static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
  589. {
  590. if (state == PCI_D0)
  591. pci_platform_power_transition(dev, PCI_D0);
  592. }
  593. /**
  594. * __pci_complete_power_transition - Complete power transition of a PCI device
  595. * @dev: PCI device to handle.
  596. * @state: State to put the device into.
  597. *
  598. * This function should not be called directly by device drivers.
  599. */
  600. int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
  601. {
  602. return state >= PCI_D0 ?
  603. pci_platform_power_transition(dev, state) : -EINVAL;
  604. }
  605. EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
  606. /**
  607. * pci_set_power_state - Set the power state of a PCI device
  608. * @dev: PCI device to handle.
  609. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  610. *
  611. * Transition a device to a new power state, using the platform firmware and/or
  612. * the device's PCI PM registers.
  613. *
  614. * RETURN VALUE:
  615. * -EINVAL if the requested state is invalid.
  616. * -EIO if device does not support PCI PM or its PM capabilities register has a
  617. * wrong version, or device doesn't support the requested state.
  618. * 0 if device already is in the requested state.
  619. * 0 if device's power state has been successfully changed.
  620. */
  621. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  622. {
  623. int error;
  624. /* bound the state we're entering */
  625. if (state > PCI_D3hot)
  626. state = PCI_D3hot;
  627. else if (state < PCI_D0)
  628. state = PCI_D0;
  629. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  630. /*
  631. * If the device or the parent bridge do not support PCI PM,
  632. * ignore the request if we're doing anything other than putting
  633. * it into D0 (which would only happen on boot).
  634. */
  635. return 0;
  636. __pci_start_power_transition(dev, state);
  637. /* This device is quirked not to be put into D3, so
  638. don't put it in D3 */
  639. if (state == PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  640. return 0;
  641. error = pci_raw_set_power_state(dev, state);
  642. if (!__pci_complete_power_transition(dev, state))
  643. error = 0;
  644. /*
  645. * When aspm_policy is "powersave" this call ensures
  646. * that ASPM is configured.
  647. */
  648. if (!error && dev->bus->self)
  649. pcie_aspm_powersave_config_link(dev->bus->self);
  650. return error;
  651. }
  652. /**
  653. * pci_choose_state - Choose the power state of a PCI device
  654. * @dev: PCI device to be suspended
  655. * @state: target sleep state for the whole system. This is the value
  656. * that is passed to suspend() function.
  657. *
  658. * Returns PCI power state suitable for given device and given system
  659. * message.
  660. */
  661. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  662. {
  663. pci_power_t ret;
  664. if (!pci_find_capability(dev, PCI_CAP_ID_PM))
  665. return PCI_D0;
  666. ret = platform_pci_choose_state(dev);
  667. if (ret != PCI_POWER_ERROR)
  668. return ret;
  669. switch (state.event) {
  670. case PM_EVENT_ON:
  671. return PCI_D0;
  672. case PM_EVENT_FREEZE:
  673. case PM_EVENT_PRETHAW:
  674. /* REVISIT both freeze and pre-thaw "should" use D0 */
  675. case PM_EVENT_SUSPEND:
  676. case PM_EVENT_HIBERNATE:
  677. return PCI_D3hot;
  678. default:
  679. dev_info(&dev->dev, "unrecognized suspend event %d\n",
  680. state.event);
  681. BUG();
  682. }
  683. return PCI_D0;
  684. }
  685. EXPORT_SYMBOL(pci_choose_state);
  686. #define PCI_EXP_SAVE_REGS 7
  687. #define pcie_cap_has_devctl(type, flags) 1
  688. #define pcie_cap_has_lnkctl(type, flags) \
  689. ((flags & PCI_EXP_FLAGS_VERS) > 1 || \
  690. (type == PCI_EXP_TYPE_ROOT_PORT || \
  691. type == PCI_EXP_TYPE_ENDPOINT || \
  692. type == PCI_EXP_TYPE_LEG_END))
  693. #define pcie_cap_has_sltctl(type, flags) \
  694. ((flags & PCI_EXP_FLAGS_VERS) > 1 || \
  695. ((type == PCI_EXP_TYPE_ROOT_PORT) || \
  696. (type == PCI_EXP_TYPE_DOWNSTREAM && \
  697. (flags & PCI_EXP_FLAGS_SLOT))))
  698. #define pcie_cap_has_rtctl(type, flags) \
  699. ((flags & PCI_EXP_FLAGS_VERS) > 1 || \
  700. (type == PCI_EXP_TYPE_ROOT_PORT || \
  701. type == PCI_EXP_TYPE_RC_EC))
  702. #define pcie_cap_has_devctl2(type, flags) \
  703. ((flags & PCI_EXP_FLAGS_VERS) > 1)
  704. #define pcie_cap_has_lnkctl2(type, flags) \
  705. ((flags & PCI_EXP_FLAGS_VERS) > 1)
  706. #define pcie_cap_has_sltctl2(type, flags) \
  707. ((flags & PCI_EXP_FLAGS_VERS) > 1)
  708. static int pci_save_pcie_state(struct pci_dev *dev)
  709. {
  710. int pos, i = 0;
  711. struct pci_cap_saved_state *save_state;
  712. u16 *cap;
  713. u16 flags;
  714. pos = pci_pcie_cap(dev);
  715. if (!pos)
  716. return 0;
  717. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  718. if (!save_state) {
  719. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  720. return -ENOMEM;
  721. }
  722. cap = (u16 *)&save_state->cap.data[0];
  723. pci_read_config_word(dev, pos + PCI_EXP_FLAGS, &flags);
  724. if (pcie_cap_has_devctl(dev->pcie_type, flags))
  725. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]);
  726. if (pcie_cap_has_lnkctl(dev->pcie_type, flags))
  727. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
  728. if (pcie_cap_has_sltctl(dev->pcie_type, flags))
  729. pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
  730. if (pcie_cap_has_rtctl(dev->pcie_type, flags))
  731. pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
  732. if (pcie_cap_has_devctl2(dev->pcie_type, flags))
  733. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &cap[i++]);
  734. if (pcie_cap_has_lnkctl2(dev->pcie_type, flags))
  735. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL2, &cap[i++]);
  736. if (pcie_cap_has_sltctl2(dev->pcie_type, flags))
  737. pci_read_config_word(dev, pos + PCI_EXP_SLTCTL2, &cap[i++]);
  738. return 0;
  739. }
  740. static void pci_restore_pcie_state(struct pci_dev *dev)
  741. {
  742. int i = 0, pos;
  743. struct pci_cap_saved_state *save_state;
  744. u16 *cap;
  745. u16 flags;
  746. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  747. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  748. if (!save_state || pos <= 0)
  749. return;
  750. cap = (u16 *)&save_state->cap.data[0];
  751. pci_read_config_word(dev, pos + PCI_EXP_FLAGS, &flags);
  752. if (pcie_cap_has_devctl(dev->pcie_type, flags))
  753. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]);
  754. if (pcie_cap_has_lnkctl(dev->pcie_type, flags))
  755. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]);
  756. if (pcie_cap_has_sltctl(dev->pcie_type, flags))
  757. pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]);
  758. if (pcie_cap_has_rtctl(dev->pcie_type, flags))
  759. pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);
  760. if (pcie_cap_has_devctl2(dev->pcie_type, flags))
  761. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, cap[i++]);
  762. if (pcie_cap_has_lnkctl2(dev->pcie_type, flags))
  763. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL2, cap[i++]);
  764. if (pcie_cap_has_sltctl2(dev->pcie_type, flags))
  765. pci_write_config_word(dev, pos + PCI_EXP_SLTCTL2, cap[i++]);
  766. }
  767. static int pci_save_pcix_state(struct pci_dev *dev)
  768. {
  769. int pos;
  770. struct pci_cap_saved_state *save_state;
  771. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  772. if (pos <= 0)
  773. return 0;
  774. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  775. if (!save_state) {
  776. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  777. return -ENOMEM;
  778. }
  779. pci_read_config_word(dev, pos + PCI_X_CMD,
  780. (u16 *)save_state->cap.data);
  781. return 0;
  782. }
  783. static void pci_restore_pcix_state(struct pci_dev *dev)
  784. {
  785. int i = 0, pos;
  786. struct pci_cap_saved_state *save_state;
  787. u16 *cap;
  788. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  789. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  790. if (!save_state || pos <= 0)
  791. return;
  792. cap = (u16 *)&save_state->cap.data[0];
  793. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  794. }
  795. /**
  796. * pci_save_state - save the PCI configuration space of a device before suspending
  797. * @dev: - PCI device that we're dealing with
  798. */
  799. int
  800. pci_save_state(struct pci_dev *dev)
  801. {
  802. int i;
  803. /* XXX: 100% dword access ok here? */
  804. for (i = 0; i < 16; i++)
  805. pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
  806. dev->state_saved = true;
  807. if ((i = pci_save_pcie_state(dev)) != 0)
  808. return i;
  809. if ((i = pci_save_pcix_state(dev)) != 0)
  810. return i;
  811. return 0;
  812. }
  813. /**
  814. * pci_restore_state - Restore the saved state of a PCI device
  815. * @dev: - PCI device that we're dealing with
  816. */
  817. void pci_restore_state(struct pci_dev *dev)
  818. {
  819. int i;
  820. u32 val;
  821. if (!dev->state_saved)
  822. return;
  823. /* PCI Express register must be restored first */
  824. pci_restore_pcie_state(dev);
  825. /*
  826. * The Base Address register should be programmed before the command
  827. * register(s)
  828. */
  829. for (i = 15; i >= 0; i--) {
  830. pci_read_config_dword(dev, i * 4, &val);
  831. if (val != dev->saved_config_space[i]) {
  832. dev_printk(KERN_DEBUG, &dev->dev, "restoring config "
  833. "space at offset %#x (was %#x, writing %#x)\n",
  834. i, val, (int)dev->saved_config_space[i]);
  835. pci_write_config_dword(dev,i * 4,
  836. dev->saved_config_space[i]);
  837. }
  838. }
  839. pci_restore_pcix_state(dev);
  840. pci_restore_msi_state(dev);
  841. pci_restore_iov_state(dev);
  842. dev->state_saved = false;
  843. }
  844. struct pci_saved_state {
  845. u32 config_space[16];
  846. struct pci_cap_saved_data cap[0];
  847. };
  848. /**
  849. * pci_store_saved_state - Allocate and return an opaque struct containing
  850. * the device saved state.
  851. * @dev: PCI device that we're dealing with
  852. *
  853. * Rerturn NULL if no state or error.
  854. */
  855. struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
  856. {
  857. struct pci_saved_state *state;
  858. struct pci_cap_saved_state *tmp;
  859. struct pci_cap_saved_data *cap;
  860. struct hlist_node *pos;
  861. size_t size;
  862. if (!dev->state_saved)
  863. return NULL;
  864. size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
  865. hlist_for_each_entry(tmp, pos, &dev->saved_cap_space, next)
  866. size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  867. state = kzalloc(size, GFP_KERNEL);
  868. if (!state)
  869. return NULL;
  870. memcpy(state->config_space, dev->saved_config_space,
  871. sizeof(state->config_space));
  872. cap = state->cap;
  873. hlist_for_each_entry(tmp, pos, &dev->saved_cap_space, next) {
  874. size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  875. memcpy(cap, &tmp->cap, len);
  876. cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
  877. }
  878. /* Empty cap_save terminates list */
  879. return state;
  880. }
  881. EXPORT_SYMBOL_GPL(pci_store_saved_state);
  882. /**
  883. * pci_load_saved_state - Reload the provided save state into struct pci_dev.
  884. * @dev: PCI device that we're dealing with
  885. * @state: Saved state returned from pci_store_saved_state()
  886. */
  887. int pci_load_saved_state(struct pci_dev *dev, struct pci_saved_state *state)
  888. {
  889. struct pci_cap_saved_data *cap;
  890. dev->state_saved = false;
  891. if (!state)
  892. return 0;
  893. memcpy(dev->saved_config_space, state->config_space,
  894. sizeof(state->config_space));
  895. cap = state->cap;
  896. while (cap->size) {
  897. struct pci_cap_saved_state *tmp;
  898. tmp = pci_find_saved_cap(dev, cap->cap_nr);
  899. if (!tmp || tmp->cap.size != cap->size)
  900. return -EINVAL;
  901. memcpy(tmp->cap.data, cap->data, tmp->cap.size);
  902. cap = (struct pci_cap_saved_data *)((u8 *)cap +
  903. sizeof(struct pci_cap_saved_data) + cap->size);
  904. }
  905. dev->state_saved = true;
  906. return 0;
  907. }
  908. EXPORT_SYMBOL_GPL(pci_load_saved_state);
  909. /**
  910. * pci_load_and_free_saved_state - Reload the save state pointed to by state,
  911. * and free the memory allocated for it.
  912. * @dev: PCI device that we're dealing with
  913. * @state: Pointer to saved state returned from pci_store_saved_state()
  914. */
  915. int pci_load_and_free_saved_state(struct pci_dev *dev,
  916. struct pci_saved_state **state)
  917. {
  918. int ret = pci_load_saved_state(dev, *state);
  919. kfree(*state);
  920. *state = NULL;
  921. return ret;
  922. }
  923. EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
  924. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  925. {
  926. int err;
  927. err = pci_set_power_state(dev, PCI_D0);
  928. if (err < 0 && err != -EIO)
  929. return err;
  930. err = pcibios_enable_device(dev, bars);
  931. if (err < 0)
  932. return err;
  933. pci_fixup_device(pci_fixup_enable, dev);
  934. return 0;
  935. }
  936. /**
  937. * pci_reenable_device - Resume abandoned device
  938. * @dev: PCI device to be resumed
  939. *
  940. * Note this function is a backend of pci_default_resume and is not supposed
  941. * to be called by normal code, write proper resume handler and use it instead.
  942. */
  943. int pci_reenable_device(struct pci_dev *dev)
  944. {
  945. if (pci_is_enabled(dev))
  946. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  947. return 0;
  948. }
  949. static int __pci_enable_device_flags(struct pci_dev *dev,
  950. resource_size_t flags)
  951. {
  952. int err;
  953. int i, bars = 0;
  954. /*
  955. * Power state could be unknown at this point, either due to a fresh
  956. * boot or a device removal call. So get the current power state
  957. * so that things like MSI message writing will behave as expected
  958. * (e.g. if the device really is in D0 at enable time).
  959. */
  960. if (dev->pm_cap) {
  961. u16 pmcsr;
  962. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  963. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  964. }
  965. if (atomic_add_return(1, &dev->enable_cnt) > 1)
  966. return 0; /* already enabled */
  967. /* only skip sriov related */
  968. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  969. if (dev->resource[i].flags & flags)
  970. bars |= (1 << i);
  971. for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
  972. if (dev->resource[i].flags & flags)
  973. bars |= (1 << i);
  974. err = do_pci_enable_device(dev, bars);
  975. if (err < 0)
  976. atomic_dec(&dev->enable_cnt);
  977. return err;
  978. }
  979. /**
  980. * pci_enable_device_io - Initialize a device for use with IO space
  981. * @dev: PCI device to be initialized
  982. *
  983. * Initialize device before it's used by a driver. Ask low-level code
  984. * to enable I/O resources. Wake up the device if it was suspended.
  985. * Beware, this function can fail.
  986. */
  987. int pci_enable_device_io(struct pci_dev *dev)
  988. {
  989. return __pci_enable_device_flags(dev, IORESOURCE_IO);
  990. }
  991. /**
  992. * pci_enable_device_mem - Initialize a device for use with Memory space
  993. * @dev: PCI device to be initialized
  994. *
  995. * Initialize device before it's used by a driver. Ask low-level code
  996. * to enable Memory resources. Wake up the device if it was suspended.
  997. * Beware, this function can fail.
  998. */
  999. int pci_enable_device_mem(struct pci_dev *dev)
  1000. {
  1001. return __pci_enable_device_flags(dev, IORESOURCE_MEM);
  1002. }
  1003. /**
  1004. * pci_enable_device - Initialize device before it's used by a driver.
  1005. * @dev: PCI device to be initialized
  1006. *
  1007. * Initialize device before it's used by a driver. Ask low-level code
  1008. * to enable I/O and memory. Wake up the device if it was suspended.
  1009. * Beware, this function can fail.
  1010. *
  1011. * Note we don't actually enable the device many times if we call
  1012. * this function repeatedly (we just increment the count).
  1013. */
  1014. int pci_enable_device(struct pci_dev *dev)
  1015. {
  1016. return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  1017. }
  1018. /*
  1019. * Managed PCI resources. This manages device on/off, intx/msi/msix
  1020. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  1021. * there's no need to track it separately. pci_devres is initialized
  1022. * when a device is enabled using managed PCI device enable interface.
  1023. */
  1024. struct pci_devres {
  1025. unsigned int enabled:1;
  1026. unsigned int pinned:1;
  1027. unsigned int orig_intx:1;
  1028. unsigned int restore_intx:1;
  1029. u32 region_mask;
  1030. };
  1031. static void pcim_release(struct device *gendev, void *res)
  1032. {
  1033. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  1034. struct pci_devres *this = res;
  1035. int i;
  1036. if (dev->msi_enabled)
  1037. pci_disable_msi(dev);
  1038. if (dev->msix_enabled)
  1039. pci_disable_msix(dev);
  1040. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  1041. if (this->region_mask & (1 << i))
  1042. pci_release_region(dev, i);
  1043. if (this->restore_intx)
  1044. pci_intx(dev, this->orig_intx);
  1045. if (this->enabled && !this->pinned)
  1046. pci_disable_device(dev);
  1047. }
  1048. static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
  1049. {
  1050. struct pci_devres *dr, *new_dr;
  1051. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1052. if (dr)
  1053. return dr;
  1054. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  1055. if (!new_dr)
  1056. return NULL;
  1057. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  1058. }
  1059. static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
  1060. {
  1061. if (pci_is_managed(pdev))
  1062. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1063. return NULL;
  1064. }
  1065. /**
  1066. * pcim_enable_device - Managed pci_enable_device()
  1067. * @pdev: PCI device to be initialized
  1068. *
  1069. * Managed pci_enable_device().
  1070. */
  1071. int pcim_enable_device(struct pci_dev *pdev)
  1072. {
  1073. struct pci_devres *dr;
  1074. int rc;
  1075. dr = get_pci_dr(pdev);
  1076. if (unlikely(!dr))
  1077. return -ENOMEM;
  1078. if (dr->enabled)
  1079. return 0;
  1080. rc = pci_enable_device(pdev);
  1081. if (!rc) {
  1082. pdev->is_managed = 1;
  1083. dr->enabled = 1;
  1084. }
  1085. return rc;
  1086. }
  1087. /**
  1088. * pcim_pin_device - Pin managed PCI device
  1089. * @pdev: PCI device to pin
  1090. *
  1091. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  1092. * driver detach. @pdev must have been enabled with
  1093. * pcim_enable_device().
  1094. */
  1095. void pcim_pin_device(struct pci_dev *pdev)
  1096. {
  1097. struct pci_devres *dr;
  1098. dr = find_pci_dr(pdev);
  1099. WARN_ON(!dr || !dr->enabled);
  1100. if (dr)
  1101. dr->pinned = 1;
  1102. }
  1103. /**
  1104. * pcibios_disable_device - disable arch specific PCI resources for device dev
  1105. * @dev: the PCI device to disable
  1106. *
  1107. * Disables architecture specific PCI resources for the device. This
  1108. * is the default implementation. Architecture implementations can
  1109. * override this.
  1110. */
  1111. void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
  1112. static void do_pci_disable_device(struct pci_dev *dev)
  1113. {
  1114. u16 pci_command;
  1115. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  1116. if (pci_command & PCI_COMMAND_MASTER) {
  1117. pci_command &= ~PCI_COMMAND_MASTER;
  1118. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  1119. }
  1120. pcibios_disable_device(dev);
  1121. }
  1122. /**
  1123. * pci_disable_enabled_device - Disable device without updating enable_cnt
  1124. * @dev: PCI device to disable
  1125. *
  1126. * NOTE: This function is a backend of PCI power management routines and is
  1127. * not supposed to be called drivers.
  1128. */
  1129. void pci_disable_enabled_device(struct pci_dev *dev)
  1130. {
  1131. if (pci_is_enabled(dev))
  1132. do_pci_disable_device(dev);
  1133. }
  1134. /**
  1135. * pci_disable_device - Disable PCI device after use
  1136. * @dev: PCI device to be disabled
  1137. *
  1138. * Signal to the system that the PCI device is not in use by the system
  1139. * anymore. This only involves disabling PCI bus-mastering, if active.
  1140. *
  1141. * Note we don't actually disable the device until all callers of
  1142. * pci_enable_device() have called pci_disable_device().
  1143. */
  1144. void
  1145. pci_disable_device(struct pci_dev *dev)
  1146. {
  1147. struct pci_devres *dr;
  1148. dr = find_pci_dr(dev);
  1149. if (dr)
  1150. dr->enabled = 0;
  1151. if (atomic_sub_return(1, &dev->enable_cnt) != 0)
  1152. return;
  1153. do_pci_disable_device(dev);
  1154. dev->is_busmaster = 0;
  1155. }
  1156. /**
  1157. * pcibios_set_pcie_reset_state - set reset state for device dev
  1158. * @dev: the PCIe device reset
  1159. * @state: Reset state to enter into
  1160. *
  1161. *
  1162. * Sets the PCIe reset state for the device. This is the default
  1163. * implementation. Architecture implementations can override this.
  1164. */
  1165. int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev,
  1166. enum pcie_reset_state state)
  1167. {
  1168. return -EINVAL;
  1169. }
  1170. /**
  1171. * pci_set_pcie_reset_state - set reset state for device dev
  1172. * @dev: the PCIe device reset
  1173. * @state: Reset state to enter into
  1174. *
  1175. *
  1176. * Sets the PCI reset state for the device.
  1177. */
  1178. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  1179. {
  1180. return pcibios_set_pcie_reset_state(dev, state);
  1181. }
  1182. /**
  1183. * pci_check_pme_status - Check if given device has generated PME.
  1184. * @dev: Device to check.
  1185. *
  1186. * Check the PME status of the device and if set, clear it and clear PME enable
  1187. * (if set). Return 'true' if PME status and PME enable were both set or
  1188. * 'false' otherwise.
  1189. */
  1190. bool pci_check_pme_status(struct pci_dev *dev)
  1191. {
  1192. int pmcsr_pos;
  1193. u16 pmcsr;
  1194. bool ret = false;
  1195. if (!dev->pm_cap)
  1196. return false;
  1197. pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
  1198. pci_read_config_word(dev, pmcsr_pos, &pmcsr);
  1199. if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
  1200. return false;
  1201. /* Clear PME status. */
  1202. pmcsr |= PCI_PM_CTRL_PME_STATUS;
  1203. if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
  1204. /* Disable PME to avoid interrupt flood. */
  1205. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1206. ret = true;
  1207. }
  1208. pci_write_config_word(dev, pmcsr_pos, pmcsr);
  1209. return ret;
  1210. }
  1211. /**
  1212. * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
  1213. * @dev: Device to handle.
  1214. * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
  1215. *
  1216. * Check if @dev has generated PME and queue a resume request for it in that
  1217. * case.
  1218. */
  1219. static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
  1220. {
  1221. if (pme_poll_reset && dev->pme_poll)
  1222. dev->pme_poll = false;
  1223. if (pci_check_pme_status(dev)) {
  1224. pci_wakeup_event(dev);
  1225. pm_request_resume(&dev->dev);
  1226. }
  1227. return 0;
  1228. }
  1229. /**
  1230. * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
  1231. * @bus: Top bus of the subtree to walk.
  1232. */
  1233. void pci_pme_wakeup_bus(struct pci_bus *bus)
  1234. {
  1235. if (bus)
  1236. pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
  1237. }
  1238. /**
  1239. * pci_pme_capable - check the capability of PCI device to generate PME#
  1240. * @dev: PCI device to handle.
  1241. * @state: PCI state from which device will issue PME#.
  1242. */
  1243. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  1244. {
  1245. if (!dev->pm_cap)
  1246. return false;
  1247. return !!(dev->pme_support & (1 << state));
  1248. }
  1249. static void pci_pme_list_scan(struct work_struct *work)
  1250. {
  1251. struct pci_pme_device *pme_dev, *n;
  1252. mutex_lock(&pci_pme_list_mutex);
  1253. if (!list_empty(&pci_pme_list)) {
  1254. list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
  1255. if (pme_dev->dev->pme_poll) {
  1256. pci_pme_wakeup(pme_dev->dev, NULL);
  1257. } else {
  1258. list_del(&pme_dev->list);
  1259. kfree(pme_dev);
  1260. }
  1261. }
  1262. if (!list_empty(&pci_pme_list))
  1263. schedule_delayed_work(&pci_pme_work,
  1264. msecs_to_jiffies(PME_TIMEOUT));
  1265. }
  1266. mutex_unlock(&pci_pme_list_mutex);
  1267. }
  1268. /**
  1269. * pci_pme_active - enable or disable PCI device's PME# function
  1270. * @dev: PCI device to handle.
  1271. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  1272. *
  1273. * The caller must verify that the device is capable of generating PME# before
  1274. * calling this function with @enable equal to 'true'.
  1275. */
  1276. void pci_pme_active(struct pci_dev *dev, bool enable)
  1277. {
  1278. u16 pmcsr;
  1279. if (!dev->pm_cap)
  1280. return;
  1281. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1282. /* Clear PME_Status by writing 1 to it and enable PME# */
  1283. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  1284. if (!enable)
  1285. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1286. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  1287. /* PCI (as opposed to PCIe) PME requires that the device have
  1288. its PME# line hooked up correctly. Not all hardware vendors
  1289. do this, so the PME never gets delivered and the device
  1290. remains asleep. The easiest way around this is to
  1291. periodically walk the list of suspended devices and check
  1292. whether any have their PME flag set. The assumption is that
  1293. we'll wake up often enough anyway that this won't be a huge
  1294. hit, and the power savings from the devices will still be a
  1295. win. */
  1296. if (dev->pme_poll) {
  1297. struct pci_pme_device *pme_dev;
  1298. if (enable) {
  1299. pme_dev = kmalloc(sizeof(struct pci_pme_device),
  1300. GFP_KERNEL);
  1301. if (!pme_dev)
  1302. goto out;
  1303. pme_dev->dev = dev;
  1304. mutex_lock(&pci_pme_list_mutex);
  1305. list_add(&pme_dev->list, &pci_pme_list);
  1306. if (list_is_singular(&pci_pme_list))
  1307. schedule_delayed_work(&pci_pme_work,
  1308. msecs_to_jiffies(PME_TIMEOUT));
  1309. mutex_unlock(&pci_pme_list_mutex);
  1310. } else {
  1311. mutex_lock(&pci_pme_list_mutex);
  1312. list_for_each_entry(pme_dev, &pci_pme_list, list) {
  1313. if (pme_dev->dev == dev) {
  1314. list_del(&pme_dev->list);
  1315. kfree(pme_dev);
  1316. break;
  1317. }
  1318. }
  1319. mutex_unlock(&pci_pme_list_mutex);
  1320. }
  1321. }
  1322. out:
  1323. dev_printk(KERN_DEBUG, &dev->dev, "PME# %s\n",
  1324. enable ? "enabled" : "disabled");
  1325. }
  1326. /**
  1327. * __pci_enable_wake - enable PCI device as wakeup event source
  1328. * @dev: PCI device affected
  1329. * @state: PCI state from which device will issue wakeup events
  1330. * @runtime: True if the events are to be generated at run time
  1331. * @enable: True to enable event generation; false to disable
  1332. *
  1333. * This enables the device as a wakeup event source, or disables it.
  1334. * When such events involves platform-specific hooks, those hooks are
  1335. * called automatically by this routine.
  1336. *
  1337. * Devices with legacy power management (no standard PCI PM capabilities)
  1338. * always require such platform hooks.
  1339. *
  1340. * RETURN VALUE:
  1341. * 0 is returned on success
  1342. * -EINVAL is returned if device is not supposed to wake up the system
  1343. * Error code depending on the platform is returned if both the platform and
  1344. * the native mechanism fail to enable the generation of wake-up events
  1345. */
  1346. int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
  1347. bool runtime, bool enable)
  1348. {
  1349. int ret = 0;
  1350. if (enable && !runtime && !device_may_wakeup(&dev->dev))
  1351. return -EINVAL;
  1352. /* Don't do the same thing twice in a row for one device. */
  1353. if (!!enable == !!dev->wakeup_prepared)
  1354. return 0;
  1355. /*
  1356. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  1357. * Anderson we should be doing PME# wake enable followed by ACPI wake
  1358. * enable. To disable wake-up we call the platform first, for symmetry.
  1359. */
  1360. if (enable) {
  1361. int error;
  1362. if (pci_pme_capable(dev, state))
  1363. pci_pme_active(dev, true);
  1364. else
  1365. ret = 1;
  1366. error = runtime ? platform_pci_run_wake(dev, true) :
  1367. platform_pci_sleep_wake(dev, true);
  1368. if (ret)
  1369. ret = error;
  1370. if (!ret)
  1371. dev->wakeup_prepared = true;
  1372. } else {
  1373. if (runtime)
  1374. platform_pci_run_wake(dev, false);
  1375. else
  1376. platform_pci_sleep_wake(dev, false);
  1377. pci_pme_active(dev, false);
  1378. dev->wakeup_prepared = false;
  1379. }
  1380. return ret;
  1381. }
  1382. EXPORT_SYMBOL(__pci_enable_wake);
  1383. /**
  1384. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  1385. * @dev: PCI device to prepare
  1386. * @enable: True to enable wake-up event generation; false to disable
  1387. *
  1388. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  1389. * and this function allows them to set that up cleanly - pci_enable_wake()
  1390. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  1391. * ordering constraints.
  1392. *
  1393. * This function only returns error code if the device is not capable of
  1394. * generating PME# from both D3_hot and D3_cold, and the platform is unable to
  1395. * enable wake-up power for it.
  1396. */
  1397. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  1398. {
  1399. return pci_pme_capable(dev, PCI_D3cold) ?
  1400. pci_enable_wake(dev, PCI_D3cold, enable) :
  1401. pci_enable_wake(dev, PCI_D3hot, enable);
  1402. }
  1403. /**
  1404. * pci_target_state - find an appropriate low power state for a given PCI dev
  1405. * @dev: PCI device
  1406. *
  1407. * Use underlying platform code to find a supported low power state for @dev.
  1408. * If the platform can't manage @dev, return the deepest state from which it
  1409. * can generate wake events, based on any available PME info.
  1410. */
  1411. pci_power_t pci_target_state(struct pci_dev *dev)
  1412. {
  1413. pci_power_t target_state = PCI_D3hot;
  1414. if (platform_pci_power_manageable(dev)) {
  1415. /*
  1416. * Call the platform to choose the target state of the device
  1417. * and enable wake-up from this state if supported.
  1418. */
  1419. pci_power_t state = platform_pci_choose_state(dev);
  1420. switch (state) {
  1421. case PCI_POWER_ERROR:
  1422. case PCI_UNKNOWN:
  1423. break;
  1424. case PCI_D1:
  1425. case PCI_D2:
  1426. if (pci_no_d1d2(dev))
  1427. break;
  1428. default:
  1429. target_state = state;
  1430. }
  1431. } else if (!dev->pm_cap) {
  1432. target_state = PCI_D0;
  1433. } else if (device_may_wakeup(&dev->dev)) {
  1434. /*
  1435. * Find the deepest state from which the device can generate
  1436. * wake-up events, make it the target state and enable device
  1437. * to generate PME#.
  1438. */
  1439. if (dev->pme_support) {
  1440. while (target_state
  1441. && !(dev->pme_support & (1 << target_state)))
  1442. target_state--;
  1443. }
  1444. }
  1445. return target_state;
  1446. }
  1447. /**
  1448. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1449. * @dev: Device to handle.
  1450. *
  1451. * Choose the power state appropriate for the device depending on whether
  1452. * it can wake up the system and/or is power manageable by the platform
  1453. * (PCI_D3hot is the default) and put the device into that state.
  1454. */
  1455. int pci_prepare_to_sleep(struct pci_dev *dev)
  1456. {
  1457. pci_power_t target_state = pci_target_state(dev);
  1458. int error;
  1459. if (target_state == PCI_POWER_ERROR)
  1460. return -EIO;
  1461. pci_enable_wake(dev, target_state, device_may_wakeup(&dev->dev));
  1462. error = pci_set_power_state(dev, target_state);
  1463. if (error)
  1464. pci_enable_wake(dev, target_state, false);
  1465. return error;
  1466. }
  1467. /**
  1468. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1469. * @dev: Device to handle.
  1470. *
  1471. * Disable device's system wake-up capability and put it into D0.
  1472. */
  1473. int pci_back_from_sleep(struct pci_dev *dev)
  1474. {
  1475. pci_enable_wake(dev, PCI_D0, false);
  1476. return pci_set_power_state(dev, PCI_D0);
  1477. }
  1478. /**
  1479. * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
  1480. * @dev: PCI device being suspended.
  1481. *
  1482. * Prepare @dev to generate wake-up events at run time and put it into a low
  1483. * power state.
  1484. */
  1485. int pci_finish_runtime_suspend(struct pci_dev *dev)
  1486. {
  1487. pci_power_t target_state = pci_target_state(dev);
  1488. int error;
  1489. if (target_state == PCI_POWER_ERROR)
  1490. return -EIO;
  1491. __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev));
  1492. error = pci_set_power_state(dev, target_state);
  1493. if (error)
  1494. __pci_enable_wake(dev, target_state, true, false);
  1495. return error;
  1496. }
  1497. /**
  1498. * pci_dev_run_wake - Check if device can generate run-time wake-up events.
  1499. * @dev: Device to check.
  1500. *
  1501. * Return true if the device itself is cabable of generating wake-up events
  1502. * (through the platform or using the native PCIe PME) or if the device supports
  1503. * PME and one of its upstream bridges can generate wake-up events.
  1504. */
  1505. bool pci_dev_run_wake(struct pci_dev *dev)
  1506. {
  1507. struct pci_bus *bus = dev->bus;
  1508. if (device_run_wake(&dev->dev))
  1509. return true;
  1510. if (!dev->pme_support)
  1511. return false;
  1512. while (bus->parent) {
  1513. struct pci_dev *bridge = bus->self;
  1514. if (device_run_wake(&bridge->dev))
  1515. return true;
  1516. bus = bus->parent;
  1517. }
  1518. /* We have reached the root bus. */
  1519. if (bus->bridge)
  1520. return device_run_wake(bus->bridge);
  1521. return false;
  1522. }
  1523. EXPORT_SYMBOL_GPL(pci_dev_run_wake);
  1524. /**
  1525. * pci_pm_init - Initialize PM functions of given PCI device
  1526. * @dev: PCI device to handle.
  1527. */
  1528. void pci_pm_init(struct pci_dev *dev)
  1529. {
  1530. int pm;
  1531. u16 pmc;
  1532. pm_runtime_forbid(&dev->dev);
  1533. device_enable_async_suspend(&dev->dev);
  1534. dev->wakeup_prepared = false;
  1535. dev->pm_cap = 0;
  1536. /* find PCI PM capability in list */
  1537. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  1538. if (!pm)
  1539. return;
  1540. /* Check device's ability to generate PME# */
  1541. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  1542. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  1543. dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
  1544. pmc & PCI_PM_CAP_VER_MASK);
  1545. return;
  1546. }
  1547. dev->pm_cap = pm;
  1548. dev->d3_delay = PCI_PM_D3_WAIT;
  1549. dev->d1_support = false;
  1550. dev->d2_support = false;
  1551. if (!pci_no_d1d2(dev)) {
  1552. if (pmc & PCI_PM_CAP_D1)
  1553. dev->d1_support = true;
  1554. if (pmc & PCI_PM_CAP_D2)
  1555. dev->d2_support = true;
  1556. if (dev->d1_support || dev->d2_support)
  1557. dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
  1558. dev->d1_support ? " D1" : "",
  1559. dev->d2_support ? " D2" : "");
  1560. }
  1561. pmc &= PCI_PM_CAP_PME_MASK;
  1562. if (pmc) {
  1563. dev_printk(KERN_DEBUG, &dev->dev,
  1564. "PME# supported from%s%s%s%s%s\n",
  1565. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  1566. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  1567. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  1568. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  1569. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  1570. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  1571. dev->pme_poll = true;
  1572. /*
  1573. * Make device's PM flags reflect the wake-up capability, but
  1574. * let the user space enable it to wake up the system as needed.
  1575. */
  1576. device_set_wakeup_capable(&dev->dev, true);
  1577. /* Disable the PME# generation functionality */
  1578. pci_pme_active(dev, false);
  1579. } else {
  1580. dev->pme_support = 0;
  1581. }
  1582. }
  1583. /**
  1584. * platform_pci_wakeup_init - init platform wakeup if present
  1585. * @dev: PCI device
  1586. *
  1587. * Some devices don't have PCI PM caps but can still generate wakeup
  1588. * events through platform methods (like ACPI events). If @dev supports
  1589. * platform wakeup events, set the device flag to indicate as much. This
  1590. * may be redundant if the device also supports PCI PM caps, but double
  1591. * initialization should be safe in that case.
  1592. */
  1593. void platform_pci_wakeup_init(struct pci_dev *dev)
  1594. {
  1595. if (!platform_pci_can_wakeup(dev))
  1596. return;
  1597. device_set_wakeup_capable(&dev->dev, true);
  1598. platform_pci_sleep_wake(dev, false);
  1599. }
  1600. /**
  1601. * pci_add_save_buffer - allocate buffer for saving given capability registers
  1602. * @dev: the PCI device
  1603. * @cap: the capability to allocate the buffer for
  1604. * @size: requested size of the buffer
  1605. */
  1606. static int pci_add_cap_save_buffer(
  1607. struct pci_dev *dev, char cap, unsigned int size)
  1608. {
  1609. int pos;
  1610. struct pci_cap_saved_state *save_state;
  1611. pos = pci_find_capability(dev, cap);
  1612. if (pos <= 0)
  1613. return 0;
  1614. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  1615. if (!save_state)
  1616. return -ENOMEM;
  1617. save_state->cap.cap_nr = cap;
  1618. save_state->cap.size = size;
  1619. pci_add_saved_cap(dev, save_state);
  1620. return 0;
  1621. }
  1622. /**
  1623. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  1624. * @dev: the PCI device
  1625. */
  1626. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  1627. {
  1628. int error;
  1629. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
  1630. PCI_EXP_SAVE_REGS * sizeof(u16));
  1631. if (error)
  1632. dev_err(&dev->dev,
  1633. "unable to preallocate PCI Express save buffer\n");
  1634. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  1635. if (error)
  1636. dev_err(&dev->dev,
  1637. "unable to preallocate PCI-X save buffer\n");
  1638. }
  1639. /**
  1640. * pci_enable_ari - enable ARI forwarding if hardware support it
  1641. * @dev: the PCI device
  1642. */
  1643. void pci_enable_ari(struct pci_dev *dev)
  1644. {
  1645. int pos;
  1646. u32 cap;
  1647. u16 flags, ctrl;
  1648. struct pci_dev *bridge;
  1649. if (!pci_is_pcie(dev) || dev->devfn)
  1650. return;
  1651. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  1652. if (!pos)
  1653. return;
  1654. bridge = dev->bus->self;
  1655. if (!bridge || !pci_is_pcie(bridge))
  1656. return;
  1657. pos = pci_pcie_cap(bridge);
  1658. if (!pos)
  1659. return;
  1660. /* ARI is a PCIe v2 feature */
  1661. pci_read_config_word(bridge, pos + PCI_EXP_FLAGS, &flags);
  1662. if ((flags & PCI_EXP_FLAGS_VERS) < 2)
  1663. return;
  1664. pci_read_config_dword(bridge, pos + PCI_EXP_DEVCAP2, &cap);
  1665. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  1666. return;
  1667. pci_read_config_word(bridge, pos + PCI_EXP_DEVCTL2, &ctrl);
  1668. ctrl |= PCI_EXP_DEVCTL2_ARI;
  1669. pci_write_config_word(bridge, pos + PCI_EXP_DEVCTL2, ctrl);
  1670. bridge->ari_enabled = 1;
  1671. }
  1672. /**
  1673. * pci_enable_ido - enable ID-based ordering on a device
  1674. * @dev: the PCI device
  1675. * @type: which types of IDO to enable
  1676. *
  1677. * Enable ID-based ordering on @dev. @type can contain the bits
  1678. * %PCI_EXP_IDO_REQUEST and/or %PCI_EXP_IDO_COMPLETION to indicate
  1679. * which types of transactions are allowed to be re-ordered.
  1680. */
  1681. void pci_enable_ido(struct pci_dev *dev, unsigned long type)
  1682. {
  1683. int pos;
  1684. u16 ctrl;
  1685. pos = pci_pcie_cap(dev);
  1686. if (!pos)
  1687. return;
  1688. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
  1689. if (type & PCI_EXP_IDO_REQUEST)
  1690. ctrl |= PCI_EXP_IDO_REQ_EN;
  1691. if (type & PCI_EXP_IDO_COMPLETION)
  1692. ctrl |= PCI_EXP_IDO_CMP_EN;
  1693. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
  1694. }
  1695. EXPORT_SYMBOL(pci_enable_ido);
  1696. /**
  1697. * pci_disable_ido - disable ID-based ordering on a device
  1698. * @dev: the PCI device
  1699. * @type: which types of IDO to disable
  1700. */
  1701. void pci_disable_ido(struct pci_dev *dev, unsigned long type)
  1702. {
  1703. int pos;
  1704. u16 ctrl;
  1705. if (!pci_is_pcie(dev))
  1706. return;
  1707. pos = pci_pcie_cap(dev);
  1708. if (!pos)
  1709. return;
  1710. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
  1711. if (type & PCI_EXP_IDO_REQUEST)
  1712. ctrl &= ~PCI_EXP_IDO_REQ_EN;
  1713. if (type & PCI_EXP_IDO_COMPLETION)
  1714. ctrl &= ~PCI_EXP_IDO_CMP_EN;
  1715. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
  1716. }
  1717. EXPORT_SYMBOL(pci_disable_ido);
  1718. /**
  1719. * pci_enable_obff - enable optimized buffer flush/fill
  1720. * @dev: PCI device
  1721. * @type: type of signaling to use
  1722. *
  1723. * Try to enable @type OBFF signaling on @dev. It will try using WAKE#
  1724. * signaling if possible, falling back to message signaling only if
  1725. * WAKE# isn't supported. @type should indicate whether the PCIe link
  1726. * be brought out of L0s or L1 to send the message. It should be either
  1727. * %PCI_EXP_OBFF_SIGNAL_ALWAYS or %PCI_OBFF_SIGNAL_L0.
  1728. *
  1729. * If your device can benefit from receiving all messages, even at the
  1730. * power cost of bringing the link back up from a low power state, use
  1731. * %PCI_EXP_OBFF_SIGNAL_ALWAYS. Otherwise, use %PCI_OBFF_SIGNAL_L0 (the
  1732. * preferred type).
  1733. *
  1734. * RETURNS:
  1735. * Zero on success, appropriate error number on failure.
  1736. */
  1737. int pci_enable_obff(struct pci_dev *dev, enum pci_obff_signal_type type)
  1738. {
  1739. int pos;
  1740. u32 cap;
  1741. u16 ctrl;
  1742. int ret;
  1743. if (!pci_is_pcie(dev))
  1744. return -ENOTSUPP;
  1745. pos = pci_pcie_cap(dev);
  1746. if (!pos)
  1747. return -ENOTSUPP;
  1748. pci_read_config_dword(dev, pos + PCI_EXP_DEVCAP2, &cap);
  1749. if (!(cap & PCI_EXP_OBFF_MASK))
  1750. return -ENOTSUPP; /* no OBFF support at all */
  1751. /* Make sure the topology supports OBFF as well */
  1752. if (dev->bus) {
  1753. ret = pci_enable_obff(dev->bus->self, type);
  1754. if (ret)
  1755. return ret;
  1756. }
  1757. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
  1758. if (cap & PCI_EXP_OBFF_WAKE)
  1759. ctrl |= PCI_EXP_OBFF_WAKE_EN;
  1760. else {
  1761. switch (type) {
  1762. case PCI_EXP_OBFF_SIGNAL_L0:
  1763. if (!(ctrl & PCI_EXP_OBFF_WAKE_EN))
  1764. ctrl |= PCI_EXP_OBFF_MSGA_EN;
  1765. break;
  1766. case PCI_EXP_OBFF_SIGNAL_ALWAYS:
  1767. ctrl &= ~PCI_EXP_OBFF_WAKE_EN;
  1768. ctrl |= PCI_EXP_OBFF_MSGB_EN;
  1769. break;
  1770. default:
  1771. WARN(1, "bad OBFF signal type\n");
  1772. return -ENOTSUPP;
  1773. }
  1774. }
  1775. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
  1776. return 0;
  1777. }
  1778. EXPORT_SYMBOL(pci_enable_obff);
  1779. /**
  1780. * pci_disable_obff - disable optimized buffer flush/fill
  1781. * @dev: PCI device
  1782. *
  1783. * Disable OBFF on @dev.
  1784. */
  1785. void pci_disable_obff(struct pci_dev *dev)
  1786. {
  1787. int pos;
  1788. u16 ctrl;
  1789. if (!pci_is_pcie(dev))
  1790. return;
  1791. pos = pci_pcie_cap(dev);
  1792. if (!pos)
  1793. return;
  1794. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
  1795. ctrl &= ~PCI_EXP_OBFF_WAKE_EN;
  1796. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
  1797. }
  1798. EXPORT_SYMBOL(pci_disable_obff);
  1799. /**
  1800. * pci_ltr_supported - check whether a device supports LTR
  1801. * @dev: PCI device
  1802. *
  1803. * RETURNS:
  1804. * True if @dev supports latency tolerance reporting, false otherwise.
  1805. */
  1806. bool pci_ltr_supported(struct pci_dev *dev)
  1807. {
  1808. int pos;
  1809. u32 cap;
  1810. if (!pci_is_pcie(dev))
  1811. return false;
  1812. pos = pci_pcie_cap(dev);
  1813. if (!pos)
  1814. return false;
  1815. pci_read_config_dword(dev, pos + PCI_EXP_DEVCAP2, &cap);
  1816. return cap & PCI_EXP_DEVCAP2_LTR;
  1817. }
  1818. EXPORT_SYMBOL(pci_ltr_supported);
  1819. /**
  1820. * pci_enable_ltr - enable latency tolerance reporting
  1821. * @dev: PCI device
  1822. *
  1823. * Enable LTR on @dev if possible, which means enabling it first on
  1824. * upstream ports.
  1825. *
  1826. * RETURNS:
  1827. * Zero on success, errno on failure.
  1828. */
  1829. int pci_enable_ltr(struct pci_dev *dev)
  1830. {
  1831. int pos;
  1832. u16 ctrl;
  1833. int ret;
  1834. if (!pci_ltr_supported(dev))
  1835. return -ENOTSUPP;
  1836. pos = pci_pcie_cap(dev);
  1837. if (!pos)
  1838. return -ENOTSUPP;
  1839. /* Only primary function can enable/disable LTR */
  1840. if (PCI_FUNC(dev->devfn) != 0)
  1841. return -EINVAL;
  1842. /* Enable upstream ports first */
  1843. if (dev->bus) {
  1844. ret = pci_enable_ltr(dev->bus->self);
  1845. if (ret)
  1846. return ret;
  1847. }
  1848. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
  1849. ctrl |= PCI_EXP_LTR_EN;
  1850. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
  1851. return 0;
  1852. }
  1853. EXPORT_SYMBOL(pci_enable_ltr);
  1854. /**
  1855. * pci_disable_ltr - disable latency tolerance reporting
  1856. * @dev: PCI device
  1857. */
  1858. void pci_disable_ltr(struct pci_dev *dev)
  1859. {
  1860. int pos;
  1861. u16 ctrl;
  1862. if (!pci_ltr_supported(dev))
  1863. return;
  1864. pos = pci_pcie_cap(dev);
  1865. if (!pos)
  1866. return;
  1867. /* Only primary function can enable/disable LTR */
  1868. if (PCI_FUNC(dev->devfn) != 0)
  1869. return;
  1870. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl);
  1871. ctrl &= ~PCI_EXP_LTR_EN;
  1872. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl);
  1873. }
  1874. EXPORT_SYMBOL(pci_disable_ltr);
  1875. static int __pci_ltr_scale(int *val)
  1876. {
  1877. int scale = 0;
  1878. while (*val > 1023) {
  1879. *val = (*val + 31) / 32;
  1880. scale++;
  1881. }
  1882. return scale;
  1883. }
  1884. /**
  1885. * pci_set_ltr - set LTR latency values
  1886. * @dev: PCI device
  1887. * @snoop_lat_ns: snoop latency in nanoseconds
  1888. * @nosnoop_lat_ns: nosnoop latency in nanoseconds
  1889. *
  1890. * Figure out the scale and set the LTR values accordingly.
  1891. */
  1892. int pci_set_ltr(struct pci_dev *dev, int snoop_lat_ns, int nosnoop_lat_ns)
  1893. {
  1894. int pos, ret, snoop_scale, nosnoop_scale;
  1895. u16 val;
  1896. if (!pci_ltr_supported(dev))
  1897. return -ENOTSUPP;
  1898. snoop_scale = __pci_ltr_scale(&snoop_lat_ns);
  1899. nosnoop_scale = __pci_ltr_scale(&nosnoop_lat_ns);
  1900. if (snoop_lat_ns > PCI_LTR_VALUE_MASK ||
  1901. nosnoop_lat_ns > PCI_LTR_VALUE_MASK)
  1902. return -EINVAL;
  1903. if ((snoop_scale > (PCI_LTR_SCALE_MASK >> PCI_LTR_SCALE_SHIFT)) ||
  1904. (nosnoop_scale > (PCI_LTR_SCALE_MASK >> PCI_LTR_SCALE_SHIFT)))
  1905. return -EINVAL;
  1906. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_LTR);
  1907. if (!pos)
  1908. return -ENOTSUPP;
  1909. val = (snoop_scale << PCI_LTR_SCALE_SHIFT) | snoop_lat_ns;
  1910. ret = pci_write_config_word(dev, pos + PCI_LTR_MAX_SNOOP_LAT, val);
  1911. if (ret != 4)
  1912. return -EIO;
  1913. val = (nosnoop_scale << PCI_LTR_SCALE_SHIFT) | nosnoop_lat_ns;
  1914. ret = pci_write_config_word(dev, pos + PCI_LTR_MAX_NOSNOOP_LAT, val);
  1915. if (ret != 4)
  1916. return -EIO;
  1917. return 0;
  1918. }
  1919. EXPORT_SYMBOL(pci_set_ltr);
  1920. static int pci_acs_enable;
  1921. /**
  1922. * pci_request_acs - ask for ACS to be enabled if supported
  1923. */
  1924. void pci_request_acs(void)
  1925. {
  1926. pci_acs_enable = 1;
  1927. }
  1928. /**
  1929. * pci_enable_acs - enable ACS if hardware support it
  1930. * @dev: the PCI device
  1931. */
  1932. void pci_enable_acs(struct pci_dev *dev)
  1933. {
  1934. int pos;
  1935. u16 cap;
  1936. u16 ctrl;
  1937. if (!pci_acs_enable)
  1938. return;
  1939. if (!pci_is_pcie(dev))
  1940. return;
  1941. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  1942. if (!pos)
  1943. return;
  1944. pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
  1945. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  1946. /* Source Validation */
  1947. ctrl |= (cap & PCI_ACS_SV);
  1948. /* P2P Request Redirect */
  1949. ctrl |= (cap & PCI_ACS_RR);
  1950. /* P2P Completion Redirect */
  1951. ctrl |= (cap & PCI_ACS_CR);
  1952. /* Upstream Forwarding */
  1953. ctrl |= (cap & PCI_ACS_UF);
  1954. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  1955. }
  1956. /**
  1957. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  1958. * @dev: the PCI device
  1959. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1960. *
  1961. * Perform INTx swizzling for a device behind one level of bridge. This is
  1962. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  1963. * behind bridges on add-in cards. For devices with ARI enabled, the slot
  1964. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  1965. * the PCI Express Base Specification, Revision 2.1)
  1966. */
  1967. u8 pci_swizzle_interrupt_pin(struct pci_dev *dev, u8 pin)
  1968. {
  1969. int slot;
  1970. if (pci_ari_enabled(dev->bus))
  1971. slot = 0;
  1972. else
  1973. slot = PCI_SLOT(dev->devfn);
  1974. return (((pin - 1) + slot) % 4) + 1;
  1975. }
  1976. int
  1977. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  1978. {
  1979. u8 pin;
  1980. pin = dev->pin;
  1981. if (!pin)
  1982. return -1;
  1983. while (!pci_is_root_bus(dev->bus)) {
  1984. pin = pci_swizzle_interrupt_pin(dev, pin);
  1985. dev = dev->bus->self;
  1986. }
  1987. *bridge = dev;
  1988. return pin;
  1989. }
  1990. /**
  1991. * pci_common_swizzle - swizzle INTx all the way to root bridge
  1992. * @dev: the PCI device
  1993. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1994. *
  1995. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  1996. * bridges all the way up to a PCI root bus.
  1997. */
  1998. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  1999. {
  2000. u8 pin = *pinp;
  2001. while (!pci_is_root_bus(dev->bus)) {
  2002. pin = pci_swizzle_interrupt_pin(dev, pin);
  2003. dev = dev->bus->self;
  2004. }
  2005. *pinp = pin;
  2006. return PCI_SLOT(dev->devfn);
  2007. }
  2008. /**
  2009. * pci_release_region - Release a PCI bar
  2010. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  2011. * @bar: BAR to release
  2012. *
  2013. * Releases the PCI I/O and memory resources previously reserved by a
  2014. * successful call to pci_request_region. Call this function only
  2015. * after all use of the PCI regions has ceased.
  2016. */
  2017. void pci_release_region(struct pci_dev *pdev, int bar)
  2018. {
  2019. struct pci_devres *dr;
  2020. if (pci_resource_len(pdev, bar) == 0)
  2021. return;
  2022. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  2023. release_region(pci_resource_start(pdev, bar),
  2024. pci_resource_len(pdev, bar));
  2025. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  2026. release_mem_region(pci_resource_start(pdev, bar),
  2027. pci_resource_len(pdev, bar));
  2028. dr = find_pci_dr(pdev);
  2029. if (dr)
  2030. dr->region_mask &= ~(1 << bar);
  2031. }
  2032. /**
  2033. * __pci_request_region - Reserved PCI I/O and memory resource
  2034. * @pdev: PCI device whose resources are to be reserved
  2035. * @bar: BAR to be reserved
  2036. * @res_name: Name to be associated with resource.
  2037. * @exclusive: whether the region access is exclusive or not
  2038. *
  2039. * Mark the PCI region associated with PCI device @pdev BR @bar as
  2040. * being reserved by owner @res_name. Do not access any
  2041. * address inside the PCI regions unless this call returns
  2042. * successfully.
  2043. *
  2044. * If @exclusive is set, then the region is marked so that userspace
  2045. * is explicitly not allowed to map the resource via /dev/mem or
  2046. * sysfs MMIO access.
  2047. *
  2048. * Returns 0 on success, or %EBUSY on error. A warning
  2049. * message is also printed on failure.
  2050. */
  2051. static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
  2052. int exclusive)
  2053. {
  2054. struct pci_devres *dr;
  2055. if (pci_resource_len(pdev, bar) == 0)
  2056. return 0;
  2057. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  2058. if (!request_region(pci_resource_start(pdev, bar),
  2059. pci_resource_len(pdev, bar), res_name))
  2060. goto err_out;
  2061. }
  2062. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  2063. if (!__request_mem_region(pci_resource_start(pdev, bar),
  2064. pci_resource_len(pdev, bar), res_name,
  2065. exclusive))
  2066. goto err_out;
  2067. }
  2068. dr = find_pci_dr(pdev);
  2069. if (dr)
  2070. dr->region_mask |= 1 << bar;
  2071. return 0;
  2072. err_out:
  2073. dev_warn(&pdev->dev, "BAR %d: can't reserve %pR\n", bar,
  2074. &pdev->resource[bar]);
  2075. return -EBUSY;
  2076. }
  2077. /**
  2078. * pci_request_region - Reserve PCI I/O and memory resource
  2079. * @pdev: PCI device whose resources are to be reserved
  2080. * @bar: BAR to be reserved
  2081. * @res_name: Name to be associated with resource
  2082. *
  2083. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  2084. * being reserved by owner @res_name. Do not access any
  2085. * address inside the PCI regions unless this call returns
  2086. * successfully.
  2087. *
  2088. * Returns 0 on success, or %EBUSY on error. A warning
  2089. * message is also printed on failure.
  2090. */
  2091. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  2092. {
  2093. return __pci_request_region(pdev, bar, res_name, 0);
  2094. }
  2095. /**
  2096. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  2097. * @pdev: PCI device whose resources are to be reserved
  2098. * @bar: BAR to be reserved
  2099. * @res_name: Name to be associated with resource.
  2100. *
  2101. * Mark the PCI region associated with PCI device @pdev BR @bar as
  2102. * being reserved by owner @res_name. Do not access any
  2103. * address inside the PCI regions unless this call returns
  2104. * successfully.
  2105. *
  2106. * Returns 0 on success, or %EBUSY on error. A warning
  2107. * message is also printed on failure.
  2108. *
  2109. * The key difference that _exclusive makes it that userspace is
  2110. * explicitly not allowed to map the resource via /dev/mem or
  2111. * sysfs.
  2112. */
  2113. int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
  2114. {
  2115. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  2116. }
  2117. /**
  2118. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  2119. * @pdev: PCI device whose resources were previously reserved
  2120. * @bars: Bitmask of BARs to be released
  2121. *
  2122. * Release selected PCI I/O and memory resources previously reserved.
  2123. * Call this function only after all use of the PCI regions has ceased.
  2124. */
  2125. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  2126. {
  2127. int i;
  2128. for (i = 0; i < 6; i++)
  2129. if (bars & (1 << i))
  2130. pci_release_region(pdev, i);
  2131. }
  2132. int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  2133. const char *res_name, int excl)
  2134. {
  2135. int i;
  2136. for (i = 0; i < 6; i++)
  2137. if (bars & (1 << i))
  2138. if (__pci_request_region(pdev, i, res_name, excl))
  2139. goto err_out;
  2140. return 0;
  2141. err_out:
  2142. while(--i >= 0)
  2143. if (bars & (1 << i))
  2144. pci_release_region(pdev, i);
  2145. return -EBUSY;
  2146. }
  2147. /**
  2148. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  2149. * @pdev: PCI device whose resources are to be reserved
  2150. * @bars: Bitmask of BARs to be requested
  2151. * @res_name: Name to be associated with resource
  2152. */
  2153. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  2154. const char *res_name)
  2155. {
  2156. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  2157. }
  2158. int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
  2159. int bars, const char *res_name)
  2160. {
  2161. return __pci_request_selected_regions(pdev, bars, res_name,
  2162. IORESOURCE_EXCLUSIVE);
  2163. }
  2164. /**
  2165. * pci_release_regions - Release reserved PCI I/O and memory resources
  2166. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  2167. *
  2168. * Releases all PCI I/O and memory resources previously reserved by a
  2169. * successful call to pci_request_regions. Call this function only
  2170. * after all use of the PCI regions has ceased.
  2171. */
  2172. void pci_release_regions(struct pci_dev *pdev)
  2173. {
  2174. pci_release_selected_regions(pdev, (1 << 6) - 1);
  2175. }
  2176. /**
  2177. * pci_request_regions - Reserved PCI I/O and memory resources
  2178. * @pdev: PCI device whose resources are to be reserved
  2179. * @res_name: Name to be associated with resource.
  2180. *
  2181. * Mark all PCI regions associated with PCI device @pdev as
  2182. * being reserved by owner @res_name. Do not access any
  2183. * address inside the PCI regions unless this call returns
  2184. * successfully.
  2185. *
  2186. * Returns 0 on success, or %EBUSY on error. A warning
  2187. * message is also printed on failure.
  2188. */
  2189. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  2190. {
  2191. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  2192. }
  2193. /**
  2194. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  2195. * @pdev: PCI device whose resources are to be reserved
  2196. * @res_name: Name to be associated with resource.
  2197. *
  2198. * Mark all PCI regions associated with PCI device @pdev as
  2199. * being reserved by owner @res_name. Do not access any
  2200. * address inside the PCI regions unless this call returns
  2201. * successfully.
  2202. *
  2203. * pci_request_regions_exclusive() will mark the region so that
  2204. * /dev/mem and the sysfs MMIO access will not be allowed.
  2205. *
  2206. * Returns 0 on success, or %EBUSY on error. A warning
  2207. * message is also printed on failure.
  2208. */
  2209. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  2210. {
  2211. return pci_request_selected_regions_exclusive(pdev,
  2212. ((1 << 6) - 1), res_name);
  2213. }
  2214. static void __pci_set_master(struct pci_dev *dev, bool enable)
  2215. {
  2216. u16 old_cmd, cmd;
  2217. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  2218. if (enable)
  2219. cmd = old_cmd | PCI_COMMAND_MASTER;
  2220. else
  2221. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  2222. if (cmd != old_cmd) {
  2223. dev_dbg(&dev->dev, "%s bus mastering\n",
  2224. enable ? "enabling" : "disabling");
  2225. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2226. }
  2227. dev->is_busmaster = enable;
  2228. }
  2229. /**
  2230. * pci_set_master - enables bus-mastering for device dev
  2231. * @dev: the PCI device to enable
  2232. *
  2233. * Enables bus-mastering on the device and calls pcibios_set_master()
  2234. * to do the needed arch specific settings.
  2235. */
  2236. void pci_set_master(struct pci_dev *dev)
  2237. {
  2238. __pci_set_master(dev, true);
  2239. pcibios_set_master(dev);
  2240. }
  2241. /**
  2242. * pci_clear_master - disables bus-mastering for device dev
  2243. * @dev: the PCI device to disable
  2244. */
  2245. void pci_clear_master(struct pci_dev *dev)
  2246. {
  2247. __pci_set_master(dev, false);
  2248. }
  2249. /**
  2250. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  2251. * @dev: the PCI device for which MWI is to be enabled
  2252. *
  2253. * Helper function for pci_set_mwi.
  2254. * Originally copied from drivers/net/acenic.c.
  2255. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  2256. *
  2257. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2258. */
  2259. int pci_set_cacheline_size(struct pci_dev *dev)
  2260. {
  2261. u8 cacheline_size;
  2262. if (!pci_cache_line_size)
  2263. return -EINVAL;
  2264. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  2265. equal to or multiple of the right value. */
  2266. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  2267. if (cacheline_size >= pci_cache_line_size &&
  2268. (cacheline_size % pci_cache_line_size) == 0)
  2269. return 0;
  2270. /* Write the correct value. */
  2271. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  2272. /* Read it back. */
  2273. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  2274. if (cacheline_size == pci_cache_line_size)
  2275. return 0;
  2276. dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
  2277. "supported\n", pci_cache_line_size << 2);
  2278. return -EINVAL;
  2279. }
  2280. EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
  2281. #ifdef PCI_DISABLE_MWI
  2282. int pci_set_mwi(struct pci_dev *dev)
  2283. {
  2284. return 0;
  2285. }
  2286. int pci_try_set_mwi(struct pci_dev *dev)
  2287. {
  2288. return 0;
  2289. }
  2290. void pci_clear_mwi(struct pci_dev *dev)
  2291. {
  2292. }
  2293. #else
  2294. /**
  2295. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  2296. * @dev: the PCI device for which MWI is enabled
  2297. *
  2298. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  2299. *
  2300. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2301. */
  2302. int
  2303. pci_set_mwi(struct pci_dev *dev)
  2304. {
  2305. int rc;
  2306. u16 cmd;
  2307. rc = pci_set_cacheline_size(dev);
  2308. if (rc)
  2309. return rc;
  2310. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2311. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  2312. dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
  2313. cmd |= PCI_COMMAND_INVALIDATE;
  2314. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2315. }
  2316. return 0;
  2317. }
  2318. /**
  2319. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  2320. * @dev: the PCI device for which MWI is enabled
  2321. *
  2322. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  2323. * Callers are not required to check the return value.
  2324. *
  2325. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2326. */
  2327. int pci_try_set_mwi(struct pci_dev *dev)
  2328. {
  2329. int rc = pci_set_mwi(dev);
  2330. return rc;
  2331. }
  2332. /**
  2333. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  2334. * @dev: the PCI device to disable
  2335. *
  2336. * Disables PCI Memory-Write-Invalidate transaction on the device
  2337. */
  2338. void
  2339. pci_clear_mwi(struct pci_dev *dev)
  2340. {
  2341. u16 cmd;
  2342. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2343. if (cmd & PCI_COMMAND_INVALIDATE) {
  2344. cmd &= ~PCI_COMMAND_INVALIDATE;
  2345. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2346. }
  2347. }
  2348. #endif /* ! PCI_DISABLE_MWI */
  2349. /**
  2350. * pci_intx - enables/disables PCI INTx for device dev
  2351. * @pdev: the PCI device to operate on
  2352. * @enable: boolean: whether to enable or disable PCI INTx
  2353. *
  2354. * Enables/disables PCI INTx for device dev
  2355. */
  2356. void
  2357. pci_intx(struct pci_dev *pdev, int enable)
  2358. {
  2359. u16 pci_command, new;
  2360. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  2361. if (enable) {
  2362. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  2363. } else {
  2364. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  2365. }
  2366. if (new != pci_command) {
  2367. struct pci_devres *dr;
  2368. pci_write_config_word(pdev, PCI_COMMAND, new);
  2369. dr = find_pci_dr(pdev);
  2370. if (dr && !dr->restore_intx) {
  2371. dr->restore_intx = 1;
  2372. dr->orig_intx = !enable;
  2373. }
  2374. }
  2375. }
  2376. /**
  2377. * pci_msi_off - disables any msi or msix capabilities
  2378. * @dev: the PCI device to operate on
  2379. *
  2380. * If you want to use msi see pci_enable_msi and friends.
  2381. * This is a lower level primitive that allows us to disable
  2382. * msi operation at the device level.
  2383. */
  2384. void pci_msi_off(struct pci_dev *dev)
  2385. {
  2386. int pos;
  2387. u16 control;
  2388. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  2389. if (pos) {
  2390. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  2391. control &= ~PCI_MSI_FLAGS_ENABLE;
  2392. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  2393. }
  2394. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  2395. if (pos) {
  2396. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  2397. control &= ~PCI_MSIX_FLAGS_ENABLE;
  2398. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  2399. }
  2400. }
  2401. EXPORT_SYMBOL_GPL(pci_msi_off);
  2402. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  2403. {
  2404. return dma_set_max_seg_size(&dev->dev, size);
  2405. }
  2406. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  2407. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  2408. {
  2409. return dma_set_seg_boundary(&dev->dev, mask);
  2410. }
  2411. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  2412. static int pcie_flr(struct pci_dev *dev, int probe)
  2413. {
  2414. int i;
  2415. int pos;
  2416. u32 cap;
  2417. u16 status, control;
  2418. pos = pci_pcie_cap(dev);
  2419. if (!pos)
  2420. return -ENOTTY;
  2421. pci_read_config_dword(dev, pos + PCI_EXP_DEVCAP, &cap);
  2422. if (!(cap & PCI_EXP_DEVCAP_FLR))
  2423. return -ENOTTY;
  2424. if (probe)
  2425. return 0;
  2426. /* Wait for Transaction Pending bit clean */
  2427. for (i = 0; i < 4; i++) {
  2428. if (i)
  2429. msleep((1 << (i - 1)) * 100);
  2430. pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &status);
  2431. if (!(status & PCI_EXP_DEVSTA_TRPND))
  2432. goto clear;
  2433. }
  2434. dev_err(&dev->dev, "transaction is not cleared; "
  2435. "proceeding with reset anyway\n");
  2436. clear:
  2437. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &control);
  2438. control |= PCI_EXP_DEVCTL_BCR_FLR;
  2439. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, control);
  2440. msleep(100);
  2441. return 0;
  2442. }
  2443. static int pci_af_flr(struct pci_dev *dev, int probe)
  2444. {
  2445. int i;
  2446. int pos;
  2447. u8 cap;
  2448. u8 status;
  2449. pos = pci_find_capability(dev, PCI_CAP_ID_AF);
  2450. if (!pos)
  2451. return -ENOTTY;
  2452. pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
  2453. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  2454. return -ENOTTY;
  2455. if (probe)
  2456. return 0;
  2457. /* Wait for Transaction Pending bit clean */
  2458. for (i = 0; i < 4; i++) {
  2459. if (i)
  2460. msleep((1 << (i - 1)) * 100);
  2461. pci_read_config_byte(dev, pos + PCI_AF_STATUS, &status);
  2462. if (!(status & PCI_AF_STATUS_TP))
  2463. goto clear;
  2464. }
  2465. dev_err(&dev->dev, "transaction is not cleared; "
  2466. "proceeding with reset anyway\n");
  2467. clear:
  2468. pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  2469. msleep(100);
  2470. return 0;
  2471. }
  2472. /**
  2473. * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
  2474. * @dev: Device to reset.
  2475. * @probe: If set, only check if the device can be reset this way.
  2476. *
  2477. * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
  2478. * unset, it will be reinitialized internally when going from PCI_D3hot to
  2479. * PCI_D0. If that's the case and the device is not in a low-power state
  2480. * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
  2481. *
  2482. * NOTE: This causes the caller to sleep for twice the device power transition
  2483. * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
  2484. * by devault (i.e. unless the @dev's d3_delay field has a different value).
  2485. * Moreover, only devices in D0 can be reset by this function.
  2486. */
  2487. static int pci_pm_reset(struct pci_dev *dev, int probe)
  2488. {
  2489. u16 csr;
  2490. if (!dev->pm_cap)
  2491. return -ENOTTY;
  2492. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
  2493. if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
  2494. return -ENOTTY;
  2495. if (probe)
  2496. return 0;
  2497. if (dev->current_state != PCI_D0)
  2498. return -EINVAL;
  2499. csr &= ~PCI_PM_CTRL_STATE_MASK;
  2500. csr |= PCI_D3hot;
  2501. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  2502. pci_dev_d3_sleep(dev);
  2503. csr &= ~PCI_PM_CTRL_STATE_MASK;
  2504. csr |= PCI_D0;
  2505. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  2506. pci_dev_d3_sleep(dev);
  2507. return 0;
  2508. }
  2509. static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
  2510. {
  2511. u16 ctrl;
  2512. struct pci_dev *pdev;
  2513. if (pci_is_root_bus(dev->bus) || dev->subordinate || !dev->bus->self)
  2514. return -ENOTTY;
  2515. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  2516. if (pdev != dev)
  2517. return -ENOTTY;
  2518. if (probe)
  2519. return 0;
  2520. pci_read_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, &ctrl);
  2521. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  2522. pci_write_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, ctrl);
  2523. msleep(100);
  2524. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  2525. pci_write_config_word(dev->bus->self, PCI_BRIDGE_CONTROL, ctrl);
  2526. msleep(100);
  2527. return 0;
  2528. }
  2529. static int pci_dev_reset(struct pci_dev *dev, int probe)
  2530. {
  2531. int rc;
  2532. might_sleep();
  2533. if (!probe) {
  2534. pci_block_user_cfg_access(dev);
  2535. /* block PM suspend, driver probe, etc. */
  2536. device_lock(&dev->dev);
  2537. }
  2538. rc = pci_dev_specific_reset(dev, probe);
  2539. if (rc != -ENOTTY)
  2540. goto done;
  2541. rc = pcie_flr(dev, probe);
  2542. if (rc != -ENOTTY)
  2543. goto done;
  2544. rc = pci_af_flr(dev, probe);
  2545. if (rc != -ENOTTY)
  2546. goto done;
  2547. rc = pci_pm_reset(dev, probe);
  2548. if (rc != -ENOTTY)
  2549. goto done;
  2550. rc = pci_parent_bus_reset(dev, probe);
  2551. done:
  2552. if (!probe) {
  2553. device_unlock(&dev->dev);
  2554. pci_unblock_user_cfg_access(dev);
  2555. }
  2556. return rc;
  2557. }
  2558. /**
  2559. * __pci_reset_function - reset a PCI device function
  2560. * @dev: PCI device to reset
  2561. *
  2562. * Some devices allow an individual function to be reset without affecting
  2563. * other functions in the same device. The PCI device must be responsive
  2564. * to PCI config space in order to use this function.
  2565. *
  2566. * The device function is presumed to be unused when this function is called.
  2567. * Resetting the device will make the contents of PCI configuration space
  2568. * random, so any caller of this must be prepared to reinitialise the
  2569. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  2570. * etc.
  2571. *
  2572. * Returns 0 if the device function was successfully reset or negative if the
  2573. * device doesn't support resetting a single function.
  2574. */
  2575. int __pci_reset_function(struct pci_dev *dev)
  2576. {
  2577. return pci_dev_reset(dev, 0);
  2578. }
  2579. EXPORT_SYMBOL_GPL(__pci_reset_function);
  2580. /**
  2581. * pci_probe_reset_function - check whether the device can be safely reset
  2582. * @dev: PCI device to reset
  2583. *
  2584. * Some devices allow an individual function to be reset without affecting
  2585. * other functions in the same device. The PCI device must be responsive
  2586. * to PCI config space in order to use this function.
  2587. *
  2588. * Returns 0 if the device function can be reset or negative if the
  2589. * device doesn't support resetting a single function.
  2590. */
  2591. int pci_probe_reset_function(struct pci_dev *dev)
  2592. {
  2593. return pci_dev_reset(dev, 1);
  2594. }
  2595. /**
  2596. * pci_reset_function - quiesce and reset a PCI device function
  2597. * @dev: PCI device to reset
  2598. *
  2599. * Some devices allow an individual function to be reset without affecting
  2600. * other functions in the same device. The PCI device must be responsive
  2601. * to PCI config space in order to use this function.
  2602. *
  2603. * This function does not just reset the PCI portion of a device, but
  2604. * clears all the state associated with the device. This function differs
  2605. * from __pci_reset_function in that it saves and restores device state
  2606. * over the reset.
  2607. *
  2608. * Returns 0 if the device function was successfully reset or negative if the
  2609. * device doesn't support resetting a single function.
  2610. */
  2611. int pci_reset_function(struct pci_dev *dev)
  2612. {
  2613. int rc;
  2614. rc = pci_dev_reset(dev, 1);
  2615. if (rc)
  2616. return rc;
  2617. pci_save_state(dev);
  2618. /*
  2619. * both INTx and MSI are disabled after the Interrupt Disable bit
  2620. * is set and the Bus Master bit is cleared.
  2621. */
  2622. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  2623. rc = pci_dev_reset(dev, 0);
  2624. pci_restore_state(dev);
  2625. return rc;
  2626. }
  2627. EXPORT_SYMBOL_GPL(pci_reset_function);
  2628. /**
  2629. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  2630. * @dev: PCI device to query
  2631. *
  2632. * Returns mmrbc: maximum designed memory read count in bytes
  2633. * or appropriate error value.
  2634. */
  2635. int pcix_get_max_mmrbc(struct pci_dev *dev)
  2636. {
  2637. int cap;
  2638. u32 stat;
  2639. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  2640. if (!cap)
  2641. return -EINVAL;
  2642. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  2643. return -EINVAL;
  2644. return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
  2645. }
  2646. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  2647. /**
  2648. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  2649. * @dev: PCI device to query
  2650. *
  2651. * Returns mmrbc: maximum memory read count in bytes
  2652. * or appropriate error value.
  2653. */
  2654. int pcix_get_mmrbc(struct pci_dev *dev)
  2655. {
  2656. int cap;
  2657. u16 cmd;
  2658. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  2659. if (!cap)
  2660. return -EINVAL;
  2661. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  2662. return -EINVAL;
  2663. return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  2664. }
  2665. EXPORT_SYMBOL(pcix_get_mmrbc);
  2666. /**
  2667. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  2668. * @dev: PCI device to query
  2669. * @mmrbc: maximum memory read count in bytes
  2670. * valid values are 512, 1024, 2048, 4096
  2671. *
  2672. * If possible sets maximum memory read byte count, some bridges have erratas
  2673. * that prevent this.
  2674. */
  2675. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  2676. {
  2677. int cap;
  2678. u32 stat, v, o;
  2679. u16 cmd;
  2680. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  2681. return -EINVAL;
  2682. v = ffs(mmrbc) - 10;
  2683. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  2684. if (!cap)
  2685. return -EINVAL;
  2686. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  2687. return -EINVAL;
  2688. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  2689. return -E2BIG;
  2690. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  2691. return -EINVAL;
  2692. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  2693. if (o != v) {
  2694. if (v > o && dev->bus &&
  2695. (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  2696. return -EIO;
  2697. cmd &= ~PCI_X_CMD_MAX_READ;
  2698. cmd |= v << 2;
  2699. if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
  2700. return -EIO;
  2701. }
  2702. return 0;
  2703. }
  2704. EXPORT_SYMBOL(pcix_set_mmrbc);
  2705. /**
  2706. * pcie_get_readrq - get PCI Express read request size
  2707. * @dev: PCI device to query
  2708. *
  2709. * Returns maximum memory read request in bytes
  2710. * or appropriate error value.
  2711. */
  2712. int pcie_get_readrq(struct pci_dev *dev)
  2713. {
  2714. int ret, cap;
  2715. u16 ctl;
  2716. cap = pci_pcie_cap(dev);
  2717. if (!cap)
  2718. return -EINVAL;
  2719. ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  2720. if (!ret)
  2721. ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  2722. return ret;
  2723. }
  2724. EXPORT_SYMBOL(pcie_get_readrq);
  2725. /**
  2726. * pcie_set_readrq - set PCI Express maximum memory read request
  2727. * @dev: PCI device to query
  2728. * @rq: maximum memory read count in bytes
  2729. * valid values are 128, 256, 512, 1024, 2048, 4096
  2730. *
  2731. * If possible sets maximum memory read request in bytes
  2732. */
  2733. int pcie_set_readrq(struct pci_dev *dev, int rq)
  2734. {
  2735. int cap, err = -EINVAL;
  2736. u16 ctl, v;
  2737. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  2738. goto out;
  2739. cap = pci_pcie_cap(dev);
  2740. if (!cap)
  2741. goto out;
  2742. err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  2743. if (err)
  2744. goto out;
  2745. /*
  2746. * If using the "performance" PCIe config, we clamp the
  2747. * read rq size to the max packet size to prevent the
  2748. * host bridge generating requests larger than we can
  2749. * cope with
  2750. */
  2751. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  2752. int mps = pcie_get_mps(dev);
  2753. if (mps < 0)
  2754. return mps;
  2755. if (mps < rq)
  2756. rq = mps;
  2757. }
  2758. v = (ffs(rq) - 8) << 12;
  2759. if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
  2760. ctl &= ~PCI_EXP_DEVCTL_READRQ;
  2761. ctl |= v;
  2762. err = pci_write_config_word(dev, cap + PCI_EXP_DEVCTL, ctl);
  2763. }
  2764. out:
  2765. return err;
  2766. }
  2767. EXPORT_SYMBOL(pcie_set_readrq);
  2768. /**
  2769. * pcie_get_mps - get PCI Express maximum payload size
  2770. * @dev: PCI device to query
  2771. *
  2772. * Returns maximum payload size in bytes
  2773. * or appropriate error value.
  2774. */
  2775. int pcie_get_mps(struct pci_dev *dev)
  2776. {
  2777. int ret, cap;
  2778. u16 ctl;
  2779. cap = pci_pcie_cap(dev);
  2780. if (!cap)
  2781. return -EINVAL;
  2782. ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  2783. if (!ret)
  2784. ret = 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
  2785. return ret;
  2786. }
  2787. /**
  2788. * pcie_set_mps - set PCI Express maximum payload size
  2789. * @dev: PCI device to query
  2790. * @mps: maximum payload size in bytes
  2791. * valid values are 128, 256, 512, 1024, 2048, 4096
  2792. *
  2793. * If possible sets maximum payload size
  2794. */
  2795. int pcie_set_mps(struct pci_dev *dev, int mps)
  2796. {
  2797. int cap, err = -EINVAL;
  2798. u16 ctl, v;
  2799. if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
  2800. goto out;
  2801. v = ffs(mps) - 8;
  2802. if (v > dev->pcie_mpss)
  2803. goto out;
  2804. v <<= 5;
  2805. cap = pci_pcie_cap(dev);
  2806. if (!cap)
  2807. goto out;
  2808. err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  2809. if (err)
  2810. goto out;
  2811. if ((ctl & PCI_EXP_DEVCTL_PAYLOAD) != v) {
  2812. ctl &= ~PCI_EXP_DEVCTL_PAYLOAD;
  2813. ctl |= v;
  2814. err = pci_write_config_word(dev, cap + PCI_EXP_DEVCTL, ctl);
  2815. }
  2816. out:
  2817. return err;
  2818. }
  2819. /**
  2820. * pci_select_bars - Make BAR mask from the type of resource
  2821. * @dev: the PCI device for which BAR mask is made
  2822. * @flags: resource type mask to be selected
  2823. *
  2824. * This helper routine makes bar mask from the type of resource.
  2825. */
  2826. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  2827. {
  2828. int i, bars = 0;
  2829. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  2830. if (pci_resource_flags(dev, i) & flags)
  2831. bars |= (1 << i);
  2832. return bars;
  2833. }
  2834. /**
  2835. * pci_resource_bar - get position of the BAR associated with a resource
  2836. * @dev: the PCI device
  2837. * @resno: the resource number
  2838. * @type: the BAR type to be filled in
  2839. *
  2840. * Returns BAR position in config space, or 0 if the BAR is invalid.
  2841. */
  2842. int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
  2843. {
  2844. int reg;
  2845. if (resno < PCI_ROM_RESOURCE) {
  2846. *type = pci_bar_unknown;
  2847. return PCI_BASE_ADDRESS_0 + 4 * resno;
  2848. } else if (resno == PCI_ROM_RESOURCE) {
  2849. *type = pci_bar_mem32;
  2850. return dev->rom_base_reg;
  2851. } else if (resno < PCI_BRIDGE_RESOURCES) {
  2852. /* device specific resource */
  2853. reg = pci_iov_resource_bar(dev, resno, type);
  2854. if (reg)
  2855. return reg;
  2856. }
  2857. dev_err(&dev->dev, "BAR %d: invalid resource\n", resno);
  2858. return 0;
  2859. }
  2860. /* Some architectures require additional programming to enable VGA */
  2861. static arch_set_vga_state_t arch_set_vga_state;
  2862. void __init pci_register_set_vga_state(arch_set_vga_state_t func)
  2863. {
  2864. arch_set_vga_state = func; /* NULL disables */
  2865. }
  2866. static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
  2867. unsigned int command_bits, u32 flags)
  2868. {
  2869. if (arch_set_vga_state)
  2870. return arch_set_vga_state(dev, decode, command_bits,
  2871. flags);
  2872. return 0;
  2873. }
  2874. /**
  2875. * pci_set_vga_state - set VGA decode state on device and parents if requested
  2876. * @dev: the PCI device
  2877. * @decode: true = enable decoding, false = disable decoding
  2878. * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
  2879. * @flags: traverse ancestors and change bridges
  2880. * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
  2881. */
  2882. int pci_set_vga_state(struct pci_dev *dev, bool decode,
  2883. unsigned int command_bits, u32 flags)
  2884. {
  2885. struct pci_bus *bus;
  2886. struct pci_dev *bridge;
  2887. u16 cmd;
  2888. int rc;
  2889. WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) & (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
  2890. /* ARCH specific VGA enables */
  2891. rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
  2892. if (rc)
  2893. return rc;
  2894. if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
  2895. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2896. if (decode == true)
  2897. cmd |= command_bits;
  2898. else
  2899. cmd &= ~command_bits;
  2900. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2901. }
  2902. if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
  2903. return 0;
  2904. bus = dev->bus;
  2905. while (bus) {
  2906. bridge = bus->self;
  2907. if (bridge) {
  2908. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
  2909. &cmd);
  2910. if (decode == true)
  2911. cmd |= PCI_BRIDGE_CTL_VGA;
  2912. else
  2913. cmd &= ~PCI_BRIDGE_CTL_VGA;
  2914. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
  2915. cmd);
  2916. }
  2917. bus = bus->parent;
  2918. }
  2919. return 0;
  2920. }
  2921. #define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
  2922. static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
  2923. static DEFINE_SPINLOCK(resource_alignment_lock);
  2924. /**
  2925. * pci_specified_resource_alignment - get resource alignment specified by user.
  2926. * @dev: the PCI device to get
  2927. *
  2928. * RETURNS: Resource alignment if it is specified.
  2929. * Zero if it is not specified.
  2930. */
  2931. resource_size_t pci_specified_resource_alignment(struct pci_dev *dev)
  2932. {
  2933. int seg, bus, slot, func, align_order, count;
  2934. resource_size_t align = 0;
  2935. char *p;
  2936. spin_lock(&resource_alignment_lock);
  2937. p = resource_alignment_param;
  2938. while (*p) {
  2939. count = 0;
  2940. if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
  2941. p[count] == '@') {
  2942. p += count + 1;
  2943. } else {
  2944. align_order = -1;
  2945. }
  2946. if (sscanf(p, "%x:%x:%x.%x%n",
  2947. &seg, &bus, &slot, &func, &count) != 4) {
  2948. seg = 0;
  2949. if (sscanf(p, "%x:%x.%x%n",
  2950. &bus, &slot, &func, &count) != 3) {
  2951. /* Invalid format */
  2952. printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
  2953. p);
  2954. break;
  2955. }
  2956. }
  2957. p += count;
  2958. if (seg == pci_domain_nr(dev->bus) &&
  2959. bus == dev->bus->number &&
  2960. slot == PCI_SLOT(dev->devfn) &&
  2961. func == PCI_FUNC(dev->devfn)) {
  2962. if (align_order == -1) {
  2963. align = PAGE_SIZE;
  2964. } else {
  2965. align = 1 << align_order;
  2966. }
  2967. /* Found */
  2968. break;
  2969. }
  2970. if (*p != ';' && *p != ',') {
  2971. /* End of param or invalid format */
  2972. break;
  2973. }
  2974. p++;
  2975. }
  2976. spin_unlock(&resource_alignment_lock);
  2977. return align;
  2978. }
  2979. /**
  2980. * pci_is_reassigndev - check if specified PCI is target device to reassign
  2981. * @dev: the PCI device to check
  2982. *
  2983. * RETURNS: non-zero for PCI device is a target device to reassign,
  2984. * or zero is not.
  2985. */
  2986. int pci_is_reassigndev(struct pci_dev *dev)
  2987. {
  2988. return (pci_specified_resource_alignment(dev) != 0);
  2989. }
  2990. ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
  2991. {
  2992. if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
  2993. count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
  2994. spin_lock(&resource_alignment_lock);
  2995. strncpy(resource_alignment_param, buf, count);
  2996. resource_alignment_param[count] = '\0';
  2997. spin_unlock(&resource_alignment_lock);
  2998. return count;
  2999. }
  3000. ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
  3001. {
  3002. size_t count;
  3003. spin_lock(&resource_alignment_lock);
  3004. count = snprintf(buf, size, "%s", resource_alignment_param);
  3005. spin_unlock(&resource_alignment_lock);
  3006. return count;
  3007. }
  3008. static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
  3009. {
  3010. return pci_get_resource_alignment_param(buf, PAGE_SIZE);
  3011. }
  3012. static ssize_t pci_resource_alignment_store(struct bus_type *bus,
  3013. const char *buf, size_t count)
  3014. {
  3015. return pci_set_resource_alignment_param(buf, count);
  3016. }
  3017. BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
  3018. pci_resource_alignment_store);
  3019. static int __init pci_resource_alignment_sysfs_init(void)
  3020. {
  3021. return bus_create_file(&pci_bus_type,
  3022. &bus_attr_resource_alignment);
  3023. }
  3024. late_initcall(pci_resource_alignment_sysfs_init);
  3025. static void __devinit pci_no_domains(void)
  3026. {
  3027. #ifdef CONFIG_PCI_DOMAINS
  3028. pci_domains_supported = 0;
  3029. #endif
  3030. }
  3031. /**
  3032. * pci_ext_cfg_enabled - can we access extended PCI config space?
  3033. * @dev: The PCI device of the root bridge.
  3034. *
  3035. * Returns 1 if we can access PCI extended config space (offsets
  3036. * greater than 0xff). This is the default implementation. Architecture
  3037. * implementations can override this.
  3038. */
  3039. int __attribute__ ((weak)) pci_ext_cfg_avail(struct pci_dev *dev)
  3040. {
  3041. return 1;
  3042. }
  3043. void __weak pci_fixup_cardbus(struct pci_bus *bus)
  3044. {
  3045. }
  3046. EXPORT_SYMBOL(pci_fixup_cardbus);
  3047. static int __init pci_setup(char *str)
  3048. {
  3049. while (str) {
  3050. char *k = strchr(str, ',');
  3051. if (k)
  3052. *k++ = 0;
  3053. if (*str && (str = pcibios_setup(str)) && *str) {
  3054. if (!strcmp(str, "nomsi")) {
  3055. pci_no_msi();
  3056. } else if (!strcmp(str, "noaer")) {
  3057. pci_no_aer();
  3058. } else if (!strncmp(str, "realloc", 7)) {
  3059. pci_realloc();
  3060. } else if (!strcmp(str, "nodomains")) {
  3061. pci_no_domains();
  3062. } else if (!strncmp(str, "cbiosize=", 9)) {
  3063. pci_cardbus_io_size = memparse(str + 9, &str);
  3064. } else if (!strncmp(str, "cbmemsize=", 10)) {
  3065. pci_cardbus_mem_size = memparse(str + 10, &str);
  3066. } else if (!strncmp(str, "resource_alignment=", 19)) {
  3067. pci_set_resource_alignment_param(str + 19,
  3068. strlen(str + 19));
  3069. } else if (!strncmp(str, "ecrc=", 5)) {
  3070. pcie_ecrc_get_policy(str + 5);
  3071. } else if (!strncmp(str, "hpiosize=", 9)) {
  3072. pci_hotplug_io_size = memparse(str + 9, &str);
  3073. } else if (!strncmp(str, "hpmemsize=", 10)) {
  3074. pci_hotplug_mem_size = memparse(str + 10, &str);
  3075. } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
  3076. pcie_bus_config = PCIE_BUS_TUNE_OFF;
  3077. } else if (!strncmp(str, "pcie_bus_safe", 13)) {
  3078. pcie_bus_config = PCIE_BUS_SAFE;
  3079. } else if (!strncmp(str, "pcie_bus_perf", 13)) {
  3080. pcie_bus_config = PCIE_BUS_PERFORMANCE;
  3081. } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
  3082. pcie_bus_config = PCIE_BUS_PEER2PEER;
  3083. } else {
  3084. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  3085. str);
  3086. }
  3087. }
  3088. str = k;
  3089. }
  3090. return 0;
  3091. }
  3092. early_param("pci", pci_setup);
  3093. EXPORT_SYMBOL(pci_reenable_device);
  3094. EXPORT_SYMBOL(pci_enable_device_io);
  3095. EXPORT_SYMBOL(pci_enable_device_mem);
  3096. EXPORT_SYMBOL(pci_enable_device);
  3097. EXPORT_SYMBOL(pcim_enable_device);
  3098. EXPORT_SYMBOL(pcim_pin_device);
  3099. EXPORT_SYMBOL(pci_disable_device);
  3100. EXPORT_SYMBOL(pci_find_capability);
  3101. EXPORT_SYMBOL(pci_bus_find_capability);
  3102. EXPORT_SYMBOL(pci_release_regions);
  3103. EXPORT_SYMBOL(pci_request_regions);
  3104. EXPORT_SYMBOL(pci_request_regions_exclusive);
  3105. EXPORT_SYMBOL(pci_release_region);
  3106. EXPORT_SYMBOL(pci_request_region);
  3107. EXPORT_SYMBOL(pci_request_region_exclusive);
  3108. EXPORT_SYMBOL(pci_release_selected_regions);
  3109. EXPORT_SYMBOL(pci_request_selected_regions);
  3110. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  3111. EXPORT_SYMBOL(pci_set_master);
  3112. EXPORT_SYMBOL(pci_clear_master);
  3113. EXPORT_SYMBOL(pci_set_mwi);
  3114. EXPORT_SYMBOL(pci_try_set_mwi);
  3115. EXPORT_SYMBOL(pci_clear_mwi);
  3116. EXPORT_SYMBOL_GPL(pci_intx);
  3117. EXPORT_SYMBOL(pci_assign_resource);
  3118. EXPORT_SYMBOL(pci_find_parent_resource);
  3119. EXPORT_SYMBOL(pci_select_bars);
  3120. EXPORT_SYMBOL(pci_set_power_state);
  3121. EXPORT_SYMBOL(pci_save_state);
  3122. EXPORT_SYMBOL(pci_restore_state);
  3123. EXPORT_SYMBOL(pci_pme_capable);
  3124. EXPORT_SYMBOL(pci_pme_active);
  3125. EXPORT_SYMBOL(pci_wake_from_d3);
  3126. EXPORT_SYMBOL(pci_target_state);
  3127. EXPORT_SYMBOL(pci_prepare_to_sleep);
  3128. EXPORT_SYMBOL(pci_back_from_sleep);
  3129. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);