pci.c 115 KB

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