skl-sst-dsp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * skl-sst-dsp.c - SKL SST library generic function
  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 <sound/pcm.h>
  19. #include "../common/sst-dsp.h"
  20. #include "../common/sst-ipc.h"
  21. #include "../common/sst-dsp-priv.h"
  22. #include "skl-sst-ipc.h"
  23. /* various timeout values */
  24. #define SKL_DSP_PU_TO 50
  25. #define SKL_DSP_PD_TO 50
  26. #define SKL_DSP_RESET_TO 50
  27. void skl_dsp_set_state_locked(struct sst_dsp *ctx, int state)
  28. {
  29. mutex_lock(&ctx->mutex);
  30. ctx->sst_state = state;
  31. mutex_unlock(&ctx->mutex);
  32. }
  33. /*
  34. * Initialize core power state and usage count. To be called after
  35. * successful first boot. Hence core 0 will be running and other cores
  36. * will be reset
  37. */
  38. void skl_dsp_init_core_state(struct sst_dsp *ctx)
  39. {
  40. struct skl_sst *skl = ctx->thread_context;
  41. int i;
  42. skl->cores.state[SKL_DSP_CORE0_ID] = SKL_DSP_RUNNING;
  43. skl->cores.usage_count[SKL_DSP_CORE0_ID] = 1;
  44. for (i = SKL_DSP_CORE0_ID + 1; i < SKL_DSP_CORES_MAX; i++) {
  45. skl->cores.state[i] = SKL_DSP_RESET;
  46. skl->cores.usage_count[i] = 0;
  47. }
  48. }
  49. /* Get the mask for all enabled cores */
  50. unsigned int skl_dsp_get_enabled_cores(struct sst_dsp *ctx)
  51. {
  52. struct skl_sst *skl = ctx->thread_context;
  53. unsigned int core_mask, en_cores_mask;
  54. u32 val;
  55. core_mask = SKL_DSP_CORES_MASK(skl->cores.count);
  56. val = sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS);
  57. /* Cores having CPA bit set */
  58. en_cores_mask = (val & SKL_ADSPCS_CPA_MASK(core_mask)) >>
  59. SKL_ADSPCS_CPA_SHIFT;
  60. /* And cores having CRST bit cleared */
  61. en_cores_mask &= (~val & SKL_ADSPCS_CRST_MASK(core_mask)) >>
  62. SKL_ADSPCS_CRST_SHIFT;
  63. /* And cores having CSTALL bit cleared */
  64. en_cores_mask &= (~val & SKL_ADSPCS_CSTALL_MASK(core_mask)) >>
  65. SKL_ADSPCS_CSTALL_SHIFT;
  66. en_cores_mask &= core_mask;
  67. dev_dbg(ctx->dev, "DSP enabled cores mask = %x\n", en_cores_mask);
  68. return en_cores_mask;
  69. }
  70. static int
  71. skl_dsp_core_set_reset_state(struct sst_dsp *ctx, unsigned int core_mask)
  72. {
  73. int ret;
  74. /* update bits */
  75. sst_dsp_shim_update_bits_unlocked(ctx,
  76. SKL_ADSP_REG_ADSPCS, SKL_ADSPCS_CRST_MASK(core_mask),
  77. SKL_ADSPCS_CRST_MASK(core_mask));
  78. /* poll with timeout to check if operation successful */
  79. ret = sst_dsp_register_poll(ctx,
  80. SKL_ADSP_REG_ADSPCS,
  81. SKL_ADSPCS_CRST_MASK(core_mask),
  82. SKL_ADSPCS_CRST_MASK(core_mask),
  83. SKL_DSP_RESET_TO,
  84. "Set reset");
  85. if ((sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
  86. SKL_ADSPCS_CRST_MASK(core_mask)) !=
  87. SKL_ADSPCS_CRST_MASK(core_mask)) {
  88. dev_err(ctx->dev, "Set reset state failed: core_mask %x\n",
  89. core_mask);
  90. ret = -EIO;
  91. }
  92. return ret;
  93. }
  94. int skl_dsp_core_unset_reset_state(
  95. struct sst_dsp *ctx, unsigned int core_mask)
  96. {
  97. int ret;
  98. dev_dbg(ctx->dev, "In %s\n", __func__);
  99. /* update bits */
  100. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  101. SKL_ADSPCS_CRST_MASK(core_mask), 0);
  102. /* poll with timeout to check if operation successful */
  103. ret = sst_dsp_register_poll(ctx,
  104. SKL_ADSP_REG_ADSPCS,
  105. SKL_ADSPCS_CRST_MASK(core_mask),
  106. 0,
  107. SKL_DSP_RESET_TO,
  108. "Unset reset");
  109. if ((sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
  110. SKL_ADSPCS_CRST_MASK(core_mask)) != 0) {
  111. dev_err(ctx->dev, "Unset reset state failed: core_mask %x\n",
  112. core_mask);
  113. ret = -EIO;
  114. }
  115. return ret;
  116. }
  117. static bool
  118. is_skl_dsp_core_enable(struct sst_dsp *ctx, unsigned int core_mask)
  119. {
  120. int val;
  121. bool is_enable;
  122. val = sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS);
  123. is_enable = ((val & SKL_ADSPCS_CPA_MASK(core_mask)) &&
  124. (val & SKL_ADSPCS_SPA_MASK(core_mask)) &&
  125. !(val & SKL_ADSPCS_CRST_MASK(core_mask)) &&
  126. !(val & SKL_ADSPCS_CSTALL_MASK(core_mask)));
  127. dev_dbg(ctx->dev, "DSP core(s) enabled? %d : core_mask %x\n",
  128. is_enable, core_mask);
  129. return is_enable;
  130. }
  131. static int skl_dsp_reset_core(struct sst_dsp *ctx, unsigned int core_mask)
  132. {
  133. /* stall core */
  134. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  135. SKL_ADSPCS_CSTALL_MASK(core_mask),
  136. SKL_ADSPCS_CSTALL_MASK(core_mask));
  137. /* set reset state */
  138. return skl_dsp_core_set_reset_state(ctx, core_mask);
  139. }
  140. int skl_dsp_start_core(struct sst_dsp *ctx, unsigned int core_mask)
  141. {
  142. int ret;
  143. /* unset reset state */
  144. ret = skl_dsp_core_unset_reset_state(ctx, core_mask);
  145. if (ret < 0)
  146. return ret;
  147. /* run core */
  148. dev_dbg(ctx->dev, "unstall/run core: core_mask = %x\n", core_mask);
  149. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  150. SKL_ADSPCS_CSTALL_MASK(core_mask), 0);
  151. if (!is_skl_dsp_core_enable(ctx, core_mask)) {
  152. skl_dsp_reset_core(ctx, core_mask);
  153. dev_err(ctx->dev, "DSP start core failed: core_mask %x\n",
  154. core_mask);
  155. ret = -EIO;
  156. }
  157. return ret;
  158. }
  159. int skl_dsp_core_power_up(struct sst_dsp *ctx, unsigned int core_mask)
  160. {
  161. int ret;
  162. /* update bits */
  163. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  164. SKL_ADSPCS_SPA_MASK(core_mask),
  165. SKL_ADSPCS_SPA_MASK(core_mask));
  166. /* poll with timeout to check if operation successful */
  167. ret = sst_dsp_register_poll(ctx,
  168. SKL_ADSP_REG_ADSPCS,
  169. SKL_ADSPCS_CPA_MASK(core_mask),
  170. SKL_ADSPCS_CPA_MASK(core_mask),
  171. SKL_DSP_PU_TO,
  172. "Power up");
  173. if ((sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
  174. SKL_ADSPCS_CPA_MASK(core_mask)) !=
  175. SKL_ADSPCS_CPA_MASK(core_mask)) {
  176. dev_err(ctx->dev, "DSP core power up failed: core_mask %x\n",
  177. core_mask);
  178. ret = -EIO;
  179. }
  180. return ret;
  181. }
  182. int skl_dsp_core_power_down(struct sst_dsp *ctx, unsigned int core_mask)
  183. {
  184. /* update bits */
  185. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  186. SKL_ADSPCS_SPA_MASK(core_mask), 0);
  187. /* poll with timeout to check if operation successful */
  188. return sst_dsp_register_poll(ctx,
  189. SKL_ADSP_REG_ADSPCS,
  190. SKL_ADSPCS_CPA_MASK(core_mask),
  191. 0,
  192. SKL_DSP_PD_TO,
  193. "Power down");
  194. }
  195. int skl_dsp_enable_core(struct sst_dsp *ctx, unsigned int core_mask)
  196. {
  197. int ret;
  198. /* power up */
  199. ret = skl_dsp_core_power_up(ctx, core_mask);
  200. if (ret < 0) {
  201. dev_err(ctx->dev, "dsp core power up failed: core_mask %x\n",
  202. core_mask);
  203. return ret;
  204. }
  205. return skl_dsp_start_core(ctx, core_mask);
  206. }
  207. int skl_dsp_disable_core(struct sst_dsp *ctx, unsigned int core_mask)
  208. {
  209. int ret;
  210. ret = skl_dsp_reset_core(ctx, core_mask);
  211. if (ret < 0) {
  212. dev_err(ctx->dev, "dsp core reset failed: core_mask %x\n",
  213. core_mask);
  214. return ret;
  215. }
  216. /* power down core*/
  217. ret = skl_dsp_core_power_down(ctx, core_mask);
  218. if (ret < 0) {
  219. dev_err(ctx->dev, "dsp core power down fail mask %x: %d\n",
  220. core_mask, ret);
  221. return ret;
  222. }
  223. if (is_skl_dsp_core_enable(ctx, core_mask)) {
  224. dev_err(ctx->dev, "dsp core disable fail mask %x: %d\n",
  225. core_mask, ret);
  226. ret = -EIO;
  227. }
  228. return ret;
  229. }
  230. int skl_dsp_boot(struct sst_dsp *ctx)
  231. {
  232. int ret;
  233. if (is_skl_dsp_core_enable(ctx, SKL_DSP_CORE0_MASK)) {
  234. ret = skl_dsp_reset_core(ctx, SKL_DSP_CORE0_MASK);
  235. if (ret < 0) {
  236. dev_err(ctx->dev, "dsp core0 reset fail: %d\n", ret);
  237. return ret;
  238. }
  239. ret = skl_dsp_start_core(ctx, SKL_DSP_CORE0_MASK);
  240. if (ret < 0) {
  241. dev_err(ctx->dev, "dsp core0 start fail: %d\n", ret);
  242. return ret;
  243. }
  244. } else {
  245. ret = skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
  246. if (ret < 0) {
  247. dev_err(ctx->dev, "dsp core0 disable fail: %d\n", ret);
  248. return ret;
  249. }
  250. ret = skl_dsp_enable_core(ctx, SKL_DSP_CORE0_MASK);
  251. }
  252. return ret;
  253. }
  254. irqreturn_t skl_dsp_sst_interrupt(int irq, void *dev_id)
  255. {
  256. struct sst_dsp *ctx = dev_id;
  257. u32 val;
  258. irqreturn_t result = IRQ_NONE;
  259. spin_lock(&ctx->spinlock);
  260. val = sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPIS);
  261. ctx->intr_status = val;
  262. if (val == 0xffffffff) {
  263. spin_unlock(&ctx->spinlock);
  264. return IRQ_NONE;
  265. }
  266. if (val & SKL_ADSPIS_IPC) {
  267. skl_ipc_int_disable(ctx);
  268. result = IRQ_WAKE_THREAD;
  269. }
  270. if (val & SKL_ADSPIS_CL_DMA) {
  271. skl_cldma_int_disable(ctx);
  272. result = IRQ_WAKE_THREAD;
  273. }
  274. spin_unlock(&ctx->spinlock);
  275. return result;
  276. }
  277. /*
  278. * skl_dsp_get_core/skl_dsp_put_core will be called inside DAPM context
  279. * within the dapm mutex. Hence no separate lock is used.
  280. */
  281. int skl_dsp_get_core(struct sst_dsp *ctx, unsigned int core_id)
  282. {
  283. struct skl_sst *skl = ctx->thread_context;
  284. int ret = 0;
  285. if (core_id >= skl->cores.count) {
  286. dev_err(ctx->dev, "invalid core id: %d\n", core_id);
  287. return -EINVAL;
  288. }
  289. if (skl->cores.state[core_id] == SKL_DSP_RESET) {
  290. ret = ctx->fw_ops.set_state_D0(ctx, core_id);
  291. if (ret < 0) {
  292. dev_err(ctx->dev, "unable to get core%d\n", core_id);
  293. return ret;
  294. }
  295. }
  296. skl->cores.usage_count[core_id]++;
  297. dev_dbg(ctx->dev, "core id %d state %d usage_count %d\n",
  298. core_id, skl->cores.state[core_id],
  299. skl->cores.usage_count[core_id]);
  300. return ret;
  301. }
  302. EXPORT_SYMBOL_GPL(skl_dsp_get_core);
  303. int skl_dsp_put_core(struct sst_dsp *ctx, unsigned int core_id)
  304. {
  305. struct skl_sst *skl = ctx->thread_context;
  306. int ret = 0;
  307. if (core_id >= skl->cores.count) {
  308. dev_err(ctx->dev, "invalid core id: %d\n", core_id);
  309. return -EINVAL;
  310. }
  311. if (--skl->cores.usage_count[core_id] == 0) {
  312. ret = ctx->fw_ops.set_state_D3(ctx, core_id);
  313. if (ret < 0) {
  314. dev_err(ctx->dev, "unable to put core %d: %d\n",
  315. core_id, ret);
  316. skl->cores.usage_count[core_id]++;
  317. }
  318. }
  319. dev_dbg(ctx->dev, "core id %d state %d usage_count %d\n",
  320. core_id, skl->cores.state[core_id],
  321. skl->cores.usage_count[core_id]);
  322. return ret;
  323. }
  324. EXPORT_SYMBOL_GPL(skl_dsp_put_core);
  325. int skl_dsp_wake(struct sst_dsp *ctx)
  326. {
  327. return skl_dsp_get_core(ctx, SKL_DSP_CORE0_ID);
  328. }
  329. EXPORT_SYMBOL_GPL(skl_dsp_wake);
  330. int skl_dsp_sleep(struct sst_dsp *ctx)
  331. {
  332. return skl_dsp_put_core(ctx, SKL_DSP_CORE0_ID);
  333. }
  334. EXPORT_SYMBOL_GPL(skl_dsp_sleep);
  335. struct sst_dsp *skl_dsp_ctx_init(struct device *dev,
  336. struct sst_dsp_device *sst_dev, int irq)
  337. {
  338. int ret;
  339. struct sst_dsp *sst;
  340. sst = devm_kzalloc(dev, sizeof(*sst), GFP_KERNEL);
  341. if (sst == NULL)
  342. return NULL;
  343. spin_lock_init(&sst->spinlock);
  344. mutex_init(&sst->mutex);
  345. sst->dev = dev;
  346. sst->sst_dev = sst_dev;
  347. sst->irq = irq;
  348. sst->ops = sst_dev->ops;
  349. sst->thread_context = sst_dev->thread_context;
  350. /* Initialise SST Audio DSP */
  351. if (sst->ops->init) {
  352. ret = sst->ops->init(sst, NULL);
  353. if (ret < 0)
  354. return NULL;
  355. }
  356. /* Register the ISR */
  357. ret = request_threaded_irq(sst->irq, sst->ops->irq_handler,
  358. sst_dev->thread, IRQF_SHARED, "AudioDSP", sst);
  359. if (ret) {
  360. dev_err(sst->dev, "unable to grab threaded IRQ %d, disabling device\n",
  361. sst->irq);
  362. return NULL;
  363. }
  364. return sst;
  365. }
  366. void skl_dsp_free(struct sst_dsp *dsp)
  367. {
  368. skl_ipc_int_disable(dsp);
  369. free_irq(dsp->irq, dsp);
  370. skl_ipc_op_int_disable(dsp);
  371. skl_dsp_disable_core(dsp, SKL_DSP_CORE0_MASK);
  372. }
  373. EXPORT_SYMBOL_GPL(skl_dsp_free);
  374. bool is_skl_dsp_running(struct sst_dsp *ctx)
  375. {
  376. return (ctx->sst_state == SKL_DSP_RUNNING);
  377. }
  378. EXPORT_SYMBOL_GPL(is_skl_dsp_running);