drm_mm.c 25 KB

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