blob: 4a2de7897344132852cbe9acdc6601e398f964a5 [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
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100036/**
37 * Emit blits for scheduled buffer swaps.
38 *
39 * This function will be called with the HW lock held.
40 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100041static void i915_vblank_tasklet(struct drm_device *dev)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100042{
43 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlieaf6061a2008-05-07 12:15:39 +100044 unsigned long irqflags;
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110045 struct list_head *list, *tmp, hits, *hit;
Dave Airlieaf6061a2008-05-07 12:15:39 +100046 int nhits, nrects, slice[2], upper[2], lower[2], i;
47 unsigned counter[2] = { atomic_read(&dev->vbl_received),
48 atomic_read(&dev->vbl_received2) };
Dave Airliec60ce622007-07-11 15:27:12 +100049 struct drm_drawable_info *drw;
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110050 drm_i915_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieaf6061a2008-05-07 12:15:39 +100051 u32 cpp = dev_priv->cpp;
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110052 u32 cmd = (cpp == 4) ? (XY_SRC_COPY_BLT_CMD |
53 XY_SRC_COPY_BLT_WRITE_ALPHA |
54 XY_SRC_COPY_BLT_WRITE_RGB)
55 : XY_SRC_COPY_BLT_CMD;
Keith Packard7b832b52008-04-21 16:31:10 +100056 u32 src_pitch = sarea_priv->pitch * cpp;
57 u32 dst_pitch = sarea_priv->pitch * cpp;
58 u32 ropcpp = (0xcc << 16) | ((cpp - 1) << 24);
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110059 RING_LOCALS;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100060
Jesse Barnes3d258022008-07-01 12:32:52 -070061 if (IS_I965G(dev) && sarea_priv->front_tiled) {
Keith Packard7b832b52008-04-21 16:31:10 +100062 cmd |= XY_SRC_COPY_BLT_DST_TILED;
63 dst_pitch >>= 2;
64 }
Jesse Barnes3d258022008-07-01 12:32:52 -070065 if (IS_I965G(dev) && sarea_priv->back_tiled) {
Keith Packard7b832b52008-04-21 16:31:10 +100066 cmd |= XY_SRC_COPY_BLT_SRC_TILED;
67 src_pitch >>= 2;
68 }
69
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100070 DRM_DEBUG("\n");
71
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110072 INIT_LIST_HEAD(&hits);
73
74 nhits = nrects = 0;
75
Dave Airlieaf6061a2008-05-07 12:15:39 +100076 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100077
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110078 /* Find buffer swaps scheduled for this vertical blank */
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100079 list_for_each_safe(list, tmp, &dev_priv->vbl_swaps.head) {
80 drm_i915_vbl_swap_t *vbl_swap =
81 list_entry(list, drm_i915_vbl_swap_t, head);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100082
Dave Airlieaf6061a2008-05-07 12:15:39 +100083 if ((counter[vbl_swap->pipe] - vbl_swap->sequence) > (1<<23))
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110084 continue;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100085
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110086 list_del(list);
87 dev_priv->swaps_pending--;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100088
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110089 spin_unlock(&dev_priv->swaps_lock);
90 spin_lock(&dev->drw_lock);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100091
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110092 drw = drm_get_drawable_info(dev, vbl_swap->drw_id);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +100093
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +110094 if (!drw) {
95 spin_unlock(&dev->drw_lock);
96 drm_free(vbl_swap, sizeof(*vbl_swap), DRM_MEM_DRIVER);
97 spin_lock(&dev_priv->swaps_lock);
98 continue;
99 }
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000100
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100101 list_for_each(hit, &hits) {
102 drm_i915_vbl_swap_t *swap_cmp =
103 list_entry(hit, drm_i915_vbl_swap_t, head);
Dave Airliec60ce622007-07-11 15:27:12 +1000104 struct drm_drawable_info *drw_cmp =
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100105 drm_get_drawable_info(dev, swap_cmp->drw_id);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000106
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100107 if (drw_cmp &&
108 drw_cmp->rects[0].y1 > drw->rects[0].y1) {
109 list_add_tail(list, hit);
110 break;
111 }
112 }
113
114 spin_unlock(&dev->drw_lock);
115
116 /* List of hits was empty, or we reached the end of it */
117 if (hit == &hits)
118 list_add_tail(list, hits.prev);
119
120 nhits++;
121
122 spin_lock(&dev_priv->swaps_lock);
123 }
124
Dave Airlieaf6061a2008-05-07 12:15:39 +1000125 if (nhits == 0) {
126 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000127 return;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000128 }
129
130 spin_unlock(&dev_priv->swaps_lock);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000131
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100132 i915_kernel_lost_context(dev);
133
Dave Airlieaf6061a2008-05-07 12:15:39 +1000134 if (IS_I965G(dev)) {
135 BEGIN_LP_RING(4);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000136
Dave Airlieaf6061a2008-05-07 12:15:39 +1000137 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
138 OUT_RING(0);
139 OUT_RING(((sarea_priv->width - 1) & 0xffff) | ((sarea_priv->height - 1) << 16));
140 OUT_RING(0);
141 ADVANCE_LP_RING();
142 } else {
143 BEGIN_LP_RING(6);
144
145 OUT_RING(GFX_OP_DRAWRECT_INFO);
146 OUT_RING(0);
147 OUT_RING(0);
148 OUT_RING(sarea_priv->width | sarea_priv->height << 16);
149 OUT_RING(sarea_priv->width | sarea_priv->height << 16);
150 OUT_RING(0);
151
152 ADVANCE_LP_RING();
153 }
154
155 sarea_priv->ctxOwner = DRM_KERNEL_CONTEXT;
156
157 upper[0] = upper[1] = 0;
158 slice[0] = max(sarea_priv->pipeA_h / nhits, 1);
159 slice[1] = max(sarea_priv->pipeB_h / nhits, 1);
160 lower[0] = sarea_priv->pipeA_y + slice[0];
161 lower[1] = sarea_priv->pipeB_y + slice[0];
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100162
163 spin_lock(&dev->drw_lock);
164
165 /* Emit blits for buffer swaps, partitioning both outputs into as many
166 * slices as there are buffer swaps scheduled in order to avoid tearing
167 * (based on the assumption that a single buffer swap would always
168 * complete before scanout starts).
169 */
170 for (i = 0; i++ < nhits;
171 upper[0] = lower[0], lower[0] += slice[0],
172 upper[1] = lower[1], lower[1] += slice[1]) {
173 if (i == nhits)
174 lower[0] = lower[1] = sarea_priv->height;
175
176 list_for_each(hit, &hits) {
177 drm_i915_vbl_swap_t *swap_hit =
178 list_entry(hit, drm_i915_vbl_swap_t, head);
Dave Airliec60ce622007-07-11 15:27:12 +1000179 struct drm_clip_rect *rect;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000180 int num_rects, pipe;
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100181 unsigned short top, bottom;
182
183 drw = drm_get_drawable_info(dev, swap_hit->drw_id);
184
185 if (!drw)
186 continue;
187
188 rect = drw->rects;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000189 pipe = swap_hit->pipe;
190 top = upper[pipe];
191 bottom = lower[pipe];
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100192
193 for (num_rects = drw->num_rects; num_rects--; rect++) {
194 int y1 = max(rect->y1, top);
195 int y2 = min(rect->y2, bottom);
196
197 if (y1 >= y2)
198 continue;
199
200 BEGIN_LP_RING(8);
201
202 OUT_RING(cmd);
Keith Packard7b832b52008-04-21 16:31:10 +1000203 OUT_RING(ropcpp | dst_pitch);
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100204 OUT_RING((y1 << 16) | rect->x1);
205 OUT_RING((y2 << 16) | rect->x2);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000206 OUT_RING(sarea_priv->front_offset);
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100207 OUT_RING((y1 << 16) | rect->x1);
Keith Packard7b832b52008-04-21 16:31:10 +1000208 OUT_RING(src_pitch);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000209 OUT_RING(sarea_priv->back_offset);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000210
211 ADVANCE_LP_RING();
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000212 }
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000213 }
214 }
215
Dave Airlieaf6061a2008-05-07 12:15:39 +1000216 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=3188a242006-12-11 18:32:27 +1100217
218 list_for_each_safe(hit, tmp, &hits) {
219 drm_i915_vbl_swap_t *swap_hit =
220 list_entry(hit, drm_i915_vbl_swap_t, head);
221
222 list_del(hit);
223
224 drm_free(swap_hit, sizeof(*swap_hit), DRM_MEM_DRIVER);
225 }
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
229{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000230 struct drm_device *dev = (struct drm_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000232 u16 temp;
Dave Airliee4a7b1d2007-09-28 11:46:28 +1000233 u32 pipea_stats, pipeb_stats;
234
Jesse Barnes585fb112008-07-29 11:54:06 -0700235 pipea_stats = I915_READ(PIPEASTAT);
236 pipeb_stats = I915_READ(PIPEBSTAT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000237
Jesse Barnes585fb112008-07-29 11:54:06 -0700238 temp = I915_READ16(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000239
Jesse Barnes585fb112008-07-29 11:54:06 -0700240 temp &= (I915_USER_INTERRUPT |
241 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
242 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000243
244 DRM_DEBUG("%s flag=%08x\n", __FUNCTION__, temp);
245
246 if (temp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return IRQ_NONE;
248
Jesse Barnes585fb112008-07-29 11:54:06 -0700249 I915_WRITE16(IIR, temp);
250 (void) I915_READ16(IIR);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000251 DRM_READMEMORYBARRIER();
Dave Airlie0d6aa602006-01-02 20:14:23 +1100252
Dave Airlieaf6061a2008-05-07 12:15:39 +1000253 dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Dave Airlie6e5fca52006-03-20 18:34:29 +1100254
Jesse Barnes585fb112008-07-29 11:54:06 -0700255 if (temp & I915_USER_INTERRUPT)
Dave Airlie0d6aa602006-01-02 20:14:23 +1100256 DRM_WAKEUP(&dev_priv->irq_queue);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000257
Jesse Barnes585fb112008-07-29 11:54:06 -0700258 if (temp & (I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
259 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT)) {
Dave Airlieaf6061a2008-05-07 12:15:39 +1000260 int vblank_pipe = dev_priv->vblank_pipe;
261
262 if ((vblank_pipe &
263 (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B))
264 == (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B)) {
Jesse Barnes585fb112008-07-29 11:54:06 -0700265 if (temp & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT)
Dave Airlieaf6061a2008-05-07 12:15:39 +1000266 atomic_inc(&dev->vbl_received);
Jesse Barnes585fb112008-07-29 11:54:06 -0700267 if (temp & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT)
Dave Airlieaf6061a2008-05-07 12:15:39 +1000268 atomic_inc(&dev->vbl_received2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700269 } else if (((temp & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) &&
Dave Airlieaf6061a2008-05-07 12:15:39 +1000270 (vblank_pipe & DRM_I915_VBLANK_PIPE_A)) ||
Jesse Barnes585fb112008-07-29 11:54:06 -0700271 ((temp & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) &&
Dave Airlieaf6061a2008-05-07 12:15:39 +1000272 (vblank_pipe & DRM_I915_VBLANK_PIPE_B)))
273 atomic_inc(&dev->vbl_received);
274
275 DRM_WAKEUP(&dev->vbl_queue);
276 drm_vbl_send_signals(dev);
277
=?utf-8?q?Michel_D=C3=A4nzer?=2228ed62006-10-25 01:05:09 +1000278 if (dev_priv->swaps_pending > 0)
279 drm_locked_tasklet(dev, i915_vblank_tasklet);
Jesse Barnes585fb112008-07-29 11:54:06 -0700280 I915_WRITE(PIPEASTAT,
Dave Airlieaf6061a2008-05-07 12:15:39 +1000281 pipea_stats|I915_VBLANK_INTERRUPT_ENABLE|
Jesse Barnes585fb112008-07-29 11:54:06 -0700282 PIPE_VBLANK_INTERRUPT_STATUS);
283 I915_WRITE(PIPEBSTAT,
Dave Airlieaf6061a2008-05-07 12:15:39 +1000284 pipeb_stats|I915_VBLANK_INTERRUPT_ENABLE|
Jesse Barnes585fb112008-07-29 11:54:06 -0700285 PIPE_VBLANK_INTERRUPT_STATUS);
Dave Airlie0d6aa602006-01-02 20:14:23 +1100286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 return IRQ_HANDLED;
289}
290
Dave Airlieaf6061a2008-05-07 12:15:39 +1000291static int i915_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 RING_LOCALS;
295
296 i915_kernel_lost_context(dev);
297
Márton Németh3e684ea2008-01-24 15:58:57 +1000298 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Alan Hourihanec29b6692006-08-12 16:29:24 +1000300 dev_priv->sarea_priv->last_enqueue = ++dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Alan Hourihanec29b6692006-08-12 16:29:24 +1000302 if (dev_priv->counter > 0x7FFFFFFFUL)
303 dev_priv->sarea_priv->last_enqueue = dev_priv->counter = 1;
304
305 BEGIN_LP_RING(6);
Jesse Barnes585fb112008-07-29 11:54:06 -0700306 OUT_RING(MI_STORE_DWORD_INDEX);
307 OUT_RING(5 << MI_STORE_DWORD_INDEX_SHIFT);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000308 OUT_RING(dev_priv->counter);
309 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 OUT_RING(0);
Jesse Barnes585fb112008-07-29 11:54:06 -0700311 OUT_RING(MI_USER_INTERRUPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ADVANCE_LP_RING();
Dave Airliebc5f4522007-11-05 12:50:58 +1000313
Alan Hourihanec29b6692006-08-12 16:29:24 +1000314 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Dave Airlie84b1fd12007-07-11 15:53:27 +1000317static int i915_wait_irq(struct drm_device * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
320 int ret = 0;
321
Márton Németh3e684ea2008-01-24 15:58:57 +1000322 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 READ_BREADCRUMB(dev_priv));
324
325 if (READ_BREADCRUMB(dev_priv) >= irq_nr)
326 return 0;
327
328 dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
329
330 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
331 READ_BREADCRUMB(dev_priv) >= irq_nr);
332
Eric Anholt20caafa2007-08-25 19:22:43 +1000333 if (ret == -EBUSY) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000334 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
336 }
337
Dave Airlieaf6061a2008-05-07 12:15:39 +1000338 dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return ret;
340}
341
Dave Airlieaf6061a2008-05-07 12:15:39 +1000342static int i915_driver_vblank_do_wait(struct drm_device *dev, unsigned int *sequence,
343 atomic_t *counter)
344{
345 drm_i915_private_t *dev_priv = dev->dev_private;
346 unsigned int cur_vblank;
347 int ret = 0;
348
349 if (!dev_priv) {
350 DRM_ERROR("called with no initialization\n");
351 return -EINVAL;
352 }
353
354 DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
355 (((cur_vblank = atomic_read(counter))
356 - *sequence) <= (1<<23)));
357
358 *sequence = cur_vblank;
359
360 return ret;
361}
362
363
364int i915_driver_vblank_wait(struct drm_device *dev, unsigned int *sequence)
365{
366 return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received);
367}
368
369int i915_driver_vblank_wait2(struct drm_device *dev, unsigned int *sequence)
370{
371 return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received2);
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/* Needs the lock as it touches the ring.
375 */
Eric Anholtc153f452007-09-03 12:06:45 +1000376int i915_irq_emit(struct drm_device *dev, void *data,
377 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000380 drm_i915_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int result;
382
Eric Anholt6c340ea2007-08-25 20:23:09 +1000383 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000386 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 result = i915_emit_irq(dev);
391
Eric Anholtc153f452007-09-03 12:06:45 +1000392 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000394 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 return 0;
398}
399
400/* Doesn't need the hardware lock.
401 */
Eric Anholtc153f452007-09-03 12:06:45 +1000402int i915_irq_wait(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_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000409 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
Eric Anholtc153f452007-09-03 12:06:45 +1000413 return i915_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Dave Airlie84b1fd12007-07-11 15:53:27 +1000416static void i915_enable_interrupt (struct drm_device *dev)
Dave Airlie702880f2006-06-24 17:07:34 +1000417{
418 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000419 u16 flag;
Dave Airlie702880f2006-06-24 17:07:34 +1000420
Dave Airlieaf6061a2008-05-07 12:15:39 +1000421 flag = 0;
422 if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A)
Jesse Barnes585fb112008-07-29 11:54:06 -0700423 flag |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000424 if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B)
Jesse Barnes585fb112008-07-29 11:54:06 -0700425 flag |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000426
Jesse Barnes585fb112008-07-29 11:54:06 -0700427 I915_WRITE16(IER, I915_USER_INTERRUPT | flag);
Dave Airlie702880f2006-06-24 17:07:34 +1000428}
429
430/* Set the vblank monitor pipe
431 */
Eric Anholtc153f452007-09-03 12:06:45 +1000432int i915_vblank_pipe_set(struct drm_device *dev, void *data,
433 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000434{
Dave Airlie702880f2006-06-24 17:07:34 +1000435 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000436 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +1000437
438 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000439 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000440 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000441 }
442
Eric Anholtc153f452007-09-03 12:06:45 +1000443 if (pipe->pipe & ~(DRM_I915_VBLANK_PIPE_A|DRM_I915_VBLANK_PIPE_B)) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000444 DRM_ERROR("called with invalid pipe 0x%x\n", pipe->pipe);
Eric Anholt20caafa2007-08-25 19:22:43 +1000445 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000446 }
447
Eric Anholtc153f452007-09-03 12:06:45 +1000448 dev_priv->vblank_pipe = pipe->pipe;
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000449
Dave Airlieaf6061a2008-05-07 12:15:39 +1000450 i915_enable_interrupt (dev);
451
=?utf-8?q?Michel_D=C3=A4nzer?=5b516942006-10-25 00:08:23 +1000452 return 0;
Dave Airlie702880f2006-06-24 17:07:34 +1000453}
454
Eric Anholtc153f452007-09-03 12:06:45 +1000455int i915_vblank_pipe_get(struct drm_device *dev, void *data,
456 struct drm_file *file_priv)
Dave Airlie702880f2006-06-24 17:07:34 +1000457{
Dave Airlie702880f2006-06-24 17:07:34 +1000458 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000459 drm_i915_vblank_pipe_t *pipe = data;
Dave Airlie702880f2006-06-24 17:07:34 +1000460 u16 flag;
461
462 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000463 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000464 return -EINVAL;
Dave Airlie702880f2006-06-24 17:07:34 +1000465 }
466
Jesse Barnes585fb112008-07-29 11:54:06 -0700467 flag = I915_READ(IER);
Eric Anholtc153f452007-09-03 12:06:45 +1000468 pipe->pipe = 0;
Jesse Barnes585fb112008-07-29 11:54:06 -0700469 if (flag & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT)
Eric Anholtc153f452007-09-03 12:06:45 +1000470 pipe->pipe |= DRM_I915_VBLANK_PIPE_A;
Jesse Barnes585fb112008-07-29 11:54:06 -0700471 if (flag & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT)
Eric Anholtc153f452007-09-03 12:06:45 +1000472 pipe->pipe |= DRM_I915_VBLANK_PIPE_B;
473
Dave Airlie702880f2006-06-24 17:07:34 +1000474 return 0;
475}
476
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000477/**
478 * Schedule buffer swap at given vertical blank.
479 */
Eric Anholtc153f452007-09-03 12:06:45 +1000480int i915_vblank_swap(struct drm_device *dev, void *data,
481 struct drm_file *file_priv)
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000482{
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000483 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000484 drm_i915_vblank_swap_t *swap = data;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000485 drm_i915_vbl_swap_t *vbl_swap;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000486 unsigned int pipe, seqtype, curseq;
=?utf-8?q?Michel_D=C3=A4nzer?=a0b136b2006-10-25 00:12:52 +1000487 unsigned long irqflags;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000488 struct list_head *list;
489
490 if (!dev_priv) {
491 DRM_ERROR("%s called with no initialization\n", __func__);
Eric Anholt20caafa2007-08-25 19:22:43 +1000492 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000493 }
494
Dave Airlieaf6061a2008-05-07 12:15:39 +1000495 if (dev_priv->sarea_priv->rotation) {
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000496 DRM_DEBUG("Rotation not supported\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000497 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000498 }
499
Eric Anholtc153f452007-09-03 12:06:45 +1000500 if (swap->seqtype & ~(_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE |
Dave Airlieaf6061a2008-05-07 12:15:39 +1000501 _DRM_VBLANK_SECONDARY | _DRM_VBLANK_NEXTONMISS)) {
Eric Anholtc153f452007-09-03 12:06:45 +1000502 DRM_ERROR("Invalid sequence type 0x%x\n", swap->seqtype);
Eric Anholt20caafa2007-08-25 19:22:43 +1000503 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000504 }
505
Dave Airlieaf6061a2008-05-07 12:15:39 +1000506 pipe = (swap->seqtype & _DRM_VBLANK_SECONDARY) ? 1 : 0;
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000507
Eric Anholtc153f452007-09-03 12:06:45 +1000508 seqtype = swap->seqtype & (_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE);
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000509
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000510 if (!(dev_priv->vblank_pipe & (1 << pipe))) {
511 DRM_ERROR("Invalid pipe %d\n", pipe);
Eric Anholt20caafa2007-08-25 19:22:43 +1000512 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000513 }
514
515 spin_lock_irqsave(&dev->drw_lock, irqflags);
516
Eric Anholtc153f452007-09-03 12:06:45 +1000517 if (!drm_get_drawable_info(dev, swap->drawable)) {
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000518 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
Eric Anholtc153f452007-09-03 12:06:45 +1000519 DRM_DEBUG("Invalid drawable ID %d\n", swap->drawable);
Eric Anholt20caafa2007-08-25 19:22:43 +1000520 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000521 }
522
523 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
524
Dave Airlieaf6061a2008-05-07 12:15:39 +1000525 curseq = atomic_read(pipe ? &dev->vbl_received2 : &dev->vbl_received);
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000526
=?utf-8?q?Michel_D=C3=A4nzer?=2228ed62006-10-25 01:05:09 +1000527 if (seqtype == _DRM_VBLANK_RELATIVE)
Eric Anholtc153f452007-09-03 12:06:45 +1000528 swap->sequence += curseq;
=?utf-8?q?Michel_D=C3=A4nzer?=2228ed62006-10-25 01:05:09 +1000529
Eric Anholtc153f452007-09-03 12:06:45 +1000530 if ((curseq - swap->sequence) <= (1<<23)) {
531 if (swap->seqtype & _DRM_VBLANK_NEXTONMISS) {
532 swap->sequence = curseq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=2228ed62006-10-25 01:05:09 +1000533 } else {
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000534 DRM_DEBUG("Missed target sequence\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000535 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000536 }
=?utf-8?q?Michel_D=C3=A4nzer?=541f29a2006-10-24 23:38:54 +1000537 }
538
=?utf-8?q?Michel_D=C3=A4nzer?=2228ed62006-10-25 01:05:09 +1000539 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
540
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000541 list_for_each(list, &dev_priv->vbl_swaps.head) {
542 vbl_swap = list_entry(list, drm_i915_vbl_swap_t, head);
543
Eric Anholtc153f452007-09-03 12:06:45 +1000544 if (vbl_swap->drw_id == swap->drawable &&
Dave Airlieaf6061a2008-05-07 12:15:39 +1000545 vbl_swap->pipe == pipe &&
Eric Anholtc153f452007-09-03 12:06:45 +1000546 vbl_swap->sequence == swap->sequence) {
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000547 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
548 DRM_DEBUG("Already scheduled\n");
549 return 0;
550 }
551 }
552
553 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
554
=?utf-8?q?Michel_D=C3=A4nzer?=21fa60e2006-10-25 00:10:59 +1000555 if (dev_priv->swaps_pending >= 100) {
556 DRM_DEBUG("Too many swaps queued\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000557 return -EBUSY;
=?utf-8?q?Michel_D=C3=A4nzer?=21fa60e2006-10-25 00:10:59 +1000558 }
559
Dave Airlie54583bf2007-10-14 21:21:30 +1000560 vbl_swap = drm_calloc(1, sizeof(*vbl_swap), DRM_MEM_DRIVER);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000561
562 if (!vbl_swap) {
563 DRM_ERROR("Failed to allocate memory to queue swap\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000564 return -ENOMEM;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000565 }
566
567 DRM_DEBUG("\n");
568
Eric Anholtc153f452007-09-03 12:06:45 +1000569 vbl_swap->drw_id = swap->drawable;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000570 vbl_swap->pipe = pipe;
Eric Anholtc153f452007-09-03 12:06:45 +1000571 vbl_swap->sequence = swap->sequence;
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000572
573 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
574
Li Zefand5b0d1b2007-12-17 09:47:19 +1000575 list_add_tail(&vbl_swap->head, &dev_priv->vbl_swaps.head);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000576 dev_priv->swaps_pending++;
577
578 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
579
580 return 0;
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/* drm_dma.h hooks
584*/
Dave Airlie84b1fd12007-07-11 15:53:27 +1000585void i915_driver_irq_preinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
588
Jesse Barnes585fb112008-07-29 11:54:06 -0700589 I915_WRITE16(HWSTAM, 0xfffe);
590 I915_WRITE16(IMR, 0x0);
591 I915_WRITE16(IER, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Dave Airlieaf6061a2008-05-07 12:15:39 +1000594void i915_driver_irq_postinstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
597
Thomas Gleixnera6399bd2007-05-26 05:56:14 +1000598 spin_lock_init(&dev_priv->swaps_lock);
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000599 INIT_LIST_HEAD(&dev_priv->vbl_swaps.head);
600 dev_priv->swaps_pending = 0;
601
Dave Airlieaf6061a2008-05-07 12:15:39 +1000602 if (!dev_priv->vblank_pipe)
603 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A;
Dave Airlie702880f2006-06-24 17:07:34 +1000604 i915_enable_interrupt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
606}
607
Dave Airlie84b1fd12007-07-11 15:53:27 +1000608void i915_driver_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000611 u16 temp;
Dave Airlie91e37382006-02-18 15:17:04 +1100612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!dev_priv)
614 return;
615
Jesse Barnes585fb112008-07-29 11:54:06 -0700616 I915_WRITE16(HWSTAM, 0xffff);
617 I915_WRITE16(IMR, 0xffff);
618 I915_WRITE16(IER, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +1100619
Jesse Barnes585fb112008-07-29 11:54:06 -0700620 temp = I915_READ16(IIR);
621 I915_WRITE16(IIR, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}