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