drm_mm.c 25 KB

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