#ifndef DMA_H
#define DMA_H

#include <stdint.h>

/* DMA page allocator — allocates physical pages below 4GB for device DMA.
 * Uses a fixed region starting at DMA_BASE (16MB) with identity mapping.
 * virt == phys for all DMA memory. */

#define DMA_BASE        0x1000000   /* 16 MB */
#define DMA_SIZE        0x1000000   /* 16 MB (total 4096 pages) */
#define DMA_PAGE_SIZE   4096

/* Initialize DMA page allocator */
void dma_init(void);

/* Allocate contiguous physical pages. Returns virtual address (== phys).
 * phys_out receives the physical address (same as return value). */
void *dma_alloc_pages(uint32_t num_pages, uint32_t *phys_out);

/* Allocate a single page */
void *dma_alloc_page(uint32_t *phys_out);

/* Free previously allocated pages */
void dma_free(void *addr, uint32_t num_pages);

/* Allocate contiguous memory (any size, rounded up to pages) */
void *dma_alloc(uint32_t size, uint32_t *phys_out);

/* Free contiguous memory */
void dma_free_size(void *addr, uint32_t size);

#endif
