Pull button into test branch
[linux-2.6] / drivers / char / drm / i915_irq.c
1 /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2  */
3 /*
4  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  */
28
29 #include "drmP.h"
30 #include "drm.h"
31 #include "i915_drm.h"
32 #include "i915_drv.h"
33
34 #define USER_INT_FLAG (1<<1)
35 #define VSYNC_PIPEB_FLAG (1<<5)
36 #define VSYNC_PIPEA_FLAG (1<<7)
37
38 #define MAX_NOPID ((u32)~0)
39
40 /**
41  * Emit blits for scheduled buffer swaps.
42  *
43  * This function will be called with the HW lock held.
44  */
45 static void i915_vblank_tasklet(drm_device_t *dev)
46 {
47         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
48         unsigned long irqflags;
49         struct list_head *list, *tmp;
50
51         DRM_DEBUG("\n");
52
53         spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
54
55         list_for_each_safe(list, tmp, &dev_priv->vbl_swaps.head) {
56                 drm_i915_vbl_swap_t *vbl_swap =
57                         list_entry(list, drm_i915_vbl_swap_t, head);
58                 atomic_t *counter = vbl_swap->pipe ? &dev->vbl_received2 :
59                         &dev->vbl_received;
60
61                 if ((atomic_read(counter) - vbl_swap->sequence) <= (1<<23)) {
62                         drm_drawable_info_t *drw;
63
64                         spin_unlock(&dev_priv->swaps_lock);
65
66                         spin_lock(&dev->drw_lock);
67
68                         drw = drm_get_drawable_info(dev, vbl_swap->drw_id);
69                                 
70                         if (drw) {
71                                 int i, num_rects = drw->num_rects;
72                                 drm_clip_rect_t *rect = drw->rects;
73                                 drm_i915_sarea_t *sarea_priv =
74                                     dev_priv->sarea_priv;
75                                 u32 cpp = dev_priv->cpp;
76                                 u32 cmd = (cpp == 4) ? (XY_SRC_COPY_BLT_CMD |
77                                                         XY_SRC_COPY_BLT_WRITE_ALPHA |
78                                                         XY_SRC_COPY_BLT_WRITE_RGB)
79                                                      : XY_SRC_COPY_BLT_CMD;
80                                 u32 pitchropcpp = (sarea_priv->pitch * cpp) |
81                                                   (0xcc << 16) | (cpp << 23) |
82                                                   (1 << 24);
83                                 RING_LOCALS;
84
85                                 i915_kernel_lost_context(dev);
86
87                                 BEGIN_LP_RING(6);
88
89                                 OUT_RING(GFX_OP_DRAWRECT_INFO);
90                                 OUT_RING(0);
91                                 OUT_RING(0);
92                                 OUT_RING(sarea_priv->width |
93                                          sarea_priv->height << 16);
94                                 OUT_RING(sarea_priv->width |
95                                          sarea_priv->height << 16);
96                                 OUT_RING(0);
97
98                                 ADVANCE_LP_RING();
99
100                                 sarea_priv->ctxOwner = DRM_KERNEL_CONTEXT;
101
102                                 for (i = 0; i < num_rects; i++, rect++) {
103                                         BEGIN_LP_RING(8);
104
105                                         OUT_RING(cmd);
106                                         OUT_RING(pitchropcpp);
107                                         OUT_RING((rect->y1 << 16) | rect->x1);
108                                         OUT_RING((rect->y2 << 16) | rect->x2);
109                                         OUT_RING(sarea_priv->front_offset);
110                                         OUT_RING((rect->y1 << 16) | rect->x1);
111                                         OUT_RING(pitchropcpp & 0xffff);
112                                         OUT_RING(sarea_priv->back_offset);
113
114                                         ADVANCE_LP_RING();
115                                 }
116                         }
117
118                         spin_unlock(&dev->drw_lock);
119
120                         spin_lock(&dev_priv->swaps_lock);
121
122                         list_del(list);
123
124                         drm_free(vbl_swap, sizeof(*vbl_swap), DRM_MEM_DRIVER);
125
126                         dev_priv->swaps_pending--;
127                 }
128         }
129
130         spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
131 }
132
133 irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
134 {
135         drm_device_t *dev = (drm_device_t *) arg;
136         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
137         u16 temp;
138
139         temp = I915_READ16(I915REG_INT_IDENTITY_R);
140
141         temp &= (USER_INT_FLAG | VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG);
142
143         DRM_DEBUG("%s flag=%08x\n", __FUNCTION__, temp);
144
145         if (temp == 0)
146                 return IRQ_NONE;
147
148         I915_WRITE16(I915REG_INT_IDENTITY_R, temp);
149
150         dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
151
152         if (temp & USER_INT_FLAG)
153                 DRM_WAKEUP(&dev_priv->irq_queue);
154
155         if (temp & (VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG)) {
156                 int vblank_pipe = dev_priv->vblank_pipe;
157
158                 if ((vblank_pipe &
159                      (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B))
160                     == (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B)) {
161                         if (temp & VSYNC_PIPEA_FLAG)
162                                 atomic_inc(&dev->vbl_received);
163                         if (temp & VSYNC_PIPEB_FLAG)
164                                 atomic_inc(&dev->vbl_received2);
165                 } else if (((temp & VSYNC_PIPEA_FLAG) &&
166                             (vblank_pipe & DRM_I915_VBLANK_PIPE_A)) ||
167                            ((temp & VSYNC_PIPEB_FLAG) &&
168                             (vblank_pipe & DRM_I915_VBLANK_PIPE_B)))
169                         atomic_inc(&dev->vbl_received);
170
171                 DRM_WAKEUP(&dev->vbl_queue);
172                 drm_vbl_send_signals(dev);
173
174                 if (dev_priv->swaps_pending > 0)
175                         drm_locked_tasklet(dev, i915_vblank_tasklet);
176         }
177
178         return IRQ_HANDLED;
179 }
180
181 static int i915_emit_irq(drm_device_t * dev)
182 {
183         drm_i915_private_t *dev_priv = dev->dev_private;
184         RING_LOCALS;
185
186         i915_kernel_lost_context(dev);
187
188         DRM_DEBUG("%s\n", __FUNCTION__);
189
190         dev_priv->sarea_priv->last_enqueue = ++dev_priv->counter;
191
192         if (dev_priv->counter > 0x7FFFFFFFUL)
193                 dev_priv->sarea_priv->last_enqueue = dev_priv->counter = 1;
194
195         BEGIN_LP_RING(6);
196         OUT_RING(CMD_STORE_DWORD_IDX);
197         OUT_RING(20);
198         OUT_RING(dev_priv->counter);
199         OUT_RING(0);
200         OUT_RING(0);
201         OUT_RING(GFX_OP_USER_INTERRUPT);
202         ADVANCE_LP_RING();
203         
204         return dev_priv->counter;
205 }
206
207 static int i915_wait_irq(drm_device_t * dev, int irq_nr)
208 {
209         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
210         int ret = 0;
211
212         DRM_DEBUG("%s irq_nr=%d breadcrumb=%d\n", __FUNCTION__, irq_nr,
213                   READ_BREADCRUMB(dev_priv));
214
215         if (READ_BREADCRUMB(dev_priv) >= irq_nr)
216                 return 0;
217
218         dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
219
220         DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
221                     READ_BREADCRUMB(dev_priv) >= irq_nr);
222
223         if (ret == DRM_ERR(EBUSY)) {
224                 DRM_ERROR("%s: EBUSY -- rec: %d emitted: %d\n",
225                           __FUNCTION__,
226                           READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
227         }
228
229         dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
230         return ret;
231 }
232
233 static int i915_driver_vblank_do_wait(drm_device_t *dev, unsigned int *sequence,
234                                       atomic_t *counter)
235 {
236         drm_i915_private_t *dev_priv = dev->dev_private;
237         unsigned int cur_vblank;
238         int ret = 0;
239
240         if (!dev_priv) {
241                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
242                 return DRM_ERR(EINVAL);
243         }
244
245         DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
246                     (((cur_vblank = atomic_read(counter))
247                         - *sequence) <= (1<<23)));
248         
249         *sequence = cur_vblank;
250
251         return ret;
252 }
253
254
255 int i915_driver_vblank_wait(drm_device_t *dev, unsigned int *sequence)
256 {
257         return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received);
258 }
259
260 int i915_driver_vblank_wait2(drm_device_t *dev, unsigned int *sequence)
261 {
262         return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received2);
263 }
264
265 /* Needs the lock as it touches the ring.
266  */
267 int i915_irq_emit(DRM_IOCTL_ARGS)
268 {
269         DRM_DEVICE;
270         drm_i915_private_t *dev_priv = dev->dev_private;
271         drm_i915_irq_emit_t emit;
272         int result;
273
274         LOCK_TEST_WITH_RETURN(dev, filp);
275
276         if (!dev_priv) {
277                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
278                 return DRM_ERR(EINVAL);
279         }
280
281         DRM_COPY_FROM_USER_IOCTL(emit, (drm_i915_irq_emit_t __user *) data,
282                                  sizeof(emit));
283
284         result = i915_emit_irq(dev);
285
286         if (DRM_COPY_TO_USER(emit.irq_seq, &result, sizeof(int))) {
287                 DRM_ERROR("copy_to_user\n");
288                 return DRM_ERR(EFAULT);
289         }
290
291         return 0;
292 }
293
294 /* Doesn't need the hardware lock.
295  */
296 int i915_irq_wait(DRM_IOCTL_ARGS)
297 {
298         DRM_DEVICE;
299         drm_i915_private_t *dev_priv = dev->dev_private;
300         drm_i915_irq_wait_t irqwait;
301
302         if (!dev_priv) {
303                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
304                 return DRM_ERR(EINVAL);
305         }
306
307         DRM_COPY_FROM_USER_IOCTL(irqwait, (drm_i915_irq_wait_t __user *) data,
308                                  sizeof(irqwait));
309
310         return i915_wait_irq(dev, irqwait.irq_seq);
311 }
312
313 static void i915_enable_interrupt (drm_device_t *dev)
314 {
315         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
316         u16 flag;
317
318         flag = 0;
319         if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A)
320                 flag |= VSYNC_PIPEA_FLAG;
321         if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B)
322                 flag |= VSYNC_PIPEB_FLAG;
323
324         I915_WRITE16(I915REG_INT_ENABLE_R, USER_INT_FLAG | flag);
325 }
326
327 /* Set the vblank monitor pipe
328  */
329 int i915_vblank_pipe_set(DRM_IOCTL_ARGS)
330 {
331         DRM_DEVICE;
332         drm_i915_private_t *dev_priv = dev->dev_private;
333         drm_i915_vblank_pipe_t pipe;
334
335         if (!dev_priv) {
336                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
337                 return DRM_ERR(EINVAL);
338         }
339
340         DRM_COPY_FROM_USER_IOCTL(pipe, (drm_i915_vblank_pipe_t __user *) data,
341                                  sizeof(pipe));
342
343         if (pipe.pipe & ~(DRM_I915_VBLANK_PIPE_A|DRM_I915_VBLANK_PIPE_B)) {
344                 DRM_ERROR("%s called with invalid pipe 0x%x\n", 
345                           __FUNCTION__, pipe.pipe);
346                 return DRM_ERR(EINVAL);
347         }
348
349         dev_priv->vblank_pipe = pipe.pipe;
350
351         i915_enable_interrupt (dev);
352
353         return 0;
354 }
355
356 int i915_vblank_pipe_get(DRM_IOCTL_ARGS)
357 {
358         DRM_DEVICE;
359         drm_i915_private_t *dev_priv = dev->dev_private;
360         drm_i915_vblank_pipe_t pipe;
361         u16 flag;
362
363         if (!dev_priv) {
364                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
365                 return DRM_ERR(EINVAL);
366         }
367
368         flag = I915_READ(I915REG_INT_ENABLE_R);
369         pipe.pipe = 0;
370         if (flag & VSYNC_PIPEA_FLAG)
371                 pipe.pipe |= DRM_I915_VBLANK_PIPE_A;
372         if (flag & VSYNC_PIPEB_FLAG)
373                 pipe.pipe |= DRM_I915_VBLANK_PIPE_B;
374         DRM_COPY_TO_USER_IOCTL((drm_i915_vblank_pipe_t __user *) data, pipe,
375                                  sizeof(pipe));
376         return 0;
377 }
378
379 /**
380  * Schedule buffer swap at given vertical blank.
381  */
382 int i915_vblank_swap(DRM_IOCTL_ARGS)
383 {
384         DRM_DEVICE;
385         drm_i915_private_t *dev_priv = dev->dev_private;
386         drm_i915_vblank_swap_t swap;
387         drm_i915_vbl_swap_t *vbl_swap;
388         unsigned int pipe, seqtype, curseq;
389         unsigned long irqflags;
390         struct list_head *list;
391
392         if (!dev_priv) {
393                 DRM_ERROR("%s called with no initialization\n", __func__);
394                 return DRM_ERR(EINVAL);
395         }
396
397         if (dev_priv->sarea_priv->rotation) {
398                 DRM_DEBUG("Rotation not supported\n");
399                 return DRM_ERR(EINVAL);
400         }
401
402         DRM_COPY_FROM_USER_IOCTL(swap, (drm_i915_vblank_swap_t __user *) data,
403                                  sizeof(swap));
404
405         if (swap.seqtype & ~(_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE |
406                              _DRM_VBLANK_SECONDARY | _DRM_VBLANK_NEXTONMISS)) {
407                 DRM_ERROR("Invalid sequence type 0x%x\n", swap.seqtype);
408                 return DRM_ERR(EINVAL);
409         }
410
411         pipe = (swap.seqtype & _DRM_VBLANK_SECONDARY) ? 1 : 0;
412
413         seqtype = swap.seqtype & (_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE);
414
415         if (!(dev_priv->vblank_pipe & (1 << pipe))) {
416                 DRM_ERROR("Invalid pipe %d\n", pipe);
417                 return DRM_ERR(EINVAL);
418         }
419
420         spin_lock_irqsave(&dev->drw_lock, irqflags);
421
422         if (!drm_get_drawable_info(dev, swap.drawable)) {
423                 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
424                 DRM_ERROR("Invalid drawable ID %d\n", swap.drawable);
425                 return DRM_ERR(EINVAL);
426         }
427
428         spin_unlock_irqrestore(&dev->drw_lock, irqflags);
429
430         curseq = atomic_read(pipe ? &dev->vbl_received2 : &dev->vbl_received);
431
432         if (seqtype == _DRM_VBLANK_RELATIVE)
433                 swap.sequence += curseq;
434
435         if ((curseq - swap.sequence) <= (1<<23)) {
436                 if (swap.seqtype & _DRM_VBLANK_NEXTONMISS) {
437                         swap.sequence = curseq + 1;
438                 } else {
439                         DRM_DEBUG("Missed target sequence\n");
440                         return DRM_ERR(EINVAL);
441                 }
442         }
443
444         spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
445
446         list_for_each(list, &dev_priv->vbl_swaps.head) {
447                 vbl_swap = list_entry(list, drm_i915_vbl_swap_t, head);
448
449                 if (vbl_swap->drw_id == swap.drawable &&
450                     vbl_swap->pipe == pipe &&
451                     vbl_swap->sequence == swap.sequence) {
452                         spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
453                         DRM_DEBUG("Already scheduled\n");
454                         return 0;
455                 }
456         }
457
458         spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
459
460         if (dev_priv->swaps_pending >= 100) {
461                 DRM_DEBUG("Too many swaps queued\n");
462                 return DRM_ERR(EBUSY);
463         }
464
465         vbl_swap = drm_calloc(1, sizeof(vbl_swap), DRM_MEM_DRIVER);
466
467         if (!vbl_swap) {
468                 DRM_ERROR("Failed to allocate memory to queue swap\n");
469                 return DRM_ERR(ENOMEM);
470         }
471
472         DRM_DEBUG("\n");
473
474         vbl_swap->drw_id = swap.drawable;
475         vbl_swap->pipe = pipe;
476         vbl_swap->sequence = swap.sequence;
477
478         spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
479
480         list_add_tail((struct list_head *)vbl_swap, &dev_priv->vbl_swaps.head);
481         dev_priv->swaps_pending++;
482
483         spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
484
485         DRM_COPY_TO_USER_IOCTL((drm_i915_vblank_swap_t __user *) data, swap,
486                                sizeof(swap));
487
488         return 0;
489 }
490
491 /* drm_dma.h hooks
492 */
493 void i915_driver_irq_preinstall(drm_device_t * dev)
494 {
495         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
496
497         I915_WRITE16(I915REG_HWSTAM, 0xfffe);
498         I915_WRITE16(I915REG_INT_MASK_R, 0x0);
499         I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
500 }
501
502 void i915_driver_irq_postinstall(drm_device_t * dev)
503 {
504         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
505
506         dev_priv->swaps_lock = SPIN_LOCK_UNLOCKED;
507         INIT_LIST_HEAD(&dev_priv->vbl_swaps.head);
508         dev_priv->swaps_pending = 0;
509
510         if (!dev_priv->vblank_pipe)
511                 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A;
512         i915_enable_interrupt(dev);
513         DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
514 }
515
516 void i915_driver_irq_uninstall(drm_device_t * dev)
517 {
518         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
519         u16 temp;
520
521         if (!dev_priv)
522                 return;
523
524         I915_WRITE16(I915REG_HWSTAM, 0xffff);
525         I915_WRITE16(I915REG_INT_MASK_R, 0xffff);
526         I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
527
528         temp = I915_READ16(I915REG_INT_IDENTITY_R);
529         I915_WRITE16(I915REG_INT_IDENTITY_R, temp);
530 }