blob: 06bd732e6463b23e6ed9968e5500299bd37bcd9e [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>
41#include <asm/uaccess.h>
42
43#define I830_BUF_FREE 2
44#define I830_BUF_CLIENT 1
Dave Airliebc5f4522007-11-05 12:50:58 +100045#define I830_BUF_HARDWARE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define I830_BUF_UNMAPPED 0
48#define I830_BUF_MAPPED 1
49
Dave Airlie056219e2007-07-11 16:17:42 +100050static struct drm_buf *i830_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Dave Airliecdd55a22007-07-11 16:32:08 +100052 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100053 int i;
54 int used;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /* Linear search might not be the best solution */
57
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +100059 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100062 used = cmpxchg(buf_priv->in_use, I830_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 I830_BUF_CLIENT);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100064 if (used == I830_BUF_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return buf;
66 }
67 }
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
Dave Airlie056219e2007-07-11 16:17:42 +100075static 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
Eric Anholt6c340ea2007-08-25 20:23:09 +1000125static 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
Dave Airlie056219e2007-07-11 16:17:42 +1000158static 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
Dave Airlieeddca552007-07-11 16:09:54 +1000178static 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
Dave Airlieeddca552007-07-11 16:09:54 +1000208static 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
224 if (dev_priv->ring.virtual_start) {
Dave Airlieb9094d32007-01-08 21:31:13 +1100225 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227 if (dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 pci_free_consistent(dev->pdev, PAGE_SIZE,
229 dev_priv->hw_status_page,
230 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 /* Need to rewrite hardware status page */
232 I830_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
Eric Anholt9a298b22009-03-24 12:23:04 -0700235 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000236 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000239 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100242 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Dave Airlieeddca552007-07-11 16:09:54 +1000248int i830_wait_ring(struct drm_device * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250 drm_i830_private_t *dev_priv = dev->dev_private;
251 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
252 int iters = 0;
253 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned int last_head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
255
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256 end = jiffies + (HZ * 3);
257 while (ring->space < n) {
258 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
259 ring->space = ring->head - (ring->tail + 8);
260 if (ring->space < 0)
261 ring->space += ring->Size;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (ring->head != last_head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000264 end = jiffies + (HZ * 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 last_head = ring->head;
266 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000267
268 iters++;
269 if (time_before(end, jiffies)) {
270 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
271 DRM_ERROR("lockup\n");
272 goto out_wait_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 udelay(1);
275 dev_priv->sarea_priv->perf_boxes |= I830_BOX_WAIT;
276 }
277
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278 out_wait_ring:
279 return iters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Dave Airlieeddca552007-07-11 16:09:54 +1000282static void i830_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284 drm_i830_private_t *dev_priv = dev->dev_private;
285 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
286
287 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
288 ring->tail = I830_READ(LP_RING + RING_TAIL) & TAIL_ADDR;
289 ring->space = ring->head - (ring->tail + 8);
290 if (ring->space < 0)
291 ring->space += ring->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 if (ring->head == ring->tail)
294 dev_priv->sarea_priv->perf_boxes |= I830_BOX_RING_EMPTY;
295}
296
Dave Airlieeddca552007-07-11 16:09:54 +1000297static int i830_freelist_init(struct drm_device * dev, drm_i830_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Dave Airliecdd55a22007-07-11 16:32:08 +1000299 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300 int my_idx = 36;
301 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
302 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000304 if (dma->buf_count > 1019) {
305 /* Not enough space in the status page for the freelist */
306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000309 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000310 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000311 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 buf_priv->in_use = hw_status++;
314 buf_priv->my_use_idx = my_idx;
315 my_idx += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000317 *buf_priv->in_use = I830_BUF_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Dave Airlieb9094d32007-01-08 21:31:13 +1100319 buf_priv->map.offset = buf->bus_address;
320 buf_priv->map.size = buf->total;
321 buf_priv->map.type = _DRM_AGP;
322 buf_priv->map.flags = 0;
323 buf_priv->map.mtrr = 0;
324
325 drm_core_ioremap(&buf_priv->map, dev);
326 buf_priv->kernel_virtual = buf_priv->map.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 return 0;
329}
330
Dave Airlieeddca552007-07-11 16:09:54 +1000331static int i830_dma_initialize(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 drm_i830_private_t * dev_priv,
333 drm_i830_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Dave Airlie55910512007-07-11 16:53:40 +1000335 struct drm_map_list *r_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 memset(dev_priv, 0, sizeof(drm_i830_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dave Airliebd1b3312007-05-26 05:01:51 +1000339 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000340 if (r_list->map &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 break;
345 }
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000348 if (!dev_priv->sarea_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 dev->dev_private = (void *)dev_priv;
350 i830_dma_cleanup(dev);
351 DRM_ERROR("can not find sarea!\n");
352 return -EINVAL;
353 }
354 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 if (!dev_priv->mmio_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 dev->dev_private = (void *)dev_priv;
357 i830_dma_cleanup(dev);
358 DRM_ERROR("can not find mmio map!\n");
359 return -EINVAL;
360 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000361 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000363 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 dev->dev_private = (void *)dev_priv;
365 i830_dma_cleanup(dev);
366 DRM_ERROR("can not find dma buffer map!\n");
367 return -EINVAL;
368 }
369
370 dev_priv->sarea_priv = (drm_i830_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 dev_priv->ring.Start = init->ring_start;
374 dev_priv->ring.End = init->ring_end;
375 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Dave Airlieb9094d32007-01-08 21:31:13 +1100377 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
378 dev_priv->ring.map.size = init->ring_size;
379 dev_priv->ring.map.type = _DRM_AGP;
380 dev_priv->ring.map.flags = 0;
381 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dave Airlieb9094d32007-01-08 21:31:13 +1100383 drm_core_ioremap(&dev_priv->ring.map, dev);
384
385 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000386 dev->dev_private = (void *)dev_priv;
387 i830_dma_cleanup(dev);
388 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000390 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
Dave Airlieb9094d32007-01-08 21:31:13 +1100393 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
394
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 dev_priv->w = init->w;
398 dev_priv->h = init->h;
399 dev_priv->pitch = init->pitch;
400 dev_priv->back_offset = init->back_offset;
401 dev_priv->depth_offset = init->depth_offset;
402 dev_priv->front_offset = init->front_offset;
403
404 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
405 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
406 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
407
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000408 DRM_DEBUG("front_di1 %x\n", dev_priv->front_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 DRM_DEBUG("back_offset %x\n", dev_priv->back_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 DRM_DEBUG("back_di1 %x\n", dev_priv->back_di1);
411 DRM_DEBUG("pitch_bits %x\n", init->pitch_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 dev_priv->cpp = init->cpp;
414 /* We are using separate values as placeholders for mechanisms for
415 * private backbuffer/depthbuffer usage.
416 */
417
418 dev_priv->back_pitch = init->back_pitch;
419 dev_priv->depth_pitch = init->depth_pitch;
420 dev_priv->do_boxes = 0;
421 dev_priv->use_mi_batchbuffer_start = 0;
422
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000423 /* Program Hardware Status Page */
424 dev_priv->hw_status_page =
425 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
426 &dev_priv->dma_status_page);
427 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 dev->dev_private = (void *)dev_priv;
429 i830_dma_cleanup(dev);
430 DRM_ERROR("Can not allocate hardware status page\n");
431 return -ENOMEM;
432 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000433 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435
436 I830_WRITE(0x02080, dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 DRM_DEBUG("Enabled hardware status page\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438
439 /* Now we need to init our freelist */
440 if (i830_freelist_init(dev, dev_priv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000442 i830_dma_cleanup(dev);
443 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000445 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 dev->dev_private = (void *)dev_priv;
448
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Eric Anholtc153f452007-09-03 12:06:45 +1000452static int i830_dma_init(struct drm_device *dev, void *data,
453 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000455 drm_i830_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000456 drm_i830_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000457 int retcode = 0;
458
Eric Anholtc153f452007-09-03 12:06:45 +1000459 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000460 case I830_INIT_DMA:
Eric Anholt9a298b22009-03-24 12:23:04 -0700461 dev_priv = kmalloc(sizeof(drm_i830_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000462 if (dev_priv == NULL)
463 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000464 retcode = i830_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000465 break;
466 case I830_CLEANUP_DMA:
467 retcode = i830_dma_cleanup(dev);
468 break;
469 default:
470 retcode = -EINVAL;
471 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473
474 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477#define GFX_OP_STIPPLE ((0x3<<29)|(0x1d<<24)|(0x83<<16))
478#define ST1_ENABLE (1<<16)
479#define ST1_MASK (0xffff)
480
481/* Most efficient way to verify state for the i830 is as it is
482 * emitted. Non-conformant state is silently dropped.
483 */
Dave Airlieeddca552007-07-11 16:09:54 +1000484static void i830EmitContextVerified(struct drm_device * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000486 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int i, j = 0;
488 unsigned int tmp;
489 RING_LOCALS;
490
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000491 BEGIN_LP_RING(I830_CTX_SETUP_SIZE + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493 for (i = 0; i < I830_CTXREG_BLENDCOLR0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000495 if ((tmp & (7 << 29)) == CMD_3D &&
496 (tmp & (0x1f << 24)) < (0x1d << 24)) {
497 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 j++;
499 } else {
500 DRM_ERROR("Skipping %d\n", i);
501 }
502 }
503
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504 OUT_RING(STATE3D_CONST_BLEND_COLOR_CMD);
505 OUT_RING(code[I830_CTXREG_BLENDCOLR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 j += 2;
507
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000508 for (i = I830_CTXREG_VF; i < I830_CTXREG_MCSB0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000510 if ((tmp & (7 << 29)) == CMD_3D &&
511 (tmp & (0x1f << 24)) < (0x1d << 24)) {
512 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 j++;
514 } else {
515 DRM_ERROR("Skipping %d\n", i);
516 }
517 }
518
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 OUT_RING(STATE3D_MAP_COORD_SETBIND_CMD);
520 OUT_RING(code[I830_CTXREG_MCSB1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 j += 2;
522
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000523 if (j & 1)
524 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 ADVANCE_LP_RING();
527}
528
Dave Airlieeddca552007-07-11 16:09:54 +1000529static void i830EmitTexVerified(struct drm_device * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int i, j = 0;
533 unsigned int tmp;
534 RING_LOCALS;
535
536 if (code[I830_TEXREG_MI0] == GFX_OP_MAP_INFO ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 (code[I830_TEXREG_MI0] & ~(0xf * LOAD_TEXTURE_MAP0)) ==
538 (STATE3D_LOAD_STATE_IMMEDIATE_2 | 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 BEGIN_LP_RING(I830_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000542 OUT_RING(code[I830_TEXREG_MI0]); /* TM0LI */
543 OUT_RING(code[I830_TEXREG_MI1]); /* TM0S0 */
544 OUT_RING(code[I830_TEXREG_MI2]); /* TM0S1 */
545 OUT_RING(code[I830_TEXREG_MI3]); /* TM0S2 */
546 OUT_RING(code[I830_TEXREG_MI4]); /* TM0S3 */
547 OUT_RING(code[I830_TEXREG_MI5]); /* TM0S4 */
548
549 for (i = 6; i < I830_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000555 if (j & 1)
556 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000559 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 printk("rejected packet %x\n", code[0]);
561}
562
Dave Airlieeddca552007-07-11 16:09:54 +1000563static void i830EmitTexBlendVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000564 unsigned int *code, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000566 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int i, j = 0;
568 unsigned int tmp;
569 RING_LOCALS;
570
571 if (!num)
572 return;
573
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574 BEGIN_LP_RING(num + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 j++;
580 }
581
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 if (j & 1)
583 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 ADVANCE_LP_RING();
586}
587
Dave Airlieeddca552007-07-11 16:09:54 +1000588static void i830EmitTexPalette(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 unsigned int *palette, int number, int is_shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int i;
593 RING_LOCALS;
594
595 return;
596
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 BEGIN_LP_RING(258);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000599 if (is_shared == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 OUT_RING(CMD_OP_MAP_PALETTE_LOAD |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 MAP_PALETTE_NUM(0) | MAP_PALETTE_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 } else {
603 OUT_RING(CMD_OP_MAP_PALETTE_LOAD | MAP_PALETTE_NUM(number));
604 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000605 for (i = 0; i < 256; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 OUT_RING(palette[i]);
607 }
608 OUT_RING(0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609 /* KW: WHERE IS THE ADVANCE_LP_RING? This is effectively a noop!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
611}
612
613/* Need to do some additional checking when setting the dest buffer.
614 */
Dave Airlieeddca552007-07-11 16:09:54 +1000615static void i830EmitDestVerified(struct drm_device * dev, unsigned int *code)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000616{
617 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 unsigned int tmp;
619 RING_LOCALS;
620
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 BEGIN_LP_RING(I830_DEST_SETUP_SIZE + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 tmp = code[I830_DESTREG_CBUFADDR];
624 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
625 if (((int)outring) & 8) {
626 OUT_RING(0);
627 OUT_RING(0);
628 }
629
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000630 OUT_RING(CMD_OP_DESTBUFFER_INFO);
631 OUT_RING(BUF_3D_ID_COLOR_BACK |
632 BUF_3D_PITCH(dev_priv->back_pitch * dev_priv->cpp) |
633 BUF_3D_USE_FENCE);
634 OUT_RING(tmp);
635 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 OUT_RING(CMD_OP_DESTBUFFER_INFO);
638 OUT_RING(BUF_3D_ID_DEPTH | BUF_3D_USE_FENCE |
639 BUF_3D_PITCH(dev_priv->depth_pitch * dev_priv->cpp));
640 OUT_RING(dev_priv->zi1);
641 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 } else {
643 DRM_ERROR("bad di1 %x (allow %x or %x)\n",
644 tmp, dev_priv->front_di1, dev_priv->back_di1);
645 }
646
647 /* invarient:
648 */
649
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 OUT_RING(GFX_OP_DESTBUFFER_VARS);
651 OUT_RING(code[I830_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 OUT_RING(GFX_OP_DRAWRECT_INFO);
654 OUT_RING(code[I830_DESTREG_DR1]);
655 OUT_RING(code[I830_DESTREG_DR2]);
656 OUT_RING(code[I830_DESTREG_DR3]);
657 OUT_RING(code[I830_DESTREG_DR4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* Need to verify this */
660 tmp = code[I830_DESTREG_SENABLE];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 if ((tmp & ~0x3) == GFX_OP_SCISSOR_ENABLE) {
662 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 } else {
664 DRM_ERROR("bad scissor enable\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000668 OUT_RING(GFX_OP_SCISSOR_RECT);
669 OUT_RING(code[I830_DESTREG_SR1]);
670 OUT_RING(code[I830_DESTREG_SR2]);
671 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 ADVANCE_LP_RING();
674}
675
Dave Airlieeddca552007-07-11 16:09:54 +1000676static void i830EmitStippleVerified(struct drm_device * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 RING_LOCALS;
680
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000681 BEGIN_LP_RING(2);
682 OUT_RING(GFX_OP_STIPPLE);
683 OUT_RING(code[1]);
684 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Dave Airlieeddca552007-07-11 16:09:54 +1000687static void i830EmitState(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 drm_i830_private_t *dev_priv = dev->dev_private;
690 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned int dirty = sarea_priv->dirty;
692
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700693 DRM_DEBUG("%s %x\n", __func__, dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (dirty & I830_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 i830EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 sarea_priv->dirty &= ~I830_UPLOAD_BUFFERS;
698 }
699
700 if (dirty & I830_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 i830EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 sarea_priv->dirty &= ~I830_UPLOAD_CTX;
703 }
704
705 if (dirty & I830_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 i830EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 sarea_priv->dirty &= ~I830_UPLOAD_TEX0;
708 }
709
710 if (dirty & I830_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 i830EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 sarea_priv->dirty &= ~I830_UPLOAD_TEX1;
713 }
714
715 if (dirty & I830_UPLOAD_TEXBLEND0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[0],
717 sarea_priv->TexBlendStateWordsUsed[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND0;
719 }
720
721 if (dirty & I830_UPLOAD_TEXBLEND1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[1],
723 sarea_priv->TexBlendStateWordsUsed[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND1;
725 }
726
727 if (dirty & I830_UPLOAD_TEX_PALETTE_SHARED) {
728 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 1);
729 } else {
730 if (dirty & I830_UPLOAD_TEX_PALETTE_N(0)) {
731 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 0);
732 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(0);
733 }
734 if (dirty & I830_UPLOAD_TEX_PALETTE_N(1)) {
735 i830EmitTexPalette(dev, sarea_priv->Palette[1], 1, 0);
736 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(1);
737 }
738
739 /* 1.3:
740 */
741#if 0
742 if (dirty & I830_UPLOAD_TEX_PALETTE_N(2)) {
743 i830EmitTexPalette(dev, sarea_priv->Palette2[0], 0, 0);
744 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
745 }
746 if (dirty & I830_UPLOAD_TEX_PALETTE_N(3)) {
747 i830EmitTexPalette(dev, sarea_priv->Palette2[1], 1, 0);
748 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
749 }
750#endif
751 }
752
753 /* 1.3:
754 */
755 if (dirty & I830_UPLOAD_STIPPLE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 i830EmitStippleVerified(dev, sarea_priv->StippleState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 sarea_priv->dirty &= ~I830_UPLOAD_STIPPLE;
758 }
759
760 if (dirty & I830_UPLOAD_TEX2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 i830EmitTexVerified(dev, sarea_priv->TexState2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 sarea_priv->dirty &= ~I830_UPLOAD_TEX2;
763 }
764
765 if (dirty & I830_UPLOAD_TEX3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000766 i830EmitTexVerified(dev, sarea_priv->TexState3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 sarea_priv->dirty &= ~I830_UPLOAD_TEX3;
768 }
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (dirty & I830_UPLOAD_TEXBLEND2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000771 i830EmitTexBlendVerified(dev,
772 sarea_priv->TexBlendState2,
773 sarea_priv->TexBlendStateWordsUsed2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND2;
776 }
777
778 if (dirty & I830_UPLOAD_TEXBLEND3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 i830EmitTexBlendVerified(dev,
780 sarea_priv->TexBlendState3,
781 sarea_priv->TexBlendStateWordsUsed3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND3;
783 }
784}
785
786/* ================================================================
787 * Performance monitoring functions
788 */
789
Dave Airlieeddca552007-07-11 16:09:54 +1000790static void i830_fill_box(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 u32 color;
795 unsigned int BR13, CMD;
796 RING_LOCALS;
797
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 BR13 = (0xF0 << 16) | (dev_priv->pitch * dev_priv->cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 CMD = XY_COLOR_BLT_CMD;
800 x += dev_priv->sarea_priv->boxes[0].x1;
801 y += dev_priv->sarea_priv->boxes[0].y1;
802
803 if (dev_priv->cpp == 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 BR13 |= (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 CMD |= (XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 } else {
808 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 BEGIN_LP_RING(6);
813 OUT_RING(CMD);
814 OUT_RING(BR13);
815 OUT_RING((y << 16) | x);
816 OUT_RING(((y + h) << 16) | (x + w));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 if (dev_priv->current_page == 1) {
819 OUT_RING(dev_priv->front_offset);
820 } else {
821 OUT_RING(dev_priv->back_offset);
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 ADVANCE_LP_RING();
826}
827
Dave Airlieeddca552007-07-11 16:09:54 +1000828static void i830_cp_performance_boxes(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /* Purple box for page flipping
833 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000834 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_FLIP)
835 i830_fill_box(dev, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /* Red box if we have to wait for idle at any point
838 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_WAIT)
840 i830_fill_box(dev, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* Blue box: lost context?
843 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_LOST_CONTEXT)
845 i830_fill_box(dev, 28, 4, 8, 8, 0, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /* Yellow box for texture swaps
848 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_TEXTURE_LOAD)
850 i830_fill_box(dev, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* Green box if hardware never idles (as far as we can tell)
853 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 if (!(dev_priv->sarea_priv->perf_boxes & I830_BOX_RING_EMPTY))
855 i830_fill_box(dev, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * (not a great measure, easily confused)
859 */
860 if (dev_priv->dma_used) {
861 int bar = dev_priv->dma_used / 10240;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 if (bar > 100)
863 bar = 100;
864 if (bar < 1)
865 bar = 1;
866 i830_fill_box(dev, 4, 16, bar, 4, 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 dev_priv->dma_used = 0;
868 }
869
870 dev_priv->sarea_priv->perf_boxes = 0;
871}
872
Dave Airlieeddca552007-07-11 16:09:54 +1000873static void i830_dma_dispatch_clear(struct drm_device * dev, int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned int clear_color,
875 unsigned int clear_zval,
876 unsigned int clear_depthmask)
877{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 drm_i830_private_t *dev_priv = dev->dev_private;
879 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000881 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 int pitch = dev_priv->pitch;
883 int cpp = dev_priv->cpp;
884 int i;
885 unsigned int BR13, CMD, D_CMD;
886 RING_LOCALS;
887
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 if (dev_priv->current_page == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 unsigned int tmp = flags;
890
891 flags &= ~(I830_FRONT | I830_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 if (tmp & I830_FRONT)
893 flags |= I830_BACK;
894 if (tmp & I830_BACK)
895 flags |= I830_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 switch (cpp) {
901 case 2:
902 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 D_CMD = CMD = XY_COLOR_BLT_CMD;
904 break;
905 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24) | (1 << 25);
907 CMD = (XY_COLOR_BLT_CMD | XY_COLOR_BLT_WRITE_ALPHA |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 XY_COLOR_BLT_WRITE_RGB);
909 D_CMD = XY_COLOR_BLT_CMD;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 if (clear_depthmask & 0x00ffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 D_CMD |= XY_COLOR_BLT_WRITE_RGB;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 if (clear_depthmask & 0xff000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 D_CMD |= XY_COLOR_BLT_WRITE_ALPHA;
914 break;
915 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 D_CMD = CMD = XY_COLOR_BLT_CMD;
918 break;
919 }
920
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 if (nbox > I830_NR_SAREA_CLIPRECTS)
922 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (pbox->x1 > pbox->x2 ||
926 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 continue;
929
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 if (flags & I830_FRONT) {
931 DRM_DEBUG("clear front\n");
932 BEGIN_LP_RING(6);
933 OUT_RING(CMD);
934 OUT_RING(BR13);
935 OUT_RING((pbox->y1 << 16) | pbox->x1);
936 OUT_RING((pbox->y2 << 16) | pbox->x2);
937 OUT_RING(dev_priv->front_offset);
938 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 ADVANCE_LP_RING();
940 }
941
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 if (flags & I830_BACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 DRM_DEBUG("clear back\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000944 BEGIN_LP_RING(6);
945 OUT_RING(CMD);
946 OUT_RING(BR13);
947 OUT_RING((pbox->y1 << 16) | pbox->x1);
948 OUT_RING((pbox->y2 << 16) | pbox->x2);
949 OUT_RING(dev_priv->back_offset);
950 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 ADVANCE_LP_RING();
952 }
953
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000954 if (flags & I830_DEPTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 DRM_DEBUG("clear depth\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000956 BEGIN_LP_RING(6);
957 OUT_RING(D_CMD);
958 OUT_RING(BR13);
959 OUT_RING((pbox->y1 << 16) | pbox->x1);
960 OUT_RING((pbox->y2 << 16) | pbox->x2);
961 OUT_RING(dev_priv->depth_offset);
962 OUT_RING(clear_zval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 ADVANCE_LP_RING();
964 }
965 }
966}
967
Dave Airlieeddca552007-07-11 16:09:54 +1000968static void i830_dma_dispatch_swap(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000970 drm_i830_private_t *dev_priv = dev->dev_private;
971 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000973 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int pitch = dev_priv->pitch;
975 int cpp = dev_priv->cpp;
976 int i;
977 unsigned int CMD, BR13;
978 RING_LOCALS;
979
980 DRM_DEBUG("swapbuffers\n");
981
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000982 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 if (dev_priv->do_boxes)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000985 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 switch (cpp) {
988 case 2:
989 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 CMD = XY_SRC_COPY_BLT_CMD;
991 break;
992 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000993 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24) | (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
995 XY_SRC_COPY_BLT_WRITE_RGB);
996 break;
997 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000998 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 CMD = XY_SRC_COPY_BLT_CMD;
1000 break;
1001 }
1002
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 if (nbox > I830_NR_SAREA_CLIPRECTS)
1004 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001006 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (pbox->x1 > pbox->x2 ||
1008 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 continue;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 DRM_DEBUG("dispatch swap %d,%d-%d,%d!\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001013 pbox->x1, pbox->y1, pbox->x2, pbox->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001015 BEGIN_LP_RING(8);
1016 OUT_RING(CMD);
1017 OUT_RING(BR13);
1018 OUT_RING((pbox->y1 << 16) | pbox->x1);
1019 OUT_RING((pbox->y2 << 16) | pbox->x2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 if (dev_priv->current_page == 0)
1022 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 OUT_RING((pbox->y1 << 16) | pbox->x1);
1027 OUT_RING(BR13 & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 if (dev_priv->current_page == 0)
1030 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 ADVANCE_LP_RING();
1035 }
1036}
1037
Dave Airlieeddca552007-07-11 16:09:54 +10001038static void i830_dma_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 RING_LOCALS;
1042
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001044 __func__,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 dev_priv->current_page,
1046 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (dev_priv->do_boxes) {
1051 dev_priv->sarea_priv->perf_boxes |= I830_BOX_FLIP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 BEGIN_LP_RING(2);
1056 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1057 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ADVANCE_LP_RING();
1059
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 BEGIN_LP_RING(6);
1061 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
1062 OUT_RING(0);
1063 if (dev_priv->current_page == 0) {
1064 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 dev_priv->current_page = 1;
1066 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001067 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 dev_priv->current_page = 0;
1069 }
1070 OUT_RING(0);
1071 ADVANCE_LP_RING();
1072
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 BEGIN_LP_RING(2);
1074 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
1075 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1079}
1080
Dave Airlieeddca552007-07-11 16:09:54 +10001081static void i830_dma_dispatch_vertex(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +10001082 struct drm_buf * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +10001087 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001088 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 unsigned long address = (unsigned long)buf->bus_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090 unsigned long start = address - dev->agp->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 int i = 0, u;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 if (nbox > I830_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 nbox = I830_NR_SAREA_CLIPRECTS;
1098
1099 if (discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001100 u = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 I830_BUF_HARDWARE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 if (u != I830_BUF_CLIENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 DRM_DEBUG("xxxx 2\n");
1104 }
1105 }
1106
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001107 if (used > 4 * 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 used = 0;
1109
1110 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001111 i830EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001113 DRM_DEBUG("dispatch vertex addr 0x%lx, used 0x%x nbox %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 address, used, nbox);
1115
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 dev_priv->counter++;
1117 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1118 DRM_DEBUG("i830_dma_dispatch\n");
1119 DRM_DEBUG("start : %lx\n", start);
1120 DRM_DEBUG("used : %d\n", used);
1121 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 if (buf_priv->currently_mapped == I830_BUF_MAPPED) {
1124 u32 *vp = buf_priv->kernel_virtual;
1125
1126 vp[0] = (GFX_OP_PRIMITIVE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127 sarea_priv->vertex_prim | ((used / 4) - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (dev_priv->use_mi_batchbuffer_start) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130 vp[used / 4] = MI_BATCH_BUFFER_END;
1131 used += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (used & 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001135 vp[used / 4] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 used += 4;
1137 }
1138
1139 i830_unmap_buffer(buf);
1140 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (used) {
1143 do {
1144 if (i < nbox) {
1145 BEGIN_LP_RING(6);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001146 OUT_RING(GFX_OP_DRAWRECT_INFO);
1147 OUT_RING(sarea_priv->
1148 BufferState[I830_DESTREG_DR1]);
1149 OUT_RING(box[i].x1 | (box[i].y1 << 16));
1150 OUT_RING(box[i].x2 | (box[i].y2 << 16));
1151 OUT_RING(sarea_priv->
1152 BufferState[I830_DESTREG_DR4]);
1153 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ADVANCE_LP_RING();
1155 }
1156
1157 if (dev_priv->use_mi_batchbuffer_start) {
1158 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001159 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
1160 OUT_RING(start | MI_BATCH_NON_SECURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001162 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001164 OUT_RING(MI_BATCH_BUFFER);
1165 OUT_RING(start | MI_BATCH_NON_SECURE);
1166 OUT_RING(start + used - 4);
1167 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ADVANCE_LP_RING();
1169 }
1170
1171 } while (++i < nbox);
1172 }
1173
1174 if (discard) {
1175 dev_priv->counter++;
1176
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001177 (void)cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
1178 I830_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001181 OUT_RING(CMD_STORE_DWORD_IDX);
1182 OUT_RING(20);
1183 OUT_RING(dev_priv->counter);
1184 OUT_RING(CMD_STORE_DWORD_IDX);
1185 OUT_RING(buf_priv->my_use_idx);
1186 OUT_RING(I830_BUF_FREE);
1187 OUT_RING(CMD_REPORT_HEAD);
1188 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 ADVANCE_LP_RING();
1190 }
1191}
1192
Dave Airlieeddca552007-07-11 16:09:54 +10001193static void i830_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001195 drm_i830_private_t *dev_priv = dev->dev_private;
1196 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001198 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001200 BEGIN_LP_RING(4);
1201 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1202 OUT_RING(CMD_REPORT_HEAD);
1203 OUT_RING(0);
1204 OUT_RING(0);
1205 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001207 i830_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Dave Airlieeddca552007-07-11 16:09:54 +10001210static int i830_flush_queue(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001212 drm_i830_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001213 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001214 int i, ret = 0;
1215 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001217 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001219 BEGIN_LP_RING(2);
1220 OUT_RING(CMD_REPORT_HEAD);
1221 OUT_RING(0);
1222 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001224 i830_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001225
1226 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001227 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001228 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1229
1230 int used = cmpxchg(buf_priv->in_use, I830_BUF_HARDWARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 I830_BUF_FREE);
1232
1233 if (used == I830_BUF_HARDWARE)
1234 DRM_DEBUG("reclaimed from HARDWARE\n");
1235 if (used == I830_BUF_CLIENT)
1236 DRM_DEBUG("still on client\n");
1237 }
1238
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/* Must be called with the lock held */
Eric Anholt6c340ea2007-08-25 20:23:09 +10001243static void i830_reclaim_buffers(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Dave Airliecdd55a22007-07-11 16:32:08 +10001245 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001246 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001248 if (!dma)
1249 return;
1250 if (!dev->dev_private)
1251 return;
1252 if (!dma->buflist)
1253 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001255 i830_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001258 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001259 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1260
Eric Anholt6c340ea2007-08-25 20:23:09 +10001261 if (buf->file_priv == file_priv && buf_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001262 int used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 I830_BUF_FREE);
1264
1265 if (used == I830_BUF_CLIENT)
1266 DRM_DEBUG("reclaimed from client\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001267 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
1268 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 }
1271}
1272
Eric Anholtc153f452007-09-03 12:06:45 +10001273static int i830_flush_ioctl(struct drm_device *dev, void *data,
1274 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001276 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001278 i830_flush_queue(dev);
1279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Eric Anholtc153f452007-09-03 12:06:45 +10001282static int i830_dma_vertex(struct drm_device *dev, void *data,
1283 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Dave Airliecdd55a22007-07-11 16:32:08 +10001285 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001286 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1287 u32 *hw_status = dev_priv->hw_status_page;
1288 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1289 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001290 drm_i830_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Eric Anholt6c340ea2007-08-25 20:23:09 +10001292 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 DRM_DEBUG("i830 dma vertex, idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001295 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Eric Anholtc153f452007-09-03 12:06:45 +10001297 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001298 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001300 i830_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +10001301 dma->buflist[vertex->idx],
1302 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001304 sarea_priv->last_enqueue = dev_priv->counter - 1;
1305 sarea_priv->last_dispatch = (int)hw_status[5];
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
1308}
1309
Eric Anholtc153f452007-09-03 12:06:45 +10001310static int i830_clear_bufs(struct drm_device *dev, void *data,
1311 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Eric Anholtc153f452007-09-03 12:06:45 +10001313 drm_i830_clear_t *clear = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001314
Eric Anholt6c340ea2007-08-25 20:23:09 +10001315 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* GH: Someone's doing nasty things... */
1318 if (!dev->dev_private) {
1319 return -EINVAL;
1320 }
1321
Eric Anholtc153f452007-09-03 12:06:45 +10001322 i830_dma_dispatch_clear(dev, clear->flags,
1323 clear->clear_color,
1324 clear->clear_depth, clear->clear_depthmask);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Eric Anholtc153f452007-09-03 12:06:45 +10001328static int i830_swap_bufs(struct drm_device *dev, void *data,
1329 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 DRM_DEBUG("i830_swap_bufs\n");
1332
Eric Anholt6c340ea2007-08-25 20:23:09 +10001333 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001335 i830_dma_dispatch_swap(dev);
1336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001340 */
Dave Airlieeddca552007-07-11 16:09:54 +10001341static void i830_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 drm_i830_private_t *dev_priv = dev->dev_private;
1344
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001345 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 dev_priv->page_flipping = 1;
1347 dev_priv->current_page = 0;
1348 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1349}
1350
Dave Airlieeddca552007-07-11 16:09:54 +10001351static int i830_do_cleanup_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 drm_i830_private_t *dev_priv = dev->dev_private;
1354
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001355 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001357 i830_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 dev_priv->page_flipping = 0;
1360 return 0;
1361}
1362
Eric Anholtc153f452007-09-03 12:06:45 +10001363static int i830_flip_bufs(struct drm_device *dev, void *data,
1364 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 drm_i830_private_t *dev_priv = dev->dev_private;
1367
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001368 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Eric Anholt6c340ea2007-08-25 20:23:09 +10001370 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001372 if (!dev_priv->page_flipping)
1373 i830_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001375 i830_dma_dispatch_flip(dev);
1376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Eric Anholtc153f452007-09-03 12:06:45 +10001379static int i830_getage(struct drm_device *dev, void *data,
1380 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001382 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1383 u32 *hw_status = dev_priv->hw_status_page;
1384 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1385 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001387 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return 0;
1389}
1390
Eric Anholtc153f452007-09-03 12:06:45 +10001391static int i830_getbuf(struct drm_device *dev, void *data,
1392 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001394 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001395 drm_i830_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1397 u32 *hw_status = dev_priv->hw_status_page;
1398 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1399 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 DRM_DEBUG("getbuf\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001402
Eric Anholt6c340ea2007-08-25 20:23:09 +10001403 LOCK_TEST_WITH_RETURN(dev, file_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001404
Eric Anholtc153f452007-09-03 12:06:45 +10001405 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Eric Anholtc153f452007-09-03 12:06:45 +10001407 retcode = i830_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 DRM_DEBUG("i830_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001410 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001412 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 return retcode;
1415}
1416
Eric Anholtc153f452007-09-03 12:06:45 +10001417static int i830_copybuf(struct drm_device *dev, void *data,
1418 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 /* Never copy - 2.4.x doesn't need it */
1421 return 0;
1422}
1423
Eric Anholtc153f452007-09-03 12:06:45 +10001424static int i830_docopy(struct drm_device *dev, void *data,
1425 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 return 0;
1428}
1429
Eric Anholtc153f452007-09-03 12:06:45 +10001430static int i830_getparam(struct drm_device *dev, void *data,
1431 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 drm_i830_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001434 drm_i830_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int value;
1436
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001437 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001438 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EINVAL;
1440 }
1441
Eric Anholtc153f452007-09-03 12:06:45 +10001442 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 case I830_PARAM_IRQ_ACTIVE:
1444 value = dev->irq_enabled;
1445 break;
1446 default:
1447 return -EINVAL;
1448 }
1449
Eric Anholtc153f452007-09-03 12:06:45 +10001450 if (copy_to_user(param->value, &value, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001451 DRM_ERROR("copy_to_user\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -EFAULT;
1453 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456}
1457
Eric Anholtc153f452007-09-03 12:06:45 +10001458static int i830_setparam(struct drm_device *dev, void *data,
1459 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 drm_i830_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001462 drm_i830_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001464 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001465 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return -EINVAL;
1467 }
1468
Eric Anholtc153f452007-09-03 12:06:45 +10001469 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 case I830_SETPARAM_USE_MI_BATCHBUFFER_START:
Eric Anholtc153f452007-09-03 12:06:45 +10001471 dev_priv->use_mi_batchbuffer_start = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 default:
1474 return -EINVAL;
1475 }
1476
1477 return 0;
1478}
1479
Dave Airlieeddca552007-07-11 16:09:54 +10001480int i830_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001481{
1482 /* i830 has 4 more counters */
1483 dev->counters += 4;
1484 dev->types[6] = _DRM_STAT_IRQ;
1485 dev->types[7] = _DRM_STAT_PRIMARY;
1486 dev->types[8] = _DRM_STAT_SECONDARY;
1487 dev->types[9] = _DRM_STAT_DMA;
1488
1489 return 0;
1490}
1491
Dave Airlieeddca552007-07-11 16:09:54 +10001492void i830_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 i830_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Eric Anholt6c340ea2007-08-25 20:23:09 +10001497void i830_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 if (dev->dev_private) {
1500 drm_i830_private_t *dev_priv = dev->dev_private;
1501 if (dev_priv->page_flipping) {
1502 i830_do_cleanup_pageflip(dev);
1503 }
1504 }
1505}
1506
Eric Anholt6c340ea2007-08-25 20:23:09 +10001507void i830_driver_reclaim_buffers_locked(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001509 i830_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Dave Airlieeddca552007-07-11 16:09:54 +10001512int i830_driver_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001514 i830_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return 0;
1516}
1517
Eric Anholtc153f452007-09-03 12:06:45 +10001518struct drm_ioctl_desc i830_ioctls[] = {
1519 DRM_IOCTL_DEF(DRM_I830_INIT, i830_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1520 DRM_IOCTL_DEF(DRM_I830_VERTEX, i830_dma_vertex, DRM_AUTH),
1521 DRM_IOCTL_DEF(DRM_I830_CLEAR, i830_clear_bufs, DRM_AUTH),
1522 DRM_IOCTL_DEF(DRM_I830_FLUSH, i830_flush_ioctl, DRM_AUTH),
1523 DRM_IOCTL_DEF(DRM_I830_GETAGE, i830_getage, DRM_AUTH),
1524 DRM_IOCTL_DEF(DRM_I830_GETBUF, i830_getbuf, DRM_AUTH),
1525 DRM_IOCTL_DEF(DRM_I830_SWAP, i830_swap_bufs, DRM_AUTH),
1526 DRM_IOCTL_DEF(DRM_I830_COPY, i830_copybuf, DRM_AUTH),
1527 DRM_IOCTL_DEF(DRM_I830_DOCOPY, i830_docopy, DRM_AUTH),
1528 DRM_IOCTL_DEF(DRM_I830_FLIP, i830_flip_bufs, DRM_AUTH),
1529 DRM_IOCTL_DEF(DRM_I830_IRQ_EMIT, i830_irq_emit, DRM_AUTH),
1530 DRM_IOCTL_DEF(DRM_I830_IRQ_WAIT, i830_irq_wait, DRM_AUTH),
1531 DRM_IOCTL_DEF(DRM_I830_GETPARAM, i830_getparam, DRM_AUTH),
1532 DRM_IOCTL_DEF(DRM_I830_SETPARAM, i830_setparam, DRM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533};
1534
1535int i830_max_ioctl = DRM_ARRAY_SIZE(i830_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001536
1537/**
1538 * Determine if the device really is AGP or not.
1539 *
1540 * All Intel graphics chipsets are treated as AGP, even if they are really
1541 * PCI-e.
1542 *
1543 * \param dev The device to be tested.
1544 *
1545 * \returns
1546 * A value of 1 is always retured to indictate every i8xx is AGP.
1547 */
Dave Airlieeddca552007-07-11 16:09:54 +10001548int i830_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001549{
1550 return 1;
1551}