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