Commit | Line | Data |
---|---|---|
3a1bd924 TH |
1 | /************************************************************************** |
2 | * | |
3 | * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA. | |
4 | * All Rights Reserved. | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a | |
7 | * copy of this software and associated documentation files (the | |
8 | * "Software"), to deal in the Software without restriction, including | |
9 | * without limitation the rights to use, copy, modify, merge, publish, | |
10 | * distribute, sub license, and/or sell copies of the Software, and to | |
11 | * permit persons to whom the Software is furnished to do so, subject to | |
12 | * the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice (including the | |
15 | * next paragraph) shall be included in all copies or substantial portions | |
16 | * of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, | |
22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. | |
25 | * | |
26 | * | |
27 | **************************************************************************/ | |
28 | ||
29 | /* | |
30 | * Generic simple memory manager implementation. Intended to be used as a base | |
31 | * class implementation for more advanced memory managers. | |
32 | * | |
33 | * Note that the algorithm used is quite simple and there might be substantial | |
34 | * performance gains if a smarter free list is implemented. Currently it is just an | |
35 | * unordered stack of free regions. This could easily be improved if an RB-tree | |
36 | * is used instead. At least if we expect heavy fragmentation. | |
37 | * | |
38 | * Aligned allocations can also see improvement. | |
39 | * | |
40 | * Authors: | |
96de0e25 | 41 | * Thomas Hellström <thomas-at-tungstengraphics-dot-com> |
3a1bd924 TH |
42 | */ |
43 | ||
760285e7 DH |
44 | #include <drm/drmP.h> |
45 | #include <drm/drm_mm.h> | |
1d58420b | 46 | #include <linux/slab.h> |
fa8a1238 | 47 | #include <linux/seq_file.h> |
2d1a8a48 | 48 | #include <linux/export.h> |
202b52b7 | 49 | #include <linux/interval_tree_generic.h> |
1d58420b | 50 | |
93110be6 DV |
51 | /** |
52 | * DOC: Overview | |
53 | * | |
54 | * drm_mm provides a simple range allocator. The drivers are free to use the | |
55 | * resource allocator from the linux core if it suits them, the upside of drm_mm | |
56 | * is that it's in the DRM core. Which means that it's easier to extend for | |
57 | * some of the crazier special purpose needs of gpus. | |
58 | * | |
59 | * The main data struct is &drm_mm, allocations are tracked in &drm_mm_node. | |
60 | * Drivers are free to embed either of them into their own suitable | |
61 | * datastructures. drm_mm itself will not do any allocations of its own, so if | |
62 | * drivers choose not to embed nodes they need to still allocate them | |
63 | * themselves. | |
64 | * | |
65 | * The range allocator also supports reservation of preallocated blocks. This is | |
66 | * useful for taking over initial mode setting configurations from the firmware, | |
67 | * where an object needs to be created which exactly matches the firmware's | |
68 | * scanout target. As long as the range is still free it can be inserted anytime | |
69 | * after the allocator is initialized, which helps with avoiding looped | |
70 | * depencies in the driver load sequence. | |
71 | * | |
72 | * drm_mm maintains a stack of most recently freed holes, which of all | |
73 | * simplistic datastructures seems to be a fairly decent approach to clustering | |
74 | * allocations and avoiding too much fragmentation. This means free space | |
75 | * searches are O(num_holes). Given that all the fancy features drm_mm supports | |
76 | * something better would be fairly complex and since gfx thrashing is a fairly | |
77 | * steep cliff not a real concern. Removing a node again is O(1). | |
78 | * | |
79 | * drm_mm supports a few features: Alignment and range restrictions can be | |
80 | * supplied. Further more every &drm_mm_node has a color value (which is just an | |
81 | * opaqua unsigned long) which in conjunction with a driver callback can be used | |
82 | * to implement sophisticated placement restrictions. The i915 DRM driver uses | |
83 | * this to implement guard pages between incompatible caching domains in the | |
84 | * graphics TT. | |
85 | * | |
62347f9e LK |
86 | * Two behaviors are supported for searching and allocating: bottom-up and top-down. |
87 | * The default is bottom-up. Top-down allocation can be used if the memory area | |
88 | * has different restrictions, or just to reduce fragmentation. | |
89 | * | |
93110be6 DV |
90 | * Finally iteration helpers to walk all nodes and all holes are provided as are |
91 | * some basic allocator dumpers for debugging. | |
92 | */ | |
93 | ||
c700c67b | 94 | static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm, |
440fd528 | 95 | u64 size, |
c700c67b DH |
96 | unsigned alignment, |
97 | unsigned long color, | |
98 | enum drm_mm_search_flags flags); | |
99 | static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm, | |
440fd528 | 100 | u64 size, |
c700c67b DH |
101 | unsigned alignment, |
102 | unsigned long color, | |
440fd528 TR |
103 | u64 start, |
104 | u64 end, | |
c700c67b | 105 | enum drm_mm_search_flags flags); |
1d58420b | 106 | |
202b52b7 CW |
107 | #define START(node) ((node)->start) |
108 | #define LAST(node) ((node)->start + (node)->size - 1) | |
109 | ||
110 | INTERVAL_TREE_DEFINE(struct drm_mm_node, rb, | |
111 | u64, __subtree_last, | |
112 | START, LAST, static inline, drm_mm_interval_tree) | |
113 | ||
114 | struct drm_mm_node * | |
115 | drm_mm_interval_first(struct drm_mm *mm, u64 start, u64 last) | |
116 | { | |
117 | return drm_mm_interval_tree_iter_first(&mm->interval_tree, | |
118 | start, last); | |
119 | } | |
120 | EXPORT_SYMBOL(drm_mm_interval_first); | |
121 | ||
122 | struct drm_mm_node * | |
123 | drm_mm_interval_next(struct drm_mm_node *node, u64 start, u64 last) | |
124 | { | |
125 | return drm_mm_interval_tree_iter_next(node, start, last); | |
126 | } | |
127 | EXPORT_SYMBOL(drm_mm_interval_next); | |
128 | ||
129 | static void drm_mm_interval_tree_add_node(struct drm_mm_node *hole_node, | |
130 | struct drm_mm_node *node) | |
131 | { | |
132 | struct drm_mm *mm = hole_node->mm; | |
133 | struct rb_node **link, *rb; | |
134 | struct drm_mm_node *parent; | |
135 | ||
136 | node->__subtree_last = LAST(node); | |
137 | ||
138 | if (hole_node->allocated) { | |
139 | rb = &hole_node->rb; | |
140 | while (rb) { | |
141 | parent = rb_entry(rb, struct drm_mm_node, rb); | |
142 | if (parent->__subtree_last >= node->__subtree_last) | |
143 | break; | |
144 | ||
145 | parent->__subtree_last = node->__subtree_last; | |
146 | rb = rb_parent(rb); | |
147 | } | |
148 | ||
149 | rb = &hole_node->rb; | |
150 | link = &hole_node->rb.rb_right; | |
151 | } else { | |
152 | rb = NULL; | |
153 | link = &mm->interval_tree.rb_node; | |
154 | } | |
155 | ||
156 | while (*link) { | |
157 | rb = *link; | |
158 | parent = rb_entry(rb, struct drm_mm_node, rb); | |
159 | if (parent->__subtree_last < node->__subtree_last) | |
160 | parent->__subtree_last = node->__subtree_last; | |
161 | if (node->start < parent->start) | |
162 | link = &parent->rb.rb_left; | |
163 | else | |
164 | link = &parent->rb.rb_right; | |
165 | } | |
166 | ||
167 | rb_link_node(&node->rb, rb, link); | |
168 | rb_insert_augmented(&node->rb, | |
169 | &mm->interval_tree, | |
170 | &drm_mm_interval_tree_augment); | |
171 | } | |
172 | ||
9fc935de DV |
173 | static void drm_mm_insert_helper(struct drm_mm_node *hole_node, |
174 | struct drm_mm_node *node, | |
440fd528 | 175 | u64 size, unsigned alignment, |
62347f9e LK |
176 | unsigned long color, |
177 | enum drm_mm_allocator_flags flags) | |
3a1bd924 | 178 | { |
ea7b1dd4 | 179 | struct drm_mm *mm = hole_node->mm; |
440fd528 TR |
180 | u64 hole_start = drm_mm_hole_node_start(hole_node); |
181 | u64 hole_end = drm_mm_hole_node_end(hole_node); | |
182 | u64 adj_start = hole_start; | |
183 | u64 adj_end = hole_end; | |
ea7b1dd4 | 184 | |
9e8944ab | 185 | BUG_ON(node->allocated); |
b0b7af18 | 186 | |
6b9d89b4 CW |
187 | if (mm->color_adjust) |
188 | mm->color_adjust(hole_node, color, &adj_start, &adj_end); | |
1d58420b | 189 | |
62347f9e LK |
190 | if (flags & DRM_MM_CREATE_TOP) |
191 | adj_start = adj_end - size; | |
192 | ||
6b9d89b4 | 193 | if (alignment) { |
440fd528 TR |
194 | u64 tmp = adj_start; |
195 | unsigned rem; | |
196 | ||
197 | rem = do_div(tmp, alignment); | |
198 | if (rem) { | |
62347f9e | 199 | if (flags & DRM_MM_CREATE_TOP) |
440fd528 | 200 | adj_start -= rem; |
62347f9e | 201 | else |
440fd528 | 202 | adj_start += alignment - rem; |
62347f9e | 203 | } |
6b9d89b4 CW |
204 | } |
205 | ||
62347f9e LK |
206 | BUG_ON(adj_start < hole_start); |
207 | BUG_ON(adj_end > hole_end); | |
208 | ||
6b9d89b4 | 209 | if (adj_start == hole_start) { |
ea7b1dd4 | 210 | hole_node->hole_follows = 0; |
6b9d89b4 CW |
211 | list_del(&hole_node->hole_stack); |
212 | } | |
ea7b1dd4 | 213 | |
6b9d89b4 | 214 | node->start = adj_start; |
ea7b1dd4 DV |
215 | node->size = size; |
216 | node->mm = mm; | |
6b9d89b4 | 217 | node->color = color; |
b0b7af18 | 218 | node->allocated = 1; |
3a1bd924 | 219 | |
ea7b1dd4 DV |
220 | INIT_LIST_HEAD(&node->hole_stack); |
221 | list_add(&node->node_list, &hole_node->node_list); | |
222 | ||
202b52b7 CW |
223 | drm_mm_interval_tree_add_node(hole_node, node); |
224 | ||
6b9d89b4 | 225 | BUG_ON(node->start + node->size > adj_end); |
ea7b1dd4 | 226 | |
6b9d89b4 | 227 | node->hole_follows = 0; |
9e8944ab | 228 | if (__drm_mm_hole_node_start(node) < hole_end) { |
ea7b1dd4 DV |
229 | list_add(&node->hole_stack, &mm->hole_stack); |
230 | node->hole_follows = 1; | |
1d58420b | 231 | } |
9fc935de DV |
232 | } |
233 | ||
e18c0412 DV |
234 | /** |
235 | * drm_mm_reserve_node - insert an pre-initialized node | |
236 | * @mm: drm_mm allocator to insert @node into | |
237 | * @node: drm_mm_node to insert | |
238 | * | |
239 | * This functions inserts an already set-up drm_mm_node into the allocator, | |
240 | * meaning that start, size and color must be set by the caller. This is useful | |
241 | * to initialize the allocator with preallocated objects which must be set-up | |
242 | * before the range allocator can be set-up, e.g. when taking over a firmware | |
243 | * framebuffer. | |
244 | * | |
245 | * Returns: | |
246 | * 0 on success, -ENOSPC if there's no hole where @node is. | |
247 | */ | |
338710e7 | 248 | int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node) |
5973c7ee | 249 | { |
202b52b7 | 250 | u64 end = node->start + node->size; |
b3a070cc | 251 | struct drm_mm_node *hole; |
202b52b7 | 252 | u64 hole_start, hole_end; |
338710e7 | 253 | |
b80d3942 HS |
254 | end = node->start + node->size; |
255 | ||
338710e7 | 256 | /* Find the relevant hole to add our node to */ |
202b52b7 CW |
257 | hole = drm_mm_interval_tree_iter_first(&mm->interval_tree, |
258 | node->start, ~(u64)0); | |
259 | if (hole) { | |
260 | if (hole->start < end) | |
261 | return -ENOSPC; | |
262 | } else { | |
263 | hole = list_entry(&mm->head_node.node_list, | |
264 | typeof(*hole), node_list); | |
265 | } | |
5973c7ee | 266 | |
202b52b7 CW |
267 | hole = list_last_entry(&hole->node_list, typeof(*hole), node_list); |
268 | if (!hole->hole_follows) | |
269 | return -ENOSPC; | |
5973c7ee | 270 | |
202b52b7 CW |
271 | hole_start = __drm_mm_hole_node_start(hole); |
272 | hole_end = __drm_mm_hole_node_end(hole); | |
273 | if (hole_start > node->start || hole_end < end) | |
274 | return -ENOSPC; | |
5973c7ee | 275 | |
202b52b7 CW |
276 | node->mm = mm; |
277 | node->allocated = 1; | |
5973c7ee | 278 | |
202b52b7 CW |
279 | INIT_LIST_HEAD(&node->hole_stack); |
280 | list_add(&node->node_list, &hole->node_list); | |
5973c7ee | 281 | |
202b52b7 CW |
282 | drm_mm_interval_tree_add_node(hole, node); |
283 | ||
284 | if (node->start == hole_start) { | |
285 | hole->hole_follows = 0; | |
286 | list_del_init(&hole->hole_stack); | |
287 | } | |
288 | ||
289 | node->hole_follows = 0; | |
290 | if (end != hole_end) { | |
291 | list_add(&node->hole_stack, &mm->hole_stack); | |
292 | node->hole_follows = 1; | |
5973c7ee CW |
293 | } |
294 | ||
202b52b7 | 295 | return 0; |
5973c7ee | 296 | } |
338710e7 | 297 | EXPORT_SYMBOL(drm_mm_reserve_node); |
5973c7ee | 298 | |
b0b7af18 | 299 | /** |
e18c0412 DV |
300 | * drm_mm_insert_node_generic - search for space and insert @node |
301 | * @mm: drm_mm to allocate from | |
302 | * @node: preallocate node to insert | |
303 | * @size: size of the allocation | |
304 | * @alignment: alignment of the allocation | |
305 | * @color: opaque tag value to use for this node | |
62347f9e LK |
306 | * @sflags: flags to fine-tune the allocation search |
307 | * @aflags: flags to fine-tune the allocation behavior | |
e18c0412 DV |
308 | * |
309 | * The preallocated node must be cleared to 0. | |
310 | * | |
311 | * Returns: | |
312 | * 0 on success, -ENOSPC if there's no suitable hole. | |
b0b7af18 | 313 | */ |
b8103450 | 314 | int drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node, |
440fd528 | 315 | u64 size, unsigned alignment, |
31e5d7c6 | 316 | unsigned long color, |
62347f9e LK |
317 | enum drm_mm_search_flags sflags, |
318 | enum drm_mm_allocator_flags aflags) | |
b0b7af18 DV |
319 | { |
320 | struct drm_mm_node *hole_node; | |
321 | ||
b8103450 | 322 | hole_node = drm_mm_search_free_generic(mm, size, alignment, |
62347f9e | 323 | color, sflags); |
b0b7af18 DV |
324 | if (!hole_node) |
325 | return -ENOSPC; | |
326 | ||
62347f9e | 327 | drm_mm_insert_helper(hole_node, node, size, alignment, color, aflags); |
b0b7af18 DV |
328 | return 0; |
329 | } | |
b8103450 CW |
330 | EXPORT_SYMBOL(drm_mm_insert_node_generic); |
331 | ||
9fc935de DV |
332 | static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node, |
333 | struct drm_mm_node *node, | |
440fd528 | 334 | u64 size, unsigned alignment, |
6b9d89b4 | 335 | unsigned long color, |
440fd528 | 336 | u64 start, u64 end, |
62347f9e | 337 | enum drm_mm_allocator_flags flags) |
a2e68e92 | 338 | { |
ea7b1dd4 | 339 | struct drm_mm *mm = hole_node->mm; |
440fd528 TR |
340 | u64 hole_start = drm_mm_hole_node_start(hole_node); |
341 | u64 hole_end = drm_mm_hole_node_end(hole_node); | |
342 | u64 adj_start = hole_start; | |
343 | u64 adj_end = hole_end; | |
a2e68e92 | 344 | |
b0b7af18 DV |
345 | BUG_ON(!hole_node->hole_follows || node->allocated); |
346 | ||
6b9d89b4 CW |
347 | if (adj_start < start) |
348 | adj_start = start; | |
901593f2 CW |
349 | if (adj_end > end) |
350 | adj_end = end; | |
351 | ||
352 | if (mm->color_adjust) | |
353 | mm->color_adjust(hole_node, color, &adj_start, &adj_end); | |
6b9d89b4 | 354 | |
fafecc01 MT |
355 | if (flags & DRM_MM_CREATE_TOP) |
356 | adj_start = adj_end - size; | |
357 | ||
6b9d89b4 | 358 | if (alignment) { |
440fd528 TR |
359 | u64 tmp = adj_start; |
360 | unsigned rem; | |
361 | ||
362 | rem = do_div(tmp, alignment); | |
363 | if (rem) { | |
62347f9e | 364 | if (flags & DRM_MM_CREATE_TOP) |
440fd528 | 365 | adj_start -= rem; |
62347f9e | 366 | else |
440fd528 | 367 | adj_start += alignment - rem; |
62347f9e | 368 | } |
6b9d89b4 | 369 | } |
ea7b1dd4 | 370 | |
6b9d89b4 | 371 | if (adj_start == hole_start) { |
ea7b1dd4 | 372 | hole_node->hole_follows = 0; |
6b9d89b4 | 373 | list_del(&hole_node->hole_stack); |
a2e68e92 JG |
374 | } |
375 | ||
6b9d89b4 | 376 | node->start = adj_start; |
ea7b1dd4 DV |
377 | node->size = size; |
378 | node->mm = mm; | |
6b9d89b4 | 379 | node->color = color; |
b0b7af18 | 380 | node->allocated = 1; |
ea7b1dd4 DV |
381 | |
382 | INIT_LIST_HEAD(&node->hole_stack); | |
383 | list_add(&node->node_list, &hole_node->node_list); | |
384 | ||
202b52b7 CW |
385 | drm_mm_interval_tree_add_node(hole_node, node); |
386 | ||
62347f9e LK |
387 | BUG_ON(node->start < start); |
388 | BUG_ON(node->start < adj_start); | |
6b9d89b4 | 389 | BUG_ON(node->start + node->size > adj_end); |
ea7b1dd4 DV |
390 | BUG_ON(node->start + node->size > end); |
391 | ||
6b9d89b4 | 392 | node->hole_follows = 0; |
9e8944ab | 393 | if (__drm_mm_hole_node_start(node) < hole_end) { |
ea7b1dd4 DV |
394 | list_add(&node->hole_stack, &mm->hole_stack); |
395 | node->hole_follows = 1; | |
a2e68e92 | 396 | } |
9fc935de DV |
397 | } |
398 | ||
b0b7af18 | 399 | /** |
e18c0412 DV |
400 | * drm_mm_insert_node_in_range_generic - ranged search for space and insert @node |
401 | * @mm: drm_mm to allocate from | |
402 | * @node: preallocate node to insert | |
403 | * @size: size of the allocation | |
404 | * @alignment: alignment of the allocation | |
405 | * @color: opaque tag value to use for this node | |
406 | * @start: start of the allowed range for this node | |
407 | * @end: end of the allowed range for this node | |
62347f9e LK |
408 | * @sflags: flags to fine-tune the allocation search |
409 | * @aflags: flags to fine-tune the allocation behavior | |
e18c0412 DV |
410 | * |
411 | * The preallocated node must be cleared to 0. | |
412 | * | |
413 | * Returns: | |
414 | * 0 on success, -ENOSPC if there's no suitable hole. | |
3a1bd924 | 415 | */ |
b8103450 | 416 | int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node, |
440fd528 | 417 | u64 size, unsigned alignment, |
62347f9e | 418 | unsigned long color, |
440fd528 | 419 | u64 start, u64 end, |
62347f9e LK |
420 | enum drm_mm_search_flags sflags, |
421 | enum drm_mm_allocator_flags aflags) | |
3a1bd924 | 422 | { |
b0b7af18 DV |
423 | struct drm_mm_node *hole_node; |
424 | ||
b8103450 CW |
425 | hole_node = drm_mm_search_free_in_range_generic(mm, |
426 | size, alignment, color, | |
62347f9e | 427 | start, end, sflags); |
b0b7af18 DV |
428 | if (!hole_node) |
429 | return -ENOSPC; | |
430 | ||
b8103450 CW |
431 | drm_mm_insert_helper_range(hole_node, node, |
432 | size, alignment, color, | |
62347f9e | 433 | start, end, aflags); |
b0b7af18 DV |
434 | return 0; |
435 | } | |
b8103450 CW |
436 | EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic); |
437 | ||
b0b7af18 | 438 | /** |
e18c0412 DV |
439 | * drm_mm_remove_node - Remove a memory node from the allocator. |
440 | * @node: drm_mm_node to remove | |
441 | * | |
442 | * This just removes a node from its drm_mm allocator. The node does not need to | |
443 | * be cleared again before it can be re-inserted into this or any other drm_mm | |
444 | * allocator. It is a bug to call this function on a un-allocated node. | |
b0b7af18 DV |
445 | */ |
446 | void drm_mm_remove_node(struct drm_mm_node *node) | |
447 | { | |
ea7b1dd4 DV |
448 | struct drm_mm *mm = node->mm; |
449 | struct drm_mm_node *prev_node; | |
3a1bd924 | 450 | |
3ef80a81 BW |
451 | if (WARN_ON(!node->allocated)) |
452 | return; | |
453 | ||
ea7b1dd4 DV |
454 | BUG_ON(node->scanned_block || node->scanned_prev_free |
455 | || node->scanned_next_free); | |
3a1bd924 | 456 | |
ea7b1dd4 DV |
457 | prev_node = |
458 | list_entry(node->node_list.prev, struct drm_mm_node, node_list); | |
709ea971 | 459 | |
ea7b1dd4 | 460 | if (node->hole_follows) { |
9e8944ab CW |
461 | BUG_ON(__drm_mm_hole_node_start(node) == |
462 | __drm_mm_hole_node_end(node)); | |
ea7b1dd4 DV |
463 | list_del(&node->hole_stack); |
464 | } else | |
9e8944ab CW |
465 | BUG_ON(__drm_mm_hole_node_start(node) != |
466 | __drm_mm_hole_node_end(node)); | |
467 | ||
249d6048 | 468 | |
ea7b1dd4 DV |
469 | if (!prev_node->hole_follows) { |
470 | prev_node->hole_follows = 1; | |
471 | list_add(&prev_node->hole_stack, &mm->hole_stack); | |
472 | } else | |
473 | list_move(&prev_node->hole_stack, &mm->hole_stack); | |
474 | ||
202b52b7 | 475 | drm_mm_interval_tree_remove(node, &mm->interval_tree); |
ea7b1dd4 | 476 | list_del(&node->node_list); |
b0b7af18 DV |
477 | node->allocated = 0; |
478 | } | |
479 | EXPORT_SYMBOL(drm_mm_remove_node); | |
480 | ||
440fd528 | 481 | static int check_free_hole(u64 start, u64 end, u64 size, unsigned alignment) |
7a6b2896 | 482 | { |
75214733 | 483 | if (end - start < size) |
7a6b2896 DV |
484 | return 0; |
485 | ||
486 | if (alignment) { | |
440fd528 TR |
487 | u64 tmp = start; |
488 | unsigned rem; | |
489 | ||
490 | rem = do_div(tmp, alignment); | |
046d669c | 491 | if (rem) |
440fd528 | 492 | start += alignment - rem; |
7a6b2896 DV |
493 | } |
494 | ||
6b9d89b4 | 495 | return end >= start + size; |
7a6b2896 DV |
496 | } |
497 | ||
c700c67b | 498 | static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm, |
440fd528 | 499 | u64 size, |
c700c67b DH |
500 | unsigned alignment, |
501 | unsigned long color, | |
502 | enum drm_mm_search_flags flags) | |
3a1bd924 | 503 | { |
55910517 DA |
504 | struct drm_mm_node *entry; |
505 | struct drm_mm_node *best; | |
440fd528 TR |
506 | u64 adj_start; |
507 | u64 adj_end; | |
508 | u64 best_size; | |
3a1bd924 | 509 | |
709ea971 DV |
510 | BUG_ON(mm->scanned_blocks); |
511 | ||
3a1bd924 TH |
512 | best = NULL; |
513 | best_size = ~0UL; | |
514 | ||
62347f9e LK |
515 | __drm_mm_for_each_hole(entry, mm, adj_start, adj_end, |
516 | flags & DRM_MM_SEARCH_BELOW) { | |
440fd528 | 517 | u64 hole_size = adj_end - adj_start; |
145bccd2 | 518 | |
6b9d89b4 CW |
519 | if (mm->color_adjust) { |
520 | mm->color_adjust(entry, color, &adj_start, &adj_end); | |
521 | if (adj_end <= adj_start) | |
522 | continue; | |
523 | } | |
524 | ||
6b9d89b4 | 525 | if (!check_free_hole(adj_start, adj_end, size, alignment)) |
1d58420b TH |
526 | continue; |
527 | ||
31e5d7c6 | 528 | if (!(flags & DRM_MM_SEARCH_BEST)) |
7a6b2896 | 529 | return entry; |
1d58420b | 530 | |
145bccd2 | 531 | if (hole_size < best_size) { |
7a6b2896 | 532 | best = entry; |
145bccd2 | 533 | best_size = hole_size; |
3a1bd924 TH |
534 | } |
535 | } | |
536 | ||
537 | return best; | |
538 | } | |
6b9d89b4 | 539 | |
c700c67b | 540 | static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm, |
440fd528 | 541 | u64 size, |
6b9d89b4 CW |
542 | unsigned alignment, |
543 | unsigned long color, | |
440fd528 TR |
544 | u64 start, |
545 | u64 end, | |
31e5d7c6 | 546 | enum drm_mm_search_flags flags) |
a2e68e92 | 547 | { |
a2e68e92 JG |
548 | struct drm_mm_node *entry; |
549 | struct drm_mm_node *best; | |
440fd528 TR |
550 | u64 adj_start; |
551 | u64 adj_end; | |
552 | u64 best_size; | |
a2e68e92 | 553 | |
709ea971 DV |
554 | BUG_ON(mm->scanned_blocks); |
555 | ||
a2e68e92 JG |
556 | best = NULL; |
557 | best_size = ~0UL; | |
558 | ||
62347f9e LK |
559 | __drm_mm_for_each_hole(entry, mm, adj_start, adj_end, |
560 | flags & DRM_MM_SEARCH_BELOW) { | |
440fd528 | 561 | u64 hole_size = adj_end - adj_start; |
145bccd2 | 562 | |
9e8944ab CW |
563 | if (adj_start < start) |
564 | adj_start = start; | |
565 | if (adj_end > end) | |
566 | adj_end = end; | |
6b9d89b4 CW |
567 | |
568 | if (mm->color_adjust) { | |
569 | mm->color_adjust(entry, color, &adj_start, &adj_end); | |
570 | if (adj_end <= adj_start) | |
571 | continue; | |
572 | } | |
573 | ||
75214733 | 574 | if (!check_free_hole(adj_start, adj_end, size, alignment)) |
a2e68e92 JG |
575 | continue; |
576 | ||
31e5d7c6 | 577 | if (!(flags & DRM_MM_SEARCH_BEST)) |
7a6b2896 | 578 | return entry; |
a2e68e92 | 579 | |
145bccd2 | 580 | if (hole_size < best_size) { |
7a6b2896 | 581 | best = entry; |
145bccd2 | 582 | best_size = hole_size; |
a2e68e92 JG |
583 | } |
584 | } | |
585 | ||
586 | return best; | |
587 | } | |
a2e68e92 | 588 | |
b0b7af18 | 589 | /** |
e18c0412 DV |
590 | * drm_mm_replace_node - move an allocation from @old to @new |
591 | * @old: drm_mm_node to remove from the allocator | |
592 | * @new: drm_mm_node which should inherit @old's allocation | |
593 | * | |
594 | * This is useful for when drivers embed the drm_mm_node structure and hence | |
595 | * can't move allocations by reassigning pointers. It's a combination of remove | |
596 | * and insert with the guarantee that the allocation start will match. | |
b0b7af18 DV |
597 | */ |
598 | void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new) | |
599 | { | |
600 | list_replace(&old->node_list, &new->node_list); | |
2bbd4492 | 601 | list_replace(&old->hole_stack, &new->hole_stack); |
202b52b7 | 602 | rb_replace_node(&old->rb, &new->rb, &old->mm->interval_tree); |
b0b7af18 DV |
603 | new->hole_follows = old->hole_follows; |
604 | new->mm = old->mm; | |
605 | new->start = old->start; | |
606 | new->size = old->size; | |
6b9d89b4 | 607 | new->color = old->color; |
202b52b7 | 608 | new->__subtree_last = old->__subtree_last; |
b0b7af18 DV |
609 | |
610 | old->allocated = 0; | |
611 | new->allocated = 1; | |
612 | } | |
613 | EXPORT_SYMBOL(drm_mm_replace_node); | |
614 | ||
93110be6 DV |
615 | /** |
616 | * DOC: lru scan roaster | |
617 | * | |
618 | * Very often GPUs need to have continuous allocations for a given object. When | |
619 | * evicting objects to make space for a new one it is therefore not most | |
620 | * efficient when we simply start to select all objects from the tail of an LRU | |
621 | * until there's a suitable hole: Especially for big objects or nodes that | |
622 | * otherwise have special allocation constraints there's a good chance we evict | |
623 | * lots of (smaller) objects unecessarily. | |
624 | * | |
625 | * The DRM range allocator supports this use-case through the scanning | |
626 | * interfaces. First a scan operation needs to be initialized with | |
627 | * drm_mm_init_scan() or drm_mm_init_scan_with_range(). The the driver adds | |
628 | * objects to the roaster (probably by walking an LRU list, but this can be | |
629 | * freely implemented) until a suitable hole is found or there's no further | |
630 | * evitable object. | |
631 | * | |
632 | * The the driver must walk through all objects again in exactly the reverse | |
633 | * order to restore the allocator state. Note that while the allocator is used | |
634 | * in the scan mode no other operation is allowed. | |
635 | * | |
636 | * Finally the driver evicts all objects selected in the scan. Adding and | |
637 | * removing an object is O(1), and since freeing a node is also O(1) the overall | |
638 | * complexity is O(scanned_objects). So like the free stack which needs to be | |
639 | * walked before a scan operation even begins this is linear in the number of | |
640 | * objects. It doesn't seem to hurt badly. | |
641 | */ | |
642 | ||
709ea971 | 643 | /** |
e18c0412 DV |
644 | * drm_mm_init_scan - initialize lru scanning |
645 | * @mm: drm_mm to scan | |
646 | * @size: size of the allocation | |
647 | * @alignment: alignment of the allocation | |
648 | * @color: opaque tag value to use for the allocation | |
709ea971 DV |
649 | * |
650 | * This simply sets up the scanning routines with the parameters for the desired | |
e18c0412 DV |
651 | * hole. Note that there's no need to specify allocation flags, since they only |
652 | * change the place a node is allocated from within a suitable hole. | |
709ea971 | 653 | * |
e18c0412 DV |
654 | * Warning: |
655 | * As long as the scan list is non-empty, no other operations than | |
709ea971 DV |
656 | * adding/removing nodes to/from the scan list are allowed. |
657 | */ | |
6b9d89b4 | 658 | void drm_mm_init_scan(struct drm_mm *mm, |
440fd528 | 659 | u64 size, |
6b9d89b4 CW |
660 | unsigned alignment, |
661 | unsigned long color) | |
709ea971 | 662 | { |
6b9d89b4 | 663 | mm->scan_color = color; |
709ea971 DV |
664 | mm->scan_alignment = alignment; |
665 | mm->scan_size = size; | |
666 | mm->scanned_blocks = 0; | |
667 | mm->scan_hit_start = 0; | |
901593f2 | 668 | mm->scan_hit_end = 0; |
d935cc61 | 669 | mm->scan_check_range = 0; |
ae0cec28 | 670 | mm->prev_scanned_node = NULL; |
709ea971 DV |
671 | } |
672 | EXPORT_SYMBOL(drm_mm_init_scan); | |
673 | ||
d935cc61 | 674 | /** |
e18c0412 DV |
675 | * drm_mm_init_scan - initialize range-restricted lru scanning |
676 | * @mm: drm_mm to scan | |
677 | * @size: size of the allocation | |
678 | * @alignment: alignment of the allocation | |
679 | * @color: opaque tag value to use for the allocation | |
680 | * @start: start of the allowed range for the allocation | |
681 | * @end: end of the allowed range for the allocation | |
d935cc61 DV |
682 | * |
683 | * This simply sets up the scanning routines with the parameters for the desired | |
e18c0412 DV |
684 | * hole. Note that there's no need to specify allocation flags, since they only |
685 | * change the place a node is allocated from within a suitable hole. | |
d935cc61 | 686 | * |
e18c0412 DV |
687 | * Warning: |
688 | * As long as the scan list is non-empty, no other operations than | |
d935cc61 DV |
689 | * adding/removing nodes to/from the scan list are allowed. |
690 | */ | |
6b9d89b4 | 691 | void drm_mm_init_scan_with_range(struct drm_mm *mm, |
440fd528 | 692 | u64 size, |
d935cc61 | 693 | unsigned alignment, |
6b9d89b4 | 694 | unsigned long color, |
440fd528 TR |
695 | u64 start, |
696 | u64 end) | |
d935cc61 | 697 | { |
6b9d89b4 | 698 | mm->scan_color = color; |
d935cc61 DV |
699 | mm->scan_alignment = alignment; |
700 | mm->scan_size = size; | |
701 | mm->scanned_blocks = 0; | |
702 | mm->scan_hit_start = 0; | |
901593f2 | 703 | mm->scan_hit_end = 0; |
d935cc61 DV |
704 | mm->scan_start = start; |
705 | mm->scan_end = end; | |
706 | mm->scan_check_range = 1; | |
ae0cec28 | 707 | mm->prev_scanned_node = NULL; |
d935cc61 DV |
708 | } |
709 | EXPORT_SYMBOL(drm_mm_init_scan_with_range); | |
710 | ||
709ea971 | 711 | /** |
e18c0412 DV |
712 | * drm_mm_scan_add_block - add a node to the scan list |
713 | * @node: drm_mm_node to add | |
714 | * | |
709ea971 DV |
715 | * Add a node to the scan list that might be freed to make space for the desired |
716 | * hole. | |
717 | * | |
e18c0412 DV |
718 | * Returns: |
719 | * True if a hole has been found, false otherwise. | |
709ea971 | 720 | */ |
e18c0412 | 721 | bool drm_mm_scan_add_block(struct drm_mm_node *node) |
709ea971 DV |
722 | { |
723 | struct drm_mm *mm = node->mm; | |
ea7b1dd4 | 724 | struct drm_mm_node *prev_node; |
440fd528 TR |
725 | u64 hole_start, hole_end; |
726 | u64 adj_start, adj_end; | |
709ea971 DV |
727 | |
728 | mm->scanned_blocks++; | |
729 | ||
ea7b1dd4 | 730 | BUG_ON(node->scanned_block); |
709ea971 | 731 | node->scanned_block = 1; |
709ea971 | 732 | |
ea7b1dd4 DV |
733 | prev_node = list_entry(node->node_list.prev, struct drm_mm_node, |
734 | node_list); | |
709ea971 | 735 | |
ea7b1dd4 DV |
736 | node->scanned_preceeds_hole = prev_node->hole_follows; |
737 | prev_node->hole_follows = 1; | |
738 | list_del(&node->node_list); | |
739 | node->node_list.prev = &prev_node->node_list; | |
ae0cec28 DV |
740 | node->node_list.next = &mm->prev_scanned_node->node_list; |
741 | mm->prev_scanned_node = node; | |
709ea971 | 742 | |
901593f2 CW |
743 | adj_start = hole_start = drm_mm_hole_node_start(prev_node); |
744 | adj_end = hole_end = drm_mm_hole_node_end(prev_node); | |
6b9d89b4 | 745 | |
d935cc61 | 746 | if (mm->scan_check_range) { |
6b9d89b4 CW |
747 | if (adj_start < mm->scan_start) |
748 | adj_start = mm->scan_start; | |
749 | if (adj_end > mm->scan_end) | |
750 | adj_end = mm->scan_end; | |
d935cc61 DV |
751 | } |
752 | ||
901593f2 CW |
753 | if (mm->color_adjust) |
754 | mm->color_adjust(prev_node, mm->scan_color, | |
755 | &adj_start, &adj_end); | |
756 | ||
6b9d89b4 | 757 | if (check_free_hole(adj_start, adj_end, |
75214733 | 758 | mm->scan_size, mm->scan_alignment)) { |
ea7b1dd4 | 759 | mm->scan_hit_start = hole_start; |
901593f2 | 760 | mm->scan_hit_end = hole_end; |
e18c0412 | 761 | return true; |
709ea971 DV |
762 | } |
763 | ||
e18c0412 | 764 | return false; |
709ea971 DV |
765 | } |
766 | EXPORT_SYMBOL(drm_mm_scan_add_block); | |
767 | ||
768 | /** | |
e18c0412 DV |
769 | * drm_mm_scan_remove_block - remove a node from the scan list |
770 | * @node: drm_mm_node to remove | |
709ea971 DV |
771 | * |
772 | * Nodes _must_ be removed in the exact same order from the scan list as they | |
773 | * have been added, otherwise the internal state of the memory manager will be | |
774 | * corrupted. | |
775 | * | |
776 | * When the scan list is empty, the selected memory nodes can be freed. An | |
31e5d7c6 DH |
777 | * immediately following drm_mm_search_free with !DRM_MM_SEARCH_BEST will then |
778 | * return the just freed block (because its at the top of the free_stack list). | |
709ea971 | 779 | * |
e18c0412 DV |
780 | * Returns: |
781 | * True if this block should be evicted, false otherwise. Will always | |
782 | * return false when no hole has been found. | |
709ea971 | 783 | */ |
e18c0412 | 784 | bool drm_mm_scan_remove_block(struct drm_mm_node *node) |
709ea971 DV |
785 | { |
786 | struct drm_mm *mm = node->mm; | |
ea7b1dd4 | 787 | struct drm_mm_node *prev_node; |
709ea971 DV |
788 | |
789 | mm->scanned_blocks--; | |
790 | ||
791 | BUG_ON(!node->scanned_block); | |
792 | node->scanned_block = 0; | |
709ea971 | 793 | |
ea7b1dd4 DV |
794 | prev_node = list_entry(node->node_list.prev, struct drm_mm_node, |
795 | node_list); | |
709ea971 | 796 | |
ea7b1dd4 | 797 | prev_node->hole_follows = node->scanned_preceeds_hole; |
ea7b1dd4 | 798 | list_add(&node->node_list, &prev_node->node_list); |
709ea971 | 799 | |
901593f2 CW |
800 | return (drm_mm_hole_node_end(node) > mm->scan_hit_start && |
801 | node->start < mm->scan_hit_end); | |
709ea971 DV |
802 | } |
803 | EXPORT_SYMBOL(drm_mm_scan_remove_block); | |
804 | ||
e18c0412 DV |
805 | /** |
806 | * drm_mm_clean - checks whether an allocator is clean | |
807 | * @mm: drm_mm allocator to check | |
808 | * | |
809 | * Returns: | |
810 | * True if the allocator is completely free, false if there's still a node | |
811 | * allocated in it. | |
812 | */ | |
813 | bool drm_mm_clean(struct drm_mm * mm) | |
3a1bd924 | 814 | { |
ea7b1dd4 | 815 | struct list_head *head = &mm->head_node.node_list; |
3a1bd924 | 816 | |
1d58420b TH |
817 | return (head->next->next == head); |
818 | } | |
249d6048 | 819 | EXPORT_SYMBOL(drm_mm_clean); |
3a1bd924 | 820 | |
e18c0412 DV |
821 | /** |
822 | * drm_mm_init - initialize a drm-mm allocator | |
823 | * @mm: the drm_mm structure to initialize | |
824 | * @start: start of the range managed by @mm | |
825 | * @size: end of the range managed by @mm | |
826 | * | |
827 | * Note that @mm must be cleared to 0 before calling this function. | |
828 | */ | |
440fd528 | 829 | void drm_mm_init(struct drm_mm * mm, u64 start, u64 size) |
1d58420b | 830 | { |
ea7b1dd4 | 831 | INIT_LIST_HEAD(&mm->hole_stack); |
709ea971 | 832 | mm->scanned_blocks = 0; |
3a1bd924 | 833 | |
ea7b1dd4 DV |
834 | /* Clever trick to avoid a special case in the free hole tracking. */ |
835 | INIT_LIST_HEAD(&mm->head_node.node_list); | |
836 | INIT_LIST_HEAD(&mm->head_node.hole_stack); | |
837 | mm->head_node.hole_follows = 1; | |
838 | mm->head_node.scanned_block = 0; | |
839 | mm->head_node.scanned_prev_free = 0; | |
840 | mm->head_node.scanned_next_free = 0; | |
841 | mm->head_node.mm = mm; | |
842 | mm->head_node.start = start + size; | |
843 | mm->head_node.size = start - mm->head_node.start; | |
844 | list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack); | |
845 | ||
202b52b7 CW |
846 | mm->interval_tree = RB_ROOT; |
847 | ||
6b9d89b4 | 848 | mm->color_adjust = NULL; |
3a1bd924 | 849 | } |
673a394b | 850 | EXPORT_SYMBOL(drm_mm_init); |
3a1bd924 | 851 | |
e18c0412 DV |
852 | /** |
853 | * drm_mm_takedown - clean up a drm_mm allocator | |
854 | * @mm: drm_mm allocator to clean up | |
855 | * | |
856 | * Note that it is a bug to call this function on an allocator which is not | |
857 | * clean. | |
858 | */ | |
55910517 | 859 | void drm_mm_takedown(struct drm_mm * mm) |
3a1bd924 | 860 | { |
c700c67b DH |
861 | WARN(!list_empty(&mm->head_node.node_list), |
862 | "Memory manager not clean during takedown.\n"); | |
3a1bd924 | 863 | } |
f453ba04 | 864 | EXPORT_SYMBOL(drm_mm_takedown); |
fa8a1238 | 865 | |
440fd528 TR |
866 | static u64 drm_mm_debug_hole(struct drm_mm_node *entry, |
867 | const char *prefix) | |
99d7e48e | 868 | { |
440fd528 | 869 | u64 hole_start, hole_end, hole_size; |
ea7b1dd4 | 870 | |
2c54b133 DV |
871 | if (entry->hole_follows) { |
872 | hole_start = drm_mm_hole_node_start(entry); | |
873 | hole_end = drm_mm_hole_node_end(entry); | |
874 | hole_size = hole_end - hole_start; | |
440fd528 TR |
875 | pr_debug("%s %#llx-%#llx: %llu: free\n", prefix, hole_start, |
876 | hole_end, hole_size); | |
2c54b133 DV |
877 | return hole_size; |
878 | } | |
879 | ||
880 | return 0; | |
881 | } | |
882 | ||
e18c0412 DV |
883 | /** |
884 | * drm_mm_debug_table - dump allocator state to dmesg | |
885 | * @mm: drm_mm allocator to dump | |
886 | * @prefix: prefix to use for dumping to dmesg | |
887 | */ | |
2c54b133 DV |
888 | void drm_mm_debug_table(struct drm_mm *mm, const char *prefix) |
889 | { | |
890 | struct drm_mm_node *entry; | |
440fd528 | 891 | u64 total_used = 0, total_free = 0, total = 0; |
2c54b133 DV |
892 | |
893 | total_free += drm_mm_debug_hole(&mm->head_node, prefix); | |
ea7b1dd4 DV |
894 | |
895 | drm_mm_for_each_node(entry, mm) { | |
440fd528 TR |
896 | pr_debug("%s %#llx-%#llx: %llu: used\n", prefix, entry->start, |
897 | entry->start + entry->size, entry->size); | |
ea7b1dd4 | 898 | total_used += entry->size; |
2c54b133 | 899 | total_free += drm_mm_debug_hole(entry, prefix); |
99d7e48e | 900 | } |
ea7b1dd4 DV |
901 | total = total_free + total_used; |
902 | ||
440fd528 TR |
903 | pr_debug("%s total: %llu, used %llu free %llu\n", prefix, total, |
904 | total_used, total_free); | |
99d7e48e JG |
905 | } |
906 | EXPORT_SYMBOL(drm_mm_debug_table); | |
907 | ||
fa8a1238 | 908 | #if defined(CONFIG_DEBUG_FS) |
440fd528 | 909 | static u64 drm_mm_dump_hole(struct seq_file *m, struct drm_mm_node *entry) |
fa8a1238 | 910 | { |
440fd528 | 911 | u64 hole_start, hole_end, hole_size; |
ea7b1dd4 | 912 | |
3a359f0b DV |
913 | if (entry->hole_follows) { |
914 | hole_start = drm_mm_hole_node_start(entry); | |
915 | hole_end = drm_mm_hole_node_end(entry); | |
916 | hole_size = hole_end - hole_start; | |
2f15791c | 917 | seq_printf(m, "%#018llx-%#018llx: %llu: free\n", hole_start, |
440fd528 | 918 | hole_end, hole_size); |
3a359f0b DV |
919 | return hole_size; |
920 | } | |
921 | ||
922 | return 0; | |
923 | } | |
924 | ||
e18c0412 DV |
925 | /** |
926 | * drm_mm_dump_table - dump allocator state to a seq_file | |
927 | * @m: seq_file to dump to | |
928 | * @mm: drm_mm allocator to dump | |
929 | */ | |
3a359f0b DV |
930 | int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm) |
931 | { | |
932 | struct drm_mm_node *entry; | |
440fd528 | 933 | u64 total_used = 0, total_free = 0, total = 0; |
3a359f0b DV |
934 | |
935 | total_free += drm_mm_dump_hole(m, &mm->head_node); | |
ea7b1dd4 DV |
936 | |
937 | drm_mm_for_each_node(entry, mm) { | |
2f15791c | 938 | seq_printf(m, "%#018llx-%#018llx: %llu: used\n", entry->start, |
440fd528 | 939 | entry->start + entry->size, entry->size); |
ea7b1dd4 | 940 | total_used += entry->size; |
3a359f0b | 941 | total_free += drm_mm_dump_hole(m, entry); |
fa8a1238 | 942 | } |
ea7b1dd4 DV |
943 | total = total_free + total_used; |
944 | ||
440fd528 TR |
945 | seq_printf(m, "total: %llu, used %llu free %llu\n", total, |
946 | total_used, total_free); | |
fa8a1238 DA |
947 | return 0; |
948 | } | |
949 | EXPORT_SYMBOL(drm_mm_dump_table); | |
950 | #endif |