blob: ee7ce7b78cf79b9dd0d18033866c83fd2d060d1e [file] [log] [blame]
Dave Airlie0d6aa602006-01-02 20:14:23 +11001/* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "drmP.h"
30#include "drm.h"
31#include "i915_drm.h"
32#include "i915_drv.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define MAX_NOPID ((u32)~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Keith Packard7c463582008-11-04 02:03:27 -080037/**
38 * Interrupts that are always left unmasked.
39 *
40 * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
41 * we leave them always unmasked in IMR and then control enabling them through
42 * PIPESTAT alone.
43 */
44#define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT | \
45 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
46 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT)
47
48/** Interrupts that we mask and unmask at runtime. */
49#define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
50
Jesse Barnes79e53942008-11-07 14:24:08 -080051#define I915_PIPE_VBLANK_STATUS (PIPE_START_VBLANK_INTERRUPT_STATUS |\
52 PIPE_VBLANK_INTERRUPT_STATUS)
53
54#define I915_PIPE_VBLANK_ENABLE (PIPE_START_VBLANK_INTERRUPT_ENABLE |\
55 PIPE_VBLANK_INTERRUPT_ENABLE)
56
57#define DRM_I915_VBLANK_PIPE_ALL (DRM_I915_VBLANK_PIPE_A | \
58 DRM_I915_VBLANK_PIPE_B)
59
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +010060void
Eric Anholted4cb412008-07-29 12:10:39 -070061i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
62{
63 if ((dev_priv->irq_mask_reg & mask) != 0) {
64 dev_priv->irq_mask_reg &= ~mask;
65 I915_WRITE(IMR, dev_priv->irq_mask_reg);
66 (void) I915_READ(IMR);
67 }
68}
69
70static inline void
71i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
72{
73 if ((dev_priv->irq_mask_reg & mask) != mask) {
74 dev_priv->irq_mask_reg |= mask;
75 I915_WRITE(IMR, dev_priv->irq_mask_reg);
76 (void) I915_READ(IMR);
77 }
78}
79
Keith Packard7c463582008-11-04 02:03:27 -080080static inline u32
81i915_pipestat(int pipe)
82{
83 if (pipe == 0)
84 return PIPEASTAT;
85 if (pipe == 1)
86 return PIPEBSTAT;
Andrew Morton9c84ba42008-12-01 13:14:08 -080087 BUG();
Keith Packard7c463582008-11-04 02:03:27 -080088}
89
90void
91i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
92{
93 if ((dev_priv->pipestat[pipe] & mask) != mask) {
94 u32 reg = i915_pipestat(pipe);
95
96 dev_priv->pipestat[pipe] |= mask;
97 /* Enable the interrupt, clear any pending status */
98 I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
99 (void) I915_READ(reg);
100 }
101}
102
103void
104i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
105{
106 if ((dev_priv->pipestat[pipe] & mask) != 0) {
107 u32 reg = i915_pipestat(pipe);
108
109 dev_priv->pipestat[pipe] &= ~mask;
110 I915_WRITE(reg, dev_priv->pipestat[pipe]);
111 (void) I915_READ(reg);
112 }
113}
114
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000115/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700116 * i915_pipe_enabled - check if a pipe is enabled
117 * @dev: DRM device
118 * @pipe: pipe to check
119 *
120 * Reading certain registers when the pipe is disabled can hang the chip.
121 * Use this routine to make sure the PLL is running and the pipe is active
122 * before reading such registers if unsure.
123 */
124static int
125i915_pipe_enabled(struct drm_device *dev, int pipe)
126{
127 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
128 unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
129
130 if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
131 return 1;
132
133 return 0;
134}
135
Keith Packard42f52ef2008-10-18 19:39:29 -0700136/* Called from drm generic code, passed a 'crtc', which
137 * we use as a pipe index
138 */
139u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700140{
141 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
142 unsigned long high_frame;
143 unsigned long low_frame;
144 u32 high1, high2, low, count;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700145
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700146 high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
147 low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
148
149 if (!i915_pipe_enabled(dev, pipe)) {
150 DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
151 return 0;
152 }
153
154 /*
155 * High & low register fields aren't synchronized, so make sure
156 * we get a low value that's stable across two reads of the high
157 * register.
158 */
159 do {
160 high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
161 PIPE_FRAME_HIGH_SHIFT);
162 low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
163 PIPE_FRAME_LOW_SHIFT);
164 high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
165 PIPE_FRAME_HIGH_SHIFT);
166 } while (high1 != high2);
167
168 count = (high1 << 8) | low;
169
170 return count;
171}
172
Jesse Barnes9880b7a2009-02-06 10:22:41 -0800173u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
174{
175 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
176 int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
177
178 if (!i915_pipe_enabled(dev, pipe)) {
179 DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
180 return 0;
181 }
182
183 return I915_READ(reg);
184}
185
Jesse Barnes5ca58282009-03-31 14:11:15 -0700186/*
187 * Handle hotplug events outside the interrupt handler proper.
188 */
189static void i915_hotplug_work_func(struct work_struct *work)
190{
191 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
192 hotplug_work);
193 struct drm_device *dev = dev_priv->dev;
194
195 /* Just fire off a uevent and let userspace tell us what to do */
196 drm_sysfs_hotplug_event(dev);
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
200{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000201 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000203 struct drm_i915_master_private *master_priv;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800204 u32 iir, new_iir;
205 u32 pipea_stats, pipeb_stats;
Keith Packard05eff842008-11-19 14:03:05 -0800206 u32 vblank_status;
207 u32 vblank_enable;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700208 int vblank = 0;
Keith Packard7c463582008-11-04 02:03:27 -0800209 unsigned long irqflags;
Keith Packard05eff842008-11-19 14:03:05 -0800210 int irq_received;
211 int ret = IRQ_NONE;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000212
Eric Anholt630681d2008-10-06 15:14:12 -0700213 atomic_inc(&dev_priv->irq_received);
214
Eric Anholted4cb412008-07-29 12:10:39 -0700215 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000216
Keith Packard05eff842008-11-19 14:03:05 -0800217 if (IS_I965G(dev)) {
218 vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
219 vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
220 } else {
221 vblank_status = I915_VBLANK_INTERRUPT_STATUS;
222 vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Keith Packard05eff842008-11-19 14:03:05 -0800225 for (;;) {
226 irq_received = iir != 0;
227
228 /* Can't rely on pipestat interrupt bit in iir as it might
229 * have been cleared after the pipestat interrupt was received.
230 * It doesn't set the bit in iir again, but it still produces
231 * interrupts (for non-MSI).
232 */
233 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
234 pipea_stats = I915_READ(PIPEASTAT);
235 pipeb_stats = I915_READ(PIPEBSTAT);
Jesse Barnes79e53942008-11-07 14:24:08 -0800236
Eric Anholtcdfbc412008-11-04 15:50:30 -0800237 /*
238 * Clear the PIPE(A|B)STAT regs before the IIR
239 */
Keith Packard05eff842008-11-19 14:03:05 -0800240 if (pipea_stats & 0x8000ffff) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800241 I915_WRITE(PIPEASTAT, pipea_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800242 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800243 }
Keith Packard7c463582008-11-04 02:03:27 -0800244
Keith Packard05eff842008-11-19 14:03:05 -0800245 if (pipeb_stats & 0x8000ffff) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800246 I915_WRITE(PIPEBSTAT, pipeb_stats);
Keith Packard05eff842008-11-19 14:03:05 -0800247 irq_received = 1;
Eric Anholtcdfbc412008-11-04 15:50:30 -0800248 }
Keith Packard05eff842008-11-19 14:03:05 -0800249 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
250
251 if (!irq_received)
252 break;
253
254 ret = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Jesse Barnes5ca58282009-03-31 14:11:15 -0700256 /* Consume port. Then clear IIR or we'll miss events */
257 if ((I915_HAS_HOTPLUG(dev)) &&
258 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
259 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
260
261 DRM_DEBUG("hotplug event received, stat 0x%08x\n",
262 hotplug_status);
263 if (hotplug_status & dev_priv->hotplug_supported_mask)
264 schedule_work(&dev_priv->hotplug_work);
265
266 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
267 I915_READ(PORT_HOTPLUG_STAT);
268 }
269
Eric Anholtcdfbc412008-11-04 15:50:30 -0800270 I915_WRITE(IIR, iir);
271 new_iir = I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100272
Dave Airlie7c1c2872008-11-28 14:22:24 +1000273 if (dev->primary->master) {
274 master_priv = dev->primary->master->driver_priv;
275 if (master_priv->sarea_priv)
276 master_priv->sarea_priv->last_dispatch =
277 READ_BREADCRUMB(dev_priv);
278 }
Keith Packard7c463582008-11-04 02:03:27 -0800279
Eric Anholtcdfbc412008-11-04 15:50:30 -0800280 if (iir & I915_USER_INTERRUPT) {
281 dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
282 DRM_WAKEUP(&dev_priv->irq_queue);
283 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700284
Keith Packard05eff842008-11-19 14:03:05 -0800285 if (pipea_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800286 vblank++;
287 drm_handle_vblank(dev, 0);
288 }
Eric Anholt673a3942008-07-30 12:06:12 -0700289
Keith Packard05eff842008-11-19 14:03:05 -0800290 if (pipeb_stats & vblank_status) {
Eric Anholtcdfbc412008-11-04 15:50:30 -0800291 vblank++;
292 drm_handle_vblank(dev, 1);
293 }
Keith Packard7c463582008-11-04 02:03:27 -0800294
Eric Anholtcdfbc412008-11-04 15:50:30 -0800295 if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
296 (iir & I915_ASLE_INTERRUPT))
297 opregion_asle_intr(dev);
Keith Packard7c463582008-11-04 02:03:27 -0800298
Eric Anholtcdfbc412008-11-04 15:50:30 -0800299 /* With MSI, interrupts are only generated when iir
300 * transitions from zero to nonzero. If another bit got
301 * set while we were handling the existing iir bits, then
302 * we would never get another interrupt.
303 *
304 * This is fine on non-MSI as well, as if we hit this path
305 * we avoid exiting the interrupt handler only to generate
306 * another one.
307 *
308 * Note that for MSI this could cause a stray interrupt report
309 * if an interrupt landed in the time between writing IIR and
310 * the posting read. This should be rare enough to never
311 * trigger the 99% of 100,000 interrupts test for disabling
312 * stray interrupts.
313 */
314 iir = new_iir;
Keith Packard05eff842008-11-19 14:03:05 -0800315 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700316
Keith Packard05eff842008-11-19 14:03:05 -0800317 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Dave Airlieaf6061a2008-05-07 12:15:39 +1000320static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000323 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 RING_LOCALS;
325
326 i915_kernel_lost_context(dev);
327
Márton Németh3e684ea2008-01-24 15:58:57 +1000328 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400330 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000331 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400332 dev_priv->counter = 1;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000333 if (master_priv->sarea_priv)
334 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000335
Keith Packard0baf8232008-11-08 11:44:14 +1000336 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700337 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000338 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000339 OUT_RING(dev_priv->counter);
Jesse Barnes585fb112008-07-29 11:54:06 -0700340 OUT_RING(MI_USER_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 ADVANCE_LP_RING();
Dave Airliebc5f4522007-11-05 12:50:58 +1000342
Alan Hourihanec29b6692006-08-12 16:29:24 +1000343 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Eric Anholt673a3942008-07-30 12:06:12 -0700346void i915_user_irq_get(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700347{
348 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700349 unsigned long irqflags;
Eric Anholted4cb412008-07-29 12:10:39 -0700350
Keith Packarde9d21d72008-10-16 11:31:38 -0700351 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700352 if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1))
353 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
Keith Packarde9d21d72008-10-16 11:31:38 -0700354 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700355}
356
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700357void i915_user_irq_put(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700358{
359 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700360 unsigned long irqflags;
Eric Anholted4cb412008-07-29 12:10:39 -0700361
Keith Packarde9d21d72008-10-16 11:31:38 -0700362 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700363 BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
364 if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0))
365 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
Keith Packarde9d21d72008-10-16 11:31:38 -0700366 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Eric Anholted4cb412008-07-29 12:10:39 -0700367}
368
Dave Airlie84b1fd12007-07-11 15:53:27 +1000369static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000372 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int ret = 0;
374
Márton Németh3e684ea2008-01-24 15:58:57 +1000375 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 READ_BREADCRUMB(dev_priv));
377
Eric Anholted4cb412008-07-29 12:10:39 -0700378 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000379 if (master_priv->sarea_priv)
380 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -0700382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dave Airlie7c1c2872008-11-28 14:22:24 +1000384 if (master_priv->sarea_priv)
385 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Eric Anholted4cb412008-07-29 12:10:39 -0700387 i915_user_irq_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
389 READ_BREADCRUMB(dev_priv) >= irq_nr);
Eric Anholted4cb412008-07-29 12:10:39 -0700390 i915_user_irq_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Eric Anholt20caafa2007-08-25 19:22:43 +1000392 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000393 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
395 }
396
Dave Airlieaf6061a2008-05-07 12:15:39 +1000397 return ret;
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/* Needs the lock as it touches the ring.
401 */
Eric Anholtc153f452007-09-03 12:06:45 +1000402int i915_irq_emit(struct drm_device *dev, void *data,
403 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000406 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int result;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000410 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000411 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Eric Anholt299eb932009-02-24 22:14:12 -0800413
414 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
415
Eric Anholt546b0972008-09-01 16:45:29 -0700416 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -0700418 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Eric Anholtc153f452007-09-03 12:06:45 +1000420 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000422 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
425 return 0;
426}
427
428/* Doesn't need the hardware lock.
429 */
Eric Anholtc153f452007-09-03 12:06:45 +1000430int i915_irq_wait(struct drm_device *dev, void *data,
431 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000434 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000437 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000438 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Eric Anholtc153f452007-09-03 12:06:45 +1000441 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Keith Packard42f52ef2008-10-18 19:39:29 -0700444/* Called from drm generic code, passed 'crtc' which
445 * we use as a pipe index
446 */
447int i915_enable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700448{
449 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700450 unsigned long irqflags;
Jesse Barnes71e0ffa2009-01-08 10:42:15 -0800451 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
452 u32 pipeconf;
453
454 pipeconf = I915_READ(pipeconf_reg);
455 if (!(pipeconf & PIPEACONF_ENABLE))
456 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700457
Keith Packarde9d21d72008-10-16 11:31:38 -0700458 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packarde9d21d72008-10-16 11:31:38 -0700459 if (IS_I965G(dev))
Keith Packard7c463582008-11-04 02:03:27 -0800460 i915_enable_pipestat(dev_priv, pipe,
461 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700462 else
Keith Packard7c463582008-11-04 02:03:27 -0800463 i915_enable_pipestat(dev_priv, pipe,
464 PIPE_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700465 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700466 return 0;
467}
468
Keith Packard42f52ef2008-10-18 19:39:29 -0700469/* Called from drm generic code, passed 'crtc' which
470 * we use as a pipe index
471 */
472void i915_disable_vblank(struct drm_device *dev, int pipe)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700473{
474 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Keith Packarde9d21d72008-10-16 11:31:38 -0700475 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700476
Keith Packarde9d21d72008-10-16 11:31:38 -0700477 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Keith Packard7c463582008-11-04 02:03:27 -0800478 i915_disable_pipestat(dev_priv, pipe,
479 PIPE_VBLANK_INTERRUPT_ENABLE |
480 PIPE_START_VBLANK_INTERRUPT_ENABLE);
Keith Packarde9d21d72008-10-16 11:31:38 -0700481 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700482}
483
Jesse Barnes79e53942008-11-07 14:24:08 -0800484void i915_enable_interrupt (struct drm_device *dev)
485{
486 struct drm_i915_private *dev_priv = dev->dev_private;
487 opregion_enable_asle(dev);
488 dev_priv->irq_enabled = 1;
489}
490
491
Dave Airlie702880f2006-06-24 17:07:34 +1000492/* Set the vblank monitor pipe
493 */
Eric Anholtc153f452007-09-03 12:06:45 +1000494int i915_vblank_pipe_set(struct drm_device *dev, void *data,
495 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000496{
Dave Airlie702880f2006-06-24 17:07:34 +1000497 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +1000498
499 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000500 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000501 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000502 }
503
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000504 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +1000505}
506
Eric Anholtc153f452007-09-03 12:06:45 +1000507int i915_vblank_pipe_get(struct drm_device *dev, void *data,
508 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000509{
Dave Airlie702880f2006-06-24 17:07:34 +1000510 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000511 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +1000512
513 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000514 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000515 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000516 }
517
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700518 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +1000519
Dave Airlie702880f2006-06-24 17:07:34 +1000520 return 0;
521}
522
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000523/**
524 * Schedule buffer swap at given vertical blank.
525 */
Eric Anholtc153f452007-09-03 12:06:45 +1000526int i915_vblank_swap(struct drm_device *dev, void *data,
527 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000528{
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800529 /* The delayed swap mechanism was fundamentally racy, and has been
530 * removed. The model was that the client requested a delayed flip/swap
531 * from the kernel, then waited for vblank before continuing to perform
532 * rendering. The problem was that the kernel might wake the client
533 * up before it dispatched the vblank swap (since the lock has to be
534 * held while touching the ringbuffer), in which case the client would
535 * clear and start the next frame before the swap occurred, and
536 * flicker would occur in addition to likely missing the vblank.
537 *
538 * In the absence of this ioctl, userland falls back to a correct path
539 * of waiting for a vblank, then dispatching the swap on its own.
540 * Context switching to userland and back is plenty fast enough for
541 * meeting the requirements of vblank swapping.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700542 */
Eric Anholtbd95e0a2008-11-04 12:01:24 -0800543 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/* drm_dma.h hooks
547*/
Dave Airlie84b1fd12007-07-11 15:53:27 +1000548void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
551
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 atomic_set(&dev_priv->irq_received, 0);
553
Jesse Barnes5ca58282009-03-31 14:11:15 -0700554 if (I915_HAS_HOTPLUG(dev)) {
555 I915_WRITE(PORT_HOTPLUG_EN, 0);
556 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
557 }
558
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700559 I915_WRITE(HWSTAM, 0xeffe);
Keith Packard7c463582008-11-04 02:03:27 -0800560 I915_WRITE(PIPEASTAT, 0);
561 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700562 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -0700563 I915_WRITE(IER, 0x0);
Keith Packard7c463582008-11-04 02:03:27 -0800564 (void) I915_READ(IER);
Jesse Barnes5ca58282009-03-31 14:11:15 -0700565 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700568int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes5ca58282009-03-31 14:11:15 -0700571 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700572
573 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700574
575 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eric Anholted4cb412008-07-29 12:10:39 -0700576
Keith Packard7c463582008-11-04 02:03:27 -0800577 /* Unmask the interrupts that we always want on. */
578 dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100579
Keith Packard7c463582008-11-04 02:03:27 -0800580 dev_priv->pipestat[0] = 0;
581 dev_priv->pipestat[1] = 0;
582
Jesse Barnes5ca58282009-03-31 14:11:15 -0700583 if (I915_HAS_HOTPLUG(dev)) {
584 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
585
586 /* Leave other bits alone */
587 hotplug_en |= HOTPLUG_EN_MASK;
588 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
589
590 dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
591 TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
592 SDVOB_HOTPLUG_INT_STATUS;
593 if (IS_G4X(dev)) {
594 dev_priv->hotplug_supported_mask |=
595 HDMIB_HOTPLUG_INT_STATUS |
596 HDMIC_HOTPLUG_INT_STATUS |
597 HDMID_HOTPLUG_INT_STATUS;
598 }
599 /* Enable in IER... */
600 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
601 /* and unmask in IMR */
602 i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
603 }
604
Keith Packard7c463582008-11-04 02:03:27 -0800605 /* Disable pipe interrupt enables, clear pending pipe status */
606 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
607 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
608 /* Clear pending interrupt status */
609 I915_WRITE(IIR, I915_READ(IIR));
610
Jesse Barnes5ca58282009-03-31 14:11:15 -0700611 I915_WRITE(IER, enable_mask);
Keith Packard7c463582008-11-04 02:03:27 -0800612 I915_WRITE(IMR, dev_priv->irq_mask_reg);
Eric Anholted4cb412008-07-29 12:10:39 -0700613 (void) I915_READ(IER);
614
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100615 opregion_enable_asle(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700617
618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Dave Airlie84b1fd12007-07-11 15:53:27 +1000621void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie91e37382006-02-18 15:17:04 +1100624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!dev_priv)
626 return;
627
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700628 dev_priv->vblank_pipe = 0;
629
Jesse Barnes5ca58282009-03-31 14:11:15 -0700630 if (I915_HAS_HOTPLUG(dev)) {
631 I915_WRITE(PORT_HOTPLUG_EN, 0);
632 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
633 }
634
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700635 I915_WRITE(HWSTAM, 0xffffffff);
Keith Packard7c463582008-11-04 02:03:27 -0800636 I915_WRITE(PIPEASTAT, 0);
637 I915_WRITE(PIPEBSTAT, 0);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700638 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -0700639 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +1100640
Keith Packard7c463582008-11-04 02:03:27 -0800641 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
642 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
643 I915_WRITE(IIR, I915_READ(IIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}