drm_mm.c 21 KB

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