blob: 61b4caf220fa83bd15815ea0f82b627f2d773727 [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>
Arnd Bergmann58374712010-07-10 23:51:39 +020040#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/pagemap.h>
42
43#define I810_BUF_FREE 2
44#define I810_BUF_CLIENT 1
Dave Airliebc5f4522007-11-05 12:50:58 +100045#define I810_BUF_HARDWARE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define I810_BUF_UNMAPPED 0
48#define I810_BUF_MAPPED 1
49
Dave Airlie056219e2007-07-11 16:17:42 +100050static struct drm_buf *i810_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Dave Airliecdd55a22007-07-11 16:32:08 +100052 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100053 int i;
54 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 /* Linear search might not be the best solution */
57
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +100059 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100062 used = cmpxchg(buf_priv->in_use, I810_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 I810_BUF_CLIENT);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +020064 if (used == I810_BUF_FREE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100067 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70/* This should only be called if the buffer is not sent to the hardware
71 * yet, the hardware updates in use for us once its on the ring buffer.
72 */
73
Nicolas Kaiseraca791c2010-07-14 21:54:13 +020074static int i810_freelist_put(struct drm_device *dev, struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100076 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
77 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Dave Airlieb5e89ed2005-09-25 14:28:13 +100079 /* In use is already a pointer */
80 used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_FREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (used != I810_BUF_CLIENT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
83 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Dave Airliec94f7022005-07-07 21:03:38 +100089static int i810_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Dave Airlieeddca552007-07-11 16:09:54 +100091 struct drm_file *priv = filp->private_data;
92 struct drm_device *dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093 drm_i810_private_t *dev_priv;
Dave Airlie056219e2007-07-11 16:17:42 +100094 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 drm_i810_buf_priv_t *buf_priv;
96
97 lock_kernel();
Dave Airlie2c14f282008-04-21 16:47:32 +100098 dev = priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000100 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 buf_priv = buf->dev_private;
102
103 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
104 vma->vm_file = filp;
105
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106 buf_priv->currently_mapped = I810_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unlock_kernel();
108
109 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlie3d774612006-08-07 20:07:43 +1000110 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000111 vma->vm_end - vma->vm_start, vma->vm_page_prot))
112 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
114}
115
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800116static const struct file_operations i810_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000117 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000118 .release = drm_release,
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000119 .unlocked_ioctl = drm_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120 .mmap = i810_mmap_buffers,
121 .fasync = drm_fasync,
Dave Airliec94f7022005-07-07 21:03:38 +1000122};
123
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200124static int i810_map_buffer(struct drm_buf *buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Dave Airlie2c14f282008-04-21 16:47:32 +1000126 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800129 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int retcode = 0;
131
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return -EINVAL;
134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000136 old_fops = file_priv->filp->f_op;
137 file_priv->filp->f_op = &i810_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 dev_priv->mmap_buffer = buf;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000139 buf_priv->virtual = (void *)do_mmap(file_priv->filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 PROT_READ | PROT_WRITE,
141 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000143 file_priv->filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000144 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Real error */
146 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000147 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 buf_priv->virtual = NULL;
149 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 return retcode;
153}
154
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200155static int i810_unmap_buffer(struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
158 int retcode = 0;
159
160 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
161 return -EINVAL;
162
163 down_write(&current->mm->mmap_sem);
164 retcode = do_munmap(current->mm,
165 (unsigned long)buf_priv->virtual,
166 (size_t) buf->total);
167 up_write(&current->mm->mmap_sem);
168
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
170 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 return retcode;
173}
174
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200175static int i810_dma_get_buffer(struct drm_device *dev, drm_i810_dma_t *d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000176 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Dave Airlie056219e2007-07-11 16:17:42 +1000178 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 drm_i810_buf_priv_t *buf_priv;
180 int retcode = 0;
181
182 buf = i810_freelist_get(dev);
183 if (!buf) {
184 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return retcode;
187 }
188
Eric Anholt6c340ea2007-08-25 20:23:09 +1000189 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (retcode) {
191 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return retcode;
194 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000195 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 buf_priv = buf->dev_private;
197 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 d->request_idx = buf->idx;
199 d->request_size = buf->total;
200 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 return retcode;
203}
204
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200205static int i810_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Dave Airliecdd55a22007-07-11 16:32:08 +1000207 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Make sure interrupts are disabled here because the uninstall ioctl
210 * may not have been called from userspace and after dev_private
211 * is freed, it's too late.
212 */
213 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
214 drm_irq_uninstall(dev);
215
216 if (dev->dev_private) {
217 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 drm_i810_private_t *dev_priv =
219 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200221 if (dev_priv->ring.virtual_start)
Dave Airlieb9094d32007-01-08 21:31:13 +1100222 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000223 if (dev_priv->hw_status_page) {
224 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dev_priv->hw_status_page,
226 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227 /* Need to rewrite hardware status page */
228 I810_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700230 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000234 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100236
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100238 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return 0;
242}
243
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200244static int i810_wait_ring(struct drm_device *dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000245{
246 drm_i810_private_t *dev_priv = dev->dev_private;
247 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
248 int iters = 0;
249 unsigned long end;
250 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
251
252 end = jiffies + (HZ * 3);
253 while (ring->space < n) {
254 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
255 ring->space = ring->head - (ring->tail + 8);
256 if (ring->space < 0)
257 ring->space += ring->Size;
258
259 if (ring->head != last_head) {
260 end = jiffies + (HZ * 3);
261 last_head = ring->head;
262 }
263
264 iters++;
265 if (time_before(end, jiffies)) {
266 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
267 DRM_ERROR("lockup\n");
268 goto out_wait_ring;
269 }
270 udelay(1);
271 }
272
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200273out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 return iters;
275}
276
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200277static void i810_kernel_lost_context(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278{
279 drm_i810_private_t *dev_priv = dev->dev_private;
280 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
281
282 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
283 ring->tail = I810_READ(LP_RING + RING_TAIL);
284 ring->space = ring->head - (ring->tail + 8);
285 if (ring->space < 0)
286 ring->space += ring->Size;
287}
288
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200289static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290{
Dave Airliecdd55a22007-07-11 16:32:08 +1000291 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000292 int my_idx = 24;
293 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
294 int i;
295
296 if (dma->buf_count > 1019) {
297 /* Not enough space in the status page for the freelist */
298 return -EINVAL;
299 }
300
301 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000302 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000303 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
304
305 buf_priv->in_use = hw_status++;
306 buf_priv->my_use_idx = my_idx;
307 my_idx += 4;
308
309 *buf_priv->in_use = I810_BUF_FREE;
310
Dave Airlieb9094d32007-01-08 21:31:13 +1100311 buf_priv->map.offset = buf->bus_address;
312 buf_priv->map.size = buf->total;
313 buf_priv->map.type = _DRM_AGP;
314 buf_priv->map.flags = 0;
315 buf_priv->map.mtrr = 0;
316
317 drm_core_ioremap(&buf_priv->map, dev);
318 buf_priv->kernel_virtual = buf_priv->map.handle;
319
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 }
321 return 0;
322}
323
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200324static int i810_dma_initialize(struct drm_device *dev,
325 drm_i810_private_t *dev_priv,
326 drm_i810_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Dave Airlie55910512007-07-11 16:53:40 +1000328 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dave Airliebd1b3312007-05-26 05:01:51 +1000331 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (r_list->map &&
333 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 break;
337 }
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!dev_priv->sarea_map) {
340 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 i810_dma_cleanup(dev);
342 DRM_ERROR("can not find sarea!\n");
343 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
346 if (!dev_priv->mmio_map) {
347 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000348 i810_dma_cleanup(dev);
349 DRM_ERROR("can not find mmio map!\n");
350 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000352 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
354 if (!dev->agp_buffer_map) {
355 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000356 i810_dma_cleanup(dev);
357 DRM_ERROR("can not find dma buffer map!\n");
358 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
361 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000362 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364 dev_priv->ring.Start = init->ring_start;
365 dev_priv->ring.End = init->ring_end;
366 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Dave Airlieb9094d32007-01-08 21:31:13 +1100368 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
369 dev_priv->ring.map.size = init->ring_size;
370 dev_priv->ring.map.type = _DRM_AGP;
371 dev_priv->ring.map.flags = 0;
372 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Dave Airlieb9094d32007-01-08 21:31:13 +1100374 drm_core_ioremap(&dev_priv->ring.map, dev);
375
376 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000377 dev->dev_private = (void *)dev_priv;
378 i810_dma_cleanup(dev);
379 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000381 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
Dave Airlieb9094d32007-01-08 21:31:13 +1100384 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
385
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000386 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 dev_priv->w = init->w;
389 dev_priv->h = init->h;
390 dev_priv->pitch = init->pitch;
391 dev_priv->back_offset = init->back_offset;
392 dev_priv->depth_offset = init->depth_offset;
393 dev_priv->front_offset = init->front_offset;
394
395 dev_priv->overlay_offset = init->overlay_offset;
396 dev_priv->overlay_physical = init->overlay_physical;
397
398 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
399 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
400 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
401
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000402 /* Program Hardware Status Page */
403 dev_priv->hw_status_page =
404 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
405 &dev_priv->dma_status_page);
406 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dev->dev_private = (void *)dev_priv;
408 i810_dma_cleanup(dev);
409 DRM_ERROR("Can not allocate hardware status page\n");
410 return -ENOMEM;
411 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
413 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (i810_freelist_init(dev, dev_priv) != 0) {
420 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000421 i810_dma_cleanup(dev);
422 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000424 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 dev->dev_private = (void *)dev_priv;
427
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Eric Anholtc153f452007-09-03 12:06:45 +1000431static int i810_dma_init(struct drm_device *dev, void *data,
432 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000434 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000435 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000436 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Eric Anholtc153f452007-09-03 12:06:45 +1000438 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000439 case I810_INIT_DMA_1_4:
440 DRM_INFO("Using v1.4 init.\n");
Eric Anholt9a298b22009-03-24 12:23:04 -0700441 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000442 if (dev_priv == NULL)
443 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000444 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 case I810_CLEANUP_DMA:
448 DRM_INFO("DMA Cleanup\n");
449 retcode = i810_dma_cleanup(dev);
450 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000451 default:
452 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000455 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/* Most efficient way to verify state for the i810 is as it is
459 * emitted. Non-conformant state is silently dropped.
460 *
461 * Use 'volatile' & local var tmp to force the emitted values to be
462 * identical to the verified ones.
463 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200464static void i810EmitContextVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000465 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int i, j = 0;
469 unsigned int tmp;
470 RING_LOCALS;
471
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000472 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 OUT_RING(GFX_OP_COLOR_FACTOR);
475 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 OUT_RING(GFX_OP_STIPPLE);
478 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000480 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 tmp = code[i];
482
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483 if ((tmp & (7 << 29)) == (3 << 29) &&
484 (tmp & (0x1f << 24)) < (0x1d << 24)) {
485 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000487 } else
488 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
491 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 ADVANCE_LP_RING();
495}
496
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200497static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000499 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 int i, j = 0;
501 unsigned int tmp;
502 RING_LOCALS;
503
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000506 OUT_RING(GFX_OP_MAP_INFO);
507 OUT_RING(code[I810_TEXREG_MI1]);
508 OUT_RING(code[I810_TEXREG_MI2]);
509 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000511 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 tmp = code[i];
513
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000514 if ((tmp & (7 << 29)) == (3 << 29) &&
515 (tmp & (0x1f << 24)) < (0x1d << 24)) {
516 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000518 } else
519 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000523 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 ADVANCE_LP_RING();
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/* Need to do some additional checking when setting the dest buffer.
529 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200530static void i810EmitDestVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 unsigned int tmp;
535 RING_LOCALS;
536
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 tmp = code[I810_DESTREG_DI1];
540 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 OUT_RING(CMD_OP_DESTBUFFER_INFO);
542 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000544 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
545 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* invarient:
548 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 OUT_RING(CMD_OP_Z_BUFFER_INFO);
550 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000552 OUT_RING(GFX_OP_DESTBUFFER_VARS);
553 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000555 OUT_RING(GFX_OP_DRAWRECT_INFO);
556 OUT_RING(code[I810_DESTREG_DR1]);
557 OUT_RING(code[I810_DESTREG_DR2]);
558 OUT_RING(code[I810_DESTREG_DR3]);
559 OUT_RING(code[I810_DESTREG_DR4]);
560 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 ADVANCE_LP_RING();
563}
564
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200565static void i810EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 drm_i810_private_t *dev_priv = dev->dev_private;
568 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570
Márton Németh3e684ea2008-01-24 15:58:57 +1000571 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
576 }
577
578 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
581 }
582
583 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000584 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
586 }
587
588 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
591 }
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/* need to verify
595 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200596static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 unsigned int clear_color,
598 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000600 drm_i810_private_t *dev_priv = dev->dev_private;
601 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000603 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int pitch = dev_priv->pitch;
605 int cpp = 2;
606 int i;
607 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608
609 if (dev_priv->current_page == 1) {
610 unsigned int tmp = flags;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 if (tmp & I810_FRONT)
614 flags |= I810_BACK;
615 if (tmp & I810_BACK)
616 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 if (nbox > I810_NR_SAREA_CLIPRECTS)
622 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned int x = pbox->x1;
626 unsigned int y = pbox->y1;
627 unsigned int width = (pbox->x2 - x) * cpp;
628 unsigned int height = pbox->y2 - y;
629 unsigned int start = y * pitch + x * cpp;
630
631 if (pbox->x1 > pbox->x2 ||
632 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 continue;
635
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636 if (flags & I810_FRONT) {
637 BEGIN_LP_RING(6);
638 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
639 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
640 OUT_RING((height << 16) | width);
641 OUT_RING(start);
642 OUT_RING(clear_color);
643 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ADVANCE_LP_RING();
645 }
646
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000647 if (flags & I810_BACK) {
648 BEGIN_LP_RING(6);
649 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
650 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
651 OUT_RING((height << 16) | width);
652 OUT_RING(dev_priv->back_offset + start);
653 OUT_RING(clear_color);
654 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 ADVANCE_LP_RING();
656 }
657
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658 if (flags & I810_DEPTH) {
659 BEGIN_LP_RING(6);
660 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
661 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
662 OUT_RING((height << 16) | width);
663 OUT_RING(dev_priv->depth_offset + start);
664 OUT_RING(clear_zval);
665 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 ADVANCE_LP_RING();
667 }
668 }
669}
670
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200671static void i810_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 drm_i810_private_t *dev_priv = dev->dev_private;
674 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000676 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int pitch = dev_priv->pitch;
678 int cpp = 2;
679 int i;
680 RING_LOCALS;
681
682 DRM_DEBUG("swapbuffers\n");
683
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 if (nbox > I810_NR_SAREA_CLIPRECTS)
687 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 unsigned int w = pbox->x2 - pbox->x1;
691 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 unsigned int start = dst;
694
695 if (pbox->x1 > pbox->x2 ||
696 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 continue;
699
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 BEGIN_LP_RING(6);
701 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
702 OUT_RING(pitch | (0xCC << 16));
703 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 OUT_RING(dev_priv->back_offset + start);
708 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 ADVANCE_LP_RING();
714 }
715}
716
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200717static void i810_dma_dispatch_vertex(struct drm_device *dev,
718 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000723 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 unsigned long address = (unsigned long)buf->bus_address;
726 unsigned long start = address - dev->agp->base;
727 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 nbox = I810_NR_SAREA_CLIPRECTS;
734
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000735 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 used = 0;
737
738 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000739 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
742 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
743
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 *(u32 *) buf_priv->kernel_virtual =
745 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000748 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 used += 4;
750 }
751
752 i810_unmap_buffer(buf);
753 }
754
755 if (used) {
756 do {
757 if (i < nbox) {
758 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
760 SC_ENABLE);
761 OUT_RING(GFX_OP_SCISSOR_INFO);
762 OUT_RING(box[i].x1 | (box[i].y1 << 16));
763 OUT_RING((box[i].x2 -
764 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 ADVANCE_LP_RING();
766 }
767
768 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 OUT_RING(CMD_OP_BATCH_BUFFER);
770 OUT_RING(start | BB1_PROTECTED);
771 OUT_RING(start + used - 4);
772 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ADVANCE_LP_RING();
774
775 } while (++i < nbox);
776 }
777
778 if (discard) {
779 dev_priv->counter++;
780
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
782 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000785 OUT_RING(CMD_STORE_DWORD_IDX);
786 OUT_RING(20);
787 OUT_RING(dev_priv->counter);
788 OUT_RING(CMD_STORE_DWORD_IDX);
789 OUT_RING(buf_priv->my_use_idx);
790 OUT_RING(I810_BUF_FREE);
791 OUT_RING(CMD_REPORT_HEAD);
792 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 ADVANCE_LP_RING();
794 }
795}
796
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200797static void i810_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int pitch = dev_priv->pitch;
801 RING_LOCALS;
802
Márton Németh3e684ea2008-01-24 15:58:57 +1000803 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 dev_priv->current_page,
805 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 i810_kernel_lost_context(dev);
808
809 BEGIN_LP_RING(2);
810 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
811 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 ADVANCE_LP_RING();
813
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* On i815 at least ASYNC is buggy */
816 /* pitch<<5 is from 11.2.8 p158,
817 its the pitch / 8 then left shifted 8,
818 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
820 if (dev_priv->current_page == 0) {
821 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 dev_priv->current_page = 1;
823 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 dev_priv->current_page = 0;
826 }
827 OUT_RING(0);
828 ADVANCE_LP_RING();
829
830 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
832 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ADVANCE_LP_RING();
834
835 /* Increment the frame counter. The client-side 3D driver must
836 * throttle the framerate by waiting for this value before
837 * performing the swapbuffer ioctl.
838 */
839 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
840
841}
842
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200843static void i810_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 drm_i810_private_t *dev_priv = dev->dev_private;
846 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 BEGIN_LP_RING(4);
851 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
852 OUT_RING(CMD_REPORT_HEAD);
853 OUT_RING(0);
854 OUT_RING(0);
855 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200860static int i810_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000863 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000864 int i, ret = 0;
865 RING_LOCALS;
866
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 BEGIN_LP_RING(2);
870 OUT_RING(CMD_REPORT_HEAD);
871 OUT_RING(0);
872 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000877 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
881 I810_BUF_FREE);
882
883 if (used == I810_BUF_HARDWARE)
884 DRM_DEBUG("reclaimed from HARDWARE\n");
885 if (used == I810_BUF_CLIENT)
886 DRM_DEBUG("still on client\n");
887 }
888
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892/* Must be called with the lock held */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200893static void i810_reclaim_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000894 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Dave Airliecdd55a22007-07-11 16:32:08 +1000896 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 if (!dma)
900 return;
901 if (!dev->dev_private)
902 return;
903 if (!dma->buflist)
904 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000909 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Eric Anholt6c340ea2007-08-25 20:23:09 +1000912 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
914 I810_BUF_FREE);
915
916 if (used == I810_BUF_CLIENT)
917 DRM_DEBUG("reclaimed from client\n");
918 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 }
922}
923
Eric Anholtc153f452007-09-03 12:06:45 +1000924static int i810_flush_ioctl(struct drm_device *dev, void *data,
925 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000927 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 i810_flush_queue(dev);
930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Eric Anholtc153f452007-09-03 12:06:45 +1000933static int i810_dma_vertex(struct drm_device *dev, void *data,
934 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Dave Airliecdd55a22007-07-11 16:32:08 +1000936 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000937 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
938 u32 *hw_status = dev_priv->hw_status_page;
939 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
940 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000941 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Eric Anholt6c340ea2007-08-25 20:23:09 +1000943 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Márton Németh3e684ea2008-01-24 15:58:57 +1000945 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000946 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Eric Anholtc153f452007-09-03 12:06:45 +1000948 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return -EINVAL;
950
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000952 dma->buflist[vertex->idx],
953 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Eric Anholtc153f452007-09-03 12:06:45 +1000955 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000957 sarea_priv->last_enqueue = dev_priv->counter - 1;
958 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 return 0;
961}
962
Eric Anholtc153f452007-09-03 12:06:45 +1000963static int i810_clear_bufs(struct drm_device *dev, void *data,
964 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Eric Anholtc153f452007-09-03 12:06:45 +1000966 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Eric Anholt6c340ea2007-08-25 20:23:09 +1000968 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000970 /* GH: Someone's doing nasty things... */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200971 if (!dev->dev_private)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Eric Anholtc153f452007-09-03 12:06:45 +1000974 i810_dma_dispatch_clear(dev, clear->flags,
975 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Eric Anholtc153f452007-09-03 12:06:45 +1000979static int i810_swap_bufs(struct drm_device *dev, void *data,
980 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Márton Németh3e684ea2008-01-24 15:58:57 +1000982 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Eric Anholt6c340ea2007-08-25 20:23:09 +1000984 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 i810_dma_dispatch_swap(dev);
987 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Eric Anholtc153f452007-09-03 12:06:45 +1000990static int i810_getage(struct drm_device *dev, void *data,
991 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000993 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
994 u32 *hw_status = dev_priv->hw_status_page;
995 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
996 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000998 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return 0;
1000}
1001
Eric Anholtc153f452007-09-03 12:06:45 +10001002static int i810_getbuf(struct drm_device *dev, void *data,
1003 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001006 drm_i810_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1008 u32 *hw_status = dev_priv->hw_status_page;
1009 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1010 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Eric Anholt6c340ea2007-08-25 20:23:09 +10001012 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Eric Anholtc153f452007-09-03 12:06:45 +10001014 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Eric Anholtc153f452007-09-03 12:06:45 +10001016 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001019 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 return retcode;
1024}
1025
Eric Anholtc153f452007-09-03 12:06:45 +10001026static int i810_copybuf(struct drm_device *dev, void *data,
1027 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 /* Never copy - 2.4.x doesn't need it */
1030 return 0;
1031}
1032
Eric Anholtc153f452007-09-03 12:06:45 +10001033static int i810_docopy(struct drm_device *dev, void *data,
1034 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 /* Never copy - 2.4.x doesn't need it */
1037 return 0;
1038}
1039
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001040static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 drm_i810_private_t *dev_priv = dev->dev_private;
1044 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1045 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1046 unsigned long address = (unsigned long)buf->bus_address;
1047 unsigned long start = address - dev->agp->base;
1048 int u;
1049 RING_LOCALS;
1050
1051 i810_kernel_lost_context(dev);
1052
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001054 if (u != I810_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 DRM_DEBUG("MC found buffer that isn't mine!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 used = 0;
1059
1060 sarea_priv->dirty = 0x7f;
1061
Márton Németh3e684ea2008-01-24 15:58:57 +10001062 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 dev_priv->counter++;
1065 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 DRM_DEBUG("start : %lx\n", start);
1067 DRM_DEBUG("used : %d\n", used);
1068 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1069
1070 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1071 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001072 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 used += 4;
1074 }
1075
1076 i810_unmap_buffer(buf);
1077 }
1078 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001079 OUT_RING(CMD_OP_BATCH_BUFFER);
1080 OUT_RING(start | BB1_PROTECTED);
1081 OUT_RING(start + used - 4);
1082 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ADVANCE_LP_RING();
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 OUT_RING(CMD_STORE_DWORD_IDX);
1087 OUT_RING(buf_priv->my_use_idx);
1088 OUT_RING(I810_BUF_FREE);
1089 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 OUT_RING(CMD_STORE_DWORD_IDX);
1092 OUT_RING(16);
1093 OUT_RING(last_render);
1094 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 ADVANCE_LP_RING();
1096}
1097
Eric Anholtc153f452007-09-03 12:06:45 +10001098static int i810_dma_mc(struct drm_device *dev, void *data,
1099 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Dave Airliecdd55a22007-07-11 16:32:08 +10001101 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 u32 *hw_status = dev_priv->hw_status_page;
1104 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001106 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Eric Anholt6c340ea2007-08-25 20:23:09 +10001108 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Eric Anholtc153f452007-09-03 12:06:45 +10001110 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -EINVAL;
1112
Eric Anholtc153f452007-09-03 12:06:45 +10001113 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1114 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Eric Anholtc153f452007-09-03 12:06:45 +10001116 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001118 sarea_priv->last_enqueue = dev_priv->counter - 1;
1119 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 return 0;
1122}
1123
Eric Anholtc153f452007-09-03 12:06:45 +10001124static int i810_rstatus(struct drm_device *dev, void *data,
1125 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Eric Anholtc153f452007-09-03 12:06:45 +10001132static int i810_ov0_info(struct drm_device *dev, void *data,
1133 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001135 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001136 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Eric Anholtc153f452007-09-03 12:06:45 +10001138 ov->offset = dev_priv->overlay_offset;
1139 ov->physical = dev_priv->overlay_physical;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return 0;
1142}
1143
Eric Anholtc153f452007-09-03 12:06:45 +10001144static int i810_fstatus(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 return I810_READ(0x30008);
1151}
1152
Eric Anholtc153f452007-09-03 12:06:45 +10001153static int i810_ov0_flip(struct drm_device *dev, void *data,
1154 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001156 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Eric Anholt6c340ea2007-08-25 20:23:09 +10001158 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001160 /* Tell the overlay to update */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001161 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 return 0;
1164}
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001167 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001168static void i810_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171
Márton Németh3e684ea2008-01-24 15:58:57 +10001172 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 dev_priv->page_flipping = 1;
1174 dev_priv->current_page = 0;
1175 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1176}
1177
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001178static int i810_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 drm_i810_private_t *dev_priv = dev->dev_private;
1181
Márton Németh3e684ea2008-01-24 15:58:57 +10001182 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001184 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 dev_priv->page_flipping = 0;
1187 return 0;
1188}
1189
Eric Anholtc153f452007-09-03 12:06:45 +10001190static int i810_flip_bufs(struct drm_device *dev, void *data,
1191 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 drm_i810_private_t *dev_priv = dev->dev_private;
1194
Márton Németh3e684ea2008-01-24 15:58:57 +10001195 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Eric Anholt6c340ea2007-08-25 20:23:09 +10001197 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001199 if (!dev_priv->page_flipping)
1200 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001202 i810_dma_dispatch_flip(dev);
1203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Dave Airlieeddca552007-07-11 16:09:54 +10001206int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001207{
1208 /* i810 has 4 more counters */
1209 dev->counters += 4;
1210 dev->types[6] = _DRM_STAT_IRQ;
1211 dev->types[7] = _DRM_STAT_PRIMARY;
1212 dev->types[8] = _DRM_STAT_SECONDARY;
1213 dev->types[9] = _DRM_STAT_DMA;
1214
1215 return 0;
1216}
1217
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001218void i810_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001220 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001223void i810_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 if (dev->dev_private) {
1226 drm_i810_private_t *dev_priv = dev->dev_private;
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001227 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 i810_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230}
1231
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001232void i810_driver_reclaim_buffers_locked(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10001233 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001235 i810_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001238int i810_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001240 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
1242}
1243
Arnd Bergmann58374712010-07-10 23:51:39 +02001244/*
1245 * call the drm_ioctl under the big kernel lock because
1246 * to lock against the i810_mmap_buffers function.
1247 */
1248long i810_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1249{
1250 int ret;
1251 lock_kernel();
1252 ret = drm_ioctl(file, cmd, arg);
1253 unlock_kernel();
1254 return ret;
1255}
1256
Eric Anholtc153f452007-09-03 12:06:45 +10001257struct drm_ioctl_desc i810_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001258 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1259 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1260 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1261 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1262 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1263 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1264 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1265 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1266 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1267 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1268 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1269 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1270 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1271 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1272 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273};
1274
1275int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001276
1277/**
1278 * Determine if the device really is AGP or not.
1279 *
1280 * All Intel graphics chipsets are treated as AGP, even if they are really
1281 * PCI-e.
1282 *
1283 * \param dev The device to be tested.
1284 *
1285 * \returns
1286 * A value of 1 is always retured to indictate every i810 is AGP.
1287 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001288int i810_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001289{
1290 return 1;
1291}