core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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
  7. //
  8. // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. //
  10. #include <linux/firmware.h>
  11. #include <linux/module.h>
  12. #include <sound/soc.h>
  13. #include <sound/sof.h>
  14. #include "sof-priv.h"
  15. #include "sof-of-dev.h"
  16. #include "ops.h"
  17. #define CREATE_TRACE_POINTS
  18. #include <trace/events/sof.h>
  19. /* see SOF_DBG_ flags */
  20. static int sof_core_debug = IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_FIRMWARE_TRACE);
  21. module_param_named(sof_debug, sof_core_debug, int, 0444);
  22. MODULE_PARM_DESC(sof_debug, "SOF core debug options (0x0 all off)");
  23. /* SOF defaults if not provided by the platform in ms */
  24. #define TIMEOUT_DEFAULT_IPC_MS 500
  25. #define TIMEOUT_DEFAULT_BOOT_MS 2000
  26. /**
  27. * sof_debug_check_flag - check if a given flag(s) is set in sof_core_debug
  28. * @mask: Flag or combination of flags to check
  29. *
  30. * Returns true if all bits set in mask is also set in sof_core_debug, otherwise
  31. * false
  32. */
  33. bool sof_debug_check_flag(int mask)
  34. {
  35. if ((sof_core_debug & mask) == mask)
  36. return true;
  37. return false;
  38. }
  39. EXPORT_SYMBOL(sof_debug_check_flag);
  40. /*
  41. * FW Panic/fault handling.
  42. */
  43. struct sof_panic_msg {
  44. u32 id;
  45. const char *msg;
  46. };
  47. /* standard FW panic types */
  48. static const struct sof_panic_msg panic_msg[] = {
  49. {SOF_IPC_PANIC_MEM, "out of memory"},
  50. {SOF_IPC_PANIC_WORK, "work subsystem init failed"},
  51. {SOF_IPC_PANIC_IPC, "IPC subsystem init failed"},
  52. {SOF_IPC_PANIC_ARCH, "arch init failed"},
  53. {SOF_IPC_PANIC_PLATFORM, "platform init failed"},
  54. {SOF_IPC_PANIC_TASK, "scheduler init failed"},
  55. {SOF_IPC_PANIC_EXCEPTION, "runtime exception"},
  56. {SOF_IPC_PANIC_DEADLOCK, "deadlock"},
  57. {SOF_IPC_PANIC_STACK, "stack overflow"},
  58. {SOF_IPC_PANIC_IDLE, "can't enter idle"},
  59. {SOF_IPC_PANIC_WFI, "invalid wait state"},
  60. {SOF_IPC_PANIC_ASSERT, "assertion failed"},
  61. };
  62. /**
  63. * sof_print_oops_and_stack - Handle the printing of DSP oops and stack trace
  64. * @sdev: Pointer to the device's sdev
  65. * @level: prink log level to use for the printing
  66. * @panic_code: the panic code
  67. * @tracep_code: tracepoint code
  68. * @oops: Pointer to DSP specific oops data
  69. * @panic_info: Pointer to the received panic information message
  70. * @stack: Pointer to the call stack data
  71. * @stack_words: Number of words in the stack data
  72. *
  73. * helper to be called from .dbg_dump callbacks. No error code is
  74. * provided, it's left as an exercise for the caller of .dbg_dump
  75. * (typically IPC or loader)
  76. */
  77. void sof_print_oops_and_stack(struct snd_sof_dev *sdev, const char *level,
  78. u32 panic_code, u32 tracep_code, void *oops,
  79. struct sof_ipc_panic_info *panic_info,
  80. void *stack, size_t stack_words)
  81. {
  82. u32 code;
  83. int i;
  84. /* is firmware dead ? */
  85. if ((panic_code & SOF_IPC_PANIC_MAGIC_MASK) != SOF_IPC_PANIC_MAGIC) {
  86. dev_printk(level, sdev->dev, "unexpected fault %#010x trace %#010x\n",
  87. panic_code, tracep_code);
  88. return; /* no fault ? */
  89. }
  90. code = panic_code & (SOF_IPC_PANIC_MAGIC_MASK | SOF_IPC_PANIC_CODE_MASK);
  91. for (i = 0; i < ARRAY_SIZE(panic_msg); i++) {
  92. if (panic_msg[i].id == code) {
  93. dev_printk(level, sdev->dev, "reason: %s (%#x)\n",
  94. panic_msg[i].msg, code & SOF_IPC_PANIC_CODE_MASK);
  95. dev_printk(level, sdev->dev, "trace point: %#010x\n", tracep_code);
  96. goto out;
  97. }
  98. }
  99. /* unknown error */
  100. dev_printk(level, sdev->dev, "unknown panic code: %#x\n",
  101. code & SOF_IPC_PANIC_CODE_MASK);
  102. dev_printk(level, sdev->dev, "trace point: %#010x\n", tracep_code);
  103. out:
  104. dev_printk(level, sdev->dev, "panic at %s:%d\n", panic_info->filename,
  105. panic_info->linenum);
  106. sof_oops(sdev, level, oops);
  107. sof_stack(sdev, level, oops, stack, stack_words);
  108. }
  109. EXPORT_SYMBOL(sof_print_oops_and_stack);
  110. /* Helper to manage DSP state */
  111. void sof_set_fw_state(struct snd_sof_dev *sdev, enum sof_fw_state new_state)
  112. {
  113. if (sdev->fw_state == new_state)
  114. return;
  115. dev_dbg(sdev->dev, "fw_state change: %d -> %d\n", sdev->fw_state, new_state);
  116. sdev->fw_state = new_state;
  117. switch (new_state) {
  118. case SOF_FW_BOOT_NOT_STARTED:
  119. case SOF_FW_BOOT_COMPLETE:
  120. case SOF_FW_CRASHED:
  121. sof_client_fw_state_dispatcher(sdev);
  122. fallthrough;
  123. default:
  124. break;
  125. }
  126. }
  127. EXPORT_SYMBOL(sof_set_fw_state);
  128. static struct snd_sof_of_mach *sof_of_machine_select(struct snd_sof_dev *sdev)
  129. {
  130. struct snd_sof_pdata *sof_pdata = sdev->pdata;
  131. const struct sof_dev_desc *desc = sof_pdata->desc;
  132. struct snd_sof_of_mach *mach = desc->of_machines;
  133. if (!mach)
  134. return NULL;
  135. for (; mach->compatible; mach++) {
  136. if (of_machine_is_compatible(mach->compatible)) {
  137. sof_pdata->tplg_filename = mach->sof_tplg_filename;
  138. if (mach->fw_filename)
  139. sof_pdata->fw_filename = mach->fw_filename;
  140. return mach;
  141. }
  142. }
  143. return NULL;
  144. }
  145. /* SOF Driver enumeration */
  146. static int sof_machine_check(struct snd_sof_dev *sdev)
  147. {
  148. struct snd_sof_pdata *sof_pdata = sdev->pdata;
  149. const struct sof_dev_desc *desc = sof_pdata->desc;
  150. struct snd_soc_acpi_mach *mach;
  151. if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) {
  152. const struct snd_sof_of_mach *of_mach;
  153. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  154. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  155. goto nocodec;
  156. /* find machine */
  157. mach = snd_sof_machine_select(sdev);
  158. if (mach) {
  159. sof_pdata->machine = mach;
  160. if (sof_pdata->subsystem_id_set) {
  161. mach->mach_params.subsystem_vendor = sof_pdata->subsystem_vendor;
  162. mach->mach_params.subsystem_device = sof_pdata->subsystem_device;
  163. mach->mach_params.subsystem_id_set = true;
  164. }
  165. snd_sof_set_mach_params(mach, sdev);
  166. return 0;
  167. }
  168. of_mach = sof_of_machine_select(sdev);
  169. if (of_mach) {
  170. sof_pdata->of_machine = of_mach;
  171. return 0;
  172. }
  173. if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) {
  174. dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n");
  175. return -ENODEV;
  176. }
  177. } else {
  178. dev_warn(sdev->dev, "Force to use nocodec mode\n");
  179. }
  180. nocodec:
  181. /* select nocodec mode */
  182. dev_warn(sdev->dev, "Using nocodec machine driver\n");
  183. mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL);
  184. if (!mach)
  185. return -ENOMEM;
  186. mach->drv_name = "sof-nocodec";
  187. if (!sof_pdata->tplg_filename)
  188. sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
  189. sof_pdata->machine = mach;
  190. snd_sof_set_mach_params(mach, sdev);
  191. return 0;
  192. }
  193. static int sof_select_ipc_and_paths(struct snd_sof_dev *sdev)
  194. {
  195. struct snd_sof_pdata *plat_data = sdev->pdata;
  196. struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base;
  197. struct sof_loadable_file_profile out_profile;
  198. struct device *dev = sdev->dev;
  199. int ret;
  200. if (base_profile->ipc_type != plat_data->desc->ipc_default)
  201. dev_info(dev,
  202. "Module parameter used, overriding default IPC %d to %d\n",
  203. plat_data->desc->ipc_default, base_profile->ipc_type);
  204. if (base_profile->fw_path)
  205. dev_dbg(dev, "Module parameter used, changed fw path to %s\n",
  206. base_profile->fw_path);
  207. else if (base_profile->fw_path_postfix)
  208. dev_dbg(dev, "Path postfix appended to default fw path: %s\n",
  209. base_profile->fw_path_postfix);
  210. if (base_profile->fw_lib_path)
  211. dev_dbg(dev, "Module parameter used, changed fw_lib path to %s\n",
  212. base_profile->fw_lib_path);
  213. else if (base_profile->fw_lib_path_postfix)
  214. dev_dbg(dev, "Path postfix appended to default fw_lib path: %s\n",
  215. base_profile->fw_lib_path_postfix);
  216. if (base_profile->fw_name)
  217. dev_dbg(dev, "Module parameter used, changed fw filename to %s\n",
  218. base_profile->fw_name);
  219. if (base_profile->tplg_path)
  220. dev_dbg(dev, "Module parameter used, changed tplg path to %s\n",
  221. base_profile->tplg_path);
  222. if (base_profile->tplg_name)
  223. dev_dbg(dev, "Module parameter used, changed tplg name to %s\n",
  224. base_profile->tplg_name);
  225. ret = sof_create_ipc_file_profile(sdev, base_profile, &out_profile);
  226. if (ret)
  227. return ret;
  228. plat_data->ipc_type = out_profile.ipc_type;
  229. plat_data->fw_filename = out_profile.fw_name;
  230. plat_data->fw_filename_prefix = out_profile.fw_path;
  231. plat_data->fw_lib_prefix = out_profile.fw_lib_path;
  232. plat_data->tplg_filename_prefix = out_profile.tplg_path;
  233. return 0;
  234. }
  235. static int validate_sof_ops(struct snd_sof_dev *sdev)
  236. {
  237. int ret;
  238. /* init ops, if necessary */
  239. ret = sof_ops_init(sdev);
  240. if (ret < 0)
  241. return ret;
  242. /* check all mandatory ops */
  243. if (!sof_ops(sdev) || !sof_ops(sdev)->probe) {
  244. dev_err(sdev->dev, "missing mandatory ops\n");
  245. sof_ops_free(sdev);
  246. return -EINVAL;
  247. }
  248. if (!sdev->dspless_mode_selected &&
  249. (!sof_ops(sdev)->run || !sof_ops(sdev)->block_read ||
  250. !sof_ops(sdev)->block_write || !sof_ops(sdev)->send_msg ||
  251. !sof_ops(sdev)->load_firmware || !sof_ops(sdev)->ipc_msg_data)) {
  252. dev_err(sdev->dev, "missing mandatory DSP ops\n");
  253. sof_ops_free(sdev);
  254. return -EINVAL;
  255. }
  256. return 0;
  257. }
  258. static int sof_init_sof_ops(struct snd_sof_dev *sdev)
  259. {
  260. struct snd_sof_pdata *plat_data = sdev->pdata;
  261. struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base;
  262. /* check IPC support */
  263. if (!(BIT(base_profile->ipc_type) & plat_data->desc->ipc_supported_mask)) {
  264. dev_err(sdev->dev,
  265. "ipc_type %d is not supported on this platform, mask is %#x\n",
  266. base_profile->ipc_type, plat_data->desc->ipc_supported_mask);
  267. return -EINVAL;
  268. }
  269. /*
  270. * Save the selected IPC type and a topology name override before
  271. * selecting ops since platform code might need this information
  272. */
  273. plat_data->ipc_type = base_profile->ipc_type;
  274. plat_data->tplg_filename = base_profile->tplg_name;
  275. return validate_sof_ops(sdev);
  276. }
  277. static int sof_init_environment(struct snd_sof_dev *sdev)
  278. {
  279. struct snd_sof_pdata *plat_data = sdev->pdata;
  280. struct sof_loadable_file_profile *base_profile = &plat_data->ipc_file_profile_base;
  281. int ret;
  282. /* probe the DSP hardware */
  283. ret = snd_sof_probe(sdev);
  284. if (ret < 0) {
  285. dev_err(sdev->dev, "failed to probe DSP %d\n", ret);
  286. goto err_sof_probe;
  287. }
  288. /* check machine info */
  289. ret = sof_machine_check(sdev);
  290. if (ret < 0) {
  291. dev_err(sdev->dev, "failed to get machine info %d\n", ret);
  292. goto err_machine_check;
  293. }
  294. ret = sof_select_ipc_and_paths(sdev);
  295. if (ret) {
  296. goto err_machine_check;
  297. } else if (plat_data->ipc_type != base_profile->ipc_type) {
  298. /* IPC type changed, re-initialize the ops */
  299. sof_ops_free(sdev);
  300. ret = validate_sof_ops(sdev);
  301. if (ret < 0) {
  302. snd_sof_remove(sdev);
  303. snd_sof_remove_late(sdev);
  304. return ret;
  305. }
  306. }
  307. return 0;
  308. err_machine_check:
  309. snd_sof_remove(sdev);
  310. err_sof_probe:
  311. snd_sof_remove_late(sdev);
  312. sof_ops_free(sdev);
  313. return ret;
  314. }
  315. /*
  316. * FW Boot State Transition Diagram
  317. *
  318. * +----------------------------------------------------------------------+
  319. * | |
  320. * ------------------ ------------------ |
  321. * | | | | |
  322. * | BOOT_FAILED |<-------| READY_FAILED | |
  323. * | |<--+ | | ------------------ |
  324. * ------------------ | ------------------ | | |
  325. * ^ | ^ | CRASHED |---+ |
  326. * | | | | | | |
  327. * (FW Boot Timeout) | (FW_READY FAIL) ------------------ | |
  328. * | | | ^ | |
  329. * | | | |(DSP Panic) | |
  330. * ------------------ | | ------------------ | |
  331. * | | | | | | | |
  332. * | IN_PROGRESS |---------------+------------->| COMPLETE | | |
  333. * | | (FW Boot OK) (FW_READY OK) | | | |
  334. * ------------------ | ------------------ | |
  335. * ^ | | | |
  336. * | | | | |
  337. * (FW Loading OK) | (System Suspend/Runtime Suspend)
  338. * | | | | |
  339. * | (FW Loading Fail) | | |
  340. * ------------------ | ------------------ | | |
  341. * | | | | |<-----+ | |
  342. * | PREPARE |---+ | NOT_STARTED |<---------------------+ |
  343. * | | | |<--------------------------+
  344. * ------------------ ------------------
  345. * | ^ | ^
  346. * | | | |
  347. * | +-----------------------+ |
  348. * | (DSP Probe OK) |
  349. * | |
  350. * | |
  351. * +------------------------------------+
  352. * (System Suspend/Runtime Suspend)
  353. */
  354. static int sof_probe_continue(struct snd_sof_dev *sdev)
  355. {
  356. struct snd_sof_pdata *plat_data = sdev->pdata;
  357. int ret;
  358. /* Initialize loadable file paths and check the environment validity */
  359. ret = sof_init_environment(sdev);
  360. if (ret)
  361. return ret;
  362. sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
  363. /* set up platform component driver */
  364. snd_sof_new_platform_drv(sdev);
  365. if (sdev->dspless_mode_selected) {
  366. sof_set_fw_state(sdev, SOF_DSPLESS_MODE);
  367. goto skip_dsp_init;
  368. }
  369. /* register any debug/trace capabilities */
  370. ret = snd_sof_dbg_init(sdev);
  371. if (ret < 0) {
  372. /*
  373. * debugfs issues are suppressed in snd_sof_dbg_init() since
  374. * we cannot rely on debugfs
  375. * here we trap errors due to memory allocation only.
  376. */
  377. dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
  378. ret);
  379. goto dbg_err;
  380. }
  381. /* init the IPC */
  382. sdev->ipc = snd_sof_ipc_init(sdev);
  383. if (!sdev->ipc) {
  384. ret = -ENOMEM;
  385. dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret);
  386. goto ipc_err;
  387. }
  388. /* load the firmware */
  389. ret = snd_sof_load_firmware(sdev);
  390. if (ret < 0) {
  391. dev_err(sdev->dev, "error: failed to load DSP firmware %d\n",
  392. ret);
  393. sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
  394. goto fw_load_err;
  395. }
  396. sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
  397. /*
  398. * Boot the firmware. The FW boot status will be modified
  399. * in snd_sof_run_firmware() depending on the outcome.
  400. */
  401. ret = snd_sof_run_firmware(sdev);
  402. if (ret < 0) {
  403. dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n",
  404. ret);
  405. sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
  406. goto fw_run_err;
  407. }
  408. if (sof_debug_check_flag(SOF_DBG_ENABLE_TRACE)) {
  409. sdev->fw_trace_is_supported = true;
  410. /* init firmware tracing */
  411. ret = sof_fw_trace_init(sdev);
  412. if (ret < 0) {
  413. /* non fatal */
  414. dev_warn(sdev->dev, "failed to initialize firmware tracing %d\n",
  415. ret);
  416. }
  417. } else {
  418. dev_dbg(sdev->dev, "SOF firmware trace disabled\n");
  419. }
  420. skip_dsp_init:
  421. /* hereafter all FW boot flows are for PM reasons */
  422. sdev->first_boot = false;
  423. /* now register audio DSP platform driver and dai */
  424. ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv,
  425. sof_ops(sdev)->drv,
  426. sof_ops(sdev)->num_drv);
  427. if (ret < 0) {
  428. dev_err(sdev->dev,
  429. "error: failed to register DSP DAI driver %d\n", ret);
  430. goto fw_trace_err;
  431. }
  432. ret = snd_sof_machine_register(sdev, plat_data);
  433. if (ret < 0) {
  434. dev_err(sdev->dev,
  435. "error: failed to register machine driver %d\n", ret);
  436. goto fw_trace_err;
  437. }
  438. ret = sof_register_clients(sdev);
  439. if (ret < 0) {
  440. dev_err(sdev->dev, "failed to register clients %d\n", ret);
  441. goto sof_machine_err;
  442. }
  443. /*
  444. * Some platforms in SOF, ex: BYT, may not have their platform PM
  445. * callbacks set. Increment the usage count so as to
  446. * prevent the device from entering runtime suspend.
  447. */
  448. if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
  449. pm_runtime_get_noresume(sdev->dev);
  450. if (plat_data->sof_probe_complete)
  451. plat_data->sof_probe_complete(sdev->dev);
  452. sdev->probe_completed = true;
  453. return 0;
  454. sof_machine_err:
  455. snd_sof_machine_unregister(sdev, plat_data);
  456. fw_trace_err:
  457. sof_fw_trace_free(sdev);
  458. fw_run_err:
  459. snd_sof_fw_unload(sdev);
  460. fw_load_err:
  461. snd_sof_ipc_free(sdev);
  462. ipc_err:
  463. dbg_err:
  464. snd_sof_free_debug(sdev);
  465. snd_sof_remove(sdev);
  466. snd_sof_remove_late(sdev);
  467. sof_ops_free(sdev);
  468. /* all resources freed, update state to match */
  469. sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
  470. sdev->first_boot = true;
  471. return ret;
  472. }
  473. static void sof_probe_work(struct work_struct *work)
  474. {
  475. struct snd_sof_dev *sdev =
  476. container_of(work, struct snd_sof_dev, probe_work);
  477. int ret;
  478. ret = sof_probe_continue(sdev);
  479. if (ret < 0) {
  480. /* errors cannot be propagated, log */
  481. dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret);
  482. }
  483. }
  484. int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data)
  485. {
  486. struct snd_sof_dev *sdev;
  487. int ret;
  488. sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
  489. if (!sdev)
  490. return -ENOMEM;
  491. /* initialize sof device */
  492. sdev->dev = dev;
  493. /* initialize default DSP power state */
  494. sdev->dsp_power_state.state = SOF_DSP_PM_D0;
  495. sdev->pdata = plat_data;
  496. sdev->first_boot = true;
  497. dev_set_drvdata(dev, sdev);
  498. if (sof_core_debug)
  499. dev_info(dev, "sof_debug value: %#x\n", sof_core_debug);
  500. if (sof_debug_check_flag(SOF_DBG_DSPLESS_MODE)) {
  501. if (plat_data->desc->dspless_mode_supported) {
  502. dev_info(dev, "Switching to DSPless mode\n");
  503. sdev->dspless_mode_selected = true;
  504. } else {
  505. dev_info(dev, "DSPless mode is not supported by the platform\n");
  506. }
  507. }
  508. /* Initialize sof_ops based on the initial selected IPC version */
  509. ret = sof_init_sof_ops(sdev);
  510. if (ret)
  511. return ret;
  512. INIT_LIST_HEAD(&sdev->pcm_list);
  513. INIT_LIST_HEAD(&sdev->kcontrol_list);
  514. INIT_LIST_HEAD(&sdev->widget_list);
  515. INIT_LIST_HEAD(&sdev->pipeline_list);
  516. INIT_LIST_HEAD(&sdev->dai_list);
  517. INIT_LIST_HEAD(&sdev->dai_link_list);
  518. INIT_LIST_HEAD(&sdev->route_list);
  519. INIT_LIST_HEAD(&sdev->ipc_client_list);
  520. INIT_LIST_HEAD(&sdev->ipc_rx_handler_list);
  521. INIT_LIST_HEAD(&sdev->fw_state_handler_list);
  522. spin_lock_init(&sdev->ipc_lock);
  523. spin_lock_init(&sdev->hw_lock);
  524. mutex_init(&sdev->power_state_access);
  525. mutex_init(&sdev->ipc_client_mutex);
  526. mutex_init(&sdev->client_event_handler_mutex);
  527. /* set default timeouts if none provided */
  528. if (plat_data->desc->ipc_timeout == 0)
  529. sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS;
  530. else
  531. sdev->ipc_timeout = plat_data->desc->ipc_timeout;
  532. if (plat_data->desc->boot_timeout == 0)
  533. sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS;
  534. else
  535. sdev->boot_timeout = plat_data->desc->boot_timeout;
  536. sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
  537. /*
  538. * first pass of probe which isn't allowed to run in a work-queue,
  539. * typically to rely on -EPROBE_DEFER dependencies
  540. */
  541. ret = snd_sof_probe_early(sdev);
  542. if (ret < 0)
  543. return ret;
  544. if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) {
  545. INIT_WORK(&sdev->probe_work, sof_probe_work);
  546. schedule_work(&sdev->probe_work);
  547. return 0;
  548. }
  549. return sof_probe_continue(sdev);
  550. }
  551. EXPORT_SYMBOL(snd_sof_device_probe);
  552. bool snd_sof_device_probe_completed(struct device *dev)
  553. {
  554. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  555. return sdev->probe_completed;
  556. }
  557. EXPORT_SYMBOL(snd_sof_device_probe_completed);
  558. int snd_sof_device_remove(struct device *dev)
  559. {
  560. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  561. struct snd_sof_pdata *pdata = sdev->pdata;
  562. int ret;
  563. bool aborted = false;
  564. if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
  565. aborted = cancel_work_sync(&sdev->probe_work);
  566. /*
  567. * Unregister any registered client device first before IPC and debugfs
  568. * to allow client drivers to be removed cleanly
  569. */
  570. sof_unregister_clients(sdev);
  571. /*
  572. * Unregister machine driver. This will unbind the snd_card which
  573. * will remove the component driver and unload the topology
  574. * before freeing the snd_card.
  575. */
  576. snd_sof_machine_unregister(sdev, pdata);
  577. /*
  578. * Balance the runtime pm usage count in case we are faced with an
  579. * exception and we forcably prevented D3 power state to preserve
  580. * context
  581. */
  582. if (sdev->d3_prevented) {
  583. sdev->d3_prevented = false;
  584. pm_runtime_put_noidle(sdev->dev);
  585. }
  586. if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) {
  587. sof_fw_trace_free(sdev);
  588. ret = snd_sof_dsp_power_down_notify(sdev);
  589. if (ret < 0)
  590. dev_warn(dev, "error: %d failed to prepare DSP for device removal",
  591. ret);
  592. snd_sof_ipc_free(sdev);
  593. snd_sof_free_debug(sdev);
  594. snd_sof_remove(sdev);
  595. snd_sof_remove_late(sdev);
  596. sof_ops_free(sdev);
  597. } else if (aborted) {
  598. /* probe_work never ran */
  599. snd_sof_remove_late(sdev);
  600. sof_ops_free(sdev);
  601. }
  602. /* release firmware */
  603. snd_sof_fw_unload(sdev);
  604. return 0;
  605. }
  606. EXPORT_SYMBOL(snd_sof_device_remove);
  607. int snd_sof_device_shutdown(struct device *dev)
  608. {
  609. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  610. if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
  611. cancel_work_sync(&sdev->probe_work);
  612. if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) {
  613. sof_fw_trace_free(sdev);
  614. return snd_sof_shutdown(sdev);
  615. }
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(snd_sof_device_shutdown);
  619. /* Machine driver registering and unregistering */
  620. int sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
  621. {
  622. struct snd_sof_pdata *plat_data = pdata;
  623. const char *drv_name;
  624. const void *mach;
  625. int size;
  626. drv_name = plat_data->machine->drv_name;
  627. mach = plat_data->machine;
  628. size = sizeof(*plat_data->machine);
  629. /* register machine driver, pass machine info as pdata */
  630. plat_data->pdev_mach =
  631. platform_device_register_data(sdev->dev, drv_name,
  632. PLATFORM_DEVID_NONE, mach, size);
  633. if (IS_ERR(plat_data->pdev_mach))
  634. return PTR_ERR(plat_data->pdev_mach);
  635. dev_dbg(sdev->dev, "created machine %s\n",
  636. dev_name(&plat_data->pdev_mach->dev));
  637. return 0;
  638. }
  639. EXPORT_SYMBOL(sof_machine_register);
  640. void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
  641. {
  642. struct snd_sof_pdata *plat_data = pdata;
  643. platform_device_unregister(plat_data->pdev_mach);
  644. }
  645. EXPORT_SYMBOL(sof_machine_unregister);
  646. MODULE_AUTHOR("Liam Girdwood");
  647. MODULE_LICENSE("Dual BSD/GPL");
  648. MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core");
  649. MODULE_ALIAS("platform:sof-audio");
  650. MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);