Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Velikov | 5966d37 | 2015-03-31 21:12:14 +0100 | [diff] [blame] | 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include "config.h" |
| 27 | #endif |
| 28 | |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <assert.h> |
| 31 | |
| 32 | #include "xf86drm.h" |
Emil Velikov | 42465fe | 2015-04-05 15:51:59 +0100 | [diff] [blame] | 33 | #include "libdrm_macros.h" |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 34 | #include "mm.h" |
| 35 | |
Emil Velikov | 5966d37 | 2015-03-31 21:12:14 +0100 | [diff] [blame] | 36 | drm_private void mmDumpMemInfo(const struct mem_block *heap) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 37 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 38 | 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 43 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 44 | 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 49 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 50 | drmMsg("\nFree list:\n"); |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 51 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 52 | 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 57 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 58 | } |
| 59 | drmMsg("End of memory blocks\n"); |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Emil Velikov | 5966d37 | 2015-03-31 21:12:14 +0100 | [diff] [blame] | 62 | drm_private struct mem_block *mmInit(int ofs, int size) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 63 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 64 | struct mem_block *heap, *block; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 65 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 66 | if (size <= 0) |
| 67 | return NULL; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 68 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 69 | heap = (struct mem_block *)calloc(1, sizeof(struct mem_block)); |
| 70 | if (!heap) |
| 71 | return NULL; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 72 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 73 | block = (struct mem_block *)calloc(1, sizeof(struct mem_block)); |
| 74 | if (!block) { |
| 75 | free(heap); |
| 76 | return NULL; |
| 77 | } |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 78 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 79 | heap->next = block; |
| 80 | heap->prev = block; |
| 81 | heap->next_free = block; |
| 82 | heap->prev_free = block; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 83 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 84 | 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 97 | static struct mem_block *SliceBlock(struct mem_block *p, |
| 98 | int startofs, int size, |
| 99 | int reserved, int alignment) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 100 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 101 | struct mem_block *newblock; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 102 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 103 | /* 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 113 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 114 | newblock->next = p->next; |
| 115 | newblock->prev = p; |
| 116 | p->next->prev = newblock; |
| 117 | p->next = newblock; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 118 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 119 | newblock->next_free = p->next_free; |
| 120 | newblock->prev_free = p; |
| 121 | p->next_free->prev_free = newblock; |
| 122 | p->next_free = newblock; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 123 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 124 | p->size -= newblock->size; |
| 125 | p = newblock; |
| 126 | } |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 127 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 128 | /* 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 138 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 139 | newblock->next = p->next; |
| 140 | newblock->prev = p; |
| 141 | p->next->prev = newblock; |
| 142 | p->next = newblock; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 143 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 144 | newblock->next_free = p->next_free; |
| 145 | newblock->prev_free = p; |
| 146 | p->next_free->prev_free = newblock; |
| 147 | p->next_free = newblock; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 148 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 149 | p->size = size; |
| 150 | } |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 151 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 152 | /* p = middle block */ |
| 153 | p->free = 0; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 154 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 155 | /* 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 159 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 160 | p->next_free = 0; |
| 161 | p->prev_free = 0; |
| 162 | |
| 163 | p->reserved = reserved; |
| 164 | return p; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Emil Velikov | 5966d37 | 2015-03-31 21:12:14 +0100 | [diff] [blame] | 167 | drm_private struct mem_block *mmAllocMem(struct mem_block *heap, int size, |
| 168 | int align2, int startSearch) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 169 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 170 | struct mem_block *p; |
| 171 | const int mask = (1 << align2) - 1; |
| 172 | int startofs = 0; |
| 173 | int endofs; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 174 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 175 | if (!heap || align2 < 0 || size <= 0) |
| 176 | return NULL; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 177 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 178 | for (p = heap->next_free; p != heap; p = p->next_free) { |
| 179 | assert(p->free); |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 180 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 181 | 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 189 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 190 | if (p == heap) |
| 191 | return NULL; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 192 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 193 | assert(p->free); |
| 194 | p = SliceBlock(p, startofs, size, 0, mask + 1); |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 195 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 196 | return p; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 199 | static int Join2Blocks(struct mem_block *p) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 200 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 201 | /* XXX there should be some assertions here */ |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 202 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 203 | /* NOTE: heap->free == 0 */ |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 204 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 205 | if (p->free && p->next->free) { |
| 206 | struct mem_block *q = p->next; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 207 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 208 | assert(p->ofs + p->size == q->ofs); |
| 209 | p->size += q->size; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 210 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 211 | p->next = q->next; |
| 212 | q->next->prev = p; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 213 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 214 | 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Emil Velikov | 5966d37 | 2015-03-31 21:12:14 +0100 | [diff] [blame] | 223 | drm_private int mmFreeMem(struct mem_block *b) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 224 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 225 | if (!b) |
| 226 | return 0; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 227 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 228 | 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 236 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 237 | 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 Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 242 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 243 | Join2Blocks(b); |
| 244 | if (b->prev != b->heap) |
| 245 | Join2Blocks(b->prev); |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 246 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 247 | return 0; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Emil Velikov | 5966d37 | 2015-03-31 21:12:14 +0100 | [diff] [blame] | 250 | drm_private void mmDestroy(struct mem_block *heap) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 251 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 252 | struct mem_block *p; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 253 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 254 | if (!heap) |
| 255 | return; |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 256 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 257 | for (p = heap->next; p != heap;) { |
| 258 | struct mem_block *next = p->next; |
| 259 | free(p); |
| 260 | p = next; |
| 261 | } |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 262 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 263 | free(heap); |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 264 | } |