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> 43589ee628SIngo Molnar #include <linux/mm_types.h> 44249d6048SJerome Glisse #include <linux/list.h> 4586e81f0eSDavid Herrmann #include <linux/spinlock.h> 465705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM 475705670dSChris Wilson #include <linux/stackdepot.h> 485705670dSChris Wilson #endif 49b5c3714fSDaniel Vetter #include <drm/drm_print.h> 50249d6048SJerome Glisse 51b3ee963fSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM 52b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUG_ON(expr) 53b3ee963fSChris Wilson #else 54b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr) 55b3ee963fSChris Wilson #endif 56b3ee963fSChris Wilson 574e64e553SChris Wilson /** 584e64e553SChris Wilson * enum drm_mm_insert_mode - control search and allocation behaviour 594e64e553SChris Wilson * 604e64e553SChris Wilson * The &struct drm_mm range manager supports finding a suitable modes using 614e64e553SChris Wilson * a number of search trees. These trees are oranised by size, by address and 624e64e553SChris Wilson * in most recent eviction order. This allows the user to find either the 634e64e553SChris Wilson * smallest hole to reuse, the lowest or highest address to reuse, or simply 644e64e553SChris Wilson * reuse the most recent eviction that fits. When allocating the &drm_mm_node 654e64e553SChris Wilson * from within the hole, the &drm_mm_insert_mode also dictate whether to 664e64e553SChris Wilson * allocate the lowest matching address or the highest. 674e64e553SChris Wilson */ 684e64e553SChris Wilson enum drm_mm_insert_mode { 694e64e553SChris Wilson /** 704e64e553SChris Wilson * @DRM_MM_INSERT_BEST: 714e64e553SChris Wilson * 724e64e553SChris Wilson * Search for the smallest hole (within the search range) that fits 734e64e553SChris Wilson * the desired node. 744e64e553SChris Wilson * 754e64e553SChris Wilson * Allocates the node from the bottom of the found hole. 764e64e553SChris Wilson */ 774e64e553SChris Wilson DRM_MM_INSERT_BEST = 0, 7831e5d7c6SDavid Herrmann 794e64e553SChris Wilson /** 804e64e553SChris Wilson * @DRM_MM_INSERT_LOW: 814e64e553SChris Wilson * 824e64e553SChris Wilson * Search for the lowest hole (address closest to 0, within the search 834e64e553SChris Wilson * range) that fits the desired node. 844e64e553SChris Wilson * 854e64e553SChris Wilson * Allocates the node from the bottom of the found hole. 864e64e553SChris Wilson */ 874e64e553SChris Wilson DRM_MM_INSERT_LOW, 8862347f9eSLauri Kasanen 894e64e553SChris Wilson /** 904e64e553SChris Wilson * @DRM_MM_INSERT_HIGH: 914e64e553SChris Wilson * 924e64e553SChris Wilson * Search for the highest hole (address closest to U64_MAX, within the 934e64e553SChris Wilson * search range) that fits the desired node. 944e64e553SChris Wilson * 954e64e553SChris Wilson * Allocates the node from the *top* of the found hole. The specified 964e64e553SChris Wilson * alignment for the node is applied to the base of the node 974e64e553SChris Wilson * (&drm_mm_node.start). 984e64e553SChris Wilson */ 994e64e553SChris Wilson DRM_MM_INSERT_HIGH, 1004e64e553SChris Wilson 1014e64e553SChris Wilson /** 1024e64e553SChris Wilson * @DRM_MM_INSERT_EVICT: 1034e64e553SChris Wilson * 1044e64e553SChris Wilson * Search for the most recently evicted hole (within the search range) 1054e64e553SChris Wilson * that fits the desired node. This is appropriate for use immediately 1064e64e553SChris Wilson * after performing an eviction scan (see drm_mm_scan_init()) and 1074e64e553SChris Wilson * removing the selected nodes to form a hole. 1084e64e553SChris Wilson * 1094e64e553SChris Wilson * Allocates the node from the bottom of the found hole. 1104e64e553SChris Wilson */ 1114e64e553SChris Wilson DRM_MM_INSERT_EVICT, 1124e64e553SChris Wilson }; 11362347f9eSLauri Kasanen 11405fc0321SDaniel Vetter /** 11505fc0321SDaniel Vetter * struct drm_mm_node - allocated block in the DRM allocator 11605fc0321SDaniel Vetter * 11705fc0321SDaniel Vetter * This represents an allocated block in a &drm_mm allocator. Except for 11805fc0321SDaniel Vetter * pre-reserved nodes inserted using drm_mm_reserve_node() the structure is 11905fc0321SDaniel Vetter * entirely opaque and should only be accessed through the provided funcions. 12005fc0321SDaniel Vetter * Since allocation of these nodes is entirely handled by the driver they can be 12105fc0321SDaniel Vetter * embedded. 12205fc0321SDaniel Vetter */ 123249d6048SJerome Glisse struct drm_mm_node { 12405fc0321SDaniel Vetter /** @color: Opaque driver-private tag. */ 12505fc0321SDaniel Vetter unsigned long color; 12605fc0321SDaniel Vetter /** @start: Start address of the allocated block. */ 12705fc0321SDaniel Vetter u64 start; 12805fc0321SDaniel Vetter /** @size: Size of the allocated block. */ 12905fc0321SDaniel Vetter u64 size; 13005fc0321SDaniel Vetter /* private: */ 1314e64e553SChris Wilson struct drm_mm *mm; 132d1024ce9SDaniel Vetter struct list_head node_list; 133ea7b1dd4SDaniel Vetter struct list_head hole_stack; 134202b52b7SChris Wilson struct rb_node rb; 1354e64e553SChris Wilson struct rb_node rb_hole_size; 1364e64e553SChris Wilson struct rb_node rb_hole_addr; 137202b52b7SChris Wilson u64 __subtree_last; 1384e64e553SChris Wilson u64 hole_size; 1394e64e553SChris Wilson bool allocated : 1; 1404e64e553SChris Wilson bool scanned_block : 1; 1415705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM 1425705670dSChris Wilson depot_stack_handle_t stack; 1435705670dSChris Wilson #endif 144249d6048SJerome Glisse }; 145249d6048SJerome Glisse 14605fc0321SDaniel Vetter /** 14705fc0321SDaniel Vetter * struct drm_mm - DRM allocator 14805fc0321SDaniel Vetter * 14905fc0321SDaniel Vetter * DRM range allocator with a few special functions and features geared towards 15005fc0321SDaniel Vetter * managing GPU memory. Except for the @color_adjust callback the structure is 15105fc0321SDaniel Vetter * entirely opaque and should only be accessed through the provided functions 15205fc0321SDaniel Vetter * and macros. This structure can be embedded into larger driver structures. 15305fc0321SDaniel Vetter */ 154249d6048SJerome Glisse struct drm_mm { 15505fc0321SDaniel Vetter /** 15605fc0321SDaniel Vetter * @color_adjust: 15705fc0321SDaniel Vetter * 15805fc0321SDaniel Vetter * Optional driver callback to further apply restrictions on a hole. The 15905fc0321SDaniel Vetter * node argument points at the node containing the hole from which the 16005fc0321SDaniel Vetter * block would be allocated (see drm_mm_hole_follows() and friends). The 16105fc0321SDaniel Vetter * other arguments are the size of the block to be allocated. The driver 16205fc0321SDaniel Vetter * can adjust the start and end as needed to e.g. insert guard pages. 16305fc0321SDaniel Vetter */ 16405fc0321SDaniel Vetter void (*color_adjust)(const struct drm_mm_node *node, 16505fc0321SDaniel Vetter unsigned long color, 16605fc0321SDaniel Vetter u64 *start, u64 *end); 16705fc0321SDaniel Vetter 16805fc0321SDaniel Vetter /* private: */ 16925985edcSLucas De Marchi /* List of all memory nodes that immediately precede a free hole. */ 170ea7b1dd4SDaniel Vetter struct list_head hole_stack; 171ea7b1dd4SDaniel Vetter /* head_node.node_list is the list of all memory nodes, ordered 172ea7b1dd4SDaniel Vetter * according to the (increasing) start address of the memory node. */ 173ea7b1dd4SDaniel Vetter struct drm_mm_node head_node; 174202b52b7SChris Wilson /* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */ 175f808c13fSDavidlohr Bueso struct rb_root_cached interval_tree; 1762f7e8769SChris Wilson struct rb_root_cached holes_size; 1774e64e553SChris Wilson struct rb_root holes_addr; 178202b52b7SChris Wilson 1799a71e277SChris Wilson unsigned long scan_active; 1809a71e277SChris Wilson }; 1819a71e277SChris Wilson 18205fc0321SDaniel Vetter /** 18305fc0321SDaniel Vetter * struct drm_mm_scan - DRM allocator eviction roaster data 18405fc0321SDaniel Vetter * 18505fc0321SDaniel Vetter * This structure tracks data needed for the eviction roaster set up using 18605fc0321SDaniel Vetter * drm_mm_scan_init(), and used with drm_mm_scan_add_block() and 18705fc0321SDaniel Vetter * drm_mm_scan_remove_block(). The structure is entirely opaque and should only 18805fc0321SDaniel Vetter * be accessed through the provided functions and macros. It is meant to be 18905fc0321SDaniel Vetter * allocated temporarily by the driver on the stack. 19005fc0321SDaniel Vetter */ 1919a71e277SChris Wilson struct drm_mm_scan { 19205fc0321SDaniel Vetter /* private: */ 1939a71e277SChris Wilson struct drm_mm *mm; 1949a71e277SChris Wilson 1959a71e277SChris Wilson u64 size; 1969a71e277SChris Wilson u64 alignment; 1979a956b15SChris Wilson u64 remainder_mask; 1989a71e277SChris Wilson 1999a71e277SChris Wilson u64 range_start; 2009a71e277SChris Wilson u64 range_end; 2019a71e277SChris Wilson 2029a71e277SChris Wilson u64 hit_start; 2039a71e277SChris Wilson u64 hit_end; 2049a71e277SChris Wilson 2059a71e277SChris Wilson unsigned long color; 2064e64e553SChris Wilson enum drm_mm_insert_mode mode; 207249d6048SJerome Glisse }; 208249d6048SJerome Glisse 209e18c0412SDaniel Vetter /** 210e18c0412SDaniel Vetter * drm_mm_node_allocated - checks whether a node is allocated 211e18c0412SDaniel Vetter * @node: drm_mm_node to check 212e18c0412SDaniel Vetter * 213ba004e39SChris Wilson * Drivers are required to clear a node prior to using it with the 214ba004e39SChris Wilson * drm_mm range manager. 215ba004e39SChris Wilson * 216ba004e39SChris Wilson * Drivers should use this helper for proper encapsulation of drm_mm 217e18c0412SDaniel Vetter * internals. 218e18c0412SDaniel Vetter * 219e18c0412SDaniel Vetter * Returns: 220e18c0412SDaniel Vetter * True if the @node is allocated. 221e18c0412SDaniel Vetter */ 22245b186f1SChris Wilson static inline bool drm_mm_node_allocated(const struct drm_mm_node *node) 223b0b7af18SDaniel Vetter { 224b0b7af18SDaniel Vetter return node->allocated; 225b0b7af18SDaniel Vetter } 226b0b7af18SDaniel Vetter 227e18c0412SDaniel Vetter /** 228e18c0412SDaniel Vetter * drm_mm_initialized - checks whether an allocator is initialized 229e18c0412SDaniel Vetter * @mm: drm_mm to check 230e18c0412SDaniel Vetter * 231ba004e39SChris Wilson * Drivers should clear the struct drm_mm prior to initialisation if they 232ba004e39SChris Wilson * want to use this function. 233ba004e39SChris Wilson * 234ba004e39SChris Wilson * Drivers should use this helper for proper encapsulation of drm_mm 235e18c0412SDaniel Vetter * internals. 236e18c0412SDaniel Vetter * 237e18c0412SDaniel Vetter * Returns: 238e18c0412SDaniel Vetter * True if the @mm is initialized. 239e18c0412SDaniel Vetter */ 24045b186f1SChris Wilson static inline bool drm_mm_initialized(const struct drm_mm *mm) 24131a5b8ceSDaniel Vetter { 242ea7b1dd4SDaniel Vetter return mm->hole_stack.next; 24331a5b8ceSDaniel Vetter } 2449e8944abSChris Wilson 2453f85fb34SChris Wilson /** 2463f85fb34SChris Wilson * drm_mm_hole_follows - checks whether a hole follows this node 2473f85fb34SChris Wilson * @node: drm_mm_node to check 2483f85fb34SChris Wilson * 2493f85fb34SChris Wilson * Holes are embedded into the drm_mm using the tail of a drm_mm_node. 2503f85fb34SChris Wilson * If you wish to know whether a hole follows this particular node, 25105fc0321SDaniel Vetter * query this function. See also drm_mm_hole_node_start() and 25205fc0321SDaniel Vetter * drm_mm_hole_node_end(). 2533f85fb34SChris Wilson * 2543f85fb34SChris Wilson * Returns: 2553f85fb34SChris Wilson * True if a hole follows the @node. 2563f85fb34SChris Wilson */ 2573f85fb34SChris Wilson static inline bool drm_mm_hole_follows(const struct drm_mm_node *node) 2583f85fb34SChris Wilson { 2594e64e553SChris Wilson return node->hole_size; 2603f85fb34SChris Wilson } 2613f85fb34SChris Wilson 26245b186f1SChris Wilson static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node) 2639e8944abSChris Wilson { 2649e8944abSChris Wilson return hole_node->start + hole_node->size; 2659e8944abSChris Wilson } 2669e8944abSChris Wilson 267e18c0412SDaniel Vetter /** 268e18c0412SDaniel Vetter * drm_mm_hole_node_start - computes the start of the hole following @node 269e18c0412SDaniel Vetter * @hole_node: drm_mm_node which implicitly tracks the following hole 270e18c0412SDaniel Vetter * 271ba004e39SChris Wilson * This is useful for driver-specific debug dumpers. Otherwise drivers should 272ba004e39SChris Wilson * not inspect holes themselves. Drivers must check first whether a hole indeed 2733f85fb34SChris Wilson * follows by looking at drm_mm_hole_follows() 274e18c0412SDaniel Vetter * 275e18c0412SDaniel Vetter * Returns: 276e18c0412SDaniel Vetter * Start of the subsequent hole. 277e18c0412SDaniel Vetter */ 27845b186f1SChris Wilson static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node) 2799e8944abSChris Wilson { 2803f85fb34SChris Wilson DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node)); 2819e8944abSChris Wilson return __drm_mm_hole_node_start(hole_node); 2829e8944abSChris Wilson } 2839e8944abSChris Wilson 28445b186f1SChris Wilson static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node) 2859e8944abSChris Wilson { 28687069f44SGeliang Tang return list_next_entry(hole_node, node_list)->start; 2879e8944abSChris Wilson } 2889e8944abSChris Wilson 289e18c0412SDaniel Vetter /** 290e18c0412SDaniel Vetter * drm_mm_hole_node_end - computes the end of the hole following @node 291e18c0412SDaniel Vetter * @hole_node: drm_mm_node which implicitly tracks the following hole 292e18c0412SDaniel Vetter * 293ba004e39SChris Wilson * This is useful for driver-specific debug dumpers. Otherwise drivers should 294ba004e39SChris Wilson * not inspect holes themselves. Drivers must check first whether a hole indeed 2953f85fb34SChris Wilson * follows by looking at drm_mm_hole_follows(). 296e18c0412SDaniel Vetter * 297e18c0412SDaniel Vetter * Returns: 298e18c0412SDaniel Vetter * End of the subsequent hole. 299e18c0412SDaniel Vetter */ 30045b186f1SChris Wilson static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node) 3019e8944abSChris Wilson { 3029e8944abSChris Wilson return __drm_mm_hole_node_end(hole_node); 3039e8944abSChris Wilson } 3049e8944abSChris Wilson 3052bc98c86SChris Wilson /** 3062bc98c86SChris Wilson * drm_mm_nodes - list of nodes under the drm_mm range manager 3072bc98c86SChris Wilson * @mm: the struct drm_mm range manger 3082bc98c86SChris Wilson * 3092bc98c86SChris Wilson * As the drm_mm range manager hides its node_list deep with its 3102bc98c86SChris Wilson * structure, extracting it looks painful and repetitive. This is 3112bc98c86SChris Wilson * not expected to be used outside of the drm_mm_for_each_node() 3122bc98c86SChris Wilson * macros and similar internal functions. 3132bc98c86SChris Wilson * 3142bc98c86SChris Wilson * Returns: 3152bc98c86SChris Wilson * The node list, may be empty. 3162bc98c86SChris Wilson */ 3172bc98c86SChris Wilson #define drm_mm_nodes(mm) (&(mm)->head_node.node_list) 318ad579002SChris Wilson 319e18c0412SDaniel Vetter /** 320e18c0412SDaniel Vetter * drm_mm_for_each_node - iterator to walk over all allocated nodes 32105fc0321SDaniel Vetter * @entry: &struct drm_mm_node to assign to in each iteration step 32205fc0321SDaniel Vetter * @mm: &drm_mm allocator to walk 323e18c0412SDaniel Vetter * 324e18c0412SDaniel Vetter * This iterator walks over all nodes in the range allocator. It is implemented 32505fc0321SDaniel Vetter * with list_for_each(), so not save against removal of elements. 326e18c0412SDaniel Vetter */ 327ad579002SChris Wilson #define drm_mm_for_each_node(entry, mm) \ 3282bc98c86SChris Wilson list_for_each_entry(entry, drm_mm_nodes(mm), node_list) 329ad579002SChris Wilson 330ad579002SChris Wilson /** 331ad579002SChris Wilson * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes 33205fc0321SDaniel Vetter * @entry: &struct drm_mm_node to assign to in each iteration step 33305fc0321SDaniel Vetter * @next: &struct drm_mm_node to store the next step 33405fc0321SDaniel Vetter * @mm: &drm_mm allocator to walk 335ad579002SChris Wilson * 336ad579002SChris Wilson * This iterator walks over all nodes in the range allocator. It is implemented 33705fc0321SDaniel Vetter * with list_for_each_safe(), so save against removal of elements. 338ad579002SChris Wilson */ 339ad579002SChris Wilson #define drm_mm_for_each_node_safe(entry, next, mm) \ 3402bc98c86SChris Wilson list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list) 3419e8944abSChris Wilson 342e18c0412SDaniel Vetter /** 343e18c0412SDaniel Vetter * drm_mm_for_each_hole - iterator to walk over all holes 3444e64e553SChris Wilson * @pos: &drm_mm_node used internally to track progress 34505fc0321SDaniel Vetter * @mm: &drm_mm allocator to walk 346e18c0412SDaniel Vetter * @hole_start: ulong variable to assign the hole start to on each iteration 347e18c0412SDaniel Vetter * @hole_end: ulong variable to assign the hole end to on each iteration 348e18c0412SDaniel Vetter * 349e18c0412SDaniel Vetter * This iterator walks over all holes in the range allocator. It is implemented 35005fc0321SDaniel Vetter * with list_for_each(), so not save against removal of elements. @entry is used 351e18c0412SDaniel Vetter * internally and will not reflect a real drm_mm_node for the very first hole. 352e18c0412SDaniel Vetter * Hence users of this iterator may not access it. 353e18c0412SDaniel Vetter * 354e18c0412SDaniel Vetter * Implementation Note: 355e18c0412SDaniel Vetter * We need to inline list_for_each_entry in order to be able to set hole_start 356e18c0412SDaniel Vetter * and hole_end on each iteration while keeping the macro sane. 3579e8944abSChris Wilson */ 3584e64e553SChris Wilson #define drm_mm_for_each_hole(pos, mm, hole_start, hole_end) \ 3594e64e553SChris Wilson for (pos = list_first_entry(&(mm)->hole_stack, \ 3604e64e553SChris Wilson typeof(*pos), hole_stack); \ 3614e64e553SChris Wilson &pos->hole_stack != &(mm)->hole_stack ? \ 3624e64e553SChris Wilson hole_start = drm_mm_hole_node_start(pos), \ 3634e64e553SChris Wilson hole_end = hole_start + pos->hole_size, \ 3644e64e553SChris Wilson 1 : 0; \ 3654e64e553SChris Wilson pos = list_next_entry(pos, hole_stack)) 36662347f9eSLauri Kasanen 367249d6048SJerome Glisse /* 368249d6048SJerome Glisse * Basic range manager support (drm_mm.c) 369249d6048SJerome Glisse */ 370e18c0412SDaniel Vetter int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node); 3714e64e553SChris Wilson int drm_mm_insert_node_in_range(struct drm_mm *mm, 372b8103450SChris Wilson struct drm_mm_node *node, 373440fd528SThierry Reding u64 size, 37471733207SChris Wilson u64 alignment, 375b8103450SChris Wilson unsigned long color, 376440fd528SThierry Reding u64 start, 377440fd528SThierry Reding u64 end, 3784e64e553SChris Wilson enum drm_mm_insert_mode mode); 37931e5d7c6SDavid Herrmann 380adb040b8SChris Wilson /** 381adb040b8SChris Wilson * drm_mm_insert_node_generic - search for space and insert @node 382adb040b8SChris Wilson * @mm: drm_mm to allocate from 383adb040b8SChris Wilson * @node: preallocate node to insert 384adb040b8SChris Wilson * @size: size of the allocation 385adb040b8SChris Wilson * @alignment: alignment of the allocation 386adb040b8SChris Wilson * @color: opaque tag value to use for this node 3874e64e553SChris Wilson * @mode: fine-tune the allocation search and placement 388adb040b8SChris Wilson * 3890a2adb02SLiviu Dudau * This is a simplified version of drm_mm_insert_node_in_range() with no 39005fc0321SDaniel Vetter * range restrictions applied. 39105fc0321SDaniel Vetter * 392adb040b8SChris Wilson * The preallocated node must be cleared to 0. 393adb040b8SChris Wilson * 394adb040b8SChris Wilson * Returns: 395adb040b8SChris Wilson * 0 on success, -ENOSPC if there's no suitable hole. 396adb040b8SChris Wilson */ 397adb040b8SChris Wilson static inline int 398adb040b8SChris Wilson drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node, 399adb040b8SChris Wilson u64 size, u64 alignment, 400adb040b8SChris Wilson unsigned long color, 4014e64e553SChris Wilson enum drm_mm_insert_mode mode) 402adb040b8SChris Wilson { 4034e64e553SChris Wilson return drm_mm_insert_node_in_range(mm, node, 4044e64e553SChris Wilson size, alignment, color, 4054e64e553SChris Wilson 0, U64_MAX, mode); 406adb040b8SChris Wilson } 407adb040b8SChris Wilson 408adb040b8SChris Wilson /** 409adb040b8SChris Wilson * drm_mm_insert_node - search for space and insert @node 410adb040b8SChris Wilson * @mm: drm_mm to allocate from 411adb040b8SChris Wilson * @node: preallocate node to insert 412adb040b8SChris Wilson * @size: size of the allocation 413adb040b8SChris Wilson * 414adb040b8SChris Wilson * This is a simplified version of drm_mm_insert_node_generic() with @color set 415adb040b8SChris Wilson * to 0. 416adb040b8SChris Wilson * 417adb040b8SChris Wilson * The preallocated node must be cleared to 0. 418adb040b8SChris Wilson * 419adb040b8SChris Wilson * Returns: 420adb040b8SChris Wilson * 0 on success, -ENOSPC if there's no suitable hole. 421adb040b8SChris Wilson */ 422adb040b8SChris Wilson static inline int drm_mm_insert_node(struct drm_mm *mm, 423adb040b8SChris Wilson struct drm_mm_node *node, 4244e64e553SChris Wilson u64 size) 425adb040b8SChris Wilson { 4264e64e553SChris Wilson return drm_mm_insert_node_generic(mm, node, size, 0, 0, 0); 427adb040b8SChris Wilson } 428adb040b8SChris Wilson 429e18c0412SDaniel Vetter void drm_mm_remove_node(struct drm_mm_node *node); 430e18c0412SDaniel Vetter void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new); 43145b186f1SChris Wilson void drm_mm_init(struct drm_mm *mm, u64 start, u64 size); 432e18c0412SDaniel Vetter void drm_mm_takedown(struct drm_mm *mm); 433ac9bb7b7SChris Wilson 434ac9bb7b7SChris Wilson /** 435ac9bb7b7SChris Wilson * drm_mm_clean - checks whether an allocator is clean 436ac9bb7b7SChris Wilson * @mm: drm_mm allocator to check 437ac9bb7b7SChris Wilson * 438ac9bb7b7SChris Wilson * Returns: 439ac9bb7b7SChris Wilson * True if the allocator is completely free, false if there's still a node 440ac9bb7b7SChris Wilson * allocated in it. 441ac9bb7b7SChris Wilson */ 442ac9bb7b7SChris Wilson static inline bool drm_mm_clean(const struct drm_mm *mm) 443ac9bb7b7SChris Wilson { 444ac9bb7b7SChris Wilson return list_empty(drm_mm_nodes(mm)); 445ac9bb7b7SChris Wilson } 446249d6048SJerome Glisse 447202b52b7SChris Wilson struct drm_mm_node * 44845b186f1SChris Wilson __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last); 449202b52b7SChris Wilson 450522e85ddSChris Wilson /** 451522e85ddSChris Wilson * drm_mm_for_each_node_in_range - iterator to walk over a range of 452522e85ddSChris Wilson * allocated nodes 4538b2fb7b6SChris Wilson * @node__: drm_mm_node structure to assign to in each iteration step 4548b2fb7b6SChris Wilson * @mm__: drm_mm allocator to walk 4558b2fb7b6SChris Wilson * @start__: starting offset, the first node will overlap this 4568b2fb7b6SChris Wilson * @end__: ending offset, the last node will start before this (but may overlap) 457522e85ddSChris Wilson * 458522e85ddSChris Wilson * This iterator walks over all nodes in the range allocator that lie 459522e85ddSChris Wilson * between @start and @end. It is implemented similarly to list_for_each(), 460522e85ddSChris Wilson * but using the internal interval tree to accelerate the search for the 461522e85ddSChris Wilson * starting node, and so not safe against removal of elements. It assumes 462522e85ddSChris Wilson * that @end is within (or is the upper limit of) the drm_mm allocator. 463bbba9693SChris Wilson * If [@start, @end] are beyond the range of the drm_mm, the iterator may walk 464bbba9693SChris Wilson * over the special _unallocated_ &drm_mm.head_node, and may even continue 465bbba9693SChris Wilson * indefinitely. 466522e85ddSChris Wilson */ 4678b2fb7b6SChris Wilson #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__) \ 4688b2fb7b6SChris Wilson for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \ 469bbba9693SChris Wilson node__->start < (end__); \ 4708b2fb7b6SChris Wilson node__ = list_next_entry(node__, node_list)) 471202b52b7SChris Wilson 4729a71e277SChris Wilson void drm_mm_scan_init_with_range(struct drm_mm_scan *scan, 4739a71e277SChris Wilson struct drm_mm *mm, 4740b04d474SChris Wilson u64 size, u64 alignment, unsigned long color, 4750b04d474SChris Wilson u64 start, u64 end, 4764e64e553SChris Wilson enum drm_mm_insert_mode mode); 4772c4b3895SChris Wilson 4782c4b3895SChris Wilson /** 4792c4b3895SChris Wilson * drm_mm_scan_init - initialize lru scanning 4802c4b3895SChris Wilson * @scan: scan state 4812c4b3895SChris Wilson * @mm: drm_mm to scan 4822c4b3895SChris Wilson * @size: size of the allocation 4832c4b3895SChris Wilson * @alignment: alignment of the allocation 4842c4b3895SChris Wilson * @color: opaque tag value to use for the allocation 4854e64e553SChris Wilson * @mode: fine-tune the allocation search and placement 4862c4b3895SChris Wilson * 48705fc0321SDaniel Vetter * This is a simplified version of drm_mm_scan_init_with_range() with no range 48805fc0321SDaniel Vetter * restrictions applied. 48905fc0321SDaniel Vetter * 4902c4b3895SChris Wilson * This simply sets up the scanning routines with the parameters for the desired 4910b04d474SChris Wilson * hole. 4922c4b3895SChris Wilson * 4932c4b3895SChris Wilson * Warning: 4942c4b3895SChris Wilson * As long as the scan list is non-empty, no other operations than 4952c4b3895SChris Wilson * adding/removing nodes to/from the scan list are allowed. 4962c4b3895SChris Wilson */ 4972c4b3895SChris Wilson static inline void drm_mm_scan_init(struct drm_mm_scan *scan, 4982c4b3895SChris Wilson struct drm_mm *mm, 4992c4b3895SChris Wilson u64 size, 5002c4b3895SChris Wilson u64 alignment, 5010b04d474SChris Wilson unsigned long color, 5024e64e553SChris Wilson enum drm_mm_insert_mode mode) 5032c4b3895SChris Wilson { 5040b04d474SChris Wilson drm_mm_scan_init_with_range(scan, mm, 5050b04d474SChris Wilson size, alignment, color, 5064e64e553SChris Wilson 0, U64_MAX, mode); 5072c4b3895SChris Wilson } 5082c4b3895SChris Wilson 5099a71e277SChris Wilson bool drm_mm_scan_add_block(struct drm_mm_scan *scan, 5109a71e277SChris Wilson struct drm_mm_node *node); 5119a71e277SChris Wilson bool drm_mm_scan_remove_block(struct drm_mm_scan *scan, 5129a71e277SChris Wilson struct drm_mm_node *node); 5133fa489daSChris Wilson struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan); 514709ea971SDaniel Vetter 515b5c3714fSDaniel Vetter void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p); 516fa8a1238SDave Airlie 517249d6048SJerome Glisse #endif 518