1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
31 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
38 #include <video/sisfb.h>
43 #define SIS_MM_ALIGN_SHIFT 4
44 #define SIS_MM_ALIGN_MASK ( (1 << SIS_MM_ALIGN_SHIFT) - 1)
46 #if defined(CONFIG_FB_SIS)
47 /* fb management via fb device */
49 #define SIS_MM_ALIGN_SHIFT 0
50 #define SIS_MM_ALIGN_MASK 0
52 static void *sis_sman_mm_allocate(void *private, unsigned long size,
55 struct sis_memreq req;
62 return (void *)~req.offset;
65 static void sis_sman_mm_free(void *private, void *ref)
67 sis_free(~((unsigned long)ref));
70 static void sis_sman_mm_destroy(void *private)
75 unsigned long sis_sman_mm_offset(void *private, void *ref)
77 return ~((unsigned long)ref);
82 static int sis_fb_init(DRM_IOCTL_ARGS)
85 drm_sis_private_t *dev_priv = dev->dev_private;
89 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *) data, sizeof(fb));
91 mutex_lock(&dev->struct_mutex);
92 #if defined(CONFIG_FB_SIS)
94 drm_sman_mm_t sman_mm;
95 sman_mm.private = (void *)0xFFFFFFFF;
96 sman_mm.allocate = sis_sman_mm_allocate;
97 sman_mm.free = sis_sman_mm_free;
98 sman_mm.destroy = sis_sman_mm_destroy;
99 sman_mm.offset = sis_sman_mm_offset;
101 drm_sman_set_manager(&dev_priv->sman, VIDEO_TYPE, &sman_mm);
104 ret = drm_sman_set_range(&dev_priv->sman, VIDEO_TYPE, 0,
105 fb.size >> SIS_MM_ALIGN_SHIFT);
109 DRM_ERROR("VRAM memory manager initialisation error\n");
110 mutex_unlock(&dev->struct_mutex);
114 dev_priv->vram_initialized = TRUE;
115 dev_priv->vram_offset = fb.offset;
117 mutex_unlock(&dev->struct_mutex);
118 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
123 static int sis_drm_alloc(drm_device_t * dev, drm_file_t * priv,
124 unsigned long data, int pool)
126 drm_sis_private_t *dev_priv = dev->dev_private;
127 drm_sis_mem_t __user *argp = (drm_sis_mem_t __user *) data;
130 drm_memblock_item_t *item;
132 DRM_COPY_FROM_USER_IOCTL(mem, argp, sizeof(mem));
134 mutex_lock(&dev->struct_mutex);
136 if (FALSE == ((pool == 0) ? dev_priv->vram_initialized :
137 dev_priv->agp_initialized)) {
139 ("Attempt to allocate from uninitialized memory manager.\n");
140 return DRM_ERR(EINVAL);
143 mem.size = (mem.size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
144 item = drm_sman_alloc(&dev_priv->sman, pool, mem.size, 0,
145 (unsigned long)priv);
147 mutex_unlock(&dev->struct_mutex);
149 mem.offset = ((pool == 0) ?
150 dev_priv->vram_offset : dev_priv->agp_offset) +
152 offset(item->mm, item->mm_info) << SIS_MM_ALIGN_SHIFT);
153 mem.free = item->user_hash.key;
154 mem.size = mem.size << SIS_MM_ALIGN_SHIFT;
159 retval = DRM_ERR(ENOMEM);
162 DRM_COPY_TO_USER_IOCTL(argp, mem, sizeof(mem));
164 DRM_DEBUG("alloc %d, size = %d, offset = %d\n", pool, mem.size,
170 static int sis_drm_free(DRM_IOCTL_ARGS)
173 drm_sis_private_t *dev_priv = dev->dev_private;
177 DRM_COPY_FROM_USER_IOCTL(mem, (drm_sis_mem_t __user *) data,
180 mutex_lock(&dev->struct_mutex);
181 ret = drm_sman_free_key(&dev_priv->sman, mem.free);
182 mutex_unlock(&dev->struct_mutex);
183 DRM_DEBUG("free = 0x%lx\n", mem.free);
188 static int sis_fb_alloc(DRM_IOCTL_ARGS)
191 return sis_drm_alloc(dev, priv, data, VIDEO_TYPE);
194 static int sis_ioctl_agp_init(DRM_IOCTL_ARGS)
197 drm_sis_private_t *dev_priv = dev->dev_private;
200 dev_priv = dev->dev_private;
202 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *) data,
204 mutex_lock(&dev->struct_mutex);
205 ret = drm_sman_set_range(&dev_priv->sman, AGP_TYPE, 0,
206 agp.size >> SIS_MM_ALIGN_SHIFT);
209 DRM_ERROR("AGP memory manager initialisation error\n");
210 mutex_unlock(&dev->struct_mutex);
214 dev_priv->agp_initialized = TRUE;
215 dev_priv->agp_offset = agp.offset;
216 mutex_unlock(&dev->struct_mutex);
218 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
222 static int sis_ioctl_agp_alloc(DRM_IOCTL_ARGS)
226 return sis_drm_alloc(dev, priv, data, AGP_TYPE);
229 void sis_lastclose(struct drm_device *dev)
231 drm_sis_private_t *dev_priv = dev->dev_private;
236 mutex_lock(&dev->struct_mutex);
237 drm_sman_cleanup(&dev_priv->sman);
238 dev_priv->vram_initialized = FALSE;
239 dev_priv->agp_initialized = FALSE;
240 mutex_unlock(&dev->struct_mutex);
243 void sis_reclaim_buffers_locked(drm_device_t * dev, struct file *filp)
245 drm_sis_private_t *dev_priv = dev->dev_private;
246 drm_file_t *priv = filp->private_data;
248 mutex_lock(&dev->struct_mutex);
249 if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)priv)) {
250 mutex_unlock(&dev->struct_mutex);
254 if (dev->driver->dma_quiescent) {
255 dev->driver->dma_quiescent(dev);
258 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)priv);
259 mutex_unlock(&dev->struct_mutex);
263 drm_ioctl_desc_t sis_ioctls[] = {
264 [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = {sis_fb_alloc, DRM_AUTH},
265 [DRM_IOCTL_NR(DRM_SIS_FB_FREE)] = {sis_drm_free, DRM_AUTH},
266 [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)] =
267 {sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY},
268 [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = {sis_ioctl_agp_alloc, DRM_AUTH},
269 [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)] = {sis_drm_free, DRM_AUTH},
270 [DRM_IOCTL_NR(DRM_SIS_FB_INIT)] =
271 {sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY}
274 int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);