xref: /rk3399_ARM-atf/plat/arm/common/plat_rmm_mem_carveout.c (revision aed7dc81a1d20fc7cbb5d96f73ec9f6979ccdf5c)
1*745c129aSAndre Przywara /*
2*745c129aSAndre Przywara  * Copyright (c) 2025, Arm Limited and Contributors. All rights reserved.
3*745c129aSAndre Przywara  *
4*745c129aSAndre Przywara  * SPDX-License-Identifier: BSD-3-Clause
5*745c129aSAndre Przywara  */
6*745c129aSAndre Przywara 
7*745c129aSAndre Przywara #include <lib/spinlock.h>
8*745c129aSAndre Przywara #include <plat/common/platform.h>
9*745c129aSAndre Przywara 
10*745c129aSAndre Przywara static spinlock_t mem_reserve_lock;
11*745c129aSAndre Przywara static uintptr_t top_mem = RMM_LIMIT;
12*745c129aSAndre Przywara 
plat_rmmd_reserve_memory(size_t size,unsigned long alignment)13*745c129aSAndre Przywara uintptr_t plat_rmmd_reserve_memory(size_t size, unsigned long alignment)
14*745c129aSAndre Przywara {
15*745c129aSAndre Przywara 	uint64_t align_mask = alignment - 1;
16*745c129aSAndre Przywara 	uintptr_t addr;
17*745c129aSAndre Przywara 
18*745c129aSAndre Przywara 	spin_lock(&mem_reserve_lock);
19*745c129aSAndre Przywara 	addr = (top_mem - size) & ~align_mask;
20*745c129aSAndre Przywara 	if (addr >= RMM_PAYLOAD_LIMIT) {
21*745c129aSAndre Przywara 		top_mem = addr;
22*745c129aSAndre Przywara 	} else {
23*745c129aSAndre Przywara 		addr = 0;
24*745c129aSAndre Przywara 	}
25*745c129aSAndre Przywara 	spin_unlock(&mem_reserve_lock);
26*745c129aSAndre Przywara 
27*745c129aSAndre Przywara 	return addr;
28*745c129aSAndre Przywara }
29