1 /* i810_dma.c -- DMA support for the i810 -*- linux-c -*-
2 * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
29 * Keith Whitwell <keith@tungstengraphics.com>
37 #include <linux/interrupt.h> /* For task queue support */
38 #include <linux/delay.h>
39 #include <linux/pagemap.h>
41 #define I810_BUF_FREE 2
42 #define I810_BUF_CLIENT 1
43 #define I810_BUF_HARDWARE 0
45 #define I810_BUF_UNMAPPED 0
46 #define I810_BUF_MAPPED 1
48 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,2)
49 #define down_write down
53 static drm_buf_t *i810_freelist_get(drm_device_t *dev)
55 drm_device_dma_t *dma = dev->dma;
59 /* Linear search might not be the best solution */
61 for (i = 0; i < dma->buf_count; i++) {
62 drm_buf_t *buf = dma->buflist[ i ];
63 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
64 /* In use is already a pointer */
65 used = cmpxchg(buf_priv->in_use, I810_BUF_FREE,
67 if (used == I810_BUF_FREE) {
74 /* This should only be called if the buffer is not sent to the hardware
75 * yet, the hardware updates in use for us once its on the ring buffer.
78 static int i810_freelist_put(drm_device_t *dev, drm_buf_t *buf)
80 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
83 /* In use is already a pointer */
84 used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_FREE);
85 if (used != I810_BUF_CLIENT) {
86 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
93 static int i810_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
95 drm_file_t *priv = filp->private_data;
97 drm_i810_private_t *dev_priv;
99 drm_i810_buf_priv_t *buf_priv;
102 dev = priv->head->dev;
103 dev_priv = dev->dev_private;
104 buf = dev_priv->mmap_buffer;
105 buf_priv = buf->dev_private;
107 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
110 buf_priv->currently_mapped = I810_BUF_MAPPED;
113 if (io_remap_pfn_range(vma, vma->vm_start,
114 VM_OFFSET(vma) >> PAGE_SHIFT,
115 vma->vm_end - vma->vm_start,
116 vma->vm_page_prot)) return -EAGAIN;
120 static struct file_operations i810_buffer_fops = {
123 .release = drm_release,
125 .mmap = i810_mmap_buffers,
126 .fasync = drm_fasync,
129 static int i810_map_buffer(drm_buf_t *buf, struct file *filp)
131 drm_file_t *priv = filp->private_data;
132 drm_device_t *dev = priv->head->dev;
133 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
134 drm_i810_private_t *dev_priv = dev->dev_private;
135 struct file_operations *old_fops;
138 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
141 down_write( ¤t->mm->mmap_sem );
142 old_fops = filp->f_op;
143 filp->f_op = &i810_buffer_fops;
144 dev_priv->mmap_buffer = buf;
145 buf_priv->virtual = (void *)do_mmap(filp, 0, buf->total,
146 PROT_READ|PROT_WRITE,
149 dev_priv->mmap_buffer = NULL;
150 filp->f_op = old_fops;
151 if ((unsigned long)buf_priv->virtual > -1024UL) {
153 DRM_ERROR("mmap error\n");
154 retcode = (signed int)buf_priv->virtual;
155 buf_priv->virtual = NULL;
157 up_write( ¤t->mm->mmap_sem );
162 static int i810_unmap_buffer(drm_buf_t *buf)
164 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
167 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
170 down_write(¤t->mm->mmap_sem);
171 retcode = do_munmap(current->mm,
172 (unsigned long)buf_priv->virtual,
173 (size_t) buf->total);
174 up_write(¤t->mm->mmap_sem);
176 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
177 buf_priv->virtual = NULL;
182 static int i810_dma_get_buffer(drm_device_t *dev, drm_i810_dma_t *d,
186 drm_i810_buf_priv_t *buf_priv;
189 buf = i810_freelist_get(dev);
192 DRM_DEBUG("retcode=%d\n", retcode);
196 retcode = i810_map_buffer(buf, filp);
198 i810_freelist_put(dev, buf);
199 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
203 buf_priv = buf->dev_private;
205 d->request_idx = buf->idx;
206 d->request_size = buf->total;
207 d->virtual = buf_priv->virtual;
212 static int i810_dma_cleanup(drm_device_t *dev)
214 drm_device_dma_t *dma = dev->dma;
216 /* Make sure interrupts are disabled here because the uninstall ioctl
217 * may not have been called from userspace and after dev_private
218 * is freed, it's too late.
220 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
221 drm_irq_uninstall(dev);
223 if (dev->dev_private) {
225 drm_i810_private_t *dev_priv =
226 (drm_i810_private_t *) dev->dev_private;
228 if (dev_priv->ring.virtual_start) {
229 drm_ioremapfree((void *) dev_priv->ring.virtual_start,
230 dev_priv->ring.Size, dev);
232 if (dev_priv->hw_status_page) {
233 pci_free_consistent(dev->pdev, PAGE_SIZE,
234 dev_priv->hw_status_page,
235 dev_priv->dma_status_page);
236 /* Need to rewrite hardware status page */
237 I810_WRITE(0x02080, 0x1ffff000);
239 drm_free(dev->dev_private, sizeof(drm_i810_private_t),
241 dev->dev_private = NULL;
243 for (i = 0; i < dma->buf_count; i++) {
244 drm_buf_t *buf = dma->buflist[ i ];
245 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
246 if ( buf_priv->kernel_virtual && buf->total )
247 drm_ioremapfree(buf_priv->kernel_virtual, buf->total, dev);
253 static int i810_wait_ring(drm_device_t *dev, int n)
255 drm_i810_private_t *dev_priv = dev->dev_private;
256 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
259 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
261 end = jiffies + (HZ*3);
262 while (ring->space < n) {
263 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
264 ring->space = ring->head - (ring->tail+8);
265 if (ring->space < 0) ring->space += ring->Size;
267 if (ring->head != last_head) {
268 end = jiffies + (HZ*3);
269 last_head = ring->head;
273 if (time_before(end, jiffies)) {
274 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
275 DRM_ERROR("lockup\n");
285 static void i810_kernel_lost_context(drm_device_t *dev)
287 drm_i810_private_t *dev_priv = dev->dev_private;
288 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
290 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
291 ring->tail = I810_READ(LP_RING + RING_TAIL);
292 ring->space = ring->head - (ring->tail+8);
293 if (ring->space < 0) ring->space += ring->Size;
296 static int i810_freelist_init(drm_device_t *dev, drm_i810_private_t *dev_priv)
298 drm_device_dma_t *dma = dev->dma;
300 u32 *hw_status = (u32 *)(dev_priv->hw_status_page + my_idx);
303 if (dma->buf_count > 1019) {
304 /* Not enough space in the status page for the freelist */
308 for (i = 0; i < dma->buf_count; i++) {
309 drm_buf_t *buf = dma->buflist[ i ];
310 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
312 buf_priv->in_use = hw_status++;
313 buf_priv->my_use_idx = my_idx;
316 *buf_priv->in_use = I810_BUF_FREE;
318 buf_priv->kernel_virtual = drm_ioremap(buf->bus_address,
324 static int i810_dma_initialize(drm_device_t *dev,
325 drm_i810_private_t *dev_priv,
326 drm_i810_init_t *init)
328 struct list_head *list;
330 memset(dev_priv, 0, sizeof(drm_i810_private_t));
332 list_for_each(list, &dev->maplist->head) {
333 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
335 r_list->map->type == _DRM_SHM &&
336 r_list->map->flags & _DRM_CONTAINS_LOCK ) {
337 dev_priv->sarea_map = r_list->map;
341 if (!dev_priv->sarea_map) {
342 dev->dev_private = (void *)dev_priv;
343 i810_dma_cleanup(dev);
344 DRM_ERROR("can not find sarea!\n");
347 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
348 if (!dev_priv->mmio_map) {
349 dev->dev_private = (void *)dev_priv;
350 i810_dma_cleanup(dev);
351 DRM_ERROR("can not find mmio map!\n");
354 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
355 if (!dev->agp_buffer_map) {
356 dev->dev_private = (void *)dev_priv;
357 i810_dma_cleanup(dev);
358 DRM_ERROR("can not find dma buffer map!\n");
362 dev_priv->sarea_priv = (drm_i810_sarea_t *)
363 ((u8 *)dev_priv->sarea_map->handle +
364 init->sarea_priv_offset);
366 dev_priv->ring.Start = init->ring_start;
367 dev_priv->ring.End = init->ring_end;
368 dev_priv->ring.Size = init->ring_size;
370 dev_priv->ring.virtual_start = drm_ioremap(dev->agp->base +
372 init->ring_size, dev);
374 if (dev_priv->ring.virtual_start == NULL) {
375 dev->dev_private = (void *) dev_priv;
376 i810_dma_cleanup(dev);
377 DRM_ERROR("can not ioremap virtual address for"
382 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
384 dev_priv->w = init->w;
385 dev_priv->h = init->h;
386 dev_priv->pitch = init->pitch;
387 dev_priv->back_offset = init->back_offset;
388 dev_priv->depth_offset = init->depth_offset;
389 dev_priv->front_offset = init->front_offset;
391 dev_priv->overlay_offset = init->overlay_offset;
392 dev_priv->overlay_physical = init->overlay_physical;
394 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
395 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
396 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
398 /* Program Hardware Status Page */
399 dev_priv->hw_status_page =
400 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
401 &dev_priv->dma_status_page);
402 if (!dev_priv->hw_status_page) {
403 dev->dev_private = (void *)dev_priv;
404 i810_dma_cleanup(dev);
405 DRM_ERROR("Can not allocate hardware status page\n");
408 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
409 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
411 I810_WRITE(0x02080, dev_priv->dma_status_page);
412 DRM_DEBUG("Enabled hardware status page\n");
414 /* Now we need to init our freelist */
415 if (i810_freelist_init(dev, dev_priv) != 0) {
416 dev->dev_private = (void *)dev_priv;
417 i810_dma_cleanup(dev);
418 DRM_ERROR("Not enough space in the status page for"
422 dev->dev_private = (void *)dev_priv;
427 /* i810 DRM version 1.1 used a smaller init structure with different
428 * ordering of values than is currently used (drm >= 1.2). There is
429 * no defined way to detect the XFree version to correct this problem,
430 * however by checking using this procedure we can detect the correct
433 * #1 Read the Smaller init structure from user-space
434 * #2 Verify the overlay_physical is a valid physical address, or NULL
435 * If it isn't then we have a v1.1 client. Fix up params.
436 * If it is, then we have a 1.2 client... get the rest of the data.
438 static int i810_dma_init_compat(drm_i810_init_t *init, unsigned long arg)
441 /* Get v1.1 init data */
442 if (copy_from_user(init, (drm_i810_pre12_init_t __user *)arg,
443 sizeof(drm_i810_pre12_init_t))) {
447 if ((!init->overlay_physical) || (init->overlay_physical > 4096)) {
449 /* This is a v1.2 client, just get the v1.2 init data */
450 DRM_INFO("Using POST v1.2 init.\n");
451 if (copy_from_user(init, (drm_i810_init_t __user *)arg,
452 sizeof(drm_i810_init_t))) {
457 /* This is a v1.1 client, fix the params */
458 DRM_INFO("Using PRE v1.2 init.\n");
459 init->pitch_bits = init->h;
460 init->pitch = init->w;
461 init->h = init->overlay_physical;
462 init->w = init->overlay_offset;
463 init->overlay_physical = 0;
464 init->overlay_offset = 0;
470 static int i810_dma_init(struct inode *inode, struct file *filp,
471 unsigned int cmd, unsigned long arg)
473 drm_file_t *priv = filp->private_data;
474 drm_device_t *dev = priv->head->dev;
475 drm_i810_private_t *dev_priv;
476 drm_i810_init_t init;
479 /* Get only the init func */
480 if (copy_from_user(&init, (void __user *)arg, sizeof(drm_i810_init_func_t)))
485 /* This case is for backward compatibility. It
486 * handles XFree 4.1.0 and 4.2.0, and has to
487 * do some parameter checking as described below.
488 * It will someday go away.
490 retcode = i810_dma_init_compat(&init, arg);
494 dev_priv = drm_alloc(sizeof(drm_i810_private_t),
496 if (dev_priv == NULL)
498 retcode = i810_dma_initialize(dev, dev_priv, &init);
502 case I810_INIT_DMA_1_4:
503 DRM_INFO("Using v1.4 init.\n");
504 if (copy_from_user(&init, (drm_i810_init_t __user *)arg,
505 sizeof(drm_i810_init_t))) {
508 dev_priv = drm_alloc(sizeof(drm_i810_private_t),
510 if (dev_priv == NULL)
512 retcode = i810_dma_initialize(dev, dev_priv, &init);
515 case I810_CLEANUP_DMA:
516 DRM_INFO("DMA Cleanup\n");
517 retcode = i810_dma_cleanup(dev);
526 /* Most efficient way to verify state for the i810 is as it is
527 * emitted. Non-conformant state is silently dropped.
529 * Use 'volatile' & local var tmp to force the emitted values to be
530 * identical to the verified ones.
532 static void i810EmitContextVerified( drm_device_t *dev,
533 volatile unsigned int *code )
535 drm_i810_private_t *dev_priv = dev->dev_private;
540 BEGIN_LP_RING( I810_CTX_SETUP_SIZE );
542 OUT_RING( GFX_OP_COLOR_FACTOR );
543 OUT_RING( code[I810_CTXREG_CF1] );
545 OUT_RING( GFX_OP_STIPPLE );
546 OUT_RING( code[I810_CTXREG_ST1] );
548 for ( i = 4 ; i < I810_CTX_SETUP_SIZE ; i++ ) {
551 if ((tmp & (7<<29)) == (3<<29) &&
552 (tmp & (0x1f<<24)) < (0x1d<<24))
557 else printk("constext state dropped!!!\n");
566 static void i810EmitTexVerified( drm_device_t *dev,
567 volatile unsigned int *code )
569 drm_i810_private_t *dev_priv = dev->dev_private;
574 BEGIN_LP_RING( I810_TEX_SETUP_SIZE );
576 OUT_RING( GFX_OP_MAP_INFO );
577 OUT_RING( code[I810_TEXREG_MI1] );
578 OUT_RING( code[I810_TEXREG_MI2] );
579 OUT_RING( code[I810_TEXREG_MI3] );
581 for ( i = 4 ; i < I810_TEX_SETUP_SIZE ; i++ ) {
584 if ((tmp & (7<<29)) == (3<<29) &&
585 (tmp & (0x1f<<24)) < (0x1d<<24))
590 else printk("texture state dropped!!!\n");
600 /* Need to do some additional checking when setting the dest buffer.
602 static void i810EmitDestVerified( drm_device_t *dev,
603 volatile unsigned int *code )
605 drm_i810_private_t *dev_priv = dev->dev_private;
609 BEGIN_LP_RING( I810_DEST_SETUP_SIZE + 2 );
611 tmp = code[I810_DESTREG_DI1];
612 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
613 OUT_RING( CMD_OP_DESTBUFFER_INFO );
616 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
617 tmp, dev_priv->front_di1, dev_priv->back_di1);
621 OUT_RING( CMD_OP_Z_BUFFER_INFO );
622 OUT_RING( dev_priv->zi1 );
624 OUT_RING( GFX_OP_DESTBUFFER_VARS );
625 OUT_RING( code[I810_DESTREG_DV1] );
627 OUT_RING( GFX_OP_DRAWRECT_INFO );
628 OUT_RING( code[I810_DESTREG_DR1] );
629 OUT_RING( code[I810_DESTREG_DR2] );
630 OUT_RING( code[I810_DESTREG_DR3] );
631 OUT_RING( code[I810_DESTREG_DR4] );
639 static void i810EmitState( drm_device_t *dev )
641 drm_i810_private_t *dev_priv = dev->dev_private;
642 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
643 unsigned int dirty = sarea_priv->dirty;
645 DRM_DEBUG("%s %x\n", __FUNCTION__, dirty);
647 if (dirty & I810_UPLOAD_BUFFERS) {
648 i810EmitDestVerified( dev, sarea_priv->BufferState );
649 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
652 if (dirty & I810_UPLOAD_CTX) {
653 i810EmitContextVerified( dev, sarea_priv->ContextState );
654 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
657 if (dirty & I810_UPLOAD_TEX0) {
658 i810EmitTexVerified( dev, sarea_priv->TexState[0] );
659 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
662 if (dirty & I810_UPLOAD_TEX1) {
663 i810EmitTexVerified( dev, sarea_priv->TexState[1] );
664 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
672 static void i810_dma_dispatch_clear( drm_device_t *dev, int flags,
673 unsigned int clear_color,
674 unsigned int clear_zval )
676 drm_i810_private_t *dev_priv = dev->dev_private;
677 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
678 int nbox = sarea_priv->nbox;
679 drm_clip_rect_t *pbox = sarea_priv->boxes;
680 int pitch = dev_priv->pitch;
685 if ( dev_priv->current_page == 1 ) {
686 unsigned int tmp = flags;
688 flags &= ~(I810_FRONT | I810_BACK);
689 if (tmp & I810_FRONT) flags |= I810_BACK;
690 if (tmp & I810_BACK) flags |= I810_FRONT;
693 i810_kernel_lost_context(dev);
695 if (nbox > I810_NR_SAREA_CLIPRECTS)
696 nbox = I810_NR_SAREA_CLIPRECTS;
698 for (i = 0 ; i < nbox ; i++, pbox++) {
699 unsigned int x = pbox->x1;
700 unsigned int y = pbox->y1;
701 unsigned int width = (pbox->x2 - x) * cpp;
702 unsigned int height = pbox->y2 - y;
703 unsigned int start = y * pitch + x * cpp;
705 if (pbox->x1 > pbox->x2 ||
706 pbox->y1 > pbox->y2 ||
707 pbox->x2 > dev_priv->w ||
708 pbox->y2 > dev_priv->h)
711 if ( flags & I810_FRONT ) {
713 OUT_RING( BR00_BITBLT_CLIENT |
714 BR00_OP_COLOR_BLT | 0x3 );
715 OUT_RING( BR13_SOLID_PATTERN | (0xF0 << 16) | pitch );
716 OUT_RING( (height << 16) | width );
718 OUT_RING( clear_color );
723 if ( flags & I810_BACK ) {
725 OUT_RING( BR00_BITBLT_CLIENT |
726 BR00_OP_COLOR_BLT | 0x3 );
727 OUT_RING( BR13_SOLID_PATTERN | (0xF0 << 16) | pitch );
728 OUT_RING( (height << 16) | width );
729 OUT_RING( dev_priv->back_offset + start );
730 OUT_RING( clear_color );
735 if ( flags & I810_DEPTH ) {
737 OUT_RING( BR00_BITBLT_CLIENT |
738 BR00_OP_COLOR_BLT | 0x3 );
739 OUT_RING( BR13_SOLID_PATTERN | (0xF0 << 16) | pitch );
740 OUT_RING( (height << 16) | width );
741 OUT_RING( dev_priv->depth_offset + start );
742 OUT_RING( clear_zval );
749 static void i810_dma_dispatch_swap( drm_device_t *dev )
751 drm_i810_private_t *dev_priv = dev->dev_private;
752 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
753 int nbox = sarea_priv->nbox;
754 drm_clip_rect_t *pbox = sarea_priv->boxes;
755 int pitch = dev_priv->pitch;
760 DRM_DEBUG("swapbuffers\n");
762 i810_kernel_lost_context(dev);
764 if (nbox > I810_NR_SAREA_CLIPRECTS)
765 nbox = I810_NR_SAREA_CLIPRECTS;
767 for (i = 0 ; i < nbox; i++, pbox++)
769 unsigned int w = pbox->x2 - pbox->x1;
770 unsigned int h = pbox->y2 - pbox->y1;
771 unsigned int dst = pbox->x1*cpp + pbox->y1*pitch;
772 unsigned int start = dst;
774 if (pbox->x1 > pbox->x2 ||
775 pbox->y1 > pbox->y2 ||
776 pbox->x2 > dev_priv->w ||
777 pbox->y2 > dev_priv->h)
781 OUT_RING( BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4 );
782 OUT_RING( pitch | (0xCC << 16));
783 OUT_RING( (h << 16) | (w * cpp));
784 if (dev_priv->current_page == 0)
785 OUT_RING(dev_priv->front_offset + start);
787 OUT_RING(dev_priv->back_offset + start);
789 if (dev_priv->current_page == 0)
790 OUT_RING(dev_priv->back_offset + start);
792 OUT_RING(dev_priv->front_offset + start);
798 static void i810_dma_dispatch_vertex(drm_device_t *dev,
803 drm_i810_private_t *dev_priv = dev->dev_private;
804 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
805 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
806 drm_clip_rect_t *box = sarea_priv->boxes;
807 int nbox = sarea_priv->nbox;
808 unsigned long address = (unsigned long)buf->bus_address;
809 unsigned long start = address - dev->agp->base;
813 i810_kernel_lost_context(dev);
815 if (nbox > I810_NR_SAREA_CLIPRECTS)
816 nbox = I810_NR_SAREA_CLIPRECTS;
821 if (sarea_priv->dirty)
822 i810EmitState( dev );
824 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
825 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
827 *(u32 *)buf_priv->kernel_virtual = ((GFX_OP_PRIMITIVE | prim | ((used/4)-2)));
830 *(u32 *)((u32)buf_priv->kernel_virtual + used) = 0;
834 i810_unmap_buffer(buf);
841 OUT_RING( GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
843 OUT_RING( GFX_OP_SCISSOR_INFO );
844 OUT_RING( box[i].x1 | (box[i].y1<<16) );
845 OUT_RING( (box[i].x2-1) | ((box[i].y2-1)<<16) );
850 OUT_RING( CMD_OP_BATCH_BUFFER );
851 OUT_RING( start | BB1_PROTECTED );
852 OUT_RING( start + used - 4 );
856 } while (++i < nbox);
862 (void) cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
866 OUT_RING( CMD_STORE_DWORD_IDX );
868 OUT_RING( dev_priv->counter );
869 OUT_RING( CMD_STORE_DWORD_IDX );
870 OUT_RING( buf_priv->my_use_idx );
871 OUT_RING( I810_BUF_FREE );
872 OUT_RING( CMD_REPORT_HEAD );
878 static void i810_dma_dispatch_flip( drm_device_t *dev )
880 drm_i810_private_t *dev_priv = dev->dev_private;
881 int pitch = dev_priv->pitch;
884 DRM_DEBUG( "%s: page=%d pfCurrentPage=%d\n",
886 dev_priv->current_page,
887 dev_priv->sarea_priv->pf_current_page);
889 i810_kernel_lost_context(dev);
892 OUT_RING( INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE );
896 BEGIN_LP_RING( I810_DEST_SETUP_SIZE + 2 );
897 /* On i815 at least ASYNC is buggy */
898 /* pitch<<5 is from 11.2.8 p158,
899 its the pitch / 8 then left shifted 8,
900 so (pitch >> 3) << 8 */
901 OUT_RING( CMD_OP_FRONTBUFFER_INFO | (pitch<<5) /*| ASYNC_FLIP */ );
902 if ( dev_priv->current_page == 0 ) {
903 OUT_RING( dev_priv->back_offset );
904 dev_priv->current_page = 1;
906 OUT_RING( dev_priv->front_offset );
907 dev_priv->current_page = 0;
913 OUT_RING( CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP );
917 /* Increment the frame counter. The client-side 3D driver must
918 * throttle the framerate by waiting for this value before
919 * performing the swapbuffer ioctl.
921 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
925 static void i810_dma_quiescent(drm_device_t *dev)
927 drm_i810_private_t *dev_priv = dev->dev_private;
930 /* printk("%s\n", __FUNCTION__); */
932 i810_kernel_lost_context(dev);
935 OUT_RING( INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE );
936 OUT_RING( CMD_REPORT_HEAD );
941 i810_wait_ring( dev, dev_priv->ring.Size - 8 );
944 static int i810_flush_queue(drm_device_t *dev)
946 drm_i810_private_t *dev_priv = dev->dev_private;
947 drm_device_dma_t *dma = dev->dma;
951 /* printk("%s\n", __FUNCTION__); */
953 i810_kernel_lost_context(dev);
956 OUT_RING( CMD_REPORT_HEAD );
960 i810_wait_ring( dev, dev_priv->ring.Size - 8 );
962 for (i = 0; i < dma->buf_count; i++) {
963 drm_buf_t *buf = dma->buflist[ i ];
964 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
966 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
969 if (used == I810_BUF_HARDWARE)
970 DRM_DEBUG("reclaimed from HARDWARE\n");
971 if (used == I810_BUF_CLIENT)
972 DRM_DEBUG("still on client\n");
978 /* Must be called with the lock held */
979 void i810_reclaim_buffers(drm_device_t *dev, struct file *filp)
981 drm_device_dma_t *dma = dev->dma;
985 if (!dev->dev_private) return;
986 if (!dma->buflist) return;
988 i810_flush_queue(dev);
990 for (i = 0; i < dma->buf_count; i++) {
991 drm_buf_t *buf = dma->buflist[ i ];
992 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
994 if (buf->filp == filp && buf_priv) {
995 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
998 if (used == I810_BUF_CLIENT)
999 DRM_DEBUG("reclaimed from client\n");
1000 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
1001 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
1006 static int i810_flush_ioctl(struct inode *inode, struct file *filp,
1007 unsigned int cmd, unsigned long arg)
1009 drm_file_t *priv = filp->private_data;
1010 drm_device_t *dev = priv->head->dev;
1012 LOCK_TEST_WITH_RETURN(dev, filp);
1014 i810_flush_queue(dev);
1019 static int i810_dma_vertex(struct inode *inode, struct file *filp,
1020 unsigned int cmd, unsigned long arg)
1022 drm_file_t *priv = filp->private_data;
1023 drm_device_t *dev = priv->head->dev;
1024 drm_device_dma_t *dma = dev->dma;
1025 drm_i810_private_t *dev_priv = (drm_i810_private_t *)dev->dev_private;
1026 u32 *hw_status = dev_priv->hw_status_page;
1027 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1028 dev_priv->sarea_priv;
1029 drm_i810_vertex_t vertex;
1031 if (copy_from_user(&vertex, (drm_i810_vertex_t __user *)arg, sizeof(vertex)))
1034 LOCK_TEST_WITH_RETURN(dev, filp);
1036 DRM_DEBUG("i810 dma vertex, idx %d used %d discard %d\n",
1037 vertex.idx, vertex.used, vertex.discard);
1039 if (vertex.idx < 0 || vertex.idx > dma->buf_count)
1042 i810_dma_dispatch_vertex( dev,
1043 dma->buflist[ vertex.idx ],
1044 vertex.discard, vertex.used );
1046 atomic_add(vertex.used, &dev->counts[_DRM_STAT_SECONDARY]);
1047 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
1048 sarea_priv->last_enqueue = dev_priv->counter-1;
1049 sarea_priv->last_dispatch = (int) hw_status[5];
1056 static int i810_clear_bufs(struct inode *inode, struct file *filp,
1057 unsigned int cmd, unsigned long arg)
1059 drm_file_t *priv = filp->private_data;
1060 drm_device_t *dev = priv->head->dev;
1061 drm_i810_clear_t clear;
1063 if (copy_from_user(&clear, (drm_i810_clear_t __user *)arg, sizeof(clear)))
1066 LOCK_TEST_WITH_RETURN(dev, filp);
1068 /* GH: Someone's doing nasty things... */
1069 if (!dev->dev_private) {
1073 i810_dma_dispatch_clear( dev, clear.flags,
1075 clear.clear_depth );
1079 static int i810_swap_bufs(struct inode *inode, struct file *filp,
1080 unsigned int cmd, unsigned long arg)
1082 drm_file_t *priv = filp->private_data;
1083 drm_device_t *dev = priv->head->dev;
1085 DRM_DEBUG("i810_swap_bufs\n");
1087 LOCK_TEST_WITH_RETURN(dev, filp);
1089 i810_dma_dispatch_swap( dev );
1093 static int i810_getage(struct inode *inode, struct file *filp, unsigned int cmd,
1096 drm_file_t *priv = filp->private_data;
1097 drm_device_t *dev = priv->head->dev;
1098 drm_i810_private_t *dev_priv = (drm_i810_private_t *)dev->dev_private;
1099 u32 *hw_status = dev_priv->hw_status_page;
1100 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1101 dev_priv->sarea_priv;
1103 sarea_priv->last_dispatch = (int) hw_status[5];
1107 static int i810_getbuf(struct inode *inode, struct file *filp, unsigned int cmd,
1110 drm_file_t *priv = filp->private_data;
1111 drm_device_t *dev = priv->head->dev;
1114 drm_i810_private_t *dev_priv = (drm_i810_private_t *)dev->dev_private;
1115 u32 *hw_status = dev_priv->hw_status_page;
1116 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1117 dev_priv->sarea_priv;
1119 if (copy_from_user(&d, (drm_i810_dma_t __user *)arg, sizeof(d)))
1122 LOCK_TEST_WITH_RETURN(dev, filp);
1126 retcode = i810_dma_get_buffer(dev, &d, filp);
1128 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
1129 current->pid, retcode, d.granted);
1131 if (copy_to_user((drm_dma_t __user *)arg, &d, sizeof(d)))
1133 sarea_priv->last_dispatch = (int) hw_status[5];
1138 static int i810_copybuf(struct inode *inode,
1139 struct file *filp, unsigned int cmd, unsigned long arg)
1141 /* Never copy - 2.4.x doesn't need it */
1145 static int i810_docopy(struct inode *inode, struct file *filp, unsigned int cmd,
1148 /* Never copy - 2.4.x doesn't need it */
1152 static void i810_dma_dispatch_mc(drm_device_t *dev, drm_buf_t *buf, int used,
1153 unsigned int last_render)
1155 drm_i810_private_t *dev_priv = dev->dev_private;
1156 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1157 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1158 unsigned long address = (unsigned long)buf->bus_address;
1159 unsigned long start = address - dev->agp->base;
1163 i810_kernel_lost_context(dev);
1165 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
1167 if (u != I810_BUF_CLIENT) {
1168 DRM_DEBUG("MC found buffer that isn't mine!\n");
1174 sarea_priv->dirty = 0x7f;
1176 DRM_DEBUG("dispatch mc addr 0x%lx, used 0x%x\n",
1179 dev_priv->counter++;
1180 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1181 DRM_DEBUG("i810_dma_dispatch_mc\n");
1182 DRM_DEBUG("start : %lx\n", start);
1183 DRM_DEBUG("used : %d\n", used);
1184 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1186 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1188 *(u32 *)((u32)buf_priv->virtual + used) = 0;
1192 i810_unmap_buffer(buf);
1195 OUT_RING( CMD_OP_BATCH_BUFFER );
1196 OUT_RING( start | BB1_PROTECTED );
1197 OUT_RING( start + used - 4 );
1203 OUT_RING( CMD_STORE_DWORD_IDX );
1204 OUT_RING( buf_priv->my_use_idx );
1205 OUT_RING( I810_BUF_FREE );
1208 OUT_RING( CMD_STORE_DWORD_IDX );
1210 OUT_RING( last_render );
1215 static int i810_dma_mc(struct inode *inode, struct file *filp,
1216 unsigned int cmd, unsigned long arg)
1218 drm_file_t *priv = filp->private_data;
1219 drm_device_t *dev = priv->head->dev;
1220 drm_device_dma_t *dma = dev->dma;
1221 drm_i810_private_t *dev_priv = (drm_i810_private_t *)dev->dev_private;
1222 u32 *hw_status = dev_priv->hw_status_page;
1223 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1224 dev_priv->sarea_priv;
1227 if (copy_from_user(&mc, (drm_i810_mc_t __user *)arg, sizeof(mc)))
1230 LOCK_TEST_WITH_RETURN(dev, filp);
1232 if (mc.idx >= dma->buf_count || mc.idx < 0)
1235 i810_dma_dispatch_mc(dev, dma->buflist[mc.idx], mc.used,
1238 atomic_add(mc.used, &dev->counts[_DRM_STAT_SECONDARY]);
1239 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
1240 sarea_priv->last_enqueue = dev_priv->counter-1;
1241 sarea_priv->last_dispatch = (int) hw_status[5];
1246 static int i810_rstatus(struct inode *inode, struct file *filp,
1247 unsigned int cmd, unsigned long arg)
1249 drm_file_t *priv = filp->private_data;
1250 drm_device_t *dev = priv->head->dev;
1251 drm_i810_private_t *dev_priv = (drm_i810_private_t *)dev->dev_private;
1253 return (int)(((u32 *)(dev_priv->hw_status_page))[4]);
1256 static int i810_ov0_info(struct inode *inode, struct file *filp,
1257 unsigned int cmd, unsigned long arg)
1259 drm_file_t *priv = filp->private_data;
1260 drm_device_t *dev = priv->head->dev;
1261 drm_i810_private_t *dev_priv = (drm_i810_private_t *)dev->dev_private;
1262 drm_i810_overlay_t data;
1264 data.offset = dev_priv->overlay_offset;
1265 data.physical = dev_priv->overlay_physical;
1266 if (copy_to_user((drm_i810_overlay_t __user *)arg,&data,sizeof(data)))
1271 static int i810_fstatus(struct inode *inode, struct file *filp,
1272 unsigned int cmd, unsigned long arg)
1274 drm_file_t *priv = filp->private_data;
1275 drm_device_t *dev = priv->head->dev;
1276 drm_i810_private_t *dev_priv = (drm_i810_private_t *)dev->dev_private;
1278 LOCK_TEST_WITH_RETURN(dev, filp);
1280 return I810_READ(0x30008);
1283 static int i810_ov0_flip(struct inode *inode, struct file *filp,
1284 unsigned int cmd, unsigned long arg)
1286 drm_file_t *priv = filp->private_data;
1287 drm_device_t *dev = priv->head->dev;
1288 drm_i810_private_t *dev_priv = (drm_i810_private_t *)dev->dev_private;
1290 LOCK_TEST_WITH_RETURN(dev, filp);
1292 //Tell the overlay to update
1293 I810_WRITE(0x30000,dev_priv->overlay_physical | 0x80000000);
1299 /* Not sure why this isn't set all the time:
1301 static void i810_do_init_pageflip( drm_device_t *dev )
1303 drm_i810_private_t *dev_priv = dev->dev_private;
1305 DRM_DEBUG("%s\n", __FUNCTION__);
1306 dev_priv->page_flipping = 1;
1307 dev_priv->current_page = 0;
1308 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1311 static int i810_do_cleanup_pageflip( drm_device_t *dev )
1313 drm_i810_private_t *dev_priv = dev->dev_private;
1315 DRM_DEBUG("%s\n", __FUNCTION__);
1316 if (dev_priv->current_page != 0)
1317 i810_dma_dispatch_flip( dev );
1319 dev_priv->page_flipping = 0;
1323 static int i810_flip_bufs(struct inode *inode, struct file *filp,
1324 unsigned int cmd, unsigned long arg)
1326 drm_file_t *priv = filp->private_data;
1327 drm_device_t *dev = priv->head->dev;
1328 drm_i810_private_t *dev_priv = dev->dev_private;
1330 DRM_DEBUG("%s\n", __FUNCTION__);
1332 LOCK_TEST_WITH_RETURN(dev, filp);
1334 if (!dev_priv->page_flipping)
1335 i810_do_init_pageflip( dev );
1337 i810_dma_dispatch_flip( dev );
1341 void i810_driver_pretakedown(drm_device_t *dev)
1343 i810_dma_cleanup( dev );
1346 void i810_driver_prerelease(drm_device_t *dev, DRMFILE filp)
1348 if (dev->dev_private) {
1349 drm_i810_private_t *dev_priv = dev->dev_private;
1350 if (dev_priv->page_flipping) {
1351 i810_do_cleanup_pageflip(dev);
1356 void i810_driver_release(drm_device_t *dev, struct file *filp)
1358 i810_reclaim_buffers(dev, filp);
1361 int i810_driver_dma_quiescent(drm_device_t *dev)
1363 i810_dma_quiescent( dev );
1367 drm_ioctl_desc_t i810_ioctls[] = {
1368 [DRM_IOCTL_NR(DRM_I810_INIT)] = { i810_dma_init, 1, 1 },
1369 [DRM_IOCTL_NR(DRM_I810_VERTEX)] = { i810_dma_vertex, 1, 0 },
1370 [DRM_IOCTL_NR(DRM_I810_CLEAR)] = { i810_clear_bufs, 1, 0 },
1371 [DRM_IOCTL_NR(DRM_I810_FLUSH)] = { i810_flush_ioctl, 1, 0 },
1372 [DRM_IOCTL_NR(DRM_I810_GETAGE)] = { i810_getage, 1, 0 },
1373 [DRM_IOCTL_NR(DRM_I810_GETBUF)] = { i810_getbuf, 1, 0 },
1374 [DRM_IOCTL_NR(DRM_I810_SWAP)] = { i810_swap_bufs, 1, 0 },
1375 [DRM_IOCTL_NR(DRM_I810_COPY)] = { i810_copybuf, 1, 0 },
1376 [DRM_IOCTL_NR(DRM_I810_DOCOPY)] = { i810_docopy, 1, 0 },
1377 [DRM_IOCTL_NR(DRM_I810_OV0INFO)] = { i810_ov0_info, 1, 0 },
1378 [DRM_IOCTL_NR(DRM_I810_FSTATUS)] = { i810_fstatus, 1, 0 },
1379 [DRM_IOCTL_NR(DRM_I810_OV0FLIP)] = { i810_ov0_flip, 1, 0 },
1380 [DRM_IOCTL_NR(DRM_I810_MC)] = { i810_dma_mc, 1, 1 },
1381 [DRM_IOCTL_NR(DRM_I810_RSTATUS)] = { i810_rstatus, 1, 0 },
1382 [DRM_IOCTL_NR(DRM_I810_FLIP)] = { i810_flip_bufs, 1, 0 }
1385 int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);