From b1027005dfc3d0ec70d75c970d9706b469353628 Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Fri, 19 Feb 2021 21:41:26 +0800 Subject: [PATCH] arch: move stdatomic --- components/newlib/CMakeLists.txt | 1 + components/newlib/newlib.lf | 1 + components/{xtensa => newlib}/stdatomic.c | 49 ++++++-- components/riscv/CMakeLists.txt | 1 - components/riscv/linker.lf | 1 - components/riscv/stdatomic.c | 129 ---------------------- components/xtensa/CMakeLists.txt | 3 - components/xtensa/linker.lf | 2 - 8 files changed, 40 insertions(+), 147 deletions(-) rename components/{xtensa => newlib}/stdatomic.c (83%) delete mode 100644 components/riscv/stdatomic.c diff --git a/components/newlib/CMakeLists.txt b/components/newlib/CMakeLists.txt index 0f869ed475..eb12848509 100644 --- a/components/newlib/CMakeLists.txt +++ b/components/newlib/CMakeLists.txt @@ -9,6 +9,7 @@ set(srcs "newlib_init.c" "syscalls.c" "termios.c" + "stdatomic.c" "time.c") set(include_dirs platform_include) diff --git a/components/newlib/newlib.lf b/components/newlib/newlib.lf index 63a93164a2..3d9a0ed14d 100644 --- a/components/newlib/newlib.lf +++ b/components/newlib/newlib.lf @@ -3,3 +3,4 @@ archive: libnewlib.a entries: heap (noflash) abort (noflash) + stdatomic (noflash) diff --git a/components/xtensa/stdatomic.c b/components/newlib/stdatomic.c similarity index 83% rename from components/xtensa/stdatomic.c rename to components/newlib/stdatomic.c index dad6170fb3..26f0810641 100644 --- a/components/xtensa/stdatomic.c +++ b/components/newlib/stdatomic.c @@ -16,13 +16,13 @@ #include "sdkconfig.h" #include +#include + +#ifdef __XTENSA__ + #include "xtensa/config/core-isa.h" #include "xtensa/xtruntime.h" -//reserved to measure atomic operation time -#define atomic_benchmark_intr_disable() -#define atomic_benchmark_intr_restore(STATE) - // This allows nested interrupts disabling and restoring via local registers or stack. // They can be called from interrupts too. // WARNING: Only applies to current CPU. @@ -37,6 +37,38 @@ XTOS_RESTORE_JUST_INTLEVEL(state); \ } while (0) +#ifndef XCHAL_HAVE_S32C1I +#error "XCHAL_HAVE_S32C1I not defined, include correct header!" +#endif + +#define NO_ATOMICS_SUPPORT (XCHAL_HAVE_S32C1I == 0) +#else // RISCV + +#include "freertos/portmacro.h" + +// This allows nested interrupts disabling and restoring via local registers or stack. +// They can be called from interrupts too. +// WARNING: Only applies to current CPU. +#define _ATOMIC_ENTER_CRITICAL(void) ({ \ + unsigned state = portENTER_CRITICAL_NESTED(); \ + atomic_benchmark_intr_disable(); \ + state; \ +}) + +#define _ATOMIC_EXIT_CRITICAL(state) do { \ + atomic_benchmark_intr_restore(state); \ + portEXIT_CRITICAL_NESTED(state); \ + } while (0) + +#define NO_ATOMICS_SUPPORT 1 // [todo] Get the equivalent XCHAL_HAVE_S32C1I check for RISCV + +#endif + + +//reserved to measure atomic operation time +#define atomic_benchmark_intr_disable() +#define atomic_benchmark_intr_restore(STATE) + #define CMP_EXCHANGE(n, type) bool __atomic_compare_exchange_ ## n (type* mem, type* expect, type desired, int success, int failure) \ { \ bool ret = false; \ @@ -96,15 +128,10 @@ return ret; \ } -#ifndef XCHAL_HAVE_S32C1I -#error "XCHAL_HAVE_S32C1I not defined, include correct header!" -#endif - -//this piece of code should only be compiled if the cpu doesn't support atomic compare and swap (s32c1i) -#if XCHAL_HAVE_S32C1I == 0 - #pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch" +#if NO_ATOMICS_SUPPORT + CMP_EXCHANGE(1, uint8_t) CMP_EXCHANGE(2, uint16_t) CMP_EXCHANGE(4, uint32_t) diff --git a/components/riscv/CMakeLists.txt b/components/riscv/CMakeLists.txt index 978d4ebdd4..e734394c5f 100644 --- a/components/riscv/CMakeLists.txt +++ b/components/riscv/CMakeLists.txt @@ -9,7 +9,6 @@ else() set(srcs "instruction_decode.c" "interrupt.c" - "stdatomic.c" "vectors.S") endif() diff --git a/components/riscv/linker.lf b/components/riscv/linker.lf index 2e5681238c..cfcb303cf0 100644 --- a/components/riscv/linker.lf +++ b/components/riscv/linker.lf @@ -3,4 +3,3 @@ archive: libriscv.a entries: interrupt (noflash_text) vectors (noflash_text) - stdatomic (noflash_text) diff --git a/components/riscv/stdatomic.c b/components/riscv/stdatomic.c deleted file mode 100644 index 7c0c395db5..0000000000 --- a/components/riscv/stdatomic.c +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//replacement for gcc built-in functions - -#include "sdkconfig.h" -#include -#include -#include "freertos/portmacro.h" - -//reserved to measure atomic operation time -#define atomic_benchmark_intr_disable() -#define atomic_benchmark_intr_restore(STATE) - -// This allows nested interrupts disabling and restoring via local registers or stack. -// They can be called from interrupts too. -// WARNING: Only applies to current CPU. -#define _ATOMIC_ENTER_CRITICAL(void) ({ \ - unsigned state = portENTER_CRITICAL_NESTED(); \ - atomic_benchmark_intr_disable(); \ - state; \ -}) - -#define _ATOMIC_EXIT_CRITICAL(state) do { \ - atomic_benchmark_intr_restore(state); \ - portEXIT_CRITICAL_NESTED(state); \ - } while (0) - -#define CMP_EXCHANGE(n, type) bool __atomic_compare_exchange_ ## n (type* mem, type* expect, type desired, int success, int failure) \ -{ \ - bool ret = false; \ - unsigned state = _ATOMIC_ENTER_CRITICAL(); \ - if (*mem == *expect) { \ - ret = true; \ - *mem = desired; \ - } else { \ - *expect = *mem; \ - } \ - _ATOMIC_EXIT_CRITICAL(state); \ - return ret; \ -} - -#define FETCH_ADD(n, type) type __atomic_fetch_add_ ## n (type* ptr, type value, int memorder) \ -{ \ - unsigned state = _ATOMIC_ENTER_CRITICAL(); \ - type ret = *ptr; \ - *ptr = *ptr + value; \ - _ATOMIC_EXIT_CRITICAL(state); \ - return ret; \ -} - -#define FETCH_SUB(n, type) type __atomic_fetch_sub_ ## n (type* ptr, type value, int memorder) \ -{ \ - unsigned state = _ATOMIC_ENTER_CRITICAL(); \ - type ret = *ptr; \ - *ptr = *ptr - value; \ - _ATOMIC_EXIT_CRITICAL(state); \ - return ret; \ -} - -#define FETCH_AND(n, type) type __atomic_fetch_and_ ## n (type* ptr, type value, int memorder) \ -{ \ - unsigned state = _ATOMIC_ENTER_CRITICAL(); \ - type ret = *ptr; \ - *ptr = *ptr & value; \ - _ATOMIC_EXIT_CRITICAL(state); \ - return ret; \ -} - -#define FETCH_OR(n, type) type __atomic_fetch_or_ ## n (type* ptr, type value, int memorder) \ -{ \ - unsigned state = _ATOMIC_ENTER_CRITICAL(); \ - type ret = *ptr; \ - *ptr = *ptr | value; \ - _ATOMIC_EXIT_CRITICAL(state); \ - return ret; \ -} - -#define FETCH_XOR(n, type) type __atomic_fetch_xor_ ## n (type* ptr, type value, int memorder) \ -{ \ - unsigned state = _ATOMIC_ENTER_CRITICAL(); \ - type ret = *ptr; \ - *ptr = *ptr ^ value; \ - _ATOMIC_EXIT_CRITICAL(state); \ - return ret; \ -} - -#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch" - -CMP_EXCHANGE(1, uint8_t) -CMP_EXCHANGE(2, uint16_t) -CMP_EXCHANGE(4, uint32_t) -CMP_EXCHANGE(8, uint64_t) - -FETCH_ADD(1, uint8_t) -FETCH_ADD(2, uint16_t) -FETCH_ADD(4, uint32_t) -FETCH_ADD(8, uint64_t) - -FETCH_SUB(1, uint8_t) -FETCH_SUB(2, uint16_t) -FETCH_SUB(4, uint32_t) -FETCH_SUB(8, uint64_t) - -FETCH_AND(1, uint8_t) -FETCH_AND(2, uint16_t) -FETCH_AND(4, uint32_t) -FETCH_AND(8, uint64_t) - -FETCH_OR(1, uint8_t) -FETCH_OR(2, uint16_t) -FETCH_OR(4, uint32_t) -FETCH_OR(8, uint64_t) - -FETCH_XOR(1, uint8_t) -FETCH_XOR(2, uint16_t) -FETCH_XOR(4, uint32_t) -FETCH_XOR(8, uint64_t) diff --git a/components/xtensa/CMakeLists.txt b/components/xtensa/CMakeLists.txt index 4be7c5eeb6..ee7d28780a 100644 --- a/components/xtensa/CMakeLists.txt +++ b/components/xtensa/CMakeLists.txt @@ -14,9 +14,6 @@ else() "${target}/trax_init.c" ) - if(IDF_TARGET STREQUAL "esp32s2") - list(APPEND srcs "stdatomic.c") - endif() endif() idf_component_register(SRCS ${srcs} diff --git a/components/xtensa/linker.lf b/components/xtensa/linker.lf index 157857b722..2d9d446097 100644 --- a/components/xtensa/linker.lf +++ b/components/xtensa/linker.lf @@ -2,8 +2,6 @@ archive: libxtensa.a entries: eri (noflash_text) - if IDF_TARGET_ESP32S2 = y: - stdatomic (noflash) [mapping:xt_hal] archive: libxt_hal.a