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