core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #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 "ops.h"
  16. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
  17. #include "probe.h"
  18. #endif
  19. /* see SOF_DBG_ flags */
  20. int sof_core_debug;
  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. * FW Panic/fault handling.
  28. */
  29. struct sof_panic_msg {
  30. u32 id;
  31. const char *msg;
  32. };
  33. /* standard FW panic types */
  34. static const struct sof_panic_msg panic_msg[] = {
  35. {SOF_IPC_PANIC_MEM, "out of memory"},
  36. {SOF_IPC_PANIC_WORK, "work subsystem init failed"},
  37. {SOF_IPC_PANIC_IPC, "IPC subsystem init failed"},
  38. {SOF_IPC_PANIC_ARCH, "arch init failed"},
  39. {SOF_IPC_PANIC_PLATFORM, "platform init failed"},
  40. {SOF_IPC_PANIC_TASK, "scheduler init failed"},
  41. {SOF_IPC_PANIC_EXCEPTION, "runtime exception"},
  42. {SOF_IPC_PANIC_DEADLOCK, "deadlock"},
  43. {SOF_IPC_PANIC_STACK, "stack overflow"},
  44. {SOF_IPC_PANIC_IDLE, "can't enter idle"},
  45. {SOF_IPC_PANIC_WFI, "invalid wait state"},
  46. {SOF_IPC_PANIC_ASSERT, "assertion failed"},
  47. };
  48. /*
  49. * helper to be called from .dbg_dump callbacks. No error code is
  50. * provided, it's left as an exercise for the caller of .dbg_dump
  51. * (typically IPC or loader)
  52. */
  53. void snd_sof_get_status(struct snd_sof_dev *sdev, u32 panic_code,
  54. u32 tracep_code, void *oops,
  55. struct sof_ipc_panic_info *panic_info,
  56. void *stack, size_t stack_words)
  57. {
  58. u32 code;
  59. int i;
  60. /* is firmware dead ? */
  61. if ((panic_code & SOF_IPC_PANIC_MAGIC_MASK) != SOF_IPC_PANIC_MAGIC) {
  62. dev_err(sdev->dev, "error: unexpected fault 0x%8.8x trace 0x%8.8x\n",
  63. panic_code, tracep_code);
  64. return; /* no fault ? */
  65. }
  66. code = panic_code & (SOF_IPC_PANIC_MAGIC_MASK | SOF_IPC_PANIC_CODE_MASK);
  67. for (i = 0; i < ARRAY_SIZE(panic_msg); i++) {
  68. if (panic_msg[i].id == code) {
  69. dev_err(sdev->dev, "error: %s\n", panic_msg[i].msg);
  70. dev_err(sdev->dev, "error: trace point %8.8x\n",
  71. tracep_code);
  72. goto out;
  73. }
  74. }
  75. /* unknown error */
  76. dev_err(sdev->dev, "error: unknown reason %8.8x\n", panic_code);
  77. dev_err(sdev->dev, "error: trace point %8.8x\n", tracep_code);
  78. out:
  79. dev_err(sdev->dev, "error: panic at %s:%d\n",
  80. panic_info->filename, panic_info->linenum);
  81. sof_oops(sdev, oops);
  82. sof_stack(sdev, oops, stack, stack_words);
  83. }
  84. EXPORT_SYMBOL(snd_sof_get_status);
  85. /*
  86. * FW Boot State Transition Diagram
  87. *
  88. * +-----------------------------------------------------------------------+
  89. * | |
  90. * ------------------ ------------------ |
  91. * | | | | |
  92. * | BOOT_FAILED | | READY_FAILED |-------------------------+ |
  93. * | | | | | |
  94. * ------------------ ------------------ | |
  95. * ^ ^ | |
  96. * | | | |
  97. * (FW Boot Timeout) (FW_READY FAIL) | |
  98. * | | | |
  99. * | | | |
  100. * ------------------ | ------------------ | |
  101. * | | | | | | |
  102. * | IN_PROGRESS |---------------+------------->| COMPLETE | | |
  103. * | | (FW Boot OK) (FW_READY OK) | | | |
  104. * ------------------ ------------------ | |
  105. * ^ | | |
  106. * | | | |
  107. * (FW Loading OK) (System Suspend/Runtime Suspend)
  108. * | | | |
  109. * | | | |
  110. * ------------------ ------------------ | | |
  111. * | | | |<-----+ | |
  112. * | PREPARE | | NOT_STARTED |<---------------------+ |
  113. * | | | |<---------------------------+
  114. * ------------------ ------------------
  115. * | ^ | ^
  116. * | | | |
  117. * | +-----------------------+ |
  118. * | (DSP Probe OK) |
  119. * | |
  120. * | |
  121. * +------------------------------------+
  122. * (System Suspend/Runtime Suspend)
  123. */
  124. static int sof_probe_continue(struct snd_sof_dev *sdev)
  125. {
  126. struct snd_sof_pdata *plat_data = sdev->pdata;
  127. int ret;
  128. /* probe the DSP hardware */
  129. ret = snd_sof_probe(sdev);
  130. if (ret < 0) {
  131. dev_err(sdev->dev, "error: failed to probe DSP %d\n", ret);
  132. return ret;
  133. }
  134. sdev->fw_state = SOF_FW_BOOT_PREPARE;
  135. /* check machine info */
  136. ret = sof_machine_check(sdev);
  137. if (ret < 0) {
  138. dev_err(sdev->dev, "error: failed to get machine info %d\n",
  139. ret);
  140. goto dbg_err;
  141. }
  142. /* set up platform component driver */
  143. snd_sof_new_platform_drv(sdev);
  144. /* register any debug/trace capabilities */
  145. ret = snd_sof_dbg_init(sdev);
  146. if (ret < 0) {
  147. /*
  148. * debugfs issues are suppressed in snd_sof_dbg_init() since
  149. * we cannot rely on debugfs
  150. * here we trap errors due to memory allocation only.
  151. */
  152. dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
  153. ret);
  154. goto dbg_err;
  155. }
  156. /* init the IPC */
  157. sdev->ipc = snd_sof_ipc_init(sdev);
  158. if (!sdev->ipc) {
  159. ret = -ENOMEM;
  160. dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret);
  161. goto ipc_err;
  162. }
  163. /* load the firmware */
  164. ret = snd_sof_load_firmware(sdev);
  165. if (ret < 0) {
  166. dev_err(sdev->dev, "error: failed to load DSP firmware %d\n",
  167. ret);
  168. goto fw_load_err;
  169. }
  170. sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS;
  171. /*
  172. * Boot the firmware. The FW boot status will be modified
  173. * in snd_sof_run_firmware() depending on the outcome.
  174. */
  175. ret = snd_sof_run_firmware(sdev);
  176. if (ret < 0) {
  177. dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n",
  178. ret);
  179. goto fw_run_err;
  180. }
  181. if (IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_FIRMWARE_TRACE) ||
  182. (sof_core_debug & SOF_DBG_ENABLE_TRACE)) {
  183. sdev->dtrace_is_supported = true;
  184. /* init DMA trace */
  185. ret = snd_sof_init_trace(sdev);
  186. if (ret < 0) {
  187. /* non fatal */
  188. dev_warn(sdev->dev,
  189. "warning: failed to initialize trace %d\n",
  190. ret);
  191. }
  192. } else {
  193. dev_dbg(sdev->dev, "SOF firmware trace disabled\n");
  194. }
  195. /* hereafter all FW boot flows are for PM reasons */
  196. sdev->first_boot = false;
  197. /* now register audio DSP platform driver and dai */
  198. ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv,
  199. sof_ops(sdev)->drv,
  200. sof_ops(sdev)->num_drv);
  201. if (ret < 0) {
  202. dev_err(sdev->dev,
  203. "error: failed to register DSP DAI driver %d\n", ret);
  204. goto fw_trace_err;
  205. }
  206. ret = snd_sof_machine_register(sdev, plat_data);
  207. if (ret < 0)
  208. goto fw_trace_err;
  209. /*
  210. * Some platforms in SOF, ex: BYT, may not have their platform PM
  211. * callbacks set. Increment the usage count so as to
  212. * prevent the device from entering runtime suspend.
  213. */
  214. if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
  215. pm_runtime_get_noresume(sdev->dev);
  216. if (plat_data->sof_probe_complete)
  217. plat_data->sof_probe_complete(sdev->dev);
  218. return 0;
  219. fw_trace_err:
  220. snd_sof_free_trace(sdev);
  221. fw_run_err:
  222. snd_sof_fw_unload(sdev);
  223. fw_load_err:
  224. snd_sof_ipc_free(sdev);
  225. ipc_err:
  226. snd_sof_free_debug(sdev);
  227. dbg_err:
  228. snd_sof_remove(sdev);
  229. /* all resources freed, update state to match */
  230. sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
  231. sdev->first_boot = true;
  232. return ret;
  233. }
  234. static void sof_probe_work(struct work_struct *work)
  235. {
  236. struct snd_sof_dev *sdev =
  237. container_of(work, struct snd_sof_dev, probe_work);
  238. int ret;
  239. ret = sof_probe_continue(sdev);
  240. if (ret < 0) {
  241. /* errors cannot be propagated, log */
  242. dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret);
  243. }
  244. }
  245. int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data)
  246. {
  247. struct snd_sof_dev *sdev;
  248. sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
  249. if (!sdev)
  250. return -ENOMEM;
  251. /* initialize sof device */
  252. sdev->dev = dev;
  253. /* initialize default DSP power state */
  254. sdev->dsp_power_state.state = SOF_DSP_PM_D0;
  255. sdev->pdata = plat_data;
  256. sdev->first_boot = true;
  257. sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
  258. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
  259. sdev->extractor_stream_tag = SOF_PROBE_INVALID_NODE_ID;
  260. #endif
  261. dev_set_drvdata(dev, sdev);
  262. /* check all mandatory ops */
  263. if (!sof_ops(sdev) || !sof_ops(sdev)->probe || !sof_ops(sdev)->run ||
  264. !sof_ops(sdev)->block_read || !sof_ops(sdev)->block_write ||
  265. !sof_ops(sdev)->send_msg || !sof_ops(sdev)->load_firmware ||
  266. !sof_ops(sdev)->ipc_msg_data || !sof_ops(sdev)->ipc_pcm_params ||
  267. !sof_ops(sdev)->fw_ready)
  268. return -EINVAL;
  269. INIT_LIST_HEAD(&sdev->pcm_list);
  270. INIT_LIST_HEAD(&sdev->kcontrol_list);
  271. INIT_LIST_HEAD(&sdev->widget_list);
  272. INIT_LIST_HEAD(&sdev->dai_list);
  273. INIT_LIST_HEAD(&sdev->route_list);
  274. spin_lock_init(&sdev->ipc_lock);
  275. spin_lock_init(&sdev->hw_lock);
  276. if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
  277. INIT_WORK(&sdev->probe_work, sof_probe_work);
  278. /* set default timeouts if none provided */
  279. if (plat_data->desc->ipc_timeout == 0)
  280. sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS;
  281. else
  282. sdev->ipc_timeout = plat_data->desc->ipc_timeout;
  283. if (plat_data->desc->boot_timeout == 0)
  284. sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS;
  285. else
  286. sdev->boot_timeout = plat_data->desc->boot_timeout;
  287. if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) {
  288. schedule_work(&sdev->probe_work);
  289. return 0;
  290. }
  291. return sof_probe_continue(sdev);
  292. }
  293. EXPORT_SYMBOL(snd_sof_device_probe);
  294. int snd_sof_device_remove(struct device *dev)
  295. {
  296. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  297. struct snd_sof_pdata *pdata = sdev->pdata;
  298. int ret;
  299. if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
  300. cancel_work_sync(&sdev->probe_work);
  301. if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) {
  302. ret = snd_sof_dsp_power_down_notify(sdev);
  303. if (ret < 0)
  304. dev_warn(dev, "error: %d failed to prepare DSP for device removal",
  305. ret);
  306. snd_sof_ipc_free(sdev);
  307. snd_sof_free_debug(sdev);
  308. snd_sof_free_trace(sdev);
  309. }
  310. /*
  311. * Unregister machine driver. This will unbind the snd_card which
  312. * will remove the component driver and unload the topology
  313. * before freeing the snd_card.
  314. */
  315. snd_sof_machine_unregister(sdev, pdata);
  316. /*
  317. * Unregistering the machine driver results in unloading the topology.
  318. * Some widgets, ex: scheduler, attempt to power down the core they are
  319. * scheduled on, when they are unloaded. Therefore, the DSP must be
  320. * removed only after the topology has been unloaded.
  321. */
  322. if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED)
  323. snd_sof_remove(sdev);
  324. /* release firmware */
  325. snd_sof_fw_unload(sdev);
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(snd_sof_device_remove);
  329. MODULE_AUTHOR("Liam Girdwood");
  330. MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core");
  331. MODULE_LICENSE("Dual BSD/GPL");
  332. MODULE_ALIAS("platform:sof-audio");