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