Skip to content

Commit 8fe5c62

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 effect kernel space use. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent 1108761 commit 8fe5c62

3 files changed

Lines changed: 41 additions & 18 deletions

File tree

src/audio/dai-zephyr.c

Lines changed: 36 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>
2627
#include <sof/list.h>
2728
#include <rtos/spinlock.h>
2829
#include <rtos/string.h>
@@ -506,6 +507,15 @@ __cold int dai_common_new(struct dai_data *dd, struct comp_dev *dev,
506507
dd->xrun = 0;
507508
dd->chan = NULL;
508509

510+
#ifdef CONFIG_SOF_USERSPACE_LL
511+
/*
512+
* copier_dai_create() uses mod_zalloc() to allocate
513+
* the 'dd' dai data object and does not set dd->heap.
514+
* If LL is run in user-space, assign the 'heap' here.
515+
*/
516+
dd->heap = zephyr_ll_user_heap();
517+
#endif
518+
509519
/* I/O performance init, keep it last so the function does not reach this in case
510520
* of return on error, so that we do not waste a slot
511521
*/
@@ -558,6 +568,7 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
558568
struct comp_dev *dev;
559569
const struct ipc_config_dai *dai_cfg = spec;
560570
struct dai_data *dd;
571+
struct k_heap *heap = NULL;
561572
int ret;
562573

563574
assert_can_be_cold();
@@ -570,10 +581,17 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
570581

571582
dev->ipc_config = *config;
572583

573-
dd = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd));
584+
#ifdef CONFIG_SOF_USERSPACE_LL
585+
heap = zephyr_ll_user_heap();
586+
#endif
587+
588+
dd = sof_heap_alloc(heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd), 0);
574589
if (!dd)
575590
goto e_data;
576591

592+
memset(dd, 0, sizeof(*dd));
593+
dd->heap = heap;
594+
577595
comp_set_drvdata(dev, dd);
578596

579597
ret = dai_common_new(dd, dev, dai_cfg);
@@ -587,7 +605,7 @@ __cold static struct comp_dev *dai_new(const struct comp_driver *drv,
587605
return dev;
588606

589607
error:
590-
rfree(dd);
608+
sof_heap_free(dd->heap, dd);
591609
e_data:
592610
comp_free_device(dev);
593611
return NULL;
@@ -615,7 +633,7 @@ __cold void dai_common_free(struct dai_data *dd)
615633

616634
dai_put(dd->dai);
617635

618-
rfree(dd->dai_spec_config);
636+
sof_heap_free(dd->heap, dd->dai_spec_config);
619637
}
620638

621639
__cold static void dai_free(struct comp_dev *dev)
@@ -629,7 +647,7 @@ __cold static void dai_free(struct comp_dev *dev)
629647

630648
dai_common_free(dd);
631649

632-
rfree(dd);
650+
sof_heap_free(dd->heap, dd);
633651
comp_free_device(dev);
634652
}
635653

@@ -824,7 +842,7 @@ static int dai_set_sg_config(struct dai_data *dd, struct comp_dev *dev, uint32_t
824842
} while (--max_block_count > 0);
825843
}
826844

