skl-sst-cldma.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * skl-sst-cldma.c - Code Loader DMA handler
  3. *
  4. * Copyright (C) 2015, Intel Corporation.
  5. * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as version 2, as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/device.h>
  18. #include <linux/mm.h>
  19. #include <linux/kthread.h>
  20. #include "../common/sst-dsp.h"
  21. #include "../common/sst-dsp-priv.h"
  22. static void skl_cldma_int_enable(struct sst_dsp *ctx)
  23. {
  24. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPIC,
  25. SKL_ADSPIC_CL_DMA, SKL_ADSPIC_CL_DMA);
  26. }
  27. void skl_cldma_int_disable(struct sst_dsp *ctx)
  28. {
  29. sst_dsp_shim_update_bits_unlocked(ctx,
  30. SKL_ADSP_REG_ADSPIC, SKL_ADSPIC_CL_DMA, 0);
  31. }
  32. /* Code loader helper APIs */
  33. static void skl_cldma_setup_bdle(struct sst_dsp *ctx,
  34. struct snd_dma_buffer *dmab_data,
  35. u32 **bdlp, int size, int with_ioc)
  36. {
  37. u32 *bdl = *bdlp;
  38. ctx->cl_dev.frags = 0;
  39. while (size > 0) {
  40. phys_addr_t addr = virt_to_phys(dmab_data->area +
  41. (ctx->cl_dev.frags * ctx->cl_dev.bufsize));
  42. bdl[0] = cpu_to_le32(lower_32_bits(addr));
  43. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  44. bdl[2] = cpu_to_le32(ctx->cl_dev.bufsize);
  45. size -= ctx->cl_dev.bufsize;
  46. bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
  47. bdl += 4;
  48. ctx->cl_dev.frags++;
  49. }
  50. }
  51. /*
  52. * Setup controller
  53. * Configure the registers to update the dma buffer address and
  54. * enable interrupts.
  55. * Note: Using the channel 1 for transfer
  56. */
  57. static void skl_cldma_setup_controller(struct sst_dsp *ctx,
  58. struct snd_dma_buffer *dmab_bdl, unsigned int max_size,
  59. u32 count)
  60. {
  61. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPL,
  62. CL_SD_BDLPLBA(dmab_bdl->addr));
  63. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPU,
  64. CL_SD_BDLPUBA(dmab_bdl->addr));
  65. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_CBL, max_size);
  66. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_LVI, count - 1);
  67. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  68. CL_SD_CTL_IOCE_MASK, CL_SD_CTL_IOCE(1));
  69. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  70. CL_SD_CTL_FEIE_MASK, CL_SD_CTL_FEIE(1));
  71. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  72. CL_SD_CTL_DEIE_MASK, CL_SD_CTL_DEIE(1));
  73. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  74. CL_SD_CTL_STRM_MASK, CL_SD_CTL_STRM(FW_CL_STREAM_NUMBER));
  75. }
  76. static void skl_cldma_setup_spb(struct sst_dsp *ctx,
  77. unsigned int size, bool enable)
  78. {
  79. if (enable)
  80. sst_dsp_shim_update_bits_unlocked(ctx,
  81. SKL_ADSP_REG_CL_SPBFIFO_SPBFCCTL,
  82. CL_SPBFIFO_SPBFCCTL_SPIBE_MASK,
  83. CL_SPBFIFO_SPBFCCTL_SPIBE(1));
  84. sst_dsp_shim_write_unlocked(ctx, SKL_ADSP_REG_CL_SPBFIFO_SPIB, size);
  85. }
  86. static void skl_cldma_cleanup_spb(struct sst_dsp *ctx)
  87. {
  88. sst_dsp_shim_update_bits_unlocked(ctx,
  89. SKL_ADSP_REG_CL_SPBFIFO_SPBFCCTL,
  90. CL_SPBFIFO_SPBFCCTL_SPIBE_MASK,
  91. CL_SPBFIFO_SPBFCCTL_SPIBE(0));
  92. sst_dsp_shim_write_unlocked(ctx, SKL_ADSP_REG_CL_SPBFIFO_SPIB, 0);
  93. }
  94. static void skl_cldma_trigger(struct sst_dsp *ctx, bool enable)
  95. {
  96. if (enable)
  97. sst_dsp_shim_update_bits_unlocked(ctx,
  98. SKL_ADSP_REG_CL_SD_CTL,
  99. CL_SD_CTL_RUN_MASK, CL_SD_CTL_RUN(1));
  100. else
  101. sst_dsp_shim_update_bits_unlocked(ctx,
  102. SKL_ADSP_REG_CL_SD_CTL,
  103. CL_SD_CTL_RUN_MASK, CL_SD_CTL_RUN(0));
  104. }
  105. static void skl_cldma_cleanup(struct sst_dsp *ctx)
  106. {
  107. skl_cldma_cleanup_spb(ctx);
  108. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  109. CL_SD_CTL_IOCE_MASK, CL_SD_CTL_IOCE(0));
  110. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  111. CL_SD_CTL_FEIE_MASK, CL_SD_CTL_FEIE(0));
  112. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  113. CL_SD_CTL_DEIE_MASK, CL_SD_CTL_DEIE(0));
  114. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  115. CL_SD_CTL_STRM_MASK, CL_SD_CTL_STRM(0));
  116. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPL, CL_SD_BDLPLBA(0));
  117. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPU, 0);
  118. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_CBL, 0);
  119. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_LVI, 0);
  120. }
  121. static int skl_cldma_wait_interruptible(struct sst_dsp *ctx)
  122. {
  123. int ret = 0;
  124. if (!wait_event_timeout(ctx->cl_dev.wait_queue,
  125. ctx->cl_dev.wait_condition,
  126. msecs_to_jiffies(SKL_WAIT_TIMEOUT))) {
  127. dev_err(ctx->dev, "%s: Wait timeout\n", __func__);
  128. ret = -EIO;
  129. goto cleanup;
  130. }
  131. dev_dbg(ctx->dev, "%s: Event wake\n", __func__);
  132. if (ctx->cl_dev.wake_status != SKL_CL_DMA_BUF_COMPLETE) {
  133. dev_err(ctx->dev, "%s: DMA Error\n", __func__);
  134. ret = -EIO;
  135. }
  136. cleanup:
  137. ctx->cl_dev.wake_status = SKL_CL_DMA_STATUS_NONE;
  138. return ret;
  139. }
  140. static void skl_cldma_stop(struct sst_dsp *ctx)
  141. {
  142. ctx->cl_dev.ops.cl_trigger(ctx, false);
  143. }
  144. static void skl_cldma_fill_buffer(struct sst_dsp *ctx, unsigned int size,
  145. const void *curr_pos, bool intr_enable, bool trigger)
  146. {
  147. dev_dbg(ctx->dev, "Size: %x, intr_enable: %d\n", size, intr_enable);
  148. dev_dbg(ctx->dev, "buf_pos_index:%d, trigger:%d\n",
  149. ctx->cl_dev.dma_buffer_offset, trigger);
  150. dev_dbg(ctx->dev, "spib position: %d\n", ctx->cl_dev.curr_spib_pos);
  151. memcpy(ctx->cl_dev.dmab_data.area + ctx->cl_dev.dma_buffer_offset,
  152. curr_pos, size);
  153. if (ctx->cl_dev.curr_spib_pos == ctx->cl_dev.bufsize)
  154. ctx->cl_dev.dma_buffer_offset = 0;
  155. else
  156. ctx->cl_dev.dma_buffer_offset = ctx->cl_dev.curr_spib_pos;
  157. ctx->cl_dev.wait_condition = false;
  158. if (intr_enable)
  159. skl_cldma_int_enable(ctx);
  160. ctx->cl_dev.ops.cl_setup_spb(ctx, ctx->cl_dev.curr_spib_pos, trigger);
  161. if (trigger)
  162. ctx->cl_dev.ops.cl_trigger(ctx, true);
  163. }
  164. /*
  165. * The CL dma doesn't have any way to update the transfer status until a BDL
  166. * buffer is fully transferred
  167. *
  168. * So Copying is divided in two parts.
  169. * 1. Interrupt on buffer done where the size to be transferred is more than
  170. * ring buffer size.
  171. * 2. Polling on fw register to identify if data left to transferred doesn't
  172. * fill the ring buffer. Caller takes care of polling the required status
  173. * register to identify the transfer status.
  174. */
  175. static int
  176. skl_cldma_copy_to_buf(struct sst_dsp *ctx, const void *bin, u32 total_size)
  177. {
  178. int ret = 0;
  179. bool start = true;
  180. unsigned int excess_bytes;
  181. u32 size;
  182. unsigned int bytes_left = total_size;
  183. const void *curr_pos = bin;
  184. if (total_size <= 0)
  185. return -EINVAL;
  186. dev_dbg(ctx->dev, "%s: Total binary size: %u\n", __func__, bytes_left);
  187. while (bytes_left) {
  188. if (bytes_left > ctx->cl_dev.bufsize) {
  189. /*
  190. * dma transfers only till the write pointer as
  191. * updated in spib
  192. */
  193. if (ctx->cl_dev.curr_spib_pos == 0)
  194. ctx->cl_dev.curr_spib_pos = ctx->cl_dev.bufsize;
  195. size = ctx->cl_dev.bufsize;
  196. skl_cldma_fill_buffer(ctx, size, curr_pos, true, start);
  197. start = false;
  198. ret = skl_cldma_wait_interruptible(ctx);
  199. if (ret < 0) {
  200. skl_cldma_stop(ctx);
  201. return ret;
  202. }
  203. } else {
  204. skl_cldma_int_disable(ctx);
  205. if ((ctx->cl_dev.curr_spib_pos + bytes_left)
  206. <= ctx->cl_dev.bufsize) {
  207. ctx->cl_dev.curr_spib_pos += bytes_left;
  208. } else {
  209. excess_bytes = bytes_left -
  210. (ctx->cl_dev.bufsize -
  211. ctx->cl_dev.curr_spib_pos);
  212. ctx->cl_dev.curr_spib_pos = excess_bytes;
  213. }
  214. size = bytes_left;
  215. skl_cldma_fill_buffer(ctx, size,
  216. curr_pos, false, start);
  217. }
  218. bytes_left -= size;
  219. curr_pos = curr_pos + size;
  220. }
  221. return ret;
  222. }
  223. void skl_cldma_process_intr(struct sst_dsp *ctx)
  224. {
  225. u8 cl_dma_intr_status;
  226. cl_dma_intr_status =
  227. sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_CL_SD_STS);
  228. if (!(cl_dma_intr_status & SKL_CL_DMA_SD_INT_COMPLETE))
  229. ctx->cl_dev.wake_status = SKL_CL_DMA_ERR;
  230. else
  231. ctx->cl_dev.wake_status = SKL_CL_DMA_BUF_COMPLETE;
  232. ctx->cl_dev.wait_condition = true;
  233. wake_up(&ctx->cl_dev.wait_queue);
  234. }
  235. int skl_cldma_prepare(struct sst_dsp *ctx)
  236. {
  237. int ret;
  238. u32 *bdl;
  239. ctx->cl_dev.bufsize = SKL_MAX_BUFFER_SIZE;
  240. /* Allocate cl ops */
  241. ctx->cl_dev.ops.cl_setup_bdle = skl_cldma_setup_bdle;
  242. ctx->cl_dev.ops.cl_setup_controller = skl_cldma_setup_controller;
  243. ctx->cl_dev.ops.cl_setup_spb = skl_cldma_setup_spb;
  244. ctx->cl_dev.ops.cl_cleanup_spb = skl_cldma_cleanup_spb;
  245. ctx->cl_dev.ops.cl_trigger = skl_cldma_trigger;
  246. ctx->cl_dev.ops.cl_cleanup_controller = skl_cldma_cleanup;
  247. ctx->cl_dev.ops.cl_copy_to_dmabuf = skl_cldma_copy_to_buf;
  248. ctx->cl_dev.ops.cl_stop_dma = skl_cldma_stop;
  249. /* Allocate buffer*/
  250. ret = ctx->dsp_ops.alloc_dma_buf(ctx->dev,
  251. &ctx->cl_dev.dmab_data, ctx->cl_dev.bufsize);
  252. if (ret < 0) {
  253. dev_err(ctx->dev, "Alloc buffer for base fw failed: %x", ret);
  254. return ret;
  255. }
  256. /* Setup Code loader BDL */
  257. ret = ctx->dsp_ops.alloc_dma_buf(ctx->dev,
  258. &ctx->cl_dev.dmab_bdl, PAGE_SIZE);
  259. if (ret < 0) {
  260. dev_err(ctx->dev, "Alloc buffer for blde failed: %x", ret);
  261. ctx->dsp_ops.free_dma_buf(ctx->dev, &ctx->cl_dev.dmab_data);
  262. return ret;
  263. }
  264. bdl = (u32 *)ctx->cl_dev.dmab_bdl.area;
  265. /* Allocate BDLs */
  266. ctx->cl_dev.ops.cl_setup_bdle(ctx, &ctx->cl_dev.dmab_data,
  267. &bdl, ctx->cl_dev.bufsize, 1);
  268. ctx->cl_dev.ops.cl_setup_controller(ctx, &ctx->cl_dev.dmab_bdl,
  269. ctx->cl_dev.bufsize, ctx->cl_dev.frags);
  270. ctx->cl_dev.curr_spib_pos = 0;
  271. ctx->cl_dev.dma_buffer_offset = 0;
  272. init_waitqueue_head(&ctx->cl_dev.wait_queue);
  273. return ret;
  274. }