Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / char / drm / via_mm.c
blob2fcf0577a7aa9e5b4b52d37cf7e897b3238e2315
1 /*
2 * Copyright 2006 Tungsten Graphics Inc., Bismarck, ND., USA.
3 * All rights reserved.
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:
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.
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
19 * 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,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
28 #include "drmP.h"
29 #include "via_drm.h"
30 #include "via_drv.h"
31 #include "drm_sman.h"
33 #define VIA_MM_ALIGN_SHIFT 4
34 #define VIA_MM_ALIGN_MASK ( (1 << VIA_MM_ALIGN_SHIFT) - 1)
36 int via_agp_init(DRM_IOCTL_ARGS)
38 DRM_DEVICE;
39 drm_via_agp_t agp;
40 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
41 int ret;
43 DRM_COPY_FROM_USER_IOCTL(agp, (drm_via_agp_t __user *) data,
44 sizeof(agp));
45 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);
49 if (ret) {
50 DRM_ERROR("AGP memory manager initialisation error\n");
51 mutex_unlock(&dev->struct_mutex);
52 return ret;
55 dev_priv->agp_initialized = 1;
56 dev_priv->agp_offset = agp.offset;
57 mutex_unlock(&dev->struct_mutex);
59 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
60 return 0;
63 int via_fb_init(DRM_IOCTL_ARGS)
65 DRM_DEVICE;
66 drm_via_fb_t fb;
67 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
68 int ret;
70 DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t __user *) data, sizeof(fb));
72 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);
76 if (ret) {
77 DRM_ERROR("VRAM memory manager initialisation error\n");
78 mutex_unlock(&dev->struct_mutex);
79 return ret;
82 dev_priv->vram_initialized = 1;
83 dev_priv->vram_offset = fb.offset;
85 mutex_unlock(&dev->struct_mutex);
86 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
88 return 0;
92 int via_final_context(struct drm_device *dev, int context)
94 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
96 via_release_futex(dev_priv, context);
98 /* Linux specific until context tracking code gets ported to BSD */
99 /* Last context, perform cleanup */
100 if (dev->ctx_count == 1 && dev->dev_private) {
101 DRM_DEBUG("Last Context\n");
102 if (dev->irq)
103 drm_irq_uninstall(dev);
104 via_cleanup_futex(dev_priv);
105 via_do_cleanup_map(dev);
107 return 1;
110 void via_lastclose(struct drm_device *dev)
112 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
114 if (!dev_priv)
115 return;
117 mutex_lock(&dev->struct_mutex);
118 drm_sman_cleanup(&dev_priv->sman);
119 dev_priv->vram_initialized = 0;
120 dev_priv->agp_initialized = 0;
121 mutex_unlock(&dev->struct_mutex);
124 int via_mem_alloc(DRM_IOCTL_ARGS)
126 DRM_DEVICE;
128 drm_via_mem_t mem;
129 int retval = 0;
130 drm_memblock_item_t *item;
131 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
132 unsigned long tmpSize;
134 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t __user *) data,
135 sizeof(mem));
137 if (mem.type > VIA_MEM_AGP) {
138 DRM_ERROR("Unknown memory type allocation\n");
139 return DRM_ERR(EINVAL);
141 mutex_lock(&dev->struct_mutex);
142 if (0 == ((mem.type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
143 dev_priv->agp_initialized)) {
144 DRM_ERROR
145 ("Attempt to allocate from uninitialized memory manager.\n");
146 mutex_unlock(&dev->struct_mutex);
147 return DRM_ERR(EINVAL);
150 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;
160 } else {
161 mem.offset = 0;
162 mem.size = 0;
163 mem.index = 0;
164 DRM_DEBUG("Video memory allocation failed\n");
165 retval = DRM_ERR(ENOMEM);
167 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t __user *) data, mem, sizeof(mem));
169 return retval;
172 int via_mem_free(DRM_IOCTL_ARGS)
174 DRM_DEVICE;
175 drm_via_private_t *dev_priv = dev->dev_private;
176 drm_via_mem_t mem;
177 int ret;
179 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t __user *) data,
180 sizeof(mem));
182 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);
187 return ret;
191 void via_reclaim_buffers_locked(drm_device_t * dev, struct file *filp)
193 drm_via_private_t *dev_priv = dev->dev_private;
194 drm_file_t *priv = filp->private_data;
196 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;
202 if (dev->driver->dma_quiescent) {
203 dev->driver->dma_quiescent(dev);
206 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)priv);
207 mutex_unlock(&dev->struct_mutex);
208 return;