diff --git a/include/sound/soc-component.h b/include/sound/soc-component.h index 73d66c97669440..9c799b22e5600e 100644 --- a/include/sound/soc-component.h +++ b/include/sound/soc-component.h @@ -411,6 +411,8 @@ int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream, struct snd_compr_tstamp64 *tstamp); int snd_soc_component_compr_copy(struct snd_compr_stream *cstream, char __user *buf, size_t count); +int snd_soc_component_compr_mmap(struct snd_compr_stream *cstream, + struct vm_area_struct *vma); int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream, struct snd_compr_metadata *metadata); int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream, diff --git a/include/uapi/sound/compress_offload.h b/include/uapi/sound/compress_offload.h index b610683fd8dbe4..157f57f685a0e4 100644 --- a/include/uapi/sound/compress_offload.h +++ b/include/uapi/sound/compress_offload.h @@ -242,6 +242,7 @@ struct snd_compr_task_status { #define SNDRV_COMPRESS_AVAIL _IOR('C', 0x21, struct snd_compr_avail) #define SNDRV_COMPRESS_TSTAMP64 _IOR('C', 0x22, struct snd_compr_tstamp64) #define SNDRV_COMPRESS_AVAIL64 _IOR('C', 0x23, struct snd_compr_avail64) +#define SNDRV_COMPRESS_ACK _IOW('C', 0x24, __u64) #define SNDRV_COMPRESS_PAUSE _IO('C', 0x30) #define SNDRV_COMPRESS_RESUME _IO('C', 0x31) #define SNDRV_COMPRESS_START _IO('C', 0x32) diff --git a/sound/core/compress_offload.c b/sound/core/compress_offload.c index 5e5e0f78f26d50..56eda6524a6cbb 100644 --- a/sound/core/compress_offload.c +++ b/sound/core/compress_offload.c @@ -33,6 +33,7 @@ #include #include #include +#include /* struct snd_compr_codec_caps overflows the ioctl bit size for some * architectures, so we need to disable the relevant ioctls. @@ -115,9 +116,14 @@ static int snd_compr_open(struct inode *inode, struct file *f) } if (dirn != compr->direction) { - pr_err("this device doesn't support this direction\n"); - ret = -EINVAL; - goto __error; + if ((f->f_flags & O_ACCMODE) == O_RDWR && + compr->direction == SND_COMPRESS_PLAYBACK) { + dirn = SND_COMPRESS_PLAYBACK; + } else { + pr_err("this device doesn't support this direction\n"); + ret = -EINVAL; + goto __error; + } } data = kzalloc_obj(*data); @@ -319,6 +325,48 @@ static int snd_compr_ioctl_avail(struct snd_compr_stream *stream, return 0; } +static int snd_compr_ioctl_ack(struct snd_compr_stream *stream, unsigned long arg) +{ + __u64 bytes; + size_t avail; + int ret = 0; + + if (copy_from_user(&bytes, (void __user *)arg, sizeof(bytes))) + return -EFAULT; + + if (bytes == 0) + return 0; + + switch (stream->runtime->state) { + case SNDRV_PCM_STATE_SETUP: + case SNDRV_PCM_STATE_PREPARED: + case SNDRV_PCM_STATE_RUNNING: + break; + default: + return -EBADFD; + } + + avail = snd_compr_get_avail(stream); + if (bytes > avail) + return -EINVAL; + + if (stream->ops->ack) { + ret = stream->ops->ack(stream, bytes); + if (ret < 0) + return ret; + } + + stream->runtime->total_bytes_available += bytes; + + if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) { + stream->runtime->state = SNDRV_PCM_STATE_PREPARED; + pr_debug("stream prepared, Houston we are good to go\n"); + } + + wake_up(&stream->runtime->sleep); + return 0; +} + static int snd_compr_write_data(struct snd_compr_stream *stream, const char __user *buf, size_t count) { @@ -448,6 +496,33 @@ static ssize_t snd_compr_read(struct file *f, char __user *buf, static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma) { + struct snd_compr_file *data = f->private_data; + struct snd_compr_stream *stream; + struct snd_compr_runtime *runtime; + + if (snd_BUG_ON(!data)) + return -EINVAL; + + stream = &data->stream; + runtime = stream->runtime; + + if (!runtime) + return -ENODATA; + + if (stream->direction == SND_COMPRESS_PLAYBACK) { + if (!(vma->vm_flags & (VM_WRITE | VM_READ))) + return -EINVAL; + } else { + if (!(vma->vm_flags & VM_READ)) + return -EINVAL; + } + + if (stream->ops->mmap) + return stream->ops->mmap(stream, vma); + + if (runtime->dma_buffer_p) + return snd_dma_buffer_mmap(runtime->dma_buffer_p, vma); + return -ENXIO; } @@ -1436,6 +1511,8 @@ static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg) return snd_compr_ioctl_avail(stream, arg, true); case SNDRV_COMPRESS_AVAIL64: return snd_compr_ioctl_avail(stream, arg, false); + case SNDRV_COMPRESS_ACK: + return snd_compr_ioctl_ack(stream, arg); case SNDRV_COMPRESS_PAUSE: return snd_compr_pause(stream); case SNDRV_COMPRESS_RESUME: diff --git a/sound/soc/soc-component.c b/sound/soc/soc-component.c index a6e280c798f3dd..7d1b457039874f 100644 --- a/sound/soc/soc-component.c +++ b/sound/soc/soc-component.c @@ -678,6 +678,30 @@ int snd_soc_component_compr_copy(struct snd_compr_stream *cstream, } EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy); +int snd_soc_component_compr_mmap(struct snd_compr_stream *cstream, + struct vm_area_struct *vma) +{ + struct snd_soc_pcm_runtime *rtd = cstream->private_data; + struct snd_soc_component *component; + int i, ret = -ENXIO; + + snd_soc_dpcm_mutex_lock(rtd); + + for_each_rtd_components(rtd, i, component) { + if (component->driver->compress_ops && + component->driver->compress_ops->mmap) { + ret = component->driver->compress_ops->mmap( + component, cstream, vma); + break; + } + } + + snd_soc_dpcm_mutex_unlock(rtd); + + return soc_component_ret(component, ret); +} +EXPORT_SYMBOL_GPL(snd_soc_component_compr_mmap); + int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream, struct snd_compr_metadata *metadata) { diff --git a/sound/soc/soc-compress.c b/sound/soc/soc-compress.c index a941ed0908c667..4e42fcc5025d26 100644 --- a/sound/soc/soc-compress.c +++ b/sound/soc/soc-compress.c @@ -682,6 +682,12 @@ static int soc_compr_get_metadata(struct snd_compr_stream *cstream, return snd_soc_component_compr_get_metadata(cstream, metadata); } +static int soc_compr_mmap(struct snd_compr_stream *cstream, + struct vm_area_struct *vma) +{ + return snd_soc_component_compr_mmap(cstream, vma); +} + /* ASoC Compress operations */ static struct snd_compr_ops soc_compr_ops = { .open = soc_compr_open, @@ -693,6 +699,7 @@ static struct snd_compr_ops soc_compr_ops = { .trigger = soc_compr_trigger, .pointer = soc_compr_pointer, .ack = soc_compr_ack, + .mmap = soc_compr_mmap, .get_caps = snd_soc_component_compr_get_caps, .get_codec_caps = snd_soc_component_compr_get_codec_caps, }; @@ -708,6 +715,7 @@ static struct snd_compr_ops soc_compr_dyn_ops = { .trigger = soc_compr_trigger_fe, .pointer = soc_compr_pointer, .ack = soc_compr_ack, + .mmap = soc_compr_mmap, .get_caps = snd_soc_component_compr_get_caps, .get_codec_caps = snd_soc_component_compr_get_codec_caps, }; diff --git a/sound/soc/sof/ipc4-compress.c b/sound/soc/sof/ipc4-compress.c index 97a4ec7519055e..8054a987794f9a 100644 --- a/sound/soc/sof/ipc4-compress.c +++ b/sound/soc/sof/ipc4-compress.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "sof-audio.h" #include "sof-priv.h" #include "sof-utils.h" @@ -14,8 +15,8 @@ #include "ipc4-topology.h" #include "ipc4-fw-reg.h" -/* Maximum processing size of the decoder/encoder is 2048 bytes */ -#define SOF_IPC4_COMPR_MAX_PROCESSING_SIZE (SZ_2K) +/* Minimum fragment size aligned to Intel HDA/ACE DMA burst hardware (128 bytes) */ +#define SOF_IPC4_COMPR_MIN_FRAGMENT_SIZE (128) #define SOF_IPC4_COMPR_MIN_FRAGMENTS 3 #define SOF_IPC4_COMPR_MAX_FRAGMENT_SIZE (SZ_128K) @@ -58,10 +59,13 @@ static u32 sof_ipc4_compr_calc_min_fragment_size(struct snd_sof_pcm_stream *sps) host_buffer_estimate = snd_pcm_format_size(SNDRV_PCM_FORMAT_S32_LE, 2 * 48); host_buffer_estimate *= sps->dsp_max_burst_size_in_ms; /* - * The minimum fragment size must not be smaller than the processing size - * or in case of deep buffer on host side, the host DMA buffer size. + * The minimum fragment size must align with HDA DMA burst boundary (128 bytes) + * or cover the deep buffer host DMA buffer size if configured. */ - return max(SOF_IPC4_COMPR_MAX_PROCESSING_SIZE, host_buffer_estimate); + if (host_buffer_estimate) + return max_t(u32, SOF_IPC4_COMPR_MIN_FRAGMENT_SIZE, host_buffer_estimate); + + return SOF_IPC4_COMPR_MIN_FRAGMENT_SIZE; } /* The buffer must be described by the page table allocated for the stream */ @@ -115,16 +119,18 @@ static int sof_ipc4_compr_stream_free(struct snd_sof_dev *sdev, int err = 0; if (spcm->prepared[dir]) { - if (spcm->pending_stop[dir]) + if (spcm->pending_stop[dir] && pcm_ops && pcm_ops->trigger) pcm_ops->trigger(sdev->component, NULL, spcm, SNDRV_PCM_TRIGGER_STOP, dir); snd_sof_compr_platform_trigger(sdev, cstream, SNDRV_PCM_TRIGGER_STOP); - err = pcm_ops->hw_free(sdev->component, NULL, spcm, dir); - if (err < 0) - spcm_err(spcm, dir, "pcm_ops->hw_free failed %d\n", err); + if (pcm_ops && pcm_ops->hw_free) { + err = pcm_ops->hw_free(sdev->component, NULL, spcm, dir); + if (err < 0) + spcm_err(spcm, dir, "pcm_ops->hw_free failed %d\n", err); + } } spcm->prepared[dir] = false; @@ -469,23 +475,24 @@ static int sof_ipc4_compr_set_params(struct snd_soc_component *component, } process = sof_ipc4_compr_get_module(spcm, dir); - if (!process) { - ret = -EINVAL; - goto free_list; - } + if (process) { + compr_data = kzalloc(sizeof(*compr_data), GFP_KERNEL); + if (!compr_data) { + ret = -ENOMEM; + goto free_list; + } + + memcpy(&compr_data->codec, &compr_params->codec, sizeof(compr_data->codec)); + compr_data->dir = dir; - compr_data = kzalloc(sizeof(*compr_data), GFP_KERNEL); - if (!compr_data) { - ret = -ENOMEM; + process->init_ext_module_data = compr_data; + process->init_ext_module_size = sizeof(*compr_data); + } else if (params->codec.id != SND_AUDIOCODEC_PCM && + params->codec.id != SND_AUDIOCODEC_BESPOKE) { + ret = -EINVAL; goto free_list; } - memcpy(&compr_data->codec, &compr_params->codec, sizeof(compr_data->codec)); - compr_data->dir = dir; - - process->init_ext_module_data = compr_data; - process->init_ext_module_size = sizeof(*compr_data); - /* * Make sure that the DSP is booted up, which might not be the * case if the on-demand DSP boot is used @@ -493,7 +500,6 @@ static int sof_ipc4_compr_set_params(struct snd_soc_component *component, ret = snd_sof_boot_dsp_firmware(sdev); if (ret) goto clear_init_ext; - /* set the host DMA ID */ if (tplg_ops && tplg_ops->host_config) tplg_ops->host_config(sdev, host_swidget, platform_params); @@ -517,14 +523,18 @@ static int sof_ipc4_compr_set_params(struct snd_soc_component *component, sof_ipc4_build_time_info(sdev, &spcm->stream[dir]); } - process->init_ext_module_data = NULL; - process->init_ext_module_size = 0; + if (process) { + process->init_ext_module_data = NULL; + process->init_ext_module_size = 0; + } return 0; clear_init_ext: - process->init_ext_module_data = NULL; - process->init_ext_module_size = 0; + if (process) { + process->init_ext_module_data = NULL; + process->init_ext_module_size = 0; + } free_list: sof_widget_list_unprepare(sdev, spcm, dir); @@ -614,10 +624,12 @@ static int sof_ipc4_compr_trigger(struct snd_soc_component *component, spcm_dbg(spcm, dir, "Entry: trigger (cmd: %d)\n", cmd); - ret = pcm_ops->trigger(component, NULL, spcm, cmd, dir); - if (ret < 0) { - spcm_err(spcm, dir, "pcm_ops->trigger failed for cmd %d\n", cmd); - return ret; + if (pcm_ops && pcm_ops->trigger) { + ret = pcm_ops->trigger(component, NULL, spcm, cmd, dir); + if (ret < 0) { + spcm_err(spcm, dir, "pcm_ops->trigger failed for cmd %d\n", cmd); + return ret; + } } if (!ret && trigger_platform) { @@ -811,6 +823,28 @@ void sof_ipc4_compr_drain_done(struct snd_sof_dev *sdev, void *ipc_message) snd_compr_drain_notify(spcm->stream[dir].cstream); } +static int sof_ipc4_compr_mmap(struct snd_soc_component *component, + struct snd_compr_stream *stream, + struct vm_area_struct *vma) +{ + struct snd_compr_runtime *runtime = stream->runtime; + + if (!runtime || !runtime->dma_area) + return -ENXIO; + + if (runtime->dma_buffer_p) + return snd_dma_buffer_mmap(runtime->dma_buffer_p, vma); + + return snd_dma_buffer_mmap(&stream->dma_buffer, vma); +} + +static int sof_ipc4_compr_ack(struct snd_soc_component *component, + struct snd_compr_stream *cstream, + size_t bytes) +{ + return 0; +} + const struct snd_compress_ops sof_ipc4_compressed_ops = { .open = sof_ipc4_compr_open, .free = sof_ipc4_compr_free, @@ -819,5 +853,7 @@ const struct snd_compress_ops sof_ipc4_compressed_ops = { .get_params = sof_ipc4_compr_get_params, .trigger = sof_ipc4_compr_trigger, .pointer = sof_ipc4_compr_pointer, + .mmap = sof_ipc4_compr_mmap, + .ack = sof_ipc4_compr_ack, .copy = sof_ipc4_compr_copy, };