2006-08-21 12:30:19 -06:00
|
|
|
|
/**************************************************************************
|
2007-03-18 15:23:43 -06:00
|
|
|
|
*
|
2007-02-08 16:07:29 -07:00
|
|
|
|
* Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
|
2006-08-21 12:30:19 -06:00
|
|
|
|
* All Rights Reserved.
|
2007-03-18 15:23:43 -06:00
|
|
|
|
*
|
2006-08-21 12:30:19 -06:00
|
|
|
|
* 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:
|
2007-02-08 16:07:29 -07:00
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
|
* of the Software.
|
2007-03-18 15:23:43 -06:00
|
|
|
|
*
|
2006-08-21 12:30:19 -06:00
|
|
|
|
* 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,
|
2007-03-18 15:23:43 -06:00
|
|
|
|
* 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
|
2006-08-21 12:30:19 -06:00
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2007-03-18 15:23:43 -06:00
|
|
|
|
*
|
2006-08-21 12:30:19 -06:00
|
|
|
|
**************************************************************************/
|
2007-02-08 16:07:29 -07:00
|
|
|
|
/*
|
|
|
|
|
* Authors: Thomas Hellstr<EFBFBD>m <thomas-at-tungstengraphics-dot-com>
|
|
|
|
|
*/
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
|
|
|
|
#include "drmP.h"
|
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
int drm_add_user_object(struct drm_file *priv, struct drm_user_object *item,
|
2006-08-21 12:30:19 -06:00
|
|
|
|
int shareable)
|
|
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
|
struct drm_device *dev = priv->head->dev;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
int ret;
|
|
|
|
|
|
2007-06-14 03:52:38 -06:00
|
|
|
|
DRM_ASSERT_LOCKED(&dev->struct_mutex);
|
|
|
|
|
|
2007-10-09 19:09:31 -06:00
|
|
|
|
/* The refcount will be bumped to 1 when we add the ref object below. */
|
|
|
|
|
atomic_set(&item->refcount, 0);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
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;
|
|
|
|
|
|
2007-10-09 19:09:31 -06:00
|
|
|
|
ret = drm_add_ref_object(priv, item, _DRM_REF_USE);
|
|
|
|
|
if (ret)
|
|
|
|
|
ret = drm_ht_remove_item(&dev->object_hash, &item->hash);
|
|
|
|
|
|
|
|
|
|
return ret;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
}
|
2007-09-22 05:34:33 -06:00
|
|
|
|
EXPORT_SYMBOL(drm_add_user_object);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
struct drm_user_object *drm_lookup_user_object(struct drm_file *priv, uint32_t key)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
|
struct drm_device *dev = priv->head->dev;
|
2007-07-15 20:48:44 -06:00
|
|
|
|
struct drm_hash_item *hash;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
int ret;
|
2007-07-15 20:48:44 -06:00
|
|
|
|
struct drm_user_object *item;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
2007-06-14 03:52:38 -06:00
|
|
|
|
DRM_ASSERT_LOCKED(&dev->struct_mutex);
|
|
|
|
|
|
2006-08-21 12:30:19 -06:00
|
|
|
|
ret = drm_ht_find_item(&dev->object_hash, key, &hash);
|
2007-11-05 02:09:18 -07:00
|
|
|
|
if (ret)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
return NULL;
|
2007-11-05 02:09:18 -07:00
|
|
|
|
|
2007-07-15 20:48:44 -06:00
|
|
|
|
item = drm_hash_entry(hash, struct drm_user_object, hash);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
|
|
|
|
if (priv != item->owner) {
|
2007-07-15 20:48:44 -06:00
|
|
|
|
struct drm_open_hash *ht = &priv->refd_object_hash[_DRM_REF_USE];
|
2006-08-21 12:30:19 -06:00
|
|
|
|
ret = drm_ht_find_item(ht, (unsigned long)item, &hash);
|
|
|
|
|
if (ret) {
|
|
|
|
|
DRM_ERROR("Object not registered for usage\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return item;
|
|
|
|
|
}
|
2007-09-22 05:34:33 -06:00
|
|
|
|
EXPORT_SYMBOL(drm_lookup_user_object);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
static void drm_deref_user_object(struct drm_file *priv, struct drm_user_object *item)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
|
struct drm_device *dev = priv->head->dev;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if (atomic_dec_and_test(&item->refcount)) {
|
|
|
|
|
ret = drm_ht_remove_item(&dev->object_hash, &item->hash);
|
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
item->remove(priv, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
static int drm_object_ref_action(struct drm_file *priv, struct drm_user_object *ro,
|
2007-07-15 21:45:39 -06:00
|
|
|
|
enum drm_ref_type action)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
int drm_add_ref_object(struct drm_file *priv, struct drm_user_object *referenced_object,
|
2007-07-15 21:45:39 -06:00
|
|
|
|
enum drm_ref_type ref_action)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
{
|
|
|
|
|
int ret = 0;
|
2007-07-15 20:48:44 -06:00
|
|
|
|
struct drm_ref_object *item;
|
|
|
|
|
struct drm_open_hash *ht = &priv->refd_object_hash[ref_action];
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
2007-06-14 03:52:38 -06:00
|
|
|
|
DRM_ASSERT_LOCKED(&priv->head->dev->struct_mutex);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-17 11:40:57 -06:00
|
|
|
|
item = drm_ctl_calloc(1, sizeof(*item), DRM_MEM_OBJECTS);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
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);
|
2006-08-30 07:08:40 -06:00
|
|
|
|
item->unref_action = ref_action;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
list_add(&item->list, &priv->refd_objects);
|
|
|
|
|
ret = drm_object_ref_action(priv, referenced_object, ref_action);
|
2007-11-05 02:09:18 -07:00
|
|
|
|
out:
|
2006-08-21 12:30:19 -06:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
struct drm_ref_object *drm_lookup_ref_object(struct drm_file *priv,
|
|
|
|
|
struct drm_user_object *referenced_object,
|
2007-07-15 21:45:39 -06:00
|
|
|
|
enum drm_ref_type ref_action)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
{
|
2007-07-15 20:48:44 -06:00
|
|
|
|
struct drm_hash_item *hash;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
int ret;
|
|
|
|
|
|
2007-06-14 03:52:38 -06:00
|
|
|
|
DRM_ASSERT_LOCKED(&priv->head->dev->struct_mutex);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
ret = drm_ht_find_item(&priv->refd_object_hash[ref_action],
|
|
|
|
|
(unsigned long)referenced_object, &hash);
|
|
|
|
|
if (ret)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2007-07-15 20:48:44 -06:00
|
|
|
|
return drm_hash_entry(hash, struct drm_ref_object, hash);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
}
|
2007-09-22 05:34:33 -06:00
|
|
|
|
EXPORT_SYMBOL(drm_lookup_ref_object);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
static void drm_remove_other_references(struct drm_file *priv,
|
|
|
|
|
struct drm_user_object *ro)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
{
|
|
|
|
|
int i;
|
2007-07-15 20:48:44 -06:00
|
|
|
|
struct drm_open_hash *ht;
|
|
|
|
|
struct drm_hash_item *hash;
|
|
|
|
|
struct drm_ref_object *item;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
|
|
|
|
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)) {
|
2007-07-15 20:48:44 -06:00
|
|
|
|
item = drm_hash_entry(hash, struct drm_ref_object, hash);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
drm_remove_ref_object(priv, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
void drm_remove_ref_object(struct drm_file *priv, struct drm_ref_object *item)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
{
|
|
|
|
|
int ret;
|
2007-07-15 20:48:44 -06:00
|
|
|
|
struct drm_user_object *user_object = (struct drm_user_object *) item->hash.key;
|
|
|
|
|
struct drm_open_hash *ht = &priv->refd_object_hash[item->unref_action];
|
2007-07-15 21:45:39 -06:00
|
|
|
|
enum drm_ref_type unref_action;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
|
2007-06-14 03:52:38 -06:00
|
|
|
|
DRM_ASSERT_LOCKED(&priv->head->dev->struct_mutex);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
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);
|
2006-10-17 11:40:57 -06:00
|
|
|
|
drm_ctl_free(item, sizeof(*item), DRM_MEM_OBJECTS);
|
2006-08-21 12:30:19 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
int drm_user_object_ref(struct drm_file *priv, uint32_t user_token,
|
|
|
|
|
enum drm_object_type type, struct drm_user_object **object)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
|
struct drm_device *dev = priv->head->dev;
|
2007-07-15 20:48:44 -06:00
|
|
|
|
struct drm_user_object *uo;
|
|
|
|
|
struct drm_hash_item *hash;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
mutex_lock(&dev->struct_mutex);
|
2007-04-03 02:54:23 -06:00
|
|
|
|
ret = drm_ht_find_item(&dev->object_hash, user_token, &hash);
|
|
|
|
|
if (ret) {
|
|
|
|
|
DRM_ERROR("Could not find user object to reference.\n");
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
2007-07-15 20:48:44 -06:00
|
|
|
|
uo = drm_hash_entry(hash, struct drm_user_object, hash);
|
2007-04-03 02:54:23 -06:00
|
|
|
|
if (uo->type != type) {
|
2006-08-21 12:30:19 -06:00
|
|
|
|
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;
|
|
|
|
|
return 0;
|
2007-11-05 02:09:18 -07:00
|
|
|
|
out_err:
|
2006-08-21 12:30:19 -06:00
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-05 02:09:18 -07:00
|
|
|
|
int drm_user_object_unref(struct drm_file *priv, uint32_t user_token,
|
2007-07-15 20:48:44 -06:00
|
|
|
|
enum drm_object_type type)
|
2006-08-21 12:30:19 -06:00
|
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
|
struct drm_device *dev = priv->head->dev;
|
2007-07-15 20:48:44 -06:00
|
|
|
|
struct drm_user_object *uo;
|
|
|
|
|
struct drm_ref_object *ro;
|
2006-08-21 12:30:19 -06:00
|
|
|
|
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);
|
|
|
|
|
return 0;
|
2007-11-05 02:09:18 -07:00
|
|
|
|
out_err:
|
2006-08-21 12:30:19 -06:00
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|