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. */
44 static bool debug_ignore_fw_version;
45 module_param_named(ignore_fw_version, debug_ignore_fw_version, bool, 0444);
46 MODULE_PARM_DESC(ignore_fw_version, "Verify FW version 0=yes (default), 1=no");
47 
48 #define AVS_LIB_NAME_SIZE	8
49 
50 struct 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 
60 struct avs_fw_ext_manifest {
61 	u32 id;
62 	u32 len;
63 	u16 version_major;
64 	u16 version_minor;
65 	u32 entries;
66 } __packed;
67 
avs_fw_ext_manifest_strip(struct firmware * fw)68 static 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 
avs_fw_manifest_offset(struct firmware * fw)84 static 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 
avs_fw_manifest_strip_verify(struct avs_dev * adev,struct firmware * fw,const struct avs_fw_version * min)99 static 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 
avs_cldma_load_basefw(struct avs_dev * adev,struct firmware * fw)135 int 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 
avs_cldma_load_library(struct avs_dev * adev,struct firmware * lib,u32 id)189 int 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 
avs_cldma_load_module(struct avs_dev * adev,struct avs_module_entry * mentry)210 static 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 	hda_cldma_set_data(cl, (void *)mod->data, mod->size);
228 	hda_cldma_transfer(cl, msecs_to_jiffies(AVS_CLDMA_START_DELAY_MS));
229 	ret = avs_ipc_load_modules(adev, &mentry->module_id, 1);
230 	hda_cldma_stop(cl);
231 
232 	if (ret) {
233 		dev_err(adev->dev, "load module %d failed: %d\n", mentry->module_id, ret);
234 		avs_release_last_firmware(adev);
235 		return AVS_IPC_RET(ret);
236 	}
237 
238 	return 0;
239 }
240 
avs_cldma_transfer_modules(struct avs_dev * adev,bool load,struct avs_module_entry * mods,u32 num_mods)241 int avs_cldma_transfer_modules(struct avs_dev *adev, bool load,
242 			       struct avs_module_entry *mods, u32 num_mods)
243 {
244 	u16 *mod_ids;
245 	int ret, i;
246 
247 	/* Either load to DSP or unload them to free space. */
248 	if (load) {
249 		for (i = 0; i < num_mods; i++) {
250 			ret = avs_cldma_load_module(adev, &mods[i]);
251 			if (ret)
252 				return ret;
253 		}
254 
255 		return 0;
256 	}
257 
258 	mod_ids = kcalloc(num_mods, sizeof(u16), GFP_KERNEL);
259 	if (!mod_ids)
260 		return -ENOMEM;
261 
262 	for (i = 0; i < num_mods; i++)
263 		mod_ids[i] = mods[i].module_id;
264 
265 	ret = avs_ipc_unload_modules(adev, mod_ids, num_mods);
266 	kfree(mod_ids);
267 	if (ret)
268 		return AVS_IPC_RET(ret);
269 
270 	return 0;
271 }
272 
273 static int
avs_hda_init_rom(struct avs_dev * adev,unsigned int dma_id,bool purge)274 avs_hda_init_rom(struct avs_dev *adev, unsigned int dma_id, bool purge)
275 {
276 	const struct avs_spec *const spec = adev->spec;
277 	unsigned int corex_mask, reg;
278 	int ret;
279 
280 	corex_mask = spec->core_init_mask & ~AVS_MAIN_CORE_MASK;
281 
282 	ret = avs_dsp_op(adev, power, spec->core_init_mask, true);
283 	if (ret < 0)
284 		goto err;
285 
286 	ret = avs_dsp_op(adev, reset, AVS_MAIN_CORE_MASK, false);
287 	if (ret < 0)
288 		goto err;
289 
290 	reinit_completion(&adev->fw_ready);
291 	avs_dsp_op(adev, int_control, true);
292 
293 	/* set boot config */
294 	ret = avs_ipc_set_boot_config(adev, dma_id, purge);
295 	if (ret) {
296 		ret = AVS_IPC_RET(ret);
297 		goto err;
298 	}
299 
300 	/* await ROM init */
301 	ret = snd_hdac_adsp_readq_poll(adev, spec->rom_status, reg,
302 				       (reg & 0xF) == AVS_ROM_INIT_DONE ||
303 				       (reg & 0xF) == APL_ROM_FW_ENTERED,
304 				       AVS_ROM_INIT_POLLING_US, APL_ROM_INIT_TIMEOUT_US);
305 	if (ret < 0) {
306 		dev_err(adev->dev, "rom init timeout: %d\n", ret);
307 		goto err;
308 	}
309 
310 	/* power down non-main cores */
311 	if (corex_mask) {
312 		ret = avs_dsp_op(adev, power, corex_mask, false);
313 		if (ret < 0)
314 			goto err;
315 	}
316 
317 	return 0;
318 
319 err:
320 	avs_dsp_core_disable(adev, spec->core_init_mask);
321 	return ret;
322 }
323 
avs_imr_load_basefw(struct avs_dev * adev)324 static int avs_imr_load_basefw(struct avs_dev *adev)
325 {
326 	int ret;
327 
328 	/* DMA id ignored when flashing from IMR as no transfer occurs. */
329 	ret = avs_hda_init_rom(adev, 0, false);
330 	if (ret < 0) {
331 		dev_err(adev->dev, "rom init failed: %d\n", ret);
332 		return ret;
333 	}
334 
335 	ret = wait_for_completion_timeout(&adev->fw_ready,
336 					  msecs_to_jiffies(AVS_FW_INIT_TIMEOUT_MS));
337 	if (!ret) {
338 		dev_err(adev->dev, "firmware ready timeout\n");
339 		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
340 		return -ETIMEDOUT;
341 	}
342 
343 	return 0;
344 }
345 
avs_hda_load_basefw(struct avs_dev * adev,struct firmware * fw)346 int avs_hda_load_basefw(struct avs_dev *adev, struct firmware *fw)
347 {
348 	struct snd_pcm_substream substream;
349 	struct snd_dma_buffer dmab;
350 	struct hdac_ext_stream *estream;
351 	struct hdac_stream *hstream;
352 	struct hdac_bus *bus = &adev->base.core;
353 	unsigned int sdfmt, reg;
354 	int ret, i;
355 
356 	/* configure hda dma */
357 	memset(&substream, 0, sizeof(substream));
358 	substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
359 	estream = snd_hdac_ext_stream_assign(bus, &substream,
360 					     HDAC_EXT_STREAM_TYPE_HOST);
361 	if (!estream)
362 		return -ENODEV;
363 	hstream = hdac_stream(estream);
364 
365 	/* code loading performed with default format */
366 	sdfmt = snd_hdac_calc_stream_format(48000, 1, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
367 	ret = snd_hdac_dsp_prepare(hstream, sdfmt, fw->size, &dmab);
368 	if (ret < 0)
369 		goto release_stream;
370 
371 	/* enable SPIB for hda stream */
372 	snd_hdac_ext_stream_spbcap_enable(bus, true, hstream->index);
373 	ret = snd_hdac_ext_stream_set_spib(bus, estream, fw->size);
374 	if (ret)
375 		goto cleanup_resources;
376 
377 	memcpy(dmab.area, fw->data, fw->size);
378 
379 	for (i = 0; i < APL_ROM_INIT_RETRIES; i++) {
380 		unsigned int dma_id = hstream->stream_tag - 1;
381 
382 		ret = avs_hda_init_rom(adev, dma_id, true);
383 		if (!ret)
384 			break;
385 		dev_info(adev->dev, "#%d rom init fail: %d\n", i + 1, ret);
386 	}
387 	if (ret < 0)
388 		goto cleanup_resources;
389 
390 	/* transfer firmware */
391 	snd_hdac_dsp_trigger(hstream, true);
392 	ret = snd_hdac_adsp_readl_poll(adev, AVS_FW_REG_STATUS(adev), reg,
393 				       (reg & AVS_ROM_STS_MASK) == APL_ROM_FW_ENTERED,
394 				       AVS_FW_INIT_POLLING_US, AVS_FW_INIT_TIMEOUT_US);
395 	snd_hdac_dsp_trigger(hstream, false);
396 	if (ret < 0) {
397 		dev_err(adev->dev, "transfer fw failed: %d\n", ret);
398 		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
399 	}
400 
401 cleanup_resources:
402 	/* disable SPIB for hda stream */
403 	snd_hdac_ext_stream_spbcap_enable(bus, false, hstream->index);
404 	snd_hdac_ext_stream_set_spib(bus, estream, 0);
405 
406 	snd_hdac_dsp_cleanup(hstream, &dmab);
407 release_stream:
408 	snd_hdac_ext_stream_release(estream, HDAC_EXT_STREAM_TYPE_HOST);
409 
410 	return ret;
411 }
412 
avs_hda_load_library(struct avs_dev * adev,struct firmware * lib,u32 id)413 int avs_hda_load_library(struct avs_dev *adev, struct firmware *lib, u32 id)
414 {
415 	struct snd_pcm_substream substream;
416 	struct snd_dma_buffer dmab;
417 	struct hdac_ext_stream *estream;
418 	struct hdac_stream *stream;
419 	struct hdac_bus *bus = &adev->base.core;
420 	unsigned int sdfmt;
421 	int ret;
422 
423 	/* configure hda dma */
424 	memset(&substream, 0, sizeof(substream));
425 	substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
426 	estream = snd_hdac_ext_stream_assign(bus, &substream,
427 					     HDAC_EXT_STREAM_TYPE_HOST);
428 	if (!estream)
429 		return -ENODEV;
430 	stream = hdac_stream(estream);
431 
432 	/* code loading performed with default format */
433 	sdfmt = snd_hdac_calc_stream_format(48000, 1, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
434 	ret = snd_hdac_dsp_prepare(stream, sdfmt, lib->size, &dmab);
435 	if (ret < 0)
436 		goto release_stream;
437 
438 	/* enable SPIB for hda stream */
439 	snd_hdac_ext_stream_spbcap_enable(bus, true, stream->index);
440 	snd_hdac_ext_stream_set_spib(bus, estream, lib->size);
441 
442 	memcpy(dmab.area, lib->data, lib->size);
443 
444 	/* transfer firmware */
445 	snd_hdac_dsp_trigger(stream, true);
446 	ret = avs_ipc_load_library(adev, stream->stream_tag - 1, id);
447 	snd_hdac_dsp_trigger(stream, false);
448 	if (ret) {
449 		dev_err(adev->dev, "transfer lib %d failed: %d\n", id, ret);
450 		ret = AVS_IPC_RET(ret);
451 	}
452 
453 	/* disable SPIB for hda stream */
454 	snd_hdac_ext_stream_spbcap_enable(bus, false, stream->index);
455 	snd_hdac_ext_stream_set_spib(bus, estream, 0);
456 
457 	snd_hdac_dsp_cleanup(stream, &dmab);
458 release_stream:
459 	snd_hdac_ext_stream_release(estream, HDAC_EXT_STREAM_TYPE_HOST);
460 
461 	return ret;
462 }
463 
avs_hda_transfer_modules(struct avs_dev * adev,bool load,struct avs_module_entry * mods,u32 num_mods)464 int avs_hda_transfer_modules(struct avs_dev *adev, bool load,
465 			     struct avs_module_entry *mods, u32 num_mods)
466 {
467 	/*
468 	 * All platforms without CLDMA are equipped with IMR,
469 	 * and thus the module transferring is offloaded to DSP.
470 	 */
471 	return 0;
472 }
473 
avs_dsp_load_libraries(struct avs_dev * adev,struct avs_tplg_library * libs,u32 num_libs)474 int avs_dsp_load_libraries(struct avs_dev *adev, struct avs_tplg_library *libs, u32 num_libs)
475 {
476 	int start, id, i = 0;
477 	int ret;
478 
479 	/* Calculate the id to assign for the next lib. */
480 	for (id = 0; id < adev->fw_cfg.max_libs_count; id++)
481 		if (adev->lib_names[id][0] == '\0')
482 			break;
483 	if (id + num_libs >= adev->fw_cfg.max_libs_count)
484 		return -EINVAL;
485 
486 	start = id;
487 	while (i < num_libs) {
488 		struct avs_fw_manifest *man;
489 		const struct firmware *fw;
490 		struct firmware stripped_fw;
491 		char *filename;
492 		int j;
493 
494 		filename = kasprintf(GFP_KERNEL, "%s/%s/%s", AVS_ROOT_DIR, adev->spec->name,
495 				     libs[i].name);
496 		if (!filename)
497 			return -ENOMEM;
498 
499 		/*
500 		 * If any call after this one fails, requested firmware is not released with
501 		 * avs_release_last_firmware() as failing to load code results in need for reload
502 		 * of entire driver module. And then avs_release_firmwares() is in place already.
503 		 */
504 		ret = avs_request_firmware(adev, &fw, filename);
505 		kfree(filename);
506 		if (ret < 0)
507 			return ret;
508 
509 		stripped_fw = *fw;
510 		ret = avs_fw_manifest_strip_verify(adev, &stripped_fw, NULL);
511 		if (ret) {
512 			dev_err(adev->dev, "invalid library data: %d\n", ret);
513 			return ret;
514 		}
515 
516 		ret = avs_fw_manifest_offset(&stripped_fw);
517 		if (ret < 0)
518 			return ret;
519 		man = (struct avs_fw_manifest *)(stripped_fw.data + ret);
520 
521 		/* Don't load anything that's already in DSP memory. */
522 		for (j = 0; j < id; j++)
523 			if (!strncmp(adev->lib_names[j], man->name, AVS_LIB_NAME_SIZE))
524 				goto next_lib;
525 
526 		ret = avs_dsp_op(adev, load_lib, &stripped_fw, id);
527 		if (ret)
528 			return ret;
529 
530 		strncpy(adev->lib_names[id], man->name, AVS_LIB_NAME_SIZE);
531 		id++;
532 next_lib:
533 		i++;
534 	}
535 
536 	return start == id ? 1 : 0;
537 }
538 
avs_dsp_load_basefw(struct avs_dev * adev)539 static int avs_dsp_load_basefw(struct avs_dev *adev)
540 {
541 	const struct avs_fw_version *min_req;
542 	const struct avs_spec *const spec = adev->spec;
543 	const struct firmware *fw;
544 	struct firmware stripped_fw;
545 	char *filename;
546 	int ret;
547 
548 	filename = kasprintf(GFP_KERNEL, "%s/%s/%s", AVS_ROOT_DIR, spec->name, AVS_BASEFW_FILENAME);
549 	if (!filename)
550 		return -ENOMEM;
551 
552 	ret = avs_request_firmware(adev, &fw, filename);
553 	kfree(filename);
554 	if (ret < 0) {
555 		dev_err(adev->dev, "request firmware failed: %d\n", ret);
556 		return ret;
557 	}
558 
559 	stripped_fw = *fw;
560 	min_req = &adev->spec->min_fw_version;
561 
562 	ret = avs_fw_manifest_strip_verify(adev, &stripped_fw, min_req);
563 	if (ret < 0) {
564 		dev_err(adev->dev, "invalid firmware data: %d\n", ret);
565 		goto release_fw;
566 	}
567 
568 	ret = avs_dsp_op(adev, load_basefw, &stripped_fw);
569 	if (ret < 0) {
570 		dev_err(adev->dev, "basefw load failed: %d\n", ret);
571 		goto release_fw;
572 	}
573 
574 	ret = wait_for_completion_timeout(&adev->fw_ready,
575 					  msecs_to_jiffies(AVS_FW_INIT_TIMEOUT_MS));
576 	if (!ret) {
577 		dev_err(adev->dev, "firmware ready timeout\n");
578 		avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
579 		ret = -ETIMEDOUT;
580 		goto release_fw;
581 	}
582 
583 	return 0;
584 
585 release_fw:
586 	avs_release_last_firmware(adev);
587 	return ret;
588 }
589 
avs_dsp_boot_firmware(struct avs_dev * adev,bool purge)590 int avs_dsp_boot_firmware(struct avs_dev *adev, bool purge)
591 {
592 	struct avs_soc_component *acomp;
593 	int ret, i;
594 
595 	/* Forgo full boot if flash from IMR succeeds. */
596 	if (!purge && avs_platattr_test(adev, IMR)) {
597 		ret = avs_imr_load_basefw(adev);
598 		if (!ret)
599 			return 0;
600 
601 		dev_dbg(adev->dev, "firmware flash from imr failed: %d\n", ret);
602 	}
603 
604 	/* Full boot, clear cached data except for basefw (slot 0). */
605 	for (i = 1; i < adev->fw_cfg.max_libs_count; i++)
606 		memset(adev->lib_names[i], 0, AVS_LIB_NAME_SIZE);
607 
608 	avs_hda_clock_gating_enable(adev, false);
609 	avs_hda_l1sen_enable(adev, false);
610 
611 	ret = avs_dsp_load_basefw(adev);
612 	if (ret)
613 		goto reenable_gating;
614 
615 	mutex_lock(&adev->comp_list_mutex);
616 	list_for_each_entry(acomp, &adev->comp_list, node) {
617 		struct avs_tplg *tplg = acomp->tplg;
618 
619 		ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs);
620 		if (ret < 0)
621 			break;
622 	}
623 	mutex_unlock(&adev->comp_list_mutex);
624 
625 reenable_gating:
626 	avs_hda_l1sen_enable(adev, true);
627 	avs_hda_clock_gating_enable(adev, true);
628 
629 	if (ret < 0)
630 		return ret;
631 
632 	/* With all code loaded, refresh module information. */
633 	ret = avs_module_info_init(adev, true);
634 	if (ret) {
635 		dev_err(adev->dev, "init module info failed: %d\n", ret);
636 		return ret;
637 	}
638 
639 	return 0;
640 }
641 
avs_dsp_first_boot_firmware(struct avs_dev * adev)642 int avs_dsp_first_boot_firmware(struct avs_dev *adev)
643 {
644 	int ret, i;
645 
646 	if (avs_platattr_test(adev, CLDMA)) {
647 		ret = hda_cldma_init(&code_loader, &adev->base.core,
648 				     adev->dsp_ba, AVS_CL_DEFAULT_BUFFER_SIZE);
649 		if (ret < 0) {
650 			dev_err(adev->dev, "cldma init failed: %d\n", ret);
651 			return ret;
652 		}
653 	}
654 
655 	ret = avs_dsp_boot_firmware(adev, true);
656 	if (ret < 0) {
657 		dev_err(adev->dev, "firmware boot failed: %d\n", ret);
658 		return ret;
659 	}
660 
661 	ret = avs_ipc_get_hw_config(adev, &adev->hw_cfg);
662 	if (ret) {
663 		dev_err(adev->dev, "get hw cfg failed: %d\n", ret);
664 		return AVS_IPC_RET(ret);
665 	}
666 
667 	ret = avs_ipc_get_fw_config(adev, &adev->fw_cfg);
668 	if (ret) {
669 		dev_err(adev->dev, "get fw cfg failed: %d\n", ret);
670 		return AVS_IPC_RET(ret);
671 	}
672 
673 	adev->core_refs = devm_kcalloc(adev->dev, adev->hw_cfg.dsp_cores,
674 				       sizeof(*adev->core_refs), GFP_KERNEL);
675 	adev->lib_names = devm_kcalloc(adev->dev, adev->fw_cfg.max_libs_count,
676 				       sizeof(*adev->lib_names), GFP_KERNEL);
677 	if (!adev->core_refs || !adev->lib_names)
678 		return -ENOMEM;
679 
680 	for (i = 0; i < adev->fw_cfg.max_libs_count; i++) {
681 		adev->lib_names[i] = devm_kzalloc(adev->dev, AVS_LIB_NAME_SIZE, GFP_KERNEL);
682 		if (!adev->lib_names[i])
683 			return -ENOMEM;
684 	}
685 
686 	/* basefw always occupies slot 0 */
687 	strcpy(&adev->lib_names[0][0], "BASEFW");
688 
689 	ida_init(&adev->ppl_ida);
690 
691 	return 0;
692 }
693