blob: b97c231a4f3952bbb45e0d7b68dff28fff54c78d [file] [log] [blame]
Vinod Koul163d2082014-10-16 20:00:13 +05301/*
2 * sst.c - Intel SST Driver for audio engine
3 *
4 * Copyright (C) 2008-14 Intel Corp
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/module.h>
Vinod Koul163d2082014-10-16 20:00:13 +053023#include <linux/fs.h>
24#include <linux/interrupt.h>
25#include <linux/firmware.h>
26#include <linux/pm_runtime.h>
27#include <linux/pm_qos.h>
28#include <linux/async.h>
Vinod Koul163d2082014-10-16 20:00:13 +053029#include <sound/core.h>
Vinod Koul163d2082014-10-16 20:00:13 +053030#include <sound/soc.h>
Vinod Koul163d2082014-10-16 20:00:13 +053031#include <asm/platform_sst_audio.h>
32#include "../sst-mfld-platform.h"
33#include "sst.h"
34#include "../sst-dsp.h"
35
36MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
37MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
38MODULE_DESCRIPTION("Intel (R) SST(R) Audio Engine Driver");
39MODULE_LICENSE("GPL v2");
40
41static inline bool sst_is_process_reply(u32 msg_id)
42{
43 return ((msg_id & PROCESS_MSG) ? true : false);
44}
45
46static inline bool sst_validate_mailbox_size(unsigned int size)
47{
48 return ((size <= SST_MAILBOX_SIZE) ? true : false);
49}
50
51static irqreturn_t intel_sst_interrupt_mrfld(int irq, void *context)
52{
53 union interrupt_reg_mrfld isr;
54 union ipc_header_mrfld header;
55 union sst_imr_reg_mrfld imr;
56 struct ipc_post *msg = NULL;
57 unsigned int size = 0;
58 struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
59 irqreturn_t retval = IRQ_HANDLED;
60
61 /* Interrupt arrived, check src */
62 isr.full = sst_shim_read64(drv->shim, SST_ISRX);
63
64 if (isr.part.done_interrupt) {
65 /* Clear done bit */
66 spin_lock(&drv->ipc_spin_lock);
67 header.full = sst_shim_read64(drv->shim,
68 drv->ipc_reg.ipcx);
69 header.p.header_high.part.done = 0;
70 sst_shim_write64(drv->shim, drv->ipc_reg.ipcx, header.full);
71
72 /* write 1 to clear status register */;
73 isr.part.done_interrupt = 1;
74 sst_shim_write64(drv->shim, SST_ISRX, isr.full);
75 spin_unlock(&drv->ipc_spin_lock);
76
77 /* we can send more messages to DSP so trigger work */
78 queue_work(drv->post_msg_wq, &drv->ipc_post_msg_wq);
79 retval = IRQ_HANDLED;
80 }
81
82 if (isr.part.busy_interrupt) {
83 /* message from dsp so copy that */
84 spin_lock(&drv->ipc_spin_lock);
85 imr.full = sst_shim_read64(drv->shim, SST_IMRX);
86 imr.part.busy_interrupt = 1;
87 sst_shim_write64(drv->shim, SST_IMRX, imr.full);
88 spin_unlock(&drv->ipc_spin_lock);
89 header.full = sst_shim_read64(drv->shim, drv->ipc_reg.ipcd);
90
91 if (sst_create_ipc_msg(&msg, header.p.header_high.part.large)) {
92 drv->ops->clear_interrupt(drv);
93 return IRQ_HANDLED;
94 }
95
96 if (header.p.header_high.part.large) {
97 size = header.p.header_low_payload;
98 if (sst_validate_mailbox_size(size)) {
99 memcpy_fromio(msg->mailbox_data,
100 drv->mailbox + drv->mailbox_recv_offset, size);
101 } else {
102 dev_err(drv->dev,
103 "Mailbox not copied, payload size is: %u\n", size);
104 header.p.header_low_payload = 0;
105 }
106 }
107
108 msg->mrfld_header = header;
109 msg->is_process_reply =
110 sst_is_process_reply(header.p.header_high.part.msg_id);
111 spin_lock(&drv->rx_msg_lock);
112 list_add_tail(&msg->node, &drv->rx_list);
113 spin_unlock(&drv->rx_msg_lock);
114 drv->ops->clear_interrupt(drv);
115 retval = IRQ_WAKE_THREAD;
116 }
117 return retval;
118}
119
120static irqreturn_t intel_sst_irq_thread_mrfld(int irq, void *context)
121{
122 struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
123 struct ipc_post *__msg, *msg = NULL;
124 unsigned long irq_flags;
125
126 spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
127 if (list_empty(&drv->rx_list)) {
128 spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
129 return IRQ_HANDLED;
130 }
131
132 list_for_each_entry_safe(msg, __msg, &drv->rx_list, node) {
133 list_del(&msg->node);
134 spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
135 if (msg->is_process_reply)
136 drv->ops->process_message(msg);
137 else
138 drv->ops->process_reply(drv, msg);
139
140 if (msg->is_large)
141 kfree(msg->mailbox_data);
142 kfree(msg);
143 spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
144 }
145 spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
146 return IRQ_HANDLED;
147}
148
Vinod Kould62f2a02014-10-31 12:38:20 +0530149static int sst_save_dsp_context_v2(struct intel_sst_drv *sst)
150{
151 int ret = 0;
152
153 ret = sst_prepare_and_post_msg(sst, SST_TASK_ID_MEDIA, IPC_CMD,
154 IPC_PREP_D3, PIPE_RSVD, 0, NULL, NULL,
155 true, true, false, true);
156
157 if (ret < 0) {
158 dev_err(sst->dev, "not suspending FW!!, Err: %d\n", ret);
159 return -EIO;
160 }
161
162 return 0;
163}
164
165
Vinod Koul163d2082014-10-16 20:00:13 +0530166static struct intel_sst_ops mrfld_ops = {
167 .interrupt = intel_sst_interrupt_mrfld,
168 .irq_thread = intel_sst_irq_thread_mrfld,
169 .clear_interrupt = intel_sst_clear_intr_mrfld,
170 .start = sst_start_mrfld,
171 .reset = intel_sst_reset_dsp_mrfld,
172 .post_message = sst_post_message_mrfld,
173 .process_reply = sst_process_reply_mrfld,
Vinod Kould62f2a02014-10-31 12:38:20 +0530174 .save_dsp_context = sst_save_dsp_context_v2,
Vinod Koul163d2082014-10-16 20:00:13 +0530175 .alloc_stream = sst_alloc_stream_mrfld,
176 .post_download = sst_post_download_mrfld,
177};
178
179int sst_driver_ops(struct intel_sst_drv *sst)
180{
181
Subhransu S. Prusty39581032014-10-24 13:49:46 +0530182 switch (sst->dev_id) {
Vinod Koul163d2082014-10-16 20:00:13 +0530183 case SST_MRFLD_PCI_ID:
184 sst->tstamp = SST_TIME_STAMP_MRFLD;
185 sst->ops = &mrfld_ops;
186 return 0;
187
188 default:
189 dev_err(sst->dev,
Subhransu S. Prusty39581032014-10-24 13:49:46 +0530190 "SST Driver capablities missing for dev_id: %x", sst->dev_id);
Vinod Koul163d2082014-10-16 20:00:13 +0530191 return -EINVAL;
192 };
193}
194
195void sst_process_pending_msg(struct work_struct *work)
196{
197 struct intel_sst_drv *ctx = container_of(work,
198 struct intel_sst_drv, ipc_post_msg_wq);
199
200 ctx->ops->post_message(ctx, NULL, false);
201}
202
Subhransu S. Prusty7e73e4d2014-10-30 16:21:45 +0530203static int sst_workqueue_init(struct intel_sst_drv *ctx)
204{
205 INIT_LIST_HEAD(&ctx->memcpy_list);
206 INIT_LIST_HEAD(&ctx->rx_list);
207 INIT_LIST_HEAD(&ctx->ipc_dispatch_list);
208 INIT_LIST_HEAD(&ctx->block_list);
209 INIT_WORK(&ctx->ipc_post_msg_wq, sst_process_pending_msg);
210 init_waitqueue_head(&ctx->wait_queue);
211
212 ctx->post_msg_wq =
213 create_singlethread_workqueue("sst_post_msg_wq");
214 if (!ctx->post_msg_wq)
215 return -EBUSY;
216 return 0;
217}
218
Subhransu S. Prusty54adc0a2014-10-30 16:21:46 +0530219static void sst_init_locks(struct intel_sst_drv *ctx)
220{
221 mutex_init(&ctx->sst_lock);
222 spin_lock_init(&ctx->rx_msg_lock);
223 spin_lock_init(&ctx->ipc_spin_lock);
224 spin_lock_init(&ctx->block_lock);
225}
226
Subhransu S. Prusty2559d992014-10-30 16:21:47 +0530227int sst_alloc_drv_context(struct intel_sst_drv **ctx,
228 struct device *dev, unsigned int dev_id)
229{
230 *ctx = devm_kzalloc(dev, sizeof(struct intel_sst_drv), GFP_KERNEL);
231 if (!(*ctx))
232 return -ENOMEM;
233
234 (*ctx)->dev = dev;
235 (*ctx)->dev_id = dev_id;
236
237 return 0;
238}
Vinod Koulf533a032014-11-04 16:25:16 +0530239EXPORT_SYMBOL_GPL(sst_alloc_drv_context);
Subhransu S. Prusty2559d992014-10-30 16:21:47 +0530240
Subhransu S. Prusty250454d8f2014-10-30 16:21:48 +0530241int sst_context_init(struct intel_sst_drv *ctx)
242{
243 int ret = 0, i;
244
245 if (!ctx->pdata)
246 return -EINVAL;
247
248 if (!ctx->pdata->probe_data)
249 return -EINVAL;
250
251 memcpy(&ctx->info, ctx->pdata->probe_data, sizeof(ctx->info));
252
253 ret = sst_driver_ops(ctx);
254 if (ret != 0)
255 return -EINVAL;
256
257 sst_init_locks(ctx);
Vinod Koulf533a032014-11-04 16:25:16 +0530258 sst_set_fw_state_locked(ctx, SST_RESET);
Subhransu S. Prusty250454d8f2014-10-30 16:21:48 +0530259
260 /* pvt_id 0 reserved for async messages */
261 ctx->pvt_id = 1;
262 ctx->stream_cnt = 0;
263 ctx->fw_in_mem = NULL;
264 /* we use memcpy, so set to 0 */
265 ctx->use_dma = 0;
266 ctx->use_lli = 0;
267
268 if (sst_workqueue_init(ctx))
269 return -EINVAL;
270
271 ctx->mailbox_recv_offset = ctx->pdata->ipc_info->mbox_recv_off;
272 ctx->ipc_reg.ipcx = SST_IPCX + ctx->pdata->ipc_info->ipc_offset;
273 ctx->ipc_reg.ipcd = SST_IPCD + ctx->pdata->ipc_info->ipc_offset;
274
275 dev_info(ctx->dev, "Got drv data max stream %d\n",
276 ctx->info.max_streams);
277
278 for (i = 1; i <= ctx->info.max_streams; i++) {
279 struct stream_info *stream = &ctx->streams[i];
280
281 memset(stream, 0, sizeof(*stream));
282 stream->pipe_id = PIPE_RSVD;
283 mutex_init(&stream->lock);
284 }
285
286 /* Register the ISR */
287 ret = devm_request_threaded_irq(ctx->dev, ctx->irq_num, ctx->ops->interrupt,
288 ctx->ops->irq_thread, 0, SST_DRV_NAME,
289 ctx);
290 if (ret)
291 goto do_free_mem;
292
293 dev_dbg(ctx->dev, "Registered IRQ %#x\n", ctx->irq_num);
294
295 /* default intr are unmasked so set this as masked */
296 sst_shim_write64(ctx->shim, SST_IMRX, 0xFFFF0038);
297
298 ctx->qos = devm_kzalloc(ctx->dev,
299 sizeof(struct pm_qos_request), GFP_KERNEL);
300 if (!ctx->qos) {
301 ret = -ENOMEM;
302 goto do_free_mem;
303 }
304 pm_qos_add_request(ctx->qos, PM_QOS_CPU_DMA_LATENCY,
305 PM_QOS_DEFAULT_VALUE);
Vinod Koulf533a032014-11-04 16:25:16 +0530306
307 dev_dbg(ctx->dev, "Requesting FW %s now...\n", ctx->firmware_name);
308 ret = request_firmware_nowait(THIS_MODULE, true, ctx->firmware_name,
309 ctx->dev, GFP_KERNEL, ctx, sst_firmware_load_cb);
310 if (ret) {
311 dev_err(ctx->dev, "Firmware download failed:%d\n", ret);
312 goto do_free_mem;
313 }
314 sst_register(ctx->dev);
Subhransu S. Prusty250454d8f2014-10-30 16:21:48 +0530315 return 0;
316
317do_free_mem:
318 destroy_workqueue(ctx->post_msg_wq);
319 return ret;
320}
Vinod Koulf533a032014-11-04 16:25:16 +0530321EXPORT_SYMBOL_GPL(sst_context_init);
Subhransu S. Prusty250454d8f2014-10-30 16:21:48 +0530322
323void sst_context_cleanup(struct intel_sst_drv *ctx)
324{
325 pm_runtime_get_noresume(ctx->dev);
326 pm_runtime_forbid(ctx->dev);
327 sst_unregister(ctx->dev);
328 sst_set_fw_state_locked(ctx, SST_SHUTDOWN);
329 flush_scheduled_work();
330 destroy_workqueue(ctx->post_msg_wq);
331 pm_qos_remove_request(ctx->qos);
332 kfree(ctx->fw_sg_list.src);
333 kfree(ctx->fw_sg_list.dst);
334 ctx->fw_sg_list.list_len = 0;
335 kfree(ctx->fw_in_mem);
336 ctx->fw_in_mem = NULL;
337 sst_memcpy_free_resources(ctx);
338 ctx = NULL;
339}
Vinod Koulf533a032014-11-04 16:25:16 +0530340EXPORT_SYMBOL_GPL(sst_context_cleanup);
Subhransu S. Prusty2559d992014-10-30 16:21:47 +0530341
Vinod Koulb0d94ac2014-11-04 16:25:17 +0530342static inline void sst_save_shim64(struct intel_sst_drv *ctx,
343 void __iomem *shim,
344 struct sst_shim_regs64 *shim_regs)
345{
346 unsigned long irq_flags;
347
348 spin_lock_irqsave(&ctx->ipc_spin_lock, irq_flags);
349
350 shim_regs->imrx = sst_shim_read64(shim, SST_IMRX),
351
352 spin_unlock_irqrestore(&ctx->ipc_spin_lock, irq_flags);
353}
354
355static inline void sst_restore_shim64(struct intel_sst_drv *ctx,
356 void __iomem *shim,
357 struct sst_shim_regs64 *shim_regs)
358{
359 unsigned long irq_flags;
360
361 /*
362 * we only need to restore IMRX for this case, rest will be
363 * initialize by FW or driver when firmware is loaded
364 */
365 spin_lock_irqsave(&ctx->ipc_spin_lock, irq_flags);
366 sst_shim_write64(shim, SST_IMRX, shim_regs->imrx),
367 spin_unlock_irqrestore(&ctx->ipc_spin_lock, irq_flags);
368}
369
Subhransu S. Prusty7fb73c72014-10-30 16:21:49 +0530370void sst_configure_runtime_pm(struct intel_sst_drv *ctx)
371{
372 pm_runtime_set_autosuspend_delay(ctx->dev, SST_SUSPEND_DELAY);
373 pm_runtime_use_autosuspend(ctx->dev);
374 pm_runtime_allow(ctx->dev);
375 pm_runtime_put_noidle(ctx->dev);
376}
Vinod Koulf533a032014-11-04 16:25:16 +0530377EXPORT_SYMBOL_GPL(sst_configure_runtime_pm);
Vinod Koul163d2082014-10-16 20:00:13 +0530378
Vinod Kould62f2a02014-10-31 12:38:20 +0530379static int intel_sst_runtime_suspend(struct device *dev)
380{
381 int ret = 0;
382 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
383
384 if (ctx->sst_state == SST_RESET) {
385 dev_dbg(dev, "LPE is already in RESET state, No action\n");
386 return 0;
387 }
388 /* save fw context */
389 if (ctx->ops->save_dsp_context(ctx))
390 return -EBUSY;
391
392 /* Move the SST state to Reset */
393 sst_set_fw_state_locked(ctx, SST_RESET);
394
395 synchronize_irq(ctx->irq_num);
396 flush_workqueue(ctx->post_msg_wq);
397
398 return ret;
399}
400
401static int intel_sst_runtime_resume(struct device *dev)
402{
403 int ret = 0;
404 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
405
Vinod Kould62f2a02014-10-31 12:38:20 +0530406 if (ctx->sst_state == SST_RESET) {
407 ret = sst_load_fw(ctx);
408 if (ret) {
409 dev_err(dev, "FW download fail %d\n", ret);
Mythri P K45f31bf2014-10-31 12:38:21 +0530410 sst_set_fw_state_locked(ctx, SST_RESET);
Vinod Kould62f2a02014-10-31 12:38:20 +0530411 }
412 }
Vinod Kould62f2a02014-10-31 12:38:20 +0530413 return ret;
414}
415
Vinod Koulf533a032014-11-04 16:25:16 +0530416const struct dev_pm_ops intel_sst_pm = {
Vinod Kould62f2a02014-10-31 12:38:20 +0530417 .runtime_suspend = intel_sst_runtime_suspend,
418 .runtime_resume = intel_sst_runtime_resume,
419};
Vinod Koulf533a032014-11-04 16:25:16 +0530420EXPORT_SYMBOL_GPL(intel_sst_pm);