blob: d5fc1c2db660d2b1b397293f9ce196c13a842820 [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
32#ifndef __INCLUDE_ALLOC__
33#define __INCLUDE_ALLOC__
34
35#include <string.h>
36#include <stdint.h>
Pierre-Louis Bossart81708a52018-04-04 18:46:50 -050037#include <sof/dma.h>
Liam Girdwoodc760f832018-03-01 12:15:15 +000038#include <platform/memory.h>
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010039
Pierre-Louis Bossart81708a52018-04-04 18:46:50 -050040struct sof;
Liam Girdwood69222cf2017-06-06 16:41:07 +010041
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010042/* Heap Memory Zones
43 *
Liam Girdwood50f7b0e2017-06-06 12:52:15 +010044 * The heap has three different zones from where memory can be allocated :-
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010045 *
Liam Girdwood50f7b0e2017-06-06 12:52:15 +010046 * 1) System Zone. Fixed size heap where alloc always succeeds and is never
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010047 * freed. Used by any init code that will never give up the memory.
48 *
Liam Girdwood50f7b0e2017-06-06 12:52:15 +010049 * 2) Runtime Zone. Main and larger heap zone where allocs are not guaranteed to
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010050 * succeed. Memory can be freed here.
51 *
Liam Girdwood50f7b0e2017-06-06 12:52:15 +010052 * 3) Buffer Zone. Largest heap zone intended for audio buffers.
53 *
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010054 * See platform/memory.h for heap size configuration and mappings.
55 */
Liam Girdwood50f7b0e2017-06-06 12:52:15 +010056#define RZONE_SYS 0
57#define RZONE_RUNTIME 1
58#define RZONE_BUFFER 2
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010059
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010060struct mm_info {
61 uint32_t used;
62 uint32_t free;
63};
64
Liam Girdwoodc760f832018-03-01 12:15:15 +000065struct block_hdr {
66 uint16_t size; /* size in blocks for continuous allocation */
Liam Girdwood1f6aee52018-03-01 16:13:05 +000067 uint16_t used; /* usage flags for page */
Liam Girdwoodc760f832018-03-01 12:15:15 +000068} __attribute__ ((packed));
69
70struct 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
82struct 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 */
92struct 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 Girdwoodc0dfb4e2016-09-21 15:57:22 +0100104/* heap allocation and free */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000105void *rmalloc(int zone, uint32_t caps, size_t bytes);
106void *rzalloc(int zone, uint32_t caps, size_t bytes);
Liam Girdwood50f7b0e2017-06-06 12:52:15 +0100107void rfree(void *ptr);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100108
109/* heap allocation and free for buffers on 1k boundary */
Liam Girdwoodc760f832018-03-01 12:15:15 +0000110void *rballoc(int zone, uint32_t flags, size_t bytes);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100111
112/* utility */
113void bzero(void *s, size_t n);
114void *memset(void *s, int c, size_t n);
Yan Wang1c6ecd02017-10-10 18:45:25 +0800115int rstrlen(const char *s);
Liam Girdwood5d7ad562018-04-06 16:23:43 +0100116int rstrcmp(const char *s1, const char *s2);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100117
118/* Heap save/restore contents and context for PM D0/D3 events */
119uint32_t mm_pm_context_size(void);
Liam Girdwood1e5c5592017-10-15 23:36:28 +0100120int mm_pm_context_save(struct dma_copy *dc, struct dma_sg_config *sg);
121int mm_pm_context_restore(struct dma_copy *dc, struct dma_sg_config *sg);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100122
123/* heap initialisation */
Pierre-Louis Bossart81708a52018-04-04 18:46:50 -0500124void init_heap(struct sof *sof);
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100125#endif