drm_mm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <linux/slab.h>
  44. unsigned long drm_mm_tail_space(struct drm_mm *mm)
  45. {
  46. struct list_head *tail_node;
  47. struct drm_mm_node *entry;
  48. tail_node = mm->ml_entry.prev;
  49. entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
  50. if (!entry->free)
  51. return 0;
  52. return entry->size;
  53. }
  54. int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size)
  55. {
  56. struct list_head *tail_node;
  57. struct drm_mm_node *entry;
  58. tail_node = mm->ml_entry.prev;
  59. entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
  60. if (!entry->free)
  61. return -ENOMEM;
  62. if (entry->size <= size)
  63. return -ENOMEM;
  64. entry->size -= size;
  65. return 0;
  66. }
  67. static int drm_mm_create_tail_node(struct drm_mm *mm,
  68. unsigned long start,
  69. unsigned long size)
  70. {
  71. struct drm_mm_node *child;
  72. child = (struct drm_mm_node *)
  73. drm_alloc(sizeof(*child), DRM_MEM_MM);
  74. if (!child)
  75. return -ENOMEM;
  76. child->free = 1;
  77. child->size = size;
  78. child->start = start;
  79. child->mm = mm;
  80. list_add_tail(&child->ml_entry, &mm->ml_entry);
  81. list_add_tail(&child->fl_entry, &mm->fl_entry);
  82. return 0;
  83. }
  84. int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size)
  85. {
  86. struct list_head *tail_node;
  87. struct drm_mm_node *entry;
  88. tail_node = mm->ml_entry.prev;
  89. entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
  90. if (!entry->free) {
  91. return drm_mm_create_tail_node(mm, entry->start + entry->size, size);
  92. }
  93. entry->size += size;
  94. return 0;
  95. }
  96. static struct drm_mm_node *drm_mm_split_at_start(struct drm_mm_node *parent,
  97. unsigned long size)
  98. {
  99. struct drm_mm_node *child;
  100. child = (struct drm_mm_node *)
  101. drm_alloc(sizeof(*child), DRM_MEM_MM);
  102. if (!child)
  103. return NULL;
  104. INIT_LIST_HEAD(&child->fl_entry);
  105. child->free = 0;
  106. child->size = size;
  107. child->start = parent->start;
  108. child->mm = parent->mm;
  109. list_add_tail(&child->ml_entry, &parent->ml_entry);
  110. INIT_LIST_HEAD(&child->fl_entry);
  111. parent->size -= size;
  112. parent->start += size;
  113. return child;
  114. }
  115. struct drm_mm_node *drm_mm_get_block(struct drm_mm_node * parent,
  116. unsigned long size, unsigned alignment)
  117. {
  118. struct drm_mm_node *align_splitoff = NULL;
  119. struct drm_mm_node *child;
  120. unsigned tmp = 0;
  121. if (alignment)
  122. tmp = parent->start % alignment;
  123. if (tmp) {
  124. align_splitoff = drm_mm_split_at_start(parent, alignment - tmp);
  125. if (!align_splitoff)
  126. return NULL;
  127. }
  128. if (parent->size == size) {
  129. list_del_init(&parent->fl_entry);
  130. parent->free = 0;
  131. return parent;
  132. } else {
  133. child = drm_mm_split_at_start(parent, size);
  134. }
  135. if (align_splitoff)
  136. drm_mm_put_block(align_splitoff);
  137. return child;
  138. }
  139. /*
  140. * Put a block. Merge with the previous and / or next block if they are free.
  141. * Otherwise add to the free stack.
  142. */
  143. void drm_mm_put_block(struct drm_mm_node * cur)
  144. {
  145. struct drm_mm *mm = cur->mm;
  146. struct list_head *cur_head = &cur->ml_entry;
  147. struct list_head *root_head = &mm->ml_entry;
  148. struct drm_mm_node *prev_node = NULL;
  149. struct drm_mm_node *next_node;
  150. int merged = 0;
  151. if (cur_head->prev != root_head) {
  152. prev_node = list_entry(cur_head->prev, struct drm_mm_node, ml_entry);
  153. if (prev_node->free) {
  154. prev_node->size += cur->size;
  155. merged = 1;
  156. }
  157. }
  158. if (cur_head->next != root_head) {
  159. next_node = list_entry(cur_head->next, struct drm_mm_node, ml_entry);
  160. if (next_node->free) {
  161. if (merged) {
  162. prev_node->size += next_node->size;
  163. list_del(&next_node->ml_entry);
  164. list_del(&next_node->fl_entry);
  165. drm_free(next_node, sizeof(*next_node),
  166. DRM_MEM_MM);
  167. } else {
  168. next_node->size += cur->size;
  169. next_node->start = cur->start;
  170. merged = 1;
  171. }
  172. }
  173. }
  174. if (!merged) {
  175. cur->free = 1;
  176. list_add(&cur->fl_entry, &mm->fl_entry);
  177. } else {
  178. list_del(&cur->ml_entry);
  179. drm_free(cur, sizeof(*cur), DRM_MEM_MM);
  180. }
  181. }
  182. struct drm_mm_node *drm_mm_search_free(const struct drm_mm * mm,
  183. unsigned long size,
  184. unsigned alignment, int best_match)
  185. {
  186. struct list_head *list;
  187. const struct list_head *free_stack = &mm->fl_entry;
  188. struct drm_mm_node *entry;
  189. struct drm_mm_node *best;
  190. unsigned long best_size;
  191. unsigned wasted;
  192. best = NULL;
  193. best_size = ~0UL;
  194. list_for_each(list, free_stack) {
  195. entry = list_entry(list, struct drm_mm_node, fl_entry);
  196. wasted = 0;
  197. if (entry->size < size)
  198. continue;
  199. if (alignment) {
  200. register unsigned tmp = entry->start % alignment;
  201. if (tmp)
  202. wasted += alignment - tmp;
  203. }
  204. if (entry->size >= size + wasted) {
  205. if (!best_match)
  206. return entry;
  207. if (size < best_size) {
  208. best = entry;
  209. best_size = entry->size;
  210. }
  211. }
  212. }
  213. return best;
  214. }
  215. int drm_mm_clean(struct drm_mm * mm)
  216. {
  217. struct list_head *head = &mm->ml_entry;
  218. return (head->next->next == head);
  219. }
  220. int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
  221. {
  222. INIT_LIST_HEAD(&mm->ml_entry);
  223. INIT_LIST_HEAD(&mm->fl_entry);
  224. return drm_mm_create_tail_node(mm, start, size);
  225. }
  226. void drm_mm_takedown(struct drm_mm * mm)
  227. {
  228. struct list_head *bnode = mm->fl_entry.next;
  229. struct drm_mm_node *entry;
  230. entry = list_entry(bnode, struct drm_mm_node, fl_entry);
  231. if (entry->ml_entry.next != &mm->ml_entry ||
  232. entry->fl_entry.next != &mm->fl_entry) {
  233. DRM_ERROR("Memory manager not clean. Delaying takedown\n");
  234. return;
  235. }
  236. list_del(&entry->fl_entry);
  237. list_del(&entry->ml_entry);
  238. drm_free(entry, sizeof(*entry), DRM_MEM_MM);
  239. }