Home | History | Annotate | Line # | Download | only in sis
sis_mm.c revision 1.2
      1 /*	$NetBSD: sis_mm.c,v 1.2 2018/08/27 04:58:36 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.2 2018/08/27 04:58:36 riastradh Exp $");
     38 
     39 #include <drm/drmP.h>
     40 #include <drm/sis_drm.h>
     41 #include "sis_drv.h"
     42 
     43 #include <video/sisfb.h>
     44 
     45 #define VIDEO_TYPE 0
     46 #define AGP_TYPE 1
     47 
     48 
     49 struct sis_memblock {
     50 	struct drm_mm_node mm_node;
     51 	struct sis_memreq req;
     52 	struct list_head owner_list;
     53 };
     54 
     55 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
     56 /* fb management via fb device */
     57 
     58 #define SIS_MM_ALIGN_SHIFT 0
     59 #define SIS_MM_ALIGN_MASK 0
     60 
     61 #else /* CONFIG_FB_SIS[_MODULE] */
     62 
     63 #define SIS_MM_ALIGN_SHIFT 4
     64 #define SIS_MM_ALIGN_MASK ((1 << SIS_MM_ALIGN_SHIFT) - 1)
     65 
     66 #endif /* CONFIG_FB_SIS[_MODULE] */
     67 
     68 static int sis_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
     69 {
     70 	drm_sis_private_t *dev_priv = dev->dev_private;
     71 	drm_sis_fb_t *fb = data;
     72 
     73 	mutex_lock(&dev->struct_mutex);
     74 	/* Unconditionally init the drm_mm, even though we don't use it when the
     75 	 * fb sis driver is available - make cleanup easier. */
     76 	drm_mm_init(&dev_priv->vram_mm, 0, fb->size >> SIS_MM_ALIGN_SHIFT);
     77 
     78 	dev_priv->vram_initialized = 1;
     79 	dev_priv->vram_offset = fb->offset;
     80 
     81 	mutex_unlock(&dev->struct_mutex);
     82 	DRM_DEBUG("offset = %lu, size = %lu\n", fb->offset, fb->size);
     83 
     84 	return 0;
     85 }
     86 
     87 static int sis_drm_alloc(struct drm_device *dev, struct drm_file *file,
     88 			 void *data, int pool)
     89 {
     90 	drm_sis_private_t *dev_priv = dev->dev_private;
     91 	drm_sis_mem_t *mem = data;
     92 	int retval = 0, user_key;
     93 	struct sis_memblock *item;
     94 	struct sis_file_private *file_priv = file->driver_priv;
     95 	unsigned long offset;
     96 
     97 	mutex_lock(&dev->struct_mutex);
     98 
     99 	if (0 == ((pool == 0) ? dev_priv->vram_initialized :
    100 		      dev_priv->agp_initialized)) {
    101 		DRM_ERROR
    102 		    ("Attempt to allocate from uninitialized memory manager.\n");
    103 		mutex_unlock(&dev->struct_mutex);
    104 		return -EINVAL;
    105 	}
    106 
    107 	item = kzalloc(sizeof(*item), GFP_KERNEL);
    108 	if (!item) {
    109 		retval = -ENOMEM;
    110 		goto fail_alloc;
    111 	}
    112 
    113 	mem->size = (mem->size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
    114 	if (pool == AGP_TYPE) {
    115 		retval = drm_mm_insert_node(&dev_priv->agp_mm,
    116 					    &item->mm_node,
    117 					    mem->size, 0,
    118 					    DRM_MM_SEARCH_DEFAULT);
    119 		offset = item->mm_node.start;
    120 	} else {
    121 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
    122 		item->req.size = mem->size;
    123 		sis_malloc(&item->req);
    124 		if (item->req.size == 0)
    125 			retval = -ENOMEM;
    126 		offset = item->req.offset;
    127 #else
    128 		retval = drm_mm_insert_node(&dev_priv->vram_mm,
    129 					    &item->mm_node,
    130 					    mem->size, 0,
    131 					    DRM_MM_SEARCH_DEFAULT);
    132 		offset = item->mm_node.start;
    133 #endif
    134 	}
    135 	if (retval)
    136 		goto fail_alloc;
    137 
    138 	retval = idr_alloc(&dev_priv->object_idr, item, 1, 0, GFP_KERNEL);
    139 	if (retval < 0)
    140 		goto fail_idr;
    141 	user_key = retval;
    142 
    143 	list_add(&item->owner_list, &file_priv->obj_list);
    144 	mutex_unlock(&dev->struct_mutex);
    145 
    146 	mem->offset = ((pool == 0) ?
    147 		      dev_priv->vram_offset : dev_priv->agp_offset) +
    148 	    (offset << SIS_MM_ALIGN_SHIFT);
    149 	mem->free = user_key;
    150 	mem->size = mem->size << SIS_MM_ALIGN_SHIFT;
    151 
    152 	return 0;
    153 
    154 fail_idr:
    155 	drm_mm_remove_node(&item->mm_node);
    156 fail_alloc:
    157 	kfree(item);
    158 	mutex_unlock(&dev->struct_mutex);
    159 
    160 	mem->offset = 0;
    161 	mem->size = 0;
    162 	mem->free = 0;
    163 
    164 	DRM_DEBUG("alloc %d, size = %ld, offset = %ld\n", pool, mem->size,
    165 		  mem->offset);
    166 
    167 	return retval;
    168 }
    169 
    170 static int sis_drm_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
    171 {
    172 	drm_sis_private_t *dev_priv = dev->dev_private;
    173 	drm_sis_mem_t *mem = data;
    174 	struct sis_memblock *obj;
    175 
    176 	mutex_lock(&dev->struct_mutex);
    177 	obj = idr_find(&dev_priv->object_idr, mem->free);
    178 	if (obj == NULL) {
    179 		mutex_unlock(&dev->struct_mutex);
    180 		return -EINVAL;
    181 	}
    182 
    183 	idr_remove(&dev_priv->object_idr, mem->free);
    184 	list_del(&obj->owner_list);
    185 	if (drm_mm_node_allocated(&obj->mm_node))
    186 		drm_mm_remove_node(&obj->mm_node);
    187 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
    188 	else
    189 		sis_free(obj->req.offset);
    190 #endif
    191 	kfree(obj);
    192 	mutex_unlock(&dev->struct_mutex);
    193 	DRM_DEBUG("free = 0x%lx\n", mem->free);
    194 
    195 	return 0;
    196 }
    197 
    198 static int sis_fb_alloc(struct drm_device *dev, void *data,
    199 			struct drm_file *file_priv)
    200 {
    201 	return sis_drm_alloc(dev, file_priv, data, VIDEO_TYPE);
    202 }
    203 
    204 static int sis_ioctl_agp_init(struct drm_device *dev, void *data,
    205 			      struct drm_file *file_priv)
    206 {
    207 	drm_sis_private_t *dev_priv = dev->dev_private;
    208 	drm_sis_agp_t *agp = data;
    209 	dev_priv = dev->dev_private;
    210 
    211 	mutex_lock(&dev->struct_mutex);
    212 	drm_mm_init(&dev_priv->agp_mm, 0, agp->size >> SIS_MM_ALIGN_SHIFT);
    213 
    214 	dev_priv->agp_initialized = 1;
    215 	dev_priv->agp_offset = agp->offset;
    216 	mutex_unlock(&dev->struct_mutex);
    217 
    218 	DRM_DEBUG("offset = %lu, size = %lu\n", agp->offset, agp->size);
    219 	return 0;
    220 }
    221 
    222 static int sis_ioctl_agp_alloc(struct drm_device *dev, void *data,
    223 			       struct drm_file *file_priv)
    224 {
    225 
    226 	return sis_drm_alloc(dev, file_priv, data, AGP_TYPE);
    227 }
    228 
    229 static drm_local_map_t *sis_reg_init(struct drm_device *dev)
    230 {
    231 	struct drm_map_list *entry;
    232 	drm_local_map_t *map;
    233 
    234 	list_for_each_entry(entry, &dev->maplist, head) {
    235 		map = entry->map;
    236 		if (!map)
    237 			continue;
    238 		if (map->type == _DRM_REGISTERS)
    239 			return map;
    240 	}
    241 	return NULL;
    242 }
    243 
    244 int sis_idle(struct drm_device *dev)
    245 {
    246 	drm_sis_private_t *dev_priv = dev->dev_private;
    247 	uint32_t idle_reg;
    248 	unsigned long end;
    249 	int i;
    250 
    251 	if (dev_priv->idle_fault)
    252 		return 0;
    253 
    254 	if (dev_priv->mmio == NULL) {
    255 		dev_priv->mmio = sis_reg_init(dev);
    256 		if (dev_priv->mmio == NULL) {
    257 			DRM_ERROR("Could not find register map.\n");
    258 			return 0;
    259 		}
    260 	}
    261 
    262 	/*
    263 	 * Implement a device switch here if needed
    264 	 */
    265 
    266 	if (dev_priv->chipset != SIS_CHIP_315)
    267 		return 0;
    268 
    269 	/*
    270 	 * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here
    271 	 * because its polling frequency is too low.
    272 	 */
    273 
    274 	end = jiffies + (HZ * 3);
    275 
    276 	for (i = 0; i < 4; ++i) {
    277 		do {
    278 			idle_reg = SIS_READ(0x85cc);
    279 		} while (!time_after_eq(jiffies, end) &&
    280 			  ((idle_reg & 0x80000000) != 0x80000000));
    281 	}
    282 
    283 	if (time_after_eq(jiffies, end)) {
    284 		DRM_ERROR("Graphics engine idle timeout. "
    285 			  "Disabling idle check\n");
    286 		dev_priv->idle_fault = 1;
    287 	}
    288 
    289 	/*
    290 	 * The caller never sees an error code. It gets trapped
    291 	 * in libdrm.
    292 	 */
    293 
    294 	return 0;
    295 }
    296 
    297 
    298 void sis_lastclose(struct drm_device *dev)
    299 {
    300 	drm_sis_private_t *dev_priv = dev->dev_private;
    301 
    302 	if (!dev_priv)
    303 		return;
    304 
    305 	mutex_lock(&dev->struct_mutex);
    306 	if (dev_priv->vram_initialized) {
    307 		drm_mm_takedown(&dev_priv->vram_mm);
    308 		dev_priv->vram_initialized = 0;
    309 	}
    310 	if (dev_priv->agp_initialized) {
    311 		drm_mm_takedown(&dev_priv->agp_mm);
    312 		dev_priv->agp_initialized = 0;
    313 	}
    314 	dev_priv->mmio = NULL;
    315 	mutex_unlock(&dev->struct_mutex);
    316 }
    317 
    318 void sis_reclaim_buffers_locked(struct drm_device *dev,
    319 				struct drm_file *file)
    320 {
    321 	struct sis_file_private *file_priv = file->driver_priv;
    322 	struct sis_memblock *entry, *next;
    323 
    324 	if (!(file->minor->master && file->master->lock.hw_lock))
    325 		return;
    326 
    327 	drm_legacy_idlelock_take(&file->master->lock);
    328 
    329 	mutex_lock(&dev->struct_mutex);
    330 	if (list_empty(&file_priv->obj_list)) {
    331 		mutex_unlock(&dev->struct_mutex);
    332 		drm_legacy_idlelock_release(&file->master->lock);
    333 
    334 		return;
    335 	}
    336 
    337 	sis_idle(dev);
    338 
    339 
    340 	list_for_each_entry_safe(entry, next, &file_priv->obj_list,
    341 				 owner_list) {
    342 		list_del(&entry->owner_list);
    343 		if (drm_mm_node_allocated(&entry->mm_node))
    344 			drm_mm_remove_node(&entry->mm_node);
    345 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
    346 		else
    347 			sis_free(entry->req.offset);
    348 #endif
    349 		kfree(entry);
    350 	}
    351 	mutex_unlock(&dev->struct_mutex);
    352 
    353 	drm_legacy_idlelock_release(&file->master->lock);
    354 
    355 	return;
    356 }
    357 
    358 const struct drm_ioctl_desc sis_ioctls[] = {
    359 	DRM_IOCTL_DEF_DRV(SIS_FB_ALLOC, sis_fb_alloc, DRM_AUTH),
    360 	DRM_IOCTL_DEF_DRV(SIS_FB_FREE, sis_drm_free, DRM_AUTH),
    361 	DRM_IOCTL_DEF_DRV(SIS_AGP_INIT, sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
    362 	DRM_IOCTL_DEF_DRV(SIS_AGP_ALLOC, sis_ioctl_agp_alloc, DRM_AUTH),
    363 	DRM_IOCTL_DEF_DRV(SIS_AGP_FREE, sis_drm_free, DRM_AUTH),
    364 	DRM_IOCTL_DEF_DRV(SIS_FB_INIT, sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
    365 };
    366 
    367 int sis_max_ioctl = ARRAY_SIZE(sis_ioctls);
    368