Skip to content

Commit 070c235

Browse files
committed
audio: module: simplify resource allocation context
Merge struct mod_alloc_ctx and struct dp_heap_user. We still need a separately allocated allocation context to make sure it persists when the module adapter object is freed but some of the connected buffers still exist, so struct mod_alloc_ctx now becomes that separate context. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 9a49a1a commit 070c235

9 files changed

Lines changed: 55 additions & 57 deletions

File tree

src/audio/buffers/comp_buffer.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,9 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer)
162162
else
163163
sof_heap_free(alloc ? alloc->heap : NULL, buffer);
164164

165-
if (alloc && alloc->client && !--alloc->client->client_count) {
166-
rfree(alloc->client);
167-
alloc->client = NULL;
168-
/* NULL is allowed */
165+
if (alloc && !--alloc->client_count) {
169166
vregion_put(alloc->vreg);
167+
rfree(alloc);
170168
}
171169
}
172170

src/audio/buffers/ring_buffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl
294294
uint32_t id)
295295
{
296296
struct ring_buffer *ring_buffer;
297-
struct mod_alloc_ctx *alloc = &dev->mod->priv.resources.alloc;
297+
struct mod_alloc_ctx *alloc = dev->mod->priv.resources.alloc;
298298
struct k_heap *heap = alloc->heap;
299299
struct vregion *vreg = alloc->vreg;
300300
int memory_flags = (is_shared ? SOF_MEM_FLAG_COHERENT : 0) |

src/audio/module_adapter/module/generic.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void mod_resource_init(struct processing_module *mod)
8686

8787
/* Init memory list */
8888
list_init(&md->resources.objpool.list);
89-
md->resources.objpool.heap = md->resources.alloc.heap;
89+
md->resources.objpool.heap = md->resources.alloc->heap;
9090
md->resources.heap_usage = 0;
9191
md->resources.heap_high_water_mark = 0;
9292
}
@@ -159,10 +159,10 @@ void mod_heap_info(struct processing_module *mod, size_t *size, uintptr_t *start
159159
struct module_resources *res = &mod->priv.resources;
160160

161161
if (size)
162-
*size = res->alloc.heap->heap.init_bytes;
162+
*size = res->alloc->heap->heap.init_bytes;
163163

164164
if (start)
165-
*start = (uintptr_t)res->alloc.client;
165+
*start = (uintptr_t)res->alloc->heap;
166166
}
167167
#endif
168168

@@ -195,7 +195,7 @@ void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignm
195195
}
196196

