drm_mm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *node,
  192. unsigned long size,
  193. unsigned alignment,
  194. unsigned long start,
  195. unsigned long end,
  196. int atomic)
  197. {
  198. struct drm_mm_node *align_splitoff = NULL;
  199. unsigned tmp = 0;
  200. unsigned wasted = 0;
  201. if (node->start < start)
  202. wasted += start - node->start;
  203. if (alignment)
  204. tmp = ((node->start + wasted) % alignment);
  205. if (tmp)
  206. wasted += alignment - tmp;
  207. if (wasted) {
  208. align_splitoff = drm_mm_split_at_start(node, wasted, atomic);
  209. if (unlikely(align_splitoff == NULL))
  210. return NULL;
  211. }
  212. if (node->size == size) {
  213. list_del_init(&node->fl_entry);
  214. node->free = 0;
  215. } else {
  216. node = drm_mm_split_at_start(node, size, atomic);
  217. }
  218. if (align_splitoff)
  219. drm_mm_put_block(align_splitoff);
  220. return node;
  221. }
  222. EXPORT_SYMBOL(drm_mm_get_block_range_generic);
  223. /*
  224. * Put a block. Merge with the previous and / or next block if they are free.
  225. * Otherwise add to the free stack.
  226. */
  227. void drm_mm_put_block(struct drm_mm_node *cur)
  228. {
  229. struct drm_mm *mm = cur->mm;
  230. struct list_head *cur_head = &cur->ml_entry;
  231. struct list_head *root_head = &mm->ml_entry;
  232. struct drm_mm_node *prev_node = NULL;
  233. struct drm_mm_node *next_node;
  234. int merged = 0;
  235. if (cur_head->prev != root_head) {
  236. prev_node =
  237. list_entry(cur_head->prev, struct drm_mm_node, ml_entry);
  238. if (prev_node->free) {
  239. prev_node->size += cur->size;
  240. merged = 1;
  241. }
  242. }
  243. if (cur_head->next != root_head) {
  244. next_node =
  245. list_entry(cur_head->next, struct drm_mm_node, ml_entry);
  246. if (next_node->free) {
  247. if (merged) {
  248. prev_node->size += next_node->size;
  249. list_del(&next_node->ml_entry);
  250. list_del(&next_node->fl_entry);
  251. spin_lock(&mm->unused_lock);
  252. if (mm->num_unused < MM_UNUSED_TARGET) {
  253. list_add(&next_node->fl_entry,
  254. &mm->unused_nodes);
  255. ++mm->num_unused;
  256. } else
  257. kfree(next_node);
  258. spin_unlock(&mm->unused_lock);
  259. } else {
  260. next_node->size += cur->size;
  261. next_node->start = cur->start;
  262. merged = 1;
  263. }
  264. }
  265. }
  266. if (!merged) {
  267. cur->free = 1;
  268. list_add(&cur->fl_entry, &mm->fl_entry);
  269. } else {
  270. list_del(&cur->ml_entry);
  271. spin_lock(&mm->unused_lock);
  272. if (mm->num_unused < MM_UNUSED_TARGET) {
  273. list_add(&cur->fl_entry, &mm->unused_nodes);
  274. ++mm->num_unused;
  275. } else
  276. kfree(cur);
  277. spin_unlock(&mm->unused_lock);
  278. }
  279. }
  280. EXPORT_SYMBOL(drm_mm_put_block);
  281. struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
  282. unsigned long size,
  283. unsigned alignment, int best_match)
  284. {
  285. struct list_head *list;
  286. const struct list_head *free_stack = &mm->fl_entry;
  287. struct drm_mm_node *entry;
  288. struct drm_mm_node *best;
  289. unsigned long best_size;
  290. unsigned wasted;
  291. best = NULL;
  292. best_size = ~0UL;
  293. list_for_each(list, free_stack) {
  294. entry = list_entry(list, struct drm_mm_node, fl_entry);
  295. wasted = 0;
  296. if (entry->size < size)
  297. continue;
  298. if (alignment) {
  299. register unsigned tmp = entry->start % alignment;
  300. if (tmp)
  301. wasted += alignment - tmp;
  302. }
  303. if (entry->size >= size + wasted) {
  304. if (!best_match)
  305. return entry;
  306. if (size < best_size) {
  307. best = entry;
  308. best_size = entry->size;
  309. }
  310. }
  311. }
  312. return best;
  313. }
  314. EXPORT_SYMBOL(drm_mm_search_free);
  315. struct drm_mm_node *drm_mm_search_free_in_range(const struct drm_mm *mm,
  316. unsigned long size,
  317. unsigned alignment,
  318. unsigned long start,
  319. unsigned long end,
  320. int best_match)
  321. {
  322. struct list_head *list;
  323. const struct list_head *free_stack = &mm->fl_entry;
  324. struct drm_mm_node *entry;
  325. struct drm_mm_node *best;
  326. unsigned long best_size;
  327. unsigned wasted;
  328. best = NULL;
  329. best_size = ~0UL;
  330. list_for_each(list, free_stack) {
  331. entry = list_entry(list, struct drm_mm_node, fl_entry);
  332. wasted = 0;
  333. if (entry->size < size)
  334. continue;
  335. if (entry->start > end || (entry->start+entry->size) < start)
  336. continue;
  337. if (entry->start < start)
  338. wasted += start - entry->start;
  339. if (alignment) {
  340. register unsigned tmp = (entry->start + wasted) % alignment;
  341. if (tmp)
  342. wasted += alignment - tmp;
  343. }
  344. if (entry->size >= size + wasted) {
  345. if (!best_match)
  346. return entry;
  347. if (size < best_size) {
  348. best = entry;
  349. best_size = entry->size;
  350. }
  351. }
  352. }
  353. return best;
  354. }
  355. EXPORT_SYMBOL(drm_mm_search_free_in_range);
  356. int drm_mm_clean(struct drm_mm * mm)
  357. {
  358. struct list_head *head = &mm->ml_entry;
  359. return (head->next->next == head);
  360. }
  361. EXPORT_SYMBOL(drm_mm_clean);
  362. int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
  363. {
  364. INIT_LIST_HEAD(&mm->ml_entry);
  365. INIT_LIST_HEAD(&mm->fl_entry);
  366. INIT_LIST_HEAD(&mm->unused_nodes);
  367. mm->num_unused = 0;
  368. spin_lock_init(&mm->unused_lock);
  369. return drm_mm_create_tail_node(mm, start, size, 0);
  370. }
  371. EXPORT_SYMBOL(drm_mm_init);
  372. void drm_mm_takedown(struct drm_mm * mm)
  373. {
  374. struct list_head *bnode = mm->fl_entry.next;
  375. struct drm_mm_node *entry;
  376. struct drm_mm_node *next;
  377. entry = list_entry(bnode, struct drm_mm_node, fl_entry);
  378. if (entry->ml_entry.next != &mm->ml_entry ||
  379. entry->fl_entry.next != &mm->fl_entry) {
  380. DRM_ERROR("Memory manager not clean. Delaying takedown\n");
  381. return;
  382. }
  383. list_del(&entry->fl_entry);
  384. list_del(&entry->ml_entry);
  385. kfree(entry);
  386. spin_lock(&mm->unused_lock);
  387. list_for_each_entry_safe(entry, next, &mm->unused_nodes, fl_entry) {
  388. list_del(&entry->fl_entry);
  389. kfree(entry);
  390. --mm->num_unused;
  391. }
  392. spin_unlock(&mm->unused_lock);
  393. BUG_ON(mm->num_unused != 0);
  394. }
  395. EXPORT_SYMBOL(drm_mm_takedown);
  396. #if defined(CONFIG_DEBUG_FS)
  397. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
  398. {
  399. struct drm_mm_node *entry;
  400. int total_used = 0, total_free = 0, total = 0;
  401. list_for_each_entry(entry, &mm->ml_entry, ml_entry) {
  402. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: %s\n", entry->start, entry->start + entry->size, entry->size, entry->free ? "free" : "used");
  403. total += entry->size;
  404. if (entry->free)
  405. total_free += entry->size;
  406. else
  407. total_used += entry->size;
  408. }
  409. seq_printf(m, "total: %d, used %d free %d\n", total, total_used, total_free);
  410. return 0;
  411. }
  412. EXPORT_SYMBOL(drm_mm_dump_table);
  413. #endif