drm_mm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 <drm/drmP.h>
  43. #include <drm/drm_mm.h>
  44. #include <linux/slab.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/export.h>
  47. #define MM_UNUSED_TARGET 4
  48. static struct drm_mm_node *drm_mm_kmalloc(struct drm_mm *mm, int atomic)
  49. {
  50. struct drm_mm_node *child;
  51. if (atomic)
  52. child = kzalloc(sizeof(*child), GFP_ATOMIC);
  53. else
  54. child = kzalloc(sizeof(*child), GFP_KERNEL);
  55. if (unlikely(child == NULL)) {
  56. spin_lock(&mm->unused_lock);
  57. if (list_empty(&mm->unused_nodes))
  58. child = NULL;
  59. else {
  60. child =
  61. list_entry(mm->unused_nodes.next,
  62. struct drm_mm_node, node_list);
  63. list_del(&child->node_list);
  64. --mm->num_unused;
  65. }
  66. spin_unlock(&mm->unused_lock);
  67. }
  68. return child;
  69. }
  70. /* drm_mm_pre_get() - pre allocate drm_mm_node structure
  71. * drm_mm: memory manager struct we are pre-allocating for
  72. *
  73. * Returns 0 on success or -ENOMEM if allocation fails.
  74. */
  75. int drm_mm_pre_get(struct drm_mm *mm)
  76. {
  77. struct drm_mm_node *node;
  78. spin_lock(&mm->unused_lock);
  79. while (mm->num_unused < MM_UNUSED_TARGET) {
  80. spin_unlock(&mm->unused_lock);
  81. node = kzalloc(sizeof(*node), GFP_KERNEL);
  82. spin_lock(&mm->unused_lock);
  83. if (unlikely(node == NULL)) {
  84. int ret = (mm->num_unused < 2) ? -ENOMEM : 0;
  85. spin_unlock(&mm->unused_lock);
  86. return ret;
  87. }
  88. ++mm->num_unused;
  89. list_add_tail(&node->node_list, &mm->unused_nodes);
  90. }
  91. spin_unlock(&mm->unused_lock);
  92. return 0;
  93. }
  94. EXPORT_SYMBOL(drm_mm_pre_get);
  95. static inline unsigned long drm_mm_hole_node_start(struct drm_mm_node *hole_node)
  96. {
  97. return hole_node->start + hole_node->size;
  98. }
  99. static inline unsigned long drm_mm_hole_node_end(struct drm_mm_node *hole_node)
  100. {
  101. struct drm_mm_node *next_node =
  102. list_entry(hole_node->node_list.next, struct drm_mm_node,
  103. node_list);
  104. return next_node->start;
  105. }
  106. static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
  107. struct drm_mm_node *node,
  108. unsigned long size, unsigned alignment,
  109. unsigned long color)
  110. {
  111. struct drm_mm *mm = hole_node->mm;
  112. unsigned long hole_start = drm_mm_hole_node_start(hole_node);
  113. unsigned long hole_end = drm_mm_hole_node_end(hole_node);
  114. unsigned long adj_start = hole_start;
  115. unsigned long adj_end = hole_end;
  116. BUG_ON(!hole_node->hole_follows || node->allocated);
  117. if (mm->color_adjust)
  118. mm->color_adjust(hole_node, color, &adj_start, &adj_end);
  119. if (alignment) {
  120. unsigned tmp = adj_start % alignment;
  121. if (tmp)
  122. adj_start += alignment - tmp;
  123. }
  124. if (adj_start == hole_start) {
  125. hole_node->hole_follows = 0;
  126. list_del(&hole_node->hole_stack);
  127. }
  128. node->start = adj_start;
  129. node->size = size;
  130. node->mm = mm;
  131. node->color = color;
  132. node->allocated = 1;
  133. INIT_LIST_HEAD(&node->hole_stack);
  134. list_add(&node->node_list, &hole_node->node_list);
  135. BUG_ON(node->start + node->size > adj_end);
  136. node->hole_follows = 0;
  137. if (node->start + node->size < hole_end) {
  138. list_add(&node->hole_stack, &mm->hole_stack);
  139. node->hole_follows = 1;
  140. }
  141. }
  142. struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *hole_node,
  143. unsigned long size,
  144. unsigned alignment,
  145. unsigned long color,
  146. int atomic)
  147. {
  148. struct drm_mm_node *node;
  149. node = drm_mm_kmalloc(hole_node->mm, atomic);
  150. if (unlikely(node == NULL))
  151. return NULL;
  152. drm_mm_insert_helper(hole_node, node, size, alignment, color);
  153. return node;
  154. }
  155. EXPORT_SYMBOL(drm_mm_get_block_generic);
  156. /**
  157. * Search for free space and insert a preallocated memory node. Returns
  158. * -ENOSPC if no suitable free area is available. The preallocated memory node
  159. * must be cleared.
  160. */
  161. int drm_mm_insert_node(struct drm_mm *mm, struct drm_mm_node *node,
  162. unsigned long size, unsigned alignment)
  163. {
  164. struct drm_mm_node *hole_node;
  165. hole_node = drm_mm_search_free(mm, size, alignment, false);
  166. if (!hole_node)
  167. return -ENOSPC;
  168. drm_mm_insert_helper(hole_node, node, size, alignment, 0);
  169. return 0;
  170. }
  171. EXPORT_SYMBOL(drm_mm_insert_node);
  172. static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
  173. struct drm_mm_node *node,
  174. unsigned long size, unsigned alignment,
  175. unsigned long color,
  176. unsigned long start, unsigned long end)
  177. {
  178. struct drm_mm *mm = hole_node->mm;
  179. unsigned long hole_start = drm_mm_hole_node_start(hole_node);
  180. unsigned long hole_end = drm_mm_hole_node_end(hole_node);
  181. unsigned long adj_start = hole_start;
  182. unsigned long adj_end = hole_end;
  183. BUG_ON(!hole_node->hole_follows || node->allocated);
  184. if (mm->color_adjust)
  185. mm->color_adjust(hole_node, color, &adj_start, &adj_end);
  186. if (adj_start < start)
  187. adj_start = start;
  188. if (alignment) {
  189. unsigned tmp = adj_start % alignment;
  190. if (tmp)
  191. adj_start += alignment - tmp;
  192. }
  193. if (adj_start == hole_start) {
  194. hole_node->hole_follows = 0;
  195. list_del(&hole_node->hole_stack);
  196. }
  197. node->start = adj_start;
  198. node->size = size;
  199. node->mm = mm;
  200. node->color = color;
  201. node->allocated = 1;
  202. INIT_LIST_HEAD(&node->hole_stack);
  203. list_add(&node->node_list, &hole_node->node_list);
  204. BUG_ON(node->start + node->size > adj_end);
  205. BUG_ON(node->start + node->size > end);
  206. node->hole_follows = 0;
  207. if (node->start + node->size < hole_end) {
  208. list_add(&node->hole_stack, &mm->hole_stack);
  209. node->hole_follows = 1;
  210. }
  211. }
  212. struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *hole_node,
  213. unsigned long size,
  214. unsigned alignment,
  215. unsigned long color,
  216. unsigned long start,
  217. unsigned long end,
  218. int atomic)
  219. {
  220. struct drm_mm_node *node;
  221. node = drm_mm_kmalloc(hole_node->mm, atomic);
  222. if (unlikely(node == NULL))
  223. return NULL;
  224. drm_mm_insert_helper_range(hole_node, node, size, alignment, color,
  225. start, end);
  226. return node;
  227. }
  228. EXPORT_SYMBOL(drm_mm_get_block_range_generic);
  229. /**
  230. * Search for free space and insert a preallocated memory node. Returns
  231. * -ENOSPC if no suitable free area is available. This is for range
  232. * restricted allocations. The preallocated memory node must be cleared.
  233. */
  234. int drm_mm_insert_node_in_range(struct drm_mm *mm, struct drm_mm_node *node,
  235. unsigned long size, unsigned alignment,
  236. unsigned long start, unsigned long end)
  237. {
  238. struct drm_mm_node *hole_node;
  239. hole_node = drm_mm_search_free_in_range(mm, size, alignment,
  240. start, end, false);
  241. if (!hole_node)
  242. return -ENOSPC;
  243. drm_mm_insert_helper_range(hole_node, node, size, alignment, 0,
  244. start, end);
  245. return 0;
  246. }
  247. EXPORT_SYMBOL(drm_mm_insert_node_in_range);
  248. /**
  249. * Remove a memory node from the allocator.
  250. */
  251. void drm_mm_remove_node(struct drm_mm_node *node)
  252. {
  253. struct drm_mm *mm = node->mm;
  254. struct drm_mm_node *prev_node;
  255. BUG_ON(node->scanned_block || node->scanned_prev_free
  256. || node->scanned_next_free);
  257. prev_node =
  258. list_entry(node->node_list.prev, struct drm_mm_node, node_list);
  259. if (node->hole_follows) {
  260. BUG_ON(drm_mm_hole_node_start(node)
  261. == drm_mm_hole_node_end(node));
  262. list_del(&node->hole_stack);
  263. } else
  264. BUG_ON(drm_mm_hole_node_start(node)
  265. != drm_mm_hole_node_end(node));
  266. if (!prev_node->hole_follows) {
  267. prev_node->hole_follows = 1;
  268. list_add(&prev_node->hole_stack, &mm->hole_stack);
  269. } else
  270. list_move(&prev_node->hole_stack, &mm->hole_stack);
  271. list_del(&node->node_list);
  272. node->allocated = 0;
  273. }
  274. EXPORT_SYMBOL(drm_mm_remove_node);
  275. /*
  276. * Remove a memory node from the allocator and free the allocated struct
  277. * drm_mm_node. Only to be used on a struct drm_mm_node obtained by one of the
  278. * drm_mm_get_block functions.
  279. */
  280. void drm_mm_put_block(struct drm_mm_node *node)
  281. {
  282. struct drm_mm *mm = node->mm;
  283. drm_mm_remove_node(node);
  284. spin_lock(&mm->unused_lock);
  285. if (mm->num_unused < MM_UNUSED_TARGET) {
  286. list_add(&node->node_list, &mm->unused_nodes);
  287. ++mm->num_unused;
  288. } else
  289. kfree(node);
  290. spin_unlock(&mm->unused_lock);
  291. }
  292. EXPORT_SYMBOL(drm_mm_put_block);
  293. static int check_free_hole(unsigned long start, unsigned long end,
  294. unsigned long size, unsigned alignment)
  295. {
  296. if (end - start < size)
  297. return 0;
  298. if (alignment) {
  299. unsigned tmp = start % alignment;
  300. if (tmp)
  301. start += alignment - tmp;
  302. }
  303. return end >= start + size;
  304. }
  305. struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
  306. unsigned long size,
  307. unsigned alignment,
  308. unsigned long color,
  309. bool best_match)
  310. {
  311. struct drm_mm_node *entry;
  312. struct drm_mm_node *best;
  313. unsigned long best_size;
  314. BUG_ON(mm->scanned_blocks);
  315. best = NULL;
  316. best_size = ~0UL;
  317. list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
  318. unsigned long adj_start = drm_mm_hole_node_start(entry);
  319. unsigned long adj_end = drm_mm_hole_node_end(entry);
  320. if (mm->color_adjust) {
  321. mm->color_adjust(entry, color, &adj_start, &adj_end);
  322. if (adj_end <= adj_start)
  323. continue;
  324. }
  325. BUG_ON(!entry->hole_follows);
  326. if (!check_free_hole(adj_start, adj_end, size, alignment))
  327. continue;
  328. if (!best_match)
  329. return entry;
  330. if (entry->size < best_size) {
  331. best = entry;
  332. best_size = entry->size;
  333. }
  334. }
  335. return best;
  336. }
  337. EXPORT_SYMBOL(drm_mm_search_free_generic);
  338. struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
  339. unsigned long size,
  340. unsigned alignment,
  341. unsigned long color,
  342. unsigned long start,
  343. unsigned long end,
  344. bool best_match)
  345. {
  346. struct drm_mm_node *entry;
  347. struct drm_mm_node *best;
  348. unsigned long best_size;
  349. BUG_ON(mm->scanned_blocks);
  350. best = NULL;
  351. best_size = ~0UL;
  352. list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
  353. unsigned long adj_start = drm_mm_hole_node_start(entry) < start ?
  354. start : drm_mm_hole_node_start(entry);
  355. unsigned long adj_end = drm_mm_hole_node_end(entry) > end ?
  356. end : drm_mm_hole_node_end(entry);
  357. BUG_ON(!entry->hole_follows);
  358. if (mm->color_adjust) {
  359. mm->color_adjust(entry, color, &adj_start, &adj_end);
  360. if (adj_end <= adj_start)
  361. continue;
  362. }
  363. if (!check_free_hole(adj_start, adj_end, size, alignment))
  364. continue;
  365. if (!best_match)
  366. return entry;
  367. if (entry->size < best_size) {
  368. best = entry;
  369. best_size = entry->size;
  370. }
  371. }
  372. return best;
  373. }
  374. EXPORT_SYMBOL(drm_mm_search_free_in_range_generic);
  375. /**
  376. * Moves an allocation. To be used with embedded struct drm_mm_node.
  377. */
  378. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
  379. {
  380. list_replace(&old->node_list, &new->node_list);
  381. list_replace(&old->hole_stack, &new->hole_stack);
  382. new->hole_follows = old->hole_follows;
  383. new->mm = old->mm;
  384. new->start = old->start;
  385. new->size = old->size;
  386. new->color = old->color;
  387. old->allocated = 0;
  388. new->allocated = 1;
  389. }
  390. EXPORT_SYMBOL(drm_mm_replace_node);
  391. /**
  392. * Initializa lru scanning.
  393. *
  394. * This simply sets up the scanning routines with the parameters for the desired
  395. * hole.
  396. *
  397. * Warning: As long as the scan list is non-empty, no other operations than
  398. * adding/removing nodes to/from the scan list are allowed.
  399. */
  400. void drm_mm_init_scan(struct drm_mm *mm,
  401. unsigned long size,
  402. unsigned alignment,
  403. unsigned long color)
  404. {
  405. mm->scan_color = color;
  406. mm->scan_alignment = alignment;
  407. mm->scan_size = size;
  408. mm->scanned_blocks = 0;
  409. mm->scan_hit_start = 0;
  410. mm->scan_hit_size = 0;
  411. mm->scan_check_range = 0;
  412. mm->prev_scanned_node = NULL;
  413. }
  414. EXPORT_SYMBOL(drm_mm_init_scan);
  415. /**
  416. * Initializa lru scanning.
  417. *
  418. * This simply sets up the scanning routines with the parameters for the desired
  419. * hole. This version is for range-restricted scans.
  420. *
  421. * Warning: As long as the scan list is non-empty, no other operations than
  422. * adding/removing nodes to/from the scan list are allowed.
  423. */
  424. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  425. unsigned long size,
  426. unsigned alignment,
  427. unsigned long color,
  428. unsigned long start,
  429. unsigned long end)
  430. {
  431. mm->scan_color = color;
  432. mm->scan_alignment = alignment;
  433. mm->scan_size = size;
  434. mm->scanned_blocks = 0;
  435. mm->scan_hit_start = 0;
  436. mm->scan_hit_size = 0;
  437. mm->scan_start = start;
  438. mm->scan_end = end;
  439. mm->scan_check_range = 1;
  440. mm->prev_scanned_node = NULL;
  441. }
  442. EXPORT_SYMBOL(drm_mm_init_scan_with_range);
  443. /**
  444. * Add a node to the scan list that might be freed to make space for the desired
  445. * hole.
  446. *
  447. * Returns non-zero, if a hole has been found, zero otherwise.
  448. */
  449. int drm_mm_scan_add_block(struct drm_mm_node *node)
  450. {
  451. struct drm_mm *mm = node->mm;
  452. struct drm_mm_node *prev_node;
  453. unsigned long hole_start, hole_end;
  454. unsigned long adj_start;
  455. unsigned long adj_end;
  456. mm->scanned_blocks++;
  457. BUG_ON(node->scanned_block);
  458. node->scanned_block = 1;
  459. prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
  460. node_list);
  461. node->scanned_preceeds_hole = prev_node->hole_follows;
  462. prev_node->hole_follows = 1;
  463. list_del(&node->node_list);
  464. node->node_list.prev = &prev_node->node_list;
  465. node->node_list.next = &mm->prev_scanned_node->node_list;
  466. mm->prev_scanned_node = node;
  467. hole_start = drm_mm_hole_node_start(prev_node);
  468. hole_end = drm_mm_hole_node_end(prev_node);
  469. adj_start = hole_start;
  470. adj_end = hole_end;
  471. if (mm->color_adjust)
  472. mm->color_adjust(prev_node, mm->scan_color, &adj_start, &adj_end);
  473. if (mm->scan_check_range) {
  474. if (adj_start < mm->scan_start)
  475. adj_start = mm->scan_start;
  476. if (adj_end > mm->scan_end)
  477. adj_end = mm->scan_end;
  478. }
  479. if (check_free_hole(adj_start, adj_end,
  480. mm->scan_size, mm->scan_alignment)) {
  481. mm->scan_hit_start = hole_start;
  482. mm->scan_hit_size = hole_end;
  483. return 1;
  484. }
  485. return 0;
  486. }
  487. EXPORT_SYMBOL(drm_mm_scan_add_block);
  488. /**
  489. * Remove a node from the scan list.
  490. *
  491. * Nodes _must_ be removed in the exact same order from the scan list as they
  492. * have been added, otherwise the internal state of the memory manager will be
  493. * corrupted.
  494. *
  495. * When the scan list is empty, the selected memory nodes can be freed. An
  496. * immediately following drm_mm_search_free with best_match = 0 will then return
  497. * the just freed block (because its at the top of the free_stack list).
  498. *
  499. * Returns one if this block should be evicted, zero otherwise. Will always
  500. * return zero when no hole has been found.
  501. */
  502. int drm_mm_scan_remove_block(struct drm_mm_node *node)
  503. {
  504. struct drm_mm *mm = node->mm;
  505. struct drm_mm_node *prev_node;
  506. mm->scanned_blocks--;
  507. BUG_ON(!node->scanned_block);
  508. node->scanned_block = 0;
  509. prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
  510. node_list);
  511. prev_node->hole_follows = node->scanned_preceeds_hole;
  512. INIT_LIST_HEAD(&node->node_list);
  513. list_add(&node->node_list, &prev_node->node_list);
  514. /* Only need to check for containement because start&size for the
  515. * complete resulting free block (not just the desired part) is
  516. * stored. */
  517. if (node->start >= mm->scan_hit_start &&
  518. node->start + node->size
  519. <= mm->scan_hit_start + mm->scan_hit_size) {
  520. return 1;
  521. }
  522. return 0;
  523. }
  524. EXPORT_SYMBOL(drm_mm_scan_remove_block);
  525. int drm_mm_clean(struct drm_mm * mm)
  526. {
  527. struct list_head *head = &mm->head_node.node_list;
  528. return (head->next->next == head);
  529. }
  530. EXPORT_SYMBOL(drm_mm_clean);
  531. int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
  532. {
  533. INIT_LIST_HEAD(&mm->hole_stack);
  534. INIT_LIST_HEAD(&mm->unused_nodes);
  535. mm->num_unused = 0;
  536. mm->scanned_blocks = 0;
  537. spin_lock_init(&mm->unused_lock);
  538. /* Clever trick to avoid a special case in the free hole tracking. */
  539. INIT_LIST_HEAD(&mm->head_node.node_list);
  540. INIT_LIST_HEAD(&mm->head_node.hole_stack);
  541. mm->head_node.hole_follows = 1;
  542. mm->head_node.scanned_block = 0;
  543. mm->head_node.scanned_prev_free = 0;
  544. mm->head_node.scanned_next_free = 0;
  545. mm->head_node.mm = mm;
  546. mm->head_node.start = start + size;
  547. mm->head_node.size = start - mm->head_node.start;
  548. list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
  549. mm->color_adjust = NULL;
  550. return 0;
  551. }
  552. EXPORT_SYMBOL(drm_mm_init);
  553. void drm_mm_takedown(struct drm_mm * mm)
  554. {
  555. struct drm_mm_node *entry, *next;
  556. if (!list_empty(&mm->head_node.node_list)) {
  557. DRM_ERROR("Memory manager not clean. Delaying takedown\n");
  558. return;
  559. }
  560. spin_lock(&mm->unused_lock);
  561. list_for_each_entry_safe(entry, next, &mm->unused_nodes, node_list) {
  562. list_del(&entry->node_list);
  563. kfree(entry);
  564. --mm->num_unused;
  565. }
  566. spin_unlock(&mm->unused_lock);
  567. BUG_ON(mm->num_unused != 0);
  568. }
  569. EXPORT_SYMBOL(drm_mm_takedown);
  570. void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
  571. {
  572. struct drm_mm_node *entry;
  573. unsigned long total_used = 0, total_free = 0, total = 0;
  574. unsigned long hole_start, hole_end, hole_size;
  575. hole_start = drm_mm_hole_node_start(&mm->head_node);
  576. hole_end = drm_mm_hole_node_end(&mm->head_node);
  577. hole_size = hole_end - hole_start;
  578. if (hole_size)
  579. printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
  580. prefix, hole_start, hole_end,
  581. hole_size);
  582. total_free += hole_size;
  583. drm_mm_for_each_node(entry, mm) {
  584. printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: used\n",
  585. prefix, entry->start, entry->start + entry->size,
  586. entry->size);
  587. total_used += entry->size;
  588. if (entry->hole_follows) {
  589. hole_start = drm_mm_hole_node_start(entry);
  590. hole_end = drm_mm_hole_node_end(entry);
  591. hole_size = hole_end - hole_start;
  592. printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
  593. prefix, hole_start, hole_end,
  594. hole_size);
  595. total_free += hole_size;
  596. }
  597. }
  598. total = total_free + total_used;
  599. printk(KERN_DEBUG "%s total: %lu, used %lu free %lu\n", prefix, total,
  600. total_used, total_free);
  601. }
  602. EXPORT_SYMBOL(drm_mm_debug_table);
  603. #if defined(CONFIG_DEBUG_FS)
  604. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
  605. {
  606. struct drm_mm_node *entry;
  607. unsigned long total_used = 0, total_free = 0, total = 0;
  608. unsigned long hole_start, hole_end, hole_size;
  609. hole_start = drm_mm_hole_node_start(&mm->head_node);
  610. hole_end = drm_mm_hole_node_end(&mm->head_node);
  611. hole_size = hole_end - hole_start;
  612. if (hole_size)
  613. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
  614. hole_start, hole_end, hole_size);
  615. total_free += hole_size;
  616. drm_mm_for_each_node(entry, mm) {
  617. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: used\n",
  618. entry->start, entry->start + entry->size,
  619. entry->size);
  620. total_used += entry->size;
  621. if (entry->hole_follows) {
  622. hole_start = drm_mm_hole_node_start(entry);
  623. hole_end = drm_mm_hole_node_end(entry);
  624. hole_size = hole_end - hole_start;
  625. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
  626. hole_start, hole_end, hole_size);
  627. total_free += hole_size;
  628. }
  629. }
  630. total = total_free + total_used;
  631. seq_printf(m, "total: %lu, used %lu free %lu\n", total, total_used, total_free);
  632. return 0;
  633. }
  634. EXPORT_SYMBOL(drm_mm_dump_table);
  635. #endif