skl-messages.c 24 KB

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