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