drm_mm.c 21 KB

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