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