The Pedigree Project 0.1
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Rtc Class Reference

#include <Rtc.h>

+ Inheritance diagram for Rtc:
+ Collaboration diagram for Rtc:

Classes

class  CmosTransactionGuard
 
struct  periodicIrqInfo_t
 

Public Member Functions

virtual bool registerHandler (TimerHandler *handler)
 
virtual bool unregisterHandler (TimerHandler *handler)
 
virtual void addAlarm (class Event *pEvent, size_t alarmSecs, size_t alarmUsecs=0)
 
virtual void removeAlarm (class Event *pEvent)
 
virtual size_t removeAlarm (class Event *pEvent, bool bRetZero)
 
virtual size_t getYear ()
 
virtual uint8_t getMonth ()
 
virtual uint8_t getDayOfMonth ()
 
virtual uint8_t getDayOfWeek ()
 
virtual uint8_t getHour ()
 
virtual uint8_t getMinute ()
 
virtual uint8_t getSecond ()
 
virtual uint64_t getNanosecond ()
 
virtual uint64_t getTickCount ()
 
virtual uint64_t getTickCountNano ()
 
virtual uint64_t getTickCountNanoFast ()
 
Time::CpuTimeSample sampleCpuTime () override
 
bool initialise1 (uint8_t centuryIndex) INITIALISATION_ONLY
 
bool initialise2 () INITIALISATION_ONLY
 
void initialiseProcessorClock () INITIALISATION_ONLY
 
bool initialise3 ()
 
virtual void synchronise (bool tohw=false)
 
void uninitialise ()
 
- Public Member Functions inherited from Timer
virtual Time::Timestamp getUnixTimestamp ()
 

Static Public Member Functions

static Rtc & instance ()
 
static EXPORTED_PUBLIC uint8_t readCmos (uint8_t index)
 
static EXPORTED_PUBLIC void writeCmos (uint8_t index, uint8_t value)
 

Protected Member Functions

 Rtc () INITIALISATION_ONLY
 
virtual ~Rtc ()
 
- Protected Member Functions inherited from Timer
 Timer ()
 
virtual ~Timer ()
 

Private Types

using Alarm = RtcAlarmQueue::Record
 

Private Member Functions

 Rtc (const Rtc &)
 
Rtc & operator= (const Rtc &)
 
IrqDisposition irq (irq_id_t number) override
 
void processElapsedTime (uint64_t observed)
 
void setPeriodicInterruptEnabled (bool enabled)
 
void setIndexLocked (uint8_t index)
 
bool waitForUpdateCompletion (uint8_t index)
 
bool read (uint8_t index, uint8_t &value)
 
uint8_t readLocked (uint8_t index)
 
void writeLocked (uint8_t index, uint8_t value)
 
uint8_t read (uint8_t index)
 
bool write (uint8_t index, uint8_t value)
 
bool readHardwareClock ()
 
bool writeHardwareClock ()
 
bool inhibitClockUpdatesLocked (uint8_t &status)
 
void drainRemoteAlarmDispatch (class Event *pEvent, void *owner)
 
virtual ~IrqHandler ()
 
- Private Member Functions inherited from IrqHandlerBase
virtual ~IrqHandlerBase ()
 

Private Attributes

IoPort m_IoPort
 
irq_id_t m_IrqId
 
size_t m_PeriodicIrqInfoIndex
 
uint8_t m_CenturyIndex
 
size_t m_Year
 
uint8_t m_Month
 
uint8_t m_DayOfMonth
 
uint8_t m_Hour
 
uint8_t m_Minute
 
uint8_t m_Second
 
uint64_t m_Nanosecond
 
Atomic< uint64_t > m_TickCount
 
uint64_t m_ProcessedTickCount
 
TimerHandlerRegistry m_HandlerRegistry
 
RtcAlarmQueue m_AlarmQueue
 
Spinlock m_Lock
 
Mutex m_CmosLock
 
PcTscClock::Calibration m_TscCalibration
 
uint64_t m_Tsc0
 
