1 /**************************************************************************
3 * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
33 int drm_add_user_object(struct drm_file *priv, struct drm_user_object *item,
36 struct drm_device *dev = priv->head->dev;
39 DRM_ASSERT_LOCKED(&dev->struct_mutex);
41 /* The refcount will be bumped to 1 when we add the ref object below. */
42 atomic_set(&item->refcount, 0);
43 item->shareable = shareable;
46 ret = drm_ht_just_insert_please(&dev->object_hash, &item->hash,
47 (unsigned long)item, 32, 0, 0);
51 ret = drm_add_ref_object(priv, item, _DRM_REF_USE);
53 ret = drm_ht_remove_item(&dev->object_hash, &item->hash);
57 EXPORT_SYMBOL(drm_add_user_object);
59 struct drm_user_object *drm_lookup_user_object(struct drm_file *priv, uint32_t key)
61 struct drm_device *dev = priv->head->dev;
62 struct drm_hash_item *hash;
64 struct drm_user_object *item;
66 DRM_ASSERT_LOCKED(&dev->struct_mutex);
68 ret = drm_ht_find_item(&dev->object_hash, key, &hash);
72 item = drm_hash_entry(hash, struct drm_user_object, hash);
74 if (priv != item->owner) {
75 struct drm_open_hash *ht = &priv->refd_object_hash[_DRM_REF_USE];
76 ret = drm_ht_find_item(ht, (unsigned long)item, &hash);
78 DRM_ERROR("Object not registered for usage\n");
84 EXPORT_SYMBOL(drm_lookup_user_object);
86 static void drm_deref_user_object(struct drm_file *priv, struct drm_user_object *item)
88 struct drm_device *dev = priv->head->dev;
91 if (atomic_dec_and_test(&item->refcount)) {
92 ret = drm_ht_remove_item(&dev->object_hash, &item->hash);
94 item->remove(priv, item);
98 static int drm_object_ref_action(struct drm_file *priv, struct drm_user_object *ro,
99 enum drm_ref_type action)
105 atomic_inc(&ro->refcount);
108 if (!ro->ref_struct_locked) {
111 ro->ref_struct_locked(priv, ro, action);
117 int drm_add_ref_object(struct drm_file *priv, struct drm_user_object *referenced_object,
118 enum drm_ref_type ref_action)
121 struct drm_ref_object *item;
122 struct drm_open_hash *ht = &priv->refd_object_hash[ref_action];
124 DRM_ASSERT_LOCKED(&priv->head->dev->struct_mutex);
125 if (!referenced_object->shareable && priv != referenced_object->owner) {
126 DRM_ERROR("Not allowed to reference this object\n");
131 * If this is not a usage reference, Check that usage has been registered
132 * first. Otherwise strange things may happen on destruction.
135 if ((ref_action != _DRM_REF_USE) && priv != referenced_object->owner) {
137 drm_lookup_ref_object(priv, referenced_object,
141 ("Object not registered for usage by this client\n");
148 drm_lookup_ref_object(priv, referenced_object, ref_action))) {
149 atomic_inc(&item->refcount);
150 return drm_object_ref_action(priv, referenced_object,
154 item = drm_ctl_calloc(1, sizeof(*item), DRM_MEM_OBJECTS);
156 DRM_ERROR("Could not allocate reference object\n");
160 atomic_set(&item->refcount, 1);
161 item->hash.key = (unsigned long)referenced_object;
162 ret = drm_ht_insert_item(ht, &item->hash);
163 item->unref_action = ref_action;
168 list_add(&item->list, &priv->refd_objects);
169 ret = drm_object_ref_action(priv, referenced_object, ref_action);
174 struct drm_ref_object *drm_lookup_ref_object(struct drm_file *priv,
175 struct drm_user_object *referenced_object,
176 enum drm_ref_type ref_action)
178 struct drm_hash_item *hash;
181 DRM_ASSERT_LOCKED(&priv->head->dev->struct_mutex);
182 ret = drm_ht_find_item(&priv->refd_object_hash[ref_action],
183 (unsigned long)referenced_object, &hash);
187 return drm_hash_entry(hash, struct drm_ref_object, hash);
189 EXPORT_SYMBOL(drm_lookup_ref_object);
191 static void drm_remove_other_references(struct drm_file *priv,
192 struct drm_user_object *ro)
195 struct drm_open_hash *ht;
196 struct drm_hash_item *hash;
197 struct drm_ref_object *item;
199 for (i = _DRM_REF_USE + 1; i < _DRM_NO_REF_TYPES; ++i) {
200 ht = &priv->refd_object_hash[i];
201 while (!drm_ht_find_item(ht, (unsigned long)ro, &hash)) {
202 item = drm_hash_entry(hash, struct drm_ref_object, hash);
203 drm_remove_ref_object(priv, item);
208 void drm_remove_ref_object(struct drm_file *priv, struct drm_ref_object *item)
211 struct drm_user_object *user_object = (struct drm_user_object *) item->hash.key;
212 struct drm_open_hash *ht = &priv->refd_object_hash[item->unref_action];
213 enum drm_ref_type unref_action;
215 DRM_ASSERT_LOCKED(&priv->head->dev->struct_mutex);
216 unref_action = item->unref_action;
217 if (atomic_dec_and_test(&item->refcount)) {
218 ret = drm_ht_remove_item(ht, &item->hash);
220 list_del_init(&item->list);
221 if (unref_action == _DRM_REF_USE)
222 drm_remove_other_references(priv, user_object);
223 drm_ctl_free(item, sizeof(*item), DRM_MEM_OBJECTS);
226 switch (unref_action) {
228 drm_deref_user_object(priv, user_object);
231 BUG_ON(!user_object->unref);
232 user_object->unref(priv, user_object, unref_action);
237 EXPORT_SYMBOL(drm_remove_ref_object);
239 int drm_user_object_ref(struct drm_file *priv, uint32_t user_token,
240 enum drm_object_type type, struct drm_user_object **object)
242 struct drm_device *dev = priv->head->dev;
243 struct drm_user_object *uo;
244 struct drm_hash_item *hash;
247 mutex_lock(&dev->struct_mutex);
248 ret = drm_ht_find_item(&dev->object_hash, user_token, &hash);
250 DRM_ERROR("Could not find user object to reference.\n");
253 uo = drm_hash_entry(hash, struct drm_user_object, hash);
254 if (uo->type != type) {
258 ret = drm_add_ref_object(priv, uo, _DRM_REF_USE);
261 mutex_unlock(&dev->struct_mutex);
265 mutex_unlock(&dev->struct_mutex);
269 int drm_user_object_unref(struct drm_file *priv, uint32_t user_token,
270 enum drm_object_type type)
272 struct drm_device *dev = priv->head->dev;
273 struct drm_user_object *uo;
274 struct drm_ref_object *ro;
277 mutex_lock(&dev->struct_mutex);
278 uo = drm_lookup_user_object(priv, user_token);
279 if (!uo || (uo->type != type)) {
283 ro = drm_lookup_ref_object(priv, uo, _DRM_REF_USE);
288 drm_remove_ref_object(priv, ro);
289 mutex_unlock(&dev->struct_mutex);
292 mutex_unlock(&dev->struct_mutex);