blob: 3f1aca87432f707de1b5c36cdaeefc4ebc95af58 [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 Airlie056219e2007-07-11 16:17:42 +100048static struct drm_buf *i810_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Dave Airliecdd55a22007-07-11 16:32:08 +100050 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100051 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++) {
Dave Airlie056219e2007-07-11 16:17:42 +100057 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 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 Airlie056219e2007-07-11 16:17:42 +100073static int i810_freelist_put(struct drm_device * dev, struct drm_buf * 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 Airlieeddca552007-07-11 16:09:54 +100090 struct drm_file *priv = filp->private_data;
91 struct drm_device *dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 drm_i810_private_t *dev_priv;
Dave Airlie056219e2007-07-11 16:17:42 +100093 struct drm_buf *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 Airlie3d774612006-08-07 20:07:43 +1000109 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110 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
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800115static const struct file_operations i810_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000117 .release = drm_release,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000118 .ioctl = drm_ioctl,
119 .mmap = i810_mmap_buffers,
120 .fasync = drm_fasync,
Dave Airliec94f7022005-07-07 21:03:38 +1000121};
122
Eric Anholt6c340ea2007-08-25 20:23:09 +1000123static int i810_map_buffer(struct drm_buf * buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000125 struct drm_device *dev = file_priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800128 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int retcode = 0;
130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -EINVAL;
133
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000134 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000135 old_fops = file_priv->filp->f_op;
136 file_priv->filp->f_op = &i810_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 dev_priv->mmap_buffer = buf;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000138 buf_priv->virtual = (void *)do_mmap(file_priv->filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139 PROT_READ | PROT_WRITE,
140 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000142 file_priv->filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000143 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* Real error */
145 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000146 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 buf_priv->virtual = NULL;
148 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000149 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return retcode;
152}
153
Dave Airlie056219e2007-07-11 16:17:42 +1000154static int i810_unmap_buffer(struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
157 int retcode = 0;
158
159 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
160 return -EINVAL;
161
162 down_write(&current->mm->mmap_sem);
163 retcode = do_munmap(current->mm,
164 (unsigned long)buf_priv->virtual,
165 (size_t) buf->total);
166 up_write(&current->mm->mmap_sem);
167
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
169 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return retcode;
172}
173
Dave Airlieeddca552007-07-11 16:09:54 +1000174static int i810_dma_get_buffer(struct drm_device * dev, drm_i810_dma_t * d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000175 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Dave Airlie056219e2007-07-11 16:17:42 +1000177 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 drm_i810_buf_priv_t *buf_priv;
179 int retcode = 0;
180
181 buf = i810_freelist_get(dev);
182 if (!buf) {
183 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return retcode;
186 }
187
Eric Anholt6c340ea2007-08-25 20:23:09 +1000188 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (retcode) {
190 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return retcode;
193 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000194 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 buf_priv = buf->dev_private;
196 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197 d->request_idx = buf->idx;
198 d->request_size = buf->total;
199 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 return retcode;
202}
203
Dave Airlieeddca552007-07-11 16:09:54 +1000204static int i810_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Dave Airliecdd55a22007-07-11 16:32:08 +1000206 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* Make sure interrupts are disabled here because the uninstall ioctl
209 * may not have been called from userspace and after dev_private
210 * is freed, it's too late.
211 */
212 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
213 drm_irq_uninstall(dev);
214
215 if (dev->dev_private) {
216 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 drm_i810_private_t *dev_priv =
218 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (dev_priv->ring.virtual_start) {
Dave Airlieb9094d32007-01-08 21:31:13 +1100221 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000223 if (dev_priv->hw_status_page) {
224 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dev_priv->hw_status_page,
226 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227 /* Need to rewrite hardware status page */
228 I810_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000230 drm_free(dev->dev_private, sizeof(drm_i810_private_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 DRM_MEM_DRIVER);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000235 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100237
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100239 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243}
244
Dave Airlieeddca552007-07-11 16:09:54 +1000245static int i810_wait_ring(struct drm_device * dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246{
247 drm_i810_private_t *dev_priv = dev->dev_private;
248 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
249 int iters = 0;
250 unsigned long end;
251 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
252
253 end = jiffies + (HZ * 3);
254 while (ring->space < n) {
255 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
256 ring->space = ring->head - (ring->tail + 8);
257 if (ring->space < 0)
258 ring->space += ring->Size;
259
260 if (ring->head != last_head) {
261 end = jiffies + (HZ * 3);
262 last_head = ring->head;
263 }
264
265 iters++;
266 if (time_before(end, jiffies)) {
267 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
268 DRM_ERROR("lockup\n");
269 goto out_wait_ring;
270 }
271 udelay(1);
272 }
273
274 out_wait_ring:
275 return iters;
276}
277
Dave Airlieeddca552007-07-11 16:09:54 +1000278static void i810_kernel_lost_context(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279{
280 drm_i810_private_t *dev_priv = dev->dev_private;
281 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
282
283 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
284 ring->tail = I810_READ(LP_RING + RING_TAIL);
285 ring->space = ring->head - (ring->tail + 8);
286 if (ring->space < 0)
287 ring->space += ring->Size;
288}
289
Dave Airlieeddca552007-07-11 16:09:54 +1000290static int i810_freelist_init(struct drm_device * dev, drm_i810_private_t * dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291{
Dave Airliecdd55a22007-07-11 16:32:08 +1000292 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 int my_idx = 24;
294 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
295 int i;
296
297 if (dma->buf_count > 1019) {
298 /* Not enough space in the status page for the freelist */
299 return -EINVAL;
300 }
301
302 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000303 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000304 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
305
306 buf_priv->in_use = hw_status++;
307 buf_priv->my_use_idx = my_idx;
308 my_idx += 4;
309
310 *buf_priv->in_use = I810_BUF_FREE;
311
Dave Airlieb9094d32007-01-08 21:31:13 +1100312 buf_priv->map.offset = buf->bus_address;
313 buf_priv->map.size = buf->total;
314 buf_priv->map.type = _DRM_AGP;
315 buf_priv->map.flags = 0;
316 buf_priv->map.mtrr = 0;
317
318 drm_core_ioremap(&buf_priv->map, dev);
319 buf_priv->kernel_virtual = buf_priv->map.handle;
320
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 }
322 return 0;
323}
324
Dave Airlieeddca552007-07-11 16:09:54 +1000325static int i810_dma_initialize(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326 drm_i810_private_t * dev_priv,
327 drm_i810_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Dave Airlie55910512007-07-11 16:53:40 +1000329 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000330 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dave Airliebd1b3312007-05-26 05:01:51 +1000332 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (r_list->map &&
334 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 break;
338 }
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!dev_priv->sarea_map) {
341 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 i810_dma_cleanup(dev);
343 DRM_ERROR("can not find sarea!\n");
344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
347 if (!dev_priv->mmio_map) {
348 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 i810_dma_cleanup(dev);
350 DRM_ERROR("can not find mmio map!\n");
351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000353 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
355 if (!dev->agp_buffer_map) {
356 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000357 i810_dma_cleanup(dev);
358 DRM_ERROR("can not find dma buffer map!\n");
359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
362 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000363 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 dev_priv->ring.Start = init->ring_start;
366 dev_priv->ring.End = init->ring_end;
367 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Dave Airlieb9094d32007-01-08 21:31:13 +1100369 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
370 dev_priv->ring.map.size = init->ring_size;
371 dev_priv->ring.map.type = _DRM_AGP;
372 dev_priv->ring.map.flags = 0;
373 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dave Airlieb9094d32007-01-08 21:31:13 +1100375 drm_core_ioremap(&dev_priv->ring.map, dev);
376
377 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000378 dev->dev_private = (void *)dev_priv;
379 i810_dma_cleanup(dev);
380 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000382 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
Dave Airlieb9094d32007-01-08 21:31:13 +1100385 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
386
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 dev_priv->w = init->w;
390 dev_priv->h = init->h;
391 dev_priv->pitch = init->pitch;
392 dev_priv->back_offset = init->back_offset;
393 dev_priv->depth_offset = init->depth_offset;
394 dev_priv->front_offset = init->front_offset;
395
396 dev_priv->overlay_offset = init->overlay_offset;
397 dev_priv->overlay_physical = init->overlay_physical;
398
399 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
400 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
401 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
402
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403 /* Program Hardware Status Page */
404 dev_priv->hw_status_page =
405 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
406 &dev_priv->dma_status_page);
407 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 dev->dev_private = (void *)dev_priv;
409 i810_dma_cleanup(dev);
410 DRM_ERROR("Can not allocate hardware status page\n");
411 return -ENOMEM;
412 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000413 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
414 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (i810_freelist_init(dev, dev_priv) != 0) {
421 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 i810_dma_cleanup(dev);
423 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000425 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 dev->dev_private = (void *)dev_priv;
428
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432/* i810 DRM version 1.1 used a smaller init structure with different
433 * ordering of values than is currently used (drm >= 1.2). There is
434 * no defined way to detect the XFree version to correct this problem,
435 * however by checking using this procedure we can detect the correct
436 * thing to do.
437 *
438 * #1 Read the Smaller init structure from user-space
439 * #2 Verify the overlay_physical is a valid physical address, or NULL
440 * If it isn't then we have a v1.1 client. Fix up params.
441 * If it is, then we have a 1.2 client... get the rest of the data.
442 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443static int i810_dma_init_compat(drm_i810_init_t * init, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445
446 /* Get v1.1 init data */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 if (copy_from_user(init, (drm_i810_pre12_init_t __user *) arg,
448 sizeof(drm_i810_pre12_init_t))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -EFAULT;
450 }
451
452 if ((!init->overlay_physical) || (init->overlay_physical > 4096)) {
453
454 /* This is a v1.2 client, just get the v1.2 init data */
455 DRM_INFO("Using POST v1.2 init.\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 if (copy_from_user(init, (drm_i810_init_t __user *) arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 sizeof(drm_i810_init_t))) {
458 return -EFAULT;
459 }
460 } else {
461
462 /* This is a v1.1 client, fix the params */
463 DRM_INFO("Using PRE v1.2 init.\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000464 init->pitch_bits = init->h;
465 init->pitch = init->w;
466 init->h = init->overlay_physical;
467 init->w = init->overlay_offset;
468 init->overlay_physical = 0;
469 init->overlay_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
472 return 0;
473}
474
Eric Anholt6c340ea2007-08-25 20:23:09 +1000475static int i810_dma_init(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000478 struct drm_device *dev = file_priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000479 drm_i810_private_t *dev_priv;
480 drm_i810_init_t init;
481 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* Get only the init func */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000484 if (copy_from_user
485 (&init, (void __user *)arg, sizeof(drm_i810_init_func_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EFAULT;
487
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 switch (init.func) {
489 case I810_INIT_DMA:
490 /* This case is for backward compatibility. It
491 * handles XFree 4.1.0 and 4.2.0, and has to
492 * do some parameter checking as described below.
493 * It will someday go away.
494 */
495 retcode = i810_dma_init_compat(&init, arg);
496 if (retcode)
497 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000499 dev_priv = drm_alloc(sizeof(drm_i810_private_t),
500 DRM_MEM_DRIVER);
501 if (dev_priv == NULL)
502 return -ENOMEM;
503 retcode = i810_dma_initialize(dev, dev_priv, &init);
504 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000506 default:
507 case I810_INIT_DMA_1_4:
508 DRM_INFO("Using v1.4 init.\n");
509 if (copy_from_user(&init, (drm_i810_init_t __user *) arg,
510 sizeof(drm_i810_init_t))) {
511 return -EFAULT;
512 }
513 dev_priv = drm_alloc(sizeof(drm_i810_private_t),
514 DRM_MEM_DRIVER);
515 if (dev_priv == NULL)
516 return -ENOMEM;
517 retcode = i810_dma_initialize(dev, dev_priv, &init);
518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 case I810_CLEANUP_DMA:
521 DRM_INFO("DMA Cleanup\n");
522 retcode = i810_dma_cleanup(dev);
523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000526 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/* Most efficient way to verify state for the i810 is as it is
530 * emitted. Non-conformant state is silently dropped.
531 *
532 * Use 'volatile' & local var tmp to force the emitted values to be
533 * identical to the verified ones.
534 */
Dave Airlieeddca552007-07-11 16:09:54 +1000535static void i810EmitContextVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000536 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int i, j = 0;
540 unsigned int tmp;
541 RING_LOCALS;
542
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 OUT_RING(GFX_OP_COLOR_FACTOR);
546 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 OUT_RING(GFX_OP_STIPPLE);
549 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 tmp = code[i];
553
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 if ((tmp & (7 << 29)) == (3 << 29) &&
555 (tmp & (0x1f << 24)) < (0x1d << 24)) {
556 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000558 } else
559 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 ADVANCE_LP_RING();
566}
567
Dave Airlieeddca552007-07-11 16:09:54 +1000568static void i810EmitTexVerified(struct drm_device * dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int i, j = 0;
572 unsigned int tmp;
573 RING_LOCALS;
574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 OUT_RING(GFX_OP_MAP_INFO);
578 OUT_RING(code[I810_TEXREG_MI1]);
579 OUT_RING(code[I810_TEXREG_MI2]);
580 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 tmp = code[i];
584
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000585 if ((tmp & (7 << 29)) == (3 << 29) &&
586 (tmp & (0x1f << 24)) < (0x1d << 24)) {
587 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 } else
590 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
593 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 ADVANCE_LP_RING();
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/* Need to do some additional checking when setting the dest buffer.
600 */
Dave Airlieeddca552007-07-11 16:09:54 +1000601static void i810EmitDestVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000604 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 unsigned int tmp;
606 RING_LOCALS;
607
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 tmp = code[I810_DESTREG_DI1];
611 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000612 OUT_RING(CMD_OP_DESTBUFFER_INFO);
613 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
616 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* invarient:
619 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 OUT_RING(CMD_OP_Z_BUFFER_INFO);
621 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623 OUT_RING(GFX_OP_DESTBUFFER_VARS);
624 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000626 OUT_RING(GFX_OP_DRAWRECT_INFO);
627 OUT_RING(code[I810_DESTREG_DR1]);
628 OUT_RING(code[I810_DESTREG_DR2]);
629 OUT_RING(code[I810_DESTREG_DR3]);
630 OUT_RING(code[I810_DESTREG_DR4]);
631 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 ADVANCE_LP_RING();
634}
635
Dave Airlieeddca552007-07-11 16:09:54 +1000636static void i810EmitState(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 drm_i810_private_t *dev_priv = dev->dev_private;
639 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 DRM_DEBUG("%s %x\n", __FUNCTION__, dirty);
643
644 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000645 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
647 }
648
649 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
652 }
653
654 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
657 }
658
659 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
662 }
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/* need to verify
666 */
Dave Airlieeddca552007-07-11 16:09:54 +1000667static void i810_dma_dispatch_clear(struct drm_device * dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000668 unsigned int clear_color,
669 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000671 drm_i810_private_t *dev_priv = dev->dev_private;
672 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000674 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int pitch = dev_priv->pitch;
676 int cpp = 2;
677 int i;
678 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000679
680 if (dev_priv->current_page == 1) {
681 unsigned int tmp = flags;
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 if (tmp & I810_FRONT)
685 flags |= I810_BACK;
686 if (tmp & I810_BACK)
687 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 if (nbox > I810_NR_SAREA_CLIPRECTS)
693 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 unsigned int x = pbox->x1;
697 unsigned int y = pbox->y1;
698 unsigned int width = (pbox->x2 - x) * cpp;
699 unsigned int height = pbox->y2 - y;
700 unsigned int start = y * pitch + x * cpp;
701
702 if (pbox->x1 > pbox->x2 ||
703 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 continue;
706
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 if (flags & I810_FRONT) {
708 BEGIN_LP_RING(6);
709 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
710 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
711 OUT_RING((height << 16) | width);
712 OUT_RING(start);
713 OUT_RING(clear_color);
714 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 ADVANCE_LP_RING();
716 }
717
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 if (flags & I810_BACK) {
719 BEGIN_LP_RING(6);
720 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
721 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
722 OUT_RING((height << 16) | width);
723 OUT_RING(dev_priv->back_offset + start);
724 OUT_RING(clear_color);
725 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 ADVANCE_LP_RING();
727 }
728
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 if (flags & I810_DEPTH) {
730 BEGIN_LP_RING(6);
731 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
732 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
733 OUT_RING((height << 16) | width);
734 OUT_RING(dev_priv->depth_offset + start);
735 OUT_RING(clear_zval);
736 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ADVANCE_LP_RING();
738 }
739 }
740}
741
Dave Airlieeddca552007-07-11 16:09:54 +1000742static void i810_dma_dispatch_swap(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 drm_i810_private_t *dev_priv = dev->dev_private;
745 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000747 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int pitch = dev_priv->pitch;
749 int cpp = 2;
750 int i;
751 RING_LOCALS;
752
753 DRM_DEBUG("swapbuffers\n");
754
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 if (nbox > I810_NR_SAREA_CLIPRECTS)
758 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 unsigned int w = pbox->x2 - pbox->x1;
762 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 unsigned int start = dst;
765
766 if (pbox->x1 > pbox->x2 ||
767 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000768 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 continue;
770
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000771 BEGIN_LP_RING(6);
772 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
773 OUT_RING(pitch | (0xCC << 16));
774 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778 OUT_RING(dev_priv->back_offset + start);
779 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 ADVANCE_LP_RING();
785 }
786}
787
Dave Airlieeddca552007-07-11 16:09:54 +1000788static void i810_dma_dispatch_vertex(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +1000789 struct drm_buf * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000794 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 unsigned long address = (unsigned long)buf->bus_address;
797 unsigned long start = address - dev->agp->base;
798 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 nbox = I810_NR_SAREA_CLIPRECTS;
805
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 used = 0;
808
809 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
813 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
814
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 *(u32 *) buf_priv->kernel_virtual =
816 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000819 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 used += 4;
821 }
822
823 i810_unmap_buffer(buf);
824 }
825
826 if (used) {
827 do {
828 if (i < nbox) {
829 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
831 SC_ENABLE);
832 OUT_RING(GFX_OP_SCISSOR_INFO);
833 OUT_RING(box[i].x1 | (box[i].y1 << 16));
834 OUT_RING((box[i].x2 -
835 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ADVANCE_LP_RING();
837 }
838
839 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 OUT_RING(CMD_OP_BATCH_BUFFER);
841 OUT_RING(start | BB1_PROTECTED);
842 OUT_RING(start + used - 4);
843 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ADVANCE_LP_RING();
845
846 } while (++i < nbox);
847 }
848
849 if (discard) {
850 dev_priv->counter++;
851
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
853 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 OUT_RING(CMD_STORE_DWORD_IDX);
857 OUT_RING(20);
858 OUT_RING(dev_priv->counter);
859 OUT_RING(CMD_STORE_DWORD_IDX);
860 OUT_RING(buf_priv->my_use_idx);
861 OUT_RING(I810_BUF_FREE);
862 OUT_RING(CMD_REPORT_HEAD);
863 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 ADVANCE_LP_RING();
865 }
866}
867
Dave Airlieeddca552007-07-11 16:09:54 +1000868static void i810_dma_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int pitch = dev_priv->pitch;
872 RING_LOCALS;
873
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
875 __FUNCTION__,
876 dev_priv->current_page,
877 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 i810_kernel_lost_context(dev);
880
881 BEGIN_LP_RING(2);
882 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
883 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 ADVANCE_LP_RING();
885
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* On i815 at least ASYNC is buggy */
888 /* pitch<<5 is from 11.2.8 p158,
889 its the pitch / 8 then left shifted 8,
890 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
892 if (dev_priv->current_page == 0) {
893 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 dev_priv->current_page = 1;
895 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 dev_priv->current_page = 0;
898 }
899 OUT_RING(0);
900 ADVANCE_LP_RING();
901
902 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000903 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
904 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 ADVANCE_LP_RING();
906
907 /* Increment the frame counter. The client-side 3D driver must
908 * throttle the framerate by waiting for this value before
909 * performing the swapbuffer ioctl.
910 */
911 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
912
913}
914
Dave Airlieeddca552007-07-11 16:09:54 +1000915static void i810_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 drm_i810_private_t *dev_priv = dev->dev_private;
918 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920/* printk("%s\n", __FUNCTION__); */
921
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000922 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 BEGIN_LP_RING(4);
925 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
926 OUT_RING(CMD_REPORT_HEAD);
927 OUT_RING(0);
928 OUT_RING(0);
929 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Dave Airlieeddca552007-07-11 16:09:54 +1000934static int i810_flush_queue(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000937 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 int i, ret = 0;
939 RING_LOCALS;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941/* printk("%s\n", __FUNCTION__); */
942
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000943 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000945 BEGIN_LP_RING(2);
946 OUT_RING(CMD_REPORT_HEAD);
947 OUT_RING(0);
948 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000953 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000954 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
957 I810_BUF_FREE);
958
959 if (used == I810_BUF_HARDWARE)
960 DRM_DEBUG("reclaimed from HARDWARE\n");
961 if (used == I810_BUF_CLIENT)
962 DRM_DEBUG("still on client\n");
963 }
964
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000965 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/* Must be called with the lock held */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000969static void i810_reclaim_buffers(struct drm_device * dev,
970 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Dave Airliecdd55a22007-07-11 16:32:08 +1000972 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000973 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000975 if (!dma)
976 return;
977 if (!dev->dev_private)
978 return;
979 if (!dma->buflist)
980 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000982 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000985 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Eric Anholt6c340ea2007-08-25 20:23:09 +1000988 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
990 I810_BUF_FREE);
991
992 if (used == I810_BUF_CLIENT)
993 DRM_DEBUG("reclaimed from client\n");
994 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 }
998}
999
Eric Anholt6c340ea2007-08-25 20:23:09 +10001000static int i810_flush_ioctl(struct inode *inode, struct drm_file *file_priv,
Dave Airliec94f7022005-07-07 21:03:38 +10001001 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001003 struct drm_device *dev = file_priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Eric Anholt6c340ea2007-08-25 20:23:09 +10001005 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 i810_flush_queue(dev);
1008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Eric Anholt6c340ea2007-08-25 20:23:09 +10001011static int i810_dma_vertex(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001014 struct drm_device *dev = file_priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001015 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1017 u32 *hw_status = dev_priv->hw_status_page;
1018 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1019 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 drm_i810_vertex_t vertex;
1021
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001022 if (copy_from_user
1023 (&vertex, (drm_i810_vertex_t __user *) arg, sizeof(vertex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -EFAULT;
1025
Eric Anholt6c340ea2007-08-25 20:23:09 +10001026 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 DRM_DEBUG("i810 dma vertex, idx %d used %d discard %d\n",
1029 vertex.idx, vertex.used, vertex.discard);
1030
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 if (vertex.idx < 0 || vertex.idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return -EINVAL;
1033
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 i810_dma_dispatch_vertex(dev,
1035 dma->buflist[vertex.idx],
1036 vertex.discard, vertex.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 atomic_add(vertex.used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 sarea_priv->last_enqueue = dev_priv->counter - 1;
1041 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 return 0;
1044}
1045
Eric Anholt6c340ea2007-08-25 20:23:09 +10001046static int i810_clear_bufs(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001049 struct drm_device *dev = file_priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 drm_i810_clear_t clear;
1051
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 if (copy_from_user
1053 (&clear, (drm_i810_clear_t __user *) arg, sizeof(clear)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return -EFAULT;
1055
Eric Anholt6c340ea2007-08-25 20:23:09 +10001056 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 /* GH: Someone's doing nasty things... */
1059 if (!dev->dev_private) {
1060 return -EINVAL;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001063 i810_dma_dispatch_clear(dev, clear.flags,
1064 clear.clear_color, clear.clear_depth);
1065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Eric Anholt6c340ea2007-08-25 20:23:09 +10001068static int i810_swap_bufs(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001071 struct drm_device *dev = file_priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 DRM_DEBUG("i810_swap_bufs\n");
1074
Eric Anholt6c340ea2007-08-25 20:23:09 +10001075 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 i810_dma_dispatch_swap(dev);
1078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Eric Anholt6c340ea2007-08-25 20:23:09 +10001081static int i810_getage(struct inode *inode, struct drm_file *file_priv,
1082 unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001085 struct drm_device *dev = file_priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1087 u32 *hw_status = dev_priv->hw_status_page;
1088 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1089 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return 0;
1093}
1094
Eric Anholt6c340ea2007-08-25 20:23:09 +10001095static int i810_getbuf(struct inode *inode, struct drm_file *file_priv,
1096 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001098 struct drm_device *dev = file_priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 int retcode = 0;
1100 drm_i810_dma_t d;
1101 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1102 u32 *hw_status = dev_priv->hw_status_page;
1103 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1104 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 if (copy_from_user(&d, (drm_i810_dma_t __user *) arg, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -EFAULT;
1108
Eric Anholt6c340ea2007-08-25 20:23:09 +10001109 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 d.granted = 0;
1112
Eric Anholt6c340ea2007-08-25 20:23:09 +10001113 retcode = i810_dma_get_buffer(dev, &d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
1116 current->pid, retcode, d.granted);
1117
Dave Airlieeddca552007-07-11 16:09:54 +10001118 if (copy_to_user((void __user *) arg, &d, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 return retcode;
1123}
1124
Eric Anholt6c340ea2007-08-25 20:23:09 +10001125static int i810_copybuf(struct inode *inode, struct drm_file *file_priv,
1126 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 /* Never copy - 2.4.x doesn't need it */
1129 return 0;
1130}
1131
Eric Anholt6c340ea2007-08-25 20:23:09 +10001132static int i810_docopy(struct inode *inode, struct drm_file *file_priv,
1133 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 /* Never copy - 2.4.x doesn't need it */
1136 return 0;
1137}
1138
Dave Airlie056219e2007-07-11 16:17:42 +10001139static void i810_dma_dispatch_mc(struct drm_device * dev, struct drm_buf * buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001140 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 drm_i810_private_t *dev_priv = dev->dev_private;
1143 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1144 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1145 unsigned long address = (unsigned long)buf->bus_address;
1146 unsigned long start = address - dev->agp->base;
1147 int u;
1148 RING_LOCALS;
1149
1150 i810_kernel_lost_context(dev);
1151
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001152 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (u != I810_BUF_CLIENT) {
1154 DRM_DEBUG("MC found buffer that isn't mine!\n");
1155 }
1156
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001157 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 used = 0;
1159
1160 sarea_priv->dirty = 0x7f;
1161
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001162 DRM_DEBUG("dispatch mc addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 dev_priv->counter++;
1165 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1166 DRM_DEBUG("i810_dma_dispatch_mc\n");
1167 DRM_DEBUG("start : %lx\n", start);
1168 DRM_DEBUG("used : %d\n", used);
1169 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1170
1171 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1172 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001173 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 used += 4;
1175 }
1176
1177 i810_unmap_buffer(buf);
1178 }
1179 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 OUT_RING(CMD_OP_BATCH_BUFFER);
1181 OUT_RING(start | BB1_PROTECTED);
1182 OUT_RING(start + used - 4);
1183 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ADVANCE_LP_RING();
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001187 OUT_RING(CMD_STORE_DWORD_IDX);
1188 OUT_RING(buf_priv->my_use_idx);
1189 OUT_RING(I810_BUF_FREE);
1190 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001192 OUT_RING(CMD_STORE_DWORD_IDX);
1193 OUT_RING(16);
1194 OUT_RING(last_render);
1195 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ADVANCE_LP_RING();
1197}
1198
Eric Anholt6c340ea2007-08-25 20:23:09 +10001199static int i810_dma_mc(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001200 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001202 struct drm_device *dev = file_priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001203 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001204 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 u32 *hw_status = dev_priv->hw_status_page;
1206 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001207 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 drm_i810_mc_t mc;
1209
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001210 if (copy_from_user(&mc, (drm_i810_mc_t __user *) arg, sizeof(mc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return -EFAULT;
1212
Eric Anholt6c340ea2007-08-25 20:23:09 +10001213 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (mc.idx >= dma->buf_count || mc.idx < 0)
1216 return -EINVAL;
1217
1218 i810_dma_dispatch_mc(dev, dma->buflist[mc.idx], mc.used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001219 mc.last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 atomic_add(mc.used, &dev->counts[_DRM_STAT_SECONDARY]);
1222 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001223 sarea_priv->last_enqueue = dev_priv->counter - 1;
1224 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return 0;
1227}
1228
Eric Anholt6c340ea2007-08-25 20:23:09 +10001229static int i810_rstatus(struct inode *inode, struct drm_file *file_priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 unsigned int cmd, unsigned long arg)
1231{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001232 struct drm_device *dev = file_priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001233 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001235 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Eric Anholt6c340ea2007-08-25 20:23:09 +10001238static int i810_ov0_info(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001239 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001241 struct drm_device *dev = file_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
Eric Anholt6c340ea2007-08-25 20:23:09 +10001253static int i810_fstatus(struct inode *inode, struct drm_file *file_priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 unsigned int cmd, unsigned long arg)
1255{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001256 struct drm_device *dev = file_priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001257 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Eric Anholt6c340ea2007-08-25 20:23:09 +10001259 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 return I810_READ(0x30008);
1262}
1263
Eric Anholt6c340ea2007-08-25 20:23:09 +10001264static int i810_ov0_flip(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001265 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001267 struct drm_device *dev = file_priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001268 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Eric Anholt6c340ea2007-08-25 20:23:09 +10001270 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 //Tell the overlay to update
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001273 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 return 0;
1276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001279 */
Dave Airlieeddca552007-07-11 16:09:54 +10001280static void i810_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 DRM_DEBUG("%s\n", __FUNCTION__);
1285 dev_priv->page_flipping = 1;
1286 dev_priv->current_page = 0;
1287 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1288}
1289
Dave Airlieeddca552007-07-11 16:09:54 +10001290static int i810_do_cleanup_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 drm_i810_private_t *dev_priv = dev->dev_private;
1293
1294 DRM_DEBUG("%s\n", __FUNCTION__);
1295 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001296 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 dev_priv->page_flipping = 0;
1299 return 0;
1300}
1301
Eric Anholt6c340ea2007-08-25 20:23:09 +10001302static int i810_flip_bufs(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001303 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001305 struct drm_device *dev = file_priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 drm_i810_private_t *dev_priv = dev->dev_private;
1307
1308 DRM_DEBUG("%s\n", __FUNCTION__);
1309
Eric Anholt6c340ea2007-08-25 20:23:09 +10001310 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001312 if (!dev_priv->page_flipping)
1313 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001315 i810_dma_dispatch_flip(dev);
1316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Dave Airlieeddca552007-07-11 16:09:54 +10001319int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001320{
1321 /* i810 has 4 more counters */
1322 dev->counters += 4;
1323 dev->types[6] = _DRM_STAT_IRQ;
1324 dev->types[7] = _DRM_STAT_PRIMARY;
1325 dev->types[8] = _DRM_STAT_SECONDARY;
1326 dev->types[9] = _DRM_STAT_DMA;
1327
1328 return 0;
1329}
1330
Dave Airlieeddca552007-07-11 16:09:54 +10001331void i810_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001333 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Eric Anholt6c340ea2007-08-25 20:23:09 +10001336void i810_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 if (dev->dev_private) {
1339 drm_i810_private_t *dev_priv = dev->dev_private;
1340 if (dev_priv->page_flipping) {
1341 i810_do_cleanup_pageflip(dev);
1342 }
1343 }
1344}
1345
Eric Anholt6c340ea2007-08-25 20:23:09 +10001346void i810_driver_reclaim_buffers_locked(struct drm_device * dev,
1347 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001349 i810_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Dave Airlieeddca552007-07-11 16:09:54 +10001352int i810_driver_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001354 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return 0;
1356}
1357
1358drm_ioctl_desc_t i810_ioctls[] = {
Dave Airliea7a2cc32006-01-02 13:54:04 +11001359 [DRM_IOCTL_NR(DRM_I810_INIT)] = {i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1360 [DRM_IOCTL_NR(DRM_I810_VERTEX)] = {i810_dma_vertex, DRM_AUTH},
1361 [DRM_IOCTL_NR(DRM_I810_CLEAR)] = {i810_clear_bufs, DRM_AUTH},
1362 [DRM_IOCTL_NR(DRM_I810_FLUSH)] = {i810_flush_ioctl, DRM_AUTH},
1363 [DRM_IOCTL_NR(DRM_I810_GETAGE)] = {i810_getage, DRM_AUTH},
1364 [DRM_IOCTL_NR(DRM_I810_GETBUF)] = {i810_getbuf, DRM_AUTH},
1365 [DRM_IOCTL_NR(DRM_I810_SWAP)] = {i810_swap_bufs, DRM_AUTH},
1366 [DRM_IOCTL_NR(DRM_I810_COPY)] = {i810_copybuf, DRM_AUTH},
1367 [DRM_IOCTL_NR(DRM_I810_DOCOPY)] = {i810_docopy, DRM_AUTH},
1368 [DRM_IOCTL_NR(DRM_I810_OV0INFO)] = {i810_ov0_info, DRM_AUTH},
1369 [DRM_IOCTL_NR(DRM_I810_FSTATUS)] = {i810_fstatus, DRM_AUTH},
1370 [DRM_IOCTL_NR(DRM_I810_OV0FLIP)] = {i810_ov0_flip, DRM_AUTH},
1371 [DRM_IOCTL_NR(DRM_I810_MC)] = {i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1372 [DRM_IOCTL_NR(DRM_I810_RSTATUS)] = {i810_rstatus, DRM_AUTH},
1373 [DRM_IOCTL_NR(DRM_I810_FLIP)] = {i810_flip_bufs, DRM_AUTH}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374};
1375
1376int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001377
1378/**
1379 * Determine if the device really is AGP or not.
1380 *
1381 * All Intel graphics chipsets are treated as AGP, even if they are really
1382 * PCI-e.
1383 *
1384 * \param dev The device to be tested.
1385 *
1386 * \returns
1387 * A value of 1 is always retured to indictate every i810 is AGP.
1388 */
Dave Airlieeddca552007-07-11 16:09:54 +10001389int i810_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001390{
1391 return 1;
1392}