drm/i915: POSTING_READ(DEIER) on ivybridge_irq_handler
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_irq.c
1 /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2 */
3 /*
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
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 *
27 */
28
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31 #include <linux/sysrq.h>
32 #include <linux/slab.h>
33 #include <drm/drmP.h>
34 #include <drm/i915_drm.h>
35 #include "i915_drv.h"
36 #include "i915_trace.h"
37 #include "intel_drv.h"
38
39 static 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
47 static const u32 hpd_cpt[] = {
48 [HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
49 [HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
50 [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
55 static 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
64 static 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
73 static 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
82 /* For display hotplug interrupt */
83 static void
84 ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
85 {
86 assert_spin_locked(&dev_priv->irq_lock);
87
88 if ((dev_priv->irq_mask & mask) != 0) {
89 dev_priv->irq_mask &= ~mask;
90 I915_WRITE(DEIMR, dev_priv->irq_mask);
91 POSTING_READ(DEIMR);
92 }
93 }
94
95 static void
96 ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
97 {
98 assert_spin_locked(&dev_priv->irq_lock);
99
100 if ((dev_priv->irq_mask & mask) != mask) {
101 dev_priv->irq_mask |= mask;
102 I915_WRITE(DEIMR, dev_priv->irq_mask);
103 POSTING_READ(DEIMR);
104 }
105 }
106
107 static bool ivb_can_enable_err_int(struct drm_device *dev)
108 {
109 struct drm_i915_private *dev_priv = dev->dev_private;
110 struct intel_crtc *crtc;
111 enum pipe pipe;
112
113 assert_spin_locked(&dev_priv->irq_lock);
114
115 for_each_pipe(pipe) {
116 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
117
118 if (crtc->cpu_fifo_underrun_disabled)
119 return false;
120 }
121
122 return true;
123 }
124
125 static bool cpt_can_enable_serr_int(struct drm_device *dev)
126 {
127 struct drm_i915_private *dev_priv = dev->dev_private;
128 enum pipe pipe;
129 struct intel_crtc *crtc;
130
131 assert_spin_locked(&dev_priv->irq_lock);
132
133 for_each_pipe(pipe) {
134 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
135
136 if (crtc->pch_fifo_underrun_disabled)
137 return false;
138 }
139
140 return true;
141 }
142
143 static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
144 enum pipe pipe, bool enable)
145 {
146 struct drm_i915_private *dev_priv = dev->dev_private;
147 uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
148 DE_PIPEB_FIFO_UNDERRUN;
149
150 if (enable)
151 ironlake_enable_display_irq(dev_priv, bit);
152 else
153 ironlake_disable_display_irq(dev_priv, bit);
154 }
155
156 static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
157 enum pipe pipe, bool enable)
158 {
159 struct drm_i915_private *dev_priv = dev->dev_private;
160 if (enable) {
161 I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
162
163 if (!ivb_can_enable_err_int(dev))
164 return;
165
166 ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
167 } else {
168 bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
169
170 /* Change the state _after_ we've read out the current one. */
171 ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
172
173 if (!was_enabled &&
174 (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
175 DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
176 pipe_name(pipe));
177 }
178 }
179 }
180
181 /**
182 * ibx_display_interrupt_update - update SDEIMR
183 * @dev_priv: driver private
184 * @interrupt_mask: mask of interrupt bits to update
185 * @enabled_irq_mask: mask of interrupt bits to enable
186 */
187 static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
188 uint32_t interrupt_mask,
189 uint32_t enabled_irq_mask)
190 {
191 uint32_t sdeimr = I915_READ(SDEIMR);
192 sdeimr &= ~interrupt_mask;
193 sdeimr |= (~enabled_irq_mask & interrupt_mask);
194
195 assert_spin_locked(&dev_priv->irq_lock);
196
197 I915_WRITE(SDEIMR, sdeimr);
198 POSTING_READ(SDEIMR);
199 }
200 #define ibx_enable_display_interrupt(dev_priv, bits) \
201 ibx_display_interrupt_update((dev_priv), (bits), (bits))
202 #define ibx_disable_display_interrupt(dev_priv, bits) \
203 ibx_display_interrupt_update((dev_priv), (bits), 0)
204
205 static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
206 enum transcoder pch_transcoder,
207 bool enable)
208 {
209 struct drm_i915_private *dev_priv = dev->dev_private;
210 uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
211 SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
212
213 if (enable)
214 ibx_enable_display_interrupt(dev_priv, bit);
215 else
216 ibx_disable_display_interrupt(dev_priv, bit);
217 }
218
219 static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
220 enum transcoder pch_transcoder,
221 bool enable)
222 {
223 struct drm_i915_private *dev_priv = dev->dev_private;
224
225 if (enable) {
226 I915_WRITE(SERR_INT,
227 SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
228
229 if (!cpt_can_enable_serr_int(dev))
230 return;
231
232 ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
233 } else {
234 uint32_t tmp = I915_READ(SERR_INT);
235 bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
236
237 /* Change the state _after_ we've read out the current one. */
238 ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
239
240 if (!was_enabled &&
241 (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
242 DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
243 transcoder_name(pch_transcoder));
244 }
245 }
246 }
247
248 /**
249 * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
250 * @dev: drm device
251 * @pipe: pipe
252 * @enable: true if we want to report FIFO underrun errors, false otherwise
253 *
254 * This function makes us disable or enable CPU fifo underruns for a specific
255 * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
256 * reporting for one pipe may also disable all the other CPU error interruts for
257 * the other pipes, due to the fact that there's just one interrupt mask/enable
258 * bit for all the pipes.
259 *
260 * Returns the previous state of underrun reporting.
261 */
262 bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
263 enum pipe pipe, bool enable)
264 {
265 struct drm_i915_private *dev_priv = dev->dev_private;
266 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
268 unsigned long flags;
269 bool ret;
270
271 spin_lock_irqsave(&dev_priv->irq_lock, flags);
272
273 ret = !intel_crtc->cpu_fifo_underrun_disabled;
274
275 if (enable == ret)
276 goto done;
277
278 intel_crtc->cpu_fifo_underrun_disabled = !enable;
279
280 if (IS_GEN5(dev) || IS_GEN6(dev))
281 ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
282 else if (IS_GEN7(dev))
283 ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
284
285 done:
286 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
287 return ret;
288 }
289
290 /**
291 * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
292 * @dev: drm device
293 * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
294 * @enable: true if we want to report FIFO underrun errors, false otherwise
295 *
296 * This function makes us disable or enable PCH fifo underruns for a specific
297 * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
298 * underrun reporting for one transcoder may also disable all the other PCH
299 * error interruts for the other transcoders, due to the fact that there's just
300 * one interrupt mask/enable bit for all the transcoders.
301 *
302 * Returns the previous state of underrun reporting.
303 */
304 bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
305 enum transcoder pch_transcoder,
306 bool enable)
307 {
308 struct drm_i915_private *dev_priv = dev->dev_private;
309 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
310 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
311 unsigned long flags;
312 bool ret;
313
314 /*
315 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
316 * has only one pch transcoder A that all pipes can use. To avoid racy
317 * pch transcoder -> pipe lookups from interrupt code simply store the
318 * underrun statistics in crtc A. Since we never expose this anywhere
319 * nor use it outside of the fifo underrun code here using the "wrong"
320 * crtc on LPT won't cause issues.
321 */
322
323 spin_lock_irqsave(&dev_priv->irq_lock, flags);
324
325 ret = !intel_crtc->pch_fifo_underrun_disabled;
326
327 if (enable == ret)
328 goto done;
329
330 intel_crtc->pch_fifo_underrun_disabled = !enable;
331
332 if (HAS_PCH_IBX(dev))
333 ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
334 else
335 cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
336
337 done:
338 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
339 return ret;
340 }
341
342
343 void
344 i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
345 {
346 u32 reg = PIPESTAT(pipe);
347 u32 pipestat = I915_READ(reg) & 0x7fff0000;
348
349 assert_spin_locked(&dev_priv->irq_lock);
350
351 if ((pipestat & mask) == mask)
352 return;
353
354 /* Enable the interrupt, clear any pending status */
355 pipestat |= mask | (mask >> 16);
356 I915_WRITE(reg, pipestat);
357 POSTING_READ(reg);
358 }
359
360 void
361 i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
362 {
363 u32 reg = PIPESTAT(pipe);
364 u32 pipestat = I915_READ(reg) & 0x7fff0000;
365
366 assert_spin_locked(&dev_priv->irq_lock);
367
368 if ((pipestat & mask) == 0)
369 return;
370
371 pipestat &= ~mask;
372 I915_WRITE(reg, pipestat);
373 POSTING_READ(reg);
374 }
375
376 /**
377 * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
378 */
379 static void i915_enable_asle_pipestat(struct drm_device *dev)
380 {
381 drm_i915_private_t *dev_priv = dev->dev_private;
382 unsigned long irqflags;
383
384 if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
385 return;
386
387 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
388
389 i915_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
390 if (INTEL_INFO(dev)->gen >= 4)
391 i915_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
392
393 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
394 }
395
396 /**
397 * i915_pipe_enabled - check if a pipe is enabled
398 * @dev: DRM device
399 * @pipe: pipe to check
400 *
401 * Reading certain registers when the pipe is disabled can hang the chip.
402 * Use this routine to make sure the PLL is running and the pipe is active
403 * before reading such registers if unsure.
404 */
405 static int
406 i915_pipe_enabled(struct drm_device *dev, int pipe)
407 {
408 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
409
410 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
411 /* Locking is horribly broken here, but whatever. */
412 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
414
415 return intel_crtc->active;
416 } else {
417 return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
418 }
419 }
420
421 /* Called from drm generic code, passed a 'crtc', which
422 * we use as a pipe index
423 */
424 static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
425 {
426 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
427 unsigned long high_frame;
428 unsigned long low_frame;
429 u32 high1, high2, low;
430
431 if (!i915_pipe_enabled(dev, pipe)) {
432 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
433 "pipe %c\n", pipe_name(pipe));
434 return 0;
435 }
436
437 high_frame = PIPEFRAME(pipe);
438 low_frame = PIPEFRAMEPIXEL(pipe);
439
440 /*
441 * High & low register fields aren't synchronized, so make sure
442 * we get a low value that's stable across two reads of the high
443 * register.
444 */
445 do {
446 high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
447 low = I915_READ(low_frame) & PIPE_FRAME_LOW_MASK;
448 high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
449 } while (high1 != high2);
450
451 high1 >>= PIPE_FRAME_HIGH_SHIFT;
452 low >>= PIPE_FRAME_LOW_SHIFT;
453 return (high1 << 8) | low;
454 }
455
456 static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
457 {
458 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
459 int reg = PIPE_FRMCOUNT_GM45(pipe);
460
461 if (!i915_pipe_enabled(dev, pipe)) {
462 DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
463 "pipe %c\n", pipe_name(pipe));
464 return 0;
465 }
466
467 return I915_READ(reg);
468 }
469
470 static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
471 int *vpos, int *hpos)
472 {
473 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
474 u32 vbl = 0, position = 0;
475 int vbl_start, vbl_end, htotal, vtotal;
476 bool in_vbl = true;
477 int ret = 0;
478 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
479 pipe);
480
481 if (!i915_pipe_enabled(dev, pipe)) {
482 DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
483 "pipe %c\n", pipe_name(pipe));
484 return 0;
485 }
486
487 /* Get vtotal. */
488 vtotal = 1 + ((I915_READ(VTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
489
490 if (INTEL_INFO(dev)->gen >= 4) {
491 /* No obvious pixelcount register. Only query vertical
492 * scanout position from Display scan line register.
493 */
494 position = I915_READ(PIPEDSL(pipe));
495
496 /* Decode into vertical scanout position. Don't have
497 * horizontal scanout position.
498 */
499 *vpos = position & 0x1fff;
500 *hpos = 0;
501 } else {
502 /* Have access to pixelcount since start of frame.
503 * We can split this into vertical and horizontal
504 * scanout position.
505 */
506 position = (I915_READ(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
507
508 htotal = 1 + ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff);
509 *vpos = position / htotal;
510 *hpos = position - (*vpos * htotal);
511 }
512
513 /* Query vblank area. */
514 vbl = I915_READ(VBLANK(cpu_transcoder));
515
516 /* Test position against vblank region. */
517 vbl_start = vbl & 0x1fff;
518 vbl_end = (vbl >> 16) & 0x1fff;
519
520 if ((*vpos < vbl_start) || (*vpos > vbl_end))
521 in_vbl = false;
522
523 /* Inside "upper part" of vblank area? Apply corrective offset: */
524 if (in_vbl && (*vpos >= vbl_start))
525 *vpos = *vpos - vtotal;
526
527 /* Readouts valid? */
528 if (vbl > 0)
529 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
530
531 /* In vblank? */
532 if (in_vbl)
533 ret |= DRM_SCANOUTPOS_INVBL;
534
535 return ret;
536 }
537
538 static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
539 int *max_error,
540 struct timeval *vblank_time,
541 unsigned flags)
542 {
543 struct drm_crtc *crtc;
544
545 if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
546 DRM_ERROR("Invalid crtc %d\n", pipe);
547 return -EINVAL;
548 }
549
550 /* Get drm_crtc to timestamp: */
551 crtc = intel_get_crtc_for_pipe(dev, pipe);
552 if (crtc == NULL) {
553 DRM_ERROR("Invalid crtc %d\n", pipe);
554 return -EINVAL;
555 }
556
557 if (!crtc->enabled) {
558 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
559 return -EBUSY;
560 }
561
562 /* Helper routine in DRM core does all the work: */
563 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
564 vblank_time, flags,
565 crtc);
566 }
567
568 static int intel_hpd_irq_event(struct drm_device *dev, struct drm_connector *connector)
569 {
570 enum drm_connector_status old_status;
571
572 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
573 old_status = connector->status;
574
575 connector->status = connector->funcs->detect(connector, false);
576 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
577 connector->base.id,
578 drm_get_connector_name(connector),
579 old_status, connector->status);
580 return (old_status != connector->status);
581 }
582
583 /*
584 * Handle hotplug events outside the interrupt handler proper.
585 */
586 #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
587
588 static void i915_hotplug_work_func(struct work_struct *work)
589 {
590 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
591 hotplug_work);
592 struct drm_device *dev = dev_priv->dev;
593 struct drm_mode_config *mode_config = &dev->mode_config;
594 struct intel_connector *intel_connector;
595 struct intel_encoder *intel_encoder;
596 struct drm_connector *connector;
597 unsigned long irqflags;
598 bool hpd_disabled = false;
599 bool changed = false;
600 u32 hpd_event_bits;
601
602 /* HPD irq before everything is fully set up. */
603 if (!dev_priv->enable_hotplug_processing)
604 return;
605
606 mutex_lock(&mode_config->mutex);
607 DRM_DEBUG_KMS("running encoder hotplug functions\n");
608
609 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
610
611 hpd_event_bits = dev_priv->hpd_event_bits;
612 dev_priv->hpd_event_bits = 0;
613 list_for_each_entry(connector, &mode_config->connector_list, head) {
614 intel_connector = to_intel_connector(connector);
615 intel_encoder = intel_connector->encoder;
616 if (intel_encoder->hpd_pin > HPD_NONE &&
617 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
618 connector->polled == DRM_CONNECTOR_POLL_HPD) {
619 DRM_INFO("HPD interrupt storm detected on connector %s: "
620 "switching from hotplug detection to polling\n",
621 drm_get_connector_name(connector));
622 dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
623 connector->polled = DRM_CONNECTOR_POLL_CONNECT
624 | DRM_CONNECTOR_POLL_DISCONNECT;
625 hpd_disabled = true;
626 }
627 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
628 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
629 drm_get_connector_name(connector), intel_encoder->hpd_pin);
630 }
631 }
632 /* if there were no outputs to poll, poll was disabled,
633 * therefore make sure it's enabled when disabling HPD on
634 * some connectors */
635 if (hpd_disabled) {
636 drm_kms_helper_poll_enable(dev);
637 mod_timer(&dev_priv->hotplug_reenable_timer,
638 jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
639 }
640
641 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
642
643 list_for_each_entry(connector, &mode_config->connector_list, head) {
644 intel_connector = to_intel_connector(connector);
645 intel_encoder = intel_connector->encoder;
646 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
647 if (intel_encoder->hot_plug)
648 intel_encoder->hot_plug(intel_encoder);
649 if (intel_hpd_irq_event(dev, connector))
650 changed = true;
651 }
652 }
653 mutex_unlock(&mode_config->mutex);
654
655 if (changed)
656 drm_kms_helper_hotplug_event(dev);
657 }
658
659 static void ironlake_rps_change_irq_handler(struct drm_device *dev)
660 {
661 drm_i915_private_t *dev_priv = dev->dev_private;
662 u32 busy_up, busy_down, max_avg, min_avg;
663 u8 new_delay;
664
665 spin_lock(&mchdev_lock);
666
667 I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
668
669 new_delay = dev_priv->ips.cur_delay;
670
671 I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
672 busy_up = I915_READ(RCPREVBSYTUPAVG);
673 busy_down = I915_READ(RCPREVBSYTDNAVG);
674 max_avg = I915_READ(RCBMAXAVG);
675 min_avg = I915_READ(RCBMINAVG);
676
677 /* Handle RCS change request from hw */
678 if (busy_up > max_avg) {
679 if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
680 new_delay = dev_priv->ips.cur_delay - 1;
681 if (new_delay < dev_priv->ips.max_delay)
682 new_delay = dev_priv->ips.max_delay;
683 } else if (busy_down < min_avg) {
684 if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
685 new_delay = dev_priv->ips.cur_delay + 1;
686 if (new_delay > dev_priv->ips.min_delay)
687 new_delay = dev_priv->ips.min_delay;
688 }
689
690 if (ironlake_set_drps(dev, new_delay))
691 dev_priv->ips.cur_delay = new_delay;
692
693 spin_unlock(&mchdev_lock);
694
695 return;
696 }
697
698 static void notify_ring(struct drm_device *dev,
699 struct intel_ring_buffer *ring)
700 {
701 if (ring->obj == NULL)
702 return;
703
704 trace_i915_gem_request_complete(ring, ring->get_seqno(ring, false));
705
706 wake_up_all(&ring->irq_queue);
707 i915_queue_hangcheck(dev);
708 }
709
710 static void gen6_pm_rps_work(struct work_struct *work)
711 {
712 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
713 rps.work);
714 u32 pm_iir, pm_imr;
715 u8 new_delay;
716
717 spin_lock_irq(&dev_priv->irq_lock);
718 pm_iir = dev_priv->rps.pm_iir;
719 dev_priv->rps.pm_iir = 0;
720 pm_imr = I915_READ(GEN6_PMIMR);
721 /* Make sure not to corrupt PMIMR state used by ringbuffer code */
722 I915_WRITE(GEN6_PMIMR, pm_imr & ~GEN6_PM_RPS_EVENTS);
723 spin_unlock_irq(&dev_priv->irq_lock);
724
725 if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
726 return;
727
728 mutex_lock(&dev_priv->rps.hw_lock);
729
730 if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
731 new_delay = dev_priv->rps.cur_delay + 1;
732
733 /*
734 * For better performance, jump directly
735 * to RPe if we're below it.
736 */
737 if (IS_VALLEYVIEW(dev_priv->dev) &&
738 dev_priv->rps.cur_delay < dev_priv->rps.rpe_delay)
739 new_delay = dev_priv->rps.rpe_delay;
740 } else
741 new_delay = dev_priv->rps.cur_delay - 1;
742
743 /* sysfs frequency interfaces may have snuck in while servicing the
744 * interrupt
745 */
746 if (new_delay >= dev_priv->rps.min_delay &&
747 new_delay <= dev_priv->rps.max_delay) {
748 if (IS_VALLEYVIEW(dev_priv->dev))
749 valleyview_set_rps(dev_priv->dev, new_delay);
750 else
751 gen6_set_rps(dev_priv->dev, new_delay);
752 }
753
754 if (IS_VALLEYVIEW(dev_priv->dev)) {
755 /*
756 * On VLV, when we enter RC6 we may not be at the minimum
757 * voltage level, so arm a timer to check. It should only
758 * fire when there's activity or once after we've entered
759 * RC6, and then won't be re-armed until the next RPS interrupt.
760 */
761 mod_delayed_work(dev_priv->wq, &dev_priv->rps.vlv_work,
762 msecs_to_jiffies(100));
763 }
764
765 mutex_unlock(&dev_priv->rps.hw_lock);
766 }
767
768
769 /**
770 * ivybridge_parity_work - Workqueue called when a parity error interrupt
771 * occurred.
772 * @work: workqueue struct
773 *
774 * Doesn't actually do anything except notify userspace. As a consequence of
775 * this event, userspace should try to remap the bad rows since statistically
776 * it is likely the same row is more likely to go bad again.
777 */
778 static void ivybridge_parity_work(struct work_struct *work)
779 {
780 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
781 l3_parity.error_work);
782 u32 error_status, row, bank, subbank;
783 char *parity_event[5];
784 uint32_t misccpctl;
785 unsigned long flags;
786
787 /* We must turn off DOP level clock gating to access the L3 registers.
788 * In order to prevent a get/put style interface, acquire struct mutex
789 * any time we access those registers.
790 */
791 mutex_lock(&dev_priv->dev->struct_mutex);
792
793 misccpctl = I915_READ(GEN7_MISCCPCTL);
794 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
795 POSTING_READ(GEN7_MISCCPCTL);
796
797 error_status = I915_READ(GEN7_L3CDERRST1);
798 row = GEN7_PARITY_ERROR_ROW(error_status);
799 bank = GEN7_PARITY_ERROR_BANK(error_status);
800 subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
801
802 I915_WRITE(GEN7_L3CDERRST1, GEN7_PARITY_ERROR_VALID |
803 GEN7_L3CDERRST1_ENABLE);
804 POSTING_READ(GEN7_L3CDERRST1);
805
806 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
807
808 spin_lock_irqsave(&dev_priv->irq_lock, flags);
809 dev_priv->gt_irq_mask &= ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
810 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
811 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
812
813 mutex_unlock(&dev_priv->dev->struct_mutex);
814
815 parity_event[0] = "L3_PARITY_ERROR=1";
816 parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
817 parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
818 parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
819 parity_event[4] = NULL;
820
821 kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
822 KOBJ_CHANGE, parity_event);
823
824 DRM_DEBUG("Parity error: Row = %d, Bank = %d, Sub bank = %d.\n",
825 row, bank, subbank);
826
827 kfree(parity_event[3]);
828 kfree(parity_event[2]);
829 kfree(parity_event[1]);
830 }
831
832 static void ivybridge_parity_error_irq_handler(struct drm_device *dev)
833 {
834 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
835
836 if (!HAS_L3_GPU_CACHE(dev))
837 return;
838
839 spin_lock(&dev_priv->irq_lock);
840 dev_priv->gt_irq_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
841 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
842 spin_unlock(&dev_priv->irq_lock);
843
844 queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
845 }
846
847 static void snb_gt_irq_handler(struct drm_device *dev,
848 struct drm_i915_private *dev_priv,
849 u32 gt_iir)
850 {
851
852 if (gt_iir &
853 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
854 notify_ring(dev, &dev_priv->ring[RCS]);
855 if (gt_iir & GT_BSD_USER_INTERRUPT)
856 notify_ring(dev, &dev_priv->ring[VCS]);
857 if (gt_iir & GT_BLT_USER_INTERRUPT)
858 notify_ring(dev, &dev_priv->ring[BCS]);
859
860 if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
861 GT_BSD_CS_ERROR_INTERRUPT |
862 GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
863 DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
864 i915_handle_error(dev, false);
865 }
866
867 if (gt_iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
868 ivybridge_parity_error_irq_handler(dev);
869 }
870
871 /* Legacy way of handling PM interrupts */
872 static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv,
873 u32 pm_iir)
874 {
875 /*
876 * IIR bits should never already be set because IMR should
877 * prevent an interrupt from being shown in IIR. The warning
878 * displays a case where we've unsafely cleared
879 * dev_priv->rps.pm_iir. Although missing an interrupt of the same
880 * type is not a problem, it displays a problem in the logic.
881 *
882 * The mask bit in IMR is cleared by dev_priv->rps.work.
883 */
884
885 spin_lock(&dev_priv->irq_lock);
886 dev_priv->rps.pm_iir |= pm_iir;
887 I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
888 POSTING_READ(GEN6_PMIMR);
889 spin_unlock(&dev_priv->irq_lock);
890
891 queue_work(dev_priv->wq, &dev_priv->rps.work);
892 }
893
894 #define HPD_STORM_DETECT_PERIOD 1000
895 #define HPD_STORM_THRESHOLD 5
896
897 static inline void intel_hpd_irq_handler(struct drm_device *dev,
898 u32 hotplug_trigger,
899 const u32 *hpd)
900 {
901 drm_i915_private_t *dev_priv = dev->dev_private;
902 int i;
903 bool storm_detected = false;
904
905 if (!hotplug_trigger)
906 return;
907
908 spin_lock(&dev_priv->irq_lock);
909 for (i = 1; i < HPD_NUM_PINS; i++) {
910
911 if (!(hpd[i] & hotplug_trigger) ||
912 dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
913 continue;
914
915 dev_priv->hpd_event_bits |= (1 << i);
916 if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
917 dev_priv->hpd_stats[i].hpd_last_jiffies
918 + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
919 dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
920 dev_priv->hpd_stats[i].hpd_cnt = 0;
921 } else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
922 dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
923 dev_priv->hpd_event_bits &= ~(1 << i);
924 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
925 storm_detected = true;
926 } else {
927 dev_priv->hpd_stats[i].hpd_cnt++;
928 }
929 }
930
931 if (storm_detected)
932 dev_priv->display.hpd_irq_setup(dev);
933 spin_unlock(&dev_priv->irq_lock);
934
935 queue_work(dev_priv->wq,
936 &dev_priv->hotplug_work);
937 }
938
939 static void gmbus_irq_handler(struct drm_device *dev)
940 {
941 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
942
943 wake_up_all(&dev_priv->gmbus_wait_queue);
944 }
945
946 static void dp_aux_irq_handler(struct drm_device *dev)
947 {
948 struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
949
950 wake_up_all(&dev_priv->gmbus_wait_queue);
951 }
952
953 /* Unlike gen6_rps_irq_handler() from which this function is originally derived,
954 * we must be able to deal with other PM interrupts. This is complicated because
955 * of the way in which we use the masks to defer the RPS work (which for
956 * posterity is necessary because of forcewake).
957 */
958 static void hsw_pm_irq_handler(struct drm_i915_private *dev_priv,
959 u32 pm_iir)
960 {
961 if (pm_iir & GEN6_PM_RPS_EVENTS) {
962 spin_lock(&dev_priv->irq_lock);
963 dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
964 I915_WRITE(GEN6_PMIMR, dev_priv->rps.pm_iir);
965 /* never want to mask useful interrupts. (also posting read) */
966 WARN_ON(I915_READ_NOTRACE(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
967 spin_unlock(&dev_priv->irq_lock);
968
969 queue_work(dev_priv->wq, &dev_priv->rps.work);
970 }
971
972 if (pm_iir & PM_VEBOX_USER_INTERRUPT)
973 notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
974
975 if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
976 DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
977 i915_handle_error(dev_priv->dev, false);
978 }
979 }
980
981 static irqreturn_t valleyview_irq_handler(int irq, void *arg)
982 {
983 struct drm_device *dev = (struct drm_device *) arg;
984 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
985 u32 iir, gt_iir, pm_iir;
986 irqreturn_t ret = IRQ_NONE;
987 unsigned long irqflags;
988 int pipe;
989 u32 pipe_stats[I915_MAX_PIPES];
990
991 atomic_inc(&dev_priv->irq_received);
992
993 while (true) {
994 iir = I915_READ(VLV_IIR);
995 gt_iir = I915_READ(GTIIR);
996 pm_iir = I915_READ(GEN6_PMIIR);
997
998 if (gt_iir == 0 && pm_iir == 0 && iir == 0)
999 goto out;
1000
1001 ret = IRQ_HANDLED;
1002
1003 snb_gt_irq_handler(dev, dev_priv, gt_iir);
1004
1005 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1006 for_each_pipe(pipe) {
1007 int reg = PIPESTAT(pipe);
1008 pipe_stats[pipe] = I915_READ(reg);
1009
1010 /*
1011 * Clear the PIPE*STAT regs before the IIR
1012 */
1013 if (pipe_stats[pipe] & 0x8000ffff) {
1014 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1015 DRM_DEBUG_DRIVER("pipe %c underrun\n",
1016 pipe_name(pipe));
1017 I915_WRITE(reg, pipe_stats[pipe]);
1018 }
1019 }
1020 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1021
1022 for_each_pipe(pipe) {
1023 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1024 drm_handle_vblank(dev, pipe);
1025
1026 if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
1027 intel_prepare_page_flip(dev, pipe);
1028 intel_finish_page_flip(dev, pipe);
1029 }
1030 }
1031
1032 /* Consume port. Then clear IIR or we'll miss events */
1033 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
1034 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1035 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
1036
1037 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1038 hotplug_status);
1039
1040 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
1041
1042 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
1043 I915_READ(PORT_HOTPLUG_STAT);
1044 }
1045
1046 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1047 gmbus_irq_handler(dev);
1048
1049 if (pm_iir & GEN6_PM_RPS_EVENTS)
1050 gen6_rps_irq_handler(dev_priv, pm_iir);
1051
1052 I915_WRITE(GTIIR, gt_iir);
1053 I915_WRITE(GEN6_PMIIR, pm_iir);
1054 I915_WRITE(VLV_IIR, iir);
1055 }
1056
1057 out:
1058 return ret;
1059 }
1060
1061 static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1062 {
1063 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1064 int pipe;
1065 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1066
1067 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
1068
1069 if (pch_iir & SDE_AUDIO_POWER_MASK) {
1070 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1071 SDE_AUDIO_POWER_SHIFT);
1072 DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1073 port_name(port));
1074 }
1075
1076 if (pch_iir & SDE_AUX_MASK)
1077 dp_aux_irq_handler(dev);
1078
1079 if (pch_iir & SDE_GMBUS)
1080 gmbus_irq_handler(dev);
1081
1082 if (pch_iir & SDE_AUDIO_HDCP_MASK)
1083 DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1084
1085 if (pch_iir & SDE_AUDIO_TRANS_MASK)
1086 DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1087
1088 if (pch_iir & SDE_POISON)
1089 DRM_ERROR("PCH poison interrupt\n");
1090
1091 if (pch_iir & SDE_FDI_MASK)
1092 for_each_pipe(pipe)
1093 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1094 pipe_name(pipe),
1095 I915_READ(FDI_RX_IIR(pipe)));
1096
1097 if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1098 DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1099
1100 if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1101 DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1102
1103 if (pch_iir & SDE_TRANSA_FIFO_UNDER)
1104 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1105 false))
1106 DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
1107
1108 if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1109 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1110 false))
1111 DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
1112 }
1113
1114 static void ivb_err_int_handler(struct drm_device *dev)
1115 {
1116 struct drm_i915_private *dev_priv = dev->dev_private;
1117 u32 err_int = I915_READ(GEN7_ERR_INT);
1118
1119 if (err_int & ERR_INT_POISON)
1120 DRM_ERROR("Poison interrupt\n");
1121
1122 if (err_int & ERR_INT_FIFO_UNDERRUN_A)
1123 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1124 DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1125
1126 if (err_int & ERR_INT_FIFO_UNDERRUN_B)
1127 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1128 DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1129
1130 if (err_int & ERR_INT_FIFO_UNDERRUN_C)
1131 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_C, false))
1132 DRM_DEBUG_DRIVER("Pipe C FIFO underrun\n");
1133
1134 I915_WRITE(GEN7_ERR_INT, err_int);
1135 }
1136
1137 static void cpt_serr_int_handler(struct drm_device *dev)
1138 {
1139 struct drm_i915_private *dev_priv = dev->dev_private;
1140 u32 serr_int = I915_READ(SERR_INT);
1141
1142 if (serr_int & SERR_INT_POISON)
1143 DRM_ERROR("PCH poison interrupt\n");
1144
1145 if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
1146 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
1147 false))
1148 DRM_DEBUG_DRIVER("PCH transcoder A FIFO underrun\n");
1149
1150 if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
1151 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
1152 false))
1153 DRM_DEBUG_DRIVER("PCH transcoder B FIFO underrun\n");
1154
1155 if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
1156 if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
1157 false))
1158 DRM_DEBUG_DRIVER("PCH transcoder C FIFO underrun\n");
1159
1160 I915_WRITE(SERR_INT, serr_int);
1161 }
1162
1163 static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
1164 {
1165 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1166 int pipe;
1167 u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1168
1169 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
1170
1171 if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1172 int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
1173 SDE_AUDIO_POWER_SHIFT_CPT);
1174 DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1175 port_name(port));
1176 }
1177
1178 if (pch_iir & SDE_AUX_MASK_CPT)
1179 dp_aux_irq_handler(dev);
1180
1181 if (pch_iir & SDE_GMBUS_CPT)
1182 gmbus_irq_handler(dev);
1183
1184 if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
1185 DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
1186
1187 if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
1188 DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
1189
1190 if (pch_iir & SDE_FDI_MASK_CPT)
1191 for_each_pipe(pipe)
1192 DRM_DEBUG_DRIVER(" pipe %c FDI IIR: 0x%08x\n",
1193 pipe_name(pipe),
1194 I915_READ(FDI_RX_IIR(pipe)));
1195
1196 if (pch_iir & SDE_ERROR_CPT)
1197 cpt_serr_int_handler(dev);
1198 }
1199
1200 static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1201 {
1202 struct drm_i915_private *dev_priv = dev->dev_private;
1203
1204 if (de_iir & DE_AUX_CHANNEL_A)
1205 dp_aux_irq_handler(dev);
1206
1207 if (de_iir & DE_GSE)
1208 intel_opregion_asle_intr(dev);
1209
1210 if (de_iir & DE_PIPEA_VBLANK)
1211 drm_handle_vblank(dev, 0);
1212
1213 if (de_iir & DE_PIPEB_VBLANK)
1214 drm_handle_vblank(dev, 1);
1215
1216 if (de_iir & DE_POISON)
1217 DRM_ERROR("Poison interrupt\n");
1218
1219 if (de_iir & DE_PIPEA_FIFO_UNDERRUN)
1220 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_A, false))
1221 DRM_DEBUG_DRIVER("Pipe A FIFO underrun\n");
1222
1223 if (de_iir & DE_PIPEB_FIFO_UNDERRUN)
1224 if (intel_set_cpu_fifo_underrun_reporting(dev, PIPE_B, false))
1225 DRM_DEBUG_DRIVER("Pipe B FIFO underrun\n");
1226
1227 if (de_iir & DE_PLANEA_FLIP_DONE) {
1228 intel_prepare_page_flip(dev, 0);
1229 intel_finish_page_flip_plane(dev, 0);
1230 }
1231
1232 if (de_iir & DE_PLANEB_FLIP_DONE) {
1233 intel_prepare_page_flip(dev, 1);
1234 intel_finish_page_flip_plane(dev, 1);
1235 }
1236
1237 /* check event from PCH */
1238 if (de_iir & DE_PCH_EVENT) {
1239 u32 pch_iir = I915_READ(SDEIIR);
1240
1241 if (HAS_PCH_CPT(dev))
1242 cpt_irq_handler(dev, pch_iir);
1243 else
1244 ibx_irq_handler(dev, pch_iir);
1245
1246 /* should clear PCH hotplug event before clear CPU irq */
1247 I915_WRITE(SDEIIR, pch_iir);
1248 }
1249
1250 if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1251 ironlake_rps_change_irq_handler(dev);
1252 }
1253
1254 static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
1255 {
1256 struct drm_i915_private *dev_priv = dev->dev_private;
1257 int i;
1258
1259 if (de_iir & DE_ERR_INT_IVB)
1260 ivb_err_int_handler(dev);
1261
1262 if (de_iir & DE_AUX_CHANNEL_A_IVB)
1263 dp_aux_irq_handler(dev);
1264
1265 if (de_iir & DE_GSE_IVB)
1266 intel_opregion_asle_intr(dev);
1267
1268 for (i = 0; i < 3; i++) {
1269 if (de_iir & (DE_PIPEA_VBLANK_IVB << (5 * i)))
1270 drm_handle_vblank(dev, i);
1271 if (de_iir & (DE_PLANEA_FLIP_DONE_IVB << (5 * i))) {
1272 intel_prepare_page_flip(dev, i);
1273 intel_finish_page_flip_plane(dev, i);
1274 }
1275 }
1276
1277 /* check event from PCH */
1278 if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
1279 u32 pch_iir = I915_READ(SDEIIR);
1280
1281 cpt_irq_handler(dev, pch_iir);
1282
1283 /* clear PCH hotplug event before clear CPU irq */
1284 I915_WRITE(SDEIIR, pch_iir);
1285 }
1286 }
1287
1288 static irqreturn_t ivybridge_irq_handler(int irq, void *arg)
1289 {
1290 struct drm_device *dev = (struct drm_device *) arg;
1291 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1292 u32 de_iir, gt_iir, de_ier, pm_iir, sde_ier = 0;
1293 irqreturn_t ret = IRQ_NONE;
1294
1295 atomic_inc(&dev_priv->irq_received);
1296
1297 /* We get interrupts on unclaimed registers, so check for this before we
1298 * do any I915_{READ,WRITE}. */
1299 if (IS_HASWELL(dev) &&
1300 (I915_READ_NOTRACE(FPGA_DBG) & FPGA_DBG_RM_NOCLAIM)) {
1301 DRM_ERROR("Unclaimed register before interrupt\n");
1302 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
1303 }
1304
1305 /* disable master interrupt before clearing iir */
1306 de_ier = I915_READ(DEIER);
1307 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
1308 POSTING_READ(DEIER);
1309
1310 /* Disable south interrupts. We'll only write to SDEIIR once, so further
1311 * interrupts will will be stored on its back queue, and then we'll be
1312 * able to process them after we restore SDEIER (as soon as we restore
1313 * it, we'll get an interrupt if SDEIIR still has something to process
1314 * due to its back queue). */
1315 if (!HAS_PCH_NOP(dev)) {
1316 sde_ier = I915_READ(SDEIER);
1317 I915_WRITE(SDEIER, 0);
1318 POSTING_READ(SDEIER);
1319 }
1320
1321 /* On Haswell, also mask ERR_INT because we don't want to risk
1322 * generating "unclaimed register" interrupts from inside the interrupt
1323 * handler. */
1324 if (IS_HASWELL(dev)) {
1325 spin_lock(&dev_priv->irq_lock);
1326 ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
1327 spin_unlock(&dev_priv->irq_lock);
1328 }
1329
1330 gt_iir = I915_READ(GTIIR);
1331 if (gt_iir) {
1332 snb_gt_irq_handler(dev, dev_priv, gt_iir);
1333 I915_WRITE(GTIIR, gt_iir);
1334 ret = IRQ_HANDLED;
1335 }
1336
1337 de_iir = I915_READ(DEIIR);
1338 if (de_iir) {
1339 ivb_display_irq_handler(dev, de_iir);
1340
1341 I915_WRITE(DEIIR, de_iir);
1342 ret = IRQ_HANDLED;
1343 }
1344
1345 pm_iir = I915_READ(GEN6_PMIIR);
1346 if (pm_iir) {
1347 if (IS_HASWELL(dev))
1348 hsw_pm_irq_handler(dev_priv, pm_iir);
1349 else if (pm_iir & GEN6_PM_RPS_EVENTS)
1350 gen6_rps_irq_handler(dev_priv, pm_iir);
1351 I915_WRITE(GEN6_PMIIR, pm_iir);
1352 ret = IRQ_HANDLED;
1353 }
1354
1355 if (IS_HASWELL(dev)) {
1356 spin_lock(&dev_priv->irq_lock);
1357 if (ivb_can_enable_err_int(dev))
1358 ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
1359 spin_unlock(&dev_priv->irq_lock);
1360 }
1361
1362 I915_WRITE(DEIER, de_ier);
1363 POSTING_READ(DEIER);
1364 if (!HAS_PCH_NOP(dev)) {
1365 I915_WRITE(SDEIER, sde_ier);
1366 POSTING_READ(SDEIER);
1367 }
1368
1369 return ret;
1370 }
1371
1372 static void ilk_gt_irq_handler(struct drm_device *dev,
1373 struct drm_i915_private *dev_priv,
1374 u32 gt_iir)
1375 {
1376 if (gt_iir &
1377 (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1378 notify_ring(dev, &dev_priv->ring[RCS]);
1379 if (gt_iir & ILK_BSD_USER_INTERRUPT)
1380 notify_ring(dev, &dev_priv->ring[VCS]);
1381 }
1382
1383 static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1384 {
1385 struct drm_device *dev = (struct drm_device *) arg;
1386 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1387 int ret = IRQ_NONE;
1388 u32 de_iir, gt_iir, de_ier, sde_ier;
1389
1390 atomic_inc(&dev_priv->irq_received);
1391
1392 /* disable master interrupt before clearing iir */
1393 de_ier = I915_READ(DEIER);
1394 I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
1395 POSTING_READ(DEIER);
1396
1397 /* Disable south interrupts. We'll only write to SDEIIR once, so further
1398 * interrupts will will be stored on its back queue, and then we'll be
1399 * able to process them after we restore SDEIER (as soon as we restore
1400 * it, we'll get an interrupt if SDEIIR still has something to process
1401 * due to its back queue). */
1402 sde_ier = I915_READ(SDEIER);
1403 I915_WRITE(SDEIER, 0);
1404 POSTING_READ(SDEIER);
1405
1406 gt_iir = I915_READ(GTIIR);
1407 if (gt_iir) {
1408 if (IS_GEN5(dev))
1409 ilk_gt_irq_handler(dev, dev_priv, gt_iir);
1410 else
1411 snb_gt_irq_handler(dev, dev_priv, gt_iir);
1412 I915_WRITE(GTIIR, gt_iir);
1413 ret = IRQ_HANDLED;
1414 }
1415
1416 de_iir = I915_READ(DEIIR);
1417 if (de_iir) {
1418 ilk_display_irq_handler(dev, de_iir);
1419 I915_WRITE(DEIIR, de_iir);
1420 ret = IRQ_HANDLED;
1421 }
1422
1423 if (IS_GEN6(dev)) {
1424 u32 pm_iir = I915_READ(GEN6_PMIIR);
1425 if (pm_iir) {
1426 if (pm_iir & GEN6_PM_RPS_EVENTS)
1427 gen6_rps_irq_handler(dev_priv, pm_iir);
1428 I915_WRITE(GEN6_PMIIR, pm_iir);
1429 ret = IRQ_HANDLED;
1430 }
1431 }
1432
1433 I915_WRITE(DEIER, de_ier);
1434 POSTING_READ(DEIER);
1435 I915_WRITE(SDEIER, sde_ier);
1436 POSTING_READ(SDEIER);
1437
1438 return ret;
1439 }
1440
1441 /**
1442 * i915_error_work_func - do process context error handling work
1443 * @work: work struct
1444 *
1445 * Fire an error uevent so userspace can see that a hang or error
1446 * was detected.
1447 */
1448 static void i915_error_work_func(struct work_struct *work)
1449 {
1450 struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
1451 work);
1452 drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
1453 gpu_error);
1454 struct drm_device *dev = dev_priv->dev;
1455 struct intel_ring_buffer *ring;
1456 char *error_event[] = { "ERROR=1", NULL };
1457 char *reset_event[] = { "RESET=1", NULL };
1458 char *reset_done_event[] = { "ERROR=0", NULL };
1459 int i, ret;
1460
1461 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
1462
1463 /*
1464 * Note that there's only one work item which does gpu resets, so we
1465 * need not worry about concurrent gpu resets potentially incrementing
1466 * error->reset_counter twice. We only need to take care of another
1467 * racing irq/hangcheck declaring the gpu dead for a second time. A
1468 * quick check for that is good enough: schedule_work ensures the
1469 * correct ordering between hang detection and this work item, and since
1470 * the reset in-progress bit is only ever set by code outside of this
1471 * work we don't need to worry about any other races.
1472 */
1473 if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
1474 DRM_DEBUG_DRIVER("resetting chip\n");
1475 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
1476 reset_event);
1477
1478 ret = i915_reset(dev);
1479
1480 if (ret == 0) {
1481 /*
1482 * After all the gem state is reset, increment the reset
1483 * counter and wake up everyone waiting for the reset to
1484 * complete.
1485 *
1486 * Since unlock operations are a one-sided barrier only,
1487 * we need to insert a barrier here to order any seqno
1488 * updates before
1489 * the counter increment.
1490 */
1491 smp_mb__before_atomic_inc();
1492 atomic_inc(&dev_priv->gpu_error.reset_counter);
1493
1494 kobject_uevent_env(&dev->primary->kdev.kobj,
1495 KOBJ_CHANGE, reset_done_event);
1496 } else {
1497 atomic_set(&error->reset_counter, I915_WEDGED);
1498 }
1499
1500 for_each_ring(ring, dev_priv, i)
1501 wake_up_all(&ring->irq_queue);
1502
1503 intel_display_handle_reset(dev);
1504
1505 wake_up_all(&dev_priv->gpu_error.reset_queue);
1506 }
1507 }
1508
1509 static void i915_report_and_clear_eir(struct drm_device *dev)
1510 {
1511 struct drm_i915_private *dev_priv = dev->dev_private;
1512 uint32_t instdone[I915_NUM_INSTDONE_REG];
1513 u32 eir = I915_READ(EIR);
1514 int pipe, i;
1515
1516 if (!eir)
1517 return;
1518
1519 pr_err("render error detected, EIR: 0x%08x\n", eir);
1520
1521 i915_get_extra_instdone(dev, instdone);
1522
1523 if (IS_G4X(dev)) {
1524 if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
1525 u32 ipeir = I915_READ(IPEIR_I965);
1526
1527 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1528 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1529 for (i = 0; i < ARRAY_SIZE(instdone); i++)
1530 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1531 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
1532 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
1533 I915_WRITE(IPEIR_I965, ipeir);
1534 POSTING_READ(IPEIR_I965);
1535 }
1536 if (eir & GM45_ERROR_PAGE_TABLE) {
1537 u32 pgtbl_err = I915_READ(PGTBL_ER);
1538 pr_err("page table error\n");
1539 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
1540 I915_WRITE(PGTBL_ER, pgtbl_err);
1541 POSTING_READ(PGTBL_ER);
1542 }
1543 }
1544
1545 if (!IS_GEN2(dev)) {
1546 if (eir & I915_ERROR_PAGE_TABLE) {
1547 u32 pgtbl_err = I915_READ(PGTBL_ER);
1548 pr_err("page table error\n");
1549 pr_err(" PGTBL_ER: 0x%08x\n", pgtbl_err);
1550 I915_WRITE(PGTBL_ER, pgtbl_err);
1551 POSTING_READ(PGTBL_ER);
1552 }
1553 }
1554
1555 if (eir & I915_ERROR_MEMORY_REFRESH) {
1556 pr_err("memory refresh error:\n");
1557 for_each_pipe(pipe)
1558 pr_err("pipe %c stat: 0x%08x\n",
1559 pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
1560 /* pipestat has already been acked */
1561 }
1562 if (eir & I915_ERROR_INSTRUCTION) {
1563 pr_err("instruction error\n");
1564 pr_err(" INSTPM: 0x%08x\n", I915_READ(INSTPM));
1565 for (i = 0; i < ARRAY_SIZE(instdone); i++)
1566 pr_err(" INSTDONE_%d: 0x%08x\n", i, instdone[i]);
1567 if (INTEL_INFO(dev)->gen < 4) {
1568 u32 ipeir = I915_READ(IPEIR);
1569
1570 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR));
1571 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR));
1572 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD));
1573 I915_WRITE(IPEIR, ipeir);
1574 POSTING_READ(IPEIR);
1575 } else {
1576 u32 ipeir = I915_READ(IPEIR_I965);
1577
1578 pr_err(" IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
1579 pr_err(" IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
1580 pr_err(" INSTPS: 0x%08x\n", I915_READ(INSTPS));
1581 pr_err(" ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
1582 I915_WRITE(IPEIR_I965, ipeir);
1583 POSTING_READ(IPEIR_I965);
1584 }
1585 }
1586
1587 I915_WRITE(EIR, eir);
1588 POSTING_READ(EIR);
1589 eir = I915_READ(EIR);
1590 if (eir) {
1591 /*
1592 * some errors might have become stuck,
1593 * mask them.
1594 */
1595 DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
1596 I915_WRITE(EMR, I915_READ(EMR) | eir);
1597 I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
1598 }
1599 }
1600
1601 /**
1602 * i915_handle_error - handle an error interrupt
1603 * @dev: drm device
1604 *
1605 * Do some basic checking of regsiter state at error interrupt time and
1606 * dump it to the syslog. Also call i915_capture_error_state() to make
1607 * sure we get a record and make it available in debugfs. Fire a uevent
1608 * so userspace knows something bad happened (should trigger collection
1609 * of a ring dump etc.).
1610 */
1611 void i915_handle_error(struct drm_device *dev, bool wedged)
1612 {
1613 struct drm_i915_private *dev_priv = dev->dev_private;
1614 struct intel_ring_buffer *ring;
1615 int i;
1616
1617 i915_capture_error_state(dev);
1618 i915_report_and_clear_eir(dev);
1619
1620 if (wedged) {
1621 atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
1622 &dev_priv->gpu_error.reset_counter);
1623
1624 /*
1625 * Wakeup waiting processes so that the reset work item
1626 * doesn't deadlock trying to grab various locks.
1627 */
1628 for_each_ring(ring, dev_priv, i)
1629 wake_up_all(&ring->irq_queue);
1630 }
1631
1632 queue_work(dev_priv->wq, &dev_priv->gpu_error.work);
1633 }
1634
1635 static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
1636 {
1637 drm_i915_private_t *dev_priv = dev->dev_private;
1638 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1639 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1640 struct drm_i915_gem_object *obj;
1641 struct intel_unpin_work *work;
1642 unsigned long flags;
1643 bool stall_detected;
1644
1645 /* Ignore early vblank irqs */
1646 if (intel_crtc == NULL)
1647 return;
1648
1649 spin_lock_irqsave(&dev->event_lock, flags);
1650 work = intel_crtc->unpin_work;
1651
1652 if (work == NULL ||
1653 atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
1654 !work->enable_stall_check) {
1655 /* Either the pending flip IRQ arrived, or we're too early. Don't check */
1656 spin_unlock_irqrestore(&dev->event_lock, flags);
1657 return;
1658 }
1659
1660 /* Potential stall - if we see that the flip has happened, assume a missed interrupt */
1661 obj = work->pending_flip_obj;
1662 if (INTEL_INFO(dev)->gen >= 4) {
1663 int dspsurf = DSPSURF(intel_crtc->plane);
1664 stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
1665 i915_gem_obj_ggtt_offset(obj);
1666 } else {
1667 int dspaddr = DSPADDR(intel_crtc->plane);
1668 stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
1669 crtc->y * crtc->fb->pitches[0] +
1670 crtc->x * crtc->fb->bits_per_pixel/8);
1671 }
1672
1673 spin_unlock_irqrestore(&dev->event_lock, flags);
1674
1675 if (stall_detected) {
1676 DRM_DEBUG_DRIVER("Pageflip stall detected\n");
1677 intel_prepare_page_flip(dev, intel_crtc->plane);
1678 }
1679 }
1680
1681 /* Called from drm generic code, passed 'crtc' which
1682 * we use as a pipe index
1683 */
1684 static int i915_enable_vblank(struct drm_device *dev, int pipe)
1685 {
1686 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1687 unsigned long irqflags;
1688
1689 if (!i915_pipe_enabled(dev, pipe))
1690 return -EINVAL;
1691
1692 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1693 if (INTEL_INFO(dev)->gen >= 4)
1694 i915_enable_pipestat(dev_priv, pipe,
1695 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1696 else
1697 i915_enable_pipestat(dev_priv, pipe,
1698 PIPE_VBLANK_INTERRUPT_ENABLE);
1699
1700 /* maintain vblank delivery even in deep C-states */
1701 if (dev_priv->info->gen == 3)
1702 I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
1703 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1704
1705 return 0;
1706 }
1707
1708 static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
1709 {
1710 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1711 unsigned long irqflags;
1712
1713 if (!i915_pipe_enabled(dev, pipe))
1714 return -EINVAL;
1715
1716 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1717 ironlake_enable_display_irq(dev_priv, (pipe == 0) ?
1718 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1719 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1720
1721 return 0;
1722 }
1723
1724 static int ivybridge_enable_vblank(struct drm_device *dev, int pipe)
1725 {
1726 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1727 unsigned long irqflags;
1728
1729 if (!i915_pipe_enabled(dev, pipe))
1730 return -EINVAL;
1731
1732 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1733 ironlake_enable_display_irq(dev_priv,
1734 DE_PIPEA_VBLANK_IVB << (5 * pipe));
1735 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1736
1737 return 0;
1738 }
1739
1740 static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
1741 {
1742 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1743 unsigned long irqflags;
1744 u32 imr;
1745
1746 if (!i915_pipe_enabled(dev, pipe))
1747 return -EINVAL;
1748
1749 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1750 imr = I915_READ(VLV_IMR);
1751 if (pipe == 0)
1752 imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1753 else
1754 imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
1755 I915_WRITE(VLV_IMR, imr);
1756 i915_enable_pipestat(dev_priv, pipe,
1757 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1758 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1759
1760 return 0;
1761 }
1762
1763 /* Called from drm generic code, passed 'crtc' which
1764 * we use as a pipe index
1765 */
1766 static void i915_disable_vblank(struct drm_device *dev, int pipe)
1767 {
1768 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1769 unsigned long irqflags;
1770
1771 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1772 if (dev_priv->info->gen == 3)
1773 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
1774
1775 i915_disable_pipestat(dev_priv, pipe,
1776 PIPE_VBLANK_INTERRUPT_ENABLE |
1777 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1778 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1779 }
1780
1781 static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
1782 {
1783 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1784 unsigned long irqflags;
1785
1786 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1787 ironlake_disable_display_irq(dev_priv, (pipe == 0) ?
1788 DE_PIPEA_VBLANK : DE_PIPEB_VBLANK);
1789 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1790 }
1791
1792 static void ivybridge_disable_vblank(struct drm_device *dev, int pipe)
1793 {
1794 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1795 unsigned long irqflags;
1796
1797 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1798 ironlake_disable_display_irq(dev_priv,
1799 DE_PIPEA_VBLANK_IVB << (pipe * 5));
1800 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1801 }
1802
1803 static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
1804 {
1805 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1806 unsigned long irqflags;
1807 u32 imr;
1808
1809 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1810 i915_disable_pipestat(dev_priv, pipe,
1811 PIPE_START_VBLANK_INTERRUPT_ENABLE);
1812 imr = I915_READ(VLV_IMR);
1813 if (pipe == 0)
1814 imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
1815 else
1816 imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
1817 I915_WRITE(VLV_IMR, imr);
1818 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1819 }
1820
1821 static u32
1822 ring_last_seqno(struct intel_ring_buffer *ring)
1823 {
1824 return list_entry(ring->request_list.prev,
1825 struct drm_i915_gem_request, list)->seqno;
1826 }
1827
1828 static bool
1829 ring_idle(struct intel_ring_buffer *ring, u32 seqno)
1830 {
1831 return (list_empty(&ring->request_list) ||
1832 i915_seqno_passed(seqno, ring_last_seqno(ring)));
1833 }
1834
1835 static struct intel_ring_buffer *
1836 semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
1837 {
1838 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1839 u32 cmd, ipehr, acthd, acthd_min;
1840
1841 ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
1842 if ((ipehr & ~(0x3 << 16)) !=
1843 (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
1844 return NULL;
1845
1846 /* ACTHD is likely pointing to the dword after the actual command,
1847 * so scan backwards until we find the MBOX.
1848 */
1849 acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
1850 acthd_min = max((int)acthd - 3 * 4, 0);
1851 do {
1852 cmd = ioread32(ring->virtual_start + acthd);
1853 if (cmd == ipehr)
1854 break;
1855
1856 acthd -= 4;
1857 if (acthd < acthd_min)
1858 return NULL;
1859 } while (1);
1860
1861 *seqno = ioread32(ring->virtual_start+acthd+4)+1;
1862 return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
1863 }
1864
1865 static int semaphore_passed(struct intel_ring_buffer *ring)
1866 {
1867 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1868 struct intel_ring_buffer *signaller;
1869 u32 seqno, ctl;
1870
1871 ring->hangcheck.deadlock = true;
1872
1873 signaller = semaphore_waits_for(ring, &seqno);
1874 if (signaller == NULL || signaller->hangcheck.deadlock)
1875 return -1;
1876
1877 /* cursory check for an unkickable deadlock */
1878 ctl = I915_READ_CTL(signaller);
1879 if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
1880 return -1;
1881
1882 return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
1883 }
1884
1885 static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
1886 {
1887 struct intel_ring_buffer *ring;
1888 int i;
1889
1890 for_each_ring(ring, dev_priv, i)
1891 ring->hangcheck.deadlock = false;
1892 }
1893
1894 static enum intel_ring_hangcheck_action
1895 ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
1896 {
1897 struct drm_device *dev = ring->dev;
1898 struct drm_i915_private *dev_priv = dev->dev_private;
1899 u32 tmp;
1900
1901 if (ring->hangcheck.acthd != acthd)
1902 return active;
1903
1904 if (IS_GEN2(dev))
1905 return hung;
1906
1907 /* Is the chip hanging on a WAIT_FOR_EVENT?
1908 * If so we can simply poke the RB_WAIT bit
1909 * and break the hang. This should work on
1910 * all but the second generation chipsets.
1911 */
1912 tmp = I915_READ_CTL(ring);
1913 if (tmp & RING_WAIT) {
1914 DRM_ERROR("Kicking stuck wait on %s\n",
1915 ring->name);
1916 I915_WRITE_CTL(ring, tmp);
1917 return kick;
1918 }
1919
1920 if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
1921 switch (semaphore_passed(ring)) {
1922 default:
1923 return hung;
1924 case 1:
1925 DRM_ERROR("Kicking stuck semaphore on %s\n",
1926 ring->name);
1927 I915_WRITE_CTL(ring, tmp);
1928 return kick;
1929 case 0:
1930 return wait;
1931 }
1932 }
1933
1934 return hung;
1935 }
1936
1937 /**
1938 * This is called when the chip hasn't reported back with completed
1939 * batchbuffers in a long time. We keep track per ring seqno progress and
1940 * if there are no progress, hangcheck score for that ring is increased.
1941 * Further, acthd is inspected to see if the ring is stuck. On stuck case
1942 * we kick the ring. If we see no progress on three subsequent calls
1943 * we assume chip is wedged and try to fix it by resetting the chip.
1944 */
1945 void i915_hangcheck_elapsed(unsigned long data)
1946 {
1947 struct drm_device *dev = (struct drm_device *)data;
1948 drm_i915_private_t *dev_priv = dev->dev_private;
1949 struct intel_ring_buffer *ring;
1950 int i;
1951 int busy_count = 0, rings_hung = 0;
1952 bool stuck[I915_NUM_RINGS] = { 0 };
1953 #define BUSY 1
1954 #define KICK 5
1955 #define HUNG 20
1956 #define FIRE 30
1957
1958 if (!i915_enable_hangcheck)
1959 return;
1960
1961 for_each_ring(ring, dev_priv, i) {
1962 u32 seqno, acthd;
1963 bool busy = true;
1964
1965 semaphore_clear_deadlocks(dev_priv);
1966
1967 seqno = ring->get_seqno(ring, false);
1968 acthd = intel_ring_get_active_head(ring);
1969
1970 if (ring->hangcheck.seqno == seqno) {
1971 if (ring_idle(ring, seqno)) {
1972 if (waitqueue_active(&ring->irq_queue)) {
1973 /* Issue a wake-up to catch stuck h/w. */
1974 DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
1975 ring->name);
1976 wake_up_all(&ring->irq_queue);
1977 ring->hangcheck.score += HUNG;
1978 } else
1979 busy = false;
1980 } else {
1981 int score;
1982
1983 /* We always increment the hangcheck score
1984 * if the ring is busy and still processing
1985 * the same request, so that no single request
1986 * can run indefinitely (such as a chain of
1987 * batches). The only time we do not increment
1988 * the hangcheck score on this ring, if this
1989 * ring is in a legitimate wait for another
1990 * ring. In that case the waiting ring is a
1991 * victim and we want to be sure we catch the
1992 * right culprit. Then every time we do kick
1993 * the ring, add a small increment to the
1994 * score so that we can catch a batch that is
1995 * being repeatedly kicked and so responsible
1996 * for stalling the machine.
1997 */
1998 ring->hangcheck.action = ring_stuck(ring,
1999 acthd);
2000
2001 switch (ring->hangcheck.action) {
2002 case wait:
2003 score = 0;
2004 break;
2005 case active:
2006 score = BUSY;
2007 break;
2008 case kick:
2009 score = KICK;
2010 break;
2011 case hung:
2012 score = HUNG;
2013 stuck[i] = true;
2014 break;
2015 }
2016 ring->hangcheck.score += score;
2017 }
2018 } else {
2019 /* Gradually reduce the count so that we catch DoS
2020 * attempts across multiple batches.
2021 */
2022 if (ring->hangcheck.score > 0)
2023 ring->hangcheck.score--;
2024 }
2025
2026 ring->hangcheck.seqno = seqno;
2027 ring->hangcheck.acthd = acthd;
2028 busy_count += busy;
2029 }
2030
2031 for_each_ring(ring, dev_priv, i) {
2032 if (ring->hangcheck.score > FIRE) {
2033 DRM_ERROR("%s on %s\n",
2034 stuck[i] ? "stuck" : "no progress",
2035 ring->name);
2036 rings_hung++;
2037 }
2038 }
2039
2040 if (rings_hung)
2041 return i915_handle_error(dev, true);
2042
2043 if (busy_count)
2044 /* Reset timer case chip hangs without another request
2045 * being added */
2046 i915_queue_hangcheck(dev);
2047 }
2048
2049 void i915_queue_hangcheck(struct drm_device *dev)
2050 {
2051 struct drm_i915_private *dev_priv = dev->dev_private;
2052 if (!i915_enable_hangcheck)
2053 return;
2054
2055 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
2056 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2057 }
2058
2059 static void ibx_irq_preinstall(struct drm_device *dev)
2060 {
2061 struct drm_i915_private *dev_priv = dev->dev_private;
2062
2063 if (HAS_PCH_NOP(dev))
2064 return;
2065
2066 /* south display irq */
2067 I915_WRITE(SDEIMR, 0xffffffff);
2068 /*
2069 * SDEIER is also touched by the interrupt handler to work around missed
2070 * PCH interrupts. Hence we can't update it after the interrupt handler
2071 * is enabled - instead we unconditionally enable all PCH interrupt
2072 * sources here, but then only unmask them as needed with SDEIMR.
2073 */
2074 I915_WRITE(SDEIER, 0xffffffff);
2075 POSTING_READ(SDEIER);
2076 }
2077
2078 static void gen5_gt_irq_preinstall(struct drm_device *dev)
2079 {
2080 struct drm_i915_private *dev_priv = dev->dev_private;
2081
2082 /* and GT */
2083 I915_WRITE(GTIMR, 0xffffffff);
2084 I915_WRITE(GTIER, 0x0);
2085 POSTING_READ(GTIER);
2086
2087 if (INTEL_INFO(dev)->gen >= 6) {
2088 /* and PM */
2089 I915_WRITE(GEN6_PMIMR, 0xffffffff);
2090 I915_WRITE(GEN6_PMIER, 0x0);
2091 POSTING_READ(GEN6_PMIER);
2092 }
2093 }
2094
2095 /* drm_dma.h hooks
2096 */
2097 static void ironlake_irq_preinstall(struct drm_device *dev)
2098 {
2099 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2100
2101 atomic_set(&dev_priv->irq_received, 0);
2102
2103 I915_WRITE(HWSTAM, 0xeffe);
2104
2105 I915_WRITE(DEIMR, 0xffffffff);
2106 I915_WRITE(DEIER, 0x0);
2107 POSTING_READ(DEIER);
2108
2109 gen5_gt_irq_preinstall(dev);
2110
2111 ibx_irq_preinstall(dev);
2112 }
2113
2114 static void valleyview_irq_preinstall(struct drm_device *dev)
2115 {
2116 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2117 int pipe;
2118
2119 atomic_set(&dev_priv->irq_received, 0);
2120
2121 /* VLV magic */
2122 I915_WRITE(VLV_IMR, 0);
2123 I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
2124 I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
2125 I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
2126
2127 /* and GT */
2128 I915_WRITE(GTIIR, I915_READ(GTIIR));
2129 I915_WRITE(GTIIR, I915_READ(GTIIR));
2130
2131 gen5_gt_irq_preinstall(dev);
2132
2133 I915_WRITE(DPINVGTT, 0xff);
2134
2135 I915_WRITE(PORT_HOTPLUG_EN, 0);
2136 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2137 for_each_pipe(pipe)
2138 I915_WRITE(PIPESTAT(pipe), 0xffff);
2139 I915_WRITE(VLV_IIR, 0xffffffff);
2140 I915_WRITE(VLV_IMR, 0xffffffff);
2141 I915_WRITE(VLV_IER, 0x0);
2142 POSTING_READ(VLV_IER);
2143 }
2144
2145 static void ibx_hpd_irq_setup(struct drm_device *dev)
2146 {
2147 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2148 struct drm_mode_config *mode_config = &dev->mode_config;
2149 struct intel_encoder *intel_encoder;
2150 u32 hotplug_irqs, hotplug, enabled_irqs = 0;
2151
2152 if (HAS_PCH_IBX(dev)) {
2153 hotplug_irqs = SDE_HOTPLUG_MASK;
2154 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2155 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2156 enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
2157 } else {
2158 hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
2159 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2160 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2161 enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
2162 }
2163
2164 ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
2165
2166 /*
2167 * Enable digital hotplug on the PCH, and configure the DP short pulse
2168 * duration to 2ms (which is the minimum in the Display Port spec)
2169 *
2170 * This register is the same on all known PCH chips.
2171 */
2172 hotplug = I915_READ(PCH_PORT_HOTPLUG);
2173 hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
2174 hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
2175 hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
2176 hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
2177 I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
2178 }
2179
2180 static void ibx_irq_postinstall(struct drm_device *dev)
2181 {
2182 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2183 u32 mask;
2184
2185 if (HAS_PCH_NOP(dev))
2186 return;
2187
2188 if (HAS_PCH_IBX(dev)) {
2189 mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2190 SDE_TRANSA_FIFO_UNDER | SDE_POISON;
2191 } else {
2192 mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
2193
2194 I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2195 }
2196
2197 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2198 I915_WRITE(SDEIMR, ~mask);
2199 }
2200
2201 static void gen5_gt_irq_postinstall(struct drm_device *dev)
2202 {
2203 struct drm_i915_private *dev_priv = dev->dev_private;
2204 u32 pm_irqs, gt_irqs;
2205
2206 pm_irqs = gt_irqs = 0;
2207
2208 dev_priv->gt_irq_mask = ~0;
2209 if (HAS_L3_GPU_CACHE(dev)) {
2210 /* L3 parity interrupt is always unmasked. */
2211 dev_priv->gt_irq_mask = ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2212 gt_irqs |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2213 }
2214
2215 gt_irqs |= GT_RENDER_USER_INTERRUPT;
2216 if (IS_GEN5(dev)) {
2217 gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
2218 ILK_BSD_USER_INTERRUPT;
2219 } else {
2220 gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
2221 }
2222
2223 I915_WRITE(GTIIR, I915_READ(GTIIR));
2224 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
2225 I915_WRITE(GTIER, gt_irqs);
2226 POSTING_READ(GTIER);
2227
2228 if (INTEL_INFO(dev)->gen >= 6) {
2229 pm_irqs |= GEN6_PM_RPS_EVENTS;
2230
2231 if (HAS_VEBOX(dev))
2232 pm_irqs |= PM_VEBOX_USER_INTERRUPT;
2233
2234 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2235 I915_WRITE(GEN6_PMIMR, 0xffffffff);
2236 I915_WRITE(GEN6_PMIER, pm_irqs);
2237 POSTING_READ(GEN6_PMIER);
2238 }
2239 }
2240
2241 static int ironlake_irq_postinstall(struct drm_device *dev)
2242 {
2243 unsigned long irqflags;
2244
2245 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2246 /* enable kind of interrupts always enabled */
2247 u32 display_mask = DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2248 DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
2249 DE_AUX_CHANNEL_A | DE_PIPEB_FIFO_UNDERRUN |
2250 DE_PIPEA_FIFO_UNDERRUN | DE_POISON;
2251
2252 dev_priv->irq_mask = ~display_mask;
2253
2254 /* should always can generate irq */
2255 I915_WRITE(DEIIR, I915_READ(DEIIR));
2256 I915_WRITE(DEIMR, dev_priv->irq_mask);
2257 I915_WRITE(DEIER, display_mask |
2258 DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT);
2259 POSTING_READ(DEIER);
2260
2261 gen5_gt_irq_postinstall(dev);
2262
2263 ibx_irq_postinstall(dev);
2264
2265 if (IS_IRONLAKE_M(dev)) {
2266 /* Enable PCU event interrupts
2267 *
2268 * spinlocking not required here for correctness since interrupt
2269 * setup is guaranteed to run in single-threaded context. But we
2270 * need it to make the assert_spin_locked happy. */
2271 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2272 ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
2273 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2274 }
2275
2276 return 0;
2277 }
2278
2279 static int ivybridge_irq_postinstall(struct drm_device *dev)
2280 {
2281 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2282 /* enable kind of interrupts always enabled */
2283 u32 display_mask =
2284 DE_MASTER_IRQ_CONTROL | DE_GSE_IVB | DE_PCH_EVENT_IVB |
2285 DE_PLANEC_FLIP_DONE_IVB |
2286 DE_PLANEB_FLIP_DONE_IVB |
2287 DE_PLANEA_FLIP_DONE_IVB |
2288 DE_AUX_CHANNEL_A_IVB |
2289 DE_ERR_INT_IVB;
2290
2291 dev_priv->irq_mask = ~display_mask;
2292
2293 /* should always can generate irq */
2294 I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2295 I915_WRITE(DEIIR, I915_READ(DEIIR));
2296 I915_WRITE(DEIMR, dev_priv->irq_mask);
2297 I915_WRITE(DEIER,
2298 display_mask |
2299 DE_PIPEC_VBLANK_IVB |
2300 DE_PIPEB_VBLANK_IVB |
2301 DE_PIPEA_VBLANK_IVB);
2302 POSTING_READ(DEIER);
2303
2304 gen5_gt_irq_postinstall(dev);
2305
2306 ibx_irq_postinstall(dev);
2307
2308 return 0;
2309 }
2310
2311 static int valleyview_irq_postinstall(struct drm_device *dev)
2312 {
2313 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2314 u32 enable_mask;
2315 u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV;
2316 unsigned long irqflags;
2317
2318 enable_mask = I915_DISPLAY_PORT_INTERRUPT;
2319 enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2320 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2321 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2322 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
2323
2324 /*
2325 *Leave vblank interrupts masked initially. enable/disable will
2326 * toggle them based on usage.
2327 */
2328 dev_priv->irq_mask = (~enable_mask) |
2329 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
2330 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
2331
2332 I915_WRITE(PORT_HOTPLUG_EN, 0);
2333 POSTING_READ(PORT_HOTPLUG_EN);
2334
2335 I915_WRITE(VLV_IMR, dev_priv->irq_mask);
2336 I915_WRITE(VLV_IER, enable_mask);
2337 I915_WRITE(VLV_IIR, 0xffffffff);
2338 I915_WRITE(PIPESTAT(0), 0xffff);
2339 I915_WRITE(PIPESTAT(1), 0xffff);
2340 POSTING_READ(VLV_IER);
2341
2342 /* Interrupt setup is already guaranteed to be single-threaded, this is
2343 * just to make the assert_spin_locked check happy. */
2344 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2345 i915_enable_pipestat(dev_priv, 0, pipestat_enable);
2346 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
2347 i915_enable_pipestat(dev_priv, 1, pipestat_enable);
2348 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2349
2350 I915_WRITE(VLV_IIR, 0xffffffff);
2351 I915_WRITE(VLV_IIR, 0xffffffff);
2352
2353 gen5_gt_irq_postinstall(dev);
2354
2355 /* ack & enable invalid PTE error interrupts */
2356 #if 0 /* FIXME: add support to irq handler for checking these bits */
2357 I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
2358 I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
2359 #endif
2360
2361 I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
2362
2363 return 0;
2364 }
2365
2366 static void valleyview_irq_uninstall(struct drm_device *dev)
2367 {
2368 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2369 int pipe;
2370
2371 if (!dev_priv)
2372 return;
2373
2374 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2375
2376 for_each_pipe(pipe)
2377 I915_WRITE(PIPESTAT(pipe), 0xffff);
2378
2379 I915_WRITE(HWSTAM, 0xffffffff);
2380 I915_WRITE(PORT_HOTPLUG_EN, 0);
2381 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2382 for_each_pipe(pipe)
2383 I915_WRITE(PIPESTAT(pipe), 0xffff);
2384 I915_WRITE(VLV_IIR, 0xffffffff);
2385 I915_WRITE(VLV_IMR, 0xffffffff);
2386 I915_WRITE(VLV_IER, 0x0);
2387 POSTING_READ(VLV_IER);
2388 }
2389
2390 static void ironlake_irq_uninstall(struct drm_device *dev)
2391 {
2392 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2393
2394 if (!dev_priv)
2395 return;
2396
2397 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2398
2399 I915_WRITE(HWSTAM, 0xffffffff);
2400
2401 I915_WRITE(DEIMR, 0xffffffff);
2402 I915_WRITE(DEIER, 0x0);
2403 I915_WRITE(DEIIR, I915_READ(DEIIR));
2404 if (IS_GEN7(dev))
2405 I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
2406
2407 I915_WRITE(GTIMR, 0xffffffff);
2408 I915_WRITE(GTIER, 0x0);
2409 I915_WRITE(GTIIR, I915_READ(GTIIR));
2410
2411 if (HAS_PCH_NOP(dev))
2412 return;
2413
2414 I915_WRITE(SDEIMR, 0xffffffff);
2415 I915_WRITE(SDEIER, 0x0);
2416 I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2417 if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2418 I915_WRITE(SERR_INT, I915_READ(SERR_INT));
2419 }
2420
2421 static void i8xx_irq_preinstall(struct drm_device * dev)
2422 {
2423 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2424 int pipe;
2425
2426 atomic_set(&dev_priv->irq_received, 0);
2427
2428 for_each_pipe(pipe)
2429 I915_WRITE(PIPESTAT(pipe), 0);
2430 I915_WRITE16(IMR, 0xffff);
2431 I915_WRITE16(IER, 0x0);
2432 POSTING_READ16(IER);
2433 }
2434
2435 static int i8xx_irq_postinstall(struct drm_device *dev)
2436 {
2437 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2438
2439 I915_WRITE16(EMR,
2440 ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2441
2442 /* Unmask the interrupts that we always want on. */
2443 dev_priv->irq_mask =
2444 ~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2445 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2446 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2447 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2448 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2449 I915_WRITE16(IMR, dev_priv->irq_mask);
2450
2451 I915_WRITE16(IER,
2452 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2453 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2454 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2455 I915_USER_INTERRUPT);
2456 POSTING_READ16(IER);
2457
2458 return 0;
2459 }
2460
2461 /*
2462 * Returns true when a page flip has completed.
2463 */
2464 static bool i8xx_handle_vblank(struct drm_device *dev,
2465 int pipe, u16 iir)
2466 {
2467 drm_i915_private_t *dev_priv = dev->dev_private;
2468 u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(pipe);
2469
2470 if (!drm_handle_vblank(dev, pipe))
2471 return false;
2472
2473 if ((iir & flip_pending) == 0)
2474 return false;
2475
2476 intel_prepare_page_flip(dev, pipe);
2477
2478 /* We detect FlipDone by looking for the change in PendingFlip from '1'
2479 * to '0' on the following vblank, i.e. IIR has the Pendingflip
2480 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
2481 * the flip is completed (no longer pending). Since this doesn't raise
2482 * an interrupt per se, we watch for the change at vblank.
2483 */
2484 if (I915_READ16(ISR) & flip_pending)
2485 return false;
2486
2487 intel_finish_page_flip(dev, pipe);
2488
2489 return true;
2490 }
2491
2492 static irqreturn_t i8xx_irq_handler(int irq, void *arg)
2493 {
2494 struct drm_device *dev = (struct drm_device *) arg;
2495 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2496 u16 iir, new_iir;
2497 u32 pipe_stats[2];
2498 unsigned long irqflags;
2499 int irq_received;
2500 int pipe;
2501 u16 flip_mask =
2502 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2503 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2504
2505 atomic_inc(&dev_priv->irq_received);
2506
2507 iir = I915_READ16(IIR);
2508 if (iir == 0)
2509 return IRQ_NONE;
2510
2511 while (iir & ~flip_mask) {
2512 /* Can't rely on pipestat interrupt bit in iir as it might
2513 * have been cleared after the pipestat interrupt was received.
2514 * It doesn't set the bit in iir again, but it still produces
2515 * interrupts (for non-MSI).
2516 */
2517 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2518 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2519 i915_handle_error(dev, false);
2520
2521 for_each_pipe(pipe) {
2522 int reg = PIPESTAT(pipe);
2523 pipe_stats[pipe] = I915_READ(reg);
2524
2525 /*
2526 * Clear the PIPE*STAT regs before the IIR
2527 */
2528 if (pipe_stats[pipe] & 0x8000ffff) {
2529 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2530 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2531 pipe_name(pipe));
2532 I915_WRITE(reg, pipe_stats[pipe]);
2533 irq_received = 1;
2534 }
2535 }
2536 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2537
2538 I915_WRITE16(IIR, iir & ~flip_mask);
2539 new_iir = I915_READ16(IIR); /* Flush posted writes */
2540
2541 i915_update_dri1_breadcrumb(dev);
2542
2543 if (iir & I915_USER_INTERRUPT)
2544 notify_ring(dev, &dev_priv->ring[RCS]);
2545
2546 if (pipe_stats[0] & PIPE_VBLANK_INTERRUPT_STATUS &&
2547 i8xx_handle_vblank(dev, 0, iir))
2548 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(0);
2549
2550 if (pipe_stats[1] & PIPE_VBLANK_INTERRUPT_STATUS &&
2551 i8xx_handle_vblank(dev, 1, iir))
2552 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(1);
2553
2554 iir = new_iir;
2555 }
2556
2557 return IRQ_HANDLED;
2558 }
2559
2560 static void i8xx_irq_uninstall(struct drm_device * dev)
2561 {
2562 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2563 int pipe;
2564
2565 for_each_pipe(pipe) {
2566 /* Clear enable bits; then clear status bits */
2567 I915_WRITE(PIPESTAT(pipe), 0);
2568 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2569 }
2570 I915_WRITE16(IMR, 0xffff);
2571 I915_WRITE16(IER, 0x0);
2572 I915_WRITE16(IIR, I915_READ16(IIR));
2573 }
2574
2575 static void i915_irq_preinstall(struct drm_device * dev)
2576 {
2577 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2578 int pipe;
2579
2580 atomic_set(&dev_priv->irq_received, 0);
2581
2582 if (I915_HAS_HOTPLUG(dev)) {
2583 I915_WRITE(PORT_HOTPLUG_EN, 0);
2584 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2585 }
2586
2587 I915_WRITE16(HWSTAM, 0xeffe);
2588 for_each_pipe(pipe)
2589 I915_WRITE(PIPESTAT(pipe), 0);
2590 I915_WRITE(IMR, 0xffffffff);
2591 I915_WRITE(IER, 0x0);
2592 POSTING_READ(IER);
2593 }
2594
2595 static int i915_irq_postinstall(struct drm_device *dev)
2596 {
2597 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2598 u32 enable_mask;
2599
2600 I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
2601
2602 /* Unmask the interrupts that we always want on. */
2603 dev_priv->irq_mask =
2604 ~(I915_ASLE_INTERRUPT |
2605 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2606 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2607 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2608 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2609 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2610
2611 enable_mask =
2612 I915_ASLE_INTERRUPT |
2613 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2614 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2615 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
2616 I915_USER_INTERRUPT;
2617
2618 if (I915_HAS_HOTPLUG(dev)) {
2619 I915_WRITE(PORT_HOTPLUG_EN, 0);
2620 POSTING_READ(PORT_HOTPLUG_EN);
2621
2622 /* Enable in IER... */
2623 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
2624 /* and unmask in IMR */
2625 dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
2626 }
2627
2628 I915_WRITE(IMR, dev_priv->irq_mask);
2629 I915_WRITE(IER, enable_mask);
2630 POSTING_READ(IER);
2631
2632 i915_enable_asle_pipestat(dev);
2633
2634 return 0;
2635 }
2636
2637 /*
2638 * Returns true when a page flip has completed.
2639 */
2640 static bool i915_handle_vblank(struct drm_device *dev,
2641 int plane, int pipe, u32 iir)
2642 {
2643 drm_i915_private_t *dev_priv = dev->dev_private;
2644 u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
2645
2646 if (!drm_handle_vblank(dev, pipe))
2647 return false;
2648
2649 if ((iir & flip_pending) == 0)
2650 return false;
2651
2652 intel_prepare_page_flip(dev, plane);
2653
2654 /* We detect FlipDone by looking for the change in PendingFlip from '1'
2655 * to '0' on the following vblank, i.e. IIR has the Pendingflip
2656 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
2657 * the flip is completed (no longer pending). Since this doesn't raise
2658 * an interrupt per se, we watch for the change at vblank.
2659 */
2660 if (I915_READ(ISR) & flip_pending)
2661 return false;
2662
2663 intel_finish_page_flip(dev, pipe);
2664
2665 return true;
2666 }
2667
2668 static irqreturn_t i915_irq_handler(int irq, void *arg)
2669 {
2670 struct drm_device *dev = (struct drm_device *) arg;
2671 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2672 u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
2673 unsigned long irqflags;
2674 u32 flip_mask =
2675 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2676 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2677 int pipe, ret = IRQ_NONE;
2678
2679 atomic_inc(&dev_priv->irq_received);
2680
2681 iir = I915_READ(IIR);
2682 do {
2683 bool irq_received = (iir & ~flip_mask) != 0;
2684 bool blc_event = false;
2685
2686 /* Can't rely on pipestat interrupt bit in iir as it might
2687 * have been cleared after the pipestat interrupt was received.
2688 * It doesn't set the bit in iir again, but it still produces
2689 * interrupts (for non-MSI).
2690 */
2691 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2692 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2693 i915_handle_error(dev, false);
2694
2695 for_each_pipe(pipe) {
2696 int reg = PIPESTAT(pipe);
2697 pipe_stats[pipe] = I915_READ(reg);
2698
2699 /* Clear the PIPE*STAT regs before the IIR */
2700 if (pipe_stats[pipe] & 0x8000ffff) {
2701 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2702 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2703 pipe_name(pipe));
2704 I915_WRITE(reg, pipe_stats[pipe]);
2705 irq_received = true;
2706 }
2707 }
2708 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2709
2710 if (!irq_received)
2711 break;
2712
2713 /* Consume port. Then clear IIR or we'll miss events */
2714 if ((I915_HAS_HOTPLUG(dev)) &&
2715 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
2716 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2717 u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
2718
2719 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2720 hotplug_status);
2721
2722 intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
2723
2724 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2725 POSTING_READ(PORT_HOTPLUG_STAT);
2726 }
2727
2728 I915_WRITE(IIR, iir & ~flip_mask);
2729 new_iir = I915_READ(IIR); /* Flush posted writes */
2730
2731 if (iir & I915_USER_INTERRUPT)
2732 notify_ring(dev, &dev_priv->ring[RCS]);
2733
2734 for_each_pipe(pipe) {
2735 int plane = pipe;
2736 if (IS_MOBILE(dev))
2737 plane = !plane;
2738
2739 if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
2740 i915_handle_vblank(dev, plane, pipe, iir))
2741 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
2742
2743 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2744 blc_event = true;
2745 }
2746
2747 if (blc_event || (iir & I915_ASLE_INTERRUPT))
2748 intel_opregion_asle_intr(dev);
2749
2750 /* With MSI, interrupts are only generated when iir
2751 * transitions from zero to nonzero. If another bit got
2752 * set while we were handling the existing iir bits, then
2753 * we would never get another interrupt.
2754 *
2755 * This is fine on non-MSI as well, as if we hit this path
2756 * we avoid exiting the interrupt handler only to generate
2757 * another one.
2758 *
2759 * Note that for MSI this could cause a stray interrupt report
2760 * if an interrupt landed in the time between writing IIR and
2761 * the posting read. This should be rare enough to never
2762 * trigger the 99% of 100,000 interrupts test for disabling
2763 * stray interrupts.
2764 */
2765 ret = IRQ_HANDLED;
2766 iir = new_iir;
2767 } while (iir & ~flip_mask);
2768
2769 i915_update_dri1_breadcrumb(dev);
2770
2771 return ret;
2772 }
2773
2774 static void i915_irq_uninstall(struct drm_device * dev)
2775 {
2776 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2777 int pipe;
2778
2779 del_timer_sync(&dev_priv->hotplug_reenable_timer);
2780
2781 if (I915_HAS_HOTPLUG(dev)) {
2782 I915_WRITE(PORT_HOTPLUG_EN, 0);
2783 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2784 }
2785
2786 I915_WRITE16(HWSTAM, 0xffff);
2787 for_each_pipe(pipe) {
2788 /* Clear enable bits; then clear status bits */
2789 I915_WRITE(PIPESTAT(pipe), 0);
2790 I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
2791 }
2792 I915_WRITE(IMR, 0xffffffff);
2793 I915_WRITE(IER, 0x0);
2794
2795 I915_WRITE(IIR, I915_READ(IIR));
2796 }
2797
2798 static void i965_irq_preinstall(struct drm_device * dev)
2799 {
2800 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2801 int pipe;
2802
2803 atomic_set(&dev_priv->irq_received, 0);
2804
2805 I915_WRITE(PORT_HOTPLUG_EN, 0);
2806 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
2807
2808 I915_WRITE(HWSTAM, 0xeffe);
2809 for_each_pipe(pipe)
2810 I915_WRITE(PIPESTAT(pipe), 0);
2811 I915_WRITE(IMR, 0xffffffff);
2812 I915_WRITE(IER, 0x0);
2813 POSTING_READ(IER);
2814 }
2815
2816 static int i965_irq_postinstall(struct drm_device *dev)
2817 {
2818 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2819 u32 enable_mask;
2820 u32 error_mask;
2821 unsigned long irqflags;
2822
2823 /* Unmask the interrupts that we always want on. */
2824 dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
2825 I915_DISPLAY_PORT_INTERRUPT |
2826 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2827 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2828 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2829 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
2830 I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
2831
2832 enable_mask = ~dev_priv->irq_mask;
2833 enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2834 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
2835 enable_mask |= I915_USER_INTERRUPT;
2836
2837 if (IS_G4X(dev))
2838 enable_mask |= I915_BSD_USER_INTERRUPT;
2839
2840 /* Interrupt setup is already guaranteed to be single-threaded, this is
2841 * just to make the assert_spin_locked check happy. */
2842 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2843 i915_enable_pipestat(dev_priv, 0, PIPE_GMBUS_EVENT_ENABLE);
2844 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2845
2846 /*
2847 * Enable some error detection, note the instruction error mask
2848 * bit is reserved, so we leave it masked.
2849 */
2850 if (IS_G4X(dev)) {
2851 error_mask = ~(GM45_ERROR_PAGE_TABLE |
2852 GM45_ERROR_MEM_PRIV |
2853 GM45_ERROR_CP_PRIV |
2854 I915_ERROR_MEMORY_REFRESH);
2855 } else {
2856 error_mask = ~(I915_ERROR_PAGE_TABLE |
2857 I915_ERROR_MEMORY_REFRESH);
2858 }
2859 I915_WRITE(EMR, error_mask);
2860
2861 I915_WRITE(IMR, dev_priv->irq_mask);
2862 I915_WRITE(IER, enable_mask);
2863 POSTING_READ(IER);
2864
2865 I915_WRITE(PORT_HOTPLUG_EN, 0);
2866 POSTING_READ(PORT_HOTPLUG_EN);
2867
2868 i915_enable_asle_pipestat(dev);
2869
2870 return 0;
2871 }
2872
2873 static void i915_hpd_irq_setup(struct drm_device *dev)
2874 {
2875 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2876 struct drm_mode_config *mode_config = &dev->mode_config;
2877 struct intel_encoder *intel_encoder;
2878 u32 hotplug_en;
2879
2880 assert_spin_locked(&dev_priv->irq_lock);
2881
2882 if (I915_HAS_HOTPLUG(dev)) {
2883 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
2884 hotplug_en &= ~HOTPLUG_INT_EN_MASK;
2885 /* Note HDMI and DP share hotplug bits */
2886 /* enable bits are the same for all generations */
2887 list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2888 if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2889 hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
2890 /* Programming the CRT detection parameters tends
2891 to generate a spurious hotplug event about three
2892 seconds later. So just do it once.
2893 */
2894 if (IS_G4X(dev))
2895 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
2896 hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
2897 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
2898
2899 /* Ignore TV since it's buggy */
2900 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
2901 }
2902 }
2903
2904 static irqreturn_t i965_irq_handler(int irq, void *arg)
2905 {
2906 struct drm_device *dev = (struct drm_device *) arg;
2907 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2908 u32 iir, new_iir;
2909 u32 pipe_stats[I915_MAX_PIPES];
2910 unsigned long irqflags;
2911 int irq_received;
2912 int ret = IRQ_NONE, pipe;
2913 u32 flip_mask =
2914 I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
2915 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
2916
2917 atomic_inc(&dev_priv->irq_received);
2918
2919 iir = I915_READ(IIR);
2920
2921 for (;;) {
2922 bool blc_event = false;
2923
2924 irq_received = (iir & ~flip_mask) != 0;
2925
2926 /* Can't rely on pipestat interrupt bit in iir as it might
2927 * have been cleared after the pipestat interrupt was received.
2928 * It doesn't set the bit in iir again, but it still produces
2929 * interrupts (for non-MSI).
2930 */
2931 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2932 if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
2933 i915_handle_error(dev, false);
2934
2935 for_each_pipe(pipe) {
2936 int reg = PIPESTAT(pipe);
2937 pipe_stats[pipe] = I915_READ(reg);
2938
2939 /*
2940 * Clear the PIPE*STAT regs before the IIR
2941 */
2942 if (pipe_stats[pipe] & 0x8000ffff) {
2943 if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
2944 DRM_DEBUG_DRIVER("pipe %c underrun\n",
2945 pipe_name(pipe));
2946 I915_WRITE(reg, pipe_stats[pipe]);
2947 irq_received = 1;
2948 }
2949 }
2950 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2951
2952 if (!irq_received)
2953 break;
2954
2955 ret = IRQ_HANDLED;
2956
2957 /* Consume port. Then clear IIR or we'll miss events */
2958 if (iir & I915_DISPLAY_PORT_INTERRUPT) {
2959 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
2960 u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
2961 HOTPLUG_INT_STATUS_G4X :
2962 HOTPLUG_INT_STATUS_I915);
2963
2964 DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2965 hotplug_status);
2966
2967 intel_hpd_irq_handler(dev, hotplug_trigger,
2968 IS_G4X(dev) ? hpd_status_gen4 : hpd_status_i915);
2969
2970 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
2971 I915_READ(PORT_HOTPLUG_STAT);
2972 }
2973
2974 I915_WRITE(IIR, iir & ~flip_mask);
2975 new_iir = I915_READ(IIR); /* Flush posted writes */
2976
2977 if (iir & I915_USER_INTERRUPT)
2978 notify_ring(dev, &dev_priv->ring[RCS]);
2979 if (iir & I915_BSD_USER_INTERRUPT)
2980 notify_ring(dev, &dev_priv->ring[VCS]);
2981
2982 for_each_pipe(pipe) {
2983 if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
2984 i915_handle_vblank(dev, pipe, pipe, iir))
2985 flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
2986
2987 if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
2988 blc_event = true;
2989 }
2990
2991
2992 if (blc_event || (iir & I915_ASLE_INTERRUPT))
2993 intel_opregion_asle_intr(dev);
2994
2995 if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
2996 gmbus_irq_handler(dev);
2997
2998 /* With MSI, interrupts are only generated when iir
2999 * transitions from zero to nonzero. If another bit got
3000 * set while we were handling the existing iir bits, then
3001 * we would never get another interrupt.
3002 *
3003 * This is fine on non-MSI as well, as if we hit this path
3004 * we avoid exiting the interrupt handler only to generate
3005 * another one.
3006 *
3007 * Note that for MSI this could cause a stray interrupt report
3008 * if an interrupt landed in the time between writing IIR and
3009 * the posting read. This should be rare enough to never
3010 * trigger the 99% of 100,000 interrupts test for disabling
3011 * stray interrupts.
3012 */
3013 iir = new_iir;
3014 }
3015
3016 i915_update_dri1_breadcrumb(dev);
3017
3018 return ret;
3019 }
3020
3021 static void i965_irq_uninstall(struct drm_device * dev)
3022 {
3023 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3024 int pipe;
3025
3026 if (!dev_priv)
3027 return;
3028
3029 del_timer_sync(&dev_priv->hotplug_reenable_timer);
3030
3031 I915_WRITE(PORT_HOTPLUG_EN, 0);
3032 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3033
3034 I915_WRITE(HWSTAM, 0xffffffff);
3035 for_each_pipe(pipe)
3036 I915_WRITE(PIPESTAT(pipe), 0);
3037 I915_WRITE(IMR, 0xffffffff);
3038 I915_WRITE(IER, 0x0);
3039
3040 for_each_pipe(pipe)
3041 I915_WRITE(PIPESTAT(pipe),
3042 I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3043 I915_WRITE(IIR, I915_READ(IIR));
3044 }
3045
3046 static void i915_reenable_hotplug_timer_func(unsigned long data)
3047 {
3048 drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3049 struct drm_device *dev = dev_priv->dev;
3050 struct drm_mode_config *mode_config = &dev->mode_config;
3051 unsigned long irqflags;
3052 int i;
3053
3054 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3055 for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3056 struct drm_connector *connector;
3057
3058 if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3059 continue;
3060
3061 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3062
3063 list_for_each_entry(connector, &mode_config->connector_list, head) {
3064 struct intel_connector *intel_connector = to_intel_connector(connector);
3065
3066 if (intel_connector->encoder->hpd_pin == i) {
3067 if (connector->polled != intel_connector->polled)
3068 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3069 drm_get_connector_name(connector));
3070 connector->polled = intel_connector->polled;
3071 if (!connector->polled)
3072 connector->polled = DRM_CONNECTOR_POLL_HPD;
3073 }
3074 }
3075 }
3076 if (dev_priv->display.hpd_irq_setup)
3077 dev_priv->display.hpd_irq_setup(dev);
3078 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3079 }
3080
3081 void intel_irq_init(struct drm_device *dev)
3082 {
3083 struct drm_i915_private *dev_priv = dev->dev_private;
3084
3085 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
3086 INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3087 INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3088 INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
3089
3090 setup_timer(&dev_priv->gpu_error.hangcheck_timer,
3091 i915_hangcheck_elapsed,
3092 (unsigned long) dev);
3093 setup_timer(&dev_priv->hotplug_reenable_timer, i915_reenable_hotplug_timer_func,
3094 (unsigned long) dev_priv);
3095
3096 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
3097
3098 dev->driver->get_vblank_counter = i915_get_vblank_counter;
3099 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3100 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3101 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3102 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3103 }
3104
3105 if (drm_core_check_feature(dev, DRIVER_MODESET))
3106 dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3107 else
3108 dev->driver->get_vblank_timestamp = NULL;
3109 dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3110
3111 if (IS_VALLEYVIEW(dev)) {
3112 dev->driver->irq_handler = valleyview_irq_handler;
3113 dev->driver->irq_preinstall = valleyview_irq_preinstall;
3114 dev->driver->irq_postinstall = valleyview_irq_postinstall;
3115 dev->driver->irq_uninstall = valleyview_irq_uninstall;
3116 dev->driver->enable_vblank = valleyview_enable_vblank;
3117 dev->driver->disable_vblank = valleyview_disable_vblank;
3118 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3119 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
3120 /* Share uninstall handlers with ILK/SNB */
3121 dev->driver->irq_handler = ivybridge_irq_handler;
3122 dev->driver->irq_preinstall = ironlake_irq_preinstall;
3123 dev->driver->irq_postinstall = ivybridge_irq_postinstall;
3124 dev->driver->irq_uninstall = ironlake_irq_uninstall;
3125 dev->driver->enable_vblank = ivybridge_enable_vblank;
3126 dev->driver->disable_vblank = ivybridge_disable_vblank;
3127 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3128 } else if (HAS_PCH_SPLIT(dev)) {
3129 dev->driver->irq_handler = ironlake_irq_handler;
3130 dev->driver->irq_preinstall = ironlake_irq_preinstall;
3131 dev->driver->irq_postinstall = ironlake_irq_postinstall;
3132 dev->driver->irq_uninstall = ironlake_irq_uninstall;
3133 dev->driver->enable_vblank = ironlake_enable_vblank;
3134 dev->driver->disable_vblank = ironlake_disable_vblank;
3135 dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3136 } else {
3137 if (INTEL_INFO(dev)->gen == 2) {
3138 dev->driver->irq_preinstall = i8xx_irq_preinstall;
3139 dev->driver->irq_postinstall = i8xx_irq_postinstall;
3140 dev->driver->irq_handler = i8xx_irq_handler;
3141 dev->driver->irq_uninstall = i8xx_irq_uninstall;
3142 } else if (INTEL_INFO(dev)->gen == 3) {
3143 dev->driver->irq_preinstall = i915_irq_preinstall;
3144 dev->driver->irq_postinstall = i915_irq_postinstall;
3145 dev->driver->irq_uninstall = i915_irq_uninstall;
3146 dev->driver->irq_handler = i915_irq_handler;
3147 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3148 } else {
3149 dev->driver->irq_preinstall = i965_irq_preinstall;
3150 dev->driver->irq_postinstall = i965_irq_postinstall;
3151 dev->driver->irq_uninstall = i965_irq_uninstall;
3152 dev->driver->irq_handler = i965_irq_handler;
3153 dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3154 }
3155 dev->driver->enable_vblank = i915_enable_vblank;
3156 dev->driver->disable_vblank = i915_disable_vblank;
3157 }
3158 }
3159
3160 void intel_hpd_init(struct drm_device *dev)
3161 {
3162 struct drm_i915_private *dev_priv = dev->dev_private;
3163 struct drm_mode_config *mode_config = &dev->mode_config;
3164 struct drm_connector *connector;
3165 unsigned long irqflags;
3166 int i;
3167
3168 for (i = 1; i < HPD_NUM_PINS; i++) {
3169 dev_priv->hpd_stats[i].hpd_cnt = 0;
3170 dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3171 }
3172 list_for_each_entry(connector, &mode_config->connector_list, head) {
3173 struct intel_connector *intel_connector = to_intel_connector(connector);
3174 connector->polled = intel_connector->polled;
3175 if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3176 connector->polled = DRM_CONNECTOR_POLL_HPD;
3177 }
3178
3179 /* Interrupt setup is already guaranteed to be single-threaded, this is
3180 * just to make the assert_spin_locked checks happy. */
3181 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3182 if (dev_priv->display.hpd_irq_setup)
3183 dev_priv->display.hpd_irq_setup(dev);
3184 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3185 }
This page took 0.162033 seconds and 6 git commands to generate.