blob: ff33e53bbbf80dfbd470a444c854dc5ae158e32b [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 Bergmann130b9852010-09-29 17:47:58 +0200119 .unlocked_ioctl = i810_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120 .mmap = i810_mmap_buffers,
121 .fasync = drm_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200122 .llseek = noop_llseek,
Dave Airliec94f7022005-07-07 21:03:38 +1000123};
124
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200125static int i810_map_buffer(struct drm_buf *buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Dave Airlie2c14f282008-04-21 16:47:32 +1000127 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800130 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int retcode = 0;
132
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -EINVAL;
135
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000136 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000137 old_fops = file_priv->filp->f_op;
138 file_priv->filp->f_op = &i810_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 dev_priv->mmap_buffer = buf;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000140 buf_priv->virtual = (void *)do_mmap(file_priv->filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 PROT_READ | PROT_WRITE,
142 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000144 file_priv->filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000145 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /* Real error */
147 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000148 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 buf_priv->virtual = NULL;
150 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000151 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 return retcode;
154}
155
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200156static int i810_unmap_buffer(struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
159 int retcode = 0;
160
161 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
162 return -EINVAL;
163
164 down_write(&current->mm->mmap_sem);
165 retcode = do_munmap(current->mm,
166 (unsigned long)buf_priv->virtual,
167 (size_t) buf->total);
168 up_write(&current->mm->mmap_sem);
169
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000170 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
171 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 return retcode;
174}
175
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200176static int i810_dma_get_buffer(struct drm_device *dev, drm_i810_dma_t *d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000177 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Dave Airlie056219e2007-07-11 16:17:42 +1000179 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 drm_i810_buf_priv_t *buf_priv;
181 int retcode = 0;
182
183 buf = i810_freelist_get(dev);
184 if (!buf) {
185 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return retcode;
188 }
189
Eric Anholt6c340ea2007-08-25 20:23:09 +1000190 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (retcode) {
192 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return retcode;
195 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000196 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 buf_priv = buf->dev_private;
198 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 d->request_idx = buf->idx;
200 d->request_size = buf->total;
201 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 return retcode;
204}
205
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200206static int i810_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Dave Airliecdd55a22007-07-11 16:32:08 +1000208 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* Make sure interrupts are disabled here because the uninstall ioctl
211 * may not have been called from userspace and after dev_private
212 * is freed, it's too late.
213 */
214 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
215 drm_irq_uninstall(dev);
216
217 if (dev->dev_private) {
218 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219 drm_i810_private_t *dev_priv =
220 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200222 if (dev_priv->ring.virtual_start)
Dave Airlieb9094d32007-01-08 21:31:13 +1100223 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000224 if (dev_priv->hw_status_page) {
225 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 dev_priv->hw_status_page,
227 dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 /* Need to rewrite hardware status page */
229 I810_WRITE(0x02080, 0x1ffff000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700231 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000235 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100237
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238 if (buf_priv->kernel_virtual && buf->total)
Dave Airlieb9094d32007-01-08 21:31:13 +1100239 drm_core_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243}
244
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200245static int i810_wait_ring(struct drm_device *dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246{
247 drm_i810_private_t *dev_priv = dev->dev_private;
248 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
249 int iters = 0;
250 unsigned long end;
251 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
252
253 end = jiffies + (HZ * 3);
254 while (ring->space < n) {
255 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
256 ring->space = ring->head - (ring->tail + 8);
257 if (ring->space < 0)
258 ring->space += ring->Size;
259
260 if (ring->head != last_head) {
261 end = jiffies + (HZ * 3);
262 last_head = ring->head;
263 }
264
265 iters++;
266 if (time_before(end, jiffies)) {
267 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
268 DRM_ERROR("lockup\n");
269 goto out_wait_ring;
270 }
271 udelay(1);
272 }
273
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200274out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000275 return iters;
276}
277
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200278static void i810_kernel_lost_context(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279{
280 drm_i810_private_t *dev_priv = dev->dev_private;
281 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
282
283 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
284 ring->tail = I810_READ(LP_RING + RING_TAIL);
285 ring->space = ring->head - (ring->tail + 8);
286 if (ring->space < 0)
287 ring->space += ring->Size;
288}
289
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200290static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291{
Dave Airliecdd55a22007-07-11 16:32:08 +1000292 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 int my_idx = 24;
294 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
295 int i;
296
297 if (dma->buf_count > 1019) {
298 /* Not enough space in the status page for the freelist */
299 return -EINVAL;
300 }
301
302 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000303 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000304 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
305
306 buf_priv->in_use = hw_status++;
307 buf_priv->my_use_idx = my_idx;
308 my_idx += 4;
309
310 *buf_priv->in_use = I810_BUF_FREE;
311
Dave Airlieb9094d32007-01-08 21:31:13 +1100312 buf_priv->map.offset = buf->bus_address;
313 buf_priv->map.size = buf->total;
314 buf_priv->map.type = _DRM_AGP;
315 buf_priv->map.flags = 0;
316 buf_priv->map.mtrr = 0;
317
318 drm_core_ioremap(&buf_priv->map, dev);
319 buf_priv->kernel_virtual = buf_priv->map.handle;
320
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 }
322 return 0;
323}
324
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200325static int i810_dma_initialize(struct drm_device *dev,
326 drm_i810_private_t *dev_priv,
327 drm_i810_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Dave Airlie55910512007-07-11 16:53:40 +1000329 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000330 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dave Airliebd1b3312007-05-26 05:01:51 +1000332 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (r_list->map &&
334 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 break;
338 }
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!dev_priv->sarea_map) {
341 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 i810_dma_cleanup(dev);
343 DRM_ERROR("can not find sarea!\n");
344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
347 if (!dev_priv->mmio_map) {
348 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 i810_dma_cleanup(dev);
350 DRM_ERROR("can not find mmio map!\n");
351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000353 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
355 if (!dev->agp_buffer_map) {
356 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000357 i810_dma_cleanup(dev);
358 DRM_ERROR("can not find dma buffer map!\n");
359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
362 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000363 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 dev_priv->ring.Start = init->ring_start;
366 dev_priv->ring.End = init->ring_end;
367 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Dave Airlieb9094d32007-01-08 21:31:13 +1100369 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
370 dev_priv->ring.map.size = init->ring_size;
371 dev_priv->ring.map.type = _DRM_AGP;
372 dev_priv->ring.map.flags = 0;
373 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dave Airlieb9094d32007-01-08 21:31:13 +1100375 drm_core_ioremap(&dev_priv->ring.map, dev);
376
377 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000378 dev->dev_private = (void *)dev_priv;
379 i810_dma_cleanup(dev);
380 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000382 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
Dave Airlieb9094d32007-01-08 21:31:13 +1100385 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
386
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 dev_priv->w = init->w;
390 dev_priv->h = init->h;
391 dev_priv->pitch = init->pitch;
392 dev_priv->back_offset = init->back_offset;
393 dev_priv->depth_offset = init->depth_offset;
394 dev_priv->front_offset = init->front_offset;
395
396 dev_priv->overlay_offset = init->overlay_offset;
397 dev_priv->overlay_physical = init->overlay_physical;
398
399 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
400 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
401 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
402
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403 /* Program Hardware Status Page */
404 dev_priv->hw_status_page =
405 pci_alloc_consistent(dev->pdev, PAGE_SIZE,
406 &dev_priv->dma_status_page);
407 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 dev->dev_private = (void *)dev_priv;
409 i810_dma_cleanup(dev);
410 DRM_ERROR("Can not allocate hardware status page\n");
411 return -ENOMEM;
412 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000413 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
414 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (i810_freelist_init(dev, dev_priv) != 0) {
421 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 i810_dma_cleanup(dev);
423 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000425 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 dev->dev_private = (void *)dev_priv;
428
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Eric Anholtc153f452007-09-03 12:06:45 +1000432static int i810_dma_init(struct drm_device *dev, void *data,
433 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000436 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000437 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Eric Anholtc153f452007-09-03 12:06:45 +1000439 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000440 case I810_INIT_DMA_1_4:
441 DRM_INFO("Using v1.4 init.\n");
Eric Anholt9a298b22009-03-24 12:23:04 -0700442 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443 if (dev_priv == NULL)
444 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000445 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 case I810_CLEANUP_DMA:
449 DRM_INFO("DMA Cleanup\n");
450 retcode = i810_dma_cleanup(dev);
451 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000452 default:
453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/* Most efficient way to verify state for the i810 is as it is
460 * emitted. Non-conformant state is silently dropped.
461 *
462 * Use 'volatile' & local var tmp to force the emitted values to be
463 * identical to the verified ones.
464 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200465static void i810EmitContextVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 int i, j = 0;
470 unsigned int tmp;
471 RING_LOCALS;
472
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475 OUT_RING(GFX_OP_COLOR_FACTOR);
476 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000478 OUT_RING(GFX_OP_STIPPLE);
479 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 tmp = code[i];
483
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000484 if ((tmp & (7 << 29)) == (3 << 29) &&
485 (tmp & (0x1f << 24)) < (0x1d << 24)) {
486 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 } else
489 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 ADVANCE_LP_RING();
496}
497
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200498static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000500 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int i, j = 0;
502 unsigned int tmp;
503 RING_LOCALS;
504
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 OUT_RING(GFX_OP_MAP_INFO);
508 OUT_RING(code[I810_TEXREG_MI1]);
509 OUT_RING(code[I810_TEXREG_MI2]);
510 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000512 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 tmp = code[i];
514
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000515 if ((tmp & (7 << 29)) == (3 << 29) &&
516 (tmp & (0x1f << 24)) < (0x1d << 24)) {
517 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 } else
520 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
523 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 ADVANCE_LP_RING();
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/* Need to do some additional checking when setting the dest buffer.
530 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200531static void i810EmitDestVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned int tmp;
536 RING_LOCALS;
537
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 tmp = code[I810_DESTREG_DI1];
541 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000542 OUT_RING(CMD_OP_DESTBUFFER_INFO);
543 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
546 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* invarient:
549 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 OUT_RING(CMD_OP_Z_BUFFER_INFO);
551 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 OUT_RING(GFX_OP_DESTBUFFER_VARS);
554 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000556 OUT_RING(GFX_OP_DRAWRECT_INFO);
557 OUT_RING(code[I810_DESTREG_DR1]);
558 OUT_RING(code[I810_DESTREG_DR2]);
559 OUT_RING(code[I810_DESTREG_DR3]);
560 OUT_RING(code[I810_DESTREG_DR4]);
561 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 ADVANCE_LP_RING();
564}
565
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200566static void i810EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 drm_i810_private_t *dev_priv = dev->dev_private;
569 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000571
Márton Németh3e684ea2008-01-24 15:58:57 +1000572 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
577 }
578
579 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
582 }
583
584 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000585 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
587 }
588
589 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
592 }
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/* need to verify
596 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200597static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 unsigned int clear_color,
599 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 drm_i810_private_t *dev_priv = dev->dev_private;
602 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000604 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int pitch = dev_priv->pitch;
606 int cpp = 2;
607 int i;
608 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609
610 if (dev_priv->current_page == 1) {
611 unsigned int tmp = flags;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 if (tmp & I810_FRONT)
615 flags |= I810_BACK;
616 if (tmp & I810_BACK)
617 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 if (nbox > I810_NR_SAREA_CLIPRECTS)
623 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unsigned int x = pbox->x1;
627 unsigned int y = pbox->y1;
628 unsigned int width = (pbox->x2 - x) * cpp;
629 unsigned int height = pbox->y2 - y;
630 unsigned int start = y * pitch + x * cpp;
631
632 if (pbox->x1 > pbox->x2 ||
633 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000634 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 continue;
636
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 if (flags & I810_FRONT) {
638 BEGIN_LP_RING(6);
639 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
640 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
641 OUT_RING((height << 16) | width);
642 OUT_RING(start);
643 OUT_RING(clear_color);
644 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 ADVANCE_LP_RING();
646 }
647
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 if (flags & I810_BACK) {
649 BEGIN_LP_RING(6);
650 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
651 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
652 OUT_RING((height << 16) | width);
653 OUT_RING(dev_priv->back_offset + start);
654 OUT_RING(clear_color);
655 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 ADVANCE_LP_RING();
657 }
658
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000659 if (flags & I810_DEPTH) {
660 BEGIN_LP_RING(6);
661 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
662 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
663 OUT_RING((height << 16) | width);
664 OUT_RING(dev_priv->depth_offset + start);
665 OUT_RING(clear_zval);
666 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ADVANCE_LP_RING();
668 }
669 }
670}
671
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200672static void i810_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000674 drm_i810_private_t *dev_priv = dev->dev_private;
675 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000677 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int pitch = dev_priv->pitch;
679 int cpp = 2;
680 int i;
681 RING_LOCALS;
682
683 DRM_DEBUG("swapbuffers\n");
684
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 if (nbox > I810_NR_SAREA_CLIPRECTS)
688 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned int w = pbox->x2 - pbox->x1;
692 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 unsigned int start = dst;
695
696 if (pbox->x1 > pbox->x2 ||
697 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000698 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 continue;
700
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 BEGIN_LP_RING(6);
702 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
703 OUT_RING(pitch | (0xCC << 16));
704 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 OUT_RING(dev_priv->back_offset + start);
709 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000713 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ADVANCE_LP_RING();
715 }
716}
717
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200718static void i810_dma_dispatch_vertex(struct drm_device *dev,
719 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000724 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 unsigned long address = (unsigned long)buf->bus_address;
727 unsigned long start = address - dev->agp->base;
728 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000733 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 nbox = I810_NR_SAREA_CLIPRECTS;
735
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 used = 0;
738
739 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
743 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
744
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000745 *(u32 *) buf_priv->kernel_virtual =
746 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000749 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 used += 4;
751 }
752
753 i810_unmap_buffer(buf);
754 }
755
756 if (used) {
757 do {
758 if (i < nbox) {
759 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
761 SC_ENABLE);
762 OUT_RING(GFX_OP_SCISSOR_INFO);
763 OUT_RING(box[i].x1 | (box[i].y1 << 16));
764 OUT_RING((box[i].x2 -
765 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ADVANCE_LP_RING();
767 }
768
769 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 OUT_RING(CMD_OP_BATCH_BUFFER);
771 OUT_RING(start | BB1_PROTECTED);
772 OUT_RING(start + used - 4);
773 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 ADVANCE_LP_RING();
775
776 } while (++i < nbox);
777 }
778
779 if (discard) {
780 dev_priv->counter++;
781
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000782 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
783 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 OUT_RING(CMD_STORE_DWORD_IDX);
787 OUT_RING(20);
788 OUT_RING(dev_priv->counter);
789 OUT_RING(CMD_STORE_DWORD_IDX);
790 OUT_RING(buf_priv->my_use_idx);
791 OUT_RING(I810_BUF_FREE);
792 OUT_RING(CMD_REPORT_HEAD);
793 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 ADVANCE_LP_RING();
795 }
796}
797
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200798static void i810_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int pitch = dev_priv->pitch;
802 RING_LOCALS;
803
Márton Németh3e684ea2008-01-24 15:58:57 +1000804 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 dev_priv->current_page,
806 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 i810_kernel_lost_context(dev);
809
810 BEGIN_LP_RING(2);
811 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
812 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 ADVANCE_LP_RING();
814
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* On i815 at least ASYNC is buggy */
817 /* pitch<<5 is from 11.2.8 p158,
818 its the pitch / 8 then left shifted 8,
819 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
821 if (dev_priv->current_page == 0) {
822 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 dev_priv->current_page = 1;
824 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 dev_priv->current_page = 0;
827 }
828 OUT_RING(0);
829 ADVANCE_LP_RING();
830
831 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
833 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 ADVANCE_LP_RING();
835
836 /* Increment the frame counter. The client-side 3D driver must
837 * throttle the framerate by waiting for this value before
838 * performing the swapbuffer ioctl.
839 */
840 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
841
842}
843
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200844static void i810_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 drm_i810_private_t *dev_priv = dev->dev_private;
847 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 BEGIN_LP_RING(4);
852 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
853 OUT_RING(CMD_REPORT_HEAD);
854 OUT_RING(0);
855 OUT_RING(0);
856 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200861static int i810_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000864 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 int i, ret = 0;
866 RING_LOCALS;
867
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 BEGIN_LP_RING(2);
871 OUT_RING(CMD_REPORT_HEAD);
872 OUT_RING(0);
873 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000878 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
882 I810_BUF_FREE);
883
884 if (used == I810_BUF_HARDWARE)
885 DRM_DEBUG("reclaimed from HARDWARE\n");
886 if (used == I810_BUF_CLIENT)
887 DRM_DEBUG("still on client\n");
888 }
889
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/* Must be called with the lock held */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200894static void i810_reclaim_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000895 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Dave Airliecdd55a22007-07-11 16:32:08 +1000897 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 if (!dma)
901 return;
902 if (!dev->dev_private)
903 return;
904 if (!dma->buflist)
905 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000910 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Eric Anholt6c340ea2007-08-25 20:23:09 +1000913 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
915 I810_BUF_FREE);
916
917 if (used == I810_BUF_CLIENT)
918 DRM_DEBUG("reclaimed from client\n");
919 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 }
923}
924
Eric Anholtc153f452007-09-03 12:06:45 +1000925static int i810_flush_ioctl(struct drm_device *dev, void *data,
926 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000928 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 i810_flush_queue(dev);
931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Eric Anholtc153f452007-09-03 12:06:45 +1000934static int i810_dma_vertex(struct drm_device *dev, void *data,
935 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Dave Airliecdd55a22007-07-11 16:32:08 +1000937 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
939 u32 *hw_status = dev_priv->hw_status_page;
940 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
941 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000942 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Eric Anholt6c340ea2007-08-25 20:23:09 +1000944 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Márton Németh3e684ea2008-01-24 15:58:57 +1000946 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000947 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Eric Anholtc153f452007-09-03 12:06:45 +1000949 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return -EINVAL;
951
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000953 dma->buflist[vertex->idx],
954 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Eric Anholtc153f452007-09-03 12:06:45 +1000956 atomic_add(vertex->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000958 sarea_priv->last_enqueue = dev_priv->counter - 1;
959 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 return 0;
962}
963
Eric Anholtc153f452007-09-03 12:06:45 +1000964static int i810_clear_bufs(struct drm_device *dev, void *data,
965 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Eric Anholtc153f452007-09-03 12:06:45 +1000967 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Eric Anholt6c340ea2007-08-25 20:23:09 +1000969 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 /* GH: Someone's doing nasty things... */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200972 if (!dev->dev_private)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000973 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Eric Anholtc153f452007-09-03 12:06:45 +1000975 i810_dma_dispatch_clear(dev, clear->flags,
976 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Eric Anholtc153f452007-09-03 12:06:45 +1000980static int i810_swap_bufs(struct drm_device *dev, void *data,
981 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Márton Németh3e684ea2008-01-24 15:58:57 +1000983 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Eric Anholt6c340ea2007-08-25 20:23:09 +1000985 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 i810_dma_dispatch_swap(dev);
988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Eric Anholtc153f452007-09-03 12:06:45 +1000991static int i810_getage(struct drm_device *dev, void *data,
992 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000994 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
995 u32 *hw_status = dev_priv->hw_status_page;
996 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
997 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return 0;
1001}
1002
Eric Anholtc153f452007-09-03 12:06:45 +10001003static int i810_getbuf(struct drm_device *dev, void *data,
1004 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001006 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001007 drm_i810_dma_t *d = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
1009 u32 *hw_status = dev_priv->hw_status_page;
1010 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
1011 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Eric Anholt6c340ea2007-08-25 20:23:09 +10001013 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Eric Anholtc153f452007-09-03 12:06:45 +10001015 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Eric Anholtc153f452007-09-03 12:06:45 +10001017 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001020 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001022 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 return retcode;
1025}
1026
Eric Anholtc153f452007-09-03 12:06:45 +10001027static int i810_copybuf(struct drm_device *dev, void *data,
1028 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 /* Never copy - 2.4.x doesn't need it */
1031 return 0;
1032}
1033
Eric Anholtc153f452007-09-03 12:06:45 +10001034static int i810_docopy(struct drm_device *dev, void *data,
1035 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 /* Never copy - 2.4.x doesn't need it */
1038 return 0;
1039}
1040
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001041static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 drm_i810_private_t *dev_priv = dev->dev_private;
1045 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1046 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1047 unsigned long address = (unsigned long)buf->bus_address;
1048 unsigned long start = address - dev->agp->base;
1049 int u;
1050 RING_LOCALS;
1051
1052 i810_kernel_lost_context(dev);
1053
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001054 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001055 if (u != I810_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 DRM_DEBUG("MC found buffer that isn't mine!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 used = 0;
1060
1061 sarea_priv->dirty = 0x7f;
1062
Márton Németh3e684ea2008-01-24 15:58:57 +10001063 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 dev_priv->counter++;
1066 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 DRM_DEBUG("start : %lx\n", start);
1068 DRM_DEBUG("used : %d\n", used);
1069 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1070
1071 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1072 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001073 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 used += 4;
1075 }
1076
1077 i810_unmap_buffer(buf);
1078 }
1079 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 OUT_RING(CMD_OP_BATCH_BUFFER);
1081 OUT_RING(start | BB1_PROTECTED);
1082 OUT_RING(start + used - 4);
1083 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 ADVANCE_LP_RING();
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 OUT_RING(CMD_STORE_DWORD_IDX);
1088 OUT_RING(buf_priv->my_use_idx);
1089 OUT_RING(I810_BUF_FREE);
1090 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 OUT_RING(CMD_STORE_DWORD_IDX);
1093 OUT_RING(16);
1094 OUT_RING(last_render);
1095 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 ADVANCE_LP_RING();
1097}
1098
Eric Anholtc153f452007-09-03 12:06:45 +10001099static int i810_dma_mc(struct drm_device *dev, void *data,
1100 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Dave Airliecdd55a22007-07-11 16:32:08 +10001102 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 u32 *hw_status = dev_priv->hw_status_page;
1105 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001107 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Eric Anholt6c340ea2007-08-25 20:23:09 +10001109 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Eric Anholtc153f452007-09-03 12:06:45 +10001111 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -EINVAL;
1113
Eric Anholtc153f452007-09-03 12:06:45 +10001114 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1115 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Eric Anholtc153f452007-09-03 12:06:45 +10001117 atomic_add(mc->used, &dev->counts[_DRM_STAT_SECONDARY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 atomic_inc(&dev->counts[_DRM_STAT_DMA]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001119 sarea_priv->last_enqueue = dev_priv->counter - 1;
1120 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 return 0;
1123}
1124
Eric Anholtc153f452007-09-03 12:06:45 +10001125static int i810_rstatus(struct drm_device *dev, void *data,
1126 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Eric Anholtc153f452007-09-03 12:06:45 +10001133static int i810_ov0_info(struct drm_device *dev, void *data,
1134 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001136 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001137 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Eric Anholtc153f452007-09-03 12:06:45 +10001139 ov->offset = dev_priv->overlay_offset;
1140 ov->physical = dev_priv->overlay_physical;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return 0;
1143}
1144
Eric Anholtc153f452007-09-03 12:06:45 +10001145static int i810_fstatus(struct drm_device *dev, void *data,
1146 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001148 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Eric Anholt6c340ea2007-08-25 20:23:09 +10001150 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return I810_READ(0x30008);
1152}
1153
Eric Anholtc153f452007-09-03 12:06:45 +10001154static int i810_ov0_flip(struct drm_device *dev, void *data,
1155 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001157 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Eric Anholt6c340ea2007-08-25 20:23:09 +10001159 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001161 /* Tell the overlay to update */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001162 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 return 0;
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001168 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001169static void i810_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001172
Márton Németh3e684ea2008-01-24 15:58:57 +10001173 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 dev_priv->page_flipping = 1;
1175 dev_priv->current_page = 0;
1176 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1177}
1178
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001179static int i810_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 drm_i810_private_t *dev_priv = dev->dev_private;
1182
Márton Németh3e684ea2008-01-24 15:58:57 +10001183 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001185 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 dev_priv->page_flipping = 0;
1188 return 0;
1189}
1190
Eric Anholtc153f452007-09-03 12:06:45 +10001191static int i810_flip_bufs(struct drm_device *dev, void *data,
1192 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 drm_i810_private_t *dev_priv = dev->dev_private;
1195
Márton Németh3e684ea2008-01-24 15:58:57 +10001196 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Eric Anholt6c340ea2007-08-25 20:23:09 +10001198 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001200 if (!dev_priv->page_flipping)
1201 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001203 i810_dma_dispatch_flip(dev);
1204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Dave Airlieeddca552007-07-11 16:09:54 +10001207int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001208{
1209 /* i810 has 4 more counters */
1210 dev->counters += 4;
1211 dev->types[6] = _DRM_STAT_IRQ;
1212 dev->types[7] = _DRM_STAT_PRIMARY;
1213 dev->types[8] = _DRM_STAT_SECONDARY;
1214 dev->types[9] = _DRM_STAT_DMA;
1215
1216 return 0;
1217}
1218
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001219void i810_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001221 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001224void i810_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 if (dev->dev_private) {
1227 drm_i810_private_t *dev_priv = dev->dev_private;
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001228 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 i810_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231}
1232
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001233void i810_driver_reclaim_buffers_locked(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10001234 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Eric Anholt6c340ea2007-08-25 20:23:09 +10001236 i810_reclaim_buffers(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001239int i810_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001241 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return 0;
1243}
1244
Arnd Bergmann58374712010-07-10 23:51:39 +02001245/*
1246 * call the drm_ioctl under the big kernel lock because
1247 * to lock against the i810_mmap_buffers function.
1248 */
1249long i810_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1250{
1251 int ret;
1252 lock_kernel();
1253 ret = drm_ioctl(file, cmd, arg);
1254 unlock_kernel();
1255 return ret;
1256}
1257
Eric Anholtc153f452007-09-03 12:06:45 +10001258struct drm_ioctl_desc i810_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001259 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1260 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1261 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1262 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1263 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1264 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1265 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1266 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1267 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1268 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1269 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1270 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1271 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1272 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1273 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274};
1275
1276int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001277
1278/**
1279 * Determine if the device really is AGP or not.
1280 *
1281 * All Intel graphics chipsets are treated as AGP, even if they are really
1282 * PCI-e.
1283 *
1284 * \param dev The device to be tested.
1285 *
1286 * \returns
1287 * A value of 1 is always retured to indictate every i810 is AGP.
1288 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001289int i810_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10001290{
1291 return 1;
1292}