blob: 0bb1cbf48109792540008b76df2181f1cdb6a426 [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 Airlie056219e2007-07-11 16:17:42 +100050static struct drm_buf *i830_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Dave Airliecdd55a22007-07-11 16:32:08 +100052 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100053 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++) {
Dave Airlie056219e2007-07-11 16:17:42 +100059 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 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 Airlie056219e2007-07-11 16:17:42 +100075static int i830_freelist_put(struct drm_device * dev, struct drm_buf * 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 Airlieeddca552007-07-11 16:09:54 +100092 struct drm_file *priv = filp->private_data;
93 struct drm_device *dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100094 drm_i830_private_t *dev_priv;
Dave Airlie056219e2007-07-11 16:17:42 +100095 struct drm_buf *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
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800117static const 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 Airlie056219e2007-07-11 16:17:42 +1000125static int i830_map_buffer(struct drm_buf * buf, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Dave Airlieeddca552007-07-11 16:09:54 +1000127 struct drm_file *priv = filp->private_data;
128 struct drm_device *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 Airlie056219e2007-07-11 16:17:42 +1000159static int i830_unmap_buffer(struct drm_buf * 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 Airlieeddca552007-07-11 16:09:54 +1000179static int i830_dma_get_buffer(struct drm_device * dev, drm_i830_dma_t * d,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct file *filp)
181{
Dave Airlie056219e2007-07-11 16:17:42 +1000182 struct drm_buf *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 Airlieeddca552007-07-11 16:09:54 +1000209static int i830_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Dave Airliecdd55a22007-07-11 16:32:08 +1000211 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
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 Airlie056219e2007-07-11 16:17:42 +1000241 struct drm_buf *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 Airlieeddca552007-07-11 16:09:54 +1000250int i830_wait_ring(struct drm_device * 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 Airlieeddca552007-07-11 16:09:54 +1000284static void i830_kernel_lost_context(struct drm_device * 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 Airlieeddca552007-07-11 16:09:54 +1000299static int i830_freelist_init(struct drm_device * dev, drm_i830_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Dave Airliecdd55a22007-07-11 16:32:08 +1000301 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000302 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++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000312 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 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 Airlieeddca552007-07-11 16:09:54 +1000333static int i830_dma_initialize(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334 drm_i830_private_t * dev_priv,
335 drm_i830_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Dave Airlie55910512007-07-11 16:53:40 +1000337 struct drm_map_list *r_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
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
Dave Airliebd1b3312007-05-26 05:01:51 +1000341 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 if (r_list->map &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 break;
347 }
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 if (!dev_priv->sarea_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 dev->dev_private = (void *)dev_priv;
352 i830_dma_cleanup(dev);
353 DRM_ERROR("can not find sarea!\n");
354 return -EINVAL;
355 }
356 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000357 if (!dev_priv->mmio_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dev->dev_private = (void *)dev_priv;
359 i830_dma_cleanup(dev);
360 DRM_ERROR("can not find mmio map!\n");
361 return -EINVAL;
362 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000363 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 dev->dev_private = (void *)dev_priv;
367 i830_dma_cleanup(dev);
368 DRM_ERROR("can not find dma buffer map!\n");
369 return -EINVAL;
370 }
371
372 dev_priv->sarea_priv = (drm_i830_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000375 dev_priv->ring.Start = init->ring_start;
376 dev_priv->ring.End = init->ring_end;
377 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dave Airlieb9094d32007-01-08 21:31:13 +1100379 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
380 dev_priv->ring.map.size = init->ring_size;
381 dev_priv->ring.map.type = _DRM_AGP;
382 dev_priv->ring.map.flags = 0;
383 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Dave Airlieb9094d32007-01-08 21:31:13 +1100385 drm_core_ioremap(&dev_priv->ring.map, dev);
386
387 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388 dev->dev_private = (void *)dev_priv;
389 i830_dma_cleanup(dev);
390 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000392 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
Dave Airlieb9094d32007-01-08 21:31:13 +1100395 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
396
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000397 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 dev_priv->w = init->w;
400 dev_priv->h = init->h;
401 dev_priv->pitch = init->pitch;
402 dev_priv->back_offset = init->back_offset;
403 dev_priv->depth_offset = init->depth_offset;
404 dev_priv->front_offset = init->front_offset;
405
406 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
407 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
408 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
409
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 DRM_DEBUG("front_di1 %x\n", dev_priv->front_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 DRM_DEBUG("back_offset %x\n", dev_priv->back_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 DRM_DEBUG("back_di1 %x\n", dev_priv->back_di1);
413 DRM_DEBUG("pitch_bits %x\n", init->pitch_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 dev_priv->cpp = init->cpp;
416 /* We are using separate values as placeholders for mechanisms for
417 * private backbuffer/depthbuffer usage.
418 */
419
420 dev_priv->back_pitch = init->back_pitch;
421 dev_priv->depth_pitch = init->depth_pitch;
422 dev_priv->do_boxes = 0;
423 dev_priv->use_mi_batchbuffer_start = 0;
424
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000425 /* Program Hardware Status Page */
426 dev_priv->hw_status_page =
427 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
428 &dev_priv->dma_status_page);
429 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 dev->dev_private = (void *)dev_priv;
431 i830_dma_cleanup(dev);
432 DRM_ERROR("Can not allocate hardware status page\n");
433 return -ENOMEM;
434 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000437
438 I830_WRITE(0x02080, dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 DRM_DEBUG("Enabled hardware status page\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000440
441 /* Now we need to init our freelist */
442 if (i830_freelist_init(dev, dev_priv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444 i830_dma_cleanup(dev);
445 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 dev->dev_private = (void *)dev_priv;
450
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454static int i830_dma_init(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000455 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Dave Airlieeddca552007-07-11 16:09:54 +1000457 struct drm_file *priv = filp->private_data;
458 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000459 drm_i830_private_t *dev_priv;
460 drm_i830_init_t init;
461 int retcode = 0;
462
463 if (copy_from_user(&init, (void *__user)arg, sizeof(init)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000465
466 switch (init.func) {
467 case I830_INIT_DMA:
468 dev_priv = drm_alloc(sizeof(drm_i830_private_t),
469 DRM_MEM_DRIVER);
470 if (dev_priv == NULL)
471 return -ENOMEM;
472 retcode = i830_dma_initialize(dev, dev_priv, &init);
473 break;
474 case I830_CLEANUP_DMA:
475 retcode = i830_dma_cleanup(dev);
476 break;
477 default:
478 retcode = -EINVAL;
479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481
482 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485#define GFX_OP_STIPPLE ((0x3<<29)|(0x1d<<24)|(0x83<<16))
486#define ST1_ENABLE (1<<16)
487#define ST1_MASK (0xffff)
488
489/* Most efficient way to verify state for the i830 is as it is
490 * emitted. Non-conformant state is silently dropped.
491 */
Dave Airlieeddca552007-07-11 16:09:54 +1000492static void i830EmitContextVerified(struct drm_device * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int i, j = 0;
496 unsigned int tmp;
497 RING_LOCALS;
498
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000499 BEGIN_LP_RING(I830_CTX_SETUP_SIZE + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000501 for (i = 0; i < I830_CTXREG_BLENDCOLR0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000503 if ((tmp & (7 << 29)) == CMD_3D &&
504 (tmp & (0x1f << 24)) < (0x1d << 24)) {
505 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 j++;
507 } else {
508 DRM_ERROR("Skipping %d\n", i);
509 }
510 }
511
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000512 OUT_RING(STATE3D_CONST_BLEND_COLOR_CMD);
513 OUT_RING(code[I830_CTXREG_BLENDCOLR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 j += 2;
515
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516 for (i = I830_CTXREG_VF; i < I830_CTXREG_MCSB0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000518 if ((tmp & (7 << 29)) == CMD_3D &&
519 (tmp & (0x1f << 24)) < (0x1d << 24)) {
520 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 j++;
522 } else {
523 DRM_ERROR("Skipping %d\n", i);
524 }
525 }
526
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000527 OUT_RING(STATE3D_MAP_COORD_SETBIND_CMD);
528 OUT_RING(code[I830_CTXREG_MCSB1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 j += 2;
530
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 if (j & 1)
532 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 ADVANCE_LP_RING();
535}
536
Dave Airlieeddca552007-07-11 16:09:54 +1000537static void i830EmitTexVerified(struct drm_device * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 int i, j = 0;
541 unsigned int tmp;
542 RING_LOCALS;
543
544 if (code[I830_TEXREG_MI0] == GFX_OP_MAP_INFO ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 (code[I830_TEXREG_MI0] & ~(0xf * LOAD_TEXTURE_MAP0)) ==
546 (STATE3D_LOAD_STATE_IMMEDIATE_2 | 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 BEGIN_LP_RING(I830_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 OUT_RING(code[I830_TEXREG_MI0]); /* TM0LI */
551 OUT_RING(code[I830_TEXREG_MI1]); /* TM0S0 */
552 OUT_RING(code[I830_TEXREG_MI2]); /* TM0S1 */
553 OUT_RING(code[I830_TEXREG_MI3]); /* TM0S2 */
554 OUT_RING(code[I830_TEXREG_MI4]); /* TM0S3 */
555 OUT_RING(code[I830_TEXREG_MI5]); /* TM0S4 */
556
557 for (i = 6; i < I830_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000559 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 if (j & 1)
564 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 printk("rejected packet %x\n", code[0]);
569}
570
Dave Airlieeddca552007-07-11 16:09:54 +1000571static void i830EmitTexBlendVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 unsigned int *code, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int i, j = 0;
576 unsigned int tmp;
577 RING_LOCALS;
578
579 if (!num)
580 return;
581
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 BEGIN_LP_RING(num + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000584 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 j++;
588 }
589
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 if (j & 1)
591 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 ADVANCE_LP_RING();
594}
595
Dave Airlieeddca552007-07-11 16:09:54 +1000596static void i830EmitTexPalette(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 unsigned int *palette, int number, int is_shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000599 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int i;
601 RING_LOCALS;
602
603 return;
604
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000605 BEGIN_LP_RING(258);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000607 if (is_shared == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 OUT_RING(CMD_OP_MAP_PALETTE_LOAD |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609 MAP_PALETTE_NUM(0) | MAP_PALETTE_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 } else {
611 OUT_RING(CMD_OP_MAP_PALETTE_LOAD | MAP_PALETTE_NUM(number));
612 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 for (i = 0; i < 256; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 OUT_RING(palette[i]);
615 }
616 OUT_RING(0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 /* KW: WHERE IS THE ADVANCE_LP_RING? This is effectively a noop!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
619}
620
621/* Need to do some additional checking when setting the dest buffer.
622 */
Dave Airlieeddca552007-07-11 16:09:54 +1000623static void i830EmitDestVerified(struct drm_device * dev, unsigned int *code)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624{
625 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unsigned int tmp;
627 RING_LOCALS;
628
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 BEGIN_LP_RING(I830_DEST_SETUP_SIZE + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 tmp = code[I830_DESTREG_CBUFADDR];
632 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
633 if (((int)outring) & 8) {
634 OUT_RING(0);
635 OUT_RING(0);
636 }
637
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 OUT_RING(CMD_OP_DESTBUFFER_INFO);
639 OUT_RING(BUF_3D_ID_COLOR_BACK |
640 BUF_3D_PITCH(dev_priv->back_pitch * dev_priv->cpp) |
641 BUF_3D_USE_FENCE);
642 OUT_RING(tmp);
643 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000645 OUT_RING(CMD_OP_DESTBUFFER_INFO);
646 OUT_RING(BUF_3D_ID_DEPTH | BUF_3D_USE_FENCE |
647 BUF_3D_PITCH(dev_priv->depth_pitch * dev_priv->cpp));
648 OUT_RING(dev_priv->zi1);
649 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 } else {
651 DRM_ERROR("bad di1 %x (allow %x or %x)\n",
652 tmp, dev_priv->front_di1, dev_priv->back_di1);
653 }
654
655 /* invarient:
656 */
657
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658 OUT_RING(GFX_OP_DESTBUFFER_VARS);
659 OUT_RING(code[I830_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 OUT_RING(GFX_OP_DRAWRECT_INFO);
662 OUT_RING(code[I830_DESTREG_DR1]);
663 OUT_RING(code[I830_DESTREG_DR2]);
664 OUT_RING(code[I830_DESTREG_DR3]);
665 OUT_RING(code[I830_DESTREG_DR4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Need to verify this */
668 tmp = code[I830_DESTREG_SENABLE];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 if ((tmp & ~0x3) == GFX_OP_SCISSOR_ENABLE) {
670 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 } else {
672 DRM_ERROR("bad scissor enable\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000676 OUT_RING(GFX_OP_SCISSOR_RECT);
677 OUT_RING(code[I830_DESTREG_SR1]);
678 OUT_RING(code[I830_DESTREG_SR2]);
679 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 ADVANCE_LP_RING();
682}
683
Dave Airlieeddca552007-07-11 16:09:54 +1000684static void i830EmitStippleVerified(struct drm_device * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 RING_LOCALS;
688
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 BEGIN_LP_RING(2);
690 OUT_RING(GFX_OP_STIPPLE);
691 OUT_RING(code[1]);
692 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Dave Airlieeddca552007-07-11 16:09:54 +1000695static void i830EmitState(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 drm_i830_private_t *dev_priv = dev->dev_private;
698 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 unsigned int dirty = sarea_priv->dirty;
700
701 DRM_DEBUG("%s %x\n", __FUNCTION__, dirty);
702
703 if (dirty & I830_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 i830EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 sarea_priv->dirty &= ~I830_UPLOAD_BUFFERS;
706 }
707
708 if (dirty & I830_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 i830EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 sarea_priv->dirty &= ~I830_UPLOAD_CTX;
711 }
712
713 if (dirty & I830_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 i830EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 sarea_priv->dirty &= ~I830_UPLOAD_TEX0;
716 }
717
718 if (dirty & I830_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 i830EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 sarea_priv->dirty &= ~I830_UPLOAD_TEX1;
721 }
722
723 if (dirty & I830_UPLOAD_TEXBLEND0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[0],
725 sarea_priv->TexBlendStateWordsUsed[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND0;
727 }
728
729 if (dirty & I830_UPLOAD_TEXBLEND1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[1],
731 sarea_priv->TexBlendStateWordsUsed[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND1;
733 }
734
735 if (dirty & I830_UPLOAD_TEX_PALETTE_SHARED) {
736 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 1);
737 } else {
738 if (dirty & I830_UPLOAD_TEX_PALETTE_N(0)) {
739 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 0);
740 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(0);
741 }
742 if (dirty & I830_UPLOAD_TEX_PALETTE_N(1)) {
743 i830EmitTexPalette(dev, sarea_priv->Palette[1], 1, 0);
744 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(1);
745 }
746
747 /* 1.3:
748 */
749#if 0
750 if (dirty & I830_UPLOAD_TEX_PALETTE_N(2)) {
751 i830EmitTexPalette(dev, sarea_priv->Palette2[0], 0, 0);
752 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
753 }
754 if (dirty & I830_UPLOAD_TEX_PALETTE_N(3)) {
755 i830EmitTexPalette(dev, sarea_priv->Palette2[1], 1, 0);
756 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
757 }
758#endif
759 }
760
761 /* 1.3:
762 */
763 if (dirty & I830_UPLOAD_STIPPLE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000764 i830EmitStippleVerified(dev, sarea_priv->StippleState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 sarea_priv->dirty &= ~I830_UPLOAD_STIPPLE;
766 }
767
768 if (dirty & I830_UPLOAD_TEX2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 i830EmitTexVerified(dev, sarea_priv->TexState2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 sarea_priv->dirty &= ~I830_UPLOAD_TEX2;
771 }
772
773 if (dirty & I830_UPLOAD_TEX3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 i830EmitTexVerified(dev, sarea_priv->TexState3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 sarea_priv->dirty &= ~I830_UPLOAD_TEX3;
776 }
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (dirty & I830_UPLOAD_TEXBLEND2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 i830EmitTexBlendVerified(dev,
780 sarea_priv->TexBlendState2,
781 sarea_priv->TexBlendStateWordsUsed2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND2;
784 }
785
786 if (dirty & I830_UPLOAD_TEXBLEND3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 i830EmitTexBlendVerified(dev,
788 sarea_priv->TexBlendState3,
789 sarea_priv->TexBlendStateWordsUsed3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND3;
791 }
792}
793
794/* ================================================================
795 * Performance monitoring functions
796 */
797
Dave Airlieeddca552007-07-11 16:09:54 +1000798static void i830_fill_box(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 u32 color;
803 unsigned int BR13, CMD;
804 RING_LOCALS;
805
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 BR13 = (0xF0 << 16) | (dev_priv->pitch * dev_priv->cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 CMD = XY_COLOR_BLT_CMD;
808 x += dev_priv->sarea_priv->boxes[0].x1;
809 y += dev_priv->sarea_priv->boxes[0].y1;
810
811 if (dev_priv->cpp == 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 BR13 |= (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 CMD |= (XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 } else {
816 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 BEGIN_LP_RING(6);
821 OUT_RING(CMD);
822 OUT_RING(BR13);
823 OUT_RING((y << 16) | x);
824 OUT_RING(((y + h) << 16) | (x + w));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 if (dev_priv->current_page == 1) {
827 OUT_RING(dev_priv->front_offset);
828 } else {
829 OUT_RING(dev_priv->back_offset);
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ADVANCE_LP_RING();
834}
835
Dave Airlieeddca552007-07-11 16:09:54 +1000836static void i830_cp_performance_boxes(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* Purple box for page flipping
841 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_FLIP)
843 i830_fill_box(dev, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* Red box if we have to wait for idle at any point
846 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_WAIT)
848 i830_fill_box(dev, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* Blue box: lost context?
851 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_LOST_CONTEXT)
853 i830_fill_box(dev, 28, 4, 8, 8, 0, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Yellow box for texture swaps
856 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_TEXTURE_LOAD)
858 i830_fill_box(dev, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* Green box if hardware never idles (as far as we can tell)
861 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 if (!(dev_priv->sarea_priv->perf_boxes & I830_BOX_RING_EMPTY))
863 i830_fill_box(dev, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * (not a great measure, easily confused)
867 */
868 if (dev_priv->dma_used) {
869 int bar = dev_priv->dma_used / 10240;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 if (bar > 100)
871 bar = 100;
872 if (bar < 1)
873 bar = 1;
874 i830_fill_box(dev, 4, 16, bar, 4, 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 dev_priv->dma_used = 0;
876 }
877
878 dev_priv->sarea_priv->perf_boxes = 0;
879}
880
Dave Airlieeddca552007-07-11 16:09:54 +1000881static void i830_dma_dispatch_clear(struct drm_device * dev, int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 unsigned int clear_color,
883 unsigned int clear_zval,
884 unsigned int clear_depthmask)
885{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 drm_i830_private_t *dev_priv = dev->dev_private;
887 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000889 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int pitch = dev_priv->pitch;
891 int cpp = dev_priv->cpp;
892 int i;
893 unsigned int BR13, CMD, D_CMD;
894 RING_LOCALS;
895
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 if (dev_priv->current_page == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 unsigned int tmp = flags;
898
899 flags &= ~(I830_FRONT | I830_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 if (tmp & I830_FRONT)
901 flags |= I830_BACK;
902 if (tmp & I830_BACK)
903 flags |= I830_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 switch (cpp) {
909 case 2:
910 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 D_CMD = CMD = XY_COLOR_BLT_CMD;
912 break;
913 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24) | (1 << 25);
915 CMD = (XY_COLOR_BLT_CMD | XY_COLOR_BLT_WRITE_ALPHA |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 XY_COLOR_BLT_WRITE_RGB);
917 D_CMD = XY_COLOR_BLT_CMD;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 if (clear_depthmask & 0x00ffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 D_CMD |= XY_COLOR_BLT_WRITE_RGB;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 if (clear_depthmask & 0xff000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 D_CMD |= XY_COLOR_BLT_WRITE_ALPHA;
922 break;
923 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 D_CMD = CMD = XY_COLOR_BLT_CMD;
926 break;
927 }
928
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 if (nbox > I830_NR_SAREA_CLIPRECTS)
930 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000932 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (pbox->x1 > pbox->x2 ||
934 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000935 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 continue;
937
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 if (flags & I830_FRONT) {
939 DRM_DEBUG("clear front\n");
940 BEGIN_LP_RING(6);
941 OUT_RING(CMD);
942 OUT_RING(BR13);
943 OUT_RING((pbox->y1 << 16) | pbox->x1);
944 OUT_RING((pbox->y2 << 16) | pbox->x2);
945 OUT_RING(dev_priv->front_offset);
946 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 ADVANCE_LP_RING();
948 }
949
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 if (flags & I830_BACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 DRM_DEBUG("clear back\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 BEGIN_LP_RING(6);
953 OUT_RING(CMD);
954 OUT_RING(BR13);
955 OUT_RING((pbox->y1 << 16) | pbox->x1);
956 OUT_RING((pbox->y2 << 16) | pbox->x2);
957 OUT_RING(dev_priv->back_offset);
958 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 ADVANCE_LP_RING();
960 }
961
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000962 if (flags & I830_DEPTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 DRM_DEBUG("clear depth\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000964 BEGIN_LP_RING(6);
965 OUT_RING(D_CMD);
966 OUT_RING(BR13);
967 OUT_RING((pbox->y1 << 16) | pbox->x1);
968 OUT_RING((pbox->y2 << 16) | pbox->x2);
969 OUT_RING(dev_priv->depth_offset);
970 OUT_RING(clear_zval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 ADVANCE_LP_RING();
972 }
973 }
974}
975
Dave Airlieeddca552007-07-11 16:09:54 +1000976static void i830_dma_dispatch_swap(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000978 drm_i830_private_t *dev_priv = dev->dev_private;
979 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000981 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int pitch = dev_priv->pitch;
983 int cpp = dev_priv->cpp;
984 int i;
985 unsigned int CMD, BR13;
986 RING_LOCALS;
987
988 DRM_DEBUG("swapbuffers\n");
989
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000990 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (dev_priv->do_boxes)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000993 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 switch (cpp) {
996 case 2:
997 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 CMD = XY_SRC_COPY_BLT_CMD;
999 break;
1000 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24) | (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
1003 XY_SRC_COPY_BLT_WRITE_RGB);
1004 break;
1005 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001006 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 CMD = XY_SRC_COPY_BLT_CMD;
1008 break;
1009 }
1010
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 if (nbox > I830_NR_SAREA_CLIPRECTS)
1012 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (pbox->x1 > pbox->x2 ||
1016 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001017 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 continue;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 DRM_DEBUG("dispatch swap %d,%d-%d,%d!\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 pbox->x1, pbox->y1, pbox->x2, pbox->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 BEGIN_LP_RING(8);
1024 OUT_RING(CMD);
1025 OUT_RING(BR13);
1026 OUT_RING((pbox->y1 << 16) | pbox->x1);
1027 OUT_RING((pbox->y2 << 16) | pbox->x2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 if (dev_priv->current_page == 0)
1030 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 OUT_RING((pbox->y1 << 16) | pbox->x1);
1035 OUT_RING(BR13 & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 if (dev_priv->current_page == 0)
1038 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 ADVANCE_LP_RING();
1043 }
1044}
1045
Dave Airlieeddca552007-07-11 16:09:54 +10001046static void i830_dma_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 RING_LOCALS;
1050
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
1052 __FUNCTION__,
1053 dev_priv->current_page,
1054 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001056 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (dev_priv->do_boxes) {
1059 dev_priv->sarea_priv->perf_boxes |= I830_BOX_FLIP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001063 BEGIN_LP_RING(2);
1064 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1065 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 ADVANCE_LP_RING();
1067
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 BEGIN_LP_RING(6);
1069 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
1070 OUT_RING(0);
1071 if (dev_priv->current_page == 0) {
1072 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 dev_priv->current_page = 1;
1074 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 dev_priv->current_page = 0;
1077 }
1078 OUT_RING(0);
1079 ADVANCE_LP_RING();
1080
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 BEGIN_LP_RING(2);
1082 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
1083 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1087}
1088
Dave Airlieeddca552007-07-11 16:09:54 +10001089static void i830_dma_dispatch_vertex(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +10001090 struct drm_buf * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +10001095 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 unsigned long address = (unsigned long)buf->bus_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098 unsigned long start = address - dev->agp->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 int i = 0, u;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001100 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001104 if (nbox > I830_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 nbox = I830_NR_SAREA_CLIPRECTS;
1106
1107 if (discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 u = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 I830_BUF_HARDWARE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001110 if (u != I830_BUF_CLIENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 DRM_DEBUG("xxxx 2\n");
1112 }
1113 }
1114
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001115 if (used > 4 * 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 used = 0;
1117
1118 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001119 i830EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001121 DRM_DEBUG("dispatch vertex addr 0x%lx, used 0x%x nbox %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 address, used, nbox);
1123
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 dev_priv->counter++;
1125 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1126 DRM_DEBUG("i830_dma_dispatch\n");
1127 DRM_DEBUG("start : %lx\n", start);
1128 DRM_DEBUG("used : %d\n", used);
1129 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (buf_priv->currently_mapped == I830_BUF_MAPPED) {
1132 u32 *vp = buf_priv->kernel_virtual;
1133
1134 vp[0] = (GFX_OP_PRIMITIVE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001135 sarea_priv->vertex_prim | ((used / 4) - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 if (dev_priv->use_mi_batchbuffer_start) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138 vp[used / 4] = MI_BATCH_BUFFER_END;
1139 used += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (used & 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001143 vp[used / 4] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 used += 4;
1145 }
1146
1147 i830_unmap_buffer(buf);
1148 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (used) {
1151 do {
1152 if (i < nbox) {
1153 BEGIN_LP_RING(6);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001154 OUT_RING(GFX_OP_DRAWRECT_INFO);
1155 OUT_RING(sarea_priv->
1156 BufferState[I830_DESTREG_DR1]);
1157 OUT_RING(box[i].x1 | (box[i].y1 << 16));
1158 OUT_RING(box[i].x2 | (box[i].y2 << 16));
1159 OUT_RING(sarea_priv->
1160 BufferState[I830_DESTREG_DR4]);
1161 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 ADVANCE_LP_RING();
1163 }
1164
1165 if (dev_priv->use_mi_batchbuffer_start) {
1166 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001167 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
1168 OUT_RING(start | MI_BATCH_NON_SECURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001170 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001172 OUT_RING(MI_BATCH_BUFFER);
1173 OUT_RING(start | MI_BATCH_NON_SECURE);
1174 OUT_RING(start + used - 4);
1175 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ADVANCE_LP_RING();
1177 }
1178
1179 } while (++i < nbox);
1180 }
1181
1182 if (discard) {
1183 dev_priv->counter++;
1184
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001185 (void)cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
1186 I830_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001189 OUT_RING(CMD_STORE_DWORD_IDX);
1190 OUT_RING(20);
1191 OUT_RING(dev_priv->counter);
1192 OUT_RING(CMD_STORE_DWORD_IDX);
1193 OUT_RING(buf_priv->my_use_idx);
1194 OUT_RING(I830_BUF_FREE);
1195 OUT_RING(CMD_REPORT_HEAD);
1196 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 ADVANCE_LP_RING();
1198 }
1199}
1200
Dave Airlieeddca552007-07-11 16:09:54 +10001201static void i830_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001203 drm_i830_private_t *dev_priv = dev->dev_private;
1204 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001206 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001208 BEGIN_LP_RING(4);
1209 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1210 OUT_RING(CMD_REPORT_HEAD);
1211 OUT_RING(0);
1212 OUT_RING(0);
1213 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001215 i830_wait_ring(dev, dev_priv->ring.Size - 8, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Dave Airlieeddca552007-07-11 16:09:54 +10001218static int i830_flush_queue(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001220 drm_i830_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001221 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001222 int i, ret = 0;
1223 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001225 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001227 BEGIN_LP_RING(2);
1228 OUT_RING(CMD_REPORT_HEAD);
1229 OUT_RING(0);
1230 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001232 i830_wait_ring(dev, dev_priv->ring.Size - 8, __FUNCTION__);
1233
1234 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001235 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001236 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1237
1238 int used = cmpxchg(buf_priv->in_use, I830_BUF_HARDWARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 I830_BUF_FREE);
1240
1241 if (used == I830_BUF_HARDWARE)
1242 DRM_DEBUG("reclaimed from HARDWARE\n");
1243 if (used == I830_BUF_CLIENT)
1244 DRM_DEBUG("still on client\n");
1245 }
1246
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
1250/* Must be called with the lock held */
Dave Airlieeddca552007-07-11 16:09:54 +10001251static void i830_reclaim_buffers(struct drm_device * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Dave Airliecdd55a22007-07-11 16:32:08 +10001253 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001254 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001256 if (!dma)
1257 return;
1258 if (!dev->dev_private)
1259 return;
1260 if (!dma->buflist)
1261 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001263 i830_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001266 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001267 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (buf->filp == filp && buf_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001270 int used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 I830_BUF_FREE);
1272
1273 if (used == I830_BUF_CLIENT)
1274 DRM_DEBUG("reclaimed from client\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001275 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
1276 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 }
1279}
1280
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001281static int i830_flush_ioctl(struct inode *inode, struct file *filp,
1282 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Dave Airlieeddca552007-07-11 16:09:54 +10001284 struct drm_file *priv = filp->private_data;
1285 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 LOCK_TEST_WITH_RETURN(dev, filp);
1288
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001289 i830_flush_queue(dev);
1290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
1293static int i830_dma_vertex(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001294 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Dave Airlieeddca552007-07-11 16:09:54 +10001296 struct drm_file *priv = filp->private_data;
1297 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001298 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001299 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1300 u32 *hw_status = dev_priv->hw_status_page;
1301 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1302 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 drm_i830_vertex_t vertex;
1304
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305 if (copy_from_user
1306 (&vertex, (drm_i830_vertex_t __user *) arg, sizeof(vertex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EFAULT;
1308
1309 LOCK_TEST_WITH_RETURN(dev, filp);
1310
1311 DRM_DEBUG("i830 dma vertex, idx %d used %d discard %d\n",
1312 vertex.idx, vertex.used, vertex.discard);
1313
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001314 if (vertex.idx < 0 || vertex.idx > dma->buf_count)
1315 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001317 i830_dma_dispatch_vertex(dev,
1318 dma->buflist[vertex.idx],
1319 vertex.discard, vertex.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001321 sarea_priv->last_enqueue = dev_priv->counter - 1;
1322 sarea_priv->last_dispatch = (int)hw_status[5];
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325}
1326
1327static int i830_clear_bufs(struct inode *inode, struct file *filp,
1328 unsigned int cmd, unsigned long arg)
1329{
Dave Airlieeddca552007-07-11 16:09:54 +10001330 struct drm_file *priv = filp->private_data;
1331 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 drm_i830_clear_t clear;
1333
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001334 if (copy_from_user
1335 (&clear, (drm_i830_clear_t __user *) arg, sizeof(clear)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 LOCK_TEST_WITH_RETURN(dev, filp);
1339
1340 /* GH: Someone's doing nasty things... */
1341 if (!dev->dev_private) {
1342 return -EINVAL;
1343 }
1344
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001345 i830_dma_dispatch_clear(dev, clear.flags,
1346 clear.clear_color,
1347 clear.clear_depth, clear.clear_depthmask);
1348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
1351static int i830_swap_bufs(struct inode *inode, struct file *filp,
1352 unsigned int cmd, unsigned long arg)
1353{
Dave Airlieeddca552007-07-11 16:09:54 +10001354 struct drm_file *priv = filp->private_data;
1355 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 DRM_DEBUG("i830_swap_bufs\n");
1358
1359 LOCK_TEST_WITH_RETURN(dev, filp);
1360
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001361 i830_dma_dispatch_swap(dev);
1362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001366 */
Dave Airlieeddca552007-07-11 16:09:54 +10001367static void i830_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 drm_i830_private_t *dev_priv = dev->dev_private;
1370
1371 DRM_DEBUG("%s\n", __FUNCTION__);
1372 dev_priv->page_flipping = 1;
1373 dev_priv->current_page = 0;
1374 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1375}
1376
Dave Airlieeddca552007-07-11 16:09:54 +10001377static int i830_do_cleanup_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 drm_i830_private_t *dev_priv = dev->dev_private;
1380
1381 DRM_DEBUG("%s\n", __FUNCTION__);
1382 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001383 i830_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 dev_priv->page_flipping = 0;
1386 return 0;
1387}
1388
1389static int i830_flip_bufs(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001390 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Dave Airlieeddca552007-07-11 16:09:54 +10001392 struct drm_file *priv = filp->private_data;
1393 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 drm_i830_private_t *dev_priv = dev->dev_private;
1395
1396 DRM_DEBUG("%s\n", __FUNCTION__);
1397
1398 LOCK_TEST_WITH_RETURN(dev, filp);
1399
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001400 if (!dev_priv->page_flipping)
1401 i830_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001403 i830_dma_dispatch_flip(dev);
1404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
1407static int i830_getage(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Dave Airlieeddca552007-07-11 16:09:54 +10001410 struct drm_file *priv = filp->private_data;
1411 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001412 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1413 u32 *hw_status = dev_priv->hw_status_page;
1414 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1415 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001417 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return 0;
1419}
1420
1421static int i830_getbuf(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001422 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Dave Airlieeddca552007-07-11 16:09:54 +10001424 struct drm_file *priv = filp->private_data;
1425 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001426 int retcode = 0;
1427 drm_i830_dma_t d;
1428 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1429 u32 *hw_status = dev_priv->hw_status_page;
1430 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1431 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 DRM_DEBUG("getbuf\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001434 if (copy_from_user(&d, (drm_i830_dma_t __user *) arg, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 LOCK_TEST_WITH_RETURN(dev, filp);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 d.granted = 0;
1440
1441 retcode = i830_dma_get_buffer(dev, &d, filp);
1442
1443 DRM_DEBUG("i830_dma: %d returning %d, granted = %d\n",
1444 current->pid, retcode, d.granted);
1445
Dave Airlieeddca552007-07-11 16:09:54 +10001446 if (copy_to_user((void __user *) arg, &d, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001448 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 return retcode;
1451}
1452
1453static int i830_copybuf(struct inode *inode,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001454 struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 /* Never copy - 2.4.x doesn't need it */
1457 return 0;
1458}
1459
1460static int i830_docopy(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001461 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 return 0;
1464}
1465
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001466static int i830_getparam(struct inode *inode, struct file *filp,
1467 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Dave Airlieeddca552007-07-11 16:09:54 +10001469 struct drm_file *priv = filp->private_data;
1470 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 drm_i830_private_t *dev_priv = dev->dev_private;
1472 drm_i830_getparam_t param;
1473 int value;
1474
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001475 if (!dev_priv) {
1476 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return -EINVAL;
1478 }
1479
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001480 if (copy_from_user
1481 (&param, (drm_i830_getparam_t __user *) arg, sizeof(param)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EFAULT;
1483
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001484 switch (param.param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 case I830_PARAM_IRQ_ACTIVE:
1486 value = dev->irq_enabled;
1487 break;
1488 default:
1489 return -EINVAL;
1490 }
1491
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001492 if (copy_to_user(param.value, &value, sizeof(int))) {
1493 DRM_ERROR("copy_to_user\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EFAULT;
1495 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498}
1499
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001500static int i830_setparam(struct inode *inode, struct file *filp,
1501 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Dave Airlieeddca552007-07-11 16:09:54 +10001503 struct drm_file *priv = filp->private_data;
1504 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 drm_i830_private_t *dev_priv = dev->dev_private;
1506 drm_i830_setparam_t param;
1507
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001508 if (!dev_priv) {
1509 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return -EINVAL;
1511 }
1512
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001513 if (copy_from_user
1514 (&param, (drm_i830_setparam_t __user *) arg, sizeof(param)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EFAULT;
1516
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001517 switch (param.param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 case I830_SETPARAM_USE_MI_BATCHBUFFER_START:
1519 dev_priv->use_mi_batchbuffer_start = param.value;
1520 break;
1521 default:
1522 return -EINVAL;
1523 }
1524
1525 return 0;
1526}
1527
Dave Airlieeddca552007-07-11 16:09:54 +10001528int i830_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001529{
1530 /* i830 has 4 more counters */
1531 dev->counters += 4;
1532 dev->types[6] = _DRM_STAT_IRQ;
1533 dev->types[7] = _DRM_STAT_PRIMARY;
1534 dev->types[8] = _DRM_STAT_SECONDARY;
1535 dev->types[9] = _DRM_STAT_DMA;
1536
1537 return 0;
1538}
1539
Dave Airlieeddca552007-07-11 16:09:54 +10001540void i830_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001542 i830_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
Dave Airlieeddca552007-07-11 16:09:54 +10001545void i830_driver_preclose(struct drm_device * dev, DRMFILE filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 if (dev->dev_private) {
1548 drm_i830_private_t *dev_priv = dev->dev_private;
1549 if (dev_priv->page_flipping) {
1550 i830_do_cleanup_pageflip(dev);
1551 }
1552 }
1553}
1554
Dave Airlieeddca552007-07-11 16:09:54 +10001555void i830_driver_reclaim_buffers_locked(struct drm_device * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 i830_reclaim_buffers(dev, filp);
1558}
1559
Dave Airlieeddca552007-07-11 16:09:54 +10001560int i830_driver_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001562 i830_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 0;
1564}
1565
1566drm_ioctl_desc_t i830_ioctls[] = {
Dave Airliea7a2cc32006-01-02 13:54:04 +11001567 [DRM_IOCTL_NR(DRM_I830_INIT)] = {i830_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1568 [DRM_IOCTL_NR(DRM_I830_VERTEX)] = {i830_dma_vertex, DRM_AUTH},
1569 [DRM_IOCTL_NR(DRM_I830_CLEAR)] = {i830_clear_bufs, DRM_AUTH},
1570 [DRM_IOCTL_NR(DRM_I830_FLUSH)] = {i830_flush_ioctl, DRM_AUTH},
1571 [DRM_IOCTL_NR(DRM_I830_GETAGE)] = {i830_getage, DRM_AUTH},
1572 [DRM_IOCTL_NR(DRM_I830_GETBUF)] = {i830_getbuf, DRM_AUTH},
1573 [DRM_IOCTL_NR(DRM_I830_SWAP)] = {i830_swap_bufs, DRM_AUTH},
1574 [DRM_IOCTL_NR(DRM_I830_COPY)] = {i830_copybuf, DRM_AUTH},
1575 [DRM_IOCTL_NR(DRM_I830_DOCOPY)] = {i830_docopy, DRM_AUTH},
1576 [DRM_IOCTL_NR(DRM_I830_FLIP)] = {i830_flip_bufs, DRM_AUTH},
1577 [DRM_IOCTL_NR(DRM_I830_IRQ_EMIT)] = {i830_irq_emit, DRM_AUTH},
1578 [DRM_IOCTL_NR(DRM_I830_IRQ_WAIT)] = {i830_irq_wait, DRM_AUTH},
1579 [DRM_IOCTL_NR(DRM_I830_GETPARAM)] = {i830_getparam, DRM_AUTH},
1580 [DRM_IOCTL_NR(DRM_I830_SETPARAM)] = {i830_setparam, DRM_AUTH}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581};
1582
1583int i830_max_ioctl = DRM_ARRAY_SIZE(i830_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001584
1585/**
1586 * Determine if the device really is AGP or not.
1587 *
1588 * All Intel graphics chipsets are treated as AGP, even if they are really
1589 * PCI-e.
1590 *
1591 * \param dev The device to be tested.
1592 *
1593 * \returns
1594 * A value of 1 is always retured to indictate every i8xx is AGP.
1595 */
Dave Airlieeddca552007-07-11 16:09:54 +10001596int i830_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001597{
1598 return 1;
1599}