pci.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454
  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 <linux/pci_hotplug.h>
  25. #include <asm-generic/pci-bridge.h>
  26. #include <asm/setup.h>
  27. #include "pci.h"
  28. const char *pci_power_names[] = {
  29. "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
  30. };
  31. EXPORT_SYMBOL_GPL(pci_power_names);
  32. int isa_dma_bridge_buggy;
  33. EXPORT_SYMBOL(isa_dma_bridge_buggy);
  34. int pci_pci_problems;
  35. EXPORT_SYMBOL(pci_pci_problems);
  36. unsigned int pci_pm_d3_delay;
  37. static void pci_pme_list_scan(struct work_struct *work);
  38. static LIST_HEAD(pci_pme_list);
  39. static DEFINE_MUTEX(pci_pme_list_mutex);
  40. static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
  41. struct pci_pme_device {
  42. struct list_head list;
  43. struct pci_dev *dev;
  44. };
  45. #define PME_TIMEOUT 1000 /* How long between PME checks */
  46. static void pci_dev_d3_sleep(struct pci_dev *dev)
  47. {
  48. unsigned int delay = dev->d3_delay;
  49. if (delay < pci_pm_d3_delay)
  50. delay = pci_pm_d3_delay;
  51. msleep(delay);
  52. }
  53. #ifdef CONFIG_PCI_DOMAINS
  54. int pci_domains_supported = 1;
  55. #endif
  56. #define DEFAULT_CARDBUS_IO_SIZE (256)
  57. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  58. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  59. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  60. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  61. #define DEFAULT_HOTPLUG_IO_SIZE (256)
  62. #define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
  63. /* pci=hpmemsize=nnM,hpiosize=nn can override this */
  64. unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
  65. unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
  66. enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_TUNE_OFF;
  67. /*
  68. * The default CLS is used if arch didn't set CLS explicitly and not
  69. * all pci devices agree on the same value. Arch can override either
  70. * the dfl or actual value as it sees fit. Don't forget this is
  71. * measured in 32-bit words, not bytes.
  72. */
  73. u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
  74. u8 pci_cache_line_size;
  75. /*
  76. * If we set up a device for bus mastering, we need to check the latency
  77. * timer as certain BIOSes forget to set it properly.
  78. */
  79. unsigned int pcibios_max_latency = 255;
  80. /* If set, the PCIe ARI capability will not be used. */
  81. static bool pcie_ari_disabled;
  82. /**
  83. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  84. * @bus: pointer to PCI bus structure to search
  85. *
  86. * Given a PCI bus, returns the highest PCI bus number present in the set
  87. * including the given PCI bus and its list of child PCI buses.
  88. */
  89. unsigned char pci_bus_max_busnr(struct pci_bus *bus)
  90. {
  91. struct pci_bus *tmp;
  92. unsigned char max, n;
  93. max = bus->busn_res.end;
  94. list_for_each_entry(tmp, &bus->children, node) {
  95. n = pci_bus_max_busnr(tmp);
  96. if(n > max)
  97. max = n;
  98. }
  99. return max;
  100. }
  101. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  102. #ifdef CONFIG_HAS_IOMEM
  103. void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
  104. {
  105. /*
  106. * Make sure the BAR is actually a memory resource, not an IO resource
  107. */
  108. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  109. WARN_ON(1);
  110. return NULL;
  111. }
  112. return ioremap_nocache(pci_resource_start(pdev, bar),
  113. pci_resource_len(pdev, bar));
  114. }
  115. EXPORT_SYMBOL_GPL(pci_ioremap_bar);
  116. #endif
  117. #define PCI_FIND_CAP_TTL 48
  118. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  119. u8 pos, int cap, int *ttl)
  120. {
  121. u8 id;
  122. while ((*ttl)--) {
  123. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  124. if (pos < 0x40)
  125. break;
  126. pos &= ~3;
  127. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  128. &id);
  129. if (id == 0xff)
  130. break;
  131. if (id == cap)
  132. return pos;
  133. pos += PCI_CAP_LIST_NEXT;
  134. }
  135. return 0;
  136. }
  137. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  138. u8 pos, int cap)
  139. {
  140. int ttl = PCI_FIND_CAP_TTL;
  141. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  142. }
  143. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  144. {
  145. return __pci_find_next_cap(dev->bus, dev->devfn,
  146. pos + PCI_CAP_LIST_NEXT, cap);
  147. }
  148. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  149. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  150. unsigned int devfn, u8 hdr_type)
  151. {
  152. u16 status;
  153. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  154. if (!(status & PCI_STATUS_CAP_LIST))
  155. return 0;
  156. switch (hdr_type) {
  157. case PCI_HEADER_TYPE_NORMAL:
  158. case PCI_HEADER_TYPE_BRIDGE:
  159. return PCI_CAPABILITY_LIST;
  160. case PCI_HEADER_TYPE_CARDBUS:
  161. return PCI_CB_CAPABILITY_LIST;
  162. default:
  163. return 0;
  164. }
  165. return 0;
  166. }
  167. /**
  168. * pci_find_capability - query for devices' capabilities
  169. * @dev: PCI device to query
  170. * @cap: capability code
  171. *
  172. * Tell if a device supports a given PCI capability.
  173. * Returns the address of the requested capability structure within the
  174. * device's PCI configuration space or 0 in case the device does not
  175. * support it. Possible values for @cap:
  176. *
  177. * %PCI_CAP_ID_PM Power Management
  178. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  179. * %PCI_CAP_ID_VPD Vital Product Data
  180. * %PCI_CAP_ID_SLOTID Slot Identification
  181. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  182. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  183. * %PCI_CAP_ID_PCIX PCI-X
  184. * %PCI_CAP_ID_EXP PCI Express
  185. */
  186. int pci_find_capability(struct pci_dev *dev, int cap)
  187. {
  188. int pos;
  189. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  190. if (pos)
  191. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  192. return pos;
  193. }
  194. /**
  195. * pci_bus_find_capability - query for devices' capabilities
  196. * @bus: the PCI bus to query
  197. * @devfn: PCI device to query
  198. * @cap: capability code
  199. *
  200. * Like pci_find_capability() but works for pci devices that do not have a
  201. * pci_dev structure set up yet.
  202. *
  203. * Returns the address of the requested capability structure within the
  204. * device's PCI configuration space or 0 in case the device does not
  205. * support it.
  206. */
  207. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  208. {
  209. int pos;
  210. u8 hdr_type;
  211. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  212. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  213. if (pos)
  214. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  215. return pos;
  216. }
  217. /**
  218. * pci_find_next_ext_capability - Find an extended capability
  219. * @dev: PCI device to query
  220. * @start: address at which to start looking (0 to start at beginning of list)
  221. * @cap: capability code
  222. *
  223. * Returns the address of the next matching extended capability structure
  224. * within the device's PCI configuration space or 0 if the device does
  225. * not support it. Some capabilities can occur several times, e.g., the
  226. * vendor-specific capability, and this provides a way to find them all.
  227. */
  228. int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
  229. {
  230. u32 header;
  231. int ttl;
  232. int pos = PCI_CFG_SPACE_SIZE;
  233. /* minimum 8 bytes per capability */
  234. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  235. if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
  236. return 0;
  237. if (start)
  238. pos = start;
  239. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  240. return 0;
  241. /*
  242. * If we have no capabilities, this is indicated by cap ID,
  243. * cap version and next pointer all being 0.
  244. */
  245. if (header == 0)
  246. return 0;
  247. while (ttl-- > 0) {
  248. if (PCI_EXT_CAP_ID(header) == cap && pos != start)
  249. return pos;
  250. pos = PCI_EXT_CAP_NEXT(header);
  251. if (pos < PCI_CFG_SPACE_SIZE)
  252. break;
  253. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  254. break;
  255. }
  256. return 0;
  257. }
  258. EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
  259. /**
  260. * pci_find_ext_capability - Find an extended capability
  261. * @dev: PCI device to query
  262. * @cap: capability code
  263. *
  264. * Returns the address of the requested extended capability structure
  265. * within the device's PCI configuration space or 0 if the device does
  266. * not support it. Possible values for @cap:
  267. *
  268. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  269. * %PCI_EXT_CAP_ID_VC Virtual Channel
  270. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  271. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  272. */
  273. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  274. {
  275. return pci_find_next_ext_capability(dev, 0, cap);
  276. }
  277. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  278. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  279. {
  280. int rc, ttl = PCI_FIND_CAP_TTL;
  281. u8 cap, mask;
  282. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  283. mask = HT_3BIT_CAP_MASK;
  284. else
  285. mask = HT_5BIT_CAP_MASK;
  286. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  287. PCI_CAP_ID_HT, &ttl);
  288. while (pos) {
  289. rc = pci_read_config_byte(dev, pos + 3, &cap);
  290. if (rc != PCIBIOS_SUCCESSFUL)
  291. return 0;
  292. if ((cap & mask) == ht_cap)
  293. return pos;
  294. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  295. pos + PCI_CAP_LIST_NEXT,
  296. PCI_CAP_ID_HT, &ttl);
  297. }
  298. return 0;
  299. }
  300. /**
  301. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  302. * @dev: PCI device to query
  303. * @pos: Position from which to continue searching
  304. * @ht_cap: Hypertransport capability code
  305. *
  306. * To be used in conjunction with pci_find_ht_capability() to search for
  307. * all capabilities matching @ht_cap. @pos should always be a value returned
  308. * from pci_find_ht_capability().
  309. *
  310. * NB. To be 100% safe against broken PCI devices, the caller should take
  311. * steps to avoid an infinite loop.
  312. */
  313. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  314. {
  315. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  316. }
  317. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  318. /**
  319. * pci_find_ht_capability - query a device's Hypertransport capabilities
  320. * @dev: PCI device to query
  321. * @ht_cap: Hypertransport capability code
  322. *
  323. * Tell if a device supports a given Hypertransport capability.
  324. * Returns an address within the device's PCI configuration space
  325. * or 0 in case the device does not support the request capability.
  326. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  327. * which has a Hypertransport capability matching @ht_cap.
  328. */
  329. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  330. {
  331. int pos;
  332. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  333. if (pos)
  334. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  335. return pos;
  336. }
  337. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  338. /**
  339. * pci_find_parent_resource - return resource region of parent bus of given region
  340. * @dev: PCI device structure contains resources to be searched
  341. * @res: child resource record for which parent is sought
  342. *
  343. * For given resource region of given device, return the resource
  344. * region of parent bus the given region is contained in.
  345. */
  346. struct resource *
  347. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  348. {
  349. const struct pci_bus *bus = dev->bus;
  350. struct resource *r;
  351. int i;
  352. pci_bus_for_each_resource(bus, r, i) {
  353. if (!r)
  354. continue;
  355. if (res->start && resource_contains(r, res)) {
  356. /*
  357. * If the window is prefetchable but the BAR is
  358. * not, the allocator made a mistake.
  359. */
  360. if (r->flags & IORESOURCE_PREFETCH &&
  361. !(res->flags & IORESOURCE_PREFETCH))
  362. return NULL;
  363. /*
  364. * If we're below a transparent bridge, there may
  365. * be both a positively-decoded aperture and a
  366. * subtractively-decoded region that contain the BAR.
  367. * We want the positively-decoded one, so this depends
  368. * on pci_bus_for_each_resource() giving us those
  369. * first.
  370. */
  371. return r;
  372. }
  373. }
  374. return NULL;
  375. }
  376. /**
  377. * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
  378. * @dev: the PCI device to operate on
  379. * @pos: config space offset of status word
  380. * @mask: mask of bit(s) to care about in status word
  381. *
  382. * Return 1 when mask bit(s) in status word clear, 0 otherwise.
  383. */
  384. int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
  385. {
  386. int i;
  387. /* Wait for Transaction Pending bit clean */
  388. for (i = 0; i < 4; i++) {
  389. u16 status;
  390. if (i)
  391. msleep((1 << (i - 1)) * 100);
  392. pci_read_config_word(dev, pos, &status);
  393. if (!(status & mask))
  394. return 1;
  395. }
  396. return 0;
  397. }
  398. /**
  399. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  400. * @dev: PCI device to have its BARs restored
  401. *
  402. * Restore the BAR values for a given device, so as to make it
  403. * accessible by its driver.
  404. */
  405. static void
  406. pci_restore_bars(struct pci_dev *dev)
  407. {
  408. int i;
  409. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
  410. pci_update_resource(dev, i);
  411. }
  412. static struct pci_platform_pm_ops *pci_platform_pm;
  413. int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
  414. {
  415. if (!ops->is_manageable || !ops->set_state || !ops->choose_state
  416. || !ops->sleep_wake)
  417. return -EINVAL;
  418. pci_platform_pm = ops;
  419. return 0;
  420. }
  421. static inline bool platform_pci_power_manageable(struct pci_dev *dev)
  422. {
  423. return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
  424. }
  425. static inline int platform_pci_set_power_state(struct pci_dev *dev,
  426. pci_power_t t)
  427. {
  428. return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
  429. }
  430. static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
  431. {
  432. return pci_platform_pm ?
  433. pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
  434. }
  435. static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
  436. {
  437. return pci_platform_pm ?
  438. pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
  439. }
  440. static inline int platform_pci_run_wake(struct pci_dev *dev, bool enable)
  441. {
  442. return pci_platform_pm ?
  443. pci_platform_pm->run_wake(dev, enable) : -ENODEV;
  444. }
  445. /**
  446. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  447. * given PCI device
  448. * @dev: PCI device to handle.
  449. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  450. *
  451. * RETURN VALUE:
  452. * -EINVAL if the requested state is invalid.
  453. * -EIO if device does not support PCI PM or its PM capabilities register has a
  454. * wrong version, or device doesn't support the requested state.
  455. * 0 if device already is in the requested state.
  456. * 0 if device's power state has been successfully changed.
  457. */
  458. static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
  459. {
  460. u16 pmcsr;
  461. bool need_restore = false;
  462. /* Check if we're already there */
  463. if (dev->current_state == state)
  464. return 0;
  465. if (!dev->pm_cap)
  466. return -EIO;
  467. if (state < PCI_D0 || state > PCI_D3hot)
  468. return -EINVAL;
  469. /* Validate current state:
  470. * Can enter D0 from any state, but if we can only go deeper
  471. * to sleep if we're already in a low power state
  472. */
  473. if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  474. && dev->current_state > state) {
  475. dev_err(&dev->dev, "invalid power transition "
  476. "(from state %d to %d)\n", dev->current_state, state);
  477. return -EINVAL;
  478. }
  479. /* check if this device supports the desired state */
  480. if ((state == PCI_D1 && !dev->d1_support)
  481. || (state == PCI_D2 && !dev->d2_support))
  482. return -EIO;
  483. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  484. /* If we're (effectively) in D3, force entire word to 0.
  485. * This doesn't affect PME_Status, disables PME_En, and
  486. * sets PowerState to 0.
  487. */
  488. switch (dev->current_state) {
  489. case PCI_D0:
  490. case PCI_D1:
  491. case PCI_D2:
  492. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  493. pmcsr |= state;
  494. break;
  495. case PCI_D3hot:
  496. case PCI_D3cold:
  497. case PCI_UNKNOWN: /* Boot-up */
  498. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  499. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  500. need_restore = true;
  501. /* Fall-through: force to D0 */
  502. default:
  503. pmcsr = 0;
  504. break;
  505. }
  506. /* enter specified state */
  507. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  508. /* Mandatory power management transition delays */
  509. /* see PCI PM 1.1 5.6.1 table 18 */
  510. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  511. pci_dev_d3_sleep(dev);
  512. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  513. udelay(PCI_PM_D2_DELAY);
  514. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  515. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  516. if (dev->current_state != state && printk_ratelimit())
  517. dev_info(&dev->dev, "Refused to change power state, "
  518. "currently in D%d\n", dev->current_state);
  519. /*
  520. * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  521. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  522. * from D3hot to D0 _may_ perform an internal reset, thereby
  523. * going to "D0 Uninitialized" rather than "D0 Initialized".
  524. * For example, at least some versions of the 3c905B and the
  525. * 3c556B exhibit this behaviour.
  526. *
  527. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  528. * devices in a D3hot state at boot. Consequently, we need to
  529. * restore at least the BARs so that the device will be
  530. * accessible to its driver.
  531. */
  532. if (need_restore)
  533. pci_restore_bars(dev);
  534. if (dev->bus->self)
  535. pcie_aspm_pm_state_change(dev->bus->self);
  536. return 0;
  537. }
  538. /**
  539. * pci_update_current_state - Read PCI power state of given device from its
  540. * PCI PM registers and cache it
  541. * @dev: PCI device to handle.
  542. * @state: State to cache in case the device doesn't have the PM capability
  543. */
  544. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  545. {
  546. if (dev->pm_cap) {
  547. u16 pmcsr;
  548. /*
  549. * Configuration space is not accessible for device in
  550. * D3cold, so just keep or set D3cold for safety
  551. */
  552. if (dev->current_state == PCI_D3cold)
  553. return;
  554. if (state == PCI_D3cold) {
  555. dev->current_state = PCI_D3cold;
  556. return;
  557. }
  558. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  559. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  560. } else {
  561. dev->current_state = state;
  562. }
  563. }
  564. /**
  565. * pci_power_up - Put the given device into D0 forcibly
  566. * @dev: PCI device to power up
  567. */
  568. void pci_power_up(struct pci_dev *dev)
  569. {
  570. if (platform_pci_power_manageable(dev))
  571. platform_pci_set_power_state(dev, PCI_D0);
  572. pci_raw_set_power_state(dev, PCI_D0);
  573. pci_update_current_state(dev, PCI_D0);
  574. }
  575. /**
  576. * pci_platform_power_transition - Use platform to change device power state
  577. * @dev: PCI device to handle.
  578. * @state: State to put the device into.
  579. */
  580. static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
  581. {
  582. int error;
  583. if (platform_pci_power_manageable(dev)) {
  584. error = platform_pci_set_power_state(dev, state);
  585. if (!error)
  586. pci_update_current_state(dev, state);
  587. } else
  588. error = -ENODEV;
  589. if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
  590. dev->current_state = PCI_D0;
  591. return error;
  592. }
  593. /**
  594. * pci_wakeup - Wake up a PCI device
  595. * @pci_dev: Device to handle.
  596. * @ign: ignored parameter
  597. */
  598. static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
  599. {
  600. pci_wakeup_event(pci_dev);
  601. pm_request_resume(&pci_dev->dev);
  602. return 0;
  603. }
  604. /**
  605. * pci_wakeup_bus - Walk given bus and wake up devices on it
  606. * @bus: Top bus of the subtree to walk.
  607. */
  608. static void pci_wakeup_bus(struct pci_bus *bus)
  609. {
  610. if (bus)
  611. pci_walk_bus(bus, pci_wakeup, NULL);
  612. }
  613. /**
  614. * __pci_start_power_transition - Start power transition of a PCI device
  615. * @dev: PCI device to handle.
  616. * @state: State to put the device into.
  617. */
  618. static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
  619. {
  620. if (state == PCI_D0) {
  621. pci_platform_power_transition(dev, PCI_D0);
  622. /*
  623. * Mandatory power management transition delays, see
  624. * PCI Express Base Specification Revision 2.0 Section
  625. * 6.6.1: Conventional Reset. Do not delay for
  626. * devices powered on/off by corresponding bridge,
  627. * because have already delayed for the bridge.
  628. */
  629. if (dev->runtime_d3cold) {
  630. msleep(dev->d3cold_delay);
  631. /*
  632. * When powering on a bridge from D3cold, the
  633. * whole hierarchy may be powered on into
  634. * D0uninitialized state, resume them to give
  635. * them a chance to suspend again
  636. */
  637. pci_wakeup_bus(dev->subordinate);
  638. }
  639. }
  640. }
  641. /**
  642. * __pci_dev_set_current_state - Set current state of a PCI device
  643. * @dev: Device to handle
  644. * @data: pointer to state to be set
  645. */
  646. static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
  647. {
  648. pci_power_t state = *(pci_power_t *)data;
  649. dev->current_state = state;
  650. return 0;
  651. }
  652. /**
  653. * __pci_bus_set_current_state - Walk given bus and set current state of devices
  654. * @bus: Top bus of the subtree to walk.
  655. * @state: state to be set
  656. */
  657. static void __pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
  658. {
  659. if (bus)
  660. pci_walk_bus(bus, __pci_dev_set_current_state, &state);
  661. }
  662. /**
  663. * __pci_complete_power_transition - Complete power transition of a PCI device
  664. * @dev: PCI device to handle.
  665. * @state: State to put the device into.
  666. *
  667. * This function should not be called directly by device drivers.
  668. */
  669. int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
  670. {
  671. int ret;
  672. if (state <= PCI_D0)
  673. return -EINVAL;
  674. ret = pci_platform_power_transition(dev, state);
  675. /* Power off the bridge may power off the whole hierarchy */
  676. if (!ret && state == PCI_D3cold)
  677. __pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
  678. return ret;
  679. }
  680. EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
  681. /**
  682. * pci_set_power_state - Set the power state of a PCI device
  683. * @dev: PCI device to handle.
  684. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  685. *
  686. * Transition a device to a new power state, using the platform firmware and/or
  687. * the device's PCI PM registers.
  688. *
  689. * RETURN VALUE:
  690. * -EINVAL if the requested state is invalid.
  691. * -EIO if device does not support PCI PM or its PM capabilities register has a
  692. * wrong version, or device doesn't support the requested state.
  693. * 0 if device already is in the requested state.
  694. * 0 if device's power state has been successfully changed.
  695. */
  696. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  697. {
  698. int error;
  699. /* bound the state we're entering */
  700. if (state > PCI_D3cold)
  701. state = PCI_D3cold;
  702. else if (state < PCI_D0)
  703. state = PCI_D0;
  704. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  705. /*
  706. * If the device or the parent bridge do not support PCI PM,
  707. * ignore the request if we're doing anything other than putting
  708. * it into D0 (which would only happen on boot).
  709. */
  710. return 0;
  711. /* Check if we're already there */
  712. if (dev->current_state == state)
  713. return 0;
  714. __pci_start_power_transition(dev, state);
  715. /* This device is quirked not to be put into D3, so
  716. don't put it in D3 */
  717. if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  718. return 0;
  719. /*
  720. * To put device in D3cold, we put device into D3hot in native
  721. * way, then put device into D3cold with platform ops
  722. */
  723. error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
  724. PCI_D3hot : state);
  725. if (!__pci_complete_power_transition(dev, state))
  726. error = 0;
  727. /*
  728. * When aspm_policy is "powersave" this call ensures
  729. * that ASPM is configured.
  730. */
  731. if (!error && dev->bus->self)
  732. pcie_aspm_powersave_config_link(dev->bus->self);
  733. return error;
  734. }
  735. /**
  736. * pci_choose_state - Choose the power state of a PCI device
  737. * @dev: PCI device to be suspended
  738. * @state: target sleep state for the whole system. This is the value
  739. * that is passed to suspend() function.
  740. *
  741. * Returns PCI power state suitable for given device and given system
  742. * message.
  743. */
  744. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  745. {
  746. pci_power_t ret;
  747. if (!dev->pm_cap)
  748. return PCI_D0;
  749. ret = platform_pci_choose_state(dev);
  750. if (ret != PCI_POWER_ERROR)
  751. return ret;
  752. switch (state.event) {
  753. case PM_EVENT_ON:
  754. return PCI_D0;
  755. case PM_EVENT_FREEZE:
  756. case PM_EVENT_PRETHAW:
  757. /* REVISIT both freeze and pre-thaw "should" use D0 */
  758. case PM_EVENT_SUSPEND:
  759. case PM_EVENT_HIBERNATE:
  760. return PCI_D3hot;
  761. default:
  762. dev_info(&dev->dev, "unrecognized suspend event %d\n",
  763. state.event);
  764. BUG();
  765. }
  766. return PCI_D0;
  767. }
  768. EXPORT_SYMBOL(pci_choose_state);
  769. #define PCI_EXP_SAVE_REGS 7
  770. static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
  771. u16 cap, bool extended)
  772. {
  773. struct pci_cap_saved_state *tmp;
  774. hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
  775. if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
  776. return tmp;
  777. }
  778. return NULL;
  779. }
  780. struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
  781. {
  782. return _pci_find_saved_cap(dev, cap, false);
  783. }
  784. struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
  785. {
  786. return _pci_find_saved_cap(dev, cap, true);
  787. }
  788. static int pci_save_pcie_state(struct pci_dev *dev)
  789. {
  790. int i = 0;
  791. struct pci_cap_saved_state *save_state;
  792. u16 *cap;
  793. if (!pci_is_pcie(dev))
  794. return 0;
  795. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  796. if (!save_state) {
  797. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  798. return -ENOMEM;
  799. }
  800. cap = (u16 *)&save_state->cap.data[0];
  801. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
  802. pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
  803. pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
  804. pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
  805. pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
  806. pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
  807. pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
  808. return 0;
  809. }
  810. static void pci_restore_pcie_state(struct pci_dev *dev)
  811. {
  812. int i = 0;
  813. struct pci_cap_saved_state *save_state;
  814. u16 *cap;
  815. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  816. if (!save_state)
  817. return;
  818. cap = (u16 *)&save_state->cap.data[0];
  819. pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
  820. pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
  821. pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
  822. pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
  823. pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
  824. pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
  825. pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
  826. }
  827. static int pci_save_pcix_state(struct pci_dev *dev)
  828. {
  829. int pos;
  830. struct pci_cap_saved_state *save_state;
  831. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  832. if (pos <= 0)
  833. return 0;
  834. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  835. if (!save_state) {
  836. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  837. return -ENOMEM;
  838. }
  839. pci_read_config_word(dev, pos + PCI_X_CMD,
  840. (u16 *)save_state->cap.data);
  841. return 0;
  842. }
  843. static void pci_restore_pcix_state(struct pci_dev *dev)
  844. {
  845. int i = 0, pos;
  846. struct pci_cap_saved_state *save_state;
  847. u16 *cap;
  848. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  849. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  850. if (!save_state || pos <= 0)
  851. return;
  852. cap = (u16 *)&save_state->cap.data[0];
  853. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  854. }
  855. /**
  856. * pci_save_state - save the PCI configuration space of a device before suspending
  857. * @dev: - PCI device that we're dealing with
  858. */
  859. int
  860. pci_save_state(struct pci_dev *dev)
  861. {
  862. int i;
  863. /* XXX: 100% dword access ok here? */
  864. for (i = 0; i < 16; i++)
  865. pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
  866. dev->state_saved = true;
  867. if ((i = pci_save_pcie_state(dev)) != 0)
  868. return i;
  869. if ((i = pci_save_pcix_state(dev)) != 0)
  870. return i;
  871. if ((i = pci_save_vc_state(dev)) != 0)
  872. return i;
  873. return 0;
  874. }
  875. static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
  876. u32 saved_val, int retry)
  877. {
  878. u32 val;
  879. pci_read_config_dword(pdev, offset, &val);
  880. if (val == saved_val)
  881. return;
  882. for (;;) {
  883. dev_dbg(&pdev->dev, "restoring config space at offset "
  884. "%#x (was %#x, writing %#x)\n", offset, val, saved_val);
  885. pci_write_config_dword(pdev, offset, saved_val);
  886. if (retry-- <= 0)
  887. return;
  888. pci_read_config_dword(pdev, offset, &val);
  889. if (val == saved_val)
  890. return;
  891. mdelay(1);
  892. }
  893. }
  894. static void pci_restore_config_space_range(struct pci_dev *pdev,
  895. int start, int end, int retry)
  896. {
  897. int index;
  898. for (index = end; index >= start; index--)
  899. pci_restore_config_dword(pdev, 4 * index,
  900. pdev->saved_config_space[index],
  901. retry);
  902. }
  903. static void pci_restore_config_space(struct pci_dev *pdev)
  904. {
  905. if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  906. pci_restore_config_space_range(pdev, 10, 15, 0);
  907. /* Restore BARs before the command register. */
  908. pci_restore_config_space_range(pdev, 4, 9, 10);
  909. pci_restore_config_space_range(pdev, 0, 3, 0);
  910. } else {
  911. pci_restore_config_space_range(pdev, 0, 15, 0);
  912. }
  913. }
  914. /**
  915. * pci_restore_state - Restore the saved state of a PCI device
  916. * @dev: - PCI device that we're dealing with
  917. */
  918. void pci_restore_state(struct pci_dev *dev)
  919. {
  920. if (!dev->state_saved)
  921. return;
  922. /* PCI Express register must be restored first */
  923. pci_restore_pcie_state(dev);
  924. pci_restore_ats_state(dev);
  925. pci_restore_vc_state(dev);
  926. pci_restore_config_space(dev);
  927. pci_restore_pcix_state(dev);
  928. pci_restore_msi_state(dev);
  929. pci_restore_iov_state(dev);
  930. dev->state_saved = false;
  931. }
  932. struct pci_saved_state {
  933. u32 config_space[16];
  934. struct pci_cap_saved_data cap[0];
  935. };
  936. /**
  937. * pci_store_saved_state - Allocate and return an opaque struct containing
  938. * the device saved state.
  939. * @dev: PCI device that we're dealing with
  940. *
  941. * Return NULL if no state or error.
  942. */
  943. struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
  944. {
  945. struct pci_saved_state *state;
  946. struct pci_cap_saved_state *tmp;
  947. struct pci_cap_saved_data *cap;
  948. size_t size;
  949. if (!dev->state_saved)
  950. return NULL;
  951. size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
  952. hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
  953. size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  954. state = kzalloc(size, GFP_KERNEL);
  955. if (!state)
  956. return NULL;
  957. memcpy(state->config_space, dev->saved_config_space,
  958. sizeof(state->config_space));
  959. cap = state->cap;
  960. hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
  961. size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  962. memcpy(cap, &tmp->cap, len);
  963. cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
  964. }
  965. /* Empty cap_save terminates list */
  966. return state;
  967. }
  968. EXPORT_SYMBOL_GPL(pci_store_saved_state);
  969. /**
  970. * pci_load_saved_state - Reload the provided save state into struct pci_dev.
  971. * @dev: PCI device that we're dealing with
  972. * @state: Saved state returned from pci_store_saved_state()
  973. */
  974. static int pci_load_saved_state(struct pci_dev *dev,
  975. struct pci_saved_state *state)
  976. {
  977. struct pci_cap_saved_data *cap;
  978. dev->state_saved = false;
  979. if (!state)
  980. return 0;
  981. memcpy(dev->saved_config_space, state->config_space,
  982. sizeof(state->config_space));
  983. cap = state->cap;
  984. while (cap->size) {
  985. struct pci_cap_saved_state *tmp;
  986. tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
  987. if (!tmp || tmp->cap.size != cap->size)
  988. return -EINVAL;
  989. memcpy(tmp->cap.data, cap->data, tmp->cap.size);
  990. cap = (struct pci_cap_saved_data *)((u8 *)cap +
  991. sizeof(struct pci_cap_saved_data) + cap->size);
  992. }
  993. dev->state_saved = true;
  994. return 0;
  995. }
  996. /**
  997. * pci_load_and_free_saved_state - Reload the save state pointed to by state,
  998. * and free the memory allocated for it.
  999. * @dev: PCI device that we're dealing with
  1000. * @state: Pointer to saved state returned from pci_store_saved_state()
  1001. */
  1002. int pci_load_and_free_saved_state(struct pci_dev *dev,
  1003. struct pci_saved_state **state)
  1004. {
  1005. int ret = pci_load_saved_state(dev, *state);
  1006. kfree(*state);
  1007. *state = NULL;
  1008. return ret;
  1009. }
  1010. EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
  1011. int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
  1012. {
  1013. return pci_enable_resources(dev, bars);
  1014. }
  1015. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  1016. {
  1017. int err;
  1018. u16 cmd;
  1019. u8 pin;
  1020. err = pci_set_power_state(dev, PCI_D0);
  1021. if (err < 0 && err != -EIO)
  1022. return err;
  1023. err = pcibios_enable_device(dev, bars);
  1024. if (err < 0)
  1025. return err;
  1026. pci_fixup_device(pci_fixup_enable, dev);
  1027. if (dev->msi_enabled || dev->msix_enabled)
  1028. return 0;
  1029. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  1030. if (pin) {
  1031. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1032. if (cmd & PCI_COMMAND_INTX_DISABLE)
  1033. pci_write_config_word(dev, PCI_COMMAND,
  1034. cmd & ~PCI_COMMAND_INTX_DISABLE);
  1035. }
  1036. return 0;
  1037. }
  1038. /**
  1039. * pci_reenable_device - Resume abandoned device
  1040. * @dev: PCI device to be resumed
  1041. *
  1042. * Note this function is a backend of pci_default_resume and is not supposed
  1043. * to be called by normal code, write proper resume handler and use it instead.
  1044. */
  1045. int pci_reenable_device(struct pci_dev *dev)
  1046. {
  1047. if (pci_is_enabled(dev))
  1048. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  1049. return 0;
  1050. }
  1051. static void pci_enable_bridge(struct pci_dev *dev)
  1052. {
  1053. struct pci_dev *bridge;
  1054. int retval;
  1055. bridge = pci_upstream_bridge(dev);
  1056. if (bridge)
  1057. pci_enable_bridge(bridge);
  1058. if (pci_is_enabled(dev)) {
  1059. if (!dev->is_busmaster)
  1060. pci_set_master(dev);
  1061. return;
  1062. }
  1063. retval = pci_enable_device(dev);
  1064. if (retval)
  1065. dev_err(&dev->dev, "Error enabling bridge (%d), continuing\n",
  1066. retval);
  1067. pci_set_master(dev);
  1068. }
  1069. static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
  1070. {
  1071. struct pci_dev *bridge;
  1072. int err;
  1073. int i, bars = 0;
  1074. /*
  1075. * Power state could be unknown at this point, either due to a fresh
  1076. * boot or a device removal call. So get the current power state
  1077. * so that things like MSI message writing will behave as expected
  1078. * (e.g. if the device really is in D0 at enable time).
  1079. */
  1080. if (dev->pm_cap) {
  1081. u16 pmcsr;
  1082. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1083. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  1084. }
  1085. if (atomic_inc_return(&dev->enable_cnt) > 1)
  1086. return 0; /* already enabled */
  1087. bridge = pci_upstream_bridge(dev);
  1088. if (bridge)
  1089. pci_enable_bridge(bridge);
  1090. /* only skip sriov related */
  1091. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  1092. if (dev->resource[i].flags & flags)
  1093. bars |= (1 << i);
  1094. for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
  1095. if (dev->resource[i].flags & flags)
  1096. bars |= (1 << i);
  1097. err = do_pci_enable_device(dev, bars);
  1098. if (err < 0)
  1099. atomic_dec(&dev->enable_cnt);
  1100. return err;
  1101. }
  1102. /**
  1103. * pci_enable_device_io - Initialize a device for use with IO space
  1104. * @dev: PCI device to be initialized
  1105. *
  1106. * Initialize device before it's used by a driver. Ask low-level code
  1107. * to enable I/O resources. Wake up the device if it was suspended.
  1108. * Beware, this function can fail.
  1109. */
  1110. int pci_enable_device_io(struct pci_dev *dev)
  1111. {
  1112. return pci_enable_device_flags(dev, IORESOURCE_IO);
  1113. }
  1114. /**
  1115. * pci_enable_device_mem - Initialize a device for use with Memory space
  1116. * @dev: PCI device to be initialized
  1117. *
  1118. * Initialize device before it's used by a driver. Ask low-level code
  1119. * to enable Memory resources. Wake up the device if it was suspended.
  1120. * Beware, this function can fail.
  1121. */
  1122. int pci_enable_device_mem(struct pci_dev *dev)
  1123. {
  1124. return pci_enable_device_flags(dev, IORESOURCE_MEM);
  1125. }
  1126. /**
  1127. * pci_enable_device - Initialize device before it's used by a driver.
  1128. * @dev: PCI device to be initialized
  1129. *
  1130. * Initialize device before it's used by a driver. Ask low-level code
  1131. * to enable I/O and memory. Wake up the device if it was suspended.
  1132. * Beware, this function can fail.
  1133. *
  1134. * Note we don't actually enable the device many times if we call
  1135. * this function repeatedly (we just increment the count).
  1136. */
  1137. int pci_enable_device(struct pci_dev *dev)
  1138. {
  1139. return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  1140. }
  1141. /*
  1142. * Managed PCI resources. This manages device on/off, intx/msi/msix
  1143. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  1144. * there's no need to track it separately. pci_devres is initialized
  1145. * when a device is enabled using managed PCI device enable interface.
  1146. */
  1147. struct pci_devres {
  1148. unsigned int enabled:1;
  1149. unsigned int pinned:1;
  1150. unsigned int orig_intx:1;
  1151. unsigned int restore_intx:1;
  1152. u32 region_mask;
  1153. };
  1154. static void pcim_release(struct device *gendev, void *res)
  1155. {
  1156. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  1157. struct pci_devres *this = res;
  1158. int i;
  1159. if (dev->msi_enabled)
  1160. pci_disable_msi(dev);
  1161. if (dev->msix_enabled)
  1162. pci_disable_msix(dev);
  1163. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  1164. if (this->region_mask & (1 << i))
  1165. pci_release_region(dev, i);
  1166. if (this->restore_intx)
  1167. pci_intx(dev, this->orig_intx);
  1168. if (this->enabled && !this->pinned)
  1169. pci_disable_device(dev);
  1170. }
  1171. static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
  1172. {
  1173. struct pci_devres *dr, *new_dr;
  1174. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1175. if (dr)
  1176. return dr;
  1177. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  1178. if (!new_dr)
  1179. return NULL;
  1180. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  1181. }
  1182. static struct pci_devres *find_pci_dr(struct pci_dev *pdev)
  1183. {
  1184. if (pci_is_managed(pdev))
  1185. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1186. return NULL;
  1187. }
  1188. /**
  1189. * pcim_enable_device - Managed pci_enable_device()
  1190. * @pdev: PCI device to be initialized
  1191. *
  1192. * Managed pci_enable_device().
  1193. */
  1194. int pcim_enable_device(struct pci_dev *pdev)
  1195. {
  1196. struct pci_devres *dr;
  1197. int rc;
  1198. dr = get_pci_dr(pdev);
  1199. if (unlikely(!dr))
  1200. return -ENOMEM;
  1201. if (dr->enabled)
  1202. return 0;
  1203. rc = pci_enable_device(pdev);
  1204. if (!rc) {
  1205. pdev->is_managed = 1;
  1206. dr->enabled = 1;
  1207. }
  1208. return rc;
  1209. }
  1210. /**
  1211. * pcim_pin_device - Pin managed PCI device
  1212. * @pdev: PCI device to pin
  1213. *
  1214. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  1215. * driver detach. @pdev must have been enabled with
  1216. * pcim_enable_device().
  1217. */
  1218. void pcim_pin_device(struct pci_dev *pdev)
  1219. {
  1220. struct pci_devres *dr;
  1221. dr = find_pci_dr(pdev);
  1222. WARN_ON(!dr || !dr->enabled);
  1223. if (dr)
  1224. dr->pinned = 1;
  1225. }
  1226. /*
  1227. * pcibios_add_device - provide arch specific hooks when adding device dev
  1228. * @dev: the PCI device being added
  1229. *
  1230. * Permits the platform to provide architecture specific functionality when
  1231. * devices are added. This is the default implementation. Architecture
  1232. * implementations can override this.
  1233. */
  1234. int __weak pcibios_add_device (struct pci_dev *dev)
  1235. {
  1236. return 0;
  1237. }
  1238. /**
  1239. * pcibios_release_device - provide arch specific hooks when releasing device dev
  1240. * @dev: the PCI device being released
  1241. *
  1242. * Permits the platform to provide architecture specific functionality when
  1243. * devices are released. This is the default implementation. Architecture
  1244. * implementations can override this.
  1245. */
  1246. void __weak pcibios_release_device(struct pci_dev *dev) {}
  1247. /**
  1248. * pcibios_disable_device - disable arch specific PCI resources for device dev
  1249. * @dev: the PCI device to disable
  1250. *
  1251. * Disables architecture specific PCI resources for the device. This
  1252. * is the default implementation. Architecture implementations can
  1253. * override this.
  1254. */
  1255. void __weak pcibios_disable_device (struct pci_dev *dev) {}
  1256. static void do_pci_disable_device(struct pci_dev *dev)
  1257. {
  1258. u16 pci_command;
  1259. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  1260. if (pci_command & PCI_COMMAND_MASTER) {
  1261. pci_command &= ~PCI_COMMAND_MASTER;
  1262. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  1263. }
  1264. pcibios_disable_device(dev);
  1265. }
  1266. /**
  1267. * pci_disable_enabled_device - Disable device without updating enable_cnt
  1268. * @dev: PCI device to disable
  1269. *
  1270. * NOTE: This function is a backend of PCI power management routines and is
  1271. * not supposed to be called drivers.
  1272. */
  1273. void pci_disable_enabled_device(struct pci_dev *dev)
  1274. {
  1275. if (pci_is_enabled(dev))
  1276. do_pci_disable_device(dev);
  1277. }
  1278. /**
  1279. * pci_disable_device - Disable PCI device after use
  1280. * @dev: PCI device to be disabled
  1281. *
  1282. * Signal to the system that the PCI device is not in use by the system
  1283. * anymore. This only involves disabling PCI bus-mastering, if active.
  1284. *
  1285. * Note we don't actually disable the device until all callers of
  1286. * pci_enable_device() have called pci_disable_device().
  1287. */
  1288. void
  1289. pci_disable_device(struct pci_dev *dev)
  1290. {
  1291. struct pci_devres *dr;
  1292. dr = find_pci_dr(dev);
  1293. if (dr)
  1294. dr->enabled = 0;
  1295. dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
  1296. "disabling already-disabled device");
  1297. if (atomic_dec_return(&dev->enable_cnt) != 0)
  1298. return;
  1299. do_pci_disable_device(dev);
  1300. dev->is_busmaster = 0;
  1301. }
  1302. /**
  1303. * pcibios_set_pcie_reset_state - set reset state for device dev
  1304. * @dev: the PCIe device reset
  1305. * @state: Reset state to enter into
  1306. *
  1307. *
  1308. * Sets the PCIe reset state for the device. This is the default
  1309. * implementation. Architecture implementations can override this.
  1310. */
  1311. int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
  1312. enum pcie_reset_state state)
  1313. {
  1314. return -EINVAL;
  1315. }
  1316. /**
  1317. * pci_set_pcie_reset_state - set reset state for device dev
  1318. * @dev: the PCIe device reset
  1319. * @state: Reset state to enter into
  1320. *
  1321. *
  1322. * Sets the PCI reset state for the device.
  1323. */
  1324. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  1325. {
  1326. return pcibios_set_pcie_reset_state(dev, state);
  1327. }
  1328. /**
  1329. * pci_check_pme_status - Check if given device has generated PME.
  1330. * @dev: Device to check.
  1331. *
  1332. * Check the PME status of the device and if set, clear it and clear PME enable
  1333. * (if set). Return 'true' if PME status and PME enable were both set or
  1334. * 'false' otherwise.
  1335. */
  1336. bool pci_check_pme_status(struct pci_dev *dev)
  1337. {
  1338. int pmcsr_pos;
  1339. u16 pmcsr;
  1340. bool ret = false;
  1341. if (!dev->pm_cap)
  1342. return false;
  1343. pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
  1344. pci_read_config_word(dev, pmcsr_pos, &pmcsr);
  1345. if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
  1346. return false;
  1347. /* Clear PME status. */
  1348. pmcsr |= PCI_PM_CTRL_PME_STATUS;
  1349. if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
  1350. /* Disable PME to avoid interrupt flood. */
  1351. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1352. ret = true;
  1353. }
  1354. pci_write_config_word(dev, pmcsr_pos, pmcsr);
  1355. return ret;
  1356. }
  1357. /**
  1358. * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
  1359. * @dev: Device to handle.
  1360. * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
  1361. *
  1362. * Check if @dev has generated PME and queue a resume request for it in that
  1363. * case.
  1364. */
  1365. static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
  1366. {
  1367. if (pme_poll_reset && dev->pme_poll)
  1368. dev->pme_poll = false;
  1369. if (pci_check_pme_status(dev)) {
  1370. pci_wakeup_event(dev);
  1371. pm_request_resume(&dev->dev);
  1372. }
  1373. return 0;
  1374. }
  1375. /**
  1376. * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
  1377. * @bus: Top bus of the subtree to walk.
  1378. */
  1379. void pci_pme_wakeup_bus(struct pci_bus *bus)
  1380. {
  1381. if (bus)
  1382. pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
  1383. }
  1384. /**
  1385. * pci_pme_capable - check the capability of PCI device to generate PME#
  1386. * @dev: PCI device to handle.
  1387. * @state: PCI state from which device will issue PME#.
  1388. */
  1389. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  1390. {
  1391. if (!dev->pm_cap)
  1392. return false;
  1393. return !!(dev->pme_support & (1 << state));
  1394. }
  1395. static void pci_pme_list_scan(struct work_struct *work)
  1396. {
  1397. struct pci_pme_device *pme_dev, *n;
  1398. mutex_lock(&pci_pme_list_mutex);
  1399. list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
  1400. if (pme_dev->dev->pme_poll) {
  1401. struct pci_dev *bridge;
  1402. bridge = pme_dev->dev->bus->self;
  1403. /*
  1404. * If bridge is in low power state, the
  1405. * configuration space of subordinate devices
  1406. * may be not accessible
  1407. */
  1408. if (bridge && bridge->current_state != PCI_D0)
  1409. continue;
  1410. pci_pme_wakeup(pme_dev->dev, NULL);
  1411. } else {
  1412. list_del(&pme_dev->list);
  1413. kfree(pme_dev);
  1414. }
  1415. }
  1416. if (!list_empty(&pci_pme_list))
  1417. schedule_delayed_work(&pci_pme_work,
  1418. msecs_to_jiffies(PME_TIMEOUT));
  1419. mutex_unlock(&pci_pme_list_mutex);
  1420. }
  1421. /**
  1422. * pci_pme_active - enable or disable PCI device's PME# function
  1423. * @dev: PCI device to handle.
  1424. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  1425. *
  1426. * The caller must verify that the device is capable of generating PME# before
  1427. * calling this function with @enable equal to 'true'.
  1428. */
  1429. void pci_pme_active(struct pci_dev *dev, bool enable)
  1430. {
  1431. u16 pmcsr;
  1432. if (!dev->pme_support)
  1433. return;
  1434. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1435. /* Clear PME_Status by writing 1 to it and enable PME# */
  1436. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  1437. if (!enable)
  1438. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1439. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  1440. /*
  1441. * PCI (as opposed to PCIe) PME requires that the device have
  1442. * its PME# line hooked up correctly. Not all hardware vendors
  1443. * do this, so the PME never gets delivered and the device
  1444. * remains asleep. The easiest way around this is to
  1445. * periodically walk the list of suspended devices and check
  1446. * whether any have their PME flag set. The assumption is that
  1447. * we'll wake up often enough anyway that this won't be a huge
  1448. * hit, and the power savings from the devices will still be a
  1449. * win.
  1450. *
  1451. * Although PCIe uses in-band PME message instead of PME# line
  1452. * to report PME, PME does not work for some PCIe devices in
  1453. * reality. For example, there are devices that set their PME
  1454. * status bits, but don't really bother to send a PME message;
  1455. * there are PCI Express Root Ports that don't bother to
  1456. * trigger interrupts when they receive PME messages from the
  1457. * devices below. So PME poll is used for PCIe devices too.
  1458. */
  1459. if (dev->pme_poll) {
  1460. struct pci_pme_device *pme_dev;
  1461. if (enable) {
  1462. pme_dev = kmalloc(sizeof(struct pci_pme_device),
  1463. GFP_KERNEL);
  1464. if (!pme_dev) {
  1465. dev_warn(&dev->dev, "can't enable PME#\n");
  1466. return;
  1467. }
  1468. pme_dev->dev = dev;
  1469. mutex_lock(&pci_pme_list_mutex);
  1470. list_add(&pme_dev->list, &pci_pme_list);
  1471. if (list_is_singular(&pci_pme_list))
  1472. schedule_delayed_work(&pci_pme_work,
  1473. msecs_to_jiffies(PME_TIMEOUT));
  1474. mutex_unlock(&pci_pme_list_mutex);
  1475. } else {
  1476. mutex_lock(&pci_pme_list_mutex);
  1477. list_for_each_entry(pme_dev, &pci_pme_list, list) {
  1478. if (pme_dev->dev == dev) {
  1479. list_del(&pme_dev->list);
  1480. kfree(pme_dev);
  1481. break;
  1482. }
  1483. }
  1484. mutex_unlock(&pci_pme_list_mutex);
  1485. }
  1486. }
  1487. dev_dbg(&dev->dev, "PME# %s\n", enable ? "enabled" : "disabled");
  1488. }
  1489. /**
  1490. * __pci_enable_wake - enable PCI device as wakeup event source
  1491. * @dev: PCI device affected
  1492. * @state: PCI state from which device will issue wakeup events
  1493. * @runtime: True if the events are to be generated at run time
  1494. * @enable: True to enable event generation; false to disable
  1495. *
  1496. * This enables the device as a wakeup event source, or disables it.
  1497. * When such events involves platform-specific hooks, those hooks are
  1498. * called automatically by this routine.
  1499. *
  1500. * Devices with legacy power management (no standard PCI PM capabilities)
  1501. * always require such platform hooks.
  1502. *
  1503. * RETURN VALUE:
  1504. * 0 is returned on success
  1505. * -EINVAL is returned if device is not supposed to wake up the system
  1506. * Error code depending on the platform is returned if both the platform and
  1507. * the native mechanism fail to enable the generation of wake-up events
  1508. */
  1509. int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
  1510. bool runtime, bool enable)
  1511. {
  1512. int ret = 0;
  1513. if (enable && !runtime && !device_may_wakeup(&dev->dev))
  1514. return -EINVAL;
  1515. /* Don't do the same thing twice in a row for one device. */
  1516. if (!!enable == !!dev->wakeup_prepared)
  1517. return 0;
  1518. /*
  1519. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  1520. * Anderson we should be doing PME# wake enable followed by ACPI wake
  1521. * enable. To disable wake-up we call the platform first, for symmetry.
  1522. */
  1523. if (enable) {
  1524. int error;
  1525. if (pci_pme_capable(dev, state))
  1526. pci_pme_active(dev, true);
  1527. else
  1528. ret = 1;
  1529. error = runtime ? platform_pci_run_wake(dev, true) :
  1530. platform_pci_sleep_wake(dev, true);
  1531. if (ret)
  1532. ret = error;
  1533. if (!ret)
  1534. dev->wakeup_prepared = true;
  1535. } else {
  1536. if (runtime)
  1537. platform_pci_run_wake(dev, false);
  1538. else
  1539. platform_pci_sleep_wake(dev, false);
  1540. pci_pme_active(dev, false);
  1541. dev->wakeup_prepared = false;
  1542. }
  1543. return ret;
  1544. }
  1545. EXPORT_SYMBOL(__pci_enable_wake);
  1546. /**
  1547. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  1548. * @dev: PCI device to prepare
  1549. * @enable: True to enable wake-up event generation; false to disable
  1550. *
  1551. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  1552. * and this function allows them to set that up cleanly - pci_enable_wake()
  1553. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  1554. * ordering constraints.
  1555. *
  1556. * This function only returns error code if the device is not capable of
  1557. * generating PME# from both D3_hot and D3_cold, and the platform is unable to
  1558. * enable wake-up power for it.
  1559. */
  1560. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  1561. {
  1562. return pci_pme_capable(dev, PCI_D3cold) ?
  1563. pci_enable_wake(dev, PCI_D3cold, enable) :
  1564. pci_enable_wake(dev, PCI_D3hot, enable);
  1565. }
  1566. /**
  1567. * pci_target_state - find an appropriate low power state for a given PCI dev
  1568. * @dev: PCI device
  1569. *
  1570. * Use underlying platform code to find a supported low power state for @dev.
  1571. * If the platform can't manage @dev, return the deepest state from which it
  1572. * can generate wake events, based on any available PME info.
  1573. */
  1574. static pci_power_t pci_target_state(struct pci_dev *dev)
  1575. {
  1576. pci_power_t target_state = PCI_D3hot;
  1577. if (platform_pci_power_manageable(dev)) {
  1578. /*
  1579. * Call the platform to choose the target state of the device
  1580. * and enable wake-up from this state if supported.
  1581. */
  1582. pci_power_t state = platform_pci_choose_state(dev);
  1583. switch (state) {
  1584. case PCI_POWER_ERROR:
  1585. case PCI_UNKNOWN:
  1586. break;
  1587. case PCI_D1:
  1588. case PCI_D2:
  1589. if (pci_no_d1d2(dev))
  1590. break;
  1591. default:
  1592. target_state = state;
  1593. }
  1594. } else if (!dev->pm_cap) {
  1595. target_state = PCI_D0;
  1596. } else if (device_may_wakeup(&dev->dev)) {
  1597. /*
  1598. * Find the deepest state from which the device can generate
  1599. * wake-up events, make it the target state and enable device
  1600. * to generate PME#.
  1601. */
  1602. if (dev->pme_support) {
  1603. while (target_state
  1604. && !(dev->pme_support & (1 << target_state)))
  1605. target_state--;
  1606. }
  1607. }
  1608. return target_state;
  1609. }
  1610. /**
  1611. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1612. * @dev: Device to handle.
  1613. *
  1614. * Choose the power state appropriate for the device depending on whether
  1615. * it can wake up the system and/or is power manageable by the platform
  1616. * (PCI_D3hot is the default) and put the device into that state.
  1617. */
  1618. int pci_prepare_to_sleep(struct pci_dev *dev)
  1619. {
  1620. pci_power_t target_state = pci_target_state(dev);
  1621. int error;
  1622. if (target_state == PCI_POWER_ERROR)
  1623. return -EIO;
  1624. /* D3cold during system suspend/hibernate is not supported */
  1625. if (target_state > PCI_D3hot)
  1626. target_state = PCI_D3hot;
  1627. pci_enable_wake(dev, target_state, device_may_wakeup(&dev->dev));
  1628. error = pci_set_power_state(dev, target_state);
  1629. if (error)
  1630. pci_enable_wake(dev, target_state, false);
  1631. return error;
  1632. }
  1633. /**
  1634. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1635. * @dev: Device to handle.
  1636. *
  1637. * Disable device's system wake-up capability and put it into D0.
  1638. */
  1639. int pci_back_from_sleep(struct pci_dev *dev)
  1640. {
  1641. pci_enable_wake(dev, PCI_D0, false);
  1642. return pci_set_power_state(dev, PCI_D0);
  1643. }
  1644. /**
  1645. * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
  1646. * @dev: PCI device being suspended.
  1647. *
  1648. * Prepare @dev to generate wake-up events at run time and put it into a low
  1649. * power state.
  1650. */
  1651. int pci_finish_runtime_suspend(struct pci_dev *dev)
  1652. {
  1653. pci_power_t target_state = pci_target_state(dev);
  1654. int error;
  1655. if (target_state == PCI_POWER_ERROR)
  1656. return -EIO;
  1657. dev->runtime_d3cold = target_state == PCI_D3cold;
  1658. __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev));
  1659. error = pci_set_power_state(dev, target_state);
  1660. if (error) {
  1661. __pci_enable_wake(dev, target_state, true, false);
  1662. dev->runtime_d3cold = false;
  1663. }
  1664. return error;
  1665. }
  1666. /**
  1667. * pci_dev_run_wake - Check if device can generate run-time wake-up events.
  1668. * @dev: Device to check.
  1669. *
  1670. * Return true if the device itself is capable of generating wake-up events
  1671. * (through the platform or using the native PCIe PME) or if the device supports
  1672. * PME and one of its upstream bridges can generate wake-up events.
  1673. */
  1674. bool pci_dev_run_wake(struct pci_dev *dev)
  1675. {
  1676. struct pci_bus *bus = dev->bus;
  1677. if (device_run_wake(&dev->dev))
  1678. return true;
  1679. if (!dev->pme_support)
  1680. return false;
  1681. while (bus->parent) {
  1682. struct pci_dev *bridge = bus->self;
  1683. if (device_run_wake(&bridge->dev))
  1684. return true;
  1685. bus = bus->parent;
  1686. }
  1687. /* We have reached the root bus. */
  1688. if (bus->bridge)
  1689. return device_run_wake(bus->bridge);
  1690. return false;
  1691. }
  1692. EXPORT_SYMBOL_GPL(pci_dev_run_wake);
  1693. void pci_config_pm_runtime_get(struct pci_dev *pdev)
  1694. {
  1695. struct device *dev = &pdev->dev;
  1696. struct device *parent = dev->parent;
  1697. if (parent)
  1698. pm_runtime_get_sync(parent);
  1699. pm_runtime_get_noresume(dev);
  1700. /*
  1701. * pdev->current_state is set to PCI_D3cold during suspending,
  1702. * so wait until suspending completes
  1703. */
  1704. pm_runtime_barrier(dev);
  1705. /*
  1706. * Only need to resume devices in D3cold, because config
  1707. * registers are still accessible for devices suspended but
  1708. * not in D3cold.
  1709. */
  1710. if (pdev->current_state == PCI_D3cold)
  1711. pm_runtime_resume(dev);
  1712. }
  1713. void pci_config_pm_runtime_put(struct pci_dev *pdev)
  1714. {
  1715. struct device *dev = &pdev->dev;
  1716. struct device *parent = dev->parent;
  1717. pm_runtime_put(dev);
  1718. if (parent)
  1719. pm_runtime_put_sync(parent);
  1720. }
  1721. /**
  1722. * pci_pm_init - Initialize PM functions of given PCI device
  1723. * @dev: PCI device to handle.
  1724. */
  1725. void pci_pm_init(struct pci_dev *dev)
  1726. {
  1727. int pm;
  1728. u16 pmc;
  1729. pm_runtime_forbid(&dev->dev);
  1730. pm_runtime_set_active(&dev->dev);
  1731. pm_runtime_enable(&dev->dev);
  1732. device_enable_async_suspend(&dev->dev);
  1733. dev->wakeup_prepared = false;
  1734. dev->pm_cap = 0;
  1735. dev->pme_support = 0;
  1736. /* find PCI PM capability in list */
  1737. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  1738. if (!pm)
  1739. return;
  1740. /* Check device's ability to generate PME# */
  1741. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  1742. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  1743. dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
  1744. pmc & PCI_PM_CAP_VER_MASK);
  1745. return;
  1746. }
  1747. dev->pm_cap = pm;
  1748. dev->d3_delay = PCI_PM_D3_WAIT;
  1749. dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
  1750. dev->d3cold_allowed = true;
  1751. dev->d1_support = false;
  1752. dev->d2_support = false;
  1753. if (!pci_no_d1d2(dev)) {
  1754. if (pmc & PCI_PM_CAP_D1)
  1755. dev->d1_support = true;
  1756. if (pmc & PCI_PM_CAP_D2)
  1757. dev->d2_support = true;
  1758. if (dev->d1_support || dev->d2_support)
  1759. dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
  1760. dev->d1_support ? " D1" : "",
  1761. dev->d2_support ? " D2" : "");
  1762. }
  1763. pmc &= PCI_PM_CAP_PME_MASK;
  1764. if (pmc) {
  1765. dev_printk(KERN_DEBUG, &dev->dev,
  1766. "PME# supported from%s%s%s%s%s\n",
  1767. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  1768. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  1769. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  1770. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  1771. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  1772. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  1773. dev->pme_poll = true;
  1774. /*
  1775. * Make device's PM flags reflect the wake-up capability, but
  1776. * let the user space enable it to wake up the system as needed.
  1777. */
  1778. device_set_wakeup_capable(&dev->dev, true);
  1779. /* Disable the PME# generation functionality */
  1780. pci_pme_active(dev, false);
  1781. }
  1782. }
  1783. static void pci_add_saved_cap(struct pci_dev *pci_dev,
  1784. struct pci_cap_saved_state *new_cap)
  1785. {
  1786. hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
  1787. }
  1788. /**
  1789. * _pci_add_cap_save_buffer - allocate buffer for saving given
  1790. * capability registers
  1791. * @dev: the PCI device
  1792. * @cap: the capability to allocate the buffer for
  1793. * @extended: Standard or Extended capability ID
  1794. * @size: requested size of the buffer
  1795. */
  1796. static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
  1797. bool extended, unsigned int size)
  1798. {
  1799. int pos;
  1800. struct pci_cap_saved_state *save_state;
  1801. if (extended)
  1802. pos = pci_find_ext_capability(dev, cap);
  1803. else
  1804. pos = pci_find_capability(dev, cap);
  1805. if (pos <= 0)
  1806. return 0;
  1807. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  1808. if (!save_state)
  1809. return -ENOMEM;
  1810. save_state->cap.cap_nr = cap;
  1811. save_state->cap.cap_extended = extended;
  1812. save_state->cap.size = size;
  1813. pci_add_saved_cap(dev, save_state);
  1814. return 0;
  1815. }
  1816. int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
  1817. {
  1818. return _pci_add_cap_save_buffer(dev, cap, false, size);
  1819. }
  1820. int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
  1821. {
  1822. return _pci_add_cap_save_buffer(dev, cap, true, size);
  1823. }
  1824. /**
  1825. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  1826. * @dev: the PCI device
  1827. */
  1828. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  1829. {
  1830. int error;
  1831. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
  1832. PCI_EXP_SAVE_REGS * sizeof(u16));
  1833. if (error)
  1834. dev_err(&dev->dev,
  1835. "unable to preallocate PCI Express save buffer\n");
  1836. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  1837. if (error)
  1838. dev_err(&dev->dev,
  1839. "unable to preallocate PCI-X save buffer\n");
  1840. pci_allocate_vc_save_buffers(dev);
  1841. }
  1842. void pci_free_cap_save_buffers(struct pci_dev *dev)
  1843. {
  1844. struct pci_cap_saved_state *tmp;
  1845. struct hlist_node *n;
  1846. hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
  1847. kfree(tmp);
  1848. }
  1849. /**
  1850. * pci_configure_ari - enable or disable ARI forwarding
  1851. * @dev: the PCI device
  1852. *
  1853. * If @dev and its upstream bridge both support ARI, enable ARI in the
  1854. * bridge. Otherwise, disable ARI in the bridge.
  1855. */
  1856. void pci_configure_ari(struct pci_dev *dev)
  1857. {
  1858. u32 cap;
  1859. struct pci_dev *bridge;
  1860. if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
  1861. return;
  1862. bridge = dev->bus->self;
  1863. if (!bridge)
  1864. return;
  1865. pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
  1866. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  1867. return;
  1868. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
  1869. pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
  1870. PCI_EXP_DEVCTL2_ARI);
  1871. bridge->ari_enabled = 1;
  1872. } else {
  1873. pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
  1874. PCI_EXP_DEVCTL2_ARI);
  1875. bridge->ari_enabled = 0;
  1876. }
  1877. }
  1878. static int pci_acs_enable;
  1879. /**
  1880. * pci_request_acs - ask for ACS to be enabled if supported
  1881. */
  1882. void pci_request_acs(void)
  1883. {
  1884. pci_acs_enable = 1;
  1885. }
  1886. /**
  1887. * pci_std_enable_acs - enable ACS on devices using standard ACS capabilites
  1888. * @dev: the PCI device
  1889. */
  1890. static int pci_std_enable_acs(struct pci_dev *dev)
  1891. {
  1892. int pos;
  1893. u16 cap;
  1894. u16 ctrl;
  1895. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  1896. if (!pos)
  1897. return -ENODEV;
  1898. pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
  1899. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  1900. /* Source Validation */
  1901. ctrl |= (cap & PCI_ACS_SV);
  1902. /* P2P Request Redirect */
  1903. ctrl |= (cap & PCI_ACS_RR);
  1904. /* P2P Completion Redirect */
  1905. ctrl |= (cap & PCI_ACS_CR);
  1906. /* Upstream Forwarding */
  1907. ctrl |= (cap & PCI_ACS_UF);
  1908. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  1909. return 0;
  1910. }
  1911. /**
  1912. * pci_enable_acs - enable ACS if hardware support it
  1913. * @dev: the PCI device
  1914. */
  1915. void pci_enable_acs(struct pci_dev *dev)
  1916. {
  1917. if (!pci_acs_enable)
  1918. return;
  1919. if (!pci_std_enable_acs(dev))
  1920. return;
  1921. pci_dev_specific_enable_acs(dev);
  1922. }
  1923. static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
  1924. {
  1925. int pos;
  1926. u16 cap, ctrl;
  1927. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
  1928. if (!pos)
  1929. return false;
  1930. /*
  1931. * Except for egress control, capabilities are either required
  1932. * or only required if controllable. Features missing from the
  1933. * capability field can therefore be assumed as hard-wired enabled.
  1934. */
  1935. pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
  1936. acs_flags &= (cap | PCI_ACS_EC);
  1937. pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
  1938. return (ctrl & acs_flags) == acs_flags;
  1939. }
  1940. /**
  1941. * pci_acs_enabled - test ACS against required flags for a given device
  1942. * @pdev: device to test
  1943. * @acs_flags: required PCI ACS flags
  1944. *
  1945. * Return true if the device supports the provided flags. Automatically
  1946. * filters out flags that are not implemented on multifunction devices.
  1947. *
  1948. * Note that this interface checks the effective ACS capabilities of the
  1949. * device rather than the actual capabilities. For instance, most single
  1950. * function endpoints are not required to support ACS because they have no
  1951. * opportunity for peer-to-peer access. We therefore return 'true'
  1952. * regardless of whether the device exposes an ACS capability. This makes
  1953. * it much easier for callers of this function to ignore the actual type
  1954. * or topology of the device when testing ACS support.
  1955. */
  1956. bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
  1957. {
  1958. int ret;
  1959. ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
  1960. if (ret >= 0)
  1961. return ret > 0;
  1962. /*
  1963. * Conventional PCI and PCI-X devices never support ACS, either
  1964. * effectively or actually. The shared bus topology implies that
  1965. * any device on the bus can receive or snoop DMA.
  1966. */
  1967. if (!pci_is_pcie(pdev))
  1968. return false;
  1969. switch (pci_pcie_type(pdev)) {
  1970. /*
  1971. * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
  1972. * but since their primary interface is PCI/X, we conservatively
  1973. * handle them as we would a non-PCIe device.
  1974. */
  1975. case PCI_EXP_TYPE_PCIE_BRIDGE:
  1976. /*
  1977. * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
  1978. * applicable... must never implement an ACS Extended Capability...".
  1979. * This seems arbitrary, but we take a conservative interpretation
  1980. * of this statement.
  1981. */
  1982. case PCI_EXP_TYPE_PCI_BRIDGE:
  1983. case PCI_EXP_TYPE_RC_EC:
  1984. return false;
  1985. /*
  1986. * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
  1987. * implement ACS in order to indicate their peer-to-peer capabilities,
  1988. * regardless of whether they are single- or multi-function devices.
  1989. */
  1990. case PCI_EXP_TYPE_DOWNSTREAM:
  1991. case PCI_EXP_TYPE_ROOT_PORT:
  1992. return pci_acs_flags_enabled(pdev, acs_flags);
  1993. /*
  1994. * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
  1995. * implemented by the remaining PCIe types to indicate peer-to-peer
  1996. * capabilities, but only when they are part of a multifunction
  1997. * device. The footnote for section 6.12 indicates the specific
  1998. * PCIe types included here.
  1999. */
  2000. case PCI_EXP_TYPE_ENDPOINT:
  2001. case PCI_EXP_TYPE_UPSTREAM:
  2002. case PCI_EXP_TYPE_LEG_END:
  2003. case PCI_EXP_TYPE_RC_END:
  2004. if (!pdev->multifunction)
  2005. break;
  2006. return pci_acs_flags_enabled(pdev, acs_flags);
  2007. }
  2008. /*
  2009. * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable
  2010. * to single function devices with the exception of downstream ports.
  2011. */
  2012. return true;
  2013. }
  2014. /**
  2015. * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
  2016. * @start: starting downstream device
  2017. * @end: ending upstream device or NULL to search to the root bus
  2018. * @acs_flags: required flags
  2019. *
  2020. * Walk up a device tree from start to end testing PCI ACS support. If
  2021. * any step along the way does not support the required flags, return false.
  2022. */
  2023. bool pci_acs_path_enabled(struct pci_dev *start,
  2024. struct pci_dev *end, u16 acs_flags)
  2025. {
  2026. struct pci_dev *pdev, *parent = start;
  2027. do {
  2028. pdev = parent;
  2029. if (!pci_acs_enabled(pdev, acs_flags))
  2030. return false;
  2031. if (pci_is_root_bus(pdev->bus))
  2032. return (end == NULL);
  2033. parent = pdev->bus->self;
  2034. } while (pdev != end);
  2035. return true;
  2036. }
  2037. /**
  2038. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  2039. * @dev: the PCI device
  2040. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
  2041. *
  2042. * Perform INTx swizzling for a device behind one level of bridge. This is
  2043. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  2044. * behind bridges on add-in cards. For devices with ARI enabled, the slot
  2045. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  2046. * the PCI Express Base Specification, Revision 2.1)
  2047. */
  2048. u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
  2049. {
  2050. int slot;
  2051. if (pci_ari_enabled(dev->bus))
  2052. slot = 0;
  2053. else
  2054. slot = PCI_SLOT(dev->devfn);
  2055. return (((pin - 1) + slot) % 4) + 1;
  2056. }
  2057. int
  2058. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  2059. {
  2060. u8 pin;
  2061. pin = dev->pin;
  2062. if (!pin)
  2063. return -1;
  2064. while (!pci_is_root_bus(dev->bus)) {
  2065. pin = pci_swizzle_interrupt_pin(dev, pin);
  2066. dev = dev->bus->self;
  2067. }
  2068. *bridge = dev;
  2069. return pin;
  2070. }
  2071. /**
  2072. * pci_common_swizzle - swizzle INTx all the way to root bridge
  2073. * @dev: the PCI device
  2074. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  2075. *
  2076. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  2077. * bridges all the way up to a PCI root bus.
  2078. */
  2079. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  2080. {
  2081. u8 pin = *pinp;
  2082. while (!pci_is_root_bus(dev->bus)) {
  2083. pin = pci_swizzle_interrupt_pin(dev, pin);
  2084. dev = dev->bus->self;
  2085. }
  2086. *pinp = pin;
  2087. return PCI_SLOT(dev->devfn);
  2088. }
  2089. /**
  2090. * pci_release_region - Release a PCI bar
  2091. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  2092. * @bar: BAR to release
  2093. *
  2094. * Releases the PCI I/O and memory resources previously reserved by a
  2095. * successful call to pci_request_region. Call this function only
  2096. * after all use of the PCI regions has ceased.
  2097. */
  2098. void pci_release_region(struct pci_dev *pdev, int bar)
  2099. {
  2100. struct pci_devres *dr;
  2101. if (pci_resource_len(pdev, bar) == 0)
  2102. return;
  2103. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  2104. release_region(pci_resource_start(pdev, bar),
  2105. pci_resource_len(pdev, bar));
  2106. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  2107. release_mem_region(pci_resource_start(pdev, bar),
  2108. pci_resource_len(pdev, bar));
  2109. dr = find_pci_dr(pdev);
  2110. if (dr)
  2111. dr->region_mask &= ~(1 << bar);
  2112. }
  2113. /**
  2114. * __pci_request_region - Reserved PCI I/O and memory resource
  2115. * @pdev: PCI device whose resources are to be reserved
  2116. * @bar: BAR to be reserved
  2117. * @res_name: Name to be associated with resource.
  2118. * @exclusive: whether the region access is exclusive or not
  2119. *
  2120. * Mark the PCI region associated with PCI device @pdev BR @bar as
  2121. * being reserved by owner @res_name. Do not access any
  2122. * address inside the PCI regions unless this call returns
  2123. * successfully.
  2124. *
  2125. * If @exclusive is set, then the region is marked so that userspace
  2126. * is explicitly not allowed to map the resource via /dev/mem or
  2127. * sysfs MMIO access.
  2128. *
  2129. * Returns 0 on success, or %EBUSY on error. A warning
  2130. * message is also printed on failure.
  2131. */
  2132. static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
  2133. int exclusive)
  2134. {
  2135. struct pci_devres *dr;
  2136. if (pci_resource_len(pdev, bar) == 0)
  2137. return 0;
  2138. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  2139. if (!request_region(pci_resource_start(pdev, bar),
  2140. pci_resource_len(pdev, bar), res_name))
  2141. goto err_out;
  2142. }
  2143. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  2144. if (!__request_mem_region(pci_resource_start(pdev, bar),
  2145. pci_resource_len(pdev, bar), res_name,
  2146. exclusive))
  2147. goto err_out;
  2148. }
  2149. dr = find_pci_dr(pdev);
  2150. if (dr)
  2151. dr->region_mask |= 1 << bar;
  2152. return 0;
  2153. err_out:
  2154. dev_warn(&pdev->dev, "BAR %d: can't reserve %pR\n", bar,
  2155. &pdev->resource[bar]);
  2156. return -EBUSY;
  2157. }
  2158. /**
  2159. * pci_request_region - Reserve PCI I/O and memory resource
  2160. * @pdev: PCI device whose resources are to be reserved
  2161. * @bar: BAR to be reserved
  2162. * @res_name: Name to be associated with resource
  2163. *
  2164. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  2165. * being reserved by owner @res_name. Do not access any
  2166. * address inside the PCI regions unless this call returns
  2167. * successfully.
  2168. *
  2169. * Returns 0 on success, or %EBUSY on error. A warning
  2170. * message is also printed on failure.
  2171. */
  2172. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  2173. {
  2174. return __pci_request_region(pdev, bar, res_name, 0);
  2175. }
  2176. /**
  2177. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  2178. * @pdev: PCI device whose resources are to be reserved
  2179. * @bar: BAR to be reserved
  2180. * @res_name: Name to be associated with resource.
  2181. *
  2182. * Mark the PCI region associated with PCI device @pdev BR @bar as
  2183. * being reserved by owner @res_name. Do not access any
  2184. * address inside the PCI regions unless this call returns
  2185. * successfully.
  2186. *
  2187. * Returns 0 on success, or %EBUSY on error. A warning
  2188. * message is also printed on failure.
  2189. *
  2190. * The key difference that _exclusive makes it that userspace is
  2191. * explicitly not allowed to map the resource via /dev/mem or
  2192. * sysfs.
  2193. */
  2194. int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
  2195. {
  2196. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  2197. }
  2198. /**
  2199. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  2200. * @pdev: PCI device whose resources were previously reserved
  2201. * @bars: Bitmask of BARs to be released
  2202. *
  2203. * Release selected PCI I/O and memory resources previously reserved.
  2204. * Call this function only after all use of the PCI regions has ceased.
  2205. */
  2206. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  2207. {
  2208. int i;
  2209. for (i = 0; i < 6; i++)
  2210. if (bars & (1 << i))
  2211. pci_release_region(pdev, i);
  2212. }
  2213. static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  2214. const char *res_name, int excl)
  2215. {
  2216. int i;
  2217. for (i = 0; i < 6; i++)
  2218. if (bars & (1 << i))
  2219. if (__pci_request_region(pdev, i, res_name, excl))
  2220. goto err_out;
  2221. return 0;
  2222. err_out:
  2223. while(--i >= 0)
  2224. if (bars & (1 << i))
  2225. pci_release_region(pdev, i);
  2226. return -EBUSY;
  2227. }
  2228. /**
  2229. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  2230. * @pdev: PCI device whose resources are to be reserved
  2231. * @bars: Bitmask of BARs to be requested
  2232. * @res_name: Name to be associated with resource
  2233. */
  2234. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  2235. const char *res_name)
  2236. {
  2237. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  2238. }
  2239. int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
  2240. int bars, const char *res_name)
  2241. {
  2242. return __pci_request_selected_regions(pdev, bars, res_name,
  2243. IORESOURCE_EXCLUSIVE);
  2244. }
  2245. /**
  2246. * pci_release_regions - Release reserved PCI I/O and memory resources
  2247. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  2248. *
  2249. * Releases all PCI I/O and memory resources previously reserved by a
  2250. * successful call to pci_request_regions. Call this function only
  2251. * after all use of the PCI regions has ceased.
  2252. */
  2253. void pci_release_regions(struct pci_dev *pdev)
  2254. {
  2255. pci_release_selected_regions(pdev, (1 << 6) - 1);
  2256. }
  2257. /**
  2258. * pci_request_regions - Reserved PCI I/O and memory resources
  2259. * @pdev: PCI device whose resources are to be reserved
  2260. * @res_name: Name to be associated with resource.
  2261. *
  2262. * Mark all PCI regions associated with PCI device @pdev as
  2263. * being reserved by owner @res_name. Do not access any
  2264. * address inside the PCI regions unless this call returns
  2265. * successfully.
  2266. *
  2267. * Returns 0 on success, or %EBUSY on error. A warning
  2268. * message is also printed on failure.
  2269. */
  2270. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  2271. {
  2272. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  2273. }
  2274. /**
  2275. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  2276. * @pdev: PCI device whose resources are to be reserved
  2277. * @res_name: Name to be associated with resource.
  2278. *
  2279. * Mark all PCI regions associated with PCI device @pdev as
  2280. * being reserved by owner @res_name. Do not access any
  2281. * address inside the PCI regions unless this call returns
  2282. * successfully.
  2283. *
  2284. * pci_request_regions_exclusive() will mark the region so that
  2285. * /dev/mem and the sysfs MMIO access will not be allowed.
  2286. *
  2287. * Returns 0 on success, or %EBUSY on error. A warning
  2288. * message is also printed on failure.
  2289. */
  2290. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  2291. {
  2292. return pci_request_selected_regions_exclusive(pdev,
  2293. ((1 << 6) - 1), res_name);
  2294. }
  2295. static void __pci_set_master(struct pci_dev *dev, bool enable)
  2296. {
  2297. u16 old_cmd, cmd;
  2298. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  2299. if (enable)
  2300. cmd = old_cmd | PCI_COMMAND_MASTER;
  2301. else
  2302. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  2303. if (cmd != old_cmd) {
  2304. dev_dbg(&dev->dev, "%s bus mastering\n",
  2305. enable ? "enabling" : "disabling");
  2306. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2307. }
  2308. dev->is_busmaster = enable;
  2309. }
  2310. /**
  2311. * pcibios_setup - process "pci=" kernel boot arguments
  2312. * @str: string used to pass in "pci=" kernel boot arguments
  2313. *
  2314. * Process kernel boot arguments. This is the default implementation.
  2315. * Architecture specific implementations can override this as necessary.
  2316. */
  2317. char * __weak __init pcibios_setup(char *str)
  2318. {
  2319. return str;
  2320. }
  2321. /**
  2322. * pcibios_set_master - enable PCI bus-mastering for device dev
  2323. * @dev: the PCI device to enable
  2324. *
  2325. * Enables PCI bus-mastering for the device. This is the default
  2326. * implementation. Architecture specific implementations can override
  2327. * this if necessary.
  2328. */
  2329. void __weak pcibios_set_master(struct pci_dev *dev)
  2330. {
  2331. u8 lat;
  2332. /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
  2333. if (pci_is_pcie(dev))
  2334. return;
  2335. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  2336. if (lat < 16)
  2337. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  2338. else if (lat > pcibios_max_latency)
  2339. lat = pcibios_max_latency;
  2340. else
  2341. return;
  2342. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  2343. }
  2344. /**
  2345. * pci_set_master - enables bus-mastering for device dev
  2346. * @dev: the PCI device to enable
  2347. *
  2348. * Enables bus-mastering on the device and calls pcibios_set_master()
  2349. * to do the needed arch specific settings.
  2350. */
  2351. void pci_set_master(struct pci_dev *dev)
  2352. {
  2353. __pci_set_master(dev, true);
  2354. pcibios_set_master(dev);
  2355. }
  2356. /**
  2357. * pci_clear_master - disables bus-mastering for device dev
  2358. * @dev: the PCI device to disable
  2359. */
  2360. void pci_clear_master(struct pci_dev *dev)
  2361. {
  2362. __pci_set_master(dev, false);
  2363. }
  2364. /**
  2365. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  2366. * @dev: the PCI device for which MWI is to be enabled
  2367. *
  2368. * Helper function for pci_set_mwi.
  2369. * Originally copied from drivers/net/acenic.c.
  2370. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  2371. *
  2372. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2373. */
  2374. int pci_set_cacheline_size(struct pci_dev *dev)
  2375. {
  2376. u8 cacheline_size;
  2377. if (!pci_cache_line_size)
  2378. return -EINVAL;
  2379. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  2380. equal to or multiple of the right value. */
  2381. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  2382. if (cacheline_size >= pci_cache_line_size &&
  2383. (cacheline_size % pci_cache_line_size) == 0)
  2384. return 0;
  2385. /* Write the correct value. */
  2386. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  2387. /* Read it back. */
  2388. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  2389. if (cacheline_size == pci_cache_line_size)
  2390. return 0;
  2391. dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
  2392. "supported\n", pci_cache_line_size << 2);
  2393. return -EINVAL;
  2394. }
  2395. EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
  2396. #ifdef PCI_DISABLE_MWI
  2397. int pci_set_mwi(struct pci_dev *dev)
  2398. {
  2399. return 0;
  2400. }
  2401. int pci_try_set_mwi(struct pci_dev *dev)
  2402. {
  2403. return 0;
  2404. }
  2405. void pci_clear_mwi(struct pci_dev *dev)
  2406. {
  2407. }
  2408. #else
  2409. /**
  2410. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  2411. * @dev: the PCI device for which MWI is enabled
  2412. *
  2413. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  2414. *
  2415. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2416. */
  2417. int
  2418. pci_set_mwi(struct pci_dev *dev)
  2419. {
  2420. int rc;
  2421. u16 cmd;
  2422. rc = pci_set_cacheline_size(dev);
  2423. if (rc)
  2424. return rc;
  2425. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2426. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  2427. dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
  2428. cmd |= PCI_COMMAND_INVALIDATE;
  2429. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2430. }
  2431. return 0;
  2432. }
  2433. /**
  2434. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  2435. * @dev: the PCI device for which MWI is enabled
  2436. *
  2437. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  2438. * Callers are not required to check the return value.
  2439. *
  2440. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2441. */
  2442. int pci_try_set_mwi(struct pci_dev *dev)
  2443. {
  2444. int rc = pci_set_mwi(dev);
  2445. return rc;
  2446. }
  2447. /**
  2448. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  2449. * @dev: the PCI device to disable
  2450. *
  2451. * Disables PCI Memory-Write-Invalidate transaction on the device
  2452. */
  2453. void
  2454. pci_clear_mwi(struct pci_dev *dev)
  2455. {
  2456. u16 cmd;
  2457. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2458. if (cmd & PCI_COMMAND_INVALIDATE) {
  2459. cmd &= ~PCI_COMMAND_INVALIDATE;
  2460. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2461. }
  2462. }
  2463. #endif /* ! PCI_DISABLE_MWI */
  2464. /**
  2465. * pci_intx - enables/disables PCI INTx for device dev
  2466. * @pdev: the PCI device to operate on
  2467. * @enable: boolean: whether to enable or disable PCI INTx
  2468. *
  2469. * Enables/disables PCI INTx for device dev
  2470. */
  2471. void
  2472. pci_intx(struct pci_dev *pdev, int enable)
  2473. {
  2474. u16 pci_command, new;
  2475. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  2476. if (enable) {
  2477. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  2478. } else {
  2479. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  2480. }
  2481. if (new != pci_command) {
  2482. struct pci_devres *dr;
  2483. pci_write_config_word(pdev, PCI_COMMAND, new);
  2484. dr = find_pci_dr(pdev);
  2485. if (dr && !dr->restore_intx) {
  2486. dr->restore_intx = 1;
  2487. dr->orig_intx = !enable;
  2488. }
  2489. }
  2490. }
  2491. /**
  2492. * pci_intx_mask_supported - probe for INTx masking support
  2493. * @dev: the PCI device to operate on
  2494. *
  2495. * Check if the device dev support INTx masking via the config space
  2496. * command word.
  2497. */
  2498. bool pci_intx_mask_supported(struct pci_dev *dev)
  2499. {
  2500. bool mask_supported = false;
  2501. u16 orig, new;
  2502. if (dev->broken_intx_masking)
  2503. return false;
  2504. pci_cfg_access_lock(dev);
  2505. pci_read_config_word(dev, PCI_COMMAND, &orig);
  2506. pci_write_config_word(dev, PCI_COMMAND,
  2507. orig ^ PCI_COMMAND_INTX_DISABLE);
  2508. pci_read_config_word(dev, PCI_COMMAND, &new);
  2509. /*
  2510. * There's no way to protect against hardware bugs or detect them
  2511. * reliably, but as long as we know what the value should be, let's
  2512. * go ahead and check it.
  2513. */
  2514. if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
  2515. dev_err(&dev->dev, "Command register changed from "
  2516. "0x%x to 0x%x: driver or hardware bug?\n", orig, new);
  2517. } else if ((new ^ orig) & PCI_COMMAND_INTX_DISABLE) {
  2518. mask_supported = true;
  2519. pci_write_config_word(dev, PCI_COMMAND, orig);
  2520. }
  2521. pci_cfg_access_unlock(dev);
  2522. return mask_supported;
  2523. }
  2524. EXPORT_SYMBOL_GPL(pci_intx_mask_supported);
  2525. static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
  2526. {
  2527. struct pci_bus *bus = dev->bus;
  2528. bool mask_updated = true;
  2529. u32 cmd_status_dword;
  2530. u16 origcmd, newcmd;
  2531. unsigned long flags;
  2532. bool irq_pending;
  2533. /*
  2534. * We do a single dword read to retrieve both command and status.
  2535. * Document assumptions that make this possible.
  2536. */
  2537. BUILD_BUG_ON(PCI_COMMAND % 4);
  2538. BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
  2539. raw_spin_lock_irqsave(&pci_lock, flags);
  2540. bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
  2541. irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
  2542. /*
  2543. * Check interrupt status register to see whether our device
  2544. * triggered the interrupt (when masking) or the next IRQ is
  2545. * already pending (when unmasking).
  2546. */
  2547. if (mask != irq_pending) {
  2548. mask_updated = false;
  2549. goto done;
  2550. }
  2551. origcmd = cmd_status_dword;
  2552. newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
  2553. if (mask)
  2554. newcmd |= PCI_COMMAND_INTX_DISABLE;
  2555. if (newcmd != origcmd)
  2556. bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
  2557. done:
  2558. raw_spin_unlock_irqrestore(&pci_lock, flags);
  2559. return mask_updated;
  2560. }
  2561. /**
  2562. * pci_check_and_mask_intx - mask INTx on pending interrupt
  2563. * @dev: the PCI device to operate on
  2564. *
  2565. * Check if the device dev has its INTx line asserted, mask it and
  2566. * return true in that case. False is returned if not interrupt was
  2567. * pending.
  2568. */
  2569. bool pci_check_and_mask_intx(struct pci_dev *dev)
  2570. {
  2571. return pci_check_and_set_intx_mask(dev, true);
  2572. }
  2573. EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
  2574. /**
  2575. * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
  2576. * @dev: the PCI device to operate on
  2577. *
  2578. * Check if the device dev has its INTx line asserted, unmask it if not
  2579. * and return true. False is returned and the mask remains active if
  2580. * there was still an interrupt pending.
  2581. */
  2582. bool pci_check_and_unmask_intx(struct pci_dev *dev)
  2583. {
  2584. return pci_check_and_set_intx_mask(dev, false);
  2585. }
  2586. EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
  2587. /**
  2588. * pci_msi_off - disables any MSI or MSI-X capabilities
  2589. * @dev: the PCI device to operate on
  2590. *
  2591. * If you want to use MSI, see pci_enable_msi() and friends.
  2592. * This is a lower-level primitive that allows us to disable
  2593. * MSI operation at the device level.
  2594. */
  2595. void pci_msi_off(struct pci_dev *dev)
  2596. {
  2597. int pos;
  2598. u16 control;
  2599. /*
  2600. * This looks like it could go in msi.c, but we need it even when
  2601. * CONFIG_PCI_MSI=n. For the same reason, we can't use
  2602. * dev->msi_cap or dev->msix_cap here.
  2603. */
  2604. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  2605. if (pos) {
  2606. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  2607. control &= ~PCI_MSI_FLAGS_ENABLE;
  2608. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  2609. }
  2610. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  2611. if (pos) {
  2612. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  2613. control &= ~PCI_MSIX_FLAGS_ENABLE;
  2614. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  2615. }
  2616. }
  2617. EXPORT_SYMBOL_GPL(pci_msi_off);
  2618. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  2619. {
  2620. return dma_set_max_seg_size(&dev->dev, size);
  2621. }
  2622. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  2623. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  2624. {
  2625. return dma_set_seg_boundary(&dev->dev, mask);
  2626. }
  2627. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  2628. /**
  2629. * pci_wait_for_pending_transaction - waits for pending transaction
  2630. * @dev: the PCI device to operate on
  2631. *
  2632. * Return 0 if transaction is pending 1 otherwise.
  2633. */
  2634. int pci_wait_for_pending_transaction(struct pci_dev *dev)
  2635. {
  2636. if (!pci_is_pcie(dev))
  2637. return 1;
  2638. return pci_wait_for_pending(dev, PCI_EXP_DEVSTA, PCI_EXP_DEVSTA_TRPND);
  2639. }
  2640. EXPORT_SYMBOL(pci_wait_for_pending_transaction);
  2641. static int pcie_flr(struct pci_dev *dev, int probe)
  2642. {
  2643. u32 cap;
  2644. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
  2645. if (!(cap & PCI_EXP_DEVCAP_FLR))
  2646. return -ENOTTY;
  2647. if (probe)
  2648. return 0;
  2649. if (!pci_wait_for_pending_transaction(dev))
  2650. dev_err(&dev->dev, "transaction is not cleared; proceeding with reset anyway\n");
  2651. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
  2652. msleep(100);
  2653. return 0;
  2654. }
  2655. static int pci_af_flr(struct pci_dev *dev, int probe)
  2656. {
  2657. int pos;
  2658. u8 cap;
  2659. pos = pci_find_capability(dev, PCI_CAP_ID_AF);
  2660. if (!pos)
  2661. return -ENOTTY;
  2662. pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
  2663. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  2664. return -ENOTTY;
  2665. if (probe)
  2666. return 0;
  2667. /* Wait for Transaction Pending bit clean */
  2668. if (pci_wait_for_pending(dev, PCI_AF_STATUS, PCI_AF_STATUS_TP))
  2669. goto clear;
  2670. dev_err(&dev->dev, "transaction is not cleared; "
  2671. "proceeding with reset anyway\n");
  2672. clear:
  2673. pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  2674. msleep(100);
  2675. return 0;
  2676. }
  2677. /**
  2678. * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
  2679. * @dev: Device to reset.
  2680. * @probe: If set, only check if the device can be reset this way.
  2681. *
  2682. * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
  2683. * unset, it will be reinitialized internally when going from PCI_D3hot to
  2684. * PCI_D0. If that's the case and the device is not in a low-power state
  2685. * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
  2686. *
  2687. * NOTE: This causes the caller to sleep for twice the device power transition
  2688. * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
  2689. * by default (i.e. unless the @dev's d3_delay field has a different value).
  2690. * Moreover, only devices in D0 can be reset by this function.
  2691. */
  2692. static int pci_pm_reset(struct pci_dev *dev, int probe)
  2693. {
  2694. u16 csr;
  2695. if (!dev->pm_cap)
  2696. return -ENOTTY;
  2697. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
  2698. if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
  2699. return -ENOTTY;
  2700. if (probe)
  2701. return 0;
  2702. if (dev->current_state != PCI_D0)
  2703. return -EINVAL;
  2704. csr &= ~PCI_PM_CTRL_STATE_MASK;
  2705. csr |= PCI_D3hot;
  2706. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  2707. pci_dev_d3_sleep(dev);
  2708. csr &= ~PCI_PM_CTRL_STATE_MASK;
  2709. csr |= PCI_D0;
  2710. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  2711. pci_dev_d3_sleep(dev);
  2712. return 0;
  2713. }
  2714. /**
  2715. * pci_reset_bridge_secondary_bus - Reset the secondary bus on a PCI bridge.
  2716. * @dev: Bridge device
  2717. *
  2718. * Use the bridge control register to assert reset on the secondary bus.
  2719. * Devices on the secondary bus are left in power-on state.
  2720. */
  2721. void pci_reset_bridge_secondary_bus(struct pci_dev *dev)
  2722. {
  2723. u16 ctrl;
  2724. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
  2725. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  2726. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  2727. /*
  2728. * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double
  2729. * this to 2ms to ensure that we meet the minimum requirement.
  2730. */
  2731. msleep(2);
  2732. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  2733. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  2734. /*
  2735. * Trhfa for conventional PCI is 2^25 clock cycles.
  2736. * Assuming a minimum 33MHz clock this results in a 1s
  2737. * delay before we can consider subordinate devices to
  2738. * be re-initialized. PCIe has some ways to shorten this,
  2739. * but we don't make use of them yet.
  2740. */
  2741. ssleep(1);
  2742. }
  2743. EXPORT_SYMBOL_GPL(pci_reset_bridge_secondary_bus);
  2744. static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
  2745. {
  2746. struct pci_dev *pdev;
  2747. if (pci_is_root_bus(dev->bus) || dev->subordinate || !dev->bus->self)
  2748. return -ENOTTY;
  2749. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  2750. if (pdev != dev)
  2751. return -ENOTTY;
  2752. if (probe)
  2753. return 0;
  2754. pci_reset_bridge_secondary_bus(dev->bus->self);
  2755. return 0;
  2756. }
  2757. static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, int probe)
  2758. {
  2759. int rc = -ENOTTY;
  2760. if (!hotplug || !try_module_get(hotplug->ops->owner))
  2761. return rc;
  2762. if (hotplug->ops->reset_slot)
  2763. rc = hotplug->ops->reset_slot(hotplug, probe);
  2764. module_put(hotplug->ops->owner);
  2765. return rc;
  2766. }
  2767. static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe)
  2768. {
  2769. struct pci_dev *pdev;
  2770. if (dev->subordinate || !dev->slot)
  2771. return -ENOTTY;
  2772. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  2773. if (pdev != dev && pdev->slot == dev->slot)
  2774. return -ENOTTY;
  2775. return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
  2776. }
  2777. static int __pci_dev_reset(struct pci_dev *dev, int probe)
  2778. {
  2779. int rc;
  2780. might_sleep();
  2781. rc = pci_dev_specific_reset(dev, probe);
  2782. if (rc != -ENOTTY)
  2783. goto done;
  2784. rc = pcie_flr(dev, probe);
  2785. if (rc != -ENOTTY)
  2786. goto done;
  2787. rc = pci_af_flr(dev, probe);
  2788. if (rc != -ENOTTY)
  2789. goto done;
  2790. rc = pci_pm_reset(dev, probe);
  2791. if (rc != -ENOTTY)
  2792. goto done;
  2793. rc = pci_dev_reset_slot_function(dev, probe);
  2794. if (rc != -ENOTTY)
  2795. goto done;
  2796. rc = pci_parent_bus_reset(dev, probe);
  2797. done:
  2798. return rc;
  2799. }
  2800. static void pci_dev_lock(struct pci_dev *dev)
  2801. {
  2802. pci_cfg_access_lock(dev);
  2803. /* block PM suspend, driver probe, etc. */
  2804. device_lock(&dev->dev);
  2805. }
  2806. /* Return 1 on successful lock, 0 on contention */
  2807. static int pci_dev_trylock(struct pci_dev *dev)
  2808. {
  2809. if (pci_cfg_access_trylock(dev)) {
  2810. if (device_trylock(&dev->dev))
  2811. return 1;
  2812. pci_cfg_access_unlock(dev);
  2813. }
  2814. return 0;
  2815. }
  2816. static void pci_dev_unlock(struct pci_dev *dev)
  2817. {
  2818. device_unlock(&dev->dev);
  2819. pci_cfg_access_unlock(dev);
  2820. }
  2821. static void pci_dev_save_and_disable(struct pci_dev *dev)
  2822. {
  2823. /*
  2824. * Wake-up device prior to save. PM registers default to D0 after
  2825. * reset and a simple register restore doesn't reliably return
  2826. * to a non-D0 state anyway.
  2827. */
  2828. pci_set_power_state(dev, PCI_D0);
  2829. pci_save_state(dev);
  2830. /*
  2831. * Disable the device by clearing the Command register, except for
  2832. * INTx-disable which is set. This not only disables MMIO and I/O port
  2833. * BARs, but also prevents the device from being Bus Master, preventing
  2834. * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
  2835. * compliant devices, INTx-disable prevents legacy interrupts.
  2836. */
  2837. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  2838. }
  2839. static void pci_dev_restore(struct pci_dev *dev)
  2840. {
  2841. pci_restore_state(dev);
  2842. }
  2843. static int pci_dev_reset(struct pci_dev *dev, int probe)
  2844. {
  2845. int rc;
  2846. if (!probe)
  2847. pci_dev_lock(dev);
  2848. rc = __pci_dev_reset(dev, probe);
  2849. if (!probe)
  2850. pci_dev_unlock(dev);
  2851. return rc;
  2852. }
  2853. /**
  2854. * __pci_reset_function - reset a PCI device function
  2855. * @dev: PCI device to reset
  2856. *
  2857. * Some devices allow an individual function to be reset without affecting
  2858. * other functions in the same device. The PCI device must be responsive
  2859. * to PCI config space in order to use this function.
  2860. *
  2861. * The device function is presumed to be unused when this function is called.
  2862. * Resetting the device will make the contents of PCI configuration space
  2863. * random, so any caller of this must be prepared to reinitialise the
  2864. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  2865. * etc.
  2866. *
  2867. * Returns 0 if the device function was successfully reset or negative if the
  2868. * device doesn't support resetting a single function.
  2869. */
  2870. int __pci_reset_function(struct pci_dev *dev)
  2871. {
  2872. return pci_dev_reset(dev, 0);
  2873. }
  2874. EXPORT_SYMBOL_GPL(__pci_reset_function);
  2875. /**
  2876. * __pci_reset_function_locked - reset a PCI device function while holding
  2877. * the @dev mutex lock.
  2878. * @dev: PCI device to reset
  2879. *
  2880. * Some devices allow an individual function to be reset without affecting
  2881. * other functions in the same device. The PCI device must be responsive
  2882. * to PCI config space in order to use this function.
  2883. *
  2884. * The device function is presumed to be unused and the caller is holding
  2885. * the device mutex lock when this function is called.
  2886. * Resetting the device will make the contents of PCI configuration space
  2887. * random, so any caller of this must be prepared to reinitialise the
  2888. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  2889. * etc.
  2890. *
  2891. * Returns 0 if the device function was successfully reset or negative if the
  2892. * device doesn't support resetting a single function.
  2893. */
  2894. int __pci_reset_function_locked(struct pci_dev *dev)
  2895. {
  2896. return __pci_dev_reset(dev, 0);
  2897. }
  2898. EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
  2899. /**
  2900. * pci_probe_reset_function - check whether the device can be safely reset
  2901. * @dev: PCI device to reset
  2902. *
  2903. * Some devices allow an individual function to be reset without affecting
  2904. * other functions in the same device. The PCI device must be responsive
  2905. * to PCI config space in order to use this function.
  2906. *
  2907. * Returns 0 if the device function can be reset or negative if the
  2908. * device doesn't support resetting a single function.
  2909. */
  2910. int pci_probe_reset_function(struct pci_dev *dev)
  2911. {
  2912. return pci_dev_reset(dev, 1);
  2913. }
  2914. /**
  2915. * pci_reset_function - quiesce and reset a PCI device function
  2916. * @dev: PCI device to reset
  2917. *
  2918. * Some devices allow an individual function to be reset without affecting
  2919. * other functions in the same device. The PCI device must be responsive
  2920. * to PCI config space in order to use this function.
  2921. *
  2922. * This function does not just reset the PCI portion of a device, but
  2923. * clears all the state associated with the device. This function differs
  2924. * from __pci_reset_function in that it saves and restores device state
  2925. * over the reset.
  2926. *
  2927. * Returns 0 if the device function was successfully reset or negative if the
  2928. * device doesn't support resetting a single function.
  2929. */
  2930. int pci_reset_function(struct pci_dev *dev)
  2931. {
  2932. int rc;
  2933. rc = pci_dev_reset(dev, 1);
  2934. if (rc)
  2935. return rc;
  2936. pci_dev_save_and_disable(dev);
  2937. rc = pci_dev_reset(dev, 0);
  2938. pci_dev_restore(dev);
  2939. return rc;
  2940. }
  2941. EXPORT_SYMBOL_GPL(pci_reset_function);
  2942. /**
  2943. * pci_try_reset_function - quiesce and reset a PCI device function
  2944. * @dev: PCI device to reset
  2945. *
  2946. * Same as above, except return -EAGAIN if unable to lock device.
  2947. */
  2948. int pci_try_reset_function(struct pci_dev *dev)
  2949. {
  2950. int rc;
  2951. rc = pci_dev_reset(dev, 1);
  2952. if (rc)
  2953. return rc;
  2954. pci_dev_save_and_disable(dev);
  2955. if (pci_dev_trylock(dev)) {
  2956. rc = __pci_dev_reset(dev, 0);
  2957. pci_dev_unlock(dev);
  2958. } else
  2959. rc = -EAGAIN;
  2960. pci_dev_restore(dev);
  2961. return rc;
  2962. }
  2963. EXPORT_SYMBOL_GPL(pci_try_reset_function);
  2964. /* Lock devices from the top of the tree down */
  2965. static void pci_bus_lock(struct pci_bus *bus)
  2966. {
  2967. struct pci_dev *dev;
  2968. list_for_each_entry(dev, &bus->devices, bus_list) {
  2969. pci_dev_lock(dev);
  2970. if (dev->subordinate)
  2971. pci_bus_lock(dev->subordinate);
  2972. }
  2973. }
  2974. /* Unlock devices from the bottom of the tree up */
  2975. static void pci_bus_unlock(struct pci_bus *bus)
  2976. {
  2977. struct pci_dev *dev;
  2978. list_for_each_entry(dev, &bus->devices, bus_list) {
  2979. if (dev->subordinate)
  2980. pci_bus_unlock(dev->subordinate);
  2981. pci_dev_unlock(dev);
  2982. }
  2983. }
  2984. /* Return 1 on successful lock, 0 on contention */
  2985. static int pci_bus_trylock(struct pci_bus *bus)
  2986. {
  2987. struct pci_dev *dev;
  2988. list_for_each_entry(dev, &bus->devices, bus_list) {
  2989. if (!pci_dev_trylock(dev))
  2990. goto unlock;
  2991. if (dev->subordinate) {
  2992. if (!pci_bus_trylock(dev->subordinate)) {
  2993. pci_dev_unlock(dev);
  2994. goto unlock;
  2995. }
  2996. }
  2997. }
  2998. return 1;
  2999. unlock:
  3000. list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
  3001. if (dev->subordinate)
  3002. pci_bus_unlock(dev->subordinate);
  3003. pci_dev_unlock(dev);
  3004. }
  3005. return 0;
  3006. }
  3007. /* Lock devices from the top of the tree down */
  3008. static void pci_slot_lock(struct pci_slot *slot)
  3009. {
  3010. struct pci_dev *dev;
  3011. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3012. if (!dev->slot || dev->slot != slot)
  3013. continue;
  3014. pci_dev_lock(dev);
  3015. if (dev->subordinate)
  3016. pci_bus_lock(dev->subordinate);
  3017. }
  3018. }
  3019. /* Unlock devices from the bottom of the tree up */
  3020. static void pci_slot_unlock(struct pci_slot *slot)
  3021. {
  3022. struct pci_dev *dev;
  3023. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3024. if (!dev->slot || dev->slot != slot)
  3025. continue;
  3026. if (dev->subordinate)
  3027. pci_bus_unlock(dev->subordinate);
  3028. pci_dev_unlock(dev);
  3029. }
  3030. }
  3031. /* Return 1 on successful lock, 0 on contention */
  3032. static int pci_slot_trylock(struct pci_slot *slot)
  3033. {
  3034. struct pci_dev *dev;
  3035. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3036. if (!dev->slot || dev->slot != slot)
  3037. continue;
  3038. if (!pci_dev_trylock(dev))
  3039. goto unlock;
  3040. if (dev->subordinate) {
  3041. if (!pci_bus_trylock(dev->subordinate)) {
  3042. pci_dev_unlock(dev);
  3043. goto unlock;
  3044. }
  3045. }
  3046. }
  3047. return 1;
  3048. unlock:
  3049. list_for_each_entry_continue_reverse(dev,
  3050. &slot->bus->devices, bus_list) {
  3051. if (!dev->slot || dev->slot != slot)
  3052. continue;
  3053. if (dev->subordinate)
  3054. pci_bus_unlock(dev->subordinate);
  3055. pci_dev_unlock(dev);
  3056. }
  3057. return 0;
  3058. }
  3059. /* Save and disable devices from the top of the tree down */
  3060. static void pci_bus_save_and_disable(struct pci_bus *bus)
  3061. {
  3062. struct pci_dev *dev;
  3063. list_for_each_entry(dev, &bus->devices, bus_list) {
  3064. pci_dev_save_and_disable(dev);
  3065. if (dev->subordinate)
  3066. pci_bus_save_and_disable(dev->subordinate);
  3067. }
  3068. }
  3069. /*
  3070. * Restore devices from top of the tree down - parent bridges need to be
  3071. * restored before we can get to subordinate devices.
  3072. */
  3073. static void pci_bus_restore(struct pci_bus *bus)
  3074. {
  3075. struct pci_dev *dev;
  3076. list_for_each_entry(dev, &bus->devices, bus_list) {
  3077. pci_dev_restore(dev);
  3078. if (dev->subordinate)
  3079. pci_bus_restore(dev->subordinate);
  3080. }
  3081. }
  3082. /* Save and disable devices from the top of the tree down */
  3083. static void pci_slot_save_and_disable(struct pci_slot *slot)
  3084. {
  3085. struct pci_dev *dev;
  3086. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3087. if (!dev->slot || dev->slot != slot)
  3088. continue;
  3089. pci_dev_save_and_disable(dev);
  3090. if (dev->subordinate)
  3091. pci_bus_save_and_disable(dev->subordinate);
  3092. }
  3093. }
  3094. /*
  3095. * Restore devices from top of the tree down - parent bridges need to be
  3096. * restored before we can get to subordinate devices.
  3097. */
  3098. static void pci_slot_restore(struct pci_slot *slot)
  3099. {
  3100. struct pci_dev *dev;
  3101. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3102. if (!dev->slot || dev->slot != slot)
  3103. continue;
  3104. pci_dev_restore(dev);
  3105. if (dev->subordinate)
  3106. pci_bus_restore(dev->subordinate);
  3107. }
  3108. }
  3109. static int pci_slot_reset(struct pci_slot *slot, int probe)
  3110. {
  3111. int rc;
  3112. if (!slot)
  3113. return -ENOTTY;
  3114. if (!probe)
  3115. pci_slot_lock(slot);
  3116. might_sleep();
  3117. rc = pci_reset_hotplug_slot(slot->hotplug, probe);
  3118. if (!probe)
  3119. pci_slot_unlock(slot);
  3120. return rc;
  3121. }
  3122. /**
  3123. * pci_probe_reset_slot - probe whether a PCI slot can be reset
  3124. * @slot: PCI slot to probe
  3125. *
  3126. * Return 0 if slot can be reset, negative if a slot reset is not supported.
  3127. */
  3128. int pci_probe_reset_slot(struct pci_slot *slot)
  3129. {
  3130. return pci_slot_reset(slot, 1);
  3131. }
  3132. EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
  3133. /**
  3134. * pci_reset_slot - reset a PCI slot
  3135. * @slot: PCI slot to reset
  3136. *
  3137. * A PCI bus may host multiple slots, each slot may support a reset mechanism
  3138. * independent of other slots. For instance, some slots may support slot power
  3139. * control. In the case of a 1:1 bus to slot architecture, this function may
  3140. * wrap the bus reset to avoid spurious slot related events such as hotplug.
  3141. * Generally a slot reset should be attempted before a bus reset. All of the
  3142. * function of the slot and any subordinate buses behind the slot are reset
  3143. * through this function. PCI config space of all devices in the slot and
  3144. * behind the slot is saved before and restored after reset.
  3145. *
  3146. * Return 0 on success, non-zero on error.
  3147. */
  3148. int pci_reset_slot(struct pci_slot *slot)
  3149. {
  3150. int rc;
  3151. rc = pci_slot_reset(slot, 1);
  3152. if (rc)
  3153. return rc;
  3154. pci_slot_save_and_disable(slot);
  3155. rc = pci_slot_reset(slot, 0);
  3156. pci_slot_restore(slot);
  3157. return rc;
  3158. }
  3159. EXPORT_SYMBOL_GPL(pci_reset_slot);
  3160. /**
  3161. * pci_try_reset_slot - Try to reset a PCI slot
  3162. * @slot: PCI slot to reset
  3163. *
  3164. * Same as above except return -EAGAIN if the slot cannot be locked
  3165. */
  3166. int pci_try_reset_slot(struct pci_slot *slot)
  3167. {
  3168. int rc;
  3169. rc = pci_slot_reset(slot, 1);
  3170. if (rc)
  3171. return rc;
  3172. pci_slot_save_and_disable(slot);
  3173. if (pci_slot_trylock(slot)) {
  3174. might_sleep();
  3175. rc = pci_reset_hotplug_slot(slot->hotplug, 0);
  3176. pci_slot_unlock(slot);
  3177. } else
  3178. rc = -EAGAIN;
  3179. pci_slot_restore(slot);
  3180. return rc;
  3181. }
  3182. EXPORT_SYMBOL_GPL(pci_try_reset_slot);
  3183. static int pci_bus_reset(struct pci_bus *bus, int probe)
  3184. {
  3185. if (!bus->self)
  3186. return -ENOTTY;
  3187. if (probe)
  3188. return 0;
  3189. pci_bus_lock(bus);
  3190. might_sleep();
  3191. pci_reset_bridge_secondary_bus(bus->self);
  3192. pci_bus_unlock(bus);
  3193. return 0;
  3194. }
  3195. /**
  3196. * pci_probe_reset_bus - probe whether a PCI bus can be reset
  3197. * @bus: PCI bus to probe
  3198. *
  3199. * Return 0 if bus can be reset, negative if a bus reset is not supported.
  3200. */
  3201. int pci_probe_reset_bus(struct pci_bus *bus)
  3202. {
  3203. return pci_bus_reset(bus, 1);
  3204. }
  3205. EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
  3206. /**
  3207. * pci_reset_bus - reset a PCI bus
  3208. * @bus: top level PCI bus to reset
  3209. *
  3210. * Do a bus reset on the given bus and any subordinate buses, saving
  3211. * and restoring state of all devices.
  3212. *
  3213. * Return 0 on success, non-zero on error.
  3214. */
  3215. int pci_reset_bus(struct pci_bus *bus)
  3216. {
  3217. int rc;
  3218. rc = pci_bus_reset(bus, 1);
  3219. if (rc)
  3220. return rc;
  3221. pci_bus_save_and_disable(bus);
  3222. rc = pci_bus_reset(bus, 0);
  3223. pci_bus_restore(bus);
  3224. return rc;
  3225. }
  3226. EXPORT_SYMBOL_GPL(pci_reset_bus);
  3227. /**
  3228. * pci_try_reset_bus - Try to reset a PCI bus
  3229. * @bus: top level PCI bus to reset
  3230. *
  3231. * Same as above except return -EAGAIN if the bus cannot be locked
  3232. */
  3233. int pci_try_reset_bus(struct pci_bus *bus)
  3234. {
  3235. int rc;
  3236. rc = pci_bus_reset(bus, 1);
  3237. if (rc)
  3238. return rc;
  3239. pci_bus_save_and_disable(bus);
  3240. if (pci_bus_trylock(bus)) {
  3241. might_sleep();
  3242. pci_reset_bridge_secondary_bus(bus->self);
  3243. pci_bus_unlock(bus);
  3244. } else
  3245. rc = -EAGAIN;
  3246. pci_bus_restore(bus);
  3247. return rc;
  3248. }
  3249. EXPORT_SYMBOL_GPL(pci_try_reset_bus);
  3250. /**
  3251. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  3252. * @dev: PCI device to query
  3253. *
  3254. * Returns mmrbc: maximum designed memory read count in bytes
  3255. * or appropriate error value.
  3256. */
  3257. int pcix_get_max_mmrbc(struct pci_dev *dev)
  3258. {
  3259. int cap;
  3260. u32 stat;
  3261. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  3262. if (!cap)
  3263. return -EINVAL;
  3264. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  3265. return -EINVAL;
  3266. return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
  3267. }
  3268. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  3269. /**
  3270. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  3271. * @dev: PCI device to query
  3272. *
  3273. * Returns mmrbc: maximum memory read count in bytes
  3274. * or appropriate error value.
  3275. */
  3276. int pcix_get_mmrbc(struct pci_dev *dev)
  3277. {
  3278. int cap;
  3279. u16 cmd;
  3280. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  3281. if (!cap)
  3282. return -EINVAL;
  3283. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  3284. return -EINVAL;
  3285. return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  3286. }
  3287. EXPORT_SYMBOL(pcix_get_mmrbc);
  3288. /**
  3289. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  3290. * @dev: PCI device to query
  3291. * @mmrbc: maximum memory read count in bytes
  3292. * valid values are 512, 1024, 2048, 4096
  3293. *
  3294. * If possible sets maximum memory read byte count, some bridges have erratas
  3295. * that prevent this.
  3296. */
  3297. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  3298. {
  3299. int cap;
  3300. u32 stat, v, o;
  3301. u16 cmd;
  3302. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  3303. return -EINVAL;
  3304. v = ffs(mmrbc) - 10;
  3305. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  3306. if (!cap)
  3307. return -EINVAL;
  3308. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  3309. return -EINVAL;
  3310. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  3311. return -E2BIG;
  3312. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  3313. return -EINVAL;
  3314. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  3315. if (o != v) {
  3316. if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  3317. return -EIO;
  3318. cmd &= ~PCI_X_CMD_MAX_READ;
  3319. cmd |= v << 2;
  3320. if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
  3321. return -EIO;
  3322. }
  3323. return 0;
  3324. }
  3325. EXPORT_SYMBOL(pcix_set_mmrbc);
  3326. /**
  3327. * pcie_get_readrq - get PCI Express read request size
  3328. * @dev: PCI device to query
  3329. *
  3330. * Returns maximum memory read request in bytes
  3331. * or appropriate error value.
  3332. */
  3333. int pcie_get_readrq(struct pci_dev *dev)
  3334. {
  3335. u16 ctl;
  3336. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  3337. return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  3338. }
  3339. EXPORT_SYMBOL(pcie_get_readrq);
  3340. /**
  3341. * pcie_set_readrq - set PCI Express maximum memory read request
  3342. * @dev: PCI device to query
  3343. * @rq: maximum memory read count in bytes
  3344. * valid values are 128, 256, 512, 1024, 2048, 4096
  3345. *
  3346. * If possible sets maximum memory read request in bytes
  3347. */
  3348. int pcie_set_readrq(struct pci_dev *dev, int rq)
  3349. {
  3350. u16 v;
  3351. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  3352. return -EINVAL;
  3353. /*
  3354. * If using the "performance" PCIe config, we clamp the
  3355. * read rq size to the max packet size to prevent the
  3356. * host bridge generating requests larger than we can
  3357. * cope with
  3358. */
  3359. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  3360. int mps = pcie_get_mps(dev);
  3361. if (mps < rq)
  3362. rq = mps;
  3363. }
  3364. v = (ffs(rq) - 8) << 12;
  3365. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  3366. PCI_EXP_DEVCTL_READRQ, v);
  3367. }
  3368. EXPORT_SYMBOL(pcie_set_readrq);
  3369. /**
  3370. * pcie_get_mps - get PCI Express maximum payload size
  3371. * @dev: PCI device to query
  3372. *
  3373. * Returns maximum payload size in bytes
  3374. */
  3375. int pcie_get_mps(struct pci_dev *dev)
  3376. {
  3377. u16 ctl;
  3378. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  3379. return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
  3380. }
  3381. EXPORT_SYMBOL(pcie_get_mps);
  3382. /**
  3383. * pcie_set_mps - set PCI Express maximum payload size
  3384. * @dev: PCI device to query
  3385. * @mps: maximum payload size in bytes
  3386. * valid values are 128, 256, 512, 1024, 2048, 4096
  3387. *
  3388. * If possible sets maximum payload size
  3389. */
  3390. int pcie_set_mps(struct pci_dev *dev, int mps)
  3391. {
  3392. u16 v;
  3393. if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
  3394. return -EINVAL;
  3395. v = ffs(mps) - 8;
  3396. if (v > dev->pcie_mpss)
  3397. return -EINVAL;
  3398. v <<= 5;
  3399. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  3400. PCI_EXP_DEVCTL_PAYLOAD, v);
  3401. }
  3402. EXPORT_SYMBOL(pcie_set_mps);
  3403. /**
  3404. * pcie_get_minimum_link - determine minimum link settings of a PCI device
  3405. * @dev: PCI device to query
  3406. * @speed: storage for minimum speed
  3407. * @width: storage for minimum width
  3408. *
  3409. * This function will walk up the PCI device chain and determine the minimum
  3410. * link width and speed of the device.
  3411. */
  3412. int pcie_get_minimum_link(struct pci_dev *dev, enum pci_bus_speed *speed,
  3413. enum pcie_link_width *width)
  3414. {
  3415. int ret;
  3416. *speed = PCI_SPEED_UNKNOWN;
  3417. *width = PCIE_LNK_WIDTH_UNKNOWN;
  3418. while (dev) {
  3419. u16 lnksta;
  3420. enum pci_bus_speed next_speed;
  3421. enum pcie_link_width next_width;
  3422. ret = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
  3423. if (ret)
  3424. return ret;
  3425. next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
  3426. next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
  3427. PCI_EXP_LNKSTA_NLW_SHIFT;
  3428. if (next_speed < *speed)
  3429. *speed = next_speed;
  3430. if (next_width < *width)
  3431. *width = next_width;
  3432. dev = dev->bus->self;
  3433. }
  3434. return 0;
  3435. }
  3436. EXPORT_SYMBOL(pcie_get_minimum_link);
  3437. /**
  3438. * pci_select_bars - Make BAR mask from the type of resource
  3439. * @dev: the PCI device for which BAR mask is made
  3440. * @flags: resource type mask to be selected
  3441. *
  3442. * This helper routine makes bar mask from the type of resource.
  3443. */
  3444. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  3445. {
  3446. int i, bars = 0;
  3447. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  3448. if (pci_resource_flags(dev, i) & flags)
  3449. bars |= (1 << i);
  3450. return bars;
  3451. }
  3452. /**
  3453. * pci_resource_bar - get position of the BAR associated with a resource
  3454. * @dev: the PCI device
  3455. * @resno: the resource number
  3456. * @type: the BAR type to be filled in
  3457. *
  3458. * Returns BAR position in config space, or 0 if the BAR is invalid.
  3459. */
  3460. int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
  3461. {
  3462. int reg;
  3463. if (resno < PCI_ROM_RESOURCE) {
  3464. *type = pci_bar_unknown;
  3465. return PCI_BASE_ADDRESS_0 + 4 * resno;
  3466. } else if (resno == PCI_ROM_RESOURCE) {
  3467. *type = pci_bar_mem32;
  3468. return dev->rom_base_reg;
  3469. } else if (resno < PCI_BRIDGE_RESOURCES) {
  3470. /* device specific resource */
  3471. reg = pci_iov_resource_bar(dev, resno, type);
  3472. if (reg)
  3473. return reg;
  3474. }
  3475. dev_err(&dev->dev, "BAR %d: invalid resource\n", resno);
  3476. return 0;
  3477. }
  3478. /* Some architectures require additional programming to enable VGA */
  3479. static arch_set_vga_state_t arch_set_vga_state;
  3480. void __init pci_register_set_vga_state(arch_set_vga_state_t func)
  3481. {
  3482. arch_set_vga_state = func; /* NULL disables */
  3483. }
  3484. static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
  3485. unsigned int command_bits, u32 flags)
  3486. {
  3487. if (arch_set_vga_state)
  3488. return arch_set_vga_state(dev, decode, command_bits,
  3489. flags);
  3490. return 0;
  3491. }
  3492. /**
  3493. * pci_set_vga_state - set VGA decode state on device and parents if requested
  3494. * @dev: the PCI device
  3495. * @decode: true = enable decoding, false = disable decoding
  3496. * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
  3497. * @flags: traverse ancestors and change bridges
  3498. * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
  3499. */
  3500. int pci_set_vga_state(struct pci_dev *dev, bool decode,
  3501. unsigned int command_bits, u32 flags)
  3502. {
  3503. struct pci_bus *bus;
  3504. struct pci_dev *bridge;
  3505. u16 cmd;
  3506. int rc;
  3507. WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) & (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
  3508. /* ARCH specific VGA enables */
  3509. rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
  3510. if (rc)
  3511. return rc;
  3512. if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
  3513. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  3514. if (decode == true)
  3515. cmd |= command_bits;
  3516. else
  3517. cmd &= ~command_bits;
  3518. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3519. }
  3520. if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
  3521. return 0;
  3522. bus = dev->bus;
  3523. while (bus) {
  3524. bridge = bus->self;
  3525. if (bridge) {
  3526. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
  3527. &cmd);
  3528. if (decode == true)
  3529. cmd |= PCI_BRIDGE_CTL_VGA;
  3530. else
  3531. cmd &= ~PCI_BRIDGE_CTL_VGA;
  3532. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
  3533. cmd);
  3534. }
  3535. bus = bus->parent;
  3536. }
  3537. return 0;
  3538. }
  3539. bool pci_device_is_present(struct pci_dev *pdev)
  3540. {
  3541. u32 v;
  3542. return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0);
  3543. }
  3544. EXPORT_SYMBOL_GPL(pci_device_is_present);
  3545. #define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
  3546. static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
  3547. static DEFINE_SPINLOCK(resource_alignment_lock);
  3548. /**
  3549. * pci_specified_resource_alignment - get resource alignment specified by user.
  3550. * @dev: the PCI device to get
  3551. *
  3552. * RETURNS: Resource alignment if it is specified.
  3553. * Zero if it is not specified.
  3554. */
  3555. static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev)
  3556. {
  3557. int seg, bus, slot, func, align_order, count;
  3558. resource_size_t align = 0;
  3559. char *p;
  3560. spin_lock(&resource_alignment_lock);
  3561. p = resource_alignment_param;
  3562. while (*p) {
  3563. count = 0;
  3564. if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
  3565. p[count] == '@') {
  3566. p += count + 1;
  3567. } else {
  3568. align_order = -1;
  3569. }
  3570. if (sscanf(p, "%x:%x:%x.%x%n",
  3571. &seg, &bus, &slot, &func, &count) != 4) {
  3572. seg = 0;
  3573. if (sscanf(p, "%x:%x.%x%n",
  3574. &bus, &slot, &func, &count) != 3) {
  3575. /* Invalid format */
  3576. printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
  3577. p);
  3578. break;
  3579. }
  3580. }
  3581. p += count;
  3582. if (seg == pci_domain_nr(dev->bus) &&
  3583. bus == dev->bus->number &&
  3584. slot == PCI_SLOT(dev->devfn) &&
  3585. func == PCI_FUNC(dev->devfn)) {
  3586. if (align_order == -1) {
  3587. align = PAGE_SIZE;
  3588. } else {
  3589. align = 1 << align_order;
  3590. }
  3591. /* Found */
  3592. break;
  3593. }
  3594. if (*p != ';' && *p != ',') {
  3595. /* End of param or invalid format */
  3596. break;
  3597. }
  3598. p++;
  3599. }
  3600. spin_unlock(&resource_alignment_lock);
  3601. return align;
  3602. }
  3603. /*
  3604. * This function disables memory decoding and releases memory resources
  3605. * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
  3606. * It also rounds up size to specified alignment.
  3607. * Later on, the kernel will assign page-aligned memory resource back
  3608. * to the device.
  3609. */
  3610. void pci_reassigndev_resource_alignment(struct pci_dev *dev)
  3611. {
  3612. int i;
  3613. struct resource *r;
  3614. resource_size_t align, size;
  3615. u16 command;
  3616. /* check if specified PCI is target device to reassign */
  3617. align = pci_specified_resource_alignment(dev);
  3618. if (!align)
  3619. return;
  3620. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
  3621. (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
  3622. dev_warn(&dev->dev,
  3623. "Can't reassign resources to host bridge.\n");
  3624. return;
  3625. }
  3626. dev_info(&dev->dev,
  3627. "Disabling memory decoding and releasing memory resources.\n");
  3628. pci_read_config_word(dev, PCI_COMMAND, &command);
  3629. command &= ~PCI_COMMAND_MEMORY;
  3630. pci_write_config_word(dev, PCI_COMMAND, command);
  3631. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
  3632. r = &dev->resource[i];
  3633. if (!(r->flags & IORESOURCE_MEM))
  3634. continue;
  3635. size = resource_size(r);
  3636. if (size < align) {
  3637. size = align;
  3638. dev_info(&dev->dev,
  3639. "Rounding up size of resource #%d to %#llx.\n",
  3640. i, (unsigned long long)size);
  3641. }
  3642. r->flags |= IORESOURCE_UNSET;
  3643. r->end = size - 1;
  3644. r->start = 0;
  3645. }
  3646. /* Need to disable bridge's resource window,
  3647. * to enable the kernel to reassign new resource
  3648. * window later on.
  3649. */
  3650. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  3651. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  3652. for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
  3653. r = &dev->resource[i];
  3654. if (!(r->flags & IORESOURCE_MEM))
  3655. continue;
  3656. r->flags |= IORESOURCE_UNSET;
  3657. r->end = resource_size(r) - 1;
  3658. r->start = 0;
  3659. }
  3660. pci_disable_bridge_window(dev);
  3661. }
  3662. }
  3663. static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
  3664. {
  3665. if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
  3666. count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
  3667. spin_lock(&resource_alignment_lock);
  3668. strncpy(resource_alignment_param, buf, count);
  3669. resource_alignment_param[count] = '\0';
  3670. spin_unlock(&resource_alignment_lock);
  3671. return count;
  3672. }
  3673. static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
  3674. {
  3675. size_t count;
  3676. spin_lock(&resource_alignment_lock);
  3677. count = snprintf(buf, size, "%s", resource_alignment_param);
  3678. spin_unlock(&resource_alignment_lock);
  3679. return count;
  3680. }
  3681. static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
  3682. {
  3683. return pci_get_resource_alignment_param(buf, PAGE_SIZE);
  3684. }
  3685. static ssize_t pci_resource_alignment_store(struct bus_type *bus,
  3686. const char *buf, size_t count)
  3687. {
  3688. return pci_set_resource_alignment_param(buf, count);
  3689. }
  3690. BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
  3691. pci_resource_alignment_store);
  3692. static int __init pci_resource_alignment_sysfs_init(void)
  3693. {
  3694. return bus_create_file(&pci_bus_type,
  3695. &bus_attr_resource_alignment);
  3696. }
  3697. late_initcall(pci_resource_alignment_sysfs_init);
  3698. static void pci_no_domains(void)
  3699. {
  3700. #ifdef CONFIG_PCI_DOMAINS
  3701. pci_domains_supported = 0;
  3702. #endif
  3703. }
  3704. /**
  3705. * pci_ext_cfg_avail - can we access extended PCI config space?
  3706. *
  3707. * Returns 1 if we can access PCI extended config space (offsets
  3708. * greater than 0xff). This is the default implementation. Architecture
  3709. * implementations can override this.
  3710. */
  3711. int __weak pci_ext_cfg_avail(void)
  3712. {
  3713. return 1;
  3714. }
  3715. void __weak pci_fixup_cardbus(struct pci_bus *bus)
  3716. {
  3717. }
  3718. EXPORT_SYMBOL(pci_fixup_cardbus);
  3719. static int __init pci_setup(char *str)
  3720. {
  3721. while (str) {
  3722. char *k = strchr(str, ',');
  3723. if (k)
  3724. *k++ = 0;
  3725. if (*str && (str = pcibios_setup(str)) && *str) {
  3726. if (!strcmp(str, "nomsi")) {
  3727. pci_no_msi();
  3728. } else if (!strcmp(str, "noaer")) {
  3729. pci_no_aer();
  3730. } else if (!strncmp(str, "realloc=", 8)) {
  3731. pci_realloc_get_opt(str + 8);
  3732. } else if (!strncmp(str, "realloc", 7)) {
  3733. pci_realloc_get_opt("on");
  3734. } else if (!strcmp(str, "nodomains")) {
  3735. pci_no_domains();
  3736. } else if (!strncmp(str, "noari", 5)) {
  3737. pcie_ari_disabled = true;
  3738. } else if (!strncmp(str, "cbiosize=", 9)) {
  3739. pci_cardbus_io_size = memparse(str + 9, &str);
  3740. } else if (!strncmp(str, "cbmemsize=", 10)) {
  3741. pci_cardbus_mem_size = memparse(str + 10, &str);
  3742. } else if (!strncmp(str, "resource_alignment=", 19)) {
  3743. pci_set_resource_alignment_param(str + 19,
  3744. strlen(str + 19));
  3745. } else if (!strncmp(str, "ecrc=", 5)) {
  3746. pcie_ecrc_get_policy(str + 5);
  3747. } else if (!strncmp(str, "hpiosize=", 9)) {
  3748. pci_hotplug_io_size = memparse(str + 9, &str);
  3749. } else if (!strncmp(str, "hpmemsize=", 10)) {
  3750. pci_hotplug_mem_size = memparse(str + 10, &str);
  3751. } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
  3752. pcie_bus_config = PCIE_BUS_TUNE_OFF;
  3753. } else if (!strncmp(str, "pcie_bus_safe", 13)) {
  3754. pcie_bus_config = PCIE_BUS_SAFE;
  3755. } else if (!strncmp(str, "pcie_bus_perf", 13)) {
  3756. pcie_bus_config = PCIE_BUS_PERFORMANCE;
  3757. } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
  3758. pcie_bus_config = PCIE_BUS_PEER2PEER;
  3759. } else if (!strncmp(str, "pcie_scan_all", 13)) {
  3760. pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
  3761. } else {
  3762. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  3763. str);
  3764. }
  3765. }
  3766. str = k;
  3767. }
  3768. return 0;
  3769. }
  3770. early_param("pci", pci_setup);
  3771. EXPORT_SYMBOL(pci_reenable_device);
  3772. EXPORT_SYMBOL(pci_enable_device_io);
  3773. EXPORT_SYMBOL(pci_enable_device_mem);
  3774. EXPORT_SYMBOL(pci_enable_device);
  3775. EXPORT_SYMBOL(pcim_enable_device);
  3776. EXPORT_SYMBOL(pcim_pin_device);
  3777. EXPORT_SYMBOL(pci_disable_device);
  3778. EXPORT_SYMBOL(pci_find_capability);
  3779. EXPORT_SYMBOL(pci_bus_find_capability);
  3780. EXPORT_SYMBOL(pci_release_regions);
  3781. EXPORT_SYMBOL(pci_request_regions);
  3782. EXPORT_SYMBOL(pci_request_regions_exclusive);
  3783. EXPORT_SYMBOL(pci_release_region);
  3784. EXPORT_SYMBOL(pci_request_region);
  3785. EXPORT_SYMBOL(pci_request_region_exclusive);
  3786. EXPORT_SYMBOL(pci_release_selected_regions);
  3787. EXPORT_SYMBOL(pci_request_selected_regions);
  3788. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  3789. EXPORT_SYMBOL(pci_set_master);
  3790. EXPORT_SYMBOL(pci_clear_master);
  3791. EXPORT_SYMBOL(pci_set_mwi);
  3792. EXPORT_SYMBOL(pci_try_set_mwi);
  3793. EXPORT_SYMBOL(pci_clear_mwi);
  3794. EXPORT_SYMBOL_GPL(pci_intx);
  3795. EXPORT_SYMBOL(pci_assign_resource);
  3796. EXPORT_SYMBOL(pci_find_parent_resource);
  3797. EXPORT_SYMBOL(pci_select_bars);
  3798. EXPORT_SYMBOL(pci_set_power_state);
  3799. EXPORT_SYMBOL(pci_save_state);
  3800. EXPORT_SYMBOL(pci_restore_state);
  3801. EXPORT_SYMBOL(pci_pme_capable);
  3802. EXPORT_SYMBOL(pci_pme_active);
  3803. EXPORT_SYMBOL(pci_wake_from_d3);
  3804. EXPORT_SYMBOL(pci_prepare_to_sleep);
  3805. EXPORT_SYMBOL(pci_back_from_sleep);
  3806. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);