1 // SPDX-License-Identifier: GPL-2.0-only
3 * skl-message.c - HDA DSP interface for FW registration, Pipe and Module
6 * Copyright (C) 2015 Intel Corp
7 * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
8 * Jeeja KP <jeeja.kp@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 #include <linux/slab.h>
13 #include <linux/pci.h>
14 #include <sound/core.h>
15 #include <sound/pcm.h>
16 #include <uapi/sound/skl-tplg-interface.h>
17 #include "skl-sst-dsp.h"
18 #include "cnl-sst-dsp.h"
19 #include "skl-sst-ipc.h"
21 #include "../common/sst-dsp.h"
22 #include "../common/sst-dsp-priv.h"
23 #include "skl-topology.h"
25 static int skl_alloc_dma_buf(struct device *dev,
26 struct snd_dma_buffer *dmab, size_t size)
28 return snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, dmab);
31 static int skl_free_dma_buf(struct device *dev, struct snd_dma_buffer *dmab)
33 snd_dma_free_pages(dmab);
37 #define SKL_ASTATE_PARAM_ID 4
39 void skl_dsp_set_astate_cfg(struct skl_dev *skl, u32 cnt, void *data)
41 struct skl_ipc_large_config_msg msg = {0};
43 msg.large_param_id = SKL_ASTATE_PARAM_ID;
44 msg.param_data_size = (cnt * sizeof(struct skl_astate_param) +
47 skl_ipc_set_large_config(&skl->ipc, &msg, data);
50 static int skl_dsp_setup_spib(struct device *dev, unsigned int size,
51 int stream_tag, int enable)
53 struct hdac_bus *bus = dev_get_drvdata(dev);
54 struct hdac_stream *stream = snd_hdac_get_stream(bus,
55 SNDRV_PCM_STREAM_PLAYBACK, stream_tag);
60 /* enable/disable SPIB for this hdac stream */
61 snd_hdac_stream_spbcap_enable(bus, enable, stream->index);
63 /* set the spib value */
64 snd_hdac_stream_set_spib(bus, stream, size);
69 static int skl_dsp_prepare(struct device *dev, unsigned int format,
70 unsigned int size, struct snd_dma_buffer *dmab)
72 struct hdac_bus *bus = dev_get_drvdata(dev);
73 struct hdac_ext_stream *estream;
74 struct hdac_stream *stream;
75 struct snd_pcm_substream substream;
81 memset(&substream, 0, sizeof(substream));
82 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
84 estream = snd_hdac_ext_stream_assign(bus, &substream,
85 HDAC_EXT_STREAM_TYPE_HOST);
89 stream = hdac_stream(estream);
91 /* assign decouple host dma channel */
92 ret = snd_hdac_dsp_prepare(stream, format, size, dmab);
96 skl_dsp_setup_spib(dev, size, stream->stream_tag, true);
98 return stream->stream_tag;
101 static int skl_dsp_trigger(struct device *dev, bool start, int stream_tag)
103 struct hdac_bus *bus = dev_get_drvdata(dev);
104 struct hdac_stream *stream;
109 stream = snd_hdac_get_stream(bus,
110 SNDRV_PCM_STREAM_PLAYBACK, stream_tag);
114 snd_hdac_dsp_trigger(stream, start);
119 static int skl_dsp_cleanup(struct device *dev,
120 struct snd_dma_buffer *dmab, int stream_tag)
122 struct hdac_bus *bus = dev_get_drvdata(dev);
123 struct hdac_stream *stream;
124 struct hdac_ext_stream *estream;
129 stream = snd_hdac_get_stream(bus,
130 SNDRV_PCM_STREAM_PLAYBACK, stream_tag);
134 estream = stream_to_hdac_ext_stream(stream);
135 skl_dsp_setup_spib(dev, 0, stream_tag, false);
136 snd_hdac_ext_stream_release(estream, HDAC_EXT_STREAM_TYPE_HOST);
138 snd_hdac_dsp_cleanup(stream, dmab);
143 static struct skl_dsp_loader_ops skl_get_loader_ops(void)
145 struct skl_dsp_loader_ops loader_ops;
147 memset(&loader_ops, 0, sizeof(struct skl_dsp_loader_ops));
149 loader_ops.alloc_dma_buf = skl_alloc_dma_buf;
150 loader_ops.free_dma_buf = skl_free_dma_buf;
155 static struct skl_dsp_loader_ops bxt_get_loader_ops(void)
157 struct skl_dsp_loader_ops loader_ops;
159 memset(&loader_ops, 0, sizeof(loader_ops));
161 loader_ops.alloc_dma_buf = skl_alloc_dma_buf;
162 loader_ops.free_dma_buf = skl_free_dma_buf;
163 loader_ops.prepare = skl_dsp_prepare;
164 loader_ops.trigger = skl_dsp_trigger;
165 loader_ops.cleanup = skl_dsp_cleanup;
170 static const struct skl_dsp_ops dsp_ops[] = {
174 .loader_ops = skl_get_loader_ops,
175 .init = skl_sst_dsp_init,
176 .init_fw = skl_sst_init_fw,
177 .cleanup = skl_sst_dsp_cleanup
182 .loader_ops = skl_get_loader_ops,
183 .init = skl_sst_dsp_init,
184 .init_fw = skl_sst_init_fw,
185 .cleanup = skl_sst_dsp_cleanup
190 .loader_ops = bxt_get_loader_ops,
191 .init = bxt_sst_dsp_init,
192 .init_fw = bxt_sst_init_fw,
193 .cleanup = bxt_sst_dsp_cleanup
198 .loader_ops = bxt_get_loader_ops,
199 .init = bxt_sst_dsp_init,
200 .init_fw = bxt_sst_init_fw,
201 .cleanup = bxt_sst_dsp_cleanup
206 .loader_ops = bxt_get_loader_ops,
207 .init = cnl_sst_dsp_init,
208 .init_fw = cnl_sst_init_fw,
209 .cleanup = cnl_sst_dsp_cleanup
214 .loader_ops = bxt_get_loader_ops,
215 .init = cnl_sst_dsp_init,
216 .init_fw = cnl_sst_init_fw,
217 .cleanup = cnl_sst_dsp_cleanup
222 .loader_ops = bxt_get_loader_ops,
223 .init = cnl_sst_dsp_init,
224 .init_fw = cnl_sst_init_fw,
225 .cleanup = cnl_sst_dsp_cleanup
230 .loader_ops = bxt_get_loader_ops,
231 .init = cnl_sst_dsp_init,
232 .init_fw = cnl_sst_init_fw,
233 .cleanup = cnl_sst_dsp_cleanup
237 const struct skl_dsp_ops *skl_get_dsp_ops(int pci_id)
241 for (i = 0; i < ARRAY_SIZE(dsp_ops); i++) {
242 if (dsp_ops[i].id == pci_id)
249 int skl_init_dsp(struct skl_dev *skl)
251 void __iomem *mmio_base;
252 struct hdac_bus *bus = skl_to_bus(skl);
253 struct skl_dsp_loader_ops loader_ops;
255 const struct skl_dsp_ops *ops;
256 struct skl_dsp_cores *cores;
259 /* enable ppcap interrupt */
260 snd_hdac_ext_bus_ppcap_enable(bus, true);
261 snd_hdac_ext_bus_ppcap_int_enable(bus, true);
263 /* read the BAR of the ADSP MMIO */
264 mmio_base = pci_ioremap_bar(skl->pci, 4);
265 if (mmio_base == NULL) {
266 dev_err(bus->dev, "ioremap error\n");
270 ops = skl_get_dsp_ops(skl->pci->device);
276 loader_ops = ops->loader_ops();
277 ret = ops->init(bus->dev, mmio_base, irq,
278 skl->fw_name, loader_ops,
286 cores->count = ops->num_cores;
288 cores->state = kcalloc(cores->count, sizeof(*cores->state), GFP_KERNEL);
294 cores->usage_count = kcalloc(cores->count, sizeof(*cores->usage_count),
296 if (!cores->usage_count) {
298 goto free_core_state;
301 dev_dbg(bus->dev, "dsp registration status=%d\n", ret);
314 int skl_free_dsp(struct skl_dev *skl)
316 struct hdac_bus *bus = skl_to_bus(skl);
318 /* disable ppcap interrupt */
319 snd_hdac_ext_bus_ppcap_int_enable(bus, false);
321 skl->dsp_ops->cleanup(bus->dev, skl);
323 kfree(skl->cores.state);
324 kfree(skl->cores.usage_count);
326 if (skl->dsp->addr.lpe)
327 iounmap(skl->dsp->addr.lpe);
333 * In the case of "suspend_active" i.e, the Audio IP being active
334 * during system suspend, immediately excecute any pending D0i3 work
335 * before suspending. This is needed for the IP to work in low power
336 * mode during system suspend. In the case of normal suspend, cancel
337 * any pending D0i3 work.
339 int skl_suspend_late_dsp(struct skl_dev *skl)
341 struct delayed_work *dwork;
346 dwork = &skl->d0i3.work;
348 if (dwork->work.func) {
349 if (skl->supend_active)
350 flush_delayed_work(dwork);
352 cancel_delayed_work_sync(dwork);
358 int skl_suspend_dsp(struct skl_dev *skl)
360 struct hdac_bus *bus = skl_to_bus(skl);
363 /* if ppcap is not supported return 0 */
367 ret = skl_dsp_sleep(skl->dsp);
371 /* disable ppcap interrupt */
372 snd_hdac_ext_bus_ppcap_int_enable(bus, false);
373 snd_hdac_ext_bus_ppcap_enable(bus, false);
378 int skl_resume_dsp(struct skl_dev *skl)
380 struct hdac_bus *bus = skl_to_bus(skl);
383 /* if ppcap is not supported return 0 */
387 /* enable ppcap interrupt */
388 snd_hdac_ext_bus_ppcap_enable(bus, true);
389 snd_hdac_ext_bus_ppcap_int_enable(bus, true);
391 /* check if DSP 1st boot is done */
392 if (skl->is_first_boot)
396 * Disable dynamic clock and power gating during firmware
397 * and library download
399 skl->enable_miscbdcge(skl->dev, false);
400 skl->clock_power_gating(skl->dev, false);
402 ret = skl_dsp_wake(skl->dsp);
403 skl->enable_miscbdcge(skl->dev, true);
404 skl->clock_power_gating(skl->dev, true);
408 if (skl->cfg.astate_cfg != NULL) {
409 skl_dsp_set_astate_cfg(skl, skl->cfg.astate_cfg->count,
410 skl->cfg.astate_cfg);
415 enum skl_bitdepth skl_get_bit_depth(int params)
419 return SKL_DEPTH_8BIT;
422 return SKL_DEPTH_16BIT;
425 return SKL_DEPTH_24BIT;
428 return SKL_DEPTH_32BIT;
431 return SKL_DEPTH_INVALID;
437 * Each module in DSP expects a base module configuration, which consists of
438 * PCM format information, which we calculate in driver and resource values
439 * which are read from widget information passed through topology binary
440 * This is send when we create a module with INIT_INSTANCE IPC msg
442 static void skl_set_base_module_format(struct skl_dev *skl,
443 struct skl_module_cfg *mconfig,
444 struct skl_base_cfg *base_cfg)
446 struct skl_module *module = mconfig->module;
447 struct skl_module_res *res = &module->resources[mconfig->res_idx];
448 struct skl_module_iface *fmt = &module->formats[mconfig->fmt_idx];
449 struct skl_module_fmt *format = &fmt->inputs[0].fmt;
451 base_cfg->audio_fmt.number_of_channels = format->channels;
453 base_cfg->audio_fmt.s_freq = format->s_freq;
454 base_cfg->audio_fmt.bit_depth = format->bit_depth;
455 base_cfg->audio_fmt.valid_bit_depth = format->valid_bit_depth;
456 base_cfg->audio_fmt.ch_cfg = format->ch_cfg;
457 base_cfg->audio_fmt.sample_type = format->sample_type;
459 dev_dbg(skl->dev, "bit_depth=%x valid_bd=%x ch_config=%x\n",
460 format->bit_depth, format->valid_bit_depth,
463 base_cfg->audio_fmt.channel_map = format->ch_map;
465 base_cfg->audio_fmt.interleaving = format->interleaving_style;
467 base_cfg->cpc = res->cpc;
468 base_cfg->ibs = res->ibs;
469 base_cfg->obs = res->obs;
470 base_cfg->is_pages = res->is_pages;
473 static void fill_pin_params(struct skl_audio_data_format *pin_fmt,
474 struct skl_module_fmt *format)
476 pin_fmt->number_of_channels = format->channels;
477 pin_fmt->s_freq = format->s_freq;
478 pin_fmt->bit_depth = format->bit_depth;
479 pin_fmt->valid_bit_depth = format->valid_bit_depth;
480 pin_fmt->ch_cfg = format->ch_cfg;
481 pin_fmt->sample_type = format->sample_type;
482 pin_fmt->channel_map = format->ch_map;
483 pin_fmt->interleaving = format->interleaving_style;
487 * Any module configuration begins with a base module configuration but
488 * can be followed by a generic extension containing audio format for all
489 * module's pins that are in use.
491 static void skl_set_base_ext_module_format(struct skl_dev *skl,
492 struct skl_module_cfg *mconfig,
493 struct skl_base_cfg_ext *base_cfg_ext)
495 struct skl_module *module = mconfig->module;
496 struct skl_module_pin_resources *pin_res;
497 struct skl_module_iface *fmt = &module->formats[mconfig->fmt_idx];
498 struct skl_module_res *res = &module->resources[mconfig->res_idx];
499 struct skl_module_fmt *format;
500 struct skl_pin_format *pin_fmt;
504 base_cfg_ext->nr_input_pins = res->nr_input_pins;
505 base_cfg_ext->nr_output_pins = res->nr_output_pins;
506 base_cfg_ext->priv_param_length =
507 mconfig->formats_config[SKL_PARAM_INIT].caps_size;
509 for (i = 0; i < res->nr_input_pins; i++) {
510 pin_res = &res->input[i];
511 pin_fmt = &base_cfg_ext->pins_fmt[i];
513 pin_fmt->pin_idx = pin_res->pin_index;
514 pin_fmt->buf_size = pin_res->buf_size;
516 format = &fmt->inputs[pin_res->pin_index].fmt;
517 fill_pin_params(&pin_fmt->audio_fmt, format);
520 for (i = 0; i < res->nr_output_pins; i++) {
521 pin_res = &res->output[i];
522 pin_fmt = &base_cfg_ext->pins_fmt[res->nr_input_pins + i];
524 pin_fmt->pin_idx = pin_res->pin_index;
525 pin_fmt->buf_size = pin_res->buf_size;
527 format = &fmt->outputs[pin_res->pin_index].fmt;
528 fill_pin_params(&pin_fmt->audio_fmt, format);
531 if (!base_cfg_ext->priv_param_length)
534 params = (char *)base_cfg_ext + sizeof(struct skl_base_cfg_ext);
535 params += (base_cfg_ext->nr_input_pins + base_cfg_ext->nr_output_pins) *
536 sizeof(struct skl_pin_format);
538 memcpy(params, mconfig->formats_config[SKL_PARAM_INIT].caps,
539 mconfig->formats_config[SKL_PARAM_INIT].caps_size);
543 * Copies copier capabilities into copier module and updates copier module
546 static void skl_copy_copier_caps(struct skl_module_cfg *mconfig,
547 struct skl_cpr_cfg *cpr_mconfig)
549 if (mconfig->formats_config[SKL_PARAM_INIT].caps_size == 0)
552 memcpy(&cpr_mconfig->gtw_cfg.config_data,
553 mconfig->formats_config[SKL_PARAM_INIT].caps,
554 mconfig->formats_config[SKL_PARAM_INIT].caps_size);
556 cpr_mconfig->gtw_cfg.config_length =
557 (mconfig->formats_config[SKL_PARAM_INIT].caps_size) / 4;
560 #define SKL_NON_GATEWAY_CPR_NODE_ID 0xFFFFFFFF
562 * Calculate the gatewat settings required for copier module, type of
563 * gateway and index of gateway to use
565 static u32 skl_get_node_id(struct skl_dev *skl,
566 struct skl_module_cfg *mconfig)
568 union skl_connector_node_id node_id = {0};
569 union skl_ssp_dma_node ssp_node = {0};
570 struct skl_pipe_params *params = mconfig->pipe->p_params;
572 switch (mconfig->dev_type) {
574 node_id.node.dma_type =
575 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
576 SKL_DMA_I2S_LINK_OUTPUT_CLASS :
577 SKL_DMA_I2S_LINK_INPUT_CLASS;
578 node_id.node.vindex = params->host_dma_id +
579 (mconfig->vbus_id << 3);
583 node_id.node.dma_type =
584 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
585 SKL_DMA_I2S_LINK_OUTPUT_CLASS :
586 SKL_DMA_I2S_LINK_INPUT_CLASS;
587 ssp_node.dma_node.time_slot_index = mconfig->time_slot;
588 ssp_node.dma_node.i2s_instance = mconfig->vbus_id;
589 node_id.node.vindex = ssp_node.val;
592 case SKL_DEVICE_DMIC:
593 node_id.node.dma_type = SKL_DMA_DMIC_LINK_INPUT_CLASS;
594 node_id.node.vindex = mconfig->vbus_id +
595 (mconfig->time_slot);
598 case SKL_DEVICE_HDALINK:
599 node_id.node.dma_type =
600 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
601 SKL_DMA_HDA_LINK_OUTPUT_CLASS :
602 SKL_DMA_HDA_LINK_INPUT_CLASS;
603 node_id.node.vindex = params->link_dma_id;
606 case SKL_DEVICE_HDAHOST:
607 node_id.node.dma_type =
608 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
609 SKL_DMA_HDA_HOST_OUTPUT_CLASS :
610 SKL_DMA_HDA_HOST_INPUT_CLASS;
611 node_id.node.vindex = params->host_dma_id;
615 node_id.val = 0xFFFFFFFF;
622 static void skl_setup_cpr_gateway_cfg(struct skl_dev *skl,
623 struct skl_module_cfg *mconfig,
624 struct skl_cpr_cfg *cpr_mconfig)
627 struct skl_module_res *res;
628 int res_idx = mconfig->res_idx;
630 cpr_mconfig->gtw_cfg.node_id = skl_get_node_id(skl, mconfig);
632 if (cpr_mconfig->gtw_cfg.node_id == SKL_NON_GATEWAY_CPR_NODE_ID) {
633 cpr_mconfig->cpr_feature_mask = 0;
637 if (skl->nr_modules) {
638 res = &mconfig->module->resources[mconfig->res_idx];
639 cpr_mconfig->gtw_cfg.dma_buffer_size = res->dma_buffer_size;
640 goto skip_buf_size_calc;
642 res = &mconfig->module->resources[res_idx];
645 switch (mconfig->hw_conn_type) {
646 case SKL_CONN_SOURCE:
647 if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
648 dma_io_buf = res->ibs;
650 dma_io_buf = res->obs;
654 if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
655 dma_io_buf = res->obs;
657 dma_io_buf = res->ibs;
661 dev_warn(skl->dev, "wrong connection type: %d\n",
662 mconfig->hw_conn_type);
666 cpr_mconfig->gtw_cfg.dma_buffer_size =
667 mconfig->dma_buffer_size * dma_io_buf;
669 /* fallback to 2ms default value */
670 if (!cpr_mconfig->gtw_cfg.dma_buffer_size) {
671 if (mconfig->hw_conn_type == SKL_CONN_SOURCE)
672 cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * res->obs;
674 cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * res->ibs;
678 cpr_mconfig->cpr_feature_mask = 0;
679 cpr_mconfig->gtw_cfg.config_length = 0;
681 skl_copy_copier_caps(mconfig, cpr_mconfig);
684 #define DMA_CONTROL_ID 5
685 #define DMA_I2S_BLOB_SIZE 21
687 int skl_dsp_set_dma_control(struct skl_dev *skl, u32 *caps,
688 u32 caps_size, u32 node_id)
690 struct skl_dma_control *dma_ctrl;
691 struct skl_ipc_large_config_msg msg = {0};
696 * if blob size zero, then return
701 msg.large_param_id = DMA_CONTROL_ID;
702 msg.param_data_size = sizeof(struct skl_dma_control) + caps_size;
704 dma_ctrl = kzalloc(msg.param_data_size, GFP_KERNEL);
705 if (dma_ctrl == NULL)
708 dma_ctrl->node_id = node_id;
711 * NHLT blob may contain additional configs along with i2s blob.
712 * firmware expects only the i2s blob size as the config_length.
713 * So fix to i2s blob size.
716 dma_ctrl->config_length = DMA_I2S_BLOB_SIZE;
718 memcpy(dma_ctrl->config_data, caps, caps_size);
720 err = skl_ipc_set_large_config(&skl->ipc, &msg, (u32 *)dma_ctrl);
725 EXPORT_SYMBOL_GPL(skl_dsp_set_dma_control);
727 static void skl_setup_out_format(struct skl_dev *skl,
728 struct skl_module_cfg *mconfig,
729 struct skl_audio_data_format *out_fmt)
731 struct skl_module *module = mconfig->module;
732 struct skl_module_iface *fmt = &module->formats[mconfig->fmt_idx];
733 struct skl_module_fmt *format = &fmt->outputs[0].fmt;
735 out_fmt->number_of_channels = (u8)format->channels;
736 out_fmt->s_freq = format->s_freq;
737 out_fmt->bit_depth = format->bit_depth;
738 out_fmt->valid_bit_depth = format->valid_bit_depth;
739 out_fmt->ch_cfg = format->ch_cfg;
741 out_fmt->channel_map = format->ch_map;
742 out_fmt->interleaving = format->interleaving_style;
743 out_fmt->sample_type = format->sample_type;
745 dev_dbg(skl->dev, "copier out format chan=%d fre=%d bitdepth=%d\n",
746 out_fmt->number_of_channels, format->s_freq, format->bit_depth);
750 * DSP needs SRC module for frequency conversion, SRC takes base module
751 * configuration and the target frequency as extra parameter passed as src
754 static void skl_set_src_format(struct skl_dev *skl,
755 struct skl_module_cfg *mconfig,
756 struct skl_src_module_cfg *src_mconfig)
758 struct skl_module *module = mconfig->module;
759 struct skl_module_iface *iface = &module->formats[mconfig->fmt_idx];
760 struct skl_module_fmt *fmt = &iface->outputs[0].fmt;
762 skl_set_base_module_format(skl, mconfig,
763 (struct skl_base_cfg *)src_mconfig);
765 src_mconfig->src_cfg = fmt->s_freq;
769 * DSP needs updown module to do channel conversion. updown module take base
770 * module configuration and channel configuration
771 * It also take coefficients and now we have defaults applied here
773 static void skl_set_updown_mixer_format(struct skl_dev *skl,
774 struct skl_module_cfg *mconfig,
775 struct skl_up_down_mixer_cfg *mixer_mconfig)
777 struct skl_module *module = mconfig->module;
778 struct skl_module_iface *iface = &module->formats[mconfig->fmt_idx];
779 struct skl_module_fmt *fmt = &iface->outputs[0].fmt;
781 skl_set_base_module_format(skl, mconfig,
782 (struct skl_base_cfg *)mixer_mconfig);
783 mixer_mconfig->out_ch_cfg = fmt->ch_cfg;
784 mixer_mconfig->ch_map = fmt->ch_map;
788 * 'copier' is DSP internal module which copies data from Host DMA (HDA host
789 * dma) or link (hda link, SSP, PDM)
790 * Here we calculate the copier module parameters, like PCM format, output
791 * format, gateway settings
792 * copier_module_config is sent as input buffer with INIT_INSTANCE IPC msg
794 static void skl_set_copier_format(struct skl_dev *skl,
795 struct skl_module_cfg *mconfig,
796 struct skl_cpr_cfg *cpr_mconfig)
798 struct skl_audio_data_format *out_fmt = &cpr_mconfig->out_fmt;
799 struct skl_base_cfg *base_cfg = (struct skl_base_cfg *)cpr_mconfig;
801 skl_set_base_module_format(skl, mconfig, base_cfg);
803 skl_setup_out_format(skl, mconfig, out_fmt);
804 skl_setup_cpr_gateway_cfg(skl, mconfig, cpr_mconfig);
808 * Mic select module allows selecting one or many input channels, thus
811 * Mic select module take base module configuration and out-format
814 static void skl_set_base_outfmt_format(struct skl_dev *skl,
815 struct skl_module_cfg *mconfig,
816 struct skl_base_outfmt_cfg *base_outfmt_mcfg)
818 struct skl_audio_data_format *out_fmt = &base_outfmt_mcfg->out_fmt;
819 struct skl_base_cfg *base_cfg =
820 (struct skl_base_cfg *)base_outfmt_mcfg;
822 skl_set_base_module_format(skl, mconfig, base_cfg);
823 skl_setup_out_format(skl, mconfig, out_fmt);
826 static u16 skl_get_module_param_size(struct skl_dev *skl,
827 struct skl_module_cfg *mconfig)
829 struct skl_module_res *res;
830 struct skl_module *module = mconfig->module;
833 switch (mconfig->m_type) {
834 case SKL_MODULE_TYPE_COPIER:
835 param_size = sizeof(struct skl_cpr_cfg);
836 param_size += mconfig->formats_config[SKL_PARAM_INIT].caps_size;
839 case SKL_MODULE_TYPE_SRCINT:
840 return sizeof(struct skl_src_module_cfg);
842 case SKL_MODULE_TYPE_UPDWMIX:
843 return sizeof(struct skl_up_down_mixer_cfg);
845 case SKL_MODULE_TYPE_BASE_OUTFMT:
846 case SKL_MODULE_TYPE_MIC_SELECT:
847 return sizeof(struct skl_base_outfmt_cfg);
849 case SKL_MODULE_TYPE_MIXER:
850 case SKL_MODULE_TYPE_KPB:
851 return sizeof(struct skl_base_cfg);
853 case SKL_MODULE_TYPE_ALGO:
855 res = &module->resources[mconfig->res_idx];
857 param_size = sizeof(struct skl_base_cfg) + sizeof(struct skl_base_cfg_ext);
858 param_size += (res->nr_input_pins + res->nr_output_pins) *
859 sizeof(struct skl_pin_format);
860 param_size += mconfig->formats_config[SKL_PARAM_INIT].caps_size;
869 * DSP firmware supports various modules like copier, SRC, updown etc.
870 * These modules required various parameters to be calculated and sent for
871 * the module initialization to DSP. By default a generic module needs only
872 * base module format configuration
875 static int skl_set_module_format(struct skl_dev *skl,
876 struct skl_module_cfg *module_config,
877 u16 *module_config_size,
882 param_size = skl_get_module_param_size(skl, module_config);
884 *param_data = kzalloc(param_size, GFP_KERNEL);
885 if (NULL == *param_data)
888 *module_config_size = param_size;
890 switch (module_config->m_type) {
891 case SKL_MODULE_TYPE_COPIER:
892 skl_set_copier_format(skl, module_config, *param_data);
895 case SKL_MODULE_TYPE_SRCINT:
896 skl_set_src_format(skl, module_config, *param_data);
899 case SKL_MODULE_TYPE_UPDWMIX:
900 skl_set_updown_mixer_format(skl, module_config, *param_data);
903 case SKL_MODULE_TYPE_BASE_OUTFMT:
904 case SKL_MODULE_TYPE_MIC_SELECT:
905 skl_set_base_outfmt_format(skl, module_config, *param_data);
908 case SKL_MODULE_TYPE_MIXER:
909 case SKL_MODULE_TYPE_KPB:
910 skl_set_base_module_format(skl, module_config, *param_data);
913 case SKL_MODULE_TYPE_ALGO:
915 skl_set_base_module_format(skl, module_config, *param_data);
916 skl_set_base_ext_module_format(skl, module_config,
918 sizeof(struct skl_base_cfg));
922 dev_dbg(skl->dev, "Module type=%d id=%d config size: %d bytes\n",
923 module_config->m_type, module_config->id.module_id,
925 print_hex_dump_debug("Module params:", DUMP_PREFIX_OFFSET, 8, 4,
926 *param_data, param_size, false);
930 static int skl_get_queue_index(struct skl_module_pin *mpin,
931 struct skl_module_inst_id id, int max)
935 for (i = 0; i < max; i++) {
936 if (mpin[i].id.module_id == id.module_id &&
937 mpin[i].id.instance_id == id.instance_id)
945 * Allocates queue for each module.
946 * if dynamic, the pin_index is allocated 0 to max_pin.
947 * In static, the pin_index is fixed based on module_id and instance id
949 static int skl_alloc_queue(struct skl_module_pin *mpin,
950 struct skl_module_cfg *tgt_cfg, int max)
953 struct skl_module_inst_id id = tgt_cfg->id;
955 * if pin in dynamic, find first free pin
956 * otherwise find match module and instance id pin as topology will
957 * ensure a unique pin is assigned to this so no need to
960 for (i = 0; i < max; i++) {
961 if (mpin[i].is_dynamic) {
962 if (!mpin[i].in_use &&
963 mpin[i].pin_state == SKL_PIN_UNBIND) {
965 mpin[i].in_use = true;
966 mpin[i].id.module_id = id.module_id;
967 mpin[i].id.instance_id = id.instance_id;
968 mpin[i].id.pvt_id = id.pvt_id;
969 mpin[i].tgt_mcfg = tgt_cfg;
973 if (mpin[i].id.module_id == id.module_id &&
974 mpin[i].id.instance_id == id.instance_id &&
975 mpin[i].pin_state == SKL_PIN_UNBIND) {
977 mpin[i].tgt_mcfg = tgt_cfg;
986 static void skl_free_queue(struct skl_module_pin *mpin, int q_index)
988 if (mpin[q_index].is_dynamic) {
989 mpin[q_index].in_use = false;
990 mpin[q_index].id.module_id = 0;
991 mpin[q_index].id.instance_id = 0;
992 mpin[q_index].id.pvt_id = 0;
994 mpin[q_index].pin_state = SKL_PIN_UNBIND;
995 mpin[q_index].tgt_mcfg = NULL;
998 /* Module state will be set to unint, if all the out pin state is UNBIND */
1000 static void skl_clear_module_state(struct skl_module_pin *mpin, int max,
1001 struct skl_module_cfg *mcfg)
1006 for (i = 0; i < max; i++) {
1007 if (mpin[i].pin_state == SKL_PIN_UNBIND)
1014 mcfg->m_state = SKL_MODULE_INIT_DONE;
1019 * A module needs to be instanataited in DSP. A mdoule is present in a
1020 * collection of module referred as a PIPE.
1021 * We first calculate the module format, based on module type and then
1022 * invoke the DSP by sending IPC INIT_INSTANCE using ipc helper
1024 int skl_init_module(struct skl_dev *skl,
1025 struct skl_module_cfg *mconfig)
1027 u16 module_config_size = 0;
1028 void *param_data = NULL;
1030 struct skl_ipc_init_instance_msg msg;
1032 dev_dbg(skl->dev, "%s: module_id = %d instance=%d\n", __func__,
1033 mconfig->id.module_id, mconfig->id.pvt_id);
1035 if (mconfig->pipe->state != SKL_PIPE_CREATED) {
1036 dev_err(skl->dev, "Pipe not created state= %d pipe_id= %d\n",
1037 mconfig->pipe->state, mconfig->pipe->ppl_id);
1041 ret = skl_set_module_format(skl, mconfig,
1042 &module_config_size, ¶m_data);
1044 dev_err(skl->dev, "Failed to set module format ret=%d\n", ret);
1048 msg.module_id = mconfig->id.module_id;
1049 msg.instance_id = mconfig->id.pvt_id;
1050 msg.ppl_instance_id = mconfig->pipe->ppl_id;
1051 msg.param_data_size = module_config_size;
1052 msg.core_id = mconfig->core_id;
1053 msg.domain = mconfig->domain;
1055 ret = skl_ipc_init_instance(&skl->ipc, &msg, param_data);
1057 dev_err(skl->dev, "Failed to init instance ret=%d\n", ret);
1061 mconfig->m_state = SKL_MODULE_INIT_DONE;
1066 static void skl_dump_bind_info(struct skl_dev *skl, struct skl_module_cfg
1067 *src_module, struct skl_module_cfg *dst_module)
1069 dev_dbg(skl->dev, "%s: src module_id = %d src_instance=%d\n",
1070 __func__, src_module->id.module_id, src_module->id.pvt_id);
1071 dev_dbg(skl->dev, "%s: dst_module=%d dst_instance=%d\n", __func__,
1072 dst_module->id.module_id, dst_module->id.pvt_id);
1074 dev_dbg(skl->dev, "src_module state = %d dst module state = %d\n",
1075 src_module->m_state, dst_module->m_state);
1079 * On module freeup, we need to unbind the module with modules
1080 * it is already bind.
1081 * Find the pin allocated and unbind then using bind_unbind IPC
1083 int skl_unbind_modules(struct skl_dev *skl,
1084 struct skl_module_cfg *src_mcfg,
1085 struct skl_module_cfg *dst_mcfg)
1088 struct skl_ipc_bind_unbind_msg msg;
1089 struct skl_module_inst_id src_id = src_mcfg->id;
1090 struct skl_module_inst_id dst_id = dst_mcfg->id;
1091 int in_max = dst_mcfg->module->max_input_pins;
1092 int out_max = src_mcfg->module->max_output_pins;
1093 int src_index, dst_index, src_pin_state, dst_pin_state;
1095 skl_dump_bind_info(skl, src_mcfg, dst_mcfg);
1097 /* get src queue index */
1098 src_index = skl_get_queue_index(src_mcfg->m_out_pin, dst_id, out_max);
1102 msg.src_queue = src_index;
1104 /* get dst queue index */
1105 dst_index = skl_get_queue_index(dst_mcfg->m_in_pin, src_id, in_max);
1109 msg.dst_queue = dst_index;
1111 src_pin_state = src_mcfg->m_out_pin[src_index].pin_state;
1112 dst_pin_state = dst_mcfg->m_in_pin[dst_index].pin_state;
1114 if (src_pin_state != SKL_PIN_BIND_DONE ||
1115 dst_pin_state != SKL_PIN_BIND_DONE)
1118 msg.module_id = src_mcfg->id.module_id;
1119 msg.instance_id = src_mcfg->id.pvt_id;
1120 msg.dst_module_id = dst_mcfg->id.module_id;
1121 msg.dst_instance_id = dst_mcfg->id.pvt_id;
1124 ret = skl_ipc_bind_unbind(&skl->ipc, &msg);
1126 /* free queue only if unbind is success */
1127 skl_free_queue(src_mcfg->m_out_pin, src_index);
1128 skl_free_queue(dst_mcfg->m_in_pin, dst_index);
1131 * check only if src module bind state, bind is
1132 * always from src -> sink
1134 skl_clear_module_state(src_mcfg->m_out_pin, out_max, src_mcfg);
1140 #define CPR_SINK_FMT_PARAM_ID 2
1143 * Once a module is instantiated it need to be 'bind' with other modules in
1144 * the pipeline. For binding we need to find the module pins which are bind
1146 * This function finds the pins and then sends bund_unbind IPC message to
1147 * DSP using IPC helper
1149 int skl_bind_modules(struct skl_dev *skl,
1150 struct skl_module_cfg *src_mcfg,
1151 struct skl_module_cfg *dst_mcfg)
1154 struct skl_ipc_bind_unbind_msg msg;
1155 int in_max = dst_mcfg->module->max_input_pins;
1156 int out_max = src_mcfg->module->max_output_pins;
1157 int src_index, dst_index;
1158 struct skl_module_fmt *format;
1159 struct skl_cpr_pin_fmt pin_fmt;
1160 struct skl_module *module;
1161 struct skl_module_iface *fmt;
1163 skl_dump_bind_info(skl, src_mcfg, dst_mcfg);
1165 if (src_mcfg->m_state < SKL_MODULE_INIT_DONE ||
1166 dst_mcfg->m_state < SKL_MODULE_INIT_DONE)
1169 src_index = skl_alloc_queue(src_mcfg->m_out_pin, dst_mcfg, out_max);
1173 msg.src_queue = src_index;
1174 dst_index = skl_alloc_queue(dst_mcfg->m_in_pin, src_mcfg, in_max);
1175 if (dst_index < 0) {
1176 skl_free_queue(src_mcfg->m_out_pin, src_index);
1181 * Copier module requires the separate large_config_set_ipc to
1182 * configure the pins other than 0
1184 if (src_mcfg->m_type == SKL_MODULE_TYPE_COPIER && src_index > 0) {
1185 pin_fmt.sink_id = src_index;
1186 module = src_mcfg->module;
1187 fmt = &module->formats[src_mcfg->fmt_idx];
1189 /* Input fmt is same as that of src module input cfg */
1190 format = &fmt->inputs[0].fmt;
1191 fill_pin_params(&(pin_fmt.src_fmt), format);
1193 format = &fmt->outputs[src_index].fmt;
1194 fill_pin_params(&(pin_fmt.dst_fmt), format);
1195 ret = skl_set_module_params(skl, (void *)&pin_fmt,
1196 sizeof(struct skl_cpr_pin_fmt),
1197 CPR_SINK_FMT_PARAM_ID, src_mcfg);
1203 msg.dst_queue = dst_index;
1205 dev_dbg(skl->dev, "src queue = %d dst queue =%d\n",
1206 msg.src_queue, msg.dst_queue);
1208 msg.module_id = src_mcfg->id.module_id;
1209 msg.instance_id = src_mcfg->id.pvt_id;
1210 msg.dst_module_id = dst_mcfg->id.module_id;
1211 msg.dst_instance_id = dst_mcfg->id.pvt_id;
1214 ret = skl_ipc_bind_unbind(&skl->ipc, &msg);
1217 src_mcfg->m_state = SKL_MODULE_BIND_DONE;
1218 src_mcfg->m_out_pin[src_index].pin_state = SKL_PIN_BIND_DONE;
1219 dst_mcfg->m_in_pin[dst_index].pin_state = SKL_PIN_BIND_DONE;
1223 /* error case , if IPC fails, clear the queue index */
1224 skl_free_queue(src_mcfg->m_out_pin, src_index);
1225 skl_free_queue(dst_mcfg->m_in_pin, dst_index);
1230 static int skl_set_pipe_state(struct skl_dev *skl, struct skl_pipe *pipe,
1231 enum skl_ipc_pipeline_state state)
1233 dev_dbg(skl->dev, "%s: pipe_state = %d\n", __func__, state);
1235 return skl_ipc_set_pipeline_state(&skl->ipc, pipe->ppl_id, state);
1239 * A pipeline is a collection of modules. Before a module in instantiated a
1240 * pipeline needs to be created for it.
1241 * This function creates pipeline, by sending create pipeline IPC messages
1244 int skl_create_pipeline(struct skl_dev *skl, struct skl_pipe *pipe)
1248 dev_dbg(skl->dev, "%s: pipe_id = %d\n", __func__, pipe->ppl_id);
1250 ret = skl_ipc_create_pipeline(&skl->ipc, pipe->memory_pages,
1251 pipe->pipe_priority, pipe->ppl_id,
1254 dev_err(skl->dev, "Failed to create pipeline\n");
1258 pipe->state = SKL_PIPE_CREATED;
1264 * A pipeline needs to be deleted on cleanup. If a pipeline is running,
1265 * then pause it first. Before actual deletion, pipeline should enter
1266 * reset state. Finish the procedure by sending delete pipeline IPC.
1267 * DSP will stop the DMA engines and release resources
1269 int skl_delete_pipe(struct skl_dev *skl, struct skl_pipe *pipe)
1273 dev_dbg(skl->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
1275 /* If pipe was not created in FW, do not try to delete it */
1276 if (pipe->state < SKL_PIPE_CREATED)
1279 /* If pipe is started, do stop the pipe in FW. */
1280 if (pipe->state >= SKL_PIPE_STARTED) {
1281 ret = skl_set_pipe_state(skl, pipe, PPL_PAUSED);
1283 dev_err(skl->dev, "Failed to stop pipeline\n");
1287 pipe->state = SKL_PIPE_PAUSED;
1290 /* reset pipe state before deletion */
1291 ret = skl_set_pipe_state(skl, pipe, PPL_RESET);
1293 dev_err(skl->dev, "Failed to reset pipe ret=%d\n", ret);
1297 pipe->state = SKL_PIPE_RESET;
1299 ret = skl_ipc_delete_pipeline(&skl->ipc, pipe->ppl_id);
1301 dev_err(skl->dev, "Failed to delete pipeline\n");
1305 pipe->state = SKL_PIPE_INVALID;
1311 * A pipeline is also a scheduling entity in DSP which can be run, stopped
1312 * For processing data the pipe need to be run by sending IPC set pipe state
1315 int skl_run_pipe(struct skl_dev *skl, struct skl_pipe *pipe)
1319 dev_dbg(skl->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
1321 /* If pipe was not created in FW, do not try to pause or delete */
1322 if (pipe->state < SKL_PIPE_CREATED)
1325 /* Pipe has to be paused before it is started */
1326 ret = skl_set_pipe_state(skl, pipe, PPL_PAUSED);
1328 dev_err(skl->dev, "Failed to pause pipe\n");
1332 pipe->state = SKL_PIPE_PAUSED;
1334 ret = skl_set_pipe_state(skl, pipe, PPL_RUNNING);
1336 dev_err(skl->dev, "Failed to start pipe\n");
1340 pipe->state = SKL_PIPE_STARTED;
1346 * Stop the pipeline by sending set pipe state IPC
1347 * DSP doesnt implement stop so we always send pause message
1349 int skl_stop_pipe(struct skl_dev *skl, struct skl_pipe *pipe)
1353 dev_dbg(skl->dev, "In %s pipe=%d\n", __func__, pipe->ppl_id);
1355 /* If pipe was not created in FW, do not try to pause or delete */
1356 if (pipe->state < SKL_PIPE_PAUSED)
1359 ret = skl_set_pipe_state(skl, pipe, PPL_PAUSED);
1361 dev_dbg(skl->dev, "Failed to stop pipe\n");
1365 pipe->state = SKL_PIPE_PAUSED;
1371 * Reset the pipeline by sending set pipe state IPC this will reset the DMA
1374 int skl_reset_pipe(struct skl_dev *skl, struct skl_pipe *pipe)
1378 /* If pipe was not created in FW, do not try to pause or delete */
1379 if (pipe->state < SKL_PIPE_PAUSED)
1382 ret = skl_set_pipe_state(skl, pipe, PPL_RESET);
1384 dev_dbg(skl->dev, "Failed to reset pipe ret=%d\n", ret);
1388 pipe->state = SKL_PIPE_RESET;
1393 /* Algo parameter set helper function */
1394 int skl_set_module_params(struct skl_dev *skl, u32 *params, int size,
1395 u32 param_id, struct skl_module_cfg *mcfg)
1397 struct skl_ipc_large_config_msg msg;
1399 msg.module_id = mcfg->id.module_id;
1400 msg.instance_id = mcfg->id.pvt_id;
1401 msg.param_data_size = size;
1402 msg.large_param_id = param_id;
1404 return skl_ipc_set_large_config(&skl->ipc, &msg, params);
1407 int skl_get_module_params(struct skl_dev *skl, u32 *params, int size,
1408 u32 param_id, struct skl_module_cfg *mcfg)
1410 struct skl_ipc_large_config_msg msg;
1411 size_t bytes = size;
1413 msg.module_id = mcfg->id.module_id;
1414 msg.instance_id = mcfg->id.pvt_id;
1415 msg.param_data_size = size;
1416 msg.large_param_id = param_id;
1418 return skl_ipc_get_large_config(&skl->ipc, &msg, ¶ms, &bytes);