xref: /openbmc/linux/sound/soc/intel/skylake/skl-sst.c (revision cd63655e8025eb9839b8aaf503e14f89da9d4f9e)
1a750ba5fSSubhransu S. Prusty /*
2a750ba5fSSubhransu S. Prusty  * skl-sst.c - HDA DSP library functions for SKL platform
3a750ba5fSSubhransu S. Prusty  *
4a750ba5fSSubhransu S. Prusty  * Copyright (C) 2014-15, Intel Corporation.
5a750ba5fSSubhransu S. Prusty  * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
6a750ba5fSSubhransu S. Prusty  *	Jeeja KP <jeeja.kp@intel.com>
7a750ba5fSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8a750ba5fSSubhransu S. Prusty  *
9a750ba5fSSubhransu S. Prusty  * This program is free software; you can redistribute it and/or modify
10a750ba5fSSubhransu S. Prusty  * it under the terms of the GNU General Public License as version 2, as
11a750ba5fSSubhransu S. Prusty  * published by the Free Software Foundation.
12a750ba5fSSubhransu S. Prusty  *
13a750ba5fSSubhransu S. Prusty  * This program is distributed in the hope that it will be useful, but
14a750ba5fSSubhransu S. Prusty  * WITHOUT ANY WARRANTY; without even the implied warranty of
15a750ba5fSSubhransu S. Prusty  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16a750ba5fSSubhransu S. Prusty  * General Public License for more details.
17a750ba5fSSubhransu S. Prusty  */
18a750ba5fSSubhransu S. Prusty 
19a750ba5fSSubhransu S. Prusty #include <linux/module.h>
20a750ba5fSSubhransu S. Prusty #include <linux/delay.h>
21a750ba5fSSubhransu S. Prusty #include <linux/device.h>
2253afce2cSJeeja KP #include <linux/err.h>
2309305da9SShreyas NC #include <linux/uuid.h>
24a750ba5fSSubhransu S. Prusty #include "../common/sst-dsp.h"
25a750ba5fSSubhransu S. Prusty #include "../common/sst-dsp-priv.h"
26a750ba5fSSubhransu S. Prusty #include "../common/sst-ipc.h"
27a750ba5fSSubhransu S. Prusty #include "skl-sst-ipc.h"
28a750ba5fSSubhransu S. Prusty 
29a750ba5fSSubhransu S. Prusty #define SKL_BASEFW_TIMEOUT	300
30a750ba5fSSubhransu S. Prusty #define SKL_INIT_TIMEOUT	1000
31a750ba5fSSubhransu S. Prusty 
32a750ba5fSSubhransu S. Prusty /* Intel HD Audio SRAM Window 0*/
33a750ba5fSSubhransu S. Prusty #define SKL_ADSP_SRAM0_BASE	0x8000
34a750ba5fSSubhransu S. Prusty 
35a750ba5fSSubhransu S. Prusty /* Firmware status window */
36a750ba5fSSubhransu S. Prusty #define SKL_ADSP_FW_STATUS	SKL_ADSP_SRAM0_BASE
37a750ba5fSSubhransu S. Prusty #define SKL_ADSP_ERROR_CODE	(SKL_ADSP_FW_STATUS + 0x4)
38a750ba5fSSubhransu S. Prusty 
396c5768b3SDharageswari R #define SKL_NUM_MODULES		1
406c5768b3SDharageswari R 
41a750ba5fSSubhransu S. Prusty static bool skl_check_fw_status(struct sst_dsp *ctx, u32 status)
42a750ba5fSSubhransu S. Prusty {
43a750ba5fSSubhransu S. Prusty 	u32 cur_sts;
44a750ba5fSSubhransu S. Prusty 
45a750ba5fSSubhransu S. Prusty 	cur_sts = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS) & SKL_FW_STS_MASK;
46a750ba5fSSubhransu S. Prusty 
47a750ba5fSSubhransu S. Prusty 	return (cur_sts == status);
48a750ba5fSSubhransu S. Prusty }
49a750ba5fSSubhransu S. Prusty 
50a750ba5fSSubhransu S. Prusty static int skl_transfer_firmware(struct sst_dsp *ctx,
51a750ba5fSSubhransu S. Prusty 		const void *basefw, u32 base_fw_size)
52a750ba5fSSubhransu S. Prusty {
53a750ba5fSSubhransu S. Prusty 	int ret = 0;
54a750ba5fSSubhransu S. Prusty 
55a750ba5fSSubhransu S. Prusty 	ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, basefw, base_fw_size);
56a750ba5fSSubhransu S. Prusty 	if (ret < 0)
57a750ba5fSSubhransu S. Prusty 		return ret;
58a750ba5fSSubhransu S. Prusty 
59a750ba5fSSubhransu S. Prusty 	ret = sst_dsp_register_poll(ctx,
60a750ba5fSSubhransu S. Prusty 			SKL_ADSP_FW_STATUS,
61a750ba5fSSubhransu S. Prusty 			SKL_FW_STS_MASK,
62a750ba5fSSubhransu S. Prusty 			SKL_FW_RFW_START,
63a750ba5fSSubhransu S. Prusty 			SKL_BASEFW_TIMEOUT,
64a750ba5fSSubhransu S. Prusty 			"Firmware boot");
65a750ba5fSSubhransu S. Prusty 
66a750ba5fSSubhransu S. Prusty 	ctx->cl_dev.ops.cl_stop_dma(ctx);
67a750ba5fSSubhransu S. Prusty 
68a750ba5fSSubhransu S. Prusty 	return ret;
69a750ba5fSSubhransu S. Prusty }
70a750ba5fSSubhransu S. Prusty 
71a750ba5fSSubhransu S. Prusty static int skl_load_base_firmware(struct sst_dsp *ctx)
72a750ba5fSSubhransu S. Prusty {
73a750ba5fSSubhransu S. Prusty 	int ret = 0, i;
74a750ba5fSSubhransu S. Prusty 	struct skl_sst *skl = ctx->thread_context;
75*cd63655eSVinod Koul 	struct firmware stripped_fw;
76a750ba5fSSubhransu S. Prusty 	u32 reg;
77a750ba5fSSubhransu S. Prusty 
7884c9e283SJeeja KP 	skl->boot_complete = false;
7984c9e283SJeeja KP 	init_waitqueue_head(&skl->boot_wait);
8084c9e283SJeeja KP 
8184c9e283SJeeja KP 	if (ctx->fw == NULL) {
82aecf6fd8SVinod Koul 		ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
83a750ba5fSSubhransu S. Prusty 		if (ret < 0) {
84a750ba5fSSubhransu S. Prusty 			dev_err(ctx->dev, "Request firmware failed %d\n", ret);
85a750ba5fSSubhransu S. Prusty 			skl_dsp_disable_core(ctx);
86a750ba5fSSubhransu S. Prusty 			return -EIO;
87a750ba5fSSubhransu S. Prusty 		}
8884c9e283SJeeja KP 	}
8984c9e283SJeeja KP 
90*cd63655eSVinod Koul 	/* check for extended manifest */
91*cd63655eSVinod Koul 	stripped_fw.data = ctx->fw->data;
92*cd63655eSVinod Koul 	stripped_fw.size = ctx->fw->size;
93*cd63655eSVinod Koul 
94*cd63655eSVinod Koul 	skl_dsp_strip_extended_manifest(&stripped_fw);
95*cd63655eSVinod Koul 
9684c9e283SJeeja KP 	ret = skl_dsp_boot(ctx);
9784c9e283SJeeja KP 	if (ret < 0) {
9884c9e283SJeeja KP 		dev_err(ctx->dev, "Boot dsp core failed ret: %d", ret);
9984c9e283SJeeja KP 		goto skl_load_base_firmware_failed;
10084c9e283SJeeja KP 	}
10184c9e283SJeeja KP 
10284c9e283SJeeja KP 	ret = skl_cldma_prepare(ctx);
10384c9e283SJeeja KP 	if (ret < 0) {
10484c9e283SJeeja KP 		dev_err(ctx->dev, "CL dma prepare failed : %d", ret);
10584c9e283SJeeja KP 		goto skl_load_base_firmware_failed;
10684c9e283SJeeja KP 	}
107a750ba5fSSubhransu S. Prusty 
108a750ba5fSSubhransu S. Prusty 	/* enable Interrupt */
109a750ba5fSSubhransu S. Prusty 	skl_ipc_int_enable(ctx);
110a750ba5fSSubhransu S. Prusty 	skl_ipc_op_int_enable(ctx);
111a750ba5fSSubhransu S. Prusty 
112a750ba5fSSubhransu S. Prusty 	/* check ROM Status */
113a750ba5fSSubhransu S. Prusty 	for (i = SKL_INIT_TIMEOUT; i > 0; --i) {
114a750ba5fSSubhransu S. Prusty 		if (skl_check_fw_status(ctx, SKL_FW_INIT)) {
115a750ba5fSSubhransu S. Prusty 			dev_dbg(ctx->dev,
116a750ba5fSSubhransu S. Prusty 				"ROM loaded, we can continue with FW loading\n");
117a750ba5fSSubhransu S. Prusty 			break;
118a750ba5fSSubhransu S. Prusty 		}
119a750ba5fSSubhransu S. Prusty 		mdelay(1);
120a750ba5fSSubhransu S. Prusty 	}
121a750ba5fSSubhransu S. Prusty 	if (!i) {
122a750ba5fSSubhransu S. Prusty 		reg = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS);
123a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev,
124a750ba5fSSubhransu S. Prusty 			"Timeout waiting for ROM init done, reg:0x%x\n", reg);
125a750ba5fSSubhransu S. Prusty 		ret = -EIO;
126ae395937SJeeja KP 		goto transfer_firmware_failed;
127a750ba5fSSubhransu S. Prusty 	}
128a750ba5fSSubhransu S. Prusty 
129*cd63655eSVinod Koul 	ret = skl_transfer_firmware(ctx, stripped_fw.data, stripped_fw.size);
130a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
131a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "Transfer firmware failed%d\n", ret);
132ae395937SJeeja KP 		goto transfer_firmware_failed;
133a750ba5fSSubhransu S. Prusty 	} else {
134a750ba5fSSubhransu S. Prusty 		ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
135a750ba5fSSubhransu S. Prusty 					msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
136a750ba5fSSubhransu S. Prusty 		if (ret == 0) {
137a750ba5fSSubhransu S. Prusty 			dev_err(ctx->dev, "DSP boot failed, FW Ready timed-out\n");
138a750ba5fSSubhransu S. Prusty 			ret = -EIO;
139ae395937SJeeja KP 			goto transfer_firmware_failed;
140a750ba5fSSubhransu S. Prusty 		}
141a750ba5fSSubhransu S. Prusty 
142a750ba5fSSubhransu S. Prusty 		dev_dbg(ctx->dev, "Download firmware successful%d\n", ret);
143a750ba5fSSubhransu S. Prusty 		skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
144a750ba5fSSubhransu S. Prusty 	}
145a750ba5fSSubhransu S. Prusty 	return 0;
146ae395937SJeeja KP transfer_firmware_failed:
147ae395937SJeeja KP 	ctx->cl_dev.ops.cl_cleanup_controller(ctx);
148a750ba5fSSubhransu S. Prusty skl_load_base_firmware_failed:
149a750ba5fSSubhransu S. Prusty 	skl_dsp_disable_core(ctx);
15084c9e283SJeeja KP 	release_firmware(ctx->fw);
15184c9e283SJeeja KP 	ctx->fw = NULL;
152a750ba5fSSubhransu S. Prusty 	return ret;
153a750ba5fSSubhransu S. Prusty }
154a750ba5fSSubhransu S. Prusty 
155a750ba5fSSubhransu S. Prusty static int skl_set_dsp_D0(struct sst_dsp *ctx)
156a750ba5fSSubhransu S. Prusty {
157a750ba5fSSubhransu S. Prusty 	int ret;
158a750ba5fSSubhransu S. Prusty 
159a750ba5fSSubhransu S. Prusty 	ret = skl_load_base_firmware(ctx);
160a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
161a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "unable to load firmware\n");
162a750ba5fSSubhransu S. Prusty 		return ret;
163a750ba5fSSubhransu S. Prusty 	}
164a750ba5fSSubhransu S. Prusty 
165a750ba5fSSubhransu S. Prusty 	skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
166a750ba5fSSubhransu S. Prusty 
167a750ba5fSSubhransu S. Prusty 	return ret;
168a750ba5fSSubhransu S. Prusty }
169a750ba5fSSubhransu S. Prusty 
170a750ba5fSSubhransu S. Prusty static int skl_set_dsp_D3(struct sst_dsp *ctx)
171a750ba5fSSubhransu S. Prusty {
172a750ba5fSSubhransu S. Prusty 	int ret;
173a750ba5fSSubhransu S. Prusty 	struct skl_ipc_dxstate_info dx;
174a750ba5fSSubhransu S. Prusty 	struct skl_sst *skl = ctx->thread_context;
175a750ba5fSSubhransu S. Prusty 
176a750ba5fSSubhransu S. Prusty 	dev_dbg(ctx->dev, "In %s:\n", __func__);
177a750ba5fSSubhransu S. Prusty 	mutex_lock(&ctx->mutex);
178a750ba5fSSubhransu S. Prusty 	if (!is_skl_dsp_running(ctx)) {
179a750ba5fSSubhransu S. Prusty 		mutex_unlock(&ctx->mutex);
180a750ba5fSSubhransu S. Prusty 		return 0;
181a750ba5fSSubhransu S. Prusty 	}
182a750ba5fSSubhransu S. Prusty 	mutex_unlock(&ctx->mutex);
183a750ba5fSSubhransu S. Prusty 
184a750ba5fSSubhransu S. Prusty 	dx.core_mask = SKL_DSP_CORE0_MASK;
185a750ba5fSSubhransu S. Prusty 	dx.dx_mask = SKL_IPC_D3_MASK;
186a750ba5fSSubhransu S. Prusty 	ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID, SKL_BASE_FW_MODULE_ID, &dx);
18753afce2cSJeeja KP 	if (ret < 0)
18853afce2cSJeeja KP 		dev_err(ctx->dev,
18953afce2cSJeeja KP 			"D3 request to FW failed, continuing reset: %d", ret);
19053afce2cSJeeja KP 
19153afce2cSJeeja KP 	/* disable Interrupt */
19253afce2cSJeeja KP 	ctx->cl_dev.ops.cl_cleanup_controller(ctx);
19353afce2cSJeeja KP 	skl_cldma_int_disable(ctx);
19453afce2cSJeeja KP 	skl_ipc_op_int_disable(ctx);
19553afce2cSJeeja KP 	skl_ipc_int_disable(ctx);
196a750ba5fSSubhransu S. Prusty 
197a750ba5fSSubhransu S. Prusty 	ret = skl_dsp_disable_core(ctx);
198a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
199a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "disable dsp core failed ret: %d\n", ret);
200a750ba5fSSubhransu S. Prusty 		ret = -EIO;
201a750ba5fSSubhransu S. Prusty 	}
202a750ba5fSSubhransu S. Prusty 	skl_dsp_set_state_locked(ctx, SKL_DSP_RESET);
203a750ba5fSSubhransu S. Prusty 
204a750ba5fSSubhransu S. Prusty 	return ret;
205a750ba5fSSubhransu S. Prusty }
206a750ba5fSSubhransu S. Prusty 
207a750ba5fSSubhransu S. Prusty static unsigned int skl_get_errorcode(struct sst_dsp *ctx)
208a750ba5fSSubhransu S. Prusty {
209a750ba5fSSubhransu S. Prusty 	 return sst_dsp_shim_read(ctx, SKL_ADSP_ERROR_CODE);
210a750ba5fSSubhransu S. Prusty }
211a750ba5fSSubhransu S. Prusty 
2126c5768b3SDharageswari R /*
2136c5768b3SDharageswari R  * since get/set_module are called from DAPM context,
2146c5768b3SDharageswari R  * we don't need lock for usage count
2156c5768b3SDharageswari R  */
216db4e5613SDan Carpenter static int skl_get_module(struct sst_dsp *ctx, u16 mod_id)
2176c5768b3SDharageswari R {
2186c5768b3SDharageswari R 	struct skl_module_table *module;
2196c5768b3SDharageswari R 
2206c5768b3SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
2216c5768b3SDharageswari R 		if (module->mod_info->mod_id == mod_id)
2226c5768b3SDharageswari R 			return ++module->usage_cnt;
2236c5768b3SDharageswari R 	}
2246c5768b3SDharageswari R 
2256c5768b3SDharageswari R 	return -EINVAL;
2266c5768b3SDharageswari R }
2276c5768b3SDharageswari R 
228db4e5613SDan Carpenter static int skl_put_module(struct sst_dsp *ctx, u16 mod_id)
2296c5768b3SDharageswari R {
2306c5768b3SDharageswari R 	struct skl_module_table *module;
2316c5768b3SDharageswari R 
2326c5768b3SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
2336c5768b3SDharageswari R 		if (module->mod_info->mod_id == mod_id)
2346c5768b3SDharageswari R 			return --module->usage_cnt;
2356c5768b3SDharageswari R 	}
2366c5768b3SDharageswari R 
2376c5768b3SDharageswari R 	return -EINVAL;
2386c5768b3SDharageswari R }
2396c5768b3SDharageswari R 
2406c5768b3SDharageswari R static struct skl_module_table *skl_fill_module_table(struct sst_dsp *ctx,
2416c5768b3SDharageswari R 						char *mod_name, int mod_id)
2426c5768b3SDharageswari R {
2436c5768b3SDharageswari R 	const struct firmware *fw;
2446c5768b3SDharageswari R 	struct skl_module_table *skl_module;
2456c5768b3SDharageswari R 	unsigned int size;
2466c5768b3SDharageswari R 	int ret;
2476c5768b3SDharageswari R 
2486c5768b3SDharageswari R 	ret = request_firmware(&fw, mod_name, ctx->dev);
2496c5768b3SDharageswari R 	if (ret < 0) {
2506c5768b3SDharageswari R 		dev_err(ctx->dev, "Request Module %s failed :%d\n",
2516c5768b3SDharageswari R 							mod_name, ret);
2526c5768b3SDharageswari R 		return NULL;
2536c5768b3SDharageswari R 	}
2546c5768b3SDharageswari R 
2556c5768b3SDharageswari R 	skl_module = devm_kzalloc(ctx->dev, sizeof(*skl_module), GFP_KERNEL);
2566c5768b3SDharageswari R 	if (skl_module == NULL) {
2576c5768b3SDharageswari R 		release_firmware(fw);
2586c5768b3SDharageswari R 		return NULL;
2596c5768b3SDharageswari R 	}
2606c5768b3SDharageswari R 
2616c5768b3SDharageswari R 	size = sizeof(*skl_module->mod_info);
2626c5768b3SDharageswari R 	skl_module->mod_info = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
2636c5768b3SDharageswari R 	if (skl_module->mod_info == NULL) {
2646c5768b3SDharageswari R 		release_firmware(fw);
2656c5768b3SDharageswari R 		return NULL;
2666c5768b3SDharageswari R 	}
2676c5768b3SDharageswari R 
2686c5768b3SDharageswari R 	skl_module->mod_info->mod_id = mod_id;
2696c5768b3SDharageswari R 	skl_module->mod_info->fw = fw;
2706c5768b3SDharageswari R 	list_add(&skl_module->list, &ctx->module_list);
2716c5768b3SDharageswari R 
2726c5768b3SDharageswari R 	return skl_module;
2736c5768b3SDharageswari R }
2746c5768b3SDharageswari R 
2756c5768b3SDharageswari R /* get a module from it's unique ID */
2766c5768b3SDharageswari R static struct skl_module_table *skl_module_get_from_id(
2776c5768b3SDharageswari R 			struct sst_dsp *ctx, u16 mod_id)
2786c5768b3SDharageswari R {
2796c5768b3SDharageswari R 	struct skl_module_table *module;
2806c5768b3SDharageswari R 
2816c5768b3SDharageswari R 	if (list_empty(&ctx->module_list)) {
2826c5768b3SDharageswari R 		dev_err(ctx->dev, "Module list is empty\n");
2836c5768b3SDharageswari R 		return NULL;
2846c5768b3SDharageswari R 	}
2856c5768b3SDharageswari R 
2866c5768b3SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
2876c5768b3SDharageswari R 		if (module->mod_info->mod_id == mod_id)
2886c5768b3SDharageswari R 			return module;
2896c5768b3SDharageswari R 	}
2906c5768b3SDharageswari R 
2916c5768b3SDharageswari R 	return NULL;
2926c5768b3SDharageswari R }
2936c5768b3SDharageswari R 
2946c5768b3SDharageswari R static int skl_transfer_module(struct sst_dsp *ctx,
2956c5768b3SDharageswari R 			struct skl_load_module_info *module)
2966c5768b3SDharageswari R {
2976c5768b3SDharageswari R 	int ret;
2986c5768b3SDharageswari R 	struct skl_sst *skl = ctx->thread_context;
2996c5768b3SDharageswari R 
3006c5768b3SDharageswari R 	ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, module->fw->data,
3016c5768b3SDharageswari R 							module->fw->size);
3026c5768b3SDharageswari R 	if (ret < 0)
3036c5768b3SDharageswari R 		return ret;
3046c5768b3SDharageswari R 
3056c5768b3SDharageswari R 	ret = skl_ipc_load_modules(&skl->ipc, SKL_NUM_MODULES,
3066c5768b3SDharageswari R 						(void *)&module->mod_id);
3076c5768b3SDharageswari R 	if (ret < 0)
3086c5768b3SDharageswari R 		dev_err(ctx->dev, "Failed to Load module: %d\n", ret);
3096c5768b3SDharageswari R 
3106c5768b3SDharageswari R 	ctx->cl_dev.ops.cl_stop_dma(ctx);
3116c5768b3SDharageswari R 
3126c5768b3SDharageswari R 	return ret;
3136c5768b3SDharageswari R }
3146c5768b3SDharageswari R 
31509305da9SShreyas NC static int skl_load_module(struct sst_dsp *ctx, u16 mod_id, u8 *guid)
3166c5768b3SDharageswari R {
3176c5768b3SDharageswari R 	struct skl_module_table *module_entry = NULL;
3186c5768b3SDharageswari R 	int ret = 0;
3196c5768b3SDharageswari R 	char mod_name[64]; /* guid str = 32 chars + 4 hyphens */
32009305da9SShreyas NC 	uuid_le *uuid_mod;
3216c5768b3SDharageswari R 
32209305da9SShreyas NC 	uuid_mod = (uuid_le *)guid;
32309305da9SShreyas NC 	snprintf(mod_name, sizeof(mod_name), "%s%pUL%s",
32409305da9SShreyas NC 				"intel/dsp_fw_", uuid_mod, ".bin");
3256c5768b3SDharageswari R 
3266c5768b3SDharageswari R 	module_entry = skl_module_get_from_id(ctx, mod_id);
3276c5768b3SDharageswari R 	if (module_entry == NULL) {
3286c5768b3SDharageswari R 		module_entry = skl_fill_module_table(ctx, mod_name, mod_id);
3296c5768b3SDharageswari R 		if (module_entry == NULL) {
3306c5768b3SDharageswari R 			dev_err(ctx->dev, "Failed to Load module\n");
3316c5768b3SDharageswari R 			return -EINVAL;
3326c5768b3SDharageswari R 		}
3336c5768b3SDharageswari R 	}
3346c5768b3SDharageswari R 
3356c5768b3SDharageswari R 	if (!module_entry->usage_cnt) {
3366c5768b3SDharageswari R 		ret = skl_transfer_module(ctx, module_entry->mod_info);
3376c5768b3SDharageswari R 		if (ret < 0) {
3386c5768b3SDharageswari R 			dev_err(ctx->dev, "Failed to Load module\n");
3396c5768b3SDharageswari R 			return ret;
3406c5768b3SDharageswari R 		}
3416c5768b3SDharageswari R 	}
3426c5768b3SDharageswari R 
3436c5768b3SDharageswari R 	ret = skl_get_module(ctx, mod_id);
3446c5768b3SDharageswari R 
3456c5768b3SDharageswari R 	return ret;
3466c5768b3SDharageswari R }
3476c5768b3SDharageswari R 
3486c5768b3SDharageswari R static int skl_unload_module(struct sst_dsp *ctx, u16 mod_id)
3496c5768b3SDharageswari R {
350db4e5613SDan Carpenter 	int usage_cnt;
3516c5768b3SDharageswari R 	struct skl_sst *skl = ctx->thread_context;
3526c5768b3SDharageswari R 	int ret = 0;
3536c5768b3SDharageswari R 
3546c5768b3SDharageswari R 	usage_cnt = skl_put_module(ctx, mod_id);
3556c5768b3SDharageswari R 	if (usage_cnt < 0) {
3566c5768b3SDharageswari R 		dev_err(ctx->dev, "Module bad usage cnt!:%d\n", usage_cnt);
3576c5768b3SDharageswari R 		return -EIO;
3586c5768b3SDharageswari R 	}
3596c5768b3SDharageswari R 	ret = skl_ipc_unload_modules(&skl->ipc,
3606c5768b3SDharageswari R 			SKL_NUM_MODULES, &mod_id);
3616c5768b3SDharageswari R 	if (ret < 0) {
3626c5768b3SDharageswari R 		dev_err(ctx->dev, "Failed to UnLoad module\n");
3636c5768b3SDharageswari R 		skl_get_module(ctx, mod_id);
3646c5768b3SDharageswari R 		return ret;
3656c5768b3SDharageswari R 	}
3666c5768b3SDharageswari R 
3676c5768b3SDharageswari R 	return ret;
3686c5768b3SDharageswari R }
3696c5768b3SDharageswari R 
3706c5768b3SDharageswari R static void skl_clear_module_table(struct sst_dsp *ctx)
3716c5768b3SDharageswari R {
3726c5768b3SDharageswari R 	struct skl_module_table *module, *tmp;
3736c5768b3SDharageswari R 
3746c5768b3SDharageswari R 	if (list_empty(&ctx->module_list))
3756c5768b3SDharageswari R 		return;
3766c5768b3SDharageswari R 
3776c5768b3SDharageswari R 	list_for_each_entry_safe(module, tmp, &ctx->module_list, list) {
3786c5768b3SDharageswari R 		list_del(&module->list);
3796c5768b3SDharageswari R 		release_firmware(module->mod_info->fw);
3806c5768b3SDharageswari R 	}
3816c5768b3SDharageswari R }
3826c5768b3SDharageswari R 
383a750ba5fSSubhransu S. Prusty static struct skl_dsp_fw_ops skl_fw_ops = {
384a750ba5fSSubhransu S. Prusty 	.set_state_D0 = skl_set_dsp_D0,
385a750ba5fSSubhransu S. Prusty 	.set_state_D3 = skl_set_dsp_D3,
386a750ba5fSSubhransu S. Prusty 	.load_fw = skl_load_base_firmware,
387a750ba5fSSubhransu S. Prusty 	.get_fw_errcode = skl_get_errorcode,
3886c5768b3SDharageswari R 	.load_mod = skl_load_module,
3896c5768b3SDharageswari R 	.unload_mod = skl_unload_module,
390a750ba5fSSubhransu S. Prusty };
391a750ba5fSSubhransu S. Prusty 
392a750ba5fSSubhransu S. Prusty static struct sst_ops skl_ops = {
393a750ba5fSSubhransu S. Prusty 	.irq_handler = skl_dsp_sst_interrupt,
394a750ba5fSSubhransu S. Prusty 	.write = sst_shim32_write,
395a750ba5fSSubhransu S. Prusty 	.read = sst_shim32_read,
396a750ba5fSSubhransu S. Prusty 	.ram_read = sst_memcpy_fromio_32,
397a750ba5fSSubhransu S. Prusty 	.ram_write = sst_memcpy_toio_32,
398a750ba5fSSubhransu S. Prusty 	.free = skl_dsp_free,
399a750ba5fSSubhransu S. Prusty };
400a750ba5fSSubhransu S. Prusty 
401a750ba5fSSubhransu S. Prusty static struct sst_dsp_device skl_dev = {
402a750ba5fSSubhransu S. Prusty 	.thread = skl_dsp_irq_thread_handler,
403a750ba5fSSubhransu S. Prusty 	.ops = &skl_ops,
404a750ba5fSSubhransu S. Prusty };
405a750ba5fSSubhransu S. Prusty 
406a750ba5fSSubhransu S. Prusty int skl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
407aecf6fd8SVinod Koul 		const char *fw_name, struct skl_dsp_loader_ops dsp_ops, struct skl_sst **dsp)
408a750ba5fSSubhransu S. Prusty {
409a750ba5fSSubhransu S. Prusty 	struct skl_sst *skl;
410a750ba5fSSubhransu S. Prusty 	struct sst_dsp *sst;
411a750ba5fSSubhransu S. Prusty 	int ret;
412a750ba5fSSubhransu S. Prusty 
413a750ba5fSSubhransu S. Prusty 	skl = devm_kzalloc(dev, sizeof(*skl), GFP_KERNEL);
414a750ba5fSSubhransu S. Prusty 	if (skl == NULL)
415a750ba5fSSubhransu S. Prusty 		return -ENOMEM;
416a750ba5fSSubhransu S. Prusty 
417a750ba5fSSubhransu S. Prusty 	skl->dev = dev;
418a750ba5fSSubhransu S. Prusty 	skl_dev.thread_context = skl;
419a750ba5fSSubhransu S. Prusty 
420a750ba5fSSubhransu S. Prusty 	skl->dsp = skl_dsp_ctx_init(dev, &skl_dev, irq);
421a750ba5fSSubhransu S. Prusty 	if (!skl->dsp) {
422a750ba5fSSubhransu S. Prusty 		dev_err(skl->dev, "%s: no device\n", __func__);
423a750ba5fSSubhransu S. Prusty 		return -ENODEV;
424a750ba5fSSubhransu S. Prusty 	}
425a750ba5fSSubhransu S. Prusty 
426a750ba5fSSubhransu S. Prusty 	sst = skl->dsp;
427a750ba5fSSubhransu S. Prusty 
428aecf6fd8SVinod Koul 	sst->fw_name = fw_name;
429a750ba5fSSubhransu S. Prusty 	sst->addr.lpe = mmio_base;
430a750ba5fSSubhransu S. Prusty 	sst->addr.shim = mmio_base;
431a750ba5fSSubhransu S. Prusty 	sst_dsp_mailbox_init(sst, (SKL_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
432a750ba5fSSubhransu S. Prusty 			SKL_ADSP_W0_UP_SZ, SKL_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
433a750ba5fSSubhransu S. Prusty 
4346c5768b3SDharageswari R 	INIT_LIST_HEAD(&sst->module_list);
435a750ba5fSSubhransu S. Prusty 	sst->dsp_ops = dsp_ops;
436a750ba5fSSubhransu S. Prusty 	sst->fw_ops = skl_fw_ops;
437a750ba5fSSubhransu S. Prusty 
438a750ba5fSSubhransu S. Prusty 	ret = skl_ipc_init(dev, skl);
439a750ba5fSSubhransu S. Prusty 	if (ret)
440a750ba5fSSubhransu S. Prusty 		return ret;
441a750ba5fSSubhransu S. Prusty 
442a750ba5fSSubhransu S. Prusty 	ret = sst->fw_ops.load_fw(sst);
443a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
444a750ba5fSSubhransu S. Prusty 		dev_err(dev, "Load base fw failed : %d", ret);
445b4fe965fSSubhransu S. Prusty 		goto cleanup;
446a750ba5fSSubhransu S. Prusty 	}
447a750ba5fSSubhransu S. Prusty 
448a750ba5fSSubhransu S. Prusty 	if (dsp)
449a750ba5fSSubhransu S. Prusty 		*dsp = skl;
450a750ba5fSSubhransu S. Prusty 
451b4fe965fSSubhransu S. Prusty 	return ret;
452a750ba5fSSubhransu S. Prusty 
453b4fe965fSSubhransu S. Prusty cleanup:
454b4fe965fSSubhransu S. Prusty 	skl_sst_dsp_cleanup(dev, skl);
455a750ba5fSSubhransu S. Prusty 	return ret;
456a750ba5fSSubhransu S. Prusty }
457a750ba5fSSubhransu S. Prusty EXPORT_SYMBOL_GPL(skl_sst_dsp_init);
458a750ba5fSSubhransu S. Prusty 
459a750ba5fSSubhransu S. Prusty void skl_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
460a750ba5fSSubhransu S. Prusty {
4616c5768b3SDharageswari R 	skl_clear_module_table(ctx->dsp);
462a750ba5fSSubhransu S. Prusty 	skl_ipc_free(&ctx->ipc);
463a750ba5fSSubhransu S. Prusty 	ctx->dsp->ops->free(ctx->dsp);
46495536d8cSDharageswari.R 	if (ctx->boot_complete) {
46595536d8cSDharageswari.R 		ctx->dsp->cl_dev.ops.cl_cleanup_controller(ctx->dsp);
46695536d8cSDharageswari.R 		skl_cldma_int_disable(ctx->dsp);
46795536d8cSDharageswari.R 	}
468a750ba5fSSubhransu S. Prusty }
469a750ba5fSSubhransu S. Prusty EXPORT_SYMBOL_GPL(skl_sst_dsp_cleanup);
470a750ba5fSSubhransu S. Prusty 
471a750ba5fSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
472a750ba5fSSubhransu S. Prusty MODULE_DESCRIPTION("Intel Skylake IPC driver");
473