blob: 997d91707ad217f545c9720c0b0b9a604617f7e2 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/pagemap.h>
41
42#define I810_BUF_FREE 2
43#define I810_BUF_CLIENT 1
Dave Airliebc5f4522007-11-05 12:50:58 +100044#define I810_BUF_HARDWARE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define I810_BUF_UNMAPPED 0
47#define I810_BUF_MAPPED 1
48
Dave Airlie056219e2007-07-11 16:17:42 +100049static struct drm_buf *i810_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Dave Airliecdd55a22007-07-11 16:32:08 +100051 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100052 int i;
53 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 /* Linear search might not be the best solution */
56
Dave Airlieb5e89ed2005-09-25 14:28:13 +100057 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +100058 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100059 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100061 used = cmpxchg(buf_priv->in_use, I810_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 I810_BUF_CLIENT);
63 if (used == I810_BUF_FREE) {
64 return buf;
65 }
66 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100067 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70/* This should only be called if the buffer is not sent to the hardware
71 * yet, the hardware updates in use for us once its on the ring buffer.
72 */
73
Dave Airlie056219e2007-07-11 16:17:42 +100074static int i810_freelist_put(struct drm_device * dev, struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100076 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
77 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Dave Airlieb5e89ed2005-09-25 14:28:13 +100079 /* In use is already a pointer */
80 used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_FREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (used != I810_BUF_CLIENT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
83 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Dave Airliec94f7022005-07-07 21:03:38 +100089static int i810_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Dave Airlieeddca552007-07-11 16:09:54 +100091 struct drm_file *priv = filp->private_data;
92 struct drm_device *dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093 drm_i810_private_t *dev_priv;
Dave Airlie056219e2007-07-11 16:17:42 +100094 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 drm_i810_buf_priv_t *buf_priv;
96
97 lock_kernel();
Dave Airlie2c14f282008-04-21 16:47:32 +100098 dev = priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000100 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 buf_priv = buf->dev_private;
102
103 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
104 vma->vm_file = filp;
105
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106 buf_priv->currently_mapped = I810_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unlock_kernel();
108
109 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlie3d774612006-08-07 20:07:43 +1000110 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000111 vma->vm_end - vma->vm_start, vma->vm_page_prot))
112 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
114}
115
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800116static const struct file_operations i810_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000117 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000118 .release = drm_release,
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000119 .unlocked_ioctl = drm_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120 .mmap = i810_mmap_buffers,
121 .fasync = drm_fasync,
Dave Airliec94f7022005-07-07 21:03:38 +1000122};
123
Eric Anholt6c340ea2007-08-25 20:23:09 +1000124static int i810_map_buffer(struct drm_buf * buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Dave Airlie2c14f282008-04-21 16:47:32 +1000126 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800129 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int retcode = 0;
131
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return -EINVAL;
134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000136 old_fops = file_priv->filp->f_op;
137 file_priv->filp->f_op = &i810_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 dev_priv->mmap_buffer = buf;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000139 buf_priv->virtual = (void *)do_mmap(file_priv->filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 PROT_READ | PROT_WRITE,
141 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000143 file_priv->filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000144 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Real error */
146 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000147 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 buf_priv->virtual = NULL;
149 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 return retcode;
153}
154
Dave Airlie056219e2007-07-11 16:17:42 +1000155static int i810_unmap_buffer(struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
158 int retcode = 0;
159
160 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
161 return -EINVAL;
162
163 down_write(&current->mm->mmap_sem);
164 retcode = do_munmap(current->mm,
165 (unsigned long)buf_priv->virtual,
166 (size_t) buf->total);
167 up_write(&current->mm->mmap_sem);
168
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
170 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 return retcode;
173}
174
Dave Airlieeddca552007-07-11 16:09:54 +1000175static int i810_dma_get_buffer(struct drm_device * dev, drm_i810_dma_t * d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000176 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Dave Airlie056219e2007-07-11 16:17:42 +1000178 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 drm_i810_buf_priv_t *buf_priv;
180 int retcode = 0;
181
182 buf = i810_freelist_get(dev);
183 if (!buf) {
184 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return retcode;
187 }
188
Eric Anholt6c340ea2007-08-25 20:23:09 +1000189 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (retcode) {
191 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return retcode;
194 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000195 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 buf_priv = buf->dev_private;
197 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 d->request_idx = buf->idx;
199 d->request_size = buf->total;
200 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 return retcode;
203}
204
Dave Airlieeddca552007-07-11 16:09:54 +1000205static int i810_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Dave Airliecdd55a22007-07-11 16:32:08 +1000207 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Make sure interrupts are disabled here because the uninstall ioctl
210 * may not have been called from userspace and after dev_private
211 * is freed, it's too late.
212 */
213 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
214 drm_irq_uninstall(dev);
215
216 if (dev->dev_private) {
217 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 drm_i810_private_t *dev_priv =
219 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 if (dev_priv->ring.virtual_start) {
Dave Airlieb9094d32007-01-08 21:31:13 +1100222 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000224 if (dev_priv->hw_status_page) {
225 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 dev_priv->hw_status_page,
227 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 /* Need to rewrite hardware status page */
229 I810_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700231 kfree(dev->dev_private);
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");
Eric Anholt9a298b22009-03-24 12:23:04 -0700442 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443 if (dev_priv == NULL)
444 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000445 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 case I810_CLEANUP_DMA:
449 DRM_INFO("DMA Cleanup\n");
450 retcode = i810_dma_cleanup(dev);
451 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000452 default:
453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/* Most efficient way to verify state for the i810 is as it is
460 * emitted. Non-conformant state is silently dropped.
461 *
462 * Use 'volatile' & local var tmp to force the emitted values to be
463 * identical to the verified ones.
464 */
Dave Airlieeddca552007-07-11 16:09:54 +1000465static void i810EmitContextVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 int i, j = 0;
470 unsigned int tmp;
471 RING_LOCALS;
472
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475 OUT_RING(GFX_OP_COLOR_FACTOR);
476 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000478 OUT_RING(GFX_OP_STIPPLE);
479 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 tmp = code[i];
483
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000484 if ((tmp & (7 << 29)) == (3 << 29) &&
485 (tmp & (0x1f << 24)) < (0x1d << 24)) {
486 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 } else
489 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 ADVANCE_LP_RING();
496}
497
Dave Airlieeddca552007-07-11 16:09:54 +1000498static void i810EmitTexVerified(struct drm_device * dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000500 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int i, j = 0;
502 unsigned int tmp;
503 RING_LOCALS;
504
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 OUT_RING(GFX_OP_MAP_INFO);
508 OUT_RING(code[I810_TEXREG_MI1]);
509 OUT_RING(code[I810_TEXREG_MI2]);
510 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000512 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 tmp = code[i];
514
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000515 if ((tmp & (7 << 29)) == (3 << 29) &&
516 (tmp & (0x1f << 24)) < (0x1d << 24)) {
517 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 } else
520 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
523 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 ADVANCE_LP_RING();
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/* Need to do some additional checking when setting the dest buffer.
530 */
Dave Airlieeddca552007-07-11 16:09:54 +1000531static void i810EmitDestVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned int tmp;
536 RING_LOCALS;
537
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 tmp = code[I810_DESTREG_DI1];
541 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000542 OUT_RING(CMD_OP_DESTBUFFER_INFO);
543 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
546 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* invarient:
549 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 OUT_RING(CMD_OP_Z_BUFFER_INFO);
551 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 OUT_RING(GFX_OP_DESTBUFFER_VARS);
554 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000556 OUT_RING(GFX_OP_DRAWRECT_INFO);
557 OUT_RING(code[I810_DESTREG_DR1]);
558 OUT_RING(code[I810_DESTREG_DR2]);
559 OUT_RING(code[I810_DESTREG_DR3]);
560 OUT_RING(code[I810_DESTREG_DR4]);
561 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 ADVANCE_LP_RING();
564}
565
Dave Airlieeddca552007-07-11 16:09:54 +1000566static void i810EmitState(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 drm_i810_private_t *dev_priv = dev->dev_private;
569 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000571
Márton Németh3e684ea2008-01-24 15:58:57 +1000572 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
577 }
578
579 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
582 }
583
584 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000585 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
587 }
588
589 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
592 }
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/* need to verify
596 */
Dave Airlieeddca552007-07-11 16:09:54 +1000597static void i810_dma_dispatch_clear(struct drm_device * dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 unsigned int clear_color,
599 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 drm_i810_private_t *dev_priv = dev->dev_private;
602 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000604 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int pitch = dev_priv->pitch;
606 int cpp = 2;
607 int i;
608 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609
610 if (dev_priv->current_page == 1) {
611 unsigned int tmp = flags;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 if (tmp & I810_FRONT)
615 flags |= I810_BACK;
616 if (tmp & I810_BACK)
617 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 if (nbox > I810_NR_SAREA_CLIPRECTS)
623 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unsigned int x = pbox->x1;
627 unsigned int y = pbox->y1;
628 unsigned int width = (pbox->x2 - x) * cpp;
629 unsigned int height = pbox->y2 - y;
630 unsigned int start = y * pitch + x * cpp;
631
632 if (pbox->x1 > pbox->x2 ||
633 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000634 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 continue;
636
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 if (flags & I810_FRONT) {
638 BEGIN_LP_RING(6);
639 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
640 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
641 OUT_RING((height << 16) | width);
642 OUT_RING(start);
643 OUT_RING(clear_color);
644 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 ADVANCE_LP_RING();
646 }
647
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 if (flags & I810_BACK) {
649 BEGIN_LP_RING(6);
650 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
651 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
652 OUT_RING((height << 16) | width);
653 OUT_RING(dev_priv->back_offset + start);
654 OUT_RING(clear_color);
655 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 ADVANCE_LP_RING();
657 }
658
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000659 if (flags & I810_DEPTH) {
660 BEGIN_LP_RING(6);
661 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
662 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
663 OUT_RING((height << 16) | width);
664 OUT_RING(dev_priv->depth_offset + start);
665 OUT_RING(clear_zval);
666 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ADVANCE_LP_RING();
668 }
669 }
670}
671
Dave Airlieeddca552007-07-11 16:09:54 +1000672static void i810_dma_dispatch_swap(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000674 drm_i810_private_t *dev_priv = dev->dev_private;
675 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000677 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int pitch = dev_priv->pitch;
679 int cpp = 2;
680 int i;
681 RING_LOCALS;
682
683 DRM_DEBUG("swapbuffers\n");
684
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 if (nbox > I810_NR_SAREA_CLIPRECTS)
688 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned int w = pbox->x2 - pbox->x1;
692 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 unsigned int start = dst;
695
696 if (pbox->x1 > pbox->x2 ||
697 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000698 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 continue;
700
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 BEGIN_LP_RING(6);
702 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
703 OUT_RING(pitch | (0xCC << 16));
704 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 OUT_RING(dev_priv->back_offset + start);
709 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000713 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ADVANCE_LP_RING();
715 }
716}
717
Dave Airlieeddca552007-07-11 16:09:54 +1000718static void i810_dma_dispatch_vertex(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +1000719 struct drm_buf * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000724 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 unsigned long address = (unsigned long)buf->bus_address;
727 unsigned long start = address - dev->agp->base;
728 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000733 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 nbox = I810_NR_SAREA_CLIPRECTS;
735
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 used = 0;
738
739 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
743 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
744
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000745 *(u32 *) buf_priv->kernel_virtual =
746 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000749 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 used += 4;
751 }
752
753 i810_unmap_buffer(buf);
754 }
755
756 if (used) {
757 do {
758 if (i < nbox) {
759 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
761 SC_ENABLE);
762 OUT_RING(GFX_OP_SCISSOR_INFO);
763 OUT_RING(box[i].x1 | (box[i].y1 << 16));
764 OUT_RING((box[i].x2 -
765 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ADVANCE_LP_RING();
767 }
768
769 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 OUT_RING(CMD_OP_BATCH_BUFFER);
771 OUT_RING(start | BB1_PROTECTED);
772 OUT_RING(start + used - 4);
773 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 ADVANCE_LP_RING();
775
776 } while (++i < nbox);
777 }
778
779 if (discard) {
780 dev_priv->counter++;
781
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000782 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
783 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 OUT_RING(CMD_STORE_DWORD_IDX);
787 OUT_RING(20);
788 OUT_RING(dev_priv->counter);
789 OUT_RING(CMD_STORE_DWORD_IDX);
790 OUT_RING(buf_priv->my_use_idx);
791 OUT_RING(I810_BUF_FREE);
792 OUT_RING(CMD_REPORT_HEAD);
793 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 ADVANCE_LP_RING();
795 }
796}
797
Dave Airlieeddca552007-07-11 16:09:54 +1000798static void i810_dma_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int pitch = dev_priv->pitch;
802 RING_LOCALS;
803
Márton Németh3e684ea2008-01-24 15:58:57 +1000804 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 dev_priv->current_page,
806 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 i810_kernel_lost_context(dev);
809
810 BEGIN_LP_RING(2);
811 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
812 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 ADVANCE_LP_RING();
814
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* On i815 at least ASYNC is buggy */
817 /* pitch<<5 is from 11.2.8 p158,
818 its the pitch / 8 then left shifted 8,
819 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
821 if (dev_priv->current_page == 0) {
822 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 dev_priv->current_page = 1;
824 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 dev_priv->current_page = 0;
827 }
828 OUT_RING(0);
829 ADVANCE_LP_RING();
830
831 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
833 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 ADVANCE_LP_RING();
835
836 /* Increment the frame counter. The client-side 3D driver must
837 * throttle the framerate by waiting for this value before
838 * performing the swapbuffer ioctl.
839 */
840 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
841
842}
843
Dave Airlieeddca552007-07-11 16:09:54 +1000844static void i810_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 drm_i810_private_t *dev_priv = dev->dev_private;
847 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 BEGIN_LP_RING(4);
852 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
853 OUT_RING(CMD_REPORT_HEAD);
854 OUT_RING(0);
855 OUT_RING(0);
856 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Dave Airlieeddca552007-07-11 16:09:54 +1000861static int i810_flush_queue(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000864 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 int i, ret = 0;
866 RING_LOCALS;
867
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 BEGIN_LP_RING(2);
871 OUT_RING(CMD_REPORT_HEAD);
872 OUT_RING(0);
873 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000878 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
882 I810_BUF_FREE);
883
884 if (used == I810_BUF_HARDWARE)
885 DRM_DEBUG("reclaimed from HARDWARE\n");
886 if (used == I810_BUF_CLIENT)
887 DRM_DEBUG("still on client\n");
888 }
889
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/* Must be called with the lock held */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000894static void i810_reclaim_buffers(struct drm_device * dev,
895 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Dave Airliecdd55a22007-07-11 16:32:08 +1000897 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 if (!dma)
901 return;
902 if (!dev->dev_private)
903 return;
904 if (!dma->buflist)
905 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000910 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Eric Anholt6c340ea2007-08-25 20:23:09 +1000913 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
915 I810_BUF_FREE);
916
917 if (used == I810_BUF_CLIENT)
918 DRM_DEBUG("reclaimed from client\n");
919 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 }
923}
924
Eric Anholtc153f452007-09-03 12:06:45 +1000925static int i810_flush_ioctl(struct drm_device *dev, void *data,
926 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000928 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 i810_flush_queue(dev);
931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Eric Anholtc153f452007-09-03 12:06:45 +1000934static int i810_dma_vertex(struct drm_device *dev, void *data,
935 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Dave Airliecdd55a22007-07-11 16:32:08 +1000937 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
939 u32 *hw_status = dev_priv->hw_status_page;
940 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
941 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000942 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Eric Anholt6c340ea2007-08-25 20:23:09 +1000944 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Márton Németh3e684ea2008-01-24 15:58:57 +1000946 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000947 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Eric Anholtc153f452007-09-03 12:06:45 +1000949 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return -EINVAL;
951
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000953 dma->buflist[vertex->idx],
954 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Eric Anholtc153f452007-09-03 12:06:45 +1000956 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000958 sarea_priv->last_enqueue = dev_priv->counter - 1;
959 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 return 0;
962}
963
Eric Anholtc153f452007-09-03 12:06:45 +1000964static int i810_clear_bufs(struct drm_device *dev, void *data,
965 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Eric Anholtc153f452007-09-03 12:06:45 +1000967 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Eric Anholt6c340ea2007-08-25 20:23:09 +1000969 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 /* GH: Someone's doing nasty things... */
972 if (!dev->dev_private) {
973 return -EINVAL;
974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Eric Anholtc153f452007-09-03 12:06:45 +1000976 i810_dma_dispatch_clear(dev, clear->flags,
977 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Eric Anholtc153f452007-09-03 12:06:45 +1000981static int i810_swap_bufs(struct drm_device *dev, void *data,
982 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Márton Németh3e684ea2008-01-24 15:58:57 +1000984 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Eric Anholt6c340ea2007-08-25 20:23:09 +1000986 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000988 i810_dma_dispatch_swap(dev);
989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Eric Anholtc153f452007-09-03 12:06:45 +1000992static int i810_getage(struct drm_device *dev, void *data,
993 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
996 u32 *hw_status = dev_priv->hw_status_page;
997 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
998 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return 0;
1002}
1003
Eric Anholtc153f452007-09-03 12:06:45 +10001004static int i810_getbuf(struct drm_device *dev, void *data,
1005 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001008 drm_i810_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1010 u32 *hw_status = dev_priv->hw_status_page;
1011 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1012 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Eric Anholt6c340ea2007-08-25 20:23:09 +10001014 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Eric Anholtc153f452007-09-03 12:06:45 +10001016 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Eric Anholtc153f452007-09-03 12:06:45 +10001018 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001021 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 return retcode;
1026}
1027
Eric Anholtc153f452007-09-03 12:06:45 +10001028static int i810_copybuf(struct drm_device *dev, void *data,
1029 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 /* Never copy - 2.4.x doesn't need it */
1032 return 0;
1033}
1034
Eric Anholtc153f452007-09-03 12:06:45 +10001035static int i810_docopy(struct drm_device *dev, void *data,
1036 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 /* Never copy - 2.4.x doesn't need it */
1039 return 0;
1040}
1041
Dave Airlie056219e2007-07-11 16:17:42 +10001042static void i810_dma_dispatch_mc(struct drm_device * dev, struct drm_buf * buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 drm_i810_private_t *dev_priv = dev->dev_private;
1046 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1047 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1048 unsigned long address = (unsigned long)buf->bus_address;
1049 unsigned long start = address - dev->agp->base;
1050 int u;
1051 RING_LOCALS;
1052
1053 i810_kernel_lost_context(dev);
1054
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (u != I810_BUF_CLIENT) {
1057 DRM_DEBUG("MC found buffer that isn't mine!\n");
1058 }
1059
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 used = 0;
1062
1063 sarea_priv->dirty = 0x7f;
1064
Márton Németh3e684ea2008-01-24 15:58:57 +10001065 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 dev_priv->counter++;
1068 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 DRM_DEBUG("start : %lx\n", start);
1070 DRM_DEBUG("used : %d\n", used);
1071 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1072
1073 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1074 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001075 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 used += 4;
1077 }
1078
1079 i810_unmap_buffer(buf);
1080 }
1081 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 OUT_RING(CMD_OP_BATCH_BUFFER);
1083 OUT_RING(start | BB1_PROTECTED);
1084 OUT_RING(start + used - 4);
1085 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 ADVANCE_LP_RING();
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 OUT_RING(CMD_STORE_DWORD_IDX);
1090 OUT_RING(buf_priv->my_use_idx);
1091 OUT_RING(I810_BUF_FREE);
1092 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 OUT_RING(CMD_STORE_DWORD_IDX);
1095 OUT_RING(16);
1096 OUT_RING(last_render);
1097 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 ADVANCE_LP_RING();
1099}
1100
Eric Anholtc153f452007-09-03 12:06:45 +10001101static int i810_dma_mc(struct drm_device *dev, void *data,
1102 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Dave Airliecdd55a22007-07-11 16:32:08 +10001104 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 u32 *hw_status = dev_priv->hw_status_page;
1107 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001109 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Eric Anholt6c340ea2007-08-25 20:23:09 +10001111 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Eric Anholtc153f452007-09-03 12:06:45 +10001113 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return -EINVAL;
1115
Eric Anholtc153f452007-09-03 12:06:45 +10001116 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1117 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Eric Anholtc153f452007-09-03 12:06:45 +10001119 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001121 sarea_priv->last_enqueue = dev_priv->counter - 1;
1122 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 return 0;
1125}
1126
Eric Anholtc153f452007-09-03 12:06:45 +10001127static int i810_rstatus(struct drm_device *dev, void *data,
1128 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
Eric Anholtc153f452007-09-03 12:06:45 +10001135static int i810_ov0_info(struct drm_device *dev, void *data,
1136 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001139 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Eric Anholtc153f452007-09-03 12:06:45 +10001141 ov->offset = dev_priv->overlay_offset;
1142 ov->physical = dev_priv->overlay_physical;
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return 0;
1145}
1146
Eric Anholtc153f452007-09-03 12:06:45 +10001147static int i810_fstatus(struct drm_device *dev, void *data,
1148 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001150 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Eric Anholt6c340ea2007-08-25 20:23:09 +10001152 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return I810_READ(0x30008);
1154}
1155
Eric Anholtc153f452007-09-03 12:06:45 +10001156static int i810_ov0_flip(struct drm_device *dev, void *data,
1157 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001159 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Eric Anholt6c340ea2007-08-25 20:23:09 +10001161 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 //Tell the overlay to update
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001164 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 return 0;
1167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001170 */
Dave Airlieeddca552007-07-11 16:09:54 +10001171static void i810_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001174
Márton Németh3e684ea2008-01-24 15:58:57 +10001175 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 dev_priv->page_flipping = 1;
1177 dev_priv->current_page = 0;
1178 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1179}
1180
Dave Airlieeddca552007-07-11 16:09:54 +10001181static int i810_do_cleanup_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
1183 drm_i810_private_t *dev_priv = dev->dev_private;
1184
Márton Németh3e684ea2008-01-24 15:58:57 +10001185 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001187 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 dev_priv->page_flipping = 0;
1190 return 0;
1191}
1192
Eric Anholtc153f452007-09-03 12:06:45 +10001193static int i810_flip_bufs(struct drm_device *dev, void *data,
1194 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 drm_i810_private_t *dev_priv = dev->dev_private;
1197
Márton Németh3e684ea2008-01-24 15:58:57 +10001198 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Eric Anholt6c340ea2007-08-25 20:23:09 +10001200 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001202 if (!dev_priv->page_flipping)
1203 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001205 i810_dma_dispatch_flip(dev);
1206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Dave Airlieeddca552007-07-11 16:09:54 +10001209int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001210{
1211 /* i810 has 4 more counters */
1212 dev->counters += 4;
1213 dev->types[6] = _DRM_STAT_IRQ;
1214 dev->types[7] = _DRM_STAT_PRIMARY;
1215 dev->types[8] = _DRM_STAT_SECONDARY;
1216 dev->types[9] = _DRM_STAT_DMA;
1217
1218 return 0;
1219}
1220
Dave Airlieeddca552007-07-11 16:09:54 +10001221void i810_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001223 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Eric Anholt6c340ea2007-08-25 20:23:09 +10001226void i810_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 if (dev->dev_private) {
1229 drm_i810_private_t *dev_priv = dev->dev_private;
1230 if (dev_priv->page_flipping) {
1231 i810_do_cleanup_pageflip(dev);
1232 }
1233 }
1234}
1235
Eric Anholt6c340ea2007-08-25 20:23:09 +10001236void i810_driver_reclaim_buffers_locked(struct drm_device * dev,
1237 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001239 i810_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Dave Airlieeddca552007-07-11 16:09:54 +10001242int i810_driver_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001244 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return 0;
1246}
1247
Eric Anholtc153f452007-09-03 12:06:45 +10001248struct drm_ioctl_desc i810_ioctls[] = {
1249 DRM_IOCTL_DEF(DRM_I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1250 DRM_IOCTL_DEF(DRM_I810_VERTEX, i810_dma_vertex, DRM_AUTH),
1251 DRM_IOCTL_DEF(DRM_I810_CLEAR, i810_clear_bufs, DRM_AUTH),
1252 DRM_IOCTL_DEF(DRM_I810_FLUSH, i810_flush_ioctl, DRM_AUTH),
1253 DRM_IOCTL_DEF(DRM_I810_GETAGE, i810_getage, DRM_AUTH),
1254 DRM_IOCTL_DEF(DRM_I810_GETBUF, i810_getbuf, DRM_AUTH),
1255 DRM_IOCTL_DEF(DRM_I810_SWAP, i810_swap_bufs, DRM_AUTH),
1256 DRM_IOCTL_DEF(DRM_I810_COPY, i810_copybuf, DRM_AUTH),
1257 DRM_IOCTL_DEF(DRM_I810_DOCOPY, i810_docopy, DRM_AUTH),
1258 DRM_IOCTL_DEF(DRM_I810_OV0INFO, i810_ov0_info, DRM_AUTH),
1259 DRM_IOCTL_DEF(DRM_I810_FSTATUS, i810_fstatus, DRM_AUTH),
1260 DRM_IOCTL_DEF(DRM_I810_OV0FLIP, i810_ov0_flip, DRM_AUTH),
1261 DRM_IOCTL_DEF(DRM_I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1262 DRM_IOCTL_DEF(DRM_I810_RSTATUS, i810_rstatus, DRM_AUTH),
1263 DRM_IOCTL_DEF(DRM_I810_FLIP, i810_flip_bufs, DRM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264};
1265
1266int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001267
1268/**
1269 * Determine if the device really is AGP or not.
1270 *
1271 * All Intel graphics chipsets are treated as AGP, even if they are really
1272 * PCI-e.
1273 *
1274 * \param dev The device to be tested.
1275 *
1276 * \returns
1277 * A value of 1 is always retured to indictate every i810 is AGP.
1278 */
Dave Airlieeddca552007-07-11 16:09:54 +10001279int i810_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001280{
1281 return 1;
1282}