827-
err = dma_sg_alloc(NULL, &config->elem_array, SOF_MEM_FLAG_USER,
845+
err = dma_sg_alloc(dd->heap, &config->elem_array, SOF_MEM_FLAG_USER,
828846
config->direction,
829847
period_count,
830848
period_bytes,
@@ -850,8 +868,9 @@ static int dai_set_dma_config(struct dai_data *dd, struct comp_dev *dev)
850868

851869
comp_dbg(dev, "entry");
852870

853-
dma_cfg = rballoc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
854-
sizeof(struct dma_config));
871+
dma_cfg = sof_heap_alloc(dd->heap,
872+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
873+
sizeof(struct dma_config), 0);
855874
if (!dma_cfg) {
856875
comp_err(dev, "dma_cfg allocation failed");
857876
return -ENOMEM;
@@ -880,10 +899,11 @@ static int dai_set_dma_config(struct dai_data *dd, struct comp_dev *dev)
880899
else
881900
dma_cfg->dma_slot = config->src_dev;
882901

883-
dma_block_cfg = rballoc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
884-
sizeof(struct dma_block_config) * dma_cfg->block_count);
902+
dma_block_cfg = sof_heap_alloc(dd->heap,
903+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
904+
sizeof(struct dma_block_config) * dma_cfg->block_count, 0);
885905
if (!dma_block_cfg) {
886-
rfree(dma_cfg);
906+
sof_heap_free(dd->heap, dma_cfg);
887907
comp_err(dev, "dma_block_config allocation failed");
888908
return -ENOMEM;
889909
}
@@ -1017,7 +1037,7 @@ static int dai_set_dma_buffer(struct dai_data *dd, struct comp_dev *dev,
10171037
return err;
10181038
}
10191039
} else {
1020-
dd->dma_buffer = buffer_alloc_range(NULL, buffer_size_preferred, buffer_size,
1040+
dd->dma_buffer = buffer_alloc_range(dd->heap, buffer_size_preferred, buffer_size,
10211041
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA,
10221042
addr_align, BUFFER_USAGE_NOT_SHARED);
10231043
if (!dd->dma_buffer) {
@@ -1105,8 +1125,8 @@ int dai_common_params(struct dai_data *dd, struct comp_dev *dev,
11051125
if (err < 0) {
11061126
buffer_free(dd->dma_buffer);
11071127
dd->dma_buffer = NULL;
1108-
dma_sg_free(NULL, &config->elem_array);
1109-
rfree(dd->z_config);
1128+
dma_sg_free(dd->heap, &config->elem_array);
1129+
sof_heap_free(dd->heap, dd->z_config);
11101130
dd->z_config = NULL;
11111131
}
11121132

@@ -1236,10 +1256,10 @@ void dai_common_reset(struct dai_data *dd, struct comp_dev *dev)
12361256
if (!dd->delayed_dma_stop)
12371257
dai_dma_release(dd, dev);
12381258

1239-
dma_sg_free(NULL, &config->elem_array);
1259+
dma_sg_free(dd->heap, &config->elem_array);
12401260
if (dd->z_config) {
1241-
rfree(dd->z_config->head_block);
1242-
rfree(dd->z_config);
1261+
sof_heap_free(dd->heap, dd->z_config->head_block);
1262+
sof_heap_free(dd->heap, dd->z_config);
12431263
dd->z_config = NULL;
12441264
}
12451265

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ struct dai_data {
168168
#endif
169169
/* Copier gain params */
170170
struct copier_gain_params *gain_data;
171+
struct k_heap *heap;
171172
};
172173

173174
/* 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
@@ -374,15 +374,17 @@ __cold int dai_config(struct dai_data *dd, struct comp_dev *dev,
374374
/* allocated dai_config if not yet */
375375
if (!dd->dai_spec_config) {
376376
size = sizeof(*copier_cfg);
377-
dd->dai_spec_config = rzalloc(SOF_MEM_FLAG_USER, size);
377+
dd->dai_spec_config = sof_heap_alloc(dd->heap, SOF_MEM_FLAG_USER, size, 0);
378378
if (!dd->dai_spec_config) {
379379
comp_err(dev, "No memory for size %d", size);
380380
return -ENOMEM;
381381
}
382382

383+
memset(dd->dai_spec_config, 0, size);
384+
383385
ret = memcpy_s(dd->dai_spec_config, size, copier_cfg, size);
384386
if (ret < 0) {
385-
rfree(dd->dai_spec_config);
387+
sof_heap_free(dd->heap, dd->dai_spec_config);
386388
dd->dai_spec_config = NULL;
387389
return -EINVAL;
388390
}

0 commit comments

Comments
 (0)