Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2//
  3// Copyright(c) 2021-2022 Intel Corporation
  4//
  5// Authors: Cezary Rojewski <cezary.rojewski@intel.com>
  6//          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
  7//
  8
  9#include <linux/firmware.h>
 10#include <linux/module.h>
 11#include <linux/slab.h>
 12#include <sound/hdaudio.h>
 13#include <sound/hdaudio_ext.h>
 14#include "avs.h"
 15#include "cldma.h"
 16#include "messages.h"
 17#include "registers.h"
 18#include "topology.h"
 19
 20#define AVS_ROM_STS_MASK		0xFF
 21#define AVS_ROM_INIT_DONE		0x1
 22#define SKL_ROM_BASEFW_ENTERED		0xF
 23#define APL_ROM_FW_ENTERED		0x5
 24#define AVS_ROM_INIT_POLLING_US		5
 25#define SKL_ROM_INIT_TIMEOUT_US		1000000
 26#define APL_ROM_INIT_TIMEOUT_US		300000
 27#define APL_ROM_INIT_RETRIES		3
 28
 29#define AVS_FW_INIT_POLLING_US		500
 30#define AVS_FW_INIT_TIMEOUT_MS		3000
 31#define AVS_FW_INIT_TIMEOUT_US		(AVS_FW_INIT_TIMEOUT_MS * 1000)
 32
 33#define AVS_CLDMA_START_DELAY_MS	100
 34
 35#define AVS_ROOT_DIR			"intel/avs"
 36#define AVS_BASEFW_FILENAME		"dsp_basefw.bin"
 37#define AVS_EXT_MANIFEST_MAGIC		0x31454124
 38#define SKL_MANIFEST_MAGIC		0x00000006
 39#define SKL_ADSPFW_OFFSET		0x284
 40#define APL_MANIFEST_MAGIC		0x44504324
 41#define APL_ADSPFW_OFFSET		0x2000
 42
 43/* Occasionally, engineering (release candidate) firmware is provided for testing. */
 44static bool debug_ignore_fw_version;
 45module_param_named(ignore_fw_version, debug_ignore_fw_version, bool, 0444);
 46MODULE_PARM_DESC(ignore_fw_version, "Ignore firmware version check 0=no (default), 1=yes");
 47
 48#define AVS_LIB_NAME_SIZE	8
 49
 50struct avs_fw_manifest {
 51	u32 id;
 52	u32 len;
 53	char name[AVS_LIB_NAME_SIZE];
 54	u32 preload_page_count;
 55	u32 img_flags;
 56	u32 feature_mask;
 57	struct avs_fw_version version;
 58} __packed;
 59static_assert(sizeof(struct avs_fw_manifest) == 36);
 60
 61struct avs_fw_ext_manifest {
 62	u32 id;
 63	u32 len;
 64	u16 version_major;
 65	u16 version_minor;
 66	u32 entries;
 67} __packed;
 68static_assert(sizeof(struct avs_fw_ext_manifest) == 16);
 69
 70static int avs_fw_ext_manifest_strip(struct firmware *fw)
 71{
 72	struct avs_fw_ext_manifest *man;
 73
 74	if (fw->size < sizeof(*man))
 75		return -EINVAL;
 76
 77	man = (struct avs_fw_ext_manifest *)fw->data;
 78	if (man->id == AVS_EXT_MANIFEST_MAGIC) {
 79		fw->data += man->len;
 80		fw->size -= man->len;
 81	}
 82
 83	return 0;
 84}
 85
 86static int avs_fw_manifest_offset(struct firmware *fw)
 87{
 88	/* Header type found in first DWORD of fw binary. */
 89	u32 magic = *(u32 *)fw->data;
 90
 91	switch (magic) {
 92	case SKL_MANIFEST_MAGIC:
 93		return SKL_ADSPFW_OFFSET;
 94	case APL_MANIFEST_MAGIC:
 95		return APL_ADSPFW_OFFSET;
 96	default:
 97		return -EINVAL;
 98	}
 99}
