Skip to content

Commit 43e2075

Browse files
committed
Add RP2 support for dma_capable
This places the VM heap in PSRAM by default. audio_dma now directly allocates its buffers to ensure they are dma_capable.
1 parent 63815c5 commit 43e2075

File tree

9 files changed

+44
-44
lines changed

9 files changed

+44
-44
lines changed

ports/espressif/supervisor/port.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,12 @@ void port_free(void *ptr) {
324324
heap_caps_free(ptr);
325325
}
326326

327-
void *port_realloc(void *ptr, size_t size) {
328-
return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT);
327+
void *port_realloc(void *ptr, size_t size, bool dma_capable) {
328+
size_t caps = MALLOC_CAP_8BIT;
329+
if (dma_capable) {
330+
caps |= MALLOC_CAP_DMA;
331+
}
332+
return heap_caps_realloc(ptr, size, caps);
329333
}
330334

331335
size_t port_heap_get_largest_free_size(void) {

ports/raspberrypi/audio_dma.c

+4-24
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,15 @@ audio_dma_result audio_dma_setup_playback(
227227
max_buffer_length /= dma->sample_spacing;
228228
}
229229

230-
dma->buffer[0] = (uint8_t *)m_realloc(dma->buffer[0],
231-
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
232-
dma->buffer_length[0], // Old size
233-
#endif
234-
max_buffer_length);
235-
230+
dma->buffer[0] = (uint8_t *)port_realloc(dma->buffer[0], max_buffer_length, true);
236231
dma->buffer_length[0] = max_buffer_length;
237232

238233
if (dma->buffer[0] == NULL) {
239234
return AUDIO_DMA_MEMORY_ERROR;
240235
}
241236

242237
if (!single_buffer) {
243-
dma->buffer[1] = (uint8_t *)m_realloc(dma->buffer[1],
244-
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
245-
dma->buffer_length[1], // Old size
246-
#endif
247-
max_buffer_length);
248-
238+
dma->buffer[1] = (uint8_t *)port_realloc(dma->buffer[1], max_buffer_length, true);
249239
dma->buffer_length[1] = max_buffer_length;
250240

251241
if (dma->buffer[1] == NULL) {
@@ -439,21 +429,11 @@ void audio_dma_init(audio_dma_t *dma) {
439429
}
440430

441431
void audio_dma_deinit(audio_dma_t *dma) {
442-
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
443-
m_free(dma->buffer[0], dma->buffer_length[0]);
444-
#else
445-
m_free(dma->buffer[0]);
446-
#endif
447-
432+
port_free(dma->buffer[0]);
448433
dma->buffer[0] = NULL;
449434
dma->buffer_length[0] = 0;
450435

451-
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
452-
m_free(dma->buffer[1], dma->buffer_length[1]);
453-
#else
454-
m_free(dma->buffer[1]);
455-
#endif
456-
436+
port_free(dma->buffer[1]);
457437
dma->buffer[1] = NULL;
458438
dma->buffer_length[1] = 0;
459439
}

ports/raspberrypi/audio_dma.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ typedef enum {
2020

2121
typedef struct {
2222
mp_obj_t sample;
23-
uint8_t *buffer[2];
23+
uint8_t *buffer[2]; // Allocated through port_malloc so they are dma-able
2424
size_t buffer_length[2];
2525
uint32_t channels_to_load_mask;
2626
uint32_t output_register_address;

ports/raspberrypi/supervisor/port.c

+28-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
// SPDX-License-Identifier: MIT
66

7+
#include <stddef.h>
78
#include <string.h>
89
#include <stdlib.h>
910

@@ -89,8 +90,7 @@ extern uint32_t _ld_itcm_size;
8990
extern uint32_t _ld_itcm_flash_copy;
9091

9192
static tlsf_t _heap = NULL;
92-
static pool_t _ram_pool = NULL;
93-
static pool_t _psram_pool = NULL;
93+
static tlsf_t _psram_heap = NULL;
9494
static size_t _psram_size = 0;
9595

9696
#ifdef CIRCUITPY_PSRAM_CHIP_SELECT
@@ -245,10 +245,9 @@ static void _port_heap_init(void) {
245245
uint32_t *heap_bottom = port_heap_get_bottom();
246246
uint32_t *heap_top = port_heap_get_top();
247247
size_t size = (heap_top - heap_bottom) * sizeof(uint32_t);
248-
_heap = tlsf_create_with_pool(heap_bottom, size, 64 * 1024 * 1024);
249-
_ram_pool = tlsf_get_pool(_heap);
248+
_heap = tlsf_create_with_pool(heap_bottom, size, size);
250249
if (_psram_size > 0) {
251-
_psram_pool = tlsf_add_pool(_heap, (void *)0x11000000, _psram_size);
250+
_psram_heap = tlsf_create_with_pool((void *)0x11000000, _psram_size, _psram_size);
252251
}
253252
}
254253

@@ -257,15 +256,31 @@ void port_heap_init(void) {
257256
}
258257

259258
void *port_malloc(size_t size, bool dma_capable) {
259+
if (!dma_capable && _psram_size > 0) {
260+
void *block = tlsf_malloc(_psram_heap, size);
261+
if (block) {
262+
return block;
263+
}
264+
}
260265
void *block = tlsf_malloc(_heap, size);
261266
return block;
262267
}
263268

264269
void port_free(void *ptr) {
265-
tlsf_free(_heap, ptr);
270+
if (((size_t)ptr) < SRAM_BASE) {
271+
tlsf_free(_psram_heap, ptr);
272+
} else {
273+
tlsf_free(_heap, ptr);
274+
}
266275
}
267276

268-
void *port_realloc(void *ptr, size_t size) {
277+
void *port_realloc(void *ptr, size_t size, bool dma_capable) {
278+
if (_psram_size > 0 && ((ptr != NULL && ((size_t)ptr) < SRAM_BASE) || (ptr == NULL && !dma_capable))) {
279+
void *block = tlsf_realloc(_psram_heap, ptr, size);
280+
if (block) {
281+
return block;
282+
}
283+
}
269284
return tlsf_realloc(_heap, ptr, size);
270285
}
271286

@@ -279,12 +294,13 @@ static bool max_size_walker(void *ptr, size_t size, int used, void *user) {
279294

280295
size_t port_heap_get_largest_free_size(void) {
281296
size_t max_size = 0;
282-
tlsf_walk_pool(_ram_pool, max_size_walker, &max_size);
283-
if (_psram_pool != NULL) {
284-
tlsf_walk_pool(_psram_pool, max_size_walker, &max_size);
297+
tlsf_walk_pool(tlsf_get_pool(_heap), max_size_walker, &max_size);
298+
max_size = tlsf_fit_size(_heap, max_size);
299+
if (_psram_heap != NULL) {
300+
tlsf_walk_pool(tlsf_get_pool(_psram_heap), max_size_walker, &max_size);
301+
max_size = tlsf_fit_size(_psram_heap, max_size);
285302
}
286-
// IDF does this. Not sure why.
287-
return tlsf_fit_size(_heap, max_size);
303+
return max_size;
288304
}
289305

290306
safe_mode_t port_init(void) {

ports/stm/supervisor/port.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ void port_free(void *ptr) {
227227
tlsf_free(_heap, ptr);
228228
}
229229

230-
void *port_realloc(void *ptr, size_t size) {
230+
void *port_realloc(void *ptr, size_t size, bool dma_capable) {
231231
return tlsf_realloc(_heap, ptr, size);
232232
}
233233
#endif

ports/zephyr-cp/supervisor/port.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void port_free(void *ptr) {
151151
tlsf_free(heap, ptr);
152152
}
153153

154-
void *port_realloc(void *ptr, size_t size) {
154+
void *port_realloc(void *ptr, size_t size, bool dma_capable) {
155155
return tlsf_realloc(heap, ptr, size);
156156
}
157157

shared-module/bitmapfilter/__init__.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void *scratchpad_alloc(size_t sz) {
4848
} else {
4949
if (scratchpad) {
5050
if (sz > scratchpad_size) {
51-
void *tmp = port_realloc(scratchpad, sz);
51+
void *tmp = port_realloc(scratchpad, sz, false);
5252
if (!tmp) {
5353
port_free(scratchpad);
5454
scratchpad = NULL;

supervisor/port_heap.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ void *port_malloc(size_t size, bool dma_capable);
2424

2525
void port_free(void *ptr);
2626

27-
void *port_realloc(void *ptr, size_t size);
27+
void *port_realloc(void *ptr, size_t size, bool dma_capable);
2828

2929
size_t port_heap_get_largest_free_size(void);

supervisor/shared/port.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ MP_WEAK void port_free(void *ptr) {
4242
tlsf_free(heap, ptr);
4343
}
4444

45-
MP_WEAK void *port_realloc(void *ptr, size_t size) {
45+
MP_WEAK void *port_realloc(void *ptr, size_t size, bool dma_capable) {
4646
return tlsf_realloc(heap, ptr, size);
4747
}
4848

0 commit comments

Comments
 (0)