xref: /openbmc/linux/sound/soc/sof/ipc3-loader.c (revision 9221b289)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2022 Intel Corporation. All rights reserved.
7 
8 #include <linux/firmware.h>
9 #include "sof-priv.h"
10 #include "sof-audio.h"
11 #include "ipc3-priv.h"
12 #include "ops.h"
13 
14 static int ipc3_fw_ext_man_get_version(struct snd_sof_dev *sdev,
15 				       const struct sof_ext_man_elem_header *hdr)
16 {
17 	const struct sof_ext_man_fw_version *v =
18 		container_of(hdr, struct sof_ext_man_fw_version, hdr);
19 
20 	memcpy(&sdev->fw_ready.version, &v->version, sizeof(v->version));
21 	sdev->fw_ready.flags = v->flags;
22 
23 	/* log ABI versions and check FW compatibility */
24 	return sof_ipc3_validate_fw_version(sdev);
25 }
26 
27 static int ipc3_fw_ext_man_get_windows(struct snd_sof_dev *sdev,
28 				       const struct sof_ext_man_elem_header *hdr)
29 {
30 	const struct sof_ext_man_window *w;
31 
32 	w = container_of(hdr, struct sof_ext_man_window, hdr);
33 
34 	return sof_ipc3_get_ext_windows(sdev, &w->ipc_window.ext_hdr);
35 }
36 
37 static int ipc3_fw_ext_man_get_cc_info(struct snd_sof_dev *sdev,
38 				       const struct sof_ext_man_elem_header *hdr)
39 {
40 	const struct sof_ext_man_cc_version *cc;
41 
42 	cc = container_of(hdr, struct sof_ext_man_cc_version, hdr);
43 
44 	return sof_ipc3_get_cc_info(sdev, &cc->cc_version.ext_hdr);
45 }
46 
47 static int ipc3_fw_ext_man_get_dbg_abi_info(struct snd_sof_dev *sdev,
48 					    const struct sof_ext_man_elem_header *hdr)
49 {
50 	const struct ext_man_dbg_abi *dbg_abi =
51 		container_of(hdr, struct ext_man_dbg_abi, hdr);
52 
53 	if (sdev->first_boot)
54 		dev_dbg(sdev->dev,
55 			"Firmware: DBG_ABI %d:%d:%d\n",
56 			SOF_ABI_VERSION_MAJOR(dbg_abi->dbg_abi.abi_dbg_version),
57 			SOF_ABI_VERSION_MINOR(dbg_abi->dbg_abi.abi_dbg_version),
58 			SOF_ABI_VERSION_PATCH(dbg_abi->dbg_abi.abi_dbg_version));
59 
60 	return 0;
61 }
62 
63 static int ipc3_fw_ext_man_get_config_data(struct snd_sof_dev *sdev,
64 					   const struct sof_ext_man_elem_header *hdr)
65 {
66 	const struct sof_ext_man_config_data *config =
67 		container_of(hdr, struct sof_ext_man_config_data, hdr);
68 	const struct sof_config_elem *elem;
69 	int elems_counter;
70 	int elems_size;
71 	int ret = 0;
72 	int i;
73 
74 	/* calculate elements counter */
75 	elems_size = config->hdr.size - sizeof(struct sof_ext_man_elem_header);
76 	elems_counter = elems_size / sizeof(struct sof_config_elem);
77 
78 	dev_dbg(sdev->dev, "manifest can hold up to %d config elements\n", elems_counter);
79 
80 	for (i = 0; i < elems_counter; ++i) {
81 		elem = &config->elems[i];
82 		dev_dbg(sdev->dev, "get index %d token %d val %d\n",
83 			i, elem->token, elem->value);
84 		switch (elem->token) {
85 		case SOF_EXT_MAN_CONFIG_EMPTY:
86 			/* unused memory space is zero filled - mapped to EMPTY elements */
87 			break;
88 		case SOF_EXT_MAN_CONFIG_IPC_MSG_SIZE:
89 			/* TODO: use ipc msg size from config data */
90 			break;
91 		case SOF_EXT_MAN_CONFIG_MEMORY_USAGE_SCAN:
92 			if (sdev->first_boot && elem->value)
93 				ret = snd_sof_dbg_memory_info_init(sdev);
94 			break;
95 		default:
96 			dev_info(sdev->dev,
97 				 "Unknown firmware configuration token %d value %d",
98 				 elem->token, elem->value);
99 			break;
100 		}
101 		if (ret < 0) {
102 			dev_err(sdev->dev,
103 				"%s: processing failed for token %d value %#x, %d\n",
104 				__func__, elem->token, elem->value, ret);
105 			return ret;
106 		}
107 	}
108 
109 	return 0;
110 }
111 
112 static ssize_t ipc3_fw_ext_man_size(struct snd_sof_dev *sdev, const struct firmware *fw)
113 {
114 	const struct sof_ext_man_header *head;
115 
116 	head = (struct sof_ext_man_header *)fw->data;
117 
118 	/*
119 	 * assert fw size is big enough to contain extended manifest header,
120 	 * it prevents from reading unallocated memory from `head` in following
121 	 * step.
122 	 */
123 	if (fw->size < sizeof(*head))
124 		return -EINVAL;
125 
126 	/*
127 	 * When fw points to extended manifest,
128 	 * then first u32 must be equal SOF_EXT_MAN_MAGIC_NUMBER.
129 	 */
130 	if (head->magic == SOF_EXT_MAN_MAGIC_NUMBER)
131 		return head->full_size;
132 
133 	/* otherwise given fw don't have an extended manifest */
134 	dev_dbg(sdev->dev, "Unexpected extended manifest magic number: %#x\n",
135 		head->magic);
136 	return 0;
137 }
138 
139 static size_t sof_ipc3_fw_parse_ext_man(struct snd_sof_dev *sdev)
140 {
141 	struct snd_sof_pdata *plat_data = sdev->pdata;
142 	const struct firmware *fw = plat_data->fw;
143 	const struct sof_ext_man_elem_header *elem_hdr;
144 	const struct sof_ext_man_header *head;
145 	ssize_t ext_man_size;
146 	ssize_t remaining;
147 	uintptr_t iptr;
148 	int ret = 0;
149 
150 	head = (struct sof_ext_man_header *)fw->data;
151 	remaining = head->full_size - head->header_size;
152 	ext_man_size = ipc3_fw_ext_man_size(sdev, fw);
153 
154 	/* Assert firmware starts with extended manifest */
155 	if (ext_man_size <= 0)
156 		return ext_man_size;
157 
158 	/* incompatible version */
159 	if (SOF_EXT_MAN_VERSION_INCOMPATIBLE(SOF_EXT_MAN_VERSION,
160 					     head->header_version)) {
161 		dev_err(sdev->dev,
162 			"extended manifest version %#x differ from used %#x\n",
163 			head->header_version, SOF_EXT_MAN_VERSION);
164 		return -EINVAL;
165 	}
166 
167 	/* get first extended manifest element header */
168 	iptr = (uintptr_t)fw->data + head->header_size;
169 
170 	while (remaining > sizeof(*elem_hdr)) {
171 		elem_hdr = (struct sof_ext_man_elem_header *)iptr;
172 
173 		dev_dbg(sdev->dev, "found sof_ext_man header type %d size %#x\n",
174 			elem_hdr->type, elem_hdr->size);
175 
176 		if (elem_hdr->size < sizeof(*elem_hdr) ||
177 		    elem_hdr->size > remaining) {
178 			dev_err(sdev->dev,
179 				"invalid sof_ext_man header size, type %d size %#x\n",
180 				elem_hdr->type, elem_hdr->size);
181 			return -EINVAL;
182 		}
183 
184 		/* process structure data */
185 		switch (elem_hdr->type) {
186 		case SOF_EXT_MAN_ELEM_FW_VERSION:
187 			ret = ipc3_fw_ext_man_get_version(sdev, elem_hdr);
188 			break;
189 		case SOF_EXT_MAN_ELEM_WINDOW:
190 			ret = ipc3_fw_ext_man_get_windows(sdev, elem_hdr);
191 			break;
192 		case SOF_EXT_MAN_ELEM_CC_VERSION:
193 			ret = ipc3_fw_ext_man_get_cc_info(sdev, elem_hdr);
194 			break;
195 		case SOF_EXT_MAN_ELEM_DBG_ABI:
196 			ret = ipc3_fw_ext_man_get_dbg_abi_info(sdev, elem_hdr);
197 			break;
198 		case SOF_EXT_MAN_ELEM_CONFIG_DATA:
199 			ret = ipc3_fw_ext_man_get_config_data(sdev, elem_hdr);
200 			break;
201 		case SOF_EXT_MAN_ELEM_PLATFORM_CONFIG_DATA:
202 			ret = snd_sof_dsp_parse_platform_ext_manifest(sdev, elem_hdr);
203 			break;
204 		default:
205 			dev_info(sdev->dev,
206 				 "unknown sof_ext_man header type %d size %#x\n",
207 				 elem_hdr->type, elem_hdr->size);
208 			break;
209 		}
210 
211 		if (ret < 0) {
212 			dev_err(sdev->dev,
213 				"failed to parse sof_ext_man header type %d size %#x\n",
214 				elem_hdr->type, elem_hdr->size);
215 			return ret;
216 		}
217 
218 		remaining -= elem_hdr->size;
219 		iptr += elem_hdr->size;
220 	}
221 
222 	if (remaining) {
223 		dev_err(sdev->dev, "error: sof_ext_man header is inconsistent\n");
224 		return -EINVAL;
225 	}
226 
227 	return ext_man_size;
228 }
229 
230 /* generic module parser for mmaped DSPs */
231 static int sof_ipc3_parse_module_memcpy(struct snd_sof_dev *sdev,
232 					struct snd_sof_mod_hdr *module)
233 {
234 	struct snd_sof_blk_hdr *block;
235 	int count, ret;
236 	u32 offset;
237 	size_t remaining;
238 
239 	dev_dbg(sdev->dev, "new module size %#x blocks %#x type %#x\n",
240 		module->size, module->num_blocks, module->type);
241 
242 	block = (struct snd_sof_blk_hdr *)((u8 *)module + sizeof(*module));
243 
244 	/* module->size doesn't include header size */
245 	remaining = module->size;
246 	for (count = 0; count < module->num_blocks; count++) {
247 		/* check for wrap */
248 		if (remaining < sizeof(*block)) {
249 			dev_err(sdev->dev, "not enough data remaining\n");
250 			return -EINVAL;
251 		}
252 
253 		/* minus header size of block */
254 		remaining -= sizeof(*block);
255 
256 		if (block->size == 0) {
257 			dev_warn(sdev->dev,
258 				 "warning: block %d size zero\n", count);
259 			dev_warn(sdev->dev, " type %#x offset %#x\n",
260 				 block->type, block->offset);
261 			continue;
262 		}
263 
264 		switch (block->type) {
265 		case SOF_FW_BLK_TYPE_RSRVD0:
266 		case SOF_FW_BLK_TYPE_ROM...SOF_FW_BLK_TYPE_RSRVD14:
267 			continue;	/* not handled atm */
268 		case SOF_FW_BLK_TYPE_IRAM:
269 		case SOF_FW_BLK_TYPE_DRAM:
270 		case SOF_FW_BLK_TYPE_SRAM:
271 			offset = block->offset;
272 			break;
273 		default:
274 			dev_err(sdev->dev, "%s: bad type %#x for block %#x\n",
275 				__func__, block->type, count);
276 			return -EINVAL;
277 		}
278 
279 		dev_dbg(sdev->dev, "block %d type %#x size %#x ==>  offset %#x\n",
280 			count, block->type, block->size, offset);
281 
282 		/* checking block->size to avoid unaligned access */
283 		if (block->size % sizeof(u32)) {
284 			dev_err(sdev->dev, "%s: invalid block size %#x\n",
285 				__func__, block->size);
286 			return -EINVAL;
287 		}
288 		ret = snd_sof_dsp_block_write(sdev, block->type, offset,
289 					      block + 1, block->size);
290 		if (ret < 0) {
291 			dev_err(sdev->dev, "%s: write to block type %#x failed\n",
292 				__func__, block->type);
293 			return ret;
294 		}
295 
296 		if (remaining < block->size) {
297 			dev_err(sdev->dev, "%s: not enough data remaining\n", __func__);
298 			return -EINVAL;
299 		}
300 
301 		/* minus body size of block */
302 		remaining -= block->size;
303 		/* next block */
304 		block = (struct snd_sof_blk_hdr *)((u8 *)block + sizeof(*block)
305 			+ block->size);
306 	}
307 
308 	return 0;
309 }
310 
311 static int sof_ipc3_load_fw_to_dsp(struct snd_sof_dev *sdev)
312 {
313 	struct snd_sof_pdata *plat_data = sdev->pdata;
314 	const struct firmware *fw = plat_data->fw;
315 	struct snd_sof_fw_header *header;
316 	struct snd_sof_mod_hdr *module;
317 	int (*load_module)(struct snd_sof_dev *sof_dev, struct snd_sof_mod_hdr *hdr);
318 	size_t remaining;
319 	int ret, count;
320 
321 	if (!plat_data->fw)
322 		return -EINVAL;
323 
324 	header = (struct snd_sof_fw_header *)(fw->data + plat_data->fw_offset);
325 	load_module = sof_ops(sdev)->load_module;
326 	if (!load_module) {
327 		dev_dbg(sdev->dev, "Using generic module loading\n");
328 		load_module = sof_ipc3_parse_module_memcpy;
329 	} else {
330 		dev_dbg(sdev->dev, "Using custom module loading\n");
331 	}
332 
333 	/* parse each module */
334 	module = (struct snd_sof_mod_hdr *)(fw->data + plat_data->fw_offset +
335 					    sizeof(*header));
336 	remaining = fw->size - sizeof(*header) - plat_data->fw_offset;
337 	/* check for wrap */
338 	if (remaining > fw->size) {
339 		dev_err(sdev->dev, "%s: fw size smaller than header size\n", __func__);
340 		return -EINVAL;
341 	}
342 
343 	for (count = 0; count < header->num_modules; count++) {
344 		/* check for wrap */
345 		if (remaining < sizeof(*module)) {
346 			dev_err(sdev->dev, "%s: not enough data for a module\n",
347 				__func__);
348 			return -EINVAL;
349 		}
350 
351 		/* minus header size of module */
352 		remaining -= sizeof(*module);
353 
354 		/* module */
355 		ret = load_module(sdev, module);
356 		if (ret < 0) {
357 			dev_err(sdev->dev, "%s: invalid module %d\n", __func__, count);
358 			return ret;
359 		}
360 
361 		if (remaining < module->size) {
362 			dev_err(sdev->dev, "%s: not enough data remaining\n", __func__);
363 			return -EINVAL;
364 		}
365 
366 		/* minus body size of module */
367 		remaining -=  module->size;
368 		module = (struct snd_sof_mod_hdr *)((u8 *)module +
369 			 sizeof(*module) + module->size);
370 	}
371 
372 	return 0;
373 }
374 
375 static int sof_ipc3_validate_firmware(struct snd_sof_dev *sdev)
376 {
377 	struct snd_sof_pdata *plat_data = sdev->pdata;
378 	const struct firmware *fw = plat_data->fw;
379 	struct snd_sof_fw_header *header;
380 	size_t fw_size = fw->size - plat_data->fw_offset;
381 
382 	if (fw->size <= plat_data->fw_offset) {
383 		dev_err(sdev->dev,
384 			"firmware size must be greater than firmware offset\n");
385 		return -EINVAL;
386 	}
387 
388 	/* Read the header information from the data pointer */
389 	header = (struct snd_sof_fw_header *)(fw->data + plat_data->fw_offset);
390 
391 	/* verify FW sig */
392 	if (strncmp(header->sig, SND_SOF_FW_SIG, SND_SOF_FW_SIG_SIZE) != 0) {
393 		dev_err(sdev->dev, "invalid firmware signature\n");
394 		return -EINVAL;
395 	}
396 
397 	/* check size is valid */
398 	if (fw_size != header->file_size + sizeof(*header)) {
399 		dev_err(sdev->dev,
400 			"invalid filesize mismatch got 0x%zx expected 0x%zx\n",
401 			fw_size, header->file_size + sizeof(*header));
402 		return -EINVAL;
403 	}
404 
405 	dev_dbg(sdev->dev, "header size=0x%x modules=0x%x abi=0x%x size=%zu\n",
406 		header->file_size, header->num_modules,
407 		header->abi, sizeof(*header));
408 
409 	return 0;
410 }
411 
412 const struct sof_ipc_fw_loader_ops ipc3_loader_ops = {
413 	.validate = sof_ipc3_validate_firmware,
414 	.parse_ext_manifest = sof_ipc3_fw_parse_ext_man,
415 	.load_fw_to_dsp = sof_ipc3_load_fw_to_dsp,
416 };
417