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