i915_irq.c 131 KB

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