blob: f7c17b23983389ae7b978224180dadcd4b6616c3 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700226 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000230 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100232
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000233 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100234 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 0;
238}
239
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200240static int i810_wait_ring(struct drm_device *dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241{
242 drm_i810_private_t *dev_priv = dev->dev_private;
243 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
244 int iters = 0;
245 unsigned long end;
246 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
247
248 end = jiffies + (HZ * 3);
249 while (ring->space < n) {
250 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
251 ring->space = ring->head - (ring->tail + 8);
252 if (ring->space < 0)
253 ring->space += ring->Size;
254
255 if (ring->head != last_head) {
256 end = jiffies + (HZ * 3);
257 last_head = ring->head;
258 }
259
260 iters++;
261 if (time_before(end, jiffies)) {
262 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
263 DRM_ERROR("lockup\n");
264 goto out_wait_ring;
265 }
266 udelay(1);
267 }
268
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200269out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270 return iters;
271}
272
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200273static void i810_kernel_lost_context(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274{
275 drm_i810_private_t *dev_priv = dev->dev_private;
276 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
277
278 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
279 ring->tail = I810_READ(LP_RING + RING_TAIL);
280 ring->space = ring->head - (ring->tail + 8);
281 if (ring->space < 0)
282 ring->space += ring->Size;
283}
284
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200285static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286{
Dave Airliecdd55a22007-07-11 16:32:08 +1000287 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 int my_idx = 24;
289 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
290 int i;
291
292 if (dma->buf_count > 1019) {
293 /* Not enough space in the status page for the freelist */
294 return -EINVAL;
295 }
296
297 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000298 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
300
301 buf_priv->in_use = hw_status++;
302 buf_priv->my_use_idx = my_idx;
303 my_idx += 4;
304
305 *buf_priv->in_use = I810_BUF_FREE;
306
Dave Airlieb9094d32007-01-08 21:31:13 +1100307 buf_priv->map.offset = buf->bus_address;
308 buf_priv->map.size = buf->total;
309 buf_priv->map.type = _DRM_AGP;
310 buf_priv->map.flags = 0;
311 buf_priv->map.mtrr = 0;
312
313 drm_core_ioremap(&buf_priv->map, dev);
314 buf_priv->kernel_virtual = buf_priv->map.handle;
315
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 }
317 return 0;
318}
319
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200320static int i810_dma_initialize(struct drm_device *dev,
321 drm_i810_private_t *dev_priv,
322 drm_i810_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Dave Airlie55910512007-07-11 16:53:40 +1000324 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Dave Airliebd1b3312007-05-26 05:01:51 +1000327 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (r_list->map &&
329 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000330 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 break;
333 }
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (!dev_priv->sarea_map) {
336 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 i810_dma_cleanup(dev);
338 DRM_ERROR("can not find sarea!\n");
339 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
342 if (!dev_priv->mmio_map) {
343 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 i810_dma_cleanup(dev);
345 DRM_ERROR("can not find mmio map!\n");
346 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000348 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
350 if (!dev->agp_buffer_map) {
351 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000352 i810_dma_cleanup(dev);
353 DRM_ERROR("can not find dma buffer map!\n");
354 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
357 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000360 dev_priv->ring.Start = init->ring_start;
361 dev_priv->ring.End = init->ring_end;
362 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dave Airlieb9094d32007-01-08 21:31:13 +1100364 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
365 dev_priv->ring.map.size = init->ring_size;
366 dev_priv->ring.map.type = _DRM_AGP;
367 dev_priv->ring.map.flags = 0;
368 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Dave Airlieb9094d32007-01-08 21:31:13 +1100370 drm_core_ioremap(&dev_priv->ring.map, dev);
371
372 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 dev->dev_private = (void *)dev_priv;
374 i810_dma_cleanup(dev);
375 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000377 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Dave Airlieb9094d32007-01-08 21:31:13 +1100380 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
381
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000382 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 dev_priv->w = init->w;
385 dev_priv->h = init->h;
386 dev_priv->pitch = init->pitch;
387 dev_priv->back_offset = init->back_offset;
388 dev_priv->depth_offset = init->depth_offset;
389 dev_priv->front_offset = init->front_offset;
390
391 dev_priv->overlay_offset = init->overlay_offset;
392 dev_priv->overlay_physical = init->overlay_physical;
393
394 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
395 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
396 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
397
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000398 /* Program Hardware Status Page */
399 dev_priv->hw_status_page =
400 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
401 &dev_priv->dma_status_page);
402 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dev->dev_private = (void *)dev_priv;
404 i810_dma_cleanup(dev);
405 DRM_ERROR("Can not allocate hardware status page\n");
406 return -ENOMEM;
407 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000408 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
409 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (i810_freelist_init(dev, dev_priv) != 0) {
416 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 i810_dma_cleanup(dev);
418 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000420 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 dev->dev_private = (void *)dev_priv;
423
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Eric Anholtc153f452007-09-03 12:06:45 +1000427static int i810_dma_init(struct drm_device *dev, void *data,
428 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000430 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000431 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000432 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Eric Anholtc153f452007-09-03 12:06:45 +1000434 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435 case I810_INIT_DMA_1_4:
436 DRM_INFO("Using v1.4 init.\n");
Eric Anholt9a298b22009-03-24 12:23:04 -0700437 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438 if (dev_priv == NULL)
439 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000440 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443 case I810_CLEANUP_DMA:
444 DRM_INFO("DMA Cleanup\n");
445 retcode = i810_dma_cleanup(dev);
446 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000447 default:
448 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/* Most efficient way to verify state for the i810 is as it is
455 * emitted. Non-conformant state is silently dropped.
456 *
457 * Use 'volatile' & local var tmp to force the emitted values to be
458 * identical to the verified ones.
459 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200460static void i810EmitContextVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int i, j = 0;
465 unsigned int tmp;
466 RING_LOCALS;
467
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470 OUT_RING(GFX_OP_COLOR_FACTOR);
471 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 OUT_RING(GFX_OP_STIPPLE);
474 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 tmp = code[i];
478
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000479 if ((tmp & (7 << 29)) == (3 << 29) &&
480 (tmp & (0x1f << 24)) < (0x1d << 24)) {
481 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483 } else
484 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
487 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 ADVANCE_LP_RING();
491}
492
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200493static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000495 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int i, j = 0;
497 unsigned int tmp;
498 RING_LOCALS;
499
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000500 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000502 OUT_RING(GFX_OP_MAP_INFO);
503 OUT_RING(code[I810_TEXREG_MI1]);
504 OUT_RING(code[I810_TEXREG_MI2]);
505 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 tmp = code[i];
509
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000510 if ((tmp & (7 << 29)) == (3 << 29) &&
511 (tmp & (0x1f << 24)) < (0x1d << 24)) {
512 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000514 } else
515 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
518 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 ADVANCE_LP_RING();
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/* Need to do some additional checking when setting the dest buffer.
525 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200526static void i810EmitDestVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000527 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000529 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 unsigned int tmp;
531 RING_LOCALS;
532
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 tmp = code[I810_DESTREG_DI1];
536 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 OUT_RING(CMD_OP_DESTBUFFER_INFO);
538 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
541 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* invarient:
544 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 OUT_RING(CMD_OP_Z_BUFFER_INFO);
546 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 OUT_RING(GFX_OP_DESTBUFFER_VARS);
549 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 OUT_RING(GFX_OP_DRAWRECT_INFO);
552 OUT_RING(code[I810_DESTREG_DR1]);
553 OUT_RING(code[I810_DESTREG_DR2]);
554 OUT_RING(code[I810_DESTREG_DR3]);
555 OUT_RING(code[I810_DESTREG_DR4]);
556 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 ADVANCE_LP_RING();
559}
560
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200561static void i810EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 drm_i810_private_t *dev_priv = dev->dev_private;
564 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000566
Márton Németh3e684ea2008-01-24 15:58:57 +1000567 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
572 }
573
574 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
577 }
578
579 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
582 }
583
584 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000585 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
587 }
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/* need to verify
591 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200592static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000593 unsigned int clear_color,
594 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 drm_i810_private_t *dev_priv = dev->dev_private;
597 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000599 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int pitch = dev_priv->pitch;
601 int cpp = 2;
602 int i;
603 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000604
605 if (dev_priv->current_page == 1) {
606 unsigned int tmp = flags;
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609 if (tmp & I810_FRONT)
610 flags |= I810_BACK;
611 if (tmp & I810_BACK)
612 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 if (nbox > I810_NR_SAREA_CLIPRECTS)
618 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned int x = pbox->x1;
622 unsigned int y = pbox->y1;
623 unsigned int width = (pbox->x2 - x) * cpp;
624 unsigned int height = pbox->y2 - y;
625 unsigned int start = y * pitch + x * cpp;
626
627 if (pbox->x1 > pbox->x2 ||
628 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 continue;
631
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000632 if (flags & I810_FRONT) {
633 BEGIN_LP_RING(6);
634 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
635 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
636 OUT_RING((height << 16) | width);
637 OUT_RING(start);
638 OUT_RING(clear_color);
639 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ADVANCE_LP_RING();
641 }
642
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643 if (flags & I810_BACK) {
644 BEGIN_LP_RING(6);
645 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
646 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
647 OUT_RING((height << 16) | width);
648 OUT_RING(dev_priv->back_offset + start);
649 OUT_RING(clear_color);
650 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ADVANCE_LP_RING();
652 }
653
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000654 if (flags & I810_DEPTH) {
655 BEGIN_LP_RING(6);
656 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
657 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
658 OUT_RING((height << 16) | width);
659 OUT_RING(dev_priv->depth_offset + start);
660 OUT_RING(clear_zval);
661 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 ADVANCE_LP_RING();
663 }
664 }
665}
666
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200667static void i810_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 drm_i810_private_t *dev_priv = dev->dev_private;
670 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000672 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int pitch = dev_priv->pitch;
674 int cpp = 2;
675 int i;
676 RING_LOCALS;
677
678 DRM_DEBUG("swapbuffers\n");
679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 if (nbox > I810_NR_SAREA_CLIPRECTS)
683 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 unsigned int w = pbox->x2 - pbox->x1;
687 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned int start = dst;
690
691 if (pbox->x1 > pbox->x2 ||
692 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 continue;
695
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 BEGIN_LP_RING(6);
697 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
698 OUT_RING(pitch | (0xCC << 16));
699 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 OUT_RING(dev_priv->back_offset + start);
704 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 ADVANCE_LP_RING();
710 }
711}
712
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200713static void i810_dma_dispatch_vertex(struct drm_device *dev,
714 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000719 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned long address = (unsigned long)buf->bus_address;
722 unsigned long start = address - dev->agp->base;
723 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000726 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 nbox = I810_NR_SAREA_CLIPRECTS;
730
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 used = 0;
733
734 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000735 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
738 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
739
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 *(u32 *) buf_priv->kernel_virtual =
741 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000744 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 used += 4;
746 }
747
748 i810_unmap_buffer(buf);
749 }
750
751 if (used) {
752 do {
753 if (i < nbox) {
754 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
756 SC_ENABLE);
757 OUT_RING(GFX_OP_SCISSOR_INFO);
758 OUT_RING(box[i].x1 | (box[i].y1 << 16));
759 OUT_RING((box[i].x2 -
760 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 ADVANCE_LP_RING();
762 }
763
764 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 OUT_RING(CMD_OP_BATCH_BUFFER);
766 OUT_RING(start | BB1_PROTECTED);
767 OUT_RING(start + used - 4);
768 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ADVANCE_LP_RING();
770
771 } while (++i < nbox);
772 }
773
774 if (discard) {
775 dev_priv->counter++;
776
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000777 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
778 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 OUT_RING(CMD_STORE_DWORD_IDX);
782 OUT_RING(20);
783 OUT_RING(dev_priv->counter);
784 OUT_RING(CMD_STORE_DWORD_IDX);
785 OUT_RING(buf_priv->my_use_idx);
786 OUT_RING(I810_BUF_FREE);
787 OUT_RING(CMD_REPORT_HEAD);
788 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 ADVANCE_LP_RING();
790 }
791}
792
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200793static void i810_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int pitch = dev_priv->pitch;
797 RING_LOCALS;
798
Márton Németh3e684ea2008-01-24 15:58:57 +1000799 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 dev_priv->current_page,
801 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 i810_kernel_lost_context(dev);
804
805 BEGIN_LP_RING(2);
806 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
807 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ADVANCE_LP_RING();
809
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* On i815 at least ASYNC is buggy */
812 /* pitch<<5 is from 11.2.8 p158,
813 its the pitch / 8 then left shifted 8,
814 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
816 if (dev_priv->current_page == 0) {
817 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 dev_priv->current_page = 1;
819 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 dev_priv->current_page = 0;
822 }
823 OUT_RING(0);
824 ADVANCE_LP_RING();
825
826 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
828 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ADVANCE_LP_RING();
830
831 /* Increment the frame counter. The client-side 3D driver must
832 * throttle the framerate by waiting for this value before
833 * performing the swapbuffer ioctl.
834 */
835 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
836
837}
838
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200839static void i810_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000841 drm_i810_private_t *dev_priv = dev->dev_private;
842 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 BEGIN_LP_RING(4);
847 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
848 OUT_RING(CMD_REPORT_HEAD);
849 OUT_RING(0);
850 OUT_RING(0);
851 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200856static int i810_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000859 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 int i, ret = 0;
861 RING_LOCALS;
862
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 BEGIN_LP_RING(2);
866 OUT_RING(CMD_REPORT_HEAD);
867 OUT_RING(0);
868 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000872 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000873 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
877 I810_BUF_FREE);
878
879 if (used == I810_BUF_HARDWARE)
880 DRM_DEBUG("reclaimed from HARDWARE\n");
881 if (used == I810_BUF_CLIENT)
882 DRM_DEBUG("still on client\n");
883 }
884
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000885 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888/* Must be called with the lock held */
Daniel Vetter87499ff2011-10-25 23:51:24 +0200889void i810_driver_reclaim_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000890 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Dave Airliecdd55a22007-07-11 16:32:08 +1000892 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 if (!dma)
896 return;
897 if (!dev->dev_private)
898 return;
899 if (!dma->buflist)
900 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000902 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000905 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Eric Anholt6c340ea2007-08-25 20:23:09 +1000908 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
910 I810_BUF_FREE);
911
912 if (used == I810_BUF_CLIENT)
913 DRM_DEBUG("reclaimed from client\n");
914 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 }
918}
919
Eric Anholtc153f452007-09-03 12:06:45 +1000920static int i810_flush_ioctl(struct drm_device *dev, void *data,
921 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000923 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 i810_flush_queue(dev);
926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Eric Anholtc153f452007-09-03 12:06:45 +1000929static int i810_dma_vertex(struct drm_device *dev, void *data,
930 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Dave Airliecdd55a22007-07-11 16:32:08 +1000932 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000933 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
934 u32 *hw_status = dev_priv->hw_status_page;
935 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
936 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000937 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Eric Anholt6c340ea2007-08-25 20:23:09 +1000939 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Márton Németh3e684ea2008-01-24 15:58:57 +1000941 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000942 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Eric Anholtc153f452007-09-03 12:06:45 +1000944 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -EINVAL;
946
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000948 dma->buflist[vertex->idx],
949 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Eric Anholtc153f452007-09-03 12:06:45 +1000951 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000953 sarea_priv->last_enqueue = dev_priv->counter - 1;
954 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 return 0;
957}
958
Eric Anholtc153f452007-09-03 12:06:45 +1000959static int i810_clear_bufs(struct drm_device *dev, void *data,
960 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Eric Anholtc153f452007-09-03 12:06:45 +1000962 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Eric Anholt6c340ea2007-08-25 20:23:09 +1000964 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000966 /* GH: Someone's doing nasty things... */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200967 if (!dev->dev_private)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Eric Anholtc153f452007-09-03 12:06:45 +1000970 i810_dma_dispatch_clear(dev, clear->flags,
971 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Eric Anholtc153f452007-09-03 12:06:45 +1000975static int i810_swap_bufs(struct drm_device *dev, void *data,
976 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Márton Németh3e684ea2008-01-24 15:58:57 +1000978 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Eric Anholt6c340ea2007-08-25 20:23:09 +1000980 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000982 i810_dma_dispatch_swap(dev);
983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Eric Anholtc153f452007-09-03 12:06:45 +1000986static int i810_getage(struct drm_device *dev, void *data,
987 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000989 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
990 u32 *hw_status = dev_priv->hw_status_page;
991 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
992 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000994 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return 0;
996}
997
Eric Anholtc153f452007-09-03 12:06:45 +1000998static int i810_getbuf(struct drm_device *dev, void *data,
999 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001002 drm_i810_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1004 u32 *hw_status = dev_priv->hw_status_page;
1005 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1006 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Eric Anholt6c340ea2007-08-25 20:23:09 +10001008 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Eric Anholtc153f452007-09-03 12:06:45 +10001010 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Eric Anholtc153f452007-09-03 12:06:45 +10001012 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001015 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001017 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 return retcode;
1020}
1021
Eric Anholtc153f452007-09-03 12:06:45 +10001022static int i810_copybuf(struct drm_device *dev, void *data,
1023 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 /* Never copy - 2.4.x doesn't need it */
1026 return 0;
1027}
1028
Eric Anholtc153f452007-09-03 12:06:45 +10001029static int i810_docopy(struct drm_device *dev, void *data,
1030 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 /* Never copy - 2.4.x doesn't need it */
1033 return 0;
1034}
1035
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001036static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 drm_i810_private_t *dev_priv = dev->dev_private;
1040 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1041 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1042 unsigned long address = (unsigned long)buf->bus_address;
1043 unsigned long start = address - dev->agp->base;
1044 int u;
1045 RING_LOCALS;
1046
1047 i810_kernel_lost_context(dev);
1048
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001050 if (u != I810_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 DRM_DEBUG("MC found buffer that isn't mine!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 used = 0;
1055
1056 sarea_priv->dirty = 0x7f;
1057
Márton Németh3e684ea2008-01-24 15:58:57 +10001058 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 dev_priv->counter++;
1061 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 DRM_DEBUG("start : %lx\n", start);
1063 DRM_DEBUG("used : %d\n", used);
1064 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1065
1066 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1067 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001068 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 used += 4;
1070 }
1071
1072 i810_unmap_buffer(buf);
1073 }
1074 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075 OUT_RING(CMD_OP_BATCH_BUFFER);
1076 OUT_RING(start | BB1_PROTECTED);
1077 OUT_RING(start + used - 4);
1078 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ADVANCE_LP_RING();
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 OUT_RING(CMD_STORE_DWORD_IDX);
1083 OUT_RING(buf_priv->my_use_idx);
1084 OUT_RING(I810_BUF_FREE);
1085 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 OUT_RING(CMD_STORE_DWORD_IDX);
1088 OUT_RING(16);
1089 OUT_RING(last_render);
1090 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 ADVANCE_LP_RING();
1092}
1093
Eric Anholtc153f452007-09-03 12:06:45 +10001094static int i810_dma_mc(struct drm_device *dev, void *data,
1095 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Dave Airliecdd55a22007-07-11 16:32:08 +10001097 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 u32 *hw_status = dev_priv->hw_status_page;
1100 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001102 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Eric Anholt6c340ea2007-08-25 20:23:09 +10001104 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Eric Anholtc153f452007-09-03 12:06:45 +10001106 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -EINVAL;
1108
Eric Anholtc153f452007-09-03 12:06:45 +10001109 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1110 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Eric Anholtc153f452007-09-03 12:06:45 +10001112 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 sarea_priv->last_enqueue = dev_priv->counter - 1;
1115 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 return 0;
1118}
1119
Eric Anholtc153f452007-09-03 12:06:45 +10001120static int i810_rstatus(struct drm_device *dev, void *data,
1121 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001125 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Eric Anholtc153f452007-09-03 12:06:45 +10001128static int i810_ov0_info(struct drm_device *dev, void *data,
1129 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001132 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Eric Anholtc153f452007-09-03 12:06:45 +10001134 ov->offset = dev_priv->overlay_offset;
1135 ov->physical = dev_priv->overlay_physical;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return 0;
1138}
1139
Eric Anholtc153f452007-09-03 12:06:45 +10001140static int i810_fstatus(struct drm_device *dev, void *data,
1141 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001143 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Eric Anholt6c340ea2007-08-25 20:23:09 +10001145 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return I810_READ(0x30008);
1147}
1148
Eric Anholtc153f452007-09-03 12:06:45 +10001149static int i810_ov0_flip(struct drm_device *dev, void *data,
1150 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001152 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Eric Anholt6c340ea2007-08-25 20:23:09 +10001154 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001156 /* Tell the overlay to update */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001157 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 return 0;
1160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001164static void i810_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001167
Márton Németh3e684ea2008-01-24 15:58:57 +10001168 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 dev_priv->page_flipping = 1;
1170 dev_priv->current_page = 0;
1171 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1172}
1173
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001174static int i810_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 drm_i810_private_t *dev_priv = dev->dev_private;
1177
Márton Németh3e684ea2008-01-24 15:58:57 +10001178 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 dev_priv->page_flipping = 0;
1183 return 0;
1184}
1185
Eric Anholtc153f452007-09-03 12:06:45 +10001186static int i810_flip_bufs(struct drm_device *dev, void *data,
1187 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 drm_i810_private_t *dev_priv = dev->dev_private;
1190
Márton Németh3e684ea2008-01-24 15:58:57 +10001191 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Eric Anholt6c340ea2007-08-25 20:23:09 +10001193 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001195 if (!dev_priv->page_flipping)
1196 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001198 i810_dma_dispatch_flip(dev);
1199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Dave Airlieeddca552007-07-11 16:09:54 +10001202int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001203{
1204 /* i810 has 4 more counters */
1205 dev->counters += 4;
1206 dev->types[6] = _DRM_STAT_IRQ;
1207 dev->types[7] = _DRM_STAT_PRIMARY;
1208 dev->types[8] = _DRM_STAT_SECONDARY;
1209 dev->types[9] = _DRM_STAT_DMA;
1210
1211 return 0;
1212}
1213
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001214void i810_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001216 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001219void i810_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 if (dev->dev_private) {
1222 drm_i810_private_t *dev_priv = dev->dev_private;
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001223 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 i810_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Daniel Vetter87499ff2011-10-25 23:51:24 +02001227 if (file_priv->master && file_priv->master->lock.hw_lock) {
1228 drm_idlelock_take(&file_priv->master->lock);
1229 i810_driver_reclaim_buffers(dev, file_priv);
1230 drm_idlelock_release(&file_priv->master->lock);
1231 } else {
1232 /* master disappeared, clean up stuff anyway and hope nothing
1233 * goes wrong */
1234 i810_driver_reclaim_buffers(dev, file_priv);
1235 }
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001239int i810_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001241 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return 0;
1243}
1244
Eric Anholtc153f452007-09-03 12:06:45 +10001245struct drm_ioctl_desc i810_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001246 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1247 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1248 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1249 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1250 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1251 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1252 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1253 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1254 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1255 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1256 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1257 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1258 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1259 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1260 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261};
1262
1263int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001264
1265/**
1266 * Determine if the device really is AGP or not.
1267 *
1268 * All Intel graphics chipsets are treated as AGP, even if they are really
1269 * PCI-e.
1270 *
1271 * \param dev The device to be tested.
1272 *
1273 * \returns
1274 * A value of 1 is always retured to indictate every i810 is AGP.
1275 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001276int i810_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001277{
1278 return 1;
1279}