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