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