[go: nahoru, domu]

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