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>
34 #include <drm/i810_drm.h>
36 #include <linux/interrupt.h> /* For task queue support */
37 #include <linux/delay.h>
38 #include <linux/slab.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 static struct drm_buf
*i810_freelist_get(struct drm_device
* dev
)
50 struct drm_device_dma
*dma
= dev
->dma
;
54 /* Linear search might not be the best solution */
56 for (i
= 0; i
< dma
->buf_count
; i
++) {
57 struct drm_buf
*buf
= dma
->buflist
[i
];
58 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
59 /* In use is already a pointer */
60 used
= cmpxchg(buf_priv
->in_use
, I810_BUF_FREE
,
62 if (used
== I810_BUF_FREE
)
68 /* This should only be called if the buffer is not sent to the hardware
69 * yet, the hardware updates in use for us once its on the ring buffer.
72 static int i810_freelist_put(struct drm_device
*dev
, struct drm_buf
*buf
)
74 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
77 /* In use is already a pointer */
78 used
= cmpxchg(buf_priv
->in_use
, I810_BUF_CLIENT
, I810_BUF_FREE
);
79 if (used
!= I810_BUF_CLIENT
) {
80 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf
->idx
);
87 static int i810_mmap_buffers(struct file
*filp
, struct vm_area_struct
*vma
)
89 struct drm_file
*priv
= filp
->private_data
;
90 struct drm_device
*dev
;
91 drm_i810_private_t
*dev_priv
;
93 drm_i810_buf_priv_t
*buf_priv
;
95 dev
= priv
->minor
->dev
;
96 dev_priv
= dev
->dev_private
;
97 buf
= dev_priv
->mmap_buffer
;
98 buf_priv
= buf
->dev_private
;
100 vma
->vm_flags
|= VM_DONTCOPY
;
102 buf_priv
->currently_mapped
= I810_BUF_MAPPED
;
104 if (io_remap_pfn_range(vma
, vma
->vm_start
,
106 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
))
111 static const struct file_operations i810_buffer_fops
= {
113 .release
= drm_release
,
114 .unlocked_ioctl
= drm_ioctl
,
115 .mmap
= i810_mmap_buffers
,
117 .compat_ioctl
= drm_compat_ioctl
,
119 .llseek
= noop_llseek
,
122 static int i810_map_buffer(struct drm_buf
*buf
, struct drm_file
*file_priv
)
124 struct drm_device
*dev
= file_priv
->minor
->dev
;
125 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
126 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
127 const struct file_operations
*old_fops
;
130 if (buf_priv
->currently_mapped
== I810_BUF_MAPPED
)
133 /* This is all entirely broken */
134 old_fops
= file_priv
->filp
->f_op
;
135 file_priv
->filp
->f_op
= &i810_buffer_fops
;
136 dev_priv
->mmap_buffer
= buf
;
137 buf_priv
->virtual = (void *)vm_mmap(file_priv
->filp
, 0, buf
->total
,
138 PROT_READ
| PROT_WRITE
,
139 MAP_SHARED
, buf
->bus_address
);
140 dev_priv
->mmap_buffer
= NULL
;
141 file_priv
->filp
->f_op
= old_fops
;
142 if (IS_ERR(buf_priv
->virtual)) {
144 DRM_ERROR("mmap error\n");
145 retcode
= PTR_ERR(buf_priv
->virtual);
146 buf_priv
->virtual = NULL
;
152 static int i810_unmap_buffer(struct drm_buf
*buf
)
154 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
157 if (buf_priv
->currently_mapped
!= I810_BUF_MAPPED
)
160 retcode
= vm_munmap((unsigned long)buf_priv
->virtual,
161 (size_t) buf
->total
);
163 buf_priv
->currently_mapped
= I810_BUF_UNMAPPED
;
164 buf_priv
->virtual = NULL
;
169 static int i810_dma_get_buffer(struct drm_device
*dev
, drm_i810_dma_t
*d
,
170 struct drm_file
*file_priv
)
173 drm_i810_buf_priv_t
*buf_priv
;
176 buf
= i810_freelist_get(dev
);
179 DRM_DEBUG("retcode=%d\n", retcode
);
183 retcode
= i810_map_buffer(buf
, file_priv
);
185 i810_freelist_put(dev
, buf
);
186 DRM_ERROR("mapbuf failed, retcode %d\n", retcode
);
189 buf
->file_priv
= file_priv
;
190 buf_priv
= buf
->dev_private
;
192 d
->request_idx
= buf
->idx
;
193 d
->request_size
= buf
->total
;
194 d
->virtual = buf_priv
->virtual;
199 static int i810_dma_cleanup(struct drm_device
*dev
)
201 struct drm_device_dma
*dma
= dev
->dma
;
203 /* Make sure interrupts are disabled here because the uninstall ioctl
204 * may not have been called from userspace and after dev_private
205 * is freed, it's too late.
207 if (drm_core_check_feature(dev
, DRIVER_HAVE_IRQ
) && dev
->irq_enabled
)
208 drm_irq_uninstall(dev
);
210 if (dev
->dev_private
) {
212 drm_i810_private_t
*dev_priv
=
213 (drm_i810_private_t
*) dev
->dev_private
;
215 if (dev_priv
->ring
.virtual_start
)
216 drm_core_ioremapfree(&dev_priv
->ring
.map
, dev
);
217 if (dev_priv
->hw_status_page
) {
218 pci_free_consistent(dev
->pdev
, PAGE_SIZE
,
219 dev_priv
->hw_status_page
,
220 dev_priv
->dma_status_page
);
222 kfree(dev
->dev_private
);
223 dev
->dev_private
= NULL
;
225 for (i
= 0; i
< dma
->buf_count
; i
++) {
226 struct drm_buf
*buf
= dma
->buflist
[i
];
227 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
229 if (buf_priv
->kernel_virtual
&& buf
->total
)
230 drm_core_ioremapfree(&buf_priv
->map
, dev
);
236 static int i810_wait_ring(struct drm_device
*dev
, int n
)
238 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
239 drm_i810_ring_buffer_t
*ring
= &(dev_priv
->ring
);
242 unsigned int last_head
= I810_READ(LP_RING
+ RING_HEAD
) & HEAD_ADDR
;
244 end
= jiffies
+ (HZ
* 3);
245 while (ring
->space
< n
) {
246 ring
->head
= I810_READ(LP_RING
+ RING_HEAD
) & HEAD_ADDR
;
247 ring
->space
= ring
->head
- (ring
->tail
+ 8);
249 ring
->space
+= ring
->Size
;
251 if (ring
->head
!= last_head
) {
252 end
= jiffies
+ (HZ
* 3);
253 last_head
= ring
->head
;
257 if (time_before(end
, jiffies
)) {
258 DRM_ERROR("space: %d wanted %d\n", ring
->space
, n
);
259 DRM_ERROR("lockup\n");
269 static void i810_kernel_lost_context(struct drm_device
*dev
)
271 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
272 drm_i810_ring_buffer_t
*ring
= &(dev_priv
->ring
);
274 ring
->head
= I810_READ(LP_RING
+ RING_HEAD
) & HEAD_ADDR
;
275 ring
->tail
= I810_READ(LP_RING
+ RING_TAIL
);
276 ring
->space
= ring
->head
- (ring
->tail
+ 8);
278 ring
->space
+= ring
->Size
;
281 static int i810_freelist_init(struct drm_device
*dev
, drm_i810_private_t
*dev_priv
)
283 struct drm_device_dma
*dma
= dev
->dma
;
285 u32
*hw_status
= (u32
*) (dev_priv
->hw_status_page
+ my_idx
);
288 if (dma
->buf_count
> 1019) {
289 /* Not enough space in the status page for the freelist */
293 for (i
= 0; i
< dma
->buf_count
; i
++) {
294 struct drm_buf
*buf
= dma
->buflist
[i
];
295 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
297 buf_priv
->in_use
= hw_status
++;
298 buf_priv
->my_use_idx
= my_idx
;
301 *buf_priv
->in_use
= I810_BUF_FREE
;
303 buf_priv
->map
.offset
= buf
->bus_address
;
304 buf_priv
->map
.size
= buf
->total
;
305 buf_priv
->map
.type
= _DRM_AGP
;
306 buf_priv
->map
.flags
= 0;
307 buf_priv
->map
.mtrr
= 0;
309 drm_core_ioremap(&buf_priv
->map
, dev
);
310 buf_priv
->kernel_virtual
= buf_priv
->map
.handle
;
316 static int i810_dma_initialize(struct drm_device
*dev
,
317 drm_i810_private_t
*dev_priv
,
318 drm_i810_init_t
*init
)
320 struct drm_map_list
*r_list
;
321 memset(dev_priv
, 0, sizeof(drm_i810_private_t
));
323 list_for_each_entry(r_list
, &dev
->maplist
, head
) {
325 r_list
->map
->type
== _DRM_SHM
&&
326 r_list
->map
->flags
& _DRM_CONTAINS_LOCK
) {
327 dev_priv
->sarea_map
= r_list
->map
;
331 if (!dev_priv
->sarea_map
) {
332 dev
->dev_private
= (void *)dev_priv
;
333 i810_dma_cleanup(dev
);
334 DRM_ERROR("can not find sarea!\n");
337 dev_priv
->mmio_map
= drm_core_findmap(dev
, init
->mmio_offset
);
338 if (!dev_priv
->mmio_map
) {
339 dev
->dev_private
= (void *)dev_priv
;
340 i810_dma_cleanup(dev
);
341 DRM_ERROR("can not find mmio map!\n");
344 dev
->agp_buffer_token
= init
->buffers_offset
;
345 dev
->agp_buffer_map
= drm_core_findmap(dev
, init
->buffers_offset
);
346 if (!dev
->agp_buffer_map
) {
347 dev
->dev_private
= (void *)dev_priv
;
348 i810_dma_cleanup(dev
);
349 DRM_ERROR("can not find dma buffer map!\n");
353 dev_priv
->sarea_priv
= (drm_i810_sarea_t
*)
354 ((u8
*) dev_priv
->sarea_map
->handle
+ init
->sarea_priv_offset
);
356 dev_priv
->ring
.Start
= init
->ring_start
;
357 dev_priv
->ring
.End
= init
->ring_end
;
358 dev_priv
->ring
.Size
= init
->ring_size
;
360 dev_priv
->ring
.map
.offset
= dev
->agp
->base
+ init
->ring_start
;
361 dev_priv
->ring
.map
.size
= init
->ring_size
;
362 dev_priv
->ring
.map
.type
= _DRM_AGP
;
363 dev_priv
->ring
.map
.flags
= 0;
364 dev_priv
->ring
.map
.mtrr
= 0;
366 drm_core_ioremap(&dev_priv
->ring
.map
, dev
);
368 if (dev_priv
->ring
.map
.handle
== NULL
) {
369 dev
->dev_private
= (void *)dev_priv
;
370 i810_dma_cleanup(dev
);
371 DRM_ERROR("can not ioremap virtual address for"
376 dev_priv
->ring
.virtual_start
= dev_priv
->ring
.map
.handle
;
378 dev_priv
->ring
.tail_mask
= dev_priv
->ring
.Size
- 1;
380 dev_priv
->w
= init
->w
;
381 dev_priv
->h
= init
->h
;
382 dev_priv
->pitch
= init
->pitch
;
383 dev_priv
->back_offset
= init
->back_offset
;
384 dev_priv
->depth_offset
= init
->depth_offset
;
385 dev_priv
->front_offset
= init
->front_offset
;
387 dev_priv
->overlay_offset
= init
->overlay_offset
;
388 dev_priv
->overlay_physical
= init
->overlay_physical
;
390 dev_priv
->front_di1
= init
->front_offset
| init
->pitch_bits
;
391 dev_priv
->back_di1
= init
->back_offset
| init
->pitch_bits
;
392 dev_priv
->zi1
= init
->depth_offset
| init
->pitch_bits
;
394 /* Program Hardware Status Page */
395 dev_priv
->hw_status_page
=
396 pci_alloc_consistent(dev
->pdev
, PAGE_SIZE
,
397 &dev_priv
->dma_status_page
);
398 if (!dev_priv
->hw_status_page
) {
399 dev
->dev_private
= (void *)dev_priv
;
400 i810_dma_cleanup(dev
);
401 DRM_ERROR("Can not allocate hardware status page\n");
404 memset(dev_priv
->hw_status_page
, 0, PAGE_SIZE
);
405 DRM_DEBUG("hw status page @ %p\n", dev_priv
->hw_status_page
);
407 I810_WRITE(0x02080, dev_priv
->dma_status_page
);
408 DRM_DEBUG("Enabled hardware status page\n");
410 /* Now we need to init our freelist */
411 if (i810_freelist_init(dev
, dev_priv
) != 0) {
412 dev
->dev_private
= (void *)dev_priv
;
413 i810_dma_cleanup(dev
);
414 DRM_ERROR("Not enough space in the status page for"
418 dev
->dev_private
= (void *)dev_priv
;
423 static int i810_dma_init(struct drm_device
*dev
, void *data
,
424 struct drm_file
*file_priv
)
426 drm_i810_private_t
*dev_priv
;
427 drm_i810_init_t
*init
= data
;
430 switch (init
->func
) {
431 case I810_INIT_DMA_1_4
:
432 DRM_INFO("Using v1.4 init.\n");
433 dev_priv
= kmalloc(sizeof(drm_i810_private_t
), GFP_KERNEL
);
434 if (dev_priv
== NULL
)
436 retcode
= i810_dma_initialize(dev
, dev_priv
, init
);
439 case I810_CLEANUP_DMA
:
440 DRM_INFO("DMA Cleanup\n");
441 retcode
= i810_dma_cleanup(dev
);
450 /* Most efficient way to verify state for the i810 is as it is
451 * emitted. Non-conformant state is silently dropped.
453 * Use 'volatile' & local var tmp to force the emitted values to be
454 * identical to the verified ones.
456 static void i810EmitContextVerified(struct drm_device
*dev
,
457 volatile unsigned int *code
)
459 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
464 BEGIN_LP_RING(I810_CTX_SETUP_SIZE
);
466 OUT_RING(GFX_OP_COLOR_FACTOR
);
467 OUT_RING(code
[I810_CTXREG_CF1
]);
469 OUT_RING(GFX_OP_STIPPLE
);
470 OUT_RING(code
[I810_CTXREG_ST1
]);
472 for (i
= 4; i
< I810_CTX_SETUP_SIZE
; i
++) {
475 if ((tmp
& (7 << 29)) == (3 << 29) &&
476 (tmp
& (0x1f << 24)) < (0x1d << 24)) {
480 printk("constext state dropped!!!\n");
489 static void i810EmitTexVerified(struct drm_device
*dev
, volatile unsigned int *code
)
491 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
496 BEGIN_LP_RING(I810_TEX_SETUP_SIZE
);
498 OUT_RING(GFX_OP_MAP_INFO
);
499 OUT_RING(code
[I810_TEXREG_MI1
]);
500 OUT_RING(code
[I810_TEXREG_MI2
]);
501 OUT_RING(code
[I810_TEXREG_MI3
]);
503 for (i
= 4; i
< I810_TEX_SETUP_SIZE
; i
++) {
506 if ((tmp
& (7 << 29)) == (3 << 29) &&
507 (tmp
& (0x1f << 24)) < (0x1d << 24)) {
511 printk("texture state dropped!!!\n");
520 /* Need to do some additional checking when setting the dest buffer.
522 static void i810EmitDestVerified(struct drm_device
*dev
,
523 volatile unsigned int *code
)
525 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
529 BEGIN_LP_RING(I810_DEST_SETUP_SIZE
+ 2);
531 tmp
= code
[I810_DESTREG_DI1
];
532 if (tmp
== dev_priv
->front_di1
|| tmp
== dev_priv
->back_di1
) {
533 OUT_RING(CMD_OP_DESTBUFFER_INFO
);
536 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
537 tmp
, dev_priv
->front_di1
, dev_priv
->back_di1
);
541 OUT_RING(CMD_OP_Z_BUFFER_INFO
);
542 OUT_RING(dev_priv
->zi1
);
544 OUT_RING(GFX_OP_DESTBUFFER_VARS
);
545 OUT_RING(code
[I810_DESTREG_DV1
]);
547 OUT_RING(GFX_OP_DRAWRECT_INFO
);
548 OUT_RING(code
[I810_DESTREG_DR1
]);
549 OUT_RING(code
[I810_DESTREG_DR2
]);
550 OUT_RING(code
[I810_DESTREG_DR3
]);
551 OUT_RING(code
[I810_DESTREG_DR4
]);
557 static void i810EmitState(struct drm_device
*dev
)
559 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
560 drm_i810_sarea_t
*sarea_priv
= dev_priv
->sarea_priv
;
561 unsigned int dirty
= sarea_priv
->dirty
;
563 DRM_DEBUG("%x\n", dirty
);
565 if (dirty
& I810_UPLOAD_BUFFERS
) {
566 i810EmitDestVerified(dev
, sarea_priv
->BufferState
);
567 sarea_priv
->dirty
&= ~I810_UPLOAD_BUFFERS
;
570 if (dirty
& I810_UPLOAD_CTX
) {
571 i810EmitContextVerified(dev
, sarea_priv
->ContextState
);
572 sarea_priv
->dirty
&= ~I810_UPLOAD_CTX
;
575 if (dirty
& I810_UPLOAD_TEX0
) {
576 i810EmitTexVerified(dev
, sarea_priv
->TexState
[0]);
577 sarea_priv
->dirty
&= ~I810_UPLOAD_TEX0
;
580 if (dirty
& I810_UPLOAD_TEX1
) {
581 i810EmitTexVerified(dev
, sarea_priv
->TexState
[1]);
582 sarea_priv
->dirty
&= ~I810_UPLOAD_TEX1
;
588 static void i810_dma_dispatch_clear(struct drm_device
*dev
, int flags
,
589 unsigned int clear_color
,
590 unsigned int clear_zval
)
592 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
593 drm_i810_sarea_t
*sarea_priv
= dev_priv
->sarea_priv
;
594 int nbox
= sarea_priv
->nbox
;
595 struct drm_clip_rect
*pbox
= sarea_priv
->boxes
;
596 int pitch
= dev_priv
->pitch
;
601 if (dev_priv
->current_page
== 1) {
602 unsigned int tmp
= flags
;
604 flags
&= ~(I810_FRONT
| I810_BACK
);
605 if (tmp
& I810_FRONT
)
611 i810_kernel_lost_context(dev
);
613 if (nbox
> I810_NR_SAREA_CLIPRECTS
)
614 nbox
= I810_NR_SAREA_CLIPRECTS
;
616 for (i
= 0; i
< nbox
; i
++, pbox
++) {
617 unsigned int x
= pbox
->x1
;
618 unsigned int y
= pbox
->y1
;
619 unsigned int width
= (pbox
->x2
- x
) * cpp
;
620 unsigned int height
= pbox
->y2
- y
;
621 unsigned int start
= y
* pitch
+ x
* cpp
;
623 if (pbox
->x1
> pbox
->x2
||
624 pbox
->y1
> pbox
->y2
||
625 pbox
->x2
> dev_priv
->w
|| pbox
->y2
> dev_priv
->h
)
628 if (flags
& I810_FRONT
) {
630 OUT_RING(BR00_BITBLT_CLIENT
| BR00_OP_COLOR_BLT
| 0x3);
631 OUT_RING(BR13_SOLID_PATTERN
| (0xF0 << 16) | pitch
);
632 OUT_RING((height
<< 16) | width
);
634 OUT_RING(clear_color
);
639 if (flags
& I810_BACK
) {
641 OUT_RING(BR00_BITBLT_CLIENT
| BR00_OP_COLOR_BLT
| 0x3);
642 OUT_RING(BR13_SOLID_PATTERN
| (0xF0 << 16) | pitch
);
643 OUT_RING((height
<< 16) | width
);
644 OUT_RING(dev_priv
->back_offset
+ start
);
645 OUT_RING(clear_color
);
650 if (flags
& I810_DEPTH
) {
652 OUT_RING(BR00_BITBLT_CLIENT
| BR00_OP_COLOR_BLT
| 0x3);
653 OUT_RING(BR13_SOLID_PATTERN
| (0xF0 << 16) | pitch
);
654 OUT_RING((height
<< 16) | width
);
655 OUT_RING(dev_priv
->depth_offset
+ start
);
656 OUT_RING(clear_zval
);
663 static void i810_dma_dispatch_swap(struct drm_device
*dev
)
665 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
666 drm_i810_sarea_t
*sarea_priv
= dev_priv
->sarea_priv
;
667 int nbox
= sarea_priv
->nbox
;
668 struct drm_clip_rect
*pbox
= sarea_priv
->boxes
;
669 int pitch
= dev_priv
->pitch
;
674 DRM_DEBUG("swapbuffers\n");
676 i810_kernel_lost_context(dev
);
678 if (nbox
> I810_NR_SAREA_CLIPRECTS
)
679 nbox
= I810_NR_SAREA_CLIPRECTS
;
681 for (i
= 0; i
< nbox
; i
++, pbox
++) {
682 unsigned int w
= pbox
->x2
- pbox
->x1
;
683 unsigned int h
= pbox
->y2
- pbox
->y1
;
684 unsigned int dst
= pbox
->x1
* cpp
+ pbox
->y1
* pitch
;
685 unsigned int start
= dst
;
687 if (pbox
->x1
> pbox
->x2
||
688 pbox
->y1
> pbox
->y2
||
689 pbox
->x2
> dev_priv
->w
|| pbox
->y2
> dev_priv
->h
)
693 OUT_RING(BR00_BITBLT_CLIENT
| BR00_OP_SRC_COPY_BLT
| 0x4);
694 OUT_RING(pitch
| (0xCC << 16));
695 OUT_RING((h
<< 16) | (w
* cpp
));
696 if (dev_priv
->current_page
== 0)
697 OUT_RING(dev_priv
->front_offset
+ start
);
699 OUT_RING(dev_priv
->back_offset
+ start
);
701 if (dev_priv
->current_page
== 0)
702 OUT_RING(dev_priv
->back_offset
+ start
);
704 OUT_RING(dev_priv
->front_offset
+ start
);
709 static void i810_dma_dispatch_vertex(struct drm_device
*dev
,
710 struct drm_buf
*buf
, int discard
, int used
)
712 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
713 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
714 drm_i810_sarea_t
*sarea_priv
= dev_priv
->sarea_priv
;
715 struct drm_clip_rect
*box
= sarea_priv
->boxes
;
716 int nbox
= sarea_priv
->nbox
;
717 unsigned long address
= (unsigned long)buf
->bus_address
;
718 unsigned long start
= address
- dev
->agp
->base
;
722 i810_kernel_lost_context(dev
);
724 if (nbox
> I810_NR_SAREA_CLIPRECTS
)
725 nbox
= I810_NR_SAREA_CLIPRECTS
;
730 if (sarea_priv
->dirty
)
733 if (buf_priv
->currently_mapped
== I810_BUF_MAPPED
) {
734 unsigned int prim
= (sarea_priv
->vertex_prim
& PR_MASK
);
736 *(u32
*) buf_priv
->kernel_virtual
=
737 ((GFX_OP_PRIMITIVE
| prim
| ((used
/ 4) - 2)));
740 *(u32
*) ((char *) buf_priv
->kernel_virtual
+ used
) = 0;
744 i810_unmap_buffer(buf
);
751 OUT_RING(GFX_OP_SCISSOR
| SC_UPDATE_SCISSOR
|
753 OUT_RING(GFX_OP_SCISSOR_INFO
);
754 OUT_RING(box
[i
].x1
| (box
[i
].y1
<< 16));
755 OUT_RING((box
[i
].x2
-
756 1) | ((box
[i
].y2
- 1) << 16));
761 OUT_RING(CMD_OP_BATCH_BUFFER
);
762 OUT_RING(start
| BB1_PROTECTED
);
763 OUT_RING(start
+ used
- 4);
767 } while (++i
< nbox
);
773 (void)cmpxchg(buf_priv
->in_use
, I810_BUF_CLIENT
,
777 OUT_RING(CMD_STORE_DWORD_IDX
);
779 OUT_RING(dev_priv
->counter
);
780 OUT_RING(CMD_STORE_DWORD_IDX
);
781 OUT_RING(buf_priv
->my_use_idx
);
782 OUT_RING(I810_BUF_FREE
);
783 OUT_RING(CMD_REPORT_HEAD
);
789 static void i810_dma_dispatch_flip(struct drm_device
*dev
)
791 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
792 int pitch
= dev_priv
->pitch
;
795 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
796 dev_priv
->current_page
,
797 dev_priv
->sarea_priv
->pf_current_page
);
799 i810_kernel_lost_context(dev
);
802 OUT_RING(INST_PARSER_CLIENT
| INST_OP_FLUSH
| INST_FLUSH_MAP_CACHE
);
806 BEGIN_LP_RING(I810_DEST_SETUP_SIZE
+ 2);
807 /* On i815 at least ASYNC is buggy */
808 /* pitch<<5 is from 11.2.8 p158,
809 its the pitch / 8 then left shifted 8,
810 so (pitch >> 3) << 8 */
811 OUT_RING(CMD_OP_FRONTBUFFER_INFO
| (pitch
<< 5) /*| ASYNC_FLIP */ );
812 if (dev_priv
->current_page
== 0) {
813 OUT_RING(dev_priv
->back_offset
);
814 dev_priv
->current_page
= 1;
816 OUT_RING(dev_priv
->front_offset
);
817 dev_priv
->current_page
= 0;
823 OUT_RING(CMD_OP_WAIT_FOR_EVENT
| WAIT_FOR_PLANE_A_FLIP
);
827 /* Increment the frame counter. The client-side 3D driver must
828 * throttle the framerate by waiting for this value before
829 * performing the swapbuffer ioctl.
831 dev_priv
->sarea_priv
->pf_current_page
= dev_priv
->current_page
;
835 static void i810_dma_quiescent(struct drm_device
*dev
)
837 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
840 i810_kernel_lost_context(dev
);
843 OUT_RING(INST_PARSER_CLIENT
| INST_OP_FLUSH
| INST_FLUSH_MAP_CACHE
);
844 OUT_RING(CMD_REPORT_HEAD
);
849 i810_wait_ring(dev
, dev_priv
->ring
.Size
- 8);
852 static int i810_flush_queue(struct drm_device
*dev
)
854 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
855 struct drm_device_dma
*dma
= dev
->dma
;
859 i810_kernel_lost_context(dev
);
862 OUT_RING(CMD_REPORT_HEAD
);
866 i810_wait_ring(dev
, dev_priv
->ring
.Size
- 8);
868 for (i
= 0; i
< dma
->buf_count
; i
++) {
869 struct drm_buf
*buf
= dma
->buflist
[i
];
870 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
872 int used
= cmpxchg(buf_priv
->in_use
, I810_BUF_HARDWARE
,
875 if (used
== I810_BUF_HARDWARE
)
876 DRM_DEBUG("reclaimed from HARDWARE\n");
877 if (used
== I810_BUF_CLIENT
)
878 DRM_DEBUG("still on client\n");
884 /* Must be called with the lock held */
885 void i810_driver_reclaim_buffers(struct drm_device
*dev
,
886 struct drm_file
*file_priv
)
888 struct drm_device_dma
*dma
= dev
->dma
;
893 if (!dev
->dev_private
)
898 i810_flush_queue(dev
);
900 for (i
= 0; i
< dma
->buf_count
; i
++) {
901 struct drm_buf
*buf
= dma
->buflist
[i
];
902 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
904 if (buf
->file_priv
== file_priv
&& buf_priv
) {
905 int used
= cmpxchg(buf_priv
->in_use
, I810_BUF_CLIENT
,
908 if (used
== I810_BUF_CLIENT
)
909 DRM_DEBUG("reclaimed from client\n");
910 if (buf_priv
->currently_mapped
== I810_BUF_MAPPED
)
911 buf_priv
->currently_mapped
= I810_BUF_UNMAPPED
;
916 static int i810_flush_ioctl(struct drm_device
*dev
, void *data
,
917 struct drm_file
*file_priv
)
919 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
921 i810_flush_queue(dev
);
925 static int i810_dma_vertex(struct drm_device
*dev
, void *data
,
926 struct drm_file
*file_priv
)
928 struct drm_device_dma
*dma
= dev
->dma
;
929 drm_i810_private_t
*dev_priv
= (drm_i810_private_t
*) dev
->dev_private
;
930 u32
*hw_status
= dev_priv
->hw_status_page
;
931 drm_i810_sarea_t
*sarea_priv
= (drm_i810_sarea_t
*)
932 dev_priv
->sarea_priv
;
933 drm_i810_vertex_t
*vertex
= data
;
935 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
937 DRM_DEBUG("idx %d used %d discard %d\n",
938 vertex
->idx
, vertex
->used
, vertex
->discard
);
940 if (vertex
->idx
< 0 || vertex
->idx
> dma
->buf_count
)
943 i810_dma_dispatch_vertex(dev
,
944 dma
->buflist
[vertex
->idx
],
945 vertex
->discard
, vertex
->used
);
947 sarea_priv
->last_enqueue
= dev_priv
->counter
- 1;
948 sarea_priv
->last_dispatch
= (int)hw_status
[5];
953 static int i810_clear_bufs(struct drm_device
*dev
, void *data
,
954 struct drm_file
*file_priv
)
956 drm_i810_clear_t
*clear
= data
;
958 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
960 /* GH: Someone's doing nasty things... */
961 if (!dev
->dev_private
)
964 i810_dma_dispatch_clear(dev
, clear
->flags
,
965 clear
->clear_color
, clear
->clear_depth
);
969 static int i810_swap_bufs(struct drm_device
*dev
, void *data
,
970 struct drm_file
*file_priv
)
974 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
976 i810_dma_dispatch_swap(dev
);
980 static int i810_getage(struct drm_device
*dev
, void *data
,
981 struct drm_file
*file_priv
)
983 drm_i810_private_t
*dev_priv
= (drm_i810_private_t
*) dev
->dev_private
;
984 u32
*hw_status
= dev_priv
->hw_status_page
;
985 drm_i810_sarea_t
*sarea_priv
= (drm_i810_sarea_t
*)
986 dev_priv
->sarea_priv
;
988 sarea_priv
->last_dispatch
= (int)hw_status
[5];
992 static int i810_getbuf(struct drm_device
*dev
, void *data
,
993 struct drm_file
*file_priv
)
996 drm_i810_dma_t
*d
= data
;
997 drm_i810_private_t
*dev_priv
= (drm_i810_private_t
*) dev
->dev_private
;
998 u32
*hw_status
= dev_priv
->hw_status_page
;
999 drm_i810_sarea_t
*sarea_priv
= (drm_i810_sarea_t
*)
1000 dev_priv
->sarea_priv
;
1002 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
1006 retcode
= i810_dma_get_buffer(dev
, d
, file_priv
);
1008 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
1009 task_pid_nr(current
), retcode
, d
->granted
);
1011 sarea_priv
->last_dispatch
= (int)hw_status
[5];
1016 static int i810_copybuf(struct drm_device
*dev
, void *data
,
1017 struct drm_file
*file_priv
)
1019 /* Never copy - 2.4.x doesn't need it */
1023 static int i810_docopy(struct drm_device
*dev
, void *data
,
1024 struct drm_file
*file_priv
)
1026 /* Never copy - 2.4.x doesn't need it */
1030 static void i810_dma_dispatch_mc(struct drm_device
*dev
, struct drm_buf
*buf
, int used
,
1031 unsigned int last_render
)
1033 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
1034 drm_i810_buf_priv_t
*buf_priv
= buf
->dev_private
;
1035 drm_i810_sarea_t
*sarea_priv
= dev_priv
->sarea_priv
;
1036 unsigned long address
= (unsigned long)buf
->bus_address
;
1037 unsigned long start
= address
- dev
->agp
->base
;
1041 i810_kernel_lost_context(dev
);
1043 u
= cmpxchg(buf_priv
->in_use
, I810_BUF_CLIENT
, I810_BUF_HARDWARE
);
1044 if (u
!= I810_BUF_CLIENT
)
1045 DRM_DEBUG("MC found buffer that isn't mine!\n");
1047 if (used
> 4 * 1024)
1050 sarea_priv
->dirty
= 0x7f;
1052 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address
, used
);
1054 dev_priv
->counter
++;
1055 DRM_DEBUG("dispatch counter : %ld\n", dev_priv
->counter
);
1056 DRM_DEBUG("start : %lx\n", start
);
1057 DRM_DEBUG("used : %d\n", used
);
1058 DRM_DEBUG("start + used - 4 : %ld\n", start
+ used
- 4);
1060 if (buf_priv
->currently_mapped
== I810_BUF_MAPPED
) {
1062 *(u32
*) ((char *) buf_priv
->virtual + used
) = 0;
1066 i810_unmap_buffer(buf
);
1069 OUT_RING(CMD_OP_BATCH_BUFFER
);
1070 OUT_RING(start
| BB1_PROTECTED
);
1071 OUT_RING(start
+ used
- 4);
1076 OUT_RING(CMD_STORE_DWORD_IDX
);
1077 OUT_RING(buf_priv
->my_use_idx
);
1078 OUT_RING(I810_BUF_FREE
);
1081 OUT_RING(CMD_STORE_DWORD_IDX
);
1083 OUT_RING(last_render
);
1088 static int i810_dma_mc(struct drm_device
*dev
, void *data
,
1089 struct drm_file
*file_priv
)
1091 struct drm_device_dma
*dma
= dev
->dma
;
1092 drm_i810_private_t
*dev_priv
= (drm_i810_private_t
*) dev
->dev_private
;
1093 u32
*hw_status
= dev_priv
->hw_status_page
;
1094 drm_i810_sarea_t
*sarea_priv
= (drm_i810_sarea_t
*)
1095 dev_priv
->sarea_priv
;
1096 drm_i810_mc_t
*mc
= data
;
1098 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
1100 if (mc
->idx
>= dma
->buf_count
|| mc
->idx
< 0)
1103 i810_dma_dispatch_mc(dev
, dma
->buflist
[mc
->idx
], mc
->used
,
1106 sarea_priv
->last_enqueue
= dev_priv
->counter
- 1;
1107 sarea_priv
->last_dispatch
= (int)hw_status
[5];
1112 static int i810_rstatus(struct drm_device
*dev
, void *data
,
1113 struct drm_file
*file_priv
)
1115 drm_i810_private_t
*dev_priv
= (drm_i810_private_t
*) dev
->dev_private
;
1117 return (int)(((u32
*) (dev_priv
->hw_status_page
))[4]);
1120 static int i810_ov0_info(struct drm_device
*dev
, void *data
,
1121 struct drm_file
*file_priv
)
1123 drm_i810_private_t
*dev_priv
= (drm_i810_private_t
*) dev
->dev_private
;
1124 drm_i810_overlay_t
*ov
= data
;
1126 ov
->offset
= dev_priv
->overlay_offset
;
1127 ov
->physical
= dev_priv
->overlay_physical
;
1132 static int i810_fstatus(struct drm_device
*dev
, void *data
,
1133 struct drm_file
*file_priv
)
1135 drm_i810_private_t
*dev_priv
= (drm_i810_private_t
*) dev
->dev_private
;
1137 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
1138 return I810_READ(0x30008);
1141 static int i810_ov0_flip(struct drm_device
*dev
, void *data
,
1142 struct drm_file
*file_priv
)
1144 drm_i810_private_t
*dev_priv
= (drm_i810_private_t
*) dev
->dev_private
;
1146 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
1148 /* Tell the overlay to update */
1149 I810_WRITE(0x30000, dev_priv
->overlay_physical
| 0x80000000);
1154 /* Not sure why this isn't set all the time:
1156 static void i810_do_init_pageflip(struct drm_device
*dev
)
1158 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
1161 dev_priv
->page_flipping
= 1;
1162 dev_priv
->current_page
= 0;
1163 dev_priv
->sarea_priv
->pf_current_page
= dev_priv
->current_page
;
1166 static int i810_do_cleanup_pageflip(struct drm_device
*dev
)
1168 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
1171 if (dev_priv
->current_page
!= 0)
1172 i810_dma_dispatch_flip(dev
);
1174 dev_priv
->page_flipping
= 0;
1178 static int i810_flip_bufs(struct drm_device
*dev
, void *data
,
1179 struct drm_file
*file_priv
)
1181 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
1185 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
1187 if (!dev_priv
->page_flipping
)
1188 i810_do_init_pageflip(dev
);
1190 i810_dma_dispatch_flip(dev
);
1194 int i810_driver_load(struct drm_device
*dev
, unsigned long flags
)
1196 /* Our userspace depends upon the agp mapping support. */
1200 pci_set_master(dev
->pdev
);
1205 void i810_driver_lastclose(struct drm_device
*dev
)
1207 i810_dma_cleanup(dev
);
1210 void i810_driver_preclose(struct drm_device
*dev
, struct drm_file
*file_priv
)
1212 if (dev
->dev_private
) {
1213 drm_i810_private_t
*dev_priv
= dev
->dev_private
;
1214 if (dev_priv
->page_flipping
)
1215 i810_do_cleanup_pageflip(dev
);
1218 if (file_priv
->master
&& file_priv
->master
->lock
.hw_lock
) {
1219 drm_idlelock_take(&file_priv
->master
->lock
);
1220 i810_driver_reclaim_buffers(dev
, file_priv
);
1221 drm_idlelock_release(&file_priv
->master
->lock
);
1223 /* master disappeared, clean up stuff anyway and hope nothing
1225 i810_driver_reclaim_buffers(dev
, file_priv
);
1230 int i810_driver_dma_quiescent(struct drm_device
*dev
)
1232 i810_dma_quiescent(dev
);
1236 const struct drm_ioctl_desc i810_ioctls
[] = {
1237 DRM_IOCTL_DEF_DRV(I810_INIT
, i810_dma_init
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
1238 DRM_IOCTL_DEF_DRV(I810_VERTEX
, i810_dma_vertex
, DRM_AUTH
|DRM_UNLOCKED
),
1239 DRM_IOCTL_DEF_DRV(I810_CLEAR
, i810_clear_bufs
, DRM_AUTH
|DRM_UNLOCKED
),
1240 DRM_IOCTL_DEF_DRV(I810_FLUSH
, i810_flush_ioctl
, DRM_AUTH
|DRM_UNLOCKED
),
1241 DRM_IOCTL_DEF_DRV(I810_GETAGE
, i810_getage
, DRM_AUTH
|DRM_UNLOCKED
),
1242 DRM_IOCTL_DEF_DRV(I810_GETBUF
, i810_getbuf
, DRM_AUTH
|DRM_UNLOCKED
),
1243 DRM_IOCTL_DEF_DRV(I810_SWAP
, i810_swap_bufs
, DRM_AUTH
|DRM_UNLOCKED
),
1244 DRM_IOCTL_DEF_DRV(I810_COPY
, i810_copybuf
, DRM_AUTH
|DRM_UNLOCKED
),
1245 DRM_IOCTL_DEF_DRV(I810_DOCOPY
, i810_docopy
, DRM_AUTH
|DRM_UNLOCKED
),
1246 DRM_IOCTL_DEF_DRV(I810_OV0INFO
, i810_ov0_info
, DRM_AUTH
|DRM_UNLOCKED
),
1247 DRM_IOCTL_DEF_DRV(I810_FSTATUS
, i810_fstatus
, DRM_AUTH
|DRM_UNLOCKED
),
1248 DRM_IOCTL_DEF_DRV(I810_OV0FLIP
, i810_ov0_flip
, DRM_AUTH
|DRM_UNLOCKED
),
1249 DRM_IOCTL_DEF_DRV(I810_MC
, i810_dma_mc
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
1250 DRM_IOCTL_DEF_DRV(I810_RSTATUS
, i810_rstatus
, DRM_AUTH
|DRM_UNLOCKED
),
1251 DRM_IOCTL_DEF_DRV(I810_FLIP
, i810_flip_bufs
, DRM_AUTH
|DRM_UNLOCKED
),
1254 int i810_max_ioctl
= DRM_ARRAY_SIZE(i810_ioctls
);
1257 * Determine if the device really is AGP or not.
1259 * All Intel graphics chipsets are treated as AGP, even if they are really
1262 * \param dev The device to be tested.
1265 * A value of 1 is always retured to indictate every i810 is AGP.
1267 int i810_driver_device_is_agp(struct drm_device
*dev
)