path.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2021 Intel Corporation
  4. //
  5. // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
  6. // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
  7. //
  8. #include <linux/acpi.h>
  9. #include <acpi/nhlt.h>
  10. #include <sound/pcm_params.h>
  11. #include <sound/soc.h>
  12. #include "avs.h"
  13. #include "control.h"
  14. #include "path.h"
  15. #include "topology.h"
  16. /* Must be called with adev->comp_list_mutex held. */
  17. static struct avs_tplg *
  18. avs_path_find_tplg(struct avs_dev *adev, const char *name)
  19. {
  20. struct avs_soc_component *acomp;
  21. list_for_each_entry(acomp, &adev->comp_list, node)
  22. if (!strcmp(acomp->tplg->name, name))
  23. return acomp->tplg;
  24. return NULL;
  25. }
  26. static struct avs_path_module *
  27. avs_path_find_module(struct avs_path_pipeline *ppl, u32 template_id)
  28. {
  29. struct avs_path_module *mod;
  30. list_for_each_entry(mod, &ppl->mod_list, node)
  31. if (mod->template->id == template_id)
  32. return mod;
  33. return NULL;
  34. }
  35. static struct avs_path_pipeline *
  36. avs_path_find_pipeline(struct avs_path *path, u32 template_id)
  37. {
  38. struct avs_path_pipeline *ppl;
  39. list_for_each_entry(ppl, &path->ppl_list, node)
  40. if (ppl->template->id == template_id)
  41. return ppl;
  42. return NULL;
  43. }
  44. static struct avs_path *
  45. avs_path_find_path(struct avs_dev *adev, const char *name, u32 template_id)
  46. {
  47. struct avs_tplg_path_template *pos, *template = NULL;
  48. struct avs_tplg *tplg;
  49. struct avs_path *path;
  50. tplg = avs_path_find_tplg(adev, name);
  51. if (!tplg)
  52. return NULL;
  53. list_for_each_entry(pos, &tplg->path_tmpl_list, node) {
  54. if (pos->id == template_id) {
  55. template = pos;
  56. break;
  57. }
  58. }
  59. if (!template)
  60. return NULL;
  61. spin_lock(&adev->path_list_lock);
  62. /* Only one variant of given path template may be instantiated at a time. */
  63. list_for_each_entry(path, &adev->path_list, node) {
  64. if (path->template->owner == template) {
  65. spin_unlock(&adev->path_list_lock);
  66. return path;
  67. }
  68. }
  69. spin_unlock(&adev->path_list_lock);
  70. return NULL;
  71. }
  72. static bool avs_test_hw_params(struct snd_pcm_hw_params *params,
  73. struct avs_audio_format *fmt)
  74. {
  75. return (params_rate(params) == fmt->sampling_freq &&
  76. params_channels(params) == fmt->num_channels &&
  77. params_physical_width(params) == fmt->bit_depth &&
  78. snd_pcm_hw_params_bits(params) == fmt->valid_bit_depth);
  79. }
  80. static struct avs_tplg_path *
  81. avs_path_find_variant(struct avs_dev *adev,
  82. struct avs_tplg_path_template *template,
  83. struct snd_pcm_hw_params *fe_params,
  84. struct snd_pcm_hw_params *be_params)
  85. {
  86. struct avs_tplg_path *variant;
  87. list_for_each_entry(variant, &template->path_list, node) {
  88. dev_dbg(adev->dev, "check FE rate %d chn %d vbd %d bd %d\n",
  89. variant->fe_fmt->sampling_freq, variant->fe_fmt->num_channels,
  90. variant->fe_fmt->valid_bit_depth, variant->fe_fmt->bit_depth);
  91. dev_dbg(adev->dev, "check BE rate %d chn %d vbd %d bd %d\n",
  92. variant->be_fmt->sampling_freq, variant->be_fmt->num_channels,
  93. variant->be_fmt->valid_bit_depth, variant->be_fmt->bit_depth);
  94. if (variant->fe_fmt && avs_test_hw_params(fe_params, variant->fe_fmt) &&
  95. variant->be_fmt && avs_test_hw_params(be_params, variant->be_fmt))
  96. return variant;
  97. }
  98. return NULL;
  99. }
  100. __maybe_unused
  101. static bool avs_dma_type_is_host(u32 dma_type)
  102. {
  103. return dma_type == AVS_DMA_HDA_HOST_OUTPUT ||
  104. dma_type == AVS_DMA_HDA_HOST_INPUT;
  105. }
  106. __maybe_unused
  107. static bool avs_dma_type_is_link(u32 dma_type)
  108. {
  109. return !avs_dma_type_is_host(dma_type);
  110. }
  111. __maybe_unused
  112. static bool avs_dma_type_is_output(u32 dma_type)
  113. {
  114. return dma_type == AVS_DMA_HDA_HOST_OUTPUT ||
  115. dma_type == AVS_DMA_HDA_LINK_OUTPUT ||
  116. dma_type == AVS_DMA_I2S_LINK_OUTPUT;
  117. }
  118. __maybe_unused
  119. static bool avs_dma_type_is_input(u32 dma_type)
  120. {
  121. return !avs_dma_type_is_output(dma_type);
  122. }
  123. static int avs_copier_create(struct avs_dev *adev, struct avs_path_module *mod)
  124. {
  125. struct avs_tplg_module *t = mod->template;
  126. struct avs_copier_cfg *cfg;
  127. struct acpi_nhlt_format_config *ep_blob;
  128. struct acpi_nhlt_endpoint *ep;
  129. union avs_connector_node_id node_id = {0};
  130. size_t cfg_size, data_size;
  131. void *data = NULL;
  132. u32 dma_type;
  133. int ret;
  134. data_size = sizeof(cfg->gtw_cfg.config);
  135. dma_type = t->cfg_ext->copier.dma_type;
  136. node_id.dma_type = dma_type;
  137. switch (dma_type) {
  138. struct avs_audio_format *fmt;
  139. int direction;
  140. case AVS_DMA_I2S_LINK_OUTPUT:
  141. case AVS_DMA_I2S_LINK_INPUT:
  142. if (avs_dma_type_is_input(dma_type))
  143. direction = SNDRV_PCM_STREAM_CAPTURE;
  144. else
  145. direction = SNDRV_PCM_STREAM_PLAYBACK;
  146. if (t->cfg_ext->copier.blob_fmt)
  147. fmt = t->cfg_ext->copier.blob_fmt;
  148. else if (direction == SNDRV_PCM_STREAM_CAPTURE)
  149. fmt = t->in_fmt;
  150. else
  151. fmt = t->cfg_ext->copier.out_fmt;
  152. ep = acpi_nhlt_find_endpoint(ACPI_NHLT_LINKTYPE_SSP,
  153. ACPI_NHLT_DEVICETYPE_CODEC, direction,
  154. t->cfg_ext->copier.vindex.i2s.instance);
  155. ep_blob = acpi_nhlt_endpoint_find_fmtcfg(ep, fmt->num_channels, fmt->sampling_freq,
  156. fmt->valid_bit_depth, fmt->bit_depth);
  157. if (!ep_blob) {
  158. dev_err(adev->dev, "no I2S ep_blob found\n");
  159. return -ENOENT;
  160. }
  161. data = ep_blob->config.capabilities;
  162. data_size = ep_blob->config.capabilities_size;
  163. /* I2S gateway's vindex is statically assigned in topology */
  164. node_id.vindex = t->cfg_ext->copier.vindex.val;
  165. break;
  166. case AVS_DMA_DMIC_LINK_INPUT:
  167. direction = SNDRV_PCM_STREAM_CAPTURE;
  168. if (t->cfg_ext->copier.blob_fmt)
  169. fmt = t->cfg_ext->copier.blob_fmt;
  170. else
  171. fmt = t->in_fmt;
  172. ep = acpi_nhlt_find_endpoint(ACPI_NHLT_LINKTYPE_PDM, -1, direction, 0);
  173. ep_blob = acpi_nhlt_endpoint_find_fmtcfg(ep, fmt->num_channels, fmt->sampling_freq,
  174. fmt->valid_bit_depth, fmt->bit_depth);
  175. if (!ep_blob) {
  176. dev_err(adev->dev, "no DMIC ep_blob found\n");
  177. return -ENOENT;
  178. }
  179. data = ep_blob->config.capabilities;
  180. data_size = ep_blob->config.capabilities_size;
  181. /* DMIC gateway's vindex is statically assigned in topology */
  182. node_id.vindex = t->cfg_ext->copier.vindex.val;
  183. break;
  184. case AVS_DMA_HDA_HOST_OUTPUT:
  185. case AVS_DMA_HDA_HOST_INPUT:
  186. /* HOST gateway's vindex is dynamically assigned with DMA id */
  187. node_id.vindex = mod->owner->owner->dma_id;
  188. break;
  189. case AVS_DMA_HDA_LINK_OUTPUT:
  190. case AVS_DMA_HDA_LINK_INPUT:
  191. node_id.vindex = t->cfg_ext->copier.vindex.val |
  192. mod->owner->owner->dma_id;
  193. break;
  194. case INVALID_OBJECT_ID:
  195. default:
  196. node_id = INVALID_NODE_ID;
  197. break;
  198. }
  199. cfg_size = offsetof(struct avs_copier_cfg, gtw_cfg.config) + data_size;
  200. if (cfg_size > AVS_MAILBOX_SIZE)
  201. return -EINVAL;
  202. cfg = adev->modcfg_buf;
  203. memset(cfg, 0, cfg_size);
  204. cfg->base.cpc = t->cfg_base->cpc;
  205. cfg->base.ibs = t->cfg_base->ibs;
  206. cfg->base.obs = t->cfg_base->obs;
  207. cfg->base.is_pages = t->cfg_base->is_pages;
  208. cfg->base.audio_fmt = *t->in_fmt;
  209. cfg->out_fmt = *t->cfg_ext->copier.out_fmt;
  210. cfg->feature_mask = t->cfg_ext->copier.feature_mask;
  211. cfg->gtw_cfg.node_id = node_id;
  212. cfg->gtw_cfg.dma_buffer_size = t->cfg_ext->copier.dma_buffer_size;
  213. /* config_length in DWORDs */
  214. cfg->gtw_cfg.config_length = DIV_ROUND_UP(data_size, 4);
  215. if (data)
  216. memcpy(&cfg->gtw_cfg.config.blob, data, data_size);
  217. mod->gtw_attrs = cfg->gtw_cfg.config.attrs;
  218. ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  219. t->core_id, t->domain, cfg, cfg_size,
  220. &mod->instance_id);
  221. return ret;
  222. }
  223. static struct avs_control_data *avs_get_module_control(struct avs_path_module *mod)
  224. {
  225. struct avs_tplg_module *t = mod->template;
  226. struct avs_tplg_path_template *path_tmpl;
  227. struct snd_soc_dapm_widget *w;
  228. int i;
  229. path_tmpl = t->owner->owner->owner;
  230. w = path_tmpl->w;
  231. for (i = 0; i < w->num_kcontrols; i++) {
  232. struct avs_control_data *ctl_data;
  233. struct soc_mixer_control *mc;
  234. mc = (struct soc_mixer_control *)w->kcontrols[i]->private_value;
  235. ctl_data = (struct avs_control_data *)mc->dobj.private;
  236. if (ctl_data->id == t->ctl_id)
  237. return ctl_data;
  238. }
  239. return NULL;
  240. }
  241. static int avs_peakvol_create(struct avs_dev *adev, struct avs_path_module *mod)
  242. {
  243. struct avs_tplg_module *t = mod->template;
  244. struct avs_control_data *ctl_data;
  245. struct avs_peakvol_cfg *cfg;
  246. int volume = S32_MAX;
  247. size_t cfg_size;
  248. int ret;
  249. ctl_data = avs_get_module_control(mod);
  250. if (ctl_data)
  251. volume = ctl_data->volume;
  252. /* As 2+ channels controls are unsupported, have a single block for all channels. */
  253. cfg_size = struct_size(cfg, vols, 1);
  254. if (cfg_size > AVS_MAILBOX_SIZE)
  255. return -EINVAL;
  256. cfg = adev->modcfg_buf;
  257. memset(cfg, 0, cfg_size);
  258. cfg->base.cpc = t->cfg_base->cpc;
  259. cfg->base.ibs = t->cfg_base->ibs;
  260. cfg->base.obs = t->cfg_base->obs;
  261. cfg->base.is_pages = t->cfg_base->is_pages;
  262. cfg->base.audio_fmt = *t->in_fmt;
  263. cfg->vols[0].target_volume = volume;
  264. cfg->vols[0].channel_id = AVS_ALL_CHANNELS_MASK;
  265. cfg->vols[0].curve_type = AVS_AUDIO_CURVE_NONE;
  266. cfg->vols[0].curve_duration = 0;
  267. ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, t->core_id,
  268. t->domain, cfg, cfg_size, &mod->instance_id);
  269. return ret;
  270. }
  271. static int avs_updown_mix_create(struct avs_dev *adev, struct avs_path_module *mod)
  272. {
  273. struct avs_tplg_module *t = mod->template;
  274. struct avs_updown_mixer_cfg cfg;
  275. int i;
  276. cfg.base.cpc = t->cfg_base->cpc;
  277. cfg.base.ibs = t->cfg_base->ibs;
  278. cfg.base.obs = t->cfg_base->obs;
  279. cfg.base.is_pages = t->cfg_base->is_pages;
  280. cfg.base.audio_fmt = *t->in_fmt;
  281. cfg.out_channel_config = t->cfg_ext->updown_mix.out_channel_config;
  282. cfg.coefficients_select = t->cfg_ext->updown_mix.coefficients_select;
  283. for (i = 0; i < AVS_CHANNELS_MAX; i++)
  284. cfg.coefficients[i] = t->cfg_ext->updown_mix.coefficients[i];
  285. cfg.channel_map = t->cfg_ext->updown_mix.channel_map;
  286. return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  287. t->core_id, t->domain, &cfg, sizeof(cfg),
  288. &mod->instance_id);
  289. }
  290. static int avs_src_create(struct avs_dev *adev, struct avs_path_module *mod)
  291. {
  292. struct avs_tplg_module *t = mod->template;
  293. struct avs_src_cfg cfg;
  294. cfg.base.cpc = t->cfg_base->cpc;
  295. cfg.base.ibs = t->cfg_base->ibs;
  296. cfg.base.obs = t->cfg_base->obs;
  297. cfg.base.is_pages = t->cfg_base->is_pages;
  298. cfg.base.audio_fmt = *t->in_fmt;
  299. cfg.out_freq = t->cfg_ext->src.out_freq;
  300. return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  301. t->core_id, t->domain, &cfg, sizeof(cfg),
  302. &mod->instance_id);
  303. }
  304. static int avs_asrc_create(struct avs_dev *adev, struct avs_path_module *mod)
  305. {
  306. struct avs_tplg_module *t = mod->template;
  307. struct avs_asrc_cfg cfg;
  308. memset(&cfg, 0, sizeof(cfg));
  309. cfg.base.cpc = t->cfg_base->cpc;
  310. cfg.base.ibs = t->cfg_base->ibs;
  311. cfg.base.obs = t->cfg_base->obs;
  312. cfg.base.is_pages = t->cfg_base->is_pages;
  313. cfg.base.audio_fmt = *t->in_fmt;
  314. cfg.out_freq = t->cfg_ext->asrc.out_freq;
  315. cfg.mode = t->cfg_ext->asrc.mode;
  316. cfg.disable_jitter_buffer = t->cfg_ext->asrc.disable_jitter_buffer;
  317. return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  318. t->core_id, t->domain, &cfg, sizeof(cfg),
  319. &mod->instance_id);
  320. }
  321. static int avs_aec_create(struct avs_dev *adev, struct avs_path_module *mod)
  322. {
  323. struct avs_tplg_module *t = mod->template;
  324. struct avs_aec_cfg cfg;
  325. cfg.base.cpc = t->cfg_base->cpc;
  326. cfg.base.ibs = t->cfg_base->ibs;
  327. cfg.base.obs = t->cfg_base->obs;
  328. cfg.base.is_pages = t->cfg_base->is_pages;
  329. cfg.base.audio_fmt = *t->in_fmt;
  330. cfg.ref_fmt = *t->cfg_ext->aec.ref_fmt;
  331. cfg.out_fmt = *t->cfg_ext->aec.out_fmt;
  332. cfg.cpc_lp_mode = t->cfg_ext->aec.cpc_lp_mode;
  333. return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  334. t->core_id, t->domain, &cfg, sizeof(cfg),
  335. &mod->instance_id);
  336. }
  337. static int avs_mux_create(struct avs_dev *adev, struct avs_path_module *mod)
  338. {
  339. struct avs_tplg_module *t = mod->template;
  340. struct avs_mux_cfg cfg;
  341. cfg.base.cpc = t->cfg_base->cpc;
  342. cfg.base.ibs = t->cfg_base->ibs;
  343. cfg.base.obs = t->cfg_base->obs;
  344. cfg.base.is_pages = t->cfg_base->is_pages;
  345. cfg.base.audio_fmt = *t->in_fmt;
  346. cfg.ref_fmt = *t->cfg_ext->mux.ref_fmt;
  347. cfg.out_fmt = *t->cfg_ext->mux.out_fmt;
  348. return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  349. t->core_id, t->domain, &cfg, sizeof(cfg),
  350. &mod->instance_id);
  351. }
  352. static int avs_wov_create(struct avs_dev *adev, struct avs_path_module *mod)
  353. {
  354. struct avs_tplg_module *t = mod->template;
  355. struct avs_wov_cfg cfg;
  356. cfg.base.cpc = t->cfg_base->cpc;
  357. cfg.base.ibs = t->cfg_base->ibs;
  358. cfg.base.obs = t->cfg_base->obs;
  359. cfg.base.is_pages = t->cfg_base->is_pages;
  360. cfg.base.audio_fmt = *t->in_fmt;
  361. cfg.cpc_lp_mode = t->cfg_ext->wov.cpc_lp_mode;
  362. return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  363. t->core_id, t->domain, &cfg, sizeof(cfg),
  364. &mod->instance_id);
  365. }
  366. static int avs_micsel_create(struct avs_dev *adev, struct avs_path_module *mod)
  367. {
  368. struct avs_tplg_module *t = mod->template;
  369. struct avs_micsel_cfg cfg;
  370. cfg.base.cpc = t->cfg_base->cpc;
  371. cfg.base.ibs = t->cfg_base->ibs;
  372. cfg.base.obs = t->cfg_base->obs;
  373. cfg.base.is_pages = t->cfg_base->is_pages;
  374. cfg.base.audio_fmt = *t->in_fmt;
  375. cfg.out_fmt = *t->cfg_ext->micsel.out_fmt;
  376. return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  377. t->core_id, t->domain, &cfg, sizeof(cfg),
  378. &mod->instance_id);
  379. }
  380. static int avs_modbase_create(struct avs_dev *adev, struct avs_path_module *mod)
  381. {
  382. struct avs_tplg_module *t = mod->template;
  383. struct avs_modcfg_base cfg;
  384. cfg.cpc = t->cfg_base->cpc;
  385. cfg.ibs = t->cfg_base->ibs;
  386. cfg.obs = t->cfg_base->obs;
  387. cfg.is_pages = t->cfg_base->is_pages;
  388. cfg.audio_fmt = *t->in_fmt;
  389. return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  390. t->core_id, t->domain, &cfg, sizeof(cfg),
  391. &mod->instance_id);
  392. }
  393. static int avs_modext_create(struct avs_dev *adev, struct avs_path_module *mod)
  394. {
  395. struct avs_tplg_module *t = mod->template;
  396. struct avs_tplg_modcfg_ext *tcfg = t->cfg_ext;
  397. struct avs_modcfg_ext *cfg;
  398. size_t cfg_size, num_pins;
  399. int ret, i;
  400. num_pins = tcfg->generic.num_input_pins + tcfg->generic.num_output_pins;
  401. cfg_size = struct_size(cfg, pin_fmts, num_pins);
  402. if (cfg_size > AVS_MAILBOX_SIZE)
  403. return -EINVAL;
  404. cfg = adev->modcfg_buf;
  405. memset(cfg, 0, cfg_size);
  406. cfg->base.cpc = t->cfg_base->cpc;
  407. cfg->base.ibs = t->cfg_base->ibs;
  408. cfg->base.obs = t->cfg_base->obs;
  409. cfg->base.is_pages = t->cfg_base->is_pages;
  410. cfg->base.audio_fmt = *t->in_fmt;
  411. cfg->num_input_pins = tcfg->generic.num_input_pins;
  412. cfg->num_output_pins = tcfg->generic.num_output_pins;
  413. /* configure pin formats */
  414. for (i = 0; i < num_pins; i++) {
  415. struct avs_tplg_pin_format *tpin = &tcfg->generic.pin_fmts[i];
  416. struct avs_pin_format *pin = &cfg->pin_fmts[i];
  417. pin->pin_index = tpin->pin_index;
  418. pin->iobs = tpin->iobs;
  419. pin->audio_fmt = *tpin->fmt;
  420. }
  421. ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
  422. t->core_id, t->domain, cfg, cfg_size,
  423. &mod->instance_id);
  424. return ret;
  425. }
  426. static int avs_probe_create(struct avs_dev *adev, struct avs_path_module *mod)
  427. {
  428. dev_err(adev->dev, "Probe module can't be instantiated by topology");
  429. return -EINVAL;
  430. }
  431. struct avs_module_create {
  432. guid_t *guid;
  433. int (*create)(struct avs_dev *adev, struct avs_path_module *mod);
  434. };
  435. static struct avs_module_create avs_module_create[] = {
  436. { &AVS_MIXIN_MOD_UUID, avs_modbase_create },
  437. { &AVS_MIXOUT_MOD_UUID, avs_modbase_create },
  438. { &AVS_KPBUFF_MOD_UUID, avs_modbase_create },
  439. { &AVS_COPIER_MOD_UUID, avs_copier_create },
  440. { &AVS_PEAKVOL_MOD_UUID, avs_peakvol_create },
  441. { &AVS_GAIN_MOD_UUID, avs_peakvol_create },
  442. { &AVS_MICSEL_MOD_UUID, avs_micsel_create },
  443. { &AVS_MUX_MOD_UUID, avs_mux_create },
  444. { &AVS_UPDWMIX_MOD_UUID, avs_updown_mix_create },
  445. { &AVS_SRCINTC_MOD_UUID, avs_src_create },
  446. { &AVS_AEC_MOD_UUID, avs_aec_create },
  447. { &AVS_ASRC_MOD_UUID, avs_asrc_create },
  448. { &AVS_INTELWOV_MOD_UUID, avs_wov_create },
  449. { &AVS_PROBE_MOD_UUID, avs_probe_create },
  450. };
  451. static int avs_path_module_type_create(struct avs_dev *adev, struct avs_path_module *mod)
  452. {
  453. const guid_t *type = &mod->template->cfg_ext->type;
  454. for (int i = 0; i < ARRAY_SIZE(avs_module_create); i++)
  455. if (guid_equal(type, avs_module_create[i].guid))
  456. return avs_module_create[i].create(adev, mod);
  457. return avs_modext_create(adev, mod);
  458. }
  459. static int avs_path_module_send_init_configs(struct avs_dev *adev, struct avs_path_module *mod)
  460. {
  461. struct avs_soc_component *acomp;
  462. acomp = to_avs_soc_component(mod->template->owner->owner->owner->owner->comp);
  463. u32 num_ids = mod->template->num_config_ids;
  464. u32 *ids = mod->template->config_ids;
  465. for (int i = 0; i < num_ids; i++) {
  466. struct avs_tplg_init_config *config = &acomp->tplg->init_configs[ids[i]];
  467. size_t len = config->length;
  468. void *data = config->data;
  469. u32 param = config->param;
  470. int ret;
  471. ret = avs_ipc_set_large_config(adev, mod->module_id, mod->instance_id,
  472. param, data, len);
  473. if (ret) {
  474. dev_err(adev->dev, "send initial module config failed: %d\n", ret);
  475. return AVS_IPC_RET(ret);
  476. }
  477. }
  478. return 0;
  479. }
  480. static void avs_path_module_free(struct avs_dev *adev, struct avs_path_module *mod)
  481. {
  482. kfree(mod);
  483. }
  484. static struct avs_path_module *
  485. avs_path_module_create(struct avs_dev *adev,
  486. struct avs_path_pipeline *owner,
  487. struct avs_tplg_module *template)
  488. {
  489. struct avs_path_module *mod;
  490. int module_id, ret;
  491. module_id = avs_get_module_id(adev, &template->cfg_ext->type);
  492. if (module_id < 0)
  493. return ERR_PTR(module_id);
  494. mod = kzalloc(sizeof(*mod), GFP_KERNEL);
  495. if (!mod)
  496. return ERR_PTR(-ENOMEM);
  497. mod->template = template;
  498. mod->module_id = module_id;
  499. mod->owner = owner;
  500. INIT_LIST_HEAD(&mod->node);
  501. ret = avs_path_module_type_create(adev, mod);
  502. if (ret) {
  503. dev_err(adev->dev, "module-type create failed: %d\n", ret);
  504. kfree(mod);
  505. return ERR_PTR(ret);
  506. }
  507. ret = avs_path_module_send_init_configs(adev, mod);
  508. if (ret) {
  509. kfree(mod);
  510. return ERR_PTR(ret);
  511. }
  512. return mod;
  513. }
  514. static int avs_path_binding_arm(struct avs_dev *adev, struct avs_path_binding *binding)
  515. {
  516. struct avs_path_module *this_mod, *target_mod;
  517. struct avs_path_pipeline *target_ppl;
  518. struct avs_path *target_path;
  519. struct avs_tplg_binding *t;
  520. t = binding->template;
  521. this_mod = avs_path_find_module(binding->owner,
  522. t->mod_id);
  523. if (!this_mod) {
  524. dev_err(adev->dev, "path mod %d not found\n", t->mod_id);
  525. return -EINVAL;
  526. }
  527. /* update with target_tplg_name too */
  528. target_path = avs_path_find_path(adev, t->target_tplg_name,
  529. t->target_path_tmpl_id);
  530. if (!target_path) {
  531. dev_err(adev->dev, "target path %s:%d not found\n",
  532. t->target_tplg_name, t->target_path_tmpl_id);
  533. return -EINVAL;
  534. }
  535. target_ppl = avs_path_find_pipeline(target_path,
  536. t->target_ppl_id);
  537. if (!target_ppl) {
  538. dev_err(adev->dev, "target ppl %d not found\n", t->target_ppl_id);
  539. return -EINVAL;
  540. }
  541. target_mod = avs_path_find_module(target_ppl, t->target_mod_id);
  542. if (!target_mod) {
  543. dev_err(adev->dev, "target mod %d not found\n", t->target_mod_id);
  544. return -EINVAL;
  545. }
  546. if (t->is_sink) {
  547. binding->sink = this_mod;
  548. binding->sink_pin = t->mod_pin;
  549. binding->source = target_mod;
  550. binding->source_pin = t->target_mod_pin;
  551. } else {
  552. binding->sink = target_mod;
  553. binding->sink_pin = t->target_mod_pin;
  554. binding->source = this_mod;
  555. binding->source_pin = t->mod_pin;
  556. }
  557. return 0;
  558. }
  559. static void avs_path_binding_free(struct avs_dev *adev, struct avs_path_binding *binding)
  560. {
  561. kfree(binding);
  562. }
  563. static struct avs_path_binding *avs_path_binding_create(struct avs_dev *adev,
  564. struct avs_path_pipeline *owner,
  565. struct avs_tplg_binding *t)
  566. {
  567. struct avs_path_binding *binding;
  568. binding = kzalloc(sizeof(*binding), GFP_KERNEL);
  569. if (!binding)
  570. return ERR_PTR(-ENOMEM);
  571. binding->template = t;
  572. binding->owner = owner;
  573. INIT_LIST_HEAD(&binding->node);
  574. return binding;
  575. }
  576. static int avs_path_pipeline_arm(struct avs_dev *adev,
  577. struct avs_path_pipeline *ppl)
  578. {
  579. struct avs_path_module *mod;
  580. list_for_each_entry(mod, &ppl->mod_list, node) {
  581. struct avs_path_module *source, *sink;
  582. int ret;
  583. /*
  584. * Only one module (so it's implicitly last) or it is the last
  585. * one, either way we don't have next module to bind it to.
  586. */
  587. if (mod == list_last_entry(&ppl->mod_list,
  588. struct avs_path_module, node))
  589. break;
  590. /* bind current module to next module on list */
  591. source = mod;
  592. sink = list_next_entry(mod, node);
  593. ret = avs_ipc_bind(adev, source->module_id, source->instance_id,
  594. sink->module_id, sink->instance_id, 0, 0);
  595. if (ret)
  596. return AVS_IPC_RET(ret);
  597. }
  598. return 0;
  599. }
  600. static void avs_path_pipeline_free(struct avs_dev *adev,
  601. struct avs_path_pipeline *ppl)
  602. {
  603. struct avs_path_binding *binding, *bsave;
  604. struct avs_path_module *mod, *save;
  605. list_for_each_entry_safe(binding, bsave, &ppl->binding_list, node) {
  606. list_del(&binding->node);
  607. avs_path_binding_free(adev, binding);
  608. }
  609. avs_dsp_delete_pipeline(adev, ppl->instance_id);
  610. /* Unload resources occupied by owned modules */
  611. list_for_each_entry_safe(mod, save, &ppl->mod_list, node) {
  612. avs_dsp_delete_module(adev, mod->module_id, mod->instance_id,
  613. mod->owner->instance_id,
  614. mod->template->core_id);
  615. avs_path_module_free(adev, mod);
  616. }
  617. list_del(&ppl->node);
  618. kfree(ppl);
  619. }
  620. static struct avs_path_pipeline *
  621. avs_path_pipeline_create(struct avs_dev *adev, struct avs_path *owner,
  622. struct avs_tplg_pipeline *template)
  623. {
  624. struct avs_path_pipeline *ppl;
  625. struct avs_tplg_pplcfg *cfg = template->cfg;
  626. struct avs_tplg_module *tmod;
  627. int ret, i;
  628. ppl = kzalloc(sizeof(*ppl), GFP_KERNEL);
  629. if (!ppl)
  630. return ERR_PTR(-ENOMEM);
  631. ppl->template = template;
  632. ppl->owner = owner;
  633. INIT_LIST_HEAD(&ppl->binding_list);
  634. INIT_LIST_HEAD(&ppl->mod_list);
  635. INIT_LIST_HEAD(&ppl->node);
  636. ret = avs_dsp_create_pipeline(adev, cfg->req_size, cfg->priority,
  637. cfg->lp, cfg->attributes,
  638. &ppl->instance_id);
  639. if (ret) {
  640. dev_err(adev->dev, "error creating pipeline %d\n", ret);
  641. kfree(ppl);
  642. return ERR_PTR(ret);
  643. }
  644. list_for_each_entry(tmod, &template->mod_list, node) {
  645. struct avs_path_module *mod;
  646. mod = avs_path_module_create(adev, ppl, tmod);
  647. if (IS_ERR(mod)) {
  648. ret = PTR_ERR(mod);
  649. dev_err(adev->dev, "error creating module %d\n", ret);
  650. goto init_err;
  651. }
  652. list_add_tail(&mod->node, &ppl->mod_list);
  653. }
  654. for (i = 0; i < template->num_bindings; i++) {
  655. struct avs_path_binding *binding;
  656. binding = avs_path_binding_create(adev, ppl, template->bindings[i]);
  657. if (IS_ERR(binding)) {
  658. ret = PTR_ERR(binding);
  659. dev_err(adev->dev, "error creating binding %d\n", ret);
  660. goto init_err;
  661. }
  662. list_add_tail(&binding->node, &ppl->binding_list);
  663. }
  664. return ppl;
  665. init_err:
  666. avs_path_pipeline_free(adev, ppl);
  667. return ERR_PTR(ret);
  668. }
  669. static int avs_path_init(struct avs_dev *adev, struct avs_path *path,
  670. struct avs_tplg_path *template, u32 dma_id)
  671. {
  672. struct avs_tplg_pipeline *tppl;
  673. path->owner = adev;
  674. path->template = template;
  675. path->dma_id = dma_id;
  676. INIT_LIST_HEAD(&path->ppl_list);
  677. INIT_LIST_HEAD(&path->node);
  678. /* create all the pipelines */
  679. list_for_each_entry(tppl, &template->ppl_list, node) {
  680. struct avs_path_pipeline *ppl;
  681. ppl = avs_path_pipeline_create(adev, path, tppl);
  682. if (IS_ERR(ppl))
  683. return PTR_ERR(ppl);
  684. list_add_tail(&ppl->node, &path->ppl_list);
  685. }
  686. spin_lock(&adev->path_list_lock);
  687. list_add_tail(&path->node, &adev->path_list);
  688. spin_unlock(&adev->path_list_lock);
  689. return 0;
  690. }
  691. static int avs_path_arm(struct avs_dev *adev, struct avs_path *path)
  692. {
  693. struct avs_path_pipeline *ppl;
  694. struct avs_path_binding *binding;
  695. int ret;
  696. list_for_each_entry(ppl, &path->ppl_list, node) {
  697. /*
  698. * Arm all ppl bindings before binding internal modules
  699. * as it costs no IPCs which isn't true for the latter.
  700. */
  701. list_for_each_entry(binding, &ppl->binding_list, node) {
  702. ret = avs_path_binding_arm(adev, binding);
  703. if (ret < 0)
  704. return ret;
  705. }
  706. ret = avs_path_pipeline_arm(adev, ppl);
  707. if (ret < 0)
  708. return ret;
  709. }
  710. return 0;
  711. }
  712. static void avs_path_free_unlocked(struct avs_path *path)
  713. {
  714. struct avs_path_pipeline *ppl, *save;
  715. spin_lock(&path->owner->path_list_lock);
  716. list_del(&path->node);
  717. spin_unlock(&path->owner->path_list_lock);
  718. list_for_each_entry_safe(ppl, save, &path->ppl_list, node)
  719. avs_path_pipeline_free(path->owner, ppl);
  720. kfree(path);
  721. }
  722. static struct avs_path *avs_path_create_unlocked(struct avs_dev *adev, u32 dma_id,
  723. struct avs_tplg_path *template)
  724. {
  725. struct avs_path *path;
  726. int ret;
  727. path = kzalloc(sizeof(*path), GFP_KERNEL);
  728. if (!path)
  729. return ERR_PTR(-ENOMEM);
  730. ret = avs_path_init(adev, path, template, dma_id);
  731. if (ret < 0)
  732. goto err;
  733. ret = avs_path_arm(adev, path);
  734. if (ret < 0)
  735. goto err;
  736. path->state = AVS_PPL_STATE_INVALID;
  737. return path;
  738. err:
  739. avs_path_free_unlocked(path);
  740. return ERR_PTR(ret);
  741. }
  742. void avs_path_free(struct avs_path *path)
  743. {
  744. struct avs_dev *adev = path->owner;
  745. mutex_lock(&adev->path_mutex);
  746. avs_path_free_unlocked(path);
  747. mutex_unlock(&adev->path_mutex);
  748. }
  749. struct avs_path *avs_path_create(struct avs_dev *adev, u32 dma_id,
  750. struct avs_tplg_path_template *template,
  751. struct snd_pcm_hw_params *fe_params,
  752. struct snd_pcm_hw_params *be_params)
  753. {
  754. struct avs_tplg_path *variant;
  755. struct avs_path *path;
  756. variant = avs_path_find_variant(adev, template, fe_params, be_params);
  757. if (!variant) {
  758. dev_err(adev->dev, "no matching variant found\n");
  759. return ERR_PTR(-ENOENT);
  760. }
  761. /* Serialize path and its components creation. */
  762. mutex_lock(&adev->path_mutex);
  763. /* Satisfy needs of avs_path_find_tplg(). */
  764. mutex_lock(&adev->comp_list_mutex);
  765. path = avs_path_create_unlocked(adev, dma_id, variant);
  766. mutex_unlock(&adev->comp_list_mutex);
  767. mutex_unlock(&adev->path_mutex);
  768. return path;
  769. }
  770. static int avs_path_bind_prepare(struct avs_dev *adev,
  771. struct avs_path_binding *binding)
  772. {
  773. const struct avs_audio_format *src_fmt, *sink_fmt;
  774. struct avs_tplg_module *tsource = binding->source->template;
  775. struct avs_path_module *source = binding->source;
  776. int ret;
  777. /*
  778. * only copier modules about to be bound
  779. * to output pin other than 0 need preparation
  780. */
  781. if (!binding->source_pin)
  782. return 0;
  783. if (!guid_equal(&tsource->cfg_ext->type, &AVS_COPIER_MOD_UUID))
  784. return 0;
  785. src_fmt = tsource->in_fmt;
  786. sink_fmt = binding->sink->template->in_fmt;
  787. ret = avs_ipc_copier_set_sink_format(adev, source->module_id,
  788. source->instance_id, binding->source_pin,
  789. src_fmt, sink_fmt);
  790. if (ret) {
  791. dev_err(adev->dev, "config copier failed: %d\n", ret);
  792. return AVS_IPC_RET(ret);
  793. }
  794. return 0;
  795. }
  796. int avs_path_bind(struct avs_path *path)
  797. {
  798. struct avs_path_pipeline *ppl;
  799. struct avs_dev *adev = path->owner;
  800. int ret;
  801. list_for_each_entry(ppl, &path->ppl_list, node) {
  802. struct avs_path_binding *binding;
  803. list_for_each_entry(binding, &ppl->binding_list, node) {
  804. struct avs_path_module *source, *sink;
  805. source = binding->source;
  806. sink = binding->sink;
  807. ret = avs_path_bind_prepare(adev, binding);
  808. if (ret < 0)
  809. return ret;
  810. ret = avs_ipc_bind(adev, source->module_id,
  811. source->instance_id, sink->module_id,
  812. sink->instance_id, binding->sink_pin,
  813. binding->source_pin);
  814. if (ret) {
  815. dev_err(adev->dev, "bind path failed: %d\n", ret);
  816. return AVS_IPC_RET(ret);
  817. }
  818. }
  819. }
  820. return 0;
  821. }
  822. int avs_path_unbind(struct avs_path *path)
  823. {
  824. struct avs_path_pipeline *ppl;
  825. struct avs_dev *adev = path->owner;
  826. int ret;
  827. list_for_each_entry(ppl, &path->ppl_list, node) {
  828. struct avs_path_binding *binding;
  829. list_for_each_entry(binding, &ppl->binding_list, node) {
  830. struct avs_path_module *source, *sink;
  831. source = binding->source;
  832. sink = binding->sink;
  833. ret = avs_ipc_unbind(adev, source->module_id,
  834. source->instance_id, sink->module_id,
  835. sink->instance_id, binding->sink_pin,
  836. binding->source_pin);
  837. if (ret) {
  838. dev_err(adev->dev, "unbind path failed: %d\n", ret);
  839. return AVS_IPC_RET(ret);
  840. }
  841. }
  842. }
  843. return 0;
  844. }
  845. int avs_path_reset(struct avs_path *path)
  846. {
  847. struct avs_path_pipeline *ppl;
  848. struct avs_dev *adev = path->owner;
  849. int ret;
  850. if (path->state == AVS_PPL_STATE_RESET)
  851. return 0;
  852. list_for_each_entry(ppl, &path->ppl_list, node) {
  853. ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id,
  854. AVS_PPL_STATE_RESET);
  855. if (ret) {
  856. dev_err(adev->dev, "reset path failed: %d\n", ret);
  857. path->state = AVS_PPL_STATE_INVALID;
  858. return AVS_IPC_RET(ret);
  859. }
  860. }
  861. path->state = AVS_PPL_STATE_RESET;
  862. return 0;
  863. }
  864. int avs_path_pause(struct avs_path *path)
  865. {
  866. struct avs_path_pipeline *ppl;
  867. struct avs_dev *adev = path->owner;
  868. int ret;
  869. if (path->state == AVS_PPL_STATE_PAUSED)
  870. return 0;
  871. list_for_each_entry_reverse(ppl, &path->ppl_list, node) {
  872. ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id,
  873. AVS_PPL_STATE_PAUSED);
  874. if (ret) {
  875. dev_err(adev->dev, "pause path failed: %d\n", ret);
  876. path->state = AVS_PPL_STATE_INVALID;
  877. return AVS_IPC_RET(ret);
  878. }
  879. }
  880. path->state = AVS_PPL_STATE_PAUSED;
  881. return 0;
  882. }
  883. int avs_path_run(struct avs_path *path, int trigger)
  884. {
  885. struct avs_path_pipeline *ppl;
  886. struct avs_dev *adev = path->owner;
  887. int ret;
  888. if (path->state == AVS_PPL_STATE_RUNNING && trigger == AVS_TPLG_TRIGGER_AUTO)
  889. return 0;
  890. list_for_each_entry(ppl, &path->ppl_list, node) {
  891. if (ppl->template->cfg->trigger != trigger)
  892. continue;
  893. ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id,
  894. AVS_PPL_STATE_RUNNING);
  895. if (ret) {
  896. dev_err(adev->dev, "run path failed: %d\n", ret);
  897. path->state = AVS_PPL_STATE_INVALID;
  898. return AVS_IPC_RET(ret);
  899. }
  900. }
  901. path->state = AVS_PPL_STATE_RUNNING;
  902. return 0;
  903. }