Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * skl-sst.c - HDA DSP library functions for SKL platform
0004  *
0005  * Copyright (C) 2014-15, Intel Corporation.
0006  * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
0007  *  Jeeja KP <jeeja.kp@intel.com>
0008  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/delay.h>
0013 #include <linux/device.h>
0014 #include <linux/err.h>
0015 #include <linux/uuid.h>
0016 #include "../common/sst-dsp.h"
0017 #include "../common/sst-dsp-priv.h"
0018 #include "../common/sst-ipc.h"
0019 #include "skl.h"
0020 
0021 #define SKL_BASEFW_TIMEOUT  300
0022 #define SKL_INIT_TIMEOUT    1000
0023 
0024 /* Intel HD Audio SRAM Window 0*/
0025 #define SKL_ADSP_SRAM0_BASE 0x8000
0026 
0027 /* Firmware status window */
0028 #define SKL_ADSP_FW_STATUS  SKL_ADSP_SRAM0_BASE
0029 #define SKL_ADSP_ERROR_CODE (SKL_ADSP_FW_STATUS + 0x4)
0030 
0031 #define SKL_NUM_MODULES     1
0032 
0033 static bool skl_check_fw_status(struct sst_dsp *ctx, u32 status)
0034 {
0035     u32 cur_sts;
0036 
0037     cur_sts = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS) & SKL_FW_STS_MASK;
0038 
0039     return (cur_sts == status);
0040 }
0041 
0042 static int skl_transfer_firmware(struct sst_dsp *ctx,
0043         const void *basefw, u32 base_fw_size)
0044 {
0045     int ret = 0;
0046 
0047     ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, basefw, base_fw_size,
0048                                 true);
0049     if (ret < 0)
0050         return ret;
0051 
0052     ret = sst_dsp_register_poll(ctx,
0053             SKL_ADSP_FW_STATUS,
0054             SKL_FW_STS_MASK,
0055             SKL_FW_RFW_START,
0056             SKL_BASEFW_TIMEOUT,
0057             "Firmware boot");
0058 
0059     ctx->cl_dev.ops.cl_stop_dma(ctx);
0060 
0061     return ret;
0062 }
0063 
0064 #define SKL_ADSP_FW_BIN_HDR_OFFSET 0x284
0065 
0066 static int skl_load_base_firmware(struct sst_dsp *ctx)
0067 {
0068     int ret = 0, i;
0069     struct skl_dev *skl = ctx->thread_context;
0070     struct firmware stripped_fw;
0071     u32 reg;
0072 
0073     skl->boot_complete = false;
0074     init_waitqueue_head(&skl->boot_wait);
0075 
0076     if (ctx->fw == NULL) {
0077         ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
0078         if (ret < 0) {
0079             dev_err(ctx->dev, "Request firmware failed %d\n", ret);
0080             return -EIO;
0081         }
0082     }
0083 
0084     /* prase uuids on first boot */
0085     if (skl->is_first_boot) {
0086         ret = snd_skl_parse_uuids(ctx, ctx->fw, SKL_ADSP_FW_BIN_HDR_OFFSET, 0);
0087         if (ret < 0) {
0088             dev_err(ctx->dev, "UUID parsing err: %d\n", ret);
0089             release_firmware(ctx->fw);
0090             skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
0091             return ret;
0092         }
0093     }
0094 
0095     /* check for extended manifest */
0096     stripped_fw.data = ctx->fw->data;
0097     stripped_fw.size = ctx->fw->size;
0098 
0099     skl_dsp_strip_extended_manifest(&stripped_fw);
0100 
0101     ret = skl_dsp_boot(ctx);
0102     if (ret < 0) {
0103         dev_err(ctx->dev, "Boot dsp core failed ret: %d\n", ret);
0104         goto skl_load_base_firmware_failed;
0105     }
0106 
0107     ret = skl_cldma_prepare(ctx);
0108     if (ret < 0) {
0109         dev_err(ctx->dev, "CL dma prepare failed : %d\n", ret);
0110         goto skl_load_base_firmware_failed;
0111     }
0112 
0113     /* enable Interrupt */
0114     skl_ipc_int_enable(ctx);
0115     skl_ipc_op_int_enable(ctx);
0116 
0117     /* check ROM Status */
0118     for (i = SKL_INIT_TIMEOUT; i > 0; --i) {
0119         if (skl_check_fw_status(ctx, SKL_FW_INIT)) {
0120             dev_dbg(ctx->dev,
0121                 "ROM loaded, we can continue with FW loading\n");
0122             break;
0123         }
0124         mdelay(1);
0125     }
0126     if (!i) {
0127         reg = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS);
0128         dev_err(ctx->dev,
0129             "Timeout waiting for ROM init done, reg:0x%x\n", reg);
0130         ret = -EIO;
0131         goto transfer_firmware_failed;
0132     }
0133 
0134     ret = skl_transfer_firmware(ctx, stripped_fw.data, stripped_fw.size);
0135     if (ret < 0) {
0136         dev_err(ctx->dev, "Transfer firmware failed%d\n", ret);
0137         goto transfer_firmware_failed;
0138     } else {
0139         ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
0140                     msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
0141         if (ret == 0) {
0142             dev_err(ctx->dev, "DSP boot failed, FW Ready timed-out\n");
0143             ret = -EIO;
0144             goto transfer_firmware_failed;
0145         }
0146 
0147         dev_dbg(ctx->dev, "Download firmware successful%d\n", ret);
0148         skl->fw_loaded = true;
0149     }
0150     return 0;
0151 transfer_firmware_failed:
0152     ctx->cl_dev.ops.cl_cleanup_controller(ctx);
0153 skl_load_base_firmware_failed:
0154     skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
0155     release_firmware(ctx->fw);
0156     ctx->fw = NULL;
0157     return ret;
0158 }
0159 
0160 static int skl_set_dsp_D0(struct sst_dsp *ctx, unsigned int core_id)
0161 {
0162     int ret;
0163     struct skl_ipc_dxstate_info dx;
0164     struct skl_dev *skl = ctx->thread_context;
0165     unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
0166 
0167     /* If core0 is being turned on, we need to load the FW */
0168     if (core_id == SKL_DSP_CORE0_ID) {
0169         ret = skl_load_base_firmware(ctx);
0170         if (ret < 0) {
0171             dev_err(ctx->dev, "unable to load firmware\n");
0172             return ret;
0173         }
0174 
0175         /* load libs as they are also lost on D3 */
0176         if (skl->lib_count > 1) {
0177             ret = ctx->fw_ops.load_library(ctx, skl->lib_info,
0178                             skl->lib_count);
0179             if (ret < 0) {
0180                 dev_err(ctx->dev, "reload libs failed: %d\n",
0181                         ret);
0182                 return ret;
0183             }
0184 
0185         }
0186     }
0187 
0188     /*
0189      * If any core other than core 0 is being moved to D0, enable the
0190      * core and send the set dx IPC for the core.
0191      */
0192     if (core_id != SKL_DSP_CORE0_ID) {
0193         ret = skl_dsp_enable_core(ctx, core_mask);
0194         if (ret < 0)
0195             return ret;
0196 
0197         dx.core_mask = core_mask;
0198         dx.dx_mask = core_mask;
0199 
0200         ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID,
0201                     SKL_BASE_FW_MODULE_ID, &dx);
0202         if (ret < 0) {
0203             dev_err(ctx->dev, "Failed to set dsp to D0:core id= %d\n",
0204                     core_id);
0205             skl_dsp_disable_core(ctx, core_mask);
0206         }
0207     }
0208 
0209     skl->cores.state[core_id] = SKL_DSP_RUNNING;
0210 
0211     return 0;
0212 }
0213 
0214 static int skl_set_dsp_D3(struct sst_dsp *ctx, unsigned int core_id)
0215 {
0216     int ret;
0217     struct skl_ipc_dxstate_info dx;
0218     struct skl_dev *skl = ctx->thread_context;
0219     unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
0220 
0221     dx.core_mask = core_mask;
0222     dx.dx_mask = SKL_IPC_D3_MASK;
0223 
0224     ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID, SKL_BASE_FW_MODULE_ID, &dx);
0225     if (ret < 0)
0226         dev_err(ctx->dev, "set Dx core %d fail: %d\n", core_id, ret);
0227 
0228     if (core_id == SKL_DSP_CORE0_ID) {
0229         /* disable Interrupt */
0230         ctx->cl_dev.ops.cl_cleanup_controller(ctx);
0231         skl_cldma_int_disable(ctx);
0232         skl_ipc_op_int_disable(ctx);
0233         skl_ipc_int_disable(ctx);
0234     }
0235 
0236     ret = skl_dsp_disable_core(ctx, core_mask);
0237     if (ret < 0)
0238         return ret;
0239 
0240     skl->cores.state[core_id] = SKL_DSP_RESET;
0241     return ret;
0242 }
0243 
0244 static unsigned int skl_get_errorcode(struct sst_dsp *ctx)
0245 {
0246      return sst_dsp_shim_read(ctx, SKL_ADSP_ERROR_CODE);
0247 }
0248 
0249 /*
0250  * since get/set_module are called from DAPM context,
0251  * we don't need lock for usage count
0252  */
0253 static int skl_get_module(struct sst_dsp *ctx, u16 mod_id)
0254 {
0255     struct skl_module_table *module;
0256 
0257     list_for_each_entry(module, &ctx->module_list, list) {
0258         if (module->mod_info->mod_id == mod_id)
0259             return ++module->usage_cnt;
0260     }
0261 
0262     return -EINVAL;
0263 }
0264 
0265 static int skl_put_module(struct sst_dsp *ctx, u16 mod_id)
0266 {
0267     struct skl_module_table *module;
0268 
0269     list_for_each_entry(module, &ctx->module_list, list) {
0270         if (module->mod_info->mod_id == mod_id)
0271             return --module->usage_cnt;
0272     }
0273 
0274     return -EINVAL;
0275 }
0276 
0277 static struct skl_module_table *skl_fill_module_table(struct sst_dsp *ctx,
0278                         char *mod_name, int mod_id)
0279 {
0280     const struct firmware *fw;
0281     struct skl_module_table *skl_module;
0282     unsigned int size;
0283     int ret;
0284 
0285     ret = request_firmware(&fw, mod_name, ctx->dev);
0286     if (ret < 0) {
0287         dev_err(ctx->dev, "Request Module %s failed :%d\n",
0288                             mod_name, ret);
0289         return NULL;
0290     }
0291 
0292     skl_module = devm_kzalloc(ctx->dev, sizeof(*skl_module), GFP_KERNEL);
0293     if (skl_module == NULL) {
0294         release_firmware(fw);
0295         return NULL;
0296     }
0297 
0298     size = sizeof(*skl_module->mod_info);
0299     skl_module->mod_info = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
0300     if (skl_module->mod_info == NULL) {
0301         release_firmware(fw);
0302         return NULL;
0303     }
0304 
0305     skl_module->mod_info->mod_id = mod_id;
0306     skl_module->mod_info->fw = fw;
0307     list_add(&skl_module->list, &ctx->module_list);
0308 
0309     return skl_module;
0310 }
0311 
0312 /* get a module from it's unique ID */
0313 static struct skl_module_table *skl_module_get_from_id(
0314             struct sst_dsp *ctx, u16 mod_id)
0315 {
0316     struct skl_module_table *module;
0317 
0318     if (list_empty(&ctx->module_list)) {
0319         dev_err(ctx->dev, "Module list is empty\n");
0320         return NULL;
0321     }
0322 
0323     list_for_each_entry(module, &ctx->module_list, list) {
0324         if (module->mod_info->mod_id == mod_id)
0325             return module;
0326     }
0327 
0328     return NULL;
0329 }
0330 
0331 static int skl_transfer_module(struct sst_dsp *ctx, const void *data,
0332             u32 size, u16 mod_id, u8 table_id, bool is_module)
0333 {
0334     int ret, bytes_left, curr_pos;
0335     struct skl_dev *skl = ctx->thread_context;
0336     skl->mod_load_complete = false;
0337 
0338     bytes_left = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, data, size, false);
0339     if (bytes_left < 0)
0340         return bytes_left;
0341 
0342     /* check is_module flag to load module or library */
0343     if (is_module)
0344         ret = skl_ipc_load_modules(&skl->ipc, SKL_NUM_MODULES, &mod_id);
0345     else
0346         ret = skl_sst_ipc_load_library(&skl->ipc, 0, table_id, false);
0347 
0348     if (ret < 0) {
0349         dev_err(ctx->dev, "Failed to Load %s with err %d\n",
0350                 is_module ? "module" : "lib", ret);
0351         goto out;
0352     }
0353 
0354     /*
0355      * if bytes_left > 0 then wait for BDL complete interrupt and
0356      * copy the next chunk till bytes_left is 0. if bytes_left is
0357      * zero, then wait for load module IPC reply
0358      */
0359     while (bytes_left > 0) {
0360         curr_pos = size - bytes_left;
0361 
0362         ret = skl_cldma_wait_interruptible(ctx);
0363         if (ret < 0)
0364             goto out;
0365 
0366         bytes_left = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx,
0367                             data + curr_pos,
0368                             bytes_left, false);
0369     }
0370 
0371     ret = wait_event_timeout(skl->mod_load_wait, skl->mod_load_complete,
0372                 msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
0373     if (ret == 0 || !skl->mod_load_status) {
0374         dev_err(ctx->dev, "Module Load failed\n");
0375         ret = -EIO;
0376     }
0377 
0378 out:
0379     ctx->cl_dev.ops.cl_stop_dma(ctx);
0380 
0381     return ret;
0382 }
0383 
0384 static int
0385 skl_load_library(struct sst_dsp *ctx, struct skl_lib_info *linfo, int lib_count)
0386 {
0387     struct skl_dev *skl = ctx->thread_context;
0388     struct firmware stripped_fw;
0389     int ret, i;
0390 
0391     /* library indices start from 1 to N. 0 represents base FW */
0392     for (i = 1; i < lib_count; i++) {
0393         ret = skl_prepare_lib_load(skl, &skl->lib_info[i], &stripped_fw,
0394                     SKL_ADSP_FW_BIN_HDR_OFFSET, i);
0395         if (ret < 0)
0396             goto load_library_failed;
0397         ret = skl_transfer_module(ctx, stripped_fw.data,
0398                 stripped_fw.size, 0, i, false);
0399         if (ret < 0)
0400             goto load_library_failed;
0401     }
0402 
0403     return 0;
0404 
0405 load_library_failed:
0406     skl_release_library(linfo, lib_count);
0407     return ret;
0408 }
0409 
0410 static int skl_load_module(struct sst_dsp *ctx, u16 mod_id, u8 *guid)
0411 {
0412     struct skl_module_table *module_entry = NULL;
0413     int ret = 0;
0414     char mod_name[64]; /* guid str = 32 chars + 4 hyphens */
0415 
0416     snprintf(mod_name, sizeof(mod_name), "intel/dsp_fw_%pUL.bin", guid);
0417 
0418     module_entry = skl_module_get_from_id(ctx, mod_id);
0419     if (module_entry == NULL) {
0420         module_entry = skl_fill_module_table(ctx, mod_name, mod_id);
0421         if (module_entry == NULL) {
0422             dev_err(ctx->dev, "Failed to Load module\n");
0423             return -EINVAL;
0424         }
0425     }
0426 
0427     if (!module_entry->usage_cnt) {
0428         ret = skl_transfer_module(ctx, module_entry->mod_info->fw->data,
0429                 module_entry->mod_info->fw->size,
0430                 mod_id, 0, true);
0431         if (ret < 0) {
0432             dev_err(ctx->dev, "Failed to Load module\n");
0433             return ret;
0434         }
0435     }
0436 
0437     ret = skl_get_module(ctx, mod_id);
0438 
0439     return ret;
0440 }
0441 
0442 static int skl_unload_module(struct sst_dsp *ctx, u16 mod_id)
0443 {
0444     int usage_cnt;
0445     struct skl_dev *skl = ctx->thread_context;
0446     int ret = 0;
0447 
0448     usage_cnt = skl_put_module(ctx, mod_id);
0449     if (usage_cnt < 0) {
0450         dev_err(ctx->dev, "Module bad usage cnt!:%d\n", usage_cnt);
0451         return -EIO;
0452     }
0453 
0454     /* if module is used by others return, no need to unload */
0455     if (usage_cnt > 0)
0456         return 0;
0457 
0458     ret = skl_ipc_unload_modules(&skl->ipc,
0459             SKL_NUM_MODULES, &mod_id);
0460     if (ret < 0) {
0461         dev_err(ctx->dev, "Failed to UnLoad module\n");
0462         skl_get_module(ctx, mod_id);
0463         return ret;
0464     }
0465 
0466     return ret;
0467 }
0468 
0469 void skl_clear_module_cnt(struct sst_dsp *ctx)
0470 {
0471     struct skl_module_table *module;
0472 
0473     if (list_empty(&ctx->module_list))
0474         return;
0475 
0476     list_for_each_entry(module, &ctx->module_list, list) {
0477         module->usage_cnt = 0;
0478     }
0479 }
0480 EXPORT_SYMBOL_GPL(skl_clear_module_cnt);
0481 
0482 static void skl_clear_module_table(struct sst_dsp *ctx)
0483 {
0484     struct skl_module_table *module, *tmp;
0485 
0486     if (list_empty(&ctx->module_list))
0487         return;
0488 
0489     list_for_each_entry_safe(module, tmp, &ctx->module_list, list) {
0490         list_del(&module->list);
0491         release_firmware(module->mod_info->fw);
0492     }
0493 }
0494 
0495 static const struct skl_dsp_fw_ops skl_fw_ops = {
0496     .set_state_D0 = skl_set_dsp_D0,
0497     .set_state_D3 = skl_set_dsp_D3,
0498     .load_fw = skl_load_base_firmware,
0499     .get_fw_errcode = skl_get_errorcode,
0500     .load_library = skl_load_library,
0501     .load_mod = skl_load_module,
0502     .unload_mod = skl_unload_module,
0503 };
0504 
0505 static struct sst_ops skl_ops = {
0506     .irq_handler = skl_dsp_sst_interrupt,
0507     .write = sst_shim32_write,
0508     .read = sst_shim32_read,
0509     .free = skl_dsp_free,
0510 };
0511 
0512 static struct sst_dsp_device skl_dev = {
0513     .thread = skl_dsp_irq_thread_handler,
0514     .ops = &skl_ops,
0515 };
0516 
0517 int skl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
0518         const char *fw_name, struct skl_dsp_loader_ops dsp_ops,
0519         struct skl_dev **dsp)
0520 {
0521     struct skl_dev *skl;
0522     struct sst_dsp *sst;
0523     int ret;
0524 
0525     ret = skl_sst_ctx_init(dev, irq, fw_name, dsp_ops, dsp, &skl_dev);
0526     if (ret < 0) {
0527         dev_err(dev, "%s: no device\n", __func__);
0528         return ret;
0529     }
0530 
0531     skl = *dsp;
0532     sst = skl->dsp;
0533     sst->addr.lpe = mmio_base;
0534     sst->addr.shim = mmio_base;
0535     sst->addr.sram0_base = SKL_ADSP_SRAM0_BASE;
0536     sst->addr.sram1_base = SKL_ADSP_SRAM1_BASE;
0537     sst->addr.w0_stat_sz = SKL_ADSP_W0_STAT_SZ;
0538     sst->addr.w0_up_sz = SKL_ADSP_W0_UP_SZ;
0539 
0540     sst_dsp_mailbox_init(sst, (SKL_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
0541             SKL_ADSP_W0_UP_SZ, SKL_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
0542 
0543     ret = skl_ipc_init(dev, skl);
0544     if (ret) {
0545         skl_dsp_free(sst);
0546         return ret;
0547     }
0548 
0549     sst->fw_ops = skl_fw_ops;
0550 
0551     return skl_dsp_acquire_irq(sst);
0552 }
0553 EXPORT_SYMBOL_GPL(skl_sst_dsp_init);
0554 
0555 int skl_sst_init_fw(struct device *dev, struct skl_dev *skl)
0556 {
0557     int ret;
0558     struct sst_dsp *sst = skl->dsp;
0559 
0560     ret = sst->fw_ops.load_fw(sst);
0561     if (ret < 0) {
0562         dev_err(dev, "Load base fw failed : %d\n", ret);
0563         return ret;
0564     }
0565 
0566     skl_dsp_init_core_state(sst);
0567 
0568     if (skl->lib_count > 1) {
0569         ret = sst->fw_ops.load_library(sst, skl->lib_info,
0570                         skl->lib_count);
0571         if (ret < 0) {
0572             dev_err(dev, "Load Library failed : %x\n", ret);
0573             return ret;
0574         }
0575     }
0576     skl->is_first_boot = false;
0577 
0578     return 0;
0579 }
0580 EXPORT_SYMBOL_GPL(skl_sst_init_fw);
0581 
0582 void skl_sst_dsp_cleanup(struct device *dev, struct skl_dev *skl)
0583 {
0584 
0585     if (skl->dsp->fw)
0586         release_firmware(skl->dsp->fw);
0587     skl_clear_module_table(skl->dsp);
0588     skl_freeup_uuid_list(skl);
0589     skl_ipc_free(&skl->ipc);
0590     skl->dsp->ops->free(skl->dsp);
0591     if (skl->boot_complete) {
0592         skl->dsp->cl_dev.ops.cl_cleanup_controller(skl->dsp);
0593         skl_cldma_int_disable(skl->dsp);
0594     }
0595 }
0596 EXPORT_SYMBOL_GPL(skl_sst_dsp_cleanup);
0597 
0598 MODULE_LICENSE("GPL v2");
0599 MODULE_DESCRIPTION("Intel Skylake IPC driver");