drm_mm.c 29 KB

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