From nobody Fri Nov 29 03:39:53 2024 Received: from inva020.nxp.com (inva020.nxp.com [92.121.34.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D42D126F0A; Wed, 25 Sep 2024 07:17:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=92.121.34.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248674; cv=none; b=Nyz1FiAzvQVNTloPQ4Dk5LWzHYhl6OKbJAl2U798TGu/9F9SPOC+l1a8Yr+0S7poT+8aePg36lq3yUClLvHQLpLaBq3sRkEpRgYnTTq/JRzvo27O/T9YNuv8rPKdewgqUic0gN+fd29CvvLW2H1D76AfdEHROB/GIV3hcxyOBYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248674; c=relaxed/simple; bh=YFMewxyEY98yX8iB2yooig+GF5bW/FO5XCjkbP1EQp4=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=J4/BJviz4PwSTHL0GMfU4PSpXpzoMIcAp74xKGwlWMoAFHGPWRftWKOqd1zsOUYSmzH/TXj/aPGBuRc8J9ZM42rnxDxV8+W6L8yOn2SizOpCtVmjCmX6uOO/XXnceG6XX1M9NJXBb8lDduKutyyNZ+3Dfz8jxlK/8nNE/wKTjjg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; arc=none smtp.client-ip=92.121.34.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 69C7D1A0308; Wed, 25 Sep 2024 09:17:50 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 0792B1A062B; Wed, 25 Sep 2024 09:17:50 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id 5A782183AD46; Wed, 25 Sep 2024 15:17:48 +0800 (+08) From: Shengjiu Wang To: vkoul@kernel.org, perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, shengjiu.wang@gmail.com, Xiubo.Lee@gmail.com, festevam@gmail.com, nicoleotsuka@gmail.com, lgirdwood@gmail.com, broonie@kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 1/7] ALSA: compress_offload: introduce accel operation mode Date: Wed, 25 Sep 2024 14:55:10 +0800 Message-Id: <1727247316-16156-2-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> References: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: ClamAV using ClamSMTP From: Jaroslav Kysela There is a requirement to expose the audio hardware that accelerates various tasks for user space such as sample rate converters, compressed stream decoders, etc. This is description for the API extension for the compress ALSA API which is able to handle "tasks" that are not bound to real-time operations and allows for the serialization of operations. For details, refer to "compress-accel.rst" document. Cc: Mark Brown Cc: Shengjiu Wang Cc: Nicolas Dufresne Cc: Amadeusz S=C5=82awi=C5=84ski Cc: Pierre-Louis Bossart Cc: Vinod Koul Signed-off-by: Jaroslav Kysela Signed-off-by: Shengjiu Wang --- .../sound/designs/compress-accel.rst | 136 +++++++ include/sound/compress_driver.h | 46 +++ include/uapi/sound/compress_offload.h | 63 +++- sound/core/Kconfig | 3 + sound/core/compress_offload.c | 351 +++++++++++++++++- 5 files changed, 591 insertions(+), 8 deletions(-) create mode 100644 Documentation/sound/designs/compress-accel.rst diff --git a/Documentation/sound/designs/compress-accel.rst b/Documentation= /sound/designs/compress-accel.rst new file mode 100644 index 000000000000..cfe4fa645ab2 --- /dev/null +++ b/Documentation/sound/designs/compress-accel.rst @@ -0,0 +1,136 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +ALSA Co-processor Acceleration API +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Jaroslav Kysela + + +Overview +=3D=3D=3D=3D=3D=3D=3D=3D + +There is a requirement to expose the audio hardware that accelerates vario= us +tasks for user space such as sample rate converters, compressed +stream decoders, etc. + +This is description for the API extension for the compress ALSA API which +is able to handle "tasks" that are not bound to real-time operations +and allows for the serialization of operations. + +Requirements +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The main requirements are: + +- serialization of multiple tasks for user space to allow multiple + operations without user space intervention + +- separate buffers (input + output) for each operation + +- expose buffers using mmap to user space + +- signal user space when the task is finished (standard poll mechanism) + +Design +=3D=3D=3D=3D=3D=3D + +A new direction SND_COMPRESS_ACCEL is introduced to identify +the passthrough API. + +The API extension shares device enumeration and parameters handling from +the main compressed API. All other realtime streaming ioctls are deactivat= ed +and a new set of task related ioctls are introduced. The standard +read/write/mmap I/O operations are not supported in the passthrough device. + +Device ("stream") state handling is reduced to OPEN/SETUP. All other +states are not available for the passthrough mode. + +Data I/O mechanism is using standard dma-buf interface with all advantages +like mmap, standard I/O, buffer sharing etc. One buffer is used for the +input data and second (separate) buffer is used for the output data. Each = task +have separate I/O buffers. + +For the buffering parameters, the fragments means a limit of allocated tas= ks +for given device. The fragment_size limits the input buffer size for the g= iven +device. The output buffer size is determined by the driver (may be differe= nt +from the input buffer size). + +State Machine +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The passthrough audio stream state machine is described below : + + +----------+ + | | + | OPEN | + | | + +----------+ + | + | + | compr_set_params() + | + v + all passthrough task ops +----------+ + +------------------------------------| | + | | SETUP | + | | + | +----------+ + | | + +------------------------------------------+ + + +Passthrough operations (ioctls) +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D + +All operations are protected using stream->device->lock (mutex). + +CREATE +------ +Creates a set of input/output buffers. The input buffer size is +fragment_size. Allocates unique seqno. + +The hardware drivers allocate internal 'struct dma_buf' for both input and +output buffers (using 'dma_buf_export()' function). The anonymous +file descriptors for those buffers are passed to user space. + +FREE +---- +Free a set of input/output buffers. If a task is active, the stop +operation is executed before. If seqno is zero, operation is executed for = all +tasks. + +START +----- +Starts (queues) a task. There are two cases of the task start - right after +the task is created. In this case, origin_seqno must be zero. +The second case is for reusing of already finished task. The origin_seqno +must identify the task to be reused. In both cases, a new seqno value +is allocated and returned to user space. + +The prerequisite is that application filled input dma buffer with +new source data and set input_size to pass the real data size to the drive= r. + +The order of data processing is preserved (first started job must be +finished at first). + +If the multiple tasks require a state handling (e.g. resampling operation), +the user space may set SND_COMPRESS_TFLG_NEW_STREAM flag to mark the +start of the new stream data. It is useful to keep the allocated buffers +for the new operation rather using open/close mechanism. + +STOP +---- +Stop (dequeues) a task. If seqno is zero, operation is executed for all +tasks. + +STATUS +------ +Obtain the task status (active, finished). Also, the driver will set +the real output data size (valid area in the output buffer). + +Credits +=3D=3D=3D=3D=3D=3D=3D +- Shengjiu Wang +- Takashi Iwai +- Vinod Koul diff --git a/include/sound/compress_driver.h b/include/sound/compress_drive= r.h index bcf872c17dd3..66fd54dd36e2 100644 --- a/include/sound/compress_driver.h +++ b/include/sound/compress_driver.h @@ -19,6 +19,30 @@ =20 struct snd_compr_ops; =20 +/** + * struct snd_compr_task_runtime: task runtime description + * @list: list of all managed tasks + * @input: input DMA buffer + * @output: output DMA buffer + * @seqno: sequence number + * @input_size: really used data in the input buffer + * @output_size: really used data in the output buffer + * @flags: see SND_COMPRESS_TFLG_* + * @state: actual task state + * @private_value: used by the lowlevel driver (opaque) + */ +struct snd_compr_task_runtime { + struct list_head list; + struct dma_buf *input; + struct dma_buf *output; + u64 seqno; + u64 input_size; + u64 output_size; + u32 flags; + u8 state; + void *private_value; +}; + /** * struct snd_compr_runtime: runtime stream description * @state: stream state @@ -37,6 +61,10 @@ struct snd_compr_ops; * @dma_addr: physical buffer address (not accessible from main CPU) * @dma_bytes: size of DMA area * @dma_buffer_p: runtime dma buffer pointer + * @active_tasks: count of active tasks + * @total_tasks: count of all tasks + * @task_seqno: last task sequence number (!=3D 0) + * @tasks: list of all tasks */ struct snd_compr_runtime { snd_pcm_state_t state; @@ -54,6 +82,13 @@ struct snd_compr_runtime { dma_addr_t dma_addr; size_t dma_bytes; struct snd_dma_buffer *dma_buffer_p; + +#if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL) + u32 active_tasks; + u32 total_tasks; + u64 task_seqno; + struct list_head tasks; +#endif }; =20 /** @@ -132,6 +167,12 @@ struct snd_compr_ops { struct snd_compr_caps *caps); int (*get_codec_caps) (struct snd_compr_stream *stream, struct snd_compr_codec_caps *codec); +#if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL) + int (*task_create)(struct snd_compr_stream *stream, struct snd_compr_task= _runtime *task); + int (*task_start)(struct snd_compr_stream *stream, struct snd_compr_task_= runtime *task); + int (*task_stop)(struct snd_compr_stream *stream, struct snd_compr_task_r= untime *task); + int (*task_free)(struct snd_compr_stream *stream, struct snd_compr_task_r= untime *task); +#endif }; =20 /** @@ -242,4 +283,9 @@ int snd_compr_free_pages(struct snd_compr_stream *strea= m); int snd_compr_stop_error(struct snd_compr_stream *stream, snd_pcm_state_t state); =20 +#if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL) +void snd_compr_task_finished(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task); +#endif + #endif diff --git a/include/uapi/sound/compress_offload.h b/include/uapi/sound/com= press_offload.h index d185957f3fe0..0a41173c77f2 100644 --- a/include/uapi/sound/compress_offload.h +++ b/include/uapi/sound/compress_offload.h @@ -14,7 +14,7 @@ #include =20 =20 -#define SNDRV_COMPRESS_VERSION SNDRV_PROTOCOL_VERSION(0, 2, 0) +#define SNDRV_COMPRESS_VERSION SNDRV_PROTOCOL_VERSION(0, 3, 0) /** * struct snd_compressed_buffer - compressed buffer * @fragment_size: size of buffer fragment in bytes @@ -68,7 +68,8 @@ struct snd_compr_avail { =20 enum snd_compr_direction { SND_COMPRESS_PLAYBACK =3D 0, - SND_COMPRESS_CAPTURE + SND_COMPRESS_CAPTURE, + SND_COMPRESS_ACCEL }; =20 /** @@ -127,6 +128,57 @@ struct snd_compr_metadata { __u32 value[8]; } __attribute__((packed, aligned(4))); =20 +/* flags for struct snd_compr_task */ +#define SND_COMPRESS_TFLG_NEW_STREAM (1 << 0) /* mark for the new stream = data */ + +/** + * struct snd_compr_task - task primitive for non-realtime operation + * @seqno: sequence number (task identifier) + * @origin_seqno: previous sequence number (task identifier) - for reuse + * @input_fd: data input file descriptor (dma-buf) + * @output_fd: data output file descriptor (dma-buf) + * @input_size: filled data in bytes (from caller, must not exceed fragmen= t size) + * @flags: see SND_COMPRESS_TFLG_* defines + */ +struct snd_compr_task { + __u64 seqno; + __u64 origin_seqno; + int input_fd; + int output_fd; + __u64 input_size; + __u32 flags; + __u8 reserved[16]; +} __attribute__((packed, aligned(4))); + +/** + * enum snd_compr_state - task state + * @SND_COMPRESS_TASK_STATE_IDLE: task is not queued + * @SND_COMPRESS_TASK_STATE_ACTIVE: task is in the queue + * @SND_COMPRESS_TASK_STATE_FINISHED: task was processed, output is availa= ble + */ +enum snd_compr_state { + SND_COMPRESS_TASK_STATE_IDLE =3D 0, + SND_COMPRESS_TASK_STATE_ACTIVE, + SND_COMPRESS_TASK_STATE_FINISHED +}; + +/** + * struct snd_compr_task_status - task status + * @seqno: sequence number (task identifier) + * @input_size: filled data in bytes (from user space) + * @output_size: filled data in bytes (from driver) + * @output_flags: reserved for future (all zeros - from driver) + * @state: actual task state (SND_COMPRESS_TASK_STATE_*) + */ +struct snd_compr_task_status { + __u64 seqno; + __u64 input_size; + __u64 output_size; + __u32 output_flags; + __u8 state; + __u8 reserved[15]; +} __attribute__((packed, aligned(4))); + /* * compress path ioctl definitions * SNDRV_COMPRESS_GET_CAPS: Query capability of DSP @@ -164,6 +216,13 @@ struct snd_compr_metadata { #define SNDRV_COMPRESS_DRAIN _IO('C', 0x34) #define SNDRV_COMPRESS_NEXT_TRACK _IO('C', 0x35) #define SNDRV_COMPRESS_PARTIAL_DRAIN _IO('C', 0x36) + +#define SNDRV_COMPRESS_TASK_CREATE _IOWR('C', 0x60, struct snd_compr_task) +#define SNDRV_COMPRESS_TASK_FREE _IOW('C', 0x61, __u64) +#define SNDRV_COMPRESS_TASK_START _IOWR('C', 0x62, struct snd_compr_task) +#define SNDRV_COMPRESS_TASK_STOP _IOW('C', 0x63, __u64) +#define SNDRV_COMPRESS_TASK_STATUS _IOWR('C', 0x68, struct snd_compr_task_= status) + /* * TODO * 1. add mmap support diff --git a/sound/core/Kconfig b/sound/core/Kconfig index 2c5b9f964703..48db44fa56fe 100644 --- a/sound/core/Kconfig +++ b/sound/core/Kconfig @@ -59,6 +59,9 @@ config SND_CORE_TEST config SND_COMPRESS_OFFLOAD tristate =20 +config SND_COMPRESS_ACCEL + bool + config SND_JACK bool =20 diff --git a/sound/core/compress_offload.c b/sound/core/compress_offload.c index b8c0d6edbdd1..2dbb12a5c91b 100644 --- a/sound/core/compress_offload.c +++ b/sound/core/compress_offload.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +55,12 @@ struct snd_compr_file { =20 static void error_delayed_work(struct work_struct *work); =20 +#if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL) +static void snd_compr_task_free_all(struct snd_compr_stream *stream); +#else +static inline void snd_compr_task_free_all(struct snd_compr_stream *stream= ) { } +#endif + /* * a note on stream states used: * we use following states in the compressed core @@ -85,6 +92,8 @@ static int snd_compr_open(struct inode *inode, struct fil= e *f) dirn =3D SND_COMPRESS_PLAYBACK; else if ((f->f_flags & O_ACCMODE) =3D=3D O_RDONLY) dirn =3D SND_COMPRESS_CAPTURE; + else if ((f->f_flags & O_ACCMODE) =3D=3D O_RDWR) + dirn =3D SND_COMPRESS_ACCEL; else return -EINVAL; =20 @@ -125,6 +134,9 @@ static int snd_compr_open(struct inode *inode, struct f= ile *f) } runtime->state =3D SNDRV_PCM_STATE_OPEN; init_waitqueue_head(&runtime->sleep); +#if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL) + INIT_LIST_HEAD(&runtime->tasks); +#endif data->stream.runtime =3D runtime; f->private_data =3D (void *)data; scoped_guard(mutex, &compr->lock) @@ -154,6 +166,8 @@ static int snd_compr_free(struct inode *inode, struct f= ile *f) break; } =20 + snd_compr_task_free_all(&data->stream); + data->stream.ops->free(&data->stream); if (!data->stream.runtime->dma_buffer_p) kfree(data->stream.runtime->buffer); @@ -226,6 +240,9 @@ snd_compr_ioctl_avail(struct snd_compr_stream *stream, = unsigned long arg) struct snd_compr_avail ioctl_avail; size_t avail; =20 + if (stream->direction =3D=3D SND_COMPRESS_ACCEL) + return -EBADFD; + avail =3D snd_compr_calc_avail(stream, &ioctl_avail); ioctl_avail.avail =3D avail; =20 @@ -287,6 +304,8 @@ static ssize_t snd_compr_write(struct file *f, const ch= ar __user *buf, return -EFAULT; =20 stream =3D &data->stream; + if (stream->direction =3D=3D SND_COMPRESS_ACCEL) + return -EBADFD; guard(mutex)(&stream->device->lock); /* write is allowed when stream is running or has been steup */ switch (stream->runtime->state) { @@ -336,6 +355,8 @@ static ssize_t snd_compr_read(struct file *f, char __us= er *buf, return -EFAULT; =20 stream =3D &data->stream; + if (stream->direction =3D=3D SND_COMPRESS_ACCEL) + return -EBADFD; guard(mutex)(&stream->device->lock); =20 /* read is allowed when stream is running, paused, draining and setup @@ -385,6 +406,7 @@ static __poll_t snd_compr_poll(struct file *f, poll_tab= le *wait) { struct snd_compr_file *data =3D f->private_data; struct snd_compr_stream *stream; + struct snd_compr_runtime *runtime; size_t avail; __poll_t retval =3D 0; =20 @@ -392,6 +414,7 @@ static __poll_t snd_compr_poll(struct file *f, poll_tab= le *wait) return EPOLLERR; =20 stream =3D &data->stream; + runtime =3D stream->runtime; =20 guard(mutex)(&stream->device->lock); =20 @@ -405,6 +428,21 @@ static __poll_t snd_compr_poll(struct file *f, poll_ta= ble *wait) =20 poll_wait(f, &stream->runtime->sleep, wait); =20 +#if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL) + if (stream->direction =3D=3D SND_COMPRESS_ACCEL) { + struct snd_compr_task_runtime *task; + + if (runtime->fragments > runtime->active_tasks) + retval |=3D EPOLLOUT | EPOLLWRNORM; + task =3D list_first_entry_or_null(&runtime->tasks, + struct snd_compr_task_runtime, + list); + if (task && task->state =3D=3D SND_COMPRESS_TASK_STATE_FINISHED) + retval |=3D EPOLLIN | EPOLLRDNORM; + return retval; + } +#endif + avail =3D snd_compr_get_avail(stream); pr_debug("avail is %ld\n", (unsigned long)avail); /* check if we have at least one fragment to fill */ @@ -521,6 +559,9 @@ static int snd_compr_allocate_buffer(struct snd_compr_s= tream *stream, unsigned int buffer_size; void *buffer =3D NULL; =20 + if (stream->direction =3D=3D SND_COMPRESS_ACCEL) + goto params; + buffer_size =3D params->buffer.fragment_size * params->buffer.fragments; if (stream->ops->copy) { buffer =3D NULL; @@ -543,18 +584,30 @@ static int snd_compr_allocate_buffer(struct snd_compr= _stream *stream, if (!buffer) return -ENOMEM; } - stream->runtime->fragment_size =3D params->buffer.fragment_size; - stream->runtime->fragments =3D params->buffer.fragments; + stream->runtime->buffer =3D buffer; stream->runtime->buffer_size =3D buffer_size; +params: + stream->runtime->fragment_size =3D params->buffer.fragment_size; + stream->runtime->fragments =3D params->buffer.fragments; return 0; } =20 -static int snd_compress_check_input(struct snd_compr_params *params) +static int +snd_compress_check_input(struct snd_compr_stream *stream, struct snd_compr= _params *params) { + u32 max_fragments; + /* first let's check the buffer parameter's */ - if (params->buffer.fragment_size =3D=3D 0 || - params->buffer.fragments > U32_MAX / params->buffer.fragment_size || + if (params->buffer.fragment_size =3D=3D 0) + return -EINVAL; + + if (stream->direction =3D=3D SND_COMPRESS_ACCEL) + max_fragments =3D 64; /* safe value */ + else + max_fragments =3D U32_MAX / params->buffer.fragment_size; + + if (params->buffer.fragments > max_fragments || params->buffer.fragments =3D=3D 0) return -EINVAL; =20 @@ -583,7 +636,7 @@ snd_compr_set_params(struct snd_compr_stream *stream, u= nsigned long arg) if (IS_ERR(params)) return PTR_ERR(params); =20 - retval =3D snd_compress_check_input(params); + retval =3D snd_compress_check_input(stream, params); if (retval) return retval; =20 @@ -939,6 +992,266 @@ static int snd_compr_partial_drain(struct snd_compr_s= tream *stream) return snd_compress_wait_for_drain(stream); } =20 +#if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL) + +static struct snd_compr_task_runtime * +snd_compr_find_task(struct snd_compr_stream *stream, __u64 seqno) +{ + struct snd_compr_task_runtime *task; + + list_for_each_entry(task, &stream->runtime->tasks, list) { + if (task->seqno =3D=3D seqno) + return task; + } + return NULL; +} + +static void snd_compr_task_free(struct snd_compr_task_runtime *task) +{ + if (task->output) + dma_buf_put(task->output); + if (task->input) + dma_buf_put(task->input); + kfree(task); +} + +static u64 snd_compr_seqno_next(struct snd_compr_stream *stream) +{ + u64 seqno =3D ++stream->runtime->task_seqno; + + if (seqno =3D=3D 0) + seqno =3D ++stream->runtime->task_seqno; + return seqno; +} + +static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_= compr_task *utask) +{ + struct snd_compr_task_runtime *task; + int retval; + + if (stream->runtime->total_tasks >=3D stream->runtime->fragments) + return -EBUSY; + if (utask->origin_seqno !=3D 0 || utask->input_size !=3D 0) + return -EINVAL; + task =3D kzalloc(sizeof(*task), GFP_KERNEL); + if (!task) + return -ENOMEM; + utask->seqno =3D snd_compr_seqno_next(stream); + task->seqno =3D utask->seqno; + task->input_size =3D utask->input_size; + retval =3D stream->ops->task_create(stream, task); + if (retval < 0) + goto cleanup; + utask->input_fd =3D dma_buf_fd(task->input, O_WRONLY | O_CLOEXEC); + if (utask->input_fd < 0) { + retval =3D utask->input_fd; + goto cleanup; + } + utask->output_fd =3D dma_buf_fd(task->output, O_RDONLY | O_CLOEXEC); + if (utask->output_fd < 0) { + retval =3D utask->output_fd; + goto cleanup; + } + /* keep dmabuf reference until freed with task free ioctl */ + dma_buf_get(utask->input_fd); + dma_buf_get(utask->output_fd); + list_add_tail(&task->list, &stream->runtime->tasks); + stream->runtime->total_tasks++; + return 0; +cleanup: + snd_compr_task_free(task); + return retval; +} + +static int snd_compr_task_create(struct snd_compr_stream *stream, unsigned= long arg) +{ + struct snd_compr_task *task __free(kfree) =3D NULL; + int retval; + + if (stream->runtime->state !=3D SNDRV_PCM_STATE_SETUP) + return -EPERM; + task =3D memdup_user((void __user *)arg, sizeof(*task)); + if (IS_ERR(task)) + return PTR_ERR(no_free_ptr(task)); + retval =3D snd_compr_task_new(stream, task); + if (retval >=3D 0) + if (copy_to_user((void __user *)arg, task, sizeof(*task))) + retval =3D -EFAULT; + return retval; +} + +static int snd_compr_task_start_prepare(struct snd_compr_task_runtime *tas= k, + struct snd_compr_task *utask) +{ + if (!task) + return -EINVAL; + if (task->state >=3D SND_COMPRESS_TASK_STATE_FINISHED) + return -EBUSY; + if (utask->input_size > task->input->size) + return -EINVAL; + task->flags =3D utask->flags; + task->input_size =3D utask->input_size; + task->state =3D SND_COMPRESS_TASK_STATE_IDLE; + return 0; +} + +static int snd_compr_task_start(struct snd_compr_stream *stream, struct sn= d_compr_task *utask) +{ + struct snd_compr_task_runtime *task; + int retval; + + if (utask->origin_seqno > 0) { + task =3D snd_compr_find_task(stream, utask->origin_seqno); + retval =3D snd_compr_task_start_prepare(task, utask); + if (retval < 0) + return retval; + utask->seqno =3D snd_compr_seqno_next(stream); + task->seqno =3D utask->seqno; + utask->origin_seqno =3D 0; + list_move_tail(&task->list, &stream->runtime->tasks); + } else { + task =3D snd_compr_find_task(stream, utask->seqno); + if (task && task->state !=3D SND_COMPRESS_TASK_STATE_IDLE) + return -EBUSY; + retval =3D snd_compr_task_start_prepare(task, utask); + if (retval < 0) + return retval; + } + retval =3D stream->ops->task_start(stream, task); + if (retval >=3D 0) { + task->state =3D SND_COMPRESS_TASK_STATE_ACTIVE; + stream->runtime->active_tasks++; + } + return retval; +} + +static int snd_compr_task_start_ioctl(struct snd_compr_stream *stream, uns= igned long arg) +{ + struct snd_compr_task *task __free(kfree) =3D NULL; + int retval; + + if (stream->runtime->state !=3D SNDRV_PCM_STATE_SETUP) + return -EPERM; + task =3D memdup_user((void __user *)arg, sizeof(*task)); + if (IS_ERR(task)) + return PTR_ERR(no_free_ptr(task)); + retval =3D snd_compr_task_start(stream, task); + if (retval >=3D 0) + if (copy_to_user((void __user *)arg, task, sizeof(*task))) + retval =3D -EFAULT; + return retval; +} + +static void snd_compr_task_stop_one(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + if (task->state !=3D SND_COMPRESS_TASK_STATE_ACTIVE) + return; + stream->ops->task_stop(stream, task); + if (!snd_BUG_ON(stream->runtime->active_tasks =3D=3D 0)) + stream->runtime->active_tasks--; + list_move_tail(&task->list, &stream->runtime->tasks); + task->state =3D SND_COMPRESS_TASK_STATE_IDLE; +} + +static void snd_compr_task_free_one(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + snd_compr_task_stop_one(stream, task); + stream->ops->task_free(stream, task); + list_del(&task->list); + snd_compr_task_free(task); + stream->runtime->total_tasks--; +} + +static void snd_compr_task_free_all(struct snd_compr_stream *stream) +{ + struct snd_compr_task_runtime *task; + + list_for_each_entry_reverse(task, &stream->runtime->tasks, list) + snd_compr_task_free_one(stream, task); +} + +typedef void (*snd_compr_seq_func_t)(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task); + +static int snd_compr_task_seq(struct snd_compr_stream *stream, unsigned lo= ng arg, + snd_compr_seq_func_t fcn) +{ + struct snd_compr_task_runtime *task; + __u64 seqno; + int retval; + + if (stream->runtime->state !=3D SNDRV_PCM_STATE_SETUP) + return -EPERM; + retval =3D get_user(seqno, (__u64 __user *)arg); + if (retval < 0) + return retval; + retval =3D 0; + if (seqno =3D=3D 0) { + list_for_each_entry_reverse(task, &stream->runtime->tasks, list) + fcn(stream, task); + } else { + task =3D snd_compr_find_task(stream, seqno); + if (!task) + retval =3D -EINVAL; + else + fcn(stream, task); + } + return retval; +} + +static int snd_compr_task_status(struct snd_compr_stream *stream, + struct snd_compr_task_status *status) +{ + struct snd_compr_task_runtime *task; + + task =3D snd_compr_find_task(stream, status->seqno); + if (!task) + return -EINVAL; + status->input_size =3D task->input_size; + status->output_size =3D task->output_size; + status->state =3D task->state; + return 0; +} + +static int snd_compr_task_status_ioctl(struct snd_compr_stream *stream, un= signed long arg) +{ + struct snd_compr_task_status *status __free(kfree) =3D NULL; + int retval; + + if (stream->runtime->state !=3D SNDRV_PCM_STATE_SETUP) + return -EPERM; + status =3D memdup_user((void __user *)arg, sizeof(*status)); + if (IS_ERR(status)) + return PTR_ERR(no_free_ptr(status)); + retval =3D snd_compr_task_status(stream, status); + if (retval >=3D 0) + if (copy_to_user((void __user *)arg, status, sizeof(*status))) + retval =3D -EFAULT; + return retval; +} + +/** + * snd_compr_task_finished: Notify that the task was finished + * @stream: pointer to stream + * @task: runtime task structure + * + * Set the finished task state and notify waiters. + */ +void snd_compr_task_finished(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + guard(mutex)(&stream->device->lock); + if (!snd_BUG_ON(stream->runtime->active_tasks =3D=3D 0)) + stream->runtime->active_tasks--; + task->state =3D SND_COMPRESS_TASK_STATE_FINISHED; + wake_up(&stream->runtime->sleep); +} +EXPORT_SYMBOL_GPL(snd_compr_task_finished); + +#endif /* CONFIG_SND_COMPRESS_ACCEL */ + static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned lon= g arg) { struct snd_compr_file *data =3D f->private_data; @@ -968,6 +1281,27 @@ static long snd_compr_ioctl(struct file *f, unsigned = int cmd, unsigned long arg) return snd_compr_set_metadata(stream, arg); case _IOC_NR(SNDRV_COMPRESS_GET_METADATA): return snd_compr_get_metadata(stream, arg); + } + + if (stream->direction =3D=3D SND_COMPRESS_ACCEL) { +#if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL) + switch (_IOC_NR(cmd)) { + case _IOC_NR(SNDRV_COMPRESS_TASK_CREATE): + return snd_compr_task_create(stream, arg); + case _IOC_NR(SNDRV_COMPRESS_TASK_FREE): + return snd_compr_task_seq(stream, arg, snd_compr_task_free_one); + case _IOC_NR(SNDRV_COMPRESS_TASK_START): + return snd_compr_task_start_ioctl(stream, arg); + case _IOC_NR(SNDRV_COMPRESS_TASK_STOP): + return snd_compr_task_seq(stream, arg, snd_compr_task_stop_one); + case _IOC_NR(SNDRV_COMPRESS_TASK_STATUS): + return snd_compr_task_status_ioctl(stream, arg); + } +#endif + return -ENOTTY; + } + + switch (_IOC_NR(cmd)) { case _IOC_NR(SNDRV_COMPRESS_TSTAMP): return snd_compr_tstamp(stream, arg); case _IOC_NR(SNDRV_COMPRESS_AVAIL): @@ -1140,6 +1474,11 @@ int snd_compress_new(struct snd_card *card, int devi= ce, }; int ret; =20 +#if !IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL) + if (snd_BUG_ON(dirn =3D=3D SND_COMPRESS_ACCEL)) + return -EINVAL; +#endif + compr->card =3D card; compr->device =3D device; compr->direction =3D dirn; --=20 2.34.1 From nobody Fri Nov 29 03:39:53 2024 Received: from inva021.nxp.com (inva021.nxp.com [92.121.34.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C710156991; Wed, 25 Sep 2024 07:17:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=92.121.34.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248675; cv=none; b=H2ORcv2s2H0hZXujavOslTneWhkF9WkeUi7rcx50l5EUPao9qhuaDaC312DycSMYD7dxfGavMO6dZe/msQvP7uhQuSd9Mik+Ji9MD14gJLzXRaZ6MHBRIxKGPzAl08QuxSQNPo7GJ6kDUgL1WuPePwmEY6EPsO/FRlXyWPsINU8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248675; c=relaxed/simple; bh=jUu/gw9EyeaLrWHEIcS7oVbSQyZEBWsLMRaM9p5I8zY=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=jMxDEH+0RL/hwsUnsJuWDpTnLxxcEHCaG1YwBfi2d3ncJfxlTofASH+fMPZ2d6fnUbt2F8kGc4DQY4UcmrYcD5zE5O+SOSLbl8LeLWSZdw7PZY6Rvb3c1d9nfA8/W5mTTkeXzM/N/Lr2aL9qW9ggovio598LxIDRJlrEO8dMXLA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; arc=none smtp.client-ip=92.121.34.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 683732016BA; Wed, 25 Sep 2024 09:17:51 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 3A86D202A12; Wed, 25 Sep 2024 09:17:51 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id 8AB56183AD44; Wed, 25 Sep 2024 15:17:49 +0800 (+08) From: Shengjiu Wang To: vkoul@kernel.org, perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, shengjiu.wang@gmail.com, Xiubo.Lee@gmail.com, festevam@gmail.com, nicoleotsuka@gmail.com, lgirdwood@gmail.com, broonie@kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 2/7] ALSA: compress: Add output rate and output format support Date: Wed, 25 Sep 2024 14:55:11 +0800 Message-Id: <1727247316-16156-3-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> References: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add 'pcm_format' for struct snd_codec, add 'pcm_formats' for struct snd_codec_desc, these are used for accelerator usage. Current accelerator example is sample rate converter (SRC). Define struct snd_codec_desc_src for descript minmum and maxmum sample rates. And add 'src_d' in union snd_codec_options structure. These are mainly used for capbility query. Signed-off-by: Jaroslav Kysela Signed-off-by: Shengjiu Wang --- include/uapi/sound/compress_params.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/include/uapi/sound/compress_params.h b/include/uapi/sound/comp= ress_params.h index ddc77322d571..bc7648a30746 100644 --- a/include/uapi/sound/compress_params.h +++ b/include/uapi/sound/compress_params.h @@ -334,6 +334,14 @@ union snd_codec_options { struct snd_dec_wma wma_d; struct snd_dec_alac alac_d; struct snd_dec_ape ape_d; + struct { + __u32 out_sample_rate; + } src_d; +} __attribute__((packed, aligned(4))); + +struct snd_codec_desc_src { + __u32 out_sample_rate_min; + __u32 out_sample_rate_max; } __attribute__((packed, aligned(4))); =20 /** struct snd_codec_desc - description of codec capabilities @@ -347,6 +355,9 @@ union snd_codec_options { * @modes: Supported modes. See SND_AUDIOMODE defines * @formats: Supported formats. See SND_AUDIOSTREAMFORMAT defines * @min_buffer: Minimum buffer size handled by codec implementation + * @pcm_formats: Output (for decoders) or input (for encoders) + * PCM formats (required to accel mode, 0 for other modes) + * @u_space: union space (for codec dependent data) * @reserved: reserved for future use * * This structure provides a scalar value for profiles, modes and stream @@ -370,7 +381,12 @@ struct snd_codec_desc { __u32 modes; __u32 formats; __u32 min_buffer; - __u32 reserved[15]; + __u32 pcm_formats; + union { + __u32 u_space[6]; + struct snd_codec_desc_src src; + } __attribute__((packed, aligned(4))); + __u32 reserved[8]; } __attribute__((packed, aligned(4))); =20 /** struct snd_codec @@ -395,6 +411,8 @@ struct snd_codec_desc { * @align: Block alignment in bytes of an audio sample. * Only required for PCM or IEC formats. * @options: encoder-specific settings + * @pcm_format: Output (for decoders) or input (for encoders) + * PCM formats (required to accel mode, 0 for other modes) * @reserved: reserved for future use */ =20 @@ -411,7 +429,8 @@ struct snd_codec { __u32 format; __u32 align; union snd_codec_options options; - __u32 reserved[3]; + __u32 pcm_format; + __u32 reserved[2]; } __attribute__((packed, aligned(4))); =20 #endif --=20 2.34.1 From nobody Fri Nov 29 03:39:53 2024 Received: from inva021.nxp.com (inva021.nxp.com [92.121.34.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50851157466; Wed, 25 Sep 2024 07:17:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=92.121.34.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248676; cv=none; b=cZpFikbmHCSCRHupod8Z1DOa4HWJOe8zhbTlA6rtWjpzdnbDExSCX79eH4NUwxulx6NEbGjlKf8kf/MatXh5J/x2/WV+p30QfuoaETyKo/Q8gAnPtqPG3ZbS0Z79koNEsV6BTz4PAybPsWdzUIShgRmR4kYgIZVnMJ8UNkLKrL8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248676; c=relaxed/simple; bh=5yureuo1A0ecuElb3Cf5jLfINBKb3VQp1fXFV66Re1M=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=kA8j5IgZmyL4QnAefWvcQvU8UaCsLKK/cCD3bV37+7Ls+bEvLaEzMGBGsMmGp4ne5IWtdhspPPUBuLTdUeRx+2WOzPsGASZugP380RxbiEDEax/PtzfwJBoQN7aF7vnGcgV4mQJHo2IrY5cjRVZ+eQso+buItK6F0L4RBYDMBOc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; arc=none smtp.client-ip=92.121.34.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 985E02025F9; Wed, 25 Sep 2024 09:17:52 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 6896C2029FB; Wed, 25 Sep 2024 09:17:52 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id B88A9183AD46; Wed, 25 Sep 2024 15:17:50 +0800 (+08) From: Shengjiu Wang To: vkoul@kernel.org, perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, shengjiu.wang@gmail.com, Xiubo.Lee@gmail.com, festevam@gmail.com, nicoleotsuka@gmail.com, lgirdwood@gmail.com, broonie@kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 3/7] ASoC: fsl_asrc: define functions for memory to memory usage Date: Wed, 25 Sep 2024 14:55:12 +0800 Message-Id: <1727247316-16156-4-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> References: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" ASRC can be used on memory to memory case, define several functions for m2m usage. m2m_prepare: prepare for the start step m2m_start: the start step m2m_unprepare: unprepare for stop step, optional m2m_stop: stop step m2m_check_format: check format is supported or not m2m_calc_out_len: calculate output length according to input length m2m_get_maxburst: burst size for dma m2m_pair_suspend: suspend function of pair, optional. m2m_pair_resume: resume function of pair get_output_fifo_size: get remaining data size in FIFO Signed-off-by: Shengjiu Wang --- sound/soc/fsl/fsl_asrc.c | 142 ++++++++++++++++++++++++++++++++ sound/soc/fsl/fsl_asrc.h | 2 + sound/soc/fsl/fsl_asrc_common.h | 61 ++++++++++++++ 3 files changed, 205 insertions(+) diff --git a/sound/soc/fsl/fsl_asrc.c b/sound/soc/fsl/fsl_asrc.c index b793263291dc..b1b35954f7ac 100644 --- a/sound/soc/fsl/fsl_asrc.c +++ b/sound/soc/fsl/fsl_asrc.c @@ -1063,6 +1063,139 @@ static int fsl_asrc_get_fifo_addr(u8 dir, enum asrc= _pair_index index) return REG_ASRDx(dir, index); } =20 +/* Get sample numbers in FIFO */ +static unsigned int fsl_asrc_get_output_fifo_size(struct fsl_asrc_pair *pa= ir) +{ + struct fsl_asrc *asrc =3D pair->asrc; + enum asrc_pair_index index =3D pair->index; + u32 val; + + regmap_read(asrc->regmap, REG_ASRFST(index), &val); + + val &=3D ASRFSTi_OUTPUT_FIFO_MASK; + + return val >> ASRFSTi_OUTPUT_FIFO_SHIFT; +} + +static int fsl_asrc_m2m_prepare(struct fsl_asrc_pair *pair) +{ + struct fsl_asrc_pair_priv *pair_priv =3D pair->private; + struct fsl_asrc *asrc =3D pair->asrc; + struct device *dev =3D &asrc->pdev->dev; + struct asrc_config config; + int ret; + + /* fill config */ + config.pair =3D pair->index; + config.channel_num =3D pair->channels; + config.input_sample_rate =3D pair->rate[IN]; + config.output_sample_rate =3D pair->rate[OUT]; + config.input_format =3D pair->sample_format[IN]; + config.output_format =3D pair->sample_format[OUT]; + config.inclk =3D INCLK_NONE; + config.outclk =3D OUTCLK_ASRCK1_CLK; + + pair_priv->config =3D &config; + ret =3D fsl_asrc_config_pair(pair, true); + if (ret) { + dev_err(dev, "failed to config pair: %d\n", ret); + return ret; + } + + pair->first_convert =3D 1; + + return 0; +} + +static int fsl_asrc_m2m_start(struct fsl_asrc_pair *pair) +{ + if (pair->first_convert) { + fsl_asrc_start_pair(pair); + pair->first_convert =3D 0; + } + /* + * Clear DMA request during the stall state of ASRC: + * During STALL state, the remaining in input fifo would never be + * smaller than the input threshold while the output fifo would not + * be bigger than output one. Thus the DMA request would be cleared. + */ + fsl_asrc_set_watermarks(pair, ASRC_FIFO_THRESHOLD_MIN, + ASRC_FIFO_THRESHOLD_MAX); + + /* Update the real input threshold to raise DMA request */ + fsl_asrc_set_watermarks(pair, ASRC_M2M_INPUTFIFO_WML, + ASRC_M2M_OUTPUTFIFO_WML); + + return 0; +} + +static int fsl_asrc_m2m_stop(struct fsl_asrc_pair *pair) +{ + if (!pair->first_convert) { + fsl_asrc_stop_pair(pair); + pair->first_convert =3D 1; + } + + return 0; +} + +/* calculate capture data length according to output data length and sampl= e rate */ +static int fsl_asrc_m2m_calc_out_len(struct fsl_asrc_pair *pair, int input= _buffer_length) +{ + unsigned int in_width, out_width; + unsigned int channels =3D pair->channels; + unsigned int in_samples, out_samples; + unsigned int out_length; + + in_width =3D snd_pcm_format_physical_width(pair->sample_format[IN]) / 8; + out_width =3D snd_pcm_format_physical_width(pair->sample_format[OUT]) / 8; + + in_samples =3D input_buffer_length / in_width / channels; + out_samples =3D pair->rate[OUT] * in_samples / pair->rate[IN]; + out_length =3D (out_samples - ASRC_OUTPUT_LAST_SAMPLE) * out_width * chan= nels; + + return out_length; +} + +static int fsl_asrc_m2m_get_maxburst(u8 dir, struct fsl_asrc_pair *pair) +{ + struct fsl_asrc *asrc =3D pair->asrc; + struct fsl_asrc_priv *asrc_priv =3D asrc->private; + int wml =3D (dir =3D=3D IN) ? ASRC_M2M_INPUTFIFO_WML : ASRC_M2M_OUTPUTFIF= O_WML; + + if (!asrc_priv->soc->use_edma) + return wml * pair->channels; + else + return 1; +} + +static int fsl_asrc_m2m_get_cap(struct fsl_asrc_m2m_cap *cap) +{ + cap->fmt_in =3D FSL_ASRC_FORMATS; + cap->fmt_out =3D FSL_ASRC_FORMATS | SNDRV_PCM_FMTBIT_S8; + + cap->rate_in =3D supported_asrc_rate; + cap->rate_in_count =3D ARRAY_SIZE(supported_asrc_rate); + cap->rate_out =3D supported_asrc_rate; + cap->rate_out_count =3D ARRAY_SIZE(supported_asrc_rate); + cap->chan_min =3D 1; + cap->chan_max =3D 10; + + return 0; +} + +static int fsl_asrc_m2m_pair_resume(struct fsl_asrc_pair *pair) +{ + struct fsl_asrc *asrc =3D pair->asrc; + int i; + + for (i =3D 0; i < pair->channels * 4; i++) + regmap_write(asrc->regmap, REG_ASRDI(pair->index), 0); + + pair->first_convert =3D 1; + return 0; +} + static int fsl_asrc_runtime_resume(struct device *dev); static int fsl_asrc_runtime_suspend(struct device *dev); =20 @@ -1147,6 +1280,15 @@ static int fsl_asrc_probe(struct platform_device *pd= ev) asrc->get_fifo_addr =3D fsl_asrc_get_fifo_addr; asrc->pair_priv_size =3D sizeof(struct fsl_asrc_pair_priv); =20 + asrc->m2m_prepare =3D fsl_asrc_m2m_prepare; + asrc->m2m_start =3D fsl_asrc_m2m_start; + asrc->m2m_stop =3D fsl_asrc_m2m_stop; + asrc->get_output_fifo_size =3D fsl_asrc_get_output_fifo_size; + asrc->m2m_calc_out_len =3D fsl_asrc_m2m_calc_out_len; + asrc->m2m_get_maxburst =3D fsl_asrc_m2m_get_maxburst; + asrc->m2m_pair_resume =3D fsl_asrc_m2m_pair_resume; + asrc->m2m_get_cap =3D fsl_asrc_m2m_get_cap; + if (of_device_is_compatible(np, "fsl,imx35-asrc")) { asrc_priv->clk_map[IN] =3D input_clk_map_imx35; asrc_priv->clk_map[OUT] =3D output_clk_map_imx35; diff --git a/sound/soc/fsl/fsl_asrc.h b/sound/soc/fsl/fsl_asrc.h index 86d2422ad606..1c492eb237f5 100644 --- a/sound/soc/fsl/fsl_asrc.h +++ b/sound/soc/fsl/fsl_asrc.h @@ -12,6 +12,8 @@ =20 #include "fsl_asrc_common.h" =20 +#define ASRC_M2M_INPUTFIFO_WML 0x4 +#define ASRC_M2M_OUTPUTFIFO_WML 0x2 #define ASRC_DMA_BUFFER_NUM 2 #define ASRC_INPUTFIFO_THRESHOLD 32 #define ASRC_OUTPUTFIFO_THRESHOLD 32 diff --git a/sound/soc/fsl/fsl_asrc_common.h b/sound/soc/fsl/fsl_asrc_commo= n.h index 7e1c13ca37f1..3bdd6ea07c09 100644 --- a/sound/soc/fsl/fsl_asrc_common.h +++ b/sound/soc/fsl/fsl_asrc_common.h @@ -21,6 +21,26 @@ enum asrc_pair_index { =20 #define PAIR_CTX_NUM 0x4 =20 +/** + * struct fsl_asrc_m2m_cap - capability data + * @fmt_in: input sample format + * @fmt_out: output sample format + * @chan_min: minimum channel number + * @chan_max: maximum channel number + * @rate_in: minimum rate + * @rate_out: maximum rete + */ +struct fsl_asrc_m2m_cap { + u64 fmt_in; + u64 fmt_out; + int chan_min; + int chan_max; + const unsigned int *rate_in; + int rate_in_count; + const unsigned int *rate_out; + int rate_out_count; +}; + /** * fsl_asrc_pair: ASRC Pair common data * @@ -34,6 +54,13 @@ enum asrc_pair_index { * @pos: hardware pointer position * @req_dma_chan: flag to release dev_to_dev chan * @private: pair private area + * @complete: dma task complete + * @sample_format: format of m2m + * @rate: rate of m2m + * @buf_len: buffer length of m2m + * @dma_buffer: buffer pointers + * @first_convert: start of conversion + * @ratio_mod: ratio modification */ struct fsl_asrc_pair { struct fsl_asrc *asrc; @@ -49,6 +76,15 @@ struct fsl_asrc_pair { bool req_dma_chan; =20 void *private; + + /* used for m2m */ + struct completion complete[2]; + snd_pcm_format_t sample_format[2]; + unsigned int rate[2]; + unsigned int buf_len[2]; + struct snd_dma_buffer dma_buffer[2]; + unsigned int first_convert; + unsigned int ratio_mod; }; =20 /** @@ -72,6 +108,17 @@ struct fsl_asrc_pair { * @request_pair: function pointer * @release_pair: function pointer * @get_fifo_addr: function pointer + * @m2m_get_cap: function pointer + * @m2m_prepare: function pointer + * @m2m_start: function pointer + * @m2m_unprepare: function pointer + * @m2m_stop: function pointer + * @m2m_calc_out_len: function pointer + * @m2m_get_maxburst: function pointer + * @m2m_pair_suspend: function pointer + * @m2m_pair_resume: function pointer + * @m2m_set_ratio_mod: function pointer + * @get_output_fifo_size: function pointer * @pair_priv_size: size of pair private struct. * @private: private data structure */ @@ -97,6 +144,20 @@ struct fsl_asrc { int (*request_pair)(int channels, struct fsl_asrc_pair *pair); void (*release_pair)(struct fsl_asrc_pair *pair); int (*get_fifo_addr)(u8 dir, enum asrc_pair_index index); + int (*m2m_get_cap)(struct fsl_asrc_m2m_cap *cap); + + int (*m2m_prepare)(struct fsl_asrc_pair *pair); + int (*m2m_start)(struct fsl_asrc_pair *pair); + int (*m2m_unprepare)(struct fsl_asrc_pair *pair); + int (*m2m_stop)(struct fsl_asrc_pair *pair); + + int (*m2m_calc_out_len)(struct fsl_asrc_pair *pair, int input_buffer_leng= th); + int (*m2m_get_maxburst)(u8 dir, struct fsl_asrc_pair *pair); + int (*m2m_pair_suspend)(struct fsl_asrc_pair *pair); + int (*m2m_pair_resume)(struct fsl_asrc_pair *pair); + int (*m2m_set_ratio_mod)(struct fsl_asrc_pair *pair, int val); + + unsigned int (*get_output_fifo_size)(struct fsl_asrc_pair *pair); size_t pair_priv_size; =20 void *private; --=20 2.34.1 From nobody Fri Nov 29 03:39:53 2024 Received: from inva021.nxp.com (inva021.nxp.com [92.121.34.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67FC9157A48; Wed, 25 Sep 2024 07:17:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=92.121.34.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248677; cv=none; b=friLqalxggRWRPoucVg8MUvDRw6/xqTTnQ0ec3edqxoS+oo40sIu/Mff3kly1mrV7EfLpl0TfXZFNcaSib1yS9yN/RtQ4yw0mxGMG0pE8VEV14VmecvxqU5zh12Q+LpHC5zzKt1f14J8h1LBfmHvLxO22gbNuYjd1VrzdsCxkYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248677; c=relaxed/simple; bh=Sqpyr1ehLvpdpSWpBcm6s7wKkpfpYWiS6ljjRuKIWrI=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=kvbFJ0e7LOoRFbJos94rWGPVo6eIRG0cG+0EEIuPN3nZckFxDbIl74ltfysCgVUgaDRKEaQLV+544zdNiseN6Ytzn0VKFV5P6Sk80lKjEAynbTMYTp16suYvyldGZQOIyMHdhb3PxErI/ceh4uBauqCDYlosALD+ifWs3QDjYH8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; arc=none smtp.client-ip=92.121.34.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id C8E212029FB; Wed, 25 Sep 2024 09:17:53 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 9691C201FFF; Wed, 25 Sep 2024 09:17:53 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id E6E51183AD44; Wed, 25 Sep 2024 15:17:51 +0800 (+08) From: Shengjiu Wang To: vkoul@kernel.org, perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, shengjiu.wang@gmail.com, Xiubo.Lee@gmail.com, festevam@gmail.com, nicoleotsuka@gmail.com, lgirdwood@gmail.com, broonie@kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 4/7] ASoC: fsl_easrc: define functions for memory to memory usage Date: Wed, 25 Sep 2024 14:55:13 +0800 Message-Id: <1727247316-16156-5-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> References: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" ASRC can be used on memory to memory case, define several functions for m2m usage and export them as function pointer. Signed-off-by: Shengjiu Wang --- sound/soc/fsl/fsl_easrc.c | 228 ++++++++++++++++++++++++++++++++++++++ sound/soc/fsl/fsl_easrc.h | 4 + 2 files changed, 232 insertions(+) diff --git a/sound/soc/fsl/fsl_easrc.c b/sound/soc/fsl/fsl_easrc.c index 962f30912091..a1a7a90ff5ef 100644 --- a/sound/soc/fsl/fsl_easrc.c +++ b/sound/soc/fsl/fsl_easrc.c @@ -1861,6 +1861,224 @@ static int fsl_easrc_get_fifo_addr(u8 dir, enum asr= c_pair_index index) return REG_EASRC_FIFO(dir, index); } =20 +/* Get sample numbers in FIFO */ +static unsigned int fsl_easrc_get_output_fifo_size(struct fsl_asrc_pair *p= air) +{ + struct fsl_asrc *asrc =3D pair->asrc; + enum asrc_pair_index index =3D pair->index; + u32 val; + + regmap_read(asrc->regmap, REG_EASRC_SFS(index), &val); + val &=3D EASRC_SFS_NSGO_MASK; + + return val >> EASRC_SFS_NSGO_SHIFT; +} + +static int fsl_easrc_m2m_prepare(struct fsl_asrc_pair *pair) +{ + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + struct fsl_asrc *asrc =3D pair->asrc; + struct device *dev =3D &asrc->pdev->dev; + int ret; + + ctx_priv->in_params.sample_rate =3D pair->rate[IN]; + ctx_priv->in_params.sample_format =3D pair->sample_format[IN]; + ctx_priv->out_params.sample_rate =3D pair->rate[OUT]; + ctx_priv->out_params.sample_format =3D pair->sample_format[OUT]; + + ctx_priv->in_params.fifo_wtmk =3D FSL_EASRC_INPUTFIFO_WML; + ctx_priv->out_params.fifo_wtmk =3D FSL_EASRC_OUTPUTFIFO_WML; + /* Fill the right half of the re-sampler with zeros */ + ctx_priv->rs_init_mode =3D 0x2; + /* Zero fill the right half of the prefilter */ + ctx_priv->pf_init_mode =3D 0x2; + + ret =3D fsl_easrc_set_ctx_format(pair, + &ctx_priv->in_params.sample_format, + &ctx_priv->out_params.sample_format); + if (ret) { + dev_err(dev, "failed to set context format: %d\n", ret); + return ret; + } + + ret =3D fsl_easrc_config_context(asrc, pair->index); + if (ret) { + dev_err(dev, "failed to config context %d\n", ret); + return ret; + } + + ctx_priv->in_params.iterations =3D 1; + ctx_priv->in_params.group_len =3D pair->channels; + ctx_priv->in_params.access_len =3D pair->channels; + ctx_priv->out_params.iterations =3D 1; + ctx_priv->out_params.group_len =3D pair->channels; + ctx_priv->out_params.access_len =3D pair->channels; + + ret =3D fsl_easrc_set_ctx_organziation(pair); + if (ret) { + dev_err(dev, "failed to set fifo organization\n"); + return ret; + } + + /* The context start flag */ + pair->first_convert =3D 1; + return 0; +} + +static int fsl_easrc_m2m_start(struct fsl_asrc_pair *pair) +{ + /* start context once */ + if (pair->first_convert) { + fsl_easrc_start_context(pair); + pair->first_convert =3D 0; + } + + return 0; +} + +static int fsl_easrc_m2m_stop(struct fsl_asrc_pair *pair) +{ + /* Stop pair/context */ + if (!pair->first_convert) { + fsl_easrc_stop_context(pair); + pair->first_convert =3D 1; + } + + return 0; +} + +/* calculate capture data length according to output data length and sampl= e rate */ +static int fsl_easrc_m2m_calc_out_len(struct fsl_asrc_pair *pair, int inpu= t_buffer_length) +{ + struct fsl_asrc *easrc =3D pair->asrc; + struct fsl_easrc_priv *easrc_priv =3D easrc->private; + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + unsigned int in_rate =3D ctx_priv->in_params.norm_rate; + unsigned int out_rate =3D ctx_priv->out_params.norm_rate; + unsigned int channels =3D pair->channels; + unsigned int in_samples, out_samples; + unsigned int in_width, out_width; + unsigned int out_length; + unsigned int frac_bits; + u64 val1, val2; + + switch (easrc_priv->rs_num_taps) { + case EASRC_RS_32_TAPS: + /* integer bits =3D 5; */ + frac_bits =3D 39; + break; + case EASRC_RS_64_TAPS: + /* integer bits =3D 6; */ + frac_bits =3D 38; + break; + case EASRC_RS_128_TAPS: + /* integer bits =3D 7; */ + frac_bits =3D 37; + break; + default: + return -EINVAL; + } + + val1 =3D (u64)in_rate << frac_bits; + do_div(val1, out_rate); + val1 +=3D (s64)ctx_priv->ratio_mod << (frac_bits - 31); + + in_width =3D snd_pcm_format_physical_width(ctx_priv->in_params.sample_for= mat) / 8; + out_width =3D snd_pcm_format_physical_width(ctx_priv->out_params.sample_f= ormat) / 8; + + ctx_priv->in_filled_len +=3D input_buffer_length; + if (ctx_priv->in_filled_len <=3D ctx_priv->in_filled_sample * in_width * = channels) { + out_length =3D 0; + } else { + in_samples =3D ctx_priv->in_filled_len / (in_width * channels) - + ctx_priv->in_filled_sample; + + /* right shift 12 bit to make ratio in 32bit space */ + val2 =3D (u64)in_samples << (frac_bits - 12); + val1 =3D val1 >> 12; + do_div(val2, val1); + out_samples =3D val2; + + out_length =3D out_samples * out_width * channels; + ctx_priv->in_filled_len =3D ctx_priv->in_filled_sample * in_width * chan= nels; + } + + return out_length; +} + +static int fsl_easrc_m2m_get_maxburst(u8 dir, struct fsl_asrc_pair *pair) +{ + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + + if (dir =3D=3D IN) + return ctx_priv->in_params.fifo_wtmk * pair->channels; + else + return ctx_priv->out_params.fifo_wtmk * pair->channels; +} + +static int fsl_easrc_m2m_pair_suspend(struct fsl_asrc_pair *pair) +{ + fsl_easrc_stop_context(pair); + + return 0; +} + +static int fsl_easrc_m2m_pair_resume(struct fsl_asrc_pair *pair) +{ + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + + pair->first_convert =3D 1; + ctx_priv->in_filled_len =3D 0; + + return 0; +} + +/* val is Q31 */ +static int fsl_easrc_m2m_set_ratio_mod(struct fsl_asrc_pair *pair, int val) +{ + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + struct fsl_asrc *easrc =3D pair->asrc; + struct fsl_easrc_priv *easrc_priv =3D easrc->private; + unsigned int frac_bits; + + ctx_priv->ratio_mod +=3D val; + + switch (easrc_priv->rs_num_taps) { + case EASRC_RS_32_TAPS: + /* integer bits =3D 5; */ + frac_bits =3D 39; + break; + case EASRC_RS_64_TAPS: + /* integer bits =3D 6; */ + frac_bits =3D 38; + break; + case EASRC_RS_128_TAPS: + /* integer bits =3D 7; */ + frac_bits =3D 37; + break; + default: + return -EINVAL; + } + + val <<=3D (frac_bits - 31); + regmap_write(easrc->regmap, REG_EASRC_RUC(pair->index), EASRC_RSUC_RS_RM(= val)); + + return 0; +} + +static int fsl_easrc_m2m_get_cap(struct fsl_asrc_m2m_cap *cap) +{ + cap->fmt_in =3D FSL_EASRC_FORMATS; + cap->fmt_out =3D FSL_EASRC_FORMATS | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; + cap->rate_in =3D easrc_rates; + cap->rate_in_count =3D ARRAY_SIZE(easrc_rates); + cap->rate_out =3D easrc_rates; + cap->rate_out_count =3D ARRAY_SIZE(easrc_rates); + cap->chan_min =3D 1; + cap->chan_max =3D 32; + return 0; +} + static const struct of_device_id fsl_easrc_dt_ids[] =3D { { .compatible =3D "fsl,imx8mn-easrc",}, {} @@ -1926,6 +2144,16 @@ static int fsl_easrc_probe(struct platform_device *p= dev) easrc->release_pair =3D fsl_easrc_release_context; easrc->get_fifo_addr =3D fsl_easrc_get_fifo_addr; easrc->pair_priv_size =3D sizeof(struct fsl_easrc_ctx_priv); + easrc->m2m_prepare =3D fsl_easrc_m2m_prepare; + easrc->m2m_start =3D fsl_easrc_m2m_start; + easrc->m2m_stop =3D fsl_easrc_m2m_stop; + easrc->get_output_fifo_size =3D fsl_easrc_get_output_fifo_size; + easrc->m2m_calc_out_len =3D fsl_easrc_m2m_calc_out_len; + easrc->m2m_get_maxburst =3D fsl_easrc_m2m_get_maxburst; + easrc->m2m_pair_suspend =3D fsl_easrc_m2m_pair_suspend; + easrc->m2m_pair_resume =3D fsl_easrc_m2m_pair_resume; + easrc->m2m_set_ratio_mod =3D fsl_easrc_m2m_set_ratio_mod; + easrc->m2m_get_cap =3D fsl_easrc_m2m_get_cap; =20 easrc_priv->rs_num_taps =3D EASRC_RS_32_TAPS; easrc_priv->const_coeff =3D 0x3FF0000000000000; diff --git a/sound/soc/fsl/fsl_easrc.h b/sound/soc/fsl/fsl_easrc.h index 7c70dac52713..c9f770862662 100644 --- a/sound/soc/fsl/fsl_easrc.h +++ b/sound/soc/fsl/fsl_easrc.h @@ -601,6 +601,8 @@ struct fsl_easrc_slot { * @out_missed_sample: sample missed in output * @st1_addexp: exponent added for stage1 * @st2_addexp: exponent added for stage2 + * @ratio_mod: update ratio + * @in_filled_len: input filled length */ struct fsl_easrc_ctx_priv { struct fsl_easrc_io_params in_params; @@ -618,6 +620,8 @@ struct fsl_easrc_ctx_priv { int out_missed_sample; int st1_addexp; int st2_addexp; + int ratio_mod; + unsigned int in_filled_len; }; =20 /** --=20 2.34.1 From nobody Fri Nov 29 03:39:53 2024 Received: from inva021.nxp.com (inva021.nxp.com [92.121.34.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A865615853A; Wed, 25 Sep 2024 07:17:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=92.121.34.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248679; cv=none; b=Qug0aOoXa34cyFSUtI5uMiwqIUU5ZWXLr3clUzVEXeWBVZOhluAzOPaDboDdapiNUqM4/O0U2h6rjj/B4eBliGJiLSwzS2k1tQDfDGQruN7UtEpy72s3mnFSKISVSk45KqXXx8NTjzTkYXPRwjfCmonm9/O4wwzslElyMKBdPVo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248679; c=relaxed/simple; bh=pYt1K79P/H3c2K7SbqLwuJcsvsa2d1GvT+MnnuEMYrI=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=dtTHhhFbEw3ajNwc5ip9qWWYp5/pDewLmjsR/93+K1hvb572XKbCmAN27JSG3F8pCqMosFVBakD901n9+8eQQtLyxN2/zPOXmrFlYI856FlRgKo7Vp5bURYKD800eSTPotcVsOUS4u5RfvoIJn6uaZkSG9sn6UXle88f2OjIeeY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; arc=none smtp.client-ip=92.121.34.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 30503201FFF; Wed, 25 Sep 2024 09:17:55 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id C4DC0202A47; Wed, 25 Sep 2024 09:17:54 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id 20A90183AD46; Wed, 25 Sep 2024 15:17:53 +0800 (+08) From: Shengjiu Wang To: vkoul@kernel.org, perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, shengjiu.wang@gmail.com, Xiubo.Lee@gmail.com, festevam@gmail.com, nicoleotsuka@gmail.com, lgirdwood@gmail.com, broonie@kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 5/7] ASoC: fsl_asrc_m2m: Add memory to memory function Date: Wed, 25 Sep 2024 14:55:14 +0800 Message-Id: <1727247316-16156-6-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> References: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Implement the ASRC memory to memory function using the compress framework, user can use this function with compress ioctl interface. This feature can be shared by ASRC and EASRC drivers Signed-off-by: Shengjiu Wang --- sound/soc/fsl/Kconfig | 1 + sound/soc/fsl/Makefile | 2 +- sound/soc/fsl/fsl_asrc_common.h | 9 + sound/soc/fsl/fsl_asrc_m2m.c | 727 ++++++++++++++++++++++++++++++++ 4 files changed, 738 insertions(+), 1 deletion(-) create mode 100644 sound/soc/fsl/fsl_asrc_m2m.c diff --git a/sound/soc/fsl/Kconfig b/sound/soc/fsl/Kconfig index e283751abfef..bff9c6bda344 100644 --- a/sound/soc/fsl/Kconfig +++ b/sound/soc/fsl/Kconfig @@ -8,6 +8,7 @@ config SND_SOC_FSL_ASRC depends on HAS_DMA select REGMAP_MMIO select SND_SOC_GENERIC_DMAENGINE_PCM + select SND_COMPRESS_ACCEL help Say Y if you want to add Asynchronous Sample Rate Converter (ASRC) support for the Freescale CPUs. diff --git a/sound/soc/fsl/Makefile b/sound/soc/fsl/Makefile index ad97244b5cc3..d656a9ab54e3 100644 --- a/sound/soc/fsl/Makefile +++ b/sound/soc/fsl/Makefile @@ -10,7 +10,7 @@ obj-$(CONFIG_SND_SOC_P1022_RDK) +=3D snd-soc-p1022-rdk.o # Freescale SSI/DMA/SAI/SPDIF Support snd-soc-fsl-audmix-y :=3D fsl_audmix.o snd-soc-fsl-asoc-card-y :=3D fsl-asoc-card.o -snd-soc-fsl-asrc-y :=3D fsl_asrc.o fsl_asrc_dma.o +snd-soc-fsl-asrc-y :=3D fsl_asrc.o fsl_asrc_dma.o fsl_asrc_m2m.o snd-soc-fsl-lpc3xxx-y :=3D lpc3xxx-pcm.o lpc3xxx-i2s.o snd-soc-fsl-sai-y :=3D fsl_sai.o snd-soc-fsl-ssi-y :=3D fsl_ssi.o diff --git a/sound/soc/fsl/fsl_asrc_common.h b/sound/soc/fsl/fsl_asrc_commo= n.h index 3bdd6ea07c09..0cd595b0f629 100644 --- a/sound/soc/fsl/fsl_asrc_common.h +++ b/sound/soc/fsl/fsl_asrc_common.h @@ -60,6 +60,7 @@ struct fsl_asrc_m2m_cap { * @buf_len: buffer length of m2m * @dma_buffer: buffer pointers * @first_convert: start of conversion + * @ratio_mod_flag: flag for new ratio modifier * @ratio_mod: ratio modification */ struct fsl_asrc_pair { @@ -84,6 +85,7 @@ struct fsl_asrc_pair { unsigned int buf_len[2]; struct snd_dma_buffer dma_buffer[2]; unsigned int first_convert; + bool ratio_mod_flag; unsigned int ratio_mod; }; =20 @@ -98,6 +100,7 @@ struct fsl_asrc_pair { * @mem_clk: clock source to access register * @ipg_clk: clock source to drive peripheral * @spba_clk: SPBA clock (optional, depending on SoC design) + * @card: compress sound card * @lock: spin lock for resource protection * @pair: pair pointers * @channel_avail: non-occupied channel numbers @@ -131,6 +134,7 @@ struct fsl_asrc { struct clk *mem_clk; struct clk *ipg_clk; struct clk *spba_clk; + struct snd_card *card; spinlock_t lock; /* spin lock for resource protection */ =20 struct fsl_asrc_pair *pair[PAIR_CTX_NUM]; @@ -166,4 +170,9 @@ struct fsl_asrc { #define DRV_NAME "fsl-asrc-dai" extern struct snd_soc_component_driver fsl_asrc_component; =20 +int fsl_asrc_m2m_init(struct fsl_asrc *asrc); +void fsl_asrc_m2m_exit(struct fsl_asrc *asrc); +int fsl_asrc_m2m_resume(struct fsl_asrc *asrc); +int fsl_asrc_m2m_suspend(struct fsl_asrc *asrc); + #endif /* _FSL_ASRC_COMMON_H */ diff --git a/sound/soc/fsl/fsl_asrc_m2m.c b/sound/soc/fsl/fsl_asrc_m2m.c new file mode 100644 index 000000000000..693ec0a477a4 --- /dev/null +++ b/sound/soc/fsl/fsl_asrc_m2m.c @@ -0,0 +1,727 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// Copyright (C) 2014-2016 Freescale Semiconductor, Inc. +// Copyright (C) 2019-2024 NXP +// +// Freescale ASRC Memory to Memory (M2M) driver + +#include +#include +#include +#include +#include +#include +#include + +#include "fsl_asrc_common.h" + +#define DIR_STR(dir) (dir) =3D=3D IN ? "in" : "out" + +#define ASRC_xPUT_DMA_CALLBACK(dir) \ + (((dir) =3D=3D IN) ? asrc_input_dma_callback \ + : asrc_output_dma_callback) + +/* Maximum output and capture buffer size */ +#define ASRC_M2M_BUFFER_SIZE (512 * 1024) + +/* Maximum output and capture period size */ +#define ASRC_M2M_PERIOD_SIZE (48 * 1024) + +/* dma complete callback */ +static void asrc_input_dma_callback(void *data) +{ + struct fsl_asrc_pair *pair =3D (struct fsl_asrc_pair *)data; + + complete(&pair->complete[IN]); +} + +/* dma complete callback */ +static void asrc_output_dma_callback(void *data) +{ + struct fsl_asrc_pair *pair =3D (struct fsl_asrc_pair *)data; + + complete(&pair->complete[OUT]); +} + +/** + *asrc_read_last_fifo: read all the remaining data from FIFO + *@pair: Structure pointer of fsl_asrc_pair + *@dma_vaddr: virtual address of capture buffer + *@length: payload length of capture buffer + */ +static void asrc_read_last_fifo(struct fsl_asrc_pair *pair, void *dma_vadd= r, u32 *length) +{ + struct fsl_asrc *asrc =3D pair->asrc; + enum asrc_pair_index index =3D pair->index; + u32 i, reg, size, t_size =3D 0, width; + u32 *reg32 =3D NULL; + u16 *reg16 =3D NULL; + u8 *reg24 =3D NULL; + + width =3D snd_pcm_format_physical_width(pair->sample_format[OUT]); + if (width =3D=3D 32) + reg32 =3D dma_vaddr + *length; + else if (width =3D=3D 16) + reg16 =3D dma_vaddr + *length; + else + reg24 =3D dma_vaddr + *length; +retry: + size =3D asrc->get_output_fifo_size(pair); + if (size + *length > ASRC_M2M_BUFFER_SIZE) + goto end; + + for (i =3D 0; i < size * pair->channels; i++) { + regmap_read(asrc->regmap, asrc->get_fifo_addr(OUT, index), ®); + if (reg32) { + *reg32++ =3D reg; + } else if (reg16) { + *reg16++ =3D (u16)reg; + } else { + *reg24++ =3D (u8)reg; + *reg24++ =3D (u8)(reg >> 8); + *reg24++ =3D (u8)(reg >> 16); + } + } + t_size +=3D size; + + /* In case there is data left in FIFO */ + if (size) + goto retry; +end: + /* Update payload length */ + if (reg32) + *length +=3D t_size * pair->channels * 4; + else if (reg16) + *length +=3D t_size * pair->channels * 2; + else + *length +=3D t_size * pair->channels * 3; +} + +/* config dma channel */ +static int asrc_dmaconfig(struct fsl_asrc_pair *pair, + struct dma_chan *chan, + u32 dma_addr, dma_addr_t buf_addr, u32 buf_len, + int dir, int width) +{ + struct fsl_asrc *asrc =3D pair->asrc; + struct device *dev =3D &asrc->pdev->dev; + struct dma_slave_config slave_config; + enum dma_slave_buswidth buswidth; + unsigned int sg_len, max_period_size; + struct scatterlist *sg; + int ret, i; + + switch (width) { + case 8: + buswidth =3D DMA_SLAVE_BUSWIDTH_1_BYTE; + break; + case 16: + buswidth =3D DMA_SLAVE_BUSWIDTH_2_BYTES; + break; + case 24: + buswidth =3D DMA_SLAVE_BUSWIDTH_3_BYTES; + break; + case 32: + buswidth =3D DMA_SLAVE_BUSWIDTH_4_BYTES; + break; + default: + dev_err(dev, "invalid word width\n"); + return -EINVAL; + } + + memset(&slave_config, 0, sizeof(slave_config)); + if (dir =3D=3D IN) { + slave_config.direction =3D DMA_MEM_TO_DEV; + slave_config.dst_addr =3D dma_addr; + slave_config.dst_addr_width =3D buswidth; + slave_config.dst_maxburst =3D asrc->m2m_get_maxburst(IN, pair); + } else { + slave_config.direction =3D DMA_DEV_TO_MEM; + slave_config.src_addr =3D dma_addr; + slave_config.src_addr_width =3D buswidth; + slave_config.src_maxburst =3D asrc->m2m_get_maxburst(OUT, pair); + } + + ret =3D dmaengine_slave_config(chan, &slave_config); + if (ret) { + dev_err(dev, "failed to config dmaengine for %s task: %d\n", + DIR_STR(dir), ret); + return -EINVAL; + } + + max_period_size =3D rounddown(ASRC_M2M_PERIOD_SIZE, width * pair->channel= s / 8); + /* scatter gather mode */ + sg_len =3D buf_len / max_period_size; + if (buf_len % max_period_size) + sg_len +=3D 1; + + sg =3D kmalloc_array(sg_len, sizeof(*sg), GFP_KERNEL); + if (!sg) + return -ENOMEM; + + sg_init_table(sg, sg_len); + for (i =3D 0; i < (sg_len - 1); i++) { + sg_dma_address(&sg[i]) =3D buf_addr + i * max_period_size; + sg_dma_len(&sg[i]) =3D max_period_size; + } + sg_dma_address(&sg[i]) =3D buf_addr + i * max_period_size; + sg_dma_len(&sg[i]) =3D buf_len - i * max_period_size; + + pair->desc[dir] =3D dmaengine_prep_slave_sg(chan, sg, sg_len, + slave_config.direction, + DMA_PREP_INTERRUPT); + kfree(sg); + if (!pair->desc[dir]) { + dev_err(dev, "failed to prepare dmaengine for %s task\n", DIR_STR(dir)); + return -EINVAL; + } + + pair->desc[dir]->callback =3D ASRC_xPUT_DMA_CALLBACK(dir); + pair->desc[dir]->callback_param =3D pair; + + return 0; +} + +/* main function of converter */ +static void asrc_m2m_device_run(struct fsl_asrc_pair *pair, struct snd_com= pr_task_runtime *task) +{ + struct fsl_asrc *asrc =3D pair->asrc; + struct device *dev =3D &asrc->pdev->dev; + enum asrc_pair_index index =3D pair->index; + struct snd_dma_buffer *src_buf, *dst_buf; + unsigned int in_buf_len; + unsigned int out_dma_len; + unsigned int width; + u32 fifo_addr; + int ret; + + /* set ratio mod */ + if (asrc->m2m_set_ratio_mod) { + if (pair->ratio_mod_flag) { + asrc->m2m_set_ratio_mod(pair, pair->ratio_mod); + pair->ratio_mod_flag =3D false; + } + } + + src_buf =3D &pair->dma_buffer[IN]; + dst_buf =3D &pair->dma_buffer[OUT]; + + width =3D snd_pcm_format_physical_width(pair->sample_format[IN]); + fifo_addr =3D asrc->paddr + asrc->get_fifo_addr(IN, index); + + in_buf_len =3D task->input_size; + + if (in_buf_len < width * pair->channels / 8 || + in_buf_len > ASRC_M2M_BUFFER_SIZE || + in_buf_len % (width * pair->channels / 8)) { + dev_err(dev, "out buffer size is error: [%d]\n", in_buf_len); + goto end; + } + + /* dma config for output dma channel */ + ret =3D asrc_dmaconfig(pair, + pair->dma_chan[IN], + fifo_addr, + src_buf->addr, + in_buf_len, IN, width); + if (ret) { + dev_err(dev, "out dma config error\n"); + goto end; + } + + width =3D snd_pcm_format_physical_width(pair->sample_format[OUT]); + fifo_addr =3D asrc->paddr + asrc->get_fifo_addr(OUT, index); + out_dma_len =3D asrc->m2m_calc_out_len(pair, in_buf_len); + if (out_dma_len > 0 && out_dma_len <=3D ASRC_M2M_BUFFER_SIZE) { + /* dma config for capture dma channel */ + ret =3D asrc_dmaconfig(pair, + pair->dma_chan[OUT], + fifo_addr, + dst_buf->addr, + out_dma_len, OUT, width); + if (ret) { + dev_err(dev, "cap dma config error\n"); + goto end; + } + } else if (out_dma_len > ASRC_M2M_BUFFER_SIZE) { + dev_err(dev, "cap buffer size error\n"); + goto end; + } + + reinit_completion(&pair->complete[IN]); + reinit_completion(&pair->complete[OUT]); + + /* Submit DMA request */ + dmaengine_submit(pair->desc[IN]); + dma_async_issue_pending(pair->desc[IN]->chan); + if (out_dma_len > 0) { + dmaengine_submit(pair->desc[OUT]); + dma_async_issue_pending(pair->desc[OUT]->chan); + } + + asrc->m2m_start(pair); + + if (!wait_for_completion_interruptible_timeout(&pair->complete[IN], 10 * = HZ)) { + dev_err(dev, "out DMA task timeout\n"); + goto end; + } + + if (out_dma_len > 0) { + if (!wait_for_completion_interruptible_timeout(&pair->complete[OUT], 10 = * HZ)) { + dev_err(dev, "cap DMA task timeout\n"); + goto end; + } + } + + /* read the last words from FIFO */ + asrc_read_last_fifo(pair, dst_buf->area, &out_dma_len); + /* update payload length for capture */ + task->output_size =3D out_dma_len; +end: + return; +} + +static int fsl_asrc_m2m_comp_open(struct snd_compr_stream *stream) +{ + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct device *dev =3D &asrc->pdev->dev; + struct fsl_asrc_pair *pair; + int size, ret; + + pair =3D kzalloc(sizeof(*pair) + asrc->pair_priv_size, GFP_KERNEL); + if (!pair) + return -ENOMEM; + + pair->private =3D (void *)pair + sizeof(struct fsl_asrc_pair); + pair->asrc =3D asrc; + + init_completion(&pair->complete[IN]); + init_completion(&pair->complete[OUT]); + + runtime->private_data =3D pair; + + size =3D ASRC_M2M_BUFFER_SIZE; + ret =3D snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, &pair->dma_buf= fer[IN]); + if (ret) + goto error_alloc_in_buf; + + ret =3D snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, &pair->dma_buf= fer[OUT]); + if (ret) + goto error_alloc_out_buf; + + ret =3D pm_runtime_get_sync(dev); + if (ret < 0) { + dev_err(dev, "Failed to power up asrc\n"); + goto err_pm_runtime; + } + + return 0; + +err_pm_runtime: + snd_dma_free_pages(&pair->dma_buffer[OUT]); +error_alloc_out_buf: + snd_dma_free_pages(&pair->dma_buffer[IN]); +error_alloc_in_buf: + kfree(pair); + return ret; +} + +static int fsl_asrc_m2m_comp_release(struct snd_compr_stream *stream) +{ + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + struct device *dev =3D &asrc->pdev->dev; + + pm_runtime_put_sync(dev); + + snd_dma_free_pages(&pair->dma_buffer[IN]); + snd_dma_free_pages(&pair->dma_buffer[OUT]); + + kfree(runtime->private_data); + + return 0; +} + +static int fsl_asrc_m2m_comp_set_params(struct snd_compr_stream *stream, + struct snd_compr_params *params) +{ + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + struct fsl_asrc_m2m_cap cap; + int ret, i; + + ret =3D asrc->m2m_get_cap(&cap); + if (ret) + return -EINVAL; + + if (pcm_format_to_bits(params->codec.format) & cap.fmt_in) + pair->sample_format[IN] =3D params->codec.format; + else + return -EINVAL; + + if (pcm_format_to_bits(params->codec.pcm_format) & cap.fmt_out) + pair->sample_format[OUT] =3D params->codec.pcm_format; + else + return -EINVAL; + + /* check input rate is in scope */ + for (i =3D 0; i < cap.rate_in_count; i++) + if (params->codec.sample_rate =3D=3D cap.rate_in[i]) { + pair->rate[IN] =3D params->codec.sample_rate; + break; + } + if (i =3D=3D cap.rate_in_count) + return -EINVAL; + + /* check output rate is in scope */ + for (i =3D 0; i < cap.rate_out_count; i++) + if (params->codec.options.src_d.out_sample_rate =3D=3D cap.rate_out[i]) { + pair->rate[OUT] =3D params->codec.options.src_d.out_sample_rate; + break; + } + if (i =3D=3D cap.rate_out_count) + return -EINVAL; + + if (params->codec.ch_in !=3D params->codec.ch_out || + params->codec.ch_in < cap.chan_min || + params->codec.ch_in > cap.chan_max) + return -EINVAL; + + pair->channels =3D params->codec.ch_in; + pair->buf_len[IN] =3D params->buffer.fragment_size; + pair->buf_len[OUT] =3D params->buffer.fragment_size; + + return 0; +} + +static int fsl_asrc_m2m_mmap(struct dma_buf *dmabuf, struct vm_area_struct= *vma) +{ + struct snd_dma_buffer *dmab =3D dmabuf->priv; + + return snd_dma_buffer_mmap(dmab, vma); +} + +static struct sg_table *fsl_asrc_m2m_map_dma_buf(struct dma_buf_attachment= *attachment, + enum dma_data_direction direction) +{ + struct snd_dma_buffer *dmab =3D attachment->dmabuf->priv; + struct sg_table *sgt; + + sgt =3D kmalloc(sizeof(*sgt), GFP_KERNEL); + if (!sgt) + return NULL; + + if (dma_get_sgtable(attachment->dev, sgt, dmab->area, dmab->addr, dmab->b= ytes) < 0) + goto free; + + if (dma_map_sgtable(attachment->dev, sgt, direction, 0)) + goto free; + + return sgt; + +free: + sg_free_table(sgt); + kfree(sgt); + return NULL; +} + +static void fsl_asrc_m2m_unmap_dma_buf(struct dma_buf_attachment *attachme= nt, + struct sg_table *table, + enum dma_data_direction direction) +{ + dma_unmap_sgtable(attachment->dev, table, direction, 0); +} + +static void fsl_asrc_m2m_release(struct dma_buf *dmabuf) +{ + /* buffer is released by fsl_asrc_m2m_comp_release() */ +} + +static const struct dma_buf_ops fsl_asrc_m2m_dma_buf_ops =3D { + .mmap =3D fsl_asrc_m2m_mmap, + .map_dma_buf =3D fsl_asrc_m2m_map_dma_buf, + .unmap_dma_buf =3D fsl_asrc_m2m_unmap_dma_buf, + .release =3D fsl_asrc_m2m_release, +}; + +static int fsl_asrc_m2m_comp_task_create(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + DEFINE_DMA_BUF_EXPORT_INFO(exp_info_in); + DEFINE_DMA_BUF_EXPORT_INFO(exp_info_out); + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + struct device *dev =3D &asrc->pdev->dev; + int ret; + + exp_info_in.ops =3D &fsl_asrc_m2m_dma_buf_ops; + exp_info_in.size =3D ASRC_M2M_BUFFER_SIZE; + exp_info_in.flags =3D O_RDWR; + exp_info_in.priv =3D &pair->dma_buffer[IN]; + task->input =3D dma_buf_export(&exp_info_in); + if (IS_ERR(task->input)) { + ret =3D PTR_ERR(task->input); + return ret; + } + + exp_info_out.ops =3D &fsl_asrc_m2m_dma_buf_ops; + exp_info_out.size =3D ASRC_M2M_BUFFER_SIZE; + exp_info_out.flags =3D O_RDWR; + exp_info_out.priv =3D &pair->dma_buffer[OUT]; + task->output =3D dma_buf_export(&exp_info_out); + if (IS_ERR(task->output)) { + ret =3D PTR_ERR(task->output); + return ret; + } + + /* Request asrc pair/context */ + ret =3D asrc->request_pair(pair->channels, pair); + if (ret) { + dev_err(dev, "failed to request pair: %d\n", ret); + goto err_request_pair; + } + + ret =3D asrc->m2m_prepare(pair); + if (ret) { + dev_err(dev, "failed to start pair part one: %d\n", ret); + goto err_start_part_one; + } + + /* Request dma channels */ + pair->dma_chan[IN] =3D asrc->get_dma_channel(pair, IN); + if (!pair->dma_chan[IN]) { + dev_err(dev, "[ctx%d] failed to get input DMA channel\n", pair->index); + ret =3D -EBUSY; + goto err_dma_channel_in; + } + + pair->dma_chan[OUT] =3D asrc->get_dma_channel(pair, OUT); + if (!pair->dma_chan[OUT]) { + dev_err(dev, "[ctx%d] failed to get output DMA channel\n", pair->index); + ret =3D -EBUSY; + goto err_dma_channel_out; + } + + return 0; + +err_dma_channel_out: + dma_release_channel(pair->dma_chan[IN]); +err_dma_channel_in: + if (asrc->m2m_unprepare) + asrc->m2m_unprepare(pair); +err_start_part_one: + asrc->release_pair(pair); +err_request_pair: + return ret; +} + +static int fsl_asrc_m2m_comp_task_start(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + + asrc_m2m_device_run(pair, task); + + return 0; +} + +static int fsl_asrc_m2m_comp_task_stop(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + return 0; +} + +static int fsl_asrc_m2m_comp_task_free(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + + /* Stop & release pair/context */ + if (asrc->m2m_stop) + asrc->m2m_stop(pair); + + if (asrc->m2m_unprepare) + asrc->m2m_unprepare(pair); + asrc->release_pair(pair); + + /* Release dma channel */ + if (pair->dma_chan[IN]) + dma_release_channel(pair->dma_chan[IN]); + if (pair->dma_chan[OUT]) + dma_release_channel(pair->dma_chan[OUT]); + + return 0; +} + +static int fsl_asrc_m2m_get_caps(struct snd_compr_stream *cstream, + struct snd_compr_caps *caps) +{ + caps->num_codecs =3D 1; + caps->min_fragment_size =3D 4096; + caps->max_fragment_size =3D 4096; + caps->min_fragments =3D 1; + caps->max_fragments =3D 1; + caps->codecs[0] =3D SND_AUDIOCODEC_PCM; + + return 0; +} + +static int fsl_asrc_m2m_fill_codec_caps(struct fsl_asrc *asrc, + struct snd_compr_codec_caps *codec) +{ + struct fsl_asrc_m2m_cap cap; + snd_pcm_format_t k; + int j =3D 0; + int ret; + + ret =3D asrc->m2m_get_cap(&cap); + if (ret) + return -EINVAL; + + pcm_for_each_format(k) { + if (pcm_format_to_bits(k) & cap.fmt_in) { + codec->descriptor[j].max_ch =3D cap.chan_max; + memcpy(codec->descriptor[j].sample_rates, + cap.rate_in, + cap.rate_in_count * sizeof(__u32)); + codec->descriptor[j].num_sample_rates =3D cap.rate_in_count; + codec->descriptor[j].formats =3D k; + codec->descriptor[j].pcm_formats =3D cap.fmt_out; + codec->descriptor[j].src.out_sample_rate_min =3D cap.rate_out[0]; + codec->descriptor[j].src.out_sample_rate_max =3D + cap.rate_out[cap.rate_out_count - 1]; + j++; + } + } + + codec->codec =3D SND_AUDIOCODEC_PCM; + codec->num_descriptors =3D j; + return 0; +} + +static int fsl_asrc_m2m_get_codec_caps(struct snd_compr_stream *stream, + struct snd_compr_codec_caps *codec) +{ + struct fsl_asrc *asrc =3D stream->private_data; + + return fsl_asrc_m2m_fill_codec_caps(asrc, codec); +} + +static struct snd_compr_ops fsl_asrc_m2m_compr_ops =3D { + .open =3D fsl_asrc_m2m_comp_open, + .free =3D fsl_asrc_m2m_comp_release, + .set_params =3D fsl_asrc_m2m_comp_set_params, + .get_caps =3D fsl_asrc_m2m_get_caps, + .get_codec_caps =3D fsl_asrc_m2m_get_codec_caps, + .task_create =3D fsl_asrc_m2m_comp_task_create, + .task_start =3D fsl_asrc_m2m_comp_task_start, + .task_stop =3D fsl_asrc_m2m_comp_task_stop, + .task_free =3D fsl_asrc_m2m_comp_task_free, +}; + +int fsl_asrc_m2m_suspend(struct fsl_asrc *asrc) +{ + struct fsl_asrc_pair *pair; + int i; + + for (i =3D 0; i < PAIR_CTX_NUM; i++) { + pair =3D asrc->pair[i]; + if (!pair) + continue; + if (!completion_done(&pair->complete[IN])) { + if (pair->dma_chan[IN]) + dmaengine_terminate_all(pair->dma_chan[IN]); + asrc_input_dma_callback((void *)pair); + } + if (!completion_done(&pair->complete[OUT])) { + if (pair->dma_chan[OUT]) + dmaengine_terminate_all(pair->dma_chan[OUT]); + asrc_output_dma_callback((void *)pair); + } + + if (asrc->m2m_pair_suspend) + asrc->m2m_pair_suspend(pair); + } + + return 0; +} +EXPORT_SYMBOL_GPL(fsl_asrc_m2m_suspend); + +int fsl_asrc_m2m_resume(struct fsl_asrc *asrc) +{ + struct fsl_asrc_pair *pair; + int i; + + for (i =3D 0; i < PAIR_CTX_NUM; i++) { + pair =3D asrc->pair[i]; + if (!pair) + continue; + if (asrc->m2m_pair_resume) + asrc->m2m_pair_resume(pair); + } + + return 0; +} +EXPORT_SYMBOL_GPL(fsl_asrc_m2m_resume); + +int fsl_asrc_m2m_init(struct fsl_asrc *asrc) +{ + struct device *dev =3D &asrc->pdev->dev; + struct snd_card *card; + struct snd_compr *compr; + int ret; + + ret =3D snd_card_new(dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, + THIS_MODULE, 0, &card); + if (ret < 0) + return ret; + + strscpy(card->driver, "fsl-asrc-m2m", sizeof(card->driver)); + strscpy(card->shortname, "ASRC-M2M", sizeof(card->shortname)); + strscpy(card->longname, "ASRC-M2M", sizeof(card->shortname)); + + asrc->card =3D card; + + compr =3D devm_kzalloc(dev, sizeof(*compr), GFP_KERNEL); + if (!compr) { + ret =3D -ENOMEM; + goto err; + } + + compr->ops =3D &fsl_asrc_m2m_compr_ops; + compr->private_data =3D asrc; + + ret =3D snd_compress_new(card, 0, SND_COMPRESS_ACCEL, "ASRC M2M", compr); + if (ret < 0) + goto err; + + ret =3D snd_card_register(card); + if (ret < 0) + goto err; + + return 0; +err: + snd_card_free(card); + return ret; +} +EXPORT_SYMBOL_GPL(fsl_asrc_m2m_init); + +void fsl_asrc_m2m_exit(struct fsl_asrc *asrc) +{ + struct snd_card *card =3D asrc->card; + + snd_card_free(card); +} +EXPORT_SYMBOL_GPL(fsl_asrc_m2m_exit); + +MODULE_IMPORT_NS(DMA_BUF); +MODULE_AUTHOR("Shengjiu Wang "); +MODULE_DESCRIPTION("Freescale ASRC M2M driver"); +MODULE_LICENSE("GPL"); --=20 2.34.1 From nobody Fri Nov 29 03:39:53 2024 Received: from inva021.nxp.com (inva021.nxp.com [92.121.34.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB93C158861; Wed, 25 Sep 2024 07:17:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=92.121.34.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248679; cv=none; b=iNV8PubWIv7Lq/ifqyIlUigeeLMA0kVlgwrBK30UVLQW+wSsdUpVyBlyviBVq7kock0Ov3A1K+8H+Uj7VEaafBblgD1pMUbCD01zGtOFjB0LTJlW5TaQeFBVisJcTK0XQ8zIDf6Si9TzqgV7ZfzEBhxMw/yg/QkMKZuyv3YmGYg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248679; c=relaxed/simple; bh=xaAIhX1TtREmjaCVFwOnH37FD+glohLokg7cZt7/iCM=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=VGwEgY6nwFlmMs+s6JykUMP8h3KDmNy34LB5jEanyCA9/Ss+LE5bHJAgxzu/QdtGsB9Xmyg00Za43qtYxUW1aC9HtTwGNyUGiE2QWMdiEcVFEtCZzqlqFj2sEaX8fcJPgsNwt/lroAOsVcnKHifJj6WvJZcmEXm7fc9uNQTI5DA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; arc=none smtp.client-ip=92.121.34.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 2F9F4202A32; Wed, 25 Sep 2024 09:17:56 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id F18642016AA; Wed, 25 Sep 2024 09:17:55 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id 4D83C183AD44; Wed, 25 Sep 2024 15:17:54 +0800 (+08) From: Shengjiu Wang To: vkoul@kernel.org, perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, shengjiu.wang@gmail.com, Xiubo.Lee@gmail.com, festevam@gmail.com, nicoleotsuka@gmail.com, lgirdwood@gmail.com, broonie@kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 6/7] ASoC: fsl_asrc: register m2m platform device Date: Wed, 25 Sep 2024 14:55:15 +0800 Message-Id: <1727247316-16156-7-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> References: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Register m2m platform device, that user can use M2M feature. Signed-off-by: Shengjiu Wang --- sound/soc/fsl/fsl_asrc.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/sound/soc/fsl/fsl_asrc.c b/sound/soc/fsl/fsl_asrc.c index b1b35954f7ac..b1d13774e2a7 100644 --- a/sound/soc/fsl/fsl_asrc.c +++ b/sound/soc/fsl/fsl_asrc.c @@ -1384,6 +1384,12 @@ static int fsl_asrc_probe(struct platform_device *pd= ev) goto err_pm_get_sync; } =20 + ret =3D fsl_asrc_m2m_init(asrc); + if (ret) { + dev_err(&pdev->dev, "failed to init m2m device %d\n", ret); + return ret; + } + return 0; =20 err_pm_get_sync: @@ -1396,6 +1402,10 @@ static int fsl_asrc_probe(struct platform_device *pd= ev) =20 static void fsl_asrc_remove(struct platform_device *pdev) { + struct fsl_asrc *asrc =3D dev_get_drvdata(&pdev->dev); + + fsl_asrc_m2m_exit(asrc); + pm_runtime_disable(&pdev->dev); if (!pm_runtime_status_suspended(&pdev->dev)) fsl_asrc_runtime_suspend(&pdev->dev); @@ -1497,10 +1507,29 @@ static int fsl_asrc_runtime_suspend(struct device *= dev) return 0; } =20 +static int fsl_asrc_suspend(struct device *dev) +{ + struct fsl_asrc *asrc =3D dev_get_drvdata(dev); + int ret; + + fsl_asrc_m2m_suspend(asrc); + ret =3D pm_runtime_force_suspend(dev); + return ret; +} + +static int fsl_asrc_resume(struct device *dev) +{ + struct fsl_asrc *asrc =3D dev_get_drvdata(dev); + int ret; + + ret =3D pm_runtime_force_resume(dev); + fsl_asrc_m2m_resume(asrc); + return ret; +} + static const struct dev_pm_ops fsl_asrc_pm =3D { - SET_RUNTIME_PM_OPS(fsl_asrc_runtime_suspend, fsl_asrc_runtime_resume, NUL= L) - SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, - pm_runtime_force_resume) + RUNTIME_PM_OPS(fsl_asrc_runtime_suspend, fsl_asrc_runtime_resume, NULL) + SYSTEM_SLEEP_PM_OPS(fsl_asrc_suspend, fsl_asrc_resume) }; =20 static const struct fsl_asrc_soc_data fsl_asrc_imx35_data =3D { @@ -1538,7 +1567,7 @@ static struct platform_driver fsl_asrc_driver =3D { .driver =3D { .name =3D "fsl-asrc", .of_match_table =3D fsl_asrc_ids, - .pm =3D &fsl_asrc_pm, + .pm =3D pm_ptr(&fsl_asrc_pm), }, }; module_platform_driver(fsl_asrc_driver); --=20 2.34.1 From nobody Fri Nov 29 03:39:53 2024 Received: from inva021.nxp.com (inva021.nxp.com [92.121.34.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE19F158A3C; Wed, 25 Sep 2024 07:17:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=92.121.34.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248680; cv=none; b=XGxq/EbmCUru1tK0xUh1mcA6YGL2JISdyX51kViSKArd8qn+1kmuLOtfcMoGRF2U4fyc0iyARrFtUwpDhh3xaIn4rqJo04IKEU3kXrxEOYmFq086juytWLlujOR2xBPNRFrXWXiVgu65HTYKDFAWc7JSpUPwj+8cmoyhNfrOPlk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248680; c=relaxed/simple; bh=L7OfXrumACimbtfyuTi7+/5rFVVmzl5jvuqinIuM8TI=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=ciEuHTLcWgtRHASDnnQEqpSe4Tc6KLtJxuvJSFtc8He2zwucTrzD73jZUA/zJAICHyAaHQxnFhvt0ZP3AakwfHF9pW4Lfw0JzRvjuGJhf5Tgqt3jpe4Gh9HC+4JWQNwki9fRIG5QAX5Qo6EkcAK4CkAWrgtZlO+2VzaZelB3TCM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; arc=none smtp.client-ip=92.121.34.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 5B9E4202A0C; Wed, 25 Sep 2024 09:17:57 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 2B885202A0A; Wed, 25 Sep 2024 09:17:57 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id 7B1E4183AD47; Wed, 25 Sep 2024 15:17:55 +0800 (+08) From: Shengjiu Wang To: vkoul@kernel.org, perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, shengjiu.wang@gmail.com, Xiubo.Lee@gmail.com, festevam@gmail.com, nicoleotsuka@gmail.com, lgirdwood@gmail.com, broonie@kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 7/7] ASoC: fsl_easrc: register m2m platform device Date: Wed, 25 Sep 2024 14:55:16 +0800 Message-Id: <1727247316-16156-8-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> References: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Register m2m platform device,that user can use M2M feature. Signed-off-by: Shengjiu Wang --- sound/soc/fsl/fsl_easrc.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/sound/soc/fsl/fsl_easrc.c b/sound/soc/fsl/fsl_easrc.c index a1a7a90ff5ef..25d1c103df32 100644 --- a/sound/soc/fsl/fsl_easrc.c +++ b/sound/soc/fsl/fsl_easrc.c @@ -2204,6 +2204,12 @@ static int fsl_easrc_probe(struct platform_device *p= dev) goto err_pm_disable; } =20 + ret =3D fsl_asrc_m2m_init(easrc); + if (ret) { + dev_err(&pdev->dev, "failed to init m2m device %d\n", ret); + return ret; + } + return 0; =20 err_pm_disable: @@ -2213,6 +2219,10 @@ static int fsl_easrc_probe(struct platform_device *p= dev) =20 static void fsl_easrc_remove(struct platform_device *pdev) { + struct fsl_asrc *easrc =3D dev_get_drvdata(&pdev->dev); + + fsl_asrc_m2m_exit(easrc); + pm_runtime_disable(&pdev->dev); } =20 @@ -2313,10 +2323,29 @@ static int fsl_easrc_runtime_resume(struct device *= dev) return ret; } =20 +static int fsl_easrc_suspend(struct device *dev) +{ + struct fsl_asrc *easrc =3D dev_get_drvdata(dev); + int ret; + + fsl_asrc_m2m_suspend(easrc); + ret =3D pm_runtime_force_suspend(dev); + return ret; +} + +static int fsl_easrc_resume(struct device *dev) +{ + struct fsl_asrc *easrc =3D dev_get_drvdata(dev); + int ret; + + ret =3D pm_runtime_force_resume(dev); + fsl_asrc_m2m_resume(easrc); + return ret; +} + static const struct dev_pm_ops fsl_easrc_pm_ops =3D { RUNTIME_PM_OPS(fsl_easrc_runtime_suspend, fsl_easrc_runtime_resume, NULL) - SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, - pm_runtime_force_resume) + SYSTEM_SLEEP_PM_OPS(fsl_easrc_suspend, fsl_easrc_resume) }; =20 static struct platform_driver fsl_easrc_driver =3D { --=20 2.34.1