Skip to content

Commit c25c6e3

Browse files
committed
module-adapter: allocate control objects on an own heap
We want to be able to serve all module memory allocations from a private heap. This commit creates such a heap for DP scheduled modules and moves struct comp_dev and struct processing_module to it. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 23802ef commit c25c6e3

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

src/audio/module_adapter/module_adapter.c

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,65 @@ struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
7474
return NULL;
7575
}
7676

77-
dev = comp_alloc(drv, sizeof(*dev));
78-
if (!dev) {
79-
comp_cl_err(drv, "module_adapter_new(), failed to allocate memory for comp_dev");
80-
return NULL;
77+
uint8_t *mod_heap_mem;
78+
struct k_heap *mod_heap;
79+
int flags;
80+
81+
if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP) {
82+
const size_t heap_size = 8 * 1024;
83+
84+
/* Keep uncached to match the default SOF heap! */
85+
mod_heap_mem = rballoc_align(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
86+
heap_size, 4096);
87+
if (!mod_heap_mem)
88+
return NULL;
89+
90+
const size_t heap_pref_size = ALIGN_UP(sizeof(*mod_heap), 8);
91+
void *mod_heap_buf = mod_heap_mem + heap_pref_size;
92+
93+
mod_heap = (struct k_heap *)mod_heap_mem;
94+
k_heap_init(mod_heap, mod_heap_buf, heap_size - heap_pref_size);
95+
96+
flags = SOF_MEM_FLAG_COHERENT;
97+
} else {
98+
mod_heap_mem = NULL;
99+
mod_heap = NULL;
100+
101+
flags = 0;
81102
}
82-
dev->ipc_config = *config;
83103

84104
/* allocate module information.
85105
* for DP shared modules this struct must be accessible from all cores
86106
* Unfortunately at this point there's no information of components the module
87107
* will be bound to. So we need to allocate shared memory for each DP module
88108
* To be removed when pipeline 2.0 is ready
89109
*/
90-
int flags = config->proc_domain == COMP_PROCESSING_DOMAIN_DP ?
91-
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT : SOF_MEM_FLAG_USER;
92110

93-
mod = rzalloc(flags, sizeof(*mod));
111+
mod = sof_heap_alloc(mod_heap, flags, sizeof(*mod), 0);
94112
if (!mod) {
95-
comp_err(dev, "module_adapter_new(), failed to allocate memory for module");
113+
comp_cl_err(drv, "module_adapter_new(), failed to allocate memory for module");
114+
goto emod;
115+
}
116+
117+
memset(mod, 0, sizeof(*mod));
118+
mod->priv.resources.heap = mod_heap;
119+
mod->priv.resources.heap_mem = mod_heap_mem;
120+
121+
/*
122+
* comp_alloc() always allocated dev uncached. Would be difficult to optimize. Only
123+
* if the whole currently active topology is running on the primary core, then it
124+
* can be cached. Effectively it can be only cached in single-core configurations.
125+
*/
126+
dev = sof_heap_alloc(mod_heap, SOF_MEM_FLAG_COHERENT, sizeof(*dev), 0);
127+
if (!dev) {
128+
comp_cl_err(drv, "module_adapter_new(), failed to allocate memory for comp_dev");
96129
goto err;
97130
}
98131

132+
memset(dev, 0, sizeof(*dev));
133+
comp_init(drv, dev, sizeof(*dev));
134+
dev->ipc_config = *config;
135+
99136
dst = &mod->priv.cfg;
100137

101138
module_set_private_data(mod, mod_priv);
@@ -159,13 +196,17 @@ struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
159196

160197
comp_dbg(dev, "module_adapter_new() done");
161198
return dev;
199+
162200
err:
163201
#if CONFIG_IPC_MAJOR_4
164202
if (mod)
165203
rfree(mod->priv.cfg.input_pins);
166204
#endif
167-
rfree(mod);
168-
rfree(dev);
205+
sof_heap_free(mod_heap, dev);
206+
sof_heap_free(mod_heap, mod);
207+
emod:
208+
rfree(mod_heap_mem);
209+
169210
return NULL;
170211
}
171212
EXPORT_SYMBOL(module_adapter_new);
@@ -1285,8 +1326,13 @@ void module_adapter_free(struct comp_dev *dev)
12851326
#endif
12861327

12871328
rfree(mod->stream_params);
1288-
rfree(mod);
1289-
rfree(dev);
1329+
1330+
struct k_heap *mod_heap = mod->priv.resources.heap;
1331+
void *mem = mod->priv.resources.heap_mem;
1332+
1333+
sof_heap_free(mod_heap, mod);
1334+
sof_heap_free(mod_heap, dev);
1335+
rfree(mem);
12901336
}
12911337
EXPORT_SYMBOL(module_adapter_free);
12921338

src/include/sof/audio/module_adapter/module/generic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ struct module_resources {
132132
struct list_item cont_chunk_list; /**< Memory container chunks */
133133
size_t heap_usage;
134134
size_t heap_high_water_mark;
135+
struct k_heap *heap;
136+
void *heap_mem;
135137
#if CONFIG_MODULE_MEMORY_API_DEBUG && defined(__ZEPHYR__)
136138
k_tid_t rsrc_mngr;
137139
#endif

0 commit comments

Comments
 (0)