i915_irq.c 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559
  1. /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
  2. */
  3. /*
  4. * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  22. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  23. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/sysrq.h>
  30. #include <linux/slab.h>
  31. #include <linux/circ_buf.h>
  32. #include <drm/drmP.h>
  33. #include <drm/i915_drm.h>
  34. #include "i915_drv.h"
  35. #include "i915_trace.h"
  36. #include "intel_drv.h"
  37. /**
  38. * DOC: interrupt handling
  39. *
  40. * These functions provide the basic support for enabling and disabling the
  41. * interrupt handling support. There's a lot more functionality in i915_irq.c
  42. * and related files, but that will be described in separate chapters.
  43. */
  44. static const u32 hpd_ibx[HPD_NUM_PINS] = {
  45. [HPD_CRT] = SDE_CRT_HOTPLUG,
  46. [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
  47. [HPD_PORT_B] = SDE_PORTB_HOTPLUG,
  48. [HPD_PORT_C] = SDE_PORTC_HOTPLUG,
  49. [HPD_PORT_D] = SDE_PORTD_HOTPLUG
  50. };
  51. static const u32 hpd_cpt[HPD_NUM_PINS] = {
  52. [HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
  53. [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
  54. [HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
  55. [HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
  56. [HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
  57. };
  58. static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
  59. [HPD_CRT] = CRT_HOTPLUG_INT_EN,
  60. [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
  61. [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
  62. [HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
  63. [HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
  64. [HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
  65. };
  66. static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
  67. [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
  68. [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
  69. [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
  70. [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
  71. [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
  72. [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
  73. };
  74. static const u32 hpd_status_i915[HPD_NUM_PINS] = { /* i915 and valleyview are the same */
  75. [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
  76. [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
  77. [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
  78. [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
  79. [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
  80. [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
  81. };
  82. /* IIR can theoretically queue up two events. Be paranoid. */
  83. #define GEN8_IRQ_RESET_NDX(type, which) do { \
  84. I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
  85. POSTING_READ(GEN8_##type##_IMR(which)); \
  86. I915_WRITE(GEN8_##type##_IER(which), 0); \
  87. I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
  88. POSTING_READ(GEN8_##type##_IIR(which)); \
  89. I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
  90. POSTING_READ(GEN8_##type##_IIR(which)); \
  91. } while (0)
  92. #define GEN5_IRQ_RESET(type) do { \
  93. I915_WRITE(type##IMR, 0xffffffff); \
  94. POSTING_READ(type##IMR); \
  95. I915_WRITE(type##IER, 0); \
  96. I915_WRITE(type##IIR, 0xffffffff); \
  97. POSTING_READ(type##IIR); \
  98. I915_WRITE(type##IIR, 0xffffffff); \
  99. POSTING_READ(type##IIR); \
  100. } while (0)
  101. /*
  102. * We should clear IMR at preinstall/uninstall, and just check at postinstall.
  103. */
  104. #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
  105. u32 val = I915_READ(reg); \
  106. if (val) { \
  107. WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
  108. (reg), val); \
  109. I915_WRITE((reg), 0xffffffff); \
  110. POSTING_READ(reg); \
  111. I915_WRITE((reg), 0xffffffff); \
  112. POSTING_READ(reg); \
  113. } \
  114. } while (0)
  115. #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
  116. GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
  117. I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
  118. I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
  119. POSTING_READ(GEN8_##type##_IMR(which)); \
  120. } while (0)
  121. #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
  122. GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
  123. I915_WRITE(type##IER, (ier_val)); \
  124. I915_WRITE(type##IMR, (imr_val)); \
  125. POSTING_READ(type##IMR); \
  126. } while (0)
  127. static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
  128. /* For display hotplug interrupt */
  129. void
  130. ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
  131. {
  132. assert_spin_locked(&dev_priv->irq_lock);
  133. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  134. return;
  135. if ((dev_priv->irq_mask & mask) != 0) {
  136. dev_priv->irq_mask &= ~mask;
  137. I915_WRITE(DEIMR, dev_priv->irq_mask);
  138. POSTING_READ(DEIMR);
  139. }
  140. }
  141. void
  142. ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
  143. {
  144. assert_spin_locked(&dev_priv->irq_lock);
  145. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  146. return;
  147. if ((dev_priv->irq_mask & mask) != mask) {
  148. dev_priv->irq_mask |= mask;
  149. I915_WRITE(DEIMR, dev_priv->irq_mask);
  150. POSTING_READ(DEIMR);
  151. }
  152. }
  153. /**
  154. * ilk_update_gt_irq - update GTIMR
  155. * @dev_priv: driver private
  156. * @interrupt_mask: mask of interrupt bits to update
  157. * @enabled_irq_mask: mask of interrupt bits to enable
  158. */
  159. static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
  160. uint32_t interrupt_mask,
  161. uint32_t enabled_irq_mask)
  162. {
  163. assert_spin_locked(&dev_priv->irq_lock);
  164. WARN_ON(enabled_irq_mask & ~interrupt_mask);
  165. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  166. return;
  167. dev_priv->gt_irq_mask &= ~interrupt_mask;
  168. dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
  169. I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
  170. POSTING_READ(GTIMR);
  171. }
  172. void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
  173. {
  174. ilk_update_gt_irq(dev_priv, mask, mask);
  175. }
  176. void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
  177. {
  178. ilk_update_gt_irq(dev_priv, mask, 0);
  179. }
  180. static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
  181. {
  182. return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
  183. }
  184. static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
  185. {
  186. return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
  187. }
  188. static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
  189. {
  190. return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
  191. }
  192. /**
  193. * snb_update_pm_irq - update GEN6_PMIMR
  194. * @dev_priv: driver private
  195. * @interrupt_mask: mask of interrupt bits to update
  196. * @enabled_irq_mask: mask of interrupt bits to enable
  197. */
  198. static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
  199. uint32_t interrupt_mask,
  200. uint32_t enabled_irq_mask)
  201. {
  202. uint32_t new_val;
  203. WARN_ON(enabled_irq_mask & ~interrupt_mask);
  204. assert_spin_locked(&dev_priv->irq_lock);
  205. new_val = dev_priv->pm_irq_mask;
  206. new_val &= ~interrupt_mask;
  207. new_val |= (~enabled_irq_mask & interrupt_mask);
  208. if (new_val != dev_priv->pm_irq_mask) {
  209. dev_priv->pm_irq_mask = new_val;
  210. I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
  211. POSTING_READ(gen6_pm_imr(dev_priv));
  212. }
  213. }
  214. void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
  215. {
  216. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  217. return;
  218. snb_update_pm_irq(dev_priv, mask, mask);
  219. }
  220. static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
  221. uint32_t mask)
  222. {
  223. snb_update_pm_irq(dev_priv, mask, 0);
  224. }
  225. void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
  226. {
  227. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  228. return;
  229. __gen6_disable_pm_irq(dev_priv, mask);
  230. }
  231. void gen6_reset_rps_interrupts(struct drm_device *dev)
  232. {
  233. struct drm_i915_private *dev_priv = dev->dev_private;
  234. uint32_t reg = gen6_pm_iir(dev_priv);
  235. spin_lock_irq(&dev_priv->irq_lock);
  236. I915_WRITE(reg, dev_priv->pm_rps_events);
  237. I915_WRITE(reg, dev_priv->pm_rps_events);
  238. POSTING_READ(reg);
  239. spin_unlock_irq(&dev_priv->irq_lock);
  240. }
  241. void gen6_enable_rps_interrupts(struct drm_device *dev)
  242. {
  243. struct drm_i915_private *dev_priv = dev->dev_private;
  244. spin_lock_irq(&dev_priv->irq_lock);
  245. WARN_ON(dev_priv->rps.pm_iir);
  246. WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
  247. dev_priv->rps.interrupts_enabled = true;
  248. I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
  249. dev_priv->pm_rps_events);
  250. gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
  251. spin_unlock_irq(&dev_priv->irq_lock);
  252. }
  253. u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
  254. {
  255. /*
  256. * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
  257. * if GEN6_PM_UP_EI_EXPIRED is masked.
  258. *
  259. * TODO: verify if this can be reproduced on VLV,CHV.
  260. */
  261. if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
  262. mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
  263. if (INTEL_INFO(dev_priv)->gen >= 8)
  264. mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
  265. return mask;
  266. }
  267. void gen6_disable_rps_interrupts(struct drm_device *dev)
  268. {
  269. struct drm_i915_private *dev_priv = dev->dev_private;
  270. spin_lock_irq(&dev_priv->irq_lock);
  271. dev_priv->rps.interrupts_enabled = false;
  272. spin_unlock_irq(&dev_priv->irq_lock);
  273. cancel_work_sync(&dev_priv->rps.work);
  274. spin_lock_irq(&dev_priv->irq_lock);
  275. I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
  276. __gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
  277. I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
  278. ~dev_priv->pm_rps_events);
  279. I915_WRITE(gen6_pm_iir(dev_priv), dev_priv->pm_rps_events);
  280. I915_WRITE(gen6_pm_iir(dev_priv), dev_priv->pm_rps_events);
  281. dev_priv->rps.pm_iir = 0;
  282. spin_unlock_irq(&dev_priv->irq_lock);
  283. }
  284. /**
  285. * ibx_display_interrupt_update - update SDEIMR
  286. * @dev_priv: driver private
  287. * @interrupt_mask: mask of interrupt bits to update
  288. * @enabled_irq_mask: mask of interrupt bits to enable
  289. */
  290. void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
  291. uint32_t interrupt_mask,
  292. uint32_t enabled_irq_mask)
  293. {
  294. uint32_t sdeimr = I915_READ(SDEIMR);
  295. sdeimr &= ~interrupt_mask;
  296. sdeimr |= (~enabled_irq_mask & interrupt_mask);
  297. WARN_ON(enabled_irq_mask & ~interrupt_mask);
  298. assert_spin_locked(&dev_priv->irq_lock);
  299. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  300. return;
  301. I915_WRITE(SDEIMR, sdeimr);
  302. POSTING_READ(SDEIMR);
  303. }
  304. static void
  305. __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
  306. u32 enable_mask, u32 status_mask)
  307. {
  308. u32 reg = PIPESTAT(pipe);
  309. u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
  310. assert_spin_locked(&dev_priv->irq_lock);
  311. WARN_ON(!intel_irqs_enabled(dev_priv));
  312. if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
  313. status_mask & ~PIPESTAT_INT_STATUS_MASK,
  314. "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
  315. pipe_name(pipe), enable_mask, status_mask))
  316. return;
  317. if ((pipestat & enable_mask) == enable_mask)
  318. return;
  319. dev_priv->pipestat_irq_mask[pipe] |= status_mask;
  320. /* Enable the interrupt, clear any pending status */
  321. pipestat |= enable_mask | status_mask;
  322. I915_WRITE(reg, pipestat);
  323. POSTING_READ(reg);
  324. }
  325. static void
  326. __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
  327. u32 enable_mask, u32 status_mask)
  328. {
  329. u32 reg = PIPESTAT(pipe);
  330. u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
  331. assert_spin_locked(&dev_priv->irq_lock);
  332. WARN_ON(!intel_irqs_enabled(dev_priv));
  333. if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
  334. status_mask & ~PIPESTAT_INT_STATUS_MASK,
  335. "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
  336. pipe_name(pipe), enable_mask, status_mask))
  337. return;
  338. if ((pipestat & enable_mask) == 0)
  339. return;
  340. dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
  341. pipestat &= ~enable_mask;
  342. I915_WRITE(reg, pipestat);
  343. POSTING_READ(reg);
  344. }
  345. static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
  346. {
  347. u32 enable_mask = status_mask << 16;
  348. /*
  349. * On pipe A we don't support the PSR interrupt yet,
  350. * on pipe B and C the same bit MBZ.
  351. */
  352. if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
  353. return 0;
  354. /*
  355. * On pipe B and C we don't support the PSR interrupt yet, on pipe
  356. * A the same bit is for perf counters which we don't use either.
  357. */
  358. if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
  359. return 0;
  360. enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
  361. SPRITE0_FLIP_DONE_INT_EN_VLV |
  362. SPRITE1_FLIP_DONE_INT_EN_VLV);
  363. if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
  364. enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
  365. if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
  366. enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
  367. return enable_mask;
  368. }
  369. void
  370. i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
  371. u32 status_mask)
  372. {
  373. u32 enable_mask;
  374. if (IS_VALLEYVIEW(dev_priv->dev))
  375. enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
  376. status_mask);
  377. else
  378. enable_mask = status_mask << 16;
  379. __i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
  380. }
  381. void
  382. i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
  383. u32 status_mask)
  384. {
  385. u32 enable_mask;
  386. if (IS_VALLEYVIEW(dev_priv->dev))
  387. enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
  388. status_mask);
  389. else
  390. enable_mask = status_mask << 16;
  391. __i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
  392. }
  393. /**
  394. * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
  395. */
  396. static void i915_enable_asle_pipestat(struct drm_device *dev)
  397. {
  398. struct drm_i915_private *dev_priv = dev->dev_private;
  399. if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
  400. return;
  401. spin_lock_irq(&dev_priv->irq_lock);
  402. i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
  403. if (INTEL_INFO(dev)->gen >= 4)
  404. i915_enable_pipestat(dev_priv, PIPE_A,
  405. PIPE_LEGACY_BLC_EVENT_STATUS);
  406. spin_unlock_irq(&dev_priv->irq_lock);
  407. }
  408. /**
  409. * i915_pipe_enabled - check if a pipe is enabled
  410. * @dev: DRM device
  411. * @pipe: pipe to check
  412. *
  413. * Reading certain registers when the pipe is disabled can hang the chip.
  414. * Use this routine to make sure the PLL is running and the pipe is active
  415. * before reading such registers if unsure.
  416. */
  417. static int
  418. i915_pipe_enabled(struct drm_device *dev, int pipe)
  419. {
  420. struct drm_i915_private *dev_priv = dev->dev_private;
  421. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  422. /* Locking is horribly broken here, but whatever. */
  423. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  424. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  425. return intel_crtc->active;
  426. } else {
  427. return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
  428. }
  429. }
  430. /*
  431. * This timing diagram depicts the video signal in and
  432. * around the vertical blanking period.
  433. *
  434. * Assumptions about the fictitious mode used in this example:
  435. * vblank_start >= 3
  436. * vsync_start = vblank_start + 1
  437. * vsync_end = vblank_start + 2
  438. * vtotal = vblank_start + 3
  439. *
  440. * start of vblank:
  441. * latch double buffered registers
  442. * increment frame counter (ctg+)
  443. * generate start of vblank interrupt (gen4+)
  444. * |
  445. * | frame start:
  446. * | generate frame start interrupt (aka. vblank interrupt) (gmch)
  447. * | may be shifted forward 1-3 extra lines via PIPECONF
  448. * | |
  449. * | | start of vsync:
  450. * | | generate vsync interrupt
  451. * | | |
  452. * ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx
  453. * . \hs/ . \hs/ \hs/ \hs/ . \hs/
  454. * ----va---> <-----------------vb--------------------> <--------va-------------
  455. * | | <----vs-----> |
  456. * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
  457. * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
  458. * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
  459. * | | |
  460. * last visible pixel first visible pixel
  461. * | increment frame counter (gen3/4)
  462. * pixel counter = vblank_start * htotal pixel counter = 0 (gen3/4)
  463. *
  464. * x = horizontal active
  465. * _ = horizontal blanking
  466. * hs = horizontal sync
  467. * va = vertical active
  468. * vb = vertical blanking
  469. * vs = vertical sync
  470. * vbs = vblank_start (number)
  471. *
  472. * Summary:
  473. * - most events happen at the start of horizontal sync
  474. * - frame start happens at the start of horizontal blank, 1-4 lines
  475. * (depending on PIPECONF settings) after the start of vblank
  476. * - gen3/4 pixel and frame counter are synchronized with the start
  477. * of horizontal active on the first line of vertical active
  478. */
  479. static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
  480. {
  481. /* Gen2 doesn't have a hardware frame counter */
  482. return 0;
  483. }
  484. /* Called from drm generic code, passed a 'crtc', which
  485. * we use as a pipe index
  486. */
  487. static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
  488. {
  489. struct drm_i915_private *dev_priv = dev->dev_private;
  490. unsigned long high_frame;
  491. unsigned long low_frame;
  492. u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
  493. if (!i915_pipe_enabled(dev, pipe)) {
  494. DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
  495. "pipe %c\n", pipe_name(pipe));
  496. return 0;
  497. }
  498. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  499. struct intel_crtc *intel_crtc =
  500. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  501. const struct drm_display_mode *mode =
  502. &intel_crtc->config->base.adjusted_mode;
  503. htotal = mode->crtc_htotal;
  504. hsync_start = mode->crtc_hsync_start;
  505. vbl_start = mode->crtc_vblank_start;
  506. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  507. vbl_start = DIV_ROUND_UP(vbl_start, 2);
  508. } else {
  509. enum transcoder cpu_transcoder = (enum transcoder) pipe;
  510. htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
  511. hsync_start = (I915_READ(HSYNC(cpu_transcoder)) & 0x1fff) + 1;
  512. vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
  513. if ((I915_READ(PIPECONF(cpu_transcoder)) &
  514. PIPECONF_INTERLACE_MASK) != PIPECONF_PROGRESSIVE)
  515. vbl_start = DIV_ROUND_UP(vbl_start, 2);
  516. }
  517. /* Convert to pixel count */
  518. vbl_start *= htotal;
  519. /* Start of vblank event occurs at start of hsync */
  520. vbl_start -= htotal - hsync_start;
  521. high_frame = PIPEFRAME(pipe);
  522. low_frame = PIPEFRAMEPIXEL(pipe);
  523. /*
  524. * High & low register fields aren't synchronized, so make sure
  525. * we get a low value that's stable across two reads of the high
  526. * register.
  527. */
  528. do {
  529. high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
  530. low = I915_READ(low_frame);
  531. high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
  532. } while (high1 != high2);
  533. high1 >>= PIPE_FRAME_HIGH_SHIFT;
  534. pixel = low & PIPE_PIXEL_MASK;
  535. low >>= PIPE_FRAME_LOW_SHIFT;
  536. /*
  537. * The frame counter increments at beginning of active.
  538. * Cook up a vblank counter by also checking the pixel
  539. * counter against vblank start.
  540. */
  541. return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
  542. }
  543. static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
  544. {
  545. struct drm_i915_private *dev_priv = dev->dev_private;
  546. int reg = PIPE_FRMCOUNT_GM45(pipe);
  547. if (!i915_pipe_enabled(dev, pipe)) {
  548. DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
  549. "pipe %c\n", pipe_name(pipe));
  550. return 0;
  551. }
  552. return I915_READ(reg);
  553. }
  554. /* raw reads, only for fast reads of display block, no need for forcewake etc. */
  555. #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
  556. static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
  557. {
  558. struct drm_device *dev = crtc->base.dev;
  559. struct drm_i915_private *dev_priv = dev->dev_private;
  560. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  561. enum pipe pipe = crtc->pipe;
  562. int position, vtotal;
  563. vtotal = mode->crtc_vtotal;
  564. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  565. vtotal /= 2;
  566. if (IS_GEN2(dev))
  567. position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
  568. else
  569. position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
  570. /*
  571. * See update_scanline_offset() for the details on the
  572. * scanline_offset adjustment.
  573. */
  574. return (position + crtc->scanline_offset) % vtotal;
  575. }
  576. static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
  577. unsigned int flags, int *vpos, int *hpos,
  578. ktime_t *stime, ktime_t *etime)
  579. {
  580. struct drm_i915_private *dev_priv = dev->dev_private;
  581. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  582. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  583. const struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
  584. int position;
  585. int vbl_start, vbl_end, hsync_start, htotal, vtotal;
  586. bool in_vbl = true;
  587. int ret = 0;
  588. unsigned long irqflags;
  589. if (!intel_crtc->active) {
  590. DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
  591. "pipe %c\n", pipe_name(pipe));
  592. return 0;
  593. }
  594. htotal = mode->crtc_htotal;
  595. hsync_start = mode->crtc_hsync_start;
  596. vtotal = mode->crtc_vtotal;
  597. vbl_start = mode->crtc_vblank_start;
  598. vbl_end = mode->crtc_vblank_end;
  599. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  600. vbl_start = DIV_ROUND_UP(vbl_start, 2);
  601. vbl_end /= 2;
  602. vtotal /= 2;
  603. }
  604. ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
  605. /*
  606. * Lock uncore.lock, as we will do multiple timing critical raw
  607. * register reads, potentially with preemption disabled, so the
  608. * following code must not block on uncore.lock.
  609. */
  610. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  611. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  612. /* Get optional system timestamp before query. */
  613. if (stime)
  614. *stime = ktime_get();
  615. if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  616. /* No obvious pixelcount register. Only query vertical
  617. * scanout position from Display scan line register.
  618. */
  619. position = __intel_get_crtc_scanline(intel_crtc);
  620. } else {
  621. /* Have access to pixelcount since start of frame.
  622. * We can split this into vertical and horizontal
  623. * scanout position.
  624. */
  625. position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
  626. /* convert to pixel counts */
  627. vbl_start *= htotal;
  628. vbl_end *= htotal;
  629. vtotal *= htotal;
  630. /*
  631. * In interlaced modes, the pixel counter counts all pixels,
  632. * so one field will have htotal more pixels. In order to avoid
  633. * the reported position from jumping backwards when the pixel
  634. * counter is beyond the length of the shorter field, just
  635. * clamp the position the length of the shorter field. This
  636. * matches how the scanline counter based position works since
  637. * the scanline counter doesn't count the two half lines.
  638. */
  639. if (position >= vtotal)
  640. position = vtotal - 1;
  641. /*
  642. * Start of vblank interrupt is triggered at start of hsync,
  643. * just prior to the first active line of vblank. However we
  644. * consider lines to start at the leading edge of horizontal
  645. * active. So, should we get here before we've crossed into
  646. * the horizontal active of the first line in vblank, we would
  647. * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
  648. * always add htotal-hsync_start to the current pixel position.
  649. */
  650. position = (position + htotal - hsync_start) % vtotal;
  651. }
  652. /* Get optional system timestamp after query. */
  653. if (etime)
  654. *etime = ktime_get();
  655. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  656. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  657. in_vbl = position >= vbl_start && position < vbl_end;
  658. /*
  659. * While in vblank, position will be negative
  660. * counting up towards 0 at vbl_end. And outside
  661. * vblank, position will be positive counting
  662. * up since vbl_end.
  663. */
  664. if (position >= vbl_start)
  665. position -= vbl_end;
  666. else
  667. position += vtotal - vbl_end;
  668. if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  669. *vpos = position;
  670. *hpos = 0;
  671. } else {
  672. *vpos = position / htotal;
  673. *hpos = position - (*vpos * htotal);
  674. }
  675. /* In vblank? */
  676. if (in_vbl)
  677. ret |= DRM_SCANOUTPOS_IN_VBLANK;
  678. return ret;
  679. }
  680. int intel_get_crtc_scanline(struct intel_crtc *crtc)
  681. {
  682. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  683. unsigned long irqflags;
  684. int position;
  685. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  686. position = __intel_get_crtc_scanline(crtc);
  687. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  688. return position;
  689. }
  690. static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
  691. int *max_error,
  692. struct timeval *vblank_time,
  693. unsigned flags)
  694. {
  695. struct drm_crtc *crtc;
  696. if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
  697. DRM_ERROR("Invalid crtc %d\n", pipe);
  698. return -EINVAL;
  699. }
  700. /* Get drm_crtc to timestamp: */
  701. crtc = intel_get_crtc_for_pipe(dev, pipe);
  702. if (crtc == NULL) {
  703. DRM_ERROR("Invalid crtc %d\n", pipe);
  704. return -EINVAL;
  705. }
  706. if (!crtc->enabled) {
  707. DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
  708. return -EBUSY;
  709. }
  710. /* Helper routine in DRM core does all the work: */
  711. return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
  712. vblank_time, flags,
  713. crtc,
  714. &to_intel_crtc(crtc)->config->base.adjusted_mode);
  715. }
  716. static bool intel_hpd_irq_event(struct drm_device *dev,
  717. struct drm_connector *connector)
  718. {
  719. enum drm_connector_status old_status;
  720. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  721. old_status = connector->status;
  722. connector->status = connector->funcs->detect(connector, false);
  723. if (old_status == connector->status)
  724. return false;
  725. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  726. connector->base.id,
  727. connector->name,
  728. drm_get_connector_status_name(old_status),
  729. drm_get_connector_status_name(connector->status));
  730. return true;
  731. }
  732. static void i915_digport_work_func(struct work_struct *work)
  733. {
  734. struct drm_i915_private *dev_priv =
  735. container_of(work, struct drm_i915_private, dig_port_work);
  736. u32 long_port_mask, short_port_mask;
  737. struct intel_digital_port *intel_dig_port;
  738. int i;
  739. u32 old_bits = 0;
  740. spin_lock_irq(&dev_priv->irq_lock);
  741. long_port_mask = dev_priv->long_hpd_port_mask;
  742. dev_priv->long_hpd_port_mask = 0;
  743. short_port_mask = dev_priv->short_hpd_port_mask;
  744. dev_priv->short_hpd_port_mask = 0;
  745. spin_unlock_irq(&dev_priv->irq_lock);
  746. for (i = 0; i < I915_MAX_PORTS; i++) {
  747. bool valid = false;
  748. bool long_hpd = false;
  749. intel_dig_port = dev_priv->hpd_irq_port[i];
  750. if (!intel_dig_port || !intel_dig_port->hpd_pulse)
  751. continue;
  752. if (long_port_mask & (1 << i)) {
  753. valid = true;
  754. long_hpd = true;
  755. } else if (short_port_mask & (1 << i))
  756. valid = true;
  757. if (valid) {
  758. enum irqreturn ret;
  759. ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
  760. if (ret == IRQ_NONE) {
  761. /* fall back to old school hpd */
  762. old_bits |= (1 << intel_dig_port->base.hpd_pin);
  763. }
  764. }
  765. }
  766. if (old_bits) {
  767. spin_lock_irq(&dev_priv->irq_lock);
  768. dev_priv->hpd_event_bits |= old_bits;
  769. spin_unlock_irq(&dev_priv->irq_lock);
  770. schedule_work(&dev_priv->hotplug_work);
  771. }
  772. }
  773. /*
  774. * Handle hotplug events outside the interrupt handler proper.
  775. */
  776. #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
  777. static void i915_hotplug_work_func(struct work_struct *work)
  778. {
  779. struct drm_i915_private *dev_priv =
  780. container_of(work, struct drm_i915_private, hotplug_work);
  781. struct drm_device *dev = dev_priv->dev;
  782. struct drm_mode_config *mode_config = &dev->mode_config;
  783. struct intel_connector *intel_connector;
  784. struct intel_encoder *intel_encoder;
  785. struct drm_connector *connector;
  786. bool hpd_disabled = false;
  787. bool changed = false;
  788. u32 hpd_event_bits;
  789. mutex_lock(&mode_config->mutex);
  790. DRM_DEBUG_KMS("running encoder hotplug functions\n");
  791. spin_lock_irq(&dev_priv->irq_lock);
  792. hpd_event_bits = dev_priv->hpd_event_bits;
  793. dev_priv->hpd_event_bits = 0;
  794. list_for_each_entry(connector, &mode_config->connector_list, head) {
  795. intel_connector = to_intel_connector(connector);
  796. if (!intel_connector->encoder)
  797. continue;
  798. intel_encoder = intel_connector->encoder;
  799. if (intel_encoder->hpd_pin > HPD_NONE &&
  800. dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
  801. connector->polled == DRM_CONNECTOR_POLL_HPD) {
  802. DRM_INFO("HPD interrupt storm detected on connector %s: "
  803. "switching from hotplug detection to polling\n",
  804. connector->name);
  805. dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
  806. connector->polled = DRM_CONNECTOR_POLL_CONNECT
  807. | DRM_CONNECTOR_POLL_DISCONNECT;
  808. hpd_disabled = true;
  809. }
  810. if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
  811. DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
  812. connector->name, intel_encoder->hpd_pin);
  813. }
  814. }
  815. /* if there were no outputs to poll, poll was disabled,
  816. * therefore make sure it's enabled when disabling HPD on
  817. * some connectors */
  818. if (hpd_disabled) {
  819. drm_kms_helper_poll_enable(dev);
  820. mod_delayed_work(system_wq, &dev_priv->hotplug_reenable_work,
  821. msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
  822. }
  823. spin_unlock_irq(&dev_priv->irq_lock);
  824. list_for_each_entry(connector, &mode_config->connector_list, head) {
  825. intel_connector = to_intel_connector(connector);
  826. if (!intel_connector->encoder)
  827. continue;
  828. intel_encoder = intel_connector->encoder;
  829. if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
  830. if (intel_encoder->hot_plug)
  831. intel_encoder->hot_plug(intel_encoder);
  832. if (intel_hpd_irq_event(dev, connector))
  833. changed = true;
  834. }
  835. }
  836. mutex_unlock(&mode_config->mutex);
  837. if (changed)
  838. drm_kms_helper_hotplug_event(dev);
  839. }
  840. static void ironlake_rps_change_irq_handler(struct drm_device *dev)
  841. {
  842. struct drm_i915_private *dev_priv = dev->dev_private;
  843. u32 busy_up, busy_down, max_avg, min_avg;
  844. u8 new_delay;
  845. spin_lock(&mchdev_lock);
  846. I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
  847. new_delay = dev_priv->ips.cur_delay;
  848. I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
  849. busy_up = I915_READ(RCPREVBSYTUPAVG);
  850. busy_down = I915_READ(RCPREVBSYTDNAVG);
  851. max_avg = I915_READ(RCBMAXAVG);
  852. min_avg = I915_READ(RCBMINAVG);
  853. /* Handle RCS change request from hw */
  854. if (busy_up > max_avg) {
  855. if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
  856. new_delay = dev_priv->ips.cur_delay - 1;
  857. if (new_delay < dev_priv->ips.max_delay)
  858. new_delay = dev_priv->ips.max_delay;
  859. } else if (busy_down < min_avg) {
  860. if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
  861. new_delay = dev_priv->ips.cur_delay + 1;
  862. if (new_delay > dev_priv->ips.min_delay)
  863. new_delay = dev_priv->ips.min_delay;
  864. }
  865. if (ironlake_set_drps(dev, new_delay))
  866. dev_priv->ips.cur_delay = new_delay;
  867. spin_unlock(&mchdev_lock);
  868. return;
  869. }
  870. static void notify_ring(struct drm_device *dev,
  871. struct intel_engine_cs *ring)
  872. {
  873. if (!intel_ring_initialized(ring))
  874. return;
  875. trace_i915_gem_request_notify(ring);
  876. wake_up_all(&ring->irq_queue);
  877. }
  878. static u32 vlv_c0_residency(struct drm_i915_private *dev_priv,
  879. struct intel_rps_ei *rps_ei)
  880. {
  881. u32 cz_ts, cz_freq_khz;
  882. u32 render_count, media_count;
  883. u32 elapsed_render, elapsed_media, elapsed_time;
  884. u32 residency = 0;
  885. cz_ts = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
  886. cz_freq_khz = DIV_ROUND_CLOSEST(dev_priv->mem_freq * 1000, 4);
  887. render_count = I915_READ(VLV_RENDER_C0_COUNT_REG);
  888. media_count = I915_READ(VLV_MEDIA_C0_COUNT_REG);
  889. if (rps_ei->cz_clock == 0) {
  890. rps_ei->cz_clock = cz_ts;
  891. rps_ei->render_c0 = render_count;
  892. rps_ei->media_c0 = media_count;
  893. return dev_priv->rps.cur_freq;
  894. }
  895. elapsed_time = cz_ts - rps_ei->cz_clock;
  896. rps_ei->cz_clock = cz_ts;
  897. elapsed_render = render_count - rps_ei->render_c0;
  898. rps_ei->render_c0 = render_count;
  899. elapsed_media = media_count - rps_ei->media_c0;
  900. rps_ei->media_c0 = media_count;
  901. /* Convert all the counters into common unit of milli sec */
  902. elapsed_time /= VLV_CZ_CLOCK_TO_MILLI_SEC;
  903. elapsed_render /= cz_freq_khz;
  904. elapsed_media /= cz_freq_khz;
  905. /*
  906. * Calculate overall C0 residency percentage
  907. * only if elapsed time is non zero
  908. */
  909. if (elapsed_time) {
  910. residency =
  911. ((max(elapsed_render, elapsed_media) * 100)
  912. / elapsed_time);
  913. }
  914. return residency;
  915. }
  916. /**
  917. * vlv_calc_delay_from_C0_counters - Increase/Decrease freq based on GPU
  918. * busy-ness calculated from C0 counters of render & media power wells
  919. * @dev_priv: DRM device private
  920. *
  921. */
  922. static int vlv_calc_delay_from_C0_counters(struct drm_i915_private *dev_priv)
  923. {
  924. u32 residency_C0_up = 0, residency_C0_down = 0;
  925. int new_delay, adj;
  926. dev_priv->rps.ei_interrupt_count++;
  927. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  928. if (dev_priv->rps.up_ei.cz_clock == 0) {
  929. vlv_c0_residency(dev_priv, &dev_priv->rps.up_ei);
  930. vlv_c0_residency(dev_priv, &dev_priv->rps.down_ei);
  931. return dev_priv->rps.cur_freq;
  932. }
  933. /*
  934. * To down throttle, C0 residency should be less than down threshold
  935. * for continous EI intervals. So calculate down EI counters
  936. * once in VLV_INT_COUNT_FOR_DOWN_EI
  937. */
  938. if (dev_priv->rps.ei_interrupt_count == VLV_INT_COUNT_FOR_DOWN_EI) {
  939. dev_priv->rps.ei_interrupt_count = 0;
  940. residency_C0_down = vlv_c0_residency(dev_priv,
  941. &dev_priv->rps.down_ei);
  942. } else {
  943. residency_C0_up = vlv_c0_residency(dev_priv,
  944. &dev_priv->rps.up_ei);
  945. }
  946. new_delay = dev_priv->rps.cur_freq;
  947. adj = dev_priv->rps.last_adj;
  948. /* C0 residency is greater than UP threshold. Increase Frequency */
  949. if (residency_C0_up >= VLV_RP_UP_EI_THRESHOLD) {
  950. if (adj > 0)
  951. adj *= 2;
  952. else
  953. adj = 1;
  954. if (dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit)
  955. new_delay = dev_priv->rps.cur_freq + adj;
  956. /*
  957. * For better performance, jump directly
  958. * to RPe if we're below it.
  959. */
  960. if (new_delay < dev_priv->rps.efficient_freq)
  961. new_delay = dev_priv->rps.efficient_freq;
  962. } else if (!dev_priv->rps.ei_interrupt_count &&
  963. (residency_C0_down < VLV_RP_DOWN_EI_THRESHOLD)) {
  964. if (adj < 0)
  965. adj *= 2;
  966. else
  967. adj = -1;
  968. /*
  969. * This means, C0 residency is less than down threshold over
  970. * a period of VLV_INT_COUNT_FOR_DOWN_EI. So, reduce the freq
  971. */
  972. if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
  973. new_delay = dev_priv->rps.cur_freq + adj;
  974. }
  975. return new_delay;
  976. }
  977. static void gen6_pm_rps_work(struct work_struct *work)
  978. {
  979. struct drm_i915_private *dev_priv =
  980. container_of(work, struct drm_i915_private, rps.work);
  981. u32 pm_iir;
  982. int new_delay, adj;
  983. spin_lock_irq(&dev_priv->irq_lock);
  984. /* Speed up work cancelation during disabling rps interrupts. */
  985. if (!dev_priv->rps.interrupts_enabled) {
  986. spin_unlock_irq(&dev_priv->irq_lock);
  987. return;
  988. }
  989. pm_iir = dev_priv->rps.pm_iir;
  990. dev_priv->rps.pm_iir = 0;
  991. /* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
  992. gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
  993. spin_unlock_irq(&dev_priv->irq_lock);
  994. /* Make sure we didn't queue anything we're not going to process. */
  995. WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
  996. if ((pm_iir & dev_priv->pm_rps_events) == 0)
  997. return;
  998. mutex_lock(&dev_priv->rps.hw_lock);
  999. adj = dev_priv->rps.last_adj;
  1000. if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
  1001. if (adj > 0)
  1002. adj *= 2;
  1003. else {
  1004. /* CHV needs even encode values */
  1005. adj = IS_CHERRYVIEW(dev_priv->dev) ? 2 : 1;
  1006. }
  1007. new_delay = dev_priv->rps.cur_freq + adj;
  1008. /*
  1009. * For better performance, jump directly
  1010. * to RPe if we're below it.
  1011. */
  1012. if (new_delay < dev_priv->rps.efficient_freq)
  1013. new_delay = dev_priv->rps.efficient_freq;
  1014. } else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
  1015. if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
  1016. new_delay = dev_priv->rps.efficient_freq;
  1017. else
  1018. new_delay = dev_priv->rps.min_freq_softlimit;
  1019. adj = 0;
  1020. } else if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
  1021. new_delay = vlv_calc_delay_from_C0_counters(dev_priv);
  1022. } else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
  1023. if (adj < 0)
  1024. adj *= 2;
  1025. else {
  1026. /* CHV needs even encode values */
  1027. adj = IS_CHERRYVIEW(dev_priv->dev) ? -2 : -1;
  1028. }
  1029. new_delay = dev_priv->rps.cur_freq + adj;
  1030. } else { /* unknown event */
  1031. new_delay = dev_priv->rps.cur_freq;
  1032. }
  1033. /* sysfs frequency interfaces may have snuck in while servicing the
  1034. * interrupt
  1035. */
  1036. new_delay = clamp_t(int, new_delay,
  1037. dev_priv->rps.min_freq_softlimit,
  1038. dev_priv->rps.max_freq_softlimit);
  1039. dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
  1040. if (IS_VALLEYVIEW(dev_priv->dev))
  1041. valleyview_set_rps(dev_priv->dev, new_delay);
  1042. else
  1043. gen6_set_rps(dev_priv->dev, new_delay);
  1044. mutex_unlock(&dev_priv->rps.hw_lock);
  1045. }
  1046. /**
  1047. * ivybridge_parity_work - Workqueue called when a parity error interrupt
  1048. * occurred.
  1049. * @work: workqueue struct
  1050. *
  1051. * Doesn't actually do anything except notify userspace. As a consequence of
  1052. * this event, userspace should try to remap the bad rows since statistically
  1053. * it is likely the same row is more likely to go bad again.
  1054. */
  1055. static void ivybridge_parity_work(struct work_struct *work)
  1056. {
  1057. struct drm_i915_private *dev_priv =
  1058. container_of(work, struct drm_i915_private, l3_parity.error_work);
  1059. u32 error_status, row, bank, subbank;
  1060. char *parity_event[6];
  1061. uint32_t misccpctl;
  1062. uint8_t slice = 0;
  1063. /* We must turn off DOP level clock gating to access the L3 registers.
  1064. * In order to prevent a get/put style interface, acquire struct mutex
  1065. * any time we access those registers.
  1066. */
  1067. mutex_lock(&dev_priv->dev->struct_mutex);
  1068. /* If we've screwed up tracking, just let the interrupt fire again */
  1069. if (WARN_ON(!dev_priv->l3_parity.which_slice))
  1070. goto out;
  1071. misccpctl = I915_READ(GEN7_MISCCPCTL);
  1072. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  1073. POSTING_READ(GEN7_MISCCPCTL);
  1074. while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
  1075. u32 reg;
  1076. slice--;
  1077. if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
  1078. break;
  1079. dev_priv->l3_parity.which_slice &= ~(1<<slice);
  1080. reg = GEN7_L3CDERRST1 + (slice * 0x200);
  1081. error_status = I915_READ(reg);
  1082. row = GEN7_PARITY_ERROR_ROW(error_status);
  1083. bank = GEN7_PARITY_ERROR_BANK(error_status);
  1084. subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
  1085. I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
  1086. POSTING_READ(reg);
  1087. parity_event[0] = I915_L3_PARITY_UEVENT "=1";
  1088. parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
  1089. parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
  1090. parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
  1091. parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
  1092. parity_event[5] = NULL;
  1093. kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
  1094. KOBJ_CHANGE, parity_event);
  1095. DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
  1096. slice, row, bank, subbank);
  1097. kfree(parity_event[4]);
  1098. kfree(parity_event[3]);
  1099. kfree(parity_event[2]);
  1100. kfree(parity_event[1]);
  1101. }
  1102. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  1103. out:
  1104. WARN_ON(dev_priv->l3_parity.which_slice);
  1105. spin_lock_irq(&dev_priv->irq_lock);
  1106. gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
  1107. spin_unlock_irq(&dev_priv->irq_lock);
  1108. mutex_unlock(&dev_priv->dev->struct_mutex);
  1109. }
  1110. static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
  1111. {
  1112. struct drm_i915_private *dev_priv = dev->dev_private;
  1113. if (!HAS_L3_DPF(dev))
  1114. return;
  1115. spin_lock(&dev_priv->irq_lock);
  1116. gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
  1117. spin_unlock(&dev_priv->irq_lock);
  1118. iir &= GT_PARITY_ERROR(dev);
  1119. if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
  1120. dev_priv->l3_parity.which_slice |= 1 << 1;
  1121. if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
  1122. dev_priv->l3_parity.which_slice |= 1 << 0;
  1123. queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
  1124. }
  1125. static void ilk_gt_irq_handler(struct drm_device *dev,
  1126. struct drm_i915_private *dev_priv,
  1127. u32 gt_iir)
  1128. {
  1129. if (gt_iir &
  1130. (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
  1131. notify_ring(dev, &dev_priv->ring[RCS]);
  1132. if (gt_iir & ILK_BSD_USER_INTERRUPT)
  1133. notify_ring(dev, &dev_priv->ring[VCS]);
  1134. }
  1135. static void snb_gt_irq_handler(struct drm_device *dev,
  1136. struct drm_i915_private *dev_priv,
  1137. u32 gt_iir)
  1138. {
  1139. if (gt_iir &
  1140. (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
  1141. notify_ring(dev, &dev_priv->ring[RCS]);
  1142. if (gt_iir & GT_BSD_USER_INTERRUPT)
  1143. notify_ring(dev, &dev_priv->ring[VCS]);
  1144. if (gt_iir & GT_BLT_USER_INTERRUPT)
  1145. notify_ring(dev, &dev_priv->ring[BCS]);
  1146. if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
  1147. GT_BSD_CS_ERROR_INTERRUPT |
  1148. GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
  1149. DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
  1150. if (gt_iir & GT_PARITY_ERROR(dev))
  1151. ivybridge_parity_error_irq_handler(dev, gt_iir);
  1152. }
  1153. static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
  1154. struct drm_i915_private *dev_priv,
  1155. u32 master_ctl)
  1156. {
  1157. struct intel_engine_cs *ring;
  1158. u32 rcs, bcs, vcs;
  1159. uint32_t tmp = 0;
  1160. irqreturn_t ret = IRQ_NONE;
  1161. if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
  1162. tmp = I915_READ(GEN8_GT_IIR(0));
  1163. if (tmp) {
  1164. I915_WRITE(GEN8_GT_IIR(0), tmp);
  1165. ret = IRQ_HANDLED;
  1166. rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
  1167. ring = &dev_priv->ring[RCS];
  1168. if (rcs & GT_RENDER_USER_INTERRUPT)
  1169. notify_ring(dev, ring);
  1170. if (rcs & GT_CONTEXT_SWITCH_INTERRUPT)
  1171. intel_lrc_irq_handler(ring);
  1172. bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
  1173. ring = &dev_priv->ring[BCS];
  1174. if (bcs & GT_RENDER_USER_INTERRUPT)
  1175. notify_ring(dev, ring);
  1176. if (bcs & GT_CONTEXT_SWITCH_INTERRUPT)
  1177. intel_lrc_irq_handler(ring);
  1178. } else
  1179. DRM_ERROR("The master control interrupt lied (GT0)!\n");
  1180. }
  1181. if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
  1182. tmp = I915_READ(GEN8_GT_IIR(1));
  1183. if (tmp) {
  1184. I915_WRITE(GEN8_GT_IIR(1), tmp);
  1185. ret = IRQ_HANDLED;
  1186. vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
  1187. ring = &dev_priv->ring[VCS];
  1188. if (vcs & GT_RENDER_USER_INTERRUPT)
  1189. notify_ring(dev, ring);
  1190. if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
  1191. intel_lrc_irq_handler(ring);
  1192. vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
  1193. ring = &dev_priv->ring[VCS2];
  1194. if (vcs & GT_RENDER_USER_INTERRUPT)
  1195. notify_ring(dev, ring);
  1196. if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
  1197. intel_lrc_irq_handler(ring);
  1198. } else
  1199. DRM_ERROR("The master control interrupt lied (GT1)!\n");
  1200. }
  1201. if (master_ctl & GEN8_GT_PM_IRQ) {
  1202. tmp = I915_READ(GEN8_GT_IIR(2));
  1203. if (tmp & dev_priv->pm_rps_events) {
  1204. I915_WRITE(GEN8_GT_IIR(2),
  1205. tmp & dev_priv->pm_rps_events);
  1206. ret = IRQ_HANDLED;
  1207. gen6_rps_irq_handler(dev_priv, tmp);
  1208. } else
  1209. DRM_ERROR("The master control interrupt lied (PM)!\n");
  1210. }
  1211. if (master_ctl & GEN8_GT_VECS_IRQ) {
  1212. tmp = I915_READ(GEN8_GT_IIR(3));
  1213. if (tmp) {
  1214. I915_WRITE(GEN8_GT_IIR(3), tmp);
  1215. ret = IRQ_HANDLED;
  1216. vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
  1217. ring = &dev_priv->ring[VECS];
  1218. if (vcs & GT_RENDER_USER_INTERRUPT)
  1219. notify_ring(dev, ring);
  1220. if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
  1221. intel_lrc_irq_handler(ring);
  1222. } else
  1223. DRM_ERROR("The master control interrupt lied (GT3)!\n");
  1224. }
  1225. return ret;
  1226. }
  1227. #define HPD_STORM_DETECT_PERIOD 1000
  1228. #define HPD_STORM_THRESHOLD 5
  1229. static int pch_port_to_hotplug_shift(enum port port)
  1230. {
  1231. switch (port) {
  1232. case PORT_A:
  1233. case PORT_E:
  1234. default:
  1235. return -1;
  1236. case PORT_B:
  1237. return 0;
  1238. case PORT_C:
  1239. return 8;
  1240. case PORT_D:
  1241. return 16;
  1242. }
  1243. }
  1244. static int i915_port_to_hotplug_shift(enum port port)
  1245. {
  1246. switch (port) {
  1247. case PORT_A:
  1248. case PORT_E:
  1249. default:
  1250. return -1;
  1251. case PORT_B:
  1252. return 17;
  1253. case PORT_C:
  1254. return 19;
  1255. case PORT_D:
  1256. return 21;
  1257. }
  1258. }
  1259. static inline enum port get_port_from_pin(enum hpd_pin pin)
  1260. {
  1261. switch (pin) {
  1262. case HPD_PORT_B:
  1263. return PORT_B;
  1264. case HPD_PORT_C:
  1265. return PORT_C;
  1266. case HPD_PORT_D:
  1267. return PORT_D;
  1268. default:
  1269. return PORT_A; /* no hpd */
  1270. }
  1271. }
  1272. static inline void intel_hpd_irq_handler(struct drm_device *dev,
  1273. u32 hotplug_trigger,
  1274. u32 dig_hotplug_reg,
  1275. const u32 hpd[HPD_NUM_PINS])
  1276. {
  1277. struct drm_i915_private *dev_priv = dev->dev_private;
  1278. int i;
  1279. enum port port;
  1280. bool storm_detected = false;
  1281. bool queue_dig = false, queue_hp = false;
  1282. u32 dig_shift;
  1283. u32 dig_port_mask = 0;
  1284. if (!hotplug_trigger)
  1285. return;
  1286. DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
  1287. hotplug_trigger, dig_hotplug_reg);
  1288. spin_lock(&dev_priv->irq_lock);
  1289. for (i = 1; i < HPD_NUM_PINS; i++) {
  1290. if (!(hpd[i] & hotplug_trigger))
  1291. continue;
  1292. port = get_port_from_pin(i);
  1293. if (port && dev_priv->hpd_irq_port[port]) {
  1294. bool long_hpd;
  1295. if (HAS_PCH_SPLIT(dev)) {
  1296. dig_shift = pch_port_to_hotplug_shift(port);
  1297. long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
  1298. } else {
  1299. dig_shift = i915_port_to_hotplug_shift(port);
  1300. long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
  1301. }
  1302. DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
  1303. port_name(port),
  1304. long_hpd ? "long" : "short");
  1305. /* for long HPD pulses we want to have the digital queue happen,
  1306. but we still want HPD storm detection to function. */
  1307. if (long_hpd) {
  1308. dev_priv->long_hpd_port_mask |= (1 << port);
  1309. dig_port_mask |= hpd[i];
  1310. } else {
  1311. /* for short HPD just trigger the digital queue */
  1312. dev_priv->short_hpd_port_mask |= (1 << port);
  1313. hotplug_trigger &= ~hpd[i];
  1314. }
  1315. queue_dig = true;
  1316. }
  1317. }
  1318. for (i = 1; i < HPD_NUM_PINS; i++) {
  1319. if (hpd[i] & hotplug_trigger &&
  1320. dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
  1321. /*
  1322. * On GMCH platforms the interrupt mask bits only
  1323. * prevent irq generation, not the setting of the
  1324. * hotplug bits itself. So only WARN about unexpected
  1325. * interrupts on saner platforms.
  1326. */
  1327. WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
  1328. "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
  1329. hotplug_trigger, i, hpd[i]);
  1330. continue;
  1331. }
  1332. if (!(hpd[i] & hotplug_trigger) ||
  1333. dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
  1334. continue;
  1335. if (!(dig_port_mask & hpd[i])) {
  1336. dev_priv->hpd_event_bits |= (1 << i);
  1337. queue_hp = true;
  1338. }
  1339. if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
  1340. dev_priv->hpd_stats[i].hpd_last_jiffies
  1341. + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
  1342. dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
  1343. dev_priv->hpd_stats[i].hpd_cnt = 0;
  1344. DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
  1345. } else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
  1346. dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
  1347. dev_priv->hpd_event_bits &= ~(1 << i);
  1348. DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
  1349. storm_detected = true;
  1350. } else {
  1351. dev_priv->hpd_stats[i].hpd_cnt++;
  1352. DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
  1353. dev_priv->hpd_stats[i].hpd_cnt);
  1354. }
  1355. }
  1356. if (storm_detected)
  1357. dev_priv->display.hpd_irq_setup(dev);
  1358. spin_unlock(&dev_priv->irq_lock);
  1359. /*
  1360. * Our hotplug handler can grab modeset locks (by calling down into the
  1361. * fb helpers). Hence it must not be run on our own dev-priv->wq work
  1362. * queue for otherwise the flush_work in the pageflip code will
  1363. * deadlock.
  1364. */
  1365. if (queue_dig)
  1366. queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
  1367. if (queue_hp)
  1368. schedule_work(&dev_priv->hotplug_work);
  1369. }
  1370. static void gmbus_irq_handler(struct drm_device *dev)
  1371. {
  1372. struct drm_i915_private *dev_priv = dev->dev_private;
  1373. wake_up_all(&dev_priv->gmbus_wait_queue);
  1374. }
  1375. static void dp_aux_irq_handler(struct drm_device *dev)
  1376. {
  1377. struct drm_i915_private *dev_priv = dev->dev_private;
  1378. wake_up_all(&dev_priv->gmbus_wait_queue);
  1379. }
  1380. #if defined(CONFIG_DEBUG_FS)
  1381. static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
  1382. uint32_t crc0, uint32_t crc1,
  1383. uint32_t crc2, uint32_t crc3,
  1384. uint32_t crc4)
  1385. {
  1386. struct drm_i915_private *dev_priv = dev->dev_private;
  1387. struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
  1388. struct intel_pipe_crc_entry *entry;
  1389. int head, tail;
  1390. spin_lock(&pipe_crc->lock);
  1391. if (!pipe_crc->entries) {
  1392. spin_unlock(&pipe_crc->lock);
  1393. DRM_DEBUG_KMS("spurious interrupt\n");
  1394. return;
  1395. }
  1396. head = pipe_crc->head;
  1397. tail = pipe_crc->tail;
  1398. if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
  1399. spin_unlock(&pipe_crc->lock);
  1400. DRM_ERROR("CRC buffer overflowing\n");
  1401. return;
  1402. }
  1403. entry = &pipe_crc->entries[head];
  1404. entry->frame = dev->driver->get_vblank_counter(dev, pipe);
  1405. entry->crc[0] = crc0;
  1406. entry->crc[1] = crc1;
  1407. entry->crc[2] = crc2;
  1408. entry->crc[3] = crc3;
  1409. entry->crc[4] = crc4;
  1410. head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
  1411. pipe_crc->head = head;
  1412. spin_unlock(&pipe_crc->lock);
  1413. wake_up_interruptible(&pipe_crc->wq);
  1414. }
  1415. #else
  1416. static inline void
  1417. display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
  1418. uint32_t crc0, uint32_t crc1,
  1419. uint32_t crc2, uint32_t crc3,
  1420. uint32_t crc4) {}
  1421. #endif
  1422. static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
  1423. {
  1424. struct drm_i915_private *dev_priv = dev->dev_private;
  1425. display_pipe_crc_irq_handler(dev, pipe,
  1426. I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
  1427. 0, 0, 0, 0);
  1428. }
  1429. static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
  1430. {
  1431. struct drm_i915_private *dev_priv = dev->dev_private;
  1432. display_pipe_crc_irq_handler(dev, pipe,
  1433. I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
  1434. I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
  1435. I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
  1436. I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
  1437. I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
  1438. }
  1439. static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
  1440. {
  1441. struct drm_i915_private *dev_priv = dev->dev_private;
  1442. uint32_t res1, res2;
  1443. if (INTEL_INFO(dev)->gen >= 3)
  1444. res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
  1445. else
  1446. res1 = 0;
  1447. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  1448. res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
  1449. else
  1450. res2 = 0;
  1451. display_pipe_crc_irq_handler(dev, pipe,
  1452. I915_READ(PIPE_CRC_RES_RED(pipe)),
  1453. I915_READ(PIPE_CRC_RES_GREEN(pipe)),
  1454. I915_READ(PIPE_CRC_RES_BLUE(pipe)),
  1455. res1, res2);
  1456. }
  1457. /* The RPS events need forcewake, so we add them to a work queue and mask their
  1458. * IMR bits until the work is done. Other interrupts can be processed without
  1459. * the work queue. */
  1460. static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
  1461. {
  1462. /* TODO: RPS on GEN9+ is not supported yet. */
  1463. if (WARN_ONCE(INTEL_INFO(dev_priv)->gen >= 9,
  1464. "GEN9+: unexpected RPS IRQ\n"))
  1465. return;
  1466. if (pm_iir & dev_priv->pm_rps_events) {
  1467. spin_lock(&dev_priv->irq_lock);
  1468. gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
  1469. if (dev_priv->rps.interrupts_enabled) {
  1470. dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
  1471. queue_work(dev_priv->wq, &dev_priv->rps.work);
  1472. }
  1473. spin_unlock(&dev_priv->irq_lock);
  1474. }
  1475. if (INTEL_INFO(dev_priv)->gen >= 8)
  1476. return;
  1477. if (HAS_VEBOX(dev_priv->dev)) {
  1478. if (pm_iir & PM_VEBOX_USER_INTERRUPT)
  1479. notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
  1480. if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
  1481. DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
  1482. }
  1483. }
  1484. static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
  1485. {
  1486. if (!drm_handle_vblank(dev, pipe))
  1487. return false;
  1488. return true;
  1489. }
  1490. static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
  1491. {
  1492. struct drm_i915_private *dev_priv = dev->dev_private;
  1493. u32 pipe_stats[I915_MAX_PIPES] = { };
  1494. int pipe;
  1495. spin_lock(&dev_priv->irq_lock);
  1496. for_each_pipe(dev_priv, pipe) {
  1497. int reg;
  1498. u32 mask, iir_bit = 0;
  1499. /*
  1500. * PIPESTAT bits get signalled even when the interrupt is
  1501. * disabled with the mask bits, and some of the status bits do
  1502. * not generate interrupts at all (like the underrun bit). Hence
  1503. * we need to be careful that we only handle what we want to
  1504. * handle.
  1505. */
  1506. /* fifo underruns are filterered in the underrun handler. */
  1507. mask = PIPE_FIFO_UNDERRUN_STATUS;
  1508. switch (pipe) {
  1509. case PIPE_A:
  1510. iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
  1511. break;
  1512. case PIPE_B:
  1513. iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
  1514. break;
  1515. case PIPE_C:
  1516. iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
  1517. break;
  1518. }
  1519. if (iir & iir_bit)
  1520. mask |= dev_priv->pipestat_irq_mask[pipe];
  1521. if (!mask)
  1522. continue;
  1523. reg = PIPESTAT(pipe);
  1524. mask |= PIPESTAT_INT_ENABLE_MASK;
  1525. pipe_stats[pipe] = I915_READ(reg) & mask;
  1526. /*
  1527. * Clear the PIPE*STAT regs before the IIR
  1528. */
  1529. if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
  1530. PIPESTAT_INT_STATUS_MASK))
  1531. I915_WRITE(reg, pipe_stats[pipe]);
  1532. }
  1533. spin_unlock(&dev_priv->irq_lock);
  1534. for_each_pipe(dev_priv, pipe) {
  1535. if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
  1536. intel_pipe_handle_vblank(dev, pipe))
  1537. intel_check_page_flip(dev, pipe);
  1538. if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
  1539. intel_prepare_page_flip(dev, pipe);
  1540. intel_finish_page_flip(dev, pipe);
  1541. }
  1542. if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
  1543. i9xx_pipe_crc_irq_handler(dev, pipe);
  1544. if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
  1545. intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
  1546. }
  1547. if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
  1548. gmbus_irq_handler(dev);
  1549. }
  1550. static void i9xx_hpd_irq_handler(struct drm_device *dev)
  1551. {
  1552. struct drm_i915_private *dev_priv = dev->dev_private;
  1553. u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
  1554. if (hotplug_status) {
  1555. I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
  1556. /*
  1557. * Make sure hotplug status is cleared before we clear IIR, or else we
  1558. * may miss hotplug events.
  1559. */
  1560. POSTING_READ(PORT_HOTPLUG_STAT);
  1561. if (IS_G4X(dev)) {
  1562. u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
  1563. intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
  1564. } else {
  1565. u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
  1566. intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
  1567. }
  1568. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
  1569. hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
  1570. dp_aux_irq_handler(dev);
  1571. }
  1572. }
  1573. static irqreturn_t valleyview_irq_handler(int irq, void *arg)
  1574. {
  1575. struct drm_device *dev = arg;
  1576. struct drm_i915_private *dev_priv = dev->dev_private;
  1577. u32 iir, gt_iir, pm_iir;
  1578. irqreturn_t ret = IRQ_NONE;
  1579. if (!intel_irqs_enabled(dev_priv))
  1580. return IRQ_NONE;
  1581. while (true) {
  1582. /* Find, clear, then process each source of interrupt */
  1583. gt_iir = I915_READ(GTIIR);
  1584. if (gt_iir)
  1585. I915_WRITE(GTIIR, gt_iir);
  1586. pm_iir = I915_READ(GEN6_PMIIR);
  1587. if (pm_iir)
  1588. I915_WRITE(GEN6_PMIIR, pm_iir);
  1589. iir = I915_READ(VLV_IIR);
  1590. if (iir) {
  1591. /* Consume port before clearing IIR or we'll miss events */
  1592. if (iir & I915_DISPLAY_PORT_INTERRUPT)
  1593. i9xx_hpd_irq_handler(dev);
  1594. I915_WRITE(VLV_IIR, iir);
  1595. }
  1596. if (gt_iir == 0 && pm_iir == 0 && iir == 0)
  1597. goto out;
  1598. ret = IRQ_HANDLED;
  1599. if (gt_iir)
  1600. snb_gt_irq_handler(dev, dev_priv, gt_iir);
  1601. if (pm_iir)
  1602. gen6_rps_irq_handler(dev_priv, pm_iir);
  1603. /* Call regardless, as some status bits might not be
  1604. * signalled in iir */
  1605. valleyview_pipestat_irq_handler(dev, iir);
  1606. }
  1607. out:
  1608. return ret;
  1609. }
  1610. static irqreturn_t cherryview_irq_handler(int irq, void *arg)
  1611. {
  1612. struct drm_device *dev = arg;
  1613. struct drm_i915_private *dev_priv = dev->dev_private;
  1614. u32 master_ctl, iir;
  1615. irqreturn_t ret = IRQ_NONE;
  1616. if (!intel_irqs_enabled(dev_priv))
  1617. return IRQ_NONE;
  1618. for (;;) {
  1619. master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
  1620. iir = I915_READ(VLV_IIR);
  1621. if (master_ctl == 0 && iir == 0)
  1622. break;
  1623. ret = IRQ_HANDLED;
  1624. I915_WRITE(GEN8_MASTER_IRQ, 0);
  1625. /* Find, clear, then process each source of interrupt */
  1626. if (iir) {
  1627. /* Consume port before clearing IIR or we'll miss events */
  1628. if (iir & I915_DISPLAY_PORT_INTERRUPT)
  1629. i9xx_hpd_irq_handler(dev);
  1630. I915_WRITE(VLV_IIR, iir);
  1631. }
  1632. gen8_gt_irq_handler(dev, dev_priv, master_ctl);
  1633. /* Call regardless, as some status bits might not be
  1634. * signalled in iir */
  1635. valleyview_pipestat_irq_handler(dev, iir);
  1636. I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
  1637. POSTING_READ(GEN8_MASTER_IRQ);
  1638. }
  1639. return ret;
  1640. }
  1641. static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
  1642. {
  1643. struct drm_i915_private *dev_priv = dev->dev_private;
  1644. int pipe;
  1645. u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
  1646. u32 dig_hotplug_reg;
  1647. dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
  1648. I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
  1649. intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
  1650. if (pch_iir & SDE_AUDIO_POWER_MASK) {
  1651. int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
  1652. SDE_AUDIO_POWER_SHIFT);
  1653. DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
  1654. port_name(port));
  1655. }
  1656. if (pch_iir & SDE_AUX_MASK)
  1657. dp_aux_irq_handler(dev);
  1658. if (pch_iir & SDE_GMBUS)
  1659. gmbus_irq_handler(dev);
  1660. if (pch_iir & SDE_AUDIO_HDCP_MASK)
  1661. DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
  1662. if (pch_iir & SDE_AUDIO_TRANS_MASK)
  1663. DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
  1664. if (pch_iir & SDE_POISON)
  1665. DRM_ERROR("PCH poison interrupt\n");
  1666. if (pch_iir & SDE_FDI_MASK)
  1667. for_each_pipe(dev_priv, pipe)
  1668. DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
  1669. pipe_name(pipe),
  1670. I915_READ(FDI_RX_IIR(pipe)));
  1671. if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
  1672. DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
  1673. if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
  1674. DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
  1675. if (pch_iir & SDE_TRANSA_FIFO_UNDER)
  1676. intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
  1677. if (pch_iir & SDE_TRANSB_FIFO_UNDER)
  1678. intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
  1679. }
  1680. static void ivb_err_int_handler(struct drm_device *dev)
  1681. {
  1682. struct drm_i915_private *dev_priv = dev->dev_private;
  1683. u32 err_int = I915_READ(GEN7_ERR_INT);
  1684. enum pipe pipe;
  1685. if (err_int & ERR_INT_POISON)
  1686. DRM_ERROR("Poison interrupt\n");
  1687. for_each_pipe(dev_priv, pipe) {
  1688. if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
  1689. intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
  1690. if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
  1691. if (IS_IVYBRIDGE(dev))
  1692. ivb_pipe_crc_irq_handler(dev, pipe);
  1693. else
  1694. hsw_pipe_crc_irq_handler(dev, pipe);
  1695. }
  1696. }
  1697. I915_WRITE(GEN7_ERR_INT, err_int);
  1698. }
  1699. static void cpt_serr_int_handler(struct drm_device *dev)
  1700. {
  1701. struct drm_i915_private *dev_priv = dev->dev_private;
  1702. u32 serr_int = I915_READ(SERR_INT);
  1703. if (serr_int & SERR_INT_POISON)
  1704. DRM_ERROR("PCH poison interrupt\n");
  1705. if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
  1706. intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
  1707. if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
  1708. intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
  1709. if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
  1710. intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
  1711. I915_WRITE(SERR_INT, serr_int);
  1712. }
  1713. static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
  1714. {
  1715. struct drm_i915_private *dev_priv = dev->dev_private;
  1716. int pipe;
  1717. u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
  1718. u32 dig_hotplug_reg;
  1719. dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
  1720. I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
  1721. intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
  1722. if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
  1723. int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
  1724. SDE_AUDIO_POWER_SHIFT_CPT);
  1725. DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
  1726. port_name(port));
  1727. }
  1728. if (pch_iir & SDE_AUX_MASK_CPT)
  1729. dp_aux_irq_handler(dev);
  1730. if (pch_iir & SDE_GMBUS_CPT)
  1731. gmbus_irq_handler(dev);
  1732. if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
  1733. DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
  1734. if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
  1735. DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
  1736. if (pch_iir & SDE_FDI_MASK_CPT)
  1737. for_each_pipe(dev_priv, pipe)
  1738. DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
  1739. pipe_name(pipe),
  1740. I915_READ(FDI_RX_IIR(pipe)));
  1741. if (pch_iir & SDE_ERROR_CPT)
  1742. cpt_serr_int_handler(dev);
  1743. }
  1744. static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
  1745. {
  1746. struct drm_i915_private *dev_priv = dev->dev_private;
  1747. enum pipe pipe;
  1748. if (de_iir & DE_AUX_CHANNEL_A)
  1749. dp_aux_irq_handler(dev);
  1750. if (de_iir & DE_GSE)
  1751. intel_opregion_asle_intr(dev);
  1752. if (de_iir & DE_POISON)
  1753. DRM_ERROR("Poison interrupt\n");
  1754. for_each_pipe(dev_priv, pipe) {
  1755. if (de_iir & DE_PIPE_VBLANK(pipe) &&
  1756. intel_pipe_handle_vblank(dev, pipe))
  1757. intel_check_page_flip(dev, pipe);
  1758. if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
  1759. intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
  1760. if (de_iir & DE_PIPE_CRC_DONE(pipe))
  1761. i9xx_pipe_crc_irq_handler(dev, pipe);
  1762. /* plane/pipes map 1:1 on ilk+ */
  1763. if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
  1764. intel_prepare_page_flip(dev, pipe);
  1765. intel_finish_page_flip_plane(dev, pipe);
  1766. }
  1767. }
  1768. /* check event from PCH */
  1769. if (de_iir & DE_PCH_EVENT) {
  1770. u32 pch_iir = I915_READ(SDEIIR);
  1771. if (HAS_PCH_CPT(dev))
  1772. cpt_irq_handler(dev, pch_iir);
  1773. else
  1774. ibx_irq_handler(dev, pch_iir);
  1775. /* should clear PCH hotplug event before clear CPU irq */
  1776. I915_WRITE(SDEIIR, pch_iir);
  1777. }
  1778. if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
  1779. ironlake_rps_change_irq_handler(dev);
  1780. }
  1781. static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
  1782. {
  1783. struct drm_i915_private *dev_priv = dev->dev_private;
  1784. enum pipe pipe;
  1785. if (de_iir & DE_ERR_INT_IVB)
  1786. ivb_err_int_handler(dev);
  1787. if (de_iir & DE_AUX_CHANNEL_A_IVB)
  1788. dp_aux_irq_handler(dev);
  1789. if (de_iir & DE_GSE_IVB)
  1790. intel_opregion_asle_intr(dev);
  1791. for_each_pipe(dev_priv, pipe) {
  1792. if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
  1793. intel_pipe_handle_vblank(dev, pipe))
  1794. intel_check_page_flip(dev, pipe);
  1795. /* plane/pipes map 1:1 on ilk+ */
  1796. if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
  1797. intel_prepare_page_flip(dev, pipe);
  1798. intel_finish_page_flip_plane(dev, pipe);
  1799. }
  1800. }
  1801. /* check event from PCH */
  1802. if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
  1803. u32 pch_iir = I915_READ(SDEIIR);
  1804. cpt_irq_handler(dev, pch_iir);
  1805. /* clear PCH hotplug event before clear CPU irq */
  1806. I915_WRITE(SDEIIR, pch_iir);
  1807. }
  1808. }
  1809. /*
  1810. * To handle irqs with the minimum potential races with fresh interrupts, we:
  1811. * 1 - Disable Master Interrupt Control.
  1812. * 2 - Find the source(s) of the interrupt.
  1813. * 3 - Clear the Interrupt Identity bits (IIR).
  1814. * 4 - Process the interrupt(s) that had bits set in the IIRs.
  1815. * 5 - Re-enable Master Interrupt Control.
  1816. */
  1817. static irqreturn_t ironlake_irq_handler(int irq, void *arg)
  1818. {
  1819. struct drm_device *dev = arg;
  1820. struct drm_i915_private *dev_priv = dev->dev_private;
  1821. u32 de_iir, gt_iir, de_ier, sde_ier = 0;
  1822. irqreturn_t ret = IRQ_NONE;
  1823. if (!intel_irqs_enabled(dev_priv))
  1824. return IRQ_NONE;
  1825. /* We get interrupts on unclaimed registers, so check for this before we
  1826. * do any I915_{READ,WRITE}. */
  1827. intel_uncore_check_errors(dev);
  1828. /* disable master interrupt before clearing iir */
  1829. de_ier = I915_READ(DEIER);
  1830. I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
  1831. POSTING_READ(DEIER);
  1832. /* Disable south interrupts. We'll only write to SDEIIR once, so further
  1833. * interrupts will will be stored on its back queue, and then we'll be
  1834. * able to process them after we restore SDEIER (as soon as we restore
  1835. * it, we'll get an interrupt if SDEIIR still has something to process
  1836. * due to its back queue). */
  1837. if (!HAS_PCH_NOP(dev)) {
  1838. sde_ier = I915_READ(SDEIER);
  1839. I915_WRITE(SDEIER, 0);
  1840. POSTING_READ(SDEIER);
  1841. }
  1842. /* Find, clear, then process each source of interrupt */
  1843. gt_iir = I915_READ(GTIIR);
  1844. if (gt_iir) {
  1845. I915_WRITE(GTIIR, gt_iir);
  1846. ret = IRQ_HANDLED;
  1847. if (INTEL_INFO(dev)->gen >= 6)
  1848. snb_gt_irq_handler(dev, dev_priv, gt_iir);
  1849. else
  1850. ilk_gt_irq_handler(dev, dev_priv, gt_iir);
  1851. }
  1852. de_iir = I915_READ(DEIIR);
  1853. if (de_iir) {
  1854. I915_WRITE(DEIIR, de_iir);
  1855. ret = IRQ_HANDLED;
  1856. if (INTEL_INFO(dev)->gen >= 7)
  1857. ivb_display_irq_handler(dev, de_iir);
  1858. else
  1859. ilk_display_irq_handler(dev, de_iir);
  1860. }
  1861. if (INTEL_INFO(dev)->gen >= 6) {
  1862. u32 pm_iir = I915_READ(GEN6_PMIIR);
  1863. if (pm_iir) {
  1864. I915_WRITE(GEN6_PMIIR, pm_iir);
  1865. ret = IRQ_HANDLED;
  1866. gen6_rps_irq_handler(dev_priv, pm_iir);
  1867. }
  1868. }
  1869. I915_WRITE(DEIER, de_ier);
  1870. POSTING_READ(DEIER);
  1871. if (!HAS_PCH_NOP(dev)) {
  1872. I915_WRITE(SDEIER, sde_ier);
  1873. POSTING_READ(SDEIER);
  1874. }
  1875. return ret;
  1876. }
  1877. static irqreturn_t gen8_irq_handler(int irq, void *arg)
  1878. {
  1879. struct drm_device *dev = arg;
  1880. struct drm_i915_private *dev_priv = dev->dev_private;
  1881. u32 master_ctl;
  1882. irqreturn_t ret = IRQ_NONE;
  1883. uint32_t tmp = 0;
  1884. enum pipe pipe;
  1885. u32 aux_mask = GEN8_AUX_CHANNEL_A;
  1886. if (!intel_irqs_enabled(dev_priv))
  1887. return IRQ_NONE;
  1888. if (IS_GEN9(dev))
  1889. aux_mask |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
  1890. GEN9_AUX_CHANNEL_D;
  1891. master_ctl = I915_READ(GEN8_MASTER_IRQ);
  1892. master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
  1893. if (!master_ctl)
  1894. return IRQ_NONE;
  1895. I915_WRITE(GEN8_MASTER_IRQ, 0);
  1896. POSTING_READ(GEN8_MASTER_IRQ);
  1897. /* Find, clear, then process each source of interrupt */
  1898. ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
  1899. if (master_ctl & GEN8_DE_MISC_IRQ) {
  1900. tmp = I915_READ(GEN8_DE_MISC_IIR);
  1901. if (tmp) {
  1902. I915_WRITE(GEN8_DE_MISC_IIR, tmp);
  1903. ret = IRQ_HANDLED;
  1904. if (tmp & GEN8_DE_MISC_GSE)
  1905. intel_opregion_asle_intr(dev);
  1906. else
  1907. DRM_ERROR("Unexpected DE Misc interrupt\n");
  1908. }
  1909. else
  1910. DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
  1911. }
  1912. if (master_ctl & GEN8_DE_PORT_IRQ) {
  1913. tmp = I915_READ(GEN8_DE_PORT_IIR);
  1914. if (tmp) {
  1915. I915_WRITE(GEN8_DE_PORT_IIR, tmp);
  1916. ret = IRQ_HANDLED;
  1917. if (tmp & aux_mask)
  1918. dp_aux_irq_handler(dev);
  1919. else
  1920. DRM_ERROR("Unexpected DE Port interrupt\n");
  1921. }
  1922. else
  1923. DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
  1924. }
  1925. for_each_pipe(dev_priv, pipe) {
  1926. uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
  1927. if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
  1928. continue;
  1929. pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
  1930. if (pipe_iir) {
  1931. ret = IRQ_HANDLED;
  1932. I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
  1933. if (pipe_iir & GEN8_PIPE_VBLANK &&
  1934. intel_pipe_handle_vblank(dev, pipe))
  1935. intel_check_page_flip(dev, pipe);
  1936. if (IS_GEN9(dev))
  1937. flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
  1938. else
  1939. flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
  1940. if (flip_done) {
  1941. intel_prepare_page_flip(dev, pipe);
  1942. intel_finish_page_flip_plane(dev, pipe);
  1943. }
  1944. if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
  1945. hsw_pipe_crc_irq_handler(dev, pipe);
  1946. if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
  1947. intel_cpu_fifo_underrun_irq_handler(dev_priv,
  1948. pipe);
  1949. if (IS_GEN9(dev))
  1950. fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
  1951. else
  1952. fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
  1953. if (fault_errors)
  1954. DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
  1955. pipe_name(pipe),
  1956. pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
  1957. } else
  1958. DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
  1959. }
  1960. if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
  1961. /*
  1962. * FIXME(BDW): Assume for now that the new interrupt handling
  1963. * scheme also closed the SDE interrupt handling race we've seen
  1964. * on older pch-split platforms. But this needs testing.
  1965. */
  1966. u32 pch_iir = I915_READ(SDEIIR);
  1967. if (pch_iir) {
  1968. I915_WRITE(SDEIIR, pch_iir);
  1969. ret = IRQ_HANDLED;
  1970. cpt_irq_handler(dev, pch_iir);
  1971. } else
  1972. DRM_ERROR("The master control interrupt lied (SDE)!\n");
  1973. }
  1974. I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
  1975. POSTING_READ(GEN8_MASTER_IRQ);
  1976. return ret;
  1977. }
  1978. static void i915_error_wake_up(struct drm_i915_private *dev_priv,
  1979. bool reset_completed)
  1980. {
  1981. struct intel_engine_cs *ring;
  1982. int i;
  1983. /*
  1984. * Notify all waiters for GPU completion events that reset state has
  1985. * been changed, and that they need to restart their wait after
  1986. * checking for potential errors (and bail out to drop locks if there is
  1987. * a gpu reset pending so that i915_error_work_func can acquire them).
  1988. */
  1989. /* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
  1990. for_each_ring(ring, dev_priv, i)
  1991. wake_up_all(&ring->irq_queue);
  1992. /* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
  1993. wake_up_all(&dev_priv->pending_flip_queue);
  1994. /*
  1995. * Signal tasks blocked in i915_gem_wait_for_error that the pending
  1996. * reset state is cleared.
  1997. */
  1998. if (reset_completed)
  1999. wake_up_all(&dev_priv->gpu_error.reset_queue);
  2000. }
  2001. /**
  2002. * i915_reset_and_wakeup - do process context error handling work
  2003. *
  2004. * Fire an error uevent so userspace can see that a hang or error
  2005. * was detected.
  2006. */
  2007. static void i915_reset_and_wakeup(struct drm_device *dev)
  2008. {
  2009. struct drm_i915_private *dev_priv = to_i915(dev);
  2010. struct i915_gpu_error *error = &dev_priv->gpu_error;
  2011. char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
  2012. char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
  2013. char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
  2014. int ret;
  2015. kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
  2016. /*
  2017. * Note that there's only one work item which does gpu resets, so we
  2018. * need not worry about concurrent gpu resets potentially incrementing
  2019. * error->reset_counter twice. We only need to take care of another
  2020. * racing irq/hangcheck declaring the gpu dead for a second time. A
  2021. * quick check for that is good enough: schedule_work ensures the
  2022. * correct ordering between hang detection and this work item, and since
  2023. * the reset in-progress bit is only ever set by code outside of this
  2024. * work we don't need to worry about any other races.
  2025. */
  2026. if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
  2027. DRM_DEBUG_DRIVER("resetting chip\n");
  2028. kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
  2029. reset_event);
  2030. /*
  2031. * In most cases it's guaranteed that we get here with an RPM
  2032. * reference held, for example because there is a pending GPU
  2033. * request that won't finish until the reset is done. This
  2034. * isn't the case at least when we get here by doing a
  2035. * simulated reset via debugs, so get an RPM reference.
  2036. */
  2037. intel_runtime_pm_get(dev_priv);
  2038. intel_prepare_reset(dev);
  2039. /*
  2040. * All state reset _must_ be completed before we update the
  2041. * reset counter, for otherwise waiters might miss the reset
  2042. * pending state and not properly drop locks, resulting in
  2043. * deadlocks with the reset work.
  2044. */
  2045. ret = i915_reset(dev);
  2046. intel_finish_reset(dev);
  2047. intel_runtime_pm_put(dev_priv);
  2048. if (ret == 0) {
  2049. /*
  2050. * After all the gem state is reset, increment the reset
  2051. * counter and wake up everyone waiting for the reset to
  2052. * complete.
  2053. *
  2054. * Since unlock operations are a one-sided barrier only,
  2055. * we need to insert a barrier here to order any seqno
  2056. * updates before
  2057. * the counter increment.
  2058. */
  2059. smp_mb__before_atomic();
  2060. atomic_inc(&dev_priv->gpu_error.reset_counter);
  2061. kobject_uevent_env(&dev->primary->kdev->kobj,
  2062. KOBJ_CHANGE, reset_done_event);
  2063. } else {
  2064. atomic_set_mask(I915_WEDGED, &error->reset_counter);
  2065. }
  2066. /*
  2067. * Note: The wake_up also serves as a memory barrier so that
  2068. * waiters see the update value of the reset counter atomic_t.
  2069. */
  2070. i915_error_wake_up(dev_priv, true);
  2071. }
  2072. }
  2073. static void i915_report_and_clear_eir(struct drm_device *dev)
  2074. {
  2075. struct drm_i915_private *dev_priv = dev->dev_private;
  2076. uint32_t instdone[I915_NUM_INSTDONE_REG];
  2077. u32 eir = I915_READ(EIR);
  2078. int pipe, i;
  2079. if (!eir)
  2080. return;
  2081. pr_err("render error detected, EIR: 0x%08x\n", eir);
  2082. i915_get_extra_instdone(dev, instdone);
  2083. if (IS_G4X(dev)) {
  2084. if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
  2085. u32 ipeir = I915_READ(IPEIR_I965);
  2086. pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
  2087. pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
  2088. for (i = 0; i < ARRAY_SIZE(instdone); i++)
  2089. pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
  2090. pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
  2091. pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
  2092. I915_WRITE(IPEIR_I965, ipeir);
  2093. POSTING_READ(IPEIR_I965);
  2094. }
  2095. if (eir & GM45_ERROR_PAGE_TABLE) {
  2096. u32 pgtbl_err = I915_READ(PGTBL_ER);
  2097. pr_err("page table error\n");
  2098. pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
  2099. I915_WRITE(PGTBL_ER, pgtbl_err);
  2100. POSTING_READ(PGTBL_ER);
  2101. }
  2102. }
  2103. if (!IS_GEN2(dev)) {
  2104. if (eir & I915_ERROR_PAGE_TABLE) {
  2105. u32 pgtbl_err = I915_READ(PGTBL_ER);
  2106. pr_err("page table error\n");
  2107. pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
  2108. I915_WRITE(PGTBL_ER, pgtbl_err);
  2109. POSTING_READ(PGTBL_ER);
  2110. }
  2111. }
  2112. if (eir & I915_ERROR_MEMORY_REFRESH) {
  2113. pr_err("memory refresh error:\n");
  2114. for_each_pipe(dev_priv, pipe)
  2115. pr_err("pipe %c stat: 0x%08x\n",
  2116. pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
  2117. /* pipestat has already been acked */
  2118. }
  2119. if (eir & I915_ERROR_INSTRUCTION) {
  2120. pr_err("instruction error\n");
  2121. pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
  2122. for (i = 0; i < ARRAY_SIZE(instdone); i++)
  2123. pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
  2124. if (INTEL_INFO(dev)->gen < 4) {
  2125. u32 ipeir = I915_READ(IPEIR);
  2126. pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
  2127. pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
  2128. pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
  2129. I915_WRITE(IPEIR, ipeir);
  2130. POSTING_READ(IPEIR);
  2131. } else {
  2132. u32 ipeir = I915_READ(IPEIR_I965);
  2133. pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
  2134. pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
  2135. pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
  2136. pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
  2137. I915_WRITE(IPEIR_I965, ipeir);
  2138. POSTING_READ(IPEIR_I965);
  2139. }
  2140. }
  2141. I915_WRITE(EIR, eir);
  2142. POSTING_READ(EIR);
  2143. eir = I915_READ(EIR);
  2144. if (eir) {
  2145. /*
  2146. * some errors might have become stuck,
  2147. * mask them.
  2148. */
  2149. DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
  2150. I915_WRITE(EMR, I915_READ(EMR) | eir);
  2151. I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  2152. }
  2153. }
  2154. /**
  2155. * i915_handle_error - handle a gpu error
  2156. * @dev: drm device
  2157. *
  2158. * Do some basic checking of regsiter state at error time and
  2159. * dump it to the syslog. Also call i915_capture_error_state() to make
  2160. * sure we get a record and make it available in debugfs. Fire a uevent
  2161. * so userspace knows something bad happened (should trigger collection
  2162. * of a ring dump etc.).
  2163. */
  2164. void i915_handle_error(struct drm_device *dev, bool wedged,
  2165. const char *fmt, ...)
  2166. {
  2167. struct drm_i915_private *dev_priv = dev->dev_private;
  2168. va_list args;
  2169. char error_msg[80];
  2170. va_start(args, fmt);
  2171. vscnprintf(error_msg, sizeof(error_msg), fmt, args);
  2172. va_end(args);
  2173. i915_capture_error_state(dev, wedged, error_msg);
  2174. i915_report_and_clear_eir(dev);
  2175. if (wedged) {
  2176. atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
  2177. &dev_priv->gpu_error.reset_counter);
  2178. /*
  2179. * Wakeup waiting processes so that the reset function
  2180. * i915_reset_and_wakeup doesn't deadlock trying to grab
  2181. * various locks. By bumping the reset counter first, the woken
  2182. * processes will see a reset in progress and back off,
  2183. * releasing their locks and then wait for the reset completion.
  2184. * We must do this for _all_ gpu waiters that might hold locks
  2185. * that the reset work needs to acquire.
  2186. *
  2187. * Note: The wake_up serves as the required memory barrier to
  2188. * ensure that the waiters see the updated value of the reset
  2189. * counter atomic_t.
  2190. */
  2191. i915_error_wake_up(dev_priv, false);
  2192. }
  2193. i915_reset_and_wakeup(dev);
  2194. }
  2195. /* Called from drm generic code, passed 'crtc' which
  2196. * we use as a pipe index
  2197. */
  2198. static int i915_enable_vblank(struct drm_device *dev, int pipe)
  2199. {
  2200. struct drm_i915_private *dev_priv = dev->dev_private;
  2201. unsigned long irqflags;
  2202. if (!i915_pipe_enabled(dev, pipe))
  2203. return -EINVAL;
  2204. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2205. if (INTEL_INFO(dev)->gen >= 4)
  2206. i915_enable_pipestat(dev_priv, pipe,
  2207. PIPE_START_VBLANK_INTERRUPT_STATUS);
  2208. else
  2209. i915_enable_pipestat(dev_priv, pipe,
  2210. PIPE_VBLANK_INTERRUPT_STATUS);
  2211. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2212. return 0;
  2213. }
  2214. static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
  2215. {
  2216. struct drm_i915_private *dev_priv = dev->dev_private;
  2217. unsigned long irqflags;
  2218. uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
  2219. DE_PIPE_VBLANK(pipe);
  2220. if (!i915_pipe_enabled(dev, pipe))
  2221. return -EINVAL;
  2222. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2223. ironlake_enable_display_irq(dev_priv, bit);
  2224. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2225. return 0;
  2226. }
  2227. static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
  2228. {
  2229. struct drm_i915_private *dev_priv = dev->dev_private;
  2230. unsigned long irqflags;
  2231. if (!i915_pipe_enabled(dev, pipe))
  2232. return -EINVAL;
  2233. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2234. i915_enable_pipestat(dev_priv, pipe,
  2235. PIPE_START_VBLANK_INTERRUPT_STATUS);
  2236. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2237. return 0;
  2238. }
  2239. static int gen8_enable_vblank(struct drm_device *dev, int pipe)
  2240. {
  2241. struct drm_i915_private *dev_priv = dev->dev_private;
  2242. unsigned long irqflags;
  2243. if (!i915_pipe_enabled(dev, pipe))
  2244. return -EINVAL;
  2245. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2246. dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
  2247. I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
  2248. POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
  2249. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2250. return 0;
  2251. }
  2252. /* Called from drm generic code, passed 'crtc' which
  2253. * we use as a pipe index
  2254. */
  2255. static void i915_disable_vblank(struct drm_device *dev, int pipe)
  2256. {
  2257. struct drm_i915_private *dev_priv = dev->dev_private;
  2258. unsigned long irqflags;
  2259. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2260. i915_disable_pipestat(dev_priv, pipe,
  2261. PIPE_VBLANK_INTERRUPT_STATUS |
  2262. PIPE_START_VBLANK_INTERRUPT_STATUS);
  2263. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2264. }
  2265. static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
  2266. {
  2267. struct drm_i915_private *dev_priv = dev->dev_private;
  2268. unsigned long irqflags;
  2269. uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
  2270. DE_PIPE_VBLANK(pipe);
  2271. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2272. ironlake_disable_display_irq(dev_priv, bit);
  2273. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2274. }
  2275. static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
  2276. {
  2277. struct drm_i915_private *dev_priv = dev->dev_private;
  2278. unsigned long irqflags;
  2279. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2280. i915_disable_pipestat(dev_priv, pipe,
  2281. PIPE_START_VBLANK_INTERRUPT_STATUS);
  2282. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2283. }
  2284. static void gen8_disable_vblank(struct drm_device *dev, int pipe)
  2285. {
  2286. struct drm_i915_private *dev_priv = dev->dev_private;
  2287. unsigned long irqflags;
  2288. if (!i915_pipe_enabled(dev, pipe))
  2289. return;
  2290. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2291. dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
  2292. I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
  2293. POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
  2294. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2295. }
  2296. static struct drm_i915_gem_request *
  2297. ring_last_request(struct intel_engine_cs *ring)
  2298. {
  2299. return list_entry(ring->request_list.prev,
  2300. struct drm_i915_gem_request, list);
  2301. }
  2302. static bool
  2303. ring_idle(struct intel_engine_cs *ring)
  2304. {
  2305. return (list_empty(&ring->request_list) ||
  2306. i915_gem_request_completed(ring_last_request(ring), false));
  2307. }
  2308. static bool
  2309. ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
  2310. {
  2311. if (INTEL_INFO(dev)->gen >= 8) {
  2312. return (ipehr >> 23) == 0x1c;
  2313. } else {
  2314. ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
  2315. return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
  2316. MI_SEMAPHORE_REGISTER);
  2317. }
  2318. }
  2319. static struct intel_engine_cs *
  2320. semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
  2321. {
  2322. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  2323. struct intel_engine_cs *signaller;
  2324. int i;
  2325. if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
  2326. for_each_ring(signaller, dev_priv, i) {
  2327. if (ring == signaller)
  2328. continue;
  2329. if (offset == signaller->semaphore.signal_ggtt[ring->id])
  2330. return signaller;
  2331. }
  2332. } else {
  2333. u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
  2334. for_each_ring(signaller, dev_priv, i) {
  2335. if(ring == signaller)
  2336. continue;
  2337. if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
  2338. return signaller;
  2339. }
  2340. }
  2341. DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
  2342. ring->id, ipehr, offset);
  2343. return NULL;
  2344. }
  2345. static struct intel_engine_cs *
  2346. semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
  2347. {
  2348. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  2349. u32 cmd, ipehr, head;
  2350. u64 offset = 0;
  2351. int i, backwards;
  2352. ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
  2353. if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
  2354. return NULL;
  2355. /*
  2356. * HEAD is likely pointing to the dword after the actual command,
  2357. * so scan backwards until we find the MBOX. But limit it to just 3
  2358. * or 4 dwords depending on the semaphore wait command size.
  2359. * Note that we don't care about ACTHD here since that might
  2360. * point at at batch, and semaphores are always emitted into the
  2361. * ringbuffer itself.
  2362. */
  2363. head = I915_READ_HEAD(ring) & HEAD_ADDR;
  2364. backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
  2365. for (i = backwards; i; --i) {
  2366. /*
  2367. * Be paranoid and presume the hw has gone off into the wild -
  2368. * our ring is smaller than what the hardware (and hence
  2369. * HEAD_ADDR) allows. Also handles wrap-around.
  2370. */
  2371. head &= ring->buffer->size - 1;
  2372. /* This here seems to blow up */
  2373. cmd = ioread32(ring->buffer->virtual_start + head);
  2374. if (cmd == ipehr)
  2375. break;
  2376. head -= 4;
  2377. }
  2378. if (!i)
  2379. return NULL;
  2380. *seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
  2381. if (INTEL_INFO(ring->dev)->gen >= 8) {
  2382. offset = ioread32(ring->buffer->virtual_start + head + 12);
  2383. offset <<= 32;
  2384. offset = ioread32(ring->buffer->virtual_start + head + 8);
  2385. }
  2386. return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
  2387. }
  2388. static int semaphore_passed(struct intel_engine_cs *ring)
  2389. {
  2390. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  2391. struct intel_engine_cs *signaller;
  2392. u32 seqno;
  2393. ring->hangcheck.deadlock++;
  2394. signaller = semaphore_waits_for(ring, &seqno);
  2395. if (signaller == NULL)
  2396. return -1;
  2397. /* Prevent pathological recursion due to driver bugs */
  2398. if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
  2399. return -1;
  2400. if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
  2401. return 1;
  2402. /* cursory check for an unkickable deadlock */
  2403. if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
  2404. semaphore_passed(signaller) < 0)
  2405. return -1;
  2406. return 0;
  2407. }
  2408. static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
  2409. {
  2410. struct intel_engine_cs *ring;
  2411. int i;
  2412. for_each_ring(ring, dev_priv, i)
  2413. ring->hangcheck.deadlock = 0;
  2414. }
  2415. static enum intel_ring_hangcheck_action
  2416. ring_stuck(struct intel_engine_cs *ring, u64 acthd)
  2417. {
  2418. struct drm_device *dev = ring->dev;
  2419. struct drm_i915_private *dev_priv = dev->dev_private;
  2420. u32 tmp;
  2421. if (acthd != ring->hangcheck.acthd) {
  2422. if (acthd > ring->hangcheck.max_acthd) {
  2423. ring->hangcheck.max_acthd = acthd;
  2424. return HANGCHECK_ACTIVE;
  2425. }
  2426. return HANGCHECK_ACTIVE_LOOP;
  2427. }
  2428. if (IS_GEN2(dev))
  2429. return HANGCHECK_HUNG;
  2430. /* Is the chip hanging on a WAIT_FOR_EVENT?
  2431. * If so we can simply poke the RB_WAIT bit
  2432. * and break the hang. This should work on
  2433. * all but the second generation chipsets.
  2434. */
  2435. tmp = I915_READ_CTL(ring);
  2436. if (tmp & RING_WAIT) {
  2437. i915_handle_error(dev, false,
  2438. "Kicking stuck wait on %s",
  2439. ring->name);
  2440. I915_WRITE_CTL(ring, tmp);
  2441. return HANGCHECK_KICK;
  2442. }
  2443. if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
  2444. switch (semaphore_passed(ring)) {
  2445. default:
  2446. return HANGCHECK_HUNG;
  2447. case 1:
  2448. i915_handle_error(dev, false,
  2449. "Kicking stuck semaphore on %s",
  2450. ring->name);
  2451. I915_WRITE_CTL(ring, tmp);
  2452. return HANGCHECK_KICK;
  2453. case 0:
  2454. return HANGCHECK_WAIT;
  2455. }
  2456. }
  2457. return HANGCHECK_HUNG;
  2458. }
  2459. /*
  2460. * This is called when the chip hasn't reported back with completed
  2461. * batchbuffers in a long time. We keep track per ring seqno progress and
  2462. * if there are no progress, hangcheck score for that ring is increased.
  2463. * Further, acthd is inspected to see if the ring is stuck. On stuck case
  2464. * we kick the ring. If we see no progress on three subsequent calls
  2465. * we assume chip is wedged and try to fix it by resetting the chip.
  2466. */
  2467. static void i915_hangcheck_elapsed(struct work_struct *work)
  2468. {
  2469. struct drm_i915_private *dev_priv =
  2470. container_of(work, typeof(*dev_priv),
  2471. gpu_error.hangcheck_work.work);
  2472. struct drm_device *dev = dev_priv->dev;
  2473. struct intel_engine_cs *ring;
  2474. int i;
  2475. int busy_count = 0, rings_hung = 0;
  2476. bool stuck[I915_NUM_RINGS] = { 0 };
  2477. #define BUSY 1
  2478. #define KICK 5
  2479. #define HUNG 20
  2480. if (!i915.enable_hangcheck)
  2481. return;
  2482. for_each_ring(ring, dev_priv, i) {
  2483. u64 acthd;
  2484. u32 seqno;
  2485. bool busy = true;
  2486. semaphore_clear_deadlocks(dev_priv);
  2487. seqno = ring->get_seqno(ring, false);
  2488. acthd = intel_ring_get_active_head(ring);
  2489. if (ring->hangcheck.seqno == seqno) {
  2490. if (ring_idle(ring)) {
  2491. ring->hangcheck.action = HANGCHECK_IDLE;
  2492. if (waitqueue_active(&ring->irq_queue)) {
  2493. /* Issue a wake-up to catch stuck h/w. */
  2494. if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
  2495. if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
  2496. DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
  2497. ring->name);
  2498. else
  2499. DRM_INFO("Fake missed irq on %s\n",
  2500. ring->name);
  2501. wake_up_all(&ring->irq_queue);
  2502. }
  2503. /* Safeguard against driver failure */
  2504. ring->hangcheck.score += BUSY;
  2505. } else
  2506. busy = false;
  2507. } else {
  2508. /* We always increment the hangcheck score
  2509. * if the ring is busy and still processing
  2510. * the same request, so that no single request
  2511. * can run indefinitely (such as a chain of
  2512. * batches). The only time we do not increment
  2513. * the hangcheck score on this ring, if this
  2514. * ring is in a legitimate wait for another
  2515. * ring. In that case the waiting ring is a
  2516. * victim and we want to be sure we catch the
  2517. * right culprit. Then every time we do kick
  2518. * the ring, add a small increment to the
  2519. * score so that we can catch a batch that is
  2520. * being repeatedly kicked and so responsible
  2521. * for stalling the machine.
  2522. */
  2523. ring->hangcheck.action = ring_stuck(ring,
  2524. acthd);
  2525. switch (ring->hangcheck.action) {
  2526. case HANGCHECK_IDLE:
  2527. case HANGCHECK_WAIT:
  2528. case HANGCHECK_ACTIVE:
  2529. break;
  2530. case HANGCHECK_ACTIVE_LOOP:
  2531. ring->hangcheck.score += BUSY;
  2532. break;
  2533. case HANGCHECK_KICK:
  2534. ring->hangcheck.score += KICK;
  2535. break;
  2536. case HANGCHECK_HUNG:
  2537. ring->hangcheck.score += HUNG;
  2538. stuck[i] = true;
  2539. break;
  2540. }
  2541. }
  2542. } else {
  2543. ring->hangcheck.action = HANGCHECK_ACTIVE;
  2544. /* Gradually reduce the count so that we catch DoS
  2545. * attempts across multiple batches.
  2546. */
  2547. if (ring->hangcheck.score > 0)
  2548. ring->hangcheck.score--;
  2549. ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
  2550. }
  2551. ring->hangcheck.seqno = seqno;
  2552. ring->hangcheck.acthd = acthd;
  2553. busy_count += busy;
  2554. }
  2555. for_each_ring(ring, dev_priv, i) {
  2556. if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
  2557. DRM_INFO("%s on %s\n",
  2558. stuck[i] ? "stuck" : "no progress",
  2559. ring->name);
  2560. rings_hung++;
  2561. }
  2562. }
  2563. if (rings_hung)
  2564. return i915_handle_error(dev, true, "Ring hung");
  2565. if (busy_count)
  2566. /* Reset timer case chip hangs without another request
  2567. * being added */
  2568. i915_queue_hangcheck(dev);
  2569. }
  2570. void i915_queue_hangcheck(struct drm_device *dev)
  2571. {
  2572. struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
  2573. if (!i915.enable_hangcheck)
  2574. return;
  2575. /* Don't continually defer the hangcheck so that it is always run at
  2576. * least once after work has been scheduled on any ring. Otherwise,
  2577. * we will ignore a hung ring if a second ring is kept busy.
  2578. */
  2579. queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
  2580. round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
  2581. }
  2582. static void ibx_irq_reset(struct drm_device *dev)
  2583. {
  2584. struct drm_i915_private *dev_priv = dev->dev_private;
  2585. if (HAS_PCH_NOP(dev))
  2586. return;
  2587. GEN5_IRQ_RESET(SDE);
  2588. if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
  2589. I915_WRITE(SERR_INT, 0xffffffff);
  2590. }
  2591. /*
  2592. * SDEIER is also touched by the interrupt handler to work around missed PCH
  2593. * interrupts. Hence we can't update it after the interrupt handler is enabled -
  2594. * instead we unconditionally enable all PCH interrupt sources here, but then
  2595. * only unmask them as needed with SDEIMR.
  2596. *
  2597. * This function needs to be called before interrupts are enabled.
  2598. */
  2599. static void ibx_irq_pre_postinstall(struct drm_device *dev)
  2600. {
  2601. struct drm_i915_private *dev_priv = dev->dev_private;
  2602. if (HAS_PCH_NOP(dev))
  2603. return;
  2604. WARN_ON(I915_READ(SDEIER) != 0);
  2605. I915_WRITE(SDEIER, 0xffffffff);
  2606. POSTING_READ(SDEIER);
  2607. }
  2608. static void gen5_gt_irq_reset(struct drm_device *dev)
  2609. {
  2610. struct drm_i915_private *dev_priv = dev->dev_private;
  2611. GEN5_IRQ_RESET(GT);
  2612. if (INTEL_INFO(dev)->gen >= 6)
  2613. GEN5_IRQ_RESET(GEN6_PM);
  2614. }
  2615. /* drm_dma.h hooks
  2616. */
  2617. static void ironlake_irq_reset(struct drm_device *dev)
  2618. {
  2619. struct drm_i915_private *dev_priv = dev->dev_private;
  2620. I915_WRITE(HWSTAM, 0xffffffff);
  2621. GEN5_IRQ_RESET(DE);
  2622. if (IS_GEN7(dev))
  2623. I915_WRITE(GEN7_ERR_INT, 0xffffffff);
  2624. gen5_gt_irq_reset(dev);
  2625. ibx_irq_reset(dev);
  2626. }
  2627. static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
  2628. {
  2629. enum pipe pipe;
  2630. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2631. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  2632. for_each_pipe(dev_priv, pipe)
  2633. I915_WRITE(PIPESTAT(pipe), 0xffff);
  2634. GEN5_IRQ_RESET(VLV_);
  2635. }
  2636. static void valleyview_irq_preinstall(struct drm_device *dev)
  2637. {
  2638. struct drm_i915_private *dev_priv = dev->dev_private;
  2639. /* VLV magic */
  2640. I915_WRITE(VLV_IMR, 0);
  2641. I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
  2642. I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
  2643. I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
  2644. gen5_gt_irq_reset(dev);
  2645. I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
  2646. vlv_display_irq_reset(dev_priv);
  2647. }
  2648. static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
  2649. {
  2650. GEN8_IRQ_RESET_NDX(GT, 0);
  2651. GEN8_IRQ_RESET_NDX(GT, 1);
  2652. GEN8_IRQ_RESET_NDX(GT, 2);
  2653. GEN8_IRQ_RESET_NDX(GT, 3);
  2654. }
  2655. static void gen8_irq_reset(struct drm_device *dev)
  2656. {
  2657. struct drm_i915_private *dev_priv = dev->dev_private;
  2658. int pipe;
  2659. I915_WRITE(GEN8_MASTER_IRQ, 0);
  2660. POSTING_READ(GEN8_MASTER_IRQ);
  2661. gen8_gt_irq_reset(dev_priv);
  2662. for_each_pipe(dev_priv, pipe)
  2663. if (intel_display_power_is_enabled(dev_priv,
  2664. POWER_DOMAIN_PIPE(pipe)))
  2665. GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
  2666. GEN5_IRQ_RESET(GEN8_DE_PORT_);
  2667. GEN5_IRQ_RESET(GEN8_DE_MISC_);
  2668. GEN5_IRQ_RESET(GEN8_PCU_);
  2669. ibx_irq_reset(dev);
  2670. }
  2671. void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv)
  2672. {
  2673. uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
  2674. spin_lock_irq(&dev_priv->irq_lock);
  2675. GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B, dev_priv->de_irq_mask[PIPE_B],
  2676. ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
  2677. GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C, dev_priv->de_irq_mask[PIPE_C],
  2678. ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
  2679. spin_unlock_irq(&dev_priv->irq_lock);
  2680. }
  2681. static void cherryview_irq_preinstall(struct drm_device *dev)
  2682. {
  2683. struct drm_i915_private *dev_priv = dev->dev_private;
  2684. I915_WRITE(GEN8_MASTER_IRQ, 0);
  2685. POSTING_READ(GEN8_MASTER_IRQ);
  2686. gen8_gt_irq_reset(dev_priv);
  2687. GEN5_IRQ_RESET(GEN8_PCU_);
  2688. I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
  2689. vlv_display_irq_reset(dev_priv);
  2690. }
  2691. static void ibx_hpd_irq_setup(struct drm_device *dev)
  2692. {
  2693. struct drm_i915_private *dev_priv = dev->dev_private;
  2694. struct intel_encoder *intel_encoder;
  2695. u32 hotplug_irqs, hotplug, enabled_irqs = 0;
  2696. if (HAS_PCH_IBX(dev)) {
  2697. hotplug_irqs = SDE_HOTPLUG_MASK;
  2698. for_each_intel_encoder(dev, intel_encoder)
  2699. if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
  2700. enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
  2701. } else {
  2702. hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
  2703. for_each_intel_encoder(dev, intel_encoder)
  2704. if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
  2705. enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
  2706. }
  2707. ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
  2708. /*
  2709. * Enable digital hotplug on the PCH, and configure the DP short pulse
  2710. * duration to 2ms (which is the minimum in the Display Port spec)
  2711. *
  2712. * This register is the same on all known PCH chips.
  2713. */
  2714. hotplug = I915_READ(PCH_PORT_HOTPLUG);
  2715. hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
  2716. hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
  2717. hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
  2718. hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
  2719. I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
  2720. }
  2721. static void ibx_irq_postinstall(struct drm_device *dev)
  2722. {
  2723. struct drm_i915_private *dev_priv = dev->dev_private;
  2724. u32 mask;
  2725. if (HAS_PCH_NOP(dev))
  2726. return;
  2727. if (HAS_PCH_IBX(dev))
  2728. mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
  2729. else
  2730. mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
  2731. GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
  2732. I915_WRITE(SDEIMR, ~mask);
  2733. }
  2734. static void gen5_gt_irq_postinstall(struct drm_device *dev)
  2735. {
  2736. struct drm_i915_private *dev_priv = dev->dev_private;
  2737. u32 pm_irqs, gt_irqs;
  2738. pm_irqs = gt_irqs = 0;
  2739. dev_priv->gt_irq_mask = ~0;
  2740. if (HAS_L3_DPF(dev)) {
  2741. /* L3 parity interrupt is always unmasked. */
  2742. dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
  2743. gt_irqs |= GT_PARITY_ERROR(dev);
  2744. }
  2745. gt_irqs |= GT_RENDER_USER_INTERRUPT;
  2746. if (IS_GEN5(dev)) {
  2747. gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
  2748. ILK_BSD_USER_INTERRUPT;
  2749. } else {
  2750. gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
  2751. }
  2752. GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
  2753. if (INTEL_INFO(dev)->gen >= 6) {
  2754. /*
  2755. * RPS interrupts will get enabled/disabled on demand when RPS
  2756. * itself is enabled/disabled.
  2757. */
  2758. if (HAS_VEBOX(dev))
  2759. pm_irqs |= PM_VEBOX_USER_INTERRUPT;
  2760. dev_priv->pm_irq_mask = 0xffffffff;
  2761. GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
  2762. }
  2763. }
  2764. static int ironlake_irq_postinstall(struct drm_device *dev)
  2765. {
  2766. struct drm_i915_private *dev_priv = dev->dev_private;
  2767. u32 display_mask, extra_mask;
  2768. if (INTEL_INFO(dev)->gen >= 7) {
  2769. display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
  2770. DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
  2771. DE_PLANEB_FLIP_DONE_IVB |
  2772. DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
  2773. extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
  2774. DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
  2775. } else {
  2776. display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
  2777. DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
  2778. DE_AUX_CHANNEL_A |
  2779. DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
  2780. DE_POISON);
  2781. extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
  2782. DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
  2783. }
  2784. dev_priv->irq_mask = ~display_mask;
  2785. I915_WRITE(HWSTAM, 0xeffe);
  2786. ibx_irq_pre_postinstall(dev);
  2787. GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
  2788. gen5_gt_irq_postinstall(dev);
  2789. ibx_irq_postinstall(dev);
  2790. if (IS_IRONLAKE_M(dev)) {
  2791. /* Enable PCU event interrupts
  2792. *
  2793. * spinlocking not required here for correctness since interrupt
  2794. * setup is guaranteed to run in single-threaded context. But we
  2795. * need it to make the assert_spin_locked happy. */
  2796. spin_lock_irq(&dev_priv->irq_lock);
  2797. ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
  2798. spin_unlock_irq(&dev_priv->irq_lock);
  2799. }
  2800. return 0;
  2801. }
  2802. static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
  2803. {
  2804. u32 pipestat_mask;
  2805. u32 iir_mask;
  2806. enum pipe pipe;
  2807. pipestat_mask = PIPESTAT_INT_STATUS_MASK |
  2808. PIPE_FIFO_UNDERRUN_STATUS;
  2809. for_each_pipe(dev_priv, pipe)
  2810. I915_WRITE(PIPESTAT(pipe), pipestat_mask);
  2811. POSTING_READ(PIPESTAT(PIPE_A));
  2812. pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
  2813. PIPE_CRC_DONE_INTERRUPT_STATUS;
  2814. i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
  2815. for_each_pipe(dev_priv, pipe)
  2816. i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
  2817. iir_mask = I915_DISPLAY_PORT_INTERRUPT |
  2818. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  2819. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
  2820. if (IS_CHERRYVIEW(dev_priv))
  2821. iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
  2822. dev_priv->irq_mask &= ~iir_mask;
  2823. I915_WRITE(VLV_IIR, iir_mask);
  2824. I915_WRITE(VLV_IIR, iir_mask);
  2825. I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
  2826. I915_WRITE(VLV_IMR, dev_priv->irq_mask);
  2827. POSTING_READ(VLV_IMR);
  2828. }
  2829. static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
  2830. {
  2831. u32 pipestat_mask;
  2832. u32 iir_mask;
  2833. enum pipe pipe;
  2834. iir_mask = I915_DISPLAY_PORT_INTERRUPT |
  2835. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  2836. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
  2837. if (IS_CHERRYVIEW(dev_priv))
  2838. iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
  2839. dev_priv->irq_mask |= iir_mask;
  2840. I915_WRITE(VLV_IMR, dev_priv->irq_mask);
  2841. I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
  2842. I915_WRITE(VLV_IIR, iir_mask);
  2843. I915_WRITE(VLV_IIR, iir_mask);
  2844. POSTING_READ(VLV_IIR);
  2845. pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
  2846. PIPE_CRC_DONE_INTERRUPT_STATUS;
  2847. i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
  2848. for_each_pipe(dev_priv, pipe)
  2849. i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
  2850. pipestat_mask = PIPESTAT_INT_STATUS_MASK |
  2851. PIPE_FIFO_UNDERRUN_STATUS;
  2852. for_each_pipe(dev_priv, pipe)
  2853. I915_WRITE(PIPESTAT(pipe), pipestat_mask);
  2854. POSTING_READ(PIPESTAT(PIPE_A));
  2855. }
  2856. void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
  2857. {
  2858. assert_spin_locked(&dev_priv->irq_lock);
  2859. if (dev_priv->display_irqs_enabled)
  2860. return;
  2861. dev_priv->display_irqs_enabled = true;
  2862. if (intel_irqs_enabled(dev_priv))
  2863. valleyview_display_irqs_install(dev_priv);
  2864. }
  2865. void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
  2866. {
  2867. assert_spin_locked(&dev_priv->irq_lock);
  2868. if (!dev_priv->display_irqs_enabled)
  2869. return;
  2870. dev_priv->display_irqs_enabled = false;
  2871. if (intel_irqs_enabled(dev_priv))
  2872. valleyview_display_irqs_uninstall(dev_priv);
  2873. }
  2874. static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
  2875. {
  2876. dev_priv->irq_mask = ~0;
  2877. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2878. POSTING_READ(PORT_HOTPLUG_EN);
  2879. I915_WRITE(VLV_IIR, 0xffffffff);
  2880. I915_WRITE(VLV_IIR, 0xffffffff);
  2881. I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
  2882. I915_WRITE(VLV_IMR, dev_priv->irq_mask);
  2883. POSTING_READ(VLV_IMR);
  2884. /* Interrupt setup is already guaranteed to be single-threaded, this is
  2885. * just to make the assert_spin_locked check happy. */
  2886. spin_lock_irq(&dev_priv->irq_lock);
  2887. if (dev_priv->display_irqs_enabled)
  2888. valleyview_display_irqs_install(dev_priv);
  2889. spin_unlock_irq(&dev_priv->irq_lock);
  2890. }
  2891. static int valleyview_irq_postinstall(struct drm_device *dev)
  2892. {
  2893. struct drm_i915_private *dev_priv = dev->dev_private;
  2894. vlv_display_irq_postinstall(dev_priv);
  2895. gen5_gt_irq_postinstall(dev);
  2896. /* ack & enable invalid PTE error interrupts */
  2897. #if 0 /* FIXME: add support to irq handler for checking these bits */
  2898. I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
  2899. I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
  2900. #endif
  2901. I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
  2902. return 0;
  2903. }
  2904. static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
  2905. {
  2906. /* These are interrupts we'll toggle with the ring mask register */
  2907. uint32_t gt_interrupts[] = {
  2908. GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
  2909. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
  2910. GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
  2911. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
  2912. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
  2913. GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
  2914. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
  2915. GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
  2916. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
  2917. 0,
  2918. GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
  2919. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
  2920. };
  2921. dev_priv->pm_irq_mask = 0xffffffff;
  2922. GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
  2923. GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
  2924. /*
  2925. * RPS interrupts will get enabled/disabled on demand when RPS itself
  2926. * is enabled/disabled.
  2927. */
  2928. GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
  2929. GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
  2930. }
  2931. static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
  2932. {
  2933. uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
  2934. uint32_t de_pipe_enables;
  2935. int pipe;
  2936. u32 aux_en = GEN8_AUX_CHANNEL_A;
  2937. if (IS_GEN9(dev_priv)) {
  2938. de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
  2939. GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
  2940. aux_en |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
  2941. GEN9_AUX_CHANNEL_D;
  2942. } else
  2943. de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
  2944. GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
  2945. de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
  2946. GEN8_PIPE_FIFO_UNDERRUN;
  2947. dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
  2948. dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
  2949. dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
  2950. for_each_pipe(dev_priv, pipe)
  2951. if (intel_display_power_is_enabled(dev_priv,
  2952. POWER_DOMAIN_PIPE(pipe)))
  2953. GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
  2954. dev_priv->de_irq_mask[pipe],
  2955. de_pipe_enables);
  2956. GEN5_IRQ_INIT(GEN8_DE_PORT_, ~aux_en, aux_en);
  2957. }
  2958. static int gen8_irq_postinstall(struct drm_device *dev)
  2959. {
  2960. struct drm_i915_private *dev_priv = dev->dev_private;
  2961. ibx_irq_pre_postinstall(dev);
  2962. gen8_gt_irq_postinstall(dev_priv);
  2963. gen8_de_irq_postinstall(dev_priv);
  2964. ibx_irq_postinstall(dev);
  2965. I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
  2966. POSTING_READ(GEN8_MASTER_IRQ);
  2967. return 0;
  2968. }
  2969. static int cherryview_irq_postinstall(struct drm_device *dev)
  2970. {
  2971. struct drm_i915_private *dev_priv = dev->dev_private;
  2972. vlv_display_irq_postinstall(dev_priv);
  2973. gen8_gt_irq_postinstall(dev_priv);
  2974. I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
  2975. POSTING_READ(GEN8_MASTER_IRQ);
  2976. return 0;
  2977. }
  2978. static void gen8_irq_uninstall(struct drm_device *dev)
  2979. {
  2980. struct drm_i915_private *dev_priv = dev->dev_private;
  2981. if (!dev_priv)
  2982. return;
  2983. gen8_irq_reset(dev);
  2984. }
  2985. static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
  2986. {
  2987. /* Interrupt setup is already guaranteed to be single-threaded, this is
  2988. * just to make the assert_spin_locked check happy. */
  2989. spin_lock_irq(&dev_priv->irq_lock);
  2990. if (dev_priv->display_irqs_enabled)
  2991. valleyview_display_irqs_uninstall(dev_priv);
  2992. spin_unlock_irq(&dev_priv->irq_lock);
  2993. vlv_display_irq_reset(dev_priv);
  2994. dev_priv->irq_mask = ~0;
  2995. }
  2996. static void valleyview_irq_uninstall(struct drm_device *dev)
  2997. {
  2998. struct drm_i915_private *dev_priv = dev->dev_private;
  2999. if (!dev_priv)
  3000. return;
  3001. I915_WRITE(VLV_MASTER_IER, 0);
  3002. gen5_gt_irq_reset(dev);
  3003. I915_WRITE(HWSTAM, 0xffffffff);
  3004. vlv_display_irq_uninstall(dev_priv);
  3005. }
  3006. static void cherryview_irq_uninstall(struct drm_device *dev)
  3007. {
  3008. struct drm_i915_private *dev_priv = dev->dev_private;
  3009. if (!dev_priv)
  3010. return;
  3011. I915_WRITE(GEN8_MASTER_IRQ, 0);
  3012. POSTING_READ(GEN8_MASTER_IRQ);
  3013. gen8_gt_irq_reset(dev_priv);
  3014. GEN5_IRQ_RESET(GEN8_PCU_);
  3015. vlv_display_irq_uninstall(dev_priv);
  3016. }
  3017. static void ironlake_irq_uninstall(struct drm_device *dev)
  3018. {
  3019. struct drm_i915_private *dev_priv = dev->dev_private;
  3020. if (!dev_priv)
  3021. return;
  3022. ironlake_irq_reset(dev);
  3023. }
  3024. static void i8xx_irq_preinstall(struct drm_device * dev)
  3025. {
  3026. struct drm_i915_private *dev_priv = dev->dev_private;
  3027. int pipe;
  3028. for_each_pipe(dev_priv, pipe)
  3029. I915_WRITE(PIPESTAT(pipe), 0);
  3030. I915_WRITE16(IMR, 0xffff);
  3031. I915_WRITE16(IER, 0x0);
  3032. POSTING_READ16(IER);
  3033. }
  3034. static int i8xx_irq_postinstall(struct drm_device *dev)
  3035. {
  3036. struct drm_i915_private *dev_priv = dev->dev_private;
  3037. I915_WRITE16(EMR,
  3038. ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
  3039. /* Unmask the interrupts that we always want on. */
  3040. dev_priv->irq_mask =
  3041. ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  3042. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  3043. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  3044. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
  3045. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  3046. I915_WRITE16(IMR, dev_priv->irq_mask);
  3047. I915_WRITE16(IER,
  3048. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  3049. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  3050. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
  3051. I915_USER_INTERRUPT);
  3052. POSTING_READ16(IER);
  3053. /* Interrupt setup is already guaranteed to be single-threaded, this is
  3054. * just to make the assert_spin_locked check happy. */
  3055. spin_lock_irq(&dev_priv->irq_lock);
  3056. i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
  3057. i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
  3058. spin_unlock_irq(&dev_priv->irq_lock);
  3059. return 0;
  3060. }
  3061. /*
  3062. * Returns true when a page flip has completed.
  3063. */
  3064. static bool i8xx_handle_vblank(struct drm_device *dev,
  3065. int plane, int pipe, u32 iir)
  3066. {
  3067. struct drm_i915_private *dev_priv = dev->dev_private;
  3068. u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
  3069. if (!intel_pipe_handle_vblank(dev, pipe))
  3070. return false;
  3071. if ((iir & flip_pending) == 0)
  3072. goto check_page_flip;
  3073. /* We detect FlipDone by looking for the change in PendingFlip from '1'
  3074. * to '0' on the following vblank, i.e. IIR has the Pendingflip
  3075. * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
  3076. * the flip is completed (no longer pending). Since this doesn't raise
  3077. * an interrupt per se, we watch for the change at vblank.
  3078. */
  3079. if (I915_READ16(ISR) & flip_pending)
  3080. goto check_page_flip;
  3081. intel_prepare_page_flip(dev, plane);
  3082. intel_finish_page_flip(dev, pipe);
  3083. return true;
  3084. check_page_flip:
  3085. intel_check_page_flip(dev, pipe);
  3086. return false;
  3087. }
  3088. static irqreturn_t i8xx_irq_handler(int irq, void *arg)
  3089. {
  3090. struct drm_device *dev = arg;
  3091. struct drm_i915_private *dev_priv = dev->dev_private;
  3092. u16 iir, new_iir;
  3093. u32 pipe_stats[2];
  3094. int pipe;
  3095. u16 flip_mask =
  3096. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  3097. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
  3098. if (!intel_irqs_enabled(dev_priv))
  3099. return IRQ_NONE;
  3100. iir = I915_READ16(IIR);
  3101. if (iir == 0)
  3102. return IRQ_NONE;
  3103. while (iir & ~flip_mask) {
  3104. /* Can't rely on pipestat interrupt bit in iir as it might
  3105. * have been cleared after the pipestat interrupt was received.
  3106. * It doesn't set the bit in iir again, but it still produces
  3107. * interrupts (for non-MSI).
  3108. */
  3109. spin_lock(&dev_priv->irq_lock);
  3110. if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
  3111. DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
  3112. for_each_pipe(dev_priv, pipe) {
  3113. int reg = PIPESTAT(pipe);
  3114. pipe_stats[pipe] = I915_READ(reg);
  3115. /*
  3116. * Clear the PIPE*STAT regs before the IIR
  3117. */
  3118. if (pipe_stats[pipe] & 0x8000ffff)
  3119. I915_WRITE(reg, pipe_stats[pipe]);
  3120. }
  3121. spin_unlock(&dev_priv->irq_lock);
  3122. I915_WRITE16(IIR, iir & ~flip_mask);
  3123. new_iir = I915_READ16(IIR); /* Flush posted writes */
  3124. if (iir & I915_USER_INTERRUPT)
  3125. notify_ring(dev, &dev_priv->ring[RCS]);
  3126. for_each_pipe(dev_priv, pipe) {
  3127. int plane = pipe;
  3128. if (HAS_FBC(dev))
  3129. plane = !plane;
  3130. if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
  3131. i8xx_handle_vblank(dev, plane, pipe, iir))
  3132. flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
  3133. if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
  3134. i9xx_pipe_crc_irq_handler(dev, pipe);
  3135. if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
  3136. intel_cpu_fifo_underrun_irq_handler(dev_priv,
  3137. pipe);
  3138. }
  3139. iir = new_iir;
  3140. }
  3141. return IRQ_HANDLED;
  3142. }
  3143. static void i8xx_irq_uninstall(struct drm_device * dev)
  3144. {
  3145. struct drm_i915_private *dev_priv = dev->dev_private;
  3146. int pipe;
  3147. for_each_pipe(dev_priv, pipe) {
  3148. /* Clear enable bits; then clear status bits */
  3149. I915_WRITE(PIPESTAT(pipe), 0);
  3150. I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
  3151. }
  3152. I915_WRITE16(IMR, 0xffff);
  3153. I915_WRITE16(IER, 0x0);
  3154. I915_WRITE16(IIR, I915_READ16(IIR));
  3155. }
  3156. static void i915_irq_preinstall(struct drm_device * dev)
  3157. {
  3158. struct drm_i915_private *dev_priv = dev->dev_private;
  3159. int pipe;
  3160. if (I915_HAS_HOTPLUG(dev)) {
  3161. I915_WRITE(PORT_HOTPLUG_EN, 0);
  3162. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  3163. }
  3164. I915_WRITE16(HWSTAM, 0xeffe);
  3165. for_each_pipe(dev_priv, pipe)
  3166. I915_WRITE(PIPESTAT(pipe), 0);
  3167. I915_WRITE(IMR, 0xffffffff);
  3168. I915_WRITE(IER, 0x0);
  3169. POSTING_READ(IER);
  3170. }
  3171. static int i915_irq_postinstall(struct drm_device *dev)
  3172. {
  3173. struct drm_i915_private *dev_priv = dev->dev_private;
  3174. u32 enable_mask;
  3175. I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
  3176. /* Unmask the interrupts that we always want on. */
  3177. dev_priv->irq_mask =
  3178. ~(I915_ASLE_INTERRUPT |
  3179. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  3180. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  3181. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  3182. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
  3183. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  3184. enable_mask =
  3185. I915_ASLE_INTERRUPT |
  3186. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  3187. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  3188. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
  3189. I915_USER_INTERRUPT;
  3190. if (I915_HAS_HOTPLUG(dev)) {
  3191. I915_WRITE(PORT_HOTPLUG_EN, 0);
  3192. POSTING_READ(PORT_HOTPLUG_EN);
  3193. /* Enable in IER... */
  3194. enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
  3195. /* and unmask in IMR */
  3196. dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
  3197. }
  3198. I915_WRITE(IMR, dev_priv->irq_mask);
  3199. I915_WRITE(IER, enable_mask);
  3200. POSTING_READ(IER);
  3201. i915_enable_asle_pipestat(dev);
  3202. /* Interrupt setup is already guaranteed to be single-threaded, this is
  3203. * just to make the assert_spin_locked check happy. */
  3204. spin_lock_irq(&dev_priv->irq_lock);
  3205. i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
  3206. i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
  3207. spin_unlock_irq(&dev_priv->irq_lock);
  3208. return 0;
  3209. }
  3210. /*
  3211. * Returns true when a page flip has completed.
  3212. */
  3213. static bool i915_handle_vblank(struct drm_device *dev,
  3214. int plane, int pipe, u32 iir)
  3215. {
  3216. struct drm_i915_private *dev_priv = dev->dev_private;
  3217. u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
  3218. if (!intel_pipe_handle_vblank(dev, pipe))
  3219. return false;
  3220. if ((iir & flip_pending) == 0)
  3221. goto check_page_flip;
  3222. /* We detect FlipDone by looking for the change in PendingFlip from '1'
  3223. * to '0' on the following vblank, i.e. IIR has the Pendingflip
  3224. * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
  3225. * the flip is completed (no longer pending). Since this doesn't raise
  3226. * an interrupt per se, we watch for the change at vblank.
  3227. */
  3228. if (I915_READ(ISR) & flip_pending)
  3229. goto check_page_flip;
  3230. intel_prepare_page_flip(dev, plane);
  3231. intel_finish_page_flip(dev, pipe);
  3232. return true;
  3233. check_page_flip:
  3234. intel_check_page_flip(dev, pipe);
  3235. return false;
  3236. }
  3237. static irqreturn_t i915_irq_handler(int irq, void *arg)
  3238. {
  3239. struct drm_device *dev = arg;
  3240. struct drm_i915_private *dev_priv = dev->dev_private;
  3241. u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
  3242. u32 flip_mask =
  3243. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  3244. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
  3245. int pipe, ret = IRQ_NONE;
  3246. if (!intel_irqs_enabled(dev_priv))
  3247. return IRQ_NONE;
  3248. iir = I915_READ(IIR);
  3249. do {
  3250. bool irq_received = (iir & ~flip_mask) != 0;
  3251. bool blc_event = false;
  3252. /* Can't rely on pipestat interrupt bit in iir as it might
  3253. * have been cleared after the pipestat interrupt was received.
  3254. * It doesn't set the bit in iir again, but it still produces
  3255. * interrupts (for non-MSI).
  3256. */
  3257. spin_lock(&dev_priv->irq_lock);
  3258. if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
  3259. DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
  3260. for_each_pipe(dev_priv, pipe) {
  3261. int reg = PIPESTAT(pipe);
  3262. pipe_stats[pipe] = I915_READ(reg);
  3263. /* Clear the PIPE*STAT regs before the IIR */
  3264. if (pipe_stats[pipe] & 0x8000ffff) {
  3265. I915_WRITE(reg, pipe_stats[pipe]);
  3266. irq_received = true;
  3267. }
  3268. }
  3269. spin_unlock(&dev_priv->irq_lock);
  3270. if (!irq_received)
  3271. break;
  3272. /* Consume port. Then clear IIR or we'll miss events */
  3273. if (I915_HAS_HOTPLUG(dev) &&
  3274. iir & I915_DISPLAY_PORT_INTERRUPT)
  3275. i9xx_hpd_irq_handler(dev);
  3276. I915_WRITE(IIR, iir & ~flip_mask);
  3277. new_iir = I915_READ(IIR); /* Flush posted writes */
  3278. if (iir & I915_USER_INTERRUPT)
  3279. notify_ring(dev, &dev_priv->ring[RCS]);
  3280. for_each_pipe(dev_priv, pipe) {
  3281. int plane = pipe;
  3282. if (HAS_FBC(dev))
  3283. plane = !plane;
  3284. if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
  3285. i915_handle_vblank(dev, plane, pipe, iir))
  3286. flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
  3287. if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
  3288. blc_event = true;
  3289. if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
  3290. i9xx_pipe_crc_irq_handler(dev, pipe);
  3291. if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
  3292. intel_cpu_fifo_underrun_irq_handler(dev_priv,
  3293. pipe);
  3294. }
  3295. if (blc_event || (iir & I915_ASLE_INTERRUPT))
  3296. intel_opregion_asle_intr(dev);
  3297. /* With MSI, interrupts are only generated when iir
  3298. * transitions from zero to nonzero. If another bit got
  3299. * set while we were handling the existing iir bits, then
  3300. * we would never get another interrupt.
  3301. *
  3302. * This is fine on non-MSI as well, as if we hit this path
  3303. * we avoid exiting the interrupt handler only to generate
  3304. * another one.
  3305. *
  3306. * Note that for MSI this could cause a stray interrupt report
  3307. * if an interrupt landed in the time between writing IIR and
  3308. * the posting read. This should be rare enough to never
  3309. * trigger the 99% of 100,000 interrupts test for disabling
  3310. * stray interrupts.
  3311. */
  3312. ret = IRQ_HANDLED;
  3313. iir = new_iir;
  3314. } while (iir & ~flip_mask);
  3315. return ret;
  3316. }
  3317. static void i915_irq_uninstall(struct drm_device * dev)
  3318. {
  3319. struct drm_i915_private *dev_priv = dev->dev_private;
  3320. int pipe;
  3321. if (I915_HAS_HOTPLUG(dev)) {
  3322. I915_WRITE(PORT_HOTPLUG_EN, 0);
  3323. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  3324. }
  3325. I915_WRITE16(HWSTAM, 0xffff);
  3326. for_each_pipe(dev_priv, pipe) {
  3327. /* Clear enable bits; then clear status bits */
  3328. I915_WRITE(PIPESTAT(pipe), 0);
  3329. I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
  3330. }
  3331. I915_WRITE(IMR, 0xffffffff);
  3332. I915_WRITE(IER, 0x0);
  3333. I915_WRITE(IIR, I915_READ(IIR));
  3334. }
  3335. static void i965_irq_preinstall(struct drm_device * dev)
  3336. {
  3337. struct drm_i915_private *dev_priv = dev->dev_private;
  3338. int pipe;
  3339. I915_WRITE(PORT_HOTPLUG_EN, 0);
  3340. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  3341. I915_WRITE(HWSTAM, 0xeffe);
  3342. for_each_pipe(dev_priv, pipe)
  3343. I915_WRITE(PIPESTAT(pipe), 0);
  3344. I915_WRITE(IMR, 0xffffffff);
  3345. I915_WRITE(IER, 0x0);
  3346. POSTING_READ(IER);
  3347. }
  3348. static int i965_irq_postinstall(struct drm_device *dev)
  3349. {
  3350. struct drm_i915_private *dev_priv = dev->dev_private;
  3351. u32 enable_mask;
  3352. u32 error_mask;
  3353. /* Unmask the interrupts that we always want on. */
  3354. dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
  3355. I915_DISPLAY_PORT_INTERRUPT |
  3356. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  3357. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  3358. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  3359. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
  3360. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  3361. enable_mask = ~dev_priv->irq_mask;
  3362. enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  3363. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
  3364. enable_mask |= I915_USER_INTERRUPT;
  3365. if (IS_G4X(dev))
  3366. enable_mask |= I915_BSD_USER_INTERRUPT;
  3367. /* Interrupt setup is already guaranteed to be single-threaded, this is
  3368. * just to make the assert_spin_locked check happy. */
  3369. spin_lock_irq(&dev_priv->irq_lock);
  3370. i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
  3371. i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
  3372. i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
  3373. spin_unlock_irq(&dev_priv->irq_lock);
  3374. /*
  3375. * Enable some error detection, note the instruction error mask
  3376. * bit is reserved, so we leave it masked.
  3377. */
  3378. if (IS_G4X(dev)) {
  3379. error_mask = ~(GM45_ERROR_PAGE_TABLE |
  3380. GM45_ERROR_MEM_PRIV |
  3381. GM45_ERROR_CP_PRIV |
  3382. I915_ERROR_MEMORY_REFRESH);
  3383. } else {
  3384. error_mask = ~(I915_ERROR_PAGE_TABLE |
  3385. I915_ERROR_MEMORY_REFRESH);
  3386. }
  3387. I915_WRITE(EMR, error_mask);
  3388. I915_WRITE(IMR, dev_priv->irq_mask);
  3389. I915_WRITE(IER, enable_mask);
  3390. POSTING_READ(IER);
  3391. I915_WRITE(PORT_HOTPLUG_EN, 0);
  3392. POSTING_READ(PORT_HOTPLUG_EN);
  3393. i915_enable_asle_pipestat(dev);
  3394. return 0;
  3395. }
  3396. static void i915_hpd_irq_setup(struct drm_device *dev)
  3397. {
  3398. struct drm_i915_private *dev_priv = dev->dev_private;
  3399. struct intel_encoder *intel_encoder;
  3400. u32 hotplug_en;
  3401. assert_spin_locked(&dev_priv->irq_lock);
  3402. hotplug_en = I915_READ(PORT_HOTPLUG_EN);
  3403. hotplug_en &= ~HOTPLUG_INT_EN_MASK;
  3404. /* Note HDMI and DP share hotplug bits */
  3405. /* enable bits are the same for all generations */
  3406. for_each_intel_encoder(dev, intel_encoder)
  3407. if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
  3408. hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
  3409. /* Programming the CRT detection parameters tends
  3410. to generate a spurious hotplug event about three
  3411. seconds later. So just do it once.
  3412. */
  3413. if (IS_G4X(dev))
  3414. hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
  3415. hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
  3416. hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
  3417. /* Ignore TV since it's buggy */
  3418. I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
  3419. }
  3420. static irqreturn_t i965_irq_handler(int irq, void *arg)
  3421. {
  3422. struct drm_device *dev = arg;
  3423. struct drm_i915_private *dev_priv = dev->dev_private;
  3424. u32 iir, new_iir;
  3425. u32 pipe_stats[I915_MAX_PIPES];
  3426. int ret = IRQ_NONE, pipe;
  3427. u32 flip_mask =
  3428. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  3429. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
  3430. if (!intel_irqs_enabled(dev_priv))
  3431. return IRQ_NONE;
  3432. iir = I915_READ(IIR);
  3433. for (;;) {
  3434. bool irq_received = (iir & ~flip_mask) != 0;
  3435. bool blc_event = false;
  3436. /* Can't rely on pipestat interrupt bit in iir as it might
  3437. * have been cleared after the pipestat interrupt was received.
  3438. * It doesn't set the bit in iir again, but it still produces
  3439. * interrupts (for non-MSI).
  3440. */
  3441. spin_lock(&dev_priv->irq_lock);
  3442. if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
  3443. DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
  3444. for_each_pipe(dev_priv, pipe) {
  3445. int reg = PIPESTAT(pipe);
  3446. pipe_stats[pipe] = I915_READ(reg);
  3447. /*
  3448. * Clear the PIPE*STAT regs before the IIR
  3449. */
  3450. if (pipe_stats[pipe] & 0x8000ffff) {
  3451. I915_WRITE(reg, pipe_stats[pipe]);
  3452. irq_received = true;
  3453. }
  3454. }
  3455. spin_unlock(&dev_priv->irq_lock);
  3456. if (!irq_received)
  3457. break;
  3458. ret = IRQ_HANDLED;
  3459. /* Consume port. Then clear IIR or we'll miss events */
  3460. if (iir & I915_DISPLAY_PORT_INTERRUPT)
  3461. i9xx_hpd_irq_handler(dev);
  3462. I915_WRITE(IIR, iir & ~flip_mask);
  3463. new_iir = I915_READ(IIR); /* Flush posted writes */
  3464. if (iir & I915_USER_INTERRUPT)
  3465. notify_ring(dev, &dev_priv->ring[RCS]);
  3466. if (iir & I915_BSD_USER_INTERRUPT)
  3467. notify_ring(dev, &dev_priv->ring[VCS]);
  3468. for_each_pipe(dev_priv, pipe) {
  3469. if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
  3470. i915_handle_vblank(dev, pipe, pipe, iir))
  3471. flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
  3472. if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
  3473. blc_event = true;
  3474. if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
  3475. i9xx_pipe_crc_irq_handler(dev, pipe);
  3476. if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
  3477. intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
  3478. }
  3479. if (blc_event || (iir & I915_ASLE_INTERRUPT))
  3480. intel_opregion_asle_intr(dev);
  3481. if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
  3482. gmbus_irq_handler(dev);
  3483. /* With MSI, interrupts are only generated when iir
  3484. * transitions from zero to nonzero. If another bit got
  3485. * set while we were handling the existing iir bits, then
  3486. * we would never get another interrupt.
  3487. *
  3488. * This is fine on non-MSI as well, as if we hit this path
  3489. * we avoid exiting the interrupt handler only to generate
  3490. * another one.
  3491. *
  3492. * Note that for MSI this could cause a stray interrupt report
  3493. * if an interrupt landed in the time between writing IIR and
  3494. * the posting read. This should be rare enough to never
  3495. * trigger the 99% of 100,000 interrupts test for disabling
  3496. * stray interrupts.
  3497. */
  3498. iir = new_iir;
  3499. }
  3500. return ret;
  3501. }
  3502. static void i965_irq_uninstall(struct drm_device * dev)
  3503. {
  3504. struct drm_i915_private *dev_priv = dev->dev_private;
  3505. int pipe;
  3506. if (!dev_priv)
  3507. return;
  3508. I915_WRITE(PORT_HOTPLUG_EN, 0);
  3509. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  3510. I915_WRITE(HWSTAM, 0xffffffff);
  3511. for_each_pipe(dev_priv, pipe)
  3512. I915_WRITE(PIPESTAT(pipe), 0);
  3513. I915_WRITE(IMR, 0xffffffff);
  3514. I915_WRITE(IER, 0x0);
  3515. for_each_pipe(dev_priv, pipe)
  3516. I915_WRITE(PIPESTAT(pipe),
  3517. I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
  3518. I915_WRITE(IIR, I915_READ(IIR));
  3519. }
  3520. static void intel_hpd_irq_reenable_work(struct work_struct *work)
  3521. {
  3522. struct drm_i915_private *dev_priv =
  3523. container_of(work, typeof(*dev_priv),
  3524. hotplug_reenable_work.work);
  3525. struct drm_device *dev = dev_priv->dev;
  3526. struct drm_mode_config *mode_config = &dev->mode_config;
  3527. int i;
  3528. intel_runtime_pm_get(dev_priv);
  3529. spin_lock_irq(&dev_priv->irq_lock);
  3530. for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
  3531. struct drm_connector *connector;
  3532. if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
  3533. continue;
  3534. dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
  3535. list_for_each_entry(connector, &mode_config->connector_list, head) {
  3536. struct intel_connector *intel_connector = to_intel_connector(connector);
  3537. if (intel_connector->encoder->hpd_pin == i) {
  3538. if (connector->polled != intel_connector->polled)
  3539. DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
  3540. connector->name);
  3541. connector->polled = intel_connector->polled;
  3542. if (!connector->polled)
  3543. connector->polled = DRM_CONNECTOR_POLL_HPD;
  3544. }
  3545. }
  3546. }
  3547. if (dev_priv->display.hpd_irq_setup)
  3548. dev_priv->display.hpd_irq_setup(dev);
  3549. spin_unlock_irq(&dev_priv->irq_lock);
  3550. intel_runtime_pm_put(dev_priv);
  3551. }
  3552. /**
  3553. * intel_irq_init - initializes irq support
  3554. * @dev_priv: i915 device instance
  3555. *
  3556. * This function initializes all the irq support including work items, timers
  3557. * and all the vtables. It does not setup the interrupt itself though.
  3558. */
  3559. void intel_irq_init(struct drm_i915_private *dev_priv)
  3560. {
  3561. struct drm_device *dev = dev_priv->dev;
  3562. INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
  3563. INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
  3564. INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
  3565. INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
  3566. /* Let's track the enabled rps events */
  3567. if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
  3568. /* WaGsvRC0ResidencyMethod:vlv */
  3569. dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
  3570. else
  3571. dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
  3572. INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
  3573. i915_hangcheck_elapsed);
  3574. INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
  3575. intel_hpd_irq_reenable_work);
  3576. pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
  3577. if (IS_GEN2(dev_priv)) {
  3578. dev->max_vblank_count = 0;
  3579. dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
  3580. } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
  3581. dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
  3582. dev->driver->get_vblank_counter = gm45_get_vblank_counter;
  3583. } else {
  3584. dev->driver->get_vblank_counter = i915_get_vblank_counter;
  3585. dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
  3586. }
  3587. /*
  3588. * Opt out of the vblank disable timer on everything except gen2.
  3589. * Gen2 doesn't have a hardware frame counter and so depends on
  3590. * vblank interrupts to produce sane vblank seuquence numbers.
  3591. */
  3592. if (!IS_GEN2(dev_priv))
  3593. dev->vblank_disable_immediate = true;
  3594. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  3595. dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
  3596. dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
  3597. }
  3598. if (IS_CHERRYVIEW(dev_priv)) {
  3599. dev->driver->irq_handler = cherryview_irq_handler;
  3600. dev->driver->irq_preinstall = cherryview_irq_preinstall;
  3601. dev->driver->irq_postinstall = cherryview_irq_postinstall;
  3602. dev->driver->irq_uninstall = cherryview_irq_uninstall;
  3603. dev->driver->enable_vblank = valleyview_enable_vblank;
  3604. dev->driver->disable_vblank = valleyview_disable_vblank;
  3605. dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
  3606. } else if (IS_VALLEYVIEW(dev_priv)) {
  3607. dev->driver->irq_handler = valleyview_irq_handler;
  3608. dev->driver->irq_preinstall = valleyview_irq_preinstall;
  3609. dev->driver->irq_postinstall = valleyview_irq_postinstall;
  3610. dev->driver->irq_uninstall = valleyview_irq_uninstall;
  3611. dev->driver->enable_vblank = valleyview_enable_vblank;
  3612. dev->driver->disable_vblank = valleyview_disable_vblank;
  3613. dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
  3614. } else if (INTEL_INFO(dev_priv)->gen >= 8) {
  3615. dev->driver->irq_handler = gen8_irq_handler;
  3616. dev->driver->irq_preinstall = gen8_irq_reset;
  3617. dev->driver->irq_postinstall = gen8_irq_postinstall;
  3618. dev->driver->irq_uninstall = gen8_irq_uninstall;
  3619. dev->driver->enable_vblank = gen8_enable_vblank;
  3620. dev->driver->disable_vblank = gen8_disable_vblank;
  3621. dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
  3622. } else if (HAS_PCH_SPLIT(dev)) {
  3623. dev->driver->irq_handler = ironlake_irq_handler;
  3624. dev->driver->irq_preinstall = ironlake_irq_reset;
  3625. dev->driver->irq_postinstall = ironlake_irq_postinstall;
  3626. dev->driver->irq_uninstall = ironlake_irq_uninstall;
  3627. dev->driver->enable_vblank = ironlake_enable_vblank;
  3628. dev->driver->disable_vblank = ironlake_disable_vblank;
  3629. dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
  3630. } else {
  3631. if (INTEL_INFO(dev_priv)->gen == 2) {
  3632. dev->driver->irq_preinstall = i8xx_irq_preinstall;
  3633. dev->driver->irq_postinstall = i8xx_irq_postinstall;
  3634. dev->driver->irq_handler = i8xx_irq_handler;
  3635. dev->driver->irq_uninstall = i8xx_irq_uninstall;
  3636. } else if (INTEL_INFO(dev_priv)->gen == 3) {
  3637. dev->driver->irq_preinstall = i915_irq_preinstall;
  3638. dev->driver->irq_postinstall = i915_irq_postinstall;
  3639. dev->driver->irq_uninstall = i915_irq_uninstall;
  3640. dev->driver->irq_handler = i915_irq_handler;
  3641. } else {
  3642. dev->driver->irq_preinstall = i965_irq_preinstall;
  3643. dev->driver->irq_postinstall = i965_irq_postinstall;
  3644. dev->driver->irq_uninstall = i965_irq_uninstall;
  3645. dev->driver->irq_handler = i965_irq_handler;
  3646. }
  3647. if (I915_HAS_HOTPLUG(dev_priv))
  3648. dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
  3649. dev->driver->enable_vblank = i915_enable_vblank;
  3650. dev->driver->disable_vblank = i915_disable_vblank;
  3651. }
  3652. }
  3653. /**
  3654. * intel_hpd_init - initializes and enables hpd support
  3655. * @dev_priv: i915 device instance
  3656. *
  3657. * This function enables the hotplug support. It requires that interrupts have
  3658. * already been enabled with intel_irq_init_hw(). From this point on hotplug and
  3659. * poll request can run concurrently to other code, so locking rules must be
  3660. * obeyed.
  3661. *
  3662. * This is a separate step from interrupt enabling to simplify the locking rules
  3663. * in the driver load and resume code.
  3664. */
  3665. void intel_hpd_init(struct drm_i915_private *dev_priv)
  3666. {
  3667. struct drm_device *dev = dev_priv->dev;
  3668. struct drm_mode_config *mode_config = &dev->mode_config;
  3669. struct drm_connector *connector;
  3670. int i;
  3671. for (i = 1; i < HPD_NUM_PINS; i++) {
  3672. dev_priv->hpd_stats[i].hpd_cnt = 0;
  3673. dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
  3674. }
  3675. list_for_each_entry(connector, &mode_config->connector_list, head) {
  3676. struct intel_connector *intel_connector = to_intel_connector(connector);
  3677. connector->polled = intel_connector->polled;
  3678. if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
  3679. connector->polled = DRM_CONNECTOR_POLL_HPD;
  3680. if (intel_connector->mst_port)
  3681. connector->polled = DRM_CONNECTOR_POLL_HPD;
  3682. }
  3683. /* Interrupt setup is already guaranteed to be single-threaded, this is
  3684. * just to make the assert_spin_locked checks happy. */
  3685. spin_lock_irq(&dev_priv->irq_lock);
  3686. if (dev_priv->display.hpd_irq_setup)
  3687. dev_priv->display.hpd_irq_setup(dev);
  3688. spin_unlock_irq(&dev_priv->irq_lock);
  3689. }
  3690. /**
  3691. * intel_irq_install - enables the hardware interrupt
  3692. * @dev_priv: i915 device instance
  3693. *
  3694. * This function enables the hardware interrupt handling, but leaves the hotplug
  3695. * handling still disabled. It is called after intel_irq_init().
  3696. *
  3697. * In the driver load and resume code we need working interrupts in a few places
  3698. * but don't want to deal with the hassle of concurrent probe and hotplug
  3699. * workers. Hence the split into this two-stage approach.
  3700. */
  3701. int intel_irq_install(struct drm_i915_private *dev_priv)
  3702. {
  3703. /*
  3704. * We enable some interrupt sources in our postinstall hooks, so mark
  3705. * interrupts as enabled _before_ actually enabling them to avoid
  3706. * special cases in our ordering checks.
  3707. */
  3708. dev_priv->pm.irqs_enabled = true;
  3709. return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
  3710. }
  3711. /**
  3712. * intel_irq_uninstall - finilizes all irq handling
  3713. * @dev_priv: i915 device instance
  3714. *
  3715. * This stops interrupt and hotplug handling and unregisters and frees all
  3716. * resources acquired in the init functions.
  3717. */
  3718. void intel_irq_uninstall(struct drm_i915_private *dev_priv)
  3719. {
  3720. drm_irq_uninstall(dev_priv->dev);
  3721. intel_hpd_cancel_work(dev_priv);
  3722. dev_priv->pm.irqs_enabled = false;
  3723. }
  3724. /**
  3725. * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
  3726. * @dev_priv: i915 device instance
  3727. *
  3728. * This function is used to disable interrupts at runtime, both in the runtime
  3729. * pm and the system suspend/resume code.
  3730. */
  3731. void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
  3732. {
  3733. dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
  3734. dev_priv->pm.irqs_enabled = false;
  3735. synchronize_irq(dev_priv->dev->irq);
  3736. }
  3737. /**
  3738. * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
  3739. * @dev_priv: i915 device instance
  3740. *
  3741. * This function is used to enable interrupts at runtime, both in the runtime
  3742. * pm and the system suspend/resume code.
  3743. */
  3744. void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
  3745. {
  3746. dev_priv->pm.irqs_enabled = true;
  3747. dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
  3748. dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
  3749. }