drm_mm.c 27 KB

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