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