pci.c 115 KB

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