summaryrefslogtreecommitdiff
path: root/shared-core/radeon_irq.c
AgeCommit message (Expand)Author
2005-11-28Assert an MIT copyright on sis_drm.h, since one was lacking and I createdEric Anholt
2005-11-11fix up radeon whitespaceDave Airlie
2005-06-06fix some issues with radeon interrupt handlingDave Airlie
2005-02-01cleanup patch from Adrian Bunk <bunk@stusta.de>Dave Airlie
2004-09-30Lindent of core build. Drivers checked for no binary diffs. A few filesJon Smirl
2004-09-27First check in for DRM that splits core from personality modulesJon Smirl
2004-08-24Merged drmfntbl-0-0-2Dave Airlie
2004-07-25sync up with current 2.6 kernel bk tree - mostly __user annotationsDave Airlie
2003-10-17- Move IRQ functions from drm_dma.h to new drm_irq.h and disentangle themEric Anholt
2003-07-29IRQ code cleanup suggested by Linus TorvaldsMichel Daenzer
2003-07-25Compile fixes for recent 2.5/2.6 Linux kernels. I hope this doesn't breakMichel Daenzer
2003-04-26Ensure driver has been initialized (dev_private != NULL) before installingLeif Delgass
2003-03-28merged drm-filp-0-1-branchKeith Whitwell
2003-02-04only acknowledge interrupts we handle - others could be used outside theMichel Daenzer
2002-12-04further vertical blank interrupt cleanups: remove unused variable,Michel Daenzer
2002-12-03vertical blank interrupt cleanups: use spinlock instead of semaphore, sendMichel Daenzer
2002-11-30vertical blank ioctl can send signal instead of blockingMichel Daenzer
2002-10-01fix wait condition for vertical blank IRQsMichel Daenzer
2002-09-29Move os-dependent stuff out of radeon_irq.cKeith Whitwell
2002-09-29Fix up BSD irq handling.Eric Anholt
2002-09-27make SW interrupts more robust: write sequence number to scratch register,Michel Daenzer
2002-09-26DRM(vblank_wait) is driver specificMichel Daenzer
2002-09-25common ioctl to wait for vertical blank IRQsMichel Daenzer
2002-09-23merged r200-0-2-branch to trunkKeith Whitwell
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
/**************************************************************************
 * 
 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * 
 **************************************************************************/

#include "drmP.h"

int drm_add_user_object(drm_file_t * priv, drm_user_object_t * item,
			int shareable)
{
	drm_device_t *dev = priv->head->dev;
	int ret;

	atomic_set(&item->refcount, 1);
	item->shareable = shareable;
	item->owner = priv;

	ret = drm_ht_just_insert_please(&dev->object_hash, &item->hash,
					(unsigned long)item, 32, 0, 0);
	if (ret)
		return ret;

	list_add_tail(&item->list, &priv->user_objects);
	return 0;
}

drm_user_object_t *drm_lookup_user_object(drm_file_t * priv, uint32_t key)
{
	drm_device_t *dev = priv->head->dev;
	drm_hash_item_t *hash;
	int ret;
	drm_user_object_t *item;

	ret = drm_ht_find_item(&dev->object_hash, key, &hash);
	if (ret) {
		return NULL;
	}
	item = drm_hash_entry(hash, drm_user_object_t, hash);

	if (priv != item->owner) {
		drm_open_hash_t *ht = &priv->refd_object_hash[_DRM_REF_USE];
		ret = drm_ht_find_item(ht, (unsigned long)item, &hash);
		if (ret) {
			DRM_ERROR("Object not registered for usage\n");
			return NULL;
		}
	}
	return item;
}

static void drm_deref_user_object(drm_file_t * priv, drm_user_object_t * item)
{
	drm_device_t *dev = priv->head->dev;
	int ret;

	if (atomic_dec_and_test(&item->refcount)) {
		ret = drm_ht_remove_item(&dev->object_hash, &item->hash);
		BUG_ON(ret);
		list_del_init(&item->list);
		item->remove(priv, item);
	}
}

int drm_remove_user_object(drm_file_t * priv, drm_user_object_t * item)
{
	if (item->owner != priv) {
		DRM_ERROR("Cannot destroy object not owned by you.\n");
		return -EINVAL;
	}
	item->owner = 0;
	item->shareable = 0;
	list_del_init(&item->list);
	drm_deref_user_object(priv, item);
	return 0;
}

static int drm_object_ref_action(drm_file_t * priv, drm_user_object_t * ro,
				 drm_ref_t action)
{
	int ret = 0;

	switch (action) {
	case _DRM_REF_USE:
		atomic_inc(&ro->refcount);
		break;
	default:
		if (!ro->ref_struct_locked) {
			break;
		} else {
			ro->ref_struct_locked(priv, ro, action);
		}
	}
	return ret;
}

