skl-sst-cldma.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * skl-sst-cldma.c - Code Loader DMA handler
  4. *
  5. * Copyright (C) 2015, Intel Corporation.
  6. * Author: Subhransu S. Prusty <[email protected]>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. */
  9. #include <linux/device.h>
  10. #include <linux/io.h>
  11. #include <linux/mm.h>
  12. #include <linux/delay.h>
  13. #include "../common/sst-dsp.h"
  14. #include "../common/sst-dsp-priv.h"
  15. static void skl_cldma_int_enable(struct sst_dsp *ctx)
  16. {
  17. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPIC,
  18. SKL_ADSPIC_CL_DMA, SKL_ADSPIC_CL_DMA);
  19. }
  20. void skl_cldma_int_disable(struct sst_dsp *ctx)
  21. {
  22. sst_dsp_shim_update_bits_unlocked(ctx,
  23. SKL_ADSP_REG_ADSPIC, SKL_ADSPIC_CL_DMA, 0);
  24. }
  25. static void skl_cldma_stream_run(struct sst_dsp *ctx, bool enable)
  26. {
  27. unsigned char val;
  28. int timeout;
  29. sst_dsp_shim_update_bits_unlocked(ctx,
  30. SKL_ADSP_REG_CL_SD_CTL,
  31. CL_SD_CTL_RUN_MASK, CL_SD_CTL_RUN(enable));
  32. udelay(3);
  33. timeout = 300;
  34. do {
  35. /* waiting for hardware to report that the stream Run bit set */
  36. val = sst_dsp_shim_read(ctx, SKL_ADSP_REG_CL_SD_CTL) &
  37. CL_SD_CTL_RUN_MASK;
  38. if (enable && val)
  39. break;
  40. else if (!enable && !val)
  41. break;
  42. udelay(3);
  43. } while (--timeout);
  44. if (timeout == 0)
  45. dev_err(ctx->dev, "Failed to set Run bit=%d enable=%d\n", val, enable);
  46. }
  47. static void skl_cldma_stream_clear(struct sst_dsp *ctx)
  48. {
  49. /* make sure Run bit is cleared before setting stream register */
  50. skl_cldma_stream_run(ctx, 0);
  51. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  52. CL_SD_CTL_IOCE_MASK, CL_SD_CTL_IOCE(0));
  53. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  54. CL_SD_CTL_FEIE_MASK, CL_SD_CTL_FEIE(0));
  55. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  56. CL_SD_CTL_DEIE_MASK, CL_SD_CTL_DEIE(0));
  57. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  58. CL_SD_CTL_STRM_MASK, CL_SD_CTL_STRM(0));
  59. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPL, CL_SD_BDLPLBA(0));
  60. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPU, 0);
  61. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_CBL, 0);
  62. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_LVI, 0);
  63. }
  64. /* Code loader helper APIs */
  65. static void skl_cldma_setup_bdle(struct sst_dsp *ctx,
  66. struct snd_dma_buffer *dmab_data,
  67. __le32 **bdlp, int size, int with_ioc)
  68. {
  69. __le32 *bdl = *bdlp;
  70. ctx->cl_dev.frags = 0;
  71. while (size > 0) {
  72. phys_addr_t addr = virt_to_phys(dmab_data->area +
  73. (ctx->cl_dev.frags * ctx->cl_dev.bufsize));
  74. bdl[0] = cpu_to_le32(lower_32_bits(addr));
  75. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  76. bdl[2] = cpu_to_le32(ctx->cl_dev.bufsize);
  77. size -= ctx->cl_dev.bufsize;
  78. bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
  79. bdl += 4;
  80. ctx->cl_dev.frags++;
  81. }
  82. }
  83. /*
  84. * Setup controller
  85. * Configure the registers to update the dma buffer address and
  86. * enable interrupts.
  87. * Note: Using the channel 1 for transfer
  88. */
  89. static void skl_cldma_setup_controller(struct sst_dsp *ctx,
  90. struct snd_dma_buffer *dmab_bdl, unsigned int max_size,
  91. u32 count)
  92. {
  93. skl_cldma_stream_clear(ctx);
  94. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPL,
  95. CL_SD_BDLPLBA(dmab_bdl->addr));
  96. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPU,
  97. CL_SD_BDLPUBA(dmab_bdl->addr));
  98. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_CBL, max_size);
  99. sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_LVI, count - 1);
  100. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  101. CL_SD_CTL_IOCE_MASK, CL_SD_CTL_IOCE(1));
  102. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  103. CL_SD_CTL_FEIE_MASK, CL_SD_CTL_FEIE(1));
  104. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  105. CL_SD_CTL_DEIE_MASK, CL_SD_CTL_DEIE(1));
  106. sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
  107. CL_SD_CTL_STRM_MASK, CL_SD_CTL_STRM(FW_CL_STREAM_NUMBER));
  108. }
  109. static void skl_cldma_setup_spb(struct sst_dsp *ctx,
  110. unsigned int size, bool enable)
  111. {
  112. if (enable)
  113. sst_dsp_shim_update_bits_unlocked(ctx,
  114. SKL_ADSP_REG_CL_SPBFIFO_SPBFCCTL,
  115. CL_SPBFIFO_SPBFCCTL_SPIBE_MASK,
  116. CL_SPBFIFO_SPBFCCTL_SPIBE(1));
  117. sst_dsp_shim_write_unlocked(ctx, SKL_ADSP_REG_CL_SPBFIFO_SPIB, size);
  118. }
  119. static void skl_cldma_cleanup_spb(struct sst_dsp *ctx)
  120. {
  121. sst_dsp_shim_update_bits_unlocked(ctx,
  122. SKL_ADSP_REG_CL_SPBFIFO_SPBFCCTL,
  123. CL_SPBFIFO_SPBFCCTL_SPIBE_MASK,
  124. CL_SPBFIFO_SPBFCCTL_SPIBE(0));
  125. sst_dsp_shim_write_unlocked(ctx, SKL_ADSP_REG_CL_SPBFIFO_SPIB, 0);
  126. }
  127. static void skl_cldma_cleanup(struct sst_dsp *ctx)
  128. {
  129. skl_cldma_cleanup_spb(ctx);
  130. skl_cldma_stream_clear(ctx);
  131. ctx->dsp_ops.free_dma_buf(ctx->dev, &ctx->cl_dev.dmab_data);
  132. ctx->dsp_ops.free_dma_buf(ctx->dev, &ctx->cl_dev.dmab_bdl);
  133. }
  134. int skl_cldma_wait_interruptible(struct sst_dsp *ctx)
  135. {
  136. int ret = 0;
  137. if (!wait_event_timeout(ctx->cl_dev.wait_queue,
  138. ctx->cl_dev.wait_condition,
  139. msecs_to_jiffies(SKL_WAIT_TIMEOUT))) {
  140. dev_err(ctx->dev, "%s: Wait timeout\n", __func__);
  141. ret = -EIO;
  142. goto cleanup;
  143. }
  144. dev_dbg(ctx->dev, "%s: Event wake\n", __func__);
  145. if (ctx->cl_dev.wake_status != SKL_CL_DMA_BUF_COMPLETE) {
  146. dev_err(ctx->dev, "%s: DMA Error\n", __func__);
  147. ret = -EIO;
  148. }
  149. cleanup:
  150. ctx->cl_dev.wake_status = SKL_CL_DMA_STATUS_NONE;
  151. return ret;
  152. }
  153. static void skl_cldma_stop(struct sst_dsp *ctx)
  154. {
  155. skl_cldma_stream_run(ctx, false);
  156. }
  157. static void skl_cldma_fill_buffer(struct sst_dsp *ctx, unsigned int size,
  158. const void *curr_pos, bool intr_enable, bool trigger)
  159. {
  160. dev_dbg(ctx->dev, "Size: %x, intr_enable: %d\n", size, intr_enable);
  161. dev_dbg(ctx->dev, "buf_pos_index:%d, trigger:%d\n",
  162. ctx->cl_dev.dma_buffer_offset, trigger);
  163. dev_dbg(ctx->dev, "spib position: %d\n", ctx->cl_dev.curr_spib_pos);
  164. /*
  165. * Check if the size exceeds buffer boundary. If it exceeds
  166. * max_buffer size, then copy till buffer size and then copy
  167. * remaining buffer from the start of ring buffer.
  168. */
  169. if (ctx->cl_dev.dma_buffer_offset + size > ctx->cl_dev.bufsize) {
  170. unsigned int size_b = ctx->cl_dev.bufsize -
  171. ctx->cl_dev.dma_buffer_offset;
  172. memcpy(ctx->cl_dev.dmab_data.area + ctx->cl_dev.dma_buffer_offset,
  173. curr_pos, size_b);
  174. size -= size_b;
  175. curr_pos += size_b;
  176. ctx->cl_dev.dma_buffer_offset = 0;
  177. }
  178. memcpy(ctx->cl_dev.dmab_data.area + ctx->cl_dev.dma_buffer_offset,
  179. curr_pos, size);
  180. if (ctx->cl_dev.curr_spib_pos == ctx->cl_dev.bufsize)
  181. ctx->cl_dev.dma_buffer_offset = 0;
  182. else
  183. ctx->cl_dev.dma_buffer_offset = ctx->cl_dev.curr_spib_pos;
  184. ctx->cl_dev.wait_condition = false;
  185. if (intr_enable)
  186. skl_cldma_int_enable(ctx);
  187. ctx->cl_dev.ops.cl_setup_spb(ctx, ctx->cl_dev.curr_spib_pos, trigger);
  188. if (trigger)
  189. ctx->cl_dev.ops.cl_trigger(ctx, true);
  190. }
  191. /*
  192. * The CL dma doesn't have any way to update the transfer status until a BDL
  193. * buffer is fully transferred
  194. *
  195. * So Copying is divided in two parts.
  196. * 1. Interrupt on buffer done where the size to be transferred is more than
  197. * ring buffer size.
  198. * 2. Polling on fw register to identify if data left to transferred doesn't
  199. * fill the ring buffer. Caller takes care of polling the required status
  200. * register to identify the transfer status.
  201. * 3. if wait flag is set, waits for DBL interrupt to copy the next chunk till
  202. * bytes_left is 0.
  203. * if wait flag is not set, doesn't wait for BDL interrupt. after ccopying
  204. * the first chunk return the no of bytes_left to be copied.
  205. */
  206. static int
  207. skl_cldma_copy_to_buf(struct sst_dsp *ctx, const void *bin,
  208. u32 total_size, bool wait)
  209. {
  210. int ret;
  211. bool start = true;
  212. unsigned int excess_bytes;
  213. u32 size;
  214. unsigned int bytes_left = total_size;
  215. const void *curr_pos = bin;
  216. if (total_size <= 0)
  217. return -EINVAL;
  218. dev_dbg(ctx->dev, "%s: Total binary size: %u\n", __func__, bytes_left);
  219. while (bytes_left) {
  220. if (bytes_left > ctx->cl_dev.bufsize) {
  221. /*
  222. * dma transfers only till the write pointer as
  223. * updated in spib
  224. */
  225. if (ctx->cl_dev.curr_spib_pos == 0)
  226. ctx->cl_dev.curr_spib_pos = ctx->cl_dev.bufsize;
  227. size = ctx->cl_dev.bufsize;
  228. skl_cldma_fill_buffer(ctx, size, curr_pos, true, start);
  229. if (wait) {
  230. start = false;
  231. ret = skl_cldma_wait_interruptible(ctx);
  232. if (ret < 0) {
  233. skl_cldma_stop(ctx);
  234. return ret;
  235. }
  236. }
  237. } else {
  238. skl_cldma_int_disable(ctx);
  239. if ((ctx->cl_dev.curr_spib_pos + bytes_left)
  240. <= ctx->cl_dev.bufsize) {
  241. ctx->cl_dev.curr_spib_pos += bytes_left;
  242. } else {
  243. excess_bytes = bytes_left -
  244. (ctx->cl_dev.bufsize -
  245. ctx->cl_dev.curr_spib_pos);
  246. ctx->cl_dev.curr_spib_pos = excess_bytes;
  247. }
  248. size = bytes_left;
  249. skl_cldma_fill_buffer(ctx, size,
  250. curr_pos, false, start);
  251. }
  252. bytes_left -= size;
  253. curr_pos = curr_pos + size;
  254. if (!wait)
  255. return bytes_left;
  256. }
  257. return bytes_left;
  258. }
  259. void skl_cldma_process_intr(struct sst_dsp *ctx)
  260. {
  261. u8 cl_dma_intr_status;
  262. cl_dma_intr_status =
  263. sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_CL_SD_STS);
  264. if (!(cl_dma_intr_status & SKL_CL_DMA_SD_INT_COMPLETE))
  265. ctx->cl_dev.wake_status = SKL_CL_DMA_ERR;
  266. else
  267. ctx->cl_dev.wake_status = SKL_CL_DMA_BUF_COMPLETE;
  268. ctx->cl_dev.wait_condition = true;
  269. wake_up(&ctx->cl_dev.wait_queue);
  270. }
  271. int skl_cldma_prepare(struct sst_dsp *ctx)
  272. {
  273. int ret;
  274. __le32 *bdl;
  275. ctx->cl_dev.bufsize = SKL_MAX_BUFFER_SIZE;
  276. /* Allocate cl ops */
  277. ctx->cl_dev.ops.cl_setup_bdle = skl_cldma_setup_bdle;
  278. ctx->cl_dev.ops.cl_setup_controller = skl_cldma_setup_controller;
  279. ctx->cl_dev.ops.cl_setup_spb = skl_cldma_setup_spb;
  280. ctx->cl_dev.ops.cl_cleanup_spb = skl_cldma_cleanup_spb;
  281. ctx->cl_dev.ops.cl_trigger = skl_cldma_stream_run;
  282. ctx->cl_dev.ops.cl_cleanup_controller = skl_cldma_cleanup;
  283. ctx->cl_dev.ops.cl_copy_to_dmabuf = skl_cldma_copy_to_buf;
  284. ctx->cl_dev.ops.cl_stop_dma = skl_cldma_stop;
  285. /* Allocate buffer*/
  286. ret = ctx->dsp_ops.alloc_dma_buf(ctx->dev,
  287. &ctx->cl_dev.dmab_data, ctx->cl_dev.bufsize);
  288. if (ret < 0) {
  289. dev_err(ctx->dev, "Alloc buffer for base fw failed: %x\n", ret);
  290. return ret;
  291. }
  292. /* Setup Code loader BDL */
  293. ret = ctx->dsp_ops.alloc_dma_buf(ctx->dev,
  294. &ctx->cl_dev.dmab_bdl, PAGE_SIZE);
  295. if (ret < 0) {
  296. dev_err(ctx->dev, "Alloc buffer for blde failed: %x\n", ret);
  297. ctx->dsp_ops.free_dma_buf(ctx->dev, &ctx->cl_dev.dmab_data);
  298. return ret;
  299. }
  300. bdl = (__le32 *)ctx->cl_dev.dmab_bdl.area;
  301. /* Allocate BDLs */
  302. ctx->cl_dev.ops.cl_setup_bdle(ctx, &ctx->cl_dev.dmab_data,
  303. &bdl, ctx->cl_dev.bufsize, 1);
  304. ctx->cl_dev.ops.cl_setup_controller(ctx, &ctx->cl_dev.dmab_bdl,
  305. ctx->cl_dev.bufsize, ctx->cl_dev.frags);
  306. ctx->cl_dev.curr_spib_pos = 0;
  307. ctx->cl_dev.dma_buffer_offset = 0;
  308. init_waitqueue_head(&ctx->cl_dev.wait_queue);
  309. return ret;
  310. }