blob: b0f815d8cea8592028cee0540f6b23ffe3dac9c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i830_dma.c -- DMA support for the I830 -*- linux-c -*-
2 * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
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:
Dave Airlieb5e89ed2005-09-25 14:28:13 +100014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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.
26 *
27 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Abraham vd Merwe <abraham@2d3d.co.za>
31 *
32 */
33
34#include "drmP.h"
35#include "drm.h"
36#include "i830_drm.h"
37#include "i830_drv.h"
38#include <linux/interrupt.h> /* For task queue support */
39#include <linux/pagemap.h> /* For FASTCALL on unlock_page() */
40#include <linux/delay.h>
41#include <asm/uaccess.h>
42
43#define I830_BUF_FREE 2
44#define I830_BUF_CLIENT 1
45#define I830_BUF_HARDWARE 0
46
47#define I830_BUF_UNMAPPED 0
48#define I830_BUF_MAPPED 1
49
Dave Airlieb5e89ed2005-09-25 14:28:13 +100050static drm_buf_t *i830_freelist_get(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100052 drm_device_dma_t *dma = dev->dma;
53 int i;
54 int used;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /* Linear search might not be the best solution */
57
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 for (i = 0; i < dma->buf_count; i++) {
59 drm_buf_t *buf = dma->buflist[i];
60 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100062 used = cmpxchg(buf_priv->in_use, I830_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 I830_BUF_CLIENT);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100064 if (used == I830_BUF_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return buf;
66 }
67 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71/* This should only be called if the buffer is not sent to the hardware
72 * yet, the hardware updates in use for us once its on the ring buffer.
73 */
74
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075static int i830_freelist_put(drm_device_t * dev, drm_buf_t * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
78 int used;
79
80 /* In use is already a pointer */
81 used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT, I830_BUF_FREE);
82 if (used != I830_BUF_CLIENT) {
83 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
84 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086
87 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Dave Airliec94f7022005-07-07 21:03:38 +100090static int i830_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 drm_file_t *priv = filp->private_data;
93 drm_device_t *dev;
94 drm_i830_private_t *dev_priv;
95 drm_buf_t *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 drm_i830_buf_priv_t *buf_priv;
97
98 lock_kernel();
Dave Airlieb5e89ed2005-09-25 14:28:13 +100099 dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000101 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
105 vma->vm_file = filp;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106
107 buf_priv->currently_mapped = I830_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unlock_kernel();
109
110 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000111 VM_OFFSET(vma) >> PAGE_SHIFT,
112 vma->vm_end - vma->vm_start, vma->vm_page_prot))
113 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 0;
115}
116
Dave Airliec94f7022005-07-07 21:03:38 +1000117static struct file_operations i830_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000118 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000119 .release = drm_release,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120 .ioctl = drm_ioctl,
121 .mmap = i830_mmap_buffers,
122 .fasync = drm_fasync,
Dave Airliec94f7022005-07-07 21:03:38 +1000123};
124
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000125static int i830_map_buffer(drm_buf_t * buf, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127 drm_file_t *priv = filp->private_data;
128 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130 drm_i830_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800131 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long virtual;
133 int retcode = 0;
134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
136 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138 down_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 old_fops = filp->f_op;
140 filp->f_op = &i830_buffer_fops;
141 dev_priv->mmap_buffer = buf;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000142 virtual = do_mmap(filp, 0, buf->total, PROT_READ | PROT_WRITE,
143 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 dev_priv->mmap_buffer = NULL;
145 filp->f_op = old_fops;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000146 if (IS_ERR((void *)virtual)) { /* ugh */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* Real error */
148 DRM_ERROR("mmap error\n");
149 retcode = virtual;
150 buf_priv->virtual = NULL;
151 } else {
152 buf_priv->virtual = (void __user *)virtual;
153 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return retcode;
157}
158
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159static int i830_unmap_buffer(drm_buf_t * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
162 int retcode = 0;
163
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164 if (buf_priv->currently_mapped != I830_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -EINVAL;
166
167 down_write(&current->mm->mmap_sem);
168 retcode = do_munmap(current->mm,
169 (unsigned long)buf_priv->virtual,
170 (size_t) buf->total);
171 up_write(&current->mm->mmap_sem);
172
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000173 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
174 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return retcode;
177}
178
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179static int i830_dma_get_buffer(drm_device_t * dev, drm_i830_dma_t * d,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct file *filp)
181{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000182 drm_buf_t *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 drm_i830_buf_priv_t *buf_priv;
184 int retcode = 0;
185
186 buf = i830_freelist_get(dev);
187 if (!buf) {
188 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000189 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return retcode;
191 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 retcode = i830_map_buffer(buf, filp);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 if (retcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 i830_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return retcode;
198 }
199 buf->filp = filp;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200 buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000202 d->request_idx = buf->idx;
203 d->request_size = buf->total;
204 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 return retcode;
207}
208
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209static int i830_dma_cleanup(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 drm_device_dma_t *dma = dev->dma;
212
213 /* Make sure interrupts are disabled here because the uninstall ioctl
214 * may not have been called from userspace and after dev_private
215 * is freed, it's too late.
216 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 if (dev->irq_enabled)
218 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (dev->dev_private) {
221 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222 drm_i830_private_t *dev_priv =
223 (drm_i830_private_t *) dev->dev_private;
224
225 if (dev_priv->ring.virtual_start) {
226 drm_ioremapfree((void *)dev_priv->ring.virtual_start,
227 dev_priv->ring.Size, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229 if (dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 pci_free_consistent(dev->pdev, PAGE_SIZE,
231 dev_priv->hw_status_page,
232 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000233 /* Need to rewrite hardware status page */
234 I830_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237 drm_free(dev->dev_private, sizeof(drm_i830_private_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 DRM_MEM_DRIVER);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 for (i = 0; i < dma->buf_count; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 drm_buf_t *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244 if (buf_priv->kernel_virtual && buf->total)
245 drm_ioremapfree(buf_priv->kernel_virtual,
246 buf->total, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000252int i830_wait_ring(drm_device_t * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 drm_i830_private_t *dev_priv = dev->dev_private;
255 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
256 int iters = 0;
257 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned int last_head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
259
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 end = jiffies + (HZ * 3);
261 while (ring->space < n) {
262 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
263 ring->space = ring->head - (ring->tail + 8);
264 if (ring->space < 0)
265 ring->space += ring->Size;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (ring->head != last_head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268 end = jiffies + (HZ * 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 last_head = ring->head;
270 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000271
272 iters++;
273 if (time_before(end, jiffies)) {
274 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
275 DRM_ERROR("lockup\n");
276 goto out_wait_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 udelay(1);
279 dev_priv->sarea_priv->perf_boxes |= I830_BOX_WAIT;
280 }
281
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000282 out_wait_ring:
283 return iters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286static void i830_kernel_lost_context(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 drm_i830_private_t *dev_priv = dev->dev_private;
289 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
290
291 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
292 ring->tail = I830_READ(LP_RING + RING_TAIL) & TAIL_ADDR;
293 ring->space = ring->head - (ring->tail + 8);
294 if (ring->space < 0)
295 ring->space += ring->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (ring->head == ring->tail)
298 dev_priv->sarea_priv->perf_boxes |= I830_BOX_RING_EMPTY;
299}
300
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000301static int i830_freelist_init(drm_device_t * dev, drm_i830_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000303 drm_device_dma_t *dma = dev->dma;
304 int my_idx = 36;
305 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
306 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000308 if (dma->buf_count > 1019) {
309 /* Not enough space in the status page for the freelist */
310 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 for (i = 0; i < dma->buf_count; i++) {
314 drm_buf_t *buf = dma->buflist[i];
315 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000317 buf_priv->in_use = hw_status++;
318 buf_priv->my_use_idx = my_idx;
319 my_idx += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 *buf_priv->in_use = I830_BUF_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 buf_priv->kernel_virtual = drm_ioremap(buf->bus_address,
324 buf->total, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 return 0;
327}
328
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329static int i830_dma_initialize(drm_device_t * dev,
330 drm_i830_private_t * dev_priv,
331 drm_i830_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct list_head *list;
334
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 memset(dev_priv, 0, sizeof(drm_i830_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 list_for_each(list, &dev->maplist->head) {
338 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 if (r_list->map &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 break;
344 }
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 if (!dev_priv->sarea_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 dev->dev_private = (void *)dev_priv;
349 i830_dma_cleanup(dev);
350 DRM_ERROR("can not find sarea!\n");
351 return -EINVAL;
352 }
353 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 if (!dev_priv->mmio_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dev->dev_private = (void *)dev_priv;
356 i830_dma_cleanup(dev);
357 DRM_ERROR("can not find mmio map!\n");
358 return -EINVAL;
359 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000360 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000362 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 dev->dev_private = (void *)dev_priv;
364 i830_dma_cleanup(dev);
365 DRM_ERROR("can not find dma buffer map!\n");
366 return -EINVAL;
367 }
368
369 dev_priv->sarea_priv = (drm_i830_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000370 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372 dev_priv->ring.Start = init->ring_start;
373 dev_priv->ring.End = init->ring_end;
374 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 dev_priv->ring.virtual_start = drm_ioremap(dev->agp->base +
377 init->ring_start,
378 init->ring_size, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380 if (dev_priv->ring.virtual_start == NULL) {
381 dev->dev_private = (void *)dev_priv;
382 i830_dma_cleanup(dev);
383 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 " ring buffer\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000385 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 dev_priv->w = init->w;
391 dev_priv->h = init->h;
392 dev_priv->pitch = init->pitch;
393 dev_priv->back_offset = init->back_offset;
394 dev_priv->depth_offset = init->depth_offset;
395 dev_priv->front_offset = init->front_offset;
396
397 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
398 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
399 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
400
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000401 DRM_DEBUG("front_di1 %x\n", dev_priv->front_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 DRM_DEBUG("back_offset %x\n", dev_priv->back_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403 DRM_DEBUG("back_di1 %x\n", dev_priv->back_di1);
404 DRM_DEBUG("pitch_bits %x\n", init->pitch_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 dev_priv->cpp = init->cpp;
407 /* We are using separate values as placeholders for mechanisms for
408 * private backbuffer/depthbuffer usage.
409 */
410
411 dev_priv->back_pitch = init->back_pitch;
412 dev_priv->depth_pitch = init->depth_pitch;
413 dev_priv->do_boxes = 0;
414 dev_priv->use_mi_batchbuffer_start = 0;
415
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 /* Program Hardware Status Page */
417 dev_priv->hw_status_page =
418 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
419 &dev_priv->dma_status_page);
420 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dev->dev_private = (void *)dev_priv;
422 i830_dma_cleanup(dev);
423 DRM_ERROR("Can not allocate hardware status page\n");
424 return -ENOMEM;
425 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000428
429 I830_WRITE(0x02080, dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 DRM_DEBUG("Enabled hardware status page\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431
432 /* Now we need to init our freelist */
433 if (i830_freelist_init(dev, dev_priv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435 i830_dma_cleanup(dev);
436 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 dev->dev_private = (void *)dev_priv;
441
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static int i830_dma_init(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 drm_file_t *priv = filp->private_data;
449 drm_device_t *dev = priv->head->dev;
450 drm_i830_private_t *dev_priv;
451 drm_i830_init_t init;
452 int retcode = 0;
453
454 if (copy_from_user(&init, (void *__user)arg, sizeof(init)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456
457 switch (init.func) {
458 case I830_INIT_DMA:
459 dev_priv = drm_alloc(sizeof(drm_i830_private_t),
460 DRM_MEM_DRIVER);
461 if (dev_priv == NULL)
462 return -ENOMEM;
463 retcode = i830_dma_initialize(dev, dev_priv, &init);
464 break;
465 case I830_CLEANUP_DMA:
466 retcode = i830_dma_cleanup(dev);
467 break;
468 default:
469 retcode = -EINVAL;
470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000472
473 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476#define GFX_OP_STIPPLE ((0x3<<29)|(0x1d<<24)|(0x83<<16))
477#define ST1_ENABLE (1<<16)
478#define ST1_MASK (0xffff)
479
480/* Most efficient way to verify state for the i830 is as it is
481 * emitted. Non-conformant state is silently dropped.
482 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483static void i830EmitContextVerified(drm_device_t * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int i, j = 0;
487 unsigned int tmp;
488 RING_LOCALS;
489
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 BEGIN_LP_RING(I830_CTX_SETUP_SIZE + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 for (i = 0; i < I830_CTXREG_BLENDCOLR0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 if ((tmp & (7 << 29)) == CMD_3D &&
495 (tmp & (0x1f << 24)) < (0x1d << 24)) {
496 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 j++;
498 } else {
499 DRM_ERROR("Skipping %d\n", i);
500 }
501 }
502
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000503 OUT_RING(STATE3D_CONST_BLEND_COLOR_CMD);
504 OUT_RING(code[I830_CTXREG_BLENDCOLR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 j += 2;
506
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 for (i = I830_CTXREG_VF; i < I830_CTXREG_MCSB0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000509 if ((tmp & (7 << 29)) == CMD_3D &&
510 (tmp & (0x1f << 24)) < (0x1d << 24)) {
511 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 j++;
513 } else {
514 DRM_ERROR("Skipping %d\n", i);
515 }
516 }
517
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000518 OUT_RING(STATE3D_MAP_COORD_SETBIND_CMD);
519 OUT_RING(code[I830_CTXREG_MCSB1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 j += 2;
521
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000522 if (j & 1)
523 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 ADVANCE_LP_RING();
526}
527
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000528static void i830EmitTexVerified(drm_device_t * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int i, j = 0;
532 unsigned int tmp;
533 RING_LOCALS;
534
535 if (code[I830_TEXREG_MI0] == GFX_OP_MAP_INFO ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000536 (code[I830_TEXREG_MI0] & ~(0xf * LOAD_TEXTURE_MAP0)) ==
537 (STATE3D_LOAD_STATE_IMMEDIATE_2 | 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 BEGIN_LP_RING(I830_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 OUT_RING(code[I830_TEXREG_MI0]); /* TM0LI */
542 OUT_RING(code[I830_TEXREG_MI1]); /* TM0S0 */
543 OUT_RING(code[I830_TEXREG_MI2]); /* TM0S1 */
544 OUT_RING(code[I830_TEXREG_MI3]); /* TM0S2 */
545 OUT_RING(code[I830_TEXREG_MI4]); /* TM0S3 */
546 OUT_RING(code[I830_TEXREG_MI5]); /* TM0S4 */
547
548 for (i = 6; i < I830_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 if (j & 1)
555 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000558 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 printk("rejected packet %x\n", code[0]);
560}
561
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000562static void i830EmitTexBlendVerified(drm_device_t * dev,
563 unsigned int *code, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int i, j = 0;
567 unsigned int tmp;
568 RING_LOCALS;
569
570 if (!num)
571 return;
572
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000573 BEGIN_LP_RING(num + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 j++;
579 }
580
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000581 if (j & 1)
582 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 ADVANCE_LP_RING();
585}
586
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587static void i830EmitTexPalette(drm_device_t * dev,
588 unsigned int *palette, int number, int is_shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int i;
592 RING_LOCALS;
593
594 return;
595
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 BEGIN_LP_RING(258);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 if (is_shared == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 OUT_RING(CMD_OP_MAP_PALETTE_LOAD |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000600 MAP_PALETTE_NUM(0) | MAP_PALETTE_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 } else {
602 OUT_RING(CMD_OP_MAP_PALETTE_LOAD | MAP_PALETTE_NUM(number));
603 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000604 for (i = 0; i < 256; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 OUT_RING(palette[i]);
606 }
607 OUT_RING(0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 /* KW: WHERE IS THE ADVANCE_LP_RING? This is effectively a noop!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
610}
611
612/* Need to do some additional checking when setting the dest buffer.
613 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614static void i830EmitDestVerified(drm_device_t * dev, unsigned int *code)
615{
616 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 unsigned int tmp;
618 RING_LOCALS;
619
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 BEGIN_LP_RING(I830_DEST_SETUP_SIZE + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 tmp = code[I830_DESTREG_CBUFADDR];
623 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
624 if (((int)outring) & 8) {
625 OUT_RING(0);
626 OUT_RING(0);
627 }
628
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 OUT_RING(CMD_OP_DESTBUFFER_INFO);
630 OUT_RING(BUF_3D_ID_COLOR_BACK |
631 BUF_3D_PITCH(dev_priv->back_pitch * dev_priv->cpp) |
632 BUF_3D_USE_FENCE);
633 OUT_RING(tmp);
634 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636 OUT_RING(CMD_OP_DESTBUFFER_INFO);
637 OUT_RING(BUF_3D_ID_DEPTH | BUF_3D_USE_FENCE |
638 BUF_3D_PITCH(dev_priv->depth_pitch * dev_priv->cpp));
639 OUT_RING(dev_priv->zi1);
640 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 } else {
642 DRM_ERROR("bad di1 %x (allow %x or %x)\n",
643 tmp, dev_priv->front_di1, dev_priv->back_di1);
644 }
645
646 /* invarient:
647 */
648
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 OUT_RING(GFX_OP_DESTBUFFER_VARS);
650 OUT_RING(code[I830_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 OUT_RING(GFX_OP_DRAWRECT_INFO);
653 OUT_RING(code[I830_DESTREG_DR1]);
654 OUT_RING(code[I830_DESTREG_DR2]);
655 OUT_RING(code[I830_DESTREG_DR3]);
656 OUT_RING(code[I830_DESTREG_DR4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* Need to verify this */
659 tmp = code[I830_DESTREG_SENABLE];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 if ((tmp & ~0x3) == GFX_OP_SCISSOR_ENABLE) {
661 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 } else {
663 DRM_ERROR("bad scissor enable\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000664 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 OUT_RING(GFX_OP_SCISSOR_RECT);
668 OUT_RING(code[I830_DESTREG_SR1]);
669 OUT_RING(code[I830_DESTREG_SR2]);
670 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 ADVANCE_LP_RING();
673}
674
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675static void i830EmitStippleVerified(drm_device_t * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 RING_LOCALS;
679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 BEGIN_LP_RING(2);
681 OUT_RING(GFX_OP_STIPPLE);
682 OUT_RING(code[1]);
683 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686static void i830EmitState(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 drm_i830_private_t *dev_priv = dev->dev_private;
689 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 unsigned int dirty = sarea_priv->dirty;
691
692 DRM_DEBUG("%s %x\n", __FUNCTION__, dirty);
693
694 if (dirty & I830_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 i830EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 sarea_priv->dirty &= ~I830_UPLOAD_BUFFERS;
697 }
698
699 if (dirty & I830_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 i830EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 sarea_priv->dirty &= ~I830_UPLOAD_CTX;
702 }
703
704 if (dirty & I830_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 i830EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 sarea_priv->dirty &= ~I830_UPLOAD_TEX0;
707 }
708
709 if (dirty & I830_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 i830EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 sarea_priv->dirty &= ~I830_UPLOAD_TEX1;
712 }
713
714 if (dirty & I830_UPLOAD_TEXBLEND0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[0],
716 sarea_priv->TexBlendStateWordsUsed[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND0;
718 }
719
720 if (dirty & I830_UPLOAD_TEXBLEND1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[1],
722 sarea_priv->TexBlendStateWordsUsed[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND1;
724 }
725
726 if (dirty & I830_UPLOAD_TEX_PALETTE_SHARED) {
727 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 1);
728 } else {
729 if (dirty & I830_UPLOAD_TEX_PALETTE_N(0)) {
730 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 0);
731 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(0);
732 }
733 if (dirty & I830_UPLOAD_TEX_PALETTE_N(1)) {
734 i830EmitTexPalette(dev, sarea_priv->Palette[1], 1, 0);
735 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(1);
736 }
737
738 /* 1.3:
739 */
740#if 0
741 if (dirty & I830_UPLOAD_TEX_PALETTE_N(2)) {
742 i830EmitTexPalette(dev, sarea_priv->Palette2[0], 0, 0);
743 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
744 }
745 if (dirty & I830_UPLOAD_TEX_PALETTE_N(3)) {
746 i830EmitTexPalette(dev, sarea_priv->Palette2[1], 1, 0);
747 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
748 }
749#endif
750 }
751
752 /* 1.3:
753 */
754 if (dirty & I830_UPLOAD_STIPPLE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 i830EmitStippleVerified(dev, sarea_priv->StippleState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 sarea_priv->dirty &= ~I830_UPLOAD_STIPPLE;
757 }
758
759 if (dirty & I830_UPLOAD_TEX2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 i830EmitTexVerified(dev, sarea_priv->TexState2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 sarea_priv->dirty &= ~I830_UPLOAD_TEX2;
762 }
763
764 if (dirty & I830_UPLOAD_TEX3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 i830EmitTexVerified(dev, sarea_priv->TexState3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 sarea_priv->dirty &= ~I830_UPLOAD_TEX3;
767 }
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (dirty & I830_UPLOAD_TEXBLEND2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 i830EmitTexBlendVerified(dev,
771 sarea_priv->TexBlendState2,
772 sarea_priv->TexBlendStateWordsUsed2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND2;
775 }
776
777 if (dirty & I830_UPLOAD_TEXBLEND3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778 i830EmitTexBlendVerified(dev,
779 sarea_priv->TexBlendState3,
780 sarea_priv->TexBlendStateWordsUsed3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND3;
782 }
783}
784
785/* ================================================================
786 * Performance monitoring functions
787 */
788
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789static void i830_fill_box(drm_device_t * dev,
790 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 u32 color;
794 unsigned int BR13, CMD;
795 RING_LOCALS;
796
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 BR13 = (0xF0 << 16) | (dev_priv->pitch * dev_priv->cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 CMD = XY_COLOR_BLT_CMD;
799 x += dev_priv->sarea_priv->boxes[0].x1;
800 y += dev_priv->sarea_priv->boxes[0].y1;
801
802 if (dev_priv->cpp == 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 BR13 |= (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 CMD |= (XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 } else {
807 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 BEGIN_LP_RING(6);
812 OUT_RING(CMD);
813 OUT_RING(BR13);
814 OUT_RING((y << 16) | x);
815 OUT_RING(((y + h) << 16) | (x + w));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 if (dev_priv->current_page == 1) {
818 OUT_RING(dev_priv->front_offset);
819 } else {
820 OUT_RING(dev_priv->back_offset);
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ADVANCE_LP_RING();
825}
826
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827static void i830_cp_performance_boxes(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /* Purple box for page flipping
832 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_FLIP)
834 i830_fill_box(dev, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* Red box if we have to wait for idle at any point
837 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_WAIT)
839 i830_fill_box(dev, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* Blue box: lost context?
842 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_LOST_CONTEXT)
844 i830_fill_box(dev, 28, 4, 8, 8, 0, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* Yellow box for texture swaps
847 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_TEXTURE_LOAD)
849 i830_fill_box(dev, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* Green box if hardware never idles (as far as we can tell)
852 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 if (!(dev_priv->sarea_priv->perf_boxes & I830_BOX_RING_EMPTY))
854 i830_fill_box(dev, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * (not a great measure, easily confused)
858 */
859 if (dev_priv->dma_used) {
860 int bar = dev_priv->dma_used / 10240;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 if (bar > 100)
862 bar = 100;
863 if (bar < 1)
864 bar = 1;
865 i830_fill_box(dev, 4, 16, bar, 4, 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 dev_priv->dma_used = 0;
867 }
868
869 dev_priv->sarea_priv->perf_boxes = 0;
870}
871
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000872static void i830_dma_dispatch_clear(drm_device_t * dev, int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 unsigned int clear_color,
874 unsigned int clear_zval,
875 unsigned int clear_depthmask)
876{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 drm_i830_private_t *dev_priv = dev->dev_private;
878 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int nbox = sarea_priv->nbox;
880 drm_clip_rect_t *pbox = sarea_priv->boxes;
881 int pitch = dev_priv->pitch;
882 int cpp = dev_priv->cpp;
883 int i;
884 unsigned int BR13, CMD, D_CMD;
885 RING_LOCALS;
886
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 if (dev_priv->current_page == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 unsigned int tmp = flags;
889
890 flags &= ~(I830_FRONT | I830_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 if (tmp & I830_FRONT)
892 flags |= I830_BACK;
893 if (tmp & I830_BACK)
894 flags |= I830_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 switch (cpp) {
900 case 2:
901 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 D_CMD = CMD = XY_COLOR_BLT_CMD;
903 break;
904 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24) | (1 << 25);
906 CMD = (XY_COLOR_BLT_CMD | XY_COLOR_BLT_WRITE_ALPHA |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 XY_COLOR_BLT_WRITE_RGB);
908 D_CMD = XY_COLOR_BLT_CMD;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 if (clear_depthmask & 0x00ffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 D_CMD |= XY_COLOR_BLT_WRITE_RGB;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 if (clear_depthmask & 0xff000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 D_CMD |= XY_COLOR_BLT_WRITE_ALPHA;
913 break;
914 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 D_CMD = CMD = XY_COLOR_BLT_CMD;
917 break;
918 }
919
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 if (nbox > I830_NR_SAREA_CLIPRECTS)
921 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (pbox->x1 > pbox->x2 ||
925 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 continue;
928
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 if (flags & I830_FRONT) {
930 DRM_DEBUG("clear front\n");
931 BEGIN_LP_RING(6);
932 OUT_RING(CMD);
933 OUT_RING(BR13);
934 OUT_RING((pbox->y1 << 16) | pbox->x1);
935 OUT_RING((pbox->y2 << 16) | pbox->x2);
936 OUT_RING(dev_priv->front_offset);
937 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 ADVANCE_LP_RING();
939 }
940
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000941 if (flags & I830_BACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 DRM_DEBUG("clear back\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000943 BEGIN_LP_RING(6);
944 OUT_RING(CMD);
945 OUT_RING(BR13);
946 OUT_RING((pbox->y1 << 16) | pbox->x1);
947 OUT_RING((pbox->y2 << 16) | pbox->x2);
948 OUT_RING(dev_priv->back_offset);
949 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ADVANCE_LP_RING();
951 }
952
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000953 if (flags & I830_DEPTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 DRM_DEBUG("clear depth\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 BEGIN_LP_RING(6);
956 OUT_RING(D_CMD);
957 OUT_RING(BR13);
958 OUT_RING((pbox->y1 << 16) | pbox->x1);
959 OUT_RING((pbox->y2 << 16) | pbox->x2);
960 OUT_RING(dev_priv->depth_offset);
961 OUT_RING(clear_zval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 ADVANCE_LP_RING();
963 }
964 }
965}
966
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967static void i830_dma_dispatch_swap(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000969 drm_i830_private_t *dev_priv = dev->dev_private;
970 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int nbox = sarea_priv->nbox;
972 drm_clip_rect_t *pbox = sarea_priv->boxes;
973 int pitch = dev_priv->pitch;
974 int cpp = dev_priv->cpp;
975 int i;
976 unsigned int CMD, BR13;
977 RING_LOCALS;
978
979 DRM_DEBUG("swapbuffers\n");
980
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000981 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (dev_priv->do_boxes)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 switch (cpp) {
987 case 2:
988 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 CMD = XY_SRC_COPY_BLT_CMD;
990 break;
991 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24) | (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
994 XY_SRC_COPY_BLT_WRITE_RGB);
995 break;
996 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000997 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 CMD = XY_SRC_COPY_BLT_CMD;
999 break;
1000 }
1001
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001002 if (nbox > I830_NR_SAREA_CLIPRECTS)
1003 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (pbox->x1 > pbox->x2 ||
1007 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 continue;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 DRM_DEBUG("dispatch swap %d,%d-%d,%d!\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 pbox->x1, pbox->y1, pbox->x2, pbox->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 BEGIN_LP_RING(8);
1015 OUT_RING(CMD);
1016 OUT_RING(BR13);
1017 OUT_RING((pbox->y1 << 16) | pbox->x1);
1018 OUT_RING((pbox->y2 << 16) | pbox->x2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001020 if (dev_priv->current_page == 0)
1021 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 OUT_RING((pbox->y1 << 16) | pbox->x1);
1026 OUT_RING(BR13 & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 if (dev_priv->current_page == 0)
1029 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 ADVANCE_LP_RING();
1034 }
1035}
1036
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037static void i830_dma_dispatch_flip(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001039 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 RING_LOCALS;
1041
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
1043 __FUNCTION__,
1044 dev_priv->current_page,
1045 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 if (dev_priv->do_boxes) {
1050 dev_priv->sarea_priv->perf_boxes |= I830_BOX_FLIP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001054 BEGIN_LP_RING(2);
1055 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1056 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 ADVANCE_LP_RING();
1058
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 BEGIN_LP_RING(6);
1060 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
1061 OUT_RING(0);
1062 if (dev_priv->current_page == 0) {
1063 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 dev_priv->current_page = 1;
1065 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 dev_priv->current_page = 0;
1068 }
1069 OUT_RING(0);
1070 ADVANCE_LP_RING();
1071
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 BEGIN_LP_RING(2);
1073 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
1074 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1078}
1079
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080static void i830_dma_dispatch_vertex(drm_device_t * dev,
1081 drm_buf_t * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
1086 drm_clip_rect_t *box = sarea_priv->boxes;
1087 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 unsigned long address = (unsigned long)buf->bus_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 unsigned long start = address - dev->agp->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int i = 0, u;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 if (nbox > I830_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 nbox = I830_NR_SAREA_CLIPRECTS;
1097
1098 if (discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 u = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 I830_BUF_HARDWARE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 if (u != I830_BUF_CLIENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 DRM_DEBUG("xxxx 2\n");
1103 }
1104 }
1105
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 if (used > 4 * 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 used = 0;
1108
1109 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001110 i830EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 DRM_DEBUG("dispatch vertex addr 0x%lx, used 0x%x nbox %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 address, used, nbox);
1114
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001115 dev_priv->counter++;
1116 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1117 DRM_DEBUG("i830_dma_dispatch\n");
1118 DRM_DEBUG("start : %lx\n", start);
1119 DRM_DEBUG("used : %d\n", used);
1120 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if (buf_priv->currently_mapped == I830_BUF_MAPPED) {
1123 u32 *vp = buf_priv->kernel_virtual;
1124
1125 vp[0] = (GFX_OP_PRIMITIVE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001126 sarea_priv->vertex_prim | ((used / 4) - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (dev_priv->use_mi_batchbuffer_start) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 vp[used / 4] = MI_BATCH_BUFFER_END;
1130 used += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (used & 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001134 vp[used / 4] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 used += 4;
1136 }
1137
1138 i830_unmap_buffer(buf);
1139 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (used) {
1142 do {
1143 if (i < nbox) {
1144 BEGIN_LP_RING(6);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001145 OUT_RING(GFX_OP_DRAWRECT_INFO);
1146 OUT_RING(sarea_priv->
1147 BufferState[I830_DESTREG_DR1]);
1148 OUT_RING(box[i].x1 | (box[i].y1 << 16));
1149 OUT_RING(box[i].x2 | (box[i].y2 << 16));
1150 OUT_RING(sarea_priv->
1151 BufferState[I830_DESTREG_DR4]);
1152 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 ADVANCE_LP_RING();
1154 }
1155
1156 if (dev_priv->use_mi_batchbuffer_start) {
1157 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001158 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
1159 OUT_RING(start | MI_BATCH_NON_SECURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001161 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 OUT_RING(MI_BATCH_BUFFER);
1164 OUT_RING(start | MI_BATCH_NON_SECURE);
1165 OUT_RING(start + used - 4);
1166 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ADVANCE_LP_RING();
1168 }
1169
1170 } while (++i < nbox);
1171 }
1172
1173 if (discard) {
1174 dev_priv->counter++;
1175
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001176 (void)cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
1177 I830_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 OUT_RING(CMD_STORE_DWORD_IDX);
1181 OUT_RING(20);
1182 OUT_RING(dev_priv->counter);
1183 OUT_RING(CMD_STORE_DWORD_IDX);
1184 OUT_RING(buf_priv->my_use_idx);
1185 OUT_RING(I830_BUF_FREE);
1186 OUT_RING(CMD_REPORT_HEAD);
1187 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 ADVANCE_LP_RING();
1189 }
1190}
1191
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001192static void i830_dma_quiescent(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001194 drm_i830_private_t *dev_priv = dev->dev_private;
1195 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001197 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001199 BEGIN_LP_RING(4);
1200 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1201 OUT_RING(CMD_REPORT_HEAD);
1202 OUT_RING(0);
1203 OUT_RING(0);
1204 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001206 i830_wait_ring(dev, dev_priv->ring.Size - 8, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001209static int i830_flush_queue(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001211 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001213 int i, ret = 0;
1214 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001216 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001218 BEGIN_LP_RING(2);
1219 OUT_RING(CMD_REPORT_HEAD);
1220 OUT_RING(0);
1221 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001223 i830_wait_ring(dev, dev_priv->ring.Size - 8, __FUNCTION__);
1224
1225 for (i = 0; i < dma->buf_count; i++) {
1226 drm_buf_t *buf = dma->buflist[i];
1227 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1228
1229 int used = cmpxchg(buf_priv->in_use, I830_BUF_HARDWARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 I830_BUF_FREE);
1231
1232 if (used == I830_BUF_HARDWARE)
1233 DRM_DEBUG("reclaimed from HARDWARE\n");
1234 if (used == I830_BUF_CLIENT)
1235 DRM_DEBUG("still on client\n");
1236 }
1237
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
1241/* Must be called with the lock held */
Dave Airliece60fe02006-02-02 19:21:38 +11001242static void i830_reclaim_buffers(drm_device_t * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001245 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001247 if (!dma)
1248 return;
1249 if (!dev->dev_private)
1250 return;
1251 if (!dma->buflist)
1252 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001254 i830_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 for (i = 0; i < dma->buf_count; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001257 drm_buf_t *buf = dma->buflist[i];
1258 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (buf->filp == filp && buf_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001261 int used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 I830_BUF_FREE);
1263
1264 if (used == I830_BUF_CLIENT)
1265 DRM_DEBUG("reclaimed from client\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001266 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
1267 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 }
1270}
1271
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001272static int i830_flush_ioctl(struct inode *inode, struct file *filp,
1273 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001275 drm_file_t *priv = filp->private_data;
1276 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 LOCK_TEST_WITH_RETURN(dev, filp);
1279
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001280 i830_flush_queue(dev);
1281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
1284static int i830_dma_vertex(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001285 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 drm_file_t *priv = filp->private_data;
1288 drm_device_t *dev = priv->head->dev;
1289 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001290 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1291 u32 *hw_status = dev_priv->hw_status_page;
1292 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1293 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 drm_i830_vertex_t vertex;
1295
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001296 if (copy_from_user
1297 (&vertex, (drm_i830_vertex_t __user *) arg, sizeof(vertex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EFAULT;
1299
1300 LOCK_TEST_WITH_RETURN(dev, filp);
1301
1302 DRM_DEBUG("i830 dma vertex, idx %d used %d discard %d\n",
1303 vertex.idx, vertex.used, vertex.discard);
1304
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305 if (vertex.idx < 0 || vertex.idx > dma->buf_count)
1306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001308 i830_dma_dispatch_vertex(dev,
1309 dma->buflist[vertex.idx],
1310 vertex.discard, vertex.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001312 sarea_priv->last_enqueue = dev_priv->counter - 1;
1313 sarea_priv->last_dispatch = (int)hw_status[5];
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316}
1317
1318static int i830_clear_bufs(struct inode *inode, struct file *filp,
1319 unsigned int cmd, unsigned long arg)
1320{
1321 drm_file_t *priv = filp->private_data;
1322 drm_device_t *dev = priv->head->dev;
1323 drm_i830_clear_t clear;
1324
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001325 if (copy_from_user
1326 (&clear, (drm_i830_clear_t __user *) arg, sizeof(clear)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 LOCK_TEST_WITH_RETURN(dev, filp);
1330
1331 /* GH: Someone's doing nasty things... */
1332 if (!dev->dev_private) {
1333 return -EINVAL;
1334 }
1335
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001336 i830_dma_dispatch_clear(dev, clear.flags,
1337 clear.clear_color,
1338 clear.clear_depth, clear.clear_depthmask);
1339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342static int i830_swap_bufs(struct inode *inode, struct file *filp,
1343 unsigned int cmd, unsigned long arg)
1344{
1345 drm_file_t *priv = filp->private_data;
1346 drm_device_t *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 DRM_DEBUG("i830_swap_bufs\n");
1349
1350 LOCK_TEST_WITH_RETURN(dev, filp);
1351
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001352 i830_dma_dispatch_swap(dev);
1353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001357 */
1358static void i830_do_init_pageflip(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 drm_i830_private_t *dev_priv = dev->dev_private;
1361
1362 DRM_DEBUG("%s\n", __FUNCTION__);
1363 dev_priv->page_flipping = 1;
1364 dev_priv->current_page = 0;
1365 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1366}
1367
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001368static int i830_do_cleanup_pageflip(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 drm_i830_private_t *dev_priv = dev->dev_private;
1371
1372 DRM_DEBUG("%s\n", __FUNCTION__);
1373 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001374 i830_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 dev_priv->page_flipping = 0;
1377 return 0;
1378}
1379
1380static int i830_flip_bufs(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 drm_file_t *priv = filp->private_data;
1384 drm_device_t *dev = priv->head->dev;
1385 drm_i830_private_t *dev_priv = dev->dev_private;
1386
1387 DRM_DEBUG("%s\n", __FUNCTION__);
1388
1389 LOCK_TEST_WITH_RETURN(dev, filp);
1390
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001391 if (!dev_priv->page_flipping)
1392 i830_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001394 i830_dma_dispatch_flip(dev);
1395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398static int i830_getage(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001399 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001401 drm_file_t *priv = filp->private_data;
1402 drm_device_t *dev = priv->head->dev;
1403 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1404 u32 *hw_status = dev_priv->hw_status_page;
1405 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1406 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return 0;
1410}
1411
1412static int i830_getbuf(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001413 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001415 drm_file_t *priv = filp->private_data;
1416 drm_device_t *dev = priv->head->dev;
1417 int retcode = 0;
1418 drm_i830_dma_t d;
1419 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1420 u32 *hw_status = dev_priv->hw_status_page;
1421 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1422 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 DRM_DEBUG("getbuf\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001425 if (copy_from_user(&d, (drm_i830_dma_t __user *) arg, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 LOCK_TEST_WITH_RETURN(dev, filp);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 d.granted = 0;
1431
1432 retcode = i830_dma_get_buffer(dev, &d, filp);
1433
1434 DRM_DEBUG("i830_dma: %d returning %d, granted = %d\n",
1435 current->pid, retcode, d.granted);
1436
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001437 if (copy_to_user((drm_dma_t __user *) arg, &d, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001439 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 return retcode;
1442}
1443
1444static int i830_copybuf(struct inode *inode,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001445 struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 /* Never copy - 2.4.x doesn't need it */
1448 return 0;
1449}
1450
1451static int i830_docopy(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001452 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 return 0;
1455}
1456
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001457static int i830_getparam(struct inode *inode, struct file *filp,
1458 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001460 drm_file_t *priv = filp->private_data;
1461 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 drm_i830_private_t *dev_priv = dev->dev_private;
1463 drm_i830_getparam_t param;
1464 int value;
1465
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001466 if (!dev_priv) {
1467 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return -EINVAL;
1469 }
1470
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001471 if (copy_from_user
1472 (&param, (drm_i830_getparam_t __user *) arg, sizeof(param)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -EFAULT;
1474
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001475 switch (param.param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 case I830_PARAM_IRQ_ACTIVE:
1477 value = dev->irq_enabled;
1478 break;
1479 default:
1480 return -EINVAL;
1481 }
1482
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001483 if (copy_to_user(param.value, &value, sizeof(int))) {
1484 DRM_ERROR("copy_to_user\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return -EFAULT;
1486 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489}
1490
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001491static int i830_setparam(struct inode *inode, struct file *filp,
1492 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 drm_file_t *priv = filp->private_data;
1495 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 drm_i830_private_t *dev_priv = dev->dev_private;
1497 drm_i830_setparam_t param;
1498
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001499 if (!dev_priv) {
1500 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EINVAL;
1502 }
1503
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001504 if (copy_from_user
1505 (&param, (drm_i830_setparam_t __user *) arg, sizeof(param)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EFAULT;
1507
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001508 switch (param.param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 case I830_SETPARAM_USE_MI_BATCHBUFFER_START:
1510 dev_priv->use_mi_batchbuffer_start = param.value;
1511 break;
1512 default:
1513 return -EINVAL;
1514 }
1515
1516 return 0;
1517}
1518
Dave Airlie22eae942005-11-10 22:16:34 +11001519int i830_driver_load(drm_device_t *dev, unsigned long flags)
1520{
1521 /* i830 has 4 more counters */
1522 dev->counters += 4;
1523 dev->types[6] = _DRM_STAT_IRQ;
1524 dev->types[7] = _DRM_STAT_PRIMARY;
1525 dev->types[8] = _DRM_STAT_SECONDARY;
1526 dev->types[9] = _DRM_STAT_DMA;
1527
1528 return 0;
1529}
1530
1531void i830_driver_lastclose(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001533 i830_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Dave Airlie22eae942005-11-10 22:16:34 +11001536void i830_driver_preclose(drm_device_t * dev, DRMFILE filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 if (dev->dev_private) {
1539 drm_i830_private_t *dev_priv = dev->dev_private;
1540 if (dev_priv->page_flipping) {
1541 i830_do_cleanup_pageflip(dev);
1542 }
1543 }
1544}
1545
Dave Airlie22eae942005-11-10 22:16:34 +11001546void i830_driver_reclaim_buffers_locked(drm_device_t * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 i830_reclaim_buffers(dev, filp);
1549}
1550
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001551int i830_driver_dma_quiescent(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001553 i830_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
1555}
1556
1557drm_ioctl_desc_t i830_ioctls[] = {
Dave Airliea7a2cc32006-01-02 13:54:04 +11001558 [DRM_IOCTL_NR(DRM_I830_INIT)] = {i830_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1559 [DRM_IOCTL_NR(DRM_I830_VERTEX)] = {i830_dma_vertex, DRM_AUTH},
1560 [DRM_IOCTL_NR(DRM_I830_CLEAR)] = {i830_clear_bufs, DRM_AUTH},
1561 [DRM_IOCTL_NR(DRM_I830_FLUSH)] = {i830_flush_ioctl, DRM_AUTH},
1562 [DRM_IOCTL_NR(DRM_I830_GETAGE)] = {i830_getage, DRM_AUTH},
1563 [DRM_IOCTL_NR(DRM_I830_GETBUF)] = {i830_getbuf, DRM_AUTH},
1564 [DRM_IOCTL_NR(DRM_I830_SWAP)] = {i830_swap_bufs, DRM_AUTH},
1565 [DRM_IOCTL_NR(DRM_I830_COPY)] = {i830_copybuf, DRM_AUTH},
1566 [DRM_IOCTL_NR(DRM_I830_DOCOPY)] = {i830_docopy, DRM_AUTH},
1567 [DRM_IOCTL_NR(DRM_I830_FLIP)] = {i830_flip_bufs, DRM_AUTH},
1568 [DRM_IOCTL_NR(DRM_I830_IRQ_EMIT)] = {i830_irq_emit, DRM_AUTH},
1569 [DRM_IOCTL_NR(DRM_I830_IRQ_WAIT)] = {i830_irq_wait, DRM_AUTH},
1570 [DRM_IOCTL_NR(DRM_I830_GETPARAM)] = {i830_getparam, DRM_AUTH},
1571 [DRM_IOCTL_NR(DRM_I830_SETPARAM)] = {i830_setparam, DRM_AUTH}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572};
1573
1574int i830_max_ioctl = DRM_ARRAY_SIZE(i830_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001575
1576/**
1577 * Determine if the device really is AGP or not.
1578 *
1579 * All Intel graphics chipsets are treated as AGP, even if they are really
1580 * PCI-e.
1581 *
1582 * \param dev The device to be tested.
1583 *
1584 * \returns
1585 * A value of 1 is always retured to indictate every i8xx is AGP.
1586 */
1587int i830_driver_device_is_agp(drm_device_t * dev)
1588{
1589 return 1;
1590}