mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
hwcrypto sha: Use spinlocks instead of semaphores for small state changes
Significant performance improvement and smaller RAM footprint.
This commit is contained in:
parent
36b65c8498
commit
02668b62e8
@ -27,7 +27,6 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/lock.h>
|
||||
#include <byteswap.h>
|
||||
#include <assert.h>
|
||||
|
||||
@ -56,9 +55,9 @@ inline static uint32_t SHA_CONTINUE_REG(esp_sha_type sha_type) {
|
||||
return SHA_1_CONTINUE_REG + sha_type * 0x10;
|
||||
}
|
||||
|
||||
/* Single lock for SHA engine memory block
|
||||
/* Single spinlock for SHA engine memory block
|
||||
*/
|
||||
static _lock_t memory_block_lock;
|
||||
static portMUX_TYPE memory_block_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
|
||||
/* Binary semaphore managing the state of each concurrent SHA engine.
|
||||
@ -75,9 +74,9 @@ static SemaphoreHandle_t engine_states[3];
|
||||
|
||||
static uint8_t engines_in_use;
|
||||
|
||||
/* Lock for engines_in_use counter
|
||||
/* Spinlock for engines_in_use counter
|
||||
*/
|
||||
static _lock_t engines_in_use_lock;
|
||||
static portMUX_TYPE engines_in_use_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
/* Index into the engine_states array */
|
||||
inline static size_t sha_engine_index(esp_sha_type type) {
|
||||
@ -123,12 +122,12 @@ inline static size_t block_length(esp_sha_type type) {
|
||||
|
||||
void esp_sha_lock_memory_block(void)
|
||||
{
|
||||
_lock_acquire(&memory_block_lock);
|
||||
portENTER_CRITICAL(&memory_block_lock);
|
||||
}
|
||||
|
||||
void esp_sha_unlock_memory_block(void)
|
||||
{
|
||||
_lock_release(&memory_block_lock);
|
||||
portEXIT_CRITICAL(&memory_block_lock);
|
||||
}
|
||||
|
||||
static SemaphoreHandle_t sha_get_engine_state(esp_sha_type sha_type)
|
||||
@ -177,7 +176,7 @@ static bool esp_sha_lock_engine_common(esp_sha_type sha_type, TickType_t ticks_t
|
||||
return false;
|
||||
}
|
||||
|
||||
_lock_acquire(&engines_in_use_lock);
|
||||
portENTER_CRITICAL(&engines_in_use_lock);
|
||||
|
||||
if (engines_in_use == 0) {
|
||||
/* Just locked first engine,
|
||||
@ -191,7 +190,7 @@ static bool esp_sha_lock_engine_common(esp_sha_type sha_type, TickType_t ticks_t
|
||||
engines_in_use++;
|
||||
assert(engines_in_use <= 3);
|
||||
|
||||
_lock_release(&engines_in_use_lock);
|
||||
portEXIT_CRITICAL(&engines_in_use_lock);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -201,7 +200,7 @@ void esp_sha_unlock_engine(esp_sha_type sha_type)
|
||||
{
|
||||
SemaphoreHandle_t *engine_state = sha_get_engine_state(sha_type);
|
||||
|
||||
_lock_acquire(&engines_in_use_lock);
|
||||
portENTER_CRITICAL(&engines_in_use_lock);
|
||||
|
||||
engines_in_use--;
|
||||
|
||||
@ -211,7 +210,7 @@ void esp_sha_unlock_engine(esp_sha_type sha_type)
|
||||
periph_module_disable(PERIPH_SHA_MODULE);
|
||||
}
|
||||
|
||||
_lock_release(&engines_in_use_lock);
|
||||
portEXIT_CRITICAL(&engines_in_use_lock);
|
||||
|
||||
xSemaphoreGive(engine_state);
|
||||
}
|
||||
@ -238,6 +237,9 @@ void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state)
|
||||
}
|
||||
#endif
|
||||
|
||||
// preemptively do this before entering the critical section, then re-check once in it
|
||||
esp_sha_wait_idle();
|
||||
|
||||
esp_sha_lock_memory_block();
|
||||
|
||||
esp_sha_wait_idle();
|
||||
@ -270,6 +272,9 @@ void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_
|
||||
}
|
||||
#endif
|
||||
|
||||
// preemptively do this before entering the critical section, then re-check once in it
|
||||
esp_sha_wait_idle();
|
||||
|
||||
esp_sha_lock_memory_block();
|
||||
|
||||
esp_sha_wait_idle();
|
||||
|
@ -166,6 +166,8 @@ void esp_sha_unlock_engine(esp_sha_type sha_type);
|
||||
* while it is in use by the SHA engine. Caller should use esp_sha_wait_idle()
|
||||
* to ensure the SHA engine is not reading from the memory block in hardware.
|
||||
*
|
||||
* @note This function enters a critical section. Do not block while holding this lock.
|
||||
*
|
||||
* @note You do not need to lock the memory block before calling esp_sha_block() or esp_sha_read_digest_state(), these functions handle memory block locking internally.
|
||||
*
|
||||
* Call esp_sha_unlock_memory_block() when done.
|
||||
@ -177,6 +179,8 @@ void esp_sha_lock_memory_block(void);
|
||||
*
|
||||
* Caller should have already locked a SHA engine before calling this function.
|
||||
*
|
||||
* This function releases the critical section entered by esp_sha_lock_memory_block().
|
||||
*
|
||||
* Call following esp_sha_lock_memory_block().
|
||||
*/
|
||||
void esp_sha_unlock_memory_block(void);
|
||||
|
Loading…
x
Reference in New Issue
Block a user