Skip to content

Commit 1f4b8f0

Browse files
committed
audio: dai-zephyr: make memory allocations user-space compatible
Convert all memory allocations to use the sof_heap_alloc() interface and pass the dai_data specific heap object. This makes dai-zephyr code compatible with use from user-space, but does not affect kernel space use. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent 2b65cde commit 1f4b8f0

3 files changed

Lines changed: 40 additions & 18 deletions

File tree

src/audio/dai-zephyr.c

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,17 @@ __cold int dai_common_new(struct dai_data *dd, struct comp_dev *dev,
537537
dma_sg_init(&dd->config.elem_array);
538538
dd->xrun = 0;
539539

540+
#ifdef CONFIG_SOF_USERSPACE_LL
541+
/*
542+
* copier_dai_create() uses mod_zalloc() to allocate
543+
* the 'dd' dai data object and does not set dd->alloc_ctx.
544+
* If LL is run in user-space, assign the 'heap' here.
545+
*/
546+
dd->alloc_ctx.heap = sof_sys_user_heap_get();
547+
#else
548+
dd->alloc_ctx.heap = NULL;
549+
#endif
550+
540551
/* I/O performance init, keep it last so the function does not reach this in case
541552
* of return on error, so that we do not waste a slot
542553
*/
@@ -589,6 +600,7 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
589600
struct comp_dev *dev;
590601
const struct ipc_config_dai *dai_cfg = spec;
591602
struct dai_data *dd;
603+
struct k_heap *heap = NULL;
592604
int ret;
593605

594606
assert_can_be_cold();
@@ -601,10 +613,12 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
601613

602614
dev->ipc_config = *config;
603615

604-
dd = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd));
616+
dd = sof_heap_alloc(heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd), 0);
605617
if (!dd)
606618
goto e_data;
607619

620+
memset(dd, 0, sizeof(*dd));
621+
608622
comp_set_drvdata(dev, dd);
609623

610624
ret = dai_common_new(dd, dev, dai_cfg);
@@ -618,7 +632,7 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
618632
return dev;
619633

620634
error:
621-
rfree(dd);
635+
sof_heap_free(heap, dd);
622636
e_data:
623637
comp_free_device(dev);
624638
return NULL;
@@ -646,7 +660,7 @@ __cold void dai_common_free(struct dai_data *dd)
646660

647661
dai_put(dd->dai);
648662

649-
rfree(dd->dai_spec_config);
663+
sof_heap_free(dd->alloc_ctx.heap, dd->dai_spec_config);
650664
}
651665

652666
__cold static void dai_free(struct comp_dev *dev)
@@ -660,7 +674,8 @@ __cold static void dai_free(struct comp_dev *dev)
660674

661675
dai_common_free(dd);
662676

663-
rfree(dd);
677+
/* heap is NULL to match what is passed in dai_new() */
678+
sof_heap_free(NULL, dd);
664679
comp_free_device(dev);
665680
}
666681

@@ -855,7 +870,7 @@ static int dai_set_sg_config(struct dai_data *dd, struct comp_dev *dev, uint32_t
855870
} while (--max_block_count > 0);
856871
}
857872

