drm_mm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. * Generic simple memory manager implementation. Intended to be used as a base
  30. * class implementation for more advanced memory managers.
  31. *
  32. * Note that the algorithm used is quite simple and there might be substantial
  33. * performance gains if a smarter free list is implemented. Currently it is just an
  34. * unordered stack of free regions. This could easily be improved if an RB-tree
  35. * is used instead. At least if we expect heavy fragmentation.
  36. *
  37. * Aligned allocations can also see improvement.
  38. *
  39. * Authors:
  40. * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
  41. */
  42. #include "drmP.h"
  43. #include "drm_mm.h"
  44. #include <linux/slab.h>
  45. #define MM_UNUSED_TARGET 4
  46. unsigned long drm_mm_tail_space(struct drm_mm *mm)
  47. {
  48. struct list_head *tail_node;
  49. struct drm_mm_node *entry;
  50. tail_node = mm->ml_entry.prev;
  51. entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
  52. if (!entry->free)
  53. return 0;
  54. return entry->size;
  55. }
  56. int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size)
  57. {
  58. struct list_head *tail_node;
  59. struct drm_mm_node *entry;
  60. tail_node = mm->ml_entry.prev;
  61. entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
  62. if (!entry->free)
  63. return -ENOMEM;
  64. if (entry->size <= size)
  65. return -ENOMEM;
  66. entry->size -= size;
  67. return 0;
  68. }
  69. static struct drm_mm_node *drm_mm_kmalloc(struct drm_mm *mm, int atomic)
  70. {
  71. struct drm_mm_node *child;
  72. if (atomic)
  73. child = kmalloc(sizeof(*child), GFP_ATOMIC);
  74. else
  75. child = kmalloc(sizeof(*child), GFP_KERNEL);
  76. if (unlikely(child == NULL)) {
  77. spin_lock(&mm->unused_lock);
  78. if (list_empty(&mm->unused_nodes))
  79. child = NULL;
  80. else {
  81. child =
  82. list_entry(mm->unused_nodes.next,
  83. struct drm_mm_node, fl_entry);
  84. list_del(&child->fl_entry);
  85. --mm->num_unused;
  86. }
  87. spin_unlock(&mm->unused_lock);
  88. }
  89. return child;
  90. }
  91. int drm_mm_pre_get(struct drm_mm *mm)
  92. {
  93. struct drm_mm_node *node;
  94. spin_lock(&mm->unused_lock);
  95. while (mm->num_unused < MM_UNUSED_TARGET) {
  96. spin_unlock(&mm->unused_lock);
  97. node = kmalloc(sizeof(*node), GFP_KERNEL);
  98. spin_lock(&mm->unused_lock);
  99. if (unlikely(node == NULL)) {
  100. int ret = (mm->num_unused < 2) ? -ENOMEM : 0;
  101. spin_unlock(&mm->unused_lock);
  102. return ret;
  103. }
  104. ++mm->num_unused;
  105. list_add_tail(&node->fl_entry, &mm->unused_nodes);
  106. }
  107. spin_unlock(&mm->unused_lock);
  108. return 0;
  109. }
  110. EXPORT_SYMBOL(drm_mm_pre_get);
  111. static int drm_mm_create_tail_node(struct drm_mm *mm,
  112. unsigned long start,
  113. unsigned long size, int atomic)
  114. {
  115. struct drm_mm_node *child;
  116. child = drm_mm_kmalloc(mm, atomic);
  117. if (unlikely(child == NULL))
  118. return -ENOMEM;
  119. child->free = 1;
  120. child->size = size;
  121. child->start = start;
  122. child->mm = mm;
  123. list_add_tail(&child->ml_entry, &mm->ml_entry);
  124. list_add_tail(&child->fl_entry, &mm->fl_entry);
  125. return 0;
  126. }
  127. int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size, int atomic)
  128. {
  129. struct list_head *tail_node;
  130. struct drm_mm_node *entry;
  131. tail_node = mm->ml_entry.prev;
  132. entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
  133. if (!entry->free) {
  134. return drm_mm_create_tail_node(mm, entry->start + entry->size,
  135. size, atomic);
  136. }
  137. entry->size += size;
  138. return 0;
  139. }
  140. static struct drm_mm_node *drm_mm_split_at_start(struct drm_mm_node *parent,
  141. unsigned long size,
  142. int atomic)
  143. {
  144. struct drm_mm_node *child;
  145. child = drm_mm_kmalloc(parent->mm, atomic);
  146. if (unlikely(child == NULL))
  147. return NULL;
  148. INIT_LIST_HEAD(&child->fl_entry);
  149. child->free = 0;
  150. child->size = size;
  151. child->start = parent->start;
  152. child->mm = parent->mm;
  153. list_add_tail(&child->ml_entry, &parent->ml_entry);
  154. INIT_LIST_HEAD(&child->fl_entry);
  155. parent->size -= size;
  156. parent->start += size;
  157. return child;
  158. }
  159. struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
  160. unsigned long size,
  161. unsigned alignment,
  162. int atomic)
  163. {
  164. struct drm_mm_node *align_splitoff = NULL;
  165. unsigned tmp = 0;
  166. if (alignment)
  167. tmp = node->start % alignment;
  168. if (tmp) {
  169. align_splitoff =
  170. drm_mm_split_at_start(node, alignment - tmp, atomic);
  171. if (unlikely(align_splitoff == NULL))
  172. return NULL;
  173. }
  174. if (node->size == size) {
  175. list_del_init(&node->fl_entry);
  176. node->free = 0;
  177. } else {
  178. node = drm_mm_split_at_start(node, size, atomic);
  179. }
  180. if (align_splitoff)
  181. drm_mm_put_block(align_splitoff);
  182. return node;
  183. }
  184. EXPORT_SYMBOL(drm_mm_get_block_generic);
  185. /*
  186. * Put a block. Merge with the previous and / or next block if they are free.
  187. * Otherwise add to the free stack.
  188. */
  189. void drm_mm_put_block(struct drm_mm_node *cur)
  190. {
  191. struct drm_mm *mm = cur->mm;
  192. struct list_head *cur_head = &cur->ml_entry;
  193. struct list_head *root_head = &mm->ml_entry;
  194. struct drm_mm_node *prev_node = NULL;
  195. struct drm_mm_node *next_node;
  196. int merged = 0;
  197. if (cur_head->prev != root_head) {
  198. prev_node =
  199. list_entry(cur_head->prev, struct drm_mm_node, ml_entry);
  200. if (prev_node->free) {
  201. prev_node->size += cur->size;
  202. merged = 1;
  203. }
  204. }
  205. if (cur_head->next != root_head) {
  206. next_node =
  207. list_entry(cur_head->next, struct drm_mm_node, ml_entry);
  208. if (next_node->free) {
  209. if (merged) {
  210. prev_node->size += next_node->size;
  211. list_del(&next_node->ml_entry);
  212. list_del(&next_node->fl_entry);
  213. if (mm->num_unused < MM_UNUSED_TARGET) {
  214. list_add(&next_node->fl_entry,
  215. &mm->unused_nodes);
  216. ++mm->num_unused;
  217. } else
  218. kfree(next_node);
  219. } else {
  220. next_node->size += cur->size;
  221. next_node->start = cur->start;
  222. merged = 1;
  223. }
  224. }
  225. }
  226. if (!merged) {
  227. cur->free = 1;
  228. list_add(&cur->fl_entry, &mm->fl_entry);
  229. } else {
  230. list_del(&cur->ml_entry);
  231. if (mm->num_unused < MM_UNUSED_TARGET) {
  232. list_add(&cur->fl_entry, &mm->unused_nodes);
  233. ++mm->num_unused;
  234. } else
  235. kfree(cur);
  236. }
  237. }
  238. EXPORT_SYMBOL(drm_mm_put_block);
  239. struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
  240. unsigned long size,
  241. unsigned alignment, int best_match)
  242. {
  243. struct list_head *list;
  244. const struct list_head *free_stack = &mm->fl_entry;
  245. struct drm_mm_node *entry;
  246. struct drm_mm_node *best;
  247. unsigned long best_size;
  248. unsigned wasted;
  249. best = NULL;
  250. best_size = ~0UL;
  251. list_for_each(list, free_stack) {
  252. entry = list_entry(list, struct drm_mm_node, fl_entry);
  253. wasted = 0;
  254. if (entry->size < size)
  255. continue;
  256. if (alignment) {
  257. register unsigned tmp = entry->start % alignment;
  258. if (tmp)
  259. wasted += alignment - tmp;
  260. }
  261. if (entry->size >= size + wasted) {
  262. if (!best_match)
  263. return entry;
  264. if (size < best_size) {
  265. best = entry;
  266. best_size = entry->size;
  267. }
  268. }
  269. }
  270. return best;
  271. }
  272. EXPORT_SYMBOL(drm_mm_search_free);
  273. int drm_mm_clean(struct drm_mm * mm)
  274. {
  275. struct list_head *head = &mm->ml_entry;
  276. return (head->next->next == head);
  277. }
  278. EXPORT_SYMBOL(drm_mm_clean);
  279. int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
  280. {
  281. INIT_LIST_HEAD(&mm->ml_entry);
  282. INIT_LIST_HEAD(&mm->fl_entry);
  283. INIT_LIST_HEAD(&mm->unused_nodes);
  284. mm->num_unused = 0;
  285. spin_lock_init(&mm->unused_lock);
  286. return drm_mm_create_tail_node(mm, start, size, 0);
  287. }
  288. EXPORT_SYMBOL(drm_mm_init);
  289. void drm_mm_takedown(struct drm_mm * mm)
  290. {
  291. struct list_head *bnode = mm->fl_entry.next;
  292. struct drm_mm_node *entry;
  293. struct drm_mm_node *next;
  294. entry = list_entry(bnode, struct drm_mm_node, fl_entry);
  295. if (entry->ml_entry.next != &mm->ml_entry ||
  296. entry->fl_entry.next != &mm->fl_entry) {
  297. DRM_ERROR("Memory manager not clean. Delaying takedown\n");
  298. return;
  299. }
  300. list_del(&entry->fl_entry);
  301. list_del(&entry->ml_entry);
  302. kfree(entry);
  303. spin_lock(&mm->unused_lock);
  304. list_for_each_entry_safe(entry, next, &mm->unused_nodes, fl_entry) {
  305. list_del(&entry->fl_entry);
  306. kfree(entry);
  307. --mm->num_unused;
  308. }
  309. spin_unlock(&mm->unused_lock);
  310. BUG_ON(mm->num_unused != 0);
  311. }
  312. EXPORT_SYMBOL(drm_mm_takedown);