drm_hashtab.c revision 1.2 1 1.2 riastrad /* $NetBSD: drm_hashtab.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $ */
2 1.2 riastrad
3 1.1 riastrad /**************************************************************************
4 1.1 riastrad *
5 1.1 riastrad * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
6 1.1 riastrad * All Rights Reserved.
7 1.1 riastrad *
8 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
9 1.1 riastrad * copy of this software and associated documentation files (the
10 1.1 riastrad * "Software"), to deal in the Software without restriction, including
11 1.1 riastrad * without limitation the rights to use, copy, modify, merge, publish,
12 1.1 riastrad * distribute, sub license, and/or sell copies of the Software, and to
13 1.1 riastrad * permit persons to whom the Software is furnished to do so, subject to
14 1.1 riastrad * the following conditions:
15 1.1 riastrad *
16 1.1 riastrad * The above copyright notice and this permission notice (including the
17 1.1 riastrad * next paragraph) shall be included in all copies or substantial portions
18 1.1 riastrad * of the Software.
19 1.1 riastrad *
20 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 1.1 riastrad * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 1.1 riastrad * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 1.1 riastrad * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 1.1 riastrad * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 1.1 riastrad *
28 1.1 riastrad *
29 1.1 riastrad **************************************************************************/
30 1.1 riastrad /*
31 1.1 riastrad * Simple open hash tab implementation.
32 1.1 riastrad *
33 1.1 riastrad * Authors:
34 1.1 riastrad * Thomas Hellstrm <thomas-at-tungstengraphics-dot-com>
35 1.1 riastrad */
36 1.1 riastrad
37 1.2 riastrad #include <sys/cdefs.h>
38 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_hashtab.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $");
39 1.2 riastrad
40 1.1 riastrad #include <drm/drmP.h>
41 1.1 riastrad #include <drm/drm_hashtab.h>
42 1.1 riastrad #include <linux/hash.h>
43 1.1 riastrad #include <linux/slab.h>
44 1.1 riastrad #include <linux/export.h>
45 1.1 riastrad
46 1.1 riastrad int drm_ht_create(struct drm_open_hash *ht, unsigned int order)
47 1.1 riastrad {
48 1.1 riastrad unsigned int size = 1 << order;
49 1.1 riastrad
50 1.1 riastrad ht->order = order;
51 1.1 riastrad ht->table = NULL;
52 1.1 riastrad if (size <= PAGE_SIZE / sizeof(*ht->table))
53 1.1 riastrad ht->table = kcalloc(size, sizeof(*ht->table), GFP_KERNEL);
54 1.1 riastrad else
55 1.1 riastrad ht->table = vzalloc(size*sizeof(*ht->table));
56 1.1 riastrad if (!ht->table) {
57 1.1 riastrad DRM_ERROR("Out of memory for hash table\n");
58 1.1 riastrad return -ENOMEM;
59 1.1 riastrad }
60 1.1 riastrad return 0;
61 1.1 riastrad }
62 1.1 riastrad EXPORT_SYMBOL(drm_ht_create);
63 1.1 riastrad
64 1.1 riastrad void drm_ht_verbose_list(struct drm_open_hash *ht, unsigned long key)
65 1.1 riastrad {
66 1.1 riastrad struct drm_hash_item *entry;
67 1.1 riastrad struct hlist_head *h_list;
68 1.1 riastrad unsigned int hashed_key;
69 1.1 riastrad int count = 0;
70 1.1 riastrad
71 1.1 riastrad hashed_key = hash_long(key, ht->order);
72 1.1 riastrad DRM_DEBUG("Key is 0x%08lx, Hashed key is 0x%08x\n", key, hashed_key);
73 1.1 riastrad h_list = &ht->table[hashed_key];
74 1.2 riastrad hlist_for_each_entry(entry, h_list, head)
75 1.1 riastrad DRM_DEBUG("count %d, key: 0x%08lx\n", count++, entry->key);
76 1.1 riastrad }
77 1.1 riastrad
78 1.1 riastrad static struct hlist_node *drm_ht_find_key(struct drm_open_hash *ht,
79 1.1 riastrad unsigned long key)
80 1.1 riastrad {
81 1.1 riastrad struct drm_hash_item *entry;
82 1.1 riastrad struct hlist_head *h_list;
83 1.1 riastrad unsigned int hashed_key;
84 1.1 riastrad
85 1.1 riastrad hashed_key = hash_long(key, ht->order);
86 1.1 riastrad h_list = &ht->table[hashed_key];
87 1.2 riastrad hlist_for_each_entry(entry, h_list, head) {
88 1.1 riastrad if (entry->key == key)
89 1.2 riastrad return &entry->head;
90 1.1 riastrad if (entry->key > key)
91 1.1 riastrad break;
92 1.1 riastrad }
93 1.1 riastrad return NULL;
94 1.1 riastrad }
95 1.1 riastrad
96 1.1 riastrad static struct hlist_node *drm_ht_find_key_rcu(struct drm_open_hash *ht,
97 1.1 riastrad unsigned long key)
98 1.1 riastrad {
99 1.1 riastrad struct drm_hash_item *entry;
100 1.1 riastrad struct hlist_head *h_list;
101 1.1 riastrad unsigned int hashed_key;
102 1.1 riastrad
103 1.1 riastrad hashed_key = hash_long(key, ht->order);
104 1.1 riastrad h_list = &ht->table[hashed_key];
105 1.2 riastrad hlist_for_each_entry_rcu(entry, h_list, head) {
106 1.1 riastrad if (entry->key == key)
107 1.2 riastrad return &entry->head;
108 1.1 riastrad if (entry->key > key)
109 1.1 riastrad break;
110 1.1 riastrad }
111 1.1 riastrad return NULL;
112 1.1 riastrad }
113 1.1 riastrad
114 1.1 riastrad int drm_ht_insert_item(struct drm_open_hash *ht, struct drm_hash_item *item)
115 1.1 riastrad {
116 1.1 riastrad struct drm_hash_item *entry;
117 1.1 riastrad struct hlist_head *h_list;
118 1.2 riastrad struct hlist_node *parent;
119 1.1 riastrad unsigned int hashed_key;
120 1.1 riastrad unsigned long key = item->key;
121 1.1 riastrad
122 1.1 riastrad hashed_key = hash_long(key, ht->order);
123 1.1 riastrad h_list = &ht->table[hashed_key];
124 1.1 riastrad parent = NULL;
125 1.2 riastrad hlist_for_each_entry(entry, h_list, head) {
126 1.1 riastrad if (entry->key == key)
127 1.1 riastrad return -EINVAL;
128 1.1 riastrad if (entry->key > key)
129 1.1 riastrad break;
130 1.2 riastrad parent = &entry->head;
131 1.1 riastrad }
132 1.1 riastrad if (parent) {
133 1.2 riastrad hlist_add_behind_rcu(&item->head, parent);
134 1.1 riastrad } else {
135 1.1 riastrad hlist_add_head_rcu(&item->head, h_list);
136 1.1 riastrad }
137 1.1 riastrad return 0;
138 1.1 riastrad }
139 1.1 riastrad EXPORT_SYMBOL(drm_ht_insert_item);
140 1.1 riastrad
141 1.1 riastrad /*
142 1.1 riastrad * Just insert an item and return any "bits" bit key that hasn't been
143 1.1 riastrad * used before.
144 1.1 riastrad */
145 1.1 riastrad int drm_ht_just_insert_please(struct drm_open_hash *ht, struct drm_hash_item *item,
146 1.1 riastrad unsigned long seed, int bits, int shift,
147 1.1 riastrad unsigned long add)
148 1.1 riastrad {
149 1.1 riastrad int ret;
150 1.1 riastrad unsigned long mask = (1 << bits) - 1;
151 1.1 riastrad unsigned long first, unshifted_key;
152 1.1 riastrad
153 1.1 riastrad unshifted_key = hash_long(seed, bits);
154 1.1 riastrad first = unshifted_key;
155 1.1 riastrad do {
156 1.1 riastrad item->key = (unshifted_key << shift) + add;
157 1.1 riastrad ret = drm_ht_insert_item(ht, item);
158 1.1 riastrad if (ret)
159 1.1 riastrad unshifted_key = (unshifted_key + 1) & mask;
160 1.1 riastrad } while(ret && (unshifted_key != first));
161 1.1 riastrad
162 1.1 riastrad if (ret) {
163 1.1 riastrad DRM_ERROR("Available key bit space exhausted\n");
164 1.1 riastrad return -EINVAL;
165 1.1 riastrad }
166 1.1 riastrad return 0;
167 1.1 riastrad }
168 1.1 riastrad EXPORT_SYMBOL(drm_ht_just_insert_please);
169 1.1 riastrad
170 1.1 riastrad int drm_ht_find_item(struct drm_open_hash *ht, unsigned long key,
171 1.1 riastrad struct drm_hash_item **item)
172 1.1 riastrad {
173 1.1 riastrad struct hlist_node *list;
174 1.1 riastrad
175 1.1 riastrad list = drm_ht_find_key_rcu(ht, key);
176 1.1 riastrad if (!list)
177 1.1 riastrad return -EINVAL;
178 1.1 riastrad
179 1.1 riastrad *item = hlist_entry(list, struct drm_hash_item, head);
180 1.1 riastrad return 0;
181 1.1 riastrad }
182 1.1 riastrad EXPORT_SYMBOL(drm_ht_find_item);
183 1.1 riastrad
184 1.1 riastrad int drm_ht_remove_key(struct drm_open_hash *ht, unsigned long key)
185 1.1 riastrad {
186 1.1 riastrad struct hlist_node *list;
187 1.1 riastrad
188 1.1 riastrad list = drm_ht_find_key(ht, key);
189 1.1 riastrad if (list) {
190 1.1 riastrad hlist_del_init_rcu(list);
191 1.1 riastrad return 0;
192 1.1 riastrad }
193 1.1 riastrad return -EINVAL;
194 1.1 riastrad }
195 1.1 riastrad
196 1.1 riastrad int drm_ht_remove_item(struct drm_open_hash *ht, struct drm_hash_item *item)
197 1.1 riastrad {
198 1.1 riastrad hlist_del_init_rcu(&item->head);
199 1.1 riastrad return 0;
200 1.1 riastrad }
201 1.1 riastrad EXPORT_SYMBOL(drm_ht_remove_item);
202 1.1 riastrad
203 1.1 riastrad void drm_ht_remove(struct drm_open_hash *ht)
204 1.1 riastrad {
205 1.1 riastrad if (ht->table) {
206 1.1 riastrad if ((PAGE_SIZE / sizeof(*ht->table)) >> ht->order)
207 1.1 riastrad kfree(ht->table);
208 1.1 riastrad else
209 1.1 riastrad vfree(ht->table);
210 1.1 riastrad ht->table = NULL;
211 1.1 riastrad }
212 1.1 riastrad }
213 1.1 riastrad EXPORT_SYMBOL(drm_ht_remove);
214