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