1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
30 * Generic simple memory manager implementation. Intended to be used as a base
31 * class implementation for more advanced memory managers.
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.
38 * Aligned allocations can also see improvement.
41 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
45 #include <linux/slab.h>
47 unsigned long drm_memrange_tail_space(struct drm_memrange *mm)
49 struct list_head *tail_node;
50 struct drm_memrange_node *entry;
52 tail_node = mm->ml_entry.prev;
53 entry = list_entry(tail_node, struct drm_memrange_node, ml_entry);
60 int drm_memrange_remove_space_from_tail(struct drm_memrange *mm, unsigned long size)
62 struct list_head *tail_node;
63 struct drm_memrange_node *entry;
65 tail_node = mm->ml_entry.prev;
66 entry = list_entry(tail_node, struct drm_memrange_node, ml_entry);
70 if (entry->size <= size)
78 static int drm_memrange_create_tail_node(struct drm_memrange *mm,
82 struct drm_memrange_node *child;
84 child = (struct drm_memrange_node *)
85 drm_ctl_alloc(sizeof(*child), DRM_MEM_MM);
94 list_add_tail(&child->ml_entry, &mm->ml_entry);
95 list_add_tail(&child->fl_entry, &mm->fl_entry);
101 int drm_memrange_add_space_to_tail(struct drm_memrange *mm, unsigned long size)
103 struct list_head *tail_node;
104 struct drm_memrange_node *entry;
106 tail_node = mm->ml_entry.prev;
107 entry = list_entry(tail_node, struct drm_memrange_node, ml_entry);
109 return drm_memrange_create_tail_node(mm, entry->start + entry->size, size);
115 static struct drm_memrange_node *drm_memrange_split_at_start(struct drm_memrange_node *parent,
118 struct drm_memrange_node *child;
120 child = (struct drm_memrange_node *)
121 drm_ctl_alloc(sizeof(*child), DRM_MEM_MM);
125 INIT_LIST_HEAD(&child->fl_entry);
129 child->start = parent->start;
130 child->mm = parent->mm;
132 list_add_tail(&child->ml_entry, &parent->ml_entry);
133 INIT_LIST_HEAD(&child->fl_entry);
135 parent->size -= size;
136 parent->start += size;
140 struct drm_memrange_node *drm_memrange_get_block(struct drm_memrange_node * parent,
141 unsigned long size, unsigned alignment)
144 struct drm_memrange_node *align_splitoff = NULL;
145 struct drm_memrange_node *child;
149 tmp = parent->start % alignment;
152 align_splitoff = drm_memrange_split_at_start(parent, alignment - tmp);
157 if (parent->size == size) {
158 list_del_init(&parent->fl_entry);
162 child = drm_memrange_split_at_start(parent, size);
166 drm_memrange_put_block(align_splitoff);
170 EXPORT_SYMBOL(drm_memrange_get_block);
173 * Put a block. Merge with the previous and / or next block if they are free.
174 * Otherwise add to the free stack.
177 void drm_memrange_put_block(struct drm_memrange_node * cur)
180 struct drm_memrange *mm = cur->mm;
181 struct list_head *cur_head = &cur->ml_entry;
182 struct list_head *root_head = &mm->ml_entry;
183 struct drm_memrange_node *prev_node = NULL;
184 struct drm_memrange_node *next_node;
188 if (cur_head->prev != root_head) {
189 prev_node = list_entry(cur_head->prev, struct drm_memrange_node, ml_entry);
190 if (prev_node->free) {
191 prev_node->size += cur->size;
195 if (cur_head->next != root_head) {
196 next_node = list_entry(cur_head->next, struct drm_memrange_node, ml_entry);
197 if (next_node->free) {
199 prev_node->size += next_node->size;
200 list_del(&next_node->ml_entry);
201 list_del(&next_node->fl_entry);
202 drm_ctl_free(next_node, sizeof(*next_node),
205 next_node->size += cur->size;
206 next_node->start = cur->start;
213 list_add(&cur->fl_entry, &mm->fl_entry);
215 list_del(&cur->ml_entry);
216 drm_ctl_free(cur, sizeof(*cur), DRM_MEM_MM);
219 EXPORT_SYMBOL(drm_memrange_put_block);
221 struct drm_memrange_node *drm_memrange_search_free(const struct drm_memrange * mm,
223 unsigned alignment, int best_match)
225 struct list_head *list;
226 const struct list_head *free_stack = &mm->fl_entry;
227 struct drm_memrange_node *entry;
228 struct drm_memrange_node *best;
229 unsigned long best_size;
235 list_for_each(list, free_stack) {
236 entry = list_entry(list, struct drm_memrange_node, fl_entry);
239 if (entry->size < size)
243 register unsigned tmp = entry->start % alignment;
245 wasted += alignment - tmp;
249 if (entry->size >= size + wasted) {
252 if (size < best_size) {
254 best_size = entry->size;
261 EXPORT_SYMBOL(drm_memrange_search_free);
263 int drm_memrange_clean(struct drm_memrange * mm)
265 struct list_head *head = &mm->ml_entry;
267 return (head->next->next == head);
270 int drm_memrange_init(struct drm_memrange * mm, unsigned long start, unsigned long size)
272 INIT_LIST_HEAD(&mm->ml_entry);
273 INIT_LIST_HEAD(&mm->fl_entry);
275 return drm_memrange_create_tail_node(mm, start, size);
278 EXPORT_SYMBOL(drm_memrange_init);
280 void drm_memrange_takedown(struct drm_memrange * mm)
282 struct list_head *bnode = mm->fl_entry.next;
283 struct drm_memrange_node *entry;
285 entry = list_entry(bnode, struct drm_memrange_node, fl_entry);
287 if (entry->ml_entry.next != &mm->ml_entry ||
288 entry->fl_entry.next != &mm->fl_entry) {
289 DRM_ERROR("Memory manager not clean. Delaying takedown\n");
293 list_del(&entry->fl_entry);
294 list_del(&entry->ml_entry);
295 drm_ctl_free(entry, sizeof(*entry), DRM_MEM_MM);
297 EXPORT_SYMBOL(drm_memrange_takedown);