blob: 8f371e8d630ffeeced2b295400d2a7a4f3233b60 [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
Dave Airlie2c14f282008-04-21 16:47:32 +100096 dev = priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 buf_priv = buf->dev_private;
100
101 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
102 vma->vm_file = filp;
103
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104 buf_priv->currently_mapped = I810_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlie3d774612006-08-07 20:07:43 +1000107 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000108 vma->vm_end - vma->vm_start, vma->vm_page_prot))
109 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0;
111}
112
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800113static const struct file_operations i810_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000115 .release = drm_release,
Arnd Bergmann1f692a12011-01-25 23:17:15 +0100116 .unlocked_ioctl = drm_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000117 .mmap = i810_mmap_buffers,
118 .fasync = drm_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200119 .llseek = noop_llseek,
Dave Airliec94f7022005-07-07 21:03:38 +1000120};
121
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200122static int i810_map_buffer(struct drm_buf *buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Dave Airlie2c14f282008-04-21 16:47:32 +1000124 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000126 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800127 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int retcode = 0;
129
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return -EINVAL;
132
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000134 old_fops = file_priv->filp->f_op;
135 file_priv->filp->f_op = &i810_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 dev_priv->mmap_buffer = buf;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000137 buf_priv->virtual = (void *)do_mmap(file_priv->filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138 PROT_READ | PROT_WRITE,
139 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000141 file_priv->filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000142 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* Real error */
144 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000145 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 buf_priv->virtual = NULL;
147 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000148 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return retcode;
151}
152
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200153static int i810_unmap_buffer(struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
156 int retcode = 0;
157
158 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
159 return -EINVAL;
160
161 down_write(&current->mm->mmap_sem);
162 retcode = do_munmap(current->mm,
163 (unsigned long)buf_priv->virtual,
164 (size_t) buf->total);
165 up_write(&current->mm->mmap_sem);
166
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000167 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
168 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 return retcode;
171}
172
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200173static int i810_dma_get_buffer(struct drm_device *dev, drm_i810_dma_t *d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000174 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Dave Airlie056219e2007-07-11 16:17:42 +1000176 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 drm_i810_buf_priv_t *buf_priv;
178 int retcode = 0;
179
180 buf = i810_freelist_get(dev);
181 if (!buf) {
182 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000183 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return retcode;
185 }
186
Eric Anholt6c340ea2007-08-25 20:23:09 +1000187 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (retcode) {
189 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000190 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return retcode;
192 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000193 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 buf_priv = buf->dev_private;
195 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 d->request_idx = buf->idx;
197 d->request_size = buf->total;
198 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 return retcode;
201}
202
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200203static int i810_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Dave Airliecdd55a22007-07-11 16:32:08 +1000205 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* Make sure interrupts are disabled here because the uninstall ioctl
208 * may not have been called from userspace and after dev_private
209 * is freed, it's too late.
210 */
211 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
212 drm_irq_uninstall(dev);
213
214 if (dev->dev_private) {
215 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 drm_i810_private_t *dev_priv =
217 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200219 if (dev_priv->ring.virtual_start)
Dave Airlieb9094d32007-01-08 21:31:13 +1100220 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000221 if (dev_priv->hw_status_page) {
222 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 dev_priv->hw_status_page,
224 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000225 /* Need to rewrite hardware status page */
226 I810_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700228 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000232 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100234
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100236 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return 0;
240}
241
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200242static int i810_wait_ring(struct drm_device *dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243{
244 drm_i810_private_t *dev_priv = dev->dev_private;
245 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
246 int iters = 0;
247 unsigned long end;
248 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
249
250 end = jiffies + (HZ * 3);
251 while (ring->space < n) {
252 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
253 ring->space = ring->head - (ring->tail + 8);
254 if (ring->space < 0)
255 ring->space += ring->Size;
256
257 if (ring->head != last_head) {
258 end = jiffies + (HZ * 3);
259 last_head = ring->head;
260 }
261
262 iters++;
263 if (time_before(end, jiffies)) {
264 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
265 DRM_ERROR("lockup\n");
266 goto out_wait_ring;
267 }
268 udelay(1);
269 }
270
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200271out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 return iters;
273}
274
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200275static void i810_kernel_lost_context(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276{
277 drm_i810_private_t *dev_priv = dev->dev_private;
278 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
279
280 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
281 ring->tail = I810_READ(LP_RING + RING_TAIL);
282 ring->space = ring->head - (ring->tail + 8);
283 if (ring->space < 0)
284 ring->space += ring->Size;
285}
286
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200287static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288{
Dave Airliecdd55a22007-07-11 16:32:08 +1000289 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290 int my_idx = 24;
291 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
292 int i;
293
294 if (dma->buf_count > 1019) {
295 /* Not enough space in the status page for the freelist */
296 return -EINVAL;
297 }
298
299 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000300 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000301 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
302
303 buf_priv->in_use = hw_status++;
304 buf_priv->my_use_idx = my_idx;
305 my_idx += 4;
306
307 *buf_priv->in_use = I810_BUF_FREE;
308
Dave Airlieb9094d32007-01-08 21:31:13 +1100309 buf_priv->map.offset = buf->bus_address;
310 buf_priv->map.size = buf->total;
311 buf_priv->map.type = _DRM_AGP;
312 buf_priv->map.flags = 0;
313 buf_priv->map.mtrr = 0;
314
315 drm_core_ioremap(&buf_priv->map, dev);
316 buf_priv->kernel_virtual = buf_priv->map.handle;
317
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 }
319 return 0;
320}
321
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200322static int i810_dma_initialize(struct drm_device *dev,
323 drm_i810_private_t *dev_priv,
324 drm_i810_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Dave Airlie55910512007-07-11 16:53:40 +1000326 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Dave Airliebd1b3312007-05-26 05:01:51 +1000329 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (r_list->map &&
331 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334 break;
335 }
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!dev_priv->sarea_map) {
338 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 i810_dma_cleanup(dev);
340 DRM_ERROR("can not find sarea!\n");
341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
344 if (!dev_priv->mmio_map) {
345 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 i810_dma_cleanup(dev);
347 DRM_ERROR("can not find mmio map!\n");
348 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000350 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
352 if (!dev->agp_buffer_map) {
353 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 i810_dma_cleanup(dev);
355 DRM_ERROR("can not find dma buffer map!\n");
356 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000360 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000362 dev_priv->ring.Start = init->ring_start;
363 dev_priv->ring.End = init->ring_end;
364 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dave Airlieb9094d32007-01-08 21:31:13 +1100366 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
367 dev_priv->ring.map.size = init->ring_size;
368 dev_priv->ring.map.type = _DRM_AGP;
369 dev_priv->ring.map.flags = 0;
370 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Airlieb9094d32007-01-08 21:31:13 +1100372 drm_core_ioremap(&dev_priv->ring.map, dev);
373
374 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000375 dev->dev_private = (void *)dev_priv;
376 i810_dma_cleanup(dev);
377 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000379 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
Dave Airlieb9094d32007-01-08 21:31:13 +1100382 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
383
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000384 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 dev_priv->w = init->w;
387 dev_priv->h = init->h;
388 dev_priv->pitch = init->pitch;
389 dev_priv->back_offset = init->back_offset;
390 dev_priv->depth_offset = init->depth_offset;
391 dev_priv->front_offset = init->front_offset;
392
393 dev_priv->overlay_offset = init->overlay_offset;
394 dev_priv->overlay_physical = init->overlay_physical;
395
396 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
397 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
398 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
399
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000400 /* Program Hardware Status Page */
401 dev_priv->hw_status_page =
402 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
403 &dev_priv->dma_status_page);
404 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 dev->dev_private = (void *)dev_priv;
406 i810_dma_cleanup(dev);
407 DRM_ERROR("Can not allocate hardware status page\n");
408 return -ENOMEM;
409 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
411 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (i810_freelist_init(dev, dev_priv) != 0) {
418 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 i810_dma_cleanup(dev);
420 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 dev->dev_private = (void *)dev_priv;
425
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Eric Anholtc153f452007-09-03 12:06:45 +1000429static int i810_dma_init(struct drm_device *dev, void *data,
430 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000432 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000433 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000434 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Eric Anholtc153f452007-09-03 12:06:45 +1000436 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000437 case I810_INIT_DMA_1_4:
438 DRM_INFO("Using v1.4 init.\n");
Eric Anholt9a298b22009-03-24 12:23:04 -0700439 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000440 if (dev_priv == NULL)
441 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000442 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000445 case I810_CLEANUP_DMA:
446 DRM_INFO("DMA Cleanup\n");
447 retcode = i810_dma_cleanup(dev);
448 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000449 default:
450 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000453 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/* Most efficient way to verify state for the i810 is as it is
457 * emitted. Non-conformant state is silently dropped.
458 *
459 * Use 'volatile' & local var tmp to force the emitted values to be
460 * identical to the verified ones.
461 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200462static void i810EmitContextVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000465 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int i, j = 0;
467 unsigned int tmp;
468 RING_LOCALS;
469
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000472 OUT_RING(GFX_OP_COLOR_FACTOR);
473 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475 OUT_RING(GFX_OP_STIPPLE);
476 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000478 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 tmp = code[i];
480
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 if ((tmp & (7 << 29)) == (3 << 29) &&
482 (tmp & (0x1f << 24)) < (0x1d << 24)) {
483 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 } else
486 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
489 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 ADVANCE_LP_RING();
493}
494
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200495static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000497 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int i, j = 0;
499 unsigned int tmp;
500 RING_LOCALS;
501
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000502 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504 OUT_RING(GFX_OP_MAP_INFO);
505 OUT_RING(code[I810_TEXREG_MI1]);
506 OUT_RING(code[I810_TEXREG_MI2]);
507 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000509 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 tmp = code[i];
511
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000512 if ((tmp & (7 << 29)) == (3 << 29) &&
513 (tmp & (0x1f << 24)) < (0x1d << 24)) {
514 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516 } else
517 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000521 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 ADVANCE_LP_RING();
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/* Need to do some additional checking when setting the dest buffer.
527 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200528static void i810EmitDestVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000529 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 unsigned int tmp;
533 RING_LOCALS;
534
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000535 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 tmp = code[I810_DESTREG_DI1];
538 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 OUT_RING(CMD_OP_DESTBUFFER_INFO);
540 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000542 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
543 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* invarient:
546 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000547 OUT_RING(CMD_OP_Z_BUFFER_INFO);
548 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 OUT_RING(GFX_OP_DESTBUFFER_VARS);
551 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 OUT_RING(GFX_OP_DRAWRECT_INFO);
554 OUT_RING(code[I810_DESTREG_DR1]);
555 OUT_RING(code[I810_DESTREG_DR2]);
556 OUT_RING(code[I810_DESTREG_DR3]);
557 OUT_RING(code[I810_DESTREG_DR4]);
558 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 ADVANCE_LP_RING();
561}
562
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200563static void i810EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 drm_i810_private_t *dev_priv = dev->dev_private;
566 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568
Márton Németh3e684ea2008-01-24 15:58:57 +1000569 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
574 }
575
576 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
579 }
580
581 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
584 }
585
586 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
589 }
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/* need to verify
593 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200594static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 unsigned int clear_color,
596 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 drm_i810_private_t *dev_priv = dev->dev_private;
599 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000601 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int pitch = dev_priv->pitch;
603 int cpp = 2;
604 int i;
605 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606
607 if (dev_priv->current_page == 1) {
608 unsigned int tmp = flags;
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 if (tmp & I810_FRONT)
612 flags |= I810_BACK;
613 if (tmp & I810_BACK)
614 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 if (nbox > I810_NR_SAREA_CLIPRECTS)
620 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unsigned int x = pbox->x1;
624 unsigned int y = pbox->y1;
625 unsigned int width = (pbox->x2 - x) * cpp;
626 unsigned int height = pbox->y2 - y;
627 unsigned int start = y * pitch + x * cpp;
628
629 if (pbox->x1 > pbox->x2 ||
630 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 continue;
633
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000634 if (flags & I810_FRONT) {
635 BEGIN_LP_RING(6);
636 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
637 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
638 OUT_RING((height << 16) | width);
639 OUT_RING(start);
640 OUT_RING(clear_color);
641 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 ADVANCE_LP_RING();
643 }
644
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000645 if (flags & I810_BACK) {
646 BEGIN_LP_RING(6);
647 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
648 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
649 OUT_RING((height << 16) | width);
650 OUT_RING(dev_priv->back_offset + start);
651 OUT_RING(clear_color);
652 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 ADVANCE_LP_RING();
654 }
655
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 if (flags & I810_DEPTH) {
657 BEGIN_LP_RING(6);
658 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
659 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
660 OUT_RING((height << 16) | width);
661 OUT_RING(dev_priv->depth_offset + start);
662 OUT_RING(clear_zval);
663 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 ADVANCE_LP_RING();
665 }
666 }
667}
668
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200669static void i810_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000671 drm_i810_private_t *dev_priv = dev->dev_private;
672 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000674 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int pitch = dev_priv->pitch;
676 int cpp = 2;
677 int i;
678 RING_LOCALS;
679
680 DRM_DEBUG("swapbuffers\n");
681
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 if (nbox > I810_NR_SAREA_CLIPRECTS)
685 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 unsigned int w = pbox->x2 - pbox->x1;
689 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned int start = dst;
692
693 if (pbox->x1 > pbox->x2 ||
694 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 continue;
697
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000698 BEGIN_LP_RING(6);
699 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
700 OUT_RING(pitch | (0xCC << 16));
701 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 OUT_RING(dev_priv->back_offset + start);
706 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 ADVANCE_LP_RING();
712 }
713}
714
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200715static void i810_dma_dispatch_vertex(struct drm_device *dev,
716 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000721 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned long address = (unsigned long)buf->bus_address;
724 unsigned long start = address - dev->agp->base;
725 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000726 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 nbox = I810_NR_SAREA_CLIPRECTS;
732
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000733 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 used = 0;
735
736 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
740 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
741
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 *(u32 *) buf_priv->kernel_virtual =
743 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000746 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 used += 4;
748 }
749
750 i810_unmap_buffer(buf);
751 }
752
753 if (used) {
754 do {
755 if (i < nbox) {
756 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
758 SC_ENABLE);
759 OUT_RING(GFX_OP_SCISSOR_INFO);
760 OUT_RING(box[i].x1 | (box[i].y1 << 16));
761 OUT_RING((box[i].x2 -
762 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ADVANCE_LP_RING();
764 }
765
766 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 OUT_RING(CMD_OP_BATCH_BUFFER);
768 OUT_RING(start | BB1_PROTECTED);
769 OUT_RING(start + used - 4);
770 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 ADVANCE_LP_RING();
772
773 } while (++i < nbox);
774 }
775
776 if (discard) {
777 dev_priv->counter++;
778
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
780 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 OUT_RING(CMD_STORE_DWORD_IDX);
784 OUT_RING(20);
785 OUT_RING(dev_priv->counter);
786 OUT_RING(CMD_STORE_DWORD_IDX);
787 OUT_RING(buf_priv->my_use_idx);
788 OUT_RING(I810_BUF_FREE);
789 OUT_RING(CMD_REPORT_HEAD);
790 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 ADVANCE_LP_RING();
792 }
793}
794
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200795static void i810_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int pitch = dev_priv->pitch;
799 RING_LOCALS;
800
Márton Németh3e684ea2008-01-24 15:58:57 +1000801 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 dev_priv->current_page,
803 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 i810_kernel_lost_context(dev);
806
807 BEGIN_LP_RING(2);
808 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
809 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ADVANCE_LP_RING();
811
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* On i815 at least ASYNC is buggy */
814 /* pitch<<5 is from 11.2.8 p158,
815 its the pitch / 8 then left shifted 8,
816 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
818 if (dev_priv->current_page == 0) {
819 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 dev_priv->current_page = 1;
821 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 dev_priv->current_page = 0;
824 }
825 OUT_RING(0);
826 ADVANCE_LP_RING();
827
828 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
830 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ADVANCE_LP_RING();
832
833 /* Increment the frame counter. The client-side 3D driver must
834 * throttle the framerate by waiting for this value before
835 * performing the swapbuffer ioctl.
836 */
837 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
838
839}
840
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200841static void i810_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 drm_i810_private_t *dev_priv = dev->dev_private;
844 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 BEGIN_LP_RING(4);
849 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
850 OUT_RING(CMD_REPORT_HEAD);
851 OUT_RING(0);
852 OUT_RING(0);
853 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200858static int i810_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000861 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 int i, ret = 0;
863 RING_LOCALS;
864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 BEGIN_LP_RING(2);
868 OUT_RING(CMD_REPORT_HEAD);
869 OUT_RING(0);
870 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000872 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000875 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
879 I810_BUF_FREE);
880
881 if (used == I810_BUF_HARDWARE)
882 DRM_DEBUG("reclaimed from HARDWARE\n");
883 if (used == I810_BUF_CLIENT)
884 DRM_DEBUG("still on client\n");
885 }
886
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890/* Must be called with the lock held */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200891static void i810_reclaim_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000892 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Dave Airliecdd55a22007-07-11 16:32:08 +1000894 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 if (!dma)
898 return;
899 if (!dev->dev_private)
900 return;
901 if (!dma->buflist)
902 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000904 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000907 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Eric Anholt6c340ea2007-08-25 20:23:09 +1000910 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
912 I810_BUF_FREE);
913
914 if (used == I810_BUF_CLIENT)
915 DRM_DEBUG("reclaimed from client\n");
916 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 }
920}
921
Eric Anholtc153f452007-09-03 12:06:45 +1000922static int i810_flush_ioctl(struct drm_device *dev, void *data,
923 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000925 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927 i810_flush_queue(dev);
928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Eric Anholtc153f452007-09-03 12:06:45 +1000931static int i810_dma_vertex(struct drm_device *dev, void *data,
932 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Dave Airliecdd55a22007-07-11 16:32:08 +1000934 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000935 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
936 u32 *hw_status = dev_priv->hw_status_page;
937 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
938 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000939 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Eric Anholt6c340ea2007-08-25 20:23:09 +1000941 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Márton Németh3e684ea2008-01-24 15:58:57 +1000943 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000944 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Eric Anholtc153f452007-09-03 12:06:45 +1000946 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return -EINVAL;
948
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000949 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000950 dma->buflist[vertex->idx],
951 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Eric Anholtc153f452007-09-03 12:06:45 +1000953 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 sarea_priv->last_enqueue = dev_priv->counter - 1;
956 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 return 0;
959}
960
Eric Anholtc153f452007-09-03 12:06:45 +1000961static int i810_clear_bufs(struct drm_device *dev, void *data,
962 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Eric Anholtc153f452007-09-03 12:06:45 +1000964 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Eric Anholt6c340ea2007-08-25 20:23:09 +1000966 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 /* GH: Someone's doing nasty things... */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200969 if (!dev->dev_private)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000970 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Eric Anholtc153f452007-09-03 12:06:45 +1000972 i810_dma_dispatch_clear(dev, clear->flags,
973 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Eric Anholtc153f452007-09-03 12:06:45 +1000977static int i810_swap_bufs(struct drm_device *dev, void *data,
978 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Márton Németh3e684ea2008-01-24 15:58:57 +1000980 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Eric Anholt6c340ea2007-08-25 20:23:09 +1000982 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 i810_dma_dispatch_swap(dev);
985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Eric Anholtc153f452007-09-03 12:06:45 +1000988static int i810_getage(struct drm_device *dev, void *data,
989 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000991 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
992 u32 *hw_status = dev_priv->hw_status_page;
993 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
994 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return 0;
998}
999
Eric Anholtc153f452007-09-03 12:06:45 +10001000static int i810_getbuf(struct drm_device *dev, void *data,
1001 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001004 drm_i810_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1006 u32 *hw_status = dev_priv->hw_status_page;
1007 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1008 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Eric Anholt6c340ea2007-08-25 20:23:09 +10001010 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Eric Anholtc153f452007-09-03 12:06:45 +10001012 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Eric Anholtc153f452007-09-03 12:06:45 +10001014 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001017 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 return retcode;
1022}
1023
Eric Anholtc153f452007-09-03 12:06:45 +10001024static int i810_copybuf(struct drm_device *dev, void *data,
1025 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 /* Never copy - 2.4.x doesn't need it */
1028 return 0;
1029}
1030
Eric Anholtc153f452007-09-03 12:06:45 +10001031static int i810_docopy(struct drm_device *dev, void *data,
1032 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 /* Never copy - 2.4.x doesn't need it */
1035 return 0;
1036}
1037
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001038static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001039 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 drm_i810_private_t *dev_priv = dev->dev_private;
1042 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1043 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1044 unsigned long address = (unsigned long)buf->bus_address;
1045 unsigned long start = address - dev->agp->base;
1046 int u;
1047 RING_LOCALS;
1048
1049 i810_kernel_lost_context(dev);
1050
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001052 if (u != I810_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 DRM_DEBUG("MC found buffer that isn't mine!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 used = 0;
1057
1058 sarea_priv->dirty = 0x7f;
1059
Márton Németh3e684ea2008-01-24 15:58:57 +10001060 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 dev_priv->counter++;
1063 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 DRM_DEBUG("start : %lx\n", start);
1065 DRM_DEBUG("used : %d\n", used);
1066 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1067
1068 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1069 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001070 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 used += 4;
1072 }
1073
1074 i810_unmap_buffer(buf);
1075 }
1076 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 OUT_RING(CMD_OP_BATCH_BUFFER);
1078 OUT_RING(start | BB1_PROTECTED);
1079 OUT_RING(start + used - 4);
1080 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 ADVANCE_LP_RING();
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 OUT_RING(CMD_STORE_DWORD_IDX);
1085 OUT_RING(buf_priv->my_use_idx);
1086 OUT_RING(I810_BUF_FREE);
1087 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 OUT_RING(CMD_STORE_DWORD_IDX);
1090 OUT_RING(16);
1091 OUT_RING(last_render);
1092 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ADVANCE_LP_RING();
1094}
1095
Eric Anholtc153f452007-09-03 12:06:45 +10001096static int i810_dma_mc(struct drm_device *dev, void *data,
1097 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Dave Airliecdd55a22007-07-11 16:32:08 +10001099 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001100 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 u32 *hw_status = dev_priv->hw_status_page;
1102 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001104 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Eric Anholt6c340ea2007-08-25 20:23:09 +10001106 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Eric Anholtc153f452007-09-03 12:06:45 +10001108 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return -EINVAL;
1110
Eric Anholtc153f452007-09-03 12:06:45 +10001111 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1112 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Eric Anholtc153f452007-09-03 12:06:45 +10001114 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 sarea_priv->last_enqueue = dev_priv->counter - 1;
1117 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 return 0;
1120}
1121
Eric Anholtc153f452007-09-03 12:06:45 +10001122static int i810_rstatus(struct drm_device *dev, void *data,
1123 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001125 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Eric Anholtc153f452007-09-03 12:06:45 +10001130static int i810_ov0_info(struct drm_device *dev, void *data,
1131 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001134 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Eric Anholtc153f452007-09-03 12:06:45 +10001136 ov->offset = dev_priv->overlay_offset;
1137 ov->physical = dev_priv->overlay_physical;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140}
1141
Eric Anholtc153f452007-09-03 12:06:45 +10001142static int i810_fstatus(struct drm_device *dev, void *data,
1143 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001145 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Eric Anholt6c340ea2007-08-25 20:23:09 +10001147 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return I810_READ(0x30008);
1149}
1150
Eric Anholtc153f452007-09-03 12:06:45 +10001151static int i810_ov0_flip(struct drm_device *dev, void *data,
1152 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001154 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric Anholt6c340ea2007-08-25 20:23:09 +10001156 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001158 /* Tell the overlay to update */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001159 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 return 0;
1162}
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001165 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001166static void i810_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001169
Márton Németh3e684ea2008-01-24 15:58:57 +10001170 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 dev_priv->page_flipping = 1;
1172 dev_priv->current_page = 0;
1173 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1174}
1175
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001176static int i810_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 drm_i810_private_t *dev_priv = dev->dev_private;
1179
Márton Németh3e684ea2008-01-24 15:58:57 +10001180 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001182 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 dev_priv->page_flipping = 0;
1185 return 0;
1186}
1187
Eric Anholtc153f452007-09-03 12:06:45 +10001188static int i810_flip_bufs(struct drm_device *dev, void *data,
1189 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 drm_i810_private_t *dev_priv = dev->dev_private;
1192
Márton Németh3e684ea2008-01-24 15:58:57 +10001193 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Eric Anholt6c340ea2007-08-25 20:23:09 +10001195 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001197 if (!dev_priv->page_flipping)
1198 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001200 i810_dma_dispatch_flip(dev);
1201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Dave Airlieeddca552007-07-11 16:09:54 +10001204int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001205{
1206 /* i810 has 4 more counters */
1207 dev->counters += 4;
1208 dev->types[6] = _DRM_STAT_IRQ;
1209 dev->types[7] = _DRM_STAT_PRIMARY;
1210 dev->types[8] = _DRM_STAT_SECONDARY;
1211 dev->types[9] = _DRM_STAT_DMA;
1212
1213 return 0;
1214}
1215
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001216void i810_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001218 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001221void i810_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 if (dev->dev_private) {
1224 drm_i810_private_t *dev_priv = dev->dev_private;
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001225 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 i810_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228}
1229
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001230void i810_driver_reclaim_buffers_locked(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10001231 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001233 i810_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001236int i810_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001238 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return 0;
1240}
1241
Eric Anholtc153f452007-09-03 12:06:45 +10001242struct drm_ioctl_desc i810_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001243 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1244 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1245 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1246 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1247 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1248 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1249 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1250 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1251 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1252 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1253 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1254 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1255 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1256 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1257 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258};
1259
1260int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001261
1262/**
1263 * Determine if the device really is AGP or not.
1264 *
1265 * All Intel graphics chipsets are treated as AGP, even if they are really
1266 * PCI-e.
1267 *
1268 * \param dev The device to be tested.
1269 *
1270 * \returns
1271 * A value of 1 is always retured to indictate every i810 is AGP.
1272 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001273int i810_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001274{
1275 return 1;
1276}