xref: /openbmc/linux/sound/soc/sof/ipc.c (revision 85d0f881471531ffb081711b13df32b1f6f1f637)
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 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_VERBOSE_IPC)
2853e0c72dSLiam Girdwood static void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
2953e0c72dSLiam Girdwood {
3053e0c72dSLiam Girdwood 	u8 *str;
3153e0c72dSLiam Girdwood 	u8 *str2 = NULL;
3253e0c72dSLiam Girdwood 	u32 glb;
3353e0c72dSLiam Girdwood 	u32 type;
34277ff236SPierre-Louis Bossart 	bool vdbg = false;
3553e0c72dSLiam Girdwood 
3653e0c72dSLiam Girdwood 	glb = cmd & SOF_GLB_TYPE_MASK;
3753e0c72dSLiam Girdwood 	type = cmd & SOF_CMD_TYPE_MASK;
3853e0c72dSLiam Girdwood 
3953e0c72dSLiam Girdwood 	switch (glb) {
4053e0c72dSLiam Girdwood 	case SOF_IPC_GLB_REPLY:
4153e0c72dSLiam Girdwood 		str = "GLB_REPLY"; break;
4253e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMPOUND:
4353e0c72dSLiam Girdwood 		str = "GLB_COMPOUND"; break;
4453e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TPLG_MSG:
4553e0c72dSLiam Girdwood 		str = "GLB_TPLG_MSG";
4653e0c72dSLiam Girdwood 		switch (type) {
4753e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_COMP_NEW:
4853e0c72dSLiam Girdwood 			str2 = "COMP_NEW"; break;
4953e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_COMP_FREE:
5053e0c72dSLiam Girdwood 			str2 = "COMP_FREE"; break;
5153e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_COMP_CONNECT:
5253e0c72dSLiam Girdwood 			str2 = "COMP_CONNECT"; break;
5353e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_NEW:
5453e0c72dSLiam Girdwood 			str2 = "PIPE_NEW"; break;
5553e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_FREE:
5653e0c72dSLiam Girdwood 			str2 = "PIPE_FREE"; break;
5753e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_CONNECT:
5853e0c72dSLiam Girdwood 			str2 = "PIPE_CONNECT"; break;
5953e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_PIPE_COMPLETE:
6053e0c72dSLiam Girdwood 			str2 = "PIPE_COMPLETE"; break;
6153e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_BUFFER_NEW:
6253e0c72dSLiam Girdwood 			str2 = "BUFFER_NEW"; break;
6353e0c72dSLiam Girdwood 		case SOF_IPC_TPLG_BUFFER_FREE:
6453e0c72dSLiam Girdwood 			str2 = "BUFFER_FREE"; break;
6553e0c72dSLiam Girdwood 		default:
6653e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
6753e0c72dSLiam Girdwood 		}
6853e0c72dSLiam Girdwood 		break;
6953e0c72dSLiam Girdwood 	case SOF_IPC_GLB_PM_MSG:
7053e0c72dSLiam Girdwood 		str = "GLB_PM_MSG";
7153e0c72dSLiam Girdwood 		switch (type) {
7253e0c72dSLiam Girdwood 		case SOF_IPC_PM_CTX_SAVE:
7353e0c72dSLiam Girdwood 			str2 = "CTX_SAVE"; break;
7453e0c72dSLiam Girdwood 		case SOF_IPC_PM_CTX_RESTORE:
7553e0c72dSLiam Girdwood 			str2 = "CTX_RESTORE"; break;
7653e0c72dSLiam Girdwood 		case SOF_IPC_PM_CTX_SIZE:
7753e0c72dSLiam Girdwood 			str2 = "CTX_SIZE"; break;
7853e0c72dSLiam Girdwood 		case SOF_IPC_PM_CLK_SET:
7953e0c72dSLiam Girdwood 			str2 = "CLK_SET"; break;
8053e0c72dSLiam Girdwood 		case SOF_IPC_PM_CLK_GET:
8153e0c72dSLiam Girdwood 			str2 = "CLK_GET"; break;
8253e0c72dSLiam Girdwood 		case SOF_IPC_PM_CLK_REQ:
8353e0c72dSLiam Girdwood 			str2 = "CLK_REQ"; break;
8453e0c72dSLiam Girdwood 		case SOF_IPC_PM_CORE_ENABLE:
8553e0c72dSLiam Girdwood 			str2 = "CORE_ENABLE"; break;
86a8f50cd9SCurtis Malainey 		case SOF_IPC_PM_GATE:
87a8f50cd9SCurtis Malainey 			str2 = "GATE"; break;
8853e0c72dSLiam Girdwood 		default:
8953e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
9053e0c72dSLiam Girdwood 		}
9153e0c72dSLiam Girdwood 		break;
9253e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMP_MSG:
938199a120SRanjani Sridharan 		str = "GLB_COMP_MSG";
9453e0c72dSLiam Girdwood 		switch (type) {
9553e0c72dSLiam Girdwood 		case SOF_IPC_COMP_SET_VALUE:
9653e0c72dSLiam Girdwood 			str2 = "SET_VALUE"; break;
9753e0c72dSLiam Girdwood 		case SOF_IPC_COMP_GET_VALUE:
9853e0c72dSLiam Girdwood 			str2 = "GET_VALUE"; break;
9953e0c72dSLiam Girdwood 		case SOF_IPC_COMP_SET_DATA:
10053e0c72dSLiam Girdwood 			str2 = "SET_DATA"; break;
10153e0c72dSLiam Girdwood 		case SOF_IPC_COMP_GET_DATA:
10253e0c72dSLiam Girdwood 			str2 = "GET_DATA"; break;
10353e0c72dSLiam Girdwood 		default:
10453e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
10553e0c72dSLiam Girdwood 		}
10653e0c72dSLiam Girdwood 		break;
10753e0c72dSLiam Girdwood 	case SOF_IPC_GLB_STREAM_MSG:
10853e0c72dSLiam Girdwood 		str = "GLB_STREAM_MSG";
10953e0c72dSLiam Girdwood 		switch (type) {
11053e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_PCM_PARAMS:
11153e0c72dSLiam Girdwood 			str2 = "PCM_PARAMS"; break;
11253e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_PCM_PARAMS_REPLY:
11353e0c72dSLiam Girdwood 			str2 = "PCM_REPLY"; break;
11453e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_PCM_FREE:
11553e0c72dSLiam Girdwood 			str2 = "PCM_FREE"; break;
11653e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_START:
11753e0c72dSLiam Girdwood 			str2 = "TRIG_START"; break;
11853e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_STOP:
11953e0c72dSLiam Girdwood 			str2 = "TRIG_STOP"; break;
12053e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_PAUSE:
12153e0c72dSLiam Girdwood 			str2 = "TRIG_PAUSE"; break;
12253e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_RELEASE:
12353e0c72dSLiam Girdwood 			str2 = "TRIG_RELEASE"; break;
12453e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_DRAIN:
12553e0c72dSLiam Girdwood 			str2 = "TRIG_DRAIN"; break;
12653e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_TRIG_XRUN:
12753e0c72dSLiam Girdwood 			str2 = "TRIG_XRUN"; break;
12853e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_POSITION:
129277ff236SPierre-Louis Bossart 			vdbg = true;
13053e0c72dSLiam Girdwood 			str2 = "POSITION"; break;
13153e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_VORBIS_PARAMS:
13253e0c72dSLiam Girdwood 			str2 = "VORBIS_PARAMS"; break;
13353e0c72dSLiam Girdwood 		case SOF_IPC_STREAM_VORBIS_FREE:
13453e0c72dSLiam Girdwood 			str2 = "VORBIS_FREE"; break;
13553e0c72dSLiam Girdwood 		default:
13653e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
13753e0c72dSLiam Girdwood 		}
13853e0c72dSLiam Girdwood 		break;
13953e0c72dSLiam Girdwood 	case SOF_IPC_FW_READY:
14053e0c72dSLiam Girdwood 		str = "FW_READY"; break;
14153e0c72dSLiam Girdwood 	case SOF_IPC_GLB_DAI_MSG:
14253e0c72dSLiam Girdwood 		str = "GLB_DAI_MSG";
14353e0c72dSLiam Girdwood 		switch (type) {
14453e0c72dSLiam Girdwood 		case SOF_IPC_DAI_CONFIG:
14553e0c72dSLiam Girdwood 			str2 = "CONFIG"; break;
14653e0c72dSLiam Girdwood 		case SOF_IPC_DAI_LOOPBACK:
14753e0c72dSLiam Girdwood 			str2 = "LOOPBACK"; break;
14853e0c72dSLiam Girdwood 		default:
14953e0c72dSLiam Girdwood 			str2 = "unknown type"; break;
15053e0c72dSLiam Girdwood 		}
15153e0c72dSLiam Girdwood 		break;
15253e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TRACE_MSG:
153b4e2d7ceSRanjani Sridharan 		str = "GLB_TRACE_MSG";
154b4e2d7ceSRanjani Sridharan 		switch (type) {
155b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_DMA_PARAMS:
156b4e2d7ceSRanjani Sridharan 			str2 = "DMA_PARAMS"; break;
157b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_DMA_POSITION:
158b4e2d7ceSRanjani Sridharan 			str2 = "DMA_POSITION"; break;
159b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_DMA_PARAMS_EXT:
160b4e2d7ceSRanjani Sridharan 			str2 = "DMA_PARAMS_EXT"; break;
161b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_FILTER_UPDATE:
162b4e2d7ceSRanjani Sridharan 			str2 = "FILTER_UPDATE"; break;
163b4e2d7ceSRanjani Sridharan 		case SOF_IPC_TRACE_DMA_FREE:
164b4e2d7ceSRanjani Sridharan 			str2 = "DMA_FREE"; break;
165b4e2d7ceSRanjani Sridharan 		default:
166b4e2d7ceSRanjani Sridharan 			str2 = "unknown type"; break;
167b4e2d7ceSRanjani Sridharan 		}
168b4e2d7ceSRanjani Sridharan 		break;
16953b22d25SRanjani Sridharan 	case SOF_IPC_GLB_TEST_MSG:
17053b22d25SRanjani Sridharan 		str = "GLB_TEST_MSG";
17153b22d25SRanjani Sridharan 		switch (type) {
17253b22d25SRanjani Sridharan 		case SOF_IPC_TEST_IPC_FLOOD:
17353b22d25SRanjani Sridharan 			str2 = "IPC_FLOOD"; break;
17453b22d25SRanjani Sridharan 		default:
17553b22d25SRanjani Sridharan 			str2 = "unknown type"; break;
17653b22d25SRanjani Sridharan 		}
17753b22d25SRanjani Sridharan 		break;
1785b10b629SKarol Trzcinski 	case SOF_IPC_GLB_DEBUG:
1795b10b629SKarol Trzcinski 		str = "GLB_DEBUG";
1805b10b629SKarol Trzcinski 		switch (type) {
1815b10b629SKarol Trzcinski 		case SOF_IPC_DEBUG_MEM_USAGE:
1825b10b629SKarol Trzcinski 			str2 = "MEM_USAGE"; break;
1835b10b629SKarol Trzcinski 		default:
1845b10b629SKarol Trzcinski 			str2 = "unknown type"; break;
1855b10b629SKarol Trzcinski 		}
1865b10b629SKarol Trzcinski 		break;
1874ba344dcSPeter Ujfalusi 	case SOF_IPC_GLB_PROBE:
1884ba344dcSPeter Ujfalusi 		str = "GLB_PROBE";
1894ba344dcSPeter Ujfalusi 		switch (type) {
1904ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_INIT:
1914ba344dcSPeter Ujfalusi 			str2 = "INIT"; break;
1924ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_DEINIT:
1934ba344dcSPeter Ujfalusi 			str2 = "DEINIT"; break;
1944ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_DMA_ADD:
1954ba344dcSPeter Ujfalusi 			str2 = "DMA_ADD"; break;
1964ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_DMA_INFO:
1974ba344dcSPeter Ujfalusi 			str2 = "DMA_INFO"; break;
1984ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_DMA_REMOVE:
1994ba344dcSPeter Ujfalusi 			str2 = "DMA_REMOVE"; break;
2004ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_POINT_ADD:
2014ba344dcSPeter Ujfalusi 			str2 = "POINT_ADD"; break;
2024ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_POINT_INFO:
2034ba344dcSPeter Ujfalusi 			str2 = "POINT_INFO"; break;
2044ba344dcSPeter Ujfalusi 		case SOF_IPC_PROBE_POINT_REMOVE:
2054ba344dcSPeter Ujfalusi 			str2 = "POINT_REMOVE"; break;
2064ba344dcSPeter Ujfalusi 		default:
2074ba344dcSPeter Ujfalusi 			str2 = "unknown type"; break;
2084ba344dcSPeter Ujfalusi 		}
2094ba344dcSPeter Ujfalusi 		break;
21053e0c72dSLiam Girdwood 	default:
21153e0c72dSLiam Girdwood 		str = "unknown GLB command"; break;
21253e0c72dSLiam Girdwood 	}
21353e0c72dSLiam Girdwood 
214277ff236SPierre-Louis Bossart 	if (str2) {
215277ff236SPierre-Louis Bossart 		if (vdbg)
216277ff236SPierre-Louis Bossart 			dev_vdbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
21753e0c72dSLiam Girdwood 		else
218277ff236SPierre-Louis Bossart 			dev_dbg(dev, "%s: 0x%x: %s: %s\n", text, cmd, str, str2);
219277ff236SPierre-Louis Bossart 	} else {
22053e0c72dSLiam Girdwood 		dev_dbg(dev, "%s: 0x%x: %s\n", text, cmd, str);
22153e0c72dSLiam Girdwood 	}
222277ff236SPierre-Louis Bossart }
22353e0c72dSLiam Girdwood #else
22453e0c72dSLiam Girdwood static inline void ipc_log_header(struct device *dev, u8 *text, u32 cmd)
22553e0c72dSLiam Girdwood {
2264cee883eSDragos Tarcatu 	if ((cmd & SOF_GLB_TYPE_MASK) != SOF_IPC_GLB_TRACE_MSG)
22753e0c72dSLiam Girdwood 		dev_dbg(dev, "%s: 0x%x\n", text, cmd);
22853e0c72dSLiam Girdwood }
22953e0c72dSLiam Girdwood #endif
23053e0c72dSLiam Girdwood 
231b4dcafe4SPeter Ujfalusi /**
232b4dcafe4SPeter Ujfalusi  * sof_ipc_send_msg - generic function to prepare and send one IPC message
233b4dcafe4SPeter Ujfalusi  * @sdev:		pointer to SOF core device struct
234b4dcafe4SPeter Ujfalusi  * @msg_data:		pointer to a message to send
235b4dcafe4SPeter Ujfalusi  * @msg_bytes:		number of bytes in the message
236b4dcafe4SPeter Ujfalusi  * @reply_bytes:	number of bytes available for the reply.
237b4dcafe4SPeter Ujfalusi  *			The buffer for the reply data is not passed to this
238b4dcafe4SPeter Ujfalusi  *			function, the available size is an information for the
239b4dcafe4SPeter Ujfalusi  *			reply handling functions.
240b4dcafe4SPeter Ujfalusi  *
241b4dcafe4SPeter Ujfalusi  * On success the function returns 0, otherwise negative error number.
242b4dcafe4SPeter Ujfalusi  *
243b4dcafe4SPeter Ujfalusi  * Note: higher level sdev->ipc->tx_mutex must be held to make sure that
244b4dcafe4SPeter Ujfalusi  *	 transfers are synchronized.
245b4dcafe4SPeter Ujfalusi  */
246b4dcafe4SPeter Ujfalusi int sof_ipc_send_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_bytes,
247b4dcafe4SPeter Ujfalusi 		     size_t reply_bytes)
248b4dcafe4SPeter Ujfalusi {
249b4dcafe4SPeter Ujfalusi 	struct snd_sof_ipc *ipc = sdev->ipc;
250b4dcafe4SPeter Ujfalusi 	struct snd_sof_ipc_msg *msg;
251b4dcafe4SPeter Ujfalusi 	int ret;
252b4dcafe4SPeter Ujfalusi 
253b4dcafe4SPeter Ujfalusi 	if (ipc->disable_ipc_tx || sdev->fw_state != SOF_FW_BOOT_COMPLETE)
254b4dcafe4SPeter Ujfalusi 		return -ENODEV;
255b4dcafe4SPeter Ujfalusi 
256b4dcafe4SPeter Ujfalusi 	/*
257b4dcafe4SPeter Ujfalusi 	 * The spin-lock is needed to protect message objects against other
258b4dcafe4SPeter Ujfalusi 	 * atomic contexts.
259b4dcafe4SPeter Ujfalusi 	 */
260b4dcafe4SPeter Ujfalusi 	spin_lock_irq(&sdev->ipc_lock);
261b4dcafe4SPeter Ujfalusi 
262b4dcafe4SPeter Ujfalusi 	/* initialise the message */
263b4dcafe4SPeter Ujfalusi 	msg = &ipc->msg;
264b4dcafe4SPeter Ujfalusi 
265b4dcafe4SPeter Ujfalusi 	/* attach message data */
266b4dcafe4SPeter Ujfalusi 	msg->msg_data = msg_data;
267b4dcafe4SPeter Ujfalusi 	msg->msg_size = msg_bytes;
268b4dcafe4SPeter Ujfalusi 
269b4dcafe4SPeter Ujfalusi 	msg->reply_size = reply_bytes;
270b4dcafe4SPeter Ujfalusi 	msg->reply_error = 0;
271b4dcafe4SPeter Ujfalusi 
272b4dcafe4SPeter Ujfalusi 	sdev->msg = msg;
273b4dcafe4SPeter Ujfalusi 
274b4dcafe4SPeter Ujfalusi 	ret = snd_sof_dsp_send_msg(sdev, msg);
275b4dcafe4SPeter Ujfalusi 	/* Next reply that we receive will be related to this message */
276b4dcafe4SPeter Ujfalusi 	if (!ret)
277b4dcafe4SPeter Ujfalusi 		msg->ipc_complete = false;
278b4dcafe4SPeter Ujfalusi 
279b4dcafe4SPeter Ujfalusi 	spin_unlock_irq(&sdev->ipc_lock);
280b4dcafe4SPeter Ujfalusi 
281b4dcafe4SPeter Ujfalusi 	return ret;
282b4dcafe4SPeter Ujfalusi }
283b4dcafe4SPeter Ujfalusi 
28453e0c72dSLiam Girdwood /* send IPC message from host to DSP */
2852a51c0f8SPeter Ujfalusi int sof_ipc_tx_message(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
2862a51c0f8SPeter Ujfalusi 		       void *reply_data, size_t reply_bytes)
28753e0c72dSLiam Girdwood {
288*85d0f881SPeter Ujfalusi 	if (msg_bytes > ipc->max_payload_size ||
289*85d0f881SPeter Ujfalusi 	    reply_bytes > ipc->max_payload_size)
290*85d0f881SPeter Ujfalusi 		return -ENOBUFS;
29163e51fd3SRanjani Sridharan 
292*85d0f881SPeter Ujfalusi 	return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data,
293*85d0f881SPeter Ujfalusi 				reply_bytes, false);
29463e51fd3SRanjani Sridharan }
29563e51fd3SRanjani Sridharan EXPORT_SYMBOL(sof_ipc_tx_message);
29663e51fd3SRanjani Sridharan 
29763e51fd3SRanjani Sridharan /*
29863e51fd3SRanjani Sridharan  * send IPC message from host to DSP without modifying the DSP state.
29963e51fd3SRanjani Sridharan  * This will be used for IPC's that can be handled by the DSP
30063e51fd3SRanjani Sridharan  * even in a low-power D0 substate.
30163e51fd3SRanjani Sridharan  */
3022a51c0f8SPeter Ujfalusi int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
30363e51fd3SRanjani Sridharan 			     void *reply_data, size_t reply_bytes)
30463e51fd3SRanjani Sridharan {
30578935913SPeter Ujfalusi 	if (msg_bytes > ipc->max_payload_size ||
30678935913SPeter Ujfalusi 	    reply_bytes > ipc->max_payload_size)
30753e0c72dSLiam Girdwood 		return -ENOBUFS;
30853e0c72dSLiam Girdwood 
309*85d0f881SPeter Ujfalusi 	return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data,
310*85d0f881SPeter Ujfalusi 				reply_bytes, true);
31153e0c72dSLiam Girdwood }
31263e51fd3SRanjani Sridharan EXPORT_SYMBOL(sof_ipc_tx_message_no_pm);
31353e0c72dSLiam Girdwood 
3148ae77801SPeter Ujfalusi /* Generic helper function to retrieve the reply */
3158ae77801SPeter Ujfalusi void snd_sof_ipc_get_reply(struct snd_sof_dev *sdev)
3168ae77801SPeter Ujfalusi {
3178ae77801SPeter Ujfalusi 	/*
3188ae77801SPeter Ujfalusi 	 * Sometimes, there is unexpected reply ipc arriving. The reply
3198ae77801SPeter Ujfalusi 	 * ipc belongs to none of the ipcs sent from driver.
3208ae77801SPeter Ujfalusi 	 * In this case, the driver must ignore the ipc.
3218ae77801SPeter Ujfalusi 	 */
322045bc49bSPeter Ujfalusi 	if (!sdev->msg) {
3238ae77801SPeter Ujfalusi 		dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
3248ae77801SPeter Ujfalusi 		return;
3258ae77801SPeter Ujfalusi 	}
3268ae77801SPeter Ujfalusi 
327045bc49bSPeter Ujfalusi 	sdev->msg->reply_error = sdev->ipc->ops->get_reply(sdev);
3288ae77801SPeter Ujfalusi }
3298ae77801SPeter Ujfalusi EXPORT_SYMBOL(snd_sof_ipc_get_reply);
3308ae77801SPeter Ujfalusi 
33153e0c72dSLiam Girdwood /* handle reply message from DSP */
332d7a1ed26SRanjani Sridharan void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id)
33353e0c72dSLiam Girdwood {
33453e0c72dSLiam Girdwood 	struct snd_sof_ipc_msg *msg = &sdev->ipc->msg;
33553e0c72dSLiam Girdwood 
33653e0c72dSLiam Girdwood 	if (msg->ipc_complete) {
337d7a1ed26SRanjani Sridharan 		dev_dbg(sdev->dev,
338d7a1ed26SRanjani Sridharan 			"no reply expected, received 0x%x, will be ignored",
33953e0c72dSLiam Girdwood 			msg_id);
340d7a1ed26SRanjani Sridharan 		return;
34153e0c72dSLiam Girdwood 	}
34253e0c72dSLiam Girdwood 
34353e0c72dSLiam Girdwood 	/* wake up and return the error if we have waiters on this message ? */
34453e0c72dSLiam Girdwood 	msg->ipc_complete = true;
34553e0c72dSLiam Girdwood 	wake_up(&msg->waitq);
34653e0c72dSLiam Girdwood }
34753e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_reply);
34853e0c72dSLiam Girdwood 
349ab3a2189SPeter Ujfalusi static void ipc_comp_notification(struct snd_sof_dev *sdev, void *msg_buf)
350756bbe42SPeter Ujfalusi {
35110f461d7SRanjani Sridharan 	const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
352ab3a2189SPeter Ujfalusi 	struct sof_ipc_cmd_hdr *hdr = msg_buf;
353756bbe42SPeter Ujfalusi 	u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
354756bbe42SPeter Ujfalusi 
355756bbe42SPeter Ujfalusi 	switch (msg_type) {
356756bbe42SPeter Ujfalusi 	case SOF_IPC_COMP_GET_VALUE:
357756bbe42SPeter Ujfalusi 	case SOF_IPC_COMP_GET_DATA:
358756bbe42SPeter Ujfalusi 		break;
359756bbe42SPeter Ujfalusi 	default:
360756bbe42SPeter Ujfalusi 		dev_err(sdev->dev, "error: unhandled component message %#x\n", msg_type);
361756bbe42SPeter Ujfalusi 		return;
362756bbe42SPeter Ujfalusi 	}
363756bbe42SPeter Ujfalusi 
36410f461d7SRanjani Sridharan 	if (tplg_ops->control->update)
36510f461d7SRanjani Sridharan 		tplg_ops->control->update(sdev, msg_buf);
366756bbe42SPeter Ujfalusi }
367756bbe42SPeter Ujfalusi 
36853e0c72dSLiam Girdwood /* DSP firmware has sent host a message  */
36953e0c72dSLiam Girdwood void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev)
37053e0c72dSLiam Girdwood {
371ab3a2189SPeter Ujfalusi 	ipc_rx_callback rx_callback = NULL;
37253e0c72dSLiam Girdwood 	struct sof_ipc_cmd_hdr hdr;
373ab3a2189SPeter Ujfalusi 	void *msg_buf;
374ab3a2189SPeter Ujfalusi 	u32 cmd;
3756a0ba071SGuennadi Liakhovetski 	int err;
37653e0c72dSLiam Girdwood 
37753e0c72dSLiam Girdwood 	/* read back header */
3786a0ba071SGuennadi Liakhovetski 	err = snd_sof_ipc_msg_data(sdev, NULL, &hdr, sizeof(hdr));
3796a0ba071SGuennadi Liakhovetski 	if (err < 0) {
3806a0ba071SGuennadi Liakhovetski 		dev_warn(sdev->dev, "failed to read IPC header: %d\n", err);
3816a0ba071SGuennadi Liakhovetski 		return;
3826a0ba071SGuennadi Liakhovetski 	}
383ab3a2189SPeter Ujfalusi 
384ab3a2189SPeter Ujfalusi 	if (hdr.size < sizeof(hdr)) {
385ab3a2189SPeter Ujfalusi 		dev_err(sdev->dev, "The received message size is invalid\n");
386ab3a2189SPeter Ujfalusi 		return;
387ab3a2189SPeter Ujfalusi 	}
388ab3a2189SPeter Ujfalusi 
38953e0c72dSLiam Girdwood 	ipc_log_header(sdev->dev, "ipc rx", hdr.cmd);
39053e0c72dSLiam Girdwood 
39153e0c72dSLiam Girdwood 	cmd = hdr.cmd & SOF_GLB_TYPE_MASK;
39253e0c72dSLiam Girdwood 
39353e0c72dSLiam Girdwood 	/* check message type */
39453e0c72dSLiam Girdwood 	switch (cmd) {
39553e0c72dSLiam Girdwood 	case SOF_IPC_GLB_REPLY:
39653e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: ipc reply unknown\n");
39753e0c72dSLiam Girdwood 		break;
39853e0c72dSLiam Girdwood 	case SOF_IPC_FW_READY:
39953e0c72dSLiam Girdwood 		/* check for FW boot completion */
4006ca5cecbSRanjani Sridharan 		if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS) {
40153e0c72dSLiam Girdwood 			err = sof_ops(sdev)->fw_ready(sdev, cmd);
4026ca5cecbSRanjani Sridharan 			if (err < 0)
40358a5c9a4SPeter Ujfalusi 				sof_set_fw_state(sdev, SOF_FW_BOOT_READY_FAILED);
4046ca5cecbSRanjani Sridharan 			else
405b2e9eb3aSPeter Ujfalusi 				sof_set_fw_state(sdev, SOF_FW_BOOT_READY_OK);
40653e0c72dSLiam Girdwood 
40753e0c72dSLiam Girdwood 			/* wake up firmware loader */
40853e0c72dSLiam Girdwood 			wake_up(&sdev->boot_wait);
40953e0c72dSLiam Girdwood 		}
41053e0c72dSLiam Girdwood 		break;
41153e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMPOUND:
41253e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TPLG_MSG:
41353e0c72dSLiam Girdwood 	case SOF_IPC_GLB_PM_MSG:
414756bbe42SPeter Ujfalusi 		break;
41553e0c72dSLiam Girdwood 	case SOF_IPC_GLB_COMP_MSG:
416ab3a2189SPeter Ujfalusi 		rx_callback = ipc_comp_notification;
41753e0c72dSLiam Girdwood 		break;
41853e0c72dSLiam Girdwood 	case SOF_IPC_GLB_STREAM_MSG:
419ab3a2189SPeter Ujfalusi 		rx_callback = ipc_stream_message;
42053e0c72dSLiam Girdwood 		break;
42153e0c72dSLiam Girdwood 	case SOF_IPC_GLB_TRACE_MSG:
422ab3a2189SPeter Ujfalusi 		rx_callback = ipc_trace_message;
42353e0c72dSLiam Girdwood 		break;
42453e0c72dSLiam Girdwood 	default:
425ab3a2189SPeter Ujfalusi 		dev_err(sdev->dev, "%s: Unknown DSP message: 0x%x\n", __func__, cmd);
42653e0c72dSLiam Girdwood 		break;
42753e0c72dSLiam Girdwood 	}
42853e0c72dSLiam Girdwood 
4296955d951SPeter Ujfalusi 	/* read the full message */
430ab3a2189SPeter Ujfalusi 	msg_buf = kmalloc(hdr.size, GFP_KERNEL);
431ab3a2189SPeter Ujfalusi 	if (!msg_buf)
432ab3a2189SPeter Ujfalusi 		return;
433ab3a2189SPeter Ujfalusi 
434ab3a2189SPeter Ujfalusi 	err = snd_sof_ipc_msg_data(sdev, NULL, msg_buf, hdr.size);
4356955d951SPeter Ujfalusi 	if (err < 0) {
4366955d951SPeter Ujfalusi 		dev_err(sdev->dev, "%s: Failed to read message: %d\n", __func__, err);
4376955d951SPeter Ujfalusi 	} else {
4386955d951SPeter Ujfalusi 		/* Call local handler for the message */
4396955d951SPeter Ujfalusi 		if (rx_callback)
440ab3a2189SPeter Ujfalusi 			rx_callback(sdev, msg_buf);
441ab3a2189SPeter Ujfalusi 
4426955d951SPeter Ujfalusi 		/* Notify registered clients */
4436955d951SPeter Ujfalusi 		sof_client_ipc_rx_dispatcher(sdev, msg_buf);
444ab3a2189SPeter Ujfalusi 	}
445ab3a2189SPeter Ujfalusi 
4466955d951SPeter Ujfalusi 	kfree(msg_buf);
4476955d951SPeter Ujfalusi 
44853e0c72dSLiam Girdwood 	ipc_log_header(sdev->dev, "ipc rx done", hdr.cmd);
44953e0c72dSLiam Girdwood }
45053e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_msgs_rx);
45153e0c72dSLiam Girdwood 
45253e0c72dSLiam Girdwood /*
45353e0c72dSLiam Girdwood  * IPC trace mechanism.
45453e0c72dSLiam Girdwood  */
45553e0c72dSLiam Girdwood 
456ab3a2189SPeter Ujfalusi static void ipc_trace_message(struct snd_sof_dev *sdev, void *msg_buf)
45753e0c72dSLiam Girdwood {
458ab3a2189SPeter Ujfalusi 	struct sof_ipc_cmd_hdr *hdr = msg_buf;
459ab3a2189SPeter Ujfalusi 	u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
46053e0c72dSLiam Girdwood 
46159fdde1dSPeter Ujfalusi 	switch (msg_type) {
46253e0c72dSLiam Girdwood 	case SOF_IPC_TRACE_DMA_POSITION:
463ab3a2189SPeter Ujfalusi 		snd_sof_trace_update_pos(sdev, msg_buf);
46453e0c72dSLiam Girdwood 		break;
46553e0c72dSLiam Girdwood 	default:
466b95b6451SPeter Ujfalusi 		dev_err(sdev->dev, "error: unhandled trace message %#x\n", msg_type);
46753e0c72dSLiam Girdwood 		break;
46853e0c72dSLiam Girdwood 	}
46953e0c72dSLiam Girdwood }
47053e0c72dSLiam Girdwood 
47153e0c72dSLiam Girdwood /*
47253e0c72dSLiam Girdwood  * IPC stream position.
47353e0c72dSLiam Girdwood  */
47453e0c72dSLiam Girdwood 
47553e0c72dSLiam Girdwood static void ipc_period_elapsed(struct snd_sof_dev *sdev, u32 msg_id)
47653e0c72dSLiam Girdwood {
477ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = sdev->component;
47853e0c72dSLiam Girdwood 	struct snd_sof_pcm_stream *stream;
47953e0c72dSLiam Girdwood 	struct sof_ipc_stream_posn posn;
48053e0c72dSLiam Girdwood 	struct snd_sof_pcm *spcm;
4816a0ba071SGuennadi Liakhovetski 	int direction, ret;
48253e0c72dSLiam Girdwood 
483ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
48453e0c72dSLiam Girdwood 	if (!spcm) {
48553e0c72dSLiam Girdwood 		dev_err(sdev->dev,
48653e0c72dSLiam Girdwood 			"error: period elapsed for unknown stream, msg_id %d\n",
48753e0c72dSLiam Girdwood 			msg_id);
48853e0c72dSLiam Girdwood 		return;
48953e0c72dSLiam Girdwood 	}
49053e0c72dSLiam Girdwood 
49153e0c72dSLiam Girdwood 	stream = &spcm->stream[direction];
4926a0ba071SGuennadi Liakhovetski 	ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
4936a0ba071SGuennadi Liakhovetski 	if (ret < 0) {
4946a0ba071SGuennadi Liakhovetski 		dev_warn(sdev->dev, "failed to read stream position: %d\n", ret);
4956a0ba071SGuennadi Liakhovetski 		return;
4966a0ba071SGuennadi Liakhovetski 	}
49753e0c72dSLiam Girdwood 
498277ff236SPierre-Louis Bossart 	dev_vdbg(sdev->dev, "posn : host 0x%llx dai 0x%llx wall 0x%llx\n",
49953e0c72dSLiam Girdwood 		 posn.host_posn, posn.dai_posn, posn.wallclock);
50053e0c72dSLiam Girdwood 
50153e0c72dSLiam Girdwood 	memcpy(&stream->posn, &posn, sizeof(posn));
50253e0c72dSLiam Girdwood 
503858f7a5cSDaniel Baluta 	if (spcm->pcm.compress)
504858f7a5cSDaniel Baluta 		snd_sof_compr_fragment_elapsed(stream->cstream);
505182b682bSAjit Kumar Pandey 	else if (stream->substream->runtime &&
506182b682bSAjit Kumar Pandey 		 !stream->substream->runtime->no_period_wakeup)
50753e0c72dSLiam Girdwood 		/* only inform ALSA for period_wakeup mode */
508ff758e9fSKeyon Jie 		snd_sof_pcm_period_elapsed(stream->substream);
50953e0c72dSLiam Girdwood }
51053e0c72dSLiam Girdwood 
51153e0c72dSLiam Girdwood /* DSP notifies host of an XRUN within FW */
51253e0c72dSLiam Girdwood static void ipc_xrun(struct snd_sof_dev *sdev, u32 msg_id)
51353e0c72dSLiam Girdwood {
514ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = sdev->component;
51553e0c72dSLiam Girdwood 	struct snd_sof_pcm_stream *stream;
51653e0c72dSLiam Girdwood 	struct sof_ipc_stream_posn posn;
51753e0c72dSLiam Girdwood 	struct snd_sof_pcm *spcm;
5186a0ba071SGuennadi Liakhovetski 	int direction, ret;
51953e0c72dSLiam Girdwood 
520ee1e79b7SRanjani Sridharan 	spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction);
52153e0c72dSLiam Girdwood 	if (!spcm) {
52253e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: XRUN for unknown stream, msg_id %d\n",
52353e0c72dSLiam Girdwood 			msg_id);
52453e0c72dSLiam Girdwood 		return;
52553e0c72dSLiam Girdwood 	}
52653e0c72dSLiam Girdwood 
52753e0c72dSLiam Girdwood 	stream = &spcm->stream[direction];
5286a0ba071SGuennadi Liakhovetski 	ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn));
5296a0ba071SGuennadi Liakhovetski 	if (ret < 0) {
5306a0ba071SGuennadi Liakhovetski 		dev_warn(sdev->dev, "failed to read overrun position: %d\n", ret);
5316a0ba071SGuennadi Liakhovetski 		return;
5326a0ba071SGuennadi Liakhovetski 	}
53353e0c72dSLiam Girdwood 
53453e0c72dSLiam Girdwood 	dev_dbg(sdev->dev,  "posn XRUN: host %llx comp %d size %d\n",
53553e0c72dSLiam Girdwood 		posn.host_posn, posn.xrun_comp_id, posn.xrun_size);
53653e0c72dSLiam Girdwood 
53753e0c72dSLiam Girdwood #if defined(CONFIG_SND_SOC_SOF_DEBUG_XRUN_STOP)
53853e0c72dSLiam Girdwood 	/* stop PCM on XRUN - used for pipeline debug */
53953e0c72dSLiam Girdwood 	memcpy(&stream->posn, &posn, sizeof(posn));
54053e0c72dSLiam Girdwood 	snd_pcm_stop_xrun(stream->substream);
54153e0c72dSLiam Girdwood #endif
54253e0c72dSLiam Girdwood }
54353e0c72dSLiam Girdwood 
54453e0c72dSLiam Girdwood /* stream notifications from DSP FW */
545ab3a2189SPeter Ujfalusi static void ipc_stream_message(struct snd_sof_dev *sdev, void *msg_buf)
54653e0c72dSLiam Girdwood {
547ab3a2189SPeter Ujfalusi 	struct sof_ipc_cmd_hdr *hdr = msg_buf;
548ab3a2189SPeter Ujfalusi 	u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK;
549ab3a2189SPeter Ujfalusi 	u32 msg_id = SOF_IPC_MESSAGE_ID(hdr->cmd);
55053e0c72dSLiam Girdwood 
55153e0c72dSLiam Girdwood 	switch (msg_type) {
55253e0c72dSLiam Girdwood 	case SOF_IPC_STREAM_POSITION:
55353e0c72dSLiam Girdwood 		ipc_period_elapsed(sdev, msg_id);
55453e0c72dSLiam Girdwood 		break;
55553e0c72dSLiam Girdwood 	case SOF_IPC_STREAM_TRIG_XRUN:
55653e0c72dSLiam Girdwood 		ipc_xrun(sdev, msg_id);
55753e0c72dSLiam Girdwood 		break;
55853e0c72dSLiam Girdwood 	default:
559b95b6451SPeter Ujfalusi 		dev_err(sdev->dev, "error: unhandled stream message %#x\n",
56053e0c72dSLiam Girdwood 			msg_id);
56153e0c72dSLiam Girdwood 		break;
56253e0c72dSLiam Girdwood 	}
56353e0c72dSLiam Girdwood }
56453e0c72dSLiam Girdwood 
56553e0c72dSLiam Girdwood /* get stream position IPC - use faster MMIO method if available on platform */
566ee1e79b7SRanjani Sridharan int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp,
56753e0c72dSLiam Girdwood 			    struct snd_sof_pcm *spcm, int direction,
56853e0c72dSLiam Girdwood 			    struct sof_ipc_stream_posn *posn)
56953e0c72dSLiam Girdwood {
570ee1e79b7SRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
57153e0c72dSLiam Girdwood 	struct sof_ipc_stream stream;
57253e0c72dSLiam Girdwood 	int err;
57353e0c72dSLiam Girdwood 
57453e0c72dSLiam Girdwood 	/* read position via slower IPC */
57553e0c72dSLiam Girdwood 	stream.hdr.size = sizeof(stream);
57653e0c72dSLiam Girdwood 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_POSITION;
57753e0c72dSLiam Girdwood 	stream.comp_id = spcm->stream[direction].comp_id;
57853e0c72dSLiam Girdwood 
57953e0c72dSLiam Girdwood 	/* send IPC to the DSP */
5802a51c0f8SPeter Ujfalusi 	err = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), posn,
58153e0c72dSLiam Girdwood 				 sizeof(*posn));
58253e0c72dSLiam Girdwood 	if (err < 0) {
58353e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: failed to get stream %d position\n",
58453e0c72dSLiam Girdwood 			stream.comp_id);
58553e0c72dSLiam Girdwood 		return err;
58653e0c72dSLiam Girdwood 	}
58753e0c72dSLiam Girdwood 
58853e0c72dSLiam Girdwood 	return 0;
58953e0c72dSLiam Girdwood }
59053e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_stream_posn);
59153e0c72dSLiam Girdwood 
59253e0c72dSLiam Girdwood /*
59353e0c72dSLiam Girdwood  * IPC get()/set() for kcontrols.
59453e0c72dSLiam Girdwood  */
59547d7328fSPeter Ujfalusi int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol, bool set)
59653e0c72dSLiam Girdwood {
597ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
598b5cee8feSRanjani Sridharan 	struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
599ee1e79b7SRanjani Sridharan 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
600*85d0f881SPeter Ujfalusi 	const struct sof_ipc_ops *iops = sdev->ipc->ops;
60147d7328fSPeter Ujfalusi 	enum sof_ipc_ctrl_type ctrl_type;
6028b001416SRanjani Sridharan 	struct snd_sof_widget *swidget;
6038b001416SRanjani Sridharan 	bool widget_found = false;
604*85d0f881SPeter Ujfalusi 	u32 ipc_cmd, msg_bytes;
60553e0c72dSLiam Girdwood 
6068b001416SRanjani Sridharan 	list_for_each_entry(swidget, &sdev->widget_list, list) {
6078b001416SRanjani Sridharan 		if (swidget->comp_id == scontrol->comp_id) {
6088b001416SRanjani Sridharan 			widget_found = true;
6098b001416SRanjani Sridharan 			break;
6108b001416SRanjani Sridharan 		}
6118b001416SRanjani Sridharan 	}
6128b001416SRanjani Sridharan 
6138b001416SRanjani Sridharan 	if (!widget_found) {
614*85d0f881SPeter Ujfalusi 		dev_err(sdev->dev, "%s: can't find widget with id %d\n", __func__,
615*85d0f881SPeter Ujfalusi 			scontrol->comp_id);
6168b001416SRanjani Sridharan 		return -EINVAL;
6178b001416SRanjani Sridharan 	}
6188b001416SRanjani Sridharan 
6198b001416SRanjani Sridharan 	/*
6208b001416SRanjani Sridharan 	 * Volatile controls should always be part of static pipelines and the widget use_count
6218b001416SRanjani Sridharan 	 * would always be > 0 in this case. For the others, just return the cached value if the
6228b001416SRanjani Sridharan 	 * widget is not set up.
6238b001416SRanjani Sridharan 	 */
6248b001416SRanjani Sridharan 	if (!swidget->use_count)
6258b001416SRanjani Sridharan 		return 0;
6268b001416SRanjani Sridharan 
62747d7328fSPeter Ujfalusi 	/*
62847d7328fSPeter Ujfalusi 	 * Select the IPC cmd and the ctrl_type based on the ctrl_cmd and the
62947d7328fSPeter Ujfalusi 	 * direction
63047d7328fSPeter Ujfalusi 	 * Note: SOF_CTRL_TYPE_VALUE_COMP_* is not used and supported currently
63147d7328fSPeter Ujfalusi 	 *	 for ctrl_type
63247d7328fSPeter Ujfalusi 	 */
63347d7328fSPeter Ujfalusi 	if (cdata->cmd == SOF_CTRL_CMD_BINARY) {
634d4a06c43SPeter Ujfalusi 		ipc_cmd = set ? SOF_IPC_COMP_SET_DATA : SOF_IPC_COMP_GET_DATA;
63547d7328fSPeter Ujfalusi 		ctrl_type = set ? SOF_CTRL_TYPE_DATA_SET : SOF_CTRL_TYPE_DATA_GET;
63647d7328fSPeter Ujfalusi 	} else {
637d4a06c43SPeter Ujfalusi 		ipc_cmd = set ? SOF_IPC_COMP_SET_VALUE : SOF_IPC_COMP_GET_VALUE;
63847d7328fSPeter Ujfalusi 		ctrl_type = set ? SOF_CTRL_TYPE_VALUE_CHAN_SET : SOF_CTRL_TYPE_VALUE_CHAN_GET;
63947d7328fSPeter Ujfalusi 	}
640d4a06c43SPeter Ujfalusi 
64153e0c72dSLiam Girdwood 	cdata->rhdr.hdr.cmd = SOF_IPC_GLB_COMP_MSG | ipc_cmd;
64253e0c72dSLiam Girdwood 	cdata->type = ctrl_type;
64353e0c72dSLiam Girdwood 	cdata->comp_id = scontrol->comp_id;
64453e0c72dSLiam Girdwood 	cdata->msg_index = 0;
64553e0c72dSLiam Girdwood 
64653e0c72dSLiam Girdwood 	/* calculate header and data size */
64753e0c72dSLiam Girdwood 	switch (cdata->type) {
64853e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_GET:
64953e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_VALUE_CHAN_SET:
650*85d0f881SPeter Ujfalusi 		cdata->num_elems = scontrol->num_channels;
651*85d0f881SPeter Ujfalusi 
652*85d0f881SPeter Ujfalusi 		msg_bytes = scontrol->num_channels *
65353e0c72dSLiam Girdwood 			    sizeof(struct sof_ipc_ctrl_value_chan);
654*85d0f881SPeter Ujfalusi 		msg_bytes += sizeof(struct sof_ipc_ctrl_data);
65553e0c72dSLiam Girdwood 		break;
65653e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_GET:
65753e0c72dSLiam Girdwood 	case SOF_CTRL_TYPE_DATA_SET:
658*85d0f881SPeter Ujfalusi 		cdata->num_elems = cdata->data->size;
659*85d0f881SPeter Ujfalusi 
660*85d0f881SPeter Ujfalusi 		msg_bytes = cdata->data->size;
661*85d0f881SPeter Ujfalusi 		msg_bytes += sizeof(struct sof_ipc_ctrl_data) +
66253e0c72dSLiam Girdwood 			     sizeof(struct sof_abi_hdr);
66353e0c72dSLiam Girdwood 		break;
66453e0c72dSLiam Girdwood 	default:
66553e0c72dSLiam Girdwood 		return -EINVAL;
66653e0c72dSLiam Girdwood 	}
66753e0c72dSLiam Girdwood 
668*85d0f881SPeter Ujfalusi 	cdata->rhdr.hdr.size = msg_bytes;
66953e0c72dSLiam Girdwood 	cdata->elems_remaining = 0;
67053e0c72dSLiam Girdwood 
671*85d0f881SPeter Ujfalusi 	return iops->set_get_data(sdev, cdata, cdata->rhdr.hdr.size, set);
67253e0c72dSLiam Girdwood }
67353e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_set_get_comp_data);
67453e0c72dSLiam Girdwood 
67553e0c72dSLiam Girdwood int snd_sof_ipc_valid(struct snd_sof_dev *sdev)
67653e0c72dSLiam Girdwood {
67753e0c72dSLiam Girdwood 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
67853e0c72dSLiam Girdwood 	struct sof_ipc_fw_version *v = &ready->version;
67953e0c72dSLiam Girdwood 
68053e0c72dSLiam Girdwood 	dev_info(sdev->dev,
68153e0c72dSLiam Girdwood 		 "Firmware info: version %d:%d:%d-%s\n",  v->major, v->minor,
68253e0c72dSLiam Girdwood 		 v->micro, v->tag);
68353e0c72dSLiam Girdwood 	dev_info(sdev->dev,
68453e0c72dSLiam Girdwood 		 "Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
68553e0c72dSLiam Girdwood 		 SOF_ABI_VERSION_MAJOR(v->abi_version),
68653e0c72dSLiam Girdwood 		 SOF_ABI_VERSION_MINOR(v->abi_version),
68753e0c72dSLiam Girdwood 		 SOF_ABI_VERSION_PATCH(v->abi_version),
68853e0c72dSLiam Girdwood 		 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
68953e0c72dSLiam Girdwood 
69053e0c72dSLiam Girdwood 	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) {
69153e0c72dSLiam Girdwood 		dev_err(sdev->dev, "error: incompatible FW ABI version\n");
69253e0c72dSLiam Girdwood 		return -EINVAL;
69353e0c72dSLiam Girdwood 	}
69453e0c72dSLiam Girdwood 
69553129e66SKai Vehmanen 	if (SOF_ABI_VERSION_MINOR(v->abi_version) > SOF_ABI_MINOR) {
6964acb1c2eSPierre-Louis Bossart 		if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
6974acb1c2eSPierre-Louis Bossart 			dev_warn(sdev->dev, "warn: FW ABI is more recent than kernel\n");
6984acb1c2eSPierre-Louis Bossart 		} else {
6994acb1c2eSPierre-Louis Bossart 			dev_err(sdev->dev, "error: FW ABI is more recent than kernel\n");
7004acb1c2eSPierre-Louis Bossart 			return -EINVAL;
7014acb1c2eSPierre-Louis Bossart 		}
7024acb1c2eSPierre-Louis Bossart 	}
7034acb1c2eSPierre-Louis Bossart 
704347d1c4bSSlawomir Blauciak 	if (ready->flags & SOF_IPC_INFO_BUILD) {
70553e0c72dSLiam Girdwood 		dev_info(sdev->dev,
70653e0c72dSLiam Girdwood 			 "Firmware debug build %d on %s-%s - options:\n"
70753e0c72dSLiam Girdwood 			 " GDB: %s\n"
70853e0c72dSLiam Girdwood 			 " lock debug: %s\n"
70953e0c72dSLiam Girdwood 			 " lock vdebug: %s\n",
71053e0c72dSLiam Girdwood 			 v->build, v->date, v->time,
711847a040dSPierre-Louis Bossart 			 (ready->flags & SOF_IPC_INFO_GDB) ?
712347d1c4bSSlawomir Blauciak 				"enabled" : "disabled",
713847a040dSPierre-Louis Bossart 			 (ready->flags & SOF_IPC_INFO_LOCKS) ?
714347d1c4bSSlawomir Blauciak 				"enabled" : "disabled",
715847a040dSPierre-Louis Bossart 			 (ready->flags & SOF_IPC_INFO_LOCKSV) ?
716347d1c4bSSlawomir Blauciak 				"enabled" : "disabled");
71753e0c72dSLiam Girdwood 	}
71853e0c72dSLiam Girdwood 
71953e0c72dSLiam Girdwood 	/* copy the fw_version into debugfs at first boot */
72053e0c72dSLiam Girdwood 	memcpy(&sdev->fw_version, v, sizeof(*v));
72153e0c72dSLiam Girdwood 
72253e0c72dSLiam Girdwood 	return 0;
72353e0c72dSLiam Girdwood }
72453e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_valid);
72553e0c72dSLiam Girdwood 
726febf5da8SRander Wang int sof_ipc_init_msg_memory(struct snd_sof_dev *sdev)
727febf5da8SRander Wang {
728febf5da8SRander Wang 	struct snd_sof_ipc_msg *msg;
729febf5da8SRander Wang 
730febf5da8SRander Wang 	msg = &sdev->ipc->msg;
731febf5da8SRander Wang 
732febf5da8SRander Wang 	msg->reply_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL);
733febf5da8SRander Wang 	if (!msg->reply_data)
734febf5da8SRander Wang 		return -ENOMEM;
735febf5da8SRander Wang 
736a3e6b369SPeter Ujfalusi 	sdev->ipc->max_payload_size = SOF_IPC_MSG_MAX_SIZE;
737a3e6b369SPeter Ujfalusi 
738febf5da8SRander Wang 	return 0;
739febf5da8SRander Wang }
740febf5da8SRander Wang 
74153e0c72dSLiam Girdwood struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev)
74253e0c72dSLiam Girdwood {
74353e0c72dSLiam Girdwood 	struct snd_sof_ipc *ipc;
74453e0c72dSLiam Girdwood 	struct snd_sof_ipc_msg *msg;
745785b3fbeSPeter Ujfalusi 	const struct sof_ipc_ops *ops;
74653e0c72dSLiam Girdwood 
74753e0c72dSLiam Girdwood 	ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL);
74853e0c72dSLiam Girdwood 	if (!ipc)
74953e0c72dSLiam Girdwood 		return NULL;
75053e0c72dSLiam Girdwood 
75153e0c72dSLiam Girdwood 	mutex_init(&ipc->tx_mutex);
75253e0c72dSLiam Girdwood 	ipc->sdev = sdev;
75353e0c72dSLiam Girdwood 	msg = &ipc->msg;
75453e0c72dSLiam Girdwood 
75553e0c72dSLiam Girdwood 	/* indicate that we aren't sending a message ATM */
75653e0c72dSLiam Girdwood 	msg->ipc_complete = true;
75753e0c72dSLiam Girdwood 
75853e0c72dSLiam Girdwood 	init_waitqueue_head(&msg->waitq);
75953e0c72dSLiam Girdwood 
7607006d20eSRanjani Sridharan 	/*
7617006d20eSRanjani Sridharan 	 * Use IPC3 ops as it is the only available version now. With the addition of new IPC
7627006d20eSRanjani Sridharan 	 * versions, this will need to be modified to use the selected version at runtime.
7637006d20eSRanjani Sridharan 	 */
7647006d20eSRanjani Sridharan 	ipc->ops = &ipc3_ops;
765785b3fbeSPeter Ujfalusi 	ops = ipc->ops;
7667006d20eSRanjani Sridharan 
7677006d20eSRanjani Sridharan 	/* check for mandatory ops */
768defad9d2SPeter Ujfalusi 	if (!ops->tx_msg || !ops->rx_msg || !ops->set_get_data || !ops->get_reply) {
769defad9d2SPeter Ujfalusi 		dev_err(sdev->dev, "Missing IPC message handling ops\n");
770defad9d2SPeter Ujfalusi 		return NULL;
771defad9d2SPeter Ujfalusi 	}
772defad9d2SPeter Ujfalusi 
773785b3fbeSPeter Ujfalusi 	if (!ops->pcm) {
774785b3fbeSPeter Ujfalusi 		dev_err(sdev->dev, "Missing IPC PCM ops\n");
775785b3fbeSPeter Ujfalusi 		return NULL;
776785b3fbeSPeter Ujfalusi 	}
777785b3fbeSPeter Ujfalusi 
778785b3fbeSPeter Ujfalusi 	if (!ops->tplg || !ops->tplg->widget || !ops->tplg->control) {
779785b3fbeSPeter Ujfalusi 		dev_err(sdev->dev, "Missing IPC topology ops\n");
7807006d20eSRanjani Sridharan 		return NULL;
7817006d20eSRanjani Sridharan 	}
7827006d20eSRanjani Sridharan 
78353e0c72dSLiam Girdwood 	return ipc;
78453e0c72dSLiam Girdwood }
78553e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_init);
78653e0c72dSLiam Girdwood 
78753e0c72dSLiam Girdwood void snd_sof_ipc_free(struct snd_sof_dev *sdev)
78853e0c72dSLiam Girdwood {
78953e0c72dSLiam Girdwood 	struct snd_sof_ipc *ipc = sdev->ipc;
79053e0c72dSLiam Girdwood 
791b06e4642SKai Vehmanen 	if (!ipc)
792b06e4642SKai Vehmanen 		return;
793b06e4642SKai Vehmanen 
79453e0c72dSLiam Girdwood 	/* disable sending of ipc's */
79553e0c72dSLiam Girdwood 	mutex_lock(&ipc->tx_mutex);
79653e0c72dSLiam Girdwood 	ipc->disable_ipc_tx = true;
79753e0c72dSLiam Girdwood 	mutex_unlock(&ipc->tx_mutex);
79853e0c72dSLiam Girdwood }
79953e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_free);
800