diff --git a/main.c b/main.c index a80391af6df..14aa2945737 100644 --- a/main.c +++ b/main.c @@ -203,6 +203,9 @@ static void start_mp(safe_mode_t safe_mode) { #endif #if MICROPY_ENABLE_GC + #if CIRCUITPY_FAST_HEAP + port_vm_heap_prefers_dma_capable = false; + #endif size_t heap_size = 0; _heap = _allocate_memory(safe_mode, "CIRCUITPY_HEAP_START_SIZE", CIRCUITPY_HEAP_START_SIZE, &heap_size); gc_init(_heap, _heap + heap_size); diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index efb5f77e9ee..8979b49c9f2 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -76,6 +76,7 @@ endif ifeq ($(CHIP_VARIANT),RP2350) # RP2350 has PSRAM that is not DMA-capable CIRCUITPY_ALL_MEMORY_DMA_CAPABLE = 0 +CIRCUITPY_FAST_HEAP ?= 1 # Default PICODVI on because it doesn't require much code in RAM to talk to HSTX. CIRCUITPY_PICODVI ?= 1 diff --git a/ports/raspberrypi/supervisor/port.c b/ports/raspberrypi/supervisor/port.c index ff3ddb4cf91..6ff7fef9bf7 100644 --- a/ports/raspberrypi/supervisor/port.c +++ b/ports/raspberrypi/supervisor/port.c @@ -310,17 +310,17 @@ void port_heap_init(void) { } void *port_malloc(size_t size, bool dma_capable) { - // Prefer internal RAM for everything: it is much faster than PSRAM - // (data there skips the external bus and the shared XIP cache). PSRAM, when - // present, serves as spillover capacity for allocations that don't need DMA. - common_hal_mcu_disable_interrupts(); - void *block = tlsf_malloc(_heap, size); - common_hal_mcu_enable_interrupts(); - if (block == NULL && !dma_capable && _psram_size > 0) { + if (!dma_capable && _psram_size > 0) { common_hal_mcu_disable_interrupts(); - block = tlsf_malloc(_psram_heap, size); + void *block = tlsf_malloc(_psram_heap, size); common_hal_mcu_enable_interrupts(); + if (block) { + return block; + } } + common_hal_mcu_disable_interrupts(); + void *block = tlsf_malloc(_heap, size); + common_hal_mcu_enable_interrupts(); return block; } @@ -335,11 +335,7 @@ void port_free(void *ptr) { } void *port_realloc(void *ptr, size_t size, bool dma_capable) { - if (ptr == NULL) { - // Fresh allocation: same internal-first policy as port_malloc. - return port_malloc(size, dma_capable); - } - if (_psram_size > 0 && ((size_t)ptr) < SRAM_BASE) { + if (_psram_size > 0 && ((ptr != NULL && ((size_t)ptr) < SRAM_BASE) || (ptr == NULL && !dma_capable))) { common_hal_mcu_disable_interrupts(); void *block = tlsf_realloc(_psram_heap, ptr, size); common_hal_mcu_enable_interrupts(); diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 8649b3e65a2..b3d527a66c3 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -101,7 +101,7 @@ extern void common_hal_mcu_enable_interrupts(void); #define MICROPY_GC_ALLOC_THRESHOLD (0) #define MICROPY_GC_SPLIT_HEAP (1) #define MICROPY_GC_SPLIT_HEAP_AUTO (1) -#define MP_PLAT_ALLOC_HEAP(size) port_malloc(size, false) +#define MP_PLAT_ALLOC_HEAP(size) port_vm_heap_grow(size) #define MP_PLAT_FREE_HEAP(ptr) port_free(ptr) #include "supervisor/port_heap.h" #define MICROPY_HELPER_LEXER_UNIX (0) diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index e3ec3618e00..1e4e81e9d2f 100755 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -377,6 +377,10 @@ CFLAGS += -DCIRCUITPY_ESPCAMERA=$(CIRCUITPY_ESPCAMERA) CIRCUITPY__EVE ?= 0 CFLAGS += -DCIRCUITPY__EVE=$(CIRCUITPY__EVE) +# supervisor.runtime.fast_heap: a program can ask for the VM heap in DMA-capable RAM. +CIRCUITPY_FAST_HEAP ?= 0 +CFLAGS += -DCIRCUITPY_FAST_HEAP=$(CIRCUITPY_FAST_HEAP) + CIRCUITPY_FLOPPYIO ?= 0 CFLAGS += -DCIRCUITPY_FLOPPYIO=$(CIRCUITPY_FLOPPYIO) diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c index 3f75dad09a9..9903202b573 100644 --- a/shared-bindings/supervisor/Runtime.c +++ b/shared-bindings/supervisor/Runtime.c @@ -14,6 +14,7 @@ #include "shared-bindings/supervisor/Runtime.h" #include "shared-bindings/supervisor/SafeModeReason.h" +#include "supervisor/port_heap.h" #include "supervisor/shared/reload.h" #include "supervisor/shared/serial.h" #include "supervisor/shared/stack.h" @@ -266,6 +267,31 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_audio_obj, supervisor_runtime_g MP_PROPERTY_GETTER(supervisor_runtime_audio_obj, (mp_obj_t)&supervisor_runtime_get_audio_obj); +//| fast_heap: bool +//| """Whether the heap grows into internal RAM first on a board that also has PSRAM. +//| Internal RAM is faster, but displays, audio and other drivers need it for their +//| buffers. Create those first, then set this. Memory the heap already has stays where +//| it is, so set it early. It is ``False`` again when the next program starts. +//| +//| Not every board has this property, and it changes nothing on a board without PSRAM.""" +//| +#if CIRCUITPY_FAST_HEAP +static mp_obj_t supervisor_runtime_get_fast_heap(mp_obj_t self) { + return mp_obj_new_bool(port_vm_heap_prefers_dma_capable); +} +MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_fast_heap_obj, supervisor_runtime_get_fast_heap); + +static mp_obj_t supervisor_runtime_set_fast_heap(mp_obj_t self, mp_obj_t state_in) { + port_vm_heap_prefers_dma_capable = mp_obj_is_true(state_in); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(supervisor_runtime_set_fast_heap_obj, supervisor_runtime_set_fast_heap); + +MP_PROPERTY_GETSET(supervisor_runtime_fast_heap_obj, + (mp_obj_t)&supervisor_runtime_get_fast_heap_obj, + (mp_obj_t)&supervisor_runtime_set_fast_heap_obj); +#endif + static const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_usb_connected), MP_ROM_PTR(&supervisor_runtime_usb_connected_obj) }, { MP_ROM_QSTR(MP_QSTR_serial_connected), MP_ROM_PTR(&supervisor_runtime_serial_connected_obj) }, @@ -281,6 +307,9 @@ static const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_display), MP_ROM_NONE }, #endif { MP_ROM_QSTR(MP_QSTR_audio), MP_ROM_PTR(&supervisor_runtime_audio_obj) }, + #if CIRCUITPY_FAST_HEAP + { MP_ROM_QSTR(MP_QSTR_fast_heap), MP_ROM_PTR(&supervisor_runtime_fast_heap_obj) }, + #endif }; static MP_DEFINE_CONST_DICT(supervisor_runtime_locals_dict, supervisor_runtime_locals_dict_table); diff --git a/supervisor/port_heap.h b/supervisor/port_heap.h index ff37d02700d..689c90afbf4 100644 --- a/supervisor/port_heap.h +++ b/supervisor/port_heap.h @@ -33,4 +33,16 @@ void *port_realloc(void *ptr, size_t size, bool dma_capable); bool port_buffer_is_dma_capable(const void *ptr, size_t len); #endif +#if CIRCUITPY_FAST_HEAP +// While set, the VM heap grows into DMA-capable memory when that has room. A program sets it +// with supervisor.runtime.fast_heap. Cleared when the VM starts. +extern bool port_vm_heap_prefers_dma_capable; + +// Returns the next chunk of the VM heap. With the flag set it may also add a DMA-capable chunk +// to the heap itself, with gc_add(), so only the heap's own growth code may call it. +void *port_vm_heap_grow(size_t size); +#else +#define port_vm_heap_grow(size) port_malloc(size, false) +#endif + size_t port_heap_get_largest_free_size(void); diff --git a/supervisor/shared/port.c b/supervisor/shared/port.c index 757c03b2d39..8ab61e27e5b 100644 --- a/supervisor/shared/port.c +++ b/supervisor/shared/port.c @@ -65,6 +65,42 @@ MP_WEAK void port_free(void *ptr) { tlsf_free(heap, ptr); } +#if CIRCUITPY_FAST_HEAP +bool port_vm_heap_prefers_dma_capable = false; + +void *port_vm_heap_grow(size_t size) { + if (!port_vm_heap_prefers_dma_capable) { + return port_malloc(size, false); + } + void *chunk = port_malloc(size, true); + if (chunk != NULL) { + return chunk; + } + chunk = port_malloc(size, false); + if (chunk == NULL) { + return NULL; + } + // DMA-capable memory has no room for a chunk this big. Add the largest one it does have + // room for as well, found to within 1 kB, so that it fills up before `chunk` is used. + size_t largest = 0; + for (size_t step = size / 2; step >= 1024; step /= 2) { + void *probe = port_malloc(largest + step, true); + if (probe != NULL) { + port_free(probe); + largest += step; + } + } + // Anything smaller than the first chunk of the heap is not worth a chunk of its own. + if (largest >= CIRCUITPY_HEAP_START_SIZE) { + void *extra = port_malloc(largest, true); + if (extra != NULL) { + gc_add(extra, (uint8_t *)extra + largest); + } + } + return chunk; +} +#endif + // Safely free an object that may have been allocated from either the GC heap or port heap. // If the pointer is on the GC heap, it will be freed by the GC automatically, so we do nothing. // If the pointer is not on the GC heap (i.e., allocated with port_malloc), we free it with port_free.