Skip to content

Commit 5094cc9

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 527e0d2 commit 5094cc9

3 files changed

Lines changed: 41 additions & 18 deletions

File tree

src/audio/dai-zephyr.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <sof/lib/notifier.h>
2424
#include <sof/lib/uuid.h>
2525
#include <sof/lib/dma.h>
26+
#include <sof/schedule/ll_schedule_domain.h> /* zephyr_ll_user_heap() */
2627
#include <sof/list.h>
2728
#include <rtos/spinlock.h>
2829
#include <rtos/string.h>
@@ -529,6 +530,17 @@ __cold int dai_common_new(struct dai_data *dd, struct comp_dev *dev,
529530
dma_sg_init(&dd->config.elem_array);
530531
dd->xrun = 0;
531532

533+
#ifdef CONFIG_SOF_USERSPACE_LL
534+
/*
535+
* copier_dai_create() uses mod_zalloc() to allocate
536+
* the 'dd' dai data object and does not set dd->alloc_ctx.
537+
* If LL is run in user-space, assign the 'heap' here.
538+
*/
539+
dd->alloc_ctx.heap = zephyr_ll_user_heap();
540+
#else
541+
dd->alloc_ctx.heap = NULL;
542+
#endif
543+
532544
/* I/O performance init, keep it last so the function does not reach this in case
533545
* of return on error, so that we do not waste a slot
534546
*/
@@ -581,6 +593,7 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
581593
struct comp_dev *dev;
582594
const struct ipc_config_dai *dai_cfg = spec;
583595
struct dai_data *dd;
596+
struct k_heap *heap = NULL;
584597
int ret;
585598

586599
assert_can_be_cold();
@@ -593,10 +606,12 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
593606

594607
dev->ipc_config = *config;
595608

596-
dd = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd));
609+
dd = sof_heap_alloc(heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd), 0);
597610
if (!dd)
598611
goto e_data;
599612

613+
memset(dd, 0, sizeof(*dd));
614+
600615
comp_set_drvdata(dev, dd);
601616

602617
ret = dai_common_new(dd, dev, dai_cfg);
@@ -610,7 +625,7 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
610625
return dev;
611626

612627
error:
613-
rfree(dd);
628+
sof_heap_free(heap, dd);
614629
e_data:
615630
comp_free_device(dev);
616631
return NULL;
@@ -638,7 +653,7 @@ __cold void dai_common_free(struct dai_data *dd)
638653

639654
dai_put(dd->dai);
640655

641-
rfree(dd->dai_spec_config);
656+
sof_heap_free(dd->alloc_ctx.heap, dd->dai_spec_config);
642657
}
643658

644659
__cold static void dai_free(struct comp_dev *dev)
@@ -652,7 +667,8 @@ __cold static void dai_free(struct comp_dev *dev)
652667

653668
dai_common_free(dd);
654669

655-
rfree(dd);
670+
/* heap is NULL to match what is passed in dai_new() */
671+
sof_heap_free(NULL, dd);
656672
comp_free_device(dev);
657673
}
658674

@@ -847,7 +863,7 @@ static int dai_set_sg_config(struct dai_data *dd, struct comp_dev *dev, uint32_t
847863
} while (--max_block_count > 0);
848864
}
849865

850-
err = dma_sg_alloc(NULL, &config->elem_array, SOF_MEM_FLAG_USER,
866+
err = dma_sg_alloc(dd->alloc_ctx.heap, &config->elem_array, SOF_MEM_FLAG_USER,
851867
config->direction,
852868
period_count,
853869
period_bytes,
@@ -873,8 +889,9 @@ static int dai_set_dma_config(struct dai_data *dd, struct comp_dev *dev)
873889

874890
comp_dbg(dev, "entry");
875891

876-
dma_cfg = rmalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
877-
sizeof(struct dma_config));
892+
dma_cfg = sof_heap_alloc(dd->alloc_ctx.heap,
893+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
894+
sizeof(struct dma_config), 0);
878895
if (!dma_cfg) {
879896
comp_err(dev, "dma_cfg allocation failed");
880897
return -ENOMEM;
@@ -903,10 +920,11 @@ static int dai_set_dma_config(struct dai_data *dd, struct comp_dev *dev)
903920
else
904921
dma_cfg->dma_slot = config->src_dev;
905922

906-
dma_block_cfg = rballoc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
907-
sizeof(struct dma_block_config) * dma_cfg->block_count);
923+
dma_block_cfg = sof_heap_alloc(dd->alloc_ctx.heap,
924+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
925+
sizeof(struct dma_block_config) * dma_cfg->block_count, 0);
908926
if (!dma_block_cfg) {
909-
rfree(dma_cfg);
927+
sof_heap_free(dd->alloc_ctx.heap, dma_cfg);
910928
comp_err(dev, "dma_block_config allocation failed");
911929
return -ENOMEM;
912930
}
@@ -1040,7 +1058,7 @@ static int dai_set_dma_buffer(struct dai_data *dd, struct comp_dev *dev,
10401058
return err;
10411059
}
10421060
} else {
1043-
dd->dma_buffer = buffer_alloc_range(NULL, buffer_size_preferred, buffer_size,
1061+
dd->dma_buffer = buffer_alloc_range(&dd->alloc_ctx, buffer_size_preferred, buffer_size,
10441062
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA,
10451063
addr_align, BUFFER_USAGE_NOT_SHARED);
10461064
if (!dd->dma_buffer) {
@@ -1128,8 +1146,8 @@ int dai_common_params(struct dai_data *dd, struct comp_dev *dev,
11281146
if (err < 0) {
11291147
buffer_free(dd->dma_buffer);
11301148
dd->dma_buffer = NULL;
1131-
dma_sg_free(NULL, &config->elem_array);
1132-
rfree(dd->z_config);
1149+
dma_sg_free(dd->alloc_ctx.heap, &config->elem_array);
1150+
sof_heap_free(dd->alloc_ctx.heap, dd->z_config);
11331151
dd->z_config = NULL;
11341152
}
11351153

@@ -1255,10 +1273,10 @@ void dai_common_reset(struct dai_data *dd, struct comp_dev *dev)
12551273
if (!dd->delayed_dma_stop)
12561274
dai_dma_release(dd, dev);
12571275

1258-
dma_sg_free(NULL, &config->elem_array);
1276+
dma_sg_free(dd->alloc_ctx.heap, &config->elem_array);
12591277
if (dd->z_config) {
1260-
rfree(dd->z_config->head_block);
1261-
rfree(dd->z_config);
1278+
sof_heap_free(dd->alloc_ctx.heap, dd->z_config->head_block);
1279+
sof_heap_free(dd->alloc_ctx.heap, dd->z_config);
12621280
dd->z_config = NULL;
12631281
}
12641282

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/audio/mod_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
@@ -387,15 +387,17 @@ __cold int dai_config(struct dai_data *dd, struct comp_dev *dev,
387387
/* allocated dai_config if not yet */
388388
if (!dd->dai_spec_config) {
389389
size = sizeof(*copier_cfg);
390-
dd->dai_spec_config = rzalloc(SOF_MEM_FLAG_USER, size);
390+
dd->dai_spec_config = sof_heap_alloc(dd->alloc_ctx.heap, SOF_MEM_FLAG_USER, size, 0);
391391
if (!dd->dai_spec_config) {
392392
comp_err(dev, "No memory for size %d", size);
393393
return -ENOMEM;
394394
}
395395

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

0 commit comments

Comments
 (0)