blob: 8d169e152d1edcb8d9b6093aa431cbcebd5db754 [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 Deak3cc134e2014-11-19 15:30:03 +0200258void gen6_reset_rps_interrupts(struct drm_device *dev)
259{
260 struct drm_i915_private *dev_priv = dev->dev_private;
261 uint32_t reg = gen6_pm_iir(dev_priv);
262
263 spin_lock_irq(&dev_priv->irq_lock);
264 I915_WRITE(reg, dev_priv->pm_rps_events);
265 I915_WRITE(reg, dev_priv->pm_rps_events);
266 POSTING_READ(reg);
267 spin_unlock_irq(&dev_priv->irq_lock);
268}
269
Imre Deakb900b942014-11-05 20:48:48 +0200270void gen6_enable_rps_interrupts(struct drm_device *dev)
271{
272 struct drm_i915_private *dev_priv = dev->dev_private;
273
274 spin_lock_irq(&dev_priv->irq_lock);
275 WARN_ON(dev_priv->rps.pm_iir);
Imre Deak3cc134e2014-11-19 15:30:03 +0200276 WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
Imre Deakd4d70aa2014-11-19 15:30:04 +0200277 dev_priv->rps.interrupts_enabled = true;
Imre Deakb900b942014-11-05 20:48:48 +0200278 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Imre Deakb900b942014-11-05 20:48:48 +0200279 spin_unlock_irq(&dev_priv->irq_lock);
280}
281
282void gen6_disable_rps_interrupts(struct drm_device *dev)
283{
284 struct drm_i915_private *dev_priv = dev->dev_private;
285
Imre Deakd4d70aa2014-11-19 15:30:04 +0200286 spin_lock_irq(&dev_priv->irq_lock);
287 dev_priv->rps.interrupts_enabled = false;
288 spin_unlock_irq(&dev_priv->irq_lock);
289
290 cancel_work_sync(&dev_priv->rps.work);
291
Imre Deakb900b942014-11-05 20:48:48 +0200292 I915_WRITE(GEN6_PMINTRMSK, INTEL_INFO(dev_priv)->gen >= 8 ?
293 ~GEN8_PMINTR_REDIRECT_TO_NON_DISP : ~0);
294 I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
295 ~dev_priv->pm_rps_events);
Imre Deakb900b942014-11-05 20:48:48 +0200296
297 spin_lock_irq(&dev_priv->irq_lock);
298 dev_priv->rps.pm_iir = 0;
299 spin_unlock_irq(&dev_priv->irq_lock);
300
301 I915_WRITE(gen6_pm_iir(dev_priv), dev_priv->pm_rps_events);
302}
303
Ben Widawsky09610212014-05-15 20:58:08 +0300304/**
Daniel Vetterfee884e2013-07-04 23:35:21 +0200305 * ibx_display_interrupt_update - update SDEIMR
306 * @dev_priv: driver private
307 * @interrupt_mask: mask of interrupt bits to update
308 * @enabled_irq_mask: mask of interrupt bits to enable
309 */
Daniel Vetter47339cd2014-09-30 10:56:46 +0200310void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
311 uint32_t interrupt_mask,
312 uint32_t enabled_irq_mask)
Daniel Vetterfee884e2013-07-04 23:35:21 +0200313{
314 uint32_t sdeimr = I915_READ(SDEIMR);
315 sdeimr &= ~interrupt_mask;
316 sdeimr |= (~enabled_irq_mask & interrupt_mask);
317
318 assert_spin_locked(&dev_priv->irq_lock);
319
Jesse Barnes9df7575f2014-06-20 09:29:20 -0700320 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300321 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300322
Daniel Vetterfee884e2013-07-04 23:35:21 +0200323 I915_WRITE(SDEIMR, sdeimr);
324 POSTING_READ(SDEIMR);
325}
Paulo Zanoni86642812013-04-12 17:57:57 -0300326
Daniel Vetterb5ea6422014-03-02 21:18:00 +0100327static void
Imre Deak755e9012014-02-10 18:42:47 +0200328__i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
329 u32 enable_mask, u32 status_mask)
Keith Packard7c463582008-11-04 02:03:27 -0800330{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200331 u32 reg = PIPESTAT(pipe);
Imre Deak755e9012014-02-10 18:42:47 +0200332 u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
Keith Packard7c463582008-11-04 02:03:27 -0800333
Daniel Vetterb79480b2013-06-27 17:52:10 +0200334 assert_spin_locked(&dev_priv->irq_lock);
Daniel Vetterd518ce52014-08-27 10:43:37 +0200335 WARN_ON(!intel_irqs_enabled(dev_priv));
Daniel Vetterb79480b2013-06-27 17:52:10 +0200336
Ville Syrjälä04feced2014-04-03 13:28:33 +0300337 if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
338 status_mask & ~PIPESTAT_INT_STATUS_MASK,
339 "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
340 pipe_name(pipe), enable_mask, status_mask))
Imre Deak755e9012014-02-10 18:42:47 +0200341 return;
342
343 if ((pipestat & enable_mask) == enable_mask)
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200344 return;
345
Imre Deak91d181d2014-02-10 18:42:49 +0200346 dev_priv->pipestat_irq_mask[pipe] |= status_mask;
347
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200348 /* Enable the interrupt, clear any pending status */
Imre Deak755e9012014-02-10 18:42:47 +0200349 pipestat |= enable_mask | status_mask;
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200350 I915_WRITE(reg, pipestat);
351 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800352}
353
Daniel Vetterb5ea6422014-03-02 21:18:00 +0100354static void
Imre Deak755e9012014-02-10 18:42:47 +0200355__i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
356 u32 enable_mask, u32 status_mask)
Keith Packard7c463582008-11-04 02:03:27 -0800357{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200358 u32 reg = PIPESTAT(pipe);
Imre Deak755e9012014-02-10 18:42:47 +0200359 u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
Keith Packard7c463582008-11-04 02:03:27 -0800360
Daniel Vetterb79480b2013-06-27 17:52:10 +0200361 assert_spin_locked(&dev_priv->irq_lock);
Daniel Vetterd518ce52014-08-27 10:43:37 +0200362 WARN_ON(!intel_irqs_enabled(dev_priv));
Daniel Vetterb79480b2013-06-27 17:52:10 +0200363
Ville Syrjälä04feced2014-04-03 13:28:33 +0300364 if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
365 status_mask & ~PIPESTAT_INT_STATUS_MASK,
366 "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
367 pipe_name(pipe), enable_mask, status_mask))
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200368 return;
369
Imre Deak755e9012014-02-10 18:42:47 +0200370 if ((pipestat & enable_mask) == 0)
371 return;
372
Imre Deak91d181d2014-02-10 18:42:49 +0200373 dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
374
Imre Deak755e9012014-02-10 18:42:47 +0200375 pipestat &= ~enable_mask;
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200376 I915_WRITE(reg, pipestat);
377 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800378}
379
Imre Deak10c59c52014-02-10 18:42:48 +0200380static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
381{
382 u32 enable_mask = status_mask << 16;
383
384 /*
Ville Syrjälä724a6902014-04-09 13:28:48 +0300385 * On pipe A we don't support the PSR interrupt yet,
386 * on pipe B and C the same bit MBZ.
Imre Deak10c59c52014-02-10 18:42:48 +0200387 */
388 if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
389 return 0;
Ville Syrjälä724a6902014-04-09 13:28:48 +0300390 /*
391 * On pipe B and C we don't support the PSR interrupt yet, on pipe
392 * A the same bit is for perf counters which we don't use either.
393 */
394 if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
395 return 0;
Imre Deak10c59c52014-02-10 18:42:48 +0200396
397 enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
398 SPRITE0_FLIP_DONE_INT_EN_VLV |
399 SPRITE1_FLIP_DONE_INT_EN_VLV);
400 if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
401 enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
402 if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
403 enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
404
405 return enable_mask;
406}
407
Imre Deak755e9012014-02-10 18:42:47 +0200408void
409i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
410 u32 status_mask)
411{
412 u32 enable_mask;
413
Imre Deak10c59c52014-02-10 18:42:48 +0200414 if (IS_VALLEYVIEW(dev_priv->dev))
415 enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
416 status_mask);
417 else
418 enable_mask = status_mask << 16;
Imre Deak755e9012014-02-10 18:42:47 +0200419 __i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
420}
421
422void
423i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
424 u32 status_mask)
425{
426 u32 enable_mask;
427
Imre Deak10c59c52014-02-10 18:42:48 +0200428 if (IS_VALLEYVIEW(dev_priv->dev))
429 enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
430 status_mask);
431 else
432 enable_mask = status_mask << 16;
Imre Deak755e9012014-02-10 18:42:47 +0200433 __i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
434}
435
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000436/**
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300437 * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
Zhao Yakui01c66882009-10-28 05:10:00 +0000438 */
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300439static void i915_enable_asle_pipestat(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +0000440{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300441 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000442
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300443 if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
444 return;
445
Daniel Vetter13321782014-09-15 14:55:29 +0200446 spin_lock_irq(&dev_priv->irq_lock);
Zhao Yakui01c66882009-10-28 05:10:00 +0000447
Imre Deak755e9012014-02-10 18:42:47 +0200448 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
Jani Nikulaf8987802013-04-29 13:02:53 +0300449 if (INTEL_INFO(dev)->gen >= 4)
Daniel Vetter3b6c42e2013-10-21 18:04:35 +0200450 i915_enable_pipestat(dev_priv, PIPE_A,
Imre Deak755e9012014-02-10 18:42:47 +0200451 PIPE_LEGACY_BLC_EVENT_STATUS);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000452
Daniel Vetter13321782014-09-15 14:55:29 +0200453 spin_unlock_irq(&dev_priv->irq_lock);
Zhao Yakui01c66882009-10-28 05:10:00 +0000454}
455
456/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700457 * i915_pipe_enabled - check if a pipe is enabled
458 * @dev: DRM device
459 * @pipe: pipe to check
460 *
461 * Reading certain registers when the pipe is disabled can hang the chip.
462 * Use this routine to make sure the PLL is running and the pipe is active
463 * before reading such registers if unsure.
464 */
465static int
466i915_pipe_enabled(struct drm_device *dev, int pipe)
467{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300468 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200469
Daniel Vettera01025a2013-05-22 00:50:23 +0200470 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
471 /* Locking is horribly broken here, but whatever. */
472 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni71f8ba62013-05-03 12:15:39 -0300474
Daniel Vettera01025a2013-05-22 00:50:23 +0200475 return intel_crtc->active;
476 } else {
477 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
478 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700479}
480
Ville Syrjäläf75f3742014-05-15 20:20:36 +0300481/*
482 * This timing diagram depicts the video signal in and
483 * around the vertical blanking period.
484 *
485 * Assumptions about the fictitious mode used in this example:
486 * vblank_start >= 3
487 * vsync_start = vblank_start + 1
488 * vsync_end = vblank_start + 2
489 * vtotal = vblank_start + 3
490 *
491 * start of vblank:
492 * latch double buffered registers
493 * increment frame counter (ctg+)
494 * generate start of vblank interrupt (gen4+)
495 * |
496 * | frame start:
497 * | generate frame start interrupt (aka. vblank interrupt) (gmch)
498 * | may be shifted forward 1-3 extra lines via PIPECONF
499 * | |
500 * | | start of vsync:
501 * | | generate vsync interrupt
502 * | | |
503 * ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx___ ___xxxx
504 * . \hs/ . \hs/ \hs/ \hs/ . \hs/
505 * ----va---> <-----------------vb--------------------> <--------va-------------
506 * | | <----vs-----> |
507 * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
508 * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
509 * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
510 * | | |
511 * last visible pixel first visible pixel
512 * | increment frame counter (gen3/4)
513 * pixel counter = vblank_start * htotal pixel counter = 0 (gen3/4)
514 *
515 * x = horizontal active
516 * _ = horizontal blanking
517 * hs = horizontal sync
518 * va = vertical active
519 * vb = vertical blanking
520 * vs = vertical sync
521 * vbs = vblank_start (number)
522 *
523 * Summary:
524 * - most events happen at the start of horizontal sync
525 * - frame start happens at the start of horizontal blank, 1-4 lines
526 * (depending on PIPECONF settings) after the start of vblank
527 * - gen3/4 pixel and frame counter are synchronized with the start
528 * of horizontal active on the first line of vertical active
529 */
530
Ville Syrjälä4cdb83e2013-10-11 21:52:44 +0300531static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
532{
533 /* Gen2 doesn't have a hardware frame counter */
534 return 0;
535}
536
Keith Packard42f52ef2008-10-18 19:39:29 -0700537/* Called from drm generic code, passed a 'crtc', which
538 * we use as a pipe index
539 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700540static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700541{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300542 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700543 unsigned long high_frame;
544 unsigned long low_frame;
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300545 u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700546
547 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800548 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800549 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700550 return 0;
551 }
552
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300553 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
554 struct intel_crtc *intel_crtc =
555 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
556 const struct drm_display_mode *mode =
557 &intel_crtc->config.adjusted_mode;
558
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300559 htotal = mode->crtc_htotal;
560 hsync_start = mode->crtc_hsync_start;
561 vbl_start = mode->crtc_vblank_start;
562 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
563 vbl_start = DIV_ROUND_UP(vbl_start, 2);
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300564 } else {
Daniel Vettera2d213d2014-02-07 16:34:05 +0100565 enum transcoder cpu_transcoder = (enum transcoder) pipe;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300566
567 htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300568 hsync_start = (I915_READ(HSYNC(cpu_transcoder)) & 0x1fff) + 1;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300569 vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300570 if ((I915_READ(PIPECONF(cpu_transcoder)) &
571 PIPECONF_INTERLACE_MASK) != PIPECONF_PROGRESSIVE)
572 vbl_start = DIV_ROUND_UP(vbl_start, 2);
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300573 }
574
Ville Syrjälä0b2a8e02014-04-29 13:35:50 +0300575 /* Convert to pixel count */
576 vbl_start *= htotal;
577
578 /* Start of vblank event occurs at start of hsync */
579 vbl_start -= htotal - hsync_start;
580
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800581 high_frame = PIPEFRAME(pipe);
582 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100583
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700584 /*
585 * High & low register fields aren't synchronized, so make sure
586 * we get a low value that's stable across two reads of the high
587 * register.
588 */
589 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100590 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300591 low = I915_READ(low_frame);
Chris Wilson5eddb702010-09-11 13:48:45 +0100592 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700593 } while (high1 != high2);
594
Chris Wilson5eddb702010-09-11 13:48:45 +0100595 high1 >>= PIPE_FRAME_HIGH_SHIFT;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300596 pixel = low & PIPE_PIXEL_MASK;
Chris Wilson5eddb702010-09-11 13:48:45 +0100597 low >>= PIPE_FRAME_LOW_SHIFT;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300598
599 /*
600 * The frame counter increments at beginning of active.
601 * Cook up a vblank counter by also checking the pixel
602 * counter against vblank start.
603 */
Ville Syrjäläedc08d02013-11-06 13:56:27 -0200604 return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700605}
606
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700607static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800608{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300609 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800610 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800611
612 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800613 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800614 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800615 return 0;
616 }
617
618 return I915_READ(reg);
619}
620
Mario Kleinerad3543e2013-10-30 05:13:08 +0100621/* raw reads, only for fast reads of display block, no need for forcewake etc. */
622#define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
Mario Kleinerad3543e2013-10-30 05:13:08 +0100623
Ville Syrjäläa225f072014-04-29 13:35:45 +0300624static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
625{
626 struct drm_device *dev = crtc->base.dev;
627 struct drm_i915_private *dev_priv = dev->dev_private;
628 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
629 enum pipe pipe = crtc->pipe;
Ville Syrjälä80715b22014-05-15 20:23:23 +0300630 int position, vtotal;
Ville Syrjäläa225f072014-04-29 13:35:45 +0300631
Ville Syrjälä80715b22014-05-15 20:23:23 +0300632 vtotal = mode->crtc_vtotal;
Ville Syrjäläa225f072014-04-29 13:35:45 +0300633 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
634 vtotal /= 2;
635
636 if (IS_GEN2(dev))
637 position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
638 else
639 position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
640
641 /*
Ville Syrjälä80715b22014-05-15 20:23:23 +0300642 * See update_scanline_offset() for the details on the
643 * scanline_offset adjustment.
Ville Syrjäläa225f072014-04-29 13:35:45 +0300644 */
Ville Syrjälä80715b22014-05-15 20:23:23 +0300645 return (position + crtc->scanline_offset) % vtotal;
Ville Syrjäläa225f072014-04-29 13:35:45 +0300646}
647
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700648static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200649 unsigned int flags, int *vpos, int *hpos,
650 ktime_t *stime, ktime_t *etime)
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100651{
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300652 struct drm_i915_private *dev_priv = dev->dev_private;
653 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
655 const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300656 int position;
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300657 int vbl_start, vbl_end, hsync_start, htotal, vtotal;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100658 bool in_vbl = true;
659 int ret = 0;
Mario Kleinerad3543e2013-10-30 05:13:08 +0100660 unsigned long irqflags;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100661
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300662 if (!intel_crtc->active) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100663 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800664 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100665 return 0;
666 }
667
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300668 htotal = mode->crtc_htotal;
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300669 hsync_start = mode->crtc_hsync_start;
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300670 vtotal = mode->crtc_vtotal;
671 vbl_start = mode->crtc_vblank_start;
672 vbl_end = mode->crtc_vblank_end;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100673
Ville Syrjäläd31faf62013-10-28 16:31:41 +0200674 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
675 vbl_start = DIV_ROUND_UP(vbl_start, 2);
676 vbl_end /= 2;
677 vtotal /= 2;
678 }
679
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300680 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
681
Mario Kleinerad3543e2013-10-30 05:13:08 +0100682 /*
683 * Lock uncore.lock, as we will do multiple timing critical raw
684 * register reads, potentially with preemption disabled, so the
685 * following code must not block on uncore.lock.
686 */
687 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300688
Mario Kleinerad3543e2013-10-30 05:13:08 +0100689 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
690
691 /* Get optional system timestamp before query. */
692 if (stime)
693 *stime = ktime_get();
694
Ville Syrjälä7c06b082013-10-11 21:52:43 +0300695 if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100696 /* No obvious pixelcount register. Only query vertical
697 * scanout position from Display scan line register.
698 */
Ville Syrjäläa225f072014-04-29 13:35:45 +0300699 position = __intel_get_crtc_scanline(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100700 } else {
701 /* Have access to pixelcount since start of frame.
702 * We can split this into vertical and horizontal
703 * scanout position.
704 */
Mario Kleinerad3543e2013-10-30 05:13:08 +0100705 position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100706
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300707 /* convert to pixel counts */
708 vbl_start *= htotal;
709 vbl_end *= htotal;
710 vtotal *= htotal;
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300711
712 /*
Ville Syrjälä7e78f1cb2014-04-29 13:35:49 +0300713 * In interlaced modes, the pixel counter counts all pixels,
714 * so one field will have htotal more pixels. In order to avoid
715 * the reported position from jumping backwards when the pixel
716 * counter is beyond the length of the shorter field, just
717 * clamp the position the length of the shorter field. This
718 * matches how the scanline counter based position works since
719 * the scanline counter doesn't count the two half lines.
720 */
721 if (position >= vtotal)
722 position = vtotal - 1;
723
724 /*
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300725 * Start of vblank interrupt is triggered at start of hsync,
726 * just prior to the first active line of vblank. However we
727 * consider lines to start at the leading edge of horizontal
728 * active. So, should we get here before we've crossed into
729 * the horizontal active of the first line in vblank, we would
730 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
731 * always add htotal-hsync_start to the current pixel position.
732 */
733 position = (position + htotal - hsync_start) % vtotal;
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300734 }
735
Mario Kleinerad3543e2013-10-30 05:13:08 +0100736 /* Get optional system timestamp after query. */
737 if (etime)
738 *etime = ktime_get();
739
740 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
741
742 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
743
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300744 in_vbl = position >= vbl_start && position < vbl_end;
745
746 /*
747 * While in vblank, position will be negative
748 * counting up towards 0 at vbl_end. And outside
749 * vblank, position will be positive counting
750 * up since vbl_end.
751 */
752 if (position >= vbl_start)
753 position -= vbl_end;
754 else
755 position += vtotal - vbl_end;
756
Ville Syrjälä7c06b082013-10-11 21:52:43 +0300757 if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300758 *vpos = position;
759 *hpos = 0;
760 } else {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100761 *vpos = position / htotal;
762 *hpos = position - (*vpos * htotal);
763 }
764
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100765 /* In vblank? */
766 if (in_vbl)
Daniel Vetter3d3cbd82014-09-10 17:36:11 +0200767 ret |= DRM_SCANOUTPOS_IN_VBLANK;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100768
769 return ret;
770}
771
Ville Syrjäläa225f072014-04-29 13:35:45 +0300772int intel_get_crtc_scanline(struct intel_crtc *crtc)
773{
774 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
775 unsigned long irqflags;
776 int position;
777
778 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
779 position = __intel_get_crtc_scanline(crtc);
780 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
781
782 return position;
783}
784
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700785static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100786 int *max_error,
787 struct timeval *vblank_time,
788 unsigned flags)
789{
Chris Wilson4041b852011-01-22 10:07:56 +0000790 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100791
Ben Widawsky7eb552a2013-03-13 14:05:41 -0700792 if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
Chris Wilson4041b852011-01-22 10:07:56 +0000793 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100794 return -EINVAL;
795 }
796
797 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000798 crtc = intel_get_crtc_for_pipe(dev, pipe);
799 if (crtc == NULL) {
800 DRM_ERROR("Invalid crtc %d\n", pipe);
801 return -EINVAL;
802 }
803
804 if (!crtc->enabled) {
805 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
806 return -EBUSY;
807 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100808
809 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000810 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
811 vblank_time, flags,
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300812 crtc,
813 &to_intel_crtc(crtc)->config.adjusted_mode);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100814}
815
Jani Nikula67c347f2013-09-17 14:26:34 +0300816static bool intel_hpd_irq_event(struct drm_device *dev,
817 struct drm_connector *connector)
Egbert Eich321a1b32013-04-11 16:00:26 +0200818{
819 enum drm_connector_status old_status;
820
821 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
822 old_status = connector->status;
823
824 connector->status = connector->funcs->detect(connector, false);
Jani Nikula67c347f2013-09-17 14:26:34 +0300825 if (old_status == connector->status)
826 return false;
827
828 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
Egbert Eich321a1b32013-04-11 16:00:26 +0200829 connector->base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +0300830 connector->name,
Jani Nikula67c347f2013-09-17 14:26:34 +0300831 drm_get_connector_status_name(old_status),
832 drm_get_connector_status_name(connector->status));
833
834 return true;
Egbert Eich321a1b32013-04-11 16:00:26 +0200835}
836
Dave Airlie13cf5502014-06-18 11:29:35 +1000837static void i915_digport_work_func(struct work_struct *work)
838{
839 struct drm_i915_private *dev_priv =
840 container_of(work, struct drm_i915_private, dig_port_work);
Dave Airlie13cf5502014-06-18 11:29:35 +1000841 u32 long_port_mask, short_port_mask;
842 struct intel_digital_port *intel_dig_port;
843 int i, ret;
844 u32 old_bits = 0;
845
Daniel Vetter4cb21832014-09-15 14:55:26 +0200846 spin_lock_irq(&dev_priv->irq_lock);
Dave Airlie13cf5502014-06-18 11:29:35 +1000847 long_port_mask = dev_priv->long_hpd_port_mask;
848 dev_priv->long_hpd_port_mask = 0;
849 short_port_mask = dev_priv->short_hpd_port_mask;
850 dev_priv->short_hpd_port_mask = 0;
Daniel Vetter4cb21832014-09-15 14:55:26 +0200851 spin_unlock_irq(&dev_priv->irq_lock);
Dave Airlie13cf5502014-06-18 11:29:35 +1000852
853 for (i = 0; i < I915_MAX_PORTS; i++) {
854 bool valid = false;
855 bool long_hpd = false;
856 intel_dig_port = dev_priv->hpd_irq_port[i];
857 if (!intel_dig_port || !intel_dig_port->hpd_pulse)
858 continue;
859
860 if (long_port_mask & (1 << i)) {
861 valid = true;
862 long_hpd = true;
863 } else if (short_port_mask & (1 << i))
864 valid = true;
865
866 if (valid) {
867 ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
868 if (ret == true) {
869 /* if we get true fallback to old school hpd */
870 old_bits |= (1 << intel_dig_port->base.hpd_pin);
871 }
872 }
873 }
874
875 if (old_bits) {
Daniel Vetter4cb21832014-09-15 14:55:26 +0200876 spin_lock_irq(&dev_priv->irq_lock);
Dave Airlie13cf5502014-06-18 11:29:35 +1000877 dev_priv->hpd_event_bits |= old_bits;
Daniel Vetter4cb21832014-09-15 14:55:26 +0200878 spin_unlock_irq(&dev_priv->irq_lock);
Dave Airlie13cf5502014-06-18 11:29:35 +1000879 schedule_work(&dev_priv->hotplug_work);
880 }
881}
882
Jesse Barnes5ca58282009-03-31 14:11:15 -0700883/*
884 * Handle hotplug events outside the interrupt handler proper.
885 */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200886#define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
887
Jesse Barnes5ca58282009-03-31 14:11:15 -0700888static void i915_hotplug_work_func(struct work_struct *work)
889{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300890 struct drm_i915_private *dev_priv =
891 container_of(work, struct drm_i915_private, hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700892 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700893 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200894 struct intel_connector *intel_connector;
895 struct intel_encoder *intel_encoder;
896 struct drm_connector *connector;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200897 bool hpd_disabled = false;
Egbert Eich321a1b32013-04-11 16:00:26 +0200898 bool changed = false;
Egbert Eich142e2392013-04-11 15:57:57 +0200899 u32 hpd_event_bits;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700900
Keith Packarda65e34c2011-07-25 10:04:56 -0700901 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -0800902 DRM_DEBUG_KMS("running encoder hotplug functions\n");
903
Daniel Vetter4cb21832014-09-15 14:55:26 +0200904 spin_lock_irq(&dev_priv->irq_lock);
Egbert Eich142e2392013-04-11 15:57:57 +0200905
906 hpd_event_bits = dev_priv->hpd_event_bits;
907 dev_priv->hpd_event_bits = 0;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200908 list_for_each_entry(connector, &mode_config->connector_list, head) {
909 intel_connector = to_intel_connector(connector);
Dave Airlie36cd7442014-05-02 13:44:18 +1000910 if (!intel_connector->encoder)
911 continue;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200912 intel_encoder = intel_connector->encoder;
913 if (intel_encoder->hpd_pin > HPD_NONE &&
914 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
915 connector->polled == DRM_CONNECTOR_POLL_HPD) {
916 DRM_INFO("HPD interrupt storm detected on connector %s: "
917 "switching from hotplug detection to polling\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300918 connector->name);
Egbert Eichcd569ae2013-04-16 13:36:57 +0200919 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
920 connector->polled = DRM_CONNECTOR_POLL_CONNECT
921 | DRM_CONNECTOR_POLL_DISCONNECT;
922 hpd_disabled = true;
923 }
Egbert Eich142e2392013-04-11 15:57:57 +0200924 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
925 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300926 connector->name, intel_encoder->hpd_pin);
Egbert Eich142e2392013-04-11 15:57:57 +0200927 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200928 }
929 /* if there were no outputs to poll, poll was disabled,
930 * therefore make sure it's enabled when disabling HPD on
931 * some connectors */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200932 if (hpd_disabled) {
Egbert Eichcd569ae2013-04-16 13:36:57 +0200933 drm_kms_helper_poll_enable(dev);
Imre Deak63237512014-08-18 15:37:02 +0300934 mod_delayed_work(system_wq, &dev_priv->hotplug_reenable_work,
935 msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
Egbert Eichac4c16c2013-04-16 13:36:58 +0200936 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200937
Daniel Vetter4cb21832014-09-15 14:55:26 +0200938 spin_unlock_irq(&dev_priv->irq_lock);
Egbert Eichcd569ae2013-04-16 13:36:57 +0200939
Egbert Eich321a1b32013-04-11 16:00:26 +0200940 list_for_each_entry(connector, &mode_config->connector_list, head) {
941 intel_connector = to_intel_connector(connector);
Dave Airlie36cd7442014-05-02 13:44:18 +1000942 if (!intel_connector->encoder)
943 continue;
Egbert Eich321a1b32013-04-11 16:00:26 +0200944 intel_encoder = intel_connector->encoder;
945 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
946 if (intel_encoder->hot_plug)
947 intel_encoder->hot_plug(intel_encoder);
948 if (intel_hpd_irq_event(dev, connector))
949 changed = true;
950 }
951 }
Keith Packard40ee3382011-07-28 15:31:19 -0700952 mutex_unlock(&mode_config->mutex);
953
Egbert Eich321a1b32013-04-11 16:00:26 +0200954 if (changed)
955 drm_kms_helper_hotplug_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700956}
957
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200958static void ironlake_rps_change_irq_handler(struct drm_device *dev)
Jesse Barnesf97108d2010-01-29 11:27:07 -0800959{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300960 struct drm_i915_private *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000961 u32 busy_up, busy_down, max_avg, min_avg;
Daniel Vetter92703882012-08-09 16:46:01 +0200962 u8 new_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200963
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200964 spin_lock(&mchdev_lock);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800965
Daniel Vetter73edd18f2012-08-08 23:35:37 +0200966 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
967
Daniel Vetter20e4d402012-08-08 23:35:39 +0200968 new_delay = dev_priv->ips.cur_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200969
Jesse Barnes7648fa92010-05-20 14:28:11 -0700970 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000971 busy_up = I915_READ(RCPREVBSYTUPAVG);
972 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800973 max_avg = I915_READ(RCBMAXAVG);
974 min_avg = I915_READ(RCBMINAVG);
975
976 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000977 if (busy_up > max_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200978 if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
979 new_delay = dev_priv->ips.cur_delay - 1;
980 if (new_delay < dev_priv->ips.max_delay)
981 new_delay = dev_priv->ips.max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000982 } else if (busy_down < min_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200983 if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
984 new_delay = dev_priv->ips.cur_delay + 1;
985 if (new_delay > dev_priv->ips.min_delay)
986 new_delay = dev_priv->ips.min_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800987 }
988
Jesse Barnes7648fa92010-05-20 14:28:11 -0700989 if (ironlake_set_drps(dev, new_delay))
Daniel Vetter20e4d402012-08-08 23:35:39 +0200990 dev_priv->ips.cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800991
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200992 spin_unlock(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +0200993
Jesse Barnesf97108d2010-01-29 11:27:07 -0800994 return;
995}
996
Chris Wilson549f7362010-10-19 11:19:32 +0100997static void notify_ring(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100998 struct intel_engine_cs *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100999{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001000 if (!intel_ring_initialized(ring))
Chris Wilson475553d2011-01-20 09:52:56 +00001001 return;
1002
Chris Wilson814e9b52013-09-23 17:33:19 -03001003 trace_i915_gem_request_complete(ring);
Chris Wilson9862e602011-01-04 22:22:17 +00001004
Chris Wilson549f7362010-10-19 11:19:32 +01001005 wake_up_all(&ring->irq_queue);
Chris Wilson549f7362010-10-19 11:19:32 +01001006}
1007
Deepak S31685c22014-07-03 17:33:01 -04001008static u32 vlv_c0_residency(struct drm_i915_private *dev_priv,
Chris Wilsonbf225f22014-07-10 20:31:18 +01001009 struct intel_rps_ei *rps_ei)
Deepak S31685c22014-07-03 17:33:01 -04001010{
1011 u32 cz_ts, cz_freq_khz;
1012 u32 render_count, media_count;
1013 u32 elapsed_render, elapsed_media, elapsed_time;
1014 u32 residency = 0;
1015
1016 cz_ts = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
1017 cz_freq_khz = DIV_ROUND_CLOSEST(dev_priv->mem_freq * 1000, 4);
1018
1019 render_count = I915_READ(VLV_RENDER_C0_COUNT_REG);
1020 media_count = I915_READ(VLV_MEDIA_C0_COUNT_REG);
1021
Chris Wilsonbf225f22014-07-10 20:31:18 +01001022 if (rps_ei->cz_clock == 0) {
1023 rps_ei->cz_clock = cz_ts;
1024 rps_ei->render_c0 = render_count;
1025 rps_ei->media_c0 = media_count;
Deepak S31685c22014-07-03 17:33:01 -04001026
1027 return dev_priv->rps.cur_freq;
1028 }
1029
Chris Wilsonbf225f22014-07-10 20:31:18 +01001030 elapsed_time = cz_ts - rps_ei->cz_clock;
1031 rps_ei->cz_clock = cz_ts;
Deepak S31685c22014-07-03 17:33:01 -04001032
Chris Wilsonbf225f22014-07-10 20:31:18 +01001033 elapsed_render = render_count - rps_ei->render_c0;
1034 rps_ei->render_c0 = render_count;
Deepak S31685c22014-07-03 17:33:01 -04001035
Chris Wilsonbf225f22014-07-10 20:31:18 +01001036 elapsed_media = media_count - rps_ei->media_c0;
1037 rps_ei->media_c0 = media_count;
Deepak S31685c22014-07-03 17:33:01 -04001038
1039 /* Convert all the counters into common unit of milli sec */
1040 elapsed_time /= VLV_CZ_CLOCK_TO_MILLI_SEC;
1041 elapsed_render /= cz_freq_khz;
1042 elapsed_media /= cz_freq_khz;
1043
1044 /*
1045 * Calculate overall C0 residency percentage
1046 * only if elapsed time is non zero
1047 */
1048 if (elapsed_time) {
1049 residency =
1050 ((max(elapsed_render, elapsed_media) * 100)
1051 / elapsed_time);
1052 }
1053
1054 return residency;
1055}
1056
1057/**
1058 * vlv_calc_delay_from_C0_counters - Increase/Decrease freq based on GPU
1059 * busy-ness calculated from C0 counters of render & media power wells
1060 * @dev_priv: DRM device private
1061 *
1062 */
Damien Lespiau4fa79042014-08-08 19:25:57 +01001063static int vlv_calc_delay_from_C0_counters(struct drm_i915_private *dev_priv)
Deepak S31685c22014-07-03 17:33:01 -04001064{
1065 u32 residency_C0_up = 0, residency_C0_down = 0;
Damien Lespiau4fa79042014-08-08 19:25:57 +01001066 int new_delay, adj;
Deepak S31685c22014-07-03 17:33:01 -04001067
1068 dev_priv->rps.ei_interrupt_count++;
1069
1070 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
1071
1072
Chris Wilsonbf225f22014-07-10 20:31:18 +01001073 if (dev_priv->rps.up_ei.cz_clock == 0) {
1074 vlv_c0_residency(dev_priv, &dev_priv->rps.up_ei);
1075 vlv_c0_residency(dev_priv, &dev_priv->rps.down_ei);
Deepak S31685c22014-07-03 17:33:01 -04001076 return dev_priv->rps.cur_freq;
1077 }
1078
1079
1080 /*
1081 * To down throttle, C0 residency should be less than down threshold
1082 * for continous EI intervals. So calculate down EI counters
1083 * once in VLV_INT_COUNT_FOR_DOWN_EI
1084 */
1085 if (dev_priv->rps.ei_interrupt_count == VLV_INT_COUNT_FOR_DOWN_EI) {
1086
1087 dev_priv->rps.ei_interrupt_count = 0;
1088
1089 residency_C0_down = vlv_c0_residency(dev_priv,
Chris Wilsonbf225f22014-07-10 20:31:18 +01001090 &dev_priv->rps.down_ei);
Deepak S31685c22014-07-03 17:33:01 -04001091 } else {
1092 residency_C0_up = vlv_c0_residency(dev_priv,
Chris Wilsonbf225f22014-07-10 20:31:18 +01001093 &dev_priv->rps.up_ei);
Deepak S31685c22014-07-03 17:33:01 -04001094 }
1095
1096 new_delay = dev_priv->rps.cur_freq;
1097
1098 adj = dev_priv->rps.last_adj;
1099 /* C0 residency is greater than UP threshold. Increase Frequency */
1100 if (residency_C0_up >= VLV_RP_UP_EI_THRESHOLD) {
1101 if (adj > 0)
1102 adj *= 2;
1103 else
1104 adj = 1;
1105
1106 if (dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit)
1107 new_delay = dev_priv->rps.cur_freq + adj;
1108
1109 /*
1110 * For better performance, jump directly
1111 * to RPe if we're below it.
1112 */
1113 if (new_delay < dev_priv->rps.efficient_freq)
1114 new_delay = dev_priv->rps.efficient_freq;
1115
1116 } else if (!dev_priv->rps.ei_interrupt_count &&
1117 (residency_C0_down < VLV_RP_DOWN_EI_THRESHOLD)) {
1118 if (adj < 0)
1119 adj *= 2;
1120 else
1121 adj = -1;
1122 /*
1123 * This means, C0 residency is less than down threshold over
1124 * a period of VLV_INT_COUNT_FOR_DOWN_EI. So, reduce the freq
1125 */
1126 if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
1127 new_delay = dev_priv->rps.cur_freq + adj;
1128 }
1129
1130 return new_delay;
1131}
1132
Ben Widawsky4912d042011-04-25 11:25:20 -07001133static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001134{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001135 struct drm_i915_private *dev_priv =
1136 container_of(work, struct drm_i915_private, rps.work);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03001137 u32 pm_iir;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001138 int new_delay, adj;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001139
Daniel Vetter59cdb632013-07-04 23:35:28 +02001140 spin_lock_irq(&dev_priv->irq_lock);
Imre Deakd4d70aa2014-11-19 15:30:04 +02001141 /* Speed up work cancelation during disabling rps interrupts. */
1142 if (!dev_priv->rps.interrupts_enabled) {
1143 spin_unlock_irq(&dev_priv->irq_lock);
1144 return;
1145 }
Daniel Vetterc6a828d2012-08-08 23:35:35 +02001146 pm_iir = dev_priv->rps.pm_iir;
1147 dev_priv->rps.pm_iir = 0;
Imre Deaka72fbc32014-11-05 20:48:31 +02001148 /* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1149 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Daniel Vetter59cdb632013-07-04 23:35:28 +02001150 spin_unlock_irq(&dev_priv->irq_lock);
Ben Widawsky4912d042011-04-25 11:25:20 -07001151
Paulo Zanoni60611c12013-08-15 11:50:01 -03001152 /* Make sure we didn't queue anything we're not going to process. */
Deepak Sa6706b42014-03-15 20:23:22 +05301153 WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
Paulo Zanoni60611c12013-08-15 11:50:01 -03001154
Deepak Sa6706b42014-03-15 20:23:22 +05301155 if ((pm_iir & dev_priv->pm_rps_events) == 0)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001156 return;
1157
Jesse Barnes4fc688c2012-11-02 11:14:01 -07001158 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01001159
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001160 adj = dev_priv->rps.last_adj;
Ville Syrjälä74250342013-06-25 21:38:11 +03001161 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001162 if (adj > 0)
1163 adj *= 2;
Deepak S13a56602014-05-23 21:00:21 +05301164 else {
1165 /* CHV needs even encode values */
1166 adj = IS_CHERRYVIEW(dev_priv->dev) ? 2 : 1;
1167 }
Ben Widawskyb39fb292014-03-19 18:31:11 -07001168 new_delay = dev_priv->rps.cur_freq + adj;
Ville Syrjälä74250342013-06-25 21:38:11 +03001169
1170 /*
1171 * For better performance, jump directly
1172 * to RPe if we're below it.
1173 */
Ben Widawskyb39fb292014-03-19 18:31:11 -07001174 if (new_delay < dev_priv->rps.efficient_freq)
1175 new_delay = dev_priv->rps.efficient_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001176 } else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07001177 if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1178 new_delay = dev_priv->rps.efficient_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001179 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07001180 new_delay = dev_priv->rps.min_freq_softlimit;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001181 adj = 0;
Deepak S31685c22014-07-03 17:33:01 -04001182 } else if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
1183 new_delay = vlv_calc_delay_from_C0_counters(dev_priv);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001184 } else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1185 if (adj < 0)
1186 adj *= 2;
Deepak S13a56602014-05-23 21:00:21 +05301187 else {
1188 /* CHV needs even encode values */
1189 adj = IS_CHERRYVIEW(dev_priv->dev) ? -2 : -1;
1190 }
Ben Widawskyb39fb292014-03-19 18:31:11 -07001191 new_delay = dev_priv->rps.cur_freq + adj;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001192 } else { /* unknown event */
Ben Widawskyb39fb292014-03-19 18:31:11 -07001193 new_delay = dev_priv->rps.cur_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001194 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001195
Ben Widawsky79249632012-09-07 19:43:42 -07001196 /* sysfs frequency interfaces may have snuck in while servicing the
1197 * interrupt
1198 */
Ville Syrjälä1272e7b2013-11-07 19:57:49 +02001199 new_delay = clamp_t(int, new_delay,
Ben Widawskyb39fb292014-03-19 18:31:11 -07001200 dev_priv->rps.min_freq_softlimit,
1201 dev_priv->rps.max_freq_softlimit);
Deepak S27544362014-01-27 21:35:05 +05301202
Ben Widawskyb39fb292014-03-19 18:31:11 -07001203 dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001204
1205 if (IS_VALLEYVIEW(dev_priv->dev))
1206 valleyview_set_rps(dev_priv->dev, new_delay);
1207 else
1208 gen6_set_rps(dev_priv->dev, new_delay);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001209
Jesse Barnes4fc688c2012-11-02 11:14:01 -07001210 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001211}
1212
Ben Widawskye3689192012-05-25 16:56:22 -07001213
1214/**
1215 * ivybridge_parity_work - Workqueue called when a parity error interrupt
1216 * occurred.
1217 * @work: workqueue struct
1218 *
1219 * Doesn't actually do anything except notify userspace. As a consequence of
1220 * this event, userspace should try to remap the bad rows since statistically
1221 * it is likely the same row is more likely to go bad again.
1222 */
1223static void ivybridge_parity_work(struct work_struct *work)
1224{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001225 struct drm_i915_private *dev_priv =
1226 container_of(work, struct drm_i915_private, l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -07001227 u32 error_status, row, bank, subbank;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001228 char *parity_event[6];
Ben Widawskye3689192012-05-25 16:56:22 -07001229 uint32_t misccpctl;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001230 uint8_t slice = 0;
Ben Widawskye3689192012-05-25 16:56:22 -07001231
1232 /* We must turn off DOP level clock gating to access the L3 registers.
1233 * In order to prevent a get/put style interface, acquire struct mutex
1234 * any time we access those registers.
1235 */
1236 mutex_lock(&dev_priv->dev->struct_mutex);
1237
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001238 /* If we've screwed up tracking, just let the interrupt fire again */
1239 if (WARN_ON(!dev_priv->l3_parity.which_slice))
1240 goto out;
1241
Ben Widawskye3689192012-05-25 16:56:22 -07001242 misccpctl = I915_READ(GEN7_MISCCPCTL);
1243 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1244 POSTING_READ(GEN7_MISCCPCTL);
1245
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001246 while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1247 u32 reg;
Ben Widawskye3689192012-05-25 16:56:22 -07001248
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001249 slice--;
1250 if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
1251 break;
1252
1253 dev_priv->l3_parity.which_slice &= ~(1<<slice);
1254
1255 reg = GEN7_L3CDERRST1 + (slice * 0x200);
1256
1257 error_status = I915_READ(reg);
1258 row = GEN7_PARITY_ERROR_ROW(error_status);
1259 bank = GEN7_PARITY_ERROR_BANK(error_status);
1260 subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1261
1262 I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
1263 POSTING_READ(reg);
1264
1265 parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1266 parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1267 parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1268 parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
1269 parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
1270 parity_event[5] = NULL;
1271
Dave Airlie5bdebb12013-10-11 14:07:25 +10001272 kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001273 KOBJ_CHANGE, parity_event);
1274
1275 DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
1276 slice, row, bank, subbank);
1277
1278 kfree(parity_event[4]);
1279 kfree(parity_event[3]);
1280 kfree(parity_event[2]);
1281 kfree(parity_event[1]);
1282 }
Ben Widawskye3689192012-05-25 16:56:22 -07001283
1284 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
1285
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001286out:
1287 WARN_ON(dev_priv->l3_parity.which_slice);
Daniel Vetter4cb21832014-09-15 14:55:26 +02001288 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetter480c8032014-07-16 09:49:40 +02001289 gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
Daniel Vetter4cb21832014-09-15 14:55:26 +02001290 spin_unlock_irq(&dev_priv->irq_lock);
Ben Widawskye3689192012-05-25 16:56:22 -07001291
1292 mutex_unlock(&dev_priv->dev->struct_mutex);
Ben Widawskye3689192012-05-25 16:56:22 -07001293}
1294
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001295static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
Ben Widawskye3689192012-05-25 16:56:22 -07001296{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001297 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3689192012-05-25 16:56:22 -07001298
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001299 if (!HAS_L3_DPF(dev))
Ben Widawskye3689192012-05-25 16:56:22 -07001300 return;
1301
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001302 spin_lock(&dev_priv->irq_lock);
Daniel Vetter480c8032014-07-16 09:49:40 +02001303 gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001304 spin_unlock(&dev_priv->irq_lock);
Ben Widawskye3689192012-05-25 16:56:22 -07001305
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001306 iir &= GT_PARITY_ERROR(dev);
1307 if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
1308 dev_priv->l3_parity.which_slice |= 1 << 1;
1309
1310 if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
1311 dev_priv->l3_parity.which_slice |= 1 << 0;
1312
Daniel Vettera4da4fa2012-11-02 19:55:07 +01001313 queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -07001314}
1315
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001316static void ilk_gt_irq_handler(struct drm_device *dev,
1317 struct drm_i915_private *dev_priv,
1318 u32 gt_iir)
1319{
1320 if (gt_iir &
1321 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1322 notify_ring(dev, &dev_priv->ring[RCS]);
1323 if (gt_iir & ILK_BSD_USER_INTERRUPT)
1324 notify_ring(dev, &dev_priv->ring[VCS]);
1325}
1326
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001327static void snb_gt_irq_handler(struct drm_device *dev,
1328 struct drm_i915_private *dev_priv,
1329 u32 gt_iir)
1330{
1331
Ben Widawskycc609d52013-05-28 19:22:29 -07001332 if (gt_iir &
1333 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001334 notify_ring(dev, &dev_priv->ring[RCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001335 if (gt_iir & GT_BSD_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001336 notify_ring(dev, &dev_priv->ring[VCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001337 if (gt_iir & GT_BLT_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001338 notify_ring(dev, &dev_priv->ring[BCS]);
1339
Ben Widawskycc609d52013-05-28 19:22:29 -07001340 if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1341 GT_BSD_CS_ERROR_INTERRUPT |
1342 GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
Mika Kuoppala58174462014-02-25 17:11:26 +02001343 i915_handle_error(dev, false, "GT error interrupt 0x%08x",
1344 gt_iir);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001345 }
Ben Widawskye3689192012-05-25 16:56:22 -07001346
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001347 if (gt_iir & GT_PARITY_ERROR(dev))
1348 ivybridge_parity_error_irq_handler(dev, gt_iir);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001349}
1350
Ben Widawskyabd58f02013-11-02 21:07:09 -07001351static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1352 struct drm_i915_private *dev_priv,
1353 u32 master_ctl)
1354{
Thomas Daniele981e7b2014-07-24 17:04:39 +01001355 struct intel_engine_cs *ring;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001356 u32 rcs, bcs, vcs;
1357 uint32_t tmp = 0;
1358 irqreturn_t ret = IRQ_NONE;
1359
1360 if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1361 tmp = I915_READ(GEN8_GT_IIR(0));
1362 if (tmp) {
Oscar Mateo38cc46d2014-06-16 16:10:59 +01001363 I915_WRITE(GEN8_GT_IIR(0), tmp);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001364 ret = IRQ_HANDLED;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001365
Ben Widawskyabd58f02013-11-02 21:07:09 -07001366 rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001367 ring = &dev_priv->ring[RCS];
Ben Widawskyabd58f02013-11-02 21:07:09 -07001368 if (rcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001369 notify_ring(dev, ring);
1370 if (rcs & GT_CONTEXT_SWITCH_INTERRUPT)
1371 intel_execlists_handle_ctx_events(ring);
1372
1373 bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1374 ring = &dev_priv->ring[BCS];
Ben Widawskyabd58f02013-11-02 21:07:09 -07001375 if (bcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001376 notify_ring(dev, ring);
1377 if (bcs & GT_CONTEXT_SWITCH_INTERRUPT)
1378 intel_execlists_handle_ctx_events(ring);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001379 } else
1380 DRM_ERROR("The master control interrupt lied (GT0)!\n");
1381 }
1382
Zhao Yakui85f9b5f2014-04-17 10:37:38 +08001383 if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
Ben Widawskyabd58f02013-11-02 21:07:09 -07001384 tmp = I915_READ(GEN8_GT_IIR(1));
1385 if (tmp) {
Oscar Mateo38cc46d2014-06-16 16:10:59 +01001386 I915_WRITE(GEN8_GT_IIR(1), tmp);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001387 ret = IRQ_HANDLED;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001388
Ben Widawskyabd58f02013-11-02 21:07:09 -07001389 vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001390 ring = &dev_priv->ring[VCS];
Ben Widawskyabd58f02013-11-02 21:07:09 -07001391 if (vcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001392 notify_ring(dev, ring);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001393 if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001394 intel_execlists_handle_ctx_events(ring);
1395
Zhao Yakui85f9b5f2014-04-17 10:37:38 +08001396 vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001397 ring = &dev_priv->ring[VCS2];
Zhao Yakui85f9b5f2014-04-17 10:37:38 +08001398 if (vcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001399 notify_ring(dev, ring);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001400 if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001401 intel_execlists_handle_ctx_events(ring);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001402 } else
1403 DRM_ERROR("The master control interrupt lied (GT1)!\n");
1404 }
1405
Ben Widawsky09610212014-05-15 20:58:08 +03001406 if (master_ctl & GEN8_GT_PM_IRQ) {
1407 tmp = I915_READ(GEN8_GT_IIR(2));
1408 if (tmp & dev_priv->pm_rps_events) {
Ben Widawsky09610212014-05-15 20:58:08 +03001409 I915_WRITE(GEN8_GT_IIR(2),
1410 tmp & dev_priv->pm_rps_events);
Oscar Mateo38cc46d2014-06-16 16:10:59 +01001411 ret = IRQ_HANDLED;
Imre Deakc9a9a262014-11-05 20:48:37 +02001412 gen6_rps_irq_handler(dev_priv, tmp);
Ben Widawsky09610212014-05-15 20:58:08 +03001413 } else
1414 DRM_ERROR("The master control interrupt lied (PM)!\n");
1415 }
1416
Ben Widawskyabd58f02013-11-02 21:07:09 -07001417 if (master_ctl & GEN8_GT_VECS_IRQ) {
1418 tmp = I915_READ(GEN8_GT_IIR(3));
1419 if (tmp) {
Oscar Mateo38cc46d2014-06-16 16:10:59 +01001420 I915_WRITE(GEN8_GT_IIR(3), tmp);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001421 ret = IRQ_HANDLED;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001422
Ben Widawskyabd58f02013-11-02 21:07:09 -07001423 vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
Thomas Daniele981e7b2014-07-24 17:04:39 +01001424 ring = &dev_priv->ring[VECS];
Ben Widawskyabd58f02013-11-02 21:07:09 -07001425 if (vcs & GT_RENDER_USER_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001426 notify_ring(dev, ring);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001427 if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
Thomas Daniele981e7b2014-07-24 17:04:39 +01001428 intel_execlists_handle_ctx_events(ring);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001429 } else
1430 DRM_ERROR("The master control interrupt lied (GT3)!\n");
1431 }
1432
1433 return ret;
1434}
1435
Egbert Eichb543fb02013-04-16 13:36:54 +02001436#define HPD_STORM_DETECT_PERIOD 1000
1437#define HPD_STORM_THRESHOLD 5
1438
Jani Nikula07c338c2014-10-02 11:16:32 +03001439static int pch_port_to_hotplug_shift(enum port port)
Dave Airlie13cf5502014-06-18 11:29:35 +10001440{
1441 switch (port) {
1442 case PORT_A:
1443 case PORT_E:
1444 default:
1445 return -1;
1446 case PORT_B:
1447 return 0;
1448 case PORT_C:
1449 return 8;
1450 case PORT_D:
1451 return 16;
1452 }
1453}
1454
Jani Nikula07c338c2014-10-02 11:16:32 +03001455static int i915_port_to_hotplug_shift(enum port port)
Dave Airlie13cf5502014-06-18 11:29:35 +10001456{
1457 switch (port) {
1458 case PORT_A:
1459 case PORT_E:
1460 default:
1461 return -1;
1462 case PORT_B:
1463 return 17;
1464 case PORT_C:
1465 return 19;
1466 case PORT_D:
1467 return 21;
1468 }
1469}
1470
1471static inline enum port get_port_from_pin(enum hpd_pin pin)
1472{
1473 switch (pin) {
1474 case HPD_PORT_B:
1475 return PORT_B;
1476 case HPD_PORT_C:
1477 return PORT_C;
1478 case HPD_PORT_D:
1479 return PORT_D;
1480 default:
1481 return PORT_A; /* no hpd */
1482 }
1483}
1484
Daniel Vetter10a504d2013-06-27 17:52:12 +02001485static inline void intel_hpd_irq_handler(struct drm_device *dev,
Daniel Vetter22062db2013-06-27 17:52:11 +02001486 u32 hotplug_trigger,
Dave Airlie13cf5502014-06-18 11:29:35 +10001487 u32 dig_hotplug_reg,
Daniel Vetter22062db2013-06-27 17:52:11 +02001488 const u32 *hpd)
Egbert Eichb543fb02013-04-16 13:36:54 +02001489{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001490 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eichb543fb02013-04-16 13:36:54 +02001491 int i;
Dave Airlie13cf5502014-06-18 11:29:35 +10001492 enum port port;
Daniel Vetter10a504d2013-06-27 17:52:12 +02001493 bool storm_detected = false;
Dave Airlie13cf5502014-06-18 11:29:35 +10001494 bool queue_dig = false, queue_hp = false;
1495 u32 dig_shift;
1496 u32 dig_port_mask = 0;
Egbert Eichb543fb02013-04-16 13:36:54 +02001497
Daniel Vetter91d131d2013-06-27 17:52:14 +02001498 if (!hotplug_trigger)
1499 return;
1500
Dave Airlie13cf5502014-06-18 11:29:35 +10001501 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
1502 hotplug_trigger, dig_hotplug_reg);
Imre Deakcc9bd492014-01-16 19:56:54 +02001503
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02001504 spin_lock(&dev_priv->irq_lock);
Egbert Eichb543fb02013-04-16 13:36:54 +02001505 for (i = 1; i < HPD_NUM_PINS; i++) {
Dave Airlie13cf5502014-06-18 11:29:35 +10001506 if (!(hpd[i] & hotplug_trigger))
1507 continue;
Egbert Eich821450c2013-04-16 13:36:55 +02001508
Dave Airlie13cf5502014-06-18 11:29:35 +10001509 port = get_port_from_pin(i);
1510 if (port && dev_priv->hpd_irq_port[port]) {
1511 bool long_hpd;
1512
Jani Nikula07c338c2014-10-02 11:16:32 +03001513 if (HAS_PCH_SPLIT(dev)) {
1514 dig_shift = pch_port_to_hotplug_shift(port);
Dave Airlie13cf5502014-06-18 11:29:35 +10001515 long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
Jani Nikula07c338c2014-10-02 11:16:32 +03001516 } else {
1517 dig_shift = i915_port_to_hotplug_shift(port);
1518 long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
Dave Airlie13cf5502014-06-18 11:29:35 +10001519 }
1520
Ville Syrjälä26fbb772014-08-11 18:37:37 +03001521 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
1522 port_name(port),
1523 long_hpd ? "long" : "short");
Dave Airlie13cf5502014-06-18 11:29:35 +10001524 /* for long HPD pulses we want to have the digital queue happen,
1525 but we still want HPD storm detection to function. */
1526 if (long_hpd) {
1527 dev_priv->long_hpd_port_mask |= (1 << port);
1528 dig_port_mask |= hpd[i];
1529 } else {
1530 /* for short HPD just trigger the digital queue */
1531 dev_priv->short_hpd_port_mask |= (1 << port);
1532 hotplug_trigger &= ~hpd[i];
1533 }
1534 queue_dig = true;
1535 }
1536 }
1537
1538 for (i = 1; i < HPD_NUM_PINS; i++) {
Daniel Vetter3ff04a162014-04-24 12:03:17 +02001539 if (hpd[i] & hotplug_trigger &&
1540 dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
1541 /*
1542 * On GMCH platforms the interrupt mask bits only
1543 * prevent irq generation, not the setting of the
1544 * hotplug bits itself. So only WARN about unexpected
1545 * interrupts on saner platforms.
1546 */
1547 WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1548 "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1549 hotplug_trigger, i, hpd[i]);
1550
1551 continue;
1552 }
Egbert Eichb8f102e2013-07-26 14:14:24 +02001553
Egbert Eichb543fb02013-04-16 13:36:54 +02001554 if (!(hpd[i] & hotplug_trigger) ||
1555 dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1556 continue;
1557
Dave Airlie13cf5502014-06-18 11:29:35 +10001558 if (!(dig_port_mask & hpd[i])) {
1559 dev_priv->hpd_event_bits |= (1 << i);
1560 queue_hp = true;
1561 }
1562
Egbert Eichb543fb02013-04-16 13:36:54 +02001563 if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1564 dev_priv->hpd_stats[i].hpd_last_jiffies
1565 + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1566 dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1567 dev_priv->hpd_stats[i].hpd_cnt = 0;
Egbert Eichb8f102e2013-07-26 14:14:24 +02001568 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001569 } else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1570 dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
Egbert Eich142e2392013-04-11 15:57:57 +02001571 dev_priv->hpd_event_bits &= ~(1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001572 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
Daniel Vetter10a504d2013-06-27 17:52:12 +02001573 storm_detected = true;
Egbert Eichb543fb02013-04-16 13:36:54 +02001574 } else {
1575 dev_priv->hpd_stats[i].hpd_cnt++;
Egbert Eichb8f102e2013-07-26 14:14:24 +02001576 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1577 dev_priv->hpd_stats[i].hpd_cnt);
Egbert Eichb543fb02013-04-16 13:36:54 +02001578 }
1579 }
1580
Daniel Vetter10a504d2013-06-27 17:52:12 +02001581 if (storm_detected)
1582 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02001583 spin_unlock(&dev_priv->irq_lock);
Daniel Vetter5876fa02013-06-27 17:52:13 +02001584
Daniel Vetter645416f2013-09-02 16:22:25 +02001585 /*
1586 * Our hotplug handler can grab modeset locks (by calling down into the
1587 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1588 * queue for otherwise the flush_work in the pageflip code will
1589 * deadlock.
1590 */
Dave Airlie13cf5502014-06-18 11:29:35 +10001591 if (queue_dig)
Dave Airlie0e32b392014-05-02 14:02:48 +10001592 queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
Dave Airlie13cf5502014-06-18 11:29:35 +10001593 if (queue_hp)
1594 schedule_work(&dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +02001595}
1596
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001597static void gmbus_irq_handler(struct drm_device *dev)
1598{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001599 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter28c70f12012-12-01 13:53:45 +01001600
Daniel Vetter28c70f12012-12-01 13:53:45 +01001601 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001602}
1603
Daniel Vetterce99c252012-12-01 13:53:47 +01001604static void dp_aux_irq_handler(struct drm_device *dev)
1605{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001606 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001607
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001608 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetterce99c252012-12-01 13:53:47 +01001609}
1610
Shuang He8bf1e9f2013-10-15 18:55:27 +01001611#if defined(CONFIG_DEBUG_FS)
Daniel Vetter277de952013-10-18 16:37:07 +02001612static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1613 uint32_t crc0, uint32_t crc1,
1614 uint32_t crc2, uint32_t crc3,
1615 uint32_t crc4)
Shuang He8bf1e9f2013-10-15 18:55:27 +01001616{
1617 struct drm_i915_private *dev_priv = dev->dev_private;
1618 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
1619 struct intel_pipe_crc_entry *entry;
Damien Lespiauac2300d2013-10-15 18:55:30 +01001620 int head, tail;
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001621
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001622 spin_lock(&pipe_crc->lock);
1623
Damien Lespiau0c912c72013-10-15 18:55:37 +01001624 if (!pipe_crc->entries) {
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001625 spin_unlock(&pipe_crc->lock);
Damien Lespiau0c912c72013-10-15 18:55:37 +01001626 DRM_ERROR("spurious interrupt\n");
1627 return;
1628 }
1629
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001630 head = pipe_crc->head;
1631 tail = pipe_crc->tail;
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001632
1633 if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001634 spin_unlock(&pipe_crc->lock);
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001635 DRM_ERROR("CRC buffer overflowing\n");
1636 return;
1637 }
1638
1639 entry = &pipe_crc->entries[head];
Shuang He8bf1e9f2013-10-15 18:55:27 +01001640
Daniel Vetter8bc5e952013-10-16 22:55:49 +02001641 entry->frame = dev->driver->get_vblank_counter(dev, pipe);
Daniel Vettereba94eb2013-10-16 22:55:46 +02001642 entry->crc[0] = crc0;
1643 entry->crc[1] = crc1;
1644 entry->crc[2] = crc2;
1645 entry->crc[3] = crc3;
1646 entry->crc[4] = crc4;
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001647
1648 head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001649 pipe_crc->head = head;
1650
1651 spin_unlock(&pipe_crc->lock);
Damien Lespiau07144422013-10-15 18:55:40 +01001652
1653 wake_up_interruptible(&pipe_crc->wq);
Shuang He8bf1e9f2013-10-15 18:55:27 +01001654}
Daniel Vetter277de952013-10-18 16:37:07 +02001655#else
1656static inline void
1657display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1658 uint32_t crc0, uint32_t crc1,
1659 uint32_t crc2, uint32_t crc3,
1660 uint32_t crc4) {}
1661#endif
Daniel Vettereba94eb2013-10-16 22:55:46 +02001662
Daniel Vetter277de952013-10-18 16:37:07 +02001663
1664static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
Daniel Vetter5a69b892013-10-16 22:55:52 +02001665{
1666 struct drm_i915_private *dev_priv = dev->dev_private;
1667
Daniel Vetter277de952013-10-18 16:37:07 +02001668 display_pipe_crc_irq_handler(dev, pipe,
1669 I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1670 0, 0, 0, 0);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001671}
1672
Daniel Vetter277de952013-10-18 16:37:07 +02001673static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
Daniel Vettereba94eb2013-10-16 22:55:46 +02001674{
1675 struct drm_i915_private *dev_priv = dev->dev_private;
1676
Daniel Vetter277de952013-10-18 16:37:07 +02001677 display_pipe_crc_irq_handler(dev, pipe,
1678 I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1679 I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1680 I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1681 I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
1682 I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
Daniel Vettereba94eb2013-10-16 22:55:46 +02001683}
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001684
Daniel Vetter277de952013-10-18 16:37:07 +02001685static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001686{
1687 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0b5c5ed2013-10-16 22:55:53 +02001688 uint32_t res1, res2;
1689
1690 if (INTEL_INFO(dev)->gen >= 3)
1691 res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
1692 else
1693 res1 = 0;
1694
1695 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
1696 res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
1697 else
1698 res2 = 0;
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001699
Daniel Vetter277de952013-10-18 16:37:07 +02001700 display_pipe_crc_irq_handler(dev, pipe,
1701 I915_READ(PIPE_CRC_RES_RED(pipe)),
1702 I915_READ(PIPE_CRC_RES_GREEN(pipe)),
1703 I915_READ(PIPE_CRC_RES_BLUE(pipe)),
1704 res1, res2);
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001705}
Shuang He8bf1e9f2013-10-15 18:55:27 +01001706
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001707/* The RPS events need forcewake, so we add them to a work queue and mask their
1708 * IMR bits until the work is done. Other interrupts can be processed without
1709 * the work queue. */
1710static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
Ben Widawskybaf02a12013-05-28 19:22:24 -07001711{
Imre Deak4a74de82014-11-19 15:30:01 +02001712 /* TODO: RPS on GEN9+ is not supported yet. */
1713 if (WARN_ONCE(INTEL_INFO(dev_priv)->gen >= 9,
1714 "GEN9+: unexpected RPS IRQ\n"))
Imre Deak132f3f12014-11-10 15:34:33 +02001715 return;
1716
Deepak Sa6706b42014-03-15 20:23:22 +05301717 if (pm_iir & dev_priv->pm_rps_events) {
Daniel Vetter59cdb632013-07-04 23:35:28 +02001718 spin_lock(&dev_priv->irq_lock);
Daniel Vetter480c8032014-07-16 09:49:40 +02001719 gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
Imre Deakd4d70aa2014-11-19 15:30:04 +02001720 if (dev_priv->rps.interrupts_enabled) {
1721 dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1722 queue_work(dev_priv->wq, &dev_priv->rps.work);
1723 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001724 spin_unlock(&dev_priv->irq_lock);
Ben Widawskybaf02a12013-05-28 19:22:24 -07001725 }
Ben Widawskybaf02a12013-05-28 19:22:24 -07001726
Imre Deakc9a9a262014-11-05 20:48:37 +02001727 if (INTEL_INFO(dev_priv)->gen >= 8)
1728 return;
1729
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001730 if (HAS_VEBOX(dev_priv->dev)) {
1731 if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1732 notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
Ben Widawsky12638c52013-05-28 19:22:31 -07001733
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001734 if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
Mika Kuoppala58174462014-02-25 17:11:26 +02001735 i915_handle_error(dev_priv->dev, false,
1736 "VEBOX CS error interrupt 0x%08x",
1737 pm_iir);
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001738 }
Ben Widawsky12638c52013-05-28 19:22:31 -07001739 }
Ben Widawskybaf02a12013-05-28 19:22:24 -07001740}
1741
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03001742static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
1743{
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03001744 if (!drm_handle_vblank(dev, pipe))
1745 return false;
1746
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03001747 return true;
1748}
1749
Imre Deakc1874ed2014-02-04 21:35:46 +02001750static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
1751{
1752 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak91d181d2014-02-10 18:42:49 +02001753 u32 pipe_stats[I915_MAX_PIPES] = { };
Imre Deakc1874ed2014-02-04 21:35:46 +02001754 int pipe;
1755
Imre Deak58ead0d2014-02-04 21:35:47 +02001756 spin_lock(&dev_priv->irq_lock);
Damien Lespiau055e3932014-08-18 13:49:10 +01001757 for_each_pipe(dev_priv, pipe) {
Imre Deak91d181d2014-02-10 18:42:49 +02001758 int reg;
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001759 u32 mask, iir_bit = 0;
Imre Deak91d181d2014-02-10 18:42:49 +02001760
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001761 /*
1762 * PIPESTAT bits get signalled even when the interrupt is
1763 * disabled with the mask bits, and some of the status bits do
1764 * not generate interrupts at all (like the underrun bit). Hence
1765 * we need to be careful that we only handle what we want to
1766 * handle.
1767 */
Daniel Vetter0f239f42014-09-30 10:56:49 +02001768
1769 /* fifo underruns are filterered in the underrun handler. */
1770 mask = PIPE_FIFO_UNDERRUN_STATUS;
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001771
1772 switch (pipe) {
1773 case PIPE_A:
1774 iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1775 break;
1776 case PIPE_B:
1777 iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1778 break;
Ville Syrjälä3278f672014-04-09 13:28:49 +03001779 case PIPE_C:
1780 iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
1781 break;
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001782 }
1783 if (iir & iir_bit)
1784 mask |= dev_priv->pipestat_irq_mask[pipe];
1785
1786 if (!mask)
Imre Deak91d181d2014-02-10 18:42:49 +02001787 continue;
1788
1789 reg = PIPESTAT(pipe);
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001790 mask |= PIPESTAT_INT_ENABLE_MASK;
1791 pipe_stats[pipe] = I915_READ(reg) & mask;
Imre Deakc1874ed2014-02-04 21:35:46 +02001792
1793 /*
1794 * Clear the PIPE*STAT regs before the IIR
1795 */
Imre Deak91d181d2014-02-10 18:42:49 +02001796 if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
1797 PIPESTAT_INT_STATUS_MASK))
Imre Deakc1874ed2014-02-04 21:35:46 +02001798 I915_WRITE(reg, pipe_stats[pipe]);
1799 }
Imre Deak58ead0d2014-02-04 21:35:47 +02001800 spin_unlock(&dev_priv->irq_lock);
Imre Deakc1874ed2014-02-04 21:35:46 +02001801
Damien Lespiau055e3932014-08-18 13:49:10 +01001802 for_each_pipe(dev_priv, pipe) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01001803 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1804 intel_pipe_handle_vblank(dev, pipe))
1805 intel_check_page_flip(dev, pipe);
Imre Deakc1874ed2014-02-04 21:35:46 +02001806
Imre Deak579a9b02014-02-04 21:35:48 +02001807 if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
Imre Deakc1874ed2014-02-04 21:35:46 +02001808 intel_prepare_page_flip(dev, pipe);
1809 intel_finish_page_flip(dev, pipe);
1810 }
1811
1812 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1813 i9xx_pipe_crc_irq_handler(dev, pipe);
1814
Daniel Vetter1f7247c2014-09-30 10:56:48 +02001815 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1816 intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
Imre Deakc1874ed2014-02-04 21:35:46 +02001817 }
1818
1819 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1820 gmbus_irq_handler(dev);
1821}
1822
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001823static void i9xx_hpd_irq_handler(struct drm_device *dev)
1824{
1825 struct drm_i915_private *dev_priv = dev->dev_private;
1826 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1827
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001828 if (hotplug_status) {
1829 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1830 /*
1831 * Make sure hotplug status is cleared before we clear IIR, or else we
1832 * may miss hotplug events.
1833 */
1834 POSTING_READ(PORT_HOTPLUG_STAT);
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001835
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001836 if (IS_G4X(dev)) {
1837 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001838
Dave Airlie13cf5502014-06-18 11:29:35 +10001839 intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001840 } else {
1841 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
1842
Dave Airlie13cf5502014-06-18 11:29:35 +10001843 intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001844 }
1845
1846 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
1847 hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1848 dp_aux_irq_handler(dev);
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001849 }
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001850}
1851
Daniel Vetterff1f5252012-10-02 15:10:55 +02001852static irqreturn_t valleyview_irq_handler(int irq, void *arg)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001853{
Daniel Vetter45a83f82014-05-12 19:17:55 +02001854 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03001855 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001856 u32 iir, gt_iir, pm_iir;
1857 irqreturn_t ret = IRQ_NONE;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001858
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001859 while (true) {
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001860 /* Find, clear, then process each source of interrupt */
1861
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001862 gt_iir = I915_READ(GTIIR);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001863 if (gt_iir)
1864 I915_WRITE(GTIIR, gt_iir);
1865
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001866 pm_iir = I915_READ(GEN6_PMIIR);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001867 if (pm_iir)
1868 I915_WRITE(GEN6_PMIIR, pm_iir);
1869
1870 iir = I915_READ(VLV_IIR);
1871 if (iir) {
1872 /* Consume port before clearing IIR or we'll miss events */
1873 if (iir & I915_DISPLAY_PORT_INTERRUPT)
1874 i9xx_hpd_irq_handler(dev);
1875 I915_WRITE(VLV_IIR, iir);
1876 }
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001877
1878 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1879 goto out;
1880
1881 ret = IRQ_HANDLED;
1882
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001883 if (gt_iir)
1884 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Paulo Zanoni60611c12013-08-15 11:50:01 -03001885 if (pm_iir)
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001886 gen6_rps_irq_handler(dev_priv, pm_iir);
Oscar Mateo3ff60f82014-06-16 16:10:58 +01001887 /* Call regardless, as some status bits might not be
1888 * signalled in iir */
1889 valleyview_pipestat_irq_handler(dev, iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001890 }
1891
1892out:
1893 return ret;
1894}
1895
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001896static irqreturn_t cherryview_irq_handler(int irq, void *arg)
1897{
Daniel Vetter45a83f82014-05-12 19:17:55 +02001898 struct drm_device *dev = arg;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001899 struct drm_i915_private *dev_priv = dev->dev_private;
1900 u32 master_ctl, iir;
1901 irqreturn_t ret = IRQ_NONE;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001902
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001903 for (;;) {
1904 master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
1905 iir = I915_READ(VLV_IIR);
Ville Syrjälä3278f672014-04-09 13:28:49 +03001906
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001907 if (master_ctl == 0 && iir == 0)
1908 break;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001909
Oscar Mateo27b6c122014-06-16 16:11:00 +01001910 ret = IRQ_HANDLED;
1911
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001912 I915_WRITE(GEN8_MASTER_IRQ, 0);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001913
Oscar Mateo27b6c122014-06-16 16:11:00 +01001914 /* Find, clear, then process each source of interrupt */
1915
1916 if (iir) {
1917 /* Consume port before clearing IIR or we'll miss events */
1918 if (iir & I915_DISPLAY_PORT_INTERRUPT)
1919 i9xx_hpd_irq_handler(dev);
1920 I915_WRITE(VLV_IIR, iir);
1921 }
1922
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001923 gen8_gt_irq_handler(dev, dev_priv, master_ctl);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001924
Oscar Mateo27b6c122014-06-16 16:11:00 +01001925 /* Call regardless, as some status bits might not be
1926 * signalled in iir */
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001927 valleyview_pipestat_irq_handler(dev, iir);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001928
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001929 I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
1930 POSTING_READ(GEN8_MASTER_IRQ);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001931 }
1932
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001933 return ret;
1934}
1935
Adam Jackson23e81d62012-06-06 15:45:44 -04001936static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
Jesse Barnes776ad802011-01-04 15:09:39 -08001937{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001938 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001939 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001940 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
Dave Airlie13cf5502014-06-18 11:29:35 +10001941 u32 dig_hotplug_reg;
Jesse Barnes776ad802011-01-04 15:09:39 -08001942
Dave Airlie13cf5502014-06-18 11:29:35 +10001943 dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
1944 I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
1945
1946 intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
Daniel Vetter91d131d2013-06-27 17:52:14 +02001947
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001948 if (pch_iir & SDE_AUDIO_POWER_MASK) {
1949 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1950 SDE_AUDIO_POWER_SHIFT);
Jesse Barnes776ad802011-01-04 15:09:39 -08001951 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001952 port_name(port));
1953 }
Jesse Barnes776ad802011-01-04 15:09:39 -08001954
Daniel Vetterce99c252012-12-01 13:53:47 +01001955 if (pch_iir & SDE_AUX_MASK)
1956 dp_aux_irq_handler(dev);
1957
Jesse Barnes776ad802011-01-04 15:09:39 -08001958 if (pch_iir & SDE_GMBUS)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001959 gmbus_irq_handler(dev);
Jesse Barnes776ad802011-01-04 15:09:39 -08001960
1961 if (pch_iir & SDE_AUDIO_HDCP_MASK)
1962 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1963
1964 if (pch_iir & SDE_AUDIO_TRANS_MASK)
1965 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1966
1967 if (pch_iir & SDE_POISON)
1968 DRM_ERROR("PCH poison interrupt\n");
1969
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001970 if (pch_iir & SDE_FDI_MASK)
Damien Lespiau055e3932014-08-18 13:49:10 +01001971 for_each_pipe(dev_priv, pipe)
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001972 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1973 pipe_name(pipe),
1974 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -08001975
1976 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1977 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1978
1979 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1980 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1981
Jesse Barnes776ad802011-01-04 15:09:39 -08001982 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02001983 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
Paulo Zanoni86642812013-04-12 17:57:57 -03001984
1985 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02001986 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
Paulo Zanoni86642812013-04-12 17:57:57 -03001987}
1988
1989static void ivb_err_int_handler(struct drm_device *dev)
1990{
1991 struct drm_i915_private *dev_priv = dev->dev_private;
1992 u32 err_int = I915_READ(GEN7_ERR_INT);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001993 enum pipe pipe;
Paulo Zanoni86642812013-04-12 17:57:57 -03001994
Paulo Zanonide032bf2013-04-12 17:57:58 -03001995 if (err_int & ERR_INT_POISON)
1996 DRM_ERROR("Poison interrupt\n");
1997
Damien Lespiau055e3932014-08-18 13:49:10 +01001998 for_each_pipe(dev_priv, pipe) {
Daniel Vetter1f7247c2014-09-30 10:56:48 +02001999 if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
2000 intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
Paulo Zanoni86642812013-04-12 17:57:57 -03002001
Daniel Vetter5a69b892013-10-16 22:55:52 +02002002 if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
2003 if (IS_IVYBRIDGE(dev))
Daniel Vetter277de952013-10-18 16:37:07 +02002004 ivb_pipe_crc_irq_handler(dev, pipe);
Daniel Vetter5a69b892013-10-16 22:55:52 +02002005 else
Daniel Vetter277de952013-10-18 16:37:07 +02002006 hsw_pipe_crc_irq_handler(dev, pipe);
Daniel Vetter5a69b892013-10-16 22:55:52 +02002007 }
2008 }
Shuang He8bf1e9f2013-10-15 18:55:27 +01002009
Paulo Zanoni86642812013-04-12 17:57:57 -03002010 I915_WRITE(GEN7_ERR_INT, err_int);
2011}
2012
2013static void cpt_serr_int_handler(struct drm_device *dev)
2014{
2015 struct drm_i915_private *dev_priv = dev->dev_private;
2016 u32 serr_int = I915_READ(SERR_INT);
2017
Paulo Zanonide032bf2013-04-12 17:57:58 -03002018 if (serr_int & SERR_INT_POISON)
2019 DRM_ERROR("PCH poison interrupt\n");
2020
Paulo Zanoni86642812013-04-12 17:57:57 -03002021 if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002022 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
Paulo Zanoni86642812013-04-12 17:57:57 -03002023
2024 if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002025 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
Paulo Zanoni86642812013-04-12 17:57:57 -03002026
2027 if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002028 intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
Paulo Zanoni86642812013-04-12 17:57:57 -03002029
2030 I915_WRITE(SERR_INT, serr_int);
Jesse Barnes776ad802011-01-04 15:09:39 -08002031}
2032
Adam Jackson23e81d62012-06-06 15:45:44 -04002033static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
2034{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002035 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson23e81d62012-06-06 15:45:44 -04002036 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02002037 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
Dave Airlie13cf5502014-06-18 11:29:35 +10002038 u32 dig_hotplug_reg;
Adam Jackson23e81d62012-06-06 15:45:44 -04002039
Dave Airlie13cf5502014-06-18 11:29:35 +10002040 dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2041 I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2042
2043 intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
Daniel Vetter91d131d2013-06-27 17:52:14 +02002044
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03002045 if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2046 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
2047 SDE_AUDIO_POWER_SHIFT_CPT);
2048 DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2049 port_name(port));
2050 }
Adam Jackson23e81d62012-06-06 15:45:44 -04002051
2052 if (pch_iir & SDE_AUX_MASK_CPT)
Daniel Vetterce99c252012-12-01 13:53:47 +01002053 dp_aux_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04002054
2055 if (pch_iir & SDE_GMBUS_CPT)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01002056 gmbus_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04002057
2058 if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
2059 DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
2060
2061 if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
2062 DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
2063
2064 if (pch_iir & SDE_FDI_MASK_CPT)
Damien Lespiau055e3932014-08-18 13:49:10 +01002065 for_each_pipe(dev_priv, pipe)
Adam Jackson23e81d62012-06-06 15:45:44 -04002066 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
2067 pipe_name(pipe),
2068 I915_READ(FDI_RX_IIR(pipe)));
Paulo Zanoni86642812013-04-12 17:57:57 -03002069
2070 if (pch_iir & SDE_ERROR_CPT)
2071 cpt_serr_int_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04002072}
2073
Paulo Zanonic008bc62013-07-12 16:35:10 -03002074static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2075{
2076 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter40da17c2013-10-21 18:04:36 +02002077 enum pipe pipe;
Paulo Zanonic008bc62013-07-12 16:35:10 -03002078
2079 if (de_iir & DE_AUX_CHANNEL_A)
2080 dp_aux_irq_handler(dev);
2081
2082 if (de_iir & DE_GSE)
2083 intel_opregion_asle_intr(dev);
2084
Paulo Zanonic008bc62013-07-12 16:35:10 -03002085 if (de_iir & DE_POISON)
2086 DRM_ERROR("Poison interrupt\n");
2087
Damien Lespiau055e3932014-08-18 13:49:10 +01002088 for_each_pipe(dev_priv, pipe) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01002089 if (de_iir & DE_PIPE_VBLANK(pipe) &&
2090 intel_pipe_handle_vblank(dev, pipe))
2091 intel_check_page_flip(dev, pipe);
Paulo Zanonic008bc62013-07-12 16:35:10 -03002092
Daniel Vetter40da17c2013-10-21 18:04:36 +02002093 if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002094 intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
Paulo Zanonic008bc62013-07-12 16:35:10 -03002095
Daniel Vetter40da17c2013-10-21 18:04:36 +02002096 if (de_iir & DE_PIPE_CRC_DONE(pipe))
2097 i9xx_pipe_crc_irq_handler(dev, pipe);
Daniel Vetter5b3a8562013-10-16 22:55:48 +02002098
Daniel Vetter40da17c2013-10-21 18:04:36 +02002099 /* plane/pipes map 1:1 on ilk+ */
2100 if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
2101 intel_prepare_page_flip(dev, pipe);
2102 intel_finish_page_flip_plane(dev, pipe);
2103 }
Paulo Zanonic008bc62013-07-12 16:35:10 -03002104 }
2105
2106 /* check event from PCH */
2107 if (de_iir & DE_PCH_EVENT) {
2108 u32 pch_iir = I915_READ(SDEIIR);
2109
2110 if (HAS_PCH_CPT(dev))
2111 cpt_irq_handler(dev, pch_iir);
2112 else
2113 ibx_irq_handler(dev, pch_iir);
2114
2115 /* should clear PCH hotplug event before clear CPU irq */
2116 I915_WRITE(SDEIIR, pch_iir);
2117 }
2118
2119 if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2120 ironlake_rps_change_irq_handler(dev);
2121}
2122
Paulo Zanoni9719fb92013-07-12 16:35:11 -03002123static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
2124{
2125 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00002126 enum pipe pipe;
Paulo Zanoni9719fb92013-07-12 16:35:11 -03002127
2128 if (de_iir & DE_ERR_INT_IVB)
2129 ivb_err_int_handler(dev);
2130
2131 if (de_iir & DE_AUX_CHANNEL_A_IVB)
2132 dp_aux_irq_handler(dev);
2133
2134 if (de_iir & DE_GSE_IVB)
2135 intel_opregion_asle_intr(dev);
2136
Damien Lespiau055e3932014-08-18 13:49:10 +01002137 for_each_pipe(dev_priv, pipe) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01002138 if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2139 intel_pipe_handle_vblank(dev, pipe))
2140 intel_check_page_flip(dev, pipe);
Daniel Vetter40da17c2013-10-21 18:04:36 +02002141
2142 /* plane/pipes map 1:1 on ilk+ */
Damien Lespiau07d27e22014-03-03 17:31:46 +00002143 if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
2144 intel_prepare_page_flip(dev, pipe);
2145 intel_finish_page_flip_plane(dev, pipe);
Paulo Zanoni9719fb92013-07-12 16:35:11 -03002146 }
2147 }
2148
2149 /* check event from PCH */
2150 if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
2151 u32 pch_iir = I915_READ(SDEIIR);
2152
2153 cpt_irq_handler(dev, pch_iir);
2154
2155 /* clear PCH hotplug event before clear CPU irq */
2156 I915_WRITE(SDEIIR, pch_iir);
2157 }
2158}
2159
Oscar Mateo72c90f62014-06-16 16:10:57 +01002160/*
2161 * To handle irqs with the minimum potential races with fresh interrupts, we:
2162 * 1 - Disable Master Interrupt Control.
2163 * 2 - Find the source(s) of the interrupt.
2164 * 3 - Clear the Interrupt Identity bits (IIR).
2165 * 4 - Process the interrupt(s) that had bits set in the IIRs.
2166 * 5 - Re-enable Master Interrupt Control.
2167 */
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002168static irqreturn_t ironlake_irq_handler(int irq, void *arg)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002169{
Daniel Vetter45a83f82014-05-12 19:17:55 +02002170 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03002171 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002172 u32 de_iir, gt_iir, de_ier, sde_ier = 0;
Chris Wilson0e434062012-05-09 21:45:44 +01002173 irqreturn_t ret = IRQ_NONE;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002174
Paulo Zanoni86642812013-04-12 17:57:57 -03002175 /* We get interrupts on unclaimed registers, so check for this before we
2176 * do any I915_{READ,WRITE}. */
Chris Wilson907b28c2013-07-19 20:36:52 +01002177 intel_uncore_check_errors(dev);
Paulo Zanoni86642812013-04-12 17:57:57 -03002178
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002179 /* disable master interrupt before clearing iir */
2180 de_ier = I915_READ(DEIER);
2181 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Paulo Zanoni23a78512013-07-12 16:35:14 -03002182 POSTING_READ(DEIER);
Chris Wilson0e434062012-05-09 21:45:44 +01002183
Paulo Zanoni44498ae2013-02-22 17:05:28 -03002184 /* Disable south interrupts. We'll only write to SDEIIR once, so further
2185 * interrupts will will be stored on its back queue, and then we'll be
2186 * able to process them after we restore SDEIER (as soon as we restore
2187 * it, we'll get an interrupt if SDEIIR still has something to process
2188 * due to its back queue). */
Ben Widawskyab5c6082013-04-05 13:12:41 -07002189 if (!HAS_PCH_NOP(dev)) {
2190 sde_ier = I915_READ(SDEIER);
2191 I915_WRITE(SDEIER, 0);
2192 POSTING_READ(SDEIER);
2193 }
Paulo Zanoni44498ae2013-02-22 17:05:28 -03002194
Oscar Mateo72c90f62014-06-16 16:10:57 +01002195 /* Find, clear, then process each source of interrupt */
2196
Chris Wilson0e434062012-05-09 21:45:44 +01002197 gt_iir = I915_READ(GTIIR);
2198 if (gt_iir) {
Oscar Mateo72c90f62014-06-16 16:10:57 +01002199 I915_WRITE(GTIIR, gt_iir);
2200 ret = IRQ_HANDLED;
Paulo Zanonid8fc8a42013-07-19 18:57:55 -03002201 if (INTEL_INFO(dev)->gen >= 6)
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002202 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Paulo Zanonid8fc8a42013-07-19 18:57:55 -03002203 else
2204 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01002205 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002206
2207 de_iir = I915_READ(DEIIR);
Chris Wilson0e434062012-05-09 21:45:44 +01002208 if (de_iir) {
Oscar Mateo72c90f62014-06-16 16:10:57 +01002209 I915_WRITE(DEIIR, de_iir);
2210 ret = IRQ_HANDLED;
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002211 if (INTEL_INFO(dev)->gen >= 7)
2212 ivb_display_irq_handler(dev, de_iir);
2213 else
2214 ilk_display_irq_handler(dev, de_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01002215 }
2216
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002217 if (INTEL_INFO(dev)->gen >= 6) {
2218 u32 pm_iir = I915_READ(GEN6_PMIIR);
2219 if (pm_iir) {
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002220 I915_WRITE(GEN6_PMIIR, pm_iir);
2221 ret = IRQ_HANDLED;
Oscar Mateo72c90f62014-06-16 16:10:57 +01002222 gen6_rps_irq_handler(dev_priv, pm_iir);
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002223 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002224 }
2225
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002226 I915_WRITE(DEIER, de_ier);
2227 POSTING_READ(DEIER);
Ben Widawskyab5c6082013-04-05 13:12:41 -07002228 if (!HAS_PCH_NOP(dev)) {
2229 I915_WRITE(SDEIER, sde_ier);
2230 POSTING_READ(SDEIER);
2231 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002232
2233 return ret;
2234}
2235
Ben Widawskyabd58f02013-11-02 21:07:09 -07002236static irqreturn_t gen8_irq_handler(int irq, void *arg)
2237{
2238 struct drm_device *dev = arg;
2239 struct drm_i915_private *dev_priv = dev->dev_private;
2240 u32 master_ctl;
2241 irqreturn_t ret = IRQ_NONE;
2242 uint32_t tmp = 0;
Daniel Vetterc42664c2013-11-07 11:05:40 +01002243 enum pipe pipe;
Jesse Barnes88e04702014-11-13 17:51:48 +00002244 u32 aux_mask = GEN8_AUX_CHANNEL_A;
2245
2246 if (IS_GEN9(dev))
2247 aux_mask |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
2248 GEN9_AUX_CHANNEL_D;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002249
Ben Widawskyabd58f02013-11-02 21:07:09 -07002250 master_ctl = I915_READ(GEN8_MASTER_IRQ);
2251 master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2252 if (!master_ctl)
2253 return IRQ_NONE;
2254
2255 I915_WRITE(GEN8_MASTER_IRQ, 0);
2256 POSTING_READ(GEN8_MASTER_IRQ);
2257
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002258 /* Find, clear, then process each source of interrupt */
2259
Ben Widawskyabd58f02013-11-02 21:07:09 -07002260 ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2261
2262 if (master_ctl & GEN8_DE_MISC_IRQ) {
2263 tmp = I915_READ(GEN8_DE_MISC_IIR);
Ben Widawskyabd58f02013-11-02 21:07:09 -07002264 if (tmp) {
2265 I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2266 ret = IRQ_HANDLED;
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002267 if (tmp & GEN8_DE_MISC_GSE)
2268 intel_opregion_asle_intr(dev);
2269 else
2270 DRM_ERROR("Unexpected DE Misc interrupt\n");
Ben Widawskyabd58f02013-11-02 21:07:09 -07002271 }
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002272 else
2273 DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
Ben Widawskyabd58f02013-11-02 21:07:09 -07002274 }
2275
Daniel Vetter6d766f02013-11-07 14:49:55 +01002276 if (master_ctl & GEN8_DE_PORT_IRQ) {
2277 tmp = I915_READ(GEN8_DE_PORT_IIR);
Daniel Vetter6d766f02013-11-07 14:49:55 +01002278 if (tmp) {
2279 I915_WRITE(GEN8_DE_PORT_IIR, tmp);
2280 ret = IRQ_HANDLED;
Jesse Barnes88e04702014-11-13 17:51:48 +00002281
2282 if (tmp & aux_mask)
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002283 dp_aux_irq_handler(dev);
2284 else
2285 DRM_ERROR("Unexpected DE Port interrupt\n");
Daniel Vetter6d766f02013-11-07 14:49:55 +01002286 }
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002287 else
2288 DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
Daniel Vetter6d766f02013-11-07 14:49:55 +01002289 }
2290
Damien Lespiau055e3932014-08-18 13:49:10 +01002291 for_each_pipe(dev_priv, pipe) {
Damien Lespiau770de832014-03-20 20:45:01 +00002292 uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002293
Daniel Vetterc42664c2013-11-07 11:05:40 +01002294 if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2295 continue;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002296
Daniel Vetterc42664c2013-11-07 11:05:40 +01002297 pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
Daniel Vetterc42664c2013-11-07 11:05:40 +01002298 if (pipe_iir) {
2299 ret = IRQ_HANDLED;
2300 I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
Damien Lespiau770de832014-03-20 20:45:01 +00002301
Chris Wilsond6bbafa2014-09-05 07:13:24 +01002302 if (pipe_iir & GEN8_PIPE_VBLANK &&
2303 intel_pipe_handle_vblank(dev, pipe))
2304 intel_check_page_flip(dev, pipe);
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002305
Damien Lespiau770de832014-03-20 20:45:01 +00002306 if (IS_GEN9(dev))
2307 flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2308 else
2309 flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2310
2311 if (flip_done) {
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002312 intel_prepare_page_flip(dev, pipe);
2313 intel_finish_page_flip_plane(dev, pipe);
2314 }
2315
2316 if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
2317 hsw_pipe_crc_irq_handler(dev, pipe);
2318
Daniel Vetter1f7247c2014-09-30 10:56:48 +02002319 if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
2320 intel_cpu_fifo_underrun_irq_handler(dev_priv,
2321 pipe);
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002322
Damien Lespiau770de832014-03-20 20:45:01 +00002323
2324 if (IS_GEN9(dev))
2325 fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2326 else
2327 fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2328
2329 if (fault_errors)
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002330 DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
2331 pipe_name(pipe),
2332 pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
Daniel Vetterc42664c2013-11-07 11:05:40 +01002333 } else
Ben Widawskyabd58f02013-11-02 21:07:09 -07002334 DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2335 }
2336
Daniel Vetter92d03a82013-11-07 11:05:43 +01002337 if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
2338 /*
2339 * FIXME(BDW): Assume for now that the new interrupt handling
2340 * scheme also closed the SDE interrupt handling race we've seen
2341 * on older pch-split platforms. But this needs testing.
2342 */
2343 u32 pch_iir = I915_READ(SDEIIR);
Daniel Vetter92d03a82013-11-07 11:05:43 +01002344 if (pch_iir) {
2345 I915_WRITE(SDEIIR, pch_iir);
2346 ret = IRQ_HANDLED;
Oscar Mateo38cc46d2014-06-16 16:10:59 +01002347 cpt_irq_handler(dev, pch_iir);
2348 } else
2349 DRM_ERROR("The master control interrupt lied (SDE)!\n");
2350
Daniel Vetter92d03a82013-11-07 11:05:43 +01002351 }
2352
Ben Widawskyabd58f02013-11-02 21:07:09 -07002353 I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2354 POSTING_READ(GEN8_MASTER_IRQ);
2355
2356 return ret;
2357}
2358
Daniel Vetter17e1df02013-09-08 21:57:13 +02002359static void i915_error_wake_up(struct drm_i915_private *dev_priv,
2360 bool reset_completed)
2361{
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002362 struct intel_engine_cs *ring;
Daniel Vetter17e1df02013-09-08 21:57:13 +02002363 int i;
2364
2365 /*
2366 * Notify all waiters for GPU completion events that reset state has
2367 * been changed, and that they need to restart their wait after
2368 * checking for potential errors (and bail out to drop locks if there is
2369 * a gpu reset pending so that i915_error_work_func can acquire them).
2370 */
2371
2372 /* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
2373 for_each_ring(ring, dev_priv, i)
2374 wake_up_all(&ring->irq_queue);
2375
2376 /* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
2377 wake_up_all(&dev_priv->pending_flip_queue);
2378
2379 /*
2380 * Signal tasks blocked in i915_gem_wait_for_error that the pending
2381 * reset state is cleared.
2382 */
2383 if (reset_completed)
2384 wake_up_all(&dev_priv->gpu_error.reset_queue);
2385}
2386
Jesse Barnes8a905232009-07-11 16:48:03 -04002387/**
2388 * i915_error_work_func - do process context error handling work
2389 * @work: work struct
2390 *
2391 * Fire an error uevent so userspace can see that a hang or error
2392 * was detected.
2393 */
2394static void i915_error_work_func(struct work_struct *work)
2395{
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002396 struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
2397 work);
Jani Nikula2d1013d2014-03-31 14:27:17 +03002398 struct drm_i915_private *dev_priv =
2399 container_of(error, struct drm_i915_private, gpu_error);
Jesse Barnes8a905232009-07-11 16:48:03 -04002400 struct drm_device *dev = dev_priv->dev;
Ben Widawskycce723e2013-07-19 09:16:42 -07002401 char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2402 char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2403 char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
Daniel Vetter17e1df02013-09-08 21:57:13 +02002404 int ret;
Jesse Barnes8a905232009-07-11 16:48:03 -04002405
Dave Airlie5bdebb12013-10-11 14:07:25 +10002406 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -04002407
Daniel Vetter7db0ba22012-12-06 16:23:37 +01002408 /*
2409 * Note that there's only one work item which does gpu resets, so we
2410 * need not worry about concurrent gpu resets potentially incrementing
2411 * error->reset_counter twice. We only need to take care of another
2412 * racing irq/hangcheck declaring the gpu dead for a second time. A
2413 * quick check for that is good enough: schedule_work ensures the
2414 * correct ordering between hang detection and this work item, and since
2415 * the reset in-progress bit is only ever set by code outside of this
2416 * work we don't need to worry about any other races.
2417 */
2418 if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +01002419 DRM_DEBUG_DRIVER("resetting chip\n");
Dave Airlie5bdebb12013-10-11 14:07:25 +10002420 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
Daniel Vetter7db0ba22012-12-06 16:23:37 +01002421 reset_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002422
Daniel Vetter17e1df02013-09-08 21:57:13 +02002423 /*
Imre Deakf454c692014-04-23 01:09:04 +03002424 * In most cases it's guaranteed that we get here with an RPM
2425 * reference held, for example because there is a pending GPU
2426 * request that won't finish until the reset is done. This
2427 * isn't the case at least when we get here by doing a
2428 * simulated reset via debugs, so get an RPM reference.
2429 */
2430 intel_runtime_pm_get(dev_priv);
2431 /*
Daniel Vetter17e1df02013-09-08 21:57:13 +02002432 * All state reset _must_ be completed before we update the
2433 * reset counter, for otherwise waiters might miss the reset
2434 * pending state and not properly drop locks, resulting in
2435 * deadlocks with the reset work.
2436 */
Daniel Vetterf69061b2012-12-06 09:01:42 +01002437 ret = i915_reset(dev);
2438
Daniel Vetter17e1df02013-09-08 21:57:13 +02002439 intel_display_handle_reset(dev);
2440
Imre Deakf454c692014-04-23 01:09:04 +03002441 intel_runtime_pm_put(dev_priv);
2442
Daniel Vetterf69061b2012-12-06 09:01:42 +01002443 if (ret == 0) {
2444 /*
2445 * After all the gem state is reset, increment the reset
2446 * counter and wake up everyone waiting for the reset to
2447 * complete.
2448 *
2449 * Since unlock operations are a one-sided barrier only,
2450 * we need to insert a barrier here to order any seqno
2451 * updates before
2452 * the counter increment.
2453 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002454 smp_mb__before_atomic();
Daniel Vetterf69061b2012-12-06 09:01:42 +01002455 atomic_inc(&dev_priv->gpu_error.reset_counter);
2456
Dave Airlie5bdebb12013-10-11 14:07:25 +10002457 kobject_uevent_env(&dev->primary->kdev->kobj,
Daniel Vetterf69061b2012-12-06 09:01:42 +01002458 KOBJ_CHANGE, reset_done_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002459 } else {
Mika Kuoppala2ac0f452013-11-12 14:44:19 +02002460 atomic_set_mask(I915_WEDGED, &error->reset_counter);
Ben Gamarif316a422009-09-14 17:48:46 -04002461 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002462
Daniel Vetter17e1df02013-09-08 21:57:13 +02002463 /*
2464 * Note: The wake_up also serves as a memory barrier so that
2465 * waiters see the update value of the reset counter atomic_t.
2466 */
2467 i915_error_wake_up(dev_priv, true);
Ben Gamarif316a422009-09-14 17:48:46 -04002468 }
Jesse Barnes8a905232009-07-11 16:48:03 -04002469}
2470
Chris Wilson35aed2e2010-05-27 13:18:12 +01002471static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04002472{
2473 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybd9854f2012-08-23 15:18:09 -07002474 uint32_t instdone[I915_NUM_INSTDONE_REG];
Jesse Barnes8a905232009-07-11 16:48:03 -04002475 u32 eir = I915_READ(EIR);
Ben Widawsky050ee912012-08-22 11:32:15 -07002476 int pipe, i;
Jesse Barnes8a905232009-07-11 16:48:03 -04002477
Chris Wilson35aed2e2010-05-27 13:18:12 +01002478 if (!eir)
2479 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04002480
Joe Perchesa70491c2012-03-18 13:00:11 -07002481 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04002482
Ben Widawskybd9854f2012-08-23 15:18:09 -07002483 i915_get_extra_instdone(dev, instdone);
2484
Jesse Barnes8a905232009-07-11 16:48:03 -04002485 if (IS_G4X(dev)) {
2486 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
2487 u32 ipeir = I915_READ(IPEIR_I965);
2488
Joe Perchesa70491c2012-03-18 13:00:11 -07002489 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2490 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Ben Widawsky050ee912012-08-22 11:32:15 -07002491 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2492 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Joe Perchesa70491c2012-03-18 13:00:11 -07002493 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002494 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002495 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002496 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002497 }
2498 if (eir & GM45_ERROR_PAGE_TABLE) {
2499 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002500 pr_err("page table error\n");
2501 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002502 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002503 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002504 }
2505 }
2506
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002507 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002508 if (eir & I915_ERROR_PAGE_TABLE) {
2509 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002510 pr_err("page table error\n");
2511 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002512 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002513 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002514 }
2515 }
2516
2517 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002518 pr_err("memory refresh error:\n");
Damien Lespiau055e3932014-08-18 13:49:10 +01002519 for_each_pipe(dev_priv, pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07002520 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002521 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04002522 /* pipestat has already been acked */
2523 }
2524 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002525 pr_err("instruction error\n");
2526 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Ben Widawsky050ee912012-08-22 11:32:15 -07002527 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2528 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002529 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002530 u32 ipeir = I915_READ(IPEIR);
2531
Joe Perchesa70491c2012-03-18 13:00:11 -07002532 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
2533 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
Joe Perchesa70491c2012-03-18 13:00:11 -07002534 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04002535 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002536 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002537 } else {
2538 u32 ipeir = I915_READ(IPEIR_I965);
2539
Joe Perchesa70491c2012-03-18 13:00:11 -07002540 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2541 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07002542 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002543 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002544 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002545 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002546 }
2547 }
2548
2549 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002550 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002551 eir = I915_READ(EIR);
2552 if (eir) {
2553 /*
2554 * some errors might have become stuck,
2555 * mask them.
2556 */
2557 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
2558 I915_WRITE(EMR, I915_READ(EMR) | eir);
2559 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2560 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01002561}
2562
2563/**
2564 * i915_handle_error - handle an error interrupt
2565 * @dev: drm device
2566 *
2567 * Do some basic checking of regsiter state at error interrupt time and
2568 * dump it to the syslog. Also call i915_capture_error_state() to make
2569 * sure we get a record and make it available in debugfs. Fire a uevent
2570 * so userspace knows something bad happened (should trigger collection
2571 * of a ring dump etc.).
2572 */
Mika Kuoppala58174462014-02-25 17:11:26 +02002573void i915_handle_error(struct drm_device *dev, bool wedged,
2574 const char *fmt, ...)
Chris Wilson35aed2e2010-05-27 13:18:12 +01002575{
2576 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala58174462014-02-25 17:11:26 +02002577 va_list args;
2578 char error_msg[80];
Chris Wilson35aed2e2010-05-27 13:18:12 +01002579
Mika Kuoppala58174462014-02-25 17:11:26 +02002580 va_start(args, fmt);
2581 vscnprintf(error_msg, sizeof(error_msg), fmt, args);
2582 va_end(args);
2583
2584 i915_capture_error_state(dev, wedged, error_msg);
Chris Wilson35aed2e2010-05-27 13:18:12 +01002585 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04002586
Ben Gamariba1234d2009-09-14 17:48:47 -04002587 if (wedged) {
Daniel Vetterf69061b2012-12-06 09:01:42 +01002588 atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2589 &dev_priv->gpu_error.reset_counter);
Ben Gamariba1234d2009-09-14 17:48:47 -04002590
Ben Gamari11ed50e2009-09-14 17:48:45 -04002591 /*
Daniel Vetter17e1df02013-09-08 21:57:13 +02002592 * Wakeup waiting processes so that the reset work function
2593 * i915_error_work_func doesn't deadlock trying to grab various
2594 * locks. By bumping the reset counter first, the woken
2595 * processes will see a reset in progress and back off,
2596 * releasing their locks and then wait for the reset completion.
2597 * We must do this for _all_ gpu waiters that might hold locks
2598 * that the reset work needs to acquire.
2599 *
2600 * Note: The wake_up serves as the required memory barrier to
2601 * ensure that the waiters see the updated value of the reset
2602 * counter atomic_t.
Ben Gamari11ed50e2009-09-14 17:48:45 -04002603 */
Daniel Vetter17e1df02013-09-08 21:57:13 +02002604 i915_error_wake_up(dev_priv, false);
Ben Gamari11ed50e2009-09-14 17:48:45 -04002605 }
2606
Daniel Vetter122f46b2013-09-04 17:36:14 +02002607 /*
2608 * Our reset work can grab modeset locks (since it needs to reset the
2609 * state of outstanding pagelips). Hence it must not be run on our own
2610 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2611 * code will deadlock.
2612 */
2613 schedule_work(&dev_priv->gpu_error.work);
Jesse Barnes8a905232009-07-11 16:48:03 -04002614}
2615
Keith Packard42f52ef2008-10-18 19:39:29 -07002616/* Called from drm generic code, passed 'crtc' which
2617 * we use as a pipe index
2618 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002619static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002620{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002621 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002622 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002623
Chris Wilson5eddb702010-09-11 13:48:45 +01002624 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002625 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002626
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002627 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002628 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08002629 i915_enable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002630 PIPE_START_VBLANK_INTERRUPT_STATUS);
Keith Packarde9d21d72008-10-16 11:31:38 -07002631 else
Keith Packard7c463582008-11-04 02:03:27 -08002632 i915_enable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002633 PIPE_VBLANK_INTERRUPT_STATUS);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002634 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002635
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002636 return 0;
2637}
2638
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002639static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002640{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002641 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf796cf82011-04-07 13:58:17 -07002642 unsigned long irqflags;
Paulo Zanonib5184212013-07-12 20:00:08 -03002643 uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
Daniel Vetter40da17c2013-10-21 18:04:36 +02002644 DE_PIPE_VBLANK(pipe);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002645
2646 if (!i915_pipe_enabled(dev, pipe))
2647 return -EINVAL;
2648
2649 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Paulo Zanonib5184212013-07-12 20:00:08 -03002650 ironlake_enable_display_irq(dev_priv, bit);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002651 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2652
2653 return 0;
2654}
2655
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002656static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
2657{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002658 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002659 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002660
2661 if (!i915_pipe_enabled(dev, pipe))
2662 return -EINVAL;
2663
2664 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002665 i915_enable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002666 PIPE_START_VBLANK_INTERRUPT_STATUS);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002667 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2668
2669 return 0;
2670}
2671
Ben Widawskyabd58f02013-11-02 21:07:09 -07002672static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2673{
2674 struct drm_i915_private *dev_priv = dev->dev_private;
2675 unsigned long irqflags;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002676
2677 if (!i915_pipe_enabled(dev, pipe))
2678 return -EINVAL;
2679
2680 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter7167d7c2013-11-07 11:05:45 +01002681 dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
2682 I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2683 POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
Ben Widawskyabd58f02013-11-02 21:07:09 -07002684 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2685 return 0;
2686}
2687
Keith Packard42f52ef2008-10-18 19:39:29 -07002688/* Called from drm generic code, passed 'crtc' which
2689 * we use as a pipe index
2690 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002691static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002692{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002693 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002694 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002695
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002696 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002697 i915_disable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002698 PIPE_VBLANK_INTERRUPT_STATUS |
2699 PIPE_START_VBLANK_INTERRUPT_STATUS);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002700 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2701}
2702
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002703static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002704{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002705 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf796cf82011-04-07 13:58:17 -07002706 unsigned long irqflags;
Paulo Zanonib5184212013-07-12 20:00:08 -03002707 uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
Daniel Vetter40da17c2013-10-21 18:04:36 +02002708 DE_PIPE_VBLANK(pipe);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002709
2710 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Paulo Zanonib5184212013-07-12 20:00:08 -03002711 ironlake_disable_display_irq(dev_priv, bit);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002712 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2713}
2714
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002715static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
2716{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002717 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002718 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002719
2720 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002721 i915_disable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002722 PIPE_START_VBLANK_INTERRUPT_STATUS);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002723 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2724}
2725
Ben Widawskyabd58f02013-11-02 21:07:09 -07002726static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2727{
2728 struct drm_i915_private *dev_priv = dev->dev_private;
2729 unsigned long irqflags;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002730
2731 if (!i915_pipe_enabled(dev, pipe))
2732 return;
2733
2734 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter7167d7c2013-11-07 11:05:45 +01002735 dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
2736 I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2737 POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
Ben Widawskyabd58f02013-11-02 21:07:09 -07002738 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2739}
2740
Chris Wilson893eead2010-10-27 14:44:35 +01002741static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002742ring_last_seqno(struct intel_engine_cs *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08002743{
Chris Wilson893eead2010-10-27 14:44:35 +01002744 return list_entry(ring->request_list.prev,
2745 struct drm_i915_gem_request, list)->seqno;
2746}
2747
Chris Wilson9107e9d2013-06-10 11:20:20 +01002748static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002749ring_idle(struct intel_engine_cs *ring, u32 seqno)
Chris Wilson893eead2010-10-27 14:44:35 +01002750{
Chris Wilson9107e9d2013-06-10 11:20:20 +01002751 return (list_empty(&ring->request_list) ||
2752 i915_seqno_passed(seqno, ring_last_seqno(ring)));
Ben Gamarif65d9422009-09-14 17:48:44 -04002753}
2754
Daniel Vettera028c4b2014-03-15 00:08:56 +01002755static bool
2756ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2757{
2758 if (INTEL_INFO(dev)->gen >= 8) {
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002759 return (ipehr >> 23) == 0x1c;
Daniel Vettera028c4b2014-03-15 00:08:56 +01002760 } else {
2761 ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2762 return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2763 MI_SEMAPHORE_REGISTER);
2764 }
2765}
2766
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002767static struct intel_engine_cs *
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002768semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
Daniel Vetter921d42e2014-03-18 10:26:04 +01002769{
2770 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002771 struct intel_engine_cs *signaller;
Daniel Vetter921d42e2014-03-18 10:26:04 +01002772 int i;
2773
2774 if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002775 for_each_ring(signaller, dev_priv, i) {
2776 if (ring == signaller)
2777 continue;
2778
2779 if (offset == signaller->semaphore.signal_ggtt[ring->id])
2780 return signaller;
2781 }
Daniel Vetter921d42e2014-03-18 10:26:04 +01002782 } else {
2783 u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2784
2785 for_each_ring(signaller, dev_priv, i) {
2786 if(ring == signaller)
2787 continue;
2788
Ben Widawskyebc348b2014-04-29 14:52:28 -07002789 if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
Daniel Vetter921d42e2014-03-18 10:26:04 +01002790 return signaller;
2791 }
2792 }
2793
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002794 DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2795 ring->id, ipehr, offset);
Daniel Vetter921d42e2014-03-18 10:26:04 +01002796
2797 return NULL;
2798}
2799
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002800static struct intel_engine_cs *
2801semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
Chris Wilsona24a11e2013-03-14 17:52:05 +02002802{
2803 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Daniel Vetter88fe4292014-03-15 00:08:55 +01002804 u32 cmd, ipehr, head;
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002805 u64 offset = 0;
2806 int i, backwards;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002807
2808 ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
Daniel Vettera028c4b2014-03-15 00:08:56 +01002809 if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
Chris Wilson6274f212013-06-10 11:20:21 +01002810 return NULL;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002811
Daniel Vetter88fe4292014-03-15 00:08:55 +01002812 /*
2813 * HEAD is likely pointing to the dword after the actual command,
2814 * so scan backwards until we find the MBOX. But limit it to just 3
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002815 * or 4 dwords depending on the semaphore wait command size.
2816 * Note that we don't care about ACTHD here since that might
Daniel Vetter88fe4292014-03-15 00:08:55 +01002817 * point at at batch, and semaphores are always emitted into the
2818 * ringbuffer itself.
Chris Wilsona24a11e2013-03-14 17:52:05 +02002819 */
Daniel Vetter88fe4292014-03-15 00:08:55 +01002820 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002821 backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
Daniel Vetter88fe4292014-03-15 00:08:55 +01002822
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002823 for (i = backwards; i; --i) {
Daniel Vetter88fe4292014-03-15 00:08:55 +01002824 /*
2825 * Be paranoid and presume the hw has gone off into the wild -
2826 * our ring is smaller than what the hardware (and hence
2827 * HEAD_ADDR) allows. Also handles wrap-around.
2828 */
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002829 head &= ring->buffer->size - 1;
Daniel Vetter88fe4292014-03-15 00:08:55 +01002830
2831 /* This here seems to blow up */
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002832 cmd = ioread32(ring->buffer->virtual_start + head);
Chris Wilsona24a11e2013-03-14 17:52:05 +02002833 if (cmd == ipehr)
2834 break;
2835
Daniel Vetter88fe4292014-03-15 00:08:55 +01002836 head -= 4;
2837 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02002838
Daniel Vetter88fe4292014-03-15 00:08:55 +01002839 if (!i)
2840 return NULL;
2841
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002842 *seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
Rodrigo Vivia6cdb932014-06-30 09:53:39 -07002843 if (INTEL_INFO(ring->dev)->gen >= 8) {
2844 offset = ioread32(ring->buffer->virtual_start + head + 12);
2845 offset <<= 32;
2846 offset = ioread32(ring->buffer->virtual_start + head + 8);
2847 }
2848 return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
Chris Wilsona24a11e2013-03-14 17:52:05 +02002849}
2850
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002851static int semaphore_passed(struct intel_engine_cs *ring)
Chris Wilson6274f212013-06-10 11:20:21 +01002852{
2853 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002854 struct intel_engine_cs *signaller;
Chris Wilsona0d036b2014-07-19 12:40:42 +01002855 u32 seqno;
Chris Wilson6274f212013-06-10 11:20:21 +01002856
Chris Wilson4be17382014-06-06 10:22:29 +01002857 ring->hangcheck.deadlock++;
Chris Wilson6274f212013-06-10 11:20:21 +01002858
2859 signaller = semaphore_waits_for(ring, &seqno);
Chris Wilson4be17382014-06-06 10:22:29 +01002860 if (signaller == NULL)
2861 return -1;
2862
2863 /* Prevent pathological recursion due to driver bugs */
2864 if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
Chris Wilson6274f212013-06-10 11:20:21 +01002865 return -1;
2866
Chris Wilson4be17382014-06-06 10:22:29 +01002867 if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
2868 return 1;
2869
Chris Wilsona0d036b2014-07-19 12:40:42 +01002870 /* cursory check for an unkickable deadlock */
2871 if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2872 semaphore_passed(signaller) < 0)
Chris Wilson4be17382014-06-06 10:22:29 +01002873 return -1;
2874
2875 return 0;
Chris Wilson6274f212013-06-10 11:20:21 +01002876}
2877
2878static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
2879{
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002880 struct intel_engine_cs *ring;
Chris Wilson6274f212013-06-10 11:20:21 +01002881 int i;
2882
2883 for_each_ring(ring, dev_priv, i)
Chris Wilson4be17382014-06-06 10:22:29 +01002884 ring->hangcheck.deadlock = 0;
Chris Wilson6274f212013-06-10 11:20:21 +01002885}
2886
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03002887static enum intel_ring_hangcheck_action
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002888ring_stuck(struct intel_engine_cs *ring, u64 acthd)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002889{
2890 struct drm_device *dev = ring->dev;
2891 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002892 u32 tmp;
2893
Mika Kuoppalaf260fe72014-08-05 17:16:26 +03002894 if (acthd != ring->hangcheck.acthd) {
2895 if (acthd > ring->hangcheck.max_acthd) {
2896 ring->hangcheck.max_acthd = acthd;
2897 return HANGCHECK_ACTIVE;
2898 }
2899
2900 return HANGCHECK_ACTIVE_LOOP;
2901 }
Chris Wilson6274f212013-06-10 11:20:21 +01002902
Chris Wilson9107e9d2013-06-10 11:20:20 +01002903 if (IS_GEN2(dev))
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002904 return HANGCHECK_HUNG;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002905
2906 /* Is the chip hanging on a WAIT_FOR_EVENT?
2907 * If so we can simply poke the RB_WAIT bit
2908 * and break the hang. This should work on
2909 * all but the second generation chipsets.
2910 */
2911 tmp = I915_READ_CTL(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002912 if (tmp & RING_WAIT) {
Mika Kuoppala58174462014-02-25 17:11:26 +02002913 i915_handle_error(dev, false,
2914 "Kicking stuck wait on %s",
2915 ring->name);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002916 I915_WRITE_CTL(ring, tmp);
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002917 return HANGCHECK_KICK;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002918 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02002919
Chris Wilson6274f212013-06-10 11:20:21 +01002920 if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
2921 switch (semaphore_passed(ring)) {
2922 default:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002923 return HANGCHECK_HUNG;
Chris Wilson6274f212013-06-10 11:20:21 +01002924 case 1:
Mika Kuoppala58174462014-02-25 17:11:26 +02002925 i915_handle_error(dev, false,
2926 "Kicking stuck semaphore on %s",
2927 ring->name);
Chris Wilson6274f212013-06-10 11:20:21 +01002928 I915_WRITE_CTL(ring, tmp);
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002929 return HANGCHECK_KICK;
Chris Wilson6274f212013-06-10 11:20:21 +01002930 case 0:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002931 return HANGCHECK_WAIT;
Chris Wilson6274f212013-06-10 11:20:21 +01002932 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002933 }
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002934
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002935 return HANGCHECK_HUNG;
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002936}
2937
Ben Gamarif65d9422009-09-14 17:48:44 -04002938/**
2939 * This is called when the chip hasn't reported back with completed
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002940 * batchbuffers in a long time. We keep track per ring seqno progress and
2941 * if there are no progress, hangcheck score for that ring is increased.
2942 * Further, acthd is inspected to see if the ring is stuck. On stuck case
2943 * we kick the ring. If we see no progress on three subsequent calls
2944 * we assume chip is wedged and try to fix it by resetting the chip.
Ben Gamarif65d9422009-09-14 17:48:44 -04002945 */
Damien Lespiaua658b5d2013-08-08 22:28:56 +01002946static void i915_hangcheck_elapsed(unsigned long data)
Ben Gamarif65d9422009-09-14 17:48:44 -04002947{
2948 struct drm_device *dev = (struct drm_device *)data;
Jani Nikula2d1013d2014-03-31 14:27:17 +03002949 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002950 struct intel_engine_cs *ring;
Chris Wilsonb4519512012-05-11 14:29:30 +01002951 int i;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002952 int busy_count = 0, rings_hung = 0;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002953 bool stuck[I915_NUM_RINGS] = { 0 };
2954#define BUSY 1
2955#define KICK 5
2956#define HUNG 20
Chris Wilson893eead2010-10-27 14:44:35 +01002957
Jani Nikulad330a952014-01-21 11:24:25 +02002958 if (!i915.enable_hangcheck)
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07002959 return;
2960
Chris Wilsonb4519512012-05-11 14:29:30 +01002961 for_each_ring(ring, dev_priv, i) {
Chris Wilson50877442014-03-21 12:41:53 +00002962 u64 acthd;
2963 u32 seqno;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002964 bool busy = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01002965
Chris Wilson6274f212013-06-10 11:20:21 +01002966 semaphore_clear_deadlocks(dev_priv);
2967
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002968 seqno = ring->get_seqno(ring, false);
2969 acthd = intel_ring_get_active_head(ring);
Chris Wilsond1e61e72012-04-10 17:00:41 +01002970
Chris Wilson9107e9d2013-06-10 11:20:20 +01002971 if (ring->hangcheck.seqno == seqno) {
2972 if (ring_idle(ring, seqno)) {
Mika Kuoppalada661462013-09-06 16:03:28 +03002973 ring->hangcheck.action = HANGCHECK_IDLE;
2974
Chris Wilson9107e9d2013-06-10 11:20:20 +01002975 if (waitqueue_active(&ring->irq_queue)) {
2976 /* Issue a wake-up to catch stuck h/w. */
Chris Wilson094f9a52013-09-25 17:34:55 +01002977 if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
Daniel Vetterf4adcd22013-10-28 09:24:13 +01002978 if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
2979 DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
2980 ring->name);
2981 else
2982 DRM_INFO("Fake missed irq on %s\n",
2983 ring->name);
Chris Wilson094f9a52013-09-25 17:34:55 +01002984 wake_up_all(&ring->irq_queue);
2985 }
2986 /* Safeguard against driver failure */
2987 ring->hangcheck.score += BUSY;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002988 } else
2989 busy = false;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002990 } else {
Chris Wilson6274f212013-06-10 11:20:21 +01002991 /* We always increment the hangcheck score
2992 * if the ring is busy and still processing
2993 * the same request, so that no single request
2994 * can run indefinitely (such as a chain of
2995 * batches). The only time we do not increment
2996 * the hangcheck score on this ring, if this
2997 * ring is in a legitimate wait for another
2998 * ring. In that case the waiting ring is a
2999 * victim and we want to be sure we catch the
3000 * right culprit. Then every time we do kick
3001 * the ring, add a small increment to the
3002 * score so that we can catch a batch that is
3003 * being repeatedly kicked and so responsible
3004 * for stalling the machine.
3005 */
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03003006 ring->hangcheck.action = ring_stuck(ring,
3007 acthd);
3008
3009 switch (ring->hangcheck.action) {
Mika Kuoppalada661462013-09-06 16:03:28 +03003010 case HANGCHECK_IDLE:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03003011 case HANGCHECK_WAIT:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03003012 case HANGCHECK_ACTIVE:
Mika Kuoppalaf260fe72014-08-05 17:16:26 +03003013 break;
3014 case HANGCHECK_ACTIVE_LOOP:
Jani Nikulaea04cb32013-08-11 12:44:02 +03003015 ring->hangcheck.score += BUSY;
Chris Wilson6274f212013-06-10 11:20:21 +01003016 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03003017 case HANGCHECK_KICK:
Jani Nikulaea04cb32013-08-11 12:44:02 +03003018 ring->hangcheck.score += KICK;
Chris Wilson6274f212013-06-10 11:20:21 +01003019 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03003020 case HANGCHECK_HUNG:
Jani Nikulaea04cb32013-08-11 12:44:02 +03003021 ring->hangcheck.score += HUNG;
Chris Wilson6274f212013-06-10 11:20:21 +01003022 stuck[i] = true;
3023 break;
3024 }
Mika Kuoppala05407ff2013-05-30 09:04:29 +03003025 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01003026 } else {
Mika Kuoppalada661462013-09-06 16:03:28 +03003027 ring->hangcheck.action = HANGCHECK_ACTIVE;
3028
Chris Wilson9107e9d2013-06-10 11:20:20 +01003029 /* Gradually reduce the count so that we catch DoS
3030 * attempts across multiple batches.
3031 */
3032 if (ring->hangcheck.score > 0)
3033 ring->hangcheck.score--;
Mika Kuoppalaf260fe72014-08-05 17:16:26 +03003034
3035 ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
Chris Wilsond1e61e72012-04-10 17:00:41 +01003036 }
3037
Mika Kuoppala05407ff2013-05-30 09:04:29 +03003038 ring->hangcheck.seqno = seqno;
3039 ring->hangcheck.acthd = acthd;
Chris Wilson9107e9d2013-06-10 11:20:20 +01003040 busy_count += busy;
Chris Wilson893eead2010-10-27 14:44:35 +01003041 }
Eric Anholtb9201c12010-01-08 14:25:16 -08003042
Mika Kuoppala92cab732013-05-24 17:16:07 +03003043 for_each_ring(ring, dev_priv, i) {
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003044 if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
Daniel Vetterb8d88d12013-08-28 10:57:59 +02003045 DRM_INFO("%s on %s\n",
3046 stuck[i] ? "stuck" : "no progress",
3047 ring->name);
Chris Wilsona43adf02013-06-10 11:20:22 +01003048 rings_hung++;
Mika Kuoppala92cab732013-05-24 17:16:07 +03003049 }
3050 }
3051
Mika Kuoppala05407ff2013-05-30 09:04:29 +03003052 if (rings_hung)
Mika Kuoppala58174462014-02-25 17:11:26 +02003053 return i915_handle_error(dev, true, "Ring hung");
Ben Gamarif65d9422009-09-14 17:48:44 -04003054
Mika Kuoppala05407ff2013-05-30 09:04:29 +03003055 if (busy_count)
3056 /* Reset timer case chip hangs without another request
3057 * being added */
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03003058 i915_queue_hangcheck(dev);
3059}
3060
3061void i915_queue_hangcheck(struct drm_device *dev)
3062{
3063 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson672e7b72014-11-19 09:47:19 +00003064 struct timer_list *timer = &dev_priv->gpu_error.hangcheck_timer;
3065
Jani Nikulad330a952014-01-21 11:24:25 +02003066 if (!i915.enable_hangcheck)
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03003067 return;
3068
Chris Wilson672e7b72014-11-19 09:47:19 +00003069 /* Don't continually defer the hangcheck, but make sure it is active */
3070 if (!timer_pending(timer))
3071 timer->expires = round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES);
3072 mod_timer(timer, timer->expires);
Ben Gamarif65d9422009-09-14 17:48:44 -04003073}
3074
Paulo Zanoni1c69eb42014-04-01 15:37:23 -03003075static void ibx_irq_reset(struct drm_device *dev)
Paulo Zanoni91738a92013-06-05 14:21:51 -03003076{
3077 struct drm_i915_private *dev_priv = dev->dev_private;
3078
3079 if (HAS_PCH_NOP(dev))
3080 return;
3081
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003082 GEN5_IRQ_RESET(SDE);
Paulo Zanoni105b1222014-04-01 15:37:17 -03003083
3084 if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3085 I915_WRITE(SERR_INT, 0xffffffff);
Paulo Zanoni622364b2014-04-01 15:37:22 -03003086}
Paulo Zanoni105b1222014-04-01 15:37:17 -03003087
Paulo Zanoni622364b2014-04-01 15:37:22 -03003088/*
3089 * SDEIER is also touched by the interrupt handler to work around missed PCH
3090 * interrupts. Hence we can't update it after the interrupt handler is enabled -
3091 * instead we unconditionally enable all PCH interrupt sources here, but then
3092 * only unmask them as needed with SDEIMR.
3093 *
3094 * This function needs to be called before interrupts are enabled.
3095 */
3096static void ibx_irq_pre_postinstall(struct drm_device *dev)
3097{
3098 struct drm_i915_private *dev_priv = dev->dev_private;
3099
3100 if (HAS_PCH_NOP(dev))
3101 return;
3102
3103 WARN_ON(I915_READ(SDEIER) != 0);
Paulo Zanoni91738a92013-06-05 14:21:51 -03003104 I915_WRITE(SDEIER, 0xffffffff);
3105 POSTING_READ(SDEIER);
3106}
3107
Paulo Zanoni7c4d6642014-04-01 15:37:19 -03003108static void gen5_gt_irq_reset(struct drm_device *dev)
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02003109{
3110 struct drm_i915_private *dev_priv = dev->dev_private;
3111
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003112 GEN5_IRQ_RESET(GT);
Paulo Zanonia9d356a2014-04-01 15:37:09 -03003113 if (INTEL_INFO(dev)->gen >= 6)
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003114 GEN5_IRQ_RESET(GEN6_PM);
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02003115}
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117/* drm_dma.h hooks
3118*/
Paulo Zanonibe30b292014-04-01 15:37:25 -03003119static void ironlake_irq_reset(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003120{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003121 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003122
Paulo Zanoni0c841212014-04-01 15:37:27 -03003123 I915_WRITE(HWSTAM, 0xffffffff);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01003124
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003125 GEN5_IRQ_RESET(DE);
Paulo Zanonic6d954c2014-04-01 15:37:18 -03003126 if (IS_GEN7(dev))
3127 I915_WRITE(GEN7_ERR_INT, 0xffffffff);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003128
Paulo Zanoni7c4d6642014-04-01 15:37:19 -03003129 gen5_gt_irq_reset(dev);
Zhenyu Wangc6501562009-11-03 18:57:21 +00003130
Paulo Zanoni1c69eb42014-04-01 15:37:23 -03003131 ibx_irq_reset(dev);
Ben Widawsky7d991632013-05-28 19:22:25 -07003132}
3133
Ville Syrjälä70591a42014-10-30 19:42:58 +02003134static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
3135{
3136 enum pipe pipe;
3137
3138 I915_WRITE(PORT_HOTPLUG_EN, 0);
3139 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3140
3141 for_each_pipe(dev_priv, pipe)
3142 I915_WRITE(PIPESTAT(pipe), 0xffff);
3143
3144 GEN5_IRQ_RESET(VLV_);
3145}
3146
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003147static void valleyview_irq_preinstall(struct drm_device *dev)
3148{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003149 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003150
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003151 /* VLV magic */
3152 I915_WRITE(VLV_IMR, 0);
3153 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
3154 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
3155 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
3156
Paulo Zanoni7c4d6642014-04-01 15:37:19 -03003157 gen5_gt_irq_reset(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003158
Ville Syrjälä7c4cde32014-10-30 19:42:51 +02003159 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003160
Ville Syrjälä70591a42014-10-30 19:42:58 +02003161 vlv_display_irq_reset(dev_priv);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003162}
3163
Daniel Vetterd6e3cca2014-05-22 22:18:22 +02003164static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3165{
3166 GEN8_IRQ_RESET_NDX(GT, 0);
3167 GEN8_IRQ_RESET_NDX(GT, 1);
3168 GEN8_IRQ_RESET_NDX(GT, 2);
3169 GEN8_IRQ_RESET_NDX(GT, 3);
3170}
3171
Paulo Zanoni823f6b32014-04-01 15:37:26 -03003172static void gen8_irq_reset(struct drm_device *dev)
Ben Widawskyabd58f02013-11-02 21:07:09 -07003173{
3174 struct drm_i915_private *dev_priv = dev->dev_private;
3175 int pipe;
3176
Ben Widawskyabd58f02013-11-02 21:07:09 -07003177 I915_WRITE(GEN8_MASTER_IRQ, 0);
3178 POSTING_READ(GEN8_MASTER_IRQ);
3179
Daniel Vetterd6e3cca2014-05-22 22:18:22 +02003180 gen8_gt_irq_reset(dev_priv);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003181
Damien Lespiau055e3932014-08-18 13:49:10 +01003182 for_each_pipe(dev_priv, pipe)
Daniel Vetterf458ebb2014-09-30 10:56:39 +02003183 if (intel_display_power_is_enabled(dev_priv,
3184 POWER_DOMAIN_PIPE(pipe)))
Paulo Zanoni813bde42014-07-04 11:50:29 -03003185 GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003186
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003187 GEN5_IRQ_RESET(GEN8_DE_PORT_);
3188 GEN5_IRQ_RESET(GEN8_DE_MISC_);
3189 GEN5_IRQ_RESET(GEN8_PCU_);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003190
Paulo Zanoni1c69eb42014-04-01 15:37:23 -03003191 ibx_irq_reset(dev);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003192}
Ben Widawskyabd58f02013-11-02 21:07:09 -07003193
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003194void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv)
3195{
Paulo Zanoni1180e202014-10-07 18:02:52 -03003196 uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003197
Daniel Vetter13321782014-09-15 14:55:29 +02003198 spin_lock_irq(&dev_priv->irq_lock);
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003199 GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B, dev_priv->de_irq_mask[PIPE_B],
Paulo Zanoni1180e202014-10-07 18:02:52 -03003200 ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003201 GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C, dev_priv->de_irq_mask[PIPE_C],
Paulo Zanoni1180e202014-10-07 18:02:52 -03003202 ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
Daniel Vetter13321782014-09-15 14:55:29 +02003203 spin_unlock_irq(&dev_priv->irq_lock);
Paulo Zanonid49bdb02014-07-04 11:50:31 -03003204}
3205
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003206static void cherryview_irq_preinstall(struct drm_device *dev)
3207{
3208 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003209
3210 I915_WRITE(GEN8_MASTER_IRQ, 0);
3211 POSTING_READ(GEN8_MASTER_IRQ);
3212
Daniel Vetterd6e3cca2014-05-22 22:18:22 +02003213 gen8_gt_irq_reset(dev_priv);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003214
3215 GEN5_IRQ_RESET(GEN8_PCU_);
3216
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003217 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
3218
Ville Syrjälä70591a42014-10-30 19:42:58 +02003219 vlv_display_irq_reset(dev_priv);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003220}
3221
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003222static void ibx_hpd_irq_setup(struct drm_device *dev)
Keith Packard7fe0b972011-09-19 13:31:02 -07003223{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003224 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003225 struct intel_encoder *intel_encoder;
Daniel Vetterfee884e2013-07-04 23:35:21 +02003226 u32 hotplug_irqs, hotplug, enabled_irqs = 0;
Keith Packard7fe0b972011-09-19 13:31:02 -07003227
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003228 if (HAS_PCH_IBX(dev)) {
Daniel Vetterfee884e2013-07-04 23:35:21 +02003229 hotplug_irqs = SDE_HOTPLUG_MASK;
Damien Lespiaub2784e12014-08-05 11:29:37 +01003230 for_each_intel_encoder(dev, intel_encoder)
Egbert Eichcd569ae2013-04-16 13:36:57 +02003231 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02003232 enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003233 } else {
Daniel Vetterfee884e2013-07-04 23:35:21 +02003234 hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
Damien Lespiaub2784e12014-08-05 11:29:37 +01003235 for_each_intel_encoder(dev, intel_encoder)
Egbert Eichcd569ae2013-04-16 13:36:57 +02003236 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02003237 enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003238 }
3239
Daniel Vetterfee884e2013-07-04 23:35:21 +02003240 ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003241
3242 /*
3243 * Enable digital hotplug on the PCH, and configure the DP short pulse
3244 * duration to 2ms (which is the minimum in the Display Port spec)
3245 *
3246 * This register is the same on all known PCH chips.
3247 */
Keith Packard7fe0b972011-09-19 13:31:02 -07003248 hotplug = I915_READ(PCH_PORT_HOTPLUG);
3249 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
3250 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
3251 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
3252 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
3253 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3254}
3255
Paulo Zanonid46da432013-02-08 17:35:15 -02003256static void ibx_irq_postinstall(struct drm_device *dev)
3257{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003258 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003259 u32 mask;
Paulo Zanonid46da432013-02-08 17:35:15 -02003260
Daniel Vetter692a04c2013-05-29 21:43:05 +02003261 if (HAS_PCH_NOP(dev))
3262 return;
3263
Paulo Zanoni105b1222014-04-01 15:37:17 -03003264 if (HAS_PCH_IBX(dev))
Daniel Vetter5c673b62014-03-07 20:34:46 +01003265 mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
Paulo Zanoni105b1222014-04-01 15:37:17 -03003266 else
Daniel Vetter5c673b62014-03-07 20:34:46 +01003267 mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
Paulo Zanoni86642812013-04-12 17:57:57 -03003268
Paulo Zanoni337ba012014-04-01 15:37:16 -03003269 GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
Paulo Zanonid46da432013-02-08 17:35:15 -02003270 I915_WRITE(SDEIMR, ~mask);
Paulo Zanonid46da432013-02-08 17:35:15 -02003271}
3272
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003273static void gen5_gt_irq_postinstall(struct drm_device *dev)
3274{
3275 struct drm_i915_private *dev_priv = dev->dev_private;
3276 u32 pm_irqs, gt_irqs;
3277
3278 pm_irqs = gt_irqs = 0;
3279
3280 dev_priv->gt_irq_mask = ~0;
Ben Widawsky040d2ba2013-09-19 11:01:40 -07003281 if (HAS_L3_DPF(dev)) {
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003282 /* L3 parity interrupt is always unmasked. */
Ben Widawsky35a85ac2013-09-19 11:13:41 -07003283 dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
3284 gt_irqs |= GT_PARITY_ERROR(dev);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003285 }
3286
3287 gt_irqs |= GT_RENDER_USER_INTERRUPT;
3288 if (IS_GEN5(dev)) {
3289 gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
3290 ILK_BSD_USER_INTERRUPT;
3291 } else {
3292 gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
3293 }
3294
Paulo Zanoni35079892014-04-01 15:37:15 -03003295 GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003296
3297 if (INTEL_INFO(dev)->gen >= 6) {
Deepak Sa6706b42014-03-15 20:23:22 +05303298 pm_irqs |= dev_priv->pm_rps_events;
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003299
3300 if (HAS_VEBOX(dev))
3301 pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3302
Paulo Zanoni605cd252013-08-06 18:57:15 -03003303 dev_priv->pm_irq_mask = 0xffffffff;
Paulo Zanoni35079892014-04-01 15:37:15 -03003304 GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003305 }
3306}
3307
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003308static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003309{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003310 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003311 u32 display_mask, extra_mask;
3312
3313 if (INTEL_INFO(dev)->gen >= 7) {
3314 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3315 DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
3316 DE_PLANEB_FLIP_DONE_IVB |
Daniel Vetter5c673b62014-03-07 20:34:46 +01003317 DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003318 extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
Daniel Vetter5c673b62014-03-07 20:34:46 +01003319 DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003320 } else {
3321 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3322 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
Daniel Vetter5b3a8562013-10-16 22:55:48 +02003323 DE_AUX_CHANNEL_A |
Daniel Vetter5b3a8562013-10-16 22:55:48 +02003324 DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
3325 DE_POISON);
Daniel Vetter5c673b62014-03-07 20:34:46 +01003326 extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3327 DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003328 }
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003329
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003330 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003331
Paulo Zanoni0c841212014-04-01 15:37:27 -03003332 I915_WRITE(HWSTAM, 0xeffe);
3333
Paulo Zanoni622364b2014-04-01 15:37:22 -03003334 ibx_irq_pre_postinstall(dev);
3335
Paulo Zanoni35079892014-04-01 15:37:15 -03003336 GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003337
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003338 gen5_gt_irq_postinstall(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003339
Paulo Zanonid46da432013-02-08 17:35:15 -02003340 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07003341
Jesse Barnesf97108d2010-01-29 11:27:07 -08003342 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter6005ce42013-06-27 13:44:59 +02003343 /* Enable PCU event interrupts
3344 *
3345 * spinlocking not required here for correctness since interrupt
Daniel Vetter4bc9d432013-06-27 13:44:58 +02003346 * setup is guaranteed to run in single-threaded context. But we
3347 * need it to make the assert_spin_locked happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02003348 spin_lock_irq(&dev_priv->irq_lock);
Jesse Barnesf97108d2010-01-29 11:27:07 -08003349 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
Daniel Vetterd6207432014-09-15 14:55:27 +02003350 spin_unlock_irq(&dev_priv->irq_lock);
Jesse Barnesf97108d2010-01-29 11:27:07 -08003351 }
3352
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003353 return 0;
3354}
3355
Imre Deakf8b79e52014-03-04 19:23:07 +02003356static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3357{
3358 u32 pipestat_mask;
3359 u32 iir_mask;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003360 enum pipe pipe;
Imre Deakf8b79e52014-03-04 19:23:07 +02003361
3362 pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3363 PIPE_FIFO_UNDERRUN_STATUS;
3364
Ville Syrjälä120dda42014-10-30 19:42:57 +02003365 for_each_pipe(dev_priv, pipe)
3366 I915_WRITE(PIPESTAT(pipe), pipestat_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003367 POSTING_READ(PIPESTAT(PIPE_A));
3368
3369 pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3370 PIPE_CRC_DONE_INTERRUPT_STATUS;
3371
Ville Syrjälä120dda42014-10-30 19:42:57 +02003372 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3373 for_each_pipe(dev_priv, pipe)
3374 i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003375
3376 iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3377 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3378 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003379 if (IS_CHERRYVIEW(dev_priv))
3380 iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
Imre Deakf8b79e52014-03-04 19:23:07 +02003381 dev_priv->irq_mask &= ~iir_mask;
3382
3383 I915_WRITE(VLV_IIR, iir_mask);
3384 I915_WRITE(VLV_IIR, iir_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003385 I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
Ville Syrjälä76e41862014-10-30 19:42:54 +02003386 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3387 POSTING_READ(VLV_IMR);
Imre Deakf8b79e52014-03-04 19:23:07 +02003388}
3389
3390static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3391{
3392 u32 pipestat_mask;
3393 u32 iir_mask;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003394 enum pipe pipe;
Imre Deakf8b79e52014-03-04 19:23:07 +02003395
3396 iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3397 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
Imre Deak6c7fba02014-03-10 19:44:48 +02003398 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003399 if (IS_CHERRYVIEW(dev_priv))
3400 iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
Imre Deakf8b79e52014-03-04 19:23:07 +02003401
3402 dev_priv->irq_mask |= iir_mask;
Imre Deakf8b79e52014-03-04 19:23:07 +02003403 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
Ville Syrjälä76e41862014-10-30 19:42:54 +02003404 I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003405 I915_WRITE(VLV_IIR, iir_mask);
3406 I915_WRITE(VLV_IIR, iir_mask);
3407 POSTING_READ(VLV_IIR);
3408
3409 pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3410 PIPE_CRC_DONE_INTERRUPT_STATUS;
3411
Ville Syrjälä120dda42014-10-30 19:42:57 +02003412 i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3413 for_each_pipe(dev_priv, pipe)
3414 i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003415
3416 pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3417 PIPE_FIFO_UNDERRUN_STATUS;
Ville Syrjälä120dda42014-10-30 19:42:57 +02003418
3419 for_each_pipe(dev_priv, pipe)
3420 I915_WRITE(PIPESTAT(pipe), pipestat_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003421 POSTING_READ(PIPESTAT(PIPE_A));
3422}
3423
3424void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3425{
3426 assert_spin_locked(&dev_priv->irq_lock);
3427
3428 if (dev_priv->display_irqs_enabled)
3429 return;
3430
3431 dev_priv->display_irqs_enabled = true;
3432
Imre Deak950eaba2014-09-08 15:21:09 +03003433 if (intel_irqs_enabled(dev_priv))
Imre Deakf8b79e52014-03-04 19:23:07 +02003434 valleyview_display_irqs_install(dev_priv);
3435}
3436
3437void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3438{
3439 assert_spin_locked(&dev_priv->irq_lock);
3440
3441 if (!dev_priv->display_irqs_enabled)
3442 return;
3443
3444 dev_priv->display_irqs_enabled = false;
3445
Imre Deak950eaba2014-09-08 15:21:09 +03003446 if (intel_irqs_enabled(dev_priv))
Imre Deakf8b79e52014-03-04 19:23:07 +02003447 valleyview_display_irqs_uninstall(dev_priv);
3448}
3449
Ville Syrjälä0e6c9a92014-10-30 19:43:00 +02003450static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003451{
Imre Deakf8b79e52014-03-04 19:23:07 +02003452 dev_priv->irq_mask = ~0;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003453
Daniel Vetter20afbda2012-12-11 14:05:07 +01003454 I915_WRITE(PORT_HOTPLUG_EN, 0);
3455 POSTING_READ(PORT_HOTPLUG_EN);
3456
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003457 I915_WRITE(VLV_IIR, 0xffffffff);
Ville Syrjälä76e41862014-10-30 19:42:54 +02003458 I915_WRITE(VLV_IIR, 0xffffffff);
3459 I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3460 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3461 POSTING_READ(VLV_IMR);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003462
Daniel Vetterb79480b2013-06-27 17:52:10 +02003463 /* Interrupt setup is already guaranteed to be single-threaded, this is
3464 * just to make the assert_spin_locked check happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02003465 spin_lock_irq(&dev_priv->irq_lock);
Imre Deakf8b79e52014-03-04 19:23:07 +02003466 if (dev_priv->display_irqs_enabled)
3467 valleyview_display_irqs_install(dev_priv);
Daniel Vetterd6207432014-09-15 14:55:27 +02003468 spin_unlock_irq(&dev_priv->irq_lock);
Ville Syrjälä0e6c9a92014-10-30 19:43:00 +02003469}
3470
3471static int valleyview_irq_postinstall(struct drm_device *dev)
3472{
3473 struct drm_i915_private *dev_priv = dev->dev_private;
3474
3475 vlv_display_irq_postinstall(dev_priv);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003476
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003477 gen5_gt_irq_postinstall(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003478
3479 /* ack & enable invalid PTE error interrupts */
3480#if 0 /* FIXME: add support to irq handler for checking these bits */
3481 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
3482 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
3483#endif
3484
3485 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003486
3487 return 0;
3488}
3489
Ben Widawskyabd58f02013-11-02 21:07:09 -07003490static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3491{
Ben Widawskyabd58f02013-11-02 21:07:09 -07003492 /* These are interrupts we'll toggle with the ring mask register */
3493 uint32_t gt_interrupts[] = {
3494 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
Oscar Mateo73d477f2014-07-24 17:04:31 +01003495 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
Ben Widawskyabd58f02013-11-02 21:07:09 -07003496 GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
Oscar Mateo73d477f2014-07-24 17:04:31 +01003497 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
3498 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
Ben Widawskyabd58f02013-11-02 21:07:09 -07003499 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
Oscar Mateo73d477f2014-07-24 17:04:31 +01003500 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3501 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
3502 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
Ben Widawskyabd58f02013-11-02 21:07:09 -07003503 0,
Oscar Mateo73d477f2014-07-24 17:04:31 +01003504 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
3505 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
Ben Widawskyabd58f02013-11-02 21:07:09 -07003506 };
3507
Ben Widawsky09610212014-05-15 20:58:08 +03003508 dev_priv->pm_irq_mask = 0xffffffff;
Deepak S9a2d2d82014-08-22 08:32:40 +05303509 GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
3510 GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
3511 GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, dev_priv->pm_rps_events);
3512 GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003513}
3514
3515static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3516{
Damien Lespiau770de832014-03-20 20:45:01 +00003517 uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3518 uint32_t de_pipe_enables;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003519 int pipe;
Jesse Barnes88e04702014-11-13 17:51:48 +00003520 u32 aux_en = GEN8_AUX_CHANNEL_A;
Damien Lespiau770de832014-03-20 20:45:01 +00003521
Jesse Barnes88e04702014-11-13 17:51:48 +00003522 if (IS_GEN9(dev_priv)) {
Damien Lespiau770de832014-03-20 20:45:01 +00003523 de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3524 GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
Jesse Barnes88e04702014-11-13 17:51:48 +00003525 aux_en |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
3526 GEN9_AUX_CHANNEL_D;
3527 } else
Damien Lespiau770de832014-03-20 20:45:01 +00003528 de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3529 GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3530
3531 de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3532 GEN8_PIPE_FIFO_UNDERRUN;
3533
Daniel Vetter13b3a0a2013-11-07 15:31:52 +01003534 dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
3535 dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
3536 dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003537
Damien Lespiau055e3932014-08-18 13:49:10 +01003538 for_each_pipe(dev_priv, pipe)
Daniel Vetterf458ebb2014-09-30 10:56:39 +02003539 if (intel_display_power_is_enabled(dev_priv,
Paulo Zanoni813bde42014-07-04 11:50:29 -03003540 POWER_DOMAIN_PIPE(pipe)))
3541 GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3542 dev_priv->de_irq_mask[pipe],
3543 de_pipe_enables);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003544
Jesse Barnes88e04702014-11-13 17:51:48 +00003545 GEN5_IRQ_INIT(GEN8_DE_PORT_, ~aux_en, aux_en);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003546}
3547
3548static int gen8_irq_postinstall(struct drm_device *dev)
3549{
3550 struct drm_i915_private *dev_priv = dev->dev_private;
3551
Paulo Zanoni622364b2014-04-01 15:37:22 -03003552 ibx_irq_pre_postinstall(dev);
3553
Ben Widawskyabd58f02013-11-02 21:07:09 -07003554 gen8_gt_irq_postinstall(dev_priv);
3555 gen8_de_irq_postinstall(dev_priv);
3556
3557 ibx_irq_postinstall(dev);
3558
3559 I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3560 POSTING_READ(GEN8_MASTER_IRQ);
3561
3562 return 0;
3563}
3564
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003565static int cherryview_irq_postinstall(struct drm_device *dev)
3566{
3567 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003568
Ville Syrjäläc2b66792014-10-30 19:43:02 +02003569 vlv_display_irq_postinstall(dev_priv);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003570
3571 gen8_gt_irq_postinstall(dev_priv);
3572
3573 I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
3574 POSTING_READ(GEN8_MASTER_IRQ);
3575
3576 return 0;
3577}
3578
Ben Widawskyabd58f02013-11-02 21:07:09 -07003579static void gen8_irq_uninstall(struct drm_device *dev)
3580{
3581 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003582
3583 if (!dev_priv)
3584 return;
3585
Paulo Zanoni823f6b32014-04-01 15:37:26 -03003586 gen8_irq_reset(dev);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003587}
3588
Ville Syrjälä8ea0be42014-10-30 19:42:59 +02003589static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
3590{
3591 /* Interrupt setup is already guaranteed to be single-threaded, this is
3592 * just to make the assert_spin_locked check happy. */
3593 spin_lock_irq(&dev_priv->irq_lock);
3594 if (dev_priv->display_irqs_enabled)
3595 valleyview_display_irqs_uninstall(dev_priv);
3596 spin_unlock_irq(&dev_priv->irq_lock);
3597
3598 vlv_display_irq_reset(dev_priv);
3599
3600 dev_priv->irq_mask = 0;
3601}
3602
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003603static void valleyview_irq_uninstall(struct drm_device *dev)
3604{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003605 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003606
3607 if (!dev_priv)
3608 return;
3609
Imre Deak843d0e72014-04-14 20:24:23 +03003610 I915_WRITE(VLV_MASTER_IER, 0);
3611
Ville Syrjälä893fce82014-10-30 19:42:56 +02003612 gen5_gt_irq_reset(dev);
3613
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003614 I915_WRITE(HWSTAM, 0xffffffff);
Imre Deakf8b79e52014-03-04 19:23:07 +02003615
Ville Syrjälä8ea0be42014-10-30 19:42:59 +02003616 vlv_display_irq_uninstall(dev_priv);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003617}
3618
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003619static void cherryview_irq_uninstall(struct drm_device *dev)
3620{
3621 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003622
3623 if (!dev_priv)
3624 return;
3625
3626 I915_WRITE(GEN8_MASTER_IRQ, 0);
3627 POSTING_READ(GEN8_MASTER_IRQ);
3628
Ville Syrjäläa2c30fb2014-10-30 19:42:52 +02003629 gen8_gt_irq_reset(dev_priv);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003630
Ville Syrjäläa2c30fb2014-10-30 19:42:52 +02003631 GEN5_IRQ_RESET(GEN8_PCU_);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003632
Ville Syrjäläc2b66792014-10-30 19:43:02 +02003633 vlv_display_irq_uninstall(dev_priv);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003634}
3635
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003636static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003637{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003638 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07003639
3640 if (!dev_priv)
3641 return;
3642
Paulo Zanonibe30b292014-04-01 15:37:25 -03003643 ironlake_irq_reset(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003644}
3645
Chris Wilsonc2798b12012-04-22 21:13:57 +01003646static void i8xx_irq_preinstall(struct drm_device * dev)
3647{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003648 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003649 int pipe;
3650
Damien Lespiau055e3932014-08-18 13:49:10 +01003651 for_each_pipe(dev_priv, pipe)
Chris Wilsonc2798b12012-04-22 21:13:57 +01003652 I915_WRITE(PIPESTAT(pipe), 0);
3653 I915_WRITE16(IMR, 0xffff);
3654 I915_WRITE16(IER, 0x0);
3655 POSTING_READ16(IER);
3656}
3657
3658static int i8xx_irq_postinstall(struct drm_device *dev)
3659{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003660 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003661
Chris Wilsonc2798b12012-04-22 21:13:57 +01003662 I915_WRITE16(EMR,
3663 ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3664
3665 /* Unmask the interrupts that we always want on. */
3666 dev_priv->irq_mask =
3667 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3668 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3669 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3670 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3671 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3672 I915_WRITE16(IMR, dev_priv->irq_mask);
3673
3674 I915_WRITE16(IER,
3675 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3676 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3677 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3678 I915_USER_INTERRUPT);
3679 POSTING_READ16(IER);
3680
Daniel Vetter379ef822013-10-16 22:55:56 +02003681 /* Interrupt setup is already guaranteed to be single-threaded, this is
3682 * just to make the assert_spin_locked check happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02003683 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak755e9012014-02-10 18:42:47 +02003684 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3685 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
Daniel Vetterd6207432014-09-15 14:55:27 +02003686 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter379ef822013-10-16 22:55:56 +02003687
Chris Wilsonc2798b12012-04-22 21:13:57 +01003688 return 0;
3689}
3690
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003691/*
3692 * Returns true when a page flip has completed.
3693 */
3694static bool i8xx_handle_vblank(struct drm_device *dev,
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003695 int plane, int pipe, u32 iir)
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003696{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003697 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003698 u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003699
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03003700 if (!intel_pipe_handle_vblank(dev, pipe))
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003701 return false;
3702
3703 if ((iir & flip_pending) == 0)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003704 goto check_page_flip;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003705
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003706 intel_prepare_page_flip(dev, plane);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003707
3708 /* We detect FlipDone by looking for the change in PendingFlip from '1'
3709 * to '0' on the following vblank, i.e. IIR has the Pendingflip
3710 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
3711 * the flip is completed (no longer pending). Since this doesn't raise
3712 * an interrupt per se, we watch for the change at vblank.
3713 */
3714 if (I915_READ16(ISR) & flip_pending)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003715 goto check_page_flip;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003716
3717 intel_finish_page_flip(dev, pipe);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003718 return true;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003719
3720check_page_flip:
3721 intel_check_page_flip(dev, pipe);
3722 return false;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003723}
3724
Daniel Vetterff1f5252012-10-02 15:10:55 +02003725static irqreturn_t i8xx_irq_handler(int irq, void *arg)
Chris Wilsonc2798b12012-04-22 21:13:57 +01003726{
Daniel Vetter45a83f82014-05-12 19:17:55 +02003727 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03003728 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003729 u16 iir, new_iir;
3730 u32 pipe_stats[2];
Chris Wilsonc2798b12012-04-22 21:13:57 +01003731 int pipe;
3732 u16 flip_mask =
3733 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3734 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3735
Chris Wilsonc2798b12012-04-22 21:13:57 +01003736 iir = I915_READ16(IIR);
3737 if (iir == 0)
3738 return IRQ_NONE;
3739
3740 while (iir & ~flip_mask) {
3741 /* Can't rely on pipestat interrupt bit in iir as it might
3742 * have been cleared after the pipestat interrupt was received.
3743 * It doesn't set the bit in iir again, but it still produces
3744 * interrupts (for non-MSI).
3745 */
Daniel Vetter222c7f52014-09-15 14:55:28 +02003746 spin_lock(&dev_priv->irq_lock);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003747 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Mika Kuoppala58174462014-02-25 17:11:26 +02003748 i915_handle_error(dev, false,
3749 "Command parser error, iir 0x%08x",
3750 iir);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003751
Damien Lespiau055e3932014-08-18 13:49:10 +01003752 for_each_pipe(dev_priv, pipe) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01003753 int reg = PIPESTAT(pipe);
3754 pipe_stats[pipe] = I915_READ(reg);
3755
3756 /*
3757 * Clear the PIPE*STAT regs before the IIR
3758 */
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02003759 if (pipe_stats[pipe] & 0x8000ffff)
Chris Wilsonc2798b12012-04-22 21:13:57 +01003760 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003761 }
Daniel Vetter222c7f52014-09-15 14:55:28 +02003762 spin_unlock(&dev_priv->irq_lock);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003763
3764 I915_WRITE16(IIR, iir & ~flip_mask);
3765 new_iir = I915_READ16(IIR); /* Flush posted writes */
3766
Chris Wilsonc2798b12012-04-22 21:13:57 +01003767 if (iir & I915_USER_INTERRUPT)
3768 notify_ring(dev, &dev_priv->ring[RCS]);
3769
Damien Lespiau055e3932014-08-18 13:49:10 +01003770 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003771 int plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01003772 if (HAS_FBC(dev))
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003773 plane = !plane;
3774
Daniel Vetter4356d582013-10-16 22:55:55 +02003775 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003776 i8xx_handle_vblank(dev, plane, pipe, iir))
3777 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003778
Daniel Vetter4356d582013-10-16 22:55:55 +02003779 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
Daniel Vetter277de952013-10-18 16:37:07 +02003780 i9xx_pipe_crc_irq_handler(dev, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02003781
Daniel Vetter1f7247c2014-09-30 10:56:48 +02003782 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3783 intel_cpu_fifo_underrun_irq_handler(dev_priv,
3784 pipe);
Daniel Vetter4356d582013-10-16 22:55:55 +02003785 }
Chris Wilsonc2798b12012-04-22 21:13:57 +01003786
3787 iir = new_iir;
3788 }
3789
3790 return IRQ_HANDLED;
3791}
3792
3793static void i8xx_irq_uninstall(struct drm_device * dev)
3794{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003795 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003796 int pipe;
3797
Damien Lespiau055e3932014-08-18 13:49:10 +01003798 for_each_pipe(dev_priv, pipe) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01003799 /* Clear enable bits; then clear status bits */
3800 I915_WRITE(PIPESTAT(pipe), 0);
3801 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3802 }
3803 I915_WRITE16(IMR, 0xffff);
3804 I915_WRITE16(IER, 0x0);
3805 I915_WRITE16(IIR, I915_READ16(IIR));
3806}
3807
Chris Wilsona266c7d2012-04-24 22:59:44 +01003808static void i915_irq_preinstall(struct drm_device * dev)
3809{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003810 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003811 int pipe;
3812
Chris Wilsona266c7d2012-04-24 22:59:44 +01003813 if (I915_HAS_HOTPLUG(dev)) {
3814 I915_WRITE(PORT_HOTPLUG_EN, 0);
3815 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3816 }
3817
Chris Wilson00d98eb2012-04-24 22:59:48 +01003818 I915_WRITE16(HWSTAM, 0xeffe);
Damien Lespiau055e3932014-08-18 13:49:10 +01003819 for_each_pipe(dev_priv, pipe)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003820 I915_WRITE(PIPESTAT(pipe), 0);
3821 I915_WRITE(IMR, 0xffffffff);
3822 I915_WRITE(IER, 0x0);
3823 POSTING_READ(IER);
3824}
3825
3826static int i915_irq_postinstall(struct drm_device *dev)
3827{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003828 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson38bde182012-04-24 22:59:50 +01003829 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003830
Chris Wilson38bde182012-04-24 22:59:50 +01003831 I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3832
3833 /* Unmask the interrupts that we always want on. */
3834 dev_priv->irq_mask =
3835 ~(I915_ASLE_INTERRUPT |
3836 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3837 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3838 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3839 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3840 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3841
3842 enable_mask =
3843 I915_ASLE_INTERRUPT |
3844 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3845 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3846 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3847 I915_USER_INTERRUPT;
3848
Chris Wilsona266c7d2012-04-24 22:59:44 +01003849 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetter20afbda2012-12-11 14:05:07 +01003850 I915_WRITE(PORT_HOTPLUG_EN, 0);
3851 POSTING_READ(PORT_HOTPLUG_EN);
3852
Chris Wilsona266c7d2012-04-24 22:59:44 +01003853 /* Enable in IER... */
3854 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3855 /* and unmask in IMR */
3856 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3857 }
3858
Chris Wilsona266c7d2012-04-24 22:59:44 +01003859 I915_WRITE(IMR, dev_priv->irq_mask);
3860 I915_WRITE(IER, enable_mask);
3861 POSTING_READ(IER);
3862
Jani Nikulaf49e38d2013-04-29 13:02:54 +03003863 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003864
Daniel Vetter379ef822013-10-16 22:55:56 +02003865 /* Interrupt setup is already guaranteed to be single-threaded, this is
3866 * just to make the assert_spin_locked check happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02003867 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak755e9012014-02-10 18:42:47 +02003868 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3869 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
Daniel Vetterd6207432014-09-15 14:55:27 +02003870 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter379ef822013-10-16 22:55:56 +02003871
Daniel Vetter20afbda2012-12-11 14:05:07 +01003872 return 0;
3873}
3874
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003875/*
3876 * Returns true when a page flip has completed.
3877 */
3878static bool i915_handle_vblank(struct drm_device *dev,
3879 int plane, int pipe, u32 iir)
3880{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003881 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003882 u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
3883
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03003884 if (!intel_pipe_handle_vblank(dev, pipe))
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003885 return false;
3886
3887 if ((iir & flip_pending) == 0)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003888 goto check_page_flip;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003889
3890 intel_prepare_page_flip(dev, plane);
3891
3892 /* We detect FlipDone by looking for the change in PendingFlip from '1'
3893 * to '0' on the following vblank, i.e. IIR has the Pendingflip
3894 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
3895 * the flip is completed (no longer pending). Since this doesn't raise
3896 * an interrupt per se, we watch for the change at vblank.
3897 */
3898 if (I915_READ(ISR) & flip_pending)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003899 goto check_page_flip;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003900
3901 intel_finish_page_flip(dev, pipe);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003902 return true;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003903
3904check_page_flip:
3905 intel_check_page_flip(dev, pipe);
3906 return false;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003907}
3908
Daniel Vetterff1f5252012-10-02 15:10:55 +02003909static irqreturn_t i915_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003910{
Daniel Vetter45a83f82014-05-12 19:17:55 +02003911 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03003912 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8291ee92012-04-24 22:59:47 +01003913 u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
Chris Wilson38bde182012-04-24 22:59:50 +01003914 u32 flip_mask =
3915 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3916 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilson38bde182012-04-24 22:59:50 +01003917 int pipe, ret = IRQ_NONE;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003918
Chris Wilsona266c7d2012-04-24 22:59:44 +01003919 iir = I915_READ(IIR);
Chris Wilson38bde182012-04-24 22:59:50 +01003920 do {
3921 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson8291ee92012-04-24 22:59:47 +01003922 bool blc_event = false;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003923
3924 /* Can't rely on pipestat interrupt bit in iir as it might
3925 * have been cleared after the pipestat interrupt was received.
3926 * It doesn't set the bit in iir again, but it still produces
3927 * interrupts (for non-MSI).
3928 */
Daniel Vetter222c7f52014-09-15 14:55:28 +02003929 spin_lock(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003930 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Mika Kuoppala58174462014-02-25 17:11:26 +02003931 i915_handle_error(dev, false,
3932 "Command parser error, iir 0x%08x",
3933 iir);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003934
Damien Lespiau055e3932014-08-18 13:49:10 +01003935 for_each_pipe(dev_priv, pipe) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003936 int reg = PIPESTAT(pipe);
3937 pipe_stats[pipe] = I915_READ(reg);
3938
Chris Wilson38bde182012-04-24 22:59:50 +01003939 /* Clear the PIPE*STAT regs before the IIR */
Chris Wilsona266c7d2012-04-24 22:59:44 +01003940 if (pipe_stats[pipe] & 0x8000ffff) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003941 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilson38bde182012-04-24 22:59:50 +01003942 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003943 }
3944 }
Daniel Vetter222c7f52014-09-15 14:55:28 +02003945 spin_unlock(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003946
3947 if (!irq_received)
3948 break;
3949
Chris Wilsona266c7d2012-04-24 22:59:44 +01003950 /* Consume port. Then clear IIR or we'll miss events */
Ville Syrjälä16c6c562014-04-01 10:54:36 +03003951 if (I915_HAS_HOTPLUG(dev) &&
3952 iir & I915_DISPLAY_PORT_INTERRUPT)
3953 i9xx_hpd_irq_handler(dev);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003954
Chris Wilson38bde182012-04-24 22:59:50 +01003955 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003956 new_iir = I915_READ(IIR); /* Flush posted writes */
3957
Chris Wilsona266c7d2012-04-24 22:59:44 +01003958 if (iir & I915_USER_INTERRUPT)
3959 notify_ring(dev, &dev_priv->ring[RCS]);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003960
Damien Lespiau055e3932014-08-18 13:49:10 +01003961 for_each_pipe(dev_priv, pipe) {
Chris Wilson38bde182012-04-24 22:59:50 +01003962 int plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01003963 if (HAS_FBC(dev))
Chris Wilson38bde182012-04-24 22:59:50 +01003964 plane = !plane;
Ville Syrjälä5e2032d2013-02-19 15:16:38 +02003965
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003966 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
3967 i915_handle_vblank(dev, plane, pipe, iir))
3968 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003969
3970 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3971 blc_event = true;
Daniel Vetter4356d582013-10-16 22:55:55 +02003972
3973 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
Daniel Vetter277de952013-10-18 16:37:07 +02003974 i9xx_pipe_crc_irq_handler(dev, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02003975
Daniel Vetter1f7247c2014-09-30 10:56:48 +02003976 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3977 intel_cpu_fifo_underrun_irq_handler(dev_priv,
3978 pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003979 }
3980
Chris Wilsona266c7d2012-04-24 22:59:44 +01003981 if (blc_event || (iir & I915_ASLE_INTERRUPT))
3982 intel_opregion_asle_intr(dev);
3983
3984 /* With MSI, interrupts are only generated when iir
3985 * transitions from zero to nonzero. If another bit got
3986 * set while we were handling the existing iir bits, then
3987 * we would never get another interrupt.
3988 *
3989 * This is fine on non-MSI as well, as if we hit this path
3990 * we avoid exiting the interrupt handler only to generate
3991 * another one.
3992 *
3993 * Note that for MSI this could cause a stray interrupt report
3994 * if an interrupt landed in the time between writing IIR and
3995 * the posting read. This should be rare enough to never
3996 * trigger the 99% of 100,000 interrupts test for disabling
3997 * stray interrupts.
3998 */
Chris Wilson38bde182012-04-24 22:59:50 +01003999 ret = IRQ_HANDLED;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004000 iir = new_iir;
Chris Wilson38bde182012-04-24 22:59:50 +01004001 } while (iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004002
4003 return ret;
4004}
4005
4006static void i915_irq_uninstall(struct drm_device * dev)
4007{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004008 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004009 int pipe;
4010
Chris Wilsona266c7d2012-04-24 22:59:44 +01004011 if (I915_HAS_HOTPLUG(dev)) {
4012 I915_WRITE(PORT_HOTPLUG_EN, 0);
4013 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4014 }
4015
Chris Wilson00d98eb2012-04-24 22:59:48 +01004016 I915_WRITE16(HWSTAM, 0xffff);
Damien Lespiau055e3932014-08-18 13:49:10 +01004017 for_each_pipe(dev_priv, pipe) {
Chris Wilson55b39752012-04-24 22:59:49 +01004018 /* Clear enable bits; then clear status bits */
Chris Wilsona266c7d2012-04-24 22:59:44 +01004019 I915_WRITE(PIPESTAT(pipe), 0);
Chris Wilson55b39752012-04-24 22:59:49 +01004020 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4021 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01004022 I915_WRITE(IMR, 0xffffffff);
4023 I915_WRITE(IER, 0x0);
4024
Chris Wilsona266c7d2012-04-24 22:59:44 +01004025 I915_WRITE(IIR, I915_READ(IIR));
4026}
4027
4028static void i965_irq_preinstall(struct drm_device * dev)
4029{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004030 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004031 int pipe;
4032
Chris Wilsonadca4732012-05-11 18:01:31 +01004033 I915_WRITE(PORT_HOTPLUG_EN, 0);
4034 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01004035
4036 I915_WRITE(HWSTAM, 0xeffe);
Damien Lespiau055e3932014-08-18 13:49:10 +01004037 for_each_pipe(dev_priv, pipe)
Chris Wilsona266c7d2012-04-24 22:59:44 +01004038 I915_WRITE(PIPESTAT(pipe), 0);
4039 I915_WRITE(IMR, 0xffffffff);
4040 I915_WRITE(IER, 0x0);
4041 POSTING_READ(IER);
4042}
4043
4044static int i965_irq_postinstall(struct drm_device *dev)
4045{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004046 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004047 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004048 u32 error_mask;
4049
Chris Wilsona266c7d2012-04-24 22:59:44 +01004050 /* Unmask the interrupts that we always want on. */
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004051 dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
Chris Wilsonadca4732012-05-11 18:01:31 +01004052 I915_DISPLAY_PORT_INTERRUPT |
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004053 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4054 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4055 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4056 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4057 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4058
4059 enable_mask = ~dev_priv->irq_mask;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02004060 enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4061 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004062 enable_mask |= I915_USER_INTERRUPT;
4063
4064 if (IS_G4X(dev))
4065 enable_mask |= I915_BSD_USER_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004066
Daniel Vetterb79480b2013-06-27 17:52:10 +02004067 /* Interrupt setup is already guaranteed to be single-threaded, this is
4068 * just to make the assert_spin_locked check happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02004069 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak755e9012014-02-10 18:42:47 +02004070 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4071 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4072 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
Daniel Vetterd6207432014-09-15 14:55:27 +02004073 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004074
Chris Wilsona266c7d2012-04-24 22:59:44 +01004075 /*
4076 * Enable some error detection, note the instruction error mask
4077 * bit is reserved, so we leave it masked.
4078 */
4079 if (IS_G4X(dev)) {
4080 error_mask = ~(GM45_ERROR_PAGE_TABLE |
4081 GM45_ERROR_MEM_PRIV |
4082 GM45_ERROR_CP_PRIV |
4083 I915_ERROR_MEMORY_REFRESH);
4084 } else {
4085 error_mask = ~(I915_ERROR_PAGE_TABLE |
4086 I915_ERROR_MEMORY_REFRESH);
4087 }
4088 I915_WRITE(EMR, error_mask);
4089
4090 I915_WRITE(IMR, dev_priv->irq_mask);
4091 I915_WRITE(IER, enable_mask);
4092 POSTING_READ(IER);
4093
Daniel Vetter20afbda2012-12-11 14:05:07 +01004094 I915_WRITE(PORT_HOTPLUG_EN, 0);
4095 POSTING_READ(PORT_HOTPLUG_EN);
4096
Jani Nikulaf49e38d2013-04-29 13:02:54 +03004097 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01004098
4099 return 0;
4100}
4101
Egbert Eichbac56d52013-02-25 12:06:51 -05004102static void i915_hpd_irq_setup(struct drm_device *dev)
Daniel Vetter20afbda2012-12-11 14:05:07 +01004103{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004104 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eichcd569ae2013-04-16 13:36:57 +02004105 struct intel_encoder *intel_encoder;
Daniel Vetter20afbda2012-12-11 14:05:07 +01004106 u32 hotplug_en;
4107
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02004108 assert_spin_locked(&dev_priv->irq_lock);
4109
Egbert Eichbac56d52013-02-25 12:06:51 -05004110 if (I915_HAS_HOTPLUG(dev)) {
4111 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4112 hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4113 /* Note HDMI and DP share hotplug bits */
Egbert Eiche5868a32013-02-28 04:17:12 -05004114 /* enable bits are the same for all generations */
Damien Lespiaub2784e12014-08-05 11:29:37 +01004115 for_each_intel_encoder(dev, intel_encoder)
Egbert Eichcd569ae2013-04-16 13:36:57 +02004116 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4117 hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
Egbert Eichbac56d52013-02-25 12:06:51 -05004118 /* Programming the CRT detection parameters tends
4119 to generate a spurious hotplug event about three
4120 seconds later. So just do it once.
4121 */
4122 if (IS_G4X(dev))
4123 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
Daniel Vetter85fc95b2013-03-27 15:47:11 +01004124 hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
Egbert Eichbac56d52013-02-25 12:06:51 -05004125 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004126
Egbert Eichbac56d52013-02-25 12:06:51 -05004127 /* Ignore TV since it's buggy */
4128 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4129 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01004130}
4131
Daniel Vetterff1f5252012-10-02 15:10:55 +02004132static irqreturn_t i965_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01004133{
Daniel Vetter45a83f82014-05-12 19:17:55 +02004134 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03004135 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004136 u32 iir, new_iir;
4137 u32 pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01004138 int ret = IRQ_NONE, pipe;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02004139 u32 flip_mask =
4140 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4141 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004142
Chris Wilsona266c7d2012-04-24 22:59:44 +01004143 iir = I915_READ(IIR);
4144
Chris Wilsona266c7d2012-04-24 22:59:44 +01004145 for (;;) {
Ville Syrjälä501e01d2014-01-17 11:35:15 +02004146 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson2c8ba292012-04-24 22:59:46 +01004147 bool blc_event = false;
4148
Chris Wilsona266c7d2012-04-24 22:59:44 +01004149 /* Can't rely on pipestat interrupt bit in iir as it might
4150 * have been cleared after the pipestat interrupt was received.
4151 * It doesn't set the bit in iir again, but it still produces
4152 * interrupts (for non-MSI).
4153 */
Daniel Vetter222c7f52014-09-15 14:55:28 +02004154 spin_lock(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004155 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Mika Kuoppala58174462014-02-25 17:11:26 +02004156 i915_handle_error(dev, false,
4157 "Command parser error, iir 0x%08x",
4158 iir);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004159
Damien Lespiau055e3932014-08-18 13:49:10 +01004160 for_each_pipe(dev_priv, pipe) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004161 int reg = PIPESTAT(pipe);
4162 pipe_stats[pipe] = I915_READ(reg);
4163
4164 /*
4165 * Clear the PIPE*STAT regs before the IIR
4166 */
4167 if (pipe_stats[pipe] & 0x8000ffff) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004168 I915_WRITE(reg, pipe_stats[pipe]);
Ville Syrjälä501e01d2014-01-17 11:35:15 +02004169 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004170 }
4171 }
Daniel Vetter222c7f52014-09-15 14:55:28 +02004172 spin_unlock(&dev_priv->irq_lock);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004173
4174 if (!irq_received)
4175 break;
4176
4177 ret = IRQ_HANDLED;
4178
4179 /* Consume port. Then clear IIR or we'll miss events */
Ville Syrjälä16c6c562014-04-01 10:54:36 +03004180 if (iir & I915_DISPLAY_PORT_INTERRUPT)
4181 i9xx_hpd_irq_handler(dev);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004182
Ville Syrjälä21ad8332013-02-19 15:16:39 +02004183 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004184 new_iir = I915_READ(IIR); /* Flush posted writes */
4185
Chris Wilsona266c7d2012-04-24 22:59:44 +01004186 if (iir & I915_USER_INTERRUPT)
4187 notify_ring(dev, &dev_priv->ring[RCS]);
4188 if (iir & I915_BSD_USER_INTERRUPT)
4189 notify_ring(dev, &dev_priv->ring[VCS]);
4190
Damien Lespiau055e3932014-08-18 13:49:10 +01004191 for_each_pipe(dev_priv, pipe) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01004192 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02004193 i915_handle_vblank(dev, pipe, pipe, iir))
4194 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004195
4196 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4197 blc_event = true;
Daniel Vetter4356d582013-10-16 22:55:55 +02004198
4199 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
Daniel Vetter277de952013-10-18 16:37:07 +02004200 i9xx_pipe_crc_irq_handler(dev, pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004201
Daniel Vetter1f7247c2014-09-30 10:56:48 +02004202 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
4203 intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004204 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01004205
4206 if (blc_event || (iir & I915_ASLE_INTERRUPT))
4207 intel_opregion_asle_intr(dev);
4208
Daniel Vetter515ac2b2012-12-01 13:53:44 +01004209 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4210 gmbus_irq_handler(dev);
4211
Chris Wilsona266c7d2012-04-24 22:59:44 +01004212 /* With MSI, interrupts are only generated when iir
4213 * transitions from zero to nonzero. If another bit got
4214 * set while we were handling the existing iir bits, then
4215 * we would never get another interrupt.
4216 *
4217 * This is fine on non-MSI as well, as if we hit this path
4218 * we avoid exiting the interrupt handler only to generate
4219 * another one.
4220 *
4221 * Note that for MSI this could cause a stray interrupt report
4222 * if an interrupt landed in the time between writing IIR and
4223 * the posting read. This should be rare enough to never
4224 * trigger the 99% of 100,000 interrupts test for disabling
4225 * stray interrupts.
4226 */
4227 iir = new_iir;
4228 }
4229
4230 return ret;
4231}
4232
4233static void i965_irq_uninstall(struct drm_device * dev)
4234{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004235 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004236 int pipe;
4237
4238 if (!dev_priv)
4239 return;
4240
Chris Wilsonadca4732012-05-11 18:01:31 +01004241 I915_WRITE(PORT_HOTPLUG_EN, 0);
4242 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01004243
4244 I915_WRITE(HWSTAM, 0xffffffff);
Damien Lespiau055e3932014-08-18 13:49:10 +01004245 for_each_pipe(dev_priv, pipe)
Chris Wilsona266c7d2012-04-24 22:59:44 +01004246 I915_WRITE(PIPESTAT(pipe), 0);
4247 I915_WRITE(IMR, 0xffffffff);
4248 I915_WRITE(IER, 0x0);
4249
Damien Lespiau055e3932014-08-18 13:49:10 +01004250 for_each_pipe(dev_priv, pipe)
Chris Wilsona266c7d2012-04-24 22:59:44 +01004251 I915_WRITE(PIPESTAT(pipe),
4252 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4253 I915_WRITE(IIR, I915_READ(IIR));
4254}
4255
Daniel Vetter4cb21832014-09-15 14:55:26 +02004256static void intel_hpd_irq_reenable_work(struct work_struct *work)
Egbert Eichac4c16c2013-04-16 13:36:58 +02004257{
Imre Deak63237512014-08-18 15:37:02 +03004258 struct drm_i915_private *dev_priv =
4259 container_of(work, typeof(*dev_priv),
4260 hotplug_reenable_work.work);
Egbert Eichac4c16c2013-04-16 13:36:58 +02004261 struct drm_device *dev = dev_priv->dev;
4262 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichac4c16c2013-04-16 13:36:58 +02004263 int i;
4264
Imre Deak63237512014-08-18 15:37:02 +03004265 intel_runtime_pm_get(dev_priv);
4266
Daniel Vetter4cb21832014-09-15 14:55:26 +02004267 spin_lock_irq(&dev_priv->irq_lock);
Egbert Eichac4c16c2013-04-16 13:36:58 +02004268 for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4269 struct drm_connector *connector;
4270
4271 if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4272 continue;
4273
4274 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4275
4276 list_for_each_entry(connector, &mode_config->connector_list, head) {
4277 struct intel_connector *intel_connector = to_intel_connector(connector);
4278
4279 if (intel_connector->encoder->hpd_pin == i) {
4280 if (connector->polled != intel_connector->polled)
4281 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03004282 connector->name);
Egbert Eichac4c16c2013-04-16 13:36:58 +02004283 connector->polled = intel_connector->polled;
4284 if (!connector->polled)
4285 connector->polled = DRM_CONNECTOR_POLL_HPD;
4286 }
4287 }
4288 }
4289 if (dev_priv->display.hpd_irq_setup)
4290 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetter4cb21832014-09-15 14:55:26 +02004291 spin_unlock_irq(&dev_priv->irq_lock);
Imre Deak63237512014-08-18 15:37:02 +03004292
4293 intel_runtime_pm_put(dev_priv);
Egbert Eichac4c16c2013-04-16 13:36:58 +02004294}
4295
Daniel Vetterfca52a52014-09-30 10:56:45 +02004296/**
4297 * intel_irq_init - initializes irq support
4298 * @dev_priv: i915 device instance
4299 *
4300 * This function initializes all the irq support including work items, timers
4301 * and all the vtables. It does not setup the interrupt itself though.
4302 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004303void intel_irq_init(struct drm_i915_private *dev_priv)
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004304{
Daniel Vetterb9632912014-09-30 10:56:44 +02004305 struct drm_device *dev = dev_priv->dev;
Chris Wilson8b2e3262012-04-24 22:59:41 +01004306
4307 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Dave Airlie13cf5502014-06-18 11:29:35 +10004308 INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
Daniel Vetter99584db2012-11-14 17:14:04 +01004309 INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02004310 INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004311 INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
Chris Wilson8b2e3262012-04-24 22:59:41 +01004312
Deepak Sa6706b42014-03-15 20:23:22 +05304313 /* Let's track the enabled rps events */
Daniel Vetterb9632912014-09-30 10:56:44 +02004314 if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
Ville Syrjälä6c65a582014-08-29 14:14:07 +03004315 /* WaGsvRC0ResidencyMethod:vlv */
Deepak S31685c22014-07-03 17:33:01 -04004316 dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
4317 else
4318 dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
Deepak Sa6706b42014-03-15 20:23:22 +05304319
Daniel Vetter99584db2012-11-14 17:14:04 +01004320 setup_timer(&dev_priv->gpu_error.hangcheck_timer,
4321 i915_hangcheck_elapsed,
Daniel Vetter61bac782012-12-01 21:03:21 +01004322 (unsigned long) dev);
Imre Deak63237512014-08-18 15:37:02 +03004323 INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
Daniel Vetter4cb21832014-09-15 14:55:26 +02004324 intel_hpd_irq_reenable_work);
Daniel Vetter61bac782012-12-01 21:03:21 +01004325
Tomas Janousek97a19a22012-12-08 13:48:13 +01004326 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01004327
Daniel Vetterb9632912014-09-30 10:56:44 +02004328 if (IS_GEN2(dev_priv)) {
Ville Syrjälä4cdb83e2013-10-11 21:52:44 +03004329 dev->max_vblank_count = 0;
4330 dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
Daniel Vetterb9632912014-09-30 10:56:44 +02004331 } else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004332 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4333 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Ville Syrjälä391f75e2013-09-25 19:55:26 +03004334 } else {
4335 dev->driver->get_vblank_counter = i915_get_vblank_counter;
4336 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004337 }
4338
Ville Syrjälä21da2702014-08-06 14:49:55 +03004339 /*
4340 * Opt out of the vblank disable timer on everything except gen2.
4341 * Gen2 doesn't have a hardware frame counter and so depends on
4342 * vblank interrupts to produce sane vblank seuquence numbers.
4343 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004344 if (!IS_GEN2(dev_priv))
Ville Syrjälä21da2702014-08-06 14:49:55 +03004345 dev->vblank_disable_immediate = true;
4346
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +03004347 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Keith Packardc3613de2011-08-12 17:05:54 -07004348 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +03004349 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4350 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004351
Daniel Vetterb9632912014-09-30 10:56:44 +02004352 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä43f328d2014-04-09 20:40:52 +03004353 dev->driver->irq_handler = cherryview_irq_handler;
4354 dev->driver->irq_preinstall = cherryview_irq_preinstall;
4355 dev->driver->irq_postinstall = cherryview_irq_postinstall;
4356 dev->driver->irq_uninstall = cherryview_irq_uninstall;
4357 dev->driver->enable_vblank = valleyview_enable_vblank;
4358 dev->driver->disable_vblank = valleyview_disable_vblank;
4359 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Daniel Vetterb9632912014-09-30 10:56:44 +02004360 } else if (IS_VALLEYVIEW(dev_priv)) {
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07004361 dev->driver->irq_handler = valleyview_irq_handler;
4362 dev->driver->irq_preinstall = valleyview_irq_preinstall;
4363 dev->driver->irq_postinstall = valleyview_irq_postinstall;
4364 dev->driver->irq_uninstall = valleyview_irq_uninstall;
4365 dev->driver->enable_vblank = valleyview_enable_vblank;
4366 dev->driver->disable_vblank = valleyview_disable_vblank;
Egbert Eichfa00abe2013-02-25 12:06:48 -05004367 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Daniel Vetterb9632912014-09-30 10:56:44 +02004368 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawskyabd58f02013-11-02 21:07:09 -07004369 dev->driver->irq_handler = gen8_irq_handler;
Daniel Vetter723761b2014-05-22 17:56:34 +02004370 dev->driver->irq_preinstall = gen8_irq_reset;
Ben Widawskyabd58f02013-11-02 21:07:09 -07004371 dev->driver->irq_postinstall = gen8_irq_postinstall;
4372 dev->driver->irq_uninstall = gen8_irq_uninstall;
4373 dev->driver->enable_vblank = gen8_enable_vblank;
4374 dev->driver->disable_vblank = gen8_disable_vblank;
4375 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004376 } else if (HAS_PCH_SPLIT(dev)) {
4377 dev->driver->irq_handler = ironlake_irq_handler;
Daniel Vetter723761b2014-05-22 17:56:34 +02004378 dev->driver->irq_preinstall = ironlake_irq_reset;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004379 dev->driver->irq_postinstall = ironlake_irq_postinstall;
4380 dev->driver->irq_uninstall = ironlake_irq_uninstall;
4381 dev->driver->enable_vblank = ironlake_enable_vblank;
4382 dev->driver->disable_vblank = ironlake_disable_vblank;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01004383 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004384 } else {
Daniel Vetterb9632912014-09-30 10:56:44 +02004385 if (INTEL_INFO(dev_priv)->gen == 2) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01004386 dev->driver->irq_preinstall = i8xx_irq_preinstall;
4387 dev->driver->irq_postinstall = i8xx_irq_postinstall;
4388 dev->driver->irq_handler = i8xx_irq_handler;
4389 dev->driver->irq_uninstall = i8xx_irq_uninstall;
Daniel Vetterb9632912014-09-30 10:56:44 +02004390 } else if (INTEL_INFO(dev_priv)->gen == 3) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004391 dev->driver->irq_preinstall = i915_irq_preinstall;
4392 dev->driver->irq_postinstall = i915_irq_postinstall;
4393 dev->driver->irq_uninstall = i915_irq_uninstall;
4394 dev->driver->irq_handler = i915_irq_handler;
Daniel Vetter20afbda2012-12-11 14:05:07 +01004395 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01004396 } else {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004397 dev->driver->irq_preinstall = i965_irq_preinstall;
4398 dev->driver->irq_postinstall = i965_irq_postinstall;
4399 dev->driver->irq_uninstall = i965_irq_uninstall;
4400 dev->driver->irq_handler = i965_irq_handler;
Egbert Eichbac56d52013-02-25 12:06:51 -05004401 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01004402 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004403 dev->driver->enable_vblank = i915_enable_vblank;
4404 dev->driver->disable_vblank = i915_disable_vblank;
4405 }
4406}
Daniel Vetter20afbda2012-12-11 14:05:07 +01004407
Daniel Vetterfca52a52014-09-30 10:56:45 +02004408/**
4409 * intel_hpd_init - initializes and enables hpd support
4410 * @dev_priv: i915 device instance
4411 *
4412 * This function enables the hotplug support. It requires that interrupts have
4413 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
4414 * poll request can run concurrently to other code, so locking rules must be
4415 * obeyed.
4416 *
4417 * This is a separate step from interrupt enabling to simplify the locking rules
4418 * in the driver load and resume code.
4419 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004420void intel_hpd_init(struct drm_i915_private *dev_priv)
Daniel Vetter20afbda2012-12-11 14:05:07 +01004421{
Daniel Vetterb9632912014-09-30 10:56:44 +02004422 struct drm_device *dev = dev_priv->dev;
Egbert Eich821450c2013-04-16 13:36:55 +02004423 struct drm_mode_config *mode_config = &dev->mode_config;
4424 struct drm_connector *connector;
4425 int i;
Daniel Vetter20afbda2012-12-11 14:05:07 +01004426
Egbert Eich821450c2013-04-16 13:36:55 +02004427 for (i = 1; i < HPD_NUM_PINS; i++) {
4428 dev_priv->hpd_stats[i].hpd_cnt = 0;
4429 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4430 }
4431 list_for_each_entry(connector, &mode_config->connector_list, head) {
4432 struct intel_connector *intel_connector = to_intel_connector(connector);
4433 connector->polled = intel_connector->polled;
Dave Airlie0e32b392014-05-02 14:02:48 +10004434 if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
4435 connector->polled = DRM_CONNECTOR_POLL_HPD;
4436 if (intel_connector->mst_port)
Egbert Eich821450c2013-04-16 13:36:55 +02004437 connector->polled = DRM_CONNECTOR_POLL_HPD;
4438 }
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02004439
4440 /* Interrupt setup is already guaranteed to be single-threaded, this is
4441 * just to make the assert_spin_locked checks happy. */
Daniel Vetterd6207432014-09-15 14:55:27 +02004442 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetter20afbda2012-12-11 14:05:07 +01004443 if (dev_priv->display.hpd_irq_setup)
4444 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterd6207432014-09-15 14:55:27 +02004445 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter20afbda2012-12-11 14:05:07 +01004446}
Paulo Zanonic67a4702013-08-19 13:18:09 -03004447
Daniel Vetterfca52a52014-09-30 10:56:45 +02004448/**
4449 * intel_irq_install - enables the hardware interrupt
4450 * @dev_priv: i915 device instance
4451 *
4452 * This function enables the hardware interrupt handling, but leaves the hotplug
4453 * handling still disabled. It is called after intel_irq_init().
4454 *
4455 * In the driver load and resume code we need working interrupts in a few places
4456 * but don't want to deal with the hassle of concurrent probe and hotplug
4457 * workers. Hence the split into this two-stage approach.
4458 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +02004459int intel_irq_install(struct drm_i915_private *dev_priv)
4460{
4461 /*
4462 * We enable some interrupt sources in our postinstall hooks, so mark
4463 * interrupts as enabled _before_ actually enabling them to avoid
4464 * special cases in our ordering checks.
4465 */
4466 dev_priv->pm.irqs_enabled = true;
4467
4468 return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
4469}
4470
Daniel Vetterfca52a52014-09-30 10:56:45 +02004471/**
4472 * intel_irq_uninstall - finilizes all irq handling
4473 * @dev_priv: i915 device instance
4474 *
4475 * This stops interrupt and hotplug handling and unregisters and frees all
4476 * resources acquired in the init functions.
4477 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +02004478void intel_irq_uninstall(struct drm_i915_private *dev_priv)
4479{
4480 drm_irq_uninstall(dev_priv->dev);
4481 intel_hpd_cancel_work(dev_priv);
4482 dev_priv->pm.irqs_enabled = false;
4483}
4484
Daniel Vetterfca52a52014-09-30 10:56:45 +02004485/**
4486 * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4487 * @dev_priv: i915 device instance
4488 *
4489 * This function is used to disable interrupts at runtime, both in the runtime
4490 * pm and the system suspend/resume code.
4491 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004492void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03004493{
Daniel Vetterb9632912014-09-30 10:56:44 +02004494 dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
Daniel Vetter2aeb7d32014-09-30 10:56:43 +02004495 dev_priv->pm.irqs_enabled = false;
Paulo Zanonic67a4702013-08-19 13:18:09 -03004496}
4497
Daniel Vetterfca52a52014-09-30 10:56:45 +02004498/**
4499 * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4500 * @dev_priv: i915 device instance
4501 *
4502 * This function is used to enable interrupts at runtime, both in the runtime
4503 * pm and the system suspend/resume code.
4504 */
Daniel Vetterb9632912014-09-30 10:56:44 +02004505void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03004506{
Daniel Vetter2aeb7d32014-09-30 10:56:43 +02004507 dev_priv->pm.irqs_enabled = true;
Daniel Vetterb9632912014-09-30 10:56:44 +02004508 dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4509 dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03004510}