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