xref: /openbmc/linux/sound/soc/sof/ipc.c (revision 045bc49bc9572f883db1a0740cb36bf6eeb206db)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only 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"
18ee1e79b7SRanjani Sridharan #include "sof-audio.h"
1953e0c72dSLiam Girdwood #include "ops.h"
20657774acSRanjani Sridharan #include "ipc3-ops.h"
2153e0c72dSLiam Girdwood 
22ab3a2189SPeter Ujfalusi typedef void (*ipc_rx_callback)(struct snd_sof_dev *sdev, void *msg_buf);
23ab3a2189SPeter Ujfalusi 
24ab3a2189SPeter Ujfalusi static void ipc_trace_message(struct snd_sof_dev *sdev, void *msg_buf);
25ab3a2189SPeter Ujfalusi static void ipc_stream_message(struct snd_sof_dev *sdev, void *msg_buf);
2653e0c72dSLiam Girdwood 
2753e0c72dSLiam Girdwood /*
2853e0c72dSLiam Girdwood  * IPC message Tx/Rx message handling.
2953e0c72dSLiam Girdwood  */
3053e0c72dSLiam Girdwood 
3153e0c72dSLiam Girdwood struct sof_ipc_ctrl_data_params {
3253e0c72dSLiam Girdwood 	size_t msg_bytes;
3353e0c72dSLiam Girdwood 	size_t hdr_bytes;
3453e0c72dSLiam Girdwood 	size_t pl_size;
3553e0c72dSLiam Girdwood 	size_t elems;
3653e0c72dSLiam Girdwood 	u32 num_msg;
3753e0c72dSLiam Girdwood 	u8 *src;
3853e0c72dSLiam Girdwood 	u8 *dst;
3953e0c72dSLiam Girdwood };
4053e0c72dSLiam Girdwood 
4153e0c72dSLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_VERBOSE_IPC)
4253e0c72dSLiam Girdwood static void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
4353e0c72dSLiam Girdwood {
4453e0c72dSLiam Girdwood 	u8 *str;
4553e0c72dSLiam Girdwood 	u8 *str2 = NULL;
4653e0c72dSLiam Girdwood 	u32 glb;
4753e0c72dSLiam Girdwood 	u32 type;
48277ff236SPierre-Louis Bossart 	bool vdbg = false;
4953e0c72dSLiam Girdwood 
5053e0c72dSLiam Girdwood 	glb = cmd & SOF_GLB_TYPE_MASK;
5153e0c72dSLiam Girdwood 	type = cmd & SOF_CMD_TYPE_MASK;
5253e0c72dSLiam Girdwood 
5353e0c72dSLiam Girdwood 	switch (glb) {
5453e0c72dSLiam Girdwood 	case SOF_IPC_GLB_REPLY:
5553e0c72dSLiam Girdwood 		str = "GLB_REPLY"; break;
5653e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMPOUND:
5753e0c72dSLiam Girdwood 		str = "GLB_COMPOUND"; break;
5853e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TPLG_MSG:
5953e0c72dSLiam Girdwood 		str = "GLB_TPLG_MSG";
6053e0c72dSLiam Girdwood 		switch (type) {
6153e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_COMP_NEW:
6253e0c72dSLiam Girdwood 			str2 = "COMP_NEW"; break;
6353e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_COMP_FREE:
6453e0c72dSLiam Girdwood 			str2 = "COMP_FREE"; break;
6553e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_COMP_CONNECT:
6653e0c72dSLiam Girdwood 			str2 = "COMP_CONNECT"; break;
6753e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_NEW:
6853e0c72dSLiam Girdwood 			str2 = "PIPE_NEW"; break;
6953e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_FREE:
7053e0c72dSLiam Girdwood 			str2 = "PIPE_FREE"; break;
7153e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_CONNECT:
7253e0c72dSLiam Girdwood 			str2 = "PIPE_CONNECT"; break;
7353e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_COMPLETE:
7453e0c72dSLiam Girdwood 			str2 = "PIPE_COMPLETE"; break;
7553e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_BUFFER_NEW:
7653e0c72dSLiam Girdwood 			str2 = "BUFFER_NEW"; break;
7753e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_BUFFER_FREE:
7853e0c72dSLiam Girdwood 			str2 = "BUFFER_FREE"; break;
7953e0c72dSLiam Girdwood 		default:
8053e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
8153e0c72dSLiam Girdwood 		}
8253e0c72dSLiam Girdwood 		break;
8353e0c72dSLiam Girdwood 	case SOF_IPC_GLB_PM_MSG:
8453e0c72dSLiam Girdwood 		str = "GLB_PM_MSG";
8553e0c72dSLiam Girdwood 		switch (type) {
8653e0c72dSLiam Girdwood 		case SOF_IPC_PM_CTX_SAVE:
8753e0c72dSLiam Girdwood 			str2 = "CTX_SAVE"; break;
8853e0c72dSLiam Girdwood 		case SOF_IPC_PM_CTX_RESTORE:
8953e0c72dSLiam Girdwood 			str2 = "CTX_RESTORE"; break;
9053e0c72dSLiam Girdwood 		case SOF_IPC_PM_CTX_SIZE:
9153e0c72dSLiam Girdwood 			str2 = "CTX_SIZE"; break;
9253e0c72dSLiam Girdwood 		case SOF_IPC_PM_CLK_SET:
9353e0c72dSLiam Girdwood 			str2 = "CLK_SET"; break;
9453e0c72dSLiam Girdwood 		case SOF_IPC_PM_CLK_GET:
9553e0c72dSLiam Girdwood 			str2 = "CLK_GET"; break;
9653e0c72dSLiam Girdwood 		case SOF_IPC_PM_CLK_REQ:
9753e0c72dSLiam Girdwood 			str2 = "CLK_REQ"; break;
9853e0c72dSLiam Girdwood 		case SOF_IPC_PM_CORE_ENABLE:
9953e0c72dSLiam Girdwood 			str2 = "CORE_ENABLE"; break;
100a8f50cd9SCurtis Malainey 		case SOF_IPC_PM_GATE:
101a8f50cd9SCurtis Malainey 			str2 = "GATE"; break;
10253e0c72dSLiam Girdwood 		default:
10353e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
10453e0c72dSLiam Girdwood 		}
10553e0c72dSLiam Girdwood 		break;
10653e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMP_MSG:
1078199a120SRanjani Sridharan 		str = "GLB_COMP_MSG";
10853e0c72dSLiam Girdwood 		switch (type) {
10953e0c72dSLiam Girdwood 		case SOF_IPC_COMP_SET_VALUE:
11053e0c72dSLiam Girdwood 			str2 = "SET_VALUE"; break;
11153e0c72dSLiam Girdwood 		case SOF_IPC_COMP_GET_VALUE:
11253e0c72dSLiam Girdwood 			str2 = "GET_VALUE"; break;
11353e0c72dSLiam Girdwood 		case SOF_IPC_COMP_SET_DATA:
11453e0c72dSLiam Girdwood 			str2 = "SET_DATA"; break;
11553e0c72dSLiam Girdwood 		case SOF_IPC_COMP_GET_DATA:
11653e0c72dSLiam Girdwood 			str2 = "GET_DATA"; break;
11753e0c72dSLiam Girdwood 		default:
11853e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
11953e0c72dSLiam Girdwood 		}
12053e0c72dSLiam Girdwood 		break;
12153e0c72dSLiam Girdwood 	case SOF_IPC_GLB_STREAM_MSG:
12253e0c72dSLiam Girdwood 		str = "GLB_STREAM_MSG";
12353e0c72dSLiam Girdwood 		switch (type) {
12453e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_PCM_PARAMS:
12553e0c72dSLiam Girdwood 			str2 = "PCM_PARAMS"; break;
12653e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_PCM_PARAMS_REPLY:
12753e0c72dSLiam Girdwood 			str2 = "PCM_REPLY"; break;
12853e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_PCM_FREE:
12953e0c72dSLiam Girdwood 			str2 = "PCM_FREE"; break;
13053e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_START:
13153e0c72dSLiam Girdwood 			str2 = "TRIG_START"; break;
13253e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_STOP:
13353e0c72dSLiam Girdwood 			str2 = "TRIG_STOP"; break;
13453e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_PAUSE:
13553e0c72dSLiam Girdwood 			str2 = "TRIG_PAUSE"; break;
13653e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_RELEASE:
13753e0c72dSLiam Girdwood 			str2 = "TRIG_RELEASE"; break;
13853e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_DRAIN:
13953e0c72dSLiam Girdwood 			str2 = "TRIG_DRAIN"; break;
14053e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_XRUN:
14153e0c72dSLiam Girdwood 			str2 = "TRIG_XRUN"; break;
14253e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_POSITION:
143277ff236SPierre-Louis Bossart 			vdbg = true;
14453e0c72dSLiam Girdwood 			str2 = "POSITION"; break;
14553e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_VORBIS_PARAMS:
14653e0c72dSLiam Girdwood 			str2 = "VORBIS_PARAMS"; break;
14753e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_VORBIS_FREE:
14853e0c72dSLiam Girdwood 			str2 = "VORBIS_FREE"; break;
14953e0c72dSLiam Girdwood 		default:
15053e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
15153e0c72dSLiam Girdwood 		}
15253e0c72dSLiam Girdwood 		break;
15353e0c72dSLiam Girdwood 	case SOF_IPC_FW_READY:
15453e0c72dSLiam Girdwood 		str = "FW_READY"; break;
15553e0c72dSLiam Girdwood 	case SOF_IPC_GLB_DAI_MSG:
15653e0c72dSLiam Girdwood 		str = "GLB_DAI_MSG";
15753e0c72dSLiam Girdwood 		switch (type) {
15853e0c72dSLiam Girdwood 		case SOF_IPC_DAI_CONFIG:
15953e0c72dSLiam Girdwood 			str2 = "CONFIG"; break;
16053e0c72dSLiam Girdwood 		case SOF_IPC_DAI_LOOPBACK:
16153e0c72dSLiam Girdwood 			str2 = "LOOPBACK"; break;
16253e0c72dSLiam Girdwood 		default:
16353e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
16453e0c72dSLiam Girdwood 		}
16553e0c72dSLiam Girdwood 		break;
16653e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TRACE_MSG:
167b4e2d7ceSRanjani Sridharan 		str = "GLB_TRACE_MSG";
168b4e2d7ceSRanjani Sridharan 		switch (type) {
169b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_DMA_PARAMS:
170b4e2d7ceSRanjani Sridharan 			str2 = "DMA_PARAMS"; break;
171b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_DMA_POSITION:
172b4e2d7ceSRanjani Sridharan 			str2 = "DMA_POSITION"; break;
173b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_DMA_PARAMS_EXT:
174b4e2d7ceSRanjani Sridharan 			str2 = "DMA_PARAMS_EXT"; break;
175b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_FILTER_UPDATE:
176b4e2d7ceSRanjani Sridharan 			str2 = "FILTER_UPDATE"; break;
177b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_DMA_FREE:
178b4e2d7ceSRanjani Sridharan 			str2 = "DMA_FREE"; break;
179b4e2d7ceSRanjani Sridharan 		default:
180b4e2d7ceSRanjani Sridharan 			str2 = "unknown type"; break;
181b4e2d7ceSRanjani Sridharan 		}
182b4e2d7ceSRanjani Sridharan 		break;
18353b22d25SRanjani Sridharan 	case SOF_IPC_GLB_TEST_MSG:
18453b22d25SRanjani Sridharan 		str = "GLB_TEST_MSG";
18553b22d25SRanjani Sridharan 		switch (type) {
18653b22d25SRanjani Sridharan 		case SOF_IPC_TEST_IPC_FLOOD:
18753b22d25SRanjani Sridharan 			str2 = "IPC_FLOOD"; break;
18853b22d25SRanjani Sridharan 		default:
18953b22d25SRanjani Sridharan 			str2 = "unknown type"; break;
19053b22d25SRanjani Sridharan 		}
19153b22d25SRanjani Sridharan 		break;
1925b10b629SKarol Trzcinski 	case SOF_IPC_GLB_DEBUG:
1935b10b629SKarol Trzcinski 		str = "GLB_DEBUG";
1945b10b629SKarol Trzcinski 		switch (type) {
1955b10b629SKarol Trzcinski 		case SOF_IPC_DEBUG_MEM_USAGE:
1965b10b629SKarol Trzcinski 			str2 = "MEM_USAGE"; break;
1975b10b629SKarol Trzcinski 		default:
1985b10b629SKarol Trzcinski 			str2 = "unknown type"; break;
1995b10b629SKarol Trzcinski 		}
2005b10b629SKarol Trzcinski 		break;
2014ba344dcSPeter Ujfalusi 	case SOF_IPC_GLB_PROBE:
2024ba344dcSPeter Ujfalusi 		str = "GLB_PROBE";
2034ba344dcSPeter Ujfalusi 		switch (type) {
2044ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_INIT:
2054ba344dcSPeter Ujfalusi 			str2 = "INIT"; break;
2064ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_DEINIT:
2074ba344dcSPeter Ujfalusi 			str2 = "DEINIT"; break;
2084ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_DMA_ADD:
2094ba344dcSPeter Ujfalusi 			str2 = "DMA_ADD"; break;
2104ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_DMA_INFO:
2114ba344dcSPeter Ujfalusi 			str2 = "DMA_INFO"; break;
2124ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_DMA_REMOVE:
2134ba344dcSPeter Ujfalusi 			str2 = "DMA_REMOVE"; break;
2144ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_POINT_ADD:
2154ba344dcSPeter Ujfalusi 			str2 = "POINT_ADD"; break;
2164ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_POINT_INFO:
2174ba344dcSPeter Ujfalusi 			str2 = "POINT_INFO"; break;
2184ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_POINT_REMOVE:
2194ba344dcSPeter Ujfalusi 			str2 = "POINT_REMOVE"; break;
2204ba344dcSPeter Ujfalusi 		default:
2214ba344dcSPeter Ujfalusi 			str2 = "unknown type"; break;
2224ba344dcSPeter Ujfalusi 		}
2234ba344dcSPeter Ujfalusi 		break;
22453e0c72dSLiam Girdwood 	default:
22553e0c72dSLiam Girdwood 		str = "unknown GLB command"; break;
22653e0c72dSLiam Girdwood 	}
22753e0c72dSLiam Girdwood 
228277ff236SPierre-Louis Bossart 	if (str2) {
229277ff236SPierre-Louis Bossart 		if (vdbg)
230277ff236SPierre-Louis Bossart 			dev_vdbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
23153e0c72dSLiam Girdwood 		else
232277ff236SPierre-Louis Bossart 			dev_dbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
233277ff236SPierre-Louis Bossart 	} else {
23453e0c72dSLiam Girdwood 		dev_dbg(dev, "%s: 0x%x: %s\n", text, cmd, str);
23553e0c72dSLiam Girdwood 	}
236277ff236SPierre-Louis Bossart }
23753e0c72dSLiam Girdwood #else
23853e0c72dSLiam Girdwood static inline void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
23953e0c72dSLiam Girdwood {
2404cee883eSDragos Tarcatu 	if ((cmd & SOF_GLB_TYPE_MASK) != SOF_IPC_GLB_TRACE_MSG)
24153e0c72dSLiam Girdwood 		dev_dbg(dev, "%s: 0x%x\n", text, cmd);
24253e0c72dSLiam Girdwood }
24353e0c72dSLiam Girdwood #endif
24453e0c72dSLiam Girdwood 
24553e0c72dSLiam Girdwood /* wait for IPC message reply */
24653e0c72dSLiam Girdwood static int tx_wait_done(struct snd_sof_ipc *ipc, struct snd_sof_ipc_msg *msg,
24753e0c72dSLiam Girdwood 			void *reply_data)
24853e0c72dSLiam Girdwood {
24953e0c72dSLiam Girdwood 	struct snd_sof_dev *sdev = ipc->sdev;
25053e0c72dSLiam Girdwood 	struct sof_ipc_cmd_hdr *hdr = msg->msg_data;
25153e0c72dSLiam Girdwood 	int ret;
25253e0c72dSLiam Girdwood 
25353e0c72dSLiam Girdwood 	/* wait for DSP IPC completion */
25453e0c72dSLiam Girdwood 	ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
2556dd78b31SKai Vehmanen 				 msecs_to_jiffies(sdev->ipc_timeout));
25653e0c72dSLiam Girdwood 
25753e0c72dSLiam Girdwood 	if (ret == 0) {
258b689d81bSPeter Ujfalusi 		dev_err(sdev->dev,
259b689d81bSPeter Ujfalusi 			"ipc tx timed out for %#x (msg/reply size: %d/%zu)\n",
260b689d81bSPeter Ujfalusi 			hdr->cmd, hdr->size, msg->reply_size);
2619a06508bSLiam Girdwood 		snd_sof_handle_fw_exception(ipc->sdev);
26253e0c72dSLiam Girdwood 		ret = -ETIMEDOUT;
26353e0c72dSLiam Girdwood 	} else {
26453e0c72dSLiam Girdwood 		ret = msg->reply_error;
2651919b42cSJaska Uimonen 		if (ret < 0) {
266b689d81bSPeter Ujfalusi 			dev_err(sdev->dev,
267b689d81bSPeter Ujfalusi 				"ipc tx error for %#x (msg/reply size: %d/%zu): %d\n",
268b689d81bSPeter Ujfalusi 				hdr->cmd, hdr->size, msg->reply_size, ret);
2691919b42cSJaska Uimonen 		} else {
27053e0c72dSLiam Girdwood 			ipc_log_header(sdev->dev, "ipc tx succeeded", hdr->cmd);
2711919b42cSJaska Uimonen 			if (msg->reply_size)
2721919b42cSJaska Uimonen 				/* copy the data returned from DSP */
2731919b42cSJaska Uimonen 				memcpy(reply_data, msg->reply_data,
2741919b42cSJaska Uimonen 				       msg->reply_size);
2751919b42cSJaska Uimonen 		}
276e6ff3db9SPeter Ujfalusi 
277e6ff3db9SPeter Ujfalusi 		/* re-enable dumps after successful IPC tx */
278e6ff3db9SPeter Ujfalusi 		if (sdev->ipc_dump_printed) {
279e6ff3db9SPeter Ujfalusi 			sdev->dbg_dump_printed = false;
280e6ff3db9SPeter Ujfalusi 			sdev->ipc_dump_printed = false;
281e6ff3db9SPeter Ujfalusi 		}
28253e0c72dSLiam Girdwood 	}
28353e0c72dSLiam Girdwood 
28453e0c72dSLiam Girdwood 	return ret;
28553e0c72dSLiam Girdwood }
28653e0c72dSLiam Girdwood 
28753e0c72dSLiam Girdwood /* send IPC message from host to DSP */
28873a548bdSPeter Ujfalusi static int sof_ipc_tx_message_unlocked(struct snd_sof_ipc *ipc,
28953e0c72dSLiam Girdwood 				       void *msg_data, size_t msg_bytes,
29053e0c72dSLiam Girdwood 				       void *reply_data, size_t reply_bytes)
29153e0c72dSLiam Girdwood {
29273a548bdSPeter Ujfalusi 	struct sof_ipc_cmd_hdr *hdr = msg_data;
29353e0c72dSLiam Girdwood 	struct snd_sof_dev *sdev = ipc->sdev;
29453e0c72dSLiam Girdwood 	struct snd_sof_ipc_msg *msg;
29553e0c72dSLiam Girdwood 	int ret;
29653e0c72dSLiam Girdwood 
29773a548bdSPeter Ujfalusi 	if (!msg_data || msg_bytes < sizeof(*hdr)) {
29873a548bdSPeter Ujfalusi 		dev_err_ratelimited(sdev->dev, "No IPC message to send\n");
29973a548bdSPeter Ujfalusi 		return -EINVAL;
30073a548bdSPeter Ujfalusi 	}
30173a548bdSPeter Ujfalusi 
3029421ff76SPeter Ujfalusi 	if (ipc->disable_ipc_tx || sdev->fw_state != SOF_FW_BOOT_COMPLETE)
30353e0c72dSLiam Girdwood 		return -ENODEV;
30453e0c72dSLiam Girdwood 
30553e0c72dSLiam Girdwood 	/*
30653e0c72dSLiam Girdwood 	 * The spin-lock is also still needed to protect message objects against
30753e0c72dSLiam Girdwood 	 * other atomic contexts.
30853e0c72dSLiam Girdwood 	 */
30953e0c72dSLiam Girdwood 	spin_lock_irq(&sdev->ipc_lock);
31053e0c72dSLiam Girdwood 
31153e0c72dSLiam Girdwood 	/* initialise the message */
31253e0c72dSLiam Girdwood 	msg = &ipc->msg;
31353e0c72dSLiam Girdwood 
31473a548bdSPeter Ujfalusi 	/* attach message data */
3152acfab71SPeter Ujfalusi 	msg->msg_data = msg_data;
31653e0c72dSLiam Girdwood 	msg->msg_size = msg_bytes;
31773a548bdSPeter Ujfalusi 
31853e0c72dSLiam Girdwood 	msg->reply_size = reply_bytes;
31953e0c72dSLiam Girdwood 	msg->reply_error = 0;
32053e0c72dSLiam Girdwood 
32153e0c72dSLiam Girdwood 	sdev->msg = msg;
32253e0c72dSLiam Girdwood 
32353e0c72dSLiam Girdwood 	ret = snd_sof_dsp_send_msg(sdev, msg);
32453e0c72dSLiam Girdwood 	/* Next reply that we receive will be related to this message */
32553e0c72dSLiam Girdwood 	if (!ret)
32653e0c72dSLiam Girdwood 		msg->ipc_complete = false;
32753e0c72dSLiam Girdwood 
32853e0c72dSLiam Girdwood 	spin_unlock_irq(&sdev->ipc_lock);
32953e0c72dSLiam Girdwood 
330f6b0c731SPeter Ujfalusi 	if (ret) {
33153e0c72dSLiam Girdwood 		dev_err_ratelimited(sdev->dev,
33253e0c72dSLiam Girdwood 				    "error: ipc tx failed with error %d\n",
33353e0c72dSLiam Girdwood 				    ret);
33453e0c72dSLiam Girdwood 		return ret;
33553e0c72dSLiam Girdwood 	}
33653e0c72dSLiam Girdwood 
33773a548bdSPeter Ujfalusi 	ipc_log_header(sdev->dev, "ipc tx", hdr->cmd);
33853e0c72dSLiam Girdwood 
33953e0c72dSLiam Girdwood 	/* now wait for completion */
340f6b0c731SPeter Ujfalusi 	return tx_wait_done(ipc, msg, reply_data);
34153e0c72dSLiam Girdwood }
34253e0c72dSLiam Girdwood 
343b4dcafe4SPeter Ujfalusi /**
344b4dcafe4SPeter Ujfalusi  * sof_ipc_send_msg - generic function to prepare and send one IPC message
345b4dcafe4SPeter Ujfalusi  * @sdev:		pointer to SOF core device struct
346b4dcafe4SPeter Ujfalusi  * @msg_data:		pointer to a message to send
347b4dcafe4SPeter Ujfalusi  * @msg_bytes:		number of bytes in the message
348b4dcafe4SPeter Ujfalusi  * @reply_bytes:	number of bytes available for the reply.
349b4dcafe4SPeter Ujfalusi  *			The buffer for the reply data is not passed to this
350b4dcafe4SPeter Ujfalusi  *			function, the available size is an information for the
351b4dcafe4SPeter Ujfalusi  *			reply handling functions.
352b4dcafe4SPeter Ujfalusi  *
353b4dcafe4SPeter Ujfalusi  * On success the function returns 0, otherwise negative error number.
354b4dcafe4SPeter Ujfalusi  *
355b4dcafe4SPeter Ujfalusi  * Note: higher level sdev->ipc->tx_mutex must be held to make sure that
356b4dcafe4SPeter Ujfalusi  *	 transfers are synchronized.
357b4dcafe4SPeter Ujfalusi  */
358b4dcafe4SPeter Ujfalusi int sof_ipc_send_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_bytes,
359b4dcafe4SPeter Ujfalusi 		     size_t reply_bytes)
360b4dcafe4SPeter Ujfalusi {
361b4dcafe4SPeter Ujfalusi 	struct snd_sof_ipc *ipc = sdev->ipc;
362b4dcafe4SPeter Ujfalusi 	struct snd_sof_ipc_msg *msg;
363b4dcafe4SPeter Ujfalusi 	int ret;
364b4dcafe4SPeter Ujfalusi 
365b4dcafe4SPeter Ujfalusi 	if (ipc->disable_ipc_tx || sdev->fw_state != SOF_FW_BOOT_COMPLETE)
366b4dcafe4SPeter Ujfalusi 		return -ENODEV;
367b4dcafe4SPeter Ujfalusi 
368b4dcafe4SPeter Ujfalusi 	/*
369b4dcafe4SPeter Ujfalusi 	 * The spin-lock is needed to protect message objects against other
370b4dcafe4SPeter Ujfalusi 	 * atomic contexts.
371b4dcafe4SPeter Ujfalusi 	 */
372b4dcafe4SPeter Ujfalusi 	spin_lock_irq(&sdev->ipc_lock);
373b4dcafe4SPeter Ujfalusi 
374b4dcafe4SPeter Ujfalusi 	/* initialise the message */
375b4dcafe4SPeter Ujfalusi 	msg = &ipc->msg;
376b4dcafe4SPeter Ujfalusi 
377b4dcafe4SPeter Ujfalusi 	/* attach message data */
378b4dcafe4SPeter Ujfalusi 	msg->msg_data = msg_data;
379b4dcafe4SPeter Ujfalusi 	msg->msg_size = msg_bytes;
380b4dcafe4SPeter Ujfalusi 
381b4dcafe4SPeter Ujfalusi 	msg->reply_size = reply_bytes;
382b4dcafe4SPeter Ujfalusi 	msg->reply_error = 0;
383b4dcafe4SPeter Ujfalusi 
384b4dcafe4SPeter Ujfalusi 	sdev->msg = msg;
385b4dcafe4SPeter Ujfalusi 
386b4dcafe4SPeter Ujfalusi 	ret = snd_sof_dsp_send_msg(sdev, msg);
387b4dcafe4SPeter Ujfalusi 	/* Next reply that we receive will be related to this message */
388b4dcafe4SPeter Ujfalusi 	if (!ret)
389b4dcafe4SPeter Ujfalusi 		msg->ipc_complete = false;
390b4dcafe4SPeter Ujfalusi 
391b4dcafe4SPeter Ujfalusi 	spin_unlock_irq(&sdev->ipc_lock);
392b4dcafe4SPeter Ujfalusi 
393b4dcafe4SPeter Ujfalusi 	return ret;
394b4dcafe4SPeter Ujfalusi }
395b4dcafe4SPeter Ujfalusi 
39653e0c72dSLiam Girdwood /* send IPC message from host to DSP */
3972a51c0f8SPeter Ujfalusi int sof_ipc_tx_message(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
3982a51c0f8SPeter Ujfalusi 		       void *reply_data, size_t reply_bytes)
39953e0c72dSLiam Girdwood {
40063e51fd3SRanjani Sridharan 	const struct sof_dsp_power_state target_state = {
40163e51fd3SRanjani Sridharan 		.state = SOF_DSP_PM_D0,
40263e51fd3SRanjani Sridharan 	};
40363e51fd3SRanjani Sridharan 	int ret;
40463e51fd3SRanjani Sridharan 
40563e51fd3SRanjani Sridharan 	/* ensure the DSP is in D0 before sending a new IPC */
40663e51fd3SRanjani Sridharan 	ret = snd_sof_dsp_set_power_state(ipc->sdev, &target_state);
40763e51fd3SRanjani Sridharan 	if (ret < 0) {
40863e51fd3SRanjani Sridharan 		dev_err(ipc->sdev->dev, "error: resuming DSP %d\n", ret);
40963e51fd3SRanjani Sridharan 		return ret;
41063e51fd3SRanjani Sridharan 	}
41163e51fd3SRanjani Sridharan 
4122a51c0f8SPeter Ujfalusi 	return sof_ipc_tx_message_no_pm(ipc, msg_data, msg_bytes,
41363e51fd3SRanjani Sridharan 					reply_data, reply_bytes);
41463e51fd3SRanjani Sridharan }
41563e51fd3SRanjani Sridharan EXPORT_SYMBOL(sof_ipc_tx_message);
41663e51fd3SRanjani Sridharan 
41763e51fd3SRanjani Sridharan /*
41863e51fd3SRanjani Sridharan  * send IPC message from host to DSP without modifying the DSP state.
41963e51fd3SRanjani Sridharan  * This will be used for IPC's that can be handled by the DSP
42063e51fd3SRanjani Sridharan  * even in a low-power D0 substate.
42163e51fd3SRanjani Sridharan  */
4222a51c0f8SPeter Ujfalusi int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
42363e51fd3SRanjani Sridharan 			     void *reply_data, size_t reply_bytes)
42463e51fd3SRanjani Sridharan {
42553e0c72dSLiam Girdwood 	int ret;
42653e0c72dSLiam Girdwood 
42778935913SPeter Ujfalusi 	if (msg_bytes > ipc->max_payload_size ||
42878935913SPeter Ujfalusi 	    reply_bytes > ipc->max_payload_size)
42953e0c72dSLiam Girdwood 		return -ENOBUFS;
43053e0c72dSLiam Girdwood 
43153e0c72dSLiam Girdwood 	/* Serialise IPC TX */
43253e0c72dSLiam Girdwood 	mutex_lock(&ipc->tx_mutex);
43353e0c72dSLiam Girdwood 
43473a548bdSPeter Ujfalusi 	ret = sof_ipc_tx_message_unlocked(ipc, msg_data, msg_bytes,
43553e0c72dSLiam Girdwood 					  reply_data, reply_bytes);
43653e0c72dSLiam Girdwood 
43753e0c72dSLiam Girdwood 	mutex_unlock(&ipc->tx_mutex);
43853e0c72dSLiam Girdwood 
43953e0c72dSLiam Girdwood 	return ret;
44053e0c72dSLiam Girdwood }
44163e51fd3SRanjani Sridharan EXPORT_SYMBOL(sof_ipc_tx_message_no_pm);
44253e0c72dSLiam Girdwood 
4438ae77801SPeter Ujfalusi /* Generic helper function to retrieve the reply */
4448ae77801SPeter Ujfalusi void snd_sof_ipc_get_reply(struct snd_sof_dev *sdev)
4458ae77801SPeter Ujfalusi {
4468ae77801SPeter Ujfalusi 	/*
4478ae77801SPeter Ujfalusi 	 * Sometimes, there is unexpected reply ipc arriving. The reply
4488ae77801SPeter Ujfalusi 	 * ipc belongs to none of the ipcs sent from driver.
4498ae77801SPeter Ujfalusi 	 * In this case, the driver must ignore the ipc.
4508ae77801SPeter Ujfalusi 	 */
451*045bc49bSPeter Ujfalusi 	if (!sdev->msg) {
4528ae77801SPeter Ujfalusi 		dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
4538ae77801SPeter Ujfalusi 		return;
4548ae77801SPeter Ujfalusi 	}
4558ae77801SPeter Ujfalusi 
456*045bc49bSPeter Ujfalusi 	sdev->msg->reply_error = sdev->ipc->ops->get_reply(sdev);
4578ae77801SPeter Ujfalusi }
4588ae77801SPeter Ujfalusi EXPORT_SYMBOL(snd_sof_ipc_get_reply);
4598ae77801SPeter Ujfalusi 
46053e0c72dSLiam Girdwood /* handle reply message from DSP */
461d7a1ed26SRanjani Sridharan void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id)
46253e0c72dSLiam Girdwood {
46353e0c72dSLiam Girdwood 	struct snd_sof_ipc_msg *msg = &sdev->ipc->msg;
46453e0c72dSLiam Girdwood 
46553e0c72dSLiam Girdwood 	if (msg->ipc_complete) {
466d7a1ed26SRanjani Sridharan 		dev_dbg(sdev->dev,
467d7a1ed26SRanjani Sridharan 			"no reply expected, received 0x%x, will be ignored",
46853e0c72dSLiam Girdwood 			msg_id);
469d7a1ed26SRanjani Sridharan 		return;
47053e0c72dSLiam Girdwood 	}
47153e0c72dSLiam Girdwood 
47253e0c72dSLiam Girdwood 	/* wake up and return the error if we have waiters on this message ? */
47353e0c72dSLiam Girdwood 	msg->ipc_complete = true;
47453e0c72dSLiam Girdwood 	wake_up(&msg->waitq);
47553e0c72dSLiam Girdwood }
47653e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_reply);
47753e0c72dSLiam Girdwood 
478ab3a2189SPeter Ujfalusi static void ipc_comp_notification(struct snd_sof_dev *sdev, void *msg_buf)
479756bbe42SPeter Ujfalusi {
48010f461d7SRanjani Sridharan 	const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
481ab3a2189SPeter Ujfalusi 	struct sof_ipc_cmd_hdr *hdr = msg_buf;
482756bbe42SPeter Ujfalusi 	u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
483756bbe42SPeter Ujfalusi 
484756bbe42SPeter Ujfalusi 	switch (msg_type) {
485756bbe42SPeter Ujfalusi 	case SOF_IPC_COMP_GET_VALUE:
486756bbe42SPeter Ujfalusi 	case SOF_IPC_COMP_GET_DATA:
487756bbe42SPeter Ujfalusi 		break;
488756bbe42SPeter Ujfalusi 	default:
489756bbe42SPeter Ujfalusi 		dev_err(sdev->dev, "error: unhandled component message %#x\n", msg_type);
490756bbe42SPeter Ujfalusi 		return;
491756bbe42SPeter Ujfalusi 	}
492756bbe42SPeter Ujfalusi 
49310f461d7SRanjani Sridharan 	if (tplg_ops->control->update)
49410f461d7SRanjani Sridharan 		tplg_ops->control->update(sdev, msg_buf);
495756bbe42SPeter Ujfalusi }
496756bbe42SPeter Ujfalusi 
49753e0c72dSLiam Girdwood /* DSP firmware has sent host a message  */
49853e0c72dSLiam Girdwood void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev)
49953e0c72dSLiam Girdwood {
500ab3a2189SPeter Ujfalusi 	ipc_rx_callback rx_callback = NULL;
50153e0c72dSLiam Girdwood 	struct sof_ipc_cmd_hdr hdr;
502ab3a2189SPeter Ujfalusi 	void *msg_buf;
503ab3a2189SPeter Ujfalusi 	u32 cmd;
5046a0ba071SGuennadi Liakhovetski 	int err;
50553e0c72dSLiam Girdwood 
50653e0c72dSLiam Girdwood 	/* read back header */
5076a0ba071SGuennadi Liakhovetski 	err = snd_sof_ipc_msg_data(sdev, NULL, &hdr, sizeof(hdr));
5086a0ba071SGuennadi Liakhovetski 	if (err < 0) {
5096a0ba071SGuennadi Liakhovetski 		dev_warn(sdev->dev, "failed to read IPC header: %d\n", err);
5106a0ba071SGuennadi Liakhovetski 		return;
5116a0ba071SGuennadi Liakhovetski 	}
512ab3a2189SPeter Ujfalusi 
513ab3a2189SPeter Ujfalusi 	if (hdr.size < sizeof(hdr)) {
514ab3a2189SPeter Ujfalusi 		dev_err(sdev->dev, "The received message size is invalid\n");
515ab3a2189SPeter Ujfalusi 		return;
516ab3a2189SPeter Ujfalusi 	}
517ab3a2189SPeter Ujfalusi 
51853e0c72dSLiam Girdwood 	ipc_log_header(sdev->dev, "ipc rx", hdr.cmd);
51953e0c72dSLiam Girdwood 
52053e0c72dSLiam Girdwood 	cmd = hdr.cmd & SOF_GLB_TYPE_MASK;
52153e0c72dSLiam Girdwood 
52253e0c72dSLiam Girdwood 	/* check message type */
52353e0c72dSLiam Girdwood 	switch (cmd) {
52453e0c72dSLiam Girdwood 	case SOF_IPC_GLB_REPLY:
52553e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: ipc reply unknown\n");
52653e0c72dSLiam Girdwood 		break;
52753e0c72dSLiam Girdwood 	case SOF_IPC_FW_READY:
52853e0c72dSLiam Girdwood 		/* check for FW boot completion */
5296ca5cecbSRanjani Sridharan 		if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS) {
53053e0c72dSLiam Girdwood 			err = sof_ops(sdev)->fw_ready(sdev, cmd);
5316ca5cecbSRanjani Sridharan 			if (err < 0)
53258a5c9a4SPeter Ujfalusi 				sof_set_fw_state(sdev, SOF_FW_BOOT_READY_FAILED);
5336ca5cecbSRanjani Sridharan 			else
534b2e9eb3aSPeter Ujfalusi 				sof_set_fw_state(sdev, SOF_FW_BOOT_READY_OK);
53553e0c72dSLiam Girdwood 
53653e0c72dSLiam Girdwood 			/* wake up firmware loader */
53753e0c72dSLiam Girdwood 			wake_up(&sdev->boot_wait);
53853e0c72dSLiam Girdwood 		}
53953e0c72dSLiam Girdwood 		break;
54053e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMPOUND:
54153e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TPLG_MSG:
54253e0c72dSLiam Girdwood 	case SOF_IPC_GLB_PM_MSG:
543756bbe42SPeter Ujfalusi 		break;
54453e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMP_MSG:
545ab3a2189SPeter Ujfalusi 		rx_callback = ipc_comp_notification;
54653e0c72dSLiam Girdwood 		break;
54753e0c72dSLiam Girdwood 	case SOF_IPC_GLB_STREAM_MSG:
548ab3a2189SPeter Ujfalusi 		rx_callback = ipc_stream_message;
54953e0c72dSLiam Girdwood 		break;
55053e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TRACE_MSG:
551ab3a2189SPeter Ujfalusi 		rx_callback = ipc_trace_message;
55253e0c72dSLiam Girdwood 		break;
55353e0c72dSLiam Girdwood 	default:
554ab3a2189SPeter Ujfalusi 		dev_err(sdev->dev, "%s: Unknown DSP message: 0x%x\n", __func__, cmd);
55553e0c72dSLiam Girdwood 		break;
55653e0c72dSLiam Girdwood 	}
55753e0c72dSLiam Girdwood 
5586955d951SPeter Ujfalusi 	/* read the full message */
559ab3a2189SPeter Ujfalusi 	msg_buf = kmalloc(hdr.size, GFP_KERNEL);
560ab3a2189SPeter Ujfalusi 	if (!msg_buf)
561ab3a2189SPeter Ujfalusi 		return;
562ab3a2189SPeter Ujfalusi 
563ab3a2189SPeter Ujfalusi 	err = snd_sof_ipc_msg_data(sdev, NULL, msg_buf, hdr.size);
5646955d951SPeter Ujfalusi 	if (err < 0) {
5656955d951SPeter Ujfalusi 		dev_err(sdev->dev, "%s: Failed to read message: %d\n", __func__, err);
5666955d951SPeter Ujfalusi 	} else {
5676955d951SPeter Ujfalusi 		/* Call local handler for the message */
5686955d951SPeter Ujfalusi 		if (rx_callback)
569ab3a2189SPeter Ujfalusi 			rx_callback(sdev, msg_buf);
570ab3a2189SPeter Ujfalusi 
5716955d951SPeter Ujfalusi 		/* Notify registered clients */
5726955d951SPeter Ujfalusi 		sof_client_ipc_rx_dispatcher(sdev, msg_buf);
573ab3a2189SPeter Ujfalusi 	}
574ab3a2189SPeter Ujfalusi 
5756955d951SPeter Ujfalusi 	kfree(msg_buf);
5766955d951SPeter Ujfalusi 
57753e0c72dSLiam Girdwood 	ipc_log_header(sdev->dev, "ipc rx done", hdr.cmd);
57853e0c72dSLiam Girdwood }
57953e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_msgs_rx);
58053e0c72dSLiam Girdwood 
58153e0c72dSLiam Girdwood /*
58253e0c72dSLiam Girdwood  * IPC trace mechanism.
58353e0c72dSLiam Girdwood  */
58453e0c72dSLiam Girdwood 
585ab3a2189SPeter Ujfalusi static void ipc_trace_message(struct snd_sof_dev *sdev, void *msg_buf)
58653e0c72dSLiam Girdwood {
587ab3a2189SPeter Ujfalusi 	struct sof_ipc_cmd_hdr *hdr = msg_buf;
588ab3a2189SPeter Ujfalusi 	u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
58953e0c72dSLiam Girdwood 
59059fdde1dSPeter Ujfalusi 	switch (msg_type) {
59153e0c72dSLiam Girdwood 	case SOF_IPC_TRACE_DMA_POSITION:
592ab3a2189SPeter Ujfalusi 		snd_sof_trace_update_pos(sdev, msg_buf);
59353e0c72dSLiam Girdwood 		break;
59453e0c72dSLiam Girdwood 	default:
595b95b6451SPeter Ujfalusi 		dev_err(sdev->dev, "error: unhandled trace message %#x\n", msg_type);
59653e0c72dSLiam Girdwood 		break;
59753e0c72dSLiam Girdwood 	}
59853e0c72dSLiam Girdwood }
59953e0c72dSLiam Girdwood 
60053e0c72dSLiam Girdwood /*
60153e0c72dSLiam Girdwood  * IPC stream position.
60253e0c72dSLiam Girdwood  */
60353e0c72dSLiam Girdwood 
60453e0c72dSLiam Girdwood static void ipc_period_elapsed(struct snd_sof_dev *sdev, u32 msg_id)
60553e0c72dSLiam Girdwood {
606ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = sdev->component;
60753e0c72dSLiam Girdwood 	struct snd_sof_pcm_stream *stream;
60853e0c72dSLiam Girdwood 	struct sof_ipc_stream_posn posn;
60953e0c72dSLiam Girdwood 	struct snd_sof_pcm *spcm;
6106a0ba071SGuennadi Liakhovetski 	int direction, ret;
61153e0c72dSLiam Girdwood 
612ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
61353e0c72dSLiam Girdwood 	if (!spcm) {
61453e0c72dSLiam Girdwood 		dev_err(sdev->dev,
61553e0c72dSLiam Girdwood 			"error: period elapsed for unknown stream, msg_id %d\n",
61653e0c72dSLiam Girdwood 			msg_id);
61753e0c72dSLiam Girdwood 		return;
61853e0c72dSLiam Girdwood 	}
61953e0c72dSLiam Girdwood 
62053e0c72dSLiam Girdwood 	stream = &spcm->stream[direction];
6216a0ba071SGuennadi Liakhovetski 	ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
6226a0ba071SGuennadi Liakhovetski 	if (ret < 0) {
6236a0ba071SGuennadi Liakhovetski 		dev_warn(sdev->dev, "failed to read stream position: %d\n", ret);
6246a0ba071SGuennadi Liakhovetski 		return;
6256a0ba071SGuennadi Liakhovetski 	}
62653e0c72dSLiam Girdwood 
627277ff236SPierre-Louis Bossart 	dev_vdbg(sdev->dev, "posn : host 0x%llx dai 0x%llx wall 0x%llx\n",
62853e0c72dSLiam Girdwood 		 posn.host_posn, posn.dai_posn, posn.wallclock);
62953e0c72dSLiam Girdwood 
63053e0c72dSLiam Girdwood 	memcpy(&stream->posn, &posn, sizeof(posn));
63153e0c72dSLiam Girdwood 
632858f7a5cSDaniel Baluta 	if (spcm->pcm.compress)
633858f7a5cSDaniel Baluta 		snd_sof_compr_fragment_elapsed(stream->cstream);
634182b682bSAjit Kumar Pandey 	else if (stream->substream->runtime &&
635182b682bSAjit Kumar Pandey 		 !stream->substream->runtime->no_period_wakeup)
63653e0c72dSLiam Girdwood 		/* only inform ALSA for period_wakeup mode */
637ff758e9fSKeyon Jie 		snd_sof_pcm_period_elapsed(stream->substream);
63853e0c72dSLiam Girdwood }
63953e0c72dSLiam Girdwood 
64053e0c72dSLiam Girdwood /* DSP notifies host of an XRUN within FW */
64153e0c72dSLiam Girdwood static void ipc_xrun(struct snd_sof_dev *sdev, u32 msg_id)
64253e0c72dSLiam Girdwood {
643ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = sdev->component;
64453e0c72dSLiam Girdwood 	struct snd_sof_pcm_stream *stream;
64553e0c72dSLiam Girdwood 	struct sof_ipc_stream_posn posn;
64653e0c72dSLiam Girdwood 	struct snd_sof_pcm *spcm;
6476a0ba071SGuennadi Liakhovetski 	int direction, ret;
64853e0c72dSLiam Girdwood 
649ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
65053e0c72dSLiam Girdwood 	if (!spcm) {
65153e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: XRUN for unknown stream, msg_id %d\n",
65253e0c72dSLiam Girdwood 			msg_id);
65353e0c72dSLiam Girdwood 		return;
65453e0c72dSLiam Girdwood 	}
65553e0c72dSLiam Girdwood 
65653e0c72dSLiam Girdwood 	stream = &spcm->stream[direction];
6576a0ba071SGuennadi Liakhovetski 	ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
6586a0ba071SGuennadi Liakhovetski 	if (ret < 0) {
6596a0ba071SGuennadi Liakhovetski 		dev_warn(sdev->dev, "failed to read overrun position: %d\n", ret);
6606a0ba071SGuennadi Liakhovetski 		return;
6616a0ba071SGuennadi Liakhovetski 	}
66253e0c72dSLiam Girdwood 
66353e0c72dSLiam Girdwood 	dev_dbg(sdev->dev,  "posn XRUN: host %llx comp %d size %d\n",
66453e0c72dSLiam Girdwood 		posn.host_posn, posn.xrun_comp_id, posn.xrun_size);
66553e0c72dSLiam Girdwood 
66653e0c72dSLiam Girdwood #if defined(CONFIG_SND_SOC_SOF_DEBUG_XRUN_STOP)
66753e0c72dSLiam Girdwood 	/* stop PCM on XRUN - used for pipeline debug */
66853e0c72dSLiam Girdwood 	memcpy(&stream->posn, &posn, sizeof(posn));
66953e0c72dSLiam Girdwood 	snd_pcm_stop_xrun(stream->substream);
67053e0c72dSLiam Girdwood #endif
67153e0c72dSLiam Girdwood }
67253e0c72dSLiam Girdwood 
67353e0c72dSLiam Girdwood /* stream notifications from DSP FW */
674ab3a2189SPeter Ujfalusi static void ipc_stream_message(struct snd_sof_dev *sdev, void *msg_buf)
67553e0c72dSLiam Girdwood {
676ab3a2189SPeter Ujfalusi 	struct sof_ipc_cmd_hdr *hdr = msg_buf;
677ab3a2189SPeter Ujfalusi 	u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
678ab3a2189SPeter Ujfalusi 	u32 msg_id = SOF_IPC_MESSAGE_ID(hdr->cmd);
67953e0c72dSLiam Girdwood 
68053e0c72dSLiam Girdwood 	switch (msg_type) {
68153e0c72dSLiam Girdwood 	case SOF_IPC_STREAM_POSITION:
68253e0c72dSLiam Girdwood 		ipc_period_elapsed(sdev, msg_id);
68353e0c72dSLiam Girdwood 		break;
68453e0c72dSLiam Girdwood 	case SOF_IPC_STREAM_TRIG_XRUN:
68553e0c72dSLiam Girdwood 		ipc_xrun(sdev, msg_id);
68653e0c72dSLiam Girdwood 		break;
68753e0c72dSLiam Girdwood 	default:
688b95b6451SPeter Ujfalusi 		dev_err(sdev->dev, "error: unhandled stream message %#x\n",
68953e0c72dSLiam Girdwood 			msg_id);
69053e0c72dSLiam Girdwood 		break;
69153e0c72dSLiam Girdwood 	}
69253e0c72dSLiam Girdwood }
69353e0c72dSLiam Girdwood 
69453e0c72dSLiam Girdwood /* get stream position IPC - use faster MMIO method if available on platform */
695ee1e79b7SRanjani Sridharan int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp,
69653e0c72dSLiam Girdwood 			    struct snd_sof_pcm *spcm, int direction,
69753e0c72dSLiam Girdwood 			    struct sof_ipc_stream_posn *posn)
69853e0c72dSLiam Girdwood {
699ee1e79b7SRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
70053e0c72dSLiam Girdwood 	struct sof_ipc_stream stream;
70153e0c72dSLiam Girdwood 	int err;
70253e0c72dSLiam Girdwood 
70353e0c72dSLiam Girdwood 	/* read position via slower IPC */
70453e0c72dSLiam Girdwood 	stream.hdr.size = sizeof(stream);
70553e0c72dSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_POSITION;
70653e0c72dSLiam Girdwood 	stream.comp_id = spcm->stream[direction].comp_id;
70753e0c72dSLiam Girdwood 
70853e0c72dSLiam Girdwood 	/* send IPC to the DSP */
7092a51c0f8SPeter Ujfalusi 	err = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), posn,
71053e0c72dSLiam Girdwood 				 sizeof(*posn));
71153e0c72dSLiam Girdwood 	if (err < 0) {
71253e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: failed to get stream %d position\n",
71353e0c72dSLiam Girdwood 			stream.comp_id);
71453e0c72dSLiam Girdwood 		return err;
71553e0c72dSLiam Girdwood 	}
71653e0c72dSLiam Girdwood 
71753e0c72dSLiam Girdwood 	return 0;
71853e0c72dSLiam Girdwood }
71953e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_stream_posn);
72053e0c72dSLiam Girdwood 
72153e0c72dSLiam Girdwood static int sof_get_ctrl_copy_params(enum sof_ipc_ctrl_type ctrl_type,
72253e0c72dSLiam Girdwood 				    struct sof_ipc_ctrl_data *src,
72353e0c72dSLiam Girdwood 				    struct sof_ipc_ctrl_data *dst,
72453e0c72dSLiam Girdwood 				    struct sof_ipc_ctrl_data_params *sparams)
72553e0c72dSLiam Girdwood {
72653e0c72dSLiam Girdwood 	switch (ctrl_type) {
72753e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_GET:
72853e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_SET:
72953e0c72dSLiam Girdwood 		sparams->src = (u8 *)src->chanv;
73053e0c72dSLiam Girdwood 		sparams->dst = (u8 *)dst->chanv;
73153e0c72dSLiam Girdwood 		break;
73253e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_GET:
73353e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_SET:
73453e0c72dSLiam Girdwood 		sparams->src = (u8 *)src->data->data;
73553e0c72dSLiam Girdwood 		sparams->dst = (u8 *)dst->data->data;
73653e0c72dSLiam Girdwood 		break;
73753e0c72dSLiam Girdwood 	default:
73853e0c72dSLiam Girdwood 		return -EINVAL;
73953e0c72dSLiam Girdwood 	}
74053e0c72dSLiam Girdwood 
74153e0c72dSLiam Girdwood 	/* calculate payload size and number of messages */
74253e0c72dSLiam Girdwood 	sparams->pl_size = SOF_IPC_MSG_MAX_SIZE - sparams->hdr_bytes;
74353e0c72dSLiam Girdwood 	sparams->num_msg = DIV_ROUND_UP(sparams->msg_bytes, sparams->pl_size);
74453e0c72dSLiam Girdwood 
74553e0c72dSLiam Girdwood 	return 0;
74653e0c72dSLiam Girdwood }
74753e0c72dSLiam Girdwood 
74853e0c72dSLiam Girdwood static int sof_set_get_large_ctrl_data(struct snd_sof_dev *sdev,
74953e0c72dSLiam Girdwood 				       struct sof_ipc_ctrl_data *cdata,
75053e0c72dSLiam Girdwood 				       struct sof_ipc_ctrl_data_params *sparams,
7519d562fdcSPeter Ujfalusi 				       bool set)
75253e0c72dSLiam Girdwood {
75353e0c72dSLiam Girdwood 	struct sof_ipc_ctrl_data *partdata;
75453e0c72dSLiam Girdwood 	size_t send_bytes;
75553e0c72dSLiam Girdwood 	size_t offset = 0;
75653e0c72dSLiam Girdwood 	size_t msg_bytes;
75753e0c72dSLiam Girdwood 	size_t pl_size;
75854d198d5STakashi Iwai 	int err;
75953e0c72dSLiam Girdwood 	int i;
76053e0c72dSLiam Girdwood 
76153e0c72dSLiam Girdwood 	/* allocate max ipc size because we have at least one */
76253e0c72dSLiam Girdwood 	partdata = kzalloc(SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL);
76353e0c72dSLiam Girdwood 	if (!partdata)
76453e0c72dSLiam Girdwood 		return -ENOMEM;
76553e0c72dSLiam Girdwood 
7669d562fdcSPeter Ujfalusi 	if (set)
76754d198d5STakashi Iwai 		err = sof_get_ctrl_copy_params(cdata->type, cdata, partdata,
76854d198d5STakashi Iwai 					       sparams);
76953e0c72dSLiam Girdwood 	else
77054d198d5STakashi Iwai 		err = sof_get_ctrl_copy_params(cdata->type, partdata, cdata,
77154d198d5STakashi Iwai 					       sparams);
77245c13803SNavid Emamdoost 	if (err < 0) {
77345c13803SNavid Emamdoost 		kfree(partdata);
77454d198d5STakashi Iwai 		return err;
77545c13803SNavid Emamdoost 	}
77653e0c72dSLiam Girdwood 
77753e0c72dSLiam Girdwood 	msg_bytes = sparams->msg_bytes;
77853e0c72dSLiam Girdwood 	pl_size = sparams->pl_size;
77953e0c72dSLiam Girdwood 
78053e0c72dSLiam Girdwood 	/* copy the header data */
78153e0c72dSLiam Girdwood 	memcpy(partdata, cdata, sparams->hdr_bytes);
78253e0c72dSLiam Girdwood 
78353e0c72dSLiam Girdwood 	/* Serialise IPC TX */
78453e0c72dSLiam Girdwood 	mutex_lock(&sdev->ipc->tx_mutex);
78553e0c72dSLiam Girdwood 
78653e0c72dSLiam Girdwood 	/* copy the payload data in a loop */
78753e0c72dSLiam Girdwood 	for (i = 0; i < sparams->num_msg; i++) {
78853e0c72dSLiam Girdwood 		send_bytes = min(msg_bytes, pl_size);
78953e0c72dSLiam Girdwood 		partdata->num_elems = send_bytes;
79053e0c72dSLiam Girdwood 		partdata->rhdr.hdr.size = sparams->hdr_bytes + send_bytes;
79153e0c72dSLiam Girdwood 		partdata->msg_index = i;
79253e0c72dSLiam Girdwood 		msg_bytes -= send_bytes;
79353e0c72dSLiam Girdwood 		partdata->elems_remaining = msg_bytes;
79453e0c72dSLiam Girdwood 
7959d562fdcSPeter Ujfalusi 		if (set)
79653e0c72dSLiam Girdwood 			memcpy(sparams->dst, sparams->src + offset, send_bytes);
79753e0c72dSLiam Girdwood 
79853e0c72dSLiam Girdwood 		err = sof_ipc_tx_message_unlocked(sdev->ipc,
79953e0c72dSLiam Girdwood 						  partdata,
80053e0c72dSLiam Girdwood 						  partdata->rhdr.hdr.size,
80153e0c72dSLiam Girdwood 						  partdata,
80253e0c72dSLiam Girdwood 						  partdata->rhdr.hdr.size);
80353e0c72dSLiam Girdwood 		if (err < 0)
80453e0c72dSLiam Girdwood 			break;
80553e0c72dSLiam Girdwood 
8069d562fdcSPeter Ujfalusi 		if (!set)
80753e0c72dSLiam Girdwood 			memcpy(sparams->dst + offset, sparams->src, send_bytes);
80853e0c72dSLiam Girdwood 
80953e0c72dSLiam Girdwood 		offset += pl_size;
81053e0c72dSLiam Girdwood 	}
81153e0c72dSLiam Girdwood 
81253e0c72dSLiam Girdwood 	mutex_unlock(&sdev->ipc->tx_mutex);
81353e0c72dSLiam Girdwood 
81453e0c72dSLiam Girdwood 	kfree(partdata);
81553e0c72dSLiam Girdwood 	return err;
81653e0c72dSLiam Girdwood }
81753e0c72dSLiam Girdwood 
81853e0c72dSLiam Girdwood /*
81953e0c72dSLiam Girdwood  * IPC get()/set() for kcontrols.
82053e0c72dSLiam Girdwood  */
82147d7328fSPeter Ujfalusi int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol, bool set)
82253e0c72dSLiam Girdwood {
823ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
824b5cee8feSRanjani Sridharan 	struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
825ee1e79b7SRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
82653e0c72dSLiam Girdwood 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
82753e0c72dSLiam Girdwood 	struct sof_ipc_fw_version *v = &ready->version;
82853e0c72dSLiam Girdwood 	struct sof_ipc_ctrl_data_params sparams;
82947d7328fSPeter Ujfalusi 	enum sof_ipc_ctrl_type ctrl_type;
8308b001416SRanjani Sridharan 	struct snd_sof_widget *swidget;
8318b001416SRanjani Sridharan 	bool widget_found = false;
832d4a06c43SPeter Ujfalusi 	u32 ipc_cmd;
83353e0c72dSLiam Girdwood 	int err;
83453e0c72dSLiam Girdwood 
8358b001416SRanjani Sridharan 	list_for_each_entry(swidget, &sdev->widget_list, list) {
8368b001416SRanjani Sridharan 		if (swidget->comp_id == scontrol->comp_id) {
8378b001416SRanjani Sridharan 			widget_found = true;
8388b001416SRanjani Sridharan 			break;
8398b001416SRanjani Sridharan 		}
8408b001416SRanjani Sridharan 	}
8418b001416SRanjani Sridharan 
8428b001416SRanjani Sridharan 	if (!widget_found) {
8438b001416SRanjani Sridharan 		dev_err(sdev->dev, "error: can't find widget with id %d\n", scontrol->comp_id);
8448b001416SRanjani Sridharan 		return -EINVAL;
8458b001416SRanjani Sridharan 	}
8468b001416SRanjani Sridharan 
8478b001416SRanjani Sridharan 	/*
8488b001416SRanjani Sridharan 	 * Volatile controls should always be part of static pipelines and the widget use_count
8498b001416SRanjani Sridharan 	 * would always be > 0 in this case. For the others, just return the cached value if the
8508b001416SRanjani Sridharan 	 * widget is not set up.
8518b001416SRanjani Sridharan 	 */
8528b001416SRanjani Sridharan 	if (!swidget->use_count)
8538b001416SRanjani Sridharan 		return 0;
8548b001416SRanjani Sridharan 
85547d7328fSPeter Ujfalusi 	/*
85647d7328fSPeter Ujfalusi 	 * Select the IPC cmd and the ctrl_type based on the ctrl_cmd and the
85747d7328fSPeter Ujfalusi 	 * direction
85847d7328fSPeter Ujfalusi 	 * Note: SOF_CTRL_TYPE_VALUE_COMP_* is not used and supported currently
85947d7328fSPeter Ujfalusi 	 *	 for ctrl_type
86047d7328fSPeter Ujfalusi 	 */
86147d7328fSPeter Ujfalusi 	if (cdata->cmd == SOF_CTRL_CMD_BINARY) {
862d4a06c43SPeter Ujfalusi 		ipc_cmd = set ? SOF_IPC_COMP_SET_DATA : SOF_IPC_COMP_GET_DATA;
86347d7328fSPeter Ujfalusi 		ctrl_type = set ? SOF_CTRL_TYPE_DATA_SET : SOF_CTRL_TYPE_DATA_GET;
86447d7328fSPeter Ujfalusi 	} else {
865d4a06c43SPeter Ujfalusi 		ipc_cmd = set ? SOF_IPC_COMP_SET_VALUE : SOF_IPC_COMP_GET_VALUE;
86647d7328fSPeter Ujfalusi 		ctrl_type = set ? SOF_CTRL_TYPE_VALUE_CHAN_SET : SOF_CTRL_TYPE_VALUE_CHAN_GET;
86747d7328fSPeter Ujfalusi 	}
868d4a06c43SPeter Ujfalusi 
86953e0c72dSLiam Girdwood 	cdata->rhdr.hdr.cmd = SOF_IPC_GLB_COMP_MSG | ipc_cmd;
87053e0c72dSLiam Girdwood 	cdata->type = ctrl_type;
87153e0c72dSLiam Girdwood 	cdata->comp_id = scontrol->comp_id;
87253e0c72dSLiam Girdwood 	cdata->msg_index = 0;
87353e0c72dSLiam Girdwood 
87453e0c72dSLiam Girdwood 	/* calculate header and data size */
87553e0c72dSLiam Girdwood 	switch (cdata->type) {
87653e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_GET:
87753e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_SET:
87853e0c72dSLiam Girdwood 		sparams.msg_bytes = scontrol->num_channels *
87953e0c72dSLiam Girdwood 			sizeof(struct sof_ipc_ctrl_value_chan);
88053e0c72dSLiam Girdwood 		sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data);
88153e0c72dSLiam Girdwood 		sparams.elems = scontrol->num_channels;
88253e0c72dSLiam Girdwood 		break;
88353e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_GET:
88453e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_SET:
88553e0c72dSLiam Girdwood 		sparams.msg_bytes = cdata->data->size;
88653e0c72dSLiam Girdwood 		sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data) +
88753e0c72dSLiam Girdwood 			sizeof(struct sof_abi_hdr);
88853e0c72dSLiam Girdwood 		sparams.elems = cdata->data->size;
88953e0c72dSLiam Girdwood 		break;
89053e0c72dSLiam Girdwood 	default:
89153e0c72dSLiam Girdwood 		return -EINVAL;
89253e0c72dSLiam Girdwood 	}
89353e0c72dSLiam Girdwood 
89453e0c72dSLiam Girdwood 	cdata->rhdr.hdr.size = sparams.msg_bytes + sparams.hdr_bytes;
89553e0c72dSLiam Girdwood 	cdata->num_elems = sparams.elems;
89653e0c72dSLiam Girdwood 	cdata->elems_remaining = 0;
89753e0c72dSLiam Girdwood 
89853e0c72dSLiam Girdwood 	/* send normal size ipc in one part */
89953e0c72dSLiam Girdwood 	if (cdata->rhdr.hdr.size <= SOF_IPC_MSG_MAX_SIZE) {
9002a51c0f8SPeter Ujfalusi 		err = sof_ipc_tx_message(sdev->ipc, cdata, cdata->rhdr.hdr.size,
9012a51c0f8SPeter Ujfalusi 					 cdata, cdata->rhdr.hdr.size);
90253e0c72dSLiam Girdwood 
90353e0c72dSLiam Girdwood 		if (err < 0)
90453e0c72dSLiam Girdwood 			dev_err(sdev->dev, "error: set/get ctrl ipc comp %d\n",
90553e0c72dSLiam Girdwood 				cdata->comp_id);
90653e0c72dSLiam Girdwood 
90753e0c72dSLiam Girdwood 		return err;
90853e0c72dSLiam Girdwood 	}
90953e0c72dSLiam Girdwood 
91053e0c72dSLiam Girdwood 	/* data is bigger than max ipc size, chop into smaller pieces */
91153e0c72dSLiam Girdwood 	dev_dbg(sdev->dev, "large ipc size %u, control size %u\n",
91253e0c72dSLiam Girdwood 		cdata->rhdr.hdr.size, scontrol->size);
91353e0c72dSLiam Girdwood 
91453e0c72dSLiam Girdwood 	/* large messages is only supported from ABI 3.3.0 onwards */
91553e0c72dSLiam Girdwood 	if (v->abi_version < SOF_ABI_VER(3, 3, 0)) {
91653e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: incompatible FW ABI version\n");
91753e0c72dSLiam Girdwood 		return -EINVAL;
91853e0c72dSLiam Girdwood 	}
91953e0c72dSLiam Girdwood 
9209d562fdcSPeter Ujfalusi 	err = sof_set_get_large_ctrl_data(sdev, cdata, &sparams, set);
92153e0c72dSLiam Girdwood 
92253e0c72dSLiam Girdwood 	if (err < 0)
92353e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: set/get large ctrl ipc comp %d\n",
92453e0c72dSLiam Girdwood 			cdata->comp_id);
92553e0c72dSLiam Girdwood 
92653e0c72dSLiam Girdwood 	return err;
92753e0c72dSLiam Girdwood }
92853e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_set_get_comp_data);
92953e0c72dSLiam Girdwood 
93053e0c72dSLiam Girdwood int snd_sof_ipc_valid(struct snd_sof_dev *sdev)
93153e0c72dSLiam Girdwood {
93253e0c72dSLiam Girdwood 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
93353e0c72dSLiam Girdwood 	struct sof_ipc_fw_version *v = &ready->version;
93453e0c72dSLiam Girdwood 
93553e0c72dSLiam Girdwood 	dev_info(sdev->dev,
93653e0c72dSLiam Girdwood 		 "Firmware info: version %d:%d:%d-%s\n",  v->major, v->minor,
93753e0c72dSLiam Girdwood 		 v->micro, v->tag);
93853e0c72dSLiam Girdwood 	dev_info(sdev->dev,
93953e0c72dSLiam Girdwood 		 "Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
94053e0c72dSLiam Girdwood 		 SOF_ABI_VERSION_MAJOR(v->abi_version),
94153e0c72dSLiam Girdwood 		 SOF_ABI_VERSION_MINOR(v->abi_version),
94253e0c72dSLiam Girdwood 		 SOF_ABI_VERSION_PATCH(v->abi_version),
94353e0c72dSLiam Girdwood 		 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
94453e0c72dSLiam Girdwood 
94553e0c72dSLiam Girdwood 	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) {
94653e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: incompatible FW ABI version\n");
94753e0c72dSLiam Girdwood 		return -EINVAL;
94853e0c72dSLiam Girdwood 	}
94953e0c72dSLiam Girdwood 
95053129e66SKai Vehmanen 	if (SOF_ABI_VERSION_MINOR(v->abi_version) > SOF_ABI_MINOR) {
9514acb1c2eSPierre-Louis Bossart 		if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
9524acb1c2eSPierre-Louis Bossart 			dev_warn(sdev->dev, "warn: FW ABI is more recent than kernel\n");
9534acb1c2eSPierre-Louis Bossart 		} else {
9544acb1c2eSPierre-Louis Bossart 			dev_err(sdev->dev, "error: FW ABI is more recent than kernel\n");
9554acb1c2eSPierre-Louis Bossart 			return -EINVAL;
9564acb1c2eSPierre-Louis Bossart 		}
9574acb1c2eSPierre-Louis Bossart 	}
9584acb1c2eSPierre-Louis Bossart 
959347d1c4bSSlawomir Blauciak 	if (ready->flags & SOF_IPC_INFO_BUILD) {
96053e0c72dSLiam Girdwood 		dev_info(sdev->dev,
96153e0c72dSLiam Girdwood 			 "Firmware debug build %d on %s-%s - options:\n"
96253e0c72dSLiam Girdwood 			 " GDB: %s\n"
96353e0c72dSLiam Girdwood 			 " lock debug: %s\n"
96453e0c72dSLiam Girdwood 			 " lock vdebug: %s\n",
96553e0c72dSLiam Girdwood 			 v->build, v->date, v->time,
966847a040dSPierre-Louis Bossart 			 (ready->flags & SOF_IPC_INFO_GDB) ?
967347d1c4bSSlawomir Blauciak 				"enabled" : "disabled",
968847a040dSPierre-Louis Bossart 			 (ready->flags & SOF_IPC_INFO_LOCKS) ?
969347d1c4bSSlawomir Blauciak 				"enabled" : "disabled",
970847a040dSPierre-Louis Bossart 			 (ready->flags & SOF_IPC_INFO_LOCKSV) ?
971347d1c4bSSlawomir Blauciak 				"enabled" : "disabled");
97253e0c72dSLiam Girdwood 	}
97353e0c72dSLiam Girdwood 
97453e0c72dSLiam Girdwood 	/* copy the fw_version into debugfs at first boot */
97553e0c72dSLiam Girdwood 	memcpy(&sdev->fw_version, v, sizeof(*v));
97653e0c72dSLiam Girdwood 
97753e0c72dSLiam Girdwood 	return 0;
97853e0c72dSLiam Girdwood }
97953e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_valid);
98053e0c72dSLiam Girdwood 
981febf5da8SRander Wang int sof_ipc_init_msg_memory(struct snd_sof_dev *sdev)
982febf5da8SRander Wang {
983febf5da8SRander Wang 	struct snd_sof_ipc_msg *msg;
984febf5da8SRander Wang 
985febf5da8SRander Wang 	msg = &sdev->ipc->msg;
986febf5da8SRander Wang 
987febf5da8SRander Wang 	msg->reply_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL);
988febf5da8SRander Wang 	if (!msg->reply_data)
989febf5da8SRander Wang 		return -ENOMEM;
990febf5da8SRander Wang 
991a3e6b369SPeter Ujfalusi 	sdev->ipc->max_payload_size = SOF_IPC_MSG_MAX_SIZE;
992a3e6b369SPeter Ujfalusi 
993febf5da8SRander Wang 	return 0;
994febf5da8SRander Wang }
995febf5da8SRander Wang 
99653e0c72dSLiam Girdwood struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev)
99753e0c72dSLiam Girdwood {
99853e0c72dSLiam Girdwood 	struct snd_sof_ipc *ipc;
99953e0c72dSLiam Girdwood 	struct snd_sof_ipc_msg *msg;
1000785b3fbeSPeter Ujfalusi 	const struct sof_ipc_ops *ops;
100153e0c72dSLiam Girdwood 
100253e0c72dSLiam Girdwood 	ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL);
100353e0c72dSLiam Girdwood 	if (!ipc)
100453e0c72dSLiam Girdwood 		return NULL;
100553e0c72dSLiam Girdwood 
100653e0c72dSLiam Girdwood 	mutex_init(&ipc->tx_mutex);
100753e0c72dSLiam Girdwood 	ipc->sdev = sdev;
100853e0c72dSLiam Girdwood 	msg = &ipc->msg;
100953e0c72dSLiam Girdwood 
101053e0c72dSLiam Girdwood 	/* indicate that we aren't sending a message ATM */
101153e0c72dSLiam Girdwood 	msg->ipc_complete = true;
101253e0c72dSLiam Girdwood 
101353e0c72dSLiam Girdwood 	init_waitqueue_head(&msg->waitq);
101453e0c72dSLiam Girdwood 
10157006d20eSRanjani Sridharan 	/*
10167006d20eSRanjani Sridharan 	 * Use IPC3 ops as it is the only available version now. With the addition of new IPC
10177006d20eSRanjani Sridharan 	 * versions, this will need to be modified to use the selected version at runtime.
10187006d20eSRanjani Sridharan 	 */
10197006d20eSRanjani Sridharan 	ipc->ops = &ipc3_ops;
1020785b3fbeSPeter Ujfalusi 	ops = ipc->ops;
10217006d20eSRanjani Sridharan 
10227006d20eSRanjani Sridharan 	/* check for mandatory ops */
1023defad9d2SPeter Ujfalusi 	if (!ops->tx_msg || !ops->rx_msg || !ops->set_get_data || !ops->get_reply) {
1024defad9d2SPeter Ujfalusi 		dev_err(sdev->dev, "Missing IPC message handling ops\n");
1025defad9d2SPeter Ujfalusi 		return NULL;
1026defad9d2SPeter Ujfalusi 	}
1027defad9d2SPeter Ujfalusi 
1028785b3fbeSPeter Ujfalusi 	if (!ops->pcm) {
1029785b3fbeSPeter Ujfalusi 		dev_err(sdev->dev, "Missing IPC PCM ops\n");
1030785b3fbeSPeter Ujfalusi 		return NULL;
1031785b3fbeSPeter Ujfalusi 	}
1032785b3fbeSPeter Ujfalusi 
1033785b3fbeSPeter Ujfalusi 	if (!ops->tplg || !ops->tplg->widget || !ops->tplg->control) {
1034785b3fbeSPeter Ujfalusi 		dev_err(sdev->dev, "Missing IPC topology ops\n");
10357006d20eSRanjani Sridharan 		return NULL;
10367006d20eSRanjani Sridharan 	}
10377006d20eSRanjani Sridharan 
103853e0c72dSLiam Girdwood 	return ipc;
103953e0c72dSLiam Girdwood }
104053e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_init);
104153e0c72dSLiam Girdwood 
104253e0c72dSLiam Girdwood void snd_sof_ipc_free(struct snd_sof_dev *sdev)
104353e0c72dSLiam Girdwood {
104453e0c72dSLiam Girdwood 	struct snd_sof_ipc *ipc = sdev->ipc;
104553e0c72dSLiam Girdwood 
1046b06e4642SKai Vehmanen 	if (!ipc)
1047b06e4642SKai Vehmanen 		return;
1048b06e4642SKai Vehmanen 
104953e0c72dSLiam Girdwood 	/* disable sending of ipc's */
105053e0c72dSLiam Girdwood 	mutex_lock(&ipc->tx_mutex);
105153e0c72dSLiam Girdwood 	ipc->disable_ipc_tx = true;
105253e0c72dSLiam Girdwood 	mutex_unlock(&ipc->tx_mutex);
105353e0c72dSLiam Girdwood }
105453e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_free);
1055