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>
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/sof.h>
21 /* see SOF_DBG_ flags */
22 static int sof_core_debug = IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_FIRMWARE_TRACE);
23 module_param_named(sof_debug, sof_core_debug, int, 0444);
24 MODULE_PARM_DESC(sof_debug, "SOF core debug options (0x0 all off)");
26 /* SOF defaults if not provided by the platform in ms */
27 #define TIMEOUT_DEFAULT_IPC_MS 500
28 #define TIMEOUT_DEFAULT_BOOT_MS 2000
31 * sof_debug_check_flag - check if a given flag(s) is set in sof_core_debug
32 * @mask: Flag or combination of flags to check
34 * Returns true if all bits set in mask is also set in sof_core_debug, otherwise
37 bool sof_debug_check_flag(int mask)
39 if ((sof_core_debug & mask) == mask)
44 EXPORT_SYMBOL(sof_debug_check_flag);
47 * FW Panic/fault handling.
50 struct sof_panic_msg {
55 /* standard FW panic types */
56 static const struct sof_panic_msg panic_msg[] = {
57 {SOF_IPC_PANIC_MEM, "out of memory"},
58 {SOF_IPC_PANIC_WORK, "work subsystem init failed"},
59 {SOF_IPC_PANIC_IPC, "IPC subsystem init failed"},
60 {SOF_IPC_PANIC_ARCH, "arch init failed"},
61 {SOF_IPC_PANIC_PLATFORM, "platform init failed"},
62 {SOF_IPC_PANIC_TASK, "scheduler init failed"},
63 {SOF_IPC_PANIC_EXCEPTION, "runtime exception"},
64 {SOF_IPC_PANIC_DEADLOCK, "deadlock"},
65 {SOF_IPC_PANIC_STACK, "stack overflow"},
66 {SOF_IPC_PANIC_IDLE, "can't enter idle"},
67 {SOF_IPC_PANIC_WFI, "invalid wait state"},
68 {SOF_IPC_PANIC_ASSERT, "assertion failed"},
72 * sof_print_oops_and_stack - Handle the printing of DSP oops and stack trace
73 * @sdev: Pointer to the device's sdev
74 * @level: prink log level to use for the printing
75 * @panic_code: the panic code
76 * @tracep_code: tracepoint code
77 * @oops: Pointer to DSP specific oops data
78 * @panic_info: Pointer to the received panic information message
79 * @stack: Pointer to the call stack data
80 * @stack_words: Number of words in the stack data
82 * helper to be called from .dbg_dump callbacks. No error code is
83 * provided, it's left as an exercise for the caller of .dbg_dump
84 * (typically IPC or loader)
86 void sof_print_oops_and_stack(struct snd_sof_dev *sdev, const char *level,
87 u32 panic_code, u32 tracep_code, void *oops,
88 struct sof_ipc_panic_info *panic_info,
89 void *stack, size_t stack_words)
94 /* is firmware dead ? */
95 if ((panic_code & SOF_IPC_PANIC_MAGIC_MASK) != SOF_IPC_PANIC_MAGIC) {
96 dev_printk(level, sdev->dev, "unexpected fault %#010x trace %#010x\n",
97 panic_code, tracep_code);
98 return; /* no fault ? */
101 code = panic_code & (SOF_IPC_PANIC_MAGIC_MASK | SOF_IPC_PANIC_CODE_MASK);
103 for (i = 0; i < ARRAY_SIZE(panic_msg); i++) {
104 if (panic_msg[i].id == code) {
105 dev_printk(level, sdev->dev, "reason: %s (%#x)\n",
106 panic_msg[i].msg, code & SOF_IPC_PANIC_CODE_MASK);
107 dev_printk(level, sdev->dev, "trace point: %#010x\n", tracep_code);
113 dev_printk(level, sdev->dev, "unknown panic code: %#x\n",
114 code & SOF_IPC_PANIC_CODE_MASK);
115 dev_printk(level, sdev->dev, "trace point: %#010x\n", tracep_code);
118 dev_printk(level, sdev->dev, "panic at %s:%d\n", panic_info->filename,
119 panic_info->linenum);
120 sof_oops(sdev, level, oops);
121 sof_stack(sdev, level, oops, stack, stack_words);
123 EXPORT_SYMBOL(sof_print_oops_and_stack);
125 /* Helper to manage DSP state */
126 void sof_set_fw_state(struct snd_sof_dev *sdev, enum sof_fw_state new_state)
128 if (sdev->fw_state == new_state)
131 dev_dbg(sdev->dev, "fw_state change: %d -> %d\n", sdev->fw_state, new_state);
132 sdev->fw_state = new_state;
135 case SOF_FW_BOOT_NOT_STARTED:
136 case SOF_FW_BOOT_COMPLETE:
138 sof_client_fw_state_dispatcher(sdev);
144 EXPORT_SYMBOL(sof_set_fw_state);
147 * FW Boot State Transition Diagram
149 * +----------------------------------------------------------------------+
151 * ------------------ ------------------ |
153 * | BOOT_FAILED |<-------| READY_FAILED | |
154 * | |<--+ | | ------------------ |
155 * ------------------ | ------------------ | | |
156 * ^ | ^ | CRASHED |---+ |
158 * (FW Boot Timeout) | (FW_READY FAIL) ------------------ | |
160 * | | | |(DSP Panic) | |
161 * ------------------ | | ------------------ | |
163 * | IN_PROGRESS |---------------+------------->| COMPLETE | | |
164 * | | (FW Boot OK) (FW_READY OK) | | | |
165 * ------------------ | ------------------ | |
168 * (FW Loading OK) | (System Suspend/Runtime Suspend)
170 * | (FW Loading Fail) | | |
171 * ------------------ | ------------------ | | |
172 * | | | | |<-----+ | |
173 * | PREPARE |---+ | NOT_STARTED |<---------------------+ |
174 * | | | |<--------------------------+
175 * ------------------ ------------------
178 * | +-----------------------+ |
182 * +------------------------------------+
183 * (System Suspend/Runtime Suspend)
186 static int sof_probe_continue(struct snd_sof_dev *sdev)
188 struct snd_sof_pdata *plat_data = sdev->pdata;
191 /* probe the DSP hardware */
192 ret = snd_sof_probe(sdev);
194 dev_err(sdev->dev, "error: failed to probe DSP %d\n", ret);
198 sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
200 /* check machine info */
201 ret = sof_machine_check(sdev);
203 dev_err(sdev->dev, "error: failed to get machine info %d\n",
208 /* set up platform component driver */
209 snd_sof_new_platform_drv(sdev);
211 /* register any debug/trace capabilities */
212 ret = snd_sof_dbg_init(sdev);
215 * debugfs issues are suppressed in snd_sof_dbg_init() since
216 * we cannot rely on debugfs
217 * here we trap errors due to memory allocation only.
219 dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
225 sdev->ipc = snd_sof_ipc_init(sdev);
228 dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret);
232 /* load the firmware */
233 ret = snd_sof_load_firmware(sdev);
235 dev_err(sdev->dev, "error: failed to load DSP firmware %d\n",
237 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
241 sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
244 * Boot the firmware. The FW boot status will be modified
245 * in snd_sof_run_firmware() depending on the outcome.
247 ret = snd_sof_run_firmware(sdev);
249 dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n",
251 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
255 if (sof_debug_check_flag(SOF_DBG_ENABLE_TRACE)) {
256 sdev->fw_trace_is_supported = true;
258 /* init firmware tracing */
259 ret = sof_fw_trace_init(sdev);
262 dev_warn(sdev->dev, "failed to initialize firmware tracing %d\n",
266 dev_dbg(sdev->dev, "SOF firmware trace disabled\n");
269 /* hereafter all FW boot flows are for PM reasons */
270 sdev->first_boot = false;
272 /* now register audio DSP platform driver and dai */
273 ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv,
275 sof_ops(sdev)->num_drv);
278 "error: failed to register DSP DAI driver %d\n", ret);
282 ret = snd_sof_machine_register(sdev, plat_data);
285 "error: failed to register machine driver %d\n", ret);
289 ret = sof_register_clients(sdev);
291 dev_err(sdev->dev, "failed to register clients %d\n", ret);
292 goto sof_machine_err;
296 * Some platforms in SOF, ex: BYT, may not have their platform PM
297 * callbacks set. Increment the usage count so as to
298 * prevent the device from entering runtime suspend.
300 if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
301 pm_runtime_get_noresume(sdev->dev);
303 if (plat_data->sof_probe_complete)
304 plat_data->sof_probe_complete(sdev->dev);
306 sdev->probe_completed = true;
311 snd_sof_machine_unregister(sdev, plat_data);
313 sof_fw_trace_free(sdev);
315 snd_sof_fw_unload(sdev);
317 snd_sof_ipc_free(sdev);
320 snd_sof_free_debug(sdev);
322 snd_sof_remove(sdev);
326 /* all resources freed, update state to match */
327 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
328 sdev->first_boot = true;
333 static void sof_probe_work(struct work_struct *work)
335 struct snd_sof_dev *sdev =
336 container_of(work, struct snd_sof_dev, probe_work);
339 ret = sof_probe_continue(sdev);
341 /* errors cannot be propagated, log */
342 dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret);
346 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data)
348 struct snd_sof_dev *sdev;
351 sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
355 /* initialize sof device */
358 /* initialize default DSP power state */
359 sdev->dsp_power_state.state = SOF_DSP_PM_D0;
361 sdev->pdata = plat_data;
362 sdev->first_boot = true;
363 dev_set_drvdata(dev, sdev);
365 /* check IPC support */
366 if (!(BIT(plat_data->ipc_type) & plat_data->desc->ipc_supported_mask)) {
367 dev_err(dev, "ipc_type %d is not supported on this platform, mask is %#x\n",
368 plat_data->ipc_type, plat_data->desc->ipc_supported_mask);
372 /* init ops, if necessary */
373 ret = sof_ops_init(sdev);
377 /* check all mandatory ops */
378 if (!sof_ops(sdev) || !sof_ops(sdev)->probe || !sof_ops(sdev)->run ||
379 !sof_ops(sdev)->block_read || !sof_ops(sdev)->block_write ||
380 !sof_ops(sdev)->send_msg || !sof_ops(sdev)->load_firmware ||
381 !sof_ops(sdev)->ipc_msg_data) {
383 dev_err(dev, "error: missing mandatory ops\n");
387 INIT_LIST_HEAD(&sdev->pcm_list);
388 INIT_LIST_HEAD(&sdev->kcontrol_list);
389 INIT_LIST_HEAD(&sdev->widget_list);
390 INIT_LIST_HEAD(&sdev->dai_list);
391 INIT_LIST_HEAD(&sdev->dai_link_list);
392 INIT_LIST_HEAD(&sdev->route_list);
393 INIT_LIST_HEAD(&sdev->ipc_client_list);
394 INIT_LIST_HEAD(&sdev->ipc_rx_handler_list);
395 INIT_LIST_HEAD(&sdev->fw_state_handler_list);
396 spin_lock_init(&sdev->ipc_lock);
397 spin_lock_init(&sdev->hw_lock);
398 mutex_init(&sdev->power_state_access);
399 mutex_init(&sdev->ipc_client_mutex);
400 mutex_init(&sdev->client_event_handler_mutex);
402 /* set default timeouts if none provided */
403 if (plat_data->desc->ipc_timeout == 0)
404 sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS;
406 sdev->ipc_timeout = plat_data->desc->ipc_timeout;
407 if (plat_data->desc->boot_timeout == 0)
408 sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS;
410 sdev->boot_timeout = plat_data->desc->boot_timeout;
412 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
414 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) {
415 INIT_WORK(&sdev->probe_work, sof_probe_work);
416 schedule_work(&sdev->probe_work);
420 return sof_probe_continue(sdev);
422 EXPORT_SYMBOL(snd_sof_device_probe);
424 bool snd_sof_device_probe_completed(struct device *dev)
426 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
428 return sdev->probe_completed;
430 EXPORT_SYMBOL(snd_sof_device_probe_completed);
432 int snd_sof_device_remove(struct device *dev)
434 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
435 struct snd_sof_pdata *pdata = sdev->pdata;
438 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
439 cancel_work_sync(&sdev->probe_work);
442 * Unregister any registered client device first before IPC and debugfs
443 * to allow client drivers to be removed cleanly
445 sof_unregister_clients(sdev);
448 * Unregister machine driver. This will unbind the snd_card which
449 * will remove the component driver and unload the topology
450 * before freeing the snd_card.
452 snd_sof_machine_unregister(sdev, pdata);
454 if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) {
455 sof_fw_trace_free(sdev);
456 ret = snd_sof_dsp_power_down_notify(sdev);
458 dev_warn(dev, "error: %d failed to prepare DSP for device removal",
461 snd_sof_ipc_free(sdev);
462 snd_sof_free_debug(sdev);
463 snd_sof_remove(sdev);
468 /* release firmware */
469 snd_sof_fw_unload(sdev);
473 EXPORT_SYMBOL(snd_sof_device_remove);
475 int snd_sof_device_shutdown(struct device *dev)
477 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
479 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
480 cancel_work_sync(&sdev->probe_work);
482 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE)
483 return snd_sof_shutdown(sdev);
487 EXPORT_SYMBOL(snd_sof_device_shutdown);
489 MODULE_AUTHOR("Liam Girdwood");
490 MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core");
491 MODULE_LICENSE("Dual BSD/GPL");
492 MODULE_ALIAS("platform:sof-audio");
493 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);