blob: d26f5dbb78538ea4532c7f79965fdd91aefd3fd1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Thomas Hellstromce65a442006-08-07 22:03:22 +100044#if defined(CONFIG_FB_SIS)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100045/* fb management via fb device */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Thomas Hellstromce65a442006-08-07 22:03:22 +100047#define SIS_MM_ALIGN_SHIFT 0
48#define SIS_MM_ALIGN_MASK 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Thomas Hellstromce65a442006-08-07 22:03:22 +100050static void *sis_sman_mm_allocate(void *private, unsigned long size,
51 unsigned alignment)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct sis_memreq req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Thomas Hellstromce65a442006-08-07 22:03:22 +100055 req.size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 sis_malloc(&req);
Thomas Hellstromce65a442006-08-07 22:03:22 +100057 if (req.size == 0)
58 return NULL;
59 else
60 return (void *)~req.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Thomas Hellstromce65a442006-08-07 22:03:22 +100063static void sis_sman_mm_free(void *private, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Thomas Hellstromce65a442006-08-07 22:03:22 +100065 sis_free(~((unsigned long)ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Thomas Hellstromce65a442006-08-07 22:03:22 +100068static void sis_sman_mm_destroy(void *private)
69{
70 ;
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Adrian Bunkfb41e542006-08-16 11:55:18 +100073static unsigned long sis_sman_mm_offset(void *private, void *ref)
Thomas Hellstromce65a442006-08-07 22:03:22 +100074{
75 return ~((unsigned long)ref);
76}
77
Adrian Bunkfb41e542006-08-16 11:55:18 +100078#else /* CONFIG_FB_SIS */
79
80#define SIS_MM_ALIGN_SHIFT 4
81#define SIS_MM_ALIGN_MASK ( (1 << SIS_MM_ALIGN_SHIFT) - 1)
82
83#endif /* CONFIG_FB_SIS */
Thomas Hellstromce65a442006-08-07 22:03:22 +100084
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085static int sis_fb_init(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 DRM_DEVICE;
88 drm_sis_private_t *dev_priv = dev->dev_private;
89 drm_sis_fb_t fb;
Thomas Hellstromce65a442006-08-07 22:03:22 +100090 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *) data, sizeof(fb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Thomas Hellstromce65a442006-08-07 22:03:22 +100094 mutex_lock(&dev->struct_mutex);
95#if defined(CONFIG_FB_SIS)
96 {
97 drm_sman_mm_t sman_mm;
98 sman_mm.private = (void *)0xFFFFFFFF;
99 sman_mm.allocate = sis_sman_mm_allocate;
100 sman_mm.free = sis_sman_mm_free;
101 sman_mm.destroy = sis_sman_mm_destroy;
102 sman_mm.offset = sis_sman_mm_offset;
103 ret =
104 drm_sman_set_manager(&dev_priv->sman, VIDEO_TYPE, &sman_mm);
105 }
106#else
107 ret = drm_sman_set_range(&dev_priv->sman, VIDEO_TYPE, 0,
108 fb.size >> SIS_MM_ALIGN_SHIFT);
109#endif
110
111 if (ret) {
112 DRM_ERROR("VRAM memory manager initialisation error\n");
113 mutex_unlock(&dev->struct_mutex);
114 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000117 dev_priv->vram_initialized = 1;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000118 dev_priv->vram_offset = fb.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Thomas Hellstromce65a442006-08-07 22:03:22 +1000120 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
122
123 return 0;
124}
125
Thomas Hellstromce65a442006-08-07 22:03:22 +1000126static int sis_drm_alloc(drm_device_t * dev, drm_file_t * priv,
127 unsigned long data, int pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 drm_sis_private_t *dev_priv = dev->dev_private;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000130 drm_sis_mem_t __user *argp = (drm_sis_mem_t __user *) data;
131 drm_sis_mem_t mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int retval = 0;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000133 drm_memblock_item_t *item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Thomas Hellstromce65a442006-08-07 22:03:22 +1000135 DRM_COPY_FROM_USER_IOCTL(mem, argp, sizeof(mem));
136
137 mutex_lock(&dev->struct_mutex);
138
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000139 if (0 == ((pool == 0) ? dev_priv->vram_initialized :
Thomas Hellstromce65a442006-08-07 22:03:22 +1000140 dev_priv->agp_initialized)) {
141 DRM_ERROR
142 ("Attempt to allocate from uninitialized memory manager.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return DRM_ERR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
Thomas Hellstromce65a442006-08-07 22:03:22 +1000146 mem.size = (mem.size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
147 item = drm_sman_alloc(&dev_priv->sman, pool, mem.size, 0,
148 (unsigned long)priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Thomas Hellstromce65a442006-08-07 22:03:22 +1000150 mutex_unlock(&dev->struct_mutex);
151 if (item) {
152 mem.offset = ((pool == 0) ?
153 dev_priv->vram_offset : dev_priv->agp_offset) +
154 (item->mm->
155 offset(item->mm, item->mm_info) << SIS_MM_ALIGN_SHIFT);
156 mem.free = item->user_hash.key;
157 mem.size = mem.size << SIS_MM_ALIGN_SHIFT;
158 } else {
159 mem.offset = 0;
160 mem.size = 0;
161 mem.free = 0;
162 retval = DRM_ERR(ENOMEM);
163 }
164
165 DRM_COPY_TO_USER_IOCTL(argp, mem, sizeof(mem));
166
167 DRM_DEBUG("alloc %d, size = %d, offset = %d\n", pool, mem.size,
168 mem.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 return retval;
171}
172
Thomas Hellstromce65a442006-08-07 22:03:22 +1000173static int sis_drm_free(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 DRM_DEVICE;
176 drm_sis_private_t *dev_priv = dev->dev_private;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000177 drm_sis_mem_t mem;
178 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Thomas Hellstromce65a442006-08-07 22:03:22 +1000180 DRM_COPY_FROM_USER_IOCTL(mem, (drm_sis_mem_t __user *) data,
181 sizeof(mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Thomas Hellstromce65a442006-08-07 22:03:22 +1000183 mutex_lock(&dev->struct_mutex);
184 ret = drm_sman_free_key(&dev_priv->sman, mem.free);
185 mutex_unlock(&dev->struct_mutex);
186 DRM_DEBUG("free = 0x%lx\n", mem.free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Thomas Hellstromce65a442006-08-07 22:03:22 +1000188 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Thomas Hellstromce65a442006-08-07 22:03:22 +1000191static int sis_fb_alloc(DRM_IOCTL_ARGS)
192{
193 DRM_DEVICE;
194 return sis_drm_alloc(dev, priv, data, VIDEO_TYPE);
195}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197static int sis_ioctl_agp_init(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 DRM_DEVICE;
200 drm_sis_private_t *dev_priv = dev->dev_private;
201 drm_sis_agp_t agp;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000202 int ret;
203 dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000205 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *) data,
206 sizeof(agp));
Thomas Hellstromce65a442006-08-07 22:03:22 +1000207 mutex_lock(&dev->struct_mutex);
208 ret = drm_sman_set_range(&dev_priv->sman, AGP_TYPE, 0,
209 agp.size >> SIS_MM_ALIGN_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Thomas Hellstromce65a442006-08-07 22:03:22 +1000211 if (ret) {
212 DRM_ERROR("AGP memory manager initialisation error\n");
213 mutex_unlock(&dev->struct_mutex);
214 return ret;
215 }
216
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000217 dev_priv->agp_initialized = 1;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000218 dev_priv->agp_offset = agp.offset;
219 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
223}
224
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000225static int sis_ioctl_agp_alloc(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 DRM_DEVICE;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000228
229 return sis_drm_alloc(dev, priv, data, AGP_TYPE);
230}
231
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000232static drm_local_map_t *sis_reg_init(drm_device_t *dev)
233{
234 drm_map_list_t *entry;
235 drm_local_map_t *map;
236
237 list_for_each_entry(entry, &dev->maplist->head, head) {
238 map = entry->map;
239 if (!map)
240 continue;
241 if (map->type == _DRM_REGISTERS) {
242 return map;
243 }
244 }
245 return NULL;
246}
247
248int sis_idle(drm_device_t *dev)
249{
250 drm_sis_private_t *dev_priv = dev->dev_private;
251 uint32_t idle_reg;
252 unsigned long end;
253 int i;
254
255 if (dev_priv->idle_fault)
256 return 0;
257
258 if (dev_priv->mmio == NULL) {
259 dev_priv->mmio = sis_reg_init(dev);
260 if (dev_priv->mmio == NULL) {
261 DRM_ERROR("Could not find register map.\n");
262 return 0;
263 }
264 }
265
266 /*
267 * Implement a device switch here if needed
268 */
269
270 if (dev_priv->chipset != SIS_CHIP_315)
271 return 0;
272
273 /*
274 * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here
275 * because its polling frequency is too low.
276 */
277
278 end = jiffies + (DRM_HZ * 3);
279
280 for (i=0; i<4; ++i) {
281 do {
282 idle_reg = SIS_READ(0x85cc);
283 } while ( !time_after_eq(jiffies, end) &&
284 ((idle_reg & 0x80000000) != 0x80000000));
285 }
286
287 if (time_after_eq(jiffies, end)) {
288 DRM_ERROR("Graphics engine idle timeout. "
289 "Disabling idle check\n");
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000290 dev_priv->idle_fault = 1;
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000291 }
292
293 /*
294 * The caller never sees an error code. It gets trapped
295 * in libdrm.
296 */
297
298 return 0;
299}
300
301
Thomas Hellstromce65a442006-08-07 22:03:22 +1000302void sis_lastclose(struct drm_device *dev)
303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 drm_sis_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305
Thomas Hellstromce65a442006-08-07 22:03:22 +1000306 if (!dev_priv)
307 return;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000308
Thomas Hellstromce65a442006-08-07 22:03:22 +1000309 mutex_lock(&dev->struct_mutex);
310 drm_sman_cleanup(&dev_priv->sman);
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000311 dev_priv->vram_initialized = 0;
312 dev_priv->agp_initialized = 0;
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000313 dev_priv->mmio = NULL;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000314 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Thomas Hellstromce65a442006-08-07 22:03:22 +1000317void sis_reclaim_buffers_locked(drm_device_t * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 drm_sis_private_t *dev_priv = dev->dev_private;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000320 drm_file_t *priv = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Thomas Hellstromce65a442006-08-07 22:03:22 +1000322 mutex_lock(&dev->struct_mutex);
323 if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)priv)) {
324 mutex_unlock(&dev->struct_mutex);
325 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
Thomas Hellstromce65a442006-08-07 22:03:22 +1000328 if (dev->driver->dma_quiescent) {
329 dev->driver->dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331
Thomas Hellstromce65a442006-08-07 22:03:22 +1000332 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)priv);
333 mutex_unlock(&dev->struct_mutex);
334 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337drm_ioctl_desc_t sis_ioctls[] = {
Dave Airliea7a2cc32006-01-02 13:54:04 +1100338 [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = {sis_fb_alloc, DRM_AUTH},
Thomas Hellstromce65a442006-08-07 22:03:22 +1000339 [DRM_IOCTL_NR(DRM_SIS_FB_FREE)] = {sis_drm_free, DRM_AUTH},
340 [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)] =
341 {sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY},
Dave Airliea7a2cc32006-01-02 13:54:04 +1100342 [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = {sis_ioctl_agp_alloc, DRM_AUTH},
Thomas Hellstromce65a442006-08-07 22:03:22 +1000343 [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)] = {sis_drm_free, DRM_AUTH},
344 [DRM_IOCTL_NR(DRM_SIS_FB_INIT)] =
345 {sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
348int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);