blob: 7ee85ea507cecdc3d2b6548a13b528738a07ef94 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i830_dma.c -- DMA support for the I830 -*- linux-c -*-
2 * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
Dave Airlieb5e89ed2005-09-25 14:28:13 +100014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Abraham vd Merwe <abraham@2d3d.co.za>
31 *
32 */
33
34#include "drmP.h"
35#include "drm.h"
36#include "i830_drm.h"
37#include "i830_drv.h"
38#include <linux/interrupt.h> /* For task queue support */
Harvey Harrison21534302008-02-13 15:03:17 -080039#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43
44#define I830_BUF_FREE 2
45#define I830_BUF_CLIENT 1
Dave Airliebc5f4522007-11-05 12:50:58 +100046#define I830_BUF_HARDWARE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define I830_BUF_UNMAPPED 0
49#define I830_BUF_MAPPED 1
50
Dave Airlie056219e2007-07-11 16:17:42 +100051static struct drm_buf *i830_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Dave Airliecdd55a22007-07-11 16:32:08 +100053 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100054 int i;
55 int used;
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 /* Linear search might not be the best solution */
58
Dave Airlieb5e89ed2005-09-25 14:28:13 +100059 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +100060 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100061 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100063 used = cmpxchg(buf_priv->in_use, I830_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 I830_BUF_CLIENT);
Nicolas Kaiser56499112010-07-15 00:14:43 +020065 if (used == I830_BUF_FREE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71/* This should only be called if the buffer is not sent to the hardware
72 * yet, the hardware updates in use for us once its on the ring buffer.
73 */
74
Nicolas Kaiser56499112010-07-15 00:14:43 +020075static int i830_freelist_put(struct drm_device *dev, struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
78 int used;
79
80 /* In use is already a pointer */
81 used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT, I830_BUF_FREE);
82 if (used != I830_BUF_CLIENT) {
83 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
84 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086
87 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Dave Airliec94f7022005-07-07 21:03:38 +100090static int i830_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Dave Airlieeddca552007-07-11 16:09:54 +100092 struct drm_file *priv = filp->private_data;
93 struct drm_device *dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100094 drm_i830_private_t *dev_priv;
Dave Airlie056219e2007-07-11 16:17:42 +100095 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 drm_i830_buf_priv_t *buf_priv;
97
98 lock_kernel();
Dave Airlie2c14f282008-04-21 16:47:32 +100099 dev = priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000101 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
105 vma->vm_file = filp;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106
107 buf_priv->currently_mapped = I830_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unlock_kernel();
109
110 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlie3d774612006-08-07 20:07:43 +1000111 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112 vma->vm_end - vma->vm_start, vma->vm_page_prot))
113 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 0;
115}
116
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800117static const struct file_operations i830_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000118 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000119 .release = drm_release,
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000120 .unlocked_ioctl = drm_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000121 .mmap = i830_mmap_buffers,
122 .fasync = drm_fasync,
Dave Airliec94f7022005-07-07 21:03:38 +1000123};
124
Nicolas Kaiser56499112010-07-15 00:14:43 +0200125static int i830_map_buffer(struct drm_buf *buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Dave Airlie2c14f282008-04-21 16:47:32 +1000127 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 drm_i830_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800130 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned long virtual;
132 int retcode = 0;
133
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000134 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
135 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000137 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000138 old_fops = file_priv->filp->f_op;
139 file_priv->filp->f_op = &i830_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 dev_priv->mmap_buffer = buf;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000141 virtual = do_mmap(file_priv->filp, 0, buf->total, PROT_READ | PROT_WRITE,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000142 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000144 file_priv->filp->f_op = old_fops;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000145 if (IS_ERR((void *)virtual)) { /* ugh */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /* Real error */
147 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000148 retcode = PTR_ERR((void *)virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 buf_priv->virtual = NULL;
150 } else {
151 buf_priv->virtual = (void __user *)virtual;
152 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000153 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return retcode;
156}
157
Nicolas Kaiser56499112010-07-15 00:14:43 +0200158static int i830_unmap_buffer(struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
161 int retcode = 0;
162
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163 if (buf_priv->currently_mapped != I830_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -EINVAL;
165
166 down_write(&current->mm->mmap_sem);
167 retcode = do_munmap(current->mm,
168 (unsigned long)buf_priv->virtual,
169 (size_t) buf->total);
170 up_write(&current->mm->mmap_sem);
171
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
173 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 return retcode;
176}
177
Nicolas Kaiser56499112010-07-15 00:14:43 +0200178static int i830_dma_get_buffer(struct drm_device *dev, drm_i830_dma_t *d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000179 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Dave Airlie056219e2007-07-11 16:17:42 +1000181 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 drm_i830_buf_priv_t *buf_priv;
183 int retcode = 0;
184
185 buf = i830_freelist_get(dev);
186 if (!buf) {
187 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return retcode;
190 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191
Eric Anholt6c340ea2007-08-25 20:23:09 +1000192 retcode = i830_map_buffer(buf, file_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 if (retcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 i830_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000195 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return retcode;
197 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000198 buf->file_priv = file_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000201 d->request_idx = buf->idx;
202 d->request_size = buf->total;
203 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 return retcode;
206}
207
Nicolas Kaiser56499112010-07-15 00:14:43 +0200208static int i830_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Dave Airliecdd55a22007-07-11 16:32:08 +1000210 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* Make sure interrupts are disabled here because the uninstall ioctl
213 * may not have been called from userspace and after dev_private
214 * is freed, it's too late.
215 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 if (dev->irq_enabled)
217 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (dev->dev_private) {
220 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000221 drm_i830_private_t *dev_priv =
222 (drm_i830_private_t *) dev->dev_private;
223
Nicolas Kaiser56499112010-07-15 00:14:43 +0200224 if (dev_priv->ring.virtual_start)
Dave Airlieb9094d32007-01-08 21:31:13 +1100225 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 if (dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 pci_free_consistent(dev->pdev, PAGE_SIZE,
228 dev_priv->hw_status_page,
229 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000230 /* Need to rewrite hardware status page */
231 I830_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Eric Anholt9a298b22009-03-24 12:23:04 -0700234 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000238 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000240 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100241 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Nicolas Kaiser56499112010-07-15 00:14:43 +0200247int i830_wait_ring(struct drm_device *dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249 drm_i830_private_t *dev_priv = dev->dev_private;
250 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
251 int iters = 0;
252 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned int last_head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
254
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000255 end = jiffies + (HZ * 3);
256 while (ring->space < n) {
257 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
258 ring->space = ring->head - (ring->tail + 8);
259 if (ring->space < 0)
260 ring->space += ring->Size;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (ring->head != last_head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263 end = jiffies + (HZ * 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 last_head = ring->head;
265 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266
267 iters++;
268 if (time_before(end, jiffies)) {
269 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
270 DRM_ERROR("lockup\n");
271 goto out_wait_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 udelay(1);
274 dev_priv->sarea_priv->perf_boxes |= I830_BOX_WAIT;
275 }
276
Nicolas Kaiser56499112010-07-15 00:14:43 +0200277out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278 return iters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Nicolas Kaiser56499112010-07-15 00:14:43 +0200281static void i830_kernel_lost_context(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283 drm_i830_private_t *dev_priv = dev->dev_private;
284 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
285
286 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
287 ring->tail = I830_READ(LP_RING + RING_TAIL) & TAIL_ADDR;
288 ring->space = ring->head - (ring->tail + 8);
289 if (ring->space < 0)
290 ring->space += ring->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (ring->head == ring->tail)
293 dev_priv->sarea_priv->perf_boxes |= I830_BOX_RING_EMPTY;
294}
295
Nicolas Kaiser56499112010-07-15 00:14:43 +0200296static int i830_freelist_init(struct drm_device *dev, drm_i830_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Dave Airliecdd55a22007-07-11 16:32:08 +1000298 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299 int my_idx = 36;
300 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
301 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000303 if (dma->buf_count > 1019) {
304 /* Not enough space in the status page for the freelist */
305 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000308 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000309 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000312 buf_priv->in_use = hw_status++;
313 buf_priv->my_use_idx = my_idx;
314 my_idx += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 *buf_priv->in_use = I830_BUF_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Dave Airlieb9094d32007-01-08 21:31:13 +1100318 buf_priv->map.offset = buf->bus_address;
319 buf_priv->map.size = buf->total;
320 buf_priv->map.type = _DRM_AGP;
321 buf_priv->map.flags = 0;
322 buf_priv->map.mtrr = 0;
323
324 drm_core_ioremap(&buf_priv->map, dev);
325 buf_priv->kernel_virtual = buf_priv->map.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 return 0;
328}
329
Nicolas Kaiser56499112010-07-15 00:14:43 +0200330static int i830_dma_initialize(struct drm_device *dev,
331 drm_i830_private_t *dev_priv,
332 drm_i830_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Dave Airlie55910512007-07-11 16:53:40 +1000334 struct drm_map_list *r_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 memset(dev_priv, 0, sizeof(drm_i830_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Dave Airliebd1b3312007-05-26 05:01:51 +1000338 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 if (r_list->map &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 break;
344 }
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 if (!dev_priv->sarea_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 dev->dev_private = (void *)dev_priv;
349 i830_dma_cleanup(dev);
350 DRM_ERROR("can not find sarea!\n");
351 return -EINVAL;
352 }
353 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 if (!dev_priv->mmio_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dev->dev_private = (void *)dev_priv;
356 i830_dma_cleanup(dev);
357 DRM_ERROR("can not find mmio map!\n");
358 return -EINVAL;
359 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000360 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000362 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 dev->dev_private = (void *)dev_priv;
364 i830_dma_cleanup(dev);
365 DRM_ERROR("can not find dma buffer map!\n");
366 return -EINVAL;
367 }
368
369 dev_priv->sarea_priv = (drm_i830_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000370 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372 dev_priv->ring.Start = init->ring_start;
373 dev_priv->ring.End = init->ring_end;
374 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dave Airlieb9094d32007-01-08 21:31:13 +1100376 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
377 dev_priv->ring.map.size = init->ring_size;
378 dev_priv->ring.map.type = _DRM_AGP;
379 dev_priv->ring.map.flags = 0;
380 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Dave Airlieb9094d32007-01-08 21:31:13 +1100382 drm_core_ioremap(&dev_priv->ring.map, dev);
383
384 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000385 dev->dev_private = (void *)dev_priv;
386 i830_dma_cleanup(dev);
387 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000389 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
Dave Airlieb9094d32007-01-08 21:31:13 +1100392 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
393
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000394 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 dev_priv->w = init->w;
397 dev_priv->h = init->h;
398 dev_priv->pitch = init->pitch;
399 dev_priv->back_offset = init->back_offset;
400 dev_priv->depth_offset = init->depth_offset;
401 dev_priv->front_offset = init->front_offset;
402
403 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
404 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
405 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
406
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000407 DRM_DEBUG("front_di1 %x\n", dev_priv->front_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 DRM_DEBUG("back_offset %x\n", dev_priv->back_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000409 DRM_DEBUG("back_di1 %x\n", dev_priv->back_di1);
410 DRM_DEBUG("pitch_bits %x\n", init->pitch_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 dev_priv->cpp = init->cpp;
413 /* We are using separate values as placeholders for mechanisms for
414 * private backbuffer/depthbuffer usage.
415 */
416
417 dev_priv->back_pitch = init->back_pitch;
418 dev_priv->depth_pitch = init->depth_pitch;
419 dev_priv->do_boxes = 0;
420 dev_priv->use_mi_batchbuffer_start = 0;
421
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 /* Program Hardware Status Page */
423 dev_priv->hw_status_page =
424 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
425 &dev_priv->dma_status_page);
426 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 dev->dev_private = (void *)dev_priv;
428 i830_dma_cleanup(dev);
429 DRM_ERROR("Can not allocate hardware status page\n");
430 return -ENOMEM;
431 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000432 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000434
435 I830_WRITE(0x02080, dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 DRM_DEBUG("Enabled hardware status page\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000437
438 /* Now we need to init our freelist */
439 if (i830_freelist_init(dev, dev_priv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 i830_dma_cleanup(dev);
442 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 dev->dev_private = (void *)dev_priv;
447
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Eric Anholtc153f452007-09-03 12:06:45 +1000451static int i830_dma_init(struct drm_device *dev, void *data,
452 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000454 drm_i830_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000455 drm_i830_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 int retcode = 0;
457
Eric Anholtc153f452007-09-03 12:06:45 +1000458 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000459 case I830_INIT_DMA:
Eric Anholt9a298b22009-03-24 12:23:04 -0700460 dev_priv = kmalloc(sizeof(drm_i830_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 if (dev_priv == NULL)
462 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000463 retcode = i830_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000464 break;
465 case I830_CLEANUP_DMA:
466 retcode = i830_dma_cleanup(dev);
467 break;
468 default:
469 retcode = -EINVAL;
470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000472
473 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476#define GFX_OP_STIPPLE ((0x3<<29)|(0x1d<<24)|(0x83<<16))
477#define ST1_ENABLE (1<<16)
478#define ST1_MASK (0xffff)
479
480/* Most efficient way to verify state for the i830 is as it is
481 * emitted. Non-conformant state is silently dropped.
482 */
Nicolas Kaiser56499112010-07-15 00:14:43 +0200483static void i830EmitContextVerified(struct drm_device *dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int i, j = 0;
487 unsigned int tmp;
488 RING_LOCALS;
489
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 BEGIN_LP_RING(I830_CTX_SETUP_SIZE + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 for (i = 0; i < I830_CTXREG_BLENDCOLR0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 if ((tmp & (7 << 29)) == CMD_3D &&
495 (tmp & (0x1f << 24)) < (0x1d << 24)) {
496 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 j++;
498 } else {
499 DRM_ERROR("Skipping %d\n", i);
500 }
501 }
502
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000503 OUT_RING(STATE3D_CONST_BLEND_COLOR_CMD);
504 OUT_RING(code[I830_CTXREG_BLENDCOLR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 j += 2;
506
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 for (i = I830_CTXREG_VF; i < I830_CTXREG_MCSB0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000509 if ((tmp & (7 << 29)) == CMD_3D &&
510 (tmp & (0x1f << 24)) < (0x1d << 24)) {
511 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 j++;
513 } else {
514 DRM_ERROR("Skipping %d\n", i);
515 }
516 }
517
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000518 OUT_RING(STATE3D_MAP_COORD_SETBIND_CMD);
519 OUT_RING(code[I830_CTXREG_MCSB1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 j += 2;
521
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000522 if (j & 1)
523 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 ADVANCE_LP_RING();
526}
527
Nicolas Kaiser56499112010-07-15 00:14:43 +0200528static void i830EmitTexVerified(struct drm_device *dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int i, j = 0;
532 unsigned int tmp;
533 RING_LOCALS;
534
535 if (code[I830_TEXREG_MI0] == GFX_OP_MAP_INFO ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000536 (code[I830_TEXREG_MI0] & ~(0xf * LOAD_TEXTURE_MAP0)) ==
537 (STATE3D_LOAD_STATE_IMMEDIATE_2 | 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 BEGIN_LP_RING(I830_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 OUT_RING(code[I830_TEXREG_MI0]); /* TM0LI */
542 OUT_RING(code[I830_TEXREG_MI1]); /* TM0S0 */
543 OUT_RING(code[I830_TEXREG_MI2]); /* TM0S1 */
544 OUT_RING(code[I830_TEXREG_MI3]); /* TM0S2 */
545 OUT_RING(code[I830_TEXREG_MI4]); /* TM0S3 */
546 OUT_RING(code[I830_TEXREG_MI5]); /* TM0S4 */
547
548 for (i = 6; i < I830_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 if (j & 1)
555 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000558 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 printk("rejected packet %x\n", code[0]);
560}
561
Nicolas Kaiser56499112010-07-15 00:14:43 +0200562static void i830EmitTexBlendVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 unsigned int *code, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int i, j = 0;
567 unsigned int tmp;
568 RING_LOCALS;
569
570 if (!num)
571 return;
572
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000573 BEGIN_LP_RING(num + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 j++;
579 }
580
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000581 if (j & 1)
582 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 ADVANCE_LP_RING();
585}
586
Nicolas Kaiser56499112010-07-15 00:14:43 +0200587static void i830EmitTexPalette(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 unsigned int *palette, int number, int is_shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int i;
592 RING_LOCALS;
593
594 return;
595
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 BEGIN_LP_RING(258);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 if (is_shared == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 OUT_RING(CMD_OP_MAP_PALETTE_LOAD |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000600 MAP_PALETTE_NUM(0) | MAP_PALETTE_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 } else {
602 OUT_RING(CMD_OP_MAP_PALETTE_LOAD | MAP_PALETTE_NUM(number));
603 }
Nicolas Kaiser56499112010-07-15 00:14:43 +0200604 for (i = 0; i < 256; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 OUT_RING(palette[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 OUT_RING(0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000607 /* KW: WHERE IS THE ADVANCE_LP_RING? This is effectively a noop!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
609}
610
611/* Need to do some additional checking when setting the dest buffer.
612 */
Nicolas Kaiser56499112010-07-15 00:14:43 +0200613static void i830EmitDestVerified(struct drm_device *dev, unsigned int *code)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614{
615 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 unsigned int tmp;
617 RING_LOCALS;
618
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 BEGIN_LP_RING(I830_DEST_SETUP_SIZE + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 tmp = code[I830_DESTREG_CBUFADDR];
622 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
623 if (((int)outring) & 8) {
624 OUT_RING(0);
625 OUT_RING(0);
626 }
627
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000628 OUT_RING(CMD_OP_DESTBUFFER_INFO);
629 OUT_RING(BUF_3D_ID_COLOR_BACK |
630 BUF_3D_PITCH(dev_priv->back_pitch * dev_priv->cpp) |
631 BUF_3D_USE_FENCE);
632 OUT_RING(tmp);
633 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 OUT_RING(CMD_OP_DESTBUFFER_INFO);
636 OUT_RING(BUF_3D_ID_DEPTH | BUF_3D_USE_FENCE |
637 BUF_3D_PITCH(dev_priv->depth_pitch * dev_priv->cpp));
638 OUT_RING(dev_priv->zi1);
639 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 } else {
641 DRM_ERROR("bad di1 %x (allow %x or %x)\n",
642 tmp, dev_priv->front_di1, dev_priv->back_di1);
643 }
644
645 /* invarient:
646 */
647
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 OUT_RING(GFX_OP_DESTBUFFER_VARS);
649 OUT_RING(code[I830_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000651 OUT_RING(GFX_OP_DRAWRECT_INFO);
652 OUT_RING(code[I830_DESTREG_DR1]);
653 OUT_RING(code[I830_DESTREG_DR2]);
654 OUT_RING(code[I830_DESTREG_DR3]);
655 OUT_RING(code[I830_DESTREG_DR4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 /* Need to verify this */
658 tmp = code[I830_DESTREG_SENABLE];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000659 if ((tmp & ~0x3) == GFX_OP_SCISSOR_ENABLE) {
660 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 } else {
662 DRM_ERROR("bad scissor enable\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000663 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 OUT_RING(GFX_OP_SCISSOR_RECT);
667 OUT_RING(code[I830_DESTREG_SR1]);
668 OUT_RING(code[I830_DESTREG_SR2]);
669 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 ADVANCE_LP_RING();
672}
673
Nicolas Kaiser56499112010-07-15 00:14:43 +0200674static void i830EmitStippleVerified(struct drm_device *dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000676 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 RING_LOCALS;
678
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000679 BEGIN_LP_RING(2);
680 OUT_RING(GFX_OP_STIPPLE);
681 OUT_RING(code[1]);
682 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Nicolas Kaiser56499112010-07-15 00:14:43 +0200685static void i830EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 drm_i830_private_t *dev_priv = dev->dev_private;
688 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned int dirty = sarea_priv->dirty;
690
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700691 DRM_DEBUG("%s %x\n", __func__, dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (dirty & I830_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 i830EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 sarea_priv->dirty &= ~I830_UPLOAD_BUFFERS;
696 }
697
698 if (dirty & I830_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 i830EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 sarea_priv->dirty &= ~I830_UPLOAD_CTX;
701 }
702
703 if (dirty & I830_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 i830EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 sarea_priv->dirty &= ~I830_UPLOAD_TEX0;
706 }
707
708 if (dirty & I830_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 i830EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 sarea_priv->dirty &= ~I830_UPLOAD_TEX1;
711 }
712
713 if (dirty & I830_UPLOAD_TEXBLEND0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[0],
715 sarea_priv->TexBlendStateWordsUsed[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND0;
717 }
718
719 if (dirty & I830_UPLOAD_TEXBLEND1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[1],
721 sarea_priv->TexBlendStateWordsUsed[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND1;
723 }
724
725 if (dirty & I830_UPLOAD_TEX_PALETTE_SHARED) {
726 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 1);
727 } else {
728 if (dirty & I830_UPLOAD_TEX_PALETTE_N(0)) {
729 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 0);
730 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(0);
731 }
732 if (dirty & I830_UPLOAD_TEX_PALETTE_N(1)) {
733 i830EmitTexPalette(dev, sarea_priv->Palette[1], 1, 0);
734 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(1);
735 }
736
737 /* 1.3:
738 */
739#if 0
740 if (dirty & I830_UPLOAD_TEX_PALETTE_N(2)) {
741 i830EmitTexPalette(dev, sarea_priv->Palette2[0], 0, 0);
742 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
743 }
744 if (dirty & I830_UPLOAD_TEX_PALETTE_N(3)) {
745 i830EmitTexPalette(dev, sarea_priv->Palette2[1], 1, 0);
746 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
747 }
748#endif
749 }
750
751 /* 1.3:
752 */
753 if (dirty & I830_UPLOAD_STIPPLE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000754 i830EmitStippleVerified(dev, sarea_priv->StippleState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 sarea_priv->dirty &= ~I830_UPLOAD_STIPPLE;
756 }
757
758 if (dirty & I830_UPLOAD_TEX2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 i830EmitTexVerified(dev, sarea_priv->TexState2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 sarea_priv->dirty &= ~I830_UPLOAD_TEX2;
761 }
762
763 if (dirty & I830_UPLOAD_TEX3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000764 i830EmitTexVerified(dev, sarea_priv->TexState3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 sarea_priv->dirty &= ~I830_UPLOAD_TEX3;
766 }
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (dirty & I830_UPLOAD_TEXBLEND2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 i830EmitTexBlendVerified(dev,
770 sarea_priv->TexBlendState2,
771 sarea_priv->TexBlendStateWordsUsed2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND2;
774 }
775
776 if (dirty & I830_UPLOAD_TEXBLEND3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000777 i830EmitTexBlendVerified(dev,
778 sarea_priv->TexBlendState3,
779 sarea_priv->TexBlendStateWordsUsed3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND3;
781 }
782}
783
784/* ================================================================
785 * Performance monitoring functions
786 */
787
Nicolas Kaiser56499112010-07-15 00:14:43 +0200788static void i830_fill_box(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 u32 color;
793 unsigned int BR13, CMD;
794 RING_LOCALS;
795
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 BR13 = (0xF0 << 16) | (dev_priv->pitch * dev_priv->cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 CMD = XY_COLOR_BLT_CMD;
798 x += dev_priv->sarea_priv->boxes[0].x1;
799 y += dev_priv->sarea_priv->boxes[0].y1;
800
801 if (dev_priv->cpp == 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 BR13 |= (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 CMD |= (XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 } else {
806 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 BEGIN_LP_RING(6);
811 OUT_RING(CMD);
812 OUT_RING(BR13);
813 OUT_RING((y << 16) | x);
814 OUT_RING(((y + h) << 16) | (x + w));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Nicolas Kaiser56499112010-07-15 00:14:43 +0200816 if (dev_priv->current_page == 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 OUT_RING(dev_priv->front_offset);
Nicolas Kaiser56499112010-07-15 00:14:43 +0200818 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ADVANCE_LP_RING();
823}
824
Nicolas Kaiser56499112010-07-15 00:14:43 +0200825static void i830_cp_performance_boxes(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* Purple box for page flipping
830 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_FLIP)
832 i830_fill_box(dev, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /* Red box if we have to wait for idle at any point
835 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_WAIT)
837 i830_fill_box(dev, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* Blue box: lost context?
840 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000841 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_LOST_CONTEXT)
842 i830_fill_box(dev, 28, 4, 8, 8, 0, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* Yellow box for texture swaps
845 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_TEXTURE_LOAD)
847 i830_fill_box(dev, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* Green box if hardware never idles (as far as we can tell)
850 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 if (!(dev_priv->sarea_priv->perf_boxes & I830_BOX_RING_EMPTY))
852 i830_fill_box(dev, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * (not a great measure, easily confused)
856 */
857 if (dev_priv->dma_used) {
858 int bar = dev_priv->dma_used / 10240;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 if (bar > 100)
860 bar = 100;
861 if (bar < 1)
862 bar = 1;
863 i830_fill_box(dev, 4, 16, bar, 4, 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 dev_priv->dma_used = 0;
865 }
866
867 dev_priv->sarea_priv->perf_boxes = 0;
868}
869
Nicolas Kaiser56499112010-07-15 00:14:43 +0200870static void i830_dma_dispatch_clear(struct drm_device *dev, int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 unsigned int clear_color,
872 unsigned int clear_zval,
873 unsigned int clear_depthmask)
874{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 drm_i830_private_t *dev_priv = dev->dev_private;
876 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000878 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int pitch = dev_priv->pitch;
880 int cpp = dev_priv->cpp;
881 int i;
882 unsigned int BR13, CMD, D_CMD;
883 RING_LOCALS;
884
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000885 if (dev_priv->current_page == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 unsigned int tmp = flags;
887
888 flags &= ~(I830_FRONT | I830_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 if (tmp & I830_FRONT)
890 flags |= I830_BACK;
891 if (tmp & I830_BACK)
892 flags |= I830_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 switch (cpp) {
898 case 2:
899 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 D_CMD = CMD = XY_COLOR_BLT_CMD;
901 break;
902 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000903 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24) | (1 << 25);
904 CMD = (XY_COLOR_BLT_CMD | XY_COLOR_BLT_WRITE_ALPHA |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 XY_COLOR_BLT_WRITE_RGB);
906 D_CMD = XY_COLOR_BLT_CMD;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 if (clear_depthmask & 0x00ffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 D_CMD |= XY_COLOR_BLT_WRITE_RGB;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 if (clear_depthmask & 0xff000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 D_CMD |= XY_COLOR_BLT_WRITE_ALPHA;
911 break;
912 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000913 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 D_CMD = CMD = XY_COLOR_BLT_CMD;
915 break;
916 }
917
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 if (nbox > I830_NR_SAREA_CLIPRECTS)
919 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (pbox->x1 > pbox->x2 ||
923 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 continue;
926
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927 if (flags & I830_FRONT) {
928 DRM_DEBUG("clear front\n");
929 BEGIN_LP_RING(6);
930 OUT_RING(CMD);
931 OUT_RING(BR13);
932 OUT_RING((pbox->y1 << 16) | pbox->x1);
933 OUT_RING((pbox->y2 << 16) | pbox->x2);
934 OUT_RING(dev_priv->front_offset);
935 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ADVANCE_LP_RING();
937 }
938
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 if (flags & I830_BACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 DRM_DEBUG("clear back\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000941 BEGIN_LP_RING(6);
942 OUT_RING(CMD);
943 OUT_RING(BR13);
944 OUT_RING((pbox->y1 << 16) | pbox->x1);
945 OUT_RING((pbox->y2 << 16) | pbox->x2);
946 OUT_RING(dev_priv->back_offset);
947 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ADVANCE_LP_RING();
949 }
950
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 if (flags & I830_DEPTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 DRM_DEBUG("clear depth\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000953 BEGIN_LP_RING(6);
954 OUT_RING(D_CMD);
955 OUT_RING(BR13);
956 OUT_RING((pbox->y1 << 16) | pbox->x1);
957 OUT_RING((pbox->y2 << 16) | pbox->x2);
958 OUT_RING(dev_priv->depth_offset);
959 OUT_RING(clear_zval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 ADVANCE_LP_RING();
961 }
962 }
963}
964
Nicolas Kaiser56499112010-07-15 00:14:43 +0200965static void i830_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 drm_i830_private_t *dev_priv = dev->dev_private;
968 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000970 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int pitch = dev_priv->pitch;
972 int cpp = dev_priv->cpp;
973 int i;
974 unsigned int CMD, BR13;
975 RING_LOCALS;
976
977 DRM_DEBUG("swapbuffers\n");
978
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (dev_priv->do_boxes)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000982 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 switch (cpp) {
985 case 2:
986 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 CMD = XY_SRC_COPY_BLT_CMD;
988 break;
989 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000990 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24) | (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
992 XY_SRC_COPY_BLT_WRITE_RGB);
993 break;
994 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 CMD = XY_SRC_COPY_BLT_CMD;
997 break;
998 }
999
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 if (nbox > I830_NR_SAREA_CLIPRECTS)
1001 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (pbox->x1 > pbox->x2 ||
1005 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001006 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 continue;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 DRM_DEBUG("dispatch swap %d,%d-%d,%d!\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 pbox->x1, pbox->y1, pbox->x2, pbox->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 BEGIN_LP_RING(8);
1013 OUT_RING(CMD);
1014 OUT_RING(BR13);
1015 OUT_RING((pbox->y1 << 16) | pbox->x1);
1016 OUT_RING((pbox->y2 << 16) | pbox->x2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 if (dev_priv->current_page == 0)
1019 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 OUT_RING((pbox->y1 << 16) | pbox->x1);
1024 OUT_RING(BR13 & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 if (dev_priv->current_page == 0)
1027 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 ADVANCE_LP_RING();
1032 }
1033}
1034
Nicolas Kaiser56499112010-07-15 00:14:43 +02001035static void i830_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 RING_LOCALS;
1039
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001041 __func__,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 dev_priv->current_page,
1043 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (dev_priv->do_boxes) {
1048 dev_priv->sarea_priv->perf_boxes |= I830_BOX_FLIP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 BEGIN_LP_RING(2);
1053 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1054 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 ADVANCE_LP_RING();
1056
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 BEGIN_LP_RING(6);
1058 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
1059 OUT_RING(0);
1060 if (dev_priv->current_page == 0) {
1061 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 dev_priv->current_page = 1;
1063 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001064 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 dev_priv->current_page = 0;
1066 }
1067 OUT_RING(0);
1068 ADVANCE_LP_RING();
1069
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 BEGIN_LP_RING(2);
1071 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
1072 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1076}
1077
Nicolas Kaiser56499112010-07-15 00:14:43 +02001078static void i830_dma_dispatch_vertex(struct drm_device *dev,
1079 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +10001084 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 unsigned long address = (unsigned long)buf->bus_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 unsigned long start = address - dev->agp->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int i = 0, u;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 if (nbox > I830_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 nbox = I830_NR_SAREA_CLIPRECTS;
1095
1096 if (discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001097 u = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 I830_BUF_HARDWARE);
Nicolas Kaiser56499112010-07-15 00:14:43 +02001099 if (u != I830_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 DRM_DEBUG("xxxx 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 if (used > 4 * 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 used = 0;
1105
1106 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001107 i830EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 DRM_DEBUG("dispatch vertex addr 0x%lx, used 0x%x nbox %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 address, used, nbox);
1111
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 dev_priv->counter++;
1113 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1114 DRM_DEBUG("i830_dma_dispatch\n");
1115 DRM_DEBUG("start : %lx\n", start);
1116 DRM_DEBUG("used : %d\n", used);
1117 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (buf_priv->currently_mapped == I830_BUF_MAPPED) {
1120 u32 *vp = buf_priv->kernel_virtual;
1121
1122 vp[0] = (GFX_OP_PRIMITIVE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 sarea_priv->vertex_prim | ((used / 4) - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if (dev_priv->use_mi_batchbuffer_start) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001126 vp[used / 4] = MI_BATCH_BUFFER_END;
1127 used += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (used & 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131 vp[used / 4] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 used += 4;
1133 }
1134
1135 i830_unmap_buffer(buf);
1136 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (used) {
1139 do {
1140 if (i < nbox) {
1141 BEGIN_LP_RING(6);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142 OUT_RING(GFX_OP_DRAWRECT_INFO);
1143 OUT_RING(sarea_priv->
1144 BufferState[I830_DESTREG_DR1]);
1145 OUT_RING(box[i].x1 | (box[i].y1 << 16));
1146 OUT_RING(box[i].x2 | (box[i].y2 << 16));
1147 OUT_RING(sarea_priv->
1148 BufferState[I830_DESTREG_DR4]);
1149 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 ADVANCE_LP_RING();
1151 }
1152
1153 if (dev_priv->use_mi_batchbuffer_start) {
1154 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001155 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
1156 OUT_RING(start | MI_BATCH_NON_SECURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001158 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001160 OUT_RING(MI_BATCH_BUFFER);
1161 OUT_RING(start | MI_BATCH_NON_SECURE);
1162 OUT_RING(start + used - 4);
1163 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 ADVANCE_LP_RING();
1165 }
1166
1167 } while (++i < nbox);
1168 }
1169
1170 if (discard) {
1171 dev_priv->counter++;
1172
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001173 (void)cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
1174 I830_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001177 OUT_RING(CMD_STORE_DWORD_IDX);
1178 OUT_RING(20);
1179 OUT_RING(dev_priv->counter);
1180 OUT_RING(CMD_STORE_DWORD_IDX);
1181 OUT_RING(buf_priv->my_use_idx);
1182 OUT_RING(I830_BUF_FREE);
1183 OUT_RING(CMD_REPORT_HEAD);
1184 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ADVANCE_LP_RING();
1186 }
1187}
1188
Nicolas Kaiser56499112010-07-15 00:14:43 +02001189static void i830_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001191 drm_i830_private_t *dev_priv = dev->dev_private;
1192 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001194 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001196 BEGIN_LP_RING(4);
1197 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1198 OUT_RING(CMD_REPORT_HEAD);
1199 OUT_RING(0);
1200 OUT_RING(0);
1201 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001203 i830_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Nicolas Kaiser56499112010-07-15 00:14:43 +02001206static int i830_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001208 drm_i830_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001209 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001210 int i, ret = 0;
1211 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001213 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001215 BEGIN_LP_RING(2);
1216 OUT_RING(CMD_REPORT_HEAD);
1217 OUT_RING(0);
1218 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001220 i830_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001221
1222 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001223 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001224 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1225
1226 int used = cmpxchg(buf_priv->in_use, I830_BUF_HARDWARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 I830_BUF_FREE);
1228
1229 if (used == I830_BUF_HARDWARE)
1230 DRM_DEBUG("reclaimed from HARDWARE\n");
1231 if (used == I830_BUF_CLIENT)
1232 DRM_DEBUG("still on client\n");
1233 }
1234
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238/* Must be called with the lock held */
Nicolas Kaiser56499112010-07-15 00:14:43 +02001239static void i830_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Dave Airliecdd55a22007-07-11 16:32:08 +10001241 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001242 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001244 if (!dma)
1245 return;
1246 if (!dev->dev_private)
1247 return;
1248 if (!dma->buflist)
1249 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001251 i830_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001254 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001255 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1256
Eric Anholt6c340ea2007-08-25 20:23:09 +10001257 if (buf->file_priv == file_priv && buf_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001258 int used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 I830_BUF_FREE);
1260
1261 if (used == I830_BUF_CLIENT)
1262 DRM_DEBUG("reclaimed from client\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001263 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
1264 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 }
1267}
1268
Eric Anholtc153f452007-09-03 12:06:45 +10001269static int i830_flush_ioctl(struct drm_device *dev, void *data,
1270 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001272 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001274 i830_flush_queue(dev);
1275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Eric Anholtc153f452007-09-03 12:06:45 +10001278static int i830_dma_vertex(struct drm_device *dev, void *data,
1279 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Dave Airliecdd55a22007-07-11 16:32:08 +10001281 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001282 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1283 u32 *hw_status = dev_priv->hw_status_page;
1284 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1285 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001286 drm_i830_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Eric Anholt6c340ea2007-08-25 20:23:09 +10001288 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 DRM_DEBUG("i830 dma vertex, idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001291 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Eric Anholtc153f452007-09-03 12:06:45 +10001293 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001294 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001296 i830_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +10001297 dma->buflist[vertex->idx],
1298 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001300 sarea_priv->last_enqueue = dev_priv->counter - 1;
1301 sarea_priv->last_dispatch = (int)hw_status[5];
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return 0;
1304}
1305
Eric Anholtc153f452007-09-03 12:06:45 +10001306static int i830_clear_bufs(struct drm_device *dev, void *data,
1307 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Eric Anholtc153f452007-09-03 12:06:45 +10001309 drm_i830_clear_t *clear = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001310
Eric Anholt6c340ea2007-08-25 20:23:09 +10001311 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /* GH: Someone's doing nasty things... */
Nicolas Kaiser56499112010-07-15 00:14:43 +02001314 if (!dev->dev_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Eric Anholtc153f452007-09-03 12:06:45 +10001317 i830_dma_dispatch_clear(dev, clear->flags,
1318 clear->clear_color,
1319 clear->clear_depth, clear->clear_depthmask);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Eric Anholtc153f452007-09-03 12:06:45 +10001323static int i830_swap_bufs(struct drm_device *dev, void *data,
1324 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 DRM_DEBUG("i830_swap_bufs\n");
1327
Eric Anholt6c340ea2007-08-25 20:23:09 +10001328 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001330 i830_dma_dispatch_swap(dev);
1331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001335 */
Nicolas Kaiser56499112010-07-15 00:14:43 +02001336static void i830_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 drm_i830_private_t *dev_priv = dev->dev_private;
1339
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001340 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 dev_priv->page_flipping = 1;
1342 dev_priv->current_page = 0;
1343 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1344}
1345
Nicolas Kaiser56499112010-07-15 00:14:43 +02001346static int i830_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 drm_i830_private_t *dev_priv = dev->dev_private;
1349
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001350 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001352 i830_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 dev_priv->page_flipping = 0;
1355 return 0;
1356}
1357
Eric Anholtc153f452007-09-03 12:06:45 +10001358static int i830_flip_bufs(struct drm_device *dev, void *data,
1359 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 drm_i830_private_t *dev_priv = dev->dev_private;
1362
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001363 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Eric Anholt6c340ea2007-08-25 20:23:09 +10001365 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001367 if (!dev_priv->page_flipping)
1368 i830_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001370 i830_dma_dispatch_flip(dev);
1371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
Eric Anholtc153f452007-09-03 12:06:45 +10001374static int i830_getage(struct drm_device *dev, void *data,
1375 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001377 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1378 u32 *hw_status = dev_priv->hw_status_page;
1379 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1380 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001382 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return 0;
1384}
1385
Eric Anholtc153f452007-09-03 12:06:45 +10001386static int i830_getbuf(struct drm_device *dev, void *data,
1387 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001389 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001390 drm_i830_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001391 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1392 u32 *hw_status = dev_priv->hw_status_page;
1393 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1394 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 DRM_DEBUG("getbuf\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001397
Eric Anholt6c340ea2007-08-25 20:23:09 +10001398 LOCK_TEST_WITH_RETURN(dev, file_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001399
Eric Anholtc153f452007-09-03 12:06:45 +10001400 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Eric Anholtc153f452007-09-03 12:06:45 +10001402 retcode = i830_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 DRM_DEBUG("i830_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001405 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001407 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 return retcode;
1410}
1411
Eric Anholtc153f452007-09-03 12:06:45 +10001412static int i830_copybuf(struct drm_device *dev, void *data,
1413 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 /* Never copy - 2.4.x doesn't need it */
1416 return 0;
1417}
1418
Eric Anholtc153f452007-09-03 12:06:45 +10001419static int i830_docopy(struct drm_device *dev, void *data,
1420 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 return 0;
1423}
1424
Eric Anholtc153f452007-09-03 12:06:45 +10001425static int i830_getparam(struct drm_device *dev, void *data,
1426 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 drm_i830_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001429 drm_i830_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 int value;
1431
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001432 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001433 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return -EINVAL;
1435 }
1436
Eric Anholtc153f452007-09-03 12:06:45 +10001437 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 case I830_PARAM_IRQ_ACTIVE:
1439 value = dev->irq_enabled;
1440 break;
1441 default:
1442 return -EINVAL;
1443 }
1444
Eric Anholtc153f452007-09-03 12:06:45 +10001445 if (copy_to_user(param->value, &value, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001446 DRM_ERROR("copy_to_user\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -EFAULT;
1448 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 0;
1451}
1452
Eric Anholtc153f452007-09-03 12:06:45 +10001453static int i830_setparam(struct drm_device *dev, void *data,
1454 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 drm_i830_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001457 drm_i830_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001459 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001460 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EINVAL;
1462 }
1463
Eric Anholtc153f452007-09-03 12:06:45 +10001464 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 case I830_SETPARAM_USE_MI_BATCHBUFFER_START:
Eric Anholtc153f452007-09-03 12:06:45 +10001466 dev_priv->use_mi_batchbuffer_start = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 break;
1468 default:
1469 return -EINVAL;
1470 }
1471
1472 return 0;
1473}
1474
Dave Airlieeddca552007-07-11 16:09:54 +10001475int i830_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001476{
1477 /* i830 has 4 more counters */
1478 dev->counters += 4;
1479 dev->types[6] = _DRM_STAT_IRQ;
1480 dev->types[7] = _DRM_STAT_PRIMARY;
1481 dev->types[8] = _DRM_STAT_SECONDARY;
1482 dev->types[9] = _DRM_STAT_DMA;
1483
1484 return 0;
1485}
1486
Nicolas Kaiser56499112010-07-15 00:14:43 +02001487void i830_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001489 i830_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Nicolas Kaiser56499112010-07-15 00:14:43 +02001492void i830_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 if (dev->dev_private) {
1495 drm_i830_private_t *dev_priv = dev->dev_private;
Nicolas Kaiser56499112010-07-15 00:14:43 +02001496 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 i830_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499}
1500
Nicolas Kaiser56499112010-07-15 00:14:43 +02001501void i830_driver_reclaim_buffers_locked(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001503 i830_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Nicolas Kaiser56499112010-07-15 00:14:43 +02001506int i830_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001508 i830_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 0;
1510}
1511
Eric Anholtc153f452007-09-03 12:06:45 +10001512struct drm_ioctl_desc i830_ioctls[] = {
1513 DRM_IOCTL_DEF(DRM_I830_INIT, i830_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1514 DRM_IOCTL_DEF(DRM_I830_VERTEX, i830_dma_vertex, DRM_AUTH),
1515 DRM_IOCTL_DEF(DRM_I830_CLEAR, i830_clear_bufs, DRM_AUTH),
1516 DRM_IOCTL_DEF(DRM_I830_FLUSH, i830_flush_ioctl, DRM_AUTH),
1517 DRM_IOCTL_DEF(DRM_I830_GETAGE, i830_getage, DRM_AUTH),
1518 DRM_IOCTL_DEF(DRM_I830_GETBUF, i830_getbuf, DRM_AUTH),
1519 DRM_IOCTL_DEF(DRM_I830_SWAP, i830_swap_bufs, DRM_AUTH),
1520 DRM_IOCTL_DEF(DRM_I830_COPY, i830_copybuf, DRM_AUTH),
1521 DRM_IOCTL_DEF(DRM_I830_DOCOPY, i830_docopy, DRM_AUTH),
1522 DRM_IOCTL_DEF(DRM_I830_FLIP, i830_flip_bufs, DRM_AUTH),
1523 DRM_IOCTL_DEF(DRM_I830_IRQ_EMIT, i830_irq_emit, DRM_AUTH),
1524 DRM_IOCTL_DEF(DRM_I830_IRQ_WAIT, i830_irq_wait, DRM_AUTH),
1525 DRM_IOCTL_DEF(DRM_I830_GETPARAM, i830_getparam, DRM_AUTH),
1526 DRM_IOCTL_DEF(DRM_I830_SETPARAM, i830_setparam, DRM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527};
1528
1529int i830_max_ioctl = DRM_ARRAY_SIZE(i830_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001530
1531/**
1532 * Determine if the device really is AGP or not.
1533 *
1534 * All Intel graphics chipsets are treated as AGP, even if they are really
1535 * PCI-e.
1536 *
1537 * \param dev The device to be tested.
1538 *
1539 * \returns
1540 * A value of 1 is always retured to indictate every i8xx is AGP.
1541 */
Nicolas Kaiser56499112010-07-15 00:14:43 +02001542int i830_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001543{
1544 return 1;
1545}