From 507c5b0b5391aac57eb8aa99edb9028f011457b3 Mon Sep 17 00:00:00 2001 From: Michael Rogenmoser Date: Mon, 31 Jan 2022 15:41:55 +0100 Subject: [PATCH 1/3] Fix dma when using `copy.merge` --- rtos/pmsis/pmsis_api/include/pmsis/cluster/dma/cl_dma.h | 2 +- rtos/pulpos/common/include/pos/implem/dma.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rtos/pmsis/pmsis_api/include/pmsis/cluster/dma/cl_dma.h b/rtos/pmsis/pmsis_api/include/pmsis/cluster/dma/cl_dma.h index ff9eb638..d5d812a7 100644 --- a/rtos/pmsis/pmsis_api/include/pmsis/cluster/dma/cl_dma.h +++ b/rtos/pmsis/pmsis_api/include/pmsis/cluster/dma/cl_dma.h @@ -189,9 +189,9 @@ static inline void pi_cl_dma_wait(void *copy); /// @cond IMPLEM #define CL_DMA_COMMON \ + uint32_t id; \ uint32_t ext; \ uint32_t loc; \ - uint32_t id; \ uint16_t size; \ pi_cl_dma_dir_e dir; \ uint8_t merge; diff --git a/rtos/pulpos/common/include/pos/implem/dma.h b/rtos/pulpos/common/include/pos/implem/dma.h index 60fc07c0..14e8d2c2 100644 --- a/rtos/pulpos/common/include/pos/implem/dma.h +++ b/rtos/pulpos/common/include/pos/implem/dma.h @@ -24,7 +24,7 @@ struct pi_cl_dma_cmd_s { - int id; + uint32_t id; struct pi_cl_dma_cmd_s *next; }; From e869249a24566b3b5c97ff8c178e78712ca8996a Mon Sep 17 00:00:00 2001 From: Michael Rogenmoser Date: Mon, 31 Jan 2022 19:20:02 +0100 Subject: [PATCH 2/3] make dma copy.id=-1 when calling free --- rtos/pulpos/common/include/pos/implem/dma.h | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/rtos/pulpos/common/include/pos/implem/dma.h b/rtos/pulpos/common/include/pos/implem/dma.h index 14e8d2c2..9c2daccf 100644 --- a/rtos/pulpos/common/include/pos/implem/dma.h +++ b/rtos/pulpos/common/include/pos/implem/dma.h @@ -44,6 +44,7 @@ static inline void __cl_dma_wait_safe(pi_cl_dma_cmd_t *copy) } plp_dma_counter_free(counter); + copy->id = -1; } @@ -60,6 +61,7 @@ static inline void __cl_dma_wait(pi_cl_dma_cmd_t *copy) } plp_dma_counter_free(counter); + copy->id = -1; eu_mutex_unlock_from_id(0); } @@ -69,7 +71,7 @@ static inline void __cl_dma_memcpy(unsigned int ext, unsigned int loc, unsigned { eu_mutex_lock_from_id(0); - int id = -1; + int id = copy->id; if (!merge) id = plp_dma_counter_alloc(); unsigned int cmd = plp_dma_getCmd(dir, size, PLP_DMA_1D, PLP_DMA_TRIG_EVT, PLP_DMA_NO_TRIG_IRQ, PLP_DMA_SHARED); // Prevent the compiler from pushing the transfer before all previous @@ -84,7 +86,7 @@ static inline void __cl_dma_memcpy(unsigned int ext, unsigned int loc, unsigned static inline void __cl_dma_memcpy_safe(unsigned int ext, unsigned int loc, unsigned short size, pi_cl_dma_dir_e dir, int merge, pi_cl_dma_cmd_t *copy) { - int id = -1; + int id = copy->id; if (!merge) id = plp_dma_counter_alloc(); unsigned int cmd = plp_dma_getCmd(dir, size, PLP_DMA_1D, PLP_DMA_TRIG_EVT, PLP_DMA_NO_TRIG_IRQ, PLP_DMA_SHARED); // Prevent the compiler from pushing the transfer before all previous @@ -131,17 +133,15 @@ static inline void __cl_dma_memcpy_2d(unsigned int ext, unsigned int loc, unsign { eu_mutex_lock_from_id(0); - int id = -1; + int id = copy->id; if (!merge) id = plp_dma_counter_alloc(); - { - unsigned int cmd = plp_dma_getCmd(dir, size, PLP_DMA_2D, PLP_DMA_TRIG_EVT, PLP_DMA_NO_TRIG_IRQ, PLP_DMA_SHARED); - // Prevent the compiler from pushing the transfer before all previous - // stores are done - __asm__ __volatile__ ("" : : : "memory"); - plp_dma_cmd_push_2d(cmd, loc, ext, stride, length); - if (!merge) copy->id = id; - } + unsigned int cmd = plp_dma_getCmd(dir, size, PLP_DMA_2D, PLP_DMA_TRIG_EVT, PLP_DMA_NO_TRIG_IRQ, PLP_DMA_SHARED); + // Prevent the compiler from pushing the transfer before all previous + // stores are done + __asm__ __volatile__ ("" : : : "memory"); + plp_dma_cmd_push_2d(cmd, loc, ext, stride, length); + if (!merge) copy->id = id; eu_mutex_unlock_from_id(0); } From 1a60100ef09209e26c24dd691dc731628bc138ca Mon Sep 17 00:00:00 2001 From: Michael Rogenmoser Date: Mon, 31 Jan 2022 19:42:25 +0100 Subject: [PATCH 3/3] Use direct link with safe function if ARCHI_HAS_DMA_DEMUX port --- rtos/pulpos/common/include/pos/implem/dma.h | 39 ++++++++++++++++++- .../include/archi/chips/pulp/properties.h | 2 + .../pulp_hal/include/hal/dma/mchan_v7.h | 13 +++++-- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/rtos/pulpos/common/include/pos/implem/dma.h b/rtos/pulpos/common/include/pos/implem/dma.h index 9c2daccf..2895f599 100644 --- a/rtos/pulpos/common/include/pos/implem/dma.h +++ b/rtos/pulpos/common/include/pos/implem/dma.h @@ -102,7 +102,7 @@ static inline void __cl_dma_memcpy_irq(unsigned int ext, unsigned int loc, unsig { eu_mutex_lock_from_id(0); - int id = -1; + int id = copy->id; if (!merge) id = plp_dma_counter_alloc(); unsigned int cmd = plp_dma_getCmd(dir, size, PLP_DMA_1D, PLP_DMA_NO_TRIG_EVT, PLP_DMA_TRIG_IRQ, PLP_DMA_SHARED); // Prevent the compiler from pushing the transfer before all previous @@ -146,16 +146,37 @@ static inline void __cl_dma_memcpy_2d(unsigned int ext, unsigned int loc, unsign eu_mutex_unlock_from_id(0); } +static inline void __cl_dma_memcpy_2d_safe(unsigned int ext, unsigned int loc, unsigned int size, unsigned int stride, unsigned short length, pi_cl_dma_dir_e dir, int merge, pi_cl_dma_cmd_t *copy) +{ + int id = copy->id; + if (!merge) id = plp_dma_counter_alloc(); + + unsigned int cmd = plp_dma_getCmd(dir, size, PLP_DMA_2D, PLP_DMA_TRIG_EVT, PLP_DMA_NO_TRIG_IRQ, PLP_DMA_SHARED); + // Prevent the compiler from pushing the transfer before all previous + // stores are done + __asm__ __volatile__ ("" : : : "memory"); + plp_dma_cmd_push_2d(cmd, loc, ext, stride, length); + if (!merge) copy->id = id; +} + static inline void pi_cl_dma_memcpy(pi_cl_dma_copy_t *copy) { +#if ARCHI_HAS_DMA_DEMUX + __cl_dma_memcpy_safe(copy->ext, copy->loc, copy->size, copy->dir, copy->merge, (pi_cl_dma_cmd_t *)copy); +#else __cl_dma_memcpy(copy->ext, copy->loc, copy->size, copy->dir, copy->merge, (pi_cl_dma_cmd_t *)copy); +#endif } static inline void pi_cl_dma_memcpy_2d(pi_cl_dma_copy_2d_t *copy) { +#if ARCHI_HAS_DMA_DEMUX + __cl_dma_memcpy_2d_safe(copy->ext, copy->loc, copy->size, copy->stride, copy->length, copy->dir, copy->merge, (pi_cl_dma_cmd_t *)copy); +#else __cl_dma_memcpy_2d(copy->ext, copy->loc, copy->size, copy->stride, copy->length, copy->dir, copy->merge, (pi_cl_dma_cmd_t *)copy); +#endif } @@ -167,13 +188,21 @@ static inline void pi_cl_dma_flush() static inline void pi_cl_dma_wait(void *copy) { +#if ARCHI_HAS_DMA_DEMUX + __cl_dma_wait_safe(copy); +#else __cl_dma_wait(copy); +#endif } static inline void pi_cl_dma_cmd(uint32_t ext, uint32_t loc, uint32_t size, pi_cl_dma_dir_e dir, pi_cl_dma_cmd_t *cmd) { +#if ARCHI_HAS_DMA_DEMUX + __cl_dma_memcpy_safe(ext, loc, size, dir, 0, (pi_cl_dma_cmd_t *)cmd); +#else __cl_dma_memcpy(ext, loc, size, dir, 0, (pi_cl_dma_cmd_t *)cmd); +#endif } @@ -185,13 +214,21 @@ static inline void pi_cl_dma_cmd_safe(uint32_t ext, uint32_t loc, uint32_t size, static inline void pi_cl_dma_cmd_2d(uint32_t ext, uint32_t loc, uint32_t size, uint32_t stride, uint32_t length, pi_cl_dma_dir_e dir, pi_cl_dma_cmd_t *cmd) { +#if ARCHI_HAS_DMA_DEMUX + __cl_dma_memcpy_2d_safe(ext, loc, size, stride, length, dir, 0, (pi_cl_dma_cmd_t *)cmd); +#else __cl_dma_memcpy_2d(ext, loc, size, stride, length, dir, 0, (pi_cl_dma_cmd_t *)cmd); +#endif } static inline void pi_cl_dma_cmd_wait(pi_cl_dma_cmd_t *cmd) { +#if ARCHI_HAS_DMA_DEMUX + __cl_dma_wait_safe((pi_cl_dma_cmd_t *)cmd); +#else __cl_dma_wait((pi_cl_dma_cmd_t *)cmd); +#endif } static inline void pi_cl_dma_cmd_wait_safe(pi_cl_dma_cmd_t *cmd) diff --git a/rtos/pulpos/pulp_archi/include/archi/chips/pulp/properties.h b/rtos/pulpos/pulp_archi/include/archi/chips/pulp/properties.h index 33414e62..84e89c17 100644 --- a/rtos/pulpos/pulp_archi/include/archi/chips/pulp/properties.h +++ b/rtos/pulpos/pulp_archi/include/archi/chips/pulp/properties.h @@ -85,6 +85,8 @@ #endif #define ARCHI_NB_CLUSTER 1 +#define ARCHI_HAS_DMA_DEMUX 1 + /* diff --git a/rtos/pulpos/pulp_hal/include/hal/dma/mchan_v7.h b/rtos/pulpos/pulp_hal/include/hal/dma/mchan_v7.h index db1e20dc..09ff4571 100644 --- a/rtos/pulpos/pulp_hal/include/hal/dma/mchan_v7.h +++ b/rtos/pulpos/pulp_hal/include/hal/dma/mchan_v7.h @@ -258,12 +258,17 @@ static inline unsigned int plp_dma_status(); /// @cond IMPLEM +#if ARCHI_HAS_DMA_DEMUX +#define DMA_ADDR ARCHI_MCHAN_DEMUX_ADDR +#else +#define DMA_ADDR ARCHI_MCHAN_EXT_ADDR +#endif #if defined(__riscv__) && !defined(RV_ISA_RV32) && !defined(__LLVM__) -#define DMA_WRITE(value, offset) __builtin_pulp_OffsetedWrite((value), (int *)ARCHI_MCHAN_EXT_ADDR, (offset)) -#define DMA_READ(offset) __builtin_pulp_OffsetedRead((int *)ARCHI_MCHAN_EXT_ADDR, (offset)) +#define DMA_WRITE(value, offset) __builtin_pulp_OffsetedWrite((value), (int *)DMA_ADDR, (offset)) +#define DMA_READ(offset) __builtin_pulp_OffsetedRead((int *)DMA_ADDR, (offset)) #else -#define DMA_WRITE(value, offset) pulp_write32(ARCHI_MCHAN_EXT_ADDR + (offset), (value)) -#define DMA_READ(offset) pulp_read32(ARCHI_MCHAN_EXT_ADDR + (offset)) +#define DMA_WRITE(value, offset) pulp_write32(DMA_ADDR + (offset), (value)) +#define DMA_READ(offset) pulp_read32(DMA_ADDR + (offset)) #endif static inline int plp_dma_counter_alloc() {