skl-sst-cldma.c 10 KB

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