drm_mm.c 25 KB

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