xref: /OK3568_Linux_fs/kernel/sound/soc/sof/loader.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // This file is provided under a dual BSD/GPLv2 license.  When using or
4*4882a593Smuzhiyun // redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun //
6*4882a593Smuzhiyun // Copyright(c) 2018 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun //
8*4882a593Smuzhiyun // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9*4882a593Smuzhiyun //
10*4882a593Smuzhiyun // Generic firmware loader.
11*4882a593Smuzhiyun //
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/firmware.h>
14*4882a593Smuzhiyun #include <sound/sof.h>
15*4882a593Smuzhiyun #include <sound/sof/ext_manifest.h>
16*4882a593Smuzhiyun #include "ops.h"
17*4882a593Smuzhiyun 
get_ext_windows(struct snd_sof_dev * sdev,const struct sof_ipc_ext_data_hdr * ext_hdr)18*4882a593Smuzhiyun static int get_ext_windows(struct snd_sof_dev *sdev,
19*4882a593Smuzhiyun 			   const struct sof_ipc_ext_data_hdr *ext_hdr)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	const struct sof_ipc_window *w =
22*4882a593Smuzhiyun 		container_of(ext_hdr, struct sof_ipc_window, ext_hdr);
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	if (w->num_windows == 0 || w->num_windows > SOF_IPC_MAX_ELEMS)
25*4882a593Smuzhiyun 		return -EINVAL;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	if (sdev->info_window) {
28*4882a593Smuzhiyun 		if (memcmp(sdev->info_window, w, ext_hdr->hdr.size)) {
29*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: mismatch between window descriptor from extended manifest and mailbox");
30*4882a593Smuzhiyun 			return -EINVAL;
31*4882a593Smuzhiyun 		}
32*4882a593Smuzhiyun 		return 0;
33*4882a593Smuzhiyun 	}
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	/* keep a local copy of the data */
36*4882a593Smuzhiyun 	sdev->info_window = devm_kmemdup(sdev->dev, w, ext_hdr->hdr.size,
37*4882a593Smuzhiyun 					 GFP_KERNEL);
38*4882a593Smuzhiyun 	if (!sdev->info_window)
39*4882a593Smuzhiyun 		return -ENOMEM;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	return 0;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
get_cc_info(struct snd_sof_dev * sdev,const struct sof_ipc_ext_data_hdr * ext_hdr)44*4882a593Smuzhiyun static int get_cc_info(struct snd_sof_dev *sdev,
45*4882a593Smuzhiyun 		       const struct sof_ipc_ext_data_hdr *ext_hdr)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	int ret;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	const struct sof_ipc_cc_version *cc =
50*4882a593Smuzhiyun 		container_of(ext_hdr, struct sof_ipc_cc_version, ext_hdr);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	if (sdev->cc_version) {
53*4882a593Smuzhiyun 		if (memcmp(sdev->cc_version, cc, cc->ext_hdr.hdr.size)) {
54*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: receive diverged cc_version descriptions");
55*4882a593Smuzhiyun 			return -EINVAL;
56*4882a593Smuzhiyun 		}
57*4882a593Smuzhiyun 		return 0;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	dev_dbg(sdev->dev, "Firmware info: used compiler %s %d:%d:%d%s used optimization flags %s\n",
61*4882a593Smuzhiyun 		cc->name, cc->major, cc->minor, cc->micro, cc->desc,
62*4882a593Smuzhiyun 		cc->optim);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	/* create read-only cc_version debugfs to store compiler version info */
65*4882a593Smuzhiyun 	/* use local copy of the cc_version to prevent data corruption */
66*4882a593Smuzhiyun 	if (sdev->first_boot) {
67*4882a593Smuzhiyun 		sdev->cc_version = devm_kmalloc(sdev->dev, cc->ext_hdr.hdr.size,
68*4882a593Smuzhiyun 						GFP_KERNEL);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 		if (!sdev->cc_version)
71*4882a593Smuzhiyun 			return -ENOMEM;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 		memcpy(sdev->cc_version, cc, cc->ext_hdr.hdr.size);
74*4882a593Smuzhiyun 		ret = snd_sof_debugfs_buf_item(sdev, sdev->cc_version,
75*4882a593Smuzhiyun 					       cc->ext_hdr.hdr.size,
76*4882a593Smuzhiyun 					       "cc_version", 0444);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 		/* errors are only due to memory allocation, not debugfs */
79*4882a593Smuzhiyun 		if (ret < 0) {
80*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: snd_sof_debugfs_buf_item failed\n");
81*4882a593Smuzhiyun 			return ret;
82*4882a593Smuzhiyun 		}
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	return 0;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /* parse the extended FW boot data structures from FW boot message */
snd_sof_fw_parse_ext_data(struct snd_sof_dev * sdev,u32 bar,u32 offset)89*4882a593Smuzhiyun int snd_sof_fw_parse_ext_data(struct snd_sof_dev *sdev, u32 bar, u32 offset)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	struct sof_ipc_ext_data_hdr *ext_hdr;
92*4882a593Smuzhiyun 	void *ext_data;
93*4882a593Smuzhiyun 	int ret = 0;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	ext_data = kzalloc(PAGE_SIZE, GFP_KERNEL);
96*4882a593Smuzhiyun 	if (!ext_data)
97*4882a593Smuzhiyun 		return -ENOMEM;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* get first header */
100*4882a593Smuzhiyun 	snd_sof_dsp_block_read(sdev, bar, offset, ext_data,
101*4882a593Smuzhiyun 			       sizeof(*ext_hdr));
102*4882a593Smuzhiyun 	ext_hdr = ext_data;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	while (ext_hdr->hdr.cmd == SOF_IPC_FW_READY) {
105*4882a593Smuzhiyun 		/* read in ext structure */
106*4882a593Smuzhiyun 		snd_sof_dsp_block_read(sdev, bar, offset + sizeof(*ext_hdr),
107*4882a593Smuzhiyun 				   (void *)((u8 *)ext_data + sizeof(*ext_hdr)),
108*4882a593Smuzhiyun 				   ext_hdr->hdr.size - sizeof(*ext_hdr));
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 		dev_dbg(sdev->dev, "found ext header type %d size 0x%x\n",
111*4882a593Smuzhiyun 			ext_hdr->type, ext_hdr->hdr.size);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 		/* process structure data */
114*4882a593Smuzhiyun 		switch (ext_hdr->type) {
115*4882a593Smuzhiyun 		case SOF_IPC_EXT_WINDOW:
116*4882a593Smuzhiyun 			ret = get_ext_windows(sdev, ext_hdr);
117*4882a593Smuzhiyun 			break;
118*4882a593Smuzhiyun 		case SOF_IPC_EXT_CC_INFO:
119*4882a593Smuzhiyun 			ret = get_cc_info(sdev, ext_hdr);
120*4882a593Smuzhiyun 			break;
121*4882a593Smuzhiyun 		case SOF_IPC_EXT_UNUSED:
122*4882a593Smuzhiyun 		case SOF_IPC_EXT_PROBE_INFO:
123*4882a593Smuzhiyun 		case SOF_IPC_EXT_USER_ABI_INFO:
124*4882a593Smuzhiyun 			/* They are supported but we don't do anything here */
125*4882a593Smuzhiyun 			break;
126*4882a593Smuzhiyun 		default:
127*4882a593Smuzhiyun 			dev_warn(sdev->dev, "warning: unknown ext header type %d size 0x%x\n",
128*4882a593Smuzhiyun 				 ext_hdr->type, ext_hdr->hdr.size);
129*4882a593Smuzhiyun 			ret = 0;
130*4882a593Smuzhiyun 			break;
131*4882a593Smuzhiyun 		}
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 		if (ret < 0) {
134*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: failed to parse ext data type %d\n",
135*4882a593Smuzhiyun 				ext_hdr->type);
136*4882a593Smuzhiyun 			break;
137*4882a593Smuzhiyun 		}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 		/* move to next header */
140*4882a593Smuzhiyun 		offset += ext_hdr->hdr.size;
141*4882a593Smuzhiyun 		snd_sof_dsp_block_read(sdev, bar, offset, ext_data,
142*4882a593Smuzhiyun 				       sizeof(*ext_hdr));
143*4882a593Smuzhiyun 		ext_hdr = ext_data;
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	kfree(ext_data);
147*4882a593Smuzhiyun 	return ret;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_fw_parse_ext_data);
150*4882a593Smuzhiyun 
ext_man_get_fw_version(struct snd_sof_dev * sdev,const struct sof_ext_man_elem_header * hdr)151*4882a593Smuzhiyun static int ext_man_get_fw_version(struct snd_sof_dev *sdev,
152*4882a593Smuzhiyun 				  const struct sof_ext_man_elem_header *hdr)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	const struct sof_ext_man_fw_version *v =
155*4882a593Smuzhiyun 		container_of(hdr, struct sof_ext_man_fw_version, hdr);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	memcpy(&sdev->fw_ready.version, &v->version, sizeof(v->version));
158*4882a593Smuzhiyun 	sdev->fw_ready.flags = v->flags;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* log ABI versions and check FW compatibility */
161*4882a593Smuzhiyun 	return snd_sof_ipc_valid(sdev);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
ext_man_get_windows(struct snd_sof_dev * sdev,const struct sof_ext_man_elem_header * hdr)164*4882a593Smuzhiyun static int ext_man_get_windows(struct snd_sof_dev *sdev,
165*4882a593Smuzhiyun 			       const struct sof_ext_man_elem_header *hdr)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	const struct sof_ext_man_window *w;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	w = container_of(hdr, struct sof_ext_man_window, hdr);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	return get_ext_windows(sdev, &w->ipc_window.ext_hdr);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
ext_man_get_cc_info(struct snd_sof_dev * sdev,const struct sof_ext_man_elem_header * hdr)174*4882a593Smuzhiyun static int ext_man_get_cc_info(struct snd_sof_dev *sdev,
175*4882a593Smuzhiyun 			       const struct sof_ext_man_elem_header *hdr)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	const struct sof_ext_man_cc_version *cc;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	cc = container_of(hdr, struct sof_ext_man_cc_version, hdr);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	return get_cc_info(sdev, &cc->cc_version.ext_hdr);
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
ext_man_get_dbg_abi_info(struct snd_sof_dev * sdev,const struct sof_ext_man_elem_header * hdr)184*4882a593Smuzhiyun static int ext_man_get_dbg_abi_info(struct snd_sof_dev *sdev,
185*4882a593Smuzhiyun 				    const struct sof_ext_man_elem_header *hdr)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	const struct ext_man_dbg_abi *dbg_abi =
188*4882a593Smuzhiyun 		container_of(hdr, struct ext_man_dbg_abi, hdr);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (sdev->first_boot)
191*4882a593Smuzhiyun 		dev_dbg(sdev->dev,
192*4882a593Smuzhiyun 			"Firmware: DBG_ABI %d:%d:%d\n",
193*4882a593Smuzhiyun 			SOF_ABI_VERSION_MAJOR(dbg_abi->dbg_abi.abi_dbg_version),
194*4882a593Smuzhiyun 			SOF_ABI_VERSION_MINOR(dbg_abi->dbg_abi.abi_dbg_version),
195*4882a593Smuzhiyun 			SOF_ABI_VERSION_PATCH(dbg_abi->dbg_abi.abi_dbg_version));
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return 0;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
snd_sof_ext_man_size(const struct firmware * fw)200*4882a593Smuzhiyun static ssize_t snd_sof_ext_man_size(const struct firmware *fw)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	const struct sof_ext_man_header *head;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	head = (struct sof_ext_man_header *)fw->data;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/*
207*4882a593Smuzhiyun 	 * assert fw size is big enough to contain extended manifest header,
208*4882a593Smuzhiyun 	 * it prevents from reading unallocated memory from `head` in following
209*4882a593Smuzhiyun 	 * step.
210*4882a593Smuzhiyun 	 */
211*4882a593Smuzhiyun 	if (fw->size < sizeof(*head))
212*4882a593Smuzhiyun 		return -EINVAL;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	/*
215*4882a593Smuzhiyun 	 * When fw points to extended manifest,
216*4882a593Smuzhiyun 	 * then first u32 must be equal SOF_EXT_MAN_MAGIC_NUMBER.
217*4882a593Smuzhiyun 	 */
218*4882a593Smuzhiyun 	if (head->magic == SOF_EXT_MAN_MAGIC_NUMBER)
219*4882a593Smuzhiyun 		return head->full_size;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/* otherwise given fw don't have an extended manifest */
222*4882a593Smuzhiyun 	return 0;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun /* parse extended FW manifest data structures */
snd_sof_fw_ext_man_parse(struct snd_sof_dev * sdev,const struct firmware * fw)226*4882a593Smuzhiyun static int snd_sof_fw_ext_man_parse(struct snd_sof_dev *sdev,
227*4882a593Smuzhiyun 				    const struct firmware *fw)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	const struct sof_ext_man_elem_header *elem_hdr;
230*4882a593Smuzhiyun 	const struct sof_ext_man_header *head;
231*4882a593Smuzhiyun 	ssize_t ext_man_size;
232*4882a593Smuzhiyun 	ssize_t remaining;
233*4882a593Smuzhiyun 	uintptr_t iptr;
234*4882a593Smuzhiyun 	int ret = 0;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	head = (struct sof_ext_man_header *)fw->data;
237*4882a593Smuzhiyun 	remaining = head->full_size - head->header_size;
238*4882a593Smuzhiyun 	ext_man_size = snd_sof_ext_man_size(fw);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	/* Assert firmware starts with extended manifest */
241*4882a593Smuzhiyun 	if (ext_man_size <= 0)
242*4882a593Smuzhiyun 		return ext_man_size;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* incompatible version */
245*4882a593Smuzhiyun 	if (SOF_EXT_MAN_VERSION_INCOMPATIBLE(SOF_EXT_MAN_VERSION,
246*4882a593Smuzhiyun 					     head->header_version)) {
247*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: extended manifest version 0x%X differ from used 0x%X\n",
248*4882a593Smuzhiyun 			head->header_version, SOF_EXT_MAN_VERSION);
249*4882a593Smuzhiyun 		return -EINVAL;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	/* get first extended manifest element header */
253*4882a593Smuzhiyun 	iptr = (uintptr_t)fw->data + head->header_size;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	while (remaining > sizeof(*elem_hdr)) {
256*4882a593Smuzhiyun 		elem_hdr = (struct sof_ext_man_elem_header *)iptr;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 		dev_dbg(sdev->dev, "found sof_ext_man header type %d size 0x%X\n",
259*4882a593Smuzhiyun 			elem_hdr->type, elem_hdr->size);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 		if (elem_hdr->size < sizeof(*elem_hdr) ||
262*4882a593Smuzhiyun 		    elem_hdr->size > remaining) {
263*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: invalid sof_ext_man header size, type %d size 0x%X\n",
264*4882a593Smuzhiyun 				elem_hdr->type, elem_hdr->size);
265*4882a593Smuzhiyun 			return -EINVAL;
266*4882a593Smuzhiyun 		}
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 		/* process structure data */
269*4882a593Smuzhiyun 		switch (elem_hdr->type) {
270*4882a593Smuzhiyun 		case SOF_EXT_MAN_ELEM_FW_VERSION:
271*4882a593Smuzhiyun 			ret = ext_man_get_fw_version(sdev, elem_hdr);
272*4882a593Smuzhiyun 			break;
273*4882a593Smuzhiyun 		case SOF_EXT_MAN_ELEM_WINDOW:
274*4882a593Smuzhiyun 			ret = ext_man_get_windows(sdev, elem_hdr);
275*4882a593Smuzhiyun 			break;
276*4882a593Smuzhiyun 		case SOF_EXT_MAN_ELEM_CC_VERSION:
277*4882a593Smuzhiyun 			ret = ext_man_get_cc_info(sdev, elem_hdr);
278*4882a593Smuzhiyun 			break;
279*4882a593Smuzhiyun 		case SOF_EXT_MAN_ELEM_DBG_ABI:
280*4882a593Smuzhiyun 			ret = ext_man_get_dbg_abi_info(sdev, elem_hdr);
281*4882a593Smuzhiyun 			break;
282*4882a593Smuzhiyun 		default:
283*4882a593Smuzhiyun 			dev_warn(sdev->dev, "warning: unknown sof_ext_man header type %d size 0x%X\n",
284*4882a593Smuzhiyun 				 elem_hdr->type, elem_hdr->size);
285*4882a593Smuzhiyun 			break;
286*4882a593Smuzhiyun 		}
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 		if (ret < 0) {
289*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: failed to parse sof_ext_man header type %d size 0x%X\n",
290*4882a593Smuzhiyun 				elem_hdr->type, elem_hdr->size);
291*4882a593Smuzhiyun 			return ret;
292*4882a593Smuzhiyun 		}
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		remaining -= elem_hdr->size;
295*4882a593Smuzhiyun 		iptr += elem_hdr->size;
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (remaining) {
299*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: sof_ext_man header is inconsistent\n");
300*4882a593Smuzhiyun 		return -EINVAL;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	return ext_man_size;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun /*
307*4882a593Smuzhiyun  * IPC Firmware ready.
308*4882a593Smuzhiyun  */
sof_get_windows(struct snd_sof_dev * sdev)309*4882a593Smuzhiyun static void sof_get_windows(struct snd_sof_dev *sdev)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	struct sof_ipc_window_elem *elem;
312*4882a593Smuzhiyun 	u32 outbox_offset = 0;
313*4882a593Smuzhiyun 	u32 stream_offset = 0;
314*4882a593Smuzhiyun 	u32 inbox_offset = 0;
315*4882a593Smuzhiyun 	u32 outbox_size = 0;
316*4882a593Smuzhiyun 	u32 stream_size = 0;
317*4882a593Smuzhiyun 	u32 inbox_size = 0;
318*4882a593Smuzhiyun 	u32 debug_size = 0;
319*4882a593Smuzhiyun 	u32 debug_offset = 0;
320*4882a593Smuzhiyun 	int window_offset;
321*4882a593Smuzhiyun 	int bar;
322*4882a593Smuzhiyun 	int i;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if (!sdev->info_window) {
325*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: have no window info\n");
326*4882a593Smuzhiyun 		return;
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	bar = snd_sof_dsp_get_bar_index(sdev, SOF_FW_BLK_TYPE_SRAM);
330*4882a593Smuzhiyun 	if (bar < 0) {
331*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: have no bar mapping\n");
332*4882a593Smuzhiyun 		return;
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	for (i = 0; i < sdev->info_window->num_windows; i++) {
336*4882a593Smuzhiyun 		elem = &sdev->info_window->window[i];
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 		window_offset = snd_sof_dsp_get_window_offset(sdev, elem->id);
339*4882a593Smuzhiyun 		if (window_offset < 0) {
340*4882a593Smuzhiyun 			dev_warn(sdev->dev, "warn: no offset for window %d\n",
341*4882a593Smuzhiyun 				 elem->id);
342*4882a593Smuzhiyun 			continue;
343*4882a593Smuzhiyun 		}
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 		switch (elem->type) {
346*4882a593Smuzhiyun 		case SOF_IPC_REGION_UPBOX:
347*4882a593Smuzhiyun 			inbox_offset = window_offset + elem->offset;
348*4882a593Smuzhiyun 			inbox_size = elem->size;
349*4882a593Smuzhiyun 			snd_sof_debugfs_io_item(sdev,
350*4882a593Smuzhiyun 						sdev->bar[bar] +
351*4882a593Smuzhiyun 						inbox_offset,
352*4882a593Smuzhiyun 						elem->size, "inbox",
353*4882a593Smuzhiyun 						SOF_DEBUGFS_ACCESS_D0_ONLY);
354*4882a593Smuzhiyun 			break;
355*4882a593Smuzhiyun 		case SOF_IPC_REGION_DOWNBOX:
356*4882a593Smuzhiyun 			outbox_offset = window_offset + elem->offset;
357*4882a593Smuzhiyun 			outbox_size = elem->size;
358*4882a593Smuzhiyun 			snd_sof_debugfs_io_item(sdev,
359*4882a593Smuzhiyun 						sdev->bar[bar] +
360*4882a593Smuzhiyun 						outbox_offset,
361*4882a593Smuzhiyun 						elem->size, "outbox",
362*4882a593Smuzhiyun 						SOF_DEBUGFS_ACCESS_D0_ONLY);
363*4882a593Smuzhiyun 			break;
364*4882a593Smuzhiyun 		case SOF_IPC_REGION_TRACE:
365*4882a593Smuzhiyun 			snd_sof_debugfs_io_item(sdev,
366*4882a593Smuzhiyun 						sdev->bar[bar] +
367*4882a593Smuzhiyun 						window_offset +
368*4882a593Smuzhiyun 						elem->offset,
369*4882a593Smuzhiyun 						elem->size, "etrace",
370*4882a593Smuzhiyun 						SOF_DEBUGFS_ACCESS_D0_ONLY);
371*4882a593Smuzhiyun 			break;
372*4882a593Smuzhiyun 		case SOF_IPC_REGION_DEBUG:
373*4882a593Smuzhiyun 			debug_offset = window_offset + elem->offset;
374*4882a593Smuzhiyun 			debug_size = elem->size;
375*4882a593Smuzhiyun 			snd_sof_debugfs_io_item(sdev,
376*4882a593Smuzhiyun 						sdev->bar[bar] +
377*4882a593Smuzhiyun 						window_offset +
378*4882a593Smuzhiyun 						elem->offset,
379*4882a593Smuzhiyun 						elem->size, "debug",
380*4882a593Smuzhiyun 						SOF_DEBUGFS_ACCESS_D0_ONLY);
381*4882a593Smuzhiyun 			break;
382*4882a593Smuzhiyun 		case SOF_IPC_REGION_STREAM:
383*4882a593Smuzhiyun 			stream_offset = window_offset + elem->offset;
384*4882a593Smuzhiyun 			stream_size = elem->size;
385*4882a593Smuzhiyun 			snd_sof_debugfs_io_item(sdev,
386*4882a593Smuzhiyun 						sdev->bar[bar] +
387*4882a593Smuzhiyun 						stream_offset,
388*4882a593Smuzhiyun 						elem->size, "stream",
389*4882a593Smuzhiyun 						SOF_DEBUGFS_ACCESS_D0_ONLY);
390*4882a593Smuzhiyun 			break;
391*4882a593Smuzhiyun 		case SOF_IPC_REGION_REGS:
392*4882a593Smuzhiyun 			snd_sof_debugfs_io_item(sdev,
393*4882a593Smuzhiyun 						sdev->bar[bar] +
394*4882a593Smuzhiyun 						window_offset +
395*4882a593Smuzhiyun 						elem->offset,
396*4882a593Smuzhiyun 						elem->size, "regs",
397*4882a593Smuzhiyun 						SOF_DEBUGFS_ACCESS_D0_ONLY);
398*4882a593Smuzhiyun 			break;
399*4882a593Smuzhiyun 		case SOF_IPC_REGION_EXCEPTION:
400*4882a593Smuzhiyun 			sdev->dsp_oops_offset = window_offset + elem->offset;
401*4882a593Smuzhiyun 			snd_sof_debugfs_io_item(sdev,
402*4882a593Smuzhiyun 						sdev->bar[bar] +
403*4882a593Smuzhiyun 						window_offset +
404*4882a593Smuzhiyun 						elem->offset,
405*4882a593Smuzhiyun 						elem->size, "exception",
406*4882a593Smuzhiyun 						SOF_DEBUGFS_ACCESS_D0_ONLY);
407*4882a593Smuzhiyun 			break;
408*4882a593Smuzhiyun 		default:
409*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: get illegal window info\n");
410*4882a593Smuzhiyun 			return;
411*4882a593Smuzhiyun 		}
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	if (outbox_size == 0 || inbox_size == 0) {
415*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: get illegal mailbox window\n");
416*4882a593Smuzhiyun 		return;
417*4882a593Smuzhiyun 	}
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	snd_sof_dsp_mailbox_init(sdev, inbox_offset, inbox_size,
420*4882a593Smuzhiyun 				 outbox_offset, outbox_size);
421*4882a593Smuzhiyun 	sdev->stream_box.offset = stream_offset;
422*4882a593Smuzhiyun 	sdev->stream_box.size = stream_size;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	sdev->debug_box.offset = debug_offset;
425*4882a593Smuzhiyun 	sdev->debug_box.size = debug_size;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	dev_dbg(sdev->dev, " mailbox upstream 0x%x - size 0x%x\n",
428*4882a593Smuzhiyun 		inbox_offset, inbox_size);
429*4882a593Smuzhiyun 	dev_dbg(sdev->dev, " mailbox downstream 0x%x - size 0x%x\n",
430*4882a593Smuzhiyun 		outbox_offset, outbox_size);
431*4882a593Smuzhiyun 	dev_dbg(sdev->dev, " stream region 0x%x - size 0x%x\n",
432*4882a593Smuzhiyun 		stream_offset, stream_size);
433*4882a593Smuzhiyun 	dev_dbg(sdev->dev, " debug region 0x%x - size 0x%x\n",
434*4882a593Smuzhiyun 		debug_offset, debug_size);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun /* check for ABI compatibility and create memory windows on first boot */
sof_fw_ready(struct snd_sof_dev * sdev,u32 msg_id)438*4882a593Smuzhiyun int sof_fw_ready(struct snd_sof_dev *sdev, u32 msg_id)
439*4882a593Smuzhiyun {
440*4882a593Smuzhiyun 	struct sof_ipc_fw_ready *fw_ready = &sdev->fw_ready;
441*4882a593Smuzhiyun 	int offset;
442*4882a593Smuzhiyun 	int bar;
443*4882a593Smuzhiyun 	int ret;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	/* mailbox must be on 4k boundary */
446*4882a593Smuzhiyun 	offset = snd_sof_dsp_get_mailbox_offset(sdev);
447*4882a593Smuzhiyun 	if (offset < 0) {
448*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: have no mailbox offset\n");
449*4882a593Smuzhiyun 		return offset;
450*4882a593Smuzhiyun 	}
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	bar = snd_sof_dsp_get_bar_index(sdev, SOF_FW_BLK_TYPE_SRAM);
453*4882a593Smuzhiyun 	if (bar < 0) {
454*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: have no bar mapping\n");
455*4882a593Smuzhiyun 		return -EINVAL;
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	dev_dbg(sdev->dev, "ipc: DSP is ready 0x%8.8x offset 0x%x\n",
459*4882a593Smuzhiyun 		msg_id, offset);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	/* no need to re-check version/ABI for subsequent boots */
462*4882a593Smuzhiyun 	if (!sdev->first_boot)
463*4882a593Smuzhiyun 		return 0;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	/* copy data from the DSP FW ready offset */
466*4882a593Smuzhiyun 	sof_block_read(sdev, bar, offset, fw_ready, sizeof(*fw_ready));
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	/* make sure ABI version is compatible */
469*4882a593Smuzhiyun 	ret = snd_sof_ipc_valid(sdev);
470*4882a593Smuzhiyun 	if (ret < 0)
471*4882a593Smuzhiyun 		return ret;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	/* now check for extended data */
474*4882a593Smuzhiyun 	snd_sof_fw_parse_ext_data(sdev, bar, offset +
475*4882a593Smuzhiyun 				  sizeof(struct sof_ipc_fw_ready));
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	sof_get_windows(sdev);
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	return 0;
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun EXPORT_SYMBOL(sof_fw_ready);
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun /* generic module parser for mmaped DSPs */
snd_sof_parse_module_memcpy(struct snd_sof_dev * sdev,struct snd_sof_mod_hdr * module)484*4882a593Smuzhiyun int snd_sof_parse_module_memcpy(struct snd_sof_dev *sdev,
485*4882a593Smuzhiyun 				struct snd_sof_mod_hdr *module)
486*4882a593Smuzhiyun {
487*4882a593Smuzhiyun 	struct snd_sof_blk_hdr *block;
488*4882a593Smuzhiyun 	int count, bar;
489*4882a593Smuzhiyun 	u32 offset;
490*4882a593Smuzhiyun 	size_t remaining;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	dev_dbg(sdev->dev, "new module size 0x%x blocks 0x%x type 0x%x\n",
493*4882a593Smuzhiyun 		module->size, module->num_blocks, module->type);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	block = (struct snd_sof_blk_hdr *)((u8 *)module + sizeof(*module));
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	/* module->size doesn't include header size */
498*4882a593Smuzhiyun 	remaining = module->size;
499*4882a593Smuzhiyun 	for (count = 0; count < module->num_blocks; count++) {
500*4882a593Smuzhiyun 		/* check for wrap */
501*4882a593Smuzhiyun 		if (remaining < sizeof(*block)) {
502*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: not enough data remaining\n");
503*4882a593Smuzhiyun 			return -EINVAL;
504*4882a593Smuzhiyun 		}
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 		/* minus header size of block */
507*4882a593Smuzhiyun 		remaining -= sizeof(*block);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 		if (block->size == 0) {
510*4882a593Smuzhiyun 			dev_warn(sdev->dev,
511*4882a593Smuzhiyun 				 "warning: block %d size zero\n", count);
512*4882a593Smuzhiyun 			dev_warn(sdev->dev, " type 0x%x offset 0x%x\n",
513*4882a593Smuzhiyun 				 block->type, block->offset);
514*4882a593Smuzhiyun 			continue;
515*4882a593Smuzhiyun 		}
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 		switch (block->type) {
518*4882a593Smuzhiyun 		case SOF_FW_BLK_TYPE_RSRVD0:
519*4882a593Smuzhiyun 		case SOF_FW_BLK_TYPE_ROM...SOF_FW_BLK_TYPE_RSRVD14:
520*4882a593Smuzhiyun 			continue;	/* not handled atm */
521*4882a593Smuzhiyun 		case SOF_FW_BLK_TYPE_IRAM:
522*4882a593Smuzhiyun 		case SOF_FW_BLK_TYPE_DRAM:
523*4882a593Smuzhiyun 		case SOF_FW_BLK_TYPE_SRAM:
524*4882a593Smuzhiyun 			offset = block->offset;
525*4882a593Smuzhiyun 			bar = snd_sof_dsp_get_bar_index(sdev, block->type);
526*4882a593Smuzhiyun 			if (bar < 0) {
527*4882a593Smuzhiyun 				dev_err(sdev->dev,
528*4882a593Smuzhiyun 					"error: no BAR mapping for block type 0x%x\n",
529*4882a593Smuzhiyun 					block->type);
530*4882a593Smuzhiyun 				return bar;
531*4882a593Smuzhiyun 			}
532*4882a593Smuzhiyun 			break;
533*4882a593Smuzhiyun 		default:
534*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: bad type 0x%x for block 0x%x\n",
535*4882a593Smuzhiyun 				block->type, count);
536*4882a593Smuzhiyun 			return -EINVAL;
537*4882a593Smuzhiyun 		}
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 		dev_dbg(sdev->dev,
540*4882a593Smuzhiyun 			"block %d type 0x%x size 0x%x ==>  offset 0x%x\n",
541*4882a593Smuzhiyun 			count, block->type, block->size, offset);
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 		/* checking block->size to avoid unaligned access */
544*4882a593Smuzhiyun 		if (block->size % sizeof(u32)) {
545*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: invalid block size 0x%x\n",
546*4882a593Smuzhiyun 				block->size);
547*4882a593Smuzhiyun 			return -EINVAL;
548*4882a593Smuzhiyun 		}
549*4882a593Smuzhiyun 		snd_sof_dsp_block_write(sdev, bar, offset,
550*4882a593Smuzhiyun 					block + 1, block->size);
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 		if (remaining < block->size) {
553*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: not enough data remaining\n");
554*4882a593Smuzhiyun 			return -EINVAL;
555*4882a593Smuzhiyun 		}
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 		/* minus body size of block */
558*4882a593Smuzhiyun 		remaining -= block->size;
559*4882a593Smuzhiyun 		/* next block */
560*4882a593Smuzhiyun 		block = (struct snd_sof_blk_hdr *)((u8 *)block + sizeof(*block)
561*4882a593Smuzhiyun 			+ block->size);
562*4882a593Smuzhiyun 	}
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	return 0;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_parse_module_memcpy);
567*4882a593Smuzhiyun 
check_header(struct snd_sof_dev * sdev,const struct firmware * fw,size_t fw_offset)568*4882a593Smuzhiyun static int check_header(struct snd_sof_dev *sdev, const struct firmware *fw,
569*4882a593Smuzhiyun 			size_t fw_offset)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	struct snd_sof_fw_header *header;
572*4882a593Smuzhiyun 	size_t fw_size = fw->size - fw_offset;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	if (fw->size <= fw_offset) {
575*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: firmware size must be greater than firmware offset\n");
576*4882a593Smuzhiyun 		return -EINVAL;
577*4882a593Smuzhiyun 	}
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	/* Read the header information from the data pointer */
580*4882a593Smuzhiyun 	header = (struct snd_sof_fw_header *)(fw->data + fw_offset);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	/* verify FW sig */
583*4882a593Smuzhiyun 	if (strncmp(header->sig, SND_SOF_FW_SIG, SND_SOF_FW_SIG_SIZE) != 0) {
584*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: invalid firmware signature\n");
585*4882a593Smuzhiyun 		return -EINVAL;
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	/* check size is valid */
589*4882a593Smuzhiyun 	if (fw_size != header->file_size + sizeof(*header)) {
590*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: invalid filesize mismatch got 0x%zx expected 0x%zx\n",
591*4882a593Smuzhiyun 			fw_size, header->file_size + sizeof(*header));
592*4882a593Smuzhiyun 		return -EINVAL;
593*4882a593Smuzhiyun 	}
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	dev_dbg(sdev->dev, "header size=0x%x modules=0x%x abi=0x%x size=%zu\n",
596*4882a593Smuzhiyun 		header->file_size, header->num_modules,
597*4882a593Smuzhiyun 		header->abi, sizeof(*header));
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	return 0;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun 
load_modules(struct snd_sof_dev * sdev,const struct firmware * fw,size_t fw_offset)602*4882a593Smuzhiyun static int load_modules(struct snd_sof_dev *sdev, const struct firmware *fw,
603*4882a593Smuzhiyun 			size_t fw_offset)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	struct snd_sof_fw_header *header;
606*4882a593Smuzhiyun 	struct snd_sof_mod_hdr *module;
607*4882a593Smuzhiyun 	int (*load_module)(struct snd_sof_dev *sof_dev,
608*4882a593Smuzhiyun 			   struct snd_sof_mod_hdr *hdr);
609*4882a593Smuzhiyun 	int ret, count;
610*4882a593Smuzhiyun 	size_t remaining;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	header = (struct snd_sof_fw_header *)(fw->data + fw_offset);
613*4882a593Smuzhiyun 	load_module = sof_ops(sdev)->load_module;
614*4882a593Smuzhiyun 	if (!load_module)
615*4882a593Smuzhiyun 		return -EINVAL;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	/* parse each module */
618*4882a593Smuzhiyun 	module = (struct snd_sof_mod_hdr *)(fw->data + fw_offset +
619*4882a593Smuzhiyun 					    sizeof(*header));
620*4882a593Smuzhiyun 	remaining = fw->size - sizeof(*header) - fw_offset;
621*4882a593Smuzhiyun 	/* check for wrap */
622*4882a593Smuzhiyun 	if (remaining > fw->size) {
623*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: fw size smaller than header size\n");
624*4882a593Smuzhiyun 		return -EINVAL;
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	for (count = 0; count < header->num_modules; count++) {
628*4882a593Smuzhiyun 		/* check for wrap */
629*4882a593Smuzhiyun 		if (remaining < sizeof(*module)) {
630*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: not enough data remaining\n");
631*4882a593Smuzhiyun 			return -EINVAL;
632*4882a593Smuzhiyun 		}
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 		/* minus header size of module */
635*4882a593Smuzhiyun 		remaining -= sizeof(*module);
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 		/* module */
638*4882a593Smuzhiyun 		ret = load_module(sdev, module);
639*4882a593Smuzhiyun 		if (ret < 0) {
640*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: invalid module %d\n", count);
641*4882a593Smuzhiyun 			return ret;
642*4882a593Smuzhiyun 		}
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 		if (remaining < module->size) {
645*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: not enough data remaining\n");
646*4882a593Smuzhiyun 			return -EINVAL;
647*4882a593Smuzhiyun 		}
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 		/* minus body size of module */
650*4882a593Smuzhiyun 		remaining -=  module->size;
651*4882a593Smuzhiyun 		module = (struct snd_sof_mod_hdr *)((u8 *)module
652*4882a593Smuzhiyun 			+ sizeof(*module) + module->size);
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	return 0;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun 
snd_sof_load_firmware_raw(struct snd_sof_dev * sdev)658*4882a593Smuzhiyun int snd_sof_load_firmware_raw(struct snd_sof_dev *sdev)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	struct snd_sof_pdata *plat_data = sdev->pdata;
661*4882a593Smuzhiyun 	const char *fw_filename;
662*4882a593Smuzhiyun 	ssize_t ext_man_size;
663*4882a593Smuzhiyun 	int ret;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	/* Don't request firmware again if firmware is already requested */
666*4882a593Smuzhiyun 	if (plat_data->fw)
667*4882a593Smuzhiyun 		return 0;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	fw_filename = kasprintf(GFP_KERNEL, "%s/%s",
670*4882a593Smuzhiyun 				plat_data->fw_filename_prefix,
671*4882a593Smuzhiyun 				plat_data->fw_filename);
672*4882a593Smuzhiyun 	if (!fw_filename)
673*4882a593Smuzhiyun 		return -ENOMEM;
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	ret = request_firmware(&plat_data->fw, fw_filename, sdev->dev);
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (ret < 0) {
678*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: request firmware %s failed err: %d\n",
679*4882a593Smuzhiyun 			fw_filename, ret);
680*4882a593Smuzhiyun 		goto err;
681*4882a593Smuzhiyun 	} else {
682*4882a593Smuzhiyun 		dev_dbg(sdev->dev, "request_firmware %s successful\n",
683*4882a593Smuzhiyun 			fw_filename);
684*4882a593Smuzhiyun 	}
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	/* check for extended manifest */
687*4882a593Smuzhiyun 	ext_man_size = snd_sof_fw_ext_man_parse(sdev, plat_data->fw);
688*4882a593Smuzhiyun 	if (ext_man_size > 0) {
689*4882a593Smuzhiyun 		/* when no error occurred, drop extended manifest */
690*4882a593Smuzhiyun 		plat_data->fw_offset = ext_man_size;
691*4882a593Smuzhiyun 	} else if (!ext_man_size) {
692*4882a593Smuzhiyun 		/* No extended manifest, so nothing to skip during FW load */
693*4882a593Smuzhiyun 		dev_dbg(sdev->dev, "firmware doesn't contain extended manifest\n");
694*4882a593Smuzhiyun 	} else {
695*4882a593Smuzhiyun 		ret = ext_man_size;
696*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: firmware %s contains unsupported or invalid extended manifest: %d\n",
697*4882a593Smuzhiyun 			fw_filename, ret);
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun err:
701*4882a593Smuzhiyun 	kfree(fw_filename);
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	return ret;
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_load_firmware_raw);
706*4882a593Smuzhiyun 
snd_sof_load_firmware_memcpy(struct snd_sof_dev * sdev)707*4882a593Smuzhiyun int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun 	struct snd_sof_pdata *plat_data = sdev->pdata;
710*4882a593Smuzhiyun 	int ret;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	ret = snd_sof_load_firmware_raw(sdev);
713*4882a593Smuzhiyun 	if (ret < 0)
714*4882a593Smuzhiyun 		return ret;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	/* make sure the FW header and file is valid */
717*4882a593Smuzhiyun 	ret = check_header(sdev, plat_data->fw, plat_data->fw_offset);
718*4882a593Smuzhiyun 	if (ret < 0) {
719*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: invalid FW header\n");
720*4882a593Smuzhiyun 		goto error;
721*4882a593Smuzhiyun 	}
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	/* prepare the DSP for FW loading */
724*4882a593Smuzhiyun 	ret = snd_sof_dsp_reset(sdev);
725*4882a593Smuzhiyun 	if (ret < 0) {
726*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: failed to reset DSP\n");
727*4882a593Smuzhiyun 		goto error;
728*4882a593Smuzhiyun 	}
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	/* parse and load firmware modules to DSP */
731*4882a593Smuzhiyun 	ret = load_modules(sdev, plat_data->fw, plat_data->fw_offset);
732*4882a593Smuzhiyun 	if (ret < 0) {
733*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: invalid FW modules\n");
734*4882a593Smuzhiyun 		goto error;
735*4882a593Smuzhiyun 	}
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	return 0;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun error:
740*4882a593Smuzhiyun 	release_firmware(plat_data->fw);
741*4882a593Smuzhiyun 	plat_data->fw = NULL;
742*4882a593Smuzhiyun 	return ret;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_load_firmware_memcpy);
746*4882a593Smuzhiyun 
snd_sof_load_firmware(struct snd_sof_dev * sdev)747*4882a593Smuzhiyun int snd_sof_load_firmware(struct snd_sof_dev *sdev)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun 	dev_dbg(sdev->dev, "loading firmware\n");
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	if (sof_ops(sdev)->load_firmware)
752*4882a593Smuzhiyun 		return sof_ops(sdev)->load_firmware(sdev);
753*4882a593Smuzhiyun 	return 0;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_load_firmware);
756*4882a593Smuzhiyun 
snd_sof_run_firmware(struct snd_sof_dev * sdev)757*4882a593Smuzhiyun int snd_sof_run_firmware(struct snd_sof_dev *sdev)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun 	int ret;
760*4882a593Smuzhiyun 	int init_core_mask;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	init_waitqueue_head(&sdev->boot_wait);
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	/* create read-only fw_version debugfs to store boot version info */
765*4882a593Smuzhiyun 	if (sdev->first_boot) {
766*4882a593Smuzhiyun 		ret = snd_sof_debugfs_buf_item(sdev, &sdev->fw_version,
767*4882a593Smuzhiyun 					       sizeof(sdev->fw_version),
768*4882a593Smuzhiyun 					       "fw_version", 0444);
769*4882a593Smuzhiyun 		/* errors are only due to memory allocation, not debugfs */
770*4882a593Smuzhiyun 		if (ret < 0) {
771*4882a593Smuzhiyun 			dev_err(sdev->dev, "error: snd_sof_debugfs_buf_item failed\n");
772*4882a593Smuzhiyun 			return ret;
773*4882a593Smuzhiyun 		}
774*4882a593Smuzhiyun 	}
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	/* perform pre fw run operations */
777*4882a593Smuzhiyun 	ret = snd_sof_dsp_pre_fw_run(sdev);
778*4882a593Smuzhiyun 	if (ret < 0) {
779*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: failed pre fw run op\n");
780*4882a593Smuzhiyun 		return ret;
781*4882a593Smuzhiyun 	}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	dev_dbg(sdev->dev, "booting DSP firmware\n");
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	/* boot the firmware on the DSP */
786*4882a593Smuzhiyun 	ret = snd_sof_dsp_run(sdev);
787*4882a593Smuzhiyun 	if (ret < 0) {
788*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: failed to reset DSP\n");
789*4882a593Smuzhiyun 		return ret;
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	init_core_mask = ret;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	/*
795*4882a593Smuzhiyun 	 * now wait for the DSP to boot. There are 3 possible outcomes:
796*4882a593Smuzhiyun 	 * 1. Boot wait times out indicating FW boot failure.
797*4882a593Smuzhiyun 	 * 2. FW boots successfully and fw_ready op succeeds.
798*4882a593Smuzhiyun 	 * 3. FW boots but fw_ready op fails.
799*4882a593Smuzhiyun 	 */
800*4882a593Smuzhiyun 	ret = wait_event_timeout(sdev->boot_wait,
801*4882a593Smuzhiyun 				 sdev->fw_state > SOF_FW_BOOT_IN_PROGRESS,
802*4882a593Smuzhiyun 				 msecs_to_jiffies(sdev->boot_timeout));
803*4882a593Smuzhiyun 	if (ret == 0) {
804*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: firmware boot failure\n");
805*4882a593Smuzhiyun 		snd_sof_dsp_dbg_dump(sdev, SOF_DBG_REGS | SOF_DBG_MBOX |
806*4882a593Smuzhiyun 			SOF_DBG_TEXT | SOF_DBG_PCI);
807*4882a593Smuzhiyun 		sdev->fw_state = SOF_FW_BOOT_FAILED;
808*4882a593Smuzhiyun 		return -EIO;
809*4882a593Smuzhiyun 	}
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	if (sdev->fw_state == SOF_FW_BOOT_COMPLETE)
812*4882a593Smuzhiyun 		dev_dbg(sdev->dev, "firmware boot complete\n");
813*4882a593Smuzhiyun 	else
814*4882a593Smuzhiyun 		return -EIO; /* FW boots but fw_ready op failed */
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	/* perform post fw run operations */
817*4882a593Smuzhiyun 	ret = snd_sof_dsp_post_fw_run(sdev);
818*4882a593Smuzhiyun 	if (ret < 0) {
819*4882a593Smuzhiyun 		dev_err(sdev->dev, "error: failed post fw run op\n");
820*4882a593Smuzhiyun 		return ret;
821*4882a593Smuzhiyun 	}
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	/* fw boot is complete. Update the active cores mask */
824*4882a593Smuzhiyun 	sdev->enabled_cores_mask = init_core_mask;
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	return 0;
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_run_firmware);
829*4882a593Smuzhiyun 
snd_sof_fw_unload(struct snd_sof_dev * sdev)830*4882a593Smuzhiyun void snd_sof_fw_unload(struct snd_sof_dev *sdev)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun 	/* TODO: support module unloading at runtime */
833*4882a593Smuzhiyun 	release_firmware(sdev->pdata->fw);
834*4882a593Smuzhiyun 	sdev->pdata->fw = NULL;
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_fw_unload);
837