drm_mm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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, node_list);
  62. list_del(&child->node_list);
  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->node_list, &mm->unused_nodes);
  89. }
  90. spin_unlock(&mm->unused_lock);
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(drm_mm_pre_get);
  94. static inline unsigned long drm_mm_hole_node_start(struct drm_mm_node *hole_node)
  95. {
  96. return hole_node->start + hole_node->size;
  97. }
  98. static inline unsigned long drm_mm_hole_node_end(struct drm_mm_node *hole_node)
  99. {
  100. struct drm_mm_node *next_node =
  101. list_entry(hole_node->node_list.next, struct drm_mm_node,
  102. node_list);
  103. return next_node->start;
  104. }
  105. struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *hole_node,
  106. unsigned long size,
  107. unsigned alignment,
  108. int atomic)
  109. {
  110. struct drm_mm_node *node;
  111. struct drm_mm *mm = hole_node->mm;
  112. unsigned long tmp = 0, wasted = 0;
  113. unsigned long hole_start = drm_mm_hole_node_start(hole_node);
  114. unsigned long hole_end = drm_mm_hole_node_end(hole_node);
  115. BUG_ON(!hole_node->hole_follows);
  116. node = drm_mm_kmalloc(mm, atomic);
  117. if (unlikely(node == NULL))
  118. return NULL;
  119. if (alignment)
  120. tmp = hole_start % alignment;
  121. if (!tmp) {
  122. hole_node->hole_follows = 0;
  123. list_del_init(&hole_node->hole_stack);
  124. } else
  125. wasted = alignment - tmp;
  126. node->start = hole_start + wasted;
  127. node->size = size;
  128. node->mm = mm;
  129. INIT_LIST_HEAD(&node->hole_stack);
  130. list_add(&node->node_list, &hole_node->node_list);
  131. BUG_ON(node->start + node->size > hole_end);
  132. if (node->start + node->size < hole_end) {
  133. list_add(&node->hole_stack, &mm->hole_stack);
  134. node->hole_follows = 1;
  135. } else {
  136. node->hole_follows = 0;
  137. }
  138. return node;
  139. }
  140. EXPORT_SYMBOL(drm_mm_get_block_generic);
  141. struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *hole_node,
  142. unsigned long size,
  143. unsigned alignment,
  144. unsigned long start,
  145. unsigned long end,
  146. int atomic)
  147. {
  148. struct drm_mm_node *node;
  149. struct drm_mm *mm = hole_node->mm;
  150. unsigned long tmp = 0, wasted = 0;
  151. unsigned long hole_start = drm_mm_hole_node_start(hole_node);
  152. unsigned long hole_end = drm_mm_hole_node_end(hole_node);
  153. BUG_ON(!hole_node->hole_follows);
  154. node = drm_mm_kmalloc(mm, atomic);
  155. if (unlikely(node == NULL))
  156. return NULL;
  157. if (hole_start < start)
  158. wasted += start - hole_start;
  159. if (alignment)
  160. tmp = (hole_start + wasted) % alignment;
  161. if (tmp)
  162. wasted += alignment - tmp;
  163. if (!wasted) {
  164. hole_node->hole_follows = 0;
  165. list_del_init(&hole_node->hole_stack);
  166. }
  167. node->start = hole_start + wasted;
  168. node->size = size;
  169. node->mm = mm;
  170. INIT_LIST_HEAD(&node->hole_stack);
  171. list_add(&node->node_list, &hole_node->node_list);
  172. BUG_ON(node->start + node->size > hole_end);
  173. BUG_ON(node->start + node->size > end);
  174. if (node->start + node->size < hole_end) {
  175. list_add(&node->hole_stack, &mm->hole_stack);
  176. node->hole_follows = 1;
  177. } else {
  178. node->hole_follows = 0;
  179. }
  180. return node;
  181. }
  182. EXPORT_SYMBOL(drm_mm_get_block_range_generic);
  183. /*
  184. * Put a block. Merge with the previous and / or next block if they are free.
  185. * Otherwise add to the free stack.
  186. */
  187. void drm_mm_put_block(struct drm_mm_node *node)
  188. {
  189. struct drm_mm *mm = node->mm;
  190. struct drm_mm_node *prev_node;
  191. BUG_ON(node->scanned_block || node->scanned_prev_free
  192. || node->scanned_next_free);
  193. prev_node =
  194. list_entry(node->node_list.prev, struct drm_mm_node, node_list);
  195. if (node->hole_follows) {
  196. BUG_ON(drm_mm_hole_node_start(node)
  197. == drm_mm_hole_node_end(node));
  198. list_del(&node->hole_stack);
  199. } else
  200. BUG_ON(drm_mm_hole_node_start(node)
  201. != drm_mm_hole_node_end(node));
  202. if (!prev_node->hole_follows) {
  203. prev_node->hole_follows = 1;
  204. list_add(&prev_node->hole_stack, &mm->hole_stack);
  205. } else
  206. list_move(&prev_node->hole_stack, &mm->hole_stack);
  207. list_del(&node->node_list);
  208. spin_lock(&mm->unused_lock);
  209. if (mm->num_unused < MM_UNUSED_TARGET) {
  210. list_add(&node->node_list, &mm->unused_nodes);
  211. ++mm->num_unused;
  212. } else
  213. kfree(node);
  214. spin_unlock(&mm->unused_lock);
  215. }
  216. EXPORT_SYMBOL(drm_mm_put_block);
  217. static int check_free_hole(unsigned long start, unsigned long end,
  218. unsigned long size, unsigned alignment)
  219. {
  220. unsigned wasted = 0;
  221. if (end - start < size)
  222. return 0;
  223. if (alignment) {
  224. unsigned tmp = start % alignment;
  225. if (tmp)
  226. wasted = alignment - tmp;
  227. }
  228. if (end >= start + size + wasted) {
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
  234. unsigned long size,
  235. unsigned alignment, int best_match)
  236. {
  237. struct drm_mm_node *entry;
  238. struct drm_mm_node *best;
  239. unsigned long best_size;
  240. BUG_ON(mm->scanned_blocks);
  241. best = NULL;
  242. best_size = ~0UL;
  243. list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
  244. BUG_ON(!entry->hole_follows);
  245. if (!check_free_hole(drm_mm_hole_node_start(entry),
  246. drm_mm_hole_node_end(entry),
  247. size, alignment))
  248. continue;
  249. if (!best_match)
  250. return entry;
  251. if (entry->size < best_size) {
  252. best = entry;
  253. best_size = entry->size;
  254. }
  255. }
  256. return best;
  257. }
  258. EXPORT_SYMBOL(drm_mm_search_free);
  259. struct drm_mm_node *drm_mm_search_free_in_range(const struct drm_mm *mm,
  260. unsigned long size,
  261. unsigned alignment,
  262. unsigned long start,
  263. unsigned long end,
  264. 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->hole_stack, hole_stack) {
  273. unsigned long adj_start = drm_mm_hole_node_start(entry) < start ?
  274. start : drm_mm_hole_node_start(entry);
  275. unsigned long adj_end = drm_mm_hole_node_end(entry) > end ?
  276. end : drm_mm_hole_node_end(entry);
  277. BUG_ON(!entry->hole_follows);
  278. if (!check_free_hole(adj_start, adj_end, size, alignment))
  279. continue;
  280. if (!best_match)
  281. return entry;
  282. if (entry->size < best_size) {
  283. best = entry;
  284. best_size = entry->size;
  285. }
  286. }
  287. return best;
  288. }
  289. EXPORT_SYMBOL(drm_mm_search_free_in_range);
  290. /**
  291. * Initializa lru scanning.
  292. *
  293. * This simply sets up the scanning routines with the parameters for the desired
  294. * hole.
  295. *
  296. * Warning: As long as the scan list is non-empty, no other operations than
  297. * adding/removing nodes to/from the scan list are allowed.
  298. */
  299. void drm_mm_init_scan(struct drm_mm *mm, unsigned long size,
  300. unsigned alignment)
  301. {
  302. mm->scan_alignment = alignment;
  303. mm->scan_size = size;
  304. mm->scanned_blocks = 0;
  305. mm->scan_hit_start = 0;
  306. mm->scan_hit_size = 0;
  307. mm->scan_check_range = 0;
  308. }
  309. EXPORT_SYMBOL(drm_mm_init_scan);
  310. /**
  311. * Initializa lru scanning.
  312. *
  313. * This simply sets up the scanning routines with the parameters for the desired
  314. * hole. This version is for range-restricted scans.
  315. *
  316. * Warning: As long as the scan list is non-empty, no other operations than
  317. * adding/removing nodes to/from the scan list are allowed.
  318. */
  319. void drm_mm_init_scan_with_range(struct drm_mm *mm, unsigned long size,
  320. unsigned alignment,
  321. unsigned long start,
  322. unsigned long end)
  323. {
  324. mm->scan_alignment = alignment;
  325. mm->scan_size = size;
  326. mm->scanned_blocks = 0;
  327. mm->scan_hit_start = 0;
  328. mm->scan_hit_size = 0;
  329. mm->scan_start = start;
  330. mm->scan_end = end;
  331. mm->scan_check_range = 1;
  332. }
  333. EXPORT_SYMBOL(drm_mm_init_scan_with_range);
  334. /**
  335. * Add a node to the scan list that might be freed to make space for the desired
  336. * hole.
  337. *
  338. * Returns non-zero, if a hole has been found, zero otherwise.
  339. */
  340. int drm_mm_scan_add_block(struct drm_mm_node *node)
  341. {
  342. struct drm_mm *mm = node->mm;
  343. struct drm_mm_node *prev_node;
  344. unsigned long hole_start, hole_end;
  345. unsigned long adj_start;
  346. unsigned long adj_end;
  347. mm->scanned_blocks++;
  348. BUG_ON(node->scanned_block);
  349. node->scanned_block = 1;
  350. prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
  351. node_list);
  352. node->scanned_preceeds_hole = prev_node->hole_follows;
  353. prev_node->hole_follows = 1;
  354. list_del(&node->node_list);
  355. node->node_list.prev = &prev_node->node_list;
  356. hole_start = drm_mm_hole_node_start(prev_node);
  357. hole_end = drm_mm_hole_node_end(prev_node);
  358. if (mm->scan_check_range) {
  359. adj_start = hole_start < mm->scan_start ?
  360. mm->scan_start : hole_start;
  361. adj_end = hole_end > mm->scan_end ?
  362. mm->scan_end : hole_end;
  363. } else {
  364. adj_start = hole_start;
  365. adj_end = hole_end;
  366. }
  367. if (check_free_hole(adj_start , adj_end,
  368. mm->scan_size, mm->scan_alignment)) {
  369. mm->scan_hit_start = hole_start;
  370. mm->scan_hit_size = hole_end;
  371. return 1;
  372. }
  373. return 0;
  374. }
  375. EXPORT_SYMBOL(drm_mm_scan_add_block);
  376. /**
  377. * Remove a node from the scan list.
  378. *
  379. * Nodes _must_ be removed in the exact same order from the scan list as they
  380. * have been added, otherwise the internal state of the memory manager will be
  381. * corrupted.
  382. *
  383. * When the scan list is empty, the selected memory nodes can be freed. An
  384. * immediatly following drm_mm_search_free with best_match = 0 will then return
  385. * the just freed block (because its at the top of the free_stack list).
  386. *
  387. * Returns one if this block should be evicted, zero otherwise. Will always
  388. * return zero when no hole has been found.
  389. */
  390. int drm_mm_scan_remove_block(struct drm_mm_node *node)
  391. {
  392. struct drm_mm *mm = node->mm;
  393. struct drm_mm_node *prev_node;
  394. mm->scanned_blocks--;
  395. BUG_ON(!node->scanned_block);
  396. node->scanned_block = 0;
  397. prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
  398. node_list);
  399. prev_node->hole_follows = node->scanned_preceeds_hole;
  400. INIT_LIST_HEAD(&node->node_list);
  401. list_add(&node->node_list, &prev_node->node_list);
  402. /* Only need to check for containement because start&size for the
  403. * complete resulting free block (not just the desired part) is
  404. * stored. */
  405. if (node->start >= mm->scan_hit_start &&
  406. node->start + node->size
  407. <= mm->scan_hit_start + mm->scan_hit_size) {
  408. return 1;
  409. }
  410. return 0;
  411. }
  412. EXPORT_SYMBOL(drm_mm_scan_remove_block);
  413. int drm_mm_clean(struct drm_mm * mm)
  414. {
  415. struct list_head *head = &mm->head_node.node_list;
  416. return (head->next->next == head);
  417. }
  418. EXPORT_SYMBOL(drm_mm_clean);
  419. int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
  420. {
  421. INIT_LIST_HEAD(&mm->hole_stack);
  422. INIT_LIST_HEAD(&mm->unused_nodes);
  423. mm->num_unused = 0;
  424. mm->scanned_blocks = 0;
  425. spin_lock_init(&mm->unused_lock);
  426. /* Clever trick to avoid a special case in the free hole tracking. */
  427. INIT_LIST_HEAD(&mm->head_node.node_list);
  428. INIT_LIST_HEAD(&mm->head_node.hole_stack);
  429. mm->head_node.hole_follows = 1;
  430. mm->head_node.scanned_block = 0;
  431. mm->head_node.scanned_prev_free = 0;
  432. mm->head_node.scanned_next_free = 0;
  433. mm->head_node.mm = mm;
  434. mm->head_node.start = start + size;
  435. mm->head_node.size = start - mm->head_node.start;
  436. list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
  437. return 0;
  438. }
  439. EXPORT_SYMBOL(drm_mm_init);
  440. void drm_mm_takedown(struct drm_mm * mm)
  441. {
  442. struct drm_mm_node *entry, *next;
  443. if (!list_empty(&mm->head_node.node_list)) {
  444. DRM_ERROR("Memory manager not clean. Delaying takedown\n");
  445. return;
  446. }
  447. spin_lock(&mm->unused_lock);
  448. list_for_each_entry_safe(entry, next, &mm->unused_nodes, node_list) {
  449. list_del(&entry->node_list);
  450. kfree(entry);
  451. --mm->num_unused;
  452. }
  453. spin_unlock(&mm->unused_lock);
  454. BUG_ON(mm->num_unused != 0);
  455. }
  456. EXPORT_SYMBOL(drm_mm_takedown);
  457. void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
  458. {
  459. struct drm_mm_node *entry;
  460. unsigned long total_used = 0, total_free = 0, total = 0;
  461. unsigned long hole_start, hole_end, hole_size;
  462. hole_start = drm_mm_hole_node_start(&mm->head_node);
  463. hole_end = drm_mm_hole_node_end(&mm->head_node);
  464. hole_size = hole_end - hole_start;
  465. if (hole_size)
  466. printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
  467. prefix, hole_start, hole_end,
  468. hole_size);
  469. total_free += hole_size;
  470. drm_mm_for_each_node(entry, mm) {
  471. printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: used\n",
  472. prefix, entry->start, entry->start + entry->size,
  473. entry->size);
  474. total_used += entry->size;
  475. if (entry->hole_follows) {
  476. hole_start = drm_mm_hole_node_start(entry);
  477. hole_end = drm_mm_hole_node_end(entry);
  478. hole_size = hole_end - hole_start;
  479. printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
  480. prefix, hole_start, hole_end,
  481. hole_size);
  482. total_free += hole_size;
  483. }
  484. }
  485. total = total_free + total_used;
  486. printk(KERN_DEBUG "%s total: %lu, used %lu free %lu\n", prefix, total,
  487. total_used, total_free);
  488. }
  489. EXPORT_SYMBOL(drm_mm_debug_table);
  490. #if defined(CONFIG_DEBUG_FS)
  491. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
  492. {
  493. struct drm_mm_node *entry;
  494. unsigned long total_used = 0, total_free = 0, total = 0;
  495. unsigned long hole_start, hole_end, hole_size;
  496. hole_start = drm_mm_hole_node_start(&mm->head_node);
  497. hole_end = drm_mm_hole_node_end(&mm->head_node);
  498. hole_size = hole_end - hole_start;
  499. if (hole_size)
  500. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
  501. hole_start, hole_end, hole_size);
  502. total_free += hole_size;
  503. drm_mm_for_each_node(entry, mm) {
  504. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: used\n",
  505. entry->start, entry->start + entry->size,
  506. entry->size);
  507. total_used += entry->size;
  508. if (entry->hole_follows) {
  509. hole_start = drm_mm_hole_node_start(&mm->head_node);
  510. hole_end = drm_mm_hole_node_end(&mm->head_node);
  511. hole_size = hole_end - hole_start;
  512. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
  513. hole_start, hole_end, hole_size);
  514. total_free += hole_size;
  515. }
  516. }
  517. total = total_free + total_used;
  518. seq_printf(m, "total: %lu, used %lu free %lu\n", total, total_used, total_free);
  519. return 0;
  520. }
  521. EXPORT_SYMBOL(drm_mm_dump_table);
  522. #endif