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. All rights reserved.
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  */
sof_ipc_send_msg(struct snd_sof_dev * sdev,void * msg_data,size_t msg_bytes,size_t reply_bytes)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 	spin_lock_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 	spin_unlock_irq(&sdev->ipc_lock);
70 
71 	return ret;
72 }
73 
74 /* send IPC message from host to DSP */
sof_ipc_tx_message(struct snd_sof_ipc * ipc,void * msg_data,size_t msg_bytes,void * reply_data,size_t reply_bytes)75 int sof_ipc_tx_message(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
76 		       void *reply_data, size_t reply_bytes)
77 {
78 	if (msg_bytes > ipc->max_payload_size ||
79 	    reply_bytes > ipc->max_payload_size)
80 		return -ENOBUFS;
81 
82 	return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data,
83 				reply_bytes, false);
84 }
85 EXPORT_SYMBOL(sof_ipc_tx_message);
86 
87 /*
88  * send IPC message from host to DSP without modifying the DSP state.
89  * This will be used for IPC's that can be handled by the DSP
90  * even in a low-power D0 substate.
91  */
sof_ipc_tx_message_no_pm(struct snd_sof_ipc * ipc,void * msg_data,size_t msg_bytes,void * reply_data,size_t reply_bytes)92 int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
93 			     void *reply_data, size_t reply_bytes)
94 {
95 	if (msg_bytes > ipc->max_payload_size ||
96 	    reply_bytes > ipc->max_payload_size)
97 		return -ENOBUFS;
98 
99 	return ipc->ops->tx_msg(ipc->sdev, msg_data, msg_bytes, reply_data,
100 				reply_bytes, true);
101 }
102 EXPORT_SYMBOL(sof_ipc_tx_message_no_pm);
103 
104 /* Generic helper function to retrieve the reply */
snd_sof_ipc_get_reply(struct snd_sof_dev * sdev)105 void snd_sof_ipc_get_reply(struct snd_sof_dev *sdev)
106 {
107 	/*
108 	 * Sometimes, there is unexpected reply ipc arriving. The reply
109 	 * ipc belongs to none of the ipcs sent from driver.
110 	 * In this case, the driver must ignore the ipc.
111 	 */
112 	if (!sdev->msg) {
113 		dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
114 		return;
115 	}
116 
117 	sdev->msg->reply_error = sdev->ipc->ops->get_reply(sdev);
118 }
119 EXPORT_SYMBOL(snd_sof_ipc_get_reply);
120 
121 /* handle reply message from DSP */
snd_sof_ipc_reply(struct snd_sof_dev * sdev,u32 msg_id)122 void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id)
123 {
124 	struct snd_sof_ipc_msg *msg = &sdev->ipc->msg;
125 
126 	if (msg->ipc_complete) {
127 		dev_dbg(sdev->dev,
128 			"no reply expected, received 0x%x, will be ignored",
129 			msg_id);
130 		return;
131 	}
132 
133 	/* wake up and return the error if we have waiters on this message ? */
134 	msg->ipc_complete = true;
135 	wake_up(&msg->waitq);
136 }
137 EXPORT_SYMBOL(snd_sof_ipc_reply);
138 
snd_sof_ipc_init(struct snd_sof_dev * sdev)139 struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev)
140 {
141 	struct snd_sof_ipc *ipc;
142 	struct snd_sof_ipc_msg *msg;
143 	const struct sof_ipc_ops *ops;
144 
145 	ipc = devm_kzalloc(sdev->dev, sizeof(*ipc), GFP_KERNEL);
146 	if (!ipc)
147 		return NULL;
148 
149 	mutex_init(&ipc->tx_mutex);
150 	ipc->sdev = sdev;
151 	msg = &ipc->msg;
152 
153 	/* indicate that we aren't sending a message ATM */
154 	msg->ipc_complete = true;
155 
156 	init_waitqueue_head(&msg->waitq);
157 
158 	switch (sdev->pdata->ipc_type) {
159 #if defined(CONFIG_SND_SOC_SOF_IPC3)
160 	case SOF_IPC:
161 		ops = &ipc3_ops;
162 		break;
163 #endif
164 #if defined(CONFIG_SND_SOC_SOF_INTEL_IPC4)
165 	case SOF_INTEL_IPC4:
166 		ops = &ipc4_ops;
167 		break;
168 #endif
169 	default:
170 		dev_err(sdev->dev, "Not supported IPC version: %d\n",
171 			sdev->pdata->ipc_type);
172 		return NULL;
173 	}
174 
175 	/* check for mandatory ops */
176 	if (!ops->tx_msg || !ops->rx_msg || !ops->set_get_data || !ops->get_reply) {
177 		dev_err(sdev->dev, "Missing IPC message handling ops\n");
178 		return NULL;
179 	}
180 
181 	if (!ops->fw_loader || !ops->fw_loader->validate ||
182 	    !ops->fw_loader->parse_ext_manifest) {
183 		dev_err(sdev->dev, "Missing IPC firmware loading ops\n");
184 		return NULL;
185 	}
186 
187 	if (!ops->pcm) {
188 		dev_err(sdev->dev, "Missing IPC PCM ops\n");
189 		return NULL;
190 	}
191 
192 	if (!ops->tplg || !ops->tplg->widget || !ops->tplg->control) {
193 		dev_err(sdev->dev, "Missing IPC topology ops\n");
194 		return NULL;
195 	}
196 
197 	if (ops->fw_tracing && (!ops->fw_tracing->init || !ops->fw_tracing->suspend ||
198 				!ops->fw_tracing->resume)) {
199 		dev_err(sdev->dev, "Missing firmware tracing ops\n");
200 		return NULL;
201 	}
202 
203 	ipc->ops = ops;
204 
205 	return ipc;
206 }
207 EXPORT_SYMBOL(snd_sof_ipc_init);
208 
snd_sof_ipc_free(struct snd_sof_dev * sdev)209 void snd_sof_ipc_free(struct snd_sof_dev *sdev)
210 {
211 	struct snd_sof_ipc *ipc = sdev->ipc;
212 
213 	if (!ipc)
214 		return;
215 
216 	/* disable sending of ipc's */
217 	mutex_lock(&ipc->tx_mutex);
218 	ipc->disable_ipc_tx = true;
219 	mutex_unlock(&ipc->tx_mutex);
220 }
221 EXPORT_SYMBOL(snd_sof_ipc_free);
222