100
101static int avs_fw_manifest_strip_verify(struct avs_dev *adev, struct firmware *fw,
102					const struct avs_fw_version *min)
103{
104	struct avs_fw_manifest *man;
105	int offset, ret;
106
107	ret = avs_fw_ext_manifest_strip(fw);
108	if (ret)
109		return ret;
110
111	offset = avs_fw_manifest_offset(fw);
112	if (offset < 0)
113		return offset;
114
115	if (fw->size < offset + sizeof(*man))
116		return -EINVAL;
117	if (!min)
118		return 0;
119
120	man = (struct avs_fw_manifest *)(fw->data + offset);
121	if (man->version.major != min->major ||
122	    man->version.minor != min->minor ||
123	    man->version.hotfix != min->hotfix ||
124	    man->version.build < min->build) {
125		dev_warn(adev->dev, "bad FW version %d.%d.%d.%d, expected %d.%d.%d.%d or newer\n",
126			 man->version.major, man->version.minor,
127			 man->version.hotfix, man->version.build,
128			 min->major, min->minor, min->hotfix, min->build);
129
130		if (!debug_ignore_fw_version)
131			return -EINVAL;
132	}
133
134	return 0;
135}
136
137int avs_cldma_load_basefw(struct avs_dev *adev, struct firmware *fw)
138{
139	struct hda_cldma *cl = &code_loader;
140	unsigned int reg;
141	int ret;
142
143	ret = avs_dsp_op(adev, power, AVS_MAIN_CORE_MASK, true);
144	if (ret < 0)
145		return ret;
146
147	ret = avs_dsp_op(adev, reset, AVS_MAIN_CORE_MASK, false);
148	if (ret < 0)
149		return ret;
150
151	ret = hda_cldma_reset(cl);
152	if (ret < 0) {
153		dev_err(adev->dev, "cldma reset failed: %d\n", ret);
154		return ret;
155	}
156	hda_cldma_setup(cl);
157
158	ret = avs_dsp_op(adev, stall, AVS_MAIN_CORE_MASK, false);
159	if (ret < 0)
160		return ret;
161
162	reinit_completion(&adev->fw_ready);
163	avs_dsp_op(adev, int_control, true);
164
165	/* await ROM init */
166	ret = snd_hdac_adsp_readl_poll(adev, AVS_FW_REG_STATUS(adev), reg,
167				       (reg & AVS_ROM_INIT_DONE) == AVS_ROM_INIT_DONE,
168				       AVS_ROM_INIT_POLLING_US, SKL_ROM_INIT_TIMEOUT_US);
169	if (ret < 0) {
170		dev_err(adev->dev, "rom init timeout: %d\n", ret);
171		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
172		return ret;
173	}
174
175	hda_cldma_set_data(cl, (void *)fw->data, fw->size);
176	/* transfer firmware */
177	hda_cldma_transfer(cl, 0);
178	ret = snd_hdac_adsp_readl_poll(adev, AVS_FW_REG_STATUS(adev), reg,
179				       (reg & AVS_ROM_STS_MASK) == SKL_ROM_BASEFW_ENTERED,
180				       AVS_FW_INIT_POLLING_US, AVS_FW_INIT_TIMEOUT_US);
181	hda_cldma_stop(cl);
182	if (ret < 0) {
183		dev_err(adev->dev, "transfer fw failed: %d\n", ret);
184		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
185		return ret;
186	}
187
188	return 0;
189}
190
191int avs_cldma_load_library(struct avs_dev *adev, struct firmware *lib, u32 id)
192{
193	struct hda_cldma *cl = &code_loader;
194	int ret;
195
196	hda_cldma_set_data(cl, (void *)lib->data, lib->size);
197	/* transfer modules manifest */
198	hda_cldma_transfer(cl, msecs_to_jiffies(AVS_CLDMA_START_DELAY_MS));
199
200	/* DMA id ignored as there is only ever one code-loader DMA */
201	ret = avs_ipc_load_library(adev, 0, id);
202	hda_cldma_stop(cl);
203
204	if (ret) {
205		ret = AVS_IPC_RET(ret);
206		dev_err(adev->dev, "transfer lib %d failed: %d\n", id, ret);
207	}
208
209	return ret;
210}
211
212static int avs_cldma_load_module(struct avs_dev *adev, struct avs_module_entry *mentry)
213{
214	struct hda_cldma *cl = &code_loader;
215	const struct firmware *mod;
216	char *mod_name;
217	int ret;
218
219	mod_name = kasprintf(GFP_KERNEL, "%s/%s/dsp_mod_%pUL.bin", AVS_ROOT_DIR,
220			     adev->spec->name, mentry->uuid.b);
221	if (!mod_name)
222		return -ENOMEM;
223
224	ret = avs_request_firmware(adev, &mod, mod_name);
225	kfree(mod_name);
226	if (ret < 0)
227		return ret;
228
229	avs_hda_power_gating_enable(adev, false);
230	avs_hda_clock_gating_enable(adev, false);
231	avs_hda_l1sen_enable(adev, false);
232
233	hda_cldma_set_data(cl, (void *)mod->data, mod->size);
234	hda_cldma_transfer(cl, msecs_to_jiffies(AVS_CLDMA_START_DELAY_MS));
235	ret = avs_ipc_load_modules(adev, &mentry->module_id, 1);
236	hda_cldma_stop(cl);
237
238	avs_hda_l1sen_enable(adev, true);
239	avs_hda_clock_gating_enable(adev, true);
240	avs_hda_power_gating_enable(adev, true);
241
242	if (ret) {
243		dev_err(adev->dev, "load module %d failed: %d\n", mentry->module_id, ret);
244		avs_release_last_firmware(adev);
245		return AVS_IPC_RET(ret);
246	}
247
248	return 0;
249}
250
251int avs_cldma_transfer_modules(struct avs_dev *adev, bool load,
252			       struct avs_module_entry *mods, u32 num_mods)
253{
254	u16 *mod_ids;
255	int ret, i;
256
257	/* Either load to DSP or unload them to free space. */
258	if (load) {
259		for (i = 0; i < num_mods; i++) {
260			ret = avs_cldma_load_module(adev, &mods[i]);
261			if (ret)
262				return ret;
263		}
264
265		return 0;
266	}
267
268	mod_ids = kcalloc(num_mods, sizeof(u16), GFP_KERNEL);
269	if (!mod_ids)
270		return -ENOMEM;
271
272	for (i = 0; i < num_mods; i++)
273		mod_ids[i] = mods[i].module_id;
274
275	ret = avs_ipc_unload_modules(adev, mod_ids, num_mods);
276	kfree(mod_ids);
277	if (ret)
278		return AVS_IPC_RET(ret);
279
280	return 0;
281}
282
283static int
284avs_hda_init_rom(struct avs_dev *adev, unsigned int dma_id, bool purge)
285{
286	const struct avs_spec *const spec = adev->spec;
287	unsigned int corex_mask, reg;
288	int ret;
289
290	corex_mask = spec->core_init_mask & ~AVS_MAIN_CORE_MASK;
291
292	ret = avs_dsp_op(adev, power, spec->core_init_mask, true);
293	if (ret < 0)
294		goto err;
295
296	ret = avs_dsp_op(adev, reset, AVS_MAIN_CORE_MASK, false);
297	if (ret < 0)
298		goto err;
299
300	reinit_completion(&adev->fw_ready);
301	avs_dsp_op(adev, int_control, true);
302
303	/* set boot config */
304	ret = avs_ipc_set_boot_config(adev, dma_id, purge);
305	if (ret) {
306		ret = AVS_IPC_RET(ret);
307		goto err;
308	}
309
310	/* await ROM init */
311	ret = snd_hdac_adsp_readl_poll(adev, spec->sram->rom_status_offset, reg,
312				       (reg & 0xF) == AVS_ROM_INIT_DONE ||
313				       (reg & 0xF) == APL_ROM_FW_ENTERED,
314				       AVS_ROM_INIT_POLLING_US, APL_ROM_INIT_TIMEOUT_US);
315	if (ret < 0) {
316		dev_err(adev->dev, "rom init timeout: %d\n", ret);
317		goto err;
318	}
319
320	/* power down non-main cores */
321	if (corex_mask) {
322		ret = avs_dsp_op(adev, power, corex_mask, false);
323		if (ret < 0)
324			goto err;
325	}
326
327	return 0;
328
329err:
330	avs_dsp_core_disable(adev, spec->core_init_mask);
331	return ret;
332}
333
334static int avs_imr_load_basefw(struct avs_dev *adev)
335{
336	int ret;
337
338	/* DMA id ignored when flashing from IMR as no transfer occurs. */
339	ret = avs_hda_init_rom(adev, 0, false);
340	if (ret < 0) {
341		dev_err(adev->dev, "rom init failed: %d\n", ret);
342		return ret;
343	}
344
345	ret = wait_for_completion_timeout(&adev->fw_ready,
346					  msecs_to_jiffies(AVS_FW_INIT_TIMEOUT_MS));
347	if (!ret) {
348		dev_err(adev->dev, "firmware ready timeout\n");
349		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
350		return -ETIMEDOUT;
351	}
352
353	return 0;
354}
355
356int avs_hda_load_basefw(struct avs_dev *adev, struct firmware *fw)
357{
358	struct snd_pcm_substream substream;
359	struct snd_dma_buffer dmab;
360	struct hdac_ext_stream *estream;
361	struct hdac_stream *hstream;
362	struct hdac_bus *bus = &adev->base.core;
363	unsigned int sdfmt, reg;
364	int ret, i;
365
366	/* configure hda dma */
367	memset(&substream, 0, sizeof(substream));
368	substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
369	estream = snd_hdac_ext_stream_assign(bus, &substream,
370					     HDAC_EXT_STREAM_TYPE_HOST);
371	if (!estream)
372		return -ENODEV;
373	hstream = hdac_stream(estream);
374
375	/* code loading performed with default format */
376	sdfmt = snd_hdac_stream_format(1, 32, 48000);
377	ret = snd_hdac_dsp_prepare(hstream, sdfmt, fw->size, &dmab);
378	if (ret < 0)
379		goto release_stream;
380
381	/* enable SPIB for hda stream */
382	snd_hdac_stream_spbcap_enable(bus, true, hstream->index);
383	ret = snd_hdac_stream_set_spib(bus, hstream, fw->size);
384	if (ret)
385		goto cleanup_resources;
386
387	memcpy(dmab.area, fw->data, fw->size);
388
389	for (i = 0; i < APL_ROM_INIT_RETRIES; i++) {
390		unsigned int dma_id = hstream->stream_tag - 1;
391
392		ret = avs_hda_init_rom(adev, dma_id, true);
393		if (!ret)
394			break;
395		dev_info(adev->dev, "#%d rom init fail: %d\n", i + 1, ret);
396	}
397	if (ret < 0)
398		goto cleanup_resources;
399
400	/* transfer firmware */
401	snd_hdac_dsp_trigger(hstream, true);
402	ret = snd_hdac_adsp_readl_poll(adev, AVS_FW_REG_STATUS(adev), reg,
403				       (reg & AVS_ROM_STS_MASK) == APL_ROM_FW_ENTERED,
404				       AVS_FW_INIT_POLLING_US, AVS_FW_INIT_TIMEOUT_US);
405	snd_hdac_dsp_trigger(hstream, false);
406	if (ret < 0) {
407		dev_err(adev->dev, "transfer fw failed: %d\n", ret);
408		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
409	}
410
411cleanup_resources:
412	/* disable SPIB for hda stream */
413	snd_hdac_stream_spbcap_enable(bus, false, hstream->index);
414	snd_hdac_stream_set_spib(bus, hstream, 0);
415
416	snd_hdac_dsp_cleanup(hstream, &dmab);
417release_stream:
418	snd_hdac_ext_stream_release(estream, HDAC_EXT_STREAM_TYPE_HOST);
419
420	return ret;
421}
422
423int avs_hda_load_library(struct avs_dev *adev, struct firmware *lib, u32 id)
424{
425	struct snd_pcm_substream substream;
426	struct snd_dma_buffer dmab;
427	struct hdac_ext_stream *estream;
428	struct hdac_stream *stream;
429	struct hdac_bus *bus = &adev->base.core;
430	unsigned int sdfmt;
431	int ret;
432
433	/* configure hda dma */
434	memset(&substream, 0, sizeof(substream));
435	substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
436	estream = snd_hdac_ext_stream_assign(bus, &substream,
437					     HDAC_EXT_STREAM_TYPE_HOST);
438	if (!estream)
439		return -ENODEV;
440	stream = hdac_stream(estream);
441
442	/* code loading performed with default format */
443	sdfmt = snd_hdac_stream_format(1, 32, 48000);
444	ret = snd_hdac_dsp_prepare(stream, sdfmt, lib->size, &dmab);
445	if (ret < 0)
446		goto release_stream;
447
448	/* enable SPIB for hda stream */
449	snd_hdac_stream_spbcap_enable(bus, true, stream->index);
450	snd_hdac_stream_set_spib(bus, stream, lib->size);
451
452	memcpy(dmab.area, lib->data, lib->size);
453
454	/* transfer firmware */
455	snd_hdac_dsp_trigger(stream, true);
456	ret = avs_ipc_load_library(adev, stream->stream_tag - 1, id);
457	snd_hdac_dsp_trigger(stream, false);
458	if (ret) {
459		dev_err(adev->dev, "transfer lib %d failed: %d\n", id, ret);
460		ret = AVS_IPC_RET(ret);
461	}
462
463	/* disable SPIB for hda stream */
464	snd_hdac_stream_spbcap_enable(bus, false, stream->index);
465	snd_hdac_stream_set_spib(bus, stream, 0);
466
467	snd_hdac_dsp_cleanup(stream, &dmab);
468release_stream:
469	snd_hdac_ext_stream_release(estream, HDAC_EXT_STREAM_TYPE_HOST);
470
471	return ret;
472}
473
474int avs_hda_transfer_modules(struct avs_dev *adev, bool load,
475			     struct avs_module_entry *mods, u32 num_mods)
476{
477	/*
478	 * All platforms without CLDMA are equipped with IMR,
479	 * and thus the module transferring is offloaded to DSP.
480	 */
481	return 0;
482}
483
484int avs_dsp_load_libraries(struct avs_dev *adev, struct avs_tplg_library *libs, u32 num_libs)
485{
486	int start, id, i = 0;
487	int ret;
488
489	/* Calculate the id to assign for the next lib. */
490	for (id = 0; id < adev->fw_cfg.max_libs_count; id++)
491		if (adev->lib_names[id][0] == '\0')
492			break;
493	if (id + num_libs >= adev->fw_cfg.max_libs_count)
494		return -EINVAL;
495
496	start = id;
497	while (i < num_libs) {
498		struct avs_fw_manifest *man;
499		const struct firmware *fw;
500		struct firmware stripped_fw;
501		char *filename;
502		int j;
503
504		filename = kasprintf(GFP_KERNEL, "%s/%s/%s", AVS_ROOT_DIR, adev->spec->name,
505				     libs[i].name);
506		if (!filename)
507			return -ENOMEM;
508
509		/*
510		 * If any call after this one fails, requested firmware is not released with
511		 * avs_release_last_firmware() as failing to load code results in need for reload
512		 * of entire driver module. And then avs_release_firmwares() is in place already.
513		 */
514		ret = avs_request_firmware(adev, &fw, filename);
515		kfree(filename);
516		if (ret < 0)
517			return ret;
518
519		stripped_fw = *fw;
520		ret = avs_fw_manifest_strip_verify(adev, &stripped_fw, NULL);
521		if (ret) {
522			dev_err(adev->dev, "invalid library data: %d\n", ret);
523			return ret;
524		}
525
526		ret = avs_fw_manifest_offset(&stripped_fw);
527		if (ret < 0)
528			return ret;
529		man = (struct avs_fw_manifest *)(stripped_fw.data + ret);
530
531		/* Don't load anything that's already in DSP memory. */
532		for (j = 0; j < id; j++)
533			if (!strncmp(adev->lib_names[j], man->name, AVS_LIB_NAME_SIZE))
534				goto next_lib;
535
536		ret = avs_dsp_op(adev, load_lib, &stripped_fw, id);
537		if (ret)
538			return ret;
539
540		strscpy(adev->lib_names[id], man->name, AVS_LIB_NAME_SIZE);
541		id++;
542next_lib:
543		i++;
544	}
545
546	return start == id ? 1 : 0;
547}
548
549static int avs_dsp_load_basefw(struct avs_dev *adev)
550{
551	const struct avs_fw_version *min_req;
552	const struct avs_spec *const spec = adev->spec;
553	const struct firmware *fw;
554	struct firmware stripped_fw;
555	char *filename;
556	int ret;
557
558	filename = kasprintf(GFP_KERNEL, "%s/%s/%s", AVS_ROOT_DIR, spec->name, AVS_BASEFW_FILENAME);
559	if (!filename)
560		return -ENOMEM;
561
562	ret = avs_request_firmware(adev, &fw, filename);
563	kfree(filename);
564	if (ret < 0) {
565		dev_err(adev->dev, "request firmware failed: %d\n", ret);
566		return ret;
567	}
568
569	stripped_fw = *fw;
570	min_req = &adev->spec->min_fw_version;
571
572	ret = avs_fw_manifest_strip_verify(adev, &stripped_fw, min_req);
573	if (ret < 0) {
574		dev_err(adev->dev, "invalid firmware data: %d\n", ret);
575		goto release_fw;
576	}
577
578	ret = avs_dsp_op(adev, load_basefw, &stripped_fw);
579	if (ret < 0) {
580		dev_err(adev->dev, "basefw load failed: %d\n", ret);
581		goto release_fw;
582	}
583
584	ret = wait_for_completion_timeout(&adev->fw_ready,
585					  msecs_to_jiffies(AVS_FW_INIT_TIMEOUT_MS));
586	if (!ret) {
587		dev_err(adev->dev, "firmware ready timeout\n");
588		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
589		ret = -ETIMEDOUT;
590		goto release_fw;
591	}
592
593	return 0;
594
595release_fw:
596	avs_release_last_firmware(adev);
597	return ret;
598}
599
600int avs_dsp_boot_firmware(struct avs_dev *adev, bool purge)
601{
602	struct avs_soc_component *acomp;
603	int ret, i;
604
605	/* Forgo full boot if flash from IMR succeeds. */
606	if (!purge && avs_platattr_test(adev, IMR)) {
607		ret = avs_imr_load_basefw(adev);
608		if (!ret)
609			return 0;
610
611		dev_dbg(adev->dev, "firmware flash from imr failed: %d\n", ret);
612	}
613
614	/* Full boot, clear cached data except for basefw (slot 0). */
615	for (i = 1; i < adev->fw_cfg.max_libs_count; i++)
616		memset(adev->lib_names[i], 0, AVS_LIB_NAME_SIZE);
617
618	avs_hda_power_gating_enable(adev, false);
619	avs_hda_clock_gating_enable(adev, false);
620	avs_hda_l1sen_enable(adev, false);
621
622	ret = avs_dsp_load_basefw(adev);
623	if (ret)
624		goto reenable_gating;
625
626	mutex_lock(&adev->comp_list_mutex);
627	list_for_each_entry(acomp, &adev->comp_list, node) {
628		struct avs_tplg *tplg = acomp->tplg;
629
630		ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs);
631		if (ret < 0)
632			break;
633	}
634	mutex_unlock(&adev->comp_list_mutex);
635
636reenable_gating:
637	avs_hda_l1sen_enable(adev, true);
638	avs_hda_clock_gating_enable(adev, true);
639	avs_hda_power_gating_enable(adev, true);
640
641	if (ret < 0)
642		return ret;
643
644	/* With all code loaded, refresh module information. */
645	ret = avs_module_info_init(adev, true);
646	if (ret) {
647		dev_err(adev->dev, "init module info failed: %d\n", ret);
648		return ret;
649	}
650
651	return 0;
652}
653
654int avs_dsp_first_boot_firmware(struct avs_dev *adev)
655{
656	int ret, i;
657
658	if (avs_platattr_test(adev, CLDMA)) {
659		ret = hda_cldma_init(&code_loader, &adev->base.core,
660				     adev->dsp_ba, AVS_CL_DEFAULT_BUFFER_SIZE);
661		if (ret < 0) {
662			dev_err(adev->dev, "cldma init failed: %d\n", ret);
663			return ret;
664		}
665	}
666
667	ret = avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
668	if (ret < 0)
669		return ret;
670
671	ret = avs_dsp_boot_firmware(adev, true);
672	if (ret < 0) {
673		dev_err(adev->dev, "firmware boot failed: %d\n", ret);
674		return ret;
675	}
676
677	ret = avs_ipc_get_hw_config(adev, &adev->hw_cfg);
678	if (ret) {
679		dev_err(adev->dev, "get hw cfg failed: %d\n", ret);
680		return AVS_IPC_RET(ret);
681	}
682
683	ret = avs_ipc_get_fw_config(adev, &adev->fw_cfg);
684	if (ret) {
685		dev_err(adev->dev, "get fw cfg failed: %d\n", ret);
686		return AVS_IPC_RET(ret);
687	}
688
689	adev->core_refs = devm_kcalloc(adev->dev, adev->hw_cfg.dsp_cores,
690				       sizeof(*adev->core_refs), GFP_KERNEL);
691	adev->lib_names = devm_kcalloc(adev->dev, adev->fw_cfg.max_libs_count,
692				       sizeof(*adev->lib_names), GFP_KERNEL);
693	if (!adev->core_refs || !adev->lib_names)
694		return -ENOMEM;
695
696	for (i = 0; i < adev->fw_cfg.max_libs_count; i++) {
697		adev->lib_names[i] = devm_kzalloc(adev->dev, AVS_LIB_NAME_SIZE, GFP_KERNEL);
698		if (!adev->lib_names[i])
699			return -ENOMEM;
700	}
701
702	/* basefw always occupies slot 0 */
703	strscpy(adev->lib_names[0], "BASEFW", AVS_LIB_NAME_SIZE);
704
705	ida_init(&adev->ppl_ida);
706
707	return 0;
708}
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2//
  3// Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
  4//
  5// Authors: Cezary Rojewski <cezary.rojewski@intel.com>
  6//          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
  7//
  8
  9#include <linux/firmware.h>
 10#include <linux/module.h>
 11#include <linux/slab.h>
 12#include <sound/hdaudio.h>
 13#include <sound/hdaudio_ext.h>
 14#include "avs.h"
 15#include "cldma.h"
 16#include "messages.h"
 17#include "registers.h"
 18#include "topology.h"
 19
 20#define AVS_ROM_STS_MASK		0xFF
 21#define AVS_ROM_INIT_DONE		0x1
 22#define SKL_ROM_BASEFW_ENTERED		0xF
 23#define APL_ROM_FW_ENTERED		0x5
 24#define AVS_ROM_INIT_POLLING_US		5
 25#define SKL_ROM_INIT_TIMEOUT_US		1000000
 26#define APL_ROM_INIT_TIMEOUT_US		300000
 27#define APL_ROM_INIT_RETRIES		3
 28
 29#define AVS_FW_INIT_POLLING_US		500
 30#define AVS_FW_INIT_TIMEOUT_MS		3000
 31#define AVS_FW_INIT_TIMEOUT_US		(AVS_FW_INIT_TIMEOUT_MS * 1000)
 32
 33#define AVS_CLDMA_START_DELAY_MS	100
 34
 35#define AVS_ROOT_DIR			"intel/avs"
 36#define AVS_BASEFW_FILENAME		"dsp_basefw.bin"
 37#define AVS_EXT_MANIFEST_MAGIC		0x31454124
 38#define SKL_MANIFEST_MAGIC		0x00000006
 39#define SKL_ADSPFW_OFFSET		0x284
 40#define APL_MANIFEST_MAGIC		0x44504324
 41#define APL_ADSPFW_OFFSET		0x2000
 42
 43/* Occasionally, engineering (release candidate) firmware is provided for testing. */
 44static bool debug_ignore_fw_version;
 45module_param_named(ignore_fw_version, debug_ignore_fw_version, bool, 0444);
 46MODULE_PARM_DESC(ignore_fw_version, "Ignore firmware version check 0=no (default), 1=yes");
 47
 48#define AVS_LIB_NAME_SIZE	8
 49
 50struct avs_fw_manifest {
 51	u32 id;
 52	u32 len;
 53	char name[AVS_LIB_NAME_SIZE];
 54	u32 preload_page_count;
 55	u32 img_flags;
 56	u32 feature_mask;
 57	struct avs_fw_version version;
 58} __packed;
 
 59
 60struct avs_fw_ext_manifest {
 61	u32 id;
 62	u32 len;
 63	u16 version_major;
 64	u16 version_minor;
 65	u32 entries;
 66} __packed;
 
 67
 68static int avs_fw_ext_manifest_strip(struct firmware *fw)
 69{
 70	struct avs_fw_ext_manifest *man;
 71
 72	if (fw->size < sizeof(*man))
 73		return -EINVAL;
 74
 75	man = (struct avs_fw_ext_manifest *)fw->data;
 76	if (man->id == AVS_EXT_MANIFEST_MAGIC) {
 77		fw->data += man->len;
 78		fw->size -= man->len;
 79	}
 80
 81	return 0;
 82}
 83
 84static int avs_fw_manifest_offset(struct firmware *fw)
 85{
 86	/* Header type found in first DWORD of fw binary. */
 87	u32 magic = *(u32 *)fw->data;
 88
 89	switch (magic) {
 90	case SKL_MANIFEST_MAGIC:
 91		return SKL_ADSPFW_OFFSET;
 92	case APL_MANIFEST_MAGIC:
 93		return APL_ADSPFW_OFFSET;
 94	default:
 95		return -EINVAL;
 96	}
 97}
 98
 99static int avs_fw_manifest_strip_verify(struct avs_dev *adev, struct firmware *fw,
100					const struct avs_fw_version *min)
101{
102	struct avs_fw_manifest *man;
103	int offset, ret;
104
105	ret = avs_fw_ext_manifest_strip(fw);
106	if (ret)
107		return ret;
108
109	offset = avs_fw_manifest_offset(fw);
110	if (offset < 0)
111		return offset;
112
113	if (fw->size < offset + sizeof(*man))
114		return -EINVAL;
115	if (!min)
116		return 0;
117
118	man = (struct avs_fw_manifest *)(fw->data + offset);
119	if (man->version.major != min->major ||
120	    man->version.minor != min->minor ||
121	    man->version.hotfix != min->hotfix ||
122	    man->version.build < min->build) {
123		dev_warn(adev->dev, "bad FW version %d.%d.%d.%d, expected %d.%d.%d.%d or newer\n",
124			 man->version.major, man->version.minor,
125			 man->version.hotfix, man->version.build,
126			 min->major, min->minor, min->hotfix, min->build);
127
128		if (!debug_ignore_fw_version)
129			return -EINVAL;
130	}
131
132	return 0;
133}
134
135int avs_cldma_load_basefw(struct avs_dev *adev, struct firmware *fw)
136{
137	struct hda_cldma *cl = &code_loader;
138	unsigned int reg;
139	int ret;
140
141	ret = avs_dsp_op(adev, power, AVS_MAIN_CORE_MASK, true);
142	if (ret < 0)
143		return ret;
144
145	ret = avs_dsp_op(adev, reset, AVS_MAIN_CORE_MASK, false);
146	if (ret < 0)
147		return ret;
148
149	ret = hda_cldma_reset(cl);
150	if (ret < 0) {
151		dev_err(adev->dev, "cldma reset failed: %d\n", ret);
152		return ret;
153	}
154	hda_cldma_setup(cl);
155
156	ret = avs_dsp_op(adev, stall, AVS_MAIN_CORE_MASK, false);
157	if (ret < 0)
158		return ret;
159
160	reinit_completion(&adev->fw_ready);
161	avs_dsp_op(adev, int_control, true);
162
163	/* await ROM init */
164	ret = snd_hdac_adsp_readl_poll(adev, AVS_FW_REG_STATUS(adev), reg,
165				       (reg & AVS_ROM_INIT_DONE) == AVS_ROM_INIT_DONE,
166				       AVS_ROM_INIT_POLLING_US, SKL_ROM_INIT_TIMEOUT_US);
167	if (ret < 0) {
168		dev_err(adev->dev, "rom init timeout: %d\n", ret);
169		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
170		return ret;
171	}
172
173	hda_cldma_set_data(cl, (void *)fw->data, fw->size);
174	/* transfer firmware */
175	hda_cldma_transfer(cl, 0);
176	ret = snd_hdac_adsp_readl_poll(adev, AVS_FW_REG_STATUS(adev), reg,
177				       (reg & AVS_ROM_STS_MASK) == SKL_ROM_BASEFW_ENTERED,
178				       AVS_FW_INIT_POLLING_US, AVS_FW_INIT_TIMEOUT_US);
179	hda_cldma_stop(cl);
180	if (ret < 0) {
181		dev_err(adev->dev, "transfer fw failed: %d\n", ret);
182		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
183		return ret;
184	}
185
186	return 0;
187}
188
189int avs_cldma_load_library(struct avs_dev *adev, struct firmware *lib, u32 id)
190{
191	struct hda_cldma *cl = &code_loader;
192	int ret;
193
194	hda_cldma_set_data(cl, (void *)lib->data, lib->size);
195	/* transfer modules manifest */
196	hda_cldma_transfer(cl, msecs_to_jiffies(AVS_CLDMA_START_DELAY_MS));
197
198	/* DMA id ignored as there is only ever one code-loader DMA */
199	ret = avs_ipc_load_library(adev, 0, id);
200	hda_cldma_stop(cl);
201
202	if (ret) {
203		ret = AVS_IPC_RET(ret);
204		dev_err(adev->dev, "transfer lib %d failed: %d\n", id, ret);
205	}
206
207	return ret;
208}
209
210static int avs_cldma_load_module(struct avs_dev *adev, struct avs_module_entry *mentry)
211{
212	struct hda_cldma *cl = &code_loader;
213	const struct firmware *mod;
214	char *mod_name;
215	int ret;
216
217	mod_name = kasprintf(GFP_KERNEL, "%s/%s/dsp_mod_%pUL.bin", AVS_ROOT_DIR,
218			     adev->spec->name, mentry->uuid.b);
219	if (!mod_name)
220		return -ENOMEM;
221
222	ret = avs_request_firmware(adev, &mod, mod_name);
223	kfree(mod_name);
224	if (ret < 0)
225		return ret;
226
227	avs_hda_power_gating_enable(adev, false);
228	avs_hda_clock_gating_enable(adev, false);
229	avs_hda_l1sen_enable(adev, false);
230
231	hda_cldma_set_data(cl, (void *)mod->data, mod->size);
232	hda_cldma_transfer(cl, msecs_to_jiffies(AVS_CLDMA_START_DELAY_MS));
233	ret = avs_ipc_load_modules(adev, &mentry->module_id, 1);
234	hda_cldma_stop(cl);
235
236	avs_hda_l1sen_enable(adev, true);
237	avs_hda_clock_gating_enable(adev, true);
238	avs_hda_power_gating_enable(adev, true);
239
240	if (ret) {
241		dev_err(adev->dev, "load module %d failed: %d\n", mentry->module_id, ret);
242		avs_release_last_firmware(adev);
243		return AVS_IPC_RET(ret);
244	}
245
246	return 0;
247}
248
249int avs_cldma_transfer_modules(struct avs_dev *adev, bool load,
250			       struct avs_module_entry *mods, u32 num_mods)
251{
252	u16 *mod_ids;
253	int ret, i;
254
255	/* Either load to DSP or unload them to free space. */
256	if (load) {
257		for (i = 0; i < num_mods; i++) {
258			ret = avs_cldma_load_module(adev, &mods[i]);
259			if (ret)
260				return ret;
261		}
262
263		return 0;
264	}
265
266	mod_ids = kcalloc(num_mods, sizeof(u16), GFP_KERNEL);
267	if (!mod_ids)
268		return -ENOMEM;
269
270	for (i = 0; i < num_mods; i++)
271		mod_ids[i] = mods[i].module_id;
272
273	ret = avs_ipc_unload_modules(adev, mod_ids, num_mods);
274	kfree(mod_ids);
275	if (ret)
276		return AVS_IPC_RET(ret);
277
278	return 0;
279}
280
281static int
282avs_hda_init_rom(struct avs_dev *adev, unsigned int dma_id, bool purge)
283{
284	const struct avs_spec *const spec = adev->spec;
285	unsigned int corex_mask, reg;
286	int ret;
287
288	corex_mask = spec->core_init_mask & ~AVS_MAIN_CORE_MASK;
289
290	ret = avs_dsp_op(adev, power, spec->core_init_mask, true);
291	if (ret < 0)
292		goto err;
293
294	ret = avs_dsp_op(adev, reset, AVS_MAIN_CORE_MASK, false);
295	if (ret < 0)
296		goto err;
297
298	reinit_completion(&adev->fw_ready);
299	avs_dsp_op(adev, int_control, true);
300
301	/* set boot config */
302	ret = avs_ipc_set_boot_config(adev, dma_id, purge);
303	if (ret) {
304		ret = AVS_IPC_RET(ret);
305		goto err;
306	}
307
308	/* await ROM init */
309	ret = snd_hdac_adsp_readq_poll(adev, spec->rom_status, reg,
310				       (reg & 0xF) == AVS_ROM_INIT_DONE ||
311				       (reg & 0xF) == APL_ROM_FW_ENTERED,
312				       AVS_ROM_INIT_POLLING_US, APL_ROM_INIT_TIMEOUT_US);
313	if (ret < 0) {
314		dev_err(adev->dev, "rom init timeout: %d\n", ret);
315		goto err;
316	}
317
318	/* power down non-main cores */
319	if (corex_mask) {
320		ret = avs_dsp_op(adev, power, corex_mask, false);
321		if (ret < 0)
322			goto err;
323	}
324
325	return 0;
326
327err:
328	avs_dsp_core_disable(adev, spec->core_init_mask);
329	return ret;
330}
331
332static int avs_imr_load_basefw(struct avs_dev *adev)
333{
334	int ret;
335
336	/* DMA id ignored when flashing from IMR as no transfer occurs. */
337	ret = avs_hda_init_rom(adev, 0, false);
338	if (ret < 0) {
339		dev_err(adev->dev, "rom init failed: %d\n", ret);
340		return ret;
341	}
342
343	ret = wait_for_completion_timeout(&adev->fw_ready,
344					  msecs_to_jiffies(AVS_FW_INIT_TIMEOUT_MS));
345	if (!ret) {
346		dev_err(adev->dev, "firmware ready timeout\n");
347		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
348		return -ETIMEDOUT;
349	}
350
351	return 0;
352}
353
354int avs_hda_load_basefw(struct avs_dev *adev, struct firmware *fw)
355{
356	struct snd_pcm_substream substream;
357	struct snd_dma_buffer dmab;
358	struct hdac_ext_stream *estream;
359	struct hdac_stream *hstream;
360	struct hdac_bus *bus = &adev->base.core;
361	unsigned int sdfmt, reg;
362	int ret, i;
363
364	/* configure hda dma */
365	memset(&substream, 0, sizeof(substream));
366	substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
367	estream = snd_hdac_ext_stream_assign(bus, &substream,
368					     HDAC_EXT_STREAM_TYPE_HOST);
369	if (!estream)
370		return -ENODEV;
371	hstream = hdac_stream(estream);
372
373	/* code loading performed with default format */
374	sdfmt = snd_hdac_calc_stream_format(48000, 1, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
375	ret = snd_hdac_dsp_prepare(hstream, sdfmt, fw->size, &dmab);
376	if (ret < 0)
377		goto release_stream;
378
379	/* enable SPIB for hda stream */
380	snd_hdac_stream_spbcap_enable(bus, true, hstream->index);
381	ret = snd_hdac_stream_set_spib(bus, hstream, fw->size);
382	if (ret)
383		goto cleanup_resources;
384
385	memcpy(dmab.area, fw->data, fw->size);
386
387	for (i = 0; i < APL_ROM_INIT_RETRIES; i++) {
388		unsigned int dma_id = hstream->stream_tag - 1;
389
390		ret = avs_hda_init_rom(adev, dma_id, true);
391		if (!ret)
392			break;
393		dev_info(adev->dev, "#%d rom init fail: %d\n", i + 1, ret);
394	}
395	if (ret < 0)
396		goto cleanup_resources;
397
398	/* transfer firmware */
399	snd_hdac_dsp_trigger(hstream, true);
400	ret = snd_hdac_adsp_readl_poll(adev, AVS_FW_REG_STATUS(adev), reg,
401				       (reg & AVS_ROM_STS_MASK) == APL_ROM_FW_ENTERED,
402				       AVS_FW_INIT_POLLING_US, AVS_FW_INIT_TIMEOUT_US);
403	snd_hdac_dsp_trigger(hstream, false);
404	if (ret < 0) {
405		dev_err(adev->dev, "transfer fw failed: %d\n", ret);
406		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
407	}
408
409cleanup_resources:
410	/* disable SPIB for hda stream */
411	snd_hdac_stream_spbcap_enable(bus, false, hstream->index);
412	snd_hdac_stream_set_spib(bus, hstream, 0);
413
414	snd_hdac_dsp_cleanup(hstream, &dmab);
415release_stream:
416	snd_hdac_ext_stream_release(estream, HDAC_EXT_STREAM_TYPE_HOST);
417
418	return ret;
419}
420
421int avs_hda_load_library(struct avs_dev *adev, struct firmware *lib, u32 id)
422{
423	struct snd_pcm_substream substream;
424	struct snd_dma_buffer dmab;
425	struct hdac_ext_stream *estream;
426	struct hdac_stream *stream;
427	struct hdac_bus *bus = &adev->base.core;
428	unsigned int sdfmt;
429	int ret;
430
431	/* configure hda dma */
432	memset(&substream, 0, sizeof(substream));
433	substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
434	estream = snd_hdac_ext_stream_assign(bus, &substream,
435					     HDAC_EXT_STREAM_TYPE_HOST);
436	if (!estream)
437		return -ENODEV;
438	stream = hdac_stream(estream);
439
440	/* code loading performed with default format */
441	sdfmt = snd_hdac_calc_stream_format(48000, 1, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
442	ret = snd_hdac_dsp_prepare(stream, sdfmt, lib->size, &dmab);
443	if (ret < 0)
444		goto release_stream;
445
446	/* enable SPIB for hda stream */
447	snd_hdac_stream_spbcap_enable(bus, true, stream->index);
448	snd_hdac_stream_set_spib(bus, stream, lib->size);
449
450	memcpy(dmab.area, lib->data, lib->size);
451
452	/* transfer firmware */
453	snd_hdac_dsp_trigger(stream, true);
454	ret = avs_ipc_load_library(adev, stream->stream_tag - 1, id);
455	snd_hdac_dsp_trigger(stream, false);
456	if (ret) {
457		dev_err(adev->dev, "transfer lib %d failed: %d\n", id, ret);
458		ret = AVS_IPC_RET(ret);
459	}
460
461	/* disable SPIB for hda stream */
462	snd_hdac_stream_spbcap_enable(bus, false, stream->index);
463	snd_hdac_stream_set_spib(bus, stream, 0);
464
465	snd_hdac_dsp_cleanup(stream, &dmab);
466release_stream:
467	snd_hdac_ext_stream_release(estream, HDAC_EXT_STREAM_TYPE_HOST);
468
469	return ret;
470}
471
472int avs_hda_transfer_modules(struct avs_dev *adev, bool load,
473			     struct avs_module_entry *mods, u32 num_mods)
474{
475	/*
476	 * All platforms without CLDMA are equipped with IMR,
477	 * and thus the module transferring is offloaded to DSP.
478	 */
479	return 0;
480}
481
482int avs_dsp_load_libraries(struct avs_dev *adev, struct avs_tplg_library *libs, u32 num_libs)
483{
484	int start, id, i = 0;
485	int ret;
486
487	/* Calculate the id to assign for the next lib. */
488	for (id = 0; id < adev->fw_cfg.max_libs_count; id++)
489		if (adev->lib_names[id][0] == '\0')
490			break;
491	if (id + num_libs >= adev->fw_cfg.max_libs_count)
492		return -EINVAL;
493
494	start = id;
495	while (i < num_libs) {
496		struct avs_fw_manifest *man;
497		const struct firmware *fw;
498		struct firmware stripped_fw;
499		char *filename;
500		int j;
501
502		filename = kasprintf(GFP_KERNEL, "%s/%s/%s", AVS_ROOT_DIR, adev->spec->name,
503				     libs[i].name);
504		if (!filename)
505			return -ENOMEM;
506
507		/*
508		 * If any call after this one fails, requested firmware is not released with
509		 * avs_release_last_firmware() as failing to load code results in need for reload
510		 * of entire driver module. And then avs_release_firmwares() is in place already.
511		 */
512		ret = avs_request_firmware(adev, &fw, filename);
513		kfree(filename);
514		if (ret < 0)
515			return ret;
516
517		stripped_fw = *fw;
518		ret = avs_fw_manifest_strip_verify(adev, &stripped_fw, NULL);
519		if (ret) {
520			dev_err(adev->dev, "invalid library data: %d\n", ret);
521			return ret;
522		}
523
524		ret = avs_fw_manifest_offset(&stripped_fw);
525		if (ret < 0)
526			return ret;
527		man = (struct avs_fw_manifest *)(stripped_fw.data + ret);
528
529		/* Don't load anything that's already in DSP memory. */
530		for (j = 0; j < id; j++)
531			if (!strncmp(adev->lib_names[j], man->name, AVS_LIB_NAME_SIZE))
532				goto next_lib;
533
534		ret = avs_dsp_op(adev, load_lib, &stripped_fw, id);
535		if (ret)
536			return ret;
537
538		strncpy(adev->lib_names[id], man->name, AVS_LIB_NAME_SIZE);
539		id++;
540next_lib:
541		i++;
542	}
543
544	return start == id ? 1 : 0;
545}
546
547static int avs_dsp_load_basefw(struct avs_dev *adev)
548{
549	const struct avs_fw_version *min_req;
550	const struct avs_spec *const spec = adev->spec;
551	const struct firmware *fw;
552	struct firmware stripped_fw;
553	char *filename;
554	int ret;
555
556	filename = kasprintf(GFP_KERNEL, "%s/%s/%s", AVS_ROOT_DIR, spec->name, AVS_BASEFW_FILENAME);
557	if (!filename)
558		return -ENOMEM;
559
560	ret = avs_request_firmware(adev, &fw, filename);
561	kfree(filename);
562	if (ret < 0) {
563		dev_err(adev->dev, "request firmware failed: %d\n", ret);
564		return ret;
565	}
566
567	stripped_fw = *fw;
568	min_req = &adev->spec->min_fw_version;
569
570	ret = avs_fw_manifest_strip_verify(adev, &stripped_fw, min_req);
571	if (ret < 0) {
572		dev_err(adev->dev, "invalid firmware data: %d\n", ret);
573		goto release_fw;
574	}
575
576	ret = avs_dsp_op(adev, load_basefw, &stripped_fw);
577	if (ret < 0) {
578		dev_err(adev->dev, "basefw load failed: %d\n", ret);
579		goto release_fw;
580	}
581
582	ret = wait_for_completion_timeout(&adev->fw_ready,
583					  msecs_to_jiffies(AVS_FW_INIT_TIMEOUT_MS));
584	if (!ret) {
585		dev_err(adev->dev, "firmware ready timeout\n");
586		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
587		ret = -ETIMEDOUT;
588		goto release_fw;
589	}
590
591	return 0;
592
593release_fw:
594	avs_release_last_firmware(adev);
595	return ret;
596}
597
598int avs_dsp_boot_firmware(struct avs_dev *adev, bool purge)
599{
600	struct avs_soc_component *acomp;
601	int ret, i;
602
603	/* Forgo full boot if flash from IMR succeeds. */
604	if (!purge && avs_platattr_test(adev, IMR)) {
605		ret = avs_imr_load_basefw(adev);
606		if (!ret)
607			return 0;
608
609		dev_dbg(adev->dev, "firmware flash from imr failed: %d\n", ret);
610	}
611
612	/* Full boot, clear cached data except for basefw (slot 0). */
613	for (i = 1; i < adev->fw_cfg.max_libs_count; i++)
614		memset(adev->lib_names[i], 0, AVS_LIB_NAME_SIZE);
615
616	avs_hda_power_gating_enable(adev, false);
617	avs_hda_clock_gating_enable(adev, false);
618	avs_hda_l1sen_enable(adev, false);
619
620	ret = avs_dsp_load_basefw(adev);
621	if (ret)
622		goto reenable_gating;
623
624	mutex_lock(&adev->comp_list_mutex);
625	list_for_each_entry(acomp, &adev->comp_list, node) {
626		struct avs_tplg *tplg = acomp->tplg;
627
628		ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs);
629		if (ret < 0)
630			break;
631	}
632	mutex_unlock(&adev->comp_list_mutex);
633
634reenable_gating:
635	avs_hda_l1sen_enable(adev, true);
636	avs_hda_clock_gating_enable(adev, true);
637	avs_hda_power_gating_enable(adev, true);
638
639	if (ret < 0)
640		return ret;
641
642	/* With all code loaded, refresh module information. */
643	ret = avs_module_info_init(adev, true);
644	if (ret) {
645		dev_err(adev->dev, "init module info failed: %d\n", ret);
646		return ret;
647	}
648
649	return 0;
650}
651
652int avs_dsp_first_boot_firmware(struct avs_dev *adev)
653{
654	int ret, i;
655
656	if (avs_platattr_test(adev, CLDMA)) {
657		ret = hda_cldma_init(&code_loader, &adev->base.core,
658				     adev->dsp_ba, AVS_CL_DEFAULT_BUFFER_SIZE);
659		if (ret < 0) {
660			dev_err(adev->dev, "cldma init failed: %d\n", ret);
661			return ret;
662		}
663	}
664
 
 
 
 
665	ret = avs_dsp_boot_firmware(adev, true);
666	if (ret < 0) {
667		dev_err(adev->dev, "firmware boot failed: %d\n", ret);
668		return ret;
669	}
670
671	ret = avs_ipc_get_hw_config(adev, &adev->hw_cfg);
672	if (ret) {
673		dev_err(adev->dev, "get hw cfg failed: %d\n", ret);
674		return AVS_IPC_RET(ret);
675	}
676
677	ret = avs_ipc_get_fw_config(adev, &adev->fw_cfg);
678	if (ret) {
679		dev_err(adev->dev, "get fw cfg failed: %d\n", ret);
680		return AVS_IPC_RET(ret);
681	}
682
683	adev->core_refs = devm_kcalloc(adev->dev, adev->hw_cfg.dsp_cores,
684				       sizeof(*adev->core_refs), GFP_KERNEL);
685	adev->lib_names = devm_kcalloc(adev->dev, adev->fw_cfg.max_libs_count,
686				       sizeof(*adev->lib_names), GFP_KERNEL);
687	if (!adev->core_refs || !adev->lib_names)
688		return -ENOMEM;
689
690	for (i = 0; i < adev->fw_cfg.max_libs_count; i++) {
691		adev->lib_names[i] = devm_kzalloc(adev->dev, AVS_LIB_NAME_SIZE, GFP_KERNEL);
692		if (!adev->lib_names[i])
693			return -ENOMEM;
694	}
695
696	/* basefw always occupies slot 0 */
697	strcpy(&adev->lib_names[0][0], "BASEFW");
698
699	ida_init(&adev->ppl_ida);
700
701	return 0;
702}