Skip to content

Commit 9a49a1a

Browse files
committed
audio: buffer: prepare to allocate on vregion
Buffers, that are accessible to userspace DP modules, have to migrate to vregion together with the latter. Prepare them for the migration. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent d89a4a0 commit 9a49a1a

11 files changed

Lines changed: 110 additions & 70 deletions

File tree

src/audio/buffers/comp_buffer.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <rtos/interrupt.h>
1717
#include <rtos/alloc.h>
1818
#include <rtos/cache.h>
19+
#include <sof/lib/vregion.h>
1920
#include <sof/list.h>
2021
#include <sof/schedule/dp_schedule.h>
2122
#include <rtos/spinlock.h>
@@ -153,15 +154,19 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer)
153154
buffer->probe_cb_free(buffer->probe_cb_arg);
154155
#endif
155156

156-
struct k_heap *heap = buffer->audio_buffer.heap;
157+
struct mod_alloc_ctx *alloc = buffer->audio_buffer.alloc;
157158

158159
rfree(buffer->stream.addr);
159-
sof_heap_free(heap, buffer);
160-
if (heap) {
161-
struct dp_heap_user *mod_heap_user = container_of(heap, struct dp_heap_user, heap);
160+
if (alloc && alloc->vreg)
161+
vregion_free(alloc->vreg, buffer);
162+
else
163+
sof_heap_free(alloc ? alloc->heap : NULL, buffer);
162164

163-
if (!--mod_heap_user->client_count)
164-
rfree(mod_heap_user);
165+
if (alloc && alloc->client && !--alloc->client->client_count) {
166+
rfree(alloc->client);
167+
alloc->client = NULL;
168+
/* NULL is allowed */
169+
vregion_put(alloc->vreg);
165170
}
166171
}
167172

@@ -192,7 +197,7 @@ static const struct audio_buffer_ops audio_buffer_ops = {
192197
.set_alignment_constants = comp_buffer_set_alignment_constants,
193198
};
194199

