Line data Source code
1 : /* $OpenBSD: drm_global.c,v 1.3 2015/09/23 23:12:11 kettenis Exp $ */
2 : /**************************************************************************
3 : *
4 : * Copyright 2008-2009 VMware, Inc., Palo Alto, CA., USA
5 : * All Rights Reserved.
6 : *
7 : * Permission is hereby granted, free of charge, to any person obtaining a
8 : * copy of this software and associated documentation files (the
9 : * "Software"), to deal in the Software without restriction, including
10 : * without limitation the rights to use, copy, modify, merge, publish,
11 : * distribute, sub license, and/or sell copies of the Software, and to
12 : * permit persons to whom the Software is furnished to do so, subject to
13 : * the following conditions:
14 : *
15 : * The above copyright notice and this permission notice (including the
16 : * next paragraph) shall be included in all copies or substantial portions
17 : * of the Software.
18 : *
19 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 : * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 : * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 : * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 : * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 : *
27 : **************************************************************************/
28 : /*
29 : * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 : */
31 :
32 : #include <dev/pci/drm/drmP.h>
33 : #include <dev/pci/drm/drm_global.h>
34 : #include <sys/rwlock.h>
35 :
36 : struct drm_global_item {
37 : struct rwlock mutex;
38 : void *object;
39 : int refcount;
40 : };
41 :
42 : static struct drm_global_item glob[DRM_GLOBAL_NUM];
43 :
44 0 : void drm_global_init(void)
45 : {
46 : int i;
47 :
48 0 : for (i = 0; i < DRM_GLOBAL_NUM; ++i) {
49 0 : struct drm_global_item *item = &glob[i];
50 0 : rw_init(&item->mutex, "gblitm");
51 0 : item->object = NULL;
52 0 : item->refcount = 0;
53 : }
54 0 : }
55 :
56 0 : void drm_global_release(void)
57 : {
58 : int i;
59 0 : for (i = 0; i < DRM_GLOBAL_NUM; ++i) {
60 0 : struct drm_global_item *item = &glob[i];
61 0 : BUG_ON(item->object != NULL);
62 0 : BUG_ON(item->refcount != 0);
63 : }
64 0 : }
65 :
66 0 : int drm_global_item_ref(struct drm_global_reference *ref)
67 : {
68 : int ret;
69 0 : struct drm_global_item *item = &glob[ref->global_type];
70 :
71 0 : mutex_lock(&item->mutex);
72 0 : if (item->refcount == 0) {
73 0 : item->object = kzalloc(ref->size, GFP_KERNEL);
74 0 : if (unlikely(item->object == NULL)) {
75 : ret = -ENOMEM;
76 0 : goto out_err;
77 : }
78 :
79 0 : ref->object = item->object;
80 0 : ret = ref->init(ref);
81 0 : if (unlikely(ret != 0))
82 : goto out_err;
83 :
84 : }
85 0 : ++item->refcount;
86 0 : ref->object = item->object;
87 0 : mutex_unlock(&item->mutex);
88 0 : return 0;
89 : out_err:
90 0 : mutex_unlock(&item->mutex);
91 0 : item->object = NULL;
92 0 : return ret;
93 0 : }
94 : EXPORT_SYMBOL(drm_global_item_ref);
95 :
96 0 : void drm_global_item_unref(struct drm_global_reference *ref)
97 : {
98 0 : struct drm_global_item *item = &glob[ref->global_type];
99 :
100 0 : mutex_lock(&item->mutex);
101 0 : BUG_ON(item->refcount == 0);
102 0 : BUG_ON(ref->object != item->object);
103 0 : if (--item->refcount == 0) {
104 0 : ref->release(ref);
105 0 : item->object = NULL;
106 0 : }
107 0 : mutex_unlock(&item->mutex);
108 0 : }
109 : EXPORT_SYMBOL(drm_global_item_unref);
110 :
|