drm_mm.c 20 KB

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