147d7195dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26eee8726SRamesh Babu /*
36eee8726SRamesh Babu  *  skl-sst-utils.c - SKL sst utils functions
46eee8726SRamesh Babu  *
56eee8726SRamesh Babu  *  Copyright (C) 2016 Intel Corp
66eee8726SRamesh Babu  */
76eee8726SRamesh Babu 
8ea6b3e94SShreyas NC #include <linux/device.h>
9ea6b3e94SShreyas NC #include <linux/slab.h>
10ea6b3e94SShreyas NC #include <linux/uuid.h>
11ea6b3e94SShreyas NC #include "../common/sst-dsp.h"
12ea6b3e94SShreyas NC #include "../common/sst-dsp-priv.h"
13bcc2a2dcSCezary Rojewski #include "skl.h"
14ea6b3e94SShreyas NC 
15ea6b3e94SShreyas NC #define DEFAULT_HASH_SHA256_LEN 32
166eee8726SRamesh Babu 
176eee8726SRamesh Babu /* FW Extended Manifest Header id = $AE1 */
186eee8726SRamesh Babu #define SKL_EXT_MANIFEST_HEADER_MAGIC   0x31454124
196eee8726SRamesh Babu 
20ea6b3e94SShreyas NC union seg_flags {
21ea6b3e94SShreyas NC 	u32 ul;
22ea6b3e94SShreyas NC 	struct {
23ea6b3e94SShreyas NC 		u32 contents : 1;
24ea6b3e94SShreyas NC 		u32 alloc    : 1;
25ea6b3e94SShreyas NC 		u32 load     : 1;
26ea6b3e94SShreyas NC 		u32 read_only : 1;
27ea6b3e94SShreyas NC 		u32 code     : 1;
28ea6b3e94SShreyas NC 		u32 data     : 1;
29ea6b3e94SShreyas NC 		u32 _rsvd0   : 2;
30ea6b3e94SShreyas NC 		u32 type     : 4;
31ea6b3e94SShreyas NC 		u32 _rsvd1   : 4;
32ea6b3e94SShreyas NC 		u32 length   : 16;
33ea6b3e94SShreyas NC 	} r;
34ea6b3e94SShreyas NC } __packed;
35ea6b3e94SShreyas NC 
36ea6b3e94SShreyas NC struct segment_desc {
37ea6b3e94SShreyas NC 	union seg_flags flags;
38ea6b3e94SShreyas NC 	u32 v_base_addr;
39ea6b3e94SShreyas NC 	u32 file_offset;
40ea6b3e94SShreyas NC };
41ea6b3e94SShreyas NC 
42ea6b3e94SShreyas NC struct module_type {
43ea6b3e94SShreyas NC 	u32 load_type  : 4;
44ea6b3e94SShreyas NC 	u32 auto_start : 1;
45ea6b3e94SShreyas NC 	u32 domain_ll  : 1;
46ea6b3e94SShreyas NC 	u32 domain_dp  : 1;
47ea6b3e94SShreyas NC 	u32 rsvd       : 25;
48ea6b3e94SShreyas NC } __packed;
49ea6b3e94SShreyas NC 
50ea6b3e94SShreyas NC struct adsp_module_entry {
51ea6b3e94SShreyas NC 	u32 struct_id;
52ea6b3e94SShreyas NC 	u8  name[8];
539e0784d0SAndy Shevchenko 	u8  uuid[16];
54ea6b3e94SShreyas NC 	struct module_type type;
55ea6b3e94SShreyas NC 	u8  hash1[DEFAULT_HASH_SHA256_LEN];
56ea6b3e94SShreyas NC 	u32 entry_point;
57ea6b3e94SShreyas NC 	u16 cfg_offset;
58ea6b3e94SShreyas NC 	u16 cfg_count;
59ea6b3e94SShreyas NC 	u32 affinity_mask;
60ea6b3e94SShreyas NC 	u16 instance_max_count;
61ea6b3e94SShreyas NC 	u16 instance_bss_size;
62ea6b3e94SShreyas NC 	struct segment_desc segments[3];
63ea6b3e94SShreyas NC } __packed;
64ea6b3e94SShreyas NC 
65ea6b3e94SShreyas NC struct adsp_fw_hdr {
66ea6b3e94SShreyas NC 	u32 id;
67ea6b3e94SShreyas NC 	u32 len;
68ea6b3e94SShreyas NC 	u8  name[8];
69ea6b3e94SShreyas NC 	u32 preload_page_count;
70ea6b3e94SShreyas NC 	u32 fw_image_flags;
71ea6b3e94SShreyas NC 	u32 feature_mask;
72ea6b3e94SShreyas NC 	u16 major;
73ea6b3e94SShreyas NC 	u16 minor;
74ea6b3e94SShreyas NC 	u16 hotfix;
75ea6b3e94SShreyas NC 	u16 build;
76ea6b3e94SShreyas NC 	u32 num_modules;
77ea6b3e94SShreyas NC 	u32 hw_buf_base;
78ea6b3e94SShreyas NC 	u32 hw_buf_length;
79ea6b3e94SShreyas NC 	u32 load_offset;
80ea6b3e94SShreyas NC } __packed;
81ea6b3e94SShreyas NC 
826eee8726SRamesh Babu struct skl_ext_manifest_hdr {
836eee8726SRamesh Babu 	u32 id;
846eee8726SRamesh Babu 	u32 len;
856eee8726SRamesh Babu 	u16 version_major;
866eee8726SRamesh Babu 	u16 version_minor;
876eee8726SRamesh Babu 	u32 entries;
886eee8726SRamesh Babu };
896eee8726SRamesh Babu 
skl_get_pvtid_map(struct uuid_module * module,int instance_id)9055a92ea9SDharageswari R static int skl_get_pvtid_map(struct uuid_module *module, int instance_id)
9155a92ea9SDharageswari R {
9255a92ea9SDharageswari R 	int pvt_id;
9355a92ea9SDharageswari R 
9455a92ea9SDharageswari R 	for (pvt_id = 0; pvt_id < module->max_instance; pvt_id++) {
9555a92ea9SDharageswari R 		if (module->instance_id[pvt_id] == instance_id)
9655a92ea9SDharageswari R 			return pvt_id;
9755a92ea9SDharageswari R 	}
9855a92ea9SDharageswari R 	return -EINVAL;
9955a92ea9SDharageswari R }
10055a92ea9SDharageswari R 
skl_get_pvt_instance_id_map(struct skl_dev * skl,int module_id,int instance_id)101bcc2a2dcSCezary Rojewski int skl_get_pvt_instance_id_map(struct skl_dev *skl,
10255a92ea9SDharageswari R 				int module_id, int instance_id)
10355a92ea9SDharageswari R {
10455a92ea9SDharageswari R 	struct uuid_module *module;
10555a92ea9SDharageswari R 
106bcc2a2dcSCezary Rojewski 	list_for_each_entry(module, &skl->uuid_list, list) {
10755a92ea9SDharageswari R 		if (module->id == module_id)
10855a92ea9SDharageswari R 			return skl_get_pvtid_map(module, instance_id);
10955a92ea9SDharageswari R 	}
11055a92ea9SDharageswari R 
11155a92ea9SDharageswari R 	return -EINVAL;
11255a92ea9SDharageswari R }
11355a92ea9SDharageswari R EXPORT_SYMBOL_GPL(skl_get_pvt_instance_id_map);
11455a92ea9SDharageswari R 
skl_getid_32(struct uuid_module * module,u64 * val,int word1_mask,int word2_mask)115700a9a63SDharageswari R static inline int skl_getid_32(struct uuid_module *module, u64 *val,
116700a9a63SDharageswari R 				int word1_mask, int word2_mask)
117700a9a63SDharageswari R {
118700a9a63SDharageswari R 	int index, max_inst, pvt_id;
119700a9a63SDharageswari R 	u32 mask_val;
120700a9a63SDharageswari R 
121700a9a63SDharageswari R 	max_inst =  module->max_instance;
122700a9a63SDharageswari R 	mask_val = (u32)(*val >> word1_mask);
123700a9a63SDharageswari R 
124700a9a63SDharageswari R 	if (mask_val != 0xffffffff) {
125700a9a63SDharageswari R 		index = ffz(mask_val);
126700a9a63SDharageswari R 		pvt_id = index + word1_mask + word2_mask;
127700a9a63SDharageswari R 		if (pvt_id <= (max_inst - 1)) {
128c8eabf82SDan Carpenter 			*val |= 1ULL << (index + word1_mask);
129700a9a63SDharageswari R 			return pvt_id;
130700a9a63SDharageswari R 		}
131700a9a63SDharageswari R 	}
132700a9a63SDharageswari R 
133700a9a63SDharageswari R 	return -EINVAL;
134700a9a63SDharageswari R }
135700a9a63SDharageswari R 
skl_pvtid_128(struct uuid_module * module)136700a9a63SDharageswari R static inline int skl_pvtid_128(struct uuid_module *module)
137700a9a63SDharageswari R {
138700a9a63SDharageswari R 	int j, i, word1_mask, word2_mask = 0, pvt_id;
139700a9a63SDharageswari R 
140700a9a63SDharageswari R 	for (j = 0; j < MAX_INSTANCE_BUFF; j++) {
141700a9a63SDharageswari R 		word1_mask = 0;
142700a9a63SDharageswari R 
143700a9a63SDharageswari R 		for (i = 0; i < 2; i++) {
144700a9a63SDharageswari R 			pvt_id = skl_getid_32(module, &module->pvt_id[j],
145700a9a63SDharageswari R 						word1_mask, word2_mask);
146700a9a63SDharageswari R 			if (pvt_id >= 0)
147700a9a63SDharageswari R 				return pvt_id;
148700a9a63SDharageswari R 
149700a9a63SDharageswari R 			word1_mask += 32;
150700a9a63SDharageswari R 			if ((word1_mask + word2_mask) >= module->max_instance)
151700a9a63SDharageswari R 				return -EINVAL;
152700a9a63SDharageswari R 		}
153700a9a63SDharageswari R 
154700a9a63SDharageswari R 		word2_mask += 64;
155700a9a63SDharageswari R 		if (word2_mask >= module->max_instance)
156700a9a63SDharageswari R 			return -EINVAL;
157700a9a63SDharageswari R 	}
158700a9a63SDharageswari R 
159700a9a63SDharageswari R 	return -EINVAL;
160700a9a63SDharageswari R }
161700a9a63SDharageswari R 
162700a9a63SDharageswari R /**
163700a9a63SDharageswari R  * skl_get_pvt_id: generate a private id for use as module id
164700a9a63SDharageswari R  *
165bcc2a2dcSCezary Rojewski  * @skl: driver context
166446c4724SGuneshwor Singh  * @uuid_mod: module's uuid
167446c4724SGuneshwor Singh  * @instance_id: module's instance id
168700a9a63SDharageswari R  *
169700a9a63SDharageswari R  * This generates a 128 bit private unique id for a module TYPE so that
170700a9a63SDharageswari R  * module instance is unique
171700a9a63SDharageswari R  */
skl_get_pvt_id(struct skl_dev * skl,guid_t * uuid_mod,int instance_id)172bcc2a2dcSCezary Rojewski int skl_get_pvt_id(struct skl_dev *skl, guid_t *uuid_mod, int instance_id)
173700a9a63SDharageswari R {
174700a9a63SDharageswari R 	struct uuid_module *module;
175700a9a63SDharageswari R 	int pvt_id;
176700a9a63SDharageswari R 
177bcc2a2dcSCezary Rojewski 	list_for_each_entry(module, &skl->uuid_list, list) {
1789e0784d0SAndy Shevchenko 		if (guid_equal(uuid_mod, &module->uuid)) {
179700a9a63SDharageswari R 
180700a9a63SDharageswari R 			pvt_id = skl_pvtid_128(module);
18155a92ea9SDharageswari R 			if (pvt_id >= 0) {
182b26199eaSJeeja KP 				module->instance_id[pvt_id] = instance_id;
183b26199eaSJeeja KP 
184700a9a63SDharageswari R 				return pvt_id;
185700a9a63SDharageswari R 			}
186700a9a63SDharageswari R 		}
18755a92ea9SDharageswari R 	}
188700a9a63SDharageswari R 
189700a9a63SDharageswari R 	return -EINVAL;
190700a9a63SDharageswari R }
191700a9a63SDharageswari R EXPORT_SYMBOL_GPL(skl_get_pvt_id);
192700a9a63SDharageswari R 
193700a9a63SDharageswari R /**
194700a9a63SDharageswari R  * skl_put_pvt_id: free up the private id allocated
195700a9a63SDharageswari R  *
196bcc2a2dcSCezary Rojewski  * @skl: driver context
197446c4724SGuneshwor Singh  * @uuid_mod: module's uuid
198446c4724SGuneshwor Singh  * @pvt_id: module pvt id
199700a9a63SDharageswari R  *
200700a9a63SDharageswari R  * This frees a 128 bit private unique id previously generated
201700a9a63SDharageswari R  */
skl_put_pvt_id(struct skl_dev * skl,guid_t * uuid_mod,int * pvt_id)202bcc2a2dcSCezary Rojewski int skl_put_pvt_id(struct skl_dev *skl, guid_t *uuid_mod, int *pvt_id)
203700a9a63SDharageswari R {
204700a9a63SDharageswari R 	int i;
205700a9a63SDharageswari R 	struct uuid_module *module;
206700a9a63SDharageswari R 
207bcc2a2dcSCezary Rojewski 	list_for_each_entry(module, &skl->uuid_list, list) {
2089e0784d0SAndy Shevchenko 		if (guid_equal(uuid_mod, &module->uuid)) {
209700a9a63SDharageswari R 
210b26199eaSJeeja KP 			if (*pvt_id != 0)
211b26199eaSJeeja KP 				i = (*pvt_id) / 64;
212700a9a63SDharageswari R 			else
213700a9a63SDharageswari R 				i = 0;
214700a9a63SDharageswari R 
215b26199eaSJeeja KP 			module->pvt_id[i] &= ~(1 << (*pvt_id));
216b26199eaSJeeja KP 			*pvt_id = -1;
217700a9a63SDharageswari R 			return 0;
218700a9a63SDharageswari R 		}
219700a9a63SDharageswari R 	}
220700a9a63SDharageswari R 
221700a9a63SDharageswari R 	return -EINVAL;
222700a9a63SDharageswari R }
223700a9a63SDharageswari R EXPORT_SYMBOL_GPL(skl_put_pvt_id);
224700a9a63SDharageswari R 
225ea6b3e94SShreyas NC /*
226ea6b3e94SShreyas NC  * Parse the firmware binary to get the UUID, module id
227ea6b3e94SShreyas NC  * and loadable flags
228ea6b3e94SShreyas NC  */
snd_skl_parse_uuids(struct sst_dsp * ctx,const struct firmware * fw,unsigned int offset,int index)229a8e2c19eSSenthilnathan Veppur int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw,
230a8e2c19eSSenthilnathan Veppur 			unsigned int offset, int index)
231ea6b3e94SShreyas NC {
232ea6b3e94SShreyas NC 	struct adsp_fw_hdr *adsp_hdr;
233ea6b3e94SShreyas NC 	struct adsp_module_entry *mod_entry;
23455a92ea9SDharageswari R 	int i, num_entry, size;
235ea6b3e94SShreyas NC 	const char *buf;
236bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = ctx->thread_context;
237ea6b3e94SShreyas NC 	struct uuid_module *module;
238ea6b3e94SShreyas NC 	struct firmware stripped_fw;
239ea6b3e94SShreyas NC 	unsigned int safe_file;
240c6193988SPierre-Louis Bossart 	int ret;
241ea6b3e94SShreyas NC 
242ea6b3e94SShreyas NC 	/* Get the FW pointer to derive ADSP header */
243a8e2c19eSSenthilnathan Veppur 	stripped_fw.data = fw->data;
244a8e2c19eSSenthilnathan Veppur 	stripped_fw.size = fw->size;
245ea6b3e94SShreyas NC 
246ea6b3e94SShreyas NC 	skl_dsp_strip_extended_manifest(&stripped_fw);
247ea6b3e94SShreyas NC 
248ea6b3e94SShreyas NC 	buf = stripped_fw.data;
249ea6b3e94SShreyas NC 
250ea6b3e94SShreyas NC 	/* check if we have enough space in file to move to header */
251ea6b3e94SShreyas NC 	safe_file = sizeof(*adsp_hdr) + offset;
252ea6b3e94SShreyas NC 	if (stripped_fw.size <= safe_file) {
253ea6b3e94SShreyas NC 		dev_err(ctx->dev, "Small fw file size, No space for hdr\n");
254ea6b3e94SShreyas NC 		return -EINVAL;
255ea6b3e94SShreyas NC 	}
256ea6b3e94SShreyas NC 
257ea6b3e94SShreyas NC 	adsp_hdr = (struct adsp_fw_hdr *)(buf + offset);
258ea6b3e94SShreyas NC 
259ea6b3e94SShreyas NC 	/* check 1st module entry is in file */
260ea6b3e94SShreyas NC 	safe_file += adsp_hdr->len + sizeof(*mod_entry);
261ea6b3e94SShreyas NC 	if (stripped_fw.size <= safe_file) {
262ea6b3e94SShreyas NC 		dev_err(ctx->dev, "Small fw file size, No module entry\n");
263ea6b3e94SShreyas NC 		return -EINVAL;
264ea6b3e94SShreyas NC 	}
265ea6b3e94SShreyas NC 
2669e0784d0SAndy Shevchenko 	mod_entry = (struct adsp_module_entry *)(buf + offset + adsp_hdr->len);
267ea6b3e94SShreyas NC 
268ea6b3e94SShreyas NC 	num_entry = adsp_hdr->num_modules;
269ea6b3e94SShreyas NC 
270ea6b3e94SShreyas NC 	/* check all entries are in file */
271ea6b3e94SShreyas NC 	safe_file += num_entry * sizeof(*mod_entry);
272ea6b3e94SShreyas NC 	if (stripped_fw.size <= safe_file) {
273ea6b3e94SShreyas NC 		dev_err(ctx->dev, "Small fw file size, No modules\n");
274ea6b3e94SShreyas NC 		return -EINVAL;
275ea6b3e94SShreyas NC 	}
276ea6b3e94SShreyas NC 
277ea6b3e94SShreyas NC 
278ea6b3e94SShreyas NC 	/*
279ea6b3e94SShreyas NC 	 * Read the UUID(GUID) from FW Manifest.
280ea6b3e94SShreyas NC 	 *
281ea6b3e94SShreyas NC 	 * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
282ea6b3e94SShreyas NC 	 * Populate the UUID table to store module_id and loadable flags
283ea6b3e94SShreyas NC 	 * for the module.
284ea6b3e94SShreyas NC 	 */
285ea6b3e94SShreyas NC 
286ea6b3e94SShreyas NC 	for (i = 0; i < num_entry; i++, mod_entry++) {
287ea6b3e94SShreyas NC 		module = kzalloc(sizeof(*module), GFP_KERNEL);
288f8e06652SPankaj Bharadiya 		if (!module) {
289f8e06652SPankaj Bharadiya 			ret = -ENOMEM;
290f8e06652SPankaj Bharadiya 			goto free_uuid_list;
291f8e06652SPankaj Bharadiya 		}
292ea6b3e94SShreyas NC 
293cade2f59SAndy Shevchenko 		import_guid(&module->uuid, mod_entry->uuid);
294ea6b3e94SShreyas NC 
295a8e2c19eSSenthilnathan Veppur 		module->id = (i | (index << 12));
296ea6b3e94SShreyas NC 		module->is_loadable = mod_entry->type.load_type;
297700a9a63SDharageswari R 		module->max_instance = mod_entry->instance_max_count;
29855a92ea9SDharageswari R 		size = sizeof(int) * mod_entry->instance_max_count;
29955a92ea9SDharageswari R 		module->instance_id = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
3000730bd2eSColin Ian King 		if (!module->instance_id) {
301f8e06652SPankaj Bharadiya 			ret = -ENOMEM;
302*3c6d52daSCezary Rojewski 			kfree(module);
303f8e06652SPankaj Bharadiya 			goto free_uuid_list;
3040730bd2eSColin Ian King 		}
305ea6b3e94SShreyas NC 
306ea6b3e94SShreyas NC 		list_add_tail(&module->list, &skl->uuid_list);
307ea6b3e94SShreyas NC 
308ea6b3e94SShreyas NC 		dev_dbg(ctx->dev,
309ea6b3e94SShreyas NC 			"Adding uuid :%pUL   mod id: %d  Loadable: %d\n",
310ea6b3e94SShreyas NC 			&module->uuid, module->id, module->is_loadable);
311ea6b3e94SShreyas NC 	}
312ea6b3e94SShreyas NC 
313ea6b3e94SShreyas NC 	return 0;
314f8e06652SPankaj Bharadiya 
315f8e06652SPankaj Bharadiya free_uuid_list:
316f8e06652SPankaj Bharadiya 	skl_freeup_uuid_list(skl);
317f8e06652SPankaj Bharadiya 	return ret;
318ea6b3e94SShreyas NC }
319ea6b3e94SShreyas NC 
skl_freeup_uuid_list(struct skl_dev * skl)320bcc2a2dcSCezary Rojewski void skl_freeup_uuid_list(struct skl_dev *skl)
321ea6b3e94SShreyas NC {
322ea6b3e94SShreyas NC 	struct uuid_module *uuid, *_uuid;
323ea6b3e94SShreyas NC 
324bcc2a2dcSCezary Rojewski 	list_for_each_entry_safe(uuid, _uuid, &skl->uuid_list, list) {
325ea6b3e94SShreyas NC 		list_del(&uuid->list);
326ea6b3e94SShreyas NC 		kfree(uuid);
327ea6b3e94SShreyas NC 	}
328ea6b3e94SShreyas NC }
329ea6b3e94SShreyas NC 
3306eee8726SRamesh Babu /*
3316eee8726SRamesh Babu  * some firmware binary contains some extended manifest. This needs
3326eee8726SRamesh Babu  * to be stripped in that case before we load and use that image.
3336eee8726SRamesh Babu  *
334ea6b3e94SShreyas NC  * Get the module id for the module by checking
335ea6b3e94SShreyas NC  * the table for the UUID for the module
3366eee8726SRamesh Babu  */
skl_dsp_strip_extended_manifest(struct firmware * fw)3376eee8726SRamesh Babu int skl_dsp_strip_extended_manifest(struct firmware *fw)
3386eee8726SRamesh Babu {
3396eee8726SRamesh Babu 	struct skl_ext_manifest_hdr *hdr;
3406eee8726SRamesh Babu 
3416eee8726SRamesh Babu 	/* check if fw file is greater than header we are looking */
3426eee8726SRamesh Babu 	if (fw->size < sizeof(hdr)) {
3436eee8726SRamesh Babu 		pr_err("%s: Firmware file small, no hdr\n", __func__);
3446eee8726SRamesh Babu 		return -EINVAL;
3456eee8726SRamesh Babu 	}
3466eee8726SRamesh Babu 
3476eee8726SRamesh Babu 	hdr = (struct skl_ext_manifest_hdr *)fw->data;
3486eee8726SRamesh Babu 
3496eee8726SRamesh Babu 	if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) {
3506eee8726SRamesh Babu 		fw->size -= hdr->len;
3516eee8726SRamesh Babu 		fw->data += hdr->len;
3526eee8726SRamesh Babu 	}
3536eee8726SRamesh Babu 
3546eee8726SRamesh Babu 	return 0;
3556eee8726SRamesh Babu }
3569fe9c711SG Kranthi 
skl_sst_ctx_init(struct device * dev,int irq,const char * fw_name,struct skl_dsp_loader_ops dsp_ops,struct skl_dev ** dsp,struct sst_dsp_device * skl_dev)3579fe9c711SG Kranthi int skl_sst_ctx_init(struct device *dev, int irq, const char *fw_name,
358bcc2a2dcSCezary Rojewski 	struct skl_dsp_loader_ops dsp_ops, struct skl_dev **dsp,
3599fe9c711SG Kranthi 	struct sst_dsp_device *skl_dev)
3609fe9c711SG Kranthi {
361bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = *dsp;
3629fe9c711SG Kranthi 	struct sst_dsp *sst;
3639fe9c711SG Kranthi 
3649fe9c711SG Kranthi 	skl->dev = dev;
3659fe9c711SG Kranthi 	skl_dev->thread_context = skl;
3669fe9c711SG Kranthi 	INIT_LIST_HEAD(&skl->uuid_list);
3679fe9c711SG Kranthi 	skl->dsp = skl_dsp_ctx_init(dev, skl_dev, irq);
3689fe9c711SG Kranthi 	if (!skl->dsp) {
3699fe9c711SG Kranthi 		dev_err(skl->dev, "%s: no device\n", __func__);
3709fe9c711SG Kranthi 		return -ENODEV;
3719fe9c711SG Kranthi 	}
3729fe9c711SG Kranthi 
3739fe9c711SG Kranthi 	sst = skl->dsp;
3749fe9c711SG Kranthi 	sst->fw_name = fw_name;
3759fe9c711SG Kranthi 	sst->dsp_ops = dsp_ops;
3769fe9c711SG Kranthi 	init_waitqueue_head(&skl->mod_load_wait);
3779fe9c711SG Kranthi 	INIT_LIST_HEAD(&sst->module_list);
3789fe9c711SG Kranthi 
3799fe9c711SG Kranthi 	skl->is_first_boot = true;
3809fe9c711SG Kranthi 
3812eed1b02SGuneshwor Singh 	return 0;
3829fe9c711SG Kranthi }
383ebe89076SSubhransu S. Prusty 
skl_prepare_lib_load(struct skl_dev * skl,struct skl_lib_info * linfo,struct firmware * stripped_fw,unsigned int hdr_offset,int index)384bcc2a2dcSCezary Rojewski int skl_prepare_lib_load(struct skl_dev *skl, struct skl_lib_info *linfo,
385ebe89076SSubhransu S. Prusty 		struct firmware *stripped_fw,
386ebe89076SSubhransu S. Prusty 		unsigned int hdr_offset, int index)
387ebe89076SSubhransu S. Prusty {
388ebe89076SSubhransu S. Prusty 	int ret;
389ebe89076SSubhransu S. Prusty 	struct sst_dsp *dsp = skl->dsp;
390ebe89076SSubhransu S. Prusty 
391ebe89076SSubhransu S. Prusty 	if (linfo->fw == NULL) {
392ebe89076SSubhransu S. Prusty 		ret = request_firmware(&linfo->fw, linfo->name,
393ebe89076SSubhransu S. Prusty 					skl->dev);
394ebe89076SSubhransu S. Prusty 		if (ret < 0) {
395ebe89076SSubhransu S. Prusty 			dev_err(skl->dev, "Request lib %s failed:%d\n",
396ebe89076SSubhransu S. Prusty 				linfo->name, ret);
397ebe89076SSubhransu S. Prusty 			return ret;
398ebe89076SSubhransu S. Prusty 		}
399ebe89076SSubhransu S. Prusty 	}
400ebe89076SSubhransu S. Prusty 
401ebe89076SSubhransu S. Prusty 	if (skl->is_first_boot) {
402ebe89076SSubhransu S. Prusty 		ret = snd_skl_parse_uuids(dsp, linfo->fw, hdr_offset, index);
403ebe89076SSubhransu S. Prusty 		if (ret < 0)
404ebe89076SSubhransu S. Prusty 			return ret;
405ebe89076SSubhransu S. Prusty 	}
406ebe89076SSubhransu S. Prusty 
407ebe89076SSubhransu S. Prusty 	stripped_fw->data = linfo->fw->data;
408ebe89076SSubhransu S. Prusty 	stripped_fw->size = linfo->fw->size;
409ebe89076SSubhransu S. Prusty 	skl_dsp_strip_extended_manifest(stripped_fw);
410ebe89076SSubhransu S. Prusty 
411ebe89076SSubhransu S. Prusty 	return 0;
412ebe89076SSubhransu S. Prusty }
413ebe89076SSubhransu S. Prusty 
skl_release_library(struct skl_lib_info * linfo,int lib_count)414ebe89076SSubhransu S. Prusty void skl_release_library(struct skl_lib_info *linfo, int lib_count)
415ebe89076SSubhransu S. Prusty {
416ebe89076SSubhransu S. Prusty 	int i;
417ebe89076SSubhransu S. Prusty 
418ebe89076SSubhransu S. Prusty 	/* library indices start from 1 to N. 0 represents base FW */
419ebe89076SSubhransu S. Prusty 	for (i = 1; i < lib_count; i++) {
420ebe89076SSubhransu S. Prusty 		if (linfo[i].fw) {
421ebe89076SSubhransu S. Prusty 			release_firmware(linfo[i].fw);
422ebe89076SSubhransu S. Prusty 			linfo[i].fw = NULL;
423ebe89076SSubhransu S. Prusty 		}
424ebe89076SSubhransu S. Prusty 	}
425ebe89076SSubhransu S. Prusty }
426