drm_mm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /**************************************************************************
  2. *
  3. * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
  4. * Copyright 2016 Intel Corporation
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. *
  28. **************************************************************************/
  29. /*
  30. * Generic simple memory manager implementation. Intended to be used as a base
  31. * class implementation for more advanced memory managers.
  32. *
  33. * Note that the algorithm used is quite simple and there might be substantial
  34. * performance gains if a smarter free list is implemented. Currently it is
  35. * just an unordered stack of free regions. This could easily be improved if
  36. * an RB-tree is used instead. At least if we expect heavy fragmentation.
  37. *
  38. * Aligned allocations can also see improvement.
  39. *
  40. * Authors:
  41. * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
  42. */
  43. #include <drm/drmP.h>
  44. #include <drm/drm_mm.h>
  45. #include <linux/slab.h>
  46. #include <linux/seq_file.h>
  47. #include <linux/export.h>
  48. #include <linux/interval_tree_generic.h>
  49. /**
  50. * DOC: Overview
  51. *
  52. * drm_mm provides a simple range allocator. The drivers are free to use the
  53. * resource allocator from the linux core if it suits them, the upside of drm_mm
  54. * is that it's in the DRM core. Which means that it's easier to extend for
  55. * some of the crazier special purpose needs of gpus.
  56. *
  57. * The main data struct is &drm_mm, allocations are tracked in &drm_mm_node.
  58. * Drivers are free to embed either of them into their own suitable
  59. * datastructures. drm_mm itself will not do any memory allocations of its own,
  60. * so if drivers choose not to embed nodes they need to still allocate them
  61. * themselves.
  62. *
  63. * The range allocator also supports reservation of preallocated blocks. This is
  64. * useful for taking over initial mode setting configurations from the firmware,
  65. * where an object needs to be created which exactly matches the firmware's
  66. * scanout target. As long as the range is still free it can be inserted anytime
  67. * after the allocator is initialized, which helps with avoiding looped
  68. * dependencies in the driver load sequence.
  69. *
  70. * drm_mm maintains a stack of most recently freed holes, which of all
  71. * simplistic datastructures seems to be a fairly decent approach to clustering
  72. * allocations and avoiding too much fragmentation. This means free space
  73. * searches are O(num_holes). Given that all the fancy features drm_mm supports
  74. * something better would be fairly complex and since gfx thrashing is a fairly
  75. * steep cliff not a real concern. Removing a node again is O(1).
  76. *
  77. * drm_mm supports a few features: Alignment and range restrictions can be
  78. * supplied. Furthermore every &drm_mm_node has a color value (which is just an
  79. * opaque unsigned long) which in conjunction with a driver callback can be used
  80. * to implement sophisticated placement restrictions. The i915 DRM driver uses
  81. * this to implement guard pages between incompatible caching domains in the
  82. * graphics TT.
  83. *
  84. * Two behaviors are supported for searching and allocating: bottom-up and
  85. * top-down. The default is bottom-up. Top-down allocation can be used if the
  86. * memory area has different restrictions, or just to reduce fragmentation.
  87. *
  88. * Finally iteration helpers to walk all nodes and all holes are provided as are
  89. * some basic allocator dumpers for debugging.
  90. *
  91. * Note that this range allocator is not thread-safe, drivers need to protect
  92. * modifications with their on locking. The idea behind this is that for a full
  93. * memory manager additional data needs to be protected anyway, hence internal
  94. * locking would be fully redundant.
  95. */
  96. static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
  97. u64 size,
  98. u64 alignment,
  99. unsigned long color,
  100. u64 start,
  101. u64 end,
  102. enum drm_mm_search_flags flags);
  103. #ifdef CONFIG_DRM_DEBUG_MM
  104. #include <linux/stackdepot.h>
  105. #define STACKDEPTH 32
  106. #define BUFSZ 4096
  107. static noinline void save_stack(struct drm_mm_node *node)
  108. {
  109. unsigned long entries[STACKDEPTH];
  110. struct stack_trace trace = {
  111. .entries = entries,
  112. .max_entries = STACKDEPTH,
  113. .skip = 1
  114. };
  115. save_stack_trace(&trace);
  116. if (trace.nr_entries != 0 &&
  117. trace.entries[trace.nr_entries-1] == ULONG_MAX)
  118. trace.nr_entries--;
  119. /* May be called under spinlock, so avoid sleeping */
  120. node->stack = depot_save_stack(&trace, GFP_NOWAIT);
  121. }
  122. static void show_leaks(struct drm_mm *mm)
  123. {
  124. struct drm_mm_node *node;
  125. unsigned long entries[STACKDEPTH];
  126. char *buf;
  127. buf = kmalloc(BUFSZ, GFP_KERNEL);
  128. if (!buf)
  129. return;
  130. list_for_each_entry(node, drm_mm_nodes(mm), node_list) {
  131. struct stack_trace trace = {
  132. .entries = entries,
  133. .max_entries = STACKDEPTH
  134. };
  135. if (!node->stack) {
  136. DRM_ERROR("node [%08llx + %08llx]: unknown owner\n",
  137. node->start, node->size);
  138. continue;
  139. }
  140. depot_fetch_stack(node->stack, &trace);
  141. snprint_stack_trace(buf, BUFSZ, &trace, 0);
  142. DRM_ERROR("node [%08llx + %08llx]: inserted at\n%s",
  143. node->start, node->size, buf);
  144. }
  145. kfree(buf);
  146. }
  147. #undef STACKDEPTH
  148. #undef BUFSZ
  149. #else
  150. static void save_stack(struct drm_mm_node *node) { }
  151. static void show_leaks(struct drm_mm *mm) { }
  152. #endif
  153. #define START(node) ((node)->start)
  154. #define LAST(node) ((node)->start + (node)->size - 1)
  155. INTERVAL_TREE_DEFINE(struct drm_mm_node, rb,
  156. u64, __subtree_last,
  157. START, LAST, static inline, drm_mm_interval_tree)
  158. struct drm_mm_node *
  159. __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last)
  160. {
  161. return drm_mm_interval_tree_iter_first((struct rb_root *)&mm->interval_tree,
  162. start, last);
  163. }
  164. EXPORT_SYMBOL(__drm_mm_interval_first);
  165. static void drm_mm_interval_tree_add_node(struct drm_mm_node *hole_node,
  166. struct drm_mm_node *node)
  167. {
  168. struct drm_mm *mm = hole_node->mm;
  169. struct rb_node **link, *rb;
  170. struct drm_mm_node *parent;
  171. node->__subtree_last = LAST(node);
  172. if (hole_node->allocated) {
  173. rb = &hole_node->rb;
  174. while (rb) {
  175. parent = rb_entry(rb, struct drm_mm_node, rb);
  176. if (parent->__subtree_last >= node->__subtree_last)
  177. break;
  178. parent->__subtree_last = node->__subtree_last;
  179. rb = rb_parent(rb);
  180. }
  181. rb = &hole_node->rb;
  182. link = &hole_node->rb.rb_right;
  183. } else {
  184. rb = NULL;
  185. link = &mm->interval_tree.rb_node;
  186. }
  187. while (*link) {
  188. rb = *link;
  189. parent = rb_entry(rb, struct drm_mm_node, rb);
  190. if (parent->__subtree_last < node->__subtree_last)
  191. parent->__subtree_last = node->__subtree_last;
  192. if (node->start < parent->start)
  193. link = &parent->rb.rb_left;
  194. else
  195. link = &parent->rb.rb_right;
  196. }
  197. rb_link_node(&node->rb, rb, link);
  198. rb_insert_augmented(&node->rb,
  199. &mm->interval_tree,
  200. &drm_mm_interval_tree_augment);
  201. }
  202. static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
  203. struct drm_mm_node *node,
  204. u64 size, u64 alignment,
  205. unsigned long color,
  206. u64 range_start, u64 range_end,
  207. enum drm_mm_allocator_flags flags)
  208. {
  209. struct drm_mm *mm = hole_node->mm;
  210. u64 hole_start = drm_mm_hole_node_start(hole_node);
  211. u64 hole_end = drm_mm_hole_node_end(hole_node);
  212. u64 adj_start = hole_start;
  213. u64 adj_end = hole_end;
  214. DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node) || node->allocated);
  215. if (mm->color_adjust)
  216. mm->color_adjust(hole_node, color, &adj_start, &adj_end);
  217. adj_start = max(adj_start, range_start);
  218. adj_end = min(adj_end, range_end);
  219. if (flags & DRM_MM_CREATE_TOP)
  220. adj_start = adj_end - size;
  221. if (alignment) {
  222. u64 rem;
  223. div64_u64_rem(adj_start, alignment, &rem);
  224. if (rem) {
  225. if (flags & DRM_MM_CREATE_TOP)
  226. adj_start -= rem;
  227. else
  228. adj_start += alignment - rem;
  229. }
  230. }
  231. if (adj_start == hole_start) {
  232. hole_node->hole_follows = 0;
  233. list_del(&hole_node->hole_stack);
  234. }
  235. node->start = adj_start;
  236. node->size = size;
  237. node->mm = mm;
  238. node->color = color;
  239. node->allocated = 1;
  240. list_add(&node->node_list, &hole_node->node_list);
  241. drm_mm_interval_tree_add_node(hole_node, node);
  242. DRM_MM_BUG_ON(node->start < range_start);
  243. DRM_MM_BUG_ON(node->start < adj_start);
  244. DRM_MM_BUG_ON(node->start + node->size > adj_end);
  245. DRM_MM_BUG_ON(node->start + node->size > range_end);
  246. node->hole_follows = 0;
  247. if (__drm_mm_hole_node_start(node) < hole_end) {
  248. list_add(&node->hole_stack, &mm->hole_stack);
  249. node->hole_follows = 1;
  250. }
  251. save_stack(node);
  252. }
  253. /**
  254. * drm_mm_reserve_node - insert an pre-initialized node
  255. * @mm: drm_mm allocator to insert @node into
  256. * @node: drm_mm_node to insert
  257. *
  258. * This functions inserts an already set-up &drm_mm_node into the allocator,
  259. * meaning that start, size and color must be set by the caller. All other
  260. * fields must be cleared to 0. This is useful to initialize the allocator with
  261. * preallocated objects which must be set-up before the range allocator can be
  262. * set-up, e.g. when taking over a firmware framebuffer.
  263. *
  264. * Returns:
  265. * 0 on success, -ENOSPC if there's no hole where @node is.
  266. */
  267. int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
  268. {
  269. u64 end = node->start + node->size;
  270. struct drm_mm_node *hole;
  271. u64 hole_start, hole_end;
  272. u64 adj_start, adj_end;
  273. end = node->start + node->size;
  274. if (unlikely(end <= node->start))
  275. return -ENOSPC;
  276. /* Find the relevant hole to add our node to */
  277. hole = drm_mm_interval_tree_iter_first(&mm->interval_tree,
  278. node->start, ~(u64)0);
  279. if (hole) {
  280. if (hole->start < end)
  281. return -ENOSPC;
  282. } else {
  283. hole = list_entry(drm_mm_nodes(mm), typeof(*hole), node_list);
  284. }
  285. hole = list_last_entry(&hole->node_list, typeof(*hole), node_list);
  286. if (!drm_mm_hole_follows(hole))
  287. return -ENOSPC;
  288. adj_start = hole_start = __drm_mm_hole_node_start(hole);
  289. adj_end = hole_end = __drm_mm_hole_node_end(hole);
  290. if (mm->color_adjust)
  291. mm->color_adjust(hole, node->color, &adj_start, &adj_end);
  292. if (adj_start > node->start || adj_end < end)
  293. return -ENOSPC;
  294. node->mm = mm;
  295. node->allocated = 1;
  296. list_add(&node->node_list, &hole->node_list);
  297. drm_mm_interval_tree_add_node(hole, node);
  298. if (node->start == hole_start) {
  299. hole->hole_follows = 0;
  300. list_del(&hole->hole_stack);
  301. }
  302. node->hole_follows = 0;
  303. if (end != hole_end) {
  304. list_add(&node->hole_stack, &mm->hole_stack);
  305. node->hole_follows = 1;
  306. }
  307. save_stack(node);
  308. return 0;
  309. }
  310. EXPORT_SYMBOL(drm_mm_reserve_node);
  311. /**
  312. * drm_mm_insert_node_in_range_generic - ranged search for space and insert @node
  313. * @mm: drm_mm to allocate from
  314. * @node: preallocate node to insert
  315. * @size: size of the allocation
  316. * @alignment: alignment of the allocation
  317. * @color: opaque tag value to use for this node
  318. * @start: start of the allowed range for this node
  319. * @end: end of the allowed range for this node
  320. * @sflags: flags to fine-tune the allocation search
  321. * @aflags: flags to fine-tune the allocation behavior
  322. *
  323. * The preallocated @node must be cleared to 0.
  324. *
  325. * Returns:
  326. * 0 on success, -ENOSPC if there's no suitable hole.
  327. */
  328. int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
  329. u64 size, u64 alignment,
  330. unsigned long color,
  331. u64 start, u64 end,
  332. enum drm_mm_search_flags sflags,
  333. enum drm_mm_allocator_flags aflags)
  334. {
  335. struct drm_mm_node *hole_node;
  336. if (WARN_ON(size == 0))
  337. return -EINVAL;
  338. hole_node = drm_mm_search_free_in_range_generic(mm,
  339. size, alignment, color,
  340. start, end, sflags);
  341. if (!hole_node)
  342. return -ENOSPC;
  343. drm_mm_insert_helper(hole_node, node,
  344. size, alignment, color,
  345. start, end, aflags);
  346. return 0;
  347. }
  348. EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
  349. /**
  350. * drm_mm_remove_node - Remove a memory node from the allocator.
  351. * @node: drm_mm_node to remove
  352. *
  353. * This just removes a node from its drm_mm allocator. The node does not need to
  354. * be cleared again before it can be re-inserted into this or any other drm_mm
  355. * allocator. It is a bug to call this function on a unallocated node.
  356. */
  357. void drm_mm_remove_node(struct drm_mm_node *node)
  358. {
  359. struct drm_mm *mm = node->mm;
  360. struct drm_mm_node *prev_node;
  361. DRM_MM_BUG_ON(!node->allocated);
  362. DRM_MM_BUG_ON(node->scanned_block);
  363. prev_node =
  364. list_entry(node->node_list.prev, struct drm_mm_node, node_list);
  365. if (drm_mm_hole_follows(node)) {
  366. DRM_MM_BUG_ON(__drm_mm_hole_node_start(node) ==
  367. __drm_mm_hole_node_end(node));
  368. list_del(&node->hole_stack);
  369. } else {
  370. DRM_MM_BUG_ON(__drm_mm_hole_node_start(node) !=
  371. __drm_mm_hole_node_end(node));
  372. }
  373. if (!drm_mm_hole_follows(prev_node)) {
  374. prev_node->hole_follows = 1;
  375. list_add(&prev_node->hole_stack, &mm->hole_stack);
  376. } else
  377. list_move(&prev_node->hole_stack, &mm->hole_stack);
  378. drm_mm_interval_tree_remove(node, &mm->interval_tree);
  379. list_del(&node->node_list);
  380. node->allocated = 0;
  381. }
  382. EXPORT_SYMBOL(drm_mm_remove_node);
  383. static int check_free_hole(u64 start, u64 end, u64 size, u64 alignment)
  384. {
  385. if (end - start < size)
  386. return 0;
  387. if (alignment) {
  388. u64 rem;
  389. div64_u64_rem(start, alignment, &rem);
  390. if (rem)
  391. start += alignment - rem;
  392. }
  393. return end >= start + size;
  394. }
  395. static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
  396. u64 size,
  397. u64 alignment,
  398. unsigned long color,
  399. u64 start,
  400. u64 end,
  401. enum drm_mm_search_flags flags)
  402. {
  403. struct drm_mm_node *entry;
  404. struct drm_mm_node *best;
  405. u64 adj_start;
  406. u64 adj_end;
  407. u64 best_size;
  408. DRM_MM_BUG_ON(mm->scan_active);
  409. best = NULL;
  410. best_size = ~0UL;
  411. __drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
  412. flags & DRM_MM_SEARCH_BELOW) {
  413. u64 hole_size = adj_end - adj_start;
  414. if (mm->color_adjust) {
  415. mm->color_adjust(entry, color, &adj_start, &adj_end);
  416. if (adj_end <= adj_start)
  417. continue;
  418. }
  419. adj_start = max(adj_start, start);
  420. adj_end = min(adj_end, end);
  421. if (!check_free_hole(adj_start, adj_end, size, alignment))
  422. continue;
  423. if (!(flags & DRM_MM_SEARCH_BEST))
  424. return entry;
  425. if (hole_size < best_size) {
  426. best = entry;
  427. best_size = hole_size;
  428. }
  429. }
  430. return best;
  431. }
  432. /**
  433. * drm_mm_replace_node - move an allocation from @old to @new
  434. * @old: drm_mm_node to remove from the allocator
  435. * @new: drm_mm_node which should inherit @old's allocation
  436. *
  437. * This is useful for when drivers embed the drm_mm_node structure and hence
  438. * can't move allocations by reassigning pointers. It's a combination of remove
  439. * and insert with the guarantee that the allocation start will match.
  440. */
  441. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
  442. {
  443. DRM_MM_BUG_ON(!old->allocated);
  444. list_replace(&old->node_list, &new->node_list);
  445. list_replace(&old->hole_stack, &new->hole_stack);
  446. rb_replace_node(&old->rb, &new->rb, &old->mm->interval_tree);
  447. new->hole_follows = old->hole_follows;
  448. new->mm = old->mm;
  449. new->start = old->start;
  450. new->size = old->size;
  451. new->color = old->color;
  452. new->__subtree_last = old->__subtree_last;
  453. old->allocated = 0;
  454. new->allocated = 1;
  455. }
  456. EXPORT_SYMBOL(drm_mm_replace_node);
  457. /**
  458. * DOC: lru scan roster
  459. *
  460. * Very often GPUs need to have continuous allocations for a given object. When
  461. * evicting objects to make space for a new one it is therefore not most
  462. * efficient when we simply start to select all objects from the tail of an LRU
  463. * until there's a suitable hole: Especially for big objects or nodes that
  464. * otherwise have special allocation constraints there's a good chance we evict
  465. * lots of (smaller) objects unnecessarily.
  466. *
  467. * The DRM range allocator supports this use-case through the scanning
  468. * interfaces. First a scan operation needs to be initialized with
  469. * drm_mm_scan_init() or drm_mm_scan_init_with_range(). The driver adds
  470. * objects to the roster, probably by walking an LRU list, but this can be
  471. * freely implemented. Eviction candiates are added using
  472. * drm_mm_scan_add_block() until a suitable hole is found or there are no
  473. * further evictable objects. Eviction roster metadata is tracked in struct
  474. * &drm_mm_scan.
  475. *
  476. * The driver must walk through all objects again in exactly the reverse
  477. * order to restore the allocator state. Note that while the allocator is used
  478. * in the scan mode no other operation is allowed.
  479. *
  480. * Finally the driver evicts all objects selected (drm_mm_scan_remove_block()
  481. * reported true) in the scan, and any overlapping nodes after color adjustment
  482. * (drm_mm_scan_color_evict()). Adding and removing an object is O(1), and
  483. * since freeing a node is also O(1) the overall complexity is
  484. * O(scanned_objects). So like the free stack which needs to be walked before a
  485. * scan operation even begins this is linear in the number of objects. It
  486. * doesn't seem to hurt too badly.
  487. */
  488. /**
  489. * drm_mm_scan_init_with_range - initialize range-restricted lru scanning
  490. * @scan: scan state
  491. * @mm: drm_mm to scan
  492. * @size: size of the allocation
  493. * @alignment: alignment of the allocation
  494. * @color: opaque tag value to use for the allocation
  495. * @start: start of the allowed range for the allocation
  496. * @end: end of the allowed range for the allocation
  497. * @flags: flags to specify how the allocation will be performed afterwards
  498. *
  499. * This simply sets up the scanning routines with the parameters for the desired
  500. * hole.
  501. *
  502. * Warning:
  503. * As long as the scan list is non-empty, no other operations than
  504. * adding/removing nodes to/from the scan list are allowed.
  505. */
  506. void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
  507. struct drm_mm *mm,
  508. u64 size,
  509. u64 alignment,
  510. unsigned long color,
  511. u64 start,
  512. u64 end,
  513. unsigned int flags)
  514. {
  515. DRM_MM_BUG_ON(start >= end);
  516. DRM_MM_BUG_ON(!size || size > end - start);
  517. DRM_MM_BUG_ON(mm->scan_active);
  518. scan->mm = mm;
  519. if (alignment <= 1)
  520. alignment = 0;
  521. scan->color = color;
  522. scan->alignment = alignment;
  523. scan->remainder_mask = is_power_of_2(alignment) ? alignment - 1 : 0;
  524. scan->size = size;
  525. scan->flags = flags;
  526. DRM_MM_BUG_ON(end <= start);
  527. scan->range_start = start;
  528. scan->range_end = end;
  529. scan->hit_start = U64_MAX;
  530. scan->hit_end = 0;
  531. }
  532. EXPORT_SYMBOL(drm_mm_scan_init_with_range);
  533. /**
  534. * drm_mm_scan_add_block - add a node to the scan list
  535. * @scan: the active drm_mm scanner
  536. * @node: drm_mm_node to add
  537. *
  538. * Add a node to the scan list that might be freed to make space for the desired
  539. * hole.
  540. *
  541. * Returns:
  542. * True if a hole has been found, false otherwise.
  543. */
  544. bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
  545. struct drm_mm_node *node)
  546. {
  547. struct drm_mm *mm = scan->mm;
  548. struct drm_mm_node *hole;
  549. u64 hole_start, hole_end;
  550. u64 col_start, col_end;
  551. u64 adj_start, adj_end;
  552. DRM_MM_BUG_ON(node->mm != mm);
  553. DRM_MM_BUG_ON(!node->allocated);
  554. DRM_MM_BUG_ON(node->scanned_block);
  555. node->scanned_block = true;
  556. mm->scan_active++;
  557. /* Remove this block from the node_list so that we enlarge the hole
  558. * (distance between the end of our previous node and the start of
  559. * or next), without poisoning the link so that we can restore it
  560. * later in drm_mm_scan_remove_block().
  561. */
  562. hole = list_prev_entry(node, node_list);
  563. DRM_MM_BUG_ON(list_next_entry(hole, node_list) != node);
  564. __list_del_entry(&node->node_list);
  565. hole_start = __drm_mm_hole_node_start(hole);
  566. hole_end = __drm_mm_hole_node_end(hole);
  567. col_start = hole_start;
  568. col_end = hole_end;
  569. if (mm->color_adjust)
  570. mm->color_adjust(hole, scan->color, &col_start, &col_end);
  571. adj_start = max(col_start, scan->range_start);
  572. adj_end = min(col_end, scan->range_end);
  573. if (adj_end <= adj_start || adj_end - adj_start < scan->size)
  574. return false;
  575. if (scan->flags == DRM_MM_CREATE_TOP)
  576. adj_start = adj_end - scan->size;
  577. if (scan->alignment) {
  578. u64 rem;
  579. if (likely(scan->remainder_mask))
  580. rem = adj_start & scan->remainder_mask;
  581. else
  582. div64_u64_rem(adj_start, scan->alignment, &rem);
  583. if (rem) {
  584. adj_start -= rem;
  585. if (scan->flags != DRM_MM_CREATE_TOP)
  586. adj_start += scan->alignment;
  587. if (adj_start < max(col_start, scan->range_start) ||
  588. min(col_end, scan->range_end) - adj_start < scan->size)
  589. return false;
  590. if (adj_end <= adj_start ||
  591. adj_end - adj_start < scan->size)
  592. return false;
  593. }
  594. }
  595. scan->hit_start = adj_start;
  596. scan->hit_end = adj_start + scan->size;
  597. DRM_MM_BUG_ON(scan->hit_start >= scan->hit_end);
  598. DRM_MM_BUG_ON(scan->hit_start < hole_start);
  599. DRM_MM_BUG_ON(scan->hit_end > hole_end);
  600. return true;
  601. }
  602. EXPORT_SYMBOL(drm_mm_scan_add_block);
  603. /**
  604. * drm_mm_scan_remove_block - remove a node from the scan list
  605. * @scan: the active drm_mm scanner
  606. * @node: drm_mm_node to remove
  607. *
  608. * Nodes **must** be removed in exactly the reverse order from the scan list as
  609. * they have been added (e.g. using list_add() as they are added and then
  610. * list_for_each() over that eviction list to remove), otherwise the internal
  611. * state of the memory manager will be corrupted.
  612. *
  613. * When the scan list is empty, the selected memory nodes can be freed. An
  614. * immediately following drm_mm_insert_node_in_range_generic() or one of the
  615. * simpler versions of that function with !DRM_MM_SEARCH_BEST will then return
  616. * the just freed block (because its at the top of the free_stack list).
  617. *
  618. * Returns:
  619. * True if this block should be evicted, false otherwise. Will always
  620. * return false when no hole has been found.
  621. */
  622. bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
  623. struct drm_mm_node *node)
  624. {
  625. struct drm_mm_node *prev_node;
  626. DRM_MM_BUG_ON(node->mm != scan->mm);
  627. DRM_MM_BUG_ON(!node->scanned_block);
  628. node->scanned_block = false;
  629. DRM_MM_BUG_ON(!node->mm->scan_active);
  630. node->mm->scan_active--;
  631. /* During drm_mm_scan_add_block() we decoupled this node leaving
  632. * its pointers intact. Now that the caller is walking back along
  633. * the eviction list we can restore this block into its rightful
  634. * place on the full node_list. To confirm that the caller is walking
  635. * backwards correctly we check that prev_node->next == node->next,
  636. * i.e. both believe the same node should be on the other side of the
  637. * hole.
  638. */
  639. prev_node = list_prev_entry(node, node_list);
  640. DRM_MM_BUG_ON(list_next_entry(prev_node, node_list) !=
  641. list_next_entry(node, node_list));
  642. list_add(&node->node_list, &prev_node->node_list);
  643. return (node->start + node->size > scan->hit_start &&
  644. node->start < scan->hit_end);
  645. }
  646. EXPORT_SYMBOL(drm_mm_scan_remove_block);
  647. /**
  648. * drm_mm_scan_color_evict - evict overlapping nodes on either side of hole
  649. * @scan: drm_mm scan with target hole
  650. *
  651. * After completing an eviction scan and removing the selected nodes, we may
  652. * need to remove a few more nodes from either side of the target hole if
  653. * mm.color_adjust is being used.
  654. *
  655. * Returns:
  656. * A node to evict, or NULL if there are no overlapping nodes.
  657. */
  658. struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan)
  659. {
  660. struct drm_mm *mm = scan->mm;
  661. struct drm_mm_node *hole;
  662. u64 hole_start, hole_end;
  663. DRM_MM_BUG_ON(list_empty(&mm->hole_stack));
  664. if (!mm->color_adjust)
  665. return NULL;
  666. hole = list_first_entry(&mm->hole_stack, typeof(*hole), hole_stack);
  667. hole_start = __drm_mm_hole_node_start(hole);
  668. hole_end = __drm_mm_hole_node_end(hole);
  669. DRM_MM_BUG_ON(hole_start > scan->hit_start);
  670. DRM_MM_BUG_ON(hole_end < scan->hit_end);
  671. mm->color_adjust(hole, scan->color, &hole_start, &hole_end);
  672. if (hole_start > scan->hit_start)
  673. return hole;
  674. if (hole_end < scan->hit_end)
  675. return list_next_entry(hole, node_list);
  676. return NULL;
  677. }
  678. EXPORT_SYMBOL(drm_mm_scan_color_evict);
  679. /**
  680. * drm_mm_init - initialize a drm-mm allocator
  681. * @mm: the drm_mm structure to initialize
  682. * @start: start of the range managed by @mm
  683. * @size: end of the range managed by @mm
  684. *
  685. * Note that @mm must be cleared to 0 before calling this function.
  686. */
  687. void drm_mm_init(struct drm_mm *mm, u64 start, u64 size)
  688. {
  689. DRM_MM_BUG_ON(start + size <= start);
  690. INIT_LIST_HEAD(&mm->hole_stack);
  691. mm->scan_active = 0;
  692. /* Clever trick to avoid a special case in the free hole tracking. */
  693. INIT_LIST_HEAD(&mm->head_node.node_list);
  694. mm->head_node.allocated = 0;
  695. mm->head_node.hole_follows = 1;
  696. mm->head_node.mm = mm;
  697. mm->head_node.start = start + size;
  698. mm->head_node.size = start - mm->head_node.start;
  699. list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
  700. mm->interval_tree = RB_ROOT;
  701. mm->color_adjust = NULL;
  702. }
  703. EXPORT_SYMBOL(drm_mm_init);
  704. /**
  705. * drm_mm_takedown - clean up a drm_mm allocator
  706. * @mm: drm_mm allocator to clean up
  707. *
  708. * Note that it is a bug to call this function on an allocator which is not
  709. * clean.
  710. */
  711. void drm_mm_takedown(struct drm_mm *mm)
  712. {
  713. if (WARN(!drm_mm_clean(mm),
  714. "Memory manager not clean during takedown.\n"))
  715. show_leaks(mm);
  716. }
  717. EXPORT_SYMBOL(drm_mm_takedown);
  718. static u64 drm_mm_dump_hole(struct drm_printer *p, const struct drm_mm_node *entry)
  719. {
  720. u64 hole_start, hole_end, hole_size;
  721. if (entry->hole_follows) {
  722. hole_start = drm_mm_hole_node_start(entry);
  723. hole_end = drm_mm_hole_node_end(entry);
  724. hole_size = hole_end - hole_start;
  725. drm_printf(p, "%#018llx-%#018llx: %llu: free\n", hole_start,
  726. hole_end, hole_size);
  727. return hole_size;
  728. }
  729. return 0;
  730. }
  731. /**
  732. * drm_mm_print - print allocator state
  733. * @mm: drm_mm allocator to print
  734. * @p: DRM printer to use
  735. */
  736. void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p)
  737. {
  738. const struct drm_mm_node *entry;
  739. u64 total_used = 0, total_free = 0, total = 0;
  740. total_free += drm_mm_dump_hole(p, &mm->head_node);
  741. drm_mm_for_each_node(entry, mm) {
  742. drm_printf(p, "%#018llx-%#018llx: %llu: used\n", entry->start,
  743. entry->start + entry->size, entry->size);
  744. total_used += entry->size;
  745. total_free += drm_mm_dump_hole(p, entry);
  746. }
  747. total = total_free + total_used;
  748. drm_printf(p, "total: %llu, used %llu free %llu\n", total,
  749. total_used, total_free);
  750. }
  751. EXPORT_SYMBOL(drm_mm_print);