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> 455705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM 465705670dSChris Wilson #include <linux/stackdepot.h> 475705670dSChris Wilson #endif 48b5c3714fSDaniel Vetter #include <drm/drm_print.h> 49249d6048SJerome Glisse 50b3ee963fSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM 51b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUG_ON(expr) 52b3ee963fSChris Wilson #else 53b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr) 54b3ee963fSChris Wilson #endif 55b3ee963fSChris Wilson 5631e5d7c6SDavid Herrmann enum drm_mm_search_flags { 5731e5d7c6SDavid Herrmann DRM_MM_SEARCH_DEFAULT = 0, 5831e5d7c6SDavid Herrmann DRM_MM_SEARCH_BEST = 1 << 0, 5962347f9eSLauri Kasanen DRM_MM_SEARCH_BELOW = 1 << 1, 6031e5d7c6SDavid Herrmann }; 6131e5d7c6SDavid Herrmann 6262347f9eSLauri Kasanen enum drm_mm_allocator_flags { 6362347f9eSLauri Kasanen DRM_MM_CREATE_DEFAULT = 0, 6462347f9eSLauri Kasanen DRM_MM_CREATE_TOP = 1 << 0, 6562347f9eSLauri Kasanen }; 6662347f9eSLauri Kasanen 6762347f9eSLauri Kasanen #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT 6862347f9eSLauri Kasanen #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP 6962347f9eSLauri Kasanen 7005fc0321SDaniel Vetter /** 7105fc0321SDaniel Vetter * struct drm_mm_node - allocated block in the DRM allocator 7205fc0321SDaniel Vetter * 7305fc0321SDaniel Vetter * This represents an allocated block in a &drm_mm allocator. Except for 7405fc0321SDaniel Vetter * pre-reserved nodes inserted using drm_mm_reserve_node() the structure is 7505fc0321SDaniel Vetter * entirely opaque and should only be accessed through the provided funcions. 7605fc0321SDaniel Vetter * Since allocation of these nodes is entirely handled by the driver they can be 7705fc0321SDaniel Vetter * embedded. 7805fc0321SDaniel Vetter */ 79249d6048SJerome Glisse struct drm_mm_node { 8005fc0321SDaniel Vetter /** @color: Opaque driver-private tag. */ 8105fc0321SDaniel Vetter unsigned long color; 8205fc0321SDaniel Vetter /** @start: Start address of the allocated block. */ 8305fc0321SDaniel Vetter u64 start; 8405fc0321SDaniel Vetter /** @size: Size of the allocated block. */ 8505fc0321SDaniel Vetter u64 size; 8605fc0321SDaniel Vetter /* private: */ 87d1024ce9SDaniel Vetter struct list_head node_list; 88ea7b1dd4SDaniel Vetter struct list_head hole_stack; 89202b52b7SChris Wilson struct rb_node rb; 90ea7b1dd4SDaniel Vetter unsigned hole_follows : 1; 91b0b7af18SDaniel Vetter unsigned allocated : 1; 92f29051f1SChris Wilson bool scanned_block : 1; 93202b52b7SChris Wilson u64 __subtree_last; 94249d6048SJerome Glisse struct drm_mm *mm; 955705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM 965705670dSChris Wilson depot_stack_handle_t stack; 975705670dSChris Wilson #endif 98249d6048SJerome Glisse }; 99249d6048SJerome Glisse 10005fc0321SDaniel Vetter /** 10105fc0321SDaniel Vetter * struct drm_mm - DRM allocator 10205fc0321SDaniel Vetter * 10305fc0321SDaniel Vetter * DRM range allocator with a few special functions and features geared towards 10405fc0321SDaniel Vetter * managing GPU memory. Except for the @color_adjust callback the structure is 10505fc0321SDaniel Vetter * entirely opaque and should only be accessed through the provided functions 10605fc0321SDaniel Vetter * and macros. This structure can be embedded into larger driver structures. 10705fc0321SDaniel Vetter */ 108249d6048SJerome Glisse struct drm_mm { 10905fc0321SDaniel Vetter /** 11005fc0321SDaniel Vetter * @color_adjust: 11105fc0321SDaniel Vetter * 11205fc0321SDaniel Vetter * Optional driver callback to further apply restrictions on a hole. The 11305fc0321SDaniel Vetter * node argument points at the node containing the hole from which the 11405fc0321SDaniel Vetter * block would be allocated (see drm_mm_hole_follows() and friends). The 11505fc0321SDaniel Vetter * other arguments are the size of the block to be allocated. The driver 11605fc0321SDaniel Vetter * can adjust the start and end as needed to e.g. insert guard pages. 11705fc0321SDaniel Vetter */ 11805fc0321SDaniel Vetter void (*color_adjust)(const struct drm_mm_node *node, 11905fc0321SDaniel Vetter unsigned long color, 12005fc0321SDaniel Vetter u64 *start, u64 *end); 12105fc0321SDaniel Vetter 12205fc0321SDaniel Vetter /* private: */ 12325985edcSLucas De Marchi /* List of all memory nodes that immediately precede a free hole. */ 124ea7b1dd4SDaniel Vetter struct list_head hole_stack; 125ea7b1dd4SDaniel Vetter /* head_node.node_list is the list of all memory nodes, ordered 126ea7b1dd4SDaniel Vetter * according to the (increasing) start address of the memory node. */ 127ea7b1dd4SDaniel Vetter struct drm_mm_node head_node; 128202b52b7SChris Wilson /* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */ 129202b52b7SChris Wilson struct rb_root interval_tree; 130202b52b7SChris Wilson 1319a71e277SChris Wilson unsigned long scan_active; 1329a71e277SChris Wilson }; 1339a71e277SChris Wilson 13405fc0321SDaniel Vetter /** 13505fc0321SDaniel Vetter * struct drm_mm_scan - DRM allocator eviction roaster data 13605fc0321SDaniel Vetter * 13705fc0321SDaniel Vetter * This structure tracks data needed for the eviction roaster set up using 13805fc0321SDaniel Vetter * drm_mm_scan_init(), and used with drm_mm_scan_add_block() and 13905fc0321SDaniel Vetter * drm_mm_scan_remove_block(). The structure is entirely opaque and should only 14005fc0321SDaniel Vetter * be accessed through the provided functions and macros. It is meant to be 14105fc0321SDaniel Vetter * allocated temporarily by the driver on the stack. 14205fc0321SDaniel Vetter */ 1439a71e277SChris Wilson struct drm_mm_scan { 14405fc0321SDaniel Vetter /* private: */ 1459a71e277SChris Wilson struct drm_mm *mm; 1469a71e277SChris Wilson 1479a71e277SChris Wilson u64 size; 1489a71e277SChris Wilson u64 alignment; 1499a956b15SChris Wilson u64 remainder_mask; 1509a71e277SChris Wilson 1519a71e277SChris Wilson u64 range_start; 1529a71e277SChris Wilson u64 range_end; 1539a71e277SChris Wilson 1549a71e277SChris Wilson u64 hit_start; 1559a71e277SChris Wilson u64 hit_end; 1569a71e277SChris Wilson 1579a71e277SChris Wilson unsigned long color; 1580b04d474SChris Wilson unsigned int flags; 159249d6048SJerome Glisse }; 160249d6048SJerome Glisse 161e18c0412SDaniel Vetter /** 162e18c0412SDaniel Vetter * drm_mm_node_allocated - checks whether a node is allocated 163e18c0412SDaniel Vetter * @node: drm_mm_node to check 164e18c0412SDaniel Vetter * 165ba004e39SChris Wilson * Drivers are required to clear a node prior to using it with the 166ba004e39SChris Wilson * drm_mm range manager. 167ba004e39SChris Wilson * 168ba004e39SChris Wilson * Drivers should use this helper for proper encapsulation of drm_mm 169e18c0412SDaniel Vetter * internals. 170e18c0412SDaniel Vetter * 171e18c0412SDaniel Vetter * Returns: 172e18c0412SDaniel Vetter * True if the @node is allocated. 173e18c0412SDaniel Vetter */ 17445b186f1SChris Wilson static inline bool drm_mm_node_allocated(const struct drm_mm_node *node) 175b0b7af18SDaniel Vetter { 176b0b7af18SDaniel Vetter return node->allocated; 177b0b7af18SDaniel Vetter } 178b0b7af18SDaniel Vetter 179e18c0412SDaniel Vetter /** 180e18c0412SDaniel Vetter * drm_mm_initialized - checks whether an allocator is initialized 181e18c0412SDaniel Vetter * @mm: drm_mm to check 182e18c0412SDaniel Vetter * 183ba004e39SChris Wilson * Drivers should clear the struct drm_mm prior to initialisation if they 184ba004e39SChris Wilson * want to use this function. 185ba004e39SChris Wilson * 186ba004e39SChris Wilson * Drivers should use this helper for proper encapsulation of drm_mm 187e18c0412SDaniel Vetter * internals. 188e18c0412SDaniel Vetter * 189e18c0412SDaniel Vetter * Returns: 190e18c0412SDaniel Vetter * True if the @mm is initialized. 191e18c0412SDaniel Vetter */ 19245b186f1SChris Wilson static inline bool drm_mm_initialized(const struct drm_mm *mm) 19331a5b8ceSDaniel Vetter { 194ea7b1dd4SDaniel Vetter return mm->hole_stack.next; 19531a5b8ceSDaniel Vetter } 1969e8944abSChris Wilson 1973f85fb34SChris Wilson /** 1983f85fb34SChris Wilson * drm_mm_hole_follows - checks whether a hole follows this node 1993f85fb34SChris Wilson * @node: drm_mm_node to check 2003f85fb34SChris Wilson * 2013f85fb34SChris Wilson * Holes are embedded into the drm_mm using the tail of a drm_mm_node. 2023f85fb34SChris Wilson * If you wish to know whether a hole follows this particular node, 20305fc0321SDaniel Vetter * query this function. See also drm_mm_hole_node_start() and 20405fc0321SDaniel Vetter * drm_mm_hole_node_end(). 2053f85fb34SChris Wilson * 2063f85fb34SChris Wilson * Returns: 2073f85fb34SChris Wilson * True if a hole follows the @node. 2083f85fb34SChris Wilson */ 2093f85fb34SChris Wilson static inline bool drm_mm_hole_follows(const struct drm_mm_node *node) 2103f85fb34SChris Wilson { 2113f85fb34SChris Wilson return node->hole_follows; 2123f85fb34SChris Wilson } 2133f85fb34SChris Wilson 21445b186f1SChris Wilson static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node) 2159e8944abSChris Wilson { 2169e8944abSChris Wilson return hole_node->start + hole_node->size; 2179e8944abSChris Wilson } 2189e8944abSChris Wilson 219e18c0412SDaniel Vetter /** 220e18c0412SDaniel Vetter * drm_mm_hole_node_start - computes the start of the hole following @node 221e18c0412SDaniel Vetter * @hole_node: drm_mm_node which implicitly tracks the following hole 222e18c0412SDaniel Vetter * 223ba004e39SChris Wilson * This is useful for driver-specific debug dumpers. Otherwise drivers should 224ba004e39SChris Wilson * not inspect holes themselves. Drivers must check first whether a hole indeed 2253f85fb34SChris Wilson * follows by looking at drm_mm_hole_follows() 226e18c0412SDaniel Vetter * 227e18c0412SDaniel Vetter * Returns: 228e18c0412SDaniel Vetter * Start of the subsequent hole. 229e18c0412SDaniel Vetter */ 23045b186f1SChris Wilson static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node) 2319e8944abSChris Wilson { 2323f85fb34SChris Wilson DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node)); 2339e8944abSChris Wilson return __drm_mm_hole_node_start(hole_node); 2349e8944abSChris Wilson } 2359e8944abSChris Wilson 23645b186f1SChris Wilson static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node) 2379e8944abSChris Wilson { 23887069f44SGeliang Tang return list_next_entry(hole_node, node_list)->start; 2399e8944abSChris Wilson } 2409e8944abSChris Wilson 241e18c0412SDaniel Vetter /** 242e18c0412SDaniel Vetter * drm_mm_hole_node_end - computes the end of the hole following @node 243e18c0412SDaniel Vetter * @hole_node: drm_mm_node which implicitly tracks the following hole 244e18c0412SDaniel Vetter * 245ba004e39SChris Wilson * This is useful for driver-specific debug dumpers. Otherwise drivers should 246ba004e39SChris Wilson * not inspect holes themselves. Drivers must check first whether a hole indeed 2473f85fb34SChris Wilson * follows by looking at drm_mm_hole_follows(). 248e18c0412SDaniel Vetter * 249e18c0412SDaniel Vetter * Returns: 250e18c0412SDaniel Vetter * End of the subsequent hole. 251e18c0412SDaniel Vetter */ 25245b186f1SChris Wilson static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node) 2539e8944abSChris Wilson { 2549e8944abSChris Wilson return __drm_mm_hole_node_end(hole_node); 2559e8944abSChris Wilson } 2569e8944abSChris Wilson 2572bc98c86SChris Wilson /** 2582bc98c86SChris Wilson * drm_mm_nodes - list of nodes under the drm_mm range manager 2592bc98c86SChris Wilson * @mm: the struct drm_mm range manger 2602bc98c86SChris Wilson * 2612bc98c86SChris Wilson * As the drm_mm range manager hides its node_list deep with its 2622bc98c86SChris Wilson * structure, extracting it looks painful and repetitive. This is 2632bc98c86SChris Wilson * not expected to be used outside of the drm_mm_for_each_node() 2642bc98c86SChris Wilson * macros and similar internal functions. 2652bc98c86SChris Wilson * 2662bc98c86SChris Wilson * Returns: 2672bc98c86SChris Wilson * The node list, may be empty. 2682bc98c86SChris Wilson */ 2692bc98c86SChris Wilson #define drm_mm_nodes(mm) (&(mm)->head_node.node_list) 270ad579002SChris Wilson 271e18c0412SDaniel Vetter /** 272e18c0412SDaniel Vetter * drm_mm_for_each_node - iterator to walk over all allocated nodes 27305fc0321SDaniel Vetter * @entry: &struct drm_mm_node to assign to in each iteration step 27405fc0321SDaniel Vetter * @mm: &drm_mm allocator to walk 275e18c0412SDaniel Vetter * 276e18c0412SDaniel Vetter * This iterator walks over all nodes in the range allocator. It is implemented 27705fc0321SDaniel Vetter * with list_for_each(), so not save against removal of elements. 278e18c0412SDaniel Vetter */ 279ad579002SChris Wilson #define drm_mm_for_each_node(entry, mm) \ 2802bc98c86SChris Wilson list_for_each_entry(entry, drm_mm_nodes(mm), node_list) 281ad579002SChris Wilson 282ad579002SChris Wilson /** 283ad579002SChris Wilson * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes 28405fc0321SDaniel Vetter * @entry: &struct drm_mm_node to assign to in each iteration step 28505fc0321SDaniel Vetter * @next: &struct drm_mm_node to store the next step 28605fc0321SDaniel Vetter * @mm: &drm_mm allocator to walk 287ad579002SChris Wilson * 288ad579002SChris Wilson * This iterator walks over all nodes in the range allocator. It is implemented 28905fc0321SDaniel Vetter * with list_for_each_safe(), so save against removal of elements. 290ad579002SChris Wilson */ 291ad579002SChris Wilson #define drm_mm_for_each_node_safe(entry, next, mm) \ 2922bc98c86SChris Wilson list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list) 2939e8944abSChris Wilson 29418b40c58SGeliang Tang #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \ 29518b40c58SGeliang Tang for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \ 29618b40c58SGeliang Tang &entry->hole_stack != &(mm)->hole_stack ? \ 29718b40c58SGeliang Tang hole_start = drm_mm_hole_node_start(entry), \ 29818b40c58SGeliang Tang hole_end = drm_mm_hole_node_end(entry), \ 29918b40c58SGeliang Tang 1 : 0; \ 30018b40c58SGeliang Tang entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack)) 30118b40c58SGeliang Tang 302e18c0412SDaniel Vetter /** 303e18c0412SDaniel Vetter * drm_mm_for_each_hole - iterator to walk over all holes 30405fc0321SDaniel Vetter * @entry: &drm_mm_node used internally to track progress 30505fc0321SDaniel Vetter * @mm: &drm_mm allocator to walk 306e18c0412SDaniel Vetter * @hole_start: ulong variable to assign the hole start to on each iteration 307e18c0412SDaniel Vetter * @hole_end: ulong variable to assign the hole end to on each iteration 308e18c0412SDaniel Vetter * 309e18c0412SDaniel Vetter * This iterator walks over all holes in the range allocator. It is implemented 31005fc0321SDaniel Vetter * with list_for_each(), so not save against removal of elements. @entry is used 311e18c0412SDaniel Vetter * internally and will not reflect a real drm_mm_node for the very first hole. 312e18c0412SDaniel Vetter * Hence users of this iterator may not access it. 313e18c0412SDaniel Vetter * 314e18c0412SDaniel Vetter * Implementation Note: 315e18c0412SDaniel Vetter * We need to inline list_for_each_entry in order to be able to set hole_start 316e18c0412SDaniel Vetter * and hole_end on each iteration while keeping the macro sane. 31762347f9eSLauri Kasanen * 31862347f9eSLauri Kasanen * The __drm_mm_for_each_hole version is similar, but with added support for 31962347f9eSLauri Kasanen * going backwards. 3209e8944abSChris Wilson */ 3219e8944abSChris Wilson #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \ 32218b40c58SGeliang Tang __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0) 32362347f9eSLauri Kasanen 324249d6048SJerome Glisse /* 325249d6048SJerome Glisse * Basic range manager support (drm_mm.c) 326249d6048SJerome Glisse */ 327e18c0412SDaniel Vetter int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node); 328e18c0412SDaniel Vetter int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, 329b8103450SChris Wilson struct drm_mm_node *node, 330440fd528SThierry Reding u64 size, 33171733207SChris Wilson u64 alignment, 332b8103450SChris Wilson unsigned long color, 333440fd528SThierry Reding u64 start, 334440fd528SThierry Reding u64 end, 33562347f9eSLauri Kasanen enum drm_mm_search_flags sflags, 33662347f9eSLauri Kasanen enum drm_mm_allocator_flags aflags); 337adb040b8SChris Wilson 338e18c0412SDaniel Vetter /** 339e18c0412SDaniel Vetter * drm_mm_insert_node_in_range - ranged search for space and insert @node 340e18c0412SDaniel Vetter * @mm: drm_mm to allocate from 341e18c0412SDaniel Vetter * @node: preallocate node to insert 342e18c0412SDaniel Vetter * @size: size of the allocation 343e18c0412SDaniel Vetter * @alignment: alignment of the allocation 344e18c0412SDaniel Vetter * @start: start of the allowed range for this node 345e18c0412SDaniel Vetter * @end: end of the allowed range for this node 346e18c0412SDaniel Vetter * @flags: flags to fine-tune the allocation 347e18c0412SDaniel Vetter * 348e18c0412SDaniel Vetter * This is a simplified version of drm_mm_insert_node_in_range_generic() with 349e18c0412SDaniel Vetter * @color set to 0. 350e18c0412SDaniel Vetter * 351e18c0412SDaniel Vetter * The preallocated node must be cleared to 0. 352e18c0412SDaniel Vetter * 353e18c0412SDaniel Vetter * Returns: 354e18c0412SDaniel Vetter * 0 on success, -ENOSPC if there's no suitable hole. 355e18c0412SDaniel Vetter */ 35631e5d7c6SDavid Herrmann static inline int drm_mm_insert_node_in_range(struct drm_mm *mm, 35731e5d7c6SDavid Herrmann struct drm_mm_node *node, 358440fd528SThierry Reding u64 size, 35971733207SChris Wilson u64 alignment, 360440fd528SThierry Reding u64 start, 361440fd528SThierry Reding u64 end, 36231e5d7c6SDavid Herrmann enum drm_mm_search_flags flags) 36331e5d7c6SDavid Herrmann { 36431e5d7c6SDavid Herrmann return drm_mm_insert_node_in_range_generic(mm, node, size, alignment, 36562347f9eSLauri Kasanen 0, start, end, flags, 36662347f9eSLauri Kasanen DRM_MM_CREATE_DEFAULT); 36731e5d7c6SDavid Herrmann } 36831e5d7c6SDavid Herrmann 369adb040b8SChris Wilson /** 370adb040b8SChris Wilson * drm_mm_insert_node_generic - search for space and insert @node 371adb040b8SChris Wilson * @mm: drm_mm to allocate from 372adb040b8SChris Wilson * @node: preallocate node to insert 373adb040b8SChris Wilson * @size: size of the allocation 374adb040b8SChris Wilson * @alignment: alignment of the allocation 375adb040b8SChris Wilson * @color: opaque tag value to use for this node 376adb040b8SChris Wilson * @sflags: flags to fine-tune the allocation search 377adb040b8SChris Wilson * @aflags: flags to fine-tune the allocation behavior 378adb040b8SChris Wilson * 37905fc0321SDaniel Vetter * This is a simplified version of drm_mm_insert_node_in_range_generic() with no 38005fc0321SDaniel Vetter * range restrictions applied. 38105fc0321SDaniel Vetter * 382adb040b8SChris Wilson * The preallocated node must be cleared to 0. 383adb040b8SChris Wilson * 384adb040b8SChris Wilson * Returns: 385adb040b8SChris Wilson * 0 on success, -ENOSPC if there's no suitable hole. 386adb040b8SChris Wilson */ 387adb040b8SChris Wilson static inline int 388adb040b8SChris Wilson drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node, 389adb040b8SChris Wilson u64 size, u64 alignment, 390adb040b8SChris Wilson unsigned long color, 391adb040b8SChris Wilson enum drm_mm_search_flags sflags, 392adb040b8SChris Wilson enum drm_mm_allocator_flags aflags) 393adb040b8SChris Wilson { 394adb040b8SChris Wilson return drm_mm_insert_node_in_range_generic(mm, node, 395adb040b8SChris Wilson size, alignment, 0, 396adb040b8SChris Wilson 0, U64_MAX, 397adb040b8SChris Wilson sflags, aflags); 398adb040b8SChris Wilson } 399adb040b8SChris Wilson 400adb040b8SChris Wilson /** 401adb040b8SChris Wilson * drm_mm_insert_node - search for space and insert @node 402adb040b8SChris Wilson * @mm: drm_mm to allocate from 403adb040b8SChris Wilson * @node: preallocate node to insert 404adb040b8SChris Wilson * @size: size of the allocation 405adb040b8SChris Wilson * @alignment: alignment of the allocation 406adb040b8SChris Wilson * @flags: flags to fine-tune the allocation 407adb040b8SChris Wilson * 408adb040b8SChris Wilson * This is a simplified version of drm_mm_insert_node_generic() with @color set 409adb040b8SChris Wilson * to 0. 410adb040b8SChris Wilson * 411adb040b8SChris Wilson * The preallocated node must be cleared to 0. 412adb040b8SChris Wilson * 413adb040b8SChris Wilson * Returns: 414adb040b8SChris Wilson * 0 on success, -ENOSPC if there's no suitable hole. 415adb040b8SChris Wilson */ 416adb040b8SChris Wilson static inline int drm_mm_insert_node(struct drm_mm *mm, 417adb040b8SChris Wilson struct drm_mm_node *node, 418adb040b8SChris Wilson u64 size, 419adb040b8SChris Wilson u64 alignment, 420adb040b8SChris Wilson enum drm_mm_search_flags flags) 421adb040b8SChris Wilson { 422adb040b8SChris Wilson return drm_mm_insert_node_generic(mm, node, 423adb040b8SChris Wilson size, alignment, 0, 424adb040b8SChris Wilson flags, DRM_MM_CREATE_DEFAULT); 425adb040b8SChris Wilson } 426adb040b8SChris Wilson 427e18c0412SDaniel Vetter void drm_mm_remove_node(struct drm_mm_node *node); 428e18c0412SDaniel Vetter void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new); 42945b186f1SChris Wilson void drm_mm_init(struct drm_mm *mm, u64 start, u64 size); 430e18c0412SDaniel Vetter void drm_mm_takedown(struct drm_mm *mm); 431ac9bb7b7SChris Wilson 432ac9bb7b7SChris Wilson /** 433ac9bb7b7SChris Wilson * drm_mm_clean - checks whether an allocator is clean 434ac9bb7b7SChris Wilson * @mm: drm_mm allocator to check 435ac9bb7b7SChris Wilson * 436ac9bb7b7SChris Wilson * Returns: 437ac9bb7b7SChris Wilson * True if the allocator is completely free, false if there's still a node 438ac9bb7b7SChris Wilson * allocated in it. 439ac9bb7b7SChris Wilson */ 440ac9bb7b7SChris Wilson static inline bool drm_mm_clean(const struct drm_mm *mm) 441ac9bb7b7SChris Wilson { 442ac9bb7b7SChris Wilson return list_empty(drm_mm_nodes(mm)); 443ac9bb7b7SChris Wilson } 444249d6048SJerome Glisse 445202b52b7SChris Wilson struct drm_mm_node * 44645b186f1SChris Wilson __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last); 447202b52b7SChris Wilson 448522e85ddSChris Wilson /** 449522e85ddSChris Wilson * drm_mm_for_each_node_in_range - iterator to walk over a range of 450522e85ddSChris Wilson * allocated nodes 4518b2fb7b6SChris Wilson * @node__: drm_mm_node structure to assign to in each iteration step 4528b2fb7b6SChris Wilson * @mm__: drm_mm allocator to walk 4538b2fb7b6SChris Wilson * @start__: starting offset, the first node will overlap this 4548b2fb7b6SChris Wilson * @end__: ending offset, the last node will start before this (but may overlap) 455522e85ddSChris Wilson * 456522e85ddSChris Wilson * This iterator walks over all nodes in the range allocator that lie 457522e85ddSChris Wilson * between @start and @end. It is implemented similarly to list_for_each(), 458522e85ddSChris Wilson * but using the internal interval tree to accelerate the search for the 459522e85ddSChris Wilson * starting node, and so not safe against removal of elements. It assumes 460522e85ddSChris Wilson * that @end is within (or is the upper limit of) the drm_mm allocator. 461522e85ddSChris Wilson */ 4628b2fb7b6SChris Wilson #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__) \ 4638b2fb7b6SChris Wilson for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \ 4648b2fb7b6SChris Wilson node__ && node__->start < (end__); \ 4658b2fb7b6SChris Wilson node__ = list_next_entry(node__, node_list)) 466202b52b7SChris Wilson 4679a71e277SChris Wilson void drm_mm_scan_init_with_range(struct drm_mm_scan *scan, 4689a71e277SChris Wilson struct drm_mm *mm, 4690b04d474SChris Wilson u64 size, u64 alignment, unsigned long color, 4700b04d474SChris Wilson u64 start, u64 end, 4710b04d474SChris Wilson unsigned int flags); 4722c4b3895SChris Wilson 4732c4b3895SChris Wilson /** 4742c4b3895SChris Wilson * drm_mm_scan_init - initialize lru scanning 4752c4b3895SChris Wilson * @scan: scan state 4762c4b3895SChris Wilson * @mm: drm_mm to scan 4772c4b3895SChris Wilson * @size: size of the allocation 4782c4b3895SChris Wilson * @alignment: alignment of the allocation 4792c4b3895SChris Wilson * @color: opaque tag value to use for the allocation 4800b04d474SChris Wilson * @flags: flags to specify how the allocation will be performed afterwards 4812c4b3895SChris Wilson * 48205fc0321SDaniel Vetter * This is a simplified version of drm_mm_scan_init_with_range() with no range 48305fc0321SDaniel Vetter * restrictions applied. 48405fc0321SDaniel Vetter * 4852c4b3895SChris Wilson * This simply sets up the scanning routines with the parameters for the desired 4860b04d474SChris Wilson * hole. 4872c4b3895SChris Wilson * 4882c4b3895SChris Wilson * Warning: 4892c4b3895SChris Wilson * As long as the scan list is non-empty, no other operations than 4902c4b3895SChris Wilson * adding/removing nodes to/from the scan list are allowed. 4912c4b3895SChris Wilson */ 4922c4b3895SChris Wilson static inline void drm_mm_scan_init(struct drm_mm_scan *scan, 4932c4b3895SChris Wilson struct drm_mm *mm, 4942c4b3895SChris Wilson u64 size, 4952c4b3895SChris Wilson u64 alignment, 4960b04d474SChris Wilson unsigned long color, 4970b04d474SChris Wilson unsigned int flags) 4982c4b3895SChris Wilson { 4990b04d474SChris Wilson drm_mm_scan_init_with_range(scan, mm, 5000b04d474SChris Wilson size, alignment, color, 5010b04d474SChris Wilson 0, U64_MAX, 5020b04d474SChris Wilson flags); 5032c4b3895SChris Wilson } 5042c4b3895SChris Wilson 5059a71e277SChris Wilson bool drm_mm_scan_add_block(struct drm_mm_scan *scan, 5069a71e277SChris Wilson struct drm_mm_node *node); 5079a71e277SChris Wilson bool drm_mm_scan_remove_block(struct drm_mm_scan *scan, 5089a71e277SChris Wilson struct drm_mm_node *node); 5093fa489daSChris Wilson struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan); 510709ea971SDaniel Vetter 511b5c3714fSDaniel Vetter void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p); 512fa8a1238SDave Airlie 513249d6048SJerome Glisse #endif 514