Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Intel Corporation |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the Intel Corporation nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> |
| 29 | * Keyon Jie <yang.jie@linux.intel.com> |
| 30 | */ |
| 31 | |
| 32 | #include <reef/alloc.h> |
| 33 | #include <reef/reef.h> |
| 34 | #include <reef/debug.h> |
Liam Girdwood | e52ce68 | 2018-02-21 15:36:25 +0000 | [diff] [blame] | 35 | #include <reef/panic.h> |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 36 | #include <reef/trace.h> |
| 37 | #include <reef/lock.h> |
| 38 | #include <platform/memory.h> |
| 39 | #include <stdint.h> |
| 40 | |
| 41 | /* debug to set memory value on every allocation */ |
| 42 | #define DEBUG_BLOCK_ALLOC 0 |
| 43 | #define DEBUG_BLOCK_ALLOC_VALUE 0x6b6b6b6b |
| 44 | |
| 45 | /* debug to set memory value on every free TODO: not working atm */ |
| 46 | #define DEBUG_BLOCK_FREE 0 |
| 47 | #define DEBUG_BLOCK_FREE_VALUE 0x5a5a5a5a |
| 48 | |
| 49 | /* memory tracing support */ |
| 50 | #if DEBUG_BLOCK_ALLOC || DEBUG_BLOCK_FREE |
| 51 | #define trace_mem(__e) trace_event(TRACE_CLASS_MEM, __e) |
| 52 | #else |
| 53 | #define trace_mem(__e) |
| 54 | #endif |
| 55 | |
| 56 | #define trace_mem_error(__e) trace_error(TRACE_CLASS_MEM, __e) |
| 57 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 58 | extern struct mm memmap; |
| 59 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 60 | /* We have 3 memory pools |
| 61 | * |
| 62 | * 1) System memory pool does not have a map and it's size is fixed at build |
| 63 | * time. Memory cannot be freed from this pool. Used by device drivers |
| 64 | * and any system core. Saved as part of PM context. |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 65 | * 2) Runtime memory pool has variable size allocation map and memory is freed |
| 66 | * on calls to rfree(). Saved as part of PM context. Global size |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 67 | * set at build time. |
| 68 | * 3) Buffer memory pool has fixed size allocation map and can be freed on |
| 69 | * module removal or calls to rfree(). Saved as part of PM context. |
| 70 | */ |
| 71 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 72 | |
| 73 | /* total size of block */ |
| 74 | static inline uint32_t block_get_size(struct block_map *map) |
| 75 | { |
| 76 | return sizeof(*map) + map->count * |
| 77 | (map->block_size + sizeof(struct block_hdr)); |
| 78 | } |
| 79 | |
| 80 | /* total size of heap */ |
| 81 | static inline uint32_t heap_get_size(struct mm_heap *heap) |
| 82 | { |
| 83 | uint32_t size = sizeof(struct mm_heap); |
| 84 | int i; |
| 85 | |
| 86 | for (i = 0; i < heap->blocks; i++) { |
| 87 | size += block_get_size(&heap->map[i]); |
| 88 | } |
| 89 | |
| 90 | return size; |
| 91 | } |
| 92 | |
| 93 | #if DEBUG_BLOCK_ALLOC || DEBUG_BLOCK_FREE |
| 94 | static void alloc_memset_region(void *ptr, uint32_t bytes, uint32_t val) |
| 95 | { |
| 96 | uint32_t count = bytes >> 2; |
| 97 | uint32_t *dest = ptr, i; |
| 98 | |
| 99 | for (i = 0; i < count; i++) |
| 100 | dest[i] = val; |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | /* allocate from system memory pool */ |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 105 | static void *rmalloc_sys(size_t bytes) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 106 | { |
| 107 | void *ptr = (void *)memmap.system.heap; |
| 108 | |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 109 | /* always succeeds or panics */ |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 110 | memmap.system.heap += bytes; |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 111 | if (memmap.system.heap >= HEAP_SYSTEM_BASE + HEAP_SYSTEM_SIZE) { |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 112 | trace_mem_error("eMd"); |
Liam Girdwood | e52ce68 | 2018-02-21 15:36:25 +0000 | [diff] [blame] | 113 | panic(SOF_IPC_PANIC_MEM); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | #if DEBUG_BLOCK_ALLOC |
| 117 | alloc_memset_region(ptr, bytes, DEBUG_BLOCK_ALLOC_VALUE); |
| 118 | #endif |
| 119 | |
| 120 | return ptr; |
| 121 | } |
| 122 | |
| 123 | /* allocate single block */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 124 | static void *alloc_block(struct mm_heap *heap, int level, |
| 125 | uint32_t caps) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 126 | { |
| 127 | struct block_map *map = &heap->map[level]; |
| 128 | struct block_hdr *hdr = &map->block[map->first_free]; |
| 129 | void *ptr; |
| 130 | int i; |
| 131 | |
| 132 | map->free_count--; |
| 133 | ptr = (void *)(map->base + map->first_free * map->block_size); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 134 | hdr->size = 1; |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 135 | hdr->used = 1; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 136 | heap->info.used += map->block_size; |
| 137 | heap->info.free -= map->block_size; |
| 138 | |
| 139 | /* find next free */ |
| 140 | for (i = map->first_free; i < map->count; ++i) { |
| 141 | |
| 142 | hdr = &map->block[i]; |
| 143 | |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 144 | if (hdr->used == 0) { |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 145 | map->first_free = i; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #if DEBUG_BLOCK_ALLOC |
| 151 | alloc_memset_region(ptr, map->block_size, DEBUG_BLOCK_ALLOC_VALUE); |
| 152 | #endif |
| 153 | |
| 154 | return ptr; |
| 155 | } |
| 156 | |
Pierre-Louis Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 157 | /* allocates continuous blocks */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 158 | static void *alloc_cont_blocks(struct mm_heap *heap, int level, |
| 159 | uint32_t caps, size_t bytes) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 160 | { |
| 161 | struct block_map *map = &heap->map[level]; |
| 162 | struct block_hdr *hdr = &map->block[map->first_free]; |
| 163 | void *ptr; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 164 | unsigned int start; |
| 165 | unsigned int current; |
| 166 | unsigned int count = bytes / map->block_size; |
| 167 | unsigned int i; |
| 168 | unsigned int remaining = map->count - count; |
| 169 | unsigned int end; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 170 | |
| 171 | if (bytes % map->block_size) |
| 172 | count++; |
| 173 | |
Pierre-Louis Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 174 | /* check for continuous blocks from "start" */ |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 175 | for (start = map->first_free; start < remaining; start++) { |
| 176 | |
| 177 | /* check that we have enough free blocks from start pos */ |
| 178 | end = start + count; |
| 179 | for (current = start; current < end; current++) { |
| 180 | hdr = &map->block[current]; |
| 181 | |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 182 | /* is block used */ |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 183 | if (hdr->used) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | |
| 187 | /* enough free blocks ? */ |
| 188 | if (current == end) |
| 189 | goto found; |
| 190 | } |
| 191 | |
| 192 | /* not found */ |
| 193 | trace_mem_error("eCb"); |
| 194 | return NULL; |
| 195 | |
| 196 | found: |
| 197 | /* found some free blocks */ |
| 198 | map->free_count -= count; |
| 199 | ptr = (void *)(map->base + start * map->block_size); |
| 200 | hdr = &map->block[start]; |
| 201 | hdr->size = count; |
| 202 | heap->info.used += count * map->block_size; |
| 203 | heap->info.free -= count * map->block_size; |
| 204 | |
| 205 | /* allocate each block */ |
| 206 | for (current = start; current < end; current++) { |
| 207 | hdr = &map->block[current]; |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 208 | hdr->used = 1; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* do we need to find a new first free block ? */ |
| 212 | if (start == map->first_free) { |
| 213 | |
| 214 | /* find next free */ |
| 215 | for (i = map->first_free + count; i < map->count; ++i) { |
| 216 | |
| 217 | hdr = &map->block[i]; |
| 218 | |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 219 | if (hdr->used == 0) { |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 220 | map->first_free = i; |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | #if DEBUG_BLOCK_ALLOC |
| 227 | alloc_memset_region(ptr, bytes, DEBUG_BLOCK_ALLOC_VALUE); |
| 228 | #endif |
| 229 | |
| 230 | return ptr; |
| 231 | } |
| 232 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 233 | static struct mm_heap *get_heap_from_ptr(void *ptr) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 234 | { |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 235 | struct mm_heap *heap; |
| 236 | int i; |
| 237 | |
| 238 | /* find mm_heap that ptr belongs to */ |
| 239 | for (i = 0; i < PLATFORM_HEAP_RUNTIME; i++) { |
| 240 | heap = &memmap.runtime[i]; |
| 241 | |
| 242 | if ((uint32_t)ptr >= heap->heap && |
| 243 | (uint32_t)ptr < heap->heap + heap->size) |
| 244 | return heap; |
| 245 | } |
| 246 | |
| 247 | for (i = 0; i < PLATFORM_HEAP_BUFFER; i++) { |
| 248 | heap = &memmap.buffer[i]; |
| 249 | |
| 250 | if ((uint32_t)ptr >= heap->heap && |
| 251 | (uint32_t)ptr < heap->heap + heap->size) |
| 252 | return heap; |
| 253 | } |
| 254 | |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | static struct mm_heap *get_runtime_heap_from_caps(uint32_t caps) |
| 259 | { |
| 260 | struct mm_heap *heap; |
| 261 | uint32_t mask; |
| 262 | int i; |
| 263 | |
| 264 | /* find first heap that support type */ |
| 265 | for (i = 0; i < PLATFORM_HEAP_RUNTIME; i++) { |
| 266 | heap = &memmap.runtime[i]; |
| 267 | mask = heap->caps & caps; |
| 268 | if (mask == caps) |
| 269 | return heap; |
| 270 | } |
| 271 | |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | static struct mm_heap *get_buffer_heap_from_caps(uint32_t caps) |
| 276 | { |
| 277 | struct mm_heap *heap; |
| 278 | uint32_t mask; |
| 279 | int i; |
| 280 | |
| 281 | /* find first heap that support type */ |
| 282 | for (i = 0; i < PLATFORM_HEAP_BUFFER; i++) { |
| 283 | heap = &memmap.buffer[i]; |
| 284 | mask = heap->caps & caps; |
| 285 | if (mask == caps) |
| 286 | return heap; |
| 287 | } |
| 288 | |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | /* free block(s) */ |
| 293 | static void free_block(void *ptr) |
| 294 | { |
| 295 | struct mm_heap *heap; |
| 296 | struct block_map *block_map; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 297 | struct block_hdr *hdr; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 298 | int i; |
| 299 | int block; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 300 | |
| 301 | /* sanity check */ |
| 302 | if (ptr == NULL) |
| 303 | return; |
| 304 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 305 | heap = get_heap_from_ptr(ptr); |
| 306 | if (heap == NULL) |
Keyon Jie | 14e2d8e | 2017-09-01 16:41:25 +0800 | [diff] [blame] | 307 | return; |
| 308 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 309 | /* find block that ptr belongs to */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 310 | for (i = 0; i < heap->blocks - 1; i++) { |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 311 | |
| 312 | /* is ptr in this block */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 313 | if ((uint32_t)ptr < heap->map[i + 1].base) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 314 | goto found; |
| 315 | } |
| 316 | |
| 317 | /* not found */ |
| 318 | trace_mem_error("eMF"); |
| 319 | return; |
| 320 | |
| 321 | found: |
Keyon Jie | 14e2d8e | 2017-09-01 16:41:25 +0800 | [diff] [blame] | 322 | /* the block i is it */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 323 | block_map = &heap->map[i]; |
Keyon Jie | 14e2d8e | 2017-09-01 16:41:25 +0800 | [diff] [blame] | 324 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 325 | /* calculate block header */ |
Keyon Jie | 14e2d8e | 2017-09-01 16:41:25 +0800 | [diff] [blame] | 326 | block = ((uint32_t)ptr - block_map->base) / block_map->block_size; |
| 327 | hdr = &block_map->block[block]; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 328 | |
Pierre-Louis Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 329 | /* free block header and continuous blocks */ |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 330 | for (i = block; i < block + hdr->size; i++) { |
Keyon Jie | 14e2d8e | 2017-09-01 16:41:25 +0800 | [diff] [blame] | 331 | hdr = &block_map->block[i]; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 332 | hdr->size = 0; |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 333 | hdr->used = 0; |
Keyon Jie | 14e2d8e | 2017-09-01 16:41:25 +0800 | [diff] [blame] | 334 | block_map->free_count++; |
| 335 | heap->info.used -= block_map->block_size; |
| 336 | heap->info.free += block_map->block_size; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 339 | /* set first free block */ |
Keyon Jie | 14e2d8e | 2017-09-01 16:41:25 +0800 | [diff] [blame] | 340 | if (block < block_map->first_free) |
| 341 | block_map->first_free = block; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 342 | |
| 343 | #if DEBUG_BLOCK_FREE |
Keyon Jie | 14e2d8e | 2017-09-01 16:41:25 +0800 | [diff] [blame] | 344 | alloc_memset_region(ptr, block_map->block_size * (i - 1), DEBUG_BLOCK_FREE_VALUE); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 345 | #endif |
| 346 | } |
| 347 | |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 348 | /* allocate single block for runtime */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 349 | static void *rmalloc_runtime(uint32_t caps, size_t bytes) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 350 | { |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 351 | struct mm_heap *heap; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 352 | int i; |
| 353 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 354 | heap = get_runtime_heap_from_caps(caps); |
| 355 | if (heap == NULL) |
| 356 | goto error; |
| 357 | |
| 358 | for (i = 0; i < heap->blocks; i++) { |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 359 | |
| 360 | /* is block big enough */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 361 | if (heap->map[i].block_size < bytes) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 362 | continue; |
| 363 | |
| 364 | /* does block have free space */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 365 | if (heap->map[i].free_count == 0) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 366 | continue; |
| 367 | |
| 368 | /* free block space exists */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 369 | return alloc_block(heap, i, caps); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 372 | error: |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 373 | trace_mem_error("eMm"); |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 374 | trace_value(bytes); |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 375 | trace_value(caps); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 376 | return NULL; |
| 377 | } |
| 378 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 379 | void *rmalloc(int zone, uint32_t caps, size_t bytes) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 380 | { |
| 381 | uint32_t flags; |
| 382 | void *ptr = NULL; |
| 383 | |
| 384 | spin_lock_irq(&memmap.lock, flags); |
| 385 | |
| 386 | switch (zone) { |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 387 | case RZONE_SYS: |
| 388 | ptr = rmalloc_sys(bytes); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 389 | break; |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 390 | case RZONE_RUNTIME: |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 391 | ptr = rmalloc_runtime(caps, bytes); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 392 | break; |
| 393 | default: |
| 394 | trace_mem_error("eMz"); |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | spin_unlock_irq(&memmap.lock, flags); |
| 399 | return ptr; |
| 400 | } |
| 401 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 402 | void *rzalloc(int zone, uint32_t caps, size_t bytes) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 403 | { |
| 404 | void *ptr = NULL; |
| 405 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 406 | ptr = rmalloc(zone, caps, bytes); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 407 | if (ptr != NULL) { |
| 408 | bzero(ptr, bytes); |
| 409 | } |
| 410 | |
| 411 | return ptr; |
| 412 | } |
| 413 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 414 | /* allocates continuous buffers */ |
| 415 | void *rballoc(int zone, uint32_t caps, size_t bytes) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 416 | { |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 417 | struct mm_heap *heap; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 418 | int i; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 419 | uint32_t flags; |
| 420 | void *ptr = NULL; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 421 | |
| 422 | spin_lock_irq(&memmap.lock, flags); |
| 423 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 424 | heap = get_buffer_heap_from_caps(caps); |
| 425 | if (heap == NULL) |
| 426 | goto out; |
| 427 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 428 | /* will request fit in single block */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 429 | for (i = 0; i < heap->blocks; i++) { |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 430 | |
| 431 | /* is block big enough */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 432 | if (heap->map[i].block_size < bytes) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 433 | continue; |
| 434 | |
| 435 | /* does block have free space */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 436 | if (heap->map[i].free_count == 0) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 437 | continue; |
| 438 | |
| 439 | /* allocate block */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 440 | ptr = alloc_block(heap, i, caps); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 441 | goto out; |
| 442 | } |
| 443 | |
| 444 | /* request spans > 1 block */ |
| 445 | |
| 446 | /* only 1 choice for block size */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 447 | if (heap->blocks == 1) { |
| 448 | ptr = alloc_cont_blocks(heap, 0, caps, bytes); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 449 | goto out; |
| 450 | } else { |
| 451 | |
| 452 | /* find best block size for request */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 453 | for (i = 0; i < heap->blocks; i++) { |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 454 | |
| 455 | /* allocate is block size smaller than request */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 456 | if (heap->map[i].block_size < bytes) |
| 457 | alloc_cont_blocks(heap, i, caps, bytes); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 461 | ptr = alloc_cont_blocks(heap, heap->blocks - 1, caps, bytes); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 462 | |
| 463 | out: |
| 464 | spin_unlock_irq(&memmap.lock, flags); |
| 465 | return ptr; |
| 466 | } |
| 467 | |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 468 | void rfree(void *ptr) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 469 | { |
| 470 | uint32_t flags; |
| 471 | |
| 472 | spin_lock_irq(&memmap.lock, flags); |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 473 | free_block(ptr); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 474 | spin_unlock_irq(&memmap.lock, flags); |
| 475 | } |
| 476 | |
| 477 | uint32_t mm_pm_context_size(void) |
| 478 | { |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 479 | uint32_t size = 0; |
| 480 | int i; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 481 | |
| 482 | /* calc context size for each area */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 483 | for (i = 0; i < PLATFORM_HEAP_BUFFER; i++) |
| 484 | size += memmap.buffer[i].info.used; |
| 485 | for (i = 0; i < PLATFORM_HEAP_RUNTIME; i++) |
| 486 | size += memmap.runtime[i].info.used; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 487 | size += memmap.system.info.used; |
| 488 | |
| 489 | /* add memory maps */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 490 | for (i = 0; i < PLATFORM_HEAP_BUFFER; i++) |
| 491 | size += heap_get_size(&memmap.buffer[i]); |
| 492 | for (i = 0; i < PLATFORM_HEAP_RUNTIME; i++) |
| 493 | size += heap_get_size(&memmap.runtime[i]); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 494 | size += heap_get_size(&memmap.system); |
| 495 | |
| 496 | /* recalc totals */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 497 | memmap.total.free = memmap.system.info.free; |
| 498 | memmap.total.used = memmap.system.info.used; |
| 499 | |
| 500 | for (i = 0; i < PLATFORM_HEAP_BUFFER; i++) { |
| 501 | memmap.total.free += memmap.buffer[i].info.free; |
| 502 | memmap.total.used += memmap.buffer[i].info.used; |
| 503 | } |
| 504 | |
| 505 | for (i = 0; i < PLATFORM_HEAP_RUNTIME; i++) { |
| 506 | memmap.total.free = memmap.runtime[i].info.free; |
| 507 | memmap.total.used = memmap.runtime[i].info.used; |
| 508 | } |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 509 | |
| 510 | return size; |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Save the DSP memories that are in use the system and modules. All pipeline and modules |
| 515 | * must be disabled before calling this functions. No allocations are permitted after |
| 516 | * calling this and before calling restore. |
| 517 | */ |
Liam Girdwood | 1e5c559 | 2017-10-15 23:36:28 +0100 | [diff] [blame] | 518 | int mm_pm_context_save(struct dma_copy *dc, struct dma_sg_config *sg) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 519 | { |
| 520 | uint32_t used; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 521 | int32_t offset = 0; |
| 522 | int32_t ret; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 523 | |
| 524 | /* first make sure SG buffer has enough space on host for DSP context */ |
| 525 | used = mm_pm_context_size(); |
| 526 | if (used > dma_sg_get_size(sg)) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | /* copy memory maps to SG */ |
Liam Girdwood | 1e5c559 | 2017-10-15 23:36:28 +0100 | [diff] [blame] | 530 | ret = dma_copy_to_host(dc, sg, offset, |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 531 | (void *)&memmap, sizeof(memmap)); |
| 532 | if (ret < 0) |
| 533 | return ret; |
| 534 | |
| 535 | /* copy system memory contents to SG */ |
Liam Girdwood | 1e5c559 | 2017-10-15 23:36:28 +0100 | [diff] [blame] | 536 | ret = dma_copy_to_host(dc, sg, offset + ret, |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 537 | (void *)memmap.system.heap, (int32_t)(memmap.system.size)); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 538 | if (ret < 0) |
| 539 | return ret; |
| 540 | |
| 541 | /* copy module memory contents to SG */ |
| 542 | // TODO: iterate over module block map and copy contents of each block |
| 543 | // to the host. |
| 544 | |
| 545 | /* copy buffer memory contents to SG */ |
| 546 | // TODO: iterate over buffer block map and copy contents of each block |
| 547 | // to the host. |
| 548 | |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | /* |
Pierre-Louis Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 553 | * Restore the DSP memories to modules and the system. This must be called immediately |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 554 | * after booting before any pipeline work. |
| 555 | */ |
Liam Girdwood | 1e5c559 | 2017-10-15 23:36:28 +0100 | [diff] [blame] | 556 | int mm_pm_context_restore(struct dma_copy *dc, struct dma_sg_config *sg) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 557 | { |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 558 | int32_t offset = 0; |
| 559 | int32_t ret; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 560 | |
| 561 | /* copy memory maps from SG */ |
Liam Girdwood | 1e5c559 | 2017-10-15 23:36:28 +0100 | [diff] [blame] | 562 | ret = dma_copy_from_host(dc, sg, offset, |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 563 | (void *)&memmap, sizeof(memmap)); |
| 564 | if (ret < 0) |
| 565 | return ret; |
| 566 | |
| 567 | /* copy system memory contents from SG */ |
Liam Girdwood | 1e5c559 | 2017-10-15 23:36:28 +0100 | [diff] [blame] | 568 | ret = dma_copy_to_host(dc, sg, offset + ret, |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 569 | (void *)memmap.system.heap, (int32_t)(memmap.system.size)); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 570 | if (ret < 0) |
| 571 | return ret; |
| 572 | |
| 573 | /* copy module memory contents from SG */ |
| 574 | // TODO: iterate over module block map and copy contents of each block |
| 575 | // to the host. This is the same block order used by the context store |
| 576 | |
| 577 | /* copy buffer memory contents from SG */ |
| 578 | // TODO: iterate over buffer block map and copy contents of each block |
| 579 | // to the host. This is the same block order used by the context store |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /* initialise map */ |
Liam Girdwood | 69222cf | 2017-06-06 16:41:07 +0100 | [diff] [blame] | 585 | void init_heap(struct reef *reef) |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 586 | { |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 587 | struct mm_heap *heap; |
Pierre-Louis Bossart | 4ccf81d | 2017-09-25 14:52:09 -0500 | [diff] [blame] | 588 | struct block_map *next_map; |
| 589 | struct block_map *current_map; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 590 | int i; |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 591 | int j; |
| 592 | int k; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 593 | |
Liam Girdwood | 3ef88cf | 2018-02-19 15:34:09 +0000 | [diff] [blame] | 594 | /* sanity check for malformed images or loader issues */ |
| 595 | if (memmap.system.heap != HEAP_SYSTEM_BASE) |
| 596 | panic(SOF_IPC_PANIC_MEM); |
| 597 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 598 | spinlock_init(&memmap.lock); |
| 599 | |
| 600 | /* initialise buffer map */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 601 | for (i = 0; i < PLATFORM_HEAP_BUFFER; i++) { |
| 602 | heap = &memmap.buffer[i]; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 603 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 604 | for (j = 0; j < heap->blocks; j++) { |
| 605 | |
| 606 | current_map = &heap->map[j]; |
| 607 | current_map->base = heap->heap; |
| 608 | |
| 609 | for (k = 1; k < heap->blocks; k++) { |
| 610 | next_map = &heap->map[k]; |
| 611 | next_map->base = current_map->base + |
| 612 | current_map->block_size * |
| 613 | current_map->count; |
| 614 | current_map = &heap->map[k]; |
| 615 | } |
| 616 | } |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 619 | /* initialise runtime map */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 620 | for (i = 0; i < PLATFORM_HEAP_RUNTIME; i++) { |
| 621 | heap = &memmap.runtime[i]; |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 622 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 623 | for (j = 0; j < heap->blocks; j++) { |
| 624 | |
| 625 | current_map = &heap->map[j]; |
| 626 | current_map->base = heap->heap; |
| 627 | |
| 628 | for (k = 1; k < heap->blocks; k++) { |
| 629 | next_map = &heap->map[k]; |
| 630 | next_map->base = current_map->base + |
| 631 | current_map->block_size * |
| 632 | current_map->count; |
| 633 | current_map = &heap->map[k]; |
| 634 | } |
| 635 | } |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 636 | } |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 637 | } |