blob: 729e9a329f76f398145ef9849a93b268d99c6df2 [file] [log] [blame]
Dave Airlie0d6aa602006-01-02 20:14:23 +11001/* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10006 *
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 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Joe Perchesa70491c2012-03-18 13:00:11 -070029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Jesse Barnes63eeaf32009-06-18 16:56:52 -070031#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Damien Lespiaub2c88f52013-10-15 18:55:29 +010033#include <linux/circ_buf.h>
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drmP.h>
35#include <drm/i915_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010037#include "i915_trace.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Daniel Vetterfca52a52014-09-30 10:56:45 +020040/**
41 * DOC: interrupt handling
42 *
43 * These functions provide the basic support for enabling and disabling the
44 * interrupt handling support. There's a lot more functionality in i915_irq.c
45 * and related files, but that will be described in separate chapters.
46 */
47
Egbert Eiche5868a32013-02-28 04:17:12 -050048static const u32 hpd_ibx[] = {
49 [HPD_CRT] = SDE_CRT_HOTPLUG,
50 [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
51 [HPD_PORT_B] = SDE_PORTB_HOTPLUG,
52 [HPD_PORT_C] = SDE_PORTC_HOTPLUG,
53 [HPD_PORT_D] = SDE_PORTD_HOTPLUG
54};
55
56static const u32 hpd_cpt[] = {
57 [HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
Daniel Vetter73c352a2013-03-26 22:38:43 +010058 [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
Egbert Eiche5868a32013-02-28 04:17:12 -050059 [HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
60 [HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
61 [HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
62};
63
64static const u32 hpd_mask_i915[] = {
65 [HPD_CRT] = CRT_HOTPLUG_INT_EN,
66 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
67 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
68 [HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
69 [HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
70 [HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
71};
72
Daniel Vetter704cfb82013-12-18 09:08:43 +010073static const u32 hpd_status_g4x[] = {
Egbert Eiche5868a32013-02-28 04:17:12 -050074 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
76 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
77 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80};
81
Egbert Eiche5868a32013-02-28 04:17:12 -050082static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
83 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
84 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
85 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
86 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
87 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
88 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
89};
90
Paulo Zanoni5c502442014-04-01 15:37:11 -030091/* IIR can theoretically queue up two events. Be paranoid. */
Paulo Zanonif86f3fb2014-04-01 15:37:14 -030092#define GEN8_IRQ_RESET_NDX(type, which) do { \
Paulo Zanoni5c502442014-04-01 15:37:11 -030093 I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
94 POSTING_READ(GEN8_##type##_IMR(which)); \
95 I915_WRITE(GEN8_##type##_IER(which), 0); \
96 I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
97 POSTING_READ(GEN8_##type##_IIR(which)); \
98 I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
99 POSTING_READ(GEN8_##type##_IIR(which)); \
100} while (0)
101
Paulo Zanonif86f3fb2014-04-01 15:37:14 -0300102#define GEN5_IRQ_RESET(type) do { \
Paulo Zanonia9d356a2014-04-01 15:37:09 -0300103 I915_WRITE(type##IMR, 0xffffffff); \
Paulo Zanoni5c502442014-04-01 15:37:11 -0300104 POSTING_READ(type##IMR); \
Paulo Zanonia9d356a2014-04-01 15:37:09 -0300105 I915_WRITE(type##IER, 0); \
Paulo Zanoni5c502442014-04-01 15:37:11 -0300106 I915_WRITE(type##IIR, 0xffffffff); \
107 POSTING_READ(type##IIR); \
108 I915_WRITE(type##IIR, 0xffffffff); \
109 POSTING_READ(type##IIR); \
Paulo Zanonia9d356a2014-04-01 15:37:09 -0300110} while (0)
111
Paulo Zanoni337ba012014-04-01 15:37:16 -0300112/*
113 * We should clear IMR at preinstall/uninstall, and just check at postinstall.
114 */
115#define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
116 u32 val = I915_READ(reg); \
117 if (val) { \
118 WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
119 (reg), val); \
120 I915_WRITE((reg), 0xffffffff); \
121 POSTING_READ(reg); \
122 I915_WRITE((reg), 0xffffffff); \
123 POSTING_READ(reg); \
124 } \
125} while (0)
126
Paulo Zanoni35079892014-04-01 15:37:15 -0300127#define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
Paulo Zanoni337ba012014-04-01 15:37:16 -0300128 GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
Paulo Zanoni35079892014-04-01 15:37:15 -0300129 I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
Ville Syrjälä7d1bd5392014-10-30 19:42:50 +0200130 I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
131 POSTING_READ(GEN8_##type##_IMR(which)); \
Paulo Zanoni35079892014-04-01 15:37:15 -0300132} while (0)
133
134#define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
Paulo Zanoni337ba012014-04-01 15:37:16 -0300135 GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
Paulo Zanoni35079892014-04-01 15:37:15 -0300136 I915_WRITE(type##IER, (ier_val)); \
Ville Syrjälä7d1bd5392014-10-30 19:42:50 +0200137 I915_WRITE(type##IMR, (imr_val)); \
138 POSTING_READ(type##IMR); \
Paulo Zanoni35079892014-04-01 15:37:15 -0300139} while (0)
140
Imre Deakc9a9a262014-11-05 20:48:37 +0200141static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
142
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800143/* For display hotplug interrupt */
Daniel Vetter47339cd2014-09-30 10:56:46 +0200144void
Jani Nikula2d1013d2014-03-31 14:27:17 +0300145ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800146{
Daniel Vetter4bc9d432013-06-27 13:44:58 +0200147 assert_spin_locked(&dev_priv->irq_lock);
148
Jesse Barnes9df7575f2014-06-20 09:29:20 -0700149 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300150 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300151
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000152 if ((dev_priv->irq_mask & mask) != 0) {
153 dev_priv->irq_mask &= ~mask;
154 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000155 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800156 }
157}
158
Daniel Vetter47339cd2014-09-30 10:56:46 +0200159void
Jani Nikula2d1013d2014-03-31 14:27:17 +0300160ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800161{
Daniel Vetter4bc9d432013-06-27 13:44:58 +0200162 assert_spin_locked(&dev_priv->irq_lock);
163
Paulo Zanoni06ffc772014-07-17 17:43:46 -0300164 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300165 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300166
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000167 if ((dev_priv->irq_mask & mask) != mask) {
168 dev_priv->irq_mask |= mask;
169 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000170 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800171 }
172}
173
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300174/**
175 * ilk_update_gt_irq - update GTIMR
176 * @dev_priv: driver private
177 * @interrupt_mask: mask of interrupt bits to update
178 * @enabled_irq_mask: mask of interrupt bits to enable
179 */
180static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
181 uint32_t interrupt_mask,
182 uint32_t enabled_irq_mask)
183{
184 assert_spin_locked(&dev_priv->irq_lock);
185
Jesse Barnes9df7575f2014-06-20 09:29:20 -0700186 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300187 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300188
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300189 dev_priv->gt_irq_mask &= ~interrupt_mask;
190 dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
191 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
192 POSTING_READ(GTIMR);
193}
194
Daniel Vetter480c8032014-07-16 09:49:40 +0200195void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300196{
197 ilk_update_gt_irq(dev_priv, mask, mask);
198}
199
Daniel Vetter480c8032014-07-16 09:49:40 +0200200void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300201{
202 ilk_update_gt_irq(dev_priv, mask, 0);
203}
204
Imre Deakb900b942014-11-05 20:48:48 +0200205static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
206{
207 return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
208}
209
Imre Deaka72fbc32014-11-05 20:48:31 +0200210static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
211{
212 return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
213}
214
Imre Deakb900b942014-11-05 20:48:48 +0200215static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
216{
217 return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
218}
219
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300220/**
221 * snb_update_pm_irq - update GEN6_PMIMR
222 * @dev_priv: driver private
223 * @interrupt_mask: mask of interrupt bits to update
224 * @enabled_irq_mask: mask of interrupt bits to enable
225 */
226static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
227 uint32_t interrupt_mask,
228 uint32_t enabled_irq_mask)
229{
Paulo Zanoni605cd252013-08-06 18:57:15 -0300230 uint32_t new_val;
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300231
232 assert_spin_locked(&dev_priv->irq_lock);
233
Jesse Barnes9df7575f2014-06-20 09:29:20 -0700234 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300235 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300236
Paulo Zanoni605cd252013-08-06 18:57:15 -0300237 new_val = dev_priv->pm_irq_mask;
Paulo Zanonif52ecbc2013-08-06 18:57:14 -0300238 new_val &= ~interrupt_mask;
239 new_val |= (~enabled_irq_mask & interrupt_mask);
240
Paulo Zanoni605cd252013-08-06 18:57:15 -0300241 if (new_val != dev_priv->pm_irq_mask) {
242 dev_priv->pm_irq_mask = new_val;
Imre Deaka72fbc32014-11-05 20:48:31 +0200243 I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
244 POSTING_READ(gen6_pm_imr(dev_priv));
Paulo Zanonif52ecbc2013-08-06 18:57:14 -0300245 }
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300246}
247
Daniel Vetter480c8032014-07-16 09:49:40 +0200248void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300249{
250 snb_update_pm_irq(dev_priv, mask, mask);
251}
252
Daniel Vetter480c8032014-07-16 09:49:40 +0200253void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300254{
255 snb_update_pm_irq(dev_priv, mask, 0);
256}
257
Imre Deakb900b942014-11-05 20:48:48 +0200258void gen6_enable_rps_interrupts(struct drm_device *dev)
259{
260 struct drm_i915_private *dev_priv = dev->dev_private;
261
262 spin_lock_irq(&dev_priv->irq_lock);
263 WARN_ON(dev_priv->rps.pm_iir);
264 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
265 I915_WRITE(gen6_pm_iir(dev_priv), dev_priv->pm_rps_events);
266 spin_unlock_irq(&dev_priv->irq_lock);
267}
268
269void gen6_disable_rps_interrupts(struct drm_device *dev)
270{
271 struct drm_i915_private *dev_priv = dev->dev_private;
272
273 I915_WRITE(GEN6_PMINTRMSK, INTEL_INFO(dev_priv)->gen >= 8 ?
274 ~GEN8_PMINTR_REDIRECT_TO_NON_DISP : ~0);
275 I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
276 ~dev_priv->pm_rps_events);
277 /* Complete PM interrupt masking here doesn't race with the rps work
278 * item again unmasking PM interrupts because that is using a different
279 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
280 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
281
282 spin_lock_irq(&dev_priv->irq_lock);
283 dev_priv->rps.pm_iir = 0;
284 spin_unlock_irq(&dev_priv->irq_lock);
285
286 I915_WRITE(gen6_pm_iir(dev_priv), dev_priv->pm_rps_events);
287}
288
Ben Widawsky09610212014-05-15 20:58:08 +0300289/**
Daniel Vetterfee884e2013-07-04 23:35:21 +0200290 * ibx_display_interrupt_update - update SDEIMR
291 * @dev_priv: driver private
292 * @interrupt_mask: mask of interrupt bits to update
293 * @enabled_irq_mask: mask of interrupt bits to enable
294 */
Daniel Vetter47339cd2014-09-30 10:56:46 +0200295void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
296 uint32_t interrupt_mask,
297 uint32_t enabled_irq_mask)
Daniel Vetterfee884e2013-07-04 23:35:21 +0200298{
299 uint32_t sdeimr = I915_READ(SDEIMR);
300 sdeimr &= ~interrupt_mask;
301 sdeimr |= (~enabled_irq_mask & interrupt_mask);
302
303 assert_spin_locked(&dev_priv->irq_lock);
304
Jesse Barnes9df7575f2014-06-20 09:29:20 -0700305 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300306 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300307
Daniel Vetterfee884e2013-07-04 23:35:21 +0200308 I915_WRITE(SDEIMR, sdeimr);
309 POSTING_READ(SDEIMR);
310}
Paulo Zanoni86642812013-04-12 17:57:57 -0300311
Daniel Vetterb5ea6422014-03-02 21:18:00 +0100312static void
Imre Deak755e9012014-02-10 18:42:47 +0200313__i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
314 u32 enable_mask, u32 status_mask)
Keith Packard7c463582008-11-04 02:03:27 -0800315{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200316 u32 reg = PIPESTAT(pipe);
Imre Deak755e9012014-02-10 18:42:47 +0200317 u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
Keith Packard7c463582008-11-04 02:03:27 -0800318
Daniel Vetterb79480b2013-06-27 17:52:10 +0200319 assert_spin_locked(&dev_priv->irq_lock);
Daniel Vetterd518ce52014-08-27 10:43:37 +0200320 WARN_ON(!intel_irqs_enabled(dev_priv));
Daniel Vetterb79480b2013-06-27 17:52:10 +0200321
Ville Syrjälä04feced2014-04-03 13:28:33 +0300322 if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
323 status_mask & ~PIPESTAT_INT_STATUS_MASK,
324 "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
325 pipe_name(pipe), enable_mask, status_mask))
Imre Deak755e9012014-02-10 18:42:47 +0200326 return;
327
328 if ((pipestat & enable_mask) == enable_mask)
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200329 return;
330
Imre Deak91d181d2014-02-10 18:42:49 +0200331 dev_priv->pipestat_irq_mask[pipe] |= status_mask;
332
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200333 /* Enable the interrupt, clear any pending status */
Imre Deak755e9012014-02-10 18:42:47 +0200334 pipestat |= enable_mask | status_mask;
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200335 I915_WRITE(reg, pipestat);
336 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800337}
338
Daniel Vetterb5ea6422014-03-02 21:18:00 +0100339static void
Imre Deak755e9012014-02-10 18:42:47 +0200340__i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
341 u32 enable_mask, u32 status_mask)
Keith Packard7c463582008-11-04 02:03:27 -0800342{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200343 u32 reg = PIPESTAT(pipe);
Imre Deak755e9012014-02-10 18:42:47 +0200344 u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
Keith Packard7c463582008-11-04 02:03:27 -0800345
Daniel Vetterb79480b2013-06-27 17:52:10 +0200346 assert_spin_locked(&dev_priv->irq_lock);
Daniel Vetterd518ce52014-08-27 10:43:37 +0200347 WARN_ON(!intel_irqs_enabled(dev_priv));
Daniel Vetterb79480b2013-06-27 17:52:10 +0200348
Ville Syrjälä04feced2014-04-03 13:28:33 +0300349 if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
350 status_mask & ~PIPESTAT_INT_STATUS_MASK,
351 "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
352 pipe_name(pipe), enable_mask, status_mask))
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200353 return;
354
Imre Deak755e9012014-02-10 18:42:47 +0200355 if ((pipestat & enable_mask) == 0)
356 return;
357
Imre Deak91d181d2014-02-10 18:42:49 +0200358 dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
359
Imre Deak755e9012014-02-10 18:42:47 +0200360 pipestat &= ~enable_mask;
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200361 I915_WRITE(reg, pipestat);
362 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800363}
364
Imre Deak10c59c52014-02-10 18:42:48 +0200365static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
366{
367 u32 enable_mask = status_mask << 16;
368
369 /*
Ville Syrjälä724a6902014-04-09 13:28:48 +0300370 * On pipe A we don't support the PSR interrupt yet,
371 * on pipe B and C the same bit MBZ.
Imre Deak10c59c52014-02-10 18:42:48 +0200372 */
373 if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
374 return 0;
Ville Syrjälä724a6902014-04-09 13:28:48 +0300375 /*
376 * On pipe B and C we don't support the PSR interrupt yet, on pipe
377 * A the same bit is for perf counters which we don't use either.
378 */
379 if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
380 return 0;
Imre Deak10c59c52014-02-10 18:42:48 +0200381
382 enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
383 SPRITE0_FLIP_DONE_INT_EN_VLV |
384 SPRITE1_FLIP_DONE_INT_EN_VLV);
385 if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
386 enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
387 if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
388 enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
389
390 return enable_mask;
391}
392
Imre Deak755e9012014-02-10 18:42:47 +0200393void
394i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
395 u32 status_mask)
396{
397 u32 enable_mask;
398
Imre Deak10c59c52014-02-10 18:42:48 +0200399 if (IS_VALLEYVIEW(dev_priv->dev))
400 enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
401 status_mask);
402 else
403 enable_mask = status_mask << 16;
Imre Deak755e9012014-02-10 18:42:47 +0200404 __i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
405}
406
407void
408i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
409 u32 status_mask)
410{
411 u32 enable_mask;
412
Imre Deak10c59c52014-02-10 18:42:48 +0200413 if (IS_VALLEYVIEW(dev_priv->dev))
414 enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
415 status_mask);
416 else
417 enable_mask = status_mask << 16;
Imre Deak755e9012014-02-10 18:42:47 +0200418 __i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
419}
420
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000421/**
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300422 * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
Zhao Yakui01c66882009-10-28 05:10:00 +0000423 */
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300424static void i915_enable_asle_pipestat(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +0000425{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300426 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000427
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300428 if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
429 return;
430
Daniel Vetter13321782014-09-15 14:55:29 +0200431 spin_lock_irq(&dev_priv->irq_lock);
Zhao Yakui01c66882009-10-28 05:10:00 +0000432
Imre Deak755e9012014-02-10 18:42:47 +0200433 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
Jani Nikulaf8987802013-04-29 13:02:53 +0300434 if (INTEL_INFO(dev)->gen >= 4)
Daniel Vetter3b6c42e2013-10-21 18:04:35 +0200435 i915_enable_pipestat(dev_priv, PIPE_A,
Imre Deak755e9012014-02-10 18:42:47 +0200436 PIPE_LEGACY_BLC_EVENT_STATUS);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000437
Daniel Vetter13321782014-09-15 14:55:29 +0200438 spin_unlock_irq(&dev_priv->irq_lock);
Zhao Yakui01c66882009-10-28 05:10:00 +0000439}
440
441/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700442 * i915_pipe_enabled - check if a pipe is enabled
443 * @dev: DRM device
444 * @pipe: pipe to check
445 *
446 * Reading certain registers when the pipe is disabled can hang the chip.
447 * Use this routine to make sure the PLL is running and the pipe is active
448 * before reading such registers if unsure.
449 */
450static int
451i915_pipe_enabled(struct drm_device *dev, int pipe)
452{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300453 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200454
Daniel Vettera01025a2013-05-22 00:50:23 +0200455 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
456 /* Locking is horribly broken here, but whatever. */
457 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni71f8ba62013-05-03 12:15:39 -0300459
Daniel Vettera01025a2013-05-22 00:50:23 +0200460 return intel_crtc->active;
461 } else {
462 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
463 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700464}
465
Ville Syrjäläf75f3742014-05-15 20:20:36 +0300466/*
467 * This timing diagram depicts the video signal in and
468 * around the vertical blanking period.
469 *
470 * Assumptions about the fictitious mode used in this example:
471 * vblank_start >= 3
472 * vsync_start = vblank_start + 1
473 * vsync_end = vblank_start + 2
474 * vtotal = vblank_start + 3
475 *
476 * start of vblank:
477 * latch double buffered registers
478 * increment frame counter (ctg+)
479 * generate start of vblank interrupt (gen4+)
480 * |
481 * | frame start:
482 * | generate frame start interrupt (aka. vblank interrupt) (gmch)
483 * | may be shifted forward 1-3 extra lines via PIPECONF
484 * | |
485 * | | start of vsync:
486 * | | generate vsync interrupt
487 * | | |
488 * ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx
489 * . \hs/ . \hs/ \hs/ \hs/ . \hs/
490 * ----va---> <-----------------vb--------------------> <--------va-------------
491 * | | <----vs-----> |
492 * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
493 * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
494 * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
495 * | | |
496 * last visible pixel first visible pixel
497 * | increment frame counter (gen3/4)
498 * pixel counter = vblank_start * htotal pixel counter = 0 (gen3/4)
499 *
500 * x = horizontal active
501 * _ = horizontal blanking
502 * hs = horizontal sync
503 * va = vertical active
504 * vb = vertical blanking
505 * vs = vertical sync
506 * vbs = vblank_start (number)
507 *
508 * Summary:
509 * - most events happen at the start of horizontal sync
510 * - frame start happens at the start of horizontal blank, 1-4 lines
511 * (depending on PIPECONF settings) after the start of vblank
512 * - gen3/4 pixel and frame counter are synchronized with the start
513 * of horizontal active on the first line of vertical active
514 */
515
Ville Syrjälä4cdb83e2013-10-11 21:52:44 +0300516static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
517{
518 /* Gen2 doesn't have a hardware frame counter */
519 return 0;
520}
521
Keith Packard42f52ef2008-10-18 19:39:29 -0700522/* Called from drm generic code, passed a 'crtc', which
523 * we use as a pipe index
524 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700525static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700526{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300527 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700528 unsigned long high_frame;
529 unsigned long low_frame;
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300530 u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700531
532 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800533 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800534 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700535 return 0;
536 }
537
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300538 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
539 struct intel_crtc *intel_crtc =
540 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
541 const struct drm_display_mode *mode =
542 &intel_crtc->config.adjusted_mode;
543
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300544 htotal = mode->crtc_htotal;
545 hsync_start = mode->crtc_hsync_start;
546 vbl_start = mode->crtc_vblank_start;
547 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
548 vbl_start = DIV_ROUND_UP(vbl_start, 2);
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300549 } else {
Daniel Vettera2d213d2014-02-07 16:34:05 +0100550 enum transcoder cpu_transcoder = (enum transcoder) pipe;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300551
552 htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300553 hsync_start = (I915_READ(HSYNC(cpu_transcoder)) & 0x1fff) + 1;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300554 vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300555 if ((I915_READ(PIPECONF(cpu_transcoder)) &
556 PIPECONF_INTERLACE_MASK) != PIPECONF_PROGRESSIVE)
557 vbl_start = DIV_ROUND_UP(vbl_start, 2);
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300558 }
559
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300560 /* Convert to pixel count */
561 vbl_start *= htotal;
562
563 /* Start of vblank event occurs at start of hsync */
564 vbl_start -= htotal - hsync_start;
565
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800566 high_frame = PIPEFRAME(pipe);
567 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100568
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700569 /*
570 * High & low register fields aren't synchronized, so make sure
571 * we get a low value that's stable across two reads of the high
572 * register.
573 */
574 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100575 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300576 low = I915_READ(low_frame);
Chris Wilson5eddb702010-09-11 13:48:45 +0100577 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700578 } while (high1 != high2);
579
Chris Wilson5eddb702010-09-11 13:48:45 +0100580 high1 >>= PIPE_FRAME_HIGH_SHIFT;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300581 pixel = low & PIPE_PIXEL_MASK;
Chris Wilson5eddb702010-09-11 13:48:45 +0100582 low >>= PIPE_FRAME_LOW_SHIFT;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300583
584 /*
585 * The frame counter increments at beginning of active.
586 * Cook up a vblank counter by also checking the pixel
587 * counter against vblank start.
588 */
Ville Syrjäläedc08d02013-11-06 13:56:27 -0200589 return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700590}
591
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700592static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800593{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300594 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800595 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800596
597 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800598 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800599 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800600 return 0;
601 }
602
603 return I915_READ(reg);
604}
605
Mario Kleinerad3543e2013-10-30 05:13:08 +0100606/* raw reads, only for fast reads of display block, no need for forcewake etc. */
607#define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
Mario Kleinerad3543e2013-10-30 05:13:08 +0100608
Ville Syrjäläa225f072014-04-29 13:35:45 +0300609static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
610{
611 struct drm_device *dev = crtc->base.dev;
612 struct drm_i915_private *dev_priv = dev->dev_private;
613 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
614 enum pipe pipe = crtc->pipe;
Ville Syrjälä80715b22014-05-15 20:23:23 +0300615 int position, vtotal;
Ville Syrjäläa225f072014-04-29 13:35:45 +0300616
Ville Syrjälä80715b22014-05-15 20:23:23 +0300617 vtotal = mode->crtc_vtotal;
Ville Syrjäläa225f072014-04-29 13:35:45 +0300618 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
619 vtotal /= 2;
620
621 if (IS_GEN2(dev))
622 position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
623 else
624 position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
625
626 /*
Ville Syrjälä80715b22014-05-15 20:23:23 +0300627 * See update_scanline_offset() for the details on the
628 * scanline_offset adjustment.
Ville Syrjäläa225f072014-04-29 13:35:45 +0300629 */
Ville Syrjälä80715b22014-05-15 20:23:23 +0300630 return (position + crtc->scanline_offset) % vtotal;
Ville Syrjäläa225f072014-04-29 13:35:45 +0300631}
632
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700633static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200634 unsigned int flags, int *vpos, int *hpos,
635 ktime_t *stime, ktime_t *etime)
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100636{
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300637 struct drm_i915_private *dev_priv = dev->dev_private;
638 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
639 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
640 const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300641 int position;
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300642 int vbl_start, vbl_end, hsync_start, htotal, vtotal;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100643 bool in_vbl = true;
644 int ret = 0;
Mario Kleinerad3543e2013-10-30 05:13:08 +0100645 unsigned long irqflags;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100646
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300647 if (!intel_crtc->active) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100648 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800649 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100650 return 0;
651 }
652
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300653 htotal = mode->crtc_htotal;
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300654 hsync_start = mode->crtc_hsync_start;
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300655 vtotal = mode->crtc_vtotal;
656 vbl_start = mode->crtc_vblank_start;
657 vbl_end = mode->crtc_vblank_end;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100658
Ville Syrjäläd31faf62013-10-28 16:31:41 +0200659 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
660 vbl_start = DIV_ROUND_UP(vbl_start, 2);
661 vbl_end /= 2;
662 vtotal /= 2;
663 }
664
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300665 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
666
Mario Kleinerad3543e2013-10-30 05:13:08 +0100667 /*
668 * Lock uncore.lock, as we will do multiple timing critical raw
669 * register reads, potentially with preemption disabled, so the
670 * following code must not block on uncore.lock.
671 */
672 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300673
Mario Kleinerad3543e2013-10-30 05:13:08 +0100674 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
675
676 /* Get optional system timestamp before query. */
677 if (stime)
678 *stime = ktime_get();
679
Ville Syrjälä7c06b082013-10-11 21:52:43 +0300680 if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100681 /* No obvious pixelcount register. Only query vertical
682 * scanout position from Display scan line register.
683 */
Ville Syrjäläa225f072014-04-29 13:35:45 +0300684 position = __intel_get_crtc_scanline(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100685 } else {
686 /* Have access to pixelcount since start of frame.
687 * We can split this into vertical and horizontal
688 * scanout position.
689 */
Mario Kleinerad3543e2013-10-30 05:13:08 +0100690 position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100691
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300692 /* convert to pixel counts */
693 vbl_start *= htotal;
694 vbl_end *= htotal;
695 vtotal *= htotal;
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300696
697 /*
Ville Syrjälä7e78f1cb2014-04-29 13:35:49 +0300698 * In interlaced modes, the pixel counter counts all pixels,
699 * so one field will have htotal more pixels. In order to avoid
700 * the reported position from jumping backwards when the pixel
701 * counter is beyond the length of the shorter field, just
702 * clamp the position the length of the shorter field. This
703 * matches how the scanline counter based position works since
704 * the scanline counter doesn't count the two half lines.
705 */
706 if (position >= vtotal)
707 position = vtotal - 1;
708
709 /*
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300710 * Start of vblank interrupt is triggered at start of hsync,
711 * just prior to the first active line of vblank. However we
712 * consider lines to start at the leading edge of horizontal
713 * active. So, should we get here before we've crossed into
714 * the horizontal active of the first line in vblank, we would
715 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
716 * always add htotal-hsync_start to the current pixel position.
717 */
718 position = (position + htotal - hsync_start) % vtotal;
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300719 }
720
Mario Kleinerad3543e2013-10-30 05:13:08 +0100721 /* Get optional system timestamp after query. */
722 if (etime)
723 *etime = ktime_get();
724
725 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
726
727 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
728
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300729 in_vbl = position >= vbl_start && position < vbl_end;
730
731 /*
732 * While in vblank, position will be negative
733 * counting up towards 0 at vbl_end. And outside
734 * vblank, position will be positive counting
735 * up since vbl_end.
736 */
737 if (position >= vbl_start)
738 position -= vbl_end;
739 else
740 position += vtotal - vbl_end;
741
Ville Syrjälä7c06b082013-10-11 21:52:43 +0300742 if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300743 *vpos = position;
744 *hpos = 0;
745 } else {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100746 *vpos = position / htotal;
747 *hpos = position - (*vpos * htotal);
748 }
749
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100750 /* In vblank? */
751 if (in_vbl)
Daniel Vetter3d3cbd82014-09-10 17:36:11 +0200752 ret |= DRM_SCANOUTPOS_IN_VBLANK;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100753
754 return ret;
755}
756
Ville Syrjäläa225f072014-04-29 13:35:45 +0300757int intel_get_crtc_scanline(struct intel_crtc *crtc)
758{
759 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
760 unsigned long irqflags;
761 int position;
762
763 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
764 position = __intel_get_crtc_scanline(crtc);
765 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
766
767 return position;
768}
769
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700770static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100771 int *max_error,
772 struct timeval *vblank_time,
773 unsigned flags)
774{
Chris Wilson4041b852011-01-22 10:07:56 +0000775 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100776
Ben Widawsky7eb552a2013-03-13 14:05:41 -0700777 if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
Chris Wilson4041b852011-01-22 10:07:56 +0000778 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100779 return -EINVAL;
780 }
781
782 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000783 crtc = intel_get_crtc_for_pipe(dev, pipe);
784 if (crtc == NULL) {
785 DRM_ERROR("Invalid crtc %d\n", pipe);
786 return -EINVAL;
787 }
788
789 if (!crtc->enabled) {
790 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
791 return -EBUSY;
792 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100793
794 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000795 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
796 vblank_time, flags,
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300797 crtc,
798 &to_intel_crtc(crtc)->config.adjusted_mode);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100799}
800
Jani Nikula67c347f2013-09-17 14:26:34 +0300801static bool intel_hpd_irq_event(struct drm_device *dev,
802 struct drm_connector *connector)
Egbert Eich321a1b32013-04-11 16:00:26 +0200803{
804 enum drm_connector_status old_status;
805
806 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
807 old_status = connector->status;
808
809 connector->status = connector->funcs->detect(connector, false);
Jani Nikula67c347f2013-09-17 14:26:34 +0300810 if (old_status == connector->status)
811 return false;
812
813 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
Egbert Eich321a1b32013-04-11 16:00:26 +0200814 connector->base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +0300815 connector->name,
Jani Nikula67c347f2013-09-17 14:26:34 +0300816 drm_get_connector_status_name(old_status),
817 drm_get_connector_status_name(connector->status));
818
819 return true;
Egbert Eich321a1b32013-04-11 16:00:26 +0200820}
821
Dave Airlie13cf5502014-06-18 11:29:35 +1000822static void i915_digport_work_func(struct work_struct *work)
823{
824 struct drm_i915_private *dev_priv =
825 container_of(work, struct drm_i915_private, dig_port_work);
Dave Airlie13cf5502014-06-18 11:29:35 +1000826 u32 long_port_mask, short_port_mask;
827 struct intel_digital_port *intel_dig_port;
828 int i, ret;
829 u32 old_bits = 0;
830
Daniel Vetter4cb21832014-09-15 14:55:26 +0200831 spin_lock_irq(&dev_priv->irq_lock);
Dave Airlie13cf5502014-06-18 11:29:35 +1000832 long_port_mask = dev_priv->long_hpd_port_mask;
833 dev_priv->long_hpd_port_mask = 0;
834 short_port_mask = dev_priv->short_hpd_port_mask;
835 dev_priv->short_hpd_port_mask = 0;
Daniel Vetter4cb21832014-09-15 14:55:26 +0200836 spin_unlock_irq(&dev_priv->irq_lock);
Dave Airlie13cf5502014-06-18 11:29:35 +1000837
838 for (i = 0; i < I915_MAX_PORTS; i++) {
839 bool valid = false;
840 bool long_hpd = false;
841 intel_dig_port = dev_priv->hpd_irq_port[i];
842 if (!intel_dig_port || !intel_dig_port->hpd_pulse)
843 continue;
844
845 if (long_port_mask & (1 << i)) {
846 valid = true;
847 long_hpd = true;
848 } else if (short_port_mask & (1 << i))
849 valid = true;
850
851 if (valid) {
852 ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
853 if (ret == true) {
854 /* if we get true fallback to old school hpd */
855 old_bits |= (1 << intel_dig_port->base.hpd_pin);
856 }
857 }
858 }
859
860 if (old_bits) {
Daniel Vetter4cb21832014-09-15 14:55:26 +0200861 spin_lock_irq(&dev_priv->irq_lock);
Dave Airlie13cf5502014-06-18 11:29:35 +1000862 dev_priv->hpd_event_bits |= old_bits;
Daniel Vetter4cb21832014-09-15 14:55:26 +0200863 spin_unlock_irq(&dev_priv->irq_lock);
Dave Airlie13cf5502014-06-18 11:29:35 +1000864 schedule_work(&dev_priv->hotplug_work);
865 }
866}
867
Jesse Barnes5ca58282009-03-31 14:11:15 -0700868/*
869 * Handle hotplug events outside the interrupt handler proper.
870 */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200871#define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
872
Jesse Barnes5ca58282009-03-31 14:11:15 -0700873static void i915_hotplug_work_func(struct work_struct *work)
874{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300875 struct drm_i915_private *dev_priv =
876 container_of(work, struct drm_i915_private, hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700877 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700878 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200879 struct intel_connector *intel_connector;
880 struct intel_encoder *intel_encoder;
881 struct drm_connector *connector;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200882 bool hpd_disabled = false;
Egbert Eich321a1b32013-04-11 16:00:26 +0200883 bool changed = false;
Egbert Eich142e2392013-04-11 15:57:57 +0200884 u32 hpd_event_bits;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700885
Keith Packarda65e34c2011-07-25 10:04:56 -0700886 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -0800887 DRM_DEBUG_KMS("running encoder hotplug functions\n");
888
Daniel Vetter4cb21832014-09-15 14:55:26 +0200889 spin_lock_irq(&dev_priv->irq_lock);
Egbert Eich142e2392013-04-11 15:57:57 +0200890
891 hpd_event_bits = dev_priv->hpd_event_bits;
892 dev_priv->hpd_event_bits = 0;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200893 list_for_each_entry(connector, &mode_config->connector_list, head) {
894 intel_connector = to_intel_connector(connector);
Dave Airlie36cd7442014-05-02 13:44:18 +1000895 if (!intel_connector->encoder)
896 continue;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200897 intel_encoder = intel_connector->encoder;
898 if (intel_encoder->hpd_pin > HPD_NONE &&
899 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
900 connector->polled == DRM_CONNECTOR_POLL_HPD) {
901 DRM_INFO("HPD interrupt storm detected on connector %s: "
902 "switching from hotplug detection to polling\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300903 connector->name);
Egbert Eichcd569ae2013-04-16 13:36:57 +0200904 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
905 connector->polled = DRM_CONNECTOR_POLL_CONNECT
906 | DRM_CONNECTOR_POLL_DISCONNECT;
907 hpd_disabled = true;
908 }
Egbert Eich142e2392013-04-11 15:57:57 +0200909 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
910 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300911 connector->name, intel_encoder->hpd_pin);
Egbert Eich142e2392013-04-11 15:57:57 +0200912 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200913 }
914 /* if there were no outputs to poll, poll was disabled,
915 * therefore make sure it's enabled when disabling HPD on
916 * some connectors */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200917 if (hpd_disabled) {
Egbert Eichcd569ae2013-04-16 13:36:57 +0200918 drm_kms_helper_poll_enable(dev);
Imre Deak63237512014-08-18 15:37:02 +0300919 mod_delayed_work(system_wq, &dev_priv->hotplug_reenable_work,
920 msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
Egbert Eichac4c16c2013-04-16 13:36:58 +0200921 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200922
Daniel Vetter4cb21832014-09-15 14:55:26 +0200923 spin_unlock_irq(&dev_priv->irq_lock);
Egbert Eichcd569ae2013-04-16 13:36:57 +0200924
Egbert Eich321a1b32013-04-11 16:00:26 +0200925 list_for_each_entry(connector, &mode_config->connector_list, head) {
926 intel_connector = to_intel_connector(connector);
Dave Airlie36cd7442014-05-02 13:44:18 +1000927 if (!intel_connector->encoder)
928 continue;
Egbert Eich321a1b32013-04-11 16:00:26 +0200929 intel_encoder = intel_connector->encoder;
930 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
931 if (intel_encoder->hot_plug)
932 intel_encoder->hot_plug(intel_encoder);
933 if (intel_hpd_irq_event(dev, connector))
934 changed = true;
935 }
936 }
Keith Packard40ee3382011-07-28 15:31:19 -0700937 mutex_unlock(&mode_config->mutex);
938
Egbert Eich321a1b32013-04-11 16:00:26 +0200939 if (changed)
940 drm_kms_helper_hotplug_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700941}
942
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200943static void ironlake_rps_change_irq_handler(struct drm_device *dev)
Jesse Barnesf97108d2010-01-29 11:27:07 -0800944{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300945 struct drm_i915_private *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000946 u32 busy_up, busy_down, max_avg, min_avg;
Daniel Vetter92703882012-08-09 16:46:01 +0200947 u8 new_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200948
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200949 spin_lock(&mchdev_lock);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800950
Daniel Vetter73edd18f2012-08-08 23:35:37 +0200951 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
952
Daniel Vetter20e4d402012-08-08 23:35:39 +0200953 new_delay = dev_priv->ips.cur_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200954
Jesse Barnes7648fa92010-05-20 14:28:11 -0700955 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000956 busy_up = I915_READ(RCPREVBSYTUPAVG);
957 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800958 max_avg = I915_READ(RCBMAXAVG);
959 min_avg = I915_READ(RCBMINAVG);
960
961 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000962 if (busy_up > max_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200963 if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
964 new_delay = dev_priv->ips.cur_delay - 1;
965 if (new_delay < dev_priv->ips.max_delay)
966 new_delay = dev_priv->ips.max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000967 } else if (busy_down < min_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200968 if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
969 new_delay = dev_priv->ips.cur_delay + 1;
970 if (new_delay > dev_priv->ips.min_delay)
971 new_delay = dev_priv->ips.min_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800972 }
973
Jesse Barnes7648fa92010-05-20 14:28:11 -0700974 if (ironlake_set_drps(dev, new_delay))
Daniel Vetter20e4d402012-08-08 23:35:39 +0200975 dev_priv->ips.cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800976
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200977 spin_unlock(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +0200978
Jesse Barnesf97108d2010-01-29 11:27:07 -0800979 return;
980}
981
Chris Wilson549f7362010-10-19 11:19:32 +0100982static void notify_ring(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100983 struct intel_engine_cs *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100984{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100985 if (!intel_ring_initialized(ring))
Chris Wilson475553d2011-01-20 09:52:56 +0000986 return;
987
Chris Wilson814e9b52013-09-23 17:33:19 -0300988 trace_i915_gem_request_complete(ring);
Chris Wilson9862e602011-01-04 22:22:17 +0000989
Chris Wilson549f7362010-10-19 11:19:32 +0100990 wake_up_all(&ring->irq_queue);
Mika Kuoppala10cd45b2013-07-03 17:22:08 +0300991 i915_queue_hangcheck(dev);
Chris Wilson549f7362010-10-19 11:19:32 +0100992}
993
Deepak S31685c22014-07-03 17:33:01 -0400994static u32 vlv_c0_residency(struct drm_i915_private *dev_priv,
Chris Wilsonbf225f22014-07-10 20:31:18 +0100995 struct intel_rps_ei *rps_ei)
Deepak S31685c22014-07-03 17:33:01 -0400996{
997 u32 cz_ts, cz_freq_khz;
998 u32 render_count, media_count;
999 u32 elapsed_render, elapsed_media, elapsed_time;
1000 u32 residency = 0;
1001
1002 cz_ts = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
1003 cz_freq_khz = DIV_ROUND_CLOSEST(dev_priv->mem_freq * 1000, 4);
1004
1005 render_count = I915_READ(VLV_RENDER_C0_COUNT_REG);
1006 media_count = I915_READ(VLV_MEDIA_C0_COUNT_REG);
1007
Chris Wilsonbf225f22014-07-10 20:31:18 +01001008 if (rps_ei->cz_clock == 0) {
1009 rps_ei->cz_clock = cz_ts;
1010 rps_ei->render_c0 = render_count;
1011 rps_ei->media_c0 = media_count;
Deepak S31685c22014-07-03 17:33:01 -04001012
1013 return dev_priv->rps.cur_freq;
1014 }
1015
Chris Wilsonbf225f22014-07-10 20:31:18 +01001016 elapsed_time = cz_ts - rps_ei->cz_clock;
1017 rps_ei->cz_clock = cz_ts;
Deepak S31685c22014-07-03 17:33:01 -04001018
Chris Wilsonbf225f22014-07-10 20:31:18 +01001019 elapsed_render = render_count - rps_ei->render_c0;
1020 rps_ei->render_c0 = render_count;
Deepak S31685c22014-07-03 17:33:01 -04001021
Chris Wilsonbf225f22014-07-10 20:31:18 +01001022 elapsed_media = media_count - rps_ei->media_c0;
1023 rps_ei->media_c0 = media_count;
Deepak S31685c22014-07-03 17:33:01 -04001024
1025 /* Convert all the counters into common unit of milli sec */
1026 elapsed_time /= VLV_CZ_CLOCK_TO_MILLI_SEC;
1027 elapsed_render /= cz_freq_khz;
1028 elapsed_media /= cz_freq_khz;
1029
1030 /*
1031 * Calculate overall C0 residency percentage
1032 * only if elapsed time is non zero
1033 */
1034 if (elapsed_time) {
1035 residency =
1036 ((max(elapsed_render, elapsed_media) * 100)
1037 / elapsed_time);
1038 }
1039
1040 return residency;
1041}
1042
1043/**
1044 * vlv_calc_delay_from_C0_counters - Increase/Decrease freq based on GPU
1045 * busy-ness calculated from C0 counters of render & media power wells
1046 * @dev_priv: DRM device private
1047 *
1048 */
Damien Lespiau4fa79042014-08-08 19:25:57 +01001049static int vlv_calc_delay_from_C0_counters(struct drm_i915_private *dev_priv)
Deepak S31685c22014-07-03 17:33:01 -04001050{
1051 u32 residency_C0_up = 0, residency_C0_down = 0;
Damien Lespiau4fa79042014-08-08 19:25:57 +01001052 int new_delay, adj;
Deepak S31685c22014-07-03 17:33:01 -04001053
1054 dev_priv->rps.ei_interrupt_count++;
1055
1056 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
1057
1058
Chris Wilsonbf225f22014-07-10 20:31:18 +01001059 if (dev_priv->rps.up_ei.cz_clock == 0) {
1060 vlv_c0_residency(dev_priv, &dev_priv->rps.up_ei);
1061 vlv_c0_residency(dev_priv, &dev_priv->rps.down_ei);
Deepak S31685c22014-07-03 17:33:01 -04001062 return dev_priv->rps.cur_freq;
1063 }
1064
1065
1066 /*
1067 * To down throttle, C0 residency should be less than down threshold
1068 * for continous EI intervals. So calculate down EI counters
1069 * once in VLV_INT_COUNT_FOR_DOWN_EI
1070 */
1071 if (dev_priv->rps.ei_interrupt_count == VLV_INT_COUNT_FOR_DOWN_EI) {
1072
1073 dev_priv->rps.ei_interrupt_count = 0;
1074
1075 residency_C0_down = vlv_c0_residency(dev_priv,
Chris Wilsonbf225f22014-07-10 20:31:18 +01001076 &dev_priv->rps.down_ei);
Deepak S31685c22014-07-03 17:33:01 -04001077 } else {
1078 residency_C0_up = vlv_c0_residency(dev_priv,
Chris Wilsonbf225f22014-07-10 20:31:18 +01001079 &dev_priv->rps.up_ei);
Deepak S31685c22014-07-03 17:33:01 -04001080 }
1081
1082 new_delay = dev_priv->rps.cur_freq;
1083
1084 adj = dev_priv->rps.last_adj;
1085 /* C0 residency is greater than UP threshold. Increase Frequency */
1086 if (residency_C0_up >= VLV_RP_UP_EI_THRESHOLD) {
1087 if (adj > 0)
1088 adj *= 2;
1089 else
1090 adj = 1;
1091
1092 if (dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit)
1093 new_delay = dev_priv->rps.cur_freq + adj;
1094
1095 /*
1096 * For better performance, jump directly
1097 * to RPe if we're below it.
1098 */
1099 if (new_delay < dev_priv->rps.efficient_freq)
1100 new_delay = dev_priv->rps.efficient_freq;
1101
1102 } else if (!dev_priv->rps.ei_interrupt_count &&
1103 (residency_C0_down < VLV_RP_DOWN_EI_THRESHOLD)) {
1104 if (adj < 0)
1105 adj *= 2;
1106 else
1107 adj = -1;
1108 /*
1109 * This means, C0 residency is less than down threshold over
1110 * a period of VLV_INT_COUNT_FOR_DOWN_EI. So, reduce the freq
1111 */
1112 if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
1113 new_delay = dev_priv->rps.cur_freq + adj;
1114 }
1115
1116 return new_delay;
1117}
1118
Ben Widawsky4912d042011-04-25 11:25:20 -07001119static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001120{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001121 struct drm_i915_private *dev_priv =
1122 container_of(work, struct drm_i915_private, rps.work);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03001123 u32 pm_iir;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001124 int new_delay, adj;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001125
Daniel Vetter59cdb632013-07-04 23:35:28 +02001126 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02001127 pm_iir = dev_priv->rps.pm_iir;
1128 dev_priv->rps.pm_iir = 0;
Imre Deaka72fbc32014-11-05 20:48:31 +02001129 /* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1130 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Daniel Vetter59cdb632013-07-04 23:35:28 +02001131 spin_unlock_irq(&dev_priv->irq_lock);
Ben Widawsky4912d042011-04-25 11:25:20 -07001132
Paulo Zanoni60611c12013-08-15 11:50:01 -03001133 /* Make sure we didn't queue anything we're not going to process. */
Deepak Sa6706b42014-03-15 20:23:22 +05301134 WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
Paulo Zanoni60611c12013-08-15 11:50:01 -03001135
Deepak Sa6706b42014-03-15 20:23:22 +05301136 if ((pm_iir & dev_priv->pm_rps_events) == 0)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001137 return;
1138
Jesse Barnes4fc688c2012-11-02 11:14:01 -07001139 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01001140
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001141 adj = dev_priv->rps.last_adj;
Ville Syrjälä74250342013-06-25 21:38:11 +03001142 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001143 if (adj > 0)
1144 adj *= 2;
Deepak S13a56602014-05-23 21:00:21 +05301145 else {
1146 /* CHV needs even encode values */
1147 adj = IS_CHERRYVIEW(dev_priv->dev) ? 2 : 1;
1148 }
Ben Widawskyb39fb292014-03-19 18:31:11 -07001149 new_delay = dev_priv->rps.cur_freq + adj;
Ville Syrjälä74250342013-06-25 21:38:11 +03001150
1151 /*
1152 * For better performance, jump directly
1153 * to RPe if we're below it.
1154 */
Ben Widawskyb39fb292014-03-19 18:31:11 -07001155 if (new_delay < dev_priv->rps.efficient_freq)
1156 new_delay = dev_priv->rps.efficient_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001157 } else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07001158 if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1159 new_delay = dev_priv->rps.efficient_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001160 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07001161 new_delay = dev_priv->rps.min_freq_softlimit;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001162 adj = 0;
Deepak S31685c22014-07-03 17:33:01 -04001163 } else if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
1164 new_delay = vlv_calc_delay_from_C0_counters(dev_priv);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001165 } else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1166 if (adj < 0)
1167 adj *= 2;
Deepak S13a56602014-05-23 21:00:21 +05301168 else {
1169 /* CHV needs even encode values */
1170 adj = IS_CHERRYVIEW(dev_priv->dev) ? -2 : -1;
1171 }
Ben Widawskyb39fb292014-03-19 18:31:11 -07001172 new_delay = dev_priv->rps.cur_freq + adj;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001173 } else { /* unknown event */
Ben Widawskyb39fb292014-03-19 18:31:11 -07001174 new_delay = dev_priv->rps.cur_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001175 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001176
Ben Widawsky79249632012-09-07 19:43:42 -07001177 /* sysfs frequency interfaces may have snuck in while servicing the
1178 * interrupt
1179 */
Ville Syrjälä1272e7b2013-11-07 19:57:49 +02001180 new_delay = clamp_t(int, new_delay,
Ben Widawskyb39fb292014-03-19 18:31:11 -07001181 dev_priv->rps.min_freq_softlimit,
1182 dev_priv->rps.max_freq_softlimit);
Deepak S27544362014-01-27 21:35:05 +05301183
Ben Widawskyb39fb292014-03-19 18:31:11 -07001184 dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001185
1186 if (IS_VALLEYVIEW(dev_priv->dev))
1187 valleyview_set_rps(dev_priv->dev, new_delay);
1188 else
1189 gen6_set_rps(dev_priv->dev, new_delay);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001190
Jesse Barnes4fc688c2012-11-02 11:14:01 -07001191 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001192}
1193
Ben Widawskye3689192012-05-25 16:56:22 -07001194
1195/**
1196 * ivybridge_parity_work - Workqueue called when a parity error interrupt
1197 * occurred.
1198 * @work: workqueue struct
1199 *
1200 * Doesn't actually do anything except notify userspace. As a consequence of
1201 * this event, userspace should try to remap the bad rows since statistically
1202 * it is likely the same row is more likely to go bad again.
1203 */
1204static void ivybridge_parity_work(struct work_struct *work)
1205{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001206 struct drm_i915_private *dev_priv =
1207 container_of(work, struct drm_i915_private, l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -07001208 u32 error_status, row, bank, subbank;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001209 char *parity_event[6];
Ben Widawskye3689192012-05-25 16:56:22 -07001210 uint32_t misccpctl;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001211 uint8_t slice = 0;
Ben Widawskye3689192012-05-25 16:56:22 -07001212
1213 /* We must turn off DOP level clock gating to access the L3 registers.
1214 * In order to prevent a get/put style interface, acquire struct mutex
1215 * any time we access those registers.
1216 */
1217 mutex_lock(&dev_priv->dev->struct_mutex);
1218
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001219 /* If we've screwed up tracking, just let the interrupt fire again */
1220 if (WARN_ON(!dev_priv->l3_parity.which_slice))
1221 goto out;
1222
Ben Widawskye3689192012-05-25 16:56:22 -07001223 misccpctl = I915_READ(GEN7_MISCCPCTL);
1224 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1225 POSTING_READ(GEN7_MISCCPCTL);
1226
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001227 while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1228 u32 reg;
Ben Widawskye3689192012-05-25 16:56:22 -07001229
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001230 slice--;
1231 if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
1232 break;
1233
1234 dev_priv->l3_parity.which_slice &= ~(1<<slice);
1235
1236 reg = GEN7_L3CDERRST1 + (slice * 0x200);
1237
1238 error_status = I915_READ(reg);
1239 row = GEN7_PARITY_ERROR_ROW(error_status);
1240 bank = GEN7_PARITY_ERROR_BANK(error_status);
1241 subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1242
1243 I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
1244 POSTING_READ(reg);
1245
1246 parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1247 parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1248 parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1249 parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
1250 parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
1251 parity_event[5] = NULL;
1252
Dave Airlie5bdebb12013-10-11 14:07:25 +10001253 kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001254 KOBJ_CHANGE, parity_event);
1255
1256 DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
1257 slice, row, bank, subbank);
1258
1259 kfree(parity_event[4]);
1260 kfree(parity_event[3]);
1261 kfree(parity_event[2]);
1262 kfree(parity_event[1]);
1263 }
Ben Widawskye3689192012-05-25 16:56:22 -07001264
1265 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
1266
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001267out:
1268 WARN_ON(dev_priv->l3_parity.which_slice);
Daniel Vetter4cb21832014-09-15 14:55:26 +02001269 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetter480c8032014-07-16 09:49:40 +02001270 gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
Daniel Vetter4cb21832014-09-15 14:55:26 +02001271 spin_unlock_irq(&dev_priv->irq_lock);
Ben Widawskye3689192012-05-25 16:56:22 -07001272
1273 mutex_unlock(&dev_priv->dev->struct_mutex);
Ben Widawskye3689192012-05-25 16:56:22 -07001274}
1275
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001276static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
Ben Widawskye3689192012-05-25 16:56:22 -07001277{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001278 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3689192012-05-25 16:56:22 -07001279
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001280 if (!HAS_L3_DPF(dev))
Ben Widawskye3689192012-05-25 16:56:22 -07001281 return;
1282
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001283 spin_lock(&dev_priv->irq_lock);
Daniel Vetter480c8032014-07-16 09:49:40 +02001284 gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001285 spin_unlock(&dev_priv->irq_lock);
Ben Widawskye3689192012-05-25 16:56:22 -07001286
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001287 iir &= GT_PARITY_ERROR(dev);
1288 if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
1289 dev_priv->l3_parity.which_slice |= 1 << 1;
1290
1291 if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
1292 dev_priv->l3_parity.which_slice |= 1 << 0;
1293
Daniel Vettera4da4fa2012-11-02 19:55:07 +01001294 queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -07001295}
1296
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001297static void ilk_gt_irq_handler(struct drm_device *dev,
1298 struct drm_i915_private *dev_priv,
1299 u32 gt_iir)
1300{
1301 if (gt_iir &
1302 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1303 notify_ring(dev, &dev_priv->ring[RCS]);
1304 if (gt_iir & ILK_BSD_USER_INTERRUPT)
1305 notify_ring(dev, &dev_priv->ring[VCS]);
1306}
1307
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001308static void snb_gt_irq_handler(struct drm_device *dev,
1309 struct drm_i915_private *dev_priv,
1310 u32 gt_iir)
1311{
1312
Ben Widawskycc609d52013-05-28 19:22:29 -07001313 if (gt_iir &
1314 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001315 notify_ring(dev, &dev_priv->ring[RCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001316 if (gt_iir & GT_BSD_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001317 notify_ring(dev, &dev_priv->ring[VCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001318 if (gt_iir & GT_BLT_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001319 notify_ring(dev, &dev_priv->ring[BCS]);
1320
Ben Widawskycc609d52013-05-28 19:22:29 -07001321 if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1322 GT_BSD_CS_ERROR_INTERRUPT |
1323 GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
Mika Kuoppala58174462014-02-25 17:11:26 +02001324 i915_handle_error(dev, false, "GT error interrupt 0x%08x",
1325 gt_iir);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001326 }
Ben Widawskye3689192012-05-25 16:56:22 -07001327
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001328 if (gt_iir & GT_PARITY_ERROR(dev))
1329 ivybridge_parity_error_irq_handler(dev, gt_iir);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001330}
1331
Ben Widawskyabd58f02013-11-02 21:07:09 -07001332static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1333 struct drm_i915_private *dev_priv,
1334 u32 master_ctl)
1335{
Thomas Daniele981e7b2014-07-24 17:04:39 +01001336 struct intel_engine_cs *ring;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001337 u32 rcs, bcs, vcs;
1338 uint32_t tmp = 0;
1339 irqreturn_t ret = IRQ_NONE;
1340
1341 if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1342 tmp = I915_READ(GEN8_GT_IIR(0));
1343 if (tmp) {
Oscar Mateo38cc46d2014-06-16 16:10:59 +01001344 I915_WRITE(GEN8_GT_IIR(0), tmp);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001345 ret = IRQ_HANDLED;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001346
Ben Widawskyabd58f02013-11-02 21:07:09 -07001347 rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001348 ring = &dev_priv->ring[RCS];
Ben Widawskyabd58f02013-11-02 21:07:09 -07001349 if (rcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001350 notify_ring(dev, ring);
1351 if (rcs & GT_CONTEXT_SWITCH_INTERRUPT)
1352 intel_execlists_handle_ctx_events(ring);
1353
1354 bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1355 ring = &dev_priv->ring[BCS];
Ben Widawskyabd58f02013-11-02 21:07:09 -07001356 if (bcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001357 notify_ring(dev, ring);
1358 if (bcs & GT_CONTEXT_SWITCH_INTERRUPT)
1359 intel_execlists_handle_ctx_events(ring);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001360 } else
1361 DRM_ERROR("The master control interrupt lied (GT0)!\n");
1362 }
1363
Zhao Yakui85f9b5f2014-04-17 10:37:38 +08001364 if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
Ben Widawskyabd58f02013-11-02 21:07:09 -07001365 tmp = I915_READ(GEN8_GT_IIR(1));
1366 if (tmp) {
Oscar Mateo38cc46d2014-06-16 16:10:59 +01001367 I915_WRITE(GEN8_GT_IIR(1), tmp);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001368 ret = IRQ_HANDLED;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001369
Ben Widawskyabd58f02013-11-02 21:07:09 -07001370 vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001371 ring = &dev_priv->ring[VCS];
Ben Widawskyabd58f02013-11-02 21:07:09 -07001372 if (vcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001373 notify_ring(dev, ring);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001374 if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001375 intel_execlists_handle_ctx_events(ring);
1376
Zhao Yakui85f9b5f2014-04-17 10:37:38 +08001377 vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001378 ring = &dev_priv->ring[VCS2];
Zhao Yakui85f9b5f2014-04-17 10:37:38 +08001379 if (vcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001380 notify_ring(dev, ring);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001381 if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001382 intel_execlists_handle_ctx_events(ring);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001383 } else
1384 DRM_ERROR("The master control interrupt lied (GT1)!\n");
1385 }
1386
Ben Widawsky09610212014-05-15 20:58:08 +03001387 if (master_ctl & GEN8_GT_PM_IRQ) {
1388 tmp = I915_READ(GEN8_GT_IIR(2));
1389 if (tmp & dev_priv->pm_rps_events) {
Ben Widawsky09610212014-05-15 20:58:08 +03001390 I915_WRITE(GEN8_GT_IIR(2),
1391 tmp & dev_priv->pm_rps_events);
Oscar Mateo38cc46d2014-06-16 16:10:59 +01001392 ret = IRQ_HANDLED;
Imre Deakc9a9a262014-11-05 20:48:37 +02001393 gen6_rps_irq_handler(dev_priv, tmp);
Ben Widawsky09610212014-05-15 20:58:08 +03001394 } else
1395 DRM_ERROR("The master control interrupt lied (PM)!\n");
1396 }
1397
Ben Widawskyabd58f02013-11-02 21:07:09 -07001398 if (master_ctl & GEN8_GT_VECS_IRQ) {
1399 tmp = I915_READ(GEN8_GT_IIR(3));
1400 if (tmp) {
Oscar Mateo38cc46d2014-06-16 16:10:59 +01001401 I915_WRITE(GEN8_GT_IIR(3), tmp);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001402 ret = IRQ_HANDLED;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001403
Ben Widawskyabd58f02013-11-02 21:07:09 -07001404 vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001405 ring = &dev_priv->ring[VECS];
Ben Widawskyabd58f02013-11-02 21:07:09 -07001406 if (vcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001407 notify_ring(dev, ring);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001408 if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001409 intel_execlists_handle_ctx_events(ring);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001410 } else
1411 DRM_ERROR("The master control interrupt lied (GT3)!\n");
1412 }
1413
1414 return ret;
1415}
1416
Egbert Eichb543fb02013-04-16 13:36:54 +02001417#define HPD_STORM_DETECT_PERIOD 1000
1418#define HPD_STORM_THRESHOLD 5
1419
Jani Nikula07c338c2014-10-02 11:16:32 +03001420static int pch_port_to_hotplug_shift(enum port port)
Dave Airlie13cf5502014-06-18 11:29:35 +10001421{
1422 switch (port) {
1423 case PORT_A:
1424 case PORT_E:
1425 default:
1426 return -1;
1427 case PORT_B:
1428 return 0;
1429 case PORT_C:
1430 return 8;
1431 case PORT_D:
1432 return 16;
1433 }
1434}
1435
Jani Nikula07c338c2014-10-02 11:16:32 +03001436static int i915_port_to_hotplug_shift(enum port port)
Dave Airlie13cf5502014-06-18 11:29:35 +10001437{
1438 switch (port) {
1439 case PORT_A:
1440 case PORT_E:
1441 default:
1442 return -1;
1443 case PORT_B:
1444 return 17;
1445 case PORT_C:
1446 return 19;
1447 case PORT_D:
1448 return 21;
1449 }
1450}
1451
1452static inline enum port get_port_from_pin(enum hpd_pin pin)
1453{
1454 switch (pin) {
1455 case HPD_PORT_B:
1456 return PORT_B;
1457 case HPD_PORT_C:
1458 return PORT_C;
1459 case HPD_PORT_D:
1460 return PORT_D;
1461 default:
1462 return PORT_A; /* no hpd */
1463 }
1464}
1465
Daniel Vetter10a504d2013-06-27 17:52:12 +02001466static inline void intel_hpd_irq_handler(struct drm_device *dev,
Daniel Vetter22062db2013-06-27 17:52:11 +02001467 u32 hotplug_trigger,
Dave Airlie13cf5502014-06-18 11:29:35 +10001468 u32 dig_hotplug_reg,
Daniel Vetter22062db2013-06-27 17:52:11 +02001469 const u32 *hpd)
Egbert Eichb543fb02013-04-16 13:36:54 +02001470{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001471 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eichb543fb02013-04-16 13:36:54 +02001472 int i;
Dave Airlie13cf5502014-06-18 11:29:35 +10001473 enum port port;
Daniel Vetter10a504d2013-06-27 17:52:12 +02001474 bool storm_detected = false;
Dave Airlie13cf5502014-06-18 11:29:35 +10001475 bool queue_dig = false, queue_hp = false;
1476 u32 dig_shift;
1477 u32 dig_port_mask = 0;
Egbert Eichb543fb02013-04-16 13:36:54 +02001478
Daniel Vetter91d131d2013-06-27 17:52:14 +02001479 if (!hotplug_trigger)
1480 return;
1481
Dave Airlie13cf5502014-06-18 11:29:35 +10001482 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
1483 hotplug_trigger, dig_hotplug_reg);
Imre Deakcc9bd492014-01-16 19:56:54 +02001484
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02001485 spin_lock(&dev_priv->irq_lock);
Egbert Eichb543fb02013-04-16 13:36:54 +02001486 for (i = 1; i < HPD_NUM_PINS; i++) {
Dave Airlie13cf5502014-06-18 11:29:35 +10001487 if (!(hpd[i] & hotplug_trigger))
1488 continue;
Egbert Eich821450c2013-04-16 13:36:55 +02001489
Dave Airlie13cf5502014-06-18 11:29:35 +10001490 port = get_port_from_pin(i);
1491 if (port && dev_priv->hpd_irq_port[port]) {
1492 bool long_hpd;
1493
Jani Nikula07c338c2014-10-02 11:16:32 +03001494 if (HAS_PCH_SPLIT(dev)) {
1495 dig_shift = pch_port_to_hotplug_shift(port);
Dave Airlie13cf5502014-06-18 11:29:35 +10001496 long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
Jani Nikula07c338c2014-10-02 11:16:32 +03001497 } else {
1498 dig_shift = i915_port_to_hotplug_shift(port);
1499 long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
Dave Airlie13cf5502014-06-18 11:29:35 +10001500 }
1501
Ville Syrjälä26fbb772014-08-11 18:37:37 +03001502 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
1503 port_name(port),
1504 long_hpd ? "long" : "short");
Dave Airlie13cf5502014-06-18 11:29:35 +10001505 /* for long HPD pulses we want to have the digital queue happen,
1506 but we still want HPD storm detection to function. */
1507 if (long_hpd) {
1508 dev_priv->long_hpd_port_mask |= (1 << port);
1509 dig_port_mask |= hpd[i];
1510 } else {
1511 /* for short HPD just trigger the digital queue */
1512 dev_priv->short_hpd_port_mask |= (1 << port);
1513 hotplug_trigger &= ~hpd[i];
1514 }
1515 queue_dig = true;
1516 }
1517 }
1518
1519 for (i = 1; i < HPD_NUM_PINS; i++) {
Daniel Vetter3ff04a162014-04-24 12:03:17 +02001520 if (hpd[i] & hotplug_trigger &&
1521 dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
1522 /*
1523 * On GMCH platforms the interrupt mask bits only
1524 * prevent irq generation, not the setting of the
1525 * hotplug bits itself. So only WARN about unexpected
1526 * interrupts on saner platforms.
1527 */
1528 WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1529 "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1530 hotplug_trigger, i, hpd[i]);
1531
1532 continue;
1533 }
Egbert Eichb8f102e2013-07-26 14:14:24 +02001534
Egbert Eichb543fb02013-04-16 13:36:54 +02001535 if (!(hpd[i] & hotplug_trigger) ||
1536 dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1537 continue;
1538
Dave Airlie13cf5502014-06-18 11:29:35 +10001539 if (!(dig_port_mask & hpd[i])) {
1540 dev_priv->hpd_event_bits |= (1 << i);
1541 queue_hp = true;
1542 }
1543
Egbert Eichb543fb02013-04-16 13:36:54 +02001544 if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1545 dev_priv->hpd_stats[i].hpd_last_jiffies
1546 + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1547 dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1548 dev_priv->hpd_stats[i].hpd_cnt = 0;
Egbert Eichb8f102e2013-07-26 14:14:24 +02001549 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001550 } else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1551 dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
Egbert Eich142e2392013-04-11 15:57:57 +02001552 dev_priv->hpd_event_bits &= ~(1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001553 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
Daniel Vetter10a504d2013-06-27 17:52:12 +02001554 storm_detected = true;
Egbert Eichb543fb02013-04-16 13:36:54 +02001555 } else {
1556 dev_priv->hpd_stats[i].hpd_cnt++;
Egbert Eichb8f102e2013-07-26 14:14:24 +02001557 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1558 dev_priv->hpd_stats[i].hpd_cnt);
Egbert Eichb543fb02013-04-16 13:36:54 +02001559 }
1560 }
1561
Daniel Vetter10a504d2013-06-27 17:52:12 +02001562 if (storm_detected)
1563 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02001564 spin_unlock(&dev_priv->irq_lock);
Daniel Vetter5876fa02013-06-27 17:52:13 +02001565
Daniel Vetter645416f2013-09-02 16:22:25 +02001566 /*
1567 * Our hotplug handler can grab modeset locks (by calling down into the
1568 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1569 * queue for otherwise the flush_work in the pageflip code will
1570 * deadlock.
1571 */
Dave Airlie13cf5502014-06-18 11:29:35 +10001572 if (queue_dig)
Dave Airlie0e32b392014-05-02 14:02:48 +10001573 queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
Dave Airlie13cf5502014-06-18 11:29:35 +10001574 if (queue_hp)
1575 schedule_work(&dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +02001576}
1577
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001578static void gmbus_irq_handler(struct drm_device *dev)
1579{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001580 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter28c70f12012-12-01 13:53:45 +01001581
Daniel Vetter28c70f12012-12-01 13:53:45 +01001582 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001583}
1584
Daniel Vetterce99c252012-12-01 13:53:47 +01001585static void dp_aux_irq_handler(struct drm_device *dev)
1586{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001587 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001588
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001589 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetterce99c252012-12-01 13:53:47 +01001590}
1591
Shuang He8bf1e9f2013-10-15 18:55:27 +01001592#if defined(CONFIG_DEBUG_FS)
Daniel Vetter277de952013-10-18 16:37:07 +02001593static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1594 uint32_t crc0, uint32_t crc1,
1595 uint32_t crc2, uint32_t crc3,
1596 uint32_t crc4)
Shuang He8bf1e9f2013-10-15 18:55:27 +01001597{
1598 struct drm_i915_private *dev_priv = dev->dev_private;
1599 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
1600 struct intel_pipe_crc_entry *entry;
Damien Lespiauac2300d2013-10-15 18:55:30 +01001601 int head, tail;
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001602
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001603 spin_lock(&pipe_crc->lock);
1604
Damien Lespiau0c912c72013-10-15 18:55:37 +01001605 if (!pipe_crc->entries) {
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001606 spin_unlock(&pipe_crc->lock);
Damien Lespiau0c912c72013-10-15 18:55:37 +01001607 DRM_ERROR("spurious interrupt\n");
1608 return;
1609 }
1610
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001611 head = pipe_crc->head;
1612 tail = pipe_crc->tail;
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001613
1614 if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001615 spin_unlock(&pipe_crc->lock);
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001616 DRM_ERROR("CRC buffer overflowing\n");
1617 return;
1618 }
1619
1620 entry = &pipe_crc->entries[head];
Shuang He8bf1e9f2013-10-15 18:55:27 +01001621
Daniel Vetter8bc5e952013-10-16 22:55:49 +02001622 entry->frame = dev->driver->get_vblank_counter(dev, pipe);
Daniel Vettereba94eb2013-10-16 22:55:46 +02001623 entry->crc[0] = crc0;
1624 entry->crc[1] = crc1;
1625 entry->crc[2] = crc2;
1626 entry->crc[3] = crc3;
1627 entry->crc[4] = crc4;
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001628
1629 head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001630 pipe_crc->head = head;
1631
1632 spin_unlock(&pipe_crc->lock);
Damien Lespiau07144422013-10-15 18:55:40 +01001633
1634 wake_up_interruptible(&pipe_crc->wq);
Shuang He8bf1e9f2013-10-15 18:55:27 +01001635}
Daniel Vetter277de952013-10-18 16:37:07 +02001636#else
1637static inline void
1638display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1639 uint32_t crc0, uint32_t crc1,
1640 uint32_t crc2, uint32_t crc3,
1641 uint32_t crc4) {}
1642#endif
Daniel Vettereba94eb2013-10-16 22:55:46 +02001643
Daniel Vetter277de952013-10-18 16:37:07 +02001644
1645static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
Daniel Vetter5a69b892013-10-16 22:55:52 +02001646{
1647 struct drm_i915_private *dev_priv = dev->dev_private;
1648
Daniel Vetter277de952013-10-18 16:37:07 +02001649 display_pipe_crc_irq_handler(dev, pipe,
1650 I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1651 0, 0, 0, 0);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001652}
1653
Daniel Vetter277de952013-10-18 16:37:07 +02001654static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
Daniel Vettereba94eb2013-10-16 22:55:46 +02001655{
1656 struct drm_i915_private *dev_priv = dev->dev_private;
1657
Daniel Vetter277de952013-10-18 16:37:07 +02001658 display_pipe_crc_irq_handler(dev, pipe,
1659 I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1660 I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1661 I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1662 I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
1663 I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
Daniel Vettereba94eb2013-10-16 22:55:46 +02001664}
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001665
Daniel Vetter277de952013-10-18 16:37:07 +02001666static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001667{
1668 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0b5c5ed2013-10-16 22:55:53 +02001669 uint32_t res1, res2;
1670
1671 if (INTEL_INFO(dev)->gen >= 3)
1672 res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
1673 else
1674 res1 = 0;
1675
1676 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
1677 res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
1678 else
1679 res2 = 0;
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001680
Daniel Vetter277de952013-10-18 16:37:07 +02001681 display_pipe_crc_irq_handler(dev, pipe,
1682 I915_READ(PIPE_CRC_RES_RED(pipe)),
1683 I915_READ(PIPE_CRC_RES_GREEN(pipe)),
1684 I915_READ(PIPE_CRC_RES_BLUE(pipe)),
1685 res1, res2);
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001686}
Shuang He8bf1e9f2013-10-15 18:55:27 +01001687
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001688/* The RPS events need forcewake, so we add them to a work queue and mask their
1689 * IMR bits until the work is done. Other interrupts can be processed without
1690 * the work queue. */
1691static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
Ben Widawskybaf02a12013-05-28 19:22:24 -07001692{
Imre Deak132f3f12014-11-10 15:34:33 +02001693 /* TODO: RPS on GEN9 is not supported yet. */
1694 if (WARN_ONCE(INTEL_INFO(dev_priv)->gen == 9,
1695 "GEN9: unexpected RPS IRQ\n"))
1696 return;
1697
Deepak Sa6706b42014-03-15 20:23:22 +05301698 if (pm_iir & dev_priv->pm_rps_events) {
Daniel Vetter59cdb632013-07-04 23:35:28 +02001699 spin_lock(&dev_priv->irq_lock);
Deepak Sa6706b42014-03-15 20:23:22 +05301700 dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
Daniel Vetter480c8032014-07-16 09:49:40 +02001701 gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
Daniel Vetter59cdb632013-07-04 23:35:28 +02001702 spin_unlock(&dev_priv->irq_lock);
Daniel Vetter2adbee62013-07-04 23:35:27 +02001703
1704 queue_work(dev_priv->wq, &dev_priv->rps.work);
Ben Widawskybaf02a12013-05-28 19:22:24 -07001705 }
Ben Widawskybaf02a12013-05-28 19:22:24 -07001706
Imre Deakc9a9a262014-11-05 20:48:37 +02001707 if (INTEL_INFO(dev_priv)->gen >= 8)
1708 return;
1709
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001710 if (HAS_VEBOX(dev_priv->dev)) {
1711 if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1712 notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
Ben Widawsky12638c52013-05-28 19:22:31 -07001713
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001714 if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
Mika Kuoppala58174462014-02-25 17:11:26 +02001715 i915_handle_error(dev_priv->dev, false,
1716 "VEBOX CS error interrupt 0x%08x",
1717 pm_iir);
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001718 }
Ben Widawsky12638c52013-05-28 19:22:31 -07001719 }
Ben Widawskybaf02a12013-05-28 19:22:24 -07001720}
1721
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03001722static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
1723{
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03001724 if (!drm_handle_vblank(dev, pipe))
1725 return false;
1726
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03001727 return true;
1728}
1729
Imre Deakc1874ed2014-02-04 21:35:46 +02001730static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
1731{
1732 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak91d181d2014-02-10 18:42:49 +02001733 u32 pipe_stats[I915_MAX_PIPES] = { };
Imre Deakc1874ed2014-02-04 21:35:46 +02001734 int pipe;
1735
Imre Deak58ead0d2014-02-04 21:35:47 +02001736 spin_lock(&dev_priv->irq_lock);
Damien Lespiau055e3932014-08-18 13:49:10 +01001737 for_each_pipe(dev_priv, pipe) {
Imre Deak91d181d2014-02-10 18:42:49 +02001738 int reg;
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001739 u32 mask, iir_bit = 0;
Imre Deak91d181d2014-02-10 18:42:49 +02001740
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001741 /*
1742 * PIPESTAT bits get signalled even when the interrupt is
1743 * disabled with the mask bits, and some of the status bits do
1744 * not generate interrupts at all (like the underrun bit). Hence
1745 * we need to be careful that we only handle what we want to
1746 * handle.
1747 */
Daniel Vetter0f239f42014-09-30 10:56:49 +02001748
1749 /* fifo underruns are filterered in the underrun handler. */
1750 mask = PIPE_FIFO_UNDERRUN_STATUS;
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001751
1752 switch (pipe) {
1753 case PIPE_A:
1754 iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1755 break;
1756 case PIPE_B:
1757 iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1758 break;
Ville Syrjälä3278f672014-04-09 13:28:49 +03001759 case PIPE_C:
1760 iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
1761 break;
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001762 }
1763 if (iir & iir_bit)
1764 mask |= dev_priv->pipestat_irq_mask[pipe];
1765
1766 if (!mask)
Imre Deak91d181d2014-02-10 18:42:49 +02001767 continue;
1768
1769 reg = PIPESTAT(pipe);
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001770 mask |= PIPESTAT_INT_ENABLE_MASK;
1771 pipe_stats[pipe] = I915_READ(reg) & mask;
Imre Deakc1874ed2014-02-04 21:35:46 +02001772
1773 /*
1774 * Clear the PIPE*STAT regs before the IIR
1775 */
Imre Deak91d181d2014-02-10 18:42:49 +02001776 if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
1777 PIPESTAT_INT_STATUS_MASK))
Imre Deakc1874ed2014-02-04 21:35:46 +02001778 I915_WRITE(reg, pipe_stats[pipe]);
1779 }
Imre Deak58ead0d2014-02-04 21:35:47 +02001780 spin_unlock(&dev_priv->irq_lock);
Imre Deakc1874ed2014-02-04 21:35:46 +02001781
Damien Lespiau055e3932014-08-18 13:49:10 +01001782 for_each_pipe(dev_priv, pipe) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01001783 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1784 intel_pipe_handle_vblank(dev, pipe))
1785 intel_check_page_flip(dev, pipe);
Imre Deakc1874ed2014-02-04 21:35:46 +02001786
Imre Deak579a9b02014-02-04 21:35:48 +02001787 if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
Imre Deakc1874ed2014-02-04 21:35:46 +02001788 intel_prepare_page_flip(dev, pipe);
1789 intel_finish_page_flip(dev, pipe);
1790 }
1791
1792 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1793 i9xx_pipe_crc_irq_handler(dev, pipe);
1794
Daniel Vetter1f7247c2014-09-30 10:56:48 +02001795 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1796 intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
Imre Deakc1874ed2014-02-04 21:35:46 +02001797 }
1798
1799 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1800 gmbus_irq_handler(dev);
1801}
1802
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001803static void i9xx_hpd_irq_handler(struct drm_device *dev)
1804{
1805 struct drm_i915_private *dev_priv = dev->dev_private;
1806 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1807
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001808 if (hotplug_status) {
1809 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1810 /*
1811 * Make sure hotplug status is cleared before we clear IIR, or else we
1812 * may miss hotplug events.
1813 */
1814 POSTING_READ(PORT_HOTPLUG_STAT);
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001815
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001816 if (IS_G4X(dev)) {
1817 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001818
Dave Airlie13cf5502014-06-18 11:29:35 +10001819 intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001820 } else {
1821 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
1822
Dave Airlie13cf5502014-06-18 11:29:35 +10001823 intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001824 }
1825
1826 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
1827 hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1828 dp_aux_irq_handler(dev);
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001829 }
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001830}
1831
Daniel Vetterff1f5252012-10-02 15:10:55 +02001832static irqreturn_t valleyview_irq_handler(int irq, void *arg)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001833{
Daniel Vetter45a83f82014-05-12 19:17:55 +02001834 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03001835 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001836 u32 iir, gt_iir, pm_iir;
1837 irqreturn_t ret = IRQ_NONE;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001838
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001839 while (true) {
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001840 /* Find, clear, then process each source of interrupt */
1841
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001842 gt_iir = I915_READ(GTIIR);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001843 if (gt_iir)
1844 I915_WRITE(GTIIR, gt_iir);
1845
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001846 pm_iir = I915_READ(GEN6_PMIIR);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001847 if (pm_iir)
1848 I915_WRITE(GEN6_PMIIR, pm_iir);
1849
1850 iir = I915_READ(VLV_IIR);
1851 if (iir) {
1852 /* Consume port before clearing IIR or we'll miss events */
1853 if (iir & I915_DISPLAY_PORT_INTERRUPT)
1854 i9xx_hpd_irq_handler(dev);
1855 I915_WRITE(VLV_IIR, iir);
1856 }
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001857
1858 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1859 goto out;
1860
1861 ret = IRQ_HANDLED;
1862
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001863 if (gt_iir)
1864 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Paulo Zanoni60611c12013-08-15 11:50:01 -03001865 if (pm_iir)
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001866 gen6_rps_irq_handler(dev_priv, pm_iir);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001867 /* Call regardless, as some status bits might not be
1868 * signalled in iir */
1869 valleyview_pipestat_irq_handler(dev, iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001870 }
1871
1872out:
1873 return ret;
1874}
1875
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001876static irqreturn_t cherryview_irq_handler(int irq, void *arg)
1877{
Daniel Vetter45a83f82014-05-12 19:17:55 +02001878 struct drm_device *dev = arg;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001879 struct drm_i915_private *dev_priv = dev->dev_private;
1880 u32 master_ctl, iir;
1881 irqreturn_t ret = IRQ_NONE;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001882
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001883 for (;;) {
1884 master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
1885 iir = I915_READ(VLV_IIR);
Ville Syrjälä3278f672014-04-09 13:28:49 +03001886
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001887 if (master_ctl == 0 && iir == 0)
1888 break;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001889
Oscar Mateo27b6c122014-06-16 16:11:00 +01001890 ret = IRQ_HANDLED;
1891
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001892 I915_WRITE(GEN8_MASTER_IRQ, 0);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001893
Oscar Mateo27b6c122014-06-16 16:11:00 +01001894 /* Find, clear, then process each source of interrupt */
1895
1896 if (iir) {
1897 /* Consume port before clearing IIR or we'll miss events */
1898 if (iir & I915_DISPLAY_PORT_INTERRUPT)
1899 i9xx_hpd_irq_handler(dev);
1900 I915_WRITE(VLV_IIR, iir);
1901 }
1902
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001903 gen8_gt_irq_handler(dev, dev_priv, master_ctl);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001904
Oscar Mateo27b6c122014-06-16 16:11:00 +01001905 /* Call regardless, as some status bits might not be
1906 * signalled in iir */
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001907 valleyview_pipestat_irq_handler(dev, iir);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001908
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001909 I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
1910 POSTING_READ(GEN8_MASTER_IRQ);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001911 }
1912
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001913 return ret;
1914}
1915
Adam Jackson23e81d62012-06-06 15:45:44 -04001916static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
Jesse Barnes776ad802011-01-04 15:09:39 -08001917{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001918 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001919 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001920 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
Dave Airlie13cf5502014-06-18 11:29:35 +10001921 u32 dig_hotplug_reg;
Jesse Barnes776ad802011-01-04 15:09:39 -08001922
Dave Airlie13cf5502014-06-18 11:29:35 +10001923 dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
1924 I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
1925
1926 intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
Daniel Vetter91d131d2013-06-27 17:52:14 +02001927
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001928 if (pch_iir & SDE_AUDIO_POWER_MASK) {
1929 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1930 SDE_AUDIO_POWER_SHIFT);
Jesse Barnes776ad802011-01-04 15:09:39 -08001931 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001932 port_name(port));
1933 }
Jesse Barnes776ad802011-01-04 15:09:39 -08001934
Daniel Vetterce99c252012-12-01 13:53:47 +01001935 if (pch_iir & SDE_AUX_MASK)
1936 dp_aux_irq_handler(dev);
1937
Jesse Barnes776ad802011-01-04 15:09:39 -08001938 if (pch_iir & SDE_GMBUS)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001939 gmbus_irq_handler(dev);
Jesse Barnes776ad802011-01-04 15:09:39 -08001940
1941 if (pch_iir & SDE_AUDIO_HDCP_MASK)
1942 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1943
1944 if (pch_iir & SDE_AUDIO_TRANS_MASK)
1945 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1946
1947 if (pch_iir & SDE_POISON)
1948 DRM_ERROR("PCH poison interrupt\n");
1949
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001950 if (pch_iir & SDE_FDI_MASK)
Damien Lespiau055e3932014-08-18 13:49:10 +01001951 for_each_pipe(dev_priv, pipe)
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001952 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1953 pipe_name(pipe),
1954 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -08001955
1956 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1957 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1958
1959 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1960 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1961
Jesse Barnes776ad802011-01-04 15:09:39 -08001962 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02001963 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
Paulo Zanoni86642812013-04-12 17:57:57 -03001964
1965 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02001966 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
Paulo Zanoni86642812013-04-12 17:57:57 -03001967}
1968
1969static void ivb_err_int_handler(struct drm_device *dev)
1970{
1971 struct drm_i915_private *dev_priv = dev->dev_private;
1972 u32 err_int = I915_READ(GEN7_ERR_INT);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001973 enum pipe pipe;
Paulo Zanoni86642812013-04-12 17:57:57 -03001974
Paulo Zanonide032bf2013-04-12 17:57:58 -03001975 if (err_int & ERR_INT_POISON)
1976 DRM_ERROR("Poison interrupt\n");
1977
Damien Lespiau055e3932014-08-18 13:49:10 +01001978 for_each_pipe(dev_priv, pipe) {
Daniel Vetter1f7247c2014-09-30 10:56:48 +02001979 if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
1980 intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
Paulo Zanoni86642812013-04-12 17:57:57 -03001981
Daniel Vetter5a69b892013-10-16 22:55:52 +02001982 if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
1983 if (IS_IVYBRIDGE(dev))
Daniel Vetter277de952013-10-18 16:37:07 +02001984 ivb_pipe_crc_irq_handler(dev, pipe);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001985 else
Daniel Vetter277de952013-10-18 16:37:07 +02001986 hsw_pipe_crc_irq_handler(dev, pipe);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001987 }
1988 }
Shuang He8bf1e9f2013-10-15 18:55:27 +01001989
Paulo Zanoni86642812013-04-12 17:57:57 -03001990 I915_WRITE(GEN7_ERR_INT, err_int);
1991}
1992
1993static void cpt_serr_int_handler(struct drm_device *dev)
1994{
1995 struct drm_i915_private *dev_priv = dev->dev_private;
1996 u32 serr_int = I915_READ(SERR_INT);
1997
Paulo Zanonide032bf2013-04-12 17:57:58 -03001998 if (serr_int & SERR_INT_POISON)
1999 DRM_ERROR("PCH poison interrupt\n");
2000
Paulo Zanoni86642812013-04-12 17:57:57 -03002001 if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002002 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
Paulo Zanoni86642812013-04-12 17:57:57 -03002003
2004 if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002005 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
Paulo Zanoni86642812013-04-12 17:57:57 -03002006
2007 if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002008 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
Paulo Zanoni86642812013-04-12 17:57:57 -03002009
2010 I915_WRITE(SERR_INT, serr_int);
Jesse Barnes776ad802011-01-04 15:09:39 -08002011}
2012
Adam Jackson23e81d62012-06-06 15:45:44 -04002013static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
2014{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002015 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson23e81d62012-06-06 15:45:44 -04002016 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02002017 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
Dave Airlie13cf5502014-06-18 11:29:35 +10002018 u32 dig_hotplug_reg;
Adam Jackson23e81d62012-06-06 15:45:44 -04002019
Dave Airlie13cf5502014-06-18 11:29:35 +10002020 dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2021 I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2022
2023 intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
Daniel Vetter91d131d2013-06-27 17:52:14 +02002024
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03002025 if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2026 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
2027 SDE_AUDIO_POWER_SHIFT_CPT);
2028 DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2029 port_name(port));
2030 }
Adam Jackson23e81d62012-06-06 15:45:44 -04002031
2032 if (pch_iir & SDE_AUX_MASK_CPT)
Daniel Vetterce99c252012-12-01 13:53:47 +01002033 dp_aux_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04002034
2035 if (pch_iir & SDE_GMBUS_CPT)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01002036 gmbus_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04002037
2038 if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
2039 DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
2040
2041 if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
2042 DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
2043
2044 if (pch_iir & SDE_FDI_MASK_CPT)
Damien Lespiau055e3932014-08-18 13:49:10 +01002045 for_each_pipe(dev_priv, pipe)
Adam Jackson23e81d62012-06-06 15:45:44 -04002046 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
2047 pipe_name(pipe),
2048 I915_READ(FDI_RX_IIR(pipe)));
Paulo Zanoni86642812013-04-12 17:57:57 -03002049
2050 if (pch_iir & SDE_ERROR_CPT)
2051 cpt_serr_int_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04002052}
2053
Paulo Zanonic008bc62013-07-12 16:35:10 -03002054static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2055{
2056 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter40da17c2013-10-21 18:04:36 +02002057 enum pipe pipe;
Paulo Zanonic008bc62013-07-12 16:35:10 -03002058
2059 if (de_iir & DE_AUX_CHANNEL_A)
2060 dp_aux_irq_handler(dev);
2061
2062 if (de_iir & DE_GSE)
2063 intel_opregion_asle_intr(dev);
2064
Paulo Zanonic008bc62013-07-12 16:35:10 -03002065 if (de_iir & DE_POISON)
2066 DRM_ERROR("Poison interrupt\n");
2067
Damien Lespiau055e3932014-08-18 13:49:10 +01002068 for_each_pipe(dev_priv, pipe) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01002069 if (de_iir & DE_PIPE_VBLANK(pipe) &&
2070 intel_pipe_handle_vblank(dev, pipe))
2071 intel_check_page_flip(dev, pipe);
Paulo Zanonic008bc62013-07-12 16:35:10 -03002072
Daniel Vetter40da17c2013-10-21 18:04:36 +02002073 if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002074 intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
Paulo Zanonic008bc62013-07-12 16:35:10 -03002075
Daniel Vetter40da17c2013-10-21 18:04:36 +02002076 if (de_iir & DE_PIPE_CRC_DONE(pipe))
2077 i9xx_pipe_crc_irq_handler(dev, pipe);
Daniel Vetter5b3a8562013-10-16 22:55:48 +02002078
Daniel Vetter40da17c2013-10-21 18:04:36 +02002079 /* plane/pipes map 1:1 on ilk+ */
2080 if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
2081 intel_prepare_page_flip(dev, pipe);
2082 intel_finish_page_flip_plane(dev, pipe);
2083 }
Paulo Zanonic008bc62013-07-12 16:35:10 -03002084 }
2085
2086 /* check event from PCH */
2087 if (de_iir & DE_PCH_EVENT) {
2088 u32 pch_iir = I915_READ(SDEIIR);
2089
2090 if (HAS_PCH_CPT(dev))
2091 cpt_irq_handler(dev, pch_iir);
2092 else
2093 ibx_irq_handler(dev, pch_iir);
2094
2095 /* should clear PCH hotplug event before clear CPU irq */
2096 I915_WRITE(SDEIIR, pch_iir);
2097 }
2098
2099 if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2100 ironlake_rps_change_irq_handler(dev);
2101}
2102
Paulo Zanoni9719fb92013-07-12 16:35:11 -03002103static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
2104{
2105 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00002106 enum pipe pipe;
Paulo Zanoni9719fb92013-07-12 16:35:11 -03002107
2108 if (de_iir & DE_ERR_INT_IVB)
2109 ivb_err_int_handler(dev);
2110
2111 if (de_iir & DE_AUX_CHANNEL_A_IVB)
2112 dp_aux_irq_handler(dev);
2113
2114 if (de_iir & DE_GSE_IVB)
2115 intel_opregion_asle_intr(dev);
2116
Damien Lespiau055e3932014-08-18 13:49:10 +01002117 for_each_pipe(dev_priv, pipe) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01002118 if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2119 intel_pipe_handle_vblank(dev, pipe))
2120 intel_check_page_flip(dev, pipe);
Daniel Vetter40da17c2013-10-21 18:04:36 +02002121
2122 /* plane/pipes map 1:1 on ilk+ */
Damien Lespiau07d27e22014-03-03 17:31:46 +00002123 if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
2124 intel_prepare_page_flip(dev, pipe);
2125 intel_finish_page_flip_plane(dev, pipe);
Paulo Zanoni9719fb92013-07-12 16:35:11 -03002126 }
2127 }
2128
2129 /* check event from PCH */
2130 if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
2131 u32 pch_iir = I915_READ(SDEIIR);
2132
2133 cpt_irq_handler(dev, pch_iir);
2134
2135 /* clear PCH hotplug event before clear CPU irq */
2136 I915_WRITE(SDEIIR, pch_iir);
2137 }
2138}
2139
Oscar Mateo72c90f62014-06-16 16:10:57 +01002140/*
2141 * To handle irqs with the minimum potential races with fresh interrupts, we:
2142 * 1 - Disable Master Interrupt Control.
2143 * 2 - Find the source(s) of the interrupt.
2144 * 3 - Clear the Interrupt Identity bits (IIR).
2145 * 4 - Process the interrupt(s) that had bits set in the IIRs.
2146 * 5 - Re-enable Master Interrupt Control.
2147 */
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002148static irqreturn_t ironlake_irq_handler(int irq, void *arg)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002149{
Daniel Vetter45a83f82014-05-12 19:17:55 +02002150 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03002151 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002152 u32 de_iir, gt_iir, de_ier, sde_ier = 0;
Chris Wilson0e434062012-05-09 21:45:44 +01002153 irqreturn_t ret = IRQ_NONE;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002154
Paulo Zanoni86642812013-04-12 17:57:57 -03002155 /* We get interrupts on unclaimed registers, so check for this before we
2156 * do any I915_{READ,WRITE}. */
Chris Wilson907b28c2013-07-19 20:36:52 +01002157 intel_uncore_check_errors(dev);
Paulo Zanoni86642812013-04-12 17:57:57 -03002158
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002159 /* disable master interrupt before clearing iir */
2160 de_ier = I915_READ(DEIER);
2161 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Paulo Zanoni23a78512013-07-12 16:35:14 -03002162 POSTING_READ(DEIER);
Chris Wilson0e434062012-05-09 21:45:44 +01002163
Paulo Zanoni44498ae2013-02-22 17:05:28 -03002164 /* Disable south interrupts. We'll only write to SDEIIR once, so further
2165 * interrupts will will be stored on its back queue, and then we'll be
2166 * able to process them after we restore SDEIER (as soon as we restore
2167 * it, we'll get an interrupt if SDEIIR still has something to process
2168 * due to its back queue). */
Ben Widawskyab5c6082013-04-05 13:12:41 -07002169 if (!HAS_PCH_NOP(dev)) {
2170 sde_ier = I915_READ(SDEIER);
2171 I915_WRITE(SDEIER, 0);
2172 POSTING_READ(SDEIER);
2173 }
Paulo Zanoni44498ae2013-02-22 17:05:28 -03002174
Oscar Mateo72c90f62014-06-16 16:10:57 +01002175 /* Find, clear, then process each source of interrupt */
2176
Chris Wilson0e434062012-05-09 21:45:44 +01002177 gt_iir = I915_READ(GTIIR);
2178 if (gt_iir) {
Oscar Mateo72c90f62014-06-16 16:10:57 +01002179 I915_WRITE(GTIIR, gt_iir);
2180 ret = IRQ_HANDLED;
Paulo Zanonid8fc8a42013-07-19 18:57:55 -03002181 if (INTEL_INFO(dev)->gen >= 6)
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002182 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Paulo Zanonid8fc8a42013-07-19 18:57:55 -03002183 else
2184 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01002185 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002186
2187 de_iir = I915_READ(DEIIR);
Chris Wilson0e434062012-05-09 21:45:44 +01002188 if (de_iir) {
Oscar Mateo72c90f62014-06-16 16:10:57 +01002189 I915_WRITE(DEIIR, de_iir);
2190 ret = IRQ_HANDLED;
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002191 if (INTEL_INFO(dev)->gen >= 7)
2192 ivb_display_irq_handler(dev, de_iir);
2193 else
2194 ilk_display_irq_handler(dev, de_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01002195 }
2196
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002197 if (INTEL_INFO(dev)->gen >= 6) {
2198 u32 pm_iir = I915_READ(GEN6_PMIIR);
2199 if (pm_iir) {
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002200 I915_WRITE(GEN6_PMIIR, pm_iir);
2201 ret = IRQ_HANDLED;
Oscar Mateo72c90f62014-06-16 16:10:57 +01002202 gen6_rps_irq_handler(dev_priv, pm_iir);
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002203 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002204 }
2205
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002206 I915_WRITE(DEIER, de_ier);
2207 POSTING_READ(DEIER);
Ben Widawskyab5c6082013-04-05 13:12:41 -07002208 if (!HAS_PCH_NOP(dev)) {
2209 I915_WRITE(SDEIER, sde_ier);
2210 POSTING_READ(SDEIER);
2211 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002212
2213 return ret;
2214}
2215
Ben Widawskyabd58f02013-11-02 21:07:09 -07002216static irqreturn_t gen8_irq_handler(int irq, void *arg)
2217{
2218 struct drm_device *dev = arg;
2219 struct drm_i915_private *dev_priv = dev->dev_private;
2220 u32 master_ctl;
2221 irqreturn_t ret = IRQ_NONE;
2222 uint32_t tmp = 0;
Daniel Vetterc42664c2013-11-07 11:05:40 +01002223 enum pipe pipe;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002224
Ben Widawskyabd58f02013-11-02 21:07:09 -07002225 master_ctl = I915_READ(GEN8_MASTER_IRQ);
2226 master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2227 if (!master_ctl)
2228 return IRQ_NONE;
2229
2230 I915_WRITE(GEN8_MASTER_IRQ, 0);
2231 POSTING_READ(GEN8_MASTER_IRQ);
2232
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002233 /* Find, clear, then process each source of interrupt */
2234
Ben Widawskyabd58f02013-11-02 21:07:09 -07002235 ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2236
2237 if (master_ctl & GEN8_DE_MISC_IRQ) {
2238 tmp = I915_READ(GEN8_DE_MISC_IIR);
Ben Widawskyabd58f02013-11-02 21:07:09 -07002239 if (tmp) {
2240 I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2241 ret = IRQ_HANDLED;
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002242 if (tmp & GEN8_DE_MISC_GSE)
2243 intel_opregion_asle_intr(dev);
2244 else
2245 DRM_ERROR("Unexpected DE Misc interrupt\n");
Ben Widawskyabd58f02013-11-02 21:07:09 -07002246 }
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002247 else
2248 DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
Ben Widawskyabd58f02013-11-02 21:07:09 -07002249 }
2250
Daniel Vetter6d766f02013-11-07 14:49:55 +01002251 if (master_ctl & GEN8_DE_PORT_IRQ) {
2252 tmp = I915_READ(GEN8_DE_PORT_IIR);
Daniel Vetter6d766f02013-11-07 14:49:55 +01002253 if (tmp) {
2254 I915_WRITE(GEN8_DE_PORT_IIR, tmp);
2255 ret = IRQ_HANDLED;
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002256 if (tmp & GEN8_AUX_CHANNEL_A)
2257 dp_aux_irq_handler(dev);
2258 else
2259 DRM_ERROR("Unexpected DE Port interrupt\n");
Daniel Vetter6d766f02013-11-07 14:49:55 +01002260 }
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002261 else
2262 DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
Daniel Vetter6d766f02013-11-07 14:49:55 +01002263 }
2264
Damien Lespiau055e3932014-08-18 13:49:10 +01002265 for_each_pipe(dev_priv, pipe) {
Damien Lespiau770de832014-03-20 20:45:01 +00002266 uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002267
Daniel Vetterc42664c2013-11-07 11:05:40 +01002268 if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2269 continue;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002270
Daniel Vetterc42664c2013-11-07 11:05:40 +01002271 pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
Daniel Vetterc42664c2013-11-07 11:05:40 +01002272 if (pipe_iir) {
2273 ret = IRQ_HANDLED;
2274 I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
Damien Lespiau770de832014-03-20 20:45:01 +00002275
Chris Wilsond6bbafa2014-09-05 07:13:24 +01002276 if (pipe_iir & GEN8_PIPE_VBLANK &&
2277 intel_pipe_handle_vblank(dev, pipe))
2278 intel_check_page_flip(dev, pipe);
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002279
Damien Lespiau770de832014-03-20 20:45:01 +00002280 if (IS_GEN9(dev))
2281 flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2282 else
2283 flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2284
2285 if (flip_done) {
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002286 intel_prepare_page_flip(dev, pipe);
2287 intel_finish_page_flip_plane(dev, pipe);
2288 }
2289
2290 if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
2291 hsw_pipe_crc_irq_handler(dev, pipe);
2292
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002293 if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
2294 intel_cpu_fifo_underrun_irq_handler(dev_priv,
2295 pipe);
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002296
Damien Lespiau770de832014-03-20 20:45:01 +00002297
2298 if (IS_GEN9(dev))
2299 fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2300 else
2301 fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2302
2303 if (fault_errors)
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002304 DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
2305 pipe_name(pipe),
2306 pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
Daniel Vetterc42664c2013-11-07 11:05:40 +01002307 } else
Ben Widawskyabd58f02013-11-02 21:07:09 -07002308 DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2309 }
2310
Daniel Vetter92d03a82013-11-07 11:05:43 +01002311 if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
2312 /*
2313 * FIXME(BDW): Assume for now that the new interrupt handling
2314 * scheme also closed the SDE interrupt handling race we've seen
2315 * on older pch-split platforms. But this needs testing.
2316 */
2317 u32 pch_iir = I915_READ(SDEIIR);
Daniel Vetter92d03a82013-11-07 11:05:43 +01002318 if (pch_iir) {
2319 I915_WRITE(SDEIIR, pch_iir);
2320 ret = IRQ_HANDLED;
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002321 cpt_irq_handler(dev, pch_iir);
2322 } else
2323 DRM_ERROR("The master control interrupt lied (SDE)!\n");
2324
Daniel Vetter92d03a82013-11-07 11:05:43 +01002325 }
2326
Ben Widawskyabd58f02013-11-02 21:07:09 -07002327 I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2328 POSTING_READ(GEN8_MASTER_IRQ);
2329
2330 return ret;
2331}
2332
Daniel Vetter17e1df02013-09-08 21:57:13 +02002333static void i915_error_wake_up(struct drm_i915_private *dev_priv,
2334 bool reset_completed)
2335{
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002336 struct intel_engine_cs *ring;
Daniel Vetter17e1df02013-09-08 21:57:13 +02002337 int i;
2338
2339 /*
2340 * Notify all waiters for GPU completion events that reset state has
2341 * been changed, and that they need to restart their wait after
2342 * checking for potential errors (and bail out to drop locks if there is
2343 * a gpu reset pending so that i915_error_work_func can acquire them).
2344 */
2345
2346 /* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
2347 for_each_ring(ring, dev_priv, i)
2348 wake_up_all(&ring->irq_queue);
2349
2350 /* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
2351 wake_up_all(&dev_priv->pending_flip_queue);
2352
2353 /*
2354 * Signal tasks blocked in i915_gem_wait_for_error that the pending
2355 * reset state is cleared.
2356 */
2357 if (reset_completed)
2358 wake_up_all(&dev_priv->gpu_error.reset_queue);
2359}
2360
Jesse Barnes8a905232009-07-11 16:48:03 -04002361/**
2362 * i915_error_work_func - do process context error handling work
2363 * @work: work struct
2364 *
2365 * Fire an error uevent so userspace can see that a hang or error
2366 * was detected.
2367 */
2368static void i915_error_work_func(struct work_struct *work)
2369{
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002370 struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
2371 work);
Jani Nikula2d1013d2014-03-31 14:27:17 +03002372 struct drm_i915_private *dev_priv =
2373 container_of(error, struct drm_i915_private, gpu_error);
Jesse Barnes8a905232009-07-11 16:48:03 -04002374 struct drm_device *dev = dev_priv->dev;
Ben Widawskycce723e2013-07-19 09:16:42 -07002375 char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2376 char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2377 char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
Daniel Vetter17e1df02013-09-08 21:57:13 +02002378 int ret;
Jesse Barnes8a905232009-07-11 16:48:03 -04002379
Dave Airlie5bdebb12013-10-11 14:07:25 +10002380 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -04002381
Daniel Vetter7db0ba22012-12-06 16:23:37 +01002382 /*
2383 * Note that there's only one work item which does gpu resets, so we
2384 * need not worry about concurrent gpu resets potentially incrementing
2385 * error->reset_counter twice. We only need to take care of another
2386 * racing irq/hangcheck declaring the gpu dead for a second time. A
2387 * quick check for that is good enough: schedule_work ensures the
2388 * correct ordering between hang detection and this work item, and since
2389 * the reset in-progress bit is only ever set by code outside of this
2390 * work we don't need to worry about any other races.
2391 */
2392 if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +01002393 DRM_DEBUG_DRIVER("resetting chip\n");
Dave Airlie5bdebb12013-10-11 14:07:25 +10002394 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
Daniel Vetter7db0ba22012-12-06 16:23:37 +01002395 reset_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002396
Daniel Vetter17e1df02013-09-08 21:57:13 +02002397 /*
Imre Deakf454c692014-04-23 01:09:04 +03002398 * In most cases it's guaranteed that we get here with an RPM
2399 * reference held, for example because there is a pending GPU
2400 * request that won't finish until the reset is done. This
2401 * isn't the case at least when we get here by doing a
2402 * simulated reset via debugs, so get an RPM reference.
2403 */
2404 intel_runtime_pm_get(dev_priv);
2405 /*
Daniel Vetter17e1df02013-09-08 21:57:13 +02002406 * All state reset _must_ be completed before we update the
2407 * reset counter, for otherwise waiters might miss the reset
2408 * pending state and not properly drop locks, resulting in
2409 * deadlocks with the reset work.
2410 */
Daniel Vetterf69061b2012-12-06 09:01:42 +01002411 ret = i915_reset(dev);
2412
Daniel Vetter17e1df02013-09-08 21:57:13 +02002413 intel_display_handle_reset(dev);
2414
Imre Deakf454c692014-04-23 01:09:04 +03002415 intel_runtime_pm_put(dev_priv);
2416
Daniel Vetterf69061b2012-12-06 09:01:42 +01002417 if (ret == 0) {
2418 /*
2419 * After all the gem state is reset, increment the reset
2420 * counter and wake up everyone waiting for the reset to
2421 * complete.
2422 *
2423 * Since unlock operations are a one-sided barrier only,
2424 * we need to insert a barrier here to order any seqno
2425 * updates before
2426 * the counter increment.
2427 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002428 smp_mb__before_atomic();
Daniel Vetterf69061b2012-12-06 09:01:42 +01002429 atomic_inc(&dev_priv->gpu_error.reset_counter);
2430
Dave Airlie5bdebb12013-10-11 14:07:25 +10002431 kobject_uevent_env(&dev->primary->kdev->kobj,
Daniel Vetterf69061b2012-12-06 09:01:42 +01002432 KOBJ_CHANGE, reset_done_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002433 } else {
Mika Kuoppala2ac0f452013-11-12 14:44:19 +02002434 atomic_set_mask(I915_WEDGED, &error->reset_counter);
Ben Gamarif316a422009-09-14 17:48:46 -04002435 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002436
Daniel Vetter17e1df02013-09-08 21:57:13 +02002437 /*
2438 * Note: The wake_up also serves as a memory barrier so that
2439 * waiters see the update value of the reset counter atomic_t.
2440 */
2441 i915_error_wake_up(dev_priv, true);
Ben Gamarif316a422009-09-14 17:48:46 -04002442 }
Jesse Barnes8a905232009-07-11 16:48:03 -04002443}
2444
Chris Wilson35aed2e2010-05-27 13:18:12 +01002445static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04002446{
2447 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybd9854f2012-08-23 15:18:09 -07002448 uint32_t instdone[I915_NUM_INSTDONE_REG];
Jesse Barnes8a905232009-07-11 16:48:03 -04002449 u32 eir = I915_READ(EIR);
Ben Widawsky050ee912012-08-22 11:32:15 -07002450 int pipe, i;
Jesse Barnes8a905232009-07-11 16:48:03 -04002451
Chris Wilson35aed2e2010-05-27 13:18:12 +01002452 if (!eir)
2453 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04002454
Joe Perchesa70491c2012-03-18 13:00:11 -07002455 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04002456
Ben Widawskybd9854f2012-08-23 15:18:09 -07002457 i915_get_extra_instdone(dev, instdone);
2458
Jesse Barnes8a905232009-07-11 16:48:03 -04002459 if (IS_G4X(dev)) {
2460 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
2461 u32 ipeir = I915_READ(IPEIR_I965);
2462
Joe Perchesa70491c2012-03-18 13:00:11 -07002463 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2464 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Ben Widawsky050ee912012-08-22 11:32:15 -07002465 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2466 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Joe Perchesa70491c2012-03-18 13:00:11 -07002467 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002468 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002469 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002470 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002471 }
2472 if (eir & GM45_ERROR_PAGE_TABLE) {
2473 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002474 pr_err("page table error\n");
2475 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002476 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002477 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002478 }
2479 }
2480
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002481 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002482 if (eir & I915_ERROR_PAGE_TABLE) {
2483 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002484 pr_err("page table error\n");
2485 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002486 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002487 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002488 }
2489 }
2490
2491 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002492 pr_err("memory refresh error:\n");
Damien Lespiau055e3932014-08-18 13:49:10 +01002493 for_each_pipe(dev_priv, pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07002494 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002495 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04002496 /* pipestat has already been acked */
2497 }
2498 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002499 pr_err("instruction error\n");
2500 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Ben Widawsky050ee912012-08-22 11:32:15 -07002501 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2502 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002503 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002504 u32 ipeir = I915_READ(IPEIR);
2505
Joe Perchesa70491c2012-03-18 13:00:11 -07002506 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
2507 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
Joe Perchesa70491c2012-03-18 13:00:11 -07002508 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04002509 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002510 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002511 } else {
2512 u32 ipeir = I915_READ(IPEIR_I965);
2513
Joe Perchesa70491c2012-03-18 13:00:11 -07002514 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2515 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07002516 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002517 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002518 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002519 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002520 }
2521 }
2522
2523 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002524 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002525 eir = I915_READ(EIR);
2526 if (eir) {
2527 /*
2528 * some errors might have become stuck,
2529 * mask them.
2530 */
2531 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
2532 I915_WRITE(EMR, I915_READ(EMR) | eir);
2533 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2534 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01002535}
2536
2537/**
2538 * i915_handle_error - handle an error interrupt
2539 * @dev: drm device
2540 *
2541 * Do some basic checking of regsiter state at error interrupt time and
2542 * dump it to the syslog. Also call i915_capture_error_state() to make
2543 * sure we get a record and make it available in debugfs. Fire a uevent
2544 * so userspace knows something bad happened (should trigger collection
2545 * of a ring dump etc.).
2546 */
Mika Kuoppala58174462014-02-25 17:11:26 +02002547void i915_handle_error(struct drm_device *dev, bool wedged,
2548 const char *fmt, ...)
Chris Wilson35aed2e2010-05-27 13:18:12 +01002549{
2550 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala58174462014-02-25 17:11:26 +02002551 va_list args;
2552 char error_msg[80];
Chris Wilson35aed2e2010-05-27 13:18:12 +01002553
Mika Kuoppala58174462014-02-25 17:11:26 +02002554 va_start(args, fmt);
2555 vscnprintf(error_msg, sizeof(error_msg), fmt, args);
2556 va_end(args);
2557
2558 i915_capture_error_state(dev, wedged, error_msg);
Chris Wilson35aed2e2010-05-27 13:18:12 +01002559 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04002560
Ben Gamariba1234d2009-09-14 17:48:47 -04002561 if (wedged) {
Daniel Vetterf69061b2012-12-06 09:01:42 +01002562 atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2563 &dev_priv->gpu_error.reset_counter);
Ben Gamariba1234d2009-09-14 17:48:47 -04002564
Ben Gamari11ed50e2009-09-14 17:48:45 -04002565 /*
Daniel Vetter17e1df02013-09-08 21:57:13 +02002566 * Wakeup waiting processes so that the reset work function
2567 * i915_error_work_func doesn't deadlock trying to grab various
2568 * locks. By bumping the reset counter first, the woken
2569 * processes will see a reset in progress and back off,
2570 * releasing their locks and then wait for the reset completion.
2571 * We must do this for _all_ gpu waiters that might hold locks
2572 * that the reset work needs to acquire.
2573 *
2574 * Note: The wake_up serves as the required memory barrier to
2575 * ensure that the waiters see the updated value of the reset
2576 * counter atomic_t.
Ben Gamari11ed50e2009-09-14 17:48:45 -04002577 */
Daniel Vetter17e1df02013-09-08 21:57:13 +02002578 i915_error_wake_up(dev_priv, false);
Ben Gamari11ed50e2009-09-14 17:48:45 -04002579 }
2580
Daniel Vetter122f46b2013-09-04 17:36:14 +02002581 /*
2582 * Our reset work can grab modeset locks (since it needs to reset the
2583 * state of outstanding pagelips). Hence it must not be run on our own
2584 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2585 * code will deadlock.
2586 */
2587 schedule_work(&dev_priv->gpu_error.work);
Jesse Barnes8a905232009-07-11 16:48:03 -04002588}
2589
Keith Packard42f52ef2008-10-18 19:39:29 -07002590/* Called from drm generic code, passed 'crtc' which
2591 * we use as a pipe index
2592 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002593static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002594{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002595 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002596 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002597
Chris Wilson5eddb702010-09-11 13:48:45 +01002598 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002599 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002600
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002601 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002602 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08002603 i915_enable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002604 PIPE_START_VBLANK_INTERRUPT_STATUS);
Keith Packarde9d21d72008-10-16 11:31:38 -07002605 else
Keith Packard7c463582008-11-04 02:03:27 -08002606 i915_enable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002607 PIPE_VBLANK_INTERRUPT_STATUS);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002608 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002609
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002610 return 0;
2611}
2612
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002613static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002614{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002615 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf796cf82011-04-07 13:58:17 -07002616 unsigned long irqflags;
Paulo Zanonib5184212013-07-12 20:00:08 -03002617 uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
Daniel Vetter40da17c2013-10-21 18:04:36 +02002618 DE_PIPE_VBLANK(pipe);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002619
2620 if (!i915_pipe_enabled(dev, pipe))
2621 return -EINVAL;
2622
2623 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Paulo Zanonib5184212013-07-12 20:00:08 -03002624 ironlake_enable_display_irq(dev_priv, bit);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002625 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2626
2627 return 0;
2628}
2629
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002630static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
2631{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002632 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002633 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002634
2635 if (!i915_pipe_enabled(dev, pipe))
2636 return -EINVAL;
2637
2638 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002639 i915_enable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002640 PIPE_START_VBLANK_INTERRUPT_STATUS);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002641 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2642
2643 return 0;
2644}
2645
Ben Widawskyabd58f02013-11-02 21:07:09 -07002646static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2647{
2648 struct drm_i915_private *dev_priv = dev->dev_private;
2649 unsigned long irqflags;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002650
2651 if (!i915_pipe_enabled(dev, pipe))
2652 return -EINVAL;
2653
2654 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter7167d7c2013-11-07 11:05:45 +01002655 dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
2656 I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2657 POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
Ben Widawskyabd58f02013-11-02 21:07:09 -07002658 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2659 return 0;
2660}
2661
Keith Packard42f52ef2008-10-18 19:39:29 -07002662/* Called from drm generic code, passed 'crtc' which
2663 * we use as a pipe index
2664 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002665static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002666{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002667 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002668 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002669
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002670 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002671 i915_disable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002672 PIPE_VBLANK_INTERRUPT_STATUS |
2673 PIPE_START_VBLANK_INTERRUPT_STATUS);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002674 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2675}
2676
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002677static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002678{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002679 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf796cf82011-04-07 13:58:17 -07002680 unsigned long irqflags;
Paulo Zanonib5184212013-07-12 20:00:08 -03002681 uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
Daniel Vetter40da17c2013-10-21 18:04:36 +02002682 DE_PIPE_VBLANK(pipe);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002683
2684 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Paulo Zanonib5184212013-07-12 20:00:08 -03002685 ironlake_disable_display_irq(dev_priv, bit);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002686 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2687}
2688
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002689static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
2690{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002691 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002692 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002693
2694 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002695 i915_disable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002696 PIPE_START_VBLANK_INTERRUPT_STATUS);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002697 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2698}
2699
Ben Widawskyabd58f02013-11-02 21:07:09 -07002700static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2701{
2702 struct drm_i915_private *dev_priv = dev->dev_private;
2703 unsigned long irqflags;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002704
2705 if (!i915_pipe_enabled(dev, pipe))
2706 return;
2707
2708 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter7167d7c2013-11-07 11:05:45 +01002709 dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
2710 I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2711 POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
Ben Widawskyabd58f02013-11-02 21:07:09 -07002712 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2713}
2714
Chris Wilson893eead2010-10-27 14:44:35 +01002715static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002716ring_last_seqno(struct intel_engine_cs *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08002717{
Chris Wilson893eead2010-10-27 14:44:35 +01002718 return list_entry(ring->request_list.prev,
2719 struct drm_i915_gem_request, list)->seqno;
2720}
2721
Chris Wilson9107e9d2013-06-10 11:20:20 +01002722static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002723ring_idle(struct intel_engine_cs *ring, u32 seqno)
Chris Wilson893eead2010-10-27 14:44:35 +01002724{
Chris Wilson9107e9d2013-06-10 11:20:20 +01002725 return (list_empty(&ring->request_list) ||
2726 i915_seqno_passed(seqno, ring_last_seqno(ring)));
Ben Gamarif65d9422009-09-14 17:48:44 -04002727}
2728
Daniel Vettera028c4b2014-03-15 00:08:56 +01002729static bool
2730ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2731{
2732 if (INTEL_INFO(dev)->gen >= 8) {
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002733 return (ipehr >> 23) == 0x1c;
Daniel Vettera028c4b2014-03-15 00:08:56 +01002734 } else {
2735 ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2736 return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2737 MI_SEMAPHORE_REGISTER);
2738 }
2739}
2740
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002741static struct intel_engine_cs *
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002742semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
Daniel Vetter921d42e2014-03-18 10:26:04 +01002743{
2744 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002745 struct intel_engine_cs *signaller;
Daniel Vetter921d42e2014-03-18 10:26:04 +01002746 int i;
2747
2748 if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002749 for_each_ring(signaller, dev_priv, i) {
2750 if (ring == signaller)
2751 continue;
2752
2753 if (offset == signaller->semaphore.signal_ggtt[ring->id])
2754 return signaller;
2755 }
Daniel Vetter921d42e2014-03-18 10:26:04 +01002756 } else {
2757 u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2758
2759 for_each_ring(signaller, dev_priv, i) {
2760 if(ring == signaller)
2761 continue;
2762
Ben Widawskyebc348b2014-04-29 14:52:28 -07002763 if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
Daniel Vetter921d42e2014-03-18 10:26:04 +01002764 return signaller;
2765 }
2766 }
2767
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002768 DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2769 ring->id, ipehr, offset);
Daniel Vetter921d42e2014-03-18 10:26:04 +01002770
2771 return NULL;
2772}
2773
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002774static struct intel_engine_cs *
2775semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
Chris Wilsona24a11e2013-03-14 17:52:05 +02002776{
2777 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Daniel Vetter88fe4292014-03-15 00:08:55 +01002778 u32 cmd, ipehr, head;
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002779 u64 offset = 0;
2780 int i, backwards;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002781
2782 ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
Daniel Vettera028c4b2014-03-15 00:08:56 +01002783 if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
Chris Wilson6274f212013-06-10 11:20:21 +01002784 return NULL;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002785
Daniel Vetter88fe4292014-03-15 00:08:55 +01002786 /*
2787 * HEAD is likely pointing to the dword after the actual command,
2788 * so scan backwards until we find the MBOX. But limit it to just 3
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002789 * or 4 dwords depending on the semaphore wait command size.
2790 * Note that we don't care about ACTHD here since that might
Daniel Vetter88fe4292014-03-15 00:08:55 +01002791 * point at at batch, and semaphores are always emitted into the
2792 * ringbuffer itself.
Chris Wilsona24a11e2013-03-14 17:52:05 +02002793 */
Daniel Vetter88fe4292014-03-15 00:08:55 +01002794 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002795 backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
Daniel Vetter88fe4292014-03-15 00:08:55 +01002796
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002797 for (i = backwards; i; --i) {
Daniel Vetter88fe4292014-03-15 00:08:55 +01002798 /*
2799 * Be paranoid and presume the hw has gone off into the wild -
2800 * our ring is smaller than what the hardware (and hence
2801 * HEAD_ADDR) allows. Also handles wrap-around.
2802 */
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002803 head &= ring->buffer->size - 1;
Daniel Vetter88fe4292014-03-15 00:08:55 +01002804
2805 /* This here seems to blow up */
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002806 cmd = ioread32(ring->buffer->virtual_start + head);
Chris Wilsona24a11e2013-03-14 17:52:05 +02002807 if (cmd == ipehr)
2808 break;
2809
Daniel Vetter88fe4292014-03-15 00:08:55 +01002810 head -= 4;
2811 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02002812
Daniel Vetter88fe4292014-03-15 00:08:55 +01002813 if (!i)
2814 return NULL;
2815
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002816 *seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002817 if (INTEL_INFO(ring->dev)->gen >= 8) {
2818 offset = ioread32(ring->buffer->virtual_start + head + 12);
2819 offset <<= 32;
2820 offset = ioread32(ring->buffer->virtual_start + head + 8);
2821 }
2822 return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
Chris Wilsona24a11e2013-03-14 17:52:05 +02002823}
2824
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002825static int semaphore_passed(struct intel_engine_cs *ring)
Chris Wilson6274f212013-06-10 11:20:21 +01002826{
2827 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002828 struct intel_engine_cs *signaller;
Chris Wilsona0d036b2014-07-19 12:40:42 +01002829 u32 seqno;
Chris Wilson6274f212013-06-10 11:20:21 +01002830
Chris Wilson4be17382014-06-06 10:22:29 +01002831 ring->hangcheck.deadlock++;
Chris Wilson6274f212013-06-10 11:20:21 +01002832
2833 signaller = semaphore_waits_for(ring, &seqno);
Chris Wilson4be17382014-06-06 10:22:29 +01002834 if (signaller == NULL)
2835 return -1;
2836
2837 /* Prevent pathological recursion due to driver bugs */
2838 if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
Chris Wilson6274f212013-06-10 11:20:21 +01002839 return -1;
2840
Chris Wilson4be17382014-06-06 10:22:29 +01002841 if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
2842 return 1;
2843
Chris Wilsona0d036b2014-07-19 12:40:42 +01002844 /* cursory check for an unkickable deadlock */
2845 if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2846 semaphore_passed(signaller) < 0)
Chris Wilson4be17382014-06-06 10:22:29 +01002847 return -1;
2848
2849 return 0;
Chris Wilson6274f212013-06-10 11:20:21 +01002850}
2851
2852static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
2853{
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002854 struct intel_engine_cs *ring;
Chris Wilson6274f212013-06-10 11:20:21 +01002855 int i;
2856
2857 for_each_ring(ring, dev_priv, i)
Chris Wilson4be17382014-06-06 10:22:29 +01002858 ring->hangcheck.deadlock = 0;
Chris Wilson6274f212013-06-10 11:20:21 +01002859}
2860
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03002861static enum intel_ring_hangcheck_action
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002862ring_stuck(struct intel_engine_cs *ring, u64 acthd)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002863{
2864 struct drm_device *dev = ring->dev;
2865 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002866 u32 tmp;
2867
Mika Kuoppalaf260fe72014-08-05 17:16:26 +03002868 if (acthd != ring->hangcheck.acthd) {
2869 if (acthd > ring->hangcheck.max_acthd) {
2870 ring->hangcheck.max_acthd = acthd;
2871 return HANGCHECK_ACTIVE;
2872 }
2873
2874 return HANGCHECK_ACTIVE_LOOP;
2875 }
Chris Wilson6274f212013-06-10 11:20:21 +01002876
Chris Wilson9107e9d2013-06-10 11:20:20 +01002877 if (IS_GEN2(dev))
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002878 return HANGCHECK_HUNG;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002879
2880 /* Is the chip hanging on a WAIT_FOR_EVENT?
2881 * If so we can simply poke the RB_WAIT bit
2882 * and break the hang. This should work on
2883 * all but the second generation chipsets.
2884 */
2885 tmp = I915_READ_CTL(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002886 if (tmp & RING_WAIT) {
Mika Kuoppala58174462014-02-25 17:11:26 +02002887 i915_handle_error(dev, false,
2888 "Kicking stuck wait on %s",
2889 ring->name);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002890 I915_WRITE_CTL(ring, tmp);
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002891 return HANGCHECK_KICK;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002892 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02002893
Chris Wilson6274f212013-06-10 11:20:21 +01002894 if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
2895 switch (semaphore_passed(ring)) {
2896 default:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002897 return HANGCHECK_HUNG;
Chris Wilson6274f212013-06-10 11:20:21 +01002898 case 1:
Mika Kuoppala58174462014-02-25 17:11:26 +02002899 i915_handle_error(dev, false,
2900 "Kicking stuck semaphore on %s",
2901 ring->name);
Chris Wilson6274f212013-06-10 11:20:21 +01002902 I915_WRITE_CTL(ring, tmp);
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002903 return HANGCHECK_KICK;
Chris Wilson6274f212013-06-10 11:20:21 +01002904 case 0:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002905 return HANGCHECK_WAIT;
Chris Wilson6274f212013-06-10 11:20:21 +01002906 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002907 }
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002908
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002909 return HANGCHECK_HUNG;
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002910}
2911
Ben Gamarif65d9422009-09-14 17:48:44 -04002912/**
2913 * This is called when the chip hasn't reported back with completed
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002914 * batchbuffers in a long time. We keep track per ring seqno progress and
2915 * if there are no progress, hangcheck score for that ring is increased.
2916 * Further, acthd is inspected to see if the ring is stuck. On stuck case
2917 * we kick the ring. If we see no progress on three subsequent calls
2918 * we assume chip is wedged and try to fix it by resetting the chip.
Ben Gamarif65d9422009-09-14 17:48:44 -04002919 */
Damien Lespiaua658b5d2013-08-08 22:28:56 +01002920static void i915_hangcheck_elapsed(unsigned long data)
Ben Gamarif65d9422009-09-14 17:48:44 -04002921{
2922 struct drm_device *dev = (struct drm_device *)data;
Jani Nikula2d1013d2014-03-31 14:27:17 +03002923 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002924 struct intel_engine_cs *ring;
Chris Wilsonb4519512012-05-11 14:29:30 +01002925 int i;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002926 int busy_count = 0, rings_hung = 0;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002927 bool stuck[I915_NUM_RINGS] = { 0 };
2928#define BUSY 1
2929#define KICK 5
2930#define HUNG 20
Chris Wilson893eead2010-10-27 14:44:35 +01002931
Jani Nikulad330a952014-01-21 11:24:25 +02002932 if (!i915.enable_hangcheck)
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07002933 return;
2934
Chris Wilsonb4519512012-05-11 14:29:30 +01002935 for_each_ring(ring, dev_priv, i) {
Chris Wilson50877442014-03-21 12:41:53 +00002936 u64 acthd;
2937 u32 seqno;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002938 bool busy = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01002939
Chris Wilson6274f212013-06-10 11:20:21 +01002940 semaphore_clear_deadlocks(dev_priv);
2941
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002942 seqno = ring->get_seqno(ring, false);
2943 acthd = intel_ring_get_active_head(ring);
Chris Wilsond1e61e72012-04-10 17:00:41 +01002944
Chris Wilson9107e9d2013-06-10 11:20:20 +01002945 if (ring->hangcheck.seqno == seqno) {
2946 if (ring_idle(ring, seqno)) {
Mika Kuoppalada661462013-09-06 16:03:28 +03002947 ring->hangcheck.action = HANGCHECK_IDLE;
2948
Chris Wilson9107e9d2013-06-10 11:20:20 +01002949 if (waitqueue_active(&ring->irq_queue)) {
2950 /* Issue a wake-up to catch stuck h/w. */
Chris Wilson094f9a52013-09-25 17:34:55 +01002951 if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
Daniel Vetterf4adcd22013-10-28 09:24:13 +01002952 if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
2953 DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
2954 ring->name);
2955 else
2956 DRM_INFO("Fake missed irq on %s\n",
2957 ring->name);
Chris Wilson094f9a52013-09-25 17:34:55 +01002958 wake_up_all(&ring->irq_queue);
2959 }
2960 /* Safeguard against driver failure */
2961 ring->hangcheck.score += BUSY;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002962 } else
2963 busy = false;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002964 } else {
Chris Wilson6274f212013-06-10 11:20:21 +01002965 /* We always increment the hangcheck score
2966 * if the ring is busy and still processing
2967 * the same request, so that no single request
2968 * can run indefinitely (such as a chain of
2969 * batches). The only time we do not increment
2970 * the hangcheck score on this ring, if this
2971 * ring is in a legitimate wait for another
2972 * ring. In that case the waiting ring is a
2973 * victim and we want to be sure we catch the
2974 * right culprit. Then every time we do kick
2975 * the ring, add a small increment to the
2976 * score so that we can catch a batch that is
2977 * being repeatedly kicked and so responsible
2978 * for stalling the machine.
2979 */
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03002980 ring->hangcheck.action = ring_stuck(ring,
2981 acthd);
2982
2983 switch (ring->hangcheck.action) {
Mika Kuoppalada661462013-09-06 16:03:28 +03002984 case HANGCHECK_IDLE:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002985 case HANGCHECK_WAIT:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002986 case HANGCHECK_ACTIVE:
Mika Kuoppalaf260fe72014-08-05 17:16:26 +03002987 break;
2988 case HANGCHECK_ACTIVE_LOOP:
Jani Nikulaea04cb32013-08-11 12:44:02 +03002989 ring->hangcheck.score += BUSY;
Chris Wilson6274f212013-06-10 11:20:21 +01002990 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002991 case HANGCHECK_KICK:
Jani Nikulaea04cb32013-08-11 12:44:02 +03002992 ring->hangcheck.score += KICK;
Chris Wilson6274f212013-06-10 11:20:21 +01002993 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002994 case HANGCHECK_HUNG:
Jani Nikulaea04cb32013-08-11 12:44:02 +03002995 ring->hangcheck.score += HUNG;
Chris Wilson6274f212013-06-10 11:20:21 +01002996 stuck[i] = true;
2997 break;
2998 }
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002999 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01003000 } else {
Mika Kuoppalada661462013-09-06 16:03:28 +03003001 ring->hangcheck.action = HANGCHECK_ACTIVE;
3002
Chris Wilson9107e9d2013-06-10 11:20:20 +01003003 /* Gradually reduce the count so that we catch DoS
3004 * attempts across multiple batches.
3005 */
3006 if (ring->hangcheck.score > 0)
3007 ring->hangcheck.score--;
Mika Kuoppalaf260fe72014-08-05 17:16:26 +03003008
3009 ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
Chris Wilsond1e61e72012-04-10 17:00:41 +01003010 }
3011
Mika Kuoppala05407ff2013-05-30 09:04:29 +03003012 ring->hangcheck.seqno = seqno;
3013 ring->hangcheck.acthd = acthd;
Chris Wilson9107e9d2013-06-10 11:20:20 +01003014 busy_count += busy;
Chris Wilson893eead2010-10-27 14:44:35 +01003015 }
Eric Anholtb9201c12010-01-08 14:25:16 -08003016
Mika Kuoppala92cab732013-05-24 17:16:07 +03003017 for_each_ring(ring, dev_priv, i) {
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003018 if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
Daniel Vetterb8d88d12013-08-28 10:57:59 +02003019 DRM_INFO("%s on %s\n",
3020 stuck[i] ? "stuck" : "no progress",
3021 ring->name);
Chris Wilsona43adf02013-06-10 11:20:22 +01003022 rings_hung++;
Mika Kuoppala92cab732013-05-24 17:16:07 +03003023 }
3024 }
3025
Mika Kuoppala05407ff2013-05-30 09:04:29 +03003026 if (rings_hung)
Mika Kuoppala58174462014-02-25 17:11:26 +02003027 return i915_handle_error(dev, true, "Ring hung");
Ben Gamarif65d9422009-09-14 17:48:44 -04003028
Mika Kuoppala05407ff2013-05-30 09:04:29 +03003029 if (busy_count)
3030 /* Reset timer case chip hangs without another request
3031 * being added */
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03003032 i915_queue_hangcheck(dev);
3033}
3034
3035void i915_queue_hangcheck(struct drm_device *dev)
3036{
3037 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulad330a952014-01-21 11:24:25 +02003038 if (!i915.enable_hangcheck)
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03003039 return;
3040
3041 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
3042 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
Ben Gamarif65d9422009-09-14 17:48:44 -04003043}
3044
Paulo Zanoni1c69eb42014-04-01 15:37:23 -03003045static void ibx_irq_reset(struct drm_device *dev)
Paulo Zanoni91738a92013-06-05 14:21:51 -03003046{
3047 struct drm_i915_private *dev_priv = dev->dev_private;
3048
3049 if (HAS_PCH_NOP(dev))
3050 return;
3051
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003052 GEN5_IRQ_RESET(SDE);
Paulo Zanoni105b1222014-04-01 15:37:17 -03003053
3054 if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3055 I915_WRITE(SERR_INT, 0xffffffff);
Paulo Zanoni622364b2014-04-01 15:37:22 -03003056}
Paulo Zanoni105b1222014-04-01 15:37:17 -03003057
Paulo Zanoni622364b2014-04-01 15:37:22 -03003058/*
3059 * SDEIER is also touched by the interrupt handler to work around missed PCH
3060 * interrupts. Hence we can't update it after the interrupt handler is enabled -
3061 * instead we unconditionally enable all PCH interrupt sources here, but then
3062 * only unmask them as needed with SDEIMR.
3063 *
3064 * This function needs to be called before interrupts are enabled.
3065 */
3066static void ibx_irq_pre_postinstall(struct drm_device *dev)
3067{
3068 struct drm_i915_private *dev_priv = dev->dev_private;
3069
3070 if (HAS_PCH_NOP(dev))
3071 return;
3072
3073 WARN_ON(I915_READ(SDEIER) != 0);
Paulo Zanoni91738a92013-06-05 14:21:51 -03003074 I915_WRITE(SDEIER, 0xffffffff);
3075 POSTING_READ(SDEIER);
3076}
3077
Paulo Zanoni7c4d6642014-04-01 15:37:19 -03003078static void gen5_gt_irq_reset(struct drm_device *dev)
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02003079{
3080 struct drm_i915_private *dev_priv = dev->dev_private;
3081
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003082 GEN5_IRQ_RESET(GT);
Paulo Zanonia9d356a2014-04-01 15:37:09 -03003083 if (INTEL_INFO(dev)->gen >= 6)
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003084 GEN5_IRQ_RESET(GEN6_PM);
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02003085}
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087/* drm_dma.h hooks
3088*/
Paulo Zanonibe30b292014-04-01 15:37:25 -03003089static void ironlake_irq_reset(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003090{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003091 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003092
Paulo Zanoni0c841212014-04-01 15:37:27 -03003093 I915_WRITE(HWSTAM, 0xffffffff);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01003094
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003095 GEN5_IRQ_RESET(DE);
Paulo Zanonic6d954c2014-04-01 15:37:18 -03003096 if (IS_GEN7(dev))
3097 I915_WRITE(GEN7_ERR_INT, 0xffffffff);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003098
Paulo Zanoni7c4d6642014-04-01 15:37:19 -03003099 gen5_gt_irq_reset(dev);
Zhenyu Wangc6501562009-11-03 18:57:21 +00003100
Paulo Zanoni1c69eb42014-04-01 15:37:23 -03003101 ibx_irq_reset(dev);
Ben Widawsky7d991632013-05-28 19:22:25 -07003102}
3103
Ville Syrjälä70591a42014-10-30 19:42:58 +02003104static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
3105{
3106 enum pipe pipe;
3107
3108 I915_WRITE(PORT_HOTPLUG_EN, 0);
3109 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3110
3111 for_each_pipe(dev_priv, pipe)
3112 I915_WRITE(PIPESTAT(pipe), 0xffff);
3113
3114 GEN5_IRQ_RESET(VLV_);
3115}
3116
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003117static void valleyview_irq_preinstall(struct drm_device *dev)
3118{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003119 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003120
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003121 /* VLV magic */
3122 I915_WRITE(VLV_IMR, 0);
3123 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
3124 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
3125 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
3126
Paulo Zanoni7c4d6642014-04-01 15:37:19 -03003127 gen5_gt_irq_reset(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003128
Ville Syrjälä7c4cde32014-10-30 19:42:51 +02003129 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003130
Ville Syrjälä70591a42014-10-30 19:42:58 +02003131 vlv_display_irq_reset(dev_priv);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003132}
3133
Daniel Vetterd6e3cca2014-05-22 22:18:22 +02003134static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3135{
3136 GEN8_IRQ_RESET_NDX(GT, 0);
3137 GEN8_IRQ_RESET_NDX(GT, 1);
3138 GEN8_IRQ_RESET_NDX(GT, 2);
3139 GEN8_IRQ_RESET_NDX(GT, 3);
3140}
3141
Paulo Zanoni823f6b32014-04-01 15:37:26 -03003142static void gen8_irq_reset(struct drm_device *dev)
Ben Widawskyabd58f02013-11-02 21:07:09 -07003143{
3144 struct drm_i915_private *dev_priv = dev->dev_private;
3145 int pipe;
3146
Ben Widawskyabd58f02013-11-02 21:07:09 -07003147 I915_WRITE(GEN8_MASTER_IRQ, 0);
3148 POSTING_READ(GEN8_MASTER_IRQ);
3149
Daniel Vetterd6e3cca2014-05-22 22:18:22 +02003150 gen8_gt_irq_reset(dev_priv);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003151
Damien Lespiau055e3932014-08-18 13:49:10 +01003152 for_each_pipe(dev_priv, pipe)
Daniel Vetterf458ebb2014-09-30 10:56:39 +02003153 if (intel_display_power_is_enabled(dev_priv,
3154 POWER_DOMAIN_PIPE(pipe)))
Paulo Zanoni813bde42014-07-04 11:50:29 -03003155 GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003156
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003157 GEN5_IRQ_RESET(GEN8_DE_PORT_);
3158 GEN5_IRQ_RESET(GEN8_DE_MISC_);
3159 GEN5_IRQ_RESET(GEN8_PCU_);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003160
Paulo Zanoni1c69eb42014-04-01 15:37:23 -03003161 ibx_irq_reset(dev);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003162}
Ben Widawskyabd58f02013-11-02 21:07:09 -07003163
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003164void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv)
3165{
Paulo Zanoni1180e202014-10-07 18:02:52 -03003166 uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003167
Daniel Vetter13321782014-09-15 14:55:29 +02003168 spin_lock_irq(&dev_priv->irq_lock);
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003169 GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B, dev_priv->de_irq_mask[PIPE_B],
Paulo Zanoni1180e202014-10-07 18:02:52 -03003170 ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003171 GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C, dev_priv->de_irq_mask[PIPE_C],
Paulo Zanoni1180e202014-10-07 18:02:52 -03003172 ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
Daniel Vetter13321782014-09-15 14:55:29 +02003173 spin_unlock_irq(&dev_priv->irq_lock);
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003174}
3175
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003176static void cherryview_irq_preinstall(struct drm_device *dev)
3177{
3178 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003179
3180 I915_WRITE(GEN8_MASTER_IRQ, 0);
3181 POSTING_READ(GEN8_MASTER_IRQ);
3182
Daniel Vetterd6e3cca2014-05-22 22:18:22 +02003183 gen8_gt_irq_reset(dev_priv);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003184
3185 GEN5_IRQ_RESET(GEN8_PCU_);
3186
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003187 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
3188
Ville Syrjälä70591a42014-10-30 19:42:58 +02003189 vlv_display_irq_reset(dev_priv);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003190}
3191
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003192static void ibx_hpd_irq_setup(struct drm_device *dev)
Keith Packard7fe0b972011-09-19 13:31:02 -07003193{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003194 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003195 struct intel_encoder *intel_encoder;
Daniel Vetterfee884e2013-07-04 23:35:21 +02003196 u32 hotplug_irqs, hotplug, enabled_irqs = 0;
Keith Packard7fe0b972011-09-19 13:31:02 -07003197
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003198 if (HAS_PCH_IBX(dev)) {
Daniel Vetterfee884e2013-07-04 23:35:21 +02003199 hotplug_irqs = SDE_HOTPLUG_MASK;
Damien Lespiaub2784e12014-08-05 11:29:37 +01003200 for_each_intel_encoder(dev, intel_encoder)
Egbert Eichcd569ae2013-04-16 13:36:57 +02003201 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02003202 enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003203 } else {
Daniel Vetterfee884e2013-07-04 23:35:21 +02003204 hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
Damien Lespiaub2784e12014-08-05 11:29:37 +01003205 for_each_intel_encoder(dev, intel_encoder)
Egbert Eichcd569ae2013-04-16 13:36:57 +02003206 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02003207 enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003208 }
3209
Daniel Vetterfee884e2013-07-04 23:35:21 +02003210 ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003211
3212 /*
3213 * Enable digital hotplug on the PCH, and configure the DP short pulse
3214 * duration to 2ms (which is the minimum in the Display Port spec)
3215 *
3216 * This register is the same on all known PCH chips.
3217 */
Keith Packard7fe0b972011-09-19 13:31:02 -07003218 hotplug = I915_READ(PCH_PORT_HOTPLUG);
3219 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
3220 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
3221 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
3222 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
3223 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3224}
3225
Paulo Zanonid46da432013-02-08 17:35:15 -02003226static void ibx_irq_postinstall(struct drm_device *dev)
3227{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003228 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003229 u32 mask;
Paulo Zanonid46da432013-02-08 17:35:15 -02003230
Daniel Vetter692a04c2013-05-29 21:43:05 +02003231 if (HAS_PCH_NOP(dev))
3232 return;
3233
Paulo Zanoni105b1222014-04-01 15:37:17 -03003234 if (HAS_PCH_IBX(dev))
Daniel Vetter5c673b62014-03-07 20:34:46 +01003235 mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
Paulo Zanoni105b1222014-04-01 15:37:17 -03003236 else
Daniel Vetter5c673b62014-03-07 20:34:46 +01003237 mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
Paulo Zanoni86642812013-04-12 17:57:57 -03003238
Paulo Zanoni337ba012014-04-01 15:37:16 -03003239 GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
Paulo Zanonid46da432013-02-08 17:35:15 -02003240 I915_WRITE(SDEIMR, ~mask);
Paulo Zanonid46da432013-02-08 17:35:15 -02003241}
3242
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003243static void gen5_gt_irq_postinstall(struct drm_device *dev)
3244{
3245 struct drm_i915_private *dev_priv = dev->dev_private;
3246 u32 pm_irqs, gt_irqs;
3247
3248 pm_irqs = gt_irqs = 0;
3249
3250 dev_priv->gt_irq_mask = ~0;
Ben Widawsky040d2ba2013-09-19 11:01:40 -07003251 if (HAS_L3_DPF(dev)) {
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003252 /* L3 parity interrupt is always unmasked. */
Ben Widawsky35a85ac2013-09-19 11:13:41 -07003253 dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
3254 gt_irqs |= GT_PARITY_ERROR(dev);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003255 }
3256
3257 gt_irqs |= GT_RENDER_USER_INTERRUPT;
3258 if (IS_GEN5(dev)) {
3259 gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
3260 ILK_BSD_USER_INTERRUPT;
3261 } else {
3262 gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
3263 }
3264
Paulo Zanoni35079892014-04-01 15:37:15 -03003265 GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003266
3267 if (INTEL_INFO(dev)->gen >= 6) {
Deepak Sa6706b42014-03-15 20:23:22 +05303268 pm_irqs |= dev_priv->pm_rps_events;
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003269
3270 if (HAS_VEBOX(dev))
3271 pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3272
Paulo Zanoni605cd252013-08-06 18:57:15 -03003273 dev_priv->pm_irq_mask = 0xffffffff;
Paulo Zanoni35079892014-04-01 15:37:15 -03003274 GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003275 }
3276}
3277
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003278static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003279{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003280 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003281 u32 display_mask, extra_mask;
3282
3283 if (INTEL_INFO(dev)->gen >= 7) {
3284 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3285 DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
3286 DE_PLANEB_FLIP_DONE_IVB |
Daniel Vetter5c673b62014-03-07 20:34:46 +01003287 DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003288 extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
Daniel Vetter5c673b62014-03-07 20:34:46 +01003289 DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003290 } else {
3291 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3292 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
Daniel Vetter5b3a8562013-10-16 22:55:48 +02003293 DE_AUX_CHANNEL_A |
Daniel Vetter5b3a8562013-10-16 22:55:48 +02003294 DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
3295 DE_POISON);
Daniel Vetter5c673b62014-03-07 20:34:46 +01003296 extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3297 DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003298 }
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003299
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003300 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003301
Paulo Zanoni0c841212014-04-01 15:37:27 -03003302 I915_WRITE(HWSTAM, 0xeffe);
3303
Paulo Zanoni622364b2014-04-01 15:37:22 -03003304 ibx_irq_pre_postinstall(dev);
3305
Paulo Zanoni35079892014-04-01 15:37:15 -03003306 GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003307
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003308 gen5_gt_irq_postinstall(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003309
Paulo Zanonid46da432013-02-08 17:35:15 -02003310 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07003311
Jesse Barnesf97108d2010-01-29 11:27:07 -08003312 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter6005ce42013-06-27 13:44:59 +02003313 /* Enable PCU event interrupts
3314 *
3315 * spinlocking not required here for correctness since interrupt
Daniel Vetter4bc9d432013-06-27 13:44:58 +02003316 * setup is guaranteed to run in single-threaded context. But we
3317 * need it to make the assert_spin_locked happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02003318 spin_lock_irq(&dev_priv->irq_lock);
Jesse Barnesf97108d2010-01-29 11:27:07 -08003319 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
Daniel Vetterd6207432014-09-15 14:55:27 +02003320 spin_unlock_irq(&dev_priv->irq_lock);
Jesse Barnesf97108d2010-01-29 11:27:07 -08003321 }
3322
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003323 return 0;
3324}
3325
Imre Deakf8b79e52014-03-04 19:23:07 +02003326static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3327{
3328 u32 pipestat_mask;
3329 u32 iir_mask;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003330 enum pipe pipe;
Imre Deakf8b79e52014-03-04 19:23:07 +02003331
3332 pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3333 PIPE_FIFO_UNDERRUN_STATUS;
3334
Ville Syrjälä120dda42014-10-30 19:42:57 +02003335 for_each_pipe(dev_priv, pipe)
3336 I915_WRITE(PIPESTAT(pipe), pipestat_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003337 POSTING_READ(PIPESTAT(PIPE_A));
3338
3339 pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3340 PIPE_CRC_DONE_INTERRUPT_STATUS;
3341
Ville Syrjälä120dda42014-10-30 19:42:57 +02003342 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3343 for_each_pipe(dev_priv, pipe)
3344 i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003345
3346 iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3347 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3348 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003349 if (IS_CHERRYVIEW(dev_priv))
3350 iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
Imre Deakf8b79e52014-03-04 19:23:07 +02003351 dev_priv->irq_mask &= ~iir_mask;
3352
3353 I915_WRITE(VLV_IIR, iir_mask);
3354 I915_WRITE(VLV_IIR, iir_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003355 I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
Ville Syrjälä76e41862014-10-30 19:42:54 +02003356 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3357 POSTING_READ(VLV_IMR);
Imre Deakf8b79e52014-03-04 19:23:07 +02003358}
3359
3360static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3361{
3362 u32 pipestat_mask;
3363 u32 iir_mask;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003364 enum pipe pipe;
Imre Deakf8b79e52014-03-04 19:23:07 +02003365
3366 iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3367 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
Imre Deak6c7fba02014-03-10 19:44:48 +02003368 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003369 if (IS_CHERRYVIEW(dev_priv))
3370 iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
Imre Deakf8b79e52014-03-04 19:23:07 +02003371
3372 dev_priv->irq_mask |= iir_mask;
Imre Deakf8b79e52014-03-04 19:23:07 +02003373 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
Ville Syrjälä76e41862014-10-30 19:42:54 +02003374 I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003375 I915_WRITE(VLV_IIR, iir_mask);
3376 I915_WRITE(VLV_IIR, iir_mask);
3377 POSTING_READ(VLV_IIR);
3378
3379 pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3380 PIPE_CRC_DONE_INTERRUPT_STATUS;
3381
Ville Syrjälä120dda42014-10-30 19:42:57 +02003382 i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3383 for_each_pipe(dev_priv, pipe)
3384 i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003385
3386 pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3387 PIPE_FIFO_UNDERRUN_STATUS;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003388
3389 for_each_pipe(dev_priv, pipe)
3390 I915_WRITE(PIPESTAT(pipe), pipestat_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003391 POSTING_READ(PIPESTAT(PIPE_A));
3392}
3393
3394void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3395{
3396 assert_spin_locked(&dev_priv->irq_lock);
3397
3398 if (dev_priv->display_irqs_enabled)
3399 return;
3400
3401 dev_priv->display_irqs_enabled = true;
3402
Imre Deak950eaba2014-09-08 15:21:09 +03003403 if (intel_irqs_enabled(dev_priv))
Imre Deakf8b79e52014-03-04 19:23:07 +02003404 valleyview_display_irqs_install(dev_priv);
3405}
3406
3407void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3408{
3409 assert_spin_locked(&dev_priv->irq_lock);
3410
3411 if (!dev_priv->display_irqs_enabled)
3412 return;
3413
3414 dev_priv->display_irqs_enabled = false;
3415
Imre Deak950eaba2014-09-08 15:21:09 +03003416 if (intel_irqs_enabled(dev_priv))
Imre Deakf8b79e52014-03-04 19:23:07 +02003417 valleyview_display_irqs_uninstall(dev_priv);
3418}
3419
Ville Syrjälä0e6c9a92014-10-30 19:43:00 +02003420static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003421{
Imre Deakf8b79e52014-03-04 19:23:07 +02003422 dev_priv->irq_mask = ~0;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003423
Daniel Vetter20afbda2012-12-11 14:05:07 +01003424 I915_WRITE(PORT_HOTPLUG_EN, 0);
3425 POSTING_READ(PORT_HOTPLUG_EN);
3426
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003427 I915_WRITE(VLV_IIR, 0xffffffff);
Ville Syrjälä76e41862014-10-30 19:42:54 +02003428 I915_WRITE(VLV_IIR, 0xffffffff);
3429 I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3430 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3431 POSTING_READ(VLV_IMR);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003432
Daniel Vetterb79480b2013-06-27 17:52:10 +02003433 /* Interrupt setup is already guaranteed to be single-threaded, this is
3434 * just to make the assert_spin_locked check happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02003435 spin_lock_irq(&dev_priv->irq_lock);
Imre Deakf8b79e52014-03-04 19:23:07 +02003436 if (dev_priv->display_irqs_enabled)
3437 valleyview_display_irqs_install(dev_priv);
Daniel Vetterd6207432014-09-15 14:55:27 +02003438 spin_unlock_irq(&dev_priv->irq_lock);
Ville Syrjälä0e6c9a92014-10-30 19:43:00 +02003439}
3440
3441static int valleyview_irq_postinstall(struct drm_device *dev)
3442{
3443 struct drm_i915_private *dev_priv = dev->dev_private;
3444
3445 vlv_display_irq_postinstall(dev_priv);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003446
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003447 gen5_gt_irq_postinstall(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003448
3449 /* ack & enable invalid PTE error interrupts */
3450#if 0 /* FIXME: add support to irq handler for checking these bits */
3451 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
3452 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
3453#endif
3454
3455 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003456
3457 return 0;
3458}
3459
Ben Widawskyabd58f02013-11-02 21:07:09 -07003460static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3461{
Ben Widawskyabd58f02013-11-02 21:07:09 -07003462 /* These are interrupts we'll toggle with the ring mask register */
3463 uint32_t gt_interrupts[] = {
3464 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
Oscar Mateo73d477f2014-07-24 17:04:31 +01003465 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
Ben Widawskyabd58f02013-11-02 21:07:09 -07003466 GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
Oscar Mateo73d477f2014-07-24 17:04:31 +01003467 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
3468 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
Ben Widawskyabd58f02013-11-02 21:07:09 -07003469 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
Oscar Mateo73d477f2014-07-24 17:04:31 +01003470 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3471 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
3472 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
Ben Widawskyabd58f02013-11-02 21:07:09 -07003473 0,
Oscar Mateo73d477f2014-07-24 17:04:31 +01003474 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
3475 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
Ben Widawskyabd58f02013-11-02 21:07:09 -07003476 };
3477
Ben Widawsky09610212014-05-15 20:58:08 +03003478 dev_priv->pm_irq_mask = 0xffffffff;
Deepak S9a2d2d82014-08-22 08:32:40 +05303479 GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
3480 GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
3481 GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, dev_priv->pm_rps_events);
3482 GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003483}
3484
3485static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3486{
Damien Lespiau770de832014-03-20 20:45:01 +00003487 uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3488 uint32_t de_pipe_enables;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003489 int pipe;
Damien Lespiau770de832014-03-20 20:45:01 +00003490
3491 if (IS_GEN9(dev_priv))
3492 de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3493 GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
3494 else
3495 de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3496 GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3497
3498 de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3499 GEN8_PIPE_FIFO_UNDERRUN;
3500
Daniel Vetter13b3a0a2013-11-07 15:31:52 +01003501 dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
3502 dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
3503 dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003504
Damien Lespiau055e3932014-08-18 13:49:10 +01003505 for_each_pipe(dev_priv, pipe)
Daniel Vetterf458ebb2014-09-30 10:56:39 +02003506 if (intel_display_power_is_enabled(dev_priv,
Paulo Zanoni813bde42014-07-04 11:50:29 -03003507 POWER_DOMAIN_PIPE(pipe)))
3508 GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3509 dev_priv->de_irq_mask[pipe],
3510 de_pipe_enables);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003511
Paulo Zanoni35079892014-04-01 15:37:15 -03003512 GEN5_IRQ_INIT(GEN8_DE_PORT_, ~GEN8_AUX_CHANNEL_A, GEN8_AUX_CHANNEL_A);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003513}
3514
3515static int gen8_irq_postinstall(struct drm_device *dev)
3516{
3517 struct drm_i915_private *dev_priv = dev->dev_private;
3518
Paulo Zanoni622364b2014-04-01 15:37:22 -03003519 ibx_irq_pre_postinstall(dev);
3520
Ben Widawskyabd58f02013-11-02 21:07:09 -07003521 gen8_gt_irq_postinstall(dev_priv);
3522 gen8_de_irq_postinstall(dev_priv);
3523
3524 ibx_irq_postinstall(dev);
3525
3526 I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3527 POSTING_READ(GEN8_MASTER_IRQ);
3528
3529 return 0;
3530}
3531
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003532static int cherryview_irq_postinstall(struct drm_device *dev)
3533{
3534 struct drm_i915_private *dev_priv = dev->dev_private;
3535 u32 enable_mask = I915_DISPLAY_PORT_INTERRUPT |
3536 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003537 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
Ville Syrjälä3278f672014-04-09 13:28:49 +03003538 I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3539 u32 pipestat_enable = PLANE_FLIP_DONE_INT_STATUS_VLV |
3540 PIPE_CRC_DONE_INTERRUPT_STATUS;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003541 int pipe;
3542
3543 /*
3544 * Leave vblank interrupts masked initially. enable/disable will
3545 * toggle them based on usage.
3546 */
Ville Syrjälä3278f672014-04-09 13:28:49 +03003547 dev_priv->irq_mask = ~enable_mask;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003548
Damien Lespiau055e3932014-08-18 13:49:10 +01003549 for_each_pipe(dev_priv, pipe)
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003550 I915_WRITE(PIPESTAT(pipe), 0xffff);
3551
Daniel Vetterd6207432014-09-15 14:55:27 +02003552 spin_lock_irq(&dev_priv->irq_lock);
Ville Syrjälä3278f672014-04-09 13:28:49 +03003553 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
Damien Lespiau055e3932014-08-18 13:49:10 +01003554 for_each_pipe(dev_priv, pipe)
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003555 i915_enable_pipestat(dev_priv, pipe, pipestat_enable);
Daniel Vetterd6207432014-09-15 14:55:27 +02003556 spin_unlock_irq(&dev_priv->irq_lock);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003557
3558 I915_WRITE(VLV_IIR, 0xffffffff);
Ville Syrjälä76e41862014-10-30 19:42:54 +02003559 I915_WRITE(VLV_IIR, 0xffffffff);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003560 I915_WRITE(VLV_IER, enable_mask);
Ville Syrjälä76e41862014-10-30 19:42:54 +02003561 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3562 POSTING_READ(VLV_IMR);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003563
3564 gen8_gt_irq_postinstall(dev_priv);
3565
3566 I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
3567 POSTING_READ(GEN8_MASTER_IRQ);
3568
3569 return 0;
3570}
3571
Ben Widawskyabd58f02013-11-02 21:07:09 -07003572static void gen8_irq_uninstall(struct drm_device *dev)
3573{
3574 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003575
3576 if (!dev_priv)
3577 return;
3578
Paulo Zanoni823f6b32014-04-01 15:37:26 -03003579 gen8_irq_reset(dev);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003580}
3581
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003582static void valleyview_irq_uninstall(struct drm_device *dev)
3583{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003584 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003585
3586 if (!dev_priv)
3587 return;
3588
Imre Deak843d0e72014-04-14 20:24:23 +03003589 I915_WRITE(VLV_MASTER_IER, 0);
3590
Ville Syrjälä893fce82014-10-30 19:42:56 +02003591 gen5_gt_irq_reset(dev);
3592
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003593 I915_WRITE(HWSTAM, 0xffffffff);
Imre Deakf8b79e52014-03-04 19:23:07 +02003594
Daniel Vetterd6207432014-09-15 14:55:27 +02003595 /* Interrupt setup is already guaranteed to be single-threaded, this is
3596 * just to make the assert_spin_locked check happy. */
3597 spin_lock_irq(&dev_priv->irq_lock);
Imre Deakf8b79e52014-03-04 19:23:07 +02003598 if (dev_priv->display_irqs_enabled)
3599 valleyview_display_irqs_uninstall(dev_priv);
Daniel Vetterd6207432014-09-15 14:55:27 +02003600 spin_unlock_irq(&dev_priv->irq_lock);
Imre Deakf8b79e52014-03-04 19:23:07 +02003601
Ville Syrjälä70591a42014-10-30 19:42:58 +02003602 vlv_display_irq_reset(dev_priv);
Imre Deakf8b79e52014-03-04 19:23:07 +02003603
Ville Syrjälä70591a42014-10-30 19:42:58 +02003604 dev_priv->irq_mask = 0;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003605}
3606
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003607static void cherryview_irq_uninstall(struct drm_device *dev)
3608{
3609 struct drm_i915_private *dev_priv = dev->dev_private;
3610 int pipe;
3611
3612 if (!dev_priv)
3613 return;
3614
3615 I915_WRITE(GEN8_MASTER_IRQ, 0);
3616 POSTING_READ(GEN8_MASTER_IRQ);
3617
Ville Syrjäläa2c30fb2014-10-30 19:42:52 +02003618 gen8_gt_irq_reset(dev_priv);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003619
Ville Syrjäläa2c30fb2014-10-30 19:42:52 +02003620 GEN5_IRQ_RESET(GEN8_PCU_);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003621
3622 I915_WRITE(PORT_HOTPLUG_EN, 0);
3623 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3624
Damien Lespiau055e3932014-08-18 13:49:10 +01003625 for_each_pipe(dev_priv, pipe)
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003626 I915_WRITE(PIPESTAT(pipe), 0xffff);
3627
Ville Syrjälä23a09c72014-10-30 19:42:55 +02003628 GEN5_IRQ_RESET(VLV_);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003629}
3630
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003631static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003632{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003633 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07003634
3635 if (!dev_priv)
3636 return;
3637
Paulo Zanonibe30b292014-04-01 15:37:25 -03003638 ironlake_irq_reset(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003639}
3640
Chris Wilsonc2798b12012-04-22 21:13:57 +01003641static void i8xx_irq_preinstall(struct drm_device * dev)
3642{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003643 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003644 int pipe;
3645
Damien Lespiau055e3932014-08-18 13:49:10 +01003646 for_each_pipe(dev_priv, pipe)
Chris Wilsonc2798b12012-04-22 21:13:57 +01003647 I915_WRITE(PIPESTAT(pipe), 0);
3648 I915_WRITE16(IMR, 0xffff);
3649 I915_WRITE16(IER, 0x0);
3650 POSTING_READ16(IER);
3651}
3652
3653static int i8xx_irq_postinstall(struct drm_device *dev)
3654{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003655 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003656
Chris Wilsonc2798b12012-04-22 21:13:57 +01003657 I915_WRITE16(EMR,
3658 ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3659
3660 /* Unmask the interrupts that we always want on. */
3661 dev_priv->irq_mask =
3662 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3663 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3664 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3665 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3666 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3667 I915_WRITE16(IMR, dev_priv->irq_mask);
3668
3669 I915_WRITE16(IER,
3670 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3671 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3672 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3673 I915_USER_INTERRUPT);
3674 POSTING_READ16(IER);
3675
Daniel Vetter379ef822013-10-16 22:55:56 +02003676 /* Interrupt setup is already guaranteed to be single-threaded, this is
3677 * just to make the assert_spin_locked check happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02003678 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak755e9012014-02-10 18:42:47 +02003679 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3680 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
Daniel Vetterd6207432014-09-15 14:55:27 +02003681 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter379ef822013-10-16 22:55:56 +02003682
Chris Wilsonc2798b12012-04-22 21:13:57 +01003683 return 0;
3684}
3685
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003686/*
3687 * Returns true when a page flip has completed.
3688 */
3689static bool i8xx_handle_vblank(struct drm_device *dev,
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003690 int plane, int pipe, u32 iir)
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003691{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003692 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003693 u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003694
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03003695 if (!intel_pipe_handle_vblank(dev, pipe))
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003696 return false;
3697
3698 if ((iir & flip_pending) == 0)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003699 goto check_page_flip;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003700
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003701 intel_prepare_page_flip(dev, plane);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003702
3703 /* We detect FlipDone by looking for the change in PendingFlip from '1'
3704 * to '0' on the following vblank, i.e. IIR has the Pendingflip
3705 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
3706 * the flip is completed (no longer pending). Since this doesn't raise
3707 * an interrupt per se, we watch for the change at vblank.
3708 */
3709 if (I915_READ16(ISR) & flip_pending)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003710 goto check_page_flip;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003711
3712 intel_finish_page_flip(dev, pipe);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003713 return true;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003714
3715check_page_flip:
3716 intel_check_page_flip(dev, pipe);
3717 return false;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003718}
3719
Daniel Vetterff1f5252012-10-02 15:10:55 +02003720static irqreturn_t i8xx_irq_handler(int irq, void *arg)
Chris Wilsonc2798b12012-04-22 21:13:57 +01003721{
Daniel Vetter45a83f82014-05-12 19:17:55 +02003722 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03003723 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003724 u16 iir, new_iir;
3725 u32 pipe_stats[2];
Chris Wilsonc2798b12012-04-22 21:13:57 +01003726 int pipe;
3727 u16 flip_mask =
3728 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3729 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3730
Chris Wilsonc2798b12012-04-22 21:13:57 +01003731 iir = I915_READ16(IIR);
3732 if (iir == 0)
3733 return IRQ_NONE;
3734
3735 while (iir & ~flip_mask) {
3736 /* Can't rely on pipestat interrupt bit in iir as it might
3737 * have been cleared after the pipestat interrupt was received.
3738 * It doesn't set the bit in iir again, but it still produces
3739 * interrupts (for non-MSI).
3740 */
Daniel Vetter222c7f52014-09-15 14:55:28 +02003741 spin_lock(&dev_priv->irq_lock);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003742 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Mika Kuoppala58174462014-02-25 17:11:26 +02003743 i915_handle_error(dev, false,
3744 "Command parser error, iir 0x%08x",
3745 iir);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003746
Damien Lespiau055e3932014-08-18 13:49:10 +01003747 for_each_pipe(dev_priv, pipe) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01003748 int reg = PIPESTAT(pipe);
3749 pipe_stats[pipe] = I915_READ(reg);
3750
3751 /*
3752 * Clear the PIPE*STAT regs before the IIR
3753 */
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02003754 if (pipe_stats[pipe] & 0x8000ffff)
Chris Wilsonc2798b12012-04-22 21:13:57 +01003755 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003756 }
Daniel Vetter222c7f52014-09-15 14:55:28 +02003757 spin_unlock(&dev_priv->irq_lock);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003758
3759 I915_WRITE16(IIR, iir & ~flip_mask);
3760 new_iir = I915_READ16(IIR); /* Flush posted writes */
3761
Daniel Vetterd05c6172012-04-26 23:28:09 +02003762 i915_update_dri1_breadcrumb(dev);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003763
3764 if (iir & I915_USER_INTERRUPT)
3765 notify_ring(dev, &dev_priv->ring[RCS]);
3766
Damien Lespiau055e3932014-08-18 13:49:10 +01003767 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003768 int plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01003769 if (HAS_FBC(dev))
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003770 plane = !plane;
3771
Daniel Vetter4356d582013-10-16 22:55:55 +02003772 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003773 i8xx_handle_vblank(dev, plane, pipe, iir))
3774 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003775
Daniel Vetter4356d582013-10-16 22:55:55 +02003776 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
Daniel Vetter277de952013-10-18 16:37:07 +02003777 i9xx_pipe_crc_irq_handler(dev, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02003778
Daniel Vetter1f7247c2014-09-30 10:56:48 +02003779 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3780 intel_cpu_fifo_underrun_irq_handler(dev_priv,
3781 pipe);
Daniel Vetter4356d582013-10-16 22:55:55 +02003782 }
Chris Wilsonc2798b12012-04-22 21:13:57 +01003783
3784 iir = new_iir;
3785 }
3786
3787 return IRQ_HANDLED;
3788}
3789
3790static void i8xx_irq_uninstall(struct drm_device * dev)
3791{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003792 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003793 int pipe;
3794
Damien Lespiau055e3932014-08-18 13:49:10 +01003795 for_each_pipe(dev_priv, pipe) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01003796 /* Clear enable bits; then clear status bits */
3797 I915_WRITE(PIPESTAT(pipe), 0);
3798 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3799 }
3800 I915_WRITE16(IMR, 0xffff);
3801 I915_WRITE16(IER, 0x0);
3802 I915_WRITE16(IIR, I915_READ16(IIR));
3803}
3804
Chris Wilsona266c7d2012-04-24 22:59:44 +01003805static void i915_irq_preinstall(struct drm_device * dev)
3806{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003807 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003808 int pipe;
3809
Chris Wilsona266c7d2012-04-24 22:59:44 +01003810 if (I915_HAS_HOTPLUG(dev)) {
3811 I915_WRITE(PORT_HOTPLUG_EN, 0);
3812 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3813 }
3814
Chris Wilson00d98eb2012-04-24 22:59:48 +01003815 I915_WRITE16(HWSTAM, 0xeffe);
Damien Lespiau055e3932014-08-18 13:49:10 +01003816 for_each_pipe(dev_priv, pipe)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003817 I915_WRITE(PIPESTAT(pipe), 0);
3818 I915_WRITE(IMR, 0xffffffff);
3819 I915_WRITE(IER, 0x0);
3820 POSTING_READ(IER);
3821}
3822
3823static int i915_irq_postinstall(struct drm_device *dev)
3824{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003825 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson38bde182012-04-24 22:59:50 +01003826 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003827
Chris Wilson38bde182012-04-24 22:59:50 +01003828 I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3829
3830 /* Unmask the interrupts that we always want on. */
3831 dev_priv->irq_mask =
3832 ~(I915_ASLE_INTERRUPT |
3833 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3834 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3835 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3836 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3837 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3838
3839 enable_mask =
3840 I915_ASLE_INTERRUPT |
3841 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3842 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3843 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3844 I915_USER_INTERRUPT;
3845
Chris Wilsona266c7d2012-04-24 22:59:44 +01003846 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetter20afbda2012-12-11 14:05:07 +01003847 I915_WRITE(PORT_HOTPLUG_EN, 0);
3848 POSTING_READ(PORT_HOTPLUG_EN);
3849
Chris Wilsona266c7d2012-04-24 22:59:44 +01003850 /* Enable in IER... */
3851 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3852 /* and unmask in IMR */
3853 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3854 }
3855
Chris Wilsona266c7d2012-04-24 22:59:44 +01003856 I915_WRITE(IMR, dev_priv->irq_mask);
3857 I915_WRITE(IER, enable_mask);
3858 POSTING_READ(IER);
3859
Jani Nikulaf49e38d2013-04-29 13:02:54 +03003860 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003861
Daniel Vetter379ef822013-10-16 22:55:56 +02003862 /* Interrupt setup is already guaranteed to be single-threaded, this is
3863 * just to make the assert_spin_locked check happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02003864 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak755e9012014-02-10 18:42:47 +02003865 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3866 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
Daniel Vetterd6207432014-09-15 14:55:27 +02003867 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter379ef822013-10-16 22:55:56 +02003868
Daniel Vetter20afbda2012-12-11 14:05:07 +01003869 return 0;
3870}
3871
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003872/*
3873 * Returns true when a page flip has completed.
3874 */
3875static bool i915_handle_vblank(struct drm_device *dev,
3876 int plane, int pipe, u32 iir)
3877{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003878 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003879 u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
3880
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03003881 if (!intel_pipe_handle_vblank(dev, pipe))
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003882 return false;
3883
3884 if ((iir & flip_pending) == 0)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003885 goto check_page_flip;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003886
3887 intel_prepare_page_flip(dev, plane);
3888
3889 /* We detect FlipDone by looking for the change in PendingFlip from '1'
3890 * to '0' on the following vblank, i.e. IIR has the Pendingflip
3891 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
3892 * the flip is completed (no longer pending). Since this doesn't raise
3893 * an interrupt per se, we watch for the change at vblank.
3894 */
3895 if (I915_READ(ISR) & flip_pending)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003896 goto check_page_flip;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003897
3898 intel_finish_page_flip(dev, pipe);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003899 return true;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003900
3901check_page_flip:
3902 intel_check_page_flip(dev, pipe);
3903 return false;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003904}
3905
Daniel Vetterff1f5252012-10-02 15:10:55 +02003906static irqreturn_t i915_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003907{
Daniel Vetter45a83f82014-05-12 19:17:55 +02003908 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03003909 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8291ee92012-04-24 22:59:47 +01003910 u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
Chris Wilson38bde182012-04-24 22:59:50 +01003911 u32 flip_mask =
3912 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3913 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilson38bde182012-04-24 22:59:50 +01003914 int pipe, ret = IRQ_NONE;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003915
Chris Wilsona266c7d2012-04-24 22:59:44 +01003916 iir = I915_READ(IIR);
Chris Wilson38bde182012-04-24 22:59:50 +01003917 do {
3918 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson8291ee92012-04-24 22:59:47 +01003919 bool blc_event = false;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003920
3921 /* Can't rely on pipestat interrupt bit in iir as it might
3922 * have been cleared after the pipestat interrupt was received.
3923 * It doesn't set the bit in iir again, but it still produces
3924 * interrupts (for non-MSI).
3925 */
Daniel Vetter222c7f52014-09-15 14:55:28 +02003926 spin_lock(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003927 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Mika Kuoppala58174462014-02-25 17:11:26 +02003928 i915_handle_error(dev, false,
3929 "Command parser error, iir 0x%08x",
3930 iir);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003931
Damien Lespiau055e3932014-08-18 13:49:10 +01003932 for_each_pipe(dev_priv, pipe) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003933 int reg = PIPESTAT(pipe);
3934 pipe_stats[pipe] = I915_READ(reg);
3935
Chris Wilson38bde182012-04-24 22:59:50 +01003936 /* Clear the PIPE*STAT regs before the IIR */
Chris Wilsona266c7d2012-04-24 22:59:44 +01003937 if (pipe_stats[pipe] & 0x8000ffff) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003938 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilson38bde182012-04-24 22:59:50 +01003939 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003940 }
3941 }
Daniel Vetter222c7f52014-09-15 14:55:28 +02003942 spin_unlock(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003943
3944 if (!irq_received)
3945 break;
3946
Chris Wilsona266c7d2012-04-24 22:59:44 +01003947 /* Consume port. Then clear IIR or we'll miss events */
Ville Syrjälä16c6c562014-04-01 10:54:36 +03003948 if (I915_HAS_HOTPLUG(dev) &&
3949 iir & I915_DISPLAY_PORT_INTERRUPT)
3950 i9xx_hpd_irq_handler(dev);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003951
Chris Wilson38bde182012-04-24 22:59:50 +01003952 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003953 new_iir = I915_READ(IIR); /* Flush posted writes */
3954
Chris Wilsona266c7d2012-04-24 22:59:44 +01003955 if (iir & I915_USER_INTERRUPT)
3956 notify_ring(dev, &dev_priv->ring[RCS]);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003957
Damien Lespiau055e3932014-08-18 13:49:10 +01003958 for_each_pipe(dev_priv, pipe) {
Chris Wilson38bde182012-04-24 22:59:50 +01003959 int plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01003960 if (HAS_FBC(dev))
Chris Wilson38bde182012-04-24 22:59:50 +01003961 plane = !plane;
Ville Syrjälä5e2032d2013-02-19 15:16:38 +02003962
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003963 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
3964 i915_handle_vblank(dev, plane, pipe, iir))
3965 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003966
3967 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3968 blc_event = true;
Daniel Vetter4356d582013-10-16 22:55:55 +02003969
3970 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
Daniel Vetter277de952013-10-18 16:37:07 +02003971 i9xx_pipe_crc_irq_handler(dev, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02003972
Daniel Vetter1f7247c2014-09-30 10:56:48 +02003973 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3974 intel_cpu_fifo_underrun_irq_handler(dev_priv,
3975 pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003976 }
3977
Chris Wilsona266c7d2012-04-24 22:59:44 +01003978 if (blc_event || (iir & I915_ASLE_INTERRUPT))
3979 intel_opregion_asle_intr(dev);
3980
3981 /* With MSI, interrupts are only generated when iir
3982 * transitions from zero to nonzero. If another bit got
3983 * set while we were handling the existing iir bits, then
3984 * we would never get another interrupt.
3985 *
3986 * This is fine on non-MSI as well, as if we hit this path
3987 * we avoid exiting the interrupt handler only to generate
3988 * another one.
3989 *
3990 * Note that for MSI this could cause a stray interrupt report
3991 * if an interrupt landed in the time between writing IIR and
3992 * the posting read. This should be rare enough to never
3993 * trigger the 99% of 100,000 interrupts test for disabling
3994 * stray interrupts.
3995 */
Chris Wilson38bde182012-04-24 22:59:50 +01003996 ret = IRQ_HANDLED;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003997 iir = new_iir;
Chris Wilson38bde182012-04-24 22:59:50 +01003998 } while (iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003999
Daniel Vetterd05c6172012-04-26 23:28:09 +02004000 i915_update_dri1_breadcrumb(dev);
Chris Wilson8291ee92012-04-24 22:59:47 +01004001
Chris Wilsona266c7d2012-04-24 22:59:44 +01004002 return ret;
4003}
4004
4005static void i915_irq_uninstall(struct drm_device * dev)
4006{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004007 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004008 int pipe;
4009
Chris Wilsona266c7d2012-04-24 22:59:44 +01004010 if (I915_HAS_HOTPLUG(dev)) {
4011 I915_WRITE(PORT_HOTPLUG_EN, 0);
4012 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4013 }
4014
Chris Wilson00d98eb2012-04-24 22:59:48 +01004015 I915_WRITE16(HWSTAM, 0xffff);
Damien Lespiau055e3932014-08-18 13:49:10 +01004016 for_each_pipe(dev_priv, pipe) {
Chris Wilson55b39752012-04-24 22:59:49 +01004017 /* Clear enable bits; then clear status bits */
Chris Wilsona266c7d2012-04-24 22:59:44 +01004018 I915_WRITE(PIPESTAT(pipe), 0);
Chris Wilson55b39752012-04-24 22:59:49 +01004019 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4020 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01004021 I915_WRITE(IMR, 0xffffffff);
4022 I915_WRITE(IER, 0x0);
4023
Chris Wilsona266c7d2012-04-24 22:59:44 +01004024 I915_WRITE(IIR, I915_READ(IIR));
4025}
4026
4027static void i965_irq_preinstall(struct drm_device * dev)
4028{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004029 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004030 int pipe;
4031
Chris Wilsonadca4732012-05-11 18:01:31 +01004032 I915_WRITE(PORT_HOTPLUG_EN, 0);
4033 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01004034
4035 I915_WRITE(HWSTAM, 0xeffe);
Damien Lespiau055e3932014-08-18 13:49:10 +01004036 for_each_pipe(dev_priv, pipe)
Chris Wilsona266c7d2012-04-24 22:59:44 +01004037 I915_WRITE(PIPESTAT(pipe), 0);
4038 I915_WRITE(IMR, 0xffffffff);
4039 I915_WRITE(IER, 0x0);
4040 POSTING_READ(IER);
4041}
4042
4043static int i965_irq_postinstall(struct drm_device *dev)
4044{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004045 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004046 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004047 u32 error_mask;
4048
Chris Wilsona266c7d2012-04-24 22:59:44 +01004049 /* Unmask the interrupts that we always want on. */
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004050 dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
Chris Wilsonadca4732012-05-11 18:01:31 +01004051 I915_DISPLAY_PORT_INTERRUPT |
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004052 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4053 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4054 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4055 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4056 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4057
4058 enable_mask = ~dev_priv->irq_mask;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02004059 enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4060 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004061 enable_mask |= I915_USER_INTERRUPT;
4062
4063 if (IS_G4X(dev))
4064 enable_mask |= I915_BSD_USER_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004065
Daniel Vetterb79480b2013-06-27 17:52:10 +02004066 /* Interrupt setup is already guaranteed to be single-threaded, this is
4067 * just to make the assert_spin_locked check happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02004068 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak755e9012014-02-10 18:42:47 +02004069 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4070 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4071 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
Daniel Vetterd6207432014-09-15 14:55:27 +02004072 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004073
Chris Wilsona266c7d2012-04-24 22:59:44 +01004074 /*
4075 * Enable some error detection, note the instruction error mask
4076 * bit is reserved, so we leave it masked.
4077 */
4078 if (IS_G4X(dev)) {
4079 error_mask = ~(GM45_ERROR_PAGE_TABLE |
4080 GM45_ERROR_MEM_PRIV |
4081 GM45_ERROR_CP_PRIV |
4082 I915_ERROR_MEMORY_REFRESH);
4083 } else {
4084 error_mask = ~(I915_ERROR_PAGE_TABLE |
4085 I915_ERROR_MEMORY_REFRESH);
4086 }
4087 I915_WRITE(EMR, error_mask);
4088
4089 I915_WRITE(IMR, dev_priv->irq_mask);
4090 I915_WRITE(IER, enable_mask);
4091 POSTING_READ(IER);
4092
Daniel Vetter20afbda2012-12-11 14:05:07 +01004093 I915_WRITE(PORT_HOTPLUG_EN, 0);
4094 POSTING_READ(PORT_HOTPLUG_EN);
4095
Jani Nikulaf49e38d2013-04-29 13:02:54 +03004096 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01004097
4098 return 0;
4099}
4100
Egbert Eichbac56d52013-02-25 12:06:51 -05004101static void i915_hpd_irq_setup(struct drm_device *dev)
Daniel Vetter20afbda2012-12-11 14:05:07 +01004102{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004103 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eichcd569ae2013-04-16 13:36:57 +02004104 struct intel_encoder *intel_encoder;
Daniel Vetter20afbda2012-12-11 14:05:07 +01004105 u32 hotplug_en;
4106
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02004107 assert_spin_locked(&dev_priv->irq_lock);
4108
Egbert Eichbac56d52013-02-25 12:06:51 -05004109 if (I915_HAS_HOTPLUG(dev)) {
4110 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4111 hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4112 /* Note HDMI and DP share hotplug bits */
Egbert Eiche5868a32013-02-28 04:17:12 -05004113 /* enable bits are the same for all generations */
Damien Lespiaub2784e12014-08-05 11:29:37 +01004114 for_each_intel_encoder(dev, intel_encoder)
Egbert Eichcd569ae2013-04-16 13:36:57 +02004115 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4116 hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
Egbert Eichbac56d52013-02-25 12:06:51 -05004117 /* Programming the CRT detection parameters tends
4118 to generate a spurious hotplug event about three
4119 seconds later. So just do it once.
4120 */
4121 if (IS_G4X(dev))
4122 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
Daniel Vetter85fc95b2013-03-27 15:47:11 +01004123 hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
Egbert Eichbac56d52013-02-25 12:06:51 -05004124 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004125
Egbert Eichbac56d52013-02-25 12:06:51 -05004126 /* Ignore TV since it's buggy */
4127 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4128 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01004129}
4130
Daniel Vetterff1f5252012-10-02 15:10:55 +02004131static irqreturn_t i965_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01004132{
Daniel Vetter45a83f82014-05-12 19:17:55 +02004133 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03004134 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004135 u32 iir, new_iir;
4136 u32 pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01004137 int ret = IRQ_NONE, pipe;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02004138 u32 flip_mask =
4139 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4140 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004141
Chris Wilsona266c7d2012-04-24 22:59:44 +01004142 iir = I915_READ(IIR);
4143
Chris Wilsona266c7d2012-04-24 22:59:44 +01004144 for (;;) {
Ville Syrjälä501e01d2014-01-17 11:35:15 +02004145 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson2c8ba292012-04-24 22:59:46 +01004146 bool blc_event = false;
4147
Chris Wilsona266c7d2012-04-24 22:59:44 +01004148 /* Can't rely on pipestat interrupt bit in iir as it might
4149 * have been cleared after the pipestat interrupt was received.
4150 * It doesn't set the bit in iir again, but it still produces
4151 * interrupts (for non-MSI).
4152 */
Daniel Vetter222c7f52014-09-15 14:55:28 +02004153 spin_lock(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004154 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Mika Kuoppala58174462014-02-25 17:11:26 +02004155 i915_handle_error(dev, false,
4156 "Command parser error, iir 0x%08x",
4157 iir);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004158
Damien Lespiau055e3932014-08-18 13:49:10 +01004159 for_each_pipe(dev_priv, pipe) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004160 int reg = PIPESTAT(pipe);
4161 pipe_stats[pipe] = I915_READ(reg);
4162
4163 /*
4164 * Clear the PIPE*STAT regs before the IIR
4165 */
4166 if (pipe_stats[pipe] & 0x8000ffff) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004167 I915_WRITE(reg, pipe_stats[pipe]);
Ville Syrjälä501e01d2014-01-17 11:35:15 +02004168 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004169 }
4170 }
Daniel Vetter222c7f52014-09-15 14:55:28 +02004171 spin_unlock(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004172
4173 if (!irq_received)
4174 break;
4175
4176 ret = IRQ_HANDLED;
4177
4178 /* Consume port. Then clear IIR or we'll miss events */
Ville Syrjälä16c6c562014-04-01 10:54:36 +03004179 if (iir & I915_DISPLAY_PORT_INTERRUPT)
4180 i9xx_hpd_irq_handler(dev);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004181
Ville Syrjälä21ad8332013-02-19 15:16:39 +02004182 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004183 new_iir = I915_READ(IIR); /* Flush posted writes */
4184
Chris Wilsona266c7d2012-04-24 22:59:44 +01004185 if (iir & I915_USER_INTERRUPT)
4186 notify_ring(dev, &dev_priv->ring[RCS]);
4187 if (iir & I915_BSD_USER_INTERRUPT)
4188 notify_ring(dev, &dev_priv->ring[VCS]);
4189
Damien Lespiau055e3932014-08-18 13:49:10 +01004190 for_each_pipe(dev_priv, pipe) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01004191 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02004192 i915_handle_vblank(dev, pipe, pipe, iir))
4193 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004194
4195 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4196 blc_event = true;
Daniel Vetter4356d582013-10-16 22:55:55 +02004197
4198 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
Daniel Vetter277de952013-10-18 16:37:07 +02004199 i9xx_pipe_crc_irq_handler(dev, pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004200
Daniel Vetter1f7247c2014-09-30 10:56:48 +02004201 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
4202 intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004203 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01004204
4205 if (blc_event || (iir & I915_ASLE_INTERRUPT))
4206 intel_opregion_asle_intr(dev);
4207
Daniel Vetter515ac2b2012-12-01 13:53:44 +01004208 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4209 gmbus_irq_handler(dev);
4210
Chris Wilsona266c7d2012-04-24 22:59:44 +01004211 /* With MSI, interrupts are only generated when iir
4212 * transitions from zero to nonzero. If another bit got
4213 * set while we were handling the existing iir bits, then
4214 * we would never get another interrupt.
4215 *
4216 * This is fine on non-MSI as well, as if we hit this path
4217 * we avoid exiting the interrupt handler only to generate
4218 * another one.
4219 *
4220 * Note that for MSI this could cause a stray interrupt report
4221 * if an interrupt landed in the time between writing IIR and
4222 * the posting read. This should be rare enough to never
4223 * trigger the 99% of 100,000 interrupts test for disabling
4224 * stray interrupts.
4225 */
4226 iir = new_iir;
4227 }
4228
Daniel Vetterd05c6172012-04-26 23:28:09 +02004229 i915_update_dri1_breadcrumb(dev);
Chris Wilson2c8ba292012-04-24 22:59:46 +01004230
Chris Wilsona266c7d2012-04-24 22:59:44 +01004231 return ret;
4232}
4233
4234static void i965_irq_uninstall(struct drm_device * dev)
4235{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004236 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004237 int pipe;
4238
4239 if (!dev_priv)
4240 return;
4241
Chris Wilsonadca4732012-05-11 18:01:31 +01004242 I915_WRITE(PORT_HOTPLUG_EN, 0);
4243 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01004244
4245 I915_WRITE(HWSTAM, 0xffffffff);
Damien Lespiau055e3932014-08-18 13:49:10 +01004246 for_each_pipe(dev_priv, pipe)
Chris Wilsona266c7d2012-04-24 22:59:44 +01004247 I915_WRITE(PIPESTAT(pipe), 0);
4248 I915_WRITE(IMR, 0xffffffff);
4249 I915_WRITE(IER, 0x0);
4250
Damien Lespiau055e3932014-08-18 13:49:10 +01004251 for_each_pipe(dev_priv, pipe)
Chris Wilsona266c7d2012-04-24 22:59:44 +01004252 I915_WRITE(PIPESTAT(pipe),
4253 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4254 I915_WRITE(IIR, I915_READ(IIR));
4255}
4256
Daniel Vetter4cb21832014-09-15 14:55:26 +02004257static void intel_hpd_irq_reenable_work(struct work_struct *work)
Egbert Eichac4c16c2013-04-16 13:36:58 +02004258{
Imre Deak63237512014-08-18 15:37:02 +03004259 struct drm_i915_private *dev_priv =
4260 container_of(work, typeof(*dev_priv),
4261 hotplug_reenable_work.work);
Egbert Eichac4c16c2013-04-16 13:36:58 +02004262 struct drm_device *dev = dev_priv->dev;
4263 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichac4c16c2013-04-16 13:36:58 +02004264 int i;
4265
Imre Deak63237512014-08-18 15:37:02 +03004266 intel_runtime_pm_get(dev_priv);
4267
Daniel Vetter4cb21832014-09-15 14:55:26 +02004268 spin_lock_irq(&dev_priv->irq_lock);
Egbert Eichac4c16c2013-04-16 13:36:58 +02004269 for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4270 struct drm_connector *connector;
4271
4272 if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4273 continue;
4274
4275 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4276
4277 list_for_each_entry(connector, &mode_config->connector_list, head) {
4278 struct intel_connector *intel_connector = to_intel_connector(connector);
4279
4280 if (intel_connector->encoder->hpd_pin == i) {
4281 if (connector->polled != intel_connector->polled)
4282 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03004283 connector->name);
Egbert Eichac4c16c2013-04-16 13:36:58 +02004284 connector->polled = intel_connector->polled;
4285 if (!connector->polled)
4286 connector->polled = DRM_CONNECTOR_POLL_HPD;
4287 }
4288 }
4289 }
4290 if (dev_priv->display.hpd_irq_setup)
4291 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetter4cb21832014-09-15 14:55:26 +02004292 spin_unlock_irq(&dev_priv->irq_lock);
Imre Deak63237512014-08-18 15:37:02 +03004293
4294 intel_runtime_pm_put(dev_priv);
Egbert Eichac4c16c2013-04-16 13:36:58 +02004295}
4296
Daniel Vetterfca52a52014-09-30 10:56:45 +02004297/**
4298 * intel_irq_init - initializes irq support
4299 * @dev_priv: i915 device instance
4300 *
4301 * This function initializes all the irq support including work items, timers
4302 * and all the vtables. It does not setup the interrupt itself though.
4303 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004304void intel_irq_init(struct drm_i915_private *dev_priv)
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004305{
Daniel Vetterb9632912014-09-30 10:56:44 +02004306 struct drm_device *dev = dev_priv->dev;
Chris Wilson8b2e3262012-04-24 22:59:41 +01004307
4308 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Dave Airlie13cf5502014-06-18 11:29:35 +10004309 INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
Daniel Vetter99584db2012-11-14 17:14:04 +01004310 INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02004311 INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004312 INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
Chris Wilson8b2e3262012-04-24 22:59:41 +01004313
Deepak Sa6706b42014-03-15 20:23:22 +05304314 /* Let's track the enabled rps events */
Daniel Vetterb9632912014-09-30 10:56:44 +02004315 if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
Ville Syrjälä6c65a582014-08-29 14:14:07 +03004316 /* WaGsvRC0ResidencyMethod:vlv */
Deepak S31685c22014-07-03 17:33:01 -04004317 dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
4318 else
4319 dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
Deepak Sa6706b42014-03-15 20:23:22 +05304320
Daniel Vetter99584db2012-11-14 17:14:04 +01004321 setup_timer(&dev_priv->gpu_error.hangcheck_timer,
4322 i915_hangcheck_elapsed,
Daniel Vetter61bac782012-12-01 21:03:21 +01004323 (unsigned long) dev);
Imre Deak63237512014-08-18 15:37:02 +03004324 INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
Daniel Vetter4cb21832014-09-15 14:55:26 +02004325 intel_hpd_irq_reenable_work);
Daniel Vetter61bac782012-12-01 21:03:21 +01004326
Tomas Janousek97a19a22012-12-08 13:48:13 +01004327 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01004328
Daniel Vetterb9632912014-09-30 10:56:44 +02004329 if (IS_GEN2(dev_priv)) {
Ville Syrjälä4cdb83e2013-10-11 21:52:44 +03004330 dev->max_vblank_count = 0;
4331 dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
Daniel Vetterb9632912014-09-30 10:56:44 +02004332 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004333 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4334 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Ville Syrjälä391f75e2013-09-25 19:55:26 +03004335 } else {
4336 dev->driver->get_vblank_counter = i915_get_vblank_counter;
4337 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004338 }
4339
Ville Syrjälä21da2702014-08-06 14:49:55 +03004340 /*
4341 * Opt out of the vblank disable timer on everything except gen2.
4342 * Gen2 doesn't have a hardware frame counter and so depends on
4343 * vblank interrupts to produce sane vblank seuquence numbers.
4344 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004345 if (!IS_GEN2(dev_priv))
Ville Syrjälä21da2702014-08-06 14:49:55 +03004346 dev->vblank_disable_immediate = true;
4347
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +03004348 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Keith Packardc3613de2011-08-12 17:05:54 -07004349 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +03004350 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4351 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004352
Daniel Vetterb9632912014-09-30 10:56:44 +02004353 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä43f328d2014-04-09 20:40:52 +03004354 dev->driver->irq_handler = cherryview_irq_handler;
4355 dev->driver->irq_preinstall = cherryview_irq_preinstall;
4356 dev->driver->irq_postinstall = cherryview_irq_postinstall;
4357 dev->driver->irq_uninstall = cherryview_irq_uninstall;
4358 dev->driver->enable_vblank = valleyview_enable_vblank;
4359 dev->driver->disable_vblank = valleyview_disable_vblank;
4360 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Daniel Vetterb9632912014-09-30 10:56:44 +02004361 } else if (IS_VALLEYVIEW(dev_priv)) {
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07004362 dev->driver->irq_handler = valleyview_irq_handler;
4363 dev->driver->irq_preinstall = valleyview_irq_preinstall;
4364 dev->driver->irq_postinstall = valleyview_irq_postinstall;
4365 dev->driver->irq_uninstall = valleyview_irq_uninstall;
4366 dev->driver->enable_vblank = valleyview_enable_vblank;
4367 dev->driver->disable_vblank = valleyview_disable_vblank;
Egbert Eichfa00abe2013-02-25 12:06:48 -05004368 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Daniel Vetterb9632912014-09-30 10:56:44 +02004369 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawskyabd58f02013-11-02 21:07:09 -07004370 dev->driver->irq_handler = gen8_irq_handler;
Daniel Vetter723761b2014-05-22 17:56:34 +02004371 dev->driver->irq_preinstall = gen8_irq_reset;
Ben Widawskyabd58f02013-11-02 21:07:09 -07004372 dev->driver->irq_postinstall = gen8_irq_postinstall;
4373 dev->driver->irq_uninstall = gen8_irq_uninstall;
4374 dev->driver->enable_vblank = gen8_enable_vblank;
4375 dev->driver->disable_vblank = gen8_disable_vblank;
4376 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004377 } else if (HAS_PCH_SPLIT(dev)) {
4378 dev->driver->irq_handler = ironlake_irq_handler;
Daniel Vetter723761b2014-05-22 17:56:34 +02004379 dev->driver->irq_preinstall = ironlake_irq_reset;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004380 dev->driver->irq_postinstall = ironlake_irq_postinstall;
4381 dev->driver->irq_uninstall = ironlake_irq_uninstall;
4382 dev->driver->enable_vblank = ironlake_enable_vblank;
4383 dev->driver->disable_vblank = ironlake_disable_vblank;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01004384 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004385 } else {
Daniel Vetterb9632912014-09-30 10:56:44 +02004386 if (INTEL_INFO(dev_priv)->gen == 2) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01004387 dev->driver->irq_preinstall = i8xx_irq_preinstall;
4388 dev->driver->irq_postinstall = i8xx_irq_postinstall;
4389 dev->driver->irq_handler = i8xx_irq_handler;
4390 dev->driver->irq_uninstall = i8xx_irq_uninstall;
Daniel Vetterb9632912014-09-30 10:56:44 +02004391 } else if (INTEL_INFO(dev_priv)->gen == 3) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004392 dev->driver->irq_preinstall = i915_irq_preinstall;
4393 dev->driver->irq_postinstall = i915_irq_postinstall;
4394 dev->driver->irq_uninstall = i915_irq_uninstall;
4395 dev->driver->irq_handler = i915_irq_handler;
Daniel Vetter20afbda2012-12-11 14:05:07 +01004396 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01004397 } else {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004398 dev->driver->irq_preinstall = i965_irq_preinstall;
4399 dev->driver->irq_postinstall = i965_irq_postinstall;
4400 dev->driver->irq_uninstall = i965_irq_uninstall;
4401 dev->driver->irq_handler = i965_irq_handler;
Egbert Eichbac56d52013-02-25 12:06:51 -05004402 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01004403 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004404 dev->driver->enable_vblank = i915_enable_vblank;
4405 dev->driver->disable_vblank = i915_disable_vblank;
4406 }
4407}
Daniel Vetter20afbda2012-12-11 14:05:07 +01004408
Daniel Vetterfca52a52014-09-30 10:56:45 +02004409/**
4410 * intel_hpd_init - initializes and enables hpd support
4411 * @dev_priv: i915 device instance
4412 *
4413 * This function enables the hotplug support. It requires that interrupts have
4414 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
4415 * poll request can run concurrently to other code, so locking rules must be
4416 * obeyed.
4417 *
4418 * This is a separate step from interrupt enabling to simplify the locking rules
4419 * in the driver load and resume code.
4420 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004421void intel_hpd_init(struct drm_i915_private *dev_priv)
Daniel Vetter20afbda2012-12-11 14:05:07 +01004422{
Daniel Vetterb9632912014-09-30 10:56:44 +02004423 struct drm_device *dev = dev_priv->dev;
Egbert Eich821450c2013-04-16 13:36:55 +02004424 struct drm_mode_config *mode_config = &dev->mode_config;
4425 struct drm_connector *connector;
4426 int i;
Daniel Vetter20afbda2012-12-11 14:05:07 +01004427
Egbert Eich821450c2013-04-16 13:36:55 +02004428 for (i = 1; i < HPD_NUM_PINS; i++) {
4429 dev_priv->hpd_stats[i].hpd_cnt = 0;
4430 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4431 }
4432 list_for_each_entry(connector, &mode_config->connector_list, head) {
4433 struct intel_connector *intel_connector = to_intel_connector(connector);
4434 connector->polled = intel_connector->polled;
Dave Airlie0e32b392014-05-02 14:02:48 +10004435 if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
4436 connector->polled = DRM_CONNECTOR_POLL_HPD;
4437 if (intel_connector->mst_port)
Egbert Eich821450c2013-04-16 13:36:55 +02004438 connector->polled = DRM_CONNECTOR_POLL_HPD;
4439 }
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02004440
4441 /* Interrupt setup is already guaranteed to be single-threaded, this is
4442 * just to make the assert_spin_locked checks happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02004443 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetter20afbda2012-12-11 14:05:07 +01004444 if (dev_priv->display.hpd_irq_setup)
4445 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterd6207432014-09-15 14:55:27 +02004446 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter20afbda2012-12-11 14:05:07 +01004447}
Paulo Zanonic67a4702013-08-19 13:18:09 -03004448
Daniel Vetterfca52a52014-09-30 10:56:45 +02004449/**
4450 * intel_irq_install - enables the hardware interrupt
4451 * @dev_priv: i915 device instance
4452 *
4453 * This function enables the hardware interrupt handling, but leaves the hotplug
4454 * handling still disabled. It is called after intel_irq_init().
4455 *
4456 * In the driver load and resume code we need working interrupts in a few places
4457 * but don't want to deal with the hassle of concurrent probe and hotplug
4458 * workers. Hence the split into this two-stage approach.
4459 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +02004460int intel_irq_install(struct drm_i915_private *dev_priv)
4461{
4462 /*
4463 * We enable some interrupt sources in our postinstall hooks, so mark
4464 * interrupts as enabled _before_ actually enabling them to avoid
4465 * special cases in our ordering checks.
4466 */
4467 dev_priv->pm.irqs_enabled = true;
4468
4469 return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
4470}
4471
Daniel Vetterfca52a52014-09-30 10:56:45 +02004472/**
4473 * intel_irq_uninstall - finilizes all irq handling
4474 * @dev_priv: i915 device instance
4475 *
4476 * This stops interrupt and hotplug handling and unregisters and frees all
4477 * resources acquired in the init functions.
4478 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +02004479void intel_irq_uninstall(struct drm_i915_private *dev_priv)
4480{
4481 drm_irq_uninstall(dev_priv->dev);
4482 intel_hpd_cancel_work(dev_priv);
4483 dev_priv->pm.irqs_enabled = false;
4484}
4485
Daniel Vetterfca52a52014-09-30 10:56:45 +02004486/**
4487 * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4488 * @dev_priv: i915 device instance
4489 *
4490 * This function is used to disable interrupts at runtime, both in the runtime
4491 * pm and the system suspend/resume code.
4492 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004493void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03004494{
Daniel Vetterb9632912014-09-30 10:56:44 +02004495 dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
Daniel Vetter2aeb7d32014-09-30 10:56:43 +02004496 dev_priv->pm.irqs_enabled = false;
Paulo Zanonic67a4702013-08-19 13:18:09 -03004497}
4498
Daniel Vetterfca52a52014-09-30 10:56:45 +02004499/**
4500 * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4501 * @dev_priv: i915 device instance
4502 *
4503 * This function is used to enable interrupts at runtime, both in the runtime
4504 * pm and the system suspend/resume code.
4505 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004506void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03004507{
Daniel Vetter2aeb7d32014-09-30 10:56:43 +02004508 dev_priv->pm.irqs_enabled = true;
Daniel Vetterb9632912014-09-30 10:56:44 +02004509 dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4510 dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03004511}