NV50: basic fbcon + misc fixes
[platform/upstream/libdrm.git] / linux-core / drm_mm.c
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:
41  * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
42  */
43
44 #include "drmP.h"
45 #include <linux/slab.h>
46
47 unsigned long drm_mm_tail_space(struct drm_mm *mm)
48 {
49         struct list_head *tail_node;
50         struct drm_mm_node *entry;
51
52         tail_node = mm->ml_entry.prev;
53         entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
54         if (!entry->free)
55                 return 0;
56
57         return entry->size;
58 }
59
60 int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size)
61 {
62         struct list_head *tail_node;
63         struct drm_mm_node *entry;
64
65         tail_node = mm->ml_entry.prev;
66         entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
67         if (!entry->free)
68                 return -ENOMEM;
69
70         if (entry->size <= size)
71                 return -ENOMEM;
72
73         entry->size -= size;
74         return 0;
75 }
76
77
78 static int drm_mm_create_tail_node(struct drm_mm *mm,
79                             unsigned long start,
80                             unsigned long size)
81 {
82         struct drm_mm_node *child;
83
84         child = (struct drm_mm_node *)
85                 drm_ctl_alloc(sizeof(*child), DRM_MEM_MM);
86         if (!child)
87                 return -ENOMEM;
88
89         child->free = 1;
90         child->size = size;
91         child->start = start;
92         child->mm = mm;
93
94         list_add_tail(&child->ml_entry, &mm->ml_entry);
95         list_add_tail(&child->fl_entry, &mm->fl_entry);
96
97         return 0;
98 }
99
100
101 int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size)
102 {
103         struct list_head *tail_node;
104         struct drm_mm_node *entry;
105
106         tail_node = mm->ml_entry.prev;
107         entry = list_entry(tail_node, struct drm_mm_node, ml_entry);
108         if (!entry->free) {
109                 return drm_mm_create_tail_node(mm, entry->start + entry->size, size);
110         }
111         entry->size += size;
112         return 0;
113 }
114
115 static struct drm_mm_node *drm_mm_split_at_start(struct drm_mm_node *parent,
116                                             unsigned long size)
117 {
118         struct drm_mm_node *child;
119
120         child = (struct drm_mm_node *)
121                 drm_ctl_alloc(sizeof(*child), DRM_MEM_MM);
122         if (!child)
123                 return NULL;
124
125         INIT_LIST_HEAD(&child->fl_entry);
126
127         child->free = 0;
128         child->size = size;
129         child->start = parent->start;
130         child->mm = parent->mm;
131
132         list_add_tail(&child->ml_entry, &parent->ml_entry);
133         INIT_LIST_HEAD(&child->fl_entry);
134
135         parent->size -= size;
136         parent->start += size;
137         return child;
138 }
139
140 struct drm_mm_node *drm_mm_get_block(struct drm_mm_node * parent,
141                                 unsigned long size, unsigned alignment)
142 {
143
144         struct drm_mm_node *align_splitoff = NULL;
145         struct drm_mm_node *child;
146         unsigned tmp = 0;
147
148         if (alignment)
149                 tmp = parent->start % alignment;
150
151         if (tmp) {
152                 align_splitoff = drm_mm_split_at_start(parent, alignment - tmp);
153                 if (!align_splitoff)
154                         return NULL;
155         }
156
157         if (parent->size == size) {
158                 list_del_init(&parent->fl_entry);
159                 parent->free = 0;
160                 return parent;
161         } else {
162                 child = drm_mm_split_at_start(parent, size);
163         }
164
165         if (align_splitoff)
166                 drm_mm_put_block(align_splitoff);
167
168         return child;
169 }
170
171 /*
172  * Put a block. Merge with the previous and / or next block if they are free.
173  * Otherwise add to the free stack.
174  */
175
176 void drm_mm_put_block(struct drm_mm_node * cur)
177 {
178
179         struct drm_mm *mm = cur->mm;
180         struct list_head *cur_head = &cur->ml_entry;
181         struct list_head *root_head = &mm->ml_entry;
182         struct drm_mm_node *prev_node = NULL;
183         struct drm_mm_node *next_node;
184
185         int merged = 0;
186
187         if (cur_head->prev != root_head) {
188                 prev_node = list_entry(cur_head->prev, struct drm_mm_node, ml_entry);
189                 if (prev_node->free) {
190                         prev_node->size += cur->size;
191                         merged = 1;
192                 }
193         }
194         if (cur_head->next != root_head) {
195                 next_node = list_entry(cur_head->next, struct drm_mm_node, ml_entry);
196                 if (next_node->free) {
197                         if (merged) {
198                                 prev_node->size += next_node->size;
199                                 list_del(&next_node->ml_entry);
200                                 list_del(&next_node->fl_entry);
201                                 drm_ctl_free(next_node, sizeof(*next_node),
202                                              DRM_MEM_MM);
203                         } else {
204                                 next_node->size += cur->size;
205                                 next_node->start = cur->start;
206                                 merged = 1;
207                         }
208                 }
209         }
210         if (!merged) {
211                 cur->free = 1;
212                 list_add(&cur->fl_entry, &mm->fl_entry);
213         } else {
214                 list_del(&cur->ml_entry);
215                 drm_ctl_free(cur, sizeof(*cur), DRM_MEM_MM);
216         }
217 }
218 EXPORT_SYMBOL(drm_mm_put_block);
219
220 struct drm_mm_node *drm_mm_search_free(const struct drm_mm * mm,
221                                   unsigned long size,
222                                   unsigned alignment, int best_match)
223 {
224         struct list_head *list;
225         const struct list_head *free_stack = &mm->fl_entry;
226         struct drm_mm_node *entry;
227         struct drm_mm_node *best;
228         unsigned long best_size;
229         unsigned wasted;
230
231         best = NULL;
232         best_size = ~0UL;
233
234         list_for_each(list, free_stack) {
235                 entry = list_entry(list, struct drm_mm_node, fl_entry);
236                 wasted = 0;
237
238                 if (entry->size < size)
239                         continue;
240
241                 if (alignment) {
242                         register unsigned tmp = entry->start % alignment;
243                         if (tmp)
244                                 wasted += alignment - tmp;
245                 }
246
247
248                 if (entry->size >= size + wasted) {
249                         if (!best_match)
250                                 return entry;
251                         if (size < best_size) {
252                                 best = entry;
253                                 best_size = entry->size;
254                         }
255                 }
256         }
257
258         return best;
259 }
260
261 int drm_mm_clean(struct drm_mm * mm)
262 {
263         struct list_head *head = &mm->ml_entry;
264
265         return (head->next->next == head);
266 }
267
268 int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
269 {
270         INIT_LIST_HEAD(&mm->ml_entry);
271         INIT_LIST_HEAD(&mm->fl_entry);
272
273         return drm_mm_create_tail_node(mm, start, size);
274 }
275
276 EXPORT_SYMBOL(drm_mm_init);
277
278 void drm_mm_takedown(struct drm_mm * mm)
279 {
280         struct list_head *bnode = mm->fl_entry.next;
281         struct drm_mm_node *entry;
282
283         entry = list_entry(bnode, struct drm_mm_node, fl_entry);
284
285         if (entry->ml_entry.next != &mm->ml_entry ||
286             entry->fl_entry.next != &mm->fl_entry) {
287                 DRM_ERROR("Memory manager not clean. Delaying takedown\n");
288                 return;
289         }
290
291         list_del(&entry->fl_entry);
292         list_del(&entry->ml_entry);
293         drm_ctl_free(entry, sizeof(*entry), DRM_MEM_MM);
294 }
295
296 EXPORT_SYMBOL(drm_mm_takedown);
297
298 void drm_mm_print(struct drm_mm *mm, const char *name)
299 {
300         struct list_head *list;
301         const struct list_head *mm_stack = &mm->ml_entry;
302         struct drm_mm_node *entry;
303
304         DRM_DEBUG("Memory usage for '%s'\n", name ? name : "unknown");
305         list_for_each(list, mm_stack) {
306                 entry = list_entry(list, struct drm_mm_node, ml_entry);
307                 DRM_DEBUG("\t0x%08lx %li %s pages\n", entry->start, entry->size,
308                         entry->free ? "free" : "used");
309         }
310 }
311 EXPORT_SYMBOL(drm_mm_print);