1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2018 Intel Corporation 7 // 8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> 9 // 10 // Generic IPC layer that can work over MMIO and SPI/I2C. PHY layer provided 11 // by platform driver code. 12 // 13 14 #include <linux/mutex.h> 15 #include <linux/types.h> 16 17 #include "sof-priv.h" 18 #include "sof-audio.h" 19 #include "ops.h" 20 21 /** 22 * sof_ipc_send_msg - generic function to prepare and send one IPC message 23 * @sdev: pointer to SOF core device struct 24 * @msg_data: pointer to a message to send 25 * @msg_bytes: number of bytes in the message 26 * @reply_bytes: number of bytes available for the reply. 27 * The buffer for the reply data is not passed to this 28 * function, the available size is an information for the 29 * reply handling functions. 30 * 31 * On success the function returns 0, otherwise negative error number. 32 * 33 * Note: higher level sdev->ipc->tx_mutex must be held to make sure that 34 * transfers are synchronized. 35 */ 36 int sof_ipc_send_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_bytes, 37 size_t reply_bytes) 38 { 39 struct snd_sof_ipc *ipc = sdev->ipc; 40 struct snd_sof_ipc_msg *msg; 41 int ret; 42 43 if (ipc->disable_ipc_tx || sdev->fw_state != SOF_FW_BOOT_COMPLETE) 44 return -ENODEV; 45 46 /* 47 * The spin-lock is needed to protect message objects against other 48 * atomic contexts. 49 */ 50 guard(spinlock_irq)(&sdev->ipc_lock); 51 52 /* initialise the message */ 53 msg = &ipc->msg; 54 55 /* attach message data */ 56 msg->msg_data = msg_data; 57 msg->msg_size = msg_bytes; 58 59 msg->reply_size = reply_bytes; 60 msg->reply_error = 0; 61 62 sdev->msg = msg; 63 64 ret = snd_sof_dsp_send_msg(sdev, msg); 65 /* Next reply that we receive will be related to this message */ 66 if (!ret) 67 msg->ipc_complete = false; 68 69 return ret; 70 } 71 72 /* send IPC message from host to DSP */ 73 int sof_ipc_tx_message(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes, 74 void *reply_data, size_t reply_bytes) 75 { 76 if (msg_bytes > ipc->max_payload_size || 77 reply_bytes > ipc->max_payload_size) 78 return -ENOBUFS; 79 80 return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data, 81 reply_bytes, false); 82 } 83 EXPORT_SYMBOL(sof_ipc_tx_message); 84 85 /* IPC set or get data from host to DSP */ 86 int sof_ipc_set_get_data(struct snd_sof_ipc *ipc, void *msg_data, 87 size_t msg_bytes, bool set) 88 { 89 return ipc->ops->set_get_data(ipc->sdev, msg_data, msg_bytes, set); 90 } 91 EXPORT_SYMBOL(sof_ipc_set_get_data); 92 93 /* 94 * send IPC message from host to DSP without modifying the DSP state. 95 * This will be used for IPC's that can be handled by the DSP 96 * even in a low-power D0 substate. 97 */ 98 int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes, 99 void *reply_data, size_t reply_bytes) 100 { 101 if (msg_bytes > ipc->max_payload_size || 102 reply_bytes > ipc->max_payload_size) 103 return -ENOBUFS; 104 105 return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data, 106 reply_bytes, true); 107 } 108 EXPORT_SYMBOL(sof_ipc_tx_message_no_pm); 109 110 /* Generic helper function to retrieve the reply */ 111 void snd_sof_ipc_get_reply(struct snd_sof_dev *sdev) 112 { 113 /* 114 * Sometimes, there is unexpected reply ipc arriving. The reply 115 * ipc belongs to none of the ipcs sent from driver. 116 * In this case, the driver must ignore the ipc. 117 */ 118 if (!sdev->msg) { 119 dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n"); 120 return; 121 } 122 123 sdev->msg->reply_error = sdev->ipc->ops->get_reply(sdev); 124 } 125 EXPORT_SYMBOL(snd_sof_ipc_get_reply); 126 127 /* handle reply message from DSP */ 128 void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id) 129 { 130 struct snd_sof_ipc_msg *msg = &sdev->ipc->msg; 131 132 if (msg->ipc_complete) { 133 dev_dbg(sdev->dev, 134 "no reply expected, received 0x%x, will be ignored", 135 msg_id); 136 return; 137 } 138 139 /* wake up and return the error if we have waiters on this message ? */ 140 msg->ipc_complete = true; 141 wake_up(&msg->waitq); 142 } 143 EXPORT_SYMBOL(snd_sof_ipc_reply); 144 145 struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev) 146 { 147 struct snd_sof_ipc *ipc; 148 struct snd_sof_ipc_msg *msg; 149 const struct sof_ipc_ops *ops; 150 151 ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL); 152 if (!ipc) 153 return NULL; 154 155 mutex_init(&ipc->tx_mutex); 156 ipc->sdev = sdev; 157 msg = &ipc->msg; 158 159 /* indicate that we aren't sending a message ATM */ 160 msg->ipc_complete = true; 161 162 init_waitqueue_head(&msg->waitq); 163 164 switch (sdev->pdata->ipc_type) { 165 #if defined(CONFIG_SND_SOC_SOF_IPC3) 166 case SOF_IPC_TYPE_3: 167 ops = &ipc3_ops; 168 break; 169 #endif 170 #if defined(CONFIG_SND_SOC_SOF_IPC4) 171 case SOF_IPC_TYPE_4: 172 ops = &ipc4_ops; 173 break; 174 #endif 175 default: 176 dev_err(sdev->dev, "Not supported IPC version: %d\n", 177 sdev->pdata->ipc_type); 178 return NULL; 179 } 180 181 /* check for mandatory ops */ 182 if (!ops->tx_msg || !ops->rx_msg || !ops->set_get_data || !ops->get_reply) { 183 dev_err(sdev->dev, "Missing IPC message handling ops\n"); 184 return NULL; 185 } 186 187 if (!ops->fw_loader || !ops->fw_loader->validate || 188 !ops->fw_loader->parse_ext_manifest) { 189 dev_err(sdev->dev, "Missing IPC firmware loading ops\n"); 190 return NULL; 191 } 192 193 if (!ops->pcm) { 194 dev_err(sdev->dev, "Missing IPC PCM ops\n"); 195 return NULL; 196 } 197 198 if (!ops->tplg || !ops->tplg->widget || !ops->tplg->control) { 199 dev_err(sdev->dev, "Missing IPC topology ops\n"); 200 return NULL; 201 } 202 203 if (ops->fw_tracing && (!ops->fw_tracing->init || !ops->fw_tracing->suspend || 204 !ops->fw_tracing->resume)) { 205 dev_err(sdev->dev, "Missing firmware tracing ops\n"); 206 return NULL; 207 } 208 209 if (ops->init && ops->init(sdev)) 210 return NULL; 211 212 ipc->ops = ops; 213 214 return ipc; 215 } 216 EXPORT_SYMBOL(snd_sof_ipc_init); 217 218 void snd_sof_ipc_free(struct snd_sof_dev *sdev) 219 { 220 struct snd_sof_ipc *ipc = sdev->ipc; 221 222 if (!ipc) 223 return; 224 225 /* disable sending of ipc's */ 226 scoped_guard(mutex, &ipc->tx_mutex) 227 ipc->disable_ipc_tx = true; 228 229 if (ipc->ops->exit) 230 ipc->ops->exit(sdev); 231 } 232 EXPORT_SYMBOL(snd_sof_ipc_free); 233