blob: 83ad4b43bcd660ab91b4237c5e840bb1c0f55768 [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
Egbert Eiche5868a32013-02-28 04:17:12 -050040static const u32 hpd_ibx[] = {
41 [HPD_CRT] = SDE_CRT_HOTPLUG,
42 [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
43 [HPD_PORT_B] = SDE_PORTB_HOTPLUG,
44 [HPD_PORT_C] = SDE_PORTC_HOTPLUG,
45 [HPD_PORT_D] = SDE_PORTD_HOTPLUG
46};
47
48static const u32 hpd_cpt[] = {
49 [HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
Daniel Vetter73c352a2013-03-26 22:38:43 +010050 [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
Egbert Eiche5868a32013-02-28 04:17:12 -050051 [HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
52 [HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
53 [HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
54};
55
56static const u32 hpd_mask_i915[] = {
57 [HPD_CRT] = CRT_HOTPLUG_INT_EN,
58 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
59 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
60 [HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
61 [HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
62 [HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
63};
64
Daniel Vetter704cfb82013-12-18 09:08:43 +010065static const u32 hpd_status_g4x[] = {
Egbert Eiche5868a32013-02-28 04:17:12 -050066 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
67 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
68 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
69 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
70 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
71 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
72};
73
Egbert Eiche5868a32013-02-28 04:17:12 -050074static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
75 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
76 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
77 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
78 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
79 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
80 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
81};
82
Paulo Zanoni5c502442014-04-01 15:37:11 -030083/* IIR can theoretically queue up two events. Be paranoid. */
Paulo Zanonif86f3fb2014-04-01 15:37:14 -030084#define GEN8_IRQ_RESET_NDX(type, which) do { \
Paulo Zanoni5c502442014-04-01 15:37:11 -030085 I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
86 POSTING_READ(GEN8_##type##_IMR(which)); \
87 I915_WRITE(GEN8_##type##_IER(which), 0); \
88 I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
89 POSTING_READ(GEN8_##type##_IIR(which)); \
90 I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
91 POSTING_READ(GEN8_##type##_IIR(which)); \
92} while (0)
93
Paulo Zanonif86f3fb2014-04-01 15:37:14 -030094#define GEN5_IRQ_RESET(type) do { \
Paulo Zanonia9d356a2014-04-01 15:37:09 -030095 I915_WRITE(type##IMR, 0xffffffff); \
Paulo Zanoni5c502442014-04-01 15:37:11 -030096 POSTING_READ(type##IMR); \
Paulo Zanonia9d356a2014-04-01 15:37:09 -030097 I915_WRITE(type##IER, 0); \
Paulo Zanoni5c502442014-04-01 15:37:11 -030098 I915_WRITE(type##IIR, 0xffffffff); \
99 POSTING_READ(type##IIR); \
100 I915_WRITE(type##IIR, 0xffffffff); \
101 POSTING_READ(type##IIR); \
Paulo Zanonia9d356a2014-04-01 15:37:09 -0300102} while (0)
103
Paulo Zanoni337ba012014-04-01 15:37:16 -0300104/*
105 * We should clear IMR at preinstall/uninstall, and just check at postinstall.
106 */
107#define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
108 u32 val = I915_READ(reg); \
109 if (val) { \
110 WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
111 (reg), val); \
112 I915_WRITE((reg), 0xffffffff); \
113 POSTING_READ(reg); \
114 I915_WRITE((reg), 0xffffffff); \
115 POSTING_READ(reg); \
116 } \
117} while (0)
118
Paulo Zanoni35079892014-04-01 15:37:15 -0300119#define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
Paulo Zanoni337ba012014-04-01 15:37:16 -0300120 GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
Paulo Zanoni35079892014-04-01 15:37:15 -0300121 I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
122 I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
123 POSTING_READ(GEN8_##type##_IER(which)); \
124} while (0)
125
126#define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
Paulo Zanoni337ba012014-04-01 15:37:16 -0300127 GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
Paulo Zanoni35079892014-04-01 15:37:15 -0300128 I915_WRITE(type##IMR, (imr_val)); \
129 I915_WRITE(type##IER, (ier_val)); \
130 POSTING_READ(type##IER); \
131} while (0)
132
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800133/* For display hotplug interrupt */
Chris Wilson995b67622010-08-20 13:23:26 +0100134static void
Jani Nikula2d1013d2014-03-31 14:27:17 +0300135ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800136{
Daniel Vetter4bc9d432013-06-27 13:44:58 +0200137 assert_spin_locked(&dev_priv->irq_lock);
138
Paulo Zanoni730488b2014-03-07 20:12:32 -0300139 if (WARN_ON(dev_priv->pm.irqs_disabled))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300140 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300141
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000142 if ((dev_priv->irq_mask & mask) != 0) {
143 dev_priv->irq_mask &= ~mask;
144 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000145 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800146 }
147}
148
Paulo Zanoni0ff98002013-02-22 17:05:31 -0300149static void
Jani Nikula2d1013d2014-03-31 14:27:17 +0300150ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800151{
Daniel Vetter4bc9d432013-06-27 13:44:58 +0200152 assert_spin_locked(&dev_priv->irq_lock);
153
Paulo Zanoni730488b2014-03-07 20:12:32 -0300154 if (WARN_ON(dev_priv->pm.irqs_disabled))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300155 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300156
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000157 if ((dev_priv->irq_mask & mask) != mask) {
158 dev_priv->irq_mask |= mask;
159 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000160 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800161 }
162}
163
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300164/**
165 * ilk_update_gt_irq - update GTIMR
166 * @dev_priv: driver private
167 * @interrupt_mask: mask of interrupt bits to update
168 * @enabled_irq_mask: mask of interrupt bits to enable
169 */
170static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
171 uint32_t interrupt_mask,
172 uint32_t enabled_irq_mask)
173{
174 assert_spin_locked(&dev_priv->irq_lock);
175
Paulo Zanoni730488b2014-03-07 20:12:32 -0300176 if (WARN_ON(dev_priv->pm.irqs_disabled))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300177 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300178
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300179 dev_priv->gt_irq_mask &= ~interrupt_mask;
180 dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
181 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
182 POSTING_READ(GTIMR);
183}
184
185void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
186{
187 ilk_update_gt_irq(dev_priv, mask, mask);
188}
189
190void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
191{
192 ilk_update_gt_irq(dev_priv, mask, 0);
193}
194
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300195/**
196 * snb_update_pm_irq - update GEN6_PMIMR
197 * @dev_priv: driver private
198 * @interrupt_mask: mask of interrupt bits to update
199 * @enabled_irq_mask: mask of interrupt bits to enable
200 */
201static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
202 uint32_t interrupt_mask,
203 uint32_t enabled_irq_mask)
204{
Paulo Zanoni605cd252013-08-06 18:57:15 -0300205 uint32_t new_val;
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300206
207 assert_spin_locked(&dev_priv->irq_lock);
208
Paulo Zanoni730488b2014-03-07 20:12:32 -0300209 if (WARN_ON(dev_priv->pm.irqs_disabled))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300210 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300211
Paulo Zanoni605cd252013-08-06 18:57:15 -0300212 new_val = dev_priv->pm_irq_mask;
Paulo Zanonif52ecbc2013-08-06 18:57:14 -0300213 new_val &= ~interrupt_mask;
214 new_val |= (~enabled_irq_mask & interrupt_mask);
215
Paulo Zanoni605cd252013-08-06 18:57:15 -0300216 if (new_val != dev_priv->pm_irq_mask) {
217 dev_priv->pm_irq_mask = new_val;
218 I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
Paulo Zanonif52ecbc2013-08-06 18:57:14 -0300219 POSTING_READ(GEN6_PMIMR);
220 }
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300221}
222
223void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
224{
225 snb_update_pm_irq(dev_priv, mask, mask);
226}
227
228void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
229{
230 snb_update_pm_irq(dev_priv, mask, 0);
231}
232
Paulo Zanoni86642812013-04-12 17:57:57 -0300233static bool ivb_can_enable_err_int(struct drm_device *dev)
234{
235 struct drm_i915_private *dev_priv = dev->dev_private;
236 struct intel_crtc *crtc;
237 enum pipe pipe;
238
Daniel Vetter4bc9d432013-06-27 13:44:58 +0200239 assert_spin_locked(&dev_priv->irq_lock);
240
Paulo Zanoni86642812013-04-12 17:57:57 -0300241 for_each_pipe(pipe) {
242 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
243
244 if (crtc->cpu_fifo_underrun_disabled)
245 return false;
246 }
247
248 return true;
249}
250
Ben Widawsky09610212014-05-15 20:58:08 +0300251/**
252 * bdw_update_pm_irq - update GT interrupt 2
253 * @dev_priv: driver private
254 * @interrupt_mask: mask of interrupt bits to update
255 * @enabled_irq_mask: mask of interrupt bits to enable
256 *
257 * Copied from the snb function, updated with relevant register offsets
258 */
259static void bdw_update_pm_irq(struct drm_i915_private *dev_priv,
260 uint32_t interrupt_mask,
261 uint32_t enabled_irq_mask)
262{
263 uint32_t new_val;
264
265 assert_spin_locked(&dev_priv->irq_lock);
266
267 if (WARN_ON(dev_priv->pm.irqs_disabled))
268 return;
269
270 new_val = dev_priv->pm_irq_mask;
271 new_val &= ~interrupt_mask;
272 new_val |= (~enabled_irq_mask & interrupt_mask);
273
274 if (new_val != dev_priv->pm_irq_mask) {
275 dev_priv->pm_irq_mask = new_val;
276 I915_WRITE(GEN8_GT_IMR(2), dev_priv->pm_irq_mask);
277 POSTING_READ(GEN8_GT_IMR(2));
278 }
279}
280
281void bdw_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
282{
283 bdw_update_pm_irq(dev_priv, mask, mask);
284}
285
286void bdw_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
287{
288 bdw_update_pm_irq(dev_priv, mask, 0);
289}
290
Paulo Zanoni86642812013-04-12 17:57:57 -0300291static bool cpt_can_enable_serr_int(struct drm_device *dev)
292{
293 struct drm_i915_private *dev_priv = dev->dev_private;
294 enum pipe pipe;
295 struct intel_crtc *crtc;
296
Daniel Vetterfee884e2013-07-04 23:35:21 +0200297 assert_spin_locked(&dev_priv->irq_lock);
298
Paulo Zanoni86642812013-04-12 17:57:57 -0300299 for_each_pipe(pipe) {
300 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
301
302 if (crtc->pch_fifo_underrun_disabled)
303 return false;
304 }
305
306 return true;
307}
308
Ville Syrjäläe69abff2014-05-16 19:40:21 +0300309static void i9xx_set_fifo_underrun_reporting(struct drm_device *dev,
310 enum pipe pipe, bool enable)
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +0200311{
312 struct drm_i915_private *dev_priv = dev->dev_private;
313 u32 reg = PIPESTAT(pipe);
Ville Syrjäläe69abff2014-05-16 19:40:21 +0300314 u32 pipestat = I915_READ(reg) & 0xffff0000;
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +0200315
316 assert_spin_locked(&dev_priv->irq_lock);
317
Ville Syrjäläe69abff2014-05-16 19:40:21 +0300318 if (enable) {
319 I915_WRITE(reg, pipestat | PIPE_FIFO_UNDERRUN_STATUS);
320 POSTING_READ(reg);
321 } else {
322 if (pipestat & PIPE_FIFO_UNDERRUN_STATUS)
323 DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
324 }
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +0200325}
326
Paulo Zanoni86642812013-04-12 17:57:57 -0300327static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
328 enum pipe pipe, bool enable)
329{
330 struct drm_i915_private *dev_priv = dev->dev_private;
331 uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
332 DE_PIPEB_FIFO_UNDERRUN;
333
334 if (enable)
335 ironlake_enable_display_irq(dev_priv, bit);
336 else
337 ironlake_disable_display_irq(dev_priv, bit);
338}
339
340static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
Daniel Vetter7336df62013-07-09 22:59:16 +0200341 enum pipe pipe, bool enable)
Paulo Zanoni86642812013-04-12 17:57:57 -0300342{
343 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni86642812013-04-12 17:57:57 -0300344 if (enable) {
Daniel Vetter7336df62013-07-09 22:59:16 +0200345 I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
346
Paulo Zanoni86642812013-04-12 17:57:57 -0300347 if (!ivb_can_enable_err_int(dev))
348 return;
349
Paulo Zanoni86642812013-04-12 17:57:57 -0300350 ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
351 } else {
Daniel Vetter7336df62013-07-09 22:59:16 +0200352 bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
353
354 /* Change the state _after_ we've read out the current one. */
Paulo Zanoni86642812013-04-12 17:57:57 -0300355 ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
Daniel Vetter7336df62013-07-09 22:59:16 +0200356
357 if (!was_enabled &&
358 (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
Ville Syrjälä823c6902014-05-16 19:40:23 +0300359 DRM_ERROR("uncleared fifo underrun on pipe %c\n",
360 pipe_name(pipe));
Daniel Vetter7336df62013-07-09 22:59:16 +0200361 }
Paulo Zanoni86642812013-04-12 17:57:57 -0300362 }
363}
364
Daniel Vetter38d83c962013-11-07 11:05:46 +0100365static void broadwell_set_fifo_underrun_reporting(struct drm_device *dev,
366 enum pipe pipe, bool enable)
367{
368 struct drm_i915_private *dev_priv = dev->dev_private;
369
370 assert_spin_locked(&dev_priv->irq_lock);
371
372 if (enable)
373 dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_FIFO_UNDERRUN;
374 else
375 dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_FIFO_UNDERRUN;
376 I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
377 POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
378}
379
Daniel Vetterfee884e2013-07-04 23:35:21 +0200380/**
381 * ibx_display_interrupt_update - update SDEIMR
382 * @dev_priv: driver private
383 * @interrupt_mask: mask of interrupt bits to update
384 * @enabled_irq_mask: mask of interrupt bits to enable
385 */
386static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
387 uint32_t interrupt_mask,
388 uint32_t enabled_irq_mask)
389{
390 uint32_t sdeimr = I915_READ(SDEIMR);
391 sdeimr &= ~interrupt_mask;
392 sdeimr |= (~enabled_irq_mask & interrupt_mask);
393
394 assert_spin_locked(&dev_priv->irq_lock);
395
Paulo Zanoni730488b2014-03-07 20:12:32 -0300396 if (WARN_ON(dev_priv->pm.irqs_disabled))
Paulo Zanonic67a4702013-08-19 13:18:09 -0300397 return;
Paulo Zanonic67a4702013-08-19 13:18:09 -0300398
Daniel Vetterfee884e2013-07-04 23:35:21 +0200399 I915_WRITE(SDEIMR, sdeimr);
400 POSTING_READ(SDEIMR);
401}
402#define ibx_enable_display_interrupt(dev_priv, bits) \
403 ibx_display_interrupt_update((dev_priv), (bits), (bits))
404#define ibx_disable_display_interrupt(dev_priv, bits) \
405 ibx_display_interrupt_update((dev_priv), (bits), 0)
406
Daniel Vetterde280752013-07-04 23:35:24 +0200407static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
408 enum transcoder pch_transcoder,
Paulo Zanoni86642812013-04-12 17:57:57 -0300409 bool enable)
410{
Paulo Zanoni86642812013-04-12 17:57:57 -0300411 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterde280752013-07-04 23:35:24 +0200412 uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
413 SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
Paulo Zanoni86642812013-04-12 17:57:57 -0300414
415 if (enable)
Daniel Vetterfee884e2013-07-04 23:35:21 +0200416 ibx_enable_display_interrupt(dev_priv, bit);
Paulo Zanoni86642812013-04-12 17:57:57 -0300417 else
Daniel Vetterfee884e2013-07-04 23:35:21 +0200418 ibx_disable_display_interrupt(dev_priv, bit);
Paulo Zanoni86642812013-04-12 17:57:57 -0300419}
420
421static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
422 enum transcoder pch_transcoder,
423 bool enable)
424{
425 struct drm_i915_private *dev_priv = dev->dev_private;
426
427 if (enable) {
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200428 I915_WRITE(SERR_INT,
429 SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
430
Paulo Zanoni86642812013-04-12 17:57:57 -0300431 if (!cpt_can_enable_serr_int(dev))
432 return;
433
Daniel Vetterfee884e2013-07-04 23:35:21 +0200434 ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
Paulo Zanoni86642812013-04-12 17:57:57 -0300435 } else {
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200436 uint32_t tmp = I915_READ(SERR_INT);
437 bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
438
439 /* Change the state _after_ we've read out the current one. */
Daniel Vetterfee884e2013-07-04 23:35:21 +0200440 ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200441
442 if (!was_enabled &&
443 (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
Ville Syrjälä823c6902014-05-16 19:40:23 +0300444 DRM_ERROR("uncleared pch fifo underrun on pch transcoder %c\n",
445 transcoder_name(pch_transcoder));
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200446 }
Paulo Zanoni86642812013-04-12 17:57:57 -0300447 }
Paulo Zanoni86642812013-04-12 17:57:57 -0300448}
449
450/**
451 * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
452 * @dev: drm device
453 * @pipe: pipe
454 * @enable: true if we want to report FIFO underrun errors, false otherwise
455 *
456 * This function makes us disable or enable CPU fifo underruns for a specific
457 * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
458 * reporting for one pipe may also disable all the other CPU error interruts for
459 * the other pipes, due to the fact that there's just one interrupt mask/enable
460 * bit for all the pipes.
461 *
462 * Returns the previous state of underrun reporting.
463 */
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +0200464static bool __intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
465 enum pipe pipe, bool enable)
Paulo Zanoni86642812013-04-12 17:57:57 -0300466{
467 struct drm_i915_private *dev_priv = dev->dev_private;
468 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni86642812013-04-12 17:57:57 -0300470 bool ret;
471
Imre Deak77961eb2014-03-05 16:20:56 +0200472 assert_spin_locked(&dev_priv->irq_lock);
473
Paulo Zanoni86642812013-04-12 17:57:57 -0300474 ret = !intel_crtc->cpu_fifo_underrun_disabled;
475
476 if (enable == ret)
477 goto done;
478
479 intel_crtc->cpu_fifo_underrun_disabled = !enable;
480
Ville Syrjäläe69abff2014-05-16 19:40:21 +0300481 if (INTEL_INFO(dev)->gen < 5 || IS_VALLEYVIEW(dev))
482 i9xx_set_fifo_underrun_reporting(dev, pipe, enable);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +0200483 else if (IS_GEN5(dev) || IS_GEN6(dev))
Paulo Zanoni86642812013-04-12 17:57:57 -0300484 ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
485 else if (IS_GEN7(dev))
Daniel Vetter7336df62013-07-09 22:59:16 +0200486 ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
Daniel Vetter38d83c962013-11-07 11:05:46 +0100487 else if (IS_GEN8(dev))
488 broadwell_set_fifo_underrun_reporting(dev, pipe, enable);
Paulo Zanoni86642812013-04-12 17:57:57 -0300489
490done:
Imre Deakf88d42f2014-03-04 19:23:09 +0200491 return ret;
492}
493
494bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
495 enum pipe pipe, bool enable)
496{
497 struct drm_i915_private *dev_priv = dev->dev_private;
498 unsigned long flags;
499 bool ret;
500
501 spin_lock_irqsave(&dev_priv->irq_lock, flags);
502 ret = __intel_set_cpu_fifo_underrun_reporting(dev, pipe, enable);
Paulo Zanoni86642812013-04-12 17:57:57 -0300503 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Imre Deakf88d42f2014-03-04 19:23:09 +0200504
Paulo Zanoni86642812013-04-12 17:57:57 -0300505 return ret;
506}
507
Imre Deak91d181d2014-02-10 18:42:49 +0200508static bool __cpu_fifo_underrun_reporting_enabled(struct drm_device *dev,
509 enum pipe pipe)
510{
511 struct drm_i915_private *dev_priv = dev->dev_private;
512 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
514
515 return !intel_crtc->cpu_fifo_underrun_disabled;
516}
517
Paulo Zanoni86642812013-04-12 17:57:57 -0300518/**
519 * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
520 * @dev: drm device
521 * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
522 * @enable: true if we want to report FIFO underrun errors, false otherwise
523 *
524 * This function makes us disable or enable PCH fifo underruns for a specific
525 * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
526 * underrun reporting for one transcoder may also disable all the other PCH
527 * error interruts for the other transcoders, due to the fact that there's just
528 * one interrupt mask/enable bit for all the transcoders.
529 *
530 * Returns the previous state of underrun reporting.
531 */
532bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
533 enum transcoder pch_transcoder,
534 bool enable)
535{
536 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterde280752013-07-04 23:35:24 +0200537 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
538 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni86642812013-04-12 17:57:57 -0300539 unsigned long flags;
540 bool ret;
541
Daniel Vetterde280752013-07-04 23:35:24 +0200542 /*
543 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
544 * has only one pch transcoder A that all pipes can use. To avoid racy
545 * pch transcoder -> pipe lookups from interrupt code simply store the
546 * underrun statistics in crtc A. Since we never expose this anywhere
547 * nor use it outside of the fifo underrun code here using the "wrong"
548 * crtc on LPT won't cause issues.
549 */
Paulo Zanoni86642812013-04-12 17:57:57 -0300550
551 spin_lock_irqsave(&dev_priv->irq_lock, flags);
552
553 ret = !intel_crtc->pch_fifo_underrun_disabled;
554
555 if (enable == ret)
556 goto done;
557
558 intel_crtc->pch_fifo_underrun_disabled = !enable;
559
560 if (HAS_PCH_IBX(dev))
Daniel Vetterde280752013-07-04 23:35:24 +0200561 ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
Paulo Zanoni86642812013-04-12 17:57:57 -0300562 else
563 cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
564
565done:
566 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
567 return ret;
568}
569
570
Daniel Vetterb5ea6422014-03-02 21:18:00 +0100571static void
Imre Deak755e9012014-02-10 18:42:47 +0200572__i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
573 u32 enable_mask, u32 status_mask)
Keith Packard7c463582008-11-04 02:03:27 -0800574{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200575 u32 reg = PIPESTAT(pipe);
Imre Deak755e9012014-02-10 18:42:47 +0200576 u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
Keith Packard7c463582008-11-04 02:03:27 -0800577
Daniel Vetterb79480b2013-06-27 17:52:10 +0200578 assert_spin_locked(&dev_priv->irq_lock);
579
Ville Syrjälä04feced2014-04-03 13:28:33 +0300580 if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
581 status_mask & ~PIPESTAT_INT_STATUS_MASK,
582 "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
583 pipe_name(pipe), enable_mask, status_mask))
Imre Deak755e9012014-02-10 18:42:47 +0200584 return;
585
586 if ((pipestat & enable_mask) == enable_mask)
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200587 return;
588
Imre Deak91d181d2014-02-10 18:42:49 +0200589 dev_priv->pipestat_irq_mask[pipe] |= status_mask;
590
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200591 /* Enable the interrupt, clear any pending status */
Imre Deak755e9012014-02-10 18:42:47 +0200592 pipestat |= enable_mask | status_mask;
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200593 I915_WRITE(reg, pipestat);
594 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800595}
596
Daniel Vetterb5ea6422014-03-02 21:18:00 +0100597static void
Imre Deak755e9012014-02-10 18:42:47 +0200598__i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
599 u32 enable_mask, u32 status_mask)
Keith Packard7c463582008-11-04 02:03:27 -0800600{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200601 u32 reg = PIPESTAT(pipe);
Imre Deak755e9012014-02-10 18:42:47 +0200602 u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
Keith Packard7c463582008-11-04 02:03:27 -0800603
Daniel Vetterb79480b2013-06-27 17:52:10 +0200604 assert_spin_locked(&dev_priv->irq_lock);
605
Ville Syrjälä04feced2014-04-03 13:28:33 +0300606 if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
607 status_mask & ~PIPESTAT_INT_STATUS_MASK,
608 "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
609 pipe_name(pipe), enable_mask, status_mask))
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200610 return;
611
Imre Deak755e9012014-02-10 18:42:47 +0200612 if ((pipestat & enable_mask) == 0)
613 return;
614
Imre Deak91d181d2014-02-10 18:42:49 +0200615 dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
616
Imre Deak755e9012014-02-10 18:42:47 +0200617 pipestat &= ~enable_mask;
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200618 I915_WRITE(reg, pipestat);
619 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800620}
621
Imre Deak10c59c52014-02-10 18:42:48 +0200622static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
623{
624 u32 enable_mask = status_mask << 16;
625
626 /*
Ville Syrjälä724a6902014-04-09 13:28:48 +0300627 * On pipe A we don't support the PSR interrupt yet,
628 * on pipe B and C the same bit MBZ.
Imre Deak10c59c52014-02-10 18:42:48 +0200629 */
630 if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
631 return 0;
Ville Syrjälä724a6902014-04-09 13:28:48 +0300632 /*
633 * On pipe B and C we don't support the PSR interrupt yet, on pipe
634 * A the same bit is for perf counters which we don't use either.
635 */
636 if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
637 return 0;
Imre Deak10c59c52014-02-10 18:42:48 +0200638
639 enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
640 SPRITE0_FLIP_DONE_INT_EN_VLV |
641 SPRITE1_FLIP_DONE_INT_EN_VLV);
642 if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
643 enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
644 if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
645 enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
646
647 return enable_mask;
648}
649
Imre Deak755e9012014-02-10 18:42:47 +0200650void
651i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
652 u32 status_mask)
653{
654 u32 enable_mask;
655
Imre Deak10c59c52014-02-10 18:42:48 +0200656 if (IS_VALLEYVIEW(dev_priv->dev))
657 enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
658 status_mask);
659 else
660 enable_mask = status_mask << 16;
Imre Deak755e9012014-02-10 18:42:47 +0200661 __i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
662}
663
664void
665i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
666 u32 status_mask)
667{
668 u32 enable_mask;
669
Imre Deak10c59c52014-02-10 18:42:48 +0200670 if (IS_VALLEYVIEW(dev_priv->dev))
671 enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
672 status_mask);
673 else
674 enable_mask = status_mask << 16;
Imre Deak755e9012014-02-10 18:42:47 +0200675 __i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
676}
677
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000678/**
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300679 * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
Zhao Yakui01c66882009-10-28 05:10:00 +0000680 */
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300681static void i915_enable_asle_pipestat(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +0000682{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300683 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000684 unsigned long irqflags;
685
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300686 if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
687 return;
688
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000689 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000690
Imre Deak755e9012014-02-10 18:42:47 +0200691 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
Jani Nikulaf8987802013-04-29 13:02:53 +0300692 if (INTEL_INFO(dev)->gen >= 4)
Daniel Vetter3b6c42e2013-10-21 18:04:35 +0200693 i915_enable_pipestat(dev_priv, PIPE_A,
Imre Deak755e9012014-02-10 18:42:47 +0200694 PIPE_LEGACY_BLC_EVENT_STATUS);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000695
696 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000697}
698
699/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700700 * i915_pipe_enabled - check if a pipe is enabled
701 * @dev: DRM device
702 * @pipe: pipe to check
703 *
704 * Reading certain registers when the pipe is disabled can hang the chip.
705 * Use this routine to make sure the PLL is running and the pipe is active
706 * before reading such registers if unsure.
707 */
708static int
709i915_pipe_enabled(struct drm_device *dev, int pipe)
710{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300711 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200712
Daniel Vettera01025a2013-05-22 00:50:23 +0200713 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
714 /* Locking is horribly broken here, but whatever. */
715 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
716 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni71f8ba62013-05-03 12:15:39 -0300717
Daniel Vettera01025a2013-05-22 00:50:23 +0200718 return intel_crtc->active;
719 } else {
720 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
721 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700722}
723
Ville Syrjälä4cdb83e2013-10-11 21:52:44 +0300724static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
725{
726 /* Gen2 doesn't have a hardware frame counter */
727 return 0;
728}
729
Keith Packard42f52ef2008-10-18 19:39:29 -0700730/* Called from drm generic code, passed a 'crtc', which
731 * we use as a pipe index
732 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700733static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700734{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300735 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700736 unsigned long high_frame;
737 unsigned long low_frame;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300738 u32 high1, high2, low, pixel, vbl_start;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700739
740 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800741 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800742 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700743 return 0;
744 }
745
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300746 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
747 struct intel_crtc *intel_crtc =
748 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
749 const struct drm_display_mode *mode =
750 &intel_crtc->config.adjusted_mode;
751
752 vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
753 } else {
Daniel Vettera2d213d2014-02-07 16:34:05 +0100754 enum transcoder cpu_transcoder = (enum transcoder) pipe;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300755 u32 htotal;
756
757 htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
758 vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
759
760 vbl_start *= htotal;
761 }
762
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800763 high_frame = PIPEFRAME(pipe);
764 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100765
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700766 /*
767 * High & low register fields aren't synchronized, so make sure
768 * we get a low value that's stable across two reads of the high
769 * register.
770 */
771 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100772 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300773 low = I915_READ(low_frame);
Chris Wilson5eddb702010-09-11 13:48:45 +0100774 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700775 } while (high1 != high2);
776
Chris Wilson5eddb702010-09-11 13:48:45 +0100777 high1 >>= PIPE_FRAME_HIGH_SHIFT;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300778 pixel = low & PIPE_PIXEL_MASK;
Chris Wilson5eddb702010-09-11 13:48:45 +0100779 low >>= PIPE_FRAME_LOW_SHIFT;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300780
781 /*
782 * The frame counter increments at beginning of active.
783 * Cook up a vblank counter by also checking the pixel
784 * counter against vblank start.
785 */
Ville Syrjäläedc08d02013-11-06 13:56:27 -0200786 return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700787}
788
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700789static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800790{
Jani Nikula2d1013d2014-03-31 14:27:17 +0300791 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800792 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800793
794 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800795 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800796 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800797 return 0;
798 }
799
800 return I915_READ(reg);
801}
802
Mario Kleinerad3543e2013-10-30 05:13:08 +0100803/* raw reads, only for fast reads of display block, no need for forcewake etc. */
804#define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
Mario Kleinerad3543e2013-10-30 05:13:08 +0100805
Ville Syrjäläa225f072014-04-29 13:35:45 +0300806static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
807{
808 struct drm_device *dev = crtc->base.dev;
809 struct drm_i915_private *dev_priv = dev->dev_private;
810 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
811 enum pipe pipe = crtc->pipe;
812 int vtotal = mode->crtc_vtotal;
813 int position;
814
815 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
816 vtotal /= 2;
817
818 if (IS_GEN2(dev))
819 position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
820 else
821 position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
822
823 /*
824 * Scanline counter increments at leading edge of hsync, and
825 * it starts counting from vtotal-1 on the first active line.
826 * That means the scanline counter value is always one less
827 * than what we would expect. Ie. just after start of vblank,
828 * which also occurs at start of hsync (on the last active line),
829 * the scanline counter will read vblank_start-1.
830 */
831 return (position + 1) % vtotal;
832}
833
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700834static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200835 unsigned int flags, int *vpos, int *hpos,
836 ktime_t *stime, ktime_t *etime)
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100837{
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300838 struct drm_i915_private *dev_priv = dev->dev_private;
839 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
841 const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300842 int position;
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300843 int vbl_start, vbl_end, hsync_start, htotal, vtotal;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100844 bool in_vbl = true;
845 int ret = 0;
Mario Kleinerad3543e2013-10-30 05:13:08 +0100846 unsigned long irqflags;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100847
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300848 if (!intel_crtc->active) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100849 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800850 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100851 return 0;
852 }
853
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300854 htotal = mode->crtc_htotal;
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300855 hsync_start = mode->crtc_hsync_start;
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300856 vtotal = mode->crtc_vtotal;
857 vbl_start = mode->crtc_vblank_start;
858 vbl_end = mode->crtc_vblank_end;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100859
Ville Syrjäläd31faf62013-10-28 16:31:41 +0200860 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
861 vbl_start = DIV_ROUND_UP(vbl_start, 2);
862 vbl_end /= 2;
863 vtotal /= 2;
864 }
865
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300866 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
867
Mario Kleinerad3543e2013-10-30 05:13:08 +0100868 /*
869 * Lock uncore.lock, as we will do multiple timing critical raw
870 * register reads, potentially with preemption disabled, so the
871 * following code must not block on uncore.lock.
872 */
873 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300874
Mario Kleinerad3543e2013-10-30 05:13:08 +0100875 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
876
877 /* Get optional system timestamp before query. */
878 if (stime)
879 *stime = ktime_get();
880
Ville Syrjälä7c06b082013-10-11 21:52:43 +0300881 if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100882 /* No obvious pixelcount register. Only query vertical
883 * scanout position from Display scan line register.
884 */
Ville Syrjäläa225f072014-04-29 13:35:45 +0300885 position = __intel_get_crtc_scanline(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100886 } else {
887 /* Have access to pixelcount since start of frame.
888 * We can split this into vertical and horizontal
889 * scanout position.
890 */
Mario Kleinerad3543e2013-10-30 05:13:08 +0100891 position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100892
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300893 /* convert to pixel counts */
894 vbl_start *= htotal;
895 vbl_end *= htotal;
896 vtotal *= htotal;
Ville Syrjälä78e8fc62014-04-29 13:35:44 +0300897
898 /*
899 * Start of vblank interrupt is triggered at start of hsync,
900 * just prior to the first active line of vblank. However we
901 * consider lines to start at the leading edge of horizontal
902 * active. So, should we get here before we've crossed into
903 * the horizontal active of the first line in vblank, we would
904 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
905 * always add htotal-hsync_start to the current pixel position.
906 */
907 position = (position + htotal - hsync_start) % vtotal;
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300908 }
909
Mario Kleinerad3543e2013-10-30 05:13:08 +0100910 /* Get optional system timestamp after query. */
911 if (etime)
912 *etime = ktime_get();
913
914 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
915
916 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
917
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300918 in_vbl = position >= vbl_start && position < vbl_end;
919
920 /*
921 * While in vblank, position will be negative
922 * counting up towards 0 at vbl_end. And outside
923 * vblank, position will be positive counting
924 * up since vbl_end.
925 */
926 if (position >= vbl_start)
927 position -= vbl_end;
928 else
929 position += vtotal - vbl_end;
930
Ville Syrjälä7c06b082013-10-11 21:52:43 +0300931 if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300932 *vpos = position;
933 *hpos = 0;
934 } else {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100935 *vpos = position / htotal;
936 *hpos = position - (*vpos * htotal);
937 }
938
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100939 /* In vblank? */
940 if (in_vbl)
941 ret |= DRM_SCANOUTPOS_INVBL;
942
943 return ret;
944}
945
Ville Syrjäläa225f072014-04-29 13:35:45 +0300946int intel_get_crtc_scanline(struct intel_crtc *crtc)
947{
948 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
949 unsigned long irqflags;
950 int position;
951
952 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
953 position = __intel_get_crtc_scanline(crtc);
954 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
955
956 return position;
957}
958
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700959static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100960 int *max_error,
961 struct timeval *vblank_time,
962 unsigned flags)
963{
Chris Wilson4041b852011-01-22 10:07:56 +0000964 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100965
Ben Widawsky7eb552a2013-03-13 14:05:41 -0700966 if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
Chris Wilson4041b852011-01-22 10:07:56 +0000967 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100968 return -EINVAL;
969 }
970
971 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000972 crtc = intel_get_crtc_for_pipe(dev, pipe);
973 if (crtc == NULL) {
974 DRM_ERROR("Invalid crtc %d\n", pipe);
975 return -EINVAL;
976 }
977
978 if (!crtc->enabled) {
979 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
980 return -EBUSY;
981 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100982
983 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000984 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
985 vblank_time, flags,
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300986 crtc,
987 &to_intel_crtc(crtc)->config.adjusted_mode);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100988}
989
Jani Nikula67c347f2013-09-17 14:26:34 +0300990static bool intel_hpd_irq_event(struct drm_device *dev,
991 struct drm_connector *connector)
Egbert Eich321a1b32013-04-11 16:00:26 +0200992{
993 enum drm_connector_status old_status;
994
995 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
996 old_status = connector->status;
997
998 connector->status = connector->funcs->detect(connector, false);
Jani Nikula67c347f2013-09-17 14:26:34 +0300999 if (old_status == connector->status)
1000 return false;
1001
1002 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
Egbert Eich321a1b32013-04-11 16:00:26 +02001003 connector->base.id,
1004 drm_get_connector_name(connector),
Jani Nikula67c347f2013-09-17 14:26:34 +03001005 drm_get_connector_status_name(old_status),
1006 drm_get_connector_status_name(connector->status));
1007
1008 return true;
Egbert Eich321a1b32013-04-11 16:00:26 +02001009}
1010
Jesse Barnes5ca58282009-03-31 14:11:15 -07001011/*
1012 * Handle hotplug events outside the interrupt handler proper.
1013 */
Egbert Eichac4c16c2013-04-16 13:36:58 +02001014#define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
1015
Jesse Barnes5ca58282009-03-31 14:11:15 -07001016static void i915_hotplug_work_func(struct work_struct *work)
1017{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001018 struct drm_i915_private *dev_priv =
1019 container_of(work, struct drm_i915_private, hotplug_work);
Jesse Barnes5ca58282009-03-31 14:11:15 -07001020 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -07001021 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +02001022 struct intel_connector *intel_connector;
1023 struct intel_encoder *intel_encoder;
1024 struct drm_connector *connector;
1025 unsigned long irqflags;
1026 bool hpd_disabled = false;
Egbert Eich321a1b32013-04-11 16:00:26 +02001027 bool changed = false;
Egbert Eich142e2392013-04-11 15:57:57 +02001028 u32 hpd_event_bits;
Jesse Barnes5ca58282009-03-31 14:11:15 -07001029
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001030 /* HPD irq before everything is fully set up. */
1031 if (!dev_priv->enable_hotplug_processing)
1032 return;
1033
Keith Packarda65e34c2011-07-25 10:04:56 -07001034 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -08001035 DRM_DEBUG_KMS("running encoder hotplug functions\n");
1036
Egbert Eichcd569ae2013-04-16 13:36:57 +02001037 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Egbert Eich142e2392013-04-11 15:57:57 +02001038
1039 hpd_event_bits = dev_priv->hpd_event_bits;
1040 dev_priv->hpd_event_bits = 0;
Egbert Eichcd569ae2013-04-16 13:36:57 +02001041 list_for_each_entry(connector, &mode_config->connector_list, head) {
1042 intel_connector = to_intel_connector(connector);
1043 intel_encoder = intel_connector->encoder;
1044 if (intel_encoder->hpd_pin > HPD_NONE &&
1045 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
1046 connector->polled == DRM_CONNECTOR_POLL_HPD) {
1047 DRM_INFO("HPD interrupt storm detected on connector %s: "
1048 "switching from hotplug detection to polling\n",
1049 drm_get_connector_name(connector));
1050 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
1051 connector->polled = DRM_CONNECTOR_POLL_CONNECT
1052 | DRM_CONNECTOR_POLL_DISCONNECT;
1053 hpd_disabled = true;
1054 }
Egbert Eich142e2392013-04-11 15:57:57 +02001055 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1056 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
1057 drm_get_connector_name(connector), intel_encoder->hpd_pin);
1058 }
Egbert Eichcd569ae2013-04-16 13:36:57 +02001059 }
1060 /* if there were no outputs to poll, poll was disabled,
1061 * therefore make sure it's enabled when disabling HPD on
1062 * some connectors */
Egbert Eichac4c16c2013-04-16 13:36:58 +02001063 if (hpd_disabled) {
Egbert Eichcd569ae2013-04-16 13:36:57 +02001064 drm_kms_helper_poll_enable(dev);
Egbert Eichac4c16c2013-04-16 13:36:58 +02001065 mod_timer(&dev_priv->hotplug_reenable_timer,
1066 jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
1067 }
Egbert Eichcd569ae2013-04-16 13:36:57 +02001068
1069 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1070
Egbert Eich321a1b32013-04-11 16:00:26 +02001071 list_for_each_entry(connector, &mode_config->connector_list, head) {
1072 intel_connector = to_intel_connector(connector);
1073 intel_encoder = intel_connector->encoder;
1074 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1075 if (intel_encoder->hot_plug)
1076 intel_encoder->hot_plug(intel_encoder);
1077 if (intel_hpd_irq_event(dev, connector))
1078 changed = true;
1079 }
1080 }
Keith Packard40ee3382011-07-28 15:31:19 -07001081 mutex_unlock(&mode_config->mutex);
1082
Egbert Eich321a1b32013-04-11 16:00:26 +02001083 if (changed)
1084 drm_kms_helper_hotplug_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -07001085}
1086
Ville Syrjälä3ca1cce2014-01-17 13:43:51 +02001087static void intel_hpd_irq_uninstall(struct drm_i915_private *dev_priv)
1088{
1089 del_timer_sync(&dev_priv->hotplug_reenable_timer);
1090}
1091
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001092static void ironlake_rps_change_irq_handler(struct drm_device *dev)
Jesse Barnesf97108d2010-01-29 11:27:07 -08001093{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001094 struct drm_i915_private *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +00001095 u32 busy_up, busy_down, max_avg, min_avg;
Daniel Vetter92703882012-08-09 16:46:01 +02001096 u8 new_delay;
Daniel Vetter92703882012-08-09 16:46:01 +02001097
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001098 spin_lock(&mchdev_lock);
Jesse Barnesf97108d2010-01-29 11:27:07 -08001099
Daniel Vetter73edd18f2012-08-08 23:35:37 +02001100 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
1101
Daniel Vetter20e4d402012-08-08 23:35:39 +02001102 new_delay = dev_priv->ips.cur_delay;
Daniel Vetter92703882012-08-09 16:46:01 +02001103
Jesse Barnes7648fa92010-05-20 14:28:11 -07001104 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +00001105 busy_up = I915_READ(RCPREVBSYTUPAVG);
1106 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -08001107 max_avg = I915_READ(RCBMAXAVG);
1108 min_avg = I915_READ(RCBMINAVG);
1109
1110 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +00001111 if (busy_up > max_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02001112 if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
1113 new_delay = dev_priv->ips.cur_delay - 1;
1114 if (new_delay < dev_priv->ips.max_delay)
1115 new_delay = dev_priv->ips.max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +00001116 } else if (busy_down < min_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02001117 if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
1118 new_delay = dev_priv->ips.cur_delay + 1;
1119 if (new_delay > dev_priv->ips.min_delay)
1120 new_delay = dev_priv->ips.min_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -08001121 }
1122
Jesse Barnes7648fa92010-05-20 14:28:11 -07001123 if (ironlake_set_drps(dev, new_delay))
Daniel Vetter20e4d402012-08-08 23:35:39 +02001124 dev_priv->ips.cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -08001125
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001126 spin_unlock(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02001127
Jesse Barnesf97108d2010-01-29 11:27:07 -08001128 return;
1129}
1130
Chris Wilson549f7362010-10-19 11:19:32 +01001131static void notify_ring(struct drm_device *dev,
1132 struct intel_ring_buffer *ring)
1133{
Chris Wilson475553d2011-01-20 09:52:56 +00001134 if (ring->obj == NULL)
1135 return;
1136
Chris Wilson814e9b52013-09-23 17:33:19 -03001137 trace_i915_gem_request_complete(ring);
Chris Wilson9862e602011-01-04 22:22:17 +00001138
Chris Wilson549f7362010-10-19 11:19:32 +01001139 wake_up_all(&ring->irq_queue);
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03001140 i915_queue_hangcheck(dev);
Chris Wilson549f7362010-10-19 11:19:32 +01001141}
1142
Ben Widawsky4912d042011-04-25 11:25:20 -07001143static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001144{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001145 struct drm_i915_private *dev_priv =
1146 container_of(work, struct drm_i915_private, rps.work);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03001147 u32 pm_iir;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001148 int new_delay, adj;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001149
Daniel Vetter59cdb632013-07-04 23:35:28 +02001150 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02001151 pm_iir = dev_priv->rps.pm_iir;
1152 dev_priv->rps.pm_iir = 0;
Ben Widawsky09610212014-05-15 20:58:08 +03001153 if (IS_BROADWELL(dev_priv->dev))
1154 bdw_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
1155 else {
1156 /* Make sure not to corrupt PMIMR state used by ringbuffer */
1157 snb_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
1158 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001159 spin_unlock_irq(&dev_priv->irq_lock);
Ben Widawsky4912d042011-04-25 11:25:20 -07001160
Paulo Zanoni60611c12013-08-15 11:50:01 -03001161 /* Make sure we didn't queue anything we're not going to process. */
Deepak Sa6706b42014-03-15 20:23:22 +05301162 WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
Paulo Zanoni60611c12013-08-15 11:50:01 -03001163
Deepak Sa6706b42014-03-15 20:23:22 +05301164 if ((pm_iir & dev_priv->pm_rps_events) == 0)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001165 return;
1166
Jesse Barnes4fc688c2012-11-02 11:14:01 -07001167 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01001168
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001169 adj = dev_priv->rps.last_adj;
Ville Syrjälä74250342013-06-25 21:38:11 +03001170 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001171 if (adj > 0)
1172 adj *= 2;
1173 else
1174 adj = 1;
Ben Widawskyb39fb292014-03-19 18:31:11 -07001175 new_delay = dev_priv->rps.cur_freq + adj;
Ville Syrjälä74250342013-06-25 21:38:11 +03001176
1177 /*
1178 * For better performance, jump directly
1179 * to RPe if we're below it.
1180 */
Ben Widawskyb39fb292014-03-19 18:31:11 -07001181 if (new_delay < dev_priv->rps.efficient_freq)
1182 new_delay = dev_priv->rps.efficient_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001183 } else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07001184 if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1185 new_delay = dev_priv->rps.efficient_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001186 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07001187 new_delay = dev_priv->rps.min_freq_softlimit;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001188 adj = 0;
1189 } else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1190 if (adj < 0)
1191 adj *= 2;
1192 else
1193 adj = -1;
Ben Widawskyb39fb292014-03-19 18:31:11 -07001194 new_delay = dev_priv->rps.cur_freq + adj;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001195 } else { /* unknown event */
Ben Widawskyb39fb292014-03-19 18:31:11 -07001196 new_delay = dev_priv->rps.cur_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001197 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001198
Ben Widawsky79249632012-09-07 19:43:42 -07001199 /* sysfs frequency interfaces may have snuck in while servicing the
1200 * interrupt
1201 */
Ville Syrjälä1272e7b2013-11-07 19:57:49 +02001202 new_delay = clamp_t(int, new_delay,
Ben Widawskyb39fb292014-03-19 18:31:11 -07001203 dev_priv->rps.min_freq_softlimit,
1204 dev_priv->rps.max_freq_softlimit);
Deepak S27544362014-01-27 21:35:05 +05301205
Ben Widawskyb39fb292014-03-19 18:31:11 -07001206 dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01001207
1208 if (IS_VALLEYVIEW(dev_priv->dev))
1209 valleyview_set_rps(dev_priv->dev, new_delay);
1210 else
1211 gen6_set_rps(dev_priv->dev, new_delay);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001212
Jesse Barnes4fc688c2012-11-02 11:14:01 -07001213 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001214}
1215
Ben Widawskye3689192012-05-25 16:56:22 -07001216
1217/**
1218 * ivybridge_parity_work - Workqueue called when a parity error interrupt
1219 * occurred.
1220 * @work: workqueue struct
1221 *
1222 * Doesn't actually do anything except notify userspace. As a consequence of
1223 * this event, userspace should try to remap the bad rows since statistically
1224 * it is likely the same row is more likely to go bad again.
1225 */
1226static void ivybridge_parity_work(struct work_struct *work)
1227{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001228 struct drm_i915_private *dev_priv =
1229 container_of(work, struct drm_i915_private, l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -07001230 u32 error_status, row, bank, subbank;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001231 char *parity_event[6];
Ben Widawskye3689192012-05-25 16:56:22 -07001232 uint32_t misccpctl;
1233 unsigned long flags;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001234 uint8_t slice = 0;
Ben Widawskye3689192012-05-25 16:56:22 -07001235
1236 /* We must turn off DOP level clock gating to access the L3 registers.
1237 * In order to prevent a get/put style interface, acquire struct mutex
1238 * any time we access those registers.
1239 */
1240 mutex_lock(&dev_priv->dev->struct_mutex);
1241
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001242 /* If we've screwed up tracking, just let the interrupt fire again */
1243 if (WARN_ON(!dev_priv->l3_parity.which_slice))
1244 goto out;
1245
Ben Widawskye3689192012-05-25 16:56:22 -07001246 misccpctl = I915_READ(GEN7_MISCCPCTL);
1247 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1248 POSTING_READ(GEN7_MISCCPCTL);
1249
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001250 while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1251 u32 reg;
Ben Widawskye3689192012-05-25 16:56:22 -07001252
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001253 slice--;
1254 if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
1255 break;
1256
1257 dev_priv->l3_parity.which_slice &= ~(1<<slice);
1258
1259 reg = GEN7_L3CDERRST1 + (slice * 0x200);
1260
1261 error_status = I915_READ(reg);
1262 row = GEN7_PARITY_ERROR_ROW(error_status);
1263 bank = GEN7_PARITY_ERROR_BANK(error_status);
1264 subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1265
1266 I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
1267 POSTING_READ(reg);
1268
1269 parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1270 parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1271 parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1272 parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
1273 parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
1274 parity_event[5] = NULL;
1275
Dave Airlie5bdebb12013-10-11 14:07:25 +10001276 kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001277 KOBJ_CHANGE, parity_event);
1278
1279 DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
1280 slice, row, bank, subbank);
1281
1282 kfree(parity_event[4]);
1283 kfree(parity_event[3]);
1284 kfree(parity_event[2]);
1285 kfree(parity_event[1]);
1286 }
Ben Widawskye3689192012-05-25 16:56:22 -07001287
1288 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
1289
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001290out:
1291 WARN_ON(dev_priv->l3_parity.which_slice);
Ben Widawskye3689192012-05-25 16:56:22 -07001292 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001293 ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
Ben Widawskye3689192012-05-25 16:56:22 -07001294 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1295
1296 mutex_unlock(&dev_priv->dev->struct_mutex);
Ben Widawskye3689192012-05-25 16:56:22 -07001297}
1298
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001299static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
Ben Widawskye3689192012-05-25 16:56:22 -07001300{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001301 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3689192012-05-25 16:56:22 -07001302
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001303 if (!HAS_L3_DPF(dev))
Ben Widawskye3689192012-05-25 16:56:22 -07001304 return;
1305
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001306 spin_lock(&dev_priv->irq_lock);
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001307 ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001308 spin_unlock(&dev_priv->irq_lock);
Ben Widawskye3689192012-05-25 16:56:22 -07001309
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001310 iir &= GT_PARITY_ERROR(dev);
1311 if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
1312 dev_priv->l3_parity.which_slice |= 1 << 1;
1313
1314 if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
1315 dev_priv->l3_parity.which_slice |= 1 << 0;
1316
Daniel Vettera4da4fa2012-11-02 19:55:07 +01001317 queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -07001318}
1319
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001320static void ilk_gt_irq_handler(struct drm_device *dev,
1321 struct drm_i915_private *dev_priv,
1322 u32 gt_iir)
1323{
1324 if (gt_iir &
1325 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1326 notify_ring(dev, &dev_priv->ring[RCS]);
1327 if (gt_iir & ILK_BSD_USER_INTERRUPT)
1328 notify_ring(dev, &dev_priv->ring[VCS]);
1329}
1330
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001331static void snb_gt_irq_handler(struct drm_device *dev,
1332 struct drm_i915_private *dev_priv,
1333 u32 gt_iir)
1334{
1335
Ben Widawskycc609d52013-05-28 19:22:29 -07001336 if (gt_iir &
1337 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001338 notify_ring(dev, &dev_priv->ring[RCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001339 if (gt_iir & GT_BSD_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001340 notify_ring(dev, &dev_priv->ring[VCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001341 if (gt_iir & GT_BLT_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001342 notify_ring(dev, &dev_priv->ring[BCS]);
1343
Ben Widawskycc609d52013-05-28 19:22:29 -07001344 if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1345 GT_BSD_CS_ERROR_INTERRUPT |
1346 GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
Mika Kuoppala58174462014-02-25 17:11:26 +02001347 i915_handle_error(dev, false, "GT error interrupt 0x%08x",
1348 gt_iir);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001349 }
Ben Widawskye3689192012-05-25 16:56:22 -07001350
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001351 if (gt_iir & GT_PARITY_ERROR(dev))
1352 ivybridge_parity_error_irq_handler(dev, gt_iir);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001353}
1354
Ben Widawsky09610212014-05-15 20:58:08 +03001355static void gen8_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1356{
1357 if ((pm_iir & dev_priv->pm_rps_events) == 0)
1358 return;
1359
1360 spin_lock(&dev_priv->irq_lock);
1361 dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1362 bdw_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1363 spin_unlock(&dev_priv->irq_lock);
1364
1365 queue_work(dev_priv->wq, &dev_priv->rps.work);
1366}
1367
Ben Widawskyabd58f02013-11-02 21:07:09 -07001368static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1369 struct drm_i915_private *dev_priv,
1370 u32 master_ctl)
1371{
1372 u32 rcs, bcs, vcs;
1373 uint32_t tmp = 0;
1374 irqreturn_t ret = IRQ_NONE;
1375
1376 if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1377 tmp = I915_READ(GEN8_GT_IIR(0));
1378 if (tmp) {
1379 ret = IRQ_HANDLED;
1380 rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1381 bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1382 if (rcs & GT_RENDER_USER_INTERRUPT)
1383 notify_ring(dev, &dev_priv->ring[RCS]);
1384 if (bcs & GT_RENDER_USER_INTERRUPT)
1385 notify_ring(dev, &dev_priv->ring[BCS]);
1386 I915_WRITE(GEN8_GT_IIR(0), tmp);
1387 } else
1388 DRM_ERROR("The master control interrupt lied (GT0)!\n");
1389 }
1390
Zhao Yakui85f9b5f2014-04-17 10:37:38 +08001391 if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
Ben Widawskyabd58f02013-11-02 21:07:09 -07001392 tmp = I915_READ(GEN8_GT_IIR(1));
1393 if (tmp) {
1394 ret = IRQ_HANDLED;
1395 vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1396 if (vcs & GT_RENDER_USER_INTERRUPT)
1397 notify_ring(dev, &dev_priv->ring[VCS]);
Zhao Yakui85f9b5f2014-04-17 10:37:38 +08001398 vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
1399 if (vcs & GT_RENDER_USER_INTERRUPT)
1400 notify_ring(dev, &dev_priv->ring[VCS2]);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001401 I915_WRITE(GEN8_GT_IIR(1), tmp);
1402 } 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) {
1409 ret = IRQ_HANDLED;
1410 gen8_rps_irq_handler(dev_priv, tmp);
1411 I915_WRITE(GEN8_GT_IIR(2),
1412 tmp & dev_priv->pm_rps_events);
1413 } 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) {
1420 ret = IRQ_HANDLED;
1421 vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1422 if (vcs & GT_RENDER_USER_INTERRUPT)
1423 notify_ring(dev, &dev_priv->ring[VECS]);
1424 I915_WRITE(GEN8_GT_IIR(3), tmp);
1425 } else
1426 DRM_ERROR("The master control interrupt lied (GT3)!\n");
1427 }
1428
1429 return ret;
1430}
1431
Egbert Eichb543fb02013-04-16 13:36:54 +02001432#define HPD_STORM_DETECT_PERIOD 1000
1433#define HPD_STORM_THRESHOLD 5
1434
Daniel Vetter10a504d2013-06-27 17:52:12 +02001435static inline void intel_hpd_irq_handler(struct drm_device *dev,
Daniel Vetter22062db2013-06-27 17:52:11 +02001436 u32 hotplug_trigger,
1437 const u32 *hpd)
Egbert Eichb543fb02013-04-16 13:36:54 +02001438{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001439 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eichb543fb02013-04-16 13:36:54 +02001440 int i;
Daniel Vetter10a504d2013-06-27 17:52:12 +02001441 bool storm_detected = false;
Egbert Eichb543fb02013-04-16 13:36:54 +02001442
Daniel Vetter91d131d2013-06-27 17:52:14 +02001443 if (!hotplug_trigger)
1444 return;
1445
Imre Deakcc9bd492014-01-16 19:56:54 +02001446 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1447 hotplug_trigger);
1448
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02001449 spin_lock(&dev_priv->irq_lock);
Egbert Eichb543fb02013-04-16 13:36:54 +02001450 for (i = 1; i < HPD_NUM_PINS; i++) {
Egbert Eich821450c2013-04-16 13:36:55 +02001451
Daniel Vetter3ff04a162014-04-24 12:03:17 +02001452 if (hpd[i] & hotplug_trigger &&
1453 dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
1454 /*
1455 * On GMCH platforms the interrupt mask bits only
1456 * prevent irq generation, not the setting of the
1457 * hotplug bits itself. So only WARN about unexpected
1458 * interrupts on saner platforms.
1459 */
1460 WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1461 "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1462 hotplug_trigger, i, hpd[i]);
1463
1464 continue;
1465 }
Egbert Eichb8f102e2013-07-26 14:14:24 +02001466
Egbert Eichb543fb02013-04-16 13:36:54 +02001467 if (!(hpd[i] & hotplug_trigger) ||
1468 dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1469 continue;
1470
Jani Nikulabc5ead8c2013-05-07 15:10:29 +03001471 dev_priv->hpd_event_bits |= (1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001472 if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1473 dev_priv->hpd_stats[i].hpd_last_jiffies
1474 + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1475 dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1476 dev_priv->hpd_stats[i].hpd_cnt = 0;
Egbert Eichb8f102e2013-07-26 14:14:24 +02001477 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001478 } else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1479 dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
Egbert Eich142e2392013-04-11 15:57:57 +02001480 dev_priv->hpd_event_bits &= ~(1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001481 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
Daniel Vetter10a504d2013-06-27 17:52:12 +02001482 storm_detected = true;
Egbert Eichb543fb02013-04-16 13:36:54 +02001483 } else {
1484 dev_priv->hpd_stats[i].hpd_cnt++;
Egbert Eichb8f102e2013-07-26 14:14:24 +02001485 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1486 dev_priv->hpd_stats[i].hpd_cnt);
Egbert Eichb543fb02013-04-16 13:36:54 +02001487 }
1488 }
1489
Daniel Vetter10a504d2013-06-27 17:52:12 +02001490 if (storm_detected)
1491 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02001492 spin_unlock(&dev_priv->irq_lock);
Daniel Vetter5876fa02013-06-27 17:52:13 +02001493
Daniel Vetter645416f2013-09-02 16:22:25 +02001494 /*
1495 * Our hotplug handler can grab modeset locks (by calling down into the
1496 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1497 * queue for otherwise the flush_work in the pageflip code will
1498 * deadlock.
1499 */
1500 schedule_work(&dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +02001501}
1502
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001503static void gmbus_irq_handler(struct drm_device *dev)
1504{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001505 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter28c70f12012-12-01 13:53:45 +01001506
Daniel Vetter28c70f12012-12-01 13:53:45 +01001507 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001508}
1509
Daniel Vetterce99c252012-12-01 13:53:47 +01001510static void dp_aux_irq_handler(struct drm_device *dev)
1511{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001512 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001513
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001514 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetterce99c252012-12-01 13:53:47 +01001515}
1516
Shuang He8bf1e9f2013-10-15 18:55:27 +01001517#if defined(CONFIG_DEBUG_FS)
Daniel Vetter277de952013-10-18 16:37:07 +02001518static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1519 uint32_t crc0, uint32_t crc1,
1520 uint32_t crc2, uint32_t crc3,
1521 uint32_t crc4)
Shuang He8bf1e9f2013-10-15 18:55:27 +01001522{
1523 struct drm_i915_private *dev_priv = dev->dev_private;
1524 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
1525 struct intel_pipe_crc_entry *entry;
Damien Lespiauac2300d2013-10-15 18:55:30 +01001526 int head, tail;
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001527
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001528 spin_lock(&pipe_crc->lock);
1529
Damien Lespiau0c912c72013-10-15 18:55:37 +01001530 if (!pipe_crc->entries) {
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001531 spin_unlock(&pipe_crc->lock);
Damien Lespiau0c912c72013-10-15 18:55:37 +01001532 DRM_ERROR("spurious interrupt\n");
1533 return;
1534 }
1535
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001536 head = pipe_crc->head;
1537 tail = pipe_crc->tail;
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001538
1539 if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001540 spin_unlock(&pipe_crc->lock);
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001541 DRM_ERROR("CRC buffer overflowing\n");
1542 return;
1543 }
1544
1545 entry = &pipe_crc->entries[head];
Shuang He8bf1e9f2013-10-15 18:55:27 +01001546
Daniel Vetter8bc5e952013-10-16 22:55:49 +02001547 entry->frame = dev->driver->get_vblank_counter(dev, pipe);
Daniel Vettereba94eb2013-10-16 22:55:46 +02001548 entry->crc[0] = crc0;
1549 entry->crc[1] = crc1;
1550 entry->crc[2] = crc2;
1551 entry->crc[3] = crc3;
1552 entry->crc[4] = crc4;
Damien Lespiaub2c88f52013-10-15 18:55:29 +01001553
1554 head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
Damien Lespiaud538bbd2013-10-21 14:29:30 +01001555 pipe_crc->head = head;
1556
1557 spin_unlock(&pipe_crc->lock);
Damien Lespiau07144422013-10-15 18:55:40 +01001558
1559 wake_up_interruptible(&pipe_crc->wq);
Shuang He8bf1e9f2013-10-15 18:55:27 +01001560}
Daniel Vetter277de952013-10-18 16:37:07 +02001561#else
1562static inline void
1563display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1564 uint32_t crc0, uint32_t crc1,
1565 uint32_t crc2, uint32_t crc3,
1566 uint32_t crc4) {}
1567#endif
Daniel Vettereba94eb2013-10-16 22:55:46 +02001568
Daniel Vetter277de952013-10-18 16:37:07 +02001569
1570static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
Daniel Vetter5a69b892013-10-16 22:55:52 +02001571{
1572 struct drm_i915_private *dev_priv = dev->dev_private;
1573
Daniel Vetter277de952013-10-18 16:37:07 +02001574 display_pipe_crc_irq_handler(dev, pipe,
1575 I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1576 0, 0, 0, 0);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001577}
1578
Daniel Vetter277de952013-10-18 16:37:07 +02001579static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
Daniel Vettereba94eb2013-10-16 22:55:46 +02001580{
1581 struct drm_i915_private *dev_priv = dev->dev_private;
1582
Daniel Vetter277de952013-10-18 16:37:07 +02001583 display_pipe_crc_irq_handler(dev, pipe,
1584 I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1585 I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1586 I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1587 I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
1588 I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
Daniel Vettereba94eb2013-10-16 22:55:46 +02001589}
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001590
Daniel Vetter277de952013-10-18 16:37:07 +02001591static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001592{
1593 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0b5c5ed2013-10-16 22:55:53 +02001594 uint32_t res1, res2;
1595
1596 if (INTEL_INFO(dev)->gen >= 3)
1597 res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
1598 else
1599 res1 = 0;
1600
1601 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
1602 res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
1603 else
1604 res2 = 0;
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001605
Daniel Vetter277de952013-10-18 16:37:07 +02001606 display_pipe_crc_irq_handler(dev, pipe,
1607 I915_READ(PIPE_CRC_RES_RED(pipe)),
1608 I915_READ(PIPE_CRC_RES_GREEN(pipe)),
1609 I915_READ(PIPE_CRC_RES_BLUE(pipe)),
1610 res1, res2);
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001611}
Shuang He8bf1e9f2013-10-15 18:55:27 +01001612
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001613/* The RPS events need forcewake, so we add them to a work queue and mask their
1614 * IMR bits until the work is done. Other interrupts can be processed without
1615 * the work queue. */
1616static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
Ben Widawskybaf02a12013-05-28 19:22:24 -07001617{
Deepak Sa6706b42014-03-15 20:23:22 +05301618 if (pm_iir & dev_priv->pm_rps_events) {
Daniel Vetter59cdb632013-07-04 23:35:28 +02001619 spin_lock(&dev_priv->irq_lock);
Deepak Sa6706b42014-03-15 20:23:22 +05301620 dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1621 snb_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
Daniel Vetter59cdb632013-07-04 23:35:28 +02001622 spin_unlock(&dev_priv->irq_lock);
Daniel Vetter2adbee62013-07-04 23:35:27 +02001623
1624 queue_work(dev_priv->wq, &dev_priv->rps.work);
Ben Widawskybaf02a12013-05-28 19:22:24 -07001625 }
Ben Widawskybaf02a12013-05-28 19:22:24 -07001626
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001627 if (HAS_VEBOX(dev_priv->dev)) {
1628 if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1629 notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
Ben Widawsky12638c52013-05-28 19:22:31 -07001630
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001631 if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
Mika Kuoppala58174462014-02-25 17:11:26 +02001632 i915_handle_error(dev_priv->dev, false,
1633 "VEBOX CS error interrupt 0x%08x",
1634 pm_iir);
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001635 }
Ben Widawsky12638c52013-05-28 19:22:31 -07001636 }
Ben Widawskybaf02a12013-05-28 19:22:24 -07001637}
1638
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03001639static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
1640{
1641 struct intel_crtc *crtc;
1642
1643 if (!drm_handle_vblank(dev, pipe))
1644 return false;
1645
1646 crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
1647 wake_up(&crtc->vbl_wait);
1648
1649 return true;
1650}
1651
Imre Deakc1874ed2014-02-04 21:35:46 +02001652static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
1653{
1654 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak91d181d2014-02-10 18:42:49 +02001655 u32 pipe_stats[I915_MAX_PIPES] = { };
Imre Deakc1874ed2014-02-04 21:35:46 +02001656 int pipe;
1657
Imre Deak58ead0d2014-02-04 21:35:47 +02001658 spin_lock(&dev_priv->irq_lock);
Imre Deakc1874ed2014-02-04 21:35:46 +02001659 for_each_pipe(pipe) {
Imre Deak91d181d2014-02-10 18:42:49 +02001660 int reg;
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001661 u32 mask, iir_bit = 0;
Imre Deak91d181d2014-02-10 18:42:49 +02001662
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001663 /*
1664 * PIPESTAT bits get signalled even when the interrupt is
1665 * disabled with the mask bits, and some of the status bits do
1666 * not generate interrupts at all (like the underrun bit). Hence
1667 * we need to be careful that we only handle what we want to
1668 * handle.
1669 */
1670 mask = 0;
1671 if (__cpu_fifo_underrun_reporting_enabled(dev, pipe))
1672 mask |= PIPE_FIFO_UNDERRUN_STATUS;
1673
1674 switch (pipe) {
1675 case PIPE_A:
1676 iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1677 break;
1678 case PIPE_B:
1679 iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1680 break;
Ville Syrjälä3278f672014-04-09 13:28:49 +03001681 case PIPE_C:
1682 iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
1683 break;
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001684 }
1685 if (iir & iir_bit)
1686 mask |= dev_priv->pipestat_irq_mask[pipe];
1687
1688 if (!mask)
Imre Deak91d181d2014-02-10 18:42:49 +02001689 continue;
1690
1691 reg = PIPESTAT(pipe);
Daniel Vetterbbb5eeb2014-02-12 17:55:36 +01001692 mask |= PIPESTAT_INT_ENABLE_MASK;
1693 pipe_stats[pipe] = I915_READ(reg) & mask;
Imre Deakc1874ed2014-02-04 21:35:46 +02001694
1695 /*
1696 * Clear the PIPE*STAT regs before the IIR
1697 */
Imre Deak91d181d2014-02-10 18:42:49 +02001698 if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
1699 PIPESTAT_INT_STATUS_MASK))
Imre Deakc1874ed2014-02-04 21:35:46 +02001700 I915_WRITE(reg, pipe_stats[pipe]);
1701 }
Imre Deak58ead0d2014-02-04 21:35:47 +02001702 spin_unlock(&dev_priv->irq_lock);
Imre Deakc1874ed2014-02-04 21:35:46 +02001703
1704 for_each_pipe(pipe) {
1705 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03001706 intel_pipe_handle_vblank(dev, pipe);
Imre Deakc1874ed2014-02-04 21:35:46 +02001707
Imre Deak579a9b02014-02-04 21:35:48 +02001708 if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
Imre Deakc1874ed2014-02-04 21:35:46 +02001709 intel_prepare_page_flip(dev, pipe);
1710 intel_finish_page_flip(dev, pipe);
1711 }
1712
1713 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1714 i9xx_pipe_crc_irq_handler(dev, pipe);
1715
1716 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
1717 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1718 DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
1719 }
1720
1721 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1722 gmbus_irq_handler(dev);
1723}
1724
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001725static void i9xx_hpd_irq_handler(struct drm_device *dev)
1726{
1727 struct drm_i915_private *dev_priv = dev->dev_private;
1728 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1729
1730 if (IS_G4X(dev)) {
1731 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
1732
1733 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_g4x);
1734 } else {
1735 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
1736
1737 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
1738 }
1739
1740 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
1741 hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1742 dp_aux_irq_handler(dev);
1743
1744 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1745 /*
1746 * Make sure hotplug status is cleared before we clear IIR, or else we
1747 * may miss hotplug events.
1748 */
1749 POSTING_READ(PORT_HOTPLUG_STAT);
1750}
1751
Daniel Vetterff1f5252012-10-02 15:10:55 +02001752static irqreturn_t valleyview_irq_handler(int irq, void *arg)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001753{
Daniel Vetter45a83f82014-05-12 19:17:55 +02001754 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03001755 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001756 u32 iir, gt_iir, pm_iir;
1757 irqreturn_t ret = IRQ_NONE;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001758
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001759 while (true) {
1760 iir = I915_READ(VLV_IIR);
1761 gt_iir = I915_READ(GTIIR);
1762 pm_iir = I915_READ(GEN6_PMIIR);
1763
1764 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1765 goto out;
1766
1767 ret = IRQ_HANDLED;
1768
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001769 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001770
Imre Deakc1874ed2014-02-04 21:35:46 +02001771 valleyview_pipestat_irq_handler(dev, iir);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001772
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001773 /* Consume port. Then clear IIR or we'll miss events */
Ville Syrjälä16c6c562014-04-01 10:54:36 +03001774 if (iir & I915_DISPLAY_PORT_INTERRUPT)
1775 i9xx_hpd_irq_handler(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001776
Paulo Zanoni60611c12013-08-15 11:50:01 -03001777 if (pm_iir)
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001778 gen6_rps_irq_handler(dev_priv, pm_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001779
1780 I915_WRITE(GTIIR, gt_iir);
1781 I915_WRITE(GEN6_PMIIR, pm_iir);
1782 I915_WRITE(VLV_IIR, iir);
1783 }
1784
1785out:
1786 return ret;
1787}
1788
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001789static irqreturn_t cherryview_irq_handler(int irq, void *arg)
1790{
Daniel Vetter45a83f82014-05-12 19:17:55 +02001791 struct drm_device *dev = arg;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001792 struct drm_i915_private *dev_priv = dev->dev_private;
1793 u32 master_ctl, iir;
1794 irqreturn_t ret = IRQ_NONE;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001795
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001796 for (;;) {
1797 master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
1798 iir = I915_READ(VLV_IIR);
Ville Syrjälä3278f672014-04-09 13:28:49 +03001799
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001800 if (master_ctl == 0 && iir == 0)
1801 break;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001802
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001803 I915_WRITE(GEN8_MASTER_IRQ, 0);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001804
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001805 gen8_gt_irq_handler(dev, dev_priv, master_ctl);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001806
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001807 valleyview_pipestat_irq_handler(dev, iir);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001808
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001809 /* Consume port. Then clear IIR or we'll miss events */
Ville Syrjälä3278f672014-04-09 13:28:49 +03001810 i9xx_hpd_irq_handler(dev);
Ville Syrjälä8e5fd592014-04-09 13:28:50 +03001811
1812 I915_WRITE(VLV_IIR, iir);
1813
1814 I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
1815 POSTING_READ(GEN8_MASTER_IRQ);
1816
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001817 ret = IRQ_HANDLED;
1818 }
1819
Ville Syrjälä43f328d2014-04-09 20:40:52 +03001820 return ret;
1821}
1822
Adam Jackson23e81d62012-06-06 15:45:44 -04001823static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
Jesse Barnes776ad802011-01-04 15:09:39 -08001824{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001825 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001826 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001827 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
Jesse Barnes776ad802011-01-04 15:09:39 -08001828
Daniel Vetter91d131d2013-06-27 17:52:14 +02001829 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
1830
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001831 if (pch_iir & SDE_AUDIO_POWER_MASK) {
1832 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1833 SDE_AUDIO_POWER_SHIFT);
Jesse Barnes776ad802011-01-04 15:09:39 -08001834 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001835 port_name(port));
1836 }
Jesse Barnes776ad802011-01-04 15:09:39 -08001837
Daniel Vetterce99c252012-12-01 13:53:47 +01001838 if (pch_iir & SDE_AUX_MASK)
1839 dp_aux_irq_handler(dev);
1840
Jesse Barnes776ad802011-01-04 15:09:39 -08001841 if (pch_iir & SDE_GMBUS)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001842 gmbus_irq_handler(dev);
Jesse Barnes776ad802011-01-04 15:09:39 -08001843
1844 if (pch_iir & SDE_AUDIO_HDCP_MASK)
1845 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1846
1847 if (pch_iir & SDE_AUDIO_TRANS_MASK)
1848 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1849
1850 if (pch_iir & SDE_POISON)
1851 DRM_ERROR("PCH poison interrupt\n");
1852
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001853 if (pch_iir & SDE_FDI_MASK)
1854 for_each_pipe(pipe)
1855 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1856 pipe_name(pipe),
1857 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -08001858
1859 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1860 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1861
1862 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1863 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1864
Jesse Barnes776ad802011-01-04 15:09:39 -08001865 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
Paulo Zanoni86642812013-04-12 17:57:57 -03001866 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1867 false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02001868 DRM_ERROR("PCH transcoder A FIFO underrun\n");
Paulo Zanoni86642812013-04-12 17:57:57 -03001869
1870 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1871 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1872 false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02001873 DRM_ERROR("PCH transcoder B FIFO underrun\n");
Paulo Zanoni86642812013-04-12 17:57:57 -03001874}
1875
1876static void ivb_err_int_handler(struct drm_device *dev)
1877{
1878 struct drm_i915_private *dev_priv = dev->dev_private;
1879 u32 err_int = I915_READ(GEN7_ERR_INT);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001880 enum pipe pipe;
Paulo Zanoni86642812013-04-12 17:57:57 -03001881
Paulo Zanonide032bf2013-04-12 17:57:58 -03001882 if (err_int & ERR_INT_POISON)
1883 DRM_ERROR("Poison interrupt\n");
1884
Daniel Vetter5a69b892013-10-16 22:55:52 +02001885 for_each_pipe(pipe) {
1886 if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
1887 if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
1888 false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02001889 DRM_ERROR("Pipe %c FIFO underrun\n",
1890 pipe_name(pipe));
Daniel Vetter5a69b892013-10-16 22:55:52 +02001891 }
Paulo Zanoni86642812013-04-12 17:57:57 -03001892
Daniel Vetter5a69b892013-10-16 22:55:52 +02001893 if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
1894 if (IS_IVYBRIDGE(dev))
Daniel Vetter277de952013-10-18 16:37:07 +02001895 ivb_pipe_crc_irq_handler(dev, pipe);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001896 else
Daniel Vetter277de952013-10-18 16:37:07 +02001897 hsw_pipe_crc_irq_handler(dev, pipe);
Daniel Vetter5a69b892013-10-16 22:55:52 +02001898 }
1899 }
Shuang He8bf1e9f2013-10-15 18:55:27 +01001900
Paulo Zanoni86642812013-04-12 17:57:57 -03001901 I915_WRITE(GEN7_ERR_INT, err_int);
1902}
1903
1904static void cpt_serr_int_handler(struct drm_device *dev)
1905{
1906 struct drm_i915_private *dev_priv = dev->dev_private;
1907 u32 serr_int = I915_READ(SERR_INT);
1908
Paulo Zanonide032bf2013-04-12 17:57:58 -03001909 if (serr_int & SERR_INT_POISON)
1910 DRM_ERROR("PCH poison interrupt\n");
1911
Paulo Zanoni86642812013-04-12 17:57:57 -03001912 if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
1913 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1914 false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02001915 DRM_ERROR("PCH transcoder A FIFO underrun\n");
Paulo Zanoni86642812013-04-12 17:57:57 -03001916
1917 if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
1918 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1919 false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02001920 DRM_ERROR("PCH transcoder B FIFO underrun\n");
Paulo Zanoni86642812013-04-12 17:57:57 -03001921
1922 if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
1923 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
1924 false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02001925 DRM_ERROR("PCH transcoder C FIFO underrun\n");
Paulo Zanoni86642812013-04-12 17:57:57 -03001926
1927 I915_WRITE(SERR_INT, serr_int);
Jesse Barnes776ad802011-01-04 15:09:39 -08001928}
1929
Adam Jackson23e81d62012-06-06 15:45:44 -04001930static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
1931{
Jani Nikula2d1013d2014-03-31 14:27:17 +03001932 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jackson23e81d62012-06-06 15:45:44 -04001933 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001934 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
Adam Jackson23e81d62012-06-06 15:45:44 -04001935
Daniel Vetter91d131d2013-06-27 17:52:14 +02001936 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
1937
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001938 if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1939 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
1940 SDE_AUDIO_POWER_SHIFT_CPT);
1941 DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1942 port_name(port));
1943 }
Adam Jackson23e81d62012-06-06 15:45:44 -04001944
1945 if (pch_iir & SDE_AUX_MASK_CPT)
Daniel Vetterce99c252012-12-01 13:53:47 +01001946 dp_aux_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001947
1948 if (pch_iir & SDE_GMBUS_CPT)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001949 gmbus_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001950
1951 if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
1952 DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
1953
1954 if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
1955 DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
1956
1957 if (pch_iir & SDE_FDI_MASK_CPT)
1958 for_each_pipe(pipe)
1959 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1960 pipe_name(pipe),
1961 I915_READ(FDI_RX_IIR(pipe)));
Paulo Zanoni86642812013-04-12 17:57:57 -03001962
1963 if (pch_iir & SDE_ERROR_CPT)
1964 cpt_serr_int_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001965}
1966
Paulo Zanonic008bc62013-07-12 16:35:10 -03001967static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1968{
1969 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter40da17c2013-10-21 18:04:36 +02001970 enum pipe pipe;
Paulo Zanonic008bc62013-07-12 16:35:10 -03001971
1972 if (de_iir & DE_AUX_CHANNEL_A)
1973 dp_aux_irq_handler(dev);
1974
1975 if (de_iir & DE_GSE)
1976 intel_opregion_asle_intr(dev);
1977
Paulo Zanonic008bc62013-07-12 16:35:10 -03001978 if (de_iir & DE_POISON)
1979 DRM_ERROR("Poison interrupt\n");
1980
Daniel Vetter40da17c2013-10-21 18:04:36 +02001981 for_each_pipe(pipe) {
1982 if (de_iir & DE_PIPE_VBLANK(pipe))
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03001983 intel_pipe_handle_vblank(dev, pipe);
Paulo Zanonic008bc62013-07-12 16:35:10 -03001984
Daniel Vetter40da17c2013-10-21 18:04:36 +02001985 if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
1986 if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02001987 DRM_ERROR("Pipe %c FIFO underrun\n",
1988 pipe_name(pipe));
Paulo Zanonic008bc62013-07-12 16:35:10 -03001989
Daniel Vetter40da17c2013-10-21 18:04:36 +02001990 if (de_iir & DE_PIPE_CRC_DONE(pipe))
1991 i9xx_pipe_crc_irq_handler(dev, pipe);
Daniel Vetter5b3a8562013-10-16 22:55:48 +02001992
Daniel Vetter40da17c2013-10-21 18:04:36 +02001993 /* plane/pipes map 1:1 on ilk+ */
1994 if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
1995 intel_prepare_page_flip(dev, pipe);
1996 intel_finish_page_flip_plane(dev, pipe);
1997 }
Paulo Zanonic008bc62013-07-12 16:35:10 -03001998 }
1999
2000 /* check event from PCH */
2001 if (de_iir & DE_PCH_EVENT) {
2002 u32 pch_iir = I915_READ(SDEIIR);
2003
2004 if (HAS_PCH_CPT(dev))
2005 cpt_irq_handler(dev, pch_iir);
2006 else
2007 ibx_irq_handler(dev, pch_iir);
2008
2009 /* should clear PCH hotplug event before clear CPU irq */
2010 I915_WRITE(SDEIIR, pch_iir);
2011 }
2012
2013 if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2014 ironlake_rps_change_irq_handler(dev);
2015}
2016
Paulo Zanoni9719fb92013-07-12 16:35:11 -03002017static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
2018{
2019 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00002020 enum pipe pipe;
Paulo Zanoni9719fb92013-07-12 16:35:11 -03002021
2022 if (de_iir & DE_ERR_INT_IVB)
2023 ivb_err_int_handler(dev);
2024
2025 if (de_iir & DE_AUX_CHANNEL_A_IVB)
2026 dp_aux_irq_handler(dev);
2027
2028 if (de_iir & DE_GSE_IVB)
2029 intel_opregion_asle_intr(dev);
2030
Damien Lespiau07d27e22014-03-03 17:31:46 +00002031 for_each_pipe(pipe) {
2032 if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03002033 intel_pipe_handle_vblank(dev, pipe);
Daniel Vetter40da17c2013-10-21 18:04:36 +02002034
2035 /* plane/pipes map 1:1 on ilk+ */
Damien Lespiau07d27e22014-03-03 17:31:46 +00002036 if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
2037 intel_prepare_page_flip(dev, pipe);
2038 intel_finish_page_flip_plane(dev, pipe);
Paulo Zanoni9719fb92013-07-12 16:35:11 -03002039 }
2040 }
2041
2042 /* check event from PCH */
2043 if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
2044 u32 pch_iir = I915_READ(SDEIIR);
2045
2046 cpt_irq_handler(dev, pch_iir);
2047
2048 /* clear PCH hotplug event before clear CPU irq */
2049 I915_WRITE(SDEIIR, pch_iir);
2050 }
2051}
2052
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002053static irqreturn_t ironlake_irq_handler(int irq, void *arg)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002054{
Daniel Vetter45a83f82014-05-12 19:17:55 +02002055 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03002056 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002057 u32 de_iir, gt_iir, de_ier, sde_ier = 0;
Chris Wilson0e434062012-05-09 21:45:44 +01002058 irqreturn_t ret = IRQ_NONE;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002059
Paulo Zanoni86642812013-04-12 17:57:57 -03002060 /* We get interrupts on unclaimed registers, so check for this before we
2061 * do any I915_{READ,WRITE}. */
Chris Wilson907b28c2013-07-19 20:36:52 +01002062 intel_uncore_check_errors(dev);
Paulo Zanoni86642812013-04-12 17:57:57 -03002063
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002064 /* disable master interrupt before clearing iir */
2065 de_ier = I915_READ(DEIER);
2066 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Paulo Zanoni23a78512013-07-12 16:35:14 -03002067 POSTING_READ(DEIER);
Chris Wilson0e434062012-05-09 21:45:44 +01002068
Paulo Zanoni44498ae2013-02-22 17:05:28 -03002069 /* Disable south interrupts. We'll only write to SDEIIR once, so further
2070 * interrupts will will be stored on its back queue, and then we'll be
2071 * able to process them after we restore SDEIER (as soon as we restore
2072 * it, we'll get an interrupt if SDEIIR still has something to process
2073 * due to its back queue). */
Ben Widawskyab5c6082013-04-05 13:12:41 -07002074 if (!HAS_PCH_NOP(dev)) {
2075 sde_ier = I915_READ(SDEIER);
2076 I915_WRITE(SDEIER, 0);
2077 POSTING_READ(SDEIER);
2078 }
Paulo Zanoni44498ae2013-02-22 17:05:28 -03002079
Chris Wilson0e434062012-05-09 21:45:44 +01002080 gt_iir = I915_READ(GTIIR);
2081 if (gt_iir) {
Paulo Zanonid8fc8a42013-07-19 18:57:55 -03002082 if (INTEL_INFO(dev)->gen >= 6)
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002083 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Paulo Zanonid8fc8a42013-07-19 18:57:55 -03002084 else
2085 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01002086 I915_WRITE(GTIIR, gt_iir);
2087 ret = IRQ_HANDLED;
2088 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002089
2090 de_iir = I915_READ(DEIIR);
Chris Wilson0e434062012-05-09 21:45:44 +01002091 if (de_iir) {
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002092 if (INTEL_INFO(dev)->gen >= 7)
2093 ivb_display_irq_handler(dev, de_iir);
2094 else
2095 ilk_display_irq_handler(dev, de_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01002096 I915_WRITE(DEIIR, de_iir);
2097 ret = IRQ_HANDLED;
2098 }
2099
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002100 if (INTEL_INFO(dev)->gen >= 6) {
2101 u32 pm_iir = I915_READ(GEN6_PMIIR);
2102 if (pm_iir) {
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03002103 gen6_rps_irq_handler(dev_priv, pm_iir);
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03002104 I915_WRITE(GEN6_PMIIR, pm_iir);
2105 ret = IRQ_HANDLED;
2106 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002107 }
2108
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002109 I915_WRITE(DEIER, de_ier);
2110 POSTING_READ(DEIER);
Ben Widawskyab5c6082013-04-05 13:12:41 -07002111 if (!HAS_PCH_NOP(dev)) {
2112 I915_WRITE(SDEIER, sde_ier);
2113 POSTING_READ(SDEIER);
2114 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002115
2116 return ret;
2117}
2118
Ben Widawskyabd58f02013-11-02 21:07:09 -07002119static irqreturn_t gen8_irq_handler(int irq, void *arg)
2120{
2121 struct drm_device *dev = arg;
2122 struct drm_i915_private *dev_priv = dev->dev_private;
2123 u32 master_ctl;
2124 irqreturn_t ret = IRQ_NONE;
2125 uint32_t tmp = 0;
Daniel Vetterc42664c2013-11-07 11:05:40 +01002126 enum pipe pipe;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002127
Ben Widawskyabd58f02013-11-02 21:07:09 -07002128 master_ctl = I915_READ(GEN8_MASTER_IRQ);
2129 master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2130 if (!master_ctl)
2131 return IRQ_NONE;
2132
2133 I915_WRITE(GEN8_MASTER_IRQ, 0);
2134 POSTING_READ(GEN8_MASTER_IRQ);
2135
2136 ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2137
2138 if (master_ctl & GEN8_DE_MISC_IRQ) {
2139 tmp = I915_READ(GEN8_DE_MISC_IIR);
2140 if (tmp & GEN8_DE_MISC_GSE)
2141 intel_opregion_asle_intr(dev);
2142 else if (tmp)
2143 DRM_ERROR("Unexpected DE Misc interrupt\n");
2144 else
2145 DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2146
2147 if (tmp) {
2148 I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2149 ret = IRQ_HANDLED;
2150 }
2151 }
2152
Daniel Vetter6d766f02013-11-07 14:49:55 +01002153 if (master_ctl & GEN8_DE_PORT_IRQ) {
2154 tmp = I915_READ(GEN8_DE_PORT_IIR);
2155 if (tmp & GEN8_AUX_CHANNEL_A)
2156 dp_aux_irq_handler(dev);
2157 else if (tmp)
2158 DRM_ERROR("Unexpected DE Port interrupt\n");
2159 else
2160 DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
2161
2162 if (tmp) {
2163 I915_WRITE(GEN8_DE_PORT_IIR, tmp);
2164 ret = IRQ_HANDLED;
2165 }
2166 }
2167
Daniel Vetterc42664c2013-11-07 11:05:40 +01002168 for_each_pipe(pipe) {
2169 uint32_t pipe_iir;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002170
Daniel Vetterc42664c2013-11-07 11:05:40 +01002171 if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2172 continue;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002173
Daniel Vetterc42664c2013-11-07 11:05:40 +01002174 pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2175 if (pipe_iir & GEN8_PIPE_VBLANK)
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03002176 intel_pipe_handle_vblank(dev, pipe);
Ben Widawskyabd58f02013-11-02 21:07:09 -07002177
Damien Lespiaud0e1f1c2014-04-08 01:22:44 +01002178 if (pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE) {
Daniel Vetterc42664c2013-11-07 11:05:40 +01002179 intel_prepare_page_flip(dev, pipe);
2180 intel_finish_page_flip_plane(dev, pipe);
Ben Widawskyabd58f02013-11-02 21:07:09 -07002181 }
Daniel Vetterc42664c2013-11-07 11:05:40 +01002182
Daniel Vetter0fbe7872013-11-07 11:05:44 +01002183 if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
2184 hsw_pipe_crc_irq_handler(dev, pipe);
2185
Daniel Vetter38d83c962013-11-07 11:05:46 +01002186 if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
2187 if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
2188 false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02002189 DRM_ERROR("Pipe %c FIFO underrun\n",
2190 pipe_name(pipe));
Daniel Vetter38d83c962013-11-07 11:05:46 +01002191 }
2192
Daniel Vetter30100f22013-11-07 14:49:24 +01002193 if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
2194 DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
2195 pipe_name(pipe),
2196 pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2197 }
Daniel Vetterc42664c2013-11-07 11:05:40 +01002198
2199 if (pipe_iir) {
2200 ret = IRQ_HANDLED;
2201 I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2202 } else
Ben Widawskyabd58f02013-11-02 21:07:09 -07002203 DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2204 }
2205
Daniel Vetter92d03a82013-11-07 11:05:43 +01002206 if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
2207 /*
2208 * FIXME(BDW): Assume for now that the new interrupt handling
2209 * scheme also closed the SDE interrupt handling race we've seen
2210 * on older pch-split platforms. But this needs testing.
2211 */
2212 u32 pch_iir = I915_READ(SDEIIR);
2213
2214 cpt_irq_handler(dev, pch_iir);
2215
2216 if (pch_iir) {
2217 I915_WRITE(SDEIIR, pch_iir);
2218 ret = IRQ_HANDLED;
2219 }
2220 }
2221
Ben Widawskyabd58f02013-11-02 21:07:09 -07002222 I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2223 POSTING_READ(GEN8_MASTER_IRQ);
2224
2225 return ret;
2226}
2227
Daniel Vetter17e1df02013-09-08 21:57:13 +02002228static void i915_error_wake_up(struct drm_i915_private *dev_priv,
2229 bool reset_completed)
2230{
2231 struct intel_ring_buffer *ring;
2232 int i;
2233
2234 /*
2235 * Notify all waiters for GPU completion events that reset state has
2236 * been changed, and that they need to restart their wait after
2237 * checking for potential errors (and bail out to drop locks if there is
2238 * a gpu reset pending so that i915_error_work_func can acquire them).
2239 */
2240
2241 /* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
2242 for_each_ring(ring, dev_priv, i)
2243 wake_up_all(&ring->irq_queue);
2244
2245 /* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
2246 wake_up_all(&dev_priv->pending_flip_queue);
2247
2248 /*
2249 * Signal tasks blocked in i915_gem_wait_for_error that the pending
2250 * reset state is cleared.
2251 */
2252 if (reset_completed)
2253 wake_up_all(&dev_priv->gpu_error.reset_queue);
2254}
2255
Jesse Barnes8a905232009-07-11 16:48:03 -04002256/**
2257 * i915_error_work_func - do process context error handling work
2258 * @work: work struct
2259 *
2260 * Fire an error uevent so userspace can see that a hang or error
2261 * was detected.
2262 */
2263static void i915_error_work_func(struct work_struct *work)
2264{
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002265 struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
2266 work);
Jani Nikula2d1013d2014-03-31 14:27:17 +03002267 struct drm_i915_private *dev_priv =
2268 container_of(error, struct drm_i915_private, gpu_error);
Jesse Barnes8a905232009-07-11 16:48:03 -04002269 struct drm_device *dev = dev_priv->dev;
Ben Widawskycce723e2013-07-19 09:16:42 -07002270 char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2271 char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2272 char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
Daniel Vetter17e1df02013-09-08 21:57:13 +02002273 int ret;
Jesse Barnes8a905232009-07-11 16:48:03 -04002274
Dave Airlie5bdebb12013-10-11 14:07:25 +10002275 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -04002276
Daniel Vetter7db0ba22012-12-06 16:23:37 +01002277 /*
2278 * Note that there's only one work item which does gpu resets, so we
2279 * need not worry about concurrent gpu resets potentially incrementing
2280 * error->reset_counter twice. We only need to take care of another
2281 * racing irq/hangcheck declaring the gpu dead for a second time. A
2282 * quick check for that is good enough: schedule_work ensures the
2283 * correct ordering between hang detection and this work item, and since
2284 * the reset in-progress bit is only ever set by code outside of this
2285 * work we don't need to worry about any other races.
2286 */
2287 if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +01002288 DRM_DEBUG_DRIVER("resetting chip\n");
Dave Airlie5bdebb12013-10-11 14:07:25 +10002289 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
Daniel Vetter7db0ba22012-12-06 16:23:37 +01002290 reset_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002291
Daniel Vetter17e1df02013-09-08 21:57:13 +02002292 /*
Imre Deakf454c692014-04-23 01:09:04 +03002293 * In most cases it's guaranteed that we get here with an RPM
2294 * reference held, for example because there is a pending GPU
2295 * request that won't finish until the reset is done. This
2296 * isn't the case at least when we get here by doing a
2297 * simulated reset via debugs, so get an RPM reference.
2298 */
2299 intel_runtime_pm_get(dev_priv);
2300 /*
Daniel Vetter17e1df02013-09-08 21:57:13 +02002301 * All state reset _must_ be completed before we update the
2302 * reset counter, for otherwise waiters might miss the reset
2303 * pending state and not properly drop locks, resulting in
2304 * deadlocks with the reset work.
2305 */
Daniel Vetterf69061b2012-12-06 09:01:42 +01002306 ret = i915_reset(dev);
2307
Daniel Vetter17e1df02013-09-08 21:57:13 +02002308 intel_display_handle_reset(dev);
2309
Imre Deakf454c692014-04-23 01:09:04 +03002310 intel_runtime_pm_put(dev_priv);
2311
Daniel Vetterf69061b2012-12-06 09:01:42 +01002312 if (ret == 0) {
2313 /*
2314 * After all the gem state is reset, increment the reset
2315 * counter and wake up everyone waiting for the reset to
2316 * complete.
2317 *
2318 * Since unlock operations are a one-sided barrier only,
2319 * we need to insert a barrier here to order any seqno
2320 * updates before
2321 * the counter increment.
2322 */
2323 smp_mb__before_atomic_inc();
2324 atomic_inc(&dev_priv->gpu_error.reset_counter);
2325
Dave Airlie5bdebb12013-10-11 14:07:25 +10002326 kobject_uevent_env(&dev->primary->kdev->kobj,
Daniel Vetterf69061b2012-12-06 09:01:42 +01002327 KOBJ_CHANGE, reset_done_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002328 } else {
Mika Kuoppala2ac0f452013-11-12 14:44:19 +02002329 atomic_set_mask(I915_WEDGED, &error->reset_counter);
Ben Gamarif316a422009-09-14 17:48:46 -04002330 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002331
Daniel Vetter17e1df02013-09-08 21:57:13 +02002332 /*
2333 * Note: The wake_up also serves as a memory barrier so that
2334 * waiters see the update value of the reset counter atomic_t.
2335 */
2336 i915_error_wake_up(dev_priv, true);
Ben Gamarif316a422009-09-14 17:48:46 -04002337 }
Jesse Barnes8a905232009-07-11 16:48:03 -04002338}
2339
Chris Wilson35aed2e2010-05-27 13:18:12 +01002340static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04002341{
2342 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybd9854f2012-08-23 15:18:09 -07002343 uint32_t instdone[I915_NUM_INSTDONE_REG];
Jesse Barnes8a905232009-07-11 16:48:03 -04002344 u32 eir = I915_READ(EIR);
Ben Widawsky050ee912012-08-22 11:32:15 -07002345 int pipe, i;
Jesse Barnes8a905232009-07-11 16:48:03 -04002346
Chris Wilson35aed2e2010-05-27 13:18:12 +01002347 if (!eir)
2348 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04002349
Joe Perchesa70491c2012-03-18 13:00:11 -07002350 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04002351
Ben Widawskybd9854f2012-08-23 15:18:09 -07002352 i915_get_extra_instdone(dev, instdone);
2353
Jesse Barnes8a905232009-07-11 16:48:03 -04002354 if (IS_G4X(dev)) {
2355 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
2356 u32 ipeir = I915_READ(IPEIR_I965);
2357
Joe Perchesa70491c2012-03-18 13:00:11 -07002358 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2359 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Ben Widawsky050ee912012-08-22 11:32:15 -07002360 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2361 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Joe Perchesa70491c2012-03-18 13:00:11 -07002362 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002363 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002364 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002365 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002366 }
2367 if (eir & GM45_ERROR_PAGE_TABLE) {
2368 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002369 pr_err("page table error\n");
2370 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002371 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002372 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002373 }
2374 }
2375
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002376 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002377 if (eir & I915_ERROR_PAGE_TABLE) {
2378 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002379 pr_err("page table error\n");
2380 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002381 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002382 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002383 }
2384 }
2385
2386 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002387 pr_err("memory refresh error:\n");
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002388 for_each_pipe(pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07002389 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002390 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04002391 /* pipestat has already been acked */
2392 }
2393 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002394 pr_err("instruction error\n");
2395 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Ben Widawsky050ee912012-08-22 11:32:15 -07002396 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2397 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002398 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002399 u32 ipeir = I915_READ(IPEIR);
2400
Joe Perchesa70491c2012-03-18 13:00:11 -07002401 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
2402 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
Joe Perchesa70491c2012-03-18 13:00:11 -07002403 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04002404 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002405 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002406 } else {
2407 u32 ipeir = I915_READ(IPEIR_I965);
2408
Joe Perchesa70491c2012-03-18 13:00:11 -07002409 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2410 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07002411 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002412 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002413 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002414 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002415 }
2416 }
2417
2418 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002419 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002420 eir = I915_READ(EIR);
2421 if (eir) {
2422 /*
2423 * some errors might have become stuck,
2424 * mask them.
2425 */
2426 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
2427 I915_WRITE(EMR, I915_READ(EMR) | eir);
2428 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2429 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01002430}
2431
2432/**
2433 * i915_handle_error - handle an error interrupt
2434 * @dev: drm device
2435 *
2436 * Do some basic checking of regsiter state at error interrupt time and
2437 * dump it to the syslog. Also call i915_capture_error_state() to make
2438 * sure we get a record and make it available in debugfs. Fire a uevent
2439 * so userspace knows something bad happened (should trigger collection
2440 * of a ring dump etc.).
2441 */
Mika Kuoppala58174462014-02-25 17:11:26 +02002442void i915_handle_error(struct drm_device *dev, bool wedged,
2443 const char *fmt, ...)
Chris Wilson35aed2e2010-05-27 13:18:12 +01002444{
2445 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala58174462014-02-25 17:11:26 +02002446 va_list args;
2447 char error_msg[80];
Chris Wilson35aed2e2010-05-27 13:18:12 +01002448
Mika Kuoppala58174462014-02-25 17:11:26 +02002449 va_start(args, fmt);
2450 vscnprintf(error_msg, sizeof(error_msg), fmt, args);
2451 va_end(args);
2452
2453 i915_capture_error_state(dev, wedged, error_msg);
Chris Wilson35aed2e2010-05-27 13:18:12 +01002454 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04002455
Ben Gamariba1234d2009-09-14 17:48:47 -04002456 if (wedged) {
Daniel Vetterf69061b2012-12-06 09:01:42 +01002457 atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2458 &dev_priv->gpu_error.reset_counter);
Ben Gamariba1234d2009-09-14 17:48:47 -04002459
Ben Gamari11ed50e2009-09-14 17:48:45 -04002460 /*
Daniel Vetter17e1df02013-09-08 21:57:13 +02002461 * Wakeup waiting processes so that the reset work function
2462 * i915_error_work_func doesn't deadlock trying to grab various
2463 * locks. By bumping the reset counter first, the woken
2464 * processes will see a reset in progress and back off,
2465 * releasing their locks and then wait for the reset completion.
2466 * We must do this for _all_ gpu waiters that might hold locks
2467 * that the reset work needs to acquire.
2468 *
2469 * Note: The wake_up serves as the required memory barrier to
2470 * ensure that the waiters see the updated value of the reset
2471 * counter atomic_t.
Ben Gamari11ed50e2009-09-14 17:48:45 -04002472 */
Daniel Vetter17e1df02013-09-08 21:57:13 +02002473 i915_error_wake_up(dev_priv, false);
Ben Gamari11ed50e2009-09-14 17:48:45 -04002474 }
2475
Daniel Vetter122f46b2013-09-04 17:36:14 +02002476 /*
2477 * Our reset work can grab modeset locks (since it needs to reset the
2478 * state of outstanding pagelips). Hence it must not be run on our own
2479 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2480 * code will deadlock.
2481 */
2482 schedule_work(&dev_priv->gpu_error.work);
Jesse Barnes8a905232009-07-11 16:48:03 -04002483}
2484
Ville Syrjälä21ad8332013-02-19 15:16:39 +02002485static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002486{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002487 struct drm_i915_private *dev_priv = dev->dev_private;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002488 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
2489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00002490 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002491 struct intel_unpin_work *work;
2492 unsigned long flags;
2493 bool stall_detected;
2494
2495 /* Ignore early vblank irqs */
2496 if (intel_crtc == NULL)
2497 return;
2498
2499 spin_lock_irqsave(&dev->event_lock, flags);
2500 work = intel_crtc->unpin_work;
2501
Chris Wilsone7d841c2012-12-03 11:36:30 +00002502 if (work == NULL ||
2503 atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2504 !work->enable_stall_check) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002505 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
2506 spin_unlock_irqrestore(&dev->event_lock, flags);
2507 return;
2508 }
2509
2510 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00002511 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002512 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002513 int dspsurf = DSPSURF(intel_crtc->plane);
Armin Reese446f2542012-03-30 16:20:16 -07002514 stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002515 i915_gem_obj_ggtt_offset(obj);
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002516 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002517 int dspaddr = DSPADDR(intel_crtc->plane);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002518 stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
Matt Roperf4510a22014-04-01 15:22:40 -07002519 crtc->y * crtc->primary->fb->pitches[0] +
2520 crtc->x * crtc->primary->fb->bits_per_pixel/8);
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002521 }
2522
2523 spin_unlock_irqrestore(&dev->event_lock, flags);
2524
2525 if (stall_detected) {
2526 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
2527 intel_prepare_page_flip(dev, intel_crtc->plane);
2528 }
2529}
2530
Keith Packard42f52ef2008-10-18 19:39:29 -07002531/* Called from drm generic code, passed 'crtc' which
2532 * we use as a pipe index
2533 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002534static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002535{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002536 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002537 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002538
Chris Wilson5eddb702010-09-11 13:48:45 +01002539 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002540 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002541
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002542 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002543 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08002544 i915_enable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002545 PIPE_START_VBLANK_INTERRUPT_STATUS);
Keith Packarde9d21d72008-10-16 11:31:38 -07002546 else
Keith Packard7c463582008-11-04 02:03:27 -08002547 i915_enable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002548 PIPE_VBLANK_INTERRUPT_STATUS);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002549
2550 /* maintain vblank delivery even in deep C-states */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002551 if (INTEL_INFO(dev)->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02002552 I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002553 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002554
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002555 return 0;
2556}
2557
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002558static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002559{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002560 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf796cf82011-04-07 13:58:17 -07002561 unsigned long irqflags;
Paulo Zanonib5184212013-07-12 20:00:08 -03002562 uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
Daniel Vetter40da17c2013-10-21 18:04:36 +02002563 DE_PIPE_VBLANK(pipe);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002564
2565 if (!i915_pipe_enabled(dev, pipe))
2566 return -EINVAL;
2567
2568 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Paulo Zanonib5184212013-07-12 20:00:08 -03002569 ironlake_enable_display_irq(dev_priv, bit);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002570 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2571
2572 return 0;
2573}
2574
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002575static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
2576{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002577 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002578 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002579
2580 if (!i915_pipe_enabled(dev, pipe))
2581 return -EINVAL;
2582
2583 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002584 i915_enable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002585 PIPE_START_VBLANK_INTERRUPT_STATUS);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002586 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2587
2588 return 0;
2589}
2590
Ben Widawskyabd58f02013-11-02 21:07:09 -07002591static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2592{
2593 struct drm_i915_private *dev_priv = dev->dev_private;
2594 unsigned long irqflags;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002595
2596 if (!i915_pipe_enabled(dev, pipe))
2597 return -EINVAL;
2598
2599 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter7167d7c2013-11-07 11:05:45 +01002600 dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
2601 I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2602 POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
Ben Widawskyabd58f02013-11-02 21:07:09 -07002603 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2604 return 0;
2605}
2606
Keith Packard42f52ef2008-10-18 19:39:29 -07002607/* Called from drm generic code, passed 'crtc' which
2608 * we use as a pipe index
2609 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002610static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002611{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002612 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002613 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002614
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002615 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002616 if (INTEL_INFO(dev)->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02002617 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson8692d00e2011-02-05 10:08:21 +00002618
Jesse Barnesf796cf82011-04-07 13:58:17 -07002619 i915_disable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002620 PIPE_VBLANK_INTERRUPT_STATUS |
2621 PIPE_START_VBLANK_INTERRUPT_STATUS);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002622 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2623}
2624
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002625static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002626{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002627 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf796cf82011-04-07 13:58:17 -07002628 unsigned long irqflags;
Paulo Zanonib5184212013-07-12 20:00:08 -03002629 uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
Daniel Vetter40da17c2013-10-21 18:04:36 +02002630 DE_PIPE_VBLANK(pipe);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002631
2632 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Paulo Zanonib5184212013-07-12 20:00:08 -03002633 ironlake_disable_display_irq(dev_priv, bit);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002634 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2635}
2636
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002637static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
2638{
Jani Nikula2d1013d2014-03-31 14:27:17 +03002639 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002640 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002641
2642 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002643 i915_disable_pipestat(dev_priv, pipe,
Imre Deak755e9012014-02-10 18:42:47 +02002644 PIPE_START_VBLANK_INTERRUPT_STATUS);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002645 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2646}
2647
Ben Widawskyabd58f02013-11-02 21:07:09 -07002648static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2649{
2650 struct drm_i915_private *dev_priv = dev->dev_private;
2651 unsigned long irqflags;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002652
2653 if (!i915_pipe_enabled(dev, pipe))
2654 return;
2655
2656 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter7167d7c2013-11-07 11:05:45 +01002657 dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
2658 I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2659 POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
Ben Widawskyabd58f02013-11-02 21:07:09 -07002660 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2661}
2662
Chris Wilson893eead2010-10-27 14:44:35 +01002663static u32
2664ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08002665{
Chris Wilson893eead2010-10-27 14:44:35 +01002666 return list_entry(ring->request_list.prev,
2667 struct drm_i915_gem_request, list)->seqno;
2668}
2669
Chris Wilson9107e9d2013-06-10 11:20:20 +01002670static bool
2671ring_idle(struct intel_ring_buffer *ring, u32 seqno)
Chris Wilson893eead2010-10-27 14:44:35 +01002672{
Chris Wilson9107e9d2013-06-10 11:20:20 +01002673 return (list_empty(&ring->request_list) ||
2674 i915_seqno_passed(seqno, ring_last_seqno(ring)));
Ben Gamarif65d9422009-09-14 17:48:44 -04002675}
2676
Daniel Vettera028c4b2014-03-15 00:08:56 +01002677static bool
2678ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2679{
2680 if (INTEL_INFO(dev)->gen >= 8) {
2681 /*
2682 * FIXME: gen8 semaphore support - currently we don't emit
2683 * semaphores on bdw anyway, but this needs to be addressed when
2684 * we merge that code.
2685 */
2686 return false;
2687 } else {
2688 ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2689 return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2690 MI_SEMAPHORE_REGISTER);
2691 }
2692}
2693
Chris Wilson6274f212013-06-10 11:20:21 +01002694static struct intel_ring_buffer *
Daniel Vetter921d42e2014-03-18 10:26:04 +01002695semaphore_wait_to_signaller_ring(struct intel_ring_buffer *ring, u32 ipehr)
2696{
2697 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2698 struct intel_ring_buffer *signaller;
2699 int i;
2700
2701 if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2702 /*
2703 * FIXME: gen8 semaphore support - currently we don't emit
2704 * semaphores on bdw anyway, but this needs to be addressed when
2705 * we merge that code.
2706 */
2707 return NULL;
2708 } else {
2709 u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2710
2711 for_each_ring(signaller, dev_priv, i) {
2712 if(ring == signaller)
2713 continue;
2714
Ben Widawskyebc348b2014-04-29 14:52:28 -07002715 if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
Daniel Vetter921d42e2014-03-18 10:26:04 +01002716 return signaller;
2717 }
2718 }
2719
2720 DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x\n",
2721 ring->id, ipehr);
2722
2723 return NULL;
2724}
2725
Chris Wilson6274f212013-06-10 11:20:21 +01002726static struct intel_ring_buffer *
2727semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
Chris Wilsona24a11e2013-03-14 17:52:05 +02002728{
2729 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Daniel Vetter88fe4292014-03-15 00:08:55 +01002730 u32 cmd, ipehr, head;
2731 int i;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002732
2733 ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
Daniel Vettera028c4b2014-03-15 00:08:56 +01002734 if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
Chris Wilson6274f212013-06-10 11:20:21 +01002735 return NULL;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002736
Daniel Vetter88fe4292014-03-15 00:08:55 +01002737 /*
2738 * HEAD is likely pointing to the dword after the actual command,
2739 * so scan backwards until we find the MBOX. But limit it to just 3
2740 * dwords. Note that we don't care about ACTHD here since that might
2741 * point at at batch, and semaphores are always emitted into the
2742 * ringbuffer itself.
Chris Wilsona24a11e2013-03-14 17:52:05 +02002743 */
Daniel Vetter88fe4292014-03-15 00:08:55 +01002744 head = I915_READ_HEAD(ring) & HEAD_ADDR;
2745
2746 for (i = 4; i; --i) {
2747 /*
2748 * Be paranoid and presume the hw has gone off into the wild -
2749 * our ring is smaller than what the hardware (and hence
2750 * HEAD_ADDR) allows. Also handles wrap-around.
2751 */
2752 head &= ring->size - 1;
2753
2754 /* This here seems to blow up */
2755 cmd = ioread32(ring->virtual_start + head);
Chris Wilsona24a11e2013-03-14 17:52:05 +02002756 if (cmd == ipehr)
2757 break;
2758
Daniel Vetter88fe4292014-03-15 00:08:55 +01002759 head -= 4;
2760 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02002761
Daniel Vetter88fe4292014-03-15 00:08:55 +01002762 if (!i)
2763 return NULL;
2764
2765 *seqno = ioread32(ring->virtual_start + head + 4) + 1;
Daniel Vetter921d42e2014-03-18 10:26:04 +01002766 return semaphore_wait_to_signaller_ring(ring, ipehr);
Chris Wilsona24a11e2013-03-14 17:52:05 +02002767}
2768
Chris Wilson6274f212013-06-10 11:20:21 +01002769static int semaphore_passed(struct intel_ring_buffer *ring)
2770{
2771 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2772 struct intel_ring_buffer *signaller;
2773 u32 seqno, ctl;
2774
2775 ring->hangcheck.deadlock = true;
2776
2777 signaller = semaphore_waits_for(ring, &seqno);
2778 if (signaller == NULL || signaller->hangcheck.deadlock)
2779 return -1;
2780
2781 /* cursory check for an unkickable deadlock */
2782 ctl = I915_READ_CTL(signaller);
2783 if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
2784 return -1;
2785
2786 return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
2787}
2788
2789static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
2790{
2791 struct intel_ring_buffer *ring;
2792 int i;
2793
2794 for_each_ring(ring, dev_priv, i)
2795 ring->hangcheck.deadlock = false;
2796}
2797
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03002798static enum intel_ring_hangcheck_action
Chris Wilson50877442014-03-21 12:41:53 +00002799ring_stuck(struct intel_ring_buffer *ring, u64 acthd)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002800{
2801 struct drm_device *dev = ring->dev;
2802 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002803 u32 tmp;
2804
Chris Wilson6274f212013-06-10 11:20:21 +01002805 if (ring->hangcheck.acthd != acthd)
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002806 return HANGCHECK_ACTIVE;
Chris Wilson6274f212013-06-10 11:20:21 +01002807
Chris Wilson9107e9d2013-06-10 11:20:20 +01002808 if (IS_GEN2(dev))
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002809 return HANGCHECK_HUNG;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002810
2811 /* Is the chip hanging on a WAIT_FOR_EVENT?
2812 * If so we can simply poke the RB_WAIT bit
2813 * and break the hang. This should work on
2814 * all but the second generation chipsets.
2815 */
2816 tmp = I915_READ_CTL(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002817 if (tmp & RING_WAIT) {
Mika Kuoppala58174462014-02-25 17:11:26 +02002818 i915_handle_error(dev, false,
2819 "Kicking stuck wait on %s",
2820 ring->name);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002821 I915_WRITE_CTL(ring, tmp);
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002822 return HANGCHECK_KICK;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002823 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02002824
Chris Wilson6274f212013-06-10 11:20:21 +01002825 if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
2826 switch (semaphore_passed(ring)) {
2827 default:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002828 return HANGCHECK_HUNG;
Chris Wilson6274f212013-06-10 11:20:21 +01002829 case 1:
Mika Kuoppala58174462014-02-25 17:11:26 +02002830 i915_handle_error(dev, false,
2831 "Kicking stuck semaphore on %s",
2832 ring->name);
Chris Wilson6274f212013-06-10 11:20:21 +01002833 I915_WRITE_CTL(ring, tmp);
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002834 return HANGCHECK_KICK;
Chris Wilson6274f212013-06-10 11:20:21 +01002835 case 0:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002836 return HANGCHECK_WAIT;
Chris Wilson6274f212013-06-10 11:20:21 +01002837 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002838 }
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002839
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002840 return HANGCHECK_HUNG;
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002841}
2842
Ben Gamarif65d9422009-09-14 17:48:44 -04002843/**
2844 * This is called when the chip hasn't reported back with completed
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002845 * batchbuffers in a long time. We keep track per ring seqno progress and
2846 * if there are no progress, hangcheck score for that ring is increased.
2847 * Further, acthd is inspected to see if the ring is stuck. On stuck case
2848 * we kick the ring. If we see no progress on three subsequent calls
2849 * we assume chip is wedged and try to fix it by resetting the chip.
Ben Gamarif65d9422009-09-14 17:48:44 -04002850 */
Damien Lespiaua658b5d2013-08-08 22:28:56 +01002851static void i915_hangcheck_elapsed(unsigned long data)
Ben Gamarif65d9422009-09-14 17:48:44 -04002852{
2853 struct drm_device *dev = (struct drm_device *)data;
Jani Nikula2d1013d2014-03-31 14:27:17 +03002854 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002855 struct intel_ring_buffer *ring;
Chris Wilsonb4519512012-05-11 14:29:30 +01002856 int i;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002857 int busy_count = 0, rings_hung = 0;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002858 bool stuck[I915_NUM_RINGS] = { 0 };
2859#define BUSY 1
2860#define KICK 5
2861#define HUNG 20
Chris Wilson893eead2010-10-27 14:44:35 +01002862
Jani Nikulad330a952014-01-21 11:24:25 +02002863 if (!i915.enable_hangcheck)
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07002864 return;
2865
Chris Wilsonb4519512012-05-11 14:29:30 +01002866 for_each_ring(ring, dev_priv, i) {
Chris Wilson50877442014-03-21 12:41:53 +00002867 u64 acthd;
2868 u32 seqno;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002869 bool busy = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01002870
Chris Wilson6274f212013-06-10 11:20:21 +01002871 semaphore_clear_deadlocks(dev_priv);
2872
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002873 seqno = ring->get_seqno(ring, false);
2874 acthd = intel_ring_get_active_head(ring);
Chris Wilsond1e61e72012-04-10 17:00:41 +01002875
Chris Wilson9107e9d2013-06-10 11:20:20 +01002876 if (ring->hangcheck.seqno == seqno) {
2877 if (ring_idle(ring, seqno)) {
Mika Kuoppalada661462013-09-06 16:03:28 +03002878 ring->hangcheck.action = HANGCHECK_IDLE;
2879
Chris Wilson9107e9d2013-06-10 11:20:20 +01002880 if (waitqueue_active(&ring->irq_queue)) {
2881 /* Issue a wake-up to catch stuck h/w. */
Chris Wilson094f9a52013-09-25 17:34:55 +01002882 if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
Daniel Vetterf4adcd22013-10-28 09:24:13 +01002883 if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
2884 DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
2885 ring->name);
2886 else
2887 DRM_INFO("Fake missed irq on %s\n",
2888 ring->name);
Chris Wilson094f9a52013-09-25 17:34:55 +01002889 wake_up_all(&ring->irq_queue);
2890 }
2891 /* Safeguard against driver failure */
2892 ring->hangcheck.score += BUSY;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002893 } else
2894 busy = false;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002895 } else {
Chris Wilson6274f212013-06-10 11:20:21 +01002896 /* We always increment the hangcheck score
2897 * if the ring is busy and still processing
2898 * the same request, so that no single request
2899 * can run indefinitely (such as a chain of
2900 * batches). The only time we do not increment
2901 * the hangcheck score on this ring, if this
2902 * ring is in a legitimate wait for another
2903 * ring. In that case the waiting ring is a
2904 * victim and we want to be sure we catch the
2905 * right culprit. Then every time we do kick
2906 * the ring, add a small increment to the
2907 * score so that we can catch a batch that is
2908 * being repeatedly kicked and so responsible
2909 * for stalling the machine.
2910 */
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03002911 ring->hangcheck.action = ring_stuck(ring,
2912 acthd);
2913
2914 switch (ring->hangcheck.action) {
Mika Kuoppalada661462013-09-06 16:03:28 +03002915 case HANGCHECK_IDLE:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002916 case HANGCHECK_WAIT:
Chris Wilson6274f212013-06-10 11:20:21 +01002917 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002918 case HANGCHECK_ACTIVE:
Jani Nikulaea04cb32013-08-11 12:44:02 +03002919 ring->hangcheck.score += BUSY;
Chris Wilson6274f212013-06-10 11:20:21 +01002920 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002921 case HANGCHECK_KICK:
Jani Nikulaea04cb32013-08-11 12:44:02 +03002922 ring->hangcheck.score += KICK;
Chris Wilson6274f212013-06-10 11:20:21 +01002923 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002924 case HANGCHECK_HUNG:
Jani Nikulaea04cb32013-08-11 12:44:02 +03002925 ring->hangcheck.score += HUNG;
Chris Wilson6274f212013-06-10 11:20:21 +01002926 stuck[i] = true;
2927 break;
2928 }
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002929 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002930 } else {
Mika Kuoppalada661462013-09-06 16:03:28 +03002931 ring->hangcheck.action = HANGCHECK_ACTIVE;
2932
Chris Wilson9107e9d2013-06-10 11:20:20 +01002933 /* Gradually reduce the count so that we catch DoS
2934 * attempts across multiple batches.
2935 */
2936 if (ring->hangcheck.score > 0)
2937 ring->hangcheck.score--;
Chris Wilsond1e61e72012-04-10 17:00:41 +01002938 }
2939
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002940 ring->hangcheck.seqno = seqno;
2941 ring->hangcheck.acthd = acthd;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002942 busy_count += busy;
Chris Wilson893eead2010-10-27 14:44:35 +01002943 }
Eric Anholtb9201c12010-01-08 14:25:16 -08002944
Mika Kuoppala92cab732013-05-24 17:16:07 +03002945 for_each_ring(ring, dev_priv, i) {
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002946 if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
Daniel Vetterb8d88d12013-08-28 10:57:59 +02002947 DRM_INFO("%s on %s\n",
2948 stuck[i] ? "stuck" : "no progress",
2949 ring->name);
Chris Wilsona43adf02013-06-10 11:20:22 +01002950 rings_hung++;
Mika Kuoppala92cab732013-05-24 17:16:07 +03002951 }
2952 }
2953
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002954 if (rings_hung)
Mika Kuoppala58174462014-02-25 17:11:26 +02002955 return i915_handle_error(dev, true, "Ring hung");
Ben Gamarif65d9422009-09-14 17:48:44 -04002956
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002957 if (busy_count)
2958 /* Reset timer case chip hangs without another request
2959 * being added */
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03002960 i915_queue_hangcheck(dev);
2961}
2962
2963void i915_queue_hangcheck(struct drm_device *dev)
2964{
2965 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulad330a952014-01-21 11:24:25 +02002966 if (!i915.enable_hangcheck)
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03002967 return;
2968
2969 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2970 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
Ben Gamarif65d9422009-09-14 17:48:44 -04002971}
2972
Paulo Zanoni1c69eb42014-04-01 15:37:23 -03002973static void ibx_irq_reset(struct drm_device *dev)
Paulo Zanoni91738a92013-06-05 14:21:51 -03002974{
2975 struct drm_i915_private *dev_priv = dev->dev_private;
2976
2977 if (HAS_PCH_NOP(dev))
2978 return;
2979
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03002980 GEN5_IRQ_RESET(SDE);
Paulo Zanoni105b1222014-04-01 15:37:17 -03002981
2982 if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2983 I915_WRITE(SERR_INT, 0xffffffff);
Paulo Zanoni622364b2014-04-01 15:37:22 -03002984}
Paulo Zanoni105b1222014-04-01 15:37:17 -03002985
Paulo Zanoni622364b2014-04-01 15:37:22 -03002986/*
2987 * SDEIER is also touched by the interrupt handler to work around missed PCH
2988 * interrupts. Hence we can't update it after the interrupt handler is enabled -
2989 * instead we unconditionally enable all PCH interrupt sources here, but then
2990 * only unmask them as needed with SDEIMR.
2991 *
2992 * This function needs to be called before interrupts are enabled.
2993 */
2994static void ibx_irq_pre_postinstall(struct drm_device *dev)
2995{
2996 struct drm_i915_private *dev_priv = dev->dev_private;
2997
2998 if (HAS_PCH_NOP(dev))
2999 return;
3000
3001 WARN_ON(I915_READ(SDEIER) != 0);
Paulo Zanoni91738a92013-06-05 14:21:51 -03003002 I915_WRITE(SDEIER, 0xffffffff);
3003 POSTING_READ(SDEIER);
3004}
3005
Paulo Zanoni7c4d6642014-04-01 15:37:19 -03003006static void gen5_gt_irq_reset(struct drm_device *dev)
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02003007{
3008 struct drm_i915_private *dev_priv = dev->dev_private;
3009
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003010 GEN5_IRQ_RESET(GT);
Paulo Zanonia9d356a2014-04-01 15:37:09 -03003011 if (INTEL_INFO(dev)->gen >= 6)
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003012 GEN5_IRQ_RESET(GEN6_PM);
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02003013}
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015/* drm_dma.h hooks
3016*/
Paulo Zanonibe30b292014-04-01 15:37:25 -03003017static void ironlake_irq_reset(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003018{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003019 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003020
Paulo Zanoni0c841212014-04-01 15:37:27 -03003021 I915_WRITE(HWSTAM, 0xffffffff);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01003022
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003023 GEN5_IRQ_RESET(DE);
Paulo Zanonic6d954c2014-04-01 15:37:18 -03003024 if (IS_GEN7(dev))
3025 I915_WRITE(GEN7_ERR_INT, 0xffffffff);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003026
Paulo Zanoni7c4d6642014-04-01 15:37:19 -03003027 gen5_gt_irq_reset(dev);
Zhenyu Wangc6501562009-11-03 18:57:21 +00003028
Paulo Zanoni1c69eb42014-04-01 15:37:23 -03003029 ibx_irq_reset(dev);
Ben Widawsky7d991632013-05-28 19:22:25 -07003030}
3031
Paulo Zanonibe30b292014-04-01 15:37:25 -03003032static void ironlake_irq_preinstall(struct drm_device *dev)
3033{
Paulo Zanonibe30b292014-04-01 15:37:25 -03003034 ironlake_irq_reset(dev);
Ben Widawsky7d991632013-05-28 19:22:25 -07003035}
3036
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003037static void valleyview_irq_preinstall(struct drm_device *dev)
3038{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003039 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003040 int pipe;
3041
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003042 /* VLV magic */
3043 I915_WRITE(VLV_IMR, 0);
3044 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
3045 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
3046 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
3047
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003048 /* and GT */
3049 I915_WRITE(GTIIR, I915_READ(GTIIR));
3050 I915_WRITE(GTIIR, I915_READ(GTIIR));
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02003051
Paulo Zanoni7c4d6642014-04-01 15:37:19 -03003052 gen5_gt_irq_reset(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003053
3054 I915_WRITE(DPINVGTT, 0xff);
3055
3056 I915_WRITE(PORT_HOTPLUG_EN, 0);
3057 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3058 for_each_pipe(pipe)
3059 I915_WRITE(PIPESTAT(pipe), 0xffff);
3060 I915_WRITE(VLV_IIR, 0xffffffff);
3061 I915_WRITE(VLV_IMR, 0xffffffff);
3062 I915_WRITE(VLV_IER, 0x0);
3063 POSTING_READ(VLV_IER);
3064}
3065
Paulo Zanoni823f6b32014-04-01 15:37:26 -03003066static void gen8_irq_reset(struct drm_device *dev)
Ben Widawskyabd58f02013-11-02 21:07:09 -07003067{
3068 struct drm_i915_private *dev_priv = dev->dev_private;
3069 int pipe;
3070
Ben Widawskyabd58f02013-11-02 21:07:09 -07003071 I915_WRITE(GEN8_MASTER_IRQ, 0);
3072 POSTING_READ(GEN8_MASTER_IRQ);
3073
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003074 GEN8_IRQ_RESET_NDX(GT, 0);
3075 GEN8_IRQ_RESET_NDX(GT, 1);
3076 GEN8_IRQ_RESET_NDX(GT, 2);
3077 GEN8_IRQ_RESET_NDX(GT, 3);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003078
Paulo Zanoni823f6b32014-04-01 15:37:26 -03003079 for_each_pipe(pipe)
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003080 GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003081
Paulo Zanonif86f3fb2014-04-01 15:37:14 -03003082 GEN5_IRQ_RESET(GEN8_DE_PORT_);
3083 GEN5_IRQ_RESET(GEN8_DE_MISC_);
3084 GEN5_IRQ_RESET(GEN8_PCU_);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003085
Paulo Zanoni1c69eb42014-04-01 15:37:23 -03003086 ibx_irq_reset(dev);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003087}
Ben Widawskyabd58f02013-11-02 21:07:09 -07003088
Paulo Zanoni823f6b32014-04-01 15:37:26 -03003089static void gen8_irq_preinstall(struct drm_device *dev)
3090{
3091 gen8_irq_reset(dev);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003092}
3093
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003094static void cherryview_irq_preinstall(struct drm_device *dev)
3095{
3096 struct drm_i915_private *dev_priv = dev->dev_private;
3097 int pipe;
3098
3099 I915_WRITE(GEN8_MASTER_IRQ, 0);
3100 POSTING_READ(GEN8_MASTER_IRQ);
3101
3102 GEN8_IRQ_RESET_NDX(GT, 0);
3103 GEN8_IRQ_RESET_NDX(GT, 1);
3104 GEN8_IRQ_RESET_NDX(GT, 2);
3105 GEN8_IRQ_RESET_NDX(GT, 3);
3106
3107 GEN5_IRQ_RESET(GEN8_PCU_);
3108
3109 POSTING_READ(GEN8_PCU_IIR);
3110
3111 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
3112
3113 I915_WRITE(PORT_HOTPLUG_EN, 0);
3114 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3115
3116 for_each_pipe(pipe)
3117 I915_WRITE(PIPESTAT(pipe), 0xffff);
3118
3119 I915_WRITE(VLV_IMR, 0xffffffff);
3120 I915_WRITE(VLV_IER, 0x0);
3121 I915_WRITE(VLV_IIR, 0xffffffff);
3122 POSTING_READ(VLV_IIR);
3123}
3124
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003125static void ibx_hpd_irq_setup(struct drm_device *dev)
Keith Packard7fe0b972011-09-19 13:31:02 -07003126{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003127 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003128 struct drm_mode_config *mode_config = &dev->mode_config;
3129 struct intel_encoder *intel_encoder;
Daniel Vetterfee884e2013-07-04 23:35:21 +02003130 u32 hotplug_irqs, hotplug, enabled_irqs = 0;
Keith Packard7fe0b972011-09-19 13:31:02 -07003131
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003132 if (HAS_PCH_IBX(dev)) {
Daniel Vetterfee884e2013-07-04 23:35:21 +02003133 hotplug_irqs = SDE_HOTPLUG_MASK;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003134 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
Egbert Eichcd569ae2013-04-16 13:36:57 +02003135 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02003136 enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003137 } else {
Daniel Vetterfee884e2013-07-04 23:35:21 +02003138 hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003139 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
Egbert Eichcd569ae2013-04-16 13:36:57 +02003140 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02003141 enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003142 }
3143
Daniel Vetterfee884e2013-07-04 23:35:21 +02003144 ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003145
3146 /*
3147 * Enable digital hotplug on the PCH, and configure the DP short pulse
3148 * duration to 2ms (which is the minimum in the Display Port spec)
3149 *
3150 * This register is the same on all known PCH chips.
3151 */
Keith Packard7fe0b972011-09-19 13:31:02 -07003152 hotplug = I915_READ(PCH_PORT_HOTPLUG);
3153 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
3154 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
3155 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
3156 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
3157 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3158}
3159
Paulo Zanonid46da432013-02-08 17:35:15 -02003160static void ibx_irq_postinstall(struct drm_device *dev)
3161{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003162 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003163 u32 mask;
Paulo Zanonid46da432013-02-08 17:35:15 -02003164
Daniel Vetter692a04c2013-05-29 21:43:05 +02003165 if (HAS_PCH_NOP(dev))
3166 return;
3167
Paulo Zanoni105b1222014-04-01 15:37:17 -03003168 if (HAS_PCH_IBX(dev))
Daniel Vetter5c673b62014-03-07 20:34:46 +01003169 mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
Paulo Zanoni105b1222014-04-01 15:37:17 -03003170 else
Daniel Vetter5c673b62014-03-07 20:34:46 +01003171 mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
Paulo Zanoni86642812013-04-12 17:57:57 -03003172
Paulo Zanoni337ba012014-04-01 15:37:16 -03003173 GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
Paulo Zanonid46da432013-02-08 17:35:15 -02003174 I915_WRITE(SDEIMR, ~mask);
Paulo Zanonid46da432013-02-08 17:35:15 -02003175}
3176
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003177static void gen5_gt_irq_postinstall(struct drm_device *dev)
3178{
3179 struct drm_i915_private *dev_priv = dev->dev_private;
3180 u32 pm_irqs, gt_irqs;
3181
3182 pm_irqs = gt_irqs = 0;
3183
3184 dev_priv->gt_irq_mask = ~0;
Ben Widawsky040d2ba2013-09-19 11:01:40 -07003185 if (HAS_L3_DPF(dev)) {
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003186 /* L3 parity interrupt is always unmasked. */
Ben Widawsky35a85ac2013-09-19 11:13:41 -07003187 dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
3188 gt_irqs |= GT_PARITY_ERROR(dev);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003189 }
3190
3191 gt_irqs |= GT_RENDER_USER_INTERRUPT;
3192 if (IS_GEN5(dev)) {
3193 gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
3194 ILK_BSD_USER_INTERRUPT;
3195 } else {
3196 gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
3197 }
3198
Paulo Zanoni35079892014-04-01 15:37:15 -03003199 GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003200
3201 if (INTEL_INFO(dev)->gen >= 6) {
Deepak Sa6706b42014-03-15 20:23:22 +05303202 pm_irqs |= dev_priv->pm_rps_events;
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003203
3204 if (HAS_VEBOX(dev))
3205 pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3206
Paulo Zanoni605cd252013-08-06 18:57:15 -03003207 dev_priv->pm_irq_mask = 0xffffffff;
Paulo Zanoni35079892014-04-01 15:37:15 -03003208 GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003209 }
3210}
3211
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003212static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003213{
Daniel Vetter4bc9d432013-06-27 13:44:58 +02003214 unsigned long irqflags;
Jani Nikula2d1013d2014-03-31 14:27:17 +03003215 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003216 u32 display_mask, extra_mask;
3217
3218 if (INTEL_INFO(dev)->gen >= 7) {
3219 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3220 DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
3221 DE_PLANEB_FLIP_DONE_IVB |
Daniel Vetter5c673b62014-03-07 20:34:46 +01003222 DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003223 extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
Daniel Vetter5c673b62014-03-07 20:34:46 +01003224 DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003225 } else {
3226 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3227 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
Daniel Vetter5b3a8562013-10-16 22:55:48 +02003228 DE_AUX_CHANNEL_A |
Daniel Vetter5b3a8562013-10-16 22:55:48 +02003229 DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
3230 DE_POISON);
Daniel Vetter5c673b62014-03-07 20:34:46 +01003231 extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3232 DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03003233 }
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003234
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003235 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003236
Paulo Zanoni0c841212014-04-01 15:37:27 -03003237 I915_WRITE(HWSTAM, 0xeffe);
3238
Paulo Zanoni622364b2014-04-01 15:37:22 -03003239 ibx_irq_pre_postinstall(dev);
3240
Paulo Zanoni35079892014-04-01 15:37:15 -03003241 GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003242
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003243 gen5_gt_irq_postinstall(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003244
Paulo Zanonid46da432013-02-08 17:35:15 -02003245 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07003246
Jesse Barnesf97108d2010-01-29 11:27:07 -08003247 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter6005ce42013-06-27 13:44:59 +02003248 /* Enable PCU event interrupts
3249 *
3250 * spinlocking not required here for correctness since interrupt
Daniel Vetter4bc9d432013-06-27 13:44:58 +02003251 * setup is guaranteed to run in single-threaded context. But we
3252 * need it to make the assert_spin_locked happy. */
3253 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf97108d2010-01-29 11:27:07 -08003254 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
Daniel Vetter4bc9d432013-06-27 13:44:58 +02003255 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnesf97108d2010-01-29 11:27:07 -08003256 }
3257
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003258 return 0;
3259}
3260
Imre Deakf8b79e52014-03-04 19:23:07 +02003261static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3262{
3263 u32 pipestat_mask;
3264 u32 iir_mask;
3265
3266 pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3267 PIPE_FIFO_UNDERRUN_STATUS;
3268
3269 I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3270 I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3271 POSTING_READ(PIPESTAT(PIPE_A));
3272
3273 pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3274 PIPE_CRC_DONE_INTERRUPT_STATUS;
3275
3276 i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3277 PIPE_GMBUS_INTERRUPT_STATUS);
3278 i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3279
3280 iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3281 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3282 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3283 dev_priv->irq_mask &= ~iir_mask;
3284
3285 I915_WRITE(VLV_IIR, iir_mask);
3286 I915_WRITE(VLV_IIR, iir_mask);
3287 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3288 I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3289 POSTING_READ(VLV_IER);
3290}
3291
3292static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3293{
3294 u32 pipestat_mask;
3295 u32 iir_mask;
3296
3297 iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3298 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
Imre Deak6c7fba02014-03-10 19:44:48 +02003299 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
Imre Deakf8b79e52014-03-04 19:23:07 +02003300
3301 dev_priv->irq_mask |= iir_mask;
3302 I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3303 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3304 I915_WRITE(VLV_IIR, iir_mask);
3305 I915_WRITE(VLV_IIR, iir_mask);
3306 POSTING_READ(VLV_IIR);
3307
3308 pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3309 PIPE_CRC_DONE_INTERRUPT_STATUS;
3310
3311 i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3312 PIPE_GMBUS_INTERRUPT_STATUS);
3313 i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3314
3315 pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3316 PIPE_FIFO_UNDERRUN_STATUS;
3317 I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3318 I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3319 POSTING_READ(PIPESTAT(PIPE_A));
3320}
3321
3322void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3323{
3324 assert_spin_locked(&dev_priv->irq_lock);
3325
3326 if (dev_priv->display_irqs_enabled)
3327 return;
3328
3329 dev_priv->display_irqs_enabled = true;
3330
3331 if (dev_priv->dev->irq_enabled)
3332 valleyview_display_irqs_install(dev_priv);
3333}
3334
3335void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3336{
3337 assert_spin_locked(&dev_priv->irq_lock);
3338
3339 if (!dev_priv->display_irqs_enabled)
3340 return;
3341
3342 dev_priv->display_irqs_enabled = false;
3343
3344 if (dev_priv->dev->irq_enabled)
3345 valleyview_display_irqs_uninstall(dev_priv);
3346}
3347
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003348static int valleyview_irq_postinstall(struct drm_device *dev)
3349{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003350 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterb79480b2013-06-27 17:52:10 +02003351 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003352
Imre Deakf8b79e52014-03-04 19:23:07 +02003353 dev_priv->irq_mask = ~0;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003354
Daniel Vetter20afbda2012-12-11 14:05:07 +01003355 I915_WRITE(PORT_HOTPLUG_EN, 0);
3356 POSTING_READ(PORT_HOTPLUG_EN);
3357
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003358 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
Imre Deakf8b79e52014-03-04 19:23:07 +02003359 I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003360 I915_WRITE(VLV_IIR, 0xffffffff);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003361 POSTING_READ(VLV_IER);
3362
Daniel Vetterb79480b2013-06-27 17:52:10 +02003363 /* Interrupt setup is already guaranteed to be single-threaded, this is
3364 * just to make the assert_spin_locked check happy. */
3365 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Imre Deakf8b79e52014-03-04 19:23:07 +02003366 if (dev_priv->display_irqs_enabled)
3367 valleyview_display_irqs_install(dev_priv);
Daniel Vetterb79480b2013-06-27 17:52:10 +02003368 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07003369
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003370 I915_WRITE(VLV_IIR, 0xffffffff);
3371 I915_WRITE(VLV_IIR, 0xffffffff);
3372
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02003373 gen5_gt_irq_postinstall(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003374
3375 /* ack & enable invalid PTE error interrupts */
3376#if 0 /* FIXME: add support to irq handler for checking these bits */
3377 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
3378 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
3379#endif
3380
3381 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003382
3383 return 0;
3384}
3385
Ben Widawskyabd58f02013-11-02 21:07:09 -07003386static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3387{
3388 int i;
3389
3390 /* These are interrupts we'll toggle with the ring mask register */
3391 uint32_t gt_interrupts[] = {
3392 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3393 GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
3394 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3395 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3396 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3397 0,
3398 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3399 };
3400
Paulo Zanoni337ba012014-04-01 15:37:16 -03003401 for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++)
Paulo Zanoni35079892014-04-01 15:37:15 -03003402 GEN8_IRQ_INIT_NDX(GT, i, ~gt_interrupts[i], gt_interrupts[i]);
Ben Widawsky09610212014-05-15 20:58:08 +03003403
3404 dev_priv->pm_irq_mask = 0xffffffff;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003405}
3406
3407static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3408{
3409 struct drm_device *dev = dev_priv->dev;
Damien Lespiaud0e1f1c2014-04-08 01:22:44 +01003410 uint32_t de_pipe_masked = GEN8_PIPE_PRIMARY_FLIP_DONE |
Daniel Vetter13b3a0a2013-11-07 15:31:52 +01003411 GEN8_PIPE_CDCLK_CRC_DONE |
Daniel Vetter13b3a0a2013-11-07 15:31:52 +01003412 GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
Daniel Vetter5c673b62014-03-07 20:34:46 +01003413 uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3414 GEN8_PIPE_FIFO_UNDERRUN;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003415 int pipe;
Daniel Vetter13b3a0a2013-11-07 15:31:52 +01003416 dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
3417 dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
3418 dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003419
Paulo Zanoni337ba012014-04-01 15:37:16 -03003420 for_each_pipe(pipe)
Paulo Zanoni35079892014-04-01 15:37:15 -03003421 GEN8_IRQ_INIT_NDX(DE_PIPE, pipe, dev_priv->de_irq_mask[pipe],
3422 de_pipe_enables);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003423
Paulo Zanoni35079892014-04-01 15:37:15 -03003424 GEN5_IRQ_INIT(GEN8_DE_PORT_, ~GEN8_AUX_CHANNEL_A, GEN8_AUX_CHANNEL_A);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003425}
3426
3427static int gen8_irq_postinstall(struct drm_device *dev)
3428{
3429 struct drm_i915_private *dev_priv = dev->dev_private;
3430
Paulo Zanoni622364b2014-04-01 15:37:22 -03003431 ibx_irq_pre_postinstall(dev);
3432
Ben Widawskyabd58f02013-11-02 21:07:09 -07003433 gen8_gt_irq_postinstall(dev_priv);
3434 gen8_de_irq_postinstall(dev_priv);
3435
3436 ibx_irq_postinstall(dev);
3437
3438 I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3439 POSTING_READ(GEN8_MASTER_IRQ);
3440
3441 return 0;
3442}
3443
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003444static int cherryview_irq_postinstall(struct drm_device *dev)
3445{
3446 struct drm_i915_private *dev_priv = dev->dev_private;
3447 u32 enable_mask = I915_DISPLAY_PORT_INTERRUPT |
3448 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003449 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
Ville Syrjälä3278f672014-04-09 13:28:49 +03003450 I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3451 u32 pipestat_enable = PLANE_FLIP_DONE_INT_STATUS_VLV |
3452 PIPE_CRC_DONE_INTERRUPT_STATUS;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003453 unsigned long irqflags;
3454 int pipe;
3455
3456 /*
3457 * Leave vblank interrupts masked initially. enable/disable will
3458 * toggle them based on usage.
3459 */
Ville Syrjälä3278f672014-04-09 13:28:49 +03003460 dev_priv->irq_mask = ~enable_mask;
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003461
3462 for_each_pipe(pipe)
3463 I915_WRITE(PIPESTAT(pipe), 0xffff);
3464
3465 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Ville Syrjälä3278f672014-04-09 13:28:49 +03003466 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003467 for_each_pipe(pipe)
3468 i915_enable_pipestat(dev_priv, pipe, pipestat_enable);
3469 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3470
3471 I915_WRITE(VLV_IIR, 0xffffffff);
3472 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3473 I915_WRITE(VLV_IER, enable_mask);
3474
3475 gen8_gt_irq_postinstall(dev_priv);
3476
3477 I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
3478 POSTING_READ(GEN8_MASTER_IRQ);
3479
3480 return 0;
3481}
3482
Ben Widawskyabd58f02013-11-02 21:07:09 -07003483static void gen8_irq_uninstall(struct drm_device *dev)
3484{
3485 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003486
3487 if (!dev_priv)
3488 return;
3489
Paulo Zanonid4eb6b12014-04-01 15:37:24 -03003490 intel_hpd_irq_uninstall(dev_priv);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003491
Paulo Zanoni823f6b32014-04-01 15:37:26 -03003492 gen8_irq_reset(dev);
Ben Widawskyabd58f02013-11-02 21:07:09 -07003493}
3494
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003495static void valleyview_irq_uninstall(struct drm_device *dev)
3496{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003497 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deakf8b79e52014-03-04 19:23:07 +02003498 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003499 int pipe;
3500
3501 if (!dev_priv)
3502 return;
3503
Imre Deak843d0e72014-04-14 20:24:23 +03003504 I915_WRITE(VLV_MASTER_IER, 0);
3505
Ville Syrjälä3ca1cce2014-01-17 13:43:51 +02003506 intel_hpd_irq_uninstall(dev_priv);
Egbert Eichac4c16c2013-04-16 13:36:58 +02003507
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003508 for_each_pipe(pipe)
3509 I915_WRITE(PIPESTAT(pipe), 0xffff);
3510
3511 I915_WRITE(HWSTAM, 0xffffffff);
3512 I915_WRITE(PORT_HOTPLUG_EN, 0);
3513 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Imre Deakf8b79e52014-03-04 19:23:07 +02003514
3515 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3516 if (dev_priv->display_irqs_enabled)
3517 valleyview_display_irqs_uninstall(dev_priv);
3518 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3519
3520 dev_priv->irq_mask = 0;
3521
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003522 I915_WRITE(VLV_IIR, 0xffffffff);
3523 I915_WRITE(VLV_IMR, 0xffffffff);
3524 I915_WRITE(VLV_IER, 0x0);
3525 POSTING_READ(VLV_IER);
3526}
3527
Ville Syrjälä43f328d2014-04-09 20:40:52 +03003528static void cherryview_irq_uninstall(struct drm_device *dev)
3529{
3530 struct drm_i915_private *dev_priv = dev->dev_private;
3531 int pipe;
3532
3533 if (!dev_priv)
3534 return;
3535
3536 I915_WRITE(GEN8_MASTER_IRQ, 0);
3537 POSTING_READ(GEN8_MASTER_IRQ);
3538
3539#define GEN8_IRQ_FINI_NDX(type, which) \
3540do { \
3541 I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
3542 I915_WRITE(GEN8_##type##_IER(which), 0); \
3543 I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
3544 POSTING_READ(GEN8_##type##_IIR(which)); \
3545 I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
3546} while (0)
3547
3548#define GEN8_IRQ_FINI(type) \
3549do { \
3550 I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
3551 I915_WRITE(GEN8_##type##_IER, 0); \
3552 I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
3553 POSTING_READ(GEN8_##type##_IIR); \
3554 I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
3555} while (0)
3556
3557 GEN8_IRQ_FINI_NDX(GT, 0);
3558 GEN8_IRQ_FINI_NDX(GT, 1);
3559 GEN8_IRQ_FINI_NDX(GT, 2);
3560 GEN8_IRQ_FINI_NDX(GT, 3);
3561
3562 GEN8_IRQ_FINI(PCU);
3563
3564#undef GEN8_IRQ_FINI
3565#undef GEN8_IRQ_FINI_NDX
3566
3567 I915_WRITE(PORT_HOTPLUG_EN, 0);
3568 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3569
3570 for_each_pipe(pipe)
3571 I915_WRITE(PIPESTAT(pipe), 0xffff);
3572
3573 I915_WRITE(VLV_IMR, 0xffffffff);
3574 I915_WRITE(VLV_IER, 0x0);
3575 I915_WRITE(VLV_IIR, 0xffffffff);
3576 POSTING_READ(VLV_IIR);
3577}
3578
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003579static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003580{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003581 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07003582
3583 if (!dev_priv)
3584 return;
3585
Ville Syrjälä3ca1cce2014-01-17 13:43:51 +02003586 intel_hpd_irq_uninstall(dev_priv);
Egbert Eichac4c16c2013-04-16 13:36:58 +02003587
Paulo Zanonibe30b292014-04-01 15:37:25 -03003588 ironlake_irq_reset(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08003589}
3590
Chris Wilsonc2798b12012-04-22 21:13:57 +01003591static void i8xx_irq_preinstall(struct drm_device * dev)
3592{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003593 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003594 int pipe;
3595
Chris Wilsonc2798b12012-04-22 21:13:57 +01003596 for_each_pipe(pipe)
3597 I915_WRITE(PIPESTAT(pipe), 0);
3598 I915_WRITE16(IMR, 0xffff);
3599 I915_WRITE16(IER, 0x0);
3600 POSTING_READ16(IER);
3601}
3602
3603static int i8xx_irq_postinstall(struct drm_device *dev)
3604{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003605 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter379ef822013-10-16 22:55:56 +02003606 unsigned long irqflags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003607
Chris Wilsonc2798b12012-04-22 21:13:57 +01003608 I915_WRITE16(EMR,
3609 ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3610
3611 /* Unmask the interrupts that we always want on. */
3612 dev_priv->irq_mask =
3613 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3614 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3615 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3616 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3617 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3618 I915_WRITE16(IMR, dev_priv->irq_mask);
3619
3620 I915_WRITE16(IER,
3621 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3622 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3623 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3624 I915_USER_INTERRUPT);
3625 POSTING_READ16(IER);
3626
Daniel Vetter379ef822013-10-16 22:55:56 +02003627 /* Interrupt setup is already guaranteed to be single-threaded, this is
3628 * just to make the assert_spin_locked check happy. */
3629 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Imre Deak755e9012014-02-10 18:42:47 +02003630 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3631 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
Daniel Vetter379ef822013-10-16 22:55:56 +02003632 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3633
Chris Wilsonc2798b12012-04-22 21:13:57 +01003634 return 0;
3635}
3636
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003637/*
3638 * Returns true when a page flip has completed.
3639 */
3640static bool i8xx_handle_vblank(struct drm_device *dev,
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003641 int plane, int pipe, u32 iir)
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003642{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003643 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003644 u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003645
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03003646 if (!intel_pipe_handle_vblank(dev, pipe))
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003647 return false;
3648
3649 if ((iir & flip_pending) == 0)
3650 return false;
3651
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003652 intel_prepare_page_flip(dev, plane);
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003653
3654 /* We detect FlipDone by looking for the change in PendingFlip from '1'
3655 * to '0' on the following vblank, i.e. IIR has the Pendingflip
3656 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
3657 * the flip is completed (no longer pending). Since this doesn't raise
3658 * an interrupt per se, we watch for the change at vblank.
3659 */
3660 if (I915_READ16(ISR) & flip_pending)
3661 return false;
3662
3663 intel_finish_page_flip(dev, pipe);
3664
3665 return true;
3666}
3667
Daniel Vetterff1f5252012-10-02 15:10:55 +02003668static irqreturn_t i8xx_irq_handler(int irq, void *arg)
Chris Wilsonc2798b12012-04-22 21:13:57 +01003669{
Daniel Vetter45a83f82014-05-12 19:17:55 +02003670 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03003671 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003672 u16 iir, new_iir;
3673 u32 pipe_stats[2];
3674 unsigned long irqflags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003675 int pipe;
3676 u16 flip_mask =
3677 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3678 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3679
Chris Wilsonc2798b12012-04-22 21:13:57 +01003680 iir = I915_READ16(IIR);
3681 if (iir == 0)
3682 return IRQ_NONE;
3683
3684 while (iir & ~flip_mask) {
3685 /* Can't rely on pipestat interrupt bit in iir as it might
3686 * have been cleared after the pipestat interrupt was received.
3687 * It doesn't set the bit in iir again, but it still produces
3688 * interrupts (for non-MSI).
3689 */
3690 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3691 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Mika Kuoppala58174462014-02-25 17:11:26 +02003692 i915_handle_error(dev, false,
3693 "Command parser error, iir 0x%08x",
3694 iir);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003695
3696 for_each_pipe(pipe) {
3697 int reg = PIPESTAT(pipe);
3698 pipe_stats[pipe] = I915_READ(reg);
3699
3700 /*
3701 * Clear the PIPE*STAT regs before the IIR
3702 */
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02003703 if (pipe_stats[pipe] & 0x8000ffff)
Chris Wilsonc2798b12012-04-22 21:13:57 +01003704 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003705 }
3706 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3707
3708 I915_WRITE16(IIR, iir & ~flip_mask);
3709 new_iir = I915_READ16(IIR); /* Flush posted writes */
3710
Daniel Vetterd05c6172012-04-26 23:28:09 +02003711 i915_update_dri1_breadcrumb(dev);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003712
3713 if (iir & I915_USER_INTERRUPT)
3714 notify_ring(dev, &dev_priv->ring[RCS]);
3715
Daniel Vetter4356d582013-10-16 22:55:55 +02003716 for_each_pipe(pipe) {
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003717 int plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01003718 if (HAS_FBC(dev))
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003719 plane = !plane;
3720
Daniel Vetter4356d582013-10-16 22:55:55 +02003721 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +02003722 i8xx_handle_vblank(dev, plane, pipe, iir))
3723 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003724
Daniel Vetter4356d582013-10-16 22:55:55 +02003725 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
Daniel Vetter277de952013-10-18 16:37:07 +02003726 i9xx_pipe_crc_irq_handler(dev, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02003727
3728 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
3729 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02003730 DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
Daniel Vetter4356d582013-10-16 22:55:55 +02003731 }
Chris Wilsonc2798b12012-04-22 21:13:57 +01003732
3733 iir = new_iir;
3734 }
3735
3736 return IRQ_HANDLED;
3737}
3738
3739static void i8xx_irq_uninstall(struct drm_device * dev)
3740{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003741 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003742 int pipe;
3743
Chris Wilsonc2798b12012-04-22 21:13:57 +01003744 for_each_pipe(pipe) {
3745 /* Clear enable bits; then clear status bits */
3746 I915_WRITE(PIPESTAT(pipe), 0);
3747 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3748 }
3749 I915_WRITE16(IMR, 0xffff);
3750 I915_WRITE16(IER, 0x0);
3751 I915_WRITE16(IIR, I915_READ16(IIR));
3752}
3753
Chris Wilsona266c7d2012-04-24 22:59:44 +01003754static void i915_irq_preinstall(struct drm_device * dev)
3755{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003756 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003757 int pipe;
3758
Chris Wilsona266c7d2012-04-24 22:59:44 +01003759 if (I915_HAS_HOTPLUG(dev)) {
3760 I915_WRITE(PORT_HOTPLUG_EN, 0);
3761 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3762 }
3763
Chris Wilson00d98eb2012-04-24 22:59:48 +01003764 I915_WRITE16(HWSTAM, 0xeffe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003765 for_each_pipe(pipe)
3766 I915_WRITE(PIPESTAT(pipe), 0);
3767 I915_WRITE(IMR, 0xffffffff);
3768 I915_WRITE(IER, 0x0);
3769 POSTING_READ(IER);
3770}
3771
3772static int i915_irq_postinstall(struct drm_device *dev)
3773{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003774 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson38bde182012-04-24 22:59:50 +01003775 u32 enable_mask;
Daniel Vetter379ef822013-10-16 22:55:56 +02003776 unsigned long irqflags;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003777
Chris Wilson38bde182012-04-24 22:59:50 +01003778 I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3779
3780 /* Unmask the interrupts that we always want on. */
3781 dev_priv->irq_mask =
3782 ~(I915_ASLE_INTERRUPT |
3783 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3784 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3785 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3786 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3787 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3788
3789 enable_mask =
3790 I915_ASLE_INTERRUPT |
3791 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3792 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3793 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3794 I915_USER_INTERRUPT;
3795
Chris Wilsona266c7d2012-04-24 22:59:44 +01003796 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetter20afbda2012-12-11 14:05:07 +01003797 I915_WRITE(PORT_HOTPLUG_EN, 0);
3798 POSTING_READ(PORT_HOTPLUG_EN);
3799
Chris Wilsona266c7d2012-04-24 22:59:44 +01003800 /* Enable in IER... */
3801 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3802 /* and unmask in IMR */
3803 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3804 }
3805
Chris Wilsona266c7d2012-04-24 22:59:44 +01003806 I915_WRITE(IMR, dev_priv->irq_mask);
3807 I915_WRITE(IER, enable_mask);
3808 POSTING_READ(IER);
3809
Jani Nikulaf49e38d2013-04-29 13:02:54 +03003810 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003811
Daniel Vetter379ef822013-10-16 22:55:56 +02003812 /* Interrupt setup is already guaranteed to be single-threaded, this is
3813 * just to make the assert_spin_locked check happy. */
3814 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Imre Deak755e9012014-02-10 18:42:47 +02003815 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3816 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
Daniel Vetter379ef822013-10-16 22:55:56 +02003817 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3818
Daniel Vetter20afbda2012-12-11 14:05:07 +01003819 return 0;
3820}
3821
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003822/*
3823 * Returns true when a page flip has completed.
3824 */
3825static bool i915_handle_vblank(struct drm_device *dev,
3826 int plane, int pipe, u32 iir)
3827{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003828 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003829 u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
3830
Ville Syrjälä8d7849d2014-04-29 13:35:46 +03003831 if (!intel_pipe_handle_vblank(dev, pipe))
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003832 return false;
3833
3834 if ((iir & flip_pending) == 0)
3835 return false;
3836
3837 intel_prepare_page_flip(dev, plane);
3838
3839 /* We detect FlipDone by looking for the change in PendingFlip from '1'
3840 * to '0' on the following vblank, i.e. IIR has the Pendingflip
3841 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
3842 * the flip is completed (no longer pending). Since this doesn't raise
3843 * an interrupt per se, we watch for the change at vblank.
3844 */
3845 if (I915_READ(ISR) & flip_pending)
3846 return false;
3847
3848 intel_finish_page_flip(dev, pipe);
3849
3850 return true;
3851}
3852
Daniel Vetterff1f5252012-10-02 15:10:55 +02003853static irqreturn_t i915_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003854{
Daniel Vetter45a83f82014-05-12 19:17:55 +02003855 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03003856 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8291ee92012-04-24 22:59:47 +01003857 u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01003858 unsigned long irqflags;
Chris Wilson38bde182012-04-24 22:59:50 +01003859 u32 flip_mask =
3860 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3861 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilson38bde182012-04-24 22:59:50 +01003862 int pipe, ret = IRQ_NONE;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003863
Chris Wilsona266c7d2012-04-24 22:59:44 +01003864 iir = I915_READ(IIR);
Chris Wilson38bde182012-04-24 22:59:50 +01003865 do {
3866 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson8291ee92012-04-24 22:59:47 +01003867 bool blc_event = false;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003868
3869 /* Can't rely on pipestat interrupt bit in iir as it might
3870 * have been cleared after the pipestat interrupt was received.
3871 * It doesn't set the bit in iir again, but it still produces
3872 * interrupts (for non-MSI).
3873 */
3874 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3875 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Mika Kuoppala58174462014-02-25 17:11:26 +02003876 i915_handle_error(dev, false,
3877 "Command parser error, iir 0x%08x",
3878 iir);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003879
3880 for_each_pipe(pipe) {
3881 int reg = PIPESTAT(pipe);
3882 pipe_stats[pipe] = I915_READ(reg);
3883
Chris Wilson38bde182012-04-24 22:59:50 +01003884 /* Clear the PIPE*STAT regs before the IIR */
Chris Wilsona266c7d2012-04-24 22:59:44 +01003885 if (pipe_stats[pipe] & 0x8000ffff) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003886 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilson38bde182012-04-24 22:59:50 +01003887 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003888 }
3889 }
3890 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3891
3892 if (!irq_received)
3893 break;
3894
Chris Wilsona266c7d2012-04-24 22:59:44 +01003895 /* Consume port. Then clear IIR or we'll miss events */
Ville Syrjälä16c6c562014-04-01 10:54:36 +03003896 if (I915_HAS_HOTPLUG(dev) &&
3897 iir & I915_DISPLAY_PORT_INTERRUPT)
3898 i9xx_hpd_irq_handler(dev);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003899
Chris Wilson38bde182012-04-24 22:59:50 +01003900 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003901 new_iir = I915_READ(IIR); /* Flush posted writes */
3902
Chris Wilsona266c7d2012-04-24 22:59:44 +01003903 if (iir & I915_USER_INTERRUPT)
3904 notify_ring(dev, &dev_priv->ring[RCS]);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003905
Chris Wilsona266c7d2012-04-24 22:59:44 +01003906 for_each_pipe(pipe) {
Chris Wilson38bde182012-04-24 22:59:50 +01003907 int plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01003908 if (HAS_FBC(dev))
Chris Wilson38bde182012-04-24 22:59:50 +01003909 plane = !plane;
Ville Syrjälä5e2032d2013-02-19 15:16:38 +02003910
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003911 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
3912 i915_handle_vblank(dev, plane, pipe, iir))
3913 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003914
3915 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3916 blc_event = true;
Daniel Vetter4356d582013-10-16 22:55:55 +02003917
3918 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
Daniel Vetter277de952013-10-18 16:37:07 +02003919 i9xx_pipe_crc_irq_handler(dev, pipe);
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02003920
3921 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
3922 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02003923 DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
Chris Wilsona266c7d2012-04-24 22:59:44 +01003924 }
3925
Chris Wilsona266c7d2012-04-24 22:59:44 +01003926 if (blc_event || (iir & I915_ASLE_INTERRUPT))
3927 intel_opregion_asle_intr(dev);
3928
3929 /* With MSI, interrupts are only generated when iir
3930 * transitions from zero to nonzero. If another bit got
3931 * set while we were handling the existing iir bits, then
3932 * we would never get another interrupt.
3933 *
3934 * This is fine on non-MSI as well, as if we hit this path
3935 * we avoid exiting the interrupt handler only to generate
3936 * another one.
3937 *
3938 * Note that for MSI this could cause a stray interrupt report
3939 * if an interrupt landed in the time between writing IIR and
3940 * the posting read. This should be rare enough to never
3941 * trigger the 99% of 100,000 interrupts test for disabling
3942 * stray interrupts.
3943 */
Chris Wilson38bde182012-04-24 22:59:50 +01003944 ret = IRQ_HANDLED;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003945 iir = new_iir;
Chris Wilson38bde182012-04-24 22:59:50 +01003946 } while (iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003947
Daniel Vetterd05c6172012-04-26 23:28:09 +02003948 i915_update_dri1_breadcrumb(dev);
Chris Wilson8291ee92012-04-24 22:59:47 +01003949
Chris Wilsona266c7d2012-04-24 22:59:44 +01003950 return ret;
3951}
3952
3953static void i915_irq_uninstall(struct drm_device * dev)
3954{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003955 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003956 int pipe;
3957
Ville Syrjälä3ca1cce2014-01-17 13:43:51 +02003958 intel_hpd_irq_uninstall(dev_priv);
Egbert Eichac4c16c2013-04-16 13:36:58 +02003959
Chris Wilsona266c7d2012-04-24 22:59:44 +01003960 if (I915_HAS_HOTPLUG(dev)) {
3961 I915_WRITE(PORT_HOTPLUG_EN, 0);
3962 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3963 }
3964
Chris Wilson00d98eb2012-04-24 22:59:48 +01003965 I915_WRITE16(HWSTAM, 0xffff);
Chris Wilson55b39752012-04-24 22:59:49 +01003966 for_each_pipe(pipe) {
3967 /* Clear enable bits; then clear status bits */
Chris Wilsona266c7d2012-04-24 22:59:44 +01003968 I915_WRITE(PIPESTAT(pipe), 0);
Chris Wilson55b39752012-04-24 22:59:49 +01003969 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3970 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01003971 I915_WRITE(IMR, 0xffffffff);
3972 I915_WRITE(IER, 0x0);
3973
Chris Wilsona266c7d2012-04-24 22:59:44 +01003974 I915_WRITE(IIR, I915_READ(IIR));
3975}
3976
3977static void i965_irq_preinstall(struct drm_device * dev)
3978{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003979 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003980 int pipe;
3981
Chris Wilsonadca4732012-05-11 18:01:31 +01003982 I915_WRITE(PORT_HOTPLUG_EN, 0);
3983 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01003984
3985 I915_WRITE(HWSTAM, 0xeffe);
3986 for_each_pipe(pipe)
3987 I915_WRITE(PIPESTAT(pipe), 0);
3988 I915_WRITE(IMR, 0xffffffff);
3989 I915_WRITE(IER, 0x0);
3990 POSTING_READ(IER);
3991}
3992
3993static int i965_irq_postinstall(struct drm_device *dev)
3994{
Jani Nikula2d1013d2014-03-31 14:27:17 +03003995 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbbba0a92012-04-24 22:59:51 +01003996 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003997 u32 error_mask;
Daniel Vetterb79480b2013-06-27 17:52:10 +02003998 unsigned long irqflags;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003999
Chris Wilsona266c7d2012-04-24 22:59:44 +01004000 /* Unmask the interrupts that we always want on. */
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004001 dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
Chris Wilsonadca4732012-05-11 18:01:31 +01004002 I915_DISPLAY_PORT_INTERRUPT |
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004003 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4004 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4005 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4006 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4007 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4008
4009 enable_mask = ~dev_priv->irq_mask;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02004010 enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4011 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
Chris Wilsonbbba0a92012-04-24 22:59:51 +01004012 enable_mask |= I915_USER_INTERRUPT;
4013
4014 if (IS_G4X(dev))
4015 enable_mask |= I915_BSD_USER_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004016
Daniel Vetterb79480b2013-06-27 17:52:10 +02004017 /* Interrupt setup is already guaranteed to be single-threaded, this is
4018 * just to make the assert_spin_locked check happy. */
4019 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Imre Deak755e9012014-02-10 18:42:47 +02004020 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4021 i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4022 i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
Daniel Vetterb79480b2013-06-27 17:52:10 +02004023 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004024
Chris Wilsona266c7d2012-04-24 22:59:44 +01004025 /*
4026 * Enable some error detection, note the instruction error mask
4027 * bit is reserved, so we leave it masked.
4028 */
4029 if (IS_G4X(dev)) {
4030 error_mask = ~(GM45_ERROR_PAGE_TABLE |
4031 GM45_ERROR_MEM_PRIV |
4032 GM45_ERROR_CP_PRIV |
4033 I915_ERROR_MEMORY_REFRESH);
4034 } else {
4035 error_mask = ~(I915_ERROR_PAGE_TABLE |
4036 I915_ERROR_MEMORY_REFRESH);
4037 }
4038 I915_WRITE(EMR, error_mask);
4039
4040 I915_WRITE(IMR, dev_priv->irq_mask);
4041 I915_WRITE(IER, enable_mask);
4042 POSTING_READ(IER);
4043
Daniel Vetter20afbda2012-12-11 14:05:07 +01004044 I915_WRITE(PORT_HOTPLUG_EN, 0);
4045 POSTING_READ(PORT_HOTPLUG_EN);
4046
Jani Nikulaf49e38d2013-04-29 13:02:54 +03004047 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01004048
4049 return 0;
4050}
4051
Egbert Eichbac56d52013-02-25 12:06:51 -05004052static void i915_hpd_irq_setup(struct drm_device *dev)
Daniel Vetter20afbda2012-12-11 14:05:07 +01004053{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004054 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eiche5868a32013-02-28 04:17:12 -05004055 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +02004056 struct intel_encoder *intel_encoder;
Daniel Vetter20afbda2012-12-11 14:05:07 +01004057 u32 hotplug_en;
4058
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02004059 assert_spin_locked(&dev_priv->irq_lock);
4060
Egbert Eichbac56d52013-02-25 12:06:51 -05004061 if (I915_HAS_HOTPLUG(dev)) {
4062 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4063 hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4064 /* Note HDMI and DP share hotplug bits */
Egbert Eiche5868a32013-02-28 04:17:12 -05004065 /* enable bits are the same for all generations */
Egbert Eichcd569ae2013-04-16 13:36:57 +02004066 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
4067 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4068 hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
Egbert Eichbac56d52013-02-25 12:06:51 -05004069 /* Programming the CRT detection parameters tends
4070 to generate a spurious hotplug event about three
4071 seconds later. So just do it once.
4072 */
4073 if (IS_G4X(dev))
4074 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
Daniel Vetter85fc95b2013-03-27 15:47:11 +01004075 hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
Egbert Eichbac56d52013-02-25 12:06:51 -05004076 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004077
Egbert Eichbac56d52013-02-25 12:06:51 -05004078 /* Ignore TV since it's buggy */
4079 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4080 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01004081}
4082
Daniel Vetterff1f5252012-10-02 15:10:55 +02004083static irqreturn_t i965_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01004084{
Daniel Vetter45a83f82014-05-12 19:17:55 +02004085 struct drm_device *dev = arg;
Jani Nikula2d1013d2014-03-31 14:27:17 +03004086 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004087 u32 iir, new_iir;
4088 u32 pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01004089 unsigned long irqflags;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004090 int ret = IRQ_NONE, pipe;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02004091 u32 flip_mask =
4092 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4093 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004094
Chris Wilsona266c7d2012-04-24 22:59:44 +01004095 iir = I915_READ(IIR);
4096
Chris Wilsona266c7d2012-04-24 22:59:44 +01004097 for (;;) {
Ville Syrjälä501e01d2014-01-17 11:35:15 +02004098 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson2c8ba292012-04-24 22:59:46 +01004099 bool blc_event = false;
4100
Chris Wilsona266c7d2012-04-24 22:59:44 +01004101 /* Can't rely on pipestat interrupt bit in iir as it might
4102 * have been cleared after the pipestat interrupt was received.
4103 * It doesn't set the bit in iir again, but it still produces
4104 * interrupts (for non-MSI).
4105 */
4106 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4107 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
Mika Kuoppala58174462014-02-25 17:11:26 +02004108 i915_handle_error(dev, false,
4109 "Command parser error, iir 0x%08x",
4110 iir);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004111
4112 for_each_pipe(pipe) {
4113 int reg = PIPESTAT(pipe);
4114 pipe_stats[pipe] = I915_READ(reg);
4115
4116 /*
4117 * Clear the PIPE*STAT regs before the IIR
4118 */
4119 if (pipe_stats[pipe] & 0x8000ffff) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004120 I915_WRITE(reg, pipe_stats[pipe]);
Ville Syrjälä501e01d2014-01-17 11:35:15 +02004121 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004122 }
4123 }
4124 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4125
4126 if (!irq_received)
4127 break;
4128
4129 ret = IRQ_HANDLED;
4130
4131 /* Consume port. Then clear IIR or we'll miss events */
Ville Syrjälä16c6c562014-04-01 10:54:36 +03004132 if (iir & I915_DISPLAY_PORT_INTERRUPT)
4133 i9xx_hpd_irq_handler(dev);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004134
Ville Syrjälä21ad8332013-02-19 15:16:39 +02004135 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004136 new_iir = I915_READ(IIR); /* Flush posted writes */
4137
Chris Wilsona266c7d2012-04-24 22:59:44 +01004138 if (iir & I915_USER_INTERRUPT)
4139 notify_ring(dev, &dev_priv->ring[RCS]);
4140 if (iir & I915_BSD_USER_INTERRUPT)
4141 notify_ring(dev, &dev_priv->ring[VCS]);
4142
Chris Wilsona266c7d2012-04-24 22:59:44 +01004143 for_each_pipe(pipe) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01004144 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02004145 i915_handle_vblank(dev, pipe, pipe, iir))
4146 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004147
4148 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4149 blc_event = true;
Daniel Vetter4356d582013-10-16 22:55:55 +02004150
4151 if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
Daniel Vetter277de952013-10-18 16:37:07 +02004152 i9xx_pipe_crc_irq_handler(dev, pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01004153
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004154 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
4155 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
Ville Syrjäläfc2c8072014-01-17 11:44:32 +02004156 DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
Ville Syrjälä2d9d2b02014-01-17 11:44:31 +02004157 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01004158
4159 if (blc_event || (iir & I915_ASLE_INTERRUPT))
4160 intel_opregion_asle_intr(dev);
4161
Daniel Vetter515ac2b2012-12-01 13:53:44 +01004162 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4163 gmbus_irq_handler(dev);
4164
Chris Wilsona266c7d2012-04-24 22:59:44 +01004165 /* With MSI, interrupts are only generated when iir
4166 * transitions from zero to nonzero. If another bit got
4167 * set while we were handling the existing iir bits, then
4168 * we would never get another interrupt.
4169 *
4170 * This is fine on non-MSI as well, as if we hit this path
4171 * we avoid exiting the interrupt handler only to generate
4172 * another one.
4173 *
4174 * Note that for MSI this could cause a stray interrupt report
4175 * if an interrupt landed in the time between writing IIR and
4176 * the posting read. This should be rare enough to never
4177 * trigger the 99% of 100,000 interrupts test for disabling
4178 * stray interrupts.
4179 */
4180 iir = new_iir;
4181 }
4182
Daniel Vetterd05c6172012-04-26 23:28:09 +02004183 i915_update_dri1_breadcrumb(dev);
Chris Wilson2c8ba292012-04-24 22:59:46 +01004184
Chris Wilsona266c7d2012-04-24 22:59:44 +01004185 return ret;
4186}
4187
4188static void i965_irq_uninstall(struct drm_device * dev)
4189{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004190 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004191 int pipe;
4192
4193 if (!dev_priv)
4194 return;
4195
Ville Syrjälä3ca1cce2014-01-17 13:43:51 +02004196 intel_hpd_irq_uninstall(dev_priv);
Egbert Eichac4c16c2013-04-16 13:36:58 +02004197
Chris Wilsonadca4732012-05-11 18:01:31 +01004198 I915_WRITE(PORT_HOTPLUG_EN, 0);
4199 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01004200
4201 I915_WRITE(HWSTAM, 0xffffffff);
4202 for_each_pipe(pipe)
4203 I915_WRITE(PIPESTAT(pipe), 0);
4204 I915_WRITE(IMR, 0xffffffff);
4205 I915_WRITE(IER, 0x0);
4206
4207 for_each_pipe(pipe)
4208 I915_WRITE(PIPESTAT(pipe),
4209 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4210 I915_WRITE(IIR, I915_READ(IIR));
4211}
4212
Ville Syrjälä3ca1cce2014-01-17 13:43:51 +02004213static void intel_hpd_irq_reenable(unsigned long data)
Egbert Eichac4c16c2013-04-16 13:36:58 +02004214{
Jani Nikula2d1013d2014-03-31 14:27:17 +03004215 struct drm_i915_private *dev_priv = (struct drm_i915_private *)data;
Egbert Eichac4c16c2013-04-16 13:36:58 +02004216 struct drm_device *dev = dev_priv->dev;
4217 struct drm_mode_config *mode_config = &dev->mode_config;
4218 unsigned long irqflags;
4219 int i;
4220
4221 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4222 for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4223 struct drm_connector *connector;
4224
4225 if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4226 continue;
4227
4228 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4229
4230 list_for_each_entry(connector, &mode_config->connector_list, head) {
4231 struct intel_connector *intel_connector = to_intel_connector(connector);
4232
4233 if (intel_connector->encoder->hpd_pin == i) {
4234 if (connector->polled != intel_connector->polled)
4235 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4236 drm_get_connector_name(connector));
4237 connector->polled = intel_connector->polled;
4238 if (!connector->polled)
4239 connector->polled = DRM_CONNECTOR_POLL_HPD;
4240 }
4241 }
4242 }
4243 if (dev_priv->display.hpd_irq_setup)
4244 dev_priv->display.hpd_irq_setup(dev);
4245 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4246}
4247
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004248void intel_irq_init(struct drm_device *dev)
4249{
Chris Wilson8b2e3262012-04-24 22:59:41 +01004250 struct drm_i915_private *dev_priv = dev->dev_private;
4251
4252 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Daniel Vetter99584db2012-11-14 17:14:04 +01004253 INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02004254 INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004255 INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
Chris Wilson8b2e3262012-04-24 22:59:41 +01004256
Deepak Sa6706b42014-03-15 20:23:22 +05304257 /* Let's track the enabled rps events */
4258 dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4259
Daniel Vetter99584db2012-11-14 17:14:04 +01004260 setup_timer(&dev_priv->gpu_error.hangcheck_timer,
4261 i915_hangcheck_elapsed,
Daniel Vetter61bac782012-12-01 21:03:21 +01004262 (unsigned long) dev);
Ville Syrjälä3ca1cce2014-01-17 13:43:51 +02004263 setup_timer(&dev_priv->hotplug_reenable_timer, intel_hpd_irq_reenable,
Egbert Eichac4c16c2013-04-16 13:36:58 +02004264 (unsigned long) dev_priv);
Daniel Vetter61bac782012-12-01 21:03:21 +01004265
Tomas Janousek97a19a22012-12-08 13:48:13 +01004266 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01004267
Ville Syrjälä4cdb83e2013-10-11 21:52:44 +03004268 if (IS_GEN2(dev)) {
4269 dev->max_vblank_count = 0;
4270 dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4271 } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004272 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4273 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Ville Syrjälä391f75e2013-09-25 19:55:26 +03004274 } else {
4275 dev->driver->get_vblank_counter = i915_get_vblank_counter;
4276 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004277 }
4278
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +03004279 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Keith Packardc3613de2011-08-12 17:05:54 -07004280 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +03004281 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4282 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004283
Ville Syrjälä43f328d2014-04-09 20:40:52 +03004284 if (IS_CHERRYVIEW(dev)) {
4285 dev->driver->irq_handler = cherryview_irq_handler;
4286 dev->driver->irq_preinstall = cherryview_irq_preinstall;
4287 dev->driver->irq_postinstall = cherryview_irq_postinstall;
4288 dev->driver->irq_uninstall = cherryview_irq_uninstall;
4289 dev->driver->enable_vblank = valleyview_enable_vblank;
4290 dev->driver->disable_vblank = valleyview_disable_vblank;
4291 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4292 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07004293 dev->driver->irq_handler = valleyview_irq_handler;
4294 dev->driver->irq_preinstall = valleyview_irq_preinstall;
4295 dev->driver->irq_postinstall = valleyview_irq_postinstall;
4296 dev->driver->irq_uninstall = valleyview_irq_uninstall;
4297 dev->driver->enable_vblank = valleyview_enable_vblank;
4298 dev->driver->disable_vblank = valleyview_disable_vblank;
Egbert Eichfa00abe2013-02-25 12:06:48 -05004299 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Ben Widawskyabd58f02013-11-02 21:07:09 -07004300 } else if (IS_GEN8(dev)) {
4301 dev->driver->irq_handler = gen8_irq_handler;
4302 dev->driver->irq_preinstall = gen8_irq_preinstall;
4303 dev->driver->irq_postinstall = gen8_irq_postinstall;
4304 dev->driver->irq_uninstall = gen8_irq_uninstall;
4305 dev->driver->enable_vblank = gen8_enable_vblank;
4306 dev->driver->disable_vblank = gen8_disable_vblank;
4307 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004308 } else if (HAS_PCH_SPLIT(dev)) {
4309 dev->driver->irq_handler = ironlake_irq_handler;
4310 dev->driver->irq_preinstall = ironlake_irq_preinstall;
4311 dev->driver->irq_postinstall = ironlake_irq_postinstall;
4312 dev->driver->irq_uninstall = ironlake_irq_uninstall;
4313 dev->driver->enable_vblank = ironlake_enable_vblank;
4314 dev->driver->disable_vblank = ironlake_disable_vblank;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01004315 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004316 } else {
Chris Wilsonc2798b12012-04-22 21:13:57 +01004317 if (INTEL_INFO(dev)->gen == 2) {
4318 dev->driver->irq_preinstall = i8xx_irq_preinstall;
4319 dev->driver->irq_postinstall = i8xx_irq_postinstall;
4320 dev->driver->irq_handler = i8xx_irq_handler;
4321 dev->driver->irq_uninstall = i8xx_irq_uninstall;
Chris Wilsona266c7d2012-04-24 22:59:44 +01004322 } else if (INTEL_INFO(dev)->gen == 3) {
4323 dev->driver->irq_preinstall = i915_irq_preinstall;
4324 dev->driver->irq_postinstall = i915_irq_postinstall;
4325 dev->driver->irq_uninstall = i915_irq_uninstall;
4326 dev->driver->irq_handler = i915_irq_handler;
Daniel Vetter20afbda2012-12-11 14:05:07 +01004327 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01004328 } else {
Chris Wilsona266c7d2012-04-24 22:59:44 +01004329 dev->driver->irq_preinstall = i965_irq_preinstall;
4330 dev->driver->irq_postinstall = i965_irq_postinstall;
4331 dev->driver->irq_uninstall = i965_irq_uninstall;
4332 dev->driver->irq_handler = i965_irq_handler;
Egbert Eichbac56d52013-02-25 12:06:51 -05004333 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01004334 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07004335 dev->driver->enable_vblank = i915_enable_vblank;
4336 dev->driver->disable_vblank = i915_disable_vblank;
4337 }
4338}
Daniel Vetter20afbda2012-12-11 14:05:07 +01004339
4340void intel_hpd_init(struct drm_device *dev)
4341{
4342 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eich821450c2013-04-16 13:36:55 +02004343 struct drm_mode_config *mode_config = &dev->mode_config;
4344 struct drm_connector *connector;
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02004345 unsigned long irqflags;
Egbert Eich821450c2013-04-16 13:36:55 +02004346 int i;
Daniel Vetter20afbda2012-12-11 14:05:07 +01004347
Egbert Eich821450c2013-04-16 13:36:55 +02004348 for (i = 1; i < HPD_NUM_PINS; i++) {
4349 dev_priv->hpd_stats[i].hpd_cnt = 0;
4350 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4351 }
4352 list_for_each_entry(connector, &mode_config->connector_list, head) {
4353 struct intel_connector *intel_connector = to_intel_connector(connector);
4354 connector->polled = intel_connector->polled;
4355 if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
4356 connector->polled = DRM_CONNECTOR_POLL_HPD;
4357 }
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02004358
4359 /* Interrupt setup is already guaranteed to be single-threaded, this is
4360 * just to make the assert_spin_locked checks happy. */
4361 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter20afbda2012-12-11 14:05:07 +01004362 if (dev_priv->display.hpd_irq_setup)
4363 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02004364 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Daniel Vetter20afbda2012-12-11 14:05:07 +01004365}
Paulo Zanonic67a4702013-08-19 13:18:09 -03004366
Paulo Zanoni5d584b22014-03-07 20:08:15 -03004367/* Disable interrupts so we can allow runtime PM. */
Paulo Zanoni730488b2014-03-07 20:12:32 -03004368void intel_runtime_pm_disable_interrupts(struct drm_device *dev)
Paulo Zanonic67a4702013-08-19 13:18:09 -03004369{
4370 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonic67a4702013-08-19 13:18:09 -03004371
Paulo Zanoni730488b2014-03-07 20:12:32 -03004372 dev->driver->irq_uninstall(dev);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03004373 dev_priv->pm.irqs_disabled = true;
Paulo Zanonic67a4702013-08-19 13:18:09 -03004374}
4375
Paulo Zanoni5d584b22014-03-07 20:08:15 -03004376/* Restore interrupts so we can recover from runtime PM. */
Paulo Zanoni730488b2014-03-07 20:12:32 -03004377void intel_runtime_pm_restore_interrupts(struct drm_device *dev)
Paulo Zanonic67a4702013-08-19 13:18:09 -03004378{
4379 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonic67a4702013-08-19 13:18:09 -03004380
Paulo Zanoni5d584b22014-03-07 20:08:15 -03004381 dev_priv->pm.irqs_disabled = false;
Paulo Zanoni730488b2014-03-07 20:12:32 -03004382 dev->driver->irq_preinstall(dev);
4383 dev->driver->irq_postinstall(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03004384}