162306a36Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// This file is provided under a dual BSD/GPLv2 license. When using or
462306a36Sopenharmony_ci// redistributing this file, you may do so under either license.
562306a36Sopenharmony_ci//
662306a36Sopenharmony_ci// Copyright(c) 2022 MediaTek Inc. All rights reserved.
762306a36Sopenharmony_ci//
862306a36Sopenharmony_ci// Author: YC Hung <yc.hung@mediatek.com>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/*
1162306a36Sopenharmony_ci * Common helpers for the audio DSP on MediaTek platforms
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <sound/sof/xtensa.h>
1662306a36Sopenharmony_ci#include "../ops.h"
1762306a36Sopenharmony_ci#include "mtk-adsp-common.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/**
2062306a36Sopenharmony_ci * mtk_adsp_get_registers() - This function is called in case of DSP oops
2162306a36Sopenharmony_ci * in order to gather information about the registers, filename and
2262306a36Sopenharmony_ci * linenumber and stack.
2362306a36Sopenharmony_ci * @sdev: SOF device
2462306a36Sopenharmony_ci * @xoops: Stores information about registers.
2562306a36Sopenharmony_ci * @panic_info: Stores information about filename and line number.
2662306a36Sopenharmony_ci * @stack: Stores the stack dump.
2762306a36Sopenharmony_ci * @stack_words: Size of the stack dump.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_cistatic void mtk_adsp_get_registers(struct snd_sof_dev *sdev,
3062306a36Sopenharmony_ci				   struct sof_ipc_dsp_oops_xtensa *xoops,
3162306a36Sopenharmony_ci				   struct sof_ipc_panic_info *panic_info,
3262306a36Sopenharmony_ci				   u32 *stack, size_t stack_words)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	u32 offset = sdev->dsp_oops_offset;
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	/* first read registers */
3762306a36Sopenharmony_ci	sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	/* then get panic info */
4062306a36Sopenharmony_ci	if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
4162306a36Sopenharmony_ci		dev_err(sdev->dev, "invalid header size 0x%x\n",
4262306a36Sopenharmony_ci			xoops->arch_hdr.totalsize);
4362306a36Sopenharmony_ci		return;
4462306a36Sopenharmony_ci	}
4562306a36Sopenharmony_ci	offset += xoops->arch_hdr.totalsize;
4662306a36Sopenharmony_ci	sof_mailbox_read(sdev, offset, panic_info, sizeof(*panic_info));
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	/* then get the stack */
4962306a36Sopenharmony_ci	offset += sizeof(*panic_info);
5062306a36Sopenharmony_ci	sof_mailbox_read(sdev, offset, stack, stack_words * sizeof(u32));
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/**
5462306a36Sopenharmony_ci * mtk_adsp_dump() - This function is called when a panic message is
5562306a36Sopenharmony_ci * received from the firmware.
5662306a36Sopenharmony_ci * @sdev: SOF device
5762306a36Sopenharmony_ci * @flags: parameter not used but required by ops prototype
5862306a36Sopenharmony_ci */
5962306a36Sopenharmony_civoid mtk_adsp_dump(struct snd_sof_dev *sdev, u32 flags)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	char *level = (flags & SOF_DBG_DUMP_OPTIONAL) ? KERN_DEBUG : KERN_ERR;
6262306a36Sopenharmony_ci	struct sof_ipc_dsp_oops_xtensa xoops;
6362306a36Sopenharmony_ci	struct sof_ipc_panic_info panic_info = {};
6462306a36Sopenharmony_ci	u32 stack[MTK_ADSP_STACK_DUMP_SIZE];
6562306a36Sopenharmony_ci	u32 status;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	/* Get information about the panic status from the debug box area.
6862306a36Sopenharmony_ci	 * Compute the trace point based on the status.
6962306a36Sopenharmony_ci	 */
7062306a36Sopenharmony_ci	sof_mailbox_read(sdev, sdev->debug_box.offset + 0x4, &status, 4);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	/* Get information about the registers, the filename and line
7362306a36Sopenharmony_ci	 * number and the stack.
7462306a36Sopenharmony_ci	 */
7562306a36Sopenharmony_ci	mtk_adsp_get_registers(sdev, &xoops, &panic_info, stack,
7662306a36Sopenharmony_ci			       MTK_ADSP_STACK_DUMP_SIZE);
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	/* Print the information to the console */
7962306a36Sopenharmony_ci	sof_print_oops_and_stack(sdev, level, status, status, &xoops, &panic_info,
8062306a36Sopenharmony_ci				 stack, MTK_ADSP_STACK_DUMP_SIZE);
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ciEXPORT_SYMBOL(mtk_adsp_dump);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
85