TTY: move allocations to tty_alloc_driver
[deliverable/linux.git] / drivers / gpu / drm / drm_mm.c
CommitLineData
3a1bd924
TH
1/**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 *
27 **************************************************************************/
28
29/*
30 * Generic simple memory manager implementation. Intended to be used as a base
31 * class implementation for more advanced memory managers.
32 *
33 * Note that the algorithm used is quite simple and there might be substantial
34 * performance gains if a smarter free list is implemented. Currently it is just an
35 * unordered stack of free regions. This could easily be improved if an RB-tree
36 * is used instead. At least if we expect heavy fragmentation.
37 *
38 * Aligned allocations can also see improvement.
39 *
40 * Authors:
96de0e25 41 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
3a1bd924
TH
42 */
43
44#include "drmP.h"
249d6048 45#include "drm_mm.h"
1d58420b 46#include <linux/slab.h>
fa8a1238 47#include <linux/seq_file.h>
2d1a8a48 48#include <linux/export.h>
1d58420b 49
249d6048
JG
50#define MM_UNUSED_TARGET 4
51
249d6048
JG
52static struct drm_mm_node *drm_mm_kmalloc(struct drm_mm *mm, int atomic)
53{
54 struct drm_mm_node *child;
55
56 if (atomic)
709ea971 57 child = kzalloc(sizeof(*child), GFP_ATOMIC);
249d6048 58 else
709ea971 59 child = kzalloc(sizeof(*child), GFP_KERNEL);
249d6048
JG
60
61 if (unlikely(child == NULL)) {
62 spin_lock(&mm->unused_lock);
63 if (list_empty(&mm->unused_nodes))
64 child = NULL;
65 else {
66 child =
67 list_entry(mm->unused_nodes.next,
ea7b1dd4
DV
68 struct drm_mm_node, node_list);
69 list_del(&child->node_list);
249d6048
JG
70 --mm->num_unused;
71 }
72 spin_unlock(&mm->unused_lock);
73 }
74 return child;
75}
76
a698cf34
JG
77/* drm_mm_pre_get() - pre allocate drm_mm_node structure
78 * drm_mm: memory manager struct we are pre-allocating for
79 *
80 * Returns 0 on success or -ENOMEM if allocation fails.
81 */
249d6048
JG
82int drm_mm_pre_get(struct drm_mm *mm)
83{
84 struct drm_mm_node *node;
85
86 spin_lock(&mm->unused_lock);
87 while (mm->num_unused < MM_UNUSED_TARGET) {
88 spin_unlock(&mm->unused_lock);
709ea971 89 node = kzalloc(sizeof(*node), GFP_KERNEL);
249d6048
JG
90 spin_lock(&mm->unused_lock);
91
92 if (unlikely(node == NULL)) {
93 int ret = (mm->num_unused < 2) ? -ENOMEM : 0;
94 spin_unlock(&mm->unused_lock);
95 return ret;
96 }
97 ++mm->num_unused;
ea7b1dd4 98 list_add_tail(&node->node_list, &mm->unused_nodes);
249d6048
JG
99 }
100 spin_unlock(&mm->unused_lock);
101 return 0;
102}
103EXPORT_SYMBOL(drm_mm_pre_get);
1d58420b 104
ea7b1dd4 105static inline unsigned long drm_mm_hole_node_start(struct drm_mm_node *hole_node)
1d58420b 106{
ea7b1dd4 107 return hole_node->start + hole_node->size;
1d58420b
TH
108}
109
ea7b1dd4 110static inline unsigned long drm_mm_hole_node_end(struct drm_mm_node *hole_node)
1d58420b 111{
ea7b1dd4
DV
112 struct drm_mm_node *next_node =
113 list_entry(hole_node->node_list.next, struct drm_mm_node,
114 node_list);
1d58420b 115
ea7b1dd4 116 return next_node->start;
1d58420b
TH
117}
118
9fc935de
DV
119static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
120 struct drm_mm_node *node,
6b9d89b4
CW
121 unsigned long size, unsigned alignment,
122 unsigned long color)
3a1bd924 123{
ea7b1dd4 124 struct drm_mm *mm = hole_node->mm;
ea7b1dd4
DV
125 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
126 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
6b9d89b4
CW
127 unsigned long adj_start = hole_start;
128 unsigned long adj_end = hole_end;
ea7b1dd4 129
b0b7af18
DV
130 BUG_ON(!hole_node->hole_follows || node->allocated);
131
6b9d89b4
CW
132 if (mm->color_adjust)
133 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
1d58420b 134
6b9d89b4
CW
135 if (alignment) {
136 unsigned tmp = adj_start % alignment;
137 if (tmp)
138 adj_start += alignment - tmp;
139 }
140
141 if (adj_start == hole_start) {
ea7b1dd4 142 hole_node->hole_follows = 0;
6b9d89b4
CW
143 list_del(&hole_node->hole_stack);
144 }
ea7b1dd4 145
6b9d89b4 146 node->start = adj_start;
ea7b1dd4
DV
147 node->size = size;
148 node->mm = mm;
6b9d89b4 149 node->color = color;
b0b7af18 150 node->allocated = 1;
3a1bd924 151
ea7b1dd4
DV
152 INIT_LIST_HEAD(&node->hole_stack);
153 list_add(&node->node_list, &hole_node->node_list);
154
6b9d89b4 155 BUG_ON(node->start + node->size > adj_end);
ea7b1dd4 156
6b9d89b4 157 node->hole_follows = 0;
ea7b1dd4
DV
158 if (node->start + node->size < hole_end) {
159 list_add(&node->hole_stack, &mm->hole_stack);
160 node->hole_follows = 1;
1d58420b 161 }
9fc935de
DV
162}
163
164struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *hole_node,
165 unsigned long size,
166 unsigned alignment,
6b9d89b4 167 unsigned long color,
9fc935de
DV
168 int atomic)
169{
170 struct drm_mm_node *node;
171
9fc935de
DV
172 node = drm_mm_kmalloc(hole_node->mm, atomic);
173 if (unlikely(node == NULL))
174 return NULL;
175
6b9d89b4 176 drm_mm_insert_helper(hole_node, node, size, alignment, color);
3a1bd924 177
e6c03c5b 178 return node;
3a1bd924 179}
89579f77 180EXPORT_SYMBOL(drm_mm_get_block_generic);
249d6048 181
b0b7af18
DV
182/**
183 * Search for free space and insert a preallocated memory node. Returns
184 * -ENOSPC if no suitable free area is available. The preallocated memory node
185 * must be cleared.
186 */
187int drm_mm_insert_node(struct drm_mm *mm, struct drm_mm_node *node,
188 unsigned long size, unsigned alignment)
189{
190 struct drm_mm_node *hole_node;
191
6b9d89b4 192 hole_node = drm_mm_search_free(mm, size, alignment, false);
b0b7af18
DV
193 if (!hole_node)
194 return -ENOSPC;
195
6b9d89b4 196 drm_mm_insert_helper(hole_node, node, size, alignment, 0);
b0b7af18
DV
197
198 return 0;
199}
200EXPORT_SYMBOL(drm_mm_insert_node);
201
9fc935de
DV
202static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
203 struct drm_mm_node *node,
204 unsigned long size, unsigned alignment,
6b9d89b4 205 unsigned long color,
9fc935de 206 unsigned long start, unsigned long end)
a2e68e92 207{
ea7b1dd4 208 struct drm_mm *mm = hole_node->mm;
ea7b1dd4
DV
209 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
210 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
6b9d89b4
CW
211 unsigned long adj_start = hole_start;
212 unsigned long adj_end = hole_end;
a2e68e92 213
b0b7af18
DV
214 BUG_ON(!hole_node->hole_follows || node->allocated);
215
6b9d89b4
CW
216 if (mm->color_adjust)
217 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
a2e68e92 218
6b9d89b4
CW
219 if (adj_start < start)
220 adj_start = start;
221
222 if (alignment) {
223 unsigned tmp = adj_start % alignment;
224 if (tmp)
225 adj_start += alignment - tmp;
226 }
ea7b1dd4 227
6b9d89b4 228 if (adj_start == hole_start) {
ea7b1dd4 229 hole_node->hole_follows = 0;
6b9d89b4 230 list_del(&hole_node->hole_stack);
a2e68e92
JG
231 }
232
6b9d89b4 233 node->start = adj_start;
ea7b1dd4
DV
234 node->size = size;
235 node->mm = mm;
6b9d89b4 236 node->color = color;
b0b7af18 237 node->allocated = 1;
ea7b1dd4
DV
238
239 INIT_LIST_HEAD(&node->hole_stack);
240 list_add(&node->node_list, &hole_node->node_list);
241
6b9d89b4 242 BUG_ON(node->start + node->size > adj_end);
ea7b1dd4
DV
243 BUG_ON(node->start + node->size > end);
244
6b9d89b4 245 node->hole_follows = 0;
ea7b1dd4
DV
246 if (node->start + node->size < hole_end) {
247 list_add(&node->hole_stack, &mm->hole_stack);
248 node->hole_follows = 1;
a2e68e92 249 }
9fc935de
DV
250}
251
252struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *hole_node,
253 unsigned long size,
254 unsigned alignment,
6b9d89b4 255 unsigned long color,
9fc935de
DV
256 unsigned long start,
257 unsigned long end,
258 int atomic)
259{
260 struct drm_mm_node *node;
261
9fc935de
DV
262 node = drm_mm_kmalloc(hole_node->mm, atomic);
263 if (unlikely(node == NULL))
264 return NULL;
265
6b9d89b4 266 drm_mm_insert_helper_range(hole_node, node, size, alignment, color,
9fc935de 267 start, end);
a2e68e92 268
a2e68e92
JG
269 return node;
270}
271EXPORT_SYMBOL(drm_mm_get_block_range_generic);
272
b0b7af18
DV
273/**
274 * Search for free space and insert a preallocated memory node. Returns
275 * -ENOSPC if no suitable free area is available. This is for range
276 * restricted allocations. The preallocated memory node must be cleared.
3a1bd924 277 */
b0b7af18
DV
278int drm_mm_insert_node_in_range(struct drm_mm *mm, struct drm_mm_node *node,
279 unsigned long size, unsigned alignment,
280 unsigned long start, unsigned long end)
3a1bd924 281{
b0b7af18
DV
282 struct drm_mm_node *hole_node;
283
284 hole_node = drm_mm_search_free_in_range(mm, size, alignment,
6b9d89b4 285 start, end, false);
b0b7af18
DV
286 if (!hole_node)
287 return -ENOSPC;
288
6b9d89b4 289 drm_mm_insert_helper_range(hole_node, node, size, alignment, 0,
b0b7af18 290 start, end);
3a1bd924 291
b0b7af18
DV
292 return 0;
293}
294EXPORT_SYMBOL(drm_mm_insert_node_in_range);
295
296/**
297 * Remove a memory node from the allocator.
298 */
299void drm_mm_remove_node(struct drm_mm_node *node)
300{
ea7b1dd4
DV
301 struct drm_mm *mm = node->mm;
302 struct drm_mm_node *prev_node;
3a1bd924 303
ea7b1dd4
DV
304 BUG_ON(node->scanned_block || node->scanned_prev_free
305 || node->scanned_next_free);
3a1bd924 306
ea7b1dd4
DV
307 prev_node =
308 list_entry(node->node_list.prev, struct drm_mm_node, node_list);
709ea971 309
ea7b1dd4
DV
310 if (node->hole_follows) {
311 BUG_ON(drm_mm_hole_node_start(node)
312 == drm_mm_hole_node_end(node));
313 list_del(&node->hole_stack);
314 } else
315 BUG_ON(drm_mm_hole_node_start(node)
316 != drm_mm_hole_node_end(node));
249d6048 317
ea7b1dd4
DV
318 if (!prev_node->hole_follows) {
319 prev_node->hole_follows = 1;
320 list_add(&prev_node->hole_stack, &mm->hole_stack);
321 } else
322 list_move(&prev_node->hole_stack, &mm->hole_stack);
323
324 list_del(&node->node_list);
b0b7af18
DV
325 node->allocated = 0;
326}
327EXPORT_SYMBOL(drm_mm_remove_node);
328
329/*
330 * Remove a memory node from the allocator and free the allocated struct
331 * drm_mm_node. Only to be used on a struct drm_mm_node obtained by one of the
332 * drm_mm_get_block functions.
333 */
334void drm_mm_put_block(struct drm_mm_node *node)
335{
336
337 struct drm_mm *mm = node->mm;
338
339 drm_mm_remove_node(node);
340
ea7b1dd4
DV
341 spin_lock(&mm->unused_lock);
342 if (mm->num_unused < MM_UNUSED_TARGET) {
343 list_add(&node->node_list, &mm->unused_nodes);
344 ++mm->num_unused;
345 } else
346 kfree(node);
347 spin_unlock(&mm->unused_lock);
348}
673a394b 349EXPORT_SYMBOL(drm_mm_put_block);
3a1bd924 350
75214733
DV
351static int check_free_hole(unsigned long start, unsigned long end,
352 unsigned long size, unsigned alignment)
7a6b2896 353{
75214733 354 if (end - start < size)
7a6b2896
DV
355 return 0;
356
357 if (alignment) {
75214733 358 unsigned tmp = start % alignment;
7a6b2896 359 if (tmp)
6b9d89b4 360 start += alignment - tmp;
7a6b2896
DV
361 }
362
6b9d89b4 363 return end >= start + size;
7a6b2896
DV
364}
365
6b9d89b4
CW
366struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
367 unsigned long size,
368 unsigned alignment,
369 unsigned long color,
370 bool best_match)
3a1bd924 371{
55910517
DA
372 struct drm_mm_node *entry;
373 struct drm_mm_node *best;
3a1bd924
TH
374 unsigned long best_size;
375
709ea971
DV
376 BUG_ON(mm->scanned_blocks);
377
3a1bd924
TH
378 best = NULL;
379 best_size = ~0UL;
380
ea7b1dd4 381 list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
6b9d89b4
CW
382 unsigned long adj_start = drm_mm_hole_node_start(entry);
383 unsigned long adj_end = drm_mm_hole_node_end(entry);
384
385 if (mm->color_adjust) {
386 mm->color_adjust(entry, color, &adj_start, &adj_end);
387 if (adj_end <= adj_start)
388 continue;
389 }
390
ea7b1dd4 391 BUG_ON(!entry->hole_follows);
6b9d89b4 392 if (!check_free_hole(adj_start, adj_end, size, alignment))
1d58420b
TH
393 continue;
394
7a6b2896
DV
395 if (!best_match)
396 return entry;
1d58420b 397
7a6b2896
DV
398 if (entry->size < best_size) {
399 best = entry;
400 best_size = entry->size;
3a1bd924
TH
401 }
402 }
403
404 return best;
405}
6b9d89b4
CW
406EXPORT_SYMBOL(drm_mm_search_free_generic);
407
408struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
409 unsigned long size,
410 unsigned alignment,
411 unsigned long color,
412 unsigned long start,
413 unsigned long end,
414 bool best_match)
a2e68e92 415{
a2e68e92
JG
416 struct drm_mm_node *entry;
417 struct drm_mm_node *best;
418 unsigned long best_size;
a2e68e92 419
709ea971
DV
420 BUG_ON(mm->scanned_blocks);
421
a2e68e92
JG
422 best = NULL;
423 best_size = ~0UL;
424
ea7b1dd4
DV
425 list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
426 unsigned long adj_start = drm_mm_hole_node_start(entry) < start ?
427 start : drm_mm_hole_node_start(entry);
428 unsigned long adj_end = drm_mm_hole_node_end(entry) > end ?
429 end : drm_mm_hole_node_end(entry);
a2e68e92 430
ea7b1dd4 431 BUG_ON(!entry->hole_follows);
6b9d89b4
CW
432
433 if (mm->color_adjust) {
434 mm->color_adjust(entry, color, &adj_start, &adj_end);
435 if (adj_end <= adj_start)
436 continue;
437 }
438
75214733 439 if (!check_free_hole(adj_start, adj_end, size, alignment))
a2e68e92
JG
440 continue;
441
7a6b2896
DV
442 if (!best_match)
443 return entry;
a2e68e92 444
7a6b2896
DV
445 if (entry->size < best_size) {
446 best = entry;
447 best_size = entry->size;
a2e68e92
JG
448 }
449 }
450
451 return best;
452}
6b9d89b4 453EXPORT_SYMBOL(drm_mm_search_free_in_range_generic);
a2e68e92 454
b0b7af18
DV
455/**
456 * Moves an allocation. To be used with embedded struct drm_mm_node.
457 */
458void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
459{
460 list_replace(&old->node_list, &new->node_list);
2bbd4492 461 list_replace(&old->hole_stack, &new->hole_stack);
b0b7af18
DV
462 new->hole_follows = old->hole_follows;
463 new->mm = old->mm;
464 new->start = old->start;
465 new->size = old->size;
6b9d89b4 466 new->color = old->color;
b0b7af18
DV
467
468 old->allocated = 0;
469 new->allocated = 1;
470}
471EXPORT_SYMBOL(drm_mm_replace_node);
472
709ea971
DV
473/**
474 * Initializa lru scanning.
475 *
476 * This simply sets up the scanning routines with the parameters for the desired
477 * hole.
478 *
479 * Warning: As long as the scan list is non-empty, no other operations than
480 * adding/removing nodes to/from the scan list are allowed.
481 */
6b9d89b4
CW
482void drm_mm_init_scan(struct drm_mm *mm,
483 unsigned long size,
484 unsigned alignment,
485 unsigned long color)
709ea971 486{
6b9d89b4 487 mm->scan_color = color;
709ea971
DV
488 mm->scan_alignment = alignment;
489 mm->scan_size = size;
490 mm->scanned_blocks = 0;
491 mm->scan_hit_start = 0;
492 mm->scan_hit_size = 0;
d935cc61 493 mm->scan_check_range = 0;
ae0cec28 494 mm->prev_scanned_node = NULL;
709ea971
DV
495}
496EXPORT_SYMBOL(drm_mm_init_scan);
497
d935cc61
DV
498/**
499 * Initializa lru scanning.
500 *
501 * This simply sets up the scanning routines with the parameters for the desired
502 * hole. This version is for range-restricted scans.
503 *
504 * Warning: As long as the scan list is non-empty, no other operations than
505 * adding/removing nodes to/from the scan list are allowed.
506 */
6b9d89b4
CW
507void drm_mm_init_scan_with_range(struct drm_mm *mm,
508 unsigned long size,
d935cc61 509 unsigned alignment,
6b9d89b4 510 unsigned long color,
d935cc61
DV
511 unsigned long start,
512 unsigned long end)
513{
6b9d89b4 514 mm->scan_color = color;
d935cc61
DV
515 mm->scan_alignment = alignment;
516 mm->scan_size = size;
517 mm->scanned_blocks = 0;
518 mm->scan_hit_start = 0;
519 mm->scan_hit_size = 0;
520 mm->scan_start = start;
521 mm->scan_end = end;
522 mm->scan_check_range = 1;
ae0cec28 523 mm->prev_scanned_node = NULL;
d935cc61
DV
524}
525EXPORT_SYMBOL(drm_mm_init_scan_with_range);
526
709ea971
DV
527/**
528 * Add a node to the scan list that might be freed to make space for the desired
529 * hole.
530 *
531 * Returns non-zero, if a hole has been found, zero otherwise.
532 */
533int drm_mm_scan_add_block(struct drm_mm_node *node)
534{
535 struct drm_mm *mm = node->mm;
ea7b1dd4
DV
536 struct drm_mm_node *prev_node;
537 unsigned long hole_start, hole_end;
d935cc61
DV
538 unsigned long adj_start;
539 unsigned long adj_end;
709ea971
DV
540
541 mm->scanned_blocks++;
542
ea7b1dd4 543 BUG_ON(node->scanned_block);
709ea971 544 node->scanned_block = 1;
709ea971 545
ea7b1dd4
DV
546 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
547 node_list);
709ea971 548
ea7b1dd4
DV
549 node->scanned_preceeds_hole = prev_node->hole_follows;
550 prev_node->hole_follows = 1;
551 list_del(&node->node_list);
552 node->node_list.prev = &prev_node->node_list;
ae0cec28
DV
553 node->node_list.next = &mm->prev_scanned_node->node_list;
554 mm->prev_scanned_node = node;
709ea971 555
ea7b1dd4
DV
556 hole_start = drm_mm_hole_node_start(prev_node);
557 hole_end = drm_mm_hole_node_end(prev_node);
6b9d89b4
CW
558
559 adj_start = hole_start;
560 adj_end = hole_end;
561
562 if (mm->color_adjust)
563 mm->color_adjust(prev_node, mm->scan_color, &adj_start, &adj_end);
564
d935cc61 565 if (mm->scan_check_range) {
6b9d89b4
CW
566 if (adj_start < mm->scan_start)
567 adj_start = mm->scan_start;
568 if (adj_end > mm->scan_end)
569 adj_end = mm->scan_end;
d935cc61
DV
570 }
571
6b9d89b4 572 if (check_free_hole(adj_start, adj_end,
75214733 573 mm->scan_size, mm->scan_alignment)) {
ea7b1dd4
DV
574 mm->scan_hit_start = hole_start;
575 mm->scan_hit_size = hole_end;
709ea971
DV
576
577 return 1;
578 }
579
580 return 0;
581}
582EXPORT_SYMBOL(drm_mm_scan_add_block);
583
584/**
585 * Remove a node from the scan list.
586 *
587 * Nodes _must_ be removed in the exact same order from the scan list as they
588 * have been added, otherwise the internal state of the memory manager will be
589 * corrupted.
590 *
591 * When the scan list is empty, the selected memory nodes can be freed. An
25985edc 592 * immediately following drm_mm_search_free with best_match = 0 will then return
709ea971
DV
593 * the just freed block (because its at the top of the free_stack list).
594 *
595 * Returns one if this block should be evicted, zero otherwise. Will always
596 * return zero when no hole has been found.
597 */
598int drm_mm_scan_remove_block(struct drm_mm_node *node)
599{
600 struct drm_mm *mm = node->mm;
ea7b1dd4 601 struct drm_mm_node *prev_node;
709ea971
DV
602
603 mm->scanned_blocks--;
604
605 BUG_ON(!node->scanned_block);
606 node->scanned_block = 0;
709ea971 607
ea7b1dd4
DV
608 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
609 node_list);
709ea971 610
ea7b1dd4
DV
611 prev_node->hole_follows = node->scanned_preceeds_hole;
612 INIT_LIST_HEAD(&node->node_list);
613 list_add(&node->node_list, &prev_node->node_list);
709ea971
DV
614
615 /* Only need to check for containement because start&size for the
616 * complete resulting free block (not just the desired part) is
617 * stored. */
618 if (node->start >= mm->scan_hit_start &&
619 node->start + node->size
620 <= mm->scan_hit_start + mm->scan_hit_size) {
621 return 1;
622 }
623
624 return 0;
625}
626EXPORT_SYMBOL(drm_mm_scan_remove_block);
627
55910517 628int drm_mm_clean(struct drm_mm * mm)
3a1bd924 629{
ea7b1dd4 630 struct list_head *head = &mm->head_node.node_list;
3a1bd924 631
1d58420b
TH
632 return (head->next->next == head);
633}
249d6048 634EXPORT_SYMBOL(drm_mm_clean);
3a1bd924 635
55910517 636int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
1d58420b 637{
ea7b1dd4 638 INIT_LIST_HEAD(&mm->hole_stack);
249d6048
JG
639 INIT_LIST_HEAD(&mm->unused_nodes);
640 mm->num_unused = 0;
709ea971 641 mm->scanned_blocks = 0;
249d6048 642 spin_lock_init(&mm->unused_lock);
3a1bd924 643
ea7b1dd4
DV
644 /* Clever trick to avoid a special case in the free hole tracking. */
645 INIT_LIST_HEAD(&mm->head_node.node_list);
646 INIT_LIST_HEAD(&mm->head_node.hole_stack);
647 mm->head_node.hole_follows = 1;
648 mm->head_node.scanned_block = 0;
649 mm->head_node.scanned_prev_free = 0;
650 mm->head_node.scanned_next_free = 0;
651 mm->head_node.mm = mm;
652 mm->head_node.start = start + size;
653 mm->head_node.size = start - mm->head_node.start;
654 list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
655
6b9d89b4
CW
656 mm->color_adjust = NULL;
657
ea7b1dd4 658 return 0;
3a1bd924 659}
673a394b 660EXPORT_SYMBOL(drm_mm_init);
3a1bd924 661
55910517 662void drm_mm_takedown(struct drm_mm * mm)
3a1bd924 663{
ea7b1dd4 664 struct drm_mm_node *entry, *next;
3a1bd924 665
ea7b1dd4 666 if (!list_empty(&mm->head_node.node_list)) {
3a1bd924
TH
667 DRM_ERROR("Memory manager not clean. Delaying takedown\n");
668 return;
669 }
670
249d6048 671 spin_lock(&mm->unused_lock);
ea7b1dd4
DV
672 list_for_each_entry_safe(entry, next, &mm->unused_nodes, node_list) {
673 list_del(&entry->node_list);
249d6048
JG
674 kfree(entry);
675 --mm->num_unused;
676 }
677 spin_unlock(&mm->unused_lock);
3a1bd924 678
249d6048 679 BUG_ON(mm->num_unused != 0);
3a1bd924 680}
f453ba04 681EXPORT_SYMBOL(drm_mm_takedown);
fa8a1238 682
99d7e48e
JG
683void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
684{
685 struct drm_mm_node *entry;
ea7b1dd4
DV
686 unsigned long total_used = 0, total_free = 0, total = 0;
687 unsigned long hole_start, hole_end, hole_size;
688
689 hole_start = drm_mm_hole_node_start(&mm->head_node);
690 hole_end = drm_mm_hole_node_end(&mm->head_node);
691 hole_size = hole_end - hole_start;
692 if (hole_size)
693 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
694 prefix, hole_start, hole_end,
695 hole_size);
696 total_free += hole_size;
697
698 drm_mm_for_each_node(entry, mm) {
699 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: used\n",
99d7e48e 700 prefix, entry->start, entry->start + entry->size,
ea7b1dd4
DV
701 entry->size);
702 total_used += entry->size;
703
704 if (entry->hole_follows) {
705 hole_start = drm_mm_hole_node_start(entry);
706 hole_end = drm_mm_hole_node_end(entry);
707 hole_size = hole_end - hole_start;
708 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
709 prefix, hole_start, hole_end,
710 hole_size);
711 total_free += hole_size;
712 }
99d7e48e 713 }
ea7b1dd4
DV
714 total = total_free + total_used;
715
716 printk(KERN_DEBUG "%s total: %lu, used %lu free %lu\n", prefix, total,
99d7e48e
JG
717 total_used, total_free);
718}
719EXPORT_SYMBOL(drm_mm_debug_table);
720
fa8a1238
DA
721#if defined(CONFIG_DEBUG_FS)
722int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
723{
724 struct drm_mm_node *entry;
ea7b1dd4
DV
725 unsigned long total_used = 0, total_free = 0, total = 0;
726 unsigned long hole_start, hole_end, hole_size;
727
728 hole_start = drm_mm_hole_node_start(&mm->head_node);
729 hole_end = drm_mm_hole_node_end(&mm->head_node);
730 hole_size = hole_end - hole_start;
731 if (hole_size)
732 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
733 hole_start, hole_end, hole_size);
734 total_free += hole_size;
735
736 drm_mm_for_each_node(entry, mm) {
737 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: used\n",
738 entry->start, entry->start + entry->size,
739 entry->size);
740 total_used += entry->size;
741 if (entry->hole_follows) {
2bbd4492
DV
742 hole_start = drm_mm_hole_node_start(entry);
743 hole_end = drm_mm_hole_node_end(entry);
ea7b1dd4
DV
744 hole_size = hole_end - hole_start;
745 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
746 hole_start, hole_end, hole_size);
747 total_free += hole_size;
748 }
fa8a1238 749 }
ea7b1dd4
DV
750 total = total_free + total_used;
751
752 seq_printf(m, "total: %lu, used %lu free %lu\n", total, total_used, total_free);
fa8a1238
DA
753 return 0;
754}
755EXPORT_SYMBOL(drm_mm_dump_table);
756#endif
This page took 0.522844 seconds and 5 git commands to generate.