blob: f4d5569834c222b70480a173632519bbbefd9f31 [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>
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/drmP.h>
34#include <drm/i915_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010036#include "i915_trace.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Egbert Eiche5868a32013-02-28 04:17:12 -050039static const u32 hpd_ibx[] = {
40 [HPD_CRT] = SDE_CRT_HOTPLUG,
41 [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
42 [HPD_PORT_B] = SDE_PORTB_HOTPLUG,
43 [HPD_PORT_C] = SDE_PORTC_HOTPLUG,
44 [HPD_PORT_D] = SDE_PORTD_HOTPLUG
45};
46
47static const u32 hpd_cpt[] = {
48 [HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
Daniel Vetter73c352a2013-03-26 22:38:43 +010049 [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
Egbert Eiche5868a32013-02-28 04:17:12 -050050 [HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
51 [HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
52 [HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
53};
54
55static const u32 hpd_mask_i915[] = {
56 [HPD_CRT] = CRT_HOTPLUG_INT_EN,
57 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
58 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
59 [HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
60 [HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
61 [HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
62};
63
64static const u32 hpd_status_gen4[] = {
65 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
66 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
67 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
68 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
69 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
70 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
71};
72
Egbert Eiche5868a32013-02-28 04:17:12 -050073static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
74 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
76 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
77 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80};
81
Zhenyu Wang036a4a72009-06-08 14:40:19 +080082/* For display hotplug interrupt */
Chris Wilson995b6762010-08-20 13:23:26 +010083static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050084ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080085{
Daniel Vetter4bc9d432013-06-27 13:44:58 +020086 assert_spin_locked(&dev_priv->irq_lock);
87
Chris Wilson1ec14ad2010-12-04 11:30:53 +000088 if ((dev_priv->irq_mask & mask) != 0) {
89 dev_priv->irq_mask &= ~mask;
90 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000091 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080092 }
93}
94
Paulo Zanoni0ff98002013-02-22 17:05:31 -030095static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050096ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080097{
Daniel Vetter4bc9d432013-06-27 13:44:58 +020098 assert_spin_locked(&dev_priv->irq_lock);
99
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000100 if ((dev_priv->irq_mask & mask) != mask) {
101 dev_priv->irq_mask |= mask;
102 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000103 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800104 }
105}
106
Paulo Zanoni86642812013-04-12 17:57:57 -0300107static bool ivb_can_enable_err_int(struct drm_device *dev)
108{
109 struct drm_i915_private *dev_priv = dev->dev_private;
110 struct intel_crtc *crtc;
111 enum pipe pipe;
112
Daniel Vetter4bc9d432013-06-27 13:44:58 +0200113 assert_spin_locked(&dev_priv->irq_lock);
114
Paulo Zanoni86642812013-04-12 17:57:57 -0300115 for_each_pipe(pipe) {
116 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
117
118 if (crtc->cpu_fifo_underrun_disabled)
119 return false;
120 }
121
122 return true;
123}
124
125static bool cpt_can_enable_serr_int(struct drm_device *dev)
126{
127 struct drm_i915_private *dev_priv = dev->dev_private;
128 enum pipe pipe;
129 struct intel_crtc *crtc;
130
Daniel Vetterfee884e2013-07-04 23:35:21 +0200131 assert_spin_locked(&dev_priv->irq_lock);
132
Paulo Zanoni86642812013-04-12 17:57:57 -0300133 for_each_pipe(pipe) {
134 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
135
136 if (crtc->pch_fifo_underrun_disabled)
137 return false;
138 }
139
140 return true;
141}
142
143static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
144 enum pipe pipe, bool enable)
145{
146 struct drm_i915_private *dev_priv = dev->dev_private;
147 uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
148 DE_PIPEB_FIFO_UNDERRUN;
149
150 if (enable)
151 ironlake_enable_display_irq(dev_priv, bit);
152 else
153 ironlake_disable_display_irq(dev_priv, bit);
154}
155
156static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
Daniel Vetter7336df62013-07-09 22:59:16 +0200157 enum pipe pipe, bool enable)
Paulo Zanoni86642812013-04-12 17:57:57 -0300158{
159 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni86642812013-04-12 17:57:57 -0300160 if (enable) {
Daniel Vetter7336df62013-07-09 22:59:16 +0200161 I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
162
Paulo Zanoni86642812013-04-12 17:57:57 -0300163 if (!ivb_can_enable_err_int(dev))
164 return;
165
Paulo Zanoni86642812013-04-12 17:57:57 -0300166 ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
167 } else {
Daniel Vetter7336df62013-07-09 22:59:16 +0200168 bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
169
170 /* Change the state _after_ we've read out the current one. */
Paulo Zanoni86642812013-04-12 17:57:57 -0300171 ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
Daniel Vetter7336df62013-07-09 22:59:16 +0200172
173 if (!was_enabled &&
174 (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
175 DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
176 pipe_name(pipe));
177 }
Paulo Zanoni86642812013-04-12 17:57:57 -0300178 }
179}
180
Daniel Vetterfee884e2013-07-04 23:35:21 +0200181/**
182 * ibx_display_interrupt_update - update SDEIMR
183 * @dev_priv: driver private
184 * @interrupt_mask: mask of interrupt bits to update
185 * @enabled_irq_mask: mask of interrupt bits to enable
186 */
187static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
188 uint32_t interrupt_mask,
189 uint32_t enabled_irq_mask)
190{
191 uint32_t sdeimr = I915_READ(SDEIMR);
192 sdeimr &= ~interrupt_mask;
193 sdeimr |= (~enabled_irq_mask & interrupt_mask);
194
195 assert_spin_locked(&dev_priv->irq_lock);
196
197 I915_WRITE(SDEIMR, sdeimr);
198 POSTING_READ(SDEIMR);
199}
200#define ibx_enable_display_interrupt(dev_priv, bits) \
201 ibx_display_interrupt_update((dev_priv), (bits), (bits))
202#define ibx_disable_display_interrupt(dev_priv, bits) \
203 ibx_display_interrupt_update((dev_priv), (bits), 0)
204
Daniel Vetterde280752013-07-04 23:35:24 +0200205static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
206 enum transcoder pch_transcoder,
Paulo Zanoni86642812013-04-12 17:57:57 -0300207 bool enable)
208{
Paulo Zanoni86642812013-04-12 17:57:57 -0300209 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterde280752013-07-04 23:35:24 +0200210 uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
211 SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
Paulo Zanoni86642812013-04-12 17:57:57 -0300212
213 if (enable)
Daniel Vetterfee884e2013-07-04 23:35:21 +0200214 ibx_enable_display_interrupt(dev_priv, bit);
Paulo Zanoni86642812013-04-12 17:57:57 -0300215 else
Daniel Vetterfee884e2013-07-04 23:35:21 +0200216 ibx_disable_display_interrupt(dev_priv, bit);
Paulo Zanoni86642812013-04-12 17:57:57 -0300217}
218
219static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
220 enum transcoder pch_transcoder,
221 bool enable)
222{
223 struct drm_i915_private *dev_priv = dev->dev_private;
224
225 if (enable) {
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200226 I915_WRITE(SERR_INT,
227 SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
228
Paulo Zanoni86642812013-04-12 17:57:57 -0300229 if (!cpt_can_enable_serr_int(dev))
230 return;
231
Daniel Vetterfee884e2013-07-04 23:35:21 +0200232 ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
Paulo Zanoni86642812013-04-12 17:57:57 -0300233 } else {
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200234 uint32_t tmp = I915_READ(SERR_INT);
235 bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
236
237 /* Change the state _after_ we've read out the current one. */
Daniel Vetterfee884e2013-07-04 23:35:21 +0200238 ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200239
240 if (!was_enabled &&
241 (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
242 DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
243 transcoder_name(pch_transcoder));
244 }
Paulo Zanoni86642812013-04-12 17:57:57 -0300245 }
Paulo Zanoni86642812013-04-12 17:57:57 -0300246}
247
248/**
249 * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
250 * @dev: drm device
251 * @pipe: pipe
252 * @enable: true if we want to report FIFO underrun errors, false otherwise
253 *
254 * This function makes us disable or enable CPU fifo underruns for a specific
255 * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
256 * reporting for one pipe may also disable all the other CPU error interruts for
257 * the other pipes, due to the fact that there's just one interrupt mask/enable
258 * bit for all the pipes.
259 *
260 * Returns the previous state of underrun reporting.
261 */
262bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
263 enum pipe pipe, bool enable)
264{
265 struct drm_i915_private *dev_priv = dev->dev_private;
266 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
268 unsigned long flags;
269 bool ret;
270
271 spin_lock_irqsave(&dev_priv->irq_lock, flags);
272
273 ret = !intel_crtc->cpu_fifo_underrun_disabled;
274
275 if (enable == ret)
276 goto done;
277
278 intel_crtc->cpu_fifo_underrun_disabled = !enable;
279
280 if (IS_GEN5(dev) || IS_GEN6(dev))
281 ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
282 else if (IS_GEN7(dev))
Daniel Vetter7336df62013-07-09 22:59:16 +0200283 ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
Paulo Zanoni86642812013-04-12 17:57:57 -0300284
285done:
286 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
287 return ret;
288}
289
290/**
291 * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
292 * @dev: drm device
293 * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
294 * @enable: true if we want to report FIFO underrun errors, false otherwise
295 *
296 * This function makes us disable or enable PCH fifo underruns for a specific
297 * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
298 * underrun reporting for one transcoder may also disable all the other PCH
299 * error interruts for the other transcoders, due to the fact that there's just
300 * one interrupt mask/enable bit for all the transcoders.
301 *
302 * Returns the previous state of underrun reporting.
303 */
304bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
305 enum transcoder pch_transcoder,
306 bool enable)
307{
308 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterde280752013-07-04 23:35:24 +0200309 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
310 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni86642812013-04-12 17:57:57 -0300311 unsigned long flags;
312 bool ret;
313
Daniel Vetterde280752013-07-04 23:35:24 +0200314 /*
315 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
316 * has only one pch transcoder A that all pipes can use. To avoid racy
317 * pch transcoder -> pipe lookups from interrupt code simply store the
318 * underrun statistics in crtc A. Since we never expose this anywhere
319 * nor use it outside of the fifo underrun code here using the "wrong"
320 * crtc on LPT won't cause issues.
321 */
Paulo Zanoni86642812013-04-12 17:57:57 -0300322
323 spin_lock_irqsave(&dev_priv->irq_lock, flags);
324
325 ret = !intel_crtc->pch_fifo_underrun_disabled;
326
327 if (enable == ret)
328 goto done;
329
330 intel_crtc->pch_fifo_underrun_disabled = !enable;
331
332 if (HAS_PCH_IBX(dev))
Daniel Vetterde280752013-07-04 23:35:24 +0200333 ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
Paulo Zanoni86642812013-04-12 17:57:57 -0300334 else
335 cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
336
337done:
338 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
339 return ret;
340}
341
342
Keith Packard7c463582008-11-04 02:03:27 -0800343void
344i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
345{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200346 u32 reg = PIPESTAT(pipe);
347 u32 pipestat = I915_READ(reg) & 0x7fff0000;
Keith Packard7c463582008-11-04 02:03:27 -0800348
Daniel Vetterb79480b2013-06-27 17:52:10 +0200349 assert_spin_locked(&dev_priv->irq_lock);
350
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200351 if ((pipestat & mask) == mask)
352 return;
353
354 /* Enable the interrupt, clear any pending status */
355 pipestat |= mask | (mask >> 16);
356 I915_WRITE(reg, pipestat);
357 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800358}
359
360void
361i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
362{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200363 u32 reg = PIPESTAT(pipe);
364 u32 pipestat = I915_READ(reg) & 0x7fff0000;
Keith Packard7c463582008-11-04 02:03:27 -0800365
Daniel Vetterb79480b2013-06-27 17:52:10 +0200366 assert_spin_locked(&dev_priv->irq_lock);
367
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200368 if ((pipestat & mask) == 0)
369 return;
370
371 pipestat &= ~mask;
372 I915_WRITE(reg, pipestat);
373 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800374}
375
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000376/**
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300377 * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
Zhao Yakui01c66882009-10-28 05:10:00 +0000378 */
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300379static void i915_enable_asle_pipestat(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +0000380{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000381 drm_i915_private_t *dev_priv = dev->dev_private;
382 unsigned long irqflags;
383
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300384 if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
385 return;
386
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000387 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000388
Jani Nikulaf8987802013-04-29 13:02:53 +0300389 i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
390 if (INTEL_INFO(dev)->gen >= 4)
391 i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000392
393 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000394}
395
396/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700397 * i915_pipe_enabled - check if a pipe is enabled
398 * @dev: DRM device
399 * @pipe: pipe to check
400 *
401 * Reading certain registers when the pipe is disabled can hang the chip.
402 * Use this routine to make sure the PLL is running and the pipe is active
403 * before reading such registers if unsure.
404 */
405static int
406i915_pipe_enabled(struct drm_device *dev, int pipe)
407{
408 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200409
Daniel Vettera01025a2013-05-22 00:50:23 +0200410 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
411 /* Locking is horribly broken here, but whatever. */
412 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni71f8ba62013-05-03 12:15:39 -0300414
Daniel Vettera01025a2013-05-22 00:50:23 +0200415 return intel_crtc->active;
416 } else {
417 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
418 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700419}
420
Keith Packard42f52ef2008-10-18 19:39:29 -0700421/* Called from drm generic code, passed a 'crtc', which
422 * we use as a pipe index
423 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700424static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700425{
426 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
427 unsigned long high_frame;
428 unsigned long low_frame;
Chris Wilson5eddb702010-09-11 13:48:45 +0100429 u32 high1, high2, low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700430
431 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800432 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800433 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700434 return 0;
435 }
436
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800437 high_frame = PIPEFRAME(pipe);
438 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100439
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700440 /*
441 * High & low register fields aren't synchronized, so make sure
442 * we get a low value that's stable across two reads of the high
443 * register.
444 */
445 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100446 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
447 low = I915_READ(low_frame) & PIPE_FRAME_LOW_MASK;
448 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700449 } while (high1 != high2);
450
Chris Wilson5eddb702010-09-11 13:48:45 +0100451 high1 >>= PIPE_FRAME_HIGH_SHIFT;
452 low >>= PIPE_FRAME_LOW_SHIFT;
453 return (high1 << 8) | low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700454}
455
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700456static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800457{
458 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800459 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800460
461 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800462 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800463 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800464 return 0;
465 }
466
467 return I915_READ(reg);
468}
469
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700470static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100471 int *vpos, int *hpos)
472{
473 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
474 u32 vbl = 0, position = 0;
475 int vbl_start, vbl_end, htotal, vtotal;
476 bool in_vbl = true;
477 int ret = 0;
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200478 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
479 pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100480
481 if (!i915_pipe_enabled(dev, pipe)) {
482 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800483 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100484 return 0;
485 }
486
487 /* Get vtotal. */
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200488 vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100489
490 if (INTEL_INFO(dev)->gen >= 4) {
491 /* No obvious pixelcount register. Only query vertical
492 * scanout position from Display scan line register.
493 */
494 position = I915_READ(PIPEDSL(pipe));
495
496 /* Decode into vertical scanout position. Don't have
497 * horizontal scanout position.
498 */
499 *vpos = position & 0x1fff;
500 *hpos = 0;
501 } else {
502 /* Have access to pixelcount since start of frame.
503 * We can split this into vertical and horizontal
504 * scanout position.
505 */
506 position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
507
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200508 htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100509 *vpos = position / htotal;
510 *hpos = position - (*vpos * htotal);
511 }
512
513 /* Query vblank area. */
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200514 vbl = I915_READ(VBLANK(cpu_transcoder));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100515
516 /* Test position against vblank region. */
517 vbl_start = vbl & 0x1fff;
518 vbl_end = (vbl >> 16) & 0x1fff;
519
520 if ((*vpos < vbl_start) || (*vpos > vbl_end))
521 in_vbl = false;
522
523 /* Inside "upper part" of vblank area? Apply corrective offset: */
524 if (in_vbl && (*vpos >= vbl_start))
525 *vpos = *vpos - vtotal;
526
527 /* Readouts valid? */
528 if (vbl > 0)
529 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
530
531 /* In vblank? */
532 if (in_vbl)
533 ret |= DRM_SCANOUTPOS_INVBL;
534
535 return ret;
536}
537
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700538static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100539 int *max_error,
540 struct timeval *vblank_time,
541 unsigned flags)
542{
Chris Wilson4041b852011-01-22 10:07:56 +0000543 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100544
Ben Widawsky7eb552a2013-03-13 14:05:41 -0700545 if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
Chris Wilson4041b852011-01-22 10:07:56 +0000546 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100547 return -EINVAL;
548 }
549
550 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000551 crtc = intel_get_crtc_for_pipe(dev, pipe);
552 if (crtc == NULL) {
553 DRM_ERROR("Invalid crtc %d\n", pipe);
554 return -EINVAL;
555 }
556
557 if (!crtc->enabled) {
558 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
559 return -EBUSY;
560 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100561
562 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000563 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
564 vblank_time, flags,
565 crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100566}
567
Egbert Eich321a1b32013-04-11 16:00:26 +0200568static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
569{
570 enum drm_connector_status old_status;
571
572 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
573 old_status = connector->status;
574
575 connector->status = connector->funcs->detect(connector, false);
576 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
577 connector->base.id,
578 drm_get_connector_name(connector),
579 old_status, connector->status);
580 return (old_status != connector->status);
581}
582
Jesse Barnes5ca58282009-03-31 14:11:15 -0700583/*
584 * Handle hotplug events outside the interrupt handler proper.
585 */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200586#define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
587
Jesse Barnes5ca58282009-03-31 14:11:15 -0700588static void i915_hotplug_work_func(struct work_struct *work)
589{
590 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
591 hotplug_work);
592 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700593 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200594 struct intel_connector *intel_connector;
595 struct intel_encoder *intel_encoder;
596 struct drm_connector *connector;
597 unsigned long irqflags;
598 bool hpd_disabled = false;
Egbert Eich321a1b32013-04-11 16:00:26 +0200599 bool changed = false;
Egbert Eich142e2392013-04-11 15:57:57 +0200600 u32 hpd_event_bits;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700601
Daniel Vetter52d7ece2012-12-01 21:03:22 +0100602 /* HPD irq before everything is fully set up. */
603 if (!dev_priv->enable_hotplug_processing)
604 return;
605
Keith Packarda65e34c2011-07-25 10:04:56 -0700606 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -0800607 DRM_DEBUG_KMS("running encoder hotplug functions\n");
608
Egbert Eichcd569ae2013-04-16 13:36:57 +0200609 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Egbert Eich142e2392013-04-11 15:57:57 +0200610
611 hpd_event_bits = dev_priv->hpd_event_bits;
612 dev_priv->hpd_event_bits = 0;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200613 list_for_each_entry(connector, &mode_config->connector_list, head) {
614 intel_connector = to_intel_connector(connector);
615 intel_encoder = intel_connector->encoder;
616 if (intel_encoder->hpd_pin > HPD_NONE &&
617 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
618 connector->polled == DRM_CONNECTOR_POLL_HPD) {
619 DRM_INFO("HPD interrupt storm detected on connector %s: "
620 "switching from hotplug detection to polling\n",
621 drm_get_connector_name(connector));
622 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
623 connector->polled = DRM_CONNECTOR_POLL_CONNECT
624 | DRM_CONNECTOR_POLL_DISCONNECT;
625 hpd_disabled = true;
626 }
Egbert Eich142e2392013-04-11 15:57:57 +0200627 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
628 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
629 drm_get_connector_name(connector), intel_encoder->hpd_pin);
630 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200631 }
632 /* if there were no outputs to poll, poll was disabled,
633 * therefore make sure it's enabled when disabling HPD on
634 * some connectors */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200635 if (hpd_disabled) {
Egbert Eichcd569ae2013-04-16 13:36:57 +0200636 drm_kms_helper_poll_enable(dev);
Egbert Eichac4c16c2013-04-16 13:36:58 +0200637 mod_timer(&dev_priv->hotplug_reenable_timer,
638 jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
639 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200640
641 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
642
Egbert Eich321a1b32013-04-11 16:00:26 +0200643 list_for_each_entry(connector, &mode_config->connector_list, head) {
644 intel_connector = to_intel_connector(connector);
645 intel_encoder = intel_connector->encoder;
646 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
647 if (intel_encoder->hot_plug)
648 intel_encoder->hot_plug(intel_encoder);
649 if (intel_hpd_irq_event(dev, connector))
650 changed = true;
651 }
652 }
Keith Packard40ee3382011-07-28 15:31:19 -0700653 mutex_unlock(&mode_config->mutex);
654
Egbert Eich321a1b32013-04-11 16:00:26 +0200655 if (changed)
656 drm_kms_helper_hotplug_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700657}
658
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200659static void ironlake_rps_change_irq_handler(struct drm_device *dev)
Jesse Barnesf97108d2010-01-29 11:27:07 -0800660{
661 drm_i915_private_t *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000662 u32 busy_up, busy_down, max_avg, min_avg;
Daniel Vetter92703882012-08-09 16:46:01 +0200663 u8 new_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200664
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200665 spin_lock(&mchdev_lock);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800666
Daniel Vetter73edd18f2012-08-08 23:35:37 +0200667 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
668
Daniel Vetter20e4d402012-08-08 23:35:39 +0200669 new_delay = dev_priv->ips.cur_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200670
Jesse Barnes7648fa92010-05-20 14:28:11 -0700671 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000672 busy_up = I915_READ(RCPREVBSYTUPAVG);
673 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800674 max_avg = I915_READ(RCBMAXAVG);
675 min_avg = I915_READ(RCBMINAVG);
676
677 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000678 if (busy_up > max_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200679 if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
680 new_delay = dev_priv->ips.cur_delay - 1;
681 if (new_delay < dev_priv->ips.max_delay)
682 new_delay = dev_priv->ips.max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000683 } else if (busy_down < min_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200684 if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
685 new_delay = dev_priv->ips.cur_delay + 1;
686 if (new_delay > dev_priv->ips.min_delay)
687 new_delay = dev_priv->ips.min_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800688 }
689
Jesse Barnes7648fa92010-05-20 14:28:11 -0700690 if (ironlake_set_drps(dev, new_delay))
Daniel Vetter20e4d402012-08-08 23:35:39 +0200691 dev_priv->ips.cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800692
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200693 spin_unlock(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +0200694
Jesse Barnesf97108d2010-01-29 11:27:07 -0800695 return;
696}
697
Chris Wilson549f7362010-10-19 11:19:32 +0100698static void notify_ring(struct drm_device *dev,
699 struct intel_ring_buffer *ring)
700{
701 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9862e602011-01-04 22:22:17 +0000702
Chris Wilson475553d2011-01-20 09:52:56 +0000703 if (ring->obj == NULL)
704 return;
705
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100706 trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
Chris Wilson9862e602011-01-04 22:22:17 +0000707
Chris Wilson549f7362010-10-19 11:19:32 +0100708 wake_up_all(&ring->irq_queue);
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -0700709 if (i915_enable_hangcheck) {
Daniel Vetter99584db2012-11-14 17:14:04 +0100710 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
Chris Wilsoncecc21f2012-10-05 17:02:56 +0100711 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -0700712 }
Chris Wilson549f7362010-10-19 11:19:32 +0100713}
714
Ben Widawsky4912d042011-04-25 11:25:20 -0700715static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800716{
Ben Widawsky4912d042011-04-25 11:25:20 -0700717 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200718 rps.work);
Ben Widawsky4912d042011-04-25 11:25:20 -0700719 u32 pm_iir, pm_imr;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100720 u8 new_delay;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800721
Daniel Vetter59cdb632013-07-04 23:35:28 +0200722 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200723 pm_iir = dev_priv->rps.pm_iir;
724 dev_priv->rps.pm_iir = 0;
Ben Widawsky4912d042011-04-25 11:25:20 -0700725 pm_imr = I915_READ(GEN6_PMIMR);
Ben Widawsky48484052013-05-28 19:22:27 -0700726 /* Make sure not to corrupt PMIMR state used by ringbuffer code */
727 I915_WRITE(GEN6_PMIMR, pm_imr & ~GEN6_PM_RPS_EVENTS);
Daniel Vetter59cdb632013-07-04 23:35:28 +0200728 spin_unlock_irq(&dev_priv->irq_lock);
Ben Widawsky4912d042011-04-25 11:25:20 -0700729
Ben Widawsky48484052013-05-28 19:22:27 -0700730 if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800731 return;
732
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700733 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100734
Ville Syrjälä74250342013-06-25 21:38:11 +0300735 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200736 new_delay = dev_priv->rps.cur_delay + 1;
Ville Syrjälä74250342013-06-25 21:38:11 +0300737
738 /*
739 * For better performance, jump directly
740 * to RPe if we're below it.
741 */
742 if (IS_VALLEYVIEW(dev_priv->dev) &&
743 dev_priv->rps.cur_delay < dev_priv->rps.rpe_delay)
744 new_delay = dev_priv->rps.rpe_delay;
745 } else
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200746 new_delay = dev_priv->rps.cur_delay - 1;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800747
Ben Widawsky79249632012-09-07 19:43:42 -0700748 /* sysfs frequency interfaces may have snuck in while servicing the
749 * interrupt
750 */
Ville Syrjäläd8289c92013-06-25 19:21:05 +0300751 if (new_delay >= dev_priv->rps.min_delay &&
752 new_delay <= dev_priv->rps.max_delay) {
Jesse Barnes0a073b82013-04-17 15:54:58 -0700753 if (IS_VALLEYVIEW(dev_priv->dev))
754 valleyview_set_rps(dev_priv->dev, new_delay);
755 else
756 gen6_set_rps(dev_priv->dev, new_delay);
Ben Widawsky79249632012-09-07 19:43:42 -0700757 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800758
Jesse Barnes52ceb902013-04-23 10:09:26 -0700759 if (IS_VALLEYVIEW(dev_priv->dev)) {
760 /*
761 * On VLV, when we enter RC6 we may not be at the minimum
762 * voltage level, so arm a timer to check. It should only
763 * fire when there's activity or once after we've entered
764 * RC6, and then won't be re-armed until the next RPS interrupt.
765 */
766 mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
767 msecs_to_jiffies(100));
768 }
769
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700770 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800771}
772
Ben Widawskye3689192012-05-25 16:56:22 -0700773
774/**
775 * ivybridge_parity_work - Workqueue called when a parity error interrupt
776 * occurred.
777 * @work: workqueue struct
778 *
779 * Doesn't actually do anything except notify userspace. As a consequence of
780 * this event, userspace should try to remap the bad rows since statistically
781 * it is likely the same row is more likely to go bad again.
782 */
783static void ivybridge_parity_work(struct work_struct *work)
784{
785 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
Daniel Vettera4da4fa2012-11-02 19:55:07 +0100786 l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -0700787 u32 error_status, row, bank, subbank;
788 char *parity_event[5];
789 uint32_t misccpctl;
790 unsigned long flags;
791
792 /* We must turn off DOP level clock gating to access the L3 registers.
793 * In order to prevent a get/put style interface, acquire struct mutex
794 * any time we access those registers.
795 */
796 mutex_lock(&dev_priv->dev->struct_mutex);
797
798 misccpctl = I915_READ(GEN7_MISCCPCTL);
799 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
800 POSTING_READ(GEN7_MISCCPCTL);
801
802 error_status = I915_READ(GEN7_L3CDERRST1);
803 row = GEN7_PARITY_ERROR_ROW(error_status);
804 bank = GEN7_PARITY_ERROR_BANK(error_status);
805 subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
806
807 I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
808 GEN7_L3CDERRST1_ENABLE);
809 POSTING_READ(GEN7_L3CDERRST1);
810
811 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
812
813 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Ben Widawskycc609d52013-05-28 19:22:29 -0700814 dev_priv->gt_irq_mask &= ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Ben Widawskye3689192012-05-25 16:56:22 -0700815 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
816 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
817
818 mutex_unlock(&dev_priv->dev->struct_mutex);
819
820 parity_event[0] = "L3_PARITY_ERROR=1";
821 parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
822 parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
823 parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
824 parity_event[4] = NULL;
825
826 kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
827 KOBJ_CHANGE, parity_event);
828
829 DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
830 row, bank, subbank);
831
832 kfree(parity_event[3]);
833 kfree(parity_event[2]);
834 kfree(parity_event[1]);
835}
836
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200837static void ivybridge_parity_error_irq_handler(struct drm_device *dev)
Ben Widawskye3689192012-05-25 16:56:22 -0700838{
839 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Ben Widawskye3689192012-05-25 16:56:22 -0700840
Ben Widawskye1ef7cc2012-07-24 20:47:31 -0700841 if (!HAS_L3_GPU_CACHE(dev))
Ben Widawskye3689192012-05-25 16:56:22 -0700842 return;
843
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200844 spin_lock(&dev_priv->irq_lock);
Ben Widawskycc609d52013-05-28 19:22:29 -0700845 dev_priv->gt_irq_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Ben Widawskye3689192012-05-25 16:56:22 -0700846 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200847 spin_unlock(&dev_priv->irq_lock);
Ben Widawskye3689192012-05-25 16:56:22 -0700848
Daniel Vettera4da4fa2012-11-02 19:55:07 +0100849 queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -0700850}
851
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200852static void snb_gt_irq_handler(struct drm_device *dev,
853 struct drm_i915_private *dev_priv,
854 u32 gt_iir)
855{
856
Ben Widawskycc609d52013-05-28 19:22:29 -0700857 if (gt_iir &
858 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200859 notify_ring(dev, &dev_priv->ring[RCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -0700860 if (gt_iir & GT_BSD_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200861 notify_ring(dev, &dev_priv->ring[VCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -0700862 if (gt_iir & GT_BLT_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200863 notify_ring(dev, &dev_priv->ring[BCS]);
864
Ben Widawskycc609d52013-05-28 19:22:29 -0700865 if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
866 GT_BSD_CS_ERROR_INTERRUPT |
867 GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200868 DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
869 i915_handle_error(dev, false);
870 }
Ben Widawskye3689192012-05-25 16:56:22 -0700871
Ben Widawskycc609d52013-05-28 19:22:29 -0700872 if (gt_iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200873 ivybridge_parity_error_irq_handler(dev);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200874}
875
Ben Widawskybaf02a12013-05-28 19:22:24 -0700876/* Legacy way of handling PM interrupts */
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200877static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv,
878 u32 pm_iir)
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100879{
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100880 /*
881 * IIR bits should never already be set because IMR should
882 * prevent an interrupt from being shown in IIR. The warning
883 * displays a case where we've unsafely cleared
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200884 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100885 * type is not a problem, it displays a problem in the logic.
886 *
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200887 * The mask bit in IMR is cleared by dev_priv->rps.work.
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100888 */
889
Daniel Vetter59cdb632013-07-04 23:35:28 +0200890 spin_lock(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200891 dev_priv->rps.pm_iir |= pm_iir;
892 I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100893 POSTING_READ(GEN6_PMIMR);
Daniel Vetter59cdb632013-07-04 23:35:28 +0200894 spin_unlock(&dev_priv->irq_lock);
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100895
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200896 queue_work(dev_priv->wq, &dev_priv->rps.work);
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100897}
898
Egbert Eichb543fb02013-04-16 13:36:54 +0200899#define HPD_STORM_DETECT_PERIOD 1000
900#define HPD_STORM_THRESHOLD 5
901
Daniel Vetter10a504d2013-06-27 17:52:12 +0200902static inline void intel_hpd_irq_handler(struct drm_device *dev,
Daniel Vetter22062db2013-06-27 17:52:11 +0200903 u32 hotplug_trigger,
904 const u32 *hpd)
Egbert Eichb543fb02013-04-16 13:36:54 +0200905{
906 drm_i915_private_t *dev_priv = dev->dev_private;
Egbert Eichb543fb02013-04-16 13:36:54 +0200907 int i;
Daniel Vetter10a504d2013-06-27 17:52:12 +0200908 bool storm_detected = false;
Egbert Eichb543fb02013-04-16 13:36:54 +0200909
Daniel Vetter91d131d2013-06-27 17:52:14 +0200910 if (!hotplug_trigger)
911 return;
912
Daniel Vetterb5ea2d52013-06-27 17:52:15 +0200913 spin_lock(&dev_priv->irq_lock);
Egbert Eichb543fb02013-04-16 13:36:54 +0200914 for (i = 1; i < HPD_NUM_PINS; i++) {
Egbert Eich821450c2013-04-16 13:36:55 +0200915
Egbert Eichb543fb02013-04-16 13:36:54 +0200916 if (!(hpd[i] & hotplug_trigger) ||
917 dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
918 continue;
919
Jani Nikulabc5ead8c2013-05-07 15:10:29 +0300920 dev_priv->hpd_event_bits |= (1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +0200921 if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
922 dev_priv->hpd_stats[i].hpd_last_jiffies
923 + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
924 dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
925 dev_priv->hpd_stats[i].hpd_cnt = 0;
926 } else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
927 dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
Egbert Eich142e2392013-04-11 15:57:57 +0200928 dev_priv->hpd_event_bits &= ~(1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +0200929 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
Daniel Vetter10a504d2013-06-27 17:52:12 +0200930 storm_detected = true;
Egbert Eichb543fb02013-04-16 13:36:54 +0200931 } else {
932 dev_priv->hpd_stats[i].hpd_cnt++;
933 }
934 }
935
Daniel Vetter10a504d2013-06-27 17:52:12 +0200936 if (storm_detected)
937 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterb5ea2d52013-06-27 17:52:15 +0200938 spin_unlock(&dev_priv->irq_lock);
Daniel Vetter5876fa02013-06-27 17:52:13 +0200939
940 queue_work(dev_priv->wq,
941 &dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +0200942}
943
Daniel Vetter515ac2b2012-12-01 13:53:44 +0100944static void gmbus_irq_handler(struct drm_device *dev)
945{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100946 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
947
Daniel Vetter28c70f12012-12-01 13:53:45 +0100948 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetter515ac2b2012-12-01 13:53:44 +0100949}
950
Daniel Vetterce99c252012-12-01 13:53:47 +0100951static void dp_aux_irq_handler(struct drm_device *dev)
952{
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100953 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
954
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100955 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetterce99c252012-12-01 13:53:47 +0100956}
957
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200958/* Unlike gen6_rps_irq_handler() from which this function is originally derived,
Ben Widawskybaf02a12013-05-28 19:22:24 -0700959 * we must be able to deal with other PM interrupts. This is complicated because
960 * of the way in which we use the masks to defer the RPS work (which for
961 * posterity is necessary because of forcewake).
962 */
963static void hsw_pm_irq_handler(struct drm_i915_private *dev_priv,
964 u32 pm_iir)
965{
Daniel Vetter41a05a32013-07-04 23:35:26 +0200966 if (pm_iir & GEN6_PM_RPS_EVENTS) {
Daniel Vetter59cdb632013-07-04 23:35:28 +0200967 spin_lock(&dev_priv->irq_lock);
Daniel Vetter41a05a32013-07-04 23:35:26 +0200968 dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
Ben Widawskybaf02a12013-05-28 19:22:24 -0700969 I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
970 /* never want to mask useful interrupts. (also posting read) */
Ben Widawsky48484052013-05-28 19:22:27 -0700971 WARN_ON(I915_READ_NOTRACE(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
Daniel Vetter59cdb632013-07-04 23:35:28 +0200972 spin_unlock(&dev_priv->irq_lock);
Daniel Vetter2adbee62013-07-04 23:35:27 +0200973
974 queue_work(dev_priv->wq, &dev_priv->rps.work);
Ben Widawskybaf02a12013-05-28 19:22:24 -0700975 }
Ben Widawskybaf02a12013-05-28 19:22:24 -0700976
Daniel Vetter41a05a32013-07-04 23:35:26 +0200977 if (pm_iir & PM_VEBOX_USER_INTERRUPT)
978 notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
Ben Widawsky12638c52013-05-28 19:22:31 -0700979
Daniel Vetter41a05a32013-07-04 23:35:26 +0200980 if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
981 DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
982 i915_handle_error(dev_priv->dev, false);
Ben Widawsky12638c52013-05-28 19:22:31 -0700983 }
Ben Widawskybaf02a12013-05-28 19:22:24 -0700984}
985
Daniel Vetterff1f5252012-10-02 15:10:55 +0200986static irqreturn_t valleyview_irq_handler(int irq, void *arg)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700987{
988 struct drm_device *dev = (struct drm_device *) arg;
989 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
990 u32 iir, gt_iir, pm_iir;
991 irqreturn_t ret = IRQ_NONE;
992 unsigned long irqflags;
993 int pipe;
994 u32 pipe_stats[I915_MAX_PIPES];
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700995
996 atomic_inc(&dev_priv->irq_received);
997
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700998 while (true) {
999 iir = I915_READ(VLV_IIR);
1000 gt_iir = I915_READ(GTIIR);
1001 pm_iir = I915_READ(GEN6_PMIIR);
1002
1003 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1004 goto out;
1005
1006 ret = IRQ_HANDLED;
1007
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001008 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001009
1010 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1011 for_each_pipe(pipe) {
1012 int reg = PIPESTAT(pipe);
1013 pipe_stats[pipe] = I915_READ(reg);
1014
1015 /*
1016 * Clear the PIPE*STAT regs before the IIR
1017 */
1018 if (pipe_stats[pipe] & 0x8000ffff) {
1019 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1020 DRM_DEBUG_DRIVER("pipe %c underrun\n",
1021 pipe_name(pipe));
1022 I915_WRITE(reg, pipe_stats[pipe]);
1023 }
1024 }
1025 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1026
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001027 for_each_pipe(pipe) {
1028 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1029 drm_handle_vblank(dev, pipe);
1030
1031 if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
1032 intel_prepare_page_flip(dev, pipe);
1033 intel_finish_page_flip(dev, pipe);
1034 }
1035 }
1036
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001037 /* Consume port. Then clear IIR or we'll miss events */
1038 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
1039 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
Egbert Eichb543fb02013-04-16 13:36:54 +02001040 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001041
1042 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1043 hotplug_status);
Daniel Vetter91d131d2013-06-27 17:52:14 +02001044
1045 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
1046
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001047 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1048 I915_READ(PORT_HOTPLUG_STAT);
1049 }
1050
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001051 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1052 gmbus_irq_handler(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001053
Ben Widawsky48484052013-05-28 19:22:27 -07001054 if (pm_iir & GEN6_PM_RPS_EVENTS)
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001055 gen6_rps_irq_handler(dev_priv, pm_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001056
1057 I915_WRITE(GTIIR, gt_iir);
1058 I915_WRITE(GEN6_PMIIR, pm_iir);
1059 I915_WRITE(VLV_IIR, iir);
1060 }
1061
1062out:
1063 return ret;
1064}
1065
Adam Jackson23e81d62012-06-06 15:45:44 -04001066static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
Jesse Barnes776ad802011-01-04 15:09:39 -08001067{
1068 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001069 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001070 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
Jesse Barnes776ad802011-01-04 15:09:39 -08001071
Daniel Vetter91d131d2013-06-27 17:52:14 +02001072 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
1073
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001074 if (pch_iir & SDE_AUDIO_POWER_MASK) {
1075 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1076 SDE_AUDIO_POWER_SHIFT);
Jesse Barnes776ad802011-01-04 15:09:39 -08001077 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001078 port_name(port));
1079 }
Jesse Barnes776ad802011-01-04 15:09:39 -08001080
Daniel Vetterce99c252012-12-01 13:53:47 +01001081 if (pch_iir & SDE_AUX_MASK)
1082 dp_aux_irq_handler(dev);
1083
Jesse Barnes776ad802011-01-04 15:09:39 -08001084 if (pch_iir & SDE_GMBUS)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001085 gmbus_irq_handler(dev);
Jesse Barnes776ad802011-01-04 15:09:39 -08001086
1087 if (pch_iir & SDE_AUDIO_HDCP_MASK)
1088 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1089
1090 if (pch_iir & SDE_AUDIO_TRANS_MASK)
1091 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1092
1093 if (pch_iir & SDE_POISON)
1094 DRM_ERROR("PCH poison interrupt\n");
1095
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001096 if (pch_iir & SDE_FDI_MASK)
1097 for_each_pipe(pipe)
1098 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1099 pipe_name(pipe),
1100 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -08001101
1102 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1103 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1104
1105 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1106 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1107
Jesse Barnes776ad802011-01-04 15:09:39 -08001108 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
Paulo Zanoni86642812013-04-12 17:57:57 -03001109 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1110 false))
1111 DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
1112
1113 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1114 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1115 false))
1116 DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
1117}
1118
1119static void ivb_err_int_handler(struct drm_device *dev)
1120{
1121 struct drm_i915_private *dev_priv = dev->dev_private;
1122 u32 err_int = I915_READ(GEN7_ERR_INT);
1123
Paulo Zanonide032bf2013-04-12 17:57:58 -03001124 if (err_int & ERR_INT_POISON)
1125 DRM_ERROR("Poison interrupt\n");
1126
Paulo Zanoni86642812013-04-12 17:57:57 -03001127 if (err_int & ERR_INT_FIFO_UNDERRUN_A)
1128 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1129 DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1130
1131 if (err_int & ERR_INT_FIFO_UNDERRUN_B)
1132 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1133 DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1134
1135 if (err_int & ERR_INT_FIFO_UNDERRUN_C)
1136 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
1137 DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
1138
1139 I915_WRITE(GEN7_ERR_INT, err_int);
1140}
1141
1142static void cpt_serr_int_handler(struct drm_device *dev)
1143{
1144 struct drm_i915_private *dev_priv = dev->dev_private;
1145 u32 serr_int = I915_READ(SERR_INT);
1146
Paulo Zanonide032bf2013-04-12 17:57:58 -03001147 if (serr_int & SERR_INT_POISON)
1148 DRM_ERROR("PCH poison interrupt\n");
1149
Paulo Zanoni86642812013-04-12 17:57:57 -03001150 if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
1151 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1152 false))
1153 DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
1154
1155 if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
1156 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1157 false))
1158 DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
1159
1160 if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
1161 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
1162 false))
1163 DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
1164
1165 I915_WRITE(SERR_INT, serr_int);
Jesse Barnes776ad802011-01-04 15:09:39 -08001166}
1167
Adam Jackson23e81d62012-06-06 15:45:44 -04001168static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
1169{
1170 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1171 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001172 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
Adam Jackson23e81d62012-06-06 15:45:44 -04001173
Daniel Vetter91d131d2013-06-27 17:52:14 +02001174 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
1175
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001176 if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1177 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
1178 SDE_AUDIO_POWER_SHIFT_CPT);
1179 DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1180 port_name(port));
1181 }
Adam Jackson23e81d62012-06-06 15:45:44 -04001182
1183 if (pch_iir & SDE_AUX_MASK_CPT)
Daniel Vetterce99c252012-12-01 13:53:47 +01001184 dp_aux_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001185
1186 if (pch_iir & SDE_GMBUS_CPT)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001187 gmbus_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001188
1189 if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
1190 DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
1191
1192 if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
1193 DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
1194
1195 if (pch_iir & SDE_FDI_MASK_CPT)
1196 for_each_pipe(pipe)
1197 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1198 pipe_name(pipe),
1199 I915_READ(FDI_RX_IIR(pipe)));
Paulo Zanoni86642812013-04-12 17:57:57 -03001200
1201 if (pch_iir & SDE_ERROR_CPT)
1202 cpt_serr_int_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001203}
1204
Daniel Vetterff1f5252012-10-02 15:10:55 +02001205static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001206{
1207 struct drm_device *dev = (struct drm_device *) arg;
1208 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Ben Widawskyab5c6082013-04-05 13:12:41 -07001209 u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
Chris Wilson0e434062012-05-09 21:45:44 +01001210 irqreturn_t ret = IRQ_NONE;
1211 int i;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001212
1213 atomic_inc(&dev_priv->irq_received);
1214
Paulo Zanoni86642812013-04-12 17:57:57 -03001215 /* We get interrupts on unclaimed registers, so check for this before we
1216 * do any I915_{READ,WRITE}. */
1217 if (IS_HASWELL(dev) &&
1218 (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
1219 DRM_ERROR("Unclaimed register before interrupt\n");
1220 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
1221 }
1222
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001223 /* disable master interrupt before clearing iir */
1224 de_ier = I915_READ(DEIER);
1225 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson0e434062012-05-09 21:45:44 +01001226
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001227 /* Disable south interrupts. We'll only write to SDEIIR once, so further
1228 * interrupts will will be stored on its back queue, and then we'll be
1229 * able to process them after we restore SDEIER (as soon as we restore
1230 * it, we'll get an interrupt if SDEIIR still has something to process
1231 * due to its back queue). */
Ben Widawskyab5c6082013-04-05 13:12:41 -07001232 if (!HAS_PCH_NOP(dev)) {
1233 sde_ier = I915_READ(SDEIER);
1234 I915_WRITE(SDEIER, 0);
1235 POSTING_READ(SDEIER);
1236 }
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001237
Paulo Zanoni86642812013-04-12 17:57:57 -03001238 /* On Haswell, also mask ERR_INT because we don't want to risk
1239 * generating "unclaimed register" interrupts from inside the interrupt
1240 * handler. */
Daniel Vetter4bc9d432013-06-27 13:44:58 +02001241 if (IS_HASWELL(dev)) {
1242 spin_lock(&dev_priv->irq_lock);
Paulo Zanoni86642812013-04-12 17:57:57 -03001243 ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
Daniel Vetter4bc9d432013-06-27 13:44:58 +02001244 spin_unlock(&dev_priv->irq_lock);
1245 }
Paulo Zanoni86642812013-04-12 17:57:57 -03001246
Chris Wilson0e434062012-05-09 21:45:44 +01001247 gt_iir = I915_READ(GTIIR);
1248 if (gt_iir) {
1249 snb_gt_irq_handler(dev, dev_priv, gt_iir);
1250 I915_WRITE(GTIIR, gt_iir);
1251 ret = IRQ_HANDLED;
1252 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001253
1254 de_iir = I915_READ(DEIIR);
Chris Wilson0e434062012-05-09 21:45:44 +01001255 if (de_iir) {
Paulo Zanoni86642812013-04-12 17:57:57 -03001256 if (de_iir & DE_ERR_INT_IVB)
1257 ivb_err_int_handler(dev);
1258
Daniel Vetterce99c252012-12-01 13:53:47 +01001259 if (de_iir & DE_AUX_CHANNEL_A_IVB)
1260 dp_aux_irq_handler(dev);
1261
Chris Wilson0e434062012-05-09 21:45:44 +01001262 if (de_iir & DE_GSE_IVB)
Jani Nikula81a07802013-04-24 22:18:44 +03001263 intel_opregion_asle_intr(dev);
Chris Wilson0e434062012-05-09 21:45:44 +01001264
1265 for (i = 0; i < 3; i++) {
Daniel Vetter74d44442012-10-02 17:54:35 +02001266 if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
1267 drm_handle_vblank(dev, i);
Chris Wilson0e434062012-05-09 21:45:44 +01001268 if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
1269 intel_prepare_page_flip(dev, i);
1270 intel_finish_page_flip_plane(dev, i);
1271 }
Chris Wilson0e434062012-05-09 21:45:44 +01001272 }
1273
1274 /* check event from PCH */
Ben Widawskyab5c6082013-04-05 13:12:41 -07001275 if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
Chris Wilson0e434062012-05-09 21:45:44 +01001276 u32 pch_iir = I915_READ(SDEIIR);
1277
Adam Jackson23e81d62012-06-06 15:45:44 -04001278 cpt_irq_handler(dev, pch_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01001279
1280 /* clear PCH hotplug event before clear CPU irq */
1281 I915_WRITE(SDEIIR, pch_iir);
1282 }
1283
1284 I915_WRITE(DEIIR, de_iir);
1285 ret = IRQ_HANDLED;
1286 }
1287
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001288 pm_iir = I915_READ(GEN6_PMIIR);
Chris Wilson0e434062012-05-09 21:45:44 +01001289 if (pm_iir) {
Ben Widawskybaf02a12013-05-28 19:22:24 -07001290 if (IS_HASWELL(dev))
1291 hsw_pm_irq_handler(dev_priv, pm_iir);
Ben Widawsky48484052013-05-28 19:22:27 -07001292 else if (pm_iir & GEN6_PM_RPS_EVENTS)
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001293 gen6_rps_irq_handler(dev_priv, pm_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01001294 I915_WRITE(GEN6_PMIIR, pm_iir);
1295 ret = IRQ_HANDLED;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001296 }
1297
Daniel Vetter4bc9d432013-06-27 13:44:58 +02001298 if (IS_HASWELL(dev)) {
1299 spin_lock(&dev_priv->irq_lock);
1300 if (ivb_can_enable_err_int(dev))
1301 ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
1302 spin_unlock(&dev_priv->irq_lock);
1303 }
Paulo Zanoni86642812013-04-12 17:57:57 -03001304
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001305 I915_WRITE(DEIER, de_ier);
1306 POSTING_READ(DEIER);
Ben Widawskyab5c6082013-04-05 13:12:41 -07001307 if (!HAS_PCH_NOP(dev)) {
1308 I915_WRITE(SDEIER, sde_ier);
1309 POSTING_READ(SDEIER);
1310 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001311
1312 return ret;
1313}
1314
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001315static void ilk_gt_irq_handler(struct drm_device *dev,
1316 struct drm_i915_private *dev_priv,
1317 u32 gt_iir)
1318{
Ben Widawskycc609d52013-05-28 19:22:29 -07001319 if (gt_iir &
1320 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001321 notify_ring(dev, &dev_priv->ring[RCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001322 if (gt_iir & ILK_BSD_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001323 notify_ring(dev, &dev_priv->ring[VCS]);
1324}
1325
Daniel Vetterff1f5252012-10-02 15:10:55 +02001326static irqreturn_t ironlake_irq_handler(int irq, void *arg)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001327{
Jesse Barnes46979952011-04-07 13:53:55 -07001328 struct drm_device *dev = (struct drm_device *) arg;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001329 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1330 int ret = IRQ_NONE;
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001331 u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001332
Jesse Barnes46979952011-04-07 13:53:55 -07001333 atomic_inc(&dev_priv->irq_received);
1334
Zou, Nanhai2d109a82009-11-06 02:13:01 +00001335 /* disable master interrupt before clearing iir */
1336 de_ier = I915_READ(DEIER);
1337 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001338 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +00001339
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001340 /* Disable south interrupts. We'll only write to SDEIIR once, so further
1341 * interrupts will will be stored on its back queue, and then we'll be
1342 * able to process them after we restore SDEIER (as soon as we restore
1343 * it, we'll get an interrupt if SDEIIR still has something to process
1344 * due to its back queue). */
1345 sde_ier = I915_READ(SDEIER);
1346 I915_WRITE(SDEIER, 0);
1347 POSTING_READ(SDEIER);
1348
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001349 de_iir = I915_READ(DEIIR);
1350 gt_iir = I915_READ(GTIIR);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001351 pm_iir = I915_READ(GEN6_PMIIR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001352
Daniel Vetteracd15b62012-11-30 11:24:50 +01001353 if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
Zou Nan haic7c85102010-01-15 10:29:06 +08001354 goto done;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001355
Zou Nan haic7c85102010-01-15 10:29:06 +08001356 ret = IRQ_HANDLED;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001357
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001358 if (IS_GEN5(dev))
1359 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1360 else
1361 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +08001362
Daniel Vetterce99c252012-12-01 13:53:47 +01001363 if (de_iir & DE_AUX_CHANNEL_A)
1364 dp_aux_irq_handler(dev);
1365
Zou Nan haic7c85102010-01-15 10:29:06 +08001366 if (de_iir & DE_GSE)
Jani Nikula81a07802013-04-24 22:18:44 +03001367 intel_opregion_asle_intr(dev);
Zou Nan haic7c85102010-01-15 10:29:06 +08001368
Daniel Vetter74d44442012-10-02 17:54:35 +02001369 if (de_iir & DE_PIPEA_VBLANK)
1370 drm_handle_vblank(dev, 0);
1371
1372 if (de_iir & DE_PIPEB_VBLANK)
1373 drm_handle_vblank(dev, 1);
1374
Paulo Zanonide032bf2013-04-12 17:57:58 -03001375 if (de_iir & DE_POISON)
1376 DRM_ERROR("Poison interrupt\n");
1377
Paulo Zanoni86642812013-04-12 17:57:57 -03001378 if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1379 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1380 DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1381
1382 if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1383 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1384 DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1385
Zhenyu Wangf072d2e2010-02-09 09:46:19 +08001386 if (de_iir & DE_PLANEA_FLIP_DONE) {
Jesse Barnes013d5aa2010-01-29 11:18:31 -08001387 intel_prepare_page_flip(dev, 0);
Chris Wilson2bbda382010-09-02 17:59:39 +01001388 intel_finish_page_flip_plane(dev, 0);
Jesse Barnes013d5aa2010-01-29 11:18:31 -08001389 }
1390
Zhenyu Wangf072d2e2010-02-09 09:46:19 +08001391 if (de_iir & DE_PLANEB_FLIP_DONE) {
1392 intel_prepare_page_flip(dev, 1);
Chris Wilson2bbda382010-09-02 17:59:39 +01001393 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes013d5aa2010-01-29 11:18:31 -08001394 }
Li Pengc062df62010-01-23 00:12:58 +08001395
Zou Nan haic7c85102010-01-15 10:29:06 +08001396 /* check event from PCH */
Jesse Barnes776ad802011-01-04 15:09:39 -08001397 if (de_iir & DE_PCH_EVENT) {
Daniel Vetteracd15b62012-11-30 11:24:50 +01001398 u32 pch_iir = I915_READ(SDEIIR);
1399
Adam Jackson23e81d62012-06-06 15:45:44 -04001400 if (HAS_PCH_CPT(dev))
1401 cpt_irq_handler(dev, pch_iir);
1402 else
1403 ibx_irq_handler(dev, pch_iir);
Daniel Vetteracd15b62012-11-30 11:24:50 +01001404
1405 /* should clear PCH hotplug event before clear CPU irq */
1406 I915_WRITE(SDEIIR, pch_iir);
Jesse Barnes776ad802011-01-04 15:09:39 -08001407 }
Zou Nan haic7c85102010-01-15 10:29:06 +08001408
Daniel Vetter73edd18f2012-08-08 23:35:37 +02001409 if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001410 ironlake_rps_change_irq_handler(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08001411
Ben Widawsky48484052013-05-28 19:22:27 -07001412 if (IS_GEN6(dev) && pm_iir & GEN6_PM_RPS_EVENTS)
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001413 gen6_rps_irq_handler(dev_priv, pm_iir);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001414
Zou Nan haic7c85102010-01-15 10:29:06 +08001415 I915_WRITE(GTIIR, gt_iir);
1416 I915_WRITE(DEIIR, de_iir);
Ben Widawsky4912d042011-04-25 11:25:20 -07001417 I915_WRITE(GEN6_PMIIR, pm_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +08001418
1419done:
Zou, Nanhai2d109a82009-11-06 02:13:01 +00001420 I915_WRITE(DEIER, de_ier);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001421 POSTING_READ(DEIER);
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001422 I915_WRITE(SDEIER, sde_ier);
1423 POSTING_READ(SDEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +00001424
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001425 return ret;
1426}
1427
Jesse Barnes8a905232009-07-11 16:48:03 -04001428/**
1429 * i915_error_work_func - do process context error handling work
1430 * @work: work struct
1431 *
1432 * Fire an error uevent so userspace can see that a hang or error
1433 * was detected.
1434 */
1435static void i915_error_work_func(struct work_struct *work)
1436{
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001437 struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
1438 work);
1439 drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
1440 gpu_error);
Jesse Barnes8a905232009-07-11 16:48:03 -04001441 struct drm_device *dev = dev_priv->dev;
Daniel Vetterf69061b2012-12-06 09:01:42 +01001442 struct intel_ring_buffer *ring;
Ben Gamarif316a422009-09-14 17:48:46 -04001443 char *error_event[] = { "ERROR=1", NULL };
1444 char *reset_event[] = { "RESET=1", NULL };
1445 char *reset_done_event[] = { "ERROR=0", NULL };
Daniel Vetterf69061b2012-12-06 09:01:42 +01001446 int i, ret;
Jesse Barnes8a905232009-07-11 16:48:03 -04001447
Ben Gamarif316a422009-09-14 17:48:46 -04001448 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -04001449
Daniel Vetter7db0ba22012-12-06 16:23:37 +01001450 /*
1451 * Note that there's only one work item which does gpu resets, so we
1452 * need not worry about concurrent gpu resets potentially incrementing
1453 * error->reset_counter twice. We only need to take care of another
1454 * racing irq/hangcheck declaring the gpu dead for a second time. A
1455 * quick check for that is good enough: schedule_work ensures the
1456 * correct ordering between hang detection and this work item, and since
1457 * the reset in-progress bit is only ever set by code outside of this
1458 * work we don't need to worry about any other races.
1459 */
1460 if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +01001461 DRM_DEBUG_DRIVER("resetting chip\n");
Daniel Vetter7db0ba22012-12-06 16:23:37 +01001462 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
1463 reset_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001464
Daniel Vetterf69061b2012-12-06 09:01:42 +01001465 ret = i915_reset(dev);
1466
1467 if (ret == 0) {
1468 /*
1469 * After all the gem state is reset, increment the reset
1470 * counter and wake up everyone waiting for the reset to
1471 * complete.
1472 *
1473 * Since unlock operations are a one-sided barrier only,
1474 * we need to insert a barrier here to order any seqno
1475 * updates before
1476 * the counter increment.
1477 */
1478 smp_mb__before_atomic_inc();
1479 atomic_inc(&dev_priv->gpu_error.reset_counter);
1480
1481 kobject_uevent_env(&dev->primary->kdev.kobj,
1482 KOBJ_CHANGE, reset_done_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001483 } else {
1484 atomic_set(&error->reset_counter, I915_WEDGED);
Ben Gamarif316a422009-09-14 17:48:46 -04001485 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001486
Daniel Vetterf69061b2012-12-06 09:01:42 +01001487 for_each_ring(ring, dev_priv, i)
1488 wake_up_all(&ring->irq_queue);
1489
Ville Syrjälä96a02912013-02-18 19:08:49 +02001490 intel_display_handle_reset(dev);
1491
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001492 wake_up_all(&dev_priv->gpu_error.reset_queue);
Ben Gamarif316a422009-09-14 17:48:46 -04001493 }
Jesse Barnes8a905232009-07-11 16:48:03 -04001494}
1495
Daniel Vetter85f9e502012-08-31 21:42:26 +02001496/* NB: please notice the memset */
1497static void i915_get_extra_instdone(struct drm_device *dev,
1498 uint32_t *instdone)
1499{
1500 struct drm_i915_private *dev_priv = dev->dev_private;
1501 memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
1502
1503 switch(INTEL_INFO(dev)->gen) {
1504 case 2:
1505 case 3:
1506 instdone[0] = I915_READ(INSTDONE);
1507 break;
1508 case 4:
1509 case 5:
1510 case 6:
1511 instdone[0] = I915_READ(INSTDONE_I965);
1512 instdone[1] = I915_READ(INSTDONE1);
1513 break;
1514 default:
1515 WARN_ONCE(1, "Unsupported platform\n");
1516 case 7:
1517 instdone[0] = I915_READ(GEN7_INSTDONE_1);
1518 instdone[1] = I915_READ(GEN7_SC_INSTDONE);
1519 instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
1520 instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
1521 break;
1522 }
1523}
1524
Chris Wilson3bd3c932010-08-19 08:19:30 +01001525#ifdef CONFIG_DEBUG_FS
Chris Wilson9df30792010-02-18 10:24:56 +00001526static struct drm_i915_error_object *
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001527i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1528 struct drm_i915_gem_object *src,
1529 const int num_pages)
Chris Wilson9df30792010-02-18 10:24:56 +00001530{
1531 struct drm_i915_error_object *dst;
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001532 int i;
Chris Wilsone56660d2010-08-07 11:01:26 +01001533 u32 reloc_offset;
Chris Wilson9df30792010-02-18 10:24:56 +00001534
Chris Wilson05394f32010-11-08 19:18:58 +00001535 if (src == NULL || src->pages == NULL)
Chris Wilson9df30792010-02-18 10:24:56 +00001536 return NULL;
1537
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001538 dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +00001539 if (dst == NULL)
1540 return NULL;
1541
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001542 reloc_offset = dst->gtt_offset = i915_gem_obj_ggtt_offset(src);
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001543 for (i = 0; i < num_pages; i++) {
Andrew Morton788885a2010-05-11 14:07:05 -07001544 unsigned long flags;
Chris Wilsone56660d2010-08-07 11:01:26 +01001545 void *d;
Andrew Morton788885a2010-05-11 14:07:05 -07001546
Chris Wilsone56660d2010-08-07 11:01:26 +01001547 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +00001548 if (d == NULL)
1549 goto unwind;
Chris Wilsone56660d2010-08-07 11:01:26 +01001550
Andrew Morton788885a2010-05-11 14:07:05 -07001551 local_irq_save(flags);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001552 if (reloc_offset < dev_priv->gtt.mappable_end &&
Daniel Vetter74898d72012-02-15 23:50:22 +01001553 src->has_global_gtt_mapping) {
Chris Wilson172975aa2011-12-14 13:57:25 +01001554 void __iomem *s;
1555
1556 /* Simply ignore tiling or any overlapping fence.
1557 * It's part of the error state, and this hopefully
1558 * captures what the GPU read.
1559 */
1560
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001561 s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
Chris Wilson172975aa2011-12-14 13:57:25 +01001562 reloc_offset);
1563 memcpy_fromio(d, s, PAGE_SIZE);
1564 io_mapping_unmap_atomic(s);
Chris Wilson960e3562012-11-15 11:32:23 +00001565 } else if (src->stolen) {
1566 unsigned long offset;
1567
1568 offset = dev_priv->mm.stolen_base;
1569 offset += src->stolen->start;
1570 offset += i << PAGE_SHIFT;
1571
Daniel Vetter1a240d42012-11-29 22:18:51 +01001572 memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
Chris Wilson172975aa2011-12-14 13:57:25 +01001573 } else {
Chris Wilson9da3da62012-06-01 15:20:22 +01001574 struct page *page;
Chris Wilson172975aa2011-12-14 13:57:25 +01001575 void *s;
1576
Chris Wilson9da3da62012-06-01 15:20:22 +01001577 page = i915_gem_object_get_page(src, i);
Chris Wilson172975aa2011-12-14 13:57:25 +01001578
Chris Wilson9da3da62012-06-01 15:20:22 +01001579 drm_clflush_pages(&page, 1);
1580
1581 s = kmap_atomic(page);
Chris Wilson172975aa2011-12-14 13:57:25 +01001582 memcpy(d, s, PAGE_SIZE);
1583 kunmap_atomic(s);
1584
Chris Wilson9da3da62012-06-01 15:20:22 +01001585 drm_clflush_pages(&page, 1);
Chris Wilson172975aa2011-12-14 13:57:25 +01001586 }
Andrew Morton788885a2010-05-11 14:07:05 -07001587 local_irq_restore(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +01001588
Chris Wilson9da3da62012-06-01 15:20:22 +01001589 dst->pages[i] = d;
Chris Wilsone56660d2010-08-07 11:01:26 +01001590
1591 reloc_offset += PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +00001592 }
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001593 dst->page_count = num_pages;
Chris Wilson9df30792010-02-18 10:24:56 +00001594
1595 return dst;
1596
1597unwind:
Chris Wilson9da3da62012-06-01 15:20:22 +01001598 while (i--)
1599 kfree(dst->pages[i]);
Chris Wilson9df30792010-02-18 10:24:56 +00001600 kfree(dst);
1601 return NULL;
1602}
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001603#define i915_error_object_create(dev_priv, src) \
1604 i915_error_object_create_sized((dev_priv), (src), \
1605 (src)->base.size>>PAGE_SHIFT)
Chris Wilson9df30792010-02-18 10:24:56 +00001606
1607static void
1608i915_error_object_free(struct drm_i915_error_object *obj)
1609{
1610 int page;
1611
1612 if (obj == NULL)
1613 return;
1614
1615 for (page = 0; page < obj->page_count; page++)
1616 kfree(obj->pages[page]);
1617
1618 kfree(obj);
1619}
1620
Daniel Vetter742cbee2012-04-27 15:17:39 +02001621void
1622i915_error_state_free(struct kref *error_ref)
Chris Wilson9df30792010-02-18 10:24:56 +00001623{
Daniel Vetter742cbee2012-04-27 15:17:39 +02001624 struct drm_i915_error_state *error = container_of(error_ref,
1625 typeof(*error), ref);
Chris Wilsone2f973d2011-01-27 19:15:11 +00001626 int i;
1627
Chris Wilson52d39a22012-02-15 11:25:37 +00001628 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
1629 i915_error_object_free(error->ring[i].batchbuffer);
1630 i915_error_object_free(error->ring[i].ringbuffer);
Ben Widawsky7ed73da2013-05-25 14:42:54 -07001631 i915_error_object_free(error->ring[i].ctx);
Chris Wilson52d39a22012-02-15 11:25:37 +00001632 kfree(error->ring[i].requests);
1633 }
Chris Wilsone2f973d2011-01-27 19:15:11 +00001634
Chris Wilson9df30792010-02-18 10:24:56 +00001635 kfree(error->active_bo);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001636 kfree(error->overlay);
Ben Widawsky7ed73da2013-05-25 14:42:54 -07001637 kfree(error->display);
Chris Wilson9df30792010-02-18 10:24:56 +00001638 kfree(error);
1639}
Chris Wilson1b502472012-04-24 15:47:30 +01001640static void capture_bo(struct drm_i915_error_buffer *err,
1641 struct drm_i915_gem_object *obj)
1642{
1643 err->size = obj->base.size;
1644 err->name = obj->base.name;
Chris Wilson0201f1e2012-07-20 12:41:01 +01001645 err->rseqno = obj->last_read_seqno;
1646 err->wseqno = obj->last_write_seqno;
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001647 err->gtt_offset = i915_gem_obj_ggtt_offset(obj);
Chris Wilson1b502472012-04-24 15:47:30 +01001648 err->read_domains = obj->base.read_domains;
1649 err->write_domain = obj->base.write_domain;
1650 err->fence_reg = obj->fence_reg;
1651 err->pinned = 0;
1652 if (obj->pin_count > 0)
1653 err->pinned = 1;
1654 if (obj->user_pin_count > 0)
1655 err->pinned = -1;
1656 err->tiling = obj->tiling_mode;
1657 err->dirty = obj->dirty;
1658 err->purgeable = obj->madv != I915_MADV_WILLNEED;
1659 err->ring = obj->ring ? obj->ring->id : -1;
1660 err->cache_level = obj->cache_level;
1661}
Chris Wilson9df30792010-02-18 10:24:56 +00001662
Chris Wilson1b502472012-04-24 15:47:30 +01001663static u32 capture_active_bo(struct drm_i915_error_buffer *err,
1664 int count, struct list_head *head)
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001665{
1666 struct drm_i915_gem_object *obj;
1667 int i = 0;
1668
1669 list_for_each_entry(obj, head, mm_list) {
Chris Wilson1b502472012-04-24 15:47:30 +01001670 capture_bo(err++, obj);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001671 if (++i == count)
1672 break;
Chris Wilson1b502472012-04-24 15:47:30 +01001673 }
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001674
Chris Wilson1b502472012-04-24 15:47:30 +01001675 return i;
1676}
1677
1678static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
1679 int count, struct list_head *head)
1680{
1681 struct drm_i915_gem_object *obj;
1682 int i = 0;
1683
Ben Widawsky35c20a62013-05-31 11:28:48 -07001684 list_for_each_entry(obj, head, global_list) {
Chris Wilson1b502472012-04-24 15:47:30 +01001685 if (obj->pin_count == 0)
1686 continue;
1687
1688 capture_bo(err++, obj);
1689 if (++i == count)
1690 break;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001691 }
1692
1693 return i;
1694}
1695
Chris Wilson748ebc62010-10-24 10:28:47 +01001696static void i915_gem_record_fences(struct drm_device *dev,
1697 struct drm_i915_error_state *error)
1698{
1699 struct drm_i915_private *dev_priv = dev->dev_private;
1700 int i;
1701
1702 /* Fences */
1703 switch (INTEL_INFO(dev)->gen) {
Daniel Vetter775d17b2011-10-09 21:52:01 +02001704 case 7:
Chris Wilson748ebc62010-10-24 10:28:47 +01001705 case 6:
Ville Syrjälä42b5aea2013-04-09 13:02:47 +03001706 for (i = 0; i < dev_priv->num_fence_regs; i++)
Chris Wilson748ebc62010-10-24 10:28:47 +01001707 error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1708 break;
1709 case 5:
1710 case 4:
1711 for (i = 0; i < 16; i++)
1712 error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1713 break;
1714 case 3:
1715 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1716 for (i = 0; i < 8; i++)
1717 error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1718 case 2:
1719 for (i = 0; i < 8; i++)
1720 error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1721 break;
1722
Ben Widawsky7dbf9d62012-12-18 10:31:22 -08001723 default:
1724 BUG();
Chris Wilson748ebc62010-10-24 10:28:47 +01001725 }
1726}
1727
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001728static struct drm_i915_error_object *
1729i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1730 struct intel_ring_buffer *ring)
1731{
1732 struct drm_i915_gem_object *obj;
1733 u32 seqno;
1734
1735 if (!ring->get_seqno)
1736 return NULL;
1737
Daniel Vetterb45305f2012-12-17 16:21:27 +01001738 if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1739 u32 acthd = I915_READ(ACTHD);
1740
1741 if (WARN_ON(ring->id != RCS))
1742 return NULL;
1743
1744 obj = ring->private;
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001745 if (acthd >= i915_gem_obj_ggtt_offset(obj) &&
1746 acthd < i915_gem_obj_ggtt_offset(obj) + obj->base.size)
Daniel Vetterb45305f2012-12-17 16:21:27 +01001747 return i915_error_object_create(dev_priv, obj);
1748 }
1749
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001750 seqno = ring->get_seqno(ring, false);
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001751 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1752 if (obj->ring != ring)
1753 continue;
1754
Chris Wilson0201f1e2012-07-20 12:41:01 +01001755 if (i915_seqno_passed(seqno, obj->last_read_seqno))
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001756 continue;
1757
1758 if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1759 continue;
1760
1761 /* We need to copy these to an anonymous buffer as the simplest
1762 * method to avoid being overwritten by userspace.
1763 */
1764 return i915_error_object_create(dev_priv, obj);
1765 }
1766
1767 return NULL;
1768}
1769
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001770static void i915_record_ring_state(struct drm_device *dev,
1771 struct drm_i915_error_state *error,
1772 struct intel_ring_buffer *ring)
1773{
1774 struct drm_i915_private *dev_priv = dev->dev_private;
1775
Daniel Vetter33f3f512011-12-14 13:57:39 +01001776 if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilson12f55812012-07-05 17:14:01 +01001777 error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
Daniel Vetter33f3f512011-12-14 13:57:39 +01001778 error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001779 error->semaphore_mboxes[ring->id][0]
1780 = I915_READ(RING_SYNC_0(ring->mmio_base));
1781 error->semaphore_mboxes[ring->id][1]
1782 = I915_READ(RING_SYNC_1(ring->mmio_base));
Chris Wilsondf2b23d2012-11-27 17:06:54 +00001783 error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1784 error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
Daniel Vetter33f3f512011-12-14 13:57:39 +01001785 }
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001786
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001787 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter9d2f41f2012-04-02 21:41:45 +02001788 error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001789 error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1790 error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1791 error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001792 error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
Ben Widawsky050ee912012-08-22 11:32:15 -07001793 if (ring->id == RCS)
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001794 error->bbaddr = I915_READ64(BB_ADDR);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001795 } else {
Daniel Vetter9d2f41f2012-04-02 21:41:45 +02001796 error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001797 error->ipeir[ring->id] = I915_READ(IPEIR);
1798 error->ipehr[ring->id] = I915_READ(IPEHR);
1799 error->instdone[ring->id] = I915_READ(INSTDONE);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001800 }
1801
Ben Widawsky9574b3f2012-04-26 16:03:01 -07001802 error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001803 error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001804 error->seqno[ring->id] = ring->get_seqno(ring, false);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001805 error->acthd[ring->id] = intel_ring_get_active_head(ring);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001806 error->head[ring->id] = I915_READ_HEAD(ring);
1807 error->tail[ring->id] = I915_READ_TAIL(ring);
Chris Wilson0f3b6842013-01-15 12:05:55 +00001808 error->ctl[ring->id] = I915_READ_CTL(ring);
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001809
1810 error->cpu_ring_head[ring->id] = ring->head;
1811 error->cpu_ring_tail[ring->id] = ring->tail;
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001812}
1813
Ben Widawsky8c123e52013-03-04 17:00:29 -08001814
1815static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
1816 struct drm_i915_error_state *error,
1817 struct drm_i915_error_ring *ering)
1818{
1819 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1820 struct drm_i915_gem_object *obj;
1821
1822 /* Currently render ring is the only HW context user */
1823 if (ring->id != RCS || !error->ccid)
1824 return;
1825
Ben Widawsky35c20a62013-05-31 11:28:48 -07001826 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001827 if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) {
Ben Widawsky8c123e52013-03-04 17:00:29 -08001828 ering->ctx = i915_error_object_create_sized(dev_priv,
1829 obj, 1);
Damien Lespiau3ef8fb52013-06-24 14:54:50 +01001830 break;
Ben Widawsky8c123e52013-03-04 17:00:29 -08001831 }
1832 }
1833}
1834
Chris Wilson52d39a22012-02-15 11:25:37 +00001835static void i915_gem_record_rings(struct drm_device *dev,
1836 struct drm_i915_error_state *error)
1837{
1838 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001839 struct intel_ring_buffer *ring;
Chris Wilson52d39a22012-02-15 11:25:37 +00001840 struct drm_i915_gem_request *request;
1841 int i, count;
1842
Chris Wilsonb4519512012-05-11 14:29:30 +01001843 for_each_ring(ring, dev_priv, i) {
Chris Wilson52d39a22012-02-15 11:25:37 +00001844 i915_record_ring_state(dev, error, ring);
1845
1846 error->ring[i].batchbuffer =
1847 i915_error_first_batchbuffer(dev_priv, ring);
1848
1849 error->ring[i].ringbuffer =
1850 i915_error_object_create(dev_priv, ring->obj);
1851
Ben Widawsky8c123e52013-03-04 17:00:29 -08001852
1853 i915_gem_record_active_context(ring, error, &error->ring[i]);
1854
Chris Wilson52d39a22012-02-15 11:25:37 +00001855 count = 0;
1856 list_for_each_entry(request, &ring->request_list, list)
1857 count++;
1858
1859 error->ring[i].num_requests = count;
1860 error->ring[i].requests =
1861 kmalloc(count*sizeof(struct drm_i915_error_request),
1862 GFP_ATOMIC);
1863 if (error->ring[i].requests == NULL) {
1864 error->ring[i].num_requests = 0;
1865 continue;
1866 }
1867
1868 count = 0;
1869 list_for_each_entry(request, &ring->request_list, list) {
1870 struct drm_i915_error_request *erq;
1871
1872 erq = &error->ring[i].requests[count++];
1873 erq->seqno = request->seqno;
1874 erq->jiffies = request->emitted_jiffies;
Chris Wilsonee4f42b2012-02-15 11:25:38 +00001875 erq->tail = request->tail;
Chris Wilson52d39a22012-02-15 11:25:37 +00001876 }
1877 }
1878}
1879
Ben Widawsky26b7c222013-06-27 16:30:03 -07001880static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv,
1881 struct drm_i915_error_state *error)
1882{
1883 struct drm_i915_gem_object *obj;
1884 int i;
1885
1886 i = 0;
1887 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1888 i++;
1889 error->active_bo_count = i;
1890 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
1891 if (obj->pin_count)
1892 i++;
1893 error->pinned_bo_count = i - error->active_bo_count;
1894
1895 if (i) {
1896 error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
1897 GFP_ATOMIC);
1898 if (error->active_bo)
1899 error->pinned_bo =
1900 error->active_bo + error->active_bo_count;
1901 }
1902
1903 if (error->active_bo)
1904 error->active_bo_count =
1905 capture_active_bo(error->active_bo,
1906 error->active_bo_count,
1907 &dev_priv->mm.active_list);
1908
1909 if (error->pinned_bo)
1910 error->pinned_bo_count =
1911 capture_pinned_bo(error->pinned_bo,
1912 error->pinned_bo_count,
1913 &dev_priv->mm.bound_list);
1914}
1915
Jesse Barnes8a905232009-07-11 16:48:03 -04001916/**
1917 * i915_capture_error_state - capture an error record for later analysis
1918 * @dev: drm device
1919 *
1920 * Should be called when an error is detected (either a hang or an error
1921 * interrupt) to capture error state from the time of the error. Fills
1922 * out a structure which becomes available in debugfs for user level tools
1923 * to pick up.
1924 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001925static void i915_capture_error_state(struct drm_device *dev)
1926{
1927 struct drm_i915_private *dev_priv = dev->dev_private;
1928 struct drm_i915_error_state *error;
1929 unsigned long flags;
Ben Widawsky26b7c222013-06-27 16:30:03 -07001930 int pipe;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001931
Daniel Vetter99584db2012-11-14 17:14:04 +01001932 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1933 error = dev_priv->gpu_error.first_error;
1934 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001935 if (error)
1936 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001937
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001938 /* Account for pipe specific data like PIPE*STAT */
Daniel Vetter33f3f512011-12-14 13:57:39 +01001939 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001940 if (!error) {
Chris Wilson9df30792010-02-18 10:24:56 +00001941 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1942 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001943 }
1944
Paulo Zanoni5d83d292013-03-06 20:03:22 -03001945 DRM_INFO("capturing error event; look for more information in "
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +03001946 "/sys/class/drm/card%d/error\n", dev->primary->index);
Chris Wilson2fa772f2010-10-01 13:23:27 +01001947
Daniel Vetter742cbee2012-04-27 15:17:39 +02001948 kref_init(&error->ref);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001949 error->eir = I915_READ(EIR);
1950 error->pgtbl_er = I915_READ(PGTBL_ER);
Ben Widawsky211816e2013-02-24 18:10:01 -08001951 if (HAS_HW_CONTEXTS(dev))
1952 error->ccid = I915_READ(CCID);
Ben Widawskybe998e22012-04-26 16:03:00 -07001953
1954 if (HAS_PCH_SPLIT(dev))
1955 error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1956 else if (IS_VALLEYVIEW(dev))
1957 error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1958 else if (IS_GEN2(dev))
1959 error->ier = I915_READ16(IER);
1960 else
1961 error->ier = I915_READ(IER);
1962
Chris Wilson0f3b6842013-01-15 12:05:55 +00001963 if (INTEL_INFO(dev)->gen >= 6)
1964 error->derrmr = I915_READ(DERRMR);
1965
1966 if (IS_VALLEYVIEW(dev))
1967 error->forcewake = I915_READ(FORCEWAKE_VLV);
1968 else if (INTEL_INFO(dev)->gen >= 7)
1969 error->forcewake = I915_READ(FORCEWAKE_MT);
1970 else if (INTEL_INFO(dev)->gen == 6)
1971 error->forcewake = I915_READ(FORCEWAKE);
1972
Paulo Zanoni4f3308b2013-03-22 14:24:16 -03001973 if (!HAS_PCH_SPLIT(dev))
1974 for_each_pipe(pipe)
1975 error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001976
Daniel Vetter33f3f512011-12-14 13:57:39 +01001977 if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilsonf4068392010-10-27 20:36:41 +01001978 error->error = I915_READ(ERROR_GEN6);
Daniel Vetter33f3f512011-12-14 13:57:39 +01001979 error->done_reg = I915_READ(DONE_REG);
1980 }
Chris Wilsonadd354d2010-10-29 19:00:51 +01001981
Ben Widawsky71e172e2012-08-20 16:15:13 -07001982 if (INTEL_INFO(dev)->gen == 7)
1983 error->err_int = I915_READ(GEN7_ERR_INT);
1984
Ben Widawsky050ee912012-08-22 11:32:15 -07001985 i915_get_extra_instdone(dev, error->extra_instdone);
1986
Ben Widawsky26b7c222013-06-27 16:30:03 -07001987 i915_gem_capture_buffers(dev_priv, error);
Chris Wilson748ebc62010-10-24 10:28:47 +01001988 i915_gem_record_fences(dev, error);
Chris Wilson52d39a22012-02-15 11:25:37 +00001989 i915_gem_record_rings(dev, error);
Chris Wilson9df30792010-02-18 10:24:56 +00001990
Jesse Barnes8a905232009-07-11 16:48:03 -04001991 do_gettimeofday(&error->time);
1992
Chris Wilson6ef3d422010-08-04 20:26:07 +01001993 error->overlay = intel_overlay_capture_error_state(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00001994 error->display = intel_display_capture_error_state(dev);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001995
Daniel Vetter99584db2012-11-14 17:14:04 +01001996 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1997 if (dev_priv->gpu_error.first_error == NULL) {
1998 dev_priv->gpu_error.first_error = error;
Chris Wilson9df30792010-02-18 10:24:56 +00001999 error = NULL;
2000 }
Daniel Vetter99584db2012-11-14 17:14:04 +01002001 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00002002
2003 if (error)
Daniel Vetter742cbee2012-04-27 15:17:39 +02002004 i915_error_state_free(&error->ref);
Chris Wilson9df30792010-02-18 10:24:56 +00002005}
2006
2007void i915_destroy_error_state(struct drm_device *dev)
2008{
2009 struct drm_i915_private *dev_priv = dev->dev_private;
2010 struct drm_i915_error_state *error;
Ben Widawsky6dc0e812012-01-23 15:30:02 -08002011 unsigned long flags;
Chris Wilson9df30792010-02-18 10:24:56 +00002012
Daniel Vetter99584db2012-11-14 17:14:04 +01002013 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
2014 error = dev_priv->gpu_error.first_error;
2015 dev_priv->gpu_error.first_error = NULL;
2016 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00002017
2018 if (error)
Daniel Vetter742cbee2012-04-27 15:17:39 +02002019 kref_put(&error->ref, i915_error_state_free);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07002020}
Chris Wilson3bd3c932010-08-19 08:19:30 +01002021#else
2022#define i915_capture_error_state(x)
2023#endif
Jesse Barnes63eeaf32009-06-18 16:56:52 -07002024
Chris Wilson35aed2e2010-05-27 13:18:12 +01002025static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04002026{
2027 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybd9854f2012-08-23 15:18:09 -07002028 uint32_t instdone[I915_NUM_INSTDONE_REG];
Jesse Barnes8a905232009-07-11 16:48:03 -04002029 u32 eir = I915_READ(EIR);
Ben Widawsky050ee912012-08-22 11:32:15 -07002030 int pipe, i;
Jesse Barnes8a905232009-07-11 16:48:03 -04002031
Chris Wilson35aed2e2010-05-27 13:18:12 +01002032 if (!eir)
2033 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04002034
Joe Perchesa70491c2012-03-18 13:00:11 -07002035 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04002036
Ben Widawskybd9854f2012-08-23 15:18:09 -07002037 i915_get_extra_instdone(dev, instdone);
2038
Jesse Barnes8a905232009-07-11 16:48:03 -04002039 if (IS_G4X(dev)) {
2040 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
2041 u32 ipeir = I915_READ(IPEIR_I965);
2042
Joe Perchesa70491c2012-03-18 13:00:11 -07002043 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2044 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Ben Widawsky050ee912012-08-22 11:32:15 -07002045 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2046 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Joe Perchesa70491c2012-03-18 13:00:11 -07002047 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002048 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002049 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002050 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002051 }
2052 if (eir & GM45_ERROR_PAGE_TABLE) {
2053 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002054 pr_err("page table error\n");
2055 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002056 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002057 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002058 }
2059 }
2060
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002061 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002062 if (eir & I915_ERROR_PAGE_TABLE) {
2063 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002064 pr_err("page table error\n");
2065 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002066 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002067 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002068 }
2069 }
2070
2071 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002072 pr_err("memory refresh error:\n");
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002073 for_each_pipe(pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07002074 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002075 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04002076 /* pipestat has already been acked */
2077 }
2078 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002079 pr_err("instruction error\n");
2080 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Ben Widawsky050ee912012-08-22 11:32:15 -07002081 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2082 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002083 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002084 u32 ipeir = I915_READ(IPEIR);
2085
Joe Perchesa70491c2012-03-18 13:00:11 -07002086 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
2087 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
Joe Perchesa70491c2012-03-18 13:00:11 -07002088 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04002089 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002090 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002091 } else {
2092 u32 ipeir = I915_READ(IPEIR_I965);
2093
Joe Perchesa70491c2012-03-18 13:00:11 -07002094 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2095 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07002096 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002097 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002098 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002099 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002100 }
2101 }
2102
2103 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002104 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002105 eir = I915_READ(EIR);
2106 if (eir) {
2107 /*
2108 * some errors might have become stuck,
2109 * mask them.
2110 */
2111 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
2112 I915_WRITE(EMR, I915_READ(EMR) | eir);
2113 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2114 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01002115}
2116
2117/**
2118 * i915_handle_error - handle an error interrupt
2119 * @dev: drm device
2120 *
2121 * Do some basic checking of regsiter state at error interrupt time and
2122 * dump it to the syslog. Also call i915_capture_error_state() to make
2123 * sure we get a record and make it available in debugfs. Fire a uevent
2124 * so userspace knows something bad happened (should trigger collection
2125 * of a ring dump etc.).
2126 */
Chris Wilson527f9e92010-11-11 01:16:58 +00002127void i915_handle_error(struct drm_device *dev, bool wedged)
Chris Wilson35aed2e2010-05-27 13:18:12 +01002128{
2129 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002130 struct intel_ring_buffer *ring;
2131 int i;
Chris Wilson35aed2e2010-05-27 13:18:12 +01002132
2133 i915_capture_error_state(dev);
2134 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04002135
Ben Gamariba1234d2009-09-14 17:48:47 -04002136 if (wedged) {
Daniel Vetterf69061b2012-12-06 09:01:42 +01002137 atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2138 &dev_priv->gpu_error.reset_counter);
Ben Gamariba1234d2009-09-14 17:48:47 -04002139
Ben Gamari11ed50e2009-09-14 17:48:45 -04002140 /*
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002141 * Wakeup waiting processes so that the reset work item
2142 * doesn't deadlock trying to grab various locks.
Ben Gamari11ed50e2009-09-14 17:48:45 -04002143 */
Chris Wilsonb4519512012-05-11 14:29:30 +01002144 for_each_ring(ring, dev_priv, i)
2145 wake_up_all(&ring->irq_queue);
Ben Gamari11ed50e2009-09-14 17:48:45 -04002146 }
2147
Daniel Vetter99584db2012-11-14 17:14:04 +01002148 queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
Jesse Barnes8a905232009-07-11 16:48:03 -04002149}
2150
Ville Syrjälä21ad8332013-02-19 15:16:39 +02002151static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002152{
2153 drm_i915_private_t *dev_priv = dev->dev_private;
2154 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
2155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00002156 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002157 struct intel_unpin_work *work;
2158 unsigned long flags;
2159 bool stall_detected;
2160
2161 /* Ignore early vblank irqs */
2162 if (intel_crtc == NULL)
2163 return;
2164
2165 spin_lock_irqsave(&dev->event_lock, flags);
2166 work = intel_crtc->unpin_work;
2167
Chris Wilsone7d841c2012-12-03 11:36:30 +00002168 if (work == NULL ||
2169 atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2170 !work->enable_stall_check) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002171 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
2172 spin_unlock_irqrestore(&dev->event_lock, flags);
2173 return;
2174 }
2175
2176 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00002177 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002178 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002179 int dspsurf = DSPSURF(intel_crtc->plane);
Armin Reese446f2542012-03-30 16:20:16 -07002180 stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002181 i915_gem_obj_ggtt_offset(obj);
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002182 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002183 int dspaddr = DSPADDR(intel_crtc->plane);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002184 stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002185 crtc->y * crtc->fb->pitches[0] +
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002186 crtc->x * crtc->fb->bits_per_pixel/8);
2187 }
2188
2189 spin_unlock_irqrestore(&dev->event_lock, flags);
2190
2191 if (stall_detected) {
2192 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
2193 intel_prepare_page_flip(dev, intel_crtc->plane);
2194 }
2195}
2196
Keith Packard42f52ef2008-10-18 19:39:29 -07002197/* Called from drm generic code, passed 'crtc' which
2198 * we use as a pipe index
2199 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002200static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002201{
2202 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002203 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002204
Chris Wilson5eddb702010-09-11 13:48:45 +01002205 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002206 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002207
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002208 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002209 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08002210 i915_enable_pipestat(dev_priv, pipe,
2211 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -07002212 else
Keith Packard7c463582008-11-04 02:03:27 -08002213 i915_enable_pipestat(dev_priv, pipe,
2214 PIPE_VBLANK_INTERRUPT_ENABLE);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002215
2216 /* maintain vblank delivery even in deep C-states */
2217 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02002218 I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002219 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002220
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002221 return 0;
2222}
2223
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002224static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002225{
2226 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2227 unsigned long irqflags;
2228
2229 if (!i915_pipe_enabled(dev, pipe))
2230 return -EINVAL;
2231
2232 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2233 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04002234 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002235 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2236
2237 return 0;
2238}
2239
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002240static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002241{
2242 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2243 unsigned long irqflags;
2244
2245 if (!i915_pipe_enabled(dev, pipe))
2246 return -EINVAL;
2247
2248 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilsonb615b572012-05-02 09:52:12 +01002249 ironlake_enable_display_irq(dev_priv,
2250 DE_PIPEA_VBLANK_IVB << (5 * pipe));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002251 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2252
2253 return 0;
2254}
2255
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002256static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
2257{
2258 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2259 unsigned long irqflags;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002260 u32 imr;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002261
2262 if (!i915_pipe_enabled(dev, pipe))
2263 return -EINVAL;
2264
2265 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002266 imr = I915_READ(VLV_IMR);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002267 if (pipe == 0)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002268 imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002269 else
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002270 imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002271 I915_WRITE(VLV_IMR, imr);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002272 i915_enable_pipestat(dev_priv, pipe,
2273 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002274 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2275
2276 return 0;
2277}
2278
Keith Packard42f52ef2008-10-18 19:39:29 -07002279/* Called from drm generic code, passed 'crtc' which
2280 * we use as a pipe index
2281 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002282static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002283{
2284 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002285 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002286
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002287 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002288 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02002289 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson8692d00e2011-02-05 10:08:21 +00002290
Jesse Barnesf796cf82011-04-07 13:58:17 -07002291 i915_disable_pipestat(dev_priv, pipe,
2292 PIPE_VBLANK_INTERRUPT_ENABLE |
2293 PIPE_START_VBLANK_INTERRUPT_ENABLE);
2294 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2295}
2296
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002297static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002298{
2299 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2300 unsigned long irqflags;
2301
2302 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2303 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04002304 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002305 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002306}
2307
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002308static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002309{
2310 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2311 unsigned long irqflags;
2312
2313 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilsonb615b572012-05-02 09:52:12 +01002314 ironlake_disable_display_irq(dev_priv,
2315 DE_PIPEA_VBLANK_IVB << (pipe * 5));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002316 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2317}
2318
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002319static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
2320{
2321 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2322 unsigned long irqflags;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002323 u32 imr;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002324
2325 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002326 i915_disable_pipestat(dev_priv, pipe,
2327 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002328 imr = I915_READ(VLV_IMR);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002329 if (pipe == 0)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002330 imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002331 else
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002332 imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002333 I915_WRITE(VLV_IMR, imr);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002334 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2335}
2336
Chris Wilson893eead2010-10-27 14:44:35 +01002337static u32
2338ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08002339{
Chris Wilson893eead2010-10-27 14:44:35 +01002340 return list_entry(ring->request_list.prev,
2341 struct drm_i915_gem_request, list)->seqno;
2342}
2343
Chris Wilson9107e9d2013-06-10 11:20:20 +01002344static bool
2345ring_idle(struct intel_ring_buffer *ring, u32 seqno)
Chris Wilson893eead2010-10-27 14:44:35 +01002346{
Chris Wilson9107e9d2013-06-10 11:20:20 +01002347 return (list_empty(&ring->request_list) ||
2348 i915_seqno_passed(seqno, ring_last_seqno(ring)));
Ben Gamarif65d9422009-09-14 17:48:44 -04002349}
2350
Chris Wilson6274f212013-06-10 11:20:21 +01002351static struct intel_ring_buffer *
2352semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
Chris Wilsona24a11e2013-03-14 17:52:05 +02002353{
2354 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6274f212013-06-10 11:20:21 +01002355 u32 cmd, ipehr, acthd, acthd_min;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002356
2357 ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2358 if ((ipehr & ~(0x3 << 16)) !=
2359 (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
Chris Wilson6274f212013-06-10 11:20:21 +01002360 return NULL;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002361
2362 /* ACTHD is likely pointing to the dword after the actual command,
2363 * so scan backwards until we find the MBOX.
2364 */
Chris Wilson6274f212013-06-10 11:20:21 +01002365 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002366 acthd_min = max((int)acthd - 3 * 4, 0);
2367 do {
2368 cmd = ioread32(ring->virtual_start + acthd);
2369 if (cmd == ipehr)
2370 break;
2371
2372 acthd -= 4;
2373 if (acthd < acthd_min)
Chris Wilson6274f212013-06-10 11:20:21 +01002374 return NULL;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002375 } while (1);
2376
Chris Wilson6274f212013-06-10 11:20:21 +01002377 *seqno = ioread32(ring->virtual_start+acthd+4)+1;
2378 return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
Chris Wilsona24a11e2013-03-14 17:52:05 +02002379}
2380
Chris Wilson6274f212013-06-10 11:20:21 +01002381static int semaphore_passed(struct intel_ring_buffer *ring)
2382{
2383 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2384 struct intel_ring_buffer *signaller;
2385 u32 seqno, ctl;
2386
2387 ring->hangcheck.deadlock = true;
2388
2389 signaller = semaphore_waits_for(ring, &seqno);
2390 if (signaller == NULL || signaller->hangcheck.deadlock)
2391 return -1;
2392
2393 /* cursory check for an unkickable deadlock */
2394 ctl = I915_READ_CTL(signaller);
2395 if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
2396 return -1;
2397
2398 return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
2399}
2400
2401static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
2402{
2403 struct intel_ring_buffer *ring;
2404 int i;
2405
2406 for_each_ring(ring, dev_priv, i)
2407 ring->hangcheck.deadlock = false;
2408}
2409
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03002410static enum intel_ring_hangcheck_action
2411ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002412{
2413 struct drm_device *dev = ring->dev;
2414 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002415 u32 tmp;
2416
Chris Wilson6274f212013-06-10 11:20:21 +01002417 if (ring->hangcheck.acthd != acthd)
2418 return active;
2419
Chris Wilson9107e9d2013-06-10 11:20:20 +01002420 if (IS_GEN2(dev))
Chris Wilson6274f212013-06-10 11:20:21 +01002421 return hung;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002422
2423 /* Is the chip hanging on a WAIT_FOR_EVENT?
2424 * If so we can simply poke the RB_WAIT bit
2425 * and break the hang. This should work on
2426 * all but the second generation chipsets.
2427 */
2428 tmp = I915_READ_CTL(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002429 if (tmp & RING_WAIT) {
2430 DRM_ERROR("Kicking stuck wait on %s\n",
2431 ring->name);
2432 I915_WRITE_CTL(ring, tmp);
Chris Wilson6274f212013-06-10 11:20:21 +01002433 return kick;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002434 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02002435
Chris Wilson6274f212013-06-10 11:20:21 +01002436 if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
2437 switch (semaphore_passed(ring)) {
2438 default:
2439 return hung;
2440 case 1:
2441 DRM_ERROR("Kicking stuck semaphore on %s\n",
2442 ring->name);
2443 I915_WRITE_CTL(ring, tmp);
2444 return kick;
2445 case 0:
2446 return wait;
2447 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002448 }
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002449
Chris Wilson6274f212013-06-10 11:20:21 +01002450 return hung;
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002451}
2452
Ben Gamarif65d9422009-09-14 17:48:44 -04002453/**
2454 * This is called when the chip hasn't reported back with completed
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002455 * batchbuffers in a long time. We keep track per ring seqno progress and
2456 * if there are no progress, hangcheck score for that ring is increased.
2457 * Further, acthd is inspected to see if the ring is stuck. On stuck case
2458 * we kick the ring. If we see no progress on three subsequent calls
2459 * we assume chip is wedged and try to fix it by resetting the chip.
Ben Gamarif65d9422009-09-14 17:48:44 -04002460 */
2461void i915_hangcheck_elapsed(unsigned long data)
2462{
2463 struct drm_device *dev = (struct drm_device *)data;
2464 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002465 struct intel_ring_buffer *ring;
Chris Wilsonb4519512012-05-11 14:29:30 +01002466 int i;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002467 int busy_count = 0, rings_hung = 0;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002468 bool stuck[I915_NUM_RINGS] = { 0 };
2469#define BUSY 1
2470#define KICK 5
2471#define HUNG 20
2472#define FIRE 30
Chris Wilson893eead2010-10-27 14:44:35 +01002473
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07002474 if (!i915_enable_hangcheck)
2475 return;
2476
Chris Wilsonb4519512012-05-11 14:29:30 +01002477 for_each_ring(ring, dev_priv, i) {
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002478 u32 seqno, acthd;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002479 bool busy = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01002480
Chris Wilson6274f212013-06-10 11:20:21 +01002481 semaphore_clear_deadlocks(dev_priv);
2482
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002483 seqno = ring->get_seqno(ring, false);
2484 acthd = intel_ring_get_active_head(ring);
Chris Wilsond1e61e72012-04-10 17:00:41 +01002485
Chris Wilson9107e9d2013-06-10 11:20:20 +01002486 if (ring->hangcheck.seqno == seqno) {
2487 if (ring_idle(ring, seqno)) {
2488 if (waitqueue_active(&ring->irq_queue)) {
2489 /* Issue a wake-up to catch stuck h/w. */
2490 DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
2491 ring->name);
2492 wake_up_all(&ring->irq_queue);
2493 ring->hangcheck.score += HUNG;
2494 } else
2495 busy = false;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002496 } else {
Chris Wilson9107e9d2013-06-10 11:20:20 +01002497 int score;
2498
Chris Wilson6274f212013-06-10 11:20:21 +01002499 /* We always increment the hangcheck score
2500 * if the ring is busy and still processing
2501 * the same request, so that no single request
2502 * can run indefinitely (such as a chain of
2503 * batches). The only time we do not increment
2504 * the hangcheck score on this ring, if this
2505 * ring is in a legitimate wait for another
2506 * ring. In that case the waiting ring is a
2507 * victim and we want to be sure we catch the
2508 * right culprit. Then every time we do kick
2509 * the ring, add a small increment to the
2510 * score so that we can catch a batch that is
2511 * being repeatedly kicked and so responsible
2512 * for stalling the machine.
2513 */
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03002514 ring->hangcheck.action = ring_stuck(ring,
2515 acthd);
2516
2517 switch (ring->hangcheck.action) {
Chris Wilson6274f212013-06-10 11:20:21 +01002518 case wait:
2519 score = 0;
2520 break;
2521 case active:
Chris Wilson9107e9d2013-06-10 11:20:20 +01002522 score = BUSY;
Chris Wilson6274f212013-06-10 11:20:21 +01002523 break;
2524 case kick:
2525 score = KICK;
2526 break;
2527 case hung:
2528 score = HUNG;
2529 stuck[i] = true;
2530 break;
2531 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002532 ring->hangcheck.score += score;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002533 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002534 } else {
2535 /* Gradually reduce the count so that we catch DoS
2536 * attempts across multiple batches.
2537 */
2538 if (ring->hangcheck.score > 0)
2539 ring->hangcheck.score--;
Chris Wilsond1e61e72012-04-10 17:00:41 +01002540 }
2541
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002542 ring->hangcheck.seqno = seqno;
2543 ring->hangcheck.acthd = acthd;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002544 busy_count += busy;
Chris Wilson893eead2010-10-27 14:44:35 +01002545 }
Eric Anholtb9201c12010-01-08 14:25:16 -08002546
Mika Kuoppala92cab732013-05-24 17:16:07 +03002547 for_each_ring(ring, dev_priv, i) {
Chris Wilson9107e9d2013-06-10 11:20:20 +01002548 if (ring->hangcheck.score > FIRE) {
Ben Widawskyacd78c12013-06-13 21:33:33 -07002549 DRM_ERROR("%s on %s\n",
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002550 stuck[i] ? "stuck" : "no progress",
Chris Wilsona43adf02013-06-10 11:20:22 +01002551 ring->name);
2552 rings_hung++;
Mika Kuoppala92cab732013-05-24 17:16:07 +03002553 }
2554 }
2555
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002556 if (rings_hung)
2557 return i915_handle_error(dev, true);
Ben Gamarif65d9422009-09-14 17:48:44 -04002558
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002559 if (busy_count)
2560 /* Reset timer case chip hangs without another request
2561 * being added */
2562 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2563 round_jiffies_up(jiffies +
2564 DRM_I915_HANGCHECK_JIFFIES));
Ben Gamarif65d9422009-09-14 17:48:44 -04002565}
2566
Paulo Zanoni91738a92013-06-05 14:21:51 -03002567static void ibx_irq_preinstall(struct drm_device *dev)
2568{
2569 struct drm_i915_private *dev_priv = dev->dev_private;
2570
2571 if (HAS_PCH_NOP(dev))
2572 return;
2573
2574 /* south display irq */
2575 I915_WRITE(SDEIMR, 0xffffffff);
2576 /*
2577 * SDEIER is also touched by the interrupt handler to work around missed
2578 * PCH interrupts. Hence we can't update it after the interrupt handler
2579 * is enabled - instead we unconditionally enable all PCH interrupt
2580 * sources here, but then only unmask them as needed with SDEIMR.
2581 */
2582 I915_WRITE(SDEIER, 0xffffffff);
2583 POSTING_READ(SDEIER);
2584}
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586/* drm_dma.h hooks
2587*/
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002588static void ironlake_irq_preinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002589{
2590 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2591
Jesse Barnes46979952011-04-07 13:53:55 -07002592 atomic_set(&dev_priv->irq_received, 0);
2593
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002594 I915_WRITE(HWSTAM, 0xeffe);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01002595
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002596 /* XXX hotplug from PCH */
2597
2598 I915_WRITE(DEIMR, 0xffffffff);
2599 I915_WRITE(DEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002600 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002601
2602 /* and GT */
2603 I915_WRITE(GTIMR, 0xffffffff);
2604 I915_WRITE(GTIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002605 POSTING_READ(GTIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00002606
Paulo Zanoni91738a92013-06-05 14:21:51 -03002607 ibx_irq_preinstall(dev);
Ben Widawsky7d991632013-05-28 19:22:25 -07002608}
2609
2610static void ivybridge_irq_preinstall(struct drm_device *dev)
2611{
2612 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2613
2614 atomic_set(&dev_priv->irq_received, 0);
2615
2616 I915_WRITE(HWSTAM, 0xeffe);
2617
2618 /* XXX hotplug from PCH */
2619
2620 I915_WRITE(DEIMR, 0xffffffff);
2621 I915_WRITE(DEIER, 0x0);
2622 POSTING_READ(DEIER);
2623
2624 /* and GT */
2625 I915_WRITE(GTIMR, 0xffffffff);
2626 I915_WRITE(GTIER, 0x0);
2627 POSTING_READ(GTIER);
2628
Ben Widawskyeda63ff2013-05-28 19:22:26 -07002629 /* Power management */
2630 I915_WRITE(GEN6_PMIMR, 0xffffffff);
2631 I915_WRITE(GEN6_PMIER, 0x0);
2632 POSTING_READ(GEN6_PMIER);
2633
Paulo Zanoni91738a92013-06-05 14:21:51 -03002634 ibx_irq_preinstall(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002635}
2636
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002637static void valleyview_irq_preinstall(struct drm_device *dev)
2638{
2639 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2640 int pipe;
2641
2642 atomic_set(&dev_priv->irq_received, 0);
2643
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002644 /* VLV magic */
2645 I915_WRITE(VLV_IMR, 0);
2646 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
2647 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
2648 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
2649
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002650 /* and GT */
2651 I915_WRITE(GTIIR, I915_READ(GTIIR));
2652 I915_WRITE(GTIIR, I915_READ(GTIIR));
2653 I915_WRITE(GTIMR, 0xffffffff);
2654 I915_WRITE(GTIER, 0x0);
2655 POSTING_READ(GTIER);
2656
2657 I915_WRITE(DPINVGTT, 0xff);
2658
2659 I915_WRITE(PORT_HOTPLUG_EN, 0);
2660 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2661 for_each_pipe(pipe)
2662 I915_WRITE(PIPESTAT(pipe), 0xffff);
2663 I915_WRITE(VLV_IIR, 0xffffffff);
2664 I915_WRITE(VLV_IMR, 0xffffffff);
2665 I915_WRITE(VLV_IER, 0x0);
2666 POSTING_READ(VLV_IER);
2667}
2668
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002669static void ibx_hpd_irq_setup(struct drm_device *dev)
Keith Packard7fe0b972011-09-19 13:31:02 -07002670{
2671 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002672 struct drm_mode_config *mode_config = &dev->mode_config;
2673 struct intel_encoder *intel_encoder;
Daniel Vetterfee884e2013-07-04 23:35:21 +02002674 u32 hotplug_irqs, hotplug, enabled_irqs = 0;
Keith Packard7fe0b972011-09-19 13:31:02 -07002675
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002676 if (HAS_PCH_IBX(dev)) {
Daniel Vetterfee884e2013-07-04 23:35:21 +02002677 hotplug_irqs = SDE_HOTPLUG_MASK;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002678 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
Egbert Eichcd569ae2013-04-16 13:36:57 +02002679 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02002680 enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002681 } else {
Daniel Vetterfee884e2013-07-04 23:35:21 +02002682 hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002683 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
Egbert Eichcd569ae2013-04-16 13:36:57 +02002684 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02002685 enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002686 }
2687
Daniel Vetterfee884e2013-07-04 23:35:21 +02002688 ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002689
2690 /*
2691 * Enable digital hotplug on the PCH, and configure the DP short pulse
2692 * duration to 2ms (which is the minimum in the Display Port spec)
2693 *
2694 * This register is the same on all known PCH chips.
2695 */
Keith Packard7fe0b972011-09-19 13:31:02 -07002696 hotplug = I915_READ(PCH_PORT_HOTPLUG);
2697 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
2698 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
2699 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
2700 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
2701 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
2702}
2703
Paulo Zanonid46da432013-02-08 17:35:15 -02002704static void ibx_irq_postinstall(struct drm_device *dev)
2705{
2706 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002707 u32 mask;
Paulo Zanonid46da432013-02-08 17:35:15 -02002708
Daniel Vetter692a04c2013-05-29 21:43:05 +02002709 if (HAS_PCH_NOP(dev))
2710 return;
2711
Paulo Zanoni86642812013-04-12 17:57:57 -03002712 if (HAS_PCH_IBX(dev)) {
2713 mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
Paulo Zanonide032bf2013-04-12 17:57:58 -03002714 SDE_TRANSA_FIFO_UNDER | SDE_POISON;
Paulo Zanoni86642812013-04-12 17:57:57 -03002715 } else {
2716 mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
2717
2718 I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2719 }
Ben Widawskyab5c6082013-04-05 13:12:41 -07002720
Paulo Zanonid46da432013-02-08 17:35:15 -02002721 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2722 I915_WRITE(SDEIMR, ~mask);
Paulo Zanonid46da432013-02-08 17:35:15 -02002723}
2724
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002725static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002726{
Daniel Vetter4bc9d432013-06-27 13:44:58 +02002727 unsigned long irqflags;
2728
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002729 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2730 /* enable kind of interrupts always enabled */
Jesse Barnes013d5aa2010-01-29 11:18:31 -08002731 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
Daniel Vetterce99c252012-12-01 13:53:47 +01002732 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
Paulo Zanoni86642812013-04-12 17:57:57 -03002733 DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
Paulo Zanonide032bf2013-04-12 17:57:58 -03002734 DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
Ben Widawskycc609d52013-05-28 19:22:29 -07002735 u32 gt_irqs;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002736
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002737 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002738
2739 /* should always can generate irq */
2740 I915_WRITE(DEIIR, I915_READ(DEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002741 I915_WRITE(DEIMR, dev_priv->irq_mask);
Daniel Vetter6005ce42013-06-27 13:44:59 +02002742 I915_WRITE(DEIER, display_mask |
2743 DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002744 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002745
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002746 dev_priv->gt_irq_mask = ~0;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002747
2748 I915_WRITE(GTIIR, I915_READ(GTIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002749 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002750
Ben Widawskycc609d52013-05-28 19:22:29 -07002751 gt_irqs = GT_RENDER_USER_INTERRUPT;
2752
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002753 if (IS_GEN6(dev))
Ben Widawskycc609d52013-05-28 19:22:29 -07002754 gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002755 else
Ben Widawskycc609d52013-05-28 19:22:29 -07002756 gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
2757 ILK_BSD_USER_INTERRUPT;
2758
2759 I915_WRITE(GTIER, gt_irqs);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002760 POSTING_READ(GTIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002761
Paulo Zanonid46da432013-02-08 17:35:15 -02002762 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07002763
Jesse Barnesf97108d2010-01-29 11:27:07 -08002764 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter6005ce42013-06-27 13:44:59 +02002765 /* Enable PCU event interrupts
2766 *
2767 * spinlocking not required here for correctness since interrupt
Daniel Vetter4bc9d432013-06-27 13:44:58 +02002768 * setup is guaranteed to run in single-threaded context. But we
2769 * need it to make the assert_spin_locked happy. */
2770 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf97108d2010-01-29 11:27:07 -08002771 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
Daniel Vetter4bc9d432013-06-27 13:44:58 +02002772 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnesf97108d2010-01-29 11:27:07 -08002773 }
2774
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002775 return 0;
2776}
2777
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002778static int ivybridge_irq_postinstall(struct drm_device *dev)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002779{
2780 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2781 /* enable kind of interrupts always enabled */
Chris Wilsonb615b572012-05-02 09:52:12 +01002782 u32 display_mask =
2783 DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2784 DE_PLANEC_FLIP_DONE_IVB |
2785 DE_PLANEB_FLIP_DONE_IVB |
Daniel Vetterce99c252012-12-01 13:53:47 +01002786 DE_PLANEA_FLIP_DONE_IVB |
Paulo Zanoni86642812013-04-12 17:57:57 -03002787 DE_AUX_CHANNEL_A_IVB |
2788 DE_ERR_INT_IVB;
Ben Widawsky12638c52013-05-28 19:22:31 -07002789 u32 pm_irqs = GEN6_PM_RPS_EVENTS;
Ben Widawskycc609d52013-05-28 19:22:29 -07002790 u32 gt_irqs;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002791
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002792 dev_priv->irq_mask = ~display_mask;
2793
2794 /* should always can generate irq */
Paulo Zanoni86642812013-04-12 17:57:57 -03002795 I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002796 I915_WRITE(DEIIR, I915_READ(DEIIR));
2797 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilsonb615b572012-05-02 09:52:12 +01002798 I915_WRITE(DEIER,
2799 display_mask |
2800 DE_PIPEC_VBLANK_IVB |
2801 DE_PIPEB_VBLANK_IVB |
2802 DE_PIPEA_VBLANK_IVB);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002803 POSTING_READ(DEIER);
2804
Ben Widawskycc609d52013-05-28 19:22:29 -07002805 dev_priv->gt_irq_mask = ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002806
2807 I915_WRITE(GTIIR, I915_READ(GTIIR));
2808 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2809
Ben Widawskycc609d52013-05-28 19:22:29 -07002810 gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2811 GT_BLT_USER_INTERRUPT | GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2812 I915_WRITE(GTIER, gt_irqs);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002813 POSTING_READ(GTIER);
2814
Ben Widawsky12638c52013-05-28 19:22:31 -07002815 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2816 if (HAS_VEBOX(dev))
2817 pm_irqs |= PM_VEBOX_USER_INTERRUPT |
2818 PM_VEBOX_CS_ERROR_INTERRUPT;
2819
2820 /* Our enable/disable rps functions may touch these registers so
2821 * make sure to set a known state for only the non-RPS bits.
2822 * The RMW is extra paranoia since this should be called after being set
2823 * to a known state in preinstall.
2824 * */
2825 I915_WRITE(GEN6_PMIMR,
2826 (I915_READ(GEN6_PMIMR) | ~GEN6_PM_RPS_EVENTS) & ~pm_irqs);
2827 I915_WRITE(GEN6_PMIER,
2828 (I915_READ(GEN6_PMIER) & GEN6_PM_RPS_EVENTS) | pm_irqs);
2829 POSTING_READ(GEN6_PMIER);
Ben Widawskyeda63ff2013-05-28 19:22:26 -07002830
Paulo Zanonid46da432013-02-08 17:35:15 -02002831 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07002832
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002833 return 0;
2834}
2835
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002836static int valleyview_irq_postinstall(struct drm_device *dev)
2837{
2838 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Ben Widawskycc609d52013-05-28 19:22:29 -07002839 u32 gt_irqs;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002840 u32 enable_mask;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002841 u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
Daniel Vetterb79480b2013-06-27 17:52:10 +02002842 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002843
2844 enable_mask = I915_DISPLAY_PORT_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002845 enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2846 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2847 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002848 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
2849
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002850 /*
2851 *Leave vblank interrupts masked initially. enable/disable will
2852 * toggle them based on usage.
2853 */
2854 dev_priv->irq_mask = (~enable_mask) |
2855 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2856 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002857
Daniel Vetter20afbda2012-12-11 14:05:07 +01002858 I915_WRITE(PORT_HOTPLUG_EN, 0);
2859 POSTING_READ(PORT_HOTPLUG_EN);
2860
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002861 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
2862 I915_WRITE(VLV_IER, enable_mask);
2863 I915_WRITE(VLV_IIR, 0xffffffff);
2864 I915_WRITE(PIPESTAT(0), 0xffff);
2865 I915_WRITE(PIPESTAT(1), 0xffff);
2866 POSTING_READ(VLV_IER);
2867
Daniel Vetterb79480b2013-06-27 17:52:10 +02002868 /* Interrupt setup is already guaranteed to be single-threaded, this is
2869 * just to make the assert_spin_locked check happy. */
2870 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002871 i915_enable_pipestat(dev_priv, 0, pipestat_enable);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01002872 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002873 i915_enable_pipestat(dev_priv, 1, pipestat_enable);
Daniel Vetterb79480b2013-06-27 17:52:10 +02002874 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002875
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002876 I915_WRITE(VLV_IIR, 0xffffffff);
2877 I915_WRITE(VLV_IIR, 0xffffffff);
2878
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002879 I915_WRITE(GTIIR, I915_READ(GTIIR));
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002880 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Jesse Barnes3bcedbe2012-09-19 13:29:01 -07002881
Ben Widawskycc609d52013-05-28 19:22:29 -07002882 gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2883 GT_BLT_USER_INTERRUPT;
2884 I915_WRITE(GTIER, gt_irqs);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002885 POSTING_READ(GTIER);
2886
2887 /* ack & enable invalid PTE error interrupts */
2888#if 0 /* FIXME: add support to irq handler for checking these bits */
2889 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
2890 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
2891#endif
2892
2893 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
Daniel Vetter20afbda2012-12-11 14:05:07 +01002894
2895 return 0;
2896}
2897
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002898static void valleyview_irq_uninstall(struct drm_device *dev)
2899{
2900 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2901 int pipe;
2902
2903 if (!dev_priv)
2904 return;
2905
Egbert Eichac4c16c2013-04-16 13:36:58 +02002906 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2907
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002908 for_each_pipe(pipe)
2909 I915_WRITE(PIPESTAT(pipe), 0xffff);
2910
2911 I915_WRITE(HWSTAM, 0xffffffff);
2912 I915_WRITE(PORT_HOTPLUG_EN, 0);
2913 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2914 for_each_pipe(pipe)
2915 I915_WRITE(PIPESTAT(pipe), 0xffff);
2916 I915_WRITE(VLV_IIR, 0xffffffff);
2917 I915_WRITE(VLV_IMR, 0xffffffff);
2918 I915_WRITE(VLV_IER, 0x0);
2919 POSTING_READ(VLV_IER);
2920}
2921
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002922static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002923{
2924 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07002925
2926 if (!dev_priv)
2927 return;
2928
Egbert Eichac4c16c2013-04-16 13:36:58 +02002929 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2930
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002931 I915_WRITE(HWSTAM, 0xffffffff);
2932
2933 I915_WRITE(DEIMR, 0xffffffff);
2934 I915_WRITE(DEIER, 0x0);
2935 I915_WRITE(DEIIR, I915_READ(DEIIR));
Paulo Zanoni86642812013-04-12 17:57:57 -03002936 if (IS_GEN7(dev))
2937 I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002938
2939 I915_WRITE(GTIMR, 0xffffffff);
2940 I915_WRITE(GTIER, 0x0);
2941 I915_WRITE(GTIIR, I915_READ(GTIIR));
Keith Packard192aac1f2011-09-20 10:12:44 -07002942
Ben Widawskyab5c6082013-04-05 13:12:41 -07002943 if (HAS_PCH_NOP(dev))
2944 return;
2945
Keith Packard192aac1f2011-09-20 10:12:44 -07002946 I915_WRITE(SDEIMR, 0xffffffff);
2947 I915_WRITE(SDEIER, 0x0);
2948 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Paulo Zanoni86642812013-04-12 17:57:57 -03002949 if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2950 I915_WRITE(SERR_INT, I915_READ(SERR_INT));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002951}
2952
Chris Wilsonc2798b12012-04-22 21:13:57 +01002953static void i8xx_irq_preinstall(struct drm_device * dev)
2954{
2955 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2956 int pipe;
2957
2958 atomic_set(&dev_priv->irq_received, 0);
2959
2960 for_each_pipe(pipe)
2961 I915_WRITE(PIPESTAT(pipe), 0);
2962 I915_WRITE16(IMR, 0xffff);
2963 I915_WRITE16(IER, 0x0);
2964 POSTING_READ16(IER);
2965}
2966
2967static int i8xx_irq_postinstall(struct drm_device *dev)
2968{
2969 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2970
Chris Wilsonc2798b12012-04-22 21:13:57 +01002971 I915_WRITE16(EMR,
2972 ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2973
2974 /* Unmask the interrupts that we always want on. */
2975 dev_priv->irq_mask =
2976 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2977 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2978 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2979 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2980 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2981 I915_WRITE16(IMR, dev_priv->irq_mask);
2982
2983 I915_WRITE16(IER,
2984 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2985 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2986 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2987 I915_USER_INTERRUPT);
2988 POSTING_READ16(IER);
2989
2990 return 0;
2991}
2992
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002993/*
2994 * Returns true when a page flip has completed.
2995 */
2996static bool i8xx_handle_vblank(struct drm_device *dev,
2997 int pipe, u16 iir)
2998{
2999 drm_i915_private_t *dev_priv = dev->dev_private;
3000 u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
3001
3002 if (!drm_handle_vblank(dev, pipe))
3003 return false;
3004
3005 if ((iir & flip_pending) == 0)
3006 return false;
3007
3008 intel_prepare_page_flip(dev, pipe);
3009
3010 /* We detect FlipDone by looking for the change in PendingFlip from '1'
3011 * to '0' on the following vblank, i.e. IIR has the Pendingflip
3012 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
3013 * the flip is completed (no longer pending). Since this doesn't raise
3014 * an interrupt per se, we watch for the change at vblank.
3015 */
3016 if (I915_READ16(ISR) & flip_pending)
3017 return false;
3018
3019 intel_finish_page_flip(dev, pipe);
3020
3021 return true;
3022}
3023
Daniel Vetterff1f5252012-10-02 15:10:55 +02003024static irqreturn_t i8xx_irq_handler(int irq, void *arg)
Chris Wilsonc2798b12012-04-22 21:13:57 +01003025{
3026 struct drm_device *dev = (struct drm_device *) arg;
3027 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003028 u16 iir, new_iir;
3029 u32 pipe_stats[2];
3030 unsigned long irqflags;
3031 int irq_received;
3032 int pipe;
3033 u16 flip_mask =
3034 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3035 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3036
3037 atomic_inc(&dev_priv->irq_received);
3038
3039 iir = I915_READ16(IIR);
3040 if (iir == 0)
3041 return IRQ_NONE;
3042
3043 while (iir & ~flip_mask) {
3044 /* Can't rely on pipestat interrupt bit in iir as it might
3045 * have been cleared after the pipestat interrupt was received.
3046 * It doesn't set the bit in iir again, but it still produces
3047 * interrupts (for non-MSI).
3048 */
3049 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3050 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3051 i915_handle_error(dev, false);
3052
3053 for_each_pipe(pipe) {
3054 int reg = PIPESTAT(pipe);
3055 pipe_stats[pipe] = I915_READ(reg);
3056
3057 /*
3058 * Clear the PIPE*STAT regs before the IIR
3059 */
3060 if (pipe_stats[pipe] & 0x8000ffff) {
3061 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3062 DRM_DEBUG_DRIVER("pipe %c underrun\n",
3063 pipe_name(pipe));
3064 I915_WRITE(reg, pipe_stats[pipe]);
3065 irq_received = 1;
3066 }
3067 }
3068 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3069
3070 I915_WRITE16(IIR, iir & ~flip_mask);
3071 new_iir = I915_READ16(IIR); /* Flush posted writes */
3072
Daniel Vetterd05c6172012-04-26 23:28:09 +02003073 i915_update_dri1_breadcrumb(dev);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003074
3075 if (iir & I915_USER_INTERRUPT)
3076 notify_ring(dev, &dev_priv->ring[RCS]);
3077
3078 if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003079 i8xx_handle_vblank(dev, 0, iir))
3080 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003081
3082 if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003083 i8xx_handle_vblank(dev, 1, iir))
3084 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
Chris Wilsonc2798b12012-04-22 21:13:57 +01003085
3086 iir = new_iir;
3087 }
3088
3089 return IRQ_HANDLED;
3090}
3091
3092static void i8xx_irq_uninstall(struct drm_device * dev)
3093{
3094 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3095 int pipe;
3096
Chris Wilsonc2798b12012-04-22 21:13:57 +01003097 for_each_pipe(pipe) {
3098 /* Clear enable bits; then clear status bits */
3099 I915_WRITE(PIPESTAT(pipe), 0);
3100 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3101 }
3102 I915_WRITE16(IMR, 0xffff);
3103 I915_WRITE16(IER, 0x0);
3104 I915_WRITE16(IIR, I915_READ16(IIR));
3105}
3106
Chris Wilsona266c7d2012-04-24 22:59:44 +01003107static void i915_irq_preinstall(struct drm_device * dev)
3108{
3109 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3110 int pipe;
3111
3112 atomic_set(&dev_priv->irq_received, 0);
3113
3114 if (I915_HAS_HOTPLUG(dev)) {
3115 I915_WRITE(PORT_HOTPLUG_EN, 0);
3116 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3117 }
3118
Chris Wilson00d98eb2012-04-24 22:59:48 +01003119 I915_WRITE16(HWSTAM, 0xeffe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003120 for_each_pipe(pipe)
3121 I915_WRITE(PIPESTAT(pipe), 0);
3122 I915_WRITE(IMR, 0xffffffff);
3123 I915_WRITE(IER, 0x0);
3124 POSTING_READ(IER);
3125}
3126
3127static int i915_irq_postinstall(struct drm_device *dev)
3128{
3129 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson38bde182012-04-24 22:59:50 +01003130 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003131
Chris Wilson38bde182012-04-24 22:59:50 +01003132 I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3133
3134 /* Unmask the interrupts that we always want on. */
3135 dev_priv->irq_mask =
3136 ~(I915_ASLE_INTERRUPT |
3137 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3138 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3139 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3140 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3141 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3142
3143 enable_mask =
3144 I915_ASLE_INTERRUPT |
3145 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3146 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3147 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3148 I915_USER_INTERRUPT;
3149
Chris Wilsona266c7d2012-04-24 22:59:44 +01003150 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetter20afbda2012-12-11 14:05:07 +01003151 I915_WRITE(PORT_HOTPLUG_EN, 0);
3152 POSTING_READ(PORT_HOTPLUG_EN);
3153
Chris Wilsona266c7d2012-04-24 22:59:44 +01003154 /* Enable in IER... */
3155 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3156 /* and unmask in IMR */
3157 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3158 }
3159
Chris Wilsona266c7d2012-04-24 22:59:44 +01003160 I915_WRITE(IMR, dev_priv->irq_mask);
3161 I915_WRITE(IER, enable_mask);
3162 POSTING_READ(IER);
3163
Jani Nikulaf49e38d2013-04-29 13:02:54 +03003164 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003165
3166 return 0;
3167}
3168
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003169/*
3170 * Returns true when a page flip has completed.
3171 */
3172static bool i915_handle_vblank(struct drm_device *dev,
3173 int plane, int pipe, u32 iir)
3174{
3175 drm_i915_private_t *dev_priv = dev->dev_private;
3176 u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
3177
3178 if (!drm_handle_vblank(dev, pipe))
3179 return false;
3180
3181 if ((iir & flip_pending) == 0)
3182 return false;
3183
3184 intel_prepare_page_flip(dev, plane);
3185
3186 /* We detect FlipDone by looking for the change in PendingFlip from '1'
3187 * to '0' on the following vblank, i.e. IIR has the Pendingflip
3188 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
3189 * the flip is completed (no longer pending). Since this doesn't raise
3190 * an interrupt per se, we watch for the change at vblank.
3191 */
3192 if (I915_READ(ISR) & flip_pending)
3193 return false;
3194
3195 intel_finish_page_flip(dev, pipe);
3196
3197 return true;
3198}
3199
Daniel Vetterff1f5252012-10-02 15:10:55 +02003200static irqreturn_t i915_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003201{
3202 struct drm_device *dev = (struct drm_device *) arg;
3203 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson8291ee92012-04-24 22:59:47 +01003204 u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01003205 unsigned long irqflags;
Chris Wilson38bde182012-04-24 22:59:50 +01003206 u32 flip_mask =
3207 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3208 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilson38bde182012-04-24 22:59:50 +01003209 int pipe, ret = IRQ_NONE;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003210
3211 atomic_inc(&dev_priv->irq_received);
3212
3213 iir = I915_READ(IIR);
Chris Wilson38bde182012-04-24 22:59:50 +01003214 do {
3215 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson8291ee92012-04-24 22:59:47 +01003216 bool blc_event = false;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003217
3218 /* Can't rely on pipestat interrupt bit in iir as it might
3219 * have been cleared after the pipestat interrupt was received.
3220 * It doesn't set the bit in iir again, but it still produces
3221 * interrupts (for non-MSI).
3222 */
3223 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3224 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3225 i915_handle_error(dev, false);
3226
3227 for_each_pipe(pipe) {
3228 int reg = PIPESTAT(pipe);
3229 pipe_stats[pipe] = I915_READ(reg);
3230
Chris Wilson38bde182012-04-24 22:59:50 +01003231 /* Clear the PIPE*STAT regs before the IIR */
Chris Wilsona266c7d2012-04-24 22:59:44 +01003232 if (pipe_stats[pipe] & 0x8000ffff) {
3233 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3234 DRM_DEBUG_DRIVER("pipe %c underrun\n",
3235 pipe_name(pipe));
3236 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilson38bde182012-04-24 22:59:50 +01003237 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003238 }
3239 }
3240 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3241
3242 if (!irq_received)
3243 break;
3244
Chris Wilsona266c7d2012-04-24 22:59:44 +01003245 /* Consume port. Then clear IIR or we'll miss events */
3246 if ((I915_HAS_HOTPLUG(dev)) &&
3247 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3248 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
Egbert Eichb543fb02013-04-16 13:36:54 +02003249 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003250
3251 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3252 hotplug_status);
Daniel Vetter91d131d2013-06-27 17:52:14 +02003253
3254 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
3255
Chris Wilsona266c7d2012-04-24 22:59:44 +01003256 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
Chris Wilson38bde182012-04-24 22:59:50 +01003257 POSTING_READ(PORT_HOTPLUG_STAT);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003258 }
3259
Chris Wilson38bde182012-04-24 22:59:50 +01003260 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003261 new_iir = I915_READ(IIR); /* Flush posted writes */
3262
Chris Wilsona266c7d2012-04-24 22:59:44 +01003263 if (iir & I915_USER_INTERRUPT)
3264 notify_ring(dev, &dev_priv->ring[RCS]);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003265
Chris Wilsona266c7d2012-04-24 22:59:44 +01003266 for_each_pipe(pipe) {
Chris Wilson38bde182012-04-24 22:59:50 +01003267 int plane = pipe;
3268 if (IS_MOBILE(dev))
3269 plane = !plane;
Ville Syrjälä5e2032d2013-02-19 15:16:38 +02003270
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003271 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
3272 i915_handle_vblank(dev, plane, pipe, iir))
3273 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003274
3275 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3276 blc_event = true;
3277 }
3278
Chris Wilsona266c7d2012-04-24 22:59:44 +01003279 if (blc_event || (iir & I915_ASLE_INTERRUPT))
3280 intel_opregion_asle_intr(dev);
3281
3282 /* With MSI, interrupts are only generated when iir
3283 * transitions from zero to nonzero. If another bit got
3284 * set while we were handling the existing iir bits, then
3285 * we would never get another interrupt.
3286 *
3287 * This is fine on non-MSI as well, as if we hit this path
3288 * we avoid exiting the interrupt handler only to generate
3289 * another one.
3290 *
3291 * Note that for MSI this could cause a stray interrupt report
3292 * if an interrupt landed in the time between writing IIR and
3293 * the posting read. This should be rare enough to never
3294 * trigger the 99% of 100,000 interrupts test for disabling
3295 * stray interrupts.
3296 */
Chris Wilson38bde182012-04-24 22:59:50 +01003297 ret = IRQ_HANDLED;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003298 iir = new_iir;
Chris Wilson38bde182012-04-24 22:59:50 +01003299 } while (iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003300
Daniel Vetterd05c6172012-04-26 23:28:09 +02003301 i915_update_dri1_breadcrumb(dev);
Chris Wilson8291ee92012-04-24 22:59:47 +01003302
Chris Wilsona266c7d2012-04-24 22:59:44 +01003303 return ret;
3304}
3305
3306static void i915_irq_uninstall(struct drm_device * dev)
3307{
3308 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3309 int pipe;
3310
Egbert Eichac4c16c2013-04-16 13:36:58 +02003311 del_timer_sync(&dev_priv->hotplug_reenable_timer);
3312
Chris Wilsona266c7d2012-04-24 22:59:44 +01003313 if (I915_HAS_HOTPLUG(dev)) {
3314 I915_WRITE(PORT_HOTPLUG_EN, 0);
3315 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3316 }
3317
Chris Wilson00d98eb2012-04-24 22:59:48 +01003318 I915_WRITE16(HWSTAM, 0xffff);
Chris Wilson55b39752012-04-24 22:59:49 +01003319 for_each_pipe(pipe) {
3320 /* Clear enable bits; then clear status bits */
Chris Wilsona266c7d2012-04-24 22:59:44 +01003321 I915_WRITE(PIPESTAT(pipe), 0);
Chris Wilson55b39752012-04-24 22:59:49 +01003322 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3323 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01003324 I915_WRITE(IMR, 0xffffffff);
3325 I915_WRITE(IER, 0x0);
3326
Chris Wilsona266c7d2012-04-24 22:59:44 +01003327 I915_WRITE(IIR, I915_READ(IIR));
3328}
3329
3330static void i965_irq_preinstall(struct drm_device * dev)
3331{
3332 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3333 int pipe;
3334
3335 atomic_set(&dev_priv->irq_received, 0);
3336
Chris Wilsonadca4732012-05-11 18:01:31 +01003337 I915_WRITE(PORT_HOTPLUG_EN, 0);
3338 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01003339
3340 I915_WRITE(HWSTAM, 0xeffe);
3341 for_each_pipe(pipe)
3342 I915_WRITE(PIPESTAT(pipe), 0);
3343 I915_WRITE(IMR, 0xffffffff);
3344 I915_WRITE(IER, 0x0);
3345 POSTING_READ(IER);
3346}
3347
3348static int i965_irq_postinstall(struct drm_device *dev)
3349{
3350 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonbbba0a92012-04-24 22:59:51 +01003351 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003352 u32 error_mask;
Daniel Vetterb79480b2013-06-27 17:52:10 +02003353 unsigned long irqflags;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003354
Chris Wilsona266c7d2012-04-24 22:59:44 +01003355 /* Unmask the interrupts that we always want on. */
Chris Wilsonbbba0a92012-04-24 22:59:51 +01003356 dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
Chris Wilsonadca4732012-05-11 18:01:31 +01003357 I915_DISPLAY_PORT_INTERRUPT |
Chris Wilsonbbba0a92012-04-24 22:59:51 +01003358 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3359 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3360 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3361 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3362 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3363
3364 enable_mask = ~dev_priv->irq_mask;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003365 enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3366 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
Chris Wilsonbbba0a92012-04-24 22:59:51 +01003367 enable_mask |= I915_USER_INTERRUPT;
3368
3369 if (IS_G4X(dev))
3370 enable_mask |= I915_BSD_USER_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003371
Daniel Vetterb79480b2013-06-27 17:52:10 +02003372 /* Interrupt setup is already guaranteed to be single-threaded, this is
3373 * just to make the assert_spin_locked check happy. */
3374 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01003375 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
Daniel Vetterb79480b2013-06-27 17:52:10 +02003376 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003377
Chris Wilsona266c7d2012-04-24 22:59:44 +01003378 /*
3379 * Enable some error detection, note the instruction error mask
3380 * bit is reserved, so we leave it masked.
3381 */
3382 if (IS_G4X(dev)) {
3383 error_mask = ~(GM45_ERROR_PAGE_TABLE |
3384 GM45_ERROR_MEM_PRIV |
3385 GM45_ERROR_CP_PRIV |
3386 I915_ERROR_MEMORY_REFRESH);
3387 } else {
3388 error_mask = ~(I915_ERROR_PAGE_TABLE |
3389 I915_ERROR_MEMORY_REFRESH);
3390 }
3391 I915_WRITE(EMR, error_mask);
3392
3393 I915_WRITE(IMR, dev_priv->irq_mask);
3394 I915_WRITE(IER, enable_mask);
3395 POSTING_READ(IER);
3396
Daniel Vetter20afbda2012-12-11 14:05:07 +01003397 I915_WRITE(PORT_HOTPLUG_EN, 0);
3398 POSTING_READ(PORT_HOTPLUG_EN);
3399
Jani Nikulaf49e38d2013-04-29 13:02:54 +03003400 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003401
3402 return 0;
3403}
3404
Egbert Eichbac56d52013-02-25 12:06:51 -05003405static void i915_hpd_irq_setup(struct drm_device *dev)
Daniel Vetter20afbda2012-12-11 14:05:07 +01003406{
3407 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Egbert Eiche5868a32013-02-28 04:17:12 -05003408 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +02003409 struct intel_encoder *intel_encoder;
Daniel Vetter20afbda2012-12-11 14:05:07 +01003410 u32 hotplug_en;
3411
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02003412 assert_spin_locked(&dev_priv->irq_lock);
3413
Egbert Eichbac56d52013-02-25 12:06:51 -05003414 if (I915_HAS_HOTPLUG(dev)) {
3415 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3416 hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3417 /* Note HDMI and DP share hotplug bits */
Egbert Eiche5868a32013-02-28 04:17:12 -05003418 /* enable bits are the same for all generations */
Egbert Eichcd569ae2013-04-16 13:36:57 +02003419 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3420 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3421 hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
Egbert Eichbac56d52013-02-25 12:06:51 -05003422 /* Programming the CRT detection parameters tends
3423 to generate a spurious hotplug event about three
3424 seconds later. So just do it once.
3425 */
3426 if (IS_G4X(dev))
3427 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
Daniel Vetter85fc95b2013-03-27 15:47:11 +01003428 hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
Egbert Eichbac56d52013-02-25 12:06:51 -05003429 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003430
Egbert Eichbac56d52013-02-25 12:06:51 -05003431 /* Ignore TV since it's buggy */
3432 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3433 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01003434}
3435
Daniel Vetterff1f5252012-10-02 15:10:55 +02003436static irqreturn_t i965_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003437{
3438 struct drm_device *dev = (struct drm_device *) arg;
3439 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003440 u32 iir, new_iir;
3441 u32 pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01003442 unsigned long irqflags;
3443 int irq_received;
3444 int ret = IRQ_NONE, pipe;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003445 u32 flip_mask =
3446 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3447 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003448
3449 atomic_inc(&dev_priv->irq_received);
3450
3451 iir = I915_READ(IIR);
3452
Chris Wilsona266c7d2012-04-24 22:59:44 +01003453 for (;;) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01003454 bool blc_event = false;
3455
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003456 irq_received = (iir & ~flip_mask) != 0;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003457
3458 /* Can't rely on pipestat interrupt bit in iir as it might
3459 * have been cleared after the pipestat interrupt was received.
3460 * It doesn't set the bit in iir again, but it still produces
3461 * interrupts (for non-MSI).
3462 */
3463 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3464 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3465 i915_handle_error(dev, false);
3466
3467 for_each_pipe(pipe) {
3468 int reg = PIPESTAT(pipe);
3469 pipe_stats[pipe] = I915_READ(reg);
3470
3471 /*
3472 * Clear the PIPE*STAT regs before the IIR
3473 */
3474 if (pipe_stats[pipe] & 0x8000ffff) {
3475 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3476 DRM_DEBUG_DRIVER("pipe %c underrun\n",
3477 pipe_name(pipe));
3478 I915_WRITE(reg, pipe_stats[pipe]);
3479 irq_received = 1;
3480 }
3481 }
3482 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3483
3484 if (!irq_received)
3485 break;
3486
3487 ret = IRQ_HANDLED;
3488
3489 /* Consume port. Then clear IIR or we'll miss events */
Chris Wilsonadca4732012-05-11 18:01:31 +01003490 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003491 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
Egbert Eichb543fb02013-04-16 13:36:54 +02003492 u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3493 HOTPLUG_INT_STATUS_G4X :
Daniel Vetter4f7fd702013-06-24 21:33:28 +02003494 HOTPLUG_INT_STATUS_I915);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003495
3496 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3497 hotplug_status);
Daniel Vetter91d131d2013-06-27 17:52:14 +02003498
3499 intel_hpd_irq_handler(dev, hotplug_trigger,
3500 IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
3501
Chris Wilsona266c7d2012-04-24 22:59:44 +01003502 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3503 I915_READ(PORT_HOTPLUG_STAT);
3504 }
3505
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003506 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003507 new_iir = I915_READ(IIR); /* Flush posted writes */
3508
Chris Wilsona266c7d2012-04-24 22:59:44 +01003509 if (iir & I915_USER_INTERRUPT)
3510 notify_ring(dev, &dev_priv->ring[RCS]);
3511 if (iir & I915_BSD_USER_INTERRUPT)
3512 notify_ring(dev, &dev_priv->ring[VCS]);
3513
Chris Wilsona266c7d2012-04-24 22:59:44 +01003514 for_each_pipe(pipe) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01003515 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003516 i915_handle_vblank(dev, pipe, pipe, iir))
3517 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003518
3519 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3520 blc_event = true;
3521 }
3522
3523
3524 if (blc_event || (iir & I915_ASLE_INTERRUPT))
3525 intel_opregion_asle_intr(dev);
3526
Daniel Vetter515ac2b2012-12-01 13:53:44 +01003527 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3528 gmbus_irq_handler(dev);
3529
Chris Wilsona266c7d2012-04-24 22:59:44 +01003530 /* With MSI, interrupts are only generated when iir
3531 * transitions from zero to nonzero. If another bit got
3532 * set while we were handling the existing iir bits, then
3533 * we would never get another interrupt.
3534 *
3535 * This is fine on non-MSI as well, as if we hit this path
3536 * we avoid exiting the interrupt handler only to generate
3537 * another one.
3538 *
3539 * Note that for MSI this could cause a stray interrupt report
3540 * if an interrupt landed in the time between writing IIR and
3541 * the posting read. This should be rare enough to never
3542 * trigger the 99% of 100,000 interrupts test for disabling
3543 * stray interrupts.
3544 */
3545 iir = new_iir;
3546 }
3547
Daniel Vetterd05c6172012-04-26 23:28:09 +02003548 i915_update_dri1_breadcrumb(dev);
Chris Wilson2c8ba292012-04-24 22:59:46 +01003549
Chris Wilsona266c7d2012-04-24 22:59:44 +01003550 return ret;
3551}
3552
3553static void i965_irq_uninstall(struct drm_device * dev)
3554{
3555 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3556 int pipe;
3557
3558 if (!dev_priv)
3559 return;
3560
Egbert Eichac4c16c2013-04-16 13:36:58 +02003561 del_timer_sync(&dev_priv->hotplug_reenable_timer);
3562
Chris Wilsonadca4732012-05-11 18:01:31 +01003563 I915_WRITE(PORT_HOTPLUG_EN, 0);
3564 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01003565
3566 I915_WRITE(HWSTAM, 0xffffffff);
3567 for_each_pipe(pipe)
3568 I915_WRITE(PIPESTAT(pipe), 0);
3569 I915_WRITE(IMR, 0xffffffff);
3570 I915_WRITE(IER, 0x0);
3571
3572 for_each_pipe(pipe)
3573 I915_WRITE(PIPESTAT(pipe),
3574 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3575 I915_WRITE(IIR, I915_READ(IIR));
3576}
3577
Egbert Eichac4c16c2013-04-16 13:36:58 +02003578static void i915_reenable_hotplug_timer_func(unsigned long data)
3579{
3580 drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3581 struct drm_device *dev = dev_priv->dev;
3582 struct drm_mode_config *mode_config = &dev->mode_config;
3583 unsigned long irqflags;
3584 int i;
3585
3586 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3587 for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3588 struct drm_connector *connector;
3589
3590 if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3591 continue;
3592
3593 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3594
3595 list_for_each_entry(connector, &mode_config->connector_list, head) {
3596 struct intel_connector *intel_connector = to_intel_connector(connector);
3597
3598 if (intel_connector->encoder->hpd_pin == i) {
3599 if (connector->polled != intel_connector->polled)
3600 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3601 drm_get_connector_name(connector));
3602 connector->polled = intel_connector->polled;
3603 if (!connector->polled)
3604 connector->polled = DRM_CONNECTOR_POLL_HPD;
3605 }
3606 }
3607 }
3608 if (dev_priv->display.hpd_irq_setup)
3609 dev_priv->display.hpd_irq_setup(dev);
3610 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3611}
3612
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003613void intel_irq_init(struct drm_device *dev)
3614{
Chris Wilson8b2e3262012-04-24 22:59:41 +01003615 struct drm_i915_private *dev_priv = dev->dev_private;
3616
3617 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Daniel Vetter99584db2012-11-14 17:14:04 +01003618 INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003619 INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01003620 INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
Chris Wilson8b2e3262012-04-24 22:59:41 +01003621
Daniel Vetter99584db2012-11-14 17:14:04 +01003622 setup_timer(&dev_priv->gpu_error.hangcheck_timer,
3623 i915_hangcheck_elapsed,
Daniel Vetter61bac782012-12-01 21:03:21 +01003624 (unsigned long) dev);
Egbert Eichac4c16c2013-04-16 13:36:58 +02003625 setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3626 (unsigned long) dev_priv);
Daniel Vetter61bac782012-12-01 21:03:21 +01003627
Tomas Janousek97a19a22012-12-08 13:48:13 +01003628 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01003629
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003630 dev->driver->get_vblank_counter = i915_get_vblank_counter;
3631 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eugeni Dodonov7d4e1462012-05-09 15:37:09 -03003632 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003633 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3634 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3635 }
3636
Keith Packardc3613de2011-08-12 17:05:54 -07003637 if (drm_core_check_feature(dev, DRIVER_MODESET))
3638 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3639 else
3640 dev->driver->get_vblank_timestamp = NULL;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003641 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3642
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003643 if (IS_VALLEYVIEW(dev)) {
3644 dev->driver->irq_handler = valleyview_irq_handler;
3645 dev->driver->irq_preinstall = valleyview_irq_preinstall;
3646 dev->driver->irq_postinstall = valleyview_irq_postinstall;
3647 dev->driver->irq_uninstall = valleyview_irq_uninstall;
3648 dev->driver->enable_vblank = valleyview_enable_vblank;
3649 dev->driver->disable_vblank = valleyview_disable_vblank;
Egbert Eichfa00abe2013-02-25 12:06:48 -05003650 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Daniel Vetter4a06e202012-12-01 13:53:40 +01003651 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Ben Widawsky7d991632013-05-28 19:22:25 -07003652 /* Share uninstall handlers with ILK/SNB */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003653 dev->driver->irq_handler = ivybridge_irq_handler;
Ben Widawsky7d991632013-05-28 19:22:25 -07003654 dev->driver->irq_preinstall = ivybridge_irq_preinstall;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003655 dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3656 dev->driver->irq_uninstall = ironlake_irq_uninstall;
3657 dev->driver->enable_vblank = ivybridge_enable_vblank;
3658 dev->driver->disable_vblank = ivybridge_disable_vblank;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003659 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003660 } else if (HAS_PCH_SPLIT(dev)) {
3661 dev->driver->irq_handler = ironlake_irq_handler;
3662 dev->driver->irq_preinstall = ironlake_irq_preinstall;
3663 dev->driver->irq_postinstall = ironlake_irq_postinstall;
3664 dev->driver->irq_uninstall = ironlake_irq_uninstall;
3665 dev->driver->enable_vblank = ironlake_enable_vblank;
3666 dev->driver->disable_vblank = ironlake_disable_vblank;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003667 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003668 } else {
Chris Wilsonc2798b12012-04-22 21:13:57 +01003669 if (INTEL_INFO(dev)->gen == 2) {
3670 dev->driver->irq_preinstall = i8xx_irq_preinstall;
3671 dev->driver->irq_postinstall = i8xx_irq_postinstall;
3672 dev->driver->irq_handler = i8xx_irq_handler;
3673 dev->driver->irq_uninstall = i8xx_irq_uninstall;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003674 } else if (INTEL_INFO(dev)->gen == 3) {
3675 dev->driver->irq_preinstall = i915_irq_preinstall;
3676 dev->driver->irq_postinstall = i915_irq_postinstall;
3677 dev->driver->irq_uninstall = i915_irq_uninstall;
3678 dev->driver->irq_handler = i915_irq_handler;
Daniel Vetter20afbda2012-12-11 14:05:07 +01003679 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003680 } else {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003681 dev->driver->irq_preinstall = i965_irq_preinstall;
3682 dev->driver->irq_postinstall = i965_irq_postinstall;
3683 dev->driver->irq_uninstall = i965_irq_uninstall;
3684 dev->driver->irq_handler = i965_irq_handler;
Egbert Eichbac56d52013-02-25 12:06:51 -05003685 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003686 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003687 dev->driver->enable_vblank = i915_enable_vblank;
3688 dev->driver->disable_vblank = i915_disable_vblank;
3689 }
3690}
Daniel Vetter20afbda2012-12-11 14:05:07 +01003691
3692void intel_hpd_init(struct drm_device *dev)
3693{
3694 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eich821450c2013-04-16 13:36:55 +02003695 struct drm_mode_config *mode_config = &dev->mode_config;
3696 struct drm_connector *connector;
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02003697 unsigned long irqflags;
Egbert Eich821450c2013-04-16 13:36:55 +02003698 int i;
Daniel Vetter20afbda2012-12-11 14:05:07 +01003699
Egbert Eich821450c2013-04-16 13:36:55 +02003700 for (i = 1; i < HPD_NUM_PINS; i++) {
3701 dev_priv->hpd_stats[i].hpd_cnt = 0;
3702 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3703 }
3704 list_for_each_entry(connector, &mode_config->connector_list, head) {
3705 struct intel_connector *intel_connector = to_intel_connector(connector);
3706 connector->polled = intel_connector->polled;
3707 if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3708 connector->polled = DRM_CONNECTOR_POLL_HPD;
3709 }
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02003710
3711 /* Interrupt setup is already guaranteed to be single-threaded, this is
3712 * just to make the assert_spin_locked checks happy. */
3713 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003714 if (dev_priv->display.hpd_irq_setup)
3715 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02003716 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003717}