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