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