blob: af22111397d8cad6822cfa1b659c291ec885fa45 [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
David Howells6bb9e4b2008-07-30 12:29:37 -070044#if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
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
David Howells48f185d2008-07-30 12:29:38 -070060 return (void *)(unsigned long)~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
David Howells6bb9e4b2008-07-30 12:29:37 -070078#else /* CONFIG_FB_SIS[_MODULE] */
Adrian Bunkfb41e542006-08-16 11:55:18 +100079
80#define SIS_MM_ALIGN_SHIFT 4
81#define SIS_MM_ALIGN_MASK ( (1 << SIS_MM_ALIGN_SHIFT) - 1)
82
David Howells6bb9e4b2008-07-30 12:29:37 -070083#endif /* CONFIG_FB_SIS[_MODULE] */
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);
David Howells6bb9e4b2008-07-30 12:29:37 -070092#if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
Thomas Hellstromce65a442006-08-07 22:03:22 +100093 {
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);
Márton Németh3e684ea2008-01-24 15:58:57 +1000118 DRM_DEBUG("offset = %u, size = %u\n", 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");
Roel Kluin038477a2007-11-05 10:53:18 +1000137 mutex_unlock(&dev->struct_mutex);
Eric Anholt20caafa2007-08-25 19:22:43 +1000138 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140
Eric Anholtc153f452007-09-03 12:06:45 +1000141 mem->size = (mem->size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
142 item = drm_sman_alloc(&dev_priv->sman, pool, mem->size, 0,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000143 (unsigned long)file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Thomas Hellstromce65a442006-08-07 22:03:22 +1000145 mutex_unlock(&dev->struct_mutex);
146 if (item) {
Eric Anholtc153f452007-09-03 12:06:45 +1000147 mem->offset = ((pool == 0) ?
Thomas Hellstromce65a442006-08-07 22:03:22 +1000148 dev_priv->vram_offset : dev_priv->agp_offset) +
149 (item->mm->
150 offset(item->mm, item->mm_info) << SIS_MM_ALIGN_SHIFT);
Eric Anholtc153f452007-09-03 12:06:45 +1000151 mem->free = item->user_hash.key;
152 mem->size = mem->size << SIS_MM_ALIGN_SHIFT;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000153 } else {
Eric Anholtc153f452007-09-03 12:06:45 +1000154 mem->offset = 0;
155 mem->size = 0;
156 mem->free = 0;
Eric Anholt20caafa2007-08-25 19:22:43 +1000157 retval = -ENOMEM;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000158 }
159
Eric Anholtc153f452007-09-03 12:06:45 +1000160 DRM_DEBUG("alloc %d, size = %d, offset = %d\n", pool, mem->size,
161 mem->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 return retval;
164}
165
Eric Anholtc153f452007-09-03 12:06:45 +1000166static int sis_drm_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 drm_sis_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000169 drm_sis_mem_t *mem = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000170 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Thomas Hellstromce65a442006-08-07 22:03:22 +1000172 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000173 ret = drm_sman_free_key(&dev_priv->sman, mem->free);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000174 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000175 DRM_DEBUG("free = 0x%lx\n", mem->free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Thomas Hellstromce65a442006-08-07 22:03:22 +1000177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Eric Anholtc153f452007-09-03 12:06:45 +1000180static int sis_fb_alloc(struct drm_device *dev, void *data,
181 struct drm_file *file_priv)
Thomas Hellstromce65a442006-08-07 22:03:22 +1000182{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000183 return sis_drm_alloc(dev, file_priv, data, VIDEO_TYPE);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000184}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Eric Anholtc153f452007-09-03 12:06:45 +1000186static int sis_ioctl_agp_init(struct drm_device *dev, void *data,
187 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 drm_sis_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000190 drm_sis_agp_t *agp = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000191 int ret;
192 dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Thomas Hellstromce65a442006-08-07 22:03:22 +1000194 mutex_lock(&dev->struct_mutex);
195 ret = drm_sman_set_range(&dev_priv->sman, AGP_TYPE, 0,
Eric Anholtc153f452007-09-03 12:06:45 +1000196 agp->size >> SIS_MM_ALIGN_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Thomas Hellstromce65a442006-08-07 22:03:22 +1000198 if (ret) {
199 DRM_ERROR("AGP memory manager initialisation error\n");
200 mutex_unlock(&dev->struct_mutex);
201 return ret;
202 }
203
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000204 dev_priv->agp_initialized = 1;
Eric Anholtc153f452007-09-03 12:06:45 +1000205 dev_priv->agp_offset = agp->offset;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000206 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Márton Németh3e684ea2008-01-24 15:58:57 +1000208 DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
210}
211
Eric Anholtc153f452007-09-03 12:06:45 +1000212static int sis_ioctl_agp_alloc(struct drm_device *dev, void *data,
213 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000215
Eric Anholt6c340ea2007-08-25 20:23:09 +1000216 return sis_drm_alloc(dev, file_priv, data, AGP_TYPE);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000217}
218
Dave Airlie84b1fd12007-07-11 15:53:27 +1000219static drm_local_map_t *sis_reg_init(struct drm_device *dev)
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000220{
Dave Airlie55910512007-07-11 16:53:40 +1000221 struct drm_map_list *entry;
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000222 drm_local_map_t *map;
223
Dave Airliebd1b3312007-05-26 05:01:51 +1000224 list_for_each_entry(entry, &dev->maplist, head) {
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000225 map = entry->map;
226 if (!map)
227 continue;
228 if (map->type == _DRM_REGISTERS) {
229 return map;
230 }
231 }
232 return NULL;
233}
234
Dave Airlie84b1fd12007-07-11 15:53:27 +1000235int sis_idle(struct drm_device *dev)
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000236{
237 drm_sis_private_t *dev_priv = dev->dev_private;
238 uint32_t idle_reg;
239 unsigned long end;
240 int i;
241
242 if (dev_priv->idle_fault)
243 return 0;
244
245 if (dev_priv->mmio == NULL) {
246 dev_priv->mmio = sis_reg_init(dev);
247 if (dev_priv->mmio == NULL) {
248 DRM_ERROR("Could not find register map.\n");
249 return 0;
250 }
251 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000252
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000253 /*
254 * Implement a device switch here if needed
255 */
256
257 if (dev_priv->chipset != SIS_CHIP_315)
258 return 0;
259
260 /*
261 * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here
262 * because its polling frequency is too low.
263 */
264
265 end = jiffies + (DRM_HZ * 3);
266
267 for (i=0; i<4; ++i) {
268 do {
269 idle_reg = SIS_READ(0x85cc);
270 } while ( !time_after_eq(jiffies, end) &&
271 ((idle_reg & 0x80000000) != 0x80000000));
272 }
273
274 if (time_after_eq(jiffies, end)) {
275 DRM_ERROR("Graphics engine idle timeout. "
276 "Disabling idle check\n");
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000277 dev_priv->idle_fault = 1;
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000278 }
279
280 /*
281 * The caller never sees an error code. It gets trapped
282 * in libdrm.
283 */
284
285 return 0;
286}
287
288
Thomas Hellstromce65a442006-08-07 22:03:22 +1000289void sis_lastclose(struct drm_device *dev)
290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 drm_sis_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000292
Thomas Hellstromce65a442006-08-07 22:03:22 +1000293 if (!dev_priv)
294 return;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295
Thomas Hellstromce65a442006-08-07 22:03:22 +1000296 mutex_lock(&dev->struct_mutex);
297 drm_sman_cleanup(&dev_priv->sman);
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000298 dev_priv->vram_initialized = 0;
299 dev_priv->agp_initialized = 0;
Thomas Hellstrom7981bf72006-08-08 21:34:46 +1000300 dev_priv->mmio = NULL;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000301 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Eric Anholt6c340ea2007-08-25 20:23:09 +1000304void sis_reclaim_buffers_locked(struct drm_device * dev,
305 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 drm_sis_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Thomas Hellstromce65a442006-08-07 22:03:22 +1000309 mutex_lock(&dev->struct_mutex);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000310 if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)file_priv)) {
Thomas Hellstromce65a442006-08-07 22:03:22 +1000311 mutex_unlock(&dev->struct_mutex);
312 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
Thomas Hellstromce65a442006-08-07 22:03:22 +1000315 if (dev->driver->dma_quiescent) {
316 dev->driver->dma_quiescent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318
Eric Anholt6c340ea2007-08-25 20:23:09 +1000319 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)file_priv);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000320 mutex_unlock(&dev->struct_mutex);
321 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Eric Anholtc153f452007-09-03 12:06:45 +1000324struct drm_ioctl_desc sis_ioctls[] = {
325 DRM_IOCTL_DEF(DRM_SIS_FB_ALLOC, sis_fb_alloc, DRM_AUTH),
326 DRM_IOCTL_DEF(DRM_SIS_FB_FREE, sis_drm_free, DRM_AUTH),
327 DRM_IOCTL_DEF(DRM_SIS_AGP_INIT, sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
328 DRM_IOCTL_DEF(DRM_SIS_AGP_ALLOC, sis_ioctl_agp_alloc, DRM_AUTH),
329 DRM_IOCTL_DEF(DRM_SIS_AGP_FREE, sis_drm_free, DRM_AUTH),
330 DRM_IOCTL_DEF(DRM_SIS_FB_INIT, sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331};
332
333int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);