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