Skip to content

Commit a54de05

Browse files
author
Cyril Chao
committed
platform: mtk: wire native Zephyr DMA/DAI into build and platform init
Register sof_dma[] entries, allocate dma_chan_data[] arrays in platform_init(), add CONFIG_DMA_MTK_SOF_HOST_DMA Kconfig symbol, guard legacy-only code paths, and enable native drivers for MT8196. Signed-off-by: Cyril Chao <cyril.chao@mediatek.corp-partner.google.com>
1 parent 7f126eb commit a54de05

8 files changed

Lines changed: 115 additions & 6 deletions

File tree

app/boards/mt8196_mt8196_adsp.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
# config goes in prj.conf.
66
CONFIG_MTK=y
77
CONFIG_RIMAGE_SIGNING_SCHEMA="mt8196"
8+
CONFIG_ZEPHYR_NATIVE_DRIVERS=y
9+
CONFIG_DMA=y
10+
CONFIG_DAI=y

src/ipc/ipc3/host-page-table.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ static int ipc_get_page_descriptors(struct sof_dma *dmac, uint8_t *page_table,
9898
* reload() function which may not be the case for all
9999
* vendors.
100100
*/
101-
if (!IS_ENABLED(CONFIG_DMA_NXP_SOF_HOST_DMA)) {
101+
if (!IS_ENABLED(CONFIG_DMA_NXP_SOF_HOST_DMA) &&
102+
!IS_ENABLED(CONFIG_DMA_MTK_SOF_HOST_DMA)) {
102103
tr_err(&ipc_tr, "DMAC not supported for page transfer");
103104
return -ENOTSUP;
104105
}

src/platform/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ config MT8365
312312

313313
config MTK
314314
bool "Build for Mediatek (Zephyr)"
315-
select SCHEDULE_DMA_MULTI_CHANNEL
315+
select SCHEDULE_DMA_MULTI_CHANNEL if !ZEPHYR_NATIVE_DRIVERS
316316
select HOST_PTABLE
317317
help
318318
Select if your target is a Mediatek DSP. Builds Zephyr firmware.

src/platform/mtk/dai.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
// SPDX-License-Identifier: BSD-3-Clause
22
// Copyright(c) 2024 Google LLC. All rights reserved.
33
// Author: Andy Ross <andyross@google.com>
4+
#ifndef CONFIG_ZEPHYR_NATIVE_DRIVERS
45
#include <sof/lib/dai-legacy.h>
6+
#endif
57
#include <ipc/dai.h>
68
#include <sof/drivers/afe-drv.h>
9+
#include <stdbool.h>
10+
#include <errno.h>
11+
#include <zephyr/devicetree.h>
712

813
/* The legacy driver stores register addresses as an offset from an
914
* arbitrary base address (which is not actually a unified block of
@@ -158,6 +163,7 @@ static struct mtk_base_memif_data afe_memifs[] = {
158163
DT_FOREACH_STATUS_OKAY(mediatek_afe, EMPTY_STRUCT)
159164
};
160165

166+
#ifndef CONFIG_ZEPHYR_NATIVE_DRIVERS
161167
static struct dai mtk_dais[] = {
162168
DT_FOREACH_STATUS_OKAY(mediatek_afe, EMPTY_STRUCT)
163169
};
@@ -210,6 +216,7 @@ static const struct dai_info mtk_dai_info = {
210216
.dai_type_array = mtk_dai_types,
211217
.num_dai_types = ARRAY_SIZE(mtk_dai_types),
212218
};
219+
#endif /* !CONFIG_ZEPHYR_NATIVE_DRIVERS */
213220

214221
#if defined(CONFIG_SOC_SERIES_MT818X) || defined(CONFIG_SOC_MT8195)
215222
static unsigned int mtk_afe2adsp_addr(unsigned int addr)
@@ -324,7 +331,7 @@ struct mtk_base_afe_platform mtk_afe_platform = {
324331
.memif_32bit_supported = 0,
325332
.irq_datas = NULL,
326333
.irqs_size = 0,
327-
.dais_size = ARRAY_SIZE(mtk_dais),
334+
.dais_size = ARRAY_SIZE(afes),
328335
.afe_fs = mtk_afe_fs,
329336
.irq_fs = mtk_afe_fs_timing,
330337
#if defined(CONFIG_SOC_SERIES_MT818X) || defined(CONFIG_SOC_MT8195)
@@ -342,6 +349,7 @@ int mtk_dai_init(struct sof *sof)
342349
afe_memifs[i].id = i;
343350
cfg_convert(&afes[i], &afe_memifs[i]);
344351

352+
#ifndef CONFIG_ZEPHYR_NATIVE_DRIVERS
345353
/* Also initialize the dais array */
346354
extern const struct dai_driver afe_dai_driver;
347355

@@ -362,6 +370,7 @@ int mtk_dai_init(struct sof *sof)
362370
int hs = (i << 16) | di;
363371

364372
mtk_dais[di].plat_data.fifo[0].handshake = hs;
373+
#endif
365374
}
366375

367376
/* DTS stores the direction as a boolean property, but the
@@ -377,7 +386,9 @@ int mtk_dai_init(struct sof *sof)
377386
for (/**/; i < ARRAY_SIZE(afes); i++)
378387
__ASSERT_NO_MSG(!afes[i].downlink);
379388

389+
#ifndef CONFIG_ZEPHYR_NATIVE_DRIVERS
380390
sof->dai_info = &mtk_dai_info;
381391
sof->dma_info = &mtk_dma_info;
392+
#endif
382393
return 0;
383394
}

