xref: /kernel/linux/linux-6.6/sound/soc/sof/amd/acp-ipc.c (revision 62306a36)
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) 2021 Advanced Micro Devices, Inc.
762306a36Sopenharmony_ci//
862306a36Sopenharmony_ci// Authors: Balakishore Pati <Balakishore.pati@amd.com>
962306a36Sopenharmony_ci//	    Ajit Kumar Pandey <AjitKumar.Pandey@amd.com>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/* ACP-specific SOF IPC code */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/module.h>
1462306a36Sopenharmony_ci#include "../ops.h"
1562306a36Sopenharmony_ci#include "acp.h"
1662306a36Sopenharmony_ci#include "acp-dsp-offset.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_civoid acp_mailbox_write(struct snd_sof_dev *sdev, u32 offset, void *message, size_t bytes)
1962306a36Sopenharmony_ci{
2062306a36Sopenharmony_ci	memcpy_to_scratch(sdev, offset, message, bytes);
2162306a36Sopenharmony_ci}
2262306a36Sopenharmony_ciEXPORT_SYMBOL_NS(acp_mailbox_write, SND_SOC_SOF_AMD_COMMON);
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_civoid acp_mailbox_read(struct snd_sof_dev *sdev, u32 offset, void *message, size_t bytes)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	memcpy_from_scratch(sdev, offset, message, bytes);
2762306a36Sopenharmony_ci}
2862306a36Sopenharmony_ciEXPORT_SYMBOL_NS(acp_mailbox_read, SND_SOC_SOF_AMD_COMMON);
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistatic void acpbus_trigger_host_to_dsp_swintr(struct acp_dev_data *adata)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	struct snd_sof_dev *sdev = adata->dev;
3362306a36Sopenharmony_ci	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
3462306a36Sopenharmony_ci	u32 swintr_trigger;
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	swintr_trigger = snd_sof_dsp_read(sdev, ACP_DSP_BAR, desc->dsp_intr_base +
3762306a36Sopenharmony_ci						DSP_SW_INTR_TRIG_OFFSET);
3862306a36Sopenharmony_ci	swintr_trigger |= 0x01;
3962306a36Sopenharmony_ci	snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->dsp_intr_base + DSP_SW_INTR_TRIG_OFFSET,
4062306a36Sopenharmony_ci			  swintr_trigger);
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic void acp_ipc_host_msg_set(struct snd_sof_dev *sdev)
4462306a36Sopenharmony_ci{
4562306a36Sopenharmony_ci	unsigned int host_msg = sdev->debug_box.offset +
4662306a36Sopenharmony_ci				offsetof(struct scratch_ipc_conf, sof_host_msg_write);
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + host_msg, 1);
4962306a36Sopenharmony_ci}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistatic void acp_dsp_ipc_host_done(struct snd_sof_dev *sdev)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	unsigned int dsp_msg = sdev->debug_box.offset +
5462306a36Sopenharmony_ci			       offsetof(struct scratch_ipc_conf, sof_dsp_msg_write);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + dsp_msg, 0);
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_cistatic void acp_dsp_ipc_dsp_done(struct snd_sof_dev *sdev)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	unsigned int dsp_ack = sdev->debug_box.offset +
6262306a36Sopenharmony_ci			       offsetof(struct scratch_ipc_conf, sof_dsp_ack_write);
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + dsp_ack, 0);
6562306a36Sopenharmony_ci}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ciint acp_sof_ipc_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	struct acp_dev_data *adata = sdev->pdata->hw_pdata;
7062306a36Sopenharmony_ci	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
7162306a36Sopenharmony_ci	unsigned int offset = sdev->host_box.offset;
7262306a36Sopenharmony_ci	unsigned int count = ACP_HW_SEM_RETRY_COUNT;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	while (snd_sof_dsp_read(sdev, ACP_DSP_BAR, desc->hw_semaphore_offset)) {
7562306a36Sopenharmony_ci		/* Wait until acquired HW Semaphore Lock or timeout*/
7662306a36Sopenharmony_ci		count--;
7762306a36Sopenharmony_ci		if (!count) {
7862306a36Sopenharmony_ci			dev_err(sdev->dev, "%s: Failed to acquire HW lock\n", __func__);
7962306a36Sopenharmony_ci			return -EINVAL;
8062306a36Sopenharmony_ci		}
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	acp_mailbox_write(sdev, offset, msg->msg_data, msg->msg_size);
8462306a36Sopenharmony_ci	acp_ipc_host_msg_set(sdev);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	/* Trigger host to dsp interrupt for the msg */
8762306a36Sopenharmony_ci	acpbus_trigger_host_to_dsp_swintr(adata);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	/* Unlock or Release HW Semaphore */
9062306a36Sopenharmony_ci	snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->hw_semaphore_offset, 0x0);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	return 0;
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ciEXPORT_SYMBOL_NS(acp_sof_ipc_send_msg, SND_SOC_SOF_AMD_COMMON);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_cistatic void acp_dsp_ipc_get_reply(struct snd_sof_dev *sdev)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci	struct snd_sof_ipc_msg *msg = sdev->msg;
9962306a36Sopenharmony_ci	struct sof_ipc_reply reply;
10062306a36Sopenharmony_ci	struct sof_ipc_cmd_hdr *hdr;
10162306a36Sopenharmony_ci	unsigned int offset = sdev->host_box.offset;
10262306a36Sopenharmony_ci	int ret = 0;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci       /*
10562306a36Sopenharmony_ci	* Sometimes, there is unexpected reply ipc arriving. The reply
10662306a36Sopenharmony_ci	* ipc belongs to none of the ipcs sent from driver.
10762306a36Sopenharmony_ci	* In this case, the driver must ignore the ipc.
10862306a36Sopenharmony_ci	*/
10962306a36Sopenharmony_ci	if (!msg) {
11062306a36Sopenharmony_ci		dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
11162306a36Sopenharmony_ci		return;
11262306a36Sopenharmony_ci	}
11362306a36Sopenharmony_ci	hdr = msg->msg_data;
11462306a36Sopenharmony_ci	if (hdr->cmd == (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CTX_SAVE) ||
11562306a36Sopenharmony_ci	    hdr->cmd == (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_GATE)) {
11662306a36Sopenharmony_ci		/*
11762306a36Sopenharmony_ci		 * memory windows are powered off before sending IPC reply,
11862306a36Sopenharmony_ci		 * so we can't read the mailbox for CTX_SAVE and PM_GATE
11962306a36Sopenharmony_ci		 * replies.
12062306a36Sopenharmony_ci		 */
12162306a36Sopenharmony_ci		reply.error = 0;
12262306a36Sopenharmony_ci		reply.hdr.cmd = SOF_IPC_GLB_REPLY;
12362306a36Sopenharmony_ci		reply.hdr.size = sizeof(reply);
12462306a36Sopenharmony_ci		memcpy(msg->reply_data, &reply, sizeof(reply));
12562306a36Sopenharmony_ci		goto out;
12662306a36Sopenharmony_ci	}
12762306a36Sopenharmony_ci	/* get IPC reply from DSP in the mailbox */
12862306a36Sopenharmony_ci	acp_mailbox_read(sdev, offset, &reply, sizeof(reply));
12962306a36Sopenharmony_ci	if (reply.error < 0) {
13062306a36Sopenharmony_ci		memcpy(msg->reply_data, &reply, sizeof(reply));
13162306a36Sopenharmony_ci		ret = reply.error;
13262306a36Sopenharmony_ci	} else {
13362306a36Sopenharmony_ci		/*
13462306a36Sopenharmony_ci		 * To support an IPC tx_message with a
13562306a36Sopenharmony_ci		 * reply_size set to zero.
13662306a36Sopenharmony_ci		 */
13762306a36Sopenharmony_ci		if (!msg->reply_size)
13862306a36Sopenharmony_ci			goto out;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci		/* reply correct size ? */
14162306a36Sopenharmony_ci		if (reply.hdr.size != msg->reply_size &&
14262306a36Sopenharmony_ci		    !(reply.hdr.cmd & SOF_IPC_GLB_PROBE)) {
14362306a36Sopenharmony_ci			dev_err(sdev->dev, "reply expected %zu got %u bytes\n",
14462306a36Sopenharmony_ci				msg->reply_size, reply.hdr.size);
14562306a36Sopenharmony_ci			ret = -EINVAL;
14662306a36Sopenharmony_ci		}
14762306a36Sopenharmony_ci		/* read the message */
14862306a36Sopenharmony_ci		if (msg->reply_size > 0)
14962306a36Sopenharmony_ci			acp_mailbox_read(sdev, offset, msg->reply_data, msg->reply_size);
15062306a36Sopenharmony_ci	}
15162306a36Sopenharmony_ciout:
15262306a36Sopenharmony_ci	msg->reply_error = ret;
15362306a36Sopenharmony_ci}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ciirqreturn_t acp_sof_ipc_irq_thread(int irq, void *context)
15662306a36Sopenharmony_ci{
15762306a36Sopenharmony_ci	struct snd_sof_dev *sdev = context;
15862306a36Sopenharmony_ci	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
15962306a36Sopenharmony_ci	struct acp_dev_data *adata = sdev->pdata->hw_pdata;
16062306a36Sopenharmony_ci	unsigned int dsp_msg_write = sdev->debug_box.offset +
16162306a36Sopenharmony_ci				     offsetof(struct scratch_ipc_conf, sof_dsp_msg_write);
16262306a36Sopenharmony_ci	unsigned int dsp_ack_write = sdev->debug_box.offset +
16362306a36Sopenharmony_ci				     offsetof(struct scratch_ipc_conf, sof_dsp_ack_write);
16462306a36Sopenharmony_ci	bool ipc_irq = false;
16562306a36Sopenharmony_ci	int dsp_msg, dsp_ack;
16662306a36Sopenharmony_ci	unsigned int status;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	if (sdev->first_boot && sdev->fw_state != SOF_FW_BOOT_COMPLETE) {
16962306a36Sopenharmony_ci		acp_mailbox_read(sdev, sdev->dsp_box.offset, &status, sizeof(status));
17062306a36Sopenharmony_ci		if ((status & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) {
17162306a36Sopenharmony_ci			snd_sof_dsp_panic(sdev, sdev->dsp_box.offset + sizeof(status),
17262306a36Sopenharmony_ci					  true);
17362306a36Sopenharmony_ci			status = 0;
17462306a36Sopenharmony_ci			acp_mailbox_write(sdev, sdev->dsp_box.offset, &status, sizeof(status));
17562306a36Sopenharmony_ci			return IRQ_HANDLED;
17662306a36Sopenharmony_ci		}
17762306a36Sopenharmony_ci		snd_sof_ipc_msgs_rx(sdev);
17862306a36Sopenharmony_ci		acp_dsp_ipc_host_done(sdev);
17962306a36Sopenharmony_ci		return IRQ_HANDLED;
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	dsp_msg = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + dsp_msg_write);
18362306a36Sopenharmony_ci	if (dsp_msg) {
18462306a36Sopenharmony_ci		snd_sof_ipc_msgs_rx(sdev);
18562306a36Sopenharmony_ci		acp_dsp_ipc_host_done(sdev);
18662306a36Sopenharmony_ci		ipc_irq = true;
18762306a36Sopenharmony_ci	}
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	dsp_ack = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + dsp_ack_write);
19062306a36Sopenharmony_ci	if (dsp_ack) {
19162306a36Sopenharmony_ci		spin_lock_irq(&sdev->ipc_lock);
19262306a36Sopenharmony_ci		/* handle immediate reply from DSP core */
19362306a36Sopenharmony_ci		acp_dsp_ipc_get_reply(sdev);
19462306a36Sopenharmony_ci		snd_sof_ipc_reply(sdev, 0);
19562306a36Sopenharmony_ci		/* set the done bit */
19662306a36Sopenharmony_ci		acp_dsp_ipc_dsp_done(sdev);
19762306a36Sopenharmony_ci		spin_unlock_irq(&sdev->ipc_lock);
19862306a36Sopenharmony_ci		ipc_irq = true;
19962306a36Sopenharmony_ci	}
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	acp_mailbox_read(sdev, sdev->debug_box.offset, &status, sizeof(u32));
20262306a36Sopenharmony_ci	if ((status & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) {
20362306a36Sopenharmony_ci		snd_sof_dsp_panic(sdev, sdev->dsp_oops_offset, true);
20462306a36Sopenharmony_ci		status = 0;
20562306a36Sopenharmony_ci		acp_mailbox_write(sdev, sdev->debug_box.offset, &status, sizeof(status));
20662306a36Sopenharmony_ci		return IRQ_HANDLED;
20762306a36Sopenharmony_ci	}
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	if (desc->probe_reg_offset) {
21062306a36Sopenharmony_ci		u32 val;
21162306a36Sopenharmony_ci		u32 posn;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci		/* Probe register consists of two parts
21462306a36Sopenharmony_ci		 * (0-30) bit has cumulative position value
21562306a36Sopenharmony_ci		 * 31 bit is a synchronization flag between DSP and CPU
21662306a36Sopenharmony_ci		 * for the position update
21762306a36Sopenharmony_ci		 */
21862306a36Sopenharmony_ci		val = snd_sof_dsp_read(sdev, ACP_DSP_BAR, desc->probe_reg_offset);
21962306a36Sopenharmony_ci		if (val & PROBE_STATUS_BIT) {
22062306a36Sopenharmony_ci			posn = val & ~PROBE_STATUS_BIT;
22162306a36Sopenharmony_ci			if (adata->probe_stream) {
22262306a36Sopenharmony_ci				/* Probe related posn value is of 31 bits limited to 2GB
22362306a36Sopenharmony_ci				 * once wrapped DSP won't send posn interrupt.
22462306a36Sopenharmony_ci				 */
22562306a36Sopenharmony_ci				adata->probe_stream->cstream_posn = posn;
22662306a36Sopenharmony_ci				snd_compr_fragment_elapsed(adata->probe_stream->cstream);
22762306a36Sopenharmony_ci				snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->probe_reg_offset, posn);
22862306a36Sopenharmony_ci				ipc_irq = true;
22962306a36Sopenharmony_ci			}
23062306a36Sopenharmony_ci		}
23162306a36Sopenharmony_ci	}
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	if (!ipc_irq)
23462306a36Sopenharmony_ci		dev_dbg_ratelimited(sdev->dev, "nothing to do in IPC IRQ thread\n");
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	return IRQ_HANDLED;
23762306a36Sopenharmony_ci}
23862306a36Sopenharmony_ciEXPORT_SYMBOL_NS(acp_sof_ipc_irq_thread, SND_SOC_SOF_AMD_COMMON);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ciint acp_sof_ipc_msg_data(struct snd_sof_dev *sdev, struct snd_sof_pcm_stream *sps,
24162306a36Sopenharmony_ci			 void *p, size_t sz)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	unsigned int offset = sdev->dsp_box.offset;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	if (!sps || !sdev->stream_box.size) {
24662306a36Sopenharmony_ci		acp_mailbox_read(sdev, offset, p, sz);
24762306a36Sopenharmony_ci	} else {
24862306a36Sopenharmony_ci		struct snd_pcm_substream *substream = sps->substream;
24962306a36Sopenharmony_ci		struct acp_dsp_stream *stream;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci		if (!substream || !substream->runtime)
25262306a36Sopenharmony_ci			return -ESTRPIPE;
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci		stream = substream->runtime->private_data;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci		if (!stream)
25762306a36Sopenharmony_ci			return -ESTRPIPE;
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci		acp_mailbox_read(sdev, stream->posn_offset, p, sz);
26062306a36Sopenharmony_ci	}
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	return 0;
26362306a36Sopenharmony_ci}
26462306a36Sopenharmony_ciEXPORT_SYMBOL_NS(acp_sof_ipc_msg_data, SND_SOC_SOF_AMD_COMMON);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ciint acp_set_stream_data_offset(struct snd_sof_dev *sdev,
26762306a36Sopenharmony_ci			       struct snd_sof_pcm_stream *sps,
26862306a36Sopenharmony_ci			       size_t posn_offset)
26962306a36Sopenharmony_ci{
27062306a36Sopenharmony_ci	struct snd_pcm_substream *substream = sps->substream;
27162306a36Sopenharmony_ci	struct acp_dsp_stream *stream = substream->runtime->private_data;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	/* check for unaligned offset or overflow */
27462306a36Sopenharmony_ci	if (posn_offset > sdev->stream_box.size ||
27562306a36Sopenharmony_ci	    posn_offset % sizeof(struct sof_ipc_stream_posn) != 0)
27662306a36Sopenharmony_ci		return -EINVAL;
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	stream->posn_offset = sdev->stream_box.offset + posn_offset;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	dev_dbg(sdev->dev, "pcm: stream dir %d, posn mailbox offset is %zu",
28162306a36Sopenharmony_ci		substream->stream, stream->posn_offset);
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	return 0;
28462306a36Sopenharmony_ci}
28562306a36Sopenharmony_ciEXPORT_SYMBOL_NS(acp_set_stream_data_offset, SND_SOC_SOF_AMD_COMMON);
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ciint acp_sof_ipc_get_mailbox_offset(struct snd_sof_dev *sdev)
28862306a36Sopenharmony_ci{
28962306a36Sopenharmony_ci	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	return desc->sram_pte_offset;
29262306a36Sopenharmony_ci}
29362306a36Sopenharmony_ciEXPORT_SYMBOL_NS(acp_sof_ipc_get_mailbox_offset, SND_SOC_SOF_AMD_COMMON);
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ciint acp_sof_ipc_get_window_offset(struct snd_sof_dev *sdev, u32 id)
29662306a36Sopenharmony_ci{
29762306a36Sopenharmony_ci	return 0;
29862306a36Sopenharmony_ci}
29962306a36Sopenharmony_ciEXPORT_SYMBOL_NS(acp_sof_ipc_get_window_offset, SND_SOC_SOF_AMD_COMMON);
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ciMODULE_DESCRIPTION("AMD ACP sof-ipc driver");
302