1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
11 #include <linux/firmware.h>
12 #include <linux/module.h>
13 #include <sound/soc.h>
14 #include <sound/sof.h>
16 #include "sof-of-dev.h"
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/sof.h>
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)");
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
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
35 * Returns true if all bits set in mask is also set in sof_core_debug, otherwise
38 bool sof_debug_check_flag(int mask)
40 if ((sof_core_debug & mask) == mask)
45 EXPORT_SYMBOL(sof_debug_check_flag);
48 * FW Panic/fault handling.
51 struct sof_panic_msg {
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"},
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
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)
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)
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 ? */
102 code = panic_code & (SOF_IPC_PANIC_MAGIC_MASK | SOF_IPC_PANIC_CODE_MASK);
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);
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);
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);
124 EXPORT_SYMBOL(sof_print_oops_and_stack);
126 /* Helper to manage DSP state */
127 void sof_set_fw_state(struct snd_sof_dev *sdev, enum sof_fw_state new_state)
129 if (sdev->fw_state == new_state)
132 dev_dbg(sdev->dev, "fw_state change: %d -> %d\n", sdev->fw_state, new_state);
133 sdev->fw_state = new_state;
136 case SOF_FW_BOOT_NOT_STARTED:
137 case SOF_FW_BOOT_COMPLETE:
139 sof_client_fw_state_dispatcher(sdev);
145 EXPORT_SYMBOL(sof_set_fw_state);
147 static struct snd_sof_of_mach *sof_of_machine_select(struct snd_sof_dev *sdev)
149 struct snd_sof_pdata *sof_pdata = sdev->pdata;
150 const struct sof_dev_desc *desc = sof_pdata->desc;
151 struct snd_sof_of_mach *mach = desc->of_machines;
156 for (; mach->compatible; mach++) {
157 if (of_machine_is_compatible(mach->compatible)) {
158 sof_pdata->tplg_filename = mach->sof_tplg_filename;
159 if (mach->fw_filename)
160 sof_pdata->fw_filename = mach->fw_filename;
169 /* SOF Driver enumeration */
170 static int sof_machine_check(struct snd_sof_dev *sdev)
172 struct snd_sof_pdata *sof_pdata = sdev->pdata;
173 const struct sof_dev_desc *desc = sof_pdata->desc;
174 struct snd_soc_acpi_mach *mach;
176 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) {
177 const struct snd_sof_of_mach *of_mach;
179 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
180 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
184 mach = snd_sof_machine_select(sdev);
186 sof_pdata->machine = mach;
188 if (sof_pdata->subsystem_id_set) {
189 mach->mach_params.subsystem_vendor = sof_pdata->subsystem_vendor;
190 mach->mach_params.subsystem_device = sof_pdata->subsystem_device;
191 mach->mach_params.subsystem_id_set = true;
194 snd_sof_set_mach_params(mach, sdev);
198 of_mach = sof_of_machine_select(sdev);
200 sof_pdata->of_machine = of_mach;
204 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) {
205 dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n");
209 dev_warn(sdev->dev, "Force to use nocodec mode\n");
213 /* select nocodec mode */
214 dev_warn(sdev->dev, "Using nocodec machine driver\n");
215 mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL);
219 mach->drv_name = "sof-nocodec";
220 if (!sof_pdata->tplg_filename)
221 sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
223 sof_pdata->machine = mach;
224 snd_sof_set_mach_params(mach, sdev);
229 static int sof_select_ipc_and_paths(struct snd_sof_dev *sdev)
231 struct snd_sof_pdata *plat_data = sdev->pdata;
232 struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base;
233 struct sof_loadable_file_profile out_profile;
234 struct device *dev = sdev->dev;
237 if (base_profile->ipc_type != plat_data->desc->ipc_default)
239 "Module parameter used, overriding default IPC %d to %d\n",
240 plat_data->desc->ipc_default, base_profile->ipc_type);
242 if (base_profile->fw_path)
243 dev_dbg(dev, "Module parameter used, changed fw path to %s\n",
244 base_profile->fw_path);
245 else if (base_profile->fw_path_postfix)
246 dev_dbg(dev, "Path postfix appended to default fw path: %s\n",
247 base_profile->fw_path_postfix);
249 if (base_profile->fw_lib_path)
250 dev_dbg(dev, "Module parameter used, changed fw_lib path to %s\n",
251 base_profile->fw_lib_path);
252 else if (base_profile->fw_lib_path_postfix)
253 dev_dbg(dev, "Path postfix appended to default fw_lib path: %s\n",
254 base_profile->fw_lib_path_postfix);
256 if (base_profile->fw_name)
257 dev_dbg(dev, "Module parameter used, changed fw filename to %s\n",
258 base_profile->fw_name);
260 if (base_profile->tplg_path)
261 dev_dbg(dev, "Module parameter used, changed tplg path to %s\n",
262 base_profile->tplg_path);
264 if (base_profile->tplg_name)
265 dev_dbg(dev, "Module parameter used, changed tplg name to %s\n",
266 base_profile->tplg_name);
268 ret = sof_create_ipc_file_profile(sdev, base_profile, &out_profile);
272 plat_data->ipc_type = out_profile.ipc_type;
273 plat_data->fw_filename = out_profile.fw_name;
274 plat_data->fw_filename_prefix = out_profile.fw_path;
275 plat_data->fw_lib_prefix = out_profile.fw_lib_path;
276 plat_data->tplg_filename_prefix = out_profile.tplg_path;
281 static int validate_sof_ops(struct snd_sof_dev *sdev)
285 /* init ops, if necessary */
286 ret = sof_ops_init(sdev);
290 /* check all mandatory ops */
291 if (!sof_ops(sdev) || !sof_ops(sdev)->probe) {
292 dev_err(sdev->dev, "missing mandatory ops\n");
297 if (!sdev->dspless_mode_selected &&
298 (!sof_ops(sdev)->run || !sof_ops(sdev)->block_read ||
299 !sof_ops(sdev)->block_write || !sof_ops(sdev)->send_msg ||
300 !sof_ops(sdev)->load_firmware || !sof_ops(sdev)->ipc_msg_data)) {
301 dev_err(sdev->dev, "missing mandatory DSP ops\n");
309 static int sof_init_sof_ops(struct snd_sof_dev *sdev)
311 struct snd_sof_pdata *plat_data = sdev->pdata;
312 struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base;
314 /* check IPC support */
315 if (!(BIT(base_profile->ipc_type) & plat_data->desc->ipc_supported_mask)) {
317 "ipc_type %d is not supported on this platform, mask is %#x\n",
318 base_profile->ipc_type, plat_data->desc->ipc_supported_mask);
323 * Save the selected IPC type and a topology name override before
324 * selecting ops since platform code might need this information
326 plat_data->ipc_type = base_profile->ipc_type;
327 plat_data->tplg_filename = base_profile->tplg_name;
329 return validate_sof_ops(sdev);
332 static int sof_init_environment(struct snd_sof_dev *sdev)
334 struct snd_sof_pdata *plat_data = sdev->pdata;
335 struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base;
338 /* probe the DSP hardware */
339 ret = snd_sof_probe(sdev);
341 dev_err(sdev->dev, "failed to probe DSP %d\n", ret);
345 /* check machine info */
346 ret = sof_machine_check(sdev);
348 dev_err(sdev->dev, "failed to get machine info %d\n", ret);
349 goto err_machine_check;
352 ret = sof_select_ipc_and_paths(sdev);
353 if (!ret && plat_data->ipc_type != base_profile->ipc_type) {
354 /* IPC type changed, re-initialize the ops */
357 ret = validate_sof_ops(sdev);
359 snd_sof_remove(sdev);
360 snd_sof_remove_late(sdev);
368 snd_sof_remove(sdev);
370 snd_sof_remove_late(sdev);
377 * FW Boot State Transition Diagram
379 * +----------------------------------------------------------------------+
381 * ------------------ ------------------ |
383 * | BOOT_FAILED |<-------| READY_FAILED | |
384 * | |<--+ | | ------------------ |
385 * ------------------ | ------------------ | | |
386 * ^ | ^ | CRASHED |---+ |
388 * (FW Boot Timeout) | (FW_READY FAIL) ------------------ | |
390 * | | | |(DSP Panic) | |
391 * ------------------ | | ------------------ | |
393 * | IN_PROGRESS |---------------+------------->| COMPLETE | | |
394 * | | (FW Boot OK) (FW_READY OK) | | | |
395 * ------------------ | ------------------ | |
398 * (FW Loading OK) | (System Suspend/Runtime Suspend)
400 * | (FW Loading Fail) | | |
401 * ------------------ | ------------------ | | |
402 * | | | | |<-----+ | |
403 * | PREPARE |---+ | NOT_STARTED |<---------------------+ |
404 * | | | |<--------------------------+
405 * ------------------ ------------------
408 * | +-----------------------+ |
412 * +------------------------------------+
413 * (System Suspend/Runtime Suspend)
416 static int sof_probe_continue(struct snd_sof_dev *sdev)
418 struct snd_sof_pdata *plat_data = sdev->pdata;
421 /* Initialize loadable file paths and check the environment validity */
422 ret = sof_init_environment(sdev);
426 sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
428 /* set up platform component driver */
429 snd_sof_new_platform_drv(sdev);
431 if (sdev->dspless_mode_selected) {
432 sof_set_fw_state(sdev, SOF_DSPLESS_MODE);
436 /* register any debug/trace capabilities */
437 ret = snd_sof_dbg_init(sdev);
440 * debugfs issues are suppressed in snd_sof_dbg_init() since
441 * we cannot rely on debugfs
442 * here we trap errors due to memory allocation only.
444 dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
450 sdev->ipc = snd_sof_ipc_init(sdev);
453 dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret);
457 /* load the firmware */
458 ret = snd_sof_load_firmware(sdev);
460 dev_err(sdev->dev, "error: failed to load DSP firmware %d\n",
462 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
466 sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
469 * Boot the firmware. The FW boot status will be modified
470 * in snd_sof_run_firmware() depending on the outcome.
472 ret = snd_sof_run_firmware(sdev);
474 dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n",
476 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
480 if (sof_debug_check_flag(SOF_DBG_ENABLE_TRACE)) {
481 sdev->fw_trace_is_supported = true;
483 /* init firmware tracing */
484 ret = sof_fw_trace_init(sdev);
487 dev_warn(sdev->dev, "failed to initialize firmware tracing %d\n",
491 dev_dbg(sdev->dev, "SOF firmware trace disabled\n");
495 /* hereafter all FW boot flows are for PM reasons */
496 sdev->first_boot = false;
498 /* now register audio DSP platform driver and dai */
499 ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv,
501 sof_ops(sdev)->num_drv);
504 "error: failed to register DSP DAI driver %d\n", ret);
508 ret = snd_sof_machine_register(sdev, plat_data);
511 "error: failed to register machine driver %d\n", ret);
515 ret = sof_register_clients(sdev);
517 dev_err(sdev->dev, "failed to register clients %d\n", ret);
518 goto sof_machine_err;
522 * Some platforms in SOF, ex: BYT, may not have their platform PM
523 * callbacks set. Increment the usage count so as to
524 * prevent the device from entering runtime suspend.
526 if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
527 pm_runtime_get_noresume(sdev->dev);
529 if (plat_data->sof_probe_complete)
530 plat_data->sof_probe_complete(sdev->dev);
532 sdev->probe_completed = true;
537 snd_sof_machine_unregister(sdev, plat_data);
539 sof_fw_trace_free(sdev);
541 snd_sof_fw_unload(sdev);
543 snd_sof_ipc_free(sdev);
546 snd_sof_free_debug(sdev);
547 snd_sof_remove(sdev);
548 snd_sof_remove_late(sdev);
551 /* all resources freed, update state to match */
552 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
553 sdev->first_boot = true;
558 static void sof_probe_work(struct work_struct *work)
560 struct snd_sof_dev *sdev =
561 container_of(work, struct snd_sof_dev, probe_work);
564 ret = sof_probe_continue(sdev);
566 /* errors cannot be propagated, log */
567 dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret);
571 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data)
573 struct snd_sof_dev *sdev;
576 sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
580 /* initialize sof device */
583 /* initialize default DSP power state */
584 sdev->dsp_power_state.state = SOF_DSP_PM_D0;
586 sdev->pdata = plat_data;
587 sdev->first_boot = true;
588 dev_set_drvdata(dev, sdev);
591 dev_info(dev, "sof_debug value: %#x\n", sof_core_debug);
593 if (sof_debug_check_flag(SOF_DBG_DSPLESS_MODE)) {
594 if (plat_data->desc->dspless_mode_supported) {
595 dev_info(dev, "Switching to DSPless mode\n");
596 sdev->dspless_mode_selected = true;
598 dev_info(dev, "DSPless mode is not supported by the platform\n");
602 /* Initialize sof_ops based on the initial selected IPC version */
603 ret = sof_init_sof_ops(sdev);
607 INIT_LIST_HEAD(&sdev->pcm_list);
608 INIT_LIST_HEAD(&sdev->kcontrol_list);
609 INIT_LIST_HEAD(&sdev->widget_list);
610 INIT_LIST_HEAD(&sdev->pipeline_list);
611 INIT_LIST_HEAD(&sdev->dai_list);
612 INIT_LIST_HEAD(&sdev->dai_link_list);
613 INIT_LIST_HEAD(&sdev->route_list);
614 INIT_LIST_HEAD(&sdev->ipc_client_list);
615 INIT_LIST_HEAD(&sdev->ipc_rx_handler_list);
616 INIT_LIST_HEAD(&sdev->fw_state_handler_list);
617 spin_lock_init(&sdev->ipc_lock);
618 spin_lock_init(&sdev->hw_lock);
619 mutex_init(&sdev->power_state_access);
620 mutex_init(&sdev->ipc_client_mutex);
621 mutex_init(&sdev->client_event_handler_mutex);
623 /* set default timeouts if none provided */
624 if (plat_data->desc->ipc_timeout == 0)
625 sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS;
627 sdev->ipc_timeout = plat_data->desc->ipc_timeout;
628 if (plat_data->desc->boot_timeout == 0)
629 sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS;
631 sdev->boot_timeout = plat_data->desc->boot_timeout;
633 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
636 * first pass of probe which isn't allowed to run in a work-queue,
637 * typically to rely on -EPROBE_DEFER dependencies
639 ret = snd_sof_probe_early(sdev);
643 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) {
644 INIT_WORK(&sdev->probe_work, sof_probe_work);
645 schedule_work(&sdev->probe_work);
649 return sof_probe_continue(sdev);
651 EXPORT_SYMBOL(snd_sof_device_probe);
653 bool snd_sof_device_probe_completed(struct device *dev)
655 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
657 return sdev->probe_completed;
659 EXPORT_SYMBOL(snd_sof_device_probe_completed);
661 int snd_sof_device_remove(struct device *dev)
663 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
664 struct snd_sof_pdata *pdata = sdev->pdata;
666 bool aborted = false;
668 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
669 aborted = cancel_work_sync(&sdev->probe_work);
672 * Unregister any registered client device first before IPC and debugfs
673 * to allow client drivers to be removed cleanly
675 sof_unregister_clients(sdev);
678 * Unregister machine driver. This will unbind the snd_card which
679 * will remove the component driver and unload the topology
680 * before freeing the snd_card.
682 snd_sof_machine_unregister(sdev, pdata);
685 * Balance the runtime pm usage count in case we are faced with an
686 * exception and we forcably prevented D3 power state to preserve
689 if (sdev->d3_prevented) {
690 sdev->d3_prevented = false;
691 pm_runtime_put_noidle(sdev->dev);
694 if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) {
695 sof_fw_trace_free(sdev);
696 ret = snd_sof_dsp_power_down_notify(sdev);
698 dev_warn(dev, "error: %d failed to prepare DSP for device removal",
701 snd_sof_ipc_free(sdev);
702 snd_sof_free_debug(sdev);
703 snd_sof_remove(sdev);
704 snd_sof_remove_late(sdev);
706 } else if (aborted) {
707 /* probe_work never ran */
708 snd_sof_remove_late(sdev);
712 /* release firmware */
713 snd_sof_fw_unload(sdev);
717 EXPORT_SYMBOL(snd_sof_device_remove);
719 int snd_sof_device_shutdown(struct device *dev)
721 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
723 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
724 cancel_work_sync(&sdev->probe_work);
726 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) {
727 sof_fw_trace_free(sdev);
728 return snd_sof_shutdown(sdev);
733 EXPORT_SYMBOL(snd_sof_device_shutdown);
735 /* Machine driver registering and unregistering */
736 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
738 struct snd_sof_pdata *plat_data = pdata;
739 const char *drv_name;
743 drv_name = plat_data->machine->drv_name;
744 mach = plat_data->machine;
745 size = sizeof(*plat_data->machine);
747 /* register machine driver, pass machine info as pdata */
748 plat_data->pdev_mach =
749 platform_device_register_data(sdev->dev, drv_name,
750 PLATFORM_DEVID_NONE, mach, size);
751 if (IS_ERR(plat_data->pdev_mach))
752 return PTR_ERR(plat_data->pdev_mach);
754 dev_dbg(sdev->dev, "created machine %s\n",
755 dev_name(&plat_data->pdev_mach->dev));
759 EXPORT_SYMBOL(sof_machine_register);
761 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
763 struct snd_sof_pdata *plat_data = pdata;
765 platform_device_unregister(plat_data->pdev_mach);
767 EXPORT_SYMBOL(sof_machine_unregister);
769 MODULE_AUTHOR("Liam Girdwood");
770 MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core");
771 MODULE_LICENSE("Dual BSD/GPL");
772 MODULE_ALIAS("platform:sof-audio");
773 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);