858-
err = dma_sg_alloc(NULL, &config->elem_array, SOF_MEM_FLAG_USER,
873+
err = dma_sg_alloc(dd->alloc_ctx.heap, &config->elem_array, SOF_MEM_FLAG_USER,
859874
config->direction,
860875
period_count,
861876
period_bytes,
@@ -881,8 +896,9 @@ static int dai_set_dma_config(struct dai_data *dd, struct comp_dev *dev)
881896

882897
comp_dbg(dev, "entry");
883898

884-
dma_cfg = rmalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
885-
sizeof(struct dma_config));
899+
dma_cfg = sof_heap_alloc(dd->alloc_ctx.heap,
900+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
901+
sizeof(struct dma_config), 0);
886902
if (!dma_cfg) {
887903
comp_err(dev, "dma_cfg allocation failed");
888904
return -ENOMEM;
@@ -911,10 +927,11 @@ static int dai_set_dma_config(struct dai_data *dd, struct comp_dev *dev)
911927
else
912928
dma_cfg->dma_slot = config->src_dev;
913929

914-
dma_block_cfg = rballoc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
915-
sizeof(struct dma_block_config) * dma_cfg->block_count);
930+
dma_block_cfg = sof_heap_alloc(dd->alloc_ctx.heap,
931+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
932+
sizeof(struct dma_block_config) * dma_cfg->block_count, 0);
916933
if (!dma_block_cfg) {
917-
rfree(dma_cfg);
934+
sof_heap_free(dd->alloc_ctx.heap, dma_cfg);
918935
comp_err(dev, "dma_block_config allocation failed");
919936
return -ENOMEM;
920937
}
@@ -1048,7 +1065,7 @@ static int dai_set_dma_buffer(struct dai_data *dd, struct comp_dev *dev,
10481065
return err;
10491066
}
10501067
} else {
1051-
dd->dma_buffer = buffer_alloc_range(NULL, buffer_size_preferred, buffer_size,
1068+
dd->dma_buffer = buffer_alloc_range(&dd->alloc_ctx, buffer_size_preferred, buffer_size,
10521069
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA,
10531070
addr_align, BUFFER_USAGE_NOT_SHARED);
10541071
if (!dd->dma_buffer) {
@@ -1136,8 +1153,8 @@ int dai_common_params(struct dai_data *dd, struct comp_dev *dev,
11361153
if (err < 0) {
11371154
buffer_free(dd->dma_buffer);
11381155
dd->dma_buffer = NULL;
1139-
dma_sg_free(NULL, &config->elem_array);
1140-
rfree(dd->z_config);
1156+
dma_sg_free(dd->alloc_ctx.heap, &config->elem_array);
1157+
sof_heap_free(dd->alloc_ctx.heap, dd->z_config);
11411158
dd->z_config = NULL;
11421159
}
11431160

@@ -1263,10 +1280,10 @@ void dai_common_reset(struct dai_data *dd, struct comp_dev *dev)
12631280
if (!dd->delayed_dma_stop)
12641281
dai_dma_release(dd, dev);
12651282

1266-
dma_sg_free(NULL, &config->elem_array);
1283+
dma_sg_free(dd->alloc_ctx.heap, &config->elem_array);
12671284
if (dd->z_config) {
1268-
rfree(dd->z_config->head_block);
1269-
rfree(dd->z_config);
1285+
sof_heap_free(dd->alloc_ctx.heap, dd->z_config->head_block);
1286+
sof_heap_free(dd->alloc_ctx.heap, dd->z_config);
12701287
dd->z_config = NULL;
12711288
}
12721289

src/include/sof/lib/dai-zephyr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <sof/ctx_alloc.h>
3030
#include <sof/audio/pcm_converter.h>
3131
#include <sof/audio/ipc-config.h>
32+
#include <sof/audio/component.h>
3233
#include <ipc/dai.h>
3334
#include <errno.h>
3435
#include <stddef.h>
@@ -169,6 +170,8 @@ struct dai_data {
169170
#endif
170171
/* Copier gain params */
171172
struct copier_gain_params *gain_data;
173+
174+
struct mod_alloc_ctx alloc_ctx;
172175
};
173176

174177
/* these 3 are here to satisfy clk.c and ssp.h interconnection, will be removed leter */

src/ipc/ipc4/dai.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,17 @@ __cold int dai_config(struct dai_data *dd, struct comp_dev *dev,
386386
/* allocated dai_config if not yet */
387387
if (!dd->dai_spec_config) {
388388
size = sizeof(*copier_cfg);
389-
dd->dai_spec_config = rzalloc(SOF_MEM_FLAG_USER, size);
389+
dd->dai_spec_config = sof_heap_alloc(dd->alloc_ctx.heap, SOF_MEM_FLAG_USER, size, 0);
390390
if (!dd->dai_spec_config) {
391391
comp_err(dev, "No memory for size %d", size);
392392
return -ENOMEM;
393393
}
394394

395+
memset(dd->dai_spec_config, 0, size);
396+
395397
ret = memcpy_s(dd->dai_spec_config, size, copier_cfg, size);
396398
if (ret < 0) {
397-
rfree(dd->dai_spec_config);
399+
sof_heap_free(dd->alloc_ctx.heap, dd->dai_spec_config);
398400
dd->dai_spec_config = NULL;
399401
return -EINVAL;
400402
}

0 commit comments

Comments
 (0)