drm_mm.c 25 KB

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