xref: /linux/sound/soc/sof/core.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
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) 2018 Intel Corporation
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10 
11 #include <linux/firmware.h>
12 #include <linux/module.h>
13 #include <sound/soc.h>
14 #include <sound/sof.h>
15 #include "sof-priv.h"
16 #include "sof-of-dev.h"
17 #include "ops.h"
18 
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/sof.h>
21 
22 /* Module parameters for firmware, topology and IPC type override */
23 static char *override_fw_path;
24 module_param_named(fw_path, override_fw_path, charp, 0444);
25 MODULE_PARM_DESC(fw_path, "alternate path for SOF firmware.");
26 
27 static char *override_fw_filename;
28 module_param_named(fw_filename, override_fw_filename, charp, 0444);
29 MODULE_PARM_DESC(fw_filename, "alternate filename for SOF firmware.");
30 
31 static char *override_lib_path;
32 module_param_named(lib_path, override_lib_path, charp, 0444);
33 MODULE_PARM_DESC(lib_path, "alternate path for SOF firmware libraries.");
34 
35 static char *override_tplg_path;
36 module_param_named(tplg_path, override_tplg_path, charp, 0444);
37 MODULE_PARM_DESC(tplg_path, "alternate path for SOF topology.");
38 
39 static char *override_tplg_filename;
40 module_param_named(tplg_filename, override_tplg_filename, charp, 0444);
41 MODULE_PARM_DESC(tplg_filename, "alternate filename for SOF topology.");
42 
43 static int override_ipc_type = -1;
44 module_param_named(ipc_type, override_ipc_type, int, 0444);
45 MODULE_PARM_DESC(ipc_type, "Force SOF IPC type. 0 - IPC3, 1 - IPC4");
46 
47 /* see SOF_DBG_ flags */
48 static int sof_core_debug =  IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_FIRMWARE_TRACE);
49 module_param_named(sof_debug, sof_core_debug, int, 0444);
50 MODULE_PARM_DESC(sof_debug, "SOF core debug options (0x0 all off)");
51 
52 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
53 static unsigned int sof_ipc_timeout_ms;
54 static unsigned int sof_boot_timeout_ms;
55 module_param_named(ipc_timeout, sof_ipc_timeout_ms, uint, 0444);
56 MODULE_PARM_DESC(ipc_timeout,
57 		 "Set the IPC timeout value in ms (0 to use the platform default)");
58 module_param_named(boot_timeout, sof_boot_timeout_ms, uint, 0444);
59 MODULE_PARM_DESC(boot_timeout,
60 		 "Set the DSP boot timeout value in ms (0 to use the platform default)");
61 #endif
62 
63 /* SOF defaults if not provided by the platform in ms */
64 #define TIMEOUT_DEFAULT_IPC_MS  500
65 #define TIMEOUT_DEFAULT_BOOT_MS 2000
66 
67 /**
68  * sof_debug_check_flag - check if a given flag(s) is set in sof_core_debug
69  * @mask: Flag or combination of flags to check
70  *
71  * Returns true if all bits set in mask is also set in sof_core_debug, otherwise
72  * false
73  */
74 bool sof_debug_check_flag(int mask)
75 {
76 	if ((sof_core_debug & mask) == mask)
77 		return true;
78 
79 	return false;
80 }
81 EXPORT_SYMBOL(sof_debug_check_flag);
82 
83 /*
84  * FW Panic/fault handling.
85  */
86 
87 struct sof_panic_msg {
88 	u32 id;
89 	const char *msg;
90 };
91 
92 /* standard FW panic types */
93 static const struct sof_panic_msg panic_msg[] = {
94 	{SOF_IPC_PANIC_MEM, "out of memory"},
95 	{SOF_IPC_PANIC_WORK, "work subsystem init failed"},
96 	{SOF_IPC_PANIC_IPC, "IPC subsystem init failed"},
97 	{SOF_IPC_PANIC_ARCH, "arch init failed"},
98 	{SOF_IPC_PANIC_PLATFORM, "platform init failed"},
99 	{SOF_IPC_PANIC_TASK, "scheduler init failed"},
100 	{SOF_IPC_PANIC_EXCEPTION, "runtime exception"},
101 	{SOF_IPC_PANIC_DEADLOCK, "deadlock"},
102 	{SOF_IPC_PANIC_STACK, "stack overflow"},
103 	{SOF_IPC_PANIC_IDLE, "can't enter idle"},
104 	{SOF_IPC_PANIC_WFI, "invalid wait state"},
105 	{SOF_IPC_PANIC_ASSERT, "assertion failed"},
106 };
107 
108 /**
109  * sof_print_oops_and_stack - Handle the printing of DSP oops and stack trace
110  * @sdev: Pointer to the device's sdev
111  * @level: prink log level to use for the printing
112  * @panic_code: the panic code
113  * @tracep_code: tracepoint code
114  * @oops: Pointer to DSP specific oops data
115  * @panic_info: Pointer to the received panic information message
116  * @stack: Pointer to the call stack data
117  * @stack_words: Number of words in the stack data
118  *
119  * helper to be called from .dbg_dump callbacks. No error code is
120  * provided, it's left as an exercise for the caller of .dbg_dump
121  * (typically IPC or loader)
122  */
123 void sof_print_oops_and_stack(struct snd_sof_dev *sdev, const char *level,
124 			      u32 panic_code, u32 tracep_code, void *oops,
125 			      struct sof_ipc_panic_info *panic_info,
126 			      void *stack, size_t stack_words)
127 {
128 	u32 code;
129 	int i;
130 
131 	/* is firmware dead ? */
132 	if ((panic_code & SOF_IPC_PANIC_MAGIC_MASK) != SOF_IPC_PANIC_MAGIC) {
133 		dev_printk(level, sdev->dev, "unexpected fault %#010x trace %#010x\n",
134 			   panic_code, tracep_code);
135 		return; /* no fault ? */
136 	}
137 
138 	code = panic_code & (SOF_IPC_PANIC_MAGIC_MASK | SOF_IPC_PANIC_CODE_MASK);
139 
140 	for (i = 0; i < ARRAY_SIZE(panic_msg); i++) {
141 		if (panic_msg[i].id == code) {
142 			dev_printk(level, sdev->dev, "reason: %s (%#x)\n",
143 				   panic_msg[i].msg, code & SOF_IPC_PANIC_CODE_MASK);
144 			dev_printk(level, sdev->dev, "trace point: %#010x\n", tracep_code);
145 			goto out;
146 		}
147 	}
148 
149 	/* unknown error */
150 	dev_printk(level, sdev->dev, "unknown panic code: %#x\n",
151 		   code & SOF_IPC_PANIC_CODE_MASK);
152 	dev_printk(level, sdev->dev, "trace point: %#010x\n", tracep_code);
153 
154 out:
155 	dev_printk(level, sdev->dev, "panic at %s:%d\n", panic_info->filename,
156 		   panic_info->linenum);
157 	sof_oops(sdev, level, oops);
158 	sof_stack(sdev, level, oops, stack, stack_words);
159 }
160 EXPORT_SYMBOL(sof_print_oops_and_stack);
161 
162 /* Helper to manage DSP state */
163 void sof_set_fw_state(struct snd_sof_dev *sdev, enum sof_fw_state new_state)
164 {
165 	if (sdev->fw_state == new_state)
166 		return;
167 
168 	dev_dbg(sdev->dev, "fw_state change: %d -> %d\n", sdev->fw_state, new_state);
169 	sdev->fw_state = new_state;
170 
171 	switch (new_state) {
172 	case SOF_FW_BOOT_NOT_STARTED:
173 	case SOF_FW_BOOT_COMPLETE:
174 	case SOF_FW_CRASHED:
175 		sof_client_fw_state_dispatcher(sdev);
176 		fallthrough;
177 	default:
178 		break;
179 	}
180 }
181 EXPORT_SYMBOL(sof_set_fw_state);
182 
183 static struct snd_sof_of_mach *sof_of_machine_select(struct snd_sof_dev *sdev)
184 {
185 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
186 	const struct sof_dev_desc *desc = sof_pdata->desc;
187 	struct snd_sof_of_mach *mach = desc->of_machines;
188 
189 	if (!mach)
190 		return NULL;
191 
192 	for (; mach->compatible; mach++) {
193 		if (of_machine_is_compatible(mach->compatible)) {
194 			sof_pdata->tplg_filename = mach->sof_tplg_filename;
195 			if (mach->fw_filename)
196 				sof_pdata->fw_filename = mach->fw_filename;
197 
198 			return mach;
199 		}
200 	}
201 
202 	return NULL;
203 }
204 
205 /* SOF Driver enumeration */
206 static int sof_machine_check(struct snd_sof_dev *sdev)
207 {
208 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
209 	const struct sof_dev_desc *desc = sof_pdata->desc;
210 	struct snd_soc_acpi_mach *mach;
211 
212 	if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) {
213 		const struct snd_sof_of_mach *of_mach;
214 
215 		if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
216 		    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
217 			goto nocodec;
218 
219 		/* find machine */
220 		mach = snd_sof_machine_select(sdev);
221 		if (mach) {
222 			sof_pdata->machine = mach;
223 
224 			if (sof_pdata->subsystem_id_set) {
225 				mach->mach_params.subsystem_vendor = sof_pdata->subsystem_vendor;
226 				mach->mach_params.subsystem_device = sof_pdata->subsystem_device;
227 				mach->mach_params.subsystem_id_set = true;
228 			}
229 
230 			snd_sof_set_mach_params(mach, sdev);
231 			return 0;
232 		}
233 
234 		of_mach = sof_of_machine_select(sdev);
235 		if (of_mach) {
236 			sof_pdata->of_machine = of_mach;
237 			return 0;
238 		}
239 
240 		if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) {
241 			dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n");
242 			return -ENODEV;
243 		}
244 	} else {
245 		dev_warn(sdev->dev, "Force to use nocodec mode\n");
246 	}
247 
248 nocodec:
249 	/* select nocodec mode */
250 	dev_warn(sdev->dev, "Using nocodec machine driver\n");
251 	mach = devm_kzalloc_obj(sdev->dev, *mach);
252 	if (!mach)
253 		return -ENOMEM;
254 
255 	mach->drv_name = "sof-nocodec";
256 	if (!sof_pdata->tplg_filename)
257 		sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
258 
259 	sof_pdata->machine = mach;
260 	snd_sof_set_mach_params(mach, sdev);
261 
262 	return 0;
263 }
264 
265 static int sof_select_ipc_and_paths(struct snd_sof_dev *sdev)
266 {
267 	struct snd_sof_pdata *plat_data = sdev->pdata;
268 	struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base;
269 	struct sof_loadable_file_profile out_profile;
270 	struct device *dev = sdev->dev;
271 	int ret;
272 
273 	if (base_profile->ipc_type != plat_data->desc->ipc_default)
274 		dev_info(dev,
275 			 "Module parameter used, overriding default IPC %d to %d\n",
276 			 plat_data->desc->ipc_default, base_profile->ipc_type);
277 
278 	if (base_profile->fw_path)
279 		dev_dbg(dev, "Module parameter used, changed fw path to %s\n",
280 			base_profile->fw_path);
281 	else if (base_profile->fw_path_postfix)
282 		dev_dbg(dev, "Path postfix appended to default fw path: %s\n",
283 			base_profile->fw_path_postfix);
284 
285 	if (base_profile->fw_lib_path)
286 		dev_dbg(dev, "Module parameter used, changed fw_lib path to %s\n",
287 			base_profile->fw_lib_path);
288 	else if (base_profile->fw_lib_path_postfix)
289 		dev_dbg(dev, "Path postfix appended to default fw_lib path: %s\n",
290 			base_profile->fw_lib_path_postfix);
291 
292 	if (base_profile->fw_name)
293 		dev_dbg(dev, "Module parameter used, changed fw filename to %s\n",
294 			base_profile->fw_name);
295 
296 	if (base_profile->tplg_path)
297 		dev_dbg(dev, "Module parameter used, changed tplg path to %s\n",
298 			base_profile->tplg_path);
299 
300 	if (base_profile->tplg_name)
301 		dev_dbg(dev, "Module parameter used, changed tplg name to %s\n",
302 			base_profile->tplg_name);
303 
304 	ret = sof_create_ipc_file_profile(sdev, base_profile, &out_profile);
305 	if (ret)
306 		return ret;
307 
308 	plat_data->ipc_type = out_profile.ipc_type;
309 	plat_data->fw_filename = out_profile.fw_name;
310 	plat_data->fw_filename_prefix = out_profile.fw_path;
311 	plat_data->fw_lib_prefix = out_profile.fw_lib_path;
312 	plat_data->tplg_filename_prefix = out_profile.tplg_path;
313 
314 	return 0;
315 }
316 
317 static int validate_sof_ops(struct snd_sof_dev *sdev)
318 {
319 	int ret;
320 
321 	/* init ops, if necessary */
322 	ret = sof_ops_init(sdev);
323 	if (ret < 0)
324 		return ret;
325 
326 	/* check all mandatory ops */
327 	if (!sof_ops(sdev) || !sof_ops(sdev)->probe) {
328 		dev_err(sdev->dev, "missing mandatory ops\n");
329 		sof_ops_free(sdev);
330 		return -EINVAL;
331 	}
332 
333 	if (!sdev->dspless_mode_selected &&
334 	    (!sof_ops(sdev)->run || !sof_ops(sdev)->block_read ||
335 	     !sof_ops(sdev)->block_write || !sof_ops(sdev)->send_msg ||
336 	     !sof_ops(sdev)->load_firmware || !sof_ops(sdev)->ipc_msg_data)) {
337 		dev_err(sdev->dev, "missing mandatory DSP ops\n");
338 		sof_ops_free(sdev);
339 		return -EINVAL;
340 	}
341 
342 	return 0;
343 }
344 
345 static int sof_init_sof_ops(struct snd_sof_dev *sdev)
346 {
347 	struct snd_sof_pdata *plat_data = sdev->pdata;
348 	struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base;
349 
350 	/* check IPC support */
351 	if (!(BIT(base_profile->ipc_type) & plat_data->desc->ipc_supported_mask)) {
352 		dev_err(sdev->dev,
353 			"ipc_type %d is not supported on this platform, mask is %#x\n",
354 			base_profile->ipc_type, plat_data->desc->ipc_supported_mask);
355 		return -EINVAL;
356 	}
357 
358 	/*
359 	 * Save the selected IPC type and a topology name override before
360 	 * selecting ops since platform code might need this information
361 	 */
362 	plat_data->ipc_type = base_profile->ipc_type;
363 	plat_data->tplg_filename = base_profile->tplg_name;
364 
365 	return validate_sof_ops(sdev);
366 }
367 
368 static int sof_init_environment(struct snd_sof_dev *sdev)
369 {
370 	struct snd_sof_pdata *plat_data = sdev->pdata;
371 	struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base;
372 	int ret;
373 
374 	/* probe the DSP hardware */
375 	ret = snd_sof_probe(sdev);
376 	if (ret < 0) {
377 		dev_err(sdev->dev, "failed to probe DSP %d\n", ret);
378 		goto err_sof_probe;
379 	}
380 
381 	/* check machine info */
382 	ret = sof_machine_check(sdev);
383 	if (ret < 0) {
384 		dev_err(sdev->dev, "failed to get machine info %d\n", ret);
385 		goto err_machine_check;
386 	}
387 
388 	ret = sof_select_ipc_and_paths(sdev);
389 	if (ret) {
390 		goto err_machine_check;
391 	} else if (plat_data->ipc_type != base_profile->ipc_type) {
392 		/* IPC type changed, re-initialize the ops */
393 		sof_ops_free(sdev);
394 
395 		ret = validate_sof_ops(sdev);
396 		if (ret < 0) {
397 			snd_sof_remove(sdev);
398 			snd_sof_remove_late(sdev);
399 			return ret;
400 		}
401 	}
402 
403 	return 0;
404 
405 err_machine_check:
406 	snd_sof_remove(sdev);
407 err_sof_probe:
408 	snd_sof_remove_late(sdev);
409 	sof_ops_free(sdev);
410 
411 	return ret;
412 }
413 
414 /*
415  *			FW Boot State Transition Diagram
416  *
417  *    +----------------------------------------------------------------------+
418  *    |									     |
419  * ------------------	     ------------------				     |
420  * |		    |	     |		      |				     |
421  * |   BOOT_FAILED  |<-------|  READY_FAILED  |				     |
422  * |		    |<--+    |	              |	   ------------------	     |
423  * ------------------	|    ------------------	   |		    |	     |
424  *	^		|	    ^		   |	CRASHED	    |---+    |
425  *	|		|	    |		   |		    |	|    |
426  * (FW Boot Timeout)	|	(FW_READY FAIL)	   ------------------	|    |
427  *	|		|	    |		     ^			|    |
428  *	|		|	    |		     |(DSP Panic)	|    |
429  * ------------------	|	    |		   ------------------	|    |
430  * |		    |	|	    |		   |		    |	|    |
431  * |   IN_PROGRESS  |---------------+------------->|    COMPLETE    |	|    |
432  * |		    | (FW Boot OK)   (FW_READY OK) |		    |	|    |
433  * ------------------	|			   ------------------	|    |
434  *	^		|				|		|    |
435  *	|		|				|		|    |
436  * (FW Loading OK)	|			(System Suspend/Runtime Suspend)
437  *	|		|				|		|    |
438  *	|	(FW Loading Fail)			|		|    |
439  * ------------------	|	------------------	|		|    |
440  * |		    |	|	|		 |<-----+		|    |
441  * |   PREPARE	    |---+	|   NOT_STARTED  |<---------------------+    |
442  * |		    |		|		 |<--------------------------+
443  * ------------------		------------------
444  *    |	    ^			    |	   ^
445  *    |	    |			    |	   |
446  *    |	    +-----------------------+	   |
447  *    |		(DSP Probe OK)		   |
448  *    |					   |
449  *    |					   |
450  *    +------------------------------------+
451  *	(System Suspend/Runtime Suspend)
452  */
453 
454 static int sof_probe_continue(struct snd_sof_dev *sdev)
455 {
456 	struct snd_sof_pdata *plat_data = sdev->pdata;
457 	int ret;
458 
459 	/* Initialize loadable file paths and check the environment validity */
460 	ret = sof_init_environment(sdev);
461 	if (ret)
462 		return ret;
463 
464 	sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
465 
466 	if (sdev->dspless_mode_selected) {
467 		sof_set_fw_state(sdev, SOF_DSPLESS_MODE);
468 
469 		/* set up platform component driver */
470 		snd_sof_new_platform_drv(sdev);
471 
472 		goto skip_dsp_init;
473 	}
474 
475 	/* register any debug/trace capabilities */
476 	ret = snd_sof_dbg_init(sdev);
477 	if (ret < 0) {
478 		/*
479 		 * debugfs issues are suppressed in snd_sof_dbg_init() since
480 		 * we cannot rely on debugfs
481 		 * here we trap errors due to memory allocation only.
482 		 */
483 		dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
484 			ret);
485 		goto dbg_err;
486 	}
487 
488 	/* init the IPC */
489 	sdev->ipc = snd_sof_ipc_init(sdev);
490 	if (!sdev->ipc) {
491 		ret = -ENOMEM;
492 		dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret);
493 		goto ipc_err;
494 	}
495 
496 	/* set up platform component driver after initializing the IPC ops */
497 	snd_sof_new_platform_drv(sdev);
498 
499 	/* load the firmware */
500 	ret = snd_sof_load_firmware(sdev);
501 	if (ret < 0) {
502 		dev_err(sdev->dev, "error: failed to load DSP firmware %d\n",
503 			ret);
504 		sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
505 		goto fw_load_err;
506 	}
507 
508 	sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
509 
510 	/*
511 	 * Boot the firmware. The FW boot status will be modified
512 	 * in snd_sof_run_firmware() depending on the outcome.
513 	 */
514 	ret = snd_sof_run_firmware(sdev);
515 	if (ret < 0) {
516 		dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n",
517 			ret);
518 		sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
519 		goto fw_run_err;
520 	}
521 
522 	if (sof_debug_check_flag(SOF_DBG_ENABLE_TRACE)) {
523 		sdev->fw_trace_is_supported = true;
524 
525 		/* init firmware tracing */
526 		ret = sof_fw_trace_init(sdev);
527 		if (ret < 0) {
528 			/* non fatal */
529 			dev_warn(sdev->dev, "failed to initialize firmware tracing %d\n",
530 				 ret);
531 		}
532 	} else {
533 		dev_dbg(sdev->dev, "SOF firmware trace disabled\n");
534 	}
535 
536 skip_dsp_init:
537 	/* hereafter all FW boot flows are for PM reasons */
538 	sdev->first_boot = false;
539 
540 	/* now register audio DSP platform driver and dai */
541 	ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv,
542 					      sof_ops(sdev)->drv,
543 					      sof_ops(sdev)->num_drv);
544 	if (ret < 0) {
545 		dev_err(sdev->dev,
546 			"error: failed to register DSP DAI driver %d\n", ret);
547 		goto fw_trace_err;
548 	}
549 
550 	ret = snd_sof_machine_register(sdev, plat_data);
551 	if (ret < 0) {
552 		dev_err(sdev->dev,
553 			"error: failed to register machine driver %d\n", ret);
554 		goto fw_trace_err;
555 	}
556 
557 	ret = sof_register_clients(sdev);
558 	if (ret < 0) {
559 		dev_err(sdev->dev, "failed to register clients %d\n", ret);
560 		goto sof_machine_err;
561 	}
562 
563 	/*
564 	 * Some platforms in SOF, ex: BYT, may not have their platform PM
565 	 * callbacks set. Increment the usage count so as to
566 	 * prevent the device from entering runtime suspend.
567 	 */
568 	if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
569 		pm_runtime_get_noresume(sdev->dev);
570 
571 	if (plat_data->sof_probe_complete)
572 		plat_data->sof_probe_complete(sdev->dev);
573 
574 	sdev->probe_completed = true;
575 
576 	return 0;
577 
578 sof_machine_err:
579 	snd_sof_machine_unregister(sdev, plat_data);
580 fw_trace_err:
581 	sof_fw_trace_free(sdev);
582 fw_run_err:
583 	snd_sof_fw_unload(sdev);
584 fw_load_err:
585 	snd_sof_ipc_free(sdev);
586 ipc_err:
587 dbg_err:
588 	snd_sof_free_debug(sdev);
589 	snd_sof_remove(sdev);
590 	snd_sof_remove_late(sdev);
591 	sof_ops_free(sdev);
592 
593 	/* all resources freed, update state to match */
594 	sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
595 	sdev->first_boot = true;
596 
597 	return ret;
598 }
599 
600 static void sof_probe_work(struct work_struct *work)
601 {
602 	struct snd_sof_dev *sdev =
603 		container_of(work, struct snd_sof_dev, probe_work);
604 	int ret;
605 
606 	ret = sof_probe_continue(sdev);
607 	if (ret < 0) {
608 		/* errors cannot be propagated, log */
609 		dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret);
610 	}
611 }
612 
613 static void
614 sof_apply_profile_override(struct sof_loadable_file_profile *path_override,
615 			   struct snd_sof_pdata *plat_data)
616 {
617 	if (override_ipc_type >= 0 && override_ipc_type < SOF_IPC_TYPE_COUNT)
618 		path_override->ipc_type = override_ipc_type;
619 	if (override_fw_path)
620 		path_override->fw_path = override_fw_path;
621 	if (override_fw_filename)
622 		path_override->fw_name = override_fw_filename;
623 	if (override_lib_path)
624 		path_override->fw_lib_path = override_lib_path;
625 	if (override_tplg_path)
626 		path_override->tplg_path = override_tplg_path;
627 	if (override_tplg_filename) {
628 		path_override->tplg_name = override_tplg_filename;
629 		/* User requested a specific topology file and expect it to be loaded */
630 		plat_data->disable_function_topology = true;
631 	}
632 }
633 
634 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data)
635 {
636 	struct snd_sof_dev *sdev;
637 	int ret;
638 
639 	sdev = devm_kzalloc_obj(dev, *sdev);
640 	if (!sdev)
641 		return -ENOMEM;
642 
643 	/* initialize sof device */
644 	sdev->dev = dev;
645 
646 	/* initialize default DSP power state */
647 	sdev->dsp_power_state.state = SOF_DSP_PM_D0;
648 
649 	sdev->pdata = plat_data;
650 	sdev->first_boot = true;
651 	dev_set_drvdata(dev, sdev);
652 
653 	if (sof_core_debug)
654 		dev_info(dev, "sof_debug value: %#x\n", sof_core_debug);
655 
656 	if (sof_debug_check_flag(SOF_DBG_DSPLESS_MODE)) {
657 		if (plat_data->desc->dspless_mode_supported) {
658 			dev_info(dev, "Switching to DSPless mode\n");
659 			sdev->dspless_mode_selected = true;
660 		} else {
661 			dev_info(dev, "DSPless mode is not supported by the platform\n");
662 		}
663 	}
664 
665 	sof_apply_profile_override(&plat_data->ipc_file_profile_base, plat_data);
666 
667 	/* Initialize sof_ops based on the initial selected IPC version */
668 	ret = sof_init_sof_ops(sdev);
669 	if (ret)
670 		return ret;
671 
672 	INIT_LIST_HEAD(&sdev->pcm_list);
673 	INIT_LIST_HEAD(&sdev->kcontrol_list);
674 	INIT_LIST_HEAD(&sdev->widget_list);
675 	INIT_LIST_HEAD(&sdev->pipeline_list);
676 	INIT_LIST_HEAD(&sdev->dai_list);
677 	INIT_LIST_HEAD(&sdev->dai_link_list);
678 	INIT_LIST_HEAD(&sdev->route_list);
679 	INIT_LIST_HEAD(&sdev->ipc_client_list);
680 	INIT_LIST_HEAD(&sdev->ipc_rx_handler_list);
681 	INIT_LIST_HEAD(&sdev->fw_state_handler_list);
682 	spin_lock_init(&sdev->ipc_lock);
683 	spin_lock_init(&sdev->hw_lock);
684 	mutex_init(&sdev->power_state_access);
685 	mutex_init(&sdev->ipc_client_mutex);
686 	mutex_init(&sdev->client_event_handler_mutex);
687 	mutex_init(&sdev->dsp_fw_boot_mutex);
688 
689 	/* set default timeouts if none provided */
690 	if (plat_data->desc->ipc_timeout == 0)
691 		sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS;
692 	else
693 		sdev->ipc_timeout = plat_data->desc->ipc_timeout;
694 	if (plat_data->desc->boot_timeout == 0)
695 		sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS;
696 	else
697 		sdev->boot_timeout = plat_data->desc->boot_timeout;
698 
699 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
700 	/* Override the timeout values with module parameter, if set */
701 	if (sof_ipc_timeout_ms)
702 		sdev->ipc_timeout = sof_ipc_timeout_ms;
703 
704 	if (sof_boot_timeout_ms)
705 		sdev->boot_timeout = sof_boot_timeout_ms;
706 #endif
707 
708 	sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
709 
710 	/*
711 	 * first pass of probe which isn't allowed to run in a work-queue,
712 	 * typically to rely on -EPROBE_DEFER dependencies
713 	 */
714 	ret = snd_sof_probe_early(sdev);
715 	if (ret < 0)
716 		return ret;
717 
718 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) {
719 		INIT_WORK(&sdev->probe_work, sof_probe_work);
720 		schedule_work(&sdev->probe_work);
721 		return 0;
722 	}
723 
724 	return sof_probe_continue(sdev);
725 }
726 EXPORT_SYMBOL(snd_sof_device_probe);
727 
728 bool snd_sof_device_probe_completed(struct device *dev)
729 {
730 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
731 
732 	return sdev->probe_completed;
733 }
734 EXPORT_SYMBOL(snd_sof_device_probe_completed);
735 
736 int snd_sof_device_remove(struct device *dev)
737 {
738 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
739 	struct snd_sof_pdata *pdata = sdev->pdata;
740 	int ret;
741 	bool aborted = false;
742 
743 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
744 		aborted = cancel_work_sync(&sdev->probe_work);
745 
746 	/*
747 	 * Unregister any registered client device first before IPC and debugfs
748 	 * to allow client drivers to be removed cleanly
749 	 */
750 	sof_unregister_clients(sdev);
751 
752 	/*
753 	 * Unregister machine driver. This will unbind the snd_card which
754 	 * will remove the component driver and unload the topology
755 	 * before freeing the snd_card.
756 	 */
757 	snd_sof_machine_unregister(sdev, pdata);
758 
759 	/*
760 	 * Balance the runtime pm usage count in case we are faced with an
761 	 * exception and we forcably prevented D3 power state to preserve
762 	 * context
763 	 */
764 	if (sdev->d3_prevented) {
765 		sdev->d3_prevented = false;
766 		pm_runtime_put_noidle(sdev->dev);
767 	}
768 
769 	if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) {
770 		sof_fw_trace_free(sdev);
771 		ret = snd_sof_dsp_power_down_notify(sdev);
772 		if (ret < 0)
773 			dev_warn(dev, "error: %d failed to prepare DSP for device removal",
774 				 ret);
775 
776 		snd_sof_ipc_free(sdev);
777 		snd_sof_free_debug(sdev);
778 		snd_sof_remove(sdev);
779 		snd_sof_remove_late(sdev);
780 		sof_ops_free(sdev);
781 	} else if (aborted) {
782 		/* probe_work never ran */
783 		snd_sof_remove_late(sdev);
784 		sof_ops_free(sdev);
785 	}
786 
787 	/* release firmware */
788 	snd_sof_fw_unload(sdev);
789 
790 	return 0;
791 }
792 EXPORT_SYMBOL(snd_sof_device_remove);
793 
794 int snd_sof_device_shutdown(struct device *dev)
795 {
796 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
797 
798 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
799 		cancel_work_sync(&sdev->probe_work);
800 
801 	if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) {
802 		sof_fw_trace_free(sdev);
803 		return snd_sof_shutdown(sdev);
804 	}
805 
806 	return 0;
807 }
808 EXPORT_SYMBOL(snd_sof_device_shutdown);
809 
810 /* Machine driver registering and unregistering */
811 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
812 {
813 	struct snd_sof_pdata *plat_data = pdata;
814 	const char *drv_name;
815 	const void *mach;
816 	int size;
817 
818 	drv_name = plat_data->machine->drv_name;
819 	mach = plat_data->machine;
820 	size = sizeof(*plat_data->machine);
821 
822 	/* register machine driver, pass machine info as pdata */
823 	plat_data->pdev_mach =
824 		platform_device_register_data(sdev->dev, drv_name,
825 					      PLATFORM_DEVID_NONE, mach, size);
826 	if (IS_ERR(plat_data->pdev_mach))
827 		return PTR_ERR(plat_data->pdev_mach);
828 
829 	dev_dbg(sdev->dev, "created machine %s\n",
830 		dev_name(&plat_data->pdev_mach->dev));
831 
832 	return 0;
833 }
834 EXPORT_SYMBOL(sof_machine_register);
835 
836 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
837 {
838 	struct snd_sof_pdata *plat_data = pdata;
839 
840 	platform_device_unregister(plat_data->pdev_mach);
841 }
842 EXPORT_SYMBOL(sof_machine_unregister);
843 
844 MODULE_AUTHOR("Liam Girdwood");
845 MODULE_LICENSE("Dual BSD/GPL");
846 MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core");
847 MODULE_ALIAS("platform:sof-audio");
848 MODULE_IMPORT_NS("SND_SOC_SOF_CLIENT");
849