2 * sst.c - Intel SST Driver for audio engine
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/module.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>
29 #include <linux/acpi.h>
30 #include <linux/sysfs.h>
31 #include <sound/core.h>
32 #include <sound/soc.h>
33 #include <asm/platform_sst_audio.h>
34 #include "../sst-mfld-platform.h"
36 #include "../../common/sst-dsp.h"
38 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
39 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
40 MODULE_DESCRIPTION("Intel (R) SST(R) Audio Engine Driver");
41 MODULE_LICENSE("GPL v2");
43 static inline bool sst_is_process_reply(u32 msg_id)
45 return ((msg_id & PROCESS_MSG) ? true : false);
48 static inline bool sst_validate_mailbox_size(unsigned int size)
50 return ((size <= SST_MAILBOX_SIZE) ? true : false);
53 static irqreturn_t intel_sst_interrupt_mrfld(int irq, void *context)
55 union interrupt_reg_mrfld isr;
56 union ipc_header_mrfld header;
57 union sst_imr_reg_mrfld imr;
58 struct ipc_post *msg = NULL;
59 unsigned int size = 0;
60 struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
61 irqreturn_t retval = IRQ_HANDLED;
63 /* Interrupt arrived, check src */
64 isr.full = sst_shim_read64(drv->shim, SST_ISRX);
66 if (isr.part.done_interrupt) {
68 spin_lock(&drv->ipc_spin_lock);
69 header.full = sst_shim_read64(drv->shim,
71 header.p.header_high.part.done = 0;
72 sst_shim_write64(drv->shim, drv->ipc_reg.ipcx, header.full);
74 /* write 1 to clear status register */;
75 isr.part.done_interrupt = 1;
76 sst_shim_write64(drv->shim, SST_ISRX, isr.full);
77 spin_unlock(&drv->ipc_spin_lock);
79 /* we can send more messages to DSP so trigger work */
80 queue_work(drv->post_msg_wq, &drv->ipc_post_msg_wq);
84 if (isr.part.busy_interrupt) {
85 /* message from dsp so copy that */
86 spin_lock(&drv->ipc_spin_lock);
87 imr.full = sst_shim_read64(drv->shim, SST_IMRX);
88 imr.part.busy_interrupt = 1;
89 sst_shim_write64(drv->shim, SST_IMRX, imr.full);
90 spin_unlock(&drv->ipc_spin_lock);
91 header.full = sst_shim_read64(drv->shim, drv->ipc_reg.ipcd);
93 if (sst_create_ipc_msg(&msg, header.p.header_high.part.large)) {
94 drv->ops->clear_interrupt(drv);
98 if (header.p.header_high.part.large) {
99 size = header.p.header_low_payload;
100 if (sst_validate_mailbox_size(size)) {
101 memcpy_fromio(msg->mailbox_data,
102 drv->mailbox + drv->mailbox_recv_offset, size);
105 "Mailbox not copied, payload size is: %u\n", size);
106 header.p.header_low_payload = 0;
110 msg->mrfld_header = header;
111 msg->is_process_reply =
112 sst_is_process_reply(header.p.header_high.part.msg_id);
113 spin_lock(&drv->rx_msg_lock);
114 list_add_tail(&msg->node, &drv->rx_list);
115 spin_unlock(&drv->rx_msg_lock);
116 drv->ops->clear_interrupt(drv);
117 retval = IRQ_WAKE_THREAD;
122 static irqreturn_t intel_sst_irq_thread_mrfld(int irq, void *context)
124 struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
125 struct ipc_post *__msg, *msg = NULL;
126 unsigned long irq_flags;
128 spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
129 if (list_empty(&drv->rx_list)) {
130 spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
134 list_for_each_entry_safe(msg, __msg, &drv->rx_list, node) {
135 list_del(&msg->node);
136 spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
137 if (msg->is_process_reply)
138 drv->ops->process_message(msg);
140 drv->ops->process_reply(drv, msg);
143 kfree(msg->mailbox_data);
145 spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
147 spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
151 static int sst_save_dsp_context_v2(struct intel_sst_drv *sst)
155 ret = sst_prepare_and_post_msg(sst, SST_TASK_ID_MEDIA, IPC_CMD,
156 IPC_PREP_D3, PIPE_RSVD, 0, NULL, NULL,
157 true, true, false, true);
160 dev_err(sst->dev, "not suspending FW!!, Err: %d\n", ret);
168 static struct intel_sst_ops mrfld_ops = {
169 .interrupt = intel_sst_interrupt_mrfld,
170 .irq_thread = intel_sst_irq_thread_mrfld,
171 .clear_interrupt = intel_sst_clear_intr_mrfld,
172 .start = sst_start_mrfld,
173 .reset = intel_sst_reset_dsp_mrfld,
174 .post_message = sst_post_message_mrfld,
175 .process_reply = sst_process_reply_mrfld,
176 .save_dsp_context = sst_save_dsp_context_v2,
177 .alloc_stream = sst_alloc_stream_mrfld,
178 .post_download = sst_post_download_mrfld,
181 int sst_driver_ops(struct intel_sst_drv *sst)
184 switch (sst->dev_id) {
185 case SST_MRFLD_PCI_ID:
186 case SST_BYT_ACPI_ID:
187 case SST_CHV_ACPI_ID:
188 sst->tstamp = SST_TIME_STAMP_MRFLD;
189 sst->ops = &mrfld_ops;
194 "SST Driver capabilities missing for dev_id: %x",
200 void sst_process_pending_msg(struct work_struct *work)
202 struct intel_sst_drv *ctx = container_of(work,
203 struct intel_sst_drv, ipc_post_msg_wq);
205 ctx->ops->post_message(ctx, NULL, false);
208 static int sst_workqueue_init(struct intel_sst_drv *ctx)
210 INIT_LIST_HEAD(&ctx->memcpy_list);
211 INIT_LIST_HEAD(&ctx->rx_list);
212 INIT_LIST_HEAD(&ctx->ipc_dispatch_list);
213 INIT_LIST_HEAD(&ctx->block_list);
214 INIT_WORK(&ctx->ipc_post_msg_wq, sst_process_pending_msg);
215 init_waitqueue_head(&ctx->wait_queue);
218 create_singlethread_workqueue("sst_post_msg_wq");
219 if (!ctx->post_msg_wq)
224 static void sst_init_locks(struct intel_sst_drv *ctx)
226 mutex_init(&ctx->sst_lock);
227 spin_lock_init(&ctx->rx_msg_lock);
228 spin_lock_init(&ctx->ipc_spin_lock);
229 spin_lock_init(&ctx->block_lock);
232 int sst_alloc_drv_context(struct intel_sst_drv **ctx,
233 struct device *dev, unsigned int dev_id)
235 *ctx = devm_kzalloc(dev, sizeof(struct intel_sst_drv), GFP_KERNEL);
240 (*ctx)->dev_id = dev_id;
244 EXPORT_SYMBOL_GPL(sst_alloc_drv_context);
246 static ssize_t firmware_version_show(struct device *dev,
247 struct device_attribute *attr, char *buf)
249 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
251 if (ctx->fw_version.type == 0 && ctx->fw_version.major == 0 &&
252 ctx->fw_version.minor == 0 && ctx->fw_version.build == 0)
253 return sprintf(buf, "FW not yet loaded\n");
255 return sprintf(buf, "v%02x.%02x.%02x.%02x\n",
256 ctx->fw_version.type, ctx->fw_version.major,
257 ctx->fw_version.minor, ctx->fw_version.build);
261 static DEVICE_ATTR_RO(firmware_version);
263 static const struct attribute *sst_fw_version_attrs[] = {
264 &dev_attr_firmware_version.attr,
268 static const struct attribute_group sst_fw_version_attr_group = {
269 .attrs = (struct attribute **)sst_fw_version_attrs,
272 int sst_context_init(struct intel_sst_drv *ctx)
279 if (!ctx->pdata->probe_data)
282 memcpy(&ctx->info, ctx->pdata->probe_data, sizeof(ctx->info));
284 ret = sst_driver_ops(ctx);
289 sst_set_fw_state_locked(ctx, SST_RESET);
291 /* pvt_id 0 reserved for async messages */
294 ctx->fw_in_mem = NULL;
295 /* we use memcpy, so set to 0 */
299 if (sst_workqueue_init(ctx))
302 ctx->mailbox_recv_offset = ctx->pdata->ipc_info->mbox_recv_off;
303 ctx->ipc_reg.ipcx = SST_IPCX + ctx->pdata->ipc_info->ipc_offset;
304 ctx->ipc_reg.ipcd = SST_IPCD + ctx->pdata->ipc_info->ipc_offset;
306 dev_info(ctx->dev, "Got drv data max stream %d\n",
307 ctx->info.max_streams);
309 for (i = 1; i <= ctx->info.max_streams; i++) {
310 struct stream_info *stream = &ctx->streams[i];
312 memset(stream, 0, sizeof(*stream));
313 stream->pipe_id = PIPE_RSVD;
314 mutex_init(&stream->lock);
317 /* Register the ISR */
318 ret = devm_request_threaded_irq(ctx->dev, ctx->irq_num, ctx->ops->interrupt,
319 ctx->ops->irq_thread, 0, SST_DRV_NAME,
324 dev_dbg(ctx->dev, "Registered IRQ %#x\n", ctx->irq_num);
326 /* default intr are unmasked so set this as masked */
327 sst_shim_write64(ctx->shim, SST_IMRX, 0xFFFF0038);
329 ctx->qos = devm_kzalloc(ctx->dev,
330 sizeof(struct pm_qos_request), GFP_KERNEL);
335 pm_qos_add_request(ctx->qos, PM_QOS_CPU_DMA_LATENCY,
336 PM_QOS_DEFAULT_VALUE);
338 dev_dbg(ctx->dev, "Requesting FW %s now...\n", ctx->firmware_name);
339 ret = reject_firmware_nowait(THIS_MODULE, true, ctx->firmware_name,
340 ctx->dev, GFP_KERNEL, ctx, sst_firmware_load_cb);
342 dev_err(ctx->dev, "Firmware download failed:%d\n", ret);
346 ret = sysfs_create_group(&ctx->dev->kobj,
347 &sst_fw_version_attr_group);
350 "Unable to create sysfs\n");
354 sst_register(ctx->dev);
357 sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
360 destroy_workqueue(ctx->post_msg_wq);
363 EXPORT_SYMBOL_GPL(sst_context_init);
365 void sst_context_cleanup(struct intel_sst_drv *ctx)
367 pm_runtime_get_noresume(ctx->dev);
368 pm_runtime_disable(ctx->dev);
369 sst_unregister(ctx->dev);
370 sst_set_fw_state_locked(ctx, SST_SHUTDOWN);
371 sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
372 flush_scheduled_work();
373 destroy_workqueue(ctx->post_msg_wq);
374 pm_qos_remove_request(ctx->qos);
375 kfree(ctx->fw_sg_list.src);
376 kfree(ctx->fw_sg_list.dst);
377 ctx->fw_sg_list.list_len = 0;
378 kfree(ctx->fw_in_mem);
379 ctx->fw_in_mem = NULL;
380 sst_memcpy_free_resources(ctx);
383 EXPORT_SYMBOL_GPL(sst_context_cleanup);
385 void sst_configure_runtime_pm(struct intel_sst_drv *ctx)
387 pm_runtime_set_autosuspend_delay(ctx->dev, SST_SUSPEND_DELAY);
388 pm_runtime_use_autosuspend(ctx->dev);
390 * For acpi devices, the actual physical device state is
391 * initially active. So change the state to active before
396 pm_runtime_set_active(ctx->dev);
398 pm_runtime_enable(ctx->dev);
401 pm_runtime_set_active(ctx->dev);
403 pm_runtime_put_noidle(ctx->dev);
405 EXPORT_SYMBOL_GPL(sst_configure_runtime_pm);
407 static int intel_sst_runtime_suspend(struct device *dev)
410 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
412 if (ctx->sst_state == SST_RESET) {
413 dev_dbg(dev, "LPE is already in RESET state, No action\n");
416 /* save fw context */
417 if (ctx->ops->save_dsp_context(ctx))
420 /* Move the SST state to Reset */
421 sst_set_fw_state_locked(ctx, SST_RESET);
423 synchronize_irq(ctx->irq_num);
424 flush_workqueue(ctx->post_msg_wq);
426 ctx->ops->reset(ctx);
431 static int intel_sst_suspend(struct device *dev)
433 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
434 struct sst_fw_save *fw_save;
437 /* check first if we are already in SW reset */
438 if (ctx->sst_state == SST_RESET)
442 * check if any stream is active and running
443 * they should already by suspend by soc_suspend
445 for (i = 1; i <= ctx->info.max_streams; i++) {
446 struct stream_info *stream = &ctx->streams[i];
448 if (stream->status == STREAM_RUNNING) {
449 dev_err(dev, "stream %d is running, can't suspend, abort\n", i);
453 if (ctx->pdata->streams_lost_on_suspend) {
454 stream->resume_status = stream->status;
455 stream->resume_prev = stream->prev;
456 if (stream->status != STREAM_UN_INIT)
457 sst_free_stream(ctx, i);
460 synchronize_irq(ctx->irq_num);
461 flush_workqueue(ctx->post_msg_wq);
463 /* Move the SST state to Reset */
464 sst_set_fw_state_locked(ctx, SST_RESET);
466 /* tell DSP we are suspending */
467 if (ctx->ops->save_dsp_context(ctx))
470 /* save the memories */
471 fw_save = kzalloc(sizeof(*fw_save), GFP_KERNEL);
474 fw_save->iram = kvzalloc(ctx->iram_end - ctx->iram_base, GFP_KERNEL);
475 if (!fw_save->iram) {
479 fw_save->dram = kvzalloc(ctx->dram_end - ctx->dram_base, GFP_KERNEL);
480 if (!fw_save->dram) {
484 fw_save->sram = kvzalloc(SST_MAILBOX_SIZE, GFP_KERNEL);
485 if (!fw_save->sram) {
490 fw_save->ddr = kvzalloc(ctx->ddr_end - ctx->ddr_base, GFP_KERNEL);
496 memcpy32_fromio(fw_save->iram, ctx->iram, ctx->iram_end - ctx->iram_base);
497 memcpy32_fromio(fw_save->dram, ctx->dram, ctx->dram_end - ctx->dram_base);
498 memcpy32_fromio(fw_save->sram, ctx->mailbox, SST_MAILBOX_SIZE);
499 memcpy32_fromio(fw_save->ddr, ctx->ddr, ctx->ddr_end - ctx->ddr_base);
501 ctx->fw_save = fw_save;
502 ctx->ops->reset(ctx);
505 kvfree(fw_save->sram);
507 kvfree(fw_save->dram);
509 kvfree(fw_save->iram);
515 static int intel_sst_resume(struct device *dev)
517 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
518 struct sst_fw_save *fw_save = ctx->fw_save;
519 struct sst_block *block;
525 sst_set_fw_state_locked(ctx, SST_FW_LOADING);
527 /* we have to restore the memory saved */
528 ctx->ops->reset(ctx);
532 memcpy32_toio(ctx->iram, fw_save->iram, ctx->iram_end - ctx->iram_base);
533 memcpy32_toio(ctx->dram, fw_save->dram, ctx->dram_end - ctx->dram_base);
534 memcpy32_toio(ctx->mailbox, fw_save->sram, SST_MAILBOX_SIZE);
535 memcpy32_toio(ctx->ddr, fw_save->ddr, ctx->ddr_end - ctx->ddr_base);
537 kvfree(fw_save->sram);
538 kvfree(fw_save->dram);
539 kvfree(fw_save->iram);
540 kvfree(fw_save->ddr);
543 block = sst_create_block(ctx, 0, FW_DWNL_ID);
548 /* start and wait for ack */
549 ctx->ops->start(ctx);
550 ret = sst_wait_timeout(ctx, block);
552 dev_err(ctx->dev, "fw download failed %d\n", ret);
553 /* FW download failed due to timeout */
557 sst_set_fw_state_locked(ctx, SST_FW_RUNNING);
560 if (ctx->pdata->streams_lost_on_suspend) {
561 for (i = 1; i <= ctx->info.max_streams; i++) {
562 struct stream_info *stream = &ctx->streams[i];
564 if (stream->resume_status != STREAM_UN_INIT) {
565 dev_dbg(ctx->dev, "Re-allocing stream %d status %d prev %d\n",
566 i, stream->resume_status,
567 stream->resume_prev);
568 sst_realloc_stream(ctx, i);
569 stream->status = stream->resume_status;
570 stream->prev = stream->resume_prev;
575 sst_free_block(ctx, block);
579 const struct dev_pm_ops intel_sst_pm = {
580 .suspend = intel_sst_suspend,
581 .resume = intel_sst_resume,
582 .runtime_suspend = intel_sst_runtime_suspend,
584 EXPORT_SYMBOL_GPL(intel_sst_pm);