i915_irq.c 131 KB

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