i915_irq.c 131 KB

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