From 43864f7fb45dfd3413be9c2fb62940996da2b3a0 Mon Sep 17 00:00:00 2001 From: "harshal.patil" Date: Fri, 15 Sep 2023 17:25:18 +0530 Subject: [PATCH] feat(ds): use RCC atomic block to enable/reset the DS peripheral --- components/esp_hw_support/esp_ds.c | 11 +++++-- components/esp_hw_support/esp_hmac.c | 12 ++++++-- .../esp_private/esp_crypto_lock_internal.h | 2 ++ components/hal/esp32c3/include/hal/ds_ll.h | 28 +++++++++++++++++ components/hal/esp32c6/include/hal/ds_ll.h | 20 +++++++++++++ components/hal/esp32h2/include/hal/ds_ll.h | 20 +++++++++++++ components/hal/esp32p4/include/hal/ds_ll.h | 30 +++++++++++++++++++ components/hal/esp32s3/include/hal/ds_ll.h | 30 ++++++++++++++++++- .../hal/test_apps/crypto/main/ds/test_ds.c | 14 +++++++-- .../test_apps/crypto/main/hmac/test_hmac.c | 12 ++++++-- 10 files changed, 169 insertions(+), 10 deletions(-) diff --git a/components/esp_hw_support/esp_ds.c b/components/esp_hw_support/esp_ds.c index 332a282b8e..29f25b7638 100644 --- a/components/esp_hw_support/esp_ds.c +++ b/components/esp_hw_support/esp_ds.c @@ -266,7 +266,11 @@ static void ds_acquire_enable(void) } periph_module_enable(PERIPH_SHA_MODULE); - periph_module_enable(PERIPH_DS_MODULE); + + DS_RCC_ATOMIC() { + ds_ll_enable_bus_clock(true); + ds_ll_reset_register(); + } hmac_hal_start(); } @@ -275,7 +279,10 @@ static void ds_disable_release(void) { ds_hal_finish(); - periph_module_disable(PERIPH_DS_MODULE); + DS_RCC_ATOMIC() { + ds_ll_enable_bus_clock(false); + } + periph_module_disable(PERIPH_SHA_MODULE); HMAC_RCC_ATOMIC() { diff --git a/components/esp_hw_support/esp_hmac.c b/components/esp_hw_support/esp_hmac.c index acb69d8f3c..c71c021d0e 100644 --- a/components/esp_hw_support/esp_hmac.c +++ b/components/esp_hw_support/esp_hmac.c @@ -18,6 +18,7 @@ #include "soc/system_reg.h" #if !CONFIG_IDF_TARGET_ESP32S2 +#include "hal/ds_ll.h" #include "hal/hmac_hal.h" #include "hal/hmac_ll.h" #include "esp_private/periph_ctrl.h" @@ -75,7 +76,11 @@ esp_err_t esp_hmac_calculate(hmac_key_id_t key_id, } periph_module_enable(PERIPH_SHA_MODULE); - periph_module_enable(PERIPH_DS_MODULE); + + DS_RCC_ATOMIC() { + ds_ll_enable_bus_clock(true); + ds_ll_reset_register(); + } hmac_hal_start(); @@ -137,7 +142,10 @@ esp_err_t esp_hmac_calculate(hmac_key_id_t key_id, // Read back result (bit swapped) hmac_hal_read_result_256(hmac); - periph_module_disable(PERIPH_DS_MODULE); + DS_RCC_ATOMIC() { + ds_ll_enable_bus_clock(false); + } + periph_module_disable(PERIPH_SHA_MODULE); HMAC_RCC_ATOMIC() { diff --git a/components/esp_hw_support/include/esp_private/esp_crypto_lock_internal.h b/components/esp_hw_support/include/esp_private/esp_crypto_lock_internal.h index ceca9963d6..67c4212345 100644 --- a/components/esp_hw_support/include/esp_private/esp_crypto_lock_internal.h +++ b/components/esp_hw_support/include/esp_private/esp_crypto_lock_internal.h @@ -17,10 +17,12 @@ extern "C" { #define MPI_RCC_ATOMIC() #define ECC_RCC_ATOMIC() #define HMAC_RCC_ATOMIC() +#define DS_RCC_ATOMIC() #else /* !SOC_RCC_IS_INDEPENDENT */ #define MPI_RCC_ATOMIC() PERIPH_RCC_ATOMIC() #define ECC_RCC_ATOMIC() PERIPH_RCC_ATOMIC() #define HMAC_RCC_ATOMIC() PERIPH_RCC_ATOMIC() +#define DS_RCC_ATOMIC() PERIPH_RCC_ATOMIC() #endif /* SOC_RCC_IS_INDEPENDENT */ #ifdef __cplusplus diff --git a/components/hal/esp32c3/include/hal/ds_ll.h b/components/hal/esp32c3/include/hal/ds_ll.h index 8817c3581d..809d7b46de 100644 --- a/components/hal/esp32c3/include/hal/ds_ll.h +++ b/components/hal/esp32c3/include/hal/ds_ll.h @@ -17,12 +17,40 @@ #include "soc/hwcrypto_reg.h" #include "soc/soc_caps.h" +#include "soc/system_struct.h" #include "hal/ds_types.h" #ifdef __cplusplus extern "C" { #endif +/** + * @brief Enable the bus clock for Digital Signature peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void ds_ll_enable_bus_clock(bool enable) +{ + SYSTEM.perip_clk_en1.reg_crypto_ds_clk_en = enable; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define ds_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the Digital Signature peripheral module + */ +static inline void ds_ll_reset_register(void) +{ + SYSTEM.perip_rst_en1.reg_crypto_ds_rst = 1; + SYSTEM.perip_rst_en1.reg_crypto_ds_rst = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define ds_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_reset_register(__VA_ARGS__) + static inline void ds_ll_start(void) { REG_WRITE(DS_SET_START_REG, 1); diff --git a/components/hal/esp32c6/include/hal/ds_ll.h b/components/hal/esp32c6/include/hal/ds_ll.h index 2f56cfe9cd..85d4d3ef90 100644 --- a/components/hal/esp32c6/include/hal/ds_ll.h +++ b/components/hal/esp32c6/include/hal/ds_ll.h @@ -17,6 +17,7 @@ #include "soc/hwcrypto_reg.h" #include "soc/soc_caps.h" +#include "soc/pcr_struct.h" #include "hal/ds_types.h" @@ -24,6 +25,25 @@ extern "C" { #endif +/** + * @brief Enable the bus clock for Digital Signature peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void ds_ll_enable_bus_clock(bool enable) +{ + PCR.ds_conf.ds_clk_en = enable; +} + +/** + * @brief Reset the Digital Signature peripheral module + */ +static inline void ds_ll_reset_register(void) +{ + PCR.ds_conf.ds_rst_en = 1; + PCR.ds_conf.ds_rst_en = 0; +} + static inline void ds_ll_start(void) { REG_WRITE(DS_SET_START_REG, 1); diff --git a/components/hal/esp32h2/include/hal/ds_ll.h b/components/hal/esp32h2/include/hal/ds_ll.h index 7a4a3fa353..2c4ccf2009 100644 --- a/components/hal/esp32h2/include/hal/ds_ll.h +++ b/components/hal/esp32h2/include/hal/ds_ll.h @@ -17,12 +17,32 @@ #include "soc/hwcrypto_reg.h" #include "soc/soc_caps.h" +#include "soc/pcr_struct.h" #include "hal/ds_types.h" #ifdef __cplusplus extern "C" { #endif +/** + * @brief Enable the bus clock for Digital Signature peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void ds_ll_enable_bus_clock(bool enable) +{ + PCR.ds_conf.ds_clk_en = enable; +} + +/** + * @brief Reset the Digital Signature peripheral module + */ +static inline void ds_ll_reset_register(void) +{ + PCR.ds_conf.ds_rst_en = 1; + PCR.ds_conf.ds_rst_en = 0; +} + static inline void ds_ll_start(void) { REG_WRITE(DS_SET_START_REG, 1); diff --git a/components/hal/esp32p4/include/hal/ds_ll.h b/components/hal/esp32p4/include/hal/ds_ll.h index 2f56cfe9cd..64d556e040 100644 --- a/components/hal/esp32p4/include/hal/ds_ll.h +++ b/components/hal/esp32p4/include/hal/ds_ll.h @@ -16,6 +16,7 @@ #include #include "soc/hwcrypto_reg.h" +#include "soc/hp_sys_clkrst_struct.h" #include "soc/soc_caps.h" #include "hal/ds_types.h" @@ -24,6 +25,35 @@ extern "C" { #endif +/** + * @brief Enable the bus clock for DS peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void ds_ll_enable_bus_clock(bool enable) +{ + HP_SYS_CLKRST.peri_clk_ctrl25.reg_crypto_ds_clk_en = enable; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define ds_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the DS peripheral module + */ +static inline void ds_ll_reset_register(void) +{ + HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_ds = 1; + HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_ds = 0; + HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_crypto = 1; + HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_crypto = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define ds_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_reset_register(__VA_ARGS__) + static inline void ds_ll_start(void) { REG_WRITE(DS_SET_START_REG, 1); diff --git a/components/hal/esp32s3/include/hal/ds_ll.h b/components/hal/esp32s3/include/hal/ds_ll.h index 928533a65e..053194c36f 100644 --- a/components/hal/esp32s3/include/hal/ds_ll.h +++ b/components/hal/esp32s3/include/hal/ds_ll.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -12,12 +12,40 @@ #include "soc/hwcrypto_reg.h" #include "soc/soc_caps.h" +#include "soc/system_struct.h" #include "hal/ds_types.h" #ifdef __cplusplus extern "C" { #endif +/** + * @brief Enable the bus clock for Digital Signature peripheral module + * + * @param true to enable the module, false to disable the module + */ +static inline void ds_ll_enable_bus_clock(bool enable) +{ + SYSTEM.perip_clk_en1.crypto_ds_clk_en = enable; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define ds_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the Digital Signature peripheral module + */ +static inline void ds_ll_reset_register(void) +{ + SYSTEM.perip_rst_en1.crypto_ds_rst = 1; + SYSTEM.perip_rst_en1.crypto_ds_rst = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define ds_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_reset_register(__VA_ARGS__) + static inline void ds_ll_start(void) { REG_WRITE(DS_SET_START_REG, 1); diff --git a/components/hal/test_apps/crypto/main/ds/test_ds.c b/components/hal/test_apps/crypto/main/ds/test_ds.c index 21c09456ad..c954260104 100644 --- a/components/hal/test_apps/crypto/main/ds/test_ds.c +++ b/components/hal/test_apps/crypto/main/ds/test_ds.c @@ -139,20 +139,28 @@ static void ds_acquire_enable(void) } periph_module_enable(PERIPH_SHA_MODULE); - periph_module_enable(PERIPH_DS_MODULE); + + DS_RCC_ATOMIC() { + ds_ll_enable_bus_clock(true); + ds_ll_reset_register(); + } + hmac_hal_start(); } static void ds_disable_release(void) { ds_hal_finish(); - periph_module_disable(PERIPH_DS_MODULE); + + DS_RCC_ATOMIC() { + ds_ll_enable_bus_clock(false); + } + periph_module_disable(PERIPH_SHA_MODULE); HMAC_RCC_ATOMIC() { hmac_ll_enable_bus_clock(false); } - } diff --git a/components/hal/test_apps/crypto/main/hmac/test_hmac.c b/components/hal/test_apps/crypto/main/hmac/test_hmac.c index e6c47acd73..27478062ab 100644 --- a/components/hal/test_apps/crypto/main/hmac/test_hmac.c +++ b/components/hal/test_apps/crypto/main/hmac/test_hmac.c @@ -41,6 +41,7 @@ static esp_err_t hmac_jtag_disable(void) #include "hal/hmac_hal.h" #include "hal/hmac_ll.h" +#include "hal/ds_ll.h" #include "esp_private/periph_ctrl.h" #define SHA256_BLOCK_SZ 64 @@ -78,7 +79,11 @@ static esp_err_t hmac_calculate(hmac_key_id_t key_id, const void *message, size_ } periph_module_enable(PERIPH_SHA_MODULE); - periph_module_enable(PERIPH_DS_MODULE); + + DS_RCC_ATOMIC() { + ds_ll_enable_bus_clock(true); + ds_ll_reset_register(); + } hmac_hal_start(); @@ -130,7 +135,10 @@ static esp_err_t hmac_calculate(hmac_key_id_t key_id, const void *message, size_ hmac_hal_read_result_256(hmac); - periph_module_disable(PERIPH_DS_MODULE); + DS_RCC_ATOMIC() { + ds_ll_enable_bus_clock(false); + } + periph_module_disable(PERIPH_SHA_MODULE); HMAC_RCC_ATOMIC() {