blob: 09c86ed89927a82a8b384fe9c095252960350a8f [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/pagemap.h>
41
42#define I810_BUF_FREE 2
43#define I810_BUF_CLIENT 1
Dave Airliebc5f4522007-11-05 12:50:58 +100044#define I810_BUF_HARDWARE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define I810_BUF_UNMAPPED 0
47#define I810_BUF_MAPPED 1
48
Dave Airlie056219e2007-07-11 16:17:42 +100049static struct drm_buf *i810_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Dave Airliecdd55a22007-07-11 16:32:08 +100051 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100052 int i;
53 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 /* Linear search might not be the best solution */
56
Dave Airlieb5e89ed2005-09-25 14:28:13 +100057 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +100058 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100059 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100061 used = cmpxchg(buf_priv->in_use, I810_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 I810_BUF_CLIENT);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +020063 if (used == I810_BUF_FREE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
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
Nicolas Kaiseraca791c2010-07-14 21:54:13 +020073static 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 Airlie2c14f282008-04-21 16:47:32 +100097 dev = priv->minor->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,
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000118 .unlocked_ioctl = drm_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000119 .mmap = i810_mmap_buffers,
120 .fasync = drm_fasync,
Dave Airliec94f7022005-07-07 21:03:38 +1000121};
122
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200123static int i810_map_buffer(struct drm_buf *buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Dave Airlie2c14f282008-04-21 16:47:32 +1000125 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800128 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int retcode = 0;
130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -EINVAL;
133
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000134 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000135 old_fops = file_priv->filp->f_op;
136 file_priv->filp->f_op = &i810_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 dev_priv->mmap_buffer = buf;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000138 buf_priv->virtual = (void *)do_mmap(file_priv->filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139 PROT_READ | PROT_WRITE,
140 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000142 file_priv->filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000143 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* Real error */
145 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000146 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 buf_priv->virtual = NULL;
148 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000149 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return retcode;
152}
153
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200154static int i810_unmap_buffer(struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
157 int retcode = 0;
158
159 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
160 return -EINVAL;
161
162 down_write(&current->mm->mmap_sem);
163 retcode = do_munmap(current->mm,
164 (unsigned long)buf_priv->virtual,
165 (size_t) buf->total);
166 up_write(&current->mm->mmap_sem);
167
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
169 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return retcode;
172}
173
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200174static int i810_dma_get_buffer(struct drm_device *dev, drm_i810_dma_t *d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000175 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Dave Airlie056219e2007-07-11 16:17:42 +1000177 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 drm_i810_buf_priv_t *buf_priv;
179 int retcode = 0;
180
181 buf = i810_freelist_get(dev);
182 if (!buf) {
183 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return retcode;
186 }
187
Eric Anholt6c340ea2007-08-25 20:23:09 +1000188 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (retcode) {
190 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return retcode;
193 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000194 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 buf_priv = buf->dev_private;
196 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197 d->request_idx = buf->idx;
198 d->request_size = buf->total;
199 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 return retcode;
202}
203
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200204static int i810_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Dave Airliecdd55a22007-07-11 16:32:08 +1000206 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* Make sure interrupts are disabled here because the uninstall ioctl
209 * may not have been called from userspace and after dev_private
210 * is freed, it's too late.
211 */
212 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
213 drm_irq_uninstall(dev);
214
215 if (dev->dev_private) {
216 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 drm_i810_private_t *dev_priv =
218 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200220 if (dev_priv->ring.virtual_start)
Dave Airlieb9094d32007-01-08 21:31:13 +1100221 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222 if (dev_priv->hw_status_page) {
223 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 dev_priv->hw_status_page,
225 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 /* Need to rewrite hardware status page */
227 I810_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700229 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000230 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000233 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100235
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000236 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100237 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
241}
242
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200243static int i810_wait_ring(struct drm_device *dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244{
245 drm_i810_private_t *dev_priv = dev->dev_private;
246 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
247 int iters = 0;
248 unsigned long end;
249 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
250
251 end = jiffies + (HZ * 3);
252 while (ring->space < n) {
253 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
254 ring->space = ring->head - (ring->tail + 8);
255 if (ring->space < 0)
256 ring->space += ring->Size;
257
258 if (ring->head != last_head) {
259 end = jiffies + (HZ * 3);
260 last_head = ring->head;
261 }
262
263 iters++;
264 if (time_before(end, jiffies)) {
265 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
266 DRM_ERROR("lockup\n");
267 goto out_wait_ring;
268 }
269 udelay(1);
270 }
271
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200272out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000273 return iters;
274}
275
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200276static void i810_kernel_lost_context(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277{
278 drm_i810_private_t *dev_priv = dev->dev_private;
279 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
280
281 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
282 ring->tail = I810_READ(LP_RING + RING_TAIL);
283 ring->space = ring->head - (ring->tail + 8);
284 if (ring->space < 0)
285 ring->space += ring->Size;
286}
287
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200288static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289{
Dave Airliecdd55a22007-07-11 16:32:08 +1000290 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291 int my_idx = 24;
292 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
293 int i;
294
295 if (dma->buf_count > 1019) {
296 /* Not enough space in the status page for the freelist */
297 return -EINVAL;
298 }
299
300 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000301 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000302 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
303
304 buf_priv->in_use = hw_status++;
305 buf_priv->my_use_idx = my_idx;
306 my_idx += 4;
307
308 *buf_priv->in_use = I810_BUF_FREE;
309
Dave Airlieb9094d32007-01-08 21:31:13 +1100310 buf_priv->map.offset = buf->bus_address;
311 buf_priv->map.size = buf->total;
312 buf_priv->map.type = _DRM_AGP;
313 buf_priv->map.flags = 0;
314 buf_priv->map.mtrr = 0;
315
316 drm_core_ioremap(&buf_priv->map, dev);
317 buf_priv->kernel_virtual = buf_priv->map.handle;
318
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000319 }
320 return 0;
321}
322
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200323static int i810_dma_initialize(struct drm_device *dev,
324 drm_i810_private_t *dev_priv,
325 drm_i810_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Dave Airlie55910512007-07-11 16:53:40 +1000327 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000328 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Dave Airliebd1b3312007-05-26 05:01:51 +1000330 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (r_list->map &&
332 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 break;
336 }
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!dev_priv->sarea_map) {
339 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000340 i810_dma_cleanup(dev);
341 DRM_ERROR("can not find sarea!\n");
342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
345 if (!dev_priv->mmio_map) {
346 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 i810_dma_cleanup(dev);
348 DRM_ERROR("can not find mmio map!\n");
349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000351 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
353 if (!dev->agp_buffer_map) {
354 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 i810_dma_cleanup(dev);
356 DRM_ERROR("can not find dma buffer map!\n");
357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000363 dev_priv->ring.Start = init->ring_start;
364 dev_priv->ring.End = init->ring_end;
365 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Dave Airlieb9094d32007-01-08 21:31:13 +1100367 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
368 dev_priv->ring.map.size = init->ring_size;
369 dev_priv->ring.map.type = _DRM_AGP;
370 dev_priv->ring.map.flags = 0;
371 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Dave Airlieb9094d32007-01-08 21:31:13 +1100373 drm_core_ioremap(&dev_priv->ring.map, dev);
374
375 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 dev->dev_private = (void *)dev_priv;
377 i810_dma_cleanup(dev);
378 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000380 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
Dave Airlieb9094d32007-01-08 21:31:13 +1100383 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
384
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000385 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 dev_priv->w = init->w;
388 dev_priv->h = init->h;
389 dev_priv->pitch = init->pitch;
390 dev_priv->back_offset = init->back_offset;
391 dev_priv->depth_offset = init->depth_offset;
392 dev_priv->front_offset = init->front_offset;
393
394 dev_priv->overlay_offset = init->overlay_offset;
395 dev_priv->overlay_physical = init->overlay_physical;
396
397 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
398 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
399 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
400
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000401 /* Program Hardware Status Page */
402 dev_priv->hw_status_page =
403 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
404 &dev_priv->dma_status_page);
405 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 dev->dev_private = (void *)dev_priv;
407 i810_dma_cleanup(dev);
408 DRM_ERROR("Can not allocate hardware status page\n");
409 return -ENOMEM;
410 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
412 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000415 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (i810_freelist_init(dev, dev_priv) != 0) {
419 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000420 i810_dma_cleanup(dev);
421 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000423 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 dev->dev_private = (void *)dev_priv;
426
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Eric Anholtc153f452007-09-03 12:06:45 +1000430static int i810_dma_init(struct drm_device *dev, void *data,
431 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000433 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000434 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Eric Anholtc153f452007-09-03 12:06:45 +1000437 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438 case I810_INIT_DMA_1_4:
439 DRM_INFO("Using v1.4 init.\n");
Eric Anholt9a298b22009-03-24 12:23:04 -0700440 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 if (dev_priv == NULL)
442 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000443 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 case I810_CLEANUP_DMA:
447 DRM_INFO("DMA Cleanup\n");
448 retcode = i810_dma_cleanup(dev);
449 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000450 default:
451 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000454 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/* Most efficient way to verify state for the i810 is as it is
458 * emitted. Non-conformant state is silently dropped.
459 *
460 * Use 'volatile' & local var tmp to force the emitted values to be
461 * identical to the verified ones.
462 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200463static void i810EmitContextVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000464 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int i, j = 0;
468 unsigned int tmp;
469 RING_LOCALS;
470
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000471 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 OUT_RING(GFX_OP_COLOR_FACTOR);
474 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 OUT_RING(GFX_OP_STIPPLE);
477 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000479 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 tmp = code[i];
481
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482 if ((tmp & (7 << 29)) == (3 << 29) &&
483 (tmp & (0x1f << 24)) < (0x1d << 24)) {
484 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000486 } else
487 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000491 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 ADVANCE_LP_RING();
494}
495
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200496static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000498 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int i, j = 0;
500 unsigned int tmp;
501 RING_LOCALS;
502
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000503 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505 OUT_RING(GFX_OP_MAP_INFO);
506 OUT_RING(code[I810_TEXREG_MI1]);
507 OUT_RING(code[I810_TEXREG_MI2]);
508 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000510 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 tmp = code[i];
512
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000513 if ((tmp & (7 << 29)) == (3 << 29) &&
514 (tmp & (0x1f << 24)) < (0x1d << 24)) {
515 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000517 } else
518 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000522 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 ADVANCE_LP_RING();
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/* Need to do some additional checking when setting the dest buffer.
528 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200529static void i810EmitDestVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned int tmp;
534 RING_LOCALS;
535
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000536 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 tmp = code[I810_DESTREG_DI1];
539 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 OUT_RING(CMD_OP_DESTBUFFER_INFO);
541 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
544 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /* invarient:
547 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 OUT_RING(CMD_OP_Z_BUFFER_INFO);
549 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 OUT_RING(GFX_OP_DESTBUFFER_VARS);
552 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 OUT_RING(GFX_OP_DRAWRECT_INFO);
555 OUT_RING(code[I810_DESTREG_DR1]);
556 OUT_RING(code[I810_DESTREG_DR2]);
557 OUT_RING(code[I810_DESTREG_DR3]);
558 OUT_RING(code[I810_DESTREG_DR4]);
559 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 ADVANCE_LP_RING();
562}
563
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200564static void i810EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000566 drm_i810_private_t *dev_priv = dev->dev_private;
567 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000569
Márton Németh3e684ea2008-01-24 15:58:57 +1000570 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000573 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
575 }
576
577 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
580 }
581
582 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
585 }
586
587 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
590 }
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/* need to verify
594 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200595static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 unsigned int clear_color,
597 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000599 drm_i810_private_t *dev_priv = dev->dev_private;
600 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000602 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int pitch = dev_priv->pitch;
604 int cpp = 2;
605 int i;
606 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000607
608 if (dev_priv->current_page == 1) {
609 unsigned int tmp = flags;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000612 if (tmp & I810_FRONT)
613 flags |= I810_BACK;
614 if (tmp & I810_BACK)
615 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000618 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 if (nbox > I810_NR_SAREA_CLIPRECTS)
621 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 unsigned int x = pbox->x1;
625 unsigned int y = pbox->y1;
626 unsigned int width = (pbox->x2 - x) * cpp;
627 unsigned int height = pbox->y2 - y;
628 unsigned int start = y * pitch + x * cpp;
629
630 if (pbox->x1 > pbox->x2 ||
631 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000632 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 continue;
634
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 if (flags & I810_FRONT) {
636 BEGIN_LP_RING(6);
637 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
638 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
639 OUT_RING((height << 16) | width);
640 OUT_RING(start);
641 OUT_RING(clear_color);
642 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 ADVANCE_LP_RING();
644 }
645
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 if (flags & I810_BACK) {
647 BEGIN_LP_RING(6);
648 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
649 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
650 OUT_RING((height << 16) | width);
651 OUT_RING(dev_priv->back_offset + start);
652 OUT_RING(clear_color);
653 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 ADVANCE_LP_RING();
655 }
656
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657 if (flags & I810_DEPTH) {
658 BEGIN_LP_RING(6);
659 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
660 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
661 OUT_RING((height << 16) | width);
662 OUT_RING(dev_priv->depth_offset + start);
663 OUT_RING(clear_zval);
664 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 ADVANCE_LP_RING();
666 }
667 }
668}
669
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200670static void i810_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000672 drm_i810_private_t *dev_priv = dev->dev_private;
673 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000675 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int pitch = dev_priv->pitch;
677 int cpp = 2;
678 int i;
679 RING_LOCALS;
680
681 DRM_DEBUG("swapbuffers\n");
682
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685 if (nbox > I810_NR_SAREA_CLIPRECTS)
686 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned int w = pbox->x2 - pbox->x1;
690 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 unsigned int start = dst;
693
694 if (pbox->x1 > pbox->x2 ||
695 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 continue;
698
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 BEGIN_LP_RING(6);
700 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
701 OUT_RING(pitch | (0xCC << 16));
702 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 OUT_RING(dev_priv->back_offset + start);
707 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 ADVANCE_LP_RING();
713 }
714}
715
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200716static void i810_dma_dispatch_vertex(struct drm_device *dev,
717 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000722 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned long address = (unsigned long)buf->bus_address;
725 unsigned long start = address - dev->agp->base;
726 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000727 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 nbox = I810_NR_SAREA_CLIPRECTS;
733
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 used = 0;
736
737 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
741 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
742
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 *(u32 *) buf_priv->kernel_virtual =
744 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000747 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 used += 4;
749 }
750
751 i810_unmap_buffer(buf);
752 }
753
754 if (used) {
755 do {
756 if (i < nbox) {
757 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000758 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
759 SC_ENABLE);
760 OUT_RING(GFX_OP_SCISSOR_INFO);
761 OUT_RING(box[i].x1 | (box[i].y1 << 16));
762 OUT_RING((box[i].x2 -
763 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ADVANCE_LP_RING();
765 }
766
767 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000768 OUT_RING(CMD_OP_BATCH_BUFFER);
769 OUT_RING(start | BB1_PROTECTED);
770 OUT_RING(start + used - 4);
771 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 ADVANCE_LP_RING();
773
774 } while (++i < nbox);
775 }
776
777 if (discard) {
778 dev_priv->counter++;
779
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
781 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 OUT_RING(CMD_STORE_DWORD_IDX);
785 OUT_RING(20);
786 OUT_RING(dev_priv->counter);
787 OUT_RING(CMD_STORE_DWORD_IDX);
788 OUT_RING(buf_priv->my_use_idx);
789 OUT_RING(I810_BUF_FREE);
790 OUT_RING(CMD_REPORT_HEAD);
791 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ADVANCE_LP_RING();
793 }
794}
795
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200796static void i810_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int pitch = dev_priv->pitch;
800 RING_LOCALS;
801
Márton Németh3e684ea2008-01-24 15:58:57 +1000802 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 dev_priv->current_page,
804 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 i810_kernel_lost_context(dev);
807
808 BEGIN_LP_RING(2);
809 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
810 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 ADVANCE_LP_RING();
812
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* On i815 at least ASYNC is buggy */
815 /* pitch<<5 is from 11.2.8 p158,
816 its the pitch / 8 then left shifted 8,
817 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
819 if (dev_priv->current_page == 0) {
820 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 dev_priv->current_page = 1;
822 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 dev_priv->current_page = 0;
825 }
826 OUT_RING(0);
827 ADVANCE_LP_RING();
828
829 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
831 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 ADVANCE_LP_RING();
833
834 /* Increment the frame counter. The client-side 3D driver must
835 * throttle the framerate by waiting for this value before
836 * performing the swapbuffer ioctl.
837 */
838 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
839
840}
841
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200842static void i810_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 drm_i810_private_t *dev_priv = dev->dev_private;
845 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 BEGIN_LP_RING(4);
850 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
851 OUT_RING(CMD_REPORT_HEAD);
852 OUT_RING(0);
853 OUT_RING(0);
854 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200859static int i810_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000862 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 int i, ret = 0;
864 RING_LOCALS;
865
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868 BEGIN_LP_RING(2);
869 OUT_RING(CMD_REPORT_HEAD);
870 OUT_RING(0);
871 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000873 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000876 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
880 I810_BUF_FREE);
881
882 if (used == I810_BUF_HARDWARE)
883 DRM_DEBUG("reclaimed from HARDWARE\n");
884 if (used == I810_BUF_CLIENT)
885 DRM_DEBUG("still on client\n");
886 }
887
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891/* Must be called with the lock held */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200892static void i810_reclaim_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000893 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Dave Airliecdd55a22007-07-11 16:32:08 +1000895 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 if (!dma)
899 return;
900 if (!dev->dev_private)
901 return;
902 if (!dma->buflist)
903 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000908 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Eric Anholt6c340ea2007-08-25 20:23:09 +1000911 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
913 I810_BUF_FREE);
914
915 if (used == I810_BUF_CLIENT)
916 DRM_DEBUG("reclaimed from client\n");
917 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 }
921}
922
Eric Anholtc153f452007-09-03 12:06:45 +1000923static int i810_flush_ioctl(struct drm_device *dev, void *data,
924 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000926 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 i810_flush_queue(dev);
929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Eric Anholtc153f452007-09-03 12:06:45 +1000932static int i810_dma_vertex(struct drm_device *dev, void *data,
933 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Dave Airliecdd55a22007-07-11 16:32:08 +1000935 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
937 u32 *hw_status = dev_priv->hw_status_page;
938 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
939 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000940 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Eric Anholt6c340ea2007-08-25 20:23:09 +1000942 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Márton Németh3e684ea2008-01-24 15:58:57 +1000944 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000945 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Eric Anholtc153f452007-09-03 12:06:45 +1000947 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -EINVAL;
949
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000951 dma->buflist[vertex->idx],
952 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Eric Anholtc153f452007-09-03 12:06:45 +1000954 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000956 sarea_priv->last_enqueue = dev_priv->counter - 1;
957 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 return 0;
960}
961
Eric Anholtc153f452007-09-03 12:06:45 +1000962static int i810_clear_bufs(struct drm_device *dev, void *data,
963 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Eric Anholtc153f452007-09-03 12:06:45 +1000965 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Eric Anholt6c340ea2007-08-25 20:23:09 +1000967 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000969 /* GH: Someone's doing nasty things... */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200970 if (!dev->dev_private)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Eric Anholtc153f452007-09-03 12:06:45 +1000973 i810_dma_dispatch_clear(dev, clear->flags,
974 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Eric Anholtc153f452007-09-03 12:06:45 +1000978static int i810_swap_bufs(struct drm_device *dev, void *data,
979 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Márton Németh3e684ea2008-01-24 15:58:57 +1000981 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Eric Anholt6c340ea2007-08-25 20:23:09 +1000983 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000985 i810_dma_dispatch_swap(dev);
986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Eric Anholtc153f452007-09-03 12:06:45 +1000989static int i810_getage(struct drm_device *dev, void *data,
990 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
993 u32 *hw_status = dev_priv->hw_status_page;
994 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
995 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000997 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 0;
999}
1000
Eric Anholtc153f452007-09-03 12:06:45 +10001001static int i810_getbuf(struct drm_device *dev, void *data,
1002 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001004 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001005 drm_i810_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001006 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1007 u32 *hw_status = dev_priv->hw_status_page;
1008 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1009 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Eric Anholt6c340ea2007-08-25 20:23:09 +10001011 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Eric Anholtc153f452007-09-03 12:06:45 +10001013 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Eric Anholtc153f452007-09-03 12:06:45 +10001015 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001018 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001020 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 return retcode;
1023}
1024
Eric Anholtc153f452007-09-03 12:06:45 +10001025static int i810_copybuf(struct drm_device *dev, void *data,
1026 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 /* Never copy - 2.4.x doesn't need it */
1029 return 0;
1030}
1031
Eric Anholtc153f452007-09-03 12:06:45 +10001032static int i810_docopy(struct drm_device *dev, void *data,
1033 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 /* Never copy - 2.4.x doesn't need it */
1036 return 0;
1037}
1038
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001039static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 drm_i810_private_t *dev_priv = dev->dev_private;
1043 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1044 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1045 unsigned long address = (unsigned long)buf->bus_address;
1046 unsigned long start = address - dev->agp->base;
1047 int u;
1048 RING_LOCALS;
1049
1050 i810_kernel_lost_context(dev);
1051
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001053 if (u != I810_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 DRM_DEBUG("MC found buffer that isn't mine!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001056 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 used = 0;
1058
1059 sarea_priv->dirty = 0x7f;
1060
Márton Németh3e684ea2008-01-24 15:58:57 +10001061 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 dev_priv->counter++;
1064 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 DRM_DEBUG("start : %lx\n", start);
1066 DRM_DEBUG("used : %d\n", used);
1067 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1068
1069 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1070 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001071 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 used += 4;
1073 }
1074
1075 i810_unmap_buffer(buf);
1076 }
1077 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 OUT_RING(CMD_OP_BATCH_BUFFER);
1079 OUT_RING(start | BB1_PROTECTED);
1080 OUT_RING(start + used - 4);
1081 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ADVANCE_LP_RING();
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 OUT_RING(CMD_STORE_DWORD_IDX);
1086 OUT_RING(buf_priv->my_use_idx);
1087 OUT_RING(I810_BUF_FREE);
1088 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090 OUT_RING(CMD_STORE_DWORD_IDX);
1091 OUT_RING(16);
1092 OUT_RING(last_render);
1093 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ADVANCE_LP_RING();
1095}
1096
Eric Anholtc153f452007-09-03 12:06:45 +10001097static int i810_dma_mc(struct drm_device *dev, void *data,
1098 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Dave Airliecdd55a22007-07-11 16:32:08 +10001100 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 u32 *hw_status = dev_priv->hw_status_page;
1103 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001104 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001105 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Eric Anholt6c340ea2007-08-25 20:23:09 +10001107 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Eric Anholtc153f452007-09-03 12:06:45 +10001109 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return -EINVAL;
1111
Eric Anholtc153f452007-09-03 12:06:45 +10001112 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1113 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Eric Anholtc153f452007-09-03 12:06:45 +10001115 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001117 sarea_priv->last_enqueue = dev_priv->counter - 1;
1118 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 return 0;
1121}
1122
Eric Anholtc153f452007-09-03 12:06:45 +10001123static int i810_rstatus(struct drm_device *dev, void *data,
1124 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001126 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Eric Anholtc153f452007-09-03 12:06:45 +10001131static int i810_ov0_info(struct drm_device *dev, void *data,
1132 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001134 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001135 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Eric Anholtc153f452007-09-03 12:06:45 +10001137 ov->offset = dev_priv->overlay_offset;
1138 ov->physical = dev_priv->overlay_physical;
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return 0;
1141}
1142
Eric Anholtc153f452007-09-03 12:06:45 +10001143static int i810_fstatus(struct drm_device *dev, void *data,
1144 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001146 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Eric Anholt6c340ea2007-08-25 20:23:09 +10001148 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return I810_READ(0x30008);
1150}
1151
Eric Anholtc153f452007-09-03 12:06:45 +10001152static int i810_ov0_flip(struct drm_device *dev, void *data,
1153 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001155 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Eric Anholt6c340ea2007-08-25 20:23:09 +10001157 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001159 /* Tell the overlay to update */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001160 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 return 0;
1163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001166 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001167static void i810_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001170
Márton Németh3e684ea2008-01-24 15:58:57 +10001171 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 dev_priv->page_flipping = 1;
1173 dev_priv->current_page = 0;
1174 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1175}
1176
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001177static int i810_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 drm_i810_private_t *dev_priv = dev->dev_private;
1180
Márton Németh3e684ea2008-01-24 15:58:57 +10001181 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001183 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 dev_priv->page_flipping = 0;
1186 return 0;
1187}
1188
Eric Anholtc153f452007-09-03 12:06:45 +10001189static int i810_flip_bufs(struct drm_device *dev, void *data,
1190 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 drm_i810_private_t *dev_priv = dev->dev_private;
1193
Márton Németh3e684ea2008-01-24 15:58:57 +10001194 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Eric Anholt6c340ea2007-08-25 20:23:09 +10001196 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001198 if (!dev_priv->page_flipping)
1199 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001201 i810_dma_dispatch_flip(dev);
1202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Dave Airlieeddca552007-07-11 16:09:54 +10001205int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001206{
1207 /* i810 has 4 more counters */
1208 dev->counters += 4;
1209 dev->types[6] = _DRM_STAT_IRQ;
1210 dev->types[7] = _DRM_STAT_PRIMARY;
1211 dev->types[8] = _DRM_STAT_SECONDARY;
1212 dev->types[9] = _DRM_STAT_DMA;
1213
1214 return 0;
1215}
1216
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001217void i810_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001219 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001222void i810_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 if (dev->dev_private) {
1225 drm_i810_private_t *dev_priv = dev->dev_private;
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001226 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 i810_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229}
1230
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001231void i810_driver_reclaim_buffers_locked(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10001232 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001234 i810_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001237int i810_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001239 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return 0;
1241}
1242
Eric Anholtc153f452007-09-03 12:06:45 +10001243struct drm_ioctl_desc i810_ioctls[] = {
1244 DRM_IOCTL_DEF(DRM_I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1245 DRM_IOCTL_DEF(DRM_I810_VERTEX, i810_dma_vertex, DRM_AUTH),
1246 DRM_IOCTL_DEF(DRM_I810_CLEAR, i810_clear_bufs, DRM_AUTH),
1247 DRM_IOCTL_DEF(DRM_I810_FLUSH, i810_flush_ioctl, DRM_AUTH),
1248 DRM_IOCTL_DEF(DRM_I810_GETAGE, i810_getage, DRM_AUTH),
1249 DRM_IOCTL_DEF(DRM_I810_GETBUF, i810_getbuf, DRM_AUTH),
1250 DRM_IOCTL_DEF(DRM_I810_SWAP, i810_swap_bufs, DRM_AUTH),
1251 DRM_IOCTL_DEF(DRM_I810_COPY, i810_copybuf, DRM_AUTH),
1252 DRM_IOCTL_DEF(DRM_I810_DOCOPY, i810_docopy, DRM_AUTH),
1253 DRM_IOCTL_DEF(DRM_I810_OV0INFO, i810_ov0_info, DRM_AUTH),
1254 DRM_IOCTL_DEF(DRM_I810_FSTATUS, i810_fstatus, DRM_AUTH),
1255 DRM_IOCTL_DEF(DRM_I810_OV0FLIP, i810_ov0_flip, DRM_AUTH),
1256 DRM_IOCTL_DEF(DRM_I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1257 DRM_IOCTL_DEF(DRM_I810_RSTATUS, i810_rstatus, DRM_AUTH),
1258 DRM_IOCTL_DEF(DRM_I810_FLIP, i810_flip_bufs, DRM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259};
1260
1261int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001262
1263/**
1264 * Determine if the device really is AGP or not.
1265 *
1266 * All Intel graphics chipsets are treated as AGP, even if they are really
1267 * PCI-e.
1268 *
1269 * \param dev The device to be tested.
1270 *
1271 * \returns
1272 * A value of 1 is always retured to indictate every i810 is AGP.
1273 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001274int i810_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001275{
1276 return 1;
1277}