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