blob: ef03a59c1df870a622b4f09690b2c9feb8070d0c [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"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define MAX_NOPID ((u32)~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Eric Anholted4cb412008-07-29 12:10:39 -070036/** These are the interrupts used by the driver */
37#define I915_INTERRUPT_ENABLE_MASK (I915_USER_INTERRUPT | \
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +010038 I915_ASLE_INTERRUPT | \
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070039 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +010040 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT)
Eric Anholted4cb412008-07-29 12:10:39 -070041
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +010042void
Eric Anholted4cb412008-07-29 12:10:39 -070043i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
44{
45 if ((dev_priv->irq_mask_reg & mask) != 0) {
46 dev_priv->irq_mask_reg &= ~mask;
47 I915_WRITE(IMR, dev_priv->irq_mask_reg);
48 (void) I915_READ(IMR);
49 }
50}
51
52static inline void
53i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
54{
55 if ((dev_priv->irq_mask_reg & mask) != mask) {
56 dev_priv->irq_mask_reg |= mask;
57 I915_WRITE(IMR, dev_priv->irq_mask_reg);
58 (void) I915_READ(IMR);
59 }
60}
61
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100062/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070063 * i915_get_pipe - return the the pipe associated with a given plane
64 * @dev: DRM device
65 * @plane: plane to look for
66 *
67 * The Intel Mesa & 2D drivers call the vblank routines with a plane number
68 * rather than a pipe number, since they may not always be equal. This routine
69 * maps the given @plane back to a pipe number.
70 */
71static int
72i915_get_pipe(struct drm_device *dev, int plane)
73{
74 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
75 u32 dspcntr;
76
77 dspcntr = plane ? I915_READ(DSPBCNTR) : I915_READ(DSPACNTR);
78
79 return dspcntr & DISPPLANE_SEL_PIPE_MASK ? 1 : 0;
80}
81
82/**
83 * i915_get_plane - return the the plane associated with a given pipe
84 * @dev: DRM device
85 * @pipe: pipe to look for
86 *
87 * The Intel Mesa & 2D drivers call the vblank routines with a plane number
88 * rather than a plane number, since they may not always be equal. This routine
89 * maps the given @pipe back to a plane number.
90 */
91static int
92i915_get_plane(struct drm_device *dev, int pipe)
93{
94 if (i915_get_pipe(dev, 0) == pipe)
95 return 0;
96 return 1;
97}
98
99/**
100 * i915_pipe_enabled - check if a pipe is enabled
101 * @dev: DRM device
102 * @pipe: pipe to check
103 *
104 * Reading certain registers when the pipe is disabled can hang the chip.
105 * Use this routine to make sure the PLL is running and the pipe is active
106 * before reading such registers if unsure.
107 */
108static int
109i915_pipe_enabled(struct drm_device *dev, int pipe)
110{
111 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
112 unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
113
114 if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
115 return 1;
116
117 return 0;
118}
119
120/**
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000121 * Emit blits for scheduled buffer swaps.
122 *
123 * This function will be called with the HW lock held.
124 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000125static void i915_vblank_tasklet(struct drm_device *dev)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000126{
127 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000128 unsigned long irqflags;
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100129 struct list_head *list, *tmp, hits, *hit;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000130 int nhits, nrects, slice[2], upper[2], lower[2], i;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700131 unsigned counter[2];
Dave Airliec60ce622007-07-11 15:27:12 +1000132 struct drm_drawable_info *drw;
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100133 drm_i915_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000134 u32 cpp = dev_priv->cpp;
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100135 u32 cmd = (cpp == 4) ? (XY_SRC_COPY_BLT_CMD |
136 XY_SRC_COPY_BLT_WRITE_ALPHA |
137 XY_SRC_COPY_BLT_WRITE_RGB)
138 : XY_SRC_COPY_BLT_CMD;
Keith Packard7b832b52008-04-21 16:31:10 +1000139 u32 src_pitch = sarea_priv->pitch * cpp;
140 u32 dst_pitch = sarea_priv->pitch * cpp;
141 u32 ropcpp = (0xcc << 16) | ((cpp - 1) << 24);
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100142 RING_LOCALS;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000143
Jesse Barnes3d258022008-07-01 12:32:52 -0700144 if (IS_I965G(dev) && sarea_priv->front_tiled) {
Keith Packard7b832b52008-04-21 16:31:10 +1000145 cmd |= XY_SRC_COPY_BLT_DST_TILED;
146 dst_pitch >>= 2;
147 }
Jesse Barnes3d258022008-07-01 12:32:52 -0700148 if (IS_I965G(dev) && sarea_priv->back_tiled) {
Keith Packard7b832b52008-04-21 16:31:10 +1000149 cmd |= XY_SRC_COPY_BLT_SRC_TILED;
150 src_pitch >>= 2;
151 }
152
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700153 counter[0] = drm_vblank_count(dev, 0);
154 counter[1] = drm_vblank_count(dev, 1);
155
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000156 DRM_DEBUG("\n");
157
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100158 INIT_LIST_HEAD(&hits);
159
160 nhits = nrects = 0;
161
Dave Airlieaf6061a2008-05-07 12:15:39 +1000162 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000163
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100164 /* Find buffer swaps scheduled for this vertical blank */
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000165 list_for_each_safe(list, tmp, &dev_priv->vbl_swaps.head) {
166 drm_i915_vbl_swap_t *vbl_swap =
167 list_entry(list, drm_i915_vbl_swap_t, head);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700168 int pipe = i915_get_pipe(dev, vbl_swap->plane);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000169
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700170 if ((counter[pipe] - vbl_swap->sequence) > (1<<23))
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100171 continue;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000172
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100173 list_del(list);
174 dev_priv->swaps_pending--;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700175 drm_vblank_put(dev, pipe);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000176
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100177 spin_unlock(&dev_priv->swaps_lock);
178 spin_lock(&dev->drw_lock);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000179
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100180 drw = drm_get_drawable_info(dev, vbl_swap->drw_id);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000181
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100182 if (!drw) {
183 spin_unlock(&dev->drw_lock);
184 drm_free(vbl_swap, sizeof(*vbl_swap), DRM_MEM_DRIVER);
185 spin_lock(&dev_priv->swaps_lock);
186 continue;
187 }
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000188
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100189 list_for_each(hit, &hits) {
190 drm_i915_vbl_swap_t *swap_cmp =
191 list_entry(hit, drm_i915_vbl_swap_t, head);
Dave Airliec60ce622007-07-11 15:27:12 +1000192 struct drm_drawable_info *drw_cmp =
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100193 drm_get_drawable_info(dev, swap_cmp->drw_id);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000194
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100195 if (drw_cmp &&
196 drw_cmp->rects[0].y1 > drw->rects[0].y1) {
197 list_add_tail(list, hit);
198 break;
199 }
200 }
201
202 spin_unlock(&dev->drw_lock);
203
204 /* List of hits was empty, or we reached the end of it */
205 if (hit == &hits)
206 list_add_tail(list, hits.prev);
207
208 nhits++;
209
210 spin_lock(&dev_priv->swaps_lock);
211 }
212
Dave Airlieaf6061a2008-05-07 12:15:39 +1000213 if (nhits == 0) {
214 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000215 return;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000216 }
217
218 spin_unlock(&dev_priv->swaps_lock);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000219
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100220 i915_kernel_lost_context(dev);
221
Dave Airlieaf6061a2008-05-07 12:15:39 +1000222 if (IS_I965G(dev)) {
223 BEGIN_LP_RING(4);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000224
Dave Airlieaf6061a2008-05-07 12:15:39 +1000225 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
226 OUT_RING(0);
227 OUT_RING(((sarea_priv->width - 1) & 0xffff) | ((sarea_priv->height - 1) << 16));
228 OUT_RING(0);
229 ADVANCE_LP_RING();
230 } else {
231 BEGIN_LP_RING(6);
232
233 OUT_RING(GFX_OP_DRAWRECT_INFO);
234 OUT_RING(0);
235 OUT_RING(0);
236 OUT_RING(sarea_priv->width | sarea_priv->height << 16);
237 OUT_RING(sarea_priv->width | sarea_priv->height << 16);
238 OUT_RING(0);
239
240 ADVANCE_LP_RING();
241 }
242
243 sarea_priv->ctxOwner = DRM_KERNEL_CONTEXT;
244
245 upper[0] = upper[1] = 0;
246 slice[0] = max(sarea_priv->pipeA_h / nhits, 1);
247 slice[1] = max(sarea_priv->pipeB_h / nhits, 1);
248 lower[0] = sarea_priv->pipeA_y + slice[0];
249 lower[1] = sarea_priv->pipeB_y + slice[0];
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100250
251 spin_lock(&dev->drw_lock);
252
253 /* Emit blits for buffer swaps, partitioning both outputs into as many
254 * slices as there are buffer swaps scheduled in order to avoid tearing
255 * (based on the assumption that a single buffer swap would always
256 * complete before scanout starts).
257 */
258 for (i = 0; i++ < nhits;
259 upper[0] = lower[0], lower[0] += slice[0],
260 upper[1] = lower[1], lower[1] += slice[1]) {
261 if (i == nhits)
262 lower[0] = lower[1] = sarea_priv->height;
263
264 list_for_each(hit, &hits) {
265 drm_i915_vbl_swap_t *swap_hit =
266 list_entry(hit, drm_i915_vbl_swap_t, head);
Dave Airliec60ce622007-07-11 15:27:12 +1000267 struct drm_clip_rect *rect;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700268 int num_rects, plane;
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100269 unsigned short top, bottom;
270
271 drw = drm_get_drawable_info(dev, swap_hit->drw_id);
272
273 if (!drw)
274 continue;
275
276 rect = drw->rects;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700277 plane = swap_hit->plane;
278 top = upper[plane];
279 bottom = lower[plane];
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100280
281 for (num_rects = drw->num_rects; num_rects--; rect++) {
282 int y1 = max(rect->y1, top);
283 int y2 = min(rect->y2, bottom);
284
285 if (y1 >= y2)
286 continue;
287
288 BEGIN_LP_RING(8);
289
290 OUT_RING(cmd);
Keith Packard7b832b52008-04-21 16:31:10 +1000291 OUT_RING(ropcpp | dst_pitch);
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100292 OUT_RING((y1 << 16) | rect->x1);
293 OUT_RING((y2 << 16) | rect->x2);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000294 OUT_RING(sarea_priv->front_offset);
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100295 OUT_RING((y1 << 16) | rect->x1);
Keith Packard7b832b52008-04-21 16:31:10 +1000296 OUT_RING(src_pitch);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000297 OUT_RING(sarea_priv->back_offset);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000298
299 ADVANCE_LP_RING();
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000300 }
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000301 }
302 }
303
Dave Airlieaf6061a2008-05-07 12:15:39 +1000304 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100305
306 list_for_each_safe(hit, tmp, &hits) {
307 drm_i915_vbl_swap_t *swap_hit =
308 list_entry(hit, drm_i915_vbl_swap_t, head);
309
310 list_del(hit);
311
312 drm_free(swap_hit, sizeof(*swap_hit), DRM_MEM_DRIVER);
313 }
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000314}
315
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700316u32 i915_get_vblank_counter(struct drm_device *dev, int plane)
317{
318 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
319 unsigned long high_frame;
320 unsigned long low_frame;
321 u32 high1, high2, low, count;
322 int pipe;
323
324 pipe = i915_get_pipe(dev, plane);
325 high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
326 low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
327
328 if (!i915_pipe_enabled(dev, pipe)) {
329 DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
330 return 0;
331 }
332
333 /*
334 * High & low register fields aren't synchronized, so make sure
335 * we get a low value that's stable across two reads of the high
336 * register.
337 */
338 do {
339 high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
340 PIPE_FRAME_HIGH_SHIFT);
341 low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
342 PIPE_FRAME_LOW_SHIFT);
343 high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
344 PIPE_FRAME_HIGH_SHIFT);
345 } while (high1 != high2);
346
347 count = (high1 << 8) | low;
348
349 return count;
350}
351
Eric Anholt546b0972008-09-01 16:45:29 -0700352void
353i915_gem_vblank_work_handler(struct work_struct *work)
354{
355 drm_i915_private_t *dev_priv;
356 struct drm_device *dev;
357
358 dev_priv = container_of(work, drm_i915_private_t,
359 mm.vblank_work);
360 dev = dev_priv->dev;
361
362 mutex_lock(&dev->struct_mutex);
363 i915_vblank_tasklet(dev);
364 mutex_unlock(&dev->struct_mutex);
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
368{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000369 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Eric Anholted4cb412008-07-29 12:10:39 -0700371 u32 iir;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700372 u32 pipea_stats, pipeb_stats;
373 int vblank = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000374
Eric Anholted4cb412008-07-29 12:10:39 -0700375 if (dev->pdev->msi_enabled)
376 I915_WRITE(IMR, ~0);
377 iir = I915_READ(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000378
Eric Anholted4cb412008-07-29 12:10:39 -0700379 if (iir == 0) {
380 if (dev->pdev->msi_enabled) {
381 I915_WRITE(IMR, dev_priv->irq_mask_reg);
382 (void) I915_READ(IMR);
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return IRQ_NONE;
Eric Anholted4cb412008-07-29 12:10:39 -0700385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700387 /*
388 * Clear the PIPE(A|B)STAT regs before the IIR otherwise
389 * we may get extra interrupts.
390 */
391 if (iir & I915_DISPLAY_PIPE_A_EVENT_INTERRUPT) {
392 pipea_stats = I915_READ(PIPEASTAT);
393 if (!(dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A))
394 pipea_stats &= ~(PIPE_START_VBLANK_INTERRUPT_ENABLE |
395 PIPE_VBLANK_INTERRUPT_ENABLE);
396 else if (pipea_stats & (PIPE_START_VBLANK_INTERRUPT_STATUS|
397 PIPE_VBLANK_INTERRUPT_STATUS)) {
398 vblank++;
399 drm_handle_vblank(dev, i915_get_plane(dev, 0));
400 }
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100401
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700402 I915_WRITE(PIPEASTAT, pipea_stats);
403 }
404 if (iir & I915_DISPLAY_PIPE_B_EVENT_INTERRUPT) {
405 pipeb_stats = I915_READ(PIPEBSTAT);
406 /* Ack the event */
407 I915_WRITE(PIPEBSTAT, pipeb_stats);
Dave Airlie0d6aa602006-01-02 20:14:23 +1100408
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700409 /* The vblank interrupt gets enabled even if we didn't ask for
410 it, so make sure it's shut down again */
411 if (!(dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B))
412 pipeb_stats &= ~(PIPE_START_VBLANK_INTERRUPT_ENABLE |
413 PIPE_VBLANK_INTERRUPT_ENABLE);
414 else if (pipeb_stats & (PIPE_START_VBLANK_INTERRUPT_STATUS|
415 PIPE_VBLANK_INTERRUPT_STATUS)) {
416 vblank++;
417 drm_handle_vblank(dev, i915_get_plane(dev, 1));
418 }
Dave Airlie6e5fca52006-03-20 18:34:29 +1100419
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700420 if (pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS)
421 opregion_asle_intr(dev);
422 I915_WRITE(PIPEBSTAT, pipeb_stats);
Dave Airlie0d6aa602006-01-02 20:14:23 +1100423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Eric Anholt673a3942008-07-30 12:06:12 -0700425 I915_WRITE(IIR, iir);
426 if (dev->pdev->msi_enabled)
427 I915_WRITE(IMR, dev_priv->irq_mask_reg);
428 (void) I915_READ(IIR); /* Flush posted writes */
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100429
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400430 if (dev_priv->sarea_priv)
431 dev_priv->sarea_priv->last_dispatch =
432 READ_BREADCRUMB(dev_priv);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700433
Eric Anholt673a3942008-07-30 12:06:12 -0700434 if (iir & I915_USER_INTERRUPT) {
435 dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
436 DRM_WAKEUP(&dev_priv->irq_queue);
437 }
438
439 if (iir & I915_ASLE_INTERRUPT)
440 opregion_asle_intr(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700441
Eric Anholt546b0972008-09-01 16:45:29 -0700442 if (vblank && dev_priv->swaps_pending > 0) {
443 if (dev_priv->ring.ring_obj == NULL)
444 drm_locked_tasklet(dev, i915_vblank_tasklet);
445 else
446 schedule_work(&dev_priv->mm.vblank_work);
447 }
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return IRQ_HANDLED;
450}
451
Dave Airlieaf6061a2008-05-07 12:15:39 +1000452static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 RING_LOCALS;
456
457 i915_kernel_lost_context(dev);
458
Márton Németh3e684ea2008-01-24 15:58:57 +1000459 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400461 dev_priv->counter++;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000462 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400463 dev_priv->counter = 1;
464 if (dev_priv->sarea_priv)
465 dev_priv->sarea_priv->last_enqueue = dev_priv->counter;
Alan Hourihanec29b6692006-08-12 16:29:24 +1000466
467 BEGIN_LP_RING(6);
Jesse Barnes585fb112008-07-29 11:54:06 -0700468 OUT_RING(MI_STORE_DWORD_INDEX);
469 OUT_RING(5 << MI_STORE_DWORD_INDEX_SHIFT);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000470 OUT_RING(dev_priv->counter);
471 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 OUT_RING(0);
Jesse Barnes585fb112008-07-29 11:54:06 -0700473 OUT_RING(MI_USER_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 ADVANCE_LP_RING();
Dave Airliebc5f4522007-11-05 12:50:58 +1000475
Alan Hourihanec29b6692006-08-12 16:29:24 +1000476 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Eric Anholt673a3942008-07-30 12:06:12 -0700479void i915_user_irq_get(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700480{
481 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
482
483 spin_lock(&dev_priv->user_irq_lock);
484 if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1))
485 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
486 spin_unlock(&dev_priv->user_irq_lock);
487}
488
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700489void i915_user_irq_put(struct drm_device *dev)
Eric Anholted4cb412008-07-29 12:10:39 -0700490{
491 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
492
493 spin_lock(&dev_priv->user_irq_lock);
494 BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
495 if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0))
496 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
497 spin_unlock(&dev_priv->user_irq_lock);
498}
499
Dave Airlie84b1fd12007-07-11 15:53:27 +1000500static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
503 int ret = 0;
504
Márton Németh3e684ea2008-01-24 15:58:57 +1000505 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 READ_BREADCRUMB(dev_priv));
507
Eric Anholted4cb412008-07-29 12:10:39 -0700508 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400509 if (dev_priv->sarea_priv) {
510 dev_priv->sarea_priv->last_dispatch =
511 READ_BREADCRUMB(dev_priv);
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
Eric Anholted4cb412008-07-29 12:10:39 -0700514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400516 if (dev_priv->sarea_priv)
517 dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Eric Anholted4cb412008-07-29 12:10:39 -0700519 i915_user_irq_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
521 READ_BREADCRUMB(dev_priv) >= irq_nr);
Eric Anholted4cb412008-07-29 12:10:39 -0700522 i915_user_irq_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Eric Anholt20caafa2007-08-25 19:22:43 +1000524 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000525 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
527 }
528
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400529 if (dev_priv->sarea_priv)
530 dev_priv->sarea_priv->last_dispatch =
531 READ_BREADCRUMB(dev_priv);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000532
533 return ret;
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/* Needs the lock as it touches the ring.
537 */
Eric Anholtc153f452007-09-03 12:06:45 +1000538int i915_irq_emit(struct drm_device *dev, void *data,
539 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000542 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int result;
544
Eric Anholt546b0972008-09-01 16:45:29 -0700545 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000548 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000549 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Eric Anholt546b0972008-09-01 16:45:29 -0700551 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 result = i915_emit_irq(dev);
Eric Anholt546b0972008-09-01 16:45:29 -0700553 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Eric Anholtc153f452007-09-03 12:06:45 +1000555 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000557 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
560 return 0;
561}
562
563/* Doesn't need the hardware lock.
564 */
Eric Anholtc153f452007-09-03 12:06:45 +1000565int i915_irq_wait(struct drm_device *dev, void *data,
566 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000569 drm_i915_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000572 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Eric Anholtc153f452007-09-03 12:06:45 +1000576 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700579int i915_enable_vblank(struct drm_device *dev, int plane)
580{
581 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
582 int pipe = i915_get_pipe(dev, plane);
583 u32 pipestat_reg = 0;
584 u32 pipestat;
585
586 switch (pipe) {
587 case 0:
588 pipestat_reg = PIPEASTAT;
589 i915_enable_irq(dev_priv, I915_DISPLAY_PIPE_A_EVENT_INTERRUPT);
590 break;
591 case 1:
592 pipestat_reg = PIPEBSTAT;
593 i915_enable_irq(dev_priv, I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
594 break;
595 default:
596 DRM_ERROR("tried to enable vblank on non-existent pipe %d\n",
597 pipe);
598 break;
599 }
600
601 if (pipestat_reg) {
602 pipestat = I915_READ(pipestat_reg);
603 if (IS_I965G(dev))
604 pipestat |= PIPE_START_VBLANK_INTERRUPT_ENABLE;
605 else
606 pipestat |= PIPE_VBLANK_INTERRUPT_ENABLE;
607 /* Clear any stale interrupt status */
608 pipestat |= (PIPE_START_VBLANK_INTERRUPT_STATUS |
609 PIPE_VBLANK_INTERRUPT_STATUS);
610 I915_WRITE(pipestat_reg, pipestat);
611 }
612
613 return 0;
614}
615
616void i915_disable_vblank(struct drm_device *dev, int plane)
617{
618 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
619 int pipe = i915_get_pipe(dev, plane);
620 u32 pipestat_reg = 0;
621 u32 pipestat;
622
623 switch (pipe) {
624 case 0:
625 pipestat_reg = PIPEASTAT;
626 i915_disable_irq(dev_priv, I915_DISPLAY_PIPE_A_EVENT_INTERRUPT);
627 break;
628 case 1:
629 pipestat_reg = PIPEBSTAT;
630 i915_disable_irq(dev_priv, I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
631 break;
632 default:
633 DRM_ERROR("tried to disable vblank on non-existent pipe %d\n",
634 pipe);
635 break;
636 }
637
638 if (pipestat_reg) {
639 pipestat = I915_READ(pipestat_reg);
640 pipestat &= ~(PIPE_START_VBLANK_INTERRUPT_ENABLE |
641 PIPE_VBLANK_INTERRUPT_ENABLE);
642 /* Clear any stale interrupt status */
643 pipestat |= (PIPE_START_VBLANK_INTERRUPT_STATUS |
644 PIPE_VBLANK_INTERRUPT_STATUS);
645 I915_WRITE(pipestat_reg, pipestat);
646 }
647}
648
Dave Airlie702880f2006-06-24 17:07:34 +1000649/* Set the vblank monitor pipe
650 */
Eric Anholtc153f452007-09-03 12:06:45 +1000651int i915_vblank_pipe_set(struct drm_device *dev, void *data,
652 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000653{
Dave Airlie702880f2006-06-24 17:07:34 +1000654 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie702880f2006-06-24 17:07:34 +1000655
656 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000657 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000658 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000659 }
660
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000661 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +1000662}
663
Eric Anholtc153f452007-09-03 12:06:45 +1000664int i915_vblank_pipe_get(struct drm_device *dev, void *data,
665 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000666{
Dave Airlie702880f2006-06-24 17:07:34 +1000667 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000668 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +1000669
670 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000671 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000672 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000673 }
674
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700675 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
Eric Anholtc153f452007-09-03 12:06:45 +1000676
Dave Airlie702880f2006-06-24 17:07:34 +1000677 return 0;
678}
679
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000680/**
681 * Schedule buffer swap at given vertical blank.
682 */
Eric Anholtc153f452007-09-03 12:06:45 +1000683int i915_vblank_swap(struct drm_device *dev, void *data,
684 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000685{
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000686 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000687 drm_i915_vblank_swap_t *swap = data;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000688 drm_i915_vbl_swap_t *vbl_swap;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700689 unsigned int pipe, seqtype, curseq, plane;
=?utf-8?q?Michel_D=C3=A4nzer?=a0b136b2006-10-25 00:12:52 +1000690 unsigned long irqflags;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000691 struct list_head *list;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700692 int ret;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000693
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400694 if (!dev_priv || !dev_priv->sarea_priv) {
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000695 DRM_ERROR("%s called with no initialization\n", __func__);
Eric Anholt20caafa2007-08-25 19:22:43 +1000696 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000697 }
698
Dave Airlieaf6061a2008-05-07 12:15:39 +1000699 if (dev_priv->sarea_priv->rotation) {
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000700 DRM_DEBUG("Rotation not supported\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000701 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000702 }
703
Eric Anholtc153f452007-09-03 12:06:45 +1000704 if (swap->seqtype & ~(_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE |
Dave Airlieaf6061a2008-05-07 12:15:39 +1000705 _DRM_VBLANK_SECONDARY | _DRM_VBLANK_NEXTONMISS)) {
Eric Anholtc153f452007-09-03 12:06:45 +1000706 DRM_ERROR("Invalid sequence type 0x%x\n", swap->seqtype);
Eric Anholt20caafa2007-08-25 19:22:43 +1000707 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000708 }
709
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700710 plane = (swap->seqtype & _DRM_VBLANK_SECONDARY) ? 1 : 0;
711 pipe = i915_get_pipe(dev, plane);
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000712
Eric Anholtc153f452007-09-03 12:06:45 +1000713 seqtype = swap->seqtype & (_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE);
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000714
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000715 if (!(dev_priv->vblank_pipe & (1 << pipe))) {
716 DRM_ERROR("Invalid pipe %d\n", pipe);
Eric Anholt20caafa2007-08-25 19:22:43 +1000717 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000718 }
719
720 spin_lock_irqsave(&dev->drw_lock, irqflags);
721
Eric Anholtc153f452007-09-03 12:06:45 +1000722 if (!drm_get_drawable_info(dev, swap->drawable)) {
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000723 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
Eric Anholtc153f452007-09-03 12:06:45 +1000724 DRM_DEBUG("Invalid drawable ID %d\n", swap->drawable);
Eric Anholt20caafa2007-08-25 19:22:43 +1000725 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000726 }
727
728 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
729
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700730 /*
731 * We take the ref here and put it when the swap actually completes
732 * in the tasklet.
733 */
734 ret = drm_vblank_get(dev, pipe);
735 if (ret)
736 return ret;
737 curseq = drm_vblank_count(dev, pipe);
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000738
=?utf-8?q?Michel_D=C3=A4nzer?=2228ed62006-10-25 01:05:09 +1000739 if (seqtype == _DRM_VBLANK_RELATIVE)
Eric Anholtc153f452007-09-03 12:06:45 +1000740 swap->sequence += curseq;
=?utf-8?q?Michel_D=C3=A4nzer?=2228ed62006-10-25 01:05:09 +1000741
Eric Anholtc153f452007-09-03 12:06:45 +1000742 if ((curseq - swap->sequence) <= (1<<23)) {
743 if (swap->seqtype & _DRM_VBLANK_NEXTONMISS) {
744 swap->sequence = curseq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=2228ed62006-10-25 01:05:09 +1000745 } else {
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000746 DRM_DEBUG("Missed target sequence\n");
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700747 drm_vblank_put(dev, pipe);
Eric Anholt20caafa2007-08-25 19:22:43 +1000748 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000749 }
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000750 }
751
=?utf-8?q?Michel_D=C3=A4nzer?=2228ed62006-10-25 01:05:09 +1000752 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
753
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000754 list_for_each(list, &dev_priv->vbl_swaps.head) {
755 vbl_swap = list_entry(list, drm_i915_vbl_swap_t, head);
756
Eric Anholtc153f452007-09-03 12:06:45 +1000757 if (vbl_swap->drw_id == swap->drawable &&
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700758 vbl_swap->plane == plane &&
Eric Anholtc153f452007-09-03 12:06:45 +1000759 vbl_swap->sequence == swap->sequence) {
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000760 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
761 DRM_DEBUG("Already scheduled\n");
762 return 0;
763 }
764 }
765
766 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
767
=?utf-8?q?Michel_D=C3=A4nzer?=21fa60e2006-10-25 00:10:59 +1000768 if (dev_priv->swaps_pending >= 100) {
769 DRM_DEBUG("Too many swaps queued\n");
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700770 drm_vblank_put(dev, pipe);
Eric Anholt20caafa2007-08-25 19:22:43 +1000771 return -EBUSY;
=?utf-8?q?Michel_D=C3=A4nzer?=21fa60e2006-10-25 00:10:59 +1000772 }
773
Dave Airlie54583bf2007-10-14 21:21:30 +1000774 vbl_swap = drm_calloc(1, sizeof(*vbl_swap), DRM_MEM_DRIVER);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000775
776 if (!vbl_swap) {
777 DRM_ERROR("Failed to allocate memory to queue swap\n");
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700778 drm_vblank_put(dev, pipe);
Eric Anholt20caafa2007-08-25 19:22:43 +1000779 return -ENOMEM;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000780 }
781
782 DRM_DEBUG("\n");
783
Eric Anholtc153f452007-09-03 12:06:45 +1000784 vbl_swap->drw_id = swap->drawable;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700785 vbl_swap->plane = plane;
Eric Anholtc153f452007-09-03 12:06:45 +1000786 vbl_swap->sequence = swap->sequence;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000787
788 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
789
Li Zefand5b0d1b2007-12-17 09:47:19 +1000790 list_add_tail(&vbl_swap->head, &dev_priv->vbl_swaps.head);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000791 dev_priv->swaps_pending++;
792
793 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
794
795 return 0;
796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/* drm_dma.h hooks
799*/
Dave Airlie84b1fd12007-07-11 15:53:27 +1000800void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
803
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700804 I915_WRITE(HWSTAM, 0xeffe);
805 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -0700806 I915_WRITE(IER, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700809int i915_driver_irq_postinstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700812 int ret, num_pipes = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Thomas Gleixnera6399bd2007-05-26 05:56:14 +1000814 spin_lock_init(&dev_priv->swaps_lock);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000815 INIT_LIST_HEAD(&dev_priv->vbl_swaps.head);
816 dev_priv->swaps_pending = 0;
817
Eric Anholted4cb412008-07-29 12:10:39 -0700818 /* Set initial unmasked IRQs to just the selected vblank pipes. */
819 dev_priv->irq_mask_reg = ~0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700820
821 ret = drm_vblank_init(dev, num_pipes);
822 if (ret)
823 return ret;
824
825 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
826 dev_priv->irq_mask_reg &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
827 dev_priv->irq_mask_reg &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
828
829 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eric Anholted4cb412008-07-29 12:10:39 -0700830
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100831 dev_priv->irq_mask_reg &= I915_INTERRUPT_ENABLE_MASK;
832
Eric Anholted4cb412008-07-29 12:10:39 -0700833 I915_WRITE(IMR, dev_priv->irq_mask_reg);
834 I915_WRITE(IER, I915_INTERRUPT_ENABLE_MASK);
835 (void) I915_READ(IER);
836
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100837 opregion_enable_asle(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700839
840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Dave Airlie84b1fd12007-07-11 15:53:27 +1000843void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700846 u32 temp;
Dave Airlie91e37382006-02-18 15:17:04 +1100847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (!dev_priv)
849 return;
850
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700851 dev_priv->vblank_pipe = 0;
852
853 I915_WRITE(HWSTAM, 0xffffffff);
854 I915_WRITE(IMR, 0xffffffff);
Eric Anholted4cb412008-07-29 12:10:39 -0700855 I915_WRITE(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +1100856
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700857 temp = I915_READ(PIPEASTAT);
858 I915_WRITE(PIPEASTAT, temp);
859 temp = I915_READ(PIPEBSTAT);
860 I915_WRITE(PIPEBSTAT, temp);
Eric Anholted4cb412008-07-29 12:10:39 -0700861 temp = I915_READ(IIR);
862 I915_WRITE(IIR, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}