blob: 576a417690d4774df01345e956237bc868f6fff1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i810_dma.c -- DMA support for the i810 -*- linux-c -*-
2 * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
29 * Keith Whitwell <keith@tungstengraphics.com>
30 *
31 */
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/drmP.h>
34#include <drm/i810_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "i810_drv.h"
36#include <linux/interrupt.h> /* For task queue support */
37#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/pagemap.h>
40
41#define I810_BUF_FREE 2
42#define I810_BUF_CLIENT 1
Dave Airliebc5f4522007-11-05 12:50:58 +100043#define I810_BUF_HARDWARE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define I810_BUF_UNMAPPED 0
46#define I810_BUF_MAPPED 1
47
Dave Airlie056219e2007-07-11 16:17:42 +100048static struct drm_buf *i810_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Dave Airliecdd55a22007-07-11 16:32:08 +100050 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100051 int i;
52 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /* Linear search might not be the best solution */
55
Dave Airlieb5e89ed2005-09-25 14:28:13 +100056 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +100057 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /* In use is already a pointer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 used = cmpxchg(buf_priv->in_use, I810_BUF_FREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 I810_BUF_CLIENT);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +020062 if (used == I810_BUF_FREE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68/* This should only be called if the buffer is not sent to the hardware
69 * yet, the hardware updates in use for us once its on the ring buffer.
70 */
71
Nicolas Kaiseraca791c2010-07-14 21:54:13 +020072static int i810_freelist_put(struct drm_device *dev, struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100074 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
75 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 /* In use is already a pointer */
78 used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_FREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (used != I810_BUF_CLIENT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100080 DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
81 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
Dave Airlieb5e89ed2005-09-25 14:28:13 +100084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Dave Airliec94f7022005-07-07 21:03:38 +100087static int i810_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Dave Airlieeddca552007-07-11 16:09:54 +100089 struct drm_file *priv = filp->private_data;
90 struct drm_device *dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100091 drm_i810_private_t *dev_priv;
Dave Airlie056219e2007-07-11 16:17:42 +100092 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 drm_i810_buf_priv_t *buf_priv;
94
Dave Airlie2c14f282008-04-21 16:47:32 +100095 dev = priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097 buf = dev_priv->mmap_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 buf_priv = buf->dev_private;
99
Al Viro80537962013-05-11 12:27:16 -0400100 vma->vm_flags |= VM_DONTCOPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000102 buf_priv->currently_mapped = I810_BUF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlie3d774612006-08-07 20:07:43 +1000105 vma->vm_pgoff,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106 vma->vm_end - vma->vm_start, vma->vm_page_prot))
107 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800111static const struct file_operations i810_buffer_fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112 .open = drm_open,
Dave Airliec94f7022005-07-07 21:03:38 +1000113 .release = drm_release,
Arnd Bergmann1f692a12011-01-25 23:17:15 +0100114 .unlocked_ioctl = drm_ioctl,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000115 .mmap = i810_mmap_buffers,
Keith Packard804d74a2012-07-09 15:40:07 -0700116 .compat_ioctl = drm_compat_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200117 .llseek = noop_llseek,
Dave Airliec94f7022005-07-07 21:03:38 +1000118};
119
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200120static int i810_map_buffer(struct drm_buf *buf, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Dave Airlie2c14f282008-04-21 16:47:32 +1000122 struct drm_device *dev = file_priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000124 drm_i810_private_t *dev_priv = dev->dev_private;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800125 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int retcode = 0;
127
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return -EINVAL;
130
Linus Torvalds6be5ceb2012-04-20 17:13:58 -0700131 /* This is all entirely broken */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000132 old_fops = file_priv->filp->f_op;
133 file_priv->filp->f_op = &i810_buffer_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 dev_priv->mmap_buffer = buf;
Al Viro244ca2b2012-05-29 21:24:36 -0400135 buf_priv->virtual = (void *)vm_mmap(file_priv->filp, 0, buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000136 PROT_READ | PROT_WRITE,
137 MAP_SHARED, buf->bus_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 dev_priv->mmap_buffer = NULL;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000139 file_priv->filp->f_op = old_fops;
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000140 if (IS_ERR(buf_priv->virtual)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* Real error */
142 DRM_ERROR("mmap error\n");
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000143 retcode = PTR_ERR(buf_priv->virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 buf_priv->virtual = NULL;
145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 return retcode;
148}
149
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200150static int i810_unmap_buffer(struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
153 int retcode = 0;
154
155 if (buf_priv->currently_mapped != I810_BUF_MAPPED)
156 return -EINVAL;
157
Al Virobfce2812012-04-20 21:57:04 -0400158 retcode = vm_munmap((unsigned long)buf_priv->virtual,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 (size_t) buf->total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000161 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
162 buf_priv->virtual = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return retcode;
165}
166
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200167static int i810_dma_get_buffer(struct drm_device *dev, drm_i810_dma_t *d,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000168 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Dave Airlie056219e2007-07-11 16:17:42 +1000170 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 drm_i810_buf_priv_t *buf_priv;
172 int retcode = 0;
173
174 buf = i810_freelist_get(dev);
175 if (!buf) {
176 retcode = -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000177 DRM_DEBUG("retcode=%d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return retcode;
179 }
180
Eric Anholt6c340ea2007-08-25 20:23:09 +1000181 retcode = i810_map_buffer(buf, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (retcode) {
183 i810_freelist_put(dev, buf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184 DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return retcode;
186 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000187 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 buf_priv = buf->dev_private;
189 d->granted = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000190 d->request_idx = buf->idx;
191 d->request_size = buf->total;
192 d->virtual = buf_priv->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 return retcode;
195}
196
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200197static int i810_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Dave Airliecdd55a22007-07-11 16:32:08 +1000199 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* Make sure interrupts are disabled here because the uninstall ioctl
202 * may not have been called from userspace and after dev_private
203 * is freed, it's too late.
204 */
205 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
206 drm_irq_uninstall(dev);
207
208 if (dev->dev_private) {
209 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 drm_i810_private_t *dev_priv =
211 (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200213 if (dev_priv->ring.virtual_start)
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200214 drm_legacy_ioremapfree(&dev_priv->ring.map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000215 if (dev_priv->hw_status_page) {
216 pci_free_consistent(dev->pdev, PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 dev_priv->hw_status_page,
218 dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700220 kfree(dev->dev_private);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000221 dev->dev_private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000224 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb9094d32007-01-08 21:31:13 +1100226
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227 if (buf_priv->kernel_virtual && buf->total)
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200228 drm_legacy_ioremapfree(&buf_priv->map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232}
233
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200234static int i810_wait_ring(struct drm_device *dev, int n)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235{
236 drm_i810_private_t *dev_priv = dev->dev_private;
237 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
238 int iters = 0;
239 unsigned long end;
240 unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
241
242 end = jiffies + (HZ * 3);
243 while (ring->space < n) {
244 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
245 ring->space = ring->head - (ring->tail + 8);
246 if (ring->space < 0)
247 ring->space += ring->Size;
248
249 if (ring->head != last_head) {
250 end = jiffies + (HZ * 3);
251 last_head = ring->head;
252 }
253
254 iters++;
255 if (time_before(end, jiffies)) {
256 DRM_ERROR("space: %d wanted %d\n", ring->space, n);
257 DRM_ERROR("lockup\n");
258 goto out_wait_ring;
259 }
260 udelay(1);
261 }
262
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200263out_wait_ring:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000264 return iters;
265}
266
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200267static void i810_kernel_lost_context(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268{
269 drm_i810_private_t *dev_priv = dev->dev_private;
270 drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
271
272 ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
273 ring->tail = I810_READ(LP_RING + RING_TAIL);
274 ring->space = ring->head - (ring->tail + 8);
275 if (ring->space < 0)
276 ring->space += ring->Size;
277}
278
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200279static int i810_freelist_init(struct drm_device *dev, drm_i810_private_t *dev_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280{
Dave Airliecdd55a22007-07-11 16:32:08 +1000281 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000282 int my_idx = 24;
283 u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
284 int i;
285
286 if (dma->buf_count > 1019) {
287 /* Not enough space in the status page for the freelist */
288 return -EINVAL;
289 }
290
291 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000292 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
294
295 buf_priv->in_use = hw_status++;
296 buf_priv->my_use_idx = my_idx;
297 my_idx += 4;
298
299 *buf_priv->in_use = I810_BUF_FREE;
300
Dave Airlieb9094d32007-01-08 21:31:13 +1100301 buf_priv->map.offset = buf->bus_address;
302 buf_priv->map.size = buf->total;
303 buf_priv->map.type = _DRM_AGP;
304 buf_priv->map.flags = 0;
305 buf_priv->map.mtrr = 0;
306
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200307 drm_legacy_ioremap(&buf_priv->map, dev);
Dave Airlieb9094d32007-01-08 21:31:13 +1100308 buf_priv->kernel_virtual = buf_priv->map.handle;
309
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 }
311 return 0;
312}
313
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200314static int i810_dma_initialize(struct drm_device *dev,
315 drm_i810_private_t *dev_priv,
316 drm_i810_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Dave Airlie55910512007-07-11 16:53:40 +1000318 struct drm_map_list *r_list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000319 memset(dev_priv, 0, sizeof(drm_i810_private_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Dave Airliebd1b3312007-05-26 05:01:51 +1000321 list_for_each_entry(r_list, &dev->maplist, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (r_list->map &&
323 r_list->map->type == _DRM_SHM &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 r_list->map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 dev_priv->sarea_map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326 break;
327 }
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!dev_priv->sarea_map) {
330 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 i810_dma_cleanup(dev);
332 DRM_ERROR("can not find sarea!\n");
333 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200335 dev_priv->mmio_map = drm_legacy_findmap(dev, init->mmio_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!dev_priv->mmio_map) {
337 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 i810_dma_cleanup(dev);
339 DRM_ERROR("can not find mmio map!\n");
340 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000342 dev->agp_buffer_token = init->buffers_offset;
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200343 dev->agp_buffer_map = drm_legacy_findmap(dev, init->buffers_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (!dev->agp_buffer_map) {
345 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 i810_dma_cleanup(dev);
347 DRM_ERROR("can not find dma buffer map!\n");
348 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 dev_priv->sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000352 ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 dev_priv->ring.Start = init->ring_start;
355 dev_priv->ring.End = init->ring_end;
356 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Dave Airlieb9094d32007-01-08 21:31:13 +1100358 dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
359 dev_priv->ring.map.size = init->ring_size;
360 dev_priv->ring.map.type = _DRM_AGP;
361 dev_priv->ring.map.flags = 0;
362 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200364 drm_legacy_ioremap(&dev_priv->ring.map, dev);
Dave Airlieb9094d32007-01-08 21:31:13 +1100365
366 if (dev_priv->ring.map.handle == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000367 dev->dev_private = (void *)dev_priv;
368 i810_dma_cleanup(dev);
369 DRM_ERROR("can not ioremap virtual address for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000371 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Dave Airlieb9094d32007-01-08 21:31:13 +1100374 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
375
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 dev_priv->w = init->w;
379 dev_priv->h = init->h;
380 dev_priv->pitch = init->pitch;
381 dev_priv->back_offset = init->back_offset;
382 dev_priv->depth_offset = init->depth_offset;
383 dev_priv->front_offset = init->front_offset;
384
385 dev_priv->overlay_offset = init->overlay_offset;
386 dev_priv->overlay_physical = init->overlay_physical;
387
388 dev_priv->front_di1 = init->front_offset | init->pitch_bits;
389 dev_priv->back_di1 = init->back_offset | init->pitch_bits;
390 dev_priv->zi1 = init->depth_offset | init->pitch_bits;
391
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392 /* Program Hardware Status Page */
393 dev_priv->hw_status_page =
Joe Perches59e26232014-08-08 14:24:19 -0700394 pci_zalloc_consistent(dev->pdev, PAGE_SIZE,
395 &dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396 if (!dev_priv->hw_status_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 dev->dev_private = (void *)dev_priv;
398 i810_dma_cleanup(dev);
399 DRM_ERROR("Can not allocate hardware status page\n");
400 return -ENOMEM;
401 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000402 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 I810_WRITE(0x02080, dev_priv->dma_status_page);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000405 DRM_DEBUG("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000407 /* Now we need to init our freelist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (i810_freelist_init(dev, dev_priv) != 0) {
409 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 i810_dma_cleanup(dev);
411 DRM_ERROR("Not enough space in the status page for"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 " the freelist\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000413 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 dev->dev_private = (void *)dev_priv;
416
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Eric Anholtc153f452007-09-03 12:06:45 +1000420static int i810_dma_init(struct drm_device *dev, void *data,
421 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000423 drm_i810_private_t *dev_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000424 drm_i810_init_t *init = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000425 int retcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Eric Anholtc153f452007-09-03 12:06:45 +1000427 switch (init->func) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000428 case I810_INIT_DMA_1_4:
429 DRM_INFO("Using v1.4 init.\n");
Eric Anholt9a298b22009-03-24 12:23:04 -0700430 dev_priv = kmalloc(sizeof(drm_i810_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 if (dev_priv == NULL)
432 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000433 retcode = i810_dma_initialize(dev, dev_priv, init);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000436 case I810_CLEANUP_DMA:
437 DRM_INFO("DMA Cleanup\n");
438 retcode = i810_dma_cleanup(dev);
439 break;
Eric Anholtc153f452007-09-03 12:06:45 +1000440 default:
441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444 return retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/* Most efficient way to verify state for the i810 is as it is
448 * emitted. Non-conformant state is silently dropped.
449 *
450 * Use 'volatile' & local var tmp to force the emitted values to be
451 * identical to the verified ones.
452 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200453static void i810EmitContextVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000454 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int i, j = 0;
458 unsigned int tmp;
459 RING_LOCALS;
460
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 OUT_RING(GFX_OP_COLOR_FACTOR);
464 OUT_RING(code[I810_CTXREG_CF1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 OUT_RING(GFX_OP_STIPPLE);
467 OUT_RING(code[I810_CTXREG_ST1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000469 for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 tmp = code[i];
471
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000472 if ((tmp & (7 << 29)) == (3 << 29) &&
473 (tmp & (0x1f << 24)) < (0x1d << 24)) {
474 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 } else
477 printk("constext state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479
480 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 ADVANCE_LP_RING();
484}
485
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200486static void i810EmitTexVerified(struct drm_device *dev, volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int i, j = 0;
490 unsigned int tmp;
491 RING_LOCALS;
492
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493 BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000495 OUT_RING(GFX_OP_MAP_INFO);
496 OUT_RING(code[I810_TEXREG_MI1]);
497 OUT_RING(code[I810_TEXREG_MI2]);
498 OUT_RING(code[I810_TEXREG_MI3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000500 for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 tmp = code[i];
502
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000503 if ((tmp & (7 << 29)) == (3 << 29) &&
504 (tmp & (0x1f << 24)) < (0x1d << 24)) {
505 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 j++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 } else
508 printk("texture state dropped!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 if (j & 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000512 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 ADVANCE_LP_RING();
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/* Need to do some additional checking when setting the dest buffer.
518 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200519static void i810EmitDestVerified(struct drm_device *dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 volatile unsigned int *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000522 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 unsigned int tmp;
524 RING_LOCALS;
525
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000526 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 tmp = code[I810_DESTREG_DI1];
529 if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530 OUT_RING(CMD_OP_DESTBUFFER_INFO);
531 OUT_RING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
534 tmp, dev_priv->front_di1, dev_priv->back_di1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* invarient:
537 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 OUT_RING(CMD_OP_Z_BUFFER_INFO);
539 OUT_RING(dev_priv->zi1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 OUT_RING(GFX_OP_DESTBUFFER_VARS);
542 OUT_RING(code[I810_DESTREG_DV1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000544 OUT_RING(GFX_OP_DRAWRECT_INFO);
545 OUT_RING(code[I810_DESTREG_DR1]);
546 OUT_RING(code[I810_DESTREG_DR2]);
547 OUT_RING(code[I810_DESTREG_DR3]);
548 OUT_RING(code[I810_DESTREG_DR4]);
549 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 ADVANCE_LP_RING();
552}
553
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200554static void i810EmitState(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000556 drm_i810_private_t *dev_priv = dev->dev_private;
557 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned int dirty = sarea_priv->dirty;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000559
Márton Németh3e684ea2008-01-24 15:58:57 +1000560 DRM_DEBUG("%x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (dirty & I810_UPLOAD_BUFFERS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 i810EmitDestVerified(dev, sarea_priv->BufferState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
565 }
566
567 if (dirty & I810_UPLOAD_CTX) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 i810EmitContextVerified(dev, sarea_priv->ContextState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 sarea_priv->dirty &= ~I810_UPLOAD_CTX;
570 }
571
572 if (dirty & I810_UPLOAD_TEX0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000573 i810EmitTexVerified(dev, sarea_priv->TexState[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
575 }
576
577 if (dirty & I810_UPLOAD_TEX1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 i810EmitTexVerified(dev, sarea_priv->TexState[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
580 }
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/* need to verify
584 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200585static void i810_dma_dispatch_clear(struct drm_device *dev, int flags,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 unsigned int clear_color,
587 unsigned int clear_zval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 drm_i810_private_t *dev_priv = dev->dev_private;
590 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000592 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int pitch = dev_priv->pitch;
594 int cpp = 2;
595 int i;
596 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597
598 if (dev_priv->current_page == 1) {
599 unsigned int tmp = flags;
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 flags &= ~(I810_FRONT | I810_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602 if (tmp & I810_FRONT)
603 flags |= I810_BACK;
604 if (tmp & I810_BACK)
605 flags |= I810_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000610 if (nbox > I810_NR_SAREA_CLIPRECTS)
611 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 unsigned int x = pbox->x1;
615 unsigned int y = pbox->y1;
616 unsigned int width = (pbox->x2 - x) * cpp;
617 unsigned int height = pbox->y2 - y;
618 unsigned int start = y * pitch + x * cpp;
619
620 if (pbox->x1 > pbox->x2 ||
621 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 continue;
624
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 if (flags & I810_FRONT) {
626 BEGIN_LP_RING(6);
627 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
628 OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
629 OUT_RING((height << 16) | width);
630 OUT_RING(start);
631 OUT_RING(clear_color);
632 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 ADVANCE_LP_RING();
634 }
635
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636 if (flags & I810_BACK) {
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(dev_priv->back_offset + 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_DEPTH) {
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->depth_offset + start);
653 OUT_RING(clear_zval);
654 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 ADVANCE_LP_RING();
656 }
657 }
658}
659
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200660static void i810_dma_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000662 drm_i810_private_t *dev_priv = dev->dev_private;
663 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000665 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int pitch = dev_priv->pitch;
667 int cpp = 2;
668 int i;
669 RING_LOCALS;
670
671 DRM_DEBUG("swapbuffers\n");
672
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 if (nbox > I810_NR_SAREA_CLIPRECTS)
676 nbox = I810_NR_SAREA_CLIPRECTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 for (i = 0; i < nbox; i++, pbox++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned int w = pbox->x2 - pbox->x1;
680 unsigned int h = pbox->y2 - pbox->y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000681 unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 unsigned int start = dst;
683
684 if (pbox->x1 > pbox->x2 ||
685 pbox->y1 > pbox->y2 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 continue;
688
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 BEGIN_LP_RING(6);
690 OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
691 OUT_RING(pitch | (0xCC << 16));
692 OUT_RING((h << 16) | (w * cpp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 OUT_RING(dev_priv->back_offset + start);
697 OUT_RING(pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 OUT_RING(dev_priv->back_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 OUT_RING(dev_priv->front_offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ADVANCE_LP_RING();
703 }
704}
705
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200706static void i810_dma_dispatch_vertex(struct drm_device *dev,
707 struct drm_buf *buf, int discard, int used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieeddca552007-07-11 16:09:54 +1000712 struct drm_clip_rect *box = sarea_priv->boxes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000713 int nbox = sarea_priv->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 unsigned long address = (unsigned long)buf->bus_address;
715 unsigned long start = address - dev->agp->base;
716 int i = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 if (nbox > I810_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 nbox = I810_NR_SAREA_CLIPRECTS;
723
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 used = 0;
726
727 if (sarea_priv->dirty)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 i810EmitState(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
731 unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
732
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000733 *(u32 *) buf_priv->kernel_virtual =
734 ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +1000737 *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 used += 4;
739 }
740
741 i810_unmap_buffer(buf);
742 }
743
744 if (used) {
745 do {
746 if (i < nbox) {
747 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
749 SC_ENABLE);
750 OUT_RING(GFX_OP_SCISSOR_INFO);
751 OUT_RING(box[i].x1 | (box[i].y1 << 16));
752 OUT_RING((box[i].x2 -
753 1) | ((box[i].y2 - 1) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 ADVANCE_LP_RING();
755 }
756
757 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000758 OUT_RING(CMD_OP_BATCH_BUFFER);
759 OUT_RING(start | BB1_PROTECTED);
760 OUT_RING(start + used - 4);
761 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ADVANCE_LP_RING();
763
764 } while (++i < nbox);
765 }
766
767 if (discard) {
768 dev_priv->counter++;
769
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
771 I810_BUF_HARDWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 OUT_RING(CMD_STORE_DWORD_IDX);
775 OUT_RING(20);
776 OUT_RING(dev_priv->counter);
777 OUT_RING(CMD_STORE_DWORD_IDX);
778 OUT_RING(buf_priv->my_use_idx);
779 OUT_RING(I810_BUF_FREE);
780 OUT_RING(CMD_REPORT_HEAD);
781 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 ADVANCE_LP_RING();
783 }
784}
785
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200786static void i810_dma_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 drm_i810_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int pitch = dev_priv->pitch;
790 RING_LOCALS;
791
Márton Németh3e684ea2008-01-24 15:58:57 +1000792 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 dev_priv->current_page,
794 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 i810_kernel_lost_context(dev);
797
798 BEGIN_LP_RING(2);
799 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
800 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ADVANCE_LP_RING();
802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* On i815 at least ASYNC is buggy */
805 /* pitch<<5 is from 11.2.8 p158,
806 its the pitch / 8 then left shifted 8,
807 so (pitch >> 3) << 8 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
809 if (dev_priv->current_page == 0) {
810 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 dev_priv->current_page = 1;
812 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 dev_priv->current_page = 0;
815 }
816 OUT_RING(0);
817 ADVANCE_LP_RING();
818
819 BEGIN_LP_RING(2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
821 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ADVANCE_LP_RING();
823
824 /* Increment the frame counter. The client-side 3D driver must
825 * throttle the framerate by waiting for this value before
826 * performing the swapbuffer ioctl.
827 */
828 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
829
830}
831
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200832static void i810_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000834 drm_i810_private_t *dev_priv = dev->dev_private;
835 RING_LOCALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 BEGIN_LP_RING(4);
840 OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
841 OUT_RING(CMD_REPORT_HEAD);
842 OUT_RING(0);
843 OUT_RING(0);
844 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200849static int i810_flush_queue(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000852 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 int i, ret = 0;
854 RING_LOCALS;
855
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 i810_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 BEGIN_LP_RING(2);
859 OUT_RING(CMD_REPORT_HEAD);
860 OUT_RING(0);
861 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 i810_wait_ring(dev, dev_priv->ring.Size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000866 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
870 I810_BUF_FREE);
871
872 if (used == I810_BUF_HARDWARE)
873 DRM_DEBUG("reclaimed from HARDWARE\n");
874 if (used == I810_BUF_CLIENT)
875 DRM_DEBUG("still on client\n");
876 }
877
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881/* Must be called with the lock held */
Daniel Vetterd5346b32012-06-11 21:50:23 +0200882void i810_driver_reclaim_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000883 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Dave Airliecdd55a22007-07-11 16:32:08 +1000885 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 if (!dma)
889 return;
890 if (!dev->dev_private)
891 return;
892 if (!dma->buflist)
893 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 i810_flush_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000898 struct drm_buf *buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Eric Anholt6c340ea2007-08-25 20:23:09 +1000901 if (buf->file_priv == file_priv && buf_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
903 I810_BUF_FREE);
904
905 if (used == I810_BUF_CLIENT)
906 DRM_DEBUG("reclaimed from client\n");
907 if (buf_priv->currently_mapped == I810_BUF_MAPPED)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 buf_priv->currently_mapped = I810_BUF_UNMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 }
911}
912
Eric Anholtc153f452007-09-03 12:06:45 +1000913static int i810_flush_ioctl(struct drm_device *dev, void *data,
914 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000916 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 i810_flush_queue(dev);
919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Eric Anholtc153f452007-09-03 12:06:45 +1000922static int i810_dma_vertex(struct drm_device *dev, void *data,
923 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Dave Airliecdd55a22007-07-11 16:32:08 +1000925 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
927 u32 *hw_status = dev_priv->hw_status_page;
928 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
929 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000930 drm_i810_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Eric Anholt6c340ea2007-08-25 20:23:09 +1000932 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Márton Németh3e684ea2008-01-24 15:58:57 +1000934 DRM_DEBUG("idx %d used %d discard %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000935 vertex->idx, vertex->used, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Eric Anholtc153f452007-09-03 12:06:45 +1000937 if (vertex->idx < 0 || vertex->idx > dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return -EINVAL;
939
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000940 i810_dma_dispatch_vertex(dev,
Eric Anholtc153f452007-09-03 12:06:45 +1000941 dma->buflist[vertex->idx],
942 vertex->discard, vertex->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000944 sarea_priv->last_enqueue = dev_priv->counter - 1;
945 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 return 0;
948}
949
Eric Anholtc153f452007-09-03 12:06:45 +1000950static int i810_clear_bufs(struct drm_device *dev, void *data,
951 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Eric Anholtc153f452007-09-03 12:06:45 +1000953 drm_i810_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Eric Anholt6c340ea2007-08-25 20:23:09 +1000955 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000957 /* GH: Someone's doing nasty things... */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +0200958 if (!dev->dev_private)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000959 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Eric Anholtc153f452007-09-03 12:06:45 +1000961 i810_dma_dispatch_clear(dev, clear->flags,
962 clear->clear_color, clear->clear_depth);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Eric Anholtc153f452007-09-03 12:06:45 +1000966static int i810_swap_bufs(struct drm_device *dev, void *data,
967 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Márton Németh3e684ea2008-01-24 15:58:57 +1000969 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Eric Anholt6c340ea2007-08-25 20:23:09 +1000971 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000973 i810_dma_dispatch_swap(dev);
974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Eric Anholtc153f452007-09-03 12:06:45 +1000977static int i810_getage(struct drm_device *dev, void *data,
978 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000980 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
981 u32 *hw_status = dev_priv->hw_status_page;
982 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
983 dev_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000985 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987}
988
Eric Anholtc153f452007-09-03 12:06:45 +1000989static int i810_getbuf(struct drm_device *dev, void *data,
990 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 int retcode = 0;
Eric Anholtc153f452007-09-03 12:06:45 +1000993 drm_i810_dma_t *d = data;
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
Eric Anholt6c340ea2007-08-25 20:23:09 +1000999 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Eric Anholtc153f452007-09-03 12:06:45 +10001001 d->granted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Anholtc153f452007-09-03 12:06:45 +10001003 retcode = i810_dma_get_buffer(dev, d, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001006 task_pid_nr(current), retcode, d->granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 return retcode;
1011}
1012
Eric Anholtc153f452007-09-03 12:06:45 +10001013static int i810_copybuf(struct drm_device *dev, void *data,
1014 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 /* Never copy - 2.4.x doesn't need it */
1017 return 0;
1018}
1019
Eric Anholtc153f452007-09-03 12:06:45 +10001020static int i810_docopy(struct drm_device *dev, void *data,
1021 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 /* Never copy - 2.4.x doesn't need it */
1024 return 0;
1025}
1026
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001027static void i810_dma_dispatch_mc(struct drm_device *dev, struct drm_buf *buf, int used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 unsigned int last_render)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 drm_i810_private_t *dev_priv = dev->dev_private;
1031 drm_i810_buf_priv_t *buf_priv = buf->dev_private;
1032 drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
1033 unsigned long address = (unsigned long)buf->bus_address;
1034 unsigned long start = address - dev->agp->base;
1035 int u;
1036 RING_LOCALS;
1037
1038 i810_kernel_lost_context(dev);
1039
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001041 if (u != I810_BUF_CLIENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 DRM_DEBUG("MC found buffer that isn't mine!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 if (used > 4 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 used = 0;
1046
1047 sarea_priv->dirty = 0x7f;
1048
Márton Németh3e684ea2008-01-24 15:58:57 +10001049 DRM_DEBUG("addr 0x%lx, used 0x%x\n", address, used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 dev_priv->counter++;
1052 DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 DRM_DEBUG("start : %lx\n", start);
1054 DRM_DEBUG("used : %d\n", used);
1055 DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
1056
1057 if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
1058 if (used & 4) {
Denis Vlasenkoc7aed172006-08-16 11:54:07 +10001059 *(u32 *) ((char *) buf_priv->virtual + used) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 used += 4;
1061 }
1062
1063 i810_unmap_buffer(buf);
1064 }
1065 BEGIN_LP_RING(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 OUT_RING(CMD_OP_BATCH_BUFFER);
1067 OUT_RING(start | BB1_PROTECTED);
1068 OUT_RING(start + used - 4);
1069 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ADVANCE_LP_RING();
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 BEGIN_LP_RING(8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 OUT_RING(CMD_STORE_DWORD_IDX);
1074 OUT_RING(buf_priv->my_use_idx);
1075 OUT_RING(I810_BUF_FREE);
1076 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 OUT_RING(CMD_STORE_DWORD_IDX);
1079 OUT_RING(16);
1080 OUT_RING(last_render);
1081 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ADVANCE_LP_RING();
1083}
1084
Eric Anholtc153f452007-09-03 12:06:45 +10001085static int i810_dma_mc(struct drm_device *dev, void *data,
1086 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Dave Airliecdd55a22007-07-11 16:32:08 +10001088 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 u32 *hw_status = dev_priv->hw_status_page;
1091 drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001093 drm_i810_mc_t *mc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Eric Anholt6c340ea2007-08-25 20:23:09 +10001095 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Eric Anholtc153f452007-09-03 12:06:45 +10001097 if (mc->idx >= dma->buf_count || mc->idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return -EINVAL;
1099
Eric Anholtc153f452007-09-03 12:06:45 +10001100 i810_dma_dispatch_mc(dev, dma->buflist[mc->idx], mc->used,
1101 mc->last_render);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 sarea_priv->last_enqueue = dev_priv->counter - 1;
1104 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 return 0;
1107}
1108
Eric Anholtc153f452007-09-03 12:06:45 +10001109static int i810_rstatus(struct drm_device *dev, void *data,
1110 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Eric Anholtc153f452007-09-03 12:06:45 +10001117static int i810_ov0_info(struct drm_device *dev, void *data,
1118 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001121 drm_i810_overlay_t *ov = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Eric Anholtc153f452007-09-03 12:06:45 +10001123 ov->offset = dev_priv->overlay_offset;
1124 ov->physical = dev_priv->overlay_physical;
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return 0;
1127}
1128
Eric Anholtc153f452007-09-03 12:06:45 +10001129static int i810_fstatus(struct drm_device *dev, void *data,
1130 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Eric Anholt6c340ea2007-08-25 20:23:09 +10001134 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return I810_READ(0x30008);
1136}
1137
Eric Anholtc153f452007-09-03 12:06:45 +10001138static int i810_ov0_flip(struct drm_device *dev, void *data,
1139 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141 drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Eric Anholt6c340ea2007-08-25 20:23:09 +10001143 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001145 /* Tell the overlay to update */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001146 I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 return 0;
1149}
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001152 */
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001153static void i810_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 drm_i810_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001156
Márton Németh3e684ea2008-01-24 15:58:57 +10001157 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 dev_priv->page_flipping = 1;
1159 dev_priv->current_page = 0;
1160 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
1161}
1162
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001163static int i810_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 drm_i810_private_t *dev_priv = dev->dev_private;
1166
Márton Németh3e684ea2008-01-24 15:58:57 +10001167 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (dev_priv->current_page != 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001169 i810_dma_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 dev_priv->page_flipping = 0;
1172 return 0;
1173}
1174
Eric Anholtc153f452007-09-03 12:06:45 +10001175static int i810_flip_bufs(struct drm_device *dev, void *data,
1176 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 drm_i810_private_t *dev_priv = dev->dev_private;
1179
Márton Németh3e684ea2008-01-24 15:58:57 +10001180 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Eric Anholt6c340ea2007-08-25 20:23:09 +10001182 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001184 if (!dev_priv->page_flipping)
1185 i810_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001187 i810_dma_dispatch_flip(dev);
1188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Dave Airlieeddca552007-07-11 16:09:54 +10001191int i810_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001192{
Daniel Vetter49d66d82017-01-25 07:26:50 +01001193 dev->agp = drm_agp_init(dev);
1194 if (dev->agp) {
1195 dev->agp->agp_mtrr = arch_phys_wc_add(
1196 dev->agp->agp_info.aper_base,
1197 dev->agp->agp_info.aper_size *
1198 1024 * 1024);
1199 }
1200
Daniel Vetter24986ee2013-12-11 11:34:33 +01001201 /* Our userspace depends upon the agp mapping support. */
1202 if (!dev->agp)
1203 return -EINVAL;
1204
Dave Airlie466e69b2011-12-19 11:15:29 +00001205 pci_set_master(dev->pdev);
1206
Dave Airlie22eae942005-11-10 22:16:34 +11001207 return 0;
1208}
1209
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001210void i810_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001212 i810_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001215void i810_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 if (dev->dev_private) {
1218 drm_i810_private_t *dev_priv = dev->dev_private;
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001219 if (dev_priv->page_flipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 i810_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Daniel Vetterd5346b32012-06-11 21:50:23 +02001223 if (file_priv->master && file_priv->master->lock.hw_lock) {
David Herrmannbb6d8222014-08-29 12:12:46 +02001224 drm_legacy_idlelock_take(&file_priv->master->lock);
Daniel Vetterd5346b32012-06-11 21:50:23 +02001225 i810_driver_reclaim_buffers(dev, file_priv);
David Herrmannbb6d8222014-08-29 12:12:46 +02001226 drm_legacy_idlelock_release(&file_priv->master->lock);
Daniel Vetterd5346b32012-06-11 21:50:23 +02001227 } else {
1228 /* master disappeared, clean up stuff anyway and hope nothing
1229 * goes wrong */
1230 i810_driver_reclaim_buffers(dev, file_priv);
1231 }
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Nicolas Kaiseraca791c2010-07-14 21:54:13 +02001235int i810_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001237 i810_dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return 0;
1239}
1240
Rob Clarkbaa70942013-08-02 13:27:49 -04001241const struct drm_ioctl_desc i810_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001242 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1243 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1244 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1245 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1246 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1247 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1248 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1249 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1250 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1251 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1252 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1253 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1254 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1255 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1256 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257};
1258
Damien Lespiauf95aeb12014-06-09 14:39:49 +01001259int i810_max_ioctl = ARRAY_SIZE(i810_ioctls);