drm_mm.c 19 KB

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