1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  skl-message.c - HDA DSP interface for FW registration, Pipe and Module
4  *  configurations
5  *
6  *  Copyright (C) 2015 Intel Corp
7  *  Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
8  *	   Jeeja KP <jeeja.kp@intel.com>
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  */
11 
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"
20 #include "skl.h"
21 #include "../common/sst-dsp.h"
22 #include "../common/sst-dsp-priv.h"
23 #include "skl-topology.h"
24 
25 static int skl_alloc_dma_buf(struct device *dev,
26 		struct snd_dma_buffer *dmab, size_t size)
27 {
28 	return snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, dmab);
29 }
30 
31 static int skl_free_dma_buf(struct device *dev, struct snd_dma_buffer *dmab)
32 {
33 	snd_dma_free_pages(dmab);
34 	return 0;
35 }
36 
37 #define SKL_ASTATE_PARAM_ID	4
38 
39 void skl_dsp_set_astate_cfg(struct skl_dev *skl, u32 cnt, void *data)
40 {
41 	struct skl_ipc_large_config_msg	msg = {0};
42 
43 	msg.large_param_id = SKL_ASTATE_PARAM_ID;
44 	msg.param_data_size = (cnt * sizeof(struct skl_astate_param) +
45 				sizeof(cnt));
46 
47 	skl_ipc_set_large_config(&skl->ipc, &msg, data);
48 }
49 
50 static int skl_dsp_setup_spib(struct device *dev, unsigned int size,
51 				int stream_tag, int enable)
52 {
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);
56 	struct hdac_ext_stream *estream;
57 
58 	if (!stream)
59 		return -EINVAL;
60 
61 	estream = stream_to_hdac_ext_stream(stream);
62 	/* enable/disable SPIB for this hdac stream */
63 	snd_hdac_ext_stream_spbcap_enable(bus, enable, stream->index);
64 
65 	/* set the spib value */
66 	snd_hdac_ext_stream_set_spib(bus, estream, size);
67 
68 	return 0;
69 }
70 
71 static int skl_dsp_prepare(struct device *dev, unsigned int format,
72 			unsigned int size, struct snd_dma_buffer *dmab)
73 {
74 	struct hdac_bus *bus = dev_get_drvdata(dev);
75 	struct hdac_ext_stream *estream;
76 	struct hdac_stream *stream;
77 	struct snd_pcm_substream substream;
78 	int ret;
79 
80 	if (!bus)
81 		return -ENODEV;
82 
83 	memset(&substream, 0, sizeof(substream));
84 	substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
85 
86 	estream = snd_hdac_ext_stream_assign(bus, &substream,
87 					HDAC_EXT_STREAM_TYPE_HOST);
88 	if (!estream)
89 		return -ENODEV;
90 
91 	stream = hdac_stream(estream);
92 
93 	/* assign decouple host dma channel */
94 	ret = snd_hdac_dsp_prepare(stream, format, size, dmab);
95 	if (ret < 0)
96 		return ret;
97 
98 	skl_dsp_setup_spib(dev, size, stream->stream_tag, true);
99 
100 	return stream->stream_tag;
101 }
102 
103 static int skl_dsp_trigger(struct device *dev, bool start, int stream_tag)
104 {
105 	struct hdac_bus *bus = dev_get_drvdata(dev);
106 	struct hdac_stream *stream;
107 
108 	if (!bus)
109 		return -ENODEV;
110 
111 	stream = snd_hdac_get_stream(bus,
112 		SNDRV_PCM_STREAM_PLAYBACK, stream_tag);
113 	if (!stream)
114 		return -EINVAL;
115 
116 	snd_hdac_dsp_trigger(stream, start);
117 
118 	return 0;
119 }
120 
121 static int skl_dsp_cleanup(struct device *dev,
122 		struct snd_dma_buffer *dmab, int stream_tag)
123 {
124 	struct hdac_bus *bus = dev_get_drvdata(dev);
125 	struct hdac_stream *stream;
126 	struct hdac_ext_stream *estream;
127 
128 	if (!bus)
129 		return -ENODEV;
130 
131 	stream = snd_hdac_get_stream(bus,
132 		SNDRV_PCM_STREAM_PLAYBACK, stream_tag);
133 	if (!stream)
134 		return -EINVAL;
135 
136 	estream = stream_to_hdac_ext_stream(stream);
137 	skl_dsp_setup_spib(dev, 0, stream_tag, false);
138 	snd_hdac_ext_stream_release(estream, HDAC_EXT_STREAM_TYPE_HOST);
139 
140 	snd_hdac_dsp_cleanup(stream, dmab);
141 
142 	return 0;
143 }
144 
145 static struct skl_dsp_loader_ops skl_get_loader_ops(void)
146 {
147 	struct skl_dsp_loader_ops loader_ops;
148 
149 	memset(&loader_ops, 0, sizeof(struct skl_dsp_loader_ops));
150 
151 	loader_ops.alloc_dma_buf = skl_alloc_dma_buf;
152 	loader_ops.free_dma_buf = skl_free_dma_buf;
153 
154 	return loader_ops;
155 };
156 
157 static struct skl_dsp_loader_ops bxt_get_loader_ops(void)
158 {
159 	struct skl_dsp_loader_ops loader_ops;
160 
161 	memset(&loader_ops, 0, sizeof(loader_ops));
162 
163 	loader_ops.alloc_dma_buf = skl_alloc_dma_buf;
164 	loader_ops.free_dma_buf = skl_free_dma_buf;
165 	loader_ops.prepare = skl_dsp_prepare;
166 	loader_ops.trigger = skl_dsp_trigger;
167 	loader_ops.cleanup = skl_dsp_cleanup;
168 
169 	return loader_ops;
170 };
171 
172 static const struct skl_dsp_ops dsp_ops[] = {
173 	{
174 		.id = 0x9d70,
175 		.num_cores = 2,
176 		.loader_ops = skl_get_loader_ops,
177 		.init = skl_sst_dsp_init,
178 		.init_fw = skl_sst_init_fw,
179 		.cleanup = skl_sst_dsp_cleanup
180 	},
181 	{
182 		.id = 0x9d71,
183 		.num_cores = 2,
184 		.loader_ops = skl_get_loader_ops,
185 		.init = skl_sst_dsp_init,
186 		.init_fw = skl_sst_init_fw,
187 		.cleanup = skl_sst_dsp_cleanup
188 	},
189 	{
190 		.id = 0x5a98,
191 		.num_cores = 2,
192 		.loader_ops = bxt_get_loader_ops,
193 		.init = bxt_sst_dsp_init,
194 		.init_fw = bxt_sst_init_fw,
195 		.cleanup = bxt_sst_dsp_cleanup
196 	},
197 	{
198 		.id = 0x3198,
199 		.num_cores = 2,
200 		.loader_ops = bxt_get_loader_ops,
201 		.init = bxt_sst_dsp_init,
202 		.init_fw = bxt_sst_init_fw,
203 		.cleanup = bxt_sst_dsp_cleanup
204 	},
205 	{
206 		.id = 0x9dc8,
207 		.num_cores = 4,
208 		.loader_ops = bxt_get_loader_ops,
209 		.init = cnl_sst_dsp_init,
210 		.init_fw = cnl_sst_init_fw,
211 		.cleanup = cnl_sst_dsp_cleanup
212 	},
213 	{
214 		.id = 0xa348,
215 		.num_cores = 4,
216 		.loader_ops = bxt_get_loader_ops,
217 		.init = cnl_sst_dsp_init,
218 		.init_fw = cnl_sst_init_fw,
219 		.cleanup = cnl_sst_dsp_cleanup
220 	},
221 	{
222 		.id = 0x02c8,
223 		.num_cores = 4,
224 		.loader_ops = bxt_get_loader_ops,
225 		.init = cnl_sst_dsp_init,
226 		.init_fw = cnl_sst_init_fw,
227 		.cleanup = cnl_sst_dsp_cleanup
228 	},
229 	{
230 		.id = 0x06c8,
231 		.num_cores = 4,
232 		.loader_ops = bxt_get_loader_ops,
233 		.init = cnl_sst_dsp_init,
234 		.init_fw = cnl_sst_init_fw,
235 		.cleanup = cnl_sst_dsp_cleanup
236 	},
237 };
238 
239 const struct skl_dsp_ops *skl_get_dsp_ops(int pci_id)
240 {
241 	int i;
242 
243 	for (i = 0; i < ARRAY_SIZE(dsp_ops); i++) {
244 		if (dsp_ops[i].id == pci_id)
245 			return &dsp_ops[i];
246 	}
247 
248 	return NULL;
249 }
250 
251 int skl_init_dsp(struct skl_dev *skl)
252 {
253 	void __iomem *mmio_base;
254 	struct hdac_bus *bus = skl_to_bus(skl);
255 	struct skl_dsp_loader_ops loader_ops;
256 	int irq = bus->irq;
257 	const struct skl_dsp_ops *ops;
258 	struct skl_dsp_cores *cores;
259 	int ret;
260 
261 	/* enable ppcap interrupt */
262 	snd_hdac_ext_bus_ppcap_enable(bus, true);
263 	snd_hdac_ext_bus_ppcap_int_enable(bus, true);
264 
265 	/* read the BAR of the ADSP MMIO */
266 	mmio_base = pci_ioremap_bar(skl->pci, 4);
267 	if (mmio_base == NULL) {
268 		dev_err(bus->dev, "ioremap error\n");
269 		return -ENXIO;
270 	}
271 
272 	ops = skl_get_dsp_ops(skl->pci->device);
273 	if (!ops) {
274 		ret = -EIO;
275 		goto unmap_mmio;
276 	}
277 
278 	loader_ops = ops->loader_ops();
279 	ret = ops->init(bus->dev, mmio_base, irq,
280 				skl->fw_name, loader_ops,
281 				&skl);
282 
283 	if (ret < 0)
284 		goto unmap_mmio;
285 
286 	skl->dsp_ops = ops;
287 	cores = &skl->cores;
288 	cores->count = ops->num_cores;
289 
290 	cores->state = kcalloc(cores->count, sizeof(*cores->state), GFP_KERNEL);
291 	if (!cores->state) {
292 		ret = -ENOMEM;
293 		goto unmap_mmio;
294 	}
295 
296 	cores->usage_count = kcalloc(cores->count, sizeof(*cores->usage_count),
297 				     GFP_KERNEL);
298 	if (!cores->usage_count) {
299 		ret = -ENOMEM;
300 		goto free_core_state;
301 	}
302 
303 	dev_dbg(bus->dev, "dsp registration status=%d\n", ret);
304 
305 	return 0;
306 
307 free_core_state:
308 	kfree(cores->state);
309 
310 unmap_mmio:
311 	iounmap(mmio_base);
312 
313 	return ret;
314 }
315 
316 int skl_free_dsp(struct skl_dev *skl)
317 {
318 	struct hdac_bus *bus = skl_to_bus(skl);
319 
320 	/* disable  ppcap interrupt */
321 	snd_hdac_ext_bus_ppcap_int_enable(bus, false);
322 
323 	skl->dsp_ops->cleanup(bus->dev, skl);
324 
325 	kfree(skl->cores.state);
326 	kfree(skl->cores.usage_count);
327 
328 	if (skl->dsp->addr.lpe)
329 		iounmap(skl->dsp->addr.lpe);
330 
331 	return 0;
332 }
333 
334 /*
335  * In the case of "suspend_active" i.e, the Audio IP being active
336  * during system suspend, immediately excecute any pending D0i3 work
337  * before suspending. This is needed for the IP to work in low power
338  * mode during system suspend. In the case of normal suspend, cancel
339  * any pending D0i3 work.
340  */
341 int skl_suspend_late_dsp(struct skl_dev *skl)
342 {
343 	struct delayed_work *dwork;
344 
345 	if (!skl)
346 		return 0;
347 
348 	dwork = &skl->d0i3.work;
349 
350 	if (dwork->work.func) {
351 		if (skl->supend_active)
352 			flush_delayed_work(dwork);
353 		else
354 			cancel_delayed_work_sync(dwork);
355 	}
356 
357 	return 0;
358 }
359 
360 int skl_suspend_dsp(struct skl_dev *skl)
361 {
362 	struct hdac_bus *bus = skl_to_bus(skl);
363 	int ret;
364 
365 	/* if ppcap is not supported return 0 */
366 	if (!bus->ppcap)
367 		return 0;
368 
369 	ret = skl_dsp_sleep(skl->dsp);
370 	if (ret < 0)
371 		return ret;
372 
373 	/* disable ppcap interrupt */
374 	snd_hdac_ext_bus_ppcap_int_enable(bus, false);
375 	snd_hdac_ext_bus_ppcap_enable(bus, false);
376 
377 	return 0;
378 }
379 
380 int skl_resume_dsp(struct skl_dev *skl)
381 {
382 	struct hdac_bus *bus = skl_to_bus(skl);
383 	int ret;
384 
385 	/* if ppcap is not supported return 0 */
386 	if (!bus->ppcap)
387 		return 0;
388 
389 	/* enable ppcap interrupt */
390 	snd_hdac_ext_bus_ppcap_enable(bus, true);
391 	snd_hdac_ext_bus_ppcap_int_enable(bus, true);
392 
393 	/* check if DSP 1st boot is done */
394 	if (skl->is_first_boot)
395 		return 0;
396 
397 	/*
398 	 * Disable dynamic clock and power gating during firmware
399 	 * and library download
400 	 */
401 	skl->enable_miscbdcge(skl->dev, false);
402 	skl->clock_power_gating(skl->dev, false);
403 
404 	ret = skl_dsp_wake(skl->dsp);
405 	skl->enable_miscbdcge(skl->dev, true);
406 	skl->clock_power_gating(skl->dev, true);
407 	if (ret < 0)
408 		return ret;
409 
410 	if (skl->cfg.astate_cfg != NULL) {
411 		skl_dsp_set_astate_cfg(skl, skl->cfg.astate_cfg->count,
412 					skl->cfg.astate_cfg);
413 	}
414 	return ret;
415 }
416 
417 enum skl_bitdepth skl_get_bit_depth(int params)
418 {
419 	switch (params) {
420 	case 8:
421 		return SKL_DEPTH_8BIT;
422 
423 	case 16:
424 		return SKL_DEPTH_16BIT;
425 
426 	case 24:
427 		return SKL_DEPTH_24BIT;
428 
429 	case 32:
430 		return SKL_DEPTH_32BIT;
431 
432 	default:
433 		return SKL_DEPTH_INVALID;
434 
435 	}
436 }
437 
438 /*
439  * Each module in DSP expects a base module configuration, which consists of
440  * PCM format information, which we calculate in driver and resource values
441  * which are read from widget information passed through topology binary
442  * This is send when we create a module with INIT_INSTANCE IPC msg
443  */
444 static void skl_set_base_module_format(struct skl_dev *skl,
445 			struct skl_module_cfg *mconfig,
446 			struct skl_base_cfg *base_cfg)
447 {
448 	struct skl_module *module = mconfig->module;
449 	struct skl_module_res *res = &module->resources[mconfig->res_idx];
450 	struct skl_module_iface *fmt = &module->formats[mconfig->fmt_idx];
451 	struct skl_module_fmt *format = &fmt->inputs[0].fmt;
452 
453 	base_cfg->audio_fmt.number_of_channels = format->channels;
454 
455 	base_cfg->audio_fmt.s_freq = format->s_freq;
456 	base_cfg->audio_fmt.bit_depth = format->bit_depth;
457 	base_cfg->audio_fmt.valid_bit_depth = format->valid_bit_depth;
458 	base_cfg->audio_fmt.ch_cfg = format->ch_cfg;
459 	base_cfg->audio_fmt.sample_type = format->sample_type;
460 
461 	dev_dbg(skl->dev, "bit_depth=%x valid_bd=%x ch_config=%x\n",
462 			format->bit_depth, format->valid_bit_depth,
463 			format->ch_cfg);
464 
465 	base_cfg->audio_fmt.channel_map = format->ch_map;
466 
467 	base_cfg->audio_fmt.interleaving = format->interleaving_style;
468 
469 	base_cfg->cpc = res->cpc;
470 	base_cfg->ibs = res->ibs;
471 	base_cfg->obs = res->obs;
472 	base_cfg->is_pages = res->is_pages;
473 }
474 
475 static void fill_pin_params(struct skl_audio_data_format *pin_fmt,
476 			    struct skl_module_fmt *format)
477 {
478 	pin_fmt->number_of_channels = format->channels;
479 	pin_fmt->s_freq = format->s_freq;
480 	pin_fmt->bit_depth = format->bit_depth;
481 	pin_fmt->valid_bit_depth = format->valid_bit_depth;
482 	pin_fmt->ch_cfg = format->ch_cfg;
483 	pin_fmt->sample_type = format->sample_type;
484 	pin_fmt->channel_map = format->ch_map;
485 	pin_fmt->interleaving = format->interleaving_style;
486 }
487 
488 /*
489  * Any module configuration begins with a base module configuration but
490  * can be followed by a generic extension containing audio format for all
491  * module's pins that are in use.
492  */
493 static void skl_set_base_ext_module_format(struct skl_dev *skl,
494 					   struct skl_module_cfg *mconfig,
495 					   struct skl_base_cfg_ext *base_cfg_ext)
496 {
497 	struct skl_module *module = mconfig->module;
498 	struct skl_module_pin_resources *pin_res;
499 	struct skl_module_iface *fmt = &module->formats[mconfig->fmt_idx];
500 	struct skl_module_res *res = &module->resources[mconfig->res_idx];
501 	struct skl_module_fmt *format;
502 	struct skl_pin_format *pin_fmt;
503 	char *params;
504 	int i;
505 
506 	base_cfg_ext->nr_input_pins = res->nr_input_pins;
507 	base_cfg_ext->nr_output_pins = res->nr_output_pins;
508 	base_cfg_ext->priv_param_length =
509 		mconfig->formats_config[SKL_PARAM_INIT].caps_size;
510 
511 	for (i = 0; i < res->nr_input_pins; i++) {
512 		pin_res = &res->input[i];
513 		pin_fmt = &base_cfg_ext->pins_fmt[i];
514 
515 		pin_fmt->pin_idx = pin_res->pin_index;
516 		pin_fmt->buf_size = pin_res->buf_size;
517 
518 		format = &fmt->inputs[pin_res->pin_index].fmt;
519 		fill_pin_params(&pin_fmt->audio_fmt, format);
520 	}
521 
522 	for (i = 0; i < res->nr_output_pins; i++) {
523 		pin_res = &res->output[i];
524 		pin_fmt = &base_cfg_ext->pins_fmt[res->nr_input_pins + i];
525 
526 		pin_fmt->pin_idx = pin_res->pin_index;
527 		pin_fmt->buf_size = pin_res->buf_size;
528 
529 		format = &fmt->outputs[pin_res->pin_index].fmt;
530 		fill_pin_params(&pin_fmt->audio_fmt, format);
531 	}
532 
533 	if (!base_cfg_ext->priv_param_length)
534 		return;
535 
536 	params = (char *)base_cfg_ext + sizeof(struct skl_base_cfg_ext);
537 	params += (base_cfg_ext->nr_input_pins + base_cfg_ext->nr_output_pins) *
538 		  sizeof(struct skl_pin_format);
539 
540 	memcpy(params, mconfig->formats_config[SKL_PARAM_INIT].caps,
541 	       mconfig->formats_config[SKL_PARAM_INIT].caps_size);
542 }
543 
544 /*
545  * Copies copier capabilities into copier module and updates copier module
546  * config size.
547  */
548 static void skl_copy_copier_caps(struct skl_module_cfg *mconfig,
549 				struct skl_cpr_cfg *cpr_mconfig)
550 {
551 	if (mconfig->formats_config[SKL_PARAM_INIT].caps_size == 0)
552 		return;
553 
554 	memcpy(cpr_mconfig->gtw_cfg.config_data,
555 			mconfig->formats_config[SKL_PARAM_INIT].caps,
556 			mconfig->formats_config[SKL_PARAM_INIT].caps_size);
557 
558 	cpr_mconfig->gtw_cfg.config_length =
559 			(mconfig->formats_config[SKL_PARAM_INIT].caps_size) / 4;
560 }
561 
562 #define SKL_NON_GATEWAY_CPR_NODE_ID 0xFFFFFFFF
563 /*
564  * Calculate the gatewat settings required for copier module, type of
565  * gateway and index of gateway to use
566  */
567 static u32 skl_get_node_id(struct skl_dev *skl,
568 			struct skl_module_cfg *mconfig)
569 {
570 	union skl_connector_node_id node_id = {0};
571 	union skl_ssp_dma_node ssp_node  = {0};
572 	struct skl_pipe_params *params = mconfig->pipe->p_params;
573 
574 	switch (mconfig->dev_type) {
575 	case SKL_DEVICE_BT:
576 		node_id.node.dma_type =
577 			(SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
578 			SKL_DMA_I2S_LINK_OUTPUT_CLASS :
579 			SKL_DMA_I2S_LINK_INPUT_CLASS;
580 		node_id.node.vindex = params->host_dma_id +
581 					(mconfig->vbus_id << 3);
582 		break;
583 
584 	case SKL_DEVICE_I2S:
585 		node_id.node.dma_type =
586 			(SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
587 			SKL_DMA_I2S_LINK_OUTPUT_CLASS :
588 			SKL_DMA_I2S_LINK_INPUT_CLASS;
589 		ssp_node.dma_node.time_slot_index = mconfig->time_slot;
590 		ssp_node.dma_node.i2s_instance = mconfig->vbus_id;
591 		node_id.node.vindex = ssp_node.val;
592 		break;
593 
594 	case SKL_DEVICE_DMIC:
595 		node_id.node.dma_type = SKL_DMA_DMIC_LINK_INPUT_CLASS;
596 		node_id.node.vindex = mconfig->vbus_id +
597 					 (mconfig->time_slot);
598 		break;
599 
600 	case SKL_DEVICE_HDALINK:
601 		node_id.node.dma_type =
602 			(SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
603 			SKL_DMA_HDA_LINK_OUTPUT_CLASS :
604 			SKL_DMA_HDA_LINK_INPUT_CLASS;
605 		node_id.node.vindex = params->link_dma_id;
606 		break;
607 
608 	case SKL_DEVICE_HDAHOST:
609 		node_id.node.dma_type =
610 			(SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
611 			SKL_DMA_HDA_HOST_OUTPUT_CLASS :
612 			SKL_DMA_HDA_HOST_INPUT_CLASS;
613 		node_id.node.vindex = params->host_dma_id;
614 		break;
615 
616 	default:
617 		node_id.val = 0xFFFFFFFF;
618 		break;
619 	}
620 
621 	return node_id.val;
622 }
623 
624 static void skl_setup_cpr_gateway_cfg(struct skl_dev *skl,
625 			struct skl_module_cfg *mconfig,
626 			struct skl_cpr_cfg *cpr_mconfig)
627 {
628 	u32 dma_io_buf;
629 	struct skl_module_res *res;
630 	int res_idx = mconfig->res_idx;
631 
632 	cpr_mconfig->gtw_cfg.node_id = skl_get_node_id(skl, mconfig);
633 
634 	if (cpr_mconfig->gtw_cfg.node_id == SKL_NON_GATEWAY_CPR_NODE_ID) {
635 		cpr_mconfig->cpr_feature_mask = 0;
636 		return;
637 	}
638 
639 	if (skl->nr_modules) {
640 		res = &mconfig->module->resources[mconfig->res_idx];
641 		cpr_mconfig->gtw_cfg.dma_buffer_size = res->dma_buffer_size;
642 		goto skip_buf_size_calc;
643 	} else {
644 		res = &mconfig->module->resources[res_idx];
645 	}
646 
647 	switch (mconfig->hw_conn_type) {
648 	case SKL_CONN_SOURCE:
649 		if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
650 			dma_io_buf =  res->ibs;
651 		else
652 			dma_io_buf =  res->obs;
653 		break;
654 
655 	case SKL_CONN_SINK:
656 		if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
657 			dma_io_buf =  res->obs;
658 		else
659 			dma_io_buf =  res->ibs;
660 		break;
661 
662 	default:
663 		dev_warn(skl->dev, "wrong connection type: %d\n",
664 				mconfig->hw_conn_type);
665 		return;
666 	}
667 
668 	cpr_mconfig->gtw_cfg.dma_buffer_size =
669 				mconfig->dma_buffer_size * dma_io_buf;
670 
671 	/* fallback to 2ms default value */
672 	if (!cpr_mconfig->gtw_cfg.dma_buffer_size) {
673 		if (mconfig->hw_conn_type == SKL_CONN_SOURCE)
674 			cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * res->obs;
675 		else
676 			cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * res->ibs;
677 	}
678 
679 skip_buf_size_calc:
680 	cpr_mconfig->cpr_feature_mask = 0;
681 	cpr_mconfig->gtw_cfg.config_length  = 0;
682 
683 	skl_copy_copier_caps(mconfig, cpr_mconfig);
684 }
685 
686 #define DMA_CONTROL_ID 5
687 #define DMA_I2S_BLOB_SIZE 21
688 
689 int skl_dsp_set_dma_control(struct skl_dev *skl, u32 *caps,
690 				u32 caps_size, u32 node_id)
691 {
692 	struct skl_dma_control *dma_ctrl;
693 	struct skl_ipc_large_config_msg msg = {0};
694 	int err = 0;
695 
696 
697 	/*
698 	 * if blob size zero, then return
699 	 */
700 	if (caps_size == 0)
701 		return 0;
702 
703 	msg.large_param_id = DMA_CONTROL_ID;
704 	msg.param_data_size = sizeof(struct skl_dma_control) + caps_size;
705 
706 	dma_ctrl = kzalloc(msg.param_data_size, GFP_KERNEL);
707 	if (dma_ctrl == NULL)
708 		return -ENOMEM;
709 
710 	dma_ctrl->node_id = node_id;
711 
712 	/*
713 	 * NHLT blob may contain additional configs along with i2s blob.
714 	 * firmware expects only the i2s blob size as the config_length.
715 	 * So fix to i2s blob size.
716 	 * size in dwords.
717 	 */
718 	dma_ctrl->config_length = DMA_I2S_BLOB_SIZE;
719 
720 	memcpy(dma_ctrl->config_data, caps, caps_size);
721 
722 	err = skl_ipc_set_large_config(&skl->ipc, &msg, (u32 *)dma_ctrl);
723 
724 	kfree(dma_ctrl);
725 	return err;
726 }
727 EXPORT_SYMBOL_GPL(skl_dsp_set_dma_control);
728 
729 static void skl_setup_out_format(struct skl_dev *skl,
730 			struct skl_module_cfg *mconfig,
731 			struct skl_audio_data_format *out_fmt)
732 {
733 	struct skl_module *module = mconfig->module;
734 	struct skl_module_iface *fmt = &module->formats[mconfig->fmt_idx];
735 	struct skl_module_fmt *format = &fmt->outputs[0].fmt;
736 
737 	out_fmt->number_of_channels = (u8)format->channels;
738 	out_fmt->s_freq = format->s_freq;
739 	out_fmt->bit_depth = format->bit_depth;
740 	out_fmt->valid_bit_depth = format->valid_bit_depth;
741 	out_fmt->ch_cfg = format->ch_cfg;
742 
743 	out_fmt->channel_map = format->ch_map;
744 	out_fmt->interleaving = format->interleaving_style;
745 	out_fmt->sample_type = format->sample_type;
746 
747 	dev_dbg(skl->dev, "copier out format chan=%d fre=%d bitdepth=%d\n",
748 		out_fmt->number_of_channels, format->s_freq, format->bit_depth);
749 }
750 
751 /*
752  * DSP needs SRC module for frequency conversion, SRC takes base module
753  * configuration and the target frequency as extra parameter passed as src
754  * config
755  */
756 static void skl_set_src_format(struct skl_dev *skl,
757 			struct skl_module_cfg *mconfig,
758 			struct skl_src_module_cfg *src_mconfig)
759 {
760 	struct skl_module *module = mconfig->module;
761 	struct skl_module_iface *iface = &module->formats[mconfig->fmt_idx];
762 	struct skl_module_fmt *fmt = &iface->outputs[0].fmt;
763 
764 	skl_set_base_module_format(skl, mconfig,
765 		(struct skl_base_cfg *)src_mconfig);
766 
767 	src_mconfig->src_cfg = fmt->s_freq;
768 }
769 
770 /*
771  * DSP needs updown module to do channel conversion. updown module take base
772  * module configuration and channel configuration
773  * It also take coefficients and now we have defaults applied here
774  */
775 static void skl_set_updown_mixer_format(struct skl_dev *skl,
776 			struct skl_module_cfg *mconfig,
777 			struct skl_up_down_mixer_cfg *mixer_mconfig)
778 {
779 	struct skl_module *module = mconfig->module;
780 	struct skl_module_iface *iface = &module->formats[mconfig->fmt_idx];
781 	struct skl_module_fmt *fmt = &iface->outputs[0].fmt;
782 
783 	skl_set_base_module_format(skl,	mconfig,
784 		(struct skl_base_cfg *)mixer_mconfig);
785 	mixer_mconfig->out_ch_cfg = fmt->ch_cfg;
786 	mixer_mconfig->ch_map = fmt->ch_map;
787 }
788 
789 /*
790  * 'copier' is DSP internal module which copies data from Host DMA (HDA host
791  * dma) or link (hda link, SSP, PDM)
792  * Here we calculate the copier module parameters, like PCM format, output
793  * format, gateway settings
794  * copier_module_config is sent as input buffer with INIT_INSTANCE IPC msg
795  */
796 static void skl_set_copier_format(struct skl_dev *skl,
797 			struct skl_module_cfg *mconfig,
798 			struct skl_cpr_cfg *cpr_mconfig)
799 {
800 	struct skl_audio_data_format *out_fmt = &cpr_mconfig->out_fmt;
801 	struct skl_base_cfg *base_cfg = (struct skl_base_cfg *)cpr_mconfig;
802 
803 	skl_set_base_module_format(skl, mconfig, base_cfg);
804 
805 	skl_setup_out_format(skl, mconfig, out_fmt);
806 	skl_setup_cpr_gateway_cfg(skl, mconfig, cpr_mconfig);
807 }
808 
809 /*
810  * Algo module are DSP pre processing modules. Algo module take base module
811  * configuration and params
812  */
813 
814 static void skl_set_algo_format(struct skl_dev *skl,
815 			struct skl_module_cfg *mconfig,
816 			struct skl_algo_cfg *algo_mcfg)
817 {
818 	struct skl_base_cfg *base_cfg = (struct skl_base_cfg *)algo_mcfg;
819 
820 	skl_set_base_module_format(skl, mconfig, base_cfg);
821 	if (mconfig->formats_config[SKL_PARAM_INIT].caps_size == 0)
822 		return;
823 
824 	memcpy(algo_mcfg->params,
825 			mconfig->formats_config[SKL_PARAM_INIT].caps,
826 			mconfig->formats_config[SKL_PARAM_INIT].caps_size);
827 
828 }
829 
830 /*
831  * Mic select module allows selecting one or many input channels, thus
832  * acting as a demux.
833  *
834  * Mic select module take base module configuration and out-format
835  * configuration
836  */
837 static void skl_set_base_outfmt_format(struct skl_dev *skl,
838 			struct skl_module_cfg *mconfig,
839 			struct skl_base_outfmt_cfg *base_outfmt_mcfg)
840 {
841 	struct skl_audio_data_format *out_fmt = &base_outfmt_mcfg->out_fmt;
842 	struct skl_base_cfg *base_cfg =
843 				(struct skl_base_cfg *)base_outfmt_mcfg;
844 
845 	skl_set_base_module_format(skl, mconfig, base_cfg);
846 	skl_setup_out_format(skl, mconfig, out_fmt);
847 }
848 
849 static u16 skl_get_module_param_size(struct skl_dev *skl,
850 			struct skl_module_cfg *mconfig)
851 {
852 	u16 param_size;
853 
854 	switch (mconfig->m_type) {
855 	case SKL_MODULE_TYPE_COPIER:
856 		param_size = sizeof(struct skl_cpr_cfg);
857 		param_size += mconfig->formats_config[SKL_PARAM_INIT].caps_size;
858 		return param_size;
859 
860 	case SKL_MODULE_TYPE_SRCINT:
861 		return sizeof(struct skl_src_module_cfg);
862 
863 	case SKL_MODULE_TYPE_UPDWMIX:
864 		return sizeof(struct skl_up_down_mixer_cfg);
865 
866 	case SKL_MODULE_TYPE_ALGO:
867 		param_size = sizeof(struct skl_base_cfg);
868 		param_size += mconfig->formats_config[SKL_PARAM_INIT].caps_size;
869 		return param_size;
870 
871 	case SKL_MODULE_TYPE_BASE_OUTFMT:
872 	case SKL_MODULE_TYPE_MIC_SELECT:
873 		return sizeof(struct skl_base_outfmt_cfg);
874 
875 	case SKL_MODULE_TYPE_MIXER:
876 	case SKL_MODULE_TYPE_KPB:
877 		return sizeof(struct skl_base_cfg);
878 
879 	default:
880 		/*
881 		 * return only base cfg when no specific module type is
882 		 * specified
883 		 */
884 		return sizeof(struct skl_base_cfg);
885 	}
886 
887 	return 0;
888 }
889 
890 /*
891  * DSP firmware supports various modules like copier, SRC, updown etc.
892  * These modules required various parameters to be calculated and sent for
893  * the module initialization to DSP. By default a generic module needs only
894  * base module format configuration
895  */
896 
897 static int skl_set_module_format(struct skl_dev *skl,
898 			struct skl_module_cfg *module_config,
899 			u16 *module_config_size,
900 			void **param_data)
901 {
902 	u16 param_size;
903 
904 	param_size  = skl_get_module_param_size(skl, module_config);
905 
906 	*param_data = kzalloc(param_size, GFP_KERNEL);
907 	if (NULL == *param_data)
908 		return -ENOMEM;
909 
910 	*module_config_size = param_size;
911 
912 	switch (module_config->m_type) {
913 	case SKL_MODULE_TYPE_COPIER:
914 		skl_set_copier_format(skl, module_config, *param_data);
915 		break;
916 
917 	case SKL_MODULE_TYPE_SRCINT:
918 		skl_set_src_format(skl, module_config, *param_data);
919 		break;
920 
921 	case SKL_MODULE_TYPE_UPDWMIX:
922 		skl_set_updown_mixer_format(skl, module_config, *param_data);
923 		break;
924 
925 	case SKL_MODULE_TYPE_ALGO:
926 		skl_set_algo_format(skl, module_config, *param_data);
927 		break;
928 
929 	case SKL_MODULE_TYPE_BASE_OUTFMT:
930 	case SKL_MODULE_TYPE_MIC_SELECT:
931 		skl_set_base_outfmt_format(skl, module_config, *param_data);
932 		break;
933 
934 	case SKL_MODULE_TYPE_MIXER:
935 	case SKL_MODULE_TYPE_KPB:
936 		skl_set_base_module_format(skl, module_config, *param_data);
937 		break;
938 
939 	default:
940 		skl_set_base_module_format(skl, module_config, *param_data);
941 		break;
942 
943 	}
944 
945 	dev_dbg(skl->dev, "Module type=%d id=%d config size: %d bytes\n",
946 			module_config->m_type, module_config->id.module_id,
947 			param_size);
948 	print_hex_dump_debug("Module params:", DUMP_PREFIX_OFFSET, 8, 4,
949 			*param_data, param_size, false);
950 	return 0;
951 }
952 
953 static int skl_get_queue_index(struct skl_module_pin *mpin,
954 				struct skl_module_inst_id id, int max)
955 {
956 	int i;
957 
958 	for (i = 0; i < max; i++)  {
959 		if (mpin[i].id.module_id == id.module_id &&
960 			mpin[i].id.instance_id == id.instance_id)
961 			return i;
962 	}
963 
964 	return -EINVAL;
965 }
966 
967 /*
968  * Allocates queue for each module.
969  * if dynamic, the pin_index is allocated 0 to max_pin.
970  * In static, the pin_index is fixed based on module_id and instance id
971  */
972 static int skl_alloc_queue(struct skl_module_pin *mpin,
973 			struct skl_module_cfg *tgt_cfg, int max)
974 {
975 	int i;
976 	struct skl_module_inst_id id = tgt_cfg->id;
977 	/*
978 	 * if pin in dynamic, find first free pin
979 	 * otherwise find match module and instance id pin as topology will
980 	 * ensure a unique pin is assigned to this so no need to
981 	 * allocate/free
982 	 */
983 	for (i = 0; i < max; i++)  {
984 		if (mpin[i].is_dynamic) {
985 			if (!mpin[i].in_use &&
986 				mpin[i].pin_state == SKL_PIN_UNBIND) {
987 
988 				mpin[i].in_use = true;
989 				mpin[i].id.module_id = id.module_id;
990 				mpin[i].id.instance_id = id.instance_id;
991 				mpin[i].id.pvt_id = id.pvt_id;
992 				mpin[i].tgt_mcfg = tgt_cfg;
993 				return i;
994 			}
995 		} else {
996 			if (mpin[i].id.module_id == id.module_id &&
997 				mpin[i].id.instance_id == id.instance_id &&
998 				mpin[i].pin_state == SKL_PIN_UNBIND) {
999 
1000 				mpin[i].tgt_mcfg = tgt_cfg;
1001 				return i;
1002 			}
1003 		}
1004 	}
1005 
1006 	return -EINVAL;
1007 }
1008 
1009 static void skl_free_queue(struct skl_module_pin *mpin, int q_index)
1010 {
1011 	if (mpin[q_index].is_dynamic) {
1012 		mpin[q_index].in_use = false;
1013 		mpin[q_index].id.module_id = 0;
1014 		mpin[q_index].id.instance_id = 0;
1015 		mpin[q_index].id.pvt_id = 0;
1016 	}
1017 	mpin[q_index].pin_state = SKL_PIN_UNBIND;
1018 	mpin[q_index].tgt_mcfg = NULL;
1019 }
1020 
1021 /* Module state will be set to unint, if all the out pin state is UNBIND */
1022 
1023 static void skl_clear_module_state(struct skl_module_pin *mpin, int max,
1024 						struct skl_module_cfg *mcfg)
1025 {
1026 	int i;
1027 	bool found = false;
1028 
1029 	for (i = 0; i < max; i++)  {
1030 		if (mpin[i].pin_state == SKL_PIN_UNBIND)
1031 			continue;
1032 		found = true;
1033 		break;
1034 	}
1035 
1036 	if (!found)
1037 		mcfg->m_state = SKL_MODULE_INIT_DONE;
1038 	return;
1039 }
1040 
1041 /*
1042  * A module needs to be instanataited in DSP. A mdoule is present in a
1043  * collection of module referred as a PIPE.
1044  * We first calculate the module format, based on module type and then
1045  * invoke the DSP by sending IPC INIT_INSTANCE using ipc helper
1046  */
1047 int skl_init_module(struct skl_dev *skl,
1048 			struct skl_module_cfg *mconfig)
1049 {
1050 	u16 module_config_size = 0;
1051 	void *param_data = NULL;
1052 	int ret;
1053 	struct skl_ipc_init_instance_msg msg;
1054 
1055 	dev_dbg(skl->dev, "%s: module_id = %d instance=%d\n", __func__,
1056 		 mconfig->id.module_id, mconfig->id.pvt_id);
1057 
1058 	if (mconfig->pipe->state != SKL_PIPE_CREATED) {
1059 		dev_err(skl->dev, "Pipe not created state= %d pipe_id= %d\n",
1060 				 mconfig->pipe->state, mconfig->pipe->ppl_id);
1061 		return -EIO;
1062 	}
1063 
1064 	ret = skl_set_module_format(skl, mconfig,
1065 			&module_config_size, &param_data);
1066 	if (ret < 0) {
1067 		dev_err(skl->dev, "Failed to set module format ret=%d\n", ret);
1068 		return ret;
1069 	}
1070 
1071 	msg.module_id = mconfig->id.module_id;
1072 	msg.instance_id = mconfig->id.pvt_id;
1073 	msg.ppl_instance_id = mconfig->pipe->ppl_id;
1074 	msg.param_data_size = module_config_size;
1075 	msg.core_id = mconfig->core_id;
1076 	msg.domain = mconfig->domain;
1077 
1078 	ret = skl_ipc_init_instance(&skl->ipc, &msg, param_data);
1079 	if (ret < 0) {
1080 		dev_err(skl->dev, "Failed to init instance ret=%d\n", ret);
1081 		kfree(param_data);
1082 		return ret;
1083 	}
1084 	mconfig->m_state = SKL_MODULE_INIT_DONE;
1085 	kfree(param_data);
1086 	return ret;
1087 }
1088 
1089 static void skl_dump_bind_info(struct skl_dev *skl, struct skl_module_cfg
1090 	*src_module, struct skl_module_cfg *dst_module)
1091 {
1092 	dev_dbg(skl->dev, "%s: src module_id = %d  src_instance=%d\n",
1093 		__func__, src_module->id.module_id, src_module->id.pvt_id);
1094 	dev_dbg(skl->dev, "%s: dst_module=%d dst_instance=%d\n", __func__,
1095 		 dst_module->id.module_id, dst_module->id.pvt_id);
1096 
1097 	dev_dbg(skl->dev, "src_module state = %d dst module state = %d\n",
1098 		src_module->m_state, dst_module->m_state);
1099 }
1100 
1101 /*
1102  * On module freeup, we need to unbind the module with modules
1103  * it is already bind.
1104  * Find the pin allocated and unbind then using bind_unbind IPC
1105  */
1106 int skl_unbind_modules(struct skl_dev *skl,
1107 			struct skl_module_cfg *src_mcfg,
1108 			struct skl_module_cfg *dst_mcfg)
1109 {
1110 	int ret;
1111 	struct skl_ipc_bind_unbind_msg msg;
1112 	struct skl_module_inst_id src_id = src_mcfg->id;
1113 	struct skl_module_inst_id dst_id = dst_mcfg->id;
1114 	int in_max = dst_mcfg->module->max_input_pins;
1115 	int out_max = src_mcfg->module->max_output_pins;
1116 	int src_index, dst_index, src_pin_state, dst_pin_state;
1117 
1118 	skl_dump_bind_info(skl, src_mcfg, dst_mcfg);
1119 
1120 	/* get src queue index */
1121 	src_index = skl_get_queue_index(src_mcfg->m_out_pin, dst_id, out_max);
1122 	if (src_index < 0)
1123 		return 0;
1124 
1125 	msg.src_queue = src_index;
1126 
1127 	/* get dst queue index */
1128 	dst_index  = skl_get_queue_index(dst_mcfg->m_in_pin, src_id, in_max);
1129 	if (dst_index < 0)
1130 		return 0;
1131 
1132 	msg.dst_queue = dst_index;
1133 
1134 	src_pin_state = src_mcfg->m_out_pin[src_index].pin_state;
1135 	dst_pin_state = dst_mcfg->m_in_pin[dst_index].pin_state;
1136 
1137 	if (src_pin_state != SKL_PIN_BIND_DONE ||
1138 		dst_pin_state != SKL_PIN_BIND_DONE)
1139 		return 0;
1140 
1141 	msg.module_id = src_mcfg->id.module_id;
1142 	msg.instance_id = src_mcfg->id.pvt_id;
1143 	msg.dst_module_id = dst_mcfg->id.module_id;
1144 	msg.dst_instance_id = dst_mcfg->id.pvt_id;
1145 	msg.bind = false;
1146 
1147 	ret = skl_ipc_bind_unbind(&skl->ipc, &msg);
1148 	if (!ret) {
1149 		/* free queue only if unbind is success */
1150 		skl_free_queue(src_mcfg->m_out_pin, src_index);
1151 		skl_free_queue(dst_mcfg->m_in_pin, dst_index);
1152 
1153 		/*
1154 		 * check only if src module bind state, bind is
1155 		 * always from src -> sink
1156 		 */
1157 		skl_clear_module_state(src_mcfg->m_out_pin, out_max, src_mcfg);
1158 	}
1159 
1160 	return ret;
1161 }
1162 
1163 #define CPR_SINK_FMT_PARAM_ID 2
1164 
1165 /*
1166  * Once a module is instantiated it need to be 'bind' with other modules in
1167  * the pipeline. For binding we need to find the module pins which are bind
1168  * together
1169  * This function finds the pins and then sends bund_unbind IPC message to
1170  * DSP using IPC helper
1171  */
1172 int skl_bind_modules(struct skl_dev *skl,
1173 			struct skl_module_cfg *src_mcfg,
1174 			struct skl_module_cfg *dst_mcfg)
1175 {
1176 	int ret = 0;
1177 	struct skl_ipc_bind_unbind_msg msg;
1178 	int in_max = dst_mcfg->module->max_input_pins;
1179 	int out_max = src_mcfg->module->max_output_pins;
1180 	int src_index, dst_index;
1181 	struct skl_module_fmt *format;
1182 	struct skl_cpr_pin_fmt pin_fmt;
1183 	struct skl_module *module;
1184 	struct skl_module_iface *fmt;
1185 
1186 	skl_dump_bind_info(skl, src_mcfg, dst_mcfg);
1187 
1188 	if (src_mcfg->m_state < SKL_MODULE_INIT_DONE ||
1189 		dst_mcfg->m_state < SKL_MODULE_INIT_DONE)
1190 		return 0;
1191 
1192 	src_index = skl_alloc_queue(src_mcfg->m_out_pin, dst_mcfg, out_max);
1193 	if (src_index < 0)
1194 		return -EINVAL;
1195 
1196 	msg.src_queue = src_index;
1197 	dst_index = skl_alloc_queue(dst_mcfg->m_in_pin, src_mcfg, in_max);
1198 	if (dst_index < 0) {
1199 		skl_free_queue(src_mcfg->m_out_pin, src_index);
1200 		return -EINVAL;
1201 	}
1202 
1203 	/*
1204 	 * Copier module requires the separate large_config_set_ipc to
1205 	 * configure the pins other than 0
1206 	 */
1207 	if (src_mcfg->m_type == SKL_MODULE_TYPE_COPIER && src_index > 0) {
1208 		pin_fmt.sink_id = src_index;
1209 		module = src_mcfg->module;
1210 		fmt = &module->formats[src_mcfg->fmt_idx];
1211 
1212 		/* Input fmt is same as that of src module input cfg */
1213 		format = &fmt->inputs[0].fmt;
1214 		fill_pin_params(&(pin_fmt.src_fmt), format);
1215 
1216 		format = &fmt->outputs[src_index].fmt;
1217 		fill_pin_params(&(pin_fmt.dst_fmt), format);
1218 		ret = skl_set_module_params(skl, (void *)&pin_fmt,
1219 					sizeof(struct skl_cpr_pin_fmt),
1220 					CPR_SINK_FMT_PARAM_ID, src_mcfg);
1221 
1222 		if (ret < 0)
1223 			goto out;
1224 	}
1225 
1226 	msg.dst_queue = dst_index;
1227 
1228 	dev_dbg(skl->dev, "src queue = %d dst queue =%d\n",
1229 			 msg.src_queue, msg.dst_queue);
1230 
1231 	msg.module_id = src_mcfg->id.module_id;
1232 	msg.instance_id = src_mcfg->id.pvt_id;
1233 	msg.dst_module_id = dst_mcfg->id.module_id;
1234 	msg.dst_instance_id = dst_mcfg->id.pvt_id;
1235 	msg.bind = true;
1236 
1237 	ret = skl_ipc_bind_unbind(&skl->ipc, &msg);
1238 
1239 	if (!ret) {
1240 		src_mcfg->m_state = SKL_MODULE_BIND_DONE;
1241 		src_mcfg->m_out_pin[src_index].pin_state = SKL_PIN_BIND_DONE;
1242 		dst_mcfg->m_in_pin[dst_index].pin_state = SKL_PIN_BIND_DONE;
1243 		return ret;
1244 	}
1245 out:
1246 	/* error case , if IPC fails, clear the queue index */
1247 	skl_free_queue(src_mcfg->m_out_pin, src_index);
1248 	skl_free_queue(dst_mcfg->m_in_pin, dst_index);
1249 
1250 	return ret;
1251 }
1252 
1253 static int skl_set_pipe_state(struct skl_dev *skl, struct skl_pipe *pipe,
1254 	enum skl_ipc_pipeline_state state)
1255 {
1256 	dev_dbg(skl->dev, "%s: pipe_state = %d\n", __func__, state);
1257 
1258 	return skl_ipc_set_pipeline_state(&skl->ipc, pipe->ppl_id, state);
1259 }
1260 
1261 /*
1262  * A pipeline is a collection of modules. Before a module in instantiated a
1263  * pipeline needs to be created for it.
1264  * This function creates pipeline, by sending create pipeline IPC messages
1265  * to FW
1266  */
1267 int skl_create_pipeline(struct skl_dev *skl, struct skl_pipe *pipe)
1268 {
1269 	int ret;
1270 
1271 	dev_dbg(skl->dev, "%s: pipe_id = %d\n", __func__, pipe->ppl_id);
1272 
1273 	ret = skl_ipc_create_pipeline(&skl->ipc, pipe->memory_pages,
1274 				pipe->pipe_priority, pipe->ppl_id,
1275 				pipe->lp_mode);
1276 	if (ret < 0) {
1277 		dev_err(skl->dev, "Failed to create pipeline\n");
1278 		return ret;
1279 	}
1280 
1281 	pipe->state = SKL_PIPE_CREATED;
1282 
1283 	return 0;
1284 }
1285 
1286 /*
1287  * A pipeline needs to be deleted on cleanup. If a pipeline is running,
1288  * then pause it first. Before actual deletion, pipeline should enter
1289  * reset state. Finish the procedure by sending delete pipeline IPC.
1290  * DSP will stop the DMA engines and release resources
1291  */
1292 int skl_delete_pipe(struct skl_dev *skl, struct skl_pipe *pipe)
1293 {
1294 	int ret;
1295 
1296 	dev_dbg(skl->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
1297 
1298 	/* If pipe was not created in FW, do not try to delete it */
1299 	if (pipe->state < SKL_PIPE_CREATED)
1300 		return 0;
1301 
1302 	/* If pipe is started, do stop the pipe in FW. */
1303 	if (pipe->state >= SKL_PIPE_STARTED) {
1304 		ret = skl_set_pipe_state(skl, pipe, PPL_PAUSED);
1305 		if (ret < 0) {
1306 			dev_err(skl->dev, "Failed to stop pipeline\n");
1307 			return ret;
1308 		}
1309 
1310 		pipe->state = SKL_PIPE_PAUSED;
1311 	}
1312 
1313 	/* reset pipe state before deletion */
1314 	ret = skl_set_pipe_state(skl, pipe, PPL_RESET);
1315 	if (ret < 0) {
1316 		dev_err(skl->dev, "Failed to reset pipe ret=%d\n", ret);
1317 		return ret;
1318 	}
1319 
1320 	pipe->state = SKL_PIPE_RESET;
1321 
1322 	ret = skl_ipc_delete_pipeline(&skl->ipc, pipe->ppl_id);
1323 	if (ret < 0) {
1324 		dev_err(skl->dev, "Failed to delete pipeline\n");
1325 		return ret;
1326 	}
1327 
1328 	pipe->state = SKL_PIPE_INVALID;
1329 
1330 	return ret;
1331 }
1332 
1333 /*
1334  * A pipeline is also a scheduling entity in DSP which can be run, stopped
1335  * For processing data the pipe need to be run by sending IPC set pipe state
1336  * to DSP
1337  */
1338 int skl_run_pipe(struct skl_dev *skl, struct skl_pipe *pipe)
1339 {
1340 	int ret;
1341 
1342 	dev_dbg(skl->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
1343 
1344 	/* If pipe was not created in FW, do not try to pause or delete */
1345 	if (pipe->state < SKL_PIPE_CREATED)
1346 		return 0;
1347 
1348 	/* Pipe has to be paused before it is started */
1349 	ret = skl_set_pipe_state(skl, pipe, PPL_PAUSED);
1350 	if (ret < 0) {
1351 		dev_err(skl->dev, "Failed to pause pipe\n");
1352 		return ret;
1353 	}
1354 
1355 	pipe->state = SKL_PIPE_PAUSED;
1356 
1357 	ret = skl_set_pipe_state(skl, pipe, PPL_RUNNING);
1358 	if (ret < 0) {
1359 		dev_err(skl->dev, "Failed to start pipe\n");
1360 		return ret;
1361 	}
1362 
1363 	pipe->state = SKL_PIPE_STARTED;
1364 
1365 	return 0;
1366 }
1367 
1368 /*
1369  * Stop the pipeline by sending set pipe state IPC
1370  * DSP doesnt implement stop so we always send pause message
1371  */
1372 int skl_stop_pipe(struct skl_dev *skl, struct skl_pipe *pipe)
1373 {
1374 	int ret;
1375 
1376 	dev_dbg(skl->dev, "In %s pipe=%d\n", __func__, pipe->ppl_id);
1377 
1378 	/* If pipe was not created in FW, do not try to pause or delete */
1379 	if (pipe->state < SKL_PIPE_PAUSED)
1380 		return 0;
1381 
1382 	ret = skl_set_pipe_state(skl, pipe, PPL_PAUSED);
1383 	if (ret < 0) {
1384 		dev_dbg(skl->dev, "Failed to stop pipe\n");
1385 		return ret;
1386 	}
1387 
1388 	pipe->state = SKL_PIPE_PAUSED;
1389 
1390 	return 0;
1391 }
1392 
1393 /*
1394  * Reset the pipeline by sending set pipe state IPC this will reset the DMA
1395  * from the DSP side
1396  */
1397 int skl_reset_pipe(struct skl_dev *skl, struct skl_pipe *pipe)
1398 {
1399 	int ret;
1400 
1401 	/* If pipe was not created in FW, do not try to pause or delete */
1402 	if (pipe->state < SKL_PIPE_PAUSED)
1403 		return 0;
1404 
1405 	ret = skl_set_pipe_state(skl, pipe, PPL_RESET);
1406 	if (ret < 0) {
1407 		dev_dbg(skl->dev, "Failed to reset pipe ret=%d\n", ret);
1408 		return ret;
1409 	}
1410 
1411 	pipe->state = SKL_PIPE_RESET;
1412 
1413 	return 0;
1414 }
1415 
1416 /* Algo parameter set helper function */
1417 int skl_set_module_params(struct skl_dev *skl, u32 *params, int size,
1418 				u32 param_id, struct skl_module_cfg *mcfg)
1419 {
1420 	struct skl_ipc_large_config_msg msg;
1421 
1422 	msg.module_id = mcfg->id.module_id;
1423 	msg.instance_id = mcfg->id.pvt_id;
1424 	msg.param_data_size = size;
1425 	msg.large_param_id = param_id;
1426 
1427 	return skl_ipc_set_large_config(&skl->ipc, &msg, params);
1428 }
1429 
1430 int skl_get_module_params(struct skl_dev *skl, u32 *params, int size,
1431 			  u32 param_id, struct skl_module_cfg *mcfg)
1432 {
1433 	struct skl_ipc_large_config_msg msg;
1434 	size_t bytes = size;
1435 
1436 	msg.module_id = mcfg->id.module_id;
1437 	msg.instance_id = mcfg->id.pvt_id;
1438 	msg.param_data_size = size;
1439 	msg.large_param_id = param_id;
1440 
1441 	return skl_ipc_get_large_config(&skl->ipc, &msg, &params, &bytes);
1442 }
1443