blob: 6be1c575758063ff0721bf723544ff3d3d418a63 [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:
Jan Engelhardt96de0e22007-10-19 23:21:04 +020031 * 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
Eric Anholtc153f452007-09-03 12:06:45 +100085static int sis_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 drm_sis_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +100088 drm_sis_fb_t *fb = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +100089 int ret;
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 {
Dave Airliecca53072007-07-17 12:55:58 +100094 struct drm_sman_mm sman_mm;
Thomas Hellstromce65a442006-08-07 22:03:22 +100095 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,
Eric Anholtc153f452007-09-03 12:06:45 +1000105 fb->size >> SIS_MM_ALIGN_SHIFT);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000106#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
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000114 dev_priv->vram_initialized = 1;
Eric Anholtc153f452007-09-03 12:06:45 +1000115 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);
Eric Anholtc153f452007-09-03 12:06:45 +1000118 DRM_DEBUG("offset = %u, size = %u", fb->offset, fb->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 return 0;
121}
122
Eric Anholt6c340ea2007-08-25 20:23:09 +1000123static int sis_drm_alloc(struct drm_device *dev, struct drm_file *file_priv,
Eric Anholtc153f452007-09-03 12:06:45 +1000124 void *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;
Eric Anholtc153f452007-09-03 12:06:45 +1000127 drm_sis_mem_t *mem = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int retval = 0;
Dave Airlie9698b4d2007-07-12 10:21:05 +1000129 struct drm_memblock_item *item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Thomas Hellstromce65a442006-08-07 22:03:22 +1000131 mutex_lock(&dev->struct_mutex);
132
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000133 if (0 == ((pool == 0) ? dev_priv->vram_initialized :
Thomas Hellstromce65a442006-08-07 22:03:22 +1000134 dev_priv->agp_initialized)) {
135 DRM_ERROR
136 ("Attempt to allocate from uninitialized memory manager.\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000137 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
Eric Anholtc153f452007-09-03 12:06:45 +1000140 mem->size = (mem->size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
141 item = drm_sman_alloc(&dev_priv->sman, pool, mem->size, 0,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000142 (unsigned long)file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Thomas Hellstromce65a442006-08-07 22:03:22 +1000144 mutex_unlock(&dev->struct_mutex);
145 if (item) {
Eric Anholtc153f452007-09-03 12:06:45 +1000146 mem->offset = ((pool == 0) ?
Thomas Hellstromce65a442006-08-07 22:03:22 +1000147 dev_priv->vram_offset : dev_priv->agp_offset) +
148 (item->mm->
149 offset(item->mm, item->mm_info) << SIS_MM_ALIGN_SHIFT);
Eric Anholtc153f452007-09-03 12:06:45 +1000150 mem->free = item->user_hash.key;
151 mem->size = mem->size << SIS_MM_ALIGN_SHIFT;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000152 } else {
Eric Anholtc153f452007-09-03 12:06:45 +1000153 mem->offset = 0;
154 mem->size = 0;
155 mem->free = 0;
Eric Anholt20caafa2007-08-25 19:22:43 +1000156 retval = -ENOMEM;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000157 }
158
Eric Anholtc153f452007-09-03 12:06:45 +1000159 DRM_DEBUG("alloc %d, size = %d, offset = %d\n", pool, mem->size,
160 mem->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 return retval;
163}
164
Eric Anholtc153f452007-09-03 12:06:45 +1000165static int sis_drm_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 drm_sis_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000168 drm_sis_mem_t *mem = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000169 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Thomas Hellstromce65a442006-08-07 22:03:22 +1000171 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000172 ret = drm_sman_free_key(&dev_priv->sman, mem->free);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000173 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000174 DRM_DEBUG("free = 0x%lx\n", mem->free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Thomas Hellstromce65a442006-08-07 22:03:22 +1000176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Eric Anholtc153f452007-09-03 12:06:45 +1000179static int sis_fb_alloc(struct drm_device *dev, void *data,
180 struct drm_file *file_priv)
Thomas Hellstromce65a442006-08-07 22:03:22 +1000181{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000182 return sis_drm_alloc(dev, file_priv, data, VIDEO_TYPE);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Eric Anholtc153f452007-09-03 12:06:45 +1000185static int sis_ioctl_agp_init(struct drm_device *dev, void *data,
186 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 drm_sis_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000189 drm_sis_agp_t *agp = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000190 int ret;
191 dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Thomas Hellstromce65a442006-08-07 22:03:22 +1000193 mutex_lock(&dev->struct_mutex);
194 ret = drm_sman_set_range(&dev_priv->sman, AGP_TYPE, 0,
Eric Anholtc153f452007-09-03 12:06:45 +1000195 agp->size >> SIS_MM_ALIGN_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Thomas Hellstromce65a442006-08-07 22:03:22 +1000197 if (ret) {
198 DRM_ERROR("AGP memory manager initialisation error\n");
199 mutex_unlock(&dev->struct_mutex);
200 return ret;
201 }
202
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000203 dev_priv->agp_initialized = 1;
Eric Anholtc153f452007-09-03 12:06:45 +1000204 dev_priv->agp_offset = agp->offset;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000205 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Eric Anholtc153f452007-09-03 12:06:45 +1000207 DRM_DEBUG("offset = %u, size = %u", agp->offset, agp->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209}
210
Eric Anholtc153f452007-09-03 12:06:45 +1000211static int sis_ioctl_agp_alloc(struct drm_device *dev, void *data,
212 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000214
Eric Anholt6c340ea2007-08-25 20:23:09 +1000215 return sis_drm_alloc(dev, file_priv, data, AGP_TYPE);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000216}
217
Dave Airlie84b1fd12007-07-11 15:53:27 +1000218static drm_local_map_t *sis_reg_init(struct drm_device *dev)
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000219{
Dave Airlie55910512007-07-11 16:53:40 +1000220 struct drm_map_list *entry;
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000221 drm_local_map_t *map;
222
Dave Airliebd1b3312007-05-26 05:01:51 +1000223 list_for_each_entry(entry, &dev->maplist, head) {
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000224 map = entry->map;
225 if (!map)
226 continue;
227 if (map->type == _DRM_REGISTERS) {
228 return map;
229 }
230 }
231 return NULL;
232}
233
Dave Airlie84b1fd12007-07-11 15:53:27 +1000234int sis_idle(struct drm_device *dev)
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000235{
236 drm_sis_private_t *dev_priv = dev->dev_private;
237 uint32_t idle_reg;
238 unsigned long end;
239 int i;
240
241 if (dev_priv->idle_fault)
242 return 0;
243
244 if (dev_priv->mmio == NULL) {
245 dev_priv->mmio = sis_reg_init(dev);
246 if (dev_priv->mmio == NULL) {
247 DRM_ERROR("Could not find register map.\n");
248 return 0;
249 }
250 }
251
252 /*
253 * Implement a device switch here if needed
254 */
255
256 if (dev_priv->chipset != SIS_CHIP_315)
257 return 0;
258
259 /*
260 * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here
261 * because its polling frequency is too low.
262 */
263
264 end = jiffies + (DRM_HZ * 3);
265
266 for (i=0; i<4; ++i) {
267 do {
268 idle_reg = SIS_READ(0x85cc);
269 } while ( !time_after_eq(jiffies, end) &&
270 ((idle_reg & 0x80000000) != 0x80000000));
271 }
272
273 if (time_after_eq(jiffies, end)) {
274 DRM_ERROR("Graphics engine idle timeout. "
275 "Disabling idle check\n");
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000276 dev_priv->idle_fault = 1;
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000277 }
278
279 /*
280 * The caller never sees an error code. It gets trapped
281 * in libdrm.
282 */
283
284 return 0;
285}
286
287
Thomas Hellstromce65a442006-08-07 22:03:22 +1000288void sis_lastclose(struct drm_device *dev)
289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 drm_sis_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291
Thomas Hellstromce65a442006-08-07 22:03:22 +1000292 if (!dev_priv)
293 return;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294
Thomas Hellstromce65a442006-08-07 22:03:22 +1000295 mutex_lock(&dev->struct_mutex);
296 drm_sman_cleanup(&dev_priv->sman);
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000297 dev_priv->vram_initialized = 0;
298 dev_priv->agp_initialized = 0;
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000299 dev_priv->mmio = NULL;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000300 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Eric Anholt6c340ea2007-08-25 20:23:09 +1000303void sis_reclaim_buffers_locked(struct drm_device * dev,
304 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 drm_sis_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Thomas Hellstromce65a442006-08-07 22:03:22 +1000308 mutex_lock(&dev->struct_mutex);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000309 if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)file_priv)) {
Thomas Hellstromce65a442006-08-07 22:03:22 +1000310 mutex_unlock(&dev->struct_mutex);
311 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
Thomas Hellstromce65a442006-08-07 22:03:22 +1000314 if (dev->driver->dma_quiescent) {
315 dev->driver->dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000317
Eric Anholt6c340ea2007-08-25 20:23:09 +1000318 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)file_priv);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000319 mutex_unlock(&dev->struct_mutex);
320 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Eric Anholtc153f452007-09-03 12:06:45 +1000323struct drm_ioctl_desc sis_ioctls[] = {
324 DRM_IOCTL_DEF(DRM_SIS_FB_ALLOC, sis_fb_alloc, DRM_AUTH),
325 DRM_IOCTL_DEF(DRM_SIS_FB_FREE, sis_drm_free, DRM_AUTH),
326 DRM_IOCTL_DEF(DRM_SIS_AGP_INIT, sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
327 DRM_IOCTL_DEF(DRM_SIS_AGP_ALLOC, sis_ioctl_agp_alloc, DRM_AUTH),
328 DRM_IOCTL_DEF(DRM_SIS_AGP_FREE, sis_drm_free, DRM_AUTH),
329 DRM_IOCTL_DEF(DRM_SIS_FB_INIT, sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330};
331
332int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);