blob: 57d892eaaa6effc66defdbb890bb9f1b68208515 [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 */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000133 old_fops = file_priv->filp->f_op;
134 file_priv->filp->f_op = &i810_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 dev_priv->mmap_buffer = buf;
Al Viro244ca2b2012-05-29 21:24:36 -0400136 buf_priv->virtual = (void *)vm_mmap(file_priv->filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000137 PROT_READ | PROT_WRITE,
138 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000140 file_priv->filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000141 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* Real error */
143 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000144 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 buf_priv->virtual = NULL;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return retcode;
149}
150
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200151static int i810_unmap_buffer(struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
154 int retcode = 0;
155
156 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
157 return -EINVAL;
158
Al Virobfce2812012-04-20 21:57:04 -0400159 retcode = vm_munmap((unsigned long)buf_priv->virtual,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 (size_t) buf->total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000162 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
163 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 return retcode;
166}
167
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200168static int i810_dma_get_buffer(struct drm_device *dev, drm_i810_dma_t *d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000169 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Dave Airlie056219e2007-07-11 16:17:42 +1000171 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 drm_i810_buf_priv_t *buf_priv;
173 int retcode = 0;
174
175 buf = i810_freelist_get(dev);
176 if (!buf) {
177 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return retcode;
180 }
181
Eric Anholt6c340ea2007-08-25 20:23:09 +1000182 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (retcode) {
184 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return retcode;
187 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000188 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 buf_priv = buf->dev_private;
190 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 d->request_idx = buf->idx;
192 d->request_size = buf->total;
193 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 return retcode;
196}
197
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200198static int i810_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Dave Airliecdd55a22007-07-11 16:32:08 +1000200 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* Make sure interrupts are disabled here because the uninstall ioctl
203 * may not have been called from userspace and after dev_private
204 * is freed, it's too late.
205 */
206 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
207 drm_irq_uninstall(dev);
208
209 if (dev->dev_private) {
210 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 drm_i810_private_t *dev_priv =
212 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200214 if (dev_priv->ring.virtual_start)
Dave Airlieb9094d32007-01-08 21:31:13 +1100215 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 if (dev_priv->hw_status_page) {
217 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 dev_priv->hw_status_page,
219 dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700221 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000225 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100227
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100229 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233}
234
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200235static int i810_wait_ring(struct drm_device *dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000236{
237 drm_i810_private_t *dev_priv = dev->dev_private;
238 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
239 int iters = 0;
240 unsigned long end;
241 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
242
243 end = jiffies + (HZ * 3);
244 while (ring->space < n) {
245 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
246 ring->space = ring->head - (ring->tail + 8);
247 if (ring->space < 0)
248 ring->space += ring->Size;
249
250 if (ring->head != last_head) {
251 end = jiffies + (HZ * 3);
252 last_head = ring->head;
253 }
254
255 iters++;
256 if (time_before(end, jiffies)) {
257 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
258 DRM_ERROR("lockup\n");
259 goto out_wait_ring;
260 }
261 udelay(1);
262 }
263
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200264out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000265 return iters;
266}
267
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200268static void i810_kernel_lost_context(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000269{
270 drm_i810_private_t *dev_priv = dev->dev_private;
271 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
272
273 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
274 ring->tail = I810_READ(LP_RING + RING_TAIL);
275 ring->space = ring->head - (ring->tail + 8);
276 if (ring->space < 0)
277 ring->space += ring->Size;
278}
279
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200280static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281{
Dave Airliecdd55a22007-07-11 16:32:08 +1000282 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283 int my_idx = 24;
284 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
285 int i;
286
287 if (dma->buf_count > 1019) {
288 /* Not enough space in the status page for the freelist */
289 return -EINVAL;
290 }
291
292 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000293 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
295
296 buf_priv->in_use = hw_status++;
297 buf_priv->my_use_idx = my_idx;
298 my_idx += 4;
299
300 *buf_priv->in_use = I810_BUF_FREE;
301
Dave Airlieb9094d32007-01-08 21:31:13 +1100302 buf_priv->map.offset = buf->bus_address;
303 buf_priv->map.size = buf->total;
304 buf_priv->map.type = _DRM_AGP;
305 buf_priv->map.flags = 0;
306 buf_priv->map.mtrr = 0;
307
308 drm_core_ioremap(&buf_priv->map, dev);
309 buf_priv->kernel_virtual = buf_priv->map.handle;
310
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000311 }
312 return 0;
313}
314
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200315static int i810_dma_initialize(struct drm_device *dev,
316 drm_i810_private_t *dev_priv,
317 drm_i810_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Dave Airlie55910512007-07-11 16:53:40 +1000319 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dave Airliebd1b3312007-05-26 05:01:51 +1000322 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (r_list->map &&
324 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 break;
328 }
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!dev_priv->sarea_map) {
331 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 i810_dma_cleanup(dev);
333 DRM_ERROR("can not find sarea!\n");
334 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
337 if (!dev_priv->mmio_map) {
338 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 i810_dma_cleanup(dev);
340 DRM_ERROR("can not find mmio map!\n");
341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000343 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
345 if (!dev->agp_buffer_map) {
346 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 i810_dma_cleanup(dev);
348 DRM_ERROR("can not find dma buffer map!\n");
349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000353 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 dev_priv->ring.Start = init->ring_start;
356 dev_priv->ring.End = init->ring_end;
357 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Dave Airlieb9094d32007-01-08 21:31:13 +1100359 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
360 dev_priv->ring.map.size = init->ring_size;
361 dev_priv->ring.map.type = _DRM_AGP;
362 dev_priv->ring.map.flags = 0;
363 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Dave Airlieb9094d32007-01-08 21:31:13 +1100365 drm_core_ioremap(&dev_priv->ring.map, dev);
366
367 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000368 dev->dev_private = (void *)dev_priv;
369 i810_dma_cleanup(dev);
370 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000372 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
Dave Airlieb9094d32007-01-08 21:31:13 +1100375 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
376
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000377 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 dev_priv->w = init->w;
380 dev_priv->h = init->h;
381 dev_priv->pitch = init->pitch;
382 dev_priv->back_offset = init->back_offset;
383 dev_priv->depth_offset = init->depth_offset;
384 dev_priv->front_offset = init->front_offset;
385
386 dev_priv->overlay_offset = init->overlay_offset;
387 dev_priv->overlay_physical = init->overlay_physical;
388
389 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
390 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
391 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
392
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000393 /* Program Hardware Status Page */
394 dev_priv->hw_status_page =
395 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
396 &dev_priv->dma_status_page);
397 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 dev->dev_private = (void *)dev_priv;
399 i810_dma_cleanup(dev);
400 DRM_ERROR("Can not allocate hardware status page\n");
401 return -ENOMEM;
402 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
404 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000407 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000409 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (i810_freelist_init(dev, dev_priv) != 0) {
411 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 i810_dma_cleanup(dev);
413 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000415 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 dev->dev_private = (void *)dev_priv;
418
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Eric Anholtc153f452007-09-03 12:06:45 +1000422static int i810_dma_init(struct drm_device *dev, void *data,
423 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000425 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000426 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000427 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Eric Anholtc153f452007-09-03 12:06:45 +1000429 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000430 case I810_INIT_DMA_1_4:
431 DRM_INFO("Using v1.4 init.\n");
Eric Anholt9a298b22009-03-24 12:23:04 -0700432 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000433 if (dev_priv == NULL)
434 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000435 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438 case I810_CLEANUP_DMA:
439 DRM_INFO("DMA Cleanup\n");
440 retcode = i810_dma_cleanup(dev);
441 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000442 default:
443 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* Most efficient way to verify state for the i810 is as it is
450 * emitted. Non-conformant state is silently dropped.
451 *
452 * Use 'volatile' & local var tmp to force the emitted values to be
453 * identical to the verified ones.
454 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200455static void i810EmitContextVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int i, j = 0;
460 unsigned int tmp;
461 RING_LOCALS;
462
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000465 OUT_RING(GFX_OP_COLOR_FACTOR);
466 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 OUT_RING(GFX_OP_STIPPLE);
469 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000471 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 tmp = code[i];
473
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 if ((tmp & (7 << 29)) == (3 << 29) &&
475 (tmp & (0x1f << 24)) < (0x1d << 24)) {
476 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000478 } else
479 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 ADVANCE_LP_RING();
486}
487
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200488static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 int i, j = 0;
492 unsigned int tmp;
493 RING_LOCALS;
494
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000495 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000497 OUT_RING(GFX_OP_MAP_INFO);
498 OUT_RING(code[I810_TEXREG_MI1]);
499 OUT_RING(code[I810_TEXREG_MI2]);
500 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000502 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 tmp = code[i];
504
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505 if ((tmp & (7 << 29)) == (3 << 29) &&
506 (tmp & (0x1f << 24)) < (0x1d << 24)) {
507 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000509 } else
510 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
513 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000514 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 ADVANCE_LP_RING();
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/* Need to do some additional checking when setting the dest buffer.
520 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200521static void i810EmitDestVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000522 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 unsigned int tmp;
526 RING_LOCALS;
527
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000528 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 tmp = code[I810_DESTREG_DI1];
531 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 OUT_RING(CMD_OP_DESTBUFFER_INFO);
533 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000535 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
536 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* invarient:
539 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 OUT_RING(CMD_OP_Z_BUFFER_INFO);
541 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 OUT_RING(GFX_OP_DESTBUFFER_VARS);
544 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 OUT_RING(GFX_OP_DRAWRECT_INFO);
547 OUT_RING(code[I810_DESTREG_DR1]);
548 OUT_RING(code[I810_DESTREG_DR2]);
549 OUT_RING(code[I810_DESTREG_DR3]);
550 OUT_RING(code[I810_DESTREG_DR4]);
551 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 ADVANCE_LP_RING();
554}
555
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200556static void i810EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000558 drm_i810_private_t *dev_priv = dev->dev_private;
559 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000561
Márton Németh3e684ea2008-01-24 15:58:57 +1000562 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
567 }
568
569 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
572 }
573
574 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
577 }
578
579 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
582 }
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/* need to verify
586 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200587static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 unsigned int clear_color,
589 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 drm_i810_private_t *dev_priv = dev->dev_private;
592 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000594 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int pitch = dev_priv->pitch;
596 int cpp = 2;
597 int i;
598 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000599
600 if (dev_priv->current_page == 1) {
601 unsigned int tmp = flags;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000604 if (tmp & I810_FRONT)
605 flags |= I810_BACK;
606 if (tmp & I810_BACK)
607 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000610 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000612 if (nbox > I810_NR_SAREA_CLIPRECTS)
613 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 unsigned int x = pbox->x1;
617 unsigned int y = pbox->y1;
618 unsigned int width = (pbox->x2 - x) * cpp;
619 unsigned int height = pbox->y2 - y;
620 unsigned int start = y * pitch + x * cpp;
621
622 if (pbox->x1 > pbox->x2 ||
623 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 continue;
626
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000627 if (flags & I810_FRONT) {
628 BEGIN_LP_RING(6);
629 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
630 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
631 OUT_RING((height << 16) | width);
632 OUT_RING(start);
633 OUT_RING(clear_color);
634 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 ADVANCE_LP_RING();
636 }
637
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 if (flags & I810_BACK) {
639 BEGIN_LP_RING(6);
640 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
641 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
642 OUT_RING((height << 16) | width);
643 OUT_RING(dev_priv->back_offset + start);
644 OUT_RING(clear_color);
645 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ADVANCE_LP_RING();
647 }
648
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 if (flags & I810_DEPTH) {
650 BEGIN_LP_RING(6);
651 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
652 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
653 OUT_RING((height << 16) | width);
654 OUT_RING(dev_priv->depth_offset + start);
655 OUT_RING(clear_zval);
656 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ADVANCE_LP_RING();
658 }
659 }
660}
661
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200662static void i810_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000664 drm_i810_private_t *dev_priv = dev->dev_private;
665 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000667 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int pitch = dev_priv->pitch;
669 int cpp = 2;
670 int i;
671 RING_LOCALS;
672
673 DRM_DEBUG("swapbuffers\n");
674
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 if (nbox > I810_NR_SAREA_CLIPRECTS)
678 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 unsigned int w = pbox->x2 - pbox->x1;
682 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned int start = dst;
685
686 if (pbox->x1 > pbox->x2 ||
687 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 continue;
690
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 BEGIN_LP_RING(6);
692 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
693 OUT_RING(pitch | (0xCC << 16));
694 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000698 OUT_RING(dev_priv->back_offset + start);
699 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 ADVANCE_LP_RING();
705 }
706}
707
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200708static void i810_dma_dispatch_vertex(struct drm_device *dev,
709 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000713 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000714 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 unsigned long address = (unsigned long)buf->bus_address;
717 unsigned long start = address - dev->agp->base;
718 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 nbox = I810_NR_SAREA_CLIPRECTS;
725
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000726 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 used = 0;
728
729 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
733 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
734
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000735 *(u32 *) buf_priv->kernel_virtual =
736 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000739 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 used += 4;
741 }
742
743 i810_unmap_buffer(buf);
744 }
745
746 if (used) {
747 do {
748 if (i < nbox) {
749 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
751 SC_ENABLE);
752 OUT_RING(GFX_OP_SCISSOR_INFO);
753 OUT_RING(box[i].x1 | (box[i].y1 << 16));
754 OUT_RING((box[i].x2 -
755 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 ADVANCE_LP_RING();
757 }
758
759 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 OUT_RING(CMD_OP_BATCH_BUFFER);
761 OUT_RING(start | BB1_PROTECTED);
762 OUT_RING(start + used - 4);
763 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ADVANCE_LP_RING();
765
766 } while (++i < nbox);
767 }
768
769 if (discard) {
770 dev_priv->counter++;
771
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000772 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
773 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 OUT_RING(CMD_STORE_DWORD_IDX);
777 OUT_RING(20);
778 OUT_RING(dev_priv->counter);
779 OUT_RING(CMD_STORE_DWORD_IDX);
780 OUT_RING(buf_priv->my_use_idx);
781 OUT_RING(I810_BUF_FREE);
782 OUT_RING(CMD_REPORT_HEAD);
783 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 ADVANCE_LP_RING();
785 }
786}
787
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200788static void i810_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000790 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int pitch = dev_priv->pitch;
792 RING_LOCALS;
793
Márton Németh3e684ea2008-01-24 15:58:57 +1000794 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 dev_priv->current_page,
796 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 i810_kernel_lost_context(dev);
799
800 BEGIN_LP_RING(2);
801 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
802 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 ADVANCE_LP_RING();
804
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /* On i815 at least ASYNC is buggy */
807 /* pitch<<5 is from 11.2.8 p158,
808 its the pitch / 8 then left shifted 8,
809 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
811 if (dev_priv->current_page == 0) {
812 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 dev_priv->current_page = 1;
814 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 dev_priv->current_page = 0;
817 }
818 OUT_RING(0);
819 ADVANCE_LP_RING();
820
821 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
823 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ADVANCE_LP_RING();
825
826 /* Increment the frame counter. The client-side 3D driver must
827 * throttle the framerate by waiting for this value before
828 * performing the swapbuffer ioctl.
829 */
830 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
831
832}
833
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200834static void i810_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 drm_i810_private_t *dev_priv = dev->dev_private;
837 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000841 BEGIN_LP_RING(4);
842 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
843 OUT_RING(CMD_REPORT_HEAD);
844 OUT_RING(0);
845 OUT_RING(0);
846 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200851static int i810_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000854 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 int i, ret = 0;
856 RING_LOCALS;
857
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 BEGIN_LP_RING(2);
861 OUT_RING(CMD_REPORT_HEAD);
862 OUT_RING(0);
863 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000868 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
872 I810_BUF_FREE);
873
874 if (used == I810_BUF_HARDWARE)
875 DRM_DEBUG("reclaimed from HARDWARE\n");
876 if (used == I810_BUF_CLIENT)
877 DRM_DEBUG("still on client\n");
878 }
879
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000880 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883/* Must be called with the lock held */
Daniel Vetterd5346b32012-06-11 21:50:23 +0200884void i810_driver_reclaim_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000885 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Dave Airliecdd55a22007-07-11 16:32:08 +1000887 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 if (!dma)
891 return;
892 if (!dev->dev_private)
893 return;
894 if (!dma->buflist)
895 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000900 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Eric Anholt6c340ea2007-08-25 20:23:09 +1000903 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
905 I810_BUF_FREE);
906
907 if (used == I810_BUF_CLIENT)
908 DRM_DEBUG("reclaimed from client\n");
909 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 }
913}
914
Eric Anholtc153f452007-09-03 12:06:45 +1000915static int i810_flush_ioctl(struct drm_device *dev, void *data,
916 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000918 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 i810_flush_queue(dev);
921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Eric Anholtc153f452007-09-03 12:06:45 +1000924static int i810_dma_vertex(struct drm_device *dev, void *data,
925 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Dave Airliecdd55a22007-07-11 16:32:08 +1000927 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
929 u32 *hw_status = dev_priv->hw_status_page;
930 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
931 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000932 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Eric Anholt6c340ea2007-08-25 20:23:09 +1000934 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Márton Németh3e684ea2008-01-24 15:58:57 +1000936 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000937 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Eric Anholtc153f452007-09-03 12:06:45 +1000939 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EINVAL;
941
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000943 dma->buflist[vertex->idx],
944 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Eric Anholtc153f452007-09-03 12:06:45 +1000946 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000948 sarea_priv->last_enqueue = dev_priv->counter - 1;
949 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 return 0;
952}
953
Eric Anholtc153f452007-09-03 12:06:45 +1000954static int i810_clear_bufs(struct drm_device *dev, void *data,
955 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Eric Anholtc153f452007-09-03 12:06:45 +1000957 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Eric Anholt6c340ea2007-08-25 20:23:09 +1000959 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000961 /* GH: Someone's doing nasty things... */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200962 if (!dev->dev_private)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Eric Anholtc153f452007-09-03 12:06:45 +1000965 i810_dma_dispatch_clear(dev, clear->flags,
966 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Eric Anholtc153f452007-09-03 12:06:45 +1000970static int i810_swap_bufs(struct drm_device *dev, void *data,
971 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Márton Németh3e684ea2008-01-24 15:58:57 +1000973 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Eric Anholt6c340ea2007-08-25 20:23:09 +1000975 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000977 i810_dma_dispatch_swap(dev);
978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Eric Anholtc153f452007-09-03 12:06:45 +1000981static int i810_getage(struct drm_device *dev, void *data,
982 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
985 u32 *hw_status = dev_priv->hw_status_page;
986 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
987 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000989 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return 0;
991}
992
Eric Anholtc153f452007-09-03 12:06:45 +1000993static int i810_getbuf(struct drm_device *dev, void *data,
994 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +1000997 drm_i810_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000998 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
999 u32 *hw_status = dev_priv->hw_status_page;
1000 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1001 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Anholt6c340ea2007-08-25 20:23:09 +10001003 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Eric Anholtc153f452007-09-03 12:06:45 +10001005 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Eric Anholtc153f452007-09-03 12:06:45 +10001007 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001010 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 return retcode;
1015}
1016
Eric Anholtc153f452007-09-03 12:06:45 +10001017static int i810_copybuf(struct drm_device *dev, void *data,
1018 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 /* Never copy - 2.4.x doesn't need it */
1021 return 0;
1022}
1023
Eric Anholtc153f452007-09-03 12:06:45 +10001024static int i810_docopy(struct drm_device *dev, void *data,
1025 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 /* Never copy - 2.4.x doesn't need it */
1028 return 0;
1029}
1030
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001031static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 drm_i810_private_t *dev_priv = dev->dev_private;
1035 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1036 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1037 unsigned long address = (unsigned long)buf->bus_address;
1038 unsigned long start = address - dev->agp->base;
1039 int u;
1040 RING_LOCALS;
1041
1042 i810_kernel_lost_context(dev);
1043
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001045 if (u != I810_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 DRM_DEBUG("MC found buffer that isn't mine!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 used = 0;
1050
1051 sarea_priv->dirty = 0x7f;
1052
Márton Németh3e684ea2008-01-24 15:58:57 +10001053 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 dev_priv->counter++;
1056 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 DRM_DEBUG("start : %lx\n", start);
1058 DRM_DEBUG("used : %d\n", used);
1059 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1060
1061 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1062 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001063 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 used += 4;
1065 }
1066
1067 i810_unmap_buffer(buf);
1068 }
1069 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 OUT_RING(CMD_OP_BATCH_BUFFER);
1071 OUT_RING(start | BB1_PROTECTED);
1072 OUT_RING(start + used - 4);
1073 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ADVANCE_LP_RING();
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 OUT_RING(CMD_STORE_DWORD_IDX);
1078 OUT_RING(buf_priv->my_use_idx);
1079 OUT_RING(I810_BUF_FREE);
1080 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 OUT_RING(CMD_STORE_DWORD_IDX);
1083 OUT_RING(16);
1084 OUT_RING(last_render);
1085 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 ADVANCE_LP_RING();
1087}
1088
Eric Anholtc153f452007-09-03 12:06:45 +10001089static int i810_dma_mc(struct drm_device *dev, void *data,
1090 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Dave Airliecdd55a22007-07-11 16:32:08 +10001092 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 u32 *hw_status = dev_priv->hw_status_page;
1095 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001097 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Eric Anholt6c340ea2007-08-25 20:23:09 +10001099 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Eric Anholtc153f452007-09-03 12:06:45 +10001101 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return -EINVAL;
1103
Eric Anholtc153f452007-09-03 12:06:45 +10001104 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1105 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Eric Anholtc153f452007-09-03 12:06:45 +10001107 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 sarea_priv->last_enqueue = dev_priv->counter - 1;
1110 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 return 0;
1113}
1114
Eric Anholtc153f452007-09-03 12:06:45 +10001115static int i810_rstatus(struct drm_device *dev, void *data,
1116 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001118 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Eric Anholtc153f452007-09-03 12:06:45 +10001123static int i810_ov0_info(struct drm_device *dev, void *data,
1124 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001126 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001127 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Eric Anholtc153f452007-09-03 12:06:45 +10001129 ov->offset = dev_priv->overlay_offset;
1130 ov->physical = dev_priv->overlay_physical;
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return 0;
1133}
1134
Eric Anholtc153f452007-09-03 12:06:45 +10001135static int i810_fstatus(struct drm_device *dev, void *data,
1136 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Eric Anholt6c340ea2007-08-25 20:23:09 +10001140 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return I810_READ(0x30008);
1142}
1143
Eric Anholtc153f452007-09-03 12:06:45 +10001144static int i810_ov0_flip(struct drm_device *dev, void *data,
1145 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001147 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Eric Anholt6c340ea2007-08-25 20:23:09 +10001149 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001151 /* Tell the overlay to update */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001152 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 return 0;
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001158 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001159static void i810_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001162
Márton Németh3e684ea2008-01-24 15:58:57 +10001163 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 dev_priv->page_flipping = 1;
1165 dev_priv->current_page = 0;
1166 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1167}
1168
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001169static int i810_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 drm_i810_private_t *dev_priv = dev->dev_private;
1172
Márton Németh3e684ea2008-01-24 15:58:57 +10001173 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001175 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 dev_priv->page_flipping = 0;
1178 return 0;
1179}
1180
Eric Anholtc153f452007-09-03 12:06:45 +10001181static int i810_flip_bufs(struct drm_device *dev, void *data,
1182 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 drm_i810_private_t *dev_priv = dev->dev_private;
1185
Márton Németh3e684ea2008-01-24 15:58:57 +10001186 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Eric Anholt6c340ea2007-08-25 20:23:09 +10001188 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001190 if (!dev_priv->page_flipping)
1191 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001193 i810_dma_dispatch_flip(dev);
1194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Dave Airlieeddca552007-07-11 16:09:54 +10001197int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001198{
1199 /* i810 has 4 more counters */
1200 dev->counters += 4;
1201 dev->types[6] = _DRM_STAT_IRQ;
1202 dev->types[7] = _DRM_STAT_PRIMARY;
1203 dev->types[8] = _DRM_STAT_SECONDARY;
1204 dev->types[9] = _DRM_STAT_DMA;
1205
Dave Airlie466e69b2011-12-19 11:15:29 +00001206 pci_set_master(dev->pdev);
1207
Dave Airlie22eae942005-11-10 22:16:34 +11001208 return 0;
1209}
1210
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001211void i810_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001213 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001216void i810_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 if (dev->dev_private) {
1219 drm_i810_private_t *dev_priv = dev->dev_private;
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001220 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 i810_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Daniel Vetterd5346b32012-06-11 21:50:23 +02001224 if (file_priv->master && file_priv->master->lock.hw_lock) {
1225 drm_idlelock_take(&file_priv->master->lock);
1226 i810_driver_reclaim_buffers(dev, file_priv);
1227 drm_idlelock_release(&file_priv->master->lock);
1228 } else {
1229 /* master disappeared, clean up stuff anyway and hope nothing
1230 * goes wrong */
1231 i810_driver_reclaim_buffers(dev, file_priv);
1232 }
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001236int i810_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001238 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return 0;
1240}
1241
Eric Anholtc153f452007-09-03 12:06:45 +10001242struct drm_ioctl_desc i810_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001243 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1244 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1245 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1246 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1247 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1248 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1249 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1250 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1251 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1252 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1253 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1254 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1255 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1256 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1257 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258};
1259
1260int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001261
1262/**
1263 * Determine if the device really is AGP or not.
1264 *
1265 * All Intel graphics chipsets are treated as AGP, even if they are really
1266 * PCI-e.
1267 *
1268 * \param dev The device to be tested.
1269 *
1270 * \returns
1271 * A value of 1 is always retured to indictate every i810 is AGP.
1272 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001273int i810_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001274{
1275 return 1;
1276}