From 700197d49f7f61fd753fbc6bb36adb71d865e7fa Mon Sep 17 00:00:00 2001 From: Ranjani Sridharan Date: Mon, 11 Jun 2018 20:37:14 -0700 Subject: [PATCH] dma: Introduce new API for dma_get() This patch introduces a new API for allocating DMAC's by taking into account the dma dev type, copy direction, capabilities and access control requested by the user. It also updates also users to use the new API. It also consolidates the new API in a common file for all platforms. Signed-off-by: Ranjani Sridharan --- src/arch/xtensa/Makefile.am | 1 + src/audio/dai.c | 7 ++- src/audio/host.c | 13 ++--- src/host/common_test.c | 2 +- src/include/sof/dma.h | 6 ++- src/ipc/apl-ipc.c | 8 +++- src/ipc/byt-ipc.c | 9 ++-- src/ipc/cnl-ipc.c | 8 +++- src/ipc/dma-copy.c | 8 +++- src/ipc/hsw-ipc.c | 9 ++-- src/lib/Makefile.am | 11 ++++- src/lib/dma.c | 89 +++++++++++++++++++++++++++++++++++ src/platform/apollolake/dma.c | 12 ----- src/platform/baytrail/dma.c | 12 ----- src/platform/cannonlake/dma.c | 12 ----- src/platform/haswell/dma.c | 12 ----- 16 files changed, 150 insertions(+), 69 deletions(-) create mode 100644 src/lib/dma.c diff --git a/src/arch/xtensa/Makefile.am b/src/arch/xtensa/Makefile.am index 4ffe15f77..88dc8c667 100644 --- a/src/arch/xtensa/Makefile.am +++ b/src/arch/xtensa/Makefile.am @@ -65,6 +65,7 @@ sof_LDADD = \ ../../lib/libcore.a \ ../../platform/$(PLATFORM)/libplatform.a \ ../../ipc/libsof_ipc.a \ + ../../lib/libdma.a \ ../../audio/libaudio.a \ ../../drivers/libdrivers.a \ ../../math/libsof_math.a \ diff --git a/src/audio/dai.c b/src/audio/dai.c index 1eeecd26d..21a3b8029 100644 --- a/src/audio/dai.c +++ b/src/audio/dai.c @@ -176,6 +176,7 @@ static struct comp_dev *dai_new(struct sof_ipc_comp *comp) struct sof_ipc_comp_dai *dai; struct sof_ipc_comp_dai *ipc_dai = (struct sof_ipc_comp_dai *)comp; struct dai_data *dd; + uint32_t dir, caps, dma_dev; trace_dai("new"); @@ -201,7 +202,11 @@ static struct comp_dev *dai_new(struct sof_ipc_comp *comp) goto error; } - dd->dma = dma_get(ipc_dai->dmac_id); + /* request GP LP DMA with shared access privilege */ + dir = DMA_DIR_MEM_TO_DEV | DMA_DIR_DEV_TO_MEM; + caps = DMA_CAP_GP_LP | DMA_CAP_GP_HP; + dma_dev = DMA_DEV_SSP | DMA_DEV_DMIC; + dd->dma = dma_get(dir, caps, dma_dev, DMA_ACCESS_SHARED); if (dd->dma == NULL) { trace_dai_error("eDd"); goto error; diff --git a/src/audio/host.c b/src/audio/host.c index 0285e4a0a..e374a2fa0 100644 --- a/src/audio/host.c +++ b/src/audio/host.c @@ -503,6 +503,7 @@ static struct comp_dev *host_new(struct sof_ipc_comp *comp) struct sof_ipc_comp_host *host; struct sof_ipc_comp_host *ipc_host = (struct sof_ipc_comp_host *)comp; struct dma_sg_elem *elem; + uint32_t dir, caps, dma_dev; trace_host("new"); @@ -529,15 +530,15 @@ static struct comp_dev *host_new(struct sof_ipc_comp *comp) comp_set_drvdata(dev, hd); -#if !defined CONFIG_DMA_GW - hd->dma = dma_get(ipc_host->dmac_id); -#else + /* request HDA DMA with shared access privilege */ if (ipc_host->direction == SOF_IPC_STREAM_PLAYBACK) - hd->dma = dma_get(DMA_HOST_OUT_DMAC); + dir = DMA_DIR_HMEM_TO_LMEM; else - hd->dma = dma_get(DMA_HOST_IN_DMAC); -#endif + dir = DMA_DIR_LMEM_TO_HMEM; + caps = 0; + dma_dev = DMA_DEV_HDA; + hd->dma = dma_get(dir, caps, dma_dev, DMA_ACCESS_SHARED); if (hd->dma == NULL) { trace_host_error("eDM"); goto error; diff --git a/src/host/common_test.c b/src/host/common_test.c index e28688330..0851085ea 100644 --- a/src/host/common_test.c +++ b/src/host/common_test.c @@ -200,7 +200,7 @@ struct dai *dai_get(uint32_t type, uint32_t index) return NULL; } -struct dma *dma_get(int dmac_id) +struct dma *dma_get(uint32_t dir, uint32_t caps, uint32_t dev, uint32_t flags) { return NULL; } diff --git a/src/include/sof/dma.h b/src/include/sof/dma.h index ae39bd876..77da305f4 100644 --- a/src/include/sof/dma.h +++ b/src/include/sof/dma.h @@ -56,6 +56,10 @@ #define DMA_DEV_SSP (1 << 1) #define DMA_DEV_DMIC (1 << 2) +/* DMA access privilege flag */ +#define DMA_ACCESS_EXCLUSIVE 1 +#define DMA_ACCESS_SHARED 0 + /* DMA IRQ types */ #define DMA_IRQ_TYPE_BLOCK (1 << 0) #define DMA_IRQ_TYPE_LLIST (1 << 1) @@ -150,7 +154,7 @@ struct dma_int { uint32_t irq; }; -struct dma *dma_get(int dmac_id); +struct dma *dma_get(uint32_t dir, uint32_t caps, uint32_t dev, uint32_t flags); /* initialize all platform DMAC's */ int dmac_init(void); diff --git a/src/ipc/apl-ipc.c b/src/ipc/apl-ipc.c index 78291d03b..ca1975299 100644 --- a/src/ipc/apl-ipc.c +++ b/src/ipc/apl-ipc.c @@ -177,6 +177,7 @@ out: int platform_ipc_init(struct ipc *ipc) { struct intel_ipc_data *iipc; + uint32_t dir, caps, dev; int i; _ipc = ipc; @@ -198,8 +199,11 @@ int platform_ipc_init(struct ipc *ipc) if (iipc->page_table) bzero(iipc->page_table, HOST_PAGE_SIZE); - /* dma */ - iipc->dmac = dma_get(DMA_GP_LP_DMAC0); + /* request GP DMA with shared access privilege */ + caps = 0; + dir = DMA_DIR_HMEM_TO_LMEM; + dev = DMA_DEV_HDA; + iipc->dmac = dma_get(dir, caps, dev, DMA_ACCESS_SHARED); /* PM */ iipc->pm_prepare_D3 = 0; diff --git a/src/ipc/byt-ipc.c b/src/ipc/byt-ipc.c index 87e7949bd..fe2be6bc3 100644 --- a/src/ipc/byt-ipc.c +++ b/src/ipc/byt-ipc.c @@ -202,7 +202,7 @@ out: int platform_ipc_init(struct ipc *ipc) { struct intel_ipc_data *iipc; - uint32_t imrd; + uint32_t imrd, dir, caps, dev; int i; _ipc = ipc; @@ -225,8 +225,11 @@ int platform_ipc_init(struct ipc *ipc) if (iipc->page_table) bzero(iipc->page_table, PLATFORM_PAGE_TABLE_SIZE); - /* dma */ - iipc->dmac = dma_get(DMA_ID_DMAC0); + /* request HDA DMA with shared access privilege */ + caps = 0; + dir = DMA_DIR_HMEM_TO_LMEM; + dev = DMA_DEV_HDA; + iipc->dmac = dma_get(dir, caps, dev, DMA_ACCESS_SHARED); /* PM */ iipc->pm_prepare_D3 = 0; diff --git a/src/ipc/cnl-ipc.c b/src/ipc/cnl-ipc.c index ba651643a..e5fed55f7 100644 --- a/src/ipc/cnl-ipc.c +++ b/src/ipc/cnl-ipc.c @@ -177,6 +177,7 @@ out: int platform_ipc_init(struct ipc *ipc) { struct intel_ipc_data *iipc; + uint32_t dir, caps, dev; int i; _ipc = ipc; @@ -198,8 +199,11 @@ int platform_ipc_init(struct ipc *ipc) if (iipc->page_table) bzero(iipc->page_table, HOST_PAGE_SIZE); - /* dma */ - iipc->dmac = dma_get(DMA_GP_LP_DMAC0); + /* request GP DMA with shared access privilege */ + caps = 0; + dir = DMA_DIR_HMEM_TO_LMEM; + dev = DMA_DEV_HDA; + iipc->dmac = dma_get(dir, caps, dev, DMA_ACCESS_SHARED); /* PM */ iipc->pm_prepare_D3 = 0; diff --git a/src/ipc/dma-copy.c b/src/ipc/dma-copy.c index be72eb8c4..81388b94f 100644 --- a/src/ipc/dma-copy.c +++ b/src/ipc/dma-copy.c @@ -375,7 +375,13 @@ int dma_copy_from_host_nowait(struct dma_copy *dc, struct dma_sg_config *host_sg int dma_copy_new(struct dma_copy *dc, int dmac) { - dc->dmac = dma_get(dmac); + uint32_t dir, cap, dev; + + /* request HDA DMA in the dir LMEM->HMEM with shared access */ + dir = DMA_DIR_LMEM_TO_HMEM; + dev = DMA_DEV_HDA; + cap = 0; + dc->dmac = dma_get(dir, cap, dev, DMA_ACCESS_SHARED); if (dc->dmac == NULL) { trace_dma_error("ec0"); return -ENODEV; diff --git a/src/ipc/hsw-ipc.c b/src/ipc/hsw-ipc.c index aa4d9e145..7b54032cd 100644 --- a/src/ipc/hsw-ipc.c +++ b/src/ipc/hsw-ipc.c @@ -198,7 +198,7 @@ out: int platform_ipc_init(struct ipc *ipc) { struct intel_ipc_data *iipc; - uint32_t imrd; + uint32_t imrd, dir, caps, dev; int i; _ipc = ipc; @@ -221,8 +221,11 @@ int platform_ipc_init(struct ipc *ipc) if (iipc->page_table) bzero(iipc->page_table, PLATFORM_PAGE_TABLE_SIZE); - /* dma */ - iipc->dmac = dma_get(DMA_ID_DMAC1); + /* request GP DMA with shared access privilege */ + caps = 0; + dir = DMA_DIR_HMEM_TO_LMEM; + dev = DMA_DEV_HDA; + iipc->dmac = dma_get(dir, caps, dev, DMA_ACCESS_SHARED); /* PM */ iipc->pm_prepare_D3 = 0; diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 825bfbdb5..874d2fb5d 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -1,4 +1,4 @@ -noinst_LIBRARIES = libcore.a +noinst_LIBRARIES = libcore.a libdma.a libcore_a_SOURCES = \ lib.c \ @@ -16,3 +16,12 @@ libcore_a_CFLAGS = \ $(ARCH_INCDIR) \ $(SOF_INCDIR) \ $(PLATFORM_INCDIR) + +libdma_a_SOURCES = \ + dma.c + +libdma_a_CFLAGS = \ + $(ARCH_CFLAGS) \ + $(ARCH_INCDIR) \ + $(SOF_INCDIR) \ + $(PLATFORM_INCDIR) diff --git a/src/lib/dma.c b/src/lib/dma.c new file mode 100644 index 000000000..29028f495 --- /dev/null +++ b/src/lib/dma.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Ranjani Sridharan + */ + +#include +#include +#include + +/* + * API to request a platform DMAC. + * Users can request DMAC based on dev type, copy direction, capabilities + * and access privilege. + * For exclusive access, ret DMAC with no channels draining. + * For shared access, ret DMAC with the least number of channels draining. + */ + +struct dma *dma_get(uint32_t dir, uint32_t cap, uint32_t dev, uint32_t flags) +{ + int i, ch_count; + int min_ch_count = INT32_MAX; + int dma_index = -1; + + for (i = 0; i < PLATFORM_NUM_DMACS; i++) { + + /* skip if this DMAC does not support the requested dir */ + if (dir && (dma[i].plat_data.dir & dir) == 0) + continue; + + /* skip if this DMAC does not support the requested caps */ + if (cap && (dma[i].plat_data.caps & cap) == 0) + continue; + + /* skip if this DMAC does not support the requested dev */ + if (dev && (dma[i].plat_data.devs & dev) == 0) + continue; + + /* if exclusive access is requested */ + if (flags & DMA_ACCESS_EXCLUSIVE) { + + /* ret DMA with no channels draining */ + if (!atomic_read(&dma[i].num_channels_busy)) + return &dma[i]; + } else { + + /* get number of channels draining in this DMAC*/ + ch_count = atomic_read(&dma[i].num_channels_busy); + + /* pick DMAC with the least num of channels draining */ + if (ch_count < min_ch_count) { + dma_index = i; + min_ch_count = ch_count; + } + } + } + + /* return DMAC */ + if (dma_index >= 0) { + tracev_value(dma[dma_index].plat_data.id); + return &dma[dma_index]; + } + + return NULL; +} diff --git a/src/platform/apollolake/dma.c b/src/platform/apollolake/dma.c index b0142596b..a591e8b64 100644 --- a/src/platform/apollolake/dma.c +++ b/src/platform/apollolake/dma.c @@ -190,18 +190,6 @@ struct dma dma[PLATFORM_NUM_DMACS] = { .ops = &hda_link_dma_ops, },}; -struct dma *dma_get(int dmac_id) -{ - int i; - - for (i = 0; i < ARRAY_SIZE(dma); i++) { - if (dma[i].plat_data.id == dmac_id) - return &dma[i]; - } - - return NULL; -} - /* Initialize all platform DMAC's */ int dmac_init(void) { diff --git a/src/platform/baytrail/dma.c b/src/platform/baytrail/dma.c index f82cc55c8..c3c7772d1 100644 --- a/src/platform/baytrail/dma.c +++ b/src/platform/baytrail/dma.c @@ -190,18 +190,6 @@ struct dma dma[PLATFORM_NUM_DMACS] = { #endif }; -struct dma *dma_get(int dmac_id) -{ - int i; - - for (i = 0; i < ARRAY_SIZE(dma); i++) { - if (dma[i].plat_data.id == dmac_id) - return &dma[i]; - } - - return NULL; -} - /* Initialize all platform DMAC's */ int dmac_init(void) { diff --git a/src/platform/cannonlake/dma.c b/src/platform/cannonlake/dma.c index e46ff07e4..afb87bca6 100644 --- a/src/platform/cannonlake/dma.c +++ b/src/platform/cannonlake/dma.c @@ -191,18 +191,6 @@ struct dma dma[PLATFORM_NUM_DMACS] = { .ops = &hda_link_dma_ops, },}; -struct dma *dma_get(int dmac_id) -{ - int i; - - for (i = 0; i < ARRAY_SIZE(dma); i++) { - if (dma[i].plat_data.id == dmac_id) - return &dma[i]; - } - - return NULL; -} - /* Initialize all platform DMAC's */ int dmac_init(void) { diff --git a/src/platform/haswell/dma.c b/src/platform/haswell/dma.c index 90c834c84..ca7d06b0c 100644 --- a/src/platform/haswell/dma.c +++ b/src/platform/haswell/dma.c @@ -132,18 +132,6 @@ struct dma dma[PLATFORM_NUM_DMACS] = { .ops = &dw_dma_ops, },}; -struct dma *dma_get(int dmac_id) -{ - switch (dmac_id) { - case DMA_ID_DMAC0: - return &dma[0]; - case DMA_ID_DMAC1: - return &dma[1]; - default: - return NULL; - } -} - /* Initialize all platform DMAC's */ int dmac_init(void) {