skl-messages.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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. static int skl_dsp_setup_spib(struct device *dev, unsigned int size,
  62. int stream_tag, int enable)
  63. {
  64. struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
  65. struct hdac_bus *bus = ebus_to_hbus(ebus);
  66. struct hdac_stream *stream = snd_hdac_get_stream(bus,
  67. SNDRV_PCM_STREAM_PLAYBACK, stream_tag);
  68. struct hdac_ext_stream *estream;
  69. if (!stream)
  70. return -EINVAL;
  71. estream = stream_to_hdac_ext_stream(stream);
  72. /* enable/disable SPIB for this hdac stream */
  73. snd_hdac_ext_stream_spbcap_enable(ebus, enable, stream->index);
  74. /* set the spib value */
  75. snd_hdac_ext_stream_set_spib(ebus, estream, size);
  76. return 0;
  77. }
  78. static int skl_dsp_prepare(struct device *dev, unsigned int format,
  79. unsigned int size, struct snd_dma_buffer *dmab)
  80. {
  81. struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
  82. struct hdac_bus *bus = ebus_to_hbus(ebus);
  83. struct hdac_ext_stream *estream;
  84. struct hdac_stream *stream;
  85. struct snd_pcm_substream substream;
  86. int ret;
  87. if (!bus)
  88. return -ENODEV;
  89. memset(&substream, 0, sizeof(substream));
  90. substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
  91. estream = snd_hdac_ext_stream_assign(ebus, &substream,
  92. HDAC_EXT_STREAM_TYPE_HOST);
  93. if (!estream)
  94. return -ENODEV;
  95. stream = hdac_stream(estream);
  96. /* assign decouple host dma channel */
  97. ret = snd_hdac_dsp_prepare(stream, format, size, dmab);
  98. if (ret < 0)
  99. return ret;
  100. skl_dsp_setup_spib(dev, size, stream->stream_tag, true);
  101. return stream->stream_tag;
  102. }
  103. static int skl_dsp_trigger(struct device *dev, bool start, int stream_tag)
  104. {
  105. struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
  106. struct hdac_stream *stream;
  107. struct hdac_bus *bus = ebus_to_hbus(ebus);
  108. if (!bus)
  109. return -ENODEV;
  110. stream = snd_hdac_get_stream(bus,
  111. SNDRV_PCM_STREAM_PLAYBACK, stream_tag);
  112. if (!stream)
  113. return -EINVAL;
  114. snd_hdac_dsp_trigger(stream, start);
  115. return 0;
  116. }
  117. static int skl_dsp_cleanup(struct device *dev,
  118. struct snd_dma_buffer *dmab, int stream_tag)
  119. {
  120. struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
  121. struct hdac_stream *stream;
  122. struct hdac_ext_stream *estream;
  123. struct hdac_bus *bus = ebus_to_hbus(ebus);
  124. if (!bus)
  125. return -ENODEV;
  126. stream = snd_hdac_get_stream(bus,
  127. SNDRV_PCM_STREAM_PLAYBACK, stream_tag);
  128. if (!stream)
  129. return -EINVAL;
  130. estream = stream_to_hdac_ext_stream(stream);
  131. skl_dsp_setup_spib(dev, 0, stream_tag, false);
  132. snd_hdac_ext_stream_release(estream, HDAC_EXT_STREAM_TYPE_HOST);
  133. snd_hdac_dsp_cleanup(stream, dmab);
  134. return 0;
  135. }
  136. static struct skl_dsp_loader_ops skl_get_loader_ops(void)
  137. {
  138. struct skl_dsp_loader_ops loader_ops;
  139. memset(&loader_ops, 0, sizeof(struct skl_dsp_loader_ops));
  140. loader_ops.alloc_dma_buf = skl_alloc_dma_buf;
  141. loader_ops.free_dma_buf = skl_free_dma_buf;
  142. return loader_ops;
  143. };
  144. static struct skl_dsp_loader_ops bxt_get_loader_ops(void)
  145. {
  146. struct skl_dsp_loader_ops loader_ops;
  147. memset(&loader_ops, 0, sizeof(loader_ops));
  148. loader_ops.alloc_dma_buf = skl_alloc_dma_buf;
  149. loader_ops.free_dma_buf = skl_free_dma_buf;
  150. loader_ops.prepare = skl_dsp_prepare;
  151. loader_ops.trigger = skl_dsp_trigger;
  152. loader_ops.cleanup = skl_dsp_cleanup;
  153. return loader_ops;
  154. };
  155. static const struct skl_dsp_ops dsp_ops[] = {
  156. {
  157. .id = 0x9d70,
  158. .loader_ops = skl_get_loader_ops,
  159. .init = skl_sst_dsp_init,
  160. .init_fw = skl_sst_init_fw,
  161. .cleanup = skl_sst_dsp_cleanup
  162. },
  163. {
  164. .id = 0x9d71,
  165. .loader_ops = skl_get_loader_ops,
  166. .init = skl_sst_dsp_init,
  167. .init_fw = skl_sst_init_fw,
  168. .cleanup = skl_sst_dsp_cleanup
  169. },
  170. {
  171. .id = 0x5a98,
  172. .loader_ops = bxt_get_loader_ops,
  173. .init = bxt_sst_dsp_init,
  174. .init_fw = bxt_sst_init_fw,
  175. .cleanup = bxt_sst_dsp_cleanup
  176. },
  177. };
  178. const struct skl_dsp_ops *skl_get_dsp_ops(int pci_id)
  179. {
  180. int i;
  181. for (i = 0; i < ARRAY_SIZE(dsp_ops); i++) {
  182. if (dsp_ops[i].id == pci_id)
  183. return &dsp_ops[i];
  184. }
  185. return NULL;
  186. }
  187. int skl_init_dsp(struct skl *skl)
  188. {
  189. void __iomem *mmio_base;
  190. struct hdac_ext_bus *ebus = &skl->ebus;
  191. struct hdac_bus *bus = ebus_to_hbus(ebus);
  192. struct skl_dsp_loader_ops loader_ops;
  193. int irq = bus->irq;
  194. const struct skl_dsp_ops *ops;
  195. int ret;
  196. /* enable ppcap interrupt */
  197. snd_hdac_ext_bus_ppcap_enable(&skl->ebus, true);
  198. snd_hdac_ext_bus_ppcap_int_enable(&skl->ebus, true);
  199. /* read the BAR of the ADSP MMIO */
  200. mmio_base = pci_ioremap_bar(skl->pci, 4);
  201. if (mmio_base == NULL) {
  202. dev_err(bus->dev, "ioremap error\n");
  203. return -ENXIO;
  204. }
  205. ops = skl_get_dsp_ops(skl->pci->device);
  206. if (!ops)
  207. return -EIO;
  208. loader_ops = ops->loader_ops();
  209. ret = ops->init(bus->dev, mmio_base, irq,
  210. skl->fw_name, loader_ops,
  211. &skl->skl_sst);
  212. if (ret < 0)
  213. return ret;
  214. dev_dbg(bus->dev, "dsp registration status=%d\n", ret);
  215. return ret;
  216. }
  217. int skl_free_dsp(struct skl *skl)
  218. {
  219. struct hdac_ext_bus *ebus = &skl->ebus;
  220. struct hdac_bus *bus = ebus_to_hbus(ebus);
  221. struct skl_sst *ctx = skl->skl_sst;
  222. const struct skl_dsp_ops *ops;
  223. /* disable ppcap interrupt */
  224. snd_hdac_ext_bus_ppcap_int_enable(&skl->ebus, false);
  225. ops = skl_get_dsp_ops(skl->pci->device);
  226. if (!ops)
  227. return -EIO;
  228. ops->cleanup(bus->dev, ctx);
  229. if (ctx->dsp->addr.lpe)
  230. iounmap(ctx->dsp->addr.lpe);
  231. return 0;
  232. }
  233. /*
  234. * In the case of "suspend_active" i.e, the Audio IP being active
  235. * during system suspend, immediately excecute any pending D0i3 work
  236. * before suspending. This is needed for the IP to work in low power
  237. * mode during system suspend. In the case of normal suspend, cancel
  238. * any pending D0i3 work.
  239. */
  240. int skl_suspend_late_dsp(struct skl *skl)
  241. {
  242. struct skl_sst *ctx = skl->skl_sst;
  243. struct delayed_work *dwork;
  244. if (!ctx)
  245. return 0;
  246. dwork = &ctx->d0i3.work;
  247. if (dwork->work.func) {
  248. if (skl->supend_active)
  249. flush_delayed_work(dwork);
  250. else
  251. cancel_delayed_work_sync(dwork);
  252. }
  253. return 0;
  254. }
  255. int skl_suspend_dsp(struct skl *skl)
  256. {
  257. struct skl_sst *ctx = skl->skl_sst;
  258. int ret;
  259. /* if ppcap is not supported return 0 */
  260. if (!skl->ebus.bus.ppcap)
  261. return 0;
  262. ret = skl_dsp_sleep(ctx->dsp);
  263. if (ret < 0)
  264. return ret;
  265. /* disable ppcap interrupt */
  266. snd_hdac_ext_bus_ppcap_int_enable(&skl->ebus, false);
  267. snd_hdac_ext_bus_ppcap_enable(&skl->ebus, false);
  268. return 0;
  269. }
  270. int skl_resume_dsp(struct skl *skl)
  271. {
  272. struct skl_sst *ctx = skl->skl_sst;
  273. int ret;
  274. /* if ppcap is not supported return 0 */
  275. if (!skl->ebus.bus.ppcap)
  276. return 0;
  277. /* enable ppcap interrupt */
  278. snd_hdac_ext_bus_ppcap_enable(&skl->ebus, true);
  279. snd_hdac_ext_bus_ppcap_int_enable(&skl->ebus, true);
  280. /* check if DSP 1st boot is done */
  281. if (skl->skl_sst->is_first_boot == true)
  282. return 0;
  283. ret = skl_dsp_wake(ctx->dsp);
  284. if (ret < 0)
  285. return ret;
  286. skl_dsp_enable_notification(skl->skl_sst, false);
  287. return ret;
  288. }
  289. enum skl_bitdepth skl_get_bit_depth(int params)
  290. {
  291. switch (params) {
  292. case 8:
  293. return SKL_DEPTH_8BIT;
  294. case 16:
  295. return SKL_DEPTH_16BIT;
  296. case 24:
  297. return SKL_DEPTH_24BIT;
  298. case 32:
  299. return SKL_DEPTH_32BIT;
  300. default:
  301. return SKL_DEPTH_INVALID;
  302. }
  303. }
  304. /*
  305. * Each module in DSP expects a base module configuration, which consists of
  306. * PCM format information, which we calculate in driver and resource values
  307. * which are read from widget information passed through topology binary
  308. * This is send when we create a module with INIT_INSTANCE IPC msg
  309. */
  310. static void skl_set_base_module_format(struct skl_sst *ctx,
  311. struct skl_module_cfg *mconfig,
  312. struct skl_base_cfg *base_cfg)
  313. {
  314. struct skl_module_fmt *format = &mconfig->in_fmt[0];
  315. base_cfg->audio_fmt.number_of_channels = (u8)format->channels;
  316. base_cfg->audio_fmt.s_freq = format->s_freq;
  317. base_cfg->audio_fmt.bit_depth = format->bit_depth;
  318. base_cfg->audio_fmt.valid_bit_depth = format->valid_bit_depth;
  319. base_cfg->audio_fmt.ch_cfg = format->ch_cfg;
  320. dev_dbg(ctx->dev, "bit_depth=%x valid_bd=%x ch_config=%x\n",
  321. format->bit_depth, format->valid_bit_depth,
  322. format->ch_cfg);
  323. base_cfg->audio_fmt.channel_map = format->ch_map;
  324. base_cfg->audio_fmt.interleaving = format->interleaving_style;
  325. base_cfg->cps = mconfig->mcps;
  326. base_cfg->ibs = mconfig->ibs;
  327. base_cfg->obs = mconfig->obs;
  328. base_cfg->is_pages = mconfig->mem_pages;
  329. }
  330. /*
  331. * Copies copier capabilities into copier module and updates copier module
  332. * config size.
  333. */
  334. static void skl_copy_copier_caps(struct skl_module_cfg *mconfig,
  335. struct skl_cpr_cfg *cpr_mconfig)
  336. {
  337. if (mconfig->formats_config.caps_size == 0)
  338. return;
  339. memcpy(cpr_mconfig->gtw_cfg.config_data,
  340. mconfig->formats_config.caps,
  341. mconfig->formats_config.caps_size);
  342. cpr_mconfig->gtw_cfg.config_length =
  343. (mconfig->formats_config.caps_size) / 4;
  344. }
  345. #define SKL_NON_GATEWAY_CPR_NODE_ID 0xFFFFFFFF
  346. /*
  347. * Calculate the gatewat settings required for copier module, type of
  348. * gateway and index of gateway to use
  349. */
  350. static u32 skl_get_node_id(struct skl_sst *ctx,
  351. struct skl_module_cfg *mconfig)
  352. {
  353. union skl_connector_node_id node_id = {0};
  354. union skl_ssp_dma_node ssp_node = {0};
  355. struct skl_pipe_params *params = mconfig->pipe->p_params;
  356. switch (mconfig->dev_type) {
  357. case SKL_DEVICE_BT:
  358. node_id.node.dma_type =
  359. (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
  360. SKL_DMA_I2S_LINK_OUTPUT_CLASS :
  361. SKL_DMA_I2S_LINK_INPUT_CLASS;
  362. node_id.node.vindex = params->host_dma_id +
  363. (mconfig->vbus_id << 3);
  364. break;
  365. case SKL_DEVICE_I2S:
  366. node_id.node.dma_type =
  367. (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
  368. SKL_DMA_I2S_LINK_OUTPUT_CLASS :
  369. SKL_DMA_I2S_LINK_INPUT_CLASS;
  370. ssp_node.dma_node.time_slot_index = mconfig->time_slot;
  371. ssp_node.dma_node.i2s_instance = mconfig->vbus_id;
  372. node_id.node.vindex = ssp_node.val;
  373. break;
  374. case SKL_DEVICE_DMIC:
  375. node_id.node.dma_type = SKL_DMA_DMIC_LINK_INPUT_CLASS;
  376. node_id.node.vindex = mconfig->vbus_id +
  377. (mconfig->time_slot);
  378. break;
  379. case SKL_DEVICE_HDALINK:
  380. node_id.node.dma_type =
  381. (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
  382. SKL_DMA_HDA_LINK_OUTPUT_CLASS :
  383. SKL_DMA_HDA_LINK_INPUT_CLASS;
  384. node_id.node.vindex = params->link_dma_id;
  385. break;
  386. case SKL_DEVICE_HDAHOST:
  387. node_id.node.dma_type =
  388. (SKL_CONN_SOURCE == mconfig->hw_conn_type) ?
  389. SKL_DMA_HDA_HOST_OUTPUT_CLASS :
  390. SKL_DMA_HDA_HOST_INPUT_CLASS;
  391. node_id.node.vindex = params->host_dma_id;
  392. break;
  393. default:
  394. node_id.val = 0xFFFFFFFF;
  395. break;
  396. }
  397. return node_id.val;
  398. }
  399. static void skl_setup_cpr_gateway_cfg(struct skl_sst *ctx,
  400. struct skl_module_cfg *mconfig,
  401. struct skl_cpr_cfg *cpr_mconfig)
  402. {
  403. cpr_mconfig->gtw_cfg.node_id = skl_get_node_id(ctx, mconfig);
  404. if (cpr_mconfig->gtw_cfg.node_id == SKL_NON_GATEWAY_CPR_NODE_ID) {
  405. cpr_mconfig->cpr_feature_mask = 0;
  406. return;
  407. }
  408. if (SKL_CONN_SOURCE == mconfig->hw_conn_type)
  409. cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * mconfig->obs;
  410. else
  411. cpr_mconfig->gtw_cfg.dma_buffer_size = 2 * mconfig->ibs;
  412. cpr_mconfig->cpr_feature_mask = 0;
  413. cpr_mconfig->gtw_cfg.config_length = 0;
  414. skl_copy_copier_caps(mconfig, cpr_mconfig);
  415. }
  416. #define DMA_CONTROL_ID 5
  417. int skl_dsp_set_dma_control(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
  418. {
  419. struct skl_dma_control *dma_ctrl;
  420. struct skl_ipc_large_config_msg msg = {0};
  421. int err = 0;
  422. /*
  423. * if blob size zero, then return
  424. */
  425. if (mconfig->formats_config.caps_size == 0)
  426. return 0;
  427. msg.large_param_id = DMA_CONTROL_ID;
  428. msg.param_data_size = sizeof(struct skl_dma_control) +
  429. mconfig->formats_config.caps_size;
  430. dma_ctrl = kzalloc(msg.param_data_size, GFP_KERNEL);
  431. if (dma_ctrl == NULL)
  432. return -ENOMEM;
  433. dma_ctrl->node_id = skl_get_node_id(ctx, mconfig);
  434. /* size in dwords */
  435. dma_ctrl->config_length = mconfig->formats_config.caps_size / 4;
  436. memcpy(dma_ctrl->config_data, mconfig->formats_config.caps,
  437. mconfig->formats_config.caps_size);
  438. err = skl_ipc_set_large_config(&ctx->ipc, &msg, (u32 *)dma_ctrl);
  439. kfree(dma_ctrl);
  440. return err;
  441. }
  442. static void skl_setup_out_format(struct skl_sst *ctx,
  443. struct skl_module_cfg *mconfig,
  444. struct skl_audio_data_format *out_fmt)
  445. {
  446. struct skl_module_fmt *format = &mconfig->out_fmt[0];
  447. out_fmt->number_of_channels = (u8)format->channels;
  448. out_fmt->s_freq = format->s_freq;
  449. out_fmt->bit_depth = format->bit_depth;
  450. out_fmt->valid_bit_depth = format->valid_bit_depth;
  451. out_fmt->ch_cfg = format->ch_cfg;
  452. out_fmt->channel_map = format->ch_map;
  453. out_fmt->interleaving = format->interleaving_style;
  454. out_fmt->sample_type = format->sample_type;
  455. dev_dbg(ctx->dev, "copier out format chan=%d fre=%d bitdepth=%d\n",
  456. out_fmt->number_of_channels, format->s_freq, format->bit_depth);
  457. }
  458. /*
  459. * DSP needs SRC module for frequency conversion, SRC takes base module
  460. * configuration and the target frequency as extra parameter passed as src
  461. * config
  462. */
  463. static void skl_set_src_format(struct skl_sst *ctx,
  464. struct skl_module_cfg *mconfig,
  465. struct skl_src_module_cfg *src_mconfig)
  466. {
  467. struct skl_module_fmt *fmt = &mconfig->out_fmt[0];
  468. skl_set_base_module_format(ctx, mconfig,
  469. (struct skl_base_cfg *)src_mconfig);
  470. src_mconfig->src_cfg = fmt->s_freq;
  471. }
  472. /*
  473. * DSP needs updown module to do channel conversion. updown module take base
  474. * module configuration and channel configuration
  475. * It also take coefficients and now we have defaults applied here
  476. */
  477. static void skl_set_updown_mixer_format(struct skl_sst *ctx,
  478. struct skl_module_cfg *mconfig,
  479. struct skl_up_down_mixer_cfg *mixer_mconfig)
  480. {
  481. struct skl_module_fmt *fmt = &mconfig->out_fmt[0];
  482. int i = 0;
  483. skl_set_base_module_format(ctx, mconfig,
  484. (struct skl_base_cfg *)mixer_mconfig);
  485. mixer_mconfig->out_ch_cfg = fmt->ch_cfg;
  486. /* Select F/W default coefficient */
  487. mixer_mconfig->coeff_sel = 0x0;
  488. /* User coeff, don't care since we are selecting F/W defaults */
  489. for (i = 0; i < UP_DOWN_MIXER_MAX_COEFF; i++)
  490. mixer_mconfig->coeff[i] = 0xDEADBEEF;
  491. }
  492. /*
  493. * 'copier' is DSP internal module which copies data from Host DMA (HDA host
  494. * dma) or link (hda link, SSP, PDM)
  495. * Here we calculate the copier module parameters, like PCM format, output
  496. * format, gateway settings
  497. * copier_module_config is sent as input buffer with INIT_INSTANCE IPC msg
  498. */
  499. static void skl_set_copier_format(struct skl_sst *ctx,
  500. struct skl_module_cfg *mconfig,
  501. struct skl_cpr_cfg *cpr_mconfig)
  502. {
  503. struct skl_audio_data_format *out_fmt = &cpr_mconfig->out_fmt;
  504. struct skl_base_cfg *base_cfg = (struct skl_base_cfg *)cpr_mconfig;
  505. skl_set_base_module_format(ctx, mconfig, base_cfg);
  506. skl_setup_out_format(ctx, mconfig, out_fmt);
  507. skl_setup_cpr_gateway_cfg(ctx, mconfig, cpr_mconfig);
  508. }
  509. /*
  510. * Algo module are DSP pre processing modules. Algo module take base module
  511. * configuration and params
  512. */
  513. static void skl_set_algo_format(struct skl_sst *ctx,
  514. struct skl_module_cfg *mconfig,
  515. struct skl_algo_cfg *algo_mcfg)
  516. {
  517. struct skl_base_cfg *base_cfg = (struct skl_base_cfg *)algo_mcfg;
  518. skl_set_base_module_format(ctx, mconfig, base_cfg);
  519. if (mconfig->formats_config.caps_size == 0)
  520. return;
  521. memcpy(algo_mcfg->params,
  522. mconfig->formats_config.caps,
  523. mconfig->formats_config.caps_size);
  524. }
  525. /*
  526. * Mic select module allows selecting one or many input channels, thus
  527. * acting as a demux.
  528. *
  529. * Mic select module take base module configuration and out-format
  530. * configuration
  531. */
  532. static void skl_set_base_outfmt_format(struct skl_sst *ctx,
  533. struct skl_module_cfg *mconfig,
  534. struct skl_base_outfmt_cfg *base_outfmt_mcfg)
  535. {
  536. struct skl_audio_data_format *out_fmt = &base_outfmt_mcfg->out_fmt;
  537. struct skl_base_cfg *base_cfg =
  538. (struct skl_base_cfg *)base_outfmt_mcfg;
  539. skl_set_base_module_format(ctx, mconfig, base_cfg);
  540. skl_setup_out_format(ctx, mconfig, out_fmt);
  541. }
  542. static u16 skl_get_module_param_size(struct skl_sst *ctx,
  543. struct skl_module_cfg *mconfig)
  544. {
  545. u16 param_size;
  546. switch (mconfig->m_type) {
  547. case SKL_MODULE_TYPE_COPIER:
  548. param_size = sizeof(struct skl_cpr_cfg);
  549. param_size += mconfig->formats_config.caps_size;
  550. return param_size;
  551. case SKL_MODULE_TYPE_SRCINT:
  552. return sizeof(struct skl_src_module_cfg);
  553. case SKL_MODULE_TYPE_UPDWMIX:
  554. return sizeof(struct skl_up_down_mixer_cfg);
  555. case SKL_MODULE_TYPE_ALGO:
  556. param_size = sizeof(struct skl_base_cfg);
  557. param_size += mconfig->formats_config.caps_size;
  558. return param_size;
  559. case SKL_MODULE_TYPE_BASE_OUTFMT:
  560. case SKL_MODULE_TYPE_KPB:
  561. return sizeof(struct skl_base_outfmt_cfg);
  562. default:
  563. /*
  564. * return only base cfg when no specific module type is
  565. * specified
  566. */
  567. return sizeof(struct skl_base_cfg);
  568. }
  569. return 0;
  570. }
  571. /*
  572. * DSP firmware supports various modules like copier, SRC, updown etc.
  573. * These modules required various parameters to be calculated and sent for
  574. * the module initialization to DSP. By default a generic module needs only
  575. * base module format configuration
  576. */
  577. static int skl_set_module_format(struct skl_sst *ctx,
  578. struct skl_module_cfg *module_config,
  579. u16 *module_config_size,
  580. void **param_data)
  581. {
  582. u16 param_size;
  583. param_size = skl_get_module_param_size(ctx, module_config);
  584. *param_data = kzalloc(param_size, GFP_KERNEL);
  585. if (NULL == *param_data)
  586. return -ENOMEM;
  587. *module_config_size = param_size;
  588. switch (module_config->m_type) {
  589. case SKL_MODULE_TYPE_COPIER:
  590. skl_set_copier_format(ctx, module_config, *param_data);
  591. break;
  592. case SKL_MODULE_TYPE_SRCINT:
  593. skl_set_src_format(ctx, module_config, *param_data);
  594. break;
  595. case SKL_MODULE_TYPE_UPDWMIX:
  596. skl_set_updown_mixer_format(ctx, module_config, *param_data);
  597. break;
  598. case SKL_MODULE_TYPE_ALGO:
  599. skl_set_algo_format(ctx, module_config, *param_data);
  600. break;
  601. case SKL_MODULE_TYPE_BASE_OUTFMT:
  602. case SKL_MODULE_TYPE_KPB:
  603. skl_set_base_outfmt_format(ctx, module_config, *param_data);
  604. break;
  605. default:
  606. skl_set_base_module_format(ctx, module_config, *param_data);
  607. break;
  608. }
  609. dev_dbg(ctx->dev, "Module type=%d config size: %d bytes\n",
  610. module_config->id.module_id, param_size);
  611. print_hex_dump_debug("Module params:", DUMP_PREFIX_OFFSET, 8, 4,
  612. *param_data, param_size, false);
  613. return 0;
  614. }
  615. static int skl_get_queue_index(struct skl_module_pin *mpin,
  616. struct skl_module_inst_id id, int max)
  617. {
  618. int i;
  619. for (i = 0; i < max; i++) {
  620. if (mpin[i].id.module_id == id.module_id &&
  621. mpin[i].id.instance_id == id.instance_id)
  622. return i;
  623. }
  624. return -EINVAL;
  625. }
  626. /*
  627. * Allocates queue for each module.
  628. * if dynamic, the pin_index is allocated 0 to max_pin.
  629. * In static, the pin_index is fixed based on module_id and instance id
  630. */
  631. static int skl_alloc_queue(struct skl_module_pin *mpin,
  632. struct skl_module_cfg *tgt_cfg, int max)
  633. {
  634. int i;
  635. struct skl_module_inst_id id = tgt_cfg->id;
  636. /*
  637. * if pin in dynamic, find first free pin
  638. * otherwise find match module and instance id pin as topology will
  639. * ensure a unique pin is assigned to this so no need to
  640. * allocate/free
  641. */
  642. for (i = 0; i < max; i++) {
  643. if (mpin[i].is_dynamic) {
  644. if (!mpin[i].in_use &&
  645. mpin[i].pin_state == SKL_PIN_UNBIND) {
  646. mpin[i].in_use = true;
  647. mpin[i].id.module_id = id.module_id;
  648. mpin[i].id.instance_id = id.instance_id;
  649. mpin[i].id.pvt_id = id.pvt_id;
  650. mpin[i].tgt_mcfg = tgt_cfg;
  651. return i;
  652. }
  653. } else {
  654. if (mpin[i].id.module_id == id.module_id &&
  655. mpin[i].id.instance_id == id.instance_id &&
  656. mpin[i].pin_state == SKL_PIN_UNBIND) {
  657. mpin[i].tgt_mcfg = tgt_cfg;
  658. return i;
  659. }
  660. }
  661. }
  662. return -EINVAL;
  663. }
  664. static void skl_free_queue(struct skl_module_pin *mpin, int q_index)
  665. {
  666. if (mpin[q_index].is_dynamic) {
  667. mpin[q_index].in_use = false;
  668. mpin[q_index].id.module_id = 0;
  669. mpin[q_index].id.instance_id = 0;
  670. mpin[q_index].id.pvt_id = 0;
  671. }
  672. mpin[q_index].pin_state = SKL_PIN_UNBIND;
  673. mpin[q_index].tgt_mcfg = NULL;
  674. }
  675. /* Module state will be set to unint, if all the out pin state is UNBIND */
  676. static void skl_clear_module_state(struct skl_module_pin *mpin, int max,
  677. struct skl_module_cfg *mcfg)
  678. {
  679. int i;
  680. bool found = false;
  681. for (i = 0; i < max; i++) {
  682. if (mpin[i].pin_state == SKL_PIN_UNBIND)
  683. continue;
  684. found = true;
  685. break;
  686. }
  687. if (!found)
  688. mcfg->m_state = SKL_MODULE_UNINIT;
  689. return;
  690. }
  691. /*
  692. * A module needs to be instanataited in DSP. A mdoule is present in a
  693. * collection of module referred as a PIPE.
  694. * We first calculate the module format, based on module type and then
  695. * invoke the DSP by sending IPC INIT_INSTANCE using ipc helper
  696. */
  697. int skl_init_module(struct skl_sst *ctx,
  698. struct skl_module_cfg *mconfig)
  699. {
  700. u16 module_config_size = 0;
  701. void *param_data = NULL;
  702. int ret;
  703. struct skl_ipc_init_instance_msg msg;
  704. dev_dbg(ctx->dev, "%s: module_id = %d instance=%d\n", __func__,
  705. mconfig->id.module_id, mconfig->id.pvt_id);
  706. if (mconfig->pipe->state != SKL_PIPE_CREATED) {
  707. dev_err(ctx->dev, "Pipe not created state= %d pipe_id= %d\n",
  708. mconfig->pipe->state, mconfig->pipe->ppl_id);
  709. return -EIO;
  710. }
  711. ret = skl_set_module_format(ctx, mconfig,
  712. &module_config_size, &param_data);
  713. if (ret < 0) {
  714. dev_err(ctx->dev, "Failed to set module format ret=%d\n", ret);
  715. return ret;
  716. }
  717. msg.module_id = mconfig->id.module_id;
  718. msg.instance_id = mconfig->id.pvt_id;
  719. msg.ppl_instance_id = mconfig->pipe->ppl_id;
  720. msg.param_data_size = module_config_size;
  721. msg.core_id = mconfig->core_id;
  722. msg.domain = mconfig->domain;
  723. ret = skl_ipc_init_instance(&ctx->ipc, &msg, param_data);
  724. if (ret < 0) {
  725. dev_err(ctx->dev, "Failed to init instance ret=%d\n", ret);
  726. kfree(param_data);
  727. return ret;
  728. }
  729. mconfig->m_state = SKL_MODULE_INIT_DONE;
  730. kfree(param_data);
  731. return ret;
  732. }
  733. static void skl_dump_bind_info(struct skl_sst *ctx, struct skl_module_cfg
  734. *src_module, struct skl_module_cfg *dst_module)
  735. {
  736. dev_dbg(ctx->dev, "%s: src module_id = %d src_instance=%d\n",
  737. __func__, src_module->id.module_id, src_module->id.pvt_id);
  738. dev_dbg(ctx->dev, "%s: dst_module=%d dst_instacne=%d\n", __func__,
  739. dst_module->id.module_id, dst_module->id.pvt_id);
  740. dev_dbg(ctx->dev, "src_module state = %d dst module state = %d\n",
  741. src_module->m_state, dst_module->m_state);
  742. }
  743. /*
  744. * On module freeup, we need to unbind the module with modules
  745. * it is already bind.
  746. * Find the pin allocated and unbind then using bind_unbind IPC
  747. */
  748. int skl_unbind_modules(struct skl_sst *ctx,
  749. struct skl_module_cfg *src_mcfg,
  750. struct skl_module_cfg *dst_mcfg)
  751. {
  752. int ret;
  753. struct skl_ipc_bind_unbind_msg msg;
  754. struct skl_module_inst_id src_id = src_mcfg->id;
  755. struct skl_module_inst_id dst_id = dst_mcfg->id;
  756. int in_max = dst_mcfg->max_in_queue;
  757. int out_max = src_mcfg->max_out_queue;
  758. int src_index, dst_index, src_pin_state, dst_pin_state;
  759. skl_dump_bind_info(ctx, src_mcfg, dst_mcfg);
  760. /* get src queue index */
  761. src_index = skl_get_queue_index(src_mcfg->m_out_pin, dst_id, out_max);
  762. if (src_index < 0)
  763. return 0;
  764. msg.src_queue = src_index;
  765. /* get dst queue index */
  766. dst_index = skl_get_queue_index(dst_mcfg->m_in_pin, src_id, in_max);
  767. if (dst_index < 0)
  768. return 0;
  769. msg.dst_queue = dst_index;
  770. src_pin_state = src_mcfg->m_out_pin[src_index].pin_state;
  771. dst_pin_state = dst_mcfg->m_in_pin[dst_index].pin_state;
  772. if (src_pin_state != SKL_PIN_BIND_DONE ||
  773. dst_pin_state != SKL_PIN_BIND_DONE)
  774. return 0;
  775. msg.module_id = src_mcfg->id.module_id;
  776. msg.instance_id = src_mcfg->id.pvt_id;
  777. msg.dst_module_id = dst_mcfg->id.module_id;
  778. msg.dst_instance_id = dst_mcfg->id.pvt_id;
  779. msg.bind = false;
  780. ret = skl_ipc_bind_unbind(&ctx->ipc, &msg);
  781. if (!ret) {
  782. /* free queue only if unbind is success */
  783. skl_free_queue(src_mcfg->m_out_pin, src_index);
  784. skl_free_queue(dst_mcfg->m_in_pin, dst_index);
  785. /*
  786. * check only if src module bind state, bind is
  787. * always from src -> sink
  788. */
  789. skl_clear_module_state(src_mcfg->m_out_pin, out_max, src_mcfg);
  790. }
  791. return ret;
  792. }
  793. /*
  794. * Once a module is instantiated it need to be 'bind' with other modules in
  795. * the pipeline. For binding we need to find the module pins which are bind
  796. * together
  797. * This function finds the pins and then sends bund_unbind IPC message to
  798. * DSP using IPC helper
  799. */
  800. int skl_bind_modules(struct skl_sst *ctx,
  801. struct skl_module_cfg *src_mcfg,
  802. struct skl_module_cfg *dst_mcfg)
  803. {
  804. int ret;
  805. struct skl_ipc_bind_unbind_msg msg;
  806. int in_max = dst_mcfg->max_in_queue;
  807. int out_max = src_mcfg->max_out_queue;
  808. int src_index, dst_index;
  809. skl_dump_bind_info(ctx, src_mcfg, dst_mcfg);
  810. if (src_mcfg->m_state < SKL_MODULE_INIT_DONE ||
  811. dst_mcfg->m_state < SKL_MODULE_INIT_DONE)
  812. return 0;
  813. src_index = skl_alloc_queue(src_mcfg->m_out_pin, dst_mcfg, out_max);
  814. if (src_index < 0)
  815. return -EINVAL;
  816. msg.src_queue = src_index;
  817. dst_index = skl_alloc_queue(dst_mcfg->m_in_pin, src_mcfg, in_max);
  818. if (dst_index < 0) {
  819. skl_free_queue(src_mcfg->m_out_pin, src_index);
  820. return -EINVAL;
  821. }
  822. msg.dst_queue = dst_index;
  823. dev_dbg(ctx->dev, "src queue = %d dst queue =%d\n",
  824. msg.src_queue, msg.dst_queue);
  825. msg.module_id = src_mcfg->id.module_id;
  826. msg.instance_id = src_mcfg->id.pvt_id;
  827. msg.dst_module_id = dst_mcfg->id.module_id;
  828. msg.dst_instance_id = dst_mcfg->id.pvt_id;
  829. msg.bind = true;
  830. ret = skl_ipc_bind_unbind(&ctx->ipc, &msg);
  831. if (!ret) {
  832. src_mcfg->m_state = SKL_MODULE_BIND_DONE;
  833. src_mcfg->m_out_pin[src_index].pin_state = SKL_PIN_BIND_DONE;
  834. dst_mcfg->m_in_pin[dst_index].pin_state = SKL_PIN_BIND_DONE;
  835. } else {
  836. /* error case , if IPC fails, clear the queue index */
  837. skl_free_queue(src_mcfg->m_out_pin, src_index);
  838. skl_free_queue(dst_mcfg->m_in_pin, dst_index);
  839. }
  840. return ret;
  841. }
  842. static int skl_set_pipe_state(struct skl_sst *ctx, struct skl_pipe *pipe,
  843. enum skl_ipc_pipeline_state state)
  844. {
  845. dev_dbg(ctx->dev, "%s: pipe_satate = %d\n", __func__, state);
  846. return skl_ipc_set_pipeline_state(&ctx->ipc, pipe->ppl_id, state);
  847. }
  848. /*
  849. * A pipeline is a collection of modules. Before a module in instantiated a
  850. * pipeline needs to be created for it.
  851. * This function creates pipeline, by sending create pipeline IPC messages
  852. * to FW
  853. */
  854. int skl_create_pipeline(struct skl_sst *ctx, struct skl_pipe *pipe)
  855. {
  856. int ret;
  857. dev_dbg(ctx->dev, "%s: pipe_id = %d\n", __func__, pipe->ppl_id);
  858. ret = skl_ipc_create_pipeline(&ctx->ipc, pipe->memory_pages,
  859. pipe->pipe_priority, pipe->ppl_id,
  860. pipe->lp_mode);
  861. if (ret < 0) {
  862. dev_err(ctx->dev, "Failed to create pipeline\n");
  863. return ret;
  864. }
  865. pipe->state = SKL_PIPE_CREATED;
  866. return 0;
  867. }
  868. /*
  869. * A pipeline needs to be deleted on cleanup. If a pipeline is running, then
  870. * pause the pipeline first and then delete it
  871. * The pipe delete is done by sending delete pipeline IPC. DSP will stop the
  872. * DMA engines and releases resources
  873. */
  874. int skl_delete_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
  875. {
  876. int ret;
  877. dev_dbg(ctx->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
  878. /* If pipe is started, do stop the pipe in FW. */
  879. if (pipe->state > SKL_PIPE_STARTED) {
  880. ret = skl_set_pipe_state(ctx, pipe, PPL_PAUSED);
  881. if (ret < 0) {
  882. dev_err(ctx->dev, "Failed to stop pipeline\n");
  883. return ret;
  884. }
  885. pipe->state = SKL_PIPE_PAUSED;
  886. }
  887. /* If pipe was not created in FW, do not try to delete it */
  888. if (pipe->state < SKL_PIPE_CREATED)
  889. return 0;
  890. ret = skl_ipc_delete_pipeline(&ctx->ipc, pipe->ppl_id);
  891. if (ret < 0) {
  892. dev_err(ctx->dev, "Failed to delete pipeline\n");
  893. return ret;
  894. }
  895. pipe->state = SKL_PIPE_INVALID;
  896. return ret;
  897. }
  898. /*
  899. * A pipeline is also a scheduling entity in DSP which can be run, stopped
  900. * For processing data the pipe need to be run by sending IPC set pipe state
  901. * to DSP
  902. */
  903. int skl_run_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
  904. {
  905. int ret;
  906. dev_dbg(ctx->dev, "%s: pipe = %d\n", __func__, pipe->ppl_id);
  907. /* If pipe was not created in FW, do not try to pause or delete */
  908. if (pipe->state < SKL_PIPE_CREATED)
  909. return 0;
  910. /* Pipe has to be paused before it is started */
  911. ret = skl_set_pipe_state(ctx, pipe, PPL_PAUSED);
  912. if (ret < 0) {
  913. dev_err(ctx->dev, "Failed to pause pipe\n");
  914. return ret;
  915. }
  916. pipe->state = SKL_PIPE_PAUSED;
  917. ret = skl_set_pipe_state(ctx, pipe, PPL_RUNNING);
  918. if (ret < 0) {
  919. dev_err(ctx->dev, "Failed to start pipe\n");
  920. return ret;
  921. }
  922. pipe->state = SKL_PIPE_STARTED;
  923. return 0;
  924. }
  925. /*
  926. * Stop the pipeline by sending set pipe state IPC
  927. * DSP doesnt implement stop so we always send pause message
  928. */
  929. int skl_stop_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
  930. {
  931. int ret;
  932. dev_dbg(ctx->dev, "In %s pipe=%d\n", __func__, pipe->ppl_id);
  933. /* If pipe was not created in FW, do not try to pause or delete */
  934. if (pipe->state < SKL_PIPE_PAUSED)
  935. return 0;
  936. ret = skl_set_pipe_state(ctx, pipe, PPL_PAUSED);
  937. if (ret < 0) {
  938. dev_dbg(ctx->dev, "Failed to stop pipe\n");
  939. return ret;
  940. }
  941. pipe->state = SKL_PIPE_PAUSED;
  942. return 0;
  943. }
  944. /*
  945. * Reset the pipeline by sending set pipe state IPC this will reset the DMA
  946. * from the DSP side
  947. */
  948. int skl_reset_pipe(struct skl_sst *ctx, struct skl_pipe *pipe)
  949. {
  950. int ret;
  951. /* If pipe was not created in FW, do not try to pause or delete */
  952. if (pipe->state < SKL_PIPE_PAUSED)
  953. return 0;
  954. ret = skl_set_pipe_state(ctx, pipe, PPL_RESET);
  955. if (ret < 0) {
  956. dev_dbg(ctx->dev, "Failed to reset pipe ret=%d\n", ret);
  957. return ret;
  958. }
  959. pipe->state = SKL_PIPE_RESET;
  960. return 0;
  961. }
  962. /* Algo parameter set helper function */
  963. int skl_set_module_params(struct skl_sst *ctx, u32 *params, int size,
  964. u32 param_id, struct skl_module_cfg *mcfg)
  965. {
  966. struct skl_ipc_large_config_msg msg;
  967. msg.module_id = mcfg->id.module_id;
  968. msg.instance_id = mcfg->id.pvt_id;
  969. msg.param_data_size = size;
  970. msg.large_param_id = param_id;
  971. return skl_ipc_set_large_config(&ctx->ipc, &msg, params);
  972. }
  973. int skl_get_module_params(struct skl_sst *ctx, u32 *params, int size,
  974. u32 param_id, struct skl_module_cfg *mcfg)
  975. {
  976. struct skl_ipc_large_config_msg msg;
  977. msg.module_id = mcfg->id.module_id;
  978. msg.instance_id = mcfg->id.pvt_id;
  979. msg.param_data_size = size;
  980. msg.large_param_id = param_id;
  981. return skl_ipc_get_large_config(&ctx->ipc, &msg, params);
  982. }