blob: 810625093c06bec3741acbc98fb5db178befe183 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i810_dma.c -- DMA support for the i810 -*- 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:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
29 * Keith Whitwell <keith@tungstengraphics.com>
30 *
31 */
32
33#include "drmP.h"
34#include "drm.h"
35#include "i810_drm.h"
36#include "i810_drv.h"
37#include <linux/interrupt.h> /* For task queue support */
38#include <linux/delay.h>
39#include <linux/pagemap.h>
40
41#define I810_BUF_FREE 2
42#define I810_BUF_CLIENT 1
43#define I810_BUF_HARDWARE 0
44
45#define I810_BUF_UNMAPPED 0
46#define I810_BUF_MAPPED 1
47
Dave Airlieb5e89ed2005-09-25 14:28:13 +100048static drm_buf_t *i810_freelist_get(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100050 drm_device_dma_t *dma = dev->dma;
51 int i;
52 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /* Linear search might not be the best solution */
55
Dave Airlieb5e89ed2005-09-25 14:28:13 +100056 for (i = 0; i < dma->buf_count; i++) {
57 drm_buf_t *buf = dma->buflist[i];
58 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 used = cmpxchg(buf_priv->in_use, I810_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 I810_BUF_CLIENT);
62 if (used == I810_BUF_FREE) {
63 return buf;
64 }
65 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100066 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69/* This should only be called if the buffer is not sent to the hardware
70 * yet, the hardware updates in use for us once its on the ring buffer.
71 */
72
Dave Airlieb5e89ed2005-09-25 14:28:13 +100073static int i810_freelist_put(drm_device_t * dev, drm_buf_t * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
76 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078 /* In use is already a pointer */
79 used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_FREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (used != I810_BUF_CLIENT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100081 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
82 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Dave Airliec94f7022005-07-07 21:03:38 +100088static int i810_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090 drm_file_t *priv = filp->private_data;
91 drm_device_t *dev;
92 drm_i810_private_t *dev_priv;
93 drm_buf_t *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 drm_i810_buf_priv_t *buf_priv;
95
96 lock_kernel();
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097 dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100099 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 buf_priv = buf->dev_private;
101
102 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
103 vma->vm_file = filp;
104
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 buf_priv->currently_mapped = I810_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unlock_kernel();
107
108 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 VM_OFFSET(vma) >> PAGE_SHIFT,
110 vma->vm_end - vma->vm_start, vma->vm_page_prot))
111 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
113}
114
Dave Airliec94f7022005-07-07 21:03:38 +1000115static struct file_operations i810_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 .open = drm_open,
117 .flush = drm_flush,
Dave Airliec94f7022005-07-07 21:03:38 +1000118 .release = drm_release,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000119 .ioctl = drm_ioctl,
120 .mmap = i810_mmap_buffers,
121 .fasync = drm_fasync,
Dave Airliec94f7022005-07-07 21:03:38 +1000122};
123
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000124static int i810_map_buffer(drm_buf_t * buf, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000126 drm_file_t *priv = filp->private_data;
127 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 drm_i810_private_t *dev_priv = dev->dev_private;
130 struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int retcode = 0;
132
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -EINVAL;
135
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000136 down_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 old_fops = filp->f_op;
138 filp->f_op = &i810_buffer_fops;
139 dev_priv->mmap_buffer = buf;
140 buf_priv->virtual = (void *)do_mmap(filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 PROT_READ | PROT_WRITE,
142 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 dev_priv->mmap_buffer = NULL;
144 filp->f_op = old_fops;
145 if ((unsigned long)buf_priv->virtual > -1024UL) {
146 /* Real error */
147 DRM_ERROR("mmap error\n");
148 retcode = (signed int)buf_priv->virtual;
149 buf_priv->virtual = NULL;
150 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000151 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 return retcode;
154}
155
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156static int i810_unmap_buffer(drm_buf_t * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
159 int retcode = 0;
160
161 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
162 return -EINVAL;
163
164 down_write(&current->mm->mmap_sem);
165 retcode = do_munmap(current->mm,
166 (unsigned long)buf_priv->virtual,
167 (size_t) buf->total);
168 up_write(&current->mm->mmap_sem);
169
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000170 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
171 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 return retcode;
174}
175
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176static int i810_dma_get_buffer(drm_device_t * dev, drm_i810_dma_t * d,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct file *filp)
178{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179 drm_buf_t *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 drm_i810_buf_priv_t *buf_priv;
181 int retcode = 0;
182
183 buf = i810_freelist_get(dev);
184 if (!buf) {
185 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return retcode;
188 }
189
190 retcode = i810_map_buffer(buf, filp);
191 if (retcode) {
192 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return retcode;
195 }
196 buf->filp = filp;
197 buf_priv = buf->dev_private;
198 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 d->request_idx = buf->idx;
200 d->request_size = buf->total;
201 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 return retcode;
204}
205
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206static int i810_dma_cleanup(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 drm_device_dma_t *dma = dev->dma;
209
210 /* Make sure interrupts are disabled here because the uninstall ioctl
211 * may not have been called from userspace and after dev_private
212 * is freed, it's too late.
213 */
214 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
215 drm_irq_uninstall(dev);
216
217 if (dev->dev_private) {
218 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219 drm_i810_private_t *dev_priv =
220 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (dev_priv->ring.virtual_start) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000223 drm_ioremapfree((void *)dev_priv->ring.virtual_start,
224 dev_priv->ring.Size, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 if (dev_priv->hw_status_page) {
227 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 dev_priv->hw_status_page,
229 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000230 /* Need to rewrite hardware status page */
231 I810_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000233 drm_free(dev->dev_private, sizeof(drm_i810_private_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 DRM_MEM_DRIVER);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 for (i = 0; i < dma->buf_count; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238 drm_buf_t *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000240 if (buf_priv->kernel_virtual && buf->total)
241 drm_ioremapfree(buf_priv->kernel_virtual,
242 buf->total, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246}
247
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000248static int i810_wait_ring(drm_device_t * dev, int n)
249{
250 drm_i810_private_t *dev_priv = dev->dev_private;
251 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
252 int iters = 0;
253 unsigned long end;
254 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
255
256 end = jiffies + (HZ * 3);
257 while (ring->space < n) {
258 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
259 ring->space = ring->head - (ring->tail + 8);
260 if (ring->space < 0)
261 ring->space += ring->Size;
262
263 if (ring->head != last_head) {
264 end = jiffies + (HZ * 3);
265 last_head = ring->head;
266 }
267
268 iters++;
269 if (time_before(end, jiffies)) {
270 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
271 DRM_ERROR("lockup\n");
272 goto out_wait_ring;
273 }
274 udelay(1);
275 }
276
277 out_wait_ring:
278 return iters;
279}
280
281static void i810_kernel_lost_context(drm_device_t * dev)
282{
283 drm_i810_private_t *dev_priv = dev->dev_private;
284 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
285
286 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
287 ring->tail = I810_READ(LP_RING + RING_TAIL);
288 ring->space = ring->head - (ring->tail + 8);
289 if (ring->space < 0)
290 ring->space += ring->Size;
291}
292
293static int i810_freelist_init(drm_device_t * dev, drm_i810_private_t * dev_priv)
294{
295 drm_device_dma_t *dma = dev->dma;
296 int my_idx = 24;
297 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
298 int i;
299
300 if (dma->buf_count > 1019) {
301 /* Not enough space in the status page for the freelist */
302 return -EINVAL;
303 }
304
305 for (i = 0; i < dma->buf_count; i++) {
306 drm_buf_t *buf = dma->buflist[i];
307 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
308
309 buf_priv->in_use = hw_status++;
310 buf_priv->my_use_idx = my_idx;
311 my_idx += 4;
312
313 *buf_priv->in_use = I810_BUF_FREE;
314
315 buf_priv->kernel_virtual = drm_ioremap(buf->bus_address,
316 buf->total, dev);
317 }
318 return 0;
319}
320
321static int i810_dma_initialize(drm_device_t * dev,
322 drm_i810_private_t * dev_priv,
323 drm_i810_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 struct list_head *list;
326
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 list_for_each(list, &dev->maplist->head) {
330 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
331 if (r_list->map &&
332 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 break;
336 }
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!dev_priv->sarea_map) {
339 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000340 i810_dma_cleanup(dev);
341 DRM_ERROR("can not find sarea!\n");
342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
345 if (!dev_priv->mmio_map) {
346 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 i810_dma_cleanup(dev);
348 DRM_ERROR("can not find mmio map!\n");
349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000351 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
353 if (!dev->agp_buffer_map) {
354 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 i810_dma_cleanup(dev);
356 DRM_ERROR("can not find dma buffer map!\n");
357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000363 dev_priv->ring.Start = init->ring_start;
364 dev_priv->ring.End = init->ring_end;
365 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000367 dev_priv->ring.virtual_start = drm_ioremap(dev->agp->base +
368 init->ring_start,
369 init->ring_size, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 if (dev_priv->ring.virtual_start == NULL) {
372 dev->dev_private = (void *)dev_priv;
373 i810_dma_cleanup(dev);
374 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 " ring buffer\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000379 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 dev_priv->w = init->w;
382 dev_priv->h = init->h;
383 dev_priv->pitch = init->pitch;
384 dev_priv->back_offset = init->back_offset;
385 dev_priv->depth_offset = init->depth_offset;
386 dev_priv->front_offset = init->front_offset;
387
388 dev_priv->overlay_offset = init->overlay_offset;
389 dev_priv->overlay_physical = init->overlay_physical;
390
391 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
392 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
393 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
394
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 /* Program Hardware Status Page */
396 dev_priv->hw_status_page =
397 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
398 &dev_priv->dma_status_page);
399 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dev->dev_private = (void *)dev_priv;
401 i810_dma_cleanup(dev);
402 DRM_ERROR("Can not allocate hardware status page\n");
403 return -ENOMEM;
404 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000405 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
406 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000409 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (i810_freelist_init(dev, dev_priv) != 0) {
413 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 i810_dma_cleanup(dev);
415 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 dev->dev_private = (void *)dev_priv;
420
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/* i810 DRM version 1.1 used a smaller init structure with different
425 * ordering of values than is currently used (drm >= 1.2). There is
426 * no defined way to detect the XFree version to correct this problem,
427 * however by checking using this procedure we can detect the correct
428 * thing to do.
429 *
430 * #1 Read the Smaller init structure from user-space
431 * #2 Verify the overlay_physical is a valid physical address, or NULL
432 * If it isn't then we have a v1.1 client. Fix up params.
433 * If it is, then we have a 1.2 client... get the rest of the data.
434 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435static int i810_dma_init_compat(drm_i810_init_t * init, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437
438 /* Get v1.1 init data */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000439 if (copy_from_user(init, (drm_i810_pre12_init_t __user *) arg,
440 sizeof(drm_i810_pre12_init_t))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return -EFAULT;
442 }
443
444 if ((!init->overlay_physical) || (init->overlay_physical > 4096)) {
445
446 /* This is a v1.2 client, just get the v1.2 init data */
447 DRM_INFO("Using POST v1.2 init.\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 if (copy_from_user(init, (drm_i810_init_t __user *) arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 sizeof(drm_i810_init_t))) {
450 return -EFAULT;
451 }
452 } else {
453
454 /* This is a v1.1 client, fix the params */
455 DRM_INFO("Using PRE v1.2 init.\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 init->pitch_bits = init->h;
457 init->pitch = init->w;
458 init->h = init->overlay_physical;
459 init->w = init->overlay_offset;
460 init->overlay_physical = 0;
461 init->overlay_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 return 0;
465}
466
467static int i810_dma_init(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470 drm_file_t *priv = filp->private_data;
471 drm_device_t *dev = priv->head->dev;
472 drm_i810_private_t *dev_priv;
473 drm_i810_init_t init;
474 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* Get only the init func */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 if (copy_from_user
478 (&init, (void __user *)arg, sizeof(drm_i810_init_func_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return -EFAULT;
480
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 switch (init.func) {
482 case I810_INIT_DMA:
483 /* This case is for backward compatibility. It
484 * handles XFree 4.1.0 and 4.2.0, and has to
485 * do some parameter checking as described below.
486 * It will someday go away.
487 */
488 retcode = i810_dma_init_compat(&init, arg);
489 if (retcode)
490 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 dev_priv = drm_alloc(sizeof(drm_i810_private_t),
493 DRM_MEM_DRIVER);
494 if (dev_priv == NULL)
495 return -ENOMEM;
496 retcode = i810_dma_initialize(dev, dev_priv, &init);
497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000499 default:
500 case I810_INIT_DMA_1_4:
501 DRM_INFO("Using v1.4 init.\n");
502 if (copy_from_user(&init, (drm_i810_init_t __user *) arg,
503 sizeof(drm_i810_init_t))) {
504 return -EFAULT;
505 }
506 dev_priv = drm_alloc(sizeof(drm_i810_private_t),
507 DRM_MEM_DRIVER);
508 if (dev_priv == NULL)
509 return -ENOMEM;
510 retcode = i810_dma_initialize(dev, dev_priv, &init);
511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000513 case I810_CLEANUP_DMA:
514 DRM_INFO("DMA Cleanup\n");
515 retcode = i810_dma_cleanup(dev);
516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/* Most efficient way to verify state for the i810 is as it is
523 * emitted. Non-conformant state is silently dropped.
524 *
525 * Use 'volatile' & local var tmp to force the emitted values to be
526 * identical to the verified ones.
527 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000528static void i810EmitContextVerified(drm_device_t * dev,
529 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int i, j = 0;
533 unsigned int tmp;
534 RING_LOCALS;
535
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000536 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 OUT_RING(GFX_OP_COLOR_FACTOR);
539 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 OUT_RING(GFX_OP_STIPPLE);
542 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000544 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 tmp = code[i];
546
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000547 if ((tmp & (7 << 29)) == (3 << 29) &&
548 (tmp & (0x1f << 24)) < (0x1d << 24)) {
549 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 } else
552 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
555 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000556 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 ADVANCE_LP_RING();
559}
560
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000561static void i810EmitTexVerified(drm_device_t * dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int i, j = 0;
565 unsigned int tmp;
566 RING_LOCALS;
567
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 OUT_RING(GFX_OP_MAP_INFO);
571 OUT_RING(code[I810_TEXREG_MI1]);
572 OUT_RING(code[I810_TEXREG_MI2]);
573 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 tmp = code[i];
577
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 if ((tmp & (7 << 29)) == (3 << 29) &&
579 (tmp & (0x1f << 24)) < (0x1d << 24)) {
580 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 } else
583 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 ADVANCE_LP_RING();
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/* Need to do some additional checking when setting the dest buffer.
593 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594static void i810EmitDestVerified(drm_device_t * dev,
595 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 unsigned int tmp;
599 RING_LOCALS;
600
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 tmp = code[I810_DESTREG_DI1];
604 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000605 OUT_RING(CMD_OP_DESTBUFFER_INFO);
606 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
609 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* invarient:
612 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 OUT_RING(CMD_OP_Z_BUFFER_INFO);
614 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000616 OUT_RING(GFX_OP_DESTBUFFER_VARS);
617 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 OUT_RING(GFX_OP_DRAWRECT_INFO);
620 OUT_RING(code[I810_DESTREG_DR1]);
621 OUT_RING(code[I810_DESTREG_DR2]);
622 OUT_RING(code[I810_DESTREG_DR3]);
623 OUT_RING(code[I810_DESTREG_DR4]);
624 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 ADVANCE_LP_RING();
627}
628
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629static void i810EmitState(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 drm_i810_private_t *dev_priv = dev->dev_private;
632 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 DRM_DEBUG("%s %x\n", __FUNCTION__, dirty);
636
637 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
640 }
641
642 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
645 }
646
647 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
650 }
651
652 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
655 }
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/* need to verify
659 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660static void i810_dma_dispatch_clear(drm_device_t * dev, int flags,
661 unsigned int clear_color,
662 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000664 drm_i810_private_t *dev_priv = dev->dev_private;
665 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int nbox = sarea_priv->nbox;
667 drm_clip_rect_t *pbox = sarea_priv->boxes;
668 int pitch = dev_priv->pitch;
669 int cpp = 2;
670 int i;
671 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000672
673 if (dev_priv->current_page == 1) {
674 unsigned int tmp = flags;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 if (tmp & I810_FRONT)
678 flags |= I810_BACK;
679 if (tmp & I810_BACK)
680 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685 if (nbox > I810_NR_SAREA_CLIPRECTS)
686 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned int x = pbox->x1;
690 unsigned int y = pbox->y1;
691 unsigned int width = (pbox->x2 - x) * cpp;
692 unsigned int height = pbox->y2 - y;
693 unsigned int start = y * pitch + x * cpp;
694
695 if (pbox->x1 > pbox->x2 ||
696 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 continue;
699
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 if (flags & I810_FRONT) {
701 BEGIN_LP_RING(6);
702 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
703 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
704 OUT_RING((height << 16) | width);
705 OUT_RING(start);
706 OUT_RING(clear_color);
707 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 ADVANCE_LP_RING();
709 }
710
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 if (flags & I810_BACK) {
712 BEGIN_LP_RING(6);
713 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
714 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
715 OUT_RING((height << 16) | width);
716 OUT_RING(dev_priv->back_offset + start);
717 OUT_RING(clear_color);
718 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 ADVANCE_LP_RING();
720 }
721
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 if (flags & I810_DEPTH) {
723 BEGIN_LP_RING(6);
724 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
725 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
726 OUT_RING((height << 16) | width);
727 OUT_RING(dev_priv->depth_offset + start);
728 OUT_RING(clear_zval);
729 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ADVANCE_LP_RING();
731 }
732 }
733}
734
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000735static void i810_dma_dispatch_swap(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 drm_i810_private_t *dev_priv = dev->dev_private;
738 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int nbox = sarea_priv->nbox;
740 drm_clip_rect_t *pbox = sarea_priv->boxes;
741 int pitch = dev_priv->pitch;
742 int cpp = 2;
743 int i;
744 RING_LOCALS;
745
746 DRM_DEBUG("swapbuffers\n");
747
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 if (nbox > I810_NR_SAREA_CLIPRECTS)
751 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000753 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 unsigned int w = pbox->x2 - pbox->x1;
755 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 unsigned int start = dst;
758
759 if (pbox->x1 > pbox->x2 ||
760 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 continue;
763
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000764 BEGIN_LP_RING(6);
765 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
766 OUT_RING(pitch | (0xCC << 16));
767 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000771 OUT_RING(dev_priv->back_offset + start);
772 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ADVANCE_LP_RING();
778 }
779}
780
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781static void i810_dma_dispatch_vertex(drm_device_t * dev,
782 drm_buf_t * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
787 drm_clip_rect_t *box = sarea_priv->boxes;
788 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 unsigned long address = (unsigned long)buf->bus_address;
790 unsigned long start = address - dev->agp->base;
791 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 nbox = I810_NR_SAREA_CLIPRECTS;
798
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 used = 0;
801
802 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
806 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
807
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 *(u32 *) buf_priv->kernel_virtual =
809 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (used & 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 *(u32 *) ((u32) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 used += 4;
814 }
815
816 i810_unmap_buffer(buf);
817 }
818
819 if (used) {
820 do {
821 if (i < nbox) {
822 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
824 SC_ENABLE);
825 OUT_RING(GFX_OP_SCISSOR_INFO);
826 OUT_RING(box[i].x1 | (box[i].y1 << 16));
827 OUT_RING((box[i].x2 -
828 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ADVANCE_LP_RING();
830 }
831
832 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 OUT_RING(CMD_OP_BATCH_BUFFER);
834 OUT_RING(start | BB1_PROTECTED);
835 OUT_RING(start + used - 4);
836 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 ADVANCE_LP_RING();
838
839 } while (++i < nbox);
840 }
841
842 if (discard) {
843 dev_priv->counter++;
844
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
846 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 OUT_RING(CMD_STORE_DWORD_IDX);
850 OUT_RING(20);
851 OUT_RING(dev_priv->counter);
852 OUT_RING(CMD_STORE_DWORD_IDX);
853 OUT_RING(buf_priv->my_use_idx);
854 OUT_RING(I810_BUF_FREE);
855 OUT_RING(CMD_REPORT_HEAD);
856 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 ADVANCE_LP_RING();
858 }
859}
860
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861static void i810_dma_dispatch_flip(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 int pitch = dev_priv->pitch;
865 RING_LOCALS;
866
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
868 __FUNCTION__,
869 dev_priv->current_page,
870 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000872 i810_kernel_lost_context(dev);
873
874 BEGIN_LP_RING(2);
875 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
876 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ADVANCE_LP_RING();
878
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* On i815 at least ASYNC is buggy */
881 /* pitch<<5 is from 11.2.8 p158,
882 its the pitch / 8 then left shifted 8,
883 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000884 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
885 if (dev_priv->current_page == 0) {
886 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 dev_priv->current_page = 1;
888 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 dev_priv->current_page = 0;
891 }
892 OUT_RING(0);
893 ADVANCE_LP_RING();
894
895 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
897 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 ADVANCE_LP_RING();
899
900 /* Increment the frame counter. The client-side 3D driver must
901 * throttle the framerate by waiting for this value before
902 * performing the swapbuffer ioctl.
903 */
904 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
905
906}
907
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908static void i810_dma_quiescent(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 drm_i810_private_t *dev_priv = dev->dev_private;
911 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913/* printk("%s\n", __FUNCTION__); */
914
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 BEGIN_LP_RING(4);
918 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
919 OUT_RING(CMD_REPORT_HEAD);
920 OUT_RING(0);
921 OUT_RING(0);
922 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927static int i810_flush_queue(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 int i, ret = 0;
932 RING_LOCALS;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/* printk("%s\n", __FUNCTION__); */
935
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 BEGIN_LP_RING(2);
939 OUT_RING(CMD_REPORT_HEAD);
940 OUT_RING(0);
941 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000943 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000945 for (i = 0; i < dma->buf_count; i++) {
946 drm_buf_t *buf = dma->buflist[i];
947 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
950 I810_BUF_FREE);
951
952 if (used == I810_BUF_HARDWARE)
953 DRM_DEBUG("reclaimed from HARDWARE\n");
954 if (used == I810_BUF_CLIENT)
955 DRM_DEBUG("still on client\n");
956 }
957
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000958 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/* Must be called with the lock held */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000962void i810_reclaim_buffers(drm_device_t * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000965 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 if (!dma)
968 return;
969 if (!dev->dev_private)
970 return;
971 if (!dma->buflist)
972 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 for (i = 0; i < dma->buf_count; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000977 drm_buf_t *buf = dma->buflist[i];
978 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (buf->filp == filp && buf_priv) {
981 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
982 I810_BUF_FREE);
983
984 if (used == I810_BUF_CLIENT)
985 DRM_DEBUG("reclaimed from client\n");
986 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 }
990}
991
Dave Airliec94f7022005-07-07 21:03:38 +1000992static int i810_flush_ioctl(struct inode *inode, struct file *filp,
993 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 drm_file_t *priv = filp->private_data;
996 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 LOCK_TEST_WITH_RETURN(dev, filp);
999
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 i810_flush_queue(dev);
1001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004static int i810_dma_vertex(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 drm_file_t *priv = filp->private_data;
1008 drm_device_t *dev = priv->head->dev;
1009 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1011 u32 *hw_status = dev_priv->hw_status_page;
1012 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1013 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 drm_i810_vertex_t vertex;
1015
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 if (copy_from_user
1017 (&vertex, (drm_i810_vertex_t __user *) arg, sizeof(vertex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return -EFAULT;
1019
1020 LOCK_TEST_WITH_RETURN(dev, filp);
1021
1022 DRM_DEBUG("i810 dma vertex, idx %d used %d discard %d\n",
1023 vertex.idx, vertex.used, vertex.discard);
1024
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 if (vertex.idx < 0 || vertex.idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EINVAL;
1027
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 i810_dma_dispatch_vertex(dev,
1029 dma->buflist[vertex.idx],
1030 vertex.discard, vertex.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 atomic_add(vertex.used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 sarea_priv->last_enqueue = dev_priv->counter - 1;
1035 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 return 0;
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040static int i810_clear_bufs(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 drm_file_t *priv = filp->private_data;
1044 drm_device_t *dev = priv->head->dev;
1045 drm_i810_clear_t clear;
1046
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 if (copy_from_user
1048 (&clear, (drm_i810_clear_t __user *) arg, sizeof(clear)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EFAULT;
1050
1051 LOCK_TEST_WITH_RETURN(dev, filp);
1052
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 /* GH: Someone's doing nasty things... */
1054 if (!dev->dev_private) {
1055 return -EINVAL;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 i810_dma_dispatch_clear(dev, clear.flags,
1059 clear.clear_color, clear.clear_depth);
1060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
1063static int i810_swap_bufs(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001064 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 drm_file_t *priv = filp->private_data;
1067 drm_device_t *dev = priv->head->dev;
1068
1069 DRM_DEBUG("i810_swap_bufs\n");
1070
1071 LOCK_TEST_WITH_RETURN(dev, filp);
1072
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 i810_dma_dispatch_swap(dev);
1074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077static int i810_getage(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 drm_file_t *priv = filp->private_data;
1081 drm_device_t *dev = priv->head->dev;
1082 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1083 u32 *hw_status = dev_priv->hw_status_page;
1084 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1085 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return 0;
1089}
1090
1091static int i810_getbuf(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 drm_file_t *priv = filp->private_data;
1095 drm_device_t *dev = priv->head->dev;
1096 int retcode = 0;
1097 drm_i810_dma_t d;
1098 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1099 u32 *hw_status = dev_priv->hw_status_page;
1100 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1101 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 if (copy_from_user(&d, (drm_i810_dma_t __user *) arg, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -EFAULT;
1105
1106 LOCK_TEST_WITH_RETURN(dev, filp);
1107
1108 d.granted = 0;
1109
1110 retcode = i810_dma_get_buffer(dev, &d, filp);
1111
1112 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
1113 current->pid, retcode, d.granted);
1114
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001115 if (copy_to_user((drm_dma_t __user *) arg, &d, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001117 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 return retcode;
1120}
1121
1122static int i810_copybuf(struct inode *inode,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 /* Never copy - 2.4.x doesn't need it */
1126 return 0;
1127}
1128
1129static int i810_docopy(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 /* Never copy - 2.4.x doesn't need it */
1133 return 0;
1134}
1135
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001136static void i810_dma_dispatch_mc(drm_device_t * dev, drm_buf_t * buf, int used,
1137 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 drm_i810_private_t *dev_priv = dev->dev_private;
1140 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1141 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1142 unsigned long address = (unsigned long)buf->bus_address;
1143 unsigned long start = address - dev->agp->base;
1144 int u;
1145 RING_LOCALS;
1146
1147 i810_kernel_lost_context(dev);
1148
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001149 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (u != I810_BUF_CLIENT) {
1151 DRM_DEBUG("MC found buffer that isn't mine!\n");
1152 }
1153
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001154 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 used = 0;
1156
1157 sarea_priv->dirty = 0x7f;
1158
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001159 DRM_DEBUG("dispatch mc addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 dev_priv->counter++;
1162 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1163 DRM_DEBUG("i810_dma_dispatch_mc\n");
1164 DRM_DEBUG("start : %lx\n", start);
1165 DRM_DEBUG("used : %d\n", used);
1166 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1167
1168 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1169 if (used & 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001170 *(u32 *) ((u32) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 used += 4;
1172 }
1173
1174 i810_unmap_buffer(buf);
1175 }
1176 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001177 OUT_RING(CMD_OP_BATCH_BUFFER);
1178 OUT_RING(start | BB1_PROTECTED);
1179 OUT_RING(start + used - 4);
1180 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 ADVANCE_LP_RING();
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001184 OUT_RING(CMD_STORE_DWORD_IDX);
1185 OUT_RING(buf_priv->my_use_idx);
1186 OUT_RING(I810_BUF_FREE);
1187 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001189 OUT_RING(CMD_STORE_DWORD_IDX);
1190 OUT_RING(16);
1191 OUT_RING(last_render);
1192 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 ADVANCE_LP_RING();
1194}
1195
1196static int i810_dma_mc(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001197 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 drm_file_t *priv = filp->private_data;
1200 drm_device_t *dev = priv->head->dev;
1201 drm_device_dma_t *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001202 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 u32 *hw_status = dev_priv->hw_status_page;
1204 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001205 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 drm_i810_mc_t mc;
1207
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001208 if (copy_from_user(&mc, (drm_i810_mc_t __user *) arg, sizeof(mc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -EFAULT;
1210
1211 LOCK_TEST_WITH_RETURN(dev, filp);
1212
1213 if (mc.idx >= dma->buf_count || mc.idx < 0)
1214 return -EINVAL;
1215
1216 i810_dma_dispatch_mc(dev, dma->buflist[mc.idx], mc.used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001217 mc.last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 atomic_add(mc.used, &dev->counts[_DRM_STAT_SECONDARY]);
1220 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001221 sarea_priv->last_enqueue = dev_priv->counter - 1;
1222 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 return 0;
1225}
1226
1227static int i810_rstatus(struct inode *inode, struct file *filp,
1228 unsigned int cmd, unsigned long arg)
1229{
1230 drm_file_t *priv = filp->private_data;
1231 drm_device_t *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001232 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001234 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237static int i810_ov0_info(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001238 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 drm_file_t *priv = filp->private_data;
1241 drm_device_t *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001242 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 drm_i810_overlay_t data;
1244
1245 data.offset = dev_priv->overlay_offset;
1246 data.physical = dev_priv->overlay_physical;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001247 if (copy_to_user
1248 ((drm_i810_overlay_t __user *) arg, &data, sizeof(data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -EFAULT;
1250 return 0;
1251}
1252
1253static int i810_fstatus(struct inode *inode, struct file *filp,
1254 unsigned int cmd, unsigned long arg)
1255{
1256 drm_file_t *priv = filp->private_data;
1257 drm_device_t *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001258 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 LOCK_TEST_WITH_RETURN(dev, filp);
1261
1262 return I810_READ(0x30008);
1263}
1264
1265static int i810_ov0_flip(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001266 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 drm_file_t *priv = filp->private_data;
1269 drm_device_t *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001270 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 LOCK_TEST_WITH_RETURN(dev, filp);
1273
1274 //Tell the overlay to update
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001275 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 return 0;
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001281 */
1282static void i810_do_init_pageflip(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 DRM_DEBUG("%s\n", __FUNCTION__);
1287 dev_priv->page_flipping = 1;
1288 dev_priv->current_page = 0;
1289 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1290}
1291
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001292static int i810_do_cleanup_pageflip(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 drm_i810_private_t *dev_priv = dev->dev_private;
1295
1296 DRM_DEBUG("%s\n", __FUNCTION__);
1297 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001298 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 dev_priv->page_flipping = 0;
1301 return 0;
1302}
1303
1304static int i810_flip_bufs(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 drm_file_t *priv = filp->private_data;
1308 drm_device_t *dev = priv->head->dev;
1309 drm_i810_private_t *dev_priv = dev->dev_private;
1310
1311 DRM_DEBUG("%s\n", __FUNCTION__);
1312
1313 LOCK_TEST_WITH_RETURN(dev, filp);
1314
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001315 if (!dev_priv->page_flipping)
1316 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001318 i810_dma_dispatch_flip(dev);
1319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001322void i810_driver_pretakedown(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001324 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001327void i810_driver_prerelease(drm_device_t * dev, DRMFILE filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 if (dev->dev_private) {
1330 drm_i810_private_t *dev_priv = dev->dev_private;
1331 if (dev_priv->page_flipping) {
1332 i810_do_cleanup_pageflip(dev);
1333 }
1334 }
1335}
1336
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001337void i810_driver_release(drm_device_t * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 i810_reclaim_buffers(dev, filp);
1340}
1341
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001342int i810_driver_dma_quiescent(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001344 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return 0;
1346}
1347
1348drm_ioctl_desc_t i810_ioctls[] = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001349 [DRM_IOCTL_NR(DRM_I810_INIT)] = {i810_dma_init, 1, 1}
1350 ,
1351 [DRM_IOCTL_NR(DRM_I810_VERTEX)] = {i810_dma_vertex, 1, 0}
1352 ,
1353 [DRM_IOCTL_NR(DRM_I810_CLEAR)] = {i810_clear_bufs, 1, 0}
1354 ,
1355 [DRM_IOCTL_NR(DRM_I810_FLUSH)] = {i810_flush_ioctl, 1, 0}
1356 ,
1357 [DRM_IOCTL_NR(DRM_I810_GETAGE)] = {i810_getage, 1, 0}
1358 ,
1359 [DRM_IOCTL_NR(DRM_I810_GETBUF)] = {i810_getbuf, 1, 0}
1360 ,
1361 [DRM_IOCTL_NR(DRM_I810_SWAP)] = {i810_swap_bufs, 1, 0}
1362 ,
1363 [DRM_IOCTL_NR(DRM_I810_COPY)] = {i810_copybuf, 1, 0}
1364 ,
1365 [DRM_IOCTL_NR(DRM_I810_DOCOPY)] = {i810_docopy, 1, 0}
1366 ,
1367 [DRM_IOCTL_NR(DRM_I810_OV0INFO)] = {i810_ov0_info, 1, 0}
1368 ,
1369 [DRM_IOCTL_NR(DRM_I810_FSTATUS)] = {i810_fstatus, 1, 0}
1370 ,
1371 [DRM_IOCTL_NR(DRM_I810_OV0FLIP)] = {i810_ov0_flip, 1, 0}
1372 ,
1373 [DRM_IOCTL_NR(DRM_I810_MC)] = {i810_dma_mc, 1, 1}
1374 ,
1375 [DRM_IOCTL_NR(DRM_I810_RSTATUS)] = {i810_rstatus, 1, 0}
1376 ,
1377 [DRM_IOCTL_NR(DRM_I810_FLIP)] = {i810_flip_bufs, 1, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378};
1379
1380int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001381
1382/**
1383 * Determine if the device really is AGP or not.
1384 *
1385 * All Intel graphics chipsets are treated as AGP, even if they are really
1386 * PCI-e.
1387 *
1388 * \param dev The device to be tested.
1389 *
1390 * \returns
1391 * A value of 1 is always retured to indictate every i810 is AGP.
1392 */
1393int i810_driver_device_is_agp(drm_device_t * dev)
1394{
1395 return 1;
1396}