1 // SPDX-License-Identifier: GPL-2.0-only
3 * skl-sst-cldma.c - Code Loader DMA handler
5 * Copyright (C) 2015, Intel Corporation.
6 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 #include <linux/device.h>
13 #include <linux/delay.h>
14 #include <sound/hda_register.h>
15 #include "../common/sst-dsp.h"
16 #include "../common/sst-dsp-priv.h"
18 static void skl_cldma_int_enable(struct sst_dsp *ctx)
20 sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPIC,
21 SKL_ADSPIC_CL_DMA, SKL_ADSPIC_CL_DMA);
24 void skl_cldma_int_disable(struct sst_dsp *ctx)
26 sst_dsp_shim_update_bits_unlocked(ctx,
27 SKL_ADSP_REG_ADSPIC, SKL_ADSPIC_CL_DMA, 0);
30 static void skl_cldma_stream_run(struct sst_dsp *ctx, bool enable)
35 sst_dsp_shim_update_bits_unlocked(ctx,
36 SKL_ADSP_REG_CL_SD_CTL,
37 CL_SD_CTL_RUN_MASK, CL_SD_CTL_RUN(enable));
42 /* waiting for hardware to report that the stream Run bit set */
43 val = sst_dsp_shim_read(ctx, SKL_ADSP_REG_CL_SD_CTL) &
47 else if (!enable && !val)
53 dev_err(ctx->dev, "Failed to set Run bit=%d enable=%d\n", val, enable);
56 static void skl_cldma_stream_clear(struct sst_dsp *ctx)
58 /* make sure Run bit is cleared before setting stream register */
59 skl_cldma_stream_run(ctx, 0);
61 sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
62 CL_SD_CTL_IOCE_MASK, CL_SD_CTL_IOCE(0));
63 sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
64 CL_SD_CTL_FEIE_MASK, CL_SD_CTL_FEIE(0));
65 sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
66 CL_SD_CTL_DEIE_MASK, CL_SD_CTL_DEIE(0));
67 sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
68 CL_SD_CTL_STRM_MASK, CL_SD_CTL_STRM(0));
70 sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPL, CL_SD_BDLPLBA(0));
71 sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPU, 0);
73 sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_CBL, 0);
74 sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_LVI, 0);
77 /* Code loader helper APIs */
78 static void skl_cldma_setup_bdle(struct sst_dsp *ctx,
79 struct snd_dma_buffer *dmab_data,
80 __le32 **bdlp, int size, int with_ioc)
83 int remaining = ctx->cl_dev.bufsize;
86 ctx->cl_dev.frags = 0;
87 while (remaining > 0) {
91 addr = snd_sgbuf_get_addr(dmab_data, offset);
92 bdl[0] = cpu_to_le32(lower_32_bits(addr));
93 bdl[1] = cpu_to_le32(upper_32_bits(addr));
94 chunk = snd_sgbuf_get_chunk_size(dmab_data, offset, size);
95 bdl[2] = cpu_to_le32(chunk);
98 bdl[3] = (remaining > 0) ? 0 : cpu_to_le32(0x01);
108 * Configure the registers to update the dma buffer address and
110 * Note: Using the channel 1 for transfer
112 static void skl_cldma_setup_controller(struct sst_dsp *ctx,
113 struct snd_dma_buffer *dmab_bdl, unsigned int max_size,
116 skl_cldma_stream_clear(ctx);
117 sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPL,
118 CL_SD_BDLPLBA(dmab_bdl->addr));
119 sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_BDLPU,
120 CL_SD_BDLPUBA(dmab_bdl->addr));
122 sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_CBL, max_size);
123 sst_dsp_shim_write(ctx, SKL_ADSP_REG_CL_SD_LVI, count - 1);
124 sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
125 CL_SD_CTL_IOCE_MASK, CL_SD_CTL_IOCE(1));
126 sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
127 CL_SD_CTL_FEIE_MASK, CL_SD_CTL_FEIE(1));
128 sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
129 CL_SD_CTL_DEIE_MASK, CL_SD_CTL_DEIE(1));
130 sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_CL_SD_CTL,
131 CL_SD_CTL_STRM_MASK, CL_SD_CTL_STRM(FW_CL_STREAM_NUMBER));
134 static void skl_cldma_setup_spb(struct sst_dsp *ctx,
135 unsigned int size, bool enable)
138 sst_dsp_shim_update_bits_unlocked(ctx,
139 SKL_ADSP_REG_CL_SPBFIFO_SPBFCCTL,
140 CL_SPBFIFO_SPBFCCTL_SPIBE_MASK,
141 CL_SPBFIFO_SPBFCCTL_SPIBE(1));
143 sst_dsp_shim_write_unlocked(ctx, SKL_ADSP_REG_CL_SPBFIFO_SPIB, size);
146 static void skl_cldma_cleanup_spb(struct sst_dsp *ctx)
148 sst_dsp_shim_update_bits_unlocked(ctx,
149 SKL_ADSP_REG_CL_SPBFIFO_SPBFCCTL,
150 CL_SPBFIFO_SPBFCCTL_SPIBE_MASK,
151 CL_SPBFIFO_SPBFCCTL_SPIBE(0));
153 sst_dsp_shim_write_unlocked(ctx, SKL_ADSP_REG_CL_SPBFIFO_SPIB, 0);
156 static void skl_cldma_cleanup(struct sst_dsp *ctx)
158 skl_cldma_cleanup_spb(ctx);
159 skl_cldma_stream_clear(ctx);
161 ctx->dsp_ops.free_dma_buf(ctx->dev, &ctx->cl_dev.dmab_data);
162 ctx->dsp_ops.free_dma_buf(ctx->dev, &ctx->cl_dev.dmab_bdl);
165 int skl_cldma_wait_interruptible(struct sst_dsp *ctx)
169 if (!wait_event_timeout(ctx->cl_dev.wait_queue,
170 ctx->cl_dev.wait_condition,
171 msecs_to_jiffies(SKL_WAIT_TIMEOUT))) {
172 dev_err(ctx->dev, "%s: Wait timeout\n", __func__);
177 dev_dbg(ctx->dev, "%s: Event wake\n", __func__);
178 if (ctx->cl_dev.wake_status != SKL_CL_DMA_BUF_COMPLETE) {
179 dev_err(ctx->dev, "%s: DMA Error\n", __func__);
184 ctx->cl_dev.wake_status = SKL_CL_DMA_STATUS_NONE;
188 static void skl_cldma_stop(struct sst_dsp *ctx)
190 skl_cldma_stream_run(ctx, false);
193 static void skl_cldma_fill_buffer(struct sst_dsp *ctx, unsigned int size,
194 const void *curr_pos, bool intr_enable, bool trigger)
196 dev_dbg(ctx->dev, "Size: %x, intr_enable: %d\n", size, intr_enable);
197 dev_dbg(ctx->dev, "buf_pos_index:%d, trigger:%d\n",
198 ctx->cl_dev.dma_buffer_offset, trigger);
199 dev_dbg(ctx->dev, "spib position: %d\n", ctx->cl_dev.curr_spib_pos);
202 * Check if the size exceeds buffer boundary. If it exceeds
203 * max_buffer size, then copy till buffer size and then copy
204 * remaining buffer from the start of ring buffer.
206 if (ctx->cl_dev.dma_buffer_offset + size > ctx->cl_dev.bufsize) {
207 unsigned int size_b = ctx->cl_dev.bufsize -
208 ctx->cl_dev.dma_buffer_offset;
209 memcpy(ctx->cl_dev.dmab_data.area + ctx->cl_dev.dma_buffer_offset,
213 ctx->cl_dev.dma_buffer_offset = 0;
216 memcpy(ctx->cl_dev.dmab_data.area + ctx->cl_dev.dma_buffer_offset,
219 if (ctx->cl_dev.curr_spib_pos == ctx->cl_dev.bufsize)
220 ctx->cl_dev.dma_buffer_offset = 0;
222 ctx->cl_dev.dma_buffer_offset = ctx->cl_dev.curr_spib_pos;
224 ctx->cl_dev.wait_condition = false;
227 skl_cldma_int_enable(ctx);
229 ctx->cl_dev.ops.cl_setup_spb(ctx, ctx->cl_dev.curr_spib_pos, trigger);
231 ctx->cl_dev.ops.cl_trigger(ctx, true);
235 * The CL dma doesn't have any way to update the transfer status until a BDL
236 * buffer is fully transferred
238 * So Copying is divided in two parts.
239 * 1. Interrupt on buffer done where the size to be transferred is more than
241 * 2. Polling on fw register to identify if data left to transferred doesn't
242 * fill the ring buffer. Caller takes care of polling the required status
243 * register to identify the transfer status.
244 * 3. if wait flag is set, waits for DBL interrupt to copy the next chunk till
246 * if wait flag is not set, doesn't wait for BDL interrupt. after ccopying
247 * the first chunk return the no of bytes_left to be copied.
250 skl_cldma_copy_to_buf(struct sst_dsp *ctx, const void *bin,
251 u32 total_size, bool wait)
255 unsigned int excess_bytes;
257 unsigned int bytes_left = total_size;
258 const void *curr_pos = bin;
263 dev_dbg(ctx->dev, "%s: Total binary size: %u\n", __func__, bytes_left);
266 if (bytes_left > ctx->cl_dev.bufsize) {
269 * dma transfers only till the write pointer as
272 if (ctx->cl_dev.curr_spib_pos == 0)
273 ctx->cl_dev.curr_spib_pos = ctx->cl_dev.bufsize;
275 size = ctx->cl_dev.bufsize;
276 skl_cldma_fill_buffer(ctx, size, curr_pos, true, start);
280 ret = skl_cldma_wait_interruptible(ctx);
287 skl_cldma_int_disable(ctx);
289 if ((ctx->cl_dev.curr_spib_pos + bytes_left)
290 <= ctx->cl_dev.bufsize) {
291 ctx->cl_dev.curr_spib_pos += bytes_left;
293 excess_bytes = bytes_left -
294 (ctx->cl_dev.bufsize -
295 ctx->cl_dev.curr_spib_pos);
296 ctx->cl_dev.curr_spib_pos = excess_bytes;
300 skl_cldma_fill_buffer(ctx, size,
301 curr_pos, false, start);
304 curr_pos = curr_pos + size;
312 void skl_cldma_process_intr(struct sst_dsp *ctx)
314 u8 cl_dma_intr_status;
317 sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_CL_SD_STS);
319 if (!(cl_dma_intr_status & SKL_CL_DMA_SD_INT_COMPLETE))
320 ctx->cl_dev.wake_status = SKL_CL_DMA_ERR;
322 ctx->cl_dev.wake_status = SKL_CL_DMA_BUF_COMPLETE;
324 ctx->cl_dev.wait_condition = true;
325 wake_up(&ctx->cl_dev.wait_queue);
328 int skl_cldma_prepare(struct sst_dsp *ctx)
333 ctx->cl_dev.bufsize = SKL_MAX_BUFFER_SIZE;
335 /* Allocate cl ops */
336 ctx->cl_dev.ops.cl_setup_bdle = skl_cldma_setup_bdle;
337 ctx->cl_dev.ops.cl_setup_controller = skl_cldma_setup_controller;
338 ctx->cl_dev.ops.cl_setup_spb = skl_cldma_setup_spb;
339 ctx->cl_dev.ops.cl_cleanup_spb = skl_cldma_cleanup_spb;
340 ctx->cl_dev.ops.cl_trigger = skl_cldma_stream_run;
341 ctx->cl_dev.ops.cl_cleanup_controller = skl_cldma_cleanup;
342 ctx->cl_dev.ops.cl_copy_to_dmabuf = skl_cldma_copy_to_buf;
343 ctx->cl_dev.ops.cl_stop_dma = skl_cldma_stop;
346 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, ctx->dev, ctx->cl_dev.bufsize,
347 &ctx->cl_dev.dmab_data);
349 dev_err(ctx->dev, "Alloc buffer for base fw failed: %x\n", ret);
353 /* Setup Code loader BDL */
354 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, ctx->dev, BDL_SIZE, &ctx->cl_dev.dmab_bdl);
356 dev_err(ctx->dev, "Alloc buffer for blde failed: %x\n", ret);
357 ctx->dsp_ops.free_dma_buf(ctx->dev, &ctx->cl_dev.dmab_data);
360 bdl = (__le32 *)ctx->cl_dev.dmab_bdl.area;
363 ctx->cl_dev.ops.cl_setup_bdle(ctx, &ctx->cl_dev.dmab_data,
364 &bdl, ctx->cl_dev.bufsize, 1);
365 ctx->cl_dev.ops.cl_setup_controller(ctx, &ctx->cl_dev.dmab_bdl,
366 ctx->cl_dev.bufsize, ctx->cl_dev.frags);
368 ctx->cl_dev.curr_spib_pos = 0;
369 ctx->cl_dev.dma_buffer_offset = 0;
370 init_waitqueue_head(&ctx->cl_dev.wait_queue);