Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA. |
Chris Wilson | ba004e3 | 2016-12-22 08:36:25 +0000 | [diff] [blame] | 4 | * Copyright 2016 Intel Corporation |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 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 | |
| 34 | #ifndef _DRM_MM_H_ |
| 35 | #define _DRM_MM_H_ |
| 36 | |
| 37 | /* |
| 38 | * Generic range manager structs |
| 39 | */ |
David Herrmann | 86e81f0e | 2013-07-25 18:02:31 +0200 | [diff] [blame] | 40 | #include <linux/bug.h> |
Chris Wilson | 202b52b | 2016-08-03 16:04:09 +0100 | [diff] [blame] | 41 | #include <linux/rbtree.h> |
David Herrmann | 86e81f0e | 2013-07-25 18:02:31 +0200 | [diff] [blame] | 42 | #include <linux/kernel.h> |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 43 | #include <linux/list.h> |
David Herrmann | 86e81f0e | 2013-07-25 18:02:31 +0200 | [diff] [blame] | 44 | #include <linux/spinlock.h> |
Dave Airlie | f1938cd | 2009-09-08 11:32:08 +1000 | [diff] [blame] | 45 | #ifdef CONFIG_DEBUG_FS |
| 46 | #include <linux/seq_file.h> |
| 47 | #endif |
Chris Wilson | 5705670 | 2016-10-31 09:08:06 +0000 | [diff] [blame] | 48 | #ifdef CONFIG_DRM_DEBUG_MM |
| 49 | #include <linux/stackdepot.h> |
| 50 | #endif |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 51 | |
Chris Wilson | b3ee963 | 2016-12-22 08:36:06 +0000 | [diff] [blame] | 52 | #ifdef CONFIG_DRM_DEBUG_MM |
| 53 | #define DRM_MM_BUG_ON(expr) BUG_ON(expr) |
| 54 | #else |
| 55 | #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr) |
| 56 | #endif |
| 57 | |
David Herrmann | 31e5d7c | 2013-07-27 13:36:27 +0200 | [diff] [blame] | 58 | enum drm_mm_search_flags { |
| 59 | DRM_MM_SEARCH_DEFAULT = 0, |
| 60 | DRM_MM_SEARCH_BEST = 1 << 0, |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 61 | DRM_MM_SEARCH_BELOW = 1 << 1, |
David Herrmann | 31e5d7c | 2013-07-27 13:36:27 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 64 | enum drm_mm_allocator_flags { |
| 65 | DRM_MM_CREATE_DEFAULT = 0, |
| 66 | DRM_MM_CREATE_TOP = 1 << 0, |
| 67 | }; |
| 68 | |
| 69 | #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT |
| 70 | #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP |
| 71 | |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 72 | struct drm_mm_node { |
Daniel Vetter | d1024ce | 2010-07-02 15:02:14 +0100 | [diff] [blame] | 73 | struct list_head node_list; |
Daniel Vetter | ea7b1dd | 2011-02-18 17:59:12 +0100 | [diff] [blame] | 74 | struct list_head hole_stack; |
Chris Wilson | 202b52b | 2016-08-03 16:04:09 +0100 | [diff] [blame] | 75 | struct rb_node rb; |
Daniel Vetter | ea7b1dd | 2011-02-18 17:59:12 +0100 | [diff] [blame] | 76 | unsigned hole_follows : 1; |
Daniel Vetter | b0b7af1 | 2011-02-18 17:59:14 +0100 | [diff] [blame] | 77 | unsigned allocated : 1; |
Chris Wilson | f29051f | 2016-12-22 08:36:35 +0000 | [diff] [blame] | 78 | bool scanned_block : 1; |
Chris Wilson | 6b9d89b | 2012-07-10 11:15:23 +0100 | [diff] [blame] | 79 | unsigned long color; |
Thierry Reding | 440fd52 | 2015-01-23 09:05:06 +0100 | [diff] [blame] | 80 | u64 start; |
| 81 | u64 size; |
Chris Wilson | 202b52b | 2016-08-03 16:04:09 +0100 | [diff] [blame] | 82 | u64 __subtree_last; |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 83 | struct drm_mm *mm; |
Chris Wilson | 5705670 | 2016-10-31 09:08:06 +0000 | [diff] [blame] | 84 | #ifdef CONFIG_DRM_DEBUG_MM |
| 85 | depot_stack_handle_t stack; |
| 86 | #endif |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | struct drm_mm { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 90 | /* List of all memory nodes that immediately precede a free hole. */ |
Daniel Vetter | ea7b1dd | 2011-02-18 17:59:12 +0100 | [diff] [blame] | 91 | struct list_head hole_stack; |
| 92 | /* head_node.node_list is the list of all memory nodes, ordered |
| 93 | * according to the (increasing) start address of the memory node. */ |
| 94 | struct drm_mm_node head_node; |
Chris Wilson | 202b52b | 2016-08-03 16:04:09 +0100 | [diff] [blame] | 95 | /* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */ |
| 96 | struct rb_root interval_tree; |
| 97 | |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 98 | void (*color_adjust)(const struct drm_mm_node *node, |
| 99 | unsigned long color, |
Thierry Reding | 440fd52 | 2015-01-23 09:05:06 +0100 | [diff] [blame] | 100 | u64 *start, u64 *end); |
Chris Wilson | 9a71e27 | 2016-12-22 08:36:29 +0000 | [diff] [blame] | 101 | |
| 102 | unsigned long scan_active; |
| 103 | }; |
| 104 | |
| 105 | struct drm_mm_scan { |
| 106 | struct drm_mm *mm; |
| 107 | |
| 108 | u64 size; |
| 109 | u64 alignment; |
Chris Wilson | 9a956b1 | 2016-12-22 08:36:34 +0000 | [diff] [blame] | 110 | u64 remainder_mask; |
Chris Wilson | 9a71e27 | 2016-12-22 08:36:29 +0000 | [diff] [blame] | 111 | |
| 112 | u64 range_start; |
| 113 | u64 range_end; |
| 114 | |
| 115 | u64 hit_start; |
| 116 | u64 hit_end; |
| 117 | |
Chris Wilson | 9a71e27 | 2016-12-22 08:36:29 +0000 | [diff] [blame] | 118 | unsigned long color; |
Chris Wilson | 0b04d47 | 2016-12-22 08:36:33 +0000 | [diff] [blame] | 119 | unsigned int flags; |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 122 | /** |
| 123 | * drm_mm_node_allocated - checks whether a node is allocated |
| 124 | * @node: drm_mm_node to check |
| 125 | * |
Chris Wilson | ba004e3 | 2016-12-22 08:36:25 +0000 | [diff] [blame] | 126 | * Drivers are required to clear a node prior to using it with the |
| 127 | * drm_mm range manager. |
| 128 | * |
| 129 | * Drivers should use this helper for proper encapsulation of drm_mm |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 130 | * internals. |
| 131 | * |
| 132 | * Returns: |
| 133 | * True if the @node is allocated. |
| 134 | */ |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 135 | static inline bool drm_mm_node_allocated(const struct drm_mm_node *node) |
Daniel Vetter | b0b7af1 | 2011-02-18 17:59:14 +0100 | [diff] [blame] | 136 | { |
| 137 | return node->allocated; |
| 138 | } |
| 139 | |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 140 | /** |
| 141 | * drm_mm_initialized - checks whether an allocator is initialized |
| 142 | * @mm: drm_mm to check |
| 143 | * |
Chris Wilson | ba004e3 | 2016-12-22 08:36:25 +0000 | [diff] [blame] | 144 | * Drivers should clear the struct drm_mm prior to initialisation if they |
| 145 | * want to use this function. |
| 146 | * |
| 147 | * Drivers should use this helper for proper encapsulation of drm_mm |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 148 | * internals. |
| 149 | * |
| 150 | * Returns: |
| 151 | * True if the @mm is initialized. |
| 152 | */ |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 153 | static inline bool drm_mm_initialized(const struct drm_mm *mm) |
Daniel Vetter | 31a5b8ce | 2011-02-18 17:59:11 +0100 | [diff] [blame] | 154 | { |
Daniel Vetter | ea7b1dd | 2011-02-18 17:59:12 +0100 | [diff] [blame] | 155 | return mm->hole_stack.next; |
Daniel Vetter | 31a5b8ce | 2011-02-18 17:59:11 +0100 | [diff] [blame] | 156 | } |
Chris Wilson | 9e8944a | 2012-11-15 11:32:17 +0000 | [diff] [blame] | 157 | |
Chris Wilson | 3f85fb3 | 2016-12-22 08:36:37 +0000 | [diff] [blame] | 158 | /** |
| 159 | * drm_mm_hole_follows - checks whether a hole follows this node |
| 160 | * @node: drm_mm_node to check |
| 161 | * |
| 162 | * Holes are embedded into the drm_mm using the tail of a drm_mm_node. |
| 163 | * If you wish to know whether a hole follows this particular node, |
| 164 | * query this function. |
| 165 | * |
| 166 | * Returns: |
| 167 | * True if a hole follows the @node. |
| 168 | */ |
| 169 | static inline bool drm_mm_hole_follows(const struct drm_mm_node *node) |
| 170 | { |
| 171 | return node->hole_follows; |
| 172 | } |
| 173 | |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 174 | static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node) |
Chris Wilson | 9e8944a | 2012-11-15 11:32:17 +0000 | [diff] [blame] | 175 | { |
| 176 | return hole_node->start + hole_node->size; |
| 177 | } |
| 178 | |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 179 | /** |
| 180 | * drm_mm_hole_node_start - computes the start of the hole following @node |
| 181 | * @hole_node: drm_mm_node which implicitly tracks the following hole |
| 182 | * |
Chris Wilson | ba004e3 | 2016-12-22 08:36:25 +0000 | [diff] [blame] | 183 | * This is useful for driver-specific debug dumpers. Otherwise drivers should |
| 184 | * not inspect holes themselves. Drivers must check first whether a hole indeed |
Chris Wilson | 3f85fb3 | 2016-12-22 08:36:37 +0000 | [diff] [blame] | 185 | * follows by looking at drm_mm_hole_follows() |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 186 | * |
| 187 | * Returns: |
| 188 | * Start of the subsequent hole. |
| 189 | */ |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 190 | static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node) |
Chris Wilson | 9e8944a | 2012-11-15 11:32:17 +0000 | [diff] [blame] | 191 | { |
Chris Wilson | 3f85fb3 | 2016-12-22 08:36:37 +0000 | [diff] [blame] | 192 | DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node)); |
Chris Wilson | 9e8944a | 2012-11-15 11:32:17 +0000 | [diff] [blame] | 193 | return __drm_mm_hole_node_start(hole_node); |
| 194 | } |
| 195 | |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 196 | static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node) |
Chris Wilson | 9e8944a | 2012-11-15 11:32:17 +0000 | [diff] [blame] | 197 | { |
Geliang Tang | 87069f4 | 2015-11-25 21:23:07 +0800 | [diff] [blame] | 198 | return list_next_entry(hole_node, node_list)->start; |
Chris Wilson | 9e8944a | 2012-11-15 11:32:17 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 201 | /** |
| 202 | * drm_mm_hole_node_end - computes the end of the hole following @node |
| 203 | * @hole_node: drm_mm_node which implicitly tracks the following hole |
| 204 | * |
Chris Wilson | ba004e3 | 2016-12-22 08:36:25 +0000 | [diff] [blame] | 205 | * This is useful for driver-specific debug dumpers. Otherwise drivers should |
| 206 | * not inspect holes themselves. Drivers must check first whether a hole indeed |
Chris Wilson | 3f85fb3 | 2016-12-22 08:36:37 +0000 | [diff] [blame] | 207 | * follows by looking at drm_mm_hole_follows(). |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 208 | * |
| 209 | * Returns: |
| 210 | * End of the subsequent hole. |
| 211 | */ |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 212 | static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node) |
Chris Wilson | 9e8944a | 2012-11-15 11:32:17 +0000 | [diff] [blame] | 213 | { |
| 214 | return __drm_mm_hole_node_end(hole_node); |
| 215 | } |
| 216 | |
Chris Wilson | 2bc98c8 | 2016-12-22 08:36:05 +0000 | [diff] [blame] | 217 | /** |
| 218 | * drm_mm_nodes - list of nodes under the drm_mm range manager |
| 219 | * @mm: the struct drm_mm range manger |
| 220 | * |
| 221 | * As the drm_mm range manager hides its node_list deep with its |
| 222 | * structure, extracting it looks painful and repetitive. This is |
| 223 | * not expected to be used outside of the drm_mm_for_each_node() |
| 224 | * macros and similar internal functions. |
| 225 | * |
| 226 | * Returns: |
| 227 | * The node list, may be empty. |
| 228 | */ |
| 229 | #define drm_mm_nodes(mm) (&(mm)->head_node.node_list) |
Chris Wilson | ad57900 | 2016-12-16 07:46:41 +0000 | [diff] [blame] | 230 | |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 231 | /** |
| 232 | * drm_mm_for_each_node - iterator to walk over all allocated nodes |
| 233 | * @entry: drm_mm_node structure to assign to in each iteration step |
| 234 | * @mm: drm_mm allocator to walk |
| 235 | * |
| 236 | * This iterator walks over all nodes in the range allocator. It is implemented |
| 237 | * with list_for_each, so not save against removal of elements. |
| 238 | */ |
Chris Wilson | ad57900 | 2016-12-16 07:46:41 +0000 | [diff] [blame] | 239 | #define drm_mm_for_each_node(entry, mm) \ |
Chris Wilson | 2bc98c8 | 2016-12-22 08:36:05 +0000 | [diff] [blame] | 240 | list_for_each_entry(entry, drm_mm_nodes(mm), node_list) |
Chris Wilson | ad57900 | 2016-12-16 07:46:41 +0000 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes |
| 244 | * @entry: drm_mm_node structure to assign to in each iteration step |
| 245 | * @next: drm_mm_node structure to store the next step |
| 246 | * @mm: drm_mm allocator to walk |
| 247 | * |
| 248 | * This iterator walks over all nodes in the range allocator. It is implemented |
| 249 | * with list_for_each_safe, so save against removal of elements. |
| 250 | */ |
| 251 | #define drm_mm_for_each_node_safe(entry, next, mm) \ |
Chris Wilson | 2bc98c8 | 2016-12-22 08:36:05 +0000 | [diff] [blame] | 252 | list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list) |
Chris Wilson | 9e8944a | 2012-11-15 11:32:17 +0000 | [diff] [blame] | 253 | |
Geliang Tang | 18b40c5 | 2015-11-21 22:04:04 +0800 | [diff] [blame] | 254 | #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \ |
| 255 | for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \ |
| 256 | &entry->hole_stack != &(mm)->hole_stack ? \ |
| 257 | hole_start = drm_mm_hole_node_start(entry), \ |
| 258 | hole_end = drm_mm_hole_node_end(entry), \ |
| 259 | 1 : 0; \ |
| 260 | entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack)) |
| 261 | |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 262 | /** |
| 263 | * drm_mm_for_each_hole - iterator to walk over all holes |
| 264 | * @entry: drm_mm_node used internally to track progress |
| 265 | * @mm: drm_mm allocator to walk |
| 266 | * @hole_start: ulong variable to assign the hole start to on each iteration |
| 267 | * @hole_end: ulong variable to assign the hole end to on each iteration |
| 268 | * |
| 269 | * This iterator walks over all holes in the range allocator. It is implemented |
| 270 | * with list_for_each, so not save against removal of elements. @entry is used |
| 271 | * internally and will not reflect a real drm_mm_node for the very first hole. |
| 272 | * Hence users of this iterator may not access it. |
| 273 | * |
| 274 | * Implementation Note: |
| 275 | * We need to inline list_for_each_entry in order to be able to set hole_start |
| 276 | * and hole_end on each iteration while keeping the macro sane. |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 277 | * |
| 278 | * The __drm_mm_for_each_hole version is similar, but with added support for |
| 279 | * going backwards. |
Chris Wilson | 9e8944a | 2012-11-15 11:32:17 +0000 | [diff] [blame] | 280 | */ |
| 281 | #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \ |
Geliang Tang | 18b40c5 | 2015-11-21 22:04:04 +0800 | [diff] [blame] | 282 | __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0) |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 283 | |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 284 | /* |
| 285 | * Basic range manager support (drm_mm.c) |
| 286 | */ |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 287 | int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node); |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 288 | int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, |
| 289 | struct drm_mm_node *node, |
Thierry Reding | 440fd52 | 2015-01-23 09:05:06 +0100 | [diff] [blame] | 290 | u64 size, |
Chris Wilson | 7173320 | 2016-12-22 08:36:24 +0000 | [diff] [blame] | 291 | u64 alignment, |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 292 | unsigned long color, |
Thierry Reding | 440fd52 | 2015-01-23 09:05:06 +0100 | [diff] [blame] | 293 | u64 start, |
| 294 | u64 end, |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 295 | enum drm_mm_search_flags sflags, |
| 296 | enum drm_mm_allocator_flags aflags); |
Chris Wilson | adb040b | 2016-12-22 08:36:39 +0000 | [diff] [blame^] | 297 | |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 298 | /** |
| 299 | * drm_mm_insert_node_in_range - ranged search for space and insert @node |
| 300 | * @mm: drm_mm to allocate from |
| 301 | * @node: preallocate node to insert |
| 302 | * @size: size of the allocation |
| 303 | * @alignment: alignment of the allocation |
| 304 | * @start: start of the allowed range for this node |
| 305 | * @end: end of the allowed range for this node |
| 306 | * @flags: flags to fine-tune the allocation |
| 307 | * |
| 308 | * This is a simplified version of drm_mm_insert_node_in_range_generic() with |
| 309 | * @color set to 0. |
| 310 | * |
| 311 | * The preallocated node must be cleared to 0. |
| 312 | * |
| 313 | * Returns: |
| 314 | * 0 on success, -ENOSPC if there's no suitable hole. |
| 315 | */ |
David Herrmann | 31e5d7c | 2013-07-27 13:36:27 +0200 | [diff] [blame] | 316 | static inline int drm_mm_insert_node_in_range(struct drm_mm *mm, |
| 317 | struct drm_mm_node *node, |
Thierry Reding | 440fd52 | 2015-01-23 09:05:06 +0100 | [diff] [blame] | 318 | u64 size, |
Chris Wilson | 7173320 | 2016-12-22 08:36:24 +0000 | [diff] [blame] | 319 | u64 alignment, |
Thierry Reding | 440fd52 | 2015-01-23 09:05:06 +0100 | [diff] [blame] | 320 | u64 start, |
| 321 | u64 end, |
David Herrmann | 31e5d7c | 2013-07-27 13:36:27 +0200 | [diff] [blame] | 322 | enum drm_mm_search_flags flags) |
| 323 | { |
| 324 | return drm_mm_insert_node_in_range_generic(mm, node, size, alignment, |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 325 | 0, start, end, flags, |
| 326 | DRM_MM_CREATE_DEFAULT); |
David Herrmann | 31e5d7c | 2013-07-27 13:36:27 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Chris Wilson | adb040b | 2016-12-22 08:36:39 +0000 | [diff] [blame^] | 329 | /** |
| 330 | * drm_mm_insert_node_generic - search for space and insert @node |
| 331 | * @mm: drm_mm to allocate from |
| 332 | * @node: preallocate node to insert |
| 333 | * @size: size of the allocation |
| 334 | * @alignment: alignment of the allocation |
| 335 | * @color: opaque tag value to use for this node |
| 336 | * @sflags: flags to fine-tune the allocation search |
| 337 | * @aflags: flags to fine-tune the allocation behavior |
| 338 | * |
| 339 | * The preallocated node must be cleared to 0. |
| 340 | * |
| 341 | * Returns: |
| 342 | * 0 on success, -ENOSPC if there's no suitable hole. |
| 343 | */ |
| 344 | static inline int |
| 345 | drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node, |
| 346 | u64 size, u64 alignment, |
| 347 | unsigned long color, |
| 348 | enum drm_mm_search_flags sflags, |
| 349 | enum drm_mm_allocator_flags aflags) |
| 350 | { |
| 351 | return drm_mm_insert_node_in_range_generic(mm, node, |
| 352 | size, alignment, 0, |
| 353 | 0, U64_MAX, |
| 354 | sflags, aflags); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * drm_mm_insert_node - search for space and insert @node |
| 359 | * @mm: drm_mm to allocate from |
| 360 | * @node: preallocate node to insert |
| 361 | * @size: size of the allocation |
| 362 | * @alignment: alignment of the allocation |
| 363 | * @flags: flags to fine-tune the allocation |
| 364 | * |
| 365 | * This is a simplified version of drm_mm_insert_node_generic() with @color set |
| 366 | * to 0. |
| 367 | * |
| 368 | * The preallocated node must be cleared to 0. |
| 369 | * |
| 370 | * Returns: |
| 371 | * 0 on success, -ENOSPC if there's no suitable hole. |
| 372 | */ |
| 373 | static inline int drm_mm_insert_node(struct drm_mm *mm, |
| 374 | struct drm_mm_node *node, |
| 375 | u64 size, |
| 376 | u64 alignment, |
| 377 | enum drm_mm_search_flags flags) |
| 378 | { |
| 379 | return drm_mm_insert_node_generic(mm, node, |
| 380 | size, alignment, 0, |
| 381 | flags, DRM_MM_CREATE_DEFAULT); |
| 382 | } |
| 383 | |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 384 | void drm_mm_remove_node(struct drm_mm_node *node); |
| 385 | void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new); |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 386 | void drm_mm_init(struct drm_mm *mm, u64 start, u64 size); |
Daniel Vetter | e18c041 | 2014-01-23 00:39:13 +0100 | [diff] [blame] | 387 | void drm_mm_takedown(struct drm_mm *mm); |
Chris Wilson | ac9bb7b | 2016-12-22 08:36:27 +0000 | [diff] [blame] | 388 | |
| 389 | /** |
| 390 | * drm_mm_clean - checks whether an allocator is clean |
| 391 | * @mm: drm_mm allocator to check |
| 392 | * |
| 393 | * Returns: |
| 394 | * True if the allocator is completely free, false if there's still a node |
| 395 | * allocated in it. |
| 396 | */ |
| 397 | static inline bool drm_mm_clean(const struct drm_mm *mm) |
| 398 | { |
| 399 | return list_empty(drm_mm_nodes(mm)); |
| 400 | } |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 401 | |
Chris Wilson | 202b52b | 2016-08-03 16:04:09 +0100 | [diff] [blame] | 402 | struct drm_mm_node * |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 403 | __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last); |
Chris Wilson | 202b52b | 2016-08-03 16:04:09 +0100 | [diff] [blame] | 404 | |
Chris Wilson | 522e85d | 2016-11-23 14:11:14 +0000 | [diff] [blame] | 405 | /** |
| 406 | * drm_mm_for_each_node_in_range - iterator to walk over a range of |
| 407 | * allocated nodes |
Chris Wilson | 8b2fb7b | 2016-11-27 11:16:23 +0000 | [diff] [blame] | 408 | * @node__: drm_mm_node structure to assign to in each iteration step |
| 409 | * @mm__: drm_mm allocator to walk |
| 410 | * @start__: starting offset, the first node will overlap this |
| 411 | * @end__: ending offset, the last node will start before this (but may overlap) |
Chris Wilson | 522e85d | 2016-11-23 14:11:14 +0000 | [diff] [blame] | 412 | * |
| 413 | * This iterator walks over all nodes in the range allocator that lie |
| 414 | * between @start and @end. It is implemented similarly to list_for_each(), |
| 415 | * but using the internal interval tree to accelerate the search for the |
| 416 | * starting node, and so not safe against removal of elements. It assumes |
| 417 | * that @end is within (or is the upper limit of) the drm_mm allocator. |
| 418 | */ |
Chris Wilson | 8b2fb7b | 2016-11-27 11:16:23 +0000 | [diff] [blame] | 419 | #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__) \ |
| 420 | for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \ |
| 421 | node__ && node__->start < (end__); \ |
| 422 | node__ = list_next_entry(node__, node_list)) |
Chris Wilson | 202b52b | 2016-08-03 16:04:09 +0100 | [diff] [blame] | 423 | |
Chris Wilson | 9a71e27 | 2016-12-22 08:36:29 +0000 | [diff] [blame] | 424 | void drm_mm_scan_init_with_range(struct drm_mm_scan *scan, |
| 425 | struct drm_mm *mm, |
Chris Wilson | 0b04d47 | 2016-12-22 08:36:33 +0000 | [diff] [blame] | 426 | u64 size, u64 alignment, unsigned long color, |
| 427 | u64 start, u64 end, |
| 428 | unsigned int flags); |
Chris Wilson | 2c4b389 | 2016-12-22 08:36:31 +0000 | [diff] [blame] | 429 | |
| 430 | /** |
| 431 | * drm_mm_scan_init - initialize lru scanning |
| 432 | * @scan: scan state |
| 433 | * @mm: drm_mm to scan |
| 434 | * @size: size of the allocation |
| 435 | * @alignment: alignment of the allocation |
| 436 | * @color: opaque tag value to use for the allocation |
Chris Wilson | 0b04d47 | 2016-12-22 08:36:33 +0000 | [diff] [blame] | 437 | * @flags: flags to specify how the allocation will be performed afterwards |
Chris Wilson | 2c4b389 | 2016-12-22 08:36:31 +0000 | [diff] [blame] | 438 | * |
| 439 | * This simply sets up the scanning routines with the parameters for the desired |
Chris Wilson | 0b04d47 | 2016-12-22 08:36:33 +0000 | [diff] [blame] | 440 | * hole. |
Chris Wilson | 2c4b389 | 2016-12-22 08:36:31 +0000 | [diff] [blame] | 441 | * |
| 442 | * Warning: |
| 443 | * As long as the scan list is non-empty, no other operations than |
| 444 | * adding/removing nodes to/from the scan list are allowed. |
| 445 | */ |
| 446 | static inline void drm_mm_scan_init(struct drm_mm_scan *scan, |
| 447 | struct drm_mm *mm, |
| 448 | u64 size, |
| 449 | u64 alignment, |
Chris Wilson | 0b04d47 | 2016-12-22 08:36:33 +0000 | [diff] [blame] | 450 | unsigned long color, |
| 451 | unsigned int flags) |
Chris Wilson | 2c4b389 | 2016-12-22 08:36:31 +0000 | [diff] [blame] | 452 | { |
Chris Wilson | 0b04d47 | 2016-12-22 08:36:33 +0000 | [diff] [blame] | 453 | drm_mm_scan_init_with_range(scan, mm, |
| 454 | size, alignment, color, |
| 455 | 0, U64_MAX, |
| 456 | flags); |
Chris Wilson | 2c4b389 | 2016-12-22 08:36:31 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Chris Wilson | 9a71e27 | 2016-12-22 08:36:29 +0000 | [diff] [blame] | 459 | bool drm_mm_scan_add_block(struct drm_mm_scan *scan, |
| 460 | struct drm_mm_node *node); |
| 461 | bool drm_mm_scan_remove_block(struct drm_mm_scan *scan, |
| 462 | struct drm_mm_node *node); |
Chris Wilson | 3fa489d | 2016-12-22 08:36:36 +0000 | [diff] [blame] | 463 | struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan); |
Daniel Vetter | 709ea97 | 2010-07-02 15:02:16 +0100 | [diff] [blame] | 464 | |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 465 | void drm_mm_debug_table(const struct drm_mm *mm, const char *prefix); |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 466 | #ifdef CONFIG_DEBUG_FS |
Chris Wilson | 45b186f | 2016-12-16 07:46:42 +0000 | [diff] [blame] | 467 | int drm_mm_dump_table(struct seq_file *m, const struct drm_mm *mm); |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 468 | #endif |
| 469 | |
Jerome Glisse | 249d604 | 2009-04-08 17:11:16 +0200 | [diff] [blame] | 470 | #endif |