blob: a1001a686aa9e705acf37cfc5bf48ad2792fbcb6 [file] [log] [blame]
Jeeja KPd255b092015-07-21 23:53:56 +05301/*
2 * skl-message.c - HDA DSP interface for FW registration, Pipe and Module
3 * configurations
4 *
5 * Copyright (C) 2015 Intel Corp
6 * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
7 * Jeeja KP <jeeja.kp@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as version 2, as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 */
19
20#include <linux/slab.h>
21#include <linux/pci.h>
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include "skl-sst-dsp.h"
25#include "skl-sst-ipc.h"
26#include "skl.h"
27#include "../common/sst-dsp.h"
28#include "../common/sst-dsp-priv.h"
Jeeja KP23db4722015-08-01 19:40:41 +053029#include "skl-topology.h"
30#include "skl-tplg-interface.h"
Jeeja KPd255b092015-07-21 23:53:56 +053031
32static int skl_alloc_dma_buf(struct device *dev,
33 struct snd_dma_buffer *dmab, size_t size)
34{
35 struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
36 struct hdac_bus *bus = ebus_to_hbus(ebus);
37
38 if (!bus)
39 return -ENODEV;
40
41 return bus->io_ops->dma_alloc_pages(bus, SNDRV_DMA_TYPE_DEV, size, dmab);
42}
43
44static int skl_free_dma_buf(struct device *dev, struct snd_dma_buffer *dmab)
45{
46 struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
47 struct hdac_bus *bus = ebus_to_hbus(ebus);
48
49 if (!bus)
50 return -ENODEV;
51
52 bus->io_ops->dma_free_pages(bus, dmab);
53
54 return 0;
55}
56
Jeeja KP4e109962015-10-22 23:22:39 +053057#define NOTIFICATION_PARAM_ID 3
58#define NOTIFICATION_MASK 0xf
59
60/* disable notfication for underruns/overruns from firmware module */
61static void skl_dsp_enable_notification(struct skl_sst *ctx, bool enable)
62{
63 struct notification_mask mask;
64 struct skl_ipc_large_config_msg msg = {0};
65
66 mask.notify = NOTIFICATION_MASK;
67 mask.enable = enable;
68
69 msg.large_param_id = NOTIFICATION_PARAM_ID;
70 msg.param_data_size = sizeof(mask);
71
72 skl_ipc_set_large_config(&ctx->ipc, &msg, (u32 *)&mask);
73}
74
Jeeja KPd255b092015-07-21 23:53:56 +053075int skl_init_dsp(struct skl *skl)
76{
77 void __iomem *mmio_base;
78 struct hdac_ext_bus *ebus = &skl->ebus;
79 struct hdac_bus *bus = ebus_to_hbus(ebus);
80 int irq = bus->irq;
81 struct skl_dsp_loader_ops loader_ops;
82 int ret;
83
84 loader_ops.alloc_dma_buf = skl_alloc_dma_buf;
85 loader_ops.free_dma_buf = skl_free_dma_buf;
86
87 /* enable ppcap interrupt */
88 snd_hdac_ext_bus_ppcap_enable(&skl->ebus, true);
89 snd_hdac_ext_bus_ppcap_int_enable(&skl->ebus, true);
90
91 /* read the BAR of the ADSP MMIO */
92 mmio_base = pci_ioremap_bar(skl->pci, 4);
93 if (mmio_base == NULL) {
94 dev_err(bus->dev, "ioremap error\n");
95 return -ENXIO;
96 }
97
98 ret = skl_sst_dsp_init(bus->dev, mmio_base, irq,
99 loader_ops, &skl->skl_sst);
Jeeja KP2ac454f2015-10-22 23:22:40 +0530100 if (ret < 0)
101 return ret;
102
Jeeja KP4e109962015-10-22 23:22:39 +0530103 skl_dsp_enable_notification(skl->skl_sst, false);
Jeeja KPd255b092015-07-21 23:53:56 +0530104 dev_dbg(bus->dev, "dsp registration status=%d\n", ret);
105
106 return ret;
107}
108
109void skl_free_dsp(struct skl *skl)
110{
111 struct hdac_ext_bus *ebus = &skl->ebus;
112 struct hdac_bus *bus = ebus_to_hbus(ebus);
113 struct skl_sst *ctx = skl->skl_sst;
114
115 /* disable ppcap interrupt */
116 snd_hdac_ext_bus_ppcap_int_enable(&skl->ebus, false);
117
118 skl_sst_dsp_cleanup(bus->dev, ctx);
119 if (ctx->dsp->addr.lpe)
120 iounmap(ctx->dsp->addr.lpe);
121}
122
123int skl_suspend_dsp(struct skl *skl)
124{
125 struct skl_sst *ctx = skl->skl_sst;
126 int ret;
127
128 /* if ppcap is not supported return 0 */
129 if (!skl->ebus.ppcap)
130 return 0;
131
132 ret = skl_dsp_sleep(ctx->dsp);
133 if (ret < 0)
134 return ret;
135
136 /* disable ppcap interrupt */
137 snd_hdac_ext_bus_ppcap_int_enable(&skl->ebus, false);
138 snd_hdac_ext_bus_ppcap_enable(&skl->ebus, false);
139
140 return 0;
141}
142
143int skl_resume_dsp(struct skl *skl)
144{
145 struct skl_sst *ctx = skl->skl_sst;
Jeeja KP4e109962015-10-22 23:22:39 +0530146 int ret;
Jeeja KPd255b092015-07-21 23:53:56 +0530147
148 /* if ppcap is not supported return 0 */
149 if (!skl->ebus.ppcap)
150 return 0;
151
152 /* enable ppcap interrupt */
153 snd_hdac_ext_bus_ppcap_enable(&skl->ebus, true);
154 snd_hdac_ext_bus_ppcap_int_enable(&skl->ebus, true);
155
Jeeja KP4e109962015-10-22 23:22:39 +0530156 ret = skl_dsp_wake(ctx->dsp);
157 if (ret < 0)
158 return ret;
159
160 skl_dsp_enable_notification(skl->skl_sst, false);
161 return ret;
Jeeja KPd255b092015-07-21 23:53:56 +0530162}
Jeeja KP23db4722015-08-01 19:40:41 +0530163
164enum skl_bitdepth skl_get_bit_depth(int params)
165{
166 switch (params) {
167 case 8:
168 return SKL_DEPTH_8BIT;
169
170 case 16:
171 return SKL_DEPTH_16BIT;
172
173 case 24:
174 return SKL_DEPTH_24BIT;
175
176 case 32:
177 return SKL_DEPTH_32BIT;
178
179 default:
180 return SKL_DEPTH_INVALID;
181
182 }
183}
184
185static u32 skl_create_channel_map(enum skl_ch_cfg ch_cfg)
186{
187 u32 config;
188
189 switch (ch_cfg) {
190 case SKL_CH_CFG_MONO:
191 config = (0xFFFFFFF0 | SKL_CHANNEL_LEFT);
192 break;
193
194 case SKL_CH_CFG_STEREO:
195 config = (0xFFFFFF00 | SKL_CHANNEL_LEFT
196 | (SKL_CHANNEL_RIGHT << 4));
197 break;
198
199 case SKL_CH_CFG_2_1:
200 config = (0xFFFFF000 | SKL_CHANNEL_LEFT
201 | (SKL_CHANNEL_RIGHT << 4)
202 | (SKL_CHANNEL_LFE << 8));
203 break;
204
205 case SKL_CH_CFG_3_0:
206 config = (0xFFFFF000 | SKL_CHANNEL_LEFT
207 | (SKL_CHANNEL_CENTER << 4)
208 | (SKL_CHANNEL_RIGHT << 8));
209 break;
210
211 case SKL_CH_CFG_3_1:
212 config = (0xFFFF0000 | SKL_CHANNEL_LEFT
213 | (SKL_CHANNEL_CENTER << 4)
214 | (SKL_CHANNEL_RIGHT << 8)
215 | (SKL_CHANNEL_LFE << 12));
216 break;
217
218 case SKL_CH_CFG_QUATRO:
219 config = (0xFFFF0000 | SKL_CHANNEL_LEFT
220 | (SKL_CHANNEL_RIGHT << 4)
221 | (SKL_CHANNEL_LEFT_SURROUND << 8)
222 | (SKL_CHANNEL_RIGHT_SURROUND << 12));
223 break;
224
225 case SKL_CH_CFG_4_0:
226 config = (0xFFFF0000 | SKL_CHANNEL_LEFT
227 | (SKL_CHANNEL_CENTER << 4)
228 | (SKL_CHANNEL_RIGHT << 8)
229 | (SKL_CHANNEL_CENTER_SURROUND << 12));
230 break;
231
232 case SKL_CH_CFG_5_0:
233 config = (0xFFF00000 | SKL_CHANNEL_LEFT
234 | (SKL_CHANNEL_CENTER << 4)
235 | (SKL_CHANNEL_RIGHT << 8)
236 | (SKL_CHANNEL_LEFT_SURROUND << 12)
237 | (SKL_CHANNEL_RIGHT_SURROUND << 16));
238 break;
239
240 case SKL_CH_CFG_5_1:
241 config = (0xFF000000 | SKL_CHANNEL_CENTER
242 | (SKL_CHANNEL_LEFT << 4)
243 | (SKL_CHANNEL_RIGHT << 8)
244 | (SKL_CHANNEL_LEFT_SURROUND << 12)
245 | (SKL_CHANNEL_RIGHT_SURROUND << 16)
246 | (SKL_CHANNEL_LFE << 20));
247 break;
248
249 case SKL_CH_CFG_DUAL_MONO:
250 config = (0xFFFFFF00 | SKL_CHANNEL_LEFT
251 | (SKL_CHANNEL_LEFT << 4));
252 break;
253
254 case SKL_CH_CFG_I2S_DUAL_STEREO_0:
255 config = (0xFFFFFF00 | SKL_CHANNEL_LEFT
256 | (SKL_CHANNEL_RIGHT << 4));
257 break;
258
259 case SKL_CH_CFG_I2S_DUAL_STEREO_1:
260 config = (0xFFFF00FF | (SKL_CHANNEL_LEFT << 8)
261 | (SKL_CHANNEL_RIGHT << 12));
262 break;
263
264 default:
265 config = 0xFFFFFFFF;
266 break;
267
268 }
269
270 return config;
271}
272
273/*
274 * Each module in DSP expects a base module configuration, which consists of
275 * PCM format information, which we calculate in driver and resource values
276 * which are read from widget information passed through topology binary
277 * This is send when we create a module with INIT_INSTANCE IPC msg
278 */
279static void skl_set_base_module_format(struct skl_sst *ctx,
280 struct skl_module_cfg *mconfig,
281 struct skl_base_cfg *base_cfg)
282{
283 struct skl_module_fmt *format = &mconfig->in_fmt;
284
285 base_cfg->audio_fmt.number_of_channels = (u8)format->channels;
286
287 base_cfg->audio_fmt.s_freq = format->s_freq;
288 base_cfg->audio_fmt.bit_depth = format->bit_depth;
289 base_cfg->audio_fmt.valid_bit_depth = format->valid_bit_depth;
290 base_cfg->audio_fmt.ch_cfg = format->ch_cfg;
291
292 dev_dbg(ctx->dev, "bit_depth=%x valid_bd=%x ch_config=%x\n",
293 format->bit_depth, format->valid_bit_depth,
294 format->ch_cfg);
295
296 base_cfg->audio_fmt.channel_map = skl_create_channel_map(
297 base_cfg->audio_fmt.ch_cfg);
298
299 base_cfg->audio_fmt.interleaving = SKL_INTERLEAVING_PER_CHANNEL;
300
301 base_cfg->cps = mconfig->mcps;
302 base_cfg->ibs = mconfig->ibs;
303 base_cfg->obs = mconfig->obs;
304}
305
306/*
307 * Copies copier capabilities into copier module and updates copier module
308 * config size.
309 */
310static void skl_copy_copier_caps(struct skl_module_cfg *mconfig,
311 struct skl_cpr_cfg *cpr_mconfig)
312{
313 if (mconfig->formats_config.caps_size == 0)
314 return;
315
316 memcpy(cpr_mconfig->gtw_cfg.config_data,
317 mconfig->formats_config.caps,
318 mconfig->formats_config.caps_size);
319
320 cpr_mconfig->gtw_cfg.config_length =
321 (mconfig->formats_config.caps_size) / 4;
322}
323
324/*
325 * Calculate the gatewat settings required for copier module, type of
326 * gateway and index of gateway to use
327 */
328static void skl_setup_cpr_gateway_cfg(struct skl_sst *ctx,
329 struct skl_module_cfg *mconfig,
330 struct skl_cpr_cfg *cpr_mconfig)
331{
332 union skl_connector_node_id node_id = {0};
Jeeja KPd7b18812015-10-22 23:22:38 +0530333 union skl_ssp_dma_node ssp_node = {0};
Jeeja KP23db4722015-08-01 19:40:41 +0530334 struct skl_pipe_params *params = mconfig->pipe->p_params;
335
336 switch (mconfig->dev_type) {
337 case SKL_DEVICE_BT:
338 node_id.node.dma_type =
339 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
340 SKL_DMA_I2S_LINK_OUTPUT_CLASS :
341 SKL_DMA_I2S_LINK_INPUT_CLASS;
342 node_id.node.vindex = params->host_dma_id +
343 (mconfig->vbus_id << 3);
344 break;
345
346 case SKL_DEVICE_I2S:
347 node_id.node.dma_type =
348 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
349 SKL_DMA_I2S_LINK_OUTPUT_CLASS :
350 SKL_DMA_I2S_LINK_INPUT_CLASS;
Jeeja KPd7b18812015-10-22 23:22:38 +0530351 ssp_node.dma_node.time_slot_index = mconfig->time_slot;
352 ssp_node.dma_node.i2s_instance = mconfig->vbus_id;
353 node_id.node.vindex = ssp_node.val;
Jeeja KP23db4722015-08-01 19:40:41 +0530354 break;
355
356 case SKL_DEVICE_DMIC:
357 node_id.node.dma_type = SKL_DMA_DMIC_LINK_INPUT_CLASS;
358 node_id.node.vindex = mconfig->vbus_id +
359 (mconfig->time_slot);
360 break;
361
362 case SKL_DEVICE_HDALINK:
363 node_id.node.dma_type =
364 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
365 SKL_DMA_HDA_LINK_OUTPUT_CLASS :
366 SKL_DMA_HDA_LINK_INPUT_CLASS;
367 node_id.node.vindex = params->link_dma_id;
368 break;
369
370 default:
371 node_id.node.dma_type =
372 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
373 SKL_DMA_HDA_HOST_OUTPUT_CLASS :
374 SKL_DMA_HDA_HOST_INPUT_CLASS;
375 node_id.node.vindex = params->host_dma_id;
376 break;
377 }
378
379 cpr_mconfig->gtw_cfg.node_id = node_id.val;
380
381 if (SKL_CONN_SOURCE == mconfig->hw_conn_type)
382 cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * mconfig->obs;
383 else
384 cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * mconfig->ibs;
385
386 cpr_mconfig->cpr_feature_mask = 0;
387 cpr_mconfig->gtw_cfg.config_length = 0;
388
389 skl_copy_copier_caps(mconfig, cpr_mconfig);
390}
391
392static void skl_setup_out_format(struct skl_sst *ctx,
393 struct skl_module_cfg *mconfig,
394 struct skl_audio_data_format *out_fmt)
395{
396 struct skl_module_fmt *format = &mconfig->out_fmt;
397
398 out_fmt->number_of_channels = (u8)format->channels;
399 out_fmt->s_freq = format->s_freq;
400 out_fmt->bit_depth = format->bit_depth;
401 out_fmt->valid_bit_depth = format->valid_bit_depth;
402 out_fmt->ch_cfg = format->ch_cfg;
403
404 out_fmt->channel_map = skl_create_channel_map(out_fmt->ch_cfg);
405 out_fmt->interleaving = SKL_INTERLEAVING_PER_CHANNEL;
406
407 dev_dbg(ctx->dev, "copier out format chan=%d fre=%d bitdepth=%d\n",
408 out_fmt->number_of_channels, format->s_freq, format->bit_depth);
409}
410
411/*
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530412 * DSP needs SRC module for frequency conversion, SRC takes base module
413 * configuration and the target frequency as extra parameter passed as src
414 * config
415 */
416static void skl_set_src_format(struct skl_sst *ctx,
417 struct skl_module_cfg *mconfig,
418 struct skl_src_module_cfg *src_mconfig)
419{
420 struct skl_module_fmt *fmt = &mconfig->out_fmt;
421
422 skl_set_base_module_format(ctx, mconfig,
423 (struct skl_base_cfg *)src_mconfig);
424
425 src_mconfig->src_cfg = fmt->s_freq;
426}
427
428/*
429 * DSP needs updown module to do channel conversion. updown module take base
430 * module configuration and channel configuration
431 * It also take coefficients and now we have defaults applied here
432 */
433static void skl_set_updown_mixer_format(struct skl_sst *ctx,
434 struct skl_module_cfg *mconfig,
435 struct skl_up_down_mixer_cfg *mixer_mconfig)
436{
437 struct skl_module_fmt *fmt = &mconfig->out_fmt;
438 int i = 0;
439
440 skl_set_base_module_format(ctx, mconfig,
441 (struct skl_base_cfg *)mixer_mconfig);
442 mixer_mconfig->out_ch_cfg = fmt->ch_cfg;
443
444 /* Select F/W default coefficient */
445 mixer_mconfig->coeff_sel = 0x0;
446
447 /* User coeff, don't care since we are selecting F/W defaults */
448 for (i = 0; i < UP_DOWN_MIXER_MAX_COEFF; i++)
449 mixer_mconfig->coeff[i] = 0xDEADBEEF;
450}
451
452/*
Jeeja KP23db4722015-08-01 19:40:41 +0530453 * 'copier' is DSP internal module which copies data from Host DMA (HDA host
454 * dma) or link (hda link, SSP, PDM)
455 * Here we calculate the copier module parameters, like PCM format, output
456 * format, gateway settings
457 * copier_module_config is sent as input buffer with INIT_INSTANCE IPC msg
458 */
459static void skl_set_copier_format(struct skl_sst *ctx,
460 struct skl_module_cfg *mconfig,
461 struct skl_cpr_cfg *cpr_mconfig)
462{
463 struct skl_audio_data_format *out_fmt = &cpr_mconfig->out_fmt;
464 struct skl_base_cfg *base_cfg = (struct skl_base_cfg *)cpr_mconfig;
465
466 skl_set_base_module_format(ctx, mconfig, base_cfg);
467
468 skl_setup_out_format(ctx, mconfig, out_fmt);
469 skl_setup_cpr_gateway_cfg(ctx, mconfig, cpr_mconfig);
470}
471
472static u16 skl_get_module_param_size(struct skl_sst *ctx,
473 struct skl_module_cfg *mconfig)
474{
475 u16 param_size;
476
477 switch (mconfig->m_type) {
478 case SKL_MODULE_TYPE_COPIER:
479 param_size = sizeof(struct skl_cpr_cfg);
480 param_size += mconfig->formats_config.caps_size;
481 return param_size;
482
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530483 case SKL_MODULE_TYPE_SRCINT:
484 return sizeof(struct skl_src_module_cfg);
485
486 case SKL_MODULE_TYPE_UPDWMIX:
487 return sizeof(struct skl_up_down_mixer_cfg);
488
Jeeja KP23db4722015-08-01 19:40:41 +0530489 default:
490 /*
491 * return only base cfg when no specific module type is
492 * specified
493 */
494 return sizeof(struct skl_base_cfg);
495 }
496
497 return 0;
498}
499
500/*
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530501 * DSP firmware supports various modules like copier, SRC, updown etc.
502 * These modules required various parameters to be calculated and sent for
503 * the module initialization to DSP. By default a generic module needs only
504 * base module format configuration
Jeeja KP23db4722015-08-01 19:40:41 +0530505 */
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530506
Jeeja KP23db4722015-08-01 19:40:41 +0530507static int skl_set_module_format(struct skl_sst *ctx,
508 struct skl_module_cfg *module_config,
509 u16 *module_config_size,
510 void **param_data)
511{
512 u16 param_size;
513
514 param_size = skl_get_module_param_size(ctx, module_config);
515
516 *param_data = kzalloc(param_size, GFP_KERNEL);
517 if (NULL == *param_data)
518 return -ENOMEM;
519
520 *module_config_size = param_size;
521
522 switch (module_config->m_type) {
523 case SKL_MODULE_TYPE_COPIER:
524 skl_set_copier_format(ctx, module_config, *param_data);
525 break;
526
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530527 case SKL_MODULE_TYPE_SRCINT:
528 skl_set_src_format(ctx, module_config, *param_data);
529 break;
530
531 case SKL_MODULE_TYPE_UPDWMIX:
532 skl_set_updown_mixer_format(ctx, module_config, *param_data);
533 break;
534
Jeeja KP23db4722015-08-01 19:40:41 +0530535 default:
536 skl_set_base_module_format(ctx, module_config, *param_data);
537 break;
538
539 }
540
541 dev_dbg(ctx->dev, "Module type=%d config size: %d bytes\n",
542 module_config->id.module_id, param_size);
543 print_hex_dump(KERN_DEBUG, "Module params:", DUMP_PREFIX_OFFSET, 8, 4,
544 *param_data, param_size, false);
545 return 0;
546}
547
548static int skl_get_queue_index(struct skl_module_pin *mpin,
549 struct skl_module_inst_id id, int max)
550{
551 int i;
552
553 for (i = 0; i < max; i++) {
554 if (mpin[i].id.module_id == id.module_id &&
555 mpin[i].id.instance_id == id.instance_id)
556 return i;
557 }
558
559 return -EINVAL;
560}
561
562/*
563 * Allocates queue for each module.
564 * if dynamic, the pin_index is allocated 0 to max_pin.
565 * In static, the pin_index is fixed based on module_id and instance id
566 */
567static int skl_alloc_queue(struct skl_module_pin *mpin,
568 struct skl_module_inst_id id, int max)
569{
570 int i;
571
572 /*
573 * if pin in dynamic, find first free pin
574 * otherwise find match module and instance id pin as topology will
575 * ensure a unique pin is assigned to this so no need to
576 * allocate/free
577 */
578 for (i = 0; i < max; i++) {
579 if (mpin[i].is_dynamic) {
580 if (!mpin[i].in_use) {
581 mpin[i].in_use = true;
582 mpin[i].id.module_id = id.module_id;
583 mpin[i].id.instance_id = id.instance_id;
584 return i;
585 }
586 } else {
587 if (mpin[i].id.module_id == id.module_id &&
588 mpin[i].id.instance_id == id.instance_id)
589 return i;
590 }
591 }
592
593 return -EINVAL;
594}
595
596static void skl_free_queue(struct skl_module_pin *mpin, int q_index)
597{
598 if (mpin[q_index].is_dynamic) {
599 mpin[q_index].in_use = false;
600 mpin[q_index].id.module_id = 0;
601 mpin[q_index].id.instance_id = 0;
602 }
603}
Jeeja KPbeb73b22015-08-01 19:40:43 +0530604
605/*
606 * A module needs to be instanataited in DSP. A mdoule is present in a
607 * collection of module referred as a PIPE.
608 * We first calculate the module format, based on module type and then
609 * invoke the DSP by sending IPC INIT_INSTANCE using ipc helper
610 */
611int skl_init_module(struct skl_sst *ctx,
612 struct skl_module_cfg *mconfig, char *param)
613{
614 u16 module_config_size = 0;
615 void *param_data = NULL;
616 int ret;
617 struct skl_ipc_init_instance_msg msg;
618
619 dev_dbg(ctx->dev, "%s: module_id = %d instance=%d\n", __func__,
620 mconfig->id.module_id, mconfig->id.instance_id);
621
622 if (mconfig->pipe->state != SKL_PIPE_CREATED) {
623 dev_err(ctx->dev, "Pipe not created state= %d pipe_id= %d\n",
624 mconfig->pipe->state, mconfig->pipe->ppl_id);
625 return -EIO;
626 }
627
628 ret = skl_set_module_format(ctx, mconfig,
629 &module_config_size, &param_data);
630 if (ret < 0) {
631 dev_err(ctx->dev, "Failed to set module format ret=%d\n", ret);
632 return ret;
633 }
634
635 msg.module_id = mconfig->id.module_id;
636 msg.instance_id = mconfig->id.instance_id;
637 msg.ppl_instance_id = mconfig->pipe->ppl_id;
638 msg.param_data_size = module_config_size;
639 msg.core_id = mconfig->core_id;
640
641 ret = skl_ipc_init_instance(&ctx->ipc, &msg, param_data);
642 if (ret < 0) {
643 dev_err(ctx->dev, "Failed to init instance ret=%d\n", ret);
644 kfree(param_data);
645 return ret;
646 }
647 mconfig->m_state = SKL_MODULE_INIT_DONE;
648
649 return ret;
650}
651
652static void skl_dump_bind_info(struct skl_sst *ctx, struct skl_module_cfg
653 *src_module, struct skl_module_cfg *dst_module)
654{
655 dev_dbg(ctx->dev, "%s: src module_id = %d src_instance=%d\n",
656 __func__, src_module->id.module_id, src_module->id.instance_id);
657 dev_dbg(ctx->dev, "%s: dst_module=%d dst_instacne=%d\n", __func__,
658 dst_module->id.module_id, dst_module->id.instance_id);
659
660 dev_dbg(ctx->dev, "src_module state = %d dst module state = %d\n",
661 src_module->m_state, dst_module->m_state);
662}
663
664/*
665 * On module freeup, we need to unbind the module with modules
666 * it is already bind.
667 * Find the pin allocated and unbind then using bind_unbind IPC
668 */
669int skl_unbind_modules(struct skl_sst *ctx,
670 struct skl_module_cfg *src_mcfg,
671 struct skl_module_cfg *dst_mcfg)
672{
673 int ret;
674 struct skl_ipc_bind_unbind_msg msg;
675 struct skl_module_inst_id src_id = src_mcfg->id;
676 struct skl_module_inst_id dst_id = dst_mcfg->id;
677 int in_max = dst_mcfg->max_in_queue;
678 int out_max = src_mcfg->max_out_queue;
679 int src_index, dst_index;
680
681 skl_dump_bind_info(ctx, src_mcfg, dst_mcfg);
682
683 if (src_mcfg->m_state != SKL_MODULE_BIND_DONE)
684 return 0;
685
686 /*
687 * if intra module unbind, check if both modules are BIND,
688 * then send unbind
689 */
690 if ((src_mcfg->pipe->ppl_id != dst_mcfg->pipe->ppl_id) &&
691 dst_mcfg->m_state != SKL_MODULE_BIND_DONE)
692 return 0;
693 else if (src_mcfg->m_state < SKL_MODULE_INIT_DONE &&
694 dst_mcfg->m_state < SKL_MODULE_INIT_DONE)
695 return 0;
696
697 /* get src queue index */
698 src_index = skl_get_queue_index(src_mcfg->m_out_pin, dst_id, out_max);
699 if (src_index < 0)
700 return -EINVAL;
701
702 msg.src_queue = src_mcfg->m_out_pin[src_index].pin_index;
703
704 /* get dst queue index */
705 dst_index = skl_get_queue_index(dst_mcfg->m_in_pin, src_id, in_max);
706 if (dst_index < 0)
707 return -EINVAL;
708
709 msg.dst_queue = dst_mcfg->m_in_pin[dst_index].pin_index;
710
711 msg.module_id = src_mcfg->id.module_id;
712 msg.instance_id = src_mcfg->id.instance_id;
713 msg.dst_module_id = dst_mcfg->id.module_id;
714 msg.dst_instance_id = dst_mcfg->id.instance_id;
715 msg.bind = false;
716
717 ret = skl_ipc_bind_unbind(&ctx->ipc, &msg);
718 if (!ret) {
719 src_mcfg->m_state = SKL_MODULE_UNINIT;
720 /* free queue only if unbind is success */
721 skl_free_queue(src_mcfg->m_out_pin, src_index);
722 skl_free_queue(dst_mcfg->m_in_pin, dst_index);
723 }
724
725 return ret;
726}
727
728/*
729 * Once a module is instantiated it need to be 'bind' with other modules in
730 * the pipeline. For binding we need to find the module pins which are bind
731 * together
732 * This function finds the pins and then sends bund_unbind IPC message to
733 * DSP using IPC helper
734 */
735int skl_bind_modules(struct skl_sst *ctx,
736 struct skl_module_cfg *src_mcfg,
737 struct skl_module_cfg *dst_mcfg)
738{
739 int ret;
740 struct skl_ipc_bind_unbind_msg msg;
741 struct skl_module_inst_id src_id = src_mcfg->id;
742 struct skl_module_inst_id dst_id = dst_mcfg->id;
743 int in_max = dst_mcfg->max_in_queue;
744 int out_max = src_mcfg->max_out_queue;
745 int src_index, dst_index;
746
747 skl_dump_bind_info(ctx, src_mcfg, dst_mcfg);
748
749 if (src_mcfg->m_state < SKL_MODULE_INIT_DONE &&
750 dst_mcfg->m_state < SKL_MODULE_INIT_DONE)
751 return 0;
752
753 src_index = skl_alloc_queue(src_mcfg->m_out_pin, dst_id, out_max);
754 if (src_index < 0)
755 return -EINVAL;
756
757 msg.src_queue = src_mcfg->m_out_pin[src_index].pin_index;
758 dst_index = skl_alloc_queue(dst_mcfg->m_in_pin, src_id, in_max);
759 if (dst_index < 0) {
760 skl_free_queue(src_mcfg->m_out_pin, src_index);
761 return -EINVAL;
762 }
763
764 msg.dst_queue = dst_mcfg->m_in_pin[dst_index].pin_index;
765
766 dev_dbg(ctx->dev, "src queue = %d dst queue =%d\n",
767 msg.src_queue, msg.dst_queue);
768
769 msg.module_id = src_mcfg->id.module_id;
770 msg.instance_id = src_mcfg->id.instance_id;
771 msg.dst_module_id = dst_mcfg->id.module_id;
772 msg.dst_instance_id = dst_mcfg->id.instance_id;
773 msg.bind = true;
774
775 ret = skl_ipc_bind_unbind(&ctx->ipc, &msg);
776
777 if (!ret) {
778 src_mcfg->m_state = SKL_MODULE_BIND_DONE;
779 } else {
780 /* error case , if IPC fails, clear the queue index */
781 skl_free_queue(src_mcfg->m_out_pin, src_index);
782 skl_free_queue(dst_mcfg->m_in_pin, dst_index);
783 }
784
785 return ret;
786}
Jeeja KPc9b1e832015-08-01 19:40:44 +0530787
788static int skl_set_pipe_state(struct skl_sst *ctx, struct skl_pipe *pipe,
789 enum skl_ipc_pipeline_state state)
790{
791 dev_dbg(ctx->dev, "%s: pipe_satate = %d\n", __func__, state);
792
793 return skl_ipc_set_pipeline_state(&ctx->ipc, pipe->ppl_id, state);
794}
795
796/*
797 * A pipeline is a collection of modules. Before a module in instantiated a
798 * pipeline needs to be created for it.
799 * This function creates pipeline, by sending create pipeline IPC messages
800 * to FW
801 */
802int skl_create_pipeline(struct skl_sst *ctx, struct skl_pipe *pipe)
803{
804 int ret;
805
806 dev_dbg(ctx->dev, "%s: pipe_id = %d\n", __func__, pipe->ppl_id);
807
808 ret = skl_ipc_create_pipeline(&ctx->ipc, pipe->memory_pages,
809 pipe->pipe_priority, pipe->ppl_id);
810 if (ret < 0) {
811 dev_err(ctx->dev, "Failed to create pipeline\n");
812 return ret;
813 }
814
815 pipe->state = SKL_PIPE_CREATED;
816
817 return 0;
818}
819
820/*
821 * A pipeline needs to be deleted on cleanup. If a pipeline is running, then
822 * pause the pipeline first and then delete it
823 * The pipe delete is done by sending delete pipeline IPC. DSP will stop the
824 * DMA engines and releases resources
825 */
826int skl_delete_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
827{
828 int ret;
829
830 dev_dbg(ctx->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
831
832 /* If pipe is not started, do not try to stop the pipe in FW. */
833 if (pipe->state > SKL_PIPE_STARTED) {
834 ret = skl_set_pipe_state(ctx, pipe, PPL_PAUSED);
835 if (ret < 0) {
836 dev_err(ctx->dev, "Failed to stop pipeline\n");
837 return ret;
838 }
839
840 pipe->state = SKL_PIPE_PAUSED;
841 } else {
842 /* If pipe was not created in FW, do not try to delete it */
843 if (pipe->state < SKL_PIPE_CREATED)
844 return 0;
845
846 ret = skl_ipc_delete_pipeline(&ctx->ipc, pipe->ppl_id);
847 if (ret < 0)
848 dev_err(ctx->dev, "Failed to delete pipeline\n");
849 }
850
851 return ret;
852}
853
854/*
855 * A pipeline is also a scheduling entity in DSP which can be run, stopped
856 * For processing data the pipe need to be run by sending IPC set pipe state
857 * to DSP
858 */
859int skl_run_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
860{
861 int ret;
862
863 dev_dbg(ctx->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
864
865 /* If pipe was not created in FW, do not try to pause or delete */
866 if (pipe->state < SKL_PIPE_CREATED)
867 return 0;
868
869 /* Pipe has to be paused before it is started */
870 ret = skl_set_pipe_state(ctx, pipe, PPL_PAUSED);
871 if (ret < 0) {
872 dev_err(ctx->dev, "Failed to pause pipe\n");
873 return ret;
874 }
875
876 pipe->state = SKL_PIPE_PAUSED;
877
878 ret = skl_set_pipe_state(ctx, pipe, PPL_RUNNING);
879 if (ret < 0) {
880 dev_err(ctx->dev, "Failed to start pipe\n");
881 return ret;
882 }
883
884 pipe->state = SKL_PIPE_STARTED;
885
886 return 0;
887}
888
889/*
890 * Stop the pipeline by sending set pipe state IPC
891 * DSP doesnt implement stop so we always send pause message
892 */
893int skl_stop_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
894{
895 int ret;
896
897 dev_dbg(ctx->dev, "In %s pipe=%d\n", __func__, pipe->ppl_id);
898
899 /* If pipe was not created in FW, do not try to pause or delete */
900 if (pipe->state < SKL_PIPE_PAUSED)
901 return 0;
902
903 ret = skl_set_pipe_state(ctx, pipe, PPL_PAUSED);
904 if (ret < 0) {
905 dev_dbg(ctx->dev, "Failed to stop pipe\n");
906 return ret;
907 }
908
909 pipe->state = SKL_PIPE_CREATED;
910
911 return 0;
912}