4343#include "esp_sha_dma_priv.h"
4444#include "sdkconfig.h"
4545
46- #ifdef SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT
47- #include "esp_flash_encrypt .h"
48- #endif /* SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT */
46+ #ifdef SOC_GDMA_EXT_MEM_ENC_ALIGNMENT
47+ #include "hal/efuse_hal .h"
48+ #endif /* SOC_GDMA_EXT_MEM_ENC_ALIGNMENT */
4949
5050#if SOC_SHA_CRYPTO_DMA
5151#include "hal/crypto_dma_ll.h"
@@ -154,7 +154,7 @@ static DRAM_ATTR crypto_dma_desc_t s_dma_descr_buf;
154154static esp_err_t esp_sha_dma_process (esp_sha_type sha_type , const void * input , uint32_t ilen ,
155155 const void * buf , uint32_t buf_len , bool is_first_block );
156156
157- #ifdef SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT
157+ #ifdef SOC_GDMA_EXT_MEM_ENC_ALIGNMENT
158158static esp_err_t esp_sha_dma_process_ext (esp_sha_type sha_type , const void * input , uint32_t ilen ,
159159 const void * buf , uint32_t buf_len , bool is_first_block ,
160160 bool realloc_input , bool realloc_buf )
@@ -170,7 +170,7 @@ static esp_err_t esp_sha_dma_process_ext(esp_sha_type sha_type, const void *inpu
170170
171171 if (realloc_input ) {
172172 heap_caps = MALLOC_CAP_8BIT | (esp_ptr_external_ram (input ) ? MALLOC_CAP_SPIRAM : MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL );
173- input_copy = heap_caps_aligned_alloc (SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT , ilen , heap_caps );
173+ input_copy = heap_caps_aligned_alloc (SOC_GDMA_EXT_MEM_ENC_ALIGNMENT , ilen , heap_caps );
174174 if (input_copy == NULL ) {
175175 ESP_LOGE (TAG , "Failed to allocate aligned SPIRAM memory" );
176176 return ret ;
@@ -183,7 +183,7 @@ static esp_err_t esp_sha_dma_process_ext(esp_sha_type sha_type, const void *inpu
183183
184184 if (realloc_buf ) {
185185 heap_caps = MALLOC_CAP_8BIT | (esp_ptr_external_ram (buf ) ? MALLOC_CAP_SPIRAM : MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL );
186- buf_copy = heap_caps_aligned_alloc (SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT , buf_len , heap_caps );
186+ buf_copy = heap_caps_aligned_alloc (SOC_GDMA_EXT_MEM_ENC_ALIGNMENT , buf_len , heap_caps );
187187 if (buf_copy == NULL ) {
188188 ESP_LOGE (TAG , "Failed to allocate aligned internal memory" );
189189 return ret ;
@@ -206,7 +206,7 @@ static esp_err_t esp_sha_dma_process_ext(esp_sha_type sha_type, const void *inpu
206206
207207 return ret ;
208208}
209- #endif /* SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT */
209+ #endif /* SOC_GDMA_EXT_MEM_ENC_ALIGNMENT */
210210
211211/* Performs SHA on multiple blocks at a time */
212212static esp_err_t esp_sha_dma_process (esp_sha_type sha_type , const void * input , uint32_t ilen ,
@@ -227,17 +227,17 @@ static esp_err_t esp_sha_dma_process(esp_sha_type sha_type, const void *input, u
227227
228228/* When SHA-DMA operations are carried out using external memory with external memory encryption enabled,
229229 we need to make sure that the addresses and the sizes of the buffers on which the DMA operates are 16 byte-aligned. */
230- #ifdef SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT
231- if (esp_flash_encryption_enabled ()) {
230+ #ifdef SOC_GDMA_EXT_MEM_ENC_ALIGNMENT
231+ if (efuse_hal_flash_encryption_enabled ()) {
232232 if (esp_ptr_external_ram (input ) || esp_ptr_external_ram (buf ) || esp_ptr_in_drom (input ) || esp_ptr_in_drom (buf )) {
233233 bool input_needs_realloc = false;
234234 bool buf_needs_realloc = false;
235235
236- if (ilen && ((intptr_t )(input ) & (SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT - 1 )) != 0 ) {
236+ if (ilen && ((intptr_t )(input ) & (SOC_GDMA_EXT_MEM_ENC_ALIGNMENT - 1 )) != 0 ) {
237237 input_needs_realloc = true;
238238 }
239239
240- if (buf_len && ((intptr_t )(buf ) & (SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT - 1 )) != 0 ) {
240+ if (buf_len && ((intptr_t )(buf ) & (SOC_GDMA_EXT_MEM_ENC_ALIGNMENT - 1 )) != 0 ) {
241241 buf_needs_realloc = true;
242242 }
243243
@@ -246,7 +246,7 @@ static esp_err_t esp_sha_dma_process(esp_sha_type sha_type, const void *input, u
246246 }
247247 }
248248 }
249- #endif /* SOC_AXI_DMA_EXT_MEM_ENC_ALIGNMENT */
249+ #endif /* SOC_GDMA_EXT_MEM_ENC_ALIGNMENT */
250250
251251 /* DMA descriptor for Memory to DMA-SHA transfer */
252252 if (ilen ) {
@@ -273,6 +273,19 @@ static esp_err_t esp_sha_dma_process(esp_sha_type sha_type, const void *input, u
273273 s_dma_descr_buf .next = (& s_dma_descr_input );
274274 }
275275
276+ /* Write back buffers to memory if they are in external RAM
277+ * The writeback needs to be performed in esp_sha_dma_process() instead of esp_sha_dma() to make
278+ * sure that if the buffers are reallocated, then the writeback is performed on the new buffers.
279+ */
280+ #if (CONFIG_SPIRAM && SOC_PSRAM_DMA_CAPABLE )
281+ if (esp_ptr_external_ram (input )) {
282+ esp_cache_msync ((void * )input , ilen , ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED );
283+ }
284+ if (esp_ptr_external_ram (buf )) {
285+ esp_cache_msync ((void * )buf , buf_len , ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED );
286+ }
287+ #endif
288+
276289#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
277290 if (ilen ) {
278291 ESP_ERROR_CHECK (esp_cache_msync (& s_dma_descr_input , sizeof (crypto_dma_desc_t ), ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED ));
@@ -317,15 +330,6 @@ int esp_sha_dma(esp_sha_type sha_type, const void *input, uint32_t ilen,
317330 return 0 ;
318331 }
319332
320- #if (CONFIG_SPIRAM && SOC_PSRAM_DMA_CAPABLE )
321- if (esp_ptr_external_ram (input )) {
322- esp_cache_msync ((void * )input , ilen , ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED );
323- }
324- if (esp_ptr_external_ram (buf )) {
325- esp_cache_msync ((void * )buf , buf_len , ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED );
326- }
327- #endif
328-
329333 /* Copy to internal buf if buf is in non DMA capable memory */
330334 if (!s_check_dma_capable (buf ) && (buf_len != 0 )) {
331335 dma_cap_buf = heap_caps_malloc (sizeof (unsigned char ) * buf_len , MALLOC_CAP_8BIT |MALLOC_CAP_DMA |MALLOC_CAP_INTERNAL );
0 commit comments