ASoC: Intel: Skylake: Clean up of driver resources in suspend
[deliverable/linux.git] / sound / soc / intel / skylake / skl-sst.c
1 /*
2 * skl-sst.c - HDA DSP library functions for SKL platform
3 *
4 * Copyright (C) 2014-15, Intel Corporation.
5 * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
6 * Jeeja KP <jeeja.kp@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 */
18
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/err.h>
23 #include <linux/uuid.h>
24 #include "../common/sst-dsp.h"
25 #include "../common/sst-dsp-priv.h"
26 #include "../common/sst-ipc.h"
27 #include "skl-sst-ipc.h"
28
29 #define SKL_BASEFW_TIMEOUT 300
30 #define SKL_INIT_TIMEOUT 1000
31
32 /* Intel HD Audio SRAM Window 0*/
33 #define SKL_ADSP_SRAM0_BASE 0x8000
34
35 /* Firmware status window */
36 #define SKL_ADSP_FW_STATUS SKL_ADSP_SRAM0_BASE
37 #define SKL_ADSP_ERROR_CODE (SKL_ADSP_FW_STATUS + 0x4)
38
39 #define SKL_NUM_MODULES 1
40
41 static bool skl_check_fw_status(struct sst_dsp *ctx, u32 status)
42 {
43 u32 cur_sts;
44
45 cur_sts = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS) & SKL_FW_STS_MASK;
46
47 return (cur_sts == status);
48 }
49
50 static int skl_transfer_firmware(struct sst_dsp *ctx,
51 const void *basefw, u32 base_fw_size)
52 {
53 int ret = 0;
54
55 ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, basefw, base_fw_size);
56 if (ret < 0)
57 return ret;
58
59 ret = sst_dsp_register_poll(ctx,
60 SKL_ADSP_FW_STATUS,
61 SKL_FW_STS_MASK,
62 SKL_FW_RFW_START,
63 SKL_BASEFW_TIMEOUT,
64 "Firmware boot");
65
66 ctx->cl_dev.ops.cl_stop_dma(ctx);
67
68 return ret;
69 }
70
71 #define SKL_ADSP_FW_BIN_HDR_OFFSET 0x284
72
73 static int skl_load_base_firmware(struct sst_dsp *ctx)
74 {
75 int ret = 0, i;
76 struct skl_sst *skl = ctx->thread_context;
77 struct firmware stripped_fw;
78 u32 reg;
79
80 skl->boot_complete = false;
81 init_waitqueue_head(&skl->boot_wait);
82
83 if (ctx->fw == NULL) {
84 ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
85 if (ret < 0) {
86 dev_err(ctx->dev, "Request firmware failed %d\n", ret);
87 skl_dsp_disable_core(ctx);
88 return -EIO;
89 }
90
91 }
92
93 ret = snd_skl_parse_uuids(ctx, SKL_ADSP_FW_BIN_HDR_OFFSET);
94 if (ret < 0) {
95 dev_err(ctx->dev,
96 "UUID parsing err: %d\n", ret);
97 release_firmware(ctx->fw);
98 skl_dsp_disable_core(ctx);
99 return ret;
100 }
101
102 /* check for extended manifest */
103 stripped_fw.data = ctx->fw->data;
104 stripped_fw.size = ctx->fw->size;
105
106 skl_dsp_strip_extended_manifest(&stripped_fw);
107
108 ret = skl_dsp_boot(ctx);
109 if (ret < 0) {
110 dev_err(ctx->dev, "Boot dsp core failed ret: %d", ret);
111 goto skl_load_base_firmware_failed;
112 }
113
114 ret = skl_cldma_prepare(ctx);
115 if (ret < 0) {
116 dev_err(ctx->dev, "CL dma prepare failed : %d", ret);
117 goto skl_load_base_firmware_failed;
118 }
119
120 /* enable Interrupt */
121 skl_ipc_int_enable(ctx);
122 skl_ipc_op_int_enable(ctx);
123
124 /* check ROM Status */
125 for (i = SKL_INIT_TIMEOUT; i > 0; --i) {
126 if (skl_check_fw_status(ctx, SKL_FW_INIT)) {
127 dev_dbg(ctx->dev,
128 "ROM loaded, we can continue with FW loading\n");
129 break;
130 }
131 mdelay(1);
132 }
133 if (!i) {
134 reg = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS);
135 dev_err(ctx->dev,
136 "Timeout waiting for ROM init done, reg:0x%x\n", reg);
137 ret = -EIO;
138 goto transfer_firmware_failed;
139 }
140
141 ret = skl_transfer_firmware(ctx, stripped_fw.data, stripped_fw.size);
142 if (ret < 0) {
143 dev_err(ctx->dev, "Transfer firmware failed%d\n", ret);
144 goto transfer_firmware_failed;
145 } else {
146 ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
147 msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
148 if (ret == 0) {
149 dev_err(ctx->dev, "DSP boot failed, FW Ready timed-out\n");
150 ret = -EIO;
151 goto transfer_firmware_failed;
152 }
153
154 dev_dbg(ctx->dev, "Download firmware successful%d\n", ret);
155 skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
156 }
157 return 0;
158 transfer_firmware_failed:
159 ctx->cl_dev.ops.cl_cleanup_controller(ctx);
160 skl_load_base_firmware_failed:
161 skl_dsp_disable_core(ctx);
162 release_firmware(ctx->fw);
163 ctx->fw = NULL;
164 return ret;
165 }
166
167 static int skl_set_dsp_D0(struct sst_dsp *ctx)
168 {
169 int ret;
170
171 ret = skl_load_base_firmware(ctx);
172 if (ret < 0) {
173 dev_err(ctx->dev, "unable to load firmware\n");
174 return ret;
175 }
176
177 skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
178
179 return ret;
180 }
181
182 static int skl_set_dsp_D3(struct sst_dsp *ctx)
183 {
184 int ret;
185 struct skl_ipc_dxstate_info dx;
186 struct skl_sst *skl = ctx->thread_context;
187
188 dev_dbg(ctx->dev, "In %s:\n", __func__);
189 mutex_lock(&ctx->mutex);
190 if (!is_skl_dsp_running(ctx)) {
191 mutex_unlock(&ctx->mutex);
192 return 0;
193 }
194 mutex_unlock(&ctx->mutex);
195
196 dx.core_mask = SKL_DSP_CORE0_MASK;
197 dx.dx_mask = SKL_IPC_D3_MASK;
198 ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID, SKL_BASE_FW_MODULE_ID, &dx);
199 if (ret < 0)
200 dev_err(ctx->dev,
201 "D3 request to FW failed, continuing reset: %d", ret);
202
203 /* disable Interrupt */
204 ctx->cl_dev.ops.cl_cleanup_controller(ctx);
205 skl_cldma_int_disable(ctx);
206 skl_ipc_op_int_disable(ctx);
207 skl_ipc_int_disable(ctx);
208
209 ret = skl_dsp_disable_core(ctx);
210 if (ret < 0) {
211 dev_err(ctx->dev, "disable dsp core failed ret: %d\n", ret);
212 ret = -EIO;
213 }
214 skl_dsp_set_state_locked(ctx, SKL_DSP_RESET);
215
216 return ret;
217 }
218
219 static unsigned int skl_get_errorcode(struct sst_dsp *ctx)
220 {
221 return sst_dsp_shim_read(ctx, SKL_ADSP_ERROR_CODE);
222 }
223
224 /*
225 * since get/set_module are called from DAPM context,
226 * we don't need lock for usage count
227 */
228 static int skl_get_module(struct sst_dsp *ctx, u16 mod_id)
229 {
230 struct skl_module_table *module;
231
232 list_for_each_entry(module, &ctx->module_list, list) {
233 if (module->mod_info->mod_id == mod_id)
234 return ++module->usage_cnt;
235 }
236
237 return -EINVAL;
238 }
239
240 static int skl_put_module(struct sst_dsp *ctx, u16 mod_id)
241 {
242 struct skl_module_table *module;
243
244 list_for_each_entry(module, &ctx->module_list, list) {
245 if (module->mod_info->mod_id == mod_id)
246 return --module->usage_cnt;
247 }
248
249 return -EINVAL;
250 }
251
252 static struct skl_module_table *skl_fill_module_table(struct sst_dsp *ctx,
253 char *mod_name, int mod_id)
254 {
255 const struct firmware *fw;
256 struct skl_module_table *skl_module;
257 unsigned int size;
258 int ret;
259
260 ret = request_firmware(&fw, mod_name, ctx->dev);
261 if (ret < 0) {
262 dev_err(ctx->dev, "Request Module %s failed :%d\n",
263 mod_name, ret);
264 return NULL;
265 }
266
267 skl_module = devm_kzalloc(ctx->dev, sizeof(*skl_module), GFP_KERNEL);
268 if (skl_module == NULL) {
269 release_firmware(fw);
270 return NULL;
271 }
272
273 size = sizeof(*skl_module->mod_info);
274 skl_module->mod_info = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
275 if (skl_module->mod_info == NULL) {
276 release_firmware(fw);
277 return NULL;
278 }
279
280 skl_module->mod_info->mod_id = mod_id;
281 skl_module->mod_info->fw = fw;
282 list_add(&skl_module->list, &ctx->module_list);
283
284 return skl_module;
285 }
286
287 /* get a module from it's unique ID */
288 static struct skl_module_table *skl_module_get_from_id(
289 struct sst_dsp *ctx, u16 mod_id)
290 {
291 struct skl_module_table *module;
292
293 if (list_empty(&ctx->module_list)) {
294 dev_err(ctx->dev, "Module list is empty\n");
295 return NULL;
296 }
297
298 list_for_each_entry(module, &ctx->module_list, list) {
299 if (module->mod_info->mod_id == mod_id)
300 return module;
301 }
302
303 return NULL;
304 }
305
306 static int skl_transfer_module(struct sst_dsp *ctx,
307 struct skl_load_module_info *module)
308 {
309 int ret;
310 struct skl_sst *skl = ctx->thread_context;
311
312 ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, module->fw->data,
313 module->fw->size);
314 if (ret < 0)
315 return ret;
316
317 ret = skl_ipc_load_modules(&skl->ipc, SKL_NUM_MODULES,
318 (void *)&module->mod_id);
319 if (ret < 0)
320 dev_err(ctx->dev, "Failed to Load module: %d\n", ret);
321
322 ctx->cl_dev.ops.cl_stop_dma(ctx);
323
324 return ret;
325 }
326
327 static int skl_load_module(struct sst_dsp *ctx, u16 mod_id, u8 *guid)
328 {
329 struct skl_module_table *module_entry = NULL;
330 int ret = 0;
331 char mod_name[64]; /* guid str = 32 chars + 4 hyphens */
332 uuid_le *uuid_mod;
333
334 uuid_mod = (uuid_le *)guid;
335 snprintf(mod_name, sizeof(mod_name), "%s%pUL%s",
336 "intel/dsp_fw_", uuid_mod, ".bin");
337
338 module_entry = skl_module_get_from_id(ctx, mod_id);
339 if (module_entry == NULL) {
340 module_entry = skl_fill_module_table(ctx, mod_name, mod_id);
341 if (module_entry == NULL) {
342 dev_err(ctx->dev, "Failed to Load module\n");
343 return -EINVAL;
344 }
345 }
346
347 if (!module_entry->usage_cnt) {
348 ret = skl_transfer_module(ctx, module_entry->mod_info);
349 if (ret < 0) {
350 dev_err(ctx->dev, "Failed to Load module\n");
351 return ret;
352 }
353 }
354
355 ret = skl_get_module(ctx, mod_id);
356
357 return ret;
358 }
359
360 static int skl_unload_module(struct sst_dsp *ctx, u16 mod_id)
361 {
362 int usage_cnt;
363 struct skl_sst *skl = ctx->thread_context;
364 int ret = 0;
365
366 usage_cnt = skl_put_module(ctx, mod_id);
367 if (usage_cnt < 0) {
368 dev_err(ctx->dev, "Module bad usage cnt!:%d\n", usage_cnt);
369 return -EIO;
370 }
371 ret = skl_ipc_unload_modules(&skl->ipc,
372 SKL_NUM_MODULES, &mod_id);
373 if (ret < 0) {
374 dev_err(ctx->dev, "Failed to UnLoad module\n");
375 skl_get_module(ctx, mod_id);
376 return ret;
377 }
378
379 return ret;
380 }
381
382 void skl_clear_module_cnt(struct sst_dsp *ctx)
383 {
384 struct skl_module_table *module;
385
386 list_for_each_entry(module, &ctx->module_list, list) {
387 module->usage_cnt = 0;
388 }
389 }
390 EXPORT_SYMBOL_GPL(skl_clear_module_cnt);
391
392 static void skl_clear_module_table(struct sst_dsp *ctx)
393 {
394 struct skl_module_table *module, *tmp;
395
396 if (list_empty(&ctx->module_list))
397 return;
398
399 list_for_each_entry_safe(module, tmp, &ctx->module_list, list) {
400 list_del(&module->list);
401 release_firmware(module->mod_info->fw);
402 }
403 }
404
405 static struct skl_dsp_fw_ops skl_fw_ops = {
406 .set_state_D0 = skl_set_dsp_D0,
407 .set_state_D3 = skl_set_dsp_D3,
408 .load_fw = skl_load_base_firmware,
409 .get_fw_errcode = skl_get_errorcode,
410 .load_mod = skl_load_module,
411 .unload_mod = skl_unload_module,
412 };
413
414 static struct sst_ops skl_ops = {
415 .irq_handler = skl_dsp_sst_interrupt,
416 .write = sst_shim32_write,
417 .read = sst_shim32_read,
418 .ram_read = sst_memcpy_fromio_32,
419 .ram_write = sst_memcpy_toio_32,
420 .free = skl_dsp_free,
421 };
422
423 static struct sst_dsp_device skl_dev = {
424 .thread = skl_dsp_irq_thread_handler,
425 .ops = &skl_ops,
426 };
427
428 int skl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
429 const char *fw_name, struct skl_dsp_loader_ops dsp_ops, struct skl_sst **dsp)
430 {
431 struct skl_sst *skl;
432 struct sst_dsp *sst;
433 int ret;
434
435 skl = devm_kzalloc(dev, sizeof(*skl), GFP_KERNEL);
436 if (skl == NULL)
437 return -ENOMEM;
438
439 skl->dev = dev;
440 skl_dev.thread_context = skl;
441 INIT_LIST_HEAD(&skl->uuid_list);
442
443 skl->dsp = skl_dsp_ctx_init(dev, &skl_dev, irq);
444 if (!skl->dsp) {
445 dev_err(skl->dev, "%s: no device\n", __func__);
446 return -ENODEV;
447 }
448
449 sst = skl->dsp;
450
451 sst->fw_name = fw_name;
452 sst->addr.lpe = mmio_base;
453 sst->addr.shim = mmio_base;
454 sst_dsp_mailbox_init(sst, (SKL_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
455 SKL_ADSP_W0_UP_SZ, SKL_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
456
457 INIT_LIST_HEAD(&sst->module_list);
458 sst->dsp_ops = dsp_ops;
459 sst->fw_ops = skl_fw_ops;
460
461 ret = skl_ipc_init(dev, skl);
462 if (ret)
463 return ret;
464
465 ret = sst->fw_ops.load_fw(sst);
466 if (ret < 0) {
467 dev_err(dev, "Load base fw failed : %d", ret);
468 goto cleanup;
469 }
470
471 if (dsp)
472 *dsp = skl;
473
474 return ret;
475
476 cleanup:
477 skl_sst_dsp_cleanup(dev, skl);
478 return ret;
479 }
480 EXPORT_SYMBOL_GPL(skl_sst_dsp_init);
481
482 void skl_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
483 {
484 skl_clear_module_table(ctx->dsp);
485 skl_freeup_uuid_list(ctx);
486 skl_ipc_free(&ctx->ipc);
487 ctx->dsp->ops->free(ctx->dsp);
488 if (ctx->boot_complete) {
489 ctx->dsp->cl_dev.ops.cl_cleanup_controller(ctx->dsp);
490 skl_cldma_int_disable(ctx->dsp);
491 }
492 }
493 EXPORT_SYMBOL_GPL(skl_sst_dsp_cleanup);
494
495 MODULE_LICENSE("GPL v2");
496 MODULE_DESCRIPTION("Intel Skylake IPC driver");
This page took 0.067538 seconds and 6 git commands to generate.