pci.c 104 KB

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