blob: a4ba453b3d273b72126f6827cda13065cea69da4 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103 buf_priv->currently_mapped = I810_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlie3d774612006-08-07 20:07:43 +1000106 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000107 vma->vm_end - vma->vm_start, vma->vm_page_prot))
108 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 0;
110}
111
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800112static const struct file_operations i810_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000113 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000114 .release = drm_release,
Arnd Bergmann1f692a12011-01-25 23:17:15 +0100115 .unlocked_ioctl = drm_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 .mmap = i810_mmap_buffers,
117 .fasync = drm_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200118 .llseek = noop_llseek,
Dave Airliec94f7022005-07-07 21:03:38 +1000119};
120
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200121static int i810_map_buffer(struct drm_buf *buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Dave Airlie2c14f282008-04-21 16:47:32 +1000123 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000125 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800126 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int retcode = 0;
128
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -EINVAL;
131
Linus Torvalds6be5ceb2012-04-20 17:13:58 -0700132 /* This is all entirely broken */
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
Linus Torvaldsa46ef992012-04-20 16:20:01 -0700161 retcode = vm_munmap(current->mm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 (unsigned long)buf_priv->virtual,
163 (size_t) buf->total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000165 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
166 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 return retcode;
169}
170
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200171static int i810_dma_get_buffer(struct drm_device *dev, drm_i810_dma_t *d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000172 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Dave Airlie056219e2007-07-11 16:17:42 +1000174 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 drm_i810_buf_priv_t *buf_priv;
176 int retcode = 0;
177
178 buf = i810_freelist_get(dev);
179 if (!buf) {
180 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return retcode;
183 }
184
Eric Anholt6c340ea2007-08-25 20:23:09 +1000185 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (retcode) {
187 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return retcode;
190 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000191 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 buf_priv = buf->dev_private;
193 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 d->request_idx = buf->idx;
195 d->request_size = buf->total;
196 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 return retcode;
199}
200
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200201static int i810_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Dave Airliecdd55a22007-07-11 16:32:08 +1000203 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* Make sure interrupts are disabled here because the uninstall ioctl
206 * may not have been called from userspace and after dev_private
207 * is freed, it's too late.
208 */
209 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
210 drm_irq_uninstall(dev);
211
212 if (dev->dev_private) {
213 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000214 drm_i810_private_t *dev_priv =
215 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200217 if (dev_priv->ring.virtual_start)
Dave Airlieb9094d32007-01-08 21:31:13 +1100218 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219 if (dev_priv->hw_status_page) {
220 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dev_priv->hw_status_page,
222 dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700224 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000225 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000228 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100230
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100232 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236}
237
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200238static int i810_wait_ring(struct drm_device *dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239{
240 drm_i810_private_t *dev_priv = dev->dev_private;
241 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
242 int iters = 0;
243 unsigned long end;
244 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
245
246 end = jiffies + (HZ * 3);
247 while (ring->space < n) {
248 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
249 ring->space = ring->head - (ring->tail + 8);
250 if (ring->space < 0)
251 ring->space += ring->Size;
252
253 if (ring->head != last_head) {
254 end = jiffies + (HZ * 3);
255 last_head = ring->head;
256 }
257
258 iters++;
259 if (time_before(end, jiffies)) {
260 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
261 DRM_ERROR("lockup\n");
262 goto out_wait_ring;
263 }
264 udelay(1);
265 }
266
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200267out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268 return iters;
269}
270
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200271static void i810_kernel_lost_context(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272{
273 drm_i810_private_t *dev_priv = dev->dev_private;
274 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
275
276 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
277 ring->tail = I810_READ(LP_RING + RING_TAIL);
278 ring->space = ring->head - (ring->tail + 8);
279 if (ring->space < 0)
280 ring->space += ring->Size;
281}
282
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200283static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284{
Dave Airliecdd55a22007-07-11 16:32:08 +1000285 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286 int my_idx = 24;
287 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
288 int i;
289
290 if (dma->buf_count > 1019) {
291 /* Not enough space in the status page for the freelist */
292 return -EINVAL;
293 }
294
295 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000296 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
298
299 buf_priv->in_use = hw_status++;
300 buf_priv->my_use_idx = my_idx;
301 my_idx += 4;
302
303 *buf_priv->in_use = I810_BUF_FREE;
304
Dave Airlieb9094d32007-01-08 21:31:13 +1100305 buf_priv->map.offset = buf->bus_address;
306 buf_priv->map.size = buf->total;
307 buf_priv->map.type = _DRM_AGP;
308 buf_priv->map.flags = 0;
309 buf_priv->map.mtrr = 0;
310
311 drm_core_ioremap(&buf_priv->map, dev);
312 buf_priv->kernel_virtual = buf_priv->map.handle;
313
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000314 }
315 return 0;
316}
317
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200318static int i810_dma_initialize(struct drm_device *dev,
319 drm_i810_private_t *dev_priv,
320 drm_i810_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Dave Airlie55910512007-07-11 16:53:40 +1000322 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Dave Airliebd1b3312007-05-26 05:01:51 +1000325 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (r_list->map &&
327 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000328 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000330 break;
331 }
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!dev_priv->sarea_map) {
334 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 i810_dma_cleanup(dev);
336 DRM_ERROR("can not find sarea!\n");
337 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
340 if (!dev_priv->mmio_map) {
341 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 i810_dma_cleanup(dev);
343 DRM_ERROR("can not find mmio map!\n");
344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000346 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
348 if (!dev->agp_buffer_map) {
349 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 i810_dma_cleanup(dev);
351 DRM_ERROR("can not find dma buffer map!\n");
352 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
355 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000356 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 dev_priv->ring.Start = init->ring_start;
359 dev_priv->ring.End = init->ring_end;
360 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Dave Airlieb9094d32007-01-08 21:31:13 +1100362 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
363 dev_priv->ring.map.size = init->ring_size;
364 dev_priv->ring.map.type = _DRM_AGP;
365 dev_priv->ring.map.flags = 0;
366 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Dave Airlieb9094d32007-01-08 21:31:13 +1100368 drm_core_ioremap(&dev_priv->ring.map, dev);
369
370 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 dev->dev_private = (void *)dev_priv;
372 i810_dma_cleanup(dev);
373 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000375 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Dave Airlieb9094d32007-01-08 21:31:13 +1100378 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
379
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 dev_priv->w = init->w;
383 dev_priv->h = init->h;
384 dev_priv->pitch = init->pitch;
385 dev_priv->back_offset = init->back_offset;
386 dev_priv->depth_offset = init->depth_offset;
387 dev_priv->front_offset = init->front_offset;
388
389 dev_priv->overlay_offset = init->overlay_offset;
390 dev_priv->overlay_physical = init->overlay_physical;
391
392 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
393 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
394 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
395
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396 /* Program Hardware Status Page */
397 dev_priv->hw_status_page =
398 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
399 &dev_priv->dma_status_page);
400 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dev->dev_private = (void *)dev_priv;
402 i810_dma_cleanup(dev);
403 DRM_ERROR("Can not allocate hardware status page\n");
404 return -ENOMEM;
405 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000406 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
407 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (i810_freelist_init(dev, dev_priv) != 0) {
414 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000415 i810_dma_cleanup(dev);
416 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 dev->dev_private = (void *)dev_priv;
421
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Eric Anholtc153f452007-09-03 12:06:45 +1000425static int i810_dma_init(struct drm_device *dev, void *data,
426 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000428 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000429 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000430 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Eric Anholtc153f452007-09-03 12:06:45 +1000432 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000433 case I810_INIT_DMA_1_4:
434 DRM_INFO("Using v1.4 init.\n");
Eric Anholt9a298b22009-03-24 12:23:04 -0700435 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000436 if (dev_priv == NULL)
437 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000438 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 case I810_CLEANUP_DMA:
442 DRM_INFO("DMA Cleanup\n");
443 retcode = i810_dma_cleanup(dev);
444 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000445 default:
446 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000449 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/* Most efficient way to verify state for the i810 is as it is
453 * emitted. Non-conformant state is silently dropped.
454 *
455 * Use 'volatile' & local var tmp to force the emitted values to be
456 * identical to the verified ones.
457 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200458static void i810EmitContextVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000459 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int i, j = 0;
463 unsigned int tmp;
464 RING_LOCALS;
465
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 OUT_RING(GFX_OP_COLOR_FACTOR);
469 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000471 OUT_RING(GFX_OP_STIPPLE);
472 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 tmp = code[i];
476
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 if ((tmp & (7 << 29)) == (3 << 29) &&
478 (tmp & (0x1f << 24)) < (0x1d << 24)) {
479 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 } else
482 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484
485 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000486 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 ADVANCE_LP_RING();
489}
490
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200491static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int i, j = 0;
495 unsigned int tmp;
496 RING_LOCALS;
497
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000498 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000500 OUT_RING(GFX_OP_MAP_INFO);
501 OUT_RING(code[I810_TEXREG_MI1]);
502 OUT_RING(code[I810_TEXREG_MI2]);
503 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 tmp = code[i];
507
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000508 if ((tmp & (7 << 29)) == (3 << 29) &&
509 (tmp & (0x1f << 24)) < (0x1d << 24)) {
510 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000512 } else
513 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000517 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 ADVANCE_LP_RING();
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/* Need to do some additional checking when setting the dest buffer.
523 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200524static void i810EmitDestVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000525 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000527 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 unsigned int tmp;
529 RING_LOCALS;
530
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 tmp = code[I810_DESTREG_DI1];
534 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000535 OUT_RING(CMD_OP_DESTBUFFER_INFO);
536 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
539 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* invarient:
542 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 OUT_RING(CMD_OP_Z_BUFFER_INFO);
544 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 OUT_RING(GFX_OP_DESTBUFFER_VARS);
547 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 OUT_RING(GFX_OP_DRAWRECT_INFO);
550 OUT_RING(code[I810_DESTREG_DR1]);
551 OUT_RING(code[I810_DESTREG_DR2]);
552 OUT_RING(code[I810_DESTREG_DR3]);
553 OUT_RING(code[I810_DESTREG_DR4]);
554 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 ADVANCE_LP_RING();
557}
558
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200559static void i810EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000561 drm_i810_private_t *dev_priv = dev->dev_private;
562 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000564
Márton Németh3e684ea2008-01-24 15:58:57 +1000565 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
570 }
571
572 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000573 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
575 }
576
577 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
580 }
581
582 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
585 }
586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/* need to verify
589 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200590static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 unsigned int clear_color,
592 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 drm_i810_private_t *dev_priv = dev->dev_private;
595 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000597 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int pitch = dev_priv->pitch;
599 int cpp = 2;
600 int i;
601 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602
603 if (dev_priv->current_page == 1) {
604 unsigned int tmp = flags;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000607 if (tmp & I810_FRONT)
608 flags |= I810_BACK;
609 if (tmp & I810_BACK)
610 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 if (nbox > I810_NR_SAREA_CLIPRECTS)
616 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000618 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 unsigned int x = pbox->x1;
620 unsigned int y = pbox->y1;
621 unsigned int width = (pbox->x2 - x) * cpp;
622 unsigned int height = pbox->y2 - y;
623 unsigned int start = y * pitch + x * cpp;
624
625 if (pbox->x1 > pbox->x2 ||
626 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000627 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 continue;
629
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000630 if (flags & I810_FRONT) {
631 BEGIN_LP_RING(6);
632 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
633 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
634 OUT_RING((height << 16) | width);
635 OUT_RING(start);
636 OUT_RING(clear_color);
637 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 ADVANCE_LP_RING();
639 }
640
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641 if (flags & I810_BACK) {
642 BEGIN_LP_RING(6);
643 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
644 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
645 OUT_RING((height << 16) | width);
646 OUT_RING(dev_priv->back_offset + start);
647 OUT_RING(clear_color);
648 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 ADVANCE_LP_RING();
650 }
651
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 if (flags & I810_DEPTH) {
653 BEGIN_LP_RING(6);
654 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
655 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
656 OUT_RING((height << 16) | width);
657 OUT_RING(dev_priv->depth_offset + start);
658 OUT_RING(clear_zval);
659 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 ADVANCE_LP_RING();
661 }
662 }
663}
664
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200665static void i810_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 drm_i810_private_t *dev_priv = dev->dev_private;
668 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000670 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int pitch = dev_priv->pitch;
672 int cpp = 2;
673 int i;
674 RING_LOCALS;
675
676 DRM_DEBUG("swapbuffers\n");
677
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 if (nbox > I810_NR_SAREA_CLIPRECTS)
681 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned int w = pbox->x2 - pbox->x1;
685 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 unsigned int start = dst;
688
689 if (pbox->x1 > pbox->x2 ||
690 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 continue;
693
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 BEGIN_LP_RING(6);
695 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
696 OUT_RING(pitch | (0xCC << 16));
697 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 OUT_RING(dev_priv->back_offset + start);
702 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 ADVANCE_LP_RING();
708 }
709}
710
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200711static void i810_dma_dispatch_vertex(struct drm_device *dev,
712 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000717 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned long address = (unsigned long)buf->bus_address;
720 unsigned long start = address - dev->agp->base;
721 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000726 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 nbox = I810_NR_SAREA_CLIPRECTS;
728
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 used = 0;
731
732 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000733 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
736 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
737
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 *(u32 *) buf_priv->kernel_virtual =
739 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000742 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 used += 4;
744 }
745
746 i810_unmap_buffer(buf);
747 }
748
749 if (used) {
750 do {
751 if (i < nbox) {
752 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000753 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
754 SC_ENABLE);
755 OUT_RING(GFX_OP_SCISSOR_INFO);
756 OUT_RING(box[i].x1 | (box[i].y1 << 16));
757 OUT_RING((box[i].x2 -
758 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ADVANCE_LP_RING();
760 }
761
762 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 OUT_RING(CMD_OP_BATCH_BUFFER);
764 OUT_RING(start | BB1_PROTECTED);
765 OUT_RING(start + used - 4);
766 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 ADVANCE_LP_RING();
768
769 } while (++i < nbox);
770 }
771
772 if (discard) {
773 dev_priv->counter++;
774
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
776 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 OUT_RING(CMD_STORE_DWORD_IDX);
780 OUT_RING(20);
781 OUT_RING(dev_priv->counter);
782 OUT_RING(CMD_STORE_DWORD_IDX);
783 OUT_RING(buf_priv->my_use_idx);
784 OUT_RING(I810_BUF_FREE);
785 OUT_RING(CMD_REPORT_HEAD);
786 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 ADVANCE_LP_RING();
788 }
789}
790
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200791static void i810_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int pitch = dev_priv->pitch;
795 RING_LOCALS;
796
Márton Németh3e684ea2008-01-24 15:58:57 +1000797 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 dev_priv->current_page,
799 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 i810_kernel_lost_context(dev);
802
803 BEGIN_LP_RING(2);
804 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
805 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ADVANCE_LP_RING();
807
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /* On i815 at least ASYNC is buggy */
810 /* pitch<<5 is from 11.2.8 p158,
811 its the pitch / 8 then left shifted 8,
812 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
814 if (dev_priv->current_page == 0) {
815 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 dev_priv->current_page = 1;
817 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 dev_priv->current_page = 0;
820 }
821 OUT_RING(0);
822 ADVANCE_LP_RING();
823
824 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
826 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 ADVANCE_LP_RING();
828
829 /* Increment the frame counter. The client-side 3D driver must
830 * throttle the framerate by waiting for this value before
831 * performing the swapbuffer ioctl.
832 */
833 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
834
835}
836
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200837static void i810_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 drm_i810_private_t *dev_priv = dev->dev_private;
840 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 BEGIN_LP_RING(4);
845 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
846 OUT_RING(CMD_REPORT_HEAD);
847 OUT_RING(0);
848 OUT_RING(0);
849 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200854static int i810_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000857 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 int i, ret = 0;
859 RING_LOCALS;
860
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 BEGIN_LP_RING(2);
864 OUT_RING(CMD_REPORT_HEAD);
865 OUT_RING(0);
866 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000871 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000872 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
875 I810_BUF_FREE);
876
877 if (used == I810_BUF_HARDWARE)
878 DRM_DEBUG("reclaimed from HARDWARE\n");
879 if (used == I810_BUF_CLIENT)
880 DRM_DEBUG("still on client\n");
881 }
882
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886/* Must be called with the lock held */
Daniel Vetter6e877b52012-01-29 17:05:52 +0100887static void i810_reclaim_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000888 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Dave Airliecdd55a22007-07-11 16:32:08 +1000890 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 if (!dma)
894 return;
895 if (!dev->dev_private)
896 return;
897 if (!dma->buflist)
898 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000903 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000904 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Eric Anholt6c340ea2007-08-25 20:23:09 +1000906 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
908 I810_BUF_FREE);
909
910 if (used == I810_BUF_CLIENT)
911 DRM_DEBUG("reclaimed from client\n");
912 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000913 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 }
916}
917
Eric Anholtc153f452007-09-03 12:06:45 +1000918static int i810_flush_ioctl(struct drm_device *dev, void *data,
919 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000921 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923 i810_flush_queue(dev);
924 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Eric Anholtc153f452007-09-03 12:06:45 +1000927static int i810_dma_vertex(struct drm_device *dev, void *data,
928 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Dave Airliecdd55a22007-07-11 16:32:08 +1000930 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
932 u32 *hw_status = dev_priv->hw_status_page;
933 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
934 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000935 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Eric Anholt6c340ea2007-08-25 20:23:09 +1000937 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Márton Németh3e684ea2008-01-24 15:58:57 +1000939 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000940 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Eric Anholtc153f452007-09-03 12:06:45 +1000942 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -EINVAL;
944
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000945 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000946 dma->buflist[vertex->idx],
947 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Eric Anholtc153f452007-09-03 12:06:45 +1000949 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 sarea_priv->last_enqueue = dev_priv->counter - 1;
952 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 return 0;
955}
956
Eric Anholtc153f452007-09-03 12:06:45 +1000957static int i810_clear_bufs(struct drm_device *dev, void *data,
958 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Eric Anholtc153f452007-09-03 12:06:45 +1000960 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Eric Anholt6c340ea2007-08-25 20:23:09 +1000962 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000964 /* GH: Someone's doing nasty things... */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200965 if (!dev->dev_private)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000966 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Eric Anholtc153f452007-09-03 12:06:45 +1000968 i810_dma_dispatch_clear(dev, clear->flags,
969 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Eric Anholtc153f452007-09-03 12:06:45 +1000973static int i810_swap_bufs(struct drm_device *dev, void *data,
974 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Márton Németh3e684ea2008-01-24 15:58:57 +1000976 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Eric Anholt6c340ea2007-08-25 20:23:09 +1000978 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000980 i810_dma_dispatch_swap(dev);
981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Eric Anholtc153f452007-09-03 12:06:45 +1000984static int i810_getage(struct drm_device *dev, void *data,
985 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
988 u32 *hw_status = dev_priv->hw_status_page;
989 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
990 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return 0;
994}
995
Eric Anholtc153f452007-09-03 12:06:45 +1000996static int i810_getbuf(struct drm_device *dev, void *data,
997 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001000 drm_i810_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1002 u32 *hw_status = dev_priv->hw_status_page;
1003 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1004 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Eric Anholt6c340ea2007-08-25 20:23:09 +10001006 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Eric Anholtc153f452007-09-03 12:06:45 +10001008 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Eric Anholtc153f452007-09-03 12:06:45 +10001010 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001013 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001015 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 return retcode;
1018}
1019
Eric Anholtc153f452007-09-03 12:06:45 +10001020static int i810_copybuf(struct drm_device *dev, void *data,
1021 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 /* Never copy - 2.4.x doesn't need it */
1024 return 0;
1025}
1026
Eric Anholtc153f452007-09-03 12:06:45 +10001027static int i810_docopy(struct drm_device *dev, void *data,
1028 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 /* Never copy - 2.4.x doesn't need it */
1031 return 0;
1032}
1033
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001034static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001035 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 drm_i810_private_t *dev_priv = dev->dev_private;
1038 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1039 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1040 unsigned long address = (unsigned long)buf->bus_address;
1041 unsigned long start = address - dev->agp->base;
1042 int u;
1043 RING_LOCALS;
1044
1045 i810_kernel_lost_context(dev);
1046
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001048 if (u != I810_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 DRM_DEBUG("MC found buffer that isn't mine!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 used = 0;
1053
1054 sarea_priv->dirty = 0x7f;
1055
Márton Németh3e684ea2008-01-24 15:58:57 +10001056 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 dev_priv->counter++;
1059 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 DRM_DEBUG("start : %lx\n", start);
1061 DRM_DEBUG("used : %d\n", used);
1062 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1063
1064 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1065 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001066 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 used += 4;
1068 }
1069
1070 i810_unmap_buffer(buf);
1071 }
1072 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 OUT_RING(CMD_OP_BATCH_BUFFER);
1074 OUT_RING(start | BB1_PROTECTED);
1075 OUT_RING(start + used - 4);
1076 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 ADVANCE_LP_RING();
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 OUT_RING(CMD_STORE_DWORD_IDX);
1081 OUT_RING(buf_priv->my_use_idx);
1082 OUT_RING(I810_BUF_FREE);
1083 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 OUT_RING(CMD_STORE_DWORD_IDX);
1086 OUT_RING(16);
1087 OUT_RING(last_render);
1088 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 ADVANCE_LP_RING();
1090}
1091
Eric Anholtc153f452007-09-03 12:06:45 +10001092static int i810_dma_mc(struct drm_device *dev, void *data,
1093 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Dave Airliecdd55a22007-07-11 16:32:08 +10001095 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 u32 *hw_status = dev_priv->hw_status_page;
1098 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001100 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Eric Anholt6c340ea2007-08-25 20:23:09 +10001102 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Eric Anholtc153f452007-09-03 12:06:45 +10001104 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -EINVAL;
1106
Eric Anholtc153f452007-09-03 12:06:45 +10001107 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1108 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Eric Anholtc153f452007-09-03 12:06:45 +10001110 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 sarea_priv->last_enqueue = dev_priv->counter - 1;
1113 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 return 0;
1116}
1117
Eric Anholtc153f452007-09-03 12:06:45 +10001118static int i810_rstatus(struct drm_device *dev, void *data,
1119 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001121 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
Eric Anholtc153f452007-09-03 12:06:45 +10001126static int i810_ov0_info(struct drm_device *dev, void *data,
1127 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001130 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Eric Anholtc153f452007-09-03 12:06:45 +10001132 ov->offset = dev_priv->overlay_offset;
1133 ov->physical = dev_priv->overlay_physical;
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return 0;
1136}
1137
Eric Anholtc153f452007-09-03 12:06:45 +10001138static int i810_fstatus(struct drm_device *dev, void *data,
1139 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Eric Anholt6c340ea2007-08-25 20:23:09 +10001143 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return I810_READ(0x30008);
1145}
1146
Eric Anholtc153f452007-09-03 12:06:45 +10001147static int i810_ov0_flip(struct drm_device *dev, void *data,
1148 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001150 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Eric Anholt6c340ea2007-08-25 20:23:09 +10001152 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001154 /* Tell the overlay to update */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001155 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 return 0;
1158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001161 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001162static void i810_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001165
Márton Németh3e684ea2008-01-24 15:58:57 +10001166 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 dev_priv->page_flipping = 1;
1168 dev_priv->current_page = 0;
1169 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1170}
1171
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001172static int i810_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 drm_i810_private_t *dev_priv = dev->dev_private;
1175
Márton Németh3e684ea2008-01-24 15:58:57 +10001176 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001178 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 dev_priv->page_flipping = 0;
1181 return 0;
1182}
1183
Eric Anholtc153f452007-09-03 12:06:45 +10001184static int i810_flip_bufs(struct drm_device *dev, void *data,
1185 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 drm_i810_private_t *dev_priv = dev->dev_private;
1188
Márton Németh3e684ea2008-01-24 15:58:57 +10001189 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Eric Anholt6c340ea2007-08-25 20:23:09 +10001191 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001193 if (!dev_priv->page_flipping)
1194 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001196 i810_dma_dispatch_flip(dev);
1197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Dave Airlieeddca552007-07-11 16:09:54 +10001200int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001201{
1202 /* i810 has 4 more counters */
1203 dev->counters += 4;
1204 dev->types[6] = _DRM_STAT_IRQ;
1205 dev->types[7] = _DRM_STAT_PRIMARY;
1206 dev->types[8] = _DRM_STAT_SECONDARY;
1207 dev->types[9] = _DRM_STAT_DMA;
1208
Dave Airlie466e69b2011-12-19 11:15:29 +00001209 pci_set_master(dev->pdev);
1210
Dave Airlie22eae942005-11-10 22:16:34 +11001211 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 }
Daniel Vetter6e877b52012-01-29 17:05:52 +01001226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Daniel Vetter6e877b52012-01-29 17:05:52 +01001228void i810_driver_reclaim_buffers_locked(struct drm_device *dev,
1229 struct drm_file *file_priv)
1230{
1231 i810_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001234int i810_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001236 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return 0;
1238}
1239
Eric Anholtc153f452007-09-03 12:06:45 +10001240struct drm_ioctl_desc i810_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001241 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1242 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1243 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1244 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1245 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1246 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1247 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1248 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1249 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1250 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1251 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1252 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1253 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1254 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1255 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256};
1257
1258int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001259
1260/**
1261 * Determine if the device really is AGP or not.
1262 *
1263 * All Intel graphics chipsets are treated as AGP, even if they are really
1264 * PCI-e.
1265 *
1266 * \param dev The device to be tested.
1267 *
1268 * \returns
1269 * A value of 1 is always retured to indictate every i810 is AGP.
1270 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001271int i810_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001272{
1273 return 1;
1274}