xref: /dflybsd-src/sys/dev/drm/drm_global.c (revision a85cb24f18e3804e75ab8bcda7692564d0563317)
15718399fSFrançois Tigeot /**************************************************************************
25718399fSFrançois Tigeot  *
35718399fSFrançois Tigeot  * Copyright 2008-2009 VMware, Inc., Palo Alto, CA., USA
45718399fSFrançois Tigeot  * All Rights Reserved.
55718399fSFrançois Tigeot  *
65718399fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
75718399fSFrançois Tigeot  * copy of this software and associated documentation files (the
85718399fSFrançois Tigeot  * "Software"), to deal in the Software without restriction, including
95718399fSFrançois Tigeot  * without limitation the rights to use, copy, modify, merge, publish,
105718399fSFrançois Tigeot  * distribute, sub license, and/or sell copies of the Software, and to
115718399fSFrançois Tigeot  * permit persons to whom the Software is furnished to do so, subject to
125718399fSFrançois Tigeot  * the following conditions:
135718399fSFrançois Tigeot  *
145718399fSFrançois Tigeot  * The above copyright notice and this permission notice (including the
155718399fSFrançois Tigeot  * next paragraph) shall be included in all copies or substantial portions
165718399fSFrançois Tigeot  * of the Software.
175718399fSFrançois Tigeot  *
185718399fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
195718399fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
205718399fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
215718399fSFrançois Tigeot  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
225718399fSFrançois Tigeot  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
235718399fSFrançois Tigeot  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
245718399fSFrançois Tigeot  * USE OR OTHER DEALINGS IN THE SOFTWARE.
255718399fSFrançois Tigeot  *
265718399fSFrançois Tigeot  **************************************************************************/
275718399fSFrançois Tigeot /*
285718399fSFrançois Tigeot  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
295718399fSFrançois Tigeot  */
305718399fSFrançois Tigeot 
3118e26a6dSFrançois Tigeot #include <drm/drmP.h>
322c9916cdSFrançois Tigeot #include <linux/mutex.h>
332c9916cdSFrançois Tigeot #include <linux/slab.h>
342c9916cdSFrançois Tigeot #include <linux/module.h>
3518e26a6dSFrançois Tigeot #include <drm/drm_global.h>
365718399fSFrançois Tigeot 
375718399fSFrançois Tigeot struct drm_global_item {
385718399fSFrançois Tigeot 	struct lock mutex;
395718399fSFrançois Tigeot 	void *object;
405718399fSFrançois Tigeot 	int refcount;
415718399fSFrançois Tigeot };
425718399fSFrançois Tigeot 
435718399fSFrançois Tigeot static struct drm_global_item glob[DRM_GLOBAL_NUM];
445718399fSFrançois Tigeot 
drm_global_init(void)455718399fSFrançois Tigeot void drm_global_init(void)
465718399fSFrançois Tigeot {
475718399fSFrançois Tigeot 	int i;
485718399fSFrançois Tigeot 
495718399fSFrançois Tigeot 	for (i = 0; i < DRM_GLOBAL_NUM; ++i) {
505718399fSFrançois Tigeot 		struct drm_global_item *item = &glob[i];
515718399fSFrançois Tigeot 		lockinit(&item->mutex, "drmgi", 0, LK_CANRECURSE);
525718399fSFrançois Tigeot 		item->object = NULL;
535718399fSFrançois Tigeot 		item->refcount = 0;
545718399fSFrançois Tigeot 	}
555718399fSFrançois Tigeot }
565718399fSFrançois Tigeot 
drm_global_release(void)575718399fSFrançois Tigeot void drm_global_release(void)
585718399fSFrançois Tigeot {
595718399fSFrançois Tigeot 	int i;
605718399fSFrançois Tigeot 	for (i = 0; i < DRM_GLOBAL_NUM; ++i) {
615718399fSFrançois Tigeot 		struct drm_global_item *item = &glob[i];
622c9916cdSFrançois Tigeot 		BUG_ON(item->object != NULL);
632c9916cdSFrançois Tigeot 		BUG_ON(item->refcount != 0);
645718399fSFrançois Tigeot 	}
655718399fSFrançois Tigeot }
665718399fSFrançois Tigeot 
67*a85cb24fSFrançois Tigeot /**
68*a85cb24fSFrançois Tigeot  * drm_global_item_ref - Initialize and acquire reference to memory
69*a85cb24fSFrançois Tigeot  * object
70*a85cb24fSFrançois Tigeot  * @ref: Object for initialization
71*a85cb24fSFrançois Tigeot  *
72*a85cb24fSFrançois Tigeot  * This initializes a memory object, allocating memory and calling the
73*a85cb24fSFrançois Tigeot  * .init() hook. Further calls will increase the reference count for
74*a85cb24fSFrançois Tigeot  * that item.
75*a85cb24fSFrançois Tigeot  *
76*a85cb24fSFrançois Tigeot  * Returns:
77*a85cb24fSFrançois Tigeot  * Zero on success, non-zero otherwise.
78*a85cb24fSFrançois Tigeot  */
drm_global_item_ref(struct drm_global_reference * ref)795718399fSFrançois Tigeot int drm_global_item_ref(struct drm_global_reference *ref)
805718399fSFrançois Tigeot {
811dedbd3bSFrançois Tigeot 	int ret = 0;
825718399fSFrançois Tigeot 	struct drm_global_item *item = &glob[ref->global_type];
835718399fSFrançois Tigeot 
842c9916cdSFrançois Tigeot 	mutex_lock(&item->mutex);
855718399fSFrançois Tigeot 	if (item->refcount == 0) {
861dedbd3bSFrançois Tigeot 		ref->object = kzalloc(ref->size, GFP_KERNEL);
871dedbd3bSFrançois Tigeot 		if (unlikely(ref->object == NULL)) {
882c9916cdSFrançois Tigeot 			ret = -ENOMEM;
891dedbd3bSFrançois Tigeot 			goto error_unlock;
902c9916cdSFrançois Tigeot 		}
915718399fSFrançois Tigeot 		ret = ref->init(ref);
925718399fSFrançois Tigeot 		if (unlikely(ret != 0))
931dedbd3bSFrançois Tigeot 			goto error_free;
945718399fSFrançois Tigeot 
951dedbd3bSFrançois Tigeot 		item->object = ref->object;
961dedbd3bSFrançois Tigeot 	} else {
975718399fSFrançois Tigeot 		ref->object = item->object;
981dedbd3bSFrançois Tigeot 	}
991dedbd3bSFrançois Tigeot 
1001dedbd3bSFrançois Tigeot 	++item->refcount;
1012c9916cdSFrançois Tigeot 	mutex_unlock(&item->mutex);
1025718399fSFrançois Tigeot 	return 0;
1031dedbd3bSFrançois Tigeot 
1041dedbd3bSFrançois Tigeot error_free:
1051dedbd3bSFrançois Tigeot 	kfree(ref->object);
1061dedbd3bSFrançois Tigeot 	ref->object = NULL;
1071dedbd3bSFrançois Tigeot error_unlock:
1082c9916cdSFrançois Tigeot 	mutex_unlock(&item->mutex);
1095718399fSFrançois Tigeot 	return ret;
1105718399fSFrançois Tigeot }
1112c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_global_item_ref);
1125718399fSFrançois Tigeot 
113*a85cb24fSFrançois Tigeot /**
114*a85cb24fSFrançois Tigeot  * drm_global_item_unref - Drop reference to memory
115*a85cb24fSFrançois Tigeot  * object
116*a85cb24fSFrançois Tigeot  * @ref: Object being removed
117*a85cb24fSFrançois Tigeot  *
118*a85cb24fSFrançois Tigeot  * Drop a reference to the memory object and eventually call the
119*a85cb24fSFrançois Tigeot  * release() hook.  The allocated object should be dropped in the
120*a85cb24fSFrançois Tigeot  * release() hook or before calling this function
121*a85cb24fSFrançois Tigeot  *
122*a85cb24fSFrançois Tigeot  */
123*a85cb24fSFrançois Tigeot 
drm_global_item_unref(struct drm_global_reference * ref)1245718399fSFrançois Tigeot void drm_global_item_unref(struct drm_global_reference *ref)
1255718399fSFrançois Tigeot {
1265718399fSFrançois Tigeot 	struct drm_global_item *item = &glob[ref->global_type];
1275718399fSFrançois Tigeot 
1282c9916cdSFrançois Tigeot 	mutex_lock(&item->mutex);
1292c9916cdSFrançois Tigeot 	BUG_ON(item->refcount == 0);
1302c9916cdSFrançois Tigeot 	BUG_ON(ref->object != item->object);
1315718399fSFrançois Tigeot 	if (--item->refcount == 0) {
1325718399fSFrançois Tigeot 		ref->release(ref);
1335718399fSFrançois Tigeot 		item->object = NULL;
1345718399fSFrançois Tigeot 	}
1352c9916cdSFrançois Tigeot 	mutex_unlock(&item->mutex);
1365718399fSFrançois Tigeot }
1372c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_global_item_unref);
1382c9916cdSFrançois Tigeot 
139