drm_mm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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/kernel.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 <drm/drm_print.h>
  48. #ifdef CONFIG_DRM_DEBUG_MM
  49. #define DRM_MM_BUG_ON(expr) BUG_ON(expr)
  50. #else
  51. #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
  52. #endif
  53. /**
  54. * enum drm_mm_insert_mode - control search and allocation behaviour
  55. *
  56. * The &struct drm_mm range manager supports finding a suitable modes using
  57. * a number of search trees. These trees are oranised by size, by address and
  58. * in most recent eviction order. This allows the user to find either the
  59. * smallest hole to reuse, the lowest or highest address to reuse, or simply
  60. * reuse the most recent eviction that fits. When allocating the &drm_mm_node
  61. * from within the hole, the &drm_mm_insert_mode also dictate whether to
  62. * allocate the lowest matching address or the highest.
  63. */
  64. enum drm_mm_insert_mode {
  65. /**
  66. * @DRM_MM_INSERT_BEST:
  67. *
  68. * Search for the smallest hole (within the search range) that fits
  69. * the desired node.
  70. *
  71. * Allocates the node from the bottom of the found hole.
  72. */
  73. DRM_MM_INSERT_BEST = 0,
  74. /**
  75. * @DRM_MM_INSERT_LOW:
  76. *
  77. * Search for the lowest hole (address closest to 0, within the search
  78. * range) that fits the desired node.
  79. *
  80. * Allocates the node from the bottom of the found hole.
  81. */
  82. DRM_MM_INSERT_LOW,
  83. /**
  84. * @DRM_MM_INSERT_HIGH:
  85. *
  86. * Search for the highest hole (address closest to U64_MAX, within the
  87. * search range) that fits the desired node.
  88. *
  89. * Allocates the node from the *top* of the found hole. The specified
  90. * alignment for the node is applied to the base of the node
  91. * (&drm_mm_node.start).
  92. */
  93. DRM_MM_INSERT_HIGH,
  94. /**
  95. * @DRM_MM_INSERT_EVICT:
  96. *
  97. * Search for the most recently evicted hole (within the search range)
  98. * that fits the desired node. This is appropriate for use immediately
  99. * after performing an eviction scan (see drm_mm_scan_init()) and
  100. * removing the selected nodes to form a hole.
  101. *
  102. * Allocates the node from the bottom of the found hole.
  103. */
  104. DRM_MM_INSERT_EVICT,
  105. };
  106. /**
  107. * struct drm_mm_node - allocated block in the DRM allocator
  108. *
  109. * This represents an allocated block in a &drm_mm allocator. Except for
  110. * pre-reserved nodes inserted using drm_mm_reserve_node() the structure is
  111. * entirely opaque and should only be accessed through the provided funcions.
  112. * Since allocation of these nodes is entirely handled by the driver they can be
  113. * embedded.
  114. */
  115. struct drm_mm_node {
  116. /** @color: Opaque driver-private tag. */
  117. unsigned long color;
  118. /** @start: Start address of the allocated block. */
  119. u64 start;
  120. /** @size: Size of the allocated block. */
  121. u64 size;
  122. /* private: */
  123. struct drm_mm *mm;
  124. struct list_head node_list;
  125. struct list_head hole_stack;
  126. struct rb_node rb;
  127. struct rb_node rb_hole_size;
  128. struct rb_node rb_hole_addr;
  129. u64 __subtree_last;
  130. u64 hole_size;
  131. bool allocated : 1;
  132. bool scanned_block : 1;
  133. #ifdef CONFIG_DRM_DEBUG_MM
  134. depot_stack_handle_t stack;
  135. #endif
  136. };
  137. /**
  138. * struct drm_mm - DRM allocator
  139. *
  140. * DRM range allocator with a few special functions and features geared towards
  141. * managing GPU memory. Except for the @color_adjust callback the structure is
  142. * entirely opaque and should only be accessed through the provided functions
  143. * and macros. This structure can be embedded into larger driver structures.
  144. */
  145. struct drm_mm {
  146. /**
  147. * @color_adjust:
  148. *
  149. * Optional driver callback to further apply restrictions on a hole. The
  150. * node argument points at the node containing the hole from which the
  151. * block would be allocated (see drm_mm_hole_follows() and friends). The
  152. * other arguments are the size of the block to be allocated. The driver
  153. * can adjust the start and end as needed to e.g. insert guard pages.
  154. */
  155. void (*color_adjust)(const struct drm_mm_node *node,
  156. unsigned long color,
  157. u64 *start, u64 *end);
  158. /* private: */
  159. /* List of all memory nodes that immediately precede a free hole. */
  160. struct list_head hole_stack;
  161. /* head_node.node_list is the list of all memory nodes, ordered
  162. * according to the (increasing) start address of the memory node. */
  163. struct drm_mm_node head_node;
  164. /* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
  165. struct rb_root_cached interval_tree;
  166. struct rb_root holes_size;
  167. struct rb_root holes_addr;
  168. unsigned long scan_active;
  169. };
  170. /**
  171. * struct drm_mm_scan - DRM allocator eviction roaster data
  172. *
  173. * This structure tracks data needed for the eviction roaster set up using
  174. * drm_mm_scan_init(), and used with drm_mm_scan_add_block() and
  175. * drm_mm_scan_remove_block(). The structure is entirely opaque and should only
  176. * be accessed through the provided functions and macros. It is meant to be
  177. * allocated temporarily by the driver on the stack.
  178. */
  179. struct drm_mm_scan {
  180. /* private: */
  181. struct drm_mm *mm;
  182. u64 size;
  183. u64 alignment;
  184. u64 remainder_mask;
  185. u64 range_start;
  186. u64 range_end;
  187. u64 hit_start;
  188. u64 hit_end;
  189. unsigned long color;
  190. enum drm_mm_insert_mode mode;
  191. };
  192. /**
  193. * drm_mm_node_allocated - checks whether a node is allocated
  194. * @node: drm_mm_node to check
  195. *
  196. * Drivers are required to clear a node prior to using it with the
  197. * drm_mm range manager.
  198. *
  199. * Drivers should use this helper for proper encapsulation of drm_mm
  200. * internals.
  201. *
  202. * Returns:
  203. * True if the @node is allocated.
  204. */
  205. static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
  206. {
  207. return node->allocated;
  208. }
  209. /**
  210. * drm_mm_initialized - checks whether an allocator is initialized
  211. * @mm: drm_mm to check
  212. *
  213. * Drivers should clear the struct drm_mm prior to initialisation if they
  214. * want to use this function.
  215. *
  216. * Drivers should use this helper for proper encapsulation of drm_mm
  217. * internals.
  218. *
  219. * Returns:
  220. * True if the @mm is initialized.
  221. */
  222. static inline bool drm_mm_initialized(const struct drm_mm *mm)
  223. {
  224. return mm->hole_stack.next;
  225. }
  226. /**
  227. * drm_mm_hole_follows - checks whether a hole follows this node
  228. * @node: drm_mm_node to check
  229. *
  230. * Holes are embedded into the drm_mm using the tail of a drm_mm_node.
  231. * If you wish to know whether a hole follows this particular node,
  232. * query this function. See also drm_mm_hole_node_start() and
  233. * drm_mm_hole_node_end().
  234. *
  235. * Returns:
  236. * True if a hole follows the @node.
  237. */
  238. static inline bool drm_mm_hole_follows(const struct drm_mm_node *node)
  239. {
  240. return node->hole_size;
  241. }
  242. static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
  243. {
  244. return hole_node->start + hole_node->size;
  245. }
  246. /**
  247. * drm_mm_hole_node_start - computes the start of the hole following @node
  248. * @hole_node: drm_mm_node which implicitly tracks the following hole
  249. *
  250. * This is useful for driver-specific debug dumpers. Otherwise drivers should
  251. * not inspect holes themselves. Drivers must check first whether a hole indeed
  252. * follows by looking at drm_mm_hole_follows()
  253. *
  254. * Returns:
  255. * Start of the subsequent hole.
  256. */
  257. static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
  258. {
  259. DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node));
  260. return __drm_mm_hole_node_start(hole_node);
  261. }
  262. static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
  263. {
  264. return list_next_entry(hole_node, node_list)->start;
  265. }
  266. /**
  267. * drm_mm_hole_node_end - computes the end of the hole following @node
  268. * @hole_node: drm_mm_node which implicitly tracks the following hole
  269. *
  270. * This is useful for driver-specific debug dumpers. Otherwise drivers should
  271. * not inspect holes themselves. Drivers must check first whether a hole indeed
  272. * follows by looking at drm_mm_hole_follows().
  273. *
  274. * Returns:
  275. * End of the subsequent hole.
  276. */
  277. static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
  278. {
  279. return __drm_mm_hole_node_end(hole_node);
  280. }
  281. /**
  282. * drm_mm_nodes - list of nodes under the drm_mm range manager
  283. * @mm: the struct drm_mm range manger
  284. *
  285. * As the drm_mm range manager hides its node_list deep with its
  286. * structure, extracting it looks painful and repetitive. This is
  287. * not expected to be used outside of the drm_mm_for_each_node()
  288. * macros and similar internal functions.
  289. *
  290. * Returns:
  291. * The node list, may be empty.
  292. */
  293. #define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
  294. /**
  295. * drm_mm_for_each_node - iterator to walk over all allocated nodes
  296. * @entry: &struct drm_mm_node to assign to in each iteration step
  297. * @mm: &drm_mm allocator to walk
  298. *
  299. * This iterator walks over all nodes in the range allocator. It is implemented
  300. * with list_for_each(), so not save against removal of elements.
  301. */
  302. #define drm_mm_for_each_node(entry, mm) \
  303. list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
  304. /**
  305. * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
  306. * @entry: &struct drm_mm_node to assign to in each iteration step
  307. * @next: &struct drm_mm_node to store the next step
  308. * @mm: &drm_mm allocator to walk
  309. *
  310. * This iterator walks over all nodes in the range allocator. It is implemented
  311. * with list_for_each_safe(), so save against removal of elements.
  312. */
  313. #define drm_mm_for_each_node_safe(entry, next, mm) \
  314. list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
  315. /**
  316. * drm_mm_for_each_hole - iterator to walk over all holes
  317. * @pos: &drm_mm_node used internally to track progress
  318. * @mm: &drm_mm allocator to walk
  319. * @hole_start: ulong variable to assign the hole start to on each iteration
  320. * @hole_end: ulong variable to assign the hole end to on each iteration
  321. *
  322. * This iterator walks over all holes in the range allocator. It is implemented
  323. * with list_for_each(), so not save against removal of elements. @entry is used
  324. * internally and will not reflect a real drm_mm_node for the very first hole.
  325. * Hence users of this iterator may not access it.
  326. *
  327. * Implementation Note:
  328. * We need to inline list_for_each_entry in order to be able to set hole_start
  329. * and hole_end on each iteration while keeping the macro sane.
  330. */
  331. #define drm_mm_for_each_hole(pos, mm, hole_start, hole_end) \
  332. for (pos = list_first_entry(&(mm)->hole_stack, \
  333. typeof(*pos), hole_stack); \
  334. &pos->hole_stack != &(mm)->hole_stack ? \
  335. hole_start = drm_mm_hole_node_start(pos), \
  336. hole_end = hole_start + pos->hole_size, \
  337. 1 : 0; \
  338. pos = list_next_entry(pos, hole_stack))
  339. /*
  340. * Basic range manager support (drm_mm.c)
  341. */
  342. int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
  343. int drm_mm_insert_node_in_range(struct drm_mm *mm,
  344. struct drm_mm_node *node,
  345. u64 size,
  346. u64 alignment,
  347. unsigned long color,
  348. u64 start,
  349. u64 end,
  350. enum drm_mm_insert_mode mode);
  351. /**
  352. * drm_mm_insert_node_generic - search for space and insert @node
  353. * @mm: drm_mm to allocate from
  354. * @node: preallocate node to insert
  355. * @size: size of the allocation
  356. * @alignment: alignment of the allocation
  357. * @color: opaque tag value to use for this node
  358. * @mode: fine-tune the allocation search and placement
  359. *
  360. * This is a simplified version of drm_mm_insert_node_in_range_generic() with no
  361. * range restrictions applied.
  362. *
  363. * The preallocated node must be cleared to 0.
  364. *
  365. * Returns:
  366. * 0 on success, -ENOSPC if there's no suitable hole.
  367. */
  368. static inline int
  369. drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
  370. u64 size, u64 alignment,
  371. unsigned long color,
  372. enum drm_mm_insert_mode mode)
  373. {
  374. return drm_mm_insert_node_in_range(mm, node,
  375. size, alignment, color,
  376. 0, U64_MAX, mode);
  377. }
  378. /**
  379. * drm_mm_insert_node - search for space and insert @node
  380. * @mm: drm_mm to allocate from
  381. * @node: preallocate node to insert
  382. * @size: size of the allocation
  383. *
  384. * This is a simplified version of drm_mm_insert_node_generic() with @color set
  385. * to 0.
  386. *
  387. * The preallocated node must be cleared to 0.
  388. *
  389. * Returns:
  390. * 0 on success, -ENOSPC if there's no suitable hole.
  391. */
  392. static inline int drm_mm_insert_node(struct drm_mm *mm,
  393. struct drm_mm_node *node,
  394. u64 size)
  395. {
  396. return drm_mm_insert_node_generic(mm, node, size, 0, 0, 0);
  397. }
  398. void drm_mm_remove_node(struct drm_mm_node *node);
  399. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
  400. void drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
  401. void drm_mm_takedown(struct drm_mm *mm);
  402. /**
  403. * drm_mm_clean - checks whether an allocator is clean
  404. * @mm: drm_mm allocator to check
  405. *
  406. * Returns:
  407. * True if the allocator is completely free, false if there's still a node
  408. * allocated in it.
  409. */
  410. static inline bool drm_mm_clean(const struct drm_mm *mm)
  411. {
  412. return list_empty(drm_mm_nodes(mm));
  413. }
  414. struct drm_mm_node *
  415. __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
  416. /**
  417. * drm_mm_for_each_node_in_range - iterator to walk over a range of
  418. * allocated nodes
  419. * @node__: drm_mm_node structure to assign to in each iteration step
  420. * @mm__: drm_mm allocator to walk
  421. * @start__: starting offset, the first node will overlap this
  422. * @end__: ending offset, the last node will start before this (but may overlap)
  423. *
  424. * This iterator walks over all nodes in the range allocator that lie
  425. * between @start and @end. It is implemented similarly to list_for_each(),
  426. * but using the internal interval tree to accelerate the search for the
  427. * starting node, and so not safe against removal of elements. It assumes
  428. * that @end is within (or is the upper limit of) the drm_mm allocator.
  429. * If [@start, @end] are beyond the range of the drm_mm, the iterator may walk
  430. * over the special _unallocated_ &drm_mm.head_node, and may even continue
  431. * indefinitely.
  432. */
  433. #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__) \
  434. for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
  435. node__->start < (end__); \
  436. node__ = list_next_entry(node__, node_list))
  437. void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
  438. struct drm_mm *mm,
  439. u64 size, u64 alignment, unsigned long color,
  440. u64 start, u64 end,
  441. enum drm_mm_insert_mode mode);
  442. /**
  443. * drm_mm_scan_init - initialize lru scanning
  444. * @scan: scan state
  445. * @mm: drm_mm to scan
  446. * @size: size of the allocation
  447. * @alignment: alignment of the allocation
  448. * @color: opaque tag value to use for the allocation
  449. * @mode: fine-tune the allocation search and placement
  450. *
  451. * This is a simplified version of drm_mm_scan_init_with_range() with no range
  452. * restrictions applied.
  453. *
  454. * This simply sets up the scanning routines with the parameters for the desired
  455. * hole.
  456. *
  457. * Warning:
  458. * As long as the scan list is non-empty, no other operations than
  459. * adding/removing nodes to/from the scan list are allowed.
  460. */
  461. static inline void drm_mm_scan_init(struct drm_mm_scan *scan,
  462. struct drm_mm *mm,
  463. u64 size,
  464. u64 alignment,
  465. unsigned long color,
  466. enum drm_mm_insert_mode mode)
  467. {
  468. drm_mm_scan_init_with_range(scan, mm,
  469. size, alignment, color,
  470. 0, U64_MAX, mode);
  471. }
  472. bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
  473. struct drm_mm_node *node);
  474. bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
  475. struct drm_mm_node *node);
  476. struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
  477. void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p);
  478. #endif