drm/i915: Remove some legacy mmio accessors from interrupt handling
[linux-2.6-microblaze.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/circ_buf.h>
32 #include <linux/cpuidle.h>
33 #include <linux/slab.h>
34 #include <linux/sysrq.h>
35
36 #include <drm/drm_drv.h>
37 #include <drm/drm_irq.h>
38 #include <drm/i915_drm.h>
39
40 #include "display/intel_fifo_underrun.h"
41 #include "display/intel_hotplug.h"
42 #include "display/intel_lpe_audio.h"
43 #include "display/intel_psr.h"
44
45 #include "i915_drv.h"
46 #include "i915_irq.h"
47 #include "i915_trace.h"
48 #include "intel_drv.h"
49 #include "intel_pm.h"
50
51 /**
52  * DOC: interrupt handling
53  *
54  * These functions provide the basic support for enabling and disabling the
55  * interrupt handling support. There's a lot more functionality in i915_irq.c
56  * and related files, but that will be described in separate chapters.
57  */
58
59 static const u32 hpd_ilk[HPD_NUM_PINS] = {
60         [HPD_PORT_A] = DE_DP_A_HOTPLUG,
61 };
62
63 static const u32 hpd_ivb[HPD_NUM_PINS] = {
64         [HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
65 };
66
67 static const u32 hpd_bdw[HPD_NUM_PINS] = {
68         [HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
69 };
70
71 static const u32 hpd_ibx[HPD_NUM_PINS] = {
72         [HPD_CRT] = SDE_CRT_HOTPLUG,
73         [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
74         [HPD_PORT_B] = SDE_PORTB_HOTPLUG,
75         [HPD_PORT_C] = SDE_PORTC_HOTPLUG,
76         [HPD_PORT_D] = SDE_PORTD_HOTPLUG
77 };
78
79 static const u32 hpd_cpt[HPD_NUM_PINS] = {
80         [HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
81         [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
82         [HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
83         [HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
84         [HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
85 };
86
87 static const u32 hpd_spt[HPD_NUM_PINS] = {
88         [HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
89         [HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
90         [HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
91         [HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
92         [HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
93 };
94
95 static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
96         [HPD_CRT] = CRT_HOTPLUG_INT_EN,
97         [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
98         [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
99         [HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
100         [HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
101         [HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
102 };
103
104 static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
105         [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
106         [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
107         [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
108         [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
109         [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
110         [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
111 };
112
113 static const u32 hpd_status_i915[HPD_NUM_PINS] = {
114         [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
115         [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
116         [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
117         [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
118         [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
119         [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
120 };
121
122 /* BXT hpd list */
123 static const u32 hpd_bxt[HPD_NUM_PINS] = {
124         [HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
125         [HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
126         [HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
127 };
128
129 static const u32 hpd_gen11[HPD_NUM_PINS] = {
130         [HPD_PORT_C] = GEN11_TC1_HOTPLUG | GEN11_TBT1_HOTPLUG,
131         [HPD_PORT_D] = GEN11_TC2_HOTPLUG | GEN11_TBT2_HOTPLUG,
132         [HPD_PORT_E] = GEN11_TC3_HOTPLUG | GEN11_TBT3_HOTPLUG,
133         [HPD_PORT_F] = GEN11_TC4_HOTPLUG | GEN11_TBT4_HOTPLUG
134 };
135
136 static const u32 hpd_icp[HPD_NUM_PINS] = {
137         [HPD_PORT_A] = SDE_DDIA_HOTPLUG_ICP,
138         [HPD_PORT_B] = SDE_DDIB_HOTPLUG_ICP,
139         [HPD_PORT_C] = SDE_TC1_HOTPLUG_ICP,
140         [HPD_PORT_D] = SDE_TC2_HOTPLUG_ICP,
141         [HPD_PORT_E] = SDE_TC3_HOTPLUG_ICP,
142         [HPD_PORT_F] = SDE_TC4_HOTPLUG_ICP
143 };
144
145 static const u32 hpd_mcc[HPD_NUM_PINS] = {
146         [HPD_PORT_A] = SDE_DDIA_HOTPLUG_ICP,
147         [HPD_PORT_B] = SDE_DDIB_HOTPLUG_ICP,
148         [HPD_PORT_C] = SDE_TC1_HOTPLUG_ICP
149 };
150
151 static void gen3_irq_reset(struct intel_uncore *uncore, i915_reg_t imr,
152                            i915_reg_t iir, i915_reg_t ier)
153 {
154         intel_uncore_write(uncore, imr, 0xffffffff);
155         intel_uncore_posting_read(uncore, imr);
156
157         intel_uncore_write(uncore, ier, 0);
158
159         /* IIR can theoretically queue up two events. Be paranoid. */
160         intel_uncore_write(uncore, iir, 0xffffffff);
161         intel_uncore_posting_read(uncore, iir);
162         intel_uncore_write(uncore, iir, 0xffffffff);
163         intel_uncore_posting_read(uncore, iir);
164 }
165
166 static void gen2_irq_reset(struct intel_uncore *uncore)
167 {
168         intel_uncore_write16(uncore, GEN2_IMR, 0xffff);
169         intel_uncore_posting_read16(uncore, GEN2_IMR);
170
171         intel_uncore_write16(uncore, GEN2_IER, 0);
172
173         /* IIR can theoretically queue up two events. Be paranoid. */
174         intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
175         intel_uncore_posting_read16(uncore, GEN2_IIR);
176         intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
177         intel_uncore_posting_read16(uncore, GEN2_IIR);
178 }
179
180 #define GEN8_IRQ_RESET_NDX(uncore, type, which) \
181 ({ \
182         unsigned int which_ = which; \
183         gen3_irq_reset((uncore), GEN8_##type##_IMR(which_), \
184                        GEN8_##type##_IIR(which_), GEN8_##type##_IER(which_)); \
185 })
186
187 #define GEN3_IRQ_RESET(uncore, type) \
188         gen3_irq_reset((uncore), type##IMR, type##IIR, type##IER)
189
190 #define GEN2_IRQ_RESET(uncore) \
191         gen2_irq_reset(uncore)
192
193 /*
194  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
195  */
196 static void gen3_assert_iir_is_zero(struct intel_uncore *uncore, i915_reg_t reg)
197 {
198         u32 val = intel_uncore_read(uncore, reg);
199
200         if (val == 0)
201                 return;
202
203         WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
204              i915_mmio_reg_offset(reg), val);
205         intel_uncore_write(uncore, reg, 0xffffffff);
206         intel_uncore_posting_read(uncore, reg);
207         intel_uncore_write(uncore, reg, 0xffffffff);
208         intel_uncore_posting_read(uncore, reg);
209 }
210
211 static void gen2_assert_iir_is_zero(struct intel_uncore *uncore)
212 {
213         u16 val = intel_uncore_read16(uncore, GEN2_IIR);
214
215         if (val == 0)
216                 return;
217
218         WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
219              i915_mmio_reg_offset(GEN2_IIR), val);
220         intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
221         intel_uncore_posting_read16(uncore, GEN2_IIR);
222         intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
223         intel_uncore_posting_read16(uncore, GEN2_IIR);
224 }
225
226 static void gen3_irq_init(struct intel_uncore *uncore,
227                           i915_reg_t imr, u32 imr_val,
228                           i915_reg_t ier, u32 ier_val,
229                           i915_reg_t iir)
230 {
231         gen3_assert_iir_is_zero(uncore, iir);
232
233         intel_uncore_write(uncore, ier, ier_val);
234         intel_uncore_write(uncore, imr, imr_val);
235         intel_uncore_posting_read(uncore, imr);
236 }
237
238 static void gen2_irq_init(struct intel_uncore *uncore,
239                           u32 imr_val, u32 ier_val)
240 {
241         gen2_assert_iir_is_zero(uncore);
242
243         intel_uncore_write16(uncore, GEN2_IER, ier_val);
244         intel_uncore_write16(uncore, GEN2_IMR, imr_val);
245         intel_uncore_posting_read16(uncore, GEN2_IMR);
246 }
247
248 #define GEN8_IRQ_INIT_NDX(uncore, type, which, imr_val, ier_val) \
249 ({ \
250         unsigned int which_ = which; \
251         gen3_irq_init((uncore), \
252                       GEN8_##type##_IMR(which_), imr_val, \
253                       GEN8_##type##_IER(which_), ier_val, \
254                       GEN8_##type##_IIR(which_)); \
255 })
256
257 #define GEN3_IRQ_INIT(uncore, type, imr_val, ier_val) \
258         gen3_irq_init((uncore), \
259                       type##IMR, imr_val, \
260                       type##IER, ier_val, \
261                       type##IIR)
262
263 #define GEN2_IRQ_INIT(uncore, imr_val, ier_val) \
264         gen2_irq_init((uncore), imr_val, ier_val)
265
266 static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
267 static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
268
269 /* For display hotplug interrupt */
270 static inline void
271 i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
272                                      u32 mask,
273                                      u32 bits)
274 {
275         u32 val;
276
277         lockdep_assert_held(&dev_priv->irq_lock);
278         WARN_ON(bits & ~mask);
279
280         val = I915_READ(PORT_HOTPLUG_EN);
281         val &= ~mask;
282         val |= bits;
283         I915_WRITE(PORT_HOTPLUG_EN, val);
284 }
285
286 /**
287  * i915_hotplug_interrupt_update - update hotplug interrupt enable
288  * @dev_priv: driver private
289  * @mask: bits to update
290  * @bits: bits to enable
291  * NOTE: the HPD enable bits are modified both inside and outside
292  * of an interrupt context. To avoid that read-modify-write cycles
293  * interfer, these bits are protected by a spinlock. Since this
294  * function is usually not called from a context where the lock is
295  * held already, this function acquires the lock itself. A non-locking
296  * version is also available.
297  */
298 void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
299                                    u32 mask,
300                                    u32 bits)
301 {
302         spin_lock_irq(&dev_priv->irq_lock);
303         i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
304         spin_unlock_irq(&dev_priv->irq_lock);
305 }
306
307 static u32
308 gen11_gt_engine_identity(struct intel_gt *gt,
309                          const unsigned int bank, const unsigned int bit);
310
311 static bool gen11_reset_one_iir(struct intel_gt *gt,
312                                 const unsigned int bank,
313                                 const unsigned int bit)
314 {
315         void __iomem * const regs = gt->uncore->regs;
316         u32 dw;
317
318         lockdep_assert_held(&gt->i915->irq_lock);
319
320         dw = raw_reg_read(regs, GEN11_GT_INTR_DW(bank));
321         if (dw & BIT(bit)) {
322                 /*
323                  * According to the BSpec, DW_IIR bits cannot be cleared without
324                  * first servicing the Selector & Shared IIR registers.
325                  */
326                 gen11_gt_engine_identity(gt, bank, bit);
327
328                 /*
329                  * We locked GT INT DW by reading it. If we want to (try
330                  * to) recover from this succesfully, we need to clear
331                  * our bit, otherwise we are locking the register for
332                  * everybody.
333                  */
334                 raw_reg_write(regs, GEN11_GT_INTR_DW(bank), BIT(bit));
335
336                 return true;
337         }
338
339         return false;
340 }
341
342 /**
343  * ilk_update_display_irq - update DEIMR
344  * @dev_priv: driver private
345  * @interrupt_mask: mask of interrupt bits to update
346  * @enabled_irq_mask: mask of interrupt bits to enable
347  */
348 void ilk_update_display_irq(struct drm_i915_private *dev_priv,
349                             u32 interrupt_mask,
350                             u32 enabled_irq_mask)
351 {
352         u32 new_val;
353
354         lockdep_assert_held(&dev_priv->irq_lock);
355
356         WARN_ON(enabled_irq_mask & ~interrupt_mask);
357
358         if (WARN_ON(!intel_irqs_enabled(dev_priv)))
359                 return;
360
361         new_val = dev_priv->irq_mask;
362         new_val &= ~interrupt_mask;
363         new_val |= (~enabled_irq_mask & interrupt_mask);
364
365         if (new_val != dev_priv->irq_mask) {
366                 dev_priv->irq_mask = new_val;
367                 I915_WRITE(DEIMR, dev_priv->irq_mask);
368                 POSTING_READ(DEIMR);
369         }
370 }
371
372 /**
373  * ilk_update_gt_irq - update GTIMR
374  * @dev_priv: driver private
375  * @interrupt_mask: mask of interrupt bits to update
376  * @enabled_irq_mask: mask of interrupt bits to enable
377  */
378 static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
379                               u32 interrupt_mask,
380                               u32 enabled_irq_mask)
381 {
382         lockdep_assert_held(&dev_priv->irq_lock);
383
384         WARN_ON(enabled_irq_mask & ~interrupt_mask);
385
386         if (WARN_ON(!intel_irqs_enabled(dev_priv)))
387                 return;
388
389         dev_priv->gt_irq_mask &= ~interrupt_mask;
390         dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
391         I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
392 }
393
394 void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, u32 mask)
395 {
396         ilk_update_gt_irq(dev_priv, mask, mask);
397         intel_uncore_posting_read_fw(&dev_priv->uncore, GTIMR);
398 }
399
400 void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, u32 mask)
401 {
402         ilk_update_gt_irq(dev_priv, mask, 0);
403 }
404
405 static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
406 {
407         WARN_ON_ONCE(INTEL_GEN(dev_priv) >= 11);
408
409         return INTEL_GEN(dev_priv) >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
410 }
411
412 static void write_pm_imr(struct drm_i915_private *dev_priv)
413 {
414         i915_reg_t reg;
415         u32 mask = dev_priv->pm_imr;
416
417         if (INTEL_GEN(dev_priv) >= 11) {
418                 reg = GEN11_GPM_WGBOXPERF_INTR_MASK;
419                 /* pm is in upper half */
420                 mask = mask << 16;
421         } else if (INTEL_GEN(dev_priv) >= 8) {
422                 reg = GEN8_GT_IMR(2);
423         } else {
424                 reg = GEN6_PMIMR;
425         }
426
427         I915_WRITE(reg, mask);
428         POSTING_READ(reg);
429 }
430
431 static void write_pm_ier(struct drm_i915_private *dev_priv)
432 {
433         i915_reg_t reg;
434         u32 mask = dev_priv->pm_ier;
435
436         if (INTEL_GEN(dev_priv) >= 11) {
437                 reg = GEN11_GPM_WGBOXPERF_INTR_ENABLE;
438                 /* pm is in upper half */
439                 mask = mask << 16;
440         } else if (INTEL_GEN(dev_priv) >= 8) {
441                 reg = GEN8_GT_IER(2);
442         } else {
443                 reg = GEN6_PMIER;
444         }
445
446         I915_WRITE(reg, mask);
447 }
448
449 /**
450  * snb_update_pm_irq - update GEN6_PMIMR
451  * @dev_priv: driver private
452  * @interrupt_mask: mask of interrupt bits to update
453  * @enabled_irq_mask: mask of interrupt bits to enable
454  */
455 static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
456                               u32 interrupt_mask,
457                               u32 enabled_irq_mask)
458 {
459         u32 new_val;
460
461         WARN_ON(enabled_irq_mask & ~interrupt_mask);
462
463         lockdep_assert_held(&dev_priv->irq_lock);
464
465         new_val = dev_priv->pm_imr;
466         new_val &= ~interrupt_mask;
467         new_val |= (~enabled_irq_mask & interrupt_mask);
468
469         if (new_val != dev_priv->pm_imr) {
470                 dev_priv->pm_imr = new_val;
471                 write_pm_imr(dev_priv);
472         }
473 }
474
475 void gen6_unmask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
476 {
477         if (WARN_ON(!intel_irqs_enabled(dev_priv)))
478                 return;
479
480         snb_update_pm_irq(dev_priv, mask, mask);
481 }
482
483 static void __gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
484 {
485         snb_update_pm_irq(dev_priv, mask, 0);
486 }
487
488 void gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
489 {
490         if (WARN_ON(!intel_irqs_enabled(dev_priv)))
491                 return;
492
493         __gen6_mask_pm_irq(dev_priv, mask);
494 }
495
496 static void gen6_reset_pm_iir(struct drm_i915_private *dev_priv, u32 reset_mask)
497 {
498         i915_reg_t reg = gen6_pm_iir(dev_priv);
499
500         lockdep_assert_held(&dev_priv->irq_lock);
501
502         I915_WRITE(reg, reset_mask);
503         I915_WRITE(reg, reset_mask);
504         POSTING_READ(reg);
505 }
506
507 static void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, u32 enable_mask)
508 {
509         lockdep_assert_held(&dev_priv->irq_lock);
510
511         dev_priv->pm_ier |= enable_mask;
512         write_pm_ier(dev_priv);
513         gen6_unmask_pm_irq(dev_priv, enable_mask);
514         /* unmask_pm_irq provides an implicit barrier (POSTING_READ) */
515 }
516
517 static void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, u32 disable_mask)
518 {
519         lockdep_assert_held(&dev_priv->irq_lock);
520
521         dev_priv->pm_ier &= ~disable_mask;
522         __gen6_mask_pm_irq(dev_priv, disable_mask);
523         write_pm_ier(dev_priv);
524         /* though a barrier is missing here, but don't really need a one */
525 }
526
527 void gen11_reset_rps_interrupts(struct drm_i915_private *dev_priv)
528 {
529         spin_lock_irq(&dev_priv->irq_lock);
530
531         while (gen11_reset_one_iir(&dev_priv->gt, 0, GEN11_GTPM))
532                 ;
533
534         dev_priv->gt_pm.rps.pm_iir = 0;
535
536         spin_unlock_irq(&dev_priv->irq_lock);
537 }
538
539 void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
540 {
541         spin_lock_irq(&dev_priv->irq_lock);
542         gen6_reset_pm_iir(dev_priv, GEN6_PM_RPS_EVENTS);
543         dev_priv->gt_pm.rps.pm_iir = 0;
544         spin_unlock_irq(&dev_priv->irq_lock);
545 }
546
547 void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
548 {
549         struct intel_rps *rps = &dev_priv->gt_pm.rps;
550
551         if (READ_ONCE(rps->interrupts_enabled))
552                 return;
553
554         spin_lock_irq(&dev_priv->irq_lock);
555         WARN_ON_ONCE(rps->pm_iir);
556
557         if (INTEL_GEN(dev_priv) >= 11)
558                 WARN_ON_ONCE(gen11_reset_one_iir(&dev_priv->gt, 0, GEN11_GTPM));
559         else
560                 WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
561
562         rps->interrupts_enabled = true;
563         gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
564
565         spin_unlock_irq(&dev_priv->irq_lock);
566 }
567
568 void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
569 {
570         struct intel_rps *rps = &dev_priv->gt_pm.rps;
571
572         if (!READ_ONCE(rps->interrupts_enabled))
573                 return;
574
575         spin_lock_irq(&dev_priv->irq_lock);
576         rps->interrupts_enabled = false;
577
578         I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
579
580         gen6_disable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
581
582         spin_unlock_irq(&dev_priv->irq_lock);
583         intel_synchronize_irq(dev_priv);
584
585         /* Now that we will not be generating any more work, flush any
586          * outstanding tasks. As we are called on the RPS idle path,
587          * we will reset the GPU to minimum frequencies, so the current
588          * state of the worker can be discarded.
589          */
590         cancel_work_sync(&rps->work);
591         if (INTEL_GEN(dev_priv) >= 11)
592                 gen11_reset_rps_interrupts(dev_priv);
593         else
594                 gen6_reset_rps_interrupts(dev_priv);
595 }
596
597 void gen9_reset_guc_interrupts(struct drm_i915_private *dev_priv)
598 {
599         assert_rpm_wakelock_held(&dev_priv->runtime_pm);
600
601         spin_lock_irq(&dev_priv->irq_lock);
602         gen6_reset_pm_iir(dev_priv, dev_priv->pm_guc_events);
603         spin_unlock_irq(&dev_priv->irq_lock);
604 }
605
606 void gen9_enable_guc_interrupts(struct drm_i915_private *dev_priv)
607 {
608         assert_rpm_wakelock_held(&dev_priv->runtime_pm);
609
610         spin_lock_irq(&dev_priv->irq_lock);
611         if (!dev_priv->guc.interrupts.enabled) {
612                 WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) &
613                                        dev_priv->pm_guc_events);
614                 dev_priv->guc.interrupts.enabled = true;
615                 gen6_enable_pm_irq(dev_priv, dev_priv->pm_guc_events);
616         }
617         spin_unlock_irq(&dev_priv->irq_lock);
618 }
619
620 void gen9_disable_guc_interrupts(struct drm_i915_private *dev_priv)
621 {
622         assert_rpm_wakelock_held(&dev_priv->runtime_pm);
623
624         spin_lock_irq(&dev_priv->irq_lock);
625         dev_priv->guc.interrupts.enabled = false;
626
627         gen6_disable_pm_irq(dev_priv, dev_priv->pm_guc_events);
628
629         spin_unlock_irq(&dev_priv->irq_lock);
630         intel_synchronize_irq(dev_priv);
631
632         gen9_reset_guc_interrupts(dev_priv);
633 }
634
635 void gen11_reset_guc_interrupts(struct drm_i915_private *i915)
636 {
637         spin_lock_irq(&i915->irq_lock);
638         gen11_reset_one_iir(&i915->gt, 0, GEN11_GUC);
639         spin_unlock_irq(&i915->irq_lock);
640 }
641
642 void gen11_enable_guc_interrupts(struct drm_i915_private *dev_priv)
643 {
644         spin_lock_irq(&dev_priv->irq_lock);
645         if (!dev_priv->guc.interrupts.enabled) {
646                 u32 events = REG_FIELD_PREP(ENGINE1_MASK,
647                                             GEN11_GUC_INTR_GUC2HOST);
648
649                 WARN_ON_ONCE(gen11_reset_one_iir(&dev_priv->gt, 0, GEN11_GUC));
650                 I915_WRITE(GEN11_GUC_SG_INTR_ENABLE, events);
651                 I915_WRITE(GEN11_GUC_SG_INTR_MASK, ~events);
652                 dev_priv->guc.interrupts.enabled = true;
653         }
654         spin_unlock_irq(&dev_priv->irq_lock);
655 }
656
657 void gen11_disable_guc_interrupts(struct drm_i915_private *dev_priv)
658 {
659         spin_lock_irq(&dev_priv->irq_lock);
660         dev_priv->guc.interrupts.enabled = false;
661
662         I915_WRITE(GEN11_GUC_SG_INTR_MASK, ~0);
663         I915_WRITE(GEN11_GUC_SG_INTR_ENABLE, 0);
664
665         spin_unlock_irq(&dev_priv->irq_lock);
666         intel_synchronize_irq(dev_priv);
667
668         gen11_reset_guc_interrupts(dev_priv);
669 }
670
671 /**
672  * bdw_update_port_irq - update DE port interrupt
673  * @dev_priv: driver private
674  * @interrupt_mask: mask of interrupt bits to update
675  * @enabled_irq_mask: mask of interrupt bits to enable
676  */
677 static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
678                                 u32 interrupt_mask,
679                                 u32 enabled_irq_mask)
680 {
681         u32 new_val;
682         u32 old_val;
683
684         lockdep_assert_held(&dev_priv->irq_lock);
685
686         WARN_ON(enabled_irq_mask & ~interrupt_mask);
687
688         if (WARN_ON(!intel_irqs_enabled(dev_priv)))
689                 return;
690
691         old_val = I915_READ(GEN8_DE_PORT_IMR);
692
693         new_val = old_val;
694         new_val &= ~interrupt_mask;
695         new_val |= (~enabled_irq_mask & interrupt_mask);
696
697         if (new_val != old_val) {
698                 I915_WRITE(GEN8_DE_PORT_IMR, new_val);
699                 POSTING_READ(GEN8_DE_PORT_IMR);
700         }
701 }
702
703 /**
704  * bdw_update_pipe_irq - update DE pipe interrupt
705  * @dev_priv: driver private
706  * @pipe: pipe whose interrupt to update
707  * @interrupt_mask: mask of interrupt bits to update
708  * @enabled_irq_mask: mask of interrupt bits to enable
709  */
710 void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
711                          enum pipe pipe,
712                          u32 interrupt_mask,
713                          u32 enabled_irq_mask)
714 {
715         u32 new_val;
716
717         lockdep_assert_held(&dev_priv->irq_lock);
718
719         WARN_ON(enabled_irq_mask & ~interrupt_mask);
720
721         if (WARN_ON(!intel_irqs_enabled(dev_priv)))
722                 return;
723
724         new_val = dev_priv->de_irq_mask[pipe];
725         new_val &= ~interrupt_mask;
726         new_val |= (~enabled_irq_mask & interrupt_mask);
727
728         if (new_val != dev_priv->de_irq_mask[pipe]) {
729                 dev_priv->de_irq_mask[pipe] = new_val;
730                 I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
731                 POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
732         }
733 }
734
735 /**
736  * ibx_display_interrupt_update - update SDEIMR
737  * @dev_priv: driver private
738  * @interrupt_mask: mask of interrupt bits to update
739  * @enabled_irq_mask: mask of interrupt bits to enable
740  */
741 void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
742                                   u32 interrupt_mask,
743                                   u32 enabled_irq_mask)
744 {
745         u32 sdeimr = I915_READ(SDEIMR);
746         sdeimr &= ~interrupt_mask;
747         sdeimr |= (~enabled_irq_mask & interrupt_mask);
748
749         WARN_ON(enabled_irq_mask & ~interrupt_mask);
750
751         lockdep_assert_held(&dev_priv->irq_lock);
752
753         if (WARN_ON(!intel_irqs_enabled(dev_priv)))
754                 return;
755
756         I915_WRITE(SDEIMR, sdeimr);
757         POSTING_READ(SDEIMR);
758 }
759
760 u32 i915_pipestat_enable_mask(struct drm_i915_private *dev_priv,
761                               enum pipe pipe)
762 {
763         u32 status_mask = dev_priv->pipestat_irq_mask[pipe];
764         u32 enable_mask = status_mask << 16;
765
766         lockdep_assert_held(&dev_priv->irq_lock);
767
768         if (INTEL_GEN(dev_priv) < 5)
769                 goto out;
770
771         /*
772          * On pipe A we don't support the PSR interrupt yet,
773          * on pipe B and C the same bit MBZ.
774          */
775         if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
776                 return 0;
777         /*
778          * On pipe B and C we don't support the PSR interrupt yet, on pipe
779          * A the same bit is for perf counters which we don't use either.
780          */
781         if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
782                 return 0;
783
784         enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
785                          SPRITE0_FLIP_DONE_INT_EN_VLV |
786                          SPRITE1_FLIP_DONE_INT_EN_VLV);
787         if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
788                 enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
789         if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
790                 enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
791
792 out:
793         WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
794                   status_mask & ~PIPESTAT_INT_STATUS_MASK,
795                   "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
796                   pipe_name(pipe), enable_mask, status_mask);
797
798         return enable_mask;
799 }
800
801 void i915_enable_pipestat(struct drm_i915_private *dev_priv,
802                           enum pipe pipe, u32 status_mask)
803 {
804         i915_reg_t reg = PIPESTAT(pipe);
805         u32 enable_mask;
806
807         WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
808                   "pipe %c: status_mask=0x%x\n",
809                   pipe_name(pipe), status_mask);
810
811         lockdep_assert_held(&dev_priv->irq_lock);
812         WARN_ON(!intel_irqs_enabled(dev_priv));
813
814         if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == status_mask)
815                 return;
816
817         dev_priv->pipestat_irq_mask[pipe] |= status_mask;
818         enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
819
820         I915_WRITE(reg, enable_mask | status_mask);
821         POSTING_READ(reg);
822 }
823
824 void i915_disable_pipestat(struct drm_i915_private *dev_priv,
825                            enum pipe pipe, u32 status_mask)
826 {
827         i915_reg_t reg = PIPESTAT(pipe);
828         u32 enable_mask;
829
830         WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
831                   "pipe %c: status_mask=0x%x\n",
832                   pipe_name(pipe), status_mask);
833
834         lockdep_assert_held(&dev_priv->irq_lock);
835         WARN_ON(!intel_irqs_enabled(dev_priv));
836
837         if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == 0)
838                 return;
839
840         dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
841         enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
842
843         I915_WRITE(reg, enable_mask | status_mask);
844         POSTING_READ(reg);
845 }
846
847 static bool i915_has_asle(struct drm_i915_private *dev_priv)
848 {
849         if (!dev_priv->opregion.asle)
850                 return false;
851
852         return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
853 }
854
855 /**
856  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
857  * @dev_priv: i915 device private
858  */
859 static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
860 {
861         if (!i915_has_asle(dev_priv))
862                 return;
863
864         spin_lock_irq(&dev_priv->irq_lock);
865
866         i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
867         if (INTEL_GEN(dev_priv) >= 4)
868                 i915_enable_pipestat(dev_priv, PIPE_A,
869                                      PIPE_LEGACY_BLC_EVENT_STATUS);
870
871         spin_unlock_irq(&dev_priv->irq_lock);
872 }
873
874 /*
875  * This timing diagram depicts the video signal in and
876  * around the vertical blanking period.
877  *
878  * Assumptions about the fictitious mode used in this example:
879  *  vblank_start >= 3
880  *  vsync_start = vblank_start + 1
881  *  vsync_end = vblank_start + 2
882  *  vtotal = vblank_start + 3
883  *
884  *           start of vblank:
885  *           latch double buffered registers
886  *           increment frame counter (ctg+)
887  *           generate start of vblank interrupt (gen4+)
888  *           |
889  *           |          frame start:
890  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
891  *           |          may be shifted forward 1-3 extra lines via PIPECONF
892  *           |          |
893  *           |          |  start of vsync:
894  *           |          |  generate vsync interrupt
895  *           |          |  |
896  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
897  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
898  * ----va---> <-----------------vb--------------------> <--------va-------------
899  *       |          |       <----vs----->                     |
900  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
901  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
902  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
903  *       |          |                                         |
904  *       last visible pixel                                   first visible pixel
905  *                  |                                         increment frame counter (gen3/4)
906  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
907  *
908  * x  = horizontal active
909  * _  = horizontal blanking
910  * hs = horizontal sync
911  * va = vertical active
912  * vb = vertical blanking
913  * vs = vertical sync
914  * vbs = vblank_start (number)
915  *
916  * Summary:
917  * - most events happen at the start of horizontal sync
918  * - frame start happens at the start of horizontal blank, 1-4 lines
919  *   (depending on PIPECONF settings) after the start of vblank
920  * - gen3/4 pixel and frame counter are synchronized with the start
921  *   of horizontal active on the first line of vertical active
922  */
923
924 /* Called from drm generic code, passed a 'crtc', which
925  * we use as a pipe index
926  */
927 u32 i915_get_vblank_counter(struct drm_crtc *crtc)
928 {
929         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
930         struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[drm_crtc_index(crtc)];
931         const struct drm_display_mode *mode = &vblank->hwmode;
932         enum pipe pipe = to_intel_crtc(crtc)->pipe;
933         i915_reg_t high_frame, low_frame;
934         u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
935         unsigned long irqflags;
936
937         /*
938          * On i965gm TV output the frame counter only works up to
939          * the point when we enable the TV encoder. After that the
940          * frame counter ceases to work and reads zero. We need a
941          * vblank wait before enabling the TV encoder and so we
942          * have to enable vblank interrupts while the frame counter
943          * is still in a working state. However the core vblank code
944          * does not like us returning non-zero frame counter values
945          * when we've told it that we don't have a working frame
946          * counter. Thus we must stop non-zero values leaking out.
947          */
948         if (!vblank->max_vblank_count)
949                 return 0;
950
951         htotal = mode->crtc_htotal;
952         hsync_start = mode->crtc_hsync_start;
953         vbl_start = mode->crtc_vblank_start;
954         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
955                 vbl_start = DIV_ROUND_UP(vbl_start, 2);
956
957         /* Convert to pixel count */
958         vbl_start *= htotal;
959
960         /* Start of vblank event occurs at start of hsync */
961         vbl_start -= htotal - hsync_start;
962
963         high_frame = PIPEFRAME(pipe);
964         low_frame = PIPEFRAMEPIXEL(pipe);
965
966         spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
967
968         /*
969          * High & low register fields aren't synchronized, so make sure
970          * we get a low value that's stable across two reads of the high
971          * register.
972          */
973         do {
974                 high1 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
975                 low   = I915_READ_FW(low_frame);
976                 high2 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
977         } while (high1 != high2);
978
979         spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
980
981         high1 >>= PIPE_FRAME_HIGH_SHIFT;
982         pixel = low & PIPE_PIXEL_MASK;
983         low >>= PIPE_FRAME_LOW_SHIFT;
984
985         /*
986          * The frame counter increments at beginning of active.
987          * Cook up a vblank counter by also checking the pixel
988          * counter against vblank start.
989          */
990         return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
991 }
992
993 u32 g4x_get_vblank_counter(struct drm_crtc *crtc)
994 {
995         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
996         enum pipe pipe = to_intel_crtc(crtc)->pipe;
997
998         return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
999 }
1000
1001 /*
1002  * On certain encoders on certain platforms, pipe
1003  * scanline register will not work to get the scanline,
1004  * since the timings are driven from the PORT or issues
1005  * with scanline register updates.
1006  * This function will use Framestamp and current
1007  * timestamp registers to calculate the scanline.
1008  */
1009 static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
1010 {
1011         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1012         struct drm_vblank_crtc *vblank =
1013                 &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
1014         const struct drm_display_mode *mode = &vblank->hwmode;
1015         u32 vblank_start = mode->crtc_vblank_start;
1016         u32 vtotal = mode->crtc_vtotal;
1017         u32 htotal = mode->crtc_htotal;
1018         u32 clock = mode->crtc_clock;
1019         u32 scanline, scan_prev_time, scan_curr_time, scan_post_time;
1020
1021         /*
1022          * To avoid the race condition where we might cross into the
1023          * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
1024          * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
1025          * during the same frame.
1026          */
1027         do {
1028                 /*
1029                  * This field provides read back of the display
1030                  * pipe frame time stamp. The time stamp value
1031                  * is sampled at every start of vertical blank.
1032                  */
1033                 scan_prev_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
1034
1035                 /*
1036                  * The TIMESTAMP_CTR register has the current
1037                  * time stamp value.
1038                  */
1039                 scan_curr_time = I915_READ_FW(IVB_TIMESTAMP_CTR);
1040
1041                 scan_post_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
1042         } while (scan_post_time != scan_prev_time);
1043
1044         scanline = div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
1045                                         clock), 1000 * htotal);
1046         scanline = min(scanline, vtotal - 1);
1047         scanline = (scanline + vblank_start) % vtotal;
1048
1049         return scanline;
1050 }
1051
1052 /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
1053 static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
1054 {
1055         struct drm_device *dev = crtc->base.dev;
1056         struct drm_i915_private *dev_priv = to_i915(dev);
1057         const struct drm_display_mode *mode;
1058         struct drm_vblank_crtc *vblank;
1059         enum pipe pipe = crtc->pipe;
1060         int position, vtotal;
1061
1062         if (!crtc->active)
1063                 return -1;
1064
1065         vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
1066         mode = &vblank->hwmode;
1067
1068         if (mode->private_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
1069                 return __intel_get_crtc_scanline_from_timestamp(crtc);
1070
1071         vtotal = mode->crtc_vtotal;
1072         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1073                 vtotal /= 2;
1074
1075         if (IS_GEN(dev_priv, 2))
1076                 position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
1077         else
1078                 position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
1079
1080         /*
1081          * On HSW, the DSL reg (0x70000) appears to return 0 if we
1082          * read it just before the start of vblank.  So try it again
1083          * so we don't accidentally end up spanning a vblank frame
1084          * increment, causing the pipe_update_end() code to squak at us.
1085          *
1086          * The nature of this problem means we can't simply check the ISR
1087          * bit and return the vblank start value; nor can we use the scanline
1088          * debug register in the transcoder as it appears to have the same
1089          * problem.  We may need to extend this to include other platforms,
1090          * but so far testing only shows the problem on HSW.
1091          */
1092         if (HAS_DDI(dev_priv) && !position) {
1093                 int i, temp;
1094
1095                 for (i = 0; i < 100; i++) {
1096                         udelay(1);
1097                         temp = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
1098                         if (temp != position) {
1099                                 position = temp;
1100                                 break;
1101                         }
1102                 }
1103         }
1104
1105         /*
1106          * See update_scanline_offset() for the details on the
1107          * scanline_offset adjustment.
1108          */
1109         return (position + crtc->scanline_offset) % vtotal;
1110 }
1111
1112 bool i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
1113                               bool in_vblank_irq, int *vpos, int *hpos,
1114                               ktime_t *stime, ktime_t *etime,
1115                               const struct drm_display_mode *mode)
1116 {
1117         struct drm_i915_private *dev_priv = to_i915(dev);
1118         struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1119                                                                 pipe);
1120         int position;
1121         int vbl_start, vbl_end, hsync_start, htotal, vtotal;
1122         unsigned long irqflags;
1123         bool use_scanline_counter = INTEL_GEN(dev_priv) >= 5 ||
1124                 IS_G4X(dev_priv) || IS_GEN(dev_priv, 2) ||
1125                 mode->private_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
1126
1127         if (WARN_ON(!mode->crtc_clock)) {
1128                 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
1129                                  "pipe %c\n", pipe_name(pipe));
1130                 return false;
1131         }
1132
1133         htotal = mode->crtc_htotal;
1134         hsync_start = mode->crtc_hsync_start;
1135         vtotal = mode->crtc_vtotal;
1136         vbl_start = mode->crtc_vblank_start;
1137         vbl_end = mode->crtc_vblank_end;
1138
1139         if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1140                 vbl_start = DIV_ROUND_UP(vbl_start, 2);
1141                 vbl_end /= 2;
1142                 vtotal /= 2;
1143         }
1144
1145         /*
1146          * Lock uncore.lock, as we will do multiple timing critical raw
1147          * register reads, potentially with preemption disabled, so the
1148          * following code must not block on uncore.lock.
1149          */
1150         spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1151
1152         /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1153
1154         /* Get optional system timestamp before query. */
1155         if (stime)
1156                 *stime = ktime_get();
1157
1158         if (use_scanline_counter) {
1159                 /* No obvious pixelcount register. Only query vertical
1160                  * scanout position from Display scan line register.
1161                  */
1162                 position = __intel_get_crtc_scanline(intel_crtc);
1163         } else {
1164                 /* Have access to pixelcount since start of frame.
1165                  * We can split this into vertical and horizontal
1166                  * scanout position.
1167                  */
1168                 position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
1169
1170                 /* convert to pixel counts */
1171                 vbl_start *= htotal;
1172                 vbl_end *= htotal;
1173                 vtotal *= htotal;
1174
1175                 /*
1176                  * In interlaced modes, the pixel counter counts all pixels,
1177                  * so one field will have htotal more pixels. In order to avoid
1178                  * the reported position from jumping backwards when the pixel
1179                  * counter is beyond the length of the shorter field, just
1180                  * clamp the position the length of the shorter field. This
1181                  * matches how the scanline counter based position works since
1182                  * the scanline counter doesn't count the two half lines.
1183                  */
1184                 if (position >= vtotal)
1185                         position = vtotal - 1;
1186
1187                 /*
1188                  * Start of vblank interrupt is triggered at start of hsync,
1189                  * just prior to the first active line of vblank. However we
1190                  * consider lines to start at the leading edge of horizontal
1191                  * active. So, should we get here before we've crossed into
1192                  * the horizontal active of the first line in vblank, we would
1193                  * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
1194                  * always add htotal-hsync_start to the current pixel position.
1195                  */
1196                 position = (position + htotal - hsync_start) % vtotal;
1197         }
1198
1199         /* Get optional system timestamp after query. */
1200         if (etime)
1201                 *etime = ktime_get();
1202
1203         /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1204
1205         spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1206
1207         /*
1208          * While in vblank, position will be negative
1209          * counting up towards 0 at vbl_end. And outside
1210          * vblank, position will be positive counting
1211          * up since vbl_end.
1212          */
1213         if (position >= vbl_start)
1214                 position -= vbl_end;
1215         else
1216                 position += vtotal - vbl_end;
1217
1218         if (use_scanline_counter) {
1219                 *vpos = position;
1220                 *hpos = 0;
1221         } else {
1222                 *vpos = position / htotal;
1223                 *hpos = position - (*vpos * htotal);
1224         }
1225
1226         return true;
1227 }
1228
1229 int intel_get_crtc_scanline(struct intel_crtc *crtc)
1230 {
1231         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1232         unsigned long irqflags;
1233         int position;
1234
1235         spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1236         position = __intel_get_crtc_scanline(crtc);
1237         spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1238
1239         return position;
1240 }
1241
1242 static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
1243 {
1244         struct intel_uncore *uncore = &dev_priv->uncore;
1245         u32 busy_up, busy_down, max_avg, min_avg;
1246         u8 new_delay;
1247
1248         spin_lock(&mchdev_lock);
1249
1250         intel_uncore_write16(uncore,
1251                              MEMINTRSTS,
1252                              intel_uncore_read(uncore, MEMINTRSTS));
1253
1254         new_delay = dev_priv->ips.cur_delay;
1255
1256         intel_uncore_write16(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
1257         busy_up = intel_uncore_read(uncore, RCPREVBSYTUPAVG);
1258         busy_down = intel_uncore_read(uncore, RCPREVBSYTDNAVG);
1259         max_avg = intel_uncore_read(uncore, RCBMAXAVG);
1260         min_avg = intel_uncore_read(uncore, RCBMINAVG);
1261
1262         /* Handle RCS change request from hw */
1263         if (busy_up > max_avg) {
1264                 if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
1265                         new_delay = dev_priv->ips.cur_delay - 1;
1266                 if (new_delay < dev_priv->ips.max_delay)
1267                         new_delay = dev_priv->ips.max_delay;
1268         } else if (busy_down < min_avg) {
1269                 if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
1270                         new_delay = dev_priv->ips.cur_delay + 1;
1271                 if (new_delay > dev_priv->ips.min_delay)
1272                         new_delay = dev_priv->ips.min_delay;
1273         }
1274
1275         if (ironlake_set_drps(dev_priv, new_delay))
1276                 dev_priv->ips.cur_delay = new_delay;
1277
1278         spin_unlock(&mchdev_lock);
1279
1280         return;
1281 }
1282
1283 static void vlv_c0_read(struct drm_i915_private *dev_priv,
1284                         struct intel_rps_ei *ei)
1285 {
1286         ei->ktime = ktime_get_raw();
1287         ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
1288         ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
1289 }
1290
1291 void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
1292 {
1293         memset(&dev_priv->gt_pm.rps.ei, 0, sizeof(dev_priv->gt_pm.rps.ei));
1294 }
1295
1296 static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
1297 {
1298         struct intel_rps *rps = &dev_priv->gt_pm.rps;
1299         const struct intel_rps_ei *prev = &rps->ei;
1300         struct intel_rps_ei now;
1301         u32 events = 0;
1302
1303         if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
1304                 return 0;
1305
1306         vlv_c0_read(dev_priv, &now);
1307
1308         if (prev->ktime) {
1309                 u64 time, c0;
1310                 u32 render, media;
1311
1312                 time = ktime_us_delta(now.ktime, prev->ktime);
1313
1314                 time *= dev_priv->czclk_freq;
1315
1316                 /* Workload can be split between render + media,
1317                  * e.g. SwapBuffers being blitted in X after being rendered in
1318                  * mesa. To account for this we need to combine both engines
1319                  * into our activity counter.
1320                  */
1321                 render = now.render_c0 - prev->render_c0;
1322                 media = now.media_c0 - prev->media_c0;
1323                 c0 = max(render, media);
1324                 c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1325
1326                 if (c0 > time * rps->power.up_threshold)
1327                         events = GEN6_PM_RP_UP_THRESHOLD;
1328                 else if (c0 < time * rps->power.down_threshold)
1329                         events = GEN6_PM_RP_DOWN_THRESHOLD;
1330         }
1331
1332         rps->ei = now;
1333         return events;
1334 }
1335
1336 static void gen6_pm_rps_work(struct work_struct *work)
1337 {
1338         struct drm_i915_private *dev_priv =
1339                 container_of(work, struct drm_i915_private, gt_pm.rps.work);
1340         struct intel_rps *rps = &dev_priv->gt_pm.rps;
1341         bool client_boost = false;
1342         int new_delay, adj, min, max;
1343         u32 pm_iir = 0;
1344
1345         spin_lock_irq(&dev_priv->irq_lock);
1346         if (rps->interrupts_enabled) {
1347                 pm_iir = fetch_and_zero(&rps->pm_iir);
1348                 client_boost = atomic_read(&rps->num_waiters);
1349         }
1350         spin_unlock_irq(&dev_priv->irq_lock);
1351
1352         /* Make sure we didn't queue anything we're not going to process. */
1353         WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
1354         if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
1355                 goto out;
1356
1357         mutex_lock(&rps->lock);
1358
1359         pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
1360
1361         adj = rps->last_adj;
1362         new_delay = rps->cur_freq;
1363         min = rps->min_freq_softlimit;
1364         max = rps->max_freq_softlimit;
1365         if (client_boost)
1366                 max = rps->max_freq;
1367         if (client_boost && new_delay < rps->boost_freq) {
1368                 new_delay = rps->boost_freq;
1369                 adj = 0;
1370         } else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1371                 if (adj > 0)
1372                         adj *= 2;
1373                 else /* CHV needs even encode values */
1374                         adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
1375
1376                 if (new_delay >= rps->max_freq_softlimit)
1377                         adj = 0;
1378         } else if (client_boost) {
1379                 adj = 0;
1380         } else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1381                 if (rps->cur_freq > rps->efficient_freq)
1382                         new_delay = rps->efficient_freq;
1383                 else if (rps->cur_freq > rps->min_freq_softlimit)
1384                         new_delay = rps->min_freq_softlimit;
1385                 adj = 0;
1386         } else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1387                 if (adj < 0)
1388                         adj *= 2;
1389                 else /* CHV needs even encode values */
1390                         adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1391
1392                 if (new_delay <= rps->min_freq_softlimit)
1393                         adj = 0;
1394         } else { /* unknown event */
1395                 adj = 0;
1396         }
1397
1398         rps->last_adj = adj;
1399
1400         /*
1401          * Limit deboosting and boosting to keep ourselves at the extremes
1402          * when in the respective power modes (i.e. slowly decrease frequencies
1403          * while in the HIGH_POWER zone and slowly increase frequencies while
1404          * in the LOW_POWER zone). On idle, we will hit the timeout and drop
1405          * to the next level quickly, and conversely if busy we expect to
1406          * hit a waitboost and rapidly switch into max power.
1407          */
1408         if ((adj < 0 && rps->power.mode == HIGH_POWER) ||
1409             (adj > 0 && rps->power.mode == LOW_POWER))
1410                 rps->last_adj = 0;
1411
1412         /* sysfs frequency interfaces may have snuck in while servicing the
1413          * interrupt
1414          */
1415         new_delay += adj;
1416         new_delay = clamp_t(int, new_delay, min, max);
1417
1418         if (intel_set_rps(dev_priv, new_delay)) {
1419                 DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
1420                 rps->last_adj = 0;
1421         }
1422
1423         mutex_unlock(&rps->lock);
1424
1425 out:
1426         /* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1427         spin_lock_irq(&dev_priv->irq_lock);
1428         if (rps->interrupts_enabled)
1429                 gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
1430         spin_unlock_irq(&dev_priv->irq_lock);
1431 }
1432
1433
1434 /**
1435  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1436  * occurred.
1437  * @work: workqueue struct
1438  *
1439  * Doesn't actually do anything except notify userspace. As a consequence of
1440  * this event, userspace should try to remap the bad rows since statistically
1441  * it is likely the same row is more likely to go bad again.
1442  */
1443 static void ivybridge_parity_work(struct work_struct *work)
1444 {
1445         struct drm_i915_private *dev_priv =
1446                 container_of(work, typeof(*dev_priv), l3_parity.error_work);
1447         u32 error_status, row, bank, subbank;
1448         char *parity_event[6];
1449         u32 misccpctl;
1450         u8 slice = 0;
1451
1452         /* We must turn off DOP level clock gating to access the L3 registers.
1453          * In order to prevent a get/put style interface, acquire struct mutex
1454          * any time we access those registers.
1455          */
1456         mutex_lock(&dev_priv->drm.struct_mutex);
1457
1458         /* If we've screwed up tracking, just let the interrupt fire again */
1459         if (WARN_ON(!dev_priv->l3_parity.which_slice))
1460                 goto out;
1461
1462         misccpctl = I915_READ(GEN7_MISCCPCTL);
1463         I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1464         POSTING_READ(GEN7_MISCCPCTL);
1465
1466         while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1467                 i915_reg_t reg;
1468
1469                 slice--;
1470                 if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
1471                         break;
1472
1473                 dev_priv->l3_parity.which_slice &= ~(1<<slice);
1474
1475                 reg = GEN7_L3CDERRST1(slice);
1476
1477                 error_status = I915_READ(reg);
1478                 row = GEN7_PARITY_ERROR_ROW(error_status);
1479                 bank = GEN7_PARITY_ERROR_BANK(error_status);
1480                 subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1481
1482                 I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
1483                 POSTING_READ(reg);
1484
1485                 parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1486                 parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1487                 parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1488                 parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
1489                 parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
1490                 parity_event[5] = NULL;
1491
1492                 kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1493                                    KOBJ_CHANGE, parity_event);
1494
1495                 DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
1496                           slice, row, bank, subbank);
1497
1498                 kfree(parity_event[4]);
1499                 kfree(parity_event[3]);
1500                 kfree(parity_event[2]);
1501                 kfree(parity_event[1]);
1502         }
1503
1504         I915_WRITE(GEN7_MISCCPCTL, misccpctl);
1505
1506 out:
1507         WARN_ON(dev_priv->l3_parity.which_slice);
1508         spin_lock_irq(&dev_priv->irq_lock);
1509         gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1510         spin_unlock_irq(&dev_priv->irq_lock);
1511
1512         mutex_unlock(&dev_priv->drm.struct_mutex);
1513 }
1514
1515 static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1516                                                u32 iir)
1517 {
1518         if (!HAS_L3_DPF(dev_priv))
1519                 return;
1520
1521         spin_lock(&dev_priv->irq_lock);
1522         gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1523         spin_unlock(&dev_priv->irq_lock);
1524
1525         iir &= GT_PARITY_ERROR(dev_priv);
1526         if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
1527                 dev_priv->l3_parity.which_slice |= 1 << 1;
1528
1529         if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
1530                 dev_priv->l3_parity.which_slice |= 1 << 0;
1531
1532         queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1533 }
1534
1535 static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1536                                u32 gt_iir)
1537 {
1538         if (gt_iir & GT_RENDER_USER_INTERRUPT)
1539                 intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
1540         if (gt_iir & ILK_BSD_USER_INTERRUPT)
1541                 intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
1542 }
1543
1544 static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1545                                u32 gt_iir)
1546 {
1547         if (gt_iir & GT_RENDER_USER_INTERRUPT)
1548                 intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
1549         if (gt_iir & GT_BSD_USER_INTERRUPT)
1550                 intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
1551         if (gt_iir & GT_BLT_USER_INTERRUPT)
1552                 intel_engine_breadcrumbs_irq(dev_priv->engine[BCS0]);
1553
1554         if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1555                       GT_BSD_CS_ERROR_INTERRUPT |
1556                       GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1557                 DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1558
1559         if (gt_iir & GT_PARITY_ERROR(dev_priv))
1560                 ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1561 }
1562
1563 static void
1564 gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir)
1565 {
1566         bool tasklet = false;
1567
1568         if (iir & GT_CONTEXT_SWITCH_INTERRUPT)
1569                 tasklet = true;
1570
1571         if (iir & GT_RENDER_USER_INTERRUPT) {
1572                 intel_engine_breadcrumbs_irq(engine);
1573                 tasklet |= intel_engine_needs_breadcrumb_tasklet(engine);
1574         }
1575
1576         if (tasklet)
1577                 tasklet_hi_schedule(&engine->execlists.tasklet);
1578 }
1579
1580 static void gen8_gt_irq_ack(struct drm_i915_private *i915,
1581                             u32 master_ctl, u32 gt_iir[4])
1582 {
1583         void __iomem * const regs = i915->uncore.regs;
1584
1585 #define GEN8_GT_IRQS (GEN8_GT_RCS_IRQ | \
1586                       GEN8_GT_BCS_IRQ | \
1587                       GEN8_GT_VCS0_IRQ | \
1588                       GEN8_GT_VCS1_IRQ | \
1589                       GEN8_GT_VECS_IRQ | \
1590                       GEN8_GT_PM_IRQ | \
1591                       GEN8_GT_GUC_IRQ)
1592
1593         if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1594                 gt_iir[0] = raw_reg_read(regs, GEN8_GT_IIR(0));
1595                 if (likely(gt_iir[0]))
1596                         raw_reg_write(regs, GEN8_GT_IIR(0), gt_iir[0]);
1597         }
1598
1599         if (master_ctl & (GEN8_GT_VCS0_IRQ | GEN8_GT_VCS1_IRQ)) {
1600                 gt_iir[1] = raw_reg_read(regs, GEN8_GT_IIR(1));
1601                 if (likely(gt_iir[1]))
1602                         raw_reg_write(regs, GEN8_GT_IIR(1), gt_iir[1]);
1603         }
1604
1605         if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
1606                 gt_iir[2] = raw_reg_read(regs, GEN8_GT_IIR(2));
1607                 if (likely(gt_iir[2]))
1608                         raw_reg_write(regs, GEN8_GT_IIR(2), gt_iir[2]);
1609         }
1610
1611         if (master_ctl & GEN8_GT_VECS_IRQ) {
1612                 gt_iir[3] = raw_reg_read(regs, GEN8_GT_IIR(3));
1613                 if (likely(gt_iir[3]))
1614                         raw_reg_write(regs, GEN8_GT_IIR(3), gt_iir[3]);
1615         }
1616 }
1617
1618 static void gen8_gt_irq_handler(struct drm_i915_private *i915,
1619                                 u32 master_ctl, u32 gt_iir[4])
1620 {
1621         if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1622                 gen8_cs_irq_handler(i915->engine[RCS0],
1623                                     gt_iir[0] >> GEN8_RCS_IRQ_SHIFT);
1624                 gen8_cs_irq_handler(i915->engine[BCS0],
1625                                     gt_iir[0] >> GEN8_BCS_IRQ_SHIFT);
1626         }
1627
1628         if (master_ctl & (GEN8_GT_VCS0_IRQ | GEN8_GT_VCS1_IRQ)) {
1629                 gen8_cs_irq_handler(i915->engine[VCS0],
1630                                     gt_iir[1] >> GEN8_VCS0_IRQ_SHIFT);
1631                 gen8_cs_irq_handler(i915->engine[VCS1],
1632                                     gt_iir[1] >> GEN8_VCS1_IRQ_SHIFT);
1633         }
1634
1635         if (master_ctl & GEN8_GT_VECS_IRQ) {
1636                 gen8_cs_irq_handler(i915->engine[VECS0],
1637                                     gt_iir[3] >> GEN8_VECS_IRQ_SHIFT);
1638         }
1639
1640         if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
1641                 gen6_rps_irq_handler(i915, gt_iir[2]);
1642                 gen9_guc_irq_handler(i915, gt_iir[2]);
1643         }
1644 }
1645
1646 static bool gen11_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1647 {
1648         switch (pin) {
1649         case HPD_PORT_C:
1650                 return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC1);
1651         case HPD_PORT_D:
1652                 return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC2);
1653         case HPD_PORT_E:
1654                 return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC3);
1655         case HPD_PORT_F:
1656                 return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC4);
1657         default:
1658                 return false;
1659         }
1660 }
1661
1662 static bool bxt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1663 {
1664         switch (pin) {
1665         case HPD_PORT_A:
1666                 return val & PORTA_HOTPLUG_LONG_DETECT;
1667         case HPD_PORT_B:
1668                 return val & PORTB_HOTPLUG_LONG_DETECT;
1669         case HPD_PORT_C:
1670                 return val & PORTC_HOTPLUG_LONG_DETECT;
1671         default:
1672                 return false;
1673         }
1674 }
1675
1676 static bool icp_ddi_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1677 {
1678         switch (pin) {
1679         case HPD_PORT_A:
1680                 return val & ICP_DDIA_HPD_LONG_DETECT;
1681         case HPD_PORT_B:
1682                 return val & ICP_DDIB_HPD_LONG_DETECT;
1683         default:
1684                 return false;
1685         }
1686 }
1687
1688 static bool icp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1689 {
1690         switch (pin) {
1691         case HPD_PORT_C:
1692                 return val & ICP_TC_HPD_LONG_DETECT(PORT_TC1);
1693         case HPD_PORT_D:
1694                 return val & ICP_TC_HPD_LONG_DETECT(PORT_TC2);
1695         case HPD_PORT_E:
1696                 return val & ICP_TC_HPD_LONG_DETECT(PORT_TC3);
1697         case HPD_PORT_F:
1698                 return val & ICP_TC_HPD_LONG_DETECT(PORT_TC4);
1699         default:
1700                 return false;
1701         }
1702 }
1703
1704 static bool spt_port_hotplug2_long_detect(enum hpd_pin pin, u32 val)
1705 {
1706         switch (pin) {
1707         case HPD_PORT_E:
1708                 return val & PORTE_HOTPLUG_LONG_DETECT;
1709         default:
1710                 return false;
1711         }
1712 }
1713
1714 static bool spt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1715 {
1716         switch (pin) {
1717         case HPD_PORT_A:
1718                 return val & PORTA_HOTPLUG_LONG_DETECT;
1719         case HPD_PORT_B:
1720                 return val & PORTB_HOTPLUG_LONG_DETECT;
1721         case HPD_PORT_C:
1722                 return val & PORTC_HOTPLUG_LONG_DETECT;
1723         case HPD_PORT_D:
1724                 return val & PORTD_HOTPLUG_LONG_DETECT;
1725         default:
1726                 return false;
1727         }
1728 }
1729
1730 static bool ilk_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1731 {
1732         switch (pin) {
1733         case HPD_PORT_A:
1734                 return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1735         default:
1736                 return false;
1737         }
1738 }
1739
1740 static bool pch_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1741 {
1742         switch (pin) {
1743         case HPD_PORT_B:
1744                 return val & PORTB_HOTPLUG_LONG_DETECT;
1745         case HPD_PORT_C:
1746                 return val & PORTC_HOTPLUG_LONG_DETECT;
1747         case HPD_PORT_D:
1748                 return val & PORTD_HOTPLUG_LONG_DETECT;
1749         default:
1750                 return false;
1751         }
1752 }
1753
1754 static bool i9xx_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1755 {
1756         switch (pin) {
1757         case HPD_PORT_B:
1758                 return val & PORTB_HOTPLUG_INT_LONG_PULSE;
1759         case HPD_PORT_C:
1760                 return val & PORTC_HOTPLUG_INT_LONG_PULSE;
1761         case HPD_PORT_D:
1762                 return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1763         default:
1764                 return false;
1765         }
1766 }
1767
1768 /*
1769  * Get a bit mask of pins that have triggered, and which ones may be long.
1770  * This can be called multiple times with the same masks to accumulate
1771  * hotplug detection results from several registers.
1772  *
1773  * Note that the caller is expected to zero out the masks initially.
1774  */
1775 static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
1776                                u32 *pin_mask, u32 *long_mask,
1777                                u32 hotplug_trigger, u32 dig_hotplug_reg,
1778                                const u32 hpd[HPD_NUM_PINS],
1779                                bool long_pulse_detect(enum hpd_pin pin, u32 val))
1780 {
1781         enum hpd_pin pin;
1782
1783         for_each_hpd_pin(pin) {
1784                 if ((hpd[pin] & hotplug_trigger) == 0)
1785                         continue;
1786
1787                 *pin_mask |= BIT(pin);
1788
1789                 if (long_pulse_detect(pin, dig_hotplug_reg))
1790                         *long_mask |= BIT(pin);
1791         }
1792
1793         DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x, long 0x%08x\n",
1794                          hotplug_trigger, dig_hotplug_reg, *pin_mask, *long_mask);
1795
1796 }
1797
1798 static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1799 {
1800         wake_up_all(&dev_priv->gmbus_wait_queue);
1801 }
1802
1803 static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1804 {
1805         wake_up_all(&dev_priv->gmbus_wait_queue);
1806 }
1807
1808 #if defined(CONFIG_DEBUG_FS)
1809 static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
1810                                          enum pipe pipe,
1811                                          u32 crc0, u32 crc1,
1812                                          u32 crc2, u32 crc3,
1813                                          u32 crc4)
1814 {
1815         struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
1816         struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
1817         u32 crcs[5] = { crc0, crc1, crc2, crc3, crc4 };
1818
1819         trace_intel_pipe_crc(crtc, crcs);
1820
1821         spin_lock(&pipe_crc->lock);
1822         /*
1823          * For some not yet identified reason, the first CRC is
1824          * bonkers. So let's just wait for the next vblank and read
1825          * out the buggy result.
1826          *
1827          * On GEN8+ sometimes the second CRC is bonkers as well, so
1828          * don't trust that one either.
1829          */
1830         if (pipe_crc->skipped <= 0 ||
1831             (INTEL_GEN(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
1832                 pipe_crc->skipped++;
1833                 spin_unlock(&pipe_crc->lock);
1834                 return;
1835         }
1836         spin_unlock(&pipe_crc->lock);
1837
1838         drm_crtc_add_crc_entry(&crtc->base, true,
1839                                 drm_crtc_accurate_vblank_count(&crtc->base),
1840                                 crcs);
1841 }
1842 #else
1843 static inline void
1844 display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
1845                              enum pipe pipe,
1846                              u32 crc0, u32 crc1,
1847                              u32 crc2, u32 crc3,
1848                              u32 crc4) {}
1849 #endif
1850
1851
1852 static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
1853                                      enum pipe pipe)
1854 {
1855         display_pipe_crc_irq_handler(dev_priv, pipe,
1856                                      I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1857                                      0, 0, 0, 0);
1858 }
1859
1860 static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
1861                                      enum pipe pipe)
1862 {
1863         display_pipe_crc_irq_handler(dev_priv, pipe,
1864                                      I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1865                                      I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1866                                      I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1867                                      I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
1868                                      I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1869 }
1870
1871 static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
1872                                       enum pipe pipe)
1873 {
1874         u32 res1, res2;
1875
1876         if (INTEL_GEN(dev_priv) >= 3)
1877                 res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
1878         else
1879                 res1 = 0;
1880
1881         if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
1882                 res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
1883         else
1884                 res2 = 0;
1885
1886         display_pipe_crc_irq_handler(dev_priv, pipe,
1887                                      I915_READ(PIPE_CRC_RES_RED(pipe)),
1888                                      I915_READ(PIPE_CRC_RES_GREEN(pipe)),
1889                                      I915_READ(PIPE_CRC_RES_BLUE(pipe)),
1890                                      res1, res2);
1891 }
1892
1893 /* The RPS events need forcewake, so we add them to a work queue and mask their
1894  * IMR bits until the work is done. Other interrupts can be processed without
1895  * the work queue. */
1896 static void gen11_rps_irq_handler(struct drm_i915_private *i915, u32 pm_iir)
1897 {
1898         struct intel_rps *rps = &i915->gt_pm.rps;
1899         const u32 events = i915->pm_rps_events & pm_iir;
1900
1901         lockdep_assert_held(&i915->irq_lock);
1902
1903         if (unlikely(!events))
1904                 return;
1905
1906         gen6_mask_pm_irq(i915, events);
1907
1908         if (!rps->interrupts_enabled)
1909                 return;
1910
1911         rps->pm_iir |= events;
1912         schedule_work(&rps->work);
1913 }
1914
1915 static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1916 {
1917         struct intel_rps *rps = &dev_priv->gt_pm.rps;
1918
1919         if (pm_iir & dev_priv->pm_rps_events) {
1920                 spin_lock(&dev_priv->irq_lock);
1921                 gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1922                 if (rps->interrupts_enabled) {
1923                         rps->pm_iir |= pm_iir & dev_priv->pm_rps_events;
1924                         schedule_work(&rps->work);
1925                 }
1926                 spin_unlock(&dev_priv->irq_lock);
1927         }
1928
1929         if (INTEL_GEN(dev_priv) >= 8)
1930                 return;
1931
1932         if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1933                 intel_engine_breadcrumbs_irq(dev_priv->engine[VECS0]);
1934
1935         if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1936                 DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
1937 }
1938
1939 static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
1940 {
1941         if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT)
1942                 intel_guc_to_host_event_handler(&dev_priv->guc);
1943 }
1944
1945 static void gen11_guc_irq_handler(struct drm_i915_private *i915, u16 iir)
1946 {
1947         if (iir & GEN11_GUC_INTR_GUC2HOST)
1948                 intel_guc_to_host_event_handler(&i915->guc);
1949 }
1950
1951 static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
1952 {
1953         enum pipe pipe;
1954
1955         for_each_pipe(dev_priv, pipe) {
1956                 I915_WRITE(PIPESTAT(pipe),
1957                            PIPESTAT_INT_STATUS_MASK |
1958                            PIPE_FIFO_UNDERRUN_STATUS);
1959
1960                 dev_priv->pipestat_irq_mask[pipe] = 0;
1961         }
1962 }
1963
1964 static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
1965                                   u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1966 {
1967         int pipe;
1968
1969         spin_lock(&dev_priv->irq_lock);
1970
1971         if (!dev_priv->display_irqs_enabled) {
1972                 spin_unlock(&dev_priv->irq_lock);
1973                 return;
1974         }
1975
1976         for_each_pipe(dev_priv, pipe) {
1977                 i915_reg_t reg;
1978                 u32 status_mask, enable_mask, iir_bit = 0;
1979
1980                 /*
1981                  * PIPESTAT bits get signalled even when the interrupt is
1982                  * disabled with the mask bits, and some of the status bits do
1983                  * not generate interrupts at all (like the underrun bit). Hence
1984                  * we need to be careful that we only handle what we want to
1985                  * handle.
1986                  */
1987
1988                 /* fifo underruns are filterered in the underrun handler. */
1989                 status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1990
1991                 switch (pipe) {
1992                 case PIPE_A:
1993                         iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1994                         break;
1995                 case PIPE_B:
1996                         iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1997                         break;
1998                 case PIPE_C:
1999                         iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
2000                         break;
2001                 }
2002                 if (iir & iir_bit)
2003                         status_mask |= dev_priv->pipestat_irq_mask[pipe];
2004
2005                 if (!status_mask)
2006                         continue;
2007
2008                 reg = PIPESTAT(pipe);
2009                 pipe_stats[pipe] = I915_READ(reg) & status_mask;
2010                 enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
2011
2012                 /*
2013                  * Clear the PIPE*STAT regs before the IIR
2014                  *
2015                  * Toggle the enable bits to make sure we get an
2016                  * edge in the ISR pipe event bit if we don't clear
2017                  * all the enabled status bits. Otherwise the edge
2018                  * triggered IIR on i965/g4x wouldn't notice that
2019                  * an interrupt is still pending.
2020                  */
2021                 if (pipe_stats[pipe]) {
2022                         I915_WRITE(reg, pipe_stats[pipe]);
2023                         I915_WRITE(reg, enable_mask);
2024                 }
2025         }
2026         spin_unlock(&dev_priv->irq_lock);
2027 }
2028
2029 static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
2030                                       u16 iir, u32 pipe_stats[I915_MAX_PIPES])
2031 {
2032         enum pipe pipe;
2033
2034         for_each_pipe(dev_priv, pipe) {
2035                 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
2036                         drm_handle_vblank(&dev_priv->drm, pipe);
2037
2038                 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
2039                         i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2040
2041                 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2042                         intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2043         }
2044 }
2045
2046 static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
2047                                       u32 iir, u32 pipe_stats[I915_MAX_PIPES])
2048 {
2049         bool blc_event = false;
2050         enum pipe pipe;
2051
2052         for_each_pipe(dev_priv, pipe) {
2053                 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
2054                         drm_handle_vblank(&dev_priv->drm, pipe);
2055
2056                 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2057                         blc_event = true;
2058
2059                 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
2060                         i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2061
2062                 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2063                         intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2064         }
2065
2066         if (blc_event || (iir & I915_ASLE_INTERRUPT))
2067                 intel_opregion_asle_intr(dev_priv);
2068 }
2069
2070 static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
2071                                       u32 iir, u32 pipe_stats[I915_MAX_PIPES])
2072 {
2073         bool blc_event = false;
2074         enum pipe pipe;
2075
2076         for_each_pipe(dev_priv, pipe) {
2077                 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
2078                         drm_handle_vblank(&dev_priv->drm, pipe);
2079
2080                 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2081                         blc_event = true;
2082
2083                 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
2084                         i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2085
2086                 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2087                         intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2088         }
2089
2090         if (blc_event || (iir & I915_ASLE_INTERRUPT))
2091                 intel_opregion_asle_intr(dev_priv);
2092
2093         if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2094                 gmbus_irq_handler(dev_priv);
2095 }
2096
2097 static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
2098                                             u32 pipe_stats[I915_MAX_PIPES])
2099 {
2100         enum pipe pipe;
2101
2102         for_each_pipe(dev_priv, pipe) {
2103                 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
2104                         drm_handle_vblank(&dev_priv->drm, pipe);
2105
2106                 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
2107                         i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2108
2109                 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2110                         intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2111         }
2112
2113         if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2114                 gmbus_irq_handler(dev_priv);
2115 }
2116
2117 static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
2118 {
2119         u32 hotplug_status = 0, hotplug_status_mask;
2120         int i;
2121
2122         if (IS_G4X(dev_priv) ||
2123             IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2124                 hotplug_status_mask = HOTPLUG_INT_STATUS_G4X |
2125                         DP_AUX_CHANNEL_MASK_INT_STATUS_G4X;
2126         else
2127                 hotplug_status_mask = HOTPLUG_INT_STATUS_I915;
2128
2129         /*
2130          * We absolutely have to clear all the pending interrupt
2131          * bits in PORT_HOTPLUG_STAT. Otherwise the ISR port
2132          * interrupt bit won't have an edge, and the i965/g4x
2133          * edge triggered IIR will not notice that an interrupt
2134          * is still pending. We can't use PORT_HOTPLUG_EN to
2135          * guarantee the edge as the act of toggling the enable
2136          * bits can itself generate a new hotplug interrupt :(
2137          */
2138         for (i = 0; i < 10; i++) {
2139                 u32 tmp = I915_READ(PORT_HOTPLUG_STAT) & hotplug_status_mask;
2140
2141                 if (tmp == 0)
2142                         return hotplug_status;
2143
2144                 hotplug_status |= tmp;
2145                 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2146         }
2147
2148         WARN_ONCE(1,
2149                   "PORT_HOTPLUG_STAT did not clear (0x%08x)\n",
2150                   I915_READ(PORT_HOTPLUG_STAT));
2151
2152         return hotplug_status;
2153 }
2154
2155 static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
2156                                  u32 hotplug_status)
2157 {
2158         u32 pin_mask = 0, long_mask = 0;
2159
2160         if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
2161             IS_CHERRYVIEW(dev_priv)) {
2162                 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
2163
2164                 if (hotplug_trigger) {
2165                         intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2166                                            hotplug_trigger, hotplug_trigger,
2167                                            hpd_status_g4x,
2168                                            i9xx_port_hotplug_long_detect);
2169
2170                         intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2171                 }
2172
2173                 if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
2174                         dp_aux_irq_handler(dev_priv);
2175         } else {
2176                 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
2177
2178                 if (hotplug_trigger) {
2179                         intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2180                                            hotplug_trigger, hotplug_trigger,
2181                                            hpd_status_i915,
2182                                            i9xx_port_hotplug_long_detect);
2183                         intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2184                 }
2185         }
2186 }
2187
2188 static irqreturn_t valleyview_irq_handler(int irq, void *arg)
2189 {
2190         struct drm_i915_private *dev_priv = arg;
2191         irqreturn_t ret = IRQ_NONE;
2192
2193         if (!intel_irqs_enabled(dev_priv))
2194                 return IRQ_NONE;
2195
2196         /* IRQs are synced during runtime_suspend, we don't require a wakeref */
2197         disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2198
2199         do {
2200                 u32 iir, gt_iir, pm_iir;
2201                 u32 pipe_stats[I915_MAX_PIPES] = {};
2202                 u32 hotplug_status = 0;
2203                 u32 ier = 0;
2204
2205                 gt_iir = I915_READ(GTIIR);
2206                 pm_iir = I915_READ(GEN6_PMIIR);
2207                 iir = I915_READ(VLV_IIR);
2208
2209                 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
2210                         break;
2211
2212                 ret = IRQ_HANDLED;
2213
2214                 /*
2215                  * Theory on interrupt generation, based on empirical evidence:
2216                  *
2217                  * x = ((VLV_IIR & VLV_IER) ||
2218                  *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
2219                  *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
2220                  *
2221                  * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2222                  * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
2223                  * guarantee the CPU interrupt will be raised again even if we
2224                  * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
2225                  * bits this time around.
2226                  */
2227                 I915_WRITE(VLV_MASTER_IER, 0);
2228                 ier = I915_READ(VLV_IER);
2229                 I915_WRITE(VLV_IER, 0);
2230
2231                 if (gt_iir)
2232                         I915_WRITE(GTIIR, gt_iir);
2233                 if (pm_iir)
2234                         I915_WRITE(GEN6_PMIIR, pm_iir);
2235
2236                 if (iir & I915_DISPLAY_PORT_INTERRUPT)
2237                         hotplug_status = i9xx_hpd_irq_ack(dev_priv);
2238
2239                 /* Call regardless, as some status bits might not be
2240                  * signalled in iir */
2241                 i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
2242
2243                 if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2244                            I915_LPE_PIPE_B_INTERRUPT))
2245                         intel_lpe_audio_irq_handler(dev_priv);
2246
2247                 /*
2248                  * VLV_IIR is single buffered, and reflects the level
2249                  * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
2250                  */
2251                 if (iir)
2252                         I915_WRITE(VLV_IIR, iir);
2253
2254                 I915_WRITE(VLV_IER, ier);
2255                 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
2256
2257                 if (gt_iir)
2258                         snb_gt_irq_handler(dev_priv, gt_iir);
2259                 if (pm_iir)
2260                         gen6_rps_irq_handler(dev_priv, pm_iir);
2261
2262                 if (hotplug_status)
2263                         i9xx_hpd_irq_handler(dev_priv, hotplug_status);
2264
2265                 valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2266         } while (0);
2267
2268         enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2269
2270         return ret;
2271 }
2272
2273 static irqreturn_t cherryview_irq_handler(int irq, void *arg)
2274 {
2275         struct drm_i915_private *dev_priv = arg;
2276         irqreturn_t ret = IRQ_NONE;
2277
2278         if (!intel_irqs_enabled(dev_priv))
2279                 return IRQ_NONE;
2280
2281         /* IRQs are synced during runtime_suspend, we don't require a wakeref */
2282         disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2283
2284         do {
2285                 u32 master_ctl, iir;
2286                 u32 pipe_stats[I915_MAX_PIPES] = {};
2287                 u32 hotplug_status = 0;
2288                 u32 gt_iir[4];
2289                 u32 ier = 0;
2290
2291                 master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
2292                 iir = I915_READ(VLV_IIR);
2293
2294                 if (master_ctl == 0 && iir == 0)
2295                         break;
2296
2297                 ret = IRQ_HANDLED;
2298
2299                 /*
2300                  * Theory on interrupt generation, based on empirical evidence:
2301                  *
2302                  * x = ((VLV_IIR & VLV_IER) ||
2303                  *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
2304                  *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
2305                  *
2306                  * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2307                  * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
2308                  * guarantee the CPU interrupt will be raised again even if we
2309                  * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
2310                  * bits this time around.
2311                  */
2312                 I915_WRITE(GEN8_MASTER_IRQ, 0);
2313                 ier = I915_READ(VLV_IER);
2314                 I915_WRITE(VLV_IER, 0);
2315
2316                 gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2317
2318                 if (iir & I915_DISPLAY_PORT_INTERRUPT)
2319                         hotplug_status = i9xx_hpd_irq_ack(dev_priv);
2320
2321                 /* Call regardless, as some status bits might not be
2322                  * signalled in iir */
2323                 i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
2324
2325                 if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2326                            I915_LPE_PIPE_B_INTERRUPT |
2327                            I915_LPE_PIPE_C_INTERRUPT))
2328                         intel_lpe_audio_irq_handler(dev_priv);
2329
2330                 /*
2331                  * VLV_IIR is single buffered, and reflects the level
2332                  * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
2333                  */
2334                 if (iir)
2335                         I915_WRITE(VLV_IIR, iir);
2336
2337                 I915_WRITE(VLV_IER, ier);
2338                 I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2339
2340                 gen8_gt_irq_handler(dev_priv, master_ctl, gt_iir);
2341
2342                 if (hotplug_status)
2343                         i9xx_hpd_irq_handler(dev_priv, hotplug_status);
2344
2345                 valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2346         } while (0);
2347
2348         enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2349
2350         return ret;
2351 }
2352
2353 static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
2354                                 u32 hotplug_trigger,
2355                                 const u32 hpd[HPD_NUM_PINS])
2356 {
2357         u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2358
2359         /*
2360          * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
2361          * unless we touch the hotplug register, even if hotplug_trigger is
2362          * zero. Not acking leads to "The master control interrupt lied (SDE)!"
2363          * errors.
2364          */
2365         dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2366         if (!hotplug_trigger) {
2367                 u32 mask = PORTA_HOTPLUG_STATUS_MASK |
2368                         PORTD_HOTPLUG_STATUS_MASK |
2369                         PORTC_HOTPLUG_STATUS_MASK |
2370                         PORTB_HOTPLUG_STATUS_MASK;
2371                 dig_hotplug_reg &= ~mask;
2372         }
2373
2374         I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2375         if (!hotplug_trigger)
2376                 return;
2377
2378         intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
2379                            dig_hotplug_reg, hpd,
2380                            pch_port_hotplug_long_detect);
2381
2382         intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2383 }
2384
2385 static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
2386 {
2387         int pipe;
2388         u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
2389
2390         ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
2391
2392         if (pch_iir & SDE_AUDIO_POWER_MASK) {
2393                 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2394                                SDE_AUDIO_POWER_SHIFT);
2395                 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2396                                  port_name(port));
2397         }
2398
2399         if (pch_iir & SDE_AUX_MASK)
2400                 dp_aux_irq_handler(dev_priv);
2401
2402         if (pch_iir & SDE_GMBUS)
2403                 gmbus_irq_handler(dev_priv);
2404
2405         if (pch_iir & SDE_AUDIO_HDCP_MASK)
2406                 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2407
2408         if (pch_iir & SDE_AUDIO_TRANS_MASK)
2409                 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2410
2411         if (pch_iir & SDE_POISON)
2412                 DRM_ERROR("PCH poison interrupt\n");
2413
2414         if (pch_iir & SDE_FDI_MASK)
2415                 for_each_pipe(dev_priv, pipe)
2416                         DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
2417                                          pipe_name(pipe),
2418                                          I915_READ(FDI_RX_IIR(pipe)));
2419
2420         if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2421                 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2422
2423         if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2424                 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2425
2426         if (pch_iir & SDE_TRANSA_FIFO_UNDER)
2427                 intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
2428
2429         if (pch_iir & SDE_TRANSB_FIFO_UNDER)
2430                 intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
2431 }
2432
2433 static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
2434 {
2435         u32 err_int = I915_READ(GEN7_ERR_INT);
2436         enum pipe pipe;
2437
2438         if (err_int & ERR_INT_POISON)
2439                 DRM_ERROR("Poison interrupt\n");
2440
2441         for_each_pipe(dev_priv, pipe) {
2442                 if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
2443                         intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2444
2445                 if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
2446                         if (IS_IVYBRIDGE(dev_priv))
2447                                 ivb_pipe_crc_irq_handler(dev_priv, pipe);
2448                         else
2449                                 hsw_pipe_crc_irq_handler(dev_priv, pipe);
2450                 }
2451         }
2452
2453         I915_WRITE(GEN7_ERR_INT, err_int);
2454 }
2455
2456 static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
2457 {
2458         u32 serr_int = I915_READ(SERR_INT);
2459         enum pipe pipe;
2460
2461         if (serr_int & SERR_INT_POISON)
2462                 DRM_ERROR("PCH poison interrupt\n");
2463
2464         for_each_pipe(dev_priv, pipe)
2465                 if (serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pipe))
2466                         intel_pch_fifo_underrun_irq_handler(dev_priv, pipe);
2467
2468         I915_WRITE(SERR_INT, serr_int);
2469 }
2470
2471 static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
2472 {
2473         int pipe;
2474         u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2475
2476         ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
2477
2478         if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2479                 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
2480                                SDE_AUDIO_POWER_SHIFT_CPT);
2481                 DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2482                                  port_name(port));
2483         }
2484
2485         if (pch_iir & SDE_AUX_MASK_CPT)
2486                 dp_aux_irq_handler(dev_priv);
2487
2488         if (pch_iir & SDE_GMBUS_CPT)
2489                 gmbus_irq_handler(dev_priv);
2490
2491         if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
2492                 DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
2493
2494         if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
2495                 DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
2496
2497         if (pch_iir & SDE_FDI_MASK_CPT)
2498                 for_each_pipe(dev_priv, pipe)
2499                         DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
2500                                          pipe_name(pipe),
2501                                          I915_READ(FDI_RX_IIR(pipe)));
2502
2503         if (pch_iir & SDE_ERROR_CPT)
2504                 cpt_serr_int_handler(dev_priv);
2505 }
2506
2507 static void icp_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir,
2508                             const u32 *pins)
2509 {
2510         u32 ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_ICP;
2511         u32 tc_hotplug_trigger = pch_iir & SDE_TC_MASK_ICP;
2512         u32 pin_mask = 0, long_mask = 0;
2513
2514         if (ddi_hotplug_trigger) {
2515                 u32 dig_hotplug_reg;
2516
2517                 dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_DDI);
2518                 I915_WRITE(SHOTPLUG_CTL_DDI, dig_hotplug_reg);
2519
2520                 intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2521                                    ddi_hotplug_trigger,
2522                                    dig_hotplug_reg, pins,
2523                                    icp_ddi_port_hotplug_long_detect);
2524         }
2525
2526         if (tc_hotplug_trigger) {
2527                 u32 dig_hotplug_reg;
2528
2529                 dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_TC);
2530                 I915_WRITE(SHOTPLUG_CTL_TC, dig_hotplug_reg);
2531
2532                 intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2533                                    tc_hotplug_trigger,
2534                                    dig_hotplug_reg, pins,
2535                                    icp_tc_port_hotplug_long_detect);
2536         }
2537
2538         if (pin_mask)
2539                 intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2540
2541         if (pch_iir & SDE_GMBUS_ICP)
2542                 gmbus_irq_handler(dev_priv);
2543 }
2544
2545 static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
2546 {
2547         u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
2548                 ~SDE_PORTE_HOTPLUG_SPT;
2549         u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
2550         u32 pin_mask = 0, long_mask = 0;
2551
2552         if (hotplug_trigger) {
2553                 u32 dig_hotplug_reg;
2554
2555                 dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2556                 I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2557
2558                 intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2559                                    hotplug_trigger, dig_hotplug_reg, hpd_spt,
2560                                    spt_port_hotplug_long_detect);
2561         }
2562
2563         if (hotplug2_trigger) {
2564                 u32 dig_hotplug_reg;
2565
2566                 dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
2567                 I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
2568
2569                 intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2570                                    hotplug2_trigger, dig_hotplug_reg, hpd_spt,
2571                                    spt_port_hotplug2_long_detect);
2572         }
2573
2574         if (pin_mask)
2575                 intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2576
2577         if (pch_iir & SDE_GMBUS_CPT)
2578                 gmbus_irq_handler(dev_priv);
2579 }
2580
2581 static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
2582                                 u32 hotplug_trigger,
2583                                 const u32 hpd[HPD_NUM_PINS])
2584 {
2585         u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2586
2587         dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2588         I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2589
2590         intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
2591                            dig_hotplug_reg, hpd,
2592                            ilk_port_hotplug_long_detect);
2593
2594         intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2595 }
2596
2597 static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
2598                                     u32 de_iir)
2599 {
2600         enum pipe pipe;
2601         u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
2602
2603         if (hotplug_trigger)
2604                 ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
2605
2606         if (de_iir & DE_AUX_CHANNEL_A)
2607                 dp_aux_irq_handler(dev_priv);
2608
2609         if (de_iir & DE_GSE)
2610                 intel_opregion_asle_intr(dev_priv);
2611
2612         if (de_iir & DE_POISON)
2613                 DRM_ERROR("Poison interrupt\n");
2614
2615         for_each_pipe(dev_priv, pipe) {
2616                 if (de_iir & DE_PIPE_VBLANK(pipe))
2617                         drm_handle_vblank(&dev_priv->drm, pipe);
2618
2619                 if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
2620                         intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2621
2622                 if (de_iir & DE_PIPE_CRC_DONE(pipe))
2623                         i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2624         }
2625
2626         /* check event from PCH */
2627         if (de_iir & DE_PCH_EVENT) {
2628                 u32 pch_iir = I915_READ(SDEIIR);
2629
2630                 if (HAS_PCH_CPT(dev_priv))
2631                         cpt_irq_handler(dev_priv, pch_iir);
2632                 else
2633                         ibx_irq_handler(dev_priv, pch_iir);
2634
2635                 /* should clear PCH hotplug event before clear CPU irq */
2636                 I915_WRITE(SDEIIR, pch_iir);
2637         }
2638
2639         if (IS_GEN(dev_priv, 5) && de_iir & DE_PCU_EVENT)
2640                 ironlake_rps_change_irq_handler(dev_priv);
2641 }
2642
2643 static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
2644                                     u32 de_iir)
2645 {
2646         enum pipe pipe;
2647         u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
2648
2649         if (hotplug_trigger)
2650                 ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
2651
2652         if (de_iir & DE_ERR_INT_IVB)
2653                 ivb_err_int_handler(dev_priv);
2654
2655         if (de_iir & DE_EDP_PSR_INT_HSW) {
2656                 u32 psr_iir = I915_READ(EDP_PSR_IIR);
2657
2658                 intel_psr_irq_handler(dev_priv, psr_iir);
2659                 I915_WRITE(EDP_PSR_IIR, psr_iir);
2660         }
2661
2662         if (de_iir & DE_AUX_CHANNEL_A_IVB)
2663                 dp_aux_irq_handler(dev_priv);
2664
2665         if (de_iir & DE_GSE_IVB)
2666                 intel_opregion_asle_intr(dev_priv);
2667
2668         for_each_pipe(dev_priv, pipe) {
2669                 if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
2670                         drm_handle_vblank(&dev_priv->drm, pipe);
2671         }
2672
2673         /* check event from PCH */
2674         if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
2675                 u32 pch_iir = I915_READ(SDEIIR);
2676
2677                 cpt_irq_handler(dev_priv, pch_iir);
2678
2679                 /* clear PCH hotplug event before clear CPU irq */
2680                 I915_WRITE(SDEIIR, pch_iir);
2681         }
2682 }
2683
2684 /*
2685  * To handle irqs with the minimum potential races with fresh interrupts, we:
2686  * 1 - Disable Master Interrupt Control.
2687  * 2 - Find the source(s) of the interrupt.
2688  * 3 - Clear the Interrupt Identity bits (IIR).
2689  * 4 - Process the interrupt(s) that had bits set in the IIRs.
2690  * 5 - Re-enable Master Interrupt Control.
2691  */
2692 static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2693 {
2694         struct drm_i915_private *dev_priv = arg;
2695         u32 de_iir, gt_iir, de_ier, sde_ier = 0;
2696         irqreturn_t ret = IRQ_NONE;
2697
2698         if (!intel_irqs_enabled(dev_priv))
2699                 return IRQ_NONE;
2700
2701         /* IRQs are synced during runtime_suspend, we don't require a wakeref */
2702         disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2703
2704         /* disable master interrupt before clearing iir  */
2705         de_ier = I915_READ(DEIER);
2706         I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
2707
2708         /* Disable south interrupts. We'll only write to SDEIIR once, so further
2709          * interrupts will will be stored on its back queue, and then we'll be
2710          * able to process them after we restore SDEIER (as soon as we restore
2711          * it, we'll get an interrupt if SDEIIR still has something to process
2712          * due to its back queue). */
2713         if (!HAS_PCH_NOP(dev_priv)) {
2714                 sde_ier = I915_READ(SDEIER);
2715                 I915_WRITE(SDEIER, 0);
2716         }
2717
2718         /* Find, clear, then process each source of interrupt */
2719
2720         gt_iir = I915_READ(GTIIR);
2721         if (gt_iir) {
2722                 I915_WRITE(GTIIR, gt_iir);
2723                 ret = IRQ_HANDLED;
2724                 if (INTEL_GEN(dev_priv) >= 6)
2725                         snb_gt_irq_handler(dev_priv, gt_iir);
2726                 else
2727                         ilk_gt_irq_handler(dev_priv, gt_iir);
2728         }
2729
2730         de_iir = I915_READ(DEIIR);
2731         if (de_iir) {
2732                 I915_WRITE(DEIIR, de_iir);
2733                 ret = IRQ_HANDLED;
2734                 if (INTEL_GEN(dev_priv) >= 7)
2735                         ivb_display_irq_handler(dev_priv, de_iir);
2736                 else
2737                         ilk_display_irq_handler(dev_priv, de_iir);
2738         }
2739
2740         if (INTEL_GEN(dev_priv) >= 6) {
2741                 u32 pm_iir = I915_READ(GEN6_PMIIR);
2742                 if (pm_iir) {
2743                         I915_WRITE(GEN6_PMIIR, pm_iir);
2744                         ret = IRQ_HANDLED;
2745                         gen6_rps_irq_handler(dev_priv, pm_iir);
2746                 }
2747         }
2748
2749         I915_WRITE(DEIER, de_ier);
2750         if (!HAS_PCH_NOP(dev_priv))
2751                 I915_WRITE(SDEIER, sde_ier);
2752
2753         /* IRQs are synced during runtime_suspend, we don't require a wakeref */
2754         enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2755
2756         return ret;
2757 }
2758
2759 static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
2760                                 u32 hotplug_trigger,
2761                                 const u32 hpd[HPD_NUM_PINS])
2762 {
2763         u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2764
2765         dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2766         I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2767
2768         intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
2769                            dig_hotplug_reg, hpd,
2770                            bxt_port_hotplug_long_detect);
2771
2772         intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2773 }
2774
2775 static void gen11_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2776 {
2777         u32 pin_mask = 0, long_mask = 0;
2778         u32 trigger_tc = iir & GEN11_DE_TC_HOTPLUG_MASK;
2779         u32 trigger_tbt = iir & GEN11_DE_TBT_HOTPLUG_MASK;
2780
2781         if (trigger_tc) {
2782                 u32 dig_hotplug_reg;
2783
2784                 dig_hotplug_reg = I915_READ(GEN11_TC_HOTPLUG_CTL);
2785                 I915_WRITE(GEN11_TC_HOTPLUG_CTL, dig_hotplug_reg);
2786
2787                 intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tc,
2788                                    dig_hotplug_reg, hpd_gen11,
2789                                    gen11_port_hotplug_long_detect);
2790         }
2791
2792         if (trigger_tbt) {
2793                 u32 dig_hotplug_reg;
2794
2795                 dig_hotplug_reg = I915_READ(GEN11_TBT_HOTPLUG_CTL);
2796                 I915_WRITE(GEN11_TBT_HOTPLUG_CTL, dig_hotplug_reg);
2797
2798                 intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tbt,
2799                                    dig_hotplug_reg, hpd_gen11,
2800                                    gen11_port_hotplug_long_detect);
2801         }
2802
2803         if (pin_mask)
2804                 intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2805         else
2806                 DRM_ERROR("Unexpected DE HPD interrupt 0x%08x\n", iir);
2807 }
2808
2809 static u32 gen8_de_port_aux_mask(struct drm_i915_private *dev_priv)
2810 {
2811         u32 mask = GEN8_AUX_CHANNEL_A;
2812
2813         if (INTEL_GEN(dev_priv) >= 9)
2814                 mask |= GEN9_AUX_CHANNEL_B |
2815                         GEN9_AUX_CHANNEL_C |
2816                         GEN9_AUX_CHANNEL_D;
2817
2818         if (IS_CNL_WITH_PORT_F(dev_priv))
2819                 mask |= CNL_AUX_CHANNEL_F;
2820
2821         if (INTEL_GEN(dev_priv) >= 11)
2822                 mask |= ICL_AUX_CHANNEL_E |
2823                         CNL_AUX_CHANNEL_F;
2824
2825         return mask;
2826 }
2827
2828 static irqreturn_t
2829 gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2830 {
2831         irqreturn_t ret = IRQ_NONE;
2832         u32 iir;
2833         enum pipe pipe;
2834
2835         if (master_ctl & GEN8_DE_MISC_IRQ) {
2836                 iir = I915_READ(GEN8_DE_MISC_IIR);
2837                 if (iir) {
2838                         bool found = false;
2839
2840                         I915_WRITE(GEN8_DE_MISC_IIR, iir);
2841                         ret = IRQ_HANDLED;
2842
2843                         if (iir & GEN8_DE_MISC_GSE) {
2844                                 intel_opregion_asle_intr(dev_priv);
2845                                 found = true;
2846                         }
2847
2848                         if (iir & GEN8_DE_EDP_PSR) {
2849                                 u32 psr_iir = I915_READ(EDP_PSR_IIR);
2850
2851                                 intel_psr_irq_handler(dev_priv, psr_iir);
2852                                 I915_WRITE(EDP_PSR_IIR, psr_iir);
2853                                 found = true;
2854                         }
2855
2856                         if (!found)
2857                                 DRM_ERROR("Unexpected DE Misc interrupt\n");
2858                 }
2859                 else
2860                         DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2861         }
2862
2863         if (INTEL_GEN(dev_priv) >= 11 && (master_ctl & GEN11_DE_HPD_IRQ)) {
2864                 iir = I915_READ(GEN11_DE_HPD_IIR);
2865                 if (iir) {
2866                         I915_WRITE(GEN11_DE_HPD_IIR, iir);
2867                         ret = IRQ_HANDLED;
2868                         gen11_hpd_irq_handler(dev_priv, iir);
2869                 } else {
2870                         DRM_ERROR("The master control interrupt lied, (DE HPD)!\n");
2871                 }
2872         }
2873
2874         if (master_ctl & GEN8_DE_PORT_IRQ) {
2875                 iir = I915_READ(GEN8_DE_PORT_IIR);
2876                 if (iir) {
2877                         u32 tmp_mask;
2878                         bool found = false;
2879
2880                         I915_WRITE(GEN8_DE_PORT_IIR, iir);
2881                         ret = IRQ_HANDLED;
2882
2883                         if (iir & gen8_de_port_aux_mask(dev_priv)) {
2884                                 dp_aux_irq_handler(dev_priv);
2885                                 found = true;
2886                         }
2887
2888                         if (IS_GEN9_LP(dev_priv)) {
2889                                 tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2890                                 if (tmp_mask) {
2891                                         bxt_hpd_irq_handler(dev_priv, tmp_mask,
2892                                                             hpd_bxt);
2893                                         found = true;
2894                                 }
2895                         } else if (IS_BROADWELL(dev_priv)) {
2896                                 tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2897                                 if (tmp_mask) {
2898                                         ilk_hpd_irq_handler(dev_priv,
2899                                                             tmp_mask, hpd_bdw);
2900                                         found = true;
2901                                 }
2902                         }
2903
2904                         if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
2905                                 gmbus_irq_handler(dev_priv);
2906                                 found = true;
2907                         }
2908
2909                         if (!found)
2910                                 DRM_ERROR("Unexpected DE Port interrupt\n");
2911                 }
2912                 else
2913                         DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
2914         }
2915
2916         for_each_pipe(dev_priv, pipe) {
2917                 u32 fault_errors;
2918
2919                 if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2920                         continue;
2921
2922                 iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2923                 if (!iir) {
2924                         DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2925                         continue;
2926                 }
2927
2928                 ret = IRQ_HANDLED;
2929                 I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2930
2931                 if (iir & GEN8_PIPE_VBLANK)
2932                         drm_handle_vblank(&dev_priv->drm, pipe);
2933
2934                 if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
2935                         hsw_pipe_crc_irq_handler(dev_priv, pipe);
2936
2937                 if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2938                         intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2939
2940                 fault_errors = iir;
2941                 if (INTEL_GEN(dev_priv) >= 9)
2942                         fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2943                 else
2944                         fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2945
2946                 if (fault_errors)
2947                         DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
2948                                   pipe_name(pipe),
2949                                   fault_errors);
2950         }
2951
2952         if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2953             master_ctl & GEN8_DE_PCH_IRQ) {
2954                 /*
2955                  * FIXME(BDW): Assume for now that the new interrupt handling
2956                  * scheme also closed the SDE interrupt handling race we've seen
2957                  * on older pch-split platforms. But this needs testing.
2958                  */
2959                 iir = I915_READ(SDEIIR);
2960                 if (iir) {
2961                         I915_WRITE(SDEIIR, iir);
2962                         ret = IRQ_HANDLED;
2963
2964                         if (INTEL_PCH_TYPE(dev_priv) >= PCH_MCC)
2965                                 icp_irq_handler(dev_priv, iir, hpd_mcc);
2966                         else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
2967                                 icp_irq_handler(dev_priv, iir, hpd_icp);
2968                         else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
2969                                 spt_irq_handler(dev_priv, iir);
2970                         else
2971                                 cpt_irq_handler(dev_priv, iir);
2972                 } else {
2973                         /*
2974                          * Like on previous PCH there seems to be something
2975                          * fishy going on with forwarding PCH interrupts.
2976                          */
2977                         DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
2978                 }
2979         }
2980
2981         return ret;
2982 }
2983
2984 static inline u32 gen8_master_intr_disable(void __iomem * const regs)
2985 {
2986         raw_reg_write(regs, GEN8_MASTER_IRQ, 0);
2987
2988         /*
2989          * Now with master disabled, get a sample of level indications
2990          * for this interrupt. Indications will be cleared on related acks.
2991          * New indications can and will light up during processing,
2992          * and will generate new interrupt after enabling master.
2993          */
2994         return raw_reg_read(regs, GEN8_MASTER_IRQ);
2995 }
2996
2997 static inline void gen8_master_intr_enable(void __iomem * const regs)
2998 {
2999         raw_reg_write(regs, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3000 }
3001
3002 static irqreturn_t gen8_irq_handler(int irq, void *arg)
3003 {
3004         struct drm_i915_private *dev_priv = arg;
3005         void __iomem * const regs = dev_priv->uncore.regs;
3006         u32 master_ctl;
3007         u32 gt_iir[4];
3008
3009         if (!intel_irqs_enabled(dev_priv))
3010                 return IRQ_NONE;
3011
3012         master_ctl = gen8_master_intr_disable(regs);
3013         if (!master_ctl) {
3014                 gen8_master_intr_enable(regs);
3015                 return IRQ_NONE;
3016         }
3017
3018         /* Find, clear, then process each source of interrupt */
3019         gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
3020
3021         /* IRQs are synced during runtime_suspend, we don't require a wakeref */
3022         if (master_ctl & ~GEN8_GT_IRQS) {
3023                 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
3024                 gen8_de_irq_handler(dev_priv, master_ctl);
3025                 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
3026         }
3027
3028         gen8_master_intr_enable(regs);
3029
3030         gen8_gt_irq_handler(dev_priv, master_ctl, gt_iir);
3031
3032         return IRQ_HANDLED;
3033 }
3034
3035 static u32
3036 gen11_gt_engine_identity(struct intel_gt *gt,
3037                          const unsigned int bank, const unsigned int bit)
3038 {
3039         void __iomem * const regs = gt->uncore->regs;
3040         u32 timeout_ts;
3041         u32 ident;
3042
3043         lockdep_assert_held(&gt->i915->irq_lock);
3044
3045         raw_reg_write(regs, GEN11_IIR_REG_SELECTOR(bank), BIT(bit));
3046
3047         /*
3048          * NB: Specs do not specify how long to spin wait,
3049          * so we do ~100us as an educated guess.
3050          */
3051         timeout_ts = (local_clock() >> 10) + 100;
3052         do {
3053                 ident = raw_reg_read(regs, GEN11_INTR_IDENTITY_REG(bank));
3054         } while (!(ident & GEN11_INTR_DATA_VALID) &&
3055                  !time_after32(local_clock() >> 10, timeout_ts));
3056
3057         if (unlikely(!(ident & GEN11_INTR_DATA_VALID))) {
3058                 DRM_ERROR("INTR_IDENTITY_REG%u:%u 0x%08x not valid!\n",
3059                           bank, bit, ident);
3060                 return 0;
3061         }
3062
3063         raw_reg_write(regs, GEN11_INTR_IDENTITY_REG(bank),
3064                       GEN11_INTR_DATA_VALID);
3065
3066         return ident;
3067 }
3068
3069 static void
3070 gen11_other_irq_handler(struct intel_gt *gt, const u8 instance,
3071                         const u16 iir)
3072 {
3073         struct drm_i915_private *i915 = gt->i915;
3074
3075         if (instance == OTHER_GUC_INSTANCE)
3076                 return gen11_guc_irq_handler(i915, iir);
3077
3078         if (instance == OTHER_GTPM_INSTANCE)
3079                 return gen11_rps_irq_handler(i915, iir);
3080
3081         WARN_ONCE(1, "unhandled other interrupt instance=0x%x, iir=0x%x\n",
3082                   instance, iir);
3083 }
3084
3085 static void
3086 gen11_engine_irq_handler(struct intel_gt *gt, const u8 class,
3087                          const u8 instance, const u16 iir)
3088 {
3089         struct intel_engine_cs *engine;
3090
3091         if (instance <= MAX_ENGINE_INSTANCE)
3092                 engine = gt->i915->engine_class[class][instance];
3093         else
3094                 engine = NULL;
3095
3096         if (likely(engine))
3097                 return gen8_cs_irq_handler(engine, iir);
3098
3099         WARN_ONCE(1, "unhandled engine interrupt class=0x%x, instance=0x%x\n",
3100                   class, instance);
3101 }
3102
3103 static void
3104 gen11_gt_identity_handler(struct intel_gt *gt, const u32 identity)
3105 {
3106         const u8 class = GEN11_INTR_ENGINE_CLASS(identity);
3107         const u8 instance = GEN11_INTR_ENGINE_INSTANCE(identity);
3108         const u16 intr = GEN11_INTR_ENGINE_INTR(identity);
3109
3110         if (unlikely(!intr))
3111                 return;
3112
3113         if (class <= COPY_ENGINE_CLASS)
3114                 return gen11_engine_irq_handler(gt, class, instance, intr);
3115
3116         if (class == OTHER_CLASS)
3117                 return gen11_other_irq_handler(gt, instance, intr);
3118
3119         WARN_ONCE(1, "unknown interrupt class=0x%x, instance=0x%x, intr=0x%x\n",
3120                   class, instance, intr);
3121 }
3122
3123 static void
3124 gen11_gt_bank_handler(struct intel_gt *gt, const unsigned int bank)
3125 {
3126         void __iomem * const regs = gt->uncore->regs;
3127         unsigned long intr_dw;
3128         unsigned int bit;
3129
3130         lockdep_assert_held(&gt->i915->irq_lock);
3131
3132         intr_dw = raw_reg_read(regs, GEN11_GT_INTR_DW(bank));
3133
3134         for_each_set_bit(bit, &intr_dw, 32) {
3135                 const u32 ident = gen11_gt_engine_identity(gt, bank, bit);
3136
3137                 gen11_gt_identity_handler(gt, ident);
3138         }
3139
3140         /* Clear must be after shared has been served for engine */
3141         raw_reg_write(regs, GEN11_GT_INTR_DW(bank), intr_dw);
3142 }
3143
3144 static void
3145 gen11_gt_irq_handler(struct intel_gt *gt, const u32 master_ctl)
3146 {
3147         struct drm_i915_private *i915 = gt->i915;
3148         unsigned int bank;
3149
3150         spin_lock(&i915->irq_lock);
3151
3152         for (bank = 0; bank < 2; bank++) {
3153                 if (master_ctl & GEN11_GT_DW_IRQ(bank))
3154                         gen11_gt_bank_handler(gt, bank);
3155         }
3156
3157         spin_unlock(&i915->irq_lock);
3158 }
3159
3160 static u32
3161 gen11_gu_misc_irq_ack(struct intel_gt *gt, const u32 master_ctl)
3162 {
3163         void __iomem * const regs = gt->uncore->regs;
3164         u32 iir;
3165
3166         if (!(master_ctl & GEN11_GU_MISC_IRQ))
3167                 return 0;
3168
3169         iir = raw_reg_read(regs, GEN11_GU_MISC_IIR);
3170         if (likely(iir))
3171                 raw_reg_write(regs, GEN11_GU_MISC_IIR, iir);
3172
3173         return iir;
3174 }
3175
3176 static void
3177 gen11_gu_misc_irq_handler(struct intel_gt *gt, const u32 iir)
3178 {
3179         if (iir & GEN11_GU_MISC_GSE)
3180                 intel_opregion_asle_intr(gt->i915);
3181 }
3182
3183 static inline u32 gen11_master_intr_disable(void __iomem * const regs)
3184 {
3185         raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
3186
3187         /*
3188          * Now with master disabled, get a sample of level indications
3189          * for this interrupt. Indications will be cleared on related acks.
3190          * New indications can and will light up during processing,
3191          * and will generate new interrupt after enabling master.
3192          */
3193         return raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
3194 }
3195
3196 static inline void gen11_master_intr_enable(void __iomem * const regs)
3197 {
3198         raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
3199 }
3200
3201 static irqreturn_t gen11_irq_handler(int irq, void *arg)
3202 {
3203         struct drm_i915_private * const i915 = arg;
3204         void __iomem * const regs = i915->uncore.regs;
3205         struct intel_gt *gt = &i915->gt;
3206         u32 master_ctl;
3207         u32 gu_misc_iir;
3208
3209         if (!intel_irqs_enabled(i915))
3210                 return IRQ_NONE;
3211
3212         master_ctl = gen11_master_intr_disable(regs);
3213         if (!master_ctl) {
3214                 gen11_master_intr_enable(regs);
3215                 return IRQ_NONE;
3216         }
3217
3218         /* Find, clear, then process each source of interrupt. */
3219         gen11_gt_irq_handler(gt, master_ctl);
3220
3221         /* IRQs are synced during runtime_suspend, we don't require a wakeref */
3222         if (master_ctl & GEN11_DISPLAY_IRQ) {
3223                 const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
3224
3225                 disable_rpm_wakeref_asserts(&i915->runtime_pm);
3226                 /*
3227                  * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
3228                  * for the display related bits.
3229                  */
3230                 gen8_de_irq_handler(i915, disp_ctl);
3231                 enable_rpm_wakeref_asserts(&i915->runtime_pm);
3232         }
3233
3234         gu_misc_iir = gen11_gu_misc_irq_ack(gt, master_ctl);
3235
3236         gen11_master_intr_enable(regs);
3237
3238         gen11_gu_misc_irq_handler(gt, gu_misc_iir);
3239
3240         return IRQ_HANDLED;
3241 }
3242
3243 /* Called from drm generic code, passed 'crtc' which
3244  * we use as a pipe index
3245  */
3246 int i8xx_enable_vblank(struct drm_crtc *crtc)
3247 {
3248         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3249         enum pipe pipe = to_intel_crtc(crtc)->pipe;
3250         unsigned long irqflags;
3251
3252         spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3253         i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
3254         spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3255
3256         return 0;
3257 }
3258
3259 int i945gm_enable_vblank(struct drm_crtc *crtc)
3260 {
3261         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3262
3263         if (dev_priv->i945gm_vblank.enabled++ == 0)
3264                 schedule_work(&dev_priv->i945gm_vblank.work);
3265
3266         return i8xx_enable_vblank(crtc);
3267 }
3268
3269 int i965_enable_vblank(struct drm_crtc *crtc)
3270 {
3271         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3272         enum pipe pipe = to_intel_crtc(crtc)->pipe;
3273         unsigned long irqflags;
3274
3275         spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3276         i915_enable_pipestat(dev_priv, pipe,
3277                              PIPE_START_VBLANK_INTERRUPT_STATUS);
3278         spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3279
3280         return 0;
3281 }
3282
3283 int ilk_enable_vblank(struct drm_crtc *crtc)
3284 {
3285         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3286         enum pipe pipe = to_intel_crtc(crtc)->pipe;
3287         unsigned long irqflags;
3288         u32 bit = INTEL_GEN(dev_priv) >= 7 ?
3289                 DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3290
3291         spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3292         ilk_enable_display_irq(dev_priv, bit);
3293         spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3294
3295         /* Even though there is no DMC, frame counter can get stuck when
3296          * PSR is active as no frames are generated.
3297          */
3298         if (HAS_PSR(dev_priv))
3299                 drm_crtc_vblank_restore(crtc);
3300
3301         return 0;
3302 }
3303
3304 int bdw_enable_vblank(struct drm_crtc *crtc)
3305 {
3306         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3307         enum pipe pipe = to_intel_crtc(crtc)->pipe;
3308         unsigned long irqflags;
3309
3310         spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3311         bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3312         spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3313
3314         /* Even if there is no DMC, frame counter can get stuck when
3315          * PSR is active as no frames are generated, so check only for PSR.
3316          */
3317         if (HAS_PSR(dev_priv))
3318                 drm_crtc_vblank_restore(crtc);
3319
3320         return 0;
3321 }
3322
3323 /* Called from drm generic code, passed 'crtc' which
3324  * we use as a pipe index
3325  */
3326 void i8xx_disable_vblank(struct drm_crtc *crtc)
3327 {
3328         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3329         enum pipe pipe = to_intel_crtc(crtc)->pipe;
3330         unsigned long irqflags;
3331
3332         spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3333         i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
3334         spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3335 }
3336
3337 void i945gm_disable_vblank(struct drm_crtc *crtc)
3338 {
3339         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3340
3341         i8xx_disable_vblank(crtc);
3342
3343         if (--dev_priv->i945gm_vblank.enabled == 0)
3344                 schedule_work(&dev_priv->i945gm_vblank.work);
3345 }
3346
3347 void i965_disable_vblank(struct drm_crtc *crtc)
3348 {
3349         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3350         enum pipe pipe = to_intel_crtc(crtc)->pipe;
3351         unsigned long irqflags;
3352
3353         spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3354         i915_disable_pipestat(dev_priv, pipe,
3355                               PIPE_START_VBLANK_INTERRUPT_STATUS);
3356         spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3357 }
3358
3359 void ilk_disable_vblank(struct drm_crtc *crtc)
3360 {
3361         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3362         enum pipe pipe = to_intel_crtc(crtc)->pipe;
3363         unsigned long irqflags;
3364         u32 bit = INTEL_GEN(dev_priv) >= 7 ?
3365                 DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3366
3367         spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3368         ilk_disable_display_irq(dev_priv, bit);
3369         spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3370 }
3371
3372 void bdw_disable_vblank(struct drm_crtc *crtc)
3373 {
3374         struct drm_i915_private *dev_priv = to_i915(crtc->dev);
3375         enum pipe pipe = to_intel_crtc(crtc)->pipe;
3376         unsigned long irqflags;
3377
3378         spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3379         bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3380         spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3381 }
3382
3383 static void i945gm_vblank_work_func(struct work_struct *work)
3384 {
3385         struct drm_i915_private *dev_priv =
3386                 container_of(work, struct drm_i915_private, i945gm_vblank.work);
3387
3388         /*
3389          * Vblank interrupts fail to wake up the device from C3,
3390          * hence we want to prevent C3 usage while vblank interrupts
3391          * are enabled.
3392          */
3393         pm_qos_update_request(&dev_priv->i945gm_vblank.pm_qos,
3394                               READ_ONCE(dev_priv->i945gm_vblank.enabled) ?
3395                               dev_priv->i945gm_vblank.c3_disable_latency :
3396                               PM_QOS_DEFAULT_VALUE);
3397 }
3398
3399 static int cstate_disable_latency(const char *name)
3400 {
3401         const struct cpuidle_driver *drv;
3402         int i;
3403
3404         drv = cpuidle_get_driver();
3405         if (!drv)
3406                 return 0;
3407
3408         for (i = 0; i < drv->state_count; i++) {
3409                 const struct cpuidle_state *state = &drv->states[i];
3410
3411                 if (!strcmp(state->name, name))
3412                         return state->exit_latency ?
3413                                 state->exit_latency - 1 : 0;
3414         }
3415
3416         return 0;
3417 }
3418
3419 static void i945gm_vblank_work_init(struct drm_i915_private *dev_priv)
3420 {
3421         INIT_WORK(&dev_priv->i945gm_vblank.work,
3422                   i945gm_vblank_work_func);
3423
3424         dev_priv->i945gm_vblank.c3_disable_latency =
3425                 cstate_disable_latency("C3");
3426         pm_qos_add_request(&dev_priv->i945gm_vblank.pm_qos,
3427                            PM_QOS_CPU_DMA_LATENCY,
3428                            PM_QOS_DEFAULT_VALUE);
3429 }
3430
3431 static void i945gm_vblank_work_fini(struct drm_i915_private *dev_priv)
3432 {
3433         cancel_work_sync(&dev_priv->i945gm_vblank.work);
3434         pm_qos_remove_request(&dev_priv->i945gm_vblank.pm_qos);
3435 }
3436
3437 static void ibx_irq_reset(struct drm_i915_private *dev_priv)
3438 {
3439         struct intel_uncore *uncore = &dev_priv->uncore;
3440
3441         if (HAS_PCH_NOP(dev_priv))
3442                 return;
3443
3444         GEN3_IRQ_RESET(uncore, SDE);
3445
3446         if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3447                 I915_WRITE(SERR_INT, 0xffffffff);
3448 }
3449
3450 /*
3451  * SDEIER is also touched by the interrupt handler to work around missed PCH
3452  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3453  * instead we unconditionally enable all PCH interrupt sources here, but then
3454  * only unmask them as needed with SDEIMR.
3455  *
3456  * This function needs to be called before interrupts are enabled.
3457  */
3458 static void ibx_irq_pre_postinstall(struct drm_i915_private *dev_priv)
3459 {
3460         if (HAS_PCH_NOP(dev_priv))
3461                 return;
3462
3463         WARN_ON(I915_READ(SDEIER) != 0);
3464         I915_WRITE(SDEIER, 0xffffffff);
3465         POSTING_READ(SDEIER);
3466 }
3467
3468 static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
3469 {
3470         struct intel_uncore *uncore = &dev_priv->uncore;
3471
3472         GEN3_IRQ_RESET(uncore, GT);
3473         if (INTEL_GEN(dev_priv) >= 6)
3474                 GEN3_IRQ_RESET(uncore, GEN6_PM);
3475 }
3476
3477 static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
3478 {
3479         struct intel_uncore *uncore = &dev_priv->uncore;
3480
3481         if (IS_CHERRYVIEW(dev_priv))
3482                 intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
3483         else
3484                 intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK);
3485
3486         i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
3487         intel_uncore_write(uncore, PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3488
3489         i9xx_pipestat_irq_reset(dev_priv);
3490
3491         GEN3_IRQ_RESET(uncore, VLV_);
3492         dev_priv->irq_mask = ~0u;
3493 }
3494
3495 static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
3496 {
3497         struct intel_uncore *uncore = &dev_priv->uncore;
3498
3499         u32 pipestat_mask;
3500         u32 enable_mask;
3501         enum pipe pipe;
3502
3503         pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
3504
3505         i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3506         for_each_pipe(dev_priv, pipe)
3507                 i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3508
3509         enable_mask = I915_DISPLAY_PORT_INTERRUPT |
3510                 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3511                 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3512                 I915_LPE_PIPE_A_INTERRUPT |
3513                 I915_LPE_PIPE_B_INTERRUPT;
3514
3515         if (IS_CHERRYVIEW(dev_priv))
3516                 enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
3517                         I915_LPE_PIPE_C_INTERRUPT;
3518
3519         WARN_ON(dev_priv->irq_mask != ~0u);
3520
3521         dev_priv->irq_mask = ~enable_mask;
3522
3523         GEN3_IRQ_INIT(uncore, VLV_, dev_priv->irq_mask, enable_mask);
3524 }
3525
3526 /* drm_dma.h hooks
3527 */
3528 static void ironlake_irq_reset(struct drm_i915_private *dev_priv)
3529 {
3530         struct intel_uncore *uncore = &dev_priv->uncore;
3531
3532         GEN3_IRQ_RESET(uncore, DE);
3533         if (IS_GEN(dev_priv, 7))
3534                 intel_uncore_write(uncore, GEN7_ERR_INT, 0xffffffff);
3535
3536         if (IS_HASWELL(dev_priv)) {
3537                 intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
3538                 intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
3539         }
3540
3541         gen5_gt_irq_reset(dev_priv);
3542
3543         ibx_irq_reset(dev_priv);
3544 }
3545
3546 static void valleyview_irq_reset(struct drm_i915_private *dev_priv)
3547 {
3548         I915_WRITE(VLV_MASTER_IER, 0);
3549         POSTING_READ(VLV_MASTER_IER);
3550
3551         gen5_gt_irq_reset(dev_priv);
3552
3553         spin_lock_irq(&dev_priv->irq_lock);
3554         if (dev_priv->display_irqs_enabled)
3555                 vlv_display_irq_reset(dev_priv);
3556         spin_unlock_irq(&dev_priv->irq_lock);
3557 }
3558
3559 static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3560 {
3561         struct intel_uncore *uncore = &dev_priv->uncore;
3562
3563         GEN8_IRQ_RESET_NDX(uncore, GT, 0);
3564         GEN8_IRQ_RESET_NDX(uncore, GT, 1);
3565         GEN8_IRQ_RESET_NDX(uncore, GT, 2);
3566         GEN8_IRQ_RESET_NDX(uncore, GT, 3);
3567 }
3568
3569 static void gen8_irq_reset(struct drm_i915_private *dev_priv)
3570 {
3571         struct intel_uncore *uncore = &dev_priv->uncore;
3572         int pipe;
3573
3574         gen8_master_intr_disable(dev_priv->uncore.regs);
3575
3576         gen8_gt_irq_reset(dev_priv);
3577
3578         intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
3579         intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
3580
3581         for_each_pipe(dev_priv, pipe)
3582                 if (intel_display_power_is_enabled(dev_priv,
3583                                                    POWER_DOMAIN_PIPE(pipe)))
3584                         GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
3585
3586         GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
3587         GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
3588         GEN3_IRQ_RESET(uncore, GEN8_PCU_);
3589
3590         if (HAS_PCH_SPLIT(dev_priv))
3591                 ibx_irq_reset(dev_priv);
3592 }
3593
3594 static void gen11_gt_irq_reset(struct intel_gt *gt)
3595 {
3596         struct intel_uncore *uncore = gt->uncore;
3597
3598         /* Disable RCS, BCS, VCS and VECS class engines. */
3599         intel_uncore_write(uncore, GEN11_RENDER_COPY_INTR_ENABLE, 0);
3600         intel_uncore_write(uncore, GEN11_VCS_VECS_INTR_ENABLE,    0);
3601
3602         /* Restore masks irqs on RCS, BCS, VCS and VECS engines. */
3603         intel_uncore_write(uncore, GEN11_RCS0_RSVD_INTR_MASK,   ~0);
3604         intel_uncore_write(uncore, GEN11_BCS_RSVD_INTR_MASK,    ~0);
3605         intel_uncore_write(uncore, GEN11_VCS0_VCS1_INTR_MASK,   ~0);
3606         intel_uncore_write(uncore, GEN11_VCS2_VCS3_INTR_MASK,   ~0);
3607         intel_uncore_write(uncore, GEN11_VECS0_VECS1_INTR_MASK, ~0);
3608
3609         intel_uncore_write(uncore, GEN11_GPM_WGBOXPERF_INTR_ENABLE, 0);
3610         intel_uncore_write(uncore, GEN11_GPM_WGBOXPERF_INTR_MASK,  ~0);
3611         intel_uncore_write(uncore, GEN11_GUC_SG_INTR_ENABLE, 0);
3612         intel_uncore_write(uncore, GEN11_GUC_SG_INTR_MASK,  ~0);
3613 }
3614
3615 static void gen11_irq_reset(struct drm_i915_private *dev_priv)
3616 {
3617         struct intel_uncore *uncore = &dev_priv->uncore;
3618         int pipe;
3619
3620         gen11_master_intr_disable(dev_priv->uncore.regs);
3621
3622         gen11_gt_irq_reset(&dev_priv->gt);
3623
3624         intel_uncore_write(uncore, GEN11_DISPLAY_INT_CTL, 0);
3625
3626         intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
3627         intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
3628
3629         for_each_pipe(dev_priv, pipe)
3630                 if (intel_display_power_is_enabled(dev_priv,
3631                                                    POWER_DOMAIN_PIPE(pipe)))
3632                         GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
3633
3634         GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
3635         GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
3636         GEN3_IRQ_RESET(uncore, GEN11_DE_HPD_);
3637         GEN3_IRQ_RESET(uncore, GEN11_GU_MISC_);
3638         GEN3_IRQ_RESET(uncore, GEN8_PCU_);
3639
3640         if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3641                 GEN3_IRQ_RESET(uncore, SDE);
3642 }
3643
3644 void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
3645                                      u8 pipe_mask)
3646 {
3647         struct intel_uncore *uncore = &dev_priv->uncore;
3648
3649         u32 extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3650         enum pipe pipe;
3651
3652         spin_lock_irq(&dev_priv->irq_lock);
3653
3654         if (!intel_irqs_enabled(dev_priv)) {
3655                 spin_unlock_irq(&dev_priv->irq_lock);
3656                 return;
3657         }
3658
3659         for_each_pipe_masked(dev_priv, pipe, pipe_mask)
3660                 GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
3661                                   dev_priv->de_irq_mask[pipe],
3662                                   ~dev_priv->de_irq_mask[pipe] | extra_ier);
3663
3664         spin_unlock_irq(&dev_priv->irq_lock);
3665 }
3666
3667 void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3668                                      u8 pipe_mask)
3669 {
3670         struct intel_uncore *uncore = &dev_priv->uncore;
3671         enum pipe pipe;
3672
3673         spin_lock_irq(&dev_priv->irq_lock);
3674
3675         if (!intel_irqs_enabled(dev_priv)) {
3676                 spin_unlock_irq(&dev_priv->irq_lock);
3677                 return;
3678         }
3679
3680         for_each_pipe_masked(dev_priv, pipe, pipe_mask)
3681                 GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
3682
3683         spin_unlock_irq(&dev_priv->irq_lock);
3684
3685         /* make sure we're done processing display irqs */
3686         intel_synchronize_irq(dev_priv);
3687 }
3688
3689 static void cherryview_irq_reset(struct drm_i915_private *dev_priv)
3690 {
3691         struct intel_uncore *uncore = &dev_priv->uncore;
3692
3693         I915_WRITE(GEN8_MASTER_IRQ, 0);
3694         POSTING_READ(GEN8_MASTER_IRQ);
3695
3696         gen8_gt_irq_reset(dev_priv);
3697
3698         GEN3_IRQ_RESET(uncore, GEN8_PCU_);
3699
3700         spin_lock_irq(&dev_priv->irq_lock);
3701         if (dev_priv->display_irqs_enabled)
3702                 vlv_display_irq_reset(dev_priv);
3703         spin_unlock_irq(&dev_priv->irq_lock);
3704 }
3705
3706 static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
3707                                   const u32 hpd[HPD_NUM_PINS])
3708 {
3709         struct intel_encoder *encoder;
3710         u32 enabled_irqs = 0;
3711
3712         for_each_intel_encoder(&dev_priv->drm, encoder)
3713                 if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
3714                         enabled_irqs |= hpd[encoder->hpd_pin];
3715
3716         return enabled_irqs;
3717 }
3718
3719 static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
3720 {
3721         u32 hotplug;
3722
3723         /*
3724          * Enable digital hotplug on the PCH, and configure the DP short pulse
3725          * duration to 2ms (which is the minimum in the Display Port spec).
3726          * The pulse duration bits are reserved on LPT+.
3727          */
3728         hotplug = I915_READ(PCH_PORT_HOTPLUG);
3729         hotplug &= ~(PORTB_PULSE_DURATION_MASK |
3730                      PORTC_PULSE_DURATION_MASK |
3731                      PORTD_PULSE_DURATION_MASK);
3732         hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
3733         hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
3734         hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
3735         /*
3736          * When CPU and PCH are on the same package, port A
3737          * HPD must be enabled in both north and south.
3738          */
3739         if (HAS_PCH_LPT_LP(dev_priv))
3740                 hotplug |= PORTA_HOTPLUG_ENABLE;
3741         I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3742 }
3743
3744 static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
3745 {
3746         u32 hotplug_irqs, enabled_irqs;
3747
3748         if (HAS_PCH_IBX(dev_priv)) {
3749                 hotplug_irqs = SDE_HOTPLUG_MASK;
3750                 enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
3751         } else {
3752                 hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3753                 enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
3754         }
3755
3756         ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
3757
3758         ibx_hpd_detection_setup(dev_priv);
3759 }
3760
3761 static void icp_hpd_detection_setup(struct drm_i915_private *dev_priv)
3762 {
3763         u32 hotplug;
3764
3765         hotplug = I915_READ(SHOTPLUG_CTL_DDI);
3766         hotplug |= ICP_DDIA_HPD_ENABLE |
3767                    ICP_DDIB_HPD_ENABLE;
3768         I915_WRITE(SHOTPLUG_CTL_DDI, hotplug);
3769
3770         hotplug = I915_READ(SHOTPLUG_CTL_TC);
3771         hotplug |= ICP_TC_HPD_ENABLE(PORT_TC1) |
3772                    ICP_TC_HPD_ENABLE(PORT_TC2) |
3773                    ICP_TC_HPD_ENABLE(PORT_TC3) |
3774                    ICP_TC_HPD_ENABLE(PORT_TC4);
3775         I915_WRITE(SHOTPLUG_CTL_TC, hotplug);
3776 }
3777
3778 static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv)
3779 {
3780         u32 hotplug_irqs, enabled_irqs;
3781
3782         hotplug_irqs = SDE_DDI_MASK_ICP | SDE_TC_MASK_ICP;
3783         enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_icp);
3784
3785         ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
3786
3787         icp_hpd_detection_setup(dev_priv);
3788 }
3789
3790 static void gen11_hpd_detection_setup(struct drm_i915_private *dev_priv)
3791 {
3792         u32 hotplug;
3793
3794         hotplug = I915_READ(GEN11_TC_HOTPLUG_CTL);
3795         hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3796                    GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3797                    GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3798                    GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3799         I915_WRITE(GEN11_TC_HOTPLUG_CTL, hotplug);
3800
3801         hotplug = I915_READ(GEN11_TBT_HOTPLUG_CTL);
3802         hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3803                    GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3804                    GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3805                    GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3806         I915_WRITE(GEN11_TBT_HOTPLUG_CTL, hotplug);
3807 }
3808
3809 static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
3810 {
3811         u32 hotplug_irqs, enabled_irqs;
3812         u32 val;
3813
3814         enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_gen11);
3815         hotplug_irqs = GEN11_DE_TC_HOTPLUG_MASK | GEN11_DE_TBT_HOTPLUG_MASK;
3816
3817         val = I915_READ(GEN11_DE_HPD_IMR);
3818         val &= ~hotplug_irqs;
3819         I915_WRITE(GEN11_DE_HPD_IMR, val);
3820         POSTING_READ(GEN11_DE_HPD_IMR);
3821
3822         gen11_hpd_detection_setup(dev_priv);
3823
3824         if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3825                 icp_hpd_irq_setup(dev_priv);
3826 }
3827
3828 static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
3829 {
3830         u32 val, hotplug;
3831
3832         /* Display WA #1179 WaHardHangonHotPlug: cnp */
3833         if (HAS_PCH_CNP(dev_priv)) {
3834                 val = I915_READ(SOUTH_CHICKEN1);
3835                 val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
3836                 val |= CHASSIS_CLK_REQ_DURATION(0xf);
3837                 I915_WRITE(SOUTH_CHICKEN1, val);
3838         }
3839
3840         /* Enable digital hotplug on the PCH */
3841         hotplug = I915_READ(PCH_PORT_HOTPLUG);
3842         hotplug |= PORTA_HOTPLUG_ENABLE |
3843                    PORTB_HOTPLUG_ENABLE |
3844                    PORTC_HOTPLUG_ENABLE |
3845                    PORTD_HOTPLUG_ENABLE;
3846         I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3847
3848         hotplug = I915_READ(PCH_PORT_HOTPLUG2);
3849         hotplug |= PORTE_HOTPLUG_ENABLE;
3850         I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
3851 }
3852
3853 static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
3854 {
3855         u32 hotplug_irqs, enabled_irqs;
3856
3857         hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
3858         enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
3859
3860         ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
3861
3862         spt_hpd_detection_setup(dev_priv);
3863 }
3864
3865 static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
3866 {
3867         u32 hotplug;
3868
3869         /*
3870          * Enable digital hotplug on the CPU, and configure the DP short pulse
3871          * duration to 2ms (which is the minimum in the Display Port spec)
3872          * The pulse duration bits are reserved on HSW+.
3873          */
3874         hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3875         hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3876         hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
3877                    DIGITAL_PORTA_PULSE_DURATION_2ms;
3878         I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3879 }
3880
3881 static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3882 {
3883         u32 hotplug_irqs, enabled_irqs;
3884
3885         if (INTEL_GEN(dev_priv) >= 8) {
3886                 hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
3887                 enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
3888
3889                 bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3890         } else if (INTEL_GEN(dev_priv) >= 7) {
3891                 hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
3892                 enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
3893
3894                 ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
3895         } else {
3896                 hotplug_irqs = DE_DP_A_HOTPLUG;
3897                 enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3898
3899                 ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
3900         }
3901
3902         ilk_hpd_detection_setup(dev_priv);
3903
3904         ibx_hpd_irq_setup(dev_priv);
3905 }
3906
3907 static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
3908                                       u32 enabled_irqs)
3909 {
3910         u32 hotplug;
3911
3912         hotplug = I915_READ(PCH_PORT_HOTPLUG);
3913         hotplug |= PORTA_HOTPLUG_ENABLE |
3914                    PORTB_HOTPLUG_ENABLE |
3915                    PORTC_HOTPLUG_ENABLE;
3916
3917         DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3918                       hotplug, enabled_irqs);
3919         hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3920
3921         /*
3922          * For BXT invert bit has to be set based on AOB design
3923          * for HPD detection logic, update it based on VBT fields.
3924          */
3925         if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3926             intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3927                 hotplug |= BXT_DDIA_HPD_INVERT;
3928         if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3929             intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3930                 hotplug |= BXT_DDIB_HPD_INVERT;
3931         if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3932             intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3933                 hotplug |= BXT_DDIC_HPD_INVERT;
3934
3935         I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3936 }
3937
3938 static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
3939 {
3940         __bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
3941 }
3942
3943 static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
3944 {
3945         u32 hotplug_irqs, enabled_irqs;
3946
3947         enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
3948         hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3949
3950         bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3951
3952         __bxt_hpd_detection_setup(dev_priv, enabled_irqs);
3953 }
3954
3955 static void ibx_irq_postinstall(struct drm_i915_private *dev_priv)
3956 {
3957         u32 mask;
3958
3959         if (HAS_PCH_NOP(dev_priv))
3960                 return;
3961
3962         if (HAS_PCH_IBX(dev_priv))
3963                 mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3964         else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3965                 mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
3966         else
3967                 mask = SDE_GMBUS_CPT;
3968
3969         gen3_assert_iir_is_zero(&dev_priv->uncore, SDEIIR);
3970         I915_WRITE(SDEIMR, ~mask);
3971
3972         if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
3973             HAS_PCH_LPT(dev_priv))
3974                 ibx_hpd_detection_setup(dev_priv);
3975         else
3976                 spt_hpd_detection_setup(dev_priv);
3977 }
3978
3979 static void gen5_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3980 {
3981         struct intel_uncore *uncore = &dev_priv->uncore;
3982         u32 pm_irqs, gt_irqs;
3983
3984         pm_irqs = gt_irqs = 0;
3985
3986         dev_priv->gt_irq_mask = ~0;
3987         if (HAS_L3_DPF(dev_priv)) {
3988                 /* L3 parity interrupt is always unmasked. */
3989                 dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3990                 gt_irqs |= GT_PARITY_ERROR(dev_priv);
3991         }
3992
3993         gt_irqs |= GT_RENDER_USER_INTERRUPT;
3994         if (IS_GEN(dev_priv, 5)) {
3995                 gt_irqs |= ILK_BSD_USER_INTERRUPT;
3996         } else {
3997                 gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
3998         }
3999
4000         GEN3_IRQ_INIT(uncore, GT, dev_priv->gt_irq_mask, gt_irqs);
4001
4002         if (INTEL_GEN(dev_priv) >= 6) {
4003                 /*
4004                  * RPS interrupts will get enabled/disabled on demand when RPS
4005                  * itself is enabled/disabled.
4006                  */
4007                 if (HAS_ENGINE(dev_priv, VECS0)) {
4008                         pm_irqs |= PM_VEBOX_USER_INTERRUPT;
4009                         dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
4010                 }
4011
4012                 dev_priv->pm_imr = 0xffffffff;
4013                 GEN3_IRQ_INIT(uncore, GEN6_PM, dev_priv->pm_imr, pm_irqs);
4014         }
4015 }
4016
4017 static void ironlake_irq_postinstall(struct drm_i915_private *dev_priv)
4018 {
4019         struct intel_uncore *uncore = &dev_priv->uncore;
4020         u32 display_mask, extra_mask;
4021
4022         if (INTEL_GEN(dev_priv) >= 7) {
4023                 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
4024                                 DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
4025                 extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
4026                               DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
4027                               DE_DP_A_HOTPLUG_IVB);
4028         } else {
4029                 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
4030                                 DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
4031                                 DE_PIPEA_CRC_DONE | DE_POISON);
4032                 extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
4033                               DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
4034                               DE_DP_A_HOTPLUG);
4035         }
4036
4037         if (IS_HASWELL(dev_priv)) {
4038                 gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
4039                 intel_psr_irq_control(dev_priv, dev_priv->psr.debug);
4040                 display_mask |= DE_EDP_PSR_INT_HSW;
4041         }
4042
4043         dev_priv->irq_mask = ~display_mask;
4044
4045         ibx_irq_pre_postinstall(dev_priv);
4046
4047         GEN3_IRQ_INIT(uncore, DE, dev_priv->irq_mask,
4048                       display_mask | extra_mask);
4049
4050         gen5_gt_irq_postinstall(dev_priv);
4051
4052         ilk_hpd_detection_setup(dev_priv);
4053
4054         ibx_irq_postinstall(dev_priv);
4055
4056         if (IS_IRONLAKE_M(dev_priv)) {
4057                 /* Enable PCU event interrupts
4058                  *
4059                  * spinlocking not required here for correctness since interrupt
4060                  * setup is guaranteed to run in single-threaded context. But we
4061                  * need it to make the assert_spin_locked happy. */
4062                 spin_lock_irq(&dev_priv->irq_lock);
4063                 ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
4064                 spin_unlock_irq(&dev_priv->irq_lock);
4065         }
4066 }
4067
4068 void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
4069 {
4070         lockdep_assert_held(&dev_priv->irq_lock);
4071
4072         if (dev_priv->display_irqs_enabled)
4073                 return;
4074
4075         dev_priv->display_irqs_enabled = true;
4076
4077         if (intel_irqs_enabled(dev_priv)) {
4078                 vlv_display_irq_reset(dev_priv);
4079                 vlv_display_irq_postinstall(dev_priv);
4080         }
4081 }
4082
4083 void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
4084 {
4085         lockdep_assert_held(&dev_priv->irq_lock);
4086
4087         if (!dev_priv->display_irqs_enabled)
4088                 return;
4089
4090         dev_priv->display_irqs_enabled = false;
4091
4092         if (intel_irqs_enabled(dev_priv))
4093                 vlv_display_irq_reset(dev_priv);
4094 }
4095
4096
4097 static void valleyview_irq_postinstall(struct drm_i915_private *dev_priv)
4098 {
4099         gen5_gt_irq_postinstall(dev_priv);
4100
4101         spin_lock_irq(&dev_priv->irq_lock);
4102         if (dev_priv->display_irqs_enabled)
4103                 vlv_display_irq_postinstall(dev_priv);
4104         spin_unlock_irq(&dev_priv->irq_lock);
4105
4106         I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
4107         POSTING_READ(VLV_MASTER_IER);
4108 }
4109
4110 static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
4111 {
4112         struct intel_uncore *uncore = &dev_priv->uncore;
4113
4114         /* These are interrupts we'll toggle with the ring mask register */
4115         u32 gt_interrupts[] = {
4116                 (GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
4117                  GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
4118                  GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
4119                  GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT),
4120
4121                 (GT_RENDER_USER_INTERRUPT << GEN8_VCS0_IRQ_SHIFT |
4122                  GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS0_IRQ_SHIFT |
4123                  GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
4124                  GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT),
4125
4126                 0,
4127
4128                 (GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
4129                  GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT)
4130         };
4131
4132         dev_priv->pm_ier = 0x0;
4133         dev_priv->pm_imr = ~dev_priv->pm_ier;
4134         GEN8_IRQ_INIT_NDX(uncore, GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
4135         GEN8_IRQ_INIT_NDX(uncore, GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
4136         /*
4137          * RPS interrupts will get enabled/disabled on demand when RPS itself
4138          * is enabled/disabled. Same wil be the case for GuC interrupts.
4139          */
4140         GEN8_IRQ_INIT_NDX(uncore, GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
4141         GEN8_IRQ_INIT_NDX(uncore, GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
4142 }
4143
4144 static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
4145 {
4146         struct intel_uncore *uncore = &dev_priv->uncore;
4147
4148         u32 de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
4149         u32 de_pipe_enables;
4150         u32 de_port_masked = GEN8_AUX_CHANNEL_A;
4151         u32 de_port_enables;
4152         u32 de_misc_masked = GEN8_DE_EDP_PSR;
4153         enum pipe pipe;
4154
4155         if (INTEL_GEN(dev_priv) <= 10)
4156                 de_misc_masked |= GEN8_DE_MISC_GSE;
4157
4158         if (INTEL_GEN(dev_priv) >= 9) {
4159                 de_pipe_masked |= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
4160                 de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
4161                                   GEN9_AUX_CHANNEL_D;
4162                 if (IS_GEN9_LP(dev_priv))
4163                         de_port_masked |= BXT_DE_PORT_GMBUS;
4164         } else {
4165                 de_pipe_masked |= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
4166         }
4167
4168         if (INTEL_GEN(dev_priv) >= 11)
4169                 de_port_masked |= ICL_AUX_CHANNEL_E;
4170
4171         if (IS_CNL_WITH_PORT_F(dev_priv) || INTEL_GEN(dev_priv) >= 11)
4172                 de_port_masked |= CNL_AUX_CHANNEL_F;
4173
4174         de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
4175                                            GEN8_PIPE_FIFO_UNDERRUN;
4176
4177         de_port_enables = de_port_masked;
4178         if (IS_GEN9_LP(dev_priv))
4179                 de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
4180         else if (IS_BROADWELL(dev_priv))
4181                 de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
4182
4183         gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
4184         intel_psr_irq_control(dev_priv, dev_priv->psr.debug);
4185
4186         for_each_pipe(dev_priv, pipe) {
4187                 dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
4188
4189                 if (intel_display_power_is_enabled(dev_priv,
4190                                 POWER_DOMAIN_PIPE(pipe)))
4191                         GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
4192                                           dev_priv->de_irq_mask[pipe],
4193                                           de_pipe_enables);
4194         }
4195
4196         GEN3_IRQ_INIT(uncore, GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
4197         GEN3_IRQ_INIT(uncore, GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
4198
4199         if (INTEL_GEN(dev_priv) >= 11) {
4200                 u32 de_hpd_masked = 0;
4201                 u32 de_hpd_enables = GEN11_DE_TC_HOTPLUG_MASK |
4202                                      GEN11_DE_TBT_HOTPLUG_MASK;
4203
4204                 GEN3_IRQ_INIT(uncore, GEN11_DE_HPD_, ~de_hpd_masked,
4205                               de_hpd_enables);
4206                 gen11_hpd_detection_setup(dev_priv);
4207         } else if (IS_GEN9_LP(dev_priv)) {
4208                 bxt_hpd_detection_setup(dev_priv);
4209         } else if (IS_BROADWELL(dev_priv)) {
4210                 ilk_hpd_detection_setup(dev_priv);
4211         }
4212 }
4213
4214 static void gen8_irq_postinstall(struct drm_i915_private *dev_priv)
4215 {
4216         if (HAS_PCH_SPLIT(dev_priv))
4217                 ibx_irq_pre_postinstall(dev_priv);
4218
4219         gen8_gt_irq_postinstall(dev_priv);
4220         gen8_de_irq_postinstall(dev_priv);
4221
4222         if (HAS_PCH_SPLIT(dev_priv))
4223                 ibx_irq_postinstall(dev_priv);
4224
4225         gen8_master_intr_enable(dev_priv->uncore.regs);
4226 }
4227
4228 static void gen11_gt_irq_postinstall(struct intel_gt *gt)
4229 {
4230         const u32 irqs = GT_RENDER_USER_INTERRUPT | GT_CONTEXT_SWITCH_INTERRUPT;
4231         struct drm_i915_private *dev_priv = gt->i915;
4232         struct intel_uncore *uncore = gt->uncore;
4233         const u32 dmask = irqs << 16 | irqs;
4234         const u32 smask = irqs << 16;
4235
4236         BUILD_BUG_ON(irqs & 0xffff0000);
4237
4238         /* Enable RCS, BCS, VCS and VECS class interrupts. */
4239         intel_uncore_write(uncore, GEN11_RENDER_COPY_INTR_ENABLE, dmask);
4240         intel_uncore_write(uncore, GEN11_VCS_VECS_INTR_ENABLE, dmask);
4241
4242         /* Unmask irqs on RCS, BCS, VCS and VECS engines. */
4243         intel_uncore_write(uncore, GEN11_RCS0_RSVD_INTR_MASK, ~smask);
4244         intel_uncore_write(uncore, GEN11_BCS_RSVD_INTR_MASK, ~smask);
4245         intel_uncore_write(uncore, GEN11_VCS0_VCS1_INTR_MASK, ~dmask);
4246         intel_uncore_write(uncore, GEN11_VCS2_VCS3_INTR_MASK, ~dmask);
4247         intel_uncore_write(uncore, GEN11_VECS0_VECS1_INTR_MASK, ~dmask);
4248
4249         /*
4250          * RPS interrupts will get enabled/disabled on demand when RPS itself
4251          * is enabled/disabled.
4252          */
4253         dev_priv->pm_ier = 0x0;
4254         dev_priv->pm_imr = ~dev_priv->pm_ier;
4255         intel_uncore_write(uncore, GEN11_GPM_WGBOXPERF_INTR_ENABLE, 0);
4256         intel_uncore_write(uncore, GEN11_GPM_WGBOXPERF_INTR_MASK,  ~0);
4257
4258         /* Same thing for GuC interrupts */
4259         intel_uncore_write(uncore, GEN11_GUC_SG_INTR_ENABLE, 0);
4260         intel_uncore_write(uncore, GEN11_GUC_SG_INTR_MASK,  ~0);
4261 }
4262
4263 static void icp_irq_postinstall(struct drm_i915_private *dev_priv)
4264 {
4265         u32 mask = SDE_GMBUS_ICP;
4266
4267         WARN_ON(I915_READ(SDEIER) != 0);
4268         I915_WRITE(SDEIER, 0xffffffff);
4269         POSTING_READ(SDEIER);
4270
4271         gen3_assert_iir_is_zero(&dev_priv->uncore, SDEIIR);
4272         I915_WRITE(SDEIMR, ~mask);
4273
4274         icp_hpd_detection_setup(dev_priv);
4275 }
4276
4277 static void gen11_irq_postinstall(struct drm_i915_private *dev_priv)
4278 {
4279         struct intel_uncore *uncore = &dev_priv->uncore;
4280         u32 gu_misc_masked = GEN11_GU_MISC_GSE;
4281
4282         if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
4283                 icp_irq_postinstall(dev_priv);
4284
4285         gen11_gt_irq_postinstall(&dev_priv->gt);
4286         gen8_de_irq_postinstall(dev_priv);
4287
4288         GEN3_IRQ_INIT(uncore, GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
4289
4290         I915_WRITE(GEN11_DISPLAY_INT_CTL, GEN11_DISPLAY_IRQ_ENABLE);
4291
4292         gen11_master_intr_enable(uncore->regs);
4293         POSTING_READ(GEN11_GFX_MSTR_IRQ);
4294 }
4295
4296 static void cherryview_irq_postinstall(struct drm_i915_private *dev_priv)
4297 {
4298         gen8_gt_irq_postinstall(dev_priv);
4299
4300         spin_lock_irq(&dev_priv->irq_lock);
4301         if (dev_priv->display_irqs_enabled)
4302                 vlv_display_irq_postinstall(dev_priv);
4303         spin_unlock_irq(&dev_priv->irq_lock);
4304
4305         I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
4306         POSTING_READ(GEN8_MASTER_IRQ);
4307 }
4308
4309 static void i8xx_irq_reset(struct drm_i915_private *dev_priv)
4310 {
4311         struct intel_uncore *uncore = &dev_priv->uncore;
4312
4313         i9xx_pipestat_irq_reset(dev_priv);
4314
4315         GEN2_IRQ_RESET(uncore);
4316 }
4317
4318 static void i8xx_irq_postinstall(struct drm_i915_private *dev_priv)
4319 {
4320         struct intel_uncore *uncore = &dev_priv->uncore;
4321         u16 enable_mask;
4322
4323         intel_uncore_write16(uncore,
4324                              EMR,
4325                              ~(I915_ERROR_PAGE_TABLE |
4326                                I915_ERROR_MEMORY_REFRESH));
4327
4328         /* Unmask the interrupts that we always want on. */
4329         dev_priv->irq_mask =
4330                 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4331                   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4332                   I915_MASTER_ERROR_INTERRUPT);
4333
4334         enable_mask =
4335                 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4336                 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4337                 I915_MASTER_ERROR_INTERRUPT |
4338                 I915_USER_INTERRUPT;
4339
4340         GEN2_IRQ_INIT(uncore, dev_priv->irq_mask, enable_mask);
4341
4342         /* Interrupt setup is already guaranteed to be single-threaded, this is
4343          * just to make the assert_spin_locked check happy. */
4344         spin_lock_irq(&dev_priv->irq_lock);
4345         i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4346         i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4347         spin_unlock_irq(&dev_priv->irq_lock);
4348 }
4349
4350 static void i8xx_error_irq_ack(struct drm_i915_private *i915,
4351                                u16 *eir, u16 *eir_stuck)
4352 {
4353         struct intel_uncore *uncore = &i915->uncore;
4354         u16 emr;
4355
4356         *eir = intel_uncore_read16(uncore, EIR);
4357
4358         if (*eir)
4359                 intel_uncore_write16(uncore, EIR, *eir);
4360
4361         *eir_stuck = intel_uncore_read16(uncore, EIR);
4362         if (*eir_stuck == 0)
4363                 return;
4364
4365         /*
4366          * Toggle all EMR bits to make sure we get an edge
4367          * in the ISR master error bit if we don't clear
4368          * all the EIR bits. Otherwise the edge triggered
4369          * IIR on i965/g4x wouldn't notice that an interrupt
4370          * is still pending. Also some EIR bits can't be
4371          * cleared except by handling the underlying error
4372          * (or by a GPU reset) so we mask any bit that
4373          * remains set.
4374          */
4375         emr = intel_uncore_read16(uncore, EMR);
4376         intel_uncore_write16(uncore, EMR, 0xffff);
4377         intel_uncore_write16(uncore, EMR, emr | *eir_stuck);
4378 }
4379
4380 static void i8xx_error_irq_handler(struct drm_i915_private *dev_priv,
4381                                    u16 eir, u16 eir_stuck)
4382 {
4383         DRM_DEBUG("Master Error: EIR 0x%04x\n", eir);
4384
4385         if (eir_stuck)
4386                 DRM_DEBUG_DRIVER("EIR stuck: 0x%04x, masked\n", eir_stuck);
4387 }
4388
4389 static void i9xx_error_irq_ack(struct drm_i915_private *dev_priv,
4390                                u32 *eir, u32 *eir_stuck)
4391 {
4392         u32 emr;
4393
4394         *eir = I915_READ(EIR);
4395
4396         I915_WRITE(EIR, *eir);
4397
4398         *eir_stuck = I915_READ(EIR);
4399         if (*eir_stuck == 0)
4400                 return;
4401
4402         /*
4403          * Toggle all EMR bits to make sure we get an edge
4404          * in the ISR master error bit if we don't clear
4405          * all the EIR bits. Otherwise the edge triggered
4406          * IIR on i965/g4x wouldn't notice that an interrupt
4407          * is still pending. Also some EIR bits can't be
4408          * cleared except by handling the underlying error
4409          * (or by a GPU reset) so we mask any bit that
4410          * remains set.
4411          */
4412         emr = I915_READ(EMR);
4413         I915_WRITE(EMR, 0xffffffff);
4414         I915_WRITE(EMR, emr | *eir_stuck);
4415 }
4416
4417 static void i9xx_error_irq_handler(struct drm_i915_private *dev_priv,
4418                                    u32 eir, u32 eir_stuck)
4419 {
4420         DRM_DEBUG("Master Error, EIR 0x%08x\n", eir);
4421
4422         if (eir_stuck)
4423                 DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masked\n", eir_stuck);
4424 }
4425
4426 static irqreturn_t i8xx_irq_handler(int irq, void *arg)
4427 {
4428         struct drm_i915_private *dev_priv = arg;
4429         irqreturn_t ret = IRQ_NONE;
4430
4431         if (!intel_irqs_enabled(dev_priv))
4432                 return IRQ_NONE;
4433
4434         /* IRQs are synced during runtime_suspend, we don't require a wakeref */
4435         disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
4436
4437         do {
4438                 u32 pipe_stats[I915_MAX_PIPES] = {};
4439                 u16 eir = 0, eir_stuck = 0;
4440                 u16 iir;
4441
4442                 iir = intel_uncore_read16(&dev_priv->uncore, GEN2_IIR);
4443                 if (iir == 0)
4444                         break;
4445
4446                 ret = IRQ_HANDLED;
4447
4448                 /* Call regardless, as some status bits might not be
4449                  * signalled in iir */
4450                 i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4451
4452                 if (iir & I915_MASTER_ERROR_INTERRUPT)
4453                         i8xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
4454
4455                 intel_uncore_write16(&dev_priv->uncore, GEN2_IIR, iir);
4456
4457                 if (iir & I915_USER_INTERRUPT)
4458                         intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4459
4460                 if (iir & I915_MASTER_ERROR_INTERRUPT)
4461                         i8xx_error_irq_handler(dev_priv, eir, eir_stuck);
4462
4463                 i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4464         } while (0);
4465
4466         enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
4467
4468         return ret;
4469 }
4470
4471 static void i915_irq_reset(struct drm_i915_private *dev_priv)
4472 {
4473         struct intel_uncore *uncore = &dev_priv->uncore;
4474
4475         if (I915_HAS_HOTPLUG(dev_priv)) {
4476                 i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4477                 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4478         }
4479
4480         i9xx_pipestat_irq_reset(dev_priv);
4481
4482         GEN3_IRQ_RESET(uncore, GEN2_);
4483 }
4484
4485 static void i915_irq_postinstall(struct drm_i915_private *dev_priv)
4486 {
4487         struct intel_uncore *uncore = &dev_priv->uncore;
4488         u32 enable_mask;
4489
4490         I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE |
4491                           I915_ERROR_MEMORY_REFRESH));
4492
4493         /* Unmask the interrupts that we always want on. */
4494         dev_priv->irq_mask =
4495                 ~(I915_ASLE_INTERRUPT |
4496                   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4497                   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4498                   I915_MASTER_ERROR_INTERRUPT);
4499
4500         enable_mask =
4501                 I915_ASLE_INTERRUPT |
4502                 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4503                 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4504                 I915_MASTER_ERROR_INTERRUPT |
4505                 I915_USER_INTERRUPT;
4506
4507         if (I915_HAS_HOTPLUG(dev_priv)) {
4508                 /* Enable in IER... */
4509                 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4510                 /* and unmask in IMR */
4511                 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4512         }
4513
4514         GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
4515
4516         /* Interrupt setup is already guaranteed to be single-threaded, this is
4517          * just to make the assert_spin_locked check happy. */
4518         spin_lock_irq(&dev_priv->irq_lock);
4519         i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4520         i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4521         spin_unlock_irq(&dev_priv->irq_lock);
4522
4523         i915_enable_asle_pipestat(dev_priv);
4524 }
4525
4526 static irqreturn_t i915_irq_handler(int irq, void *arg)
4527 {
4528         struct drm_i915_private *dev_priv = arg;
4529         irqreturn_t ret = IRQ_NONE;
4530
4531         if (!intel_irqs_enabled(dev_priv))
4532                 return IRQ_NONE;
4533
4534         /* IRQs are synced during runtime_suspend, we don't require a wakeref */
4535         disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
4536
4537         do {
4538                 u32 pipe_stats[I915_MAX_PIPES] = {};
4539                 u32 eir = 0, eir_stuck = 0;
4540                 u32 hotplug_status = 0;
4541                 u32 iir;
4542
4543                 iir = I915_READ(GEN2_IIR);
4544                 if (iir == 0)
4545                         break;
4546
4547                 ret = IRQ_HANDLED;
4548
4549                 if (I915_HAS_HOTPLUG(dev_priv) &&
4550                     iir & I915_DISPLAY_PORT_INTERRUPT)
4551                         hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4552
4553                 /* Call regardless, as some status bits might not be
4554                  * signalled in iir */
4555                 i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4556
4557                 if (iir & I915_MASTER_ERROR_INTERRUPT)
4558                         i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
4559
4560                 I915_WRITE(GEN2_IIR, iir);
4561
4562                 if (iir & I915_USER_INTERRUPT)
4563                         intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4564
4565                 if (iir & I915_MASTER_ERROR_INTERRUPT)
4566                         i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4567
4568                 if (hotplug_status)
4569                         i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4570
4571                 i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4572         } while (0);
4573
4574         enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
4575
4576         return ret;
4577 }
4578
4579 static void i965_irq_reset(struct drm_i915_private *dev_priv)
4580 {
4581         struct intel_uncore *uncore = &dev_priv->uncore;
4582
4583         i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4584         I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4585
4586         i9xx_pipestat_irq_reset(dev_priv);
4587
4588         GEN3_IRQ_RESET(uncore, GEN2_);
4589 }
4590
4591 static void i965_irq_postinstall(struct drm_i915_private *dev_priv)
4592 {
4593         struct intel_uncore *uncore = &dev_priv->uncore;
4594         u32 enable_mask;
4595         u32 error_mask;
4596
4597         /*
4598          * Enable some error detection, note the instruction error mask
4599          * bit is reserved, so we leave it masked.
4600          */
4601         if (IS_G4X(dev_priv)) {
4602                 error_mask = ~(GM45_ERROR_PAGE_TABLE |
4603                                GM45_ERROR_MEM_PRIV |
4604                                GM45_ERROR_CP_PRIV |
4605                                I915_ERROR_MEMORY_REFRESH);
4606         } else {
4607                 error_mask = ~(I915_ERROR_PAGE_TABLE |
4608                                I915_ERROR_MEMORY_REFRESH);
4609         }
4610         I915_WRITE(EMR, error_mask);
4611
4612         /* Unmask the interrupts that we always want on. */
4613         dev_priv->irq_mask =
4614                 ~(I915_ASLE_INTERRUPT |
4615                   I915_DISPLAY_PORT_INTERRUPT |
4616                   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4617                   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4618                   I915_MASTER_ERROR_INTERRUPT);
4619
4620         enable_mask =
4621                 I915_ASLE_INTERRUPT |
4622                 I915_DISPLAY_PORT_INTERRUPT |
4623                 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4624                 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4625                 I915_MASTER_ERROR_INTERRUPT |
4626                 I915_USER_INTERRUPT;
4627
4628         if (IS_G4X(dev_priv))
4629                 enable_mask |= I915_BSD_USER_INTERRUPT;
4630
4631         GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
4632
4633         /* Interrupt setup is already guaranteed to be single-threaded, this is
4634          * just to make the assert_spin_locked check happy. */
4635         spin_lock_irq(&dev_priv->irq_lock);
4636         i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4637         i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4638         i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4639         spin_unlock_irq(&dev_priv->irq_lock);
4640
4641         i915_enable_asle_pipestat(dev_priv);
4642 }
4643
4644 static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
4645 {
4646         u32 hotplug_en;
4647
4648         lockdep_assert_held(&dev_priv->irq_lock);
4649
4650         /* Note HDMI and DP share hotplug bits */
4651         /* enable bits are the same for all generations */
4652         hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4653         /* Programming the CRT detection parameters tends
4654            to generate a spurious hotplug event about three
4655            seconds later.  So just do it once.
4656         */
4657         if (IS_G4X(dev_priv))
4658                 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4659         hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4660
4661         /* Ignore TV since it's buggy */
4662         i915_hotplug_interrupt_update_locked(dev_priv,
4663                                              HOTPLUG_INT_EN_MASK |
4664                                              CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4665                                              CRT_HOTPLUG_ACTIVATION_PERIOD_64,
4666                                              hotplug_en);
4667 }
4668
4669 static irqreturn_t i965_irq_handler(int irq, void *arg)
4670 {
4671         struct drm_i915_private *dev_priv = arg;
4672         irqreturn_t ret = IRQ_NONE;
4673
4674         if (!intel_irqs_enabled(dev_priv))
4675                 return IRQ_NONE;
4676
4677         /* IRQs are synced during runtime_suspend, we don't require a wakeref */
4678         disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
4679
4680         do {
4681                 u32 pipe_stats[I915_MAX_PIPES] = {};
4682                 u32 eir = 0, eir_stuck = 0;
4683                 u32 hotplug_status = 0;
4684                 u32 iir;
4685
4686                 iir = I915_READ(GEN2_IIR);
4687                 if (iir == 0)
4688                         break;
4689
4690                 ret = IRQ_HANDLED;
4691
4692                 if (iir & I915_DISPLAY_PORT_INTERRUPT)
4693                         hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4694
4695                 /* Call regardless, as some status bits might not be
4696                  * signalled in iir */
4697                 i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4698
4699                 if (iir & I915_MASTER_ERROR_INTERRUPT)
4700                         i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
4701
4702                 I915_WRITE(GEN2_IIR, iir);
4703
4704                 if (iir & I915_USER_INTERRUPT)
4705                         intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4706
4707                 if (iir & I915_BSD_USER_INTERRUPT)
4708                         intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
4709
4710                 if (iir & I915_MASTER_ERROR_INTERRUPT)
4711                         i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4712
4713                 if (hotplug_status)
4714                         i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4715
4716                 i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4717         } while (0);
4718
4719         enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
4720
4721         return ret;
4722 }
4723
4724 /**
4725  * intel_irq_init - initializes irq support
4726  * @dev_priv: i915 device instance
4727  *
4728  * This function initializes all the irq support including work items, timers
4729  * and all the vtables. It does not setup the interrupt itself though.
4730  */
4731 void intel_irq_init(struct drm_i915_private *dev_priv)
4732 {
4733         struct drm_device *dev = &dev_priv->drm;
4734         struct intel_rps *rps = &dev_priv->gt_pm.rps;
4735         int i;
4736
4737         if (IS_I945GM(dev_priv))
4738                 i945gm_vblank_work_init(dev_priv);
4739
4740         intel_hpd_init_work(dev_priv);
4741
4742         INIT_WORK(&rps->work, gen6_pm_rps_work);
4743
4744         INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
4745         for (i = 0; i < MAX_L3_SLICES; ++i)
4746                 dev_priv->l3_parity.remap_info[i] = NULL;
4747
4748         if (HAS_GUC_SCHED(dev_priv) && INTEL_GEN(dev_priv) < 11)
4749                 dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
4750
4751         /* Let's track the enabled rps events */
4752         if (IS_VALLEYVIEW(dev_priv))
4753                 /* WaGsvRC0ResidencyMethod:vlv */
4754                 dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
4755         else
4756                 dev_priv->pm_rps_events = (GEN6_PM_RP_UP_THRESHOLD |
4757                                            GEN6_PM_RP_DOWN_THRESHOLD |
4758                                            GEN6_PM_RP_DOWN_TIMEOUT);
4759
4760         /* We share the register with other engine */
4761         if (INTEL_GEN(dev_priv) > 9)
4762                 GEM_WARN_ON(dev_priv->pm_rps_events & 0xffff0000);
4763
4764         rps->pm_intrmsk_mbz = 0;
4765
4766         /*
4767          * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
4768          * if GEN6_PM_UP_EI_EXPIRED is masked.
4769          *
4770          * TODO: verify if this can be reproduced on VLV,CHV.
4771          */
4772         if (INTEL_GEN(dev_priv) <= 7)
4773                 rps->pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
4774
4775         if (INTEL_GEN(dev_priv) >= 8)
4776                 rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
4777
4778         dev->vblank_disable_immediate = true;
4779
4780         /* Most platforms treat the display irq block as an always-on
4781          * power domain. vlv/chv can disable it at runtime and need
4782          * special care to avoid writing any of the display block registers
4783          * outside of the power domain. We defer setting up the display irqs
4784          * in this case to the runtime pm.
4785          */
4786         dev_priv->display_irqs_enabled = true;
4787         if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4788                 dev_priv->display_irqs_enabled = false;
4789
4790         dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
4791         /* If we have MST support, we want to avoid doing short HPD IRQ storm
4792          * detection, as short HPD storms will occur as a natural part of
4793          * sideband messaging with MST.
4794          * On older platforms however, IRQ storms can occur with both long and
4795          * short pulses, as seen on some G4x systems.
4796          */
4797         dev_priv->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(dev_priv);
4798
4799         if (HAS_GMCH(dev_priv)) {
4800                 if (I915_HAS_HOTPLUG(dev_priv))
4801                         dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4802         } else {
4803                 if (INTEL_GEN(dev_priv) >= 11)
4804                         dev_priv->display.hpd_irq_setup = gen11_hpd_irq_setup;
4805                 else if (IS_GEN9_LP(dev_priv))
4806                         dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
4807                 else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
4808                         dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
4809                 else
4810                         dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4811         }
4812 }
4813
4814 /**
4815  * intel_irq_fini - deinitializes IRQ support
4816  * @i915: i915 device instance
4817  *
4818  * This function deinitializes all the IRQ support.
4819  */
4820 void intel_irq_fini(struct drm_i915_private *i915)
4821 {
4822         int i;
4823
4824         if (IS_I945GM(i915))
4825                 i945gm_vblank_work_fini(i915);
4826
4827         for (i = 0; i < MAX_L3_SLICES; ++i)
4828                 kfree(i915->l3_parity.remap_info[i]);
4829 }
4830
4831 static irq_handler_t intel_irq_handler(struct drm_i915_private *dev_priv)
4832 {
4833         if (HAS_GMCH(dev_priv)) {
4834                 if (IS_CHERRYVIEW(dev_priv))
4835                         return cherryview_irq_handler;
4836                 else if (IS_VALLEYVIEW(dev_priv))
4837                         return valleyview_irq_handler;
4838                 else if (IS_GEN(dev_priv, 4))
4839                         return i965_irq_handler;
4840                 else if (IS_GEN(dev_priv, 3))
4841                         return i915_irq_handler;
4842                 else
4843                         return i8xx_irq_handler;
4844         } else {
4845                 if (INTEL_GEN(dev_priv) >= 11)
4846                         return gen11_irq_handler;
4847                 else if (INTEL_GEN(dev_priv) >= 8)
4848                         return gen8_irq_handler;
4849                 else
4850                         return ironlake_irq_handler;
4851         }
4852 }
4853
4854 static void intel_irq_reset(struct drm_i915_private *dev_priv)
4855 {
4856         if (HAS_GMCH(dev_priv)) {
4857                 if (IS_CHERRYVIEW(dev_priv))
4858                         cherryview_irq_reset(dev_priv);
4859                 else if (IS_VALLEYVIEW(dev_priv))
4860                         valleyview_irq_reset(dev_priv);
4861                 else if (IS_GEN(dev_priv, 4))
4862                         i965_irq_reset(dev_priv);
4863                 else if (IS_GEN(dev_priv, 3))
4864                         i915_irq_reset(dev_priv);
4865                 else
4866                         i8xx_irq_reset(dev_priv);
4867         } else {
4868                 if (INTEL_GEN(dev_priv) >= 11)
4869                         gen11_irq_reset(dev_priv);
4870                 else if (INTEL_GEN(dev_priv) >= 8)
4871                         gen8_irq_reset(dev_priv);
4872                 else
4873                         ironlake_irq_reset(dev_priv);
4874         }
4875 }
4876
4877 static void intel_irq_postinstall(struct drm_i915_private *dev_priv)
4878 {
4879         if (HAS_GMCH(dev_priv)) {
4880                 if (IS_CHERRYVIEW(dev_priv))
4881                         cherryview_irq_postinstall(dev_priv);
4882                 else if (IS_VALLEYVIEW(dev_priv))
4883                         valleyview_irq_postinstall(dev_priv);
4884                 else if (IS_GEN(dev_priv, 4))
4885                         i965_irq_postinstall(dev_priv);
4886                 else if (IS_GEN(dev_priv, 3))
4887                         i915_irq_postinstall(dev_priv);
4888                 else
4889                         i8xx_irq_postinstall(dev_priv);
4890         } else {
4891                 if (INTEL_GEN(dev_priv) >= 11)
4892                         gen11_irq_postinstall(dev_priv);
4893                 else if (INTEL_GEN(dev_priv) >= 8)
4894                         gen8_irq_postinstall(dev_priv);
4895                 else
4896                         ironlake_irq_postinstall(dev_priv);
4897         }
4898 }
4899
4900 /**
4901  * intel_irq_install - enables the hardware interrupt
4902  * @dev_priv: i915 device instance
4903  *
4904  * This function enables the hardware interrupt handling, but leaves the hotplug
4905  * handling still disabled. It is called after intel_irq_init().
4906  *
4907  * In the driver load and resume code we need working interrupts in a few places
4908  * but don't want to deal with the hassle of concurrent probe and hotplug
4909  * workers. Hence the split into this two-stage approach.
4910  */
4911 int intel_irq_install(struct drm_i915_private *dev_priv)
4912 {
4913         int irq = dev_priv->drm.pdev->irq;
4914         int ret;
4915
4916         /*
4917          * We enable some interrupt sources in our postinstall hooks, so mark
4918          * interrupts as enabled _before_ actually enabling them to avoid
4919          * special cases in our ordering checks.
4920          */
4921         dev_priv->runtime_pm.irqs_enabled = true;
4922
4923         dev_priv->drm.irq_enabled = true;
4924
4925         intel_irq_reset(dev_priv);
4926
4927         ret = request_irq(irq, intel_irq_handler(dev_priv),
4928                           IRQF_SHARED, DRIVER_NAME, dev_priv);
4929         if (ret < 0) {
4930                 dev_priv->drm.irq_enabled = false;
4931                 return ret;
4932         }
4933
4934         intel_irq_postinstall(dev_priv);
4935
4936         return ret;
4937 }
4938
4939 /**
4940  * intel_irq_uninstall - finilizes all irq handling
4941  * @dev_priv: i915 device instance
4942  *
4943  * This stops interrupt and hotplug handling and unregisters and frees all
4944  * resources acquired in the init functions.
4945  */
4946 void intel_irq_uninstall(struct drm_i915_private *dev_priv)
4947 {
4948         int irq = dev_priv->drm.pdev->irq;
4949
4950         /*
4951          * FIXME we can get called twice during driver load
4952          * error handling due to intel_modeset_cleanup()
4953          * calling us out of sequence. Would be nice if
4954          * it didn't do that...
4955          */
4956         if (!dev_priv->drm.irq_enabled)
4957                 return;
4958
4959         dev_priv->drm.irq_enabled = false;
4960
4961         intel_irq_reset(dev_priv);
4962
4963         free_irq(irq, dev_priv);
4964
4965         intel_hpd_cancel_work(dev_priv);
4966         dev_priv->runtime_pm.irqs_enabled = false;
4967 }
4968
4969 /**
4970  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4971  * @dev_priv: i915 device instance
4972  *
4973  * This function is used to disable interrupts at runtime, both in the runtime
4974  * pm and the system suspend/resume code.
4975  */
4976 void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4977 {
4978         intel_irq_reset(dev_priv);
4979         dev_priv->runtime_pm.irqs_enabled = false;
4980         intel_synchronize_irq(dev_priv);
4981 }
4982
4983 /**
4984  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4985  * @dev_priv: i915 device instance
4986  *
4987  * This function is used to enable interrupts at runtime, both in the runtime
4988  * pm and the system suspend/resume code.
4989  */
4990 void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4991 {
4992         dev_priv->runtime_pm.irqs_enabled = true;
4993         intel_irq_reset(dev_priv);
4994         intel_irq_postinstall(dev_priv);
4995 }