summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/StorageKit/DmaPool.h
diff options
context:
space:
mode:
authorAmlal <amlal@nekernel.org>2025-04-27 19:51:43 +0200
committerAmlal <amlal@nekernel.org>2025-04-27 19:51:43 +0200
commit11dd858e1d9da71a182bc707ca3a481dafbccbe4 (patch)
tree0b2ce000054b257e005199f31ac6a21a0eb5ffe5 /dev/kernel/StorageKit/DmaPool.h
parentcb2f383f45dda8d1cdcef0b87fe4c70243659701 (diff)
dev, kernel: refactor and improve parts of the kernel, related to I/O
Signed-off-by: Amlal <amlal@nekernel.org>
Diffstat (limited to 'dev/kernel/StorageKit/DmaPool.h')
-rw-r--r--dev/kernel/StorageKit/DmaPool.h80
1 files changed, 80 insertions, 0 deletions
diff --git a/dev/kernel/StorageKit/DmaPool.h b/dev/kernel/StorageKit/DmaPool.h
new file mode 100644
index 00000000..9e59910a
--- /dev/null
+++ b/dev/kernel/StorageKit/DmaPool.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2025 Amlal El Mahrouss. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <KernelKit/DebugOutput.h>
+
+#ifdef __NE_AMD64__
+#define NE_DMA_POOL_START (0x1000000)
+#define NE_DMA_POOL_SIZE (0x1000000)
+
+namespace Kernel {
+/// @brief DMA pool base pointer, here we're sure that AHCI or whatever tricky standard sees it.
+inline UInt8* kDmaPoolPtr = (UInt8*) NE_DMA_POOL_START;
+
+/// @brief allocate from the rtl_dma_alloc system.
+/// @param size the size of the chunk to allocate.
+/// @param align alignement of pointer.
+inline VoidPtr rtl_dma_alloc(SizeT size, SizeT align) {
+ if (!size) {
+ return nullptr;
+ }
+
+ UIntPtr addr = (UIntPtr) kDmaPoolPtr;
+
+ /// here we just align the address according to a `align` variable, i'd rather be a power of two really.
+ addr = (addr + (align - 1)) & ~(align - 1);
+
+ if ((addr + size) >= (NE_DMA_POOL_START + NE_DMA_POOL_SIZE)) {
+ kout << "DMA Pool exhausted!\r";
+
+ return nullptr;
+ }
+
+ kDmaPoolPtr = (UInt8*) (addr + size);
+ return (VoidPtr) addr;
+}
+
+inline Void rtl_dma_free(SizeT size) {
+ if (!size) return;
+
+ kDmaPoolPtr = (UInt8*) (kDmaPoolPtr - size);
+}
+
+inline Void rtl_dma_flush(VoidPtr ptr, SizeT size_buffer) {
+ if (ptr > (Void*) (NE_DMA_POOL_START + NE_DMA_POOL_SIZE)) {
+ return;
+ }
+
+ if (!ptr) {
+ return;
+ }
+
+ for (SizeT i = 0; i < size_buffer; ++i) {
+ asm volatile("clflush (%0)" : : "r"((UInt8*) ptr + i) : "memory");
+ }
+
+ asm volatile("mfence" ::: "memory");
+}
+} // namespace Kernel
+#endif \ No newline at end of file