summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/HALKit/AMD64/HalTimer.cc
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-12-27 08:55:09 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-12-27 08:55:09 +0100
commite159d3895e29f38ec1345d396a593a04b7475b92 (patch)
tree5be83ae6f069465780504a642f00955aa3e7b509 /src/kernel/HALKit/AMD64/HalTimer.cc
parent9e746d42d2e3faa526f12ba222f5ee6924dd30f9 (diff)
kernel! giant source code refactor.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'src/kernel/HALKit/AMD64/HalTimer.cc')
-rw-r--r--src/kernel/HALKit/AMD64/HalTimer.cc193
1 files changed, 92 insertions, 101 deletions
diff --git a/src/kernel/HALKit/AMD64/HalTimer.cc b/src/kernel/HALKit/AMD64/HalTimer.cc
index 8d5c0737..275c1ece 100644
--- a/src/kernel/HALKit/AMD64/HalTimer.cc
+++ b/src/kernel/HALKit/AMD64/HalTimer.cc
@@ -1,101 +1,92 @@
-/* ========================================
-
- Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
-
- File: HalTimer.cc
- Purpose: HAL timer
-
- Revision History:
-
- 07/07/24: Added file (amlel)
-
-======================================== */
-
-#include <ArchKit/ArchKit.h>
-#include <KernelKit/Timer.h>
-#include <modules/ACPI/ACPIFactoryInterface.h>
-
-/// ================================================================================
-/// @note timer slot 0
-/// ================================================================================
-
-#define kHPETSignature ("HPET")
-
-#define kHPETCounterRegValue (0x00)
-#define kHPETConfigRegValue (0x20)
-#define kHPETCompRegValue (0x24)
-#define kHPETInterruptRegValue (0x2C)
-
-/// ================================================================================
-///! BUGS: 0
-///! @file HalTimer.cc
-///! @brief Hardware Timer (HPET)
-/// ================================================================================
-
-namespace Kernel::Detail {
-struct HPET_BLOCK : public Kernel::SDT {
- Kernel::UInt8 hardware_rev_id;
- Kernel::UInt8 comparator_count : 5;
- Kernel::UInt8 counter_size : 1;
- Kernel::UInt8 reserved : 1;
- Kernel::UInt8 legacy_replacement : 1;
- Kernel::UInt16 pci_vendor_id;
- ACPI_ADDRESS address;
- Kernel::UInt8 hpet_number;
- Kernel::UInt16 minimum_tick;
- Kernel::UInt8 page_protection;
-} PACKED;
-} // namespace Kernel::Detail
-
-using namespace Kernel;
-
-HardwareTimer::HardwareTimer(UInt64 ms) : fWaitFor(ms) {
- auto power = PowerFactoryInterface(kHandoverHeader->f_HardwareTables.f_VendorPtr);
-
- auto hpet = (Detail::HPET_BLOCK*) power.Find(kHPETSignature).Leak().Leak();
- MUST_PASS(hpet);
-
- fDigitalTimer = (UInt8*) hpet->address.Address;
-
- if (hpet->page_protection) {
- HAL::mm_map_page((VoidPtr) fDigitalTimer, (VoidPtr) fDigitalTimer,
- HAL::kMMFlagsWr | HAL::kMMFlagsPCD | HAL::kMMFlagsPwt);
- }
-
- // if not enabled yet.
- if (!(*((volatile UInt64*) ((UInt8*) fDigitalTimer + kHPETConfigRegValue)) & (1 << 0))) {
- *((volatile UInt64*) ((UInt8*) fDigitalTimer + kHPETConfigRegValue)) =
- *((volatile UInt64*) ((UInt8*) fDigitalTimer + kHPETConfigRegValue)) | (1 << 0) |
- (1 << 3); // enable timer & one shot conf
- }
-}
-
-HardwareTimer::~HardwareTimer() {
- fDigitalTimer = nullptr;
- fWaitFor = 0;
-}
-
-/***********************************************************************************/
-/// @brief Wait for the timer to stop spinning.
-/***********************************************************************************/
-
-BOOL HardwareTimer::Wait() {
- if (fWaitFor < 1) return NO;
- if (fWaitFor > 1'000'000) return NO; // max 1000s = 16 minutes
-
- UInt64 hpet_cap = *((volatile UInt64*) (fDigitalTimer));
- UInt64 femtoseconds_per_tick = (hpet_cap >> 32);
-
- if (femtoseconds_per_tick == 0) return NO;
-
- volatile UInt64* timer = (volatile UInt64*) (fDigitalTimer + kHPETCounterRegValue);
-
- UInt64 now = *timer;
-
- UInt64 fs_wait = fWaitFor * 1'000'000'000'000ULL;
- UInt64 stop_at = now + (fs_wait / femtoseconds_per_tick);
-
- while (*timer < (stop_at)) asm volatile("pause");
-
- return YES;
-}
+// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
+// Licensed under the Apache License, Version 2.0 (see LICENSE file)
+// Official repository: https://github.com/nekernel-org/nekernel
+
+#include <ArchKit/ArchKit.h>
+#include <KernelKit/Timer.h>
+#include <modules/ACPI/ACPIFactoryInterface.h>
+
+/// ================================================================================
+/// @note timer slot 0
+/// ================================================================================
+
+#define kHPETSignature ("HPET")
+
+#define kHPETCounterRegValue (0x00)
+#define kHPETConfigRegValue (0x20)
+#define kHPETCompRegValue (0x24)
+#define kHPETInterruptRegValue (0x2C)
+
+/// ================================================================================
+///! BUGS: 0
+///! @file HalTimer.cc
+///! @brief Hardware Timer (HPET)
+/// ================================================================================
+
+namespace Kernel::Detail {
+struct HPET_BLOCK : public Kernel::SDT {
+ Kernel::UInt8 hardware_rev_id;
+ Kernel::UInt8 comparator_count : 5;
+ Kernel::UInt8 counter_size : 1;
+ Kernel::UInt8 reserved : 1;
+ Kernel::UInt8 legacy_replacement : 1;
+ Kernel::UInt16 pci_vendor_id;
+ ACPI_ADDRESS address;
+ Kernel::UInt8 hpet_number;
+ Kernel::UInt16 minimum_tick;
+ Kernel::UInt8 page_protection;
+} PACKED;
+} // namespace Kernel::Detail
+
+using namespace Kernel;
+
+HardwareTimer::HardwareTimer(UInt64 ms) : fWaitFor(ms) {
+ auto power = PowerFactoryInterface(kHandoverHeader->f_HardwareTables.f_VendorPtr);
+
+ auto hpet = (Detail::HPET_BLOCK*) power.Find(kHPETSignature).Leak().Leak();
+ MUST_PASS(hpet);
+
+ fDigitalTimer = (UInt8*) hpet->address.Address;
+
+ if (hpet->page_protection) {
+ HAL::mm_map_page((VoidPtr) fDigitalTimer, (VoidPtr) fDigitalTimer,
+ HAL::kMMFlagsWr | HAL::kMMFlagsPCD | HAL::kMMFlagsPwt);
+ }
+
+ // if not enabled yet.
+ if (!(*((volatile UInt64*) ((UInt8*) fDigitalTimer + kHPETConfigRegValue)) & (1 << 0))) {
+ *((volatile UInt64*) ((UInt8*) fDigitalTimer + kHPETConfigRegValue)) =
+ *((volatile UInt64*) ((UInt8*) fDigitalTimer + kHPETConfigRegValue)) | (1 << 0) |
+ (1 << 3); // enable timer & one shot conf
+ }
+}
+
+HardwareTimer::~HardwareTimer() {
+ fDigitalTimer = nullptr;
+ fWaitFor = 0;
+}
+
+/***********************************************************************************/
+/// @brief Wait for the timer to stop spinning.
+/***********************************************************************************/
+
+BOOL HardwareTimer::Wait() {
+ if (fWaitFor < 1) return NO;
+ if (fWaitFor > 1'000'000) return NO; // max 1000s = 16 minutes
+
+ UInt64 hpet_cap = *((volatile UInt64*) (fDigitalTimer));
+ UInt64 femtoseconds_per_tick = (hpet_cap >> 32);
+
+ if (femtoseconds_per_tick == 0) return NO;
+
+ volatile UInt64* timer = (volatile UInt64*) (fDigitalTimer + kHPETCounterRegValue);
+
+ UInt64 now = *timer;
+
+ UInt64 fs_wait = fWaitFor * 1'000'000'000'000ULL;
+ UInt64 stop_at = now + (fs_wait / femtoseconds_per_tick);
+
+ while (*timer < (stop_at)) asm volatile("pause");
+
+ return YES;
+}