blob: 41a2b41b232b91fb4f8f46dfe05a13d0918cef33 [file] [log] [blame]
Vinod Koulea12aa42014-10-16 20:00:16 +05301/*
2 * sst_ipc.c - Intel SST Driver for audio engine
3 *
4 * Copyright (C) 2008-14 Intel Corporation
5 * Authors: Vinod Koul <vinod.koul@intel.com>
6 * Harsha Priya <priya.harsha@intel.com>
7 * Dharageswari R <dharageswari.r@intel.com>
8 * KP Jeeja <jeeja.kp@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 */
22#include <linux/pci.h>
23#include <linux/firmware.h>
24#include <linux/sched.h>
25#include <linux/delay.h>
26#include <linux/pm_runtime.h>
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/soc.h>
30#include <sound/compress_driver.h>
31#include <asm/intel-mid.h>
32#include <asm/platform_sst_audio.h>
33#include "../sst-mfld-platform.h"
34#include "sst.h"
35#include "../sst-dsp.h"
36
37struct sst_block *sst_create_block(struct intel_sst_drv *ctx,
38 u32 msg_id, u32 drv_id)
39{
40 struct sst_block *msg = NULL;
41
42 dev_dbg(ctx->dev, "Enter\n");
43 msg = kzalloc(sizeof(*msg), GFP_KERNEL);
44 if (!msg)
45 return NULL;
46 msg->condition = false;
47 msg->on = true;
48 msg->msg_id = msg_id;
49 msg->drv_id = drv_id;
50 spin_lock_bh(&ctx->block_lock);
51 list_add_tail(&msg->node, &ctx->block_list);
52 spin_unlock_bh(&ctx->block_lock);
53
54 return msg;
55}
56
57int sst_wake_up_block(struct intel_sst_drv *ctx, int result,
58 u32 drv_id, u32 ipc, void *data, u32 size)
59{
60 struct sst_block *block = NULL;
61
62 dev_dbg(ctx->dev, "Enter\n");
63
64 spin_lock_bh(&ctx->block_lock);
65 list_for_each_entry(block, &ctx->block_list, node) {
66 dev_dbg(ctx->dev, "Block ipc %d, drv_id %d\n", block->msg_id,
67 block->drv_id);
68 if (block->msg_id == ipc && block->drv_id == drv_id) {
69 dev_dbg(ctx->dev, "free up the block\n");
70 block->ret_code = result;
71 block->data = data;
72 block->size = size;
73 block->condition = true;
74 spin_unlock_bh(&ctx->block_lock);
75 wake_up(&ctx->wait_queue);
76 return 0;
77 }
78 }
79 spin_unlock_bh(&ctx->block_lock);
80 dev_dbg(ctx->dev,
81 "Block not found or a response received for a short msg for ipc %d, drv_id %d\n",
82 ipc, drv_id);
83 return -EINVAL;
84}
85
86int sst_free_block(struct intel_sst_drv *ctx, struct sst_block *freed)
87{
88 struct sst_block *block = NULL, *__block;
89
90 dev_dbg(ctx->dev, "Enter\n");
91 spin_lock_bh(&ctx->block_lock);
92 list_for_each_entry_safe(block, __block, &ctx->block_list, node) {
93 if (block == freed) {
94 pr_debug("pvt_id freed --> %d\n", freed->drv_id);
95 /* toggle the index position of pvt_id */
96 list_del(&freed->node);
97 spin_unlock_bh(&ctx->block_lock);
98 kfree(freed->data);
99 freed->data = NULL;
100 kfree(freed);
101 return 0;
102 }
103 }
104 spin_unlock_bh(&ctx->block_lock);
105 dev_err(ctx->dev, "block is already freed!!!\n");
106 return -EINVAL;
107}
108
109int sst_post_message_mrfld(struct intel_sst_drv *sst_drv_ctx,
110 struct ipc_post *ipc_msg, bool sync)
111{
112 struct ipc_post *msg = ipc_msg;
113 union ipc_header_mrfld header;
114 unsigned int loop_count = 0;
115 int retval = 0;
116 unsigned long irq_flags;
117
118 dev_dbg(sst_drv_ctx->dev, "Enter: sync: %d\n", sync);
119 spin_lock_irqsave(&sst_drv_ctx->ipc_spin_lock, irq_flags);
120 header.full = sst_shim_read64(sst_drv_ctx->shim, SST_IPCX);
121 if (sync) {
122 while (header.p.header_high.part.busy) {
123 if (loop_count > 25) {
124 dev_err(sst_drv_ctx->dev,
125 "sst: Busy wait failed, cant send this msg\n");
126 retval = -EBUSY;
127 goto out;
128 }
129 cpu_relax();
130 loop_count++;
131 header.full = sst_shim_read64(sst_drv_ctx->shim, SST_IPCX);
132 }
133 } else {
134 if (list_empty(&sst_drv_ctx->ipc_dispatch_list)) {
135 /* queue is empty, nothing to send */
136 spin_unlock_irqrestore(&sst_drv_ctx->ipc_spin_lock, irq_flags);
137 dev_dbg(sst_drv_ctx->dev,
138 "Empty msg queue... NO Action\n");
139 return 0;
140 }
141
142 if (header.p.header_high.part.busy) {
143 spin_unlock_irqrestore(&sst_drv_ctx->ipc_spin_lock, irq_flags);
144 dev_dbg(sst_drv_ctx->dev, "Busy not free... post later\n");
145 return 0;
146 }
147
148 /* copy msg from list */
149 msg = list_entry(sst_drv_ctx->ipc_dispatch_list.next,
150 struct ipc_post, node);
151 list_del(&msg->node);
152 }
153 dev_dbg(sst_drv_ctx->dev, "sst: Post message: header = %x\n",
154 msg->mrfld_header.p.header_high.full);
155 dev_dbg(sst_drv_ctx->dev, "sst: size = 0x%x\n",
156 msg->mrfld_header.p.header_low_payload);
157
158 if (msg->mrfld_header.p.header_high.part.large)
159 memcpy_toio(sst_drv_ctx->mailbox + SST_MAILBOX_SEND,
160 msg->mailbox_data,
161 msg->mrfld_header.p.header_low_payload);
162
163 sst_shim_write64(sst_drv_ctx->shim, SST_IPCX, msg->mrfld_header.full);
164
165out:
166 spin_unlock_irqrestore(&sst_drv_ctx->ipc_spin_lock, irq_flags);
167 kfree(msg->mailbox_data);
168 kfree(msg);
169 return retval;
170}
171
172void intel_sst_clear_intr_mrfld(struct intel_sst_drv *sst_drv_ctx)
173{
174 union interrupt_reg_mrfld isr;
175 union interrupt_reg_mrfld imr;
176 union ipc_header_mrfld clear_ipc;
177 unsigned long irq_flags;
178
179 spin_lock_irqsave(&sst_drv_ctx->ipc_spin_lock, irq_flags);
180 imr.full = sst_shim_read64(sst_drv_ctx->shim, SST_IMRX);
181 isr.full = sst_shim_read64(sst_drv_ctx->shim, SST_ISRX);
182
183 /* write 1 to clear*/
184 isr.part.busy_interrupt = 1;
185 sst_shim_write64(sst_drv_ctx->shim, SST_ISRX, isr.full);
186
187 /* Set IA done bit */
188 clear_ipc.full = sst_shim_read64(sst_drv_ctx->shim, SST_IPCD);
189
190 clear_ipc.p.header_high.part.busy = 0;
191 clear_ipc.p.header_high.part.done = 1;
192 clear_ipc.p.header_low_payload = IPC_ACK_SUCCESS;
193 sst_shim_write64(sst_drv_ctx->shim, SST_IPCD, clear_ipc.full);
194 /* un mask busy interrupt */
195 imr.part.busy_interrupt = 0;
196 sst_shim_write64(sst_drv_ctx->shim, SST_IMRX, imr.full);
197 spin_unlock_irqrestore(&sst_drv_ctx->ipc_spin_lock, irq_flags);
198}
199
200
201/*
202 * process_fw_init - process the FW init msg
203 *
204 * @msg: IPC message mailbox data from FW
205 *
206 * This function processes the FW init msg from FW
207 * marks FW state and prints debug info of loaded FW
208 */
209static void process_fw_init(struct intel_sst_drv *sst_drv_ctx,
210 void *msg)
211{
212 struct ipc_header_fw_init *init =
213 (struct ipc_header_fw_init *)msg;
214 int retval = 0;
215
216 dev_dbg(sst_drv_ctx->dev, "*** FW Init msg came***\n");
217 if (init->result) {
218 sst_drv_ctx->sst_state = SST_RESET;
219 dev_err(sst_drv_ctx->dev, "FW Init failed, Error %x\n",
220 init->result);
221 retval = init->result;
222 goto ret;
223 }
224
225ret:
226 sst_wake_up_block(sst_drv_ctx, retval, FW_DWNL_ID, 0 , NULL, 0);
227}
228
229static void process_fw_async_msg(struct intel_sst_drv *sst_drv_ctx,
230 struct ipc_post *msg)
231{
232 u32 msg_id;
233 int str_id;
234 u32 data_size, i;
235 void *data_offset;
236 struct stream_info *stream;
237 union ipc_header_high msg_high;
238 u32 msg_low, pipe_id;
239
240 msg_high = msg->mrfld_header.p.header_high;
241 msg_low = msg->mrfld_header.p.header_low_payload;
242 msg_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->cmd_id;
243 data_offset = (msg->mailbox_data + sizeof(struct ipc_dsp_hdr));
244 data_size = msg_low - (sizeof(struct ipc_dsp_hdr));
245
246 switch (msg_id) {
247 case IPC_SST_PERIOD_ELAPSED_MRFLD:
248 pipe_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->pipe_id;
249 str_id = get_stream_id_mrfld(sst_drv_ctx, pipe_id);
250 if (str_id > 0) {
251 dev_dbg(sst_drv_ctx->dev,
252 "Period elapsed rcvd for pipe id 0x%x\n",
253 pipe_id);
254 stream = &sst_drv_ctx->streams[str_id];
255 if (stream->period_elapsed)
256 stream->period_elapsed(stream->pcm_substream);
257 if (stream->compr_cb)
258 stream->compr_cb(stream->compr_cb_param);
259 }
260 break;
261
262 case IPC_IA_DRAIN_STREAM_MRFLD:
263 pipe_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->pipe_id;
264 str_id = get_stream_id_mrfld(sst_drv_ctx, pipe_id);
265 if (str_id > 0) {
266 stream = &sst_drv_ctx->streams[str_id];
267 if (stream->drain_notify)
268 stream->drain_notify(stream->drain_cb_param);
269 }
270 break;
271
272 case IPC_IA_FW_ASYNC_ERR_MRFLD:
273 dev_err(sst_drv_ctx->dev, "FW sent async error msg:\n");
274 for (i = 0; i < (data_size/4); i++)
275 print_hex_dump(KERN_DEBUG, NULL, DUMP_PREFIX_NONE,
276 16, 4, data_offset, data_size, false);
277 break;
278
279 case IPC_IA_FW_INIT_CMPLT_MRFLD:
280 process_fw_init(sst_drv_ctx, data_offset);
281 break;
282
283 case IPC_IA_BUF_UNDER_RUN_MRFLD:
284 pipe_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->pipe_id;
285 str_id = get_stream_id_mrfld(sst_drv_ctx, pipe_id);
286 if (str_id > 0)
287 dev_err(sst_drv_ctx->dev,
288 "Buffer under-run for pipe:%#x str_id:%d\n",
289 pipe_id, str_id);
290 break;
291
292 default:
293 dev_err(sst_drv_ctx->dev,
294 "Unrecognized async msg from FW msg_id %#x\n", msg_id);
295 }
296}
297
298void sst_process_reply_mrfld(struct intel_sst_drv *sst_drv_ctx,
299 struct ipc_post *msg)
300{
301 unsigned int drv_id;
302 void *data;
303 union ipc_header_high msg_high;
304 u32 msg_low;
305 struct ipc_dsp_hdr *dsp_hdr;
306 unsigned int cmd_id;
307
308 msg_high = msg->mrfld_header.p.header_high;
309 msg_low = msg->mrfld_header.p.header_low_payload;
310
311 dev_dbg(sst_drv_ctx->dev, "IPC process message header %x payload %x\n",
312 msg->mrfld_header.p.header_high.full,
313 msg->mrfld_header.p.header_low_payload);
314
315 drv_id = msg_high.part.drv_id;
316
317 /* Check for async messages first */
318 if (drv_id == SST_ASYNC_DRV_ID) {
319 /*FW sent async large message*/
320 process_fw_async_msg(sst_drv_ctx, msg);
321 return;
322 }
323
324 /* FW sent short error response for an IPC */
325 if (msg_high.part.result && drv_id && !msg_high.part.large) {
326 /* 32-bit FW error code in msg_low */
327 dev_err(sst_drv_ctx->dev, "FW sent error response 0x%x", msg_low);
328 sst_wake_up_block(sst_drv_ctx, msg_high.part.result,
329 msg_high.part.drv_id,
330 msg_high.part.msg_id, NULL, 0);
331 return;
332 }
333
334 /*
335 * Process all valid responses
336 * if it is a large message, the payload contains the size to
337 * copy from mailbox
338 **/
339 if (msg_high.part.large) {
340 data = kzalloc(msg_low, GFP_KERNEL);
341 if (!data)
342 return;
343 memcpy(data, (void *) msg->mailbox_data, msg_low);
344 /* Copy command id so that we can use to put sst to reset */
345 dsp_hdr = (struct ipc_dsp_hdr *)data;
346 cmd_id = dsp_hdr->cmd_id;
347 dev_dbg(sst_drv_ctx->dev, "cmd_id %d\n", dsp_hdr->cmd_id);
348 if (sst_wake_up_block(sst_drv_ctx, msg_high.part.result,
349 msg_high.part.drv_id,
350 msg_high.part.msg_id, data, msg_low))
351 kfree(data);
352 } else {
353 sst_wake_up_block(sst_drv_ctx, msg_high.part.result,
354 msg_high.part.drv_id,
355 msg_high.part.msg_id, NULL, 0);
356 }
357
358}