11099013bSjsg /**************************************************************************
21099013bSjsg *
31099013bSjsg * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
41099013bSjsg * All Rights Reserved.
51099013bSjsg *
61099013bSjsg * Permission is hereby granted, free of charge, to any person obtaining a
71099013bSjsg * copy of this software and associated documentation files (the
81099013bSjsg * "Software"), to deal in the Software without restriction, including
91099013bSjsg * without limitation the rights to use, copy, modify, merge, publish,
101099013bSjsg * distribute, sub license, and/or sell copies of the Software, and to
111099013bSjsg * permit persons to whom the Software is furnished to do so, subject to
121099013bSjsg * the following conditions:
131099013bSjsg *
141099013bSjsg * The above copyright notice and this permission notice (including the
151099013bSjsg * next paragraph) shall be included in all copies or substantial portions
161099013bSjsg * of the Software.
171099013bSjsg *
181099013bSjsg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
191099013bSjsg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
201099013bSjsg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
211099013bSjsg * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
221099013bSjsg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
231099013bSjsg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
241099013bSjsg * USE OR OTHER DEALINGS IN THE SOFTWARE.
251099013bSjsg *
261099013bSjsg *
271099013bSjsg **************************************************************************/
281099013bSjsg /*
291099013bSjsg * Simple open hash tab implementation.
301099013bSjsg *
311099013bSjsg * Authors:
321099013bSjsg * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
331099013bSjsg */
341099013bSjsg
35c349dbc7Sjsg #include <linux/hash.h>
36c349dbc7Sjsg #include <linux/mm.h>
37c349dbc7Sjsg #include <linux/rculist.h>
38c349dbc7Sjsg #include <linux/slab.h>
39c349dbc7Sjsg #include <linux/vmalloc.h>
401099013bSjsg
41c349dbc7Sjsg #include <drm/drm_print.h>
421099013bSjsg
43*1bb76ff1Sjsg #include "drm_legacy.h"
44*1bb76ff1Sjsg
drm_ht_create(struct drm_open_hash * ht,unsigned int order)451099013bSjsg int drm_ht_create(struct drm_open_hash *ht, unsigned int order)
461099013bSjsg {
471099013bSjsg unsigned int size = 1 << order;
481099013bSjsg
491099013bSjsg ht->order = order;
501099013bSjsg ht->table = NULL;
511099013bSjsg if (size <= PAGE_SIZE / sizeof(*ht->table))
52a070f0ccSjsg ht->table = kcalloc(size, sizeof(*ht->table), GFP_KERNEL);
531099013bSjsg else
54c349dbc7Sjsg ht->table = vzalloc(array_size(size, sizeof(*ht->table)));
551099013bSjsg if (!ht->table) {
561099013bSjsg DRM_ERROR("Out of memory for hash table\n");
571099013bSjsg return -ENOMEM;
581099013bSjsg }
591099013bSjsg return 0;
601099013bSjsg }
611099013bSjsg
drm_ht_verbose_list(struct drm_open_hash * ht,unsigned long key)621099013bSjsg void drm_ht_verbose_list(struct drm_open_hash *ht, unsigned long key)
631099013bSjsg {
641099013bSjsg struct drm_hash_item *entry;
651099013bSjsg struct hlist_head *h_list;
661099013bSjsg unsigned int hashed_key;
671099013bSjsg int count = 0;
681099013bSjsg
691099013bSjsg hashed_key = hash_long(key, ht->order);
701099013bSjsg DRM_DEBUG("Key is 0x%08lx, Hashed key is 0x%08x\n", key, hashed_key);
711099013bSjsg h_list = &ht->table[hashed_key];
72e1001332Skettenis hlist_for_each_entry(entry, h_list, head)
731099013bSjsg DRM_DEBUG("count %d, key: 0x%08lx\n", count++, entry->key);
741099013bSjsg }
751099013bSjsg
76c349dbc7Sjsg #ifdef notyet
drm_ht_find_key(struct drm_open_hash * ht,unsigned long key)77c349dbc7Sjsg static struct hlist_node *drm_ht_find_key(struct drm_open_hash *ht,
781099013bSjsg unsigned long key)
791099013bSjsg {
801099013bSjsg struct drm_hash_item *entry;
811099013bSjsg struct hlist_head *h_list;
821099013bSjsg unsigned int hashed_key;
831099013bSjsg
841099013bSjsg hashed_key = hash_long(key, ht->order);
851099013bSjsg h_list = &ht->table[hashed_key];
86e1001332Skettenis hlist_for_each_entry(entry, h_list, head) {
871099013bSjsg if (entry->key == key)
88e1001332Skettenis return &entry->head;
891099013bSjsg if (entry->key > key)
901099013bSjsg break;
911099013bSjsg }
921099013bSjsg return NULL;
931099013bSjsg }
94c349dbc7Sjsg #endif
951099013bSjsg
drm_ht_find_key_rcu(struct drm_open_hash * ht,unsigned long key)96c349dbc7Sjsg static struct hlist_node *drm_ht_find_key_rcu(struct drm_open_hash *ht,
971099013bSjsg unsigned long key)
981099013bSjsg {
99c349dbc7Sjsg STUB();
1001099013bSjsg return NULL;
1011099013bSjsg #ifdef notyet
1021099013bSjsg struct drm_hash_item *entry;
1031099013bSjsg struct hlist_head *h_list;
1041099013bSjsg unsigned int hashed_key;
1051099013bSjsg
1061099013bSjsg hashed_key = hash_long(key, ht->order);
1071099013bSjsg h_list = &ht->table[hashed_key];
108e1001332Skettenis hlist_for_each_entry_rcu(entry, h_list, head) {
1091099013bSjsg if (entry->key == key)
110e1001332Skettenis return &entry->head;
1111099013bSjsg if (entry->key > key)
1121099013bSjsg break;
1131099013bSjsg }
1141099013bSjsg return NULL;
1151099013bSjsg #endif
1161099013bSjsg }
1171099013bSjsg
drm_ht_insert_item(struct drm_open_hash * ht,struct drm_hash_item * item)1181099013bSjsg int drm_ht_insert_item(struct drm_open_hash *ht, struct drm_hash_item *item)
1191099013bSjsg {
120c349dbc7Sjsg STUB();
1211099013bSjsg return -ENOSYS;
1221099013bSjsg #ifdef notyet
1231099013bSjsg struct drm_hash_item *entry;
1241099013bSjsg struct hlist_head *h_list;
125e1001332Skettenis struct hlist_node *parent;
1261099013bSjsg unsigned int hashed_key;
1271099013bSjsg unsigned long key = item->key;
1281099013bSjsg
1291099013bSjsg hashed_key = hash_long(key, ht->order);
1301099013bSjsg h_list = &ht->table[hashed_key];
1311099013bSjsg parent = NULL;
132e1001332Skettenis hlist_for_each_entry(entry, h_list, head) {
1331099013bSjsg if (entry->key == key)
1341099013bSjsg return -EINVAL;
1351099013bSjsg if (entry->key > key)
1361099013bSjsg break;
137e1001332Skettenis parent = &entry->head;
1381099013bSjsg }
1391099013bSjsg if (parent) {
140c349dbc7Sjsg hlist_add_behind_rcu(&item->head, parent);
1411099013bSjsg } else {
1421099013bSjsg hlist_add_head_rcu(&item->head, h_list);
1431099013bSjsg }
1441099013bSjsg return 0;
1451099013bSjsg #endif
1461099013bSjsg }
1471099013bSjsg
1481099013bSjsg /*
1491099013bSjsg * Just insert an item and return any "bits" bit key that hasn't been
1501099013bSjsg * used before.
1511099013bSjsg */
drm_ht_just_insert_please(struct drm_open_hash * ht,struct drm_hash_item * item,unsigned long seed,int bits,int shift,unsigned long add)1521099013bSjsg int drm_ht_just_insert_please(struct drm_open_hash *ht, struct drm_hash_item *item,
1531099013bSjsg unsigned long seed, int bits, int shift,
1541099013bSjsg unsigned long add)
1551099013bSjsg {
1561099013bSjsg int ret;
157c349dbc7Sjsg unsigned long mask = (1UL << bits) - 1;
1581099013bSjsg unsigned long first, unshifted_key;
1591099013bSjsg
1601099013bSjsg unshifted_key = hash_long(seed, bits);
1611099013bSjsg first = unshifted_key;
1621099013bSjsg do {
1631099013bSjsg item->key = (unshifted_key << shift) + add;
1641099013bSjsg ret = drm_ht_insert_item(ht, item);
1651099013bSjsg if (ret)
1661099013bSjsg unshifted_key = (unshifted_key + 1) & mask;
1671099013bSjsg } while(ret && (unshifted_key != first));
1681099013bSjsg
1691099013bSjsg if (ret) {
1701099013bSjsg DRM_ERROR("Available key bit space exhausted\n");
1711099013bSjsg return -EINVAL;
1721099013bSjsg }
1731099013bSjsg return 0;
1741099013bSjsg }
1751099013bSjsg
drm_ht_find_item(struct drm_open_hash * ht,unsigned long key,struct drm_hash_item ** item)1761099013bSjsg int drm_ht_find_item(struct drm_open_hash *ht, unsigned long key,
1771099013bSjsg struct drm_hash_item **item)
1781099013bSjsg {
1791099013bSjsg struct hlist_node *list;
1801099013bSjsg
1811099013bSjsg list = drm_ht_find_key_rcu(ht, key);
1821099013bSjsg if (!list)
1831099013bSjsg return -EINVAL;
1841099013bSjsg
1851099013bSjsg *item = hlist_entry(list, struct drm_hash_item, head);
1861099013bSjsg return 0;
1871099013bSjsg }
1881099013bSjsg
drm_ht_remove_key(struct drm_open_hash * ht,unsigned long key)1891099013bSjsg int drm_ht_remove_key(struct drm_open_hash *ht, unsigned long key)
1901099013bSjsg {
191c349dbc7Sjsg STUB();
1921099013bSjsg return -ENOSYS;
1931099013bSjsg #ifdef notyet
1941099013bSjsg struct hlist_node *list;
1951099013bSjsg
1961099013bSjsg list = drm_ht_find_key(ht, key);
1971099013bSjsg if (list) {
1981099013bSjsg hlist_del_init_rcu(list);
1991099013bSjsg return 0;
2001099013bSjsg }
2011099013bSjsg return -EINVAL;
2021099013bSjsg #endif
2031099013bSjsg }
2041099013bSjsg
drm_ht_remove_item(struct drm_open_hash * ht,struct drm_hash_item * item)2051099013bSjsg int drm_ht_remove_item(struct drm_open_hash *ht, struct drm_hash_item *item)
2061099013bSjsg {
207c349dbc7Sjsg STUB();
2081099013bSjsg return -ENOSYS;
2091099013bSjsg #ifdef notyet
2101099013bSjsg hlist_del_init_rcu(&item->head);
2111099013bSjsg return 0;
2121099013bSjsg #endif
2131099013bSjsg }
2141099013bSjsg
drm_ht_remove(struct drm_open_hash * ht)2151099013bSjsg void drm_ht_remove(struct drm_open_hash *ht)
2161099013bSjsg {
2171099013bSjsg if (ht->table) {
218c349dbc7Sjsg kvfree(ht->table);
2191099013bSjsg ht->table = NULL;
2201099013bSjsg }
2211099013bSjsg }
222