blob: 6a757691488e7d711b35f3076fe24a89f48318b5 [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
73static const u32 hpd_status_i965[] = {
74 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I965,
76 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I965,
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
82static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
83 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
84 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
85 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
86 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
87 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
88 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
89};
90
Egbert Eichcd569ae2013-04-16 13:36:57 +020091static void ibx_hpd_irq_setup(struct drm_device *dev);
92static void i915_hpd_irq_setup(struct drm_device *dev);
Egbert Eiche5868a32013-02-28 04:17:12 -050093
Zhenyu Wang036a4a72009-06-08 14:40:19 +080094/* For display hotplug interrupt */
Chris Wilson995b6762010-08-20 13:23:26 +010095static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050096ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080097{
Chris Wilson1ec14ad2010-12-04 11:30:53 +000098 if ((dev_priv->irq_mask & mask) != 0) {
99 dev_priv->irq_mask &= ~mask;
100 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000101 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800102 }
103}
104
Paulo Zanoni0ff98002013-02-22 17:05:31 -0300105static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500106ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800107{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000108 if ((dev_priv->irq_mask & mask) != mask) {
109 dev_priv->irq_mask |= mask;
110 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000111 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800112 }
113}
114
Paulo Zanoni86642812013-04-12 17:57:57 -0300115static bool ivb_can_enable_err_int(struct drm_device *dev)
116{
117 struct drm_i915_private *dev_priv = dev->dev_private;
118 struct intel_crtc *crtc;
119 enum pipe pipe;
120
121 for_each_pipe(pipe) {
122 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
123
124 if (crtc->cpu_fifo_underrun_disabled)
125 return false;
126 }
127
128 return true;
129}
130
131static bool cpt_can_enable_serr_int(struct drm_device *dev)
132{
133 struct drm_i915_private *dev_priv = dev->dev_private;
134 enum pipe pipe;
135 struct intel_crtc *crtc;
136
137 for_each_pipe(pipe) {
138 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
139
140 if (crtc->pch_fifo_underrun_disabled)
141 return false;
142 }
143
144 return true;
145}
146
147static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
148 enum pipe pipe, bool enable)
149{
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
152 DE_PIPEB_FIFO_UNDERRUN;
153
154 if (enable)
155 ironlake_enable_display_irq(dev_priv, bit);
156 else
157 ironlake_disable_display_irq(dev_priv, bit);
158}
159
160static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
161 bool enable)
162{
163 struct drm_i915_private *dev_priv = dev->dev_private;
164
165 if (enable) {
166 if (!ivb_can_enable_err_int(dev))
167 return;
168
169 I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN_A |
170 ERR_INT_FIFO_UNDERRUN_B |
171 ERR_INT_FIFO_UNDERRUN_C);
172
173 ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
174 } else {
175 ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
176 }
177}
178
179static void ibx_set_fifo_underrun_reporting(struct intel_crtc *crtc,
180 bool enable)
181{
182 struct drm_device *dev = crtc->base.dev;
183 struct drm_i915_private *dev_priv = dev->dev_private;
184 uint32_t bit = (crtc->pipe == PIPE_A) ? SDE_TRANSA_FIFO_UNDER :
185 SDE_TRANSB_FIFO_UNDER;
186
187 if (enable)
188 I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~bit);
189 else
190 I915_WRITE(SDEIMR, I915_READ(SDEIMR) | bit);
191
192 POSTING_READ(SDEIMR);
193}
194
195static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
196 enum transcoder pch_transcoder,
197 bool enable)
198{
199 struct drm_i915_private *dev_priv = dev->dev_private;
200
201 if (enable) {
202 if (!cpt_can_enable_serr_int(dev))
203 return;
204
205 I915_WRITE(SERR_INT, SERR_INT_TRANS_A_FIFO_UNDERRUN |
206 SERR_INT_TRANS_B_FIFO_UNDERRUN |
207 SERR_INT_TRANS_C_FIFO_UNDERRUN);
208
209 I915_WRITE(SDEIMR, I915_READ(SDEIMR) & ~SDE_ERROR_CPT);
210 } else {
211 I915_WRITE(SDEIMR, I915_READ(SDEIMR) | SDE_ERROR_CPT);
212 }
213
214 POSTING_READ(SDEIMR);
215}
216
217/**
218 * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
219 * @dev: drm device
220 * @pipe: pipe
221 * @enable: true if we want to report FIFO underrun errors, false otherwise
222 *
223 * This function makes us disable or enable CPU fifo underruns for a specific
224 * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
225 * reporting for one pipe may also disable all the other CPU error interruts for
226 * the other pipes, due to the fact that there's just one interrupt mask/enable
227 * bit for all the pipes.
228 *
229 * Returns the previous state of underrun reporting.
230 */
231bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
232 enum pipe pipe, bool enable)
233{
234 struct drm_i915_private *dev_priv = dev->dev_private;
235 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
237 unsigned long flags;
238 bool ret;
239
240 spin_lock_irqsave(&dev_priv->irq_lock, flags);
241
242 ret = !intel_crtc->cpu_fifo_underrun_disabled;
243
244 if (enable == ret)
245 goto done;
246
247 intel_crtc->cpu_fifo_underrun_disabled = !enable;
248
249 if (IS_GEN5(dev) || IS_GEN6(dev))
250 ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
251 else if (IS_GEN7(dev))
252 ivybridge_set_fifo_underrun_reporting(dev, enable);
253
254done:
255 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
256 return ret;
257}
258
259/**
260 * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
261 * @dev: drm device
262 * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
263 * @enable: true if we want to report FIFO underrun errors, false otherwise
264 *
265 * This function makes us disable or enable PCH fifo underruns for a specific
266 * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
267 * underrun reporting for one transcoder may also disable all the other PCH
268 * error interruts for the other transcoders, due to the fact that there's just
269 * one interrupt mask/enable bit for all the transcoders.
270 *
271 * Returns the previous state of underrun reporting.
272 */
273bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
274 enum transcoder pch_transcoder,
275 bool enable)
276{
277 struct drm_i915_private *dev_priv = dev->dev_private;
278 enum pipe p;
279 struct drm_crtc *crtc;
280 struct intel_crtc *intel_crtc;
281 unsigned long flags;
282 bool ret;
283
284 if (HAS_PCH_LPT(dev)) {
285 crtc = NULL;
286 for_each_pipe(p) {
287 struct drm_crtc *c = dev_priv->pipe_to_crtc_mapping[p];
288 if (intel_pipe_has_type(c, INTEL_OUTPUT_ANALOG)) {
289 crtc = c;
290 break;
291 }
292 }
293 if (!crtc) {
294 DRM_ERROR("PCH FIFO underrun, but no CRTC using the PCH found\n");
295 return false;
296 }
297 } else {
298 crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
299 }
300 intel_crtc = to_intel_crtc(crtc);
301
302 spin_lock_irqsave(&dev_priv->irq_lock, flags);
303
304 ret = !intel_crtc->pch_fifo_underrun_disabled;
305
306 if (enable == ret)
307 goto done;
308
309 intel_crtc->pch_fifo_underrun_disabled = !enable;
310
311 if (HAS_PCH_IBX(dev))
312 ibx_set_fifo_underrun_reporting(intel_crtc, enable);
313 else
314 cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
315
316done:
317 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
318 return ret;
319}
320
321
Keith Packard7c463582008-11-04 02:03:27 -0800322void
323i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
324{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200325 u32 reg = PIPESTAT(pipe);
326 u32 pipestat = I915_READ(reg) & 0x7fff0000;
Keith Packard7c463582008-11-04 02:03:27 -0800327
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200328 if ((pipestat & mask) == mask)
329 return;
330
331 /* Enable the interrupt, clear any pending status */
332 pipestat |= mask | (mask >> 16);
333 I915_WRITE(reg, pipestat);
334 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800335}
336
337void
338i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
339{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200340 u32 reg = PIPESTAT(pipe);
341 u32 pipestat = I915_READ(reg) & 0x7fff0000;
Keith Packard7c463582008-11-04 02:03:27 -0800342
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200343 if ((pipestat & mask) == 0)
344 return;
345
346 pipestat &= ~mask;
347 I915_WRITE(reg, pipestat);
348 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800349}
350
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000351/**
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300352 * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
Zhao Yakui01c66882009-10-28 05:10:00 +0000353 */
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300354static void i915_enable_asle_pipestat(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +0000355{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000356 drm_i915_private_t *dev_priv = dev->dev_private;
357 unsigned long irqflags;
358
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300359 if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
360 return;
361
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000362 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000363
Jani Nikulaf8987802013-04-29 13:02:53 +0300364 i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
365 if (INTEL_INFO(dev)->gen >= 4)
366 i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000367
368 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000369}
370
371/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700372 * i915_pipe_enabled - check if a pipe is enabled
373 * @dev: DRM device
374 * @pipe: pipe to check
375 *
376 * Reading certain registers when the pipe is disabled can hang the chip.
377 * Use this routine to make sure the PLL is running and the pipe is active
378 * before reading such registers if unsure.
379 */
380static int
381i915_pipe_enabled(struct drm_device *dev, int pipe)
382{
383 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200384
Daniel Vettera01025a2013-05-22 00:50:23 +0200385 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
386 /* Locking is horribly broken here, but whatever. */
387 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
388 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni71f8ba62013-05-03 12:15:39 -0300389
Daniel Vettera01025a2013-05-22 00:50:23 +0200390 return intel_crtc->active;
391 } else {
392 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
393 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700394}
395
Keith Packard42f52ef2008-10-18 19:39:29 -0700396/* Called from drm generic code, passed a 'crtc', which
397 * we use as a pipe index
398 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700399static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700400{
401 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
402 unsigned long high_frame;
403 unsigned long low_frame;
Chris Wilson5eddb702010-09-11 13:48:45 +0100404 u32 high1, high2, low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700405
406 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800407 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800408 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700409 return 0;
410 }
411
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800412 high_frame = PIPEFRAME(pipe);
413 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100414
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700415 /*
416 * High & low register fields aren't synchronized, so make sure
417 * we get a low value that's stable across two reads of the high
418 * register.
419 */
420 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100421 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
422 low = I915_READ(low_frame) & PIPE_FRAME_LOW_MASK;
423 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700424 } while (high1 != high2);
425
Chris Wilson5eddb702010-09-11 13:48:45 +0100426 high1 >>= PIPE_FRAME_HIGH_SHIFT;
427 low >>= PIPE_FRAME_LOW_SHIFT;
428 return (high1 << 8) | low;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700429}
430
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700431static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800432{
433 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800434 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800435
436 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800437 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800438 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800439 return 0;
440 }
441
442 return I915_READ(reg);
443}
444
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700445static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100446 int *vpos, int *hpos)
447{
448 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
449 u32 vbl = 0, position = 0;
450 int vbl_start, vbl_end, htotal, vtotal;
451 bool in_vbl = true;
452 int ret = 0;
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200453 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
454 pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100455
456 if (!i915_pipe_enabled(dev, pipe)) {
457 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800458 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100459 return 0;
460 }
461
462 /* Get vtotal. */
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200463 vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100464
465 if (INTEL_INFO(dev)->gen >= 4) {
466 /* No obvious pixelcount register. Only query vertical
467 * scanout position from Display scan line register.
468 */
469 position = I915_READ(PIPEDSL(pipe));
470
471 /* Decode into vertical scanout position. Don't have
472 * horizontal scanout position.
473 */
474 *vpos = position & 0x1fff;
475 *hpos = 0;
476 } else {
477 /* Have access to pixelcount since start of frame.
478 * We can split this into vertical and horizontal
479 * scanout position.
480 */
481 position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
482
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200483 htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100484 *vpos = position / htotal;
485 *hpos = position - (*vpos * htotal);
486 }
487
488 /* Query vblank area. */
Paulo Zanonife2b8f92012-10-23 18:30:02 -0200489 vbl = I915_READ(VBLANK(cpu_transcoder));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100490
491 /* Test position against vblank region. */
492 vbl_start = vbl & 0x1fff;
493 vbl_end = (vbl >> 16) & 0x1fff;
494
495 if ((*vpos < vbl_start) || (*vpos > vbl_end))
496 in_vbl = false;
497
498 /* Inside "upper part" of vblank area? Apply corrective offset: */
499 if (in_vbl && (*vpos >= vbl_start))
500 *vpos = *vpos - vtotal;
501
502 /* Readouts valid? */
503 if (vbl > 0)
504 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
505
506 /* In vblank? */
507 if (in_vbl)
508 ret |= DRM_SCANOUTPOS_INVBL;
509
510 return ret;
511}
512
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700513static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100514 int *max_error,
515 struct timeval *vblank_time,
516 unsigned flags)
517{
Chris Wilson4041b852011-01-22 10:07:56 +0000518 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100519
Ben Widawsky7eb552a2013-03-13 14:05:41 -0700520 if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
Chris Wilson4041b852011-01-22 10:07:56 +0000521 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100522 return -EINVAL;
523 }
524
525 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000526 crtc = intel_get_crtc_for_pipe(dev, pipe);
527 if (crtc == NULL) {
528 DRM_ERROR("Invalid crtc %d\n", pipe);
529 return -EINVAL;
530 }
531
532 if (!crtc->enabled) {
533 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
534 return -EBUSY;
535 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100536
537 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000538 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
539 vblank_time, flags,
540 crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100541}
542
Egbert Eich321a1b32013-04-11 16:00:26 +0200543static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
544{
545 enum drm_connector_status old_status;
546
547 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
548 old_status = connector->status;
549
550 connector->status = connector->funcs->detect(connector, false);
551 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
552 connector->base.id,
553 drm_get_connector_name(connector),
554 old_status, connector->status);
555 return (old_status != connector->status);
556}
557
Jesse Barnes5ca58282009-03-31 14:11:15 -0700558/*
559 * Handle hotplug events outside the interrupt handler proper.
560 */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200561#define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
562
Jesse Barnes5ca58282009-03-31 14:11:15 -0700563static void i915_hotplug_work_func(struct work_struct *work)
564{
565 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
566 hotplug_work);
567 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700568 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200569 struct intel_connector *intel_connector;
570 struct intel_encoder *intel_encoder;
571 struct drm_connector *connector;
572 unsigned long irqflags;
573 bool hpd_disabled = false;
Egbert Eich321a1b32013-04-11 16:00:26 +0200574 bool changed = false;
Egbert Eich142e2392013-04-11 15:57:57 +0200575 u32 hpd_event_bits;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700576
Daniel Vetter52d7ece2012-12-01 21:03:22 +0100577 /* HPD irq before everything is fully set up. */
578 if (!dev_priv->enable_hotplug_processing)
579 return;
580
Keith Packarda65e34c2011-07-25 10:04:56 -0700581 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -0800582 DRM_DEBUG_KMS("running encoder hotplug functions\n");
583
Egbert Eichcd569ae2013-04-16 13:36:57 +0200584 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Egbert Eich142e2392013-04-11 15:57:57 +0200585
586 hpd_event_bits = dev_priv->hpd_event_bits;
587 dev_priv->hpd_event_bits = 0;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200588 list_for_each_entry(connector, &mode_config->connector_list, head) {
589 intel_connector = to_intel_connector(connector);
590 intel_encoder = intel_connector->encoder;
591 if (intel_encoder->hpd_pin > HPD_NONE &&
592 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
593 connector->polled == DRM_CONNECTOR_POLL_HPD) {
594 DRM_INFO("HPD interrupt storm detected on connector %s: "
595 "switching from hotplug detection to polling\n",
596 drm_get_connector_name(connector));
597 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
598 connector->polled = DRM_CONNECTOR_POLL_CONNECT
599 | DRM_CONNECTOR_POLL_DISCONNECT;
600 hpd_disabled = true;
601 }
Egbert Eich142e2392013-04-11 15:57:57 +0200602 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
603 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
604 drm_get_connector_name(connector), intel_encoder->hpd_pin);
605 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200606 }
607 /* if there were no outputs to poll, poll was disabled,
608 * therefore make sure it's enabled when disabling HPD on
609 * some connectors */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200610 if (hpd_disabled) {
Egbert Eichcd569ae2013-04-16 13:36:57 +0200611 drm_kms_helper_poll_enable(dev);
Egbert Eichac4c16c2013-04-16 13:36:58 +0200612 mod_timer(&dev_priv->hotplug_reenable_timer,
613 jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
614 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200615
616 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
617
Egbert Eich321a1b32013-04-11 16:00:26 +0200618 list_for_each_entry(connector, &mode_config->connector_list, head) {
619 intel_connector = to_intel_connector(connector);
620 intel_encoder = intel_connector->encoder;
621 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
622 if (intel_encoder->hot_plug)
623 intel_encoder->hot_plug(intel_encoder);
624 if (intel_hpd_irq_event(dev, connector))
625 changed = true;
626 }
627 }
Keith Packard40ee3382011-07-28 15:31:19 -0700628 mutex_unlock(&mode_config->mutex);
629
Egbert Eich321a1b32013-04-11 16:00:26 +0200630 if (changed)
631 drm_kms_helper_hotplug_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700632}
633
Daniel Vetter73edd18f2012-08-08 23:35:37 +0200634static void ironlake_handle_rps_change(struct drm_device *dev)
Jesse Barnesf97108d2010-01-29 11:27:07 -0800635{
636 drm_i915_private_t *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000637 u32 busy_up, busy_down, max_avg, min_avg;
Daniel Vetter92703882012-08-09 16:46:01 +0200638 u8 new_delay;
639 unsigned long flags;
640
641 spin_lock_irqsave(&mchdev_lock, flags);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800642
Daniel Vetter73edd18f2012-08-08 23:35:37 +0200643 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
644
Daniel Vetter20e4d402012-08-08 23:35:39 +0200645 new_delay = dev_priv->ips.cur_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200646
Jesse Barnes7648fa92010-05-20 14:28:11 -0700647 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000648 busy_up = I915_READ(RCPREVBSYTUPAVG);
649 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800650 max_avg = I915_READ(RCBMAXAVG);
651 min_avg = I915_READ(RCBMINAVG);
652
653 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000654 if (busy_up > max_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200655 if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
656 new_delay = dev_priv->ips.cur_delay - 1;
657 if (new_delay < dev_priv->ips.max_delay)
658 new_delay = dev_priv->ips.max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000659 } else if (busy_down < min_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200660 if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
661 new_delay = dev_priv->ips.cur_delay + 1;
662 if (new_delay > dev_priv->ips.min_delay)
663 new_delay = dev_priv->ips.min_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800664 }
665
Jesse Barnes7648fa92010-05-20 14:28:11 -0700666 if (ironlake_set_drps(dev, new_delay))
Daniel Vetter20e4d402012-08-08 23:35:39 +0200667 dev_priv->ips.cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800668
Daniel Vetter92703882012-08-09 16:46:01 +0200669 spin_unlock_irqrestore(&mchdev_lock, flags);
670
Jesse Barnesf97108d2010-01-29 11:27:07 -0800671 return;
672}
673
Chris Wilson549f7362010-10-19 11:19:32 +0100674static void notify_ring(struct drm_device *dev,
675 struct intel_ring_buffer *ring)
676{
677 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9862e602011-01-04 22:22:17 +0000678
Chris Wilson475553d2011-01-20 09:52:56 +0000679 if (ring->obj == NULL)
680 return;
681
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100682 trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
Chris Wilson9862e602011-01-04 22:22:17 +0000683
Chris Wilson549f7362010-10-19 11:19:32 +0100684 wake_up_all(&ring->irq_queue);
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -0700685 if (i915_enable_hangcheck) {
Daniel Vetter99584db2012-11-14 17:14:04 +0100686 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
Chris Wilsoncecc21f2012-10-05 17:02:56 +0100687 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -0700688 }
Chris Wilson549f7362010-10-19 11:19:32 +0100689}
690
Ben Widawsky4912d042011-04-25 11:25:20 -0700691static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800692{
Ben Widawsky4912d042011-04-25 11:25:20 -0700693 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200694 rps.work);
Ben Widawsky4912d042011-04-25 11:25:20 -0700695 u32 pm_iir, pm_imr;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100696 u8 new_delay;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800697
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200698 spin_lock_irq(&dev_priv->rps.lock);
699 pm_iir = dev_priv->rps.pm_iir;
700 dev_priv->rps.pm_iir = 0;
Ben Widawsky4912d042011-04-25 11:25:20 -0700701 pm_imr = I915_READ(GEN6_PMIMR);
Ben Widawsky48484052013-05-28 19:22:27 -0700702 /* Make sure not to corrupt PMIMR state used by ringbuffer code */
703 I915_WRITE(GEN6_PMIMR, pm_imr & ~GEN6_PM_RPS_EVENTS);
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200704 spin_unlock_irq(&dev_priv->rps.lock);
Ben Widawsky4912d042011-04-25 11:25:20 -0700705
Ben Widawsky48484052013-05-28 19:22:27 -0700706 if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800707 return;
708
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700709 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100710
711 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD)
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200712 new_delay = dev_priv->rps.cur_delay + 1;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100713 else
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200714 new_delay = dev_priv->rps.cur_delay - 1;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800715
Ben Widawsky79249632012-09-07 19:43:42 -0700716 /* sysfs frequency interfaces may have snuck in while servicing the
717 * interrupt
718 */
719 if (!(new_delay > dev_priv->rps.max_delay ||
720 new_delay < dev_priv->rps.min_delay)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -0700721 if (IS_VALLEYVIEW(dev_priv->dev))
722 valleyview_set_rps(dev_priv->dev, new_delay);
723 else
724 gen6_set_rps(dev_priv->dev, new_delay);
Ben Widawsky79249632012-09-07 19:43:42 -0700725 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800726
Jesse Barnes52ceb902013-04-23 10:09:26 -0700727 if (IS_VALLEYVIEW(dev_priv->dev)) {
728 /*
729 * On VLV, when we enter RC6 we may not be at the minimum
730 * voltage level, so arm a timer to check. It should only
731 * fire when there's activity or once after we've entered
732 * RC6, and then won't be re-armed until the next RPS interrupt.
733 */
734 mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
735 msecs_to_jiffies(100));
736 }
737
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700738 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800739}
740
Ben Widawskye3689192012-05-25 16:56:22 -0700741
742/**
743 * ivybridge_parity_work - Workqueue called when a parity error interrupt
744 * occurred.
745 * @work: workqueue struct
746 *
747 * Doesn't actually do anything except notify userspace. As a consequence of
748 * this event, userspace should try to remap the bad rows since statistically
749 * it is likely the same row is more likely to go bad again.
750 */
751static void ivybridge_parity_work(struct work_struct *work)
752{
753 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
Daniel Vettera4da4fa2012-11-02 19:55:07 +0100754 l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -0700755 u32 error_status, row, bank, subbank;
756 char *parity_event[5];
757 uint32_t misccpctl;
758 unsigned long flags;
759
760 /* We must turn off DOP level clock gating to access the L3 registers.
761 * In order to prevent a get/put style interface, acquire struct mutex
762 * any time we access those registers.
763 */
764 mutex_lock(&dev_priv->dev->struct_mutex);
765
766 misccpctl = I915_READ(GEN7_MISCCPCTL);
767 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
768 POSTING_READ(GEN7_MISCCPCTL);
769
770 error_status = I915_READ(GEN7_L3CDERRST1);
771 row = GEN7_PARITY_ERROR_ROW(error_status);
772 bank = GEN7_PARITY_ERROR_BANK(error_status);
773 subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
774
775 I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
776 GEN7_L3CDERRST1_ENABLE);
777 POSTING_READ(GEN7_L3CDERRST1);
778
779 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
780
781 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Ben Widawskycc609d52013-05-28 19:22:29 -0700782 dev_priv->gt_irq_mask &= ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Ben Widawskye3689192012-05-25 16:56:22 -0700783 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
784 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
785
786 mutex_unlock(&dev_priv->dev->struct_mutex);
787
788 parity_event[0] = "L3_PARITY_ERROR=1";
789 parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
790 parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
791 parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
792 parity_event[4] = NULL;
793
794 kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
795 KOBJ_CHANGE, parity_event);
796
797 DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
798 row, bank, subbank);
799
800 kfree(parity_event[3]);
801 kfree(parity_event[2]);
802 kfree(parity_event[1]);
803}
804
Daniel Vetterd2ba8472012-05-31 14:57:41 +0200805static void ivybridge_handle_parity_error(struct drm_device *dev)
Ben Widawskye3689192012-05-25 16:56:22 -0700806{
807 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
808 unsigned long flags;
809
Ben Widawskye1ef7cc2012-07-24 20:47:31 -0700810 if (!HAS_L3_GPU_CACHE(dev))
Ben Widawskye3689192012-05-25 16:56:22 -0700811 return;
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
Daniel Vettera4da4fa2012-11-02 19:55:07 +0100818 queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -0700819}
820
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200821static void snb_gt_irq_handler(struct drm_device *dev,
822 struct drm_i915_private *dev_priv,
823 u32 gt_iir)
824{
825
Ben Widawskycc609d52013-05-28 19:22:29 -0700826 if (gt_iir &
827 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200828 notify_ring(dev, &dev_priv->ring[RCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -0700829 if (gt_iir & GT_BSD_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200830 notify_ring(dev, &dev_priv->ring[VCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -0700831 if (gt_iir & GT_BLT_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200832 notify_ring(dev, &dev_priv->ring[BCS]);
833
Ben Widawskycc609d52013-05-28 19:22:29 -0700834 if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
835 GT_BSD_CS_ERROR_INTERRUPT |
836 GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200837 DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
838 i915_handle_error(dev, false);
839 }
Ben Widawskye3689192012-05-25 16:56:22 -0700840
Ben Widawskycc609d52013-05-28 19:22:29 -0700841 if (gt_iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
Ben Widawskye3689192012-05-25 16:56:22 -0700842 ivybridge_handle_parity_error(dev);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200843}
844
Ben Widawskybaf02a12013-05-28 19:22:24 -0700845/* Legacy way of handling PM interrupts */
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100846static void gen6_queue_rps_work(struct drm_i915_private *dev_priv,
847 u32 pm_iir)
848{
849 unsigned long flags;
850
851 /*
852 * IIR bits should never already be set because IMR should
853 * prevent an interrupt from being shown in IIR. The warning
854 * displays a case where we've unsafely cleared
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200855 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100856 * type is not a problem, it displays a problem in the logic.
857 *
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200858 * The mask bit in IMR is cleared by dev_priv->rps.work.
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100859 */
860
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200861 spin_lock_irqsave(&dev_priv->rps.lock, flags);
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200862 dev_priv->rps.pm_iir |= pm_iir;
863 I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100864 POSTING_READ(GEN6_PMIMR);
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200865 spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100866
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200867 queue_work(dev_priv->wq, &dev_priv->rps.work);
Chris Wilsonfc6826d2012-04-15 11:56:03 +0100868}
869
Egbert Eichb543fb02013-04-16 13:36:54 +0200870#define HPD_STORM_DETECT_PERIOD 1000
871#define HPD_STORM_THRESHOLD 5
872
Egbert Eichcd569ae2013-04-16 13:36:57 +0200873static inline bool hotplug_irq_storm_detect(struct drm_device *dev,
Egbert Eichb543fb02013-04-16 13:36:54 +0200874 u32 hotplug_trigger,
875 const u32 *hpd)
876{
877 drm_i915_private_t *dev_priv = dev->dev_private;
878 unsigned long irqflags;
879 int i;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200880 bool ret = false;
Egbert Eichb543fb02013-04-16 13:36:54 +0200881
882 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
883
884 for (i = 1; i < HPD_NUM_PINS; i++) {
Egbert Eich821450c2013-04-16 13:36:55 +0200885
Egbert Eichb543fb02013-04-16 13:36:54 +0200886 if (!(hpd[i] & hotplug_trigger) ||
887 dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
888 continue;
889
Jani Nikulabc5ead8c2013-05-07 15:10:29 +0300890 dev_priv->hpd_event_bits |= (1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +0200891 if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
892 dev_priv->hpd_stats[i].hpd_last_jiffies
893 + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
894 dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
895 dev_priv->hpd_stats[i].hpd_cnt = 0;
896 } else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
897 dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
Egbert Eich142e2392013-04-11 15:57:57 +0200898 dev_priv->hpd_event_bits &= ~(1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +0200899 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
Egbert Eichcd569ae2013-04-16 13:36:57 +0200900 ret = true;
Egbert Eichb543fb02013-04-16 13:36:54 +0200901 } else {
902 dev_priv->hpd_stats[i].hpd_cnt++;
903 }
904 }
905
906 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Egbert Eichcd569ae2013-04-16 13:36:57 +0200907
908 return ret;
Egbert Eichb543fb02013-04-16 13:36:54 +0200909}
910
Daniel Vetter515ac2b2012-12-01 13:53:44 +0100911static void gmbus_irq_handler(struct drm_device *dev)
912{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100913 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
914
Daniel Vetter28c70f12012-12-01 13:53:45 +0100915 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetter515ac2b2012-12-01 13:53:44 +0100916}
917
Daniel Vetterce99c252012-12-01 13:53:47 +0100918static void dp_aux_irq_handler(struct drm_device *dev)
919{
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100920 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
921
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100922 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetterce99c252012-12-01 13:53:47 +0100923}
924
Ben Widawskybaf02a12013-05-28 19:22:24 -0700925/* Unlike gen6_queue_rps_work() from which this function is originally derived,
926 * we must be able to deal with other PM interrupts. This is complicated because
927 * of the way in which we use the masks to defer the RPS work (which for
928 * posterity is necessary because of forcewake).
929 */
930static void hsw_pm_irq_handler(struct drm_i915_private *dev_priv,
931 u32 pm_iir)
932{
933 unsigned long flags;
934
935 spin_lock_irqsave(&dev_priv->rps.lock, flags);
Ben Widawsky48484052013-05-28 19:22:27 -0700936 dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
Ben Widawskybaf02a12013-05-28 19:22:24 -0700937 if (dev_priv->rps.pm_iir) {
938 I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
939 /* never want to mask useful interrupts. (also posting read) */
Ben Widawsky48484052013-05-28 19:22:27 -0700940 WARN_ON(I915_READ_NOTRACE(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
Ben Widawskybaf02a12013-05-28 19:22:24 -0700941 /* TODO: if queue_work is slow, move it out of the spinlock */
942 queue_work(dev_priv->wq, &dev_priv->rps.work);
943 }
944 spin_unlock_irqrestore(&dev_priv->rps.lock, flags);
945
Ben Widawsky12638c52013-05-28 19:22:31 -0700946 if (pm_iir & ~GEN6_PM_RPS_EVENTS) {
947 if (pm_iir & PM_VEBOX_USER_INTERRUPT)
948 notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
949
950 if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
951 DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
952 i915_handle_error(dev_priv->dev, false);
953 }
954 }
Ben Widawskybaf02a12013-05-28 19:22:24 -0700955}
956
Daniel Vetterff1f5252012-10-02 15:10:55 +0200957static irqreturn_t valleyview_irq_handler(int irq, void *arg)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700958{
959 struct drm_device *dev = (struct drm_device *) arg;
960 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
961 u32 iir, gt_iir, pm_iir;
962 irqreturn_t ret = IRQ_NONE;
963 unsigned long irqflags;
964 int pipe;
965 u32 pipe_stats[I915_MAX_PIPES];
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700966
967 atomic_inc(&dev_priv->irq_received);
968
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700969 while (true) {
970 iir = I915_READ(VLV_IIR);
971 gt_iir = I915_READ(GTIIR);
972 pm_iir = I915_READ(GEN6_PMIIR);
973
974 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
975 goto out;
976
977 ret = IRQ_HANDLED;
978
Daniel Vettere7b4c6b2012-03-30 20:24:35 +0200979 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -0700980
981 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
982 for_each_pipe(pipe) {
983 int reg = PIPESTAT(pipe);
984 pipe_stats[pipe] = I915_READ(reg);
985
986 /*
987 * Clear the PIPE*STAT regs before the IIR
988 */
989 if (pipe_stats[pipe] & 0x8000ffff) {
990 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
991 DRM_DEBUG_DRIVER("pipe %c underrun\n",
992 pipe_name(pipe));
993 I915_WRITE(reg, pipe_stats[pipe]);
994 }
995 }
996 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
997
Jesse Barnes31acc7f2012-06-20 10:53:11 -0700998 for_each_pipe(pipe) {
999 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1000 drm_handle_vblank(dev, pipe);
1001
1002 if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
1003 intel_prepare_page_flip(dev, pipe);
1004 intel_finish_page_flip(dev, pipe);
1005 }
1006 }
1007
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001008 /* Consume port. Then clear IIR or we'll miss events */
1009 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
1010 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
Egbert Eichb543fb02013-04-16 13:36:54 +02001011 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001012
1013 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1014 hotplug_status);
Egbert Eichb543fb02013-04-16 13:36:54 +02001015 if (hotplug_trigger) {
Egbert Eichcd569ae2013-04-16 13:36:57 +02001016 if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
1017 i915_hpd_irq_setup(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001018 queue_work(dev_priv->wq,
1019 &dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +02001020 }
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001021 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1022 I915_READ(PORT_HOTPLUG_STAT);
1023 }
1024
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001025 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1026 gmbus_irq_handler(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001027
Ben Widawsky48484052013-05-28 19:22:27 -07001028 if (pm_iir & GEN6_PM_RPS_EVENTS)
Chris Wilsonfc6826d2012-04-15 11:56:03 +01001029 gen6_queue_rps_work(dev_priv, pm_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001030
1031 I915_WRITE(GTIIR, gt_iir);
1032 I915_WRITE(GEN6_PMIIR, pm_iir);
1033 I915_WRITE(VLV_IIR, iir);
1034 }
1035
1036out:
1037 return ret;
1038}
1039
Adam Jackson23e81d62012-06-06 15:45:44 -04001040static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
Jesse Barnes776ad802011-01-04 15:09:39 -08001041{
1042 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001043 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001044 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
Jesse Barnes776ad802011-01-04 15:09:39 -08001045
Egbert Eichb543fb02013-04-16 13:36:54 +02001046 if (hotplug_trigger) {
Egbert Eichcd569ae2013-04-16 13:36:57 +02001047 if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_ibx))
1048 ibx_hpd_irq_setup(dev);
Daniel Vetter76e43832012-10-12 20:14:05 +02001049 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +02001050 }
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001051 if (pch_iir & SDE_AUDIO_POWER_MASK) {
1052 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1053 SDE_AUDIO_POWER_SHIFT);
Jesse Barnes776ad802011-01-04 15:09:39 -08001054 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001055 port_name(port));
1056 }
Jesse Barnes776ad802011-01-04 15:09:39 -08001057
Daniel Vetterce99c252012-12-01 13:53:47 +01001058 if (pch_iir & SDE_AUX_MASK)
1059 dp_aux_irq_handler(dev);
1060
Jesse Barnes776ad802011-01-04 15:09:39 -08001061 if (pch_iir & SDE_GMBUS)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001062 gmbus_irq_handler(dev);
Jesse Barnes776ad802011-01-04 15:09:39 -08001063
1064 if (pch_iir & SDE_AUDIO_HDCP_MASK)
1065 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1066
1067 if (pch_iir & SDE_AUDIO_TRANS_MASK)
1068 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1069
1070 if (pch_iir & SDE_POISON)
1071 DRM_ERROR("PCH poison interrupt\n");
1072
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001073 if (pch_iir & SDE_FDI_MASK)
1074 for_each_pipe(pipe)
1075 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1076 pipe_name(pipe),
1077 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -08001078
1079 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1080 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1081
1082 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1083 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1084
Jesse Barnes776ad802011-01-04 15:09:39 -08001085 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
Paulo Zanoni86642812013-04-12 17:57:57 -03001086 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1087 false))
1088 DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
1089
1090 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1091 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1092 false))
1093 DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
1094}
1095
1096static void ivb_err_int_handler(struct drm_device *dev)
1097{
1098 struct drm_i915_private *dev_priv = dev->dev_private;
1099 u32 err_int = I915_READ(GEN7_ERR_INT);
1100
Paulo Zanonide032bf2013-04-12 17:57:58 -03001101 if (err_int & ERR_INT_POISON)
1102 DRM_ERROR("Poison interrupt\n");
1103
Paulo Zanoni86642812013-04-12 17:57:57 -03001104 if (err_int & ERR_INT_FIFO_UNDERRUN_A)
1105 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1106 DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1107
1108 if (err_int & ERR_INT_FIFO_UNDERRUN_B)
1109 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1110 DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1111
1112 if (err_int & ERR_INT_FIFO_UNDERRUN_C)
1113 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
1114 DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
1115
1116 I915_WRITE(GEN7_ERR_INT, err_int);
1117}
1118
1119static void cpt_serr_int_handler(struct drm_device *dev)
1120{
1121 struct drm_i915_private *dev_priv = dev->dev_private;
1122 u32 serr_int = I915_READ(SERR_INT);
1123
Paulo Zanonide032bf2013-04-12 17:57:58 -03001124 if (serr_int & SERR_INT_POISON)
1125 DRM_ERROR("PCH poison interrupt\n");
1126
Paulo Zanoni86642812013-04-12 17:57:57 -03001127 if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
1128 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1129 false))
1130 DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
1131
1132 if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
1133 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1134 false))
1135 DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
1136
1137 if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
1138 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
1139 false))
1140 DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
1141
1142 I915_WRITE(SERR_INT, serr_int);
Jesse Barnes776ad802011-01-04 15:09:39 -08001143}
1144
Adam Jackson23e81d62012-06-06 15:45:44 -04001145static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
1146{
1147 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1148 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001149 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
Adam Jackson23e81d62012-06-06 15:45:44 -04001150
Egbert Eichb543fb02013-04-16 13:36:54 +02001151 if (hotplug_trigger) {
Egbert Eichcd569ae2013-04-16 13:36:57 +02001152 if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_cpt))
1153 ibx_hpd_irq_setup(dev);
Daniel Vetter76e43832012-10-12 20:14:05 +02001154 queue_work(dev_priv->wq, &dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +02001155 }
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001156 if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1157 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
1158 SDE_AUDIO_POWER_SHIFT_CPT);
1159 DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1160 port_name(port));
1161 }
Adam Jackson23e81d62012-06-06 15:45:44 -04001162
1163 if (pch_iir & SDE_AUX_MASK_CPT)
Daniel Vetterce99c252012-12-01 13:53:47 +01001164 dp_aux_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001165
1166 if (pch_iir & SDE_GMBUS_CPT)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001167 gmbus_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001168
1169 if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
1170 DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
1171
1172 if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
1173 DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
1174
1175 if (pch_iir & SDE_FDI_MASK_CPT)
1176 for_each_pipe(pipe)
1177 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1178 pipe_name(pipe),
1179 I915_READ(FDI_RX_IIR(pipe)));
Paulo Zanoni86642812013-04-12 17:57:57 -03001180
1181 if (pch_iir & SDE_ERROR_CPT)
1182 cpt_serr_int_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001183}
1184
Daniel Vetterff1f5252012-10-02 15:10:55 +02001185static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001186{
1187 struct drm_device *dev = (struct drm_device *) arg;
1188 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Ben Widawskyab5c6082013-04-05 13:12:41 -07001189 u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
Chris Wilson0e434062012-05-09 21:45:44 +01001190 irqreturn_t ret = IRQ_NONE;
1191 int i;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001192
1193 atomic_inc(&dev_priv->irq_received);
1194
Paulo Zanoni86642812013-04-12 17:57:57 -03001195 /* We get interrupts on unclaimed registers, so check for this before we
1196 * do any I915_{READ,WRITE}. */
1197 if (IS_HASWELL(dev) &&
1198 (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
1199 DRM_ERROR("Unclaimed register before interrupt\n");
1200 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
1201 }
1202
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001203 /* disable master interrupt before clearing iir */
1204 de_ier = I915_READ(DEIER);
1205 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson0e434062012-05-09 21:45:44 +01001206
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001207 /* Disable south interrupts. We'll only write to SDEIIR once, so further
1208 * interrupts will will be stored on its back queue, and then we'll be
1209 * able to process them after we restore SDEIER (as soon as we restore
1210 * it, we'll get an interrupt if SDEIIR still has something to process
1211 * due to its back queue). */
Ben Widawskyab5c6082013-04-05 13:12:41 -07001212 if (!HAS_PCH_NOP(dev)) {
1213 sde_ier = I915_READ(SDEIER);
1214 I915_WRITE(SDEIER, 0);
1215 POSTING_READ(SDEIER);
1216 }
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001217
Paulo Zanoni86642812013-04-12 17:57:57 -03001218 /* On Haswell, also mask ERR_INT because we don't want to risk
1219 * generating "unclaimed register" interrupts from inside the interrupt
1220 * handler. */
1221 if (IS_HASWELL(dev))
1222 ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
1223
Chris Wilson0e434062012-05-09 21:45:44 +01001224 gt_iir = I915_READ(GTIIR);
1225 if (gt_iir) {
1226 snb_gt_irq_handler(dev, dev_priv, gt_iir);
1227 I915_WRITE(GTIIR, gt_iir);
1228 ret = IRQ_HANDLED;
1229 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001230
1231 de_iir = I915_READ(DEIIR);
Chris Wilson0e434062012-05-09 21:45:44 +01001232 if (de_iir) {
Paulo Zanoni86642812013-04-12 17:57:57 -03001233 if (de_iir & DE_ERR_INT_IVB)
1234 ivb_err_int_handler(dev);
1235
Daniel Vetterce99c252012-12-01 13:53:47 +01001236 if (de_iir & DE_AUX_CHANNEL_A_IVB)
1237 dp_aux_irq_handler(dev);
1238
Chris Wilson0e434062012-05-09 21:45:44 +01001239 if (de_iir & DE_GSE_IVB)
Jani Nikula81a07802013-04-24 22:18:44 +03001240 intel_opregion_asle_intr(dev);
Chris Wilson0e434062012-05-09 21:45:44 +01001241
1242 for (i = 0; i < 3; i++) {
Daniel Vetter74d44442012-10-02 17:54:35 +02001243 if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
1244 drm_handle_vblank(dev, i);
Chris Wilson0e434062012-05-09 21:45:44 +01001245 if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
1246 intel_prepare_page_flip(dev, i);
1247 intel_finish_page_flip_plane(dev, i);
1248 }
Chris Wilson0e434062012-05-09 21:45:44 +01001249 }
1250
1251 /* check event from PCH */
Ben Widawskyab5c6082013-04-05 13:12:41 -07001252 if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
Chris Wilson0e434062012-05-09 21:45:44 +01001253 u32 pch_iir = I915_READ(SDEIIR);
1254
Adam Jackson23e81d62012-06-06 15:45:44 -04001255 cpt_irq_handler(dev, pch_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01001256
1257 /* clear PCH hotplug event before clear CPU irq */
1258 I915_WRITE(SDEIIR, pch_iir);
1259 }
1260
1261 I915_WRITE(DEIIR, de_iir);
1262 ret = IRQ_HANDLED;
1263 }
1264
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001265 pm_iir = I915_READ(GEN6_PMIIR);
Chris Wilson0e434062012-05-09 21:45:44 +01001266 if (pm_iir) {
Ben Widawskybaf02a12013-05-28 19:22:24 -07001267 if (IS_HASWELL(dev))
1268 hsw_pm_irq_handler(dev_priv, pm_iir);
Ben Widawsky48484052013-05-28 19:22:27 -07001269 else if (pm_iir & GEN6_PM_RPS_EVENTS)
Chris Wilson0e434062012-05-09 21:45:44 +01001270 gen6_queue_rps_work(dev_priv, pm_iir);
1271 I915_WRITE(GEN6_PMIIR, pm_iir);
1272 ret = IRQ_HANDLED;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001273 }
1274
Paulo Zanoni86642812013-04-12 17:57:57 -03001275 if (IS_HASWELL(dev) && ivb_can_enable_err_int(dev))
1276 ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
1277
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001278 I915_WRITE(DEIER, de_ier);
1279 POSTING_READ(DEIER);
Ben Widawskyab5c6082013-04-05 13:12:41 -07001280 if (!HAS_PCH_NOP(dev)) {
1281 I915_WRITE(SDEIER, sde_ier);
1282 POSTING_READ(SDEIER);
1283 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001284
1285 return ret;
1286}
1287
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001288static void ilk_gt_irq_handler(struct drm_device *dev,
1289 struct drm_i915_private *dev_priv,
1290 u32 gt_iir)
1291{
Ben Widawskycc609d52013-05-28 19:22:29 -07001292 if (gt_iir &
1293 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001294 notify_ring(dev, &dev_priv->ring[RCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001295 if (gt_iir & ILK_BSD_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001296 notify_ring(dev, &dev_priv->ring[VCS]);
1297}
1298
Daniel Vetterff1f5252012-10-02 15:10:55 +02001299static irqreturn_t ironlake_irq_handler(int irq, void *arg)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001300{
Jesse Barnes46979952011-04-07 13:53:55 -07001301 struct drm_device *dev = (struct drm_device *) arg;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001302 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1303 int ret = IRQ_NONE;
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001304 u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001305
Jesse Barnes46979952011-04-07 13:53:55 -07001306 atomic_inc(&dev_priv->irq_received);
1307
Zou, Nanhai2d109a82009-11-06 02:13:01 +00001308 /* disable master interrupt before clearing iir */
1309 de_ier = I915_READ(DEIER);
1310 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001311 POSTING_READ(DEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +00001312
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001313 /* Disable south interrupts. We'll only write to SDEIIR once, so further
1314 * interrupts will will be stored on its back queue, and then we'll be
1315 * able to process them after we restore SDEIER (as soon as we restore
1316 * it, we'll get an interrupt if SDEIIR still has something to process
1317 * due to its back queue). */
1318 sde_ier = I915_READ(SDEIER);
1319 I915_WRITE(SDEIER, 0);
1320 POSTING_READ(SDEIER);
1321
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001322 de_iir = I915_READ(DEIIR);
1323 gt_iir = I915_READ(GTIIR);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001324 pm_iir = I915_READ(GEN6_PMIIR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001325
Daniel Vetteracd15b62012-11-30 11:24:50 +01001326 if (de_iir == 0 && gt_iir == 0 && (!IS_GEN6(dev) || pm_iir == 0))
Zou Nan haic7c85102010-01-15 10:29:06 +08001327 goto done;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001328
Zou Nan haic7c85102010-01-15 10:29:06 +08001329 ret = IRQ_HANDLED;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001330
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001331 if (IS_GEN5(dev))
1332 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1333 else
1334 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +08001335
Daniel Vetterce99c252012-12-01 13:53:47 +01001336 if (de_iir & DE_AUX_CHANNEL_A)
1337 dp_aux_irq_handler(dev);
1338
Zou Nan haic7c85102010-01-15 10:29:06 +08001339 if (de_iir & DE_GSE)
Jani Nikula81a07802013-04-24 22:18:44 +03001340 intel_opregion_asle_intr(dev);
Zou Nan haic7c85102010-01-15 10:29:06 +08001341
Daniel Vetter74d44442012-10-02 17:54:35 +02001342 if (de_iir & DE_PIPEA_VBLANK)
1343 drm_handle_vblank(dev, 0);
1344
1345 if (de_iir & DE_PIPEB_VBLANK)
1346 drm_handle_vblank(dev, 1);
1347
Paulo Zanonide032bf2013-04-12 17:57:58 -03001348 if (de_iir & DE_POISON)
1349 DRM_ERROR("Poison interrupt\n");
1350
Paulo Zanoni86642812013-04-12 17:57:57 -03001351 if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1352 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1353 DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1354
1355 if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1356 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1357 DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1358
Zhenyu Wangf072d2e2010-02-09 09:46:19 +08001359 if (de_iir & DE_PLANEA_FLIP_DONE) {
Jesse Barnes013d5aa2010-01-29 11:18:31 -08001360 intel_prepare_page_flip(dev, 0);
Chris Wilson2bbda382010-09-02 17:59:39 +01001361 intel_finish_page_flip_plane(dev, 0);
Jesse Barnes013d5aa2010-01-29 11:18:31 -08001362 }
1363
Zhenyu Wangf072d2e2010-02-09 09:46:19 +08001364 if (de_iir & DE_PLANEB_FLIP_DONE) {
1365 intel_prepare_page_flip(dev, 1);
Chris Wilson2bbda382010-09-02 17:59:39 +01001366 intel_finish_page_flip_plane(dev, 1);
Jesse Barnes013d5aa2010-01-29 11:18:31 -08001367 }
Li Pengc062df62010-01-23 00:12:58 +08001368
Zou Nan haic7c85102010-01-15 10:29:06 +08001369 /* check event from PCH */
Jesse Barnes776ad802011-01-04 15:09:39 -08001370 if (de_iir & DE_PCH_EVENT) {
Daniel Vetteracd15b62012-11-30 11:24:50 +01001371 u32 pch_iir = I915_READ(SDEIIR);
1372
Adam Jackson23e81d62012-06-06 15:45:44 -04001373 if (HAS_PCH_CPT(dev))
1374 cpt_irq_handler(dev, pch_iir);
1375 else
1376 ibx_irq_handler(dev, pch_iir);
Daniel Vetteracd15b62012-11-30 11:24:50 +01001377
1378 /* should clear PCH hotplug event before clear CPU irq */
1379 I915_WRITE(SDEIIR, pch_iir);
Jesse Barnes776ad802011-01-04 15:09:39 -08001380 }
Zou Nan haic7c85102010-01-15 10:29:06 +08001381
Daniel Vetter73edd18f2012-08-08 23:35:37 +02001382 if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1383 ironlake_handle_rps_change(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08001384
Ben Widawsky48484052013-05-28 19:22:27 -07001385 if (IS_GEN6(dev) && pm_iir & GEN6_PM_RPS_EVENTS)
Chris Wilsonfc6826d2012-04-15 11:56:03 +01001386 gen6_queue_rps_work(dev_priv, pm_iir);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08001387
Zou Nan haic7c85102010-01-15 10:29:06 +08001388 I915_WRITE(GTIIR, gt_iir);
1389 I915_WRITE(DEIIR, de_iir);
Ben Widawsky4912d042011-04-25 11:25:20 -07001390 I915_WRITE(GEN6_PMIIR, pm_iir);
Zou Nan haic7c85102010-01-15 10:29:06 +08001391
1392done:
Zou, Nanhai2d109a82009-11-06 02:13:01 +00001393 I915_WRITE(DEIER, de_ier);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001394 POSTING_READ(DEIER);
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001395 I915_WRITE(SDEIER, sde_ier);
1396 POSTING_READ(SDEIER);
Zou, Nanhai2d109a82009-11-06 02:13:01 +00001397
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001398 return ret;
1399}
1400
Jesse Barnes8a905232009-07-11 16:48:03 -04001401/**
1402 * i915_error_work_func - do process context error handling work
1403 * @work: work struct
1404 *
1405 * Fire an error uevent so userspace can see that a hang or error
1406 * was detected.
1407 */
1408static void i915_error_work_func(struct work_struct *work)
1409{
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001410 struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
1411 work);
1412 drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
1413 gpu_error);
Jesse Barnes8a905232009-07-11 16:48:03 -04001414 struct drm_device *dev = dev_priv->dev;
Daniel Vetterf69061b2012-12-06 09:01:42 +01001415 struct intel_ring_buffer *ring;
Ben Gamarif316a422009-09-14 17:48:46 -04001416 char *error_event[] = { "ERROR=1", NULL };
1417 char *reset_event[] = { "RESET=1", NULL };
1418 char *reset_done_event[] = { "ERROR=0", NULL };
Daniel Vetterf69061b2012-12-06 09:01:42 +01001419 int i, ret;
Jesse Barnes8a905232009-07-11 16:48:03 -04001420
Ben Gamarif316a422009-09-14 17:48:46 -04001421 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -04001422
Daniel Vetter7db0ba22012-12-06 16:23:37 +01001423 /*
1424 * Note that there's only one work item which does gpu resets, so we
1425 * need not worry about concurrent gpu resets potentially incrementing
1426 * error->reset_counter twice. We only need to take care of another
1427 * racing irq/hangcheck declaring the gpu dead for a second time. A
1428 * quick check for that is good enough: schedule_work ensures the
1429 * correct ordering between hang detection and this work item, and since
1430 * the reset in-progress bit is only ever set by code outside of this
1431 * work we don't need to worry about any other races.
1432 */
1433 if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +01001434 DRM_DEBUG_DRIVER("resetting chip\n");
Daniel Vetter7db0ba22012-12-06 16:23:37 +01001435 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
1436 reset_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001437
Daniel Vetterf69061b2012-12-06 09:01:42 +01001438 ret = i915_reset(dev);
1439
1440 if (ret == 0) {
1441 /*
1442 * After all the gem state is reset, increment the reset
1443 * counter and wake up everyone waiting for the reset to
1444 * complete.
1445 *
1446 * Since unlock operations are a one-sided barrier only,
1447 * we need to insert a barrier here to order any seqno
1448 * updates before
1449 * the counter increment.
1450 */
1451 smp_mb__before_atomic_inc();
1452 atomic_inc(&dev_priv->gpu_error.reset_counter);
1453
1454 kobject_uevent_env(&dev->primary->kdev.kobj,
1455 KOBJ_CHANGE, reset_done_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001456 } else {
1457 atomic_set(&error->reset_counter, I915_WEDGED);
Ben Gamarif316a422009-09-14 17:48:46 -04001458 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001459
Daniel Vetterf69061b2012-12-06 09:01:42 +01001460 for_each_ring(ring, dev_priv, i)
1461 wake_up_all(&ring->irq_queue);
1462
Ville Syrjälä96a02912013-02-18 19:08:49 +02001463 intel_display_handle_reset(dev);
1464
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001465 wake_up_all(&dev_priv->gpu_error.reset_queue);
Ben Gamarif316a422009-09-14 17:48:46 -04001466 }
Jesse Barnes8a905232009-07-11 16:48:03 -04001467}
1468
Daniel Vetter85f9e502012-08-31 21:42:26 +02001469/* NB: please notice the memset */
1470static void i915_get_extra_instdone(struct drm_device *dev,
1471 uint32_t *instdone)
1472{
1473 struct drm_i915_private *dev_priv = dev->dev_private;
1474 memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
1475
1476 switch(INTEL_INFO(dev)->gen) {
1477 case 2:
1478 case 3:
1479 instdone[0] = I915_READ(INSTDONE);
1480 break;
1481 case 4:
1482 case 5:
1483 case 6:
1484 instdone[0] = I915_READ(INSTDONE_I965);
1485 instdone[1] = I915_READ(INSTDONE1);
1486 break;
1487 default:
1488 WARN_ONCE(1, "Unsupported platform\n");
1489 case 7:
1490 instdone[0] = I915_READ(GEN7_INSTDONE_1);
1491 instdone[1] = I915_READ(GEN7_SC_INSTDONE);
1492 instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
1493 instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
1494 break;
1495 }
1496}
1497
Chris Wilson3bd3c932010-08-19 08:19:30 +01001498#ifdef CONFIG_DEBUG_FS
Chris Wilson9df30792010-02-18 10:24:56 +00001499static struct drm_i915_error_object *
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001500i915_error_object_create_sized(struct drm_i915_private *dev_priv,
1501 struct drm_i915_gem_object *src,
1502 const int num_pages)
Chris Wilson9df30792010-02-18 10:24:56 +00001503{
1504 struct drm_i915_error_object *dst;
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001505 int i;
Chris Wilsone56660d2010-08-07 11:01:26 +01001506 u32 reloc_offset;
Chris Wilson9df30792010-02-18 10:24:56 +00001507
Chris Wilson05394f32010-11-08 19:18:58 +00001508 if (src == NULL || src->pages == NULL)
Chris Wilson9df30792010-02-18 10:24:56 +00001509 return NULL;
1510
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001511 dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +00001512 if (dst == NULL)
1513 return NULL;
1514
Chris Wilson05394f32010-11-08 19:18:58 +00001515 reloc_offset = src->gtt_offset;
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001516 for (i = 0; i < num_pages; i++) {
Andrew Morton788885a2010-05-11 14:07:05 -07001517 unsigned long flags;
Chris Wilsone56660d2010-08-07 11:01:26 +01001518 void *d;
Andrew Morton788885a2010-05-11 14:07:05 -07001519
Chris Wilsone56660d2010-08-07 11:01:26 +01001520 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
Chris Wilson9df30792010-02-18 10:24:56 +00001521 if (d == NULL)
1522 goto unwind;
Chris Wilsone56660d2010-08-07 11:01:26 +01001523
Andrew Morton788885a2010-05-11 14:07:05 -07001524 local_irq_save(flags);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001525 if (reloc_offset < dev_priv->gtt.mappable_end &&
Daniel Vetter74898d72012-02-15 23:50:22 +01001526 src->has_global_gtt_mapping) {
Chris Wilson172975aa2011-12-14 13:57:25 +01001527 void __iomem *s;
1528
1529 /* Simply ignore tiling or any overlapping fence.
1530 * It's part of the error state, and this hopefully
1531 * captures what the GPU read.
1532 */
1533
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001534 s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
Chris Wilson172975aa2011-12-14 13:57:25 +01001535 reloc_offset);
1536 memcpy_fromio(d, s, PAGE_SIZE);
1537 io_mapping_unmap_atomic(s);
Chris Wilson960e3562012-11-15 11:32:23 +00001538 } else if (src->stolen) {
1539 unsigned long offset;
1540
1541 offset = dev_priv->mm.stolen_base;
1542 offset += src->stolen->start;
1543 offset += i << PAGE_SHIFT;
1544
Daniel Vetter1a240d42012-11-29 22:18:51 +01001545 memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
Chris Wilson172975aa2011-12-14 13:57:25 +01001546 } else {
Chris Wilson9da3da62012-06-01 15:20:22 +01001547 struct page *page;
Chris Wilson172975aa2011-12-14 13:57:25 +01001548 void *s;
1549
Chris Wilson9da3da62012-06-01 15:20:22 +01001550 page = i915_gem_object_get_page(src, i);
Chris Wilson172975aa2011-12-14 13:57:25 +01001551
Chris Wilson9da3da62012-06-01 15:20:22 +01001552 drm_clflush_pages(&page, 1);
1553
1554 s = kmap_atomic(page);
Chris Wilson172975aa2011-12-14 13:57:25 +01001555 memcpy(d, s, PAGE_SIZE);
1556 kunmap_atomic(s);
1557
Chris Wilson9da3da62012-06-01 15:20:22 +01001558 drm_clflush_pages(&page, 1);
Chris Wilson172975aa2011-12-14 13:57:25 +01001559 }
Andrew Morton788885a2010-05-11 14:07:05 -07001560 local_irq_restore(flags);
Chris Wilsone56660d2010-08-07 11:01:26 +01001561
Chris Wilson9da3da62012-06-01 15:20:22 +01001562 dst->pages[i] = d;
Chris Wilsone56660d2010-08-07 11:01:26 +01001563
1564 reloc_offset += PAGE_SIZE;
Chris Wilson9df30792010-02-18 10:24:56 +00001565 }
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001566 dst->page_count = num_pages;
Chris Wilson05394f32010-11-08 19:18:58 +00001567 dst->gtt_offset = src->gtt_offset;
Chris Wilson9df30792010-02-18 10:24:56 +00001568
1569 return dst;
1570
1571unwind:
Chris Wilson9da3da62012-06-01 15:20:22 +01001572 while (i--)
1573 kfree(dst->pages[i]);
Chris Wilson9df30792010-02-18 10:24:56 +00001574 kfree(dst);
1575 return NULL;
1576}
Ben Widawskyd0d045e2013-02-24 18:10:00 -08001577#define i915_error_object_create(dev_priv, src) \
1578 i915_error_object_create_sized((dev_priv), (src), \
1579 (src)->base.size>>PAGE_SHIFT)
Chris Wilson9df30792010-02-18 10:24:56 +00001580
1581static void
1582i915_error_object_free(struct drm_i915_error_object *obj)
1583{
1584 int page;
1585
1586 if (obj == NULL)
1587 return;
1588
1589 for (page = 0; page < obj->page_count; page++)
1590 kfree(obj->pages[page]);
1591
1592 kfree(obj);
1593}
1594
Daniel Vetter742cbee2012-04-27 15:17:39 +02001595void
1596i915_error_state_free(struct kref *error_ref)
Chris Wilson9df30792010-02-18 10:24:56 +00001597{
Daniel Vetter742cbee2012-04-27 15:17:39 +02001598 struct drm_i915_error_state *error = container_of(error_ref,
1599 typeof(*error), ref);
Chris Wilsone2f973d2011-01-27 19:15:11 +00001600 int i;
1601
Chris Wilson52d39a22012-02-15 11:25:37 +00001602 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
1603 i915_error_object_free(error->ring[i].batchbuffer);
1604 i915_error_object_free(error->ring[i].ringbuffer);
Ben Widawsky7ed73da2013-05-25 14:42:54 -07001605 i915_error_object_free(error->ring[i].ctx);
Chris Wilson52d39a22012-02-15 11:25:37 +00001606 kfree(error->ring[i].requests);
1607 }
Chris Wilsone2f973d2011-01-27 19:15:11 +00001608
Chris Wilson9df30792010-02-18 10:24:56 +00001609 kfree(error->active_bo);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001610 kfree(error->overlay);
Ben Widawsky7ed73da2013-05-25 14:42:54 -07001611 kfree(error->display);
Chris Wilson9df30792010-02-18 10:24:56 +00001612 kfree(error);
1613}
Chris Wilson1b502472012-04-24 15:47:30 +01001614static void capture_bo(struct drm_i915_error_buffer *err,
1615 struct drm_i915_gem_object *obj)
1616{
1617 err->size = obj->base.size;
1618 err->name = obj->base.name;
Chris Wilson0201f1e2012-07-20 12:41:01 +01001619 err->rseqno = obj->last_read_seqno;
1620 err->wseqno = obj->last_write_seqno;
Chris Wilson1b502472012-04-24 15:47:30 +01001621 err->gtt_offset = obj->gtt_offset;
1622 err->read_domains = obj->base.read_domains;
1623 err->write_domain = obj->base.write_domain;
1624 err->fence_reg = obj->fence_reg;
1625 err->pinned = 0;
1626 if (obj->pin_count > 0)
1627 err->pinned = 1;
1628 if (obj->user_pin_count > 0)
1629 err->pinned = -1;
1630 err->tiling = obj->tiling_mode;
1631 err->dirty = obj->dirty;
1632 err->purgeable = obj->madv != I915_MADV_WILLNEED;
1633 err->ring = obj->ring ? obj->ring->id : -1;
1634 err->cache_level = obj->cache_level;
1635}
Chris Wilson9df30792010-02-18 10:24:56 +00001636
Chris Wilson1b502472012-04-24 15:47:30 +01001637static u32 capture_active_bo(struct drm_i915_error_buffer *err,
1638 int count, struct list_head *head)
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001639{
1640 struct drm_i915_gem_object *obj;
1641 int i = 0;
1642
1643 list_for_each_entry(obj, head, mm_list) {
Chris Wilson1b502472012-04-24 15:47:30 +01001644 capture_bo(err++, obj);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001645 if (++i == count)
1646 break;
Chris Wilson1b502472012-04-24 15:47:30 +01001647 }
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001648
Chris Wilson1b502472012-04-24 15:47:30 +01001649 return i;
1650}
1651
1652static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
1653 int count, struct list_head *head)
1654{
1655 struct drm_i915_gem_object *obj;
1656 int i = 0;
1657
Ben Widawsky35c20a62013-05-31 11:28:48 -07001658 list_for_each_entry(obj, head, global_list) {
Chris Wilson1b502472012-04-24 15:47:30 +01001659 if (obj->pin_count == 0)
1660 continue;
1661
1662 capture_bo(err++, obj);
1663 if (++i == count)
1664 break;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001665 }
1666
1667 return i;
1668}
1669
Chris Wilson748ebc62010-10-24 10:28:47 +01001670static void i915_gem_record_fences(struct drm_device *dev,
1671 struct drm_i915_error_state *error)
1672{
1673 struct drm_i915_private *dev_priv = dev->dev_private;
1674 int i;
1675
1676 /* Fences */
1677 switch (INTEL_INFO(dev)->gen) {
Daniel Vetter775d17b2011-10-09 21:52:01 +02001678 case 7:
Chris Wilson748ebc62010-10-24 10:28:47 +01001679 case 6:
Ville Syrjälä42b5aea2013-04-09 13:02:47 +03001680 for (i = 0; i < dev_priv->num_fence_regs; i++)
Chris Wilson748ebc62010-10-24 10:28:47 +01001681 error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
1682 break;
1683 case 5:
1684 case 4:
1685 for (i = 0; i < 16; i++)
1686 error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
1687 break;
1688 case 3:
1689 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
1690 for (i = 0; i < 8; i++)
1691 error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
1692 case 2:
1693 for (i = 0; i < 8; i++)
1694 error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
1695 break;
1696
Ben Widawsky7dbf9d62012-12-18 10:31:22 -08001697 default:
1698 BUG();
Chris Wilson748ebc62010-10-24 10:28:47 +01001699 }
1700}
1701
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001702static struct drm_i915_error_object *
1703i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
1704 struct intel_ring_buffer *ring)
1705{
1706 struct drm_i915_gem_object *obj;
1707 u32 seqno;
1708
1709 if (!ring->get_seqno)
1710 return NULL;
1711
Daniel Vetterb45305f2012-12-17 16:21:27 +01001712 if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
1713 u32 acthd = I915_READ(ACTHD);
1714
1715 if (WARN_ON(ring->id != RCS))
1716 return NULL;
1717
1718 obj = ring->private;
1719 if (acthd >= obj->gtt_offset &&
1720 acthd < obj->gtt_offset + obj->base.size)
1721 return i915_error_object_create(dev_priv, obj);
1722 }
1723
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001724 seqno = ring->get_seqno(ring, false);
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001725 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
1726 if (obj->ring != ring)
1727 continue;
1728
Chris Wilson0201f1e2012-07-20 12:41:01 +01001729 if (i915_seqno_passed(seqno, obj->last_read_seqno))
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001730 continue;
1731
1732 if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
1733 continue;
1734
1735 /* We need to copy these to an anonymous buffer as the simplest
1736 * method to avoid being overwritten by userspace.
1737 */
1738 return i915_error_object_create(dev_priv, obj);
1739 }
1740
1741 return NULL;
1742}
1743
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001744static void i915_record_ring_state(struct drm_device *dev,
1745 struct drm_i915_error_state *error,
1746 struct intel_ring_buffer *ring)
1747{
1748 struct drm_i915_private *dev_priv = dev->dev_private;
1749
Daniel Vetter33f3f512011-12-14 13:57:39 +01001750 if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilson12f55812012-07-05 17:14:01 +01001751 error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
Daniel Vetter33f3f512011-12-14 13:57:39 +01001752 error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001753 error->semaphore_mboxes[ring->id][0]
1754 = I915_READ(RING_SYNC_0(ring->mmio_base));
1755 error->semaphore_mboxes[ring->id][1]
1756 = I915_READ(RING_SYNC_1(ring->mmio_base));
Chris Wilsondf2b23d2012-11-27 17:06:54 +00001757 error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
1758 error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
Daniel Vetter33f3f512011-12-14 13:57:39 +01001759 }
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001760
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001761 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter9d2f41f2012-04-02 21:41:45 +02001762 error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001763 error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
1764 error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
1765 error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001766 error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
Ben Widawsky050ee912012-08-22 11:32:15 -07001767 if (ring->id == RCS)
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001768 error->bbaddr = I915_READ64(BB_ADDR);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001769 } else {
Daniel Vetter9d2f41f2012-04-02 21:41:45 +02001770 error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001771 error->ipeir[ring->id] = I915_READ(IPEIR);
1772 error->ipehr[ring->id] = I915_READ(IPEHR);
1773 error->instdone[ring->id] = I915_READ(INSTDONE);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001774 }
1775
Ben Widawsky9574b3f2012-04-26 16:03:01 -07001776 error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001777 error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001778 error->seqno[ring->id] = ring->get_seqno(ring, false);
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001779 error->acthd[ring->id] = intel_ring_get_active_head(ring);
Daniel Vetterc1cd90e2011-12-14 13:57:02 +01001780 error->head[ring->id] = I915_READ_HEAD(ring);
1781 error->tail[ring->id] = I915_READ_TAIL(ring);
Chris Wilson0f3b6842013-01-15 12:05:55 +00001782 error->ctl[ring->id] = I915_READ_CTL(ring);
Daniel Vetter7e3b8732012-02-01 22:26:45 +01001783
1784 error->cpu_ring_head[ring->id] = ring->head;
1785 error->cpu_ring_tail[ring->id] = ring->tail;
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001786}
1787
Ben Widawsky8c123e52013-03-04 17:00:29 -08001788
1789static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
1790 struct drm_i915_error_state *error,
1791 struct drm_i915_error_ring *ering)
1792{
1793 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1794 struct drm_i915_gem_object *obj;
1795
1796 /* Currently render ring is the only HW context user */
1797 if (ring->id != RCS || !error->ccid)
1798 return;
1799
Ben Widawsky35c20a62013-05-31 11:28:48 -07001800 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky8c123e52013-03-04 17:00:29 -08001801 if ((error->ccid & PAGE_MASK) == obj->gtt_offset) {
1802 ering->ctx = i915_error_object_create_sized(dev_priv,
1803 obj, 1);
1804 }
1805 }
1806}
1807
Chris Wilson52d39a22012-02-15 11:25:37 +00001808static void i915_gem_record_rings(struct drm_device *dev,
1809 struct drm_i915_error_state *error)
1810{
1811 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001812 struct intel_ring_buffer *ring;
Chris Wilson52d39a22012-02-15 11:25:37 +00001813 struct drm_i915_gem_request *request;
1814 int i, count;
1815
Chris Wilsonb4519512012-05-11 14:29:30 +01001816 for_each_ring(ring, dev_priv, i) {
Chris Wilson52d39a22012-02-15 11:25:37 +00001817 i915_record_ring_state(dev, error, ring);
1818
1819 error->ring[i].batchbuffer =
1820 i915_error_first_batchbuffer(dev_priv, ring);
1821
1822 error->ring[i].ringbuffer =
1823 i915_error_object_create(dev_priv, ring->obj);
1824
Ben Widawsky8c123e52013-03-04 17:00:29 -08001825
1826 i915_gem_record_active_context(ring, error, &error->ring[i]);
1827
Chris Wilson52d39a22012-02-15 11:25:37 +00001828 count = 0;
1829 list_for_each_entry(request, &ring->request_list, list)
1830 count++;
1831
1832 error->ring[i].num_requests = count;
1833 error->ring[i].requests =
1834 kmalloc(count*sizeof(struct drm_i915_error_request),
1835 GFP_ATOMIC);
1836 if (error->ring[i].requests == NULL) {
1837 error->ring[i].num_requests = 0;
1838 continue;
1839 }
1840
1841 count = 0;
1842 list_for_each_entry(request, &ring->request_list, list) {
1843 struct drm_i915_error_request *erq;
1844
1845 erq = &error->ring[i].requests[count++];
1846 erq->seqno = request->seqno;
1847 erq->jiffies = request->emitted_jiffies;
Chris Wilsonee4f42b2012-02-15 11:25:38 +00001848 erq->tail = request->tail;
Chris Wilson52d39a22012-02-15 11:25:37 +00001849 }
1850 }
1851}
1852
Jesse Barnes8a905232009-07-11 16:48:03 -04001853/**
1854 * i915_capture_error_state - capture an error record for later analysis
1855 * @dev: drm device
1856 *
1857 * Should be called when an error is detected (either a hang or an error
1858 * interrupt) to capture error state from the time of the error. Fills
1859 * out a structure which becomes available in debugfs for user level tools
1860 * to pick up.
1861 */
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001862static void i915_capture_error_state(struct drm_device *dev)
1863{
1864 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001865 struct drm_i915_gem_object *obj;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001866 struct drm_i915_error_state *error;
1867 unsigned long flags;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001868 int i, pipe;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001869
Daniel Vetter99584db2012-11-14 17:14:04 +01001870 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1871 error = dev_priv->gpu_error.first_error;
1872 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001873 if (error)
1874 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001875
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001876 /* Account for pipe specific data like PIPE*STAT */
Daniel Vetter33f3f512011-12-14 13:57:39 +01001877 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001878 if (!error) {
Chris Wilson9df30792010-02-18 10:24:56 +00001879 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1880 return;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001881 }
1882
Paulo Zanoni5d83d292013-03-06 20:03:22 -03001883 DRM_INFO("capturing error event; look for more information in "
Ben Widawsky2f86f192013-01-28 15:32:15 -08001884 "/sys/kernel/debug/dri/%d/i915_error_state\n",
Chris Wilsonb6f78332011-02-01 14:15:55 +00001885 dev->primary->index);
Chris Wilson2fa772f2010-10-01 13:23:27 +01001886
Daniel Vetter742cbee2012-04-27 15:17:39 +02001887 kref_init(&error->ref);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001888 error->eir = I915_READ(EIR);
1889 error->pgtbl_er = I915_READ(PGTBL_ER);
Ben Widawsky211816e2013-02-24 18:10:01 -08001890 if (HAS_HW_CONTEXTS(dev))
1891 error->ccid = I915_READ(CCID);
Ben Widawskybe998e22012-04-26 16:03:00 -07001892
1893 if (HAS_PCH_SPLIT(dev))
1894 error->ier = I915_READ(DEIER) | I915_READ(GTIER);
1895 else if (IS_VALLEYVIEW(dev))
1896 error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1897 else if (IS_GEN2(dev))
1898 error->ier = I915_READ16(IER);
1899 else
1900 error->ier = I915_READ(IER);
1901
Chris Wilson0f3b6842013-01-15 12:05:55 +00001902 if (INTEL_INFO(dev)->gen >= 6)
1903 error->derrmr = I915_READ(DERRMR);
1904
1905 if (IS_VALLEYVIEW(dev))
1906 error->forcewake = I915_READ(FORCEWAKE_VLV);
1907 else if (INTEL_INFO(dev)->gen >= 7)
1908 error->forcewake = I915_READ(FORCEWAKE_MT);
1909 else if (INTEL_INFO(dev)->gen == 6)
1910 error->forcewake = I915_READ(FORCEWAKE);
1911
Paulo Zanoni4f3308b2013-03-22 14:24:16 -03001912 if (!HAS_PCH_SPLIT(dev))
1913 for_each_pipe(pipe)
1914 error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
Daniel Vetterd27b1e02011-12-14 13:57:01 +01001915
Daniel Vetter33f3f512011-12-14 13:57:39 +01001916 if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilsonf4068392010-10-27 20:36:41 +01001917 error->error = I915_READ(ERROR_GEN6);
Daniel Vetter33f3f512011-12-14 13:57:39 +01001918 error->done_reg = I915_READ(DONE_REG);
1919 }
Chris Wilsonadd354d2010-10-29 19:00:51 +01001920
Ben Widawsky71e172e2012-08-20 16:15:13 -07001921 if (INTEL_INFO(dev)->gen == 7)
1922 error->err_int = I915_READ(GEN7_ERR_INT);
1923
Ben Widawsky050ee912012-08-22 11:32:15 -07001924 i915_get_extra_instdone(dev, error->extra_instdone);
1925
Chris Wilson748ebc62010-10-24 10:28:47 +01001926 i915_gem_record_fences(dev, error);
Chris Wilson52d39a22012-02-15 11:25:37 +00001927 i915_gem_record_rings(dev, error);
Chris Wilson9df30792010-02-18 10:24:56 +00001928
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001929 /* Record buffers on the active and pinned lists. */
Chris Wilson9df30792010-02-18 10:24:56 +00001930 error->active_bo = NULL;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001931 error->pinned_bo = NULL;
Chris Wilson9df30792010-02-18 10:24:56 +00001932
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001933 i = 0;
1934 list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
1935 i++;
1936 error->active_bo_count = i;
Ben Widawsky35c20a62013-05-31 11:28:48 -07001937 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
Chris Wilson1b502472012-04-24 15:47:30 +01001938 if (obj->pin_count)
1939 i++;
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001940 error->pinned_bo_count = i - error->active_bo_count;
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001941
Chris Wilson8e934db2011-01-24 12:34:00 +00001942 error->active_bo = NULL;
1943 error->pinned_bo = NULL;
Chris Wilsonbcfb2e22011-01-07 21:06:07 +00001944 if (i) {
1945 error->active_bo = kmalloc(sizeof(*error->active_bo)*i,
Chris Wilson9df30792010-02-18 10:24:56 +00001946 GFP_ATOMIC);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001947 if (error->active_bo)
1948 error->pinned_bo =
1949 error->active_bo + error->active_bo_count;
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001950 }
1951
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001952 if (error->active_bo)
1953 error->active_bo_count =
Chris Wilson1b502472012-04-24 15:47:30 +01001954 capture_active_bo(error->active_bo,
1955 error->active_bo_count,
1956 &dev_priv->mm.active_list);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001957
1958 if (error->pinned_bo)
1959 error->pinned_bo_count =
Chris Wilson1b502472012-04-24 15:47:30 +01001960 capture_pinned_bo(error->pinned_bo,
1961 error->pinned_bo_count,
Chris Wilson6c085a72012-08-20 11:40:46 +02001962 &dev_priv->mm.bound_list);
Chris Wilsonc724e8a2010-11-22 08:07:02 +00001963
Jesse Barnes8a905232009-07-11 16:48:03 -04001964 do_gettimeofday(&error->time);
1965
Chris Wilson6ef3d422010-08-04 20:26:07 +01001966 error->overlay = intel_overlay_capture_error_state(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00001967 error->display = intel_display_capture_error_state(dev);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001968
Daniel Vetter99584db2012-11-14 17:14:04 +01001969 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1970 if (dev_priv->gpu_error.first_error == NULL) {
1971 dev_priv->gpu_error.first_error = error;
Chris Wilson9df30792010-02-18 10:24:56 +00001972 error = NULL;
1973 }
Daniel Vetter99584db2012-11-14 17:14:04 +01001974 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001975
1976 if (error)
Daniel Vetter742cbee2012-04-27 15:17:39 +02001977 i915_error_state_free(&error->ref);
Chris Wilson9df30792010-02-18 10:24:56 +00001978}
1979
1980void i915_destroy_error_state(struct drm_device *dev)
1981{
1982 struct drm_i915_private *dev_priv = dev->dev_private;
1983 struct drm_i915_error_state *error;
Ben Widawsky6dc0e812012-01-23 15:30:02 -08001984 unsigned long flags;
Chris Wilson9df30792010-02-18 10:24:56 +00001985
Daniel Vetter99584db2012-11-14 17:14:04 +01001986 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1987 error = dev_priv->gpu_error.first_error;
1988 dev_priv->gpu_error.first_error = NULL;
1989 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
Chris Wilson9df30792010-02-18 10:24:56 +00001990
1991 if (error)
Daniel Vetter742cbee2012-04-27 15:17:39 +02001992 kref_put(&error->ref, i915_error_state_free);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001993}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001994#else
1995#define i915_capture_error_state(x)
1996#endif
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001997
Chris Wilson35aed2e2010-05-27 13:18:12 +01001998static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04001999{
2000 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybd9854f2012-08-23 15:18:09 -07002001 uint32_t instdone[I915_NUM_INSTDONE_REG];
Jesse Barnes8a905232009-07-11 16:48:03 -04002002 u32 eir = I915_READ(EIR);
Ben Widawsky050ee912012-08-22 11:32:15 -07002003 int pipe, i;
Jesse Barnes8a905232009-07-11 16:48:03 -04002004
Chris Wilson35aed2e2010-05-27 13:18:12 +01002005 if (!eir)
2006 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04002007
Joe Perchesa70491c2012-03-18 13:00:11 -07002008 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04002009
Ben Widawskybd9854f2012-08-23 15:18:09 -07002010 i915_get_extra_instdone(dev, instdone);
2011
Jesse Barnes8a905232009-07-11 16:48:03 -04002012 if (IS_G4X(dev)) {
2013 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
2014 u32 ipeir = I915_READ(IPEIR_I965);
2015
Joe Perchesa70491c2012-03-18 13:00:11 -07002016 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2017 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Ben Widawsky050ee912012-08-22 11:32:15 -07002018 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2019 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Joe Perchesa70491c2012-03-18 13:00:11 -07002020 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002021 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002022 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002023 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002024 }
2025 if (eir & GM45_ERROR_PAGE_TABLE) {
2026 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002027 pr_err("page table error\n");
2028 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002029 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002030 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002031 }
2032 }
2033
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002034 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002035 if (eir & I915_ERROR_PAGE_TABLE) {
2036 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07002037 pr_err("page table error\n");
2038 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04002039 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002040 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04002041 }
2042 }
2043
2044 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002045 pr_err("memory refresh error:\n");
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002046 for_each_pipe(pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07002047 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002048 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04002049 /* pipestat has already been acked */
2050 }
2051 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07002052 pr_err("instruction error\n");
2053 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Ben Widawsky050ee912012-08-22 11:32:15 -07002054 for (i = 0; i < ARRAY_SIZE(instdone); i++)
2055 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002056 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04002057 u32 ipeir = I915_READ(IPEIR);
2058
Joe Perchesa70491c2012-03-18 13:00:11 -07002059 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
2060 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
Joe Perchesa70491c2012-03-18 13:00:11 -07002061 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04002062 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002063 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002064 } else {
2065 u32 ipeir = I915_READ(IPEIR_I965);
2066
Joe Perchesa70491c2012-03-18 13:00:11 -07002067 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2068 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07002069 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07002070 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04002071 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002072 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04002073 }
2074 }
2075
2076 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002077 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04002078 eir = I915_READ(EIR);
2079 if (eir) {
2080 /*
2081 * some errors might have become stuck,
2082 * mask them.
2083 */
2084 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
2085 I915_WRITE(EMR, I915_READ(EMR) | eir);
2086 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2087 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01002088}
2089
2090/**
2091 * i915_handle_error - handle an error interrupt
2092 * @dev: drm device
2093 *
2094 * Do some basic checking of regsiter state at error interrupt time and
2095 * dump it to the syslog. Also call i915_capture_error_state() to make
2096 * sure we get a record and make it available in debugfs. Fire a uevent
2097 * so userspace knows something bad happened (should trigger collection
2098 * of a ring dump etc.).
2099 */
Chris Wilson527f9e92010-11-11 01:16:58 +00002100void i915_handle_error(struct drm_device *dev, bool wedged)
Chris Wilson35aed2e2010-05-27 13:18:12 +01002101{
2102 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002103 struct intel_ring_buffer *ring;
2104 int i;
Chris Wilson35aed2e2010-05-27 13:18:12 +01002105
2106 i915_capture_error_state(dev);
2107 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04002108
Ben Gamariba1234d2009-09-14 17:48:47 -04002109 if (wedged) {
Daniel Vetterf69061b2012-12-06 09:01:42 +01002110 atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2111 &dev_priv->gpu_error.reset_counter);
Ben Gamariba1234d2009-09-14 17:48:47 -04002112
Ben Gamari11ed50e2009-09-14 17:48:45 -04002113 /*
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002114 * Wakeup waiting processes so that the reset work item
2115 * doesn't deadlock trying to grab various locks.
Ben Gamari11ed50e2009-09-14 17:48:45 -04002116 */
Chris Wilsonb4519512012-05-11 14:29:30 +01002117 for_each_ring(ring, dev_priv, i)
2118 wake_up_all(&ring->irq_queue);
Ben Gamari11ed50e2009-09-14 17:48:45 -04002119 }
2120
Daniel Vetter99584db2012-11-14 17:14:04 +01002121 queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
Jesse Barnes8a905232009-07-11 16:48:03 -04002122}
2123
Ville Syrjälä21ad8332013-02-19 15:16:39 +02002124static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002125{
2126 drm_i915_private_t *dev_priv = dev->dev_private;
2127 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
2128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00002129 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002130 struct intel_unpin_work *work;
2131 unsigned long flags;
2132 bool stall_detected;
2133
2134 /* Ignore early vblank irqs */
2135 if (intel_crtc == NULL)
2136 return;
2137
2138 spin_lock_irqsave(&dev->event_lock, flags);
2139 work = intel_crtc->unpin_work;
2140
Chris Wilsone7d841c2012-12-03 11:36:30 +00002141 if (work == NULL ||
2142 atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2143 !work->enable_stall_check) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002144 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
2145 spin_unlock_irqrestore(&dev->event_lock, flags);
2146 return;
2147 }
2148
2149 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00002150 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002151 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002152 int dspsurf = DSPSURF(intel_crtc->plane);
Armin Reese446f2542012-03-30 16:20:16 -07002153 stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2154 obj->gtt_offset;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002155 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002156 int dspaddr = DSPADDR(intel_crtc->plane);
Chris Wilson05394f32010-11-08 19:18:58 +00002157 stall_detected = I915_READ(dspaddr) == (obj->gtt_offset +
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002158 crtc->y * crtc->fb->pitches[0] +
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01002159 crtc->x * crtc->fb->bits_per_pixel/8);
2160 }
2161
2162 spin_unlock_irqrestore(&dev->event_lock, flags);
2163
2164 if (stall_detected) {
2165 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
2166 intel_prepare_page_flip(dev, intel_crtc->plane);
2167 }
2168}
2169
Keith Packard42f52ef2008-10-18 19:39:29 -07002170/* Called from drm generic code, passed 'crtc' which
2171 * we use as a pipe index
2172 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002173static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002174{
2175 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002176 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002177
Chris Wilson5eddb702010-09-11 13:48:45 +01002178 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08002179 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002180
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002181 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002182 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08002183 i915_enable_pipestat(dev_priv, pipe,
2184 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -07002185 else
Keith Packard7c463582008-11-04 02:03:27 -08002186 i915_enable_pipestat(dev_priv, pipe,
2187 PIPE_VBLANK_INTERRUPT_ENABLE);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002188
2189 /* maintain vblank delivery even in deep C-states */
2190 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02002191 I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002192 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002193
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002194 return 0;
2195}
2196
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002197static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002198{
2199 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2200 unsigned long irqflags;
2201
2202 if (!i915_pipe_enabled(dev, pipe))
2203 return -EINVAL;
2204
2205 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2206 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04002207 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Jesse Barnesf796cf82011-04-07 13:58:17 -07002208 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2209
2210 return 0;
2211}
2212
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002213static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002214{
2215 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2216 unsigned long irqflags;
2217
2218 if (!i915_pipe_enabled(dev, pipe))
2219 return -EINVAL;
2220
2221 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilsonb615b572012-05-02 09:52:12 +01002222 ironlake_enable_display_irq(dev_priv,
2223 DE_PIPEA_VBLANK_IVB << (5 * pipe));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002224 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2225
2226 return 0;
2227}
2228
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002229static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
2230{
2231 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2232 unsigned long irqflags;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002233 u32 imr;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002234
2235 if (!i915_pipe_enabled(dev, pipe))
2236 return -EINVAL;
2237
2238 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002239 imr = I915_READ(VLV_IMR);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002240 if (pipe == 0)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002241 imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002242 else
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002243 imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002244 I915_WRITE(VLV_IMR, imr);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002245 i915_enable_pipestat(dev_priv, pipe,
2246 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002247 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2248
2249 return 0;
2250}
2251
Keith Packard42f52ef2008-10-18 19:39:29 -07002252/* Called from drm generic code, passed 'crtc' which
2253 * we use as a pipe index
2254 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002255static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002256{
2257 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07002258 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002259
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002260 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00002261 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02002262 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson8692d00e2011-02-05 10:08:21 +00002263
Jesse Barnesf796cf82011-04-07 13:58:17 -07002264 i915_disable_pipestat(dev_priv, pipe,
2265 PIPE_VBLANK_INTERRUPT_ENABLE |
2266 PIPE_START_VBLANK_INTERRUPT_ENABLE);
2267 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2268}
2269
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002270static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07002271{
2272 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2273 unsigned long irqflags;
2274
2275 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2276 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
Akshay Joshi0206e352011-08-16 15:34:10 -04002277 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002278 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07002279}
2280
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002281static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002282{
2283 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2284 unsigned long irqflags;
2285
2286 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilsonb615b572012-05-02 09:52:12 +01002287 ironlake_disable_display_irq(dev_priv,
2288 DE_PIPEA_VBLANK_IVB << (pipe * 5));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002289 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2290}
2291
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002292static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
2293{
2294 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2295 unsigned long irqflags;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002296 u32 imr;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002297
2298 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002299 i915_disable_pipestat(dev_priv, pipe,
2300 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002301 imr = I915_READ(VLV_IMR);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002302 if (pipe == 0)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002303 imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002304 else
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002305 imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002306 I915_WRITE(VLV_IMR, imr);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002307 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2308}
2309
Chris Wilson893eead2010-10-27 14:44:35 +01002310static u32
2311ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08002312{
Chris Wilson893eead2010-10-27 14:44:35 +01002313 return list_entry(ring->request_list.prev,
2314 struct drm_i915_gem_request, list)->seqno;
2315}
2316
Chris Wilson9107e9d2013-06-10 11:20:20 +01002317static bool
2318ring_idle(struct intel_ring_buffer *ring, u32 seqno)
Chris Wilson893eead2010-10-27 14:44:35 +01002319{
Chris Wilson9107e9d2013-06-10 11:20:20 +01002320 return (list_empty(&ring->request_list) ||
2321 i915_seqno_passed(seqno, ring_last_seqno(ring)));
Ben Gamarif65d9422009-09-14 17:48:44 -04002322}
2323
Chris Wilsona24a11e2013-03-14 17:52:05 +02002324static bool semaphore_passed(struct intel_ring_buffer *ring)
2325{
2326 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2327 u32 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2328 struct intel_ring_buffer *signaller;
2329 u32 cmd, ipehr, acthd_min;
2330
2331 ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2332 if ((ipehr & ~(0x3 << 16)) !=
2333 (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
2334 return false;
2335
2336 /* ACTHD is likely pointing to the dword after the actual command,
2337 * so scan backwards until we find the MBOX.
2338 */
2339 acthd_min = max((int)acthd - 3 * 4, 0);
2340 do {
2341 cmd = ioread32(ring->virtual_start + acthd);
2342 if (cmd == ipehr)
2343 break;
2344
2345 acthd -= 4;
2346 if (acthd < acthd_min)
2347 return false;
2348 } while (1);
2349
2350 signaller = &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2351 return i915_seqno_passed(signaller->get_seqno(signaller, false),
2352 ioread32(ring->virtual_start+acthd+4)+1);
2353}
2354
Chris Wilson9107e9d2013-06-10 11:20:20 +01002355static bool ring_hung(struct intel_ring_buffer *ring)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002356{
2357 struct drm_device *dev = ring->dev;
2358 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002359 u32 tmp;
2360
2361 if (IS_GEN2(dev))
2362 return true;
2363
2364 /* Is the chip hanging on a WAIT_FOR_EVENT?
2365 * If so we can simply poke the RB_WAIT bit
2366 * and break the hang. This should work on
2367 * all but the second generation chipsets.
2368 */
2369 tmp = I915_READ_CTL(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002370 if (tmp & RING_WAIT) {
2371 DRM_ERROR("Kicking stuck wait on %s\n",
2372 ring->name);
2373 I915_WRITE_CTL(ring, tmp);
Chris Wilson9107e9d2013-06-10 11:20:20 +01002374 return false;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002375 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02002376
2377 if (INTEL_INFO(dev)->gen >= 6 &&
2378 tmp & RING_WAIT_SEMAPHORE &&
2379 semaphore_passed(ring)) {
2380 DRM_ERROR("Kicking stuck semaphore on %s\n",
2381 ring->name);
2382 I915_WRITE_CTL(ring, tmp);
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002383 return false;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002384 }
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002385
Chris Wilson9107e9d2013-06-10 11:20:20 +01002386 return true;
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002387}
2388
Ben Gamarif65d9422009-09-14 17:48:44 -04002389/**
2390 * This is called when the chip hasn't reported back with completed
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002391 * batchbuffers in a long time. We keep track per ring seqno progress and
2392 * if there are no progress, hangcheck score for that ring is increased.
2393 * Further, acthd is inspected to see if the ring is stuck. On stuck case
2394 * we kick the ring. If we see no progress on three subsequent calls
2395 * we assume chip is wedged and try to fix it by resetting the chip.
Ben Gamarif65d9422009-09-14 17:48:44 -04002396 */
2397void i915_hangcheck_elapsed(unsigned long data)
2398{
2399 struct drm_device *dev = (struct drm_device *)data;
2400 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002401 struct intel_ring_buffer *ring;
Chris Wilsonb4519512012-05-11 14:29:30 +01002402 int i;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002403 int busy_count = 0, rings_hung = 0;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002404 bool stuck[I915_NUM_RINGS] = { 0 };
2405#define BUSY 1
2406#define KICK 5
2407#define HUNG 20
2408#define FIRE 30
Chris Wilson893eead2010-10-27 14:44:35 +01002409
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07002410 if (!i915_enable_hangcheck)
2411 return;
2412
Chris Wilsonb4519512012-05-11 14:29:30 +01002413 for_each_ring(ring, dev_priv, i) {
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002414 u32 seqno, acthd;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002415 bool busy = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01002416
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002417 seqno = ring->get_seqno(ring, false);
2418 acthd = intel_ring_get_active_head(ring);
Chris Wilsond1e61e72012-04-10 17:00:41 +01002419
Chris Wilson9107e9d2013-06-10 11:20:20 +01002420 if (ring->hangcheck.seqno == seqno) {
2421 if (ring_idle(ring, seqno)) {
2422 if (waitqueue_active(&ring->irq_queue)) {
2423 /* Issue a wake-up to catch stuck h/w. */
2424 DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
2425 ring->name);
2426 wake_up_all(&ring->irq_queue);
2427 ring->hangcheck.score += HUNG;
2428 } else
2429 busy = false;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002430 } else {
Chris Wilson9107e9d2013-06-10 11:20:20 +01002431 int score;
2432
2433 stuck[i] = ring->hangcheck.acthd == acthd;
2434 if (stuck[i]) {
2435 /* Every time we kick the ring, add a
2436 * small increment to the hangcheck
2437 * score so that we can catch a
2438 * batch that is repeatedly kicked.
2439 */
2440 score = ring_hung(ring) ? HUNG : KICK;
2441 } else
2442 score = BUSY;
2443
2444 ring->hangcheck.score += score;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002445 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002446 } else {
2447 /* Gradually reduce the count so that we catch DoS
2448 * attempts across multiple batches.
2449 */
2450 if (ring->hangcheck.score > 0)
2451 ring->hangcheck.score--;
Chris Wilsond1e61e72012-04-10 17:00:41 +01002452 }
2453
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002454 ring->hangcheck.seqno = seqno;
2455 ring->hangcheck.acthd = acthd;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002456 busy_count += busy;
Chris Wilson893eead2010-10-27 14:44:35 +01002457 }
Eric Anholtb9201c12010-01-08 14:25:16 -08002458
Mika Kuoppala92cab732013-05-24 17:16:07 +03002459 for_each_ring(ring, dev_priv, i) {
Chris Wilson9107e9d2013-06-10 11:20:20 +01002460 if (ring->hangcheck.score > FIRE) {
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002461 rings_hung++;
2462 DRM_ERROR("%s: %s on %s 0x%x\n", ring->name,
2463 stuck[i] ? "stuck" : "no progress",
2464 stuck[i] ? "addr" : "seqno",
2465 stuck[i] ? ring->hangcheck.acthd & HEAD_ADDR :
2466 ring->hangcheck.seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03002467 }
2468 }
2469
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002470 if (rings_hung)
2471 return i915_handle_error(dev, true);
Ben Gamarif65d9422009-09-14 17:48:44 -04002472
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002473 if (busy_count)
2474 /* Reset timer case chip hangs without another request
2475 * being added */
2476 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2477 round_jiffies_up(jiffies +
2478 DRM_I915_HANGCHECK_JIFFIES));
Ben Gamarif65d9422009-09-14 17:48:44 -04002479}
2480
Paulo Zanoni91738a92013-06-05 14:21:51 -03002481static void ibx_irq_preinstall(struct drm_device *dev)
2482{
2483 struct drm_i915_private *dev_priv = dev->dev_private;
2484
2485 if (HAS_PCH_NOP(dev))
2486 return;
2487
2488 /* south display irq */
2489 I915_WRITE(SDEIMR, 0xffffffff);
2490 /*
2491 * SDEIER is also touched by the interrupt handler to work around missed
2492 * PCH interrupts. Hence we can't update it after the interrupt handler
2493 * is enabled - instead we unconditionally enable all PCH interrupt
2494 * sources here, but then only unmask them as needed with SDEIMR.
2495 */
2496 I915_WRITE(SDEIER, 0xffffffff);
2497 POSTING_READ(SDEIER);
2498}
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500/* drm_dma.h hooks
2501*/
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002502static void ironlake_irq_preinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002503{
2504 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2505
Jesse Barnes46979952011-04-07 13:53:55 -07002506 atomic_set(&dev_priv->irq_received, 0);
2507
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002508 I915_WRITE(HWSTAM, 0xeffe);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01002509
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002510 /* XXX hotplug from PCH */
2511
2512 I915_WRITE(DEIMR, 0xffffffff);
2513 I915_WRITE(DEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002514 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002515
2516 /* and GT */
2517 I915_WRITE(GTIMR, 0xffffffff);
2518 I915_WRITE(GTIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002519 POSTING_READ(GTIER);
Zhenyu Wangc6501562009-11-03 18:57:21 +00002520
Paulo Zanoni91738a92013-06-05 14:21:51 -03002521 ibx_irq_preinstall(dev);
Ben Widawsky7d991632013-05-28 19:22:25 -07002522}
2523
2524static void ivybridge_irq_preinstall(struct drm_device *dev)
2525{
2526 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2527
2528 atomic_set(&dev_priv->irq_received, 0);
2529
2530 I915_WRITE(HWSTAM, 0xeffe);
2531
2532 /* XXX hotplug from PCH */
2533
2534 I915_WRITE(DEIMR, 0xffffffff);
2535 I915_WRITE(DEIER, 0x0);
2536 POSTING_READ(DEIER);
2537
2538 /* and GT */
2539 I915_WRITE(GTIMR, 0xffffffff);
2540 I915_WRITE(GTIER, 0x0);
2541 POSTING_READ(GTIER);
2542
Ben Widawskyeda63ff2013-05-28 19:22:26 -07002543 /* Power management */
2544 I915_WRITE(GEN6_PMIMR, 0xffffffff);
2545 I915_WRITE(GEN6_PMIER, 0x0);
2546 POSTING_READ(GEN6_PMIER);
2547
Paulo Zanoni91738a92013-06-05 14:21:51 -03002548 ibx_irq_preinstall(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002549}
2550
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002551static void valleyview_irq_preinstall(struct drm_device *dev)
2552{
2553 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2554 int pipe;
2555
2556 atomic_set(&dev_priv->irq_received, 0);
2557
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002558 /* VLV magic */
2559 I915_WRITE(VLV_IMR, 0);
2560 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
2561 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
2562 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
2563
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002564 /* and GT */
2565 I915_WRITE(GTIIR, I915_READ(GTIIR));
2566 I915_WRITE(GTIIR, I915_READ(GTIIR));
2567 I915_WRITE(GTIMR, 0xffffffff);
2568 I915_WRITE(GTIER, 0x0);
2569 POSTING_READ(GTIER);
2570
2571 I915_WRITE(DPINVGTT, 0xff);
2572
2573 I915_WRITE(PORT_HOTPLUG_EN, 0);
2574 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2575 for_each_pipe(pipe)
2576 I915_WRITE(PIPESTAT(pipe), 0xffff);
2577 I915_WRITE(VLV_IIR, 0xffffffff);
2578 I915_WRITE(VLV_IMR, 0xffffffff);
2579 I915_WRITE(VLV_IER, 0x0);
2580 POSTING_READ(VLV_IER);
2581}
2582
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002583static void ibx_hpd_irq_setup(struct drm_device *dev)
Keith Packard7fe0b972011-09-19 13:31:02 -07002584{
2585 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002586 struct drm_mode_config *mode_config = &dev->mode_config;
2587 struct intel_encoder *intel_encoder;
2588 u32 mask = ~I915_READ(SDEIMR);
2589 u32 hotplug;
Keith Packard7fe0b972011-09-19 13:31:02 -07002590
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002591 if (HAS_PCH_IBX(dev)) {
Egbert Eich995e6b32013-04-16 13:36:56 +02002592 mask &= ~SDE_HOTPLUG_MASK;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002593 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
Egbert Eichcd569ae2013-04-16 13:36:57 +02002594 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2595 mask |= hpd_ibx[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002596 } else {
Egbert Eich995e6b32013-04-16 13:36:56 +02002597 mask &= ~SDE_HOTPLUG_MASK_CPT;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002598 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
Egbert Eichcd569ae2013-04-16 13:36:57 +02002599 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2600 mask |= hpd_cpt[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002601 }
2602
2603 I915_WRITE(SDEIMR, ~mask);
2604
2605 /*
2606 * Enable digital hotplug on the PCH, and configure the DP short pulse
2607 * duration to 2ms (which is the minimum in the Display Port spec)
2608 *
2609 * This register is the same on all known PCH chips.
2610 */
Keith Packard7fe0b972011-09-19 13:31:02 -07002611 hotplug = I915_READ(PCH_PORT_HOTPLUG);
2612 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
2613 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
2614 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
2615 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
2616 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
2617}
2618
Paulo Zanonid46da432013-02-08 17:35:15 -02002619static void ibx_irq_postinstall(struct drm_device *dev)
2620{
2621 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002622 u32 mask;
Paulo Zanonid46da432013-02-08 17:35:15 -02002623
Daniel Vetter692a04c2013-05-29 21:43:05 +02002624 if (HAS_PCH_NOP(dev))
2625 return;
2626
Paulo Zanoni86642812013-04-12 17:57:57 -03002627 if (HAS_PCH_IBX(dev)) {
2628 mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
Paulo Zanonide032bf2013-04-12 17:57:58 -03002629 SDE_TRANSA_FIFO_UNDER | SDE_POISON;
Paulo Zanoni86642812013-04-12 17:57:57 -03002630 } else {
2631 mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
2632
2633 I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2634 }
Ben Widawskyab5c6082013-04-05 13:12:41 -07002635
Paulo Zanonid46da432013-02-08 17:35:15 -02002636 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2637 I915_WRITE(SDEIMR, ~mask);
Paulo Zanonid46da432013-02-08 17:35:15 -02002638}
2639
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002640static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002641{
2642 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2643 /* enable kind of interrupts always enabled */
Jesse Barnes013d5aa2010-01-29 11:18:31 -08002644 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
Daniel Vetterce99c252012-12-01 13:53:47 +01002645 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
Paulo Zanoni86642812013-04-12 17:57:57 -03002646 DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
Paulo Zanonide032bf2013-04-12 17:57:58 -03002647 DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
Ben Widawskycc609d52013-05-28 19:22:29 -07002648 u32 gt_irqs;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002649
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002650 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002651
2652 /* should always can generate irq */
2653 I915_WRITE(DEIIR, I915_READ(DEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002654 I915_WRITE(DEIMR, dev_priv->irq_mask);
2655 I915_WRITE(DEIER, display_mask | DE_PIPEA_VBLANK | DE_PIPEB_VBLANK);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002656 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002657
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002658 dev_priv->gt_irq_mask = ~0;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002659
2660 I915_WRITE(GTIIR, I915_READ(GTIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002661 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002662
Ben Widawskycc609d52013-05-28 19:22:29 -07002663 gt_irqs = GT_RENDER_USER_INTERRUPT;
2664
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002665 if (IS_GEN6(dev))
Ben Widawskycc609d52013-05-28 19:22:29 -07002666 gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002667 else
Ben Widawskycc609d52013-05-28 19:22:29 -07002668 gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
2669 ILK_BSD_USER_INTERRUPT;
2670
2671 I915_WRITE(GTIER, gt_irqs);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002672 POSTING_READ(GTIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002673
Paulo Zanonid46da432013-02-08 17:35:15 -02002674 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07002675
Jesse Barnesf97108d2010-01-29 11:27:07 -08002676 if (IS_IRONLAKE_M(dev)) {
2677 /* Clear & enable PCU event interrupts */
2678 I915_WRITE(DEIIR, DE_PCU_EVENT);
2679 I915_WRITE(DEIER, I915_READ(DEIER) | DE_PCU_EVENT);
2680 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2681 }
2682
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002683 return 0;
2684}
2685
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002686static int ivybridge_irq_postinstall(struct drm_device *dev)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002687{
2688 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2689 /* enable kind of interrupts always enabled */
Chris Wilsonb615b572012-05-02 09:52:12 +01002690 u32 display_mask =
2691 DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2692 DE_PLANEC_FLIP_DONE_IVB |
2693 DE_PLANEB_FLIP_DONE_IVB |
Daniel Vetterce99c252012-12-01 13:53:47 +01002694 DE_PLANEA_FLIP_DONE_IVB |
Paulo Zanoni86642812013-04-12 17:57:57 -03002695 DE_AUX_CHANNEL_A_IVB |
2696 DE_ERR_INT_IVB;
Ben Widawsky12638c52013-05-28 19:22:31 -07002697 u32 pm_irqs = GEN6_PM_RPS_EVENTS;
Ben Widawskycc609d52013-05-28 19:22:29 -07002698 u32 gt_irqs;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002699
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002700 dev_priv->irq_mask = ~display_mask;
2701
2702 /* should always can generate irq */
Paulo Zanoni86642812013-04-12 17:57:57 -03002703 I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002704 I915_WRITE(DEIIR, I915_READ(DEIIR));
2705 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilsonb615b572012-05-02 09:52:12 +01002706 I915_WRITE(DEIER,
2707 display_mask |
2708 DE_PIPEC_VBLANK_IVB |
2709 DE_PIPEB_VBLANK_IVB |
2710 DE_PIPEA_VBLANK_IVB);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002711 POSTING_READ(DEIER);
2712
Ben Widawskycc609d52013-05-28 19:22:29 -07002713 dev_priv->gt_irq_mask = ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002714
2715 I915_WRITE(GTIIR, I915_READ(GTIIR));
2716 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2717
Ben Widawskycc609d52013-05-28 19:22:29 -07002718 gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2719 GT_BLT_USER_INTERRUPT | GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2720 I915_WRITE(GTIER, gt_irqs);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002721 POSTING_READ(GTIER);
2722
Ben Widawsky12638c52013-05-28 19:22:31 -07002723 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2724 if (HAS_VEBOX(dev))
2725 pm_irqs |= PM_VEBOX_USER_INTERRUPT |
2726 PM_VEBOX_CS_ERROR_INTERRUPT;
2727
2728 /* Our enable/disable rps functions may touch these registers so
2729 * make sure to set a known state for only the non-RPS bits.
2730 * The RMW is extra paranoia since this should be called after being set
2731 * to a known state in preinstall.
2732 * */
2733 I915_WRITE(GEN6_PMIMR,
2734 (I915_READ(GEN6_PMIMR) | ~GEN6_PM_RPS_EVENTS) & ~pm_irqs);
2735 I915_WRITE(GEN6_PMIER,
2736 (I915_READ(GEN6_PMIER) & GEN6_PM_RPS_EVENTS) | pm_irqs);
2737 POSTING_READ(GEN6_PMIER);
Ben Widawskyeda63ff2013-05-28 19:22:26 -07002738
Paulo Zanonid46da432013-02-08 17:35:15 -02002739 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07002740
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07002741 return 0;
2742}
2743
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002744static int valleyview_irq_postinstall(struct drm_device *dev)
2745{
2746 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Ben Widawskycc609d52013-05-28 19:22:29 -07002747 u32 gt_irqs;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002748 u32 enable_mask;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002749 u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002750
2751 enable_mask = I915_DISPLAY_PORT_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002752 enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2753 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2754 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002755 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
2756
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002757 /*
2758 *Leave vblank interrupts masked initially. enable/disable will
2759 * toggle them based on usage.
2760 */
2761 dev_priv->irq_mask = (~enable_mask) |
2762 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2763 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002764
Daniel Vetter20afbda2012-12-11 14:05:07 +01002765 I915_WRITE(PORT_HOTPLUG_EN, 0);
2766 POSTING_READ(PORT_HOTPLUG_EN);
2767
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002768 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
2769 I915_WRITE(VLV_IER, enable_mask);
2770 I915_WRITE(VLV_IIR, 0xffffffff);
2771 I915_WRITE(PIPESTAT(0), 0xffff);
2772 I915_WRITE(PIPESTAT(1), 0xffff);
2773 POSTING_READ(VLV_IER);
2774
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002775 i915_enable_pipestat(dev_priv, 0, pipestat_enable);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01002776 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002777 i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2778
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002779 I915_WRITE(VLV_IIR, 0xffffffff);
2780 I915_WRITE(VLV_IIR, 0xffffffff);
2781
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002782 I915_WRITE(GTIIR, I915_READ(GTIIR));
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002783 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
Jesse Barnes3bcedbe2012-09-19 13:29:01 -07002784
Ben Widawskycc609d52013-05-28 19:22:29 -07002785 gt_irqs = GT_RENDER_USER_INTERRUPT | GT_BSD_USER_INTERRUPT |
2786 GT_BLT_USER_INTERRUPT;
2787 I915_WRITE(GTIER, gt_irqs);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002788 POSTING_READ(GTIER);
2789
2790 /* ack & enable invalid PTE error interrupts */
2791#if 0 /* FIXME: add support to irq handler for checking these bits */
2792 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
2793 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
2794#endif
2795
2796 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
Daniel Vetter20afbda2012-12-11 14:05:07 +01002797
2798 return 0;
2799}
2800
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002801static void valleyview_irq_uninstall(struct drm_device *dev)
2802{
2803 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2804 int pipe;
2805
2806 if (!dev_priv)
2807 return;
2808
Egbert Eichac4c16c2013-04-16 13:36:58 +02002809 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2810
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002811 for_each_pipe(pipe)
2812 I915_WRITE(PIPESTAT(pipe), 0xffff);
2813
2814 I915_WRITE(HWSTAM, 0xffffffff);
2815 I915_WRITE(PORT_HOTPLUG_EN, 0);
2816 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2817 for_each_pipe(pipe)
2818 I915_WRITE(PIPESTAT(pipe), 0xffff);
2819 I915_WRITE(VLV_IIR, 0xffffffff);
2820 I915_WRITE(VLV_IMR, 0xffffffff);
2821 I915_WRITE(VLV_IER, 0x0);
2822 POSTING_READ(VLV_IER);
2823}
2824
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002825static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002826{
2827 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07002828
2829 if (!dev_priv)
2830 return;
2831
Egbert Eichac4c16c2013-04-16 13:36:58 +02002832 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2833
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002834 I915_WRITE(HWSTAM, 0xffffffff);
2835
2836 I915_WRITE(DEIMR, 0xffffffff);
2837 I915_WRITE(DEIER, 0x0);
2838 I915_WRITE(DEIIR, I915_READ(DEIIR));
Paulo Zanoni86642812013-04-12 17:57:57 -03002839 if (IS_GEN7(dev))
2840 I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002841
2842 I915_WRITE(GTIMR, 0xffffffff);
2843 I915_WRITE(GTIER, 0x0);
2844 I915_WRITE(GTIIR, I915_READ(GTIIR));
Keith Packard192aac1f2011-09-20 10:12:44 -07002845
Ben Widawskyab5c6082013-04-05 13:12:41 -07002846 if (HAS_PCH_NOP(dev))
2847 return;
2848
Keith Packard192aac1f2011-09-20 10:12:44 -07002849 I915_WRITE(SDEIMR, 0xffffffff);
2850 I915_WRITE(SDEIER, 0x0);
2851 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Paulo Zanoni86642812013-04-12 17:57:57 -03002852 if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2853 I915_WRITE(SERR_INT, I915_READ(SERR_INT));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002854}
2855
Chris Wilsonc2798b12012-04-22 21:13:57 +01002856static void i8xx_irq_preinstall(struct drm_device * dev)
2857{
2858 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2859 int pipe;
2860
2861 atomic_set(&dev_priv->irq_received, 0);
2862
2863 for_each_pipe(pipe)
2864 I915_WRITE(PIPESTAT(pipe), 0);
2865 I915_WRITE16(IMR, 0xffff);
2866 I915_WRITE16(IER, 0x0);
2867 POSTING_READ16(IER);
2868}
2869
2870static int i8xx_irq_postinstall(struct drm_device *dev)
2871{
2872 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2873
Chris Wilsonc2798b12012-04-22 21:13:57 +01002874 I915_WRITE16(EMR,
2875 ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2876
2877 /* Unmask the interrupts that we always want on. */
2878 dev_priv->irq_mask =
2879 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2880 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2881 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2882 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2883 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2884 I915_WRITE16(IMR, dev_priv->irq_mask);
2885
2886 I915_WRITE16(IER,
2887 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2888 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2889 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2890 I915_USER_INTERRUPT);
2891 POSTING_READ16(IER);
2892
2893 return 0;
2894}
2895
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002896/*
2897 * Returns true when a page flip has completed.
2898 */
2899static bool i8xx_handle_vblank(struct drm_device *dev,
2900 int pipe, u16 iir)
2901{
2902 drm_i915_private_t *dev_priv = dev->dev_private;
2903 u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
2904
2905 if (!drm_handle_vblank(dev, pipe))
2906 return false;
2907
2908 if ((iir & flip_pending) == 0)
2909 return false;
2910
2911 intel_prepare_page_flip(dev, pipe);
2912
2913 /* We detect FlipDone by looking for the change in PendingFlip from '1'
2914 * to '0' on the following vblank, i.e. IIR has the Pendingflip
2915 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
2916 * the flip is completed (no longer pending). Since this doesn't raise
2917 * an interrupt per se, we watch for the change at vblank.
2918 */
2919 if (I915_READ16(ISR) & flip_pending)
2920 return false;
2921
2922 intel_finish_page_flip(dev, pipe);
2923
2924 return true;
2925}
2926
Daniel Vetterff1f5252012-10-02 15:10:55 +02002927static irqreturn_t i8xx_irq_handler(int irq, void *arg)
Chris Wilsonc2798b12012-04-22 21:13:57 +01002928{
2929 struct drm_device *dev = (struct drm_device *) arg;
2930 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002931 u16 iir, new_iir;
2932 u32 pipe_stats[2];
2933 unsigned long irqflags;
2934 int irq_received;
2935 int pipe;
2936 u16 flip_mask =
2937 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2938 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2939
2940 atomic_inc(&dev_priv->irq_received);
2941
2942 iir = I915_READ16(IIR);
2943 if (iir == 0)
2944 return IRQ_NONE;
2945
2946 while (iir & ~flip_mask) {
2947 /* Can't rely on pipestat interrupt bit in iir as it might
2948 * have been cleared after the pipestat interrupt was received.
2949 * It doesn't set the bit in iir again, but it still produces
2950 * interrupts (for non-MSI).
2951 */
2952 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2953 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2954 i915_handle_error(dev, false);
2955
2956 for_each_pipe(pipe) {
2957 int reg = PIPESTAT(pipe);
2958 pipe_stats[pipe] = I915_READ(reg);
2959
2960 /*
2961 * Clear the PIPE*STAT regs before the IIR
2962 */
2963 if (pipe_stats[pipe] & 0x8000ffff) {
2964 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2965 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2966 pipe_name(pipe));
2967 I915_WRITE(reg, pipe_stats[pipe]);
2968 irq_received = 1;
2969 }
2970 }
2971 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2972
2973 I915_WRITE16(IIR, iir & ~flip_mask);
2974 new_iir = I915_READ16(IIR); /* Flush posted writes */
2975
Daniel Vetterd05c6172012-04-26 23:28:09 +02002976 i915_update_dri1_breadcrumb(dev);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002977
2978 if (iir & I915_USER_INTERRUPT)
2979 notify_ring(dev, &dev_priv->ring[RCS]);
2980
2981 if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002982 i8xx_handle_vblank(dev, 0, iir))
2983 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002984
2985 if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002986 i8xx_handle_vblank(dev, 1, iir))
2987 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002988
2989 iir = new_iir;
2990 }
2991
2992 return IRQ_HANDLED;
2993}
2994
2995static void i8xx_irq_uninstall(struct drm_device * dev)
2996{
2997 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2998 int pipe;
2999
Chris Wilsonc2798b12012-04-22 21:13:57 +01003000 for_each_pipe(pipe) {
3001 /* Clear enable bits; then clear status bits */
3002 I915_WRITE(PIPESTAT(pipe), 0);
3003 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3004 }
3005 I915_WRITE16(IMR, 0xffff);
3006 I915_WRITE16(IER, 0x0);
3007 I915_WRITE16(IIR, I915_READ16(IIR));
3008}
3009
Chris Wilsona266c7d2012-04-24 22:59:44 +01003010static void i915_irq_preinstall(struct drm_device * dev)
3011{
3012 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3013 int pipe;
3014
3015 atomic_set(&dev_priv->irq_received, 0);
3016
3017 if (I915_HAS_HOTPLUG(dev)) {
3018 I915_WRITE(PORT_HOTPLUG_EN, 0);
3019 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3020 }
3021
Chris Wilson00d98eb2012-04-24 22:59:48 +01003022 I915_WRITE16(HWSTAM, 0xeffe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003023 for_each_pipe(pipe)
3024 I915_WRITE(PIPESTAT(pipe), 0);
3025 I915_WRITE(IMR, 0xffffffff);
3026 I915_WRITE(IER, 0x0);
3027 POSTING_READ(IER);
3028}
3029
3030static int i915_irq_postinstall(struct drm_device *dev)
3031{
3032 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson38bde182012-04-24 22:59:50 +01003033 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003034
Chris Wilson38bde182012-04-24 22:59:50 +01003035 I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3036
3037 /* Unmask the interrupts that we always want on. */
3038 dev_priv->irq_mask =
3039 ~(I915_ASLE_INTERRUPT |
3040 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3041 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3042 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3043 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3044 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3045
3046 enable_mask =
3047 I915_ASLE_INTERRUPT |
3048 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3049 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3050 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3051 I915_USER_INTERRUPT;
3052
Chris Wilsona266c7d2012-04-24 22:59:44 +01003053 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetter20afbda2012-12-11 14:05:07 +01003054 I915_WRITE(PORT_HOTPLUG_EN, 0);
3055 POSTING_READ(PORT_HOTPLUG_EN);
3056
Chris Wilsona266c7d2012-04-24 22:59:44 +01003057 /* Enable in IER... */
3058 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3059 /* and unmask in IMR */
3060 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3061 }
3062
Chris Wilsona266c7d2012-04-24 22:59:44 +01003063 I915_WRITE(IMR, dev_priv->irq_mask);
3064 I915_WRITE(IER, enable_mask);
3065 POSTING_READ(IER);
3066
Jani Nikulaf49e38d2013-04-29 13:02:54 +03003067 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003068
3069 return 0;
3070}
3071
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003072/*
3073 * Returns true when a page flip has completed.
3074 */
3075static bool i915_handle_vblank(struct drm_device *dev,
3076 int plane, int pipe, u32 iir)
3077{
3078 drm_i915_private_t *dev_priv = dev->dev_private;
3079 u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
3080
3081 if (!drm_handle_vblank(dev, pipe))
3082 return false;
3083
3084 if ((iir & flip_pending) == 0)
3085 return false;
3086
3087 intel_prepare_page_flip(dev, plane);
3088
3089 /* We detect FlipDone by looking for the change in PendingFlip from '1'
3090 * to '0' on the following vblank, i.e. IIR has the Pendingflip
3091 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
3092 * the flip is completed (no longer pending). Since this doesn't raise
3093 * an interrupt per se, we watch for the change at vblank.
3094 */
3095 if (I915_READ(ISR) & flip_pending)
3096 return false;
3097
3098 intel_finish_page_flip(dev, pipe);
3099
3100 return true;
3101}
3102
Daniel Vetterff1f5252012-10-02 15:10:55 +02003103static irqreturn_t i915_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003104{
3105 struct drm_device *dev = (struct drm_device *) arg;
3106 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson8291ee92012-04-24 22:59:47 +01003107 u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01003108 unsigned long irqflags;
Chris Wilson38bde182012-04-24 22:59:50 +01003109 u32 flip_mask =
3110 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3111 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilson38bde182012-04-24 22:59:50 +01003112 int pipe, ret = IRQ_NONE;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003113
3114 atomic_inc(&dev_priv->irq_received);
3115
3116 iir = I915_READ(IIR);
Chris Wilson38bde182012-04-24 22:59:50 +01003117 do {
3118 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson8291ee92012-04-24 22:59:47 +01003119 bool blc_event = false;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003120
3121 /* Can't rely on pipestat interrupt bit in iir as it might
3122 * have been cleared after the pipestat interrupt was received.
3123 * It doesn't set the bit in iir again, but it still produces
3124 * interrupts (for non-MSI).
3125 */
3126 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3127 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3128 i915_handle_error(dev, false);
3129
3130 for_each_pipe(pipe) {
3131 int reg = PIPESTAT(pipe);
3132 pipe_stats[pipe] = I915_READ(reg);
3133
Chris Wilson38bde182012-04-24 22:59:50 +01003134 /* Clear the PIPE*STAT regs before the IIR */
Chris Wilsona266c7d2012-04-24 22:59:44 +01003135 if (pipe_stats[pipe] & 0x8000ffff) {
3136 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3137 DRM_DEBUG_DRIVER("pipe %c underrun\n",
3138 pipe_name(pipe));
3139 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilson38bde182012-04-24 22:59:50 +01003140 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003141 }
3142 }
3143 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3144
3145 if (!irq_received)
3146 break;
3147
Chris Wilsona266c7d2012-04-24 22:59:44 +01003148 /* Consume port. Then clear IIR or we'll miss events */
3149 if ((I915_HAS_HOTPLUG(dev)) &&
3150 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3151 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
Egbert Eichb543fb02013-04-16 13:36:54 +02003152 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003153
3154 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3155 hotplug_status);
Egbert Eichb543fb02013-04-16 13:36:54 +02003156 if (hotplug_trigger) {
Egbert Eichcd569ae2013-04-16 13:36:57 +02003157 if (hotplug_irq_storm_detect(dev, hotplug_trigger, hpd_status_i915))
3158 i915_hpd_irq_setup(dev);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003159 queue_work(dev_priv->wq,
3160 &dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +02003161 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01003162 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
Chris Wilson38bde182012-04-24 22:59:50 +01003163 POSTING_READ(PORT_HOTPLUG_STAT);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003164 }
3165
Chris Wilson38bde182012-04-24 22:59:50 +01003166 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003167 new_iir = I915_READ(IIR); /* Flush posted writes */
3168
Chris Wilsona266c7d2012-04-24 22:59:44 +01003169 if (iir & I915_USER_INTERRUPT)
3170 notify_ring(dev, &dev_priv->ring[RCS]);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003171
Chris Wilsona266c7d2012-04-24 22:59:44 +01003172 for_each_pipe(pipe) {
Chris Wilson38bde182012-04-24 22:59:50 +01003173 int plane = pipe;
3174 if (IS_MOBILE(dev))
3175 plane = !plane;
Ville Syrjälä5e2032d2013-02-19 15:16:38 +02003176
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003177 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
3178 i915_handle_vblank(dev, plane, pipe, iir))
3179 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003180
3181 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3182 blc_event = true;
3183 }
3184
Chris Wilsona266c7d2012-04-24 22:59:44 +01003185 if (blc_event || (iir & I915_ASLE_INTERRUPT))
3186 intel_opregion_asle_intr(dev);
3187
3188 /* With MSI, interrupts are only generated when iir
3189 * transitions from zero to nonzero. If another bit got
3190 * set while we were handling the existing iir bits, then
3191 * we would never get another interrupt.
3192 *
3193 * This is fine on non-MSI as well, as if we hit this path
3194 * we avoid exiting the interrupt handler only to generate
3195 * another one.
3196 *
3197 * Note that for MSI this could cause a stray interrupt report
3198 * if an interrupt landed in the time between writing IIR and
3199 * the posting read. This should be rare enough to never
3200 * trigger the 99% of 100,000 interrupts test for disabling
3201 * stray interrupts.
3202 */
Chris Wilson38bde182012-04-24 22:59:50 +01003203 ret = IRQ_HANDLED;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003204 iir = new_iir;
Chris Wilson38bde182012-04-24 22:59:50 +01003205 } while (iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003206
Daniel Vetterd05c6172012-04-26 23:28:09 +02003207 i915_update_dri1_breadcrumb(dev);
Chris Wilson8291ee92012-04-24 22:59:47 +01003208
Chris Wilsona266c7d2012-04-24 22:59:44 +01003209 return ret;
3210}
3211
3212static void i915_irq_uninstall(struct drm_device * dev)
3213{
3214 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3215 int pipe;
3216
Egbert Eichac4c16c2013-04-16 13:36:58 +02003217 del_timer_sync(&dev_priv->hotplug_reenable_timer);
3218
Chris Wilsona266c7d2012-04-24 22:59:44 +01003219 if (I915_HAS_HOTPLUG(dev)) {
3220 I915_WRITE(PORT_HOTPLUG_EN, 0);
3221 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3222 }
3223
Chris Wilson00d98eb2012-04-24 22:59:48 +01003224 I915_WRITE16(HWSTAM, 0xffff);
Chris Wilson55b39752012-04-24 22:59:49 +01003225 for_each_pipe(pipe) {
3226 /* Clear enable bits; then clear status bits */
Chris Wilsona266c7d2012-04-24 22:59:44 +01003227 I915_WRITE(PIPESTAT(pipe), 0);
Chris Wilson55b39752012-04-24 22:59:49 +01003228 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3229 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01003230 I915_WRITE(IMR, 0xffffffff);
3231 I915_WRITE(IER, 0x0);
3232
Chris Wilsona266c7d2012-04-24 22:59:44 +01003233 I915_WRITE(IIR, I915_READ(IIR));
3234}
3235
3236static void i965_irq_preinstall(struct drm_device * dev)
3237{
3238 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3239 int pipe;
3240
3241 atomic_set(&dev_priv->irq_received, 0);
3242
Chris Wilsonadca4732012-05-11 18:01:31 +01003243 I915_WRITE(PORT_HOTPLUG_EN, 0);
3244 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01003245
3246 I915_WRITE(HWSTAM, 0xeffe);
3247 for_each_pipe(pipe)
3248 I915_WRITE(PIPESTAT(pipe), 0);
3249 I915_WRITE(IMR, 0xffffffff);
3250 I915_WRITE(IER, 0x0);
3251 POSTING_READ(IER);
3252}
3253
3254static int i965_irq_postinstall(struct drm_device *dev)
3255{
3256 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonbbba0a92012-04-24 22:59:51 +01003257 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003258 u32 error_mask;
3259
Chris Wilsona266c7d2012-04-24 22:59:44 +01003260 /* Unmask the interrupts that we always want on. */
Chris Wilsonbbba0a92012-04-24 22:59:51 +01003261 dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
Chris Wilsonadca4732012-05-11 18:01:31 +01003262 I915_DISPLAY_PORT_INTERRUPT |
Chris Wilsonbbba0a92012-04-24 22:59:51 +01003263 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3264 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3265 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3266 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3267 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3268
3269 enable_mask = ~dev_priv->irq_mask;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003270 enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3271 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
Chris Wilsonbbba0a92012-04-24 22:59:51 +01003272 enable_mask |= I915_USER_INTERRUPT;
3273
3274 if (IS_G4X(dev))
3275 enable_mask |= I915_BSD_USER_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003276
Daniel Vetter515ac2b2012-12-01 13:53:44 +01003277 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003278
Chris Wilsona266c7d2012-04-24 22:59:44 +01003279 /*
3280 * Enable some error detection, note the instruction error mask
3281 * bit is reserved, so we leave it masked.
3282 */
3283 if (IS_G4X(dev)) {
3284 error_mask = ~(GM45_ERROR_PAGE_TABLE |
3285 GM45_ERROR_MEM_PRIV |
3286 GM45_ERROR_CP_PRIV |
3287 I915_ERROR_MEMORY_REFRESH);
3288 } else {
3289 error_mask = ~(I915_ERROR_PAGE_TABLE |
3290 I915_ERROR_MEMORY_REFRESH);
3291 }
3292 I915_WRITE(EMR, error_mask);
3293
3294 I915_WRITE(IMR, dev_priv->irq_mask);
3295 I915_WRITE(IER, enable_mask);
3296 POSTING_READ(IER);
3297
Daniel Vetter20afbda2012-12-11 14:05:07 +01003298 I915_WRITE(PORT_HOTPLUG_EN, 0);
3299 POSTING_READ(PORT_HOTPLUG_EN);
3300
Jani Nikulaf49e38d2013-04-29 13:02:54 +03003301 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003302
3303 return 0;
3304}
3305
Egbert Eichbac56d52013-02-25 12:06:51 -05003306static void i915_hpd_irq_setup(struct drm_device *dev)
Daniel Vetter20afbda2012-12-11 14:05:07 +01003307{
3308 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Egbert Eiche5868a32013-02-28 04:17:12 -05003309 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +02003310 struct intel_encoder *intel_encoder;
Daniel Vetter20afbda2012-12-11 14:05:07 +01003311 u32 hotplug_en;
3312
Egbert Eichbac56d52013-02-25 12:06:51 -05003313 if (I915_HAS_HOTPLUG(dev)) {
3314 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3315 hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3316 /* Note HDMI and DP share hotplug bits */
Egbert Eiche5868a32013-02-28 04:17:12 -05003317 /* enable bits are the same for all generations */
Egbert Eichcd569ae2013-04-16 13:36:57 +02003318 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3319 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3320 hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
Egbert Eichbac56d52013-02-25 12:06:51 -05003321 /* Programming the CRT detection parameters tends
3322 to generate a spurious hotplug event about three
3323 seconds later. So just do it once.
3324 */
3325 if (IS_G4X(dev))
3326 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
Daniel Vetter85fc95b2013-03-27 15:47:11 +01003327 hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
Egbert Eichbac56d52013-02-25 12:06:51 -05003328 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003329
Egbert Eichbac56d52013-02-25 12:06:51 -05003330 /* Ignore TV since it's buggy */
3331 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3332 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01003333}
3334
Daniel Vetterff1f5252012-10-02 15:10:55 +02003335static irqreturn_t i965_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003336{
3337 struct drm_device *dev = (struct drm_device *) arg;
3338 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003339 u32 iir, new_iir;
3340 u32 pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01003341 unsigned long irqflags;
3342 int irq_received;
3343 int ret = IRQ_NONE, pipe;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003344 u32 flip_mask =
3345 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3346 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003347
3348 atomic_inc(&dev_priv->irq_received);
3349
3350 iir = I915_READ(IIR);
3351
Chris Wilsona266c7d2012-04-24 22:59:44 +01003352 for (;;) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01003353 bool blc_event = false;
3354
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003355 irq_received = (iir & ~flip_mask) != 0;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003356
3357 /* Can't rely on pipestat interrupt bit in iir as it might
3358 * have been cleared after the pipestat interrupt was received.
3359 * It doesn't set the bit in iir again, but it still produces
3360 * interrupts (for non-MSI).
3361 */
3362 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3363 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3364 i915_handle_error(dev, false);
3365
3366 for_each_pipe(pipe) {
3367 int reg = PIPESTAT(pipe);
3368 pipe_stats[pipe] = I915_READ(reg);
3369
3370 /*
3371 * Clear the PIPE*STAT regs before the IIR
3372 */
3373 if (pipe_stats[pipe] & 0x8000ffff) {
3374 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3375 DRM_DEBUG_DRIVER("pipe %c underrun\n",
3376 pipe_name(pipe));
3377 I915_WRITE(reg, pipe_stats[pipe]);
3378 irq_received = 1;
3379 }
3380 }
3381 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3382
3383 if (!irq_received)
3384 break;
3385
3386 ret = IRQ_HANDLED;
3387
3388 /* Consume port. Then clear IIR or we'll miss events */
Chris Wilsonadca4732012-05-11 18:01:31 +01003389 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003390 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
Egbert Eichb543fb02013-04-16 13:36:54 +02003391 u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3392 HOTPLUG_INT_STATUS_G4X :
3393 HOTPLUG_INT_STATUS_I965);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003394
3395 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3396 hotplug_status);
Egbert Eichb543fb02013-04-16 13:36:54 +02003397 if (hotplug_trigger) {
Egbert Eichcd569ae2013-04-16 13:36:57 +02003398 if (hotplug_irq_storm_detect(dev, hotplug_trigger,
3399 IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i965))
3400 i915_hpd_irq_setup(dev);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003401 queue_work(dev_priv->wq,
3402 &dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +02003403 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01003404 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3405 I915_READ(PORT_HOTPLUG_STAT);
3406 }
3407
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003408 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003409 new_iir = I915_READ(IIR); /* Flush posted writes */
3410
Chris Wilsona266c7d2012-04-24 22:59:44 +01003411 if (iir & I915_USER_INTERRUPT)
3412 notify_ring(dev, &dev_priv->ring[RCS]);
3413 if (iir & I915_BSD_USER_INTERRUPT)
3414 notify_ring(dev, &dev_priv->ring[VCS]);
3415
Chris Wilsona266c7d2012-04-24 22:59:44 +01003416 for_each_pipe(pipe) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01003417 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003418 i915_handle_vblank(dev, pipe, pipe, iir))
3419 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003420
3421 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3422 blc_event = true;
3423 }
3424
3425
3426 if (blc_event || (iir & I915_ASLE_INTERRUPT))
3427 intel_opregion_asle_intr(dev);
3428
Daniel Vetter515ac2b2012-12-01 13:53:44 +01003429 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3430 gmbus_irq_handler(dev);
3431
Chris Wilsona266c7d2012-04-24 22:59:44 +01003432 /* With MSI, interrupts are only generated when iir
3433 * transitions from zero to nonzero. If another bit got
3434 * set while we were handling the existing iir bits, then
3435 * we would never get another interrupt.
3436 *
3437 * This is fine on non-MSI as well, as if we hit this path
3438 * we avoid exiting the interrupt handler only to generate
3439 * another one.
3440 *
3441 * Note that for MSI this could cause a stray interrupt report
3442 * if an interrupt landed in the time between writing IIR and
3443 * the posting read. This should be rare enough to never
3444 * trigger the 99% of 100,000 interrupts test for disabling
3445 * stray interrupts.
3446 */
3447 iir = new_iir;
3448 }
3449
Daniel Vetterd05c6172012-04-26 23:28:09 +02003450 i915_update_dri1_breadcrumb(dev);
Chris Wilson2c8ba292012-04-24 22:59:46 +01003451
Chris Wilsona266c7d2012-04-24 22:59:44 +01003452 return ret;
3453}
3454
3455static void i965_irq_uninstall(struct drm_device * dev)
3456{
3457 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3458 int pipe;
3459
3460 if (!dev_priv)
3461 return;
3462
Egbert Eichac4c16c2013-04-16 13:36:58 +02003463 del_timer_sync(&dev_priv->hotplug_reenable_timer);
3464
Chris Wilsonadca4732012-05-11 18:01:31 +01003465 I915_WRITE(PORT_HOTPLUG_EN, 0);
3466 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01003467
3468 I915_WRITE(HWSTAM, 0xffffffff);
3469 for_each_pipe(pipe)
3470 I915_WRITE(PIPESTAT(pipe), 0);
3471 I915_WRITE(IMR, 0xffffffff);
3472 I915_WRITE(IER, 0x0);
3473
3474 for_each_pipe(pipe)
3475 I915_WRITE(PIPESTAT(pipe),
3476 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3477 I915_WRITE(IIR, I915_READ(IIR));
3478}
3479
Egbert Eichac4c16c2013-04-16 13:36:58 +02003480static void i915_reenable_hotplug_timer_func(unsigned long data)
3481{
3482 drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3483 struct drm_device *dev = dev_priv->dev;
3484 struct drm_mode_config *mode_config = &dev->mode_config;
3485 unsigned long irqflags;
3486 int i;
3487
3488 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3489 for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3490 struct drm_connector *connector;
3491
3492 if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3493 continue;
3494
3495 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3496
3497 list_for_each_entry(connector, &mode_config->connector_list, head) {
3498 struct intel_connector *intel_connector = to_intel_connector(connector);
3499
3500 if (intel_connector->encoder->hpd_pin == i) {
3501 if (connector->polled != intel_connector->polled)
3502 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3503 drm_get_connector_name(connector));
3504 connector->polled = intel_connector->polled;
3505 if (!connector->polled)
3506 connector->polled = DRM_CONNECTOR_POLL_HPD;
3507 }
3508 }
3509 }
3510 if (dev_priv->display.hpd_irq_setup)
3511 dev_priv->display.hpd_irq_setup(dev);
3512 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3513}
3514
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003515void intel_irq_init(struct drm_device *dev)
3516{
Chris Wilson8b2e3262012-04-24 22:59:41 +01003517 struct drm_i915_private *dev_priv = dev->dev_private;
3518
3519 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Daniel Vetter99584db2012-11-14 17:14:04 +01003520 INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003521 INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01003522 INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
Chris Wilson8b2e3262012-04-24 22:59:41 +01003523
Daniel Vetter99584db2012-11-14 17:14:04 +01003524 setup_timer(&dev_priv->gpu_error.hangcheck_timer,
3525 i915_hangcheck_elapsed,
Daniel Vetter61bac782012-12-01 21:03:21 +01003526 (unsigned long) dev);
Egbert Eichac4c16c2013-04-16 13:36:58 +02003527 setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3528 (unsigned long) dev_priv);
Daniel Vetter61bac782012-12-01 21:03:21 +01003529
Tomas Janousek97a19a22012-12-08 13:48:13 +01003530 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01003531
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003532 dev->driver->get_vblank_counter = i915_get_vblank_counter;
3533 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eugeni Dodonov7d4e1462012-05-09 15:37:09 -03003534 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003535 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3536 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3537 }
3538
Keith Packardc3613de2011-08-12 17:05:54 -07003539 if (drm_core_check_feature(dev, DRIVER_MODESET))
3540 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3541 else
3542 dev->driver->get_vblank_timestamp = NULL;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003543 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3544
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003545 if (IS_VALLEYVIEW(dev)) {
3546 dev->driver->irq_handler = valleyview_irq_handler;
3547 dev->driver->irq_preinstall = valleyview_irq_preinstall;
3548 dev->driver->irq_postinstall = valleyview_irq_postinstall;
3549 dev->driver->irq_uninstall = valleyview_irq_uninstall;
3550 dev->driver->enable_vblank = valleyview_enable_vblank;
3551 dev->driver->disable_vblank = valleyview_disable_vblank;
Egbert Eichfa00abe2013-02-25 12:06:48 -05003552 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Daniel Vetter4a06e202012-12-01 13:53:40 +01003553 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Ben Widawsky7d991632013-05-28 19:22:25 -07003554 /* Share uninstall handlers with ILK/SNB */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003555 dev->driver->irq_handler = ivybridge_irq_handler;
Ben Widawsky7d991632013-05-28 19:22:25 -07003556 dev->driver->irq_preinstall = ivybridge_irq_preinstall;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003557 dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3558 dev->driver->irq_uninstall = ironlake_irq_uninstall;
3559 dev->driver->enable_vblank = ivybridge_enable_vblank;
3560 dev->driver->disable_vblank = ivybridge_disable_vblank;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003561 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003562 } else if (HAS_PCH_SPLIT(dev)) {
3563 dev->driver->irq_handler = ironlake_irq_handler;
3564 dev->driver->irq_preinstall = ironlake_irq_preinstall;
3565 dev->driver->irq_postinstall = ironlake_irq_postinstall;
3566 dev->driver->irq_uninstall = ironlake_irq_uninstall;
3567 dev->driver->enable_vblank = ironlake_enable_vblank;
3568 dev->driver->disable_vblank = ironlake_disable_vblank;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003569 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003570 } else {
Chris Wilsonc2798b12012-04-22 21:13:57 +01003571 if (INTEL_INFO(dev)->gen == 2) {
3572 dev->driver->irq_preinstall = i8xx_irq_preinstall;
3573 dev->driver->irq_postinstall = i8xx_irq_postinstall;
3574 dev->driver->irq_handler = i8xx_irq_handler;
3575 dev->driver->irq_uninstall = i8xx_irq_uninstall;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003576 } else if (INTEL_INFO(dev)->gen == 3) {
3577 dev->driver->irq_preinstall = i915_irq_preinstall;
3578 dev->driver->irq_postinstall = i915_irq_postinstall;
3579 dev->driver->irq_uninstall = i915_irq_uninstall;
3580 dev->driver->irq_handler = i915_irq_handler;
Daniel Vetter20afbda2012-12-11 14:05:07 +01003581 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003582 } else {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003583 dev->driver->irq_preinstall = i965_irq_preinstall;
3584 dev->driver->irq_postinstall = i965_irq_postinstall;
3585 dev->driver->irq_uninstall = i965_irq_uninstall;
3586 dev->driver->irq_handler = i965_irq_handler;
Egbert Eichbac56d52013-02-25 12:06:51 -05003587 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003588 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003589 dev->driver->enable_vblank = i915_enable_vblank;
3590 dev->driver->disable_vblank = i915_disable_vblank;
3591 }
3592}
Daniel Vetter20afbda2012-12-11 14:05:07 +01003593
3594void intel_hpd_init(struct drm_device *dev)
3595{
3596 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eich821450c2013-04-16 13:36:55 +02003597 struct drm_mode_config *mode_config = &dev->mode_config;
3598 struct drm_connector *connector;
3599 int i;
Daniel Vetter20afbda2012-12-11 14:05:07 +01003600
Egbert Eich821450c2013-04-16 13:36:55 +02003601 for (i = 1; i < HPD_NUM_PINS; i++) {
3602 dev_priv->hpd_stats[i].hpd_cnt = 0;
3603 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3604 }
3605 list_for_each_entry(connector, &mode_config->connector_list, head) {
3606 struct intel_connector *intel_connector = to_intel_connector(connector);
3607 connector->polled = intel_connector->polled;
3608 if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3609 connector->polled = DRM_CONNECTOR_POLL_HPD;
3610 }
Daniel Vetter20afbda2012-12-11 14:05:07 +01003611 if (dev_priv->display.hpd_irq_setup)
3612 dev_priv->display.hpd_irq_setup(dev);
3613}