pci.c 119 KB

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