skl-sst.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/device.h>
  21. #include <linux/err.h>
  22. #include <linux/uuid.h>
  23. #include "../common/sst-dsp.h"
  24. #include "../common/sst-dsp-priv.h"
  25. #include "../common/sst-ipc.h"
  26. #include "skl-sst-ipc.h"
  27. #define SKL_BASEFW_TIMEOUT 300
  28. #define SKL_INIT_TIMEOUT 1000
  29. /* Intel HD Audio SRAM Window 0*/
  30. #define SKL_ADSP_SRAM0_BASE 0x8000
  31. /* Firmware status window */
  32. #define SKL_ADSP_FW_STATUS SKL_ADSP_SRAM0_BASE
  33. #define SKL_ADSP_ERROR_CODE (SKL_ADSP_FW_STATUS + 0x4)
  34. #define SKL_NUM_MODULES 1
  35. static bool skl_check_fw_status(struct sst_dsp *ctx, u32 status)
  36. {
  37. u32 cur_sts;
  38. cur_sts = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS) & SKL_FW_STS_MASK;
  39. return (cur_sts == status);
  40. }
  41. static int skl_transfer_firmware(struct sst_dsp *ctx,
  42. const void *basefw, u32 base_fw_size)
  43. {
  44. int ret = 0;
  45. ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, basefw, base_fw_size,
  46. true);
  47. if (ret < 0)
  48. return ret;
  49. ret = sst_dsp_register_poll(ctx,
  50. SKL_ADSP_FW_STATUS,
  51. SKL_FW_STS_MASK,
  52. SKL_FW_RFW_START,
  53. SKL_BASEFW_TIMEOUT,
  54. "Firmware boot");
  55. ctx->cl_dev.ops.cl_stop_dma(ctx);
  56. return ret;
  57. }
  58. #define SKL_ADSP_FW_BIN_HDR_OFFSET 0x284
  59. static int skl_load_base_firmware(struct sst_dsp *ctx)
  60. {
  61. int ret = 0, i;
  62. struct skl_sst *skl = ctx->thread_context;
  63. struct firmware stripped_fw;
  64. u32 reg;
  65. skl->boot_complete = false;
  66. init_waitqueue_head(&skl->boot_wait);
  67. if (ctx->fw == NULL) {
  68. ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
  69. if (ret < 0) {
  70. dev_err(ctx->dev, "Request firmware failed %d\n", ret);
  71. return -EIO;
  72. }
  73. }
  74. /* prase uuids on first boot */
  75. if (skl->is_first_boot) {
  76. ret = snd_skl_parse_uuids(ctx, ctx->fw, SKL_ADSP_FW_BIN_HDR_OFFSET, 0);
  77. if (ret < 0) {
  78. dev_err(ctx->dev, "UUID parsing err: %d\n", ret);
  79. release_firmware(ctx->fw);
  80. skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
  81. return ret;
  82. }
  83. }
  84. /* check for extended manifest */
  85. stripped_fw.data = ctx->fw->data;
  86. stripped_fw.size = ctx->fw->size;
  87. skl_dsp_strip_extended_manifest(&stripped_fw);
  88. ret = skl_dsp_boot(ctx);
  89. if (ret < 0) {
  90. dev_err(ctx->dev, "Boot dsp core failed ret: %d\n", ret);
  91. goto skl_load_base_firmware_failed;
  92. }
  93. ret = skl_cldma_prepare(ctx);
  94. if (ret < 0) {
  95. dev_err(ctx->dev, "CL dma prepare failed : %d\n", ret);
  96. goto skl_load_base_firmware_failed;
  97. }
  98. /* enable Interrupt */
  99. skl_ipc_int_enable(ctx);
  100. skl_ipc_op_int_enable(ctx);
  101. /* check ROM Status */
  102. for (i = SKL_INIT_TIMEOUT; i > 0; --i) {
  103. if (skl_check_fw_status(ctx, SKL_FW_INIT)) {
  104. dev_dbg(ctx->dev,
  105. "ROM loaded, we can continue with FW loading\n");
  106. break;
  107. }
  108. mdelay(1);
  109. }
  110. if (!i) {
  111. reg = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS);
  112. dev_err(ctx->dev,
  113. "Timeout waiting for ROM init done, reg:0x%x\n", reg);
  114. ret = -EIO;
  115. goto transfer_firmware_failed;
  116. }
  117. ret = skl_transfer_firmware(ctx, stripped_fw.data, stripped_fw.size);
  118. if (ret < 0) {
  119. dev_err(ctx->dev, "Transfer firmware failed%d\n", ret);
  120. goto transfer_firmware_failed;
  121. } else {
  122. ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
  123. msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
  124. if (ret == 0) {
  125. dev_err(ctx->dev, "DSP boot failed, FW Ready timed-out\n");
  126. ret = -EIO;
  127. goto transfer_firmware_failed;
  128. }
  129. dev_dbg(ctx->dev, "Download firmware successful%d\n", ret);
  130. skl->fw_loaded = true;
  131. }
  132. return 0;
  133. transfer_firmware_failed:
  134. ctx->cl_dev.ops.cl_cleanup_controller(ctx);
  135. skl_load_base_firmware_failed:
  136. skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
  137. release_firmware(ctx->fw);
  138. ctx->fw = NULL;
  139. return ret;
  140. }
  141. static int skl_set_dsp_D0(struct sst_dsp *ctx, unsigned int core_id)
  142. {
  143. int ret;
  144. struct skl_ipc_dxstate_info dx;
  145. struct skl_sst *skl = ctx->thread_context;
  146. unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
  147. /* If core0 is being turned on, we need to load the FW */
  148. if (core_id == SKL_DSP_CORE0_ID) {
  149. ret = skl_load_base_firmware(ctx);
  150. if (ret < 0) {
  151. dev_err(ctx->dev, "unable to load firmware\n");
  152. return ret;
  153. }
  154. /* load libs as they are also lost on D3 */
  155. if (skl->lib_count > 1) {
  156. ret = ctx->fw_ops.load_library(ctx, skl->lib_info,
  157. skl->lib_count);
  158. if (ret < 0) {
  159. dev_err(ctx->dev, "reload libs failed: %d\n",
  160. ret);
  161. return ret;
  162. }
  163. }
  164. }
  165. /*
  166. * If any core other than core 0 is being moved to D0, enable the
  167. * core and send the set dx IPC for the core.
  168. */
  169. if (core_id != SKL_DSP_CORE0_ID) {
  170. ret = skl_dsp_enable_core(ctx, core_mask);
  171. if (ret < 0)
  172. return ret;
  173. dx.core_mask = core_mask;
  174. dx.dx_mask = core_mask;
  175. ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID,
  176. SKL_BASE_FW_MODULE_ID, &dx);
  177. if (ret < 0) {
  178. dev_err(ctx->dev, "Failed to set dsp to D0:core id= %d\n",
  179. core_id);
  180. skl_dsp_disable_core(ctx, core_mask);
  181. }
  182. }
  183. skl->cores.state[core_id] = SKL_DSP_RUNNING;
  184. return 0;
  185. }
  186. static int skl_set_dsp_D3(struct sst_dsp *ctx, unsigned int core_id)
  187. {
  188. int ret;
  189. struct skl_ipc_dxstate_info dx;
  190. struct skl_sst *skl = ctx->thread_context;
  191. unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
  192. dx.core_mask = core_mask;
  193. dx.dx_mask = SKL_IPC_D3_MASK;
  194. ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID, SKL_BASE_FW_MODULE_ID, &dx);
  195. if (ret < 0)
  196. dev_err(ctx->dev, "set Dx core %d fail: %d\n", core_id, ret);
  197. if (core_id == SKL_DSP_CORE0_ID) {
  198. /* disable Interrupt */
  199. ctx->cl_dev.ops.cl_cleanup_controller(ctx);
  200. skl_cldma_int_disable(ctx);
  201. skl_ipc_op_int_disable(ctx);
  202. skl_ipc_int_disable(ctx);
  203. }
  204. ret = skl_dsp_disable_core(ctx, core_mask);
  205. if (ret < 0)
  206. return ret;
  207. skl->cores.state[core_id] = SKL_DSP_RESET;
  208. return ret;
  209. }
  210. static unsigned int skl_get_errorcode(struct sst_dsp *ctx)
  211. {
  212. return sst_dsp_shim_read(ctx, SKL_ADSP_ERROR_CODE);
  213. }
  214. /*
  215. * since get/set_module are called from DAPM context,
  216. * we don't need lock for usage count
  217. */
  218. static int skl_get_module(struct sst_dsp *ctx, u16 mod_id)
  219. {
  220. struct skl_module_table *module;
  221. list_for_each_entry(module, &ctx->module_list, list) {
  222. if (module->mod_info->mod_id == mod_id)
  223. return ++module->usage_cnt;
  224. }
  225. return -EINVAL;
  226. }
  227. static int skl_put_module(struct sst_dsp *ctx, u16 mod_id)
  228. {
  229. struct skl_module_table *module;
  230. list_for_each_entry(module, &ctx->module_list, list) {
  231. if (module->mod_info->mod_id == mod_id)
  232. return --module->usage_cnt;
  233. }
  234. return -EINVAL;
  235. }
  236. static struct skl_module_table *skl_fill_module_table(struct sst_dsp *ctx,
  237. char *mod_name, int mod_id)
  238. {
  239. const struct firmware *fw;
  240. struct skl_module_table *skl_module;
  241. unsigned int size;
  242. int ret;
  243. ret = request_firmware(&fw, mod_name, ctx->dev);
  244. if (ret < 0) {
  245. dev_err(ctx->dev, "Request Module %s failed :%d\n",
  246. mod_name, ret);
  247. return NULL;
  248. }
  249. skl_module = devm_kzalloc(ctx->dev, sizeof(*skl_module), GFP_KERNEL);
  250. if (skl_module == NULL) {
  251. release_firmware(fw);
  252. return NULL;
  253. }
  254. size = sizeof(*skl_module->mod_info);
  255. skl_module->mod_info = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
  256. if (skl_module->mod_info == NULL) {
  257. release_firmware(fw);
  258. return NULL;
  259. }
  260. skl_module->mod_info->mod_id = mod_id;
  261. skl_module->mod_info->fw = fw;
  262. list_add(&skl_module->list, &ctx->module_list);
  263. return skl_module;
  264. }
  265. /* get a module from it's unique ID */
  266. static struct skl_module_table *skl_module_get_from_id(
  267. struct sst_dsp *ctx, u16 mod_id)
  268. {
  269. struct skl_module_table *module;
  270. if (list_empty(&ctx->module_list)) {
  271. dev_err(ctx->dev, "Module list is empty\n");
  272. return NULL;
  273. }
  274. list_for_each_entry(module, &ctx->module_list, list) {
  275. if (module->mod_info->mod_id == mod_id)
  276. return module;
  277. }
  278. return NULL;
  279. }
  280. static int skl_transfer_module(struct sst_dsp *ctx, const void *data,
  281. u32 size, u16 mod_id, u8 table_id, bool is_module)
  282. {
  283. int ret, bytes_left, curr_pos;
  284. struct skl_sst *skl = ctx->thread_context;
  285. skl->mod_load_complete = false;
  286. bytes_left = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, data, size, false);
  287. if (bytes_left < 0)
  288. return bytes_left;
  289. /* check is_module flag to load module or library */
  290. if (is_module)
  291. ret = skl_ipc_load_modules(&skl->ipc, SKL_NUM_MODULES, &mod_id);
  292. else
  293. ret = skl_sst_ipc_load_library(&skl->ipc, 0, table_id, false);
  294. if (ret < 0) {
  295. dev_err(ctx->dev, "Failed to Load %s with err %d\n",
  296. is_module ? "module" : "lib", ret);
  297. goto out;
  298. }
  299. /*
  300. * if bytes_left > 0 then wait for BDL complete interrupt and
  301. * copy the next chunk till bytes_left is 0. if bytes_left is
  302. * is zero, then wait for load module IPC reply
  303. */
  304. while (bytes_left > 0) {
  305. curr_pos = size - bytes_left;
  306. ret = skl_cldma_wait_interruptible(ctx);
  307. if (ret < 0)
  308. goto out;
  309. bytes_left = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx,
  310. data + curr_pos,
  311. bytes_left, false);
  312. }
  313. ret = wait_event_timeout(skl->mod_load_wait, skl->mod_load_complete,
  314. msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
  315. if (ret == 0 || !skl->mod_load_status) {
  316. dev_err(ctx->dev, "Module Load failed\n");
  317. ret = -EIO;
  318. }
  319. out:
  320. ctx->cl_dev.ops.cl_stop_dma(ctx);
  321. return ret;
  322. }
  323. static int
  324. skl_load_library(struct sst_dsp *ctx, struct skl_lib_info *linfo, int lib_count)
  325. {
  326. struct skl_sst *skl = ctx->thread_context;
  327. struct firmware stripped_fw;
  328. int ret, i;
  329. /* library indices start from 1 to N. 0 represents base FW */
  330. for (i = 1; i < lib_count; i++) {
  331. ret = skl_prepare_lib_load(skl, &skl->lib_info[i], &stripped_fw,
  332. SKL_ADSP_FW_BIN_HDR_OFFSET, i);
  333. if (ret < 0)
  334. goto load_library_failed;
  335. ret = skl_transfer_module(ctx, stripped_fw.data,
  336. stripped_fw.size, 0, i, false);
  337. if (ret < 0)
  338. goto load_library_failed;
  339. }
  340. return 0;
  341. load_library_failed:
  342. skl_release_library(linfo, lib_count);
  343. return ret;
  344. }
  345. static int skl_load_module(struct sst_dsp *ctx, u16 mod_id, u8 *guid)
  346. {
  347. struct skl_module_table *module_entry = NULL;
  348. int ret = 0;
  349. char mod_name[64]; /* guid str = 32 chars + 4 hyphens */
  350. uuid_le *uuid_mod;
  351. uuid_mod = (uuid_le *)guid;
  352. snprintf(mod_name, sizeof(mod_name), "%s%pUL%s",
  353. "intel/dsp_fw_", uuid_mod, ".bin");
  354. module_entry = skl_module_get_from_id(ctx, mod_id);
  355. if (module_entry == NULL) {
  356. module_entry = skl_fill_module_table(ctx, mod_name, mod_id);
  357. if (module_entry == NULL) {
  358. dev_err(ctx->dev, "Failed to Load module\n");
  359. return -EINVAL;
  360. }
  361. }
  362. if (!module_entry->usage_cnt) {
  363. ret = skl_transfer_module(ctx, module_entry->mod_info->fw->data,
  364. module_entry->mod_info->fw->size,
  365. mod_id, 0, true);
  366. if (ret < 0) {
  367. dev_err(ctx->dev, "Failed to Load module\n");
  368. return ret;
  369. }
  370. }
  371. ret = skl_get_module(ctx, mod_id);
  372. return ret;
  373. }
  374. static int skl_unload_module(struct sst_dsp *ctx, u16 mod_id)
  375. {
  376. int usage_cnt;
  377. struct skl_sst *skl = ctx->thread_context;
  378. int ret = 0;
  379. usage_cnt = skl_put_module(ctx, mod_id);
  380. if (usage_cnt < 0) {
  381. dev_err(ctx->dev, "Module bad usage cnt!:%d\n", usage_cnt);
  382. return -EIO;
  383. }
  384. /* if module is used by others return, no need to unload */
  385. if (usage_cnt > 0)
  386. return 0;
  387. ret = skl_ipc_unload_modules(&skl->ipc,
  388. SKL_NUM_MODULES, &mod_id);
  389. if (ret < 0) {
  390. dev_err(ctx->dev, "Failed to UnLoad module\n");
  391. skl_get_module(ctx, mod_id);
  392. return ret;
  393. }
  394. return ret;
  395. }
  396. void skl_clear_module_cnt(struct sst_dsp *ctx)
  397. {
  398. struct skl_module_table *module;
  399. if (list_empty(&ctx->module_list))
  400. return;
  401. list_for_each_entry(module, &ctx->module_list, list) {
  402. module->usage_cnt = 0;
  403. }
  404. }
  405. EXPORT_SYMBOL_GPL(skl_clear_module_cnt);
  406. static void skl_clear_module_table(struct sst_dsp *ctx)
  407. {
  408. struct skl_module_table *module, *tmp;
  409. if (list_empty(&ctx->module_list))
  410. return;
  411. list_for_each_entry_safe(module, tmp, &ctx->module_list, list) {
  412. list_del(&module->list);
  413. release_firmware(module->mod_info->fw);
  414. }
  415. }
  416. static const struct skl_dsp_fw_ops skl_fw_ops = {
  417. .set_state_D0 = skl_set_dsp_D0,
  418. .set_state_D3 = skl_set_dsp_D3,
  419. .load_fw = skl_load_base_firmware,
  420. .get_fw_errcode = skl_get_errorcode,
  421. .load_library = skl_load_library,
  422. .load_mod = skl_load_module,
  423. .unload_mod = skl_unload_module,
  424. };
  425. static struct sst_ops skl_ops = {
  426. .irq_handler = skl_dsp_sst_interrupt,
  427. .write = sst_shim32_write,
  428. .read = sst_shim32_read,
  429. .ram_read = sst_memcpy_fromio_32,
  430. .ram_write = sst_memcpy_toio_32,
  431. .free = skl_dsp_free,
  432. };
  433. static struct sst_dsp_device skl_dev = {
  434. .thread = skl_dsp_irq_thread_handler,
  435. .ops = &skl_ops,
  436. };
  437. int skl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
  438. const char *fw_name, struct skl_dsp_loader_ops dsp_ops, struct skl_sst **dsp)
  439. {
  440. struct skl_sst *skl;
  441. struct sst_dsp *sst;
  442. int ret;
  443. ret = skl_sst_ctx_init(dev, irq, fw_name, dsp_ops, dsp, &skl_dev);
  444. if (ret < 0) {
  445. dev_err(dev, "%s: no device\n", __func__);
  446. return ret;
  447. }
  448. skl = *dsp;
  449. sst = skl->dsp;
  450. sst->addr.lpe = mmio_base;
  451. sst->addr.shim = mmio_base;
  452. sst->addr.sram0_base = SKL_ADSP_SRAM0_BASE;
  453. sst->addr.sram1_base = SKL_ADSP_SRAM1_BASE;
  454. sst->addr.w0_stat_sz = SKL_ADSP_W0_STAT_SZ;
  455. sst->addr.w0_up_sz = SKL_ADSP_W0_UP_SZ;
  456. sst_dsp_mailbox_init(sst, (SKL_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
  457. SKL_ADSP_W0_UP_SZ, SKL_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
  458. ret = skl_ipc_init(dev, skl);
  459. if (ret) {
  460. skl_dsp_free(sst);
  461. return ret;
  462. }
  463. sst->fw_ops = skl_fw_ops;
  464. return skl_dsp_acquire_irq(sst);
  465. }
  466. EXPORT_SYMBOL_GPL(skl_sst_dsp_init);
  467. int skl_sst_init_fw(struct device *dev, struct skl_sst *ctx)
  468. {
  469. int ret;
  470. struct sst_dsp *sst = ctx->dsp;
  471. ret = sst->fw_ops.load_fw(sst);
  472. if (ret < 0) {
  473. dev_err(dev, "Load base fw failed : %d\n", ret);
  474. return ret;
  475. }
  476. skl_dsp_init_core_state(sst);
  477. if (ctx->lib_count > 1) {
  478. ret = sst->fw_ops.load_library(sst, ctx->lib_info,
  479. ctx->lib_count);
  480. if (ret < 0) {
  481. dev_err(dev, "Load Library failed : %x\n", ret);
  482. return ret;
  483. }
  484. }
  485. ctx->is_first_boot = false;
  486. return 0;
  487. }
  488. EXPORT_SYMBOL_GPL(skl_sst_init_fw);
  489. void skl_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
  490. {
  491. if (ctx->dsp->fw)
  492. release_firmware(ctx->dsp->fw);
  493. skl_clear_module_table(ctx->dsp);
  494. skl_freeup_uuid_list(ctx);
  495. skl_ipc_free(&ctx->ipc);
  496. ctx->dsp->ops->free(ctx->dsp);
  497. if (ctx->boot_complete) {
  498. ctx->dsp->cl_dev.ops.cl_cleanup_controller(ctx->dsp);
  499. skl_cldma_int_disable(ctx->dsp);
  500. }
  501. }
  502. EXPORT_SYMBOL_GPL(skl_sst_dsp_cleanup);
  503. MODULE_LICENSE("GPL v2");
  504. MODULE_DESCRIPTION("Intel Skylake IPC driver");