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"
26 #include "skl-tplg-interface.h"
28 #define BXT_BASEFW_TIMEOUT 3000
29 #define BXT_INIT_TIMEOUT 500
30 #define BXT_IPC_PURGE_FW 0x01004000
32 #define BXT_ROM_INIT 0x5
33 #define BXT_ADSP_SRAM0_BASE 0x80000
35 /* Firmware status window */
36 #define BXT_ADSP_FW_STATUS BXT_ADSP_SRAM0_BASE
37 #define BXT_ADSP_ERROR_CODE (BXT_ADSP_FW_STATUS + 0x4)
39 #define BXT_ADSP_SRAM1_BASE 0xA0000
41 #define BXT_INSTANCE_ID 0
42 #define BXT_BASE_FW_MODULE_ID 0
44 #define BXT_ADSP_FW_BIN_HDR_OFFSET 0x2000
46 /* Delay before scheduling D0i3 entry */
47 #define BXT_D0I3_DELAY 5000
49 static unsigned int bxt_get_errorcode(struct sst_dsp
*ctx
)
51 return sst_dsp_shim_read(ctx
, BXT_ADSP_ERROR_CODE
);
55 bxt_load_library(struct sst_dsp
*ctx
, struct skl_dfw_manifest
*minfo
)
57 struct snd_dma_buffer dmab
;
58 struct skl_sst
*skl
= ctx
->thread_context
;
59 const struct firmware
*fw
= NULL
;
60 struct firmware stripped_fw
;
61 int ret
= 0, i
, dma_id
, stream_tag
;
63 /* library indices start from 1 to N. 0 represents base FW */
64 for (i
= 1; i
< minfo
->lib_count
; i
++) {
65 ret
= request_firmware(&fw
, minfo
->lib
[i
].name
, ctx
->dev
);
67 dev_err(ctx
->dev
, "Request lib %s failed:%d\n",
68 minfo
->lib
[i
].name
, ret
);
72 if (skl
->is_first_boot
) {
73 ret
= snd_skl_parse_uuids(ctx
, fw
,
74 BXT_ADSP_FW_BIN_HDR_OFFSET
, i
);
76 goto load_library_failed
;
79 stripped_fw
.data
= fw
->data
;
80 stripped_fw
.size
= fw
->size
;
81 skl_dsp_strip_extended_manifest(&stripped_fw
);
83 stream_tag
= ctx
->dsp_ops
.prepare(ctx
->dev
, 0x40,
84 stripped_fw
.size
, &dmab
);
85 if (stream_tag
<= 0) {
86 dev_err(ctx
->dev
, "Lib prepare DMA err: %x\n",
89 goto load_library_failed
;
92 dma_id
= stream_tag
- 1;
93 memcpy(dmab
.area
, stripped_fw
.data
, stripped_fw
.size
);
95 ctx
->dsp_ops
.trigger(ctx
->dev
, true, stream_tag
);
96 ret
= skl_sst_ipc_load_library(&skl
->ipc
, dma_id
, i
);
98 dev_err(ctx
->dev
, "IPC Load Lib for %s fail: %d\n",
99 minfo
->lib
[i
].name
, ret
);
101 ctx
->dsp_ops
.trigger(ctx
->dev
, false, stream_tag
);
102 ctx
->dsp_ops
.cleanup(ctx
->dev
, &dmab
, stream_tag
);
103 release_firmware(fw
);
110 release_firmware(fw
);
115 * First boot sequence has some extra steps. Core 0 waits for power
116 * status on core 1, so power up core 1 also momentarily, keep it in
117 * reset/stall and then turn it off
119 static int sst_bxt_prepare_fw(struct sst_dsp
*ctx
,
120 const void *fwdata
, u32 fwsize
)
122 int stream_tag
, ret
, i
;
125 stream_tag
= ctx
->dsp_ops
.prepare(ctx
->dev
, 0x40, fwsize
, &ctx
->dmab
);
126 if (stream_tag
<= 0) {
127 dev_err(ctx
->dev
, "Failed to prepare DMA FW loading err: %x\n",
132 ctx
->dsp_ops
.stream_tag
= stream_tag
;
133 memcpy(ctx
->dmab
.area
, fwdata
, fwsize
);
135 /* Step 1: Power up core 0 and core1 */
136 ret
= skl_dsp_core_power_up(ctx
, SKL_DSP_CORE0_MASK
|
137 SKL_DSP_CORE_MASK(1));
139 dev_err(ctx
->dev
, "dsp core0/1 power up failed\n");
140 goto base_fw_load_failed
;
143 /* Step 2: Purge FW request */
144 sst_dsp_shim_write(ctx
, SKL_ADSP_REG_HIPCI
, SKL_ADSP_REG_HIPCI_BUSY
|
145 (BXT_IPC_PURGE_FW
| ((stream_tag
- 1) << 9)));
147 /* Step 3: Unset core0 reset state & unstall/run core0 */
148 ret
= skl_dsp_start_core(ctx
, SKL_DSP_CORE0_MASK
);
150 dev_err(ctx
->dev
, "Start dsp core failed ret: %d\n", ret
);
152 goto base_fw_load_failed
;
155 /* Step 4: Wait for DONE Bit */
156 for (i
= BXT_INIT_TIMEOUT
; i
> 0; --i
) {
157 reg
= sst_dsp_shim_read(ctx
, SKL_ADSP_REG_HIPCIE
);
159 if (reg
& SKL_ADSP_REG_HIPCIE_DONE
) {
160 sst_dsp_shim_update_bits_forced(ctx
,
162 SKL_ADSP_REG_HIPCIE_DONE
,
163 SKL_ADSP_REG_HIPCIE_DONE
);
169 dev_info(ctx
->dev
, "Waiting for HIPCIE done, reg: 0x%x\n", reg
);
170 sst_dsp_shim_update_bits(ctx
, SKL_ADSP_REG_HIPCIE
,
171 SKL_ADSP_REG_HIPCIE_DONE
,
172 SKL_ADSP_REG_HIPCIE_DONE
);
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 for (i
= BXT_INIT_TIMEOUT
; i
> 0; --i
) {
189 (sst_dsp_shim_read(ctx
, BXT_ADSP_FW_STATUS
) &
192 dev_info(ctx
->dev
, "ROM loaded, continue FW loading\n");
198 dev_err(ctx
->dev
, "Timeout for ROM init, HIPCIE: 0x%x\n", reg
);
200 goto base_fw_load_failed
;
206 ctx
->dsp_ops
.cleanup(ctx
->dev
, &ctx
->dmab
, stream_tag
);
207 skl_dsp_core_power_down(ctx
, SKL_DSP_CORE_MASK(1));
208 skl_dsp_disable_core(ctx
, SKL_DSP_CORE0_MASK
);
212 static int sst_transfer_fw_host_dma(struct sst_dsp
*ctx
)
216 ctx
->dsp_ops
.trigger(ctx
->dev
, true, ctx
->dsp_ops
.stream_tag
);
217 ret
= sst_dsp_register_poll(ctx
, BXT_ADSP_FW_STATUS
, SKL_FW_STS_MASK
,
218 BXT_ROM_INIT
, BXT_BASEFW_TIMEOUT
, "Firmware boot");
220 ctx
->dsp_ops
.trigger(ctx
->dev
, false, ctx
->dsp_ops
.stream_tag
);
221 ctx
->dsp_ops
.cleanup(ctx
->dev
, &ctx
->dmab
, ctx
->dsp_ops
.stream_tag
);
226 static int bxt_load_base_firmware(struct sst_dsp
*ctx
)
228 struct firmware stripped_fw
;
229 struct skl_sst
*skl
= ctx
->thread_context
;
232 ret
= request_firmware(&ctx
->fw
, ctx
->fw_name
, ctx
->dev
);
234 dev_err(ctx
->dev
, "Request firmware failed %d\n", ret
);
235 goto sst_load_base_firmware_failed
;
238 /* check for extended manifest */
240 goto sst_load_base_firmware_failed
;
242 /* prase uuids on first boot */
243 if (skl
->is_first_boot
) {
244 ret
= snd_skl_parse_uuids(ctx
, ctx
->fw
, BXT_ADSP_FW_BIN_HDR_OFFSET
, 0);
246 goto sst_load_base_firmware_failed
;
249 stripped_fw
.data
= ctx
->fw
->data
;
250 stripped_fw
.size
= ctx
->fw
->size
;
251 skl_dsp_strip_extended_manifest(&stripped_fw
);
253 ret
= sst_bxt_prepare_fw(ctx
, stripped_fw
.data
, stripped_fw
.size
);
254 /* Retry Enabling core and ROM load. Retry seemed to help */
256 ret
= sst_bxt_prepare_fw(ctx
, stripped_fw
.data
, stripped_fw
.size
);
258 dev_err(ctx
->dev
, "Error code=0x%x: FW status=0x%x\n",
259 sst_dsp_shim_read(ctx
, BXT_ADSP_ERROR_CODE
),
260 sst_dsp_shim_read(ctx
, BXT_ADSP_FW_STATUS
));
262 dev_err(ctx
->dev
, "Core En/ROM load fail:%d\n", ret
);
263 goto sst_load_base_firmware_failed
;
267 ret
= sst_transfer_fw_host_dma(ctx
);
269 dev_err(ctx
->dev
, "Transfer firmware failed %d\n", ret
);
270 dev_info(ctx
->dev
, "Error code=0x%x: FW status=0x%x\n",
271 sst_dsp_shim_read(ctx
, BXT_ADSP_ERROR_CODE
),
272 sst_dsp_shim_read(ctx
, BXT_ADSP_FW_STATUS
));
274 skl_dsp_disable_core(ctx
, SKL_DSP_CORE0_MASK
);
276 dev_dbg(ctx
->dev
, "Firmware download successful\n");
277 ret
= wait_event_timeout(skl
->boot_wait
, skl
->boot_complete
,
278 msecs_to_jiffies(SKL_IPC_BOOT_MSECS
));
280 dev_err(ctx
->dev
, "DSP boot fail, FW Ready timeout\n");
281 skl_dsp_disable_core(ctx
, SKL_DSP_CORE0_MASK
);
285 skl
->fw_loaded
= true;
289 sst_load_base_firmware_failed
:
290 release_firmware(ctx
->fw
);
295 * Decide the D0i3 state that can be targeted based on the usecase
296 * ref counts and DSP state
298 * Decision Matrix: (X= dont care; state = target state)
300 * DSP state != SKL_DSP_RUNNING ; state = no d0i3
302 * DSP state == SKL_DSP_RUNNING , the following matrix applies
303 * non_d0i3 >0; streaming =X; non_streaming =X; state = no d0i3
304 * non_d0i3 =X; streaming =0; non_streaming =0; state = no d0i3
305 * non_d0i3 =0; streaming >0; non_streaming =X; state = streaming d0i3
306 * non_d0i3 =0; streaming =0; non_streaming =X; state = non-streaming d0i3
308 static int bxt_d0i3_target_state(struct sst_dsp
*ctx
)
310 struct skl_sst
*skl
= ctx
->thread_context
;
311 struct skl_d0i3_data
*d0i3
= &skl
->d0i3
;
313 if (skl
->cores
.state
[SKL_DSP_CORE0_ID
] != SKL_DSP_RUNNING
)
314 return SKL_DSP_D0I3_NONE
;
317 return SKL_DSP_D0I3_NONE
;
318 else if (d0i3
->streaming
)
319 return SKL_DSP_D0I3_STREAMING
;
320 else if (d0i3
->non_streaming
)
321 return SKL_DSP_D0I3_NON_STREAMING
;
323 return SKL_DSP_D0I3_NONE
;
326 static void bxt_set_dsp_D0i3(struct work_struct
*work
)
329 struct skl_ipc_d0ix_msg msg
;
330 struct skl_sst
*skl
= container_of(work
,
331 struct skl_sst
, d0i3
.work
.work
);
332 struct sst_dsp
*ctx
= skl
->dsp
;
333 struct skl_d0i3_data
*d0i3
= &skl
->d0i3
;
336 dev_dbg(ctx
->dev
, "In %s:\n", __func__
);
338 /* D0i3 entry allowed only if core 0 alone is running */
339 if (skl_dsp_get_enabled_cores(ctx
) != SKL_DSP_CORE0_MASK
) {
341 "D0i3 allowed when only core0 running:Exit\n");
345 target_state
= bxt_d0i3_target_state(ctx
);
346 if (target_state
== SKL_DSP_D0I3_NONE
)
353 if (target_state
== SKL_DSP_D0I3_STREAMING
)
356 ret
= skl_ipc_set_d0ix(&skl
->ipc
, &msg
);
359 dev_err(ctx
->dev
, "Failed to set DSP to D0i3 state\n");
363 /* Set Vendor specific register D0I3C.I3 to enable D0i3*/
364 if (skl
->update_d0i3c
)
365 skl
->update_d0i3c(skl
->dev
, true);
367 d0i3
->state
= target_state
;
368 skl
->cores
.state
[SKL_DSP_CORE0_ID
] = SKL_DSP_RUNNING_D0I3
;
371 static int bxt_schedule_dsp_D0i3(struct sst_dsp
*ctx
)
373 struct skl_sst
*skl
= ctx
->thread_context
;
374 struct skl_d0i3_data
*d0i3
= &skl
->d0i3
;
376 /* Schedule D0i3 only if the usecase ref counts are appropriate */
377 if (bxt_d0i3_target_state(ctx
) != SKL_DSP_D0I3_NONE
) {
379 dev_dbg(ctx
->dev
, "%s: Schedule D0i3\n", __func__
);
381 schedule_delayed_work(&d0i3
->work
,
382 msecs_to_jiffies(BXT_D0I3_DELAY
));
388 static int bxt_set_dsp_D0i0(struct sst_dsp
*ctx
)
391 struct skl_ipc_d0ix_msg msg
;
392 struct skl_sst
*skl
= ctx
->thread_context
;
394 dev_dbg(ctx
->dev
, "In %s:\n", __func__
);
396 /* First Cancel any pending attempt to put DSP to D0i3 */
397 cancel_delayed_work_sync(&skl
->d0i3
.work
);
399 /* If DSP is currently in D0i3, bring it to D0i0 */
400 if (skl
->cores
.state
[SKL_DSP_CORE0_ID
] != SKL_DSP_RUNNING_D0I3
)
403 dev_dbg(ctx
->dev
, "Set DSP to D0i0\n");
410 if (skl
->d0i3
.state
== SKL_DSP_D0I3_STREAMING
)
413 /* Clear Vendor specific register D0I3C.I3 to disable D0i3*/
414 if (skl
->update_d0i3c
)
415 skl
->update_d0i3c(skl
->dev
, false);
417 ret
= skl_ipc_set_d0ix(&skl
->ipc
, &msg
);
419 dev_err(ctx
->dev
, "Failed to set DSP to D0i0\n");
423 skl
->cores
.state
[SKL_DSP_CORE0_ID
] = SKL_DSP_RUNNING
;
424 skl
->d0i3
.state
= SKL_DSP_D0I3_NONE
;
429 static int bxt_set_dsp_D0(struct sst_dsp
*ctx
, unsigned int core_id
)
431 struct skl_sst
*skl
= ctx
->thread_context
;
433 struct skl_ipc_dxstate_info dx
;
434 unsigned int core_mask
= SKL_DSP_CORE_MASK(core_id
);
435 struct skl_dfw_manifest
*minfo
= &skl
->manifest
;
437 if (skl
->fw_loaded
== false) {
438 skl
->boot_complete
= false;
439 ret
= bxt_load_base_firmware(ctx
);
441 dev_err(ctx
->dev
, "reload fw failed: %d\n", ret
);
445 if (minfo
->lib_count
> 1) {
446 ret
= bxt_load_library(ctx
, minfo
);
448 dev_err(ctx
->dev
, "reload libs failed: %d\n", ret
);
455 /* If core 0 is being turned on, turn on core 1 as well */
456 if (core_id
== SKL_DSP_CORE0_ID
)
457 ret
= skl_dsp_core_power_up(ctx
, core_mask
|
458 SKL_DSP_CORE_MASK(1));
460 ret
= skl_dsp_core_power_up(ctx
, core_mask
);
465 if (core_id
== SKL_DSP_CORE0_ID
) {
468 * Enable interrupt after SPA is set and before
471 skl_ipc_int_enable(ctx
);
472 skl_ipc_op_int_enable(ctx
);
473 skl
->boot_complete
= false;
476 ret
= skl_dsp_start_core(ctx
, core_mask
);
480 if (core_id
== SKL_DSP_CORE0_ID
) {
481 ret
= wait_event_timeout(skl
->boot_wait
,
483 msecs_to_jiffies(SKL_IPC_BOOT_MSECS
));
485 /* If core 1 was turned on for booting core 0, turn it off */
486 skl_dsp_core_power_down(ctx
, SKL_DSP_CORE_MASK(1));
488 dev_err(ctx
->dev
, "%s: DSP boot timeout\n", __func__
);
489 dev_err(ctx
->dev
, "Error code=0x%x: FW status=0x%x\n",
490 sst_dsp_shim_read(ctx
, BXT_ADSP_ERROR_CODE
),
491 sst_dsp_shim_read(ctx
, BXT_ADSP_FW_STATUS
));
492 dev_err(ctx
->dev
, "Failed to set core0 to D0 state\n");
498 /* Tell FW if additional core in now On */
500 if (core_id
!= SKL_DSP_CORE0_ID
) {
501 dx
.core_mask
= core_mask
;
502 dx
.dx_mask
= core_mask
;
504 ret
= skl_ipc_set_dx(&skl
->ipc
, BXT_INSTANCE_ID
,
505 BXT_BASE_FW_MODULE_ID
, &dx
);
507 dev_err(ctx
->dev
, "IPC set_dx for core %d fail: %d\n",
513 skl
->cores
.state
[core_id
] = SKL_DSP_RUNNING
;
516 if (core_id
== SKL_DSP_CORE0_ID
)
517 core_mask
|= SKL_DSP_CORE_MASK(1);
518 skl_dsp_disable_core(ctx
, core_mask
);
523 static int bxt_set_dsp_D3(struct sst_dsp
*ctx
, unsigned int core_id
)
526 struct skl_ipc_dxstate_info dx
;
527 struct skl_sst
*skl
= ctx
->thread_context
;
528 unsigned int core_mask
= SKL_DSP_CORE_MASK(core_id
);
530 dx
.core_mask
= core_mask
;
531 dx
.dx_mask
= SKL_IPC_D3_MASK
;
533 dev_dbg(ctx
->dev
, "core mask=%x dx_mask=%x\n",
534 dx
.core_mask
, dx
.dx_mask
);
536 ret
= skl_ipc_set_dx(&skl
->ipc
, BXT_INSTANCE_ID
,
537 BXT_BASE_FW_MODULE_ID
, &dx
);
540 "Failed to set DSP to D3:core id = %d;Continue reset\n",
543 ret
= skl_dsp_disable_core(ctx
, core_mask
);
545 dev_err(ctx
->dev
, "Failed to disable core %d\n", ret
);
548 skl
->cores
.state
[core_id
] = SKL_DSP_RESET
;
552 static struct skl_dsp_fw_ops bxt_fw_ops
= {
553 .set_state_D0
= bxt_set_dsp_D0
,
554 .set_state_D3
= bxt_set_dsp_D3
,
555 .set_state_D0i3
= bxt_schedule_dsp_D0i3
,
556 .set_state_D0i0
= bxt_set_dsp_D0i0
,
557 .load_fw
= bxt_load_base_firmware
,
558 .get_fw_errcode
= bxt_get_errorcode
,
559 .load_library
= bxt_load_library
,
562 static struct sst_ops skl_ops
= {
563 .irq_handler
= skl_dsp_sst_interrupt
,
564 .write
= sst_shim32_write
,
565 .read
= sst_shim32_read
,
566 .ram_read
= sst_memcpy_fromio_32
,
567 .ram_write
= sst_memcpy_toio_32
,
568 .free
= skl_dsp_free
,
571 static struct sst_dsp_device skl_dev
= {
572 .thread
= skl_dsp_irq_thread_handler
,
576 int bxt_sst_dsp_init(struct device
*dev
, void __iomem
*mmio_base
, int irq
,
577 const char *fw_name
, struct skl_dsp_loader_ops dsp_ops
,
578 struct skl_sst
**dsp
)
584 skl
= devm_kzalloc(dev
, sizeof(*skl
), GFP_KERNEL
);
589 skl_dev
.thread_context
= skl
;
590 INIT_LIST_HEAD(&skl
->uuid_list
);
592 skl
->dsp
= skl_dsp_ctx_init(dev
, &skl_dev
, irq
);
594 dev_err(skl
->dev
, "skl_dsp_ctx_init failed\n");
599 sst
->fw_name
= fw_name
;
600 sst
->dsp_ops
= dsp_ops
;
601 sst
->fw_ops
= bxt_fw_ops
;
602 sst
->addr
.lpe
= mmio_base
;
603 sst
->addr
.shim
= mmio_base
;
605 sst_dsp_mailbox_init(sst
, (BXT_ADSP_SRAM0_BASE
+ SKL_ADSP_W0_STAT_SZ
),
606 SKL_ADSP_W0_UP_SZ
, BXT_ADSP_SRAM1_BASE
, SKL_ADSP_W1_SZ
);
608 INIT_LIST_HEAD(&sst
->module_list
);
609 ret
= skl_ipc_init(dev
, skl
);
613 /* set the D0i3 check */
614 skl
->ipc
.ops
.check_dsp_lp_on
= skl_ipc_check_D0i0
;
616 skl
->cores
.count
= 2;
617 skl
->boot_complete
= false;
618 init_waitqueue_head(&skl
->boot_wait
);
619 skl
->is_first_boot
= true;
620 INIT_DELAYED_WORK(&skl
->d0i3
.work
, bxt_set_dsp_D0i3
);
621 skl
->d0i3
.state
= SKL_DSP_D0I3_NONE
;
628 EXPORT_SYMBOL_GPL(bxt_sst_dsp_init
);
630 int bxt_sst_init_fw(struct device
*dev
, struct skl_sst
*ctx
)
633 struct sst_dsp
*sst
= ctx
->dsp
;
635 ret
= sst
->fw_ops
.load_fw(sst
);
637 dev_err(dev
, "Load base fw failed: %x\n", ret
);
641 skl_dsp_init_core_state(sst
);
643 if (ctx
->manifest
.lib_count
> 1) {
644 ret
= sst
->fw_ops
.load_library(sst
, &ctx
->manifest
);
646 dev_err(dev
, "Load Library failed : %x\n", ret
);
650 ctx
->is_first_boot
= false;
654 EXPORT_SYMBOL_GPL(bxt_sst_init_fw
);
656 void bxt_sst_dsp_cleanup(struct device
*dev
, struct skl_sst
*ctx
)
658 skl_freeup_uuid_list(ctx
);
659 skl_ipc_free(&ctx
->ipc
);
660 ctx
->dsp
->cl_dev
.ops
.cl_cleanup_controller(ctx
->dsp
);
662 if (ctx
->dsp
->addr
.lpe
)
663 iounmap(ctx
->dsp
->addr
.lpe
);
665 ctx
->dsp
->ops
->free(ctx
->dsp
);
667 EXPORT_SYMBOL_GPL(bxt_sst_dsp_cleanup
);
669 MODULE_LICENSE("GPL v2");
670 MODULE_DESCRIPTION("Intel Broxton IPC driver");