drm_mm.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /**************************************************************************
  2. *
  3. * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
  4. * Copyright 2016 Intel Corporation
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. *
  28. **************************************************************************/
  29. /*
  30. * Authors:
  31. * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
  32. */
  33. #ifndef _DRM_MM_H_
  34. #define _DRM_MM_H_
  35. /*
  36. * Generic range manager structs
  37. */
  38. #include <linux/bug.h>
  39. #include <linux/rbtree.h>
  40. #include <linux/limits.h>
  41. #include <linux/mm_types.h>
  42. #include <linux/list.h>
  43. #include <linux/spinlock.h>
  44. #ifdef CONFIG_DRM_DEBUG_MM
  45. #include <linux/stackdepot.h>
  46. #endif
  47. #include <linux/types.h>
  48. #include <drm/drm_print.h>
  49. #ifdef CONFIG_DRM_DEBUG_MM
  50. #define DRM_MM_BUG_ON(expr) BUG_ON(expr)
  51. #else
  52. #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
  53. #endif
  54. /**
  55. * enum drm_mm_insert_mode - control search and allocation behaviour
  56. *
  57. * The &struct drm_mm range manager supports finding a suitable modes using
  58. * a number of search trees. These trees are oranised by size, by address and
  59. * in most recent eviction order. This allows the user to find either the
  60. * smallest hole to reuse, the lowest or highest address to reuse, or simply
  61. * reuse the most recent eviction that fits. When allocating the &drm_mm_node
  62. * from within the hole, the &drm_mm_insert_mode also dictate whether to
  63. * allocate the lowest matching address or the highest.
  64. */
  65. enum drm_mm_insert_mode {
  66. /**
  67. * @DRM_MM_INSERT_BEST:
  68. *
  69. * Search for the smallest hole (within the search range) that fits
  70. * the desired node.
  71. *
  72. * Allocates the node from the bottom of the found hole.
  73. */
  74. DRM_MM_INSERT_BEST = 0,
  75. /**
  76. * @DRM_MM_INSERT_LOW:
  77. *
  78. * Search for the lowest hole (address closest to 0, within the search
  79. * range) that fits the desired node.
  80. *
  81. * Allocates the node from the bottom of the found hole.
  82. */
  83. DRM_MM_INSERT_LOW,
  84. /**
  85. * @DRM_MM_INSERT_HIGH:
  86. *
  87. * Search for the highest hole (address closest to U64_MAX, within the
  88. * search range) that fits the desired node.
  89. *
  90. * Allocates the node from the *top* of the found hole. The specified
  91. * alignment for the node is applied to the base of the node
  92. * (&drm_mm_node.start).
  93. */
  94. DRM_MM_INSERT_HIGH,
  95. /**
  96. * @DRM_MM_INSERT_EVICT:
  97. *
  98. * Search for the most recently evicted hole (within the search range)
  99. * that fits the desired node. This is appropriate for use immediately
  100. * after performing an eviction scan (see drm_mm_scan_init()) and
  101. * removing the selected nodes to form a hole.
  102. *
  103. * Allocates the node from the bottom of the found hole.
  104. */
  105. DRM_MM_INSERT_EVICT,
  106. /**
  107. * @DRM_MM_INSERT_ONCE:
  108. *
  109. * Only check the first hole for suitablity and report -ENOSPC
  110. * immediately otherwise, rather than check every hole until a
  111. * suitable one is found. Can only be used in conjunction with another
  112. * search method such as DRM_MM_INSERT_HIGH or DRM_MM_INSERT_LOW.
  113. */
  114. DRM_MM_INSERT_ONCE = BIT(31),
  115. /**
  116. * @DRM_MM_INSERT_HIGHEST:
  117. *
  118. * Only check the highest hole (the hole with the largest address) and
  119. * insert the node at the top of the hole or report -ENOSPC if
  120. * unsuitable.
  121. *
  122. * Does not search all holes.
  123. */
  124. DRM_MM_INSERT_HIGHEST = DRM_MM_INSERT_HIGH | DRM_MM_INSERT_ONCE,
  125. /**
  126. * @DRM_MM_INSERT_LOWEST:
  127. *
  128. * Only check the lowest hole (the hole with the smallest address) and
  129. * insert the node at the bottom of the hole or report -ENOSPC if
  130. * unsuitable.
  131. *
  132. * Does not search all holes.
  133. */
  134. DRM_MM_INSERT_LOWEST = DRM_MM_INSERT_LOW | DRM_MM_INSERT_ONCE,
  135. };
  136. /**
  137. * struct drm_mm_node - allocated block in the DRM allocator
  138. *
  139. * This represents an allocated block in a &drm_mm allocator. Except for
  140. * pre-reserved nodes inserted using drm_mm_reserve_node() the structure is
  141. * entirely opaque and should only be accessed through the provided funcions.
  142. * Since allocation of these nodes is entirely handled by the driver they can be
  143. * embedded.
  144. */
  145. struct drm_mm_node {
  146. /** @color: Opaque driver-private tag. */
  147. unsigned long color;
  148. /** @start: Start address of the allocated block. */
  149. u64 start;
  150. /** @size: Size of the allocated block. */
  151. u64 size;
  152. /* private: */
  153. struct drm_mm *mm;
  154. struct list_head node_list;
  155. struct list_head hole_stack;
  156. struct rb_node rb;
  157. struct rb_node rb_hole_size;
  158. struct rb_node rb_hole_addr;
  159. u64 __subtree_last;
  160. u64 hole_size;
  161. u64 subtree_max_hole;
  162. unsigned long flags;
  163. #define DRM_MM_NODE_ALLOCATED_BIT 0
  164. #define DRM_MM_NODE_SCANNED_BIT 1
  165. #ifdef CONFIG_DRM_DEBUG_MM
  166. depot_stack_handle_t stack;
  167. #endif
  168. };
  169. /**
  170. * struct drm_mm - DRM allocator
  171. *
  172. * DRM range allocator with a few special functions and features geared towards
  173. * managing GPU memory. Except for the @color_adjust callback the structure is
  174. * entirely opaque and should only be accessed through the provided functions
  175. * and macros. This structure can be embedded into larger driver structures.
  176. */
  177. struct drm_mm {
  178. /**
  179. * @color_adjust:
  180. *
  181. * Optional driver callback to further apply restrictions on a hole. The
  182. * node argument points at the node containing the hole from which the
  183. * block would be allocated (see drm_mm_hole_follows() and friends). The
  184. * other arguments are the size of the block to be allocated. The driver
  185. * can adjust the start and end as needed to e.g. insert guard pages.
  186. */
  187. void (*color_adjust)(const struct drm_mm_node *node,
  188. unsigned long color,
  189. u64 *start, u64 *end);
  190. /* private: */
  191. /* List of all memory nodes that immediately precede a free hole. */
  192. struct list_head hole_stack;
  193. /* head_node.node_list is the list of all memory nodes, ordered
  194. * according to the (increasing) start address of the memory node. */
  195. struct drm_mm_node head_node;
  196. /* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
  197. struct rb_root_cached interval_tree;
  198. struct rb_root_cached holes_size;
  199. struct rb_root holes_addr;
  200. unsigned long scan_active;
  201. };
  202. /**
  203. * struct drm_mm_scan - DRM allocator eviction roaster data
  204. *
  205. * This structure tracks data needed for the eviction roaster set up using
  206. * drm_mm_scan_init(), and used with drm_mm_scan_add_block() and
  207. * drm_mm_scan_remove_block(). The structure is entirely opaque and should only
  208. * be accessed through the provided functions and macros. It is meant to be
  209. * allocated temporarily by the driver on the stack.
  210. */
  211. struct drm_mm_scan {
  212. /* private: */
  213. struct drm_mm *mm;
  214. u64 size;
  215. u64 alignment;
  216. u64 remainder_mask;
  217. u64 range_start;
  218. u64 range_end;
  219. u64 hit_start;
  220. u64 hit_end;
  221. unsigned long color;
  222. enum drm_mm_insert_mode mode;
  223. };
  224. /**
  225. * drm_mm_node_allocated - checks whether a node is allocated
  226. * @node: drm_mm_node to check
  227. *
  228. * Drivers are required to clear a node prior to using it with the
  229. * drm_mm range manager.
  230. *
  231. * Drivers should use this helper for proper encapsulation of drm_mm
  232. * internals.
  233. *
  234. * Returns:
  235. * True if the @node is allocated.
  236. */
  237. static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
  238. {
  239. return test_bit(DRM_MM_NODE_ALLOCATED_BIT, &node->flags);
  240. }
  241. /**
  242. * drm_mm_initialized - checks whether an allocator is initialized
  243. * @mm: drm_mm to check
  244. *
  245. * Drivers should clear the struct drm_mm prior to initialisation if they
  246. * want to use this function.
  247. *
  248. * Drivers should use this helper for proper encapsulation of drm_mm
  249. * internals.
  250. *
  251. * Returns:
  252. * True if the @mm is initialized.
  253. */
  254. static inline bool drm_mm_initialized(const struct drm_mm *mm)
  255. {
  256. return READ_ONCE(mm->hole_stack.next);
  257. }
  258. /**
  259. * drm_mm_hole_follows - checks whether a hole follows this node
  260. * @node: drm_mm_node to check
  261. *
  262. * Holes are embedded into the drm_mm using the tail of a drm_mm_node.
  263. * If you wish to know whether a hole follows this particular node,
  264. * query this function. See also drm_mm_hole_node_start() and
  265. * drm_mm_hole_node_end().
  266. *
  267. * Returns:
  268. * True if a hole follows the @node.
  269. */
  270. static inline bool drm_mm_hole_follows(const struct drm_mm_node *node)
  271. {
  272. return node->hole_size;
  273. }
  274. static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
  275. {
  276. return hole_node->start + hole_node->size;
  277. }
  278. /**
  279. * drm_mm_hole_node_start - computes the start of the hole following @node
  280. * @hole_node: drm_mm_node which implicitly tracks the following hole
  281. *
  282. * This is useful for driver-specific debug dumpers. Otherwise drivers should
  283. * not inspect holes themselves. Drivers must check first whether a hole indeed
  284. * follows by looking at drm_mm_hole_follows()
  285. *
  286. * Returns:
  287. * Start of the subsequent hole.
  288. */
  289. static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
  290. {
  291. DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node));
  292. return __drm_mm_hole_node_start(hole_node);
  293. }
  294. static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
  295. {
  296. return list_next_entry(hole_node, node_list)->start;
  297. }
  298. /**
  299. * drm_mm_hole_node_end - computes the end of the hole following @node
  300. * @hole_node: drm_mm_node which implicitly tracks the following hole
  301. *
  302. * This is useful for driver-specific debug dumpers. Otherwise drivers should
  303. * not inspect holes themselves. Drivers must check first whether a hole indeed
  304. * follows by looking at drm_mm_hole_follows().
  305. *
  306. * Returns:
  307. * End of the subsequent hole.
  308. */
  309. static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
  310. {
  311. return __drm_mm_hole_node_end(hole_node);
  312. }
  313. /**
  314. * drm_mm_nodes - list of nodes under the drm_mm range manager
  315. * @mm: the struct drm_mm range manager
  316. *
  317. * As the drm_mm range manager hides its node_list deep with its
  318. * structure, extracting it looks painful and repetitive. This is
  319. * not expected to be used outside of the drm_mm_for_each_node()
  320. * macros and similar internal functions.
  321. *
  322. * Returns:
  323. * The node list, may be empty.
  324. */
  325. #define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
  326. /**
  327. * drm_mm_for_each_node - iterator to walk over all allocated nodes
  328. * @entry: &struct drm_mm_node to assign to in each iteration step
  329. * @mm: &drm_mm allocator to walk
  330. *
  331. * This iterator walks over all nodes in the range allocator. It is implemented
  332. * with list_for_each(), so not save against removal of elements.
  333. */
  334. #define drm_mm_for_each_node(entry, mm) \
  335. list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
  336. /**
  337. * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
  338. * @entry: &struct drm_mm_node to assign to in each iteration step
  339. * @next: &struct drm_mm_node to store the next step
  340. * @mm: &drm_mm allocator to walk
  341. *
  342. * This iterator walks over all nodes in the range allocator. It is implemented
  343. * with list_for_each_safe(), so save against removal of elements.
  344. */
  345. #define drm_mm_for_each_node_safe(entry, next, mm) \
  346. list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
  347. /**
  348. * drm_mm_for_each_hole - iterator to walk over all holes
  349. * @pos: &drm_mm_node used internally to track progress
  350. * @mm: &drm_mm allocator to walk
  351. * @hole_start: ulong variable to assign the hole start to on each iteration
  352. * @hole_end: ulong variable to assign the hole end to on each iteration
  353. *
  354. * This iterator walks over all holes in the range allocator. It is implemented
  355. * with list_for_each(), so not save against removal of elements. @entry is used
  356. * internally and will not reflect a real drm_mm_node for the very first hole.
  357. * Hence users of this iterator may not access it.
  358. *
  359. * Implementation Note:
  360. * We need to inline list_for_each_entry in order to be able to set hole_start
  361. * and hole_end on each iteration while keeping the macro sane.
  362. */
  363. #define drm_mm_for_each_hole(pos, mm, hole_start, hole_end) \
  364. for (pos = list_first_entry(&(mm)->hole_stack, \
  365. typeof(*pos), hole_stack); \
  366. &pos->hole_stack != &(mm)->hole_stack ? \
  367. hole_start = drm_mm_hole_node_start(pos), \
  368. hole_end = hole_start + pos->hole_size, \
  369. 1 : 0; \
  370. pos = list_next_entry(pos, hole_stack))
  371. /*
  372. * Basic range manager support (drm_mm.c)
  373. */
  374. int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
  375. int drm_mm_insert_node_in_range(struct drm_mm *mm,
  376. struct drm_mm_node *node,
  377. u64 size,
  378. u64 alignment,
  379. unsigned long color,
  380. u64 start,
  381. u64 end,
  382. enum drm_mm_insert_mode mode);
  383. /**
  384. * drm_mm_insert_node_generic - search for space and insert @node
  385. * @mm: drm_mm to allocate from
  386. * @node: preallocate node to insert
  387. * @size: size of the allocation
  388. * @alignment: alignment of the allocation
  389. * @color: opaque tag value to use for this node
  390. * @mode: fine-tune the allocation search and placement
  391. *
  392. * This is a simplified version of drm_mm_insert_node_in_range() with no
  393. * range restrictions applied.
  394. *
  395. * The preallocated node must be cleared to 0.
  396. *
  397. * Returns:
  398. * 0 on success, -ENOSPC if there's no suitable hole.
  399. */
  400. static inline int
  401. drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
  402. u64 size, u64 alignment,
  403. unsigned long color,
  404. enum drm_mm_insert_mode mode)
  405. {
  406. return drm_mm_insert_node_in_range(mm, node,
  407. size, alignment, color,
  408. 0, U64_MAX, mode);
  409. }
  410. /**
  411. * drm_mm_insert_node - search for space and insert @node
  412. * @mm: drm_mm to allocate from
  413. * @node: preallocate node to insert
  414. * @size: size of the allocation
  415. *
  416. * This is a simplified version of drm_mm_insert_node_generic() with @color set
  417. * to 0.
  418. *
  419. * The preallocated node must be cleared to 0.
  420. *
  421. * Returns:
  422. * 0 on success, -ENOSPC if there's no suitable hole.
  423. */
  424. static inline int drm_mm_insert_node(struct drm_mm *mm,
  425. struct drm_mm_node *node,
  426. u64 size)
  427. {
  428. return drm_mm_insert_node_generic(mm, node, size, 0, 0, 0);
  429. }
  430. void drm_mm_remove_node(struct drm_mm_node *node);
  431. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
  432. void drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
  433. void drm_mm_takedown(struct drm_mm *mm);
  434. /**
  435. * drm_mm_clean - checks whether an allocator is clean
  436. * @mm: drm_mm allocator to check
  437. *
  438. * Returns:
  439. * True if the allocator is completely free, false if there's still a node
  440. * allocated in it.
  441. */
  442. static inline bool drm_mm_clean(const struct drm_mm *mm)
  443. {
  444. return list_empty(drm_mm_nodes(mm));
  445. }
  446. struct drm_mm_node *
  447. __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
  448. /**
  449. * drm_mm_for_each_node_in_range - iterator to walk over a range of
  450. * allocated nodes
  451. * @node__: drm_mm_node structure to assign to in each iteration step
  452. * @mm__: drm_mm allocator to walk
  453. * @start__: starting offset, the first node will overlap this
  454. * @end__: ending offset, the last node will start before this (but may overlap)
  455. *
  456. * This iterator walks over all nodes in the range allocator that lie
  457. * between @start and @end. It is implemented similarly to list_for_each(),
  458. * but using the internal interval tree to accelerate the search for the
  459. * starting node, and so not safe against removal of elements. It assumes
  460. * that @end is within (or is the upper limit of) the drm_mm allocator.
  461. * If [@start, @end] are beyond the range of the drm_mm, the iterator may walk
  462. * over the special _unallocated_ &drm_mm.head_node, and may even continue
  463. * indefinitely.
  464. */
  465. #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__) \
  466. for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
  467. node__->start < (end__); \
  468. node__ = list_next_entry(node__, node_list))
  469. void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
  470. struct drm_mm *mm,
  471. u64 size, u64 alignment, unsigned long color,
  472. u64 start, u64 end,
  473. enum drm_mm_insert_mode mode);
  474. /**
  475. * drm_mm_scan_init - initialize lru scanning
  476. * @scan: scan state
  477. * @mm: drm_mm to scan
  478. * @size: size of the allocation
  479. * @alignment: alignment of the allocation
  480. * @color: opaque tag value to use for the allocation
  481. * @mode: fine-tune the allocation search and placement
  482. *
  483. * This is a simplified version of drm_mm_scan_init_with_range() with no range
  484. * restrictions applied.
  485. *
  486. * This simply sets up the scanning routines with the parameters for the desired
  487. * hole.
  488. *
  489. * Warning:
  490. * As long as the scan list is non-empty, no other operations than
  491. * adding/removing nodes to/from the scan list are allowed.
  492. */
  493. static inline void drm_mm_scan_init(struct drm_mm_scan *scan,
  494. struct drm_mm *mm,
  495. u64 size,
  496. u64 alignment,
  497. unsigned long color,
  498. enum drm_mm_insert_mode mode)
  499. {
  500. drm_mm_scan_init_with_range(scan, mm,
  501. size, alignment, color,
  502. 0, U64_MAX, mode);
  503. }
  504. bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
  505. struct drm_mm_node *node);
  506. bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
  507. struct drm_mm_node *node);
  508. struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
  509. void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p);
  510. #endif