1 /*
2 * Copyright 2012 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24
25 #include "core/os.h"
26 #include "core/mm.h"
27
28 #define node(root, dir) ((root)->nl_entry.dir == &mm->nodes) ? NULL : \
29 list_entry((root)->nl_entry.dir, struct nouveau_mm_node, nl_entry)
30
31 static void
nouveau_mm_dump(struct nouveau_mm * mm,const char * header)32 nouveau_mm_dump(struct nouveau_mm *mm, const char *header)
33 {
34 struct nouveau_mm_node *node;
35
36 printk(KERN_ERR "nouveau: %s\n", header);
37 printk(KERN_ERR "nouveau: node list:\n");
38 list_for_each_entry(node, &mm->nodes, nl_entry) {
39 printk(KERN_ERR "nouveau: \t%08x %08x %d\n",
40 node->offset, node->length, node->type);
41 }
42 printk(KERN_ERR "nouveau: free list:\n");
43 list_for_each_entry(node, &mm->free, fl_entry) {
44 printk(KERN_ERR "nouveau: \t%08x %08x %d\n",
45 node->offset, node->length, node->type);
46 }
47 }
48
49 void
nouveau_mm_free(struct nouveau_mm * mm,struct nouveau_mm_node ** pthis)50 nouveau_mm_free(struct nouveau_mm *mm, struct nouveau_mm_node **pthis)
51 {
52 struct nouveau_mm_node *this = *pthis;
53
54 if (this) {
55 struct nouveau_mm_node *prev = node(this, prev);
56 struct nouveau_mm_node *next = node(this, next);
57
58 if (prev && prev->type == NVKM_MM_TYPE_NONE) {
59 prev->length += this->length;
60 list_del(&this->nl_entry);
61 kfree(this); this = prev;
62 }
63
64 if (next && next->type == NVKM_MM_TYPE_NONE) {
65 next->offset = this->offset;
66 next->length += this->length;
67 if (this->type == NVKM_MM_TYPE_NONE)
68 list_del(&this->fl_entry);
69 list_del(&this->nl_entry);
70 kfree(this); this = NULL;
71 }
72
73 if (this && this->type != NVKM_MM_TYPE_NONE) {
74 list_for_each_entry(prev, &mm->free, fl_entry) {
75 if (this->offset < prev->offset)
76 break;
77 }
78
79 list_add_tail(&this->fl_entry, &prev->fl_entry);
80 this->type = NVKM_MM_TYPE_NONE;
81 }
82 }
83
84 *pthis = NULL;
85 }
86
87 static struct nouveau_mm_node *
region_head(struct nouveau_mm * mm,struct nouveau_mm_node * a,u32 size)88 region_head(struct nouveau_mm *mm, struct nouveau_mm_node *a, u32 size)
89 {
90 struct nouveau_mm_node *b;
91
92 if (a->length == size)
93 return a;
94
95 b = kmalloc(sizeof(*b), GFP_KERNEL);
96 if (unlikely(b == NULL))
97 return NULL;
98
99 b->offset = a->offset;
100 b->length = size;
101 b->heap = a->heap;
102 b->type = a->type;
103 a->offset += size;
104 a->length -= size;
105 list_add_tail(&b->nl_entry, &a->nl_entry);
106 if (b->type == NVKM_MM_TYPE_NONE)
107 list_add_tail(&b->fl_entry, &a->fl_entry);
108 return b;
109 }
110
111 int
nouveau_mm_head(struct nouveau_mm * mm,u8 heap,u8 type,u32 size_max,u32 size_min,u32 align,struct nouveau_mm_node ** pnode)112 nouveau_mm_head(struct nouveau_mm *mm, u8 heap, u8 type, u32 size_max,
113 u32 size_min, u32 align, struct nouveau_mm_node **pnode)
114 {
115 struct nouveau_mm_node *prev, *this, *next;
116 u32 mask = align - 1;
117 u32 splitoff;
118 u32 s, e;
119
120 BUG_ON(type == NVKM_MM_TYPE_NONE || type == NVKM_MM_TYPE_HOLE);
121
122 list_for_each_entry(this, &mm->free, fl_entry) {
123 if (unlikely(heap != NVKM_MM_HEAP_ANY)) {
124 if (this->heap != heap)
125 continue;
126 }
127 e = this->offset + this->length;
128 s = this->offset;
129
130 prev = node(this, prev);
131 if (prev && prev->type != type)
132 s = roundup(s, mm->block_size);
133
134 next = node(this, next);
135 if (next && next->type != type)
136 e = rounddown(e, mm->block_size);
137
138 s = (s + mask) & ~mask;
139 e &= ~mask;
140 if (s > e || e - s < size_min)
141 continue;
142
143 splitoff = s - this->offset;
144 if (splitoff && !region_head(mm, this, splitoff))
145 return -ENOMEM;
146
147 this = region_head(mm, this, min(size_max, e - s));
148 if (!this)
149 return -ENOMEM;
150
151 this->type = type;
152 list_del(&this->fl_entry);
153 *pnode = this;
154 return 0;
155 }
156
157 return -ENOSPC;
158 }
159
160 static struct nouveau_mm_node *
region_tail(struct nouveau_mm * mm,struct nouveau_mm_node * a,u32 size)161 region_tail(struct nouveau_mm *mm, struct nouveau_mm_node *a, u32 size)
162 {
163 struct nouveau_mm_node *b;
164
165 if (a->length == size)
166 return a;
167
168 b = kmalloc(sizeof(*b), GFP_KERNEL);
169 if (unlikely(b == NULL))
170 return NULL;
171
172 a->length -= size;
173 b->offset = a->offset + a->length;
174 b->length = size;
175 b->heap = a->heap;
176 b->type = a->type;
177
178 list_add(&b->nl_entry, &a->nl_entry);
179 if (b->type == NVKM_MM_TYPE_NONE)
180 list_add(&b->fl_entry, &a->fl_entry);
181 return b;
182 }
183
184 int
nouveau_mm_tail(struct nouveau_mm * mm,u8 heap,u8 type,u32 size_max,u32 size_min,u32 align,struct nouveau_mm_node ** pnode)185 nouveau_mm_tail(struct nouveau_mm *mm, u8 heap, u8 type, u32 size_max,
186 u32 size_min, u32 align, struct nouveau_mm_node **pnode)
187 {
188 struct nouveau_mm_node *prev, *this, *next;
189 u32 mask = align - 1;
190
191 BUG_ON(type == NVKM_MM_TYPE_NONE || type == NVKM_MM_TYPE_HOLE);
192
193 list_for_each_entry_reverse(this, &mm->free, fl_entry) {
194 u32 e = this->offset + this->length;
195 u32 s = this->offset;
196 u32 c = 0, a;
197 if (unlikely(heap != NVKM_MM_HEAP_ANY)) {
198 if (this->heap != heap)
199 continue;
200 }
201
202 prev = node(this, prev);
203 if (prev && prev->type != type)
204 s = roundup(s, mm->block_size);
205
206 next = node(this, next);
207 if (next && next->type != type) {
208 e = rounddown(e, mm->block_size);
209 c = next->offset - e;
210 }
211
212 s = (s + mask) & ~mask;
213 a = e - s;
214 if (s > e || a < size_min)
215 continue;
216
217 a = min(a, size_max);
218 s = (e - a) & ~mask;
219 c += (e - s) - a;
220
221 if (c && !region_tail(mm, this, c))
222 return -ENOMEM;
223
224 this = region_tail(mm, this, a);
225 if (!this)
226 return -ENOMEM;
227
228 this->type = type;
229 list_del(&this->fl_entry);
230 *pnode = this;
231 return 0;
232 }
233
234 return -ENOSPC;
235 }
236
237 int
nouveau_mm_init(struct nouveau_mm * mm,u32 offset,u32 length,u32 block)238 nouveau_mm_init(struct nouveau_mm *mm, u32 offset, u32 length, u32 block)
239 {
240 struct nouveau_mm_node *node, *prev;
241 u32 next;
242
243 if (nouveau_mm_initialised(mm)) {
244 prev = list_last_entry(&mm->nodes, typeof(*node), nl_entry);
245 next = prev->offset + prev->length;
246 if (next != offset) {
247 BUG_ON(next > offset);
248 if (!(node = kzalloc(sizeof(*node), GFP_KERNEL)))
249 return -ENOMEM;
250 node->type = NVKM_MM_TYPE_HOLE;
251 node->offset = next;
252 node->length = offset - next;
253 list_add_tail(&node->nl_entry, &mm->nodes);
254 }
255 BUG_ON(block != mm->block_size);
256 } else {
257 INIT_LIST_HEAD(&mm->nodes);
258 INIT_LIST_HEAD(&mm->free);
259 mm->block_size = block;
260 mm->heap_nodes = 0;
261 }
262
263 node = kzalloc(sizeof(*node), GFP_KERNEL);
264 if (!node)
265 return -ENOMEM;
266
267 if (length) {
268 node->offset = roundup(offset, mm->block_size);
269 node->length = rounddown(offset + length, mm->block_size);
270 node->length -= node->offset;
271 }
272
273 list_add_tail(&node->nl_entry, &mm->nodes);
274 list_add_tail(&node->fl_entry, &mm->free);
275 node->heap = ++mm->heap_nodes;
276 return 0;
277 }
278
279 int
nouveau_mm_fini(struct nouveau_mm * mm)280 nouveau_mm_fini(struct nouveau_mm *mm)
281 {
282 struct nouveau_mm_node *node, *temp;
283 int nodes = 0;
284
285 if (!nouveau_mm_initialised(mm))
286 return 0;
287
288 list_for_each_entry(node, &mm->nodes, nl_entry) {
289 if (node->type != NVKM_MM_TYPE_HOLE) {
290 if (++nodes > mm->heap_nodes) {
291 nouveau_mm_dump(mm, "mm not clean!");
292 return -EBUSY;
293 }
294 }
295 }
296
297 list_for_each_entry_safe(node, temp, &mm->nodes, nl_entry) {
298 list_del(&node->nl_entry);
299 kfree(node);
300 }
301 mm->heap_nodes = 0;
302 return 0;
303 }
304