Home | History | Annotate | Line # | Download | only in sis
sis_mm.c revision 1.1.1.4
      1 /*	$NetBSD: sis_mm.c,v 1.1.1.4 2021/12/18 20:15:53 riastradh Exp $	*/
      2 
      3 /**************************************************************************
      4  *
      5  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
      6  * All Rights Reserved.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the
     10  * "Software"), to deal in the Software without restriction, including
     11  * without limitation the rights to use, copy, modify, merge, publish,
     12  * distribute, sub license, and/or sell copies of the Software, and to
     13  * permit persons to whom the Software is furnished to do so, subject to
     14  * the following conditions:
     15  *
     16  * The above copyright notice and this permission notice (including the
     17  * next paragraph) shall be included in all copies or substantial portions
     18  * of the Software.
     19  *
     20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     27  *
     28  *
     29  **************************************************************************/
     30 
     31 /*
     32  * Authors:
     33  *    Thomas Hellstrm <thomas-at-tungstengraphics-dot-com>
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: sis_mm.c,v 1.1.1.4 2021/12/18 20:15:53 riastradh Exp $");
     38 
     39 #include <video/sisfb.h>
     40 
     41 #include <drm/drm_device.h>
     42 #include <drm/drm_file.h>
     43 #include <drm/sis_drm.h>
     44 
     45 #include "sis_drv.h"
     46 
     47 
     48 #define VIDEO_TYPE 0
     49 #define AGP_TYPE 1
     50 
     51 
     52 struct sis_memblock {
     53 	struct drm_mm_node mm_node;
     54 	struct sis_memreq req;
     55 	struct list_head owner_list;
     56 };
     57 
     58 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
     59 /* fb management via fb device */
     60 
     61 #define SIS_MM_ALIGN_SHIFT 0
     62 #define SIS_MM_ALIGN_MASK 0
     63 
     64 #else /* CONFIG_FB_SIS[_MODULE] */
     65 
     66 #define SIS_MM_ALIGN_SHIFT 4
     67 #define SIS_MM_ALIGN_MASK ((1 << SIS_MM_ALIGN_SHIFT) - 1)
     68 
     69 #endif /* CONFIG_FB_SIS[_MODULE] */
     70 
     71 static int sis_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
     72 {
     73 	drm_sis_private_t *dev_priv = dev->dev_private;
     74 	drm_sis_fb_t *fb = data;
     75 
     76 	mutex_lock(&dev->struct_mutex);
     77 	/* Unconditionally init the drm_mm, even though we don't use it when the
     78 	 * fb sis driver is available - make cleanup easier. */
     79 	drm_mm_init(&dev_priv->vram_mm, 0, fb->size >> SIS_MM_ALIGN_SHIFT);
     80 
     81 	dev_priv->vram_initialized = 1;
     82 	dev_priv->vram_offset = fb->offset;
     83 
     84 	mutex_unlock(&dev->struct_mutex);
     85 	DRM_DEBUG("offset = %lu, size = %lu\n", fb->offset, fb->size);
     86 
     87 	return 0;
     88 }
     89 
     90 static int sis_drm_alloc(struct drm_device *dev, struct drm_file *file,
     91 			 void *data, int pool)
     92 {
     93 	drm_sis_private_t *dev_priv = dev->dev_private;
     94 	drm_sis_mem_t *mem = data;
     95 	int retval = 0, user_key;
     96 	struct sis_memblock *item;
     97 	struct sis_file_private *file_priv = file->driver_priv;
     98 	unsigned long offset;
     99 
    100 	mutex_lock(&dev->struct_mutex);
    101 
    102 	if (0 == ((pool == 0) ? dev_priv->vram_initialized :
    103 		      dev_priv->agp_initialized)) {
    104 		DRM_ERROR
    105 		    ("Attempt to allocate from uninitialized memory manager.\n");
    106 		mutex_unlock(&dev->struct_mutex);
    107 		return -EINVAL;
    108 	}
    109 
    110 	item = kzalloc(sizeof(*item), GFP_KERNEL);
    111 	if (!item) {
    112 		retval = -ENOMEM;
    113 		goto fail_alloc;
    114 	}
    115 
    116 	mem->size = (mem->size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
    117 	if (pool == AGP_TYPE) {
    118 		retval = drm_mm_insert_node(&dev_priv->agp_mm,
    119 					    &item->mm_node,
    120 					    mem->size);
    121 		offset = item->mm_node.start;
    122 	} else {
    123 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
    124 		item->req.size = mem->size;
    125 		sis_malloc(&item->req);
    126 		if (item->req.size == 0)
    127 			retval = -ENOMEM;
    128 		offset = item->req.offset;
    129 #else
    130 		retval = drm_mm_insert_node(&dev_priv->vram_mm,
    131 					    &item->mm_node,
    132 					    mem->size);
    133 		offset = item->mm_node.start;
    134 #endif
    135 	}
    136 	if (retval)
    137 		goto fail_alloc;
    138 
    139 	retval = idr_alloc(&dev_priv->object_idr, item, 1, 0, GFP_KERNEL);
    140 	if (retval < 0)
    141 		goto fail_idr;
    142 	user_key = retval;
    143 
    144 	list_add(&item->owner_list, &file_priv->obj_list);
    145 	mutex_unlock(&dev->struct_mutex);
    146 
    147 	mem->offset = ((pool == 0) ?
    148 		      dev_priv->vram_offset : dev_priv->agp_offset) +
    149 	    (offset << SIS_MM_ALIGN_SHIFT);
    150 	mem->free = user_key;
    151 	mem->size = mem->size << SIS_MM_ALIGN_SHIFT;
    152 
    153 	return 0;
    154 
    155 fail_idr:
    156 	drm_mm_remove_node(&item->mm_node);
    157 fail_alloc:
    158 	kfree(item);
    159 	mutex_unlock(&dev->struct_mutex);
    160 
    161 	mem->offset = 0;
    162 	mem->size = 0;
    163 	mem->free = 0;
    164 
    165 	DRM_DEBUG("alloc %d, size = %ld, offset = %ld\n", pool, mem->size,
    166 		  mem->offset);
    167 
    168 	return retval;
    169 }
    170 
    171 static int sis_drm_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
    172 {
    173 	drm_sis_private_t *dev_priv = dev->dev_private;
    174 	drm_sis_mem_t *mem = data;
    175 	struct sis_memblock *obj;
    176 
    177 	mutex_lock(&dev->struct_mutex);
    178 	obj = idr_find(&dev_priv->object_idr, mem->free);
    179 	if (obj == NULL) {
    180 		mutex_unlock(&dev->struct_mutex);
    181 		return -EINVAL;
    182 	}
    183 
    184 	idr_remove(&dev_priv->object_idr, mem->free);
    185 	list_del(&obj->owner_list);
    186 	if (drm_mm_node_allocated(&obj->mm_node))
    187 		drm_mm_remove_node(&obj->mm_node);
    188 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
    189 	else
    190 		sis_free(obj->req.offset);
    191 #endif
    192 	kfree(obj);
    193 	mutex_unlock(&dev->struct_mutex);
    194 	DRM_DEBUG("free = 0x%lx\n", mem->free);
    195 
    196 	return 0;
    197 }
    198 
    199 static int sis_fb_alloc(struct drm_device *dev, void *data,
    200 			struct drm_file *file_priv)
    201 {
    202 	return sis_drm_alloc(dev, file_priv, data, VIDEO_TYPE);
    203 }
    204 
    205 static int sis_ioctl_agp_init(struct drm_device *dev, void *data,
    206 			      struct drm_file *file_priv)
    207 {
    208 	drm_sis_private_t *dev_priv = dev->dev_private;
    209 	drm_sis_agp_t *agp = data;
    210 	dev_priv = dev->dev_private;
    211 
    212 	mutex_lock(&dev->struct_mutex);
    213 	drm_mm_init(&dev_priv->agp_mm, 0, agp->size >> SIS_MM_ALIGN_SHIFT);
    214 
    215 	dev_priv->agp_initialized = 1;
    216 	dev_priv->agp_offset = agp->offset;
    217 	mutex_unlock(&dev->struct_mutex);
    218 
    219 	DRM_DEBUG("offset = %lu, size = %lu\n", agp->offset, agp->size);
    220 	return 0;
    221 }
    222 
    223 static int sis_ioctl_agp_alloc(struct drm_device *dev, void *data,
    224 			       struct drm_file *file_priv)
    225 {
    226 
    227 	return sis_drm_alloc(dev, file_priv, data, AGP_TYPE);
    228 }
    229 
    230 static drm_local_map_t *sis_reg_init(struct drm_device *dev)
    231 {
    232 	struct drm_map_list *entry;
    233 	drm_local_map_t *map;
    234 
    235 	list_for_each_entry(entry, &dev->maplist, head) {
    236 		map = entry->map;
    237 		if (!map)
    238 			continue;
    239 		if (map->type == _DRM_REGISTERS)
    240 			return map;
    241 	}
    242 	return NULL;
    243 }
    244 
    245 int sis_idle(struct drm_device *dev)
    246 {
    247 	drm_sis_private_t *dev_priv = dev->dev_private;
    248 	uint32_t idle_reg;
    249 	unsigned long end;
    250 	int i;
    251 
    252 	if (dev_priv->idle_fault)
    253 		return 0;
    254 
    255 	if (dev_priv->mmio == NULL) {
    256 		dev_priv->mmio = sis_reg_init(dev);
    257 		if (dev_priv->mmio == NULL) {
    258 			DRM_ERROR("Could not find register map.\n");
    259 			return 0;
    260 		}
    261 	}
    262 
    263 	/*
    264 	 * Implement a device switch here if needed
    265 	 */
    266 
    267 	if (dev_priv->chipset != SIS_CHIP_315)
    268 		return 0;
    269 
    270 	/*
    271 	 * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here
    272 	 * because its polling frequency is too low.
    273 	 */
    274 
    275 	end = jiffies + (HZ * 3);
    276 
    277 	for (i = 0; i < 4; ++i) {
    278 		do {
    279 			idle_reg = SIS_READ(0x85cc);
    280 		} while (!time_after_eq(jiffies, end) &&
    281 			  ((idle_reg & 0x80000000) != 0x80000000));
    282 	}
    283 
    284 	if (time_after_eq(jiffies, end)) {
    285 		DRM_ERROR("Graphics engine idle timeout. "
    286 			  "Disabling idle check\n");
    287 		dev_priv->idle_fault = 1;
    288 	}
    289 
    290 	/*
    291 	 * The caller never sees an error code. It gets trapped
    292 	 * in libdrm.
    293 	 */
    294 
    295 	return 0;
    296 }
    297 
    298 
    299 void sis_lastclose(struct drm_device *dev)
    300 {
    301 	drm_sis_private_t *dev_priv = dev->dev_private;
    302 
    303 	if (!dev_priv)
    304 		return;
    305 
    306 	mutex_lock(&dev->struct_mutex);
    307 	if (dev_priv->vram_initialized) {
    308 		drm_mm_takedown(&dev_priv->vram_mm);
    309 		dev_priv->vram_initialized = 0;
    310 	}
    311 	if (dev_priv->agp_initialized) {
    312 		drm_mm_takedown(&dev_priv->agp_mm);
    313 		dev_priv->agp_initialized = 0;
    314 	}
    315 	dev_priv->mmio = NULL;
    316 	mutex_unlock(&dev->struct_mutex);
    317 }
    318 
    319 void sis_reclaim_buffers_locked(struct drm_device *dev,
    320 				struct drm_file *file)
    321 {
    322 	struct sis_file_private *file_priv = file->driver_priv;
    323 	struct sis_memblock *entry, *next;
    324 
    325 	if (!(dev->master && file->master->lock.hw_lock))
    326 		return;
    327 
    328 	drm_legacy_idlelock_take(&file->master->lock);
    329 
    330 	mutex_lock(&dev->struct_mutex);
    331 	if (list_empty(&file_priv->obj_list)) {
    332 		mutex_unlock(&dev->struct_mutex);
    333 		drm_legacy_idlelock_release(&file->master->lock);
    334 
    335 		return;
    336 	}
    337 
    338 	sis_idle(dev);
    339 
    340 
    341 	list_for_each_entry_safe(entry, next, &file_priv->obj_list,
    342 				 owner_list) {
    343 		list_del(&entry->owner_list);
    344 		if (drm_mm_node_allocated(&entry->mm_node))
    345 			drm_mm_remove_node(&entry->mm_node);
    346 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
    347 		else
    348 			sis_free(entry->req.offset);
    349 #endif
    350 		kfree(entry);
    351 	}
    352 	mutex_unlock(&dev->struct_mutex);
    353 
    354 	drm_legacy_idlelock_release(&file->master->lock);
    355 
    356 	return;
    357 }
    358 
    359 const struct drm_ioctl_desc sis_ioctls[] = {
    360 	DRM_IOCTL_DEF_DRV(SIS_FB_ALLOC, sis_fb_alloc, DRM_AUTH),
    361 	DRM_IOCTL_DEF_DRV(SIS_FB_FREE, sis_drm_free, DRM_AUTH),
    362 	DRM_IOCTL_DEF_DRV(SIS_AGP_INIT, sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
    363 	DRM_IOCTL_DEF_DRV(SIS_AGP_ALLOC, sis_ioctl_agp_alloc, DRM_AUTH),
    364 	DRM_IOCTL_DEF_DRV(SIS_AGP_FREE, sis_drm_free, DRM_AUTH),
    365 	DRM_IOCTL_DEF_DRV(SIS_FB_INIT, sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
    366 };
    367 
    368 int sis_max_ioctl = ARRAY_SIZE(sis_ioctls);
    369