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