diff options
Diffstat (limited to 'dev/kernel/StorageKit')
| -rw-r--r-- | dev/kernel/StorageKit/AHCI.h | 58 | ||||
| -rw-r--r-- | dev/kernel/StorageKit/ATA.h | 70 | ||||
| -rw-r--r-- | dev/kernel/StorageKit/DmaPool.h | 92 | ||||
| -rw-r--r-- | dev/kernel/StorageKit/NVME.h | 36 | ||||
| -rw-r--r-- | dev/kernel/StorageKit/PRDT.h | 43 | ||||
| -rw-r--r-- | dev/kernel/StorageKit/SCSI.h | 4 | ||||
| -rw-r--r-- | dev/kernel/StorageKit/StorageKit.h | 25 |
7 files changed, 205 insertions, 123 deletions
diff --git a/dev/kernel/StorageKit/AHCI.h b/dev/kernel/StorageKit/AHCI.h index 68a42c46..d40feb36 100644 --- a/dev/kernel/StorageKit/AHCI.h +++ b/dev/kernel/StorageKit/AHCI.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -10,42 +10,40 @@ #include <KernelKit/DriveMgr.h> #include <NewKit/OwnPtr.h> -namespace Kernel -{ - /// @brief AHCIDeviceInterface class - /// @details This class is used to send and receive data from the AHCI device. - /// @note The class is derived from the IDeviceObject class. - class AHCIDeviceInterface NE_DEVICE<MountpointInterface*> - { - public: - explicit AHCIDeviceInterface(void (*out)(IDeviceObject* self, MountpointInterface* out), - void (*in)(IDeviceObject* self, MountpointInterface* in)); +namespace Kernel { +/// @brief AHCIDeviceInterface class +/// @details This class is used to send and receive data from the AHCI device. +/// @note The class is derived from the IDeviceObject class. +class AHCIDeviceInterface NE_DEVICE<MountpointInterface*> { + public: + explicit AHCIDeviceInterface(void (*out)(IDeviceObject* self, MountpointInterface* out), + void (*in)(IDeviceObject* self, MountpointInterface* in)); - virtual ~AHCIDeviceInterface() override; + virtual ~AHCIDeviceInterface() override; - public: - AHCIDeviceInterface& operator=(const AHCIDeviceInterface&) = default; - AHCIDeviceInterface(const AHCIDeviceInterface&) = default; + public: + AHCIDeviceInterface& operator=(const AHCIDeviceInterface&) = default; + AHCIDeviceInterface(const AHCIDeviceInterface&) = default; - const Char* Name() const override; + const Char* Name() const override; - const UInt16& GetPortsImplemented(); + const UInt16& GetPortsImplemented(); - Void SetPortsImplemented(const UInt16& pi); + Void SetPortsImplemented(const UInt16& pi); - const UInt32& GetIndex(); + const UInt32& GetIndex(); - Void SetIndex(const UInt32& drv); + Void SetIndex(const UInt32& drv); - public: - AHCIDeviceInterface& operator<<(MountpointInterface* Data) override; - AHCIDeviceInterface& operator>>(MountpointInterface* Data) override; + public: + AHCIDeviceInterface& operator<<(MountpointInterface* Data) override; + AHCIDeviceInterface& operator>>(MountpointInterface* Data) override; - private: - UInt16 fPortsImplemented{0U}; - UInt32 fDriveIndex{0U}; - }; + private: + UInt16 fPortsImplemented{0U}; + UInt32 fDriveIndex{0U}; +}; - UInt16 sk_init_ahci_device(BOOL atapi); - ErrorOr<AHCIDeviceInterface> sk_acquire_ahci_device(Int32 drv_index); -} // namespace Kernel +UInt16 sk_init_ahci_device(BOOL atapi); +ErrorOr<AHCIDeviceInterface> sk_acquire_ahci_device(Int32 drv_index); +} // namespace Kernel diff --git a/dev/kernel/StorageKit/ATA.h b/dev/kernel/StorageKit/ATA.h index 04cf88d7..5887c579 100644 --- a/dev/kernel/StorageKit/ATA.h +++ b/dev/kernel/StorageKit/ATA.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -11,48 +11,46 @@ #include <NewKit/OwnPtr.h> #include <NewKit/Utils.h> -namespace Kernel -{ - /// @brief ATA device interface class. - class ATADeviceInterface : public IDeviceObject<MountpointInterface*> - { - public: - explicit ATADeviceInterface(void (*Out)(IDeviceObject*, MountpointInterface* outpacket), - void (*In)(IDeviceObject*, MountpointInterface* inpacket)); +namespace Kernel { +/// @brief ATA device interface class. +class ATADeviceInterface : public IDeviceObject<MountpointInterface*> { + public: + explicit ATADeviceInterface(void (*Out)(IDeviceObject*, MountpointInterface* outpacket), + void (*In)(IDeviceObject*, MountpointInterface* inpacket)); - virtual ~ATADeviceInterface(); + virtual ~ATADeviceInterface(); - public: - ATADeviceInterface& operator<<(MountpointInterface* Data) override; - ATADeviceInterface& operator>>(MountpointInterface* Data) override; + public: + ATADeviceInterface& operator<<(MountpointInterface* Data) override; + ATADeviceInterface& operator>>(MountpointInterface* Data) override; - public: - ATADeviceInterface& operator=(const ATADeviceInterface&) = default; - ATADeviceInterface(const ATADeviceInterface&) = default; + public: + ATADeviceInterface& operator=(const ATADeviceInterface&) = default; + ATADeviceInterface(const ATADeviceInterface&) = default; - const Char* Name() const override; + const Char* Name() const override; - const UInt16& GetIO(); - Void SetIO(const UInt16& io); + const UInt16& GetIO(); + Void SetIO(const UInt16& io); - const UInt16& GetMaster(); - Void SetMaster(const UInt16& master); + const UInt16& GetMaster(); + Void SetMaster(const UInt16& master); - const UInt32& GetIndex(); - Void SetIndex(const UInt32& drv); + const UInt32& GetIndex(); + Void SetIndex(const UInt32& drv); - private: - UInt32 fDriveIndex{0U}; - UInt16 fIO, fMaster{0U}; - }; + private: + UInt32 fDriveIndex{0U}; + UInt16 fIO, fMaster{0U}; +}; - /// @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); +/// @brief Initialize an PIO device (StorageKit function) +/// @param is_master is the current PIO master? +/// @return [io:master] for PIO device. +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<ATADeviceInterface> sk_acquire_pio_device(Int32 drv_index); -} // namespace Kernel +/// @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<ATADeviceInterface> sk_acquire_ata_device(Int32 drv_index); +} // namespace Kernel diff --git a/dev/kernel/StorageKit/DmaPool.h b/dev/kernel/StorageKit/DmaPool.h new file mode 100644 index 00000000..a5d8c880 --- /dev/null +++ b/dev/kernel/StorageKit/DmaPool.h @@ -0,0 +1,92 @@ +/* + * 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; +inline const UInt8* kDmaPoolEnd = (UInt8*) (NE_DMA_POOL_START + NE_DMA_POOL_SIZE); + +/***********************************************************************************/ +/// @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) >= reinterpret_cast<UIntPtr>(kDmaPoolEnd)) { + kout << "DMA Pool is exhausted!\r"; + + err_global_get() = kErrorDmaExhausted; + + return nullptr; + } + + kDmaPoolPtr = (UInt8*) (addr + size); + return (VoidPtr) addr; +} + +/***********************************************************************************/ +/// @brief Free DMA pointer. +/***********************************************************************************/ +inline Void rtl_dma_free(SizeT size) { + if (!size) return; + + kDmaPoolPtr = (UInt8*) (kDmaPoolPtr - size); +} + +/***********************************************************************************/ +/// @brief Flush DMA pointer. +/***********************************************************************************/ +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/NVME.h b/dev/kernel/StorageKit/NVME.h index c86135d0..aae36a94 100644 --- a/dev/kernel/StorageKit/NVME.h +++ b/dev/kernel/StorageKit/NVME.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -9,26 +9,24 @@ #include <KernelKit/DeviceMgr.h> #include <KernelKit/DriveMgr.h> -namespace Kernel -{ - class NVMEDeviceInterface final NE_DEVICE<MountpointInterface*> - { - public: - explicit NVMEDeviceInterface(Void (*out)(IDeviceObject*, MountpointInterface* out_packet), - Void (*in)(IDeviceObject*, MountpointInterface* in_packet), - Void (*cleanup)(Void)); +namespace Kernel { +class NVMEDeviceInterface final NE_DEVICE<MountpointInterface*> { + public: + explicit NVMEDeviceInterface(Void (*out)(IDeviceObject*, MountpointInterface* out_packet), + Void (*in)(IDeviceObject*, MountpointInterface* in_packet), + Void (*cleanup)(Void)); - ~NVMEDeviceInterface() override; + ~NVMEDeviceInterface() override; - public: - NE_COPY_DEFAULT(NVMEDeviceInterface) + public: + NE_COPY_DEFAULT(NVMEDeviceInterface) - const Char* Name() const override; + const Char* Name() const override; - public: - OwnPtr<MountpointInterface*> operator()(UInt32 dma_low, UInt32 dma_high, SizeT dma_sz); + public: + OwnPtr<MountpointInterface*> operator()(UInt32 dma_low, UInt32 dma_high, SizeT dma_sz); - private: - Void (*fCleanup)(Void) = {nullptr}; - }; -} // namespace Kernel + private: + Void (*fCleanup)(Void) = {nullptr}; +}; +} // namespace Kernel diff --git a/dev/kernel/StorageKit/PRDT.h b/dev/kernel/StorageKit/PRDT.h index dde9c208..f67cad05 100644 --- a/dev/kernel/StorageKit/PRDT.h +++ b/dev/kernel/StorageKit/PRDT.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -12,25 +12,22 @@ #define kPrdtTransferSize (sizeof(Kernel::UShort)) -namespace Kernel -{ - /// @brief Tranfer information about PRD. - enum - { - kPRDTTransferInProgress, - kPRDTTransferIsDone, - kPRDTTransferCount, - }; - - /// @brief Physical Region Descriptor Table. - struct PRDT final - { - UInt32 fPhysAddress; - UInt32 fSectorCount; - UInt8 fEndBit; - }; - - void construct_prdt(Ref<PRDT>& prd); - - EXTERN_C Int32 kPRDTTransferStatus; -} // namespace Kernel +namespace Kernel { +/// @brief Tranfer information about PRD. +enum { + kPRDTTransferInProgress, + kPRDTTransferIsDone, + kPRDTTransferCount, +}; + +/// @brief Physical Region Descriptor Table. +struct PRDT final { + UInt32 fPhysAddress; + UInt32 fSectorCount; + UInt8 fEndBit; +}; + +void construct_prdt(Ref<PRDT>& prd); + +EXTERN_C Int32 kPRDTTransferStatus; +} // namespace Kernel diff --git a/dev/kernel/StorageKit/SCSI.h b/dev/kernel/StorageKit/SCSI.h index 655638f5..25e11716 100644 --- a/dev/kernel/StorageKit/SCSI.h +++ b/dev/kernel/StorageKit/SCSI.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -8,4 +8,4 @@ #include <modules/SCSI/SCSI.h> -extern const scsi_packet_type<12> kCDRomPacketTemplate; +namespace Kernel {} // namespace Kernel
\ No newline at end of file diff --git a/dev/kernel/StorageKit/StorageKit.h b/dev/kernel/StorageKit/StorageKit.h index bbd34476..adb9f5dc 100644 --- a/dev/kernel/StorageKit/StorageKit.h +++ b/dev/kernel/StorageKit/StorageKit.h @@ -1,22 +1,21 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ #pragma once -#define kDriveSectorSizeHDD (512U) -#define kDriveSectorSizeSSD (512U) -#define kDriveSectorSizeOptical (2048) +#define kDriveSectorSizeHDD (512U) +#define kDriveSectorSizeSSD (512U) +#define kDriveSectorSizeOptical (2048U) -namespace Kernel -{ - template <typename T> - class IDeviceObject; +namespace Kernel { +template <typename T> +class IDeviceObject; - class NVMEDeviceInterface; - class AHCIDeviceInterface; - class ATADeviceInterface; - class SCSIDeviceInterface; -} // namespace Kernel +class NVMEDeviceInterface; +class AHCIDeviceInterface; +class ATADeviceInterface; +class SCSIDeviceInterface; +} // namespace Kernel |