src/platform/mtk/platform.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,13 @@ struct ipc_data_host_buffer *ipc_platform_get_host_buffer(struct ipc *ipc)
157157
int platform_ipc_init(struct ipc *ipc)
158158
{
159159
mtk_host_buffer.page_table = hostbuf_ptable;
160+
#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS
161+
mtk_host_buffer.dmac = sof_dma_get(SOF_DMA_DIR_HMEM_TO_LMEM, 0,
162+
SOF_DMA_DEV_HOST, SOF_DMA_ACCESS_SHARED);
163+
#else
160164
mtk_host_buffer.dmac = dma_get(DMA_DIR_HMEM_TO_LMEM, 0, DMA_DEV_HOST,
161165
DMA_ACCESS_SHARED);
166+
#endif
162167

163168
schedule_task_init_edf(&ipc->ipc_task, SOF_UUID(zipc_task_uuid),
164169
&ipc_task_ops, ipc, 0, 0);
@@ -210,18 +215,63 @@ void clocks_init(struct sof *sof)
210215
sof->clocks = clks;
211216
}
212217

218+
#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS
219+
/*
220+
* Allocate SOF-level channel descriptor arrays (struct dma_chan_data[]) for
221+
* each sof_dma entry. Native Zephyr DMA drivers manage their own internal
222+
* channel state but do not allocate the SOF-layer chan[] array that
223+
* dai-zephyr.c and host-zephyr.c rely on (e.g. dd->chan = &dd->dma->chan[i]).
224+
* Legacy DMA drivers (dummy-dma, afe-memif) did this in their own probe();
225+
* in the native path we do it here after dmac_init() registers the DMA info.
226+
*/
227+
static int mtk_dma_chan_alloc(struct sof *sof)
228+
{
229+
int i, j;
230+
231+
for (i = 0; i < sof->dma_info->num_dmas; i++) {
232+
struct sof_dma *d = &sof->dma_info->dma_array[i];
233+
234+
if (!d->plat_data.channels || d->chan)
235+
continue;
236+
237+
d->chan = rzalloc(SOF_MEM_FLAG_KERNEL,
238+
d->plat_data.channels * sizeof(struct dma_chan_data));
239+
if (!d->chan)
240+
return -ENOMEM;
241+
242+
for (j = 0; j < d->plat_data.channels; j++) {
243+
d->chan[j].dma = d;
244+
d->chan[j].index = j;
245+
}
246+
}
247+
248+
return 0;
249+
}
250+
#endif
251+
213252
int platform_init(struct sof *sof)
214253
{
254+
int ret;
255+
215256
clocks_init(sof);
216257
scheduler_init_edf();
217258
sof->platform_timer_domain = zephyr_domain_init(PLATFORM_DEFAULT_CLOCK);
218259
scheduler_init_ll(sof->platform_timer_domain);
219260
mtk_dai_init(sof);
261+
#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS
262+
/* In native mode, DMA is registered in sof/zephyr/lib/dma.c */
263+
dmac_init(sof);
264+
ret = mtk_dma_chan_alloc(sof);
265+
if (ret < 0)
266+
return ret;
267+
#endif
220268
ipc_init(sof);
269+
#if CONFIG_SCHEDULE_DMA_MULTI_CHANNEL
221270
sof->platform_dma_domain =
222271
dma_multi_chan_domain_init(&sof->dma_info->dma_array[0],
223272
sof->dma_info->num_dmas,
224273
PLATFORM_DEFAULT_CLOCK, false);
274+
#endif
225275
sa_init(sof, CONFIG_SYSTICK_PERIOD);
226276
return 0;
227277
}

