drm_mm.c 20 KB

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