PcTscClock::MonotonicPublication m_MonotonicTicks
 

Static Private Attributes

static periodicIrqInfo_t periodicIrqInfo [12]
 
static Rtc m_Instance
 

Detailed Description

Class for the Real-time clock / CMOS implementing the Timer interface

Definition at line 46 of file Rtc.h.

Member Typedef Documentation

◆ Alarm

Definition at line 220 of file Rtc.h.

Constructor & Destructor Documentation

◆ Rtc() [1/2]

Rtc::Rtc ( )
protected

The default constructor

Definition at line 545 of file Rtc.cc.

◆ ~Rtc()

virtual Rtc::~Rtc ( )
inlineprotectedvirtual

The destructor

Definition at line 101 of file Rtc.h.

◆ Rtc() [2/2]

Rtc::Rtc ( const Rtc &  )
private

The copy-constructor

Note
NOT implemented

Member Function Documentation

◆ addAlarm()

void Rtc::addAlarm ( class Event *  pEvent,
size_t  alarmSecs,
size_t  alarmUsecs = 0 
)
virtual

Dispatches the Event pEvent to the current thread in alarmSecs time.

Parameters
pEventEvent to dispatch.
alarmSecsNumber of seconds to wait.

Implements Timer.

Definition at line 134 of file Rtc.cc.

References Spinlock::acquire(), getTickCountNano(), ProcessorBase::information(), m_AlarmQueue, m_Lock, and Spinlock::release().

◆ drainRemoteAlarmDispatch()

void Rtc::drainRemoteAlarmDispatch ( class Event *  pEvent,
void *  owner 
)
private

Drains a sendEvent handoff owned by another processor.

Definition at line 154 of file Rtc.cc.

References Spinlock::acquire(), Thread::getDebugState(), ProcessorBase::getInterrupts(), ProcessorBase::information(), Scheduler::instance(), m_AlarmQueue, m_Lock, ProcessorBase::pause(), Spinlock::release(), Thread::setDebugState(), and Scheduler::yield().

Referenced by removeAlarm(), and removeAlarm().

+ Here is the caller graph for this function:

◆ getDayOfMonth()

uint8_t Rtc::getDayOfMonth ( )
virtual

Get the current day of month

Returns
the current day of month

Implements Timer.

Definition at line 245 of file Rtc.cc.

References m_DayOfMonth.

◆ getDayOfWeek()

uint8_t Rtc::getDayOfWeek ( )
virtual

Get the current day of week

Returns
the current day of week

Implements Timer.

Definition at line 248 of file Rtc.cc.

References m_DayOfMonth, m_Month, and m_Year.

◆ getHour()

uint8_t Rtc::getHour ( )
virtual

Get the current hour

Returns
the current hour

Implements Timer.

Definition at line 262 of file Rtc.cc.

References m_Hour.

◆ getMinute()

uint8_t Rtc::getMinute ( )
virtual

Get the current minute

Returns
the current minute

Implements Timer.

Definition at line 265 of file Rtc.cc.

References m_Minute.

◆ getMonth()

uint8_t Rtc::getMonth ( )
virtual

Get the current month

Returns
the current month

Implements Timer.

Definition at line 242 of file Rtc.cc.

References m_Month.

◆ getNanosecond()

uint64_t Rtc::getNanosecond ( )
virtual

Get the current nanosecond

Returns
the current nanosecond

Implements Timer.

Definition at line 271 of file Rtc.cc.

References m_Nanosecond.

◆ getSecond()

uint8_t Rtc::getSecond ( )
virtual

Get the current second

Returns
the current second

Implements Timer.

Definition at line 268 of file Rtc.cc.

References m_Second.

◆ getTickCount()

uint64_t Rtc::getTickCount ( )
virtual

Get the Tick count (time elapsed since system bootup)

Returns
the tick count in milliseconds

Implements Timer.

Definition at line 274 of file Rtc.cc.

References getTickCountNano().

◆ getTickCountNano()

