drm/i915: fix how we mask PMIMR when adding work to the queue
[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
760285e7
DH
44#include <drm/drmP.h>
45#include <drm/drm_mm.h>
1d58420b 46#include <linux/slab.h>
fa8a1238 47#include <linux/seq_file.h>
2d1a8a48 48#include <linux/export.h>
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
9fc935de
DV
105static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
106 struct drm_mm_node *node,
6b9d89b4
CW
107 unsigned long size, unsigned alignment,
108 unsigned long color)
3a1bd924 109{
ea7b1dd4 110 struct drm_mm *mm = hole_node->mm;
ea7b1dd4
DV
111 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
112 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
6b9d89b4
CW
113 unsigned long adj_start = hole_start;
114 unsigned long adj_end = hole_end;
ea7b1dd4 115
9e8944ab 116 BUG_ON(node->allocated);
b0b7af18 117
6b9d89b4
CW
118 if (mm->color_adjust)
119 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
1d58420b 120
6b9d89b4
CW
121 if (alignment) {
122 unsigned tmp = adj_start % alignment;
123 if (tmp)
124 adj_start += alignment - tmp;
125 }
126
127 if (adj_start == hole_start) {
ea7b1dd4 128 hole_node->hole_follows = 0;
6b9d89b4
CW
129 list_del(&hole_node->hole_stack);
130 }
ea7b1dd4 131
6b9d89b4 132 node->start = adj_start;
ea7b1dd4
DV
133 node->size = size;
134 node->mm = mm;
6b9d89b4 135 node->color = color;
b0b7af18 136 node->allocated = 1;
3a1bd924 137
ea7b1dd4
DV
138 INIT_LIST_HEAD(&node->hole_stack);
139 list_add(&node->node_list, &hole_node->node_list);
140
6b9d89b4 141 BUG_ON(node->start + node->size > adj_end);
ea7b1dd4 142
6b9d89b4 143 node->hole_follows = 0;
9e8944ab 144 if (__drm_mm_hole_node_start(node) < hole_end) {
ea7b1dd4
DV
145 list_add(&node->hole_stack, &mm->hole_stack);
146 node->hole_follows = 1;
1d58420b 147 }
9fc935de
DV
148}
149
338710e7 150int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
5973c7ee 151{
b3a070cc 152 struct drm_mm_node *hole;
338710e7 153 unsigned long end = node->start + node->size;
9e8944ab
CW
154 unsigned long hole_start;
155 unsigned long hole_end;
5973c7ee 156
338710e7
BW
157 BUG_ON(node == NULL);
158
159 /* Find the relevant hole to add our node to */
9e8944ab 160 drm_mm_for_each_hole(hole, mm, hole_start, hole_end) {
338710e7 161 if (hole_start > node->start || hole_end < end)
5973c7ee
CW
162 continue;
163
5973c7ee
CW
164 node->mm = mm;
165 node->allocated = 1;
166
167 INIT_LIST_HEAD(&node->hole_stack);
168 list_add(&node->node_list, &hole->node_list);
169
338710e7 170 if (node->start == hole_start) {
5973c7ee
CW
171 hole->hole_follows = 0;
172 list_del_init(&hole->hole_stack);
173 }
174
175 node->hole_follows = 0;
176 if (end != hole_end) {
177 list_add(&node->hole_stack, &mm->hole_stack);
178 node->hole_follows = 1;
179 }
180
b3a070cc 181 return 0;
5973c7ee
CW
182 }
183
338710e7
BW
184 WARN(1, "no hole found for node 0x%lx + 0x%lx\n",
185 node->start, node->size);
b3a070cc 186 return -ENOSPC;
5973c7ee 187}
338710e7 188EXPORT_SYMBOL(drm_mm_reserve_node);
5973c7ee 189
9fc935de
DV
190struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *hole_node,
191 unsigned long size,
192 unsigned alignment,
6b9d89b4 193 unsigned long color,
9fc935de
DV
194 int atomic)
195{
196 struct drm_mm_node *node;
197
9fc935de
DV
198 node = drm_mm_kmalloc(hole_node->mm, atomic);
199 if (unlikely(node == NULL))
200 return NULL;
201
6b9d89b4 202 drm_mm_insert_helper(hole_node, node, size, alignment, color);
3a1bd924 203
e6c03c5b 204 return node;
3a1bd924 205}
89579f77 206EXPORT_SYMBOL(drm_mm_get_block_generic);
249d6048 207
b0b7af18
DV
208/**
209 * Search for free space and insert a preallocated memory node. Returns
210 * -ENOSPC if no suitable free area is available. The preallocated memory node
211 * must be cleared.
212 */
b8103450
CW
213int drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
214 unsigned long size, unsigned alignment,
215 unsigned long color)
b0b7af18
DV
216{
217 struct drm_mm_node *hole_node;
218
b8103450
CW
219 hole_node = drm_mm_search_free_generic(mm, size, alignment,
220 color, 0);
b0b7af18
DV
221 if (!hole_node)
222 return -ENOSPC;
223
b8103450 224 drm_mm_insert_helper(hole_node, node, size, alignment, color);
b0b7af18
DV
225 return 0;
226}
b8103450
CW
227EXPORT_SYMBOL(drm_mm_insert_node_generic);
228
229int drm_mm_insert_node(struct drm_mm *mm, struct drm_mm_node *node,
230 unsigned long size, unsigned alignment)
231{
232 return drm_mm_insert_node_generic(mm, node, size, alignment, 0);
233}
b0b7af18
DV
234EXPORT_SYMBOL(drm_mm_insert_node);
235
9fc935de
DV
236static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
237 struct drm_mm_node *node,
238 unsigned long size, unsigned alignment,
6b9d89b4 239 unsigned long color,
9fc935de 240 unsigned long start, unsigned long end)
a2e68e92 241{
ea7b1dd4 242 struct drm_mm *mm = hole_node->mm;
ea7b1dd4
DV
243 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
244 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
6b9d89b4
CW
245 unsigned long adj_start = hole_start;
246 unsigned long adj_end = hole_end;
a2e68e92 247
b0b7af18
DV
248 BUG_ON(!hole_node->hole_follows || node->allocated);
249
6b9d89b4
CW
250 if (adj_start < start)
251 adj_start = start;
901593f2
CW
252 if (adj_end > end)
253 adj_end = end;
254
255 if (mm->color_adjust)
256 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
6b9d89b4
CW
257
258 if (alignment) {
259 unsigned tmp = adj_start % alignment;
260 if (tmp)
261 adj_start += alignment - tmp;
262 }
ea7b1dd4 263
6b9d89b4 264 if (adj_start == hole_start) {
ea7b1dd4 265 hole_node->hole_follows = 0;
6b9d89b4 266 list_del(&hole_node->hole_stack);
a2e68e92
JG
267 }
268
6b9d89b4 269 node->start = adj_start;
ea7b1dd4
DV
270 node->size = size;
271 node->mm = mm;
6b9d89b4 272 node->color = color;
b0b7af18 273 node->allocated = 1;
ea7b1dd4
DV
274
275 INIT_LIST_HEAD(&node->hole_stack);
276 list_add(&node->node_list, &hole_node->node_list);
277
6b9d89b4 278 BUG_ON(node->start + node->size > adj_end);
ea7b1dd4
DV
279 BUG_ON(node->start + node->size > end);
280
6b9d89b4 281 node->hole_follows = 0;
9e8944ab 282 if (__drm_mm_hole_node_start(node) < hole_end) {
ea7b1dd4
DV
283 list_add(&node->hole_stack, &mm->hole_stack);
284 node->hole_follows = 1;
a2e68e92 285 }
9fc935de
DV
286}
287
288struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *hole_node,
289 unsigned long size,
290 unsigned alignment,
6b9d89b4 291 unsigned long color,
9fc935de
DV
292 unsigned long start,
293 unsigned long end,
294 int atomic)
295{
296 struct drm_mm_node *node;
297
9fc935de
DV
298 node = drm_mm_kmalloc(hole_node->mm, atomic);
299 if (unlikely(node == NULL))
300 return NULL;
301
6b9d89b4 302 drm_mm_insert_helper_range(hole_node, node, size, alignment, color,
9fc935de 303 start, end);
a2e68e92 304
a2e68e92
JG
305 return node;
306}
307EXPORT_SYMBOL(drm_mm_get_block_range_generic);
308
b0b7af18
DV
309/**
310 * Search for free space and insert a preallocated memory node. Returns
311 * -ENOSPC if no suitable free area is available. This is for range
312 * restricted allocations. The preallocated memory node must be cleared.
3a1bd924 313 */
b8103450
CW
314int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
315 unsigned long size, unsigned alignment, unsigned long color,
316 unsigned long start, unsigned long end)
3a1bd924 317{
b0b7af18
DV
318 struct drm_mm_node *hole_node;
319
b8103450
CW
320 hole_node = drm_mm_search_free_in_range_generic(mm,
321 size, alignment, color,
322 start, end, 0);
b0b7af18
DV
323 if (!hole_node)
324 return -ENOSPC;
325
b8103450
CW
326 drm_mm_insert_helper_range(hole_node, node,
327 size, alignment, color,
b0b7af18 328 start, end);
b0b7af18
DV
329 return 0;
330}
b8103450
CW
331EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
332
333int drm_mm_insert_node_in_range(struct drm_mm *mm, struct drm_mm_node *node,
334 unsigned long size, unsigned alignment,
335 unsigned long start, unsigned long end)
336{
337 return drm_mm_insert_node_in_range_generic(mm, node, size, alignment, 0, start, end);
338}
b0b7af18
DV
339EXPORT_SYMBOL(drm_mm_insert_node_in_range);
340
341/**
342 * Remove a memory node from the allocator.
343 */
344void drm_mm_remove_node(struct drm_mm_node *node)
345{
ea7b1dd4
DV
346 struct drm_mm *mm = node->mm;
347 struct drm_mm_node *prev_node;
3a1bd924 348
3ef80a81
BW
349 if (WARN_ON(!node->allocated))
350 return;
351
ea7b1dd4
DV
352 BUG_ON(node->scanned_block || node->scanned_prev_free
353 || node->scanned_next_free);
3a1bd924 354
ea7b1dd4
DV
355 prev_node =
356 list_entry(node->node_list.prev, struct drm_mm_node, node_list);
709ea971 357
ea7b1dd4 358 if (node->hole_follows) {
9e8944ab
CW
359 BUG_ON(__drm_mm_hole_node_start(node) ==
360 __drm_mm_hole_node_end(node));
ea7b1dd4
DV
361 list_del(&node->hole_stack);
362 } else
9e8944ab
CW
363 BUG_ON(__drm_mm_hole_node_start(node) !=
364 __drm_mm_hole_node_end(node));
365
249d6048 366
ea7b1dd4
DV
367 if (!prev_node->hole_follows) {
368 prev_node->hole_follows = 1;
369 list_add(&prev_node->hole_stack, &mm->hole_stack);
370 } else
371 list_move(&prev_node->hole_stack, &mm->hole_stack);
372
373 list_del(&node->node_list);
b0b7af18
DV
374 node->allocated = 0;
375}
376EXPORT_SYMBOL(drm_mm_remove_node);
377
378/*
379 * Remove a memory node from the allocator and free the allocated struct
380 * drm_mm_node. Only to be used on a struct drm_mm_node obtained by one of the
381 * drm_mm_get_block functions.
382 */
383void drm_mm_put_block(struct drm_mm_node *node)
384{
385
386 struct drm_mm *mm = node->mm;
387
388 drm_mm_remove_node(node);
389
ea7b1dd4
DV
390 spin_lock(&mm->unused_lock);
391 if (mm->num_unused < MM_UNUSED_TARGET) {
392 list_add(&node->node_list, &mm->unused_nodes);
393 ++mm->num_unused;
394 } else
395 kfree(node);
396 spin_unlock(&mm->unused_lock);
397}
673a394b 398EXPORT_SYMBOL(drm_mm_put_block);
3a1bd924 399
75214733
DV
400static int check_free_hole(unsigned long start, unsigned long end,
401 unsigned long size, unsigned alignment)
7a6b2896 402{
75214733 403 if (end - start < size)
7a6b2896
DV
404 return 0;
405
406 if (alignment) {
75214733 407 unsigned tmp = start % alignment;
7a6b2896 408 if (tmp)
6b9d89b4 409 start += alignment - tmp;
7a6b2896
DV
410 }
411
6b9d89b4 412 return end >= start + size;
7a6b2896
DV
413}
414
6b9d89b4
CW
415struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
416 unsigned long size,
417 unsigned alignment,
418 unsigned long color,
419 bool best_match)
3a1bd924 420{
55910517
DA
421 struct drm_mm_node *entry;
422 struct drm_mm_node *best;
9e8944ab
CW
423 unsigned long adj_start;
424 unsigned long adj_end;
3a1bd924
TH
425 unsigned long best_size;
426
709ea971
DV
427 BUG_ON(mm->scanned_blocks);
428
3a1bd924
TH
429 best = NULL;
430 best_size = ~0UL;
431
9e8944ab 432 drm_mm_for_each_hole(entry, mm, adj_start, adj_end) {
6b9d89b4
CW
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
6b9d89b4 439 if (!check_free_hole(adj_start, adj_end, size, alignment))
1d58420b
TH
440 continue;
441
7a6b2896
DV
442 if (!best_match)
443 return entry;
1d58420b 444
7a6b2896
DV
445 if (entry->size < best_size) {
446 best = entry;
447 best_size = entry->size;
3a1bd924
TH
448 }
449 }
450
451 return best;
452}
6b9d89b4
CW
453EXPORT_SYMBOL(drm_mm_search_free_generic);
454
455struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
456 unsigned long size,
457 unsigned alignment,
458 unsigned long color,
459 unsigned long start,
460 unsigned long end,
461 bool best_match)
a2e68e92 462{
a2e68e92
JG
463 struct drm_mm_node *entry;
464 struct drm_mm_node *best;
9e8944ab
CW
465 unsigned long adj_start;
466 unsigned long adj_end;
a2e68e92 467 unsigned long best_size;
a2e68e92 468
709ea971
DV
469 BUG_ON(mm->scanned_blocks);
470
a2e68e92
JG
471 best = NULL;
472 best_size = ~0UL;
473
9e8944ab
CW
474 drm_mm_for_each_hole(entry, mm, adj_start, adj_end) {
475 if (adj_start < start)
476 adj_start = start;
477 if (adj_end > end)
478 adj_end = end;
6b9d89b4
CW
479
480 if (mm->color_adjust) {
481 mm->color_adjust(entry, color, &adj_start, &adj_end);
482 if (adj_end <= adj_start)
483 continue;
484 }
485
75214733 486 if (!check_free_hole(adj_start, adj_end, size, alignment))
a2e68e92
JG
487 continue;
488
7a6b2896
DV
489 if (!best_match)
490 return entry;
a2e68e92 491
7a6b2896
DV
492 if (entry->size < best_size) {
493 best = entry;
494 best_size = entry->size;
a2e68e92
JG
495 }
496 }
497
498 return best;
499}
6b9d89b4 500EXPORT_SYMBOL(drm_mm_search_free_in_range_generic);
a2e68e92 501
b0b7af18
DV
502/**
503 * Moves an allocation. To be used with embedded struct drm_mm_node.
504 */
505void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
506{
507 list_replace(&old->node_list, &new->node_list);
2bbd4492 508 list_replace(&old->hole_stack, &new->hole_stack);
b0b7af18
DV
509 new->hole_follows = old->hole_follows;
510 new->mm = old->mm;
511 new->start = old->start;
512 new->size = old->size;
6b9d89b4 513 new->color = old->color;
b0b7af18
DV
514
515 old->allocated = 0;
516 new->allocated = 1;
517}
518EXPORT_SYMBOL(drm_mm_replace_node);
519
709ea971
DV
520/**
521 * Initializa lru scanning.
522 *
523 * This simply sets up the scanning routines with the parameters for the desired
524 * hole.
525 *
526 * Warning: As long as the scan list is non-empty, no other operations than
527 * adding/removing nodes to/from the scan list are allowed.
528 */
6b9d89b4
CW
529void drm_mm_init_scan(struct drm_mm *mm,
530 unsigned long size,
531 unsigned alignment,
532 unsigned long color)
709ea971 533{
6b9d89b4 534 mm->scan_color = color;
709ea971
DV
535 mm->scan_alignment = alignment;
536 mm->scan_size = size;
537 mm->scanned_blocks = 0;
538 mm->scan_hit_start = 0;
901593f2 539 mm->scan_hit_end = 0;
d935cc61 540 mm->scan_check_range = 0;
ae0cec28 541 mm->prev_scanned_node = NULL;
709ea971
DV
542}
543EXPORT_SYMBOL(drm_mm_init_scan);
544
d935cc61
DV
545/**
546 * Initializa lru scanning.
547 *
548 * This simply sets up the scanning routines with the parameters for the desired
549 * hole. This version is for range-restricted scans.
550 *
551 * Warning: As long as the scan list is non-empty, no other operations than
552 * adding/removing nodes to/from the scan list are allowed.
553 */
6b9d89b4
CW
554void drm_mm_init_scan_with_range(struct drm_mm *mm,
555 unsigned long size,
d935cc61 556 unsigned alignment,
6b9d89b4 557 unsigned long color,
d935cc61
DV
558 unsigned long start,
559 unsigned long end)
560{
6b9d89b4 561 mm->scan_color = color;
d935cc61
DV
562 mm->scan_alignment = alignment;
563 mm->scan_size = size;
564 mm->scanned_blocks = 0;
565 mm->scan_hit_start = 0;
901593f2 566 mm->scan_hit_end = 0;
d935cc61
DV
567 mm->scan_start = start;
568 mm->scan_end = end;
569 mm->scan_check_range = 1;
ae0cec28 570 mm->prev_scanned_node = NULL;
d935cc61
DV
571}
572EXPORT_SYMBOL(drm_mm_init_scan_with_range);
573
709ea971
DV
574/**
575 * Add a node to the scan list that might be freed to make space for the desired
576 * hole.
577 *
578 * Returns non-zero, if a hole has been found, zero otherwise.
579 */
580int drm_mm_scan_add_block(struct drm_mm_node *node)
581{
582 struct drm_mm *mm = node->mm;
ea7b1dd4
DV
583 struct drm_mm_node *prev_node;
584 unsigned long hole_start, hole_end;
901593f2 585 unsigned long adj_start, adj_end;
709ea971
DV
586
587 mm->scanned_blocks++;
588
ea7b1dd4 589 BUG_ON(node->scanned_block);
709ea971 590 node->scanned_block = 1;
709ea971 591
ea7b1dd4
DV
592 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
593 node_list);
709ea971 594
ea7b1dd4
DV
595 node->scanned_preceeds_hole = prev_node->hole_follows;
596 prev_node->hole_follows = 1;
597 list_del(&node->node_list);
598 node->node_list.prev = &prev_node->node_list;
ae0cec28
DV
599 node->node_list.next = &mm->prev_scanned_node->node_list;
600 mm->prev_scanned_node = node;
709ea971 601
901593f2
CW
602 adj_start = hole_start = drm_mm_hole_node_start(prev_node);
603 adj_end = hole_end = drm_mm_hole_node_end(prev_node);
6b9d89b4 604
d935cc61 605 if (mm->scan_check_range) {
6b9d89b4
CW
606 if (adj_start < mm->scan_start)
607 adj_start = mm->scan_start;
608 if (adj_end > mm->scan_end)
609 adj_end = mm->scan_end;
d935cc61
DV
610 }
611
901593f2
CW
612 if (mm->color_adjust)
613 mm->color_adjust(prev_node, mm->scan_color,
614 &adj_start, &adj_end);
615
6b9d89b4 616 if (check_free_hole(adj_start, adj_end,
75214733 617 mm->scan_size, mm->scan_alignment)) {
ea7b1dd4 618 mm->scan_hit_start = hole_start;
901593f2 619 mm->scan_hit_end = hole_end;
709ea971
DV
620 return 1;
621 }
622
623 return 0;
624}
625EXPORT_SYMBOL(drm_mm_scan_add_block);
626
627/**
628 * Remove a node from the scan list.
629 *
630 * Nodes _must_ be removed in the exact same order from the scan list as they
631 * have been added, otherwise the internal state of the memory manager will be
632 * corrupted.
633 *
634 * When the scan list is empty, the selected memory nodes can be freed. An
25985edc 635 * immediately following drm_mm_search_free with best_match = 0 will then return
709ea971
DV
636 * the just freed block (because its at the top of the free_stack list).
637 *
638 * Returns one if this block should be evicted, zero otherwise. Will always
639 * return zero when no hole has been found.
640 */
641int drm_mm_scan_remove_block(struct drm_mm_node *node)
642{
643 struct drm_mm *mm = node->mm;
ea7b1dd4 644 struct drm_mm_node *prev_node;
709ea971
DV
645
646 mm->scanned_blocks--;
647
648 BUG_ON(!node->scanned_block);
649 node->scanned_block = 0;
709ea971 650
ea7b1dd4
DV
651 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
652 node_list);
709ea971 653
ea7b1dd4 654 prev_node->hole_follows = node->scanned_preceeds_hole;
ea7b1dd4 655 list_add(&node->node_list, &prev_node->node_list);
709ea971 656
901593f2
CW
657 return (drm_mm_hole_node_end(node) > mm->scan_hit_start &&
658 node->start < mm->scan_hit_end);
709ea971
DV
659}
660EXPORT_SYMBOL(drm_mm_scan_remove_block);
661
55910517 662int drm_mm_clean(struct drm_mm * mm)
3a1bd924 663{
ea7b1dd4 664 struct list_head *head = &mm->head_node.node_list;
3a1bd924 665
1d58420b
TH
666 return (head->next->next == head);
667}
249d6048 668EXPORT_SYMBOL(drm_mm_clean);
3a1bd924 669
77ef8bbc 670void drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
1d58420b 671{
ea7b1dd4 672 INIT_LIST_HEAD(&mm->hole_stack);
249d6048
JG
673 INIT_LIST_HEAD(&mm->unused_nodes);
674 mm->num_unused = 0;
709ea971 675 mm->scanned_blocks = 0;
249d6048 676 spin_lock_init(&mm->unused_lock);
3a1bd924 677
ea7b1dd4
DV
678 /* Clever trick to avoid a special case in the free hole tracking. */
679 INIT_LIST_HEAD(&mm->head_node.node_list);
680 INIT_LIST_HEAD(&mm->head_node.hole_stack);
681 mm->head_node.hole_follows = 1;
682 mm->head_node.scanned_block = 0;
683 mm->head_node.scanned_prev_free = 0;
684 mm->head_node.scanned_next_free = 0;
685 mm->head_node.mm = mm;
686 mm->head_node.start = start + size;
687 mm->head_node.size = start - mm->head_node.start;
688 list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
689
6b9d89b4 690 mm->color_adjust = NULL;
3a1bd924 691}
673a394b 692EXPORT_SYMBOL(drm_mm_init);
3a1bd924 693
55910517 694void drm_mm_takedown(struct drm_mm * mm)
3a1bd924 695{
ea7b1dd4 696 struct drm_mm_node *entry, *next;
3a1bd924 697
e5ad449b
DV
698 if (WARN(!list_empty(&mm->head_node.node_list),
699 "Memory manager not clean. Delaying takedown\n")) {
3a1bd924
TH
700 return;
701 }
702
249d6048 703 spin_lock(&mm->unused_lock);
ea7b1dd4
DV
704 list_for_each_entry_safe(entry, next, &mm->unused_nodes, node_list) {
705 list_del(&entry->node_list);
249d6048
JG
706 kfree(entry);
707 --mm->num_unused;
708 }
709 spin_unlock(&mm->unused_lock);
3a1bd924 710
249d6048 711 BUG_ON(mm->num_unused != 0);
3a1bd924 712}
f453ba04 713EXPORT_SYMBOL(drm_mm_takedown);
fa8a1238 714
2c54b133
DV
715static unsigned long drm_mm_debug_hole(struct drm_mm_node *entry,
716 const char *prefix)
99d7e48e 717{
ea7b1dd4
DV
718 unsigned long hole_start, hole_end, hole_size;
719
2c54b133
DV
720 if (entry->hole_follows) {
721 hole_start = drm_mm_hole_node_start(entry);
722 hole_end = drm_mm_hole_node_end(entry);
723 hole_size = hole_end - hole_start;
ea7b1dd4
DV
724 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
725 prefix, hole_start, hole_end,
726 hole_size);
2c54b133
DV
727 return hole_size;
728 }
729
730 return 0;
731}
732
733void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
734{
735 struct drm_mm_node *entry;
736 unsigned long total_used = 0, total_free = 0, total = 0;
737
738 total_free += drm_mm_debug_hole(&mm->head_node, prefix);
ea7b1dd4
DV
739
740 drm_mm_for_each_node(entry, mm) {
741 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: used\n",
99d7e48e 742 prefix, entry->start, entry->start + entry->size,
ea7b1dd4
DV
743 entry->size);
744 total_used += entry->size;
2c54b133 745 total_free += drm_mm_debug_hole(entry, prefix);
99d7e48e 746 }
ea7b1dd4
DV
747 total = total_free + total_used;
748
749 printk(KERN_DEBUG "%s total: %lu, used %lu free %lu\n", prefix, total,
99d7e48e
JG
750 total_used, total_free);
751}
752EXPORT_SYMBOL(drm_mm_debug_table);
753
fa8a1238 754#if defined(CONFIG_DEBUG_FS)
3a359f0b 755static unsigned long drm_mm_dump_hole(struct seq_file *m, struct drm_mm_node *entry)
fa8a1238 756{
ea7b1dd4
DV
757 unsigned long hole_start, hole_end, hole_size;
758
3a359f0b
DV
759 if (entry->hole_follows) {
760 hole_start = drm_mm_hole_node_start(entry);
761 hole_end = drm_mm_hole_node_end(entry);
762 hole_size = hole_end - hole_start;
ea7b1dd4
DV
763 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: free\n",
764 hole_start, hole_end, hole_size);
3a359f0b
DV
765 return hole_size;
766 }
767
768 return 0;
769}
770
771int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
772{
773 struct drm_mm_node *entry;
774 unsigned long total_used = 0, total_free = 0, total = 0;
775
776 total_free += drm_mm_dump_hole(m, &mm->head_node);
ea7b1dd4
DV
777
778 drm_mm_for_each_node(entry, mm) {
779 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: used\n",
780 entry->start, entry->start + entry->size,
781 entry->size);
782 total_used += entry->size;
3a359f0b 783 total_free += drm_mm_dump_hole(m, entry);
fa8a1238 784 }
ea7b1dd4
DV
785 total = total_free + total_used;
786
787 seq_printf(m, "total: %lu, used %lu free %lu\n", total, total_used, total_free);
fa8a1238
DA
788 return 0;
789}
790EXPORT_SYMBOL(drm_mm_dump_table);
791#endif
This page took 0.69062 seconds and 5 git commands to generate.