summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/include/sys/xt-shmem/xf-mem.h
blob: dc277871d844e5525b7d436c321c0398f3ff8c85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

/*******************************************************************************
 * xf-mem.h
 *
 * System-specific memory allocator
 *
 *******************************************************************************/

#ifndef __XF_H
#error "xf-mem.h mustn't be included directly"
#endif

/*******************************************************************************
 * System specific memory pools
 ******************************************************************************/

#if XF_CFG_CORES_NUM > 1
/* ...shared memory pool for communication within DSP-cluster */
extern xf_mm_pool_t     xf_dsp_shmem_pool;
#endif

/*******************************************************************************
 * Platform-specific SHMEM allocation registering functions
 ******************************************************************************/

/* ...register shmem allocation address */
static inline void xf_shmem_alloc_addref(u32 core, xf_message_t *m)
{
}

/* ...unregister shmem allocation address */
static inline void xf_shmem_alloc_rmref(u32 core, xf_message_t *m)
{
}

/*******************************************************************************
 * API functions
 ******************************************************************************/

/* ...allocate aligned memory on particular core specifying if it is shared */
static inline void * xf_mem_alloc(u32 size, u32 align, u32 core, u32 shared)
{
#if XF_CFG_CORES_NUM > 1    
    if (shared)
    {
        /* ...if memory is shared, core is dropped */
        return xf_mm_alloc(&xf_dsp_shmem_pool, size);
    }
#endif
    
    /* ...select local memory pool basing on core specification */
    return xf_mm_alloc(&XF_CORE_DATA(core)->local_pool, size);
}
#ifdef XAF_ENABLE_NON_HIKEY
/* ...redefine macro to add bugchecks */
#define xf_mem_alloc(size, align, core, shared)                                 \
({                                                                              \
    void *__data;                                                               \
    /* ...size must be properly aligned */                                      \
    BUG(!XF_MM_ALIGNED(size), _x("Bad size: %u"), size);                        \
    __data = (xf_mem_alloc)(size, align, core, shared);                         \
    TRACE(1, _b("alloc-%u: %p[%u] (shared=%u)"), core, __data, size, shared);   \
    __data;                                                                     \
})
#endif
/* ...release allocated memory */
static inline void xf_mem_free(void *p, u32 size, u32 core, u32 shared)
{
#if XF_CFG_CORES_NUM > 1    
    if (shared)
    {
        /* ...if memory is shared, core is dropped */
        xf_mm_free(&xf_dsp_shmem_pool, p, size);
        return;
    }
#endif
    
    /* ...select proper pool basing on core specification */
    xf_mm_free(&XF_CORE_DATA(core)->local_pool, p, size);
}
#ifdef XAF_ENABLE_NON_HIKEY
/* ...redefine macro to add bugchecks */
#define xf_mem_free(p, size, core, shared)                                      \
({                                                                              \
    void *__data = (p);                                                         \
    /* ...size must be properly aligned */                                      \
    BUG(!XF_MM_ALIGNED(size), _x("Bad size: %u"), size);                        \
    TRACE(1, _b("free-%u: %p[%u] (shared=%u)"), core, __data, size, shared);    \
    (xf_mem_free)(__data, size, core, shared);                                  \
})
#endif
/* ...allocate AP-DSP shared memory */
static inline int xf_shmem_alloc(u32 core, xf_message_t *m)
{
    xf_mm_pool_t   *pool = &XF_CORE_DATA(core)->shared_pool;

    /* ...length is always cache-line aligned */    
    if ((m->buffer = xf_mm_alloc(pool, XF_ALIGNED(m->length))) != NULL)
    {
        /* ...register allocation address */
        xf_shmem_alloc_addref(core, m);

        return 0;
    }
    else
    {
        return -ENOMEM;
    }
}

/* ...free AP-DSP shared memory */
static inline void xf_shmem_free(u32 core, xf_message_t *m)
{
    xf_mm_pool_t   *pool = &XF_CORE_DATA(core)->shared_pool;

    /* ...length is always cache-line aligned */
    xf_mm_free(pool, m->buffer, XF_ALIGNED(m->length));

    /* ...unregister allocation address */
    xf_shmem_alloc_rmref(core, m);
}

/*******************************************************************************
 * Scratch memory management
 ******************************************************************************/

static inline void * xf_scratch_mem_init(u32 core)
{
    /* ...allocate scratch memory from local DSP memory */
    return xf_mem_alloc(XF_CFG_CODEC_SCRATCHMEM_SIZE, XF_CFG_CODEC_SCRATCHMEM_ALIGN, core, 0);
}

/*******************************************************************************
 * Helpers - hmm; they are platform-independent - tbd
 ******************************************************************************/

/* ...allocate local buffer */
static inline int xf_mm_alloc_buffer(u32 size, u32 align, u32 core, xf_mm_buffer_t *b)
{
    /* ...allocate memory from proper local pool */
    if ((size = XF_MM(size)) != 0)
        XF_CHK_ERR(b->addr = xf_mem_alloc(size, align, core, 0), -ENOMEM);
    else
        b->addr = NULL;

    /* ...save address */
    b->size = size;
    
    return 0;
}

/* ...free local buffer */
static inline void  xf_mm_free_buffer(xf_mm_buffer_t *b, u32 core)
{
    if (b->addr)
    {
        xf_mem_free(b->addr, b->size, core, 0);
    }
}