sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / sound / soc / intel / common / sst-ipc.c
blob62f3a8e0ec87696389d46c7824297b5e36fce188
1 /*
2 * Intel SST generic IPC Support
4 * Copyright (C) 2015, Intel Corporation. All rights reserved.
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.
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.
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>
31 #include "sst-dsp.h"
32 #include "sst-dsp-priv.h"
33 #include "sst-ipc.h"
35 /* IPC message timeout (msecs) */
36 #define IPC_TIMEOUT_MSECS 300
38 #define IPC_EMPTY_LIST_SIZE 8
40 /* locks held by caller */
41 static struct ipc_message *msg_get_empty(struct sst_generic_ipc *ipc)
43 struct ipc_message *msg = NULL;
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);
51 return msg;
54 static int tx_wait_done(struct sst_generic_ipc *ipc,
55 struct ipc_message *msg, void *rx_data)
57 unsigned long flags;
58 int ret;
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));
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");
69 list_del(&msg->list);
70 ret = -ETIMEDOUT;
71 } else {
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;
79 list_add_tail(&msg->list, &ipc->empty_list);
80 spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
81 return ret;
84 static 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)
88 struct ipc_message *msg;
89 unsigned long flags;
91 spin_lock_irqsave(&ipc->dsp->spinlock, flags);
93 msg = msg_get_empty(ipc);
94 if (msg == NULL) {
95 spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
96 return -EBUSY;
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;
107 if ((tx_bytes) && (ipc->ops.tx_data_copy != NULL))
108 ipc->ops.tx_data_copy(msg, tx_data, tx_bytes);
110 list_add_tail(&msg->list, &ipc->tx_list);
111 schedule_work(&ipc->kwork);
112 spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
114 if (wait)
115 return tx_wait_done(ipc, msg, rx_data);
116 else
117 return 0;
120 static int msg_empty_list_init(struct sst_generic_ipc *ipc)
122 int i;
124 ipc->msg = kzalloc(sizeof(struct ipc_message) *
125 IPC_EMPTY_LIST_SIZE, GFP_KERNEL);
126 if (ipc->msg == NULL)
127 return -ENOMEM;
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;
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;
140 init_waitqueue_head(&ipc->msg[i].waitq);
141 list_add(&ipc->msg[i].list, &ipc->empty_list);
144 return 0;
146 free_mem:
147 while (i > 0) {
148 kfree(ipc->msg[i-1].tx_data);
149 kfree(ipc->msg[i-1].rx_data);
150 --i;
152 kfree(ipc->msg);
154 return -ENOMEM;
157 static void ipc_tx_msgs(struct work_struct *work)
159 struct sst_generic_ipc *ipc =
160 container_of(work, struct sst_generic_ipc, kwork);
161 struct ipc_message *msg;
163 spin_lock_irq(&ipc->dsp->spinlock);
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
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;
175 msg = list_first_entry(&ipc->tx_list, struct ipc_message, list);
176 list_move(&msg->list, &ipc->rx_list);
178 if (ipc->ops.tx_msg != NULL)
179 ipc->ops.tx_msg(ipc, msg);
182 spin_unlock_irq(&ipc->dsp->spinlock);
185 int 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)
188 int ret;
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
195 if (ipc->ops.check_dsp_lp_on)
196 if (ipc->ops.check_dsp_lp_on(ipc->dsp, true))
197 return -EIO;
199 ret = ipc_tx_message(ipc, header, tx_data, tx_bytes,
200 rx_data, rx_bytes, 1);
202 if (ipc->ops.check_dsp_lp_on)
203 if (ipc->ops.check_dsp_lp_on(ipc->dsp, false))
204 return -EIO;
206 return ret;
208 EXPORT_SYMBOL_GPL(sst_ipc_tx_message_wait);
210 int sst_ipc_tx_message_nowait(struct sst_generic_ipc *ipc, u64 header,
211 void *tx_data, size_t tx_bytes)
213 return ipc_tx_message(ipc, header, tx_data, tx_bytes,
214 NULL, 0, 0);
216 EXPORT_SYMBOL_GPL(sst_ipc_tx_message_nowait);
218 int 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)
221 return ipc_tx_message(ipc, header, tx_data, tx_bytes,
222 rx_data, rx_bytes, 1);
224 EXPORT_SYMBOL_GPL(sst_ipc_tx_message_nopm);
226 struct ipc_message *sst_ipc_reply_find_msg(struct sst_generic_ipc *ipc,
227 u64 header)
229 struct ipc_message *msg;
230 u64 mask;
232 if (ipc->ops.reply_msg_match != NULL)
233 header = ipc->ops.reply_msg_match(header, &mask);
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;
241 list_for_each_entry(msg, &ipc->rx_list, list) {
242 if ((msg->header & mask) == header)
243 return msg;
246 return NULL;
248 EXPORT_SYMBOL_GPL(sst_ipc_reply_find_msg);
250 /* locks held by caller */
251 void sst_ipc_tx_msg_reply_complete(struct sst_generic_ipc *ipc,
252 struct ipc_message *msg)
254 msg->complete = true;
256 if (!msg->wait)
257 list_add_tail(&msg->list, &ipc->empty_list);
258 else
259 wake_up(&msg->waitq);
261 EXPORT_SYMBOL_GPL(sst_ipc_tx_msg_reply_complete);
263 void sst_ipc_drop_all(struct sst_generic_ipc *ipc)
265 struct ipc_message *msg, *tmp;
266 unsigned long flags;
267 int tx_drop_cnt = 0, rx_drop_cnt = 0;
269 /* drop all TX and Rx messages before we stall + reset DSP */
270 spin_lock_irqsave(&ipc->dsp->spinlock, flags);
272 list_for_each_entry_safe(msg, tmp, &ipc->tx_list, list) {
273 list_move(&msg->list, &ipc->empty_list);
274 tx_drop_cnt++;
277 list_for_each_entry_safe(msg, tmp, &ipc->rx_list, list) {
278 list_move(&msg->list, &ipc->empty_list);
279 rx_drop_cnt++;
282 spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
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);
288 EXPORT_SYMBOL_GPL(sst_ipc_drop_all);
290 int sst_ipc_init(struct sst_generic_ipc *ipc)
292 int ret;
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);
299 ret = msg_empty_list_init(ipc);
300 if (ret < 0)
301 return -ENOMEM;
303 INIT_WORK(&ipc->kwork, ipc_tx_msgs);
304 return 0;
306 EXPORT_SYMBOL_GPL(sst_ipc_init);
308 void sst_ipc_fini(struct sst_generic_ipc *ipc)
310 int i;
312 cancel_work_sync(&ipc->kwork);
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);
319 kfree(ipc->msg);
322 EXPORT_SYMBOL_GPL(sst_ipc_fini);
324 /* Module information */
325 MODULE_AUTHOR("Jin Yao");
326 MODULE_DESCRIPTION("Intel SST IPC generic");
327 MODULE_LICENSE("GPL v2");