xref: /openbmc/linux/sound/soc/intel/common/sst-ipc.c (revision 2169e6da)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel SST generic IPC Support
4  *
5  * Copyright (C) 2015, Intel Corporation. All rights reserved.
6  */
7 
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/list.h>
11 #include <linux/wait.h>
12 #include <linux/module.h>
13 #include <linux/spinlock.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/sched.h>
18 #include <linux/delay.h>
19 #include <linux/platform_device.h>
20 #include <sound/asound.h>
21 
22 #include "sst-dsp.h"
23 #include "sst-dsp-priv.h"
24 #include "sst-ipc.h"
25 
26 /* IPC message timeout (msecs) */
27 #define IPC_TIMEOUT_MSECS	300
28 
29 #define IPC_EMPTY_LIST_SIZE	8
30 
31 /* locks held by caller */
32 static struct ipc_message *msg_get_empty(struct sst_generic_ipc *ipc)
33 {
34 	struct ipc_message *msg = NULL;
35 
36 	if (!list_empty(&ipc->empty_list)) {
37 		msg = list_first_entry(&ipc->empty_list, struct ipc_message,
38 			list);
39 		list_del(&msg->list);
40 	}
41 
42 	return msg;
43 }
44 
45 static int tx_wait_done(struct sst_generic_ipc *ipc,
46 	struct ipc_message *msg, void *rx_data)
47 {
48 	unsigned long flags;
49 	int ret;
50 
51 	/* wait for DSP completion (in all cases atm inc pending) */
52 	ret = wait_event_timeout(msg->waitq, msg->complete,
53 		msecs_to_jiffies(IPC_TIMEOUT_MSECS));
54 
55 	spin_lock_irqsave(&ipc->dsp->spinlock, flags);
56 	if (ret == 0) {
57 		if (ipc->ops.shim_dbg != NULL)
58 			ipc->ops.shim_dbg(ipc, "message timeout");
59 
60 		list_del(&msg->list);
61 		ret = -ETIMEDOUT;
62 	} else {
63 
64 		/* copy the data returned from DSP */
65 		if (rx_data)
66 			memcpy(rx_data, msg->rx_data, msg->rx_size);
67 		ret = msg->errno;
68 	}
69 
70 	list_add_tail(&msg->list, &ipc->empty_list);
71 	spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
72 	return ret;
73 }
74 
75 static int ipc_tx_message(struct sst_generic_ipc *ipc, u64 header,
76 	void *tx_data, size_t tx_bytes, void *rx_data,
77 	size_t rx_bytes, int wait)
78 {
79 	struct ipc_message *msg;
80 	unsigned long flags;
81 
82 	spin_lock_irqsave(&ipc->dsp->spinlock, flags);
83 
84 	msg = msg_get_empty(ipc);
85 	if (msg == NULL) {
86 		spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
87 		return -EBUSY;
88 	}
89 
90 	msg->header = header;
91 	msg->tx_size = tx_bytes;
92 	msg->rx_size = rx_bytes;
93 	msg->wait = wait;
94 	msg->errno = 0;
95 	msg->pending = false;
96 	msg->complete = false;
97 
98 	if ((tx_bytes) && (ipc->ops.tx_data_copy != NULL))
99 		ipc->ops.tx_data_copy(msg, tx_data, tx_bytes);
100 
101 	list_add_tail(&msg->list, &ipc->tx_list);
102 	schedule_work(&ipc->kwork);
103 	spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
104 
105 	if (wait)
106 		return tx_wait_done(ipc, msg, rx_data);
107 	else
108 		return 0;
109 }
110 
111 static int msg_empty_list_init(struct sst_generic_ipc *ipc)
112 {
113 	int i;
114 
115 	ipc->msg = kcalloc(IPC_EMPTY_LIST_SIZE, sizeof(struct ipc_message),
116 			   GFP_KERNEL);
117 	if (ipc->msg == NULL)
118 		return -ENOMEM;
119 
120 	for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
121 		ipc->msg[i].tx_data = kzalloc(ipc->tx_data_max_size, GFP_KERNEL);
122 		if (ipc->msg[i].tx_data == NULL)
123 			goto free_mem;
124 
125 		ipc->msg[i].rx_data = kzalloc(ipc->rx_data_max_size, GFP_KERNEL);
126 		if (ipc->msg[i].rx_data == NULL) {
127 			kfree(ipc->msg[i].tx_data);
128 			goto free_mem;
129 		}
130 
131 		init_waitqueue_head(&ipc->msg[i].waitq);
132 		list_add(&ipc->msg[i].list, &ipc->empty_list);
133 	}
134 
135 	return 0;
136 
137 free_mem:
138 	while (i > 0) {
139 		kfree(ipc->msg[i-1].tx_data);
140 		kfree(ipc->msg[i-1].rx_data);
141 		--i;
142 	}
143 	kfree(ipc->msg);
144 
145 	return -ENOMEM;
146 }
147 
148 static void ipc_tx_msgs(struct work_struct *work)
149 {
150 	struct sst_generic_ipc *ipc =
151 		container_of(work, struct sst_generic_ipc, kwork);
152 	struct ipc_message *msg;
153 
154 	spin_lock_irq(&ipc->dsp->spinlock);
155 
156 	while (!list_empty(&ipc->tx_list) && !ipc->pending) {
157 		/* if the DSP is busy, we will TX messages after IRQ.
158 		 * also postpone if we are in the middle of processing
159 		 * completion irq
160 		 */
161 		if (ipc->ops.is_dsp_busy && ipc->ops.is_dsp_busy(ipc->dsp)) {
162 			dev_dbg(ipc->dev, "ipc_tx_msgs dsp busy\n");
163 			break;
164 		}
165 
166 		msg = list_first_entry(&ipc->tx_list, struct ipc_message, list);
167 		list_move(&msg->list, &ipc->rx_list);
168 
169 		if (ipc->ops.tx_msg != NULL)
170 			ipc->ops.tx_msg(ipc, msg);
171 	}
172 
173 	spin_unlock_irq(&ipc->dsp->spinlock);
174 }
175 
176 int sst_ipc_tx_message_wait(struct sst_generic_ipc *ipc, u64 header,
177 	void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes)
178 {
179 	int ret;
180 
181 	/*
182 	 * DSP maybe in lower power active state, so
183 	 * check if the DSP supports DSP lp On method
184 	 * if so invoke that before sending IPC
185 	 */
186 	if (ipc->ops.check_dsp_lp_on)
187 		if (ipc->ops.check_dsp_lp_on(ipc->dsp, true))
188 			return -EIO;
189 
190 	ret = ipc_tx_message(ipc, header, tx_data, tx_bytes,
191 		rx_data, rx_bytes, 1);
192 
193 	if (ipc->ops.check_dsp_lp_on)
194 		if (ipc->ops.check_dsp_lp_on(ipc->dsp, false))
195 			return -EIO;
196 
197 	return ret;
198 }
199 EXPORT_SYMBOL_GPL(sst_ipc_tx_message_wait);
200 
201 int sst_ipc_tx_message_nowait(struct sst_generic_ipc *ipc, u64 header,
202 	void *tx_data, size_t tx_bytes)
203 {
204 	return ipc_tx_message(ipc, header, tx_data, tx_bytes,
205 		NULL, 0, 0);
206 }
207 EXPORT_SYMBOL_GPL(sst_ipc_tx_message_nowait);
208 
209 int sst_ipc_tx_message_nopm(struct sst_generic_ipc *ipc, u64 header,
210 	void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes)
211 {
212 	return ipc_tx_message(ipc, header, tx_data, tx_bytes,
213 		rx_data, rx_bytes, 1);
214 }
215 EXPORT_SYMBOL_GPL(sst_ipc_tx_message_nopm);
216 
217 struct ipc_message *sst_ipc_reply_find_msg(struct sst_generic_ipc *ipc,
218 	u64 header)
219 {
220 	struct ipc_message *msg;
221 	u64 mask;
222 
223 	if (ipc->ops.reply_msg_match != NULL)
224 		header = ipc->ops.reply_msg_match(header, &mask);
225 
226 	if (list_empty(&ipc->rx_list)) {
227 		dev_err(ipc->dev, "error: rx list empty but received 0x%llx\n",
228 			header);
229 		return NULL;
230 	}
231 
232 	list_for_each_entry(msg, &ipc->rx_list, list) {
233 		if ((msg->header & mask) == header)
234 			return msg;
235 	}
236 
237 	return NULL;
238 }
239 EXPORT_SYMBOL_GPL(sst_ipc_reply_find_msg);
240 
241 /* locks held by caller */
242 void sst_ipc_tx_msg_reply_complete(struct sst_generic_ipc *ipc,
243 	struct ipc_message *msg)
244 {
245 	msg->complete = true;
246 
247 	if (!msg->wait)
248 		list_add_tail(&msg->list, &ipc->empty_list);
249 	else
250 		wake_up(&msg->waitq);
251 }
252 EXPORT_SYMBOL_GPL(sst_ipc_tx_msg_reply_complete);
253 
254 void sst_ipc_drop_all(struct sst_generic_ipc *ipc)
255 {
256 	struct ipc_message *msg, *tmp;
257 	unsigned long flags;
258 	int tx_drop_cnt = 0, rx_drop_cnt = 0;
259 
260 	/* drop all TX and Rx messages before we stall + reset DSP */
261 	spin_lock_irqsave(&ipc->dsp->spinlock, flags);
262 
263 	list_for_each_entry_safe(msg, tmp, &ipc->tx_list, list) {
264 		list_move(&msg->list, &ipc->empty_list);
265 		tx_drop_cnt++;
266 	}
267 
268 	list_for_each_entry_safe(msg, tmp, &ipc->rx_list, list) {
269 		list_move(&msg->list, &ipc->empty_list);
270 		rx_drop_cnt++;
271 	}
272 
273 	spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
274 
275 	if (tx_drop_cnt || rx_drop_cnt)
276 		dev_err(ipc->dev, "dropped IPC msg RX=%d, TX=%d\n",
277 			tx_drop_cnt, rx_drop_cnt);
278 }
279 EXPORT_SYMBOL_GPL(sst_ipc_drop_all);
280 
281 int sst_ipc_init(struct sst_generic_ipc *ipc)
282 {
283 	int ret;
284 
285 	INIT_LIST_HEAD(&ipc->tx_list);
286 	INIT_LIST_HEAD(&ipc->rx_list);
287 	INIT_LIST_HEAD(&ipc->empty_list);
288 	init_waitqueue_head(&ipc->wait_txq);
289 
290 	ret = msg_empty_list_init(ipc);
291 	if (ret < 0)
292 		return -ENOMEM;
293 
294 	INIT_WORK(&ipc->kwork, ipc_tx_msgs);
295 	return 0;
296 }
297 EXPORT_SYMBOL_GPL(sst_ipc_init);
298 
299 void sst_ipc_fini(struct sst_generic_ipc *ipc)
300 {
301 	int i;
302 
303 	cancel_work_sync(&ipc->kwork);
304 
305 	if (ipc->msg) {
306 		for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
307 			kfree(ipc->msg[i].tx_data);
308 			kfree(ipc->msg[i].rx_data);
309 		}
310 		kfree(ipc->msg);
311 	}
312 }
313 EXPORT_SYMBOL_GPL(sst_ipc_fini);
314 
315 /* Module information */
316 MODULE_AUTHOR("Jin Yao");
317 MODULE_DESCRIPTION("Intel SST IPC generic");
318 MODULE_LICENSE("GPL v2");
319