#include "ipc3-priv.h"
 
 /* IPC set()/get() for kcontrols. */
-static int sof_ipc3_set_get_kcontrol_data(struct snd_sof_control *scontrol, bool set)
+static int sof_ipc3_set_get_kcontrol_data(struct snd_sof_control *scontrol,
+                                         bool set, bool lock)
 {
        struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scontrol->scomp);
        struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
        struct snd_sof_widget *swidget;
        bool widget_found = false;
        u32 ipc_cmd, msg_bytes;
+       int ret = 0;
 
        list_for_each_entry(swidget, &sdev->widget_list, list) {
                if (swidget->comp_id == scontrol->comp_id) {
                return -EINVAL;
        }
 
+       if (lock)
+               mutex_lock(&swidget->setup_mutex);
+       else
+               lockdep_assert_held(&swidget->setup_mutex);
+
        /*
-        * Volatile controls should always be part of static pipelines and the widget use_count
-        * would always be > 0 in this case. For the others, just return the cached value if the
-        * widget is not set up.
+        * Volatile controls should always be part of static pipelines and the
+        * widget use_count would always be > 0 in this case. For the others,
+        * just return the cached value if the widget is not set up.
         */
        if (!swidget->use_count)
-               return 0;
+               goto unlock;
 
        /*
         * Select the IPC cmd and the ctrl_type based on the ctrl_cmd and the
                             sizeof(struct sof_abi_hdr);
                break;
        default:
-               return -EINVAL;
+               ret = -EINVAL;
+               goto unlock;
        }
 
        cdata->rhdr.hdr.size = msg_bytes;
        cdata->elems_remaining = 0;
 
-       return iops->set_get_data(sdev, cdata, cdata->rhdr.hdr.size, set);
+       ret = iops->set_get_data(sdev, cdata, cdata->rhdr.hdr.size, set);
+
+unlock:
+       if (lock)
+               mutex_unlock(&swidget->setup_mutex);
+
+       return ret;
 }
 
 static void snd_sof_refresh_control(struct snd_sof_control *scontrol)
 
        /* refresh the component data from DSP */
        scontrol->comp_data_dirty = false;
-       ret = sof_ipc3_set_get_kcontrol_data(scontrol, false);
+       ret = sof_ipc3_set_get_kcontrol_data(scontrol, false, true);
        if (ret < 0) {
                dev_err(scomp->dev, "Failed to get control data: %d\n", ret);
 
 
        /* notify DSP of mixer updates */
        if (pm_runtime_active(scomp->dev)) {
-               int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true);
+               int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
 
                if (ret < 0) {
                        dev_err(scomp->dev, "Failed to set mixer updates for %s\n",
 
        /* notify DSP of mixer updates */
        if (pm_runtime_active(scomp->dev)) {
-               int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true);
+               int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
 
                if (ret < 0) {
                        dev_err(scomp->dev, "Failed to set mixer updates for %s\n",
 
        /* notify DSP of enum updates */
        if (pm_runtime_active(scomp->dev)) {
-               int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true);
+               int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
 
                if (ret < 0) {
                        dev_err(scomp->dev, "Failed to set enum updates for %s\n",
 
        /* notify DSP of byte control updates */
        if (pm_runtime_active(scomp->dev))
-               return sof_ipc3_set_get_kcontrol_data(scontrol, true);
+               return sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
 
        return 0;
 }
 
        /* notify DSP of byte control updates */
        if (pm_runtime_active(scomp->dev))
-               return sof_ipc3_set_get_kcontrol_data(scontrol, true);
+               return sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
 
        return 0;
 }
        cdata->data->abi = SOF_ABI_VERSION;
 
        /* get all the component data from DSP */
-       ret = sof_ipc3_set_get_kcontrol_data(scontrol, false);
+       ret = sof_ipc3_set_get_kcontrol_data(scontrol, false, true);
        if (ret < 0)
                return ret;
 
        list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
                if (scontrol->comp_id == swidget->comp_id) {
                        /* set kcontrol data in DSP */
-                       ret = sof_ipc3_set_get_kcontrol_data(scontrol, true);
+                       ret = sof_ipc3_set_get_kcontrol_data(scontrol, true, false);
                        if (ret < 0) {
                                dev_err(sdev->dev,
                                        "kcontrol %d set up failed for widget %s\n",
                        if (swidget->dynamic_pipeline_widget)
                                continue;
 
-                       ret = sof_ipc3_set_get_kcontrol_data(scontrol, false);
+                       ret = sof_ipc3_set_get_kcontrol_data(scontrol, false, false);
                        if (ret < 0)
                                dev_warn(sdev->dev,
                                         "kcontrol %d read failed for widget %s\n",
 
                /* Do not free widgets for static pipelines with FW older than SOF2.2 */
                if (!verify && !swidget->dynamic_pipeline_widget &&
                    SOF_FW_VER(v->major, v->minor, v->micro) < SOF_FW_VER(2, 2, 0)) {
+                       mutex_lock(&swidget->setup_mutex);
                        swidget->use_count = 0;
+                       mutex_unlock(&swidget->setup_mutex);
                        if (swidget->spipe)
                                swidget->spipe->complete = 0;
                        continue;
 
 #include "ipc4-priv.h"
 #include "ipc4-topology.h"
 
-static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol, bool set)
+static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol,
+                                         bool set, bool lock)
 {
        struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
        struct snd_soc_component *scomp = scontrol->scomp;
        struct sof_ipc4_msg *msg = &cdata->msg;
        struct snd_sof_widget *swidget;
        bool widget_found = false;
+       int ret = 0;
 
        /* find widget associated with the control */
        list_for_each_entry(swidget, &sdev->widget_list, list) {
                return -ENOENT;
        }
 
+       if (lock)
+               mutex_lock(&swidget->setup_mutex);
+       else
+               lockdep_assert_held(&swidget->setup_mutex);
+
        /*
-        * Volatile controls should always be part of static pipelines and the widget use_count
-        * would always be > 0 in this case. For the others, just return the cached value if the
-        * widget is not set up.
+        * Volatile controls should always be part of static pipelines and the
+        * widget use_count would always be > 0 in this case. For the others,
+        * just return the cached value if the widget is not set up.
         */
        if (!swidget->use_count)
-               return 0;
+               goto unlock;
 
        msg->primary &= ~SOF_IPC4_MOD_INSTANCE_MASK;
        msg->primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id);
 
-       return iops->set_get_data(sdev, msg, msg->data_size, set);
+       ret = iops->set_get_data(sdev, msg, msg->data_size, set);
+
+unlock:
+       if (lock)
+               mutex_unlock(&swidget->setup_mutex);
+
+       return ret;
 }
 
 static int
 sof_ipc4_set_volume_data(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
-                        struct snd_sof_control *scontrol)
+                        struct snd_sof_control *scontrol, bool lock)
 {
        struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
        struct sof_ipc4_gain *gain = swidget->private;
                msg->data_ptr = &data;
                msg->data_size = sizeof(data);
 
-               ret = sof_ipc4_set_get_kcontrol_data(scontrol, true);
+               ret = sof_ipc4_set_get_kcontrol_data(scontrol, true, lock);
                msg->data_ptr = NULL;
                msg->data_size = 0;
                if (ret < 0) {
                return false;
        }
 
-       ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol);
+       ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol, true);
        if (ret < 0)
                return false;
 
 
        list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
                if (scontrol->comp_id == swidget->comp_id) {
-                       ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol);
+                       ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol, false);
                        if (ret < 0) {
                                dev_err(sdev->dev, "%s: kcontrol %d set up failed for widget %s\n",
                                        __func__, scontrol->comp_id, swidget->widget->name);
 
                }
 }
 
-int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
+static int sof_widget_free_unlocked(struct snd_sof_dev *sdev,
+                                   struct snd_sof_widget *swidget)
 {
        const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
        struct snd_sof_widget *pipe_widget;
         * skip for static pipelines
         */
        if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
-               ret = sof_widget_free(sdev, pipe_widget);
+               ret = sof_widget_free_unlocked(sdev, pipe_widget);
                if (ret < 0 && !err)
                        err = ret;
        }
 
        return err;
 }
+
+int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
+{
+       int ret;
+
+       mutex_lock(&swidget->setup_mutex);
+       ret = sof_widget_free_unlocked(sdev, swidget);
+       mutex_unlock(&swidget->setup_mutex);
+
+       return ret;
+}
 EXPORT_SYMBOL(sof_widget_free);
 
-int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
+static int sof_widget_setup_unlocked(struct snd_sof_dev *sdev,
+                                    struct snd_sof_widget *swidget)
 {
        const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
        bool use_count_decremented = false;
                        goto use_count_dec;
                }
 
-               ret = sof_widget_setup(sdev, swidget->spipe->pipe_widget);
+               ret = sof_widget_setup_unlocked(sdev, swidget->spipe->pipe_widget);
                if (ret < 0)
                        goto use_count_dec;
        }
 
 widget_free:
        /* widget use_count and core ref_count will both be decremented by sof_widget_free() */
-       sof_widget_free(sdev, swidget);
+       sof_widget_free_unlocked(sdev, swidget);
        use_count_decremented = true;
 core_put:
        snd_sof_dsp_core_put(sdev, swidget->core);
 pipe_widget_free:
        if (swidget->id != snd_soc_dapm_scheduler)
-               sof_widget_free(sdev, swidget->spipe->pipe_widget);
+               sof_widget_free_unlocked(sdev, swidget->spipe->pipe_widget);
 use_count_dec:
        if (!use_count_decremented)
                swidget->use_count--;
 
        return ret;
 }
+
+int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
+{
+       int ret;
+
+       mutex_lock(&swidget->setup_mutex);
+       ret = sof_widget_setup_unlocked(sdev, swidget);
+       mutex_unlock(&swidget->setup_mutex);
+
+       return ret;
+}
 EXPORT_SYMBOL(sof_widget_setup);
 
 int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource,
 
         * up in the DSP.
         */
        bool prepared;
-       int use_count; /* use_count will be protected by the PCM mutex held by the core */
+
+       struct mutex setup_mutex; /* to protect the swidget setup and free operations */
+
+       /*
+        * use_count is protected by the PCM mutex held by the core and the
+        * setup_mutex against non stream domain races (kcontrol access for
+        * example)
+        */
+       int use_count;
+
        int core;
        int id; /* id is the DAPM widget type */
        /*
 
        swidget->id = w->id;
        swidget->pipeline_id = index;
        swidget->private = NULL;
+       mutex_init(&swidget->setup_mutex);
+
        ida_init(&swidget->src_queue_ida);
        ida_init(&swidget->sink_queue_ida);