2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2011 The FreeBSD Foundation
6 * This software was developed by Konstantin Belousov under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
38 #include <sys/malloc.h>
40 #include <dev/drm2/drm_gem_names.h>
42 MALLOC_DEFINE(M_GEM_NAMES, "gem_name", "Hash headers for the gem names");
44 static void drm_gem_names_delete_name(struct drm_gem_names *names,
45 struct drm_gem_name *np);
48 drm_gem_names_init(struct drm_gem_names *names)
51 names->unr = new_unrhdr(1, INT_MAX, NULL); /* XXXKIB */
52 names->names_hash = hashinit(1000 /* XXXKIB */, M_GEM_NAMES,
54 mtx_init(&names->lock, "drmnames", NULL, MTX_DEF);
58 drm_gem_names_fini(struct drm_gem_names *names)
60 struct drm_gem_name *np;
63 mtx_lock(&names->lock);
64 for (i = 0; i <= names->hash_mask; i++) {
65 while ((np = LIST_FIRST(&names->names_hash[i])) != NULL) {
66 drm_gem_names_delete_name(names, np);
67 mtx_lock(&names->lock);
70 mtx_unlock(&names->lock);
71 mtx_destroy(&names->lock);
72 hashdestroy(names->names_hash, M_GEM_NAMES, names->hash_mask);
73 delete_unrhdr(names->unr);
76 static struct drm_gem_names_head *
77 gem_name_hash_index(struct drm_gem_names *names, int name)
80 return (&names->names_hash[name & names->hash_mask]);
84 drm_gem_name_ref(struct drm_gem_names *names, uint32_t name,
87 struct drm_gem_name *n;
89 mtx_lock(&names->lock);
90 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
91 if (n->name == name) {
94 mtx_unlock(&names->lock);
98 mtx_unlock(&names->lock);
102 struct drm_gem_ptr_match_arg {
108 drm_gem_ptr_match(uint32_t name, void *ptr, void *arg)
110 struct drm_gem_ptr_match_arg *a;
121 drm_gem_find_name(struct drm_gem_names *names, void *ptr)
123 struct drm_gem_ptr_match_arg arg;
127 drm_gem_names_foreach(names, drm_gem_ptr_match, &arg);
132 drm_gem_find_ptr(struct drm_gem_names *names, uint32_t name)
134 struct drm_gem_name *n;
137 mtx_lock(&names->lock);
138 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
139 if (n->name == name) {
141 mtx_unlock(&names->lock);
145 mtx_unlock(&names->lock);
150 drm_gem_name_create(struct drm_gem_names *names, void *p, uint32_t *name)
152 struct drm_gem_name *np;
158 np = malloc(sizeof(struct drm_gem_name), M_GEM_NAMES, M_WAITOK);
159 mtx_lock(&names->lock);
160 np->name = alloc_unr(names->unr);
161 if (np->name == -1) {
162 mtx_unlock(&names->lock);
163 free(np, M_GEM_NAMES);
168 LIST_INSERT_HEAD(gem_name_hash_index(names, np->name), np, link);
169 mtx_unlock(&names->lock);
174 drm_gem_names_delete_name(struct drm_gem_names *names, struct drm_gem_name *np)
177 mtx_assert(&names->lock, MA_OWNED);
178 LIST_REMOVE(np, link);
179 mtx_unlock(&names->lock);
180 free_unr(names->unr, np->name);
181 free(np, M_GEM_NAMES);
185 drm_gem_names_remove(struct drm_gem_names *names, uint32_t name)
187 struct drm_gem_name *n;
190 mtx_lock(&names->lock);
191 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
192 if (n->name == name) {
194 drm_gem_names_delete_name(names, n);
198 mtx_unlock(&names->lock);
203 drm_gem_names_foreach(struct drm_gem_names *names,
204 int (*f)(uint32_t, void *, void *), void *arg)
206 struct drm_gem_name *np;
207 struct drm_gem_name marker;
210 bzero(&marker, sizeof(marker));
212 mtx_lock(&names->lock);
213 for (i = 0; i <= names->hash_mask; i++) {
214 for (np = LIST_FIRST(&names->names_hash[i]); np != NULL; ) {
215 if (np->name == -1) {
216 np = LIST_NEXT(np, link);
219 LIST_INSERT_AFTER(np, &marker, link);
220 mtx_unlock(&names->lock);
221 fres = f(np->name, np->ptr, arg);
222 mtx_lock(&names->lock);
223 np = LIST_NEXT(&marker, link);
224 LIST_REMOVE(&marker, link);
229 mtx_unlock(&names->lock);