xref: /linux/sound/soc/sof/compress.c (revision 74395567a3011a07f51cf959be96c1eecb3e6df8)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // Copyright 2021 NXP
4 //
5 // Author: Daniel Baluta <daniel.baluta@nxp.com>
6 
7 #include <sound/soc.h>
8 #include <sound/sof.h>
9 #include <sound/compress_driver.h>
10 #include "sof-audio.h"
11 #include "sof-priv.h"
12 #include "sof-utils.h"
13 #include "ops.h"
14 
15 static void sof_set_transferred_bytes(struct sof_compr_stream *sstream,
16 				      u64 host_pos, u64 buffer_size)
17 {
18 	u64 prev_pos;
19 	unsigned int copied;
20 
21 	div64_u64_rem(sstream->copied_total, buffer_size, &prev_pos);
22 
23 	if (host_pos < prev_pos)
24 		copied = (buffer_size - prev_pos) + host_pos;
25 	else
26 		copied = host_pos - prev_pos;
27 
28 	sstream->copied_total += copied;
29 }
30 
31 static void snd_sof_compr_fragment_elapsed_work(struct work_struct *work)
32 {
33 	struct snd_sof_pcm_stream *sps =
34 		container_of(work, struct snd_sof_pcm_stream,
35 			     period_elapsed_work);
36 
37 	snd_compr_fragment_elapsed(sps->cstream);
38 }
39 
40 void snd_sof_compr_init_elapsed_work(struct work_struct *work)
41 {
42 	INIT_WORK(work, snd_sof_compr_fragment_elapsed_work);
43 }
44 
45 /*
46  * sof compr fragment elapse, this could be called in irq thread context
47  */
48 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream)
49 {
50 	struct snd_soc_pcm_runtime *rtd;
51 	struct snd_compr_runtime *crtd;
52 	struct snd_soc_component *component;
53 	struct sof_compr_stream *sstream;
54 	struct snd_sof_pcm *spcm;
55 
56 	if (!cstream)
57 		return;
58 
59 	rtd = cstream->private_data;
60 	crtd = cstream->runtime;
61 	sstream = crtd->private_data;
62 	component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
63 
64 	spcm = snd_sof_find_spcm_dai(component, rtd);
65 	if (!spcm) {
66 		dev_err(component->dev,
67 			"fragment elapsed called for unknown stream!\n");
68 		return;
69 	}
70 
71 	sof_set_transferred_bytes(sstream, spcm->stream[cstream->direction].posn.host_posn,
72 				  crtd->buffer_size);
73 
74 	/* use the same workqueue-based solution as for PCM, cf. snd_sof_pcm_elapsed */
75 	schedule_work(&spcm->stream[cstream->direction].period_elapsed_work);
76 }
77 
78 static int create_page_table(struct snd_soc_component *component,
79 			     struct snd_compr_stream *cstream,
80 			     unsigned char *dma_area, size_t size)
81 {
82 	struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p;
83 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
84 	int dir = cstream->direction;
85 	struct snd_sof_pcm *spcm;
86 
87 	spcm = snd_sof_find_spcm_dai(component, rtd);
88 	if (!spcm)
89 		return -EINVAL;
90 
91 	return snd_sof_create_page_table(component->dev, dmab,
92 					 spcm->stream[dir].page_table.area, size);
93 }
94 
95 static int sof_compr_open(struct snd_soc_component *component,
96 			  struct snd_compr_stream *cstream)
97 {
98 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
99 	struct snd_compr_runtime *crtd = cstream->runtime;
100 	struct sof_compr_stream *sstream;
101 	struct snd_sof_pcm *spcm;
102 	int dir;
103 
104 	sstream = kzalloc(sizeof(*sstream), GFP_KERNEL);
105 	if (!sstream)
106 		return -ENOMEM;
107 
108 	spcm = snd_sof_find_spcm_dai(component, rtd);
109 	if (!spcm) {
110 		kfree(sstream);
111 		return -EINVAL;
112 	}
113 
114 	dir = cstream->direction;
115 
116 	if (spcm->stream[dir].cstream) {
117 		kfree(sstream);
118 		return -EBUSY;
119 	}
120 
121 	spcm->stream[dir].cstream = cstream;
122 	spcm->stream[dir].posn.host_posn = 0;
123 	spcm->stream[dir].posn.dai_posn = 0;
124 	spcm->prepared[dir] = false;
125 
126 	crtd->private_data = sstream;
127 
128 	return 0;
129 }
130 
131 static int sof_compr_free(struct snd_soc_component *component,
132 			  struct snd_compr_stream *cstream)
133 {
134 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
135 	struct sof_compr_stream *sstream = cstream->runtime->private_data;
136 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
137 	struct sof_ipc_stream stream;
138 	struct sof_ipc_reply reply;
139 	struct snd_sof_pcm *spcm;
140 	int ret = 0;
141 
142 	spcm = snd_sof_find_spcm_dai(component, rtd);
143 	if (!spcm)
144 		return -EINVAL;
145 
146 	stream.hdr.size = sizeof(stream);
147 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
148 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
149 
150 	if (spcm->prepared[cstream->direction]) {
151 		ret = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream),
152 					 &reply, sizeof(reply));
153 		if (!ret)
154 			spcm->prepared[cstream->direction] = false;
155 	}
156 
157 	cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work);
158 	spcm->stream[cstream->direction].cstream = NULL;
159 	kfree(sstream);
160 
161 	return ret;
162 }
163 
164 static int sof_compr_set_params(struct snd_soc_component *component,
165 				struct snd_compr_stream *cstream, struct snd_compr_params *params)
166 {
167 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
168 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
169 	struct snd_compr_runtime *crtd = cstream->runtime;
170 	struct sof_ipc_pcm_params_reply ipc_params_reply;
171 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
172 	struct sof_ipc_fw_version *v = &ready->version;
173 	struct sof_compr_stream *sstream;
174 	struct sof_ipc_pcm_params *pcm;
175 	struct snd_sof_pcm *spcm;
176 	size_t ext_data_size;
177 	int ret;
178 
179 	if (v->abi_version < SOF_ABI_VER(3, 22, 0)) {
180 		dev_err(component->dev,
181 			"Compress params not supported with FW ABI version %d:%d:%d\n",
182 			SOF_ABI_VERSION_MAJOR(v->abi_version),
183 			SOF_ABI_VERSION_MINOR(v->abi_version),
184 			SOF_ABI_VERSION_PATCH(v->abi_version));
185 		return -EINVAL;
186 	}
187 
188 	sstream = crtd->private_data;
189 
190 	spcm = snd_sof_find_spcm_dai(component, rtd);
191 
192 	if (!spcm)
193 		return -EINVAL;
194 
195 	ext_data_size = sizeof(params->codec);
196 
197 	if (sizeof(*pcm) + ext_data_size > sdev->ipc->max_payload_size)
198 		return -EINVAL;
199 
200 	pcm = kzalloc(sizeof(*pcm) + ext_data_size, GFP_KERNEL);
201 	if (!pcm)
202 		return -ENOMEM;
203 
204 	cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
205 	cstream->dma_buffer.dev.dev = sdev->dev;
206 	ret = snd_compr_malloc_pages(cstream, crtd->buffer_size);
207 	if (ret < 0)
208 		goto out;
209 
210 	ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes);
211 	if (ret < 0)
212 		goto out;
213 
214 	pcm->params.buffer.pages = PFN_UP(crtd->dma_bytes);
215 	pcm->hdr.size = sizeof(*pcm) + ext_data_size;
216 	pcm->hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
217 
218 	pcm->comp_id = spcm->stream[cstream->direction].comp_id;
219 	pcm->params.hdr.size = sizeof(pcm->params) + ext_data_size;
220 	pcm->params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr;
221 	pcm->params.buffer.size = crtd->dma_bytes;
222 	pcm->params.direction = cstream->direction;
223 	pcm->params.channels = params->codec.ch_out;
224 	pcm->params.rate = params->codec.sample_rate;
225 	pcm->params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
226 	pcm->params.frame_fmt = SOF_IPC_FRAME_S32_LE;
227 	pcm->params.sample_container_bytes =
228 		snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3;
229 	pcm->params.host_period_bytes = params->buffer.fragment_size;
230 	pcm->params.ext_data_length = ext_data_size;
231 
232 	memcpy((u8 *)pcm->params.ext_data, &params->codec, ext_data_size);
233 
234 	ret = sof_ipc_tx_message(sdev->ipc, pcm, sizeof(*pcm) + ext_data_size,
235 				 &ipc_params_reply, sizeof(ipc_params_reply));
236 	if (ret < 0) {
237 		dev_err(component->dev, "error ipc failed\n");
238 		goto out;
239 	}
240 
241 	ret = snd_sof_set_stream_data_offset(sdev, &spcm->stream[cstream->direction],
242 					     ipc_params_reply.posn_offset);
243 	if (ret < 0) {
244 		dev_err(component->dev, "Invalid stream data offset for Compr %d\n",
245 			spcm->pcm.pcm_id);
246 		goto out;
247 	}
248 
249 	sstream->sampling_rate = params->codec.sample_rate;
250 	sstream->channels = params->codec.ch_out;
251 	sstream->sample_container_bytes = pcm->params.sample_container_bytes;
252 
253 	spcm->prepared[cstream->direction] = true;
254 
255 out:
256 	kfree(pcm);
257 
258 	return ret;
259 }
260 
261 static int sof_compr_get_params(struct snd_soc_component *component,
262 				struct snd_compr_stream *cstream, struct snd_codec *params)
263 {
264 	/* TODO: we don't query the supported codecs for now, if the
265 	 * application asks for an unsupported codec the set_params() will fail.
266 	 */
267 	return 0;
268 }
269 
270 static int sof_compr_trigger(struct snd_soc_component *component,
271 			     struct snd_compr_stream *cstream, int cmd)
272 {
273 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
274 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
275 	struct sof_ipc_stream stream;
276 	struct sof_ipc_reply reply;
277 	struct snd_sof_pcm *spcm;
278 
279 	spcm = snd_sof_find_spcm_dai(component, rtd);
280 	if (!spcm)
281 		return -EINVAL;
282 
283 	stream.hdr.size = sizeof(stream);
284 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
285 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
286 
287 	switch (cmd) {
288 	case SNDRV_PCM_TRIGGER_START:
289 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
290 		break;
291 	case SNDRV_PCM_TRIGGER_STOP:
292 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
293 		break;
294 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
295 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
296 		break;
297 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
298 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
299 		break;
300 	default:
301 		dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd);
302 		break;
303 	}
304 
305 	return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream),
306 				  &reply, sizeof(reply));
307 }
308 
309 static int sof_compr_copy_playback(struct snd_compr_runtime *rtd,
310 				   char __user *buf, size_t count)
311 {
312 	void *ptr;
313 	unsigned int offset, n;
314 	int ret;
315 
316 	div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset);
317 	ptr = rtd->dma_area + offset;
318 	n = rtd->buffer_size - offset;
319 
320 	if (count < n) {
321 		ret = copy_from_user(ptr, buf, count);
322 	} else {
323 		ret = copy_from_user(ptr, buf, n);
324 		ret += copy_from_user(rtd->dma_area, buf + n, count - n);
325 	}
326 
327 	return count - ret;
328 }
329 
330 static int sof_compr_copy_capture(struct snd_compr_runtime *rtd,
331 				  char __user *buf, size_t count)
332 {
333 	void *ptr;
334 	unsigned int offset, n;
335 	int ret;
336 
337 	div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
338 	ptr = rtd->dma_area + offset;
339 	n = rtd->buffer_size - offset;
340 
341 	if (count < n) {
342 		ret = copy_to_user(buf, ptr, count);
343 	} else {
344 		ret = copy_to_user(buf, ptr, n);
345 		ret += copy_to_user(buf + n, rtd->dma_area, count - n);
346 	}
347 
348 	return count - ret;
349 }
350 
351 static int sof_compr_copy(struct snd_soc_component *component,
352 			  struct snd_compr_stream *cstream,
353 			  char __user *buf, size_t count)
354 {
355 	struct snd_compr_runtime *rtd = cstream->runtime;
356 
357 	if (count > rtd->buffer_size)
358 		count = rtd->buffer_size;
359 
360 	if (cstream->direction == SND_COMPRESS_PLAYBACK)
361 		return sof_compr_copy_playback(rtd, buf, count);
362 	else
363 		return sof_compr_copy_capture(rtd, buf, count);
364 }
365 
366 static int sof_compr_pointer(struct snd_soc_component *component,
367 			     struct snd_compr_stream *cstream,
368 			     struct snd_compr_tstamp *tstamp)
369 {
370 	struct snd_sof_pcm *spcm;
371 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
372 	struct sof_compr_stream *sstream = cstream->runtime->private_data;
373 
374 	spcm = snd_sof_find_spcm_dai(component, rtd);
375 	if (!spcm)
376 		return -EINVAL;
377 
378 	tstamp->sampling_rate = sstream->sampling_rate;
379 	tstamp->copied_total = sstream->copied_total;
380 	tstamp->pcm_io_frames = div_u64(spcm->stream[cstream->direction].posn.dai_posn,
381 					sstream->channels * sstream->sample_container_bytes);
382 
383 	return 0;
384 }
385 
386 struct snd_compress_ops sof_compressed_ops = {
387 	.open		= sof_compr_open,
388 	.free		= sof_compr_free,
389 	.set_params	= sof_compr_set_params,
390 	.get_params	= sof_compr_get_params,
391 	.trigger	= sof_compr_trigger,
392 	.pointer	= sof_compr_pointer,
393 	.copy		= sof_compr_copy,
394 };
395 EXPORT_SYMBOL(sof_compressed_ops);
396