From b94c9e152178615196baa416e66754e57dc7c4d5 Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Fri, 13 Oct 2023 18:33:02 +0200 Subject: [PATCH 1/3] Zephyr integration: Add support for hosted test environment Provide the metal_sleep_usec() required for hosted environments. Signed-off-by: Alberto Escolar Piedras --- lib/system/zephyr/sys.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/system/zephyr/sys.h b/lib/system/zephyr/sys.h index 9133e105..9c60ecdf 100644 --- a/lib/system/zephyr/sys.h +++ b/lib/system/zephyr/sys.h @@ -17,6 +17,7 @@ #define __METAL_ZEPHYR_SYS__H__ #include +#include #ifdef __cplusplus extern "C" { @@ -39,6 +40,11 @@ struct metal_state { struct metal_common_state common; }; +static inline void metal_wait_usec(uint32_t usec_to_wait) +{ + k_busy_wait(usec_to_wait); +} + #ifdef __cplusplus } #endif From dafb78443a5e5dcdc45667822907bc7980fab4cd Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Fri, 13 Oct 2023 18:30:13 +0200 Subject: [PATCH 2/3] Add support for hosted test environments Add a new hosted test environment "processor". This is meant as a build target used when the code is not built for a real target but as part of a test in a hosted environment (for ex. as a test environment like Zephyr's native_sim target). When building for this target PROJECT_PROCESSOR should be set as "hosted". In this, the "metal_sleep_usec()" call is expected to be provided by the system folder headers. Signed-off-by: Alberto Escolar Piedras --- lib/processor/hosted/CMakeLists.txt | 2 ++ lib/processor/hosted/atomic.h | 15 +++++++++++++++ lib/processor/hosted/cpu.h | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 lib/processor/hosted/CMakeLists.txt create mode 100644 lib/processor/hosted/atomic.h create mode 100644 lib/processor/hosted/cpu.h diff --git a/lib/processor/hosted/CMakeLists.txt b/lib/processor/hosted/CMakeLists.txt new file mode 100644 index 00000000..13ce0230 --- /dev/null +++ b/lib/processor/hosted/CMakeLists.txt @@ -0,0 +1,2 @@ +collect (PROJECT_LIB_HEADERS atomic.h) +collect (PROJECT_LIB_HEADERS cpu.h) diff --git a/lib/processor/hosted/atomic.h b/lib/processor/hosted/atomic.h new file mode 100644 index 00000000..ed153c30 --- /dev/null +++ b/lib/processor/hosted/atomic.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * @file hosted/atomic.h + * @brief Hosted environment atomic primitives for libmetal. + */ + +#ifndef __METAL_HOSTED_ATOMIC__H__ +#define __METAL_HOSTED_ATOMIC__H__ + +#endif /* __METAL_HOSTED_ATOMIC__H__ */ diff --git a/lib/processor/hosted/cpu.h b/lib/processor/hosted/cpu.h new file mode 100644 index 00000000..be8a1680 --- /dev/null +++ b/lib/processor/hosted/cpu.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * @file hosted/cpu.h + * @brief Hosted environment CPU specific primitives + */ + +#include + +#ifndef __METAL_HOSTED_CPU__H__ +#define __METAL_HOSTED_CPU__H__ + +static inline void metal_cpu_yield(void) +{ + metal_wait_usec(10); +} + +#endif /* __METAL_HOSTED_CPU__H__ */ From 56366e3dcbc0e88e41d8d8e9d12080e59d8bcd1e Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Wed, 1 Nov 2023 09:27:50 +0100 Subject: [PATCH 3/3] Refactor equal processor headers into new generic ones Refactor all processor headers which had the same content into a generic set, to reduce the number of duplicates. Signed-off-by: Alberto Escolar Piedras --- lib/atomic.h | 4 ++++ lib/config.h | 2 ++ lib/cpu.h | 6 ++++++ lib/processor/CMakeLists.txt | 14 +++++++++++++- lib/processor/aarch64/CMakeLists.txt | 3 --- lib/processor/aarch64/atomic.h | 15 --------------- lib/processor/arm/CMakeLists.txt | 3 --- lib/processor/arm/atomic.h | 15 --------------- lib/processor/arm/cpu.h | 17 ----------------- lib/processor/ceva/CMakeLists.txt | 2 -- lib/processor/csky/CMakeLists.txt | 2 -- lib/processor/csky/cpu.h | 17 ----------------- lib/processor/generic/atomic.h | 15 +++++++++++++++ lib/processor/generic/cpu.h | 17 +++++++++++++++++ lib/processor/hosted/CMakeLists.txt | 2 -- lib/processor/hosted/atomic.h | 15 --------------- lib/processor/microblaze/CMakeLists.txt | 3 --- lib/processor/microblaze/atomic.h | 15 --------------- lib/processor/microblaze/cpu.h | 20 -------------------- lib/processor/riscv/CMakeLists.txt | 2 -- lib/processor/riscv/cpu.h | 17 ----------------- lib/processor/x86/CMakeLists.txt | 3 --- lib/processor/x86/atomic.h | 16 ---------------- lib/processor/x86_64/CMakeLists.txt | 3 --- lib/processor/x86_64/atomic.h | 16 ---------------- lib/processor/xtensa/CMakeLists.txt | 2 -- lib/processor/xtensa/atomic.h | 15 --------------- 27 files changed, 57 insertions(+), 204 deletions(-) delete mode 100644 lib/processor/aarch64/CMakeLists.txt delete mode 100644 lib/processor/aarch64/atomic.h delete mode 100644 lib/processor/arm/CMakeLists.txt delete mode 100644 lib/processor/arm/atomic.h delete mode 100644 lib/processor/arm/cpu.h delete mode 100644 lib/processor/ceva/CMakeLists.txt delete mode 100644 lib/processor/csky/CMakeLists.txt delete mode 100644 lib/processor/csky/cpu.h create mode 100644 lib/processor/generic/atomic.h create mode 100644 lib/processor/generic/cpu.h delete mode 100644 lib/processor/hosted/CMakeLists.txt delete mode 100644 lib/processor/hosted/atomic.h delete mode 100644 lib/processor/microblaze/CMakeLists.txt delete mode 100644 lib/processor/microblaze/atomic.h delete mode 100644 lib/processor/microblaze/cpu.h delete mode 100644 lib/processor/riscv/CMakeLists.txt delete mode 100644 lib/processor/riscv/cpu.h delete mode 100644 lib/processor/x86/CMakeLists.txt delete mode 100644 lib/processor/x86/atomic.h delete mode 100644 lib/processor/x86_64/CMakeLists.txt delete mode 100644 lib/processor/x86_64/atomic.h delete mode 100644 lib/processor/xtensa/CMakeLists.txt delete mode 100644 lib/processor/xtensa/atomic.h diff --git a/lib/atomic.h b/lib/atomic.h index 5fc04fc6..1afd15b8 100644 --- a/lib/atomic.h +++ b/lib/atomic.h @@ -107,7 +107,11 @@ using std::atomic_signal_fence; #elif defined(__GNUC__) # include #else +#if defined(HAVE_PROCESSOR_ATOMIC_H) # include +#else +# include +#endif /* defined(HAVE_PROCESSOR_ATOMIC_H) */ #endif #endif /* __METAL_ATOMIC__H__ */ diff --git a/lib/config.h b/lib/config.h index 7661a132..3b64a26e 100644 --- a/lib/config.h +++ b/lib/config.h @@ -42,6 +42,8 @@ extern "C" { #cmakedefine HAVE_STDATOMIC_H #cmakedefine HAVE_FUTEX_H +#cmakedefine HAVE_PROCESSOR_ATOMIC_H +#cmakedefine HAVE_PROCESSOR_CPU_H #ifdef __cplusplus } diff --git a/lib/cpu.h b/lib/cpu.h index 9deade61..3ee3596b 100644 --- a/lib/cpu.h +++ b/lib/cpu.h @@ -12,6 +12,12 @@ #ifndef __METAL_CPU__H__ #define __METAL_CPU__H__ +#include + +#if defined(HAVE_PROCESSOR_CPU_H) # include +#else +# include +#endif #endif /* __METAL_CPU__H__ */ diff --git a/lib/processor/CMakeLists.txt b/lib/processor/CMakeLists.txt index d542b56a..90200ff7 100644 --- a/lib/processor/CMakeLists.txt +++ b/lib/processor/CMakeLists.txt @@ -1,2 +1,14 @@ -add_subdirectory (${PROJECT_PROCESSOR}) +if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_PROCESSOR}/cpu.h") + collect (PROJECT_LIB_HEADERS ${PROJECT_PROCESSOR}/cpu.h) + set(HAVE_PROCESSOR_CPU_H 1 CACHE INTERNAL "Have include ${PROJECT_PROCESSOR}/cpu.h") +else() + collect (PROJECT_LIB_HEADERS generic/cpu.h) +endif() + +if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_PROCESSOR}/atomic.h") + collect (PROJECT_LIB_HEADERS ${PROJECT_PROCESSOR}/atomic.h) + set(HAVE_PROCESSOR_ATOMIC_H 1 CACHE INTERNAL "Have include ${PROJECT_PROCESSOR}/atomic.h") +else() + collect (PROJECT_LIB_HEADERS generic/atomic.h) +endif() diff --git a/lib/processor/aarch64/CMakeLists.txt b/lib/processor/aarch64/CMakeLists.txt deleted file mode 100644 index a20967bf..00000000 --- a/lib/processor/aarch64/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -collect (PROJECT_LIB_HEADERS atomic.h) -collect (PROJECT_LIB_HEADERS cpu.h) - diff --git a/lib/processor/aarch64/atomic.h b/lib/processor/aarch64/atomic.h deleted file mode 100644 index 410346c1..00000000 --- a/lib/processor/aarch64/atomic.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file gcc/atomic.h - * @brief GCC specific atomic primitives for libmetal. - */ - -#ifndef __METAL_AARCH64_ATOMIC__H__ -#define __METAL_AARCH64_ATOMIC__H__ - -#endif /* __METAL_ARM_ATOMIC__H__ */ diff --git a/lib/processor/arm/CMakeLists.txt b/lib/processor/arm/CMakeLists.txt deleted file mode 100644 index a20967bf..00000000 --- a/lib/processor/arm/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -collect (PROJECT_LIB_HEADERS atomic.h) -collect (PROJECT_LIB_HEADERS cpu.h) - diff --git a/lib/processor/arm/atomic.h b/lib/processor/arm/atomic.h deleted file mode 100644 index ab5ee406..00000000 --- a/lib/processor/arm/atomic.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file arm/atomic.h - * @brief Arm specific atomic primitives for libmetal. - */ - -#ifndef __METAL_ARM_ATOMIC__H__ -#define __METAL_ARM_ATOMIC__H__ - -#endif /* __METAL_ARM_ATOMIC__H__ */ diff --git a/lib/processor/arm/cpu.h b/lib/processor/arm/cpu.h deleted file mode 100644 index 0ea96102..00000000 --- a/lib/processor/arm/cpu.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file cpu.h - * @brief CPU specific primitives - */ - -#ifndef __METAL_ARM_CPU__H__ -#define __METAL_ARM_CPU__H__ - -#define metal_cpu_yield() - -#endif /* __METAL_ARM_CPU__H__ */ diff --git a/lib/processor/ceva/CMakeLists.txt b/lib/processor/ceva/CMakeLists.txt deleted file mode 100644 index 11b0f7d5..00000000 --- a/lib/processor/ceva/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -collect (PROJECT_LIB_HEADERS cpu.h) - diff --git a/lib/processor/csky/CMakeLists.txt b/lib/processor/csky/CMakeLists.txt deleted file mode 100644 index 11b0f7d5..00000000 --- a/lib/processor/csky/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -collect (PROJECT_LIB_HEADERS cpu.h) - diff --git a/lib/processor/csky/cpu.h b/lib/processor/csky/cpu.h deleted file mode 100644 index a3938488..00000000 --- a/lib/processor/csky/cpu.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (c) 2018, Pinecone Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file cpu.h - * @brief CPU specific primitives - */ - -#ifndef __METAL_CSKY_CPU__H__ -#define __METAL_CSKY_CPU__H__ - -#define metal_cpu_yield() - -#endif /* __METAL_CSKY_CPU__H__ */ diff --git a/lib/processor/generic/atomic.h b/lib/processor/generic/atomic.h new file mode 100644 index 00000000..9a2b252c --- /dev/null +++ b/lib/processor/generic/atomic.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * @file generic/atomic.h + * @brief Generic environment atomic primitives for libmetal. + */ + +#ifndef __METAL_GENERIC_ATOMIC__H__ +#define __METAL_GENERIC_ATOMIC__H__ + +#endif /* __METAL_GENERIC_ATOMIC__H__ */ diff --git a/lib/processor/generic/cpu.h b/lib/processor/generic/cpu.h new file mode 100644 index 00000000..60a72576 --- /dev/null +++ b/lib/processor/generic/cpu.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * @file generic/cpu.h + * @brief Generic CPU specific primitives + */ + +#ifndef __METAL_GENERIC_CPU__H__ +#define __METAL_GENERIC_CPU__H__ + +#define metal_cpu_yield() + +#endif /* __METAL_GENERIC_CPU__H__ */ diff --git a/lib/processor/hosted/CMakeLists.txt b/lib/processor/hosted/CMakeLists.txt deleted file mode 100644 index 13ce0230..00000000 --- a/lib/processor/hosted/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -collect (PROJECT_LIB_HEADERS atomic.h) -collect (PROJECT_LIB_HEADERS cpu.h) diff --git a/lib/processor/hosted/atomic.h b/lib/processor/hosted/atomic.h deleted file mode 100644 index ed153c30..00000000 --- a/lib/processor/hosted/atomic.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file hosted/atomic.h - * @brief Hosted environment atomic primitives for libmetal. - */ - -#ifndef __METAL_HOSTED_ATOMIC__H__ -#define __METAL_HOSTED_ATOMIC__H__ - -#endif /* __METAL_HOSTED_ATOMIC__H__ */ diff --git a/lib/processor/microblaze/CMakeLists.txt b/lib/processor/microblaze/CMakeLists.txt deleted file mode 100644 index a20967bf..00000000 --- a/lib/processor/microblaze/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -collect (PROJECT_LIB_HEADERS atomic.h) -collect (PROJECT_LIB_HEADERS cpu.h) - diff --git a/lib/processor/microblaze/atomic.h b/lib/processor/microblaze/atomic.h deleted file mode 100644 index d576662f..00000000 --- a/lib/processor/microblaze/atomic.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2017, Xilinx Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file microblaze/atomic.h - * @brief Microblaze specific atomic primitives for libmetal - */ - -#ifndef __METAL_MICROBLAZE_ATOMIC__H__ -#define __METAL_MICROBLAZE_ATOMIC__H__ - -#endif /* __METAL_MICROBLAZE_ATOMIC__H__ */ diff --git a/lib/processor/microblaze/cpu.h b/lib/processor/microblaze/cpu.h deleted file mode 100644 index 012df465..00000000 --- a/lib/processor/microblaze/cpu.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2017, Xilinx Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file cpu.h - * @brief CPU specific primitives on microblaze platform. - */ - -#ifndef __METAL_MICROBLAZE__H__ -#define __METAL_MICROBLAZE__H__ - -#include -#include - -#define metal_cpu_yield() - -#endif /* __METAL_MICROBLAZE__H__ */ diff --git a/lib/processor/riscv/CMakeLists.txt b/lib/processor/riscv/CMakeLists.txt deleted file mode 100644 index 11b0f7d5..00000000 --- a/lib/processor/riscv/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -collect (PROJECT_LIB_HEADERS cpu.h) - diff --git a/lib/processor/riscv/cpu.h b/lib/processor/riscv/cpu.h deleted file mode 100644 index ff02d9f8..00000000 --- a/lib/processor/riscv/cpu.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (c) 2018, Pinecone Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file cpu.h - * @brief CPU specific primitives - */ - -#ifndef __METAL_RISCV_CPU__H__ -#define __METAL_RISCV_CPU__H__ - -#define metal_cpu_yield() - -#endif /* __METAL_RISCV_CPU__H__ */ diff --git a/lib/processor/x86/CMakeLists.txt b/lib/processor/x86/CMakeLists.txt deleted file mode 100644 index a20967bf..00000000 --- a/lib/processor/x86/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -collect (PROJECT_LIB_HEADERS atomic.h) -collect (PROJECT_LIB_HEADERS cpu.h) - diff --git a/lib/processor/x86/atomic.h b/lib/processor/x86/atomic.h deleted file mode 100644 index bf98a9d1..00000000 --- a/lib/processor/x86/atomic.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file gcc/atomic.h - * @brief GCC specific atomic primitives for libmetal. - */ - -#ifndef __METAL_X86_ATOMIC__H__ -#define __METAL_X86_ATOMIC__H__ - - -#endif /* __METAL_X86_ATOMIC__H__ */ diff --git a/lib/processor/x86_64/CMakeLists.txt b/lib/processor/x86_64/CMakeLists.txt deleted file mode 100644 index a20967bf..00000000 --- a/lib/processor/x86_64/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -collect (PROJECT_LIB_HEADERS atomic.h) -collect (PROJECT_LIB_HEADERS cpu.h) - diff --git a/lib/processor/x86_64/atomic.h b/lib/processor/x86_64/atomic.h deleted file mode 100644 index ee773997..00000000 --- a/lib/processor/x86_64/atomic.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file gcc/atomic.h - * @brief GCC specific atomic primitives for libmetal. - */ - -#ifndef __METAL_X86_64_ATOMIC__H__ -#define __METAL_X86_64_ATOMIC__H__ - - -#endif /* __METAL_X86_64_ATOMIC__H__ */ diff --git a/lib/processor/xtensa/CMakeLists.txt b/lib/processor/xtensa/CMakeLists.txt deleted file mode 100644 index 13ce0230..00000000 --- a/lib/processor/xtensa/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -collect (PROJECT_LIB_HEADERS atomic.h) -collect (PROJECT_LIB_HEADERS cpu.h) diff --git a/lib/processor/xtensa/atomic.h b/lib/processor/xtensa/atomic.h deleted file mode 100644 index 6f87ea92..00000000 --- a/lib/processor/xtensa/atomic.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2021, Xiaomi Inc. and Contributors. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * @file xtensa/atomic.h - * @brief Xtensa specific atomic primitives for libmetal. - */ - -#ifndef __METAL_XTENSA_ATOMIC__H__ -#define __METAL_XTENSA_ATOMIC__H__ - -#endif /* __METAL_XTENSA_ATOMIC__H__ */