1249d6048SJerome Glisse /************************************************************************** 2249d6048SJerome Glisse * 3249d6048SJerome Glisse * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA. 4ba004e39SChris Wilson * Copyright 2016 Intel Corporation 5249d6048SJerome Glisse * All Rights Reserved. 6249d6048SJerome Glisse * 7249d6048SJerome Glisse * Permission is hereby granted, free of charge, to any person obtaining a 8249d6048SJerome Glisse * copy of this software and associated documentation files (the 9249d6048SJerome Glisse * "Software"), to deal in the Software without restriction, including 10249d6048SJerome Glisse * without limitation the rights to use, copy, modify, merge, publish, 11249d6048SJerome Glisse * distribute, sub license, and/or sell copies of the Software, and to 12249d6048SJerome Glisse * permit persons to whom the Software is furnished to do so, subject to 13249d6048SJerome Glisse * the following conditions: 14249d6048SJerome Glisse * 15249d6048SJerome Glisse * The above copyright notice and this permission notice (including the 16249d6048SJerome Glisse * next paragraph) shall be included in all copies or substantial portions 17249d6048SJerome Glisse * of the Software. 18249d6048SJerome Glisse * 19249d6048SJerome Glisse * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20249d6048SJerome Glisse * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21249d6048SJerome Glisse * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22249d6048SJerome Glisse * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23249d6048SJerome Glisse * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24249d6048SJerome Glisse * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25249d6048SJerome Glisse * USE OR OTHER DEALINGS IN THE SOFTWARE. 26249d6048SJerome Glisse * 27249d6048SJerome Glisse * 28249d6048SJerome Glisse **************************************************************************/ 29249d6048SJerome Glisse /* 30249d6048SJerome Glisse * Authors: 31249d6048SJerome Glisse * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com> 32249d6048SJerome Glisse */ 33249d6048SJerome Glisse 34249d6048SJerome Glisse #ifndef _DRM_MM_H_ 35249d6048SJerome Glisse #define _DRM_MM_H_ 36249d6048SJerome Glisse 37249d6048SJerome Glisse /* 38249d6048SJerome Glisse * Generic range manager structs 39249d6048SJerome Glisse */ 4086e81f0eSDavid Herrmann #include <linux/bug.h> 41202b52b7SChris Wilson #include <linux/rbtree.h> 4286e81f0eSDavid Herrmann #include <linux/kernel.h> 43249d6048SJerome Glisse #include <linux/list.h> 4486e81f0eSDavid Herrmann #include <linux/spinlock.h> 45f1938cd6SDave Airlie #ifdef CONFIG_DEBUG_FS 46f1938cd6SDave Airlie #include <linux/seq_file.h> 47f1938cd6SDave Airlie #endif 485705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM 495705670dSChris Wilson #include <linux/stackdepot.h> 505705670dSChris Wilson #endif 51249d6048SJerome Glisse 52b3ee963fSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM 53b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUG_ON(expr) 54b3ee963fSChris Wilson #else 55b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr) 56b3ee963fSChris Wilson #endif 57b3ee963fSChris Wilson 5831e5d7c6SDavid Herrmann enum drm_mm_search_flags { 5931e5d7c6SDavid Herrmann DRM_MM_SEARCH_DEFAULT = 0, 6031e5d7c6SDavid Herrmann DRM_MM_SEARCH_BEST = 1 << 0, 6162347f9eSLauri Kasanen DRM_MM_SEARCH_BELOW = 1 << 1, 6231e5d7c6SDavid Herrmann }; 6331e5d7c6SDavid Herrmann 6462347f9eSLauri Kasanen enum drm_mm_allocator_flags { 6562347f9eSLauri Kasanen DRM_MM_CREATE_DEFAULT = 0, 6662347f9eSLauri Kasanen DRM_MM_CREATE_TOP = 1 << 0, 6762347f9eSLauri Kasanen }; 6862347f9eSLauri Kasanen 6962347f9eSLauri Kasanen #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT 7062347f9eSLauri Kasanen #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP 7162347f9eSLauri Kasanen 72249d6048SJerome Glisse struct drm_mm_node { 73d1024ce9SDaniel Vetter struct list_head node_list; 74ea7b1dd4SDaniel Vetter struct list_head hole_stack; 75202b52b7SChris Wilson struct rb_node rb; 76ea7b1dd4SDaniel Vetter unsigned hole_follows : 1; 77b0b7af18SDaniel Vetter unsigned allocated : 1; 78f29051f1SChris Wilson bool scanned_block : 1; 796b9d89b4SChris Wilson unsigned long color; 80440fd528SThierry Reding u64 start; 81440fd528SThierry Reding u64 size; 82202b52b7SChris Wilson u64 __subtree_last; 83249d6048SJerome Glisse struct drm_mm *mm; 845705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM 855705670dSChris Wilson depot_stack_handle_t stack; 865705670dSChris Wilson #endif 87249d6048SJerome Glisse }; 88249d6048SJerome Glisse 89249d6048SJerome Glisse struct drm_mm { 9025985edcSLucas De Marchi /* List of all memory nodes that immediately precede a free hole. */ 91ea7b1dd4SDaniel Vetter struct list_head hole_stack; 92ea7b1dd4SDaniel Vetter /* head_node.node_list is the list of all memory nodes, ordered 93ea7b1dd4SDaniel Vetter * according to the (increasing) start address of the memory node. */ 94ea7b1dd4SDaniel Vetter struct drm_mm_node head_node; 95202b52b7SChris Wilson /* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */ 96202b52b7SChris Wilson struct rb_root interval_tree; 97202b52b7SChris Wilson 9845b186f1SChris Wilson void (*color_adjust)(const struct drm_mm_node *node, 9945b186f1SChris Wilson unsigned long color, 100440fd528SThierry Reding u64 *start, u64 *end); 1019a71e277SChris Wilson 1029a71e277SChris Wilson unsigned long scan_active; 1039a71e277SChris Wilson }; 1049a71e277SChris Wilson 1059a71e277SChris Wilson struct drm_mm_scan { 1069a71e277SChris Wilson struct drm_mm *mm; 1079a71e277SChris Wilson 1089a71e277SChris Wilson u64 size; 1099a71e277SChris Wilson u64 alignment; 1109a956b15SChris Wilson u64 remainder_mask; 1119a71e277SChris Wilson 1129a71e277SChris Wilson u64 range_start; 1139a71e277SChris Wilson u64 range_end; 1149a71e277SChris Wilson 1159a71e277SChris Wilson u64 hit_start; 1169a71e277SChris Wilson u64 hit_end; 1179a71e277SChris Wilson 1189a71e277SChris Wilson unsigned long color; 1190b04d474SChris Wilson unsigned int flags; 120249d6048SJerome Glisse }; 121249d6048SJerome Glisse 122e18c0412SDaniel Vetter /** 123e18c0412SDaniel Vetter * drm_mm_node_allocated - checks whether a node is allocated 124e18c0412SDaniel Vetter * @node: drm_mm_node to check 125e18c0412SDaniel Vetter * 126ba004e39SChris Wilson * Drivers are required to clear a node prior to using it with the 127ba004e39SChris Wilson * drm_mm range manager. 128ba004e39SChris Wilson * 129ba004e39SChris Wilson * Drivers should use this helper for proper encapsulation of drm_mm 130e18c0412SDaniel Vetter * internals. 131e18c0412SDaniel Vetter * 132e18c0412SDaniel Vetter * Returns: 133e18c0412SDaniel Vetter * True if the @node is allocated. 134e18c0412SDaniel Vetter */ 13545b186f1SChris Wilson static inline bool drm_mm_node_allocated(const struct drm_mm_node *node) 136b0b7af18SDaniel Vetter { 137b0b7af18SDaniel Vetter return node->allocated; 138b0b7af18SDaniel Vetter } 139b0b7af18SDaniel Vetter 140e18c0412SDaniel Vetter /** 141e18c0412SDaniel Vetter * drm_mm_initialized - checks whether an allocator is initialized 142e18c0412SDaniel Vetter * @mm: drm_mm to check 143e18c0412SDaniel Vetter * 144ba004e39SChris Wilson * Drivers should clear the struct drm_mm prior to initialisation if they 145ba004e39SChris Wilson * want to use this function. 146ba004e39SChris Wilson * 147ba004e39SChris Wilson * Drivers should use this helper for proper encapsulation of drm_mm 148e18c0412SDaniel Vetter * internals. 149e18c0412SDaniel Vetter * 150e18c0412SDaniel Vetter * Returns: 151e18c0412SDaniel Vetter * True if the @mm is initialized. 152e18c0412SDaniel Vetter */ 15345b186f1SChris Wilson static inline bool drm_mm_initialized(const struct drm_mm *mm) 15431a5b8ceSDaniel Vetter { 155ea7b1dd4SDaniel Vetter return mm->hole_stack.next; 15631a5b8ceSDaniel Vetter } 1579e8944abSChris Wilson 1583f85fb34SChris Wilson /** 1593f85fb34SChris Wilson * drm_mm_hole_follows - checks whether a hole follows this node 1603f85fb34SChris Wilson * @node: drm_mm_node to check 1613f85fb34SChris Wilson * 1623f85fb34SChris Wilson * Holes are embedded into the drm_mm using the tail of a drm_mm_node. 1633f85fb34SChris Wilson * If you wish to know whether a hole follows this particular node, 1643f85fb34SChris Wilson * query this function. 1653f85fb34SChris Wilson * 1663f85fb34SChris Wilson * Returns: 1673f85fb34SChris Wilson * True if a hole follows the @node. 1683f85fb34SChris Wilson */ 1693f85fb34SChris Wilson static inline bool drm_mm_hole_follows(const struct drm_mm_node *node) 1703f85fb34SChris Wilson { 1713f85fb34SChris Wilson return node->hole_follows; 1723f85fb34SChris Wilson } 1733f85fb34SChris Wilson 17445b186f1SChris Wilson static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node) 1759e8944abSChris Wilson { 1769e8944abSChris Wilson return hole_node->start + hole_node->size; 1779e8944abSChris Wilson } 1789e8944abSChris Wilson 179e18c0412SDaniel Vetter /** 180e18c0412SDaniel Vetter * drm_mm_hole_node_start - computes the start of the hole following @node 181e18c0412SDaniel Vetter * @hole_node: drm_mm_node which implicitly tracks the following hole 182e18c0412SDaniel Vetter * 183ba004e39SChris Wilson * This is useful for driver-specific debug dumpers. Otherwise drivers should 184ba004e39SChris Wilson * not inspect holes themselves. Drivers must check first whether a hole indeed 1853f85fb34SChris Wilson * follows by looking at drm_mm_hole_follows() 186e18c0412SDaniel Vetter * 187e18c0412SDaniel Vetter * Returns: 188e18c0412SDaniel Vetter * Start of the subsequent hole. 189e18c0412SDaniel Vetter */ 19045b186f1SChris Wilson static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node) 1919e8944abSChris Wilson { 1923f85fb34SChris Wilson DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node)); 1939e8944abSChris Wilson return __drm_mm_hole_node_start(hole_node); 1949e8944abSChris Wilson } 1959e8944abSChris Wilson 19645b186f1SChris Wilson static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node) 1979e8944abSChris Wilson { 19887069f44SGeliang Tang return list_next_entry(hole_node, node_list)->start; 1999e8944abSChris Wilson } 2009e8944abSChris Wilson 201e18c0412SDaniel Vetter /** 202e18c0412SDaniel Vetter * drm_mm_hole_node_end - computes the end of the hole following @node 203e18c0412SDaniel Vetter * @hole_node: drm_mm_node which implicitly tracks the following hole 204e18c0412SDaniel Vetter * 205ba004e39SChris Wilson * This is useful for driver-specific debug dumpers. Otherwise drivers should 206ba004e39SChris Wilson * not inspect holes themselves. Drivers must check first whether a hole indeed 2073f85fb34SChris Wilson * follows by looking at drm_mm_hole_follows(). 208e18c0412SDaniel Vetter * 209e18c0412SDaniel Vetter * Returns: 210e18c0412SDaniel Vetter * End of the subsequent hole. 211e18c0412SDaniel Vetter */ 21245b186f1SChris Wilson static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node) 2139e8944abSChris Wilson { 2149e8944abSChris Wilson return __drm_mm_hole_node_end(hole_node); 2159e8944abSChris Wilson } 2169e8944abSChris Wilson 2172bc98c86SChris Wilson /** 2182bc98c86SChris Wilson * drm_mm_nodes - list of nodes under the drm_mm range manager 2192bc98c86SChris Wilson * @mm: the struct drm_mm range manger 2202bc98c86SChris Wilson * 2212bc98c86SChris Wilson * As the drm_mm range manager hides its node_list deep with its 2222bc98c86SChris Wilson * structure, extracting it looks painful and repetitive. This is 2232bc98c86SChris Wilson * not expected to be used outside of the drm_mm_for_each_node() 2242bc98c86SChris Wilson * macros and similar internal functions. 2252bc98c86SChris Wilson * 2262bc98c86SChris Wilson * Returns: 2272bc98c86SChris Wilson * The node list, may be empty. 2282bc98c86SChris Wilson */ 2292bc98c86SChris Wilson #define drm_mm_nodes(mm) (&(mm)->head_node.node_list) 230ad579002SChris Wilson 231e18c0412SDaniel Vetter /** 232e18c0412SDaniel Vetter * drm_mm_for_each_node - iterator to walk over all allocated nodes 233e18c0412SDaniel Vetter * @entry: drm_mm_node structure to assign to in each iteration step 234e18c0412SDaniel Vetter * @mm: drm_mm allocator to walk 235e18c0412SDaniel Vetter * 236e18c0412SDaniel Vetter * This iterator walks over all nodes in the range allocator. It is implemented 237e18c0412SDaniel Vetter * with list_for_each, so not save against removal of elements. 238e18c0412SDaniel Vetter */ 239ad579002SChris Wilson #define drm_mm_for_each_node(entry, mm) \ 2402bc98c86SChris Wilson list_for_each_entry(entry, drm_mm_nodes(mm), node_list) 241ad579002SChris Wilson 242ad579002SChris Wilson /** 243ad579002SChris Wilson * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes 244ad579002SChris Wilson * @entry: drm_mm_node structure to assign to in each iteration step 245ad579002SChris Wilson * @next: drm_mm_node structure to store the next step 246ad579002SChris Wilson * @mm: drm_mm allocator to walk 247ad579002SChris Wilson * 248ad579002SChris Wilson * This iterator walks over all nodes in the range allocator. It is implemented 249ad579002SChris Wilson * with list_for_each_safe, so save against removal of elements. 250ad579002SChris Wilson */ 251ad579002SChris Wilson #define drm_mm_for_each_node_safe(entry, next, mm) \ 2522bc98c86SChris Wilson list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list) 2539e8944abSChris Wilson 25418b40c58SGeliang Tang #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \ 25518b40c58SGeliang Tang for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \ 25618b40c58SGeliang Tang &entry->hole_stack != &(mm)->hole_stack ? \ 25718b40c58SGeliang Tang hole_start = drm_mm_hole_node_start(entry), \ 25818b40c58SGeliang Tang hole_end = drm_mm_hole_node_end(entry), \ 25918b40c58SGeliang Tang 1 : 0; \ 26018b40c58SGeliang Tang entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack)) 26118b40c58SGeliang Tang 262e18c0412SDaniel Vetter /** 263e18c0412SDaniel Vetter * drm_mm_for_each_hole - iterator to walk over all holes 264e18c0412SDaniel Vetter * @entry: drm_mm_node used internally to track progress 265e18c0412SDaniel Vetter * @mm: drm_mm allocator to walk 266e18c0412SDaniel Vetter * @hole_start: ulong variable to assign the hole start to on each iteration 267e18c0412SDaniel Vetter * @hole_end: ulong variable to assign the hole end to on each iteration 268e18c0412SDaniel Vetter * 269e18c0412SDaniel Vetter * This iterator walks over all holes in the range allocator. It is implemented 270e18c0412SDaniel Vetter * with list_for_each, so not save against removal of elements. @entry is used 271e18c0412SDaniel Vetter * internally and will not reflect a real drm_mm_node for the very first hole. 272e18c0412SDaniel Vetter * Hence users of this iterator may not access it. 273e18c0412SDaniel Vetter * 274e18c0412SDaniel Vetter * Implementation Note: 275e18c0412SDaniel Vetter * We need to inline list_for_each_entry in order to be able to set hole_start 276e18c0412SDaniel Vetter * and hole_end on each iteration while keeping the macro sane. 27762347f9eSLauri Kasanen * 27862347f9eSLauri Kasanen * The __drm_mm_for_each_hole version is similar, but with added support for 27962347f9eSLauri Kasanen * going backwards. 2809e8944abSChris Wilson */ 2819e8944abSChris Wilson #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \ 28218b40c58SGeliang Tang __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0) 28362347f9eSLauri Kasanen 284249d6048SJerome Glisse /* 285249d6048SJerome Glisse * Basic range manager support (drm_mm.c) 286249d6048SJerome Glisse */ 287e18c0412SDaniel Vetter int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node); 288e18c0412SDaniel Vetter int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, 289b8103450SChris Wilson struct drm_mm_node *node, 290440fd528SThierry Reding u64 size, 29171733207SChris Wilson u64 alignment, 292b8103450SChris Wilson unsigned long color, 293440fd528SThierry Reding u64 start, 294440fd528SThierry Reding u64 end, 29562347f9eSLauri Kasanen enum drm_mm_search_flags sflags, 29662347f9eSLauri Kasanen enum drm_mm_allocator_flags aflags); 297adb040b8SChris Wilson 298e18c0412SDaniel Vetter /** 299e18c0412SDaniel Vetter * drm_mm_insert_node_in_range - ranged search for space and insert @node 300e18c0412SDaniel Vetter * @mm: drm_mm to allocate from 301e18c0412SDaniel Vetter * @node: preallocate node to insert 302e18c0412SDaniel Vetter * @size: size of the allocation 303e18c0412SDaniel Vetter * @alignment: alignment of the allocation 304e18c0412SDaniel Vetter * @start: start of the allowed range for this node 305e18c0412SDaniel Vetter * @end: end of the allowed range for this node 306e18c0412SDaniel Vetter * @flags: flags to fine-tune the allocation 307e18c0412SDaniel Vetter * 308e18c0412SDaniel Vetter * This is a simplified version of drm_mm_insert_node_in_range_generic() with 309e18c0412SDaniel Vetter * @color set to 0. 310e18c0412SDaniel Vetter * 311e18c0412SDaniel Vetter * The preallocated node must be cleared to 0. 312e18c0412SDaniel Vetter * 313e18c0412SDaniel Vetter * Returns: 314e18c0412SDaniel Vetter * 0 on success, -ENOSPC if there's no suitable hole. 315e18c0412SDaniel Vetter */ 31631e5d7c6SDavid Herrmann static inline int drm_mm_insert_node_in_range(struct drm_mm *mm, 31731e5d7c6SDavid Herrmann struct drm_mm_node *node, 318440fd528SThierry Reding u64 size, 31971733207SChris Wilson u64 alignment, 320440fd528SThierry Reding u64 start, 321440fd528SThierry Reding u64 end, 32231e5d7c6SDavid Herrmann enum drm_mm_search_flags flags) 32331e5d7c6SDavid Herrmann { 32431e5d7c6SDavid Herrmann return drm_mm_insert_node_in_range_generic(mm, node, size, alignment, 32562347f9eSLauri Kasanen 0, start, end, flags, 32662347f9eSLauri Kasanen DRM_MM_CREATE_DEFAULT); 32731e5d7c6SDavid Herrmann } 32831e5d7c6SDavid Herrmann 329adb040b8SChris Wilson /** 330adb040b8SChris Wilson * drm_mm_insert_node_generic - search for space and insert @node 331adb040b8SChris Wilson * @mm: drm_mm to allocate from 332adb040b8SChris Wilson * @node: preallocate node to insert 333adb040b8SChris Wilson * @size: size of the allocation 334adb040b8SChris Wilson * @alignment: alignment of the allocation 335adb040b8SChris Wilson * @color: opaque tag value to use for this node 336adb040b8SChris Wilson * @sflags: flags to fine-tune the allocation search 337adb040b8SChris Wilson * @aflags: flags to fine-tune the allocation behavior 338adb040b8SChris Wilson * 339adb040b8SChris Wilson * The preallocated node must be cleared to 0. 340adb040b8SChris Wilson * 341adb040b8SChris Wilson * Returns: 342adb040b8SChris Wilson * 0 on success, -ENOSPC if there's no suitable hole. 343adb040b8SChris Wilson */ 344adb040b8SChris Wilson static inline int 345adb040b8SChris Wilson drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node, 346adb040b8SChris Wilson u64 size, u64 alignment, 347adb040b8SChris Wilson unsigned long color, 348adb040b8SChris Wilson enum drm_mm_search_flags sflags, 349adb040b8SChris Wilson enum drm_mm_allocator_flags aflags) 350adb040b8SChris Wilson { 351adb040b8SChris Wilson return drm_mm_insert_node_in_range_generic(mm, node, 352adb040b8SChris Wilson size, alignment, 0, 353adb040b8SChris Wilson 0, U64_MAX, 354adb040b8SChris Wilson sflags, aflags); 355adb040b8SChris Wilson } 356adb040b8SChris Wilson 357adb040b8SChris Wilson /** 358adb040b8SChris Wilson * drm_mm_insert_node - search for space and insert @node 359adb040b8SChris Wilson * @mm: drm_mm to allocate from 360adb040b8SChris Wilson * @node: preallocate node to insert 361adb040b8SChris Wilson * @size: size of the allocation 362adb040b8SChris Wilson * @alignment: alignment of the allocation 363adb040b8SChris Wilson * @flags: flags to fine-tune the allocation 364adb040b8SChris Wilson * 365adb040b8SChris Wilson * This is a simplified version of drm_mm_insert_node_generic() with @color set 366adb040b8SChris Wilson * to 0. 367adb040b8SChris Wilson * 368adb040b8SChris Wilson * The preallocated node must be cleared to 0. 369adb040b8SChris Wilson * 370adb040b8SChris Wilson * Returns: 371adb040b8SChris Wilson * 0 on success, -ENOSPC if there's no suitable hole. 372adb040b8SChris Wilson */ 373adb040b8SChris Wilson static inline int drm_mm_insert_node(struct drm_mm *mm, 374adb040b8SChris Wilson struct drm_mm_node *node, 375adb040b8SChris Wilson u64 size, 376adb040b8SChris Wilson u64 alignment, 377adb040b8SChris Wilson enum drm_mm_search_flags flags) 378adb040b8SChris Wilson { 379adb040b8SChris Wilson return drm_mm_insert_node_generic(mm, node, 380adb040b8SChris Wilson size, alignment, 0, 381adb040b8SChris Wilson flags, DRM_MM_CREATE_DEFAULT); 382adb040b8SChris Wilson } 383adb040b8SChris Wilson 384e18c0412SDaniel Vetter void drm_mm_remove_node(struct drm_mm_node *node); 385e18c0412SDaniel Vetter void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new); 38645b186f1SChris Wilson void drm_mm_init(struct drm_mm *mm, u64 start, u64 size); 387e18c0412SDaniel Vetter void drm_mm_takedown(struct drm_mm *mm); 388ac9bb7b7SChris Wilson 389ac9bb7b7SChris Wilson /** 390ac9bb7b7SChris Wilson * drm_mm_clean - checks whether an allocator is clean 391ac9bb7b7SChris Wilson * @mm: drm_mm allocator to check 392ac9bb7b7SChris Wilson * 393ac9bb7b7SChris Wilson * Returns: 394ac9bb7b7SChris Wilson * True if the allocator is completely free, false if there's still a node 395ac9bb7b7SChris Wilson * allocated in it. 396ac9bb7b7SChris Wilson */ 397ac9bb7b7SChris Wilson static inline bool drm_mm_clean(const struct drm_mm *mm) 398ac9bb7b7SChris Wilson { 399ac9bb7b7SChris Wilson return list_empty(drm_mm_nodes(mm)); 400ac9bb7b7SChris Wilson } 401249d6048SJerome Glisse 402202b52b7SChris Wilson struct drm_mm_node * 40345b186f1SChris Wilson __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last); 404202b52b7SChris Wilson 405522e85ddSChris Wilson /** 406522e85ddSChris Wilson * drm_mm_for_each_node_in_range - iterator to walk over a range of 407522e85ddSChris Wilson * allocated nodes 4088b2fb7b6SChris Wilson * @node__: drm_mm_node structure to assign to in each iteration step 4098b2fb7b6SChris Wilson * @mm__: drm_mm allocator to walk 4108b2fb7b6SChris Wilson * @start__: starting offset, the first node will overlap this 4118b2fb7b6SChris Wilson * @end__: ending offset, the last node will start before this (but may overlap) 412522e85ddSChris Wilson * 413522e85ddSChris Wilson * This iterator walks over all nodes in the range allocator that lie 414522e85ddSChris Wilson * between @start and @end. It is implemented similarly to list_for_each(), 415522e85ddSChris Wilson * but using the internal interval tree to accelerate the search for the 416522e85ddSChris Wilson * starting node, and so not safe against removal of elements. It assumes 417522e85ddSChris Wilson * that @end is within (or is the upper limit of) the drm_mm allocator. 418522e85ddSChris Wilson */ 4198b2fb7b6SChris Wilson #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__) \ 4208b2fb7b6SChris Wilson for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \ 4218b2fb7b6SChris Wilson node__ && node__->start < (end__); \ 4228b2fb7b6SChris Wilson node__ = list_next_entry(node__, node_list)) 423202b52b7SChris Wilson 4249a71e277SChris Wilson void drm_mm_scan_init_with_range(struct drm_mm_scan *scan, 4259a71e277SChris Wilson struct drm_mm *mm, 4260b04d474SChris Wilson u64 size, u64 alignment, unsigned long color, 4270b04d474SChris Wilson u64 start, u64 end, 4280b04d474SChris Wilson unsigned int flags); 4292c4b3895SChris Wilson 4302c4b3895SChris Wilson /** 4312c4b3895SChris Wilson * drm_mm_scan_init - initialize lru scanning 4322c4b3895SChris Wilson * @scan: scan state 4332c4b3895SChris Wilson * @mm: drm_mm to scan 4342c4b3895SChris Wilson * @size: size of the allocation 4352c4b3895SChris Wilson * @alignment: alignment of the allocation 4362c4b3895SChris Wilson * @color: opaque tag value to use for the allocation 4370b04d474SChris Wilson * @flags: flags to specify how the allocation will be performed afterwards 4382c4b3895SChris Wilson * 4392c4b3895SChris Wilson * This simply sets up the scanning routines with the parameters for the desired 4400b04d474SChris Wilson * hole. 4412c4b3895SChris Wilson * 4422c4b3895SChris Wilson * Warning: 4432c4b3895SChris Wilson * As long as the scan list is non-empty, no other operations than 4442c4b3895SChris Wilson * adding/removing nodes to/from the scan list are allowed. 4452c4b3895SChris Wilson */ 4462c4b3895SChris Wilson static inline void drm_mm_scan_init(struct drm_mm_scan *scan, 4472c4b3895SChris Wilson struct drm_mm *mm, 4482c4b3895SChris Wilson u64 size, 4492c4b3895SChris Wilson u64 alignment, 4500b04d474SChris Wilson unsigned long color, 4510b04d474SChris Wilson unsigned int flags) 4522c4b3895SChris Wilson { 4530b04d474SChris Wilson drm_mm_scan_init_with_range(scan, mm, 4540b04d474SChris Wilson size, alignment, color, 4550b04d474SChris Wilson 0, U64_MAX, 4560b04d474SChris Wilson flags); 4572c4b3895SChris Wilson } 4582c4b3895SChris Wilson 4599a71e277SChris Wilson bool drm_mm_scan_add_block(struct drm_mm_scan *scan, 4609a71e277SChris Wilson struct drm_mm_node *node); 4619a71e277SChris Wilson bool drm_mm_scan_remove_block(struct drm_mm_scan *scan, 4629a71e277SChris Wilson struct drm_mm_node *node); 4633fa489daSChris Wilson struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan); 464709ea971SDaniel Vetter 46545b186f1SChris Wilson void drm_mm_debug_table(const struct drm_mm *mm, const char *prefix); 466fa8a1238SDave Airlie #ifdef CONFIG_DEBUG_FS 46745b186f1SChris Wilson int drm_mm_dump_table(struct seq_file *m, const struct drm_mm *mm); 468fa8a1238SDave Airlie #endif 469fa8a1238SDave Airlie 470249d6048SJerome Glisse #endif 471