drm_mm.c 19 KB

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