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 struct snd_sof_ipc_msg *msg = sdev->msg; 4475db8eb5bSPeter Ujfalusi struct sof_ipc_reply *reply; 4488ae77801SPeter Ujfalusi int ret = 0; 4498ae77801SPeter Ujfalusi 4508ae77801SPeter Ujfalusi /* 4518ae77801SPeter Ujfalusi * Sometimes, there is unexpected reply ipc arriving. The reply 4528ae77801SPeter Ujfalusi * ipc belongs to none of the ipcs sent from driver. 4538ae77801SPeter Ujfalusi * In this case, the driver must ignore the ipc. 4548ae77801SPeter Ujfalusi */ 4558ae77801SPeter Ujfalusi if (!msg) { 4568ae77801SPeter Ujfalusi dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n"); 4578ae77801SPeter Ujfalusi return; 4588ae77801SPeter Ujfalusi } 4598ae77801SPeter Ujfalusi 4608ae77801SPeter Ujfalusi /* get the generic reply */ 4615db8eb5bSPeter Ujfalusi reply = msg->reply_data; 4625db8eb5bSPeter Ujfalusi snd_sof_dsp_mailbox_read(sdev, sdev->host_box.offset, reply, sizeof(*reply)); 4638ae77801SPeter Ujfalusi 4645db8eb5bSPeter Ujfalusi if (reply->error < 0) { 4655db8eb5bSPeter Ujfalusi ret = reply->error; 4665db8eb5bSPeter Ujfalusi } else if (!reply->hdr.size) { 4678ae77801SPeter Ujfalusi /* Reply should always be >= sizeof(struct sof_ipc_reply) */ 4688ae77801SPeter Ujfalusi if (msg->reply_size) 4698ae77801SPeter Ujfalusi dev_err(sdev->dev, 4708ae77801SPeter Ujfalusi "empty reply received, expected %zu bytes\n", 4718ae77801SPeter Ujfalusi msg->reply_size); 4728ae77801SPeter Ujfalusi else 4738ae77801SPeter Ujfalusi dev_err(sdev->dev, "empty reply received\n"); 4748ae77801SPeter Ujfalusi 4758ae77801SPeter Ujfalusi ret = -EINVAL; 4768ae77801SPeter Ujfalusi } else if (msg->reply_size > 0) { 4775db8eb5bSPeter Ujfalusi if (reply->hdr.size == msg->reply_size) { 4788ae77801SPeter Ujfalusi ret = 0; 4795db8eb5bSPeter Ujfalusi } else if (reply->hdr.size < msg->reply_size) { 4808ae77801SPeter Ujfalusi dev_dbg(sdev->dev, 4818ae77801SPeter Ujfalusi "reply size (%u) is less than expected (%zu)\n", 4825db8eb5bSPeter Ujfalusi reply->hdr.size, msg->reply_size); 4838ae77801SPeter Ujfalusi 4845db8eb5bSPeter Ujfalusi msg->reply_size = reply->hdr.size; 4858ae77801SPeter Ujfalusi ret = 0; 4868ae77801SPeter Ujfalusi } else { 4878ae77801SPeter Ujfalusi dev_err(sdev->dev, 4888ae77801SPeter Ujfalusi "reply size (%u) exceeds the buffer size (%zu)\n", 4895db8eb5bSPeter Ujfalusi reply->hdr.size, msg->reply_size); 4908ae77801SPeter Ujfalusi ret = -EINVAL; 4918ae77801SPeter Ujfalusi } 4928ae77801SPeter Ujfalusi 4935db8eb5bSPeter Ujfalusi /* 4945db8eb5bSPeter Ujfalusi * get the full message if reply->hdr.size <= msg->reply_size 4955db8eb5bSPeter Ujfalusi * and the reply->hdr.size > sizeof(struct sof_ipc_reply) 4965db8eb5bSPeter Ujfalusi */ 4975db8eb5bSPeter Ujfalusi if (!ret && msg->reply_size > sizeof(*reply)) 4988ae77801SPeter Ujfalusi snd_sof_dsp_mailbox_read(sdev, sdev->host_box.offset, 4998ae77801SPeter Ujfalusi msg->reply_data, msg->reply_size); 5008ae77801SPeter Ujfalusi } 5018ae77801SPeter Ujfalusi 5028ae77801SPeter Ujfalusi msg->reply_error = ret; 5038ae77801SPeter Ujfalusi } 5048ae77801SPeter Ujfalusi EXPORT_SYMBOL(snd_sof_ipc_get_reply); 5058ae77801SPeter Ujfalusi 50653e0c72dSLiam Girdwood /* handle reply message from DSP */ 507d7a1ed26SRanjani Sridharan void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id) 50853e0c72dSLiam Girdwood { 50953e0c72dSLiam Girdwood struct snd_sof_ipc_msg *msg = &sdev->ipc->msg; 51053e0c72dSLiam Girdwood 51153e0c72dSLiam Girdwood if (msg->ipc_complete) { 512d7a1ed26SRanjani Sridharan dev_dbg(sdev->dev, 513d7a1ed26SRanjani Sridharan "no reply expected, received 0x%x, will be ignored", 51453e0c72dSLiam Girdwood msg_id); 515d7a1ed26SRanjani Sridharan return; 51653e0c72dSLiam Girdwood } 51753e0c72dSLiam Girdwood 51853e0c72dSLiam Girdwood /* wake up and return the error if we have waiters on this message ? */ 51953e0c72dSLiam Girdwood msg->ipc_complete = true; 52053e0c72dSLiam Girdwood wake_up(&msg->waitq); 52153e0c72dSLiam Girdwood } 52253e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_reply); 52353e0c72dSLiam Girdwood 524ab3a2189SPeter Ujfalusi static void ipc_comp_notification(struct snd_sof_dev *sdev, void *msg_buf) 525756bbe42SPeter Ujfalusi { 52610f461d7SRanjani Sridharan const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg; 527ab3a2189SPeter Ujfalusi struct sof_ipc_cmd_hdr *hdr = msg_buf; 528756bbe42SPeter Ujfalusi u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK; 529756bbe42SPeter Ujfalusi 530756bbe42SPeter Ujfalusi switch (msg_type) { 531756bbe42SPeter Ujfalusi case SOF_IPC_COMP_GET_VALUE: 532756bbe42SPeter Ujfalusi case SOF_IPC_COMP_GET_DATA: 533756bbe42SPeter Ujfalusi break; 534756bbe42SPeter Ujfalusi default: 535756bbe42SPeter Ujfalusi dev_err(sdev->dev, "error: unhandled component message %#x\n", msg_type); 536756bbe42SPeter Ujfalusi return; 537756bbe42SPeter Ujfalusi } 538756bbe42SPeter Ujfalusi 53910f461d7SRanjani Sridharan if (tplg_ops->control->update) 54010f461d7SRanjani Sridharan tplg_ops->control->update(sdev, msg_buf); 541756bbe42SPeter Ujfalusi } 542756bbe42SPeter Ujfalusi 54353e0c72dSLiam Girdwood /* DSP firmware has sent host a message */ 54453e0c72dSLiam Girdwood void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev) 54553e0c72dSLiam Girdwood { 546ab3a2189SPeter Ujfalusi ipc_rx_callback rx_callback = NULL; 54753e0c72dSLiam Girdwood struct sof_ipc_cmd_hdr hdr; 548ab3a2189SPeter Ujfalusi void *msg_buf; 549ab3a2189SPeter Ujfalusi u32 cmd; 5506a0ba071SGuennadi Liakhovetski int err; 55153e0c72dSLiam Girdwood 55253e0c72dSLiam Girdwood /* read back header */ 5536a0ba071SGuennadi Liakhovetski err = snd_sof_ipc_msg_data(sdev, NULL, &hdr, sizeof(hdr)); 5546a0ba071SGuennadi Liakhovetski if (err < 0) { 5556a0ba071SGuennadi Liakhovetski dev_warn(sdev->dev, "failed to read IPC header: %d\n", err); 5566a0ba071SGuennadi Liakhovetski return; 5576a0ba071SGuennadi Liakhovetski } 558ab3a2189SPeter Ujfalusi 559ab3a2189SPeter Ujfalusi if (hdr.size < sizeof(hdr)) { 560ab3a2189SPeter Ujfalusi dev_err(sdev->dev, "The received message size is invalid\n"); 561ab3a2189SPeter Ujfalusi return; 562ab3a2189SPeter Ujfalusi } 563ab3a2189SPeter Ujfalusi 56453e0c72dSLiam Girdwood ipc_log_header(sdev->dev, "ipc rx", hdr.cmd); 56553e0c72dSLiam Girdwood 56653e0c72dSLiam Girdwood cmd = hdr.cmd & SOF_GLB_TYPE_MASK; 56753e0c72dSLiam Girdwood 56853e0c72dSLiam Girdwood /* check message type */ 56953e0c72dSLiam Girdwood switch (cmd) { 57053e0c72dSLiam Girdwood case SOF_IPC_GLB_REPLY: 57153e0c72dSLiam Girdwood dev_err(sdev->dev, "error: ipc reply unknown\n"); 57253e0c72dSLiam Girdwood break; 57353e0c72dSLiam Girdwood case SOF_IPC_FW_READY: 57453e0c72dSLiam Girdwood /* check for FW boot completion */ 5756ca5cecbSRanjani Sridharan if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS) { 57653e0c72dSLiam Girdwood err = sof_ops(sdev)->fw_ready(sdev, cmd); 5776ca5cecbSRanjani Sridharan if (err < 0) 57858a5c9a4SPeter Ujfalusi sof_set_fw_state(sdev, SOF_FW_BOOT_READY_FAILED); 5796ca5cecbSRanjani Sridharan else 580b2e9eb3aSPeter Ujfalusi sof_set_fw_state(sdev, SOF_FW_BOOT_READY_OK); 58153e0c72dSLiam Girdwood 58253e0c72dSLiam Girdwood /* wake up firmware loader */ 58353e0c72dSLiam Girdwood wake_up(&sdev->boot_wait); 58453e0c72dSLiam Girdwood } 58553e0c72dSLiam Girdwood break; 58653e0c72dSLiam Girdwood case SOF_IPC_GLB_COMPOUND: 58753e0c72dSLiam Girdwood case SOF_IPC_GLB_TPLG_MSG: 58853e0c72dSLiam Girdwood case SOF_IPC_GLB_PM_MSG: 589756bbe42SPeter Ujfalusi break; 59053e0c72dSLiam Girdwood case SOF_IPC_GLB_COMP_MSG: 591ab3a2189SPeter Ujfalusi rx_callback = ipc_comp_notification; 59253e0c72dSLiam Girdwood break; 59353e0c72dSLiam Girdwood case SOF_IPC_GLB_STREAM_MSG: 594ab3a2189SPeter Ujfalusi rx_callback = ipc_stream_message; 59553e0c72dSLiam Girdwood break; 59653e0c72dSLiam Girdwood case SOF_IPC_GLB_TRACE_MSG: 597ab3a2189SPeter Ujfalusi rx_callback = ipc_trace_message; 59853e0c72dSLiam Girdwood break; 59953e0c72dSLiam Girdwood default: 600ab3a2189SPeter Ujfalusi dev_err(sdev->dev, "%s: Unknown DSP message: 0x%x\n", __func__, cmd); 60153e0c72dSLiam Girdwood break; 60253e0c72dSLiam Girdwood } 60353e0c72dSLiam Girdwood 6046955d951SPeter Ujfalusi /* read the full message */ 605ab3a2189SPeter Ujfalusi msg_buf = kmalloc(hdr.size, GFP_KERNEL); 606ab3a2189SPeter Ujfalusi if (!msg_buf) 607ab3a2189SPeter Ujfalusi return; 608ab3a2189SPeter Ujfalusi 609ab3a2189SPeter Ujfalusi err = snd_sof_ipc_msg_data(sdev, NULL, msg_buf, hdr.size); 6106955d951SPeter Ujfalusi if (err < 0) { 6116955d951SPeter Ujfalusi dev_err(sdev->dev, "%s: Failed to read message: %d\n", __func__, err); 6126955d951SPeter Ujfalusi } else { 6136955d951SPeter Ujfalusi /* Call local handler for the message */ 6146955d951SPeter Ujfalusi if (rx_callback) 615ab3a2189SPeter Ujfalusi rx_callback(sdev, msg_buf); 616ab3a2189SPeter Ujfalusi 6176955d951SPeter Ujfalusi /* Notify registered clients */ 6186955d951SPeter Ujfalusi sof_client_ipc_rx_dispatcher(sdev, msg_buf); 619ab3a2189SPeter Ujfalusi } 620ab3a2189SPeter Ujfalusi 6216955d951SPeter Ujfalusi kfree(msg_buf); 6226955d951SPeter Ujfalusi 62353e0c72dSLiam Girdwood ipc_log_header(sdev->dev, "ipc rx done", hdr.cmd); 62453e0c72dSLiam Girdwood } 62553e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_msgs_rx); 62653e0c72dSLiam Girdwood 62753e0c72dSLiam Girdwood /* 62853e0c72dSLiam Girdwood * IPC trace mechanism. 62953e0c72dSLiam Girdwood */ 63053e0c72dSLiam Girdwood 631ab3a2189SPeter Ujfalusi static void ipc_trace_message(struct snd_sof_dev *sdev, void *msg_buf) 63253e0c72dSLiam Girdwood { 633ab3a2189SPeter Ujfalusi struct sof_ipc_cmd_hdr *hdr = msg_buf; 634ab3a2189SPeter Ujfalusi u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK; 63553e0c72dSLiam Girdwood 63659fdde1dSPeter Ujfalusi switch (msg_type) { 63753e0c72dSLiam Girdwood case SOF_IPC_TRACE_DMA_POSITION: 638ab3a2189SPeter Ujfalusi snd_sof_trace_update_pos(sdev, msg_buf); 63953e0c72dSLiam Girdwood break; 64053e0c72dSLiam Girdwood default: 641b95b6451SPeter Ujfalusi dev_err(sdev->dev, "error: unhandled trace message %#x\n", msg_type); 64253e0c72dSLiam Girdwood break; 64353e0c72dSLiam Girdwood } 64453e0c72dSLiam Girdwood } 64553e0c72dSLiam Girdwood 64653e0c72dSLiam Girdwood /* 64753e0c72dSLiam Girdwood * IPC stream position. 64853e0c72dSLiam Girdwood */ 64953e0c72dSLiam Girdwood 65053e0c72dSLiam Girdwood static void ipc_period_elapsed(struct snd_sof_dev *sdev, u32 msg_id) 65153e0c72dSLiam Girdwood { 652ee1e79b7SRanjani Sridharan struct snd_soc_component *scomp = sdev->component; 65353e0c72dSLiam Girdwood struct snd_sof_pcm_stream *stream; 65453e0c72dSLiam Girdwood struct sof_ipc_stream_posn posn; 65553e0c72dSLiam Girdwood struct snd_sof_pcm *spcm; 6566a0ba071SGuennadi Liakhovetski int direction, ret; 65753e0c72dSLiam Girdwood 658ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction); 65953e0c72dSLiam Girdwood if (!spcm) { 66053e0c72dSLiam Girdwood dev_err(sdev->dev, 66153e0c72dSLiam Girdwood "error: period elapsed for unknown stream, msg_id %d\n", 66253e0c72dSLiam Girdwood msg_id); 66353e0c72dSLiam Girdwood return; 66453e0c72dSLiam Girdwood } 66553e0c72dSLiam Girdwood 66653e0c72dSLiam Girdwood stream = &spcm->stream[direction]; 6676a0ba071SGuennadi Liakhovetski ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn)); 6686a0ba071SGuennadi Liakhovetski if (ret < 0) { 6696a0ba071SGuennadi Liakhovetski dev_warn(sdev->dev, "failed to read stream position: %d\n", ret); 6706a0ba071SGuennadi Liakhovetski return; 6716a0ba071SGuennadi Liakhovetski } 67253e0c72dSLiam Girdwood 673277ff236SPierre-Louis Bossart dev_vdbg(sdev->dev, "posn : host 0x%llx dai 0x%llx wall 0x%llx\n", 67453e0c72dSLiam Girdwood posn.host_posn, posn.dai_posn, posn.wallclock); 67553e0c72dSLiam Girdwood 67653e0c72dSLiam Girdwood memcpy(&stream->posn, &posn, sizeof(posn)); 67753e0c72dSLiam Girdwood 678858f7a5cSDaniel Baluta if (spcm->pcm.compress) 679858f7a5cSDaniel Baluta snd_sof_compr_fragment_elapsed(stream->cstream); 680182b682bSAjit Kumar Pandey else if (stream->substream->runtime && 681182b682bSAjit Kumar Pandey !stream->substream->runtime->no_period_wakeup) 68253e0c72dSLiam Girdwood /* only inform ALSA for period_wakeup mode */ 683ff758e9fSKeyon Jie snd_sof_pcm_period_elapsed(stream->substream); 68453e0c72dSLiam Girdwood } 68553e0c72dSLiam Girdwood 68653e0c72dSLiam Girdwood /* DSP notifies host of an XRUN within FW */ 68753e0c72dSLiam Girdwood static void ipc_xrun(struct snd_sof_dev *sdev, u32 msg_id) 68853e0c72dSLiam Girdwood { 689ee1e79b7SRanjani Sridharan struct snd_soc_component *scomp = sdev->component; 69053e0c72dSLiam Girdwood struct snd_sof_pcm_stream *stream; 69153e0c72dSLiam Girdwood struct sof_ipc_stream_posn posn; 69253e0c72dSLiam Girdwood struct snd_sof_pcm *spcm; 6936a0ba071SGuennadi Liakhovetski int direction, ret; 69453e0c72dSLiam Girdwood 695ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_comp(scomp, msg_id, &direction); 69653e0c72dSLiam Girdwood if (!spcm) { 69753e0c72dSLiam Girdwood dev_err(sdev->dev, "error: XRUN for unknown stream, msg_id %d\n", 69853e0c72dSLiam Girdwood msg_id); 69953e0c72dSLiam Girdwood return; 70053e0c72dSLiam Girdwood } 70153e0c72dSLiam Girdwood 70253e0c72dSLiam Girdwood stream = &spcm->stream[direction]; 7036a0ba071SGuennadi Liakhovetski ret = snd_sof_ipc_msg_data(sdev, stream->substream, &posn, sizeof(posn)); 7046a0ba071SGuennadi Liakhovetski if (ret < 0) { 7056a0ba071SGuennadi Liakhovetski dev_warn(sdev->dev, "failed to read overrun position: %d\n", ret); 7066a0ba071SGuennadi Liakhovetski return; 7076a0ba071SGuennadi Liakhovetski } 70853e0c72dSLiam Girdwood 70953e0c72dSLiam Girdwood dev_dbg(sdev->dev, "posn XRUN: host %llx comp %d size %d\n", 71053e0c72dSLiam Girdwood posn.host_posn, posn.xrun_comp_id, posn.xrun_size); 71153e0c72dSLiam Girdwood 71253e0c72dSLiam Girdwood #if defined(CONFIG_SND_SOC_SOF_DEBUG_XRUN_STOP) 71353e0c72dSLiam Girdwood /* stop PCM on XRUN - used for pipeline debug */ 71453e0c72dSLiam Girdwood memcpy(&stream->posn, &posn, sizeof(posn)); 71553e0c72dSLiam Girdwood snd_pcm_stop_xrun(stream->substream); 71653e0c72dSLiam Girdwood #endif 71753e0c72dSLiam Girdwood } 71853e0c72dSLiam Girdwood 71953e0c72dSLiam Girdwood /* stream notifications from DSP FW */ 720ab3a2189SPeter Ujfalusi static void ipc_stream_message(struct snd_sof_dev *sdev, void *msg_buf) 72153e0c72dSLiam Girdwood { 722ab3a2189SPeter Ujfalusi struct sof_ipc_cmd_hdr *hdr = msg_buf; 723ab3a2189SPeter Ujfalusi u32 msg_type = hdr->cmd & SOF_CMD_TYPE_MASK; 724ab3a2189SPeter Ujfalusi u32 msg_id = SOF_IPC_MESSAGE_ID(hdr->cmd); 72553e0c72dSLiam Girdwood 72653e0c72dSLiam Girdwood switch (msg_type) { 72753e0c72dSLiam Girdwood case SOF_IPC_STREAM_POSITION: 72853e0c72dSLiam Girdwood ipc_period_elapsed(sdev, msg_id); 72953e0c72dSLiam Girdwood break; 73053e0c72dSLiam Girdwood case SOF_IPC_STREAM_TRIG_XRUN: 73153e0c72dSLiam Girdwood ipc_xrun(sdev, msg_id); 73253e0c72dSLiam Girdwood break; 73353e0c72dSLiam Girdwood default: 734b95b6451SPeter Ujfalusi dev_err(sdev->dev, "error: unhandled stream message %#x\n", 73553e0c72dSLiam Girdwood msg_id); 73653e0c72dSLiam Girdwood break; 73753e0c72dSLiam Girdwood } 73853e0c72dSLiam Girdwood } 73953e0c72dSLiam Girdwood 74053e0c72dSLiam Girdwood /* get stream position IPC - use faster MMIO method if available on platform */ 741ee1e79b7SRanjani Sridharan int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp, 74253e0c72dSLiam Girdwood struct snd_sof_pcm *spcm, int direction, 74353e0c72dSLiam Girdwood struct sof_ipc_stream_posn *posn) 74453e0c72dSLiam Girdwood { 745ee1e79b7SRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 74653e0c72dSLiam Girdwood struct sof_ipc_stream stream; 74753e0c72dSLiam Girdwood int err; 74853e0c72dSLiam Girdwood 74953e0c72dSLiam Girdwood /* read position via slower IPC */ 75053e0c72dSLiam Girdwood stream.hdr.size = sizeof(stream); 75153e0c72dSLiam Girdwood stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_POSITION; 75253e0c72dSLiam Girdwood stream.comp_id = spcm->stream[direction].comp_id; 75353e0c72dSLiam Girdwood 75453e0c72dSLiam Girdwood /* send IPC to the DSP */ 7552a51c0f8SPeter Ujfalusi err = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), posn, 75653e0c72dSLiam Girdwood sizeof(*posn)); 75753e0c72dSLiam Girdwood if (err < 0) { 75853e0c72dSLiam Girdwood dev_err(sdev->dev, "error: failed to get stream %d position\n", 75953e0c72dSLiam Girdwood stream.comp_id); 76053e0c72dSLiam Girdwood return err; 76153e0c72dSLiam Girdwood } 76253e0c72dSLiam Girdwood 76353e0c72dSLiam Girdwood return 0; 76453e0c72dSLiam Girdwood } 76553e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_stream_posn); 76653e0c72dSLiam Girdwood 76753e0c72dSLiam Girdwood static int sof_get_ctrl_copy_params(enum sof_ipc_ctrl_type ctrl_type, 76853e0c72dSLiam Girdwood struct sof_ipc_ctrl_data *src, 76953e0c72dSLiam Girdwood struct sof_ipc_ctrl_data *dst, 77053e0c72dSLiam Girdwood struct sof_ipc_ctrl_data_params *sparams) 77153e0c72dSLiam Girdwood { 77253e0c72dSLiam Girdwood switch (ctrl_type) { 77353e0c72dSLiam Girdwood case SOF_CTRL_TYPE_VALUE_CHAN_GET: 77453e0c72dSLiam Girdwood case SOF_CTRL_TYPE_VALUE_CHAN_SET: 77553e0c72dSLiam Girdwood sparams->src = (u8 *)src->chanv; 77653e0c72dSLiam Girdwood sparams->dst = (u8 *)dst->chanv; 77753e0c72dSLiam Girdwood break; 77853e0c72dSLiam Girdwood case SOF_CTRL_TYPE_DATA_GET: 77953e0c72dSLiam Girdwood case SOF_CTRL_TYPE_DATA_SET: 78053e0c72dSLiam Girdwood sparams->src = (u8 *)src->data->data; 78153e0c72dSLiam Girdwood sparams->dst = (u8 *)dst->data->data; 78253e0c72dSLiam Girdwood break; 78353e0c72dSLiam Girdwood default: 78453e0c72dSLiam Girdwood return -EINVAL; 78553e0c72dSLiam Girdwood } 78653e0c72dSLiam Girdwood 78753e0c72dSLiam Girdwood /* calculate payload size and number of messages */ 78853e0c72dSLiam Girdwood sparams->pl_size = SOF_IPC_MSG_MAX_SIZE - sparams->hdr_bytes; 78953e0c72dSLiam Girdwood sparams->num_msg = DIV_ROUND_UP(sparams->msg_bytes, sparams->pl_size); 79053e0c72dSLiam Girdwood 79153e0c72dSLiam Girdwood return 0; 79253e0c72dSLiam Girdwood } 79353e0c72dSLiam Girdwood 79453e0c72dSLiam Girdwood static int sof_set_get_large_ctrl_data(struct snd_sof_dev *sdev, 79553e0c72dSLiam Girdwood struct sof_ipc_ctrl_data *cdata, 79653e0c72dSLiam Girdwood struct sof_ipc_ctrl_data_params *sparams, 7979d562fdcSPeter Ujfalusi bool set) 79853e0c72dSLiam Girdwood { 79953e0c72dSLiam Girdwood struct sof_ipc_ctrl_data *partdata; 80053e0c72dSLiam Girdwood size_t send_bytes; 80153e0c72dSLiam Girdwood size_t offset = 0; 80253e0c72dSLiam Girdwood size_t msg_bytes; 80353e0c72dSLiam Girdwood size_t pl_size; 80454d198d5STakashi Iwai int err; 80553e0c72dSLiam Girdwood int i; 80653e0c72dSLiam Girdwood 80753e0c72dSLiam Girdwood /* allocate max ipc size because we have at least one */ 80853e0c72dSLiam Girdwood partdata = kzalloc(SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL); 80953e0c72dSLiam Girdwood if (!partdata) 81053e0c72dSLiam Girdwood return -ENOMEM; 81153e0c72dSLiam Girdwood 8129d562fdcSPeter Ujfalusi if (set) 81354d198d5STakashi Iwai err = sof_get_ctrl_copy_params(cdata->type, cdata, partdata, 81454d198d5STakashi Iwai sparams); 81553e0c72dSLiam Girdwood else 81654d198d5STakashi Iwai err = sof_get_ctrl_copy_params(cdata->type, partdata, cdata, 81754d198d5STakashi Iwai sparams); 81845c13803SNavid Emamdoost if (err < 0) { 81945c13803SNavid Emamdoost kfree(partdata); 82054d198d5STakashi Iwai return err; 82145c13803SNavid Emamdoost } 82253e0c72dSLiam Girdwood 82353e0c72dSLiam Girdwood msg_bytes = sparams->msg_bytes; 82453e0c72dSLiam Girdwood pl_size = sparams->pl_size; 82553e0c72dSLiam Girdwood 82653e0c72dSLiam Girdwood /* copy the header data */ 82753e0c72dSLiam Girdwood memcpy(partdata, cdata, sparams->hdr_bytes); 82853e0c72dSLiam Girdwood 82953e0c72dSLiam Girdwood /* Serialise IPC TX */ 83053e0c72dSLiam Girdwood mutex_lock(&sdev->ipc->tx_mutex); 83153e0c72dSLiam Girdwood 83253e0c72dSLiam Girdwood /* copy the payload data in a loop */ 83353e0c72dSLiam Girdwood for (i = 0; i < sparams->num_msg; i++) { 83453e0c72dSLiam Girdwood send_bytes = min(msg_bytes, pl_size); 83553e0c72dSLiam Girdwood partdata->num_elems = send_bytes; 83653e0c72dSLiam Girdwood partdata->rhdr.hdr.size = sparams->hdr_bytes + send_bytes; 83753e0c72dSLiam Girdwood partdata->msg_index = i; 83853e0c72dSLiam Girdwood msg_bytes -= send_bytes; 83953e0c72dSLiam Girdwood partdata->elems_remaining = msg_bytes; 84053e0c72dSLiam Girdwood 8419d562fdcSPeter Ujfalusi if (set) 84253e0c72dSLiam Girdwood memcpy(sparams->dst, sparams->src + offset, send_bytes); 84353e0c72dSLiam Girdwood 84453e0c72dSLiam Girdwood err = sof_ipc_tx_message_unlocked(sdev->ipc, 84553e0c72dSLiam Girdwood partdata, 84653e0c72dSLiam Girdwood partdata->rhdr.hdr.size, 84753e0c72dSLiam Girdwood partdata, 84853e0c72dSLiam Girdwood partdata->rhdr.hdr.size); 84953e0c72dSLiam Girdwood if (err < 0) 85053e0c72dSLiam Girdwood break; 85153e0c72dSLiam Girdwood 8529d562fdcSPeter Ujfalusi if (!set) 85353e0c72dSLiam Girdwood memcpy(sparams->dst + offset, sparams->src, send_bytes); 85453e0c72dSLiam Girdwood 85553e0c72dSLiam Girdwood offset += pl_size; 85653e0c72dSLiam Girdwood } 85753e0c72dSLiam Girdwood 85853e0c72dSLiam Girdwood mutex_unlock(&sdev->ipc->tx_mutex); 85953e0c72dSLiam Girdwood 86053e0c72dSLiam Girdwood kfree(partdata); 86153e0c72dSLiam Girdwood return err; 86253e0c72dSLiam Girdwood } 86353e0c72dSLiam Girdwood 86453e0c72dSLiam Girdwood /* 86553e0c72dSLiam Girdwood * IPC get()/set() for kcontrols. 86653e0c72dSLiam Girdwood */ 86747d7328fSPeter Ujfalusi int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol, bool set) 86853e0c72dSLiam Girdwood { 869ee1e79b7SRanjani Sridharan struct snd_soc_component *scomp = scontrol->scomp; 870b5cee8feSRanjani Sridharan struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data; 871ee1e79b7SRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 87253e0c72dSLiam Girdwood struct sof_ipc_fw_ready *ready = &sdev->fw_ready; 87353e0c72dSLiam Girdwood struct sof_ipc_fw_version *v = &ready->version; 87453e0c72dSLiam Girdwood struct sof_ipc_ctrl_data_params sparams; 87547d7328fSPeter Ujfalusi enum sof_ipc_ctrl_type ctrl_type; 8768b001416SRanjani Sridharan struct snd_sof_widget *swidget; 8778b001416SRanjani Sridharan bool widget_found = false; 878d4a06c43SPeter Ujfalusi u32 ipc_cmd; 87953e0c72dSLiam Girdwood int err; 88053e0c72dSLiam Girdwood 8818b001416SRanjani Sridharan list_for_each_entry(swidget, &sdev->widget_list, list) { 8828b001416SRanjani Sridharan if (swidget->comp_id == scontrol->comp_id) { 8838b001416SRanjani Sridharan widget_found = true; 8848b001416SRanjani Sridharan break; 8858b001416SRanjani Sridharan } 8868b001416SRanjani Sridharan } 8878b001416SRanjani Sridharan 8888b001416SRanjani Sridharan if (!widget_found) { 8898b001416SRanjani Sridharan dev_err(sdev->dev, "error: can't find widget with id %d\n", scontrol->comp_id); 8908b001416SRanjani Sridharan return -EINVAL; 8918b001416SRanjani Sridharan } 8928b001416SRanjani Sridharan 8938b001416SRanjani Sridharan /* 8948b001416SRanjani Sridharan * Volatile controls should always be part of static pipelines and the widget use_count 8958b001416SRanjani Sridharan * would always be > 0 in this case. For the others, just return the cached value if the 8968b001416SRanjani Sridharan * widget is not set up. 8978b001416SRanjani Sridharan */ 8988b001416SRanjani Sridharan if (!swidget->use_count) 8998b001416SRanjani Sridharan return 0; 9008b001416SRanjani Sridharan 90147d7328fSPeter Ujfalusi /* 90247d7328fSPeter Ujfalusi * Select the IPC cmd and the ctrl_type based on the ctrl_cmd and the 90347d7328fSPeter Ujfalusi * direction 90447d7328fSPeter Ujfalusi * Note: SOF_CTRL_TYPE_VALUE_COMP_* is not used and supported currently 90547d7328fSPeter Ujfalusi * for ctrl_type 90647d7328fSPeter Ujfalusi */ 90747d7328fSPeter Ujfalusi if (cdata->cmd == SOF_CTRL_CMD_BINARY) { 908d4a06c43SPeter Ujfalusi ipc_cmd = set ? SOF_IPC_COMP_SET_DATA : SOF_IPC_COMP_GET_DATA; 90947d7328fSPeter Ujfalusi ctrl_type = set ? SOF_CTRL_TYPE_DATA_SET : SOF_CTRL_TYPE_DATA_GET; 91047d7328fSPeter Ujfalusi } else { 911d4a06c43SPeter Ujfalusi ipc_cmd = set ? SOF_IPC_COMP_SET_VALUE : SOF_IPC_COMP_GET_VALUE; 91247d7328fSPeter Ujfalusi ctrl_type = set ? SOF_CTRL_TYPE_VALUE_CHAN_SET : SOF_CTRL_TYPE_VALUE_CHAN_GET; 91347d7328fSPeter Ujfalusi } 914d4a06c43SPeter Ujfalusi 91553e0c72dSLiam Girdwood cdata->rhdr.hdr.cmd = SOF_IPC_GLB_COMP_MSG | ipc_cmd; 91653e0c72dSLiam Girdwood cdata->type = ctrl_type; 91753e0c72dSLiam Girdwood cdata->comp_id = scontrol->comp_id; 91853e0c72dSLiam Girdwood cdata->msg_index = 0; 91953e0c72dSLiam Girdwood 92053e0c72dSLiam Girdwood /* calculate header and data size */ 92153e0c72dSLiam Girdwood switch (cdata->type) { 92253e0c72dSLiam Girdwood case SOF_CTRL_TYPE_VALUE_CHAN_GET: 92353e0c72dSLiam Girdwood case SOF_CTRL_TYPE_VALUE_CHAN_SET: 92453e0c72dSLiam Girdwood sparams.msg_bytes = scontrol->num_channels * 92553e0c72dSLiam Girdwood sizeof(struct sof_ipc_ctrl_value_chan); 92653e0c72dSLiam Girdwood sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data); 92753e0c72dSLiam Girdwood sparams.elems = scontrol->num_channels; 92853e0c72dSLiam Girdwood break; 92953e0c72dSLiam Girdwood case SOF_CTRL_TYPE_DATA_GET: 93053e0c72dSLiam Girdwood case SOF_CTRL_TYPE_DATA_SET: 93153e0c72dSLiam Girdwood sparams.msg_bytes = cdata->data->size; 93253e0c72dSLiam Girdwood sparams.hdr_bytes = sizeof(struct sof_ipc_ctrl_data) + 93353e0c72dSLiam Girdwood sizeof(struct sof_abi_hdr); 93453e0c72dSLiam Girdwood sparams.elems = cdata->data->size; 93553e0c72dSLiam Girdwood break; 93653e0c72dSLiam Girdwood default: 93753e0c72dSLiam Girdwood return -EINVAL; 93853e0c72dSLiam Girdwood } 93953e0c72dSLiam Girdwood 94053e0c72dSLiam Girdwood cdata->rhdr.hdr.size = sparams.msg_bytes + sparams.hdr_bytes; 94153e0c72dSLiam Girdwood cdata->num_elems = sparams.elems; 94253e0c72dSLiam Girdwood cdata->elems_remaining = 0; 94353e0c72dSLiam Girdwood 94453e0c72dSLiam Girdwood /* send normal size ipc in one part */ 94553e0c72dSLiam Girdwood if (cdata->rhdr.hdr.size <= SOF_IPC_MSG_MAX_SIZE) { 9462a51c0f8SPeter Ujfalusi err = sof_ipc_tx_message(sdev->ipc, cdata, cdata->rhdr.hdr.size, 9472a51c0f8SPeter Ujfalusi cdata, cdata->rhdr.hdr.size); 94853e0c72dSLiam Girdwood 94953e0c72dSLiam Girdwood if (err < 0) 95053e0c72dSLiam Girdwood dev_err(sdev->dev, "error: set/get ctrl ipc comp %d\n", 95153e0c72dSLiam Girdwood cdata->comp_id); 95253e0c72dSLiam Girdwood 95353e0c72dSLiam Girdwood return err; 95453e0c72dSLiam Girdwood } 95553e0c72dSLiam Girdwood 95653e0c72dSLiam Girdwood /* data is bigger than max ipc size, chop into smaller pieces */ 95753e0c72dSLiam Girdwood dev_dbg(sdev->dev, "large ipc size %u, control size %u\n", 95853e0c72dSLiam Girdwood cdata->rhdr.hdr.size, scontrol->size); 95953e0c72dSLiam Girdwood 96053e0c72dSLiam Girdwood /* large messages is only supported from ABI 3.3.0 onwards */ 96153e0c72dSLiam Girdwood if (v->abi_version < SOF_ABI_VER(3, 3, 0)) { 96253e0c72dSLiam Girdwood dev_err(sdev->dev, "error: incompatible FW ABI version\n"); 96353e0c72dSLiam Girdwood return -EINVAL; 96453e0c72dSLiam Girdwood } 96553e0c72dSLiam Girdwood 9669d562fdcSPeter Ujfalusi err = sof_set_get_large_ctrl_data(sdev, cdata, &sparams, set); 96753e0c72dSLiam Girdwood 96853e0c72dSLiam Girdwood if (err < 0) 96953e0c72dSLiam Girdwood dev_err(sdev->dev, "error: set/get large ctrl ipc comp %d\n", 97053e0c72dSLiam Girdwood cdata->comp_id); 97153e0c72dSLiam Girdwood 97253e0c72dSLiam Girdwood return err; 97353e0c72dSLiam Girdwood } 97453e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_set_get_comp_data); 97553e0c72dSLiam Girdwood 97653e0c72dSLiam Girdwood int snd_sof_ipc_valid(struct snd_sof_dev *sdev) 97753e0c72dSLiam Girdwood { 97853e0c72dSLiam Girdwood struct sof_ipc_fw_ready *ready = &sdev->fw_ready; 97953e0c72dSLiam Girdwood struct sof_ipc_fw_version *v = &ready->version; 98053e0c72dSLiam Girdwood 98153e0c72dSLiam Girdwood dev_info(sdev->dev, 98253e0c72dSLiam Girdwood "Firmware info: version %d:%d:%d-%s\n", v->major, v->minor, 98353e0c72dSLiam Girdwood v->micro, v->tag); 98453e0c72dSLiam Girdwood dev_info(sdev->dev, 98553e0c72dSLiam Girdwood "Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n", 98653e0c72dSLiam Girdwood SOF_ABI_VERSION_MAJOR(v->abi_version), 98753e0c72dSLiam Girdwood SOF_ABI_VERSION_MINOR(v->abi_version), 98853e0c72dSLiam Girdwood SOF_ABI_VERSION_PATCH(v->abi_version), 98953e0c72dSLiam Girdwood SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH); 99053e0c72dSLiam Girdwood 99153e0c72dSLiam Girdwood if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) { 99253e0c72dSLiam Girdwood dev_err(sdev->dev, "error: incompatible FW ABI version\n"); 99353e0c72dSLiam Girdwood return -EINVAL; 99453e0c72dSLiam Girdwood } 99553e0c72dSLiam Girdwood 99653129e66SKai Vehmanen if (SOF_ABI_VERSION_MINOR(v->abi_version) > SOF_ABI_MINOR) { 9974acb1c2eSPierre-Louis Bossart if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) { 9984acb1c2eSPierre-Louis Bossart dev_warn(sdev->dev, "warn: FW ABI is more recent than kernel\n"); 9994acb1c2eSPierre-Louis Bossart } else { 10004acb1c2eSPierre-Louis Bossart dev_err(sdev->dev, "error: FW ABI is more recent than kernel\n"); 10014acb1c2eSPierre-Louis Bossart return -EINVAL; 10024acb1c2eSPierre-Louis Bossart } 10034acb1c2eSPierre-Louis Bossart } 10044acb1c2eSPierre-Louis Bossart 1005347d1c4bSSlawomir Blauciak if (ready->flags & SOF_IPC_INFO_BUILD) { 100653e0c72dSLiam Girdwood dev_info(sdev->dev, 100753e0c72dSLiam Girdwood "Firmware debug build %d on %s-%s - options:\n" 100853e0c72dSLiam Girdwood " GDB: %s\n" 100953e0c72dSLiam Girdwood " lock debug: %s\n" 101053e0c72dSLiam Girdwood " lock vdebug: %s\n", 101153e0c72dSLiam Girdwood v->build, v->date, v->time, 1012847a040dSPierre-Louis Bossart (ready->flags & SOF_IPC_INFO_GDB) ? 1013347d1c4bSSlawomir Blauciak "enabled" : "disabled", 1014847a040dSPierre-Louis Bossart (ready->flags & SOF_IPC_INFO_LOCKS) ? 1015347d1c4bSSlawomir Blauciak "enabled" : "disabled", 1016847a040dSPierre-Louis Bossart (ready->flags & SOF_IPC_INFO_LOCKSV) ? 1017347d1c4bSSlawomir Blauciak "enabled" : "disabled"); 101853e0c72dSLiam Girdwood } 101953e0c72dSLiam Girdwood 102053e0c72dSLiam Girdwood /* copy the fw_version into debugfs at first boot */ 102153e0c72dSLiam Girdwood memcpy(&sdev->fw_version, v, sizeof(*v)); 102253e0c72dSLiam Girdwood 102353e0c72dSLiam Girdwood return 0; 102453e0c72dSLiam Girdwood } 102553e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_valid); 102653e0c72dSLiam Girdwood 1027febf5da8SRander Wang int sof_ipc_init_msg_memory(struct snd_sof_dev *sdev) 1028febf5da8SRander Wang { 1029febf5da8SRander Wang struct snd_sof_ipc_msg *msg; 1030febf5da8SRander Wang 1031febf5da8SRander Wang msg = &sdev->ipc->msg; 1032febf5da8SRander Wang 1033febf5da8SRander Wang msg->reply_data = devm_kzalloc(sdev->dev, SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL); 1034febf5da8SRander Wang if (!msg->reply_data) 1035febf5da8SRander Wang return -ENOMEM; 1036febf5da8SRander Wang 1037a3e6b369SPeter Ujfalusi sdev->ipc->max_payload_size = SOF_IPC_MSG_MAX_SIZE; 1038a3e6b369SPeter Ujfalusi 1039febf5da8SRander Wang return 0; 1040febf5da8SRander Wang } 1041febf5da8SRander Wang 104253e0c72dSLiam Girdwood struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev) 104353e0c72dSLiam Girdwood { 104453e0c72dSLiam Girdwood struct snd_sof_ipc *ipc; 104553e0c72dSLiam Girdwood struct snd_sof_ipc_msg *msg; 1046785b3fbeSPeter Ujfalusi const struct sof_ipc_ops *ops; 104753e0c72dSLiam Girdwood 104853e0c72dSLiam Girdwood ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL); 104953e0c72dSLiam Girdwood if (!ipc) 105053e0c72dSLiam Girdwood return NULL; 105153e0c72dSLiam Girdwood 105253e0c72dSLiam Girdwood mutex_init(&ipc->tx_mutex); 105353e0c72dSLiam Girdwood ipc->sdev = sdev; 105453e0c72dSLiam Girdwood msg = &ipc->msg; 105553e0c72dSLiam Girdwood 105653e0c72dSLiam Girdwood /* indicate that we aren't sending a message ATM */ 105753e0c72dSLiam Girdwood msg->ipc_complete = true; 105853e0c72dSLiam Girdwood 105953e0c72dSLiam Girdwood init_waitqueue_head(&msg->waitq); 106053e0c72dSLiam Girdwood 10617006d20eSRanjani Sridharan /* 10627006d20eSRanjani Sridharan * Use IPC3 ops as it is the only available version now. With the addition of new IPC 10637006d20eSRanjani Sridharan * versions, this will need to be modified to use the selected version at runtime. 10647006d20eSRanjani Sridharan */ 10657006d20eSRanjani Sridharan ipc->ops = &ipc3_ops; 1066785b3fbeSPeter Ujfalusi ops = ipc->ops; 10677006d20eSRanjani Sridharan 10687006d20eSRanjani Sridharan /* check for mandatory ops */ 1069*defad9d2SPeter Ujfalusi if (!ops->tx_msg || !ops->rx_msg || !ops->set_get_data || !ops->get_reply) { 1070*defad9d2SPeter Ujfalusi dev_err(sdev->dev, "Missing IPC message handling ops\n"); 1071*defad9d2SPeter Ujfalusi return NULL; 1072*defad9d2SPeter Ujfalusi } 1073*defad9d2SPeter Ujfalusi 1074785b3fbeSPeter Ujfalusi if (!ops->pcm) { 1075785b3fbeSPeter Ujfalusi dev_err(sdev->dev, "Missing IPC PCM ops\n"); 1076785b3fbeSPeter Ujfalusi return NULL; 1077785b3fbeSPeter Ujfalusi } 1078785b3fbeSPeter Ujfalusi 1079785b3fbeSPeter Ujfalusi if (!ops->tplg || !ops->tplg->widget || !ops->tplg->control) { 1080785b3fbeSPeter Ujfalusi dev_err(sdev->dev, "Missing IPC topology ops\n"); 10817006d20eSRanjani Sridharan return NULL; 10827006d20eSRanjani Sridharan } 10837006d20eSRanjani Sridharan 108453e0c72dSLiam Girdwood return ipc; 108553e0c72dSLiam Girdwood } 108653e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_init); 108753e0c72dSLiam Girdwood 108853e0c72dSLiam Girdwood void snd_sof_ipc_free(struct snd_sof_dev *sdev) 108953e0c72dSLiam Girdwood { 109053e0c72dSLiam Girdwood struct snd_sof_ipc *ipc = sdev->ipc; 109153e0c72dSLiam Girdwood 1092b06e4642SKai Vehmanen if (!ipc) 1093b06e4642SKai Vehmanen return; 1094b06e4642SKai Vehmanen 109553e0c72dSLiam Girdwood /* disable sending of ipc's */ 109653e0c72dSLiam Girdwood mutex_lock(&ipc->tx_mutex); 109753e0c72dSLiam Girdwood ipc->disable_ipc_tx = true; 109853e0c72dSLiam Girdwood mutex_unlock(&ipc->tx_mutex); 109953e0c72dSLiam Girdwood } 110053e0c72dSLiam Girdwood EXPORT_SYMBOL(snd_sof_ipc_free); 1101