xref: /openbmc/linux/sound/soc/intel/skylake/skl-sst.c (revision bcc2a2dc3ba8c3a7aed856f840afa6a47e3cb8e0)
147d7195dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a750ba5fSSubhransu S. Prusty /*
3a750ba5fSSubhransu S. Prusty  * skl-sst.c - HDA DSP library functions for SKL platform
4a750ba5fSSubhransu S. Prusty  *
5a750ba5fSSubhransu S. Prusty  * Copyright (C) 2014-15, Intel Corporation.
6a750ba5fSSubhransu S. Prusty  * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
7a750ba5fSSubhransu S. Prusty  *	Jeeja KP <jeeja.kp@intel.com>
8a750ba5fSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9a750ba5fSSubhransu S. Prusty  */
10a750ba5fSSubhransu S. Prusty 
11a750ba5fSSubhransu S. Prusty #include <linux/module.h>
12a750ba5fSSubhransu S. Prusty #include <linux/delay.h>
13a750ba5fSSubhransu S. Prusty #include <linux/device.h>
1453afce2cSJeeja KP #include <linux/err.h>
1509305da9SShreyas NC #include <linux/uuid.h>
16a750ba5fSSubhransu S. Prusty #include "../common/sst-dsp.h"
17a750ba5fSSubhransu S. Prusty #include "../common/sst-dsp-priv.h"
18a750ba5fSSubhransu S. Prusty #include "../common/sst-ipc.h"
19*bcc2a2dcSCezary Rojewski #include "skl.h"
20a750ba5fSSubhransu S. Prusty 
21a750ba5fSSubhransu S. Prusty #define SKL_BASEFW_TIMEOUT	300
22a750ba5fSSubhransu S. Prusty #define SKL_INIT_TIMEOUT	1000
23a750ba5fSSubhransu S. Prusty 
24a750ba5fSSubhransu S. Prusty /* Intel HD Audio SRAM Window 0*/
25a750ba5fSSubhransu S. Prusty #define SKL_ADSP_SRAM0_BASE	0x8000
26a750ba5fSSubhransu S. Prusty 
27a750ba5fSSubhransu S. Prusty /* Firmware status window */
28a750ba5fSSubhransu S. Prusty #define SKL_ADSP_FW_STATUS	SKL_ADSP_SRAM0_BASE
29a750ba5fSSubhransu S. Prusty #define SKL_ADSP_ERROR_CODE	(SKL_ADSP_FW_STATUS + 0x4)
30a750ba5fSSubhransu S. Prusty 
316c5768b3SDharageswari R #define SKL_NUM_MODULES		1
326c5768b3SDharageswari R 
33a750ba5fSSubhransu S. Prusty static bool skl_check_fw_status(struct sst_dsp *ctx, u32 status)
34a750ba5fSSubhransu S. Prusty {
35a750ba5fSSubhransu S. Prusty 	u32 cur_sts;
36a750ba5fSSubhransu S. Prusty 
37a750ba5fSSubhransu S. Prusty 	cur_sts = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS) & SKL_FW_STS_MASK;
38a750ba5fSSubhransu S. Prusty 
39a750ba5fSSubhransu S. Prusty 	return (cur_sts == status);
40a750ba5fSSubhransu S. Prusty }
41a750ba5fSSubhransu S. Prusty 
42a750ba5fSSubhransu S. Prusty static int skl_transfer_firmware(struct sst_dsp *ctx,
43a750ba5fSSubhransu S. Prusty 		const void *basefw, u32 base_fw_size)
44a750ba5fSSubhransu S. Prusty {
45a750ba5fSSubhransu S. Prusty 	int ret = 0;
46a750ba5fSSubhransu S. Prusty 
47b7d0254cSJeeja KP 	ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, basefw, base_fw_size,
48b7d0254cSJeeja KP 								true);
49a750ba5fSSubhransu S. Prusty 	if (ret < 0)
50a750ba5fSSubhransu S. Prusty 		return ret;
51a750ba5fSSubhransu S. Prusty 
52a750ba5fSSubhransu S. Prusty 	ret = sst_dsp_register_poll(ctx,
53a750ba5fSSubhransu S. Prusty 			SKL_ADSP_FW_STATUS,
54a750ba5fSSubhransu S. Prusty 			SKL_FW_STS_MASK,
55a750ba5fSSubhransu S. Prusty 			SKL_FW_RFW_START,
56a750ba5fSSubhransu S. Prusty 			SKL_BASEFW_TIMEOUT,
57a750ba5fSSubhransu S. Prusty 			"Firmware boot");
58a750ba5fSSubhransu S. Prusty 
59a750ba5fSSubhransu S. Prusty 	ctx->cl_dev.ops.cl_stop_dma(ctx);
60a750ba5fSSubhransu S. Prusty 
61a750ba5fSSubhransu S. Prusty 	return ret;
62a750ba5fSSubhransu S. Prusty }
63a750ba5fSSubhransu S. Prusty 
6406711051SVinod Koul #define SKL_ADSP_FW_BIN_HDR_OFFSET 0x284
6506711051SVinod Koul 
66a750ba5fSSubhransu S. Prusty static int skl_load_base_firmware(struct sst_dsp *ctx)
67a750ba5fSSubhransu S. Prusty {
68a750ba5fSSubhransu S. Prusty 	int ret = 0, i;
69*bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = ctx->thread_context;
70cd63655eSVinod Koul 	struct firmware stripped_fw;
71a750ba5fSSubhransu S. Prusty 	u32 reg;
72a750ba5fSSubhransu S. Prusty 
7384c9e283SJeeja KP 	skl->boot_complete = false;
7484c9e283SJeeja KP 	init_waitqueue_head(&skl->boot_wait);
7584c9e283SJeeja KP 
7684c9e283SJeeja KP 	if (ctx->fw == NULL) {
77aecf6fd8SVinod Koul 		ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
78a750ba5fSSubhransu S. Prusty 		if (ret < 0) {
79a750ba5fSSubhransu S. Prusty 			dev_err(ctx->dev, "Request firmware failed %d\n", ret);
80a750ba5fSSubhransu S. Prusty 			return -EIO;
81a750ba5fSSubhransu S. Prusty 		}
8206711051SVinod Koul 	}
8306711051SVinod Koul 
84e280823cSVinod Koul 	/* prase uuids on first boot */
85e280823cSVinod Koul 	if (skl->is_first_boot) {
86a8e2c19eSSenthilnathan Veppur 		ret = snd_skl_parse_uuids(ctx, ctx->fw, SKL_ADSP_FW_BIN_HDR_OFFSET, 0);
8706711051SVinod Koul 		if (ret < 0) {
88e280823cSVinod Koul 			dev_err(ctx->dev, "UUID parsing err: %d\n", ret);
8906711051SVinod Koul 			release_firmware(ctx->fw);
90052f103cSJayachandran B 			skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
9106711051SVinod Koul 			return ret;
9284c9e283SJeeja KP 		}
93e280823cSVinod Koul 	}
9484c9e283SJeeja KP 
95cd63655eSVinod Koul 	/* check for extended manifest */
96cd63655eSVinod Koul 	stripped_fw.data = ctx->fw->data;
97cd63655eSVinod Koul 	stripped_fw.size = ctx->fw->size;
98cd63655eSVinod Koul 
99cd63655eSVinod Koul 	skl_dsp_strip_extended_manifest(&stripped_fw);
100cd63655eSVinod Koul 
10184c9e283SJeeja KP 	ret = skl_dsp_boot(ctx);
10284c9e283SJeeja KP 	if (ret < 0) {
103ecd286a9SColin Ian King 		dev_err(ctx->dev, "Boot dsp core failed ret: %d\n", ret);
10484c9e283SJeeja KP 		goto skl_load_base_firmware_failed;
10584c9e283SJeeja KP 	}
10684c9e283SJeeja KP 
10784c9e283SJeeja KP 	ret = skl_cldma_prepare(ctx);
10884c9e283SJeeja KP 	if (ret < 0) {
109ecd286a9SColin Ian King 		dev_err(ctx->dev, "CL dma prepare failed : %d\n", ret);
11084c9e283SJeeja KP 		goto skl_load_base_firmware_failed;
11184c9e283SJeeja KP 	}
112a750ba5fSSubhransu S. Prusty 
113a750ba5fSSubhransu S. Prusty 	/* enable Interrupt */
114a750ba5fSSubhransu S. Prusty 	skl_ipc_int_enable(ctx);
115a750ba5fSSubhransu S. Prusty 	skl_ipc_op_int_enable(ctx);
116a750ba5fSSubhransu S. Prusty 
117a750ba5fSSubhransu S. Prusty 	/* check ROM Status */
118a750ba5fSSubhransu S. Prusty 	for (i = SKL_INIT_TIMEOUT; i > 0; --i) {
119a750ba5fSSubhransu S. Prusty 		if (skl_check_fw_status(ctx, SKL_FW_INIT)) {
120a750ba5fSSubhransu S. Prusty 			dev_dbg(ctx->dev,
121a750ba5fSSubhransu S. Prusty 				"ROM loaded, we can continue with FW loading\n");
122a750ba5fSSubhransu S. Prusty 			break;
123a750ba5fSSubhransu S. Prusty 		}
124a750ba5fSSubhransu S. Prusty 		mdelay(1);
125a750ba5fSSubhransu S. Prusty 	}
126a750ba5fSSubhransu S. Prusty 	if (!i) {
127a750ba5fSSubhransu S. Prusty 		reg = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS);
128a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev,
129a750ba5fSSubhransu S. Prusty 			"Timeout waiting for ROM init done, reg:0x%x\n", reg);
130a750ba5fSSubhransu S. Prusty 		ret = -EIO;
131ae395937SJeeja KP 		goto transfer_firmware_failed;
132a750ba5fSSubhransu S. Prusty 	}
133a750ba5fSSubhransu S. Prusty 
134cd63655eSVinod Koul 	ret = skl_transfer_firmware(ctx, stripped_fw.data, stripped_fw.size);
135a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
136a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "Transfer firmware failed%d\n", ret);
137ae395937SJeeja KP 		goto transfer_firmware_failed;
138a750ba5fSSubhransu S. Prusty 	} else {
139a750ba5fSSubhransu S. Prusty 		ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
140a750ba5fSSubhransu S. Prusty 					msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
141a750ba5fSSubhransu S. Prusty 		if (ret == 0) {
142a750ba5fSSubhransu S. Prusty 			dev_err(ctx->dev, "DSP boot failed, FW Ready timed-out\n");
143a750ba5fSSubhransu S. Prusty 			ret = -EIO;
144ae395937SJeeja KP 			goto transfer_firmware_failed;
145a750ba5fSSubhransu S. Prusty 		}
146a750ba5fSSubhransu S. Prusty 
147a750ba5fSSubhransu S. Prusty 		dev_dbg(ctx->dev, "Download firmware successful%d\n", ret);
1481665c177SJayachandran B 		skl->fw_loaded = true;
149a750ba5fSSubhransu S. Prusty 	}
150a750ba5fSSubhransu S. Prusty 	return 0;
151ae395937SJeeja KP transfer_firmware_failed:
152ae395937SJeeja KP 	ctx->cl_dev.ops.cl_cleanup_controller(ctx);
153a750ba5fSSubhransu S. Prusty skl_load_base_firmware_failed:
154052f103cSJayachandran B 	skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
15584c9e283SJeeja KP 	release_firmware(ctx->fw);
15684c9e283SJeeja KP 	ctx->fw = NULL;
157a750ba5fSSubhransu S. Prusty 	return ret;
158a750ba5fSSubhransu S. Prusty }
159a750ba5fSSubhransu S. Prusty 
160052f103cSJayachandran B static int skl_set_dsp_D0(struct sst_dsp *ctx, unsigned int core_id)
161a750ba5fSSubhransu S. Prusty {
162a750ba5fSSubhransu S. Prusty 	int ret;
16340a16603SJayachandran B 	struct skl_ipc_dxstate_info dx;
164*bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = ctx->thread_context;
16540a16603SJayachandran B 	unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
166a750ba5fSSubhransu S. Prusty 
16740a16603SJayachandran B 	/* If core0 is being turned on, we need to load the FW */
16840a16603SJayachandran B 	if (core_id == SKL_DSP_CORE0_ID) {
169a750ba5fSSubhransu S. Prusty 		ret = skl_load_base_firmware(ctx);
170a750ba5fSSubhransu S. Prusty 		if (ret < 0) {
171a750ba5fSSubhransu S. Prusty 			dev_err(ctx->dev, "unable to load firmware\n");
172a750ba5fSSubhransu S. Prusty 			return ret;
173a750ba5fSSubhransu S. Prusty 		}
174b6726009SSodhi, VunnyX 
175b6726009SSodhi, VunnyX 		/* load libs as they are also lost on D3 */
176b6726009SSodhi, VunnyX 		if (skl->lib_count > 1) {
177b6726009SSodhi, VunnyX 			ret = ctx->fw_ops.load_library(ctx, skl->lib_info,
178b6726009SSodhi, VunnyX 							skl->lib_count);
179b6726009SSodhi, VunnyX 			if (ret < 0) {
180b6726009SSodhi, VunnyX 				dev_err(ctx->dev, "reload libs failed: %d\n",
181b6726009SSodhi, VunnyX 						ret);
182b6726009SSodhi, VunnyX 				return ret;
183b6726009SSodhi, VunnyX 			}
184b6726009SSodhi, VunnyX 
185b6726009SSodhi, VunnyX 		}
18640a16603SJayachandran B 	}
187a750ba5fSSubhransu S. Prusty 
18840a16603SJayachandran B 	/*
18940a16603SJayachandran B 	 * If any core other than core 0 is being moved to D0, enable the
19040a16603SJayachandran B 	 * core and send the set dx IPC for the core.
19140a16603SJayachandran B 	 */
19240a16603SJayachandran B 	if (core_id != SKL_DSP_CORE0_ID) {
19340a16603SJayachandran B 		ret = skl_dsp_enable_core(ctx, core_mask);
19440a16603SJayachandran B 		if (ret < 0)
19540a16603SJayachandran B 			return ret;
19640a16603SJayachandran B 
19740a16603SJayachandran B 		dx.core_mask = core_mask;
19840a16603SJayachandran B 		dx.dx_mask = core_mask;
19940a16603SJayachandran B 
20040a16603SJayachandran B 		ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID,
20140a16603SJayachandran B 					SKL_BASE_FW_MODULE_ID, &dx);
20240a16603SJayachandran B 		if (ret < 0) {
20340a16603SJayachandran B 			dev_err(ctx->dev, "Failed to set dsp to D0:core id= %d\n",
20440a16603SJayachandran B 					core_id);
20540a16603SJayachandran B 			skl_dsp_disable_core(ctx, core_mask);
20640a16603SJayachandran B 		}
20740a16603SJayachandran B 	}
20840a16603SJayachandran B 
20940a16603SJayachandran B 	skl->cores.state[core_id] = SKL_DSP_RUNNING;
210a750ba5fSSubhransu S. Prusty 
211b6726009SSodhi, VunnyX 	return 0;
212a750ba5fSSubhransu S. Prusty }
213a750ba5fSSubhransu S. Prusty 
214052f103cSJayachandran B static int skl_set_dsp_D3(struct sst_dsp *ctx, unsigned int core_id)
215a750ba5fSSubhransu S. Prusty {
216a750ba5fSSubhransu S. Prusty 	int ret;
217a750ba5fSSubhransu S. Prusty 	struct skl_ipc_dxstate_info dx;
218*bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = ctx->thread_context;
21940a16603SJayachandran B 	unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
220a750ba5fSSubhransu S. Prusty 
22140a16603SJayachandran B 	dx.core_mask = core_mask;
222a750ba5fSSubhransu S. Prusty 	dx.dx_mask = SKL_IPC_D3_MASK;
22340a16603SJayachandran B 
224a750ba5fSSubhransu S. Prusty 	ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID, SKL_BASE_FW_MODULE_ID, &dx);
22553afce2cSJeeja KP 	if (ret < 0)
22640a16603SJayachandran B 		dev_err(ctx->dev, "set Dx core %d fail: %d\n", core_id, ret);
22753afce2cSJeeja KP 
22840a16603SJayachandran B 	if (core_id == SKL_DSP_CORE0_ID) {
22953afce2cSJeeja KP 		/* disable Interrupt */
23053afce2cSJeeja KP 		ctx->cl_dev.ops.cl_cleanup_controller(ctx);
23153afce2cSJeeja KP 		skl_cldma_int_disable(ctx);
23253afce2cSJeeja KP 		skl_ipc_op_int_disable(ctx);
23353afce2cSJeeja KP 		skl_ipc_int_disable(ctx);
234a750ba5fSSubhransu S. Prusty 	}
235a750ba5fSSubhransu S. Prusty 
23640a16603SJayachandran B 	ret = skl_dsp_disable_core(ctx, core_mask);
23740a16603SJayachandran B 	if (ret < 0)
23840a16603SJayachandran B 		return ret;
23940a16603SJayachandran B 
24040a16603SJayachandran B 	skl->cores.state[core_id] = SKL_DSP_RESET;
241a750ba5fSSubhransu S. Prusty 	return ret;
242a750ba5fSSubhransu S. Prusty }
243a750ba5fSSubhransu S. Prusty 
244a750ba5fSSubhransu S. Prusty static unsigned int skl_get_errorcode(struct sst_dsp *ctx)
245a750ba5fSSubhransu S. Prusty {
246a750ba5fSSubhransu S. Prusty 	 return sst_dsp_shim_read(ctx, SKL_ADSP_ERROR_CODE);
247a750ba5fSSubhransu S. Prusty }
248a750ba5fSSubhransu S. Prusty 
2496c5768b3SDharageswari R /*
2506c5768b3SDharageswari R  * since get/set_module are called from DAPM context,
2516c5768b3SDharageswari R  * we don't need lock for usage count
2526c5768b3SDharageswari R  */
253db4e5613SDan Carpenter static int skl_get_module(struct sst_dsp *ctx, u16 mod_id)
2546c5768b3SDharageswari R {
2556c5768b3SDharageswari R 	struct skl_module_table *module;
2566c5768b3SDharageswari R 
2576c5768b3SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
2586c5768b3SDharageswari R 		if (module->mod_info->mod_id == mod_id)
2596c5768b3SDharageswari R 			return ++module->usage_cnt;
2606c5768b3SDharageswari R 	}
2616c5768b3SDharageswari R 
2626c5768b3SDharageswari R 	return -EINVAL;
2636c5768b3SDharageswari R }
2646c5768b3SDharageswari R 
265db4e5613SDan Carpenter static int skl_put_module(struct sst_dsp *ctx, u16 mod_id)
2666c5768b3SDharageswari R {
2676c5768b3SDharageswari R 	struct skl_module_table *module;
2686c5768b3SDharageswari R 
2696c5768b3SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
2706c5768b3SDharageswari R 		if (module->mod_info->mod_id == mod_id)
2716c5768b3SDharageswari R 			return --module->usage_cnt;
2726c5768b3SDharageswari R 	}
2736c5768b3SDharageswari R 
2746c5768b3SDharageswari R 	return -EINVAL;
2756c5768b3SDharageswari R }
2766c5768b3SDharageswari R 
2776c5768b3SDharageswari R static struct skl_module_table *skl_fill_module_table(struct sst_dsp *ctx,
2786c5768b3SDharageswari R 						char *mod_name, int mod_id)
2796c5768b3SDharageswari R {
2806c5768b3SDharageswari R 	const struct firmware *fw;
2816c5768b3SDharageswari R 	struct skl_module_table *skl_module;
2826c5768b3SDharageswari R 	unsigned int size;
2836c5768b3SDharageswari R 	int ret;
2846c5768b3SDharageswari R 
2856c5768b3SDharageswari R 	ret = request_firmware(&fw, mod_name, ctx->dev);
2866c5768b3SDharageswari R 	if (ret < 0) {
2876c5768b3SDharageswari R 		dev_err(ctx->dev, "Request Module %s failed :%d\n",
2886c5768b3SDharageswari R 							mod_name, ret);
2896c5768b3SDharageswari R 		return NULL;
2906c5768b3SDharageswari R 	}
2916c5768b3SDharageswari R 
2926c5768b3SDharageswari R 	skl_module = devm_kzalloc(ctx->dev, sizeof(*skl_module), GFP_KERNEL);
2936c5768b3SDharageswari R 	if (skl_module == NULL) {
2946c5768b3SDharageswari R 		release_firmware(fw);
2956c5768b3SDharageswari R 		return NULL;
2966c5768b3SDharageswari R 	}
2976c5768b3SDharageswari R 
2986c5768b3SDharageswari R 	size = sizeof(*skl_module->mod_info);
2996c5768b3SDharageswari R 	skl_module->mod_info = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
3006c5768b3SDharageswari R 	if (skl_module->mod_info == NULL) {
3016c5768b3SDharageswari R 		release_firmware(fw);
3026c5768b3SDharageswari R 		return NULL;
3036c5768b3SDharageswari R 	}
3046c5768b3SDharageswari R 
3056c5768b3SDharageswari R 	skl_module->mod_info->mod_id = mod_id;
3066c5768b3SDharageswari R 	skl_module->mod_info->fw = fw;
3076c5768b3SDharageswari R 	list_add(&skl_module->list, &ctx->module_list);
3086c5768b3SDharageswari R 
3096c5768b3SDharageswari R 	return skl_module;
3106c5768b3SDharageswari R }
3116c5768b3SDharageswari R 
3126c5768b3SDharageswari R /* get a module from it's unique ID */
3136c5768b3SDharageswari R static struct skl_module_table *skl_module_get_from_id(
3146c5768b3SDharageswari R 			struct sst_dsp *ctx, u16 mod_id)
3156c5768b3SDharageswari R {
3166c5768b3SDharageswari R 	struct skl_module_table *module;
3176c5768b3SDharageswari R 
3186c5768b3SDharageswari R 	if (list_empty(&ctx->module_list)) {
3196c5768b3SDharageswari R 		dev_err(ctx->dev, "Module list is empty\n");
3206c5768b3SDharageswari R 		return NULL;
3216c5768b3SDharageswari R 	}
3226c5768b3SDharageswari R 
3236c5768b3SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
3246c5768b3SDharageswari R 		if (module->mod_info->mod_id == mod_id)
3256c5768b3SDharageswari R 			return module;
3266c5768b3SDharageswari R 	}
3276c5768b3SDharageswari R 
3286c5768b3SDharageswari R 	return NULL;
3296c5768b3SDharageswari R }
3306c5768b3SDharageswari R 
331b7d0254cSJeeja KP static int skl_transfer_module(struct sst_dsp *ctx, const void *data,
3324e0277d2SG Kranthi 			u32 size, u16 mod_id, u8 table_id, bool is_module)
3336c5768b3SDharageswari R {
334b7d0254cSJeeja KP 	int ret, bytes_left, curr_pos;
335*bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = ctx->thread_context;
336b7d0254cSJeeja KP 	skl->mod_load_complete = false;
3376c5768b3SDharageswari R 
338b7d0254cSJeeja KP 	bytes_left = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, data, size, false);
339b7d0254cSJeeja KP 	if (bytes_left < 0)
340b7d0254cSJeeja KP 		return bytes_left;
3416c5768b3SDharageswari R 
342b6726009SSodhi, VunnyX 	/* check is_module flag to load module or library */
343b6726009SSodhi, VunnyX 	if (is_module)
344b7d0254cSJeeja KP 		ret = skl_ipc_load_modules(&skl->ipc, SKL_NUM_MODULES, &mod_id);
345b6726009SSodhi, VunnyX 	else
346b6726009SSodhi, VunnyX 		ret = skl_sst_ipc_load_library(&skl->ipc, 0, table_id, false);
347b6726009SSodhi, VunnyX 
348b7d0254cSJeeja KP 	if (ret < 0) {
349b6726009SSodhi, VunnyX 		dev_err(ctx->dev, "Failed to Load %s with err %d\n",
350b6726009SSodhi, VunnyX 				is_module ? "module" : "lib", ret);
351b7d0254cSJeeja KP 		goto out;
352b7d0254cSJeeja KP 	}
3536c5768b3SDharageswari R 
354b7d0254cSJeeja KP 	/*
355b7d0254cSJeeja KP 	 * if bytes_left > 0 then wait for BDL complete interrupt and
356b7d0254cSJeeja KP 	 * copy the next chunk till bytes_left is 0. if bytes_left is
357b7d0254cSJeeja KP 	 * is zero, then wait for load module IPC reply
358b7d0254cSJeeja KP 	 */
359b7d0254cSJeeja KP 	while (bytes_left > 0) {
360b7d0254cSJeeja KP 		curr_pos = size - bytes_left;
361b7d0254cSJeeja KP 
362b7d0254cSJeeja KP 		ret = skl_cldma_wait_interruptible(ctx);
363b7d0254cSJeeja KP 		if (ret < 0)
364b7d0254cSJeeja KP 			goto out;
365b7d0254cSJeeja KP 
366b7d0254cSJeeja KP 		bytes_left = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx,
367b7d0254cSJeeja KP 							data + curr_pos,
368b7d0254cSJeeja KP 							bytes_left, false);
369b7d0254cSJeeja KP 	}
370b7d0254cSJeeja KP 
371b7d0254cSJeeja KP 	ret = wait_event_timeout(skl->mod_load_wait, skl->mod_load_complete,
372b7d0254cSJeeja KP 				msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
373b7d0254cSJeeja KP 	if (ret == 0 || !skl->mod_load_status) {
374b7d0254cSJeeja KP 		dev_err(ctx->dev, "Module Load failed\n");
375b7d0254cSJeeja KP 		ret = -EIO;
376b7d0254cSJeeja KP 	}
377b7d0254cSJeeja KP 
378b7d0254cSJeeja KP out:
3796c5768b3SDharageswari R 	ctx->cl_dev.ops.cl_stop_dma(ctx);
3806c5768b3SDharageswari R 
3816c5768b3SDharageswari R 	return ret;
3826c5768b3SDharageswari R }
3836c5768b3SDharageswari R 
384b6726009SSodhi, VunnyX static int
385651e4890SPradeep Tewani skl_load_library(struct sst_dsp *ctx, struct skl_lib_info *linfo, int lib_count)
386b6726009SSodhi, VunnyX {
387*bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = ctx->thread_context;
388b6726009SSodhi, VunnyX 	struct firmware stripped_fw;
389b6726009SSodhi, VunnyX 	int ret, i;
390b6726009SSodhi, VunnyX 
391b6726009SSodhi, VunnyX 	/* library indices start from 1 to N. 0 represents base FW */
392b6726009SSodhi, VunnyX 	for (i = 1; i < lib_count; i++) {
393b6726009SSodhi, VunnyX 		ret = skl_prepare_lib_load(skl, &skl->lib_info[i], &stripped_fw,
394b6726009SSodhi, VunnyX 					SKL_ADSP_FW_BIN_HDR_OFFSET, i);
395b6726009SSodhi, VunnyX 		if (ret < 0)
396b6726009SSodhi, VunnyX 			goto load_library_failed;
397b6726009SSodhi, VunnyX 		ret = skl_transfer_module(ctx, stripped_fw.data,
398b6726009SSodhi, VunnyX 				stripped_fw.size, 0, i, false);
399b6726009SSodhi, VunnyX 		if (ret < 0)
400b6726009SSodhi, VunnyX 			goto load_library_failed;
401b6726009SSodhi, VunnyX 	}
402b6726009SSodhi, VunnyX 
403b6726009SSodhi, VunnyX 	return 0;
404b6726009SSodhi, VunnyX 
405b6726009SSodhi, VunnyX load_library_failed:
406b6726009SSodhi, VunnyX 	skl_release_library(linfo, lib_count);
407b6726009SSodhi, VunnyX 	return ret;
408b6726009SSodhi, VunnyX }
409b6726009SSodhi, VunnyX 
41009305da9SShreyas NC static int skl_load_module(struct sst_dsp *ctx, u16 mod_id, u8 *guid)
4116c5768b3SDharageswari R {
4126c5768b3SDharageswari R 	struct skl_module_table *module_entry = NULL;
4136c5768b3SDharageswari R 	int ret = 0;
4146c5768b3SDharageswari R 	char mod_name[64]; /* guid str = 32 chars + 4 hyphens */
4156c5768b3SDharageswari R 
41609305da9SShreyas NC 	snprintf(mod_name, sizeof(mod_name), "%s%pUL%s",
4179e0784d0SAndy Shevchenko 					     "intel/dsp_fw_", guid, ".bin");
4186c5768b3SDharageswari R 
4196c5768b3SDharageswari R 	module_entry = skl_module_get_from_id(ctx, mod_id);
4206c5768b3SDharageswari R 	if (module_entry == NULL) {
4216c5768b3SDharageswari R 		module_entry = skl_fill_module_table(ctx, mod_name, mod_id);
4226c5768b3SDharageswari R 		if (module_entry == NULL) {
4236c5768b3SDharageswari R 			dev_err(ctx->dev, "Failed to Load module\n");
4246c5768b3SDharageswari R 			return -EINVAL;
4256c5768b3SDharageswari R 		}
4266c5768b3SDharageswari R 	}
4276c5768b3SDharageswari R 
4286c5768b3SDharageswari R 	if (!module_entry->usage_cnt) {
429b7d0254cSJeeja KP 		ret = skl_transfer_module(ctx, module_entry->mod_info->fw->data,
4304e0277d2SG Kranthi 				module_entry->mod_info->fw->size,
4314e0277d2SG Kranthi 				mod_id, 0, true);
4326c5768b3SDharageswari R 		if (ret < 0) {
4336c5768b3SDharageswari R 			dev_err(ctx->dev, "Failed to Load module\n");
4346c5768b3SDharageswari R 			return ret;
4356c5768b3SDharageswari R 		}
4366c5768b3SDharageswari R 	}
4376c5768b3SDharageswari R 
4386c5768b3SDharageswari R 	ret = skl_get_module(ctx, mod_id);
4396c5768b3SDharageswari R 
4406c5768b3SDharageswari R 	return ret;
4416c5768b3SDharageswari R }
4426c5768b3SDharageswari R 
4436c5768b3SDharageswari R static int skl_unload_module(struct sst_dsp *ctx, u16 mod_id)
4446c5768b3SDharageswari R {
445db4e5613SDan Carpenter 	int usage_cnt;
446*bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = ctx->thread_context;
4476c5768b3SDharageswari R 	int ret = 0;
4486c5768b3SDharageswari R 
4496c5768b3SDharageswari R 	usage_cnt = skl_put_module(ctx, mod_id);
4506c5768b3SDharageswari R 	if (usage_cnt < 0) {
4516c5768b3SDharageswari R 		dev_err(ctx->dev, "Module bad usage cnt!:%d\n", usage_cnt);
4526c5768b3SDharageswari R 		return -EIO;
4536c5768b3SDharageswari R 	}
454f7ea7777SVinod Koul 
455f7ea7777SVinod Koul 	/* if module is used by others return, no need to unload */
456f7ea7777SVinod Koul 	if (usage_cnt > 0)
457f7ea7777SVinod Koul 		return 0;
458f7ea7777SVinod Koul 
4596c5768b3SDharageswari R 	ret = skl_ipc_unload_modules(&skl->ipc,
4606c5768b3SDharageswari R 			SKL_NUM_MODULES, &mod_id);
4616c5768b3SDharageswari R 	if (ret < 0) {
4626c5768b3SDharageswari R 		dev_err(ctx->dev, "Failed to UnLoad module\n");
4636c5768b3SDharageswari R 		skl_get_module(ctx, mod_id);
4646c5768b3SDharageswari R 		return ret;
4656c5768b3SDharageswari R 	}
4666c5768b3SDharageswari R 
4676c5768b3SDharageswari R 	return ret;
4686c5768b3SDharageswari R }
4696c5768b3SDharageswari R 
470fe3f4442SDharageswari R void skl_clear_module_cnt(struct sst_dsp *ctx)
471fe3f4442SDharageswari R {
472fe3f4442SDharageswari R 	struct skl_module_table *module;
473fe3f4442SDharageswari R 
474a35aeaeeSVinod Koul 	if (list_empty(&ctx->module_list))
475a35aeaeeSVinod Koul 		return;
476a35aeaeeSVinod Koul 
477fe3f4442SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
478fe3f4442SDharageswari R 		module->usage_cnt = 0;
479fe3f4442SDharageswari R 	}
480fe3f4442SDharageswari R }
481fe3f4442SDharageswari R EXPORT_SYMBOL_GPL(skl_clear_module_cnt);
482fe3f4442SDharageswari R 
4836c5768b3SDharageswari R static void skl_clear_module_table(struct sst_dsp *ctx)
4846c5768b3SDharageswari R {
4856c5768b3SDharageswari R 	struct skl_module_table *module, *tmp;
4866c5768b3SDharageswari R 
4876c5768b3SDharageswari R 	if (list_empty(&ctx->module_list))
4886c5768b3SDharageswari R 		return;
4896c5768b3SDharageswari R 
4906c5768b3SDharageswari R 	list_for_each_entry_safe(module, tmp, &ctx->module_list, list) {
4916c5768b3SDharageswari R 		list_del(&module->list);
4926c5768b3SDharageswari R 		release_firmware(module->mod_info->fw);
4936c5768b3SDharageswari R 	}
4946c5768b3SDharageswari R }
4956c5768b3SDharageswari R 
4962788808aSBhumika Goyal static const struct skl_dsp_fw_ops skl_fw_ops = {
497a750ba5fSSubhransu S. Prusty 	.set_state_D0 = skl_set_dsp_D0,
498a750ba5fSSubhransu S. Prusty 	.set_state_D3 = skl_set_dsp_D3,
499a750ba5fSSubhransu S. Prusty 	.load_fw = skl_load_base_firmware,
500a750ba5fSSubhransu S. Prusty 	.get_fw_errcode = skl_get_errorcode,
501651e4890SPradeep Tewani 	.load_library = skl_load_library,
50289b0d8a5SSubhransu S. Prusty 	.load_mod = skl_load_module,
50389b0d8a5SSubhransu S. Prusty 	.unload_mod = skl_unload_module,
50489b0d8a5SSubhransu S. Prusty };
50589b0d8a5SSubhransu S. Prusty 
506a750ba5fSSubhransu S. Prusty static struct sst_ops skl_ops = {
507a750ba5fSSubhransu S. Prusty 	.irq_handler = skl_dsp_sst_interrupt,
508a750ba5fSSubhransu S. Prusty 	.write = sst_shim32_write,
509a750ba5fSSubhransu S. Prusty 	.read = sst_shim32_read,
510a750ba5fSSubhransu S. Prusty 	.ram_read = sst_memcpy_fromio_32,
511a750ba5fSSubhransu S. Prusty 	.ram_write = sst_memcpy_toio_32,
512a750ba5fSSubhransu S. Prusty 	.free = skl_dsp_free,
513a750ba5fSSubhransu S. Prusty };
514a750ba5fSSubhransu S. Prusty 
515a750ba5fSSubhransu S. Prusty static struct sst_dsp_device skl_dev = {
516a750ba5fSSubhransu S. Prusty 	.thread = skl_dsp_irq_thread_handler,
517a750ba5fSSubhransu S. Prusty 	.ops = &skl_ops,
518a750ba5fSSubhransu S. Prusty };
519a750ba5fSSubhransu S. Prusty 
520a750ba5fSSubhransu S. Prusty int skl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
521*bcc2a2dcSCezary Rojewski 		const char *fw_name, struct skl_dsp_loader_ops dsp_ops,
522*bcc2a2dcSCezary Rojewski 		struct skl_dev **dsp)
523a750ba5fSSubhransu S. Prusty {
524*bcc2a2dcSCezary Rojewski 	struct skl_dev *skl;
525a750ba5fSSubhransu S. Prusty 	struct sst_dsp *sst;
526a750ba5fSSubhransu S. Prusty 	int ret;
527a750ba5fSSubhransu S. Prusty 
5289fe9c711SG Kranthi 	ret = skl_sst_ctx_init(dev, irq, fw_name, dsp_ops, dsp, &skl_dev);
5299fe9c711SG Kranthi 	if (ret < 0) {
5309fe9c711SG Kranthi 		dev_err(dev, "%s: no device\n", __func__);
5319fe9c711SG Kranthi 		return ret;
532a750ba5fSSubhransu S. Prusty 	}
533a750ba5fSSubhransu S. Prusty 
5349fe9c711SG Kranthi 	skl = *dsp;
535a750ba5fSSubhransu S. Prusty 	sst = skl->dsp;
536a750ba5fSSubhransu S. Prusty 	sst->addr.lpe = mmio_base;
537a750ba5fSSubhransu S. Prusty 	sst->addr.shim = mmio_base;
53809e914d6SGuneshwor Singh 	sst->addr.sram0_base = SKL_ADSP_SRAM0_BASE;
53909e914d6SGuneshwor Singh 	sst->addr.sram1_base = SKL_ADSP_SRAM1_BASE;
54009e914d6SGuneshwor Singh 	sst->addr.w0_stat_sz = SKL_ADSP_W0_STAT_SZ;
54109e914d6SGuneshwor Singh 	sst->addr.w0_up_sz = SKL_ADSP_W0_UP_SZ;
54209e914d6SGuneshwor Singh 
543a750ba5fSSubhransu S. Prusty 	sst_dsp_mailbox_init(sst, (SKL_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
544a750ba5fSSubhransu S. Prusty 			SKL_ADSP_W0_UP_SZ, SKL_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
545a750ba5fSSubhransu S. Prusty 
5462eed1b02SGuneshwor Singh 	ret = skl_ipc_init(dev, skl);
5473b3011adSSubhransu S. Prusty 	if (ret) {
5483b3011adSSubhransu S. Prusty 		skl_dsp_free(sst);
5492eed1b02SGuneshwor Singh 		return ret;
5503b3011adSSubhransu S. Prusty 	}
5512eed1b02SGuneshwor Singh 
552a750ba5fSSubhransu S. Prusty 	sst->fw_ops = skl_fw_ops;
553a750ba5fSSubhransu S. Prusty 
5548e9d8e19SSubhransu S. Prusty 	return skl_dsp_acquire_irq(sst);
555a750ba5fSSubhransu S. Prusty }
556a750ba5fSSubhransu S. Prusty EXPORT_SYMBOL_GPL(skl_sst_dsp_init);
557a750ba5fSSubhransu S. Prusty 
558*bcc2a2dcSCezary Rojewski int skl_sst_init_fw(struct device *dev, struct skl_dev *skl)
55978cdbbdaSVinod Koul {
56078cdbbdaSVinod Koul 	int ret;
561*bcc2a2dcSCezary Rojewski 	struct sst_dsp *sst = skl->dsp;
56278cdbbdaSVinod Koul 
56378cdbbdaSVinod Koul 	ret = sst->fw_ops.load_fw(sst);
56478cdbbdaSVinod Koul 	if (ret < 0) {
565ecd286a9SColin Ian King 		dev_err(dev, "Load base fw failed : %d\n", ret);
56678cdbbdaSVinod Koul 		return ret;
56778cdbbdaSVinod Koul 	}
56878cdbbdaSVinod Koul 
56978cdbbdaSVinod Koul 	skl_dsp_init_core_state(sst);
570b6726009SSodhi, VunnyX 
571*bcc2a2dcSCezary Rojewski 	if (skl->lib_count > 1) {
572*bcc2a2dcSCezary Rojewski 		ret = sst->fw_ops.load_library(sst, skl->lib_info,
573*bcc2a2dcSCezary Rojewski 						skl->lib_count);
574b6726009SSodhi, VunnyX 		if (ret < 0) {
575b6726009SSodhi, VunnyX 			dev_err(dev, "Load Library failed : %x\n", ret);
576b6726009SSodhi, VunnyX 			return ret;
577b6726009SSodhi, VunnyX 		}
578b6726009SSodhi, VunnyX 	}
579*bcc2a2dcSCezary Rojewski 	skl->is_first_boot = false;
58078cdbbdaSVinod Koul 
58178cdbbdaSVinod Koul 	return 0;
58278cdbbdaSVinod Koul }
58378cdbbdaSVinod Koul EXPORT_SYMBOL_GPL(skl_sst_init_fw);
58478cdbbdaSVinod Koul 
585*bcc2a2dcSCezary Rojewski void skl_sst_dsp_cleanup(struct device *dev, struct skl_dev *skl)
586a750ba5fSSubhransu S. Prusty {
587bc65a326SJeeja KP 
588*bcc2a2dcSCezary Rojewski 	if (skl->dsp->fw)
589*bcc2a2dcSCezary Rojewski 		release_firmware(skl->dsp->fw);
590*bcc2a2dcSCezary Rojewski 	skl_clear_module_table(skl->dsp);
591*bcc2a2dcSCezary Rojewski 	skl_freeup_uuid_list(skl);
592*bcc2a2dcSCezary Rojewski 	skl_ipc_free(&skl->ipc);
593*bcc2a2dcSCezary Rojewski 	skl->dsp->ops->free(skl->dsp);
594*bcc2a2dcSCezary Rojewski 	if (skl->boot_complete) {
595*bcc2a2dcSCezary Rojewski 		skl->dsp->cl_dev.ops.cl_cleanup_controller(skl->dsp);
596*bcc2a2dcSCezary Rojewski 		skl_cldma_int_disable(skl->dsp);
59795536d8cSDharageswari.R 	}
598a750ba5fSSubhransu S. Prusty }
599a750ba5fSSubhransu S. Prusty EXPORT_SYMBOL_GPL(skl_sst_dsp_cleanup);
600a750ba5fSSubhransu S. Prusty 
601a750ba5fSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
602a750ba5fSSubhransu S. Prusty MODULE_DESCRIPTION("Intel Skylake IPC driver");
603