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 | #ifndef __INCLUDE_ALLOC__ |
| 33 | #define __INCLUDE_ALLOC__ |
| 34 | |
| 35 | #include <string.h> |
| 36 | #include <stdint.h> |
Pierre-Louis Bossart | 81708a5 | 2018-04-04 18:46:50 -0500 | [diff] [blame] | 37 | #include <sof/dma.h> |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 38 | #include <platform/memory.h> |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 39 | |
Pierre-Louis Bossart | 81708a5 | 2018-04-04 18:46:50 -0500 | [diff] [blame] | 40 | struct sof; |
Liam Girdwood | 69222cf | 2017-06-06 16:41:07 +0100 | [diff] [blame] | 41 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 42 | /* Heap Memory Zones |
| 43 | * |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 44 | * The heap has three different zones from where memory can be allocated :- |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 45 | * |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 46 | * 1) System Zone. Fixed size heap where alloc always succeeds and is never |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 47 | * freed. Used by any init code that will never give up the memory. |
| 48 | * |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 49 | * 2) Runtime Zone. Main and larger heap zone where allocs are not guaranteed to |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 50 | * succeed. Memory can be freed here. |
| 51 | * |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 52 | * 3) Buffer Zone. Largest heap zone intended for audio buffers. |
| 53 | * |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 54 | * See platform/memory.h for heap size configuration and mappings. |
| 55 | */ |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 56 | #define RZONE_SYS 0 |
| 57 | #define RZONE_RUNTIME 1 |
| 58 | #define RZONE_BUFFER 2 |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 59 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 60 | struct mm_info { |
| 61 | uint32_t used; |
| 62 | uint32_t free; |
| 63 | }; |
| 64 | |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 65 | struct block_hdr { |
| 66 | uint16_t size; /* size in blocks for continuous allocation */ |
Liam Girdwood | 1f6aee5 | 2018-03-01 16:13:05 +0000 | [diff] [blame] | 67 | uint16_t used; /* usage flags for page */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 68 | } __attribute__ ((packed)); |
| 69 | |
| 70 | struct block_map { |
| 71 | uint16_t block_size; /* size of block in bytes */ |
| 72 | uint16_t count; /* number of blocks in map */ |
| 73 | uint16_t free_count; /* number of free blocks */ |
| 74 | uint16_t first_free; /* index of first free block */ |
| 75 | struct block_hdr *block; /* base block header */ |
| 76 | uint32_t base; /* base address of space */ |
| 77 | } __attribute__ ((packed)); |
| 78 | |
| 79 | #define BLOCK_DEF(sz, cnt, hdr) \ |
| 80 | {.block_size = sz, .count = cnt, .free_count = cnt, .block = hdr} |
| 81 | |
| 82 | struct mm_heap { |
| 83 | uint32_t blocks; |
| 84 | struct block_map *map; |
| 85 | uint32_t heap; |
| 86 | uint32_t size; |
| 87 | uint32_t caps; |
| 88 | struct mm_info info; |
| 89 | }; |
| 90 | |
| 91 | /* heap block memory map */ |
| 92 | struct mm { |
| 93 | /* system heap - used during init cannot be freed */ |
| 94 | struct mm_heap system; |
| 95 | /* general heap for components */ |
| 96 | struct mm_heap runtime[PLATFORM_HEAP_RUNTIME]; |
| 97 | /* general component buffer heap */ |
| 98 | struct mm_heap buffer[PLATFORM_HEAP_BUFFER]; |
| 99 | |
| 100 | struct mm_info total; |
| 101 | spinlock_t lock; /* all allocs and frees are atomic */ |
| 102 | }; |
| 103 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 104 | /* heap allocation and free */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 105 | void *rmalloc(int zone, uint32_t caps, size_t bytes); |
| 106 | void *rzalloc(int zone, uint32_t caps, size_t bytes); |
Liam Girdwood | 50f7b0e | 2017-06-06 12:52:15 +0100 | [diff] [blame] | 107 | void rfree(void *ptr); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 108 | |
| 109 | /* heap allocation and free for buffers on 1k boundary */ |
Liam Girdwood | c760f83 | 2018-03-01 12:15:15 +0000 | [diff] [blame] | 110 | void *rballoc(int zone, uint32_t flags, size_t bytes); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 111 | |
| 112 | /* utility */ |
| 113 | void bzero(void *s, size_t n); |
| 114 | void *memset(void *s, int c, size_t n); |
Yan Wang | 1c6ecd0 | 2017-10-10 18:45:25 +0800 | [diff] [blame] | 115 | int rstrlen(const char *s); |
Liam Girdwood | 5d7ad56 | 2018-04-06 16:23:43 +0100 | [diff] [blame] | 116 | int rstrcmp(const char *s1, const char *s2); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 117 | |
| 118 | /* Heap save/restore contents and context for PM D0/D3 events */ |
| 119 | uint32_t mm_pm_context_size(void); |
Liam Girdwood | 1e5c559 | 2017-10-15 23:36:28 +0100 | [diff] [blame] | 120 | int mm_pm_context_save(struct dma_copy *dc, struct dma_sg_config *sg); |
| 121 | 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] | 122 | |
| 123 | /* heap initialisation */ |
Pierre-Louis Bossart | 81708a5 | 2018-04-04 18:46:50 -0500 | [diff] [blame] | 124 | void init_heap(struct sof *sof); |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 125 | #endif |