Skip to content
3 changes: 3 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ports/raspberrypi/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions ports/raspberrypi/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion py/circuitpy_mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 29 additions & 0 deletions shared-bindings/supervisor/Runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) },
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions supervisor/port_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
36 changes: 36 additions & 0 deletions supervisor/shared/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading