i915_irq.c 126 KB

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