blob: 65759a9a85c8e425714c3281f080880eaaf8d0fc [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);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 if (used == I830_BUF_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return buf;
67 }
68 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100069 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72/* This should only be called if the buffer is not sent to the hardware
73 * yet, the hardware updates in use for us once its on the ring buffer.
74 */
75
Dave Airlie056219e2007-07-11 16:17:42 +100076static int i830_freelist_put(struct drm_device * dev, struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
79 int used;
80
81 /* In use is already a pointer */
82 used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT, I830_BUF_FREE);
83 if (used != I830_BUF_CLIENT) {
84 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
85 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100087
88 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Dave Airliec94f7022005-07-07 21:03:38 +100091static int i830_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Dave Airlieeddca552007-07-11 16:09:54 +100093 struct drm_file *priv = filp->private_data;
94 struct drm_device *dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095 drm_i830_private_t *dev_priv;
Dave Airlie056219e2007-07-11 16:17:42 +100096 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 drm_i830_buf_priv_t *buf_priv;
98
99 lock_kernel();
Dave Airlie2c14f282008-04-21 16:47:32 +1000100 dev = priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000102 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
106 vma->vm_file = filp;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000107
108 buf_priv->currently_mapped = I830_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 unlock_kernel();
110
111 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlie3d774612006-08-07 20:07:43 +1000112 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000113 vma->vm_end - vma->vm_start, vma->vm_page_prot))
114 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116}
117
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800118static const struct file_operations i830_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000119 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000120 .release = drm_release,
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000121 .unlocked_ioctl = drm_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000122 .mmap = i830_mmap_buffers,
123 .fasync = drm_fasync,
Dave Airliec94f7022005-07-07 21:03:38 +1000124};
125
Eric Anholt6c340ea2007-08-25 20:23:09 +1000126static int i830_map_buffer(struct drm_buf * buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Dave Airlie2c14f282008-04-21 16:47:32 +1000128 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130 drm_i830_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800131 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long virtual;
133 int retcode = 0;
134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
136 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000139 old_fops = file_priv->filp->f_op;
140 file_priv->filp->f_op = &i830_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 dev_priv->mmap_buffer = buf;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000142 virtual = do_mmap(file_priv->filp, 0, buf->total, PROT_READ | PROT_WRITE,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000143 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000145 file_priv->filp->f_op = old_fops;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000146 if (IS_ERR((void *)virtual)) { /* ugh */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* Real error */
148 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000149 retcode = PTR_ERR((void *)virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 buf_priv->virtual = NULL;
151 } else {
152 buf_priv->virtual = (void __user *)virtual;
153 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return retcode;
157}
158
Dave Airlie056219e2007-07-11 16:17:42 +1000159static int i830_unmap_buffer(struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
162 int retcode = 0;
163
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164 if (buf_priv->currently_mapped != I830_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -EINVAL;
166
167 down_write(&current->mm->mmap_sem);
168 retcode = do_munmap(current->mm,
169 (unsigned long)buf_priv->virtual,
170 (size_t) buf->total);
171 up_write(&current->mm->mmap_sem);
172
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000173 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
174 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return retcode;
177}
178
Dave Airlieeddca552007-07-11 16:09:54 +1000179static int i830_dma_get_buffer(struct drm_device * dev, drm_i830_dma_t * d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000180 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Dave Airlie056219e2007-07-11 16:17:42 +1000182 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 drm_i830_buf_priv_t *buf_priv;
184 int retcode = 0;
185
186 buf = i830_freelist_get(dev);
187 if (!buf) {
188 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000189 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return retcode;
191 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192
Eric Anholt6c340ea2007-08-25 20:23:09 +1000193 retcode = i830_map_buffer(buf, file_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 if (retcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 i830_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return retcode;
198 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000199 buf->file_priv = file_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200 buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000202 d->request_idx = buf->idx;
203 d->request_size = buf->total;
204 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 return retcode;
207}
208
Dave Airlieeddca552007-07-11 16:09:54 +1000209static int i830_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Dave Airliecdd55a22007-07-11 16:32:08 +1000211 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* Make sure interrupts are disabled here because the uninstall ioctl
214 * may not have been called from userspace and after dev_private
215 * is freed, it's too late.
216 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 if (dev->irq_enabled)
218 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (dev->dev_private) {
221 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222 drm_i830_private_t *dev_priv =
223 (drm_i830_private_t *) dev->dev_private;
224
225 if (dev_priv->ring.virtual_start) {
Dave Airlieb9094d32007-01-08 21:31:13 +1100226 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 if (dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 pci_free_consistent(dev->pdev, PAGE_SIZE,
230 dev_priv->hw_status_page,
231 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232 /* Need to rewrite hardware status page */
233 I830_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235
Eric Anholt9a298b22009-03-24 12:23:04 -0700236 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000240 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100243 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Dave Airlieeddca552007-07-11 16:09:54 +1000249int i830_wait_ring(struct drm_device * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251 drm_i830_private_t *dev_priv = dev->dev_private;
252 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
253 int iters = 0;
254 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned int last_head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
256
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000257 end = jiffies + (HZ * 3);
258 while (ring->space < n) {
259 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
260 ring->space = ring->head - (ring->tail + 8);
261 if (ring->space < 0)
262 ring->space += ring->Size;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (ring->head != last_head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000265 end = jiffies + (HZ * 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 last_head = ring->head;
267 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268
269 iters++;
270 if (time_before(end, jiffies)) {
271 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
272 DRM_ERROR("lockup\n");
273 goto out_wait_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 udelay(1);
276 dev_priv->sarea_priv->perf_boxes |= I830_BOX_WAIT;
277 }
278
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279 out_wait_ring:
280 return iters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Dave Airlieeddca552007-07-11 16:09:54 +1000283static void i830_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 drm_i830_private_t *dev_priv = dev->dev_private;
286 drm_i830_ring_buffer_t *ring = &(dev_priv->ring);
287
288 ring->head = I830_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
289 ring->tail = I830_READ(LP_RING + RING_TAIL) & TAIL_ADDR;
290 ring->space = ring->head - (ring->tail + 8);
291 if (ring->space < 0)
292 ring->space += ring->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (ring->head == ring->tail)
295 dev_priv->sarea_priv->perf_boxes |= I830_BOX_RING_EMPTY;
296}
297
Dave Airlieeddca552007-07-11 16:09:54 +1000298static int i830_freelist_init(struct drm_device * dev, drm_i830_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Dave Airliecdd55a22007-07-11 16:32:08 +1000300 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000301 int my_idx = 36;
302 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
303 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 if (dma->buf_count > 1019) {
306 /* Not enough space in the status page for the freelist */
307 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000311 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000312 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000314 buf_priv->in_use = hw_status++;
315 buf_priv->my_use_idx = my_idx;
316 my_idx += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 *buf_priv->in_use = I830_BUF_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Dave Airlieb9094d32007-01-08 21:31:13 +1100320 buf_priv->map.offset = buf->bus_address;
321 buf_priv->map.size = buf->total;
322 buf_priv->map.type = _DRM_AGP;
323 buf_priv->map.flags = 0;
324 buf_priv->map.mtrr = 0;
325
326 drm_core_ioremap(&buf_priv->map, dev);
327 buf_priv->kernel_virtual = buf_priv->map.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 return 0;
330}
331
Dave Airlieeddca552007-07-11 16:09:54 +1000332static int i830_dma_initialize(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 drm_i830_private_t * dev_priv,
334 drm_i830_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Dave Airlie55910512007-07-11 16:53:40 +1000336 struct drm_map_list *r_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 memset(dev_priv, 0, sizeof(drm_i830_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Dave Airliebd1b3312007-05-26 05:01:51 +1000340 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 if (r_list->map &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000345 break;
346 }
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 if (!dev_priv->sarea_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 dev->dev_private = (void *)dev_priv;
351 i830_dma_cleanup(dev);
352 DRM_ERROR("can not find sarea!\n");
353 return -EINVAL;
354 }
355 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000356 if (!dev_priv->mmio_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 dev->dev_private = (void *)dev_priv;
358 i830_dma_cleanup(dev);
359 DRM_ERROR("can not find mmio map!\n");
360 return -EINVAL;
361 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000362 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 dev->dev_private = (void *)dev_priv;
366 i830_dma_cleanup(dev);
367 DRM_ERROR("can not find dma buffer map!\n");
368 return -EINVAL;
369 }
370
371 dev_priv->sarea_priv = (drm_i830_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000374 dev_priv->ring.Start = init->ring_start;
375 dev_priv->ring.End = init->ring_end;
376 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dave Airlieb9094d32007-01-08 21:31:13 +1100378 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
379 dev_priv->ring.map.size = init->ring_size;
380 dev_priv->ring.map.type = _DRM_AGP;
381 dev_priv->ring.map.flags = 0;
382 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dave Airlieb9094d32007-01-08 21:31:13 +1100384 drm_core_ioremap(&dev_priv->ring.map, dev);
385
386 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 dev->dev_private = (void *)dev_priv;
388 i830_dma_cleanup(dev);
389 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000391 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Dave Airlieb9094d32007-01-08 21:31:13 +1100394 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
395
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 dev_priv->w = init->w;
399 dev_priv->h = init->h;
400 dev_priv->pitch = init->pitch;
401 dev_priv->back_offset = init->back_offset;
402 dev_priv->depth_offset = init->depth_offset;
403 dev_priv->front_offset = init->front_offset;
404
405 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
406 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
407 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
408
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000409 DRM_DEBUG("front_di1 %x\n", dev_priv->front_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 DRM_DEBUG("back_offset %x\n", dev_priv->back_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 DRM_DEBUG("back_di1 %x\n", dev_priv->back_di1);
412 DRM_DEBUG("pitch_bits %x\n", init->pitch_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 dev_priv->cpp = init->cpp;
415 /* We are using separate values as placeholders for mechanisms for
416 * private backbuffer/depthbuffer usage.
417 */
418
419 dev_priv->back_pitch = init->back_pitch;
420 dev_priv->depth_pitch = init->depth_pitch;
421 dev_priv->do_boxes = 0;
422 dev_priv->use_mi_batchbuffer_start = 0;
423
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000424 /* Program Hardware Status Page */
425 dev_priv->hw_status_page =
426 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
427 &dev_priv->dma_status_page);
428 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dev->dev_private = (void *)dev_priv;
430 i830_dma_cleanup(dev);
431 DRM_ERROR("Can not allocate hardware status page\n");
432 return -ENOMEM;
433 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000434 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000436
437 I830_WRITE(0x02080, dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 DRM_DEBUG("Enabled hardware status page\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000439
440 /* Now we need to init our freelist */
441 if (i830_freelist_init(dev, dev_priv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443 i830_dma_cleanup(dev);
444 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 dev->dev_private = (void *)dev_priv;
449
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Eric Anholtc153f452007-09-03 12:06:45 +1000453static int i830_dma_init(struct drm_device *dev, void *data,
454 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 drm_i830_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000457 drm_i830_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 int retcode = 0;
459
Eric Anholtc153f452007-09-03 12:06:45 +1000460 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 case I830_INIT_DMA:
Eric Anholt9a298b22009-03-24 12:23:04 -0700462 dev_priv = kmalloc(sizeof(drm_i830_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 if (dev_priv == NULL)
464 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000465 retcode = i830_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 break;
467 case I830_CLEANUP_DMA:
468 retcode = i830_dma_cleanup(dev);
469 break;
470 default:
471 retcode = -EINVAL;
472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474
475 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478#define GFX_OP_STIPPLE ((0x3<<29)|(0x1d<<24)|(0x83<<16))
479#define ST1_ENABLE (1<<16)
480#define ST1_MASK (0xffff)
481
482/* Most efficient way to verify state for the i830 is as it is
483 * emitted. Non-conformant state is silently dropped.
484 */
Dave Airlieeddca552007-07-11 16:09:54 +1000485static void i830EmitContextVerified(struct drm_device * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000487 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int i, j = 0;
489 unsigned int tmp;
490 RING_LOCALS;
491
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 BEGIN_LP_RING(I830_CTX_SETUP_SIZE + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 for (i = 0; i < I830_CTXREG_BLENDCOLR0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000496 if ((tmp & (7 << 29)) == CMD_3D &&
497 (tmp & (0x1f << 24)) < (0x1d << 24)) {
498 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 j++;
500 } else {
501 DRM_ERROR("Skipping %d\n", i);
502 }
503 }
504
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505 OUT_RING(STATE3D_CONST_BLEND_COLOR_CMD);
506 OUT_RING(code[I830_CTXREG_BLENDCOLR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 j += 2;
508
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000509 for (i = I830_CTXREG_VF; i < I830_CTXREG_MCSB0; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000511 if ((tmp & (7 << 29)) == CMD_3D &&
512 (tmp & (0x1f << 24)) < (0x1d << 24)) {
513 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 j++;
515 } else {
516 DRM_ERROR("Skipping %d\n", i);
517 }
518 }
519
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 OUT_RING(STATE3D_MAP_COORD_SETBIND_CMD);
521 OUT_RING(code[I830_CTXREG_MCSB1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 j += 2;
523
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 if (j & 1)
525 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 ADVANCE_LP_RING();
528}
529
Dave Airlieeddca552007-07-11 16:09:54 +1000530static void i830EmitTexVerified(struct drm_device * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int i, j = 0;
534 unsigned int tmp;
535 RING_LOCALS;
536
537 if (code[I830_TEXREG_MI0] == GFX_OP_MAP_INFO ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 (code[I830_TEXREG_MI0] & ~(0xf * LOAD_TEXTURE_MAP0)) ==
539 (STATE3D_LOAD_STATE_IMMEDIATE_2 | 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 BEGIN_LP_RING(I830_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 OUT_RING(code[I830_TEXREG_MI0]); /* TM0LI */
544 OUT_RING(code[I830_TEXREG_MI1]); /* TM0S0 */
545 OUT_RING(code[I830_TEXREG_MI2]); /* TM0S1 */
546 OUT_RING(code[I830_TEXREG_MI3]); /* TM0S2 */
547 OUT_RING(code[I830_TEXREG_MI4]); /* TM0S3 */
548 OUT_RING(code[I830_TEXREG_MI5]); /* TM0S4 */
549
550 for (i = 6; i < I830_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000552 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000556 if (j & 1)
557 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 printk("rejected packet %x\n", code[0]);
562}
563
Dave Airlieeddca552007-07-11 16:09:54 +1000564static void i830EmitTexBlendVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 unsigned int *code, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int i, j = 0;
569 unsigned int tmp;
570 RING_LOCALS;
571
572 if (!num)
573 return;
574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 BEGIN_LP_RING(num + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 tmp = code[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 j++;
581 }
582
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 if (j & 1)
584 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 ADVANCE_LP_RING();
587}
588
Dave Airlieeddca552007-07-11 16:09:54 +1000589static void i830EmitTexPalette(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 unsigned int *palette, int number, int is_shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000592 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int i;
594 RING_LOCALS;
595
596 return;
597
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 BEGIN_LP_RING(258);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000600 if (is_shared == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 OUT_RING(CMD_OP_MAP_PALETTE_LOAD |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602 MAP_PALETTE_NUM(0) | MAP_PALETTE_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 } else {
604 OUT_RING(CMD_OP_MAP_PALETTE_LOAD | MAP_PALETTE_NUM(number));
605 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 for (i = 0; i < 256; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 OUT_RING(palette[i]);
608 }
609 OUT_RING(0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000610 /* KW: WHERE IS THE ADVANCE_LP_RING? This is effectively a noop!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
612}
613
614/* Need to do some additional checking when setting the dest buffer.
615 */
Dave Airlieeddca552007-07-11 16:09:54 +1000616static void i830EmitDestVerified(struct drm_device * dev, unsigned int *code)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617{
618 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 unsigned int tmp;
620 RING_LOCALS;
621
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 BEGIN_LP_RING(I830_DEST_SETUP_SIZE + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 tmp = code[I830_DESTREG_CBUFADDR];
625 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
626 if (((int)outring) & 8) {
627 OUT_RING(0);
628 OUT_RING(0);
629 }
630
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 OUT_RING(CMD_OP_DESTBUFFER_INFO);
632 OUT_RING(BUF_3D_ID_COLOR_BACK |
633 BUF_3D_PITCH(dev_priv->back_pitch * dev_priv->cpp) |
634 BUF_3D_USE_FENCE);
635 OUT_RING(tmp);
636 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 OUT_RING(CMD_OP_DESTBUFFER_INFO);
639 OUT_RING(BUF_3D_ID_DEPTH | BUF_3D_USE_FENCE |
640 BUF_3D_PITCH(dev_priv->depth_pitch * dev_priv->cpp));
641 OUT_RING(dev_priv->zi1);
642 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 } else {
644 DRM_ERROR("bad di1 %x (allow %x or %x)\n",
645 tmp, dev_priv->front_di1, dev_priv->back_di1);
646 }
647
648 /* invarient:
649 */
650
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000651 OUT_RING(GFX_OP_DESTBUFFER_VARS);
652 OUT_RING(code[I830_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000654 OUT_RING(GFX_OP_DRAWRECT_INFO);
655 OUT_RING(code[I830_DESTREG_DR1]);
656 OUT_RING(code[I830_DESTREG_DR2]);
657 OUT_RING(code[I830_DESTREG_DR3]);
658 OUT_RING(code[I830_DESTREG_DR4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* Need to verify this */
661 tmp = code[I830_DESTREG_SENABLE];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000662 if ((tmp & ~0x3) == GFX_OP_SCISSOR_ENABLE) {
663 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 } else {
665 DRM_ERROR("bad scissor enable\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 OUT_RING(GFX_OP_SCISSOR_RECT);
670 OUT_RING(code[I830_DESTREG_SR1]);
671 OUT_RING(code[I830_DESTREG_SR2]);
672 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 ADVANCE_LP_RING();
675}
676
Dave Airlieeddca552007-07-11 16:09:54 +1000677static void i830EmitStippleVerified(struct drm_device * dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000679 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 RING_LOCALS;
681
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 BEGIN_LP_RING(2);
683 OUT_RING(GFX_OP_STIPPLE);
684 OUT_RING(code[1]);
685 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Dave Airlieeddca552007-07-11 16:09:54 +1000688static void i830EmitState(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 drm_i830_private_t *dev_priv = dev->dev_private;
691 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 unsigned int dirty = sarea_priv->dirty;
693
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700694 DRM_DEBUG("%s %x\n", __func__, dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 if (dirty & I830_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 i830EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 sarea_priv->dirty &= ~I830_UPLOAD_BUFFERS;
699 }
700
701 if (dirty & I830_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000702 i830EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 sarea_priv->dirty &= ~I830_UPLOAD_CTX;
704 }
705
706 if (dirty & I830_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 i830EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 sarea_priv->dirty &= ~I830_UPLOAD_TEX0;
709 }
710
711 if (dirty & I830_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 i830EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 sarea_priv->dirty &= ~I830_UPLOAD_TEX1;
714 }
715
716 if (dirty & I830_UPLOAD_TEXBLEND0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[0],
718 sarea_priv->TexBlendStateWordsUsed[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND0;
720 }
721
722 if (dirty & I830_UPLOAD_TEXBLEND1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[1],
724 sarea_priv->TexBlendStateWordsUsed[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND1;
726 }
727
728 if (dirty & I830_UPLOAD_TEX_PALETTE_SHARED) {
729 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 1);
730 } else {
731 if (dirty & I830_UPLOAD_TEX_PALETTE_N(0)) {
732 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 0);
733 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(0);
734 }
735 if (dirty & I830_UPLOAD_TEX_PALETTE_N(1)) {
736 i830EmitTexPalette(dev, sarea_priv->Palette[1], 1, 0);
737 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(1);
738 }
739
740 /* 1.3:
741 */
742#if 0
743 if (dirty & I830_UPLOAD_TEX_PALETTE_N(2)) {
744 i830EmitTexPalette(dev, sarea_priv->Palette2[0], 0, 0);
745 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
746 }
747 if (dirty & I830_UPLOAD_TEX_PALETTE_N(3)) {
748 i830EmitTexPalette(dev, sarea_priv->Palette2[1], 1, 0);
749 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
750 }
751#endif
752 }
753
754 /* 1.3:
755 */
756 if (dirty & I830_UPLOAD_STIPPLE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 i830EmitStippleVerified(dev, sarea_priv->StippleState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 sarea_priv->dirty &= ~I830_UPLOAD_STIPPLE;
759 }
760
761 if (dirty & I830_UPLOAD_TEX2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000762 i830EmitTexVerified(dev, sarea_priv->TexState2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 sarea_priv->dirty &= ~I830_UPLOAD_TEX2;
764 }
765
766 if (dirty & I830_UPLOAD_TEX3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 i830EmitTexVerified(dev, sarea_priv->TexState3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 sarea_priv->dirty &= ~I830_UPLOAD_TEX3;
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (dirty & I830_UPLOAD_TEXBLEND2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000772 i830EmitTexBlendVerified(dev,
773 sarea_priv->TexBlendState2,
774 sarea_priv->TexBlendStateWordsUsed2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND2;
777 }
778
779 if (dirty & I830_UPLOAD_TEXBLEND3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 i830EmitTexBlendVerified(dev,
781 sarea_priv->TexBlendState3,
782 sarea_priv->TexBlendStateWordsUsed3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND3;
784 }
785}
786
787/* ================================================================
788 * Performance monitoring functions
789 */
790
Dave Airlieeddca552007-07-11 16:09:54 +1000791static void i830_fill_box(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 u32 color;
796 unsigned int BR13, CMD;
797 RING_LOCALS;
798
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 BR13 = (0xF0 << 16) | (dev_priv->pitch * dev_priv->cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 CMD = XY_COLOR_BLT_CMD;
801 x += dev_priv->sarea_priv->boxes[0].x1;
802 y += dev_priv->sarea_priv->boxes[0].y1;
803
804 if (dev_priv->cpp == 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 BR13 |= (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 CMD |= (XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 } else {
809 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 BEGIN_LP_RING(6);
814 OUT_RING(CMD);
815 OUT_RING(BR13);
816 OUT_RING((y << 16) | x);
817 OUT_RING(((y + h) << 16) | (x + w));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 if (dev_priv->current_page == 1) {
820 OUT_RING(dev_priv->front_offset);
821 } else {
822 OUT_RING(dev_priv->back_offset);
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ADVANCE_LP_RING();
827}
828
Dave Airlieeddca552007-07-11 16:09:54 +1000829static void i830_cp_performance_boxes(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Purple box for page flipping
834 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_FLIP)
836 i830_fill_box(dev, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 /* Red box if we have to wait for idle at any point
839 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_WAIT)
841 i830_fill_box(dev, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* Blue box: lost context?
844 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_LOST_CONTEXT)
846 i830_fill_box(dev, 28, 4, 8, 8, 0, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /* Yellow box for texture swaps
849 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_TEXTURE_LOAD)
851 i830_fill_box(dev, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /* Green box if hardware never idles (as far as we can tell)
854 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 if (!(dev_priv->sarea_priv->perf_boxes & I830_BOX_RING_EMPTY))
856 i830_fill_box(dev, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * (not a great measure, easily confused)
860 */
861 if (dev_priv->dma_used) {
862 int bar = dev_priv->dma_used / 10240;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 if (bar > 100)
864 bar = 100;
865 if (bar < 1)
866 bar = 1;
867 i830_fill_box(dev, 4, 16, bar, 4, 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 dev_priv->dma_used = 0;
869 }
870
871 dev_priv->sarea_priv->perf_boxes = 0;
872}
873
Dave Airlieeddca552007-07-11 16:09:54 +1000874static void i830_dma_dispatch_clear(struct drm_device * dev, int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 unsigned int clear_color,
876 unsigned int clear_zval,
877 unsigned int clear_depthmask)
878{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 drm_i830_private_t *dev_priv = dev->dev_private;
880 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000882 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 int pitch = dev_priv->pitch;
884 int cpp = dev_priv->cpp;
885 int i;
886 unsigned int BR13, CMD, D_CMD;
887 RING_LOCALS;
888
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 if (dev_priv->current_page == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 unsigned int tmp = flags;
891
892 flags &= ~(I830_FRONT | I830_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 if (tmp & I830_FRONT)
894 flags |= I830_BACK;
895 if (tmp & I830_BACK)
896 flags |= I830_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 switch (cpp) {
902 case 2:
903 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 D_CMD = CMD = XY_COLOR_BLT_CMD;
905 break;
906 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24) | (1 << 25);
908 CMD = (XY_COLOR_BLT_CMD | XY_COLOR_BLT_WRITE_ALPHA |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 XY_COLOR_BLT_WRITE_RGB);
910 D_CMD = XY_COLOR_BLT_CMD;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 if (clear_depthmask & 0x00ffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 D_CMD |= XY_COLOR_BLT_WRITE_RGB;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000913 if (clear_depthmask & 0xff000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 D_CMD |= XY_COLOR_BLT_WRITE_ALPHA;
915 break;
916 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 D_CMD = CMD = XY_COLOR_BLT_CMD;
919 break;
920 }
921
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000922 if (nbox > I830_NR_SAREA_CLIPRECTS)
923 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (pbox->x1 > pbox->x2 ||
927 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 continue;
930
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 if (flags & I830_FRONT) {
932 DRM_DEBUG("clear front\n");
933 BEGIN_LP_RING(6);
934 OUT_RING(CMD);
935 OUT_RING(BR13);
936 OUT_RING((pbox->y1 << 16) | pbox->x1);
937 OUT_RING((pbox->y2 << 16) | pbox->x2);
938 OUT_RING(dev_priv->front_offset);
939 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 ADVANCE_LP_RING();
941 }
942
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000943 if (flags & I830_BACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 DRM_DEBUG("clear back\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000945 BEGIN_LP_RING(6);
946 OUT_RING(CMD);
947 OUT_RING(BR13);
948 OUT_RING((pbox->y1 << 16) | pbox->x1);
949 OUT_RING((pbox->y2 << 16) | pbox->x2);
950 OUT_RING(dev_priv->back_offset);
951 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ADVANCE_LP_RING();
953 }
954
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 if (flags & I830_DEPTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 DRM_DEBUG("clear depth\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000957 BEGIN_LP_RING(6);
958 OUT_RING(D_CMD);
959 OUT_RING(BR13);
960 OUT_RING((pbox->y1 << 16) | pbox->x1);
961 OUT_RING((pbox->y2 << 16) | pbox->x2);
962 OUT_RING(dev_priv->depth_offset);
963 OUT_RING(clear_zval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 ADVANCE_LP_RING();
965 }
966 }
967}
968
Dave Airlieeddca552007-07-11 16:09:54 +1000969static void i830_dma_dispatch_swap(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 drm_i830_private_t *dev_priv = dev->dev_private;
972 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000974 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 int pitch = dev_priv->pitch;
976 int cpp = dev_priv->cpp;
977 int i;
978 unsigned int CMD, BR13;
979 RING_LOCALS;
980
981 DRM_DEBUG("swapbuffers\n");
982
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000983 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 if (dev_priv->do_boxes)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000988 switch (cpp) {
989 case 2:
990 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 CMD = XY_SRC_COPY_BLT_CMD;
992 break;
993 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000994 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24) | (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
996 XY_SRC_COPY_BLT_WRITE_RGB);
997 break;
998 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 CMD = XY_SRC_COPY_BLT_CMD;
1001 break;
1002 }
1003
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001004 if (nbox > I830_NR_SAREA_CLIPRECTS)
1005 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (pbox->x1 > pbox->x2 ||
1009 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 continue;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 DRM_DEBUG("dispatch swap %d,%d-%d,%d!\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 pbox->x1, pbox->y1, pbox->x2, pbox->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 BEGIN_LP_RING(8);
1017 OUT_RING(CMD);
1018 OUT_RING(BR13);
1019 OUT_RING((pbox->y1 << 16) | pbox->x1);
1020 OUT_RING((pbox->y2 << 16) | pbox->x2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001022 if (dev_priv->current_page == 0)
1023 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 OUT_RING((pbox->y1 << 16) | pbox->x1);
1028 OUT_RING(BR13 & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 if (dev_priv->current_page == 0)
1031 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 ADVANCE_LP_RING();
1036 }
1037}
1038
Dave Airlieeddca552007-07-11 16:09:54 +10001039static void i830_dma_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 RING_LOCALS;
1043
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001045 __func__,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 dev_priv->current_page,
1047 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (dev_priv->do_boxes) {
1052 dev_priv->sarea_priv->perf_boxes |= I830_BOX_FLIP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001056 BEGIN_LP_RING(2);
1057 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1058 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ADVANCE_LP_RING();
1060
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 BEGIN_LP_RING(6);
1062 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
1063 OUT_RING(0);
1064 if (dev_priv->current_page == 0) {
1065 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 dev_priv->current_page = 1;
1067 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 dev_priv->current_page = 0;
1070 }
1071 OUT_RING(0);
1072 ADVANCE_LP_RING();
1073
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 BEGIN_LP_RING(2);
1075 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
1076 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1080}
1081
Dave Airlieeddca552007-07-11 16:09:54 +10001082static void i830_dma_dispatch_vertex(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +10001083 struct drm_buf * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +10001088 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 unsigned long address = (unsigned long)buf->bus_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 unsigned long start = address - dev->agp->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int i = 0, u;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001097 if (nbox > I830_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 nbox = I830_NR_SAREA_CLIPRECTS;
1099
1100 if (discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 u = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 I830_BUF_HARDWARE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 if (u != I830_BUF_CLIENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 DRM_DEBUG("xxxx 2\n");
1105 }
1106 }
1107
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 if (used > 4 * 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 used = 0;
1110
1111 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 i830EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 DRM_DEBUG("dispatch vertex addr 0x%lx, used 0x%x nbox %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 address, used, nbox);
1116
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001117 dev_priv->counter++;
1118 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1119 DRM_DEBUG("i830_dma_dispatch\n");
1120 DRM_DEBUG("start : %lx\n", start);
1121 DRM_DEBUG("used : %d\n", used);
1122 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 if (buf_priv->currently_mapped == I830_BUF_MAPPED) {
1125 u32 *vp = buf_priv->kernel_virtual;
1126
1127 vp[0] = (GFX_OP_PRIMITIVE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 sarea_priv->vertex_prim | ((used / 4) - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 if (dev_priv->use_mi_batchbuffer_start) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131 vp[used / 4] = MI_BATCH_BUFFER_END;
1132 used += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (used & 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001136 vp[used / 4] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 used += 4;
1138 }
1139
1140 i830_unmap_buffer(buf);
1141 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (used) {
1144 do {
1145 if (i < nbox) {
1146 BEGIN_LP_RING(6);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001147 OUT_RING(GFX_OP_DRAWRECT_INFO);
1148 OUT_RING(sarea_priv->
1149 BufferState[I830_DESTREG_DR1]);
1150 OUT_RING(box[i].x1 | (box[i].y1 << 16));
1151 OUT_RING(box[i].x2 | (box[i].y2 << 16));
1152 OUT_RING(sarea_priv->
1153 BufferState[I830_DESTREG_DR4]);
1154 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 ADVANCE_LP_RING();
1156 }
1157
1158 if (dev_priv->use_mi_batchbuffer_start) {
1159 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001160 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
1161 OUT_RING(start | MI_BATCH_NON_SECURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001165 OUT_RING(MI_BATCH_BUFFER);
1166 OUT_RING(start | MI_BATCH_NON_SECURE);
1167 OUT_RING(start + used - 4);
1168 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 ADVANCE_LP_RING();
1170 }
1171
1172 } while (++i < nbox);
1173 }
1174
1175 if (discard) {
1176 dev_priv->counter++;
1177
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001178 (void)cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
1179 I830_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001182 OUT_RING(CMD_STORE_DWORD_IDX);
1183 OUT_RING(20);
1184 OUT_RING(dev_priv->counter);
1185 OUT_RING(CMD_STORE_DWORD_IDX);
1186 OUT_RING(buf_priv->my_use_idx);
1187 OUT_RING(I830_BUF_FREE);
1188 OUT_RING(CMD_REPORT_HEAD);
1189 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ADVANCE_LP_RING();
1191 }
1192}
1193
Dave Airlieeddca552007-07-11 16:09:54 +10001194static void i830_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001196 drm_i830_private_t *dev_priv = dev->dev_private;
1197 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001199 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001201 BEGIN_LP_RING(4);
1202 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1203 OUT_RING(CMD_REPORT_HEAD);
1204 OUT_RING(0);
1205 OUT_RING(0);
1206 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001208 i830_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Dave Airlieeddca552007-07-11 16:09:54 +10001211static int i830_flush_queue(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001213 drm_i830_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001214 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001215 int i, ret = 0;
1216 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001218 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001220 BEGIN_LP_RING(2);
1221 OUT_RING(CMD_REPORT_HEAD);
1222 OUT_RING(0);
1223 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001225 i830_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001226
1227 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001228 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001229 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1230
1231 int used = cmpxchg(buf_priv->in_use, I830_BUF_HARDWARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 I830_BUF_FREE);
1233
1234 if (used == I830_BUF_HARDWARE)
1235 DRM_DEBUG("reclaimed from HARDWARE\n");
1236 if (used == I830_BUF_CLIENT)
1237 DRM_DEBUG("still on client\n");
1238 }
1239
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243/* Must be called with the lock held */
Eric Anholt6c340ea2007-08-25 20:23:09 +10001244static void i830_reclaim_buffers(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Dave Airliecdd55a22007-07-11 16:32:08 +10001246 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001247 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001249 if (!dma)
1250 return;
1251 if (!dev->dev_private)
1252 return;
1253 if (!dma->buflist)
1254 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001256 i830_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001259 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001260 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1261
Eric Anholt6c340ea2007-08-25 20:23:09 +10001262 if (buf->file_priv == file_priv && buf_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001263 int used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 I830_BUF_FREE);
1265
1266 if (used == I830_BUF_CLIENT)
1267 DRM_DEBUG("reclaimed from client\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001268 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
1269 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 }
1272}
1273
Eric Anholtc153f452007-09-03 12:06:45 +10001274static int i830_flush_ioctl(struct drm_device *dev, void *data,
1275 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001277 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001279 i830_flush_queue(dev);
1280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Eric Anholtc153f452007-09-03 12:06:45 +10001283static int i830_dma_vertex(struct drm_device *dev, void *data,
1284 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Dave Airliecdd55a22007-07-11 16:32:08 +10001286 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001287 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1288 u32 *hw_status = dev_priv->hw_status_page;
1289 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1290 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001291 drm_i830_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Eric Anholt6c340ea2007-08-25 20:23:09 +10001293 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 DRM_DEBUG("i830 dma vertex, idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001296 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Eric Anholtc153f452007-09-03 12:06:45 +10001298 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001301 i830_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +10001302 dma->buflist[vertex->idx],
1303 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305 sarea_priv->last_enqueue = dev_priv->counter - 1;
1306 sarea_priv->last_dispatch = (int)hw_status[5];
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309}
1310
Eric Anholtc153f452007-09-03 12:06:45 +10001311static int i830_clear_bufs(struct drm_device *dev, void *data,
1312 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Eric Anholtc153f452007-09-03 12:06:45 +10001314 drm_i830_clear_t *clear = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001315
Eric Anholt6c340ea2007-08-25 20:23:09 +10001316 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /* GH: Someone's doing nasty things... */
1319 if (!dev->dev_private) {
1320 return -EINVAL;
1321 }
1322
Eric Anholtc153f452007-09-03 12:06:45 +10001323 i830_dma_dispatch_clear(dev, clear->flags,
1324 clear->clear_color,
1325 clear->clear_depth, clear->clear_depthmask);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Eric Anholtc153f452007-09-03 12:06:45 +10001329static int i830_swap_bufs(struct drm_device *dev, void *data,
1330 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 DRM_DEBUG("i830_swap_bufs\n");
1333
Eric Anholt6c340ea2007-08-25 20:23:09 +10001334 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001336 i830_dma_dispatch_swap(dev);
1337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001341 */
Dave Airlieeddca552007-07-11 16:09:54 +10001342static void i830_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 drm_i830_private_t *dev_priv = dev->dev_private;
1345
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001346 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 dev_priv->page_flipping = 1;
1348 dev_priv->current_page = 0;
1349 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1350}
1351
Dave Airlieeddca552007-07-11 16:09:54 +10001352static int i830_do_cleanup_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 drm_i830_private_t *dev_priv = dev->dev_private;
1355
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001356 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001358 i830_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 dev_priv->page_flipping = 0;
1361 return 0;
1362}
1363
Eric Anholtc153f452007-09-03 12:06:45 +10001364static int i830_flip_bufs(struct drm_device *dev, void *data,
1365 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 drm_i830_private_t *dev_priv = dev->dev_private;
1368
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001369 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Eric Anholt6c340ea2007-08-25 20:23:09 +10001371 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001373 if (!dev_priv->page_flipping)
1374 i830_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001376 i830_dma_dispatch_flip(dev);
1377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Eric Anholtc153f452007-09-03 12:06:45 +10001380static int i830_getage(struct drm_device *dev, void *data,
1381 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001383 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1384 u32 *hw_status = dev_priv->hw_status_page;
1385 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1386 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001388 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return 0;
1390}
1391
Eric Anholtc153f452007-09-03 12:06:45 +10001392static int i830_getbuf(struct drm_device *dev, void *data,
1393 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001395 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001396 drm_i830_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001397 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1398 u32 *hw_status = dev_priv->hw_status_page;
1399 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1400 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 DRM_DEBUG("getbuf\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001403
Eric Anholt6c340ea2007-08-25 20:23:09 +10001404 LOCK_TEST_WITH_RETURN(dev, file_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001405
Eric Anholtc153f452007-09-03 12:06:45 +10001406 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Eric Anholtc153f452007-09-03 12:06:45 +10001408 retcode = i830_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 DRM_DEBUG("i830_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001411 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001413 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 return retcode;
1416}
1417
Eric Anholtc153f452007-09-03 12:06:45 +10001418static int i830_copybuf(struct drm_device *dev, void *data,
1419 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 /* Never copy - 2.4.x doesn't need it */
1422 return 0;
1423}
1424
Eric Anholtc153f452007-09-03 12:06:45 +10001425static int i830_docopy(struct drm_device *dev, void *data,
1426 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 return 0;
1429}
1430
Eric Anholtc153f452007-09-03 12:06:45 +10001431static int i830_getparam(struct drm_device *dev, void *data,
1432 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 drm_i830_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001435 drm_i830_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int value;
1437
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001438 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001439 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return -EINVAL;
1441 }
1442
Eric Anholtc153f452007-09-03 12:06:45 +10001443 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 case I830_PARAM_IRQ_ACTIVE:
1445 value = dev->irq_enabled;
1446 break;
1447 default:
1448 return -EINVAL;
1449 }
1450
Eric Anholtc153f452007-09-03 12:06:45 +10001451 if (copy_to_user(param->value, &value, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001452 DRM_ERROR("copy_to_user\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -EFAULT;
1454 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return 0;
1457}
1458
Eric Anholtc153f452007-09-03 12:06:45 +10001459static int i830_setparam(struct drm_device *dev, void *data,
1460 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 drm_i830_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001463 drm_i830_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001465 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001466 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EINVAL;
1468 }
1469
Eric Anholtc153f452007-09-03 12:06:45 +10001470 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 case I830_SETPARAM_USE_MI_BATCHBUFFER_START:
Eric Anholtc153f452007-09-03 12:06:45 +10001472 dev_priv->use_mi_batchbuffer_start = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 break;
1474 default:
1475 return -EINVAL;
1476 }
1477
1478 return 0;
1479}
1480
Dave Airlieeddca552007-07-11 16:09:54 +10001481int i830_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001482{
1483 /* i830 has 4 more counters */
1484 dev->counters += 4;
1485 dev->types[6] = _DRM_STAT_IRQ;
1486 dev->types[7] = _DRM_STAT_PRIMARY;
1487 dev->types[8] = _DRM_STAT_SECONDARY;
1488 dev->types[9] = _DRM_STAT_DMA;
1489
1490 return 0;
1491}
1492
Dave Airlieeddca552007-07-11 16:09:54 +10001493void i830_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001495 i830_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Eric Anholt6c340ea2007-08-25 20:23:09 +10001498void i830_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 if (dev->dev_private) {
1501 drm_i830_private_t *dev_priv = dev->dev_private;
1502 if (dev_priv->page_flipping) {
1503 i830_do_cleanup_pageflip(dev);
1504 }
1505 }
1506}
1507
Eric Anholt6c340ea2007-08-25 20:23:09 +10001508void i830_driver_reclaim_buffers_locked(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001510 i830_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Dave Airlieeddca552007-07-11 16:09:54 +10001513int i830_driver_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 i830_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return 0;
1517}
1518
Eric Anholtc153f452007-09-03 12:06:45 +10001519struct drm_ioctl_desc i830_ioctls[] = {
1520 DRM_IOCTL_DEF(DRM_I830_INIT, i830_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1521 DRM_IOCTL_DEF(DRM_I830_VERTEX, i830_dma_vertex, DRM_AUTH),
1522 DRM_IOCTL_DEF(DRM_I830_CLEAR, i830_clear_bufs, DRM_AUTH),
1523 DRM_IOCTL_DEF(DRM_I830_FLUSH, i830_flush_ioctl, DRM_AUTH),
1524 DRM_IOCTL_DEF(DRM_I830_GETAGE, i830_getage, DRM_AUTH),
1525 DRM_IOCTL_DEF(DRM_I830_GETBUF, i830_getbuf, DRM_AUTH),
1526 DRM_IOCTL_DEF(DRM_I830_SWAP, i830_swap_bufs, DRM_AUTH),
1527 DRM_IOCTL_DEF(DRM_I830_COPY, i830_copybuf, DRM_AUTH),
1528 DRM_IOCTL_DEF(DRM_I830_DOCOPY, i830_docopy, DRM_AUTH),
1529 DRM_IOCTL_DEF(DRM_I830_FLIP, i830_flip_bufs, DRM_AUTH),
1530 DRM_IOCTL_DEF(DRM_I830_IRQ_EMIT, i830_irq_emit, DRM_AUTH),
1531 DRM_IOCTL_DEF(DRM_I830_IRQ_WAIT, i830_irq_wait, DRM_AUTH),
1532 DRM_IOCTL_DEF(DRM_I830_GETPARAM, i830_getparam, DRM_AUTH),
1533 DRM_IOCTL_DEF(DRM_I830_SETPARAM, i830_setparam, DRM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534};
1535
1536int i830_max_ioctl = DRM_ARRAY_SIZE(i830_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001537
1538/**
1539 * Determine if the device really is AGP or not.
1540 *
1541 * All Intel graphics chipsets are treated as AGP, even if they are really
1542 * PCI-e.
1543 *
1544 * \param dev The device to be tested.
1545 *
1546 * \returns
1547 * A value of 1 is always retured to indictate every i8xx is AGP.
1548 */
Dave Airlieeddca552007-07-11 16:09:54 +10001549int i830_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001550{
1551 return 1;
1552}