drm_mm.c 20 KB

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