drm_mm.h 16 KB

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