Notes of kernel hacking.

Modifications on Kernel Side

DTS:

...
	reserved-memory {
		#address-cells = <2>;
		#size-cells = <2>;
		ranges;

		...

		memorypool@40000000 {
			compatible = "memory-pool";
			reusable;
			reg = <0x0 0x40000000 0x0 0x00400000>;
		};
	};
...

kernel/dma/contiguous.c:

...
RESERVEDMEM_OF_DECLARE(cma_shadow, "memory-pool", rmem_cma_setup);

Allocation from the cma memory pool:

struct page *cma_pages;

cma_pages = dma_alloc_from_contiguous(NULL, (NUM_SHADOW_MMIO * SHADOW_MMIO_SIZE) >> PAGE_SHIFT, 0, false);
if (!cma_pages) {
	printk(KERN_ERR "Failed to allocate memory for mmio region\n");
	return;
}
printk("cma_pages.addr: 0x%lx\n", (unsigned long)page_to_phys(cma_pages));

Modifications on ATF Side

Add a static memory mapping:

#define MEMPOOL_MEM MAP_REGION_FLAT(MEMPOOL_BASE,		\
					MEMPOOL_SIZE,		\
					MT_MEMORY | MT_RW | MT_NS)

#ifdef IMAGE_BL31
const mmap_region_t bl_regions[] = {
	...
	MEMPOOL_MEM,
	{0}
};
#endif

References

CCS2022 - StrongBox