blob: dfa5c68c276340a866f1e82d1fa4f6d9310d5a66 [file] [log] [blame]
Dave Airlie0d6aa602006-01-02 20:14:23 +11001/* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Joe Perchesa70491c2012-03-18 13:00:11 -070029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Jesse Barnes63eeaf32009-06-18 16:56:52 -070031#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/drmP.h>
34#include <drm/i915_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010036#include "i915_trace.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Egbert Eiche5868a32013-02-28 04:17:12 -050039static const u32 hpd_ibx[] = {
40 [HPD_CRT] = SDE_CRT_HOTPLUG,
41 [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
42 [HPD_PORT_B] = SDE_PORTB_HOTPLUG,
43 [HPD_PORT_C] = SDE_PORTC_HOTPLUG,
44 [HPD_PORT_D] = SDE_PORTD_HOTPLUG
45};
46
47static const u32 hpd_cpt[] = {
48 [HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
Daniel Vetter73c352a2013-03-26 22:38:43 +010049 [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
Egbert Eiche5868a32013-02-28 04:17:12 -050050 [HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
51 [HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
52 [HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
53};
54
55static const u32 hpd_mask_i915[] = {
56 [HPD_CRT] = CRT_HOTPLUG_INT_EN,
57 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
58 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
59 [HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
60 [HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
61 [HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
62};
63
64static const u32 hpd_status_gen4[] = {
65 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
66 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
67 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
68 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
69 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
70 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
71};
72
Egbert Eiche5868a32013-02-28 04:17:12 -050073static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
74 [HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75 [HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
76 [HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
77 [HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78 [HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79 [HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80};
81
Zhenyu Wang036a4a72009-06-08 14:40:19 +080082/* For display hotplug interrupt */
Chris Wilson995b6762010-08-20 13:23:26 +010083static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -050084ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +080085{
Daniel Vetter4bc9d432013-06-27 13:44:58 +020086 assert_spin_locked(&dev_priv->irq_lock);
87
Paulo Zanonic67a4702013-08-19 13:18:09 -030088 if (dev_priv->pc8.irqs_disabled) {
89 WARN(1, "IRQs disabled\n");
90 dev_priv->pc8.regsave.deimr &= ~mask;
91 return;
92 }
93
Chris Wilson1ec14ad2010-12-04 11:30:53 +000094 if ((dev_priv->irq_mask & mask) != 0) {
95 dev_priv->irq_mask &= ~mask;
96 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +000097 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +080098 }
99}
100
Paulo Zanoni0ff98002013-02-22 17:05:31 -0300101static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500102ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800103{
Daniel Vetter4bc9d432013-06-27 13:44:58 +0200104 assert_spin_locked(&dev_priv->irq_lock);
105
Paulo Zanonic67a4702013-08-19 13:18:09 -0300106 if (dev_priv->pc8.irqs_disabled) {
107 WARN(1, "IRQs disabled\n");
108 dev_priv->pc8.regsave.deimr |= mask;
109 return;
110 }
111
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000112 if ((dev_priv->irq_mask & mask) != mask) {
113 dev_priv->irq_mask |= mask;
114 I915_WRITE(DEIMR, dev_priv->irq_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +0000115 POSTING_READ(DEIMR);
Zhenyu Wang036a4a72009-06-08 14:40:19 +0800116 }
117}
118
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300119/**
120 * ilk_update_gt_irq - update GTIMR
121 * @dev_priv: driver private
122 * @interrupt_mask: mask of interrupt bits to update
123 * @enabled_irq_mask: mask of interrupt bits to enable
124 */
125static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
126 uint32_t interrupt_mask,
127 uint32_t enabled_irq_mask)
128{
129 assert_spin_locked(&dev_priv->irq_lock);
130
Paulo Zanonic67a4702013-08-19 13:18:09 -0300131 if (dev_priv->pc8.irqs_disabled) {
132 WARN(1, "IRQs disabled\n");
133 dev_priv->pc8.regsave.gtimr &= ~interrupt_mask;
134 dev_priv->pc8.regsave.gtimr |= (~enabled_irq_mask &
135 interrupt_mask);
136 return;
137 }
138
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300139 dev_priv->gt_irq_mask &= ~interrupt_mask;
140 dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
141 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
142 POSTING_READ(GTIMR);
143}
144
145void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
146{
147 ilk_update_gt_irq(dev_priv, mask, mask);
148}
149
150void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
151{
152 ilk_update_gt_irq(dev_priv, mask, 0);
153}
154
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300155/**
156 * snb_update_pm_irq - update GEN6_PMIMR
157 * @dev_priv: driver private
158 * @interrupt_mask: mask of interrupt bits to update
159 * @enabled_irq_mask: mask of interrupt bits to enable
160 */
161static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
162 uint32_t interrupt_mask,
163 uint32_t enabled_irq_mask)
164{
Paulo Zanoni605cd252013-08-06 18:57:15 -0300165 uint32_t new_val;
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300166
167 assert_spin_locked(&dev_priv->irq_lock);
168
Paulo Zanonic67a4702013-08-19 13:18:09 -0300169 if (dev_priv->pc8.irqs_disabled) {
170 WARN(1, "IRQs disabled\n");
171 dev_priv->pc8.regsave.gen6_pmimr &= ~interrupt_mask;
172 dev_priv->pc8.regsave.gen6_pmimr |= (~enabled_irq_mask &
173 interrupt_mask);
174 return;
175 }
176
Paulo Zanoni605cd252013-08-06 18:57:15 -0300177 new_val = dev_priv->pm_irq_mask;
Paulo Zanonif52ecbc2013-08-06 18:57:14 -0300178 new_val &= ~interrupt_mask;
179 new_val |= (~enabled_irq_mask & interrupt_mask);
180
Paulo Zanoni605cd252013-08-06 18:57:15 -0300181 if (new_val != dev_priv->pm_irq_mask) {
182 dev_priv->pm_irq_mask = new_val;
183 I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
Paulo Zanonif52ecbc2013-08-06 18:57:14 -0300184 POSTING_READ(GEN6_PMIMR);
185 }
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300186}
187
188void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
189{
190 snb_update_pm_irq(dev_priv, mask, mask);
191}
192
193void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
194{
195 snb_update_pm_irq(dev_priv, mask, 0);
196}
197
Paulo Zanoni86642812013-04-12 17:57:57 -0300198static bool ivb_can_enable_err_int(struct drm_device *dev)
199{
200 struct drm_i915_private *dev_priv = dev->dev_private;
201 struct intel_crtc *crtc;
202 enum pipe pipe;
203
Daniel Vetter4bc9d432013-06-27 13:44:58 +0200204 assert_spin_locked(&dev_priv->irq_lock);
205
Paulo Zanoni86642812013-04-12 17:57:57 -0300206 for_each_pipe(pipe) {
207 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
208
209 if (crtc->cpu_fifo_underrun_disabled)
210 return false;
211 }
212
213 return true;
214}
215
216static bool cpt_can_enable_serr_int(struct drm_device *dev)
217{
218 struct drm_i915_private *dev_priv = dev->dev_private;
219 enum pipe pipe;
220 struct intel_crtc *crtc;
221
Daniel Vetterfee884e2013-07-04 23:35:21 +0200222 assert_spin_locked(&dev_priv->irq_lock);
223
Paulo Zanoni86642812013-04-12 17:57:57 -0300224 for_each_pipe(pipe) {
225 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
226
227 if (crtc->pch_fifo_underrun_disabled)
228 return false;
229 }
230
231 return true;
232}
233
234static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
235 enum pipe pipe, bool enable)
236{
237 struct drm_i915_private *dev_priv = dev->dev_private;
238 uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
239 DE_PIPEB_FIFO_UNDERRUN;
240
241 if (enable)
242 ironlake_enable_display_irq(dev_priv, bit);
243 else
244 ironlake_disable_display_irq(dev_priv, bit);
245}
246
247static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
Daniel Vetter7336df62013-07-09 22:59:16 +0200248 enum pipe pipe, bool enable)
Paulo Zanoni86642812013-04-12 17:57:57 -0300249{
250 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni86642812013-04-12 17:57:57 -0300251 if (enable) {
Daniel Vetter7336df62013-07-09 22:59:16 +0200252 I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
253
Paulo Zanoni86642812013-04-12 17:57:57 -0300254 if (!ivb_can_enable_err_int(dev))
255 return;
256
Paulo Zanoni86642812013-04-12 17:57:57 -0300257 ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
258 } else {
Daniel Vetter7336df62013-07-09 22:59:16 +0200259 bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
260
261 /* Change the state _after_ we've read out the current one. */
Paulo Zanoni86642812013-04-12 17:57:57 -0300262 ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
Daniel Vetter7336df62013-07-09 22:59:16 +0200263
264 if (!was_enabled &&
265 (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
266 DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
267 pipe_name(pipe));
268 }
Paulo Zanoni86642812013-04-12 17:57:57 -0300269 }
270}
271
Daniel Vetterfee884e2013-07-04 23:35:21 +0200272/**
273 * ibx_display_interrupt_update - update SDEIMR
274 * @dev_priv: driver private
275 * @interrupt_mask: mask of interrupt bits to update
276 * @enabled_irq_mask: mask of interrupt bits to enable
277 */
278static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
279 uint32_t interrupt_mask,
280 uint32_t enabled_irq_mask)
281{
282 uint32_t sdeimr = I915_READ(SDEIMR);
283 sdeimr &= ~interrupt_mask;
284 sdeimr |= (~enabled_irq_mask & interrupt_mask);
285
286 assert_spin_locked(&dev_priv->irq_lock);
287
Paulo Zanonic67a4702013-08-19 13:18:09 -0300288 if (dev_priv->pc8.irqs_disabled &&
289 (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
290 WARN(1, "IRQs disabled\n");
291 dev_priv->pc8.regsave.sdeimr &= ~interrupt_mask;
292 dev_priv->pc8.regsave.sdeimr |= (~enabled_irq_mask &
293 interrupt_mask);
294 return;
295 }
296
Daniel Vetterfee884e2013-07-04 23:35:21 +0200297 I915_WRITE(SDEIMR, sdeimr);
298 POSTING_READ(SDEIMR);
299}
300#define ibx_enable_display_interrupt(dev_priv, bits) \
301 ibx_display_interrupt_update((dev_priv), (bits), (bits))
302#define ibx_disable_display_interrupt(dev_priv, bits) \
303 ibx_display_interrupt_update((dev_priv), (bits), 0)
304
Daniel Vetterde280752013-07-04 23:35:24 +0200305static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
306 enum transcoder pch_transcoder,
Paulo Zanoni86642812013-04-12 17:57:57 -0300307 bool enable)
308{
Paulo Zanoni86642812013-04-12 17:57:57 -0300309 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterde280752013-07-04 23:35:24 +0200310 uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
311 SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
Paulo Zanoni86642812013-04-12 17:57:57 -0300312
313 if (enable)
Daniel Vetterfee884e2013-07-04 23:35:21 +0200314 ibx_enable_display_interrupt(dev_priv, bit);
Paulo Zanoni86642812013-04-12 17:57:57 -0300315 else
Daniel Vetterfee884e2013-07-04 23:35:21 +0200316 ibx_disable_display_interrupt(dev_priv, bit);
Paulo Zanoni86642812013-04-12 17:57:57 -0300317}
318
319static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
320 enum transcoder pch_transcoder,
321 bool enable)
322{
323 struct drm_i915_private *dev_priv = dev->dev_private;
324
325 if (enable) {
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200326 I915_WRITE(SERR_INT,
327 SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
328
Paulo Zanoni86642812013-04-12 17:57:57 -0300329 if (!cpt_can_enable_serr_int(dev))
330 return;
331
Daniel Vetterfee884e2013-07-04 23:35:21 +0200332 ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
Paulo Zanoni86642812013-04-12 17:57:57 -0300333 } else {
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200334 uint32_t tmp = I915_READ(SERR_INT);
335 bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
336
337 /* Change the state _after_ we've read out the current one. */
Daniel Vetterfee884e2013-07-04 23:35:21 +0200338 ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
Daniel Vetter1dd246f2013-07-10 08:30:23 +0200339
340 if (!was_enabled &&
341 (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
342 DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
343 transcoder_name(pch_transcoder));
344 }
Paulo Zanoni86642812013-04-12 17:57:57 -0300345 }
Paulo Zanoni86642812013-04-12 17:57:57 -0300346}
347
348/**
349 * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
350 * @dev: drm device
351 * @pipe: pipe
352 * @enable: true if we want to report FIFO underrun errors, false otherwise
353 *
354 * This function makes us disable or enable CPU fifo underruns for a specific
355 * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
356 * reporting for one pipe may also disable all the other CPU error interruts for
357 * the other pipes, due to the fact that there's just one interrupt mask/enable
358 * bit for all the pipes.
359 *
360 * Returns the previous state of underrun reporting.
361 */
362bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
363 enum pipe pipe, bool enable)
364{
365 struct drm_i915_private *dev_priv = dev->dev_private;
366 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
368 unsigned long flags;
369 bool ret;
370
371 spin_lock_irqsave(&dev_priv->irq_lock, flags);
372
373 ret = !intel_crtc->cpu_fifo_underrun_disabled;
374
375 if (enable == ret)
376 goto done;
377
378 intel_crtc->cpu_fifo_underrun_disabled = !enable;
379
380 if (IS_GEN5(dev) || IS_GEN6(dev))
381 ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
382 else if (IS_GEN7(dev))
Daniel Vetter7336df62013-07-09 22:59:16 +0200383 ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
Paulo Zanoni86642812013-04-12 17:57:57 -0300384
385done:
386 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
387 return ret;
388}
389
390/**
391 * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
392 * @dev: drm device
393 * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
394 * @enable: true if we want to report FIFO underrun errors, false otherwise
395 *
396 * This function makes us disable or enable PCH fifo underruns for a specific
397 * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
398 * underrun reporting for one transcoder may also disable all the other PCH
399 * error interruts for the other transcoders, due to the fact that there's just
400 * one interrupt mask/enable bit for all the transcoders.
401 *
402 * Returns the previous state of underrun reporting.
403 */
404bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
405 enum transcoder pch_transcoder,
406 bool enable)
407{
408 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterde280752013-07-04 23:35:24 +0200409 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni86642812013-04-12 17:57:57 -0300411 unsigned long flags;
412 bool ret;
413
Daniel Vetterde280752013-07-04 23:35:24 +0200414 /*
415 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
416 * has only one pch transcoder A that all pipes can use. To avoid racy
417 * pch transcoder -> pipe lookups from interrupt code simply store the
418 * underrun statistics in crtc A. Since we never expose this anywhere
419 * nor use it outside of the fifo underrun code here using the "wrong"
420 * crtc on LPT won't cause issues.
421 */
Paulo Zanoni86642812013-04-12 17:57:57 -0300422
423 spin_lock_irqsave(&dev_priv->irq_lock, flags);
424
425 ret = !intel_crtc->pch_fifo_underrun_disabled;
426
427 if (enable == ret)
428 goto done;
429
430 intel_crtc->pch_fifo_underrun_disabled = !enable;
431
432 if (HAS_PCH_IBX(dev))
Daniel Vetterde280752013-07-04 23:35:24 +0200433 ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
Paulo Zanoni86642812013-04-12 17:57:57 -0300434 else
435 cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
436
437done:
438 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
439 return ret;
440}
441
442
Keith Packard7c463582008-11-04 02:03:27 -0800443void
444i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
445{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200446 u32 reg = PIPESTAT(pipe);
447 u32 pipestat = I915_READ(reg) & 0x7fff0000;
Keith Packard7c463582008-11-04 02:03:27 -0800448
Daniel Vetterb79480b2013-06-27 17:52:10 +0200449 assert_spin_locked(&dev_priv->irq_lock);
450
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200451 if ((pipestat & mask) == mask)
452 return;
453
454 /* Enable the interrupt, clear any pending status */
455 pipestat |= mask | (mask >> 16);
456 I915_WRITE(reg, pipestat);
457 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800458}
459
460void
461i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
462{
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200463 u32 reg = PIPESTAT(pipe);
464 u32 pipestat = I915_READ(reg) & 0x7fff0000;
Keith Packard7c463582008-11-04 02:03:27 -0800465
Daniel Vetterb79480b2013-06-27 17:52:10 +0200466 assert_spin_locked(&dev_priv->irq_lock);
467
Ville Syrjälä46c06a32013-02-20 21:16:18 +0200468 if ((pipestat & mask) == 0)
469 return;
470
471 pipestat &= ~mask;
472 I915_WRITE(reg, pipestat);
473 POSTING_READ(reg);
Keith Packard7c463582008-11-04 02:03:27 -0800474}
475
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000476/**
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300477 * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
Zhao Yakui01c66882009-10-28 05:10:00 +0000478 */
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300479static void i915_enable_asle_pipestat(struct drm_device *dev)
Zhao Yakui01c66882009-10-28 05:10:00 +0000480{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000481 drm_i915_private_t *dev_priv = dev->dev_private;
482 unsigned long irqflags;
483
Jani Nikulaf49e38d2013-04-29 13:02:54 +0300484 if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
485 return;
486
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000487 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000488
Jani Nikulaf8987802013-04-29 13:02:53 +0300489 i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
490 if (INTEL_INFO(dev)->gen >= 4)
491 i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000492
493 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Zhao Yakui01c66882009-10-28 05:10:00 +0000494}
495
496/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700497 * i915_pipe_enabled - check if a pipe is enabled
498 * @dev: DRM device
499 * @pipe: pipe to check
500 *
501 * Reading certain registers when the pipe is disabled can hang the chip.
502 * Use this routine to make sure the PLL is running and the pipe is active
503 * before reading such registers if unsure.
504 */
505static int
506i915_pipe_enabled(struct drm_device *dev, int pipe)
507{
508 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200509
Daniel Vettera01025a2013-05-22 00:50:23 +0200510 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
511 /* Locking is horribly broken here, but whatever. */
512 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni71f8ba62013-05-03 12:15:39 -0300514
Daniel Vettera01025a2013-05-22 00:50:23 +0200515 return intel_crtc->active;
516 } else {
517 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
518 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700519}
520
Keith Packard42f52ef2008-10-18 19:39:29 -0700521/* Called from drm generic code, passed a 'crtc', which
522 * we use as a pipe index
523 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700524static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700525{
526 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
527 unsigned long high_frame;
528 unsigned long low_frame;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300529 u32 high1, high2, low, pixel, vbl_start;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700530
531 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800532 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800533 "pipe %c\n", pipe_name(pipe));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700534 return 0;
535 }
536
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300537 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
538 struct intel_crtc *intel_crtc =
539 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
540 const struct drm_display_mode *mode =
541 &intel_crtc->config.adjusted_mode;
542
543 vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
544 } else {
545 enum transcoder cpu_transcoder =
546 intel_pipe_to_cpu_transcoder(dev_priv, pipe);
547 u32 htotal;
548
549 htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
550 vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
551
552 vbl_start *= htotal;
553 }
554
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800555 high_frame = PIPEFRAME(pipe);
556 low_frame = PIPEFRAMEPIXEL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +0100557
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700558 /*
559 * High & low register fields aren't synchronized, so make sure
560 * we get a low value that's stable across two reads of the high
561 * register.
562 */
563 do {
Chris Wilson5eddb702010-09-11 13:48:45 +0100564 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300565 low = I915_READ(low_frame);
Chris Wilson5eddb702010-09-11 13:48:45 +0100566 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700567 } while (high1 != high2);
568
Chris Wilson5eddb702010-09-11 13:48:45 +0100569 high1 >>= PIPE_FRAME_HIGH_SHIFT;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300570 pixel = low & PIPE_PIXEL_MASK;
Chris Wilson5eddb702010-09-11 13:48:45 +0100571 low >>= PIPE_FRAME_LOW_SHIFT;
Ville Syrjälä391f75e2013-09-25 19:55:26 +0300572
573 /*
574 * The frame counter increments at beginning of active.
575 * Cook up a vblank counter by also checking the pixel
576 * counter against vblank start.
577 */
578 return ((high1 << 8) | low) + (pixel >= vbl_start);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700579}
580
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700581static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800582{
583 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800584 int reg = PIPE_FRMCOUNT_GM45(pipe);
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800585
586 if (!i915_pipe_enabled(dev, pipe)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +0800587 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800588 "pipe %c\n", pipe_name(pipe));
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800589 return 0;
590 }
591
592 return I915_READ(reg);
593}
594
Ville Syrjälä54ddcbd2013-09-23 13:02:07 +0300595static bool g4x_pipe_in_vblank(struct drm_device *dev, enum pipe pipe)
596{
597 struct drm_i915_private *dev_priv = dev->dev_private;
598 uint32_t status;
599
600 if (IS_VALLEYVIEW(dev)) {
601 status = pipe == PIPE_A ?
602 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
603 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
604
605 return I915_READ(VLV_ISR) & status;
606 } else if (IS_G4X(dev)) {
607 status = pipe == PIPE_A ?
608 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT :
609 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
610
611 return I915_READ(ISR) & status;
612 } else if (INTEL_INFO(dev)->gen < 7) {
613 status = pipe == PIPE_A ?
614 DE_PIPEA_VBLANK :
615 DE_PIPEB_VBLANK;
616
617 return I915_READ(DEISR) & status;
618 } else {
619 switch (pipe) {
620 default:
621 case PIPE_A:
622 status = DE_PIPEA_VBLANK_IVB;
623 break;
624 case PIPE_B:
625 status = DE_PIPEB_VBLANK_IVB;
626 break;
627 case PIPE_C:
628 status = DE_PIPEC_VBLANK_IVB;
629 break;
630 }
631
632 return I915_READ(DEISR) & status;
633 }
634}
635
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700636static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100637 int *vpos, int *hpos)
638{
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300639 struct drm_i915_private *dev_priv = dev->dev_private;
640 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
642 const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300643 int position;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100644 int vbl_start, vbl_end, htotal, vtotal;
645 bool in_vbl = true;
646 int ret = 0;
647
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300648 if (!intel_crtc->active) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100649 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800650 "pipe %c\n", pipe_name(pipe));
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100651 return 0;
652 }
653
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300654 htotal = mode->crtc_htotal;
655 vtotal = mode->crtc_vtotal;
656 vbl_start = mode->crtc_vblank_start;
657 vbl_end = mode->crtc_vblank_end;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100658
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +0300659 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
660
661 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100662 /* No obvious pixelcount register. Only query vertical
663 * scanout position from Display scan line register.
664 */
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300665 position = I915_READ(PIPEDSL(pipe)) & 0x1fff;
Ville Syrjälä54ddcbd2013-09-23 13:02:07 +0300666
667 /*
668 * The scanline counter increments at the leading edge
669 * of hsync, ie. it completely misses the active portion
670 * of the line. Fix up the counter at both edges of vblank
671 * to get a more accurate picture whether we're in vblank
672 * or not.
673 */
674 in_vbl = g4x_pipe_in_vblank(dev, pipe);
675 if ((in_vbl && position == vbl_start - 1) ||
676 (!in_vbl && position == vbl_end - 1))
677 position = (position + 1) % vtotal;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100678 } else {
679 /* Have access to pixelcount since start of frame.
680 * We can split this into vertical and horizontal
681 * scanout position.
682 */
683 position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
684
Ville Syrjälä3aa18df2013-10-11 19:10:32 +0300685 /* convert to pixel counts */
686 vbl_start *= htotal;
687 vbl_end *= htotal;
688 vtotal *= htotal;
689 }
690
691 in_vbl = position >= vbl_start && position < vbl_end;
692
693 /*
694 * While in vblank, position will be negative
695 * counting up towards 0 at vbl_end. And outside
696 * vblank, position will be positive counting
697 * up since vbl_end.
698 */
699 if (position >= vbl_start)
700 position -= vbl_end;
701 else
702 position += vtotal - vbl_end;
703
704 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
705 *vpos = position;
706 *hpos = 0;
707 } else {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100708 *vpos = position / htotal;
709 *hpos = position - (*vpos * htotal);
710 }
711
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100712 /* In vblank? */
713 if (in_vbl)
714 ret |= DRM_SCANOUTPOS_INVBL;
715
716 return ret;
717}
718
Jesse Barnesf71d4af2011-06-28 13:00:41 -0700719static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100720 int *max_error,
721 struct timeval *vblank_time,
722 unsigned flags)
723{
Chris Wilson4041b852011-01-22 10:07:56 +0000724 struct drm_crtc *crtc;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100725
Ben Widawsky7eb552a2013-03-13 14:05:41 -0700726 if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
Chris Wilson4041b852011-01-22 10:07:56 +0000727 DRM_ERROR("Invalid crtc %d\n", pipe);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100728 return -EINVAL;
729 }
730
731 /* Get drm_crtc to timestamp: */
Chris Wilson4041b852011-01-22 10:07:56 +0000732 crtc = intel_get_crtc_for_pipe(dev, pipe);
733 if (crtc == NULL) {
734 DRM_ERROR("Invalid crtc %d\n", pipe);
735 return -EINVAL;
736 }
737
738 if (!crtc->enabled) {
739 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
740 return -EBUSY;
741 }
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100742
743 /* Helper routine in DRM core does all the work: */
Chris Wilson4041b852011-01-22 10:07:56 +0000744 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
745 vblank_time, flags,
746 crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +0100747}
748
Jani Nikula67c347f2013-09-17 14:26:34 +0300749static bool intel_hpd_irq_event(struct drm_device *dev,
750 struct drm_connector *connector)
Egbert Eich321a1b32013-04-11 16:00:26 +0200751{
752 enum drm_connector_status old_status;
753
754 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
755 old_status = connector->status;
756
757 connector->status = connector->funcs->detect(connector, false);
Jani Nikula67c347f2013-09-17 14:26:34 +0300758 if (old_status == connector->status)
759 return false;
760
761 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
Egbert Eich321a1b32013-04-11 16:00:26 +0200762 connector->base.id,
763 drm_get_connector_name(connector),
Jani Nikula67c347f2013-09-17 14:26:34 +0300764 drm_get_connector_status_name(old_status),
765 drm_get_connector_status_name(connector->status));
766
767 return true;
Egbert Eich321a1b32013-04-11 16:00:26 +0200768}
769
Jesse Barnes5ca58282009-03-31 14:11:15 -0700770/*
771 * Handle hotplug events outside the interrupt handler proper.
772 */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200773#define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
774
Jesse Barnes5ca58282009-03-31 14:11:15 -0700775static void i915_hotplug_work_func(struct work_struct *work)
776{
777 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
778 hotplug_work);
779 struct drm_device *dev = dev_priv->dev;
Keith Packardc31c4ba2009-05-06 11:48:58 -0700780 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200781 struct intel_connector *intel_connector;
782 struct intel_encoder *intel_encoder;
783 struct drm_connector *connector;
784 unsigned long irqflags;
785 bool hpd_disabled = false;
Egbert Eich321a1b32013-04-11 16:00:26 +0200786 bool changed = false;
Egbert Eich142e2392013-04-11 15:57:57 +0200787 u32 hpd_event_bits;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700788
Daniel Vetter52d7ece2012-12-01 21:03:22 +0100789 /* HPD irq before everything is fully set up. */
790 if (!dev_priv->enable_hotplug_processing)
791 return;
792
Keith Packarda65e34c2011-07-25 10:04:56 -0700793 mutex_lock(&mode_config->mutex);
Jesse Barnese67189ab2011-02-11 14:44:51 -0800794 DRM_DEBUG_KMS("running encoder hotplug functions\n");
795
Egbert Eichcd569ae2013-04-16 13:36:57 +0200796 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Egbert Eich142e2392013-04-11 15:57:57 +0200797
798 hpd_event_bits = dev_priv->hpd_event_bits;
799 dev_priv->hpd_event_bits = 0;
Egbert Eichcd569ae2013-04-16 13:36:57 +0200800 list_for_each_entry(connector, &mode_config->connector_list, head) {
801 intel_connector = to_intel_connector(connector);
802 intel_encoder = intel_connector->encoder;
803 if (intel_encoder->hpd_pin > HPD_NONE &&
804 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
805 connector->polled == DRM_CONNECTOR_POLL_HPD) {
806 DRM_INFO("HPD interrupt storm detected on connector %s: "
807 "switching from hotplug detection to polling\n",
808 drm_get_connector_name(connector));
809 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
810 connector->polled = DRM_CONNECTOR_POLL_CONNECT
811 | DRM_CONNECTOR_POLL_DISCONNECT;
812 hpd_disabled = true;
813 }
Egbert Eich142e2392013-04-11 15:57:57 +0200814 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
815 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
816 drm_get_connector_name(connector), intel_encoder->hpd_pin);
817 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200818 }
819 /* if there were no outputs to poll, poll was disabled,
820 * therefore make sure it's enabled when disabling HPD on
821 * some connectors */
Egbert Eichac4c16c2013-04-16 13:36:58 +0200822 if (hpd_disabled) {
Egbert Eichcd569ae2013-04-16 13:36:57 +0200823 drm_kms_helper_poll_enable(dev);
Egbert Eichac4c16c2013-04-16 13:36:58 +0200824 mod_timer(&dev_priv->hotplug_reenable_timer,
825 jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
826 }
Egbert Eichcd569ae2013-04-16 13:36:57 +0200827
828 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
829
Egbert Eich321a1b32013-04-11 16:00:26 +0200830 list_for_each_entry(connector, &mode_config->connector_list, head) {
831 intel_connector = to_intel_connector(connector);
832 intel_encoder = intel_connector->encoder;
833 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
834 if (intel_encoder->hot_plug)
835 intel_encoder->hot_plug(intel_encoder);
836 if (intel_hpd_irq_event(dev, connector))
837 changed = true;
838 }
839 }
Keith Packard40ee3382011-07-28 15:31:19 -0700840 mutex_unlock(&mode_config->mutex);
841
Egbert Eich321a1b32013-04-11 16:00:26 +0200842 if (changed)
843 drm_kms_helper_hotplug_event(dev);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700844}
845
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200846static void ironlake_rps_change_irq_handler(struct drm_device *dev)
Jesse Barnesf97108d2010-01-29 11:27:07 -0800847{
848 drm_i915_private_t *dev_priv = dev->dev_private;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000849 u32 busy_up, busy_down, max_avg, min_avg;
Daniel Vetter92703882012-08-09 16:46:01 +0200850 u8 new_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200851
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200852 spin_lock(&mchdev_lock);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800853
Daniel Vetter73edd18f2012-08-08 23:35:37 +0200854 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
855
Daniel Vetter20e4d402012-08-08 23:35:39 +0200856 new_delay = dev_priv->ips.cur_delay;
Daniel Vetter92703882012-08-09 16:46:01 +0200857
Jesse Barnes7648fa92010-05-20 14:28:11 -0700858 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000859 busy_up = I915_READ(RCPREVBSYTUPAVG);
860 busy_down = I915_READ(RCPREVBSYTDNAVG);
Jesse Barnesf97108d2010-01-29 11:27:07 -0800861 max_avg = I915_READ(RCBMAXAVG);
862 min_avg = I915_READ(RCBMINAVG);
863
864 /* Handle RCS change request from hw */
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000865 if (busy_up > max_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200866 if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
867 new_delay = dev_priv->ips.cur_delay - 1;
868 if (new_delay < dev_priv->ips.max_delay)
869 new_delay = dev_priv->ips.max_delay;
Matthew Garrettb5b72e82010-02-02 18:30:47 +0000870 } else if (busy_down < min_avg) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200871 if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
872 new_delay = dev_priv->ips.cur_delay + 1;
873 if (new_delay > dev_priv->ips.min_delay)
874 new_delay = dev_priv->ips.min_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800875 }
876
Jesse Barnes7648fa92010-05-20 14:28:11 -0700877 if (ironlake_set_drps(dev, new_delay))
Daniel Vetter20e4d402012-08-08 23:35:39 +0200878 dev_priv->ips.cur_delay = new_delay;
Jesse Barnesf97108d2010-01-29 11:27:07 -0800879
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +0200880 spin_unlock(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +0200881
Jesse Barnesf97108d2010-01-29 11:27:07 -0800882 return;
883}
884
Chris Wilson549f7362010-10-19 11:19:32 +0100885static void notify_ring(struct drm_device *dev,
886 struct intel_ring_buffer *ring)
887{
Chris Wilson475553d2011-01-20 09:52:56 +0000888 if (ring->obj == NULL)
889 return;
890
Chris Wilson814e9b52013-09-23 17:33:19 -0300891 trace_i915_gem_request_complete(ring);
Chris Wilson9862e602011-01-04 22:22:17 +0000892
Chris Wilson549f7362010-10-19 11:19:32 +0100893 wake_up_all(&ring->irq_queue);
Mika Kuoppala10cd45b2013-07-03 17:22:08 +0300894 i915_queue_hangcheck(dev);
Chris Wilson549f7362010-10-19 11:19:32 +0100895}
896
Ben Widawsky4912d042011-04-25 11:25:20 -0700897static void gen6_pm_rps_work(struct work_struct *work)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800898{
Ben Widawsky4912d042011-04-25 11:25:20 -0700899 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200900 rps.work);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300901 u32 pm_iir;
Chris Wilsondd75fdc2013-09-25 17:34:57 +0100902 int new_delay, adj;
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800903
Daniel Vetter59cdb632013-07-04 23:35:28 +0200904 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +0200905 pm_iir = dev_priv->rps.pm_iir;
906 dev_priv->rps.pm_iir = 0;
Ben Widawsky48484052013-05-28 19:22:27 -0700907 /* Make sure not to corrupt PMIMR state used by ringbuffer code */
Paulo Zanoniedbfdb42013-08-06 18:57:13 -0300908 snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
Daniel Vetter59cdb632013-07-04 23:35:28 +0200909 spin_unlock_irq(&dev_priv->irq_lock);
Ben Widawsky4912d042011-04-25 11:25:20 -0700910
Paulo Zanoni60611c12013-08-15 11:50:01 -0300911 /* Make sure we didn't queue anything we're not going to process. */
912 WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
913
Ben Widawsky48484052013-05-28 19:22:27 -0700914 if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800915 return;
916
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700917 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +0100918
Chris Wilsondd75fdc2013-09-25 17:34:57 +0100919 adj = dev_priv->rps.last_adj;
Ville Syrjälä74250342013-06-25 21:38:11 +0300920 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
Chris Wilsondd75fdc2013-09-25 17:34:57 +0100921 if (adj > 0)
922 adj *= 2;
923 else
924 adj = 1;
925 new_delay = dev_priv->rps.cur_delay + adj;
Ville Syrjälä74250342013-06-25 21:38:11 +0300926
927 /*
928 * For better performance, jump directly
929 * to RPe if we're below it.
930 */
Chris Wilsondd75fdc2013-09-25 17:34:57 +0100931 if (new_delay < dev_priv->rps.rpe_delay)
Ville Syrjälä74250342013-06-25 21:38:11 +0300932 new_delay = dev_priv->rps.rpe_delay;
Chris Wilsondd75fdc2013-09-25 17:34:57 +0100933 } else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
934 if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
935 new_delay = dev_priv->rps.rpe_delay;
936 else
937 new_delay = dev_priv->rps.min_delay;
938 adj = 0;
939 } else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
940 if (adj < 0)
941 adj *= 2;
942 else
943 adj = -1;
944 new_delay = dev_priv->rps.cur_delay + adj;
945 } else { /* unknown event */
946 new_delay = dev_priv->rps.cur_delay;
947 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800948
Ben Widawsky79249632012-09-07 19:43:42 -0700949 /* sysfs frequency interfaces may have snuck in while servicing the
950 * interrupt
951 */
Chris Wilsondd75fdc2013-09-25 17:34:57 +0100952 if (new_delay < (int)dev_priv->rps.min_delay)
953 new_delay = dev_priv->rps.min_delay;
954 if (new_delay > (int)dev_priv->rps.max_delay)
955 new_delay = dev_priv->rps.max_delay;
956 dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_delay;
957
958 if (IS_VALLEYVIEW(dev_priv->dev))
959 valleyview_set_rps(dev_priv->dev, new_delay);
960 else
961 gen6_set_rps(dev_priv->dev, new_delay);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800962
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700963 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -0800964}
965
Ben Widawskye3689192012-05-25 16:56:22 -0700966
967/**
968 * ivybridge_parity_work - Workqueue called when a parity error interrupt
969 * occurred.
970 * @work: workqueue struct
971 *
972 * Doesn't actually do anything except notify userspace. As a consequence of
973 * this event, userspace should try to remap the bad rows since statistically
974 * it is likely the same row is more likely to go bad again.
975 */
976static void ivybridge_parity_work(struct work_struct *work)
977{
978 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
Daniel Vettera4da4fa2012-11-02 19:55:07 +0100979 l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -0700980 u32 error_status, row, bank, subbank;
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700981 char *parity_event[6];
Ben Widawskye3689192012-05-25 16:56:22 -0700982 uint32_t misccpctl;
983 unsigned long flags;
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700984 uint8_t slice = 0;
Ben Widawskye3689192012-05-25 16:56:22 -0700985
986 /* We must turn off DOP level clock gating to access the L3 registers.
987 * In order to prevent a get/put style interface, acquire struct mutex
988 * any time we access those registers.
989 */
990 mutex_lock(&dev_priv->dev->struct_mutex);
991
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700992 /* If we've screwed up tracking, just let the interrupt fire again */
993 if (WARN_ON(!dev_priv->l3_parity.which_slice))
994 goto out;
995
Ben Widawskye3689192012-05-25 16:56:22 -0700996 misccpctl = I915_READ(GEN7_MISCCPCTL);
997 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
998 POSTING_READ(GEN7_MISCCPCTL);
999
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001000 while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1001 u32 reg;
Ben Widawskye3689192012-05-25 16:56:22 -07001002
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001003 slice--;
1004 if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
1005 break;
1006
1007 dev_priv->l3_parity.which_slice &= ~(1<<slice);
1008
1009 reg = GEN7_L3CDERRST1 + (slice * 0x200);
1010
1011 error_status = I915_READ(reg);
1012 row = GEN7_PARITY_ERROR_ROW(error_status);
1013 bank = GEN7_PARITY_ERROR_BANK(error_status);
1014 subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1015
1016 I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
1017 POSTING_READ(reg);
1018
1019 parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1020 parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1021 parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1022 parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
1023 parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
1024 parity_event[5] = NULL;
1025
1026 kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
1027 KOBJ_CHANGE, parity_event);
1028
1029 DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
1030 slice, row, bank, subbank);
1031
1032 kfree(parity_event[4]);
1033 kfree(parity_event[3]);
1034 kfree(parity_event[2]);
1035 kfree(parity_event[1]);
1036 }
Ben Widawskye3689192012-05-25 16:56:22 -07001037
1038 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
1039
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001040out:
1041 WARN_ON(dev_priv->l3_parity.which_slice);
Ben Widawskye3689192012-05-25 16:56:22 -07001042 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001043 ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
Ben Widawskye3689192012-05-25 16:56:22 -07001044 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1045
1046 mutex_unlock(&dev_priv->dev->struct_mutex);
Ben Widawskye3689192012-05-25 16:56:22 -07001047}
1048
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001049static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
Ben Widawskye3689192012-05-25 16:56:22 -07001050{
1051 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Ben Widawskye3689192012-05-25 16:56:22 -07001052
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001053 if (!HAS_L3_DPF(dev))
Ben Widawskye3689192012-05-25 16:56:22 -07001054 return;
1055
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001056 spin_lock(&dev_priv->irq_lock);
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001057 ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001058 spin_unlock(&dev_priv->irq_lock);
Ben Widawskye3689192012-05-25 16:56:22 -07001059
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001060 iir &= GT_PARITY_ERROR(dev);
1061 if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
1062 dev_priv->l3_parity.which_slice |= 1 << 1;
1063
1064 if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
1065 dev_priv->l3_parity.which_slice |= 1 << 0;
1066
Daniel Vettera4da4fa2012-11-02 19:55:07 +01001067 queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
Ben Widawskye3689192012-05-25 16:56:22 -07001068}
1069
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001070static void ilk_gt_irq_handler(struct drm_device *dev,
1071 struct drm_i915_private *dev_priv,
1072 u32 gt_iir)
1073{
1074 if (gt_iir &
1075 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1076 notify_ring(dev, &dev_priv->ring[RCS]);
1077 if (gt_iir & ILK_BSD_USER_INTERRUPT)
1078 notify_ring(dev, &dev_priv->ring[VCS]);
1079}
1080
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001081static void snb_gt_irq_handler(struct drm_device *dev,
1082 struct drm_i915_private *dev_priv,
1083 u32 gt_iir)
1084{
1085
Ben Widawskycc609d52013-05-28 19:22:29 -07001086 if (gt_iir &
1087 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001088 notify_ring(dev, &dev_priv->ring[RCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001089 if (gt_iir & GT_BSD_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001090 notify_ring(dev, &dev_priv->ring[VCS]);
Ben Widawskycc609d52013-05-28 19:22:29 -07001091 if (gt_iir & GT_BLT_USER_INTERRUPT)
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001092 notify_ring(dev, &dev_priv->ring[BCS]);
1093
Ben Widawskycc609d52013-05-28 19:22:29 -07001094 if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1095 GT_BSD_CS_ERROR_INTERRUPT |
1096 GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001097 DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
1098 i915_handle_error(dev, false);
1099 }
Ben Widawskye3689192012-05-25 16:56:22 -07001100
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001101 if (gt_iir & GT_PARITY_ERROR(dev))
1102 ivybridge_parity_error_irq_handler(dev, gt_iir);
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001103}
1104
Egbert Eichb543fb02013-04-16 13:36:54 +02001105#define HPD_STORM_DETECT_PERIOD 1000
1106#define HPD_STORM_THRESHOLD 5
1107
Daniel Vetter10a504d2013-06-27 17:52:12 +02001108static inline void intel_hpd_irq_handler(struct drm_device *dev,
Daniel Vetter22062db2013-06-27 17:52:11 +02001109 u32 hotplug_trigger,
1110 const u32 *hpd)
Egbert Eichb543fb02013-04-16 13:36:54 +02001111{
1112 drm_i915_private_t *dev_priv = dev->dev_private;
Egbert Eichb543fb02013-04-16 13:36:54 +02001113 int i;
Daniel Vetter10a504d2013-06-27 17:52:12 +02001114 bool storm_detected = false;
Egbert Eichb543fb02013-04-16 13:36:54 +02001115
Daniel Vetter91d131d2013-06-27 17:52:14 +02001116 if (!hotplug_trigger)
1117 return;
1118
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02001119 spin_lock(&dev_priv->irq_lock);
Egbert Eichb543fb02013-04-16 13:36:54 +02001120 for (i = 1; i < HPD_NUM_PINS; i++) {
Egbert Eich821450c2013-04-16 13:36:55 +02001121
Egbert Eichb8f102e2013-07-26 14:14:24 +02001122 WARN(((hpd[i] & hotplug_trigger) &&
1123 dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED),
1124 "Received HPD interrupt although disabled\n");
1125
Egbert Eichb543fb02013-04-16 13:36:54 +02001126 if (!(hpd[i] & hotplug_trigger) ||
1127 dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1128 continue;
1129
Jani Nikulabc5ead8c2013-05-07 15:10:29 +03001130 dev_priv->hpd_event_bits |= (1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001131 if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1132 dev_priv->hpd_stats[i].hpd_last_jiffies
1133 + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1134 dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1135 dev_priv->hpd_stats[i].hpd_cnt = 0;
Egbert Eichb8f102e2013-07-26 14:14:24 +02001136 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001137 } else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1138 dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
Egbert Eich142e2392013-04-11 15:57:57 +02001139 dev_priv->hpd_event_bits &= ~(1 << i);
Egbert Eichb543fb02013-04-16 13:36:54 +02001140 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
Daniel Vetter10a504d2013-06-27 17:52:12 +02001141 storm_detected = true;
Egbert Eichb543fb02013-04-16 13:36:54 +02001142 } else {
1143 dev_priv->hpd_stats[i].hpd_cnt++;
Egbert Eichb8f102e2013-07-26 14:14:24 +02001144 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1145 dev_priv->hpd_stats[i].hpd_cnt);
Egbert Eichb543fb02013-04-16 13:36:54 +02001146 }
1147 }
1148
Daniel Vetter10a504d2013-06-27 17:52:12 +02001149 if (storm_detected)
1150 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02001151 spin_unlock(&dev_priv->irq_lock);
Daniel Vetter5876fa02013-06-27 17:52:13 +02001152
Daniel Vetter645416f2013-09-02 16:22:25 +02001153 /*
1154 * Our hotplug handler can grab modeset locks (by calling down into the
1155 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1156 * queue for otherwise the flush_work in the pageflip code will
1157 * deadlock.
1158 */
1159 schedule_work(&dev_priv->hotplug_work);
Egbert Eichb543fb02013-04-16 13:36:54 +02001160}
1161
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001162static void gmbus_irq_handler(struct drm_device *dev)
1163{
Daniel Vetter28c70f12012-12-01 13:53:45 +01001164 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
1165
Daniel Vetter28c70f12012-12-01 13:53:45 +01001166 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001167}
1168
Daniel Vetterce99c252012-12-01 13:53:47 +01001169static void dp_aux_irq_handler(struct drm_device *dev)
1170{
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001171 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
1172
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001173 wake_up_all(&dev_priv->gmbus_wait_queue);
Daniel Vetterce99c252012-12-01 13:53:47 +01001174}
1175
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001176/* The RPS events need forcewake, so we add them to a work queue and mask their
1177 * IMR bits until the work is done. Other interrupts can be processed without
1178 * the work queue. */
1179static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
Ben Widawskybaf02a12013-05-28 19:22:24 -07001180{
Daniel Vetter41a05a32013-07-04 23:35:26 +02001181 if (pm_iir & GEN6_PM_RPS_EVENTS) {
Daniel Vetter59cdb632013-07-04 23:35:28 +02001182 spin_lock(&dev_priv->irq_lock);
Daniel Vetter41a05a32013-07-04 23:35:26 +02001183 dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
Paulo Zanoni4d3b3d52013-08-09 17:04:36 -03001184 snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
Daniel Vetter59cdb632013-07-04 23:35:28 +02001185 spin_unlock(&dev_priv->irq_lock);
Daniel Vetter2adbee62013-07-04 23:35:27 +02001186
1187 queue_work(dev_priv->wq, &dev_priv->rps.work);
Ben Widawskybaf02a12013-05-28 19:22:24 -07001188 }
Ben Widawskybaf02a12013-05-28 19:22:24 -07001189
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001190 if (HAS_VEBOX(dev_priv->dev)) {
1191 if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1192 notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
Ben Widawsky12638c52013-05-28 19:22:31 -07001193
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001194 if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
1195 DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
1196 i915_handle_error(dev_priv->dev, false);
1197 }
Ben Widawsky12638c52013-05-28 19:22:31 -07001198 }
Ben Widawskybaf02a12013-05-28 19:22:24 -07001199}
1200
Daniel Vetterff1f5252012-10-02 15:10:55 +02001201static irqreturn_t valleyview_irq_handler(int irq, void *arg)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001202{
1203 struct drm_device *dev = (struct drm_device *) arg;
1204 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1205 u32 iir, gt_iir, pm_iir;
1206 irqreturn_t ret = IRQ_NONE;
1207 unsigned long irqflags;
1208 int pipe;
1209 u32 pipe_stats[I915_MAX_PIPES];
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001210
1211 atomic_inc(&dev_priv->irq_received);
1212
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001213 while (true) {
1214 iir = I915_READ(VLV_IIR);
1215 gt_iir = I915_READ(GTIIR);
1216 pm_iir = I915_READ(GEN6_PMIIR);
1217
1218 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1219 goto out;
1220
1221 ret = IRQ_HANDLED;
1222
Daniel Vettere7b4c6b2012-03-30 20:24:35 +02001223 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001224
1225 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1226 for_each_pipe(pipe) {
1227 int reg = PIPESTAT(pipe);
1228 pipe_stats[pipe] = I915_READ(reg);
1229
1230 /*
1231 * Clear the PIPE*STAT regs before the IIR
1232 */
1233 if (pipe_stats[pipe] & 0x8000ffff) {
1234 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1235 DRM_DEBUG_DRIVER("pipe %c underrun\n",
1236 pipe_name(pipe));
1237 I915_WRITE(reg, pipe_stats[pipe]);
1238 }
1239 }
1240 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1241
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001242 for_each_pipe(pipe) {
1243 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1244 drm_handle_vblank(dev, pipe);
1245
1246 if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
1247 intel_prepare_page_flip(dev, pipe);
1248 intel_finish_page_flip(dev, pipe);
1249 }
1250 }
1251
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001252 /* Consume port. Then clear IIR or we'll miss events */
1253 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
1254 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
Egbert Eichb543fb02013-04-16 13:36:54 +02001255 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001256
1257 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1258 hotplug_status);
Daniel Vetter91d131d2013-06-27 17:52:14 +02001259
1260 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
1261
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001262 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1263 I915_READ(PORT_HOTPLUG_STAT);
1264 }
1265
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001266 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1267 gmbus_irq_handler(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001268
Paulo Zanoni60611c12013-08-15 11:50:01 -03001269 if (pm_iir)
Daniel Vetterd0ecd7e2013-07-04 23:35:25 +02001270 gen6_rps_irq_handler(dev_priv, pm_iir);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001271
1272 I915_WRITE(GTIIR, gt_iir);
1273 I915_WRITE(GEN6_PMIIR, pm_iir);
1274 I915_WRITE(VLV_IIR, iir);
1275 }
1276
1277out:
1278 return ret;
1279}
1280
Adam Jackson23e81d62012-06-06 15:45:44 -04001281static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
Jesse Barnes776ad802011-01-04 15:09:39 -08001282{
1283 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001284 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001285 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
Jesse Barnes776ad802011-01-04 15:09:39 -08001286
Daniel Vetter91d131d2013-06-27 17:52:14 +02001287 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
1288
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001289 if (pch_iir & SDE_AUDIO_POWER_MASK) {
1290 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1291 SDE_AUDIO_POWER_SHIFT);
Jesse Barnes776ad802011-01-04 15:09:39 -08001292 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001293 port_name(port));
1294 }
Jesse Barnes776ad802011-01-04 15:09:39 -08001295
Daniel Vetterce99c252012-12-01 13:53:47 +01001296 if (pch_iir & SDE_AUX_MASK)
1297 dp_aux_irq_handler(dev);
1298
Jesse Barnes776ad802011-01-04 15:09:39 -08001299 if (pch_iir & SDE_GMBUS)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001300 gmbus_irq_handler(dev);
Jesse Barnes776ad802011-01-04 15:09:39 -08001301
1302 if (pch_iir & SDE_AUDIO_HDCP_MASK)
1303 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1304
1305 if (pch_iir & SDE_AUDIO_TRANS_MASK)
1306 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1307
1308 if (pch_iir & SDE_POISON)
1309 DRM_ERROR("PCH poison interrupt\n");
1310
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001311 if (pch_iir & SDE_FDI_MASK)
1312 for_each_pipe(pipe)
1313 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1314 pipe_name(pipe),
1315 I915_READ(FDI_RX_IIR(pipe)));
Jesse Barnes776ad802011-01-04 15:09:39 -08001316
1317 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1318 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1319
1320 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1321 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1322
Jesse Barnes776ad802011-01-04 15:09:39 -08001323 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
Paulo Zanoni86642812013-04-12 17:57:57 -03001324 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1325 false))
1326 DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
1327
1328 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1329 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1330 false))
1331 DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
1332}
1333
1334static void ivb_err_int_handler(struct drm_device *dev)
1335{
1336 struct drm_i915_private *dev_priv = dev->dev_private;
1337 u32 err_int = I915_READ(GEN7_ERR_INT);
1338
Paulo Zanonide032bf2013-04-12 17:57:58 -03001339 if (err_int & ERR_INT_POISON)
1340 DRM_ERROR("Poison interrupt\n");
1341
Paulo Zanoni86642812013-04-12 17:57:57 -03001342 if (err_int & ERR_INT_FIFO_UNDERRUN_A)
1343 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1344 DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1345
1346 if (err_int & ERR_INT_FIFO_UNDERRUN_B)
1347 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1348 DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1349
1350 if (err_int & ERR_INT_FIFO_UNDERRUN_C)
1351 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
1352 DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
1353
1354 I915_WRITE(GEN7_ERR_INT, err_int);
1355}
1356
1357static void cpt_serr_int_handler(struct drm_device *dev)
1358{
1359 struct drm_i915_private *dev_priv = dev->dev_private;
1360 u32 serr_int = I915_READ(SERR_INT);
1361
Paulo Zanonide032bf2013-04-12 17:57:58 -03001362 if (serr_int & SERR_INT_POISON)
1363 DRM_ERROR("PCH poison interrupt\n");
1364
Paulo Zanoni86642812013-04-12 17:57:57 -03001365 if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
1366 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1367 false))
1368 DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
1369
1370 if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
1371 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1372 false))
1373 DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
1374
1375 if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
1376 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
1377 false))
1378 DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
1379
1380 I915_WRITE(SERR_INT, serr_int);
Jesse Barnes776ad802011-01-04 15:09:39 -08001381}
1382
Adam Jackson23e81d62012-06-06 15:45:44 -04001383static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
1384{
1385 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1386 int pipe;
Egbert Eichb543fb02013-04-16 13:36:54 +02001387 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
Adam Jackson23e81d62012-06-06 15:45:44 -04001388
Daniel Vetter91d131d2013-06-27 17:52:14 +02001389 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
1390
Ville Syrjäläcfc33bf2013-04-17 17:48:48 +03001391 if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1392 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
1393 SDE_AUDIO_POWER_SHIFT_CPT);
1394 DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1395 port_name(port));
1396 }
Adam Jackson23e81d62012-06-06 15:45:44 -04001397
1398 if (pch_iir & SDE_AUX_MASK_CPT)
Daniel Vetterce99c252012-12-01 13:53:47 +01001399 dp_aux_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001400
1401 if (pch_iir & SDE_GMBUS_CPT)
Daniel Vetter515ac2b2012-12-01 13:53:44 +01001402 gmbus_irq_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001403
1404 if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
1405 DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
1406
1407 if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
1408 DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
1409
1410 if (pch_iir & SDE_FDI_MASK_CPT)
1411 for_each_pipe(pipe)
1412 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1413 pipe_name(pipe),
1414 I915_READ(FDI_RX_IIR(pipe)));
Paulo Zanoni86642812013-04-12 17:57:57 -03001415
1416 if (pch_iir & SDE_ERROR_CPT)
1417 cpt_serr_int_handler(dev);
Adam Jackson23e81d62012-06-06 15:45:44 -04001418}
1419
Paulo Zanonic008bc62013-07-12 16:35:10 -03001420static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1421{
1422 struct drm_i915_private *dev_priv = dev->dev_private;
1423
1424 if (de_iir & DE_AUX_CHANNEL_A)
1425 dp_aux_irq_handler(dev);
1426
1427 if (de_iir & DE_GSE)
1428 intel_opregion_asle_intr(dev);
1429
1430 if (de_iir & DE_PIPEA_VBLANK)
1431 drm_handle_vblank(dev, 0);
1432
1433 if (de_iir & DE_PIPEB_VBLANK)
1434 drm_handle_vblank(dev, 1);
1435
1436 if (de_iir & DE_POISON)
1437 DRM_ERROR("Poison interrupt\n");
1438
1439 if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1440 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1441 DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1442
1443 if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1444 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1445 DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1446
1447 if (de_iir & DE_PLANEA_FLIP_DONE) {
1448 intel_prepare_page_flip(dev, 0);
1449 intel_finish_page_flip_plane(dev, 0);
1450 }
1451
1452 if (de_iir & DE_PLANEB_FLIP_DONE) {
1453 intel_prepare_page_flip(dev, 1);
1454 intel_finish_page_flip_plane(dev, 1);
1455 }
1456
1457 /* check event from PCH */
1458 if (de_iir & DE_PCH_EVENT) {
1459 u32 pch_iir = I915_READ(SDEIIR);
1460
1461 if (HAS_PCH_CPT(dev))
1462 cpt_irq_handler(dev, pch_iir);
1463 else
1464 ibx_irq_handler(dev, pch_iir);
1465
1466 /* should clear PCH hotplug event before clear CPU irq */
1467 I915_WRITE(SDEIIR, pch_iir);
1468 }
1469
1470 if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1471 ironlake_rps_change_irq_handler(dev);
1472}
1473
Paulo Zanoni9719fb92013-07-12 16:35:11 -03001474static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
1475{
1476 struct drm_i915_private *dev_priv = dev->dev_private;
1477 int i;
1478
1479 if (de_iir & DE_ERR_INT_IVB)
1480 ivb_err_int_handler(dev);
1481
1482 if (de_iir & DE_AUX_CHANNEL_A_IVB)
1483 dp_aux_irq_handler(dev);
1484
1485 if (de_iir & DE_GSE_IVB)
1486 intel_opregion_asle_intr(dev);
1487
1488 for (i = 0; i < 3; i++) {
1489 if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
1490 drm_handle_vblank(dev, i);
1491 if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
1492 intel_prepare_page_flip(dev, i);
1493 intel_finish_page_flip_plane(dev, i);
1494 }
1495 }
1496
1497 /* check event from PCH */
1498 if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
1499 u32 pch_iir = I915_READ(SDEIIR);
1500
1501 cpt_irq_handler(dev, pch_iir);
1502
1503 /* clear PCH hotplug event before clear CPU irq */
1504 I915_WRITE(SDEIIR, pch_iir);
1505 }
1506}
1507
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001508static irqreturn_t ironlake_irq_handler(int irq, void *arg)
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001509{
1510 struct drm_device *dev = (struct drm_device *) arg;
1511 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001512 u32 de_iir, gt_iir, de_ier, sde_ier = 0;
Chris Wilson0e434062012-05-09 21:45:44 +01001513 irqreturn_t ret = IRQ_NONE;
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001514
1515 atomic_inc(&dev_priv->irq_received);
1516
Paulo Zanoni86642812013-04-12 17:57:57 -03001517 /* We get interrupts on unclaimed registers, so check for this before we
1518 * do any I915_{READ,WRITE}. */
Chris Wilson907b28c2013-07-19 20:36:52 +01001519 intel_uncore_check_errors(dev);
Paulo Zanoni86642812013-04-12 17:57:57 -03001520
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001521 /* disable master interrupt before clearing iir */
1522 de_ier = I915_READ(DEIER);
1523 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
Paulo Zanoni23a78512013-07-12 16:35:14 -03001524 POSTING_READ(DEIER);
Chris Wilson0e434062012-05-09 21:45:44 +01001525
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001526 /* Disable south interrupts. We'll only write to SDEIIR once, so further
1527 * interrupts will will be stored on its back queue, and then we'll be
1528 * able to process them after we restore SDEIER (as soon as we restore
1529 * it, we'll get an interrupt if SDEIIR still has something to process
1530 * due to its back queue). */
Ben Widawskyab5c6082013-04-05 13:12:41 -07001531 if (!HAS_PCH_NOP(dev)) {
1532 sde_ier = I915_READ(SDEIER);
1533 I915_WRITE(SDEIER, 0);
1534 POSTING_READ(SDEIER);
1535 }
Paulo Zanoni44498ae2013-02-22 17:05:28 -03001536
Chris Wilson0e434062012-05-09 21:45:44 +01001537 gt_iir = I915_READ(GTIIR);
1538 if (gt_iir) {
Paulo Zanonid8fc8a42013-07-19 18:57:55 -03001539 if (INTEL_INFO(dev)->gen >= 6)
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001540 snb_gt_irq_handler(dev, dev_priv, gt_iir);
Paulo Zanonid8fc8a42013-07-19 18:57:55 -03001541 else
1542 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01001543 I915_WRITE(GTIIR, gt_iir);
1544 ret = IRQ_HANDLED;
1545 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001546
1547 de_iir = I915_READ(DEIIR);
Chris Wilson0e434062012-05-09 21:45:44 +01001548 if (de_iir) {
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001549 if (INTEL_INFO(dev)->gen >= 7)
1550 ivb_display_irq_handler(dev, de_iir);
1551 else
1552 ilk_display_irq_handler(dev, de_iir);
Chris Wilson0e434062012-05-09 21:45:44 +01001553 I915_WRITE(DEIIR, de_iir);
1554 ret = IRQ_HANDLED;
1555 }
1556
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001557 if (INTEL_INFO(dev)->gen >= 6) {
1558 u32 pm_iir = I915_READ(GEN6_PMIIR);
1559 if (pm_iir) {
Paulo Zanoni1403c0d2013-08-15 11:51:32 -03001560 gen6_rps_irq_handler(dev_priv, pm_iir);
Paulo Zanonif1af8fc2013-07-12 19:56:30 -03001561 I915_WRITE(GEN6_PMIIR, pm_iir);
1562 ret = IRQ_HANDLED;
1563 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001564 }
1565
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001566 I915_WRITE(DEIER, de_ier);
1567 POSTING_READ(DEIER);
Ben Widawskyab5c6082013-04-05 13:12:41 -07001568 if (!HAS_PCH_NOP(dev)) {
1569 I915_WRITE(SDEIER, sde_ier);
1570 POSTING_READ(SDEIER);
1571 }
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001572
1573 return ret;
1574}
1575
Daniel Vetter17e1df02013-09-08 21:57:13 +02001576static void i915_error_wake_up(struct drm_i915_private *dev_priv,
1577 bool reset_completed)
1578{
1579 struct intel_ring_buffer *ring;
1580 int i;
1581
1582 /*
1583 * Notify all waiters for GPU completion events that reset state has
1584 * been changed, and that they need to restart their wait after
1585 * checking for potential errors (and bail out to drop locks if there is
1586 * a gpu reset pending so that i915_error_work_func can acquire them).
1587 */
1588
1589 /* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
1590 for_each_ring(ring, dev_priv, i)
1591 wake_up_all(&ring->irq_queue);
1592
1593 /* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
1594 wake_up_all(&dev_priv->pending_flip_queue);
1595
1596 /*
1597 * Signal tasks blocked in i915_gem_wait_for_error that the pending
1598 * reset state is cleared.
1599 */
1600 if (reset_completed)
1601 wake_up_all(&dev_priv->gpu_error.reset_queue);
1602}
1603
Jesse Barnes8a905232009-07-11 16:48:03 -04001604/**
1605 * i915_error_work_func - do process context error handling work
1606 * @work: work struct
1607 *
1608 * Fire an error uevent so userspace can see that a hang or error
1609 * was detected.
1610 */
1611static void i915_error_work_func(struct work_struct *work)
1612{
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001613 struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
1614 work);
1615 drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
1616 gpu_error);
Jesse Barnes8a905232009-07-11 16:48:03 -04001617 struct drm_device *dev = dev_priv->dev;
Ben Widawskycce723e2013-07-19 09:16:42 -07001618 char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1619 char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1620 char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
Daniel Vetter17e1df02013-09-08 21:57:13 +02001621 int ret;
Jesse Barnes8a905232009-07-11 16:48:03 -04001622
Ben Gamarif316a422009-09-14 17:48:46 -04001623 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
Jesse Barnes8a905232009-07-11 16:48:03 -04001624
Daniel Vetter7db0ba22012-12-06 16:23:37 +01001625 /*
1626 * Note that there's only one work item which does gpu resets, so we
1627 * need not worry about concurrent gpu resets potentially incrementing
1628 * error->reset_counter twice. We only need to take care of another
1629 * racing irq/hangcheck declaring the gpu dead for a second time. A
1630 * quick check for that is good enough: schedule_work ensures the
1631 * correct ordering between hang detection and this work item, and since
1632 * the reset in-progress bit is only ever set by code outside of this
1633 * work we don't need to worry about any other races.
1634 */
1635 if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
Chris Wilsonf803aa52010-09-19 12:38:26 +01001636 DRM_DEBUG_DRIVER("resetting chip\n");
Daniel Vetter7db0ba22012-12-06 16:23:37 +01001637 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
1638 reset_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001639
Daniel Vetter17e1df02013-09-08 21:57:13 +02001640 /*
1641 * All state reset _must_ be completed before we update the
1642 * reset counter, for otherwise waiters might miss the reset
1643 * pending state and not properly drop locks, resulting in
1644 * deadlocks with the reset work.
1645 */
Daniel Vetterf69061b2012-12-06 09:01:42 +01001646 ret = i915_reset(dev);
1647
Daniel Vetter17e1df02013-09-08 21:57:13 +02001648 intel_display_handle_reset(dev);
1649
Daniel Vetterf69061b2012-12-06 09:01:42 +01001650 if (ret == 0) {
1651 /*
1652 * After all the gem state is reset, increment the reset
1653 * counter and wake up everyone waiting for the reset to
1654 * complete.
1655 *
1656 * Since unlock operations are a one-sided barrier only,
1657 * we need to insert a barrier here to order any seqno
1658 * updates before
1659 * the counter increment.
1660 */
1661 smp_mb__before_atomic_inc();
1662 atomic_inc(&dev_priv->gpu_error.reset_counter);
1663
1664 kobject_uevent_env(&dev->primary->kdev.kobj,
1665 KOBJ_CHANGE, reset_done_event);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001666 } else {
1667 atomic_set(&error->reset_counter, I915_WEDGED);
Ben Gamarif316a422009-09-14 17:48:46 -04001668 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001669
Daniel Vetter17e1df02013-09-08 21:57:13 +02001670 /*
1671 * Note: The wake_up also serves as a memory barrier so that
1672 * waiters see the update value of the reset counter atomic_t.
1673 */
1674 i915_error_wake_up(dev_priv, true);
Ben Gamarif316a422009-09-14 17:48:46 -04001675 }
Jesse Barnes8a905232009-07-11 16:48:03 -04001676}
1677
Chris Wilson35aed2e2010-05-27 13:18:12 +01001678static void i915_report_and_clear_eir(struct drm_device *dev)
Jesse Barnes8a905232009-07-11 16:48:03 -04001679{
1680 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybd9854f2012-08-23 15:18:09 -07001681 uint32_t instdone[I915_NUM_INSTDONE_REG];
Jesse Barnes8a905232009-07-11 16:48:03 -04001682 u32 eir = I915_READ(EIR);
Ben Widawsky050ee912012-08-22 11:32:15 -07001683 int pipe, i;
Jesse Barnes8a905232009-07-11 16:48:03 -04001684
Chris Wilson35aed2e2010-05-27 13:18:12 +01001685 if (!eir)
1686 return;
Jesse Barnes8a905232009-07-11 16:48:03 -04001687
Joe Perchesa70491c2012-03-18 13:00:11 -07001688 pr_err("render error detected, EIR: 0x%08x\n", eir);
Jesse Barnes8a905232009-07-11 16:48:03 -04001689
Ben Widawskybd9854f2012-08-23 15:18:09 -07001690 i915_get_extra_instdone(dev, instdone);
1691
Jesse Barnes8a905232009-07-11 16:48:03 -04001692 if (IS_G4X(dev)) {
1693 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
1694 u32 ipeir = I915_READ(IPEIR_I965);
1695
Joe Perchesa70491c2012-03-18 13:00:11 -07001696 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1697 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Ben Widawsky050ee912012-08-22 11:32:15 -07001698 for (i = 0; i < ARRAY_SIZE(instdone); i++)
1699 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Joe Perchesa70491c2012-03-18 13:00:11 -07001700 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07001701 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001702 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001703 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001704 }
1705 if (eir & GM45_ERROR_PAGE_TABLE) {
1706 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001707 pr_err("page table error\n");
1708 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001709 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001710 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001711 }
1712 }
1713
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001714 if (!IS_GEN2(dev)) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001715 if (eir & I915_ERROR_PAGE_TABLE) {
1716 u32 pgtbl_err = I915_READ(PGTBL_ER);
Joe Perchesa70491c2012-03-18 13:00:11 -07001717 pr_err("page table error\n");
1718 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
Jesse Barnes8a905232009-07-11 16:48:03 -04001719 I915_WRITE(PGTBL_ER, pgtbl_err);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001720 POSTING_READ(PGTBL_ER);
Jesse Barnes8a905232009-07-11 16:48:03 -04001721 }
1722 }
1723
1724 if (eir & I915_ERROR_MEMORY_REFRESH) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001725 pr_err("memory refresh error:\n");
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001726 for_each_pipe(pipe)
Joe Perchesa70491c2012-03-18 13:00:11 -07001727 pr_err("pipe %c stat: 0x%08x\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001728 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
Jesse Barnes8a905232009-07-11 16:48:03 -04001729 /* pipestat has already been acked */
1730 }
1731 if (eir & I915_ERROR_INSTRUCTION) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001732 pr_err("instruction error\n");
1733 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
Ben Widawsky050ee912012-08-22 11:32:15 -07001734 for (i = 0; i < ARRAY_SIZE(instdone); i++)
1735 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001736 if (INTEL_INFO(dev)->gen < 4) {
Jesse Barnes8a905232009-07-11 16:48:03 -04001737 u32 ipeir = I915_READ(IPEIR);
1738
Joe Perchesa70491c2012-03-18 13:00:11 -07001739 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
1740 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
Joe Perchesa70491c2012-03-18 13:00:11 -07001741 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
Jesse Barnes8a905232009-07-11 16:48:03 -04001742 I915_WRITE(IPEIR, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001743 POSTING_READ(IPEIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001744 } else {
1745 u32 ipeir = I915_READ(IPEIR_I965);
1746
Joe Perchesa70491c2012-03-18 13:00:11 -07001747 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1748 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
Joe Perchesa70491c2012-03-18 13:00:11 -07001749 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
Joe Perchesa70491c2012-03-18 13:00:11 -07001750 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
Jesse Barnes8a905232009-07-11 16:48:03 -04001751 I915_WRITE(IPEIR_I965, ipeir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001752 POSTING_READ(IPEIR_I965);
Jesse Barnes8a905232009-07-11 16:48:03 -04001753 }
1754 }
1755
1756 I915_WRITE(EIR, eir);
Chris Wilson3143a2b2010-11-16 15:55:10 +00001757 POSTING_READ(EIR);
Jesse Barnes8a905232009-07-11 16:48:03 -04001758 eir = I915_READ(EIR);
1759 if (eir) {
1760 /*
1761 * some errors might have become stuck,
1762 * mask them.
1763 */
1764 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
1765 I915_WRITE(EMR, I915_READ(EMR) | eir);
1766 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
1767 }
Chris Wilson35aed2e2010-05-27 13:18:12 +01001768}
1769
1770/**
1771 * i915_handle_error - handle an error interrupt
1772 * @dev: drm device
1773 *
1774 * Do some basic checking of regsiter state at error interrupt time and
1775 * dump it to the syslog. Also call i915_capture_error_state() to make
1776 * sure we get a record and make it available in debugfs. Fire a uevent
1777 * so userspace knows something bad happened (should trigger collection
1778 * of a ring dump etc.).
1779 */
Chris Wilson527f9e92010-11-11 01:16:58 +00001780void i915_handle_error(struct drm_device *dev, bool wedged)
Chris Wilson35aed2e2010-05-27 13:18:12 +01001781{
1782 struct drm_i915_private *dev_priv = dev->dev_private;
1783
1784 i915_capture_error_state(dev);
1785 i915_report_and_clear_eir(dev);
Jesse Barnes8a905232009-07-11 16:48:03 -04001786
Ben Gamariba1234d2009-09-14 17:48:47 -04001787 if (wedged) {
Daniel Vetterf69061b2012-12-06 09:01:42 +01001788 atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1789 &dev_priv->gpu_error.reset_counter);
Ben Gamariba1234d2009-09-14 17:48:47 -04001790
Ben Gamari11ed50e2009-09-14 17:48:45 -04001791 /*
Daniel Vetter17e1df02013-09-08 21:57:13 +02001792 * Wakeup waiting processes so that the reset work function
1793 * i915_error_work_func doesn't deadlock trying to grab various
1794 * locks. By bumping the reset counter first, the woken
1795 * processes will see a reset in progress and back off,
1796 * releasing their locks and then wait for the reset completion.
1797 * We must do this for _all_ gpu waiters that might hold locks
1798 * that the reset work needs to acquire.
1799 *
1800 * Note: The wake_up serves as the required memory barrier to
1801 * ensure that the waiters see the updated value of the reset
1802 * counter atomic_t.
Ben Gamari11ed50e2009-09-14 17:48:45 -04001803 */
Daniel Vetter17e1df02013-09-08 21:57:13 +02001804 i915_error_wake_up(dev_priv, false);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001805 }
1806
Daniel Vetter122f46b2013-09-04 17:36:14 +02001807 /*
1808 * Our reset work can grab modeset locks (since it needs to reset the
1809 * state of outstanding pagelips). Hence it must not be run on our own
1810 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
1811 * code will deadlock.
1812 */
1813 schedule_work(&dev_priv->gpu_error.work);
Jesse Barnes8a905232009-07-11 16:48:03 -04001814}
1815
Ville Syrjälä21ad8332013-02-19 15:16:39 +02001816static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001817{
1818 drm_i915_private_t *dev_priv = dev->dev_private;
1819 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00001821 struct drm_i915_gem_object *obj;
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001822 struct intel_unpin_work *work;
1823 unsigned long flags;
1824 bool stall_detected;
1825
1826 /* Ignore early vblank irqs */
1827 if (intel_crtc == NULL)
1828 return;
1829
1830 spin_lock_irqsave(&dev->event_lock, flags);
1831 work = intel_crtc->unpin_work;
1832
Chris Wilsone7d841c2012-12-03 11:36:30 +00001833 if (work == NULL ||
1834 atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1835 !work->enable_stall_check) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001836 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
1837 spin_unlock_irqrestore(&dev->event_lock, flags);
1838 return;
1839 }
1840
1841 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
Chris Wilson05394f32010-11-08 19:18:58 +00001842 obj = work->pending_flip_obj;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001843 if (INTEL_INFO(dev)->gen >= 4) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001844 int dspsurf = DSPSURF(intel_crtc->plane);
Armin Reese446f2542012-03-30 16:20:16 -07001845 stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001846 i915_gem_obj_ggtt_offset(obj);
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001847 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001848 int dspaddr = DSPADDR(intel_crtc->plane);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001849 stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001850 crtc->y * crtc->fb->pitches[0] +
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01001851 crtc->x * crtc->fb->bits_per_pixel/8);
1852 }
1853
1854 spin_unlock_irqrestore(&dev->event_lock, flags);
1855
1856 if (stall_detected) {
1857 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
1858 intel_prepare_page_flip(dev, intel_crtc->plane);
1859 }
1860}
1861
Keith Packard42f52ef2008-10-18 19:39:29 -07001862/* Called from drm generic code, passed 'crtc' which
1863 * we use as a pipe index
1864 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001865static int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001866{
1867 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001868 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001869
Chris Wilson5eddb702010-09-11 13:48:45 +01001870 if (!i915_pipe_enabled(dev, pipe))
Jesse Barnes71e0ffa2009-01-08 10:42:15 -08001871 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001872
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001873 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001874 if (INTEL_INFO(dev)->gen >= 4)
Keith Packard7c463582008-11-04 02:03:27 -08001875 i915_enable_pipestat(dev_priv, pipe,
1876 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -07001877 else
Keith Packard7c463582008-11-04 02:03:27 -08001878 i915_enable_pipestat(dev_priv, pipe,
1879 PIPE_VBLANK_INTERRUPT_ENABLE);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001880
1881 /* maintain vblank delivery even in deep C-states */
1882 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001883 I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001884 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001885
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001886 return 0;
1887}
1888
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001889static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001890{
1891 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1892 unsigned long irqflags;
Paulo Zanonib5184212013-07-12 20:00:08 -03001893 uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
1894 DE_PIPE_VBLANK_ILK(pipe);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001895
1896 if (!i915_pipe_enabled(dev, pipe))
1897 return -EINVAL;
1898
1899 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Paulo Zanonib5184212013-07-12 20:00:08 -03001900 ironlake_enable_display_irq(dev_priv, bit);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001901 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1902
1903 return 0;
1904}
1905
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001906static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
1907{
1908 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1909 unsigned long irqflags;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001910 u32 imr;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001911
1912 if (!i915_pipe_enabled(dev, pipe))
1913 return -EINVAL;
1914
1915 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001916 imr = I915_READ(VLV_IMR);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001917 if (pipe == 0)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001918 imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001919 else
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001920 imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001921 I915_WRITE(VLV_IMR, imr);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001922 i915_enable_pipestat(dev_priv, pipe,
1923 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001924 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1925
1926 return 0;
1927}
1928
Keith Packard42f52ef2008-10-18 19:39:29 -07001929/* Called from drm generic code, passed 'crtc' which
1930 * we use as a pipe index
1931 */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001932static void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001933{
1934 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -07001935 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001936
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001937 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Chris Wilson8692d00e2011-02-05 10:08:21 +00001938 if (dev_priv->info->gen == 3)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001939 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
Chris Wilson8692d00e2011-02-05 10:08:21 +00001940
Jesse Barnesf796cf82011-04-07 13:58:17 -07001941 i915_disable_pipestat(dev_priv, pipe,
1942 PIPE_VBLANK_INTERRUPT_ENABLE |
1943 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1944 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1945}
1946
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001947static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnesf796cf82011-04-07 13:58:17 -07001948{
1949 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1950 unsigned long irqflags;
Paulo Zanonib5184212013-07-12 20:00:08 -03001951 uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
1952 DE_PIPE_VBLANK_ILK(pipe);
Jesse Barnesf796cf82011-04-07 13:58:17 -07001953
1954 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Paulo Zanonib5184212013-07-12 20:00:08 -03001955 ironlake_disable_display_irq(dev_priv, bit);
Jesse Barnesb1f14ad2011-04-06 12:13:38 -07001956 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1957}
1958
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001959static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
1960{
1961 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1962 unsigned long irqflags;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001963 u32 imr;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001964
1965 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001966 i915_disable_pipestat(dev_priv, pipe,
1967 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001968 imr = I915_READ(VLV_IMR);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001969 if (pipe == 0)
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001970 imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07001971 else
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001972 imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001973 I915_WRITE(VLV_IMR, imr);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07001974 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1975}
1976
Chris Wilson893eead2010-10-27 14:44:35 +01001977static u32
1978ring_last_seqno(struct intel_ring_buffer *ring)
Zou Nan hai852835f2010-05-21 09:08:56 +08001979{
Chris Wilson893eead2010-10-27 14:44:35 +01001980 return list_entry(ring->request_list.prev,
1981 struct drm_i915_gem_request, list)->seqno;
1982}
1983
Chris Wilson9107e9d2013-06-10 11:20:20 +01001984static bool
1985ring_idle(struct intel_ring_buffer *ring, u32 seqno)
Chris Wilson893eead2010-10-27 14:44:35 +01001986{
Chris Wilson9107e9d2013-06-10 11:20:20 +01001987 return (list_empty(&ring->request_list) ||
1988 i915_seqno_passed(seqno, ring_last_seqno(ring)));
Ben Gamarif65d9422009-09-14 17:48:44 -04001989}
1990
Chris Wilson6274f212013-06-10 11:20:21 +01001991static struct intel_ring_buffer *
1992semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
Chris Wilsona24a11e2013-03-14 17:52:05 +02001993{
1994 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6274f212013-06-10 11:20:21 +01001995 u32 cmd, ipehr, acthd, acthd_min;
Chris Wilsona24a11e2013-03-14 17:52:05 +02001996
1997 ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
1998 if ((ipehr & ~(0x3 << 16)) !=
1999 (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
Chris Wilson6274f212013-06-10 11:20:21 +01002000 return NULL;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002001
2002 /* ACTHD is likely pointing to the dword after the actual command,
2003 * so scan backwards until we find the MBOX.
2004 */
Chris Wilson6274f212013-06-10 11:20:21 +01002005 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002006 acthd_min = max((int)acthd - 3 * 4, 0);
2007 do {
2008 cmd = ioread32(ring->virtual_start + acthd);
2009 if (cmd == ipehr)
2010 break;
2011
2012 acthd -= 4;
2013 if (acthd < acthd_min)
Chris Wilson6274f212013-06-10 11:20:21 +01002014 return NULL;
Chris Wilsona24a11e2013-03-14 17:52:05 +02002015 } while (1);
2016
Chris Wilson6274f212013-06-10 11:20:21 +01002017 *seqno = ioread32(ring->virtual_start+acthd+4)+1;
2018 return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
Chris Wilsona24a11e2013-03-14 17:52:05 +02002019}
2020
Chris Wilson6274f212013-06-10 11:20:21 +01002021static int semaphore_passed(struct intel_ring_buffer *ring)
2022{
2023 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2024 struct intel_ring_buffer *signaller;
2025 u32 seqno, ctl;
2026
2027 ring->hangcheck.deadlock = true;
2028
2029 signaller = semaphore_waits_for(ring, &seqno);
2030 if (signaller == NULL || signaller->hangcheck.deadlock)
2031 return -1;
2032
2033 /* cursory check for an unkickable deadlock */
2034 ctl = I915_READ_CTL(signaller);
2035 if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
2036 return -1;
2037
2038 return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
2039}
2040
2041static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
2042{
2043 struct intel_ring_buffer *ring;
2044 int i;
2045
2046 for_each_ring(ring, dev_priv, i)
2047 ring->hangcheck.deadlock = false;
2048}
2049
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03002050static enum intel_ring_hangcheck_action
2051ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002052{
2053 struct drm_device *dev = ring->dev;
2054 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002055 u32 tmp;
2056
Chris Wilson6274f212013-06-10 11:20:21 +01002057 if (ring->hangcheck.acthd != acthd)
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002058 return HANGCHECK_ACTIVE;
Chris Wilson6274f212013-06-10 11:20:21 +01002059
Chris Wilson9107e9d2013-06-10 11:20:20 +01002060 if (IS_GEN2(dev))
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002061 return HANGCHECK_HUNG;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002062
2063 /* Is the chip hanging on a WAIT_FOR_EVENT?
2064 * If so we can simply poke the RB_WAIT bit
2065 * and break the hang. This should work on
2066 * all but the second generation chipsets.
2067 */
2068 tmp = I915_READ_CTL(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002069 if (tmp & RING_WAIT) {
2070 DRM_ERROR("Kicking stuck wait on %s\n",
2071 ring->name);
Chris Wilson09e14bf2013-10-10 09:37:19 +01002072 i915_handle_error(dev, false);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002073 I915_WRITE_CTL(ring, tmp);
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002074 return HANGCHECK_KICK;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002075 }
Chris Wilsona24a11e2013-03-14 17:52:05 +02002076
Chris Wilson6274f212013-06-10 11:20:21 +01002077 if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
2078 switch (semaphore_passed(ring)) {
2079 default:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002080 return HANGCHECK_HUNG;
Chris Wilson6274f212013-06-10 11:20:21 +01002081 case 1:
2082 DRM_ERROR("Kicking stuck semaphore on %s\n",
2083 ring->name);
Chris Wilson09e14bf2013-10-10 09:37:19 +01002084 i915_handle_error(dev, false);
Chris Wilson6274f212013-06-10 11:20:21 +01002085 I915_WRITE_CTL(ring, tmp);
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002086 return HANGCHECK_KICK;
Chris Wilson6274f212013-06-10 11:20:21 +01002087 case 0:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002088 return HANGCHECK_WAIT;
Chris Wilson6274f212013-06-10 11:20:21 +01002089 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002090 }
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002091
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002092 return HANGCHECK_HUNG;
Mika Kuoppalaed5cbb02013-05-13 16:32:11 +03002093}
2094
Ben Gamarif65d9422009-09-14 17:48:44 -04002095/**
2096 * This is called when the chip hasn't reported back with completed
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002097 * batchbuffers in a long time. We keep track per ring seqno progress and
2098 * if there are no progress, hangcheck score for that ring is increased.
2099 * Further, acthd is inspected to see if the ring is stuck. On stuck case
2100 * we kick the ring. If we see no progress on three subsequent calls
2101 * we assume chip is wedged and try to fix it by resetting the chip.
Ben Gamarif65d9422009-09-14 17:48:44 -04002102 */
Damien Lespiaua658b5d2013-08-08 22:28:56 +01002103static void i915_hangcheck_elapsed(unsigned long data)
Ben Gamarif65d9422009-09-14 17:48:44 -04002104{
2105 struct drm_device *dev = (struct drm_device *)data;
2106 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002107 struct intel_ring_buffer *ring;
Chris Wilsonb4519512012-05-11 14:29:30 +01002108 int i;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002109 int busy_count = 0, rings_hung = 0;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002110 bool stuck[I915_NUM_RINGS] = { 0 };
2111#define BUSY 1
2112#define KICK 5
2113#define HUNG 20
2114#define FIRE 30
Chris Wilson893eead2010-10-27 14:44:35 +01002115
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07002116 if (!i915_enable_hangcheck)
2117 return;
2118
Chris Wilsonb4519512012-05-11 14:29:30 +01002119 for_each_ring(ring, dev_priv, i) {
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002120 u32 seqno, acthd;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002121 bool busy = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01002122
Chris Wilson6274f212013-06-10 11:20:21 +01002123 semaphore_clear_deadlocks(dev_priv);
2124
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002125 seqno = ring->get_seqno(ring, false);
2126 acthd = intel_ring_get_active_head(ring);
Chris Wilsond1e61e72012-04-10 17:00:41 +01002127
Chris Wilson9107e9d2013-06-10 11:20:20 +01002128 if (ring->hangcheck.seqno == seqno) {
2129 if (ring_idle(ring, seqno)) {
Mika Kuoppalada661462013-09-06 16:03:28 +03002130 ring->hangcheck.action = HANGCHECK_IDLE;
2131
Chris Wilson9107e9d2013-06-10 11:20:20 +01002132 if (waitqueue_active(&ring->irq_queue)) {
2133 /* Issue a wake-up to catch stuck h/w. */
Chris Wilson094f9a52013-09-25 17:34:55 +01002134 if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2135 DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
2136 ring->name);
2137 wake_up_all(&ring->irq_queue);
2138 }
2139 /* Safeguard against driver failure */
2140 ring->hangcheck.score += BUSY;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002141 } else
2142 busy = false;
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002143 } else {
Chris Wilson6274f212013-06-10 11:20:21 +01002144 /* We always increment the hangcheck score
2145 * if the ring is busy and still processing
2146 * the same request, so that no single request
2147 * can run indefinitely (such as a chain of
2148 * batches). The only time we do not increment
2149 * the hangcheck score on this ring, if this
2150 * ring is in a legitimate wait for another
2151 * ring. In that case the waiting ring is a
2152 * victim and we want to be sure we catch the
2153 * right culprit. Then every time we do kick
2154 * the ring, add a small increment to the
2155 * score so that we can catch a batch that is
2156 * being repeatedly kicked and so responsible
2157 * for stalling the machine.
2158 */
Mika Kuoppalaad8beae2013-06-12 12:35:32 +03002159 ring->hangcheck.action = ring_stuck(ring,
2160 acthd);
2161
2162 switch (ring->hangcheck.action) {
Mika Kuoppalada661462013-09-06 16:03:28 +03002163 case HANGCHECK_IDLE:
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002164 case HANGCHECK_WAIT:
Chris Wilson6274f212013-06-10 11:20:21 +01002165 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002166 case HANGCHECK_ACTIVE:
Jani Nikulaea04cb32013-08-11 12:44:02 +03002167 ring->hangcheck.score += BUSY;
Chris Wilson6274f212013-06-10 11:20:21 +01002168 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002169 case HANGCHECK_KICK:
Jani Nikulaea04cb32013-08-11 12:44:02 +03002170 ring->hangcheck.score += KICK;
Chris Wilson6274f212013-06-10 11:20:21 +01002171 break;
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002172 case HANGCHECK_HUNG:
Jani Nikulaea04cb32013-08-11 12:44:02 +03002173 ring->hangcheck.score += HUNG;
Chris Wilson6274f212013-06-10 11:20:21 +01002174 stuck[i] = true;
2175 break;
2176 }
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002177 }
Chris Wilson9107e9d2013-06-10 11:20:20 +01002178 } else {
Mika Kuoppalada661462013-09-06 16:03:28 +03002179 ring->hangcheck.action = HANGCHECK_ACTIVE;
2180
Chris Wilson9107e9d2013-06-10 11:20:20 +01002181 /* Gradually reduce the count so that we catch DoS
2182 * attempts across multiple batches.
2183 */
2184 if (ring->hangcheck.score > 0)
2185 ring->hangcheck.score--;
Chris Wilsond1e61e72012-04-10 17:00:41 +01002186 }
2187
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002188 ring->hangcheck.seqno = seqno;
2189 ring->hangcheck.acthd = acthd;
Chris Wilson9107e9d2013-06-10 11:20:20 +01002190 busy_count += busy;
Chris Wilson893eead2010-10-27 14:44:35 +01002191 }
Eric Anholtb9201c12010-01-08 14:25:16 -08002192
Mika Kuoppala92cab732013-05-24 17:16:07 +03002193 for_each_ring(ring, dev_priv, i) {
Chris Wilson9107e9d2013-06-10 11:20:20 +01002194 if (ring->hangcheck.score > FIRE) {
Daniel Vetterb8d88d12013-08-28 10:57:59 +02002195 DRM_INFO("%s on %s\n",
2196 stuck[i] ? "stuck" : "no progress",
2197 ring->name);
Chris Wilsona43adf02013-06-10 11:20:22 +01002198 rings_hung++;
Mika Kuoppala92cab732013-05-24 17:16:07 +03002199 }
2200 }
2201
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002202 if (rings_hung)
2203 return i915_handle_error(dev, true);
Ben Gamarif65d9422009-09-14 17:48:44 -04002204
Mika Kuoppala05407ff2013-05-30 09:04:29 +03002205 if (busy_count)
2206 /* Reset timer case chip hangs without another request
2207 * being added */
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03002208 i915_queue_hangcheck(dev);
2209}
2210
2211void i915_queue_hangcheck(struct drm_device *dev)
2212{
2213 struct drm_i915_private *dev_priv = dev->dev_private;
2214 if (!i915_enable_hangcheck)
2215 return;
2216
2217 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2218 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
Ben Gamarif65d9422009-09-14 17:48:44 -04002219}
2220
Paulo Zanoni91738a92013-06-05 14:21:51 -03002221static void ibx_irq_preinstall(struct drm_device *dev)
2222{
2223 struct drm_i915_private *dev_priv = dev->dev_private;
2224
2225 if (HAS_PCH_NOP(dev))
2226 return;
2227
2228 /* south display irq */
2229 I915_WRITE(SDEIMR, 0xffffffff);
2230 /*
2231 * SDEIER is also touched by the interrupt handler to work around missed
2232 * PCH interrupts. Hence we can't update it after the interrupt handler
2233 * is enabled - instead we unconditionally enable all PCH interrupt
2234 * sources here, but then only unmask them as needed with SDEIMR.
2235 */
2236 I915_WRITE(SDEIER, 0xffffffff);
2237 POSTING_READ(SDEIER);
2238}
2239
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02002240static void gen5_gt_irq_preinstall(struct drm_device *dev)
2241{
2242 struct drm_i915_private *dev_priv = dev->dev_private;
2243
2244 /* and GT */
2245 I915_WRITE(GTIMR, 0xffffffff);
2246 I915_WRITE(GTIER, 0x0);
2247 POSTING_READ(GTIER);
2248
2249 if (INTEL_INFO(dev)->gen >= 6) {
2250 /* and PM */
2251 I915_WRITE(GEN6_PMIMR, 0xffffffff);
2252 I915_WRITE(GEN6_PMIER, 0x0);
2253 POSTING_READ(GEN6_PMIER);
2254 }
2255}
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257/* drm_dma.h hooks
2258*/
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002259static void ironlake_irq_preinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002260{
2261 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2262
Jesse Barnes46979952011-04-07 13:53:55 -07002263 atomic_set(&dev_priv->irq_received, 0);
2264
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002265 I915_WRITE(HWSTAM, 0xeffe);
Daniel Vetterbdfcdb62012-01-05 01:05:26 +01002266
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002267 I915_WRITE(DEIMR, 0xffffffff);
2268 I915_WRITE(DEIER, 0x0);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002269 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002270
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02002271 gen5_gt_irq_preinstall(dev);
Zhenyu Wangc6501562009-11-03 18:57:21 +00002272
Paulo Zanoni91738a92013-06-05 14:21:51 -03002273 ibx_irq_preinstall(dev);
Ben Widawsky7d991632013-05-28 19:22:25 -07002274}
2275
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002276static void valleyview_irq_preinstall(struct drm_device *dev)
2277{
2278 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2279 int pipe;
2280
2281 atomic_set(&dev_priv->irq_received, 0);
2282
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002283 /* VLV magic */
2284 I915_WRITE(VLV_IMR, 0);
2285 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
2286 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
2287 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
2288
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002289 /* and GT */
2290 I915_WRITE(GTIIR, I915_READ(GTIIR));
2291 I915_WRITE(GTIIR, I915_READ(GTIIR));
Daniel Vetterd18ea1b2013-07-12 22:43:25 +02002292
2293 gen5_gt_irq_preinstall(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002294
2295 I915_WRITE(DPINVGTT, 0xff);
2296
2297 I915_WRITE(PORT_HOTPLUG_EN, 0);
2298 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2299 for_each_pipe(pipe)
2300 I915_WRITE(PIPESTAT(pipe), 0xffff);
2301 I915_WRITE(VLV_IIR, 0xffffffff);
2302 I915_WRITE(VLV_IMR, 0xffffffff);
2303 I915_WRITE(VLV_IER, 0x0);
2304 POSTING_READ(VLV_IER);
2305}
2306
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002307static void ibx_hpd_irq_setup(struct drm_device *dev)
Keith Packard7fe0b972011-09-19 13:31:02 -07002308{
2309 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002310 struct drm_mode_config *mode_config = &dev->mode_config;
2311 struct intel_encoder *intel_encoder;
Daniel Vetterfee884e2013-07-04 23:35:21 +02002312 u32 hotplug_irqs, hotplug, enabled_irqs = 0;
Keith Packard7fe0b972011-09-19 13:31:02 -07002313
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002314 if (HAS_PCH_IBX(dev)) {
Daniel Vetterfee884e2013-07-04 23:35:21 +02002315 hotplug_irqs = SDE_HOTPLUG_MASK;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002316 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
Egbert Eichcd569ae2013-04-16 13:36:57 +02002317 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02002318 enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002319 } else {
Daniel Vetterfee884e2013-07-04 23:35:21 +02002320 hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002321 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
Egbert Eichcd569ae2013-04-16 13:36:57 +02002322 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
Daniel Vetterfee884e2013-07-04 23:35:21 +02002323 enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002324 }
2325
Daniel Vetterfee884e2013-07-04 23:35:21 +02002326 ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002327
2328 /*
2329 * Enable digital hotplug on the PCH, and configure the DP short pulse
2330 * duration to 2ms (which is the minimum in the Display Port spec)
2331 *
2332 * This register is the same on all known PCH chips.
2333 */
Keith Packard7fe0b972011-09-19 13:31:02 -07002334 hotplug = I915_READ(PCH_PORT_HOTPLUG);
2335 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
2336 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
2337 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
2338 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
2339 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
2340}
2341
Paulo Zanonid46da432013-02-08 17:35:15 -02002342static void ibx_irq_postinstall(struct drm_device *dev)
2343{
2344 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01002345 u32 mask;
Paulo Zanonid46da432013-02-08 17:35:15 -02002346
Daniel Vetter692a04c2013-05-29 21:43:05 +02002347 if (HAS_PCH_NOP(dev))
2348 return;
2349
Paulo Zanoni86642812013-04-12 17:57:57 -03002350 if (HAS_PCH_IBX(dev)) {
2351 mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
Paulo Zanonide032bf2013-04-12 17:57:58 -03002352 SDE_TRANSA_FIFO_UNDER | SDE_POISON;
Paulo Zanoni86642812013-04-12 17:57:57 -03002353 } else {
2354 mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
2355
2356 I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2357 }
Ben Widawskyab5c6082013-04-05 13:12:41 -07002358
Paulo Zanonid46da432013-02-08 17:35:15 -02002359 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2360 I915_WRITE(SDEIMR, ~mask);
Paulo Zanonid46da432013-02-08 17:35:15 -02002361}
2362
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02002363static void gen5_gt_irq_postinstall(struct drm_device *dev)
2364{
2365 struct drm_i915_private *dev_priv = dev->dev_private;
2366 u32 pm_irqs, gt_irqs;
2367
2368 pm_irqs = gt_irqs = 0;
2369
2370 dev_priv->gt_irq_mask = ~0;
Ben Widawsky040d2ba2013-09-19 11:01:40 -07002371 if (HAS_L3_DPF(dev)) {
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02002372 /* L3 parity interrupt is always unmasked. */
Ben Widawsky35a85ac2013-09-19 11:13:41 -07002373 dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
2374 gt_irqs |= GT_PARITY_ERROR(dev);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02002375 }
2376
2377 gt_irqs |= GT_RENDER_USER_INTERRUPT;
2378 if (IS_GEN5(dev)) {
2379 gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
2380 ILK_BSD_USER_INTERRUPT;
2381 } else {
2382 gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
2383 }
2384
2385 I915_WRITE(GTIIR, I915_READ(GTIIR));
2386 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2387 I915_WRITE(GTIER, gt_irqs);
2388 POSTING_READ(GTIER);
2389
2390 if (INTEL_INFO(dev)->gen >= 6) {
2391 pm_irqs |= GEN6_PM_RPS_EVENTS;
2392
2393 if (HAS_VEBOX(dev))
2394 pm_irqs |= PM_VEBOX_USER_INTERRUPT;
2395
Paulo Zanoni605cd252013-08-06 18:57:15 -03002396 dev_priv->pm_irq_mask = 0xffffffff;
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02002397 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
Paulo Zanoni605cd252013-08-06 18:57:15 -03002398 I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02002399 I915_WRITE(GEN6_PMIER, pm_irqs);
2400 POSTING_READ(GEN6_PMIER);
2401 }
2402}
2403
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002404static int ironlake_irq_postinstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002405{
Daniel Vetter4bc9d432013-06-27 13:44:58 +02002406 unsigned long irqflags;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002407 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03002408 u32 display_mask, extra_mask;
2409
2410 if (INTEL_INFO(dev)->gen >= 7) {
2411 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
2412 DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
2413 DE_PLANEB_FLIP_DONE_IVB |
2414 DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
2415 DE_ERR_INT_IVB);
2416 extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
2417 DE_PIPEA_VBLANK_IVB);
2418
2419 I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2420 } else {
2421 display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2422 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
2423 DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2424 DE_PIPEA_FIFO_UNDERRUN | DE_POISON);
2425 extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
2426 }
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002427
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002428 dev_priv->irq_mask = ~display_mask;
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002429
2430 /* should always can generate irq */
2431 I915_WRITE(DEIIR, I915_READ(DEIIR));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002432 I915_WRITE(DEIMR, dev_priv->irq_mask);
Paulo Zanoni8e76f8d2013-07-12 20:01:56 -03002433 I915_WRITE(DEIER, display_mask | extra_mask);
Chris Wilson3143a2b2010-11-16 15:55:10 +00002434 POSTING_READ(DEIER);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002435
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02002436 gen5_gt_irq_postinstall(dev);
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002437
Paulo Zanonid46da432013-02-08 17:35:15 -02002438 ibx_irq_postinstall(dev);
Keith Packard7fe0b972011-09-19 13:31:02 -07002439
Jesse Barnesf97108d2010-01-29 11:27:07 -08002440 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter6005ce42013-06-27 13:44:59 +02002441 /* Enable PCU event interrupts
2442 *
2443 * spinlocking not required here for correctness since interrupt
Daniel Vetter4bc9d432013-06-27 13:44:58 +02002444 * setup is guaranteed to run in single-threaded context. But we
2445 * need it to make the assert_spin_locked happy. */
2446 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnesf97108d2010-01-29 11:27:07 -08002447 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
Daniel Vetter4bc9d432013-06-27 13:44:58 +02002448 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnesf97108d2010-01-29 11:27:07 -08002449 }
2450
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002451 return 0;
2452}
2453
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002454static int valleyview_irq_postinstall(struct drm_device *dev)
2455{
2456 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002457 u32 enable_mask;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002458 u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
Daniel Vetterb79480b2013-06-27 17:52:10 +02002459 unsigned long irqflags;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002460
2461 enable_mask = I915_DISPLAY_PORT_INTERRUPT;
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002462 enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2463 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2464 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002465 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
2466
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002467 /*
2468 *Leave vblank interrupts masked initially. enable/disable will
2469 * toggle them based on usage.
2470 */
2471 dev_priv->irq_mask = (~enable_mask) |
2472 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2473 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002474
Daniel Vetter20afbda2012-12-11 14:05:07 +01002475 I915_WRITE(PORT_HOTPLUG_EN, 0);
2476 POSTING_READ(PORT_HOTPLUG_EN);
2477
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002478 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
2479 I915_WRITE(VLV_IER, enable_mask);
2480 I915_WRITE(VLV_IIR, 0xffffffff);
2481 I915_WRITE(PIPESTAT(0), 0xffff);
2482 I915_WRITE(PIPESTAT(1), 0xffff);
2483 POSTING_READ(VLV_IER);
2484
Daniel Vetterb79480b2013-06-27 17:52:10 +02002485 /* Interrupt setup is already guaranteed to be single-threaded, this is
2486 * just to make the assert_spin_locked check happy. */
2487 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002488 i915_enable_pipestat(dev_priv, 0, pipestat_enable);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01002489 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002490 i915_enable_pipestat(dev_priv, 1, pipestat_enable);
Daniel Vetterb79480b2013-06-27 17:52:10 +02002491 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Jesse Barnes31acc7f2012-06-20 10:53:11 -07002492
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002493 I915_WRITE(VLV_IIR, 0xffffffff);
2494 I915_WRITE(VLV_IIR, 0xffffffff);
2495
Daniel Vetter0a9a8c92013-07-12 22:43:26 +02002496 gen5_gt_irq_postinstall(dev);
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002497
2498 /* ack & enable invalid PTE error interrupts */
2499#if 0 /* FIXME: add support to irq handler for checking these bits */
2500 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
2501 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
2502#endif
2503
2504 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
Daniel Vetter20afbda2012-12-11 14:05:07 +01002505
2506 return 0;
2507}
2508
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002509static void valleyview_irq_uninstall(struct drm_device *dev)
2510{
2511 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2512 int pipe;
2513
2514 if (!dev_priv)
2515 return;
2516
Egbert Eichac4c16c2013-04-16 13:36:58 +02002517 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2518
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07002519 for_each_pipe(pipe)
2520 I915_WRITE(PIPESTAT(pipe), 0xffff);
2521
2522 I915_WRITE(HWSTAM, 0xffffffff);
2523 I915_WRITE(PORT_HOTPLUG_EN, 0);
2524 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2525 for_each_pipe(pipe)
2526 I915_WRITE(PIPESTAT(pipe), 0xffff);
2527 I915_WRITE(VLV_IIR, 0xffffffff);
2528 I915_WRITE(VLV_IMR, 0xffffffff);
2529 I915_WRITE(VLV_IER, 0x0);
2530 POSTING_READ(VLV_IER);
2531}
2532
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002533static void ironlake_irq_uninstall(struct drm_device *dev)
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002534{
2535 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes46979952011-04-07 13:53:55 -07002536
2537 if (!dev_priv)
2538 return;
2539
Egbert Eichac4c16c2013-04-16 13:36:58 +02002540 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2541
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002542 I915_WRITE(HWSTAM, 0xffffffff);
2543
2544 I915_WRITE(DEIMR, 0xffffffff);
2545 I915_WRITE(DEIER, 0x0);
2546 I915_WRITE(DEIIR, I915_READ(DEIIR));
Paulo Zanoni86642812013-04-12 17:57:57 -03002547 if (IS_GEN7(dev))
2548 I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002549
2550 I915_WRITE(GTIMR, 0xffffffff);
2551 I915_WRITE(GTIER, 0x0);
2552 I915_WRITE(GTIIR, I915_READ(GTIIR));
Keith Packard192aac1f2011-09-20 10:12:44 -07002553
Ben Widawskyab5c6082013-04-05 13:12:41 -07002554 if (HAS_PCH_NOP(dev))
2555 return;
2556
Keith Packard192aac1f2011-09-20 10:12:44 -07002557 I915_WRITE(SDEIMR, 0xffffffff);
2558 I915_WRITE(SDEIER, 0x0);
2559 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
Paulo Zanoni86642812013-04-12 17:57:57 -03002560 if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2561 I915_WRITE(SERR_INT, I915_READ(SERR_INT));
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002562}
2563
Chris Wilsonc2798b12012-04-22 21:13:57 +01002564static void i8xx_irq_preinstall(struct drm_device * dev)
2565{
2566 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2567 int pipe;
2568
2569 atomic_set(&dev_priv->irq_received, 0);
2570
2571 for_each_pipe(pipe)
2572 I915_WRITE(PIPESTAT(pipe), 0);
2573 I915_WRITE16(IMR, 0xffff);
2574 I915_WRITE16(IER, 0x0);
2575 POSTING_READ16(IER);
2576}
2577
2578static int i8xx_irq_postinstall(struct drm_device *dev)
2579{
2580 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2581
Chris Wilsonc2798b12012-04-22 21:13:57 +01002582 I915_WRITE16(EMR,
2583 ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2584
2585 /* Unmask the interrupts that we always want on. */
2586 dev_priv->irq_mask =
2587 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2588 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2589 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2590 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2591 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2592 I915_WRITE16(IMR, dev_priv->irq_mask);
2593
2594 I915_WRITE16(IER,
2595 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2596 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2597 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2598 I915_USER_INTERRUPT);
2599 POSTING_READ16(IER);
2600
2601 return 0;
2602}
2603
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002604/*
2605 * Returns true when a page flip has completed.
2606 */
2607static bool i8xx_handle_vblank(struct drm_device *dev,
2608 int pipe, u16 iir)
2609{
2610 drm_i915_private_t *dev_priv = dev->dev_private;
2611 u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
2612
2613 if (!drm_handle_vblank(dev, pipe))
2614 return false;
2615
2616 if ((iir & flip_pending) == 0)
2617 return false;
2618
2619 intel_prepare_page_flip(dev, pipe);
2620
2621 /* We detect FlipDone by looking for the change in PendingFlip from '1'
2622 * to '0' on the following vblank, i.e. IIR has the Pendingflip
2623 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
2624 * the flip is completed (no longer pending). Since this doesn't raise
2625 * an interrupt per se, we watch for the change at vblank.
2626 */
2627 if (I915_READ16(ISR) & flip_pending)
2628 return false;
2629
2630 intel_finish_page_flip(dev, pipe);
2631
2632 return true;
2633}
2634
Daniel Vetterff1f5252012-10-02 15:10:55 +02002635static irqreturn_t i8xx_irq_handler(int irq, void *arg)
Chris Wilsonc2798b12012-04-22 21:13:57 +01002636{
2637 struct drm_device *dev = (struct drm_device *) arg;
2638 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002639 u16 iir, new_iir;
2640 u32 pipe_stats[2];
2641 unsigned long irqflags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002642 int pipe;
2643 u16 flip_mask =
2644 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2645 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2646
2647 atomic_inc(&dev_priv->irq_received);
2648
2649 iir = I915_READ16(IIR);
2650 if (iir == 0)
2651 return IRQ_NONE;
2652
2653 while (iir & ~flip_mask) {
2654 /* Can't rely on pipestat interrupt bit in iir as it might
2655 * have been cleared after the pipestat interrupt was received.
2656 * It doesn't set the bit in iir again, but it still produces
2657 * interrupts (for non-MSI).
2658 */
2659 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2660 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2661 i915_handle_error(dev, false);
2662
2663 for_each_pipe(pipe) {
2664 int reg = PIPESTAT(pipe);
2665 pipe_stats[pipe] = I915_READ(reg);
2666
2667 /*
2668 * Clear the PIPE*STAT regs before the IIR
2669 */
2670 if (pipe_stats[pipe] & 0x8000ffff) {
2671 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2672 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2673 pipe_name(pipe));
2674 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002675 }
2676 }
2677 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2678
2679 I915_WRITE16(IIR, iir & ~flip_mask);
2680 new_iir = I915_READ16(IIR); /* Flush posted writes */
2681
Daniel Vetterd05c6172012-04-26 23:28:09 +02002682 i915_update_dri1_breadcrumb(dev);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002683
2684 if (iir & I915_USER_INTERRUPT)
2685 notify_ring(dev, &dev_priv->ring[RCS]);
2686
2687 if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002688 i8xx_handle_vblank(dev, 0, iir))
2689 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002690
2691 if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002692 i8xx_handle_vblank(dev, 1, iir))
2693 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
Chris Wilsonc2798b12012-04-22 21:13:57 +01002694
2695 iir = new_iir;
2696 }
2697
2698 return IRQ_HANDLED;
2699}
2700
2701static void i8xx_irq_uninstall(struct drm_device * dev)
2702{
2703 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2704 int pipe;
2705
Chris Wilsonc2798b12012-04-22 21:13:57 +01002706 for_each_pipe(pipe) {
2707 /* Clear enable bits; then clear status bits */
2708 I915_WRITE(PIPESTAT(pipe), 0);
2709 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2710 }
2711 I915_WRITE16(IMR, 0xffff);
2712 I915_WRITE16(IER, 0x0);
2713 I915_WRITE16(IIR, I915_READ16(IIR));
2714}
2715
Chris Wilsona266c7d2012-04-24 22:59:44 +01002716static void i915_irq_preinstall(struct drm_device * dev)
2717{
2718 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2719 int pipe;
2720
2721 atomic_set(&dev_priv->irq_received, 0);
2722
2723 if (I915_HAS_HOTPLUG(dev)) {
2724 I915_WRITE(PORT_HOTPLUG_EN, 0);
2725 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2726 }
2727
Chris Wilson00d98eb2012-04-24 22:59:48 +01002728 I915_WRITE16(HWSTAM, 0xeffe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002729 for_each_pipe(pipe)
2730 I915_WRITE(PIPESTAT(pipe), 0);
2731 I915_WRITE(IMR, 0xffffffff);
2732 I915_WRITE(IER, 0x0);
2733 POSTING_READ(IER);
2734}
2735
2736static int i915_irq_postinstall(struct drm_device *dev)
2737{
2738 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson38bde182012-04-24 22:59:50 +01002739 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002740
Chris Wilson38bde182012-04-24 22:59:50 +01002741 I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2742
2743 /* Unmask the interrupts that we always want on. */
2744 dev_priv->irq_mask =
2745 ~(I915_ASLE_INTERRUPT |
2746 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2747 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2748 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2749 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2750 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2751
2752 enable_mask =
2753 I915_ASLE_INTERRUPT |
2754 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2755 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2756 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2757 I915_USER_INTERRUPT;
2758
Chris Wilsona266c7d2012-04-24 22:59:44 +01002759 if (I915_HAS_HOTPLUG(dev)) {
Daniel Vetter20afbda2012-12-11 14:05:07 +01002760 I915_WRITE(PORT_HOTPLUG_EN, 0);
2761 POSTING_READ(PORT_HOTPLUG_EN);
2762
Chris Wilsona266c7d2012-04-24 22:59:44 +01002763 /* Enable in IER... */
2764 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2765 /* and unmask in IMR */
2766 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2767 }
2768
Chris Wilsona266c7d2012-04-24 22:59:44 +01002769 I915_WRITE(IMR, dev_priv->irq_mask);
2770 I915_WRITE(IER, enable_mask);
2771 POSTING_READ(IER);
2772
Jani Nikulaf49e38d2013-04-29 13:02:54 +03002773 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01002774
2775 return 0;
2776}
2777
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002778/*
2779 * Returns true when a page flip has completed.
2780 */
2781static bool i915_handle_vblank(struct drm_device *dev,
2782 int plane, int pipe, u32 iir)
2783{
2784 drm_i915_private_t *dev_priv = dev->dev_private;
2785 u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
2786
2787 if (!drm_handle_vblank(dev, pipe))
2788 return false;
2789
2790 if ((iir & flip_pending) == 0)
2791 return false;
2792
2793 intel_prepare_page_flip(dev, plane);
2794
2795 /* We detect FlipDone by looking for the change in PendingFlip from '1'
2796 * to '0' on the following vblank, i.e. IIR has the Pendingflip
2797 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
2798 * the flip is completed (no longer pending). Since this doesn't raise
2799 * an interrupt per se, we watch for the change at vblank.
2800 */
2801 if (I915_READ(ISR) & flip_pending)
2802 return false;
2803
2804 intel_finish_page_flip(dev, pipe);
2805
2806 return true;
2807}
2808
Daniel Vetterff1f5252012-10-02 15:10:55 +02002809static irqreturn_t i915_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01002810{
2811 struct drm_device *dev = (struct drm_device *) arg;
2812 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson8291ee92012-04-24 22:59:47 +01002813 u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01002814 unsigned long irqflags;
Chris Wilson38bde182012-04-24 22:59:50 +01002815 u32 flip_mask =
2816 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2817 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilson38bde182012-04-24 22:59:50 +01002818 int pipe, ret = IRQ_NONE;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002819
2820 atomic_inc(&dev_priv->irq_received);
2821
2822 iir = I915_READ(IIR);
Chris Wilson38bde182012-04-24 22:59:50 +01002823 do {
2824 bool irq_received = (iir & ~flip_mask) != 0;
Chris Wilson8291ee92012-04-24 22:59:47 +01002825 bool blc_event = false;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002826
2827 /* Can't rely on pipestat interrupt bit in iir as it might
2828 * have been cleared after the pipestat interrupt was received.
2829 * It doesn't set the bit in iir again, but it still produces
2830 * interrupts (for non-MSI).
2831 */
2832 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2833 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2834 i915_handle_error(dev, false);
2835
2836 for_each_pipe(pipe) {
2837 int reg = PIPESTAT(pipe);
2838 pipe_stats[pipe] = I915_READ(reg);
2839
Chris Wilson38bde182012-04-24 22:59:50 +01002840 /* Clear the PIPE*STAT regs before the IIR */
Chris Wilsona266c7d2012-04-24 22:59:44 +01002841 if (pipe_stats[pipe] & 0x8000ffff) {
2842 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2843 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2844 pipe_name(pipe));
2845 I915_WRITE(reg, pipe_stats[pipe]);
Chris Wilson38bde182012-04-24 22:59:50 +01002846 irq_received = true;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002847 }
2848 }
2849 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2850
2851 if (!irq_received)
2852 break;
2853
Chris Wilsona266c7d2012-04-24 22:59:44 +01002854 /* Consume port. Then clear IIR or we'll miss events */
2855 if ((I915_HAS_HOTPLUG(dev)) &&
2856 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2857 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
Egbert Eichb543fb02013-04-16 13:36:54 +02002858 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002859
2860 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2861 hotplug_status);
Daniel Vetter91d131d2013-06-27 17:52:14 +02002862
2863 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
2864
Chris Wilsona266c7d2012-04-24 22:59:44 +01002865 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
Chris Wilson38bde182012-04-24 22:59:50 +01002866 POSTING_READ(PORT_HOTPLUG_STAT);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002867 }
2868
Chris Wilson38bde182012-04-24 22:59:50 +01002869 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002870 new_iir = I915_READ(IIR); /* Flush posted writes */
2871
Chris Wilsona266c7d2012-04-24 22:59:44 +01002872 if (iir & I915_USER_INTERRUPT)
2873 notify_ring(dev, &dev_priv->ring[RCS]);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002874
Chris Wilsona266c7d2012-04-24 22:59:44 +01002875 for_each_pipe(pipe) {
Chris Wilson38bde182012-04-24 22:59:50 +01002876 int plane = pipe;
2877 if (IS_MOBILE(dev))
2878 plane = !plane;
Ville Syrjälä5e2032d2013-02-19 15:16:38 +02002879
Ville Syrjälä90a72f82013-02-19 23:16:44 +02002880 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2881 i915_handle_vblank(dev, plane, pipe, iir))
2882 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002883
2884 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2885 blc_event = true;
2886 }
2887
Chris Wilsona266c7d2012-04-24 22:59:44 +01002888 if (blc_event || (iir & I915_ASLE_INTERRUPT))
2889 intel_opregion_asle_intr(dev);
2890
2891 /* With MSI, interrupts are only generated when iir
2892 * transitions from zero to nonzero. If another bit got
2893 * set while we were handling the existing iir bits, then
2894 * we would never get another interrupt.
2895 *
2896 * This is fine on non-MSI as well, as if we hit this path
2897 * we avoid exiting the interrupt handler only to generate
2898 * another one.
2899 *
2900 * Note that for MSI this could cause a stray interrupt report
2901 * if an interrupt landed in the time between writing IIR and
2902 * the posting read. This should be rare enough to never
2903 * trigger the 99% of 100,000 interrupts test for disabling
2904 * stray interrupts.
2905 */
Chris Wilson38bde182012-04-24 22:59:50 +01002906 ret = IRQ_HANDLED;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002907 iir = new_iir;
Chris Wilson38bde182012-04-24 22:59:50 +01002908 } while (iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002909
Daniel Vetterd05c6172012-04-26 23:28:09 +02002910 i915_update_dri1_breadcrumb(dev);
Chris Wilson8291ee92012-04-24 22:59:47 +01002911
Chris Wilsona266c7d2012-04-24 22:59:44 +01002912 return ret;
2913}
2914
2915static void i915_irq_uninstall(struct drm_device * dev)
2916{
2917 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2918 int pipe;
2919
Egbert Eichac4c16c2013-04-16 13:36:58 +02002920 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2921
Chris Wilsona266c7d2012-04-24 22:59:44 +01002922 if (I915_HAS_HOTPLUG(dev)) {
2923 I915_WRITE(PORT_HOTPLUG_EN, 0);
2924 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2925 }
2926
Chris Wilson00d98eb2012-04-24 22:59:48 +01002927 I915_WRITE16(HWSTAM, 0xffff);
Chris Wilson55b39752012-04-24 22:59:49 +01002928 for_each_pipe(pipe) {
2929 /* Clear enable bits; then clear status bits */
Chris Wilsona266c7d2012-04-24 22:59:44 +01002930 I915_WRITE(PIPESTAT(pipe), 0);
Chris Wilson55b39752012-04-24 22:59:49 +01002931 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2932 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01002933 I915_WRITE(IMR, 0xffffffff);
2934 I915_WRITE(IER, 0x0);
2935
Chris Wilsona266c7d2012-04-24 22:59:44 +01002936 I915_WRITE(IIR, I915_READ(IIR));
2937}
2938
2939static void i965_irq_preinstall(struct drm_device * dev)
2940{
2941 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2942 int pipe;
2943
2944 atomic_set(&dev_priv->irq_received, 0);
2945
Chris Wilsonadca4732012-05-11 18:01:31 +01002946 I915_WRITE(PORT_HOTPLUG_EN, 0);
2947 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01002948
2949 I915_WRITE(HWSTAM, 0xeffe);
2950 for_each_pipe(pipe)
2951 I915_WRITE(PIPESTAT(pipe), 0);
2952 I915_WRITE(IMR, 0xffffffff);
2953 I915_WRITE(IER, 0x0);
2954 POSTING_READ(IER);
2955}
2956
2957static int i965_irq_postinstall(struct drm_device *dev)
2958{
2959 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002960 u32 enable_mask;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002961 u32 error_mask;
Daniel Vetterb79480b2013-06-27 17:52:10 +02002962 unsigned long irqflags;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002963
Chris Wilsona266c7d2012-04-24 22:59:44 +01002964 /* Unmask the interrupts that we always want on. */
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002965 dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
Chris Wilsonadca4732012-05-11 18:01:31 +01002966 I915_DISPLAY_PORT_INTERRUPT |
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002967 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2968 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2969 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2970 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2971 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2972
2973 enable_mask = ~dev_priv->irq_mask;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02002974 enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2975 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
Chris Wilsonbbba0a92012-04-24 22:59:51 +01002976 enable_mask |= I915_USER_INTERRUPT;
2977
2978 if (IS_G4X(dev))
2979 enable_mask |= I915_BSD_USER_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01002980
Daniel Vetterb79480b2013-06-27 17:52:10 +02002981 /* Interrupt setup is already guaranteed to be single-threaded, this is
2982 * just to make the assert_spin_locked check happy. */
2983 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter515ac2b2012-12-01 13:53:44 +01002984 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
Daniel Vetterb79480b2013-06-27 17:52:10 +02002985 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Chris Wilsona266c7d2012-04-24 22:59:44 +01002986
Chris Wilsona266c7d2012-04-24 22:59:44 +01002987 /*
2988 * Enable some error detection, note the instruction error mask
2989 * bit is reserved, so we leave it masked.
2990 */
2991 if (IS_G4X(dev)) {
2992 error_mask = ~(GM45_ERROR_PAGE_TABLE |
2993 GM45_ERROR_MEM_PRIV |
2994 GM45_ERROR_CP_PRIV |
2995 I915_ERROR_MEMORY_REFRESH);
2996 } else {
2997 error_mask = ~(I915_ERROR_PAGE_TABLE |
2998 I915_ERROR_MEMORY_REFRESH);
2999 }
3000 I915_WRITE(EMR, error_mask);
3001
3002 I915_WRITE(IMR, dev_priv->irq_mask);
3003 I915_WRITE(IER, enable_mask);
3004 POSTING_READ(IER);
3005
Daniel Vetter20afbda2012-12-11 14:05:07 +01003006 I915_WRITE(PORT_HOTPLUG_EN, 0);
3007 POSTING_READ(PORT_HOTPLUG_EN);
3008
Jani Nikulaf49e38d2013-04-29 13:02:54 +03003009 i915_enable_asle_pipestat(dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003010
3011 return 0;
3012}
3013
Egbert Eichbac56d52013-02-25 12:06:51 -05003014static void i915_hpd_irq_setup(struct drm_device *dev)
Daniel Vetter20afbda2012-12-11 14:05:07 +01003015{
3016 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Egbert Eiche5868a32013-02-28 04:17:12 -05003017 struct drm_mode_config *mode_config = &dev->mode_config;
Egbert Eichcd569ae2013-04-16 13:36:57 +02003018 struct intel_encoder *intel_encoder;
Daniel Vetter20afbda2012-12-11 14:05:07 +01003019 u32 hotplug_en;
3020
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02003021 assert_spin_locked(&dev_priv->irq_lock);
3022
Egbert Eichbac56d52013-02-25 12:06:51 -05003023 if (I915_HAS_HOTPLUG(dev)) {
3024 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3025 hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3026 /* Note HDMI and DP share hotplug bits */
Egbert Eiche5868a32013-02-28 04:17:12 -05003027 /* enable bits are the same for all generations */
Egbert Eichcd569ae2013-04-16 13:36:57 +02003028 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3029 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3030 hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
Egbert Eichbac56d52013-02-25 12:06:51 -05003031 /* Programming the CRT detection parameters tends
3032 to generate a spurious hotplug event about three
3033 seconds later. So just do it once.
3034 */
3035 if (IS_G4X(dev))
3036 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
Daniel Vetter85fc95b2013-03-27 15:47:11 +01003037 hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
Egbert Eichbac56d52013-02-25 12:06:51 -05003038 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003039
Egbert Eichbac56d52013-02-25 12:06:51 -05003040 /* Ignore TV since it's buggy */
3041 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3042 }
Chris Wilsona266c7d2012-04-24 22:59:44 +01003043}
3044
Daniel Vetterff1f5252012-10-02 15:10:55 +02003045static irqreturn_t i965_irq_handler(int irq, void *arg)
Chris Wilsona266c7d2012-04-24 22:59:44 +01003046{
3047 struct drm_device *dev = (struct drm_device *) arg;
3048 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003049 u32 iir, new_iir;
3050 u32 pipe_stats[I915_MAX_PIPES];
Chris Wilsona266c7d2012-04-24 22:59:44 +01003051 unsigned long irqflags;
3052 int irq_received;
3053 int ret = IRQ_NONE, pipe;
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003054 u32 flip_mask =
3055 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3056 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003057
3058 atomic_inc(&dev_priv->irq_received);
3059
3060 iir = I915_READ(IIR);
3061
Chris Wilsona266c7d2012-04-24 22:59:44 +01003062 for (;;) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01003063 bool blc_event = false;
3064
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003065 irq_received = (iir & ~flip_mask) != 0;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003066
3067 /* Can't rely on pipestat interrupt bit in iir as it might
3068 * have been cleared after the pipestat interrupt was received.
3069 * It doesn't set the bit in iir again, but it still produces
3070 * interrupts (for non-MSI).
3071 */
3072 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3073 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3074 i915_handle_error(dev, false);
3075
3076 for_each_pipe(pipe) {
3077 int reg = PIPESTAT(pipe);
3078 pipe_stats[pipe] = I915_READ(reg);
3079
3080 /*
3081 * Clear the PIPE*STAT regs before the IIR
3082 */
3083 if (pipe_stats[pipe] & 0x8000ffff) {
3084 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
3085 DRM_DEBUG_DRIVER("pipe %c underrun\n",
3086 pipe_name(pipe));
3087 I915_WRITE(reg, pipe_stats[pipe]);
3088 irq_received = 1;
3089 }
3090 }
3091 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3092
3093 if (!irq_received)
3094 break;
3095
3096 ret = IRQ_HANDLED;
3097
3098 /* Consume port. Then clear IIR or we'll miss events */
Chris Wilsonadca4732012-05-11 18:01:31 +01003099 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003100 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
Egbert Eichb543fb02013-04-16 13:36:54 +02003101 u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3102 HOTPLUG_INT_STATUS_G4X :
Daniel Vetter4f7fd702013-06-24 21:33:28 +02003103 HOTPLUG_INT_STATUS_I915);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003104
3105 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
3106 hotplug_status);
Daniel Vetter91d131d2013-06-27 17:52:14 +02003107
3108 intel_hpd_irq_handler(dev, hotplug_trigger,
3109 IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
3110
Chris Wilsona266c7d2012-04-24 22:59:44 +01003111 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3112 I915_READ(PORT_HOTPLUG_STAT);
3113 }
3114
Ville Syrjälä21ad8332013-02-19 15:16:39 +02003115 I915_WRITE(IIR, iir & ~flip_mask);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003116 new_iir = I915_READ(IIR); /* Flush posted writes */
3117
Chris Wilsona266c7d2012-04-24 22:59:44 +01003118 if (iir & I915_USER_INTERRUPT)
3119 notify_ring(dev, &dev_priv->ring[RCS]);
3120 if (iir & I915_BSD_USER_INTERRUPT)
3121 notify_ring(dev, &dev_priv->ring[VCS]);
3122
Chris Wilsona266c7d2012-04-24 22:59:44 +01003123 for_each_pipe(pipe) {
Chris Wilson2c8ba292012-04-24 22:59:46 +01003124 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
Ville Syrjälä90a72f82013-02-19 23:16:44 +02003125 i915_handle_vblank(dev, pipe, pipe, iir))
3126 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
Chris Wilsona266c7d2012-04-24 22:59:44 +01003127
3128 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3129 blc_event = true;
3130 }
3131
3132
3133 if (blc_event || (iir & I915_ASLE_INTERRUPT))
3134 intel_opregion_asle_intr(dev);
3135
Daniel Vetter515ac2b2012-12-01 13:53:44 +01003136 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3137 gmbus_irq_handler(dev);
3138
Chris Wilsona266c7d2012-04-24 22:59:44 +01003139 /* With MSI, interrupts are only generated when iir
3140 * transitions from zero to nonzero. If another bit got
3141 * set while we were handling the existing iir bits, then
3142 * we would never get another interrupt.
3143 *
3144 * This is fine on non-MSI as well, as if we hit this path
3145 * we avoid exiting the interrupt handler only to generate
3146 * another one.
3147 *
3148 * Note that for MSI this could cause a stray interrupt report
3149 * if an interrupt landed in the time between writing IIR and
3150 * the posting read. This should be rare enough to never
3151 * trigger the 99% of 100,000 interrupts test for disabling
3152 * stray interrupts.
3153 */
3154 iir = new_iir;
3155 }
3156
Daniel Vetterd05c6172012-04-26 23:28:09 +02003157 i915_update_dri1_breadcrumb(dev);
Chris Wilson2c8ba292012-04-24 22:59:46 +01003158
Chris Wilsona266c7d2012-04-24 22:59:44 +01003159 return ret;
3160}
3161
3162static void i965_irq_uninstall(struct drm_device * dev)
3163{
3164 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3165 int pipe;
3166
3167 if (!dev_priv)
3168 return;
3169
Egbert Eichac4c16c2013-04-16 13:36:58 +02003170 del_timer_sync(&dev_priv->hotplug_reenable_timer);
3171
Chris Wilsonadca4732012-05-11 18:01:31 +01003172 I915_WRITE(PORT_HOTPLUG_EN, 0);
3173 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
Chris Wilsona266c7d2012-04-24 22:59:44 +01003174
3175 I915_WRITE(HWSTAM, 0xffffffff);
3176 for_each_pipe(pipe)
3177 I915_WRITE(PIPESTAT(pipe), 0);
3178 I915_WRITE(IMR, 0xffffffff);
3179 I915_WRITE(IER, 0x0);
3180
3181 for_each_pipe(pipe)
3182 I915_WRITE(PIPESTAT(pipe),
3183 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3184 I915_WRITE(IIR, I915_READ(IIR));
3185}
3186
Egbert Eichac4c16c2013-04-16 13:36:58 +02003187static void i915_reenable_hotplug_timer_func(unsigned long data)
3188{
3189 drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3190 struct drm_device *dev = dev_priv->dev;
3191 struct drm_mode_config *mode_config = &dev->mode_config;
3192 unsigned long irqflags;
3193 int i;
3194
3195 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3196 for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3197 struct drm_connector *connector;
3198
3199 if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3200 continue;
3201
3202 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3203
3204 list_for_each_entry(connector, &mode_config->connector_list, head) {
3205 struct intel_connector *intel_connector = to_intel_connector(connector);
3206
3207 if (intel_connector->encoder->hpd_pin == i) {
3208 if (connector->polled != intel_connector->polled)
3209 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3210 drm_get_connector_name(connector));
3211 connector->polled = intel_connector->polled;
3212 if (!connector->polled)
3213 connector->polled = DRM_CONNECTOR_POLL_HPD;
3214 }
3215 }
3216 }
3217 if (dev_priv->display.hpd_irq_setup)
3218 dev_priv->display.hpd_irq_setup(dev);
3219 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3220}
3221
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003222void intel_irq_init(struct drm_device *dev)
3223{
Chris Wilson8b2e3262012-04-24 22:59:41 +01003224 struct drm_i915_private *dev_priv = dev->dev_private;
3225
3226 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Daniel Vetter99584db2012-11-14 17:14:04 +01003227 INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003228 INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01003229 INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
Chris Wilson8b2e3262012-04-24 22:59:41 +01003230
Daniel Vetter99584db2012-11-14 17:14:04 +01003231 setup_timer(&dev_priv->gpu_error.hangcheck_timer,
3232 i915_hangcheck_elapsed,
Daniel Vetter61bac782012-12-01 21:03:21 +01003233 (unsigned long) dev);
Egbert Eichac4c16c2013-04-16 13:36:58 +02003234 setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3235 (unsigned long) dev_priv);
Daniel Vetter61bac782012-12-01 21:03:21 +01003236
Tomas Janousek97a19a22012-12-08 13:48:13 +01003237 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01003238
Eugeni Dodonov7d4e1462012-05-09 15:37:09 -03003239 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003240 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3241 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Ville Syrjälä391f75e2013-09-25 19:55:26 +03003242 } else {
3243 dev->driver->get_vblank_counter = i915_get_vblank_counter;
3244 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003245 }
3246
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +03003247 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Keith Packardc3613de2011-08-12 17:05:54 -07003248 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
Ville Syrjäläc2baf4b2013-09-23 14:48:50 +03003249 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3250 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003251
Jesse Barnes7e231dbe2012-03-28 13:39:38 -07003252 if (IS_VALLEYVIEW(dev)) {
3253 dev->driver->irq_handler = valleyview_irq_handler;
3254 dev->driver->irq_preinstall = valleyview_irq_preinstall;
3255 dev->driver->irq_postinstall = valleyview_irq_postinstall;
3256 dev->driver->irq_uninstall = valleyview_irq_uninstall;
3257 dev->driver->enable_vblank = valleyview_enable_vblank;
3258 dev->driver->disable_vblank = valleyview_disable_vblank;
Egbert Eichfa00abe2013-02-25 12:06:48 -05003259 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003260 } else if (HAS_PCH_SPLIT(dev)) {
3261 dev->driver->irq_handler = ironlake_irq_handler;
3262 dev->driver->irq_preinstall = ironlake_irq_preinstall;
3263 dev->driver->irq_postinstall = ironlake_irq_postinstall;
3264 dev->driver->irq_uninstall = ironlake_irq_uninstall;
3265 dev->driver->enable_vblank = ironlake_enable_vblank;
3266 dev->driver->disable_vblank = ironlake_disable_vblank;
Daniel Vetter82a28bc2013-03-27 15:55:01 +01003267 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003268 } else {
Chris Wilsonc2798b12012-04-22 21:13:57 +01003269 if (INTEL_INFO(dev)->gen == 2) {
3270 dev->driver->irq_preinstall = i8xx_irq_preinstall;
3271 dev->driver->irq_postinstall = i8xx_irq_postinstall;
3272 dev->driver->irq_handler = i8xx_irq_handler;
3273 dev->driver->irq_uninstall = i8xx_irq_uninstall;
Chris Wilsona266c7d2012-04-24 22:59:44 +01003274 } else if (INTEL_INFO(dev)->gen == 3) {
3275 dev->driver->irq_preinstall = i915_irq_preinstall;
3276 dev->driver->irq_postinstall = i915_irq_postinstall;
3277 dev->driver->irq_uninstall = i915_irq_uninstall;
3278 dev->driver->irq_handler = i915_irq_handler;
Daniel Vetter20afbda2012-12-11 14:05:07 +01003279 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003280 } else {
Chris Wilsona266c7d2012-04-24 22:59:44 +01003281 dev->driver->irq_preinstall = i965_irq_preinstall;
3282 dev->driver->irq_postinstall = i965_irq_postinstall;
3283 dev->driver->irq_uninstall = i965_irq_uninstall;
3284 dev->driver->irq_handler = i965_irq_handler;
Egbert Eichbac56d52013-02-25 12:06:51 -05003285 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
Chris Wilsonc2798b12012-04-22 21:13:57 +01003286 }
Jesse Barnesf71d4af2011-06-28 13:00:41 -07003287 dev->driver->enable_vblank = i915_enable_vblank;
3288 dev->driver->disable_vblank = i915_disable_vblank;
3289 }
3290}
Daniel Vetter20afbda2012-12-11 14:05:07 +01003291
3292void intel_hpd_init(struct drm_device *dev)
3293{
3294 struct drm_i915_private *dev_priv = dev->dev_private;
Egbert Eich821450c2013-04-16 13:36:55 +02003295 struct drm_mode_config *mode_config = &dev->mode_config;
3296 struct drm_connector *connector;
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02003297 unsigned long irqflags;
Egbert Eich821450c2013-04-16 13:36:55 +02003298 int i;
Daniel Vetter20afbda2012-12-11 14:05:07 +01003299
Egbert Eich821450c2013-04-16 13:36:55 +02003300 for (i = 1; i < HPD_NUM_PINS; i++) {
3301 dev_priv->hpd_stats[i].hpd_cnt = 0;
3302 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3303 }
3304 list_for_each_entry(connector, &mode_config->connector_list, head) {
3305 struct intel_connector *intel_connector = to_intel_connector(connector);
3306 connector->polled = intel_connector->polled;
3307 if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3308 connector->polled = DRM_CONNECTOR_POLL_HPD;
3309 }
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02003310
3311 /* Interrupt setup is already guaranteed to be single-threaded, this is
3312 * just to make the assert_spin_locked checks happy. */
3313 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003314 if (dev_priv->display.hpd_irq_setup)
3315 dev_priv->display.hpd_irq_setup(dev);
Daniel Vetterb5ea2d52013-06-27 17:52:15 +02003316 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Daniel Vetter20afbda2012-12-11 14:05:07 +01003317}
Paulo Zanonic67a4702013-08-19 13:18:09 -03003318
3319/* Disable interrupts so we can allow Package C8+. */
3320void hsw_pc8_disable_interrupts(struct drm_device *dev)
3321{
3322 struct drm_i915_private *dev_priv = dev->dev_private;
3323 unsigned long irqflags;
3324
3325 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3326
3327 dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3328 dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3329 dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3330 dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3331 dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3332
3333 ironlake_disable_display_irq(dev_priv, ~DE_PCH_EVENT_IVB);
3334 ibx_disable_display_interrupt(dev_priv, ~SDE_HOTPLUG_MASK_CPT);
3335 ilk_disable_gt_irq(dev_priv, 0xffffffff);
3336 snb_disable_pm_irq(dev_priv, 0xffffffff);
3337
3338 dev_priv->pc8.irqs_disabled = true;
3339
3340 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3341}
3342
3343/* Restore interrupts so we can recover from Package C8+. */
3344void hsw_pc8_restore_interrupts(struct drm_device *dev)
3345{
3346 struct drm_i915_private *dev_priv = dev->dev_private;
3347 unsigned long irqflags;
3348 uint32_t val, expected;
3349
3350 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3351
3352 val = I915_READ(DEIMR);
3353 expected = ~DE_PCH_EVENT_IVB;
3354 WARN(val != expected, "DEIMR is 0x%08x, not 0x%08x\n", val, expected);
3355
3356 val = I915_READ(SDEIMR) & ~SDE_HOTPLUG_MASK_CPT;
3357 expected = ~SDE_HOTPLUG_MASK_CPT;
3358 WARN(val != expected, "SDEIMR non-HPD bits are 0x%08x, not 0x%08x\n",
3359 val, expected);
3360
3361 val = I915_READ(GTIMR);
3362 expected = 0xffffffff;
3363 WARN(val != expected, "GTIMR is 0x%08x, not 0x%08x\n", val, expected);
3364
3365 val = I915_READ(GEN6_PMIMR);
3366 expected = 0xffffffff;
3367 WARN(val != expected, "GEN6_PMIMR is 0x%08x, not 0x%08x\n", val,
3368 expected);
3369
3370 dev_priv->pc8.irqs_disabled = false;
3371
3372 ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
3373 ibx_enable_display_interrupt(dev_priv,
3374 ~dev_priv->pc8.regsave.sdeimr &
3375 ~SDE_HOTPLUG_MASK_CPT);
3376 ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3377 snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3378 I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3379
3380 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3381}