blob: fecb4063c018d5ac7d91e43ad71b4db4d7b07256 [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
44#include "drmP.h"
Jerome Glisse249d6042009-04-08 17:11:16 +020045#include "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>
Thomas Hellstrom1d584202007-01-08 22:25:47 +110048
Jerome Glisse249d6042009-04-08 17:11:16 +020049#define MM_UNUSED_TARGET 4
50
Jerome Glisse249d6042009-04-08 17:11:16 +020051static struct drm_mm_node *drm_mm_kmalloc(struct drm_mm *mm, int atomic)
Thomas Hellstrom1d584202007-01-08 22:25:47 +110052{
Dave Airlie55910512007-07-11 16:53:40 +100053 struct drm_mm_node *child;
Thomas Hellstrom1d584202007-01-08 22:25:47 +110054
Jerome Glisse249d6042009-04-08 17:11:16 +020055 if (atomic)
Daniel Vetter709ea972010-07-02 15:02:16 +010056 child = kzalloc(sizeof(*child), GFP_ATOMIC);
Jerome Glisse249d6042009-04-08 17:11:16 +020057 else
Daniel Vetter709ea972010-07-02 15:02:16 +010058 child = kzalloc(sizeof(*child), GFP_KERNEL);
Jerome Glisse249d6042009-04-08 17:11:16 +020059
60 if (unlikely(child == NULL)) {
61 spin_lock(&mm->unused_lock);
62 if (list_empty(&mm->unused_nodes))
63 child = NULL;
64 else {
65 child =
66 list_entry(mm->unused_nodes.next,
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010067 struct drm_mm_node, node_list);
68 list_del(&child->node_list);
Jerome Glisse249d6042009-04-08 17:11:16 +020069 --mm->num_unused;
70 }
71 spin_unlock(&mm->unused_lock);
72 }
73 return child;
74}
75
Jerome Glissea698cf32009-11-13 20:56:58 +010076/* drm_mm_pre_get() - pre allocate drm_mm_node structure
77 * drm_mm: memory manager struct we are pre-allocating for
78 *
79 * Returns 0 on success or -ENOMEM if allocation fails.
80 */
Jerome Glisse249d6042009-04-08 17:11:16 +020081int drm_mm_pre_get(struct drm_mm *mm)
82{
83 struct drm_mm_node *node;
84
85 spin_lock(&mm->unused_lock);
86 while (mm->num_unused < MM_UNUSED_TARGET) {
87 spin_unlock(&mm->unused_lock);
Daniel Vetter709ea972010-07-02 15:02:16 +010088 node = kzalloc(sizeof(*node), GFP_KERNEL);
Jerome Glisse249d6042009-04-08 17:11:16 +020089 spin_lock(&mm->unused_lock);
90
91 if (unlikely(node == NULL)) {
92 int ret = (mm->num_unused < 2) ? -ENOMEM : 0;
93 spin_unlock(&mm->unused_lock);
94 return ret;
95 }
96 ++mm->num_unused;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010097 list_add_tail(&node->node_list, &mm->unused_nodes);
Jerome Glisse249d6042009-04-08 17:11:16 +020098 }
99 spin_unlock(&mm->unused_lock);
100 return 0;
101}
102EXPORT_SYMBOL(drm_mm_pre_get);
103
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100104static inline unsigned long drm_mm_hole_node_start(struct drm_mm_node *hole_node)
Jerome Glisse249d6042009-04-08 17:11:16 +0200105{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100106 return hole_node->start + hole_node->size;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100107}
108
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100109static inline unsigned long drm_mm_hole_node_end(struct drm_mm_node *hole_node)
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100110{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100111 struct drm_mm_node *next_node =
112 list_entry(hole_node->node_list.next, struct drm_mm_node,
113 node_list);
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100114
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100115 return next_node->start;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100116}
117
Daniel Vetter9fc935d2011-02-18 17:59:13 +0100118static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
119 struct drm_mm_node *node,
120 unsigned long size, unsigned alignment)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000121{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100122 struct drm_mm *mm = hole_node->mm;
123 unsigned long tmp = 0, wasted = 0;
124 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
125 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
126
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000127 if (alignment)
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100128 tmp = hole_start % alignment;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100129
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100130 if (!tmp) {
131 hole_node->hole_follows = 0;
132 list_del_init(&hole_node->hole_stack);
133 } else
134 wasted = alignment - tmp;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000135
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100136 node->start = hole_start + wasted;
137 node->size = size;
138 node->mm = mm;
139
140 INIT_LIST_HEAD(&node->hole_stack);
141 list_add(&node->node_list, &hole_node->node_list);
142
143 BUG_ON(node->start + node->size > hole_end);
144
145 if (node->start + node->size < hole_end) {
146 list_add(&node->hole_stack, &mm->hole_stack);
147 node->hole_follows = 1;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000148 } else {
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100149 node->hole_follows = 0;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000150 }
Daniel Vetter9fc935d2011-02-18 17:59:13 +0100151}
152
153struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *hole_node,
154 unsigned long size,
155 unsigned alignment,
156 int atomic)
157{
158 struct drm_mm_node *node;
159
160 BUG_ON(!hole_node->hole_follows);
161
162 node = drm_mm_kmalloc(hole_node->mm, atomic);
163 if (unlikely(node == NULL))
164 return NULL;
165
166 drm_mm_insert_helper(hole_node, node, size, alignment);
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100167
Chris Wilsone6c03c52009-05-22 14:14:22 +0100168 return node;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000169}
Thomas Hellstrom89579f72009-06-17 12:29:56 +0200170EXPORT_SYMBOL(drm_mm_get_block_generic);
Jerome Glisse249d6042009-04-08 17:11:16 +0200171
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,
175 unsigned long start, unsigned long end)
Jerome Glissea2e68e92009-12-07 15:52:56 +0100176{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100177 struct drm_mm *mm = hole_node->mm;
178 unsigned long tmp = 0, wasted = 0;
179 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
180 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
Jerome Glissea2e68e92009-12-07 15:52:56 +0100181
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100182 if (hole_start < start)
183 wasted += start - hole_start;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100184 if (alignment)
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100185 tmp = (hole_start + wasted) % alignment;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100186
187 if (tmp)
188 wasted += alignment - tmp;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100189
190 if (!wasted) {
191 hole_node->hole_follows = 0;
192 list_del_init(&hole_node->hole_stack);
Jerome Glissea2e68e92009-12-07 15:52:56 +0100193 }
194
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100195 node->start = hole_start + wasted;
196 node->size = size;
197 node->mm = mm;
198
199 INIT_LIST_HEAD(&node->hole_stack);
200 list_add(&node->node_list, &hole_node->node_list);
201
202 BUG_ON(node->start + node->size > hole_end);
203 BUG_ON(node->start + node->size > end);
204
205 if (node->start + node->size < hole_end) {
206 list_add(&node->hole_stack, &mm->hole_stack);
207 node->hole_follows = 1;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100208 } else {
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100209 node->hole_follows = 0;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100210 }
Daniel Vetter9fc935d2011-02-18 17:59:13 +0100211}
212
213struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *hole_node,
214 unsigned long size,
215 unsigned alignment,
216 unsigned long start,
217 unsigned long end,
218 int atomic)
219{
220 struct drm_mm_node *node;
221
222 BUG_ON(!hole_node->hole_follows);
223
224 node = drm_mm_kmalloc(hole_node->mm, atomic);
225 if (unlikely(node == NULL))
226 return NULL;
227
228 drm_mm_insert_helper_range(hole_node, node, size, alignment,
229 start, end);
Jerome Glissea2e68e92009-12-07 15:52:56 +0100230
Jerome Glissea2e68e92009-12-07 15:52:56 +0100231 return node;
232}
233EXPORT_SYMBOL(drm_mm_get_block_range_generic);
234
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000235/*
236 * Put a block. Merge with the previous and / or next block if they are free.
237 * Otherwise add to the free stack.
238 */
239
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100240void drm_mm_put_block(struct drm_mm_node *node)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000241{
242
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100243 struct drm_mm *mm = node->mm;
244 struct drm_mm_node *prev_node;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000245
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100246 BUG_ON(node->scanned_block || node->scanned_prev_free
247 || node->scanned_next_free);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000248
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100249 prev_node =
250 list_entry(node->node_list.prev, struct drm_mm_node, node_list);
Daniel Vetter709ea972010-07-02 15:02:16 +0100251
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100252 if (node->hole_follows) {
253 BUG_ON(drm_mm_hole_node_start(node)
254 == drm_mm_hole_node_end(node));
255 list_del(&node->hole_stack);
256 } else
257 BUG_ON(drm_mm_hole_node_start(node)
258 != drm_mm_hole_node_end(node));
259
260 if (!prev_node->hole_follows) {
261 prev_node->hole_follows = 1;
262 list_add(&prev_node->hole_stack, &mm->hole_stack);
263 } else
264 list_move(&prev_node->hole_stack, &mm->hole_stack);
265
266 list_del(&node->node_list);
267 spin_lock(&mm->unused_lock);
268 if (mm->num_unused < MM_UNUSED_TARGET) {
269 list_add(&node->node_list, &mm->unused_nodes);
270 ++mm->num_unused;
271 } else
272 kfree(node);
273 spin_unlock(&mm->unused_lock);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000274}
Eric Anholt673a3942008-07-30 12:06:12 -0700275EXPORT_SYMBOL(drm_mm_put_block);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000276
Daniel Vetter75214732010-08-26 21:44:17 +0200277static int check_free_hole(unsigned long start, unsigned long end,
278 unsigned long size, unsigned alignment)
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100279{
280 unsigned wasted = 0;
281
Daniel Vetter75214732010-08-26 21:44:17 +0200282 if (end - start < size)
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100283 return 0;
284
285 if (alignment) {
Daniel Vetter75214732010-08-26 21:44:17 +0200286 unsigned tmp = start % alignment;
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100287 if (tmp)
288 wasted = alignment - tmp;
289 }
290
Daniel Vetter75214732010-08-26 21:44:17 +0200291 if (end >= start + size + wasted) {
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100292 return 1;
293 }
294
295 return 0;
296}
297
Jerome Glisse249d6042009-04-08 17:11:16 +0200298struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
299 unsigned long size,
300 unsigned alignment, int best_match)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000301{
Dave Airlie55910512007-07-11 16:53:40 +1000302 struct drm_mm_node *entry;
303 struct drm_mm_node *best;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000304 unsigned long best_size;
305
Daniel Vetter709ea972010-07-02 15:02:16 +0100306 BUG_ON(mm->scanned_blocks);
307
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000308 best = NULL;
309 best_size = ~0UL;
310
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100311 list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
312 BUG_ON(!entry->hole_follows);
313 if (!check_free_hole(drm_mm_hole_node_start(entry),
314 drm_mm_hole_node_end(entry),
Daniel Vetter75214732010-08-26 21:44:17 +0200315 size, alignment))
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100316 continue;
317
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100318 if (!best_match)
319 return entry;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100320
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100321 if (entry->size < best_size) {
322 best = entry;
323 best_size = entry->size;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000324 }
325 }
326
327 return best;
328}
Jerome Glisse249d6042009-04-08 17:11:16 +0200329EXPORT_SYMBOL(drm_mm_search_free);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000330
Jerome Glissea2e68e92009-12-07 15:52:56 +0100331struct drm_mm_node *drm_mm_search_free_in_range(const struct drm_mm *mm,
332 unsigned long size,
333 unsigned alignment,
334 unsigned long start,
335 unsigned long end,
336 int best_match)
337{
Jerome Glissea2e68e92009-12-07 15:52:56 +0100338 struct drm_mm_node *entry;
339 struct drm_mm_node *best;
340 unsigned long best_size;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100341
Daniel Vetter709ea972010-07-02 15:02:16 +0100342 BUG_ON(mm->scanned_blocks);
343
Jerome Glissea2e68e92009-12-07 15:52:56 +0100344 best = NULL;
345 best_size = ~0UL;
346
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100347 list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
348 unsigned long adj_start = drm_mm_hole_node_start(entry) < start ?
349 start : drm_mm_hole_node_start(entry);
350 unsigned long adj_end = drm_mm_hole_node_end(entry) > end ?
351 end : drm_mm_hole_node_end(entry);
Jerome Glissea2e68e92009-12-07 15:52:56 +0100352
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100353 BUG_ON(!entry->hole_follows);
Daniel Vetter75214732010-08-26 21:44:17 +0200354 if (!check_free_hole(adj_start, adj_end, size, alignment))
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100355 continue;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100356
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100357 if (!best_match)
358 return entry;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100359
Daniel Vetter7a6b2892010-07-02 15:02:15 +0100360 if (entry->size < best_size) {
361 best = entry;
362 best_size = entry->size;
Jerome Glissea2e68e92009-12-07 15:52:56 +0100363 }
364 }
365
366 return best;
367}
368EXPORT_SYMBOL(drm_mm_search_free_in_range);
369
Daniel Vetter709ea972010-07-02 15:02:16 +0100370/**
371 * Initializa lru scanning.
372 *
373 * This simply sets up the scanning routines with the parameters for the desired
374 * hole.
375 *
376 * Warning: As long as the scan list is non-empty, no other operations than
377 * adding/removing nodes to/from the scan list are allowed.
378 */
379void drm_mm_init_scan(struct drm_mm *mm, unsigned long size,
380 unsigned alignment)
381{
382 mm->scan_alignment = alignment;
383 mm->scan_size = size;
384 mm->scanned_blocks = 0;
385 mm->scan_hit_start = 0;
386 mm->scan_hit_size = 0;
Daniel Vetterd935cc62010-09-16 15:13:11 +0200387 mm->scan_check_range = 0;
Daniel Vetter709ea972010-07-02 15:02:16 +0100388}
389EXPORT_SYMBOL(drm_mm_init_scan);
390
391/**
Daniel Vetterd935cc62010-09-16 15:13:11 +0200392 * Initializa lru scanning.
393 *
394 * This simply sets up the scanning routines with the parameters for the desired
395 * hole. This version is for range-restricted scans.
396 *
397 * Warning: As long as the scan list is non-empty, no other operations than
398 * adding/removing nodes to/from the scan list are allowed.
399 */
400void drm_mm_init_scan_with_range(struct drm_mm *mm, unsigned long size,
401 unsigned alignment,
402 unsigned long start,
403 unsigned long end)
404{
405 mm->scan_alignment = alignment;
406 mm->scan_size = size;
407 mm->scanned_blocks = 0;
408 mm->scan_hit_start = 0;
409 mm->scan_hit_size = 0;
410 mm->scan_start = start;
411 mm->scan_end = end;
412 mm->scan_check_range = 1;
413}
414EXPORT_SYMBOL(drm_mm_init_scan_with_range);
415
416/**
Daniel Vetter709ea972010-07-02 15:02:16 +0100417 * Add a node to the scan list that might be freed to make space for the desired
418 * hole.
419 *
420 * Returns non-zero, if a hole has been found, zero otherwise.
421 */
422int drm_mm_scan_add_block(struct drm_mm_node *node)
423{
424 struct drm_mm *mm = node->mm;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100425 struct drm_mm_node *prev_node;
426 unsigned long hole_start, hole_end;
Daniel Vetterd935cc62010-09-16 15:13:11 +0200427 unsigned long adj_start;
428 unsigned long adj_end;
Daniel Vetter709ea972010-07-02 15:02:16 +0100429
430 mm->scanned_blocks++;
431
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100432 BUG_ON(node->scanned_block);
Daniel Vetter709ea972010-07-02 15:02:16 +0100433 node->scanned_block = 1;
Daniel Vetter709ea972010-07-02 15:02:16 +0100434
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100435 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
436 node_list);
Daniel Vetter709ea972010-07-02 15:02:16 +0100437
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100438 node->scanned_preceeds_hole = prev_node->hole_follows;
439 prev_node->hole_follows = 1;
440 list_del(&node->node_list);
441 node->node_list.prev = &prev_node->node_list;
Daniel Vetter709ea972010-07-02 15:02:16 +0100442
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100443 hole_start = drm_mm_hole_node_start(prev_node);
444 hole_end = drm_mm_hole_node_end(prev_node);
Daniel Vetterd935cc62010-09-16 15:13:11 +0200445 if (mm->scan_check_range) {
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100446 adj_start = hole_start < mm->scan_start ?
447 mm->scan_start : hole_start;
448 adj_end = hole_end > mm->scan_end ?
449 mm->scan_end : hole_end;
Daniel Vetterd935cc62010-09-16 15:13:11 +0200450 } else {
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100451 adj_start = hole_start;
452 adj_end = hole_end;
Daniel Vetterd935cc62010-09-16 15:13:11 +0200453 }
454
455 if (check_free_hole(adj_start , adj_end,
Daniel Vetter75214732010-08-26 21:44:17 +0200456 mm->scan_size, mm->scan_alignment)) {
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100457 mm->scan_hit_start = hole_start;
458 mm->scan_hit_size = hole_end;
Daniel Vetter709ea972010-07-02 15:02:16 +0100459
460 return 1;
461 }
462
463 return 0;
464}
465EXPORT_SYMBOL(drm_mm_scan_add_block);
466
467/**
468 * Remove a node from the scan list.
469 *
470 * Nodes _must_ be removed in the exact same order from the scan list as they
471 * have been added, otherwise the internal state of the memory manager will be
472 * corrupted.
473 *
474 * When the scan list is empty, the selected memory nodes can be freed. An
475 * immediatly following drm_mm_search_free with best_match = 0 will then return
476 * the just freed block (because its at the top of the free_stack list).
477 *
478 * Returns one if this block should be evicted, zero otherwise. Will always
479 * return zero when no hole has been found.
480 */
481int drm_mm_scan_remove_block(struct drm_mm_node *node)
482{
483 struct drm_mm *mm = node->mm;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100484 struct drm_mm_node *prev_node;
Daniel Vetter709ea972010-07-02 15:02:16 +0100485
486 mm->scanned_blocks--;
487
488 BUG_ON(!node->scanned_block);
489 node->scanned_block = 0;
Daniel Vetter709ea972010-07-02 15:02:16 +0100490
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100491 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
492 node_list);
Daniel Vetter709ea972010-07-02 15:02:16 +0100493
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100494 prev_node->hole_follows = node->scanned_preceeds_hole;
495 INIT_LIST_HEAD(&node->node_list);
496 list_add(&node->node_list, &prev_node->node_list);
Daniel Vetter709ea972010-07-02 15:02:16 +0100497
498 /* Only need to check for containement because start&size for the
499 * complete resulting free block (not just the desired part) is
500 * stored. */
501 if (node->start >= mm->scan_hit_start &&
502 node->start + node->size
503 <= mm->scan_hit_start + mm->scan_hit_size) {
504 return 1;
505 }
506
507 return 0;
508}
509EXPORT_SYMBOL(drm_mm_scan_remove_block);
510
Dave Airlie55910512007-07-11 16:53:40 +1000511int drm_mm_clean(struct drm_mm * mm)
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100512{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100513 struct list_head *head = &mm->head_node.node_list;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100514
515 return (head->next->next == head);
516}
Jerome Glisse249d6042009-04-08 17:11:16 +0200517EXPORT_SYMBOL(drm_mm_clean);
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100518
Dave Airlie55910512007-07-11 16:53:40 +1000519int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000520{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100521 INIT_LIST_HEAD(&mm->hole_stack);
Jerome Glisse249d6042009-04-08 17:11:16 +0200522 INIT_LIST_HEAD(&mm->unused_nodes);
523 mm->num_unused = 0;
Daniel Vetter709ea972010-07-02 15:02:16 +0100524 mm->scanned_blocks = 0;
Jerome Glisse249d6042009-04-08 17:11:16 +0200525 spin_lock_init(&mm->unused_lock);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000526
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100527 /* Clever trick to avoid a special case in the free hole tracking. */
528 INIT_LIST_HEAD(&mm->head_node.node_list);
529 INIT_LIST_HEAD(&mm->head_node.hole_stack);
530 mm->head_node.hole_follows = 1;
531 mm->head_node.scanned_block = 0;
532 mm->head_node.scanned_prev_free = 0;
533 mm->head_node.scanned_next_free = 0;
534 mm->head_node.mm = mm;
535 mm->head_node.start = start + size;
536 mm->head_node.size = start - mm->head_node.start;
537 list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
538
539 return 0;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000540}
Eric Anholt673a3942008-07-30 12:06:12 -0700541EXPORT_SYMBOL(drm_mm_init);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000542
Dave Airlie55910512007-07-11 16:53:40 +1000543void drm_mm_takedown(struct drm_mm * mm)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000544{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100545 struct drm_mm_node *entry, *next;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000546
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100547 if (!list_empty(&mm->head_node.node_list)) {
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000548 DRM_ERROR("Memory manager not clean. Delaying takedown\n");
549 return;
550 }
551
Jerome Glisse249d6042009-04-08 17:11:16 +0200552 spin_lock(&mm->unused_lock);
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100553 list_for_each_entry_safe(entry, next, &mm->unused_nodes, node_list) {
554 list_del(&entry->node_list);
Jerome Glisse249d6042009-04-08 17:11:16 +0200555 kfree(entry);
556 --mm->num_unused;
557 }
558 spin_unlock(&mm->unused_lock);
559
560 BUG_ON(mm->num_unused != 0);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000561}
Dave Airlief453ba02008-11-07 14:05:41 -0800562EXPORT_SYMBOL(drm_mm_takedown);
Dave Airliefa8a1232009-08-26 13:13:37 +1000563
Jerome Glisse99d7e482009-12-09 21:55:09 +0100564void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
565{
566 struct drm_mm_node *entry;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100567 unsigned long total_used = 0, total_free = 0, total = 0;
568 unsigned long hole_start, hole_end, hole_size;
Jerome Glisse99d7e482009-12-09 21:55:09 +0100569
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100570 hole_start = drm_mm_hole_node_start(&mm->head_node);
571 hole_end = drm_mm_hole_node_end(&mm->head_node);
572 hole_size = hole_end - hole_start;
573 if (hole_size)
574 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
575 prefix, hole_start, hole_end,
576 hole_size);
577 total_free += hole_size;
578
579 drm_mm_for_each_node(entry, mm) {
580 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: used\n",
Jerome Glisse99d7e482009-12-09 21:55:09 +0100581 prefix, entry->start, entry->start + entry->size,
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100582 entry->size);
583 total_used += entry->size;
584
585 if (entry->hole_follows) {
586 hole_start = drm_mm_hole_node_start(entry);
587 hole_end = drm_mm_hole_node_end(entry);
588 hole_size = hole_end - hole_start;
589 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
590 prefix, hole_start, hole_end,
591 hole_size);
592 total_free += hole_size;
593 }
Jerome Glisse99d7e482009-12-09 21:55:09 +0100594 }
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100595 total = total_free + total_used;
596
597 printk(KERN_DEBUG "%s total: %lu, used %lu free %lu\n", prefix, total,
Jerome Glisse99d7e482009-12-09 21:55:09 +0100598 total_used, total_free);
599}
600EXPORT_SYMBOL(drm_mm_debug_table);
601
Dave Airliefa8a1232009-08-26 13:13:37 +1000602#if defined(CONFIG_DEBUG_FS)
603int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
604{
605 struct drm_mm_node *entry;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100606 unsigned long total_used = 0, total_free = 0, total = 0;
607 unsigned long hole_start, hole_end, hole_size;
Dave Airliefa8a1232009-08-26 13:13:37 +1000608
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100609 hole_start = drm_mm_hole_node_start(&mm->head_node);
610 hole_end = drm_mm_hole_node_end(&mm->head_node);
611 hole_size = hole_end - hole_start;
612 if (hole_size)
613 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
614 hole_start, hole_end, hole_size);
615 total_free += hole_size;
616
617 drm_mm_for_each_node(entry, mm) {
618 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: used\n",
619 entry->start, entry->start + entry->size,
620 entry->size);
621 total_used += entry->size;
622 if (entry->hole_follows) {
623 hole_start = drm_mm_hole_node_start(&mm->head_node);
624 hole_end = drm_mm_hole_node_end(&mm->head_node);
625 hole_size = hole_end - hole_start;
626 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
627 hole_start, hole_end, hole_size);
628 total_free += hole_size;
629 }
Dave Airliefa8a1232009-08-26 13:13:37 +1000630 }
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100631 total = total_free + total_used;
632
633 seq_printf(m, "total: %lu, used %lu free %lu\n", total, total_used, total_free);
Dave Airliefa8a1232009-08-26 13:13:37 +1000634 return 0;
635}
636EXPORT_SYMBOL(drm_mm_dump_table);
637#endif