drm_hashtab.c 5.2 KB

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