drm_mm.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. #include <linux/seq_file.h>
  46. #define MM_UNUSED_TARGET 4
  47. unsigned long drm_mm_tail_space(struct drm_mm *mm)
  48. {
  49. struct list_head *tail_node;
  50. struct drm_mm_node *entry;
  51. tail_node = mm->ml_entry.prev;
  52. entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
  53. if (!entry->free)
  54. return 0;
  55. return entry->size;
  56. }
  57. int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size)
  58. {
  59. struct list_head *tail_node;
  60. struct drm_mm_node *entry;
  61. tail_node = mm->ml_entry.prev;
  62. entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
  63. if (!entry->free)
  64. return -ENOMEM;
  65. if (entry->size <= size)
  66. return -ENOMEM;
  67. entry->size -= size;
  68. return 0;
  69. }
  70. static struct drm_mm_node *drm_mm_kmalloc(struct drm_mm *mm, int atomic)
  71. {
  72. struct drm_mm_node *child;
  73. if (atomic)
  74. child = kmalloc(sizeof(*child), GFP_ATOMIC);
  75. else
  76. child = kmalloc(sizeof(*child), GFP_KERNEL);
  77. if (unlikely(child == NULL)) {
  78. spin_lock(&mm->unused_lock);
  79. if (list_empty(&mm->unused_nodes))
  80. child = NULL;
  81. else {
  82. child =
  83. list_entry(mm->unused_nodes.next,
  84. struct drm_mm_node, fl_entry);
  85. list_del(&child->fl_entry);
  86. --mm->num_unused;
  87. }
  88. spin_unlock(&mm->unused_lock);
  89. }
  90. return child;
  91. }
  92. int drm_mm_pre_get(struct drm_mm *mm)
  93. {
  94. struct drm_mm_node *node;
  95. spin_lock(&mm->unused_lock);
  96. while (mm->num_unused < MM_UNUSED_TARGET) {
  97. spin_unlock(&mm->unused_lock);
  98. node = kmalloc(sizeof(*node), GFP_KERNEL);
  99. spin_lock(&mm->unused_lock);
  100. if (unlikely(node == NULL)) {
  101. int ret = (mm->num_unused < 2) ? -ENOMEM : 0;
  102. spin_unlock(&mm->unused_lock);
  103. return ret;
  104. }
  105. ++mm->num_unused;
  106. list_add_tail(&node->fl_entry, &mm->unused_nodes);
  107. }
  108. spin_unlock(&mm->unused_lock);
  109. return 0;
  110. }
  111. EXPORT_SYMBOL(drm_mm_pre_get);
  112. static int drm_mm_create_tail_node(struct drm_mm *mm,
  113. unsigned long start,
  114. unsigned long size, int atomic)
  115. {
  116. struct drm_mm_node *child;
  117. child = drm_mm_kmalloc(mm, atomic);
  118. if (unlikely(child == NULL))
  119. return -ENOMEM;
  120. child->free = 1;
  121. child->size = size;
  122. child->start = start;
  123. child->mm = mm;
  124. list_add_tail(&child->ml_entry, &mm->ml_entry);
  125. list_add_tail(&child->fl_entry, &mm->fl_entry);
  126. return 0;
  127. }
  128. int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size, int atomic)
  129. {
  130. struct list_head *tail_node;
  131. struct drm_mm_node *entry;
  132. tail_node = mm->ml_entry.prev;
  133. entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
  134. if (!entry->free) {
  135. return drm_mm_create_tail_node(mm, entry->start + entry->size,
  136. size, atomic);
  137. }
  138. entry->size += size;
  139. return 0;
  140. }
  141. static struct drm_mm_node *drm_mm_split_at_start(struct drm_mm_node *parent,
  142. unsigned long size,
  143. int atomic)
  144. {
  145. struct drm_mm_node *child;
  146. child = drm_mm_kmalloc(parent->mm, atomic);
  147. if (unlikely(child == NULL))
  148. return NULL;
  149. INIT_LIST_HEAD(&child->fl_entry);
  150. child->free = 0;
  151. child->size = size;
  152. child->start = parent->start;
  153. child->mm = parent->mm;
  154. list_add_tail(&child->ml_entry, &parent->ml_entry);
  155. INIT_LIST_HEAD(&child->fl_entry);
  156. parent->size -= size;
  157. parent->start += size;
  158. return child;
  159. }
  160. struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
  161. unsigned long size,
  162. unsigned alignment,
  163. int atomic)
  164. {
  165. struct drm_mm_node *align_splitoff = NULL;
  166. unsigned tmp = 0;
  167. if (alignment)
  168. tmp = node->start % alignment;
  169. if (tmp) {
  170. align_splitoff =
  171. drm_mm_split_at_start(node, alignment - tmp, atomic);
  172. if (unlikely(align_splitoff == NULL))
  173. return NULL;
  174. }
  175. if (node->size == size) {
  176. list_del_init(&node->fl_entry);
  177. node->free = 0;
  178. } else {
  179. node = drm_mm_split_at_start(node, size, atomic);
  180. }
  181. if (align_splitoff)
  182. drm_mm_put_block(align_splitoff);
  183. return node;
  184. }
  185. EXPORT_SYMBOL(drm_mm_get_block_generic);
  186. /*
  187. * Put a block. Merge with the previous and / or next block if they are free.
  188. * Otherwise add to the free stack.
  189. */
  190. void drm_mm_put_block(struct drm_mm_node *cur)
  191. {
  192. struct drm_mm *mm = cur->mm;
  193. struct list_head *cur_head = &cur->ml_entry;
  194. struct list_head *root_head = &mm->ml_entry;
  195. struct drm_mm_node *prev_node = NULL;
  196. struct drm_mm_node *next_node;
  197. int merged = 0;
  198. if (cur_head->prev != root_head) {
  199. prev_node =
  200. list_entry(cur_head->prev, struct drm_mm_node, ml_entry);
  201. if (prev_node->free) {
  202. prev_node->size += cur->size;
  203. merged = 1;
  204. }
  205. }
  206. if (cur_head->next != root_head) {
  207. next_node =
  208. list_entry(cur_head->next, struct drm_mm_node, ml_entry);
  209. if (next_node->free) {
  210. if (merged) {
  211. prev_node->size += next_node->size;
  212. list_del(&next_node->ml_entry);
  213. list_del(&next_node->fl_entry);
  214. if (mm->num_unused < MM_UNUSED_TARGET) {
  215. list_add(&next_node->fl_entry,
  216. &mm->unused_nodes);
  217. ++mm->num_unused;
  218. } else
  219. kfree(next_node);
  220. } else {
  221. next_node->size += cur->size;
  222. next_node->start = cur->start;
  223. merged = 1;
  224. }
  225. }
  226. }
  227. if (!merged) {
  228. cur->free = 1;
  229. list_add(&cur->fl_entry, &mm->fl_entry);
  230. } else {
  231. list_del(&cur->ml_entry);
  232. if (mm->num_unused < MM_UNUSED_TARGET) {
  233. list_add(&cur->fl_entry, &mm->unused_nodes);
  234. ++mm->num_unused;
  235. } else
  236. kfree(cur);
  237. }
  238. }
  239. EXPORT_SYMBOL(drm_mm_put_block);
  240. struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
  241. unsigned long size,
  242. unsigned alignment, int best_match)
  243. {
  244. struct list_head *list;
  245. const struct list_head *free_stack = &mm->fl_entry;
  246. struct drm_mm_node *entry;
  247. struct drm_mm_node *best;
  248. unsigned long best_size;
  249. unsigned wasted;
  250. best = NULL;
  251. best_size = ~0UL;
  252. list_for_each(list, free_stack) {
  253. entry = list_entry(list, struct drm_mm_node, fl_entry);
  254. wasted = 0;
  255. if (entry->size < size)
  256. continue;
  257. if (alignment) {
  258. register unsigned tmp = entry->start % alignment;
  259. if (tmp)
  260. wasted += alignment - tmp;
  261. }
  262. if (entry->size >= size + wasted) {
  263. if (!best_match)
  264. return entry;
  265. if (size < best_size) {
  266. best = entry;
  267. best_size = entry->size;
  268. }
  269. }
  270. }
  271. return best;
  272. }
  273. EXPORT_SYMBOL(drm_mm_search_free);
  274. int drm_mm_clean(struct drm_mm * mm)
  275. {
  276. struct list_head *head = &mm->ml_entry;
  277. return (head->next->next == head);
  278. }
  279. EXPORT_SYMBOL(drm_mm_clean);
  280. int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
  281. {
  282. INIT_LIST_HEAD(&mm->ml_entry);
  283. INIT_LIST_HEAD(&mm->fl_entry);
  284. INIT_LIST_HEAD(&mm->unused_nodes);
  285. mm->num_unused = 0;
  286. spin_lock_init(&mm->unused_lock);
  287. return drm_mm_create_tail_node(mm, start, size, 0);
  288. }
  289. EXPORT_SYMBOL(drm_mm_init);
  290. void drm_mm_takedown(struct drm_mm * mm)
  291. {
  292. struct list_head *bnode = mm->fl_entry.next;
  293. struct drm_mm_node *entry;
  294. struct drm_mm_node *next;
  295. entry = list_entry(bnode, struct drm_mm_node, fl_entry);
  296. if (entry->ml_entry.next != &mm->ml_entry ||
  297. entry->fl_entry.next != &mm->fl_entry) {
  298. DRM_ERROR("Memory manager not clean. Delaying takedown\n");
  299. return;
  300. }
  301. list_del(&entry->fl_entry);
  302. list_del(&entry->ml_entry);
  303. kfree(entry);
  304. spin_lock(&mm->unused_lock);
  305. list_for_each_entry_safe(entry, next, &mm->unused_nodes, fl_entry) {
  306. list_del(&entry->fl_entry);
  307. kfree(entry);
  308. --mm->num_unused;
  309. }
  310. spin_unlock(&mm->unused_lock);
  311. BUG_ON(mm->num_unused != 0);
  312. }
  313. EXPORT_SYMBOL(drm_mm_takedown);
  314. #if defined(CONFIG_DEBUG_FS)
  315. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
  316. {
  317. struct drm_mm_node *entry;
  318. int total_used = 0, total_free = 0, total = 0;
  319. list_for_each_entry(entry, &mm->ml_entry, ml_entry) {
  320. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: %s\n", entry->start, entry->start + entry->size, entry->size, entry->free ? "free" : "used");
  321. total += entry->size;
  322. if (entry->free)
  323. total_free += entry->size;
  324. else
  325. total_used += entry->size;
  326. }
  327. seq_printf(m, "total: %d, used %d free %d\n", total, total_free, total_used);
  328. return 0;
  329. }
  330. EXPORT_SYMBOL(drm_mm_dump_table);
  331. #endif