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_IO | 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,
116 .fasync = drm_fasync,
118 .compat_ioctl = drm_compat_ioctl,
120 .llseek = noop_llseek,
123 static int i810_map_buffer(struct drm_buf *buf, struct drm_file *file_priv)
125 struct drm_device *dev = file_priv->minor->dev;
126 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
127 drm_i810_private_t *dev_priv = dev->dev_private;
128 const struct file_operations *old_fops;
131 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
134 /* This is all entirely broken */
135 old_fops = file_priv->filp->f_op;
136 file_priv->filp->f_op = &i810_buffer_fops;
137 dev_priv->mmap_buffer = buf;
138 buf_priv->virtual = (void *)vm_mmap(file_priv->filp, 0, buf->total,
139 PROT_READ | PROT_WRITE,
140 MAP_SHARED, buf->bus_address);
141 dev_priv->mmap_buffer = NULL;
142 file_priv->filp->f_op = old_fops;
143 if (IS_ERR(buf_priv->virtual)) {
145 DRM_ERROR("mmap error\n");
146 retcode = PTR_ERR(buf_priv->virtual);
147 buf_priv->virtual = NULL;
153 static int i810_unmap_buffer(struct drm_buf *buf)
155 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
158 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
161 retcode = vm_munmap((unsigned long)buf_priv->virtual,
162 (size_t) buf->total);
164 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
165 buf_priv->virtual = NULL;
170 static int i810_dma_get_buffer(struct drm_device *dev, drm_i810_dma_t *d,
171 struct drm_file *file_priv)
174 drm_i810_buf_priv_t *buf_priv;
177 buf = i810_freelist_get(dev);
180 DRM_DEBUG("retcode=%d\n", retcode);
184 retcode = i810_map_buffer(buf, file_priv);
186 i810_freelist_put(dev, buf);
187 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
190 buf->file_priv = file_priv;
191 buf_priv = buf->dev_private;
193 d->request_idx = buf->idx;
194 d->request_size = buf->total;
195 d->virtual = buf_priv->virtual;
200 static int i810_dma_cleanup(struct drm_device *dev)
202 struct drm_device_dma *dma = dev->dma;
204 /* Make sure interrupts are disabled here because the uninstall ioctl
205 * may not have been called from userspace and after dev_private
206 * is freed, it's too late.
208 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
209 drm_irq_uninstall(dev);
211 if (dev->dev_private) {
213 drm_i810_private_t *dev_priv =
214 (drm_i810_private_t *) dev->dev_private;
216 if (dev_priv->ring.virtual_start)
217 drm_core_ioremapfree(&dev_priv->ring.map, dev);
218 if (dev_priv->hw_status_page) {
219 pci_free_consistent(dev->pdev, PAGE_SIZE,
220 dev_priv->hw_status_page,
221 dev_priv->dma_status_page);
223 kfree(dev->dev_private);
224 dev->dev_private = NULL;
226 for (i = 0; i < dma->buf_count; i++) {
227 struct drm_buf *buf = dma->buflist[i];
228 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
230 if (buf_priv->kernel_virtual && buf->total)
231 drm_core_ioremapfree(&buf_priv->map, dev);
237 static int i810_wait_ring(struct drm_device *dev, int n)
239 drm_i810_private_t *dev_priv = dev->dev_private;
240 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
243 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
245 end = jiffies + (HZ * 3);
246 while (ring->space < n) {
247 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
248 ring->space = ring->head - (ring->tail + 8);
250 ring->space += ring->Size;
252 if (ring->head != last_head) {
253 end = jiffies + (HZ * 3);
254 last_head = ring->head;
258 if (time_before(end, jiffies)) {
259 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
260 DRM_ERROR("lockup\n");
270 static void i810_kernel_lost_context(struct drm_device *dev)
272 drm_i810_private_t *dev_priv = dev->dev_private;
273 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
275 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
276 ring->tail = I810_READ(LP_RING + RING_TAIL);
277 ring->space = ring->head - (ring->tail + 8);
279 ring->space += ring->Size;
282 static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
284 struct drm_device_dma *dma = dev->dma;
286 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
289 if (dma->buf_count > 1019) {
290 /* Not enough space in the status page for the freelist */
294 for (i = 0; i < dma->buf_count; i++) {
295 struct drm_buf *buf = dma->buflist[i];
296 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
298 buf_priv->in_use = hw_status++;
299 buf_priv->my_use_idx = my_idx;
302 *buf_priv->in_use = I810_BUF_FREE;
304 buf_priv->map.offset = buf->bus_address;
305 buf_priv->map.size = buf->total;
306 buf_priv->map.type = _DRM_AGP;
307 buf_priv->map.flags = 0;
308 buf_priv->map.mtrr = 0;
310 drm_core_ioremap(&buf_priv->map, dev);
311 buf_priv->kernel_virtual = buf_priv->map.handle;
317 static int i810_dma_initialize(struct drm_device *dev,
318 drm_i810_private_t *dev_priv,
319 drm_i810_init_t *init)
321 struct drm_map_list *r_list;
322 memset(dev_priv, 0, sizeof(drm_i810_private_t));
324 list_for_each_entry(r_list, &dev->maplist, head) {
326 r_list->map->type == _DRM_SHM &&
327 r_list->map->flags & _DRM_CONTAINS_LOCK) {
328 dev_priv->sarea_map = r_list->map;
332 if (!dev_priv->sarea_map) {
333 dev->dev_private = (void *)dev_priv;
334 i810_dma_cleanup(dev);
335 DRM_ERROR("can not find sarea!\n");
338 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
339 if (!dev_priv->mmio_map) {
340 dev->dev_private = (void *)dev_priv;
341 i810_dma_cleanup(dev);
342 DRM_ERROR("can not find mmio map!\n");
345 dev->agp_buffer_token = init->buffers_offset;
346 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
347 if (!dev->agp_buffer_map) {
348 dev->dev_private = (void *)dev_priv;
349 i810_dma_cleanup(dev);
350 DRM_ERROR("can not find dma buffer map!\n");
354 dev_priv->sarea_priv = (drm_i810_sarea_t *)
355 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
357 dev_priv->ring.Start = init->ring_start;
358 dev_priv->ring.End = init->ring_end;
359 dev_priv->ring.Size = init->ring_size;
361 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
362 dev_priv->ring.map.size = init->ring_size;
363 dev_priv->ring.map.type = _DRM_AGP;
364 dev_priv->ring.map.flags = 0;
365 dev_priv->ring.map.mtrr = 0;
367 drm_core_ioremap(&dev_priv->ring.map, dev);
369 if (dev_priv->ring.map.handle == NULL) {
370 dev->dev_private = (void *)dev_priv;
371 i810_dma_cleanup(dev);
372 DRM_ERROR("can not ioremap virtual address for"
377 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
379 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
381 dev_priv->w = init->w;
382 dev_priv->h = init->h;
383 dev_priv->pitch = init->pitch;
384 dev_priv->back_offset = init->back_offset;
385 dev_priv->depth_offset = init->depth_offset;
386 dev_priv->front_offset = init->front_offset;
388 dev_priv->overlay_offset = init->overlay_offset;
389 dev_priv->overlay_physical = init->overlay_physical;
391 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
392 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
393 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
395 /* Program Hardware Status Page */
396 dev_priv->hw_status_page =
397 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
398 &dev_priv->dma_status_page);
399 if (!dev_priv->hw_status_page) {
400 dev->dev_private = (void *)dev_priv;
401 i810_dma_cleanup(dev);
402 DRM_ERROR("Can not allocate hardware status page\n");
405 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
406 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
408 I810_WRITE(0x02080, dev_priv->dma_status_page);
409 DRM_DEBUG("Enabled hardware status page\n");
411 /* Now we need to init our freelist */
412 if (i810_freelist_init(dev, dev_priv) != 0) {
413 dev->dev_private = (void *)dev_priv;
414 i810_dma_cleanup(dev);
415 DRM_ERROR("Not enough space in the status page for"
419 dev->dev_private = (void *)dev_priv;
424 static int i810_dma_init(struct drm_device *dev, void *data,
425 struct drm_file *file_priv)
427 drm_i810_private_t *dev_priv;
428 drm_i810_init_t *init = data;
431 switch (init->func) {
432 case I810_INIT_DMA_1_4:
433 DRM_INFO("Using v1.4 init.\n");
434 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
435 if (dev_priv == NULL)
437 retcode = i810_dma_initialize(dev, dev_priv, init);
440 case I810_CLEANUP_DMA:
441 DRM_INFO("DMA Cleanup\n");
442 retcode = i810_dma_cleanup(dev);
451 /* Most efficient way to verify state for the i810 is as it is
452 * emitted. Non-conformant state is silently dropped.
454 * Use 'volatile' & local var tmp to force the emitted values to be
455 * identical to the verified ones.
457 static void i810EmitContextVerified(struct drm_device *dev,
458 volatile unsigned int *code)
460 drm_i810_private_t *dev_priv = dev->dev_private;
465 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
467 OUT_RING(GFX_OP_COLOR_FACTOR);
468 OUT_RING(code[I810_CTXREG_CF1]);
470 OUT_RING(GFX_OP_STIPPLE);
471 OUT_RING(code[I810_CTXREG_ST1]);
473 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
476 if ((tmp & (7 << 29)) == (3 << 29) &&
477 (tmp & (0x1f << 24)) < (0x1d << 24)) {
481 printk("constext state dropped!!!\n");
490 static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
492 drm_i810_private_t *dev_priv = dev->dev_private;
497 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
499 OUT_RING(GFX_OP_MAP_INFO);
500 OUT_RING(code[I810_TEXREG_MI1]);
501 OUT_RING(code[I810_TEXREG_MI2]);
502 OUT_RING(code[I810_TEXREG_MI3]);
504 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
507 if ((tmp & (7 << 29)) == (3 << 29) &&
508 (tmp & (0x1f << 24)) < (0x1d << 24)) {
512 printk("texture state dropped!!!\n");
521 /* Need to do some additional checking when setting the dest buffer.
523 static void i810EmitDestVerified(struct drm_device *dev,
524 volatile unsigned int *code)
526 drm_i810_private_t *dev_priv = dev->dev_private;
530 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
532 tmp = code[I810_DESTREG_DI1];
533 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
534 OUT_RING(CMD_OP_DESTBUFFER_INFO);
537 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
538 tmp, dev_priv->front_di1, dev_priv->back_di1);
542 OUT_RING(CMD_OP_Z_BUFFER_INFO);
543 OUT_RING(dev_priv->zi1);
545 OUT_RING(GFX_OP_DESTBUFFER_VARS);
546 OUT_RING(code[I810_DESTREG_DV1]);
548 OUT_RING(GFX_OP_DRAWRECT_INFO);
549 OUT_RING(code[I810_DESTREG_DR1]);
550 OUT_RING(code[I810_DESTREG_DR2]);
551 OUT_RING(code[I810_DESTREG_DR3]);
552 OUT_RING(code[I810_DESTREG_DR4]);
558 static void i810EmitState(struct drm_device *dev)
560 drm_i810_private_t *dev_priv = dev->dev_private;
561 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
562 unsigned int dirty = sarea_priv->dirty;
564 DRM_DEBUG("%x\n", dirty);
566 if (dirty & I810_UPLOAD_BUFFERS) {
567 i810EmitDestVerified(dev, sarea_priv->BufferState);
568 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
571 if (dirty & I810_UPLOAD_CTX) {
572 i810EmitContextVerified(dev, sarea_priv->ContextState);
573 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
576 if (dirty & I810_UPLOAD_TEX0) {
577 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
578 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
581 if (dirty & I810_UPLOAD_TEX1) {
582 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
583 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
589 static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
590 unsigned int clear_color,
591 unsigned int clear_zval)
593 drm_i810_private_t *dev_priv = dev->dev_private;
594 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
595 int nbox = sarea_priv->nbox;
596 struct drm_clip_rect *pbox = sarea_priv->boxes;
597 int pitch = dev_priv->pitch;
602 if (dev_priv->current_page == 1) {
603 unsigned int tmp = flags;
605 flags &= ~(I810_FRONT | I810_BACK);
606 if (tmp & I810_FRONT)
612 i810_kernel_lost_context(dev);
614 if (nbox > I810_NR_SAREA_CLIPRECTS)
615 nbox = I810_NR_SAREA_CLIPRECTS;
617 for (i = 0; i < nbox; i++, pbox++) {
618 unsigned int x = pbox->x1;
619 unsigned int y = pbox->y1;
620 unsigned int width = (pbox->x2 - x) * cpp;
621 unsigned int height = pbox->y2 - y;
622 unsigned int start = y * pitch + x * cpp;
624 if (pbox->x1 > pbox->x2 ||
625 pbox->y1 > pbox->y2 ||
626 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
629 if (flags & I810_FRONT) {
631 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
632 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
633 OUT_RING((height << 16) | width);
635 OUT_RING(clear_color);
640 if (flags & I810_BACK) {
642 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
643 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
644 OUT_RING((height << 16) | width);
645 OUT_RING(dev_priv->back_offset + start);
646 OUT_RING(clear_color);
651 if (flags & I810_DEPTH) {
653 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
654 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
655 OUT_RING((height << 16) | width);
656 OUT_RING(dev_priv->depth_offset + start);
657 OUT_RING(clear_zval);
664 static void i810_dma_dispatch_swap(struct drm_device *dev)
666 drm_i810_private_t *dev_priv = dev->dev_private;
667 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
668 int nbox = sarea_priv->nbox;
669 struct drm_clip_rect *pbox = sarea_priv->boxes;
670 int pitch = dev_priv->pitch;
675 DRM_DEBUG("swapbuffers\n");
677 i810_kernel_lost_context(dev);
679 if (nbox > I810_NR_SAREA_CLIPRECTS)
680 nbox = I810_NR_SAREA_CLIPRECTS;
682 for (i = 0; i < nbox; i++, pbox++) {
683 unsigned int w = pbox->x2 - pbox->x1;
684 unsigned int h = pbox->y2 - pbox->y1;
685 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
686 unsigned int start = dst;
688 if (pbox->x1 > pbox->x2 ||
689 pbox->y1 > pbox->y2 ||
690 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
694 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
695 OUT_RING(pitch | (0xCC << 16));
696 OUT_RING((h << 16) | (w * cpp));
697 if (dev_priv->current_page == 0)
698 OUT_RING(dev_priv->front_offset + start);
700 OUT_RING(dev_priv->back_offset + start);
702 if (dev_priv->current_page == 0)
703 OUT_RING(dev_priv->back_offset + start);
705 OUT_RING(dev_priv->front_offset + start);
710 static void i810_dma_dispatch_vertex(struct drm_device *dev,
711 struct drm_buf *buf, int discard, int used)
713 drm_i810_private_t *dev_priv = dev->dev_private;
714 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
715 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
716 struct drm_clip_rect *box = sarea_priv->boxes;
717 int nbox = sarea_priv->nbox;
718 unsigned long address = (unsigned long)buf->bus_address;
719 unsigned long start = address - dev->agp->base;
723 i810_kernel_lost_context(dev);
725 if (nbox > I810_NR_SAREA_CLIPRECTS)
726 nbox = I810_NR_SAREA_CLIPRECTS;
731 if (sarea_priv->dirty)
734 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
735 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
737 *(u32 *) buf_priv->kernel_virtual =
738 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
741 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
745 i810_unmap_buffer(buf);
752 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
754 OUT_RING(GFX_OP_SCISSOR_INFO);
755 OUT_RING(box[i].x1 | (box[i].y1 << 16));
756 OUT_RING((box[i].x2 -
757 1) | ((box[i].y2 - 1) << 16));
762 OUT_RING(CMD_OP_BATCH_BUFFER);
763 OUT_RING(start | BB1_PROTECTED);
764 OUT_RING(start + used - 4);
768 } while (++i < nbox);
774 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
778 OUT_RING(CMD_STORE_DWORD_IDX);
780 OUT_RING(dev_priv->counter);
781 OUT_RING(CMD_STORE_DWORD_IDX);
782 OUT_RING(buf_priv->my_use_idx);
783 OUT_RING(I810_BUF_FREE);
784 OUT_RING(CMD_REPORT_HEAD);
790 static void i810_dma_dispatch_flip(struct drm_device *dev)
792 drm_i810_private_t *dev_priv = dev->dev_private;
793 int pitch = dev_priv->pitch;
796 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
797 dev_priv->current_page,
798 dev_priv->sarea_priv->pf_current_page);
800 i810_kernel_lost_context(dev);
803 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
807 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
808 /* On i815 at least ASYNC is buggy */
809 /* pitch<<5 is from 11.2.8 p158,
810 its the pitch / 8 then left shifted 8,
811 so (pitch >> 3) << 8 */
812 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
813 if (dev_priv->current_page == 0) {
814 OUT_RING(dev_priv->back_offset);
815 dev_priv->current_page = 1;
817 OUT_RING(dev_priv->front_offset);
818 dev_priv->current_page = 0;
824 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
828 /* Increment the frame counter. The client-side 3D driver must
829 * throttle the framerate by waiting for this value before
830 * performing the swapbuffer ioctl.
832 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
836 static void i810_dma_quiescent(struct drm_device *dev)
838 drm_i810_private_t *dev_priv = dev->dev_private;
841 i810_kernel_lost_context(dev);
844 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
845 OUT_RING(CMD_REPORT_HEAD);
850 i810_wait_ring(dev, dev_priv->ring.Size - 8);
853 static int i810_flush_queue(struct drm_device *dev)
855 drm_i810_private_t *dev_priv = dev->dev_private;
856 struct drm_device_dma *dma = dev->dma;
860 i810_kernel_lost_context(dev);
863 OUT_RING(CMD_REPORT_HEAD);
867 i810_wait_ring(dev, dev_priv->ring.Size - 8);
869 for (i = 0; i < dma->buf_count; i++) {
870 struct drm_buf *buf = dma->buflist[i];
871 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
873 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
876 if (used == I810_BUF_HARDWARE)
877 DRM_DEBUG("reclaimed from HARDWARE\n");
878 if (used == I810_BUF_CLIENT)
879 DRM_DEBUG("still on client\n");
885 /* Must be called with the lock held */
886 void i810_driver_reclaim_buffers(struct drm_device *dev,
887 struct drm_file *file_priv)
889 struct drm_device_dma *dma = dev->dma;
894 if (!dev->dev_private)
899 i810_flush_queue(dev);
901 for (i = 0; i < dma->buf_count; i++) {
902 struct drm_buf *buf = dma->buflist[i];
903 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
905 if (buf->file_priv == file_priv && buf_priv) {
906 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
909 if (used == I810_BUF_CLIENT)
910 DRM_DEBUG("reclaimed from client\n");
911 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
912 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
917 static int i810_flush_ioctl(struct drm_device *dev, void *data,
918 struct drm_file *file_priv)
920 LOCK_TEST_WITH_RETURN(dev, file_priv);
922 i810_flush_queue(dev);
926 static int i810_dma_vertex(struct drm_device *dev, void *data,
927 struct drm_file *file_priv)
929 struct drm_device_dma *dma = dev->dma;
930 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
931 u32 *hw_status = dev_priv->hw_status_page;
932 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
933 dev_priv->sarea_priv;
934 drm_i810_vertex_t *vertex = data;
936 LOCK_TEST_WITH_RETURN(dev, file_priv);
938 DRM_DEBUG("idx %d used %d discard %d\n",
939 vertex->idx, vertex->used, vertex->discard);
941 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
944 i810_dma_dispatch_vertex(dev,
945 dma->buflist[vertex->idx],
946 vertex->discard, vertex->used);
948 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
949 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
950 sarea_priv->last_enqueue = dev_priv->counter - 1;
951 sarea_priv->last_dispatch = (int)hw_status[5];
956 static int i810_clear_bufs(struct drm_device *dev, void *data,
957 struct drm_file *file_priv)
959 drm_i810_clear_t *clear = data;
961 LOCK_TEST_WITH_RETURN(dev, file_priv);
963 /* GH: Someone's doing nasty things... */
964 if (!dev->dev_private)
967 i810_dma_dispatch_clear(dev, clear->flags,
968 clear->clear_color, clear->clear_depth);
972 static int i810_swap_bufs(struct drm_device *dev, void *data,
973 struct drm_file *file_priv)
977 LOCK_TEST_WITH_RETURN(dev, file_priv);
979 i810_dma_dispatch_swap(dev);
983 static int i810_getage(struct drm_device *dev, void *data,
984 struct drm_file *file_priv)
986 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
987 u32 *hw_status = dev_priv->hw_status_page;
988 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
989 dev_priv->sarea_priv;
991 sarea_priv->last_dispatch = (int)hw_status[5];
995 static int i810_getbuf(struct drm_device *dev, void *data,
996 struct drm_file *file_priv)
999 drm_i810_dma_t *d = data;
1000 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1001 u32 *hw_status = dev_priv->hw_status_page;
1002 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1003 dev_priv->sarea_priv;
1005 LOCK_TEST_WITH_RETURN(dev, file_priv);
1009 retcode = i810_dma_get_buffer(dev, d, file_priv);
1011 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
1012 task_pid_nr(current), retcode, d->granted);
1014 sarea_priv->last_dispatch = (int)hw_status[5];
1019 static int i810_copybuf(struct drm_device *dev, void *data,
1020 struct drm_file *file_priv)
1022 /* Never copy - 2.4.x doesn't need it */
1026 static int i810_docopy(struct drm_device *dev, void *data,
1027 struct drm_file *file_priv)
1029 /* Never copy - 2.4.x doesn't need it */
1033 static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
1034 unsigned int last_render)
1036 drm_i810_private_t *dev_priv = dev->dev_private;
1037 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1038 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1039 unsigned long address = (unsigned long)buf->bus_address;
1040 unsigned long start = address - dev->agp->base;
1044 i810_kernel_lost_context(dev);
1046 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
1047 if (u != I810_BUF_CLIENT)
1048 DRM_DEBUG("MC found buffer that isn't mine!\n");
1050 if (used > 4 * 1024)
1053 sarea_priv->dirty = 0x7f;
1055 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
1057 dev_priv->counter++;
1058 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1059 DRM_DEBUG("start : %lx\n", start);
1060 DRM_DEBUG("used : %d\n", used);
1061 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1063 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1065 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
1069 i810_unmap_buffer(buf);
1072 OUT_RING(CMD_OP_BATCH_BUFFER);
1073 OUT_RING(start | BB1_PROTECTED);
1074 OUT_RING(start + used - 4);
1079 OUT_RING(CMD_STORE_DWORD_IDX);
1080 OUT_RING(buf_priv->my_use_idx);
1081 OUT_RING(I810_BUF_FREE);
1084 OUT_RING(CMD_STORE_DWORD_IDX);
1086 OUT_RING(last_render);
1091 static int i810_dma_mc(struct drm_device *dev, void *data,
1092 struct drm_file *file_priv)
1094 struct drm_device_dma *dma = dev->dma;
1095 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1096 u32 *hw_status = dev_priv->hw_status_page;
1097 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1098 dev_priv->sarea_priv;
1099 drm_i810_mc_t *mc = data;
1101 LOCK_TEST_WITH_RETURN(dev, file_priv);
1103 if (mc->idx >= dma->buf_count || mc->idx < 0)
1106 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1109 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
1110 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
1111 sarea_priv->last_enqueue = dev_priv->counter - 1;
1112 sarea_priv->last_dispatch = (int)hw_status[5];
1117 static int i810_rstatus(struct drm_device *dev, void *data,
1118 struct drm_file *file_priv)
1120 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1122 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
1125 static int i810_ov0_info(struct drm_device *dev, void *data,
1126 struct drm_file *file_priv)
1128 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1129 drm_i810_overlay_t *ov = data;
1131 ov->offset = dev_priv->overlay_offset;
1132 ov->physical = dev_priv->overlay_physical;
1137 static int i810_fstatus(struct drm_device *dev, void *data,
1138 struct drm_file *file_priv)
1140 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1142 LOCK_TEST_WITH_RETURN(dev, file_priv);
1143 return I810_READ(0x30008);
1146 static int i810_ov0_flip(struct drm_device *dev, void *data,
1147 struct drm_file *file_priv)
1149 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1151 LOCK_TEST_WITH_RETURN(dev, file_priv);
1153 /* Tell the overlay to update */
1154 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
1159 /* Not sure why this isn't set all the time:
1161 static void i810_do_init_pageflip(struct drm_device *dev)
1163 drm_i810_private_t *dev_priv = dev->dev_private;
1166 dev_priv->page_flipping = 1;
1167 dev_priv->current_page = 0;
1168 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1171 static int i810_do_cleanup_pageflip(struct drm_device *dev)
1173 drm_i810_private_t *dev_priv = dev->dev_private;
1176 if (dev_priv->current_page != 0)
1177 i810_dma_dispatch_flip(dev);
1179 dev_priv->page_flipping = 0;
1183 static int i810_flip_bufs(struct drm_device *dev, void *data,
1184 struct drm_file *file_priv)
1186 drm_i810_private_t *dev_priv = dev->dev_private;
1190 LOCK_TEST_WITH_RETURN(dev, file_priv);
1192 if (!dev_priv->page_flipping)
1193 i810_do_init_pageflip(dev);
1195 i810_dma_dispatch_flip(dev);
1199 int i810_driver_load(struct drm_device *dev, unsigned long flags)
1201 /* i810 has 4 more counters */
1203 dev->types[6] = _DRM_STAT_IRQ;
1204 dev->types[7] = _DRM_STAT_PRIMARY;
1205 dev->types[8] = _DRM_STAT_SECONDARY;
1206 dev->types[9] = _DRM_STAT_DMA;
1208 pci_set_master(dev->pdev);
1213 void i810_driver_lastclose(struct drm_device *dev)
1215 i810_dma_cleanup(dev);
1218 void i810_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
1220 if (dev->dev_private) {
1221 drm_i810_private_t *dev_priv = dev->dev_private;
1222 if (dev_priv->page_flipping)
1223 i810_do_cleanup_pageflip(dev);
1226 if (file_priv->master && file_priv->master->lock.hw_lock) {
1227 drm_idlelock_take(&file_priv->master->lock);
1228 i810_driver_reclaim_buffers(dev, file_priv);
1229 drm_idlelock_release(&file_priv->master->lock);
1231 /* master disappeared, clean up stuff anyway and hope nothing
1233 i810_driver_reclaim_buffers(dev, file_priv);
1238 int i810_driver_dma_quiescent(struct drm_device *dev)
1240 i810_dma_quiescent(dev);
1244 struct drm_ioctl_desc i810_ioctls[] = {
1245 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1246 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1247 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1248 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1249 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1250 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1251 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1252 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1253 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1254 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1255 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1256 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1257 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1258 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1259 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
1262 int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
1265 * Determine if the device really is AGP or not.
1267 * All Intel graphics chipsets are treated as AGP, even if they are really
1270 * \param dev The device to be tested.
1273 * A value of 1 is always retured to indictate every i810 is AGP.
1275 int i810_driver_device_is_agp(struct drm_device *dev)