drm_mm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. /**
  48. * DOC: Overview
  49. *
  50. * drm_mm provides a simple range allocator. The drivers are free to use the
  51. * resource allocator from the linux core if it suits them, the upside of drm_mm
  52. * is that it's in the DRM core. Which means that it's easier to extend for
  53. * some of the crazier special purpose needs of gpus.
  54. *
  55. * The main data struct is &drm_mm, allocations are tracked in &drm_mm_node.
  56. * Drivers are free to embed either of them into their own suitable
  57. * datastructures. drm_mm itself will not do any allocations of its own, so if
  58. * drivers choose not to embed nodes they need to still allocate them
  59. * themselves.
  60. *
  61. * The range allocator also supports reservation of preallocated blocks. This is
  62. * useful for taking over initial mode setting configurations from the firmware,
  63. * where an object needs to be created which exactly matches the firmware's
  64. * scanout target. As long as the range is still free it can be inserted anytime
  65. * after the allocator is initialized, which helps with avoiding looped
  66. * depencies in the driver load sequence.
  67. *
  68. * drm_mm maintains a stack of most recently freed holes, which of all
  69. * simplistic datastructures seems to be a fairly decent approach to clustering
  70. * allocations and avoiding too much fragmentation. This means free space
  71. * searches are O(num_holes). Given that all the fancy features drm_mm supports
  72. * something better would be fairly complex and since gfx thrashing is a fairly
  73. * steep cliff not a real concern. Removing a node again is O(1).
  74. *
  75. * drm_mm supports a few features: Alignment and range restrictions can be
  76. * supplied. Further more every &drm_mm_node has a color value (which is just an
  77. * opaqua unsigned long) which in conjunction with a driver callback can be used
  78. * to implement sophisticated placement restrictions. The i915 DRM driver uses
  79. * this to implement guard pages between incompatible caching domains in the
  80. * graphics TT.
  81. *
  82. * Finally iteration helpers to walk all nodes and all holes are provided as are
  83. * some basic allocator dumpers for debugging.
  84. */
  85. static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
  86. unsigned long size,
  87. unsigned alignment,
  88. unsigned long color,
  89. enum drm_mm_search_flags flags);
  90. static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
  91. unsigned long size,
  92. unsigned alignment,
  93. unsigned long color,
  94. unsigned long start,
  95. unsigned long end,
  96. enum drm_mm_search_flags flags);
  97. static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
  98. struct drm_mm_node *node,
  99. unsigned long size, unsigned alignment,
  100. unsigned long color)
  101. {
  102. struct drm_mm *mm = hole_node->mm;
  103. unsigned long hole_start = drm_mm_hole_node_start(hole_node);
  104. unsigned long hole_end = drm_mm_hole_node_end(hole_node);
  105. unsigned long adj_start = hole_start;
  106. unsigned long adj_end = hole_end;
  107. BUG_ON(node->allocated);
  108. if (mm->color_adjust)
  109. mm->color_adjust(hole_node, color, &adj_start, &adj_end);
  110. if (alignment) {
  111. unsigned tmp = adj_start % alignment;
  112. if (tmp)
  113. adj_start += alignment - tmp;
  114. }
  115. if (adj_start == hole_start) {
  116. hole_node->hole_follows = 0;
  117. list_del(&hole_node->hole_stack);
  118. }
  119. node->start = adj_start;
  120. node->size = size;
  121. node->mm = mm;
  122. node->color = color;
  123. node->allocated = 1;
  124. INIT_LIST_HEAD(&node->hole_stack);
  125. list_add(&node->node_list, &hole_node->node_list);
  126. BUG_ON(node->start + node->size > adj_end);
  127. node->hole_follows = 0;
  128. if (__drm_mm_hole_node_start(node) < hole_end) {
  129. list_add(&node->hole_stack, &mm->hole_stack);
  130. node->hole_follows = 1;
  131. }
  132. }
  133. /**
  134. * drm_mm_reserve_node - insert an pre-initialized node
  135. * @mm: drm_mm allocator to insert @node into
  136. * @node: drm_mm_node to insert
  137. *
  138. * This functions inserts an already set-up drm_mm_node into the allocator,
  139. * meaning that start, size and color must be set by the caller. This is useful
  140. * to initialize the allocator with preallocated objects which must be set-up
  141. * before the range allocator can be set-up, e.g. when taking over a firmware
  142. * framebuffer.
  143. *
  144. * Returns:
  145. * 0 on success, -ENOSPC if there's no hole where @node is.
  146. */
  147. int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
  148. {
  149. struct drm_mm_node *hole;
  150. unsigned long end = node->start + node->size;
  151. unsigned long hole_start;
  152. unsigned long hole_end;
  153. BUG_ON(node == NULL);
  154. /* Find the relevant hole to add our node to */
  155. drm_mm_for_each_hole(hole, mm, hole_start, hole_end) {
  156. if (hole_start > node->start || hole_end < end)
  157. continue;
  158. node->mm = mm;
  159. node->allocated = 1;
  160. INIT_LIST_HEAD(&node->hole_stack);
  161. list_add(&node->node_list, &hole->node_list);
  162. if (node->start == hole_start) {
  163. hole->hole_follows = 0;
  164. list_del_init(&hole->hole_stack);
  165. }
  166. node->hole_follows = 0;
  167. if (end != hole_end) {
  168. list_add(&node->hole_stack, &mm->hole_stack);
  169. node->hole_follows = 1;
  170. }
  171. return 0;
  172. }
  173. WARN(1, "no hole found for node 0x%lx + 0x%lx\n",
  174. node->start, node->size);
  175. return -ENOSPC;
  176. }
  177. EXPORT_SYMBOL(drm_mm_reserve_node);
  178. /**
  179. * drm_mm_insert_node_generic - search for space and insert @node
  180. * @mm: drm_mm to allocate from
  181. * @node: preallocate node to insert
  182. * @size: size of the allocation
  183. * @alignment: alignment of the allocation
  184. * @color: opaque tag value to use for this node
  185. * @flags: flags to fine-tune the allocation
  186. *
  187. * The preallocated node must be cleared to 0.
  188. *
  189. * Returns:
  190. * 0 on success, -ENOSPC if there's no suitable hole.
  191. */
  192. int drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
  193. unsigned long size, unsigned alignment,
  194. unsigned long color,
  195. enum drm_mm_search_flags flags)
  196. {
  197. struct drm_mm_node *hole_node;
  198. hole_node = drm_mm_search_free_generic(mm, size, alignment,
  199. color, flags);
  200. if (!hole_node)
  201. return -ENOSPC;
  202. drm_mm_insert_helper(hole_node, node, size, alignment, color);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(drm_mm_insert_node_generic);
  206. static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
  207. struct drm_mm_node *node,
  208. unsigned long size, unsigned alignment,
  209. unsigned long color,
  210. unsigned long start, unsigned long end)
  211. {
  212. struct drm_mm *mm = hole_node->mm;
  213. unsigned long hole_start = drm_mm_hole_node_start(hole_node);
  214. unsigned long hole_end = drm_mm_hole_node_end(hole_node);
  215. unsigned long adj_start = hole_start;
  216. unsigned long adj_end = hole_end;
  217. BUG_ON(!hole_node->hole_follows || node->allocated);
  218. if (adj_start < start)
  219. adj_start = start;
  220. if (adj_end > end)
  221. adj_end = end;
  222. if (mm->color_adjust)
  223. mm->color_adjust(hole_node, color, &adj_start, &adj_end);
  224. if (alignment) {
  225. unsigned tmp = adj_start % alignment;
  226. if (tmp)
  227. adj_start += alignment - tmp;
  228. }
  229. if (adj_start == hole_start) {
  230. hole_node->hole_follows = 0;
  231. list_del(&hole_node->hole_stack);
  232. }
  233. node->start = adj_start;
  234. node->size = size;
  235. node->mm = mm;
  236. node->color = color;
  237. node->allocated = 1;
  238. INIT_LIST_HEAD(&node->hole_stack);
  239. list_add(&node->node_list, &hole_node->node_list);
  240. BUG_ON(node->start + node->size > adj_end);
  241. BUG_ON(node->start + node->size > end);
  242. node->hole_follows = 0;
  243. if (__drm_mm_hole_node_start(node) < hole_end) {
  244. list_add(&node->hole_stack, &mm->hole_stack);
  245. node->hole_follows = 1;
  246. }
  247. }
  248. /**
  249. * drm_mm_insert_node_in_range_generic - ranged search for space and insert @node
  250. * @mm: drm_mm to allocate from
  251. * @node: preallocate node to insert
  252. * @size: size of the allocation
  253. * @alignment: alignment of the allocation
  254. * @color: opaque tag value to use for this node
  255. * @start: start of the allowed range for this node
  256. * @end: end of the allowed range for this node
  257. * @flags: flags to fine-tune the allocation
  258. *
  259. * The preallocated node must be cleared to 0.
  260. *
  261. * Returns:
  262. * 0 on success, -ENOSPC if there's no suitable hole.
  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. enum drm_mm_search_flags flags)
  268. {
  269. struct drm_mm_node *hole_node;
  270. hole_node = drm_mm_search_free_in_range_generic(mm,
  271. size, alignment, color,
  272. start, end, flags);
  273. if (!hole_node)
  274. return -ENOSPC;
  275. drm_mm_insert_helper_range(hole_node, node,
  276. size, alignment, color,
  277. start, end);
  278. return 0;
  279. }
  280. EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
  281. /**
  282. * drm_mm_remove_node - Remove a memory node from the allocator.
  283. * @node: drm_mm_node to remove
  284. *
  285. * This just removes a node from its drm_mm allocator. The node does not need to
  286. * be cleared again before it can be re-inserted into this or any other drm_mm
  287. * allocator. It is a bug to call this function on a un-allocated node.
  288. */
  289. void drm_mm_remove_node(struct drm_mm_node *node)
  290. {
  291. struct drm_mm *mm = node->mm;
  292. struct drm_mm_node *prev_node;
  293. if (WARN_ON(!node->allocated))
  294. return;
  295. BUG_ON(node->scanned_block || node->scanned_prev_free
  296. || node->scanned_next_free);
  297. prev_node =
  298. list_entry(node->node_list.prev, struct drm_mm_node, node_list);
  299. if (node->hole_follows) {
  300. BUG_ON(__drm_mm_hole_node_start(node) ==
  301. __drm_mm_hole_node_end(node));
  302. list_del(&node->hole_stack);
  303. } else
  304. BUG_ON(__drm_mm_hole_node_start(node) !=
  305. __drm_mm_hole_node_end(node));
  306. if (!prev_node->hole_follows) {
  307. prev_node->hole_follows = 1;
  308. list_add(&prev_node->hole_stack, &mm->hole_stack);
  309. } else
  310. list_move(&prev_node->hole_stack, &mm->hole_stack);
  311. list_del(&node->node_list);
  312. node->allocated = 0;
  313. }
  314. EXPORT_SYMBOL(drm_mm_remove_node);
  315. static int check_free_hole(unsigned long start, unsigned long end,
  316. unsigned long size, unsigned alignment)
  317. {
  318. if (end - start < size)
  319. return 0;
  320. if (alignment) {
  321. unsigned tmp = start % alignment;
  322. if (tmp)
  323. start += alignment - tmp;
  324. }
  325. return end >= start + size;
  326. }
  327. static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
  328. unsigned long size,
  329. unsigned alignment,
  330. unsigned long color,
  331. enum drm_mm_search_flags flags)
  332. {
  333. struct drm_mm_node *entry;
  334. struct drm_mm_node *best;
  335. unsigned long adj_start;
  336. unsigned long adj_end;
  337. unsigned long best_size;
  338. BUG_ON(mm->scanned_blocks);
  339. best = NULL;
  340. best_size = ~0UL;
  341. drm_mm_for_each_hole(entry, mm, adj_start, adj_end) {
  342. if (mm->color_adjust) {
  343. mm->color_adjust(entry, color, &adj_start, &adj_end);
  344. if (adj_end <= adj_start)
  345. continue;
  346. }
  347. if (!check_free_hole(adj_start, adj_end, size, alignment))
  348. continue;
  349. if (!(flags & DRM_MM_SEARCH_BEST))
  350. return entry;
  351. if (entry->size < best_size) {
  352. best = entry;
  353. best_size = entry->size;
  354. }
  355. }
  356. return best;
  357. }
  358. static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
  359. unsigned long size,
  360. unsigned alignment,
  361. unsigned long color,
  362. unsigned long start,
  363. unsigned long end,
  364. enum drm_mm_search_flags flags)
  365. {
  366. struct drm_mm_node *entry;
  367. struct drm_mm_node *best;
  368. unsigned long adj_start;
  369. unsigned long adj_end;
  370. unsigned long best_size;
  371. BUG_ON(mm->scanned_blocks);
  372. best = NULL;
  373. best_size = ~0UL;
  374. drm_mm_for_each_hole(entry, mm, adj_start, adj_end) {
  375. if (adj_start < start)
  376. adj_start = start;
  377. if (adj_end > end)
  378. adj_end = end;
  379. if (mm->color_adjust) {
  380. mm->color_adjust(entry, color, &adj_start, &adj_end);
  381. if (adj_end <= adj_start)
  382. continue;
  383. }
  384. if (!check_free_hole(adj_start, adj_end, size, alignment))
  385. continue;
  386. if (!(flags & DRM_MM_SEARCH_BEST))
  387. return entry;
  388. if (entry->size < best_size) {
  389. best = entry;
  390. best_size = entry->size;
  391. }
  392. }
  393. return best;
  394. }
  395. /**
  396. * drm_mm_replace_node - move an allocation from @old to @new
  397. * @old: drm_mm_node to remove from the allocator
  398. * @new: drm_mm_node which should inherit @old's allocation
  399. *
  400. * This is useful for when drivers embed the drm_mm_node structure and hence
  401. * can't move allocations by reassigning pointers. It's a combination of remove
  402. * and insert with the guarantee that the allocation start will match.
  403. */
  404. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
  405. {
  406. list_replace(&old->node_list, &new->node_list);
  407. list_replace(&old->hole_stack, &new->hole_stack);
  408. new->hole_follows = old->hole_follows;
  409. new->mm = old->mm;
  410. new->start = old->start;
  411. new->size = old->size;
  412. new->color = old->color;
  413. old->allocated = 0;
  414. new->allocated = 1;
  415. }
  416. EXPORT_SYMBOL(drm_mm_replace_node);
  417. /**
  418. * DOC: lru scan roaster
  419. *
  420. * Very often GPUs need to have continuous allocations for a given object. When
  421. * evicting objects to make space for a new one it is therefore not most
  422. * efficient when we simply start to select all objects from the tail of an LRU
  423. * until there's a suitable hole: Especially for big objects or nodes that
  424. * otherwise have special allocation constraints there's a good chance we evict
  425. * lots of (smaller) objects unecessarily.
  426. *
  427. * The DRM range allocator supports this use-case through the scanning
  428. * interfaces. First a scan operation needs to be initialized with
  429. * drm_mm_init_scan() or drm_mm_init_scan_with_range(). The the driver adds
  430. * objects to the roaster (probably by walking an LRU list, but this can be
  431. * freely implemented) until a suitable hole is found or there's no further
  432. * evitable object.
  433. *
  434. * The the driver must walk through all objects again in exactly the reverse
  435. * order to restore the allocator state. Note that while the allocator is used
  436. * in the scan mode no other operation is allowed.
  437. *
  438. * Finally the driver evicts all objects selected in the scan. Adding and
  439. * removing an object is O(1), and since freeing a node is also O(1) the overall
  440. * complexity is O(scanned_objects). So like the free stack which needs to be
  441. * walked before a scan operation even begins this is linear in the number of
  442. * objects. It doesn't seem to hurt badly.
  443. */
  444. /**
  445. * drm_mm_init_scan - initialize lru scanning
  446. * @mm: drm_mm to scan
  447. * @size: size of the allocation
  448. * @alignment: alignment of the allocation
  449. * @color: opaque tag value to use for the allocation
  450. *
  451. * This simply sets up the scanning routines with the parameters for the desired
  452. * hole. Note that there's no need to specify allocation flags, since they only
  453. * change the place a node is allocated from within a suitable hole.
  454. *
  455. * Warning:
  456. * As long as the scan list is non-empty, no other operations than
  457. * adding/removing nodes to/from the scan list are allowed.
  458. */
  459. void drm_mm_init_scan(struct drm_mm *mm,
  460. unsigned long size,
  461. unsigned alignment,
  462. unsigned long color)
  463. {
  464. mm->scan_color = color;
  465. mm->scan_alignment = alignment;
  466. mm->scan_size = size;
  467. mm->scanned_blocks = 0;
  468. mm->scan_hit_start = 0;
  469. mm->scan_hit_end = 0;
  470. mm->scan_check_range = 0;
  471. mm->prev_scanned_node = NULL;
  472. }
  473. EXPORT_SYMBOL(drm_mm_init_scan);
  474. /**
  475. * drm_mm_init_scan - initialize range-restricted lru scanning
  476. * @mm: drm_mm to scan
  477. * @size: size of the allocation
  478. * @alignment: alignment of the allocation
  479. * @color: opaque tag value to use for the allocation
  480. * @start: start of the allowed range for the allocation
  481. * @end: end of the allowed range for the allocation
  482. *
  483. * This simply sets up the scanning routines with the parameters for the desired
  484. * hole. Note that there's no need to specify allocation flags, since they only
  485. * change the place a node is allocated from within a suitable hole.
  486. *
  487. * Warning:
  488. * As long as the scan list is non-empty, no other operations than
  489. * adding/removing nodes to/from the scan list are allowed.
  490. */
  491. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  492. unsigned long size,
  493. unsigned alignment,
  494. unsigned long color,
  495. unsigned long start,
  496. unsigned long end)
  497. {
  498. mm->scan_color = color;
  499. mm->scan_alignment = alignment;
  500. mm->scan_size = size;
  501. mm->scanned_blocks = 0;
  502. mm->scan_hit_start = 0;
  503. mm->scan_hit_end = 0;
  504. mm->scan_start = start;
  505. mm->scan_end = end;
  506. mm->scan_check_range = 1;
  507. mm->prev_scanned_node = NULL;
  508. }
  509. EXPORT_SYMBOL(drm_mm_init_scan_with_range);
  510. /**
  511. * drm_mm_scan_add_block - add a node to the scan list
  512. * @node: drm_mm_node to add
  513. *
  514. * Add a node to the scan list that might be freed to make space for the desired
  515. * hole.
  516. *
  517. * Returns:
  518. * True if a hole has been found, false otherwise.
  519. */
  520. bool drm_mm_scan_add_block(struct drm_mm_node *node)
  521. {
  522. struct drm_mm *mm = node->mm;
  523. struct drm_mm_node *prev_node;
  524. unsigned long hole_start, hole_end;
  525. unsigned long adj_start, adj_end;
  526. mm->scanned_blocks++;
  527. BUG_ON(node->scanned_block);
  528. node->scanned_block = 1;
  529. prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
  530. node_list);
  531. node->scanned_preceeds_hole = prev_node->hole_follows;
  532. prev_node->hole_follows = 1;
  533. list_del(&node->node_list);
  534. node->node_list.prev = &prev_node->node_list;
  535. node->node_list.next = &mm->prev_scanned_node->node_list;
  536. mm->prev_scanned_node = node;
  537. adj_start = hole_start = drm_mm_hole_node_start(prev_node);
  538. adj_end = hole_end = drm_mm_hole_node_end(prev_node);
  539. if (mm->scan_check_range) {
  540. if (adj_start < mm->scan_start)
  541. adj_start = mm->scan_start;
  542. if (adj_end > mm->scan_end)
  543. adj_end = mm->scan_end;
  544. }
  545. if (mm->color_adjust)
  546. mm->color_adjust(prev_node, mm->scan_color,
  547. &adj_start, &adj_end);
  548. if (check_free_hole(adj_start, adj_end,
  549. mm->scan_size, mm->scan_alignment)) {
  550. mm->scan_hit_start = hole_start;
  551. mm->scan_hit_end = hole_end;
  552. return true;
  553. }
  554. return false;
  555. }
  556. EXPORT_SYMBOL(drm_mm_scan_add_block);
  557. /**
  558. * drm_mm_scan_remove_block - remove a node from the scan list
  559. * @node: drm_mm_node to remove
  560. *
  561. * Nodes _must_ be removed in the exact same order from the scan list as they
  562. * have been added, otherwise the internal state of the memory manager will be
  563. * corrupted.
  564. *
  565. * When the scan list is empty, the selected memory nodes can be freed. An
  566. * immediately following drm_mm_search_free with !DRM_MM_SEARCH_BEST will then
  567. * return the just freed block (because its at the top of the free_stack list).
  568. *
  569. * Returns:
  570. * True if this block should be evicted, false otherwise. Will always
  571. * return false when no hole has been found.
  572. */
  573. bool drm_mm_scan_remove_block(struct drm_mm_node *node)
  574. {
  575. struct drm_mm *mm = node->mm;
  576. struct drm_mm_node *prev_node;
  577. mm->scanned_blocks--;
  578. BUG_ON(!node->scanned_block);
  579. node->scanned_block = 0;
  580. prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
  581. node_list);
  582. prev_node->hole_follows = node->scanned_preceeds_hole;
  583. list_add(&node->node_list, &prev_node->node_list);
  584. return (drm_mm_hole_node_end(node) > mm->scan_hit_start &&
  585. node->start < mm->scan_hit_end);
  586. }
  587. EXPORT_SYMBOL(drm_mm_scan_remove_block);
  588. /**
  589. * drm_mm_clean - checks whether an allocator is clean
  590. * @mm: drm_mm allocator to check
  591. *
  592. * Returns:
  593. * True if the allocator is completely free, false if there's still a node
  594. * allocated in it.
  595. */
  596. bool drm_mm_clean(struct drm_mm * mm)
  597. {
  598. struct list_head *head = &mm->head_node.node_list;
  599. return (head->next->next == head);
  600. }
  601. EXPORT_SYMBOL(drm_mm_clean);
  602. /**
  603. * drm_mm_init - initialize a drm-mm allocator
  604. * @mm: the drm_mm structure to initialize
  605. * @start: start of the range managed by @mm
  606. * @size: end of the range managed by @mm
  607. *
  608. * Note that @mm must be cleared to 0 before calling this function.
  609. */
  610. void drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
  611. {
  612. INIT_LIST_HEAD(&mm->hole_stack);
  613. mm->scanned_blocks = 0;
  614. /* Clever trick to avoid a special case in the free hole tracking. */
  615. INIT_LIST_HEAD(&mm->head_node.node_list);
  616. INIT_LIST_HEAD(&mm->head_node.hole_stack);
  617. mm->head_node.hole_follows = 1;
  618. mm->head_node.scanned_block = 0;
  619. mm->head_node.scanned_prev_free = 0;
  620. mm->head_node.scanned_next_free = 0;
  621. mm->head_node.mm = mm;
  622. mm->head_node.start = start + size;
  623. mm->head_node.size = start - mm->head_node.start;
  624. list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
  625. mm->color_adjust = NULL;
  626. }
  627. EXPORT_SYMBOL(drm_mm_init);
  628. /**
  629. * drm_mm_takedown - clean up a drm_mm allocator
  630. * @mm: drm_mm allocator to clean up
  631. *
  632. * Note that it is a bug to call this function on an allocator which is not
  633. * clean.
  634. */
  635. void drm_mm_takedown(struct drm_mm * mm)
  636. {
  637. WARN(!list_empty(&mm->head_node.node_list),
  638. "Memory manager not clean during takedown.\n");
  639. }
  640. EXPORT_SYMBOL(drm_mm_takedown);
  641. static unsigned long drm_mm_debug_hole(struct drm_mm_node *entry,
  642. const char *prefix)
  643. {
  644. unsigned long hole_start, hole_end, hole_size;
  645. if (entry->hole_follows) {
  646. hole_start = drm_mm_hole_node_start(entry);
  647. hole_end = drm_mm_hole_node_end(entry);
  648. hole_size = hole_end - hole_start;
  649. printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
  650. prefix, hole_start, hole_end,
  651. hole_size);
  652. return hole_size;
  653. }
  654. return 0;
  655. }
  656. /**
  657. * drm_mm_debug_table - dump allocator state to dmesg
  658. * @mm: drm_mm allocator to dump
  659. * @prefix: prefix to use for dumping to dmesg
  660. */
  661. void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
  662. {
  663. struct drm_mm_node *entry;
  664. unsigned long total_used = 0, total_free = 0, total = 0;
  665. total_free += drm_mm_debug_hole(&mm->head_node, prefix);
  666. drm_mm_for_each_node(entry, mm) {
  667. printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: used\n",
  668. prefix, entry->start, entry->start + entry->size,
  669. entry->size);
  670. total_used += entry->size;
  671. total_free += drm_mm_debug_hole(entry, prefix);
  672. }
  673. total = total_free + total_used;
  674. printk(KERN_DEBUG "%s total: %lu, used %lu free %lu\n", prefix, total,
  675. total_used, total_free);
  676. }
  677. EXPORT_SYMBOL(drm_mm_debug_table);
  678. #if defined(CONFIG_DEBUG_FS)
  679. static unsigned long drm_mm_dump_hole(struct seq_file *m, struct drm_mm_node *entry)
  680. {
  681. unsigned long hole_start, hole_end, hole_size;
  682. if (entry->hole_follows) {
  683. hole_start = drm_mm_hole_node_start(entry);
  684. hole_end = drm_mm_hole_node_end(entry);
  685. hole_size = hole_end - hole_start;
  686. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
  687. hole_start, hole_end, hole_size);
  688. return hole_size;
  689. }
  690. return 0;
  691. }
  692. /**
  693. * drm_mm_dump_table - dump allocator state to a seq_file
  694. * @m: seq_file to dump to
  695. * @mm: drm_mm allocator to dump
  696. */
  697. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
  698. {
  699. struct drm_mm_node *entry;
  700. unsigned long total_used = 0, total_free = 0, total = 0;
  701. total_free += drm_mm_dump_hole(m, &mm->head_node);
  702. drm_mm_for_each_node(entry, mm) {
  703. seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: used\n",
  704. entry->start, entry->start + entry->size,
  705. entry->size);
  706. total_used += entry->size;
  707. total_free += drm_mm_dump_hole(m, entry);
  708. }
  709. total = total_free + total_used;
  710. seq_printf(m, "total: %lu, used %lu free %lu\n", total, total_used, total_free);
  711. return 0;
  712. }
  713. EXPORT_SYMBOL(drm_mm_dump_table);
  714. #endif