drm_mm.c 21 KB

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