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