NeKernel dev
Loading...
Searching...
No Matches
DmaPool.h
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2025, Amlal El Mahrouss , licensed under the Apache 2.0 license.
4
5 File: DmaPool.h
6 Purpose: Dma Pool Manager.
7
8======================================== */
9
10#pragma once
11
13
14#ifdef __NE_AMD64__
16#define kNeDMAPoolStart (0x1000000)
17#define kNeDMAPoolSize (0x1000000)
18#elif defined(__NE_ARM64__)
20
22#define kNeDMAPoolStart (0x1000000)
23#define kNeDMAPoolSize (0x1000000)
24#endif
25
26#define kNeDMABestAlign (8)
27
28namespace Kernel {
30inline UInt8* kDmaPoolPtr = (UInt8*) kNeDMAPoolStart;
31inline const UInt8* kDmaPoolEnd = (UInt8*) (kNeDMAPoolStart + kNeDMAPoolSize);
32
33/***********************************************************************************/
37/***********************************************************************************/
38inline VoidPtr rtl_dma_alloc(SizeT size, SizeT align) {
39 if (!size) {
40 return nullptr;
41 }
42
44 if ((align % kNeDMABestAlign) != 0) {
45 return nullptr;
46 }
47
49
52 addr = (addr + (align - 1)) & ~(align - 1);
53
54 if ((addr + size) > reinterpret_cast<UIntPtr>(kDmaPoolEnd)) {
56 return nullptr;
57 }
58
59 kDmaPoolPtr = (UInt8*) (addr + size);
60
62
63 return (VoidPtr) addr;
64}
65
66/***********************************************************************************/
68/***********************************************************************************/
69inline Void rtl_dma_free(SizeT size) {
70 if (!size) return;
71
72 auto ptr = (kDmaPoolPtr - size);
73
74 if (!ptr || ptr < (UInt8*) kNeDMAPoolStart) {
76 return;
77 }
78
79 kDmaPoolPtr = ptr;
80
82}
83
84/***********************************************************************************/
86/***********************************************************************************/
87inline Void rtl_dma_flush(VoidPtr ptr, SizeT size_buffer) {
88 if (ptr > kDmaPoolEnd) {
89 return;
90 }
91
92 if (!ptr || ptr < (UInt8*) kNeDMAPoolStart) {
94 return;
95 }
96
97 for (SizeT buf_idx = 0UL; buf_idx < size_buffer; ++buf_idx) {
98 HAL::mm_memory_fence((VoidPtr) ((UInt8*) ptr + buf_idx));
99 }
100}
101} // namespace Kernel
#define kNeDMABestAlign
Definition DmaPool.h:26
#define err_global_get()
Definition KPC.h:25
EXTERN_C Int32 mm_memory_fence(VoidPtr virtual_address)
clflush+mfence helper function.
Definition HalPagingMgr.cc:104
UPS inline definitions.
Definition Device.h:12
void Void
Definition Config.h:87
__SIZE_TYPE__ SizeT
Definition Config.h:60
constexpr KPCError kErrorDmaExhausted
Definition KPC.h:66
__UINT8_TYPE__ UInt8
Definition Config.h:55
void * VoidPtr
Definition Config.h:33
UInt8 * kDmaPoolPtr
DMA pool base pointer, here we're sure that AHCI or whatever tricky standard sees it.
Definition DmaPool.h:30
const UInt8 * kDmaPoolEnd
Definition DmaPool.h:31
Void rtl_dma_free(SizeT size)
Free DMA pointer.
Definition DmaPool.h:69
VoidPtr rtl_dma_alloc(SizeT size, SizeT align)
allocate from the rtl_dma_alloc system.
Definition DmaPool.h:38
Void rtl_dma_flush(VoidPtr ptr, SizeT size_buffer)
Flush DMA pointer.
Definition DmaPool.h:87
__UINTPTR_TYPE__ UIntPtr
Definition Config.h:62