13f6063ccSDavid Shao /************************************************************************** 23f6063ccSDavid Shao * 33f6063ccSDavid Shao * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA. 43f6063ccSDavid Shao * All Rights Reserved. 53f6063ccSDavid Shao * 63f6063ccSDavid Shao * Permission is hereby granted, free of charge, to any person obtaining a 73f6063ccSDavid Shao * copy of this software and associated documentation files (the 83f6063ccSDavid Shao * "Software"), to deal in the Software without restriction, including 93f6063ccSDavid Shao * without limitation the rights to use, copy, modify, merge, publish, 103f6063ccSDavid Shao * distribute, sub license, and/or sell copies of the Software, and to 113f6063ccSDavid Shao * permit persons to whom the Software is furnished to do so, subject to 123f6063ccSDavid Shao * the following conditions: 133f6063ccSDavid Shao * 143f6063ccSDavid Shao * The above copyright notice and this permission notice (including the 153f6063ccSDavid Shao * next paragraph) shall be included in all copies or substantial portions 163f6063ccSDavid Shao * of the Software. 173f6063ccSDavid Shao * 183f6063ccSDavid Shao * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 193f6063ccSDavid Shao * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 203f6063ccSDavid Shao * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 213f6063ccSDavid Shao * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 223f6063ccSDavid Shao * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 233f6063ccSDavid Shao * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 243f6063ccSDavid Shao * USE OR OTHER DEALINGS IN THE SOFTWARE. 253f6063ccSDavid Shao * 263f6063ccSDavid Shao * 276c9c2933SSascha Wildner * $FreeBSD: src/sys/dev/drm/drm_hashtab.c,v 1.1 2010/01/31 14:25:29 rnoland Exp $ 283f6063ccSDavid Shao **************************************************************************/ 293f6063ccSDavid Shao 303f6063ccSDavid Shao /* 313f6063ccSDavid Shao * Simple open hash tab implementation. 323f6063ccSDavid Shao * 333f6063ccSDavid Shao * Authors: 343f6063ccSDavid Shao * Thomas Hellström <thomas-at-tungstengraphics-dot-com> 353f6063ccSDavid Shao */ 363f6063ccSDavid Shao 373f6063ccSDavid Shao #include <sys/hash.h> 386c9c2933SSascha Wildner 39*18e26a6dSFrançois Tigeot #include <drm/drmP.h> 40*18e26a6dSFrançois Tigeot #include <drm/drm_hashtab.h> 413f6063ccSDavid Shao 423f6063ccSDavid Shao int drm_ht_create(struct drm_open_hash *ht, unsigned int order) 433f6063ccSDavid Shao { 443f6063ccSDavid Shao ht->size = 1 << order; 453f6063ccSDavid Shao ht->order = order; 463f6063ccSDavid Shao ht->table = NULL; 476c9c2933SSascha Wildner ht->table = hashinit(ht->size, DRM_MEM_HASHTAB, &ht->mask); 483f6063ccSDavid Shao if (!ht->table) { 493f6063ccSDavid Shao DRM_ERROR("Out of memory for hash table\n"); 503f6063ccSDavid Shao return -ENOMEM; 513f6063ccSDavid Shao } 523f6063ccSDavid Shao return 0; 533f6063ccSDavid Shao } 543f6063ccSDavid Shao 553f6063ccSDavid Shao void drm_ht_verbose_list(struct drm_open_hash *ht, unsigned long key) 563f6063ccSDavid Shao { 573f6063ccSDavid Shao struct drm_hash_item *entry; 583f6063ccSDavid Shao struct drm_hash_item_list *h_list; 593f6063ccSDavid Shao unsigned int hashed_key; 603f6063ccSDavid Shao int count = 0; 613f6063ccSDavid Shao 623f6063ccSDavid Shao hashed_key = hash32_buf(&key, sizeof(key), ht->order); 633f6063ccSDavid Shao DRM_DEBUG("Key is 0x%08lx, Hashed key is 0x%08x\n", key, hashed_key); 643f6063ccSDavid Shao h_list = &ht->table[hashed_key & ht->mask]; 653f6063ccSDavid Shao LIST_FOREACH(entry, h_list, head) 663f6063ccSDavid Shao DRM_DEBUG("count %d, key: 0x%08lx\n", count++, entry->key); 673f6063ccSDavid Shao } 683f6063ccSDavid Shao 693f6063ccSDavid Shao static struct drm_hash_item * 703f6063ccSDavid Shao drm_ht_find_key(struct drm_open_hash *ht, unsigned long key) 713f6063ccSDavid Shao { 723f6063ccSDavid Shao struct drm_hash_item *entry; 733f6063ccSDavid Shao struct drm_hash_item_list *h_list; 743f6063ccSDavid Shao unsigned int hashed_key; 753f6063ccSDavid Shao 763f6063ccSDavid Shao hashed_key = hash32_buf(&key, sizeof(key), ht->order); 773f6063ccSDavid Shao h_list = &ht->table[hashed_key & ht->mask]; 783f6063ccSDavid Shao LIST_FOREACH(entry, h_list, head) { 793f6063ccSDavid Shao if (entry->key == key) 803f6063ccSDavid Shao return entry; 813f6063ccSDavid Shao if (entry->key > key) 823f6063ccSDavid Shao break; 833f6063ccSDavid Shao } 843f6063ccSDavid Shao return NULL; 853f6063ccSDavid Shao } 863f6063ccSDavid Shao 873f6063ccSDavid Shao 883f6063ccSDavid Shao int drm_ht_insert_item(struct drm_open_hash *ht, struct drm_hash_item *item) 893f6063ccSDavid Shao { 903f6063ccSDavid Shao struct drm_hash_item *entry, *parent; 913f6063ccSDavid Shao struct drm_hash_item_list *h_list; 923f6063ccSDavid Shao unsigned int hashed_key; 933f6063ccSDavid Shao unsigned long key = item->key; 943f6063ccSDavid Shao 953f6063ccSDavid Shao hashed_key = hash32_buf(&key, sizeof(key), ht->order); 963f6063ccSDavid Shao h_list = &ht->table[hashed_key & ht->mask]; 973f6063ccSDavid Shao parent = NULL; 983f6063ccSDavid Shao LIST_FOREACH(entry, h_list, head) { 993f6063ccSDavid Shao if (entry->key == key) 1003f6063ccSDavid Shao return -EINVAL; 1013f6063ccSDavid Shao if (entry->key > key) 1023f6063ccSDavid Shao break; 1033f6063ccSDavid Shao parent = entry; 1043f6063ccSDavid Shao } 1053f6063ccSDavid Shao if (parent) { 1063f6063ccSDavid Shao LIST_INSERT_AFTER(parent, item, head); 1073f6063ccSDavid Shao } else { 1083f6063ccSDavid Shao LIST_INSERT_HEAD(h_list, item, head); 1093f6063ccSDavid Shao } 1103f6063ccSDavid Shao return 0; 1113f6063ccSDavid Shao } 1123f6063ccSDavid Shao 1133f6063ccSDavid Shao /* 1143f6063ccSDavid Shao * Just insert an item and return any "bits" bit key that hasn't been 1153f6063ccSDavid Shao * used before. 1163f6063ccSDavid Shao */ 1173f6063ccSDavid Shao int drm_ht_just_insert_please(struct drm_open_hash *ht, struct drm_hash_item *item, 1183f6063ccSDavid Shao unsigned long seed, int bits, int shift, 1193f6063ccSDavid Shao unsigned long add) 1203f6063ccSDavid Shao { 1213f6063ccSDavid Shao int ret; 1223f6063ccSDavid Shao unsigned long mask = (1 << bits) - 1; 1233f6063ccSDavid Shao unsigned long first, unshifted_key = 0; 1243f6063ccSDavid Shao 1253f6063ccSDavid Shao unshifted_key = hash32_buf(&seed, sizeof(seed), unshifted_key); 1263f6063ccSDavid Shao first = unshifted_key; 1273f6063ccSDavid Shao do { 1283f6063ccSDavid Shao item->key = (unshifted_key << shift) + add; 1293f6063ccSDavid Shao ret = drm_ht_insert_item(ht, item); 1303f6063ccSDavid Shao if (ret) 1313f6063ccSDavid Shao unshifted_key = (unshifted_key + 1) & mask; 1323f6063ccSDavid Shao } while(ret && (unshifted_key != first)); 1333f6063ccSDavid Shao 1343f6063ccSDavid Shao if (ret) { 1353f6063ccSDavid Shao DRM_ERROR("Available key bit space exhausted\n"); 1363f6063ccSDavid Shao return -EINVAL; 1373f6063ccSDavid Shao } 1383f6063ccSDavid Shao return 0; 1393f6063ccSDavid Shao } 1403f6063ccSDavid Shao 1413f6063ccSDavid Shao int drm_ht_find_item(struct drm_open_hash *ht, unsigned long key, 1423f6063ccSDavid Shao struct drm_hash_item **item) 1433f6063ccSDavid Shao { 1443f6063ccSDavid Shao struct drm_hash_item *entry; 1453f6063ccSDavid Shao 1463f6063ccSDavid Shao entry = drm_ht_find_key(ht, key); 1473f6063ccSDavid Shao if (!entry) 1483f6063ccSDavid Shao return -EINVAL; 1493f6063ccSDavid Shao 1503f6063ccSDavid Shao *item = entry; 1513f6063ccSDavid Shao return 0; 1523f6063ccSDavid Shao } 1533f6063ccSDavid Shao 1543f6063ccSDavid Shao int drm_ht_remove_key(struct drm_open_hash *ht, unsigned long key) 1553f6063ccSDavid Shao { 1563f6063ccSDavid Shao struct drm_hash_item *entry; 1573f6063ccSDavid Shao 1583f6063ccSDavid Shao entry = drm_ht_find_key(ht, key); 1593f6063ccSDavid Shao if (entry) { 1603f6063ccSDavid Shao LIST_REMOVE(entry, head); 1613f6063ccSDavid Shao return 0; 1623f6063ccSDavid Shao } 1633f6063ccSDavid Shao return -EINVAL; 1643f6063ccSDavid Shao } 1653f6063ccSDavid Shao 1663f6063ccSDavid Shao int drm_ht_remove_item(struct drm_open_hash *ht, struct drm_hash_item *item) 1673f6063ccSDavid Shao { 1683f6063ccSDavid Shao LIST_REMOVE(item, head); 1693f6063ccSDavid Shao return 0; 1703f6063ccSDavid Shao } 1713f6063ccSDavid Shao 1723f6063ccSDavid Shao void drm_ht_remove(struct drm_open_hash *ht) 1733f6063ccSDavid Shao { 1743f6063ccSDavid Shao if (ht->table) { 175b1cf3cedSSascha Wildner hashdestroy(ht->table, DRM_MEM_HASHTAB, ht->mask); 1763f6063ccSDavid Shao ht->table = NULL; 1773f6063ccSDavid Shao } 1783f6063ccSDavid Shao } 179