195-
static struct comp_buffer *buffer_alloc_struct(struct k_heap *heap,
200+
static struct comp_buffer *buffer_alloc_struct(struct mod_alloc_ctx *alloc,
196201
void *stream_addr, size_t size,
197202
uint32_t flags, bool is_shared)
198203
{
@@ -204,7 +209,12 @@ static struct comp_buffer *buffer_alloc_struct(struct k_heap *heap,
204209
if (is_shared)
205210
flags |= SOF_MEM_FLAG_COHERENT;
206211

207-
buffer = sof_heap_alloc(heap, flags, sizeof(*buffer), 0);
212+
if (!alloc || !alloc->vreg)
213+
buffer = sof_heap_alloc(alloc ? alloc->heap : NULL, flags, sizeof(*buffer), 0);
214+
else if (is_shared)
215+
buffer = vregion_alloc_coherent(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer));
216+
else
217+
buffer = vregion_alloc(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer));
208218
if (!buffer) {
209219
tr_err(&buffer_tr, "could not alloc structure");
210220
return NULL;
@@ -226,16 +236,16 @@ static struct comp_buffer *buffer_alloc_struct(struct k_heap *heap,
226236

227237
audio_stream_set_underrun(&buffer->stream, !!(flags & SOF_BUF_UNDERRUN_PERMITTED));
228238
audio_stream_set_overrun(&buffer->stream, !!(flags & SOF_BUF_OVERRUN_PERMITTED));
229-
buffer->audio_buffer.heap = heap;
239+
buffer->audio_buffer.alloc = alloc;
230240

231241
comp_buffer_reset_source_list(buffer);
232242
comp_buffer_reset_sink_list(buffer);
233243

234244
return buffer;
235245
}
236246

237-
struct comp_buffer *buffer_alloc(struct k_heap *heap, size_t size, uint32_t flags, uint32_t align,
238-
bool is_shared)
247+
struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint32_t flags,
248+
uint32_t align, bool is_shared)
239249
{
240250
struct comp_buffer *buffer;
241251
void *stream_addr;
@@ -255,7 +265,7 @@ struct comp_buffer *buffer_alloc(struct k_heap *heap, size_t size, uint32_t flag
255265
return NULL;
256266
}
257267

258-
buffer = buffer_alloc_struct(heap, stream_addr, size, flags, is_shared);
268+
buffer = buffer_alloc_struct(alloc, stream_addr, size, flags, is_shared);
259269
if (!buffer) {
260270
tr_err(&buffer_tr, "could not alloc buffer structure");
261271
rfree(stream_addr);
@@ -264,7 +274,7 @@ struct comp_buffer *buffer_alloc(struct k_heap *heap, size_t size, uint32_t flag
264274
return buffer;
265275
}
266276

267-
struct comp_buffer *buffer_alloc_range(struct k_heap *heap, size_t preferred_size,
277+
struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t preferred_size,
268278
size_t minimum_size,
269279
uint32_t flags, uint32_t align, bool is_shared)
270280
{
@@ -299,7 +309,7 @@ struct comp_buffer *buffer_alloc_range(struct k_heap *heap, size_t preferred_siz
299309
return NULL;
300310
}
301311

302-
buffer = buffer_alloc_struct(heap, stream_addr, size, flags, is_shared);
312+
buffer = buffer_alloc_struct(alloc, stream_addr, size, flags, is_shared);
303313
if (!buffer) {
304314
tr_err(&buffer_tr, "could not alloc buffer structure");
305315
rfree(stream_addr);

src/audio/buffers/ring_buffer.c

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <sof/common.h>
77
#include <sof/trace/trace.h>
88
#include <sof/lib/uuid.h>
9+
#include <sof/lib/vregion.h>
910

1011
#include <sof/audio/module_adapter/module/generic.h>
1112
#include <sof/audio/ring_buffer.h>
@@ -96,9 +97,15 @@ static void ring_buffer_free(struct sof_audio_buffer *audio_buffer)
9697

9798
struct ring_buffer *ring_buffer = container_of(audio_buffer,
9899
struct ring_buffer, audio_buffer);
99-
100-
sof_heap_free(audio_buffer->heap, (__sparse_force void *)ring_buffer->_data_buffer);
101-
sof_heap_free(audio_buffer->heap, ring_buffer);
100+
struct mod_alloc_ctx *alloc = audio_buffer->alloc;
101+
102+
if (alloc->vreg) {
103+
vregion_free(alloc->vreg, (__sparse_force void *)ring_buffer->_data_buffer);
104+
vregion_free(alloc->vreg, ring_buffer);
105+
} else {
106+
sof_heap_free(alloc->heap, (__sparse_force void *)ring_buffer->_data_buffer);
107+
sof_heap_free(alloc->heap, ring_buffer);
108+
}
102109
}
103110

104111
static void ring_buffer_reset(struct sof_audio_buffer *audio_buffer)
@@ -287,12 +294,19 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl
287294
uint32_t id)
288295
{
289296
struct ring_buffer *ring_buffer;
290-
struct k_heap *heap = dev->mod->priv.resources.heap;
297+
struct mod_alloc_ctx *alloc = &dev->mod->priv.resources.alloc;
298+
struct k_heap *heap = alloc->heap;
299+
struct vregion *vreg = alloc->vreg;
291300
int memory_flags = (is_shared ? SOF_MEM_FLAG_COHERENT : 0) |
292301
user_get_buffer_memory_region(dev->drv);
293302

294303
/* allocate ring_buffer structure */
295-
ring_buffer = sof_heap_alloc(heap, memory_flags, sizeof(*ring_buffer), 0);
304+
if (!vreg)
305+
ring_buffer = sof_heap_alloc(heap, memory_flags, sizeof(*ring_buffer), 0);
306+
else if (is_shared)
307+
ring_buffer = vregion_alloc_coherent(vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*ring_buffer));
308+
else
309+
ring_buffer = vregion_alloc(vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*ring_buffer));
296310
if (!ring_buffer)
297311
return NULL;
298312

@@ -307,7 +321,8 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl
307321
audio_buffer_init(&ring_buffer->audio_buffer, BUFFER_TYPE_RING_BUFFER,
308322
is_shared, &ring_buffer_source_ops, &ring_buffer_sink_ops,
309323
&audio_buffer_ops, NULL);
310-
ring_buffer->audio_buffer.heap = heap;
324+
ring_buffer->audio_buffer.alloc = alloc;
325+
ring_buffer->audio_buffer.alloc->heap = heap;
311326

312327
/* set obs/ibs in sink/source interfaces */
313328
sink_set_min_free_space(audio_buffer_get_sink(&ring_buffer->audio_buffer),
@@ -364,12 +379,21 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl
364379
/* allocate data buffer - always in cached memory alias */
365380
ring_buffer->data_buffer_size = ALIGN_UP(ring_buffer->data_buffer_size,
366381
PLATFORM_DCACHE_ALIGN);
367-
ring_buffer->_data_buffer = (__sparse_force __sparse_cache void *)sof_heap_alloc(heap,
368-
user_get_buffer_memory_region(dev->drv),
369-
ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
370-
if (!ring_buffer->_data_buffer)
382+
383+
void *data_buf;
384+
385+
if (vreg)
386+
data_buf = vregion_alloc_align(vreg, VREGION_MEM_TYPE_INTERIM, ring_buffer->data_buffer_size,
387+
PLATFORM_DCACHE_ALIGN);
388+
else
389+
data_buf = sof_heap_alloc(heap, user_get_buffer_memory_region(dev->drv),
390+
ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
391+
392+
if (!data_buf)
371393
goto err;
372394

395+
ring_buffer->_data_buffer = (__sparse_force __sparse_cache void *)data_buf;
396+
373397
tr_info(&ring_buffer_tr, "Ring buffer created, id: %u shared: %u min_available: %u min_free_space %u, size %u",
374398
id, ring_buffer_is_shared(ring_buffer), min_available, min_free_space,
375399
ring_buffer->data_buffer_size);
@@ -378,6 +402,9 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl
378402
return ring_buffer;
379403
err:
380404
tr_err(&ring_buffer_tr, "Ring buffer creation failure");
381-
sof_heap_free(heap, ring_buffer);
405+
if (vreg)
406+
vregion_free(vreg, ring_buffer);
407+
else
408+
sof_heap_free(heap, ring_buffer);
382409
return NULL;
383410
}

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.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->heap->heap.init_bytes;
162+
*size = res->alloc.heap->heap.init_bytes;
163163

164164
if (start)
165-
*start = (uintptr_t)container_of(res->heap, struct dp_heap_user, heap);
165+
*start = (uintptr_t)res->alloc.client;
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->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->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->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->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->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->heap, sizeof(*res->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->heap, sizeof(*res->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->heap, sizeof(*res->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: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
124124
}
125125

126126
memset(mod, 0, sizeof(*mod));
127-
mod->priv.resources.heap = mod_heap;
127+
mod->priv.resources.alloc.heap = mod_heap;
128+
mod->priv.resources.alloc.client = mod_heap_user;
128129
mod_resource_init(mod);
129130

130131
/*
@@ -161,7 +162,7 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv
161162

162163
static void module_adapter_mem_free(struct processing_module *mod)
163164
{
164-
struct k_heap *mod_heap = mod->priv.resources.heap;
165+
struct k_heap *mod_heap = mod->priv.resources.alloc.heap;
165166
unsigned int domain = mod->dev->ipc_config.proc_domain;
166167

167168
/*
@@ -611,8 +612,8 @@ int module_adapter_prepare(struct comp_dev *dev)
611612
if (list_is_empty(&mod->raw_data_buffers_list)) {
612613
for (i = 0; i < mod->num_of_sinks; i++) {
613614
/* allocate not shared buffer */
614-
struct comp_buffer *buffer = buffer_alloc(md->resources.heap, buff_size,
615-
memory_flags,
615+
struct comp_buffer *buffer = buffer_alloc(&md->resources.alloc,
616+
buff_size, memory_flags,
616617
PLATFORM_DCACHE_ALIGN,
617618
BUFFER_USAGE_NOT_SHARED);
618619
uint32_t flags;
@@ -623,13 +624,9 @@ int module_adapter_prepare(struct comp_dev *dev)
623624
goto free;
624625
}
625626

626-
if (md->resources.heap && md->resources.heap != dev->drv->user_heap) {
627-
struct dp_heap_user *dp_user = container_of(md->resources.heap,
628-
struct dp_heap_user,
629-
heap);
630-
631-
dp_user->client_count++;
632-
}
627+
if (md->resources.alloc.heap &&
628+
md->resources.alloc.heap != dev->drv->user_heap)
629+
md->resources.alloc.client->client_count++;
633630

634631
irq_local_disable(flags);
635632
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.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/audio_buffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ struct sof_audio_buffer {
111111
*/
112112
bool walking; /**< indicates if the buffer is being walked */
113113

114-
struct k_heap *heap;
114+
struct mod_alloc_ctx *alloc;
115115
};
116116

117117
#if CONFIG_PIPELINE_2_0

src/include/sof/audio/buffer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,15 @@ struct buffer_cb_transact {
217217
buffer->cb_type = type; \
218218
} while (0)
219219

220-
struct k_heap;
220+
struct mod_alloc_ctx;
221221

222222
/* pipeline buffer creation and destruction */
223-
struct comp_buffer *buffer_alloc(struct k_heap *heap, size_t size, uint32_t flags, uint32_t align,
224-
bool is_shared);
225-
struct comp_buffer *buffer_alloc_range(struct k_heap *heap, size_t preferred_size,
223+
struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint32_t flags,
224+
uint32_t align, bool is_shared);
225+
struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t preferred_size,
226226
size_t minimum_size,
227227
uint32_t flags, uint32_t align, bool is_shared);
228-
struct comp_buffer *buffer_new(struct k_heap *heap, const struct sof_ipc_buffer *desc,
228+
struct comp_buffer *buffer_new(struct mod_alloc_ctx *alloc, const struct sof_ipc_buffer *desc,
229229
bool is_shared);
230230

231231
int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignment);

src/include/sof/audio/component.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,15 @@ struct comp_ops {
578578
uint64_t (*get_total_data_processed)(struct comp_dev *dev, uint32_t stream_no, bool input);
579579
};
580580

581+
struct k_heap;
582+
struct vregion;
583+
struct dp_heap_user;
584+
struct mod_alloc_ctx {
585+
struct k_heap *heap;
586+
struct vregion *vreg;
587+
struct dp_heap_user *client;
588+
};
589+
581590
/**
582591
* Audio component base driver "class"
583592
* - used by all other component types.

0 commit comments

Comments
 (0)