blob: 004ecdfe1b556769327e08cfc0e81280ab94568f [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
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/drmP.h>
34#include <drm/i810_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "i810_drv.h"
36#include <linux/interrupt.h> /* For task queue support */
37#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/pagemap.h>
40
41#define I810_BUF_FREE 2
42#define I810_BUF_CLIENT 1
Dave Airliebc5f4522007-11-05 12:50:58 +100043#define I810_BUF_HARDWARE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define I810_BUF_UNMAPPED 0
46#define I810_BUF_MAPPED 1
47
Dave Airlie056219e2007-07-11 16:17:42 +100048static struct drm_buf *i810_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Dave Airliecdd55a22007-07-11 16:32:08 +100050 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100051 int i;
52 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /* Linear search might not be the best solution */
55
Dave Airlieb5e89ed2005-09-25 14:28:13 +100056 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +100057 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 used = cmpxchg(buf_priv->in_use, I810_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 I810_BUF_CLIENT);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +020062 if (used == I810_BUF_FREE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68/* This should only be called if the buffer is not sent to the hardware
69 * yet, the hardware updates in use for us once its on the ring buffer.
70 */
71
Nicolas Kaiseraca791c2010-07-14 21:54:13 +020072static int i810_freelist_put(struct drm_device *dev, struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100074 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
75 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 /* In use is already a pointer */
78 used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_FREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (used != I810_BUF_CLIENT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100080 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
81 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
Dave Airlieb5e89ed2005-09-25 14:28:13 +100084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Dave Airliec94f7022005-07-07 21:03:38 +100087static int i810_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Dave Airlieeddca552007-07-11 16:09:54 +100089 struct drm_file *priv = filp->private_data;
90 struct drm_device *dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100091 drm_i810_private_t *dev_priv;
Dave Airlie056219e2007-07-11 16:17:42 +100092 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 drm_i810_buf_priv_t *buf_priv;
94
Dave Airlie2c14f282008-04-21 16:47:32 +100095 dev = priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 buf_priv = buf->dev_private;
99
100 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000102 buf_priv->currently_mapped = I810_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlie3d774612006-08-07 20:07:43 +1000105 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106 vma->vm_end - vma->vm_start, vma->vm_page_prot))
107 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800111static const struct file_operations i810_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000113 .release = drm_release,
Arnd Bergmann1f692a12011-01-25 23:17:15 +0100114 .unlocked_ioctl = drm_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000115 .mmap = i810_mmap_buffers,
116 .fasync = drm_fasync,
Keith Packard804d74a2012-07-09 15:40:07 -0700117#ifdef CONFIG_COMPAT
118 .compat_ioctl = drm_compat_ioctl,
119#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +0200120 .llseek = noop_llseek,
Dave Airliec94f7022005-07-07 21:03:38 +1000121};
122
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200123static int i810_map_buffer(struct drm_buf *buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Dave Airlie2c14f282008-04-21 16:47:32 +1000125 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800128 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int retcode = 0;
130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -EINVAL;
133
Linus Torvalds6be5ceb2012-04-20 17:13:58 -0700134 /* This is all entirely broken */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000135 old_fops = file_priv->filp->f_op;
136 file_priv->filp->f_op = &i810_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 dev_priv->mmap_buffer = buf;
Al Viro244ca2b2012-05-29 21:24:36 -0400138 buf_priv->virtual = (void *)vm_mmap(file_priv->filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139 PROT_READ | PROT_WRITE,
140 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000142 file_priv->filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000143 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* Real error */
145 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000146 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 buf_priv->virtual = NULL;
148 }
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
Al Virobfce2812012-04-20 21:57:04 -0400161 retcode = vm_munmap((unsigned long)buf_priv->virtual,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 (size_t) buf->total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
165 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 return retcode;
168}
169
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200170static int i810_dma_get_buffer(struct drm_device *dev, drm_i810_dma_t *d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000171 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Dave Airlie056219e2007-07-11 16:17:42 +1000173 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 drm_i810_buf_priv_t *buf_priv;
175 int retcode = 0;
176
177 buf = i810_freelist_get(dev);
178 if (!buf) {
179 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000180 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return retcode;
182 }
183
Eric Anholt6c340ea2007-08-25 20:23:09 +1000184 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (retcode) {
186 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000187 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return retcode;
189 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000190 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 buf_priv = buf->dev_private;
192 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 d->request_idx = buf->idx;
194 d->request_size = buf->total;
195 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 return retcode;
198}
199
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200200static int i810_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Dave Airliecdd55a22007-07-11 16:32:08 +1000202 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* Make sure interrupts are disabled here because the uninstall ioctl
205 * may not have been called from userspace and after dev_private
206 * is freed, it's too late.
207 */
208 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
209 drm_irq_uninstall(dev);
210
211 if (dev->dev_private) {
212 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 drm_i810_private_t *dev_priv =
214 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200216 if (dev_priv->ring.virtual_start)
Dave Airlieb9094d32007-01-08 21:31:13 +1100217 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 if (dev_priv->hw_status_page) {
219 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 dev_priv->hw_status_page,
221 dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700223 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000224 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000227 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100229
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000230 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100231 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0;
235}
236
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200237static int i810_wait_ring(struct drm_device *dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238{
239 drm_i810_private_t *dev_priv = dev->dev_private;
240 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
241 int iters = 0;
242 unsigned long end;
243 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
244
245 end = jiffies + (HZ * 3);
246 while (ring->space < n) {
247 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
248 ring->space = ring->head - (ring->tail + 8);
249 if (ring->space < 0)
250 ring->space += ring->Size;
251
252 if (ring->head != last_head) {
253 end = jiffies + (HZ * 3);
254 last_head = ring->head;
255 }
256
257 iters++;
258 if (time_before(end, jiffies)) {
259 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
260 DRM_ERROR("lockup\n");
261 goto out_wait_ring;
262 }
263 udelay(1);
264 }
265
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200266out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000267 return iters;
268}
269
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200270static void i810_kernel_lost_context(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000271{
272 drm_i810_private_t *dev_priv = dev->dev_private;
273 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
274
275 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
276 ring->tail = I810_READ(LP_RING + RING_TAIL);
277 ring->space = ring->head - (ring->tail + 8);
278 if (ring->space < 0)
279 ring->space += ring->Size;
280}
281
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200282static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283{
Dave Airliecdd55a22007-07-11 16:32:08 +1000284 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 int my_idx = 24;
286 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
287 int i;
288
289 if (dma->buf_count > 1019) {
290 /* Not enough space in the status page for the freelist */
291 return -EINVAL;
292 }
293
294 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000295 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000296 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
297
298 buf_priv->in_use = hw_status++;
299 buf_priv->my_use_idx = my_idx;
300 my_idx += 4;
301
302 *buf_priv->in_use = I810_BUF_FREE;
303
Dave Airlieb9094d32007-01-08 21:31:13 +1100304 buf_priv->map.offset = buf->bus_address;
305 buf_priv->map.size = buf->total;
306 buf_priv->map.type = _DRM_AGP;
307 buf_priv->map.flags = 0;
308 buf_priv->map.mtrr = 0;
309
310 drm_core_ioremap(&buf_priv->map, dev);
311 buf_priv->kernel_virtual = buf_priv->map.handle;
312
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 }
314 return 0;
315}
316
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200317static int i810_dma_initialize(struct drm_device *dev,
318 drm_i810_private_t *dev_priv,
319 drm_i810_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Dave Airlie55910512007-07-11 16:53:40 +1000321 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000322 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Dave Airliebd1b3312007-05-26 05:01:51 +1000324 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (r_list->map &&
326 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 break;
330 }
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!dev_priv->sarea_map) {
333 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334 i810_dma_cleanup(dev);
335 DRM_ERROR("can not find sarea!\n");
336 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
339 if (!dev_priv->mmio_map) {
340 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 i810_dma_cleanup(dev);
342 DRM_ERROR("can not find mmio map!\n");
343 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000345 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
347 if (!dev->agp_buffer_map) {
348 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 i810_dma_cleanup(dev);
350 DRM_ERROR("can not find dma buffer map!\n");
351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
354 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000357 dev_priv->ring.Start = init->ring_start;
358 dev_priv->ring.End = init->ring_end;
359 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Dave Airlieb9094d32007-01-08 21:31:13 +1100361 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
362 dev_priv->ring.map.size = init->ring_size;
363 dev_priv->ring.map.type = _DRM_AGP;
364 dev_priv->ring.map.flags = 0;
365 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Dave Airlieb9094d32007-01-08 21:31:13 +1100367 drm_core_ioremap(&dev_priv->ring.map, dev);
368
369 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000370 dev->dev_private = (void *)dev_priv;
371 i810_dma_cleanup(dev);
372 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000374 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
Dave Airlieb9094d32007-01-08 21:31:13 +1100377 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
378
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000379 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 dev_priv->w = init->w;
382 dev_priv->h = init->h;
383 dev_priv->pitch = init->pitch;
384 dev_priv->back_offset = init->back_offset;
385 dev_priv->depth_offset = init->depth_offset;
386 dev_priv->front_offset = init->front_offset;
387
388 dev_priv->overlay_offset = init->overlay_offset;
389 dev_priv->overlay_physical = init->overlay_physical;
390
391 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
392 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
393 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
394
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 /* Program Hardware Status Page */
396 dev_priv->hw_status_page =
397 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
398 &dev_priv->dma_status_page);
399 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dev->dev_private = (void *)dev_priv;
401 i810_dma_cleanup(dev);
402 DRM_ERROR("Can not allocate hardware status page\n");
403 return -ENOMEM;
404 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000405 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
406 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000409 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (i810_freelist_init(dev, dev_priv) != 0) {
413 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 i810_dma_cleanup(dev);
415 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 dev->dev_private = (void *)dev_priv;
420
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Eric Anholtc153f452007-09-03 12:06:45 +1000424static int i810_dma_init(struct drm_device *dev, void *data,
425 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000427 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000428 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Eric Anholtc153f452007-09-03 12:06:45 +1000431 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000432 case I810_INIT_DMA_1_4:
433 DRM_INFO("Using v1.4 init.\n");
Eric Anholt9a298b22009-03-24 12:23:04 -0700434 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435 if (dev_priv == NULL)
436 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000437 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000440 case I810_CLEANUP_DMA:
441 DRM_INFO("DMA Cleanup\n");
442 retcode = i810_dma_cleanup(dev);
443 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000444 default:
445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/* Most efficient way to verify state for the i810 is as it is
452 * emitted. Non-conformant state is silently dropped.
453 *
454 * Use 'volatile' & local var tmp to force the emitted values to be
455 * identical to the verified ones.
456 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200457static void i810EmitContextVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000460 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int i, j = 0;
462 unsigned int tmp;
463 RING_LOCALS;
464
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000465 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 OUT_RING(GFX_OP_COLOR_FACTOR);
468 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470 OUT_RING(GFX_OP_STIPPLE);
471 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 tmp = code[i];
475
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 if ((tmp & (7 << 29)) == (3 << 29) &&
477 (tmp & (0x1f << 24)) < (0x1d << 24)) {
478 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000480 } else
481 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
484 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 ADVANCE_LP_RING();
488}
489
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200490static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int i, j = 0;
494 unsigned int tmp;
495 RING_LOCALS;
496
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000497 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000499 OUT_RING(GFX_OP_MAP_INFO);
500 OUT_RING(code[I810_TEXREG_MI1]);
501 OUT_RING(code[I810_TEXREG_MI2]);
502 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 tmp = code[i];
506
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 if ((tmp & (7 << 29)) == (3 << 29) &&
508 (tmp & (0x1f << 24)) < (0x1d << 24)) {
509 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000511 } else
512 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
515 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 ADVANCE_LP_RING();
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/* Need to do some additional checking when setting the dest buffer.
522 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200523static void i810EmitDestVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000526 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 unsigned int tmp;
528 RING_LOCALS;
529
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 tmp = code[I810_DESTREG_DI1];
533 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 OUT_RING(CMD_OP_DESTBUFFER_INFO);
535 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
538 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* invarient:
541 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000542 OUT_RING(CMD_OP_Z_BUFFER_INFO);
543 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 OUT_RING(GFX_OP_DESTBUFFER_VARS);
546 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 OUT_RING(GFX_OP_DRAWRECT_INFO);
549 OUT_RING(code[I810_DESTREG_DR1]);
550 OUT_RING(code[I810_DESTREG_DR2]);
551 OUT_RING(code[I810_DESTREG_DR3]);
552 OUT_RING(code[I810_DESTREG_DR4]);
553 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 ADVANCE_LP_RING();
556}
557
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200558static void i810EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 drm_i810_private_t *dev_priv = dev->dev_private;
561 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563
Márton Németh3e684ea2008-01-24 15:58:57 +1000564 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
569 }
570
571 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
574 }
575
576 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
579 }
580
581 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
584 }
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/* need to verify
588 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200589static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 unsigned int clear_color,
591 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000593 drm_i810_private_t *dev_priv = dev->dev_private;
594 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000596 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int pitch = dev_priv->pitch;
598 int cpp = 2;
599 int i;
600 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601
602 if (dev_priv->current_page == 1) {
603 unsigned int tmp = flags;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 if (tmp & I810_FRONT)
607 flags |= I810_BACK;
608 if (tmp & I810_BACK)
609 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000612 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 if (nbox > I810_NR_SAREA_CLIPRECTS)
615 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 unsigned int x = pbox->x1;
619 unsigned int y = pbox->y1;
620 unsigned int width = (pbox->x2 - x) * cpp;
621 unsigned int height = pbox->y2 - y;
622 unsigned int start = y * pitch + x * cpp;
623
624 if (pbox->x1 > pbox->x2 ||
625 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000626 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 continue;
628
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 if (flags & I810_FRONT) {
630 BEGIN_LP_RING(6);
631 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
632 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
633 OUT_RING((height << 16) | width);
634 OUT_RING(start);
635 OUT_RING(clear_color);
636 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ADVANCE_LP_RING();
638 }
639
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 if (flags & I810_BACK) {
641 BEGIN_LP_RING(6);
642 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
643 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
644 OUT_RING((height << 16) | width);
645 OUT_RING(dev_priv->back_offset + start);
646 OUT_RING(clear_color);
647 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ADVANCE_LP_RING();
649 }
650
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000651 if (flags & I810_DEPTH) {
652 BEGIN_LP_RING(6);
653 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
654 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
655 OUT_RING((height << 16) | width);
656 OUT_RING(dev_priv->depth_offset + start);
657 OUT_RING(clear_zval);
658 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 ADVANCE_LP_RING();
660 }
661 }
662}
663
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200664static void i810_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 drm_i810_private_t *dev_priv = dev->dev_private;
667 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000669 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 int pitch = dev_priv->pitch;
671 int cpp = 2;
672 int i;
673 RING_LOCALS;
674
675 DRM_DEBUG("swapbuffers\n");
676
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000679 if (nbox > I810_NR_SAREA_CLIPRECTS)
680 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 unsigned int w = pbox->x2 - pbox->x1;
684 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 unsigned int start = dst;
687
688 if (pbox->x1 > pbox->x2 ||
689 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 continue;
692
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 BEGIN_LP_RING(6);
694 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
695 OUT_RING(pitch | (0xCC << 16));
696 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000698 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 OUT_RING(dev_priv->back_offset + start);
701 OUT_RING(pitch);
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->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ADVANCE_LP_RING();
707 }
708}
709
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200710static void i810_dma_dispatch_vertex(struct drm_device *dev,
711 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000713 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000716 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned long address = (unsigned long)buf->bus_address;
719 unsigned long start = address - dev->agp->base;
720 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 nbox = I810_NR_SAREA_CLIPRECTS;
727
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 used = 0;
730
731 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
735 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
736
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 *(u32 *) buf_priv->kernel_virtual =
738 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000741 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 used += 4;
743 }
744
745 i810_unmap_buffer(buf);
746 }
747
748 if (used) {
749 do {
750 if (i < nbox) {
751 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000752 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
753 SC_ENABLE);
754 OUT_RING(GFX_OP_SCISSOR_INFO);
755 OUT_RING(box[i].x1 | (box[i].y1 << 16));
756 OUT_RING((box[i].x2 -
757 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 ADVANCE_LP_RING();
759 }
760
761 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000762 OUT_RING(CMD_OP_BATCH_BUFFER);
763 OUT_RING(start | BB1_PROTECTED);
764 OUT_RING(start + used - 4);
765 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ADVANCE_LP_RING();
767
768 } while (++i < nbox);
769 }
770
771 if (discard) {
772 dev_priv->counter++;
773
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
775 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778 OUT_RING(CMD_STORE_DWORD_IDX);
779 OUT_RING(20);
780 OUT_RING(dev_priv->counter);
781 OUT_RING(CMD_STORE_DWORD_IDX);
782 OUT_RING(buf_priv->my_use_idx);
783 OUT_RING(I810_BUF_FREE);
784 OUT_RING(CMD_REPORT_HEAD);
785 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 ADVANCE_LP_RING();
787 }
788}
789
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200790static void i810_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 int pitch = dev_priv->pitch;
794 RING_LOCALS;
795
Márton Németh3e684ea2008-01-24 15:58:57 +1000796 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 dev_priv->current_page,
798 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 i810_kernel_lost_context(dev);
801
802 BEGIN_LP_RING(2);
803 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
804 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ADVANCE_LP_RING();
806
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* On i815 at least ASYNC is buggy */
809 /* pitch<<5 is from 11.2.8 p158,
810 its the pitch / 8 then left shifted 8,
811 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
813 if (dev_priv->current_page == 0) {
814 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 dev_priv->current_page = 1;
816 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 dev_priv->current_page = 0;
819 }
820 OUT_RING(0);
821 ADVANCE_LP_RING();
822
823 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
825 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ADVANCE_LP_RING();
827
828 /* Increment the frame counter. The client-side 3D driver must
829 * throttle the framerate by waiting for this value before
830 * performing the swapbuffer ioctl.
831 */
832 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
833
834}
835
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200836static void i810_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 drm_i810_private_t *dev_priv = dev->dev_private;
839 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000841 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 BEGIN_LP_RING(4);
844 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
845 OUT_RING(CMD_REPORT_HEAD);
846 OUT_RING(0);
847 OUT_RING(0);
848 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200853static int i810_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000856 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 int i, ret = 0;
858 RING_LOCALS;
859
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 BEGIN_LP_RING(2);
863 OUT_RING(CMD_REPORT_HEAD);
864 OUT_RING(0);
865 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000870 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
874 I810_BUF_FREE);
875
876 if (used == I810_BUF_HARDWARE)
877 DRM_DEBUG("reclaimed from HARDWARE\n");
878 if (used == I810_BUF_CLIENT)
879 DRM_DEBUG("still on client\n");
880 }
881
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885/* Must be called with the lock held */
Daniel Vetterd5346b32012-06-11 21:50:23 +0200886void i810_driver_reclaim_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000887 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Dave Airliecdd55a22007-07-11 16:32:08 +1000889 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 if (!dma)
893 return;
894 if (!dev->dev_private)
895 return;
896 if (!dma->buflist)
897 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000902 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000903 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Eric Anholt6c340ea2007-08-25 20:23:09 +1000905 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
907 I810_BUF_FREE);
908
909 if (used == I810_BUF_CLIENT)
910 DRM_DEBUG("reclaimed from client\n");
911 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914 }
915}
916
Eric Anholtc153f452007-09-03 12:06:45 +1000917static int i810_flush_ioctl(struct drm_device *dev, void *data,
918 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000920 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000922 i810_flush_queue(dev);
923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Eric Anholtc153f452007-09-03 12:06:45 +1000926static int i810_dma_vertex(struct drm_device *dev, void *data,
927 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Dave Airliecdd55a22007-07-11 16:32:08 +1000929 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
931 u32 *hw_status = dev_priv->hw_status_page;
932 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
933 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000934 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Eric Anholt6c340ea2007-08-25 20:23:09 +1000936 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Márton Németh3e684ea2008-01-24 15:58:57 +1000938 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000939 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Eric Anholtc153f452007-09-03 12:06:45 +1000941 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return -EINVAL;
943
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000944 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000945 dma->buflist[vertex->idx],
946 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Eric Anholtc153f452007-09-03 12:06:45 +1000948 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 sarea_priv->last_enqueue = dev_priv->counter - 1;
951 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 return 0;
954}
955
Eric Anholtc153f452007-09-03 12:06:45 +1000956static int i810_clear_bufs(struct drm_device *dev, void *data,
957 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Eric Anholtc153f452007-09-03 12:06:45 +1000959 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Eric Anholt6c340ea2007-08-25 20:23:09 +1000961 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 /* GH: Someone's doing nasty things... */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200964 if (!dev->dev_private)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000965 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Eric Anholtc153f452007-09-03 12:06:45 +1000967 i810_dma_dispatch_clear(dev, clear->flags,
968 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Eric Anholtc153f452007-09-03 12:06:45 +1000972static int i810_swap_bufs(struct drm_device *dev, void *data,
973 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Márton Németh3e684ea2008-01-24 15:58:57 +1000975 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Eric Anholt6c340ea2007-08-25 20:23:09 +1000977 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 i810_dma_dispatch_swap(dev);
980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Eric Anholtc153f452007-09-03 12:06:45 +1000983static int i810_getage(struct drm_device *dev, void *data,
984 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
987 u32 *hw_status = dev_priv->hw_status_page;
988 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
989 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000991 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
993}
994
Eric Anholtc153f452007-09-03 12:06:45 +1000995static int i810_getbuf(struct drm_device *dev, void *data,
996 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000998 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +1000999 drm_i810_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001000 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1001 u32 *hw_status = dev_priv->hw_status_page;
1002 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1003 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Eric Anholt6c340ea2007-08-25 20:23:09 +10001005 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Eric Anholtc153f452007-09-03 12:06:45 +10001007 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Eric Anholtc153f452007-09-03 12:06:45 +10001009 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001012 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 return retcode;
1017}
1018
Eric Anholtc153f452007-09-03 12:06:45 +10001019static int i810_copybuf(struct drm_device *dev, void *data,
1020 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 /* Never copy - 2.4.x doesn't need it */
1023 return 0;
1024}
1025
Eric Anholtc153f452007-09-03 12:06:45 +10001026static int i810_docopy(struct drm_device *dev, void *data,
1027 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 /* Never copy - 2.4.x doesn't need it */
1030 return 0;
1031}
1032
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001033static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 drm_i810_private_t *dev_priv = dev->dev_private;
1037 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1038 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1039 unsigned long address = (unsigned long)buf->bus_address;
1040 unsigned long start = address - dev->agp->base;
1041 int u;
1042 RING_LOCALS;
1043
1044 i810_kernel_lost_context(dev);
1045
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001047 if (u != I810_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 DRM_DEBUG("MC found buffer that isn't mine!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 used = 0;
1052
1053 sarea_priv->dirty = 0x7f;
1054
Márton Németh3e684ea2008-01-24 15:58:57 +10001055 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 dev_priv->counter++;
1058 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 DRM_DEBUG("start : %lx\n", start);
1060 DRM_DEBUG("used : %d\n", used);
1061 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1062
1063 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1064 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001065 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 used += 4;
1067 }
1068
1069 i810_unmap_buffer(buf);
1070 }
1071 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 OUT_RING(CMD_OP_BATCH_BUFFER);
1073 OUT_RING(start | BB1_PROTECTED);
1074 OUT_RING(start + used - 4);
1075 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ADVANCE_LP_RING();
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001079 OUT_RING(CMD_STORE_DWORD_IDX);
1080 OUT_RING(buf_priv->my_use_idx);
1081 OUT_RING(I810_BUF_FREE);
1082 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 OUT_RING(CMD_STORE_DWORD_IDX);
1085 OUT_RING(16);
1086 OUT_RING(last_render);
1087 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 ADVANCE_LP_RING();
1089}
1090
Eric Anholtc153f452007-09-03 12:06:45 +10001091static int i810_dma_mc(struct drm_device *dev, void *data,
1092 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Dave Airliecdd55a22007-07-11 16:32:08 +10001094 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 u32 *hw_status = dev_priv->hw_status_page;
1097 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001099 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Eric Anholt6c340ea2007-08-25 20:23:09 +10001101 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Eric Anholtc153f452007-09-03 12:06:45 +10001103 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -EINVAL;
1105
Eric Anholtc153f452007-09-03 12:06:45 +10001106 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1107 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Eric Anholtc153f452007-09-03 12:06:45 +10001109 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001111 sarea_priv->last_enqueue = dev_priv->counter - 1;
1112 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 return 0;
1115}
1116
Eric Anholtc153f452007-09-03 12:06:45 +10001117static int i810_rstatus(struct drm_device *dev, void *data,
1118 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001122 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
Eric Anholtc153f452007-09-03 12:06:45 +10001125static int i810_ov0_info(struct drm_device *dev, void *data,
1126 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001129 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Eric Anholtc153f452007-09-03 12:06:45 +10001131 ov->offset = dev_priv->overlay_offset;
1132 ov->physical = dev_priv->overlay_physical;
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
1135}
1136
Eric Anholtc153f452007-09-03 12:06:45 +10001137static int i810_fstatus(struct drm_device *dev, void *data,
1138 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001140 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Eric Anholt6c340ea2007-08-25 20:23:09 +10001142 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return I810_READ(0x30008);
1144}
1145
Eric Anholtc153f452007-09-03 12:06:45 +10001146static int i810_ov0_flip(struct drm_device *dev, void *data,
1147 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001149 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Eric Anholt6c340ea2007-08-25 20:23:09 +10001151 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001153 /* Tell the overlay to update */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001154 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 return 0;
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001160 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001161static void i810_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001164
Márton Németh3e684ea2008-01-24 15:58:57 +10001165 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 dev_priv->page_flipping = 1;
1167 dev_priv->current_page = 0;
1168 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1169}
1170
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001171static int i810_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 drm_i810_private_t *dev_priv = dev->dev_private;
1174
Márton Németh3e684ea2008-01-24 15:58:57 +10001175 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001177 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 dev_priv->page_flipping = 0;
1180 return 0;
1181}
1182
Eric Anholtc153f452007-09-03 12:06:45 +10001183static int i810_flip_bufs(struct drm_device *dev, void *data,
1184 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 drm_i810_private_t *dev_priv = dev->dev_private;
1187
Márton Németh3e684ea2008-01-24 15:58:57 +10001188 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Eric Anholt6c340ea2007-08-25 20:23:09 +10001190 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001192 if (!dev_priv->page_flipping)
1193 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001195 i810_dma_dispatch_flip(dev);
1196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Dave Airlieeddca552007-07-11 16:09:54 +10001199int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001200{
1201 /* i810 has 4 more counters */
1202 dev->counters += 4;
1203 dev->types[6] = _DRM_STAT_IRQ;
1204 dev->types[7] = _DRM_STAT_PRIMARY;
1205 dev->types[8] = _DRM_STAT_SECONDARY;
1206 dev->types[9] = _DRM_STAT_DMA;
1207
Dave Airlie466e69b2011-12-19 11:15:29 +00001208 pci_set_master(dev->pdev);
1209
Dave Airlie22eae942005-11-10 22:16:34 +11001210 return 0;
1211}
1212
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001213void i810_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001215 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001218void i810_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 if (dev->dev_private) {
1221 drm_i810_private_t *dev_priv = dev->dev_private;
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001222 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 i810_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Daniel Vetterd5346b32012-06-11 21:50:23 +02001226 if (file_priv->master && file_priv->master->lock.hw_lock) {
1227 drm_idlelock_take(&file_priv->master->lock);
1228 i810_driver_reclaim_buffers(dev, file_priv);
1229 drm_idlelock_release(&file_priv->master->lock);
1230 } else {
1231 /* master disappeared, clean up stuff anyway and hope nothing
1232 * goes wrong */
1233 i810_driver_reclaim_buffers(dev, file_priv);
1234 }
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001238int i810_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001240 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
1242}
1243
Eric Anholtc153f452007-09-03 12:06:45 +10001244struct drm_ioctl_desc i810_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001245 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1246 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1247 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1248 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1249 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1250 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1251 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1252 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1253 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1254 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1255 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1256 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1257 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1258 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1259 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260};
1261
1262int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001263
1264/**
1265 * Determine if the device really is AGP or not.
1266 *
1267 * All Intel graphics chipsets are treated as AGP, even if they are really
1268 * PCI-e.
1269 *
1270 * \param dev The device to be tested.
1271 *
1272 * \returns
1273 * A value of 1 is always retured to indictate every i810 is AGP.
1274 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001275int i810_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001276{
1277 return 1;
1278}