Repository navigation
ASoC: sof: imx: add SOF probes extraction client (IPC3) #6013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ursus161
wants to merge
2
commits into
thesofproject:topic/sof-dev
Choose a base branch
from
ursus161:sof-probes-imx
base: topic/sof-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) | ||
| // | ||
| // Copyright 2026 NXP | ||
| // | ||
| // SOF probes client for i.MX (IPC3). The DSP cannot use a host DMA engine, so | ||
| // the host allocates a coherent buffer (header page + ring), gives its bus | ||
| // address to the firmware in PROBE_INIT (as stream_tag) and the firmware | ||
| // writes the extracted packets into the ring and publishes a byte counter in | ||
| // the first word of the header page. A delayed work copies new data into the | ||
| // compress buffer and wakes up the reader. | ||
|
|
||
| #include <linux/dma-mapping.h> | ||
| #include <linux/log2.h> | ||
| #include <linux/sizes.h> | ||
| #include <linux/module.h> | ||
| #include <linux/workqueue.h> | ||
| #include <sound/compress_driver.h> | ||
| #include <sound/soc.h> | ||
| #include "../sof-priv.h" | ||
| #include "../sof-client-probes.h" | ||
| #include "../sof-client.h" | ||
| #include "imx-common.h" | ||
|
|
||
| /* must match PROBE_HOST_HDR_SIZE in the firmware */ | ||
| #define IMX_PROBES_HDR_SIZE 4096 | ||
| #define IMX_PROBES_POLL_MS 10 | ||
|
|
||
| struct imx_probes_stream { | ||
| struct delayed_work work; | ||
| struct snd_compr_stream *cstream; | ||
| struct device *dev; | ||
| void *vaddr; | ||
| dma_addr_t paddr; | ||
| size_t ring_size; | ||
| u32 sampling_rate; | ||
| u64 dsp_wr; /* bytes written by the DSP (32-bit counter extended) */ | ||
| u64 dsp_rd; /* bytes consumed from the DSP ring */ | ||
| u64 produced; /* bytes copied to the compress buffer in this run */ | ||
| }; | ||
|
|
||
| /* only one probes extraction stream exists */ | ||
| static struct imx_probes_stream imx_stream; | ||
|
|
||
| static void imx_probes_sync_wr(struct imx_probes_stream *s) | ||
| { | ||
| /* the DSP counter is 32-bit and wraps */ | ||
| s->dsp_wr += (u32)(readl(s->vaddr) - (u32)s->dsp_wr); | ||
| } | ||
|
|
||
| static void imx_probes_work(struct work_struct *work) | ||
| { | ||
| struct imx_probes_stream *s = &imx_stream; | ||
| struct snd_compr_runtime *rtd; | ||
| u8 *ring = s->vaddr + IMX_PROBES_HDR_SIZE; | ||
| u64 pending, used; | ||
| u32 n, src, dst; | ||
|
|
||
| if (!s->cstream || !s->vaddr) | ||
| return; | ||
|
|
||
| rtd = s->cstream->runtime; | ||
| imx_probes_sync_wr(s); | ||
|
|
||
| pending = s->dsp_wr - s->dsp_rd; | ||
| if (pending > s->ring_size) { | ||
| /* the DSP overwrote data we did not read, skip to the oldest valid */ | ||
| dev_warn_ratelimited(s->dev, "probes ring overrun, dropped %llu bytes\n", | ||
| pending - s->ring_size); | ||
| s->dsp_rd = s->dsp_wr - s->ring_size; | ||
| pending = s->ring_size; | ||
| } | ||
|
|
||
| /* never overwrite data the reader has not consumed yet */ | ||
| used = s->produced - rtd->total_bytes_transferred; | ||
| n = min_t(u64, pending, rtd->buffer_size - used); | ||
| if (n) { | ||
| bool copied = false; | ||
|
|
||
| src = s->dsp_rd % s->ring_size; | ||
| dst = s->produced % rtd->buffer_size; | ||
| /* copy in chunks that do not wrap either buffer */ | ||
| while (n) { | ||
| u32 chunk = min3(n, (u32)(s->ring_size - src), | ||
| (u32)(rtd->buffer_size - dst)); | ||
|
|
||
| memcpy(rtd->dma_area + dst, ring + src, chunk); | ||
| s->dsp_rd += chunk; | ||
| s->produced += chunk; | ||
| src = (src + chunk) % s->ring_size; | ||
| dst = (dst + chunk) % rtd->buffer_size; | ||
| n -= chunk; | ||
| copied = true; | ||
| } | ||
| if (copied) | ||
| snd_compr_fragment_elapsed(s->cstream); | ||
| } | ||
|
|
||
| schedule_delayed_work(&s->work, msecs_to_jiffies(IMX_PROBES_POLL_MS)); | ||
| } | ||
|
|
||
| static int imx_probes_compr_startup(struct sof_client_dev *cdev, | ||
| struct snd_compr_stream *cstream, | ||
| struct snd_soc_dai *dai, u32 *stream_id) | ||
| { | ||
| struct imx_probes_stream *s = &imx_stream; | ||
|
|
||
| /* one stream only: do not touch the active stream's tag */ | ||
| if (s->cstream) | ||
| return -EBUSY; | ||
|
|
||
| s->cstream = cstream; | ||
| /* real value (buffer address) is set in set_params */ | ||
| *stream_id = 0; | ||
| return 0; | ||
|
ursus161 marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| static void imx_probes_free_buf(struct imx_probes_stream *s) | ||
| { | ||
| cancel_delayed_work_sync(&s->work); | ||
| if (s->vaddr) | ||
| dma_free_coherent(s->dev, IMX_PROBES_HDR_SIZE + s->ring_size, | ||
| s->vaddr, s->paddr); | ||
| s->vaddr = NULL; | ||
| } | ||
|
|
||
| static int imx_probes_compr_shutdown(struct sof_client_dev *cdev, | ||
| struct snd_compr_stream *cstream, | ||
| struct snd_soc_dai *dai) | ||
| { | ||
| struct imx_probes_stream *s = &imx_stream; | ||
|
|
||
| if (s->cstream != cstream) | ||
| return 0; | ||
|
|
||
| imx_probes_free_buf(s); | ||
| s->cstream = NULL; | ||
| return 0; | ||
| } | ||
|
|
||
| static int imx_probes_compr_set_params(struct sof_client_dev *cdev, | ||
| struct snd_compr_stream *cstream, | ||
| struct snd_compr_params *params, | ||
| struct snd_soc_dai *dai) | ||
| { | ||
| struct sof_probes_priv *priv = cdev->data; | ||
| struct imx_probes_stream *s = &imx_stream; | ||
| struct snd_soc_pcm_stream *pstream = &dai->driver->capture; | ||
|
|
||
| imx_probes_free_buf(s); | ||
|
|
||
| s->dev = sof_client_get_dma_dev(cdev); | ||
| /* DSP can only address 32-bit physical addresses */ | ||
| if (dma_set_coherent_mask(s->dev, DMA_BIT_MASK(32))) | ||
| return -ENOMEM; | ||
| /* firmware needs a power-of-two ring of at least 8 KiB */ | ||
| if (cstream->runtime->dma_bytes < SZ_8K || | ||
| !is_power_of_2(cstream->runtime->dma_bytes)) | ||
| return -EINVAL; | ||
| s->ring_size = cstream->runtime->dma_bytes; | ||
| s->vaddr = dma_alloc_coherent(s->dev, IMX_PROBES_HDR_SIZE + s->ring_size, | ||
| &s->paddr, GFP_KERNEL); | ||
| if (!s->vaddr) | ||
| return -ENOMEM; | ||
|
|
||
| s->dsp_wr = 0; | ||
| s->dsp_rd = 0; | ||
| s->produced = 0; | ||
| s->sampling_rate = snd_pcm_rate_bit_to_rate(pstream->rates); | ||
|
|
||
| /* the firmware takes the buffer address from the stream_tag field */ | ||
| priv->extractor_stream_tag = (u32)s->paddr; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int imx_probes_compr_trigger(struct sof_client_dev *cdev, | ||
| struct snd_compr_stream *cstream, | ||
| int cmd, struct snd_soc_dai *dai) | ||
| { | ||
| struct imx_probes_stream *s = &imx_stream; | ||
|
|
||
| switch (cmd) { | ||
| case SNDRV_PCM_TRIGGER_START: | ||
| /* a new run: the core restarts its counters, skip stale data */ | ||
| s->produced = 0; | ||
| if (s->vaddr) { | ||
| imx_probes_sync_wr(s); | ||
| s->dsp_rd = s->dsp_wr; | ||
| } | ||
| schedule_delayed_work(&s->work, msecs_to_jiffies(IMX_PROBES_POLL_MS)); | ||
| break; | ||
| case SNDRV_PCM_TRIGGER_STOP: | ||
| cancel_delayed_work_sync(&s->work); | ||
|
ursus161 marked this conversation as resolved.
Resolved
|
||
| s->produced = 0; | ||
| break; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int imx_probes_compr_pointer(struct sof_client_dev *cdev, | ||
| struct snd_compr_stream *cstream, | ||
| struct snd_compr_tstamp64 *tstamp, | ||
| struct snd_soc_dai *dai) | ||
| { | ||
| tstamp->copied_total = imx_stream.produced; | ||
| tstamp->sampling_rate = imx_stream.sampling_rate; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static const struct sof_probes_host_ops imx_probes_ops = { | ||
| .startup = imx_probes_compr_startup, | ||
| .shutdown = imx_probes_compr_shutdown, | ||
| .set_params = imx_probes_compr_set_params, | ||
| .trigger = imx_probes_compr_trigger, | ||
| .pointer = imx_probes_compr_pointer, | ||
| }; | ||
|
|
||
| int imx_probes_register(struct snd_sof_dev *sdev) | ||
| { | ||
| INIT_DELAYED_WORK(&imx_stream.work, imx_probes_work); | ||
|
|
||
| return sof_client_dev_register(sdev, "imx-probes", 0, &imx_probes_ops, | ||
| sizeof(imx_probes_ops)); | ||
| } | ||
|
|
||
| void imx_probes_unregister(struct snd_sof_dev *sdev) | ||
| { | ||
| sof_client_dev_unregister(sdev, "imx-probes", 0); | ||
| } | ||
|
|
||
| MODULE_IMPORT_NS("SND_SOC_SOF_CLIENT"); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here only the SPDX and copyright should use
//comments style. The rest should use C classic comments style/* ... */.