drm_mm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. #ifdef CONFIG_DRM_DEBUG_MM
  97. #include <linux/stackdepot.h>
  98. #define STACKDEPTH 32
  99. #define BUFSZ 4096
  100. static noinline void save_stack(struct drm_mm_node *node)
  101. {
  102. unsigned long entries[STACKDEPTH];
  103. struct stack_trace trace = {
  104. .entries = entries,
  105. .max_entries = STACKDEPTH,
  106. .skip = 1
  107. };
  108. save_stack_trace(&trace);
  109. if (trace.nr_entries != 0 &&
  110. trace.entries[trace.nr_entries-1] == ULONG_MAX)
  111. trace.nr_entries--;
  112. /* May be called under spinlock, so avoid sleeping */
  113. node->stack = depot_save_stack(&trace, GFP_NOWAIT);
  114. }
  115. static void show_leaks(struct drm_mm *mm)
  116. {
  117. struct drm_mm_node *node;
  118. unsigned long entries[STACKDEPTH];
  119. char *buf;
  120. buf = kmalloc(BUFSZ, GFP_KERNEL);
  121. if (!buf)
  122. return;
  123. list_for_each_entry(node, drm_mm_nodes(mm), node_list) {
  124. struct stack_trace trace = {
  125. .entries = entries,
  126. .max_entries = STACKDEPTH
  127. };
  128. if (!node->stack) {
  129. DRM_ERROR("node [%08llx + %08llx]: unknown owner\n",
  130. node->start, node->size);
  131. continue;
  132. }
  133. depot_fetch_stack(node->stack, &trace);
  134. snprint_stack_trace(buf, BUFSZ, &trace, 0);
  135. DRM_ERROR("node [%08llx + %08llx]: inserted at\n%s",
  136. node->start, node->size, buf);
  137. }
  138. kfree(buf);
  139. }
  140. #undef STACKDEPTH
  141. #undef BUFSZ
  142. #else
  143. static void save_stack(struct drm_mm_node *node) { }
  144. static void show_leaks(struct drm_mm *mm) { }
  145. #endif
  146. #define START(node) ((node)->start)
  147. #define LAST(node) ((node)->start + (node)->size - 1)
  148. INTERVAL_TREE_DEFINE(struct drm_mm_node, rb,
  149. u64, __subtree_last,
  150. START, LAST, static inline, drm_mm_interval_tree)
  151. struct drm_mm_node *
  152. __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last)
  153. {
  154. return drm_mm_interval_tree_iter_first((struct rb_root *)&mm->interval_tree,
  155. start, last);
  156. }
  157. EXPORT_SYMBOL(__drm_mm_interval_first);
  158. static void drm_mm_interval_tree_add_node(struct drm_mm_node *hole_node,
  159. struct drm_mm_node *node)
  160. {
  161. struct drm_mm *mm = hole_node->mm;
  162. struct rb_node **link, *rb;
  163. struct drm_mm_node *parent;
  164. node->__subtree_last = LAST(node);
  165. if (hole_node->allocated) {
  166. rb = &hole_node->rb;
  167. while (rb) {
  168. parent = rb_entry(rb, struct drm_mm_node, rb);
  169. if (parent->__subtree_last >= node->__subtree_last)
  170. break;
  171. parent->__subtree_last = node->__subtree_last;
  172. rb = rb_parent(rb);
  173. }
  174. rb = &hole_node->rb;
  175. link = &hole_node->rb.rb_right;
  176. } else {
  177. rb = NULL;
  178. link = &mm->interval_tree.rb_node;
  179. }
  180. while (*link) {
  181. rb = *link;
  182. parent = rb_entry(rb, struct drm_mm_node, rb);
  183. if (parent->__subtree_last < node->__subtree_last)
  184. parent->__subtree_last = node->__subtree_last;
  185. if (node->start < parent->start)
  186. link = &parent->rb.rb_left;
  187. else
  188. link = &parent->rb.rb_right;
  189. }
  190. rb_link_node(&node->rb, rb, link);
  191. rb_insert_augmented(&node->rb,
  192. &mm->interval_tree,
  193. &drm_mm_interval_tree_augment);
  194. }
  195. #define RB_INSERT(root, member, expr) do { \
  196. struct rb_node **link = &root.rb_node, *rb = NULL; \
  197. u64 x = expr(node); \
  198. while (*link) { \
  199. rb = *link; \
  200. if (x < expr(rb_entry(rb, struct drm_mm_node, member))) \
  201. link = &rb->rb_left; \
  202. else \
  203. link = &rb->rb_right; \
  204. } \
  205. rb_link_node(&node->member, rb, link); \
  206. rb_insert_color(&node->member, &root); \
  207. } while (0)
  208. #define HOLE_SIZE(NODE) ((NODE)->hole_size)
  209. #define HOLE_ADDR(NODE) (__drm_mm_hole_node_start(NODE))
  210. static void add_hole(struct drm_mm_node *node)
  211. {
  212. struct drm_mm *mm = node->mm;
  213. node->hole_size =
  214. __drm_mm_hole_node_end(node) - __drm_mm_hole_node_start(node);
  215. DRM_MM_BUG_ON(!drm_mm_hole_follows(node));
  216. RB_INSERT(mm->holes_size, rb_hole_size, HOLE_SIZE);
  217. RB_INSERT(mm->holes_addr, rb_hole_addr, HOLE_ADDR);
  218. list_add(&node->hole_stack, &mm->hole_stack);
  219. }
  220. static void rm_hole(struct drm_mm_node *node)
  221. {
  222. DRM_MM_BUG_ON(!drm_mm_hole_follows(node));
  223. list_del(&node->hole_stack);
  224. rb_erase(&node->rb_hole_size, &node->mm->holes_size);
  225. rb_erase(&node->rb_hole_addr, &node->mm->holes_addr);
  226. node->hole_size = 0;
  227. DRM_MM_BUG_ON(drm_mm_hole_follows(node));
  228. }
  229. static inline struct drm_mm_node *rb_hole_size_to_node(struct rb_node *rb)
  230. {
  231. return rb_entry_safe(rb, struct drm_mm_node, rb_hole_size);
  232. }
  233. static inline struct drm_mm_node *rb_hole_addr_to_node(struct rb_node *rb)
  234. {
  235. return rb_entry_safe(rb, struct drm_mm_node, rb_hole_addr);
  236. }
  237. static inline u64 rb_hole_size(struct rb_node *rb)
  238. {
  239. return rb_entry(rb, struct drm_mm_node, rb_hole_size)->hole_size;
  240. }
  241. static struct drm_mm_node *best_hole(struct drm_mm *mm, u64 size)
  242. {
  243. struct rb_node *best = NULL;
  244. struct rb_node **link = &mm->holes_size.rb_node;
  245. while (*link) {
  246. struct rb_node *rb = *link;
  247. if (size <= rb_hole_size(rb)) {
  248. link = &rb->rb_left;
  249. best = rb;
  250. } else {
  251. link = &rb->rb_right;
  252. }
  253. }
  254. return rb_hole_size_to_node(best);
  255. }
  256. static struct drm_mm_node *find_hole(struct drm_mm *mm, u64 addr)
  257. {
  258. struct drm_mm_node *node = NULL;
  259. struct rb_node **link = &mm->holes_addr.rb_node;
  260. while (*link) {
  261. u64 hole_start;
  262. node = rb_hole_addr_to_node(*link);
  263. hole_start = __drm_mm_hole_node_start(node);
  264. if (addr < hole_start)
  265. link = &node->rb_hole_addr.rb_left;
  266. else if (addr > hole_start + node->hole_size)
  267. link = &node->rb_hole_addr.rb_right;
  268. else
  269. break;
  270. }
  271. return node;
  272. }
  273. static struct drm_mm_node *
  274. first_hole(struct drm_mm *mm,
  275. u64 start, u64 end, u64 size,
  276. enum drm_mm_insert_mode mode)
  277. {
  278. if (RB_EMPTY_ROOT(&mm->holes_size))
  279. return NULL;
  280. switch (mode) {
  281. default:
  282. case DRM_MM_INSERT_BEST:
  283. return best_hole(mm, size);
  284. case DRM_MM_INSERT_LOW:
  285. return find_hole(mm, start);
  286. case DRM_MM_INSERT_HIGH:
  287. return find_hole(mm, end);
  288. case DRM_MM_INSERT_EVICT:
  289. return list_first_entry_or_null(&mm->hole_stack,
  290. struct drm_mm_node,
  291. hole_stack);
  292. }
  293. }
  294. static struct drm_mm_node *
  295. next_hole(struct drm_mm *mm,
  296. struct drm_mm_node *node,
  297. enum drm_mm_insert_mode mode)
  298. {
  299. switch (mode) {
  300. default:
  301. case DRM_MM_INSERT_BEST:
  302. return rb_hole_size_to_node(rb_next(&node->rb_hole_size));
  303. case DRM_MM_INSERT_LOW:
  304. return rb_hole_addr_to_node(rb_next(&node->rb_hole_addr));
  305. case DRM_MM_INSERT_HIGH:
  306. return rb_hole_addr_to_node(rb_prev(&node->rb_hole_addr));
  307. case DRM_MM_INSERT_EVICT:
  308. node = list_next_entry(node, hole_stack);
  309. return &node->hole_stack == &mm->hole_stack ? NULL : node;
  310. }
  311. }
  312. /**
  313. * drm_mm_reserve_node - insert an pre-initialized node
  314. * @mm: drm_mm allocator to insert @node into
  315. * @node: drm_mm_node to insert
  316. *
  317. * This functions inserts an already set-up &drm_mm_node into the allocator,
  318. * meaning that start, size and color must be set by the caller. All other
  319. * fields must be cleared to 0. This is useful to initialize the allocator with
  320. * preallocated objects which must be set-up before the range allocator can be
  321. * set-up, e.g. when taking over a firmware framebuffer.
  322. *
  323. * Returns:
  324. * 0 on success, -ENOSPC if there's no hole where @node is.
  325. */
  326. int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
  327. {
  328. u64 end = node->start + node->size;
  329. struct drm_mm_node *hole;
  330. u64 hole_start, hole_end;
  331. u64 adj_start, adj_end;
  332. end = node->start + node->size;
  333. if (unlikely(end <= node->start))
  334. return -ENOSPC;
  335. /* Find the relevant hole to add our node to */
  336. hole = find_hole(mm, node->start);
  337. if (!hole)
  338. return -ENOSPC;
  339. adj_start = hole_start = __drm_mm_hole_node_start(hole);
  340. adj_end = hole_end = hole_start + hole->hole_size;
  341. if (mm->color_adjust)
  342. mm->color_adjust(hole, node->color, &adj_start, &adj_end);
  343. if (adj_start > node->start || adj_end < end)
  344. return -ENOSPC;
  345. node->mm = mm;
  346. list_add(&node->node_list, &hole->node_list);
  347. drm_mm_interval_tree_add_node(hole, node);
  348. node->allocated = true;
  349. node->hole_size = 0;
  350. rm_hole(hole);
  351. if (node->start > hole_start)
  352. add_hole(hole);
  353. if (end < hole_end)
  354. add_hole(node);
  355. save_stack(node);
  356. return 0;
  357. }
  358. EXPORT_SYMBOL(drm_mm_reserve_node);
  359. /**
  360. * drm_mm_insert_node_in_range - ranged search for space and insert @node
  361. * @mm: drm_mm to allocate from
  362. * @node: preallocate node to insert
  363. * @size: size of the allocation
  364. * @alignment: alignment of the allocation
  365. * @color: opaque tag value to use for this node
  366. * @range_start: start of the allowed range for this node
  367. * @range_end: end of the allowed range for this node
  368. * @mode: fine-tune the allocation search and placement
  369. *
  370. * The preallocated @node must be cleared to 0.
  371. *
  372. * Returns:
  373. * 0 on success, -ENOSPC if there's no suitable hole.
  374. */
  375. int drm_mm_insert_node_in_range(struct drm_mm * const mm,
  376. struct drm_mm_node * const node,
  377. u64 size, u64 alignment,
  378. unsigned long color,
  379. u64 range_start, u64 range_end,
  380. enum drm_mm_insert_mode mode)
  381. {
  382. struct drm_mm_node *hole;
  383. u64 remainder_mask;
  384. DRM_MM_BUG_ON(range_start >= range_end);
  385. if (unlikely(size == 0 || range_end - range_start < size))
  386. return -ENOSPC;
  387. if (alignment <= 1)
  388. alignment = 0;
  389. remainder_mask = is_power_of_2(alignment) ? alignment - 1 : 0;
  390. for (hole = first_hole(mm, range_start, range_end, size, mode); hole;
  391. hole = next_hole(mm, hole, mode)) {
  392. u64 hole_start = __drm_mm_hole_node_start(hole);
  393. u64 hole_end = hole_start + hole->hole_size;
  394. u64 adj_start, adj_end;
  395. u64 col_start, col_end;
  396. if (mode == DRM_MM_INSERT_LOW && hole_start >= range_end)
  397. break;
  398. if (mode == DRM_MM_INSERT_HIGH && hole_end <= range_start)
  399. break;
  400. col_start = hole_start;
  401. col_end = hole_end;
  402. if (mm->color_adjust)
  403. mm->color_adjust(hole, color, &col_start, &col_end);
  404. adj_start = max(col_start, range_start);
  405. adj_end = min(col_end, range_end);
  406. if (adj_end <= adj_start || adj_end - adj_start < size)
  407. continue;
  408. if (mode == DRM_MM_INSERT_HIGH)
  409. adj_start = adj_end - size;
  410. if (alignment) {
  411. u64 rem;
  412. if (likely(remainder_mask))
  413. rem = adj_start & remainder_mask;
  414. else
  415. div64_u64_rem(adj_start, alignment, &rem);
  416. if (rem) {
  417. adj_start -= rem;
  418. if (mode != DRM_MM_INSERT_HIGH)
  419. adj_start += alignment;
  420. if (adj_start < max(col_start, range_start) ||
  421. min(col_end, range_end) - adj_start < size)
  422. continue;
  423. if (adj_end <= adj_start ||
  424. adj_end - adj_start < size)
  425. continue;
  426. }
  427. }
  428. node->mm = mm;
  429. node->size = size;
  430. node->start = adj_start;
  431. node->color = color;
  432. node->hole_size = 0;
  433. list_add(&node->node_list, &hole->node_list);
  434. drm_mm_interval_tree_add_node(hole, node);
  435. node->allocated = true;
  436. rm_hole(hole);
  437. if (adj_start > hole_start)
  438. add_hole(hole);
  439. if (adj_start + size < hole_end)
  440. add_hole(node);
  441. save_stack(node);
  442. return 0;
  443. }
  444. return -ENOSPC;
  445. }
  446. EXPORT_SYMBOL(drm_mm_insert_node_in_range);
  447. /**
  448. * drm_mm_remove_node - Remove a memory node from the allocator.
  449. * @node: drm_mm_node to remove
  450. *
  451. * This just removes a node from its drm_mm allocator. The node does not need to
  452. * be cleared again before it can be re-inserted into this or any other drm_mm
  453. * allocator. It is a bug to call this function on a unallocated node.
  454. */
  455. void drm_mm_remove_node(struct drm_mm_node *node)
  456. {
  457. struct drm_mm *mm = node->mm;
  458. struct drm_mm_node *prev_node;
  459. DRM_MM_BUG_ON(!node->allocated);
  460. DRM_MM_BUG_ON(node->scanned_block);
  461. prev_node = list_prev_entry(node, node_list);
  462. if (drm_mm_hole_follows(node))
  463. rm_hole(node);
  464. drm_mm_interval_tree_remove(node, &mm->interval_tree);
  465. list_del(&node->node_list);
  466. node->allocated = false;
  467. if (drm_mm_hole_follows(prev_node))
  468. rm_hole(prev_node);
  469. add_hole(prev_node);
  470. }
  471. EXPORT_SYMBOL(drm_mm_remove_node);
  472. /**
  473. * drm_mm_replace_node - move an allocation from @old to @new
  474. * @old: drm_mm_node to remove from the allocator
  475. * @new: drm_mm_node which should inherit @old's allocation
  476. *
  477. * This is useful for when drivers embed the drm_mm_node structure and hence
  478. * can't move allocations by reassigning pointers. It's a combination of remove
  479. * and insert with the guarantee that the allocation start will match.
  480. */
  481. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
  482. {
  483. DRM_MM_BUG_ON(!old->allocated);
  484. *new = *old;
  485. list_replace(&old->node_list, &new->node_list);
  486. rb_replace_node(&old->rb, &new->rb, &old->mm->interval_tree);
  487. if (drm_mm_hole_follows(old)) {
  488. list_replace(&old->hole_stack, &new->hole_stack);
  489. rb_replace_node(&old->rb_hole_size,
  490. &new->rb_hole_size,
  491. &old->mm->holes_size);
  492. rb_replace_node(&old->rb_hole_addr,
  493. &new->rb_hole_addr,
  494. &old->mm->holes_addr);
  495. }
  496. old->allocated = false;
  497. new->allocated = true;
  498. }
  499. EXPORT_SYMBOL(drm_mm_replace_node);
  500. /**
  501. * DOC: lru scan roster
  502. *
  503. * Very often GPUs need to have continuous allocations for a given object. When
  504. * evicting objects to make space for a new one it is therefore not most
  505. * efficient when we simply start to select all objects from the tail of an LRU
  506. * until there's a suitable hole: Especially for big objects or nodes that
  507. * otherwise have special allocation constraints there's a good chance we evict
  508. * lots of (smaller) objects unnecessarily.
  509. *
  510. * The DRM range allocator supports this use-case through the scanning
  511. * interfaces. First a scan operation needs to be initialized with
  512. * drm_mm_scan_init() or drm_mm_scan_init_with_range(). The driver adds
  513. * objects to the roster, probably by walking an LRU list, but this can be
  514. * freely implemented. Eviction candiates are added using
  515. * drm_mm_scan_add_block() until a suitable hole is found or there are no
  516. * further evictable objects. Eviction roster metadata is tracked in &struct
  517. * drm_mm_scan.
  518. *
  519. * The driver must walk through all objects again in exactly the reverse
  520. * order to restore the allocator state. Note that while the allocator is used
  521. * in the scan mode no other operation is allowed.
  522. *
  523. * Finally the driver evicts all objects selected (drm_mm_scan_remove_block()
  524. * reported true) in the scan, and any overlapping nodes after color adjustment
  525. * (drm_mm_scan_color_evict()). Adding and removing an object is O(1), and
  526. * since freeing a node is also O(1) the overall complexity is
  527. * O(scanned_objects). So like the free stack which needs to be walked before a
  528. * scan operation even begins this is linear in the number of objects. It
  529. * doesn't seem to hurt too badly.
  530. */
  531. /**
  532. * drm_mm_scan_init_with_range - initialize range-restricted lru scanning
  533. * @scan: scan state
  534. * @mm: drm_mm to scan
  535. * @size: size of the allocation
  536. * @alignment: alignment of the allocation
  537. * @color: opaque tag value to use for the allocation
  538. * @start: start of the allowed range for the allocation
  539. * @end: end of the allowed range for the allocation
  540. * @mode: fine-tune the allocation search and placement
  541. *
  542. * This simply sets up the scanning routines with the parameters for the desired
  543. * hole.
  544. *
  545. * Warning:
  546. * As long as the scan list is non-empty, no other operations than
  547. * adding/removing nodes to/from the scan list are allowed.
  548. */
  549. void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
  550. struct drm_mm *mm,
  551. u64 size,
  552. u64 alignment,
  553. unsigned long color,
  554. u64 start,
  555. u64 end,
  556. enum drm_mm_insert_mode mode)
  557. {
  558. DRM_MM_BUG_ON(start >= end);
  559. DRM_MM_BUG_ON(!size || size > end - start);
  560. DRM_MM_BUG_ON(mm->scan_active);
  561. scan->mm = mm;
  562. if (alignment <= 1)
  563. alignment = 0;
  564. scan->color = color;
  565. scan->alignment = alignment;
  566. scan->remainder_mask = is_power_of_2(alignment) ? alignment - 1 : 0;
  567. scan->size = size;
  568. scan->mode = mode;
  569. DRM_MM_BUG_ON(end <= start);
  570. scan->range_start = start;
  571. scan->range_end = end;
  572. scan->hit_start = U64_MAX;
  573. scan->hit_end = 0;
  574. }
  575. EXPORT_SYMBOL(drm_mm_scan_init_with_range);
  576. /**
  577. * drm_mm_scan_add_block - add a node to the scan list
  578. * @scan: the active drm_mm scanner
  579. * @node: drm_mm_node to add
  580. *
  581. * Add a node to the scan list that might be freed to make space for the desired
  582. * hole.
  583. *
  584. * Returns:
  585. * True if a hole has been found, false otherwise.
  586. */
  587. bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
  588. struct drm_mm_node *node)
  589. {
  590. struct drm_mm *mm = scan->mm;
  591. struct drm_mm_node *hole;
  592. u64 hole_start, hole_end;
  593. u64 col_start, col_end;
  594. u64 adj_start, adj_end;
  595. DRM_MM_BUG_ON(node->mm != mm);
  596. DRM_MM_BUG_ON(!node->allocated);
  597. DRM_MM_BUG_ON(node->scanned_block);
  598. node->scanned_block = true;
  599. mm->scan_active++;
  600. /* Remove this block from the node_list so that we enlarge the hole
  601. * (distance between the end of our previous node and the start of
  602. * or next), without poisoning the link so that we can restore it
  603. * later in drm_mm_scan_remove_block().
  604. */
  605. hole = list_prev_entry(node, node_list);
  606. DRM_MM_BUG_ON(list_next_entry(hole, node_list) != node);
  607. __list_del_entry(&node->node_list);
  608. hole_start = __drm_mm_hole_node_start(hole);
  609. hole_end = __drm_mm_hole_node_end(hole);
  610. col_start = hole_start;
  611. col_end = hole_end;
  612. if (mm->color_adjust)
  613. mm->color_adjust(hole, scan->color, &col_start, &col_end);
  614. adj_start = max(col_start, scan->range_start);
  615. adj_end = min(col_end, scan->range_end);
  616. if (adj_end <= adj_start || adj_end - adj_start < scan->size)
  617. return false;
  618. if (scan->mode == DRM_MM_INSERT_HIGH)
  619. adj_start = adj_end - scan->size;
  620. if (scan->alignment) {
  621. u64 rem;
  622. if (likely(scan->remainder_mask))
  623. rem = adj_start & scan->remainder_mask;
  624. else
  625. div64_u64_rem(adj_start, scan->alignment, &rem);
  626. if (rem) {
  627. adj_start -= rem;
  628. if (scan->mode != DRM_MM_INSERT_HIGH)
  629. adj_start += scan->alignment;
  630. if (adj_start < max(col_start, scan->range_start) ||
  631. min(col_end, scan->range_end) - adj_start < scan->size)
  632. return false;
  633. if (adj_end <= adj_start ||
  634. adj_end - adj_start < scan->size)
  635. return false;
  636. }
  637. }
  638. scan->hit_start = adj_start;
  639. scan->hit_end = adj_start + scan->size;
  640. DRM_MM_BUG_ON(scan->hit_start >= scan->hit_end);
  641. DRM_MM_BUG_ON(scan->hit_start < hole_start);
  642. DRM_MM_BUG_ON(scan->hit_end > hole_end);
  643. return true;
  644. }
  645. EXPORT_SYMBOL(drm_mm_scan_add_block);
  646. /**
  647. * drm_mm_scan_remove_block - remove a node from the scan list
  648. * @scan: the active drm_mm scanner
  649. * @node: drm_mm_node to remove
  650. *
  651. * Nodes **must** be removed in exactly the reverse order from the scan list as
  652. * they have been added (e.g. using list_add() as they are added and then
  653. * list_for_each() over that eviction list to remove), otherwise the internal
  654. * state of the memory manager will be corrupted.
  655. *
  656. * When the scan list is empty, the selected memory nodes can be freed. An
  657. * immediately following drm_mm_insert_node_in_range_generic() or one of the
  658. * simpler versions of that function with !DRM_MM_SEARCH_BEST will then return
  659. * the just freed block (because its at the top of the free_stack list).
  660. *
  661. * Returns:
  662. * True if this block should be evicted, false otherwise. Will always
  663. * return false when no hole has been found.
  664. */
  665. bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
  666. struct drm_mm_node *node)
  667. {
  668. struct drm_mm_node *prev_node;
  669. DRM_MM_BUG_ON(node->mm != scan->mm);
  670. DRM_MM_BUG_ON(!node->scanned_block);
  671. node->scanned_block = false;
  672. DRM_MM_BUG_ON(!node->mm->scan_active);
  673. node->mm->scan_active--;
  674. /* During drm_mm_scan_add_block() we decoupled this node leaving
  675. * its pointers intact. Now that the caller is walking back along
  676. * the eviction list we can restore this block into its rightful
  677. * place on the full node_list. To confirm that the caller is walking
  678. * backwards correctly we check that prev_node->next == node->next,
  679. * i.e. both believe the same node should be on the other side of the
  680. * hole.
  681. */
  682. prev_node = list_prev_entry(node, node_list);
  683. DRM_MM_BUG_ON(list_next_entry(prev_node, node_list) !=
  684. list_next_entry(node, node_list));
  685. list_add(&node->node_list, &prev_node->node_list);
  686. return (node->start + node->size > scan->hit_start &&
  687. node->start < scan->hit_end);
  688. }
  689. EXPORT_SYMBOL(drm_mm_scan_remove_block);
  690. /**
  691. * drm_mm_scan_color_evict - evict overlapping nodes on either side of hole
  692. * @scan: drm_mm scan with target hole
  693. *
  694. * After completing an eviction scan and removing the selected nodes, we may
  695. * need to remove a few more nodes from either side of the target hole if
  696. * mm.color_adjust is being used.
  697. *
  698. * Returns:
  699. * A node to evict, or NULL if there are no overlapping nodes.
  700. */
  701. struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan)
  702. {
  703. struct drm_mm *mm = scan->mm;
  704. struct drm_mm_node *hole;
  705. u64 hole_start, hole_end;
  706. DRM_MM_BUG_ON(list_empty(&mm->hole_stack));
  707. if (!mm->color_adjust)
  708. return NULL;
  709. hole = list_first_entry(&mm->hole_stack, typeof(*hole), hole_stack);
  710. hole_start = __drm_mm_hole_node_start(hole);
  711. hole_end = hole_start + hole->hole_size;
  712. DRM_MM_BUG_ON(hole_start > scan->hit_start);
  713. DRM_MM_BUG_ON(hole_end < scan->hit_end);
  714. mm->color_adjust(hole, scan->color, &hole_start, &hole_end);
  715. if (hole_start > scan->hit_start)
  716. return hole;
  717. if (hole_end < scan->hit_end)
  718. return list_next_entry(hole, node_list);
  719. return NULL;
  720. }
  721. EXPORT_SYMBOL(drm_mm_scan_color_evict);
  722. /**
  723. * drm_mm_init - initialize a drm-mm allocator
  724. * @mm: the drm_mm structure to initialize
  725. * @start: start of the range managed by @mm
  726. * @size: end of the range managed by @mm
  727. *
  728. * Note that @mm must be cleared to 0 before calling this function.
  729. */
  730. void drm_mm_init(struct drm_mm *mm, u64 start, u64 size)
  731. {
  732. DRM_MM_BUG_ON(start + size <= start);
  733. mm->color_adjust = NULL;
  734. INIT_LIST_HEAD(&mm->hole_stack);
  735. mm->interval_tree = RB_ROOT;
  736. mm->holes_size = RB_ROOT;
  737. mm->holes_addr = RB_ROOT;
  738. /* Clever trick to avoid a special case in the free hole tracking. */
  739. INIT_LIST_HEAD(&mm->head_node.node_list);
  740. mm->head_node.allocated = false;
  741. mm->head_node.mm = mm;
  742. mm->head_node.start = start + size;
  743. mm->head_node.size = -size;
  744. add_hole(&mm->head_node);
  745. mm->scan_active = 0;
  746. }
  747. EXPORT_SYMBOL(drm_mm_init);
  748. /**
  749. * drm_mm_takedown - clean up a drm_mm allocator
  750. * @mm: drm_mm allocator to clean up
  751. *
  752. * Note that it is a bug to call this function on an allocator which is not
  753. * clean.
  754. */
  755. void drm_mm_takedown(struct drm_mm *mm)
  756. {
  757. if (WARN(!drm_mm_clean(mm),
  758. "Memory manager not clean during takedown.\n"))
  759. show_leaks(mm);
  760. }
  761. EXPORT_SYMBOL(drm_mm_takedown);
  762. static u64 drm_mm_dump_hole(struct drm_printer *p, const struct drm_mm_node *entry)
  763. {
  764. u64 start, size;
  765. size = entry->hole_size;
  766. if (size) {
  767. start = drm_mm_hole_node_start(entry);
  768. drm_printf(p, "%#018llx-%#018llx: %llu: free\n",
  769. start, start + size, size);
  770. }
  771. return size;
  772. }
  773. /**
  774. * drm_mm_print - print allocator state
  775. * @mm: drm_mm allocator to print
  776. * @p: DRM printer to use
  777. */
  778. void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p)
  779. {
  780. const struct drm_mm_node *entry;
  781. u64 total_used = 0, total_free = 0, total = 0;
  782. total_free += drm_mm_dump_hole(p, &mm->head_node);
  783. drm_mm_for_each_node(entry, mm) {
  784. drm_printf(p, "%#018llx-%#018llx: %llu: used\n", entry->start,
  785. entry->start + entry->size, entry->size);
  786. total_used += entry->size;
  787. total_free += drm_mm_dump_hole(p, entry);
  788. }
  789. total = total_free + total_used;
  790. drm_printf(p, "total: %llu, used %llu free %llu\n", total,
  791. total_used, total_free);
  792. }
  793. EXPORT_SYMBOL(drm_mm_print);