blob: a5a7a16c4301deef8c5603e6051b83d9bcf655b6 [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)
56 child = kmalloc(sizeof(*child), GFP_ATOMIC);
57 else
58 child = kmalloc(sizeof(*child), GFP_KERNEL);
59
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,
67 struct drm_mm_node, fl_entry);
68 list_del(&child->fl_entry);
69 --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);
88 node = kmalloc(sizeof(*node), GFP_KERNEL);
89 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;
97 list_add_tail(&node->fl_entry, &mm->unused_nodes);
98 }
99 spin_unlock(&mm->unused_lock);
100 return 0;
101}
102EXPORT_SYMBOL(drm_mm_pre_get);
103
104static int drm_mm_create_tail_node(struct drm_mm *mm,
105 unsigned long start,
106 unsigned long size, int atomic)
107{
108 struct drm_mm_node *child;
109
110 child = drm_mm_kmalloc(mm, atomic);
111 if (unlikely(child == NULL))
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100112 return -ENOMEM;
113
114 child->free = 1;
115 child->size = size;
116 child->start = start;
117 child->mm = mm;
118
119 list_add_tail(&child->ml_entry, &mm->ml_entry);
120 list_add_tail(&child->fl_entry, &mm->fl_entry);
121
122 return 0;
123}
124
Dave Airlie55910512007-07-11 16:53:40 +1000125static struct drm_mm_node *drm_mm_split_at_start(struct drm_mm_node *parent,
Jerome Glisse249d6042009-04-08 17:11:16 +0200126 unsigned long size,
127 int atomic)
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100128{
Dave Airlie55910512007-07-11 16:53:40 +1000129 struct drm_mm_node *child;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100130
Jerome Glisse249d6042009-04-08 17:11:16 +0200131 child = drm_mm_kmalloc(parent->mm, atomic);
132 if (unlikely(child == NULL))
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100133 return NULL;
134
135 INIT_LIST_HEAD(&child->fl_entry);
136
137 child->free = 0;
138 child->size = size;
139 child->start = parent->start;
140 child->mm = parent->mm;
141
142 list_add_tail(&child->ml_entry, &parent->ml_entry);
143 INIT_LIST_HEAD(&child->fl_entry);
144
145 parent->size -= size;
146 parent->start += size;
147 return child;
148}
149
150
Thomas Hellstrom89579f72009-06-17 12:29:56 +0200151struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
152 unsigned long size,
153 unsigned alignment,
154 int atomic)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000155{
156
Dave Airlie55910512007-07-11 16:53:40 +1000157 struct drm_mm_node *align_splitoff = NULL;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100158 unsigned tmp = 0;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000159
160 if (alignment)
Chris Wilsone6c03c52009-05-22 14:14:22 +0100161 tmp = node->start % alignment;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100162
163 if (tmp) {
Jerome Glisse249d6042009-04-08 17:11:16 +0200164 align_splitoff =
Thomas Hellstrom89579f72009-06-17 12:29:56 +0200165 drm_mm_split_at_start(node, alignment - tmp, atomic);
Jerome Glisse249d6042009-04-08 17:11:16 +0200166 if (unlikely(align_splitoff == NULL))
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100167 return NULL;
168 }
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000169
Chris Wilsone6c03c52009-05-22 14:14:22 +0100170 if (node->size == size) {
171 list_del_init(&node->fl_entry);
172 node->free = 0;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000173 } else {
Thomas Hellstrom89579f72009-06-17 12:29:56 +0200174 node = drm_mm_split_at_start(node, size, atomic);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000175 }
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100176
177 if (align_splitoff)
178 drm_mm_put_block(align_splitoff);
179
Chris Wilsone6c03c52009-05-22 14:14:22 +0100180 return node;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000181}
Thomas Hellstrom89579f72009-06-17 12:29:56 +0200182EXPORT_SYMBOL(drm_mm_get_block_generic);
Jerome Glisse249d6042009-04-08 17:11:16 +0200183
Jerome Glissea2e68e92009-12-07 15:52:56 +0100184struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *node,
185 unsigned long size,
186 unsigned alignment,
187 unsigned long start,
188 unsigned long end,
189 int atomic)
190{
191 struct drm_mm_node *align_splitoff = NULL;
192 unsigned tmp = 0;
193 unsigned wasted = 0;
194
195 if (node->start < start)
196 wasted += start - node->start;
197 if (alignment)
198 tmp = ((node->start + wasted) % alignment);
199
200 if (tmp)
201 wasted += alignment - tmp;
202 if (wasted) {
203 align_splitoff = drm_mm_split_at_start(node, wasted, atomic);
204 if (unlikely(align_splitoff == NULL))
205 return NULL;
206 }
207
208 if (node->size == size) {
209 list_del_init(&node->fl_entry);
210 node->free = 0;
211 } else {
212 node = drm_mm_split_at_start(node, size, atomic);
213 }
214
215 if (align_splitoff)
216 drm_mm_put_block(align_splitoff);
217
218 return node;
219}
220EXPORT_SYMBOL(drm_mm_get_block_range_generic);
221
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000222/*
223 * Put a block. Merge with the previous and / or next block if they are free.
224 * Otherwise add to the free stack.
225 */
226
Jerome Glisse249d6042009-04-08 17:11:16 +0200227void drm_mm_put_block(struct drm_mm_node *cur)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000228{
229
Dave Airlie55910512007-07-11 16:53:40 +1000230 struct drm_mm *mm = cur->mm;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000231 struct list_head *cur_head = &cur->ml_entry;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100232 struct list_head *root_head = &mm->ml_entry;
Dave Airlie55910512007-07-11 16:53:40 +1000233 struct drm_mm_node *prev_node = NULL;
234 struct drm_mm_node *next_node;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000235
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000236 int merged = 0;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000237
238 if (cur_head->prev != root_head) {
Jerome Glisse249d6042009-04-08 17:11:16 +0200239 prev_node =
240 list_entry(cur_head->prev, struct drm_mm_node, ml_entry);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000241 if (prev_node->free) {
242 prev_node->size += cur->size;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000243 merged = 1;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000244 }
245 }
246 if (cur_head->next != root_head) {
Jerome Glisse249d6042009-04-08 17:11:16 +0200247 next_node =
248 list_entry(cur_head->next, struct drm_mm_node, ml_entry);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000249 if (next_node->free) {
250 if (merged) {
251 prev_node->size += next_node->size;
252 list_del(&next_node->ml_entry);
253 list_del(&next_node->fl_entry);
Jerome Glissea698cf32009-11-13 20:56:58 +0100254 spin_lock(&mm->unused_lock);
Jerome Glisse249d6042009-04-08 17:11:16 +0200255 if (mm->num_unused < MM_UNUSED_TARGET) {
256 list_add(&next_node->fl_entry,
257 &mm->unused_nodes);
258 ++mm->num_unused;
259 } else
260 kfree(next_node);
Jerome Glissea698cf32009-11-13 20:56:58 +0100261 spin_unlock(&mm->unused_lock);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000262 } else {
263 next_node->size += cur->size;
264 next_node->start = cur->start;
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000265 merged = 1;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000266 }
267 }
268 }
269 if (!merged) {
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000270 cur->free = 1;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100271 list_add(&cur->fl_entry, &mm->fl_entry);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000272 } else {
273 list_del(&cur->ml_entry);
Jerome Glissea698cf32009-11-13 20:56:58 +0100274 spin_lock(&mm->unused_lock);
Jerome Glisse249d6042009-04-08 17:11:16 +0200275 if (mm->num_unused < MM_UNUSED_TARGET) {
276 list_add(&cur->fl_entry, &mm->unused_nodes);
277 ++mm->num_unused;
278 } else
279 kfree(cur);
Jerome Glissea698cf32009-11-13 20:56:58 +0100280 spin_unlock(&mm->unused_lock);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000281 }
282}
Jerome Glisse249d6042009-04-08 17:11:16 +0200283
Eric Anholt673a3942008-07-30 12:06:12 -0700284EXPORT_SYMBOL(drm_mm_put_block);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000285
Jerome Glisse249d6042009-04-08 17:11:16 +0200286struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
287 unsigned long size,
288 unsigned alignment, int best_match)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000289{
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100290 const struct list_head *free_stack = &mm->fl_entry;
Dave Airlie55910512007-07-11 16:53:40 +1000291 struct drm_mm_node *entry;
292 struct drm_mm_node *best;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000293 unsigned long best_size;
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100294 unsigned wasted;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000295
296 best = NULL;
297 best_size = ~0UL;
298
Daniel Vetter26f37512010-07-02 15:02:11 +0100299 list_for_each_entry(entry, free_stack, fl_entry) {
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100300 wasted = 0;
301
302 if (entry->size < size)
303 continue;
304
305 if (alignment) {
306 register unsigned tmp = entry->start % alignment;
307 if (tmp)
308 wasted += alignment - tmp;
309 }
310
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100311 if (entry->size >= size + wasted) {
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000312 if (!best_match)
313 return entry;
Bob Gleitsmanne2108eb2009-12-21 13:52:09 +1000314 if (entry->size < best_size) {
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000315 best = entry;
316 best_size = entry->size;
317 }
318 }
319 }
320
321 return best;
322}
Jerome Glisse249d6042009-04-08 17:11:16 +0200323EXPORT_SYMBOL(drm_mm_search_free);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000324
Jerome Glissea2e68e92009-12-07 15:52:56 +0100325struct drm_mm_node *drm_mm_search_free_in_range(const struct drm_mm *mm,
326 unsigned long size,
327 unsigned alignment,
328 unsigned long start,
329 unsigned long end,
330 int best_match)
331{
Jerome Glissea2e68e92009-12-07 15:52:56 +0100332 const struct list_head *free_stack = &mm->fl_entry;
333 struct drm_mm_node *entry;
334 struct drm_mm_node *best;
335 unsigned long best_size;
336 unsigned wasted;
337
338 best = NULL;
339 best_size = ~0UL;
340
Daniel Vetter26f37512010-07-02 15:02:11 +0100341 list_for_each_entry(entry, free_stack, fl_entry) {
Jerome Glissea2e68e92009-12-07 15:52:56 +0100342 wasted = 0;
343
344 if (entry->size < size)
345 continue;
346
347 if (entry->start > end || (entry->start+entry->size) < start)
348 continue;
349
350 if (entry->start < start)
351 wasted += start - entry->start;
352
353 if (alignment) {
354 register unsigned tmp = (entry->start + wasted) % alignment;
355 if (tmp)
356 wasted += alignment - tmp;
357 }
358
Thomas Hellstrom775c6702010-02-12 00:17:59 +0100359 if (entry->size >= size + wasted &&
360 (entry->start + wasted + size) <= end) {
Jerome Glissea2e68e92009-12-07 15:52:56 +0100361 if (!best_match)
362 return entry;
Bob Gleitsmanne2108eb2009-12-21 13:52:09 +1000363 if (entry->size < best_size) {
Jerome Glissea2e68e92009-12-07 15:52:56 +0100364 best = entry;
365 best_size = entry->size;
366 }
367 }
368 }
369
370 return best;
371}
372EXPORT_SYMBOL(drm_mm_search_free_in_range);
373
Dave Airlie55910512007-07-11 16:53:40 +1000374int drm_mm_clean(struct drm_mm * mm)
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100375{
376 struct list_head *head = &mm->ml_entry;
377
378 return (head->next->next == head);
379}
Jerome Glisse249d6042009-04-08 17:11:16 +0200380EXPORT_SYMBOL(drm_mm_clean);
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100381
Dave Airlie55910512007-07-11 16:53:40 +1000382int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000383{
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100384 INIT_LIST_HEAD(&mm->ml_entry);
385 INIT_LIST_HEAD(&mm->fl_entry);
Jerome Glisse249d6042009-04-08 17:11:16 +0200386 INIT_LIST_HEAD(&mm->unused_nodes);
387 mm->num_unused = 0;
388 spin_lock_init(&mm->unused_lock);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000389
Jerome Glisse249d6042009-04-08 17:11:16 +0200390 return drm_mm_create_tail_node(mm, start, size, 0);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000391}
Eric Anholt673a3942008-07-30 12:06:12 -0700392EXPORT_SYMBOL(drm_mm_init);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000393
Dave Airlie55910512007-07-11 16:53:40 +1000394void drm_mm_takedown(struct drm_mm * mm)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000395{
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100396 struct list_head *bnode = mm->fl_entry.next;
Dave Airlie55910512007-07-11 16:53:40 +1000397 struct drm_mm_node *entry;
Jerome Glisse249d6042009-04-08 17:11:16 +0200398 struct drm_mm_node *next;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000399
Dave Airlie55910512007-07-11 16:53:40 +1000400 entry = list_entry(bnode, struct drm_mm_node, fl_entry);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000401
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100402 if (entry->ml_entry.next != &mm->ml_entry ||
403 entry->fl_entry.next != &mm->fl_entry) {
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000404 DRM_ERROR("Memory manager not clean. Delaying takedown\n");
405 return;
406 }
407
408 list_del(&entry->fl_entry);
409 list_del(&entry->ml_entry);
Jerome Glisse249d6042009-04-08 17:11:16 +0200410 kfree(entry);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000411
Jerome Glisse249d6042009-04-08 17:11:16 +0200412 spin_lock(&mm->unused_lock);
413 list_for_each_entry_safe(entry, next, &mm->unused_nodes, fl_entry) {
414 list_del(&entry->fl_entry);
415 kfree(entry);
416 --mm->num_unused;
417 }
418 spin_unlock(&mm->unused_lock);
419
420 BUG_ON(mm->num_unused != 0);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000421}
Dave Airlief453ba02008-11-07 14:05:41 -0800422EXPORT_SYMBOL(drm_mm_takedown);
Dave Airliefa8a1232009-08-26 13:13:37 +1000423
Jerome Glisse99d7e482009-12-09 21:55:09 +0100424void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
425{
426 struct drm_mm_node *entry;
427 int total_used = 0, total_free = 0, total = 0;
428
429 list_for_each_entry(entry, &mm->ml_entry, ml_entry) {
430 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8ld: %s\n",
431 prefix, entry->start, entry->start + entry->size,
432 entry->size, entry->free ? "free" : "used");
433 total += entry->size;
434 if (entry->free)
435 total_free += entry->size;
436 else
437 total_used += entry->size;
438 }
439 printk(KERN_DEBUG "%s total: %d, used %d free %d\n", prefix, total,
440 total_used, total_free);
441}
442EXPORT_SYMBOL(drm_mm_debug_table);
443
Dave Airliefa8a1232009-08-26 13:13:37 +1000444#if defined(CONFIG_DEBUG_FS)
445int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
446{
447 struct drm_mm_node *entry;
448 int total_used = 0, total_free = 0, total = 0;
449
450 list_for_each_entry(entry, &mm->ml_entry, ml_entry) {
451 seq_printf(m, "0x%08lx-0x%08lx: 0x%08lx: %s\n", entry->start, entry->start + entry->size, entry->size, entry->free ? "free" : "used");
452 total += entry->size;
453 if (entry->free)
454 total_free += entry->size;
455 else
456 total_used += entry->size;
457 }
Dave Airlie01d01ba2009-12-04 10:18:02 +1000458 seq_printf(m, "total: %d, used %d free %d\n", total, total_used, total_free);
Dave Airliefa8a1232009-08-26 13:13:37 +1000459 return 0;
460}
461EXPORT_SYMBOL(drm_mm_dump_table);
462#endif