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 "ops.h" 17 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES) 18 #include "sof-probes.h" 19 #endif 20 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)"); 25 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 29 30 /** 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 33 * 34 * Returns true if all bits set in mask is also set in sof_core_debug, otherwise 35 * false 36 */ 37 bool sof_debug_check_flag(int mask) 38 { 39 if ((sof_core_debug & mask) == mask) 40 return true; 41 42 return false; 43 } 44 EXPORT_SYMBOL(sof_debug_check_flag); 45 46 /* 47 * FW Panic/fault handling. 48 */ 49 50 struct sof_panic_msg { 51 u32 id; 52 const char *msg; 53 }; 54 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"}, 69 }; 70 71 /** 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 81 * 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) 85 */ 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) 90 { 91 u32 code; 92 int i; 93 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 ? */ 99 } 100 101 code = panic_code & (SOF_IPC_PANIC_MAGIC_MASK | SOF_IPC_PANIC_CODE_MASK); 102 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); 108 goto out; 109 } 110 } 111 112 /* unknown error */ 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); 116 117 out: 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); 122 } 123 EXPORT_SYMBOL(sof_print_oops_and_stack); 124 125 /* Helper to manage DSP state */ 126 void sof_set_fw_state(struct snd_sof_dev *sdev, enum sof_fw_state new_state) 127 { 128 if (sdev->fw_state == new_state) 129 return; 130 131 dev_dbg(sdev->dev, "fw_state change: %d -> %d\n", sdev->fw_state, new_state); 132 sdev->fw_state = new_state; 133 134 switch (new_state) { 135 case SOF_FW_BOOT_NOT_STARTED: 136 case SOF_FW_BOOT_COMPLETE: 137 case SOF_FW_CRASHED: 138 sof_client_fw_state_dispatcher(sdev); 139 fallthrough; 140 default: 141 break; 142 } 143 } 144 EXPORT_SYMBOL(sof_set_fw_state); 145 146 /* 147 * FW Boot State Transition Diagram 148 * 149 * +----------------------------------------------------------------------+ 150 * | | 151 * ------------------ ------------------ | 152 * | | | | | 153 * | BOOT_FAILED |<-------| READY_FAILED | | 154 * | |<--+ | | ------------------ | 155 * ------------------ | ------------------ | | | 156 * ^ | ^ | CRASHED |---+ | 157 * | | | | | | | 158 * (FW Boot Timeout) | (FW_READY FAIL) ------------------ | | 159 * | | | ^ | | 160 * | | | |(DSP Panic) | | 161 * ------------------ | | ------------------ | | 162 * | | | | | | | | 163 * | IN_PROGRESS |---------------+------------->| COMPLETE | | | 164 * | | (FW Boot OK) (FW_READY OK) | | | | 165 * ------------------ | ------------------ | | 166 * ^ | | | | 167 * | | | | | 168 * (FW Loading OK) | (System Suspend/Runtime Suspend) 169 * | | | | | 170 * | (FW Loading Fail) | | | 171 * ------------------ | ------------------ | | | 172 * | | | | |<-----+ | | 173 * | PREPARE |---+ | NOT_STARTED |<---------------------+ | 174 * | | | |<--------------------------+ 175 * ------------------ ------------------ 176 * | ^ | ^ 177 * | | | | 178 * | +-----------------------+ | 179 * | (DSP Probe OK) | 180 * | | 181 * | | 182 * +------------------------------------+ 183 * (System Suspend/Runtime Suspend) 184 */ 185 186 static int sof_probe_continue(struct snd_sof_dev *sdev) 187 { 188 struct snd_sof_pdata *plat_data = sdev->pdata; 189 int ret; 190 191 /* probe the DSP hardware */ 192 ret = snd_sof_probe(sdev); 193 if (ret < 0) { 194 dev_err(sdev->dev, "error: failed to probe DSP %d\n", ret); 195 return ret; 196 } 197 198 sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE); 199 200 /* check machine info */ 201 ret = sof_machine_check(sdev); 202 if (ret < 0) { 203 dev_err(sdev->dev, "error: failed to get machine info %d\n", 204 ret); 205 goto dsp_err; 206 } 207 208 /* set up platform component driver */ 209 snd_sof_new_platform_drv(sdev); 210 211 /* register any debug/trace capabilities */ 212 ret = snd_sof_dbg_init(sdev); 213 if (ret < 0) { 214 /* 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. 218 */ 219 dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n", 220 ret); 221 goto dbg_err; 222 } 223 224 /* init the IPC */ 225 sdev->ipc = snd_sof_ipc_init(sdev); 226 if (!sdev->ipc) { 227 ret = -ENOMEM; 228 dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret); 229 goto ipc_err; 230 } 231 232 /* load the firmware */ 233 ret = snd_sof_load_firmware(sdev); 234 if (ret < 0) { 235 dev_err(sdev->dev, "error: failed to load DSP firmware %d\n", 236 ret); 237 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED); 238 goto fw_load_err; 239 } 240 241 sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS); 242 243 /* 244 * Boot the firmware. The FW boot status will be modified 245 * in snd_sof_run_firmware() depending on the outcome. 246 */ 247 ret = snd_sof_run_firmware(sdev); 248 if (ret < 0) { 249 dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n", 250 ret); 251 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED); 252 goto fw_run_err; 253 } 254 255 if (sof_debug_check_flag(SOF_DBG_ENABLE_TRACE)) { 256 sdev->dtrace_is_supported = true; 257 258 /* init DMA trace */ 259 ret = snd_sof_init_trace(sdev); 260 if (ret < 0) { 261 /* non fatal */ 262 dev_warn(sdev->dev, 263 "warning: failed to initialize trace %d\n", 264 ret); 265 } 266 } else { 267 dev_dbg(sdev->dev, "SOF firmware trace disabled\n"); 268 } 269 270 /* hereafter all FW boot flows are for PM reasons */ 271 sdev->first_boot = false; 272 273 /* now register audio DSP platform driver and dai */ 274 ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv, 275 sof_ops(sdev)->drv, 276 sof_ops(sdev)->num_drv); 277 if (ret < 0) { 278 dev_err(sdev->dev, 279 "error: failed to register DSP DAI driver %d\n", ret); 280 goto fw_trace_err; 281 } 282 283 ret = snd_sof_machine_register(sdev, plat_data); 284 if (ret < 0) { 285 dev_err(sdev->dev, 286 "error: failed to register machine driver %d\n", ret); 287 goto fw_trace_err; 288 } 289 290 ret = sof_register_clients(sdev); 291 if (ret < 0) { 292 dev_err(sdev->dev, "failed to register clients %d\n", ret); 293 goto sof_machine_err; 294 } 295 296 /* 297 * Some platforms in SOF, ex: BYT, may not have their platform PM 298 * callbacks set. Increment the usage count so as to 299 * prevent the device from entering runtime suspend. 300 */ 301 if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume) 302 pm_runtime_get_noresume(sdev->dev); 303 304 if (plat_data->sof_probe_complete) 305 plat_data->sof_probe_complete(sdev->dev); 306 307 sdev->probe_completed = true; 308 309 return 0; 310 311 sof_machine_err: 312 snd_sof_machine_unregister(sdev, plat_data); 313 fw_trace_err: 314 snd_sof_free_trace(sdev); 315 fw_run_err: 316 snd_sof_fw_unload(sdev); 317 fw_load_err: 318 snd_sof_ipc_free(sdev); 319 ipc_err: 320 dbg_err: 321 snd_sof_free_debug(sdev); 322 dsp_err: 323 snd_sof_remove(sdev); 324 325 /* all resources freed, update state to match */ 326 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED); 327 sdev->first_boot = true; 328 329 return ret; 330 } 331 332 static void sof_probe_work(struct work_struct *work) 333 { 334 struct snd_sof_dev *sdev = 335 container_of(work, struct snd_sof_dev, probe_work); 336 int ret; 337 338 ret = sof_probe_continue(sdev); 339 if (ret < 0) { 340 /* errors cannot be propagated, log */ 341 dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret); 342 } 343 } 344 345 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data) 346 { 347 struct snd_sof_dev *sdev; 348 349 sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL); 350 if (!sdev) 351 return -ENOMEM; 352 353 /* initialize sof device */ 354 sdev->dev = dev; 355 356 /* initialize default DSP power state */ 357 sdev->dsp_power_state.state = SOF_DSP_PM_D0; 358 359 sdev->pdata = plat_data; 360 sdev->first_boot = true; 361 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES) 362 sdev->extractor_stream_tag = SOF_PROBE_INVALID_NODE_ID; 363 #endif 364 dev_set_drvdata(dev, sdev); 365 366 /* check all mandatory ops */ 367 if (!sof_ops(sdev) || !sof_ops(sdev)->probe || !sof_ops(sdev)->run || 368 !sof_ops(sdev)->block_read || !sof_ops(sdev)->block_write || 369 !sof_ops(sdev)->send_msg || !sof_ops(sdev)->load_firmware || 370 !sof_ops(sdev)->ipc_msg_data || !sof_ops(sdev)->ipc_pcm_params || 371 !sof_ops(sdev)->fw_ready) { 372 dev_err(dev, "error: missing mandatory ops\n"); 373 return -EINVAL; 374 } 375 376 INIT_LIST_HEAD(&sdev->pcm_list); 377 INIT_LIST_HEAD(&sdev->kcontrol_list); 378 INIT_LIST_HEAD(&sdev->widget_list); 379 INIT_LIST_HEAD(&sdev->dai_list); 380 INIT_LIST_HEAD(&sdev->route_list); 381 INIT_LIST_HEAD(&sdev->ipc_client_list); 382 INIT_LIST_HEAD(&sdev->ipc_rx_handler_list); 383 INIT_LIST_HEAD(&sdev->fw_state_handler_list); 384 spin_lock_init(&sdev->ipc_lock); 385 spin_lock_init(&sdev->hw_lock); 386 mutex_init(&sdev->power_state_access); 387 mutex_init(&sdev->ipc_client_mutex); 388 mutex_init(&sdev->client_event_handler_mutex); 389 390 /* set default timeouts if none provided */ 391 if (plat_data->desc->ipc_timeout == 0) 392 sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS; 393 else 394 sdev->ipc_timeout = plat_data->desc->ipc_timeout; 395 if (plat_data->desc->boot_timeout == 0) 396 sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS; 397 else 398 sdev->boot_timeout = plat_data->desc->boot_timeout; 399 400 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED); 401 402 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) { 403 INIT_WORK(&sdev->probe_work, sof_probe_work); 404 schedule_work(&sdev->probe_work); 405 return 0; 406 } 407 408 return sof_probe_continue(sdev); 409 } 410 EXPORT_SYMBOL(snd_sof_device_probe); 411 412 bool snd_sof_device_probe_completed(struct device *dev) 413 { 414 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 415 416 return sdev->probe_completed; 417 } 418 EXPORT_SYMBOL(snd_sof_device_probe_completed); 419 420 int snd_sof_device_remove(struct device *dev) 421 { 422 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 423 struct snd_sof_pdata *pdata = sdev->pdata; 424 int ret; 425 426 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) 427 cancel_work_sync(&sdev->probe_work); 428 429 /* 430 * Unregister any registered client device first before IPC and debugfs 431 * to allow client drivers to be removed cleanly 432 */ 433 sof_unregister_clients(sdev); 434 435 /* 436 * Unregister machine driver. This will unbind the snd_card which 437 * will remove the component driver and unload the topology 438 * before freeing the snd_card. 439 */ 440 snd_sof_machine_unregister(sdev, pdata); 441 442 if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) { 443 snd_sof_free_trace(sdev); 444 ret = snd_sof_dsp_power_down_notify(sdev); 445 if (ret < 0) 446 dev_warn(dev, "error: %d failed to prepare DSP for device removal", 447 ret); 448 449 snd_sof_ipc_free(sdev); 450 snd_sof_free_debug(sdev); 451 } 452 453 /* 454 * Unregistering the machine driver results in unloading the topology. 455 * Some widgets, ex: scheduler, attempt to power down the core they are 456 * scheduled on, when they are unloaded. Therefore, the DSP must be 457 * removed only after the topology has been unloaded. 458 */ 459 if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) 460 snd_sof_remove(sdev); 461 462 /* release firmware */ 463 snd_sof_fw_unload(sdev); 464 465 return 0; 466 } 467 EXPORT_SYMBOL(snd_sof_device_remove); 468 469 int snd_sof_device_shutdown(struct device *dev) 470 { 471 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 472 473 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) 474 cancel_work_sync(&sdev->probe_work); 475 476 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) 477 return snd_sof_shutdown(sdev); 478 479 return 0; 480 } 481 EXPORT_SYMBOL(snd_sof_device_shutdown); 482 483 MODULE_AUTHOR("Liam Girdwood"); 484 MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core"); 485 MODULE_LICENSE("Dual BSD/GPL"); 486 MODULE_ALIAS("platform:sof-audio"); 487 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT); 488