drm_mm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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 (adj_start < start)
  305. adj_start = start;
  306. if (adj_end > end)
  307. adj_end = end;
  308. if (mm->color_adjust)
  309. mm->color_adjust(hole_node, color, &adj_start, &adj_end);
  310. if (flags & DRM_MM_CREATE_TOP)
  311. adj_start = adj_end - size;
  312. if (alignment) {
  313. u64 tmp = adj_start;
  314. unsigned rem;
  315. rem = do_div(tmp, alignment);
  316. if (rem) {
  317. if (flags & DRM_MM_CREATE_TOP)
  318. adj_start -= rem;
  319. else
  320. adj_start += alignment - rem;
  321. }
  322. }
  323. if (adj_start == hole_start) {
  324. hole_node->hole_follows = 0;
  325. list_del(&hole_node->hole_stack);
  326. }
  327. node->start = adj_start;
  328. node->size = size;
  329. node->mm = mm;
  330. node->color = color;
  331. node->allocated = 1;
  332. list_add(&node->node_list, &hole_node->node_list);
  333. drm_mm_interval_tree_add_node(hole_node, node);
  334. BUG_ON(node->start < start);
  335. BUG_ON(node->start < adj_start);
  336. BUG_ON(node->start + node->size > adj_end);
  337. BUG_ON(node->start + node->size > end);
  338. node->hole_follows = 0;
  339. if (__drm_mm_hole_node_start(node) < hole_end) {
  340. list_add(&node->hole_stack, &mm->hole_stack);
  341. node->hole_follows = 1;
  342. }
  343. }
  344. /**
  345. * drm_mm_insert_node_in_range_generic - ranged search for space and insert @node
  346. * @mm: drm_mm to allocate from
  347. * @node: preallocate node to insert
  348. * @size: size of the allocation
  349. * @alignment: alignment of the allocation
  350. * @color: opaque tag value to use for this node
  351. * @start: start of the allowed range for this node
  352. * @end: end of the allowed range for this node
  353. * @sflags: flags to fine-tune the allocation search
  354. * @aflags: flags to fine-tune the allocation behavior
  355. *
  356. * The preallocated node must be cleared to 0.
  357. *
  358. * Returns:
  359. * 0 on success, -ENOSPC if there's no suitable hole.
  360. */
  361. int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
  362. u64 size, unsigned alignment,
  363. unsigned long color,
  364. u64 start, u64 end,
  365. enum drm_mm_search_flags sflags,
  366. enum drm_mm_allocator_flags aflags)
  367. {
  368. struct drm_mm_node *hole_node;
  369. if (WARN_ON(size == 0))
  370. return -EINVAL;
  371. hole_node = drm_mm_search_free_in_range_generic(mm,
  372. size, alignment, color,
  373. start, end, sflags);
  374. if (!hole_node)
  375. return -ENOSPC;
  376. drm_mm_insert_helper_range(hole_node, node,
  377. size, alignment, color,
  378. start, end, aflags);
  379. return 0;
  380. }
  381. EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
  382. /**
  383. * drm_mm_remove_node - Remove a memory node from the allocator.
  384. * @node: drm_mm_node to remove
  385. *
  386. * This just removes a node from its drm_mm allocator. The node does not need to
  387. * be cleared again before it can be re-inserted into this or any other drm_mm
  388. * allocator. It is a bug to call this function on a un-allocated node.
  389. */
  390. void drm_mm_remove_node(struct drm_mm_node *node)
  391. {
  392. struct drm_mm *mm = node->mm;
  393. struct drm_mm_node *prev_node;
  394. if (WARN_ON(!node->allocated))
  395. return;
  396. BUG_ON(node->scanned_block || node->scanned_prev_free
  397. || node->scanned_next_free);
  398. prev_node =
  399. list_entry(node->node_list.prev, struct drm_mm_node, node_list);
  400. if (node->hole_follows) {
  401. BUG_ON(__drm_mm_hole_node_start(node) ==
  402. __drm_mm_hole_node_end(node));
  403. list_del(&node->hole_stack);
  404. } else
  405. BUG_ON(__drm_mm_hole_node_start(node) !=
  406. __drm_mm_hole_node_end(node));
  407. if (!prev_node->hole_follows) {
  408. prev_node->hole_follows = 1;
  409. list_add(&prev_node->hole_stack, &mm->hole_stack);
  410. } else
  411. list_move(&prev_node->hole_stack, &mm->hole_stack);
  412. drm_mm_interval_tree_remove(node, &mm->interval_tree);
  413. list_del(&node->node_list);
  414. node->allocated = 0;
  415. }
  416. EXPORT_SYMBOL(drm_mm_remove_node);
  417. static int check_free_hole(u64 start, u64 end, u64 size, unsigned alignment)
  418. {
  419. if (end - start < size)
  420. return 0;
  421. if (alignment) {
  422. u64 tmp = start;
  423. unsigned rem;
  424. rem = do_div(tmp, alignment);
  425. if (rem)
  426. start += alignment - rem;
  427. }
  428. return end >= start + size;
  429. }
  430. static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
  431. u64 size,
  432. unsigned alignment,
  433. unsigned long color,
  434. enum drm_mm_search_flags flags)
  435. {
  436. struct drm_mm_node *entry;
  437. struct drm_mm_node *best;
  438. u64 adj_start;
  439. u64 adj_end;
  440. u64 best_size;
  441. BUG_ON(mm->scanned_blocks);
  442. best = NULL;
  443. best_size = ~0UL;
  444. __drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
  445. flags & DRM_MM_SEARCH_BELOW) {
  446. u64 hole_size = adj_end - adj_start;
  447. if (mm->color_adjust) {
  448. mm->color_adjust(entry, color, &adj_start, &adj_end);
  449. if (adj_end <= adj_start)
  450. continue;
  451. }
  452. if (!check_free_hole(adj_start, adj_end, size, alignment))
  453. continue;
  454. if (!(flags & DRM_MM_SEARCH_BEST))
  455. return entry;
  456. if (hole_size < best_size) {
  457. best = entry;
  458. best_size = hole_size;
  459. }
  460. }
  461. return best;
  462. }
  463. static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
  464. u64 size,
  465. unsigned alignment,
  466. unsigned long color,
  467. u64 start,
  468. u64 end,
  469. enum drm_mm_search_flags flags)
  470. {
  471. struct drm_mm_node *entry;
  472. struct drm_mm_node *best;
  473. u64 adj_start;
  474. u64 adj_end;
  475. u64 best_size;
  476. BUG_ON(mm->scanned_blocks);
  477. best = NULL;
  478. best_size = ~0UL;
  479. __drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
  480. flags & DRM_MM_SEARCH_BELOW) {
  481. u64 hole_size = adj_end - adj_start;
  482. if (adj_start < start)
  483. adj_start = start;
  484. if (adj_end > end)
  485. adj_end = end;
  486. if (mm->color_adjust) {
  487. mm->color_adjust(entry, color, &adj_start, &adj_end);
  488. if (adj_end <= adj_start)
  489. continue;
  490. }
  491. if (!check_free_hole(adj_start, adj_end, size, alignment))
  492. continue;
  493. if (!(flags & DRM_MM_SEARCH_BEST))
  494. return entry;
  495. if (hole_size < best_size) {
  496. best = entry;
  497. best_size = hole_size;
  498. }
  499. }
  500. return best;
  501. }
  502. /**
  503. * drm_mm_replace_node - move an allocation from @old to @new
  504. * @old: drm_mm_node to remove from the allocator
  505. * @new: drm_mm_node which should inherit @old's allocation
  506. *
  507. * This is useful for when drivers embed the drm_mm_node structure and hence
  508. * can't move allocations by reassigning pointers. It's a combination of remove
  509. * and insert with the guarantee that the allocation start will match.
  510. */
  511. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
  512. {
  513. list_replace(&old->node_list, &new->node_list);
  514. list_replace(&old->hole_stack, &new->hole_stack);
  515. rb_replace_node(&old->rb, &new->rb, &old->mm->interval_tree);
  516. new->hole_follows = old->hole_follows;
  517. new->mm = old->mm;
  518. new->start = old->start;
  519. new->size = old->size;
  520. new->color = old->color;
  521. new->__subtree_last = old->__subtree_last;
  522. old->allocated = 0;
  523. new->allocated = 1;
  524. }
  525. EXPORT_SYMBOL(drm_mm_replace_node);
  526. /**
  527. * DOC: lru scan roaster
  528. *
  529. * Very often GPUs need to have continuous allocations for a given object. When
  530. * evicting objects to make space for a new one it is therefore not most
  531. * efficient when we simply start to select all objects from the tail of an LRU
  532. * until there's a suitable hole: Especially for big objects or nodes that
  533. * otherwise have special allocation constraints there's a good chance we evict
  534. * lots of (smaller) objects unecessarily.
  535. *
  536. * The DRM range allocator supports this use-case through the scanning
  537. * interfaces. First a scan operation needs to be initialized with
  538. * drm_mm_init_scan() or drm_mm_init_scan_with_range(). The the driver adds
  539. * objects to the roaster (probably by walking an LRU list, but this can be
  540. * freely implemented) until a suitable hole is found or there's no further
  541. * evitable object.
  542. *
  543. * The the driver must walk through all objects again in exactly the reverse
  544. * order to restore the allocator state. Note that while the allocator is used
  545. * in the scan mode no other operation is allowed.
  546. *
  547. * Finally the driver evicts all objects selected in the scan. Adding and
  548. * removing an object is O(1), and since freeing a node is also O(1) the overall
  549. * complexity is O(scanned_objects). So like the free stack which needs to be
  550. * walked before a scan operation even begins this is linear in the number of
  551. * objects. It doesn't seem to hurt badly.
  552. */
  553. /**
  554. * drm_mm_init_scan - initialize lru scanning
  555. * @mm: drm_mm to scan
  556. * @size: size of the allocation
  557. * @alignment: alignment of the allocation
  558. * @color: opaque tag value to use for the allocation
  559. *
  560. * This simply sets up the scanning routines with the parameters for the desired
  561. * hole. Note that there's no need to specify allocation flags, since they only
  562. * change the place a node is allocated from within a suitable hole.
  563. *
  564. * Warning:
  565. * As long as the scan list is non-empty, no other operations than
  566. * adding/removing nodes to/from the scan list are allowed.
  567. */
  568. void drm_mm_init_scan(struct drm_mm *mm,
  569. u64 size,
  570. unsigned alignment,
  571. unsigned long color)
  572. {
  573. mm->scan_color = color;
  574. mm->scan_alignment = alignment;
  575. mm->scan_size = size;
  576. mm->scanned_blocks = 0;
  577. mm->scan_hit_start = 0;
  578. mm->scan_hit_end = 0;
  579. mm->scan_check_range = 0;
  580. mm->prev_scanned_node = NULL;
  581. }
  582. EXPORT_SYMBOL(drm_mm_init_scan);
  583. /**
  584. * drm_mm_init_scan - initialize range-restricted lru scanning
  585. * @mm: drm_mm to scan
  586. * @size: size of the allocation
  587. * @alignment: alignment of the allocation
  588. * @color: opaque tag value to use for the allocation
  589. * @start: start of the allowed range for the allocation
  590. * @end: end of the allowed range for the allocation
  591. *
  592. * This simply sets up the scanning routines with the parameters for the desired
  593. * hole. Note that there's no need to specify allocation flags, since they only
  594. * change the place a node is allocated from within a suitable hole.
  595. *
  596. * Warning:
  597. * As long as the scan list is non-empty, no other operations than
  598. * adding/removing nodes to/from the scan list are allowed.
  599. */
  600. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  601. u64 size,
  602. unsigned alignment,
  603. unsigned long color,
  604. u64 start,
  605. u64 end)
  606. {
  607. mm->scan_color = color;
  608. mm->scan_alignment = alignment;
  609. mm->scan_size = size;
  610. mm->scanned_blocks = 0;
  611. mm->scan_hit_start = 0;
  612. mm->scan_hit_end = 0;
  613. mm->scan_start = start;
  614. mm->scan_end = end;
  615. mm->scan_check_range = 1;
  616. mm->prev_scanned_node = NULL;
  617. }
  618. EXPORT_SYMBOL(drm_mm_init_scan_with_range);
  619. /**
  620. * drm_mm_scan_add_block - add a node to the scan list
  621. * @node: drm_mm_node to add
  622. *
  623. * Add a node to the scan list that might be freed to make space for the desired
  624. * hole.
  625. *
  626. * Returns:
  627. * True if a hole has been found, false otherwise.
  628. */
  629. bool drm_mm_scan_add_block(struct drm_mm_node *node)
  630. {
  631. struct drm_mm *mm = node->mm;
  632. struct drm_mm_node *prev_node;
  633. u64 hole_start, hole_end;
  634. u64 adj_start, adj_end;
  635. mm->scanned_blocks++;
  636. BUG_ON(node->scanned_block);
  637. node->scanned_block = 1;
  638. prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
  639. node_list);
  640. node->scanned_preceeds_hole = prev_node->hole_follows;
  641. prev_node->hole_follows = 1;
  642. list_del(&node->node_list);
  643. node->node_list.prev = &prev_node->node_list;
  644. node->node_list.next = &mm->prev_scanned_node->node_list;
  645. mm->prev_scanned_node = node;
  646. adj_start = hole_start = drm_mm_hole_node_start(prev_node);
  647. adj_end = hole_end = drm_mm_hole_node_end(prev_node);
  648. if (mm->scan_check_range) {
  649. if (adj_start < mm->scan_start)
  650. adj_start = mm->scan_start;
  651. if (adj_end > mm->scan_end)
  652. adj_end = mm->scan_end;
  653. }
  654. if (mm->color_adjust)
  655. mm->color_adjust(prev_node, mm->scan_color,
  656. &adj_start, &adj_end);
  657. if (check_free_hole(adj_start, adj_end,
  658. mm->scan_size, mm->scan_alignment)) {
  659. mm->scan_hit_start = hole_start;
  660. mm->scan_hit_end = hole_end;
  661. return true;
  662. }
  663. return false;
  664. }
  665. EXPORT_SYMBOL(drm_mm_scan_add_block);
  666. /**
  667. * drm_mm_scan_remove_block - remove a node from the scan list
  668. * @node: drm_mm_node to remove
  669. *
  670. * Nodes _must_ be removed in the exact same order from the scan list as they
  671. * have been added, otherwise the internal state of the memory manager will be
  672. * corrupted.
  673. *
  674. * When the scan list is empty, the selected memory nodes can be freed. An
  675. * immediately following drm_mm_search_free with !DRM_MM_SEARCH_BEST will then
  676. * return the just freed block (because its at the top of the free_stack list).
  677. *
  678. * Returns:
  679. * True if this block should be evicted, false otherwise. Will always
  680. * return false when no hole has been found.
  681. */
  682. bool drm_mm_scan_remove_block(struct drm_mm_node *node)
  683. {
  684. struct drm_mm *mm = node->mm;
  685. struct drm_mm_node *prev_node;
  686. mm->scanned_blocks--;
  687. BUG_ON(!node->scanned_block);
  688. node->scanned_block = 0;
  689. prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
  690. node_list);
  691. prev_node->hole_follows = node->scanned_preceeds_hole;
  692. list_add(&node->node_list, &prev_node->node_list);
  693. return (drm_mm_hole_node_end(node) > mm->scan_hit_start &&
  694. node->start < mm->scan_hit_end);
  695. }
  696. EXPORT_SYMBOL(drm_mm_scan_remove_block);
  697. /**
  698. * drm_mm_clean - checks whether an allocator is clean
  699. * @mm: drm_mm allocator to check
  700. *
  701. * Returns:
  702. * True if the allocator is completely free, false if there's still a node
  703. * allocated in it.
  704. */
  705. bool drm_mm_clean(struct drm_mm * mm)
  706. {
  707. struct list_head *head = &mm->head_node.node_list;
  708. return (head->next->next == head);
  709. }
  710. EXPORT_SYMBOL(drm_mm_clean);
  711. /**
  712. * drm_mm_init - initialize a drm-mm allocator
  713. * @mm: the drm_mm structure to initialize
  714. * @start: start of the range managed by @mm
  715. * @size: end of the range managed by @mm
  716. *
  717. * Note that @mm must be cleared to 0 before calling this function.
  718. */
  719. void drm_mm_init(struct drm_mm * mm, u64 start, u64 size)
  720. {
  721. INIT_LIST_HEAD(&mm->hole_stack);
  722. mm->scanned_blocks = 0;
  723. /* Clever trick to avoid a special case in the free hole tracking. */
  724. INIT_LIST_HEAD(&mm->head_node.node_list);
  725. mm->head_node.allocated = 0;
  726. mm->head_node.hole_follows = 1;
  727. mm->head_node.scanned_block = 0;
  728. mm->head_node.scanned_prev_free = 0;
  729. mm->head_node.scanned_next_free = 0;
  730. mm->head_node.mm = mm;
  731. mm->head_node.start = start + size;
  732. mm->head_node.size = start - mm->head_node.start;
  733. list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
  734. mm->interval_tree = RB_ROOT;
  735. mm->color_adjust = NULL;
  736. }
  737. EXPORT_SYMBOL(drm_mm_init);
  738. /**
  739. * drm_mm_takedown - clean up a drm_mm allocator
  740. * @mm: drm_mm allocator to clean up
  741. *
  742. * Note that it is a bug to call this function on an allocator which is not
  743. * clean.
  744. */
  745. void drm_mm_takedown(struct drm_mm * mm)
  746. {
  747. WARN(!list_empty(&mm->head_node.node_list),
  748. "Memory manager not clean during takedown.\n");
  749. }
  750. EXPORT_SYMBOL(drm_mm_takedown);
  751. static u64 drm_mm_debug_hole(struct drm_mm_node *entry,
  752. const char *prefix)
  753. {
  754. u64 hole_start, hole_end, hole_size;
  755. if (entry->hole_follows) {
  756. hole_start = drm_mm_hole_node_start(entry);
  757. hole_end = drm_mm_hole_node_end(entry);
  758. hole_size = hole_end - hole_start;
  759. pr_debug("%s %#llx-%#llx: %llu: free\n", prefix, hole_start,
  760. hole_end, hole_size);
  761. return hole_size;
  762. }
  763. return 0;
  764. }
  765. /**
  766. * drm_mm_debug_table - dump allocator state to dmesg
  767. * @mm: drm_mm allocator to dump
  768. * @prefix: prefix to use for dumping to dmesg
  769. */
  770. void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
  771. {
  772. struct drm_mm_node *entry;
  773. u64 total_used = 0, total_free = 0, total = 0;
  774. total_free += drm_mm_debug_hole(&mm->head_node, prefix);
  775. drm_mm_for_each_node(entry, mm) {
  776. pr_debug("%s %#llx-%#llx: %llu: used\n", prefix, entry->start,
  777. entry->start + entry->size, entry->size);
  778. total_used += entry->size;
  779. total_free += drm_mm_debug_hole(entry, prefix);
  780. }
  781. total = total_free + total_used;
  782. pr_debug("%s total: %llu, used %llu free %llu\n", prefix, total,
  783. total_used, total_free);
  784. }
  785. EXPORT_SYMBOL(drm_mm_debug_table);
  786. #if defined(CONFIG_DEBUG_FS)
  787. static u64 drm_mm_dump_hole(struct seq_file *m, struct drm_mm_node *entry)
  788. {
  789. u64 hole_start, hole_end, hole_size;
  790. if (entry->hole_follows) {
  791. hole_start = drm_mm_hole_node_start(entry);
  792. hole_end = drm_mm_hole_node_end(entry);
  793. hole_size = hole_end - hole_start;
  794. seq_printf(m, "%#018llx-%#018llx: %llu: free\n", hole_start,
  795. hole_end, hole_size);
  796. return hole_size;
  797. }
  798. return 0;
  799. }
  800. /**
  801. * drm_mm_dump_table - dump allocator state to a seq_file
  802. * @m: seq_file to dump to
  803. * @mm: drm_mm allocator to dump
  804. */
  805. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
  806. {
  807. struct drm_mm_node *entry;
  808. u64 total_used = 0, total_free = 0, total = 0;
  809. total_free += drm_mm_dump_hole(m, &mm->head_node);
  810. drm_mm_for_each_node(entry, mm) {
  811. seq_printf(m, "%#018llx-%#018llx: %llu: used\n", entry->start,
  812. entry->start + entry->size, entry->size);
  813. total_used += entry->size;
  814. total_free += drm_mm_dump_hole(m, entry);
  815. }
  816. total = total_free + total_used;
  817. seq_printf(m, "total: %llu, used %llu free %llu\n", total,
  818. total_used, total_free);
  819. return 0;
  820. }
  821. EXPORT_SYMBOL(drm_mm_dump_table);
  822. #endif