uint64_t Rtc::getTickCountNano ( )
virtual

Get the Tick count (time elapsed since system bootup) in nanosconds.

Returns
the tick count in nanoseconds

Reimplemented from Timer.

Definition at line 277 of file Rtc.cc.

References ProcessorBase::getInterrupts(), ProcessorBase::information(), m_MonotonicTicks, m_TickCount, m_Tsc0, m_TscCalibration, and ProcessorBase::setInterrupts().

Referenced by addAlarm(), getTickCount(), initialise3(), irq(), and removeAlarm().

+ Here is the caller graph for this function:

◆ getTickCountNanoFast()

uint64_t Rtc::getTickCountNanoFast ( )
virtual

Get the tick count while the caller already owns the IRQ-disabled window.

Reimplemented from Timer.

Definition at line 298 of file Rtc.cc.

References sampleCpuTime().

◆ getYear()

size_t Rtc::getYear ( )
virtual

Get the current year

Returns
the current year

Implements Timer.

Definition at line 239 of file Rtc.cc.

References m_Year.

◆ inhibitClockUpdatesLocked()

bool Rtc::inhibitClockUpdatesLocked ( uint8_t &  status)
private

Definition at line 757 of file Rtc.cc.

◆ initialise1()

bool Rtc::initialise1 ( uint8_t  centuryIndex)

Initialises the class

Returns
true, if successful, false otherwise

Definition at line 316 of file Rtc.cc.

References IoPort::allocate(), Hex, m_HandlerRegistry, m_IoPort, m_IrqId, m_PeriodicIrqInfoIndex, periodicIrqInfo, Rtc::periodicIrqInfo_t::rateBits, readHardwareClock(), TimerHandlerRegistry::reset(), and write().

Referenced by Pc::initialise().

+ Here is the caller graph for this function:

◆ initialise2()

bool Rtc::initialise2 ( )

Calibrates the TSC while the RTC line remains masked.

Definition at line 373 of file Rtc.cc.

References ProcessorBase::information(), m_MonotonicTicks, m_PeriodicIrqInfoIndex, m_ProcessedTickCount, m_TickCount, m_Tsc0, m_TscCalibration, Rtc::periodicIrqInfo_t::ns, ProcessorBase::pause(), periodicIrqInfo, and setPeriodicInterruptEnabled().

Referenced by Pc::initialise().

+ Here is the caller graph for this function:

◆ initialise3()

bool Rtc::initialise3 ( )

◆ initialiseProcessorClock()

void Rtc::initialiseProcessorClock ( )

Anchors the calling application processor to global monotonic time.

Definition at line 430 of file Rtc.cc.

References ProcessorBase::getInterrupts(), ProcessorBase::information(), m_MonotonicTicks, m_TickCount, and ProcessorBase::setInterrupts().

◆ instance()

static Rtc & Rtc::instance ( )
inlinestatic

Definition at line 48 of file Rtc.h.

◆ irq()

IrqDisposition Rtc::irq ( irq_id_t  number)
overrideprivatevirtual

Handles a pending IRQ in thread context.

Implements IrqHandler.

Definition at line 570 of file Rtc.cc.

References getTickCountNano(), m_CmosLock, and processElapsedTime().

◆ operator=()

Rtc & Rtc::operator= ( const Rtc &  )
private

The assignment operator

Note
NOT implemented

◆ processElapsedTime()

void Rtc::processElapsedTime ( uint64_t  observed)
private

◆ read() [1/2]

uint8_t Rtc::read ( uint8_t  index)
private

Read the value in the CMOS at a specific index

Parameters
[in]indexthe index
Returns
the value at the index

Definition at line 723 of file Rtc.cc.

◆ read() [2/2]

bool Rtc::read ( uint8_t  index,
uint8_t &  value 
)
private

Definition at line 729 of file Rtc.cc.

◆ readCmos()

uint8_t Rtc::readCmos ( uint8_t  index)
static

Access CMOS through the RTC-owned selector/data transaction.

