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