i915_irq.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520
  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 <drm/drmP.h>
  32. #include <drm/i915_drm.h>
  33. #include "i915_drv.h"
  34. #include "i915_trace.h"
  35. #include "intel_drv.h"
  36. static const u32 hpd_ibx[] = {
  37. [HPD_CRT] = SDE_CRT_HOTPLUG,
  38. [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
  39. [HPD_PORT_B] = SDE_PORTB_HOTPLUG,
  40. [HPD_PORT_C] = SDE_PORTC_HOTPLUG,
  41. [HPD_PORT_D] = SDE_PORTD_HOTPLUG
  42. };
  43. static const u32 hpd_cpt[] = {
  44. [HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
  45. [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
  46. [HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
  47. [HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
  48. [HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
  49. };
  50. static const u32 hpd_mask_i915[] = {
  51. [HPD_CRT] = CRT_HOTPLUG_INT_EN,
  52. [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
  53. [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
  54. [HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
  55. [HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
  56. [HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
  57. };
  58. static const u32 hpd_status_gen4[] = {
  59. [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
  60. [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
  61. [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
  62. [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
  63. [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
  64. [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
  65. };
  66. static const u32 hpd_status_i965[] = {
  67. [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
  68. [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I965,
  69. [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I965,
  70. [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
  71. [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
  72. [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
  73. };
  74. static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
  75. [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
  76. [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
  77. [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
  78. [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
  79. [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
  80. [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
  81. };
  82. static void ibx_hpd_irq_setup(struct drm_device *dev);
  83. static void i915_hpd_irq_setup(struct drm_device *dev);
  84. /* For display hotplug interrupt */
  85. static void
  86. ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
  87. {
  88. if ((dev_priv->irq_mask & mask) != 0) {
  89. dev_priv->irq_mask &= ~mask;
  90. I915_WRITE(DEIMR, dev_priv->irq_mask);
  91. POSTING_READ(DEIMR);
  92. }
  93. }
  94. static void
  95. ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
  96. {
  97. if ((dev_priv->irq_mask & mask) != mask) {
  98. dev_priv->irq_mask |= mask;
  99. I915_WRITE(DEIMR, dev_priv->irq_mask);
  100. POSTING_READ(DEIMR);
  101. }
  102. }
  103. static bool ivb_can_enable_err_int(struct drm_device *dev)
  104. {
  105. struct drm_i915_private *dev_priv = dev->dev_private;
  106. struct intel_crtc *crtc;
  107. enum pipe pipe;
  108. for_each_pipe(pipe) {
  109. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  110. if (crtc->cpu_fifo_underrun_disabled)
  111. return false;
  112. }
  113. return true;
  114. }
  115. static bool cpt_can_enable_serr_int(struct drm_device *dev)
  116. {
  117. struct drm_i915_private *dev_priv = dev->dev_private;
  118. enum pipe pipe;
  119. struct intel_crtc *crtc;
  120. for_each_pipe(pipe) {
  121. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  122. if (crtc->pch_fifo_underrun_disabled)
  123. return false;
  124. }
  125. return true;
  126. }
  127. static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
  128. enum pipe pipe, bool enable)
  129. {
  130. struct drm_i915_private *dev_priv = dev->dev_private;
  131. uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
  132. DE_PIPEB_FIFO_UNDERRUN;
  133. if (enable)
  134. ironlake_enable_display_irq(dev_priv, bit);
  135. else
  136. ironlake_disable_display_irq(dev_priv, bit);
  137. }
  138. static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
  139. bool enable)
  140. {
  141. struct drm_i915_private *dev_priv = dev->dev_private;
  142. if (enable) {
  143. if (!ivb_can_enable_err_int(dev))
  144. return;
  145. I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN_A |
  146. ERR_INT_FIFO_UNDERRUN_B |
  147. ERR_INT_FIFO_UNDERRUN_C);
  148. ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
  149. } else {
  150. ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
  151. }
  152. }
  153. static void ibx_set_fifo_underrun_reporting(struct intel_crtc *crtc,
  154. bool enable)
  155. {
  156. struct drm_device *dev = crtc->base.dev;
  157. struct drm_i915_private *dev_priv = dev->dev_private;
  158. uint32_t bit = (crtc->pipe == PIPE_A) ? SDE_TRANSA_FIFO_UNDER :
  159. SDE_TRANSB_FIFO_UNDER;
  160. if (enable)
  161. I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~bit);
  162. else
  163. I915_WRITE(SDEIMR, I915_READ(SDEIMR) | bit);
  164. POSTING_READ(SDEIMR);
  165. }
  166. static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
  167. enum transcoder pch_transcoder,
  168. bool enable)
  169. {
  170. struct drm_i915_private *dev_priv = dev->dev_private;
  171. if (enable) {
  172. if (!cpt_can_enable_serr_int(dev))
  173. return;
  174. I915_WRITE(SERR_INT, SERR_INT_TRANS_A_FIFO_UNDERRUN |
  175. SERR_INT_TRANS_B_FIFO_UNDERRUN |
  176. SERR_INT_TRANS_C_FIFO_UNDERRUN);
  177. I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~SDE_ERROR_CPT);
  178. } else {
  179. I915_WRITE(SDEIMR, I915_READ(SDEIMR) | SDE_ERROR_CPT);
  180. }
  181. POSTING_READ(SDEIMR);
  182. }
  183. /**
  184. * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
  185. * @dev: drm device
  186. * @pipe: pipe
  187. * @enable: true if we want to report FIFO underrun errors, false otherwise
  188. *
  189. * This function makes us disable or enable CPU fifo underruns for a specific
  190. * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
  191. * reporting for one pipe may also disable all the other CPU error interruts for
  192. * the other pipes, due to the fact that there's just one interrupt mask/enable
  193. * bit for all the pipes.
  194. *
  195. * Returns the previous state of underrun reporting.
  196. */
  197. bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
  198. enum pipe pipe, bool enable)
  199. {
  200. struct drm_i915_private *dev_priv = dev->dev_private;
  201. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  202. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  203. unsigned long flags;
  204. bool ret;
  205. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  206. ret = !intel_crtc->cpu_fifo_underrun_disabled;
  207. if (enable == ret)
  208. goto done;
  209. intel_crtc->cpu_fifo_underrun_disabled = !enable;
  210. if (IS_GEN5(dev) || IS_GEN6(dev))
  211. ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
  212. else if (IS_GEN7(dev))
  213. ivybridge_set_fifo_underrun_reporting(dev, enable);
  214. done:
  215. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  216. return ret;
  217. }
  218. /**
  219. * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
  220. * @dev: drm device
  221. * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
  222. * @enable: true if we want to report FIFO underrun errors, false otherwise
  223. *
  224. * This function makes us disable or enable PCH fifo underruns for a specific
  225. * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
  226. * underrun reporting for one transcoder may also disable all the other PCH
  227. * error interruts for the other transcoders, due to the fact that there's just
  228. * one interrupt mask/enable bit for all the transcoders.
  229. *
  230. * Returns the previous state of underrun reporting.
  231. */
  232. bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
  233. enum transcoder pch_transcoder,
  234. bool enable)
  235. {
  236. struct drm_i915_private *dev_priv = dev->dev_private;
  237. enum pipe p;
  238. struct drm_crtc *crtc;
  239. struct intel_crtc *intel_crtc;
  240. unsigned long flags;
  241. bool ret;
  242. if (HAS_PCH_LPT(dev)) {
  243. crtc = NULL;
  244. for_each_pipe(p) {
  245. struct drm_crtc *c = dev_priv->pipe_to_crtc_mapping[p];
  246. if (intel_pipe_has_type(c, INTEL_OUTPUT_ANALOG)) {
  247. crtc = c;
  248. break;
  249. }
  250. }
  251. if (!crtc) {
  252. DRM_ERROR("PCH FIFO underrun, but no CRTC using the PCH found\n");
  253. return false;
  254. }
  255. } else {
  256. crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
  257. }
  258. intel_crtc = to_intel_crtc(crtc);
  259. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  260. ret = !intel_crtc->pch_fifo_underrun_disabled;
  261. if (enable == ret)
  262. goto done;
  263. intel_crtc->pch_fifo_underrun_disabled = !enable;
  264. if (HAS_PCH_IBX(dev))
  265. ibx_set_fifo_underrun_reporting(intel_crtc, enable);
  266. else
  267. cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
  268. done:
  269. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  270. return ret;
  271. }
  272. void
  273. i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
  274. {
  275. u32 reg = PIPESTAT(pipe);
  276. u32 pipestat = I915_READ(reg) & 0x7fff0000;
  277. if ((pipestat & mask) == mask)
  278. return;
  279. /* Enable the interrupt, clear any pending status */
  280. pipestat |= mask | (mask >> 16);
  281. I915_WRITE(reg, pipestat);
  282. POSTING_READ(reg);
  283. }
  284. void
  285. i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
  286. {
  287. u32 reg = PIPESTAT(pipe);
  288. u32 pipestat = I915_READ(reg) & 0x7fff0000;
  289. if ((pipestat & mask) == 0)
  290. return;
  291. pipestat &= ~mask;
  292. I915_WRITE(reg, pipestat);
  293. POSTING_READ(reg);
  294. }
  295. /**
  296. * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
  297. */
  298. static void i915_enable_asle_pipestat(struct drm_device *dev)
  299. {
  300. drm_i915_private_t *dev_priv = dev->dev_private;
  301. unsigned long irqflags;
  302. if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
  303. return;
  304. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  305. i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
  306. if (INTEL_INFO(dev)->gen >= 4)
  307. i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
  308. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  309. }
  310. /**
  311. * i915_pipe_enabled - check if a pipe is enabled
  312. * @dev: DRM device
  313. * @pipe: pipe to check
  314. *
  315. * Reading certain registers when the pipe is disabled can hang the chip.
  316. * Use this routine to make sure the PLL is running and the pipe is active
  317. * before reading such registers if unsure.
  318. */
  319. static int
  320. i915_pipe_enabled(struct drm_device *dev, int pipe)
  321. {
  322. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  323. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  324. pipe);
  325. if (!intel_display_power_enabled(dev,
  326. POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
  327. return false;
  328. return I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE;
  329. }
  330. /* Called from drm generic code, passed a 'crtc', which
  331. * we use as a pipe index
  332. */
  333. static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
  334. {
  335. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  336. unsigned long high_frame;
  337. unsigned long low_frame;
  338. u32 high1, high2, low;
  339. if (!i915_pipe_enabled(dev, pipe)) {
  340. DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
  341. "pipe %c\n", pipe_name(pipe));
  342. return 0;
  343. }
  344. high_frame = PIPEFRAME(pipe);
  345. low_frame = PIPEFRAMEPIXEL(pipe);
  346. /*
  347. * High & low register fields aren't synchronized, so make sure
  348. * we get a low value that's stable across two reads of the high
  349. * register.
  350. */
  351. do {
  352. high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
  353. low = I915_READ(low_frame) & PIPE_FRAME_LOW_MASK;
  354. high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
  355. } while (high1 != high2);
  356. high1 >>= PIPE_FRAME_HIGH_SHIFT;
  357. low >>= PIPE_FRAME_LOW_SHIFT;
  358. return (high1 << 8) | low;
  359. }
  360. static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
  361. {
  362. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  363. int reg = PIPE_FRMCOUNT_GM45(pipe);
  364. if (!i915_pipe_enabled(dev, pipe)) {
  365. DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
  366. "pipe %c\n", pipe_name(pipe));
  367. return 0;
  368. }
  369. return I915_READ(reg);
  370. }
  371. static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
  372. int *vpos, int *hpos)
  373. {
  374. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  375. u32 vbl = 0, position = 0;
  376. int vbl_start, vbl_end, htotal, vtotal;
  377. bool in_vbl = true;
  378. int ret = 0;
  379. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  380. pipe);
  381. if (!i915_pipe_enabled(dev, pipe)) {
  382. DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
  383. "pipe %c\n", pipe_name(pipe));
  384. return 0;
  385. }
  386. /* Get vtotal. */
  387. vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
  388. if (INTEL_INFO(dev)->gen >= 4) {
  389. /* No obvious pixelcount register. Only query vertical
  390. * scanout position from Display scan line register.
  391. */
  392. position = I915_READ(PIPEDSL(pipe));
  393. /* Decode into vertical scanout position. Don't have
  394. * horizontal scanout position.
  395. */
  396. *vpos = position & 0x1fff;
  397. *hpos = 0;
  398. } else {
  399. /* Have access to pixelcount since start of frame.
  400. * We can split this into vertical and horizontal
  401. * scanout position.
  402. */
  403. position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
  404. htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
  405. *vpos = position / htotal;
  406. *hpos = position - (*vpos * htotal);
  407. }
  408. /* Query vblank area. */
  409. vbl = I915_READ(VBLANK(cpu_transcoder));
  410. /* Test position against vblank region. */
  411. vbl_start = vbl & 0x1fff;
  412. vbl_end = (vbl >> 16) & 0x1fff;
  413. if ((*vpos < vbl_start) || (*vpos > vbl_end))
  414. in_vbl = false;
  415. /* Inside "upper part" of vblank area? Apply corrective offset: */
  416. if (in_vbl && (*vpos >= vbl_start))
  417. *vpos = *vpos - vtotal;
  418. /* Readouts valid? */
  419. if (vbl > 0)
  420. ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
  421. /* In vblank? */
  422. if (in_vbl)
  423. ret |= DRM_SCANOUTPOS_INVBL;
  424. return ret;
  425. }
  426. static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
  427. int *max_error,
  428. struct timeval *vblank_time,
  429. unsigned flags)
  430. {
  431. struct drm_crtc *crtc;
  432. if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
  433. DRM_ERROR("Invalid crtc %d\n", pipe);
  434. return -EINVAL;
  435. }
  436. /* Get drm_crtc to timestamp: */
  437. crtc = intel_get_crtc_for_pipe(dev, pipe);
  438. if (crtc == NULL) {
  439. DRM_ERROR("Invalid crtc %d\n", pipe);
  440. return -EINVAL;
  441. }
  442. if (!crtc->enabled) {
  443. DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
  444. return -EBUSY;
  445. }
  446. /* Helper routine in DRM core does all the work: */
  447. return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
  448. vblank_time, flags,
  449. crtc);
  450. }
  451. static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
  452. {
  453. enum drm_connector_status old_status;
  454. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  455. old_status = connector->status;
  456. connector->status = connector->funcs->detect(connector, false);
  457. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
  458. connector->base.id,
  459. drm_get_connector_name(connector),
  460. old_status, connector->status);
  461. return (old_status != connector->status);
  462. }
  463. /*
  464. * Handle hotplug events outside the interrupt handler proper.
  465. */
  466. #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
  467. static void i915_hotplug_work_func(struct work_struct *work)
  468. {
  469. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  470. hotplug_work);
  471. struct drm_device *dev = dev_priv->dev;
  472. struct drm_mode_config *mode_config = &dev->mode_config;
  473. struct intel_connector *intel_connector;
  474. struct intel_encoder *intel_encoder;
  475. struct drm_connector *connector;
  476. unsigned long irqflags;
  477. bool hpd_disabled = false;
  478. bool changed = false;
  479. u32 hpd_event_bits;
  480. /* HPD irq before everything is fully set up. */
  481. if (!dev_priv->enable_hotplug_processing)
  482. return;
  483. mutex_lock(&mode_config->mutex);
  484. DRM_DEBUG_KMS("running encoder hotplug functions\n");
  485. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  486. hpd_event_bits = dev_priv->hpd_event_bits;
  487. dev_priv->hpd_event_bits = 0;
  488. list_for_each_entry(connector, &mode_config->connector_list, head) {
  489. intel_connector = to_intel_connector(connector);
  490. intel_encoder = intel_connector->encoder;
  491. if (intel_encoder->hpd_pin > HPD_NONE &&
  492. dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
  493. connector->polled == DRM_CONNECTOR_POLL_HPD) {
  494. DRM_INFO("HPD interrupt storm detected on connector %s: "
  495. "switching from hotplug detection to polling\n",
  496. drm_get_connector_name(connector));
  497. dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
  498. connector->polled = DRM_CONNECTOR_POLL_CONNECT
  499. | DRM_CONNECTOR_POLL_DISCONNECT;
  500. hpd_disabled = true;
  501. }
  502. if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
  503. DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
  504. drm_get_connector_name(connector), intel_encoder->hpd_pin);
  505. }
  506. }
  507. /* if there were no outputs to poll, poll was disabled,
  508. * therefore make sure it's enabled when disabling HPD on
  509. * some connectors */
  510. if (hpd_disabled) {
  511. drm_kms_helper_poll_enable(dev);
  512. mod_timer(&dev_priv->hotplug_reenable_timer,
  513. jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
  514. }
  515. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  516. list_for_each_entry(connector, &mode_config->connector_list, head) {
  517. intel_connector = to_intel_connector(connector);
  518. intel_encoder = intel_connector->encoder;
  519. if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
  520. if (intel_encoder->hot_plug)
  521. intel_encoder->hot_plug(intel_encoder);
  522. if (intel_hpd_irq_event(dev, connector))
  523. changed = true;
  524. }
  525. }
  526. mutex_unlock(&mode_config->mutex);
  527. if (changed)
  528. drm_kms_helper_hotplug_event(dev);
  529. }
  530. static void ironlake_handle_rps_change(struct drm_device *dev)
  531. {
  532. drm_i915_private_t *dev_priv = dev->dev_private;
  533. u32 busy_up, busy_down, max_avg, min_avg;
  534. u8 new_delay;
  535. unsigned long flags;
  536. spin_lock_irqsave(&mchdev_lock, flags);
  537. I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
  538. new_delay = dev_priv->ips.cur_delay;
  539. I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
  540. busy_up = I915_READ(RCPREVBSYTUPAVG);
  541. busy_down = I915_READ(RCPREVBSYTDNAVG);
  542. max_avg = I915_READ(RCBMAXAVG);
  543. min_avg = I915_READ(RCBMINAVG);
  544. /* Handle RCS change request from hw */
  545. if (busy_up > max_avg) {
  546. if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
  547. new_delay = dev_priv->ips.cur_delay - 1;
  548. if (new_delay < dev_priv->ips.max_delay)
  549. new_delay = dev_priv->ips.max_delay;
  550. } else if (busy_down < min_avg) {
  551. if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
  552. new_delay = dev_priv->ips.cur_delay + 1;
  553. if (new_delay > dev_priv->ips.min_delay)
  554. new_delay = dev_priv->ips.min_delay;
  555. }
  556. if (ironlake_set_drps(dev, new_delay))
  557. dev_priv->ips.cur_delay = new_delay;
  558. spin_unlock_irqrestore(&mchdev_lock, flags);
  559. return;
  560. }
  561. static void notify_ring(struct drm_device *dev,
  562. struct intel_ring_buffer *ring)
  563. {
  564. struct drm_i915_private *dev_priv = dev->dev_private;
  565. if (ring->obj == NULL)
  566. return;
  567. trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
  568. wake_up_all(&ring->irq_queue);
  569. if (i915_enable_hangcheck) {
  570. dev_priv->gpu_error.hangcheck_count = 0;
  571. mod_timer(&dev_priv->gpu_error.hangcheck_timer,
  572. round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
  573. }
  574. }
  575. static void gen6_pm_rps_work(struct work_struct *work)
  576. {
  577. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  578. rps.work);
  579. u32 pm_iir, pm_imr;
  580. u8 new_delay;
  581. spin_lock_irq(&dev_priv->rps.lock);
  582. pm_iir = dev_priv->rps.pm_iir;
  583. dev_priv->rps.pm_iir = 0;
  584. pm_imr = I915_READ(GEN6_PMIMR);
  585. I915_WRITE(GEN6_PMIMR, 0);
  586. spin_unlock_irq(&dev_priv->rps.lock);
  587. if ((pm_iir & GEN6_PM_DEFERRED_EVENTS) == 0)
  588. return;
  589. mutex_lock(&dev_priv->rps.hw_lock);
  590. if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
  591. new_delay = dev_priv->rps.cur_delay + 1;
  592. else
  593. new_delay = dev_priv->rps.cur_delay - 1;
  594. /* sysfs frequency interfaces may have snuck in while servicing the
  595. * interrupt
  596. */
  597. if (!(new_delay > dev_priv->rps.max_delay ||
  598. new_delay < dev_priv->rps.min_delay)) {
  599. if (IS_VALLEYVIEW(dev_priv->dev))
  600. valleyview_set_rps(dev_priv->dev, new_delay);
  601. else
  602. gen6_set_rps(dev_priv->dev, new_delay);
  603. }
  604. if (IS_VALLEYVIEW(dev_priv->dev)) {
  605. /*
  606. * On VLV, when we enter RC6 we may not be at the minimum
  607. * voltage level, so arm a timer to check. It should only
  608. * fire when there's activity or once after we've entered
  609. * RC6, and then won't be re-armed until the next RPS interrupt.
  610. */
  611. mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
  612. msecs_to_jiffies(100));
  613. }
  614. mutex_unlock(&dev_priv->rps.hw_lock);
  615. }
  616. /**
  617. * ivybridge_parity_work - Workqueue called when a parity error interrupt
  618. * occurred.
  619. * @work: workqueue struct
  620. *
  621. * Doesn't actually do anything except notify userspace. As a consequence of
  622. * this event, userspace should try to remap the bad rows since statistically
  623. * it is likely the same row is more likely to go bad again.
  624. */
  625. static void ivybridge_parity_work(struct work_struct *work)
  626. {
  627. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  628. l3_parity.error_work);
  629. u32 error_status, row, bank, subbank;
  630. char *parity_event[5];
  631. uint32_t misccpctl;
  632. unsigned long flags;
  633. /* We must turn off DOP level clock gating to access the L3 registers.
  634. * In order to prevent a get/put style interface, acquire struct mutex
  635. * any time we access those registers.
  636. */
  637. mutex_lock(&dev_priv->dev->struct_mutex);
  638. misccpctl = I915_READ(GEN7_MISCCPCTL);
  639. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  640. POSTING_READ(GEN7_MISCCPCTL);
  641. error_status = I915_READ(GEN7_L3CDERRST1);
  642. row = GEN7_PARITY_ERROR_ROW(error_status);
  643. bank = GEN7_PARITY_ERROR_BANK(error_status);
  644. subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
  645. I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
  646. GEN7_L3CDERRST1_ENABLE);
  647. POSTING_READ(GEN7_L3CDERRST1);
  648. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  649. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  650. dev_priv->gt_irq_mask &= ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
  651. I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
  652. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  653. mutex_unlock(&dev_priv->dev->struct_mutex);
  654. parity_event[0] = "L3_PARITY_ERROR=1";
  655. parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
  656. parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
  657. parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
  658. parity_event[4] = NULL;
  659. kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
  660. KOBJ_CHANGE, parity_event);
  661. DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
  662. row, bank, subbank);
  663. kfree(parity_event[3]);
  664. kfree(parity_event[2]);
  665. kfree(parity_event[1]);
  666. }
  667. static void ivybridge_handle_parity_error(struct drm_device *dev)
  668. {
  669. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  670. unsigned long flags;
  671. if (!HAS_L3_GPU_CACHE(dev))
  672. return;
  673. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  674. dev_priv->gt_irq_mask |= GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
  675. I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
  676. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  677. queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
  678. }
  679. static void snb_gt_irq_handler(struct drm_device *dev,
  680. struct drm_i915_private *dev_priv,
  681. u32 gt_iir)
  682. {
  683. if (gt_iir & (GEN6_RENDER_USER_INTERRUPT |
  684. GEN6_RENDER_PIPE_CONTROL_NOTIFY_INTERRUPT))
  685. notify_ring(dev, &dev_priv->ring[RCS]);
  686. if (gt_iir & GEN6_BSD_USER_INTERRUPT)
  687. notify_ring(dev, &dev_priv->ring[VCS]);
  688. if (gt_iir & GEN6_BLITTER_USER_INTERRUPT)
  689. notify_ring(dev, &dev_priv->ring[BCS]);
  690. if (gt_iir & (GT_GEN6_BLT_CS_ERROR_INTERRUPT |
  691. GT_GEN6_BSD_CS_ERROR_INTERRUPT |
  692. GT_RENDER_CS_ERROR_INTERRUPT)) {
  693. DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
  694. i915_handle_error(dev, false);
  695. }
  696. if (gt_iir & GT_GEN7_L3_PARITY_ERROR_INTERRUPT)
  697. ivybridge_handle_parity_error(dev);
  698. }
  699. static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
  700. u32 pm_iir)
  701. {
  702. unsigned long flags;
  703. /*
  704. * IIR bits should never already be set because IMR should
  705. * prevent an interrupt from being shown in IIR. The warning
  706. * displays a case where we've unsafely cleared
  707. * dev_priv->rps.pm_iir. Although missing an interrupt of the same
  708. * type is not a problem, it displays a problem in the logic.
  709. *
  710. * The mask bit in IMR is cleared by dev_priv->rps.work.
  711. */
  712. spin_lock_irqsave(&dev_priv->rps.lock, flags);
  713. dev_priv->rps.pm_iir |= pm_iir;
  714. I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
  715. POSTING_READ(GEN6_PMIMR);
  716. spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
  717. queue_work(dev_priv->wq, &dev_priv->rps.work);
  718. }
  719. #define HPD_STORM_DETECT_PERIOD 1000
  720. #define HPD_STORM_THRESHOLD 5
  721. static inline bool hotplug_irq_storm_detect(struct drm_device *dev,
  722. u32 hotplug_trigger,
  723. const u32 *hpd)
  724. {
  725. drm_i915_private_t *dev_priv = dev->dev_private;
  726. unsigned long irqflags;
  727. int i;
  728. bool ret = false;
  729. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  730. for (i = 1; i < HPD_NUM_PINS; i++) {
  731. if (!(hpd[i] & hotplug_trigger) ||
  732. dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
  733. continue;
  734. dev_priv->hpd_event_bits |= (1 << i);
  735. if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
  736. dev_priv->hpd_stats[i].hpd_last_jiffies
  737. + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
  738. dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
  739. dev_priv->hpd_stats[i].hpd_cnt = 0;
  740. } else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
  741. dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
  742. dev_priv->hpd_event_bits &= ~(1 << i);
  743. DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
  744. ret = true;
  745. } else {
  746. dev_priv->hpd_stats[i].hpd_cnt++;
  747. }
  748. }
  749. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  750. return ret;
  751. }
  752. static void gmbus_irq_handler(struct drm_device *dev)
  753. {
  754. struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
  755. wake_up_all(&dev_priv->gmbus_wait_queue);
  756. }
  757. static void dp_aux_irq_handler(struct drm_device *dev)
  758. {
  759. struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
  760. wake_up_all(&dev_priv->gmbus_wait_queue);
  761. }
  762. static irqreturn_t valleyview_irq_handler(int irq, void *arg)
  763. {
  764. struct drm_device *dev = (struct drm_device *) arg;
  765. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  766. u32 iir, gt_iir, pm_iir;
  767. irqreturn_t ret = IRQ_NONE;
  768. unsigned long irqflags;
  769. int pipe;
  770. u32 pipe_stats[I915_MAX_PIPES];
  771. atomic_inc(&dev_priv->irq_received);
  772. while (true) {
  773. iir = I915_READ(VLV_IIR);
  774. gt_iir = I915_READ(GTIIR);
  775. pm_iir = I915_READ(GEN6_PMIIR);
  776. if (gt_iir == 0 && pm_iir == 0 && iir == 0)
  777. goto out;
  778. ret = IRQ_HANDLED;
  779. snb_gt_irq_handler(dev, dev_priv, gt_iir);
  780. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  781. for_each_pipe(pipe) {
  782. int reg = PIPESTAT(pipe);
  783. pipe_stats[pipe] = I915_READ(reg);
  784. /*
  785. * Clear the PIPE*STAT regs before the IIR
  786. */
  787. if (pipe_stats[pipe] & 0x8000ffff) {
  788. if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
  789. DRM_DEBUG_DRIVER("pipe %c underrun\n",
  790. pipe_name(pipe));
  791. I915_WRITE(reg, pipe_stats[pipe]);
  792. }
  793. }
  794. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  795. for_each_pipe(pipe) {
  796. if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
  797. drm_handle_vblank(dev, pipe);
  798. if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
  799. intel_prepare_page_flip(dev, pipe);
  800. intel_finish_page_flip(dev, pipe);
  801. }
  802. }
  803. /* Consume port. Then clear IIR or we'll miss events */
  804. if (iir & I915_DISPLAY_PORT_INTERRUPT) {
  805. u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
  806. u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
  807. DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
  808. hotplug_status);
  809. if (hotplug_trigger) {
  810. if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
  811. i915_hpd_irq_setup(dev);
  812. queue_work(dev_priv->wq,
  813. &dev_priv->hotplug_work);
  814. }
  815. I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
  816. I915_READ(PORT_HOTPLUG_STAT);
  817. }
  818. if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
  819. gmbus_irq_handler(dev);
  820. if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
  821. gen6_queue_rps_work(dev_priv, pm_iir);
  822. I915_WRITE(GTIIR, gt_iir);
  823. I915_WRITE(GEN6_PMIIR, pm_iir);
  824. I915_WRITE(VLV_IIR, iir);
  825. }
  826. out:
  827. return ret;
  828. }
  829. static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
  830. {
  831. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  832. int pipe;
  833. u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
  834. if (hotplug_trigger) {
  835. if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_ibx))
  836. ibx_hpd_irq_setup(dev);
  837. queue_work(dev_priv->wq, &dev_priv->hotplug_work);
  838. }
  839. if (pch_iir & SDE_AUDIO_POWER_MASK) {
  840. int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
  841. SDE_AUDIO_POWER_SHIFT);
  842. DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
  843. port_name(port));
  844. }
  845. if (pch_iir & SDE_AUX_MASK)
  846. dp_aux_irq_handler(dev);
  847. if (pch_iir & SDE_GMBUS)
  848. gmbus_irq_handler(dev);
  849. if (pch_iir & SDE_AUDIO_HDCP_MASK)
  850. DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
  851. if (pch_iir & SDE_AUDIO_TRANS_MASK)
  852. DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
  853. if (pch_iir & SDE_POISON)
  854. DRM_ERROR("PCH poison interrupt\n");
  855. if (pch_iir & SDE_FDI_MASK)
  856. for_each_pipe(pipe)
  857. DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
  858. pipe_name(pipe),
  859. I915_READ(FDI_RX_IIR(pipe)));
  860. if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
  861. DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
  862. if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
  863. DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
  864. if (pch_iir & SDE_TRANSA_FIFO_UNDER)
  865. if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
  866. false))
  867. DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
  868. if (pch_iir & SDE_TRANSB_FIFO_UNDER)
  869. if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
  870. false))
  871. DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
  872. }
  873. static void ivb_err_int_handler(struct drm_device *dev)
  874. {
  875. struct drm_i915_private *dev_priv = dev->dev_private;
  876. u32 err_int = I915_READ(GEN7_ERR_INT);
  877. if (err_int & ERR_INT_POISON)
  878. DRM_ERROR("Poison interrupt\n");
  879. if (err_int & ERR_INT_FIFO_UNDERRUN_A)
  880. if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
  881. DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
  882. if (err_int & ERR_INT_FIFO_UNDERRUN_B)
  883. if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
  884. DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
  885. if (err_int & ERR_INT_FIFO_UNDERRUN_C)
  886. if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
  887. DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
  888. I915_WRITE(GEN7_ERR_INT, err_int);
  889. }
  890. static void cpt_serr_int_handler(struct drm_device *dev)
  891. {
  892. struct drm_i915_private *dev_priv = dev->dev_private;
  893. u32 serr_int = I915_READ(SERR_INT);
  894. if (serr_int & SERR_INT_POISON)
  895. DRM_ERROR("PCH poison interrupt\n");
  896. if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
  897. if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
  898. false))
  899. DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
  900. if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
  901. if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
  902. false))
  903. DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
  904. if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
  905. if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
  906. false))
  907. DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
  908. I915_WRITE(SERR_INT, serr_int);
  909. }
  910. static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
  911. {
  912. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  913. int pipe;
  914. u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
  915. if (hotplug_trigger) {
  916. if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_cpt))
  917. ibx_hpd_irq_setup(dev);
  918. queue_work(dev_priv->wq, &dev_priv->hotplug_work);
  919. }
  920. if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
  921. int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
  922. SDE_AUDIO_POWER_SHIFT_CPT);
  923. DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
  924. port_name(port));
  925. }
  926. if (pch_iir & SDE_AUX_MASK_CPT)
  927. dp_aux_irq_handler(dev);
  928. if (pch_iir & SDE_GMBUS_CPT)
  929. gmbus_irq_handler(dev);
  930. if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
  931. DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
  932. if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
  933. DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
  934. if (pch_iir & SDE_FDI_MASK_CPT)
  935. for_each_pipe(pipe)
  936. DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
  937. pipe_name(pipe),
  938. I915_READ(FDI_RX_IIR(pipe)));
  939. if (pch_iir & SDE_ERROR_CPT)
  940. cpt_serr_int_handler(dev);
  941. }
  942. static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
  943. {
  944. struct drm_device *dev = (struct drm_device *) arg;
  945. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  946. u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
  947. irqreturn_t ret = IRQ_NONE;
  948. int i;
  949. atomic_inc(&dev_priv->irq_received);
  950. /* We get interrupts on unclaimed registers, so check for this before we
  951. * do any I915_{READ,WRITE}. */
  952. if (IS_HASWELL(dev) &&
  953. (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
  954. DRM_ERROR("Unclaimed register before interrupt\n");
  955. I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
  956. }
  957. /* disable master interrupt before clearing iir */
  958. de_ier = I915_READ(DEIER);
  959. I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
  960. /* Disable south interrupts. We'll only write to SDEIIR once, so further
  961. * interrupts will will be stored on its back queue, and then we'll be
  962. * able to process them after we restore SDEIER (as soon as we restore
  963. * it, we'll get an interrupt if SDEIIR still has something to process
  964. * due to its back queue). */
  965. if (!HAS_PCH_NOP(dev)) {
  966. sde_ier = I915_READ(SDEIER);
  967. I915_WRITE(SDEIER, 0);
  968. POSTING_READ(SDEIER);
  969. }
  970. /* On Haswell, also mask ERR_INT because we don't want to risk
  971. * generating "unclaimed register" interrupts from inside the interrupt
  972. * handler. */
  973. if (IS_HASWELL(dev))
  974. ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
  975. gt_iir = I915_READ(GTIIR);
  976. if (gt_iir) {
  977. snb_gt_irq_handler(dev, dev_priv, gt_iir);
  978. I915_WRITE(GTIIR, gt_iir);
  979. ret = IRQ_HANDLED;
  980. }
  981. de_iir = I915_READ(DEIIR);
  982. if (de_iir) {
  983. if (de_iir & DE_ERR_INT_IVB)
  984. ivb_err_int_handler(dev);
  985. if (de_iir & DE_AUX_CHANNEL_A_IVB)
  986. dp_aux_irq_handler(dev);
  987. if (de_iir & DE_GSE_IVB)
  988. intel_opregion_asle_intr(dev);
  989. for (i = 0; i < 3; i++) {
  990. if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
  991. drm_handle_vblank(dev, i);
  992. if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
  993. intel_prepare_page_flip(dev, i);
  994. intel_finish_page_flip_plane(dev, i);
  995. }
  996. }
  997. /* check event from PCH */
  998. if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
  999. u32 pch_iir = I915_READ(SDEIIR);
  1000. cpt_irq_handler(dev, pch_iir);
  1001. /* clear PCH hotplug event before clear CPU irq */
  1002. I915_WRITE(SDEIIR, pch_iir);
  1003. }
  1004. I915_WRITE(DEIIR, de_iir);
  1005. ret = IRQ_HANDLED;
  1006. }
  1007. pm_iir = I915_READ(GEN6_PMIIR);
  1008. if (pm_iir) {
  1009. if (pm_iir & GEN6_PM_DEFERRED_EVENTS)
  1010. gen6_queue_rps_work(dev_priv, pm_iir);
  1011. I915_WRITE(GEN6_PMIIR, pm_iir);
  1012. ret = IRQ_HANDLED;
  1013. }
  1014. if (IS_HASWELL(dev) && ivb_can_enable_err_int(dev))
  1015. ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
  1016. I915_WRITE(DEIER, de_ier);
  1017. POSTING_READ(DEIER);
  1018. if (!HAS_PCH_NOP(dev)) {
  1019. I915_WRITE(SDEIER, sde_ier);
  1020. POSTING_READ(SDEIER);
  1021. }
  1022. return ret;
  1023. }
  1024. static void ilk_gt_irq_handler(struct drm_device *dev,
  1025. struct drm_i915_private *dev_priv,
  1026. u32 gt_iir)
  1027. {
  1028. if (gt_iir & (GT_USER_INTERRUPT | GT_PIPE_NOTIFY))
  1029. notify_ring(dev, &dev_priv->ring[RCS]);
  1030. if (gt_iir & GT_BSD_USER_INTERRUPT)
  1031. notify_ring(dev, &dev_priv->ring[VCS]);
  1032. }
  1033. static irqreturn_t ironlake_irq_handler(int irq, void *arg)
  1034. {
  1035. struct drm_device *dev = (struct drm_device *) arg;
  1036. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  1037. int ret = IRQ_NONE;
  1038. u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
  1039. atomic_inc(&dev_priv->irq_received);
  1040. /* disable master interrupt before clearing iir */
  1041. de_ier = I915_READ(DEIER);
  1042. I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
  1043. POSTING_READ(DEIER);
  1044. /* Disable south interrupts. We'll only write to SDEIIR once, so further
  1045. * interrupts will will be stored on its back queue, and then we'll be
  1046. * able to process them after we restore SDEIER (as soon as we restore
  1047. * it, we'll get an interrupt if SDEIIR still has something to process
  1048. * due to its back queue). */
  1049. sde_ier = I915_READ(SDEIER);
  1050. I915_WRITE(SDEIER, 0);
  1051. POSTING_READ(SDEIER);
  1052. de_iir = I915_READ(DEIIR);
  1053. gt_iir = I915_READ(GTIIR);
  1054. pm_iir = I915_READ(GEN6_PMIIR);
  1055. if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
  1056. goto done;
  1057. ret = IRQ_HANDLED;
  1058. if (IS_GEN5(dev))
  1059. ilk_gt_irq_handler(dev, dev_priv, gt_iir);
  1060. else
  1061. snb_gt_irq_handler(dev, dev_priv, gt_iir);
  1062. if (de_iir & DE_AUX_CHANNEL_A)
  1063. dp_aux_irq_handler(dev);
  1064. if (de_iir & DE_GSE)
  1065. intel_opregion_asle_intr(dev);
  1066. if (de_iir & DE_PIPEA_VBLANK)
  1067. drm_handle_vblank(dev, 0);
  1068. if (de_iir & DE_PIPEB_VBLANK)
  1069. drm_handle_vblank(dev, 1);
  1070. if (de_iir & DE_POISON)
  1071. DRM_ERROR("Poison interrupt\n");
  1072. if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
  1073. if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
  1074. DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
  1075. if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
  1076. if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
  1077. DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
  1078. if (de_iir & DE_PLANEA_FLIP_DONE) {
  1079. intel_prepare_page_flip(dev, 0);
  1080. intel_finish_page_flip_plane(dev, 0);
  1081. }
  1082. if (de_iir & DE_PLANEB_FLIP_DONE) {
  1083. intel_prepare_page_flip(dev, 1);
  1084. intel_finish_page_flip_plane(dev, 1);
  1085. }
  1086. /* check event from PCH */
  1087. if (de_iir & DE_PCH_EVENT) {
  1088. u32 pch_iir = I915_READ(SDEIIR);
  1089. if (HAS_PCH_CPT(dev))
  1090. cpt_irq_handler(dev, pch_iir);
  1091. else
  1092. ibx_irq_handler(dev, pch_iir);
  1093. /* should clear PCH hotplug event before clear CPU irq */
  1094. I915_WRITE(SDEIIR, pch_iir);
  1095. }
  1096. if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
  1097. ironlake_handle_rps_change(dev);
  1098. if (IS_GEN6(dev) && pm_iir & GEN6_PM_DEFERRED_EVENTS)
  1099. gen6_queue_rps_work(dev_priv, pm_iir);
  1100. I915_WRITE(GTIIR, gt_iir);
  1101. I915_WRITE(DEIIR, de_iir);
  1102. I915_WRITE(GEN6_PMIIR, pm_iir);
  1103. done:
  1104. I915_WRITE(DEIER, de_ier);
  1105. POSTING_READ(DEIER);
  1106. I915_WRITE(SDEIER, sde_ier);
  1107. POSTING_READ(SDEIER);
  1108. return ret;
  1109. }
  1110. /**
  1111. * i915_error_work_func - do process context error handling work
  1112. * @work: work struct
  1113. *
  1114. * Fire an error uevent so userspace can see that a hang or error
  1115. * was detected.
  1116. */
  1117. static void i915_error_work_func(struct work_struct *work)
  1118. {
  1119. struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
  1120. work);
  1121. drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
  1122. gpu_error);
  1123. struct drm_device *dev = dev_priv->dev;
  1124. struct intel_ring_buffer *ring;
  1125. char *error_event[] = { "ERROR=1", NULL };
  1126. char *reset_event[] = { "RESET=1", NULL };
  1127. char *reset_done_event[] = { "ERROR=0", NULL };
  1128. int i, ret;
  1129. kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
  1130. /*
  1131. * Note that there's only one work item which does gpu resets, so we
  1132. * need not worry about concurrent gpu resets potentially incrementing
  1133. * error->reset_counter twice. We only need to take care of another
  1134. * racing irq/hangcheck declaring the gpu dead for a second time. A
  1135. * quick check for that is good enough: schedule_work ensures the
  1136. * correct ordering between hang detection and this work item, and since
  1137. * the reset in-progress bit is only ever set by code outside of this
  1138. * work we don't need to worry about any other races.
  1139. */
  1140. if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
  1141. DRM_DEBUG_DRIVER("resetting chip\n");
  1142. kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
  1143. reset_event);
  1144. ret = i915_reset(dev);
  1145. if (ret == 0) {
  1146. /*
  1147. * After all the gem state is reset, increment the reset
  1148. * counter and wake up everyone waiting for the reset to
  1149. * complete.
  1150. *
  1151. * Since unlock operations are a one-sided barrier only,
  1152. * we need to insert a barrier here to order any seqno
  1153. * updates before
  1154. * the counter increment.
  1155. */
  1156. smp_mb__before_atomic_inc();
  1157. atomic_inc(&dev_priv->gpu_error.reset_counter);
  1158. kobject_uevent_env(&dev->primary->kdev.kobj,
  1159. KOBJ_CHANGE, reset_done_event);
  1160. } else {
  1161. atomic_set(&error->reset_counter, I915_WEDGED);
  1162. }
  1163. for_each_ring(ring, dev_priv, i)
  1164. wake_up_all(&ring->irq_queue);
  1165. intel_display_handle_reset(dev);
  1166. wake_up_all(&dev_priv->gpu_error.reset_queue);
  1167. }
  1168. }
  1169. /* NB: please notice the memset */
  1170. static void i915_get_extra_instdone(struct drm_device *dev,
  1171. uint32_t *instdone)
  1172. {
  1173. struct drm_i915_private *dev_priv = dev->dev_private;
  1174. memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
  1175. switch(INTEL_INFO(dev)->gen) {
  1176. case 2:
  1177. case 3:
  1178. instdone[0] = I915_READ(INSTDONE);
  1179. break;
  1180. case 4:
  1181. case 5:
  1182. case 6:
  1183. instdone[0] = I915_READ(INSTDONE_I965);
  1184. instdone[1] = I915_READ(INSTDONE1);
  1185. break;
  1186. default:
  1187. WARN_ONCE(1, "Unsupported platform\n");
  1188. case 7:
  1189. instdone[0] = I915_READ(GEN7_INSTDONE_1);
  1190. instdone[1] = I915_READ(GEN7_SC_INSTDONE);
  1191. instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
  1192. instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
  1193. break;
  1194. }
  1195. }
  1196. #ifdef CONFIG_DEBUG_FS
  1197. static struct drm_i915_error_object *
  1198. i915_error_object_create_sized(struct drm_i915_private *dev_priv,
  1199. struct drm_i915_gem_object *src,
  1200. const int num_pages)
  1201. {
  1202. struct drm_i915_error_object *dst;
  1203. int i;
  1204. u32 reloc_offset;
  1205. if (src == NULL || src->pages == NULL)
  1206. return NULL;
  1207. dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
  1208. if (dst == NULL)
  1209. return NULL;
  1210. reloc_offset = src->gtt_offset;
  1211. for (i = 0; i < num_pages; i++) {
  1212. unsigned long flags;
  1213. void *d;
  1214. d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
  1215. if (d == NULL)
  1216. goto unwind;
  1217. local_irq_save(flags);
  1218. if (reloc_offset < dev_priv->gtt.mappable_end &&
  1219. src->has_global_gtt_mapping) {
  1220. void __iomem *s;
  1221. /* Simply ignore tiling or any overlapping fence.
  1222. * It's part of the error state, and this hopefully
  1223. * captures what the GPU read.
  1224. */
  1225. s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  1226. reloc_offset);
  1227. memcpy_fromio(d, s, PAGE_SIZE);
  1228. io_mapping_unmap_atomic(s);
  1229. } else if (src->stolen) {
  1230. unsigned long offset;
  1231. offset = dev_priv->mm.stolen_base;
  1232. offset += src->stolen->start;
  1233. offset += i << PAGE_SHIFT;
  1234. memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
  1235. } else {
  1236. struct page *page;
  1237. void *s;
  1238. page = i915_gem_object_get_page(src, i);
  1239. drm_clflush_pages(&page, 1);
  1240. s = kmap_atomic(page);
  1241. memcpy(d, s, PAGE_SIZE);
  1242. kunmap_atomic(s);
  1243. drm_clflush_pages(&page, 1);
  1244. }
  1245. local_irq_restore(flags);
  1246. dst->pages[i] = d;
  1247. reloc_offset += PAGE_SIZE;
  1248. }
  1249. dst->page_count = num_pages;
  1250. dst->gtt_offset = src->gtt_offset;
  1251. return dst;
  1252. unwind:
  1253. while (i--)
  1254. kfree(dst->pages[i]);
  1255. kfree(dst);
  1256. return NULL;
  1257. }
  1258. #define i915_error_object_create(dev_priv, src) \
  1259. i915_error_object_create_sized((dev_priv), (src), \
  1260. (src)->base.size>>PAGE_SHIFT)
  1261. static void
  1262. i915_error_object_free(struct drm_i915_error_object *obj)
  1263. {
  1264. int page;
  1265. if (obj == NULL)
  1266. return;
  1267. for (page = 0; page < obj->page_count; page++)
  1268. kfree(obj->pages[page]);
  1269. kfree(obj);
  1270. }
  1271. void
  1272. i915_error_state_free(struct kref *error_ref)
  1273. {
  1274. struct drm_i915_error_state *error = container_of(error_ref,
  1275. typeof(*error), ref);
  1276. int i;
  1277. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  1278. i915_error_object_free(error->ring[i].batchbuffer);
  1279. i915_error_object_free(error->ring[i].ringbuffer);
  1280. kfree(error->ring[i].requests);
  1281. }
  1282. kfree(error->active_bo);
  1283. kfree(error->overlay);
  1284. kfree(error);
  1285. }
  1286. static void capture_bo(struct drm_i915_error_buffer *err,
  1287. struct drm_i915_gem_object *obj)
  1288. {
  1289. err->size = obj->base.size;
  1290. err->name = obj->base.name;
  1291. err->rseqno = obj->last_read_seqno;
  1292. err->wseqno = obj->last_write_seqno;
  1293. err->gtt_offset = obj->gtt_offset;
  1294. err->read_domains = obj->base.read_domains;
  1295. err->write_domain = obj->base.write_domain;
  1296. err->fence_reg = obj->fence_reg;
  1297. err->pinned = 0;
  1298. if (obj->pin_count > 0)
  1299. err->pinned = 1;
  1300. if (obj->user_pin_count > 0)
  1301. err->pinned = -1;
  1302. err->tiling = obj->tiling_mode;
  1303. err->dirty = obj->dirty;
  1304. err->purgeable = obj->madv != I915_MADV_WILLNEED;
  1305. err->ring = obj->ring ? obj->ring->id : -1;
  1306. err->cache_level = obj->cache_level;
  1307. }
  1308. static u32 capture_active_bo(struct drm_i915_error_buffer *err,
  1309. int count, struct list_head *head)
  1310. {
  1311. struct drm_i915_gem_object *obj;
  1312. int i = 0;
  1313. list_for_each_entry(obj, head, mm_list) {
  1314. capture_bo(err++, obj);
  1315. if (++i == count)
  1316. break;
  1317. }
  1318. return i;
  1319. }
  1320. static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
  1321. int count, struct list_head *head)
  1322. {
  1323. struct drm_i915_gem_object *obj;
  1324. int i = 0;
  1325. list_for_each_entry(obj, head, gtt_list) {
  1326. if (obj->pin_count == 0)
  1327. continue;
  1328. capture_bo(err++, obj);
  1329. if (++i == count)
  1330. break;
  1331. }
  1332. return i;
  1333. }
  1334. static void i915_gem_record_fences(struct drm_device *dev,
  1335. struct drm_i915_error_state *error)
  1336. {
  1337. struct drm_i915_private *dev_priv = dev->dev_private;
  1338. int i;
  1339. /* Fences */
  1340. switch (INTEL_INFO(dev)->gen) {
  1341. case 7:
  1342. case 6:
  1343. for (i = 0; i < dev_priv->num_fence_regs; i++)
  1344. error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
  1345. break;
  1346. case 5:
  1347. case 4:
  1348. for (i = 0; i < 16; i++)
  1349. error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
  1350. break;
  1351. case 3:
  1352. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  1353. for (i = 0; i < 8; i++)
  1354. error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
  1355. case 2:
  1356. for (i = 0; i < 8; i++)
  1357. error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
  1358. break;
  1359. default:
  1360. BUG();
  1361. }
  1362. }
  1363. static struct drm_i915_error_object *
  1364. i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
  1365. struct intel_ring_buffer *ring)
  1366. {
  1367. struct drm_i915_gem_object *obj;
  1368. u32 seqno;
  1369. if (!ring->get_seqno)
  1370. return NULL;
  1371. if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
  1372. u32 acthd = I915_READ(ACTHD);
  1373. if (WARN_ON(ring->id != RCS))
  1374. return NULL;
  1375. obj = ring->private;
  1376. if (acthd >= obj->gtt_offset &&
  1377. acthd < obj->gtt_offset + obj->base.size)
  1378. return i915_error_object_create(dev_priv, obj);
  1379. }
  1380. seqno = ring->get_seqno(ring, false);
  1381. list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
  1382. if (obj->ring != ring)
  1383. continue;
  1384. if (i915_seqno_passed(seqno, obj->last_read_seqno))
  1385. continue;
  1386. if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
  1387. continue;
  1388. /* We need to copy these to an anonymous buffer as the simplest
  1389. * method to avoid being overwritten by userspace.
  1390. */
  1391. return i915_error_object_create(dev_priv, obj);
  1392. }
  1393. return NULL;
  1394. }
  1395. static void i915_record_ring_state(struct drm_device *dev,
  1396. struct drm_i915_error_state *error,
  1397. struct intel_ring_buffer *ring)
  1398. {
  1399. struct drm_i915_private *dev_priv = dev->dev_private;
  1400. if (INTEL_INFO(dev)->gen >= 6) {
  1401. error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
  1402. error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
  1403. error->semaphore_mboxes[ring->id][0]
  1404. = I915_READ(RING_SYNC_0(ring->mmio_base));
  1405. error->semaphore_mboxes[ring->id][1]
  1406. = I915_READ(RING_SYNC_1(ring->mmio_base));
  1407. error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
  1408. error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
  1409. }
  1410. if (INTEL_INFO(dev)->gen >= 4) {
  1411. error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
  1412. error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
  1413. error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
  1414. error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
  1415. error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
  1416. if (ring->id == RCS)
  1417. error->bbaddr = I915_READ64(BB_ADDR);
  1418. } else {
  1419. error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
  1420. error->ipeir[ring->id] = I915_READ(IPEIR);
  1421. error->ipehr[ring->id] = I915_READ(IPEHR);
  1422. error->instdone[ring->id] = I915_READ(INSTDONE);
  1423. }
  1424. error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
  1425. error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
  1426. error->seqno[ring->id] = ring->get_seqno(ring, false);
  1427. error->acthd[ring->id] = intel_ring_get_active_head(ring);
  1428. error->head[ring->id] = I915_READ_HEAD(ring);
  1429. error->tail[ring->id] = I915_READ_TAIL(ring);
  1430. error->ctl[ring->id] = I915_READ_CTL(ring);
  1431. error->cpu_ring_head[ring->id] = ring->head;
  1432. error->cpu_ring_tail[ring->id] = ring->tail;
  1433. }
  1434. static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
  1435. struct drm_i915_error_state *error,
  1436. struct drm_i915_error_ring *ering)
  1437. {
  1438. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1439. struct drm_i915_gem_object *obj;
  1440. /* Currently render ring is the only HW context user */
  1441. if (ring->id != RCS || !error->ccid)
  1442. return;
  1443. list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
  1444. if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
  1445. ering->ctx = i915_error_object_create_sized(dev_priv,
  1446. obj, 1);
  1447. }
  1448. }
  1449. }
  1450. static void i915_gem_record_rings(struct drm_device *dev,
  1451. struct drm_i915_error_state *error)
  1452. {
  1453. struct drm_i915_private *dev_priv = dev->dev_private;
  1454. struct intel_ring_buffer *ring;
  1455. struct drm_i915_gem_request *request;
  1456. int i, count;
  1457. for_each_ring(ring, dev_priv, i) {
  1458. i915_record_ring_state(dev, error, ring);
  1459. error->ring[i].batchbuffer =
  1460. i915_error_first_batchbuffer(dev_priv, ring);
  1461. error->ring[i].ringbuffer =
  1462. i915_error_object_create(dev_priv, ring->obj);
  1463. i915_gem_record_active_context(ring, error, &error->ring[i]);
  1464. count = 0;
  1465. list_for_each_entry(request, &ring->request_list, list)
  1466. count++;
  1467. error->ring[i].num_requests = count;
  1468. error->ring[i].requests =
  1469. kmalloc(count*sizeof(struct drm_i915_error_request),
  1470. GFP_ATOMIC);
  1471. if (error->ring[i].requests == NULL) {
  1472. error->ring[i].num_requests = 0;
  1473. continue;
  1474. }
  1475. count = 0;
  1476. list_for_each_entry(request, &ring->request_list, list) {
  1477. struct drm_i915_error_request *erq;
  1478. erq = &error->ring[i].requests[count++];
  1479. erq->seqno = request->seqno;
  1480. erq->jiffies = request->emitted_jiffies;
  1481. erq->tail = request->tail;
  1482. }
  1483. }
  1484. }
  1485. /**
  1486. * i915_capture_error_state - capture an error record for later analysis
  1487. * @dev: drm device
  1488. *
  1489. * Should be called when an error is detected (either a hang or an error
  1490. * interrupt) to capture error state from the time of the error. Fills
  1491. * out a structure which becomes available in debugfs for user level tools
  1492. * to pick up.
  1493. */
  1494. static void i915_capture_error_state(struct drm_device *dev)
  1495. {
  1496. struct drm_i915_private *dev_priv = dev->dev_private;
  1497. struct drm_i915_gem_object *obj;
  1498. struct drm_i915_error_state *error;
  1499. unsigned long flags;
  1500. int i, pipe;
  1501. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  1502. error = dev_priv->gpu_error.first_error;
  1503. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  1504. if (error)
  1505. return;
  1506. /* Account for pipe specific data like PIPE*STAT */
  1507. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  1508. if (!error) {
  1509. DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
  1510. return;
  1511. }
  1512. DRM_INFO("capturing error event; look for more information in "
  1513. "/sys/kernel/debug/dri/%d/i915_error_state\n",
  1514. dev->primary->index);
  1515. kref_init(&error->ref);
  1516. error->eir = I915_READ(EIR);
  1517. error->pgtbl_er = I915_READ(PGTBL_ER);
  1518. if (HAS_HW_CONTEXTS(dev))
  1519. error->ccid = I915_READ(CCID);
  1520. if (HAS_PCH_SPLIT(dev))
  1521. error->ier = I915_READ(DEIER) | I915_READ(GTIER);
  1522. else if (IS_VALLEYVIEW(dev))
  1523. error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
  1524. else if (IS_GEN2(dev))
  1525. error->ier = I915_READ16(IER);
  1526. else
  1527. error->ier = I915_READ(IER);
  1528. if (INTEL_INFO(dev)->gen >= 6)
  1529. error->derrmr = I915_READ(DERRMR);
  1530. if (IS_VALLEYVIEW(dev))
  1531. error->forcewake = I915_READ(FORCEWAKE_VLV);
  1532. else if (INTEL_INFO(dev)->gen >= 7)
  1533. error->forcewake = I915_READ(FORCEWAKE_MT);
  1534. else if (INTEL_INFO(dev)->gen == 6)
  1535. error->forcewake = I915_READ(FORCEWAKE);
  1536. if (!HAS_PCH_SPLIT(dev))
  1537. for_each_pipe(pipe)
  1538. error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
  1539. if (INTEL_INFO(dev)->gen >= 6) {
  1540. error->error = I915_READ(ERROR_GEN6);
  1541. error->done_reg = I915_READ(DONE_REG);
  1542. }
  1543. if (INTEL_INFO(dev)->gen == 7)
  1544. error->err_int = I915_READ(GEN7_ERR_INT);
  1545. i915_get_extra_instdone(dev, error->extra_instdone);
  1546. i915_gem_record_fences(dev, error);
  1547. i915_gem_record_rings(dev, error);
  1548. /* Record buffers on the active and pinned lists. */
  1549. error->active_bo = NULL;
  1550. error->pinned_bo = NULL;
  1551. i = 0;
  1552. list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
  1553. i++;
  1554. error->active_bo_count = i;
  1555. list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
  1556. if (obj->pin_count)
  1557. i++;
  1558. error->pinned_bo_count = i - error->active_bo_count;
  1559. error->active_bo = NULL;
  1560. error->pinned_bo = NULL;
  1561. if (i) {
  1562. error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
  1563. GFP_ATOMIC);
  1564. if (error->active_bo)
  1565. error->pinned_bo =
  1566. error->active_bo + error->active_bo_count;
  1567. }
  1568. if (error->active_bo)
  1569. error->active_bo_count =
  1570. capture_active_bo(error->active_bo,
  1571. error->active_bo_count,
  1572. &dev_priv->mm.active_list);
  1573. if (error->pinned_bo)
  1574. error->pinned_bo_count =
  1575. capture_pinned_bo(error->pinned_bo,
  1576. error->pinned_bo_count,
  1577. &dev_priv->mm.bound_list);
  1578. do_gettimeofday(&error->time);
  1579. error->overlay = intel_overlay_capture_error_state(dev);
  1580. error->display = intel_display_capture_error_state(dev);
  1581. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  1582. if (dev_priv->gpu_error.first_error == NULL) {
  1583. dev_priv->gpu_error.first_error = error;
  1584. error = NULL;
  1585. }
  1586. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  1587. if (error)
  1588. i915_error_state_free(&error->ref);
  1589. }
  1590. void i915_destroy_error_state(struct drm_device *dev)
  1591. {
  1592. struct drm_i915_private *dev_priv = dev->dev_private;
  1593. struct drm_i915_error_state *error;
  1594. unsigned long flags;
  1595. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  1596. error = dev_priv->gpu_error.first_error;
  1597. dev_priv->gpu_error.first_error = NULL;
  1598. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  1599. if (error)
  1600. kref_put(&error->ref, i915_error_state_free);
  1601. }
  1602. #else
  1603. #define i915_capture_error_state(x)
  1604. #endif
  1605. static void i915_report_and_clear_eir(struct drm_device *dev)
  1606. {
  1607. struct drm_i915_private *dev_priv = dev->dev_private;
  1608. uint32_t instdone[I915_NUM_INSTDONE_REG];
  1609. u32 eir = I915_READ(EIR);
  1610. int pipe, i;
  1611. if (!eir)
  1612. return;
  1613. pr_err("render error detected, EIR: 0x%08x\n", eir);
  1614. i915_get_extra_instdone(dev, instdone);
  1615. if (IS_G4X(dev)) {
  1616. if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
  1617. u32 ipeir = I915_READ(IPEIR_I965);
  1618. pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
  1619. pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
  1620. for (i = 0; i < ARRAY_SIZE(instdone); i++)
  1621. pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
  1622. pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
  1623. pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
  1624. I915_WRITE(IPEIR_I965, ipeir);
  1625. POSTING_READ(IPEIR_I965);
  1626. }
  1627. if (eir & GM45_ERROR_PAGE_TABLE) {
  1628. u32 pgtbl_err = I915_READ(PGTBL_ER);
  1629. pr_err("page table error\n");
  1630. pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
  1631. I915_WRITE(PGTBL_ER, pgtbl_err);
  1632. POSTING_READ(PGTBL_ER);
  1633. }
  1634. }
  1635. if (!IS_GEN2(dev)) {
  1636. if (eir & I915_ERROR_PAGE_TABLE) {
  1637. u32 pgtbl_err = I915_READ(PGTBL_ER);
  1638. pr_err("page table error\n");
  1639. pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
  1640. I915_WRITE(PGTBL_ER, pgtbl_err);
  1641. POSTING_READ(PGTBL_ER);
  1642. }
  1643. }
  1644. if (eir & I915_ERROR_MEMORY_REFRESH) {
  1645. pr_err("memory refresh error:\n");
  1646. for_each_pipe(pipe)
  1647. pr_err("pipe %c stat: 0x%08x\n",
  1648. pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
  1649. /* pipestat has already been acked */
  1650. }
  1651. if (eir & I915_ERROR_INSTRUCTION) {
  1652. pr_err("instruction error\n");
  1653. pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
  1654. for (i = 0; i < ARRAY_SIZE(instdone); i++)
  1655. pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
  1656. if (INTEL_INFO(dev)->gen < 4) {
  1657. u32 ipeir = I915_READ(IPEIR);
  1658. pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
  1659. pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
  1660. pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
  1661. I915_WRITE(IPEIR, ipeir);
  1662. POSTING_READ(IPEIR);
  1663. } else {
  1664. u32 ipeir = I915_READ(IPEIR_I965);
  1665. pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
  1666. pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
  1667. pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
  1668. pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
  1669. I915_WRITE(IPEIR_I965, ipeir);
  1670. POSTING_READ(IPEIR_I965);
  1671. }
  1672. }
  1673. I915_WRITE(EIR, eir);
  1674. POSTING_READ(EIR);
  1675. eir = I915_READ(EIR);
  1676. if (eir) {
  1677. /*
  1678. * some errors might have become stuck,
  1679. * mask them.
  1680. */
  1681. DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
  1682. I915_WRITE(EMR, I915_READ(EMR) | eir);
  1683. I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  1684. }
  1685. }
  1686. /**
  1687. * i915_handle_error - handle an error interrupt
  1688. * @dev: drm device
  1689. *
  1690. * Do some basic checking of regsiter state at error interrupt time and
  1691. * dump it to the syslog. Also call i915_capture_error_state() to make
  1692. * sure we get a record and make it available in debugfs. Fire a uevent
  1693. * so userspace knows something bad happened (should trigger collection
  1694. * of a ring dump etc.).
  1695. */
  1696. void i915_handle_error(struct drm_device *dev, bool wedged)
  1697. {
  1698. struct drm_i915_private *dev_priv = dev->dev_private;
  1699. struct intel_ring_buffer *ring;
  1700. int i;
  1701. i915_capture_error_state(dev);
  1702. i915_report_and_clear_eir(dev);
  1703. if (wedged) {
  1704. atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
  1705. &dev_priv->gpu_error.reset_counter);
  1706. /*
  1707. * Wakeup waiting processes so that the reset work item
  1708. * doesn't deadlock trying to grab various locks.
  1709. */
  1710. for_each_ring(ring, dev_priv, i)
  1711. wake_up_all(&ring->irq_queue);
  1712. }
  1713. queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
  1714. }
  1715. static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
  1716. {
  1717. drm_i915_private_t *dev_priv = dev->dev_private;
  1718. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1719. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1720. struct drm_i915_gem_object *obj;
  1721. struct intel_unpin_work *work;
  1722. unsigned long flags;
  1723. bool stall_detected;
  1724. /* Ignore early vblank irqs */
  1725. if (intel_crtc == NULL)
  1726. return;
  1727. spin_lock_irqsave(&dev->event_lock, flags);
  1728. work = intel_crtc->unpin_work;
  1729. if (work == NULL ||
  1730. atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
  1731. !work->enable_stall_check) {
  1732. /* Either the pending flip IRQ arrived, or we're too early. Don't check */
  1733. spin_unlock_irqrestore(&dev->event_lock, flags);
  1734. return;
  1735. }
  1736. /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
  1737. obj = work->pending_flip_obj;
  1738. if (INTEL_INFO(dev)->gen >= 4) {
  1739. int dspsurf = DSPSURF(intel_crtc->plane);
  1740. stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
  1741. obj->gtt_offset;
  1742. } else {
  1743. int dspaddr = DSPADDR(intel_crtc->plane);
  1744. stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
  1745. crtc->y * crtc->fb->pitches[0] +
  1746. crtc->x * crtc->fb->bits_per_pixel/8);
  1747. }
  1748. spin_unlock_irqrestore(&dev->event_lock, flags);
  1749. if (stall_detected) {
  1750. DRM_DEBUG_DRIVER("Pageflip stall detected\n");
  1751. intel_prepare_page_flip(dev, intel_crtc->plane);
  1752. }
  1753. }
  1754. /* Called from drm generic code, passed 'crtc' which
  1755. * we use as a pipe index
  1756. */
  1757. static int i915_enable_vblank(struct drm_device *dev, int pipe)
  1758. {
  1759. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  1760. unsigned long irqflags;
  1761. if (!i915_pipe_enabled(dev, pipe))
  1762. return -EINVAL;
  1763. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  1764. if (INTEL_INFO(dev)->gen >= 4)
  1765. i915_enable_pipestat(dev_priv, pipe,
  1766. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  1767. else
  1768. i915_enable_pipestat(dev_priv, pipe,
  1769. PIPE_VBLANK_INTERRUPT_ENABLE);
  1770. /* maintain vblank delivery even in deep C-states */
  1771. if (dev_priv->info->gen == 3)
  1772. I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
  1773. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  1774. return 0;
  1775. }
  1776. static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
  1777. {
  1778. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  1779. unsigned long irqflags;
  1780. if (!i915_pipe_enabled(dev, pipe))
  1781. return -EINVAL;
  1782. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  1783. ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
  1784. DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
  1785. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  1786. return 0;
  1787. }
  1788. static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
  1789. {
  1790. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  1791. unsigned long irqflags;
  1792. if (!i915_pipe_enabled(dev, pipe))
  1793. return -EINVAL;
  1794. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  1795. ironlake_enable_display_irq(dev_priv,
  1796. DE_PIPEA_VBLANK_IVB << (5 * pipe));
  1797. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  1798. return 0;
  1799. }
  1800. static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
  1801. {
  1802. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  1803. unsigned long irqflags;
  1804. u32 imr;
  1805. if (!i915_pipe_enabled(dev, pipe))
  1806. return -EINVAL;
  1807. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  1808. imr = I915_READ(VLV_IMR);
  1809. if (pipe == 0)
  1810. imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
  1811. else
  1812. imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
  1813. I915_WRITE(VLV_IMR, imr);
  1814. i915_enable_pipestat(dev_priv, pipe,
  1815. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  1816. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  1817. return 0;
  1818. }
  1819. /* Called from drm generic code, passed 'crtc' which
  1820. * we use as a pipe index
  1821. */
  1822. static void i915_disable_vblank(struct drm_device *dev, int pipe)
  1823. {
  1824. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  1825. unsigned long irqflags;
  1826. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  1827. if (dev_priv->info->gen == 3)
  1828. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
  1829. i915_disable_pipestat(dev_priv, pipe,
  1830. PIPE_VBLANK_INTERRUPT_ENABLE |
  1831. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  1832. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  1833. }
  1834. static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
  1835. {
  1836. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  1837. unsigned long irqflags;
  1838. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  1839. ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
  1840. DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
  1841. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  1842. }
  1843. static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
  1844. {
  1845. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  1846. unsigned long irqflags;
  1847. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  1848. ironlake_disable_display_irq(dev_priv,
  1849. DE_PIPEA_VBLANK_IVB << (pipe * 5));
  1850. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  1851. }
  1852. static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
  1853. {
  1854. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  1855. unsigned long irqflags;
  1856. u32 imr;
  1857. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  1858. i915_disable_pipestat(dev_priv, pipe,
  1859. PIPE_START_VBLANK_INTERRUPT_ENABLE);
  1860. imr = I915_READ(VLV_IMR);
  1861. if (pipe == 0)
  1862. imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
  1863. else
  1864. imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
  1865. I915_WRITE(VLV_IMR, imr);
  1866. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  1867. }
  1868. static u32
  1869. ring_last_seqno(struct intel_ring_buffer *ring)
  1870. {
  1871. return list_entry(ring->request_list.prev,
  1872. struct drm_i915_gem_request, list)->seqno;
  1873. }
  1874. static bool i915_hangcheck_ring_idle(struct intel_ring_buffer *ring, bool *err)
  1875. {
  1876. if (list_empty(&ring->request_list) ||
  1877. i915_seqno_passed(ring->get_seqno(ring, false),
  1878. ring_last_seqno(ring))) {
  1879. /* Issue a wake-up to catch stuck h/w. */
  1880. if (waitqueue_active(&ring->irq_queue)) {
  1881. DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
  1882. ring->name);
  1883. wake_up_all(&ring->irq_queue);
  1884. *err = true;
  1885. }
  1886. return true;
  1887. }
  1888. return false;
  1889. }
  1890. static bool semaphore_passed(struct intel_ring_buffer *ring)
  1891. {
  1892. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1893. u32 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
  1894. struct intel_ring_buffer *signaller;
  1895. u32 cmd, ipehr, acthd_min;
  1896. ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
  1897. if ((ipehr & ~(0x3 << 16)) !=
  1898. (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
  1899. return false;
  1900. /* ACTHD is likely pointing to the dword after the actual command,
  1901. * so scan backwards until we find the MBOX.
  1902. */
  1903. acthd_min = max((int)acthd - 3 * 4, 0);
  1904. do {
  1905. cmd = ioread32(ring->virtual_start + acthd);
  1906. if (cmd == ipehr)
  1907. break;
  1908. acthd -= 4;
  1909. if (acthd < acthd_min)
  1910. return false;
  1911. } while (1);
  1912. signaller = &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
  1913. return i915_seqno_passed(signaller->get_seqno(signaller, false),
  1914. ioread32(ring->virtual_start+acthd+4)+1);
  1915. }
  1916. static bool kick_ring(struct intel_ring_buffer *ring)
  1917. {
  1918. struct drm_device *dev = ring->dev;
  1919. struct drm_i915_private *dev_priv = dev->dev_private;
  1920. u32 tmp = I915_READ_CTL(ring);
  1921. if (tmp & RING_WAIT) {
  1922. DRM_ERROR("Kicking stuck wait on %s\n",
  1923. ring->name);
  1924. I915_WRITE_CTL(ring, tmp);
  1925. return true;
  1926. }
  1927. if (INTEL_INFO(dev)->gen >= 6 &&
  1928. tmp & RING_WAIT_SEMAPHORE &&
  1929. semaphore_passed(ring)) {
  1930. DRM_ERROR("Kicking stuck semaphore on %s\n",
  1931. ring->name);
  1932. I915_WRITE_CTL(ring, tmp);
  1933. return true;
  1934. }
  1935. return false;
  1936. }
  1937. static bool i915_hangcheck_hung(struct drm_device *dev)
  1938. {
  1939. drm_i915_private_t *dev_priv = dev->dev_private;
  1940. if (dev_priv->gpu_error.hangcheck_count++ > 1) {
  1941. bool hung = true;
  1942. DRM_ERROR("Hangcheck timer elapsed... GPU hung\n");
  1943. i915_handle_error(dev, true);
  1944. if (!IS_GEN2(dev)) {
  1945. struct intel_ring_buffer *ring;
  1946. int i;
  1947. /* Is the chip hanging on a WAIT_FOR_EVENT?
  1948. * If so we can simply poke the RB_WAIT bit
  1949. * and break the hang. This should work on
  1950. * all but the second generation chipsets.
  1951. */
  1952. for_each_ring(ring, dev_priv, i)
  1953. hung &= !kick_ring(ring);
  1954. }
  1955. return hung;
  1956. }
  1957. return false;
  1958. }
  1959. /**
  1960. * This is called when the chip hasn't reported back with completed
  1961. * batchbuffers in a long time. The first time this is called we simply record
  1962. * ACTHD. If ACTHD hasn't changed by the time the hangcheck timer elapses
  1963. * again, we assume the chip is wedged and try to fix it.
  1964. */
  1965. void i915_hangcheck_elapsed(unsigned long data)
  1966. {
  1967. struct drm_device *dev = (struct drm_device *)data;
  1968. drm_i915_private_t *dev_priv = dev->dev_private;
  1969. uint32_t acthd[I915_NUM_RINGS], instdone[I915_NUM_INSTDONE_REG];
  1970. struct intel_ring_buffer *ring;
  1971. bool err = false, idle;
  1972. int i;
  1973. if (!i915_enable_hangcheck)
  1974. return;
  1975. memset(acthd, 0, sizeof(acthd));
  1976. idle = true;
  1977. for_each_ring(ring, dev_priv, i) {
  1978. idle &= i915_hangcheck_ring_idle(ring, &err);
  1979. acthd[i] = intel_ring_get_active_head(ring);
  1980. }
  1981. /* If all work is done then ACTHD clearly hasn't advanced. */
  1982. if (idle) {
  1983. if (err) {
  1984. if (i915_hangcheck_hung(dev))
  1985. return;
  1986. goto repeat;
  1987. }
  1988. dev_priv->gpu_error.hangcheck_count = 0;
  1989. return;
  1990. }
  1991. i915_get_extra_instdone(dev, instdone);
  1992. if (memcmp(dev_priv->gpu_error.last_acthd, acthd,
  1993. sizeof(acthd)) == 0 &&
  1994. memcmp(dev_priv->gpu_error.prev_instdone, instdone,
  1995. sizeof(instdone)) == 0) {
  1996. if (i915_hangcheck_hung(dev))
  1997. return;
  1998. } else {
  1999. dev_priv->gpu_error.hangcheck_count = 0;
  2000. memcpy(dev_priv->gpu_error.last_acthd, acthd,
  2001. sizeof(acthd));
  2002. memcpy(dev_priv->gpu_error.prev_instdone, instdone,
  2003. sizeof(instdone));
  2004. }
  2005. repeat:
  2006. /* Reset timer case chip hangs without another request being added */
  2007. mod_timer(&dev_priv->gpu_error.hangcheck_timer,
  2008. round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
  2009. }
  2010. /* drm_dma.h hooks
  2011. */
  2012. static void ironlake_irq_preinstall(struct drm_device *dev)
  2013. {
  2014. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2015. atomic_set(&dev_priv->irq_received, 0);
  2016. I915_WRITE(HWSTAM, 0xeffe);
  2017. /* XXX hotplug from PCH */
  2018. I915_WRITE(DEIMR, 0xffffffff);
  2019. I915_WRITE(DEIER, 0x0);
  2020. POSTING_READ(DEIER);
  2021. /* and GT */
  2022. I915_WRITE(GTIMR, 0xffffffff);
  2023. I915_WRITE(GTIER, 0x0);
  2024. POSTING_READ(GTIER);
  2025. if (HAS_PCH_NOP(dev))
  2026. return;
  2027. /* south display irq */
  2028. I915_WRITE(SDEIMR, 0xffffffff);
  2029. /*
  2030. * SDEIER is also touched by the interrupt handler to work around missed
  2031. * PCH interrupts. Hence we can't update it after the interrupt handler
  2032. * is enabled - instead we unconditionally enable all PCH interrupt
  2033. * sources here, but then only unmask them as needed with SDEIMR.
  2034. */
  2035. I915_WRITE(SDEIER, 0xffffffff);
  2036. POSTING_READ(SDEIER);
  2037. }
  2038. static void valleyview_irq_preinstall(struct drm_device *dev)
  2039. {
  2040. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2041. int pipe;
  2042. atomic_set(&dev_priv->irq_received, 0);
  2043. /* VLV magic */
  2044. I915_WRITE(VLV_IMR, 0);
  2045. I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
  2046. I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
  2047. I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
  2048. /* and GT */
  2049. I915_WRITE(GTIIR, I915_READ(GTIIR));
  2050. I915_WRITE(GTIIR, I915_READ(GTIIR));
  2051. I915_WRITE(GTIMR, 0xffffffff);
  2052. I915_WRITE(GTIER, 0x0);
  2053. POSTING_READ(GTIER);
  2054. I915_WRITE(DPINVGTT, 0xff);
  2055. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2056. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  2057. for_each_pipe(pipe)
  2058. I915_WRITE(PIPESTAT(pipe), 0xffff);
  2059. I915_WRITE(VLV_IIR, 0xffffffff);
  2060. I915_WRITE(VLV_IMR, 0xffffffff);
  2061. I915_WRITE(VLV_IER, 0x0);
  2062. POSTING_READ(VLV_IER);
  2063. }
  2064. static void ibx_hpd_irq_setup(struct drm_device *dev)
  2065. {
  2066. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2067. struct drm_mode_config *mode_config = &dev->mode_config;
  2068. struct intel_encoder *intel_encoder;
  2069. u32 mask = ~I915_READ(SDEIMR);
  2070. u32 hotplug;
  2071. if (HAS_PCH_IBX(dev)) {
  2072. mask &= ~SDE_HOTPLUG_MASK;
  2073. list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
  2074. if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
  2075. mask |= hpd_ibx[intel_encoder->hpd_pin];
  2076. } else {
  2077. mask &= ~SDE_HOTPLUG_MASK_CPT;
  2078. list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
  2079. if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
  2080. mask |= hpd_cpt[intel_encoder->hpd_pin];
  2081. }
  2082. I915_WRITE(SDEIMR, ~mask);
  2083. /*
  2084. * Enable digital hotplug on the PCH, and configure the DP short pulse
  2085. * duration to 2ms (which is the minimum in the Display Port spec)
  2086. *
  2087. * This register is the same on all known PCH chips.
  2088. */
  2089. hotplug = I915_READ(PCH_PORT_HOTPLUG);
  2090. hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
  2091. hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
  2092. hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
  2093. hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
  2094. I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
  2095. }
  2096. static void ibx_irq_postinstall(struct drm_device *dev)
  2097. {
  2098. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2099. u32 mask;
  2100. if (HAS_PCH_IBX(dev)) {
  2101. mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
  2102. SDE_TRANSA_FIFO_UNDER | SDE_POISON;
  2103. } else {
  2104. mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
  2105. I915_WRITE(SERR_INT, I915_READ(SERR_INT));
  2106. }
  2107. if (HAS_PCH_NOP(dev))
  2108. return;
  2109. I915_WRITE(SDEIIR, I915_READ(SDEIIR));
  2110. I915_WRITE(SDEIMR, ~mask);
  2111. }
  2112. static int ironlake_irq_postinstall(struct drm_device *dev)
  2113. {
  2114. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2115. /* enable kind of interrupts always enabled */
  2116. u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
  2117. DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
  2118. DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
  2119. DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
  2120. u32 render_irqs;
  2121. dev_priv->irq_mask = ~display_mask;
  2122. /* should always can generate irq */
  2123. I915_WRITE(DEIIR, I915_READ(DEIIR));
  2124. I915_WRITE(DEIMR, dev_priv->irq_mask);
  2125. I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
  2126. POSTING_READ(DEIER);
  2127. dev_priv->gt_irq_mask = ~0;
  2128. I915_WRITE(GTIIR, I915_READ(GTIIR));
  2129. I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
  2130. if (IS_GEN6(dev))
  2131. render_irqs =
  2132. GT_USER_INTERRUPT |
  2133. GEN6_BSD_USER_INTERRUPT |
  2134. GEN6_BLITTER_USER_INTERRUPT;
  2135. else
  2136. render_irqs =
  2137. GT_USER_INTERRUPT |
  2138. GT_PIPE_NOTIFY |
  2139. GT_BSD_USER_INTERRUPT;
  2140. I915_WRITE(GTIER, render_irqs);
  2141. POSTING_READ(GTIER);
  2142. ibx_irq_postinstall(dev);
  2143. if (IS_IRONLAKE_M(dev)) {
  2144. /* Clear & enable PCU event interrupts */
  2145. I915_WRITE(DEIIR, DE_PCU_EVENT);
  2146. I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
  2147. ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
  2148. }
  2149. return 0;
  2150. }
  2151. static int ivybridge_irq_postinstall(struct drm_device *dev)
  2152. {
  2153. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2154. /* enable kind of interrupts always enabled */
  2155. u32 display_mask =
  2156. DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
  2157. DE_PLANEC_FLIP_DONE_IVB |
  2158. DE_PLANEB_FLIP_DONE_IVB |
  2159. DE_PLANEA_FLIP_DONE_IVB |
  2160. DE_AUX_CHANNEL_A_IVB |
  2161. DE_ERR_INT_IVB;
  2162. u32 render_irqs;
  2163. dev_priv->irq_mask = ~display_mask;
  2164. /* should always can generate irq */
  2165. I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
  2166. I915_WRITE(DEIIR, I915_READ(DEIIR));
  2167. I915_WRITE(DEIMR, dev_priv->irq_mask);
  2168. I915_WRITE(DEIER,
  2169. display_mask |
  2170. DE_PIPEC_VBLANK_IVB |
  2171. DE_PIPEB_VBLANK_IVB |
  2172. DE_PIPEA_VBLANK_IVB);
  2173. POSTING_READ(DEIER);
  2174. dev_priv->gt_irq_mask = ~GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
  2175. I915_WRITE(GTIIR, I915_READ(GTIIR));
  2176. I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
  2177. render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
  2178. GEN6_BLITTER_USER_INTERRUPT | GT_GEN7_L3_PARITY_ERROR_INTERRUPT;
  2179. I915_WRITE(GTIER, render_irqs);
  2180. POSTING_READ(GTIER);
  2181. ibx_irq_postinstall(dev);
  2182. return 0;
  2183. }
  2184. static int valleyview_irq_postinstall(struct drm_device *dev)
  2185. {
  2186. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2187. u32 enable_mask;
  2188. u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
  2189. u32 render_irqs;
  2190. enable_mask = I915_DISPLAY_PORT_INTERRUPT;
  2191. enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  2192. I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
  2193. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  2194. I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
  2195. /*
  2196. *Leave vblank interrupts masked initially. enable/disable will
  2197. * toggle them based on usage.
  2198. */
  2199. dev_priv->irq_mask = (~enable_mask) |
  2200. I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
  2201. I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
  2202. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2203. POSTING_READ(PORT_HOTPLUG_EN);
  2204. I915_WRITE(VLV_IMR, dev_priv->irq_mask);
  2205. I915_WRITE(VLV_IER, enable_mask);
  2206. I915_WRITE(VLV_IIR, 0xffffffff);
  2207. I915_WRITE(PIPESTAT(0), 0xffff);
  2208. I915_WRITE(PIPESTAT(1), 0xffff);
  2209. POSTING_READ(VLV_IER);
  2210. i915_enable_pipestat(dev_priv, 0, pipestat_enable);
  2211. i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
  2212. i915_enable_pipestat(dev_priv, 1, pipestat_enable);
  2213. I915_WRITE(VLV_IIR, 0xffffffff);
  2214. I915_WRITE(VLV_IIR, 0xffffffff);
  2215. I915_WRITE(GTIIR, I915_READ(GTIIR));
  2216. I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
  2217. render_irqs = GT_USER_INTERRUPT | GEN6_BSD_USER_INTERRUPT |
  2218. GEN6_BLITTER_USER_INTERRUPT;
  2219. I915_WRITE(GTIER, render_irqs);
  2220. POSTING_READ(GTIER);
  2221. /* ack & enable invalid PTE error interrupts */
  2222. #if 0 /* FIXME: add support to irq handler for checking these bits */
  2223. I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
  2224. I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
  2225. #endif
  2226. I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
  2227. return 0;
  2228. }
  2229. static void valleyview_irq_uninstall(struct drm_device *dev)
  2230. {
  2231. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2232. int pipe;
  2233. if (!dev_priv)
  2234. return;
  2235. del_timer_sync(&dev_priv->hotplug_reenable_timer);
  2236. for_each_pipe(pipe)
  2237. I915_WRITE(PIPESTAT(pipe), 0xffff);
  2238. I915_WRITE(HWSTAM, 0xffffffff);
  2239. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2240. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  2241. for_each_pipe(pipe)
  2242. I915_WRITE(PIPESTAT(pipe), 0xffff);
  2243. I915_WRITE(VLV_IIR, 0xffffffff);
  2244. I915_WRITE(VLV_IMR, 0xffffffff);
  2245. I915_WRITE(VLV_IER, 0x0);
  2246. POSTING_READ(VLV_IER);
  2247. }
  2248. static void ironlake_irq_uninstall(struct drm_device *dev)
  2249. {
  2250. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2251. if (!dev_priv)
  2252. return;
  2253. del_timer_sync(&dev_priv->hotplug_reenable_timer);
  2254. I915_WRITE(HWSTAM, 0xffffffff);
  2255. I915_WRITE(DEIMR, 0xffffffff);
  2256. I915_WRITE(DEIER, 0x0);
  2257. I915_WRITE(DEIIR, I915_READ(DEIIR));
  2258. if (IS_GEN7(dev))
  2259. I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
  2260. I915_WRITE(GTIMR, 0xffffffff);
  2261. I915_WRITE(GTIER, 0x0);
  2262. I915_WRITE(GTIIR, I915_READ(GTIIR));
  2263. if (HAS_PCH_NOP(dev))
  2264. return;
  2265. I915_WRITE(SDEIMR, 0xffffffff);
  2266. I915_WRITE(SDEIER, 0x0);
  2267. I915_WRITE(SDEIIR, I915_READ(SDEIIR));
  2268. if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
  2269. I915_WRITE(SERR_INT, I915_READ(SERR_INT));
  2270. }
  2271. static void i8xx_irq_preinstall(struct drm_device * dev)
  2272. {
  2273. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2274. int pipe;
  2275. atomic_set(&dev_priv->irq_received, 0);
  2276. for_each_pipe(pipe)
  2277. I915_WRITE(PIPESTAT(pipe), 0);
  2278. I915_WRITE16(IMR, 0xffff);
  2279. I915_WRITE16(IER, 0x0);
  2280. POSTING_READ16(IER);
  2281. }
  2282. static int i8xx_irq_postinstall(struct drm_device *dev)
  2283. {
  2284. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2285. I915_WRITE16(EMR,
  2286. ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
  2287. /* Unmask the interrupts that we always want on. */
  2288. dev_priv->irq_mask =
  2289. ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  2290. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  2291. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  2292. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
  2293. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  2294. I915_WRITE16(IMR, dev_priv->irq_mask);
  2295. I915_WRITE16(IER,
  2296. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  2297. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  2298. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
  2299. I915_USER_INTERRUPT);
  2300. POSTING_READ16(IER);
  2301. return 0;
  2302. }
  2303. /*
  2304. * Returns true when a page flip has completed.
  2305. */
  2306. static bool i8xx_handle_vblank(struct drm_device *dev,
  2307. int pipe, u16 iir)
  2308. {
  2309. drm_i915_private_t *dev_priv = dev->dev_private;
  2310. u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
  2311. if (!drm_handle_vblank(dev, pipe))
  2312. return false;
  2313. if ((iir & flip_pending) == 0)
  2314. return false;
  2315. intel_prepare_page_flip(dev, pipe);
  2316. /* We detect FlipDone by looking for the change in PendingFlip from '1'
  2317. * to '0' on the following vblank, i.e. IIR has the Pendingflip
  2318. * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
  2319. * the flip is completed (no longer pending). Since this doesn't raise
  2320. * an interrupt per se, we watch for the change at vblank.
  2321. */
  2322. if (I915_READ16(ISR) & flip_pending)
  2323. return false;
  2324. intel_finish_page_flip(dev, pipe);
  2325. return true;
  2326. }
  2327. static irqreturn_t i8xx_irq_handler(int irq, void *arg)
  2328. {
  2329. struct drm_device *dev = (struct drm_device *) arg;
  2330. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2331. u16 iir, new_iir;
  2332. u32 pipe_stats[2];
  2333. unsigned long irqflags;
  2334. int irq_received;
  2335. int pipe;
  2336. u16 flip_mask =
  2337. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  2338. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
  2339. atomic_inc(&dev_priv->irq_received);
  2340. iir = I915_READ16(IIR);
  2341. if (iir == 0)
  2342. return IRQ_NONE;
  2343. while (iir & ~flip_mask) {
  2344. /* Can't rely on pipestat interrupt bit in iir as it might
  2345. * have been cleared after the pipestat interrupt was received.
  2346. * It doesn't set the bit in iir again, but it still produces
  2347. * interrupts (for non-MSI).
  2348. */
  2349. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2350. if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
  2351. i915_handle_error(dev, false);
  2352. for_each_pipe(pipe) {
  2353. int reg = PIPESTAT(pipe);
  2354. pipe_stats[pipe] = I915_READ(reg);
  2355. /*
  2356. * Clear the PIPE*STAT regs before the IIR
  2357. */
  2358. if (pipe_stats[pipe] & 0x8000ffff) {
  2359. if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
  2360. DRM_DEBUG_DRIVER("pipe %c underrun\n",
  2361. pipe_name(pipe));
  2362. I915_WRITE(reg, pipe_stats[pipe]);
  2363. irq_received = 1;
  2364. }
  2365. }
  2366. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2367. I915_WRITE16(IIR, iir & ~flip_mask);
  2368. new_iir = I915_READ16(IIR); /* Flush posted writes */
  2369. i915_update_dri1_breadcrumb(dev);
  2370. if (iir & I915_USER_INTERRUPT)
  2371. notify_ring(dev, &dev_priv->ring[RCS]);
  2372. if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
  2373. i8xx_handle_vblank(dev, 0, iir))
  2374. flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
  2375. if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
  2376. i8xx_handle_vblank(dev, 1, iir))
  2377. flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
  2378. iir = new_iir;
  2379. }
  2380. return IRQ_HANDLED;
  2381. }
  2382. static void i8xx_irq_uninstall(struct drm_device * dev)
  2383. {
  2384. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2385. int pipe;
  2386. for_each_pipe(pipe) {
  2387. /* Clear enable bits; then clear status bits */
  2388. I915_WRITE(PIPESTAT(pipe), 0);
  2389. I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
  2390. }
  2391. I915_WRITE16(IMR, 0xffff);
  2392. I915_WRITE16(IER, 0x0);
  2393. I915_WRITE16(IIR, I915_READ16(IIR));
  2394. }
  2395. static void i915_irq_preinstall(struct drm_device * dev)
  2396. {
  2397. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2398. int pipe;
  2399. atomic_set(&dev_priv->irq_received, 0);
  2400. if (I915_HAS_HOTPLUG(dev)) {
  2401. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2402. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  2403. }
  2404. I915_WRITE16(HWSTAM, 0xeffe);
  2405. for_each_pipe(pipe)
  2406. I915_WRITE(PIPESTAT(pipe), 0);
  2407. I915_WRITE(IMR, 0xffffffff);
  2408. I915_WRITE(IER, 0x0);
  2409. POSTING_READ(IER);
  2410. }
  2411. static int i915_irq_postinstall(struct drm_device *dev)
  2412. {
  2413. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2414. u32 enable_mask;
  2415. I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
  2416. /* Unmask the interrupts that we always want on. */
  2417. dev_priv->irq_mask =
  2418. ~(I915_ASLE_INTERRUPT |
  2419. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  2420. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  2421. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  2422. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
  2423. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  2424. enable_mask =
  2425. I915_ASLE_INTERRUPT |
  2426. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  2427. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  2428. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
  2429. I915_USER_INTERRUPT;
  2430. if (I915_HAS_HOTPLUG(dev)) {
  2431. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2432. POSTING_READ(PORT_HOTPLUG_EN);
  2433. /* Enable in IER... */
  2434. enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
  2435. /* and unmask in IMR */
  2436. dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
  2437. }
  2438. I915_WRITE(IMR, dev_priv->irq_mask);
  2439. I915_WRITE(IER, enable_mask);
  2440. POSTING_READ(IER);
  2441. i915_enable_asle_pipestat(dev);
  2442. return 0;
  2443. }
  2444. /*
  2445. * Returns true when a page flip has completed.
  2446. */
  2447. static bool i915_handle_vblank(struct drm_device *dev,
  2448. int plane, int pipe, u32 iir)
  2449. {
  2450. drm_i915_private_t *dev_priv = dev->dev_private;
  2451. u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
  2452. if (!drm_handle_vblank(dev, pipe))
  2453. return false;
  2454. if ((iir & flip_pending) == 0)
  2455. return false;
  2456. intel_prepare_page_flip(dev, plane);
  2457. /* We detect FlipDone by looking for the change in PendingFlip from '1'
  2458. * to '0' on the following vblank, i.e. IIR has the Pendingflip
  2459. * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
  2460. * the flip is completed (no longer pending). Since this doesn't raise
  2461. * an interrupt per se, we watch for the change at vblank.
  2462. */
  2463. if (I915_READ(ISR) & flip_pending)
  2464. return false;
  2465. intel_finish_page_flip(dev, pipe);
  2466. return true;
  2467. }
  2468. static irqreturn_t i915_irq_handler(int irq, void *arg)
  2469. {
  2470. struct drm_device *dev = (struct drm_device *) arg;
  2471. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2472. u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
  2473. unsigned long irqflags;
  2474. u32 flip_mask =
  2475. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  2476. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
  2477. int pipe, ret = IRQ_NONE;
  2478. atomic_inc(&dev_priv->irq_received);
  2479. iir = I915_READ(IIR);
  2480. do {
  2481. bool irq_received = (iir & ~flip_mask) != 0;
  2482. bool blc_event = false;
  2483. /* Can't rely on pipestat interrupt bit in iir as it might
  2484. * have been cleared after the pipestat interrupt was received.
  2485. * It doesn't set the bit in iir again, but it still produces
  2486. * interrupts (for non-MSI).
  2487. */
  2488. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2489. if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
  2490. i915_handle_error(dev, false);
  2491. for_each_pipe(pipe) {
  2492. int reg = PIPESTAT(pipe);
  2493. pipe_stats[pipe] = I915_READ(reg);
  2494. /* Clear the PIPE*STAT regs before the IIR */
  2495. if (pipe_stats[pipe] & 0x8000ffff) {
  2496. if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
  2497. DRM_DEBUG_DRIVER("pipe %c underrun\n",
  2498. pipe_name(pipe));
  2499. I915_WRITE(reg, pipe_stats[pipe]);
  2500. irq_received = true;
  2501. }
  2502. }
  2503. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2504. if (!irq_received)
  2505. break;
  2506. /* Consume port. Then clear IIR or we'll miss events */
  2507. if ((I915_HAS_HOTPLUG(dev)) &&
  2508. (iir & I915_DISPLAY_PORT_INTERRUPT)) {
  2509. u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
  2510. u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
  2511. DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
  2512. hotplug_status);
  2513. if (hotplug_trigger) {
  2514. if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
  2515. i915_hpd_irq_setup(dev);
  2516. queue_work(dev_priv->wq,
  2517. &dev_priv->hotplug_work);
  2518. }
  2519. I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
  2520. POSTING_READ(PORT_HOTPLUG_STAT);
  2521. }
  2522. I915_WRITE(IIR, iir & ~flip_mask);
  2523. new_iir = I915_READ(IIR); /* Flush posted writes */
  2524. if (iir & I915_USER_INTERRUPT)
  2525. notify_ring(dev, &dev_priv->ring[RCS]);
  2526. for_each_pipe(pipe) {
  2527. int plane = pipe;
  2528. if (IS_MOBILE(dev))
  2529. plane = !plane;
  2530. if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
  2531. i915_handle_vblank(dev, plane, pipe, iir))
  2532. flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
  2533. if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
  2534. blc_event = true;
  2535. }
  2536. if (blc_event || (iir & I915_ASLE_INTERRUPT))
  2537. intel_opregion_asle_intr(dev);
  2538. /* With MSI, interrupts are only generated when iir
  2539. * transitions from zero to nonzero. If another bit got
  2540. * set while we were handling the existing iir bits, then
  2541. * we would never get another interrupt.
  2542. *
  2543. * This is fine on non-MSI as well, as if we hit this path
  2544. * we avoid exiting the interrupt handler only to generate
  2545. * another one.
  2546. *
  2547. * Note that for MSI this could cause a stray interrupt report
  2548. * if an interrupt landed in the time between writing IIR and
  2549. * the posting read. This should be rare enough to never
  2550. * trigger the 99% of 100,000 interrupts test for disabling
  2551. * stray interrupts.
  2552. */
  2553. ret = IRQ_HANDLED;
  2554. iir = new_iir;
  2555. } while (iir & ~flip_mask);
  2556. i915_update_dri1_breadcrumb(dev);
  2557. return ret;
  2558. }
  2559. static void i915_irq_uninstall(struct drm_device * dev)
  2560. {
  2561. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2562. int pipe;
  2563. del_timer_sync(&dev_priv->hotplug_reenable_timer);
  2564. if (I915_HAS_HOTPLUG(dev)) {
  2565. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2566. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  2567. }
  2568. I915_WRITE16(HWSTAM, 0xffff);
  2569. for_each_pipe(pipe) {
  2570. /* Clear enable bits; then clear status bits */
  2571. I915_WRITE(PIPESTAT(pipe), 0);
  2572. I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
  2573. }
  2574. I915_WRITE(IMR, 0xffffffff);
  2575. I915_WRITE(IER, 0x0);
  2576. I915_WRITE(IIR, I915_READ(IIR));
  2577. }
  2578. static void i965_irq_preinstall(struct drm_device * dev)
  2579. {
  2580. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2581. int pipe;
  2582. atomic_set(&dev_priv->irq_received, 0);
  2583. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2584. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  2585. I915_WRITE(HWSTAM, 0xeffe);
  2586. for_each_pipe(pipe)
  2587. I915_WRITE(PIPESTAT(pipe), 0);
  2588. I915_WRITE(IMR, 0xffffffff);
  2589. I915_WRITE(IER, 0x0);
  2590. POSTING_READ(IER);
  2591. }
  2592. static int i965_irq_postinstall(struct drm_device *dev)
  2593. {
  2594. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2595. u32 enable_mask;
  2596. u32 error_mask;
  2597. /* Unmask the interrupts that we always want on. */
  2598. dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
  2599. I915_DISPLAY_PORT_INTERRUPT |
  2600. I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
  2601. I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
  2602. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  2603. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
  2604. I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
  2605. enable_mask = ~dev_priv->irq_mask;
  2606. enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  2607. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
  2608. enable_mask |= I915_USER_INTERRUPT;
  2609. if (IS_G4X(dev))
  2610. enable_mask |= I915_BSD_USER_INTERRUPT;
  2611. i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
  2612. /*
  2613. * Enable some error detection, note the instruction error mask
  2614. * bit is reserved, so we leave it masked.
  2615. */
  2616. if (IS_G4X(dev)) {
  2617. error_mask = ~(GM45_ERROR_PAGE_TABLE |
  2618. GM45_ERROR_MEM_PRIV |
  2619. GM45_ERROR_CP_PRIV |
  2620. I915_ERROR_MEMORY_REFRESH);
  2621. } else {
  2622. error_mask = ~(I915_ERROR_PAGE_TABLE |
  2623. I915_ERROR_MEMORY_REFRESH);
  2624. }
  2625. I915_WRITE(EMR, error_mask);
  2626. I915_WRITE(IMR, dev_priv->irq_mask);
  2627. I915_WRITE(IER, enable_mask);
  2628. POSTING_READ(IER);
  2629. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2630. POSTING_READ(PORT_HOTPLUG_EN);
  2631. i915_enable_asle_pipestat(dev);
  2632. return 0;
  2633. }
  2634. static void i915_hpd_irq_setup(struct drm_device *dev)
  2635. {
  2636. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2637. struct drm_mode_config *mode_config = &dev->mode_config;
  2638. struct intel_encoder *intel_encoder;
  2639. u32 hotplug_en;
  2640. if (I915_HAS_HOTPLUG(dev)) {
  2641. hotplug_en = I915_READ(PORT_HOTPLUG_EN);
  2642. hotplug_en &= ~HOTPLUG_INT_EN_MASK;
  2643. /* Note HDMI and DP share hotplug bits */
  2644. /* enable bits are the same for all generations */
  2645. list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
  2646. if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
  2647. hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
  2648. /* Programming the CRT detection parameters tends
  2649. to generate a spurious hotplug event about three
  2650. seconds later. So just do it once.
  2651. */
  2652. if (IS_G4X(dev))
  2653. hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
  2654. hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
  2655. hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
  2656. /* Ignore TV since it's buggy */
  2657. I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
  2658. }
  2659. }
  2660. static irqreturn_t i965_irq_handler(int irq, void *arg)
  2661. {
  2662. struct drm_device *dev = (struct drm_device *) arg;
  2663. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2664. u32 iir, new_iir;
  2665. u32 pipe_stats[I915_MAX_PIPES];
  2666. unsigned long irqflags;
  2667. int irq_received;
  2668. int ret = IRQ_NONE, pipe;
  2669. u32 flip_mask =
  2670. I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
  2671. I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
  2672. atomic_inc(&dev_priv->irq_received);
  2673. iir = I915_READ(IIR);
  2674. for (;;) {
  2675. bool blc_event = false;
  2676. irq_received = (iir & ~flip_mask) != 0;
  2677. /* Can't rely on pipestat interrupt bit in iir as it might
  2678. * have been cleared after the pipestat interrupt was received.
  2679. * It doesn't set the bit in iir again, but it still produces
  2680. * interrupts (for non-MSI).
  2681. */
  2682. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2683. if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
  2684. i915_handle_error(dev, false);
  2685. for_each_pipe(pipe) {
  2686. int reg = PIPESTAT(pipe);
  2687. pipe_stats[pipe] = I915_READ(reg);
  2688. /*
  2689. * Clear the PIPE*STAT regs before the IIR
  2690. */
  2691. if (pipe_stats[pipe] & 0x8000ffff) {
  2692. if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
  2693. DRM_DEBUG_DRIVER("pipe %c underrun\n",
  2694. pipe_name(pipe));
  2695. I915_WRITE(reg, pipe_stats[pipe]);
  2696. irq_received = 1;
  2697. }
  2698. }
  2699. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2700. if (!irq_received)
  2701. break;
  2702. ret = IRQ_HANDLED;
  2703. /* Consume port. Then clear IIR or we'll miss events */
  2704. if (iir & I915_DISPLAY_PORT_INTERRUPT) {
  2705. u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
  2706. u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
  2707. HOTPLUG_INT_STATUS_G4X :
  2708. HOTPLUG_INT_STATUS_I965);
  2709. DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
  2710. hotplug_status);
  2711. if (hotplug_trigger) {
  2712. if (hotplug_irq_storm_detect(dev, hotplug_trigger,
  2713. IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i965))
  2714. i915_hpd_irq_setup(dev);
  2715. queue_work(dev_priv->wq,
  2716. &dev_priv->hotplug_work);
  2717. }
  2718. I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
  2719. I915_READ(PORT_HOTPLUG_STAT);
  2720. }
  2721. I915_WRITE(IIR, iir & ~flip_mask);
  2722. new_iir = I915_READ(IIR); /* Flush posted writes */
  2723. if (iir & I915_USER_INTERRUPT)
  2724. notify_ring(dev, &dev_priv->ring[RCS]);
  2725. if (iir & I915_BSD_USER_INTERRUPT)
  2726. notify_ring(dev, &dev_priv->ring[VCS]);
  2727. for_each_pipe(pipe) {
  2728. if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
  2729. i915_handle_vblank(dev, pipe, pipe, iir))
  2730. flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
  2731. if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
  2732. blc_event = true;
  2733. }
  2734. if (blc_event || (iir & I915_ASLE_INTERRUPT))
  2735. intel_opregion_asle_intr(dev);
  2736. if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
  2737. gmbus_irq_handler(dev);
  2738. /* With MSI, interrupts are only generated when iir
  2739. * transitions from zero to nonzero. If another bit got
  2740. * set while we were handling the existing iir bits, then
  2741. * we would never get another interrupt.
  2742. *
  2743. * This is fine on non-MSI as well, as if we hit this path
  2744. * we avoid exiting the interrupt handler only to generate
  2745. * another one.
  2746. *
  2747. * Note that for MSI this could cause a stray interrupt report
  2748. * if an interrupt landed in the time between writing IIR and
  2749. * the posting read. This should be rare enough to never
  2750. * trigger the 99% of 100,000 interrupts test for disabling
  2751. * stray interrupts.
  2752. */
  2753. iir = new_iir;
  2754. }
  2755. i915_update_dri1_breadcrumb(dev);
  2756. return ret;
  2757. }
  2758. static void i965_irq_uninstall(struct drm_device * dev)
  2759. {
  2760. drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
  2761. int pipe;
  2762. if (!dev_priv)
  2763. return;
  2764. del_timer_sync(&dev_priv->hotplug_reenable_timer);
  2765. I915_WRITE(PORT_HOTPLUG_EN, 0);
  2766. I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
  2767. I915_WRITE(HWSTAM, 0xffffffff);
  2768. for_each_pipe(pipe)
  2769. I915_WRITE(PIPESTAT(pipe), 0);
  2770. I915_WRITE(IMR, 0xffffffff);
  2771. I915_WRITE(IER, 0x0);
  2772. for_each_pipe(pipe)
  2773. I915_WRITE(PIPESTAT(pipe),
  2774. I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
  2775. I915_WRITE(IIR, I915_READ(IIR));
  2776. }
  2777. static void i915_reenable_hotplug_timer_func(unsigned long data)
  2778. {
  2779. drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
  2780. struct drm_device *dev = dev_priv->dev;
  2781. struct drm_mode_config *mode_config = &dev->mode_config;
  2782. unsigned long irqflags;
  2783. int i;
  2784. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  2785. for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
  2786. struct drm_connector *connector;
  2787. if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
  2788. continue;
  2789. dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
  2790. list_for_each_entry(connector, &mode_config->connector_list, head) {
  2791. struct intel_connector *intel_connector = to_intel_connector(connector);
  2792. if (intel_connector->encoder->hpd_pin == i) {
  2793. if (connector->polled != intel_connector->polled)
  2794. DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
  2795. drm_get_connector_name(connector));
  2796. connector->polled = intel_connector->polled;
  2797. if (!connector->polled)
  2798. connector->polled = DRM_CONNECTOR_POLL_HPD;
  2799. }
  2800. }
  2801. }
  2802. if (dev_priv->display.hpd_irq_setup)
  2803. dev_priv->display.hpd_irq_setup(dev);
  2804. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  2805. }
  2806. void intel_irq_init(struct drm_device *dev)
  2807. {
  2808. struct drm_i915_private *dev_priv = dev->dev_private;
  2809. INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
  2810. INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
  2811. INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
  2812. INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
  2813. setup_timer(&dev_priv->gpu_error.hangcheck_timer,
  2814. i915_hangcheck_elapsed,
  2815. (unsigned long) dev);
  2816. setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
  2817. (unsigned long) dev_priv);
  2818. pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
  2819. dev->driver->get_vblank_counter = i915_get_vblank_counter;
  2820. dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
  2821. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  2822. dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
  2823. dev->driver->get_vblank_counter = gm45_get_vblank_counter;
  2824. }
  2825. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2826. dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
  2827. else
  2828. dev->driver->get_vblank_timestamp = NULL;
  2829. dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
  2830. if (IS_VALLEYVIEW(dev)) {
  2831. dev->driver->irq_handler = valleyview_irq_handler;
  2832. dev->driver->irq_preinstall = valleyview_irq_preinstall;
  2833. dev->driver->irq_postinstall = valleyview_irq_postinstall;
  2834. dev->driver->irq_uninstall = valleyview_irq_uninstall;
  2835. dev->driver->enable_vblank = valleyview_enable_vblank;
  2836. dev->driver->disable_vblank = valleyview_disable_vblank;
  2837. dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
  2838. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  2839. /* Share pre & uninstall handlers with ILK/SNB */
  2840. dev->driver->irq_handler = ivybridge_irq_handler;
  2841. dev->driver->irq_preinstall = ironlake_irq_preinstall;
  2842. dev->driver->irq_postinstall = ivybridge_irq_postinstall;
  2843. dev->driver->irq_uninstall = ironlake_irq_uninstall;
  2844. dev->driver->enable_vblank = ivybridge_enable_vblank;
  2845. dev->driver->disable_vblank = ivybridge_disable_vblank;
  2846. dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
  2847. } else if (HAS_PCH_SPLIT(dev)) {
  2848. dev->driver->irq_handler = ironlake_irq_handler;
  2849. dev->driver->irq_preinstall = ironlake_irq_preinstall;
  2850. dev->driver->irq_postinstall = ironlake_irq_postinstall;
  2851. dev->driver->irq_uninstall = ironlake_irq_uninstall;
  2852. dev->driver->enable_vblank = ironlake_enable_vblank;
  2853. dev->driver->disable_vblank = ironlake_disable_vblank;
  2854. dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
  2855. } else {
  2856. if (INTEL_INFO(dev)->gen == 2) {
  2857. dev->driver->irq_preinstall = i8xx_irq_preinstall;
  2858. dev->driver->irq_postinstall = i8xx_irq_postinstall;
  2859. dev->driver->irq_handler = i8xx_irq_handler;
  2860. dev->driver->irq_uninstall = i8xx_irq_uninstall;
  2861. } else if (INTEL_INFO(dev)->gen == 3) {
  2862. dev->driver->irq_preinstall = i915_irq_preinstall;
  2863. dev->driver->irq_postinstall = i915_irq_postinstall;
  2864. dev->driver->irq_uninstall = i915_irq_uninstall;
  2865. dev->driver->irq_handler = i915_irq_handler;
  2866. dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
  2867. } else {
  2868. dev->driver->irq_preinstall = i965_irq_preinstall;
  2869. dev->driver->irq_postinstall = i965_irq_postinstall;
  2870. dev->driver->irq_uninstall = i965_irq_uninstall;
  2871. dev->driver->irq_handler = i965_irq_handler;
  2872. dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
  2873. }
  2874. dev->driver->enable_vblank = i915_enable_vblank;
  2875. dev->driver->disable_vblank = i915_disable_vblank;
  2876. }
  2877. }
  2878. void intel_hpd_init(struct drm_device *dev)
  2879. {
  2880. struct drm_i915_private *dev_priv = dev->dev_private;
  2881. struct drm_mode_config *mode_config = &dev->mode_config;
  2882. struct drm_connector *connector;
  2883. int i;
  2884. for (i = 1; i < HPD_NUM_PINS; i++) {
  2885. dev_priv->hpd_stats[i].hpd_cnt = 0;
  2886. dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
  2887. }
  2888. list_for_each_entry(connector, &mode_config->connector_list, head) {
  2889. struct intel_connector *intel_connector = to_intel_connector(connector);
  2890. connector->polled = intel_connector->polled;
  2891. if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
  2892. connector->polled = DRM_CONNECTOR_POLL_HPD;
  2893. }
  2894. if (dev_priv->display.hpd_irq_setup)
  2895. dev_priv->display.hpd_irq_setup(dev);
  2896. }