These entrypoints exist for PC compatibility layers which need CMOS bytes but must not access ports 0x70/0x71 independently of the RTC.

Definition at line 719 of file Rtc.cc.

References m_Instance.

◆ readHardwareClock()

bool Rtc::readHardwareClock ( )
private

Transfer a complete clock snapshot without partially publishing it.

Definition at line 773 of file Rtc.cc.

References m_CmosLock, m_DayOfMonth, m_Hour, m_Minute, m_Month, m_Second, m_Year, and waitForUpdateCompletion().

Referenced by initialise1(), and synchronise().

+ Here is the caller graph for this function:

◆ readLocked()

uint8_t Rtc::readLocked ( uint8_t  index)
private

Definition at line 738 of file Rtc.cc.

◆ registerHandler()

bool Rtc::registerHandler ( TimerHandler *  handler)
virtual

Implements Timer.

Definition at line 232 of file Rtc.cc.

◆ removeAlarm() [1/2]

void Rtc::removeAlarm ( class Event *  pEvent)
virtual

Removes the event pEvent from the alarm queue. Once this returns, the timer will not begin another delivery of this event.

Parameters
pEventEvent to remove alarm for.

Implements Timer.

Definition at line 188 of file Rtc.cc.

References Spinlock::acquire(), drainRemoteAlarmDispatch(), m_AlarmQueue, m_Lock, and Spinlock::release().

◆ removeAlarm() [2/2]

size_t Rtc::removeAlarm ( class Event *  pEvent,
bool  bRetZero 
)
virtual

Removes the event pEvent from the alarm queue. Once this returns, the timer will not begin another delivery of this event.

Parameters
pEventEvent to remove alarm for.
bRetZeroIf true, returns zero rather the time until firing
Returns
The number of seconds before the event would have fired, or zero if bRetZero is true.

Implements Timer.

Definition at line 204 of file Rtc.cc.

References Spinlock::acquire(), drainRemoteAlarmDispatch(), getTickCountNano(), m_AlarmQueue, m_Lock, and Spinlock::release().

◆ sampleCpuTime()

Time::CpuTimeSample Rtc::sampleCpuTime ( )
overridevirtual

Samples nanoseconds and CPU identity while the caller keeps interrupts disabled.

Reimplemented from Timer.

Definition at line 302 of file Rtc.cc.

References ProcessorBase::index(), ProcessorBase::information(), m_Tsc0, and m_TscCalibration.

Referenced by getTickCountNanoFast().

+ Here is the caller graph for this function:

◆ setIndexLocked()

void Rtc::setIndexLocked ( uint8_t  index)
private

Set the index register

Parameters
[in]indexthe new index

Definition at line 687 of file Rtc.cc.

References m_IoPort, IoPort::read8(), and IoPort::write8().

Referenced by waitForUpdateCompletion().

+ Here is the caller graph for this function:

◆ setPeriodicInterruptEnabled()

void Rtc::setPeriodicInterruptEnabled ( bool  enabled)
private

Atomically changes the RTC periodic-interrupt source and clears C.

Definition at line 822 of file Rtc.cc.

References m_CmosLock.

Referenced by initialise2(), initialise3(), and uninitialise().

+ Here is the caller graph for this function:

◆ synchronise()

void Rtc::synchronise ( bool  tohw = false)
virtual

Synchronise the time/date with the hardware

Reimplemented from Timer.

Definition at line 501 of file Rtc.cc.

References readHardwareClock().

Referenced by uninitialise().

+ Here is the caller graph for this function:

◆ uninitialise()

void Rtc::uninitialise ( )

Uninitialises the class

Definition at line 512 of file Rtc.cc.

References IoPort::free(), m_AlarmQueue, m_HandlerRegistry, m_IoPort, m_IrqId, m_Lock, panic(), TimerHandlerRegistry::reset(), setPeriodicInterruptEnabled(), and synchronise().

Referenced by Pc::deinitialise().

+ Here is the caller graph for this function:

◆ unregisterHandler()

