change(mbedtls): replace Cache ROM APIs usage with APIs from esp_cache.h

- Only APIs used in esp_crypto_shared_gdma and aes have been replaced
- Get dcache line size using cache api instead of Kconfig
This commit is contained in:
harshal.patil
2023-10-31 14:25:06 +05:30
parent a6012c9e5a
commit 9b15189583
4 changed files with 30 additions and 30 deletions

View File

@@ -56,7 +56,7 @@ if(CONFIG_SOC_SHA_SUPPORTED)
endif() endif()
idf_component_register(SRCS ${srcs} idf_component_register(SRCS ${srcs}
PRIV_REQUIRES efuse mbedtls PRIV_REQUIRES efuse mbedtls esp_mm
REQUIRES test_utils unity REQUIRES test_utils unity
WHOLE_ARCHIVE WHOLE_ARCHIVE
PRIV_INCLUDE_DIRS "${priv_include_dirs}" PRIV_INCLUDE_DIRS "${priv_include_dirs}"

View File

@@ -180,7 +180,6 @@ if(SHA_PERIPHERAL_TYPE STREQUAL "dma")
set(SHA_DMA_SRCS "${COMPONENT_DIR}/port/sha/dma/esp_sha_crypto_dma_impl.c") set(SHA_DMA_SRCS "${COMPONENT_DIR}/port/sha/dma/esp_sha_crypto_dma_impl.c")
else() else()
set(SHA_DMA_SRCS "${COMPONENT_DIR}/port/sha/dma/esp_sha_gdma_impl.c") set(SHA_DMA_SRCS "${COMPONENT_DIR}/port/sha/dma/esp_sha_gdma_impl.c")
endif() endif()
target_sources(mbedcrypto PRIVATE "${SHA_DMA_SRCS}") target_sources(mbedcrypto PRIVATE "${SHA_DMA_SRCS}")
endif() endif()
@@ -189,14 +188,19 @@ if(AES_PERIPHERAL_TYPE STREQUAL "dma")
if(NOT CONFIG_SOC_AES_GDMA) if(NOT CONFIG_SOC_AES_GDMA)
set(AES_DMA_SRCS "${COMPONENT_DIR}/port/aes/dma/esp_aes_crypto_dma_impl.c") set(AES_DMA_SRCS "${COMPONENT_DIR}/port/aes/dma/esp_aes_crypto_dma_impl.c")
else() else()
set(AES_DMA_SRCS "${COMPONENT_DIR}/port/aes/dma/esp_aes_gdma_impl.c" set(AES_DMA_SRCS "${COMPONENT_DIR}/port/aes/dma/esp_aes_gdma_impl.c")
"${COMPONENT_DIR}/port/crypto_shared_gdma/esp_crypto_shared_gdma.c")
endif() endif()
target_include_directories(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/aes/dma/include") target_include_directories(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/aes/dma/include")
target_sources(mbedcrypto PRIVATE "${AES_DMA_SRCS}") target_sources(mbedcrypto PRIVATE "${AES_DMA_SRCS}")
endif() endif()
if(SHA_PERIPHERAL_TYPE STREQUAL "dma" OR AES_PERIPHERAL_TYPE STREQUAL "dma")
target_link_libraries(mbedcrypto PRIVATE idf::esp_mm)
if(CONFIG_SOC_SHA_GDMA OR CONFIG_SOC_AES_GDMA)
target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/crypto_shared_gdma/esp_crypto_shared_gdma.c")
endif()
endif()
if(NOT ${IDF_TARGET} STREQUAL "linux") if(NOT ${IDF_TARGET} STREQUAL "linux")
target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_hardware.c") target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_hardware.c")

View File

@@ -36,6 +36,7 @@
#include "esp_crypto_dma.h" #include "esp_crypto_dma.h"
#include "esp_heap_caps.h" #include "esp_heap_caps.h"
#include "esp_memory_utils.h" #include "esp_memory_utils.h"
#include "esp_cache.h"
#include "sys/param.h" #include "sys/param.h"
#if CONFIG_PM_ENABLE #if CONFIG_PM_ENABLE
#include "esp_pm.h" #include "esp_pm.h"
@@ -44,14 +45,7 @@
#include "hal/aes_hal.h" #include "hal/aes_hal.h"
#include "esp_aes_dma_priv.h" #include "esp_aes_dma_priv.h"
#include "esp_aes_internal.h" #include "esp_aes_internal.h"
#include "esp_private/esp_cache_private.h"
#if CONFIG_IDF_TARGET_ESP32S2
#include "esp32s2/rom/cache.h"
#elif CONFIG_IDF_TARGET_ESP32S3
#include "esp32s3/rom/cache.h"
#elif CONFIG_IDF_TARGET_ESP32P4
#include "esp32p4/rom/cache.h"
#endif
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
@@ -105,10 +99,10 @@ static bool s_check_dma_capable(const void *p);
* * Must be in DMA capable memory, so stack is not a safe place to put them * * Must be in DMA capable memory, so stack is not a safe place to put them
* * To avoid having to malloc/free them for every DMA operation * * To avoid having to malloc/free them for every DMA operation
*/ */
DMA_DESC_ALIGN_ATTR static DRAM_ATTR crypto_dma_desc_t s_stream_in_desc; static DRAM_ATTR crypto_dma_desc_t s_stream_in_desc;
DMA_DESC_ALIGN_ATTR static DRAM_ATTR crypto_dma_desc_t s_stream_out_desc; static DRAM_ATTR crypto_dma_desc_t s_stream_out_desc;
DMA_DESC_ALIGN_ATTR static DRAM_ATTR uint8_t s_stream_in[AES_BLOCK_BYTES]; static DRAM_ATTR uint8_t s_stream_in[AES_BLOCK_BYTES];
DMA_DESC_ALIGN_ATTR static DRAM_ATTR uint8_t s_stream_out[AES_BLOCK_BYTES]; static DRAM_ATTR uint8_t s_stream_out[AES_BLOCK_BYTES];
/** Append a descriptor to the chain, set head if chain empty /** Append a descriptor to the chain, set head if chain empty
* *
@@ -290,8 +284,8 @@ static int esp_aes_dma_wait_complete(bool use_intr, crypto_dma_desc_t *output_de
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE #if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
const crypto_dma_desc_t *it = output_desc_head; const crypto_dma_desc_t *it = output_desc_head;
while(it != NULL) { while(it != NULL) {
Cache_Invalidate_Addr(CACHE_MAP_L1_DCACHE | CACHE_MAP_L2_CACHE, (uint32_t)it->buffer, it->dw0.length); esp_cache_msync(it->buffer, it->dw0.length, ESP_CACHE_MSYNC_FLAG_DIR_M2C | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
Cache_Invalidate_Addr(CACHE_MAP_L1_DCACHE | CACHE_MAP_L2_CACHE, (uint32_t)it, sizeof(crypto_dma_desc_t)); esp_cache_msync((void *)it, sizeof(crypto_dma_desc_t), ESP_CACHE_MSYNC_FLAG_DIR_M2C | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
it = (const crypto_dma_desc_t*) it->next; it = (const crypto_dma_desc_t*) it->next;
}; };
#endif /* SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE */ #endif /* SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE */
@@ -419,10 +413,15 @@ static int esp_aes_process_dma(esp_aes_context *ctx, const unsigned char *input,
/* Flush cache if input in external ram */ /* Flush cache if input in external ram */
#if (CONFIG_SPIRAM && SOC_PSRAM_DMA_CAPABLE) #if (CONFIG_SPIRAM && SOC_PSRAM_DMA_CAPABLE)
if (esp_ptr_external_ram(input)) { if (esp_ptr_external_ram(input)) {
Cache_WriteBack_Addr((uint32_t)input, len); esp_cache_msync((void *)input, len, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
} }
if (esp_ptr_external_ram(output)) { if (esp_ptr_external_ram(output)) {
if ((((intptr_t)(output) & (DCACHE_LINE_SIZE - 1)) != 0) || (block_bytes % DCACHE_LINE_SIZE != 0)) { uint32_t dcache_line_size;
esp_err_t ret = esp_cache_get_alignment(ESP_CACHE_MALLOC_FLAG_PSRAM, &dcache_line_size);
if (ret != ESP_OK) {
return ret;
}
if ((((intptr_t)(output) & (dcache_line_size - 1)) != 0) || (block_bytes % dcache_line_size != 0)) {
// Non aligned ext-mem buffer // Non aligned ext-mem buffer
output_needs_realloc = true; output_needs_realloc = true;
} }
@@ -446,7 +445,7 @@ static int esp_aes_process_dma(esp_aes_context *ctx, const unsigned char *input,
crypto_dma_desc_num = dma_desc_get_required_num(block_bytes, DMA_DESCRIPTOR_BUFFER_MAX_SIZE_16B_ALIGNED); crypto_dma_desc_num = dma_desc_get_required_num(block_bytes, DMA_DESCRIPTOR_BUFFER_MAX_SIZE_16B_ALIGNED);
/* Allocate both in and out descriptors to save a malloc/free per function call */ /* Allocate both in and out descriptors to save a malloc/free per function call */
block_desc = heap_caps_aligned_calloc(DMA_DESC_MEM_ALIGN_SIZE, crypto_dma_desc_num * 2, sizeof(crypto_dma_desc_t), MALLOC_CAP_DMA); block_desc = heap_caps_aligned_calloc(8, crypto_dma_desc_num * 2, sizeof(crypto_dma_desc_t), MALLOC_CAP_DMA);
if (block_desc == NULL) { if (block_desc == NULL) {
mbedtls_platform_zeroize(output, len); mbedtls_platform_zeroize(output, len);
ESP_LOGE(TAG, "Failed to allocate memory"); ESP_LOGE(TAG, "Failed to allocate memory");
@@ -521,7 +520,7 @@ static int esp_aes_process_dma(esp_aes_context *ctx, const unsigned char *input,
#if (CONFIG_SPIRAM && SOC_PSRAM_DMA_CAPABLE) #if (CONFIG_SPIRAM && SOC_PSRAM_DMA_CAPABLE)
if (block_bytes > 0) { if (block_bytes > 0) {
if (esp_ptr_external_ram(output)) { if (esp_ptr_external_ram(output)) {
Cache_Invalidate_Addr((uint32_t)output, block_bytes); esp_cache_msync((void*)output, block_bytes, ESP_CACHE_MSYNC_FLAG_DIR_M2C | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
} }
} }
#endif #endif

View File

@@ -10,14 +10,11 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_err.h" #include "esp_err.h"
#include "esp_cache.h"
#include "esp_crypto_dma.h" #include "esp_crypto_dma.h"
#include "esp_crypto_lock.h" #include "esp_crypto_lock.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#if CONFIG_IDF_TARGET_ESP32P4
#include "esp32p4/rom/cache.h"
#endif
#if SOC_AHB_GDMA_VERSION == 1 #if SOC_AHB_GDMA_VERSION == 1
#include "hal/gdma_ll.h" #include "hal/gdma_ll.h"
#elif SOC_AXI_GDMA_SUPPORTED #elif SOC_AXI_GDMA_SUPPORTED
@@ -152,15 +149,15 @@ esp_err_t esp_crypto_shared_gdma_start_axi_ahb(const crypto_dma_desc_t *input, c
// TODO: replace with `esp_cache_msync` // TODO: replace with `esp_cache_msync`
const crypto_dma_desc_t *it = input; const crypto_dma_desc_t *it = input;
while(it != NULL) { while(it != NULL) {
Cache_WriteBack_Addr(CACHE_MAP_L1_DCACHE | CACHE_MAP_L2_CACHE, (uint32_t)it->buffer, it->dw0.length); // try using esp_cache_msync() esp_cache_msync(it->buffer, it->dw0.length, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
Cache_WriteBack_Addr(CACHE_MAP_L1_DCACHE | CACHE_MAP_L2_CACHE, (uint32_t)it, sizeof(crypto_dma_desc_t)); esp_cache_msync((void *)it, sizeof(crypto_dma_desc_t), ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
it = (const crypto_dma_desc_t*) it->next; it = (const crypto_dma_desc_t*) it->next;
} }
it = output; it = output;
while(it != NULL) { while(it != NULL) {
Cache_WriteBack_Addr(CACHE_MAP_L1_DCACHE | CACHE_MAP_L2_CACHE, (uint32_t)it->buffer, it->dw0.length); esp_cache_msync(it->buffer, it->dw0.length, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
Cache_WriteBack_Addr(CACHE_MAP_L1_DCACHE | CACHE_MAP_L2_CACHE, (uint32_t)it, sizeof(crypto_dma_desc_t)); esp_cache_msync((void *)it, sizeof(crypto_dma_desc_t), ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
it = (const crypto_dma_desc_t*) it->next; it = (const crypto_dma_desc_t*) it->next;
}; };
#endif /* SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE */ #endif /* SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE */