drm_mm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. static struct drm_mm_node *drm_mm_kmalloc(struct drm_mm *mm, int atomic)
  48. {
  49. struct drm_mm_node *child;
  50. if (atomic)
  51. child = kzalloc(sizeof(*child), GFP_ATOMIC);
  52. else
  53. child = kzalloc(sizeof(*child), GFP_KERNEL);
  54. if (unlikely(child == NULL)) {
  55. spin_lock(&mm->unused_lock);
  56. if (list_empty(&mm->unused_nodes))
  57. child = NULL;
  58. else {
  59. child =
  60. list_entry(mm->unused_nodes.next,
  61. struct drm_mm_node, free_stack);
  62. list_del(&child->free_stack);
  63. --mm->num_unused;
  64. }
  65. spin_unlock(&mm->unused_lock);
  66. }
  67. return child;
  68. }
  69. /* drm_mm_pre_get() - pre allocate drm_mm_node structure
  70. * drm_mm: memory manager struct we are pre-allocating for
  71. *
  72. * Returns 0 on success or -ENOMEM if allocation fails.
  73. */
  74. int drm_mm_pre_get(struct drm_mm *mm)
  75. {
  76. struct drm_mm_node *node;
  77. spin_lock(&mm->unused_lock);
  78. while (mm->num_unused < MM_UNUSED_TARGET) {
  79. spin_unlock(&mm->unused_lock);
  80. node = kzalloc(sizeof(*node), GFP_KERNEL);
  81. spin_lock(&mm->unused_lock);
  82. if (unlikely(node == NULL)) {
  83. int ret = (mm->num_unused < 2) ? -ENOMEM : 0;
  84. spin_unlock(&mm->unused_lock);
  85. return ret;
  86. }
  87. ++mm->num_unused;
  88. list_add_tail(&node->free_stack, &mm->unused_nodes);
  89. }
  90. spin_unlock(&mm->unused_lock);
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(drm_mm_pre_get);
  94. static int drm_mm_create_tail_node(struct drm_mm *mm,
  95. unsigned long start,
  96. unsigned long size, int atomic)
  97. {
  98. struct drm_mm_node *child;
  99. child = drm_mm_kmalloc(mm, atomic);
  100. if (unlikely(child == NULL))
  101. return -ENOMEM;
  102. child->free = 1;
  103. child->size = size;
  104. child->start = start;
  105. child->mm = mm;
  106. list_add_tail(&child->node_list, &mm->node_list);
  107. list_add_tail(&child->free_stack, &mm->free_stack);
  108. return 0;
  109. }
  110. static struct drm_mm_node *drm_mm_split_at_start(struct drm_mm_node *parent,
  111. unsigned long size,
  112. int atomic)
  113. {
  114. struct drm_mm_node *child;
  115. child = drm_mm_kmalloc(parent->mm, atomic);
  116. if (unlikely(child == NULL))
  117. return NULL;
  118. INIT_LIST_HEAD(&child->free_stack);
  119. child->size = size;
  120. child->start = parent->start;
  121. child->mm = parent->mm;
  122. list_add_tail(&child->node_list, &parent->node_list);
  123. INIT_LIST_HEAD(&child->free_stack);
  124. parent->size -= size;
  125. parent->start += size;
  126. return child;
  127. }
  128. struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
  129. unsigned long size,
  130. unsigned alignment,
  131. int atomic)
  132. {
  133. struct drm_mm_node *align_splitoff = NULL;
  134. unsigned tmp = 0;
  135. if (alignment)
  136. tmp = node->start % alignment;
  137. if (tmp) {
  138. align_splitoff =
  139. drm_mm_split_at_start(node, alignment - tmp, atomic);
  140. if (unlikely(align_splitoff == NULL))
  141. return NULL;
  142. }
  143. if (node->size == size) {
  144. list_del_init(&node->free_stack);
  145. node->free = 0;
  146. } else {
  147. node = drm_mm_split_at_start(node, size, atomic);
  148. }
  149. if (align_splitoff)
  150. drm_mm_put_block(align_splitoff);
  151. return node;
  152. }
  153. EXPORT_SYMBOL(drm_mm_get_block_generic);
  154. struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *node,
  155. unsigned long size,
  156. unsigned alignment,
  157. unsigned long start,
  158. unsigned long end,
  159. int atomic)
  160. {
  161. struct drm_mm_node *align_splitoff = NULL;
  162. unsigned tmp = 0;
  163. unsigned wasted = 0;
  164. if (node->start < start)
  165. wasted += start - node->start;
  166. if (alignment)
  167. tmp = ((node->start + wasted) % alignment);
  168. if (tmp)
  169. wasted += alignment - tmp;
  170. if (wasted) {
  171. align_splitoff = drm_mm_split_at_start(node, wasted, atomic);
  172. if (unlikely(align_splitoff == NULL))
  173. return NULL;
  174. }
  175. if (node->size == size) {
  176. list_del_init(&node->free_stack);
  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_range_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->node_list;
  194. struct list_head *root_head = &mm->node_list;
  195. struct drm_mm_node *prev_node = NULL;
  196. struct drm_mm_node *next_node;
  197. int merged = 0;
  198. BUG_ON(cur->scanned_block || cur->scanned_prev_free
  199. || cur->scanned_next_free);
  200. if (cur_head->prev != root_head) {
  201. prev_node =
  202. list_entry(cur_head->prev, struct drm_mm_node, node_list);
  203. if (prev_node->free) {
  204. prev_node->size += cur->size;
  205. merged = 1;
  206. }
  207. }
  208. if (cur_head->next != root_head) {
  209. next_node =
  210. list_entry(cur_head->next, struct drm_mm_node, node_list);
  211. if (next_node->free) {
  212. if (merged) {
  213. prev_node->size += next_node->size;
  214. list_del(&next_node->node_list);
  215. list_del(&next_node->free_stack);
  216. spin_lock(&mm->unused_lock);
  217. if (mm->num_unused < MM_UNUSED_TARGET) {
  218. list_add(&next_node->free_stack,
  219. &mm->unused_nodes);
  220. ++mm->num_unused;
  221. } else
  222. kfree(next_node);
  223. spin_unlock(&mm->unused_lock);
  224. } else {
  225. next_node->size += cur->size;
  226. next_node->start = cur->start;
  227. merged = 1;
  228. }
  229. }
  230. }
  231. if (!merged) {
  232. cur->free = 1;
  233. list_add(&cur->free_stack, &mm->free_stack);
  234. } else {
  235. list_del(&cur->node_list);
  236. spin_lock(&mm->unused_lock);
  237. if (mm->num_unused < MM_UNUSED_TARGET) {
  238. list_add(&cur->free_stack, &mm->unused_nodes);
  239. ++mm->num_unused;
  240. } else
  241. kfree(cur);
  242. spin_unlock(&mm->unused_lock);
  243. }
  244. }
  245. EXPORT_SYMBOL(drm_mm_put_block);
  246. static int check_free_mm_node(struct drm_mm_node *entry, unsigned long size,
  247. unsigned alignment)
  248. {
  249. unsigned wasted = 0;
  250. if (entry->size < size)
  251. return 0;
  252. if (alignment) {
  253. register unsigned tmp = entry->start % alignment;
  254. if (tmp)
  255. wasted = alignment - tmp;
  256. }
  257. if (entry->size >= size + wasted) {
  258. return 1;
  259. }
  260. return 0;
  261. }
  262. struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
  263. unsigned long size,
  264. unsigned alignment, int best_match)
  265. {
  266. struct drm_mm_node *entry;
  267. struct drm_mm_node *best;
  268. unsigned long best_size;
  269. BUG_ON(mm->scanned_blocks);
  270. best = NULL;
  271. best_size = ~0UL;
  272. list_for_each_entry(entry, &mm->free_stack, free_stack) {
  273. if (!check_free_mm_node(entry, size, alignment))
  274. continue;
  275. if (!best_match)
  276. return entry;
  277. if (entry->size < best_size) {
  278. best = entry;
  279. best_size = entry->size;
  280. }
  281. }
  282. return best;
  283. }
  284. EXPORT_SYMBOL(drm_mm_search_free);
  285. struct drm_mm_node *drm_mm_search_free_in_range(const struct drm_mm *mm,
  286. unsigned long size,
  287. unsigned alignment,
  288. unsigned long start,
  289. unsigned long end,
  290. int best_match)
  291. {
  292. struct drm_mm_node *entry;
  293. struct drm_mm_node *best;
  294. unsigned long best_size;
  295. BUG_ON(mm->scanned_blocks);
  296. best = NULL;
  297. best_size = ~0UL;
  298. list_for_each_entry(entry, &mm->free_stack, free_stack) {
  299. if (entry->start > end || (entry->start+entry->size) < start)
  300. continue;
  301. if (!check_free_mm_node(entry, size, alignment))
  302. continue;
  303. if (!best_match)
  304. return entry;
  305. if (entry->size < best_size) {
  306. best = entry;
  307. best_size = entry->size;
  308. }
  309. }
  310. return best;
  311. }
  312. EXPORT_SYMBOL(drm_mm_search_free_in_range);
  313. /**
  314. * Initializa lru scanning.
  315. *
  316. * This simply sets up the scanning routines with the parameters for the desired
  317. * hole.
  318. *
  319. * Warning: As long as the scan list is non-empty, no other operations than
  320. * adding/removing nodes to/from the scan list are allowed.
  321. */
  322. void drm_mm_init_scan(struct drm_mm *mm, unsigned long size,
  323. unsigned alignment)
  324. {
  325. mm->scan_alignment = alignment;
  326. mm->scan_size = size;
  327. mm->scanned_blocks = 0;
  328. mm->scan_hit_start = 0;
  329. mm->scan_hit_size = 0;
  330. }
  331. EXPORT_SYMBOL(drm_mm_init_scan);
  332. /**
  333. * Add a node to the scan list that might be freed to make space for the desired
  334. * hole.
  335. *
  336. * Returns non-zero, if a hole has been found, zero otherwise.
  337. */
  338. int drm_mm_scan_add_block(struct drm_mm_node *node)
  339. {
  340. struct drm_mm *mm = node->mm;
  341. struct list_head *prev_free, *next_free;
  342. struct drm_mm_node *prev_node, *next_node;
  343. mm->scanned_blocks++;
  344. prev_free = next_free = NULL;
  345. BUG_ON(node->free);
  346. node->scanned_block = 1;
  347. node->free = 1;
  348. if (node->node_list.prev != &mm->node_list) {
  349. prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
  350. node_list);
  351. if (prev_node->free) {
  352. list_del(&prev_node->node_list);
  353. node->start = prev_node->start;
  354. node->size += prev_node->size;
  355. prev_node->scanned_prev_free = 1;
  356. prev_free = &prev_node->free_stack;
  357. }
  358. }
  359. if (node->node_list.next != &mm->node_list) {
  360. next_node = list_entry(node->node_list.next, struct drm_mm_node,
  361. node_list);
  362. if (next_node->free) {
  363. list_del(&next_node->node_list);
  364. node->size += next_node->size;
  365. next_node->scanned_next_free = 1;
  366. next_free = &next_node->free_stack;
  367. }
  368. }
  369. /* The free_stack list is not used for allocated objects, so these two
  370. * pointers can be abused (as long as no allocations in this memory
  371. * manager happens). */
  372. node->free_stack.prev = prev_free;
  373. node->free_stack.next = next_free;
  374. if (check_free_mm_node(node, mm->scan_size, mm->scan_alignment)) {
  375. mm->scan_hit_start = node->start;
  376. mm->scan_hit_size = node->size;
  377. return 1;
  378. }
  379. return 0;
  380. }
  381. EXPORT_SYMBOL(drm_mm_scan_add_block);
  382. /**
  383. * Remove a node from the scan list.
  384. *
  385. * Nodes _must_ be removed in the exact same order from the scan list as they
  386. * have been added, otherwise the internal state of the memory manager will be
  387. * corrupted.
  388. *
  389. * When the scan list is empty, the selected memory nodes can be freed. An
  390. * immediatly following drm_mm_search_free with best_match = 0 will then return
  391. * the just freed block (because its at the top of the free_stack list).
  392. *
  393. * Returns one if this block should be evicted, zero otherwise. Will always
  394. * return zero when no hole has been found.
  395. */
  396. int drm_mm_scan_remove_block(struct drm_mm_node *node)
  397. {
  398. struct drm_mm *mm = node->mm;
  399. struct drm_mm_node *prev_node, *next_node;
  400. mm->scanned_blocks--;
  401. BUG_ON(!node->scanned_block);
  402. node->scanned_block = 0;
  403. node->free = 0;
  404. prev_node = list_entry(node->free_stack.prev, struct drm_mm_node,
  405. free_stack);
  406. next_node = list_entry(node->free_stack.next, struct drm_mm_node,
  407. free_stack);
  408. if (prev_node) {
  409. BUG_ON(!prev_node->scanned_prev_free);
  410. prev_node->scanned_prev_free = 0;
  411. list_add_tail(&prev_node->node_list, &node->node_list);
  412. node->start = prev_node->start + prev_node->size;
  413. node->size -= prev_node->size;
  414. }
  415. if (next_node) {
  416. BUG_ON(!next_node->scanned_next_free);
  417. next_node->scanned_next_free = 0;
  418. list_add(&next_node->node_list, &node->node_list);
  419. node->size -= next_node->size;
  420. }
  421. INIT_LIST_HEAD(&node->free_stack);
  422. /* Only need to check for containement because start&size for the
  423. * complete resulting free block (not just the desired part) is
  424. * stored. */
  425. if (node->start >= mm->scan_hit_start &&
  426. node->start + node->size
  427. <= mm->scan_hit_start + mm->scan_hit_size) {
  428. return 1;
  429. }
  430. return 0;
  431. }
  432. EXPORT_SYMBOL(drm_mm_scan_remove_block);
  433. int drm_mm_clean(struct drm_mm * mm)
  434. {
  435. struct list_head *head = &mm->node_list;
  436. return (head->next->next == head);
  437. }
  438. EXPORT_SYMBOL(drm_mm_clean);
  439. int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
  440. {
  441. INIT_LIST_HEAD(&mm->node_list);
  442. INIT_LIST_HEAD(&mm->free_stack);
  443. INIT_LIST_HEAD(&mm->unused_nodes);
  444. mm->num_unused = 0;
  445. mm->scanned_blocks = 0;
  446. spin_lock_init(&mm->unused_lock);
  447. return drm_mm_create_tail_node(mm, start, size, 0);
  448. }
  449. EXPORT_SYMBOL(drm_mm_init);
  450. void drm_mm_takedown(struct drm_mm * mm)
  451. {
  452. struct list_head *bnode = mm->free_stack.next;
  453. struct drm_mm_node *entry;
  454. struct drm_mm_node *next;
  455. entry = list_entry(bnode, struct drm_mm_node, free_stack);
  456. if (entry->node_list.next != &mm->node_list ||
  457. entry->free_stack.next != &mm->free_stack) {
  458. DRM_ERROR("Memory manager not clean. Delaying takedown\n");
  459. return;
  460. }
  461. list_del(&entry->free_stack);
  462. list_del(&entry->node_list);
  463. kfree(entry);
  464. spin_lock(&mm->unused_lock);
  465. list_for_each_entry_safe(entry, next, &mm->unused_nodes, free_stack) {
  466. list_del(&entry->free_stack);
  467. kfree(entry);
  468. --mm->num_unused;
  469. }
  470. spin_unlock(&mm->unused_lock);
  471. BUG_ON(mm->num_unused != 0);
  472. }
  473. EXPORT_SYMBOL(drm_mm_takedown);
  474. void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
  475. {
  476. struct drm_mm_node *entry;
  477. int total_used = 0, total_free = 0, total = 0;
  478. list_for_each_entry(entry, &mm->node_list, node_list) {
  479. printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8ld: %s\n",
  480. prefix, entry->start, entry->start + entry->size,
  481. entry->size, entry->free ? "free" : "used");
  482. total += entry->size;
  483. if (entry->free)
  484. total_free += entry->size;
  485. else
  486. total_used += entry->size;
  487. }
  488. printk(KERN_DEBUG "%s total: %d, used %d free %d\n", prefix, total,
  489. total_used, total_free);
  490. }
  491. EXPORT_SYMBOL(drm_mm_debug_table);
  492. #if defined(CONFIG_DEBUG_FS)
  493. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
  494. {
  495. struct drm_mm_node *entry;
  496. int total_used = 0, total_free = 0, total = 0;
  497. list_for_each_entry(entry, &mm->node_list, node_list) {
  498. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: %s\n", entry->start, entry->start + entry->size, entry->size, entry->free ? "free" : "used");
  499. total += entry->size;
  500. if (entry->free)
  501. total_free += entry->size;
  502. else
  503. total_used += entry->size;
  504. }
  505. seq_printf(m, "total: %d, used %d free %d\n", total, total_used, total_free);
  506. return 0;
  507. }
  508. EXPORT_SYMBOL(drm_mm_dump_table);
  509. #endif