bool Rtc::unregisterHandler ( TimerHandler *  handler)
virtual

Implements Timer.

Definition at line 236 of file Rtc.cc.

◆ waitForUpdateCompletion()

bool Rtc::waitForUpdateCompletion ( uint8_t  index)
private

Wait until the Update of the RTC entries in the CMOS is complete, if we want to access one of those entries

Parameters
[in]indexthe index we want to access afterwards
Returns
true if the update completed before the hardware deadline

Definition at line 692 of file Rtc.cc.

References m_CmosLock, m_IoPort, ProcessorBase::pause(), IoPort::read8(), and setIndexLocked().

Referenced by readHardwareClock(), and write().

+ Here is the caller graph for this function:

◆ write()

bool Rtc::write ( uint8_t  index,
uint8_t  value 
)
private

Write the value to the CMOS at a specific index

Parameters
[in]indexthe index
[in]valuethe value
Returns
true if the write was performed

Definition at line 743 of file Rtc.cc.

References m_CmosLock, and waitForUpdateCompletion().

Referenced by initialise1(), and initialise3().

+ Here is the caller graph for this function:

◆ writeCmos()

void Rtc::writeCmos ( uint8_t  index,
uint8_t  value 
)
static

Definition at line 753 of file Rtc.cc.

◆ writeHardwareClock()

bool Rtc::writeHardwareClock ( )
private

Definition at line 798 of file Rtc.cc.

◆ writeLocked()

void Rtc::writeLocked ( uint8_t  index,
uint8_t  value 
)
private

Definition at line 817 of file Rtc.cc.

Member Data Documentation

◆ m_AlarmQueue

RtcAlarmQueue Rtc::m_AlarmQueue
private

Intrusive alarm ownership; queue operations never allocate in IRQs.

Definition at line 223 of file Rtc.h.

Referenced by addAlarm(), drainRemoteAlarmDispatch(), processElapsedTime(), removeAlarm(), removeAlarm(), and uninitialise().

◆ m_CenturyIndex

uint8_t Rtc::m_CenturyIndex
private

Definition at line 178 of file Rtc.h.

◆ m_CmosLock

Mutex Rtc::m_CmosLock
private

Serialises the CMOS index/data register pair across processors.

Definition at line 228 of file Rtc.h.

Referenced by irq(), readHardwareClock(), setPeriodicInterruptEnabled(), waitForUpdateCompletion(), and write().

◆ m_DayOfMonth

uint8_t Rtc::m_DayOfMonth
private

The current day of month

Definition at line 185 of file Rtc.h.

Referenced by getDayOfMonth(), getDayOfWeek(), processElapsedTime(), and readHardwareClock().

◆ m_HandlerRegistry

TimerHandlerRegistry Rtc::m_HandlerRegistry
private

Timer handlers and their callback lifetime state.

Definition at line 218 of file Rtc.h.

Referenced by initialise1(), processElapsedTime(), and uninitialise().

◆ m_Hour

uint8_t Rtc::m_Hour
private

The current hour

Definition at line 187 of file Rtc.h.

Referenced by getHour(), processElapsedTime(), and readHardwareClock().

◆ m_Instance

Rtc Rtc::m_Instance
staticprivate

The Rtc class instance

Definition at line 215 of file Rtc.h.

Referenced by readCmos().

◆ m_IoPort

IoPort Rtc::m_IoPort
private

The CMOS/Real-time Clock I/O port range

Definition at line 170 of file Rtc.h.

Referenced by initialise1(), setIndexLocked(), uninitialise(), and waitForUpdateCompletion().

◆ m_IrqId

irq_id_t Rtc::m_IrqId
private

The IRQ Identifier

Definition at line 173 of file Rtc.h.

Referenced by initialise1(), initialise3(), and uninitialise().

◆ m_Lock

Spinlock Rtc::m_Lock
private

Protects the alarm queue and the dispatch-ownership transition.

Definition at line 225 of file Rtc.h.

