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