blob: 0eb1dca232b72565b66318fc02b4a0c3643d952f [file] [log] [blame]
Thomas Hellstromce65a442006-08-07 22:03:22 +10001/**************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
Thomas Hellstromce65a442006-08-07 22:03:22 +10003 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
Thomas Hellstromce65a442006-08-07 22:03:22 +10007 * 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:
Dave Airlieb5e89ed2005-09-25 14:28:13 +100013 *
Thomas Hellstromce65a442006-08-07 22:03:22 +100014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Thomas Hellstromce65a442006-08-07 22:03:22 +100020 * 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.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100025 *
Thomas Hellstromce65a442006-08-07 22:03:22 +100026 *
27 **************************************************************************/
28
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Authors:
Thomas Hellstromce65a442006-08-07 22:03:22 +100031 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
34#include "drmP.h"
35#include "sis_drm.h"
36#include "sis_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Hellstromce65a442006-08-07 22:03:22 +100038#include <video/sisfb.h>
39
Dave Airlieb5e89ed2005-09-25 14:28:13 +100040#define VIDEO_TYPE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define AGP_TYPE 1
42
Thomas Hellstromce65a442006-08-07 22:03:22 +100043#define SIS_MM_ALIGN_SHIFT 4
44#define SIS_MM_ALIGN_MASK ( (1 << SIS_MM_ALIGN_SHIFT) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Thomas Hellstromce65a442006-08-07 22:03:22 +100046#if defined(CONFIG_FB_SIS)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100047/* fb management via fb device */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Thomas Hellstromce65a442006-08-07 22:03:22 +100049#define SIS_MM_ALIGN_SHIFT 0
50#define SIS_MM_ALIGN_MASK 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Thomas Hellstromce65a442006-08-07 22:03:22 +100052static void *sis_sman_mm_allocate(void *private, unsigned long size,
53 unsigned alignment)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct sis_memreq req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Thomas Hellstromce65a442006-08-07 22:03:22 +100057 req.size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 sis_malloc(&req);
Thomas Hellstromce65a442006-08-07 22:03:22 +100059 if (req.size == 0)
60 return NULL;
61 else
62 return (void *)~req.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Thomas Hellstromce65a442006-08-07 22:03:22 +100065static void sis_sman_mm_free(void *private, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Thomas Hellstromce65a442006-08-07 22:03:22 +100067 sis_free(~((unsigned long)ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Thomas Hellstromce65a442006-08-07 22:03:22 +100070static void sis_sman_mm_destroy(void *private)
71{
72 ;
73}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Thomas Hellstromce65a442006-08-07 22:03:22 +100075unsigned long sis_sman_mm_offset(void *private, void *ref)
76{
77 return ~((unsigned long)ref);
78}
79
80#endif
81
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082static int sis_fb_init(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 DRM_DEVICE;
85 drm_sis_private_t *dev_priv = dev->dev_private;
86 drm_sis_fb_t fb;
Thomas Hellstromce65a442006-08-07 22:03:22 +100087 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Dave Airlieb5e89ed2005-09-25 14:28:13 +100089 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *) data, sizeof(fb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Thomas Hellstromce65a442006-08-07 22:03:22 +100091 mutex_lock(&dev->struct_mutex);
92#if defined(CONFIG_FB_SIS)
93 {
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;
100 ret =
101 drm_sman_set_manager(&dev_priv->sman, VIDEO_TYPE, &sman_mm);
102 }
103#else
104 ret = drm_sman_set_range(&dev_priv->sman, VIDEO_TYPE, 0,
105 fb.size >> SIS_MM_ALIGN_SHIFT);
106#endif
107
108 if (ret) {
109 DRM_ERROR("VRAM memory manager initialisation error\n");
110 mutex_unlock(&dev->struct_mutex);
111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
Thomas Hellstromce65a442006-08-07 22:03:22 +1000114 dev_priv->vram_initialized = TRUE;
115 dev_priv->vram_offset = fb.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Thomas Hellstromce65a442006-08-07 22:03:22 +1000117 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
119
120 return 0;
121}
122
Thomas Hellstromce65a442006-08-07 22:03:22 +1000123static int sis_drm_alloc(drm_device_t * dev, drm_file_t * priv,
124 unsigned long data, int pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 drm_sis_private_t *dev_priv = dev->dev_private;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000127 drm_sis_mem_t __user *argp = (drm_sis_mem_t __user *) data;
128 drm_sis_mem_t mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int retval = 0;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000130 drm_memblock_item_t *item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Thomas Hellstromce65a442006-08-07 22:03:22 +1000132 DRM_COPY_FROM_USER_IOCTL(mem, argp, sizeof(mem));
133
134 mutex_lock(&dev->struct_mutex);
135
136 if (FALSE == ((pool == 0) ? dev_priv->vram_initialized :
137 dev_priv->agp_initialized)) {
138 DRM_ERROR
139 ("Attempt to allocate from uninitialized memory manager.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return DRM_ERR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
Thomas Hellstromce65a442006-08-07 22:03:22 +1000143 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Thomas Hellstromce65a442006-08-07 22:03:22 +1000147 mutex_unlock(&dev->struct_mutex);
148 if (item) {
149 mem.offset = ((pool == 0) ?
150 dev_priv->vram_offset : dev_priv->agp_offset) +
151 (item->mm->
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;
155 } else {
156 mem.offset = 0;
157 mem.size = 0;
158 mem.free = 0;
159 retval = DRM_ERR(ENOMEM);
160 }
161
162 DRM_COPY_TO_USER_IOCTL(argp, mem, sizeof(mem));
163
164 DRM_DEBUG("alloc %d, size = %d, offset = %d\n", pool, mem.size,
165 mem.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 return retval;
168}
169
Thomas Hellstromce65a442006-08-07 22:03:22 +1000170static int sis_drm_free(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 DRM_DEVICE;
173 drm_sis_private_t *dev_priv = dev->dev_private;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000174 drm_sis_mem_t mem;
175 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Thomas Hellstromce65a442006-08-07 22:03:22 +1000177 DRM_COPY_FROM_USER_IOCTL(mem, (drm_sis_mem_t __user *) data,
178 sizeof(mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Thomas Hellstromce65a442006-08-07 22:03:22 +1000180 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Thomas Hellstromce65a442006-08-07 22:03:22 +1000185 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Thomas Hellstromce65a442006-08-07 22:03:22 +1000188static int sis_fb_alloc(DRM_IOCTL_ARGS)
189{
190 DRM_DEVICE;
191 return sis_drm_alloc(dev, priv, data, VIDEO_TYPE);
192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194static int sis_ioctl_agp_init(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 DRM_DEVICE;
197 drm_sis_private_t *dev_priv = dev->dev_private;
198 drm_sis_agp_t agp;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000199 int ret;
200 dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000202 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *) data,
203 sizeof(agp));
Thomas Hellstromce65a442006-08-07 22:03:22 +1000204 mutex_lock(&dev->struct_mutex);
205 ret = drm_sman_set_range(&dev_priv->sman, AGP_TYPE, 0,
206 agp.size >> SIS_MM_ALIGN_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Thomas Hellstromce65a442006-08-07 22:03:22 +1000208 if (ret) {
209 DRM_ERROR("AGP memory manager initialisation error\n");
210 mutex_unlock(&dev->struct_mutex);
211 return ret;
212 }
213
214 dev_priv->agp_initialized = TRUE;
215 dev_priv->agp_offset = agp.offset;
216 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
220}
221
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222static int sis_ioctl_agp_alloc(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 DRM_DEVICE;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000225
226 return sis_drm_alloc(dev, priv, data, AGP_TYPE);
227}
228
229void sis_lastclose(struct drm_device *dev)
230{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 drm_sis_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232
Thomas Hellstromce65a442006-08-07 22:03:22 +1000233 if (!dev_priv)
234 return;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235
Thomas Hellstromce65a442006-08-07 22:03:22 +1000236 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Thomas Hellstromce65a442006-08-07 22:03:22 +1000243void sis_reclaim_buffers_locked(drm_device_t * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 drm_sis_private_t *dev_priv = dev->dev_private;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000246 drm_file_t *priv = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Thomas Hellstromce65a442006-08-07 22:03:22 +1000248 mutex_lock(&dev->struct_mutex);
249 if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)priv)) {
250 mutex_unlock(&dev->struct_mutex);
251 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Thomas Hellstromce65a442006-08-07 22:03:22 +1000254 if (dev->driver->dma_quiescent) {
255 dev->driver->dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000257
Thomas Hellstromce65a442006-08-07 22:03:22 +1000258 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)priv);
259 mutex_unlock(&dev->struct_mutex);
260 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263drm_ioctl_desc_t sis_ioctls[] = {
Dave Airliea7a2cc32006-01-02 13:54:04 +1100264 [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = {sis_fb_alloc, DRM_AUTH},
Thomas Hellstromce65a442006-08-07 22:03:22 +1000265 [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},
Dave Airliea7a2cc32006-01-02 13:54:04 +1100268 [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = {sis_ioctl_agp_alloc, DRM_AUTH},
Thomas Hellstromce65a442006-08-07 22:03:22 +1000269 [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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272};
273
274int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);