Skip to content

Commit e113696

Browse files
committed
esp32: Flush D-cache on targets that can execute from normal RAM.
This is necessary to get native code running on the ESP32-P4. Signed-off-by: Damien George <[email protected]>
1 parent 331caee commit e113696

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

ports/esp32/esp32_common.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ list(APPEND IDF_COMPONENTS
166166
driver
167167
esp_adc
168168
esp_app_format
169+
esp_mm
169170
esp_common
170171
esp_driver_touch_sens
171172
esp_eth

ports/esp32/main.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "freertos/FreeRTOS.h"
3636
#include "freertos/task.h"
37+
#include "esp_cache.h"
3738
#include "esp_system.h"
3839
#include "nvs_flash.h"
3940
#include "esp_task.h"
@@ -295,6 +296,20 @@ static void esp_native_code_free_all(void) {
295296
}
296297

297298
void *esp_native_code_commit(void *buf, size_t len, void *reloc) {
299+
#if SOC_CPU_IDRAM_SPLIT_USING_PMP && !CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT
300+
// On targets with this configuration all RAM is executable.
301+
// So just do the native relocation (if needed) and flush the D-cache.
302+
303+
if (reloc) {
304+
mp_native_relocate(reloc, buf, (uintptr_t)buf);
305+
}
306+
esp_cache_msync(buf, len, ESP_CACHE_MSYNC_FLAG_UNALIGNED | ESP_CACHE_MSYNC_FLAG_DIR_C2M);
307+
return buf;
308+
309+
#else
310+
// On targets with this configuration only certain RAM is executable.
311+
// So reallocate executable RAM for the native code and copy it across.
312+
298313
len = (len + 3) & ~3;
299314
size_t len_node = sizeof(native_code_node_t) + len;
300315
native_code_node_t *node = heap_caps_malloc(len_node, MALLOC_CAP_EXEC);
@@ -316,4 +331,6 @@ void *esp_native_code_commit(void *buf, size_t len, void *reloc) {
316331
}
317332
memcpy(p, buf, len);
318333
return p;
334+
335+
#endif
319336
}

ports/esp32/mpconfigport.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,8 @@
289289
// type definitions for the specific machine
290290

291291
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p)))
292-
#if SOC_CPU_IDRAM_SPLIT_USING_PMP && !CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT
293-
// On targets with this configuration all RAM is executable so no need for a custom commit function.
294-
#else
295292
void *esp_native_code_commit(void *, size_t, void *);
296293
#define MP_PLAT_COMMIT_EXEC(buf, len, reloc) esp_native_code_commit(buf, len, reloc)
297-
#endif
298294
#define MP_SSIZE_MAX (0x7fffffff)
299295

300296
#if MICROPY_PY_SOCKET_EVENTS

0 commit comments

Comments
 (0)