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 /* 208 * FW Boot State Transition Diagram 209 * 210 * +----------------------------------------------------------------------+ 211 * | | 212 * ------------------ ------------------ | 213 * | | | | | 214 * | BOOT_FAILED |<-------| READY_FAILED | | 215 * | |<--+ | | ------------------ | 216 * ------------------ | ------------------ | | | 217 * ^ | ^ | CRASHED |---+ | 218 * | | | | | | | 219 * (FW Boot Timeout) | (FW_READY FAIL) ------------------ | | 220 * | | | ^ | | 221 * | | | |(DSP Panic) | | 222 * ------------------ | | ------------------ | | 223 * | | | | | | | | 224 * | IN_PROGRESS |---------------+------------->| COMPLETE | | | 225 * | | (FW Boot OK) (FW_READY OK) | | | | 226 * ------------------ | ------------------ | | 227 * ^ | | | | 228 * | | | | | 229 * (FW Loading OK) | (System Suspend/Runtime Suspend) 230 * | | | | | 231 * | (FW Loading Fail) | | | 232 * ------------------ | ------------------ | | | 233 * | | | | |<-----+ | | 234 * | PREPARE |---+ | NOT_STARTED |<---------------------+ | 235 * | | | |<--------------------------+ 236 * ------------------ ------------------ 237 * | ^ | ^ 238 * | | | | 239 * | +-----------------------+ | 240 * | (DSP Probe OK) | 241 * | | 242 * | | 243 * +------------------------------------+ 244 * (System Suspend/Runtime Suspend) 245 */ 246 247 static int sof_probe_continue(struct snd_sof_dev *sdev) 248 { 249 struct snd_sof_pdata *plat_data = sdev->pdata; 250 int ret; 251 252 /* probe the DSP hardware */ 253 ret = snd_sof_probe(sdev); 254 if (ret < 0) { 255 dev_err(sdev->dev, "error: failed to probe DSP %d\n", ret); 256 goto probe_err; 257 } 258 259 sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE); 260 261 /* check machine info */ 262 ret = sof_machine_check(sdev); 263 if (ret < 0) { 264 dev_err(sdev->dev, "error: failed to get machine info %d\n", 265 ret); 266 goto dsp_err; 267 } 268 269 /* set up platform component driver */ 270 snd_sof_new_platform_drv(sdev); 271 272 if (sdev->dspless_mode_selected) { 273 sof_set_fw_state(sdev, SOF_DSPLESS_MODE); 274 goto skip_dsp_init; 275 } 276 277 /* register any debug/trace capabilities */ 278 ret = snd_sof_dbg_init(sdev); 279 if (ret < 0) { 280 /* 281 * debugfs issues are suppressed in snd_sof_dbg_init() since 282 * we cannot rely on debugfs 283 * here we trap errors due to memory allocation only. 284 */ 285 dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n", 286 ret); 287 goto dbg_err; 288 } 289 290 /* init the IPC */ 291 sdev->ipc = snd_sof_ipc_init(sdev); 292 if (!sdev->ipc) { 293 ret = -ENOMEM; 294 dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret); 295 goto ipc_err; 296 } 297 298 /* load the firmware */ 299 ret = snd_sof_load_firmware(sdev); 300 if (ret < 0) { 301 dev_err(sdev->dev, "error: failed to load DSP firmware %d\n", 302 ret); 303 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED); 304 goto fw_load_err; 305 } 306 307 sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS); 308 309 /* 310 * Boot the firmware. The FW boot status will be modified 311 * in snd_sof_run_firmware() depending on the outcome. 312 */ 313 ret = snd_sof_run_firmware(sdev); 314 if (ret < 0) { 315 dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n", 316 ret); 317 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED); 318 goto fw_run_err; 319 } 320 321 if (sof_debug_check_flag(SOF_DBG_ENABLE_TRACE)) { 322 sdev->fw_trace_is_supported = true; 323 324 /* init firmware tracing */ 325 ret = sof_fw_trace_init(sdev); 326 if (ret < 0) { 327 /* non fatal */ 328 dev_warn(sdev->dev, "failed to initialize firmware tracing %d\n", 329 ret); 330 } 331 } else { 332 dev_dbg(sdev->dev, "SOF firmware trace disabled\n"); 333 } 334 335 skip_dsp_init: 336 /* hereafter all FW boot flows are for PM reasons */ 337 sdev->first_boot = false; 338 339 /* now register audio DSP platform driver and dai */ 340 ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv, 341 sof_ops(sdev)->drv, 342 sof_ops(sdev)->num_drv); 343 if (ret < 0) { 344 dev_err(sdev->dev, 345 "error: failed to register DSP DAI driver %d\n", ret); 346 goto fw_trace_err; 347 } 348 349 ret = snd_sof_machine_register(sdev, plat_data); 350 if (ret < 0) { 351 dev_err(sdev->dev, 352 "error: failed to register machine driver %d\n", ret); 353 goto fw_trace_err; 354 } 355 356 ret = sof_register_clients(sdev); 357 if (ret < 0) { 358 dev_err(sdev->dev, "failed to register clients %d\n", ret); 359 goto sof_machine_err; 360 } 361 362 /* 363 * Some platforms in SOF, ex: BYT, may not have their platform PM 364 * callbacks set. Increment the usage count so as to 365 * prevent the device from entering runtime suspend. 366 */ 367 if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume) 368 pm_runtime_get_noresume(sdev->dev); 369 370 if (plat_data->sof_probe_complete) 371 plat_data->sof_probe_complete(sdev->dev); 372 373 sdev->probe_completed = true; 374 375 return 0; 376 377 sof_machine_err: 378 snd_sof_machine_unregister(sdev, plat_data); 379 fw_trace_err: 380 sof_fw_trace_free(sdev); 381 fw_run_err: 382 snd_sof_fw_unload(sdev); 383 fw_load_err: 384 snd_sof_ipc_free(sdev); 385 ipc_err: 386 dbg_err: 387 snd_sof_free_debug(sdev); 388 dsp_err: 389 snd_sof_remove(sdev); 390 probe_err: 391 snd_sof_remove_late(sdev); 392 sof_ops_free(sdev); 393 394 /* all resources freed, update state to match */ 395 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED); 396 sdev->first_boot = true; 397 398 return ret; 399 } 400 401 static void sof_probe_work(struct work_struct *work) 402 { 403 struct snd_sof_dev *sdev = 404 container_of(work, struct snd_sof_dev, probe_work); 405 int ret; 406 407 ret = sof_probe_continue(sdev); 408 if (ret < 0) { 409 /* errors cannot be propagated, log */ 410 dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret); 411 } 412 } 413 414 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data) 415 { 416 struct snd_sof_dev *sdev; 417 int ret; 418 419 sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL); 420 if (!sdev) 421 return -ENOMEM; 422 423 /* initialize sof device */ 424 sdev->dev = dev; 425 426 /* initialize default DSP power state */ 427 sdev->dsp_power_state.state = SOF_DSP_PM_D0; 428 429 sdev->pdata = plat_data; 430 sdev->first_boot = true; 431 dev_set_drvdata(dev, sdev); 432 433 if (sof_core_debug) 434 dev_info(dev, "sof_debug value: %#x\n", sof_core_debug); 435 436 if (sof_debug_check_flag(SOF_DBG_DSPLESS_MODE)) { 437 if (plat_data->desc->dspless_mode_supported) { 438 dev_info(dev, "Switching to DSPless mode\n"); 439 sdev->dspless_mode_selected = true; 440 } else { 441 dev_info(dev, "DSPless mode is not supported by the platform\n"); 442 } 443 } 444 445 /* check IPC support */ 446 if (!(BIT(plat_data->ipc_type) & plat_data->desc->ipc_supported_mask)) { 447 dev_err(dev, "ipc_type %d is not supported on this platform, mask is %#x\n", 448 plat_data->ipc_type, plat_data->desc->ipc_supported_mask); 449 return -EINVAL; 450 } 451 452 /* init ops, if necessary */ 453 ret = sof_ops_init(sdev); 454 if (ret < 0) 455 return ret; 456 457 /* check all mandatory ops */ 458 if (!sof_ops(sdev) || !sof_ops(sdev)->probe) { 459 sof_ops_free(sdev); 460 dev_err(dev, "missing mandatory ops\n"); 461 return -EINVAL; 462 } 463 464 if (!sdev->dspless_mode_selected && 465 (!sof_ops(sdev)->run || !sof_ops(sdev)->block_read || 466 !sof_ops(sdev)->block_write || !sof_ops(sdev)->send_msg || 467 !sof_ops(sdev)->load_firmware || !sof_ops(sdev)->ipc_msg_data)) { 468 sof_ops_free(sdev); 469 dev_err(dev, "missing mandatory DSP ops\n"); 470 return -EINVAL; 471 } 472 473 INIT_LIST_HEAD(&sdev->pcm_list); 474 INIT_LIST_HEAD(&sdev->kcontrol_list); 475 INIT_LIST_HEAD(&sdev->widget_list); 476 INIT_LIST_HEAD(&sdev->pipeline_list); 477 INIT_LIST_HEAD(&sdev->dai_list); 478 INIT_LIST_HEAD(&sdev->dai_link_list); 479 INIT_LIST_HEAD(&sdev->route_list); 480 INIT_LIST_HEAD(&sdev->ipc_client_list); 481 INIT_LIST_HEAD(&sdev->ipc_rx_handler_list); 482 INIT_LIST_HEAD(&sdev->fw_state_handler_list); 483 spin_lock_init(&sdev->ipc_lock); 484 spin_lock_init(&sdev->hw_lock); 485 mutex_init(&sdev->power_state_access); 486 mutex_init(&sdev->ipc_client_mutex); 487 mutex_init(&sdev->client_event_handler_mutex); 488 489 /* set default timeouts if none provided */ 490 if (plat_data->desc->ipc_timeout == 0) 491 sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS; 492 else 493 sdev->ipc_timeout = plat_data->desc->ipc_timeout; 494 if (plat_data->desc->boot_timeout == 0) 495 sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS; 496 else 497 sdev->boot_timeout = plat_data->desc->boot_timeout; 498 499 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED); 500 501 /* 502 * first pass of probe which isn't allowed to run in a work-queue, 503 * typically to rely on -EPROBE_DEFER dependencies 504 */ 505 ret = snd_sof_probe_early(sdev); 506 if (ret < 0) 507 return ret; 508 509 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) { 510 INIT_WORK(&sdev->probe_work, sof_probe_work); 511 schedule_work(&sdev->probe_work); 512 return 0; 513 } 514 515 return sof_probe_continue(sdev); 516 } 517 EXPORT_SYMBOL(snd_sof_device_probe); 518 519 bool snd_sof_device_probe_completed(struct device *dev) 520 { 521 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 522 523 return sdev->probe_completed; 524 } 525 EXPORT_SYMBOL(snd_sof_device_probe_completed); 526 527 int snd_sof_device_remove(struct device *dev) 528 { 529 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 530 struct snd_sof_pdata *pdata = sdev->pdata; 531 int ret; 532 bool aborted = false; 533 534 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) 535 aborted = cancel_work_sync(&sdev->probe_work); 536 537 /* 538 * Unregister any registered client device first before IPC and debugfs 539 * to allow client drivers to be removed cleanly 540 */ 541 sof_unregister_clients(sdev); 542 543 /* 544 * Unregister machine driver. This will unbind the snd_card which 545 * will remove the component driver and unload the topology 546 * before freeing the snd_card. 547 */ 548 snd_sof_machine_unregister(sdev, pdata); 549 550 if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) { 551 sof_fw_trace_free(sdev); 552 ret = snd_sof_dsp_power_down_notify(sdev); 553 if (ret < 0) 554 dev_warn(dev, "error: %d failed to prepare DSP for device removal", 555 ret); 556 557 snd_sof_ipc_free(sdev); 558 snd_sof_free_debug(sdev); 559 snd_sof_remove(sdev); 560 snd_sof_remove_late(sdev); 561 sof_ops_free(sdev); 562 } else if (aborted) { 563 /* probe_work never ran */ 564 snd_sof_remove_late(sdev); 565 sof_ops_free(sdev); 566 } 567 568 /* release firmware */ 569 snd_sof_fw_unload(sdev); 570 571 return 0; 572 } 573 EXPORT_SYMBOL(snd_sof_device_remove); 574 575 int snd_sof_device_shutdown(struct device *dev) 576 { 577 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 578 579 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) 580 cancel_work_sync(&sdev->probe_work); 581 582 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) { 583 sof_fw_trace_free(sdev); 584 return snd_sof_shutdown(sdev); 585 } 586 587 return 0; 588 } 589 EXPORT_SYMBOL(snd_sof_device_shutdown); 590 591 /* Machine driver registering and unregistering */ 592 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata) 593 { 594 struct snd_sof_pdata *plat_data = pdata; 595 const char *drv_name; 596 const void *mach; 597 int size; 598 599 drv_name = plat_data->machine->drv_name; 600 mach = plat_data->machine; 601 size = sizeof(*plat_data->machine); 602 603 /* register machine driver, pass machine info as pdata */ 604 plat_data->pdev_mach = 605 platform_device_register_data(sdev->dev, drv_name, 606 PLATFORM_DEVID_NONE, mach, size); 607 if (IS_ERR(plat_data->pdev_mach)) 608 return PTR_ERR(plat_data->pdev_mach); 609 610 dev_dbg(sdev->dev, "created machine %s\n", 611 dev_name(&plat_data->pdev_mach->dev)); 612 613 return 0; 614 } 615 EXPORT_SYMBOL(sof_machine_register); 616 617 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata) 618 { 619 struct snd_sof_pdata *plat_data = pdata; 620 621 platform_device_unregister(plat_data->pdev_mach); 622 } 623 EXPORT_SYMBOL(sof_machine_unregister); 624 625 MODULE_AUTHOR("Liam Girdwood"); 626 MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core"); 627 MODULE_LICENSE("Dual BSD/GPL"); 628 MODULE_ALIAS("platform:sof-audio"); 629 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT); 630