blob: 5168862c92271e5d7fddfb48d3470f4f70113d92 [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 */
Arnd Bergmann58374712010-07-10 23:51:39 +020039#include <linux/smp_lock.h>
Harvey Harrison21534302008-02-13 15:03:17 -080040#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
44
45#define I830_BUF_FREE 2
46#define I830_BUF_CLIENT 1
Dave Airliebc5f4522007-11-05 12:50:58 +100047#define I830_BUF_HARDWARE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define I830_BUF_UNMAPPED 0
50#define I830_BUF_MAPPED 1
51
Dave Airlie056219e2007-07-11 16:17:42 +100052static struct drm_buf *i830_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Dave Airliecdd55a22007-07-11 16:32:08 +100054 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100055 int i;
56 int used;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /* Linear search might not be the best solution */
59
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +100061 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100062 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100064 used = cmpxchg(buf_priv->in_use, I830_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 I830_BUF_CLIENT);
Nicolas Kaiser56499112010-07-15 00:14:43 +020066 if (used == I830_BUF_FREE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
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
Nicolas Kaiser56499112010-07-15 00:14:43 +020076static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200126static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200159static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200179static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200209static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200225 if (dev_priv->ring.virtual_start)
Dave Airlieb9094d32007-01-08 21:31:13 +1100226 drm_core_ioremapfree(&dev_priv->ring.map, dev);
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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200248int 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200278out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279 return iters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Nicolas Kaiser56499112010-07-15 00:14:43 +0200282static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200297static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200331static int i830_dma_initialize(struct drm_device *dev,
332 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 */
Nicolas Kaiser56499112010-07-15 00:14:43 +0200484static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200529static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200563static 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
Nicolas Kaiser56499112010-07-15 00:14:43 +0200588static 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 }
Nicolas Kaiser56499112010-07-15 00:14:43 +0200605 for (i = 0; i < 256; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 OUT_RING(palette[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 OUT_RING(0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 /* KW: WHERE IS THE ADVANCE_LP_RING? This is effectively a noop!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
610}
611
612/* Need to do some additional checking when setting the dest buffer.
613 */
Nicolas Kaiser56499112010-07-15 00:14:43 +0200614static void i830EmitDestVerified(struct drm_device *dev, unsigned int *code)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615{
616 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 unsigned int tmp;
618 RING_LOCALS;
619
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 BEGIN_LP_RING(I830_DEST_SETUP_SIZE + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 tmp = code[I830_DESTREG_CBUFADDR];
623 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
624 if (((int)outring) & 8) {
625 OUT_RING(0);
626 OUT_RING(0);
627 }
628
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 OUT_RING(CMD_OP_DESTBUFFER_INFO);
630 OUT_RING(BUF_3D_ID_COLOR_BACK |
631 BUF_3D_PITCH(dev_priv->back_pitch * dev_priv->cpp) |
632 BUF_3D_USE_FENCE);
633 OUT_RING(tmp);
634 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636 OUT_RING(CMD_OP_DESTBUFFER_INFO);
637 OUT_RING(BUF_3D_ID_DEPTH | BUF_3D_USE_FENCE |
638 BUF_3D_PITCH(dev_priv->depth_pitch * dev_priv->cpp));
639 OUT_RING(dev_priv->zi1);
640 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 } else {
642 DRM_ERROR("bad di1 %x (allow %x or %x)\n",
643 tmp, dev_priv->front_di1, dev_priv->back_di1);
644 }
645
646 /* invarient:
647 */
648
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 OUT_RING(GFX_OP_DESTBUFFER_VARS);
650 OUT_RING(code[I830_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 OUT_RING(GFX_OP_DRAWRECT_INFO);
653 OUT_RING(code[I830_DESTREG_DR1]);
654 OUT_RING(code[I830_DESTREG_DR2]);
655 OUT_RING(code[I830_DESTREG_DR3]);
656 OUT_RING(code[I830_DESTREG_DR4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* Need to verify this */
659 tmp = code[I830_DESTREG_SENABLE];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 if ((tmp & ~0x3) == GFX_OP_SCISSOR_ENABLE) {
661 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 } else {
663 DRM_ERROR("bad scissor enable\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000664 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 OUT_RING(GFX_OP_SCISSOR_RECT);
668 OUT_RING(code[I830_DESTREG_SR1]);
669 OUT_RING(code[I830_DESTREG_SR2]);
670 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 ADVANCE_LP_RING();
673}
674
Nicolas Kaiser56499112010-07-15 00:14:43 +0200675static void i830EmitStippleVerified(struct drm_device *dev, unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 RING_LOCALS;
679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 BEGIN_LP_RING(2);
681 OUT_RING(GFX_OP_STIPPLE);
682 OUT_RING(code[1]);
683 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Nicolas Kaiser56499112010-07-15 00:14:43 +0200686static void i830EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 drm_i830_private_t *dev_priv = dev->dev_private;
689 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 unsigned int dirty = sarea_priv->dirty;
691
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700692 DRM_DEBUG("%s %x\n", __func__, dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (dirty & I830_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 i830EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 sarea_priv->dirty &= ~I830_UPLOAD_BUFFERS;
697 }
698
699 if (dirty & I830_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 i830EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 sarea_priv->dirty &= ~I830_UPLOAD_CTX;
702 }
703
704 if (dirty & I830_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 i830EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 sarea_priv->dirty &= ~I830_UPLOAD_TEX0;
707 }
708
709 if (dirty & I830_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 i830EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 sarea_priv->dirty &= ~I830_UPLOAD_TEX1;
712 }
713
714 if (dirty & I830_UPLOAD_TEXBLEND0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[0],
716 sarea_priv->TexBlendStateWordsUsed[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND0;
718 }
719
720 if (dirty & I830_UPLOAD_TEXBLEND1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 i830EmitTexBlendVerified(dev, sarea_priv->TexBlendState[1],
722 sarea_priv->TexBlendStateWordsUsed[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND1;
724 }
725
726 if (dirty & I830_UPLOAD_TEX_PALETTE_SHARED) {
727 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 1);
728 } else {
729 if (dirty & I830_UPLOAD_TEX_PALETTE_N(0)) {
730 i830EmitTexPalette(dev, sarea_priv->Palette[0], 0, 0);
731 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(0);
732 }
733 if (dirty & I830_UPLOAD_TEX_PALETTE_N(1)) {
734 i830EmitTexPalette(dev, sarea_priv->Palette[1], 1, 0);
735 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(1);
736 }
737
738 /* 1.3:
739 */
740#if 0
741 if (dirty & I830_UPLOAD_TEX_PALETTE_N(2)) {
742 i830EmitTexPalette(dev, sarea_priv->Palette2[0], 0, 0);
743 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
744 }
745 if (dirty & I830_UPLOAD_TEX_PALETTE_N(3)) {
746 i830EmitTexPalette(dev, sarea_priv->Palette2[1], 1, 0);
747 sarea_priv->dirty &= ~I830_UPLOAD_TEX_PALETTE_N(2);
748 }
749#endif
750 }
751
752 /* 1.3:
753 */
754 if (dirty & I830_UPLOAD_STIPPLE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 i830EmitStippleVerified(dev, sarea_priv->StippleState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 sarea_priv->dirty &= ~I830_UPLOAD_STIPPLE;
757 }
758
759 if (dirty & I830_UPLOAD_TEX2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 i830EmitTexVerified(dev, sarea_priv->TexState2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 sarea_priv->dirty &= ~I830_UPLOAD_TEX2;
762 }
763
764 if (dirty & I830_UPLOAD_TEX3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 i830EmitTexVerified(dev, sarea_priv->TexState3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 sarea_priv->dirty &= ~I830_UPLOAD_TEX3;
767 }
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (dirty & I830_UPLOAD_TEXBLEND2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 i830EmitTexBlendVerified(dev,
771 sarea_priv->TexBlendState2,
772 sarea_priv->TexBlendStateWordsUsed2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND2;
775 }
776
777 if (dirty & I830_UPLOAD_TEXBLEND3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778 i830EmitTexBlendVerified(dev,
779 sarea_priv->TexBlendState3,
780 sarea_priv->TexBlendStateWordsUsed3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 sarea_priv->dirty &= ~I830_UPLOAD_TEXBLEND3;
782 }
783}
784
785/* ================================================================
786 * Performance monitoring functions
787 */
788
Nicolas Kaiser56499112010-07-15 00:14:43 +0200789static void i830_fill_box(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000790 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 u32 color;
794 unsigned int BR13, CMD;
795 RING_LOCALS;
796
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 BR13 = (0xF0 << 16) | (dev_priv->pitch * dev_priv->cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 CMD = XY_COLOR_BLT_CMD;
799 x += dev_priv->sarea_priv->boxes[0].x1;
800 y += dev_priv->sarea_priv->boxes[0].y1;
801
802 if (dev_priv->cpp == 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 BR13 |= (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 CMD |= (XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 } else {
807 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 BEGIN_LP_RING(6);
812 OUT_RING(CMD);
813 OUT_RING(BR13);
814 OUT_RING((y << 16) | x);
815 OUT_RING(((y + h) << 16) | (x + w));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Nicolas Kaiser56499112010-07-15 00:14:43 +0200817 if (dev_priv->current_page == 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 OUT_RING(dev_priv->front_offset);
Nicolas Kaiser56499112010-07-15 00:14:43 +0200819 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ADVANCE_LP_RING();
824}
825
Nicolas Kaiser56499112010-07-15 00:14:43 +0200826static void i830_cp_performance_boxes(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000828 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* Purple box for page flipping
831 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_FLIP)
833 i830_fill_box(dev, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* Red box if we have to wait for idle at any point
836 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_WAIT)
838 i830_fill_box(dev, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* Blue box: lost context?
841 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_LOST_CONTEXT)
843 i830_fill_box(dev, 28, 4, 8, 8, 0, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* Yellow box for texture swaps
846 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 if (dev_priv->sarea_priv->perf_boxes & I830_BOX_TEXTURE_LOAD)
848 i830_fill_box(dev, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* Green box if hardware never idles (as far as we can tell)
851 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 if (!(dev_priv->sarea_priv->perf_boxes & I830_BOX_RING_EMPTY))
853 i830_fill_box(dev, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * (not a great measure, easily confused)
857 */
858 if (dev_priv->dma_used) {
859 int bar = dev_priv->dma_used / 10240;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 if (bar > 100)
861 bar = 100;
862 if (bar < 1)
863 bar = 1;
864 i830_fill_box(dev, 4, 16, bar, 4, 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 dev_priv->dma_used = 0;
866 }
867
868 dev_priv->sarea_priv->perf_boxes = 0;
869}
870
Nicolas Kaiser56499112010-07-15 00:14:43 +0200871static void i830_dma_dispatch_clear(struct drm_device *dev, int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 unsigned int clear_color,
873 unsigned int clear_zval,
874 unsigned int clear_depthmask)
875{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 drm_i830_private_t *dev_priv = dev->dev_private;
877 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000879 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int pitch = dev_priv->pitch;
881 int cpp = dev_priv->cpp;
882 int i;
883 unsigned int BR13, CMD, D_CMD;
884 RING_LOCALS;
885
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 if (dev_priv->current_page == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 unsigned int tmp = flags;
888
889 flags &= ~(I830_FRONT | I830_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 if (tmp & I830_FRONT)
891 flags |= I830_BACK;
892 if (tmp & I830_BACK)
893 flags |= I830_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 switch (cpp) {
899 case 2:
900 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 D_CMD = CMD = XY_COLOR_BLT_CMD;
902 break;
903 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000904 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24) | (1 << 25);
905 CMD = (XY_COLOR_BLT_CMD | XY_COLOR_BLT_WRITE_ALPHA |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 XY_COLOR_BLT_WRITE_RGB);
907 D_CMD = XY_COLOR_BLT_CMD;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 if (clear_depthmask & 0x00ffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 D_CMD |= XY_COLOR_BLT_WRITE_RGB;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 if (clear_depthmask & 0xff000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 D_CMD |= XY_COLOR_BLT_WRITE_ALPHA;
912 break;
913 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 BR13 = (0xF0 << 16) | (pitch * cpp) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 D_CMD = CMD = XY_COLOR_BLT_CMD;
916 break;
917 }
918
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 if (nbox > I830_NR_SAREA_CLIPRECTS)
920 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000922 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (pbox->x1 > pbox->x2 ||
924 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 continue;
927
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 if (flags & I830_FRONT) {
929 DRM_DEBUG("clear front\n");
930 BEGIN_LP_RING(6);
931 OUT_RING(CMD);
932 OUT_RING(BR13);
933 OUT_RING((pbox->y1 << 16) | pbox->x1);
934 OUT_RING((pbox->y2 << 16) | pbox->x2);
935 OUT_RING(dev_priv->front_offset);
936 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 ADVANCE_LP_RING();
938 }
939
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000940 if (flags & I830_BACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 DRM_DEBUG("clear back\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 BEGIN_LP_RING(6);
943 OUT_RING(CMD);
944 OUT_RING(BR13);
945 OUT_RING((pbox->y1 << 16) | pbox->x1);
946 OUT_RING((pbox->y2 << 16) | pbox->x2);
947 OUT_RING(dev_priv->back_offset);
948 OUT_RING(clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 ADVANCE_LP_RING();
950 }
951
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 if (flags & I830_DEPTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 DRM_DEBUG("clear depth\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000954 BEGIN_LP_RING(6);
955 OUT_RING(D_CMD);
956 OUT_RING(BR13);
957 OUT_RING((pbox->y1 << 16) | pbox->x1);
958 OUT_RING((pbox->y2 << 16) | pbox->x2);
959 OUT_RING(dev_priv->depth_offset);
960 OUT_RING(clear_zval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 ADVANCE_LP_RING();
962 }
963 }
964}
965
Nicolas Kaiser56499112010-07-15 00:14:43 +0200966static void i830_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 drm_i830_private_t *dev_priv = dev->dev_private;
969 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000971 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int pitch = dev_priv->pitch;
973 int cpp = dev_priv->cpp;
974 int i;
975 unsigned int CMD, BR13;
976 RING_LOCALS;
977
978 DRM_DEBUG("swapbuffers\n");
979
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000980 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (dev_priv->do_boxes)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000983 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000985 switch (cpp) {
986 case 2:
987 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 CMD = XY_SRC_COPY_BLT_CMD;
989 break;
990 case 4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000991 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24) | (1 << 25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
993 XY_SRC_COPY_BLT_WRITE_RGB);
994 break;
995 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 CMD = XY_SRC_COPY_BLT_CMD;
998 break;
999 }
1000
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 if (nbox > I830_NR_SAREA_CLIPRECTS)
1002 nbox = I830_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001004 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (pbox->x1 > pbox->x2 ||
1006 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 continue;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 DRM_DEBUG("dispatch swap %d,%d-%d,%d!\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 pbox->x1, pbox->y1, pbox->x2, pbox->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001013 BEGIN_LP_RING(8);
1014 OUT_RING(CMD);
1015 OUT_RING(BR13);
1016 OUT_RING((pbox->y1 << 16) | pbox->x1);
1017 OUT_RING((pbox->y2 << 16) | pbox->x2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019 if (dev_priv->current_page == 0)
1020 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001022 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 OUT_RING((pbox->y1 << 16) | pbox->x1);
1025 OUT_RING(BR13 & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 if (dev_priv->current_page == 0)
1028 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 ADVANCE_LP_RING();
1033 }
1034}
1035
Nicolas Kaiser56499112010-07-15 00:14:43 +02001036static void i830_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 RING_LOCALS;
1040
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001042 __func__,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 dev_priv->current_page,
1044 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (dev_priv->do_boxes) {
1049 dev_priv->sarea_priv->perf_boxes |= I830_BOX_FLIP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 i830_cp_performance_boxes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 BEGIN_LP_RING(2);
1054 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1055 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 ADVANCE_LP_RING();
1057
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 BEGIN_LP_RING(6);
1059 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
1060 OUT_RING(0);
1061 if (dev_priv->current_page == 0) {
1062 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 dev_priv->current_page = 1;
1064 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 dev_priv->current_page = 0;
1067 }
1068 OUT_RING(0);
1069 ADVANCE_LP_RING();
1070
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001071 BEGIN_LP_RING(2);
1072 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
1073 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1077}
1078
Nicolas Kaiser56499112010-07-15 00:14:43 +02001079static void i830_dma_dispatch_vertex(struct drm_device *dev,
1080 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 drm_i830_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 drm_i830_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +10001085 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 unsigned long address = (unsigned long)buf->bus_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001088 unsigned long start = address - dev->agp->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 int i = 0, u;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 if (nbox > I830_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 nbox = I830_NR_SAREA_CLIPRECTS;
1096
1097 if (discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098 u = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 I830_BUF_HARDWARE);
Nicolas Kaiser56499112010-07-15 00:14:43 +02001100 if (u != I830_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 DRM_DEBUG("xxxx 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001104 if (used > 4 * 1023)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 used = 0;
1106
1107 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 i830EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001110 DRM_DEBUG("dispatch vertex addr 0x%lx, used 0x%x nbox %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 address, used, nbox);
1112
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001113 dev_priv->counter++;
1114 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1115 DRM_DEBUG("i830_dma_dispatch\n");
1116 DRM_DEBUG("start : %lx\n", start);
1117 DRM_DEBUG("used : %d\n", used);
1118 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (buf_priv->currently_mapped == I830_BUF_MAPPED) {
1121 u32 *vp = buf_priv->kernel_virtual;
1122
1123 vp[0] = (GFX_OP_PRIMITIVE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 sarea_priv->vertex_prim | ((used / 4) - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 if (dev_priv->use_mi_batchbuffer_start) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127 vp[used / 4] = MI_BATCH_BUFFER_END;
1128 used += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (used & 4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 vp[used / 4] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 used += 4;
1134 }
1135
1136 i830_unmap_buffer(buf);
1137 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (used) {
1140 do {
1141 if (i < nbox) {
1142 BEGIN_LP_RING(6);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001143 OUT_RING(GFX_OP_DRAWRECT_INFO);
1144 OUT_RING(sarea_priv->
1145 BufferState[I830_DESTREG_DR1]);
1146 OUT_RING(box[i].x1 | (box[i].y1 << 16));
1147 OUT_RING(box[i].x2 | (box[i].y2 << 16));
1148 OUT_RING(sarea_priv->
1149 BufferState[I830_DESTREG_DR4]);
1150 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 ADVANCE_LP_RING();
1152 }
1153
1154 if (dev_priv->use_mi_batchbuffer_start) {
1155 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001156 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
1157 OUT_RING(start | MI_BATCH_NON_SECURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 ADVANCE_LP_RING();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001159 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001161 OUT_RING(MI_BATCH_BUFFER);
1162 OUT_RING(start | MI_BATCH_NON_SECURE);
1163 OUT_RING(start + used - 4);
1164 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 ADVANCE_LP_RING();
1166 }
1167
1168 } while (++i < nbox);
1169 }
1170
1171 if (discard) {
1172 dev_priv->counter++;
1173
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001174 (void)cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
1175 I830_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001178 OUT_RING(CMD_STORE_DWORD_IDX);
1179 OUT_RING(20);
1180 OUT_RING(dev_priv->counter);
1181 OUT_RING(CMD_STORE_DWORD_IDX);
1182 OUT_RING(buf_priv->my_use_idx);
1183 OUT_RING(I830_BUF_FREE);
1184 OUT_RING(CMD_REPORT_HEAD);
1185 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ADVANCE_LP_RING();
1187 }
1188}
1189
Nicolas Kaiser56499112010-07-15 00:14:43 +02001190static void i830_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001192 drm_i830_private_t *dev_priv = dev->dev_private;
1193 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001195 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001197 BEGIN_LP_RING(4);
1198 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
1199 OUT_RING(CMD_REPORT_HEAD);
1200 OUT_RING(0);
1201 OUT_RING(0);
1202 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001204 i830_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Nicolas Kaiser56499112010-07-15 00:14:43 +02001207static int i830_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001209 drm_i830_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001210 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001211 int i, ret = 0;
1212 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001214 i830_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001216 BEGIN_LP_RING(2);
1217 OUT_RING(CMD_REPORT_HEAD);
1218 OUT_RING(0);
1219 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001221 i830_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001222
1223 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001224 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001225 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1226
1227 int used = cmpxchg(buf_priv->in_use, I830_BUF_HARDWARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 I830_BUF_FREE);
1229
1230 if (used == I830_BUF_HARDWARE)
1231 DRM_DEBUG("reclaimed from HARDWARE\n");
1232 if (used == I830_BUF_CLIENT)
1233 DRM_DEBUG("still on client\n");
1234 }
1235
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239/* Must be called with the lock held */
Nicolas Kaiser56499112010-07-15 00:14:43 +02001240static void i830_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Dave Airliecdd55a22007-07-11 16:32:08 +10001242 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001243 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001245 if (!dma)
1246 return;
1247 if (!dev->dev_private)
1248 return;
1249 if (!dma->buflist)
1250 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001252 i830_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001255 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001256 drm_i830_buf_priv_t *buf_priv = buf->dev_private;
1257
Eric Anholt6c340ea2007-08-25 20:23:09 +10001258 if (buf->file_priv == file_priv && buf_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001259 int used = cmpxchg(buf_priv->in_use, I830_BUF_CLIENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 I830_BUF_FREE);
1261
1262 if (used == I830_BUF_CLIENT)
1263 DRM_DEBUG("reclaimed from client\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001264 if (buf_priv->currently_mapped == I830_BUF_MAPPED)
1265 buf_priv->currently_mapped = I830_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 }
1268}
1269
Eric Anholtc153f452007-09-03 12:06:45 +10001270static int i830_flush_ioctl(struct drm_device *dev, void *data,
1271 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001273 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001275 i830_flush_queue(dev);
1276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Eric Anholtc153f452007-09-03 12:06:45 +10001279static int i830_dma_vertex(struct drm_device *dev, void *data,
1280 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Dave Airliecdd55a22007-07-11 16:32:08 +10001282 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001283 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1284 u32 *hw_status = dev_priv->hw_status_page;
1285 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1286 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001287 drm_i830_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Eric Anholt6c340ea2007-08-25 20:23:09 +10001289 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 DRM_DEBUG("i830 dma vertex, idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001292 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Eric Anholtc153f452007-09-03 12:06:45 +10001294 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001297 i830_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +10001298 dma->buflist[vertex->idx],
1299 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001301 sarea_priv->last_enqueue = dev_priv->counter - 1;
1302 sarea_priv->last_dispatch = (int)hw_status[5];
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return 0;
1305}
1306
Eric Anholtc153f452007-09-03 12:06:45 +10001307static int i830_clear_bufs(struct drm_device *dev, void *data,
1308 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Eric Anholtc153f452007-09-03 12:06:45 +10001310 drm_i830_clear_t *clear = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001311
Eric Anholt6c340ea2007-08-25 20:23:09 +10001312 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 /* GH: Someone's doing nasty things... */
Nicolas Kaiser56499112010-07-15 00:14:43 +02001315 if (!dev->dev_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Eric Anholtc153f452007-09-03 12:06:45 +10001318 i830_dma_dispatch_clear(dev, clear->flags,
1319 clear->clear_color,
1320 clear->clear_depth, clear->clear_depthmask);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Eric Anholtc153f452007-09-03 12:06:45 +10001324static int i830_swap_bufs(struct drm_device *dev, void *data,
1325 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 DRM_DEBUG("i830_swap_bufs\n");
1328
Eric Anholt6c340ea2007-08-25 20:23:09 +10001329 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001331 i830_dma_dispatch_swap(dev);
1332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001336 */
Nicolas Kaiser56499112010-07-15 00:14:43 +02001337static void i830_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 drm_i830_private_t *dev_priv = dev->dev_private;
1340
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001341 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 dev_priv->page_flipping = 1;
1343 dev_priv->current_page = 0;
1344 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1345}
1346
Nicolas Kaiser56499112010-07-15 00:14:43 +02001347static int i830_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 drm_i830_private_t *dev_priv = dev->dev_private;
1350
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001351 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001353 i830_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 dev_priv->page_flipping = 0;
1356 return 0;
1357}
1358
Eric Anholtc153f452007-09-03 12:06:45 +10001359static int i830_flip_bufs(struct drm_device *dev, void *data,
1360 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 drm_i830_private_t *dev_priv = dev->dev_private;
1363
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001364 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Eric Anholt6c340ea2007-08-25 20:23:09 +10001366 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001368 if (!dev_priv->page_flipping)
1369 i830_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001371 i830_dma_dispatch_flip(dev);
1372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Eric Anholtc153f452007-09-03 12:06:45 +10001375static int i830_getage(struct drm_device *dev, void *data,
1376 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001378 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1379 u32 *hw_status = dev_priv->hw_status_page;
1380 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1381 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001383 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return 0;
1385}
1386
Eric Anholtc153f452007-09-03 12:06:45 +10001387static int i830_getbuf(struct drm_device *dev, void *data,
1388 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001390 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001391 drm_i830_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001392 drm_i830_private_t *dev_priv = (drm_i830_private_t *) dev->dev_private;
1393 u32 *hw_status = dev_priv->hw_status_page;
1394 drm_i830_sarea_t *sarea_priv = (drm_i830_sarea_t *)
1395 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 DRM_DEBUG("getbuf\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001398
Eric Anholt6c340ea2007-08-25 20:23:09 +10001399 LOCK_TEST_WITH_RETURN(dev, file_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001400
Eric Anholtc153f452007-09-03 12:06:45 +10001401 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Eric Anholtc153f452007-09-03 12:06:45 +10001403 retcode = i830_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 DRM_DEBUG("i830_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001406 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 return retcode;
1411}
1412
Eric Anholtc153f452007-09-03 12:06:45 +10001413static int i830_copybuf(struct drm_device *dev, void *data,
1414 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 /* Never copy - 2.4.x doesn't need it */
1417 return 0;
1418}
1419
Eric Anholtc153f452007-09-03 12:06:45 +10001420static int i830_docopy(struct drm_device *dev, void *data,
1421 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 return 0;
1424}
1425
Eric Anholtc153f452007-09-03 12:06:45 +10001426static int i830_getparam(struct drm_device *dev, void *data,
1427 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 drm_i830_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001430 drm_i830_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int value;
1432
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001433 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001434 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return -EINVAL;
1436 }
1437
Eric Anholtc153f452007-09-03 12:06:45 +10001438 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 case I830_PARAM_IRQ_ACTIVE:
1440 value = dev->irq_enabled;
1441 break;
1442 default:
1443 return -EINVAL;
1444 }
1445
Eric Anholtc153f452007-09-03 12:06:45 +10001446 if (copy_to_user(param->value, &value, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001447 DRM_ERROR("copy_to_user\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -EFAULT;
1449 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return 0;
1452}
1453
Eric Anholtc153f452007-09-03 12:06:45 +10001454static int i830_setparam(struct drm_device *dev, void *data,
1455 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 drm_i830_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001458 drm_i830_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001460 if (!dev_priv) {
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001461 DRM_ERROR("%s called with no initialization\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -EINVAL;
1463 }
1464
Eric Anholtc153f452007-09-03 12:06:45 +10001465 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 case I830_SETPARAM_USE_MI_BATCHBUFFER_START:
Eric Anholtc153f452007-09-03 12:06:45 +10001467 dev_priv->use_mi_batchbuffer_start = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 break;
1469 default:
1470 return -EINVAL;
1471 }
1472
1473 return 0;
1474}
1475
Dave Airlieeddca552007-07-11 16:09:54 +10001476int i830_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001477{
1478 /* i830 has 4 more counters */
1479 dev->counters += 4;
1480 dev->types[6] = _DRM_STAT_IRQ;
1481 dev->types[7] = _DRM_STAT_PRIMARY;
1482 dev->types[8] = _DRM_STAT_SECONDARY;
1483 dev->types[9] = _DRM_STAT_DMA;
1484
1485 return 0;
1486}
1487
Nicolas Kaiser56499112010-07-15 00:14:43 +02001488void i830_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001490 i830_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Nicolas Kaiser56499112010-07-15 00:14:43 +02001493void i830_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 if (dev->dev_private) {
1496 drm_i830_private_t *dev_priv = dev->dev_private;
Nicolas Kaiser56499112010-07-15 00:14:43 +02001497 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 i830_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500}
1501
Nicolas Kaiser56499112010-07-15 00:14:43 +02001502void i830_driver_reclaim_buffers_locked(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001504 i830_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Nicolas Kaiser56499112010-07-15 00:14:43 +02001507int i830_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001509 i830_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return 0;
1511}
1512
Arnd Bergmann58374712010-07-10 23:51:39 +02001513/*
1514 * call the drm_ioctl under the big kernel lock because
1515 * to lock against the i830_mmap_buffers function.
1516 */
1517long i830_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1518{
1519 int ret;
1520 lock_kernel();
1521 ret = drm_ioctl(file, cmd, arg);
1522 unlock_kernel();
1523 return ret;
1524}
1525
Eric Anholtc153f452007-09-03 12:06:45 +10001526struct drm_ioctl_desc i830_ioctls[] = {
Arnd Bergmann58374712010-07-10 23:51:39 +02001527 DRM_IOCTL_DEF(DRM_I830_INIT, i830_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1528 DRM_IOCTL_DEF(DRM_I830_VERTEX, i830_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1529 DRM_IOCTL_DEF(DRM_I830_CLEAR, i830_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1530 DRM_IOCTL_DEF(DRM_I830_FLUSH, i830_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1531 DRM_IOCTL_DEF(DRM_I830_GETAGE, i830_getage, DRM_AUTH|DRM_UNLOCKED),
1532 DRM_IOCTL_DEF(DRM_I830_GETBUF, i830_getbuf, DRM_AUTH|DRM_UNLOCKED),
1533 DRM_IOCTL_DEF(DRM_I830_SWAP, i830_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1534 DRM_IOCTL_DEF(DRM_I830_COPY, i830_copybuf, DRM_AUTH|DRM_UNLOCKED),
1535 DRM_IOCTL_DEF(DRM_I830_DOCOPY, i830_docopy, DRM_AUTH|DRM_UNLOCKED),
1536 DRM_IOCTL_DEF(DRM_I830_FLIP, i830_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
1537 DRM_IOCTL_DEF(DRM_I830_IRQ_EMIT, i830_irq_emit, DRM_AUTH|DRM_UNLOCKED),
1538 DRM_IOCTL_DEF(DRM_I830_IRQ_WAIT, i830_irq_wait, DRM_AUTH|DRM_UNLOCKED),
1539 DRM_IOCTL_DEF(DRM_I830_GETPARAM, i830_getparam, DRM_AUTH|DRM_UNLOCKED),
1540 DRM_IOCTL_DEF(DRM_I830_SETPARAM, i830_setparam, DRM_AUTH|DRM_UNLOCKED),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541};
1542
1543int i830_max_ioctl = DRM_ARRAY_SIZE(i830_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001544
1545/**
1546 * Determine if the device really is AGP or not.
1547 *
1548 * All Intel graphics chipsets are treated as AGP, even if they are really
1549 * PCI-e.
1550 *
1551 * \param dev The device to be tested.
1552 *
1553 * \returns
1554 * A value of 1 is always retured to indictate every i8xx is AGP.
1555 */
Nicolas Kaiser56499112010-07-15 00:14:43 +02001556int i830_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001557{
1558 return 1;
1559}