blob: 4d23bd0f8711dc6ebc01342effe757a2ebb44638 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i810_dma.c -- DMA support for the i810 -*- linux-c -*-
2 * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
29 * Keith Whitwell <keith@tungstengraphics.com>
30 *
31 */
32
33#include "drmP.h"
34#include "drm.h"
35#include "i810_drm.h"
36#include "i810_drv.h"
37#include <linux/interrupt.h> /* For task queue support */
38#include <linux/delay.h>
39#include <linux/pagemap.h>
40
41#define I810_BUF_FREE 2
42#define I810_BUF_CLIENT 1
43#define I810_BUF_HARDWARE 0
44
45#define I810_BUF_UNMAPPED 0
46#define I810_BUF_MAPPED 1
47
Dave Airlie056219e2007-07-11 16:17:42 +100048static struct drm_buf *i810_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Dave Airliecdd55a22007-07-11 16:32:08 +100050 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100051 int i;
52 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /* Linear search might not be the best solution */
55
Dave Airlieb5e89ed2005-09-25 14:28:13 +100056 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +100057 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 used = cmpxchg(buf_priv->in_use, I810_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 I810_BUF_CLIENT);
62 if (used == I810_BUF_FREE) {
63 return buf;
64 }
65 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100066 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69/* This should only be called if the buffer is not sent to the hardware
70 * yet, the hardware updates in use for us once its on the ring buffer.
71 */
72
Dave Airlie056219e2007-07-11 16:17:42 +100073static int i810_freelist_put(struct drm_device * dev, struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
76 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078 /* In use is already a pointer */
79 used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_FREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (used != I810_BUF_CLIENT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100081 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
82 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Dave Airliec94f7022005-07-07 21:03:38 +100088static int i810_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Dave Airlieeddca552007-07-11 16:09:54 +100090 struct drm_file *priv = filp->private_data;
91 struct drm_device *dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 drm_i810_private_t *dev_priv;
Dave Airlie056219e2007-07-11 16:17:42 +100093 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 drm_i810_buf_priv_t *buf_priv;
95
96 lock_kernel();
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097 dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100099 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 buf_priv = buf->dev_private;
101
102 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
103 vma->vm_file = filp;
104
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 buf_priv->currently_mapped = I810_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unlock_kernel();
107
108 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlie3d774612006-08-07 20:07:43 +1000109 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110 vma->vm_end - vma->vm_start, vma->vm_page_prot))
111 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
113}
114
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800115static const struct file_operations i810_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000117 .release = drm_release,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000118 .ioctl = drm_ioctl,
119 .mmap = i810_mmap_buffers,
120 .fasync = drm_fasync,
Dave Airliec94f7022005-07-07 21:03:38 +1000121};
122
Dave Airlie056219e2007-07-11 16:17:42 +1000123static int i810_map_buffer(struct drm_buf * buf, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Dave Airlieeddca552007-07-11 16:09:54 +1000125 struct drm_file *priv = filp->private_data;
126 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800129 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int retcode = 0;
131
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return -EINVAL;
134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135 down_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 old_fops = filp->f_op;
137 filp->f_op = &i810_buffer_fops;
138 dev_priv->mmap_buffer = buf;
139 buf_priv->virtual = (void *)do_mmap(filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 PROT_READ | PROT_WRITE,
141 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 dev_priv->mmap_buffer = NULL;
143 filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000144 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Real error */
146 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000147 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 buf_priv->virtual = NULL;
149 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 return retcode;
153}
154
Dave Airlie056219e2007-07-11 16:17:42 +1000155static int i810_unmap_buffer(struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
158 int retcode = 0;
159
160 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
161 return -EINVAL;
162
163 down_write(&current->mm->mmap_sem);
164 retcode = do_munmap(current->mm,
165 (unsigned long)buf_priv->virtual,
166 (size_t) buf->total);
167 up_write(&current->mm->mmap_sem);
168
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
170 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 return retcode;
173}
174
Dave Airlieeddca552007-07-11 16:09:54 +1000175static int i810_dma_get_buffer(struct drm_device * dev, drm_i810_dma_t * d,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct file *filp)
177{
Dave Airlie056219e2007-07-11 16:17:42 +1000178 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 drm_i810_buf_priv_t *buf_priv;
180 int retcode = 0;
181
182 buf = i810_freelist_get(dev);
183 if (!buf) {
184 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return retcode;
187 }
188
189 retcode = i810_map_buffer(buf, filp);
190 if (retcode) {
191 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return retcode;
194 }
195 buf->filp = filp;
196 buf_priv = buf->dev_private;
197 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 d->request_idx = buf->idx;
199 d->request_size = buf->total;
200 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 return retcode;
203}
204
Dave Airlieeddca552007-07-11 16:09:54 +1000205static int i810_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Dave Airliecdd55a22007-07-11 16:32:08 +1000207 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Make sure interrupts are disabled here because the uninstall ioctl
210 * may not have been called from userspace and after dev_private
211 * is freed, it's too late.
212 */
213 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
214 drm_irq_uninstall(dev);
215
216 if (dev->dev_private) {
217 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 drm_i810_private_t *dev_priv =
219 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 if (dev_priv->ring.virtual_start) {
Dave Airlieb9094d32007-01-08 21:31:13 +1100222 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000224 if (dev_priv->hw_status_page) {
225 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 dev_priv->hw_status_page,
227 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 /* Need to rewrite hardware status page */
229 I810_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 drm_free(dev->dev_private, sizeof(drm_i810_private_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 DRM_MEM_DRIVER);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000233 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000236 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100238
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100240 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
Dave Airlieeddca552007-07-11 16:09:54 +1000246static int i810_wait_ring(struct drm_device * dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247{
248 drm_i810_private_t *dev_priv = dev->dev_private;
249 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
250 int iters = 0;
251 unsigned long end;
252 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
253
254 end = jiffies + (HZ * 3);
255 while (ring->space < n) {
256 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
257 ring->space = ring->head - (ring->tail + 8);
258 if (ring->space < 0)
259 ring->space += ring->Size;
260
261 if (ring->head != last_head) {
262 end = jiffies + (HZ * 3);
263 last_head = ring->head;
264 }
265
266 iters++;
267 if (time_before(end, jiffies)) {
268 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
269 DRM_ERROR("lockup\n");
270 goto out_wait_ring;
271 }
272 udelay(1);
273 }
274
275 out_wait_ring:
276 return iters;
277}
278
Dave Airlieeddca552007-07-11 16:09:54 +1000279static void i810_kernel_lost_context(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280{
281 drm_i810_private_t *dev_priv = dev->dev_private;
282 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
283
284 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
285 ring->tail = I810_READ(LP_RING + RING_TAIL);
286 ring->space = ring->head - (ring->tail + 8);
287 if (ring->space < 0)
288 ring->space += ring->Size;
289}
290
Dave Airlieeddca552007-07-11 16:09:54 +1000291static int i810_freelist_init(struct drm_device * dev, drm_i810_private_t * dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000292{
Dave Airliecdd55a22007-07-11 16:32:08 +1000293 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294 int my_idx = 24;
295 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
296 int i;
297
298 if (dma->buf_count > 1019) {
299 /* Not enough space in the status page for the freelist */
300 return -EINVAL;
301 }
302
303 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000304 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
306
307 buf_priv->in_use = hw_status++;
308 buf_priv->my_use_idx = my_idx;
309 my_idx += 4;
310
311 *buf_priv->in_use = I810_BUF_FREE;
312
Dave Airlieb9094d32007-01-08 21:31:13 +1100313 buf_priv->map.offset = buf->bus_address;
314 buf_priv->map.size = buf->total;
315 buf_priv->map.type = _DRM_AGP;
316 buf_priv->map.flags = 0;
317 buf_priv->map.mtrr = 0;
318
319 drm_core_ioremap(&buf_priv->map, dev);
320 buf_priv->kernel_virtual = buf_priv->map.handle;
321
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000322 }
323 return 0;
324}
325
Dave Airlieeddca552007-07-11 16:09:54 +1000326static int i810_dma_initialize(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 drm_i810_private_t * dev_priv,
328 drm_i810_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Dave Airliebd1b3312007-05-26 05:01:51 +1000330 drm_map_list_t *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Dave Airliebd1b3312007-05-26 05:01:51 +1000333 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (r_list->map &&
335 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 break;
339 }
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!dev_priv->sarea_map) {
342 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 i810_dma_cleanup(dev);
344 DRM_ERROR("can not find sarea!\n");
345 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
348 if (!dev_priv->mmio_map) {
349 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 i810_dma_cleanup(dev);
351 DRM_ERROR("can not find mmio map!\n");
352 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000354 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
356 if (!dev->agp_buffer_map) {
357 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 i810_dma_cleanup(dev);
359 DRM_ERROR("can not find dma buffer map!\n");
360 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
363 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000366 dev_priv->ring.Start = init->ring_start;
367 dev_priv->ring.End = init->ring_end;
368 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Dave Airlieb9094d32007-01-08 21:31:13 +1100370 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
371 dev_priv->ring.map.size = init->ring_size;
372 dev_priv->ring.map.type = _DRM_AGP;
373 dev_priv->ring.map.flags = 0;
374 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dave Airlieb9094d32007-01-08 21:31:13 +1100376 drm_core_ioremap(&dev_priv->ring.map, dev);
377
378 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000379 dev->dev_private = (void *)dev_priv;
380 i810_dma_cleanup(dev);
381 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 " ring buffer\n");
Dave Airlieb9094d32007-01-08 21:31:13 +1100383 return DRM_ERR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
Dave Airlieb9094d32007-01-08 21:31:13 +1100386 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
387
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 dev_priv->w = init->w;
391 dev_priv->h = init->h;
392 dev_priv->pitch = init->pitch;
393 dev_priv->back_offset = init->back_offset;
394 dev_priv->depth_offset = init->depth_offset;
395 dev_priv->front_offset = init->front_offset;
396
397 dev_priv->overlay_offset = init->overlay_offset;
398 dev_priv->overlay_physical = init->overlay_physical;
399
400 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
401 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
402 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
403
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000404 /* Program Hardware Status Page */
405 dev_priv->hw_status_page =
406 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
407 &dev_priv->dma_status_page);
408 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 dev->dev_private = (void *)dev_priv;
410 i810_dma_cleanup(dev);
411 DRM_ERROR("Can not allocate hardware status page\n");
412 return -ENOMEM;
413 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
415 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000420 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (i810_freelist_init(dev, dev_priv) != 0) {
422 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000423 i810_dma_cleanup(dev);
424 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 dev->dev_private = (void *)dev_priv;
429
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433/* i810 DRM version 1.1 used a smaller init structure with different
434 * ordering of values than is currently used (drm >= 1.2). There is
435 * no defined way to detect the XFree version to correct this problem,
436 * however by checking using this procedure we can detect the correct
437 * thing to do.
438 *
439 * #1 Read the Smaller init structure from user-space
440 * #2 Verify the overlay_physical is a valid physical address, or NULL
441 * If it isn't then we have a v1.1 client. Fix up params.
442 * If it is, then we have a 1.2 client... get the rest of the data.
443 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444static int i810_dma_init_compat(drm_i810_init_t * init, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446
447 /* Get v1.1 init data */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 if (copy_from_user(init, (drm_i810_pre12_init_t __user *) arg,
449 sizeof(drm_i810_pre12_init_t))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return -EFAULT;
451 }
452
453 if ((!init->overlay_physical) || (init->overlay_physical > 4096)) {
454
455 /* This is a v1.2 client, just get the v1.2 init data */
456 DRM_INFO("Using POST v1.2 init.\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000457 if (copy_from_user(init, (drm_i810_init_t __user *) arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 sizeof(drm_i810_init_t))) {
459 return -EFAULT;
460 }
461 } else {
462
463 /* This is a v1.1 client, fix the params */
464 DRM_INFO("Using PRE v1.2 init.\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000465 init->pitch_bits = init->h;
466 init->pitch = init->w;
467 init->h = init->overlay_physical;
468 init->w = init->overlay_offset;
469 init->overlay_physical = 0;
470 init->overlay_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 return 0;
474}
475
476static int i810_dma_init(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Dave Airlieeddca552007-07-11 16:09:54 +1000479 struct drm_file *priv = filp->private_data;
480 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 drm_i810_private_t *dev_priv;
482 drm_i810_init_t init;
483 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* Get only the init func */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000486 if (copy_from_user
487 (&init, (void __user *)arg, sizeof(drm_i810_init_func_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -EFAULT;
489
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 switch (init.func) {
491 case I810_INIT_DMA:
492 /* This case is for backward compatibility. It
493 * handles XFree 4.1.0 and 4.2.0, and has to
494 * do some parameter checking as described below.
495 * It will someday go away.
496 */
497 retcode = i810_dma_init_compat(&init, arg);
498 if (retcode)
499 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000501 dev_priv = drm_alloc(sizeof(drm_i810_private_t),
502 DRM_MEM_DRIVER);
503 if (dev_priv == NULL)
504 return -ENOMEM;
505 retcode = i810_dma_initialize(dev, dev_priv, &init);
506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000508 default:
509 case I810_INIT_DMA_1_4:
510 DRM_INFO("Using v1.4 init.\n");
511 if (copy_from_user(&init, (drm_i810_init_t __user *) arg,
512 sizeof(drm_i810_init_t))) {
513 return -EFAULT;
514 }
515 dev_priv = drm_alloc(sizeof(drm_i810_private_t),
516 DRM_MEM_DRIVER);
517 if (dev_priv == NULL)
518 return -ENOMEM;
519 retcode = i810_dma_initialize(dev, dev_priv, &init);
520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000522 case I810_CLEANUP_DMA:
523 DRM_INFO("DMA Cleanup\n");
524 retcode = i810_dma_cleanup(dev);
525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000528 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/* Most efficient way to verify state for the i810 is as it is
532 * emitted. Non-conformant state is silently dropped.
533 *
534 * Use 'volatile' & local var tmp to force the emitted values to be
535 * identical to the verified ones.
536 */
Dave Airlieeddca552007-07-11 16:09:54 +1000537static void i810EmitContextVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int i, j = 0;
542 unsigned int tmp;
543 RING_LOCALS;
544
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000547 OUT_RING(GFX_OP_COLOR_FACTOR);
548 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 OUT_RING(GFX_OP_STIPPLE);
551 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 tmp = code[i];
555
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000556 if ((tmp & (7 << 29)) == (3 << 29) &&
557 (tmp & (0x1f << 24)) < (0x1d << 24)) {
558 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 } else
561 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 ADVANCE_LP_RING();
568}
569
Dave Airlieeddca552007-07-11 16:09:54 +1000570static void i810EmitTexVerified(struct drm_device * dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int i, j = 0;
574 unsigned int tmp;
575 RING_LOCALS;
576
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 OUT_RING(GFX_OP_MAP_INFO);
580 OUT_RING(code[I810_TEXREG_MI1]);
581 OUT_RING(code[I810_TEXREG_MI2]);
582 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000584 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 tmp = code[i];
586
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587 if ((tmp & (7 << 29)) == (3 << 29) &&
588 (tmp & (0x1f << 24)) < (0x1d << 24)) {
589 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 } else
592 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
595 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 ADVANCE_LP_RING();
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/* Need to do some additional checking when setting the dest buffer.
602 */
Dave Airlieeddca552007-07-11 16:09:54 +1000603static void i810EmitDestVerified(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000604 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 unsigned int tmp;
608 RING_LOCALS;
609
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000610 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 tmp = code[I810_DESTREG_DI1];
613 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 OUT_RING(CMD_OP_DESTBUFFER_INFO);
615 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
618 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /* invarient:
621 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 OUT_RING(CMD_OP_Z_BUFFER_INFO);
623 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 OUT_RING(GFX_OP_DESTBUFFER_VARS);
626 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000628 OUT_RING(GFX_OP_DRAWRECT_INFO);
629 OUT_RING(code[I810_DESTREG_DR1]);
630 OUT_RING(code[I810_DESTREG_DR2]);
631 OUT_RING(code[I810_DESTREG_DR3]);
632 OUT_RING(code[I810_DESTREG_DR4]);
633 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 ADVANCE_LP_RING();
636}
637
Dave Airlieeddca552007-07-11 16:09:54 +1000638static void i810EmitState(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 drm_i810_private_t *dev_priv = dev->dev_private;
641 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 DRM_DEBUG("%s %x\n", __FUNCTION__, dirty);
645
646 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000647 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
649 }
650
651 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
654 }
655
656 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
659 }
660
661 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000662 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
664 }
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/* need to verify
668 */
Dave Airlieeddca552007-07-11 16:09:54 +1000669static void i810_dma_dispatch_clear(struct drm_device * dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670 unsigned int clear_color,
671 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 drm_i810_private_t *dev_priv = dev->dev_private;
674 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000676 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int pitch = dev_priv->pitch;
678 int cpp = 2;
679 int i;
680 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000681
682 if (dev_priv->current_page == 1) {
683 unsigned int tmp = flags;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 if (tmp & I810_FRONT)
687 flags |= I810_BACK;
688 if (tmp & I810_BACK)
689 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 if (nbox > I810_NR_SAREA_CLIPRECTS)
695 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 unsigned int x = pbox->x1;
699 unsigned int y = pbox->y1;
700 unsigned int width = (pbox->x2 - x) * cpp;
701 unsigned int height = pbox->y2 - y;
702 unsigned int start = y * pitch + x * cpp;
703
704 if (pbox->x1 > pbox->x2 ||
705 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 continue;
708
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 if (flags & I810_FRONT) {
710 BEGIN_LP_RING(6);
711 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
712 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
713 OUT_RING((height << 16) | width);
714 OUT_RING(start);
715 OUT_RING(clear_color);
716 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ADVANCE_LP_RING();
718 }
719
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 if (flags & I810_BACK) {
721 BEGIN_LP_RING(6);
722 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
723 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
724 OUT_RING((height << 16) | width);
725 OUT_RING(dev_priv->back_offset + start);
726 OUT_RING(clear_color);
727 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 ADVANCE_LP_RING();
729 }
730
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 if (flags & I810_DEPTH) {
732 BEGIN_LP_RING(6);
733 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
734 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
735 OUT_RING((height << 16) | width);
736 OUT_RING(dev_priv->depth_offset + start);
737 OUT_RING(clear_zval);
738 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ADVANCE_LP_RING();
740 }
741 }
742}
743
Dave Airlieeddca552007-07-11 16:09:54 +1000744static void i810_dma_dispatch_swap(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 drm_i810_private_t *dev_priv = dev->dev_private;
747 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000749 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 int pitch = dev_priv->pitch;
751 int cpp = 2;
752 int i;
753 RING_LOCALS;
754
755 DRM_DEBUG("swapbuffers\n");
756
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 if (nbox > I810_NR_SAREA_CLIPRECTS)
760 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000762 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 unsigned int w = pbox->x2 - pbox->x1;
764 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 unsigned int start = dst;
767
768 if (pbox->x1 > pbox->x2 ||
769 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 continue;
772
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000773 BEGIN_LP_RING(6);
774 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
775 OUT_RING(pitch | (0xCC << 16));
776 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 OUT_RING(dev_priv->back_offset + start);
781 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000785 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 ADVANCE_LP_RING();
787 }
788}
789
Dave Airlieeddca552007-07-11 16:09:54 +1000790static void i810_dma_dispatch_vertex(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +1000791 struct drm_buf * buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000796 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 unsigned long address = (unsigned long)buf->bus_address;
799 unsigned long start = address - dev->agp->base;
800 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 nbox = I810_NR_SAREA_CLIPRECTS;
807
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 used = 0;
810
811 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
815 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
816
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 *(u32 *) buf_priv->kernel_virtual =
818 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000821 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 used += 4;
823 }
824
825 i810_unmap_buffer(buf);
826 }
827
828 if (used) {
829 do {
830 if (i < nbox) {
831 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
833 SC_ENABLE);
834 OUT_RING(GFX_OP_SCISSOR_INFO);
835 OUT_RING(box[i].x1 | (box[i].y1 << 16));
836 OUT_RING((box[i].x2 -
837 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 ADVANCE_LP_RING();
839 }
840
841 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 OUT_RING(CMD_OP_BATCH_BUFFER);
843 OUT_RING(start | BB1_PROTECTED);
844 OUT_RING(start + used - 4);
845 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 ADVANCE_LP_RING();
847
848 } while (++i < nbox);
849 }
850
851 if (discard) {
852 dev_priv->counter++;
853
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
855 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 OUT_RING(CMD_STORE_DWORD_IDX);
859 OUT_RING(20);
860 OUT_RING(dev_priv->counter);
861 OUT_RING(CMD_STORE_DWORD_IDX);
862 OUT_RING(buf_priv->my_use_idx);
863 OUT_RING(I810_BUF_FREE);
864 OUT_RING(CMD_REPORT_HEAD);
865 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 ADVANCE_LP_RING();
867 }
868}
869
Dave Airlieeddca552007-07-11 16:09:54 +1000870static void i810_dma_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000872 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int pitch = dev_priv->pitch;
874 RING_LOCALS;
875
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
877 __FUNCTION__,
878 dev_priv->current_page,
879 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000881 i810_kernel_lost_context(dev);
882
883 BEGIN_LP_RING(2);
884 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
885 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 ADVANCE_LP_RING();
887
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* On i815 at least ASYNC is buggy */
890 /* pitch<<5 is from 11.2.8 p158,
891 its the pitch / 8 then left shifted 8,
892 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
894 if (dev_priv->current_page == 0) {
895 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 dev_priv->current_page = 1;
897 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 dev_priv->current_page = 0;
900 }
901 OUT_RING(0);
902 ADVANCE_LP_RING();
903
904 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
906 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ADVANCE_LP_RING();
908
909 /* Increment the frame counter. The client-side 3D driver must
910 * throttle the framerate by waiting for this value before
911 * performing the swapbuffer ioctl.
912 */
913 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
914
915}
916
Dave Airlieeddca552007-07-11 16:09:54 +1000917static void i810_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 drm_i810_private_t *dev_priv = dev->dev_private;
920 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922/* printk("%s\n", __FUNCTION__); */
923
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 BEGIN_LP_RING(4);
927 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
928 OUT_RING(CMD_REPORT_HEAD);
929 OUT_RING(0);
930 OUT_RING(0);
931 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000933 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Dave Airlieeddca552007-07-11 16:09:54 +1000936static int i810_flush_queue(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000939 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000940 int i, ret = 0;
941 RING_LOCALS;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943/* printk("%s\n", __FUNCTION__); */
944
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000945 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 BEGIN_LP_RING(2);
948 OUT_RING(CMD_REPORT_HEAD);
949 OUT_RING(0);
950 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000954 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000955 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000956 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
959 I810_BUF_FREE);
960
961 if (used == I810_BUF_HARDWARE)
962 DRM_DEBUG("reclaimed from HARDWARE\n");
963 if (used == I810_BUF_CLIENT)
964 DRM_DEBUG("still on client\n");
965 }
966
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970/* Must be called with the lock held */
Dave Airlieeddca552007-07-11 16:09:54 +1000971static void i810_reclaim_buffers(struct drm_device * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Dave Airliecdd55a22007-07-11 16:32:08 +1000973 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000976 if (!dma)
977 return;
978 if (!dev->dev_private)
979 return;
980 if (!dma->buflist)
981 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000983 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000986 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 if (buf->filp == filp && buf_priv) {
990 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
991 I810_BUF_FREE);
992
993 if (used == I810_BUF_CLIENT)
994 DRM_DEBUG("reclaimed from client\n");
995 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998 }
999}
1000
Dave Airliec94f7022005-07-07 21:03:38 +10001001static int i810_flush_ioctl(struct inode *inode, struct file *filp,
1002 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Dave Airlieeddca552007-07-11 16:09:54 +10001004 struct drm_file *priv = filp->private_data;
1005 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 LOCK_TEST_WITH_RETURN(dev, filp);
1008
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 i810_flush_queue(dev);
1010 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013static int i810_dma_vertex(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Dave Airlieeddca552007-07-11 16:09:54 +10001016 struct drm_file *priv = filp->private_data;
1017 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001018 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1020 u32 *hw_status = dev_priv->hw_status_page;
1021 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1022 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 drm_i810_vertex_t vertex;
1024
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 if (copy_from_user
1026 (&vertex, (drm_i810_vertex_t __user *) arg, sizeof(vertex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EFAULT;
1028
1029 LOCK_TEST_WITH_RETURN(dev, filp);
1030
1031 DRM_DEBUG("i810 dma vertex, idx %d used %d discard %d\n",
1032 vertex.idx, vertex.used, vertex.discard);
1033
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 if (vertex.idx < 0 || vertex.idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return -EINVAL;
1036
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 i810_dma_dispatch_vertex(dev,
1038 dma->buflist[vertex.idx],
1039 vertex.discard, vertex.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 atomic_add(vertex.used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 sarea_priv->last_enqueue = dev_priv->counter - 1;
1044 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 return 0;
1047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049static int i810_clear_bufs(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Dave Airlieeddca552007-07-11 16:09:54 +10001052 struct drm_file *priv = filp->private_data;
1053 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 drm_i810_clear_t clear;
1055
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001056 if (copy_from_user
1057 (&clear, (drm_i810_clear_t __user *) arg, sizeof(clear)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -EFAULT;
1059
1060 LOCK_TEST_WITH_RETURN(dev, filp);
1061
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 /* GH: Someone's doing nasty things... */
1063 if (!dev->dev_private) {
1064 return -EINVAL;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001067 i810_dma_dispatch_clear(dev, clear.flags,
1068 clear.clear_color, clear.clear_depth);
1069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072static int i810_swap_bufs(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Dave Airlieeddca552007-07-11 16:09:54 +10001075 struct drm_file *priv = filp->private_data;
1076 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 DRM_DEBUG("i810_swap_bufs\n");
1079
1080 LOCK_TEST_WITH_RETURN(dev, filp);
1081
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 i810_dma_dispatch_swap(dev);
1083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
1086static int i810_getage(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Dave Airlieeddca552007-07-11 16:09:54 +10001089 struct drm_file *priv = filp->private_data;
1090 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1092 u32 *hw_status = dev_priv->hw_status_page;
1093 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1094 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098}
1099
1100static int i810_getbuf(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Dave Airlieeddca552007-07-11 16:09:54 +10001103 struct drm_file *priv = filp->private_data;
1104 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 int retcode = 0;
1106 drm_i810_dma_t d;
1107 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1108 u32 *hw_status = dev_priv->hw_status_page;
1109 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1110 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 if (copy_from_user(&d, (drm_i810_dma_t __user *) arg, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return -EFAULT;
1114
1115 LOCK_TEST_WITH_RETURN(dev, filp);
1116
1117 d.granted = 0;
1118
1119 retcode = i810_dma_get_buffer(dev, &d, filp);
1120
1121 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
1122 current->pid, retcode, d.granted);
1123
Dave Airlieeddca552007-07-11 16:09:54 +10001124 if (copy_to_user((void __user *) arg, &d, sizeof(d)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001126 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 return retcode;
1129}
1130
1131static int i810_copybuf(struct inode *inode,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 /* Never copy - 2.4.x doesn't need it */
1135 return 0;
1136}
1137
1138static int i810_docopy(struct inode *inode, struct file *filp, unsigned int cmd,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001139 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 /* Never copy - 2.4.x doesn't need it */
1142 return 0;
1143}
1144
Dave Airlie056219e2007-07-11 16:17:42 +10001145static void i810_dma_dispatch_mc(struct drm_device * dev, struct drm_buf * buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001146 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 drm_i810_private_t *dev_priv = dev->dev_private;
1149 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1150 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1151 unsigned long address = (unsigned long)buf->bus_address;
1152 unsigned long start = address - dev->agp->base;
1153 int u;
1154 RING_LOCALS;
1155
1156 i810_kernel_lost_context(dev);
1157
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001158 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (u != I810_BUF_CLIENT) {
1160 DRM_DEBUG("MC found buffer that isn't mine!\n");
1161 }
1162
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 used = 0;
1165
1166 sarea_priv->dirty = 0x7f;
1167
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001168 DRM_DEBUG("dispatch mc addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 dev_priv->counter++;
1171 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
1172 DRM_DEBUG("i810_dma_dispatch_mc\n");
1173 DRM_DEBUG("start : %lx\n", start);
1174 DRM_DEBUG("used : %d\n", used);
1175 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1176
1177 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1178 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001179 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 used += 4;
1181 }
1182
1183 i810_unmap_buffer(buf);
1184 }
1185 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001186 OUT_RING(CMD_OP_BATCH_BUFFER);
1187 OUT_RING(start | BB1_PROTECTED);
1188 OUT_RING(start + used - 4);
1189 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ADVANCE_LP_RING();
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001193 OUT_RING(CMD_STORE_DWORD_IDX);
1194 OUT_RING(buf_priv->my_use_idx);
1195 OUT_RING(I810_BUF_FREE);
1196 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001198 OUT_RING(CMD_STORE_DWORD_IDX);
1199 OUT_RING(16);
1200 OUT_RING(last_render);
1201 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 ADVANCE_LP_RING();
1203}
1204
1205static int i810_dma_mc(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001206 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Dave Airlieeddca552007-07-11 16:09:54 +10001208 struct drm_file *priv = filp->private_data;
1209 struct drm_device *dev = priv->head->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +10001210 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001211 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 u32 *hw_status = dev_priv->hw_status_page;
1213 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001214 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 drm_i810_mc_t mc;
1216
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001217 if (copy_from_user(&mc, (drm_i810_mc_t __user *) arg, sizeof(mc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return -EFAULT;
1219
1220 LOCK_TEST_WITH_RETURN(dev, filp);
1221
1222 if (mc.idx >= dma->buf_count || mc.idx < 0)
1223 return -EINVAL;
1224
1225 i810_dma_dispatch_mc(dev, dma->buflist[mc.idx], mc.used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001226 mc.last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 atomic_add(mc.used, &dev->counts[_DRM_STAT_SECONDARY]);
1229 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001230 sarea_priv->last_enqueue = dev_priv->counter - 1;
1231 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 return 0;
1234}
1235
1236static int i810_rstatus(struct inode *inode, struct file *filp,
1237 unsigned int cmd, unsigned long arg)
1238{
Dave Airlieeddca552007-07-11 16:09:54 +10001239 struct drm_file *priv = filp->private_data;
1240 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001241 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001243 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246static int i810_ov0_info(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001247 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Dave Airlieeddca552007-07-11 16:09:54 +10001249 struct drm_file *priv = filp->private_data;
1250 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001251 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 drm_i810_overlay_t data;
1253
1254 data.offset = dev_priv->overlay_offset;
1255 data.physical = dev_priv->overlay_physical;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001256 if (copy_to_user
1257 ((drm_i810_overlay_t __user *) arg, &data, sizeof(data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -EFAULT;
1259 return 0;
1260}
1261
1262static int i810_fstatus(struct inode *inode, struct file *filp,
1263 unsigned int cmd, unsigned long arg)
1264{
Dave Airlieeddca552007-07-11 16:09:54 +10001265 struct drm_file *priv = filp->private_data;
1266 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001267 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 LOCK_TEST_WITH_RETURN(dev, filp);
1270
1271 return I810_READ(0x30008);
1272}
1273
1274static int i810_ov0_flip(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001275 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Dave Airlieeddca552007-07-11 16:09:54 +10001277 struct drm_file *priv = filp->private_data;
1278 struct drm_device *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001279 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 LOCK_TEST_WITH_RETURN(dev, filp);
1282
1283 //Tell the overlay to update
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001284 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 return 0;
1287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001290 */
Dave Airlieeddca552007-07-11 16:09:54 +10001291static void i810_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 DRM_DEBUG("%s\n", __FUNCTION__);
1296 dev_priv->page_flipping = 1;
1297 dev_priv->current_page = 0;
1298 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1299}
1300
Dave Airlieeddca552007-07-11 16:09:54 +10001301static int i810_do_cleanup_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 drm_i810_private_t *dev_priv = dev->dev_private;
1304
1305 DRM_DEBUG("%s\n", __FUNCTION__);
1306 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001307 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 dev_priv->page_flipping = 0;
1310 return 0;
1311}
1312
1313static int i810_flip_bufs(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001314 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Dave Airlieeddca552007-07-11 16:09:54 +10001316 struct drm_file *priv = filp->private_data;
1317 struct drm_device *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 drm_i810_private_t *dev_priv = dev->dev_private;
1319
1320 DRM_DEBUG("%s\n", __FUNCTION__);
1321
1322 LOCK_TEST_WITH_RETURN(dev, filp);
1323
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001324 if (!dev_priv->page_flipping)
1325 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001327 i810_dma_dispatch_flip(dev);
1328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Dave Airlieeddca552007-07-11 16:09:54 +10001331int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001332{
1333 /* i810 has 4 more counters */
1334 dev->counters += 4;
1335 dev->types[6] = _DRM_STAT_IRQ;
1336 dev->types[7] = _DRM_STAT_PRIMARY;
1337 dev->types[8] = _DRM_STAT_SECONDARY;
1338 dev->types[9] = _DRM_STAT_DMA;
1339
1340 return 0;
1341}
1342
Dave Airlieeddca552007-07-11 16:09:54 +10001343void i810_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001345 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Dave Airlieeddca552007-07-11 16:09:54 +10001348void i810_driver_preclose(struct drm_device * dev, DRMFILE filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 if (dev->dev_private) {
1351 drm_i810_private_t *dev_priv = dev->dev_private;
1352 if (dev_priv->page_flipping) {
1353 i810_do_cleanup_pageflip(dev);
1354 }
1355 }
1356}
1357
Dave Airlieeddca552007-07-11 16:09:54 +10001358void i810_driver_reclaim_buffers_locked(struct drm_device * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 i810_reclaim_buffers(dev, filp);
1361}
1362
Dave Airlieeddca552007-07-11 16:09:54 +10001363int i810_driver_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 0;
1367}
1368
1369drm_ioctl_desc_t i810_ioctls[] = {
Dave Airliea7a2cc32006-01-02 13:54:04 +11001370 [DRM_IOCTL_NR(DRM_I810_INIT)] = {i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1371 [DRM_IOCTL_NR(DRM_I810_VERTEX)] = {i810_dma_vertex, DRM_AUTH},
1372 [DRM_IOCTL_NR(DRM_I810_CLEAR)] = {i810_clear_bufs, DRM_AUTH},
1373 [DRM_IOCTL_NR(DRM_I810_FLUSH)] = {i810_flush_ioctl, DRM_AUTH},
1374 [DRM_IOCTL_NR(DRM_I810_GETAGE)] = {i810_getage, DRM_AUTH},
1375 [DRM_IOCTL_NR(DRM_I810_GETBUF)] = {i810_getbuf, DRM_AUTH},
1376 [DRM_IOCTL_NR(DRM_I810_SWAP)] = {i810_swap_bufs, DRM_AUTH},
1377 [DRM_IOCTL_NR(DRM_I810_COPY)] = {i810_copybuf, DRM_AUTH},
1378 [DRM_IOCTL_NR(DRM_I810_DOCOPY)] = {i810_docopy, DRM_AUTH},
1379 [DRM_IOCTL_NR(DRM_I810_OV0INFO)] = {i810_ov0_info, DRM_AUTH},
1380 [DRM_IOCTL_NR(DRM_I810_FSTATUS)] = {i810_fstatus, DRM_AUTH},
1381 [DRM_IOCTL_NR(DRM_I810_OV0FLIP)] = {i810_ov0_flip, DRM_AUTH},
1382 [DRM_IOCTL_NR(DRM_I810_MC)] = {i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1383 [DRM_IOCTL_NR(DRM_I810_RSTATUS)] = {i810_rstatus, DRM_AUTH},
1384 [DRM_IOCTL_NR(DRM_I810_FLIP)] = {i810_flip_bufs, DRM_AUTH}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385};
1386
1387int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001388
1389/**
1390 * Determine if the device really is AGP or not.
1391 *
1392 * All Intel graphics chipsets are treated as AGP, even if they are really
1393 * PCI-e.
1394 *
1395 * \param dev The device to be tested.
1396 *
1397 * \returns
1398 * A value of 1 is always retured to indictate every i810 is AGP.
1399 */
Dave Airlieeddca552007-07-11 16:09:54 +10001400int i810_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001401{
1402 return 1;
1403}