Linux Audio

Check our new training course

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