From 11dd858e1d9da71a182bc707ca3a481dafbccbe4 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sun, 27 Apr 2025 19:51:43 +0200 Subject: dev, kernel: refactor and improve parts of the kernel, related to I/O Signed-off-by: Amlal --- dev/kernel/StorageKit/ATA.h | 4 +- dev/kernel/StorageKit/DMA.h | 71 --------------------------------- dev/kernel/StorageKit/DmaPool.h | 80 ++++++++++++++++++++++++++++++++++++++ dev/kernel/StorageKit/StorageKit.h | 2 +- 4 files changed, 83 insertions(+), 74 deletions(-) delete mode 100644 dev/kernel/StorageKit/DMA.h create mode 100644 dev/kernel/StorageKit/DmaPool.h (limited to 'dev/kernel/StorageKit') diff --git a/dev/kernel/StorageKit/ATA.h b/dev/kernel/StorageKit/ATA.h index d2950228..5887c579 100644 --- a/dev/kernel/StorageKit/ATA.h +++ b/dev/kernel/StorageKit/ATA.h @@ -47,10 +47,10 @@ class ATADeviceInterface : public IDeviceObject { /// @brief Initialize an PIO device (StorageKit function) /// @param is_master is the current PIO master? /// @return [io:master] for PIO device. -BOOL sk_init_pio_device(BOOL is_master, UInt16& io, UInt8& master); +BOOL sk_init_ata_device(BOOL is_master, UInt16& io, UInt8& master); /// @brief Acquires a new PIO device with drv_index in mind. /// @param drv_index The drive index to assign. /// @return A wrapped device interface if successful, or error code. -ErrorOr sk_acquire_pio_device(Int32 drv_index); +ErrorOr sk_acquire_ata_device(Int32 drv_index); } // namespace Kernel diff --git a/dev/kernel/StorageKit/DMA.h b/dev/kernel/StorageKit/DMA.h deleted file mode 100644 index c572ffa3..00000000 --- a/dev/kernel/StorageKit/DMA.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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 -#include - -#ifdef __NE_AMD64__ -#define DMA_POOL_START (0x1000000) -#define DMA_POOL_SIZE (0x1000000) - -namespace Kernel { -inline UInt8* kDmaPoolPtr = (UInt8*) DMA_POOL_START; - -inline VoidPtr rtl_dma_alloc(SizeT size, SizeT align) { - if (!size) { - return nullptr; - } - - UIntPtr addr = (UIntPtr) kDmaPoolPtr; - - addr = (addr + (align - 1)) & ~(align - 1); // Align up - - if (addr + size >= DMA_POOL_START + 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(Void* ptr, SizeT size_buffer) { - if (ptr > (Void*) (DMA_POOL_START + DMA_POOL_SIZE)) { - 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 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 + +#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 diff --git a/dev/kernel/StorageKit/StorageKit.h b/dev/kernel/StorageKit/StorageKit.h index 9dbb014d..adb9f5dc 100644 --- a/dev/kernel/StorageKit/StorageKit.h +++ b/dev/kernel/StorageKit/StorageKit.h @@ -8,7 +8,7 @@ #define kDriveSectorSizeHDD (512U) #define kDriveSectorSizeSSD (512U) -#define kDriveSectorSizeOptical (2048) +#define kDriveSectorSizeOptical (2048U) namespace Kernel { template -- cgit v1.2.3