i915_irq.c 130 KB

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