drm_mm.c 9.9 KB

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