blob: 737934cc620d6dddaaab1bc4d22d7c105e7b1049 [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,
Vinod Koulaecf6fd2015-11-05 21:34:15 +053099 skl->fw_name, 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
Jeeja KP23db4722015-08-01 19:40:41 +0530185/*
186 * Each module in DSP expects a base module configuration, which consists of
187 * PCM format information, which we calculate in driver and resource values
188 * which are read from widget information passed through topology binary
189 * This is send when we create a module with INIT_INSTANCE IPC msg
190 */
191static void skl_set_base_module_format(struct skl_sst *ctx,
192 struct skl_module_cfg *mconfig,
193 struct skl_base_cfg *base_cfg)
194{
Hardik T Shah4cd98992015-10-27 09:22:55 +0900195 struct skl_module_fmt *format = &mconfig->in_fmt[0];
Jeeja KP23db4722015-08-01 19:40:41 +0530196
197 base_cfg->audio_fmt.number_of_channels = (u8)format->channels;
198
199 base_cfg->audio_fmt.s_freq = format->s_freq;
200 base_cfg->audio_fmt.bit_depth = format->bit_depth;
201 base_cfg->audio_fmt.valid_bit_depth = format->valid_bit_depth;
202 base_cfg->audio_fmt.ch_cfg = format->ch_cfg;
203
204 dev_dbg(ctx->dev, "bit_depth=%x valid_bd=%x ch_config=%x\n",
205 format->bit_depth, format->valid_bit_depth,
206 format->ch_cfg);
207
Jeeja KP3e81f1a2015-10-27 09:22:59 +0900208 base_cfg->audio_fmt.channel_map = format->ch_map;
Jeeja KP23db4722015-08-01 19:40:41 +0530209
Jeeja KP3e81f1a2015-10-27 09:22:59 +0900210 base_cfg->audio_fmt.interleaving = format->interleaving_style;
Jeeja KP23db4722015-08-01 19:40:41 +0530211
212 base_cfg->cps = mconfig->mcps;
213 base_cfg->ibs = mconfig->ibs;
214 base_cfg->obs = mconfig->obs;
Jeeja KPb18c4582015-12-03 23:29:51 +0530215 base_cfg->is_pages = mconfig->mem_pages;
Jeeja KP23db4722015-08-01 19:40:41 +0530216}
217
218/*
219 * Copies copier capabilities into copier module and updates copier module
220 * config size.
221 */
222static void skl_copy_copier_caps(struct skl_module_cfg *mconfig,
223 struct skl_cpr_cfg *cpr_mconfig)
224{
225 if (mconfig->formats_config.caps_size == 0)
226 return;
227
228 memcpy(cpr_mconfig->gtw_cfg.config_data,
229 mconfig->formats_config.caps,
230 mconfig->formats_config.caps_size);
231
232 cpr_mconfig->gtw_cfg.config_length =
233 (mconfig->formats_config.caps_size) / 4;
234}
235
Jeeja KPbfa764a2015-10-22 23:22:41 +0530236#define SKL_NON_GATEWAY_CPR_NODE_ID 0xFFFFFFFF
Jeeja KP23db4722015-08-01 19:40:41 +0530237/*
238 * Calculate the gatewat settings required for copier module, type of
239 * gateway and index of gateway to use
240 */
Dharageswari.R4fdf8102016-02-05 12:19:05 +0530241static u32 skl_get_node_id(struct skl_sst *ctx,
242 struct skl_module_cfg *mconfig)
Jeeja KP23db4722015-08-01 19:40:41 +0530243{
244 union skl_connector_node_id node_id = {0};
Jeeja KPd7b18812015-10-22 23:22:38 +0530245 union skl_ssp_dma_node ssp_node = {0};
Jeeja KP23db4722015-08-01 19:40:41 +0530246 struct skl_pipe_params *params = mconfig->pipe->p_params;
247
248 switch (mconfig->dev_type) {
249 case SKL_DEVICE_BT:
250 node_id.node.dma_type =
251 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
252 SKL_DMA_I2S_LINK_OUTPUT_CLASS :
253 SKL_DMA_I2S_LINK_INPUT_CLASS;
254 node_id.node.vindex = params->host_dma_id +
255 (mconfig->vbus_id << 3);
256 break;
257
258 case SKL_DEVICE_I2S:
259 node_id.node.dma_type =
260 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
261 SKL_DMA_I2S_LINK_OUTPUT_CLASS :
262 SKL_DMA_I2S_LINK_INPUT_CLASS;
Jeeja KPd7b18812015-10-22 23:22:38 +0530263 ssp_node.dma_node.time_slot_index = mconfig->time_slot;
264 ssp_node.dma_node.i2s_instance = mconfig->vbus_id;
265 node_id.node.vindex = ssp_node.val;
Jeeja KP23db4722015-08-01 19:40:41 +0530266 break;
267
268 case SKL_DEVICE_DMIC:
269 node_id.node.dma_type = SKL_DMA_DMIC_LINK_INPUT_CLASS;
270 node_id.node.vindex = mconfig->vbus_id +
271 (mconfig->time_slot);
272 break;
273
274 case SKL_DEVICE_HDALINK:
275 node_id.node.dma_type =
276 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
277 SKL_DMA_HDA_LINK_OUTPUT_CLASS :
278 SKL_DMA_HDA_LINK_INPUT_CLASS;
279 node_id.node.vindex = params->link_dma_id;
280 break;
281
Jeeja KPbfa764a2015-10-22 23:22:41 +0530282 case SKL_DEVICE_HDAHOST:
Jeeja KP23db4722015-08-01 19:40:41 +0530283 node_id.node.dma_type =
284 (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
285 SKL_DMA_HDA_HOST_OUTPUT_CLASS :
286 SKL_DMA_HDA_HOST_INPUT_CLASS;
287 node_id.node.vindex = params->host_dma_id;
288 break;
Jeeja KPbfa764a2015-10-22 23:22:41 +0530289
290 default:
Dharageswari.R4fdf8102016-02-05 12:19:05 +0530291 node_id.val = 0xFFFFFFFF;
292 break;
293 }
294
295 return node_id.val;
296}
297
298static void skl_setup_cpr_gateway_cfg(struct skl_sst *ctx,
299 struct skl_module_cfg *mconfig,
300 struct skl_cpr_cfg *cpr_mconfig)
301{
302 cpr_mconfig->gtw_cfg.node_id = skl_get_node_id(ctx, mconfig);
303
304 if (cpr_mconfig->gtw_cfg.node_id == SKL_NON_GATEWAY_CPR_NODE_ID) {
Jeeja KPbfa764a2015-10-22 23:22:41 +0530305 cpr_mconfig->cpr_feature_mask = 0;
306 return;
Jeeja KP23db4722015-08-01 19:40:41 +0530307 }
308
Jeeja KP23db4722015-08-01 19:40:41 +0530309 if (SKL_CONN_SOURCE == mconfig->hw_conn_type)
310 cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * mconfig->obs;
311 else
312 cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * mconfig->ibs;
313
314 cpr_mconfig->cpr_feature_mask = 0;
315 cpr_mconfig->gtw_cfg.config_length = 0;
316
317 skl_copy_copier_caps(mconfig, cpr_mconfig);
318}
319
Dharageswari.Rc115fa52016-02-05 12:19:07 +0530320#define DMA_CONTROL_ID 5
321
322int skl_dsp_set_dma_control(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
323{
324 struct skl_dma_control *dma_ctrl;
325 struct skl_i2s_config_blob config_blob;
326 struct skl_ipc_large_config_msg msg = {0};
327 int err = 0;
328
329
330 /*
331 * if blob size is same as capablity size, then no dma control
332 * present so return
333 */
334 if (mconfig->formats_config.caps_size == sizeof(config_blob))
335 return 0;
336
337 msg.large_param_id = DMA_CONTROL_ID;
338 msg.param_data_size = sizeof(struct skl_dma_control) +
339 mconfig->formats_config.caps_size;
340
341 dma_ctrl = kzalloc(msg.param_data_size, GFP_KERNEL);
342 if (dma_ctrl == NULL)
343 return -ENOMEM;
344
345 dma_ctrl->node_id = skl_get_node_id(ctx, mconfig);
346
347 /* size in dwords */
348 dma_ctrl->config_length = sizeof(config_blob) / 4;
349
350 memcpy(dma_ctrl->config_data, mconfig->formats_config.caps,
351 mconfig->formats_config.caps_size);
352
353 err = skl_ipc_set_large_config(&ctx->ipc, &msg, (u32 *)dma_ctrl);
354
355 kfree(dma_ctrl);
356
357 return err;
358}
359
Jeeja KP23db4722015-08-01 19:40:41 +0530360static void skl_setup_out_format(struct skl_sst *ctx,
361 struct skl_module_cfg *mconfig,
362 struct skl_audio_data_format *out_fmt)
363{
Hardik T Shah4cd98992015-10-27 09:22:55 +0900364 struct skl_module_fmt *format = &mconfig->out_fmt[0];
Jeeja KP23db4722015-08-01 19:40:41 +0530365
366 out_fmt->number_of_channels = (u8)format->channels;
367 out_fmt->s_freq = format->s_freq;
368 out_fmt->bit_depth = format->bit_depth;
369 out_fmt->valid_bit_depth = format->valid_bit_depth;
370 out_fmt->ch_cfg = format->ch_cfg;
371
Jeeja KP3e81f1a2015-10-27 09:22:59 +0900372 out_fmt->channel_map = format->ch_map;
373 out_fmt->interleaving = format->interleaving_style;
374 out_fmt->sample_type = format->sample_type;
Jeeja KP23db4722015-08-01 19:40:41 +0530375
376 dev_dbg(ctx->dev, "copier out format chan=%d fre=%d bitdepth=%d\n",
377 out_fmt->number_of_channels, format->s_freq, format->bit_depth);
378}
379
380/*
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530381 * DSP needs SRC module for frequency conversion, SRC takes base module
382 * configuration and the target frequency as extra parameter passed as src
383 * config
384 */
385static void skl_set_src_format(struct skl_sst *ctx,
386 struct skl_module_cfg *mconfig,
387 struct skl_src_module_cfg *src_mconfig)
388{
Hardik T Shah4cd98992015-10-27 09:22:55 +0900389 struct skl_module_fmt *fmt = &mconfig->out_fmt[0];
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530390
391 skl_set_base_module_format(ctx, mconfig,
392 (struct skl_base_cfg *)src_mconfig);
393
394 src_mconfig->src_cfg = fmt->s_freq;
395}
396
397/*
398 * DSP needs updown module to do channel conversion. updown module take base
399 * module configuration and channel configuration
400 * It also take coefficients and now we have defaults applied here
401 */
402static void skl_set_updown_mixer_format(struct skl_sst *ctx,
403 struct skl_module_cfg *mconfig,
404 struct skl_up_down_mixer_cfg *mixer_mconfig)
405{
Hardik T Shah4cd98992015-10-27 09:22:55 +0900406 struct skl_module_fmt *fmt = &mconfig->out_fmt[0];
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530407 int i = 0;
408
409 skl_set_base_module_format(ctx, mconfig,
410 (struct skl_base_cfg *)mixer_mconfig);
411 mixer_mconfig->out_ch_cfg = fmt->ch_cfg;
412
413 /* Select F/W default coefficient */
414 mixer_mconfig->coeff_sel = 0x0;
415
416 /* User coeff, don't care since we are selecting F/W defaults */
417 for (i = 0; i < UP_DOWN_MIXER_MAX_COEFF; i++)
418 mixer_mconfig->coeff[i] = 0xDEADBEEF;
419}
420
421/*
Jeeja KP23db4722015-08-01 19:40:41 +0530422 * 'copier' is DSP internal module which copies data from Host DMA (HDA host
423 * dma) or link (hda link, SSP, PDM)
424 * Here we calculate the copier module parameters, like PCM format, output
425 * format, gateway settings
426 * copier_module_config is sent as input buffer with INIT_INSTANCE IPC msg
427 */
428static void skl_set_copier_format(struct skl_sst *ctx,
429 struct skl_module_cfg *mconfig,
430 struct skl_cpr_cfg *cpr_mconfig)
431{
432 struct skl_audio_data_format *out_fmt = &cpr_mconfig->out_fmt;
433 struct skl_base_cfg *base_cfg = (struct skl_base_cfg *)cpr_mconfig;
434
435 skl_set_base_module_format(ctx, mconfig, base_cfg);
436
437 skl_setup_out_format(ctx, mconfig, out_fmt);
438 skl_setup_cpr_gateway_cfg(ctx, mconfig, cpr_mconfig);
439}
440
Jeeja KP399b2102015-11-28 15:01:48 +0530441/*
442 * Algo module are DSP pre processing modules. Algo module take base module
443 * configuration and params
444 */
445
446static void skl_set_algo_format(struct skl_sst *ctx,
447 struct skl_module_cfg *mconfig,
448 struct skl_algo_cfg *algo_mcfg)
449{
450 struct skl_base_cfg *base_cfg = (struct skl_base_cfg *)algo_mcfg;
451
452 skl_set_base_module_format(ctx, mconfig, base_cfg);
453
454 if (mconfig->formats_config.caps_size == 0)
455 return;
456
457 memcpy(algo_mcfg->params,
458 mconfig->formats_config.caps,
459 mconfig->formats_config.caps_size);
460
461}
462
Dharageswari Rfd181102015-12-03 23:29:52 +0530463/*
464 * Mic select module allows selecting one or many input channels, thus
465 * acting as a demux.
466 *
467 * Mic select module take base module configuration and out-format
468 * configuration
469 */
470static void skl_set_base_outfmt_format(struct skl_sst *ctx,
471 struct skl_module_cfg *mconfig,
472 struct skl_base_outfmt_cfg *base_outfmt_mcfg)
473{
474 struct skl_audio_data_format *out_fmt = &base_outfmt_mcfg->out_fmt;
475 struct skl_base_cfg *base_cfg =
476 (struct skl_base_cfg *)base_outfmt_mcfg;
477
478 skl_set_base_module_format(ctx, mconfig, base_cfg);
479 skl_setup_out_format(ctx, mconfig, out_fmt);
480}
481
Jeeja KP23db4722015-08-01 19:40:41 +0530482static u16 skl_get_module_param_size(struct skl_sst *ctx,
483 struct skl_module_cfg *mconfig)
484{
485 u16 param_size;
486
487 switch (mconfig->m_type) {
488 case SKL_MODULE_TYPE_COPIER:
489 param_size = sizeof(struct skl_cpr_cfg);
490 param_size += mconfig->formats_config.caps_size;
491 return param_size;
492
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530493 case SKL_MODULE_TYPE_SRCINT:
494 return sizeof(struct skl_src_module_cfg);
495
496 case SKL_MODULE_TYPE_UPDWMIX:
497 return sizeof(struct skl_up_down_mixer_cfg);
498
Jeeja KP399b2102015-11-28 15:01:48 +0530499 case SKL_MODULE_TYPE_ALGO:
500 param_size = sizeof(struct skl_base_cfg);
501 param_size += mconfig->formats_config.caps_size;
502 return param_size;
503
Dharageswari Rfd181102015-12-03 23:29:52 +0530504 case SKL_MODULE_TYPE_BASE_OUTFMT:
505 return sizeof(struct skl_base_outfmt_cfg);
506
Jeeja KP23db4722015-08-01 19:40:41 +0530507 default:
508 /*
509 * return only base cfg when no specific module type is
510 * specified
511 */
512 return sizeof(struct skl_base_cfg);
513 }
514
515 return 0;
516}
517
518/*
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530519 * DSP firmware supports various modules like copier, SRC, updown etc.
520 * These modules required various parameters to be calculated and sent for
521 * the module initialization to DSP. By default a generic module needs only
522 * base module format configuration
Jeeja KP23db4722015-08-01 19:40:41 +0530523 */
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530524
Jeeja KP23db4722015-08-01 19:40:41 +0530525static int skl_set_module_format(struct skl_sst *ctx,
526 struct skl_module_cfg *module_config,
527 u16 *module_config_size,
528 void **param_data)
529{
530 u16 param_size;
531
532 param_size = skl_get_module_param_size(ctx, module_config);
533
534 *param_data = kzalloc(param_size, GFP_KERNEL);
535 if (NULL == *param_data)
536 return -ENOMEM;
537
538 *module_config_size = param_size;
539
540 switch (module_config->m_type) {
541 case SKL_MODULE_TYPE_COPIER:
542 skl_set_copier_format(ctx, module_config, *param_data);
543 break;
544
Hardik T Shaha0ffe482015-08-01 19:40:42 +0530545 case SKL_MODULE_TYPE_SRCINT:
546 skl_set_src_format(ctx, module_config, *param_data);
547 break;
548
549 case SKL_MODULE_TYPE_UPDWMIX:
550 skl_set_updown_mixer_format(ctx, module_config, *param_data);
551 break;
552
Jeeja KP399b2102015-11-28 15:01:48 +0530553 case SKL_MODULE_TYPE_ALGO:
554 skl_set_algo_format(ctx, module_config, *param_data);
555 break;
556
Dharageswari Rfd181102015-12-03 23:29:52 +0530557 case SKL_MODULE_TYPE_BASE_OUTFMT:
558 skl_set_base_outfmt_format(ctx, module_config, *param_data);
559 break;
560
Jeeja KP23db4722015-08-01 19:40:41 +0530561 default:
562 skl_set_base_module_format(ctx, module_config, *param_data);
563 break;
564
565 }
566
567 dev_dbg(ctx->dev, "Module type=%d config size: %d bytes\n",
568 module_config->id.module_id, param_size);
569 print_hex_dump(KERN_DEBUG, "Module params:", DUMP_PREFIX_OFFSET, 8, 4,
570 *param_data, param_size, false);
571 return 0;
572}
573
574static int skl_get_queue_index(struct skl_module_pin *mpin,
575 struct skl_module_inst_id id, int max)
576{
577 int i;
578
579 for (i = 0; i < max; i++) {
580 if (mpin[i].id.module_id == id.module_id &&
581 mpin[i].id.instance_id == id.instance_id)
582 return i;
583 }
584
585 return -EINVAL;
586}
587
588/*
589 * Allocates queue for each module.
590 * if dynamic, the pin_index is allocated 0 to max_pin.
591 * In static, the pin_index is fixed based on module_id and instance id
592 */
593static int skl_alloc_queue(struct skl_module_pin *mpin,
Jeeja KP4f745702015-10-27 09:22:49 +0900594 struct skl_module_cfg *tgt_cfg, int max)
Jeeja KP23db4722015-08-01 19:40:41 +0530595{
596 int i;
Jeeja KP4f745702015-10-27 09:22:49 +0900597 struct skl_module_inst_id id = tgt_cfg->id;
Jeeja KP23db4722015-08-01 19:40:41 +0530598 /*
599 * if pin in dynamic, find first free pin
600 * otherwise find match module and instance id pin as topology will
601 * ensure a unique pin is assigned to this so no need to
602 * allocate/free
603 */
604 for (i = 0; i < max; i++) {
605 if (mpin[i].is_dynamic) {
Jeeja KP4f745702015-10-27 09:22:49 +0900606 if (!mpin[i].in_use &&
607 mpin[i].pin_state == SKL_PIN_UNBIND) {
608
Jeeja KP23db4722015-08-01 19:40:41 +0530609 mpin[i].in_use = true;
610 mpin[i].id.module_id = id.module_id;
611 mpin[i].id.instance_id = id.instance_id;
Jeeja KP4f745702015-10-27 09:22:49 +0900612 mpin[i].tgt_mcfg = tgt_cfg;
Jeeja KP23db4722015-08-01 19:40:41 +0530613 return i;
614 }
615 } else {
616 if (mpin[i].id.module_id == id.module_id &&
Jeeja KP4f745702015-10-27 09:22:49 +0900617 mpin[i].id.instance_id == id.instance_id &&
618 mpin[i].pin_state == SKL_PIN_UNBIND) {
619
620 mpin[i].tgt_mcfg = tgt_cfg;
Jeeja KP23db4722015-08-01 19:40:41 +0530621 return i;
Jeeja KP4f745702015-10-27 09:22:49 +0900622 }
Jeeja KP23db4722015-08-01 19:40:41 +0530623 }
624 }
625
626 return -EINVAL;
627}
628
629static void skl_free_queue(struct skl_module_pin *mpin, int q_index)
630{
631 if (mpin[q_index].is_dynamic) {
632 mpin[q_index].in_use = false;
633 mpin[q_index].id.module_id = 0;
634 mpin[q_index].id.instance_id = 0;
635 }
Jeeja KP4f745702015-10-27 09:22:49 +0900636 mpin[q_index].pin_state = SKL_PIN_UNBIND;
637 mpin[q_index].tgt_mcfg = NULL;
638}
639
640/* Module state will be set to unint, if all the out pin state is UNBIND */
641
642static void skl_clear_module_state(struct skl_module_pin *mpin, int max,
643 struct skl_module_cfg *mcfg)
644{
645 int i;
646 bool found = false;
647
648 for (i = 0; i < max; i++) {
649 if (mpin[i].pin_state == SKL_PIN_UNBIND)
650 continue;
651 found = true;
652 break;
653 }
654
655 if (!found)
656 mcfg->m_state = SKL_MODULE_UNINIT;
657 return;
Jeeja KP23db4722015-08-01 19:40:41 +0530658}
Jeeja KPbeb73b22015-08-01 19:40:43 +0530659
660/*
661 * A module needs to be instanataited in DSP. A mdoule is present in a
662 * collection of module referred as a PIPE.
663 * We first calculate the module format, based on module type and then
664 * invoke the DSP by sending IPC INIT_INSTANCE using ipc helper
665 */
666int skl_init_module(struct skl_sst *ctx,
Jeeja KP9939a9c2015-11-28 15:01:47 +0530667 struct skl_module_cfg *mconfig)
Jeeja KPbeb73b22015-08-01 19:40:43 +0530668{
669 u16 module_config_size = 0;
670 void *param_data = NULL;
671 int ret;
672 struct skl_ipc_init_instance_msg msg;
673
674 dev_dbg(ctx->dev, "%s: module_id = %d instance=%d\n", __func__,
675 mconfig->id.module_id, mconfig->id.instance_id);
676
677 if (mconfig->pipe->state != SKL_PIPE_CREATED) {
678 dev_err(ctx->dev, "Pipe not created state= %d pipe_id= %d\n",
679 mconfig->pipe->state, mconfig->pipe->ppl_id);
680 return -EIO;
681 }
682
683 ret = skl_set_module_format(ctx, mconfig,
684 &module_config_size, &param_data);
685 if (ret < 0) {
686 dev_err(ctx->dev, "Failed to set module format ret=%d\n", ret);
687 return ret;
688 }
689
690 msg.module_id = mconfig->id.module_id;
691 msg.instance_id = mconfig->id.instance_id;
692 msg.ppl_instance_id = mconfig->pipe->ppl_id;
693 msg.param_data_size = module_config_size;
694 msg.core_id = mconfig->core_id;
695
696 ret = skl_ipc_init_instance(&ctx->ipc, &msg, param_data);
697 if (ret < 0) {
698 dev_err(ctx->dev, "Failed to init instance ret=%d\n", ret);
699 kfree(param_data);
700 return ret;
701 }
702 mconfig->m_state = SKL_MODULE_INIT_DONE;
703
704 return ret;
705}
706
707static void skl_dump_bind_info(struct skl_sst *ctx, struct skl_module_cfg
708 *src_module, struct skl_module_cfg *dst_module)
709{
710 dev_dbg(ctx->dev, "%s: src module_id = %d src_instance=%d\n",
711 __func__, src_module->id.module_id, src_module->id.instance_id);
712 dev_dbg(ctx->dev, "%s: dst_module=%d dst_instacne=%d\n", __func__,
713 dst_module->id.module_id, dst_module->id.instance_id);
714
715 dev_dbg(ctx->dev, "src_module state = %d dst module state = %d\n",
716 src_module->m_state, dst_module->m_state);
717}
718
719/*
720 * On module freeup, we need to unbind the module with modules
721 * it is already bind.
722 * Find the pin allocated and unbind then using bind_unbind IPC
723 */
724int skl_unbind_modules(struct skl_sst *ctx,
725 struct skl_module_cfg *src_mcfg,
726 struct skl_module_cfg *dst_mcfg)
727{
728 int ret;
729 struct skl_ipc_bind_unbind_msg msg;
730 struct skl_module_inst_id src_id = src_mcfg->id;
731 struct skl_module_inst_id dst_id = dst_mcfg->id;
732 int in_max = dst_mcfg->max_in_queue;
733 int out_max = src_mcfg->max_out_queue;
Jeeja KP4f745702015-10-27 09:22:49 +0900734 int src_index, dst_index, src_pin_state, dst_pin_state;
Jeeja KPbeb73b22015-08-01 19:40:43 +0530735
736 skl_dump_bind_info(ctx, src_mcfg, dst_mcfg);
737
Jeeja KPbeb73b22015-08-01 19:40:43 +0530738 /* get src queue index */
739 src_index = skl_get_queue_index(src_mcfg->m_out_pin, dst_id, out_max);
740 if (src_index < 0)
Jeeja KP9cf30492016-02-03 17:59:48 +0530741 return 0;
Jeeja KPbeb73b22015-08-01 19:40:43 +0530742
Jeeja KP4f745702015-10-27 09:22:49 +0900743 msg.src_queue = src_index;
Jeeja KPbeb73b22015-08-01 19:40:43 +0530744
745 /* get dst queue index */
746 dst_index = skl_get_queue_index(dst_mcfg->m_in_pin, src_id, in_max);
747 if (dst_index < 0)
Jeeja KP9cf30492016-02-03 17:59:48 +0530748 return 0;
Jeeja KPbeb73b22015-08-01 19:40:43 +0530749
Jeeja KP4f745702015-10-27 09:22:49 +0900750 msg.dst_queue = dst_index;
751
752 src_pin_state = src_mcfg->m_out_pin[src_index].pin_state;
753 dst_pin_state = dst_mcfg->m_in_pin[dst_index].pin_state;
754
755 if (src_pin_state != SKL_PIN_BIND_DONE ||
756 dst_pin_state != SKL_PIN_BIND_DONE)
757 return 0;
Jeeja KPbeb73b22015-08-01 19:40:43 +0530758
759 msg.module_id = src_mcfg->id.module_id;
760 msg.instance_id = src_mcfg->id.instance_id;
761 msg.dst_module_id = dst_mcfg->id.module_id;
762 msg.dst_instance_id = dst_mcfg->id.instance_id;
763 msg.bind = false;
764
765 ret = skl_ipc_bind_unbind(&ctx->ipc, &msg);
766 if (!ret) {
Jeeja KPbeb73b22015-08-01 19:40:43 +0530767 /* free queue only if unbind is success */
768 skl_free_queue(src_mcfg->m_out_pin, src_index);
769 skl_free_queue(dst_mcfg->m_in_pin, dst_index);
Jeeja KP4f745702015-10-27 09:22:49 +0900770
771 /*
772 * check only if src module bind state, bind is
773 * always from src -> sink
774 */
775 skl_clear_module_state(src_mcfg->m_out_pin, out_max, src_mcfg);
Jeeja KPbeb73b22015-08-01 19:40:43 +0530776 }
777
778 return ret;
779}
780
781/*
782 * Once a module is instantiated it need to be 'bind' with other modules in
783 * the pipeline. For binding we need to find the module pins which are bind
784 * together
785 * This function finds the pins and then sends bund_unbind IPC message to
786 * DSP using IPC helper
787 */
788int skl_bind_modules(struct skl_sst *ctx,
789 struct skl_module_cfg *src_mcfg,
790 struct skl_module_cfg *dst_mcfg)
791{
792 int ret;
793 struct skl_ipc_bind_unbind_msg msg;
Jeeja KPbeb73b22015-08-01 19:40:43 +0530794 int in_max = dst_mcfg->max_in_queue;
795 int out_max = src_mcfg->max_out_queue;
796 int src_index, dst_index;
797
798 skl_dump_bind_info(ctx, src_mcfg, dst_mcfg);
799
Jeeja KP0c684c42016-02-03 17:59:49 +0530800 if (src_mcfg->m_state < SKL_MODULE_INIT_DONE ||
Jeeja KPbeb73b22015-08-01 19:40:43 +0530801 dst_mcfg->m_state < SKL_MODULE_INIT_DONE)
802 return 0;
803
Jeeja KP4f745702015-10-27 09:22:49 +0900804 src_index = skl_alloc_queue(src_mcfg->m_out_pin, dst_mcfg, out_max);
Jeeja KPbeb73b22015-08-01 19:40:43 +0530805 if (src_index < 0)
806 return -EINVAL;
807
Jeeja KP4f745702015-10-27 09:22:49 +0900808 msg.src_queue = src_index;
809 dst_index = skl_alloc_queue(dst_mcfg->m_in_pin, src_mcfg, in_max);
Jeeja KPbeb73b22015-08-01 19:40:43 +0530810 if (dst_index < 0) {
811 skl_free_queue(src_mcfg->m_out_pin, src_index);
812 return -EINVAL;
813 }
814
Jeeja KP4f745702015-10-27 09:22:49 +0900815 msg.dst_queue = dst_index;
Jeeja KPbeb73b22015-08-01 19:40:43 +0530816
817 dev_dbg(ctx->dev, "src queue = %d dst queue =%d\n",
818 msg.src_queue, msg.dst_queue);
819
820 msg.module_id = src_mcfg->id.module_id;
821 msg.instance_id = src_mcfg->id.instance_id;
822 msg.dst_module_id = dst_mcfg->id.module_id;
823 msg.dst_instance_id = dst_mcfg->id.instance_id;
824 msg.bind = true;
825
826 ret = skl_ipc_bind_unbind(&ctx->ipc, &msg);
827
828 if (!ret) {
829 src_mcfg->m_state = SKL_MODULE_BIND_DONE;
Jeeja KP4f745702015-10-27 09:22:49 +0900830 src_mcfg->m_out_pin[src_index].pin_state = SKL_PIN_BIND_DONE;
831 dst_mcfg->m_in_pin[dst_index].pin_state = SKL_PIN_BIND_DONE;
Jeeja KPbeb73b22015-08-01 19:40:43 +0530832 } else {
833 /* error case , if IPC fails, clear the queue index */
834 skl_free_queue(src_mcfg->m_out_pin, src_index);
835 skl_free_queue(dst_mcfg->m_in_pin, dst_index);
836 }
837
838 return ret;
839}
Jeeja KPc9b1e832015-08-01 19:40:44 +0530840
841static int skl_set_pipe_state(struct skl_sst *ctx, struct skl_pipe *pipe,
842 enum skl_ipc_pipeline_state state)
843{
844 dev_dbg(ctx->dev, "%s: pipe_satate = %d\n", __func__, state);
845
846 return skl_ipc_set_pipeline_state(&ctx->ipc, pipe->ppl_id, state);
847}
848
849/*
850 * A pipeline is a collection of modules. Before a module in instantiated a
851 * pipeline needs to be created for it.
852 * This function creates pipeline, by sending create pipeline IPC messages
853 * to FW
854 */
855int skl_create_pipeline(struct skl_sst *ctx, struct skl_pipe *pipe)
856{
857 int ret;
858
859 dev_dbg(ctx->dev, "%s: pipe_id = %d\n", __func__, pipe->ppl_id);
860
861 ret = skl_ipc_create_pipeline(&ctx->ipc, pipe->memory_pages,
862 pipe->pipe_priority, pipe->ppl_id);
863 if (ret < 0) {
864 dev_err(ctx->dev, "Failed to create pipeline\n");
865 return ret;
866 }
867
868 pipe->state = SKL_PIPE_CREATED;
869
870 return 0;
871}
872
873/*
874 * A pipeline needs to be deleted on cleanup. If a pipeline is running, then
875 * pause the pipeline first and then delete it
876 * The pipe delete is done by sending delete pipeline IPC. DSP will stop the
877 * DMA engines and releases resources
878 */
879int skl_delete_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
880{
881 int ret;
882
883 dev_dbg(ctx->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
884
885 /* If pipe is not started, do not try to stop the pipe in FW. */
886 if (pipe->state > SKL_PIPE_STARTED) {
887 ret = skl_set_pipe_state(ctx, pipe, PPL_PAUSED);
888 if (ret < 0) {
889 dev_err(ctx->dev, "Failed to stop pipeline\n");
890 return ret;
891 }
892
893 pipe->state = SKL_PIPE_PAUSED;
894 } else {
895 /* If pipe was not created in FW, do not try to delete it */
896 if (pipe->state < SKL_PIPE_CREATED)
897 return 0;
898
899 ret = skl_ipc_delete_pipeline(&ctx->ipc, pipe->ppl_id);
900 if (ret < 0)
901 dev_err(ctx->dev, "Failed to delete pipeline\n");
Jeeja KPd2c7db82015-12-18 15:11:58 +0530902
903 pipe->state = SKL_PIPE_INVALID;
Jeeja KPc9b1e832015-08-01 19:40:44 +0530904 }
905
906 return ret;
907}
908
909/*
910 * A pipeline is also a scheduling entity in DSP which can be run, stopped
911 * For processing data the pipe need to be run by sending IPC set pipe state
912 * to DSP
913 */
914int skl_run_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
915{
916 int ret;
917
918 dev_dbg(ctx->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
919
920 /* If pipe was not created in FW, do not try to pause or delete */
921 if (pipe->state < SKL_PIPE_CREATED)
922 return 0;
923
924 /* Pipe has to be paused before it is started */
925 ret = skl_set_pipe_state(ctx, pipe, PPL_PAUSED);
926 if (ret < 0) {
927 dev_err(ctx->dev, "Failed to pause pipe\n");
928 return ret;
929 }
930
931 pipe->state = SKL_PIPE_PAUSED;
932
933 ret = skl_set_pipe_state(ctx, pipe, PPL_RUNNING);
934 if (ret < 0) {
935 dev_err(ctx->dev, "Failed to start pipe\n");
936 return ret;
937 }
938
939 pipe->state = SKL_PIPE_STARTED;
940
941 return 0;
942}
943
944/*
945 * Stop the pipeline by sending set pipe state IPC
946 * DSP doesnt implement stop so we always send pause message
947 */
948int skl_stop_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
949{
950 int ret;
951
952 dev_dbg(ctx->dev, "In %s pipe=%d\n", __func__, pipe->ppl_id);
953
954 /* If pipe was not created in FW, do not try to pause or delete */
955 if (pipe->state < SKL_PIPE_PAUSED)
956 return 0;
957
958 ret = skl_set_pipe_state(ctx, pipe, PPL_PAUSED);
959 if (ret < 0) {
960 dev_dbg(ctx->dev, "Failed to stop pipe\n");
961 return ret;
962 }
963
964 pipe->state = SKL_PIPE_CREATED;
965
966 return 0;
967}
Jeeja KP9939a9c2015-11-28 15:01:47 +0530968
969/* Algo parameter set helper function */
970int skl_set_module_params(struct skl_sst *ctx, u32 *params, int size,
971 u32 param_id, struct skl_module_cfg *mcfg)
972{
973 struct skl_ipc_large_config_msg msg;
974
975 msg.module_id = mcfg->id.module_id;
976 msg.instance_id = mcfg->id.instance_id;
977 msg.param_data_size = size;
978 msg.large_param_id = param_id;
979
980 return skl_ipc_set_large_config(&ctx->ipc, &msg, params);
981}
Omair M Abdullah7d9f2912015-12-03 23:29:56 +0530982
983int skl_get_module_params(struct skl_sst *ctx, u32 *params, int size,
984 u32 param_id, struct skl_module_cfg *mcfg)
985{
986 struct skl_ipc_large_config_msg msg;
987
988 msg.module_id = mcfg->id.module_id;
989 msg.instance_id = mcfg->id.instance_id;
990 msg.param_data_size = size;
991 msg.large_param_id = param_id;
992
993 return skl_ipc_get_large_config(&ctx->ipc, &msg, params);
994}