zephyr/CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,25 @@ if (CONFIG_SOC_FAMILY_MTK)
518518
zephyr_library_sources(
519519
${SOF_PLATFORM_PATH}/mtk/platform.c
520520
${SOF_PLATFORM_PATH}/mtk/dai.c
521-
${SOF_DRIVERS_PATH}/generic/dummy-dma.c
522-
${SOF_SRC_PATH}/drivers/mediatek/afe/afe-memif.c
523-
${SOF_SRC_PATH}/drivers/mediatek/afe/afe-dai.c
524521
${SOF_SRC_PATH}/drivers/mediatek/afe/afe-drv.c
525522
)
523+
524+
if (CONFIG_ZEPHYR_NATIVE_DRIVERS)
525+
# Native Zephyr DMA/DAI path
526+
zephyr_library_sources(
527+
lib/dma.c
528+
${SOF_SRC_PATH}/drivers/mediatek/afe/zephyr_mtk_afe_memif.c
529+
${SOF_SRC_PATH}/drivers/mediatek/afe/zephyr_mtk_host_dma.c
530+
${SOF_SRC_PATH}/drivers/mediatek/afe/zephyr_mtk_dai.c
531+
)
532+
else()
533+
# Legacy DMA path
534+
zephyr_library_sources(
535+
${SOF_DRIVERS_PATH}/generic/dummy-dma.c
536+
${SOF_SRC_PATH}/drivers/mediatek/afe/afe-memif.c
537+
${SOF_SRC_PATH}/drivers/mediatek/afe/afe-dai.c
538+
)
539+
endif()
526540
endif()
527541

528542
# Building for native_posix-based whole-OS host emulator

zephyr/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ config ZEPHYR_NATIVE_DRIVERS
169169
dai-zephyr
170170
will be used instead of legacy xtos version.
171171

172+
config DMA_MTK_SOF_HOST_DMA
173+
bool "MediaTek SOF host DMA driver"
174+
default y if SOC_FAMILY_MTK && ZEPHYR_NATIVE_DRIVERS
175+
help
176+
Software-based DMA driver for MTK SOF host component.
177+
MTK DSP can access host memory directly, so this driver
178+
implements DMA transfers using memcpy + cache management.
179+
172180
config DMA_DOMAIN
173181
bool "Enable the usage of DMA domain."
174182
help

zephyr/lib/dma.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,28 @@ SHARED_DATA struct sof_dma dma[] = {
239239
.z_dev = DEVICE_DT_GET(DT_NODELABEL(acp_sdw_dma)),
240240
},
241241
#endif
242+
#if DT_HAS_COMPAT_STATUS_OKAY(mediatek_afe_memif_dma)
243+
{
244+
.plat_data = {
245+
.dir = SOF_DMA_DIR_MEM_TO_DEV | SOF_DMA_DIR_DEV_TO_MEM,
246+
.devs = SOF_DMA_DEV_AFE_MEMIF,
247+
.channels = DT_PROP(DT_NODELABEL(afe_memif_dma), dma_channels),
248+
.period_count = 4,
249+
},
250+
.z_dev = DEVICE_DT_GET(DT_NODELABEL(afe_memif_dma)),
251+
},
252+
#endif
253+
#if DT_HAS_COMPAT_STATUS_OKAY(mediatek_sof_host_dma)
254+
{
255+
.plat_data = {
256+
.dir = SOF_DMA_DIR_HMEM_TO_LMEM | SOF_DMA_DIR_LMEM_TO_HMEM,
257+
.devs = SOF_DMA_DEV_HOST,
258+
.channels = DT_PROP(DT_NODELABEL(host_dma), dma_channels),
259+
.period_count = 4,
260+
},
261+
.z_dev = DEVICE_DT_GET(DT_NODELABEL(host_dma)),
262+
},
263+
#endif
242264
};
243265

244266
const struct dma_info lib_dma = {

0 commit comments

Comments
 (0)