xref: /openbmc/linux/sound/soc/sof/ipc.c (revision 6dd78b310b8d4ab5d0b851e369b5b3d46c6a32a9)
153e0c72dSLiam Girdwood // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
253e0c72dSLiam Girdwood //
353e0c72dSLiam Girdwood // This file is provided under a dual BSD/GPLv2 license.  When using or
453e0c72dSLiam Girdwood // redistributing this file, you may do so under either license.
553e0c72dSLiam Girdwood //
653e0c72dSLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
753e0c72dSLiam Girdwood //
853e0c72dSLiam Girdwood // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
953e0c72dSLiam Girdwood //
1053e0c72dSLiam Girdwood // Generic IPC layer that can work over MMIO and SPI/I2C. PHY layer provided
1153e0c72dSLiam Girdwood // by platform driver code.
1253e0c72dSLiam Girdwood //
1353e0c72dSLiam Girdwood 
1453e0c72dSLiam Girdwood #include <linux/mutex.h>
1553e0c72dSLiam Girdwood #include <linux/types.h>
1653e0c72dSLiam Girdwood 
1753e0c72dSLiam Girdwood #include "sof-priv.h"
1853e0c72dSLiam Girdwood #include "ops.h"
1953e0c72dSLiam Girdwood 
2053e0c72dSLiam Girdwood static void ipc_trace_message(struct snd_sof_dev *sdev, u32 msg_id);
2153e0c72dSLiam Girdwood static void ipc_stream_message(struct snd_sof_dev *sdev, u32 msg_cmd);
2253e0c72dSLiam Girdwood 
2353e0c72dSLiam Girdwood /*
2453e0c72dSLiam Girdwood  * IPC message Tx/Rx message handling.
2553e0c72dSLiam Girdwood  */
2653e0c72dSLiam Girdwood 
2753e0c72dSLiam Girdwood /* SOF generic IPC data */
2853e0c72dSLiam Girdwood struct snd_sof_ipc {
2953e0c72dSLiam Girdwood 	struct snd_sof_dev *sdev;
3053e0c72dSLiam Girdwood 
3153e0c72dSLiam Girdwood 	/* protects messages and the disable flag */
3253e0c72dSLiam Girdwood 	struct mutex tx_mutex;
3353e0c72dSLiam Girdwood 	/* disables further sending of ipc's */
3453e0c72dSLiam Girdwood 	bool disable_ipc_tx;
3553e0c72dSLiam Girdwood 
3653e0c72dSLiam Girdwood 	struct snd_sof_ipc_msg msg;
3753e0c72dSLiam Girdwood };
3853e0c72dSLiam Girdwood 
3953e0c72dSLiam Girdwood struct sof_ipc_ctrl_data_params {
4053e0c72dSLiam Girdwood 	size_t msg_bytes;
4153e0c72dSLiam Girdwood 	size_t hdr_bytes;
4253e0c72dSLiam Girdwood 	size_t pl_size;
4353e0c72dSLiam Girdwood 	size_t elems;
4453e0c72dSLiam Girdwood 	u32 num_msg;
4553e0c72dSLiam Girdwood 	u8 *src;
4653e0c72dSLiam Girdwood 	u8 *dst;
4753e0c72dSLiam Girdwood };
4853e0c72dSLiam Girdwood 
4953e0c72dSLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_VERBOSE_IPC)
5053e0c72dSLiam Girdwood static void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
5153e0c72dSLiam Girdwood {
5253e0c72dSLiam Girdwood 	u8 *str;
5353e0c72dSLiam Girdwood 	u8 *str2 = NULL;
5453e0c72dSLiam Girdwood 	u32 glb;
5553e0c72dSLiam Girdwood 	u32 type;
5653e0c72dSLiam Girdwood 
5753e0c72dSLiam Girdwood 	glb = cmd & SOF_GLB_TYPE_MASK;
5853e0c72dSLiam Girdwood 	type = cmd & SOF_CMD_TYPE_MASK;
5953e0c72dSLiam Girdwood 
6053e0c72dSLiam Girdwood 	switch (glb) {
6153e0c72dSLiam Girdwood 	case SOF_IPC_GLB_REPLY:
6253e0c72dSLiam Girdwood 		str = "GLB_REPLY"; break;
6353e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMPOUND:
6453e0c72dSLiam Girdwood 		str = "GLB_COMPOUND"; break;
6553e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TPLG_MSG:
6653e0c72dSLiam Girdwood 		str = "GLB_TPLG_MSG";
6753e0c72dSLiam Girdwood 		switch (type) {
6853e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_COMP_NEW:
6953e0c72dSLiam Girdwood 			str2 = "COMP_NEW"; break;
7053e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_COMP_FREE:
7153e0c72dSLiam Girdwood 			str2 = "COMP_FREE"; break;
7253e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_COMP_CONNECT:
7353e0c72dSLiam Girdwood 			str2 = "COMP_CONNECT"; break;
7453e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_NEW:
7553e0c72dSLiam Girdwood 			str2 = "PIPE_NEW"; break;
7653e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_FREE:
7753e0c72dSLiam Girdwood 			str2 = "PIPE_FREE"; break;
7853e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_CONNECT:
7953e0c72dSLiam Girdwood 			str2 = "PIPE_CONNECT"; break;
8053e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_COMPLETE:
8153e0c72dSLiam Girdwood 			str2 = "PIPE_COMPLETE"; break;
8253e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_BUFFER_NEW:
8353e0c72dSLiam Girdwood 			str2 = "BUFFER_NEW"; break;
8453e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_BUFFER_FREE:
8553e0c72dSLiam Girdwood 			str2 = "BUFFER_FREE"; break;
8653e0c72dSLiam Girdwood 		default:
8753e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
8853e0c72dSLiam Girdwood 		}
8953e0c72dSLiam Girdwood 		break;
9053e0c72dSLiam Girdwood 	case SOF_IPC_GLB_PM_MSG:
9153e0c72dSLiam Girdwood 		str = "GLB_PM_MSG";
9253e0c72dSLiam Girdwood 		switch (type) {
9353e0c72dSLiam Girdwood 		case SOF_IPC_PM_CTX_SAVE:
9453e0c72dSLiam Girdwood 			str2 = "CTX_SAVE"; break;
9553e0c72dSLiam Girdwood 		case SOF_IPC_PM_CTX_RESTORE:
9653e0c72dSLiam Girdwood 			str2 = "CTX_RESTORE"; break;
9753e0c72dSLiam Girdwood 		case SOF_IPC_PM_CTX_SIZE:
9853e0c72dSLiam Girdwood 			str2 = "CTX_SIZE"; break;
9953e0c72dSLiam Girdwood 		case SOF_IPC_PM_CLK_SET:
10053e0c72dSLiam Girdwood 			str2 = "CLK_SET"; break;
10153e0c72dSLiam Girdwood 		case SOF_IPC_PM_CLK_GET:
10253e0c72dSLiam Girdwood 			str2 = "CLK_GET"; break;
10353e0c72dSLiam Girdwood 		case SOF_IPC_PM_CLK_REQ:
10453e0c72dSLiam Girdwood 			str2 = "CLK_REQ"; break;
10553e0c72dSLiam Girdwood 		case SOF_IPC_PM_CORE_ENABLE:
10653e0c72dSLiam Girdwood 			str2 = "CORE_ENABLE"; break;
10753e0c72dSLiam Girdwood 		default:
10853e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
10953e0c72dSLiam Girdwood 		}
11053e0c72dSLiam Girdwood 		break;
11153e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMP_MSG:
1128199a120SRanjani Sridharan 		str = "GLB_COMP_MSG";
11353e0c72dSLiam Girdwood 		switch (type) {
11453e0c72dSLiam Girdwood 		case SOF_IPC_COMP_SET_VALUE:
11553e0c72dSLiam Girdwood 			str2 = "SET_VALUE"; break;
11653e0c72dSLiam Girdwood 		case SOF_IPC_COMP_GET_VALUE:
11753e0c72dSLiam Girdwood 			str2 = "GET_VALUE"; break;
11853e0c72dSLiam Girdwood 		case SOF_IPC_COMP_SET_DATA:
11953e0c72dSLiam Girdwood 			str2 = "SET_DATA"; break;
12053e0c72dSLiam Girdwood 		case SOF_IPC_COMP_GET_DATA:
12153e0c72dSLiam Girdwood 			str2 = "GET_DATA"; break;
12253e0c72dSLiam Girdwood 		default:
12353e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
12453e0c72dSLiam Girdwood 		}
12553e0c72dSLiam Girdwood 		break;
12653e0c72dSLiam Girdwood 	case SOF_IPC_GLB_STREAM_MSG:
12753e0c72dSLiam Girdwood 		str = "GLB_STREAM_MSG";
12853e0c72dSLiam Girdwood 		switch (type) {
12953e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_PCM_PARAMS:
13053e0c72dSLiam Girdwood 			str2 = "PCM_PARAMS"; break;
13153e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_PCM_PARAMS_REPLY:
13253e0c72dSLiam Girdwood 			str2 = "PCM_REPLY"; break;
13353e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_PCM_FREE:
13453e0c72dSLiam Girdwood 			str2 = "PCM_FREE"; break;
13553e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_START:
13653e0c72dSLiam Girdwood 			str2 = "TRIG_START"; break;
13753e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_STOP:
13853e0c72dSLiam Girdwood 			str2 = "TRIG_STOP"; break;
13953e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_PAUSE:
14053e0c72dSLiam Girdwood 			str2 = "TRIG_PAUSE"; break;
14153e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_RELEASE:
14253e0c72dSLiam Girdwood 			str2 = "TRIG_RELEASE"; break;
14353e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_DRAIN:
14453e0c72dSLiam Girdwood 			str2 = "TRIG_DRAIN"; break;
14553e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_XRUN:
14653e0c72dSLiam Girdwood 			str2 = "TRIG_XRUN"; break;
14753e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_POSITION:
14853e0c72dSLiam Girdwood 			str2 = "POSITION"; break;
14953e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_VORBIS_PARAMS:
15053e0c72dSLiam Girdwood 			str2 = "VORBIS_PARAMS"; break;
15153e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_VORBIS_FREE:
15253e0c72dSLiam Girdwood 			str2 = "VORBIS_FREE"; break;
15353e0c72dSLiam Girdwood 		default:
15453e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
15553e0c72dSLiam Girdwood 		}
15653e0c72dSLiam Girdwood 		break;
15753e0c72dSLiam Girdwood 	case SOF_IPC_FW_READY:
15853e0c72dSLiam Girdwood 		str = "FW_READY"; break;
15953e0c72dSLiam Girdwood 	case SOF_IPC_GLB_DAI_MSG:
16053e0c72dSLiam Girdwood 		str = "GLB_DAI_MSG";
16153e0c72dSLiam Girdwood 		switch (type) {
16253e0c72dSLiam Girdwood 		case SOF_IPC_DAI_CONFIG:
16353e0c72dSLiam Girdwood 			str2 = "CONFIG"; break;
16453e0c72dSLiam Girdwood 		case SOF_IPC_DAI_LOOPBACK:
16553e0c72dSLiam Girdwood 			str2 = "LOOPBACK"; break;
16653e0c72dSLiam Girdwood 		default:
16753e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
16853e0c72dSLiam Girdwood 		}
16953e0c72dSLiam Girdwood 		break;
17053e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TRACE_MSG:
17153e0c72dSLiam Girdwood 		str = "GLB_TRACE_MSG"; break;
17253b22d25SRanjani Sridharan 	case SOF_IPC_GLB_TEST_MSG:
17353b22d25SRanjani Sridharan 		str = "GLB_TEST_MSG";
17453b22d25SRanjani Sridharan 		switch (type) {
17553b22d25SRanjani Sridharan 		case SOF_IPC_TEST_IPC_FLOOD:
17653b22d25SRanjani Sridharan 			str2 = "IPC_FLOOD"; break;
17753b22d25SRanjani Sridharan 		default:
17853b22d25SRanjani Sridharan 			str2 = "unknown type"; break;
17953b22d25SRanjani Sridharan 		}
18053b22d25SRanjani Sridharan 		break;
18153e0c72dSLiam Girdwood 	default:
18253e0c72dSLiam Girdwood 		str = "unknown GLB command"; break;
18353e0c72dSLiam Girdwood 	}
18453e0c72dSLiam Girdwood 
18553e0c72dSLiam Girdwood 	if (str2)
18653e0c72dSLiam Girdwood 		dev_dbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
18753e0c72dSLiam Girdwood 	else
18853e0c72dSLiam Girdwood 		dev_dbg(dev, "%s: 0x%x: %s\n", text, cmd, str);
18953e0c72dSLiam Girdwood }
19053e0c72dSLiam Girdwood #else
19153e0c72dSLiam Girdwood static inline void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
19253e0c72dSLiam Girdwood {
1934cee883eSDragos Tarcatu 	if ((cmd & SOF_GLB_TYPE_MASK) != SOF_IPC_GLB_TRACE_MSG)
19453e0c72dSLiam Girdwood 		dev_dbg(dev, "%s: 0x%x\n", text, cmd);
19553e0c72dSLiam Girdwood }
19653e0c72dSLiam Girdwood #endif
19753e0c72dSLiam Girdwood 
19853e0c72dSLiam Girdwood /* wait for IPC message reply */
19953e0c72dSLiam Girdwood static int tx_wait_done(struct snd_sof_ipc *ipc, struct snd_sof_ipc_msg *msg,
20053e0c72dSLiam Girdwood 			void *reply_data)
20153e0c72dSLiam Girdwood {
20253e0c72dSLiam Girdwood 	struct snd_sof_dev *sdev = ipc->sdev;
20353e0c72dSLiam Girdwood 	struct sof_ipc_cmd_hdr *hdr = msg->msg_data;
20453e0c72dSLiam Girdwood 	int ret;
20553e0c72dSLiam Girdwood 
20653e0c72dSLiam Girdwood 	/* wait for DSP IPC completion */
20753e0c72dSLiam Girdwood 	ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
208*6dd78b31SKai Vehmanen 				 msecs_to_jiffies(sdev->ipc_timeout));
20953e0c72dSLiam Girdwood 
21053e0c72dSLiam Girdwood 	if (ret == 0) {
21153e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: ipc timed out for 0x%x size %d\n",
21253e0c72dSLiam Girdwood 			hdr->cmd, hdr->size);
21353e0c72dSLiam Girdwood 		snd_sof_dsp_dbg_dump(ipc->sdev, SOF_DBG_REGS | SOF_DBG_MBOX);
2145e4a27fdSPan Xiuli 		snd_sof_ipc_dump(ipc->sdev);
21553e0c72dSLiam Girdwood 		snd_sof_trace_notify_for_error(ipc->sdev);
21653e0c72dSLiam Girdwood 		ret = -ETIMEDOUT;
21753e0c72dSLiam Girdwood 	} else {
21853e0c72dSLiam Girdwood 		/* copy the data returned from DSP */
21953e0c72dSLiam Girdwood 		ret = msg->reply_error;
22053e0c72dSLiam Girdwood 		if (msg->reply_size)
22153e0c72dSLiam Girdwood 			memcpy(reply_data, msg->reply_data, msg->reply_size);
22253e0c72dSLiam Girdwood 		if (ret < 0)
22353e0c72dSLiam Girdwood 			dev_err(sdev->dev, "error: ipc error for 0x%x size %zu\n",
22453e0c72dSLiam Girdwood 				hdr->cmd, msg->reply_size);
22553e0c72dSLiam Girdwood 		else
22653e0c72dSLiam Girdwood 			ipc_log_header(sdev->dev, "ipc tx succeeded", hdr->cmd);
22753e0c72dSLiam Girdwood 	}
22853e0c72dSLiam Girdwood 
22953e0c72dSLiam Girdwood 	return ret;
23053e0c72dSLiam Girdwood }
23153e0c72dSLiam Girdwood 
23253e0c72dSLiam Girdwood /* send IPC message from host to DSP */
23353e0c72dSLiam Girdwood static int sof_ipc_tx_message_unlocked(struct snd_sof_ipc *ipc, u32 header,
23453e0c72dSLiam Girdwood 				       void *msg_data, size_t msg_bytes,
23553e0c72dSLiam Girdwood 				       void *reply_data, size_t reply_bytes)
23653e0c72dSLiam Girdwood {
23753e0c72dSLiam Girdwood 	struct snd_sof_dev *sdev = ipc->sdev;
23853e0c72dSLiam Girdwood 	struct snd_sof_ipc_msg *msg;
23953e0c72dSLiam Girdwood 	int ret;
24053e0c72dSLiam Girdwood 
24153e0c72dSLiam Girdwood 	if (ipc->disable_ipc_tx)
24253e0c72dSLiam Girdwood 		return -ENODEV;
24353e0c72dSLiam Girdwood 
24453e0c72dSLiam Girdwood 	/*
24553e0c72dSLiam Girdwood 	 * The spin-lock is also still needed to protect message objects against
24653e0c72dSLiam Girdwood 	 * other atomic contexts.
24753e0c72dSLiam Girdwood 	 */
24853e0c72dSLiam Girdwood 	spin_lock_irq(&sdev->ipc_lock);
24953e0c72dSLiam Girdwood 
25053e0c72dSLiam Girdwood 	/* initialise the message */
25153e0c72dSLiam Girdwood 	msg = &ipc->msg;
25253e0c72dSLiam Girdwood 
25353e0c72dSLiam Girdwood 	msg->header = header;
25453e0c72dSLiam Girdwood 	msg->msg_size = msg_bytes;
25553e0c72dSLiam Girdwood 	msg->reply_size = reply_bytes;
25653e0c72dSLiam Girdwood 	msg->reply_error = 0;
25753e0c72dSLiam Girdwood 
25853e0c72dSLiam Girdwood 	/* attach any data */
25953e0c72dSLiam Girdwood 	if (msg_bytes)
26053e0c72dSLiam Girdwood 		memcpy(msg->msg_data, msg_data, msg_bytes);
26153e0c72dSLiam Girdwood 
26253e0c72dSLiam Girdwood 	sdev->msg = msg;
26353e0c72dSLiam Girdwood 
26453e0c72dSLiam Girdwood 	ret = snd_sof_dsp_send_msg(sdev, msg);
26553e0c72dSLiam Girdwood 	/* Next reply that we receive will be related to this message */
26653e0c72dSLiam Girdwood 	if (!ret)
26753e0c72dSLiam Girdwood 		msg->ipc_complete = false;
26853e0c72dSLiam Girdwood 
26953e0c72dSLiam Girdwood 	spin_unlock_irq(&sdev->ipc_lock);
27053e0c72dSLiam Girdwood 
27153e0c72dSLiam Girdwood 	if (ret < 0) {
27253e0c72dSLiam Girdwood 		/* So far IPC TX never fails, consider making the above void */
27353e0c72dSLiam Girdwood 		dev_err_ratelimited(sdev->dev,
27453e0c72dSLiam Girdwood 				    "error: ipc tx failed with error %d\n",
27553e0c72dSLiam Girdwood 				    ret);
27653e0c72dSLiam Girdwood 		return ret;
27753e0c72dSLiam Girdwood 	}
27853e0c72dSLiam Girdwood 
27953e0c72dSLiam Girdwood 	ipc_log_header(sdev->dev, "ipc tx", msg->header);
28053e0c72dSLiam Girdwood 
28153e0c72dSLiam Girdwood 	/* now wait for completion */
28253e0c72dSLiam Girdwood 	if (!ret)
28353e0c72dSLiam Girdwood 		ret = tx_wait_done(ipc, msg, reply_data);
28453e0c72dSLiam Girdwood 
28553e0c72dSLiam Girdwood 	return ret;
28653e0c72dSLiam Girdwood }
28753e0c72dSLiam Girdwood 
28853e0c72dSLiam Girdwood /* send IPC message from host to DSP */
28953e0c72dSLiam Girdwood int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
29053e0c72dSLiam Girdwood 		       void *msg_data, size_t msg_bytes, void *reply_data,
29153e0c72dSLiam Girdwood 		       size_t reply_bytes)
29253e0c72dSLiam Girdwood {
29353e0c72dSLiam Girdwood 	int ret;
29453e0c72dSLiam Girdwood 
29553e0c72dSLiam Girdwood 	if (msg_bytes > SOF_IPC_MSG_MAX_SIZE ||
29653e0c72dSLiam Girdwood 	    reply_bytes > SOF_IPC_MSG_MAX_SIZE)
29753e0c72dSLiam Girdwood 		return -ENOBUFS;
29853e0c72dSLiam Girdwood 
29953e0c72dSLiam Girdwood 	/* Serialise IPC TX */
30053e0c72dSLiam Girdwood 	mutex_lock(&ipc->tx_mutex);
30153e0c72dSLiam Girdwood 
30253e0c72dSLiam Girdwood 	ret = sof_ipc_tx_message_unlocked(ipc, header, msg_data, msg_bytes,
30353e0c72dSLiam Girdwood 					  reply_data, reply_bytes);
30453e0c72dSLiam Girdwood 
30553e0c72dSLiam Girdwood 	mutex_unlock(&ipc->tx_mutex);
30653e0c72dSLiam Girdwood 
30753e0c72dSLiam Girdwood 	return ret;
30853e0c72dSLiam Girdwood }
30953e0c72dSLiam Girdwood EXPORT_SYMBOL(sof_ipc_tx_message);
31053e0c72dSLiam Girdwood 
31153e0c72dSLiam Girdwood /* handle reply message from DSP */
31253e0c72dSLiam Girdwood int snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id)
31353e0c72dSLiam Girdwood {
31453e0c72dSLiam Girdwood 	struct snd_sof_ipc_msg *msg = &sdev->ipc->msg;
31553e0c72dSLiam Girdwood 
31653e0c72dSLiam Girdwood 	if (msg->ipc_complete) {
31753e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: no reply expected, received 0x%x",
31853e0c72dSLiam Girdwood 			msg_id);
31953e0c72dSLiam Girdwood 		return -EINVAL;
32053e0c72dSLiam Girdwood 	}
32153e0c72dSLiam Girdwood 
32253e0c72dSLiam Girdwood 	/* wake up and return the error if we have waiters on this message ? */
32353e0c72dSLiam Girdwood 	msg->ipc_complete = true;
32453e0c72dSLiam Girdwood 	wake_up(&msg->waitq);
32553e0c72dSLiam Girdwood 
32653e0c72dSLiam Girdwood 	return 0;
32753e0c72dSLiam Girdwood }
32853e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_reply);
32953e0c72dSLiam Girdwood 
33053e0c72dSLiam Girdwood /* DSP firmware has sent host a message  */
33153e0c72dSLiam Girdwood void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev)
33253e0c72dSLiam Girdwood {
33353e0c72dSLiam Girdwood 	struct sof_ipc_cmd_hdr hdr;
33453e0c72dSLiam Girdwood 	u32 cmd, type;
33553e0c72dSLiam Girdwood 	int err = 0;
33653e0c72dSLiam Girdwood 
33753e0c72dSLiam Girdwood 	/* read back header */
33853e0c72dSLiam Girdwood 	snd_sof_ipc_msg_data(sdev, NULL, &hdr, sizeof(hdr));
33953e0c72dSLiam Girdwood 	ipc_log_header(sdev->dev, "ipc rx", hdr.cmd);
34053e0c72dSLiam Girdwood 
34153e0c72dSLiam Girdwood 	cmd = hdr.cmd & SOF_GLB_TYPE_MASK;
34253e0c72dSLiam Girdwood 	type = hdr.cmd & SOF_CMD_TYPE_MASK;
34353e0c72dSLiam Girdwood 
34453e0c72dSLiam Girdwood 	/* check message type */
34553e0c72dSLiam Girdwood 	switch (cmd) {
34653e0c72dSLiam Girdwood 	case SOF_IPC_GLB_REPLY:
34753e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: ipc reply unknown\n");
34853e0c72dSLiam Girdwood 		break;
34953e0c72dSLiam Girdwood 	case SOF_IPC_FW_READY:
35053e0c72dSLiam Girdwood 		/* check for FW boot completion */
35153e0c72dSLiam Girdwood 		if (!sdev->boot_complete) {
35253e0c72dSLiam Girdwood 			err = sof_ops(sdev)->fw_ready(sdev, cmd);
35353e0c72dSLiam Girdwood 			if (err < 0) {
35453e0c72dSLiam Girdwood 				/*
35553e0c72dSLiam Girdwood 				 * this indicates a mismatch in ABI
35653e0c72dSLiam Girdwood 				 * between the driver and fw
35753e0c72dSLiam Girdwood 				 */
35853e0c72dSLiam Girdwood 				dev_err(sdev->dev, "error: ABI mismatch %d\n",
35953e0c72dSLiam Girdwood 					err);
36053e0c72dSLiam Girdwood 			} else {
36153e0c72dSLiam Girdwood 				/* firmware boot completed OK */
36253e0c72dSLiam Girdwood 				sdev->boot_complete = true;
36353e0c72dSLiam Girdwood 			}
36453e0c72dSLiam Girdwood 
36553e0c72dSLiam Girdwood 			/* wake up firmware loader */
36653e0c72dSLiam Girdwood 			wake_up(&sdev->boot_wait);
36753e0c72dSLiam Girdwood 		}
36853e0c72dSLiam Girdwood 		break;
36953e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMPOUND:
37053e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TPLG_MSG:
37153e0c72dSLiam Girdwood 	case SOF_IPC_GLB_PM_MSG:
37253e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMP_MSG:
37353e0c72dSLiam Girdwood 		break;
37453e0c72dSLiam Girdwood 	case SOF_IPC_GLB_STREAM_MSG:
37553e0c72dSLiam Girdwood 		/* need to pass msg id into the function */
37653e0c72dSLiam Girdwood 		ipc_stream_message(sdev, hdr.cmd);
37753e0c72dSLiam Girdwood 		break;
37853e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TRACE_MSG:
37953e0c72dSLiam Girdwood 		ipc_trace_message(sdev, type);
38053e0c72dSLiam Girdwood 		break;
38153e0c72dSLiam Girdwood 	default:
38253e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: unknown DSP message 0x%x\n", cmd);
38353e0c72dSLiam Girdwood 		break;
38453e0c72dSLiam Girdwood 	}
38553e0c72dSLiam Girdwood 
38653e0c72dSLiam Girdwood 	ipc_log_header(sdev->dev, "ipc rx done", hdr.cmd);
38753e0c72dSLiam Girdwood }
38853e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_msgs_rx);
38953e0c72dSLiam Girdwood 
39053e0c72dSLiam Girdwood /*
39153e0c72dSLiam Girdwood  * IPC trace mechanism.
39253e0c72dSLiam Girdwood  */
39353e0c72dSLiam Girdwood 
39453e0c72dSLiam Girdwood static void ipc_trace_message(struct snd_sof_dev *sdev, u32 msg_id)
39553e0c72dSLiam Girdwood {
39653e0c72dSLiam Girdwood 	struct sof_ipc_dma_trace_posn posn;
39753e0c72dSLiam Girdwood 
39853e0c72dSLiam Girdwood 	switch (msg_id) {
39953e0c72dSLiam Girdwood 	case SOF_IPC_TRACE_DMA_POSITION:
40053e0c72dSLiam Girdwood 		/* read back full message */
40153e0c72dSLiam Girdwood 		snd_sof_ipc_msg_data(sdev, NULL, &posn, sizeof(posn));
40253e0c72dSLiam Girdwood 		snd_sof_trace_update_pos(sdev, &posn);
40353e0c72dSLiam Girdwood 		break;
40453e0c72dSLiam Girdwood 	default:
40553e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: unhandled trace message %x\n",
40653e0c72dSLiam Girdwood 			msg_id);
40753e0c72dSLiam Girdwood 		break;
40853e0c72dSLiam Girdwood 	}
40953e0c72dSLiam Girdwood }
41053e0c72dSLiam Girdwood 
41153e0c72dSLiam Girdwood /*
41253e0c72dSLiam Girdwood  * IPC stream position.
41353e0c72dSLiam Girdwood  */
41453e0c72dSLiam Girdwood 
41553e0c72dSLiam Girdwood static void ipc_period_elapsed(struct snd_sof_dev *sdev, u32 msg_id)
41653e0c72dSLiam Girdwood {
41753e0c72dSLiam Girdwood 	struct snd_sof_pcm_stream *stream;
41853e0c72dSLiam Girdwood 	struct sof_ipc_stream_posn posn;
41953e0c72dSLiam Girdwood 	struct snd_sof_pcm *spcm;
42053e0c72dSLiam Girdwood 	int direction;
42153e0c72dSLiam Girdwood 
42253e0c72dSLiam Girdwood 	spcm = snd_sof_find_spcm_comp(sdev, msg_id, &direction);
42353e0c72dSLiam Girdwood 	if (!spcm) {
42453e0c72dSLiam Girdwood 		dev_err(sdev->dev,
42553e0c72dSLiam Girdwood 			"error: period elapsed for unknown stream, msg_id %d\n",
42653e0c72dSLiam Girdwood 			msg_id);
42753e0c72dSLiam Girdwood 		return;
42853e0c72dSLiam Girdwood 	}
42953e0c72dSLiam Girdwood 
43053e0c72dSLiam Girdwood 	stream = &spcm->stream[direction];
43153e0c72dSLiam Girdwood 	snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
43253e0c72dSLiam Girdwood 
43353e0c72dSLiam Girdwood 	dev_dbg(sdev->dev, "posn : host 0x%llx dai 0x%llx wall 0x%llx\n",
43453e0c72dSLiam Girdwood 		posn.host_posn, posn.dai_posn, posn.wallclock);
43553e0c72dSLiam Girdwood 
43653e0c72dSLiam Girdwood 	memcpy(&stream->posn, &posn, sizeof(posn));
43753e0c72dSLiam Girdwood 
43853e0c72dSLiam Girdwood 	/* only inform ALSA for period_wakeup mode */
43953e0c72dSLiam Girdwood 	if (!stream->substream->runtime->no_period_wakeup)
440ff758e9fSKeyon Jie 		snd_sof_pcm_period_elapsed(stream->substream);
44153e0c72dSLiam Girdwood }
44253e0c72dSLiam Girdwood 
44353e0c72dSLiam Girdwood /* DSP notifies host of an XRUN within FW */
44453e0c72dSLiam Girdwood static void ipc_xrun(struct snd_sof_dev *sdev, u32 msg_id)
44553e0c72dSLiam Girdwood {
44653e0c72dSLiam Girdwood 	struct snd_sof_pcm_stream *stream;
44753e0c72dSLiam Girdwood 	struct sof_ipc_stream_posn posn;
44853e0c72dSLiam Girdwood 	struct snd_sof_pcm *spcm;
44953e0c72dSLiam Girdwood 	int direction;
45053e0c72dSLiam Girdwood 
45153e0c72dSLiam Girdwood 	spcm = snd_sof_find_spcm_comp(sdev, msg_id, &direction);
45253e0c72dSLiam Girdwood 	if (!spcm) {
45353e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: XRUN for unknown stream, msg_id %d\n",
45453e0c72dSLiam Girdwood 			msg_id);
45553e0c72dSLiam Girdwood 		return;
45653e0c72dSLiam Girdwood 	}
45753e0c72dSLiam Girdwood 
45853e0c72dSLiam Girdwood 	stream = &spcm->stream[direction];
45953e0c72dSLiam Girdwood 	snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
46053e0c72dSLiam Girdwood 
46153e0c72dSLiam Girdwood 	dev_dbg(sdev->dev,  "posn XRUN: host %llx comp %d size %d\n",
46253e0c72dSLiam Girdwood 		posn.host_posn, posn.xrun_comp_id, posn.xrun_size);
46353e0c72dSLiam Girdwood 
46453e0c72dSLiam Girdwood #if defined(CONFIG_SND_SOC_SOF_DEBUG_XRUN_STOP)
46553e0c72dSLiam Girdwood 	/* stop PCM on XRUN - used for pipeline debug */
46653e0c72dSLiam Girdwood 	memcpy(&stream->posn, &posn, sizeof(posn));
46753e0c72dSLiam Girdwood 	snd_pcm_stop_xrun(stream->substream);
46853e0c72dSLiam Girdwood #endif
46953e0c72dSLiam Girdwood }
47053e0c72dSLiam Girdwood 
47153e0c72dSLiam Girdwood /* stream notifications from DSP FW */
47253e0c72dSLiam Girdwood static void ipc_stream_message(struct snd_sof_dev *sdev, u32 msg_cmd)
47353e0c72dSLiam Girdwood {
47453e0c72dSLiam Girdwood 	/* get msg cmd type and msd id */
47553e0c72dSLiam Girdwood 	u32 msg_type = msg_cmd & SOF_CMD_TYPE_MASK;
47653e0c72dSLiam Girdwood 	u32 msg_id = SOF_IPC_MESSAGE_ID(msg_cmd);
47753e0c72dSLiam Girdwood 
47853e0c72dSLiam Girdwood 	switch (msg_type) {
47953e0c72dSLiam Girdwood 	case SOF_IPC_STREAM_POSITION:
48053e0c72dSLiam Girdwood 		ipc_period_elapsed(sdev, msg_id);
48153e0c72dSLiam Girdwood 		break;
48253e0c72dSLiam Girdwood 	case SOF_IPC_STREAM_TRIG_XRUN:
48353e0c72dSLiam Girdwood 		ipc_xrun(sdev, msg_id);
48453e0c72dSLiam Girdwood 		break;
48553e0c72dSLiam Girdwood 	default:
48653e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: unhandled stream message %x\n",
48753e0c72dSLiam Girdwood 			msg_id);
48853e0c72dSLiam Girdwood 		break;
48953e0c72dSLiam Girdwood 	}
49053e0c72dSLiam Girdwood }
49153e0c72dSLiam Girdwood 
49253e0c72dSLiam Girdwood /* get stream position IPC - use faster MMIO method if available on platform */
49353e0c72dSLiam Girdwood int snd_sof_ipc_stream_posn(struct snd_sof_dev *sdev,
49453e0c72dSLiam Girdwood 			    struct snd_sof_pcm *spcm, int direction,
49553e0c72dSLiam Girdwood 			    struct sof_ipc_stream_posn *posn)
49653e0c72dSLiam Girdwood {
49753e0c72dSLiam Girdwood 	struct sof_ipc_stream stream;
49853e0c72dSLiam Girdwood 	int err;
49953e0c72dSLiam Girdwood 
50053e0c72dSLiam Girdwood 	/* read position via slower IPC */
50153e0c72dSLiam Girdwood 	stream.hdr.size = sizeof(stream);
50253e0c72dSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_POSITION;
50353e0c72dSLiam Girdwood 	stream.comp_id = spcm->stream[direction].comp_id;
50453e0c72dSLiam Girdwood 
50553e0c72dSLiam Girdwood 	/* send IPC to the DSP */
50653e0c72dSLiam Girdwood 	err = sof_ipc_tx_message(sdev->ipc,
50753e0c72dSLiam Girdwood 				 stream.hdr.cmd, &stream, sizeof(stream), &posn,
50853e0c72dSLiam Girdwood 				 sizeof(*posn));
50953e0c72dSLiam Girdwood 	if (err < 0) {
51053e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: failed to get stream %d position\n",
51153e0c72dSLiam Girdwood 			stream.comp_id);
51253e0c72dSLiam Girdwood 		return err;
51353e0c72dSLiam Girdwood 	}
51453e0c72dSLiam Girdwood 
51553e0c72dSLiam Girdwood 	return 0;
51653e0c72dSLiam Girdwood }
51753e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_stream_posn);
51853e0c72dSLiam Girdwood 
51953e0c72dSLiam Girdwood static int sof_get_ctrl_copy_params(enum sof_ipc_ctrl_type ctrl_type,
52053e0c72dSLiam Girdwood 				    struct sof_ipc_ctrl_data *src,
52153e0c72dSLiam Girdwood 				    struct sof_ipc_ctrl_data *dst,
52253e0c72dSLiam Girdwood 				    struct sof_ipc_ctrl_data_params *sparams)
52353e0c72dSLiam Girdwood {
52453e0c72dSLiam Girdwood 	switch (ctrl_type) {
52553e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_GET:
52653e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_SET:
52753e0c72dSLiam Girdwood 		sparams->src = (u8 *)src->chanv;
52853e0c72dSLiam Girdwood 		sparams->dst = (u8 *)dst->chanv;
52953e0c72dSLiam Girdwood 		break;
53053e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_COMP_GET:
53153e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_COMP_SET:
53253e0c72dSLiam Girdwood 		sparams->src = (u8 *)src->compv;
53353e0c72dSLiam Girdwood 		sparams->dst = (u8 *)dst->compv;
53453e0c72dSLiam Girdwood 		break;
53553e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_GET:
53653e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_SET:
53753e0c72dSLiam Girdwood 		sparams->src = (u8 *)src->data->data;
53853e0c72dSLiam Girdwood 		sparams->dst = (u8 *)dst->data->data;
53953e0c72dSLiam Girdwood 		break;
54053e0c72dSLiam Girdwood 	default:
54153e0c72dSLiam Girdwood 		return -EINVAL;
54253e0c72dSLiam Girdwood 	}
54353e0c72dSLiam Girdwood 
54453e0c72dSLiam Girdwood 	/* calculate payload size and number of messages */
54553e0c72dSLiam Girdwood 	sparams->pl_size = SOF_IPC_MSG_MAX_SIZE - sparams->hdr_bytes;
54653e0c72dSLiam Girdwood 	sparams->num_msg = DIV_ROUND_UP(sparams->msg_bytes, sparams->pl_size);
54753e0c72dSLiam Girdwood 
54853e0c72dSLiam Girdwood 	return 0;
54953e0c72dSLiam Girdwood }
55053e0c72dSLiam Girdwood 
55153e0c72dSLiam Girdwood static int sof_set_get_large_ctrl_data(struct snd_sof_dev *sdev,
55253e0c72dSLiam Girdwood 				       struct sof_ipc_ctrl_data *cdata,
55353e0c72dSLiam Girdwood 				       struct sof_ipc_ctrl_data_params *sparams,
55453e0c72dSLiam Girdwood 				       bool send)
55553e0c72dSLiam Girdwood {
55653e0c72dSLiam Girdwood 	struct sof_ipc_ctrl_data *partdata;
55753e0c72dSLiam Girdwood 	size_t send_bytes;
55853e0c72dSLiam Girdwood 	size_t offset = 0;
55953e0c72dSLiam Girdwood 	size_t msg_bytes;
56053e0c72dSLiam Girdwood 	size_t pl_size;
56154d198d5STakashi Iwai 	int err;
56253e0c72dSLiam Girdwood 	int i;
56353e0c72dSLiam Girdwood 
56453e0c72dSLiam Girdwood 	/* allocate max ipc size because we have at least one */
56553e0c72dSLiam Girdwood 	partdata = kzalloc(SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL);
56653e0c72dSLiam Girdwood 	if (!partdata)
56753e0c72dSLiam Girdwood 		return -ENOMEM;
56853e0c72dSLiam Girdwood 
56953e0c72dSLiam Girdwood 	if (send)
57054d198d5STakashi Iwai 		err = sof_get_ctrl_copy_params(cdata->type, cdata, partdata,
57154d198d5STakashi Iwai 					       sparams);
57253e0c72dSLiam Girdwood 	else
57354d198d5STakashi Iwai 		err = sof_get_ctrl_copy_params(cdata->type, partdata, cdata,
57454d198d5STakashi Iwai 					       sparams);
57554d198d5STakashi Iwai 	if (err < 0)
57654d198d5STakashi Iwai 		return err;
57753e0c72dSLiam Girdwood 
57853e0c72dSLiam Girdwood 	msg_bytes = sparams->msg_bytes;
57953e0c72dSLiam Girdwood 	pl_size = sparams->pl_size;
58053e0c72dSLiam Girdwood 
58153e0c72dSLiam Girdwood 	/* copy the header data */
58253e0c72dSLiam Girdwood 	memcpy(partdata, cdata, sparams->hdr_bytes);
58353e0c72dSLiam Girdwood 
58453e0c72dSLiam Girdwood 	/* Serialise IPC TX */
58553e0c72dSLiam Girdwood 	mutex_lock(&sdev->ipc->tx_mutex);
58653e0c72dSLiam Girdwood 
58753e0c72dSLiam Girdwood 	/* copy the payload data in a loop */
58853e0c72dSLiam Girdwood 	for (i = 0; i < sparams->num_msg; i++) {
58953e0c72dSLiam Girdwood 		send_bytes = min(msg_bytes, pl_size);
59053e0c72dSLiam Girdwood 		partdata->num_elems = send_bytes;
59153e0c72dSLiam Girdwood 		partdata->rhdr.hdr.size = sparams->hdr_bytes + send_bytes;
59253e0c72dSLiam Girdwood 		partdata->msg_index = i;
59353e0c72dSLiam Girdwood 		msg_bytes -= send_bytes;
59453e0c72dSLiam Girdwood 		partdata->elems_remaining = msg_bytes;
59553e0c72dSLiam Girdwood 
59653e0c72dSLiam Girdwood 		if (send)
59753e0c72dSLiam Girdwood 			memcpy(sparams->dst, sparams->src + offset, send_bytes);
59853e0c72dSLiam Girdwood 
59953e0c72dSLiam Girdwood 		err = sof_ipc_tx_message_unlocked(sdev->ipc,
60053e0c72dSLiam Girdwood 						  partdata->rhdr.hdr.cmd,
60153e0c72dSLiam Girdwood 						  partdata,
60253e0c72dSLiam Girdwood 						  partdata->rhdr.hdr.size,
60353e0c72dSLiam Girdwood 						  partdata,
60453e0c72dSLiam Girdwood 						  partdata->rhdr.hdr.size);
60553e0c72dSLiam Girdwood 		if (err < 0)
60653e0c72dSLiam Girdwood 			break;
60753e0c72dSLiam Girdwood 
60853e0c72dSLiam Girdwood 		if (!send)
60953e0c72dSLiam Girdwood 			memcpy(sparams->dst + offset, sparams->src, send_bytes);
61053e0c72dSLiam Girdwood 
61153e0c72dSLiam Girdwood 		offset += pl_size;
61253e0c72dSLiam Girdwood 	}
61353e0c72dSLiam Girdwood 
61453e0c72dSLiam Girdwood 	mutex_unlock(&sdev->ipc->tx_mutex);
61553e0c72dSLiam Girdwood 
61653e0c72dSLiam Girdwood 	kfree(partdata);
61753e0c72dSLiam Girdwood 	return err;
61853e0c72dSLiam Girdwood }
61953e0c72dSLiam Girdwood 
62053e0c72dSLiam Girdwood /*
62153e0c72dSLiam Girdwood  * IPC get()/set() for kcontrols.
62253e0c72dSLiam Girdwood  */
62353e0c72dSLiam Girdwood int snd_sof_ipc_set_get_comp_data(struct snd_sof_ipc *ipc,
62453e0c72dSLiam Girdwood 				  struct snd_sof_control *scontrol,
62553e0c72dSLiam Girdwood 				  u32 ipc_cmd,
62653e0c72dSLiam Girdwood 				  enum sof_ipc_ctrl_type ctrl_type,
62753e0c72dSLiam Girdwood 				  enum sof_ipc_ctrl_cmd ctrl_cmd,
62853e0c72dSLiam Girdwood 				  bool send)
62953e0c72dSLiam Girdwood {
63053e0c72dSLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
63153e0c72dSLiam Girdwood 	struct snd_sof_dev *sdev = ipc->sdev;
63253e0c72dSLiam Girdwood 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
63353e0c72dSLiam Girdwood 	struct sof_ipc_fw_version *v = &ready->version;
63453e0c72dSLiam Girdwood 	struct sof_ipc_ctrl_data_params sparams;
63553e0c72dSLiam Girdwood 	size_t send_bytes;
63653e0c72dSLiam Girdwood 	int err;
63753e0c72dSLiam Girdwood 
63853e0c72dSLiam Girdwood 	/* read or write firmware volume */
63953e0c72dSLiam Girdwood 	if (scontrol->readback_offset != 0) {
64053e0c72dSLiam Girdwood 		/* write/read value header via mmaped region */
64153e0c72dSLiam Girdwood 		send_bytes = sizeof(struct sof_ipc_ctrl_value_chan) *
64253e0c72dSLiam Girdwood 		cdata->num_elems;
64353e0c72dSLiam Girdwood 		if (send)
64453e0c72dSLiam Girdwood 			snd_sof_dsp_block_write(sdev, sdev->mmio_bar,
64553e0c72dSLiam Girdwood 						scontrol->readback_offset,
64653e0c72dSLiam Girdwood 						cdata->chanv, send_bytes);
64753e0c72dSLiam Girdwood 
64853e0c72dSLiam Girdwood 		else
64953e0c72dSLiam Girdwood 			snd_sof_dsp_block_read(sdev, sdev->mmio_bar,
65053e0c72dSLiam Girdwood 					       scontrol->readback_offset,
65153e0c72dSLiam Girdwood 					       cdata->chanv, send_bytes);
65253e0c72dSLiam Girdwood 		return 0;
65353e0c72dSLiam Girdwood 	}
65453e0c72dSLiam Girdwood 
65553e0c72dSLiam Girdwood 	cdata->rhdr.hdr.cmd = SOF_IPC_GLB_COMP_MSG | ipc_cmd;
65653e0c72dSLiam Girdwood 	cdata->cmd = ctrl_cmd;
65753e0c72dSLiam Girdwood 	cdata->type = ctrl_type;
65853e0c72dSLiam Girdwood 	cdata->comp_id = scontrol->comp_id;
65953e0c72dSLiam Girdwood 	cdata->msg_index = 0;
66053e0c72dSLiam Girdwood 
66153e0c72dSLiam Girdwood 	/* calculate header and data size */
66253e0c72dSLiam Girdwood 	switch (cdata->type) {
66353e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_GET:
66453e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_SET:
66553e0c72dSLiam Girdwood 		sparams.msg_bytes = scontrol->num_channels *
66653e0c72dSLiam Girdwood 			sizeof(struct sof_ipc_ctrl_value_chan);
66753e0c72dSLiam Girdwood 		sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data);
66853e0c72dSLiam Girdwood 		sparams.elems = scontrol->num_channels;
66953e0c72dSLiam Girdwood 		break;
67053e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_COMP_GET:
67153e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_COMP_SET:
67253e0c72dSLiam Girdwood 		sparams.msg_bytes = scontrol->num_channels *
67353e0c72dSLiam Girdwood 			sizeof(struct sof_ipc_ctrl_value_comp);
67453e0c72dSLiam Girdwood 		sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data);
67553e0c72dSLiam Girdwood 		sparams.elems = scontrol->num_channels;
67653e0c72dSLiam Girdwood 		break;
67753e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_GET:
67853e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_SET:
67953e0c72dSLiam Girdwood 		sparams.msg_bytes = cdata->data->size;
68053e0c72dSLiam Girdwood 		sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data) +
68153e0c72dSLiam Girdwood 			sizeof(struct sof_abi_hdr);
68253e0c72dSLiam Girdwood 		sparams.elems = cdata->data->size;
68353e0c72dSLiam Girdwood 		break;
68453e0c72dSLiam Girdwood 	default:
68553e0c72dSLiam Girdwood 		return -EINVAL;
68653e0c72dSLiam Girdwood 	}
68753e0c72dSLiam Girdwood 
68853e0c72dSLiam Girdwood 	cdata->rhdr.hdr.size = sparams.msg_bytes + sparams.hdr_bytes;
68953e0c72dSLiam Girdwood 	cdata->num_elems = sparams.elems;
69053e0c72dSLiam Girdwood 	cdata->elems_remaining = 0;
69153e0c72dSLiam Girdwood 
69253e0c72dSLiam Girdwood 	/* send normal size ipc in one part */
69353e0c72dSLiam Girdwood 	if (cdata->rhdr.hdr.size <= SOF_IPC_MSG_MAX_SIZE) {
69453e0c72dSLiam Girdwood 		err = sof_ipc_tx_message(sdev->ipc, cdata->rhdr.hdr.cmd, cdata,
69553e0c72dSLiam Girdwood 					 cdata->rhdr.hdr.size, cdata,
69653e0c72dSLiam Girdwood 					 cdata->rhdr.hdr.size);
69753e0c72dSLiam Girdwood 
69853e0c72dSLiam Girdwood 		if (err < 0)
69953e0c72dSLiam Girdwood 			dev_err(sdev->dev, "error: set/get ctrl ipc comp %d\n",
70053e0c72dSLiam Girdwood 				cdata->comp_id);
70153e0c72dSLiam Girdwood 
70253e0c72dSLiam Girdwood 		return err;
70353e0c72dSLiam Girdwood 	}
70453e0c72dSLiam Girdwood 
70553e0c72dSLiam Girdwood 	/* data is bigger than max ipc size, chop into smaller pieces */
70653e0c72dSLiam Girdwood 	dev_dbg(sdev->dev, "large ipc size %u, control size %u\n",
70753e0c72dSLiam Girdwood 		cdata->rhdr.hdr.size, scontrol->size);
70853e0c72dSLiam Girdwood 
70953e0c72dSLiam Girdwood 	/* large messages is only supported from ABI 3.3.0 onwards */
71053e0c72dSLiam Girdwood 	if (v->abi_version < SOF_ABI_VER(3, 3, 0)) {
71153e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: incompatible FW ABI version\n");
71253e0c72dSLiam Girdwood 		return -EINVAL;
71353e0c72dSLiam Girdwood 	}
71453e0c72dSLiam Girdwood 
71553e0c72dSLiam Girdwood 	err = sof_set_get_large_ctrl_data(sdev, cdata, &sparams, send);
71653e0c72dSLiam Girdwood 
71753e0c72dSLiam Girdwood 	if (err < 0)
71853e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: set/get large ctrl ipc comp %d\n",
71953e0c72dSLiam Girdwood 			cdata->comp_id);
72053e0c72dSLiam Girdwood 
72153e0c72dSLiam Girdwood 	return err;
72253e0c72dSLiam Girdwood }
72353e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_set_get_comp_data);
72453e0c72dSLiam Girdwood 
72553e0c72dSLiam Girdwood /*
72653e0c72dSLiam Girdwood  * IPC layer enumeration.
72753e0c72dSLiam Girdwood  */
72853e0c72dSLiam Girdwood 
72953e0c72dSLiam Girdwood int snd_sof_dsp_mailbox_init(struct snd_sof_dev *sdev, u32 dspbox,
73053e0c72dSLiam Girdwood 			     size_t dspbox_size, u32 hostbox,
73153e0c72dSLiam Girdwood 			     size_t hostbox_size)
73253e0c72dSLiam Girdwood {
73353e0c72dSLiam Girdwood 	sdev->dsp_box.offset = dspbox;
73453e0c72dSLiam Girdwood 	sdev->dsp_box.size = dspbox_size;
73553e0c72dSLiam Girdwood 	sdev->host_box.offset = hostbox;
73653e0c72dSLiam Girdwood 	sdev->host_box.size = hostbox_size;
73753e0c72dSLiam Girdwood 	return 0;
73853e0c72dSLiam Girdwood }
73953e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_dsp_mailbox_init);
74053e0c72dSLiam Girdwood 
74153e0c72dSLiam Girdwood int snd_sof_ipc_valid(struct snd_sof_dev *sdev)
74253e0c72dSLiam Girdwood {
74353e0c72dSLiam Girdwood 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
74453e0c72dSLiam Girdwood 	struct sof_ipc_fw_version *v = &ready->version;
74553e0c72dSLiam Girdwood 
74653e0c72dSLiam Girdwood 	dev_info(sdev->dev,
74753e0c72dSLiam Girdwood 		 "Firmware info: version %d:%d:%d-%s\n",  v->major, v->minor,
74853e0c72dSLiam Girdwood 		 v->micro, v->tag);
74953e0c72dSLiam Girdwood 	dev_info(sdev->dev,
75053e0c72dSLiam Girdwood 		 "Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
75153e0c72dSLiam Girdwood 		 SOF_ABI_VERSION_MAJOR(v->abi_version),
75253e0c72dSLiam Girdwood 		 SOF_ABI_VERSION_MINOR(v->abi_version),
75353e0c72dSLiam Girdwood 		 SOF_ABI_VERSION_PATCH(v->abi_version),
75453e0c72dSLiam Girdwood 		 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
75553e0c72dSLiam Girdwood 
75653e0c72dSLiam Girdwood 	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) {
75753e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: incompatible FW ABI version\n");
75853e0c72dSLiam Girdwood 		return -EINVAL;
75953e0c72dSLiam Girdwood 	}
76053e0c72dSLiam Girdwood 
7614acb1c2eSPierre-Louis Bossart 	if (v->abi_version > SOF_ABI_VERSION) {
7624acb1c2eSPierre-Louis Bossart 		if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
7634acb1c2eSPierre-Louis Bossart 			dev_warn(sdev->dev, "warn: FW ABI is more recent than kernel\n");
7644acb1c2eSPierre-Louis Bossart 		} else {
7654acb1c2eSPierre-Louis Bossart 			dev_err(sdev->dev, "error: FW ABI is more recent than kernel\n");
7664acb1c2eSPierre-Louis Bossart 			return -EINVAL;
7674acb1c2eSPierre-Louis Bossart 		}
7684acb1c2eSPierre-Louis Bossart 	}
7694acb1c2eSPierre-Louis Bossart 
770347d1c4bSSlawomir Blauciak 	if (ready->flags & SOF_IPC_INFO_BUILD) {
77153e0c72dSLiam Girdwood 		dev_info(sdev->dev,
77253e0c72dSLiam Girdwood 			 "Firmware debug build %d on %s-%s - options:\n"
77353e0c72dSLiam Girdwood 			 " GDB: %s\n"
77453e0c72dSLiam Girdwood 			 " lock debug: %s\n"
77553e0c72dSLiam Girdwood 			 " lock vdebug: %s\n",
77653e0c72dSLiam Girdwood 			 v->build, v->date, v->time,
777847a040dSPierre-Louis Bossart 			 (ready->flags & SOF_IPC_INFO_GDB) ?
778347d1c4bSSlawomir Blauciak 				"enabled" : "disabled",
779847a040dSPierre-Louis Bossart 			 (ready->flags & SOF_IPC_INFO_LOCKS) ?
780347d1c4bSSlawomir Blauciak 				"enabled" : "disabled",
781847a040dSPierre-Louis Bossart 			 (ready->flags & SOF_IPC_INFO_LOCKSV) ?
782347d1c4bSSlawomir Blauciak 				"enabled" : "disabled");
78353e0c72dSLiam Girdwood 	}
78453e0c72dSLiam Girdwood 
78553e0c72dSLiam Girdwood 	/* copy the fw_version into debugfs at first boot */
78653e0c72dSLiam Girdwood 	memcpy(&sdev->fw_version, v, sizeof(*v));
78753e0c72dSLiam Girdwood 
78853e0c72dSLiam Girdwood 	return 0;
78953e0c72dSLiam Girdwood }
79053e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_valid);
79153e0c72dSLiam Girdwood 
79253e0c72dSLiam Girdwood struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev)
79353e0c72dSLiam Girdwood {
79453e0c72dSLiam Girdwood 	struct snd_sof_ipc *ipc;
79553e0c72dSLiam Girdwood 	struct snd_sof_ipc_msg *msg;
79653e0c72dSLiam Girdwood 
79753e0c72dSLiam Girdwood 	/* check if mandatory ops required for ipc are defined */
79853e0c72dSLiam Girdwood 	if (!sof_ops(sdev)->fw_ready) {
79953e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: ipc mandatory ops not defined\n");
80053e0c72dSLiam Girdwood 		return NULL;
80153e0c72dSLiam Girdwood 	}
80253e0c72dSLiam Girdwood 
80353e0c72dSLiam Girdwood 	ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL);
80453e0c72dSLiam Girdwood 	if (!ipc)
80553e0c72dSLiam Girdwood 		return NULL;
80653e0c72dSLiam Girdwood 
80753e0c72dSLiam Girdwood 	mutex_init(&ipc->tx_mutex);
80853e0c72dSLiam Girdwood 	ipc->sdev = sdev;
80953e0c72dSLiam Girdwood 	msg = &ipc->msg;
81053e0c72dSLiam Girdwood 
81153e0c72dSLiam Girdwood 	/* indicate that we aren't sending a message ATM */
81253e0c72dSLiam Girdwood 	msg->ipc_complete = true;
81353e0c72dSLiam Girdwood 
81453e0c72dSLiam Girdwood 	/* pre-allocate message data */
81553e0c72dSLiam Girdwood 	msg->msg_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE,
81653e0c72dSLiam Girdwood 				     GFP_KERNEL);
81753e0c72dSLiam Girdwood 	if (!msg->msg_data)
81853e0c72dSLiam Girdwood 		return NULL;
81953e0c72dSLiam Girdwood 
82053e0c72dSLiam Girdwood 	msg->reply_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE,
82153e0c72dSLiam Girdwood 				       GFP_KERNEL);
82253e0c72dSLiam Girdwood 	if (!msg->reply_data)
82353e0c72dSLiam Girdwood 		return NULL;
82453e0c72dSLiam Girdwood 
82553e0c72dSLiam Girdwood 	init_waitqueue_head(&msg->waitq);
82653e0c72dSLiam Girdwood 
82753e0c72dSLiam Girdwood 	return ipc;
82853e0c72dSLiam Girdwood }
82953e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_init);
83053e0c72dSLiam Girdwood 
83153e0c72dSLiam Girdwood void snd_sof_ipc_free(struct snd_sof_dev *sdev)
83253e0c72dSLiam Girdwood {
83353e0c72dSLiam Girdwood 	struct snd_sof_ipc *ipc = sdev->ipc;
83453e0c72dSLiam Girdwood 
83553e0c72dSLiam Girdwood 	/* disable sending of ipc's */
83653e0c72dSLiam Girdwood 	mutex_lock(&ipc->tx_mutex);
83753e0c72dSLiam Girdwood 	ipc->disable_ipc_tx = true;
83853e0c72dSLiam Girdwood 	mutex_unlock(&ipc->tx_mutex);
83953e0c72dSLiam Girdwood }
84053e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_free);
841