skl-messages.c 36 KB

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