mtk-adsp-common.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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) 2022 MediaTek Inc. All rights reserved.
  7. //
  8. // Author: YC Hung <yc.hung@mediatek.com>
  9. /*
  10. * Common helpers for the audio DSP on MediaTek platforms
  11. */
  12. #include <linux/module.h>
  13. #include <sound/asound.h>
  14. #include <sound/sof/xtensa.h>
  15. #include "../ops.h"
  16. #include "../sof-audio.h"
  17. #include "adsp_helper.h"
  18. #include "mtk-adsp-common.h"
  19. /**
  20. * mtk_adsp_get_registers() - This function is called in case of DSP oops
  21. * in order to gather information about the registers, filename and
  22. * linenumber and stack.
  23. * @sdev: SOF device
  24. * @xoops: Stores information about registers.
  25. * @panic_info: Stores information about filename and line number.
  26. * @stack: Stores the stack dump.
  27. * @stack_words: Size of the stack dump.
  28. */
  29. static void mtk_adsp_get_registers(struct snd_sof_dev *sdev,
  30. struct sof_ipc_dsp_oops_xtensa *xoops,
  31. struct sof_ipc_panic_info *panic_info,
  32. u32 *stack, size_t stack_words)
  33. {
  34. u32 offset = sdev->dsp_oops_offset;
  35. /* first read registers */
  36. sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
  37. /* then get panic info */
  38. if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
  39. dev_err(sdev->dev, "invalid header size 0x%x\n",
  40. xoops->arch_hdr.totalsize);
  41. return;
  42. }
  43. offset += xoops->arch_hdr.totalsize;
  44. sof_mailbox_read(sdev, offset, panic_info, sizeof(*panic_info));
  45. /* then get the stack */
  46. offset += sizeof(*panic_info);
  47. sof_mailbox_read(sdev, offset, stack, stack_words * sizeof(u32));
  48. }
  49. /**
  50. * mtk_adsp_dump() - This function is called when a panic message is
  51. * received from the firmware.
  52. * @sdev: SOF device
  53. * @flags: parameter not used but required by ops prototype
  54. */
  55. void mtk_adsp_dump(struct snd_sof_dev *sdev, u32 flags)
  56. {
  57. char *level = (flags & SOF_DBG_DUMP_OPTIONAL) ? KERN_DEBUG : KERN_ERR;
  58. struct sof_ipc_dsp_oops_xtensa xoops;
  59. struct sof_ipc_panic_info panic_info = {};
  60. u32 stack[MTK_ADSP_STACK_DUMP_SIZE];
  61. u32 status;
  62. /* Get information about the panic status from the debug box area.
  63. * Compute the trace point based on the status.
  64. */
  65. sof_mailbox_read(sdev, sdev->debug_box.offset + 0x4, &status, 4);
  66. /* Get information about the registers, the filename and line
  67. * number and the stack.
  68. */
  69. mtk_adsp_get_registers(sdev, &xoops, &panic_info, stack,
  70. MTK_ADSP_STACK_DUMP_SIZE);
  71. /* Print the information to the console */
  72. sof_print_oops_and_stack(sdev, level, status, status, &xoops, &panic_info,
  73. stack, MTK_ADSP_STACK_DUMP_SIZE);
  74. }
  75. EXPORT_SYMBOL(mtk_adsp_dump);
  76. /**
  77. * mtk_adsp_send_msg - Send message to Audio DSP
  78. * @sdev: SOF device
  79. * @msg: SOF IPC Message to send
  80. */
  81. int mtk_adsp_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
  82. {
  83. struct adsp_priv *priv = sdev->pdata->hw_pdata;
  84. sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
  85. msg->msg_size);
  86. return mtk_adsp_ipc_send(priv->dsp_ipc, MTK_ADSP_IPC_REQ, MTK_ADSP_IPC_OP_REQ);
  87. }
  88. EXPORT_SYMBOL(mtk_adsp_send_msg);
  89. /**
  90. * mtk_adsp_handle_reply - Handle reply from the Audio DSP through Mailbox
  91. * @ipc: ADSP IPC handle
  92. */
  93. void mtk_adsp_handle_reply(struct mtk_adsp_ipc *ipc)
  94. {
  95. struct adsp_priv *priv = mtk_adsp_ipc_get_data(ipc);
  96. guard(spinlock_irqsave)(&priv->sdev->ipc_lock);
  97. snd_sof_ipc_process_reply(priv->sdev, 0);
  98. }
  99. EXPORT_SYMBOL(mtk_adsp_handle_reply);
  100. /**
  101. * mtk_adsp_handle_request - Handle request from the Audio DSP through Mailbox
  102. * @ipc: ADSP IPC handle
  103. */
  104. void mtk_adsp_handle_request(struct mtk_adsp_ipc *ipc)
  105. {
  106. struct adsp_priv *priv = mtk_adsp_ipc_get_data(ipc);
  107. u32 panic_code;
  108. int ret;
  109. /* Read the message from the debug box. */
  110. sof_mailbox_read(priv->sdev, priv->sdev->debug_box.offset + 4,
  111. &panic_code, sizeof(panic_code));
  112. /* Check to see if the message is a panic code 0x0dead*** */
  113. if ((panic_code & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) {
  114. snd_sof_dsp_panic(priv->sdev, panic_code, true);
  115. } else {
  116. snd_sof_ipc_msgs_rx(priv->sdev);
  117. /* Tell DSP cmd is done */
  118. ret = mtk_adsp_ipc_send(priv->dsp_ipc, MTK_ADSP_IPC_RSP, MTK_ADSP_IPC_OP_RSP);
  119. if (ret)
  120. dev_err(priv->dev, "request send ipc failed");
  121. }
  122. }
  123. EXPORT_SYMBOL(mtk_adsp_handle_request);
  124. /**
  125. * mtk_adsp_get_bar_index - Map section type with BAR idx
  126. * @sdev: SOF device
  127. * @type: Section type as described by snd_sof_fw_blk_type
  128. *
  129. * MediaTek Audio DSPs have a 1:1 match between type and BAR idx
  130. */
  131. int mtk_adsp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
  132. {
  133. return type;
  134. }
  135. EXPORT_SYMBOL(mtk_adsp_get_bar_index);
  136. /**
  137. * mtk_adsp_stream_pcm_hw_params - Platform specific host stream hw params
  138. * @sdev: SOF device
  139. * @substream: PCM Substream
  140. * @params: hw params
  141. * @platform_params: Platform specific SOF stream parameters
  142. */
  143. int mtk_adsp_stream_pcm_hw_params(struct snd_sof_dev *sdev,
  144. struct snd_pcm_substream *substream,
  145. struct snd_pcm_hw_params *params,
  146. struct snd_sof_platform_stream_params *platform_params)
  147. {
  148. platform_params->cont_update_posn = 1;
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(mtk_adsp_stream_pcm_hw_params);
  152. /**
  153. * mtk_adsp_stream_pcm_pointer - Get host stream pointer
  154. * @sdev: SOF device
  155. * @substream: PCM substream
  156. */
  157. snd_pcm_uframes_t mtk_adsp_stream_pcm_pointer(struct snd_sof_dev *sdev,
  158. struct snd_pcm_substream *substream)
  159. {
  160. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  161. struct snd_soc_component *scomp = sdev->component;
  162. struct snd_sof_pcm_stream *stream;
  163. struct sof_ipc_stream_posn posn;
  164. struct snd_sof_pcm *spcm;
  165. snd_pcm_uframes_t pos;
  166. int ret;
  167. spcm = snd_sof_find_spcm_dai(scomp, rtd);
  168. if (!spcm) {
  169. dev_warn_ratelimited(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
  170. rtd->dai_link->id);
  171. return 0;
  172. }
  173. stream = &spcm->stream[substream->stream];
  174. ret = snd_sof_ipc_msg_data(sdev, stream, &posn, sizeof(posn));
  175. if (ret < 0) {
  176. dev_warn(sdev->dev, "failed to read stream position: %d\n", ret);
  177. return 0;
  178. }
  179. memcpy(&stream->posn, &posn, sizeof(posn));
  180. pos = spcm->stream[substream->stream].posn.host_posn;
  181. pos = bytes_to_frames(substream->runtime, pos);
  182. return pos;
  183. }
  184. EXPORT_SYMBOL(mtk_adsp_stream_pcm_pointer);
  185. MODULE_LICENSE("Dual BSD/GPL");
  186. MODULE_DESCRIPTION("SOF helpers for MTK ADSP platforms");