blob: aded1e11e8ff8a2ee86accb4ee949d471e4c226e [file] [log] [blame]
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +10001/**************************************************************************
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:
Jan Engelhardt96de0e22007-10-19 23:21:04 +020041 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100042 */
43
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drmP.h>
45#include <drm/drm_mm.h>
Thomas Hellstrom1d584202007-01-08 22:25:47 +110046#include <linux/slab.h>
Dave Airliefa8a1232009-08-26 13:13:37 +100047#include <linux/seq_file.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040048#include <linux/export.h>
Thomas Hellstrom1d584202007-01-08 22:25:47 +110049
Jerome Glisse249d6042009-04-08 17:11:16 +020050#define MM_UNUSED_TARGET 4
51
David Herrmannc700c672013-07-27 13:39:28 +020052static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
53 unsigned long size,
54 unsigned alignment,
55 unsigned long color,
56 enum drm_mm_search_flags flags);
57static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
58 unsigned long size,
59 unsigned alignment,
60 unsigned long color,
61 unsigned long start,
62 unsigned long end,
63 enum drm_mm_search_flags flags);
Jerome Glisse249d6042009-04-08 17:11:16 +020064
Daniel Vetter9fc935d2011-02-18 17:59:13 +010065static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
66 struct drm_mm_node *node,
Chris Wilson6b9d89b2012-07-10 11:15:23 +010067 unsigned long size, unsigned alignment,
68 unsigned long color)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100069{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010070 struct drm_mm *mm = hole_node->mm;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010071 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
72 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
Chris Wilson6b9d89b2012-07-10 11:15:23 +010073 unsigned long adj_start = hole_start;
74 unsigned long adj_end = hole_end;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010075
Chris Wilson9e8944a2012-11-15 11:32:17 +000076 BUG_ON(node->allocated);
Daniel Vetterb0b7af12011-02-18 17:59:14 +010077
Chris Wilson6b9d89b2012-07-10 11:15:23 +010078 if (mm->color_adjust)
79 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
Thomas Hellstrom1d584202007-01-08 22:25:47 +110080
Chris Wilson6b9d89b2012-07-10 11:15:23 +010081 if (alignment) {
82 unsigned tmp = adj_start % alignment;
83 if (tmp)
84 adj_start += alignment - tmp;
85 }
86
87 if (adj_start == hole_start) {
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010088 hole_node->hole_follows = 0;
Chris Wilson6b9d89b2012-07-10 11:15:23 +010089 list_del(&hole_node->hole_stack);
90 }
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100091
Chris Wilson6b9d89b2012-07-10 11:15:23 +010092 node->start = adj_start;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010093 node->size = size;
94 node->mm = mm;
Chris Wilson6b9d89b2012-07-10 11:15:23 +010095 node->color = color;
Daniel Vetterb0b7af12011-02-18 17:59:14 +010096 node->allocated = 1;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010097
98 INIT_LIST_HEAD(&node->hole_stack);
99 list_add(&node->node_list, &hole_node->node_list);
100
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100101 BUG_ON(node->start + node->size > adj_end);
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100102
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100103 node->hole_follows = 0;
Chris Wilson9e8944a2012-11-15 11:32:17 +0000104 if (__drm_mm_hole_node_start(node) < hole_end) {
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100105 list_add(&node->hole_stack, &mm->hole_stack);
106 node->hole_follows = 1;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000107 }
Daniel Vetter9fc935d2011-02-18 17:59:13 +0100108}
109
Ben Widawsky338710e2013-07-05 14:41:03 -0700110int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
Chris Wilson5973c7e2012-11-15 11:32:16 +0000111{
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700112 struct drm_mm_node *hole;
Ben Widawsky338710e2013-07-05 14:41:03 -0700113 unsigned long end = node->start + node->size;
Chris Wilson9e8944a2012-11-15 11:32:17 +0000114 unsigned long hole_start;
115 unsigned long hole_end;
Chris Wilson5973c7e2012-11-15 11:32:16 +0000116
Ben Widawsky338710e2013-07-05 14:41:03 -0700117 BUG_ON(node == NULL);
118
119 /* Find the relevant hole to add our node to */
Chris Wilson9e8944a2012-11-15 11:32:17 +0000120 drm_mm_for_each_hole(hole, mm, hole_start, hole_end) {
Ben Widawsky338710e2013-07-05 14:41:03 -0700121 if (hole_start > node->start || hole_end < end)
Chris Wilson5973c7e2012-11-15 11:32:16 +0000122 continue;
123
Chris Wilson5973c7e2012-11-15 11:32:16 +0000124 node->mm = mm;
125 node->allocated = 1;
126
127 INIT_LIST_HEAD(&node->hole_stack);
128 list_add(&node->node_list, &hole->node_list);
129
Ben Widawsky338710e2013-07-05 14:41:03 -0700130 if (node->start == hole_start) {
Chris Wilson5973c7e2012-11-15 11:32:16 +0000131 hole->hole_follows = 0;
132 list_del_init(&hole->hole_stack);
133 }
134
135 node->hole_follows = 0;
136 if (end != hole_end) {
137 list_add(&node->hole_stack, &mm->hole_stack);
138 node->hole_follows = 1;
139 }
140
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700141 return 0;
Chris Wilson5973c7e2012-11-15 11:32:16 +0000142 }
143
Ben Widawsky338710e2013-07-05 14:41:03 -0700144 WARN(1, "no hole found for node 0x%lx + 0x%lx\n",
145 node->start, node->size);
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700146 return -ENOSPC;
Chris Wilson5973c7e2012-11-15 11:32:16 +0000147}
Ben Widawsky338710e2013-07-05 14:41:03 -0700148EXPORT_SYMBOL(drm_mm_reserve_node);
Chris Wilson5973c7e2012-11-15 11:32:16 +0000149
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100150/**
151 * Search for free space and insert a preallocated memory node. Returns
152 * -ENOSPC if no suitable free area is available. The preallocated memory node
153 * must be cleared.
154 */
Chris Wilsonb8103452012-12-07 20:37:06 +0000155int drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
156 unsigned long size, unsigned alignment,
David Herrmann31e5d7c2013-07-27 13:36:27 +0200157 unsigned long color,
158 enum drm_mm_search_flags flags)
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100159{
160 struct drm_mm_node *hole_node;
161
Chris Wilsonb8103452012-12-07 20:37:06 +0000162 hole_node = drm_mm_search_free_generic(mm, size, alignment,
David Herrmann31e5d7c2013-07-27 13:36:27 +0200163 color, flags);
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100164 if (!hole_node)
165 return -ENOSPC;
166
Chris Wilsonb8103452012-12-07 20:37:06 +0000167 drm_mm_insert_helper(hole_node, node, size, alignment, color);
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100168 return 0;
169}
Chris Wilsonb8103452012-12-07 20:37:06 +0000170EXPORT_SYMBOL(drm_mm_insert_node_generic);
171
Daniel Vetter9fc935d2011-02-18 17:59:13 +0100172static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
173 struct drm_mm_node *node,
174 unsigned long size, unsigned alignment,
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100175 unsigned long color,
Daniel Vetter9fc935d2011-02-18 17:59:13 +0100176 unsigned long start, unsigned long end)
Jerome Glissea2e68e92009-12-07 15:52:56 +0100177{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100178 struct drm_mm *mm = hole_node->mm;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100179 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
180 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100181 unsigned long adj_start = hole_start;
182 unsigned long adj_end = hole_end;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100183
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100184 BUG_ON(!hole_node->hole_follows || node->allocated);
185
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100186 if (adj_start < start)
187 adj_start = start;
Chris Wilson901593f2012-12-19 16:51:06 +0000188 if (adj_end > end)
189 adj_end = end;
190
191 if (mm->color_adjust)
192 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100193
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100194 if (alignment) {
195 unsigned tmp = adj_start % alignment;
196 if (tmp)
197 adj_start += alignment - tmp;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100198 }
199
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100200 if (adj_start == hole_start) {
201 hole_node->hole_follows = 0;
202 list_del(&hole_node->hole_stack);
203 }
204
205 node->start = adj_start;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100206 node->size = size;
207 node->mm = mm;
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100208 node->color = color;
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100209 node->allocated = 1;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100210
211 INIT_LIST_HEAD(&node->hole_stack);
212 list_add(&node->node_list, &hole_node->node_list);
213
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100214 BUG_ON(node->start + node->size > adj_end);
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100215 BUG_ON(node->start + node->size > end);
216
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100217 node->hole_follows = 0;
Chris Wilson9e8944a2012-11-15 11:32:17 +0000218 if (__drm_mm_hole_node_start(node) < hole_end) {
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100219 list_add(&node->hole_stack, &mm->hole_stack);
220 node->hole_follows = 1;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100221 }
Daniel Vetter9fc935d2011-02-18 17:59:13 +0100222}
223
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100224/**
225 * Search for free space and insert a preallocated memory node. Returns
226 * -ENOSPC if no suitable free area is available. This is for range
227 * restricted allocations. The preallocated memory node must be cleared.
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000228 */
Chris Wilsonb8103452012-12-07 20:37:06 +0000229int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
230 unsigned long size, unsigned alignment, unsigned long color,
David Herrmann31e5d7c2013-07-27 13:36:27 +0200231 unsigned long start, unsigned long end,
232 enum drm_mm_search_flags flags)
Chris Wilsonb8103452012-12-07 20:37:06 +0000233{
234 struct drm_mm_node *hole_node;
235
236 hole_node = drm_mm_search_free_in_range_generic(mm,
237 size, alignment, color,
David Herrmann31e5d7c2013-07-27 13:36:27 +0200238 start, end, flags);
Chris Wilsonb8103452012-12-07 20:37:06 +0000239 if (!hole_node)
240 return -ENOSPC;
241
242 drm_mm_insert_helper_range(hole_node, node,
243 size, alignment, color,
244 start, end);
245 return 0;
246}
247EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
248
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100249/**
250 * Remove a memory node from the allocator.
251 */
252void drm_mm_remove_node(struct drm_mm_node *node)
253{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100254 struct drm_mm *mm = node->mm;
255 struct drm_mm_node *prev_node;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000256
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100257 BUG_ON(node->scanned_block || node->scanned_prev_free
258 || node->scanned_next_free);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000259
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100260 prev_node =
261 list_entry(node->node_list.prev, struct drm_mm_node, node_list);
Daniel Vetter709ea972010-07-02 15:02:16 +0100262
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100263 if (node->hole_follows) {
Chris Wilson9e8944a2012-11-15 11:32:17 +0000264 BUG_ON(__drm_mm_hole_node_start(node) ==
265 __drm_mm_hole_node_end(node));
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100266 list_del(&node->hole_stack);
267 } else
Chris Wilson9e8944a2012-11-15 11:32:17 +0000268 BUG_ON(__drm_mm_hole_node_start(node) !=
269 __drm_mm_hole_node_end(node));
270
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100271
272 if (!prev_node->hole_follows) {
273 prev_node->hole_follows = 1;
274 list_add(&prev_node->hole_stack, &mm->hole_stack);
275 } else
276 list_move(&prev_node->hole_stack, &mm->hole_stack);
277
278 list_del(&node->node_list);
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100279 node->allocated = 0;
280}
281EXPORT_SYMBOL(drm_mm_remove_node);
282
Daniel Vetter75214732010-08-26 21:44:17 +0200283static int check_free_hole(unsigned long start, unsigned long end,
284 unsigned long size, unsigned alignment)
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100285{
Daniel Vetter75214732010-08-26 21:44:17 +0200286 if (end - start < size)
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100287 return 0;
288
289 if (alignment) {
Daniel Vetter75214732010-08-26 21:44:17 +0200290 unsigned tmp = start % alignment;
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100291 if (tmp)
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100292 start += alignment - tmp;
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100293 }
294
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100295 return end >= start + size;
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100296}
297
David Herrmannc700c672013-07-27 13:39:28 +0200298static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
299 unsigned long size,
300 unsigned alignment,
301 unsigned long color,
302 enum drm_mm_search_flags flags)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000303{
Dave Airlie55910512007-07-11 16:53:40 +1000304 struct drm_mm_node *entry;
305 struct drm_mm_node *best;
Chris Wilson9e8944a2012-11-15 11:32:17 +0000306 unsigned long adj_start;
307 unsigned long adj_end;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000308 unsigned long best_size;
309
Daniel Vetter709ea972010-07-02 15:02:16 +0100310 BUG_ON(mm->scanned_blocks);
311
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000312 best = NULL;
313 best_size = ~0UL;
314
Chris Wilson9e8944a2012-11-15 11:32:17 +0000315 drm_mm_for_each_hole(entry, mm, adj_start, adj_end) {
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100316 if (mm->color_adjust) {
317 mm->color_adjust(entry, color, &adj_start, &adj_end);
318 if (adj_end <= adj_start)
319 continue;
320 }
321
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100322 if (!check_free_hole(adj_start, adj_end, size, alignment))
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100323 continue;
324
David Herrmann31e5d7c2013-07-27 13:36:27 +0200325 if (!(flags & DRM_MM_SEARCH_BEST))
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100326 return entry;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100327
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100328 if (entry->size < best_size) {
329 best = entry;
330 best_size = entry->size;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000331 }
332 }
333
334 return best;
335}
336
David Herrmannc700c672013-07-27 13:39:28 +0200337static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100338 unsigned long size,
339 unsigned alignment,
340 unsigned long color,
341 unsigned long start,
342 unsigned long end,
David Herrmann31e5d7c2013-07-27 13:36:27 +0200343 enum drm_mm_search_flags flags)
Jerome Glissea2e68e92009-12-07 15:52:56 +0100344{
Jerome Glissea2e68e92009-12-07 15:52:56 +0100345 struct drm_mm_node *entry;
346 struct drm_mm_node *best;
Chris Wilson9e8944a2012-11-15 11:32:17 +0000347 unsigned long adj_start;
348 unsigned long adj_end;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100349 unsigned long best_size;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100350
Daniel Vetter709ea972010-07-02 15:02:16 +0100351 BUG_ON(mm->scanned_blocks);
352
Jerome Glissea2e68e92009-12-07 15:52:56 +0100353 best = NULL;
354 best_size = ~0UL;
355
Chris Wilson9e8944a2012-11-15 11:32:17 +0000356 drm_mm_for_each_hole(entry, mm, adj_start, adj_end) {
357 if (adj_start < start)
358 adj_start = start;
359 if (adj_end > end)
360 adj_end = end;
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100361
362 if (mm->color_adjust) {
363 mm->color_adjust(entry, color, &adj_start, &adj_end);
364 if (adj_end <= adj_start)
365 continue;
366 }
367
Daniel Vetter75214732010-08-26 21:44:17 +0200368 if (!check_free_hole(adj_start, adj_end, size, alignment))
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100369 continue;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100370
David Herrmann31e5d7c2013-07-27 13:36:27 +0200371 if (!(flags & DRM_MM_SEARCH_BEST))
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100372 return entry;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100373
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100374 if (entry->size < best_size) {
375 best = entry;
376 best_size = entry->size;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100377 }
378 }
379
380 return best;
381}
Jerome Glissea2e68e92009-12-07 15:52:56 +0100382
Daniel Vetter709ea972010-07-02 15:02:16 +0100383/**
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100384 * Moves an allocation. To be used with embedded struct drm_mm_node.
385 */
386void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
387{
388 list_replace(&old->node_list, &new->node_list);
Daniel Vetter2bbd4492011-05-06 23:47:53 +0200389 list_replace(&old->hole_stack, &new->hole_stack);
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100390 new->hole_follows = old->hole_follows;
391 new->mm = old->mm;
392 new->start = old->start;
393 new->size = old->size;
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100394 new->color = old->color;
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100395
396 old->allocated = 0;
397 new->allocated = 1;
398}
399EXPORT_SYMBOL(drm_mm_replace_node);
400
401/**
Daniel Vetter709ea972010-07-02 15:02:16 +0100402 * Initializa lru scanning.
403 *
404 * This simply sets up the scanning routines with the parameters for the desired
405 * hole.
406 *
407 * Warning: As long as the scan list is non-empty, no other operations than
408 * adding/removing nodes to/from the scan list are allowed.
409 */
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100410void drm_mm_init_scan(struct drm_mm *mm,
411 unsigned long size,
412 unsigned alignment,
413 unsigned long color)
Daniel Vetter709ea972010-07-02 15:02:16 +0100414{
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100415 mm->scan_color = color;
Daniel Vetter709ea972010-07-02 15:02:16 +0100416 mm->scan_alignment = alignment;
417 mm->scan_size = size;
418 mm->scanned_blocks = 0;
419 mm->scan_hit_start = 0;
Chris Wilson901593f2012-12-19 16:51:06 +0000420 mm->scan_hit_end = 0;
Daniel Vetterd935cc62010-09-16 15:13:11 +0200421 mm->scan_check_range = 0;
Daniel Vetterae0cec22011-02-18 17:59:15 +0100422 mm->prev_scanned_node = NULL;
Daniel Vetter709ea972010-07-02 15:02:16 +0100423}
424EXPORT_SYMBOL(drm_mm_init_scan);
425
426/**
Daniel Vetterd935cc62010-09-16 15:13:11 +0200427 * Initializa lru scanning.
428 *
429 * This simply sets up the scanning routines with the parameters for the desired
430 * hole. This version is for range-restricted scans.
431 *
432 * Warning: As long as the scan list is non-empty, no other operations than
433 * adding/removing nodes to/from the scan list are allowed.
434 */
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100435void drm_mm_init_scan_with_range(struct drm_mm *mm,
436 unsigned long size,
Daniel Vetterd935cc62010-09-16 15:13:11 +0200437 unsigned alignment,
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100438 unsigned long color,
Daniel Vetterd935cc62010-09-16 15:13:11 +0200439 unsigned long start,
440 unsigned long end)
441{
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100442 mm->scan_color = color;
Daniel Vetterd935cc62010-09-16 15:13:11 +0200443 mm->scan_alignment = alignment;
444 mm->scan_size = size;
445 mm->scanned_blocks = 0;
446 mm->scan_hit_start = 0;
Chris Wilson901593f2012-12-19 16:51:06 +0000447 mm->scan_hit_end = 0;
Daniel Vetterd935cc62010-09-16 15:13:11 +0200448 mm->scan_start = start;
449 mm->scan_end = end;
450 mm->scan_check_range = 1;
Daniel Vetterae0cec22011-02-18 17:59:15 +0100451 mm->prev_scanned_node = NULL;
Daniel Vetterd935cc62010-09-16 15:13:11 +0200452}
453EXPORT_SYMBOL(drm_mm_init_scan_with_range);
454
455/**
Daniel Vetter709ea972010-07-02 15:02:16 +0100456 * Add a node to the scan list that might be freed to make space for the desired
457 * hole.
458 *
459 * Returns non-zero, if a hole has been found, zero otherwise.
460 */
461int drm_mm_scan_add_block(struct drm_mm_node *node)
462{
463 struct drm_mm *mm = node->mm;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100464 struct drm_mm_node *prev_node;
465 unsigned long hole_start, hole_end;
Chris Wilson901593f2012-12-19 16:51:06 +0000466 unsigned long adj_start, adj_end;
Daniel Vetter709ea972010-07-02 15:02:16 +0100467
468 mm->scanned_blocks++;
469
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100470 BUG_ON(node->scanned_block);
Daniel Vetter709ea972010-07-02 15:02:16 +0100471 node->scanned_block = 1;
Daniel Vetter709ea972010-07-02 15:02:16 +0100472
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100473 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
474 node_list);
Daniel Vetter709ea972010-07-02 15:02:16 +0100475
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100476 node->scanned_preceeds_hole = prev_node->hole_follows;
477 prev_node->hole_follows = 1;
478 list_del(&node->node_list);
479 node->node_list.prev = &prev_node->node_list;
Daniel Vetterae0cec22011-02-18 17:59:15 +0100480 node->node_list.next = &mm->prev_scanned_node->node_list;
481 mm->prev_scanned_node = node;
Daniel Vetter709ea972010-07-02 15:02:16 +0100482
Chris Wilson901593f2012-12-19 16:51:06 +0000483 adj_start = hole_start = drm_mm_hole_node_start(prev_node);
484 adj_end = hole_end = drm_mm_hole_node_end(prev_node);
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100485
Daniel Vetterd935cc62010-09-16 15:13:11 +0200486 if (mm->scan_check_range) {
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100487 if (adj_start < mm->scan_start)
488 adj_start = mm->scan_start;
489 if (adj_end > mm->scan_end)
490 adj_end = mm->scan_end;
Daniel Vetterd935cc62010-09-16 15:13:11 +0200491 }
492
Chris Wilson901593f2012-12-19 16:51:06 +0000493 if (mm->color_adjust)
494 mm->color_adjust(prev_node, mm->scan_color,
495 &adj_start, &adj_end);
496
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100497 if (check_free_hole(adj_start, adj_end,
Daniel Vetter75214732010-08-26 21:44:17 +0200498 mm->scan_size, mm->scan_alignment)) {
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100499 mm->scan_hit_start = hole_start;
Chris Wilson901593f2012-12-19 16:51:06 +0000500 mm->scan_hit_end = hole_end;
Daniel Vetter709ea972010-07-02 15:02:16 +0100501 return 1;
502 }
503
504 return 0;
505}
506EXPORT_SYMBOL(drm_mm_scan_add_block);
507
508/**
509 * Remove a node from the scan list.
510 *
511 * Nodes _must_ be removed in the exact same order from the scan list as they
512 * have been added, otherwise the internal state of the memory manager will be
513 * corrupted.
514 *
515 * When the scan list is empty, the selected memory nodes can be freed. An
David Herrmann31e5d7c2013-07-27 13:36:27 +0200516 * immediately following drm_mm_search_free with !DRM_MM_SEARCH_BEST will then
517 * return the just freed block (because its at the top of the free_stack list).
Daniel Vetter709ea972010-07-02 15:02:16 +0100518 *
519 * Returns one if this block should be evicted, zero otherwise. Will always
520 * return zero when no hole has been found.
521 */
522int drm_mm_scan_remove_block(struct drm_mm_node *node)
523{
524 struct drm_mm *mm = node->mm;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100525 struct drm_mm_node *prev_node;
Daniel Vetter709ea972010-07-02 15:02:16 +0100526
527 mm->scanned_blocks--;
528
529 BUG_ON(!node->scanned_block);
530 node->scanned_block = 0;
Daniel Vetter709ea972010-07-02 15:02:16 +0100531
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100532 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
533 node_list);
Daniel Vetter709ea972010-07-02 15:02:16 +0100534
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100535 prev_node->hole_follows = node->scanned_preceeds_hole;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100536 list_add(&node->node_list, &prev_node->node_list);
Daniel Vetter709ea972010-07-02 15:02:16 +0100537
Chris Wilson901593f2012-12-19 16:51:06 +0000538 return (drm_mm_hole_node_end(node) > mm->scan_hit_start &&
539 node->start < mm->scan_hit_end);
Daniel Vetter709ea972010-07-02 15:02:16 +0100540}
541EXPORT_SYMBOL(drm_mm_scan_remove_block);
542
Dave Airlie55910512007-07-11 16:53:40 +1000543int drm_mm_clean(struct drm_mm * mm)
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100544{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100545 struct list_head *head = &mm->head_node.node_list;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100546
547 return (head->next->next == head);
548}
Jerome Glisse249d6042009-04-08 17:11:16 +0200549EXPORT_SYMBOL(drm_mm_clean);
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100550
David Herrmann77ef8bb2013-07-01 20:32:58 +0200551void drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000552{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100553 INIT_LIST_HEAD(&mm->hole_stack);
Daniel Vetter709ea972010-07-02 15:02:16 +0100554 mm->scanned_blocks = 0;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000555
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100556 /* Clever trick to avoid a special case in the free hole tracking. */
557 INIT_LIST_HEAD(&mm->head_node.node_list);
558 INIT_LIST_HEAD(&mm->head_node.hole_stack);
559 mm->head_node.hole_follows = 1;
560 mm->head_node.scanned_block = 0;
561 mm->head_node.scanned_prev_free = 0;
562 mm->head_node.scanned_next_free = 0;
563 mm->head_node.mm = mm;
564 mm->head_node.start = start + size;
565 mm->head_node.size = start - mm->head_node.start;
566 list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
567
Chris Wilson6b9d89b2012-07-10 11:15:23 +0100568 mm->color_adjust = NULL;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000569}
Eric Anholt673a3942008-07-30 12:06:12 -0700570EXPORT_SYMBOL(drm_mm_init);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000571
Dave Airlie55910512007-07-11 16:53:40 +1000572void drm_mm_takedown(struct drm_mm * mm)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000573{
David Herrmannc700c672013-07-27 13:39:28 +0200574 WARN(!list_empty(&mm->head_node.node_list),
575 "Memory manager not clean during takedown.\n");
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000576}
Dave Airlief453ba02008-11-07 14:05:41 -0800577EXPORT_SYMBOL(drm_mm_takedown);
Dave Airliefa8a1232009-08-26 13:13:37 +1000578
Daniel Vetter2c54b132013-07-01 22:01:02 +0200579static unsigned long drm_mm_debug_hole(struct drm_mm_node *entry,
580 const char *prefix)
581{
582 unsigned long hole_start, hole_end, hole_size;
583
584 if (entry->hole_follows) {
585 hole_start = drm_mm_hole_node_start(entry);
586 hole_end = drm_mm_hole_node_end(entry);
587 hole_size = hole_end - hole_start;
588 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
589 prefix, hole_start, hole_end,
590 hole_size);
591 return hole_size;
592 }
593
594 return 0;
595}
596
Jerome Glisse99d7e482009-12-09 21:55:09 +0100597void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
598{
599 struct drm_mm_node *entry;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100600 unsigned long total_used = 0, total_free = 0, total = 0;
Jerome Glisse99d7e482009-12-09 21:55:09 +0100601
Daniel Vetter2c54b132013-07-01 22:01:02 +0200602 total_free += drm_mm_debug_hole(&mm->head_node, prefix);
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100603
604 drm_mm_for_each_node(entry, mm) {
605 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: used\n",
Jerome Glisse99d7e482009-12-09 21:55:09 +0100606 prefix, entry->start, entry->start + entry->size,
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100607 entry->size);
608 total_used += entry->size;
Daniel Vetter2c54b132013-07-01 22:01:02 +0200609 total_free += drm_mm_debug_hole(entry, prefix);
Jerome Glisse99d7e482009-12-09 21:55:09 +0100610 }
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100611 total = total_free + total_used;
612
613 printk(KERN_DEBUG "%s total: %lu, used %lu free %lu\n", prefix, total,
Jerome Glisse99d7e482009-12-09 21:55:09 +0100614 total_used, total_free);
615}
616EXPORT_SYMBOL(drm_mm_debug_table);
617
Dave Airliefa8a1232009-08-26 13:13:37 +1000618#if defined(CONFIG_DEBUG_FS)
Daniel Vetter3a359f02013-04-20 12:08:11 +0200619static unsigned long drm_mm_dump_hole(struct seq_file *m, struct drm_mm_node *entry)
620{
621 unsigned long hole_start, hole_end, hole_size;
622
623 if (entry->hole_follows) {
624 hole_start = drm_mm_hole_node_start(entry);
625 hole_end = drm_mm_hole_node_end(entry);
626 hole_size = hole_end - hole_start;
627 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
628 hole_start, hole_end, hole_size);
629 return hole_size;
630 }
631
632 return 0;
633}
634
Dave Airliefa8a1232009-08-26 13:13:37 +1000635int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
636{
637 struct drm_mm_node *entry;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100638 unsigned long total_used = 0, total_free = 0, total = 0;
Dave Airliefa8a1232009-08-26 13:13:37 +1000639
Daniel Vetter3a359f02013-04-20 12:08:11 +0200640 total_free += drm_mm_dump_hole(m, &mm->head_node);
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100641
642 drm_mm_for_each_node(entry, mm) {
643 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: used\n",
644 entry->start, entry->start + entry->size,
645 entry->size);
646 total_used += entry->size;
Daniel Vetter3a359f02013-04-20 12:08:11 +0200647 total_free += drm_mm_dump_hole(m, entry);
Dave Airliefa8a1232009-08-26 13:13:37 +1000648 }
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100649 total = total_free + total_used;
650
651 seq_printf(m, "total: %lu, used %lu free %lu\n", total, total_used, total_free);
Dave Airliefa8a1232009-08-26 13:13:37 +1000652 return 0;
653}
654EXPORT_SYMBOL(drm_mm_dump_table);
655#endif