197197
/* Allocate buffer memory for module */
198-
void *ptr = sof_heap_alloc(res->alloc.heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_LARGE_BUFFER,
198+
void *ptr = sof_heap_alloc(res->alloc->heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_LARGE_BUFFER,
199199
size, alignment);
200200

201201
if (!ptr) {
@@ -246,7 +246,7 @@ void *z_impl_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t
246246
}
247247

248248
/* Allocate memory for module */
249-
void *ptr = sof_heap_alloc(res->alloc.heap, flags, size, alignment);
249+
void *ptr = sof_heap_alloc(res->alloc->heap, flags, size, alignment);
250250

251251
if (!ptr) {
252252
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
@@ -323,7 +323,7 @@ const void *z_impl_mod_fast_get(struct processing_module *mod, const void * cons
323323
if (!container)
324324
return NULL;
325325

326-
ptr = fast_get(res->alloc.heap, dram_ptr, size);
326+
ptr = fast_get(res->alloc->heap, dram_ptr, size);
327327
if (!ptr) {
328328
container_put(mod, container);
329329
return NULL;
@@ -347,7 +347,7 @@ static int free_contents(struct processing_module *mod, struct module_resource *
347347

348348
switch (container->type) {
349349
case MOD_RES_HEAP:
350-
sof_heap_free(res->alloc.heap, container->ptr);
350+
sof_heap_free(res->alloc->heap, container->ptr);
351351
res->heap_usage -= container->size;
352352
return 0;
353353
#if CONFIG_COMP_BLOB
@@ -362,7 +362,7 @@ static int free_contents(struct processing_module *mod, struct module_resource *
362362
#else
363363
mdom = NULL;
364364
#endif
365-
fast_put(res->alloc.heap, mdom, container->sram_ptr);
365+
fast_put(res->alloc->heap, mdom, container->sram_ptr);
366366
return 0;
367367
#endif
368368
default:
@@ -429,7 +429,7 @@ const void *z_vrfy_mod_fast_get(struct processing_module *mod, const void * cons
429429
struct module_resources *res = &mod->priv.resources;
430430

431431
K_OOPS(K_SYSCALL_MEMORY_WRITE(mod, sizeof(*mod)));
432-
K_OOPS(K_SYSCALL_MEMORY_WRITE(res->alloc.heap, sizeof(*res->alloc.heap)));
432+
K_OOPS(K_SYSCALL_MEMORY_WRITE(res->alloc->heap, sizeof(*res->alloc->heap)));
433433
K_OOPS(K_SYSCALL_MEMORY_READ(dram_ptr, size));
434434

435435
return z_impl_mod_fast_get(mod, dram_ptr, size);
@@ -443,7 +443,7 @@ void *z_vrfy_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t
443443
struct module_resources *res = &mod->priv.resources;
444444

445445
K_OOPS(K_SYSCALL_MEMORY_WRITE(mod, sizeof(*mod)));
446-
K_OOPS(K_SYSCALL_MEMORY_WRITE(res->alloc.heap, sizeof(*res->alloc.heap)));
446+
K_OOPS(K_SYSCALL_MEMORY_WRITE(res->alloc->heap, sizeof(*res->alloc->heap)));
447447

448448
return z_impl_mod_alloc_ext(mod, flags, size, alignment);
449449
}
@@ -454,7 +454,7 @@ int z_vrfy_mod_free(struct processing_module *mod, const void *ptr)
454454
struct module_resources *res = &mod->priv.resources;
455455

456456
K_OOPS(K_SYSCALL_MEMORY_WRITE(mod, sizeof(*mod)));
457-
K_OOPS(K_SYSCALL_MEMORY_WRITE(res->alloc.heap, sizeof(*res->alloc.heap)));
457+
K_OOPS(K_SYSCALL_MEMORY_WRITE(res->alloc->heap, sizeof(*res->alloc->heap)));
458458

459459
return z_impl_mod_free(mod, ptr);
460460
}

src/audio/module_adapter/module_adapter.c

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ struct comp_dev *module_adapter_new(const struct comp_driver *drv,
5757
#define PAGE_SZ HOST_PAGE_SIZE
5858
#endif
5959

60-
static struct dp_heap_user *module_adapter_dp_heap_new(const struct comp_ipc_config *config,
61-
size_t *heap_size)
60+
static struct k_heap *module_adapter_dp_heap_new(const struct comp_ipc_config *config,
61+
size_t *heap_size)
6262
{
6363
/* src-lite with 8 channels has been seen allocating 14k in one go */
6464
/* FIXME: the size will be derived from configuration */
@@ -71,9 +71,8 @@ static struct dp_heap_user *module_adapter_dp_heap_new(const struct comp_ipc_con
7171
if (!mod_heap_mem)
7272
return NULL;
7373

74-
struct dp_heap_user *mod_heap_user = (struct dp_heap_user *)mod_heap_mem;
75-
struct k_heap *mod_heap = &mod_heap_user->heap;
76-
const size_t heap_prefix_size = ALIGN_UP(sizeof(*mod_heap_user), 4);
74+
struct k_heap *mod_heap = (struct k_heap *)mod_heap_mem;
75+
const size_t heap_prefix_size = ALIGN_UP(sizeof(*mod_heap), 4);
7776
void *mod_heap_buf = mod_heap_mem + heap_prefix_size;
7877

7978
*heap_size = buf_size - heap_prefix_size;
@@ -83,7 +82,7 @@ static struct dp_heap_user *module_adapter_dp_heap_new(const struct comp_ipc_con
8382
mod_heap->heap.init_bytes = *heap_size;
8483
#endif
8584

86-
return mod_heap_user;
85+
return mod_heap;
8786
}
8887

8988
static struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv,
@@ -99,20 +98,17 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
9998
*/
10099
uint32_t flags = config->proc_domain == COMP_PROCESSING_DOMAIN_DP ?
101100
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT : SOF_MEM_FLAG_USER;
102-
struct dp_heap_user *mod_heap_user;
103101
size_t heap_size;
104102

105103
if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP && IS_ENABLED(CONFIG_USERSPACE) &&
106104
!IS_ENABLED(CONFIG_SOF_USERSPACE_USE_DRIVER_HEAP)) {
107-
mod_heap_user = module_adapter_dp_heap_new(config, &heap_size);
108-
if (!mod_heap_user) {
105+
mod_heap = module_adapter_dp_heap_new(config, &heap_size);
106+
if (!mod_heap) {
109107
comp_cl_err(drv, "Failed to allocate DP module heap");
110108
return NULL;
111109
}
112-
mod_heap = &mod_heap_user->heap;
113110
} else {
114111
mod_heap = drv->user_heap;
115-
mod_heap_user = NULL;
116112
heap_size = 0;
117113
}
118114

@@ -123,9 +119,16 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
123119
goto emod;
124120
}
125121

122+
struct mod_alloc_ctx *alloc = rmalloc(flags, sizeof(*alloc));
123+
124+
if (!alloc)
125+
goto ealloc;
126+
126127
memset(mod, 0, sizeof(*mod));
127-
mod->priv.resources.alloc.heap = mod_heap;
128-
mod->priv.resources.alloc.client = mod_heap_user;
128+
alloc->heap = mod_heap;
129+
alloc->vreg = NULL;
130+
alloc->client_count = 0;
131+
mod->priv.resources.alloc = alloc;
129132
mod_resource_init(mod);
130133

131134
/*
@@ -138,7 +141,7 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
138141

139142
if (!dev) {
140143
comp_cl_err(drv, "failed to allocate memory for comp_dev");
141-
goto err;
144+
goto edev;
142145
}
143146

144147
memset(dev, 0, sizeof(*dev));
@@ -147,22 +150,25 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
147150
mod->dev = dev;
148151
dev->mod = mod;
149152

150-
if (mod_heap_user)
151-
mod_heap_user->client_count++;
153+
if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP)
154+
alloc->client_count++;
152155

153156
return mod;
154157

155-
err:
158+
edev:
159+
rfree(alloc);
160+
ealloc:
156161
sof_heap_free(mod_heap, mod);
157162
emod:
158-
rfree(mod_heap_user);
163+
rfree(mod_heap);
159164

160165
return NULL;
161166
}
162167

163168
static void module_adapter_mem_free(struct processing_module *mod)
164169
{
165-
struct k_heap *mod_heap = mod->priv.resources.alloc.heap;
170+
struct mod_alloc_ctx *alloc = mod->priv.resources.alloc;
171+
struct k_heap *mod_heap = alloc->heap;
166172
unsigned int domain = mod->dev->ipc_config.proc_domain;
167173

168174
/*
@@ -175,11 +181,12 @@ static void module_adapter_mem_free(struct processing_module *mod)
175181
sof_heap_free(mod_heap, mod->dev);
176182
sof_heap_free(mod_heap, mod);
177183
if (domain == COMP_PROCESSING_DOMAIN_DP) {
178-
struct dp_heap_user *mod_heap_user = container_of(mod_heap, struct dp_heap_user,
179-
heap);
180-
181-
if (mod_heap && !--mod_heap_user->client_count)
182-
rfree(mod_heap_user);
184+
if (!IS_ENABLED(CONFIG_SOF_USERSPACE_USE_DRIVER_HEAP))
185+
rfree(mod_heap);
186+
if (!--alloc->client_count)
187+
rfree(alloc);
188+
} else {
189+
rfree(alloc);
183190
}
184191
}
185192

@@ -612,7 +619,7 @@ int module_adapter_prepare(struct comp_dev *dev)
612619
if (list_is_empty(&mod->raw_data_buffers_list)) {
613620
for (i = 0; i < mod->num_of_sinks; i++) {
614621
/* allocate not shared buffer */
615-
struct comp_buffer *buffer = buffer_alloc(&md->resources.alloc,
622+
struct comp_buffer *buffer = buffer_alloc(md->resources.alloc,
616623
buff_size, memory_flags,
617624
PLATFORM_DCACHE_ALIGN,
618625
BUFFER_USAGE_NOT_SHARED);
@@ -624,9 +631,9 @@ int module_adapter_prepare(struct comp_dev *dev)
624631
goto free;
625632
}
626633

627-
if (md->resources.alloc.heap &&
628-
md->resources.alloc.heap != dev->drv->user_heap)
629-
md->resources.alloc.client->client_count++;
634+
if (md->resources.alloc->heap &&
635+
md->resources.alloc->heap != dev->drv->user_heap)
636+
md->resources.alloc->client_count++;
630637

631638
irq_local_disable(flags);
632639
list_item_prepend(&buffer->buffers_list, &mod->raw_data_buffers_list);

src/audio/module_adapter/module_adapter_ipc4.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ int module_adapter_init_data(struct comp_dev *dev,
147147
if (cfgsz == (sizeof(*cfg) + pinsz)) {
148148
dst->nb_input_pins = n_in;
149149
dst->nb_output_pins = n_out;
150-
dst->input_pins = sof_heap_alloc(dev->mod->priv.resources.alloc.heap,
150+
dst->input_pins = sof_heap_alloc(dev->mod->priv.resources.alloc->heap,
151151
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
152152
pinsz, 0);
153153
if (!dst->input_pins)

src/include/sof/audio/component.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,10 @@ struct comp_ops {
580580

581581
struct k_heap;
582582
struct vregion;
583-
struct dp_heap_user;
584583
struct mod_alloc_ctx {
585584
struct k_heap *heap;
586585
struct vregion *vreg;
587-
struct dp_heap_user *client;
586+
unsigned int client_count;
588587
};
589588

590589
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ struct module_resources {
132132
struct objpool_head objpool;
133133
size_t heap_usage;
134134
size_t heap_high_water_mark;
135-
struct mod_alloc_ctx alloc;
135+
struct mod_alloc_ctx *alloc;
136136
#if CONFIG_MODULE_MEMORY_API_DEBUG && defined(__ZEPHYR__)
137137
k_tid_t rsrc_mngr;
138138
#endif

src/include/sof/schedule/dp_schedule.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,6 @@ union scheduler_dp_thread_ipc_param {
119119
} pipeline_state;
120120
};
121121

122-
struct dp_heap_user {
123-
struct k_heap heap;
124-
/* So far relying on linear processing of serialized IPCs, but might need protection */
125-
unsigned int client_count; /* devices and buffers */
126-
};
127-
128122
#if CONFIG_ZEPHYR_DP_SCHEDULER
129123
int scheduler_dp_thread_ipc(struct processing_module *pmod, unsigned int cmd,
130124
const union scheduler_dp_thread_ipc_param *param);

src/ipc/ipc4/helper.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ __cold int ipc_comp_connect(struct ipc *ipc, ipc_pipe_comp_connect *_connect)
659659
else
660660
dp = NULL;
661661

662-
alloc = dp && dp->mod ? &dp->mod->priv.resources.alloc : NULL;
662+
alloc = dp && dp->mod ? dp->mod->priv.resources.alloc : NULL;
663663
#else
664664
alloc = NULL;
665665
#endif /* CONFIG_ZEPHYR_DP_SCHEDULER */
@@ -738,8 +738,8 @@ __cold int ipc_comp_connect(struct ipc *ipc, ipc_pipe_comp_connect *_connect)
738738
}
739739

740740
#if CONFIG_ZEPHYR_DP_SCHEDULER
741-
if (alloc && alloc->client)
742-
alloc->client->client_count++;
741+
if (alloc)
742+
alloc->client_count++;
743743
#endif
744744

745745
/*

0 commit comments

Comments
 (0)