blob: 0826cc1b7eecda6267da2c4c3edb8eeed1c8bd88 [file] [log] [blame]
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +01001/*
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 Bossart81708a52018-04-04 18:46:50 -050032#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 Lauda7d38ed12018-08-14 15:24:06 +020038#include <sof/cpu.h>
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010039#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 Girdwoodc760f832018-03-01 12:15:15 +000059extern struct mm memmap;
60
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010061/* 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 Girdwood50f7b0e2017-06-06 12:52:15 +010066 * 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 Girdwoodc0dfb4e2016-09-21 15:57:22 +010068 * 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 Girdwoodc0dfb4e2016-09-21 15:57:22 +010073
74/* total size of block */
75static 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 */
82static 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
95static 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 Girdwood50f7b0e2017-06-06 12:52:15 +0100106static void *rmalloc_sys(size_t bytes)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100107{
Tomasz Lauda7d38ed12018-08-14 15:24:06 +0200108 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 Girdwoodc0dfb4e2016-09-21 15:57:22 +0100117
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100118 /* always succeeds or panics */
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100119 memmap.system.heap += bytes;
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100120 if (memmap.system.heap >= HEAP_SYSTEM_BASE + HEAP_SYSTEM_SIZE) {
Tomasz Lauda7d38ed12018-08-14 15:24:06 +0200121 trace_mem_error("eM1");
Liam Girdwoode52ce682018-02-21 15:36:25 +0000122 panic(SOF_IPC_PANIC_MEM);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100123 }
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 Girdwoodc760f832018-03-01 12:15:15 +0000133static void *alloc_block(struct mm_heap *heap, int level,
134 uint32_t caps)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100135{
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 Girdwoodc0dfb4e2016-09-21 15:57:22 +0100143 hdr->size = 1;
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000144 hdr->used = 1;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100145 heap->info.used += map->block_size;
146 heap->info.free -= map->block_size;
Tomasz Laudabb752042018-07-06 12:09:58 +0200147 dcache_writeback_invalidate_region(hdr, sizeof(*hdr));
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100148
149 /* find next free */
150 for (i = map->first_free; i < map->count; ++i) {
151
152 hdr = &map->block[i];
153
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000154 if (hdr->used == 0) {
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100155 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 Laudabb752042018-07-06 12:09:58 +0200164 dcache_writeback_invalidate_region(map, sizeof(*map));
165 dcache_writeback_invalidate_region(heap, sizeof(*heap));
166
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100167 return ptr;
168}
169
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -0600170/* allocates continuous blocks */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000171static void *alloc_cont_blocks(struct mm_heap *heap, int level,
172 uint32_t caps, size_t bytes)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100173{
174 struct block_map *map = &heap->map[level];
175 struct block_hdr *hdr = &map->block[map->first_free];
176 void *ptr;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500177 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 Girdwoodc0dfb4e2016-09-21 15:57:22 +0100183
184 if (bytes % map->block_size)
185 count++;
186
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -0600187 /* check for continuous blocks from "start" */
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100188 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 Girdwood50f7b0e2017-06-06 12:52:15 +0100195 /* is block used */
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000196 if (hdr->used)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100197 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
209found:
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 Laudabb752042018-07-06 12:09:58 +0200217 dcache_writeback_invalidate_region(hdr, sizeof(*hdr));
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100218
219 /* allocate each block */
220 for (current = start; current < end; current++) {
221 hdr = &map->block[current];
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000222 hdr->used = 1;
Tomasz Laudabb752042018-07-06 12:09:58 +0200223 dcache_writeback_invalidate_region(hdr, sizeof(*hdr));
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100224 }
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 Girdwood1f6aee52018-03-01 16:13:05 +0000234 if (hdr->used == 0) {
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100235 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 Laudabb752042018-07-06 12:09:58 +0200245 dcache_writeback_invalidate_region(map, sizeof(*map));
246 dcache_writeback_invalidate_region(heap, sizeof(*heap));
247
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100248 return ptr;
249}
250
Liam Girdwoodc760f832018-03-01 12:15:15 +0000251static struct mm_heap *get_heap_from_ptr(void *ptr)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100252{
Liam Girdwoodc760f832018-03-01 12:15:15 +0000253 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
276static 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
293static 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) */
311static void free_block(void *ptr)
312{
313 struct mm_heap *heap;
314 struct block_map *block_map;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100315 struct block_hdr *hdr;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500316 int i;
317 int block;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100318
319 /* sanity check */
320 if (ptr == NULL)
321 return;
322
Liam Girdwoodc760f832018-03-01 12:15:15 +0000323 heap = get_heap_from_ptr(ptr);
324 if (heap == NULL)
Keyon Jie14e2d8e2017-09-01 16:41:25 +0800325 return;
326
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100327 /* find block that ptr belongs to */
Slawomir Blauciak297dcaa2018-07-04 14:41:43 +0200328 for (i = 0; i < heap->blocks; i++) {
329 block_map = &heap->map[i];
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100330 /* is ptr in this block */
Slawomir Blauciak297dcaa2018-07-04 14:41:43 +0200331 if ((uint32_t)ptr < (block_map->base +
332 (block_map->block_size * block_map->count)))
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100333 goto found;
334 }
335
336 /* not found */
337 trace_mem_error("eMF");
338 return;
339
340found:
341 /* calculate block header */
Keyon Jie14e2d8e2017-09-01 16:41:25 +0800342 block = ((uint32_t)ptr - block_map->base) / block_map->block_size;
343 hdr = &block_map->block[block];
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100344
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -0600345 /* free block header and continuous blocks */
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100346 for (i = block; i < block + hdr->size; i++) {
Keyon Jie14e2d8e2017-09-01 16:41:25 +0800347 hdr = &block_map->block[i];
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100348 hdr->size = 0;
Liam Girdwood1f6aee52018-03-01 16:13:05 +0000349 hdr->used = 0;
Keyon Jie14e2d8e2017-09-01 16:41:25 +0800350 block_map->free_count++;
351 heap->info.used -= block_map->block_size;
352 heap->info.free += block_map->block_size;
Tomasz Laudabb752042018-07-06 12:09:58 +0200353 dcache_writeback_invalidate_region(hdr, sizeof(*hdr));
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100354 }
355
Liam Girdwoodc760f832018-03-01 12:15:15 +0000356 /* set first free block */
Keyon Jie14e2d8e2017-09-01 16:41:25 +0800357 if (block < block_map->first_free)
358 block_map->first_free = block;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100359
360#if DEBUG_BLOCK_FREE
Keyon Jie14e2d8e2017-09-01 16:41:25 +0800361 alloc_memset_region(ptr, block_map->block_size * (i - 1), DEBUG_BLOCK_FREE_VALUE);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100362#endif
Tomasz Laudabb752042018-07-06 12:09:58 +0200363
364 dcache_writeback_invalidate_region(block_map, sizeof(*block_map));
365 dcache_writeback_invalidate_region(heap, sizeof(*heap));
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100366}
367
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100368/* allocate single block for runtime */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000369static void *rmalloc_runtime(uint32_t caps, size_t bytes)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100370{
Liam Girdwoodc760f832018-03-01 12:15:15 +0000371 struct mm_heap *heap;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100372 int i;
373
Liam Girdwood5972ac32018-03-06 14:15:32 +0000374 /* check runtime heap for capabilities */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000375 heap = get_runtime_heap_from_caps(caps);
Liam Girdwood5972ac32018-03-06 14:15:32 +0000376 if (heap)
377 goto find;
378
379 /* next check buffer heap for capabilities */
380 heap = get_buffer_heap_from_caps(caps);
Liam Girdwoodc760f832018-03-01 12:15:15 +0000381 if (heap == NULL)
382 goto error;
383
Liam Girdwood5972ac32018-03-06 14:15:32 +0000384find:
Liam Girdwoodc760f832018-03-01 12:15:15 +0000385 for (i = 0; i < heap->blocks; i++) {
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100386
387 /* is block big enough */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000388 if (heap->map[i].block_size < bytes)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100389 continue;
390
391 /* does block have free space */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000392 if (heap->map[i].free_count == 0)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100393 continue;
394
395 /* free block space exists */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000396 return alloc_block(heap, i, caps);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100397 }
398
Liam Girdwoodc760f832018-03-01 12:15:15 +0000399error:
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100400 trace_mem_error("eMm");
Ranjani Sridharan210989d2018-03-25 17:34:04 -0700401 trace_error_value(bytes);
402 trace_error_value(caps);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100403 return NULL;
404}
405
Liam Girdwoodc760f832018-03-01 12:15:15 +0000406void *rmalloc(int zone, uint32_t caps, size_t bytes)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100407{
408 uint32_t flags;
409 void *ptr = NULL;
410
411 spin_lock_irq(&memmap.lock, flags);
412
413 switch (zone) {
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100414 case RZONE_SYS:
415 ptr = rmalloc_sys(bytes);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100416 break;
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100417 case RZONE_RUNTIME:
Liam Girdwoodc760f832018-03-01 12:15:15 +0000418 ptr = rmalloc_runtime(caps, bytes);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100419 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 Girdwoodc760f832018-03-01 12:15:15 +0000429void *rzalloc(int zone, uint32_t caps, size_t bytes)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100430{
431 void *ptr = NULL;
432
Liam Girdwoodc760f832018-03-01 12:15:15 +0000433 ptr = rmalloc(zone, caps, bytes);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100434 if (ptr != NULL) {
435 bzero(ptr, bytes);
436 }
437
438 return ptr;
439}
440
Liam Girdwoodc760f832018-03-01 12:15:15 +0000441/* allocates continuous buffers */
442void *rballoc(int zone, uint32_t caps, size_t bytes)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100443{
Liam Girdwoodc760f832018-03-01 12:15:15 +0000444 struct mm_heap *heap;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500445 int i;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100446 uint32_t flags;
447 void *ptr = NULL;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100448
449 spin_lock_irq(&memmap.lock, flags);
450
Liam Girdwoodc760f832018-03-01 12:15:15 +0000451 heap = get_buffer_heap_from_caps(caps);
452 if (heap == NULL)
453 goto out;
454
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100455 /* will request fit in single block */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000456 for (i = 0; i < heap->blocks; i++) {
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100457
458 /* is block big enough */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000459 if (heap->map[i].block_size < bytes)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100460 continue;
461
462 /* does block have free space */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000463 if (heap->map[i].free_count == 0)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100464 continue;
465
466 /* allocate block */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000467 ptr = alloc_block(heap, i, caps);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100468 goto out;
469 }
470
471 /* request spans > 1 block */
472
473 /* only 1 choice for block size */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000474 if (heap->blocks == 1) {
475 ptr = alloc_cont_blocks(heap, 0, caps, bytes);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100476 goto out;
477 } else {
478
479 /* find best block size for request */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000480 for (i = 0; i < heap->blocks; i++) {
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100481
482 /* allocate is block size smaller than request */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000483 if (heap->map[i].block_size < bytes)
484 alloc_cont_blocks(heap, i, caps, bytes);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100485 }
486 }
487
Liam Girdwoodc760f832018-03-01 12:15:15 +0000488 ptr = alloc_cont_blocks(heap, heap->blocks - 1, caps, bytes);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100489
490out:
491 spin_unlock_irq(&memmap.lock, flags);
492 return ptr;
493}
494
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100495void rfree(void *ptr)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100496{
497 uint32_t flags;
498
499 spin_lock_irq(&memmap.lock, flags);
Liam Girdwoodc760f832018-03-01 12:15:15 +0000500 free_block(ptr);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100501 spin_unlock_irq(&memmap.lock, flags);
502}
503
504uint32_t mm_pm_context_size(void)
505{
Liam Girdwoodc760f832018-03-01 12:15:15 +0000506 uint32_t size = 0;
507 int i;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100508
509 /* calc context size for each area */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000510 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 Girdwoodc0dfb4e2016-09-21 15:57:22 +0100514 size += memmap.system.info.used;
515
516 /* add memory maps */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000517 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 Girdwoodc0dfb4e2016-09-21 15:57:22 +0100521 size += heap_get_size(&memmap.system);
522
523 /* recalc totals */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000524 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 Girdwoodc0dfb4e2016-09-21 15:57:22 +0100536
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 Girdwood1e5c5592017-10-15 23:36:28 +0100545int mm_pm_context_save(struct dma_copy *dc, struct dma_sg_config *sg)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100546{
547 uint32_t used;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500548 int32_t offset = 0;
549 int32_t ret;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100550
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 Girdwood1e5c5592017-10-15 23:36:28 +0100557 ret = dma_copy_to_host(dc, sg, offset,
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100558 (void *)&memmap, sizeof(memmap));
559 if (ret < 0)
560 return ret;
561
562 /* copy system memory contents to SG */
Liam Girdwood1e5c5592017-10-15 23:36:28 +0100563 ret = dma_copy_to_host(dc, sg, offset + ret,
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100564 (void *)memmap.system.heap, (int32_t)(memmap.system.size));
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100565 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 Bossartf9458092017-11-09 15:24:07 -0600580 * Restore the DSP memories to modules and the system. This must be called immediately
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100581 * after booting before any pipeline work.
582 */
Liam Girdwood1e5c5592017-10-15 23:36:28 +0100583int mm_pm_context_restore(struct dma_copy *dc, struct dma_sg_config *sg)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100584{
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500585 int32_t offset = 0;
586 int32_t ret;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100587
588 /* copy memory maps from SG */
Liam Girdwood1e5c5592017-10-15 23:36:28 +0100589 ret = dma_copy_from_host(dc, sg, offset,
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100590 (void *)&memmap, sizeof(memmap));
591 if (ret < 0)
592 return ret;
593
594 /* copy system memory contents from SG */
Liam Girdwood1e5c5592017-10-15 23:36:28 +0100595 ret = dma_copy_to_host(dc, sg, offset + ret,
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100596 (void *)memmap.system.heap, (int32_t)(memmap.system.size));
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100597 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 Bossart81708a52018-04-04 18:46:50 -0500612void init_heap(struct sof *sof)
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100613{
Liam Girdwoodc760f832018-03-01 12:15:15 +0000614 struct mm_heap *heap;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500615 struct block_map *next_map;
616 struct block_map *current_map;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100617 int i;
Liam Girdwoodc760f832018-03-01 12:15:15 +0000618 int j;
619 int k;
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100620
Liam Girdwood3ef88cf2018-02-19 15:34:09 +0000621 /* sanity check for malformed images or loader issues */
622 if (memmap.system.heap != HEAP_SYSTEM_BASE)
623 panic(SOF_IPC_PANIC_MEM);
624
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100625 spinlock_init(&memmap.lock);
626
627 /* initialise buffer map */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000628 for (i = 0; i < PLATFORM_HEAP_BUFFER; i++) {
629 heap = &memmap.buffer[i];
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100630
Liam Girdwoodc760f832018-03-01 12:15:15 +0000631 for (j = 0; j < heap->blocks; j++) {
632
633 current_map = &heap->map[j];
634 current_map->base = heap->heap;
Tomasz Laudabb752042018-07-06 12:09:58 +0200635 dcache_writeback_region(current_map,
636 sizeof(*current_map));
Liam Girdwoodc760f832018-03-01 12:15:15 +0000637
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 Laudabb752042018-07-06 12:09:58 +0200644 dcache_writeback_region(current_map,
645 sizeof(*current_map));
Liam Girdwoodc760f832018-03-01 12:15:15 +0000646 }
647 }
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100648 }
649
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100650 /* initialise runtime map */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000651 for (i = 0; i < PLATFORM_HEAP_RUNTIME; i++) {
652 heap = &memmap.runtime[i];
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100653
Liam Girdwoodc760f832018-03-01 12:15:15 +0000654 for (j = 0; j < heap->blocks; j++) {
655
656 current_map = &heap->map[j];
657 current_map->base = heap->heap;
Tomasz Laudabb752042018-07-06 12:09:58 +0200658 dcache_writeback_region(current_map,
659 sizeof(*current_map));
Liam Girdwoodc760f832018-03-01 12:15:15 +0000660
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 Laudabb752042018-07-06 12:09:58 +0200667 dcache_writeback_region(current_map,
668 sizeof(*current_map));
Liam Girdwoodc760f832018-03-01 12:15:15 +0000669 }
670 }
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100671 }
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100672}