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