blob: e5de8ea4154460cabe658240798ffd288570be78 [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
Dave Airliebc5f4522007-11-05 12:50:58 +100043#define I810_BUF_HARDWARE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
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 Airlie2c14f282008-04-21 16:47:32 +100097 dev = priv->minor->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{
Dave Airlie2c14f282008-04-21 16:47:32 +1000125 struct drm_device *dev = file_priv->minor->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
Eric Anholtc153f452007-09-03 12:06:45 +1000432static int i810_dma_init(struct drm_device *dev, void *data,
433 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000436 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000437 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Eric Anholtc153f452007-09-03 12:06:45 +1000439 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000440 case I810_INIT_DMA_1_4:
441 DRM_INFO("Using v1.4 init.\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000442 dev_priv = drm_alloc(sizeof(drm_i810_private_t),
443 DRM_MEM_DRIVER);
444 if (dev_priv == NULL)
445 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000446 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000449 case I810_CLEANUP_DMA:
450 DRM_INFO("DMA Cleanup\n");
451 retcode = i810_dma_cleanup(dev);
452 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000453 default:
454 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000457 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/* Most efficient way to verify state for the i810 is as it is
461 * emitted. Non-conformant state is silently dropped.
462 *
463 * Use 'volatile' & local var tmp to force the emitted values to be
464 * identical to the verified ones.
465 */
Dave Airlieeddca552007-07-11 16:09:54 +1000466static void i810EmitContextVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000469 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int i, j = 0;
471 unsigned int tmp;
472 RING_LOCALS;
473
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 OUT_RING(GFX_OP_COLOR_FACTOR);
477 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000479 OUT_RING(GFX_OP_STIPPLE);
480 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 tmp = code[i];
484
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 if ((tmp & (7 << 29)) == (3 << 29) &&
486 (tmp & (0x1f << 24)) < (0x1d << 24)) {
487 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489 } else
490 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
493 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 ADVANCE_LP_RING();
497}
498
Dave Airlieeddca552007-07-11 16:09:54 +1000499static void i810EmitTexVerified(struct drm_device * dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000501 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int i, j = 0;
503 unsigned int tmp;
504 RING_LOCALS;
505
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000506 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000508 OUT_RING(GFX_OP_MAP_INFO);
509 OUT_RING(code[I810_TEXREG_MI1]);
510 OUT_RING(code[I810_TEXREG_MI2]);
511 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000513 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 tmp = code[i];
515
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516 if ((tmp & (7 << 29)) == (3 << 29) &&
517 (tmp & (0x1f << 24)) < (0x1d << 24)) {
518 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 } else
521 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
524 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000525 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 ADVANCE_LP_RING();
528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530/* Need to do some additional checking when setting the dest buffer.
531 */
Dave Airlieeddca552007-07-11 16:09:54 +1000532static void i810EmitDestVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000535 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 unsigned int tmp;
537 RING_LOCALS;
538
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 tmp = code[I810_DESTREG_DI1];
542 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 OUT_RING(CMD_OP_DESTBUFFER_INFO);
544 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
547 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* invarient:
550 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 OUT_RING(CMD_OP_Z_BUFFER_INFO);
552 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 OUT_RING(GFX_OP_DESTBUFFER_VARS);
555 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000557 OUT_RING(GFX_OP_DRAWRECT_INFO);
558 OUT_RING(code[I810_DESTREG_DR1]);
559 OUT_RING(code[I810_DESTREG_DR2]);
560 OUT_RING(code[I810_DESTREG_DR3]);
561 OUT_RING(code[I810_DESTREG_DR4]);
562 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 ADVANCE_LP_RING();
565}
566
Dave Airlieeddca552007-07-11 16:09:54 +1000567static void i810EmitState(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000569 drm_i810_private_t *dev_priv = dev->dev_private;
570 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572
Márton Németh3e684ea2008-01-24 15:58:57 +1000573 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
578 }
579
580 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000581 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
583 }
584
585 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
588 }
589
590 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
593 }
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/* need to verify
597 */
Dave Airlieeddca552007-07-11 16:09:54 +1000598static void i810_dma_dispatch_clear(struct drm_device * dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000599 unsigned int clear_color,
600 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602 drm_i810_private_t *dev_priv = dev->dev_private;
603 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000605 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int pitch = dev_priv->pitch;
607 int cpp = 2;
608 int i;
609 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000610
611 if (dev_priv->current_page == 1) {
612 unsigned int tmp = flags;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 if (tmp & I810_FRONT)
616 flags |= I810_BACK;
617 if (tmp & I810_BACK)
618 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623 if (nbox > I810_NR_SAREA_CLIPRECTS)
624 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000626 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 unsigned int x = pbox->x1;
628 unsigned int y = pbox->y1;
629 unsigned int width = (pbox->x2 - x) * cpp;
630 unsigned int height = pbox->y2 - y;
631 unsigned int start = y * pitch + x * cpp;
632
633 if (pbox->x1 > pbox->x2 ||
634 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 continue;
637
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 if (flags & I810_FRONT) {
639 BEGIN_LP_RING(6);
640 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
641 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
642 OUT_RING((height << 16) | width);
643 OUT_RING(start);
644 OUT_RING(clear_color);
645 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ADVANCE_LP_RING();
647 }
648
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 if (flags & I810_BACK) {
650 BEGIN_LP_RING(6);
651 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
652 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
653 OUT_RING((height << 16) | width);
654 OUT_RING(dev_priv->back_offset + start);
655 OUT_RING(clear_color);
656 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ADVANCE_LP_RING();
658 }
659
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 if (flags & I810_DEPTH) {
661 BEGIN_LP_RING(6);
662 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
663 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
664 OUT_RING((height << 16) | width);
665 OUT_RING(dev_priv->depth_offset + start);
666 OUT_RING(clear_zval);
667 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 ADVANCE_LP_RING();
669 }
670 }
671}
672
Dave Airlieeddca552007-07-11 16:09:54 +1000673static void i810_dma_dispatch_swap(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 drm_i810_private_t *dev_priv = dev->dev_private;
676 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000678 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int pitch = dev_priv->pitch;
680 int cpp = 2;
681 int i;
682 RING_LOCALS;
683
684 DRM_DEBUG("swapbuffers\n");
685
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 if (nbox > I810_NR_SAREA_CLIPRECTS)
689 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 unsigned int w = pbox->x2 - pbox->x1;
693 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 unsigned int start = dst;
696
697 if (pbox->x1 > pbox->x2 ||
698 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 continue;
701
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000702 BEGIN_LP_RING(6);
703 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
704 OUT_RING(pitch | (0xCC << 16));
705 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 OUT_RING(dev_priv->back_offset + start);
710 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 ADVANCE_LP_RING();
716 }
717}
718
Dave Airlieeddca552007-07-11 16:09:54 +1000719static void i810_dma_dispatch_vertex(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +1000720 struct drm_buf * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000725 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000726 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 unsigned long address = (unsigned long)buf->bus_address;
728 unsigned long start = address - dev->agp->base;
729 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 nbox = I810_NR_SAREA_CLIPRECTS;
736
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 used = 0;
739
740 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
744 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
745
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 *(u32 *) buf_priv->kernel_virtual =
747 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000750 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 used += 4;
752 }
753
754 i810_unmap_buffer(buf);
755 }
756
757 if (used) {
758 do {
759 if (i < nbox) {
760 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
762 SC_ENABLE);
763 OUT_RING(GFX_OP_SCISSOR_INFO);
764 OUT_RING(box[i].x1 | (box[i].y1 << 16));
765 OUT_RING((box[i].x2 -
766 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 ADVANCE_LP_RING();
768 }
769
770 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000771 OUT_RING(CMD_OP_BATCH_BUFFER);
772 OUT_RING(start | BB1_PROTECTED);
773 OUT_RING(start + used - 4);
774 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ADVANCE_LP_RING();
776
777 } while (++i < nbox);
778 }
779
780 if (discard) {
781 dev_priv->counter++;
782
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
784 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 OUT_RING(CMD_STORE_DWORD_IDX);
788 OUT_RING(20);
789 OUT_RING(dev_priv->counter);
790 OUT_RING(CMD_STORE_DWORD_IDX);
791 OUT_RING(buf_priv->my_use_idx);
792 OUT_RING(I810_BUF_FREE);
793 OUT_RING(CMD_REPORT_HEAD);
794 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 ADVANCE_LP_RING();
796 }
797}
798
Dave Airlieeddca552007-07-11 16:09:54 +1000799static void i810_dma_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int pitch = dev_priv->pitch;
803 RING_LOCALS;
804
Márton Németh3e684ea2008-01-24 15:58:57 +1000805 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 dev_priv->current_page,
807 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 i810_kernel_lost_context(dev);
810
811 BEGIN_LP_RING(2);
812 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
813 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 ADVANCE_LP_RING();
815
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* On i815 at least ASYNC is buggy */
818 /* pitch<<5 is from 11.2.8 p158,
819 its the pitch / 8 then left shifted 8,
820 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
822 if (dev_priv->current_page == 0) {
823 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 dev_priv->current_page = 1;
825 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 dev_priv->current_page = 0;
828 }
829 OUT_RING(0);
830 ADVANCE_LP_RING();
831
832 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
834 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 ADVANCE_LP_RING();
836
837 /* Increment the frame counter. The client-side 3D driver must
838 * throttle the framerate by waiting for this value before
839 * performing the swapbuffer ioctl.
840 */
841 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
842
843}
844
Dave Airlieeddca552007-07-11 16:09:54 +1000845static void i810_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 drm_i810_private_t *dev_priv = dev->dev_private;
848 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 BEGIN_LP_RING(4);
853 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
854 OUT_RING(CMD_REPORT_HEAD);
855 OUT_RING(0);
856 OUT_RING(0);
857 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Dave Airlieeddca552007-07-11 16:09:54 +1000862static int i810_flush_queue(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000864 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000865 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 int i, ret = 0;
867 RING_LOCALS;
868
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 BEGIN_LP_RING(2);
872 OUT_RING(CMD_REPORT_HEAD);
873 OUT_RING(0);
874 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000879 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000880 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
883 I810_BUF_FREE);
884
885 if (used == I810_BUF_HARDWARE)
886 DRM_DEBUG("reclaimed from HARDWARE\n");
887 if (used == I810_BUF_CLIENT)
888 DRM_DEBUG("still on client\n");
889 }
890
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894/* Must be called with the lock held */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000895static void i810_reclaim_buffers(struct drm_device * dev,
896 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Dave Airliecdd55a22007-07-11 16:32:08 +1000898 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 if (!dma)
902 return;
903 if (!dev->dev_private)
904 return;
905 if (!dma->buflist)
906 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000911 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Eric Anholt6c340ea2007-08-25 20:23:09 +1000914 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
916 I810_BUF_FREE);
917
918 if (used == I810_BUF_CLIENT)
919 DRM_DEBUG("reclaimed from client\n");
920 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 }
924}
925
Eric Anholtc153f452007-09-03 12:06:45 +1000926static int i810_flush_ioctl(struct drm_device *dev, void *data,
927 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000929 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 i810_flush_queue(dev);
932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Eric Anholtc153f452007-09-03 12:06:45 +1000935static int i810_dma_vertex(struct drm_device *dev, void *data,
936 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Dave Airliecdd55a22007-07-11 16:32:08 +1000938 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
940 u32 *hw_status = dev_priv->hw_status_page;
941 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
942 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000943 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Eric Anholt6c340ea2007-08-25 20:23:09 +1000945 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Márton Németh3e684ea2008-01-24 15:58:57 +1000947 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000948 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Eric Anholtc153f452007-09-03 12:06:45 +1000950 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return -EINVAL;
952
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000953 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000954 dma->buflist[vertex->idx],
955 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Eric Anholtc153f452007-09-03 12:06:45 +1000957 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000959 sarea_priv->last_enqueue = dev_priv->counter - 1;
960 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 return 0;
963}
964
Eric Anholtc153f452007-09-03 12:06:45 +1000965static int i810_clear_bufs(struct drm_device *dev, void *data,
966 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Eric Anholtc153f452007-09-03 12:06:45 +1000968 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Eric Anholt6c340ea2007-08-25 20:23:09 +1000970 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 /* GH: Someone's doing nasty things... */
973 if (!dev->dev_private) {
974 return -EINVAL;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Eric Anholtc153f452007-09-03 12:06:45 +1000977 i810_dma_dispatch_clear(dev, clear->flags,
978 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Eric Anholtc153f452007-09-03 12:06:45 +1000982static int i810_swap_bufs(struct drm_device *dev, void *data,
983 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Márton Németh3e684ea2008-01-24 15:58:57 +1000985 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Eric Anholt6c340ea2007-08-25 20:23:09 +1000987 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000989 i810_dma_dispatch_swap(dev);
990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Eric Anholtc153f452007-09-03 12:06:45 +1000993static int i810_getage(struct drm_device *dev, void *data,
994 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
997 u32 *hw_status = dev_priv->hw_status_page;
998 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
999 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
1003}
1004
Eric Anholtc153f452007-09-03 12:06:45 +10001005static int i810_getbuf(struct drm_device *dev, void *data,
1006 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001009 drm_i810_dma_t *d = data;
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
Eric Anholt6c340ea2007-08-25 20:23:09 +10001015 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Eric Anholtc153f452007-09-03 12:06:45 +10001017 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Eric Anholtc153f452007-09-03 12:06:45 +10001019 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001022 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 return retcode;
1027}
1028
Eric Anholtc153f452007-09-03 12:06:45 +10001029static int i810_copybuf(struct drm_device *dev, void *data,
1030 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 /* Never copy - 2.4.x doesn't need it */
1033 return 0;
1034}
1035
Eric Anholtc153f452007-09-03 12:06:45 +10001036static int i810_docopy(struct drm_device *dev, void *data,
1037 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 /* Never copy - 2.4.x doesn't need it */
1040 return 0;
1041}
1042
Dave Airlie056219e2007-07-11 16:17:42 +10001043static void i810_dma_dispatch_mc(struct drm_device * dev, struct drm_buf * buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 drm_i810_private_t *dev_priv = dev->dev_private;
1047 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1048 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1049 unsigned long address = (unsigned long)buf->bus_address;
1050 unsigned long start = address - dev->agp->base;
1051 int u;
1052 RING_LOCALS;
1053
1054 i810_kernel_lost_context(dev);
1055
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001056 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (u != I810_BUF_CLIENT) {
1058 DRM_DEBUG("MC found buffer that isn't mine!\n");
1059 }
1060
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 used = 0;
1063
1064 sarea_priv->dirty = 0x7f;
1065
Márton Németh3e684ea2008-01-24 15:58:57 +10001066 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 dev_priv->counter++;
1069 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 DRM_DEBUG("start : %lx\n", start);
1071 DRM_DEBUG("used : %d\n", used);
1072 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1073
1074 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1075 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001076 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 used += 4;
1078 }
1079
1080 i810_unmap_buffer(buf);
1081 }
1082 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 OUT_RING(CMD_OP_BATCH_BUFFER);
1084 OUT_RING(start | BB1_PROTECTED);
1085 OUT_RING(start + used - 4);
1086 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ADVANCE_LP_RING();
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090 OUT_RING(CMD_STORE_DWORD_IDX);
1091 OUT_RING(buf_priv->my_use_idx);
1092 OUT_RING(I810_BUF_FREE);
1093 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 OUT_RING(CMD_STORE_DWORD_IDX);
1096 OUT_RING(16);
1097 OUT_RING(last_render);
1098 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ADVANCE_LP_RING();
1100}
1101
Eric Anholtc153f452007-09-03 12:06:45 +10001102static int i810_dma_mc(struct drm_device *dev, void *data,
1103 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Dave Airliecdd55a22007-07-11 16:32:08 +10001105 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 u32 *hw_status = dev_priv->hw_status_page;
1108 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001110 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Eric Anholt6c340ea2007-08-25 20:23:09 +10001112 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Eric Anholtc153f452007-09-03 12:06:45 +10001114 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return -EINVAL;
1116
Eric Anholtc153f452007-09-03 12:06:45 +10001117 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1118 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Eric Anholtc153f452007-09-03 12:06:45 +10001120 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001122 sarea_priv->last_enqueue = dev_priv->counter - 1;
1123 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 return 0;
1126}
1127
Eric Anholtc153f452007-09-03 12:06:45 +10001128static int i810_rstatus(struct drm_device *dev, void *data,
1129 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Eric Anholtc153f452007-09-03 12:06:45 +10001136static int i810_ov0_info(struct drm_device *dev, void *data,
1137 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001139 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001140 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Eric Anholtc153f452007-09-03 12:06:45 +10001142 ov->offset = dev_priv->overlay_offset;
1143 ov->physical = dev_priv->overlay_physical;
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146}
1147
Eric Anholtc153f452007-09-03 12:06:45 +10001148static int i810_fstatus(struct drm_device *dev, void *data,
1149 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001151 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Eric Anholt6c340ea2007-08-25 20:23:09 +10001153 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return I810_READ(0x30008);
1155}
1156
Eric Anholtc153f452007-09-03 12:06:45 +10001157static int i810_ov0_flip(struct drm_device *dev, void *data,
1158 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001160 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Eric Anholt6c340ea2007-08-25 20:23:09 +10001162 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 //Tell the overlay to update
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001165 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 return 0;
1168}
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171 */
Dave Airlieeddca552007-07-11 16:09:54 +10001172static void i810_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001175
Márton Németh3e684ea2008-01-24 15:58:57 +10001176 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 dev_priv->page_flipping = 1;
1178 dev_priv->current_page = 0;
1179 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1180}
1181
Dave Airlieeddca552007-07-11 16:09:54 +10001182static int i810_do_cleanup_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 drm_i810_private_t *dev_priv = dev->dev_private;
1185
Márton Németh3e684ea2008-01-24 15:58:57 +10001186 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001188 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 dev_priv->page_flipping = 0;
1191 return 0;
1192}
1193
Eric Anholtc153f452007-09-03 12:06:45 +10001194static int i810_flip_bufs(struct drm_device *dev, void *data,
1195 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 drm_i810_private_t *dev_priv = dev->dev_private;
1198
Márton Németh3e684ea2008-01-24 15:58:57 +10001199 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Eric Anholt6c340ea2007-08-25 20:23:09 +10001201 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001203 if (!dev_priv->page_flipping)
1204 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001206 i810_dma_dispatch_flip(dev);
1207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Dave Airlieeddca552007-07-11 16:09:54 +10001210int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001211{
1212 /* i810 has 4 more counters */
1213 dev->counters += 4;
1214 dev->types[6] = _DRM_STAT_IRQ;
1215 dev->types[7] = _DRM_STAT_PRIMARY;
1216 dev->types[8] = _DRM_STAT_SECONDARY;
1217 dev->types[9] = _DRM_STAT_DMA;
1218
1219 return 0;
1220}
1221
Dave Airlieeddca552007-07-11 16:09:54 +10001222void i810_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001224 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Eric Anholt6c340ea2007-08-25 20:23:09 +10001227void i810_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 if (dev->dev_private) {
1230 drm_i810_private_t *dev_priv = dev->dev_private;
1231 if (dev_priv->page_flipping) {
1232 i810_do_cleanup_pageflip(dev);
1233 }
1234 }
1235}
1236
Eric Anholt6c340ea2007-08-25 20:23:09 +10001237void i810_driver_reclaim_buffers_locked(struct drm_device * dev,
1238 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001240 i810_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Dave Airlieeddca552007-07-11 16:09:54 +10001243int i810_driver_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001245 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 0;
1247}
1248
Eric Anholtc153f452007-09-03 12:06:45 +10001249struct drm_ioctl_desc i810_ioctls[] = {
1250 DRM_IOCTL_DEF(DRM_I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1251 DRM_IOCTL_DEF(DRM_I810_VERTEX, i810_dma_vertex, DRM_AUTH),
1252 DRM_IOCTL_DEF(DRM_I810_CLEAR, i810_clear_bufs, DRM_AUTH),
1253 DRM_IOCTL_DEF(DRM_I810_FLUSH, i810_flush_ioctl, DRM_AUTH),
1254 DRM_IOCTL_DEF(DRM_I810_GETAGE, i810_getage, DRM_AUTH),
1255 DRM_IOCTL_DEF(DRM_I810_GETBUF, i810_getbuf, DRM_AUTH),
1256 DRM_IOCTL_DEF(DRM_I810_SWAP, i810_swap_bufs, DRM_AUTH),
1257 DRM_IOCTL_DEF(DRM_I810_COPY, i810_copybuf, DRM_AUTH),
1258 DRM_IOCTL_DEF(DRM_I810_DOCOPY, i810_docopy, DRM_AUTH),
1259 DRM_IOCTL_DEF(DRM_I810_OV0INFO, i810_ov0_info, DRM_AUTH),
1260 DRM_IOCTL_DEF(DRM_I810_FSTATUS, i810_fstatus, DRM_AUTH),
1261 DRM_IOCTL_DEF(DRM_I810_OV0FLIP, i810_ov0_flip, DRM_AUTH),
1262 DRM_IOCTL_DEF(DRM_I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1263 DRM_IOCTL_DEF(DRM_I810_RSTATUS, i810_rstatus, DRM_AUTH),
1264 DRM_IOCTL_DEF(DRM_I810_FLIP, i810_flip_bufs, DRM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265};
1266
1267int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001268
1269/**
1270 * Determine if the device really is AGP or not.
1271 *
1272 * All Intel graphics chipsets are treated as AGP, even if they are really
1273 * PCI-e.
1274 *
1275 * \param dev The device to be tested.
1276 *
1277 * \returns
1278 * A value of 1 is always retured to indictate every i810 is AGP.
1279 */
Dave Airlieeddca552007-07-11 16:09:54 +10001280int i810_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001281{
1282 return 1;
1283}