blob: 954e9dcb1603f7cba53e5c035c573c82797a7bdb [file] [log] [blame]
Eric Anholtc4857422008-06-03 10:20:49 -07001/*
2 * GLX Hardware Device Driver common code
3 * Copyright (C) 1999 Wittawat Yamwong
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24
Emil Velikov5966d372015-03-31 21:12:14 +010025#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
Eric Anholtc4857422008-06-03 10:20:49 -070029#include <stdlib.h>
30#include <assert.h>
31
32#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010033#include "libdrm_macros.h"
Eric Anholtc4857422008-06-03 10:20:49 -070034#include "mm.h"
35
Emil Velikov5966d372015-03-31 21:12:14 +010036drm_private void mmDumpMemInfo(const struct mem_block *heap)
Eric Anholtc4857422008-06-03 10:20:49 -070037{
Eric Anholtd70d6052009-10-06 12:40:42 -070038 drmMsg("Memory heap %p:\n", (void *)heap);
39 if (heap == 0) {
40 drmMsg(" heap == 0\n");
41 } else {
42 const struct mem_block *p;
Eric Anholtc4857422008-06-03 10:20:49 -070043
Eric Anholtd70d6052009-10-06 12:40:42 -070044 for (p = heap->next; p != heap; p = p->next) {
45 drmMsg(" Offset:%08x, Size:%08x, %c%c\n", p->ofs,
46 p->size, p->free ? 'F' : '.',
47 p->reserved ? 'R' : '.');
48 }
Eric Anholtc4857422008-06-03 10:20:49 -070049
Eric Anholtd70d6052009-10-06 12:40:42 -070050 drmMsg("\nFree list:\n");
Eric Anholtc4857422008-06-03 10:20:49 -070051
Eric Anholtd70d6052009-10-06 12:40:42 -070052 for (p = heap->next_free; p != heap; p = p->next_free) {
53 drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n", p->ofs,
54 p->size, p->free ? 'F' : '.',
55 p->reserved ? 'R' : '.');
56 }
Eric Anholtc4857422008-06-03 10:20:49 -070057
Eric Anholtd70d6052009-10-06 12:40:42 -070058 }
59 drmMsg("End of memory blocks\n");
Eric Anholtc4857422008-06-03 10:20:49 -070060}
61
Emil Velikov5966d372015-03-31 21:12:14 +010062drm_private struct mem_block *mmInit(int ofs, int size)
Eric Anholtc4857422008-06-03 10:20:49 -070063{
Eric Anholtd70d6052009-10-06 12:40:42 -070064 struct mem_block *heap, *block;
Eric Anholtc4857422008-06-03 10:20:49 -070065
Eric Anholtd70d6052009-10-06 12:40:42 -070066 if (size <= 0)
67 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -070068
Eric Anholtd70d6052009-10-06 12:40:42 -070069 heap = (struct mem_block *)calloc(1, sizeof(struct mem_block));
70 if (!heap)
71 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -070072
Eric Anholtd70d6052009-10-06 12:40:42 -070073 block = (struct mem_block *)calloc(1, sizeof(struct mem_block));
74 if (!block) {
75 free(heap);
76 return NULL;
77 }
Eric Anholtc4857422008-06-03 10:20:49 -070078
Eric Anholtd70d6052009-10-06 12:40:42 -070079 heap->next = block;
80 heap->prev = block;
81 heap->next_free = block;
82 heap->prev_free = block;
Eric Anholtc4857422008-06-03 10:20:49 -070083
Eric Anholtd70d6052009-10-06 12:40:42 -070084 block->heap = heap;
85 block->next = heap;
86 block->prev = heap;
87 block->next_free = heap;
88 block->prev_free = heap;
89
90 block->ofs = ofs;
91 block->size = size;
92 block->free = 1;
93
94 return heap;
Eric Anholtc4857422008-06-03 10:20:49 -070095}
96
Eric Anholtd70d6052009-10-06 12:40:42 -070097static struct mem_block *SliceBlock(struct mem_block *p,
98 int startofs, int size,
99 int reserved, int alignment)
Eric Anholtc4857422008-06-03 10:20:49 -0700100{
Eric Anholtd70d6052009-10-06 12:40:42 -0700101 struct mem_block *newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700102
Eric Anholtd70d6052009-10-06 12:40:42 -0700103 /* break left [p, newblock, p->next], then p = newblock */
104 if (startofs > p->ofs) {
105 newblock =
106 (struct mem_block *)calloc(1, sizeof(struct mem_block));
107 if (!newblock)
108 return NULL;
109 newblock->ofs = startofs;
110 newblock->size = p->size - (startofs - p->ofs);
111 newblock->free = 1;
112 newblock->heap = p->heap;
Eric Anholtc4857422008-06-03 10:20:49 -0700113
Eric Anholtd70d6052009-10-06 12:40:42 -0700114 newblock->next = p->next;
115 newblock->prev = p;
116 p->next->prev = newblock;
117 p->next = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700118
Eric Anholtd70d6052009-10-06 12:40:42 -0700119 newblock->next_free = p->next_free;
120 newblock->prev_free = p;
121 p->next_free->prev_free = newblock;
122 p->next_free = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700123
Eric Anholtd70d6052009-10-06 12:40:42 -0700124 p->size -= newblock->size;
125 p = newblock;
126 }
Eric Anholtc4857422008-06-03 10:20:49 -0700127
Eric Anholtd70d6052009-10-06 12:40:42 -0700128 /* break right, also [p, newblock, p->next] */
129 if (size < p->size) {
130 newblock =
131 (struct mem_block *)calloc(1, sizeof(struct mem_block));
132 if (!newblock)
133 return NULL;
134 newblock->ofs = startofs + size;
135 newblock->size = p->size - size;
136 newblock->free = 1;
137 newblock->heap = p->heap;
Eric Anholtc4857422008-06-03 10:20:49 -0700138
Eric Anholtd70d6052009-10-06 12:40:42 -0700139 newblock->next = p->next;
140 newblock->prev = p;
141 p->next->prev = newblock;
142 p->next = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700143
Eric Anholtd70d6052009-10-06 12:40:42 -0700144 newblock->next_free = p->next_free;
145 newblock->prev_free = p;
146 p->next_free->prev_free = newblock;
147 p->next_free = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700148
Eric Anholtd70d6052009-10-06 12:40:42 -0700149 p->size = size;
150 }
Eric Anholtc4857422008-06-03 10:20:49 -0700151
Eric Anholtd70d6052009-10-06 12:40:42 -0700152 /* p = middle block */
153 p->free = 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700154
Eric Anholtd70d6052009-10-06 12:40:42 -0700155 /* Remove p from the free list:
156 */
157 p->next_free->prev_free = p->prev_free;
158 p->prev_free->next_free = p->next_free;
Eric Anholtc4857422008-06-03 10:20:49 -0700159
Eric Anholtd70d6052009-10-06 12:40:42 -0700160 p->next_free = 0;
161 p->prev_free = 0;
162
163 p->reserved = reserved;
164 return p;
Eric Anholtc4857422008-06-03 10:20:49 -0700165}
166
Emil Velikov5966d372015-03-31 21:12:14 +0100167drm_private struct mem_block *mmAllocMem(struct mem_block *heap, int size,
168 int align2, int startSearch)
Eric Anholtc4857422008-06-03 10:20:49 -0700169{
Eric Anholtd70d6052009-10-06 12:40:42 -0700170 struct mem_block *p;
171 const int mask = (1 << align2) - 1;
172 int startofs = 0;
173 int endofs;
Eric Anholtc4857422008-06-03 10:20:49 -0700174
Eric Anholtd70d6052009-10-06 12:40:42 -0700175 if (!heap || align2 < 0 || size <= 0)
176 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -0700177
Eric Anholtd70d6052009-10-06 12:40:42 -0700178 for (p = heap->next_free; p != heap; p = p->next_free) {
179 assert(p->free);
Eric Anholtc4857422008-06-03 10:20:49 -0700180
Eric Anholtd70d6052009-10-06 12:40:42 -0700181 startofs = (p->ofs + mask) & ~mask;
182 if (startofs < startSearch) {
183 startofs = startSearch;
184 }
185 endofs = startofs + size;
186 if (endofs <= (p->ofs + p->size))
187 break;
188 }
Eric Anholtc4857422008-06-03 10:20:49 -0700189
Eric Anholtd70d6052009-10-06 12:40:42 -0700190 if (p == heap)
191 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -0700192
Eric Anholtd70d6052009-10-06 12:40:42 -0700193 assert(p->free);
194 p = SliceBlock(p, startofs, size, 0, mask + 1);
Eric Anholtc4857422008-06-03 10:20:49 -0700195
Eric Anholtd70d6052009-10-06 12:40:42 -0700196 return p;
Eric Anholtc4857422008-06-03 10:20:49 -0700197}
198
Eric Anholtd70d6052009-10-06 12:40:42 -0700199static int Join2Blocks(struct mem_block *p)
Eric Anholtc4857422008-06-03 10:20:49 -0700200{
Eric Anholtd70d6052009-10-06 12:40:42 -0700201 /* XXX there should be some assertions here */
Eric Anholtc4857422008-06-03 10:20:49 -0700202
Eric Anholtd70d6052009-10-06 12:40:42 -0700203 /* NOTE: heap->free == 0 */
Eric Anholtc4857422008-06-03 10:20:49 -0700204
Eric Anholtd70d6052009-10-06 12:40:42 -0700205 if (p->free && p->next->free) {
206 struct mem_block *q = p->next;
Eric Anholtc4857422008-06-03 10:20:49 -0700207
Eric Anholtd70d6052009-10-06 12:40:42 -0700208 assert(p->ofs + p->size == q->ofs);
209 p->size += q->size;
Eric Anholtc4857422008-06-03 10:20:49 -0700210
Eric Anholtd70d6052009-10-06 12:40:42 -0700211 p->next = q->next;
212 q->next->prev = p;
Eric Anholtc4857422008-06-03 10:20:49 -0700213
Eric Anholtd70d6052009-10-06 12:40:42 -0700214 q->next_free->prev_free = q->prev_free;
215 q->prev_free->next_free = q->next_free;
216
217 free(q);
218 return 1;
219 }
220 return 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700221}
222
Emil Velikov5966d372015-03-31 21:12:14 +0100223drm_private int mmFreeMem(struct mem_block *b)
Eric Anholtc4857422008-06-03 10:20:49 -0700224{
Eric Anholtd70d6052009-10-06 12:40:42 -0700225 if (!b)
226 return 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700227
Eric Anholtd70d6052009-10-06 12:40:42 -0700228 if (b->free) {
229 drmMsg("block already free\n");
230 return -1;
231 }
232 if (b->reserved) {
233 drmMsg("block is reserved\n");
234 return -1;
235 }
Eric Anholtc4857422008-06-03 10:20:49 -0700236
Eric Anholtd70d6052009-10-06 12:40:42 -0700237 b->free = 1;
238 b->next_free = b->heap->next_free;
239 b->prev_free = b->heap;
240 b->next_free->prev_free = b;
241 b->prev_free->next_free = b;
Eric Anholtc4857422008-06-03 10:20:49 -0700242
Eric Anholtd70d6052009-10-06 12:40:42 -0700243 Join2Blocks(b);
244 if (b->prev != b->heap)
245 Join2Blocks(b->prev);
Eric Anholtc4857422008-06-03 10:20:49 -0700246
Eric Anholtd70d6052009-10-06 12:40:42 -0700247 return 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700248}
249
Emil Velikov5966d372015-03-31 21:12:14 +0100250drm_private void mmDestroy(struct mem_block *heap)
Eric Anholtc4857422008-06-03 10:20:49 -0700251{
Eric Anholtd70d6052009-10-06 12:40:42 -0700252 struct mem_block *p;
Eric Anholtc4857422008-06-03 10:20:49 -0700253
Eric Anholtd70d6052009-10-06 12:40:42 -0700254 if (!heap)
255 return;
Eric Anholtc4857422008-06-03 10:20:49 -0700256
Eric Anholtd70d6052009-10-06 12:40:42 -0700257 for (p = heap->next; p != heap;) {
258 struct mem_block *next = p->next;
259 free(p);
260 p = next;
261 }
Eric Anholtc4857422008-06-03 10:20:49 -0700262
Eric Anholtd70d6052009-10-06 12:40:42 -0700263 free(heap);
Eric Anholtc4857422008-06-03 10:20:49 -0700264}