Referenced by addAlarm(), drainRemoteAlarmDispatch(), processElapsedTime(), removeAlarm(), removeAlarm(), and uninitialise().

◆ m_Minute

uint8_t Rtc::m_Minute
private

The current minute

Definition at line 189 of file Rtc.h.

Referenced by getMinute(), processElapsedTime(), and readHardwareClock().

◆ m_MonotonicTicks

PcTscClock::MonotonicPublication Rtc::m_MonotonicTicks
private

Cross-processor monotonic floor, published without a retry loop.

Definition at line 237 of file Rtc.h.

Referenced by getTickCountNano(), initialise2(), and initialiseProcessorClock().

◆ m_Month

uint8_t Rtc::m_Month
private

The current month

Definition at line 183 of file Rtc.h.

Referenced by getDayOfWeek(), getMonth(), processElapsedTime(), and readHardwareClock().

◆ m_Nanosecond

uint64_t Rtc::m_Nanosecond
private

The current nanosecond

Definition at line 193 of file Rtc.h.

Referenced by getNanosecond(), and processElapsedTime().

◆ m_PeriodicIrqInfoIndex

size_t Rtc::m_PeriodicIrqInfoIndex
private

Index into the periodicIrqInfo table

Definition at line 176 of file Rtc.h.

Referenced by initialise1(), initialise2(), and initialise3().

◆ m_ProcessedTickCount

uint64_t Rtc::m_ProcessedTickCount
private

Worker cursor used to aggregate delayed periodic delivery.

Definition at line 199 of file Rtc.h.

Referenced by initialise2(), initialise3(), and processElapsedTime().

◆ m_Second

uint8_t Rtc::m_Second
private

The current second

Definition at line 191 of file Rtc.h.

Referenced by getSecond(), processElapsedTime(), and readHardwareClock().

◆ m_TickCount

Atomic<uint64_t> Rtc::m_TickCount
private

Latest elapsed-time cursor published by the IRQ8 worker.

Definition at line 196 of file Rtc.h.

Referenced by getTickCountNano(), initialise2(), initialise3(), initialiseProcessorClock(), and processElapsedTime().

◆ m_Tsc0

uint64_t Rtc::m_Tsc0
private

Bootstrap fallback before a per-processor anchor is available.

Definition at line 234 of file Rtc.h.

Referenced by getTickCountNano(), initialise2(), and sampleCpuTime().

◆ m_TscCalibration

PcTscClock::Calibration Rtc::m_TscCalibration
private

Exact RTC-calibrated conversion from TSC cycles to nanoseconds.

Definition at line 231 of file Rtc.h.

Referenced by getTickCountNano(), initialise2(), and sampleCpuTime().

◆ m_Year

size_t Rtc::m_Year
private

The current year

Definition at line 181 of file Rtc.h.

Referenced by getDayOfWeek(), getYear(), processElapsedTime(), and readHardwareClock().

◆ periodicIrqInfo

Rtc::periodicIrqInfo_t Rtc::periodicIrqInfo
staticprivate
Initial value:
= {
{4, 0x0e, {250000000ULL, 250000000ULL}}, {8, 0x0d, {125000000ULL, 125000000ULL}},
{16, 0x0c, {62500000ULL, 62500000ULL}}, {32, 0x0b, {31250000ULL, 31250000ULL}},
{64, 0x0a, {15625000ULL, 15625000ULL}}, {128, 0x09, {7812500ULL, 7812500ULL}},
{256, 0x08, {3906250ULL, 3906250ULL}}, {512, 0x07, {1953125ULL, 1953125ULL}},
{1024, 0x06, {976562ULL, 976563ULL}}, {2048, 0x05, {488281ULL, 488281ULL}},
{4096, 0x04, {244140ULL, 244141ULL}}, {8192, 0x03, {122070ULL, 122070ULL}},
}

Information about the RTC's periodic irq

Definition at line 86 of file Rtc.h.

Referenced by initialise1(), initialise2(), and initialise3().


The documentation for this class was generated from the following files: