blob: 36f2547254f9321fbf4864d4e591eee1998e377b [file] [log] [blame]
Dave Airlie22f579c2005-06-28 22:48:56 +10001/*
Thomas Hellstromce65a442006-08-07 22:03:22 +10002 * Copyright 2006 Tungsten Graphics Inc., Bismarck, ND., USA.
3 * All rights reserved.
Dave Airlie22f579c2005-06-28 22:48:56 +10004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
Thomas Hellstromce65a442006-08-07 22:03:22 +100019 * THE AUTHORS OR COPYRIGHT HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Dave Airlie22f579c2005-06-28 22:48:56 +100021 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Thomas Hellstromce65a442006-08-07 22:03:22 +100024/*
25 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
26 */
27
Dave Airlie22f579c2005-06-28 22:48:56 +100028#include "drmP.h"
29#include "via_drm.h"
30#include "via_drv.h"
Thomas Hellstromce65a442006-08-07 22:03:22 +100031#include "drm_sman.h"
Dave Airlie22f579c2005-06-28 22:48:56 +100032
Thomas Hellstromce65a442006-08-07 22:03:22 +100033#define VIA_MM_ALIGN_SHIFT 4
34#define VIA_MM_ALIGN_MASK ( (1 << VIA_MM_ALIGN_SHIFT) - 1)
Dave Airlie22f579c2005-06-28 22:48:56 +100035
36int via_agp_init(DRM_IOCTL_ARGS)
37{
Thomas Hellstromce65a442006-08-07 22:03:22 +100038 DRM_DEVICE;
Dave Airlie22f579c2005-06-28 22:48:56 +100039 drm_via_agp_t agp;
Thomas Hellstromce65a442006-08-07 22:03:22 +100040 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
41 int ret;
Dave Airlie22f579c2005-06-28 22:48:56 +100042
Alexey Dobriyanbbaf3642005-07-27 11:43:56 -070043 DRM_COPY_FROM_USER_IOCTL(agp, (drm_via_agp_t __user *) data,
44 sizeof(agp));
Thomas Hellstromce65a442006-08-07 22:03:22 +100045 mutex_lock(&dev->struct_mutex);
46 ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_AGP, 0,
47 agp.size >> VIA_MM_ALIGN_SHIFT);
Dave Airlie22f579c2005-06-28 22:48:56 +100048
Thomas Hellstromce65a442006-08-07 22:03:22 +100049 if (ret) {
50 DRM_ERROR("AGP memory manager initialisation error\n");
51 mutex_unlock(&dev->struct_mutex);
52 return ret;
53 }
Dave Airlie22f579c2005-06-28 22:48:56 +100054
Dave Airlied40c8532006-08-19 17:40:50 +100055 dev_priv->agp_initialized = 1;
Thomas Hellstromce65a442006-08-07 22:03:22 +100056 dev_priv->agp_offset = agp.offset;
57 mutex_unlock(&dev->struct_mutex);
Dave Airlie22f579c2005-06-28 22:48:56 +100058
Thomas Hellstromce65a442006-08-07 22:03:22 +100059 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
Dave Airlie22f579c2005-06-28 22:48:56 +100060 return 0;
61}
62
Dave Airlie22f579c2005-06-28 22:48:56 +100063int via_fb_init(DRM_IOCTL_ARGS)
64{
Thomas Hellstromce65a442006-08-07 22:03:22 +100065 DRM_DEVICE;
Dave Airlie22f579c2005-06-28 22:48:56 +100066 drm_via_fb_t fb;
Thomas Hellstromce65a442006-08-07 22:03:22 +100067 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
68 int ret;
Dave Airlie22f579c2005-06-28 22:48:56 +100069
Alexey Dobriyanbbaf3642005-07-27 11:43:56 -070070 DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t __user *) data, sizeof(fb));
Dave Airlie22f579c2005-06-28 22:48:56 +100071
Thomas Hellstromce65a442006-08-07 22:03:22 +100072 mutex_lock(&dev->struct_mutex);
73 ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_VIDEO, 0,
74 fb.size >> VIA_MM_ALIGN_SHIFT);
Dave Airlie22f579c2005-06-28 22:48:56 +100075
Thomas Hellstromce65a442006-08-07 22:03:22 +100076 if (ret) {
77 DRM_ERROR("VRAM memory manager initialisation error\n");
78 mutex_unlock(&dev->struct_mutex);
79 return ret;
Dave Airlie22f579c2005-06-28 22:48:56 +100080 }
81
Dave Airlied40c8532006-08-19 17:40:50 +100082 dev_priv->vram_initialized = 1;
Thomas Hellstromce65a442006-08-07 22:03:22 +100083 dev_priv->vram_offset = fb.offset;
84
85 mutex_unlock(&dev->struct_mutex);
86 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
87
88 return 0;
89
Dave Airlie22f579c2005-06-28 22:48:56 +100090}
91
92int via_final_context(struct drm_device *dev, int context)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093{
Dave Airlie22f579c2005-06-28 22:48:56 +100094 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
95
Dave Airlieb5e89ed2005-09-25 14:28:13 +100096 via_release_futex(dev_priv, context);
97
Dave Airlie22f579c2005-06-28 22:48:56 +100098 /* Linux specific until context tracking code gets ported to BSD */
99 /* Last context, perform cleanup */
100 if (dev->ctx_count == 1 && dev->dev_private) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000101 DRM_DEBUG("Last Context\n");
Dave Airlie22f579c2005-06-28 22:48:56 +1000102 if (dev->irq)
103 drm_irq_uninstall(dev);
Dave Airlie22f579c2005-06-28 22:48:56 +1000104 via_cleanup_futex(dev_priv);
105 via_do_cleanup_map(dev);
106 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000107 return 1;
108}
109
Thomas Hellstromce65a442006-08-07 22:03:22 +1000110void via_lastclose(struct drm_device *dev)
111{
112 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
113
114 if (!dev_priv)
115 return;
116
117 mutex_lock(&dev->struct_mutex);
118 drm_sman_cleanup(&dev_priv->sman);
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000119 dev_priv->vram_initialized = 0;
120 dev_priv->agp_initialized = 0;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000121 mutex_unlock(&dev->struct_mutex);
122}
123
Dave Airlie22f579c2005-06-28 22:48:56 +1000124int via_mem_alloc(DRM_IOCTL_ARGS)
125{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000126 DRM_DEVICE;
127
Dave Airlie22f579c2005-06-28 22:48:56 +1000128 drm_via_mem_t mem;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000129 int retval = 0;
Dave Airlie9698b4d2007-07-12 10:21:05 +1000130 struct drm_memblock_item *item;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000131 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
132 unsigned long tmpSize;
Dave Airlie22f579c2005-06-28 22:48:56 +1000133
Alexey Dobriyanbbaf3642005-07-27 11:43:56 -0700134 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t __user *) data,
135 sizeof(mem));
Dave Airlie22f579c2005-06-28 22:48:56 +1000136
Thomas Hellstromce65a442006-08-07 22:03:22 +1000137 if (mem.type > VIA_MEM_AGP) {
138 DRM_ERROR("Unknown memory type allocation\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000139 return -EINVAL;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000140 }
141 mutex_lock(&dev->struct_mutex);
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000142 if (0 == ((mem.type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
Thomas Hellstromce65a442006-08-07 22:03:22 +1000143 dev_priv->agp_initialized)) {
144 DRM_ERROR
145 ("Attempt to allocate from uninitialized memory manager.\n");
146 mutex_unlock(&dev->struct_mutex);
Eric Anholt20caafa2007-08-25 19:22:43 +1000147 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000148 }
149
Thomas Hellstromce65a442006-08-07 22:03:22 +1000150 tmpSize = (mem.size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
151 item = drm_sman_alloc(&dev_priv->sman, mem.type, tmpSize, 0,
152 (unsigned long)priv);
153 mutex_unlock(&dev->struct_mutex);
154 if (item) {
155 mem.offset = ((mem.type == VIA_MEM_VIDEO) ?
156 dev_priv->vram_offset : dev_priv->agp_offset) +
157 (item->mm->
158 offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT);
159 mem.index = item->user_hash.key;
Dave Airlie22f579c2005-06-28 22:48:56 +1000160 } else {
Thomas Hellstromce65a442006-08-07 22:03:22 +1000161 mem.offset = 0;
162 mem.size = 0;
163 mem.index = 0;
164 DRM_DEBUG("Video memory allocation failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000165 retval = -ENOMEM;
Dave Airlie22f579c2005-06-28 22:48:56 +1000166 }
Thomas Hellstromce65a442006-08-07 22:03:22 +1000167 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t __user *) data, mem, sizeof(mem));
Dave Airlie22f579c2005-06-28 22:48:56 +1000168
Dave Airlie22f579c2005-06-28 22:48:56 +1000169 return retval;
170}
171
172int via_mem_free(DRM_IOCTL_ARGS)
173{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000174 DRM_DEVICE;
175 drm_via_private_t *dev_priv = dev->dev_private;
Dave Airlie22f579c2005-06-28 22:48:56 +1000176 drm_via_mem_t mem;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000177 int ret;
Dave Airlie22f579c2005-06-28 22:48:56 +1000178
Alexey Dobriyanbbaf3642005-07-27 11:43:56 -0700179 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t __user *) data,
180 sizeof(mem));
Dave Airlie22f579c2005-06-28 22:48:56 +1000181
Thomas Hellstromce65a442006-08-07 22:03:22 +1000182 mutex_lock(&dev->struct_mutex);
183 ret = drm_sman_free_key(&dev_priv->sman, mem.index);
184 mutex_unlock(&dev->struct_mutex);
185 DRM_DEBUG("free = 0x%lx\n", mem.index);
Dave Airlie22f579c2005-06-28 22:48:56 +1000186
Thomas Hellstromce65a442006-08-07 22:03:22 +1000187 return ret;
Dave Airlie22f579c2005-06-28 22:48:56 +1000188}
189
Thomas Hellstromce65a442006-08-07 22:03:22 +1000190
Dave Airlie84b1fd12007-07-11 15:53:27 +1000191void via_reclaim_buffers_locked(struct drm_device * dev, struct file *filp)
Dave Airlie22f579c2005-06-28 22:48:56 +1000192{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000193 drm_via_private_t *dev_priv = dev->dev_private;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000194 struct drm_file *priv = filp->private_data;
Dave Airlie22f579c2005-06-28 22:48:56 +1000195
Thomas Hellstromce65a442006-08-07 22:03:22 +1000196 mutex_lock(&dev->struct_mutex);
197 if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)priv)) {
198 mutex_unlock(&dev->struct_mutex);
199 return;
Dave Airlie22f579c2005-06-28 22:48:56 +1000200 }
201
Thomas Hellstromce65a442006-08-07 22:03:22 +1000202 if (dev->driver->dma_quiescent) {
203 dev->driver->dma_quiescent(dev);
Dave Airlie22f579c2005-06-28 22:48:56 +1000204 }
205
Thomas Hellstromce65a442006-08-07 22:03:22 +1000206 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)priv);
207 mutex_unlock(&dev->struct_mutex);
208 return;
Dave Airlie22f579c2005-06-28 22:48:56 +1000209}