2 * bxt-sst.c - DSP library functions for BXT platform
4 * Copyright (C) 2015-16 Intel Corp
5 * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
6 * Jeeja KP <jeeja.kp@intel.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/firmware.h>
21 #include <linux/device.h>
23 #include "../common/sst-dsp.h"
24 #include "../common/sst-dsp-priv.h"
25 #include "skl-sst-ipc.h"
27 #define BXT_BASEFW_TIMEOUT 3000
28 #define BXT_INIT_TIMEOUT 500
29 #define BXT_IPC_PURGE_FW 0x01004000
31 #define BXT_ROM_INIT 0x5
32 #define BXT_ADSP_SRAM0_BASE 0x80000
34 /* Firmware status window */
35 #define BXT_ADSP_FW_STATUS BXT_ADSP_SRAM0_BASE
36 #define BXT_ADSP_ERROR_CODE (BXT_ADSP_FW_STATUS + 0x4)
38 #define BXT_ADSP_SRAM1_BASE 0xA0000
40 #define BXT_INSTANCE_ID 0
41 #define BXT_BASE_FW_MODULE_ID 0
43 #define BXT_ADSP_FW_BIN_HDR_OFFSET 0x2000
45 /* Delay before scheduling D0i3 entry */
46 #define BXT_D0I3_DELAY 5000
48 static unsigned int bxt_get_errorcode(struct sst_dsp *ctx)
50 return sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE);
53 static void sst_bxt_release_library(struct skl_lib_info *linfo, int lib_count)
57 for (i = 1; i < lib_count; i++) {
59 release_firmware(linfo[i].fw);
66 bxt_load_library(struct sst_dsp *ctx, struct skl_lib_info *linfo, int lib_count)
68 struct snd_dma_buffer dmab;
69 struct skl_sst *skl = ctx->thread_context;
70 struct firmware stripped_fw;
71 int ret = 0, i, dma_id, stream_tag;
73 /* library indices start from 1 to N. 0 represents base FW */
74 for (i = 1; i < lib_count; i++) {
75 if (linfo[i].fw == NULL) {
76 ret = request_firmware(&linfo[i].fw, linfo[i].name,
79 dev_err(ctx->dev, "Request lib %s failed:%d\n",
81 goto load_library_failed;
85 if (skl->is_first_boot) {
86 ret = snd_skl_parse_uuids(ctx, linfo[i].fw,
87 BXT_ADSP_FW_BIN_HDR_OFFSET, i);
89 goto load_library_failed;
92 stripped_fw.data = linfo[i].fw->data;
93 stripped_fw.size = linfo[i].fw->size;
94 skl_dsp_strip_extended_manifest(&stripped_fw);
96 stream_tag = ctx->dsp_ops.prepare(ctx->dev, 0x40,
97 stripped_fw.size, &dmab);
98 if (stream_tag <= 0) {
99 dev_err(ctx->dev, "Lib prepare DMA err: %x\n",
102 goto load_library_failed;
105 dma_id = stream_tag - 1;
106 memcpy(dmab.area, stripped_fw.data, stripped_fw.size);
108 ctx->dsp_ops.trigger(ctx->dev, true, stream_tag);
109 ret = skl_sst_ipc_load_library(&skl->ipc, dma_id, i);
111 dev_err(ctx->dev, "IPC Load Lib for %s fail: %d\n",
114 ctx->dsp_ops.trigger(ctx->dev, false, stream_tag);
115 ctx->dsp_ops.cleanup(ctx->dev, &dmab, stream_tag);
121 sst_bxt_release_library(linfo, lib_count);
126 * First boot sequence has some extra steps. Core 0 waits for power
127 * status on core 1, so power up core 1 also momentarily, keep it in
128 * reset/stall and then turn it off
130 static int sst_bxt_prepare_fw(struct sst_dsp *ctx,
131 const void *fwdata, u32 fwsize)
135 stream_tag = ctx->dsp_ops.prepare(ctx->dev, 0x40, fwsize, &ctx->dmab);
136 if (stream_tag <= 0) {
137 dev_err(ctx->dev, "Failed to prepare DMA FW loading err: %x\n",
142 ctx->dsp_ops.stream_tag = stream_tag;
143 memcpy(ctx->dmab.area, fwdata, fwsize);
145 /* Step 1: Power up core 0 and core1 */
146 ret = skl_dsp_core_power_up(ctx, SKL_DSP_CORE0_MASK |
147 SKL_DSP_CORE_MASK(1));
149 dev_err(ctx->dev, "dsp core0/1 power up failed\n");
150 goto base_fw_load_failed;
153 /* Step 2: Purge FW request */
154 sst_dsp_shim_write(ctx, SKL_ADSP_REG_HIPCI, SKL_ADSP_REG_HIPCI_BUSY |
155 (BXT_IPC_PURGE_FW | ((stream_tag - 1) << 9)));
157 /* Step 3: Unset core0 reset state & unstall/run core0 */
158 ret = skl_dsp_start_core(ctx, SKL_DSP_CORE0_MASK);
160 dev_err(ctx->dev, "Start dsp core failed ret: %d\n", ret);
162 goto base_fw_load_failed;
165 /* Step 4: Wait for DONE Bit */
166 ret = sst_dsp_register_poll(ctx, SKL_ADSP_REG_HIPCIE,
167 SKL_ADSP_REG_HIPCIE_DONE,
168 SKL_ADSP_REG_HIPCIE_DONE,
169 BXT_INIT_TIMEOUT, "HIPCIE Done");
171 dev_err(ctx->dev, "Timout for Purge Request%d\n", ret);
172 goto base_fw_load_failed;
175 /* Step 5: power down core1 */
176 ret = skl_dsp_core_power_down(ctx, SKL_DSP_CORE_MASK(1));
178 dev_err(ctx->dev, "dsp core1 power down failed\n");
179 goto base_fw_load_failed;
182 /* Step 6: Enable Interrupt */
183 skl_ipc_int_enable(ctx);
184 skl_ipc_op_int_enable(ctx);
186 /* Step 7: Wait for ROM init */
187 ret = sst_dsp_register_poll(ctx, BXT_ADSP_FW_STATUS, SKL_FW_STS_MASK,
188 SKL_FW_INIT, BXT_INIT_TIMEOUT, "ROM Load");
190 dev_err(ctx->dev, "Timeout for ROM init, ret:%d\n", ret);
191 goto base_fw_load_failed;
197 ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, stream_tag);
198 skl_dsp_core_power_down(ctx, SKL_DSP_CORE_MASK(1));
199 skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
203 static int sst_transfer_fw_host_dma(struct sst_dsp *ctx)
207 ctx->dsp_ops.trigger(ctx->dev, true, ctx->dsp_ops.stream_tag);
208 ret = sst_dsp_register_poll(ctx, BXT_ADSP_FW_STATUS, SKL_FW_STS_MASK,
209 BXT_ROM_INIT, BXT_BASEFW_TIMEOUT, "Firmware boot");
211 ctx->dsp_ops.trigger(ctx->dev, false, ctx->dsp_ops.stream_tag);
212 ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, ctx->dsp_ops.stream_tag);
217 static int bxt_load_base_firmware(struct sst_dsp *ctx)
219 struct firmware stripped_fw;
220 struct skl_sst *skl = ctx->thread_context;
223 if (ctx->fw == NULL) {
224 ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
226 dev_err(ctx->dev, "Request firmware failed %d\n", ret);
231 /* prase uuids on first boot */
232 if (skl->is_first_boot) {
233 ret = snd_skl_parse_uuids(ctx, ctx->fw, BXT_ADSP_FW_BIN_HDR_OFFSET, 0);
235 goto sst_load_base_firmware_failed;
238 stripped_fw.data = ctx->fw->data;
239 stripped_fw.size = ctx->fw->size;
240 skl_dsp_strip_extended_manifest(&stripped_fw);
242 ret = sst_bxt_prepare_fw(ctx, stripped_fw.data, stripped_fw.size);
243 /* Retry Enabling core and ROM load. Retry seemed to help */
245 ret = sst_bxt_prepare_fw(ctx, stripped_fw.data, stripped_fw.size);
247 dev_err(ctx->dev, "Error code=0x%x: FW status=0x%x\n",
248 sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE),
249 sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS));
251 dev_err(ctx->dev, "Core En/ROM load fail:%d\n", ret);
252 goto sst_load_base_firmware_failed;
256 ret = sst_transfer_fw_host_dma(ctx);
258 dev_err(ctx->dev, "Transfer firmware failed %d\n", ret);
259 dev_info(ctx->dev, "Error code=0x%x: FW status=0x%x\n",
260 sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE),
261 sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS));
263 skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
265 dev_dbg(ctx->dev, "Firmware download successful\n");
266 ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
267 msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
269 dev_err(ctx->dev, "DSP boot fail, FW Ready timeout\n");
270 skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
274 skl->fw_loaded = true;
280 sst_load_base_firmware_failed:
281 release_firmware(ctx->fw);
287 * Decide the D0i3 state that can be targeted based on the usecase
288 * ref counts and DSP state
290 * Decision Matrix: (X= dont care; state = target state)
292 * DSP state != SKL_DSP_RUNNING ; state = no d0i3
294 * DSP state == SKL_DSP_RUNNING , the following matrix applies
295 * non_d0i3 >0; streaming =X; non_streaming =X; state = no d0i3
296 * non_d0i3 =X; streaming =0; non_streaming =0; state = no d0i3
297 * non_d0i3 =0; streaming >0; non_streaming =X; state = streaming d0i3
298 * non_d0i3 =0; streaming =0; non_streaming =X; state = non-streaming d0i3
300 static int bxt_d0i3_target_state(struct sst_dsp *ctx)
302 struct skl_sst *skl = ctx->thread_context;
303 struct skl_d0i3_data *d0i3 = &skl->d0i3;
305 if (skl->cores.state[SKL_DSP_CORE0_ID] != SKL_DSP_RUNNING)
306 return SKL_DSP_D0I3_NONE;
309 return SKL_DSP_D0I3_NONE;
310 else if (d0i3->streaming)
311 return SKL_DSP_D0I3_STREAMING;
312 else if (d0i3->non_streaming)
313 return SKL_DSP_D0I3_NON_STREAMING;
315 return SKL_DSP_D0I3_NONE;
318 static void bxt_set_dsp_D0i3(struct work_struct *work)
321 struct skl_ipc_d0ix_msg msg;
322 struct skl_sst *skl = container_of(work,
323 struct skl_sst, d0i3.work.work);
324 struct sst_dsp *ctx = skl->dsp;
325 struct skl_d0i3_data *d0i3 = &skl->d0i3;
328 dev_dbg(ctx->dev, "In %s:\n", __func__);
330 /* D0i3 entry allowed only if core 0 alone is running */
331 if (skl_dsp_get_enabled_cores(ctx) != SKL_DSP_CORE0_MASK) {
333 "D0i3 allowed when only core0 running:Exit\n");
337 target_state = bxt_d0i3_target_state(ctx);
338 if (target_state == SKL_DSP_D0I3_NONE)
345 if (target_state == SKL_DSP_D0I3_STREAMING)
348 ret = skl_ipc_set_d0ix(&skl->ipc, &msg);
351 dev_err(ctx->dev, "Failed to set DSP to D0i3 state\n");
355 /* Set Vendor specific register D0I3C.I3 to enable D0i3*/
356 if (skl->update_d0i3c)
357 skl->update_d0i3c(skl->dev, true);
359 d0i3->state = target_state;
360 skl->cores.state[SKL_DSP_CORE0_ID] = SKL_DSP_RUNNING_D0I3;
363 static int bxt_schedule_dsp_D0i3(struct sst_dsp *ctx)
365 struct skl_sst *skl = ctx->thread_context;
366 struct skl_d0i3_data *d0i3 = &skl->d0i3;
368 /* Schedule D0i3 only if the usecase ref counts are appropriate */
369 if (bxt_d0i3_target_state(ctx) != SKL_DSP_D0I3_NONE) {
371 dev_dbg(ctx->dev, "%s: Schedule D0i3\n", __func__);
373 schedule_delayed_work(&d0i3->work,
374 msecs_to_jiffies(BXT_D0I3_DELAY));
380 static int bxt_set_dsp_D0i0(struct sst_dsp *ctx)
383 struct skl_ipc_d0ix_msg msg;
384 struct skl_sst *skl = ctx->thread_context;
386 dev_dbg(ctx->dev, "In %s:\n", __func__);
388 /* First Cancel any pending attempt to put DSP to D0i3 */
389 cancel_delayed_work_sync(&skl->d0i3.work);
391 /* If DSP is currently in D0i3, bring it to D0i0 */
392 if (skl->cores.state[SKL_DSP_CORE0_ID] != SKL_DSP_RUNNING_D0I3)
395 dev_dbg(ctx->dev, "Set DSP to D0i0\n");
402 if (skl->d0i3.state == SKL_DSP_D0I3_STREAMING)
405 /* Clear Vendor specific register D0I3C.I3 to disable D0i3*/
406 if (skl->update_d0i3c)
407 skl->update_d0i3c(skl->dev, false);
409 ret = skl_ipc_set_d0ix(&skl->ipc, &msg);
411 dev_err(ctx->dev, "Failed to set DSP to D0i0\n");
415 skl->cores.state[SKL_DSP_CORE0_ID] = SKL_DSP_RUNNING;
416 skl->d0i3.state = SKL_DSP_D0I3_NONE;
421 static int bxt_set_dsp_D0(struct sst_dsp *ctx, unsigned int core_id)
423 struct skl_sst *skl = ctx->thread_context;
425 struct skl_ipc_dxstate_info dx;
426 unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
428 if (skl->fw_loaded == false) {
429 skl->boot_complete = false;
430 ret = bxt_load_base_firmware(ctx);
432 dev_err(ctx->dev, "reload fw failed: %d\n", ret);
436 if (skl->lib_count > 1) {
437 ret = bxt_load_library(ctx, skl->lib_info,
440 dev_err(ctx->dev, "reload libs failed: %d\n", ret);
447 /* If core 0 is being turned on, turn on core 1 as well */
448 if (core_id == SKL_DSP_CORE0_ID)
449 ret = skl_dsp_core_power_up(ctx, core_mask |
450 SKL_DSP_CORE_MASK(1));
452 ret = skl_dsp_core_power_up(ctx, core_mask);
457 if (core_id == SKL_DSP_CORE0_ID) {
460 * Enable interrupt after SPA is set and before
463 skl_ipc_int_enable(ctx);
464 skl_ipc_op_int_enable(ctx);
465 skl->boot_complete = false;
468 ret = skl_dsp_start_core(ctx, core_mask);
472 if (core_id == SKL_DSP_CORE0_ID) {
473 ret = wait_event_timeout(skl->boot_wait,
475 msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
477 /* If core 1 was turned on for booting core 0, turn it off */
478 skl_dsp_core_power_down(ctx, SKL_DSP_CORE_MASK(1));
480 dev_err(ctx->dev, "%s: DSP boot timeout\n", __func__);
481 dev_err(ctx->dev, "Error code=0x%x: FW status=0x%x\n",
482 sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE),
483 sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS));
484 dev_err(ctx->dev, "Failed to set core0 to D0 state\n");
490 /* Tell FW if additional core in now On */
492 if (core_id != SKL_DSP_CORE0_ID) {
493 dx.core_mask = core_mask;
494 dx.dx_mask = core_mask;
496 ret = skl_ipc_set_dx(&skl->ipc, BXT_INSTANCE_ID,
497 BXT_BASE_FW_MODULE_ID, &dx);
499 dev_err(ctx->dev, "IPC set_dx for core %d fail: %d\n",
505 skl->cores.state[core_id] = SKL_DSP_RUNNING;
508 if (core_id == SKL_DSP_CORE0_ID)
509 core_mask |= SKL_DSP_CORE_MASK(1);
510 skl_dsp_disable_core(ctx, core_mask);
515 static int bxt_set_dsp_D3(struct sst_dsp *ctx, unsigned int core_id)
518 struct skl_ipc_dxstate_info dx;
519 struct skl_sst *skl = ctx->thread_context;
520 unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
522 dx.core_mask = core_mask;
523 dx.dx_mask = SKL_IPC_D3_MASK;
525 dev_dbg(ctx->dev, "core mask=%x dx_mask=%x\n",
526 dx.core_mask, dx.dx_mask);
528 ret = skl_ipc_set_dx(&skl->ipc, BXT_INSTANCE_ID,
529 BXT_BASE_FW_MODULE_ID, &dx);
532 "Failed to set DSP to D3:core id = %d;Continue reset\n",
535 ret = skl_dsp_disable_core(ctx, core_mask);
537 dev_err(ctx->dev, "Failed to disable core %d\n", ret);
540 skl->cores.state[core_id] = SKL_DSP_RESET;
544 static struct skl_dsp_fw_ops bxt_fw_ops = {
545 .set_state_D0 = bxt_set_dsp_D0,
546 .set_state_D3 = bxt_set_dsp_D3,
547 .set_state_D0i3 = bxt_schedule_dsp_D0i3,
548 .set_state_D0i0 = bxt_set_dsp_D0i0,
549 .load_fw = bxt_load_base_firmware,
550 .get_fw_errcode = bxt_get_errorcode,
551 .load_library = bxt_load_library,
554 static struct sst_ops skl_ops = {
555 .irq_handler = skl_dsp_sst_interrupt,
556 .write = sst_shim32_write,
557 .read = sst_shim32_read,
558 .ram_read = sst_memcpy_fromio_32,
559 .ram_write = sst_memcpy_toio_32,
560 .free = skl_dsp_free,
563 static struct sst_dsp_device skl_dev = {
564 .thread = skl_dsp_irq_thread_handler,
568 int bxt_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
569 const char *fw_name, struct skl_dsp_loader_ops dsp_ops,
570 struct skl_sst **dsp)
576 skl = devm_kzalloc(dev, sizeof(*skl), GFP_KERNEL);
581 skl_dev.thread_context = skl;
582 INIT_LIST_HEAD(&skl->uuid_list);
584 skl->dsp = skl_dsp_ctx_init(dev, &skl_dev, irq);
586 dev_err(skl->dev, "skl_dsp_ctx_init failed\n");
591 sst->fw_name = fw_name;
592 sst->dsp_ops = dsp_ops;
593 sst->fw_ops = bxt_fw_ops;
594 sst->addr.lpe = mmio_base;
595 sst->addr.shim = mmio_base;
597 sst_dsp_mailbox_init(sst, (BXT_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
598 SKL_ADSP_W0_UP_SZ, BXT_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
600 INIT_LIST_HEAD(&sst->module_list);
601 ret = skl_ipc_init(dev, skl);
605 /* set the D0i3 check */
606 skl->ipc.ops.check_dsp_lp_on = skl_ipc_check_D0i0;
608 skl->cores.count = 2;
609 skl->boot_complete = false;
610 init_waitqueue_head(&skl->boot_wait);
611 skl->is_first_boot = true;
612 INIT_DELAYED_WORK(&skl->d0i3.work, bxt_set_dsp_D0i3);
613 skl->d0i3.state = SKL_DSP_D0I3_NONE;
620 EXPORT_SYMBOL_GPL(bxt_sst_dsp_init);
622 int bxt_sst_init_fw(struct device *dev, struct skl_sst *ctx)
625 struct sst_dsp *sst = ctx->dsp;
627 ret = sst->fw_ops.load_fw(sst);
629 dev_err(dev, "Load base fw failed: %x\n", ret);
633 skl_dsp_init_core_state(sst);
635 if (ctx->lib_count > 1) {
636 ret = sst->fw_ops.load_library(sst, ctx->lib_info,
639 dev_err(dev, "Load Library failed : %x\n", ret);
643 ctx->is_first_boot = false;
647 EXPORT_SYMBOL_GPL(bxt_sst_init_fw);
649 void bxt_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
652 sst_bxt_release_library(ctx->lib_info, ctx->lib_count);
654 release_firmware(ctx->dsp->fw);
655 skl_freeup_uuid_list(ctx);
656 skl_ipc_free(&ctx->ipc);
657 ctx->dsp->cl_dev.ops.cl_cleanup_controller(ctx->dsp);
659 if (ctx->dsp->addr.lpe)
660 iounmap(ctx->dsp->addr.lpe);
662 ctx->dsp->ops->free(ctx->dsp);
664 EXPORT_SYMBOL_GPL(bxt_sst_dsp_cleanup);
666 MODULE_LICENSE("GPL v2");
667 MODULE_DESCRIPTION("Intel Broxton IPC driver");