i915_irq.c 100 KB

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