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 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/malloc.h>
38 #include <dev/drm2/drm_gem_names.h>
40 MALLOC_DEFINE(M_GEM_NAMES, "gem_name", "Hash headers for the gem names");
42 static void drm_gem_names_delete_name(struct drm_gem_names *names,
43 struct drm_gem_name *np);
46 drm_gem_names_init(struct drm_gem_names *names)
49 names->unr = new_unrhdr(1, INT_MAX, NULL); /* XXXKIB */
50 names->names_hash = hashinit(1000 /* XXXKIB */, M_GEM_NAMES,
52 mtx_init(&names->lock, "drmnames", NULL, MTX_DEF);
56 drm_gem_names_fini(struct drm_gem_names *names)
58 struct drm_gem_name *np;
61 mtx_lock(&names->lock);
62 for (i = 0; i <= names->hash_mask; i++) {
63 while ((np = LIST_FIRST(&names->names_hash[i])) != NULL) {
64 drm_gem_names_delete_name(names, np);
65 mtx_lock(&names->lock);
68 mtx_unlock(&names->lock);
69 mtx_destroy(&names->lock);
70 hashdestroy(names->names_hash, M_GEM_NAMES, names->hash_mask);
71 delete_unrhdr(names->unr);
74 static struct drm_gem_names_head *
75 gem_name_hash_index(struct drm_gem_names *names, int name)
78 return (&names->names_hash[name & names->hash_mask]);
82 drm_gem_name_ref(struct drm_gem_names *names, uint32_t name,
85 struct drm_gem_name *n;
87 mtx_lock(&names->lock);
88 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
89 if (n->name == name) {
92 mtx_unlock(&names->lock);
96 mtx_unlock(&names->lock);
100 struct drm_gem_ptr_match_arg {
106 drm_gem_ptr_match(uint32_t name, void *ptr, void *arg)
108 struct drm_gem_ptr_match_arg *a;
119 drm_gem_find_name(struct drm_gem_names *names, void *ptr)
121 struct drm_gem_ptr_match_arg arg;
125 drm_gem_names_foreach(names, drm_gem_ptr_match, &arg);
130 drm_gem_find_ptr(struct drm_gem_names *names, uint32_t name)
132 struct drm_gem_name *n;
135 mtx_lock(&names->lock);
136 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
137 if (n->name == name) {
139 mtx_unlock(&names->lock);
143 mtx_unlock(&names->lock);
148 drm_gem_name_create(struct drm_gem_names *names, void *p, uint32_t *name)
150 struct drm_gem_name *np;
156 np = malloc(sizeof(struct drm_gem_name), M_GEM_NAMES, M_WAITOK);
157 mtx_lock(&names->lock);
158 np->name = alloc_unr(names->unr);
159 if (np->name == -1) {
160 mtx_unlock(&names->lock);
161 free(np, M_GEM_NAMES);
166 LIST_INSERT_HEAD(gem_name_hash_index(names, np->name), np, link);
167 mtx_unlock(&names->lock);
172 drm_gem_names_delete_name(struct drm_gem_names *names, struct drm_gem_name *np)
175 mtx_assert(&names->lock, MA_OWNED);
176 LIST_REMOVE(np, link);
177 mtx_unlock(&names->lock);
178 free_unr(names->unr, np->name);
179 free(np, M_GEM_NAMES);
183 drm_gem_names_remove(struct drm_gem_names *names, uint32_t name)
185 struct drm_gem_name *n;
188 mtx_lock(&names->lock);
189 LIST_FOREACH(n, gem_name_hash_index(names, name), link) {
190 if (n->name == name) {
192 drm_gem_names_delete_name(names, n);
196 mtx_unlock(&names->lock);
201 drm_gem_names_foreach(struct drm_gem_names *names,
202 int (*f)(uint32_t, void *, void *), void *arg)
204 struct drm_gem_name *np;
205 struct drm_gem_name marker;
208 bzero(&marker, sizeof(marker));
210 mtx_lock(&names->lock);
211 for (i = 0; i <= names->hash_mask; i++) {
212 for (np = LIST_FIRST(&names->names_hash[i]); np != NULL; ) {
213 if (np->name == -1) {
214 np = LIST_NEXT(np, link);
217 LIST_INSERT_AFTER(np, &marker, link);
218 mtx_unlock(&names->lock);
219 fres = f(np->name, np->ptr, arg);
220 mtx_lock(&names->lock);
221 np = LIST_NEXT(&marker, link);
222 LIST_REMOVE(&marker, link);
227 mtx_unlock(&names->lock);