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 static struct snd_sof_of_mach *sof_of_machine_select(struct snd_sof_dev *sdev) 148 { 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; 152 153 if (!mach) 154 return NULL; 155 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; 161 162 return mach; 163 } 164 } 165 166 return NULL; 167 } 168 169 /* SOF Driver enumeration */ 170 static int sof_machine_check(struct snd_sof_dev *sdev) 171 { 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; 175 176 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) { 177 const struct snd_sof_of_mach *of_mach; 178 179 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) && 180 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC)) 181 goto nocodec; 182 183 /* find machine */ 184 mach = snd_sof_machine_select(sdev); 185 if (mach) { 186 sof_pdata->machine = mach; 187 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; 192 } 193 194 snd_sof_set_mach_params(mach, sdev); 195 return 0; 196 } 197 198 of_mach = sof_of_machine_select(sdev); 199 if (of_mach) { 200 sof_pdata->of_machine = of_mach; 201 return 0; 202 } 203 204 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) { 205 dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n"); 206 return -ENODEV; 207 } 208 } else { 209 dev_warn(sdev->dev, "Force to use nocodec mode\n"); 210 } 211 212 nocodec: 213 /* select nocodec mode */ 214 dev_warn(sdev->dev, "Using nocodec machine driver\n"); 215 mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL); 216 if (!mach) 217 return -ENOMEM; 218 219 mach->drv_name = "sof-nocodec"; 220 if (!sof_pdata->tplg_filename) 221 sof_pdata->tplg_filename = desc->nocodec_tplg_filename; 222 223 sof_pdata->machine = mach; 224 snd_sof_set_mach_params(mach, sdev); 225 226 return 0; 227 } 228 229 static int sof_select_ipc_and_paths(struct snd_sof_dev *sdev) 230 { 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; 235 int ret; 236 237 if (base_profile->ipc_type != plat_data->desc->ipc_default) 238 dev_info(dev, 239 "Module parameter used, overriding default IPC %d to %d\n", 240 plat_data->desc->ipc_default, base_profile->ipc_type); 241 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); 248 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); 255 256 if (base_profile->fw_name) 257 dev_dbg(dev, "Module parameter used, changed fw filename to %s\n", 258 base_profile->fw_name); 259 260 if (base_profile->tplg_path) 261 dev_dbg(dev, "Module parameter used, changed tplg path to %s\n", 262 base_profile->tplg_path); 263 264 if (base_profile->tplg_name) 265 dev_dbg(dev, "Module parameter used, changed tplg name to %s\n", 266 base_profile->tplg_name); 267 268 ret = sof_create_ipc_file_profile(sdev, base_profile, &out_profile); 269 if (ret) 270 return ret; 271 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; 277 278 return 0; 279 } 280 281 static int validate_sof_ops(struct snd_sof_dev *sdev) 282 { 283 int ret; 284 285 /* init ops, if necessary */ 286 ret = sof_ops_init(sdev); 287 if (ret < 0) 288 return ret; 289 290 /* check all mandatory ops */ 291 if (!sof_ops(sdev) || !sof_ops(sdev)->probe) { 292 dev_err(sdev->dev, "missing mandatory ops\n"); 293 sof_ops_free(sdev); 294 return -EINVAL; 295 } 296 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"); 302 sof_ops_free(sdev); 303 return -EINVAL; 304 } 305 306 return 0; 307 } 308 309 static int sof_init_sof_ops(struct snd_sof_dev *sdev) 310 { 311 struct snd_sof_pdata *plat_data = sdev->pdata; 312 struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base; 313 314 /* check IPC support */ 315 if (!(BIT(base_profile->ipc_type) & plat_data->desc->ipc_supported_mask)) { 316 dev_err(sdev->dev, 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); 319 return -EINVAL; 320 } 321 322 /* 323 * Save the selected IPC type and a topology name override before 324 * selecting ops since platform code might need this information 325 */ 326 plat_data->ipc_type = base_profile->ipc_type; 327 plat_data->tplg_filename = base_profile->tplg_name; 328 329 return validate_sof_ops(sdev); 330 } 331 332 static int sof_init_environment(struct snd_sof_dev *sdev) 333 { 334 struct snd_sof_pdata *plat_data = sdev->pdata; 335 struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base; 336 int ret; 337 338 /* probe the DSP hardware */ 339 ret = snd_sof_probe(sdev); 340 if (ret < 0) { 341 dev_err(sdev->dev, "failed to probe DSP %d\n", ret); 342 sof_ops_free(sdev); 343 return ret; 344 } 345 346 /* check machine info */ 347 ret = sof_machine_check(sdev); 348 if (ret < 0) { 349 dev_err(sdev->dev, "failed to get machine info %d\n", ret); 350 goto err_machine_check; 351 } 352 353 ret = sof_select_ipc_and_paths(sdev); 354 if (!ret && plat_data->ipc_type != base_profile->ipc_type) { 355 /* IPC type changed, re-initialize the ops */ 356 sof_ops_free(sdev); 357 358 ret = validate_sof_ops(sdev); 359 if (ret < 0) { 360 snd_sof_remove(sdev); 361 return ret; 362 } 363 } 364 365 err_machine_check: 366 if (ret) { 367 snd_sof_remove(sdev); 368 sof_ops_free(sdev); 369 } 370 371 return ret; 372 } 373 374 /* 375 * FW Boot State Transition Diagram 376 * 377 * +----------------------------------------------------------------------+ 378 * | | 379 * ------------------ ------------------ | 380 * | | | | | 381 * | BOOT_FAILED |<-------| READY_FAILED | | 382 * | |<--+ | | ------------------ | 383 * ------------------ | ------------------ | | | 384 * ^ | ^ | CRASHED |---+ | 385 * | | | | | | | 386 * (FW Boot Timeout) | (FW_READY FAIL) ------------------ | | 387 * | | | ^ | | 388 * | | | |(DSP Panic) | | 389 * ------------------ | | ------------------ | | 390 * | | | | | | | | 391 * | IN_PROGRESS |---------------+------------->| COMPLETE | | | 392 * | | (FW Boot OK) (FW_READY OK) | | | | 393 * ------------------ | ------------------ | | 394 * ^ | | | | 395 * | | | | | 396 * (FW Loading OK) | (System Suspend/Runtime Suspend) 397 * | | | | | 398 * | (FW Loading Fail) | | | 399 * ------------------ | ------------------ | | | 400 * | | | | |<-----+ | | 401 * | PREPARE |---+ | NOT_STARTED |<---------------------+ | 402 * | | | |<--------------------------+ 403 * ------------------ ------------------ 404 * | ^ | ^ 405 * | | | | 406 * | +-----------------------+ | 407 * | (DSP Probe OK) | 408 * | | 409 * | | 410 * +------------------------------------+ 411 * (System Suspend/Runtime Suspend) 412 */ 413 414 static int sof_probe_continue(struct snd_sof_dev *sdev) 415 { 416 struct snd_sof_pdata *plat_data = sdev->pdata; 417 int ret; 418 419 /* Initialize loadable file paths and check the environment validity */ 420 ret = sof_init_environment(sdev); 421 if (ret) 422 return ret; 423 424 sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE); 425 426 /* set up platform component driver */ 427 snd_sof_new_platform_drv(sdev); 428 429 if (sdev->dspless_mode_selected) { 430 sof_set_fw_state(sdev, SOF_DSPLESS_MODE); 431 goto skip_dsp_init; 432 } 433 434 /* register any debug/trace capabilities */ 435 ret = snd_sof_dbg_init(sdev); 436 if (ret < 0) { 437 /* 438 * debugfs issues are suppressed in snd_sof_dbg_init() since 439 * we cannot rely on debugfs 440 * here we trap errors due to memory allocation only. 441 */ 442 dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n", 443 ret); 444 goto dbg_err; 445 } 446 447 /* init the IPC */ 448 sdev->ipc = snd_sof_ipc_init(sdev); 449 if (!sdev->ipc) { 450 ret = -ENOMEM; 451 dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret); 452 goto ipc_err; 453 } 454 455 /* load the firmware */ 456 ret = snd_sof_load_firmware(sdev); 457 if (ret < 0) { 458 dev_err(sdev->dev, "error: failed to load DSP firmware %d\n", 459 ret); 460 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED); 461 goto fw_load_err; 462 } 463 464 sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS); 465 466 /* 467 * Boot the firmware. The FW boot status will be modified 468 * in snd_sof_run_firmware() depending on the outcome. 469 */ 470 ret = snd_sof_run_firmware(sdev); 471 if (ret < 0) { 472 dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n", 473 ret); 474 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED); 475 goto fw_run_err; 476 } 477 478 if (sof_debug_check_flag(SOF_DBG_ENABLE_TRACE)) { 479 sdev->fw_trace_is_supported = true; 480 481 /* init firmware tracing */ 482 ret = sof_fw_trace_init(sdev); 483 if (ret < 0) { 484 /* non fatal */ 485 dev_warn(sdev->dev, "failed to initialize firmware tracing %d\n", 486 ret); 487 } 488 } else { 489 dev_dbg(sdev->dev, "SOF firmware trace disabled\n"); 490 } 491 492 skip_dsp_init: 493 /* hereafter all FW boot flows are for PM reasons */ 494 sdev->first_boot = false; 495 496 /* now register audio DSP platform driver and dai */ 497 ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv, 498 sof_ops(sdev)->drv, 499 sof_ops(sdev)->num_drv); 500 if (ret < 0) { 501 dev_err(sdev->dev, 502 "error: failed to register DSP DAI driver %d\n", ret); 503 goto fw_trace_err; 504 } 505 506 ret = snd_sof_machine_register(sdev, plat_data); 507 if (ret < 0) { 508 dev_err(sdev->dev, 509 "error: failed to register machine driver %d\n", ret); 510 goto fw_trace_err; 511 } 512 513 ret = sof_register_clients(sdev); 514 if (ret < 0) { 515 dev_err(sdev->dev, "failed to register clients %d\n", ret); 516 goto sof_machine_err; 517 } 518 519 /* 520 * Some platforms in SOF, ex: BYT, may not have their platform PM 521 * callbacks set. Increment the usage count so as to 522 * prevent the device from entering runtime suspend. 523 */ 524 if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume) 525 pm_runtime_get_noresume(sdev->dev); 526 527 if (plat_data->sof_probe_complete) 528 plat_data->sof_probe_complete(sdev->dev); 529 530 sdev->probe_completed = true; 531 532 return 0; 533 534 sof_machine_err: 535 snd_sof_machine_unregister(sdev, plat_data); 536 fw_trace_err: 537 sof_fw_trace_free(sdev); 538 fw_run_err: 539 snd_sof_fw_unload(sdev); 540 fw_load_err: 541 snd_sof_ipc_free(sdev); 542 ipc_err: 543 dbg_err: 544 snd_sof_free_debug(sdev); 545 snd_sof_remove(sdev); 546 snd_sof_remove_late(sdev); 547 sof_ops_free(sdev); 548 549 /* all resources freed, update state to match */ 550 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED); 551 sdev->first_boot = true; 552 553 return ret; 554 } 555 556 static void sof_probe_work(struct work_struct *work) 557 { 558 struct snd_sof_dev *sdev = 559 container_of(work, struct snd_sof_dev, probe_work); 560 int ret; 561 562 ret = sof_probe_continue(sdev); 563 if (ret < 0) { 564 /* errors cannot be propagated, log */ 565 dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret); 566 } 567 } 568 569 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data) 570 { 571 struct snd_sof_dev *sdev; 572 int ret; 573 574 sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL); 575 if (!sdev) 576 return -ENOMEM; 577 578 /* initialize sof device */ 579 sdev->dev = dev; 580 581 /* initialize default DSP power state */ 582 sdev->dsp_power_state.state = SOF_DSP_PM_D0; 583 584 sdev->pdata = plat_data; 585 sdev->first_boot = true; 586 dev_set_drvdata(dev, sdev); 587 588 if (sof_core_debug) 589 dev_info(dev, "sof_debug value: %#x\n", sof_core_debug); 590 591 if (sof_debug_check_flag(SOF_DBG_DSPLESS_MODE)) { 592 if (plat_data->desc->dspless_mode_supported) { 593 dev_info(dev, "Switching to DSPless mode\n"); 594 sdev->dspless_mode_selected = true; 595 } else { 596 dev_info(dev, "DSPless mode is not supported by the platform\n"); 597 } 598 } 599 600 /* Initialize sof_ops based on the initial selected IPC version */ 601 ret = sof_init_sof_ops(sdev); 602 if (ret) 603 return ret; 604 605 INIT_LIST_HEAD(&sdev->pcm_list); 606 INIT_LIST_HEAD(&sdev->kcontrol_list); 607 INIT_LIST_HEAD(&sdev->widget_list); 608 INIT_LIST_HEAD(&sdev->pipeline_list); 609 INIT_LIST_HEAD(&sdev->dai_list); 610 INIT_LIST_HEAD(&sdev->dai_link_list); 611 INIT_LIST_HEAD(&sdev->route_list); 612 INIT_LIST_HEAD(&sdev->ipc_client_list); 613 INIT_LIST_HEAD(&sdev->ipc_rx_handler_list); 614 INIT_LIST_HEAD(&sdev->fw_state_handler_list); 615 spin_lock_init(&sdev->ipc_lock); 616 spin_lock_init(&sdev->hw_lock); 617 mutex_init(&sdev->power_state_access); 618 mutex_init(&sdev->ipc_client_mutex); 619 mutex_init(&sdev->client_event_handler_mutex); 620 621 /* set default timeouts if none provided */ 622 if (plat_data->desc->ipc_timeout == 0) 623 sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS; 624 else 625 sdev->ipc_timeout = plat_data->desc->ipc_timeout; 626 if (plat_data->desc->boot_timeout == 0) 627 sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS; 628 else 629 sdev->boot_timeout = plat_data->desc->boot_timeout; 630 631 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED); 632 633 /* 634 * first pass of probe which isn't allowed to run in a work-queue, 635 * typically to rely on -EPROBE_DEFER dependencies 636 */ 637 ret = snd_sof_probe_early(sdev); 638 if (ret < 0) 639 return ret; 640 641 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) { 642 INIT_WORK(&sdev->probe_work, sof_probe_work); 643 schedule_work(&sdev->probe_work); 644 return 0; 645 } 646 647 return sof_probe_continue(sdev); 648 } 649 EXPORT_SYMBOL(snd_sof_device_probe); 650 651 bool snd_sof_device_probe_completed(struct device *dev) 652 { 653 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 654 655 return sdev->probe_completed; 656 } 657 EXPORT_SYMBOL(snd_sof_device_probe_completed); 658 659 int snd_sof_device_remove(struct device *dev) 660 { 661 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 662 struct snd_sof_pdata *pdata = sdev->pdata; 663 int ret; 664 bool aborted = false; 665 666 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) 667 aborted = cancel_work_sync(&sdev->probe_work); 668 669 /* 670 * Unregister any registered client device first before IPC and debugfs 671 * to allow client drivers to be removed cleanly 672 */ 673 sof_unregister_clients(sdev); 674 675 /* 676 * Unregister machine driver. This will unbind the snd_card which 677 * will remove the component driver and unload the topology 678 * before freeing the snd_card. 679 */ 680 snd_sof_machine_unregister(sdev, pdata); 681 682 /* 683 * Balance the runtime pm usage count in case we are faced with an 684 * exception and we forcably prevented D3 power state to preserve 685 * context 686 */ 687 if (sdev->d3_prevented) { 688 sdev->d3_prevented = false; 689 pm_runtime_put_noidle(sdev->dev); 690 } 691 692 if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) { 693 sof_fw_trace_free(sdev); 694 ret = snd_sof_dsp_power_down_notify(sdev); 695 if (ret < 0) 696 dev_warn(dev, "error: %d failed to prepare DSP for device removal", 697 ret); 698 699 snd_sof_ipc_free(sdev); 700 snd_sof_free_debug(sdev); 701 snd_sof_remove(sdev); 702 snd_sof_remove_late(sdev); 703 sof_ops_free(sdev); 704 } else if (aborted) { 705 /* probe_work never ran */ 706 snd_sof_remove_late(sdev); 707 sof_ops_free(sdev); 708 } 709 710 /* release firmware */ 711 snd_sof_fw_unload(sdev); 712 713 return 0; 714 } 715 EXPORT_SYMBOL(snd_sof_device_remove); 716 717 int snd_sof_device_shutdown(struct device *dev) 718 { 719 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 720 721 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) 722 cancel_work_sync(&sdev->probe_work); 723 724 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) { 725 sof_fw_trace_free(sdev); 726 return snd_sof_shutdown(sdev); 727 } 728 729 return 0; 730 } 731 EXPORT_SYMBOL(snd_sof_device_shutdown); 732 733 /* Machine driver registering and unregistering */ 734 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata) 735 { 736 struct snd_sof_pdata *plat_data = pdata; 737 const char *drv_name; 738 const void *mach; 739 int size; 740 741 drv_name = plat_data->machine->drv_name; 742 mach = plat_data->machine; 743 size = sizeof(*plat_data->machine); 744 745 /* register machine driver, pass machine info as pdata */ 746 plat_data->pdev_mach = 747 platform_device_register_data(sdev->dev, drv_name, 748 PLATFORM_DEVID_NONE, mach, size); 749 if (IS_ERR(plat_data->pdev_mach)) 750 return PTR_ERR(plat_data->pdev_mach); 751 752 dev_dbg(sdev->dev, "created machine %s\n", 753 dev_name(&plat_data->pdev_mach->dev)); 754 755 return 0; 756 } 757 EXPORT_SYMBOL(sof_machine_register); 758 759 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata) 760 { 761 struct snd_sof_pdata *plat_data = pdata; 762 763 platform_device_unregister(plat_data->pdev_mach); 764 } 765 EXPORT_SYMBOL(sof_machine_unregister); 766 767 MODULE_AUTHOR("Liam Girdwood"); 768 MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core"); 769 MODULE_LICENSE("Dual BSD/GPL"); 770 MODULE_ALIAS("platform:sof-audio"); 771 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT); 772