2023-09-08 02:54:45 -04:00
|
|
|
/*
|
2024-01-02 00:09:56 -05:00
|
|
|
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
2023-09-08 02:54:45 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_check.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_heap_caps.h"
|
2023-10-10 03:06:54 -04:00
|
|
|
#include "esp_memory_utils.h"
|
2023-09-08 02:54:45 -04:00
|
|
|
#include "esp_dma_utils.h"
|
|
|
|
#include "esp_private/esp_cache_private.h"
|
|
|
|
#include "soc/soc_caps.h"
|
2024-01-24 00:28:15 -05:00
|
|
|
#include "hal/hal_utils.h"
|
2023-09-08 02:54:45 -04:00
|
|
|
|
|
|
|
static const char *TAG = "dma_utils";
|
|
|
|
_Static_assert(ESP_DMA_MALLOC_FLAG_PSRAM == ESP_CACHE_MALLOC_FLAG_PSRAM);
|
|
|
|
|
|
|
|
#define ALIGN_UP_BY(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
|
2024-01-24 00:28:15 -05:00
|
|
|
#define ALIGN_DOWN_BY(num, align) ((num) & (~((align) - 1)))
|
2023-09-08 02:54:45 -04:00
|
|
|
|
2024-01-24 00:28:15 -05:00
|
|
|
esp_err_t esp_dma_capable_malloc(size_t size, const esp_dma_mem_info_t *dma_mem_info, void **out_ptr, size_t *actual_size)
|
|
|
|
{
|
|
|
|
ESP_RETURN_ON_FALSE_ISR(dma_mem_info && out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
|
2023-09-08 02:54:45 -04:00
|
|
|
|
2024-03-28 23:36:27 -04:00
|
|
|
size_t alignment_bytes = 0;
|
2024-01-24 00:28:15 -05:00
|
|
|
|
|
|
|
//dma align
|
2024-03-28 23:36:27 -04:00
|
|
|
size_t dma_alignment_bytes = dma_mem_info->dma_alignment_bytes;
|
2024-01-24 00:28:15 -05:00
|
|
|
|
|
|
|
//cache align
|
|
|
|
int cache_flags = 0;
|
2024-03-28 23:36:27 -04:00
|
|
|
size_t cache_alignment_bytes = 0;
|
|
|
|
|
|
|
|
int heap_caps = dma_mem_info->extra_heap_caps | MALLOC_CAP_DMA;
|
|
|
|
if (dma_mem_info->extra_heap_caps & MALLOC_CAP_SPIRAM) {
|
2024-01-24 00:28:15 -05:00
|
|
|
cache_flags |= ESP_DMA_MALLOC_FLAG_PSRAM;
|
2024-03-28 23:36:27 -04:00
|
|
|
heap_caps = dma_mem_info->extra_heap_caps | MALLOC_CAP_SPIRAM;
|
2024-01-24 00:28:15 -05:00
|
|
|
}
|
2024-03-28 23:36:27 -04:00
|
|
|
|
|
|
|
esp_err_t ret = esp_cache_get_alignment(cache_flags, &cache_alignment_bytes);
|
2024-01-24 00:28:15 -05:00
|
|
|
assert(ret == ESP_OK);
|
|
|
|
|
2024-03-28 23:36:27 -04:00
|
|
|
//Get the least common multiple of two alignment
|
|
|
|
alignment_bytes = hal_utils_calc_lcm(dma_alignment_bytes, cache_alignment_bytes);
|
2024-01-24 00:28:15 -05:00
|
|
|
|
|
|
|
//malloc
|
2024-03-28 23:36:27 -04:00
|
|
|
size = ALIGN_UP_BY(size, alignment_bytes);
|
|
|
|
void *ptr = heap_caps_aligned_alloc(alignment_bytes, size, heap_caps);
|
|
|
|
ESP_RETURN_ON_FALSE_ISR(ptr, ESP_ERR_NO_MEM, TAG, "Not enough heap memory");
|
2024-01-24 00:28:15 -05:00
|
|
|
|
|
|
|
*out_ptr = ptr;
|
|
|
|
if (actual_size) {
|
|
|
|
*actual_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2024-03-28 23:36:27 -04:00
|
|
|
esp_err_t esp_dma_capable_calloc(size_t calloc_num, size_t size, const esp_dma_mem_info_t *dma_mem_info, void **out_ptr, size_t *actual_size)
|
2023-09-08 02:54:45 -04:00
|
|
|
{
|
2024-01-24 00:28:15 -05:00
|
|
|
esp_err_t ret = ESP_FAIL;
|
|
|
|
size_t size_bytes = 0;
|
|
|
|
bool ovf = false;
|
2023-09-08 02:54:45 -04:00
|
|
|
|
2024-03-28 23:36:27 -04:00
|
|
|
ovf = __builtin_mul_overflow(calloc_num, size, &size_bytes);
|
2024-01-24 00:28:15 -05:00
|
|
|
ESP_RETURN_ON_FALSE_ISR(!ovf, ESP_ERR_INVALID_ARG, TAG, "wrong size, total size overflow");
|
2023-09-08 02:54:45 -04:00
|
|
|
|
2024-01-24 00:28:15 -05:00
|
|
|
void *ptr = NULL;
|
|
|
|
ret = esp_dma_capable_malloc(size_bytes, dma_mem_info, &ptr, actual_size);
|
|
|
|
if (ret == ESP_OK) {
|
|
|
|
memset(ptr, 0, size_bytes);
|
2023-09-08 02:54:45 -04:00
|
|
|
*out_ptr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-01-24 00:28:15 -05:00
|
|
|
static bool s_buf_in_region(const void *ptr, size_t size, esp_dma_buf_location_t location)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
if (location == ESP_DMA_BUF_LOCATION_INTERNAL) {
|
|
|
|
if (esp_ptr_dma_capable(ptr) && esp_ptr_dma_capable(ptr + size - 1)) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
} else if (location == ESP_DMA_BUF_LOCATION_PSRAM) {
|
|
|
|
#if SOC_PSRAM_DMA_CAPABLE
|
|
|
|
if (esp_ptr_external_ram(ptr) && esp_ptr_external_ram(ptr + size - 1)) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool s_is_buf_aligned(intptr_t ptr, size_t alignment)
|
|
|
|
{
|
|
|
|
return (ptr % alignment == 0);
|
|
|
|
}
|
|
|
|
|
2024-03-28 23:36:27 -04:00
|
|
|
bool esp_dma_is_buffer_alignment_satisfied(const void *ptr, size_t size, esp_dma_mem_info_t dma_mem_info)
|
2024-01-24 00:28:15 -05:00
|
|
|
{
|
|
|
|
assert(ptr);
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
for (int i = ESP_DMA_BUF_LOCATION_INTERNAL; i < ESP_DMA_BUF_LOCATION_AUTO; i++) {
|
|
|
|
if (s_buf_in_region(ptr, size, i)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-28 23:36:27 -04:00
|
|
|
size_t alignment_bytes = 0;
|
2024-01-24 00:28:15 -05:00
|
|
|
|
|
|
|
//dma align
|
2024-03-28 23:36:27 -04:00
|
|
|
size_t dma_alignment_bytes = dma_mem_info.dma_alignment_bytes;
|
2024-01-24 00:28:15 -05:00
|
|
|
|
|
|
|
//cache align
|
|
|
|
int cache_flags = 0;
|
2024-03-28 23:36:27 -04:00
|
|
|
size_t cache_alignment_bytes = 0;
|
|
|
|
if (esp_ptr_external_ram(ptr)) {
|
2024-01-24 00:28:15 -05:00
|
|
|
cache_flags |= ESP_DMA_MALLOC_FLAG_PSRAM;
|
|
|
|
}
|
2024-03-28 23:36:27 -04:00
|
|
|
esp_err_t ret = esp_cache_get_alignment(cache_flags, &cache_alignment_bytes);
|
2024-01-24 00:28:15 -05:00
|
|
|
assert(ret == ESP_OK);
|
|
|
|
|
2024-03-28 23:36:27 -04:00
|
|
|
//Get the least common multiple of two alignment
|
|
|
|
alignment_bytes = hal_utils_calc_lcm(dma_alignment_bytes, cache_alignment_bytes);
|
2024-01-24 00:28:15 -05:00
|
|
|
|
2024-03-28 23:36:27 -04:00
|
|
|
bool is_aligned = s_is_buf_aligned((intptr_t)ptr, alignment_bytes) && s_is_buf_aligned((intptr_t)size, alignment_bytes);
|
2024-01-24 00:28:15 -05:00
|
|
|
return is_aligned;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------Deprecated APIs-----------------------//
|
|
|
|
esp_err_t s_legacy_malloc(size_t size, uint32_t flags, void **out_ptr, size_t *actual_size)
|
|
|
|
{
|
|
|
|
ESP_RETURN_ON_FALSE_ISR(out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
|
|
|
|
|
|
|
|
int heap_caps = 0;
|
|
|
|
if (flags & ESP_DMA_MALLOC_FLAG_PSRAM) {
|
|
|
|
heap_caps |= MALLOC_CAP_SPIRAM;
|
|
|
|
} else {
|
|
|
|
heap_caps |= MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_dma_mem_info_t dma_mem_info = {
|
2024-03-28 23:36:27 -04:00
|
|
|
.extra_heap_caps = heap_caps,
|
|
|
|
.dma_alignment_bytes = 4, //legacy API behaviour is only check max dma buffer alignment
|
2024-01-24 00:28:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
ESP_RETURN_ON_ERROR_ISR(esp_dma_capable_malloc(size, &dma_mem_info, out_ptr, actual_size), TAG, "failed to do malloc");
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_dma_malloc(size_t size, uint32_t flags, void **out_ptr, size_t *actual_size)
|
|
|
|
{
|
|
|
|
return s_legacy_malloc(size, flags, out_ptr, actual_size);
|
|
|
|
}
|
2023-09-08 02:54:45 -04:00
|
|
|
|
|
|
|
esp_err_t esp_dma_calloc(size_t n, size_t size, uint32_t flags, void **out_ptr, size_t *actual_size)
|
|
|
|
{
|
|
|
|
ESP_RETURN_ON_FALSE_ISR(out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
|
|
|
|
|
|
|
|
esp_err_t ret = ESP_FAIL;
|
|
|
|
size_t size_bytes = 0;
|
|
|
|
bool ovf = false;
|
|
|
|
|
|
|
|
ovf = __builtin_mul_overflow(n, size, &size_bytes);
|
|
|
|
ESP_RETURN_ON_FALSE_ISR(!ovf, ESP_ERR_INVALID_ARG, TAG, "wrong size, total size overflow");
|
|
|
|
|
|
|
|
void *ptr = NULL;
|
2024-01-24 00:28:15 -05:00
|
|
|
ret = s_legacy_malloc(size_bytes, flags, &ptr, actual_size);
|
2023-09-08 02:54:45 -04:00
|
|
|
if (ret == ESP_OK) {
|
|
|
|
memset(ptr, 0, size_bytes);
|
|
|
|
*out_ptr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2023-10-10 03:06:54 -04:00
|
|
|
|
2024-01-24 00:28:15 -05:00
|
|
|
static bool s_buf_in_region_legacy(const void *ptr, size_t size, esp_dma_buf_location_t location, int *heap_caps)
|
2024-01-02 00:09:56 -05:00
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
if (location == ESP_DMA_BUF_LOCATION_INTERNAL) {
|
|
|
|
if (esp_ptr_dma_capable(ptr) && esp_ptr_dma_capable(ptr + size - 1)) {
|
2024-01-24 00:28:15 -05:00
|
|
|
*heap_caps = MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL;
|
2024-01-02 00:09:56 -05:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
} else if (location == ESP_DMA_BUF_LOCATION_PSRAM) {
|
|
|
|
#if SOC_PSRAM_DMA_CAPABLE
|
|
|
|
if (esp_ptr_external_ram(ptr) && esp_ptr_external_ram(ptr + size - 1)) {
|
2024-01-24 00:28:15 -05:00
|
|
|
*heap_caps = MALLOC_CAP_SPIRAM;
|
2024-01-02 00:09:56 -05:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2023-10-10 03:06:54 -04:00
|
|
|
bool esp_dma_is_buffer_aligned(const void *ptr, size_t size, esp_dma_buf_location_t location)
|
|
|
|
{
|
|
|
|
assert(ptr);
|
|
|
|
|
2024-01-02 00:09:56 -05:00
|
|
|
bool found = false;
|
2024-01-24 00:28:15 -05:00
|
|
|
int heap_caps = 0;
|
2024-01-02 00:09:56 -05:00
|
|
|
if (location == ESP_DMA_BUF_LOCATION_AUTO) {
|
|
|
|
for (int i = ESP_DMA_BUF_LOCATION_INTERNAL; i < ESP_DMA_BUF_LOCATION_AUTO; i++) {
|
2024-01-24 00:28:15 -05:00
|
|
|
if (s_buf_in_region_legacy(ptr, size, i, &heap_caps)) {
|
2024-01-02 00:09:56 -05:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-10 03:06:54 -04:00
|
|
|
}
|
2024-01-02 00:09:56 -05:00
|
|
|
} else if (location == ESP_DMA_BUF_LOCATION_INTERNAL) {
|
2024-01-24 00:28:15 -05:00
|
|
|
found = s_buf_in_region_legacy(ptr, size, ESP_DMA_BUF_LOCATION_INTERNAL, &heap_caps);
|
2023-10-10 03:06:54 -04:00
|
|
|
} else {
|
2024-01-24 00:28:15 -05:00
|
|
|
found = s_buf_in_region_legacy(ptr, size, ESP_DMA_BUF_LOCATION_PSRAM, &heap_caps);
|
2024-01-02 00:09:56 -05:00
|
|
|
}
|
|
|
|
if (!found) {
|
2023-10-10 03:06:54 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-24 00:28:15 -05:00
|
|
|
esp_dma_mem_info_t dma_mem_info = {
|
2024-03-28 23:36:27 -04:00
|
|
|
.extra_heap_caps = heap_caps,
|
|
|
|
.dma_alignment_bytes = 4, //legacy API behaviour is only check max dma buffer alignment
|
2024-01-24 00:28:15 -05:00
|
|
|
};
|
2024-03-28 23:36:27 -04:00
|
|
|
return esp_dma_is_buffer_alignment_satisfied(ptr, size, dma_mem_info);
|
2023-10-10 03:06:54 -04:00
|
|
|
}
|