Skip to content

Commit 43444b7

Browse files
committed
audio: host-zephyr: make component usable from user-space
Ensure an allocation context object is passed correctly whenever memory is allocated in the component. This allows to run the component both in kernel and user space. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent 9257a6f commit 43444b7

2 files changed

Lines changed: 34 additions & 14 deletions

File tree

src/audio/copier/host_copier.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ struct host_data {
120120
#ifdef CONFIG_SOF_TELEMETRY_IO_PERFORMANCE_MEASUREMENTS
121121
struct io_perf_data_item *io_perf_host_byte_count;
122122
#endif
123+
124+
struct mod_alloc_ctx alloc_ctx;
123125
};
124126

125127
int host_common_new(struct host_data *hd, struct comp_dev *dev,

src/audio/host-zephyr.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <sof/ut.h>
2626
#include <sof/trace/trace.h>
2727
#include <sof/debug/telemetry/performance_monitor.h>
28+
#include <sof/schedule/ll_schedule_domain.h> /* zephyr_ll_user_heap() */
2829
#include <ipc/stream.h>
2930
#include <ipc/topology.h>
3031
#include <user/trace.h>
@@ -610,7 +611,7 @@ static int create_local_elems(struct host_data *hd, struct comp_dev *dev,
610611
elem_array = &hd->local.elem_array;
611612

612613
/* config buffer will be used as proxy */
613-
err = dma_sg_alloc(NULL, &hd->config.elem_array, SOF_MEM_FLAG_USER,
614+
err = dma_sg_alloc(hd->alloc_ctx.heap, &hd->config.elem_array, SOF_MEM_FLAG_USER,
614615
dir, 1, 0, 0, 0);
615616
if (err < 0) {
616617
comp_err(dev, "dma_sg_alloc() failed");
@@ -620,7 +621,7 @@ static int create_local_elems(struct host_data *hd, struct comp_dev *dev,
620621
elem_array = &hd->config.elem_array;
621622
}
622623

623-
err = dma_sg_alloc(NULL, elem_array, SOF_MEM_FLAG_USER, dir, buffer_count,
624+
err = dma_sg_alloc(hd->alloc_ctx.heap, elem_array, SOF_MEM_FLAG_USER, dir, buffer_count,
624625
buffer_bytes,
625626
(uintptr_t)audio_stream_get_addr(&hd->dma_buffer->stream), 0);
626627
if (err < 0) {
@@ -729,6 +730,17 @@ __cold int host_common_new(struct host_data *hd, struct comp_dev *dev,
729730
hd->chan_index = -EINVAL;
730731
hd->copy_type = COMP_COPY_NORMAL;
731732

733+
#ifdef CONFIG_SOF_USERSPACE_LL
734+
/*
735+
* copier_host_create() uses mod_zalloc() to allocate
736+
* the 'hd' host data object and does not set hd->alloc_ctx.
737+
* If LL is run in user-space, assign the 'heap' here.
738+
*/
739+
hd->alloc_ctx.heap = zephyr_ll_user_heap();
740+
#else
741+
hd->alloc_ctx.heap = NULL;
742+
#endif
743+
732744
return 0;
733745
}
734746

@@ -739,6 +751,7 @@ __cold static struct comp_dev *host_new(const struct comp_driver *drv,
739751
struct comp_dev *dev;
740752
struct host_data *hd;
741753
const struct ipc_config_host *ipc_host = spec;
754+
struct k_heap *heap = NULL;
742755
int ret;
743756

744757
assert_can_be_cold();
@@ -750,10 +763,12 @@ __cold static struct comp_dev *host_new(const struct comp_driver *drv,
750763
return NULL;
751764
dev->ipc_config = *config;
752765

753-
hd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*hd));
766+
hd = sof_heap_alloc(heap, SOF_MEM_FLAG_USER, sizeof(*hd), 0);
754767
if (!hd)
755768
goto e_data;
756769

770+
memset(hd, 0, sizeof(*hd));
771+
757772
hd->nobytes_last_logged = k_uptime_get();
758773
comp_set_drvdata(dev, hd);
759774

@@ -766,7 +781,7 @@ __cold static struct comp_dev *host_new(const struct comp_driver *drv,
766781
return dev;
767782

768783
e_dev:
769-
rfree(hd);
784+
sof_heap_free(heap, hd);
770785
e_data:
771786
comp_free_device(dev);
772787
return NULL;
@@ -794,7 +809,7 @@ __cold void host_common_free(struct host_data *hd)
794809
sof_dma_put(hd->dma);
795810

796811
ipc_msg_free(hd->msg);
797-
dma_sg_free(NULL, &hd->config.elem_array);
812+
dma_sg_free(hd->alloc_ctx.heap, &hd->config.elem_array);
798813
}
799814

800815
__cold static void host_free(struct comp_dev *dev)
@@ -805,7 +820,8 @@ __cold static void host_free(struct comp_dev *dev)
805820

806821
comp_dbg(dev, "entry");
807822
host_common_free(hd);
808-
rfree(hd);
823+
/* NULL heap passed in host_new(), must match! */
824+
sof_heap_free(NULL, hd);
809825
comp_free_device(dev);
810826
}
811827

@@ -963,7 +979,7 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev,
963979
}
964980
} else {
965981
/* allocate not shared buffer */
966-
hd->dma_buffer = buffer_alloc_range(NULL, buffer_size_preferred, buffer_size,
982+
hd->dma_buffer = buffer_alloc_range(&hd->alloc_ctx, buffer_size_preferred, buffer_size,
967983
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA,
968984
addr_align, BUFFER_USAGE_NOT_SHARED);
969985
if (!hd->dma_buffer) {
@@ -1020,15 +1036,17 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev,
10201036

10211037
memset(dma_cfg, 0, sizeof(*dma_cfg));
10221038

1023-
dma_block_cfg = rzalloc(SOF_MEM_FLAG_USER,
1024-
sizeof(*dma_block_cfg));
1039+
dma_block_cfg = sof_heap_alloc(hd->alloc_ctx.heap, SOF_MEM_FLAG_USER,
1040+
sizeof(*dma_block_cfg), 0);
10251041

10261042
if (!dma_block_cfg) {
10271043
comp_err(dev, "dma_block_config allocation failed");
10281044
err = -ENOMEM;
10291045
goto err_release_channel;
10301046
}
10311047

1048+
memset(dma_block_cfg, 0, sizeof(*dma_block_cfg));
1049+
10321050
dma_cfg->block_count = 1;
10331051
dma_cfg->source_data_size = config->src_width;
10341052
dma_cfg->dest_data_size = config->dest_width;
@@ -1110,7 +1128,7 @@ int host_common_params(struct host_data *hd, struct comp_dev *dev,
11101128

11111129
err_free_block_cfg:
11121130
dma_cfg->head_block = NULL;
1113-
rfree(dma_block_cfg);
1131+
sof_heap_free(hd->alloc_ctx.heap, dma_block_cfg);
11141132
err_release_channel:
11151133
sof_dma_release_channel(hd->dma, hd->chan_index);
11161134
hd->chan_index = -EINVAL;
@@ -1178,9 +1196,9 @@ void host_common_reset(struct host_data *hd, uint16_t state)
11781196
}
11791197

11801198
/* free all DMA elements */
1181-
dma_sg_free(NULL, &hd->host.elem_array);
1182-
dma_sg_free(NULL, &hd->local.elem_array);
1183-
dma_sg_free(NULL, &hd->config.elem_array);
1199+
dma_sg_free(hd->alloc_ctx.heap, &hd->host.elem_array);
1200+
dma_sg_free(hd->alloc_ctx.heap, &hd->local.elem_array);
1201+
dma_sg_free(hd->alloc_ctx.heap, &hd->config.elem_array);
11841202

11851203
/* free DMA buffer */
11861204
if (hd->dma_buffer) {
@@ -1190,7 +1208,7 @@ void host_common_reset(struct host_data *hd, uint16_t state)
11901208

11911209
/* free DMA block configuration */
11921210
if (hd->z_config.head_block)
1193-
rfree(hd->z_config.head_block);
1211+
sof_heap_free(hd->alloc_ctx.heap, hd->z_config.head_block);
11941212

11951213
/* reset buffer pointers */
11961214
hd->local_pos = 0;

0 commit comments

Comments
 (0)