core.c 24 KB

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