blob: 95224455ec0cf55078d2598ace38b5730719faff [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 Airlie3d774612006-08-07 20:07:43 +1000111 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112 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");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000149 retcode = PTR_ERR((void *)virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 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) {
Dave Airlieb9094d32007-01-08 21:31:13 +1100226 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 if (dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 pci_free_consistent(dev->pdev, PAGE_SIZE,
230 dev_priv->hw_status_page,
231 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232 /* Need to rewrite hardware status page */
233 I830_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000236 drm_free(dev->dev_private, sizeof(drm_i830_private_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 DRM_MEM_DRIVER);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 for (i = 0; i < dma->buf_count; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 drm_buf_t *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100244 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250int i830_wait_ring(drm_device_t * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000252 drm_i830_private_t *dev_priv = dev->dev_private;
253 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
254 int iters = 0;
255 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 unsigned int last_head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
257
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 end = jiffies + (HZ * 3);
259 while (ring->space < n) {
260 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
261 ring->space = ring->head - (ring->tail + 8);
262 if (ring->space < 0)
263 ring->space += ring->Size;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (ring->head != last_head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266 end = jiffies + (HZ * 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 last_head = ring->head;
268 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000269
270 iters++;
271 if (time_before(end, jiffies)) {
272 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
273 DRM_ERROR("lockup\n");
274 goto out_wait_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 udelay(1);
277 dev_priv->sarea_priv->perf_boxes |= I830_BOX_WAIT;
278 }
279
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280 out_wait_ring:
281 return iters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284static void i830_kernel_lost_context(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286 drm_i830_private_t *dev_priv = dev->dev_private;
287 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
288
289 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
290 ring->tail = I830_READ(LP_RING + RING_TAIL) & TAIL_ADDR;
291 ring->space = ring->head - (ring->tail + 8);
292 if (ring->space < 0)
293 ring->space += ring->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (ring->head == ring->tail)
296 dev_priv->sarea_priv->perf_boxes |= I830_BOX_RING_EMPTY;
297}
298
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299static int i830_freelist_init(drm_device_t * dev, drm_i830_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000301 drm_device_dma_t *dma = dev->dma;
302 int my_idx = 36;
303 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
304 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000306 if (dma->buf_count > 1019) {
307 /* Not enough space in the status page for the freelist */
308 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000311 for (i = 0; i < dma->buf_count; i++) {
312 drm_buf_t *buf = dma->buflist[i];
313 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000315 buf_priv->in_use = hw_status++;
316 buf_priv->my_use_idx = my_idx;
317 my_idx += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000319 *buf_priv->in_use = I830_BUF_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Dave Airlieb9094d32007-01-08 21:31:13 +1100321 buf_priv->map.offset = buf->bus_address;
322 buf_priv->map.size = buf->total;
323 buf_priv->map.type = _DRM_AGP;
324 buf_priv->map.flags = 0;
325 buf_priv->map.mtrr = 0;
326
327 drm_core_ioremap(&buf_priv->map, dev);
328 buf_priv->kernel_virtual = buf_priv->map.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 return 0;
331}
332
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333static int i830_dma_initialize(drm_device_t * dev,
334 drm_i830_private_t * dev_priv,
335 drm_i830_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct list_head *list;
338
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 memset(dev_priv, 0, sizeof(drm_i830_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 list_for_each(list, &dev->maplist->head) {
342 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 if (r_list->map &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000345 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 break;
348 }
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000351 if (!dev_priv->sarea_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 dev->dev_private = (void *)dev_priv;
353 i830_dma_cleanup(dev);
354 DRM_ERROR("can not find sarea!\n");
355 return -EINVAL;
356 }
357 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 if (!dev_priv->mmio_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dev->dev_private = (void *)dev_priv;
360 i830_dma_cleanup(dev);
361 DRM_ERROR("can not find mmio map!\n");
362 return -EINVAL;
363 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000364 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000366 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 dev->dev_private = (void *)dev_priv;
368 i830_dma_cleanup(dev);
369 DRM_ERROR("can not find dma buffer map!\n");
370 return -EINVAL;
371 }
372
373 dev_priv->sarea_priv = (drm_i830_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000374 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 dev_priv->ring.Start = init->ring_start;
377 dev_priv->ring.End = init->ring_end;
378 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dave Airlieb9094d32007-01-08 21:31:13 +1100380 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
381 dev_priv->ring.map.size = init->ring_size;
382 dev_priv->ring.map.type = _DRM_AGP;
383 dev_priv->ring.map.flags = 0;
384 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dave Airlieb9094d32007-01-08 21:31:13 +1100386 drm_core_ioremap(&dev_priv->ring.map, dev);
387
388 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000389 dev->dev_private = (void *)dev_priv;
390 i830_dma_cleanup(dev);
391 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 " ring buffer\n");
Dave Airlieb9094d32007-01-08 21:31:13 +1100393 return DRM_ERR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
Dave Airlieb9094d32007-01-08 21:31:13 +1100396 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
397
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000398 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dev_priv->w = init->w;
401 dev_priv->h = init->h;
402 dev_priv->pitch = init->pitch;
403 dev_priv->back_offset = init->back_offset;
404 dev_priv->depth_offset = init->depth_offset;
405 dev_priv->front_offset = init->front_offset;
406
407 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
408 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
409 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
410
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 DRM_DEBUG("front_di1 %x\n", dev_priv->front_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 DRM_DEBUG("back_offset %x\n", dev_priv->back_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000413 DRM_DEBUG("back_di1 %x\n", dev_priv->back_di1);
414 DRM_DEBUG("pitch_bits %x\n", init->pitch_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 dev_priv->cpp = init->cpp;
417 /* We are using separate values as placeholders for mechanisms for
418 * private backbuffer/depthbuffer usage.
419 */
420
421 dev_priv->back_pitch = init->back_pitch;
422 dev_priv->depth_pitch = init->depth_pitch;
423 dev_priv->do_boxes = 0;
424 dev_priv->use_mi_batchbuffer_start = 0;
425
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426 /* Program Hardware Status Page */
427 dev_priv->hw_status_page =
428 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
429 &dev_priv->dma_status_page);
430 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 dev->dev_private = (void *)dev_priv;
432 i830_dma_cleanup(dev);
433 DRM_ERROR("Can not allocate hardware status page\n");
434 return -ENOMEM;
435 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000436 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438
439 I830_WRITE(0x02080, dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 DRM_DEBUG("Enabled hardware status page\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441
442 /* Now we need to init our freelist */
443 if (i830_freelist_init(dev, dev_priv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000445 i830_dma_cleanup(dev);
446 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450 dev->dev_private = (void *)dev_priv;
451
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455static int i830_dma_init(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 drm_file_t *priv = filp->private_data;
459 drm_device_t *dev = priv->head->dev;
460 drm_i830_private_t *dev_priv;
461 drm_i830_init_t init;
462 int retcode = 0;
463
464 if (copy_from_user(&init, (void *__user)arg, sizeof(init)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466
467 switch (init.func) {
468 case I830_INIT_DMA:
469 dev_priv = drm_alloc(sizeof(drm_i830_private_t),
470 DRM_MEM_DRIVER);
471 if (dev_priv == NULL)
472 return -ENOMEM;
473 retcode = i830_dma_initialize(dev, dev_priv, &init);
474 break;
475 case I830_CLEANUP_DMA:
476 retcode = i830_dma_cleanup(dev);
477 break;
478 default:
479 retcode = -EINVAL;
480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482
483 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486#define GFX_OP_STIPPLE ((0x3<<29)|(0x1d<<24)|(0x83<<16))
487#define ST1_ENABLE (1<<16)
488#define ST1_MASK (0xffff)
489
490/* Most efficient way to verify state for the i830 is as it is
491 * emitted. Non-conformant state is silently dropped.
492 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493static void i830EmitContextVerified(drm_device_t * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000495 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int i, j = 0;
497 unsigned int tmp;
498 RING_LOCALS;
499
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000500 BEGIN_LP_RING(I830_CTX_SETUP_SIZE + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000502 for (i = 0; i < I830_CTXREG_BLENDCOLR0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504 if ((tmp & (7 << 29)) == CMD_3D &&
505 (tmp & (0x1f << 24)) < (0x1d << 24)) {
506 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 j++;
508 } else {
509 DRM_ERROR("Skipping %d\n", i);
510 }
511 }
512
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000513 OUT_RING(STATE3D_CONST_BLEND_COLOR_CMD);
514 OUT_RING(code[I830_CTXREG_BLENDCOLR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 j += 2;
516
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000517 for (i = I830_CTXREG_VF; i < I830_CTXREG_MCSB0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 if ((tmp & (7 << 29)) == CMD_3D &&
520 (tmp & (0x1f << 24)) < (0x1d << 24)) {
521 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 j++;
523 } else {
524 DRM_ERROR("Skipping %d\n", i);
525 }
526 }
527
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000528 OUT_RING(STATE3D_MAP_COORD_SETBIND_CMD);
529 OUT_RING(code[I830_CTXREG_MCSB1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 j += 2;
531
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 if (j & 1)
533 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 ADVANCE_LP_RING();
536}
537
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538static void i830EmitTexVerified(drm_device_t * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int i, j = 0;
542 unsigned int tmp;
543 RING_LOCALS;
544
545 if (code[I830_TEXREG_MI0] == GFX_OP_MAP_INFO ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 (code[I830_TEXREG_MI0] & ~(0xf * LOAD_TEXTURE_MAP0)) ==
547 (STATE3D_LOAD_STATE_IMMEDIATE_2 | 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 BEGIN_LP_RING(I830_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 OUT_RING(code[I830_TEXREG_MI0]); /* TM0LI */
552 OUT_RING(code[I830_TEXREG_MI1]); /* TM0S0 */
553 OUT_RING(code[I830_TEXREG_MI2]); /* TM0S1 */
554 OUT_RING(code[I830_TEXREG_MI3]); /* TM0S2 */
555 OUT_RING(code[I830_TEXREG_MI4]); /* TM0S3 */
556 OUT_RING(code[I830_TEXREG_MI5]); /* TM0S4 */
557
558 for (i = 6; i < I830_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000564 if (j & 1)
565 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 printk("rejected packet %x\n", code[0]);
570}
571
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572static void i830EmitTexBlendVerified(drm_device_t * dev,
573 unsigned int *code, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int i, j = 0;
577 unsigned int tmp;
578 RING_LOCALS;
579
580 if (!num)
581 return;
582
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 BEGIN_LP_RING(num + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000585 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 j++;
589 }
590
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 if (j & 1)
592 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 ADVANCE_LP_RING();
595}
596
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597static void i830EmitTexPalette(drm_device_t * dev,
598 unsigned int *palette, int number, int is_shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000600 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int i;
602 RING_LOCALS;
603
604 return;
605
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 BEGIN_LP_RING(258);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 if (is_shared == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 OUT_RING(CMD_OP_MAP_PALETTE_LOAD |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000610 MAP_PALETTE_NUM(0) | MAP_PALETTE_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 } else {
612 OUT_RING(CMD_OP_MAP_PALETTE_LOAD | MAP_PALETTE_NUM(number));
613 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 for (i = 0; i < 256; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 OUT_RING(palette[i]);
616 }
617 OUT_RING(0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000618 /* KW: WHERE IS THE ADVANCE_LP_RING? This is effectively a noop!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
620}
621
622/* Need to do some additional checking when setting the dest buffer.
623 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624static void i830EmitDestVerified(drm_device_t * dev, unsigned int *code)
625{
626 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 unsigned int tmp;
628 RING_LOCALS;
629
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000630 BEGIN_LP_RING(I830_DEST_SETUP_SIZE + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 tmp = code[I830_DESTREG_CBUFADDR];
633 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
634 if (((int)outring) & 8) {
635 OUT_RING(0);
636 OUT_RING(0);
637 }
638
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000639 OUT_RING(CMD_OP_DESTBUFFER_INFO);
640 OUT_RING(BUF_3D_ID_COLOR_BACK |
641 BUF_3D_PITCH(dev_priv->back_pitch * dev_priv->cpp) |
642 BUF_3D_USE_FENCE);
643 OUT_RING(tmp);
644 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 OUT_RING(CMD_OP_DESTBUFFER_INFO);
647 OUT_RING(BUF_3D_ID_DEPTH | BUF_3D_USE_FENCE |
648 BUF_3D_PITCH(dev_priv->depth_pitch * dev_priv->cpp));
649 OUT_RING(dev_priv->zi1);
650 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 } else {
652 DRM_ERROR("bad di1 %x (allow %x or %x)\n",
653 tmp, dev_priv->front_di1, dev_priv->back_di1);
654 }
655
656 /* invarient:
657 */
658
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000659 OUT_RING(GFX_OP_DESTBUFFER_VARS);
660 OUT_RING(code[I830_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000662 OUT_RING(GFX_OP_DRAWRECT_INFO);
663 OUT_RING(code[I830_DESTREG_DR1]);
664 OUT_RING(code[I830_DESTREG_DR2]);
665 OUT_RING(code[I830_DESTREG_DR3]);
666 OUT_RING(code[I830_DESTREG_DR4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* Need to verify this */
669 tmp = code[I830_DESTREG_SENABLE];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670 if ((tmp & ~0x3) == GFX_OP_SCISSOR_ENABLE) {
671 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 } else {
673 DRM_ERROR("bad scissor enable\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000674 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 OUT_RING(GFX_OP_SCISSOR_RECT);
678 OUT_RING(code[I830_DESTREG_SR1]);
679 OUT_RING(code[I830_DESTREG_SR2]);
680 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 ADVANCE_LP_RING();
683}
684
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685static void i830EmitStippleVerified(drm_device_t * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 RING_LOCALS;
689
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 BEGIN_LP_RING(2);
691 OUT_RING(GFX_OP_STIPPLE);
692 OUT_RING(code[1]);
693 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696static void i830EmitState(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000698 drm_i830_private_t *dev_priv = dev->dev_private;
699 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 unsigned int dirty = sarea_priv->dirty;
701
702 DRM_DEBUG("%s %x\n", __FUNCTION__, dirty);
703
704 if (dirty & I830_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 i830EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 sarea_priv->dirty &= ~I830_UPLOAD_BUFFERS;
707 }
708
709 if (dirty & I830_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 i830EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 sarea_priv->dirty &= ~I830_UPLOAD_CTX;
712 }
713
714 if (dirty & I830_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 i830EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 sarea_priv->dirty &= ~I830_UPLOAD_TEX0;
717 }
718
719 if (dirty & I830_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 i830EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 sarea_priv->dirty &= ~I830_UPLOAD_TEX1;
722 }
723
724 if (dirty & I830_UPLOAD_TEXBLEND0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[0],
726 sarea_priv->TexBlendStateWordsUsed[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND0;
728 }
729
730 if (dirty & I830_UPLOAD_TEXBLEND1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[1],
732 sarea_priv->TexBlendStateWordsUsed[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND1;
734 }
735
736 if (dirty & I830_UPLOAD_TEX_PALETTE_SHARED) {
737 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 1);
738 } else {
739 if (dirty & I830_UPLOAD_TEX_PALETTE_N(0)) {
740 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 0);
741 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(0);
742 }
743 if (dirty & I830_UPLOAD_TEX_PALETTE_N(1)) {
744 i830EmitTexPalette(dev, sarea_priv->Palette[1], 1, 0);
745 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(1);
746 }
747
748 /* 1.3:
749 */
750#if 0
751 if (dirty & I830_UPLOAD_TEX_PALETTE_N(2)) {
752 i830EmitTexPalette(dev, sarea_priv->Palette2[0], 0, 0);
753 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
754 }
755 if (dirty & I830_UPLOAD_TEX_PALETTE_N(3)) {
756 i830EmitTexPalette(dev, sarea_priv->Palette2[1], 1, 0);
757 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
758 }
759#endif
760 }
761
762 /* 1.3:
763 */
764 if (dirty & I830_UPLOAD_STIPPLE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 i830EmitStippleVerified(dev, sarea_priv->StippleState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 sarea_priv->dirty &= ~I830_UPLOAD_STIPPLE;
767 }
768
769 if (dirty & I830_UPLOAD_TEX2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 i830EmitTexVerified(dev, sarea_priv->TexState2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 sarea_priv->dirty &= ~I830_UPLOAD_TEX2;
772 }
773
774 if (dirty & I830_UPLOAD_TEX3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 i830EmitTexVerified(dev, sarea_priv->TexState3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 sarea_priv->dirty &= ~I830_UPLOAD_TEX3;
777 }
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (dirty & I830_UPLOAD_TEXBLEND2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 i830EmitTexBlendVerified(dev,
781 sarea_priv->TexBlendState2,
782 sarea_priv->TexBlendStateWordsUsed2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND2;
785 }
786
787 if (dirty & I830_UPLOAD_TEXBLEND3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 i830EmitTexBlendVerified(dev,
789 sarea_priv->TexBlendState3,
790 sarea_priv->TexBlendStateWordsUsed3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND3;
792 }
793}
794
795/* ================================================================
796 * Performance monitoring functions
797 */
798
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799static void i830_fill_box(drm_device_t * dev,
800 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 u32 color;
804 unsigned int BR13, CMD;
805 RING_LOCALS;
806
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 BR13 = (0xF0 << 16) | (dev_priv->pitch * dev_priv->cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 CMD = XY_COLOR_BLT_CMD;
809 x += dev_priv->sarea_priv->boxes[0].x1;
810 y += dev_priv->sarea_priv->boxes[0].y1;
811
812 if (dev_priv->cpp == 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 BR13 |= (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 CMD |= (XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 } else {
817 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 BEGIN_LP_RING(6);
822 OUT_RING(CMD);
823 OUT_RING(BR13);
824 OUT_RING((y << 16) | x);
825 OUT_RING(((y + h) << 16) | (x + w));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827 if (dev_priv->current_page == 1) {
828 OUT_RING(dev_priv->front_offset);
829 } else {
830 OUT_RING(dev_priv->back_offset);
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 ADVANCE_LP_RING();
835}
836
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837static void i830_cp_performance_boxes(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* Purple box for page flipping
842 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_FLIP)
844 i830_fill_box(dev, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* Red box if we have to wait for idle at any point
847 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_WAIT)
849 i830_fill_box(dev, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* Blue box: lost context?
852 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_LOST_CONTEXT)
854 i830_fill_box(dev, 28, 4, 8, 8, 0, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /* Yellow box for texture swaps
857 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_TEXTURE_LOAD)
859 i830_fill_box(dev, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /* Green box if hardware never idles (as far as we can tell)
862 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 if (!(dev_priv->sarea_priv->perf_boxes & I830_BOX_RING_EMPTY))
864 i830_fill_box(dev, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 * (not a great measure, easily confused)
868 */
869 if (dev_priv->dma_used) {
870 int bar = dev_priv->dma_used / 10240;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 if (bar > 100)
872 bar = 100;
873 if (bar < 1)
874 bar = 1;
875 i830_fill_box(dev, 4, 16, bar, 4, 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 dev_priv->dma_used = 0;
877 }
878
879 dev_priv->sarea_priv->perf_boxes = 0;
880}
881
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882static void i830_dma_dispatch_clear(drm_device_t * dev, int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 unsigned int clear_color,
884 unsigned int clear_zval,
885 unsigned int clear_depthmask)
886{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 drm_i830_private_t *dev_priv = dev->dev_private;
888 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int nbox = sarea_priv->nbox;
890 drm_clip_rect_t *pbox = sarea_priv->boxes;
891 int pitch = dev_priv->pitch;
892 int cpp = dev_priv->cpp;
893 int i;
894 unsigned int BR13, CMD, D_CMD;
895 RING_LOCALS;
896
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 if (dev_priv->current_page == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 unsigned int tmp = flags;
899
900 flags &= ~(I830_FRONT | I830_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 if (tmp & I830_FRONT)
902 flags |= I830_BACK;
903 if (tmp & I830_BACK)
904 flags |= I830_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 switch (cpp) {
910 case 2:
911 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 D_CMD = CMD = XY_COLOR_BLT_CMD;
913 break;
914 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24) | (1 << 25);
916 CMD = (XY_COLOR_BLT_CMD | XY_COLOR_BLT_WRITE_ALPHA |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 XY_COLOR_BLT_WRITE_RGB);
918 D_CMD = XY_COLOR_BLT_CMD;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 if (clear_depthmask & 0x00ffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 D_CMD |= XY_COLOR_BLT_WRITE_RGB;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 if (clear_depthmask & 0xff000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 D_CMD |= XY_COLOR_BLT_WRITE_ALPHA;
923 break;
924 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 D_CMD = CMD = XY_COLOR_BLT_CMD;
927 break;
928 }
929
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 if (nbox > I830_NR_SAREA_CLIPRECTS)
931 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000933 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (pbox->x1 > pbox->x2 ||
935 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 continue;
938
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 if (flags & I830_FRONT) {
940 DRM_DEBUG("clear front\n");
941 BEGIN_LP_RING(6);
942 OUT_RING(CMD);
943 OUT_RING(BR13);
944 OUT_RING((pbox->y1 << 16) | pbox->x1);
945 OUT_RING((pbox->y2 << 16) | pbox->x2);
946 OUT_RING(dev_priv->front_offset);
947 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ADVANCE_LP_RING();
949 }
950
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 if (flags & I830_BACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 DRM_DEBUG("clear back\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000953 BEGIN_LP_RING(6);
954 OUT_RING(CMD);
955 OUT_RING(BR13);
956 OUT_RING((pbox->y1 << 16) | pbox->x1);
957 OUT_RING((pbox->y2 << 16) | pbox->x2);
958 OUT_RING(dev_priv->back_offset);
959 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 ADVANCE_LP_RING();
961 }
962
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 if (flags & I830_DEPTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 DRM_DEBUG("clear depth\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000965 BEGIN_LP_RING(6);
966 OUT_RING(D_CMD);
967 OUT_RING(BR13);
968 OUT_RING((pbox->y1 << 16) | pbox->x1);
969 OUT_RING((pbox->y2 << 16) | pbox->x2);
970 OUT_RING(dev_priv->depth_offset);
971 OUT_RING(clear_zval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ADVANCE_LP_RING();
973 }
974 }
975}
976
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000977static void i830_dma_dispatch_swap(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 drm_i830_private_t *dev_priv = dev->dev_private;
980 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 int nbox = sarea_priv->nbox;
982 drm_clip_rect_t *pbox = sarea_priv->boxes;
983 int pitch = dev_priv->pitch;
984 int cpp = dev_priv->cpp;
985 int i;
986 unsigned int CMD, BR13;
987 RING_LOCALS;
988
989 DRM_DEBUG("swapbuffers\n");
990
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000991 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (dev_priv->do_boxes)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000994 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 switch (cpp) {
997 case 2:
998 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 CMD = XY_SRC_COPY_BLT_CMD;
1000 break;
1001 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001002 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24) | (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
1004 XY_SRC_COPY_BLT_WRITE_RGB);
1005 break;
1006 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 CMD = XY_SRC_COPY_BLT_CMD;
1009 break;
1010 }
1011
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 if (nbox > I830_NR_SAREA_CLIPRECTS)
1013 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001015 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (pbox->x1 > pbox->x2 ||
1017 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 continue;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 DRM_DEBUG("dispatch swap %d,%d-%d,%d!\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001022 pbox->x1, pbox->y1, pbox->x2, pbox->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 BEGIN_LP_RING(8);
1025 OUT_RING(CMD);
1026 OUT_RING(BR13);
1027 OUT_RING((pbox->y1 << 16) | pbox->x1);
1028 OUT_RING((pbox->y2 << 16) | pbox->x2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 if (dev_priv->current_page == 0)
1031 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001035 OUT_RING((pbox->y1 << 16) | pbox->x1);
1036 OUT_RING(BR13 & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 if (dev_priv->current_page == 0)
1039 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 ADVANCE_LP_RING();
1044 }
1045}
1046
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047static void i830_dma_dispatch_flip(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 RING_LOCALS;
1051
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
1053 __FUNCTION__,
1054 dev_priv->current_page,
1055 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 if (dev_priv->do_boxes) {
1060 dev_priv->sarea_priv->perf_boxes |= I830_BOX_FLIP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001064 BEGIN_LP_RING(2);
1065 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1066 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 ADVANCE_LP_RING();
1068
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 BEGIN_LP_RING(6);
1070 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
1071 OUT_RING(0);
1072 if (dev_priv->current_page == 0) {
1073 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 dev_priv->current_page = 1;
1075 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 dev_priv->current_page = 0;
1078 }
1079 OUT_RING(0);
1080 ADVANCE_LP_RING();
1081
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 BEGIN_LP_RING(2);
1083 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
1084 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1088}
1089
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090static void i830_dma_dispatch_vertex(drm_device_t * dev,
1091 drm_buf_t * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
1096 drm_clip_rect_t *box = sarea_priv->boxes;
1097 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 unsigned long address = (unsigned long)buf->bus_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 unsigned long start = address - dev->agp->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int i = 0, u;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 if (nbox > I830_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 nbox = I830_NR_SAREA_CLIPRECTS;
1107
1108 if (discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 u = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 I830_BUF_HARDWARE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001111 if (u != I830_BUF_CLIENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 DRM_DEBUG("xxxx 2\n");
1113 }
1114 }
1115
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 if (used > 4 * 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 used = 0;
1118
1119 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 i830EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001122 DRM_DEBUG("dispatch vertex addr 0x%lx, used 0x%x nbox %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 address, used, nbox);
1124
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001125 dev_priv->counter++;
1126 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1127 DRM_DEBUG("i830_dma_dispatch\n");
1128 DRM_DEBUG("start : %lx\n", start);
1129 DRM_DEBUG("used : %d\n", used);
1130 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 if (buf_priv->currently_mapped == I830_BUF_MAPPED) {
1133 u32 *vp = buf_priv->kernel_virtual;
1134
1135 vp[0] = (GFX_OP_PRIMITIVE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001136 sarea_priv->vertex_prim | ((used / 4) - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (dev_priv->use_mi_batchbuffer_start) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001139 vp[used / 4] = MI_BATCH_BUFFER_END;
1140 used += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (used & 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001144 vp[used / 4] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 used += 4;
1146 }
1147
1148 i830_unmap_buffer(buf);
1149 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (used) {
1152 do {
1153 if (i < nbox) {
1154 BEGIN_LP_RING(6);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001155 OUT_RING(GFX_OP_DRAWRECT_INFO);
1156 OUT_RING(sarea_priv->
1157 BufferState[I830_DESTREG_DR1]);
1158 OUT_RING(box[i].x1 | (box[i].y1 << 16));
1159 OUT_RING(box[i].x2 | (box[i].y2 << 16));
1160 OUT_RING(sarea_priv->
1161 BufferState[I830_DESTREG_DR4]);
1162 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 ADVANCE_LP_RING();
1164 }
1165
1166 if (dev_priv->use_mi_batchbuffer_start) {
1167 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001168 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
1169 OUT_RING(start | MI_BATCH_NON_SECURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001173 OUT_RING(MI_BATCH_BUFFER);
1174 OUT_RING(start | MI_BATCH_NON_SECURE);
1175 OUT_RING(start + used - 4);
1176 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 ADVANCE_LP_RING();
1178 }
1179
1180 } while (++i < nbox);
1181 }
1182
1183 if (discard) {
1184 dev_priv->counter++;
1185
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001186 (void)cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
1187 I830_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001190 OUT_RING(CMD_STORE_DWORD_IDX);
1191 OUT_RING(20);
1192 OUT_RING(dev_priv->counter);
1193 OUT_RING(CMD_STORE_DWORD_IDX);
1194 OUT_RING(buf_priv->my_use_idx);
1195 OUT_RING(I830_BUF_FREE);
1196 OUT_RING(CMD_REPORT_HEAD);
1197 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ADVANCE_LP_RING();
1199 }
1200}
1201
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001202static void i830_dma_quiescent(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001204 drm_i830_private_t *dev_priv = dev->dev_private;
1205 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001207 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001209 BEGIN_LP_RING(4);
1210 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1211 OUT_RING(CMD_REPORT_HEAD);
1212 OUT_RING(0);
1213 OUT_RING(0);
1214 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001216 i830_wait_ring(dev, dev_priv->ring.Size - 8, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001219static int i830_flush_queue(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001221 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001223 int i, ret = 0;
1224 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001226 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001228 BEGIN_LP_RING(2);
1229 OUT_RING(CMD_REPORT_HEAD);
1230 OUT_RING(0);
1231 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001233 i830_wait_ring(dev, dev_priv->ring.Size - 8, __FUNCTION__);
1234
1235 for (i = 0; i < dma->buf_count; i++) {
1236 drm_buf_t *buf = dma->buflist[i];
1237 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1238
1239 int used = cmpxchg(buf_priv->in_use, I830_BUF_HARDWARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 I830_BUF_FREE);
1241
1242 if (used == I830_BUF_HARDWARE)
1243 DRM_DEBUG("reclaimed from HARDWARE\n");
1244 if (used == I830_BUF_CLIENT)
1245 DRM_DEBUG("still on client\n");
1246 }
1247
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/* Must be called with the lock held */
Dave Airliece60fe02006-02-02 19:21:38 +11001252static void i830_reclaim_buffers(drm_device_t * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001255 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001257 if (!dma)
1258 return;
1259 if (!dev->dev_private)
1260 return;
1261 if (!dma->buflist)
1262 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001264 i830_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 for (i = 0; i < dma->buf_count; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001267 drm_buf_t *buf = dma->buflist[i];
1268 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (buf->filp == filp && buf_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001271 int used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 I830_BUF_FREE);
1273
1274 if (used == I830_BUF_CLIENT)
1275 DRM_DEBUG("reclaimed from client\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001276 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
1277 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279 }
1280}
1281
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001282static int i830_flush_ioctl(struct inode *inode, struct file *filp,
1283 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001285 drm_file_t *priv = filp->private_data;
1286 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 LOCK_TEST_WITH_RETURN(dev, filp);
1289
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001290 i830_flush_queue(dev);
1291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294static int i830_dma_vertex(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001295 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 drm_file_t *priv = filp->private_data;
1298 drm_device_t *dev = priv->head->dev;
1299 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001300 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1301 u32 *hw_status = dev_priv->hw_status_page;
1302 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1303 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 drm_i830_vertex_t vertex;
1305
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001306 if (copy_from_user
1307 (&vertex, (drm_i830_vertex_t __user *) arg, sizeof(vertex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EFAULT;
1309
1310 LOCK_TEST_WITH_RETURN(dev, filp);
1311
1312 DRM_DEBUG("i830 dma vertex, idx %d used %d discard %d\n",
1313 vertex.idx, vertex.used, vertex.discard);
1314
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001315 if (vertex.idx < 0 || vertex.idx > dma->buf_count)
1316 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001318 i830_dma_dispatch_vertex(dev,
1319 dma->buflist[vertex.idx],
1320 vertex.discard, vertex.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001322 sarea_priv->last_enqueue = dev_priv->counter - 1;
1323 sarea_priv->last_dispatch = (int)hw_status[5];
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326}
1327
1328static int i830_clear_bufs(struct inode *inode, struct file *filp,
1329 unsigned int cmd, unsigned long arg)
1330{
1331 drm_file_t *priv = filp->private_data;
1332 drm_device_t *dev = priv->head->dev;
1333 drm_i830_clear_t clear;
1334
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001335 if (copy_from_user
1336 (&clear, (drm_i830_clear_t __user *) arg, sizeof(clear)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 LOCK_TEST_WITH_RETURN(dev, filp);
1340
1341 /* GH: Someone's doing nasty things... */
1342 if (!dev->dev_private) {
1343 return -EINVAL;
1344 }
1345
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001346 i830_dma_dispatch_clear(dev, clear.flags,
1347 clear.clear_color,
1348 clear.clear_depth, clear.clear_depthmask);
1349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
1352static int i830_swap_bufs(struct inode *inode, struct file *filp,
1353 unsigned int cmd, unsigned long arg)
1354{
1355 drm_file_t *priv = filp->private_data;
1356 drm_device_t *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 DRM_DEBUG("i830_swap_bufs\n");
1359
1360 LOCK_TEST_WITH_RETURN(dev, filp);
1361
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001362 i830_dma_dispatch_swap(dev);
1363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001367 */
1368static void i830_do_init_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 dev_priv->page_flipping = 1;
1374 dev_priv->current_page = 0;
1375 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1376}
1377
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001378static int i830_do_cleanup_pageflip(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 drm_i830_private_t *dev_priv = dev->dev_private;
1381
1382 DRM_DEBUG("%s\n", __FUNCTION__);
1383 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001384 i830_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 dev_priv->page_flipping = 0;
1387 return 0;
1388}
1389
1390static int i830_flip_bufs(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001391 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 drm_file_t *priv = filp->private_data;
1394 drm_device_t *dev = priv->head->dev;
1395 drm_i830_private_t *dev_priv = dev->dev_private;
1396
1397 DRM_DEBUG("%s\n", __FUNCTION__);
1398
1399 LOCK_TEST_WITH_RETURN(dev, filp);
1400
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001401 if (!dev_priv->page_flipping)
1402 i830_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001404 i830_dma_dispatch_flip(dev);
1405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
1408static int i830_getage(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001409 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001411 drm_file_t *priv = filp->private_data;
1412 drm_device_t *dev = priv->head->dev;
1413 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1414 u32 *hw_status = dev_priv->hw_status_page;
1415 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1416 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001418 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return 0;
1420}
1421
1422static int i830_getbuf(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001423 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001425 drm_file_t *priv = filp->private_data;
1426 drm_device_t *dev = priv->head->dev;
1427 int retcode = 0;
1428 drm_i830_dma_t d;
1429 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1430 u32 *hw_status = dev_priv->hw_status_page;
1431 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1432 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 DRM_DEBUG("getbuf\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001435 if (copy_from_user(&d, (drm_i830_dma_t __user *) arg, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 LOCK_TEST_WITH_RETURN(dev, filp);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 d.granted = 0;
1441
1442 retcode = i830_dma_get_buffer(dev, &d, filp);
1443
1444 DRM_DEBUG("i830_dma: %d returning %d, granted = %d\n",
1445 current->pid, retcode, d.granted);
1446
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001447 if (copy_to_user((drm_dma_t __user *) arg, &d, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001449 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 return retcode;
1452}
1453
1454static int i830_copybuf(struct inode *inode,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001455 struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 /* Never copy - 2.4.x doesn't need it */
1458 return 0;
1459}
1460
1461static int i830_docopy(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001462 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 return 0;
1465}
1466
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001467static int i830_getparam(struct inode *inode, struct file *filp,
1468 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001470 drm_file_t *priv = filp->private_data;
1471 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 drm_i830_private_t *dev_priv = dev->dev_private;
1473 drm_i830_getparam_t param;
1474 int value;
1475
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001476 if (!dev_priv) {
1477 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -EINVAL;
1479 }
1480
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001481 if (copy_from_user
1482 (&param, (drm_i830_getparam_t __user *) arg, sizeof(param)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -EFAULT;
1484
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001485 switch (param.param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 case I830_PARAM_IRQ_ACTIVE:
1487 value = dev->irq_enabled;
1488 break;
1489 default:
1490 return -EINVAL;
1491 }
1492
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001493 if (copy_to_user(param.value, &value, sizeof(int))) {
1494 DRM_ERROR("copy_to_user\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -EFAULT;
1496 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return 0;
1499}
1500
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001501static int i830_setparam(struct inode *inode, struct file *filp,
1502 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001504 drm_file_t *priv = filp->private_data;
1505 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 drm_i830_private_t *dev_priv = dev->dev_private;
1507 drm_i830_setparam_t param;
1508
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001509 if (!dev_priv) {
1510 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EINVAL;
1512 }
1513
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001514 if (copy_from_user
1515 (&param, (drm_i830_setparam_t __user *) arg, sizeof(param)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -EFAULT;
1517
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001518 switch (param.param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 case I830_SETPARAM_USE_MI_BATCHBUFFER_START:
1520 dev_priv->use_mi_batchbuffer_start = param.value;
1521 break;
1522 default:
1523 return -EINVAL;
1524 }
1525
1526 return 0;
1527}
1528
Dave Airlie22eae942005-11-10 22:16:34 +11001529int i830_driver_load(drm_device_t *dev, unsigned long flags)
1530{
1531 /* i830 has 4 more counters */
1532 dev->counters += 4;
1533 dev->types[6] = _DRM_STAT_IRQ;
1534 dev->types[7] = _DRM_STAT_PRIMARY;
1535 dev->types[8] = _DRM_STAT_SECONDARY;
1536 dev->types[9] = _DRM_STAT_DMA;
1537
1538 return 0;
1539}
1540
1541void i830_driver_lastclose(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001543 i830_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Dave Airlie22eae942005-11-10 22:16:34 +11001546void i830_driver_preclose(drm_device_t * dev, DRMFILE filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 if (dev->dev_private) {
1549 drm_i830_private_t *dev_priv = dev->dev_private;
1550 if (dev_priv->page_flipping) {
1551 i830_do_cleanup_pageflip(dev);
1552 }
1553 }
1554}
1555
Dave Airlie22eae942005-11-10 22:16:34 +11001556void i830_driver_reclaim_buffers_locked(drm_device_t * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 i830_reclaim_buffers(dev, filp);
1559}
1560
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001561int i830_driver_dma_quiescent(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001563 i830_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return 0;
1565}
1566
1567drm_ioctl_desc_t i830_ioctls[] = {
Dave Airliea7a2cc32006-01-02 13:54:04 +11001568 [DRM_IOCTL_NR(DRM_I830_INIT)] = {i830_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1569 [DRM_IOCTL_NR(DRM_I830_VERTEX)] = {i830_dma_vertex, DRM_AUTH},
1570 [DRM_IOCTL_NR(DRM_I830_CLEAR)] = {i830_clear_bufs, DRM_AUTH},
1571 [DRM_IOCTL_NR(DRM_I830_FLUSH)] = {i830_flush_ioctl, DRM_AUTH},
1572 [DRM_IOCTL_NR(DRM_I830_GETAGE)] = {i830_getage, DRM_AUTH},
1573 [DRM_IOCTL_NR(DRM_I830_GETBUF)] = {i830_getbuf, DRM_AUTH},
1574 [DRM_IOCTL_NR(DRM_I830_SWAP)] = {i830_swap_bufs, DRM_AUTH},
1575 [DRM_IOCTL_NR(DRM_I830_COPY)] = {i830_copybuf, DRM_AUTH},
1576 [DRM_IOCTL_NR(DRM_I830_DOCOPY)] = {i830_docopy, DRM_AUTH},
1577 [DRM_IOCTL_NR(DRM_I830_FLIP)] = {i830_flip_bufs, DRM_AUTH},
1578 [DRM_IOCTL_NR(DRM_I830_IRQ_EMIT)] = {i830_irq_emit, DRM_AUTH},
1579 [DRM_IOCTL_NR(DRM_I830_IRQ_WAIT)] = {i830_irq_wait, DRM_AUTH},
1580 [DRM_IOCTL_NR(DRM_I830_GETPARAM)] = {i830_getparam, DRM_AUTH},
1581 [DRM_IOCTL_NR(DRM_I830_SETPARAM)] = {i830_setparam, DRM_AUTH}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582};
1583
1584int i830_max_ioctl = DRM_ARRAY_SIZE(i830_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001585
1586/**
1587 * Determine if the device really is AGP or not.
1588 *
1589 * All Intel graphics chipsets are treated as AGP, even if they are really
1590 * PCI-e.
1591 *
1592 * \param dev The device to be tested.
1593 *
1594 * \returns
1595 * A value of 1 is always retured to indictate every i8xx is AGP.
1596 */
1597int i830_driver_device_is_agp(drm_device_t * dev)
1598{
1599 return 1;
1600}