drm_mm.c 21 KB

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