int drm_add_ref_object(drm_file_t * priv, drm_user_object_t * referenced_object,
		       drm_ref_t ref_action)
{
	int ret = 0;
	drm_ref_object_t *item;
	drm_open_hash_t *ht = &priv->refd_object_hash[ref_action];

	if (!referenced_object->shareable && priv != referenced_object->owner) {
		DRM_ERROR("Not allowed to reference this object\n");
		return -EINVAL;
	}

	/*
	 * If this is not a usage reference, Check that usage has been registered
	 * first. Otherwise strange things may happen on destruction.
	 */

	if ((ref_action != _DRM_REF_USE) && priv != referenced_object->owner) {
		item =
		    drm_lookup_ref_object(priv, referenced_object,
					  _DRM_REF_USE);
		if (!item) {
			DRM_ERROR
			    ("Object not registered for usage by this client\n");
			return -EINVAL;
		}
	}

	if (NULL !=
	    (item =
	     drm_lookup_ref_object(priv, referenced_object, ref_action))) {
		atomic_inc(&item->refcount);
		return drm_object_ref_action(priv, referenced_object,
					     ref_action);
	}

	item = drm_calloc(1, sizeof(*item), DRM_MEM_OBJECTS);
	if (item == NULL) {
		DRM_ERROR("Could not allocate reference object\n");
		return -ENOMEM;
	}

	atomic_set(&item->refcount, 1);
	item->hash.key = (unsigned long)referenced_object;
	ret = drm_ht_insert_item(ht, &item->hash);
	item->unref_action = ref_action;

	if (ret)
		goto out;

	list_add(&item->list, &priv->refd_objects);
	ret = drm_object_ref_action(priv, referenced_object, ref_action);
      out:
	return ret;
}

drm_ref_object_t *drm_lookup_ref_object(drm_file_t * priv,
					drm_user_object_t * referenced_object,
					drm_ref_t ref_action)
{
	drm_hash_item_t *hash;
	int ret;

	ret = drm_ht_find_item(&priv->refd_object_hash[ref_action],
			       (unsigned long)referenced_object, &hash);
	if (ret)
		return NULL;

	return drm_hash_entry(hash, drm_ref_object_t, hash);
}

static void drm_remove_other_references(drm_file_t * priv,
					drm_user_object_t * ro)
{
	int i;
	drm_open_hash_t *ht;
	drm_hash_item_t *hash;
	drm_ref_object_t *item;

	for (i = _DRM_REF_USE + 1; i < _DRM_NO_REF_TYPES; ++i) {
		ht = &priv->refd_object_hash[i];
		while (!drm_ht_find_item(ht, (unsigned long)ro, &hash)) {
			item = drm_hash_entry(hash, drm_ref_object_t, hash);
			drm_remove_ref_object(priv, item);
		}
	}
}

void drm_remove_ref_object(drm_file_t * priv, drm_ref_object_t * item)
{
	int ret;
	drm_user_object_t *user_object = (drm_user_object_t *) item->hash.key;
	drm_open_hash_t *ht = &priv->refd_object_hash[item->unref_action];
	drm_ref_t unref_action;

	unref_action = item->unref_action;
	if (atomic_dec_and_test(&item->refcount)) {
		ret = drm_ht_remove_item(ht, &item->hash);
		BUG_ON(ret);
		list_del_init(&item->list);
		if (unref_action == _DRM_REF_USE)
			drm_remove_other_references(priv, user_object);
		drm_free(item, sizeof(*item), DRM_MEM_OBJECTS);
	}

	switch (unref_action) {
	case _DRM_REF_USE:
		drm_deref_user_object(priv, user_object);
		break;
	default:
		BUG_ON(!user_object->unref);
		user_object->unref(priv, user_object, unref_action);
		break;
	}

}

int drm_user_object_ref(drm_file_t * priv, uint32_t user_token,
			drm_object_type_t type, drm_user_object_t ** object)
{
	drm_device_t *dev = priv->head->dev;
	drm_user_object_t *uo;
	int ret;

	mutex_lock(&dev->struct_mutex);
	uo = drm_lookup_user_object(priv, user_token);
	if (!uo || (uo->type != type)) {
		ret = -EINVAL;
		goto out_err;
	}
	ret = drm_add_ref_object(priv, uo, _DRM_REF_USE);
	if (ret)
		goto out_err;
	mutex_unlock(&dev->struct_mutex);
	*object = uo;
	DRM_ERROR("Referenced an object\n");
	return 0;
      out_err:
	mutex_unlock(&dev->struct_mutex);
	return ret;
}

int drm_user_object_unref(drm_file_t * priv, uint32_t user_token,
			  drm_object_type_t type)
{
	drm_device_t *dev = priv->head->dev;
	drm_user_object_t *uo;
	drm_ref_object_t *ro;
	int ret;

	mutex_lock(&dev->struct_mutex);
	uo = drm_lookup_user_object(priv, user_token);
	if (!uo || (uo->type != type)) {
		ret = -EINVAL;
		goto out_err;
	}
	ro = drm_lookup_ref_object(priv, uo, _DRM_REF_USE);
	if (!ro) {
		ret = -EINVAL;
		goto out_err;
	}
	drm_remove_ref_object(priv, ro);
	mutex_unlock(&dev->struct_mutex);
	DRM_ERROR("Unreferenced an object\n");
	return 0;
      out_err:
	mutex_unlock(&dev->struct_mutex);
	return ret;
}