xref: /openbmc/linux/sound/soc/intel/skylake/skl-sst.c (revision 6c5768b3aa6f554a719834591ad2c6b4e1291397)
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>
23a750ba5fSSubhransu S. Prusty #include "../common/sst-dsp.h"
24a750ba5fSSubhransu S. Prusty #include "../common/sst-dsp-priv.h"
25a750ba5fSSubhransu S. Prusty #include "../common/sst-ipc.h"
26a750ba5fSSubhransu S. Prusty #include "skl-sst-ipc.h"
27a750ba5fSSubhransu S. Prusty 
28a750ba5fSSubhransu S. Prusty #define SKL_BASEFW_TIMEOUT	300
29a750ba5fSSubhransu S. Prusty #define SKL_INIT_TIMEOUT	1000
30a750ba5fSSubhransu S. Prusty 
31a750ba5fSSubhransu S. Prusty /* Intel HD Audio SRAM Window 0*/
32a750ba5fSSubhransu S. Prusty #define SKL_ADSP_SRAM0_BASE	0x8000
33a750ba5fSSubhransu S. Prusty 
34a750ba5fSSubhransu S. Prusty /* Firmware status window */
35a750ba5fSSubhransu S. Prusty #define SKL_ADSP_FW_STATUS	SKL_ADSP_SRAM0_BASE
36a750ba5fSSubhransu S. Prusty #define SKL_ADSP_ERROR_CODE	(SKL_ADSP_FW_STATUS + 0x4)
37a750ba5fSSubhransu S. Prusty 
38a750ba5fSSubhransu S. Prusty #define SKL_INSTANCE_ID		0
39a750ba5fSSubhransu S. Prusty #define SKL_BASE_FW_MODULE_ID	0
40a750ba5fSSubhransu S. Prusty 
41*6c5768b3SDharageswari R #define SKL_NUM_MODULES		1
42*6c5768b3SDharageswari R 
43a750ba5fSSubhransu S. Prusty static bool skl_check_fw_status(struct sst_dsp *ctx, u32 status)
44a750ba5fSSubhransu S. Prusty {
45a750ba5fSSubhransu S. Prusty 	u32 cur_sts;
46a750ba5fSSubhransu S. Prusty 
47a750ba5fSSubhransu S. Prusty 	cur_sts = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS) & SKL_FW_STS_MASK;
48a750ba5fSSubhransu S. Prusty 
49a750ba5fSSubhransu S. Prusty 	return (cur_sts == status);
50a750ba5fSSubhransu S. Prusty }
51a750ba5fSSubhransu S. Prusty 
52a750ba5fSSubhransu S. Prusty static int skl_transfer_firmware(struct sst_dsp *ctx,
53a750ba5fSSubhransu S. Prusty 		const void *basefw, u32 base_fw_size)
54a750ba5fSSubhransu S. Prusty {
55a750ba5fSSubhransu S. Prusty 	int ret = 0;
56a750ba5fSSubhransu S. Prusty 
57a750ba5fSSubhransu S. Prusty 	ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, basefw, base_fw_size);
58a750ba5fSSubhransu S. Prusty 	if (ret < 0)
59a750ba5fSSubhransu S. Prusty 		return ret;
60a750ba5fSSubhransu S. Prusty 
61a750ba5fSSubhransu S. Prusty 	ret = sst_dsp_register_poll(ctx,
62a750ba5fSSubhransu S. Prusty 			SKL_ADSP_FW_STATUS,
63a750ba5fSSubhransu S. Prusty 			SKL_FW_STS_MASK,
64a750ba5fSSubhransu S. Prusty 			SKL_FW_RFW_START,
65a750ba5fSSubhransu S. Prusty 			SKL_BASEFW_TIMEOUT,
66a750ba5fSSubhransu S. Prusty 			"Firmware boot");
67a750ba5fSSubhransu S. Prusty 
68a750ba5fSSubhransu S. Prusty 	ctx->cl_dev.ops.cl_stop_dma(ctx);
69a750ba5fSSubhransu S. Prusty 
70a750ba5fSSubhransu S. Prusty 	return ret;
71a750ba5fSSubhransu S. Prusty }
72a750ba5fSSubhransu S. Prusty 
73a750ba5fSSubhransu S. Prusty static int skl_load_base_firmware(struct sst_dsp *ctx)
74a750ba5fSSubhransu S. Prusty {
75a750ba5fSSubhransu S. Prusty 	int ret = 0, i;
76a750ba5fSSubhransu S. Prusty 	struct skl_sst *skl = ctx->thread_context;
77a750ba5fSSubhransu S. Prusty 	u32 reg;
78a750ba5fSSubhransu S. Prusty 
7984c9e283SJeeja KP 	skl->boot_complete = false;
8084c9e283SJeeja KP 	init_waitqueue_head(&skl->boot_wait);
8184c9e283SJeeja KP 
8284c9e283SJeeja KP 	if (ctx->fw == NULL) {
83aecf6fd8SVinod Koul 		ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
84a750ba5fSSubhransu S. Prusty 		if (ret < 0) {
85a750ba5fSSubhransu S. Prusty 			dev_err(ctx->dev, "Request firmware failed %d\n", ret);
86a750ba5fSSubhransu S. Prusty 			skl_dsp_disable_core(ctx);
87a750ba5fSSubhransu S. Prusty 			return -EIO;
88a750ba5fSSubhransu S. Prusty 		}
8984c9e283SJeeja KP 	}
9084c9e283SJeeja KP 
9184c9e283SJeeja KP 	ret = skl_dsp_boot(ctx);
9284c9e283SJeeja KP 	if (ret < 0) {
9384c9e283SJeeja KP 		dev_err(ctx->dev, "Boot dsp core failed ret: %d", ret);
9484c9e283SJeeja KP 		goto skl_load_base_firmware_failed;
9584c9e283SJeeja KP 	}
9684c9e283SJeeja KP 
9784c9e283SJeeja KP 	ret = skl_cldma_prepare(ctx);
9884c9e283SJeeja KP 	if (ret < 0) {
9984c9e283SJeeja KP 		dev_err(ctx->dev, "CL dma prepare failed : %d", ret);
10084c9e283SJeeja KP 		goto skl_load_base_firmware_failed;
10184c9e283SJeeja KP 	}
102a750ba5fSSubhransu S. Prusty 
103a750ba5fSSubhransu S. Prusty 	/* enable Interrupt */
104a750ba5fSSubhransu S. Prusty 	skl_ipc_int_enable(ctx);
105a750ba5fSSubhransu S. Prusty 	skl_ipc_op_int_enable(ctx);
106a750ba5fSSubhransu S. Prusty 
107a750ba5fSSubhransu S. Prusty 	/* check ROM Status */
108a750ba5fSSubhransu S. Prusty 	for (i = SKL_INIT_TIMEOUT; i > 0; --i) {
109a750ba5fSSubhransu S. Prusty 		if (skl_check_fw_status(ctx, SKL_FW_INIT)) {
110a750ba5fSSubhransu S. Prusty 			dev_dbg(ctx->dev,
111a750ba5fSSubhransu S. Prusty 				"ROM loaded, we can continue with FW loading\n");
112a750ba5fSSubhransu S. Prusty 			break;
113a750ba5fSSubhransu S. Prusty 		}
114a750ba5fSSubhransu S. Prusty 		mdelay(1);
115a750ba5fSSubhransu S. Prusty 	}
116a750ba5fSSubhransu S. Prusty 	if (!i) {
117a750ba5fSSubhransu S. Prusty 		reg = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS);
118a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev,
119a750ba5fSSubhransu S. Prusty 			"Timeout waiting for ROM init done, reg:0x%x\n", reg);
120a750ba5fSSubhransu S. Prusty 		ret = -EIO;
121ae395937SJeeja KP 		goto transfer_firmware_failed;
122a750ba5fSSubhransu S. Prusty 	}
123a750ba5fSSubhransu S. Prusty 
12484c9e283SJeeja KP 	ret = skl_transfer_firmware(ctx, ctx->fw->data, ctx->fw->size);
125a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
126a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "Transfer firmware failed%d\n", ret);
127ae395937SJeeja KP 		goto transfer_firmware_failed;
128a750ba5fSSubhransu S. Prusty 	} else {
129a750ba5fSSubhransu S. Prusty 		ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
130a750ba5fSSubhransu S. Prusty 					msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
131a750ba5fSSubhransu S. Prusty 		if (ret == 0) {
132a750ba5fSSubhransu S. Prusty 			dev_err(ctx->dev, "DSP boot failed, FW Ready timed-out\n");
133a750ba5fSSubhransu S. Prusty 			ret = -EIO;
134ae395937SJeeja KP 			goto transfer_firmware_failed;
135a750ba5fSSubhransu S. Prusty 		}
136a750ba5fSSubhransu S. Prusty 
137a750ba5fSSubhransu S. Prusty 		dev_dbg(ctx->dev, "Download firmware successful%d\n", ret);
138a750ba5fSSubhransu S. Prusty 		skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
139a750ba5fSSubhransu S. Prusty 	}
140a750ba5fSSubhransu S. Prusty 	return 0;
141ae395937SJeeja KP transfer_firmware_failed:
142ae395937SJeeja KP 	ctx->cl_dev.ops.cl_cleanup_controller(ctx);
143a750ba5fSSubhransu S. Prusty skl_load_base_firmware_failed:
144a750ba5fSSubhransu S. Prusty 	skl_dsp_disable_core(ctx);
14584c9e283SJeeja KP 	release_firmware(ctx->fw);
14684c9e283SJeeja KP 	ctx->fw = NULL;
147a750ba5fSSubhransu S. Prusty 	return ret;
148a750ba5fSSubhransu S. Prusty }
149a750ba5fSSubhransu S. Prusty 
150a750ba5fSSubhransu S. Prusty static int skl_set_dsp_D0(struct sst_dsp *ctx)
151a750ba5fSSubhransu S. Prusty {
152a750ba5fSSubhransu S. Prusty 	int ret;
153a750ba5fSSubhransu S. Prusty 
154a750ba5fSSubhransu S. Prusty 	ret = skl_load_base_firmware(ctx);
155a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
156a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "unable to load firmware\n");
157a750ba5fSSubhransu S. Prusty 		return ret;
158a750ba5fSSubhransu S. Prusty 	}
159a750ba5fSSubhransu S. Prusty 
160a750ba5fSSubhransu S. Prusty 	skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
161a750ba5fSSubhransu S. Prusty 
162a750ba5fSSubhransu S. Prusty 	return ret;
163a750ba5fSSubhransu S. Prusty }
164a750ba5fSSubhransu S. Prusty 
165a750ba5fSSubhransu S. Prusty static int skl_set_dsp_D3(struct sst_dsp *ctx)
166a750ba5fSSubhransu S. Prusty {
167a750ba5fSSubhransu S. Prusty 	int ret;
168a750ba5fSSubhransu S. Prusty 	struct skl_ipc_dxstate_info dx;
169a750ba5fSSubhransu S. Prusty 	struct skl_sst *skl = ctx->thread_context;
170a750ba5fSSubhransu S. Prusty 
171a750ba5fSSubhransu S. Prusty 	dev_dbg(ctx->dev, "In %s:\n", __func__);
172a750ba5fSSubhransu S. Prusty 	mutex_lock(&ctx->mutex);
173a750ba5fSSubhransu S. Prusty 	if (!is_skl_dsp_running(ctx)) {
174a750ba5fSSubhransu S. Prusty 		mutex_unlock(&ctx->mutex);
175a750ba5fSSubhransu S. Prusty 		return 0;
176a750ba5fSSubhransu S. Prusty 	}
177a750ba5fSSubhransu S. Prusty 	mutex_unlock(&ctx->mutex);
178a750ba5fSSubhransu S. Prusty 
179a750ba5fSSubhransu S. Prusty 	dx.core_mask = SKL_DSP_CORE0_MASK;
180a750ba5fSSubhransu S. Prusty 	dx.dx_mask = SKL_IPC_D3_MASK;
181a750ba5fSSubhransu S. Prusty 	ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID, SKL_BASE_FW_MODULE_ID, &dx);
18253afce2cSJeeja KP 	if (ret < 0)
18353afce2cSJeeja KP 		dev_err(ctx->dev,
18453afce2cSJeeja KP 			"D3 request to FW failed, continuing reset: %d", ret);
18553afce2cSJeeja KP 
18653afce2cSJeeja KP 	/* disable Interrupt */
18753afce2cSJeeja KP 	ctx->cl_dev.ops.cl_cleanup_controller(ctx);
18853afce2cSJeeja KP 	skl_cldma_int_disable(ctx);
18953afce2cSJeeja KP 	skl_ipc_op_int_disable(ctx);
19053afce2cSJeeja KP 	skl_ipc_int_disable(ctx);
191a750ba5fSSubhransu S. Prusty 
192a750ba5fSSubhransu S. Prusty 	ret = skl_dsp_disable_core(ctx);
193a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
194a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "disable dsp core failed ret: %d\n", ret);
195a750ba5fSSubhransu S. Prusty 		ret = -EIO;
196a750ba5fSSubhransu S. Prusty 	}
197a750ba5fSSubhransu S. Prusty 	skl_dsp_set_state_locked(ctx, SKL_DSP_RESET);
198a750ba5fSSubhransu S. Prusty 
199a750ba5fSSubhransu S. Prusty 	return ret;
200a750ba5fSSubhransu S. Prusty }
201a750ba5fSSubhransu S. Prusty 
202a750ba5fSSubhransu S. Prusty static unsigned int skl_get_errorcode(struct sst_dsp *ctx)
203a750ba5fSSubhransu S. Prusty {
204a750ba5fSSubhransu S. Prusty 	 return sst_dsp_shim_read(ctx, SKL_ADSP_ERROR_CODE);
205a750ba5fSSubhransu S. Prusty }
206a750ba5fSSubhransu S. Prusty 
207*6c5768b3SDharageswari R /*
208*6c5768b3SDharageswari R  * since get/set_module are called from DAPM context,
209*6c5768b3SDharageswari R  * we don't need lock for usage count
210*6c5768b3SDharageswari R  */
211*6c5768b3SDharageswari R static unsigned int skl_get_module(struct sst_dsp *ctx, u16 mod_id)
212*6c5768b3SDharageswari R {
213*6c5768b3SDharageswari R 	struct skl_module_table *module;
214*6c5768b3SDharageswari R 
215*6c5768b3SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
216*6c5768b3SDharageswari R 		if (module->mod_info->mod_id == mod_id)
217*6c5768b3SDharageswari R 			return ++module->usage_cnt;
218*6c5768b3SDharageswari R 	}
219*6c5768b3SDharageswari R 
220*6c5768b3SDharageswari R 	return -EINVAL;
221*6c5768b3SDharageswari R }
222*6c5768b3SDharageswari R 
223*6c5768b3SDharageswari R static unsigned int skl_put_module(struct sst_dsp *ctx, u16 mod_id)
224*6c5768b3SDharageswari R {
225*6c5768b3SDharageswari R 	struct skl_module_table *module;
226*6c5768b3SDharageswari R 
227*6c5768b3SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
228*6c5768b3SDharageswari R 		if (module->mod_info->mod_id == mod_id)
229*6c5768b3SDharageswari R 			return --module->usage_cnt;
230*6c5768b3SDharageswari R 	}
231*6c5768b3SDharageswari R 
232*6c5768b3SDharageswari R 	return -EINVAL;
233*6c5768b3SDharageswari R }
234*6c5768b3SDharageswari R 
235*6c5768b3SDharageswari R static struct skl_module_table *skl_fill_module_table(struct sst_dsp *ctx,
236*6c5768b3SDharageswari R 						char *mod_name, int mod_id)
237*6c5768b3SDharageswari R {
238*6c5768b3SDharageswari R 	const struct firmware *fw;
239*6c5768b3SDharageswari R 	struct skl_module_table *skl_module;
240*6c5768b3SDharageswari R 	unsigned int size;
241*6c5768b3SDharageswari R 	int ret;
242*6c5768b3SDharageswari R 
243*6c5768b3SDharageswari R 	ret = request_firmware(&fw, mod_name, ctx->dev);
244*6c5768b3SDharageswari R 	if (ret < 0) {
245*6c5768b3SDharageswari R 		dev_err(ctx->dev, "Request Module %s failed :%d\n",
246*6c5768b3SDharageswari R 							mod_name, ret);
247*6c5768b3SDharageswari R 		return NULL;
248*6c5768b3SDharageswari R 	}
249*6c5768b3SDharageswari R 
250*6c5768b3SDharageswari R 	skl_module = devm_kzalloc(ctx->dev, sizeof(*skl_module), GFP_KERNEL);
251*6c5768b3SDharageswari R 	if (skl_module == NULL) {
252*6c5768b3SDharageswari R 		release_firmware(fw);
253*6c5768b3SDharageswari R 		return NULL;
254*6c5768b3SDharageswari R 	}
255*6c5768b3SDharageswari R 
256*6c5768b3SDharageswari R 	size = sizeof(*skl_module->mod_info);
257*6c5768b3SDharageswari R 	skl_module->mod_info = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
258*6c5768b3SDharageswari R 	if (skl_module->mod_info == NULL) {
259*6c5768b3SDharageswari R 		release_firmware(fw);
260*6c5768b3SDharageswari R 		return NULL;
261*6c5768b3SDharageswari R 	}
262*6c5768b3SDharageswari R 
263*6c5768b3SDharageswari R 	skl_module->mod_info->mod_id = mod_id;
264*6c5768b3SDharageswari R 	skl_module->mod_info->fw = fw;
265*6c5768b3SDharageswari R 	list_add(&skl_module->list, &ctx->module_list);
266*6c5768b3SDharageswari R 
267*6c5768b3SDharageswari R 	return skl_module;
268*6c5768b3SDharageswari R }
269*6c5768b3SDharageswari R 
270*6c5768b3SDharageswari R /* get a module from it's unique ID */
271*6c5768b3SDharageswari R static struct skl_module_table *skl_module_get_from_id(
272*6c5768b3SDharageswari R 			struct sst_dsp *ctx, u16 mod_id)
273*6c5768b3SDharageswari R {
274*6c5768b3SDharageswari R 	struct skl_module_table *module;
275*6c5768b3SDharageswari R 
276*6c5768b3SDharageswari R 	if (list_empty(&ctx->module_list)) {
277*6c5768b3SDharageswari R 		dev_err(ctx->dev, "Module list is empty\n");
278*6c5768b3SDharageswari R 		return NULL;
279*6c5768b3SDharageswari R 	}
280*6c5768b3SDharageswari R 
281*6c5768b3SDharageswari R 	list_for_each_entry(module, &ctx->module_list, list) {
282*6c5768b3SDharageswari R 		if (module->mod_info->mod_id == mod_id)
283*6c5768b3SDharageswari R 			return module;
284*6c5768b3SDharageswari R 	}
285*6c5768b3SDharageswari R 
286*6c5768b3SDharageswari R 	return NULL;
287*6c5768b3SDharageswari R }
288*6c5768b3SDharageswari R 
289*6c5768b3SDharageswari R static int skl_transfer_module(struct sst_dsp *ctx,
290*6c5768b3SDharageswari R 			struct skl_load_module_info *module)
291*6c5768b3SDharageswari R {
292*6c5768b3SDharageswari R 	int ret;
293*6c5768b3SDharageswari R 	struct skl_sst *skl = ctx->thread_context;
294*6c5768b3SDharageswari R 
295*6c5768b3SDharageswari R 	ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, module->fw->data,
296*6c5768b3SDharageswari R 							module->fw->size);
297*6c5768b3SDharageswari R 	if (ret < 0)
298*6c5768b3SDharageswari R 		return ret;
299*6c5768b3SDharageswari R 
300*6c5768b3SDharageswari R 	ret = skl_ipc_load_modules(&skl->ipc, SKL_NUM_MODULES,
301*6c5768b3SDharageswari R 						(void *)&module->mod_id);
302*6c5768b3SDharageswari R 	if (ret < 0)
303*6c5768b3SDharageswari R 		dev_err(ctx->dev, "Failed to Load module: %d\n", ret);
304*6c5768b3SDharageswari R 
305*6c5768b3SDharageswari R 	ctx->cl_dev.ops.cl_stop_dma(ctx);
306*6c5768b3SDharageswari R 
307*6c5768b3SDharageswari R 	return ret;
308*6c5768b3SDharageswari R }
309*6c5768b3SDharageswari R 
310*6c5768b3SDharageswari R static int skl_load_module(struct sst_dsp *ctx, u16 mod_id, char *guid)
311*6c5768b3SDharageswari R {
312*6c5768b3SDharageswari R 	struct skl_module_table *module_entry = NULL;
313*6c5768b3SDharageswari R 	int ret = 0;
314*6c5768b3SDharageswari R 	char mod_name[64]; /* guid str = 32 chars + 4 hyphens */
315*6c5768b3SDharageswari R 
316*6c5768b3SDharageswari R 	snprintf(mod_name, sizeof(mod_name), "%s%s%s",
317*6c5768b3SDharageswari R 			"intel/dsp_fw_", guid, ".bin");
318*6c5768b3SDharageswari R 
319*6c5768b3SDharageswari R 	module_entry = skl_module_get_from_id(ctx, mod_id);
320*6c5768b3SDharageswari R 	if (module_entry == NULL) {
321*6c5768b3SDharageswari R 		module_entry = skl_fill_module_table(ctx, mod_name, mod_id);
322*6c5768b3SDharageswari R 		if (module_entry == NULL) {
323*6c5768b3SDharageswari R 			dev_err(ctx->dev, "Failed to Load module\n");
324*6c5768b3SDharageswari R 			return -EINVAL;
325*6c5768b3SDharageswari R 		}
326*6c5768b3SDharageswari R 	}
327*6c5768b3SDharageswari R 
328*6c5768b3SDharageswari R 	if (!module_entry->usage_cnt) {
329*6c5768b3SDharageswari R 		ret = skl_transfer_module(ctx, module_entry->mod_info);
330*6c5768b3SDharageswari R 		if (ret < 0) {
331*6c5768b3SDharageswari R 			dev_err(ctx->dev, "Failed to Load module\n");
332*6c5768b3SDharageswari R 			return ret;
333*6c5768b3SDharageswari R 		}
334*6c5768b3SDharageswari R 	}
335*6c5768b3SDharageswari R 
336*6c5768b3SDharageswari R 	ret = skl_get_module(ctx, mod_id);
337*6c5768b3SDharageswari R 
338*6c5768b3SDharageswari R 	return ret;
339*6c5768b3SDharageswari R }
340*6c5768b3SDharageswari R 
341*6c5768b3SDharageswari R static int skl_unload_module(struct sst_dsp *ctx, u16 mod_id)
342*6c5768b3SDharageswari R {
343*6c5768b3SDharageswari R 	unsigned int usage_cnt;
344*6c5768b3SDharageswari R 	struct skl_sst *skl = ctx->thread_context;
345*6c5768b3SDharageswari R 	int ret = 0;
346*6c5768b3SDharageswari R 
347*6c5768b3SDharageswari R 	usage_cnt = skl_put_module(ctx, mod_id);
348*6c5768b3SDharageswari R 	if (usage_cnt < 0) {
349*6c5768b3SDharageswari R 		dev_err(ctx->dev, "Module bad usage cnt!:%d\n", usage_cnt);
350*6c5768b3SDharageswari R 		return -EIO;
351*6c5768b3SDharageswari R 	}
352*6c5768b3SDharageswari R 	ret = skl_ipc_unload_modules(&skl->ipc,
353*6c5768b3SDharageswari R 			SKL_NUM_MODULES, &mod_id);
354*6c5768b3SDharageswari R 	if (ret < 0) {
355*6c5768b3SDharageswari R 		dev_err(ctx->dev, "Failed to UnLoad module\n");
356*6c5768b3SDharageswari R 		skl_get_module(ctx, mod_id);
357*6c5768b3SDharageswari R 		return ret;
358*6c5768b3SDharageswari R 	}
359*6c5768b3SDharageswari R 
360*6c5768b3SDharageswari R 	return ret;
361*6c5768b3SDharageswari R }
362*6c5768b3SDharageswari R 
363*6c5768b3SDharageswari R static void skl_clear_module_table(struct sst_dsp *ctx)
364*6c5768b3SDharageswari R {
365*6c5768b3SDharageswari R 	struct skl_module_table *module, *tmp;
366*6c5768b3SDharageswari R 
367*6c5768b3SDharageswari R 	if (list_empty(&ctx->module_list))
368*6c5768b3SDharageswari R 		return;
369*6c5768b3SDharageswari R 
370*6c5768b3SDharageswari R 	list_for_each_entry_safe(module, tmp, &ctx->module_list, list) {
371*6c5768b3SDharageswari R 		list_del(&module->list);
372*6c5768b3SDharageswari R 		release_firmware(module->mod_info->fw);
373*6c5768b3SDharageswari R 	}
374*6c5768b3SDharageswari R }
375*6c5768b3SDharageswari R 
376a750ba5fSSubhransu S. Prusty static struct skl_dsp_fw_ops skl_fw_ops = {
377a750ba5fSSubhransu S. Prusty 	.set_state_D0 = skl_set_dsp_D0,
378a750ba5fSSubhransu S. Prusty 	.set_state_D3 = skl_set_dsp_D3,
379a750ba5fSSubhransu S. Prusty 	.load_fw = skl_load_base_firmware,
380a750ba5fSSubhransu S. Prusty 	.get_fw_errcode = skl_get_errorcode,
381*6c5768b3SDharageswari R 	.load_mod = skl_load_module,
382*6c5768b3SDharageswari R 	.unload_mod = skl_unload_module,
383a750ba5fSSubhransu S. Prusty };
384a750ba5fSSubhransu S. Prusty 
385a750ba5fSSubhransu S. Prusty static struct sst_ops skl_ops = {
386a750ba5fSSubhransu S. Prusty 	.irq_handler = skl_dsp_sst_interrupt,
387a750ba5fSSubhransu S. Prusty 	.write = sst_shim32_write,
388a750ba5fSSubhransu S. Prusty 	.read = sst_shim32_read,
389a750ba5fSSubhransu S. Prusty 	.ram_read = sst_memcpy_fromio_32,
390a750ba5fSSubhransu S. Prusty 	.ram_write = sst_memcpy_toio_32,
391a750ba5fSSubhransu S. Prusty 	.free = skl_dsp_free,
392a750ba5fSSubhransu S. Prusty };
393a750ba5fSSubhransu S. Prusty 
394a750ba5fSSubhransu S. Prusty static struct sst_dsp_device skl_dev = {
395a750ba5fSSubhransu S. Prusty 	.thread = skl_dsp_irq_thread_handler,
396a750ba5fSSubhransu S. Prusty 	.ops = &skl_ops,
397a750ba5fSSubhransu S. Prusty };
398a750ba5fSSubhransu S. Prusty 
399a750ba5fSSubhransu S. Prusty int skl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
400aecf6fd8SVinod Koul 		const char *fw_name, struct skl_dsp_loader_ops dsp_ops, struct skl_sst **dsp)
401a750ba5fSSubhransu S. Prusty {
402a750ba5fSSubhransu S. Prusty 	struct skl_sst *skl;
403a750ba5fSSubhransu S. Prusty 	struct sst_dsp *sst;
404a750ba5fSSubhransu S. Prusty 	int ret;
405a750ba5fSSubhransu S. Prusty 
406a750ba5fSSubhransu S. Prusty 	skl = devm_kzalloc(dev, sizeof(*skl), GFP_KERNEL);
407a750ba5fSSubhransu S. Prusty 	if (skl == NULL)
408a750ba5fSSubhransu S. Prusty 		return -ENOMEM;
409a750ba5fSSubhransu S. Prusty 
410a750ba5fSSubhransu S. Prusty 	skl->dev = dev;
411a750ba5fSSubhransu S. Prusty 	skl_dev.thread_context = skl;
412a750ba5fSSubhransu S. Prusty 
413a750ba5fSSubhransu S. Prusty 	skl->dsp = skl_dsp_ctx_init(dev, &skl_dev, irq);
414a750ba5fSSubhransu S. Prusty 	if (!skl->dsp) {
415a750ba5fSSubhransu S. Prusty 		dev_err(skl->dev, "%s: no device\n", __func__);
416a750ba5fSSubhransu S. Prusty 		return -ENODEV;
417a750ba5fSSubhransu S. Prusty 	}
418a750ba5fSSubhransu S. Prusty 
419a750ba5fSSubhransu S. Prusty 	sst = skl->dsp;
420a750ba5fSSubhransu S. Prusty 
421aecf6fd8SVinod Koul 	sst->fw_name = fw_name;
422a750ba5fSSubhransu S. Prusty 	sst->addr.lpe = mmio_base;
423a750ba5fSSubhransu S. Prusty 	sst->addr.shim = mmio_base;
424a750ba5fSSubhransu S. Prusty 	sst_dsp_mailbox_init(sst, (SKL_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
425a750ba5fSSubhransu S. Prusty 			SKL_ADSP_W0_UP_SZ, SKL_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
426a750ba5fSSubhransu S. Prusty 
427*6c5768b3SDharageswari R 	INIT_LIST_HEAD(&sst->module_list);
428a750ba5fSSubhransu S. Prusty 	sst->dsp_ops = dsp_ops;
429a750ba5fSSubhransu S. Prusty 	sst->fw_ops = skl_fw_ops;
430a750ba5fSSubhransu S. Prusty 
431a750ba5fSSubhransu S. Prusty 	ret = skl_ipc_init(dev, skl);
432a750ba5fSSubhransu S. Prusty 	if (ret)
433a750ba5fSSubhransu S. Prusty 		return ret;
434a750ba5fSSubhransu S. Prusty 
435a750ba5fSSubhransu S. Prusty 	ret = sst->fw_ops.load_fw(sst);
436a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
437a750ba5fSSubhransu S. Prusty 		dev_err(dev, "Load base fw failed : %d", ret);
438b4fe965fSSubhransu S. Prusty 		goto cleanup;
439a750ba5fSSubhransu S. Prusty 	}
440a750ba5fSSubhransu S. Prusty 
441a750ba5fSSubhransu S. Prusty 	if (dsp)
442a750ba5fSSubhransu S. Prusty 		*dsp = skl;
443a750ba5fSSubhransu S. Prusty 
444b4fe965fSSubhransu S. Prusty 	return ret;
445a750ba5fSSubhransu S. Prusty 
446b4fe965fSSubhransu S. Prusty cleanup:
447b4fe965fSSubhransu S. Prusty 	skl_sst_dsp_cleanup(dev, skl);
448a750ba5fSSubhransu S. Prusty 	return ret;
449a750ba5fSSubhransu S. Prusty }
450a750ba5fSSubhransu S. Prusty EXPORT_SYMBOL_GPL(skl_sst_dsp_init);
451a750ba5fSSubhransu S. Prusty 
452a750ba5fSSubhransu S. Prusty void skl_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
453a750ba5fSSubhransu S. Prusty {
454*6c5768b3SDharageswari R 	skl_clear_module_table(ctx->dsp);
455a750ba5fSSubhransu S. Prusty 	skl_ipc_free(&ctx->ipc);
456a750ba5fSSubhransu S. Prusty 	ctx->dsp->ops->free(ctx->dsp);
457a750ba5fSSubhransu S. Prusty }
458a750ba5fSSubhransu S. Prusty EXPORT_SYMBOL_GPL(skl_sst_dsp_cleanup);
459a750ba5fSSubhransu S. Prusty 
460a750ba5fSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
461a750ba5fSSubhransu S. Prusty MODULE_DESCRIPTION("Intel Skylake IPC driver");
462