diff --git a/sound/soc/sof/Makefile b/sound/soc/sof/Makefile index 6f76927e0a1c2f..4a823c79abaf90 100644 --- a/sound/soc/sof/Makefile +++ b/sound/soc/sof/Makefile @@ -1,7 +1,8 @@ # SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) snd-sof-objs := core.o ops.o loader.o ipc.o pcm.o pm.o debug.o topology.o\ - control.o trace.o utils.o sof-audio.o stream-ipc.o ipc4.o + control.o trace.o utils.o sof-audio.o stream-ipc.o ipc4.o\ + ipc3-topology.o snd-sof-$(CONFIG_SND_SOC_SOF_DEBUG_PROBES) += sof-probes.o snd-sof-$(CONFIG_SND_SOC_SOF_COMPRESS) += compress.o diff --git a/sound/soc/sof/ipc3-topology.c b/sound/soc/sof/ipc3-topology.c new file mode 100644 index 00000000000000..2c44310fae3cc3 --- /dev/null +++ b/sound/soc/sof/ipc3-topology.c @@ -0,0 +1,206 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) +// +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// Copyright(c) 2021 Intel Corporation. All rights reserved. +// +// + +#include +#include "sof-priv.h" +#include "sof-audio.h" +#include "ops.h" + +int sof_widget_update_ipc_comp_host(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget) +{ + struct sof_ipc_comp_host *host; + size_t ipc_size = sizeof(*host); + + host = (struct sof_ipc_comp_host *) + sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); + if (!host) + return -ENOMEM; + + /* configure host comp IPC message */ + host->comp.type = SOF_COMP_HOST; + if (swidget->id == snd_soc_dapm_aif_out) + host->direction = SOF_IPC_STREAM_CAPTURE; + else + host->direction = SOF_IPC_STREAM_PLAYBACK; + host->config.hdr.size = sizeof(host->config); + + /* update pcm_tokens */ + sof_update_ipc_object(host, pcm_tokens, pcm_token_size, swidget->num_tuples, + swidget->tuples, sizeof(*host), 1); + + /* update comp_tokens */ + sof_update_ipc_object(&host->config, comp_tokens_new, comp_token_size, swidget->num_tuples, + swidget->tuples, sizeof(host->config), 1); + + dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name); + sof_dbg_comp_config(scomp, &host->config); + + swidget->private = host; + + return 0; +} + + +int sof_widget_update_ipc_comp_dai(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget) +{ + struct sof_ipc_comp_dai *comp_dai; + size_t ipc_size = sizeof(*comp_dai); + struct snd_sof_dai *dai = swidget->private; + + comp_dai = (struct sof_ipc_comp_dai *) + sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); + if (!comp_dai) + return -ENOMEM; + + /* configure dai IPC message */ + comp_dai->comp.type = SOF_COMP_DAI; + comp_dai->config.hdr.size = sizeof(comp_dai->config); + + /* update dai_tokens */ + sof_update_ipc_object(comp_dai, dai_tokens_new, dai_token_size, dai->num_tuples, + dai->tuples, sizeof(*comp_dai), 1); + + /* update comp_tokens */ + sof_update_ipc_object(&comp_dai->config, comp_tokens_new, comp_token_size, dai->num_tuples, + dai->tuples, sizeof(comp_dai->config), 1); + + /* + * copy only the sof_ipc_comp_dai to avoid collapsing + * the snd_sof_dai, the extended data is kept in the + * snd_sof_widget. + */ + memcpy(&dai->comp_dai, comp_dai, sizeof(*comp_dai)); + + dev_dbg(scomp->dev, "%s dai %s: type %d index %d\n", + __func__, swidget->widget->name, comp_dai->type, comp_dai->dai_index); + sof_dbg_comp_config(scomp, &comp_dai->config); + return 0; +} + +int sof_widget_update_ipc_comp_pipeline(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget) +{ + struct sof_ipc_pipe_new *pipeline; + struct snd_sof_widget *comp_swidget; + size_t ipc_size = sizeof(*pipeline); + + pipeline = (struct sof_ipc_pipe_new *) + sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); + if (!pipeline) + return -ENOMEM; + + /* configure dai IPC message */ + pipeline->hdr.size = sizeof(*pipeline); + pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW; + pipeline->pipeline_id = swidget->pipeline_id; + pipeline->comp_id = swidget->comp_id; + + /* component at start of pipeline is our stream id */ + comp_swidget = snd_sof_find_swidget(scomp, swidget->sname);; + + if (!comp_swidget) { + dev_err(scomp->dev, "error: widget %s refers to non existent widget %s\n", + swidget->widget->name, swidget->sname); + return -EINVAL; + } + + pipeline->sched_id = comp_swidget->comp_id; + + sof_update_ipc_object(pipeline, sched_tokens, sched_token_size, swidget->num_tuples, + swidget->tuples, sizeof(*pipeline), 1); + + sof_update_ipc_object(swidget, pipeline_tokens, pipeline_token_size, swidget->num_tuples, + swidget->tuples, sizeof(*swidget), 1); + + if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) + pipeline->core = SOF_DSP_PRIMARY_CORE; + + if (sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_OVERRIDE)) + swidget->dynamic_pipeline_widget = + sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_ENABLE); + + dev_dbg(scomp->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d dynamic %d\n", + swidget->widget->name, pipeline->period, pipeline->priority, + pipeline->period_mips, pipeline->core, pipeline->frames_per_sched, + swidget->dynamic_pipeline_widget); + + swidget->private = pipeline; + + return 0; +} + +int sof_widget_update_ipc_comp_mixer(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget) +{ + struct sof_ipc_comp_mixer *mixer; + size_t ipc_size = sizeof(*mixer); + + mixer = (struct sof_ipc_comp_mixer *) + sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); + if (!mixer) + return -ENOMEM; + + /* configure mixer IPC message */ + mixer->comp.type = SOF_COMP_MIXER; + mixer->config.hdr.size = sizeof(mixer->config); + + sof_update_ipc_object(&mixer->config, comp_tokens_new, comp_token_size, swidget->num_tuples, + swidget->tuples, sizeof(mixer->config), 1); + + sof_dbg_comp_config(scomp, &mixer->config); + + swidget->private = mixer; + + return 0; +} +int sof_widget_update_ipc_comp_pga(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget) +{ + struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); + struct sof_ipc_comp_volume *volume; + struct snd_sof_control *scontrol; + size_t ipc_size = sizeof(*volume); + int min_step; + int max_step; + + volume = (struct sof_ipc_comp_volume *) + sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); + if (!volume) + return -ENOMEM; + + /* configure volume IPC message */ + volume->comp.type = SOF_COMP_VOLUME; + volume->config.hdr.size = sizeof(volume->config); + + sof_update_ipc_object(volume, volume_tokens, volume_token_size, swidget->num_tuples, + swidget->tuples, sizeof(*volume), 1); + + sof_update_ipc_object(&volume->config, comp_tokens_new, comp_token_size, swidget->num_tuples, + swidget->tuples, sizeof(volume->config), 1); + + sof_dbg_comp_config(scomp, &volume->config); + + swidget->private = volume; + + list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { + if (scontrol->comp_id == swidget->comp_id && + scontrol->volume_table) { + min_step = scontrol->min_volume_step; + max_step = scontrol->max_volume_step; + volume->min_value = scontrol->volume_table[min_step]; + volume->max_value = scontrol->volume_table[max_step]; + volume->channels = scontrol->num_channels; + break; + } + } + + return 0; +} diff --git a/sound/soc/sof/sof-audio.h b/sound/soc/sof/sof-audio.h index f3009e6b91a10b..7c9f9ea2b944ae 100644 --- a/sound/soc/sof/sof-audio.h +++ b/sound/soc/sof/sof-audio.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "sof-priv.h" #define SOF_AUDIO_PCM_DRV_NAME "sof-audio-component" @@ -30,6 +31,40 @@ #define WIDGET_IS_DAI(id) ((id) == snd_soc_dapm_dai_in || (id) == snd_soc_dapm_dai_out) +struct snd_sof_widget; +struct sof_ipc_comp *sof_comp_alloc(struct snd_sof_widget *swidget, size_t *ipc_size, int index); + +/* + * Topology Token Parsing. + * New tokens should be added to headers and parsing tables below. + */ + +struct sof_topology_token { + u32 token; + u32 type; + u32 (*get_token)(void *elem, void *object, u32 offset, u32 size); + u32 offset; + u32 size; +}; + +struct snd_sof_tuple { + u32 token; + u32 value; +}; + +extern const struct sof_topology_token pcm_tokens[]; +extern const struct sof_topology_token dai_tokens_new[]; +extern const struct sof_topology_token sched_tokens[]; +extern const struct sof_topology_token pipeline_tokens[]; +extern const struct sof_topology_token volume_tokens[]; +extern const struct sof_topology_token comp_tokens_new[]; +extern int pcm_token_size; +extern int comp_token_size; +extern int dai_token_size; +extern int sched_token_size; +extern int pipeline_token_size; +extern int volume_token_size; + /* PCM stream, mapped to FW component */ struct snd_sof_pcm_stream { u32 comp_id; @@ -55,6 +90,8 @@ struct snd_sof_pcm { struct list_head list; /* list in sdev pcm list */ struct snd_pcm_hw_params params[2]; bool prepared[2]; /* PCM_PARAMS set successfully */ + int num_tuples; + struct snd_sof_tuple *tuples; }; struct snd_sof_led_control { @@ -82,9 +119,10 @@ struct snd_sof_control { /* if true, the control's data needs to be updated from Firmware */ bool comp_data_dirty; -}; -struct snd_sof_widget; + int num_tuples; + struct snd_sof_tuple *tuples; +}; /* ASoC SOF DAPM widget */ struct snd_sof_widget { @@ -95,6 +133,7 @@ struct snd_sof_widget { int use_count; /* use_count will be protected by the PCM mutex held by the core */ int core; int id; + int num_tuples; /* * Flag indicating if the widget should be set up dynamically when a PCM is opened. @@ -113,6 +152,10 @@ struct snd_sof_widget { /* extended data for UUID components */ struct sof_ipc_comp_ext comp_ext; + struct snd_sof_tuple *tuples; + + char sname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; + void *private; /* core does not touch this */ }; @@ -133,6 +176,7 @@ struct snd_sof_route { struct snd_sof_dai { struct snd_soc_component *scomp; const char *name; + int num_tuples; struct sof_ipc_comp_dai comp_dai; int number_configs; @@ -140,6 +184,7 @@ struct snd_sof_dai { bool configured; /* DAI configured during BE hw_params */ struct sof_ipc_dai_config *dai_config; struct list_head list; /* list in sdev dai list */ + struct snd_sof_tuple *tuples; }; /* @@ -264,4 +309,18 @@ int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev struct snd_sof_pcm *spcm); int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream, struct snd_sof_pcm *spcm, int dir, bool free_widget_list); +int sof_widget_update_ipc_comp_host(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget); +int sof_widget_update_ipc_comp_dai(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget); +int sof_widget_update_ipc_comp_pipeline(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget); +int sof_widget_update_ipc_comp_mixer(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget); +int sof_widget_update_ipc_comp_pga(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget); +void sof_dbg_comp_config(struct snd_soc_component *scomp, struct sof_ipc_comp_config *config); +void sof_update_ipc_object(void *object, const struct sof_topology_token *tokens, int count, + int num_tuples, struct snd_sof_tuple *tuples, + size_t object_size, int num_sets); #endif diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c index e72dcae5e7ee7a..0bdd4fb5aa9819 100644 --- a/sound/soc/sof/topology.c +++ b/sound/soc/sof/topology.c @@ -465,73 +465,74 @@ static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type) return SOF_COMP_NONE; } -/* - * Topology Token Parsing. - * New tokens should be added to headers and parsing tables below. - */ - -struct sof_topology_token { - u32 token; - u32 type; - int (*get_token)(void *elem, void *object, u32 offset, u32 size); - u32 offset; - u32 size; -}; - -static int get_token_u32(void *elem, void *object, u32 offset, u32 size) +u32 get_token_u32(void *elem, void *object, u32 offset, u32 size) { struct snd_soc_tplg_vendor_value_elem *velem = elem; u32 *val = (u32 *)((u8 *)object + offset); *val = le32_to_cpu(velem->value); - return 0; + return *val; } -static int get_token_u16(void *elem, void *object, u32 offset, u32 size) +static u32 get_token_u16(void *elem, void *object, u32 offset, u32 size) { struct snd_soc_tplg_vendor_value_elem *velem = elem; u16 *val = (u16 *)((u8 *)object + offset); *val = (u16)le32_to_cpu(velem->value); - return 0; + return *val; } -static int get_token_uuid(void *elem, void *object, u32 offset, u32 size) +static u32 get_token_uuid(void *elem, void *object, u32 offset, u32 size) { struct snd_soc_tplg_vendor_uuid_elem *velem = elem; u8 *dst = (u8 *)object + offset; memcpy(dst, velem->uuid, UUID_SIZE); - return 0; + return *dst; } -static int get_token_comp_format(void *elem, void *object, u32 offset, u32 size) +u32 get_token_comp_format(void *elem, void *object, u32 offset, u32 size) { struct snd_soc_tplg_vendor_string_elem *velem = elem; u32 *val = (u32 *)((u8 *)object + offset); *val = find_format(velem->string); - return 0; + return *val; } -static int get_token_dai_type(void *elem, void *object, u32 offset, u32 size) +u32 get_token_comp_format_new(void *elem, void *object, u32 offset, u32 size) +{ + struct snd_soc_tplg_vendor_string_elem *velem = elem; + + return find_format(velem->string); +} + +static u32 get_token_dai_type(void *elem, void *object, u32 offset, u32 size) { struct snd_soc_tplg_vendor_string_elem *velem = elem; u32 *val = (u32 *)((u8 *)object + offset); *val = find_dai(velem->string); - return 0; + return *val; +} + +static u32 get_token_dai_type_new(void *elem, void *object, u32 offset, u32 size) +{ + struct snd_soc_tplg_vendor_string_elem *velem = elem; + + return find_dai(velem->string); } -static int get_token_process_type(void *elem, void *object, u32 offset, +static u32 get_token_process_type(void *elem, void *object, u32 offset, u32 size) { struct snd_soc_tplg_vendor_string_elem *velem = elem; u32 *val = (u32 *)((u8 *)object + offset); *val = find_process(velem->string); - return 0; + return *val; } /* Buffers */ @@ -543,7 +544,7 @@ static const struct sof_topology_token buffer_tokens[] = { }; /* DAI */ -static const struct sof_topology_token dai_tokens[] = { +const struct sof_topology_token dai_tokens[] = { {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, offsetof(struct sof_ipc_comp_dai, type), 0}, {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, @@ -552,6 +553,16 @@ static const struct sof_topology_token dai_tokens[] = { offsetof(struct sof_ipc_comp_dai, direction), 0}, }; +const struct sof_topology_token dai_tokens_new[] = { + {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type_new, + offsetof(struct sof_ipc_comp_dai, type), 0}, + {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_ipc_comp_dai, dai_index), 0}, + {SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_ipc_comp_dai, direction), 0}, +}; +int dai_token_size = ARRAY_SIZE(dai_tokens_new); + /* BE DAI link */ static const struct sof_topology_token dai_link_tokens[] = { {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, @@ -561,7 +572,7 @@ static const struct sof_topology_token dai_link_tokens[] = { }; /* scheduling */ -static const struct sof_topology_token sched_tokens[] = { +const struct sof_topology_token sched_tokens[] = { {SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, offsetof(struct sof_ipc_pipe_new, period), 0}, {SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, @@ -575,21 +586,24 @@ static const struct sof_topology_token sched_tokens[] = { {SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, offsetof(struct sof_ipc_pipe_new, time_domain), 0}, }; +int sched_token_size = ARRAY_SIZE(sched_tokens); -static const struct sof_topology_token pipeline_tokens[] = { +const struct sof_topology_token pipeline_tokens[] = { {SOF_TKN_SCHED_DYNAMIC_PIPELINE, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, offsetof(struct snd_sof_widget, dynamic_pipeline_widget), 0}, }; +int pipeline_token_size = ARRAY_SIZE(pipeline_tokens); /* volume */ -static const struct sof_topology_token volume_tokens[] = { +const struct sof_topology_token volume_tokens[] = { {SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, offsetof(struct sof_ipc_comp_volume, ramp), 0}, {SOF_TKN_VOLUME_RAMP_STEP_MS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, offsetof(struct sof_ipc_comp_volume, initial_ramp), 0}, }; +int volume_token_size = ARRAY_SIZE(volume_tokens); /* SRC */ static const struct sof_topology_token src_tokens[] = { @@ -625,11 +639,13 @@ static const struct sof_topology_token process_tokens[] = { }; /* PCM */ -static const struct sof_topology_token pcm_tokens[] = { +const struct sof_topology_token pcm_tokens[] = { {SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, - offsetof(struct sof_ipc_comp_host, dmac_config), 0}, + offsetof(struct sof_ipc_comp_host, dmac_config), 0}, }; +int pcm_token_size = ARRAY_SIZE(pcm_tokens); + /* PCM */ static const struct sof_topology_token stream_tokens[] = { {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, @@ -641,18 +657,33 @@ static const struct sof_topology_token stream_tokens[] = { }; /* Generic components */ -static const struct sof_topology_token comp_tokens[] = { +const struct sof_topology_token comp_tokens[] = { {SOF_TKN_COMP_PERIOD_SINK_COUNT, - SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, - offsetof(struct sof_ipc_comp_config, periods_sink), 0}, + SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_ipc_comp_config, periods_sink), 0}, {SOF_TKN_COMP_PERIOD_SOURCE_COUNT, - SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, - offsetof(struct sof_ipc_comp_config, periods_source), 0}, + SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_ipc_comp_config, periods_source), 0}, {SOF_TKN_COMP_FORMAT, - SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format, - offsetof(struct sof_ipc_comp_config, frame_fmt), 0}, + SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format, + offsetof(struct sof_ipc_comp_config, frame_fmt), 0}, +}; + +/* this should replace comp_tokens[] after switch all token parsing */ +const struct sof_topology_token comp_tokens_new[] = { + {SOF_TKN_COMP_PERIOD_SINK_COUNT, + SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_ipc_comp_config, periods_sink), 0}, + {SOF_TKN_COMP_PERIOD_SOURCE_COUNT, + SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_ipc_comp_config, periods_source), 0}, + {SOF_TKN_COMP_FORMAT, + SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format_new, + offsetof(struct sof_ipc_comp_config, frame_fmt), 0}, }; +int comp_token_size = ARRAY_SIZE(comp_tokens_new); + /* SSP */ static const struct sof_topology_token ssp_tokens[] = { {SOF_TKN_INTEL_SSP_CLKS_CONTROL, @@ -894,6 +925,65 @@ static int sof_parse_string_tokens(struct snd_soc_component *scomp, return found; } +/* this should replace sof_parse_string_tokens() after switch all token parsing */ +static int sof_parse_string_tokens_new(struct snd_soc_component *scomp, + const struct sof_topology_token *tokens, + int count, struct snd_soc_tplg_vendor_array *array, + struct snd_sof_tuple **tuples, int *num_tuples) +{ + struct snd_soc_tplg_vendor_string_elem *elem; + int found = 0; + int i, j; + u32 value; + + /* parse element by element */ + for (i = 0; i < le32_to_cpu(array->num_elems); i++) { + elem = &array->string[i]; + + /* search for token */ + for (j = 0; j < count; j++) { + struct snd_sof_tuple *new_tuples; + + /* match token type */ + if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING) + continue; + + /* match token id */ + if (tokens[j].token != le32_to_cpu(elem->token)) + continue; + + /* matched - get value */ + value = tokens[j].get_token(elem, NULL, 0, tokens[j].size); + + if (!tuples && !num_tuples) + continue; + + /* save token in the tuple array */ + if (!(*num_tuples)) { + new_tuples = devm_kzalloc(scomp->dev, sizeof(struct snd_sof_tuple), + GFP_KERNEL); + if (!new_tuples) + return -ENOMEM; + } else { + new_tuples = devm_krealloc(scomp->dev, *tuples, + sizeof(struct snd_sof_tuple) * + (*num_tuples + 1), + GFP_KERNEL); + if (!new_tuples) + return -ENOMEM; + } + + new_tuples[*num_tuples].token = tokens[j].token; + new_tuples[*num_tuples].value = value; + *tuples = new_tuples; + (*num_tuples)++; + found++; + } + } + + return found; +} + static int sof_parse_word_tokens(struct snd_soc_component *scomp, void *object, const struct sof_topology_token *tokens, @@ -934,6 +1024,65 @@ static int sof_parse_word_tokens(struct snd_soc_component *scomp, return found; } +/* this should replace sof_parse_word_tokens() after switch all token parsing */ +static int sof_parse_word_tokens_new(struct snd_soc_component *scomp, + const struct sof_topology_token *tokens, + int count, + struct snd_soc_tplg_vendor_array *array, + struct snd_sof_tuple **tuples, int *num_tuples) +{ + struct snd_soc_tplg_vendor_value_elem *elem; + int found = 0; + int i, j; + + /* parse element by element */ + for (i = 0; i < le32_to_cpu(array->num_elems); i++) { + elem = &array->value[i]; + + /* search for token */ + for (j = 0; j < count; j++) { + struct snd_sof_tuple *new_tuples; + + /* match token type */ + if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD || + tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT || + tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE || + tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL)) + continue; + + /* match token id */ + if (tokens[j].token != le32_to_cpu(elem->token)) + continue; + + if (!tuples && !num_tuples) + continue; + + /* save token in the tuple array */ + if (!(*num_tuples)) { + new_tuples = devm_kzalloc(scomp->dev, sizeof(struct snd_sof_tuple), + GFP_KERNEL); + if (!new_tuples) + return -ENOMEM; + } else { + new_tuples = devm_krealloc(scomp->dev, *tuples, + sizeof(struct snd_sof_tuple) * + (*num_tuples + 1), + GFP_KERNEL); + if (!new_tuples) + return -ENOMEM; + } + + new_tuples[*num_tuples].token = tokens[j].token; + new_tuples[*num_tuples].value = le32_to_cpu(elem->value); + *tuples = new_tuples; + (*num_tuples)++; + found++; + } + } + + return found; +} + /** * sof_parse_token_sets - Parse multiple sets of tokens * @scomp: pointer to soc component @@ -1016,6 +1165,90 @@ static int sof_parse_token_sets(struct snd_soc_component *scomp, return 0; } +/* this should replace sof_parse_token_sets() after all token parsing is switched */ +static int sof_parse_token_sets_new(struct snd_soc_component *scomp, + void *object, const struct sof_topology_token *tokens, + int count, struct snd_soc_tplg_vendor_array *array, + int priv_size, int sets, size_t object_size, + struct snd_sof_tuple **tuples, int *num_tuples) +{ + size_t offset = 0; + int found = 0; + int total = 0; + int asize; + + while (priv_size > 0 && total < count * sets) { + asize = le32_to_cpu(array->size); + + /* validate asize */ + if (asize < 0) { /* FIXME: A zero-size array makes no sense */ + dev_err(scomp->dev, "error: invalid array size 0x%x\n", + asize); + return -EINVAL; + } + + /* make sure there is enough data before parsing */ + priv_size -= asize; + if (priv_size < 0) { + dev_err(scomp->dev, "error: invalid array size 0x%x\n", + asize); + return -EINVAL; + } + + /* call correct parser depending on type */ + switch (le32_to_cpu(array->type)) { + case SND_SOC_TPLG_TUPLE_TYPE_UUID: + found += sof_parse_uuid_tokens(scomp, object, tokens, count, array, + offset); + break; + case SND_SOC_TPLG_TUPLE_TYPE_STRING: + found += sof_parse_string_tokens_new(scomp, tokens, count, array, + tuples, num_tuples); + break; + case SND_SOC_TPLG_TUPLE_TYPE_BOOL: + case SND_SOC_TPLG_TUPLE_TYPE_BYTE: + case SND_SOC_TPLG_TUPLE_TYPE_WORD: + case SND_SOC_TPLG_TUPLE_TYPE_SHORT: + found += sof_parse_word_tokens_new(scomp, tokens, count, array, tuples, + num_tuples); + break; + default: + dev_err(scomp->dev, "error: unknown token type %d\n", + array->type); + return -EINVAL; + } + + /* next array */ + array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + + asize); + + /* move to next target struct */ + if (found >= count) { + offset += object_size; + total += found; + found = 0; + } + } + + return 0; +} + +/* this should replace sof_parse_tokens() after switch all token parsing */ +static int sof_parse_tokens_new(struct snd_soc_component *scomp, void *object, + const struct sof_topology_token *tokens, int count, + struct snd_soc_tplg_vendor_array *array, + int priv_size, struct snd_sof_tuple **tuples, int *num_tuples) +{ + /* + * sof_parse_tokens is used when topology contains only a single set of + * identical tuples arrays. So additional parameters to + * sof_parse_token_sets are sets = 1 (only 1 set) and + * object_size = 0 (irrelevant). + */ + return sof_parse_token_sets_new(scomp, object, tokens, count, array, + priv_size, 1, 0, tuples, num_tuples); +} + static int sof_parse_tokens(struct snd_soc_component *scomp, void *object, const struct sof_topology_token *tokens, @@ -1033,14 +1266,63 @@ static int sof_parse_tokens(struct snd_soc_component *scomp, priv_size, 1, 0); } -static void sof_dbg_comp_config(struct snd_soc_component *scomp, - struct sof_ipc_comp_config *config) +void sof_dbg_comp_config(struct snd_soc_component *scomp, struct sof_ipc_comp_config *config) { dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n", config->periods_sink, config->periods_source, config->frame_fmt); } +/* + * update the IPC struct based on the tuple array. + */ +void sof_update_ipc_object(void *object, const struct sof_topology_token *tokens, int count, + int num_tuples, struct snd_sof_tuple *tuples, + size_t object_size, int num_sets) +{ + int i,j; + int num_tokens_matched = 0; + int offset = 0; + + for (i = 0; i < count; i++) { + for (j = 0; j < num_tuples; j++) { + if (tokens[i].token == tuples[j].token) { + switch (tokens[i].type) { + case SND_SOC_TPLG_TUPLE_TYPE_WORD: + case SND_SOC_TPLG_TUPLE_TYPE_STRING: + { + u32 *val = (u32 *)((u8 *)object + tokens[i].offset + + offset); + + *val = tuples[j].value; + break; + } + case SND_SOC_TPLG_TUPLE_TYPE_SHORT: + case SND_SOC_TPLG_TUPLE_TYPE_BOOL: + { + u16 *val = (u16 *)((u8 *)object + tokens[i].offset + + offset); + + *val = (u16) tuples[j].value; + break; + } + default: + break; + } + + num_tokens_matched++; + if (!(num_tokens_matched % count)) { + /* found all required tokens */ + if ((num_tokens_matched / count) == num_sets) + return; + /* move to the next set */ + offset += object_size; + } + } + } + } +} + /* * Standard Kcontrols. */ @@ -1436,8 +1718,7 @@ static int sof_connect_dai_widget(struct snd_soc_component *scomp, * * Return: The pointer to the new allocated component, NULL if failed. */ -static struct sof_ipc_comp *sof_comp_alloc(struct snd_sof_widget *swidget, - size_t *ipc_size, int index) +struct sof_ipc_comp *sof_comp_alloc(struct snd_sof_widget *swidget, size_t *ipc_size, int index) { u8 nil_uuid[SOF_UUID_SIZE] = {0}; struct sof_ipc_comp *comp; @@ -1476,54 +1757,31 @@ static int sof_widget_load_dai(struct snd_soc_component *scomp, int index, struct snd_sof_dai *dai) { struct snd_soc_tplg_private *private = &tw->priv; - struct sof_ipc_comp_dai *comp_dai; - size_t ipc_size = sizeof(*comp_dai); int ret; - comp_dai = (struct sof_ipc_comp_dai *) - sof_comp_alloc(swidget, &ipc_size, index); - if (!comp_dai) - return -ENOMEM; - - /* configure dai IPC message */ - comp_dai->comp.type = SOF_COMP_DAI; - comp_dai->config.hdr.size = sizeof(comp_dai->config); - - ret = sof_parse_tokens(scomp, comp_dai, dai_tokens, - ARRAY_SIZE(dai_tokens), private->array, - le32_to_cpu(private->size)); + ret = sof_parse_tokens_new(scomp, NULL, dai_tokens_new, + ARRAY_SIZE(dai_tokens_new), private->array, + le32_to_cpu(private->size), &dai->tuples, + &dai->num_tuples); if (ret != 0) { dev_err(scomp->dev, "error: parse dai tokens failed %d\n", le32_to_cpu(private->size)); - goto finish; + return ret; } - ret = sof_parse_tokens(scomp, &comp_dai->config, comp_tokens, - ARRAY_SIZE(comp_tokens), private->array, - le32_to_cpu(private->size)); + ret = sof_parse_tokens_new(scomp, NULL, comp_tokens_new, + ARRAY_SIZE(comp_tokens_new), private->array, + le32_to_cpu(private->size), &dai->tuples, + &dai->num_tuples); if (ret != 0) { dev_err(scomp->dev, "error: parse dai.cfg tokens failed %d\n", private->size); - goto finish; + return ret; } - dev_dbg(scomp->dev, "dai %s: type %d index %d\n", - swidget->widget->name, comp_dai->type, comp_dai->dai_index); - sof_dbg_comp_config(scomp, &comp_dai->config); - - if (dai) { + if (dai) dai->scomp = scomp; - /* - * copy only the sof_ipc_comp_dai to avoid collapsing - * the snd_sof_dai, the extended data is kept in the - * snd_sof_widget. - */ - memcpy(&dai->comp_dai, comp_dai, sizeof(*comp_dai)); - } - -finish: - kfree(comp_dai); return ret; } @@ -1598,47 +1856,25 @@ static int sof_widget_load_pcm(struct snd_soc_component *scomp, int index, struct snd_soc_tplg_dapm_widget *tw) { struct snd_soc_tplg_private *private = &tw->priv; - struct sof_ipc_comp_host *host; - size_t ipc_size = sizeof(*host); int ret; - host = (struct sof_ipc_comp_host *) - sof_comp_alloc(swidget, &ipc_size, index); - if (!host) - return -ENOMEM; - - /* configure host comp IPC message */ - host->comp.type = SOF_COMP_HOST; - host->direction = dir; - host->config.hdr.size = sizeof(host->config); - - ret = sof_parse_tokens(scomp, host, pcm_tokens, - ARRAY_SIZE(pcm_tokens), private->array, - le32_to_cpu(private->size)); + ret = sof_parse_tokens_new(scomp, NULL, pcm_tokens, ARRAY_SIZE(pcm_tokens), private->array, + le32_to_cpu(private->size), &swidget->tuples, + &swidget->num_tuples); if (ret != 0) { - dev_err(scomp->dev, "error: parse host tokens failed %d\n", - private->size); - goto err; + dev_err(scomp->dev, "parsing host tokens failed %d\n", ret); + return ret; } - ret = sof_parse_tokens(scomp, &host->config, comp_tokens, - ARRAY_SIZE(comp_tokens), private->array, - le32_to_cpu(private->size)); + ret = sof_parse_tokens_new(scomp, NULL, comp_tokens_new, ARRAY_SIZE(comp_tokens_new), + private->array, le32_to_cpu(private->size), &swidget->tuples, + &swidget->num_tuples); if (ret != 0) { - dev_err(scomp->dev, "error: parse host.cfg tokens failed %d\n", - le32_to_cpu(private->size)); - goto err; + dev_err(scomp->dev, "parsing host comp tokens failed %d\n", ret); + return ret; } - dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name); - sof_dbg_comp_config(scomp, &host->config); - - swidget->private = host; - return 0; -err: - kfree(host); - return ret; } /* @@ -1649,70 +1885,32 @@ static int sof_widget_load_pipeline(struct snd_soc_component *scomp, int index, struct snd_soc_tplg_dapm_widget *tw) { struct snd_soc_tplg_private *private = &tw->priv; - struct sof_ipc_pipe_new *pipeline; - struct snd_sof_widget *comp_swidget; int ret; - pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL); - if (!pipeline) - return -ENOMEM; - - /* configure dai IPC message */ - pipeline->hdr.size = sizeof(*pipeline); - pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW; - pipeline->pipeline_id = index; - pipeline->comp_id = swidget->comp_id; - - /* component at start of pipeline is our stream id */ - comp_swidget = snd_sof_find_swidget(scomp, tw->sname); - if (!comp_swidget) { - dev_err(scomp->dev, "error: widget %s refers to non existent widget %s\n", - tw->name, tw->sname); - ret = -EINVAL; - goto err; - } - - pipeline->sched_id = comp_swidget->comp_id; + memcpy(swidget->sname, tw->sname, sizeof(tw->sname)); - dev_dbg(scomp->dev, "tplg: pipeline id %d comp %d scheduling comp id %d\n", - pipeline->pipeline_id, pipeline->comp_id, pipeline->sched_id); - - ret = sof_parse_tokens(scomp, pipeline, sched_tokens, - ARRAY_SIZE(sched_tokens), private->array, - le32_to_cpu(private->size)); + ret = sof_parse_tokens_new(scomp, NULL, sched_tokens, + ARRAY_SIZE(sched_tokens), private->array, + le32_to_cpu(private->size), &swidget->tuples, + &swidget->num_tuples); if (ret != 0) { dev_err(scomp->dev, "error: parse pipeline tokens failed %d\n", private->size); - goto err; + return ret; } - ret = sof_parse_tokens(scomp, swidget, pipeline_tokens, - ARRAY_SIZE(pipeline_tokens), private->array, - le32_to_cpu(private->size)); + ret = sof_parse_tokens_new(scomp, NULL, pipeline_tokens, + ARRAY_SIZE(pipeline_tokens), private->array, + le32_to_cpu(private->size), &swidget->tuples, + &swidget->num_tuples); if (ret != 0) { dev_err(scomp->dev, "error: parse dynamic pipeline token failed %d\n", private->size); - goto err; + return ret; } - if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) - pipeline->core = SOF_DSP_PRIMARY_CORE; - - if (sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_OVERRIDE)) - swidget->dynamic_pipeline_widget = - sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_ENABLE); - - dev_dbg(scomp->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d dynamic %d\n", - swidget->widget->name, pipeline->period, pipeline->priority, - pipeline->period_mips, pipeline->core, pipeline->frames_per_sched, - swidget->dynamic_pipeline_widget); - - swidget->private = pipeline; return 0; -err: - kfree(pipeline); - return ret; } /* @@ -1724,33 +1922,18 @@ static int sof_widget_load_mixer(struct snd_soc_component *scomp, int index, struct snd_soc_tplg_dapm_widget *tw) { struct snd_soc_tplg_private *private = &tw->priv; - struct sof_ipc_comp_mixer *mixer; - size_t ipc_size = sizeof(*mixer); int ret; - mixer = (struct sof_ipc_comp_mixer *) - sof_comp_alloc(swidget, &ipc_size, index); - if (!mixer) - return -ENOMEM; - - /* configure mixer IPC message */ - mixer->comp.type = SOF_COMP_MIXER; - mixer->config.hdr.size = sizeof(mixer->config); - - ret = sof_parse_tokens(scomp, &mixer->config, comp_tokens, - ARRAY_SIZE(comp_tokens), private->array, - le32_to_cpu(private->size)); + ret = sof_parse_tokens_new(scomp, NULL, comp_tokens, + ARRAY_SIZE(comp_tokens), private->array, + le32_to_cpu(private->size), &swidget->tuples, + &swidget->num_tuples); if (ret != 0) { dev_err(scomp->dev, "error: parse mixer.cfg tokens failed %d\n", private->size); - kfree(mixer); return ret; } - sof_dbg_comp_config(scomp, &mixer->config); - - swidget->private = mixer; - return 0; } @@ -1800,68 +1983,36 @@ static int sof_widget_load_pga(struct snd_soc_component *scomp, int index, struct snd_sof_widget *swidget, struct snd_soc_tplg_dapm_widget *tw) { - struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); struct snd_soc_tplg_private *private = &tw->priv; - struct sof_ipc_comp_volume *volume; - struct snd_sof_control *scontrol; - size_t ipc_size = sizeof(*volume); - int min_step; - int max_step; int ret; - volume = (struct sof_ipc_comp_volume *) - sof_comp_alloc(swidget, &ipc_size, index); - if (!volume) - return -ENOMEM; - if (!le32_to_cpu(tw->num_kcontrols)) { dev_err(scomp->dev, "error: invalid kcontrol count %d for volume\n", tw->num_kcontrols); - ret = -EINVAL; - goto err; + return -EINVAL; } - /* configure volume IPC message */ - volume->comp.type = SOF_COMP_VOLUME; - volume->config.hdr.size = sizeof(volume->config); - - ret = sof_parse_tokens(scomp, volume, volume_tokens, - ARRAY_SIZE(volume_tokens), private->array, - le32_to_cpu(private->size)); + ret = sof_parse_tokens_new(scomp, NULL, volume_tokens, + ARRAY_SIZE(volume_tokens), private->array, + le32_to_cpu(private->size), &swidget->tuples, + &swidget->num_tuples); if (ret != 0) { dev_err(scomp->dev, "error: parse volume tokens failed %d\n", private->size); - goto err; + return ret; } - ret = sof_parse_tokens(scomp, &volume->config, comp_tokens, - ARRAY_SIZE(comp_tokens), private->array, - le32_to_cpu(private->size)); + ret = sof_parse_tokens_new(scomp, NULL, comp_tokens_new, + ARRAY_SIZE(comp_tokens_new), private->array, + le32_to_cpu(private->size), &swidget->tuples, + &swidget->num_tuples); if (ret != 0) { dev_err(scomp->dev, "error: parse volume.cfg tokens failed %d\n", le32_to_cpu(private->size)); - goto err; + return ret; } - sof_dbg_comp_config(scomp, &volume->config); - - swidget->private = volume; - - list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { - if (scontrol->comp_id == swidget->comp_id && - scontrol->volume_table) { - min_step = scontrol->min_volume_step; - max_step = scontrol->max_volume_step; - volume->min_value = scontrol->volume_table[min_step]; - volume->max_value = scontrol->volume_table[max_step]; - volume->channels = scontrol->num_channels; - break; - } - } return 0; -err: - kfree(volume); - return ret; } /* @@ -2329,6 +2480,7 @@ static int sof_widget_ready(struct snd_soc_component *scomp, int index, } list_add(&dai->list, &sdev->dai_list); swidget->private = dai; + sof_widget_update_ipc_comp_dai(scomp, swidget); break; case snd_soc_dapm_mixer: ret = sof_widget_load_mixer(scomp, index, swidget, tw); @@ -3521,6 +3673,27 @@ static int sof_complete(struct snd_soc_component *scomp) struct snd_sof_widget *swidget, *comp_swidget; int ret; + /* now update all ipc based on the widget tuple array for host components */ + list_for_each_entry(swidget, &sdev->widget_list, list) { + switch (swidget->id) { + case snd_soc_dapm_aif_in: + case snd_soc_dapm_aif_out: + sof_widget_update_ipc_comp_host(scomp, swidget); + break; + case snd_soc_dapm_scheduler: + sof_widget_update_ipc_comp_pipeline(scomp, swidget); + break; + case snd_soc_dapm_mixer: + sof_widget_update_ipc_comp_mixer(scomp, swidget); + break; + case snd_soc_dapm_pga: + sof_widget_update_ipc_comp_pga(scomp, swidget); + break; + default: + break; + } + } + /* set the pipe_widget and apply the dynamic_pipeline_widget_flag */ list_for_each_entry(swidget, &sdev->widget_list, list) { switch (swidget->id) {