Commit e3cc7a8c authored by Tom Burdick's avatar Tom Burdick Committed by Alberto Escolar
Browse files

pmci: mctp: Supply dedicated mctp heap



Unfortunately libc malloc/free are wrapped with a sys_mutex which is
troublesome for mctp as we likely want to allocate from interrupts in many
cases.

Supply mctp with a dedicated heap initialized and assigned at boot
saving applications a step. The default size is quite small and works
well so long as messages are small.

Signed-off-by: default avatarTom Burdick <thomas.burdick@intel.com>
parent 508676ca
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
zephyr_library()
zephyr_library_sources(mctp_memory.c)
zephyr_library_sources_ifdef(CONFIG_MCTP_UART mctp_uart.c)
+9 −0
Original line number Diff line number Diff line
@@ -9,6 +9,15 @@ menuconfig MCTP

if MCTP

config MCTP_HEAP_SIZE
	int "MCTP Heap Size"
	default 1024
	help
	  MCTP requires a heap for allocating packet buffers. A dedicated
	  heap is provided to MCTP at startup avoiding the need to specify
	  libmctp's allocation operations. This setting defines the size of
	  the dedicated MCTP heap in bytes. Defaults to 1KB for small packets.

config MCTP_UART
	bool "MCTP UART Binding"
	depends on UART_ASYNC_API
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2025 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 */

#include <zephyr/spinlock.h>
#include <zephyr/sys/sys_heap.h>
#include <libmctp.h>

static uint8_t MCTP_MEM[CONFIG_MCTP_HEAP_SIZE];

static struct {
	struct k_spinlock lock;
	struct sys_heap heap;
} mctp_heap;

static void *mctp_heap_alloc(size_t bytes)
{
	k_spinlock_key_t key = k_spin_lock(&mctp_heap.lock);

	void *ptr = sys_heap_alloc(&mctp_heap.heap, bytes);

	k_spin_unlock(&mctp_heap.lock, key);

	return ptr;
}

static void mctp_heap_free(void *ptr)
{
	k_spinlock_key_t key = k_spin_lock(&mctp_heap.lock);

	sys_heap_free(&mctp_heap.heap, ptr);

	k_spin_unlock(&mctp_heap.lock, key);
}

static void *mctp_heap_realloc(void *ptr, size_t bytes)
{
	k_spinlock_key_t key = k_spin_lock(&mctp_heap.lock);

	void *new_ptr = sys_heap_realloc(&mctp_heap.heap, ptr, bytes);

	k_spin_unlock(&mctp_heap.lock, key);

	return new_ptr;
}


static int mctp_heap_init(void)
{
	sys_heap_init(&mctp_heap.heap, MCTP_MEM, sizeof(MCTP_MEM));
	mctp_set_alloc_ops(mctp_heap_alloc, mctp_heap_free, mctp_heap_realloc);
	return 0;
}

SYS_INIT_NAMED(mctp_memory, mctp_heap_init, POST_KERNEL, 0);