From 59b16a6b81e2d553e90ef5df62963285d24b3c98 Mon Sep 17 00:00:00 2001 From: Stefan Ghetu <106436265+ursus161@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:25:45 +0200 Subject: [PATCH] probe: IPC3+Zephyr: extract to a host ring through the i.MX host DMA IPC3 PROBE_INIT carries no host buffer address: on Intel the kernel programs the destination into the HDA stream, so the firmware never needs one. On i.MX the host DMA (nxp_sof_host_dma) is configured by the firmware itself and was never given a destination, so the extraction channel had nowhere to deliver to and dma_reload() never moved any probe data. Have the kernel pass the bus address of a coherent host buffer in the PROBE_INIT stream_tag field. The buffer is a header page followed by a ring whose size is a power of two of at least the local probe ring. Before every transfer point the host DMA block at the bytes about to go out, at the offset given by the write counter, then let the common dma_reload() path copy them, exactly as on the other Zephyr-native platforms. The transfer size is already clamped at the end of the local ring and the host ring is a multiple of it, so the transfer never has to wrap in the host ring either. The host DMA reports no position, so after each transfer publish the write counter in the first word of the header page for the kernel to poll. Use channel 0 in probe_dma_init() under PROBE_HOST_RING, since stream_tag carries an address there, not a channel index; every other IPC3 platform (e.g. AMD ACP, MediaTek) keeps its previous channel selection unchanged. Also enable CONFIG_PROBE=y and CONFIG_PROBE_POINTS_MAX=16 for the i.MX8MP EVK ADSP board. Signed-off-by: Stefan Ghetu --- app/boards/imx8mp_evk_mimx8ml8_adsp.conf | 2 + src/kernel/probe/probe.c | 132 ++++++++++++++++++++--- 2 files changed, 120 insertions(+), 14 deletions(-) diff --git a/app/boards/imx8mp_evk_mimx8ml8_adsp.conf b/app/boards/imx8mp_evk_mimx8ml8_adsp.conf index da9e56d759a4..0f94141309e6 100644 --- a/app/boards/imx8mp_evk_mimx8ml8_adsp.conf +++ b/app/boards/imx8mp_evk_mimx8ml8_adsp.conf @@ -6,6 +6,8 @@ CONFIG_DMA=y CONFIG_DMA_NXP_SDMA=y CONFIG_DAI_NXP_MICFIL=y CONFIG_TRACE=n +CONFIG_PROBE=y +CONFIG_PROBE_POINTS_MAX=16 CONFIG_SHARED_INTERRUPTS=y CONFIG_ZEPHYR_NATIVE_DRIVERS=y CONFIG_SAI_HAS_MCLK_CONFIG_OPTION=y diff --git a/src/kernel/probe/probe.c b/src/kernel/probe/probe.c index 434317ffe093..aa7286bae495 100644 --- a/src/kernel/probe/probe.c +++ b/src/kernel/probe/probe.c @@ -51,8 +51,17 @@ LOG_MODULE_REGISTER(probe, CONFIG_SOF_LOG_LEVEL); #define PROBE_POINT_INVALID 0xFFFFFFFF #define PROBE_BUFFER_LOCAL_SIZE 8192 +/* host buffer layout: u32 write counter in the first bytes, ring data after the header */ +#define PROBE_HOST_HDR_SIZE 4096 #define DMA_ELEM_SIZE 32 +/* i.MX IPC3: the DSP writes extraction data straight into the host ring */ +#if CONFIG_IPC_MAJOR_3 && CONFIG_ZEPHYR_NATIVE_DRIVERS && CONFIG_IMX +#define PROBE_HOST_RING 1 +#else +#define PROBE_HOST_RING 0 +#endif + /** * DMA buffer */ @@ -74,6 +83,11 @@ struct probe_dma_ext { struct dma_sg_config config; /**< DMA SG config */ struct probe_dma_buf dmapb; /**< DMA buffer pointer */ struct dma_copy dc; /**< DMA copy */ +#if PROBE_HOST_RING + uintptr_t host_base; /**< host buffer: header + ring (IPC3) */ + uint32_t host_size; /**< host ring size in bytes (IPC3) */ + uint32_t host_wr; /**< total bytes written to host ring (IPC3) */ +#endif }; /** @@ -195,13 +209,18 @@ __cold static int probe_dma_init(struct probe_dma_ext *dma, uint32_t direction) { uint32_t addr_align; uint32_t channel; - struct dma_config dma_cfg; - struct dma_block_config dma_block_cfg; + struct dma_config dma_cfg = {0}; + struct dma_block_config dma_block_cfg = {0}; int err = 0; assert_can_be_cold(); +#if PROBE_HOST_RING + /* i.MX: stream_tag carries the host buffer address; use host DMA channel 0 */ + channel = 0; +#else channel = ((union ipc4_connector_node_id)dma->stream_tag).f.v_index; +#endif /* request DMA in the dir LMEM->HMEM with shared access */ dma->dc.dmac = sof_dma_get(direction, 0, SOF_DMA_DEV_HOST, @@ -259,6 +278,10 @@ __cold static int probe_dma_init(struct probe_dma_ext *dma, uint32_t direction) case SOF_DMA_DIR_LMEM_TO_HMEM: dma_cfg.channel_direction = MEMORY_TO_HOST; dma_block_cfg.source_address = (uint32_t)dma->dmapb.addr; +#if PROBE_HOST_RING + /* re-pointed before every transfer, see probe_host_reconfig() */ + dma_block_cfg.dest_address = (uint32_t)(dma->host_base + PROBE_HOST_HDR_SIZE); +#endif break; case SOF_DMA_DIR_HMEM_TO_LMEM: dma_cfg.channel_direction = HOST_TO_MEMORY; @@ -320,6 +343,58 @@ __cold static int probe_dma_deinit(struct probe_dma_ext *dma) return 0; } +#if PROBE_HOST_RING +/** + * \brief Point the i.MX host DMA at the bytes about to be transferred. + * + * The host DMA copies the block it was last configured with, so the block + * is updated before every transfer. The transfer never has to wrap in the + * host ring: probe_task() already clamps it at the end of the local ring, + * and the host ring is a multiple of the local ring, so every host ring + * boundary falls on a local one. + * + * \param[in] dma extraction DMA. + * \param[in] bytes size of the next transfer. + * \return 0 on success, error code otherwise. + */ +static int probe_host_reconfig(struct probe_dma_ext *dma, uint32_t bytes) +{ + uint32_t off = dma->host_wr % dma->host_size; + struct dma_block_config blk = { + .source_address = (uint32_t)dma->dmapb.r_ptr, + .dest_address = (uint32_t)(dma->host_base + PROBE_HOST_HDR_SIZE + off), + .block_size = bytes, + }; + struct dma_config cfg = { + .channel_direction = MEMORY_TO_HOST, + .source_data_size = sizeof(uint32_t), + .dest_data_size = sizeof(uint32_t), + .block_count = 1, + .head_block = &blk, + }; + + return dma_config(dma->dc.dmac->z_dev, dma->dc.chan->index, &cfg); +} + +/** + * \brief Publish the host ring write counter after a transfer. + * + * The data itself was already flushed by the host DMA, so the counter is + * only ever visible to the host once the bytes it covers are. + * + * \param[in] dma extraction DMA. + * \param[in] bytes size of the transfer just completed. + */ +static void probe_host_publish(struct probe_dma_ext *dma, uint32_t bytes) +{ + /* host_wr wraps at 2^32; kernel side casts its counter to u32 before subtracting */ + dma->host_wr += bytes; + *(volatile uint32_t *)dma->host_base = dma->host_wr; + dcache_writeback_region((__sparse_force void __sparse_cache *)dma->host_base, + sizeof(uint32_t)); +} +#endif + /* * \brief Probe task for extraction. * @@ -350,24 +425,34 @@ static enum task_state probe_task(void *data) if (avail + _probe->ext_dma.dmapb.r_ptr >= _probe->ext_dma.dmapb.end_addr) avail = _probe->ext_dma.dmapb.end_addr - _probe->ext_dma.dmapb.r_ptr; - if (avail > 0) + if (!avail) + return SOF_TASK_STATE_RESCHEDULE; + +#if PROBE_HOST_RING + err = probe_host_reconfig(&_probe->ext_dma, avail); + if (err < 0) { + tr_err(&pr_tr, "host dma reconfig failed."); + return SOF_TASK_STATE_COMPLETED; + } +#endif #if CONFIG_ZEPHYR_NATIVE_DRIVERS - err = dma_reload(_probe->ext_dma.dc.dmac->z_dev, - _probe->ext_dma.dc.chan->index, 0, 0, avail); + err = dma_reload(_probe->ext_dma.dc.dmac->z_dev, + _probe->ext_dma.dc.chan->index, 0, 0, avail); #else - err = dma_copy_to_host_nowait(&_probe->ext_dma.dc, - &_probe->ext_dma.config, 0, - (void *)_probe->ext_dma.dmapb.r_ptr, - avail); + err = dma_copy_to_host_nowait(&_probe->ext_dma.dc, + &_probe->ext_dma.config, 0, + (void *)_probe->ext_dma.dmapb.r_ptr, + avail); #endif - else - return SOF_TASK_STATE_RESCHEDULE; - if (err < 0) { tr_err(&pr_tr, "dma_copy_to_host() failed."); return err; } +#if PROBE_HOST_RING + probe_host_publish(&_probe->ext_dma, avail); +#endif + /* buffer data sent, set read pointer and clear avail bytes */ _probe->ext_dma.dmapb.r_ptr += avail; if (_probe->ext_dma.dmapb.r_ptr >= _probe->ext_dma.dmapb.end_addr) @@ -417,6 +502,19 @@ __cold int probe_init(const struct probe_dma *probe_dma) return -EINVAL; } +#if PROBE_HOST_RING + /* + * a power of two of at least the local ring size: a multiple of the + * local ring keeps transfers from wrapping in the host ring, a power + * of two keeps the write counter consistent across its 2^32 wrap + */ + if (probe_dma && (probe_dma->dma_buffer_size < PROBE_BUFFER_LOCAL_SIZE || + (probe_dma->dma_buffer_size & (probe_dma->dma_buffer_size - 1)))) { + tr_err(&pr_tr, "bad host ring size %u", probe_dma->dma_buffer_size); + return -EINVAL; + } +#endif + /* alloc probes main struct */ sof_get()->probe = rzalloc(SOF_MEM_FLAG_USER, sizeof(*_probe)); @@ -446,6 +544,12 @@ __cold int probe_init(const struct probe_dma *probe_dma) _probe->ext_dma.stream_tag = probe_dma->stream_tag; _probe->ext_dma.dma_buffer_size = probe_dma->dma_buffer_size; +#if PROBE_HOST_RING + /* i.MX: stream_tag holds the physical address of the host ring buffer */ + _probe->ext_dma.host_base = probe_dma->stream_tag; + _probe->ext_dma.host_size = probe_dma->dma_buffer_size; + _probe->ext_dma.host_wr = 0; +#endif err = probe_dma_init(&_probe->ext_dma, SOF_DMA_DIR_LMEM_TO_HMEM); if (err < 0) { @@ -1249,7 +1353,7 @@ __cold int probe_point_add(uint32_t count, const struct probe_point *probe) #elif CONFIG_AMD_ACP_HOST_DMA_PROBE dev = ipc_get_buffer_by_id(ipc_get(), buf_id->full_id); #else - dev = ipc_get_comp_by_id(ipc_get(), buf_id->full_id); + dev = ipc_get_comp_dev(ipc_get(), COMP_TYPE_ANY, buf_id->full_id); #endif /* check if buffer exists */ if (!dev) { @@ -1512,7 +1616,7 @@ __cold int probe_point_remove(uint32_t count, const uint32_t *buffer_id) #if CONFIG_AMD_ACP_HOST_DMA_PROBE dev = ipc_get_buffer_by_id(ipc_get(), buffer_id[i]); #else - dev = ipc_get_comp_by_id(ipc_get(), buffer_id[i]); + dev = ipc_get_comp_dev(ipc_get(), COMP_TYPE_ANY, buffer_id[i]); #endif if (dev) { struct comp_buffer *probe_buf =