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/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/malloc.h>
37 #include <dev/drm2/drm_gem_names.h>
39 MALLOC_DEFINE(M_GEM_NAMES, "gem_name", "Hash headers for the gem names");
41 static void drm_gem_names_delete_name(struct drm_gem_names *names,
42 struct drm_gem_name *np);
45 drm_gem_names_init(struct drm_gem_names *names)
48 names->unr = new_unrhdr(1, INT_MAX, NULL); /* XXXKIB */
49 names->names_hash = hashinit(1000 /* XXXKIB */, M_GEM_NAMES,
51 mtx_init(&names->lock, "drmnames", NULL, MTX_DEF);
55 drm_gem_names_fini(struct drm_gem_names *names)
57 struct drm_gem_name *np;
60 mtx_lock(&names->lock);
61 for (i = 0; i <= names->hash_mask; i++) {
62 while ((np = LIST_FIRST(&names->names_hash[i])) != NULL) {
63 drm_gem_names_delete_name(names, np);
64 mtx_lock(&names->lock);
67 mtx_unlock(&names->lock);
68 mtx_destroy(&names->lock);
69 hashdestroy(names->names_hash, M_GEM_NAMES, names->hash_mask);
70 delete_unrhdr(names->unr);
73 static struct drm_gem_names_head *
74 gem_name_hash_index(struct drm_gem_names *names, int name)
77 return (&names->names_hash[name & names->hash_mask]);
81 drm_gem_name_ref(struct drm_gem_names *names, uint32_t name,
84 struct drm_gem_name *n;
86 mtx_lock(&names->lock);
87 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
88 if (n->name == name) {
91 mtx_unlock(&names->lock);
95 mtx_unlock(&names->lock);
99 struct drm_gem_ptr_match_arg {
105 drm_gem_ptr_match(uint32_t name, void *ptr, void *arg)
107 struct drm_gem_ptr_match_arg *a;
118 drm_gem_find_name(struct drm_gem_names *names, void *ptr)
120 struct drm_gem_ptr_match_arg arg;
124 drm_gem_names_foreach(names, drm_gem_ptr_match, &arg);
129 drm_gem_find_ptr(struct drm_gem_names *names, uint32_t name)
131 struct drm_gem_name *n;
134 mtx_lock(&names->lock);
135 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
136 if (n->name == name) {
138 mtx_unlock(&names->lock);
142 mtx_unlock(&names->lock);
147 drm_gem_name_create(struct drm_gem_names *names, void *p, uint32_t *name)
149 struct drm_gem_name *np;
155 np = malloc(sizeof(struct drm_gem_name), M_GEM_NAMES, M_WAITOK);
156 mtx_lock(&names->lock);
157 np->name = alloc_unr(names->unr);
158 if (np->name == -1) {
159 mtx_unlock(&names->lock);
160 free(np, M_GEM_NAMES);
165 LIST_INSERT_HEAD(gem_name_hash_index(names, np->name), np, link);
166 mtx_unlock(&names->lock);
171 drm_gem_names_delete_name(struct drm_gem_names *names, struct drm_gem_name *np)
174 mtx_assert(&names->lock, MA_OWNED);
175 LIST_REMOVE(np, link);
176 mtx_unlock(&names->lock);
177 free_unr(names->unr, np->name);
178 free(np, M_GEM_NAMES);
182 drm_gem_names_remove(struct drm_gem_names *names, uint32_t name)
184 struct drm_gem_name *n;
187 mtx_lock(&names->lock);
188 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
189 if (n->name == name) {
191 drm_gem_names_delete_name(names, n);
195 mtx_unlock(&names->lock);
200 drm_gem_names_foreach(struct drm_gem_names *names,
201 int (*f)(uint32_t, void *, void *), void *arg)
203 struct drm_gem_name *np;
204 struct drm_gem_name marker;
207 bzero(&marker, sizeof(marker));
209 mtx_lock(&names->lock);
210 for (i = 0; i <= names->hash_mask; i++) {
211 for (np = LIST_FIRST(&names->names_hash[i]); np != NULL; ) {
212 if (np->name == -1) {
213 np = LIST_NEXT(np, link);
216 LIST_INSERT_AFTER(np, &marker, link);
217 mtx_unlock(&names->lock);
218 fres = f(np->name, np->ptr, arg);
219 mtx_lock(&names->lock);
220 np = LIST_NEXT(&marker, link);
221 LIST_REMOVE(&marker, link);
226 mtx_unlock(&names->lock);