2 * Copyright (c) 2011 The FreeBSD Foundation
5 * This software was developed by Konstantin Belousov under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/malloc.h>
39 #include <dev/drm2/drm_gem_names.h>
41 MALLOC_DEFINE(M_GEM_NAMES, "gem_name", "Hash headers for the gem names");
43 static void drm_gem_names_delete_name(struct drm_gem_names *names,
44 struct drm_gem_name *np);
47 drm_gem_names_init(struct drm_gem_names *names)
50 names->unr = new_unrhdr(1, INT_MAX, NULL); /* XXXKIB */
51 names->names_hash = hashinit(1000 /* XXXKIB */, M_GEM_NAMES,
53 mtx_init(&names->lock, "drmnames", NULL, MTX_DEF);
57 drm_gem_names_fini(struct drm_gem_names *names)
59 struct drm_gem_name *np;
62 mtx_lock(&names->lock);
63 for (i = 0; i <= names->hash_mask; i++) {
64 while ((np = LIST_FIRST(&names->names_hash[i])) != NULL) {
65 drm_gem_names_delete_name(names, np);
66 mtx_lock(&names->lock);
69 mtx_unlock(&names->lock);
70 mtx_destroy(&names->lock);
71 hashdestroy(names->names_hash, M_GEM_NAMES, names->hash_mask);
72 delete_unrhdr(names->unr);
75 static struct drm_gem_names_head *
76 gem_name_hash_index(struct drm_gem_names *names, int name)
79 return (&names->names_hash[name & names->hash_mask]);
83 drm_gem_name_ref(struct drm_gem_names *names, uint32_t name,
86 struct drm_gem_name *n;
88 mtx_lock(&names->lock);
89 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
90 if (n->name == name) {
93 mtx_unlock(&names->lock);
97 mtx_unlock(&names->lock);
101 struct drm_gem_ptr_match_arg {
107 drm_gem_ptr_match(uint32_t name, void *ptr, void *arg)
109 struct drm_gem_ptr_match_arg *a;
120 drm_gem_find_name(struct drm_gem_names *names, void *ptr)
122 struct drm_gem_ptr_match_arg arg;
126 drm_gem_names_foreach(names, drm_gem_ptr_match, &arg);
131 drm_gem_name_create(struct drm_gem_names *names, void *p, uint32_t *name)
133 struct drm_gem_name *np;
135 np = malloc(sizeof(struct drm_gem_name), M_GEM_NAMES, M_WAITOK);
136 mtx_lock(&names->lock);
138 mtx_unlock(&names->lock);
141 np->name = alloc_unr(names->unr);
142 if (np->name == -1) {
143 mtx_unlock(&names->lock);
144 free(np, M_GEM_NAMES);
149 LIST_INSERT_HEAD(gem_name_hash_index(names, np->name), np, link);
150 mtx_unlock(&names->lock);
155 drm_gem_names_delete_name(struct drm_gem_names *names, struct drm_gem_name *np)
158 mtx_assert(&names->lock, MA_OWNED);
159 LIST_REMOVE(np, link);
160 mtx_unlock(&names->lock);
161 free_unr(names->unr, np->name);
162 free(np, M_GEM_NAMES);
166 drm_gem_names_remove(struct drm_gem_names *names, uint32_t name)
168 struct drm_gem_name *n;
171 mtx_lock(&names->lock);
172 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
173 if (n->name == name) {
175 drm_gem_names_delete_name(names, n);
179 mtx_unlock(&names->lock);
184 drm_gem_names_foreach(struct drm_gem_names *names,
185 int (*f)(uint32_t, void *, void *), void *arg)
187 struct drm_gem_name *np;
188 struct drm_gem_name marker;
191 bzero(&marker, sizeof(marker));
193 mtx_lock(&names->lock);
194 for (i = 0; i <= names->hash_mask; i++) {
195 for (np = LIST_FIRST(&names->names_hash[i]); np != NULL; ) {
196 if (np->name == -1) {
197 np = LIST_NEXT(np, link);
200 LIST_INSERT_AFTER(np, &marker, link);
201 mtx_unlock(&names->lock);
202 fres = f(np->name, np->ptr, arg);
203 mtx_lock(&names->lock);
204 np = LIST_NEXT(&marker, link);
205 LIST_REMOVE(&marker, link);
210 mtx_unlock(&names->lock);