2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-06-07 21:02:01 +03:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-07-09 15:18:03 +03:00
|
|
|
#include <Kernel/Arch/i386/PIC.h>
|
2019-07-09 15:23:12 +03:00
|
|
|
#include <Kernel/Arch/i386/PIT.h>
|
2019-07-09 15:18:03 +03:00
|
|
|
#include <Kernel/IO.h>
|
|
|
|
#include <Kernel/Scheduler.h>
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
#define IRQ_TIMER 0
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
extern "C" void timer_interrupt_entry();
|
2019-11-06 15:15:55 +03:00
|
|
|
extern "C" void timer_interrupt_handler(RegisterDump);
|
2018-10-16 12:01:38 +03:00
|
|
|
|
|
|
|
asm(
|
2019-01-31 19:31:23 +03:00
|
|
|
".globl timer_interrupt_entry \n"
|
|
|
|
"timer_interrupt_entry: \n"
|
2019-10-04 17:31:34 +03:00
|
|
|
" pushl $0x0\n"
|
2018-10-16 12:01:38 +03:00
|
|
|
" pusha\n"
|
2019-12-15 14:11:39 +03:00
|
|
|
" pushl %ds\n"
|
|
|
|
" pushl %es\n"
|
|
|
|
" pushl %fs\n"
|
|
|
|
" pushl %gs\n"
|
|
|
|
" pushl %ss\n"
|
|
|
|
" mov $0x10, %ax\n"
|
|
|
|
" mov %ax, %ds\n"
|
|
|
|
" mov %ax, %es\n"
|
2019-11-10 00:40:35 +03:00
|
|
|
" cld\n"
|
2019-01-31 19:31:23 +03:00
|
|
|
" call timer_interrupt_handler\n"
|
2019-12-15 14:11:39 +03:00
|
|
|
" add $0x4, %esp\n"
|
|
|
|
" popl %gs\n"
|
|
|
|
" popl %fs\n"
|
|
|
|
" popl %es\n"
|
|
|
|
" popl %ds\n"
|
2018-10-16 12:01:38 +03:00
|
|
|
" popa\n"
|
2019-10-04 17:31:34 +03:00
|
|
|
" add $0x4, %esp\n"
|
2019-06-07 12:43:58 +03:00
|
|
|
" iret\n");
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
static u32 s_ticks_this_second;
|
|
|
|
static u32 s_seconds_since_boot;
|
2019-02-01 05:50:06 +03:00
|
|
|
|
2019-11-06 15:15:55 +03:00
|
|
|
void timer_interrupt_handler(RegisterDump regs)
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2020-01-08 09:27:37 +03:00
|
|
|
clac();
|
2018-10-16 12:01:38 +03:00
|
|
|
IRQHandlerScope scope(IRQ_TIMER);
|
2019-03-25 04:06:57 +03:00
|
|
|
if (++s_ticks_this_second >= TICKS_PER_SECOND) {
|
|
|
|
// FIXME: Synchronize with the RTC somehow to prevent drifting apart.
|
|
|
|
++s_seconds_since_boot;
|
|
|
|
s_ticks_this_second = 0;
|
|
|
|
}
|
2018-11-08 02:24:59 +03:00
|
|
|
Scheduler::timer_tick(regs);
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace PIT {
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 ticks_this_second()
|
2019-02-01 05:50:06 +03:00
|
|
|
{
|
2019-03-25 04:06:57 +03:00
|
|
|
return s_ticks_this_second;
|
2019-02-01 05:50:06 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 seconds_since_boot()
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2019-03-25 04:06:57 +03:00
|
|
|
return s_seconds_since_boot;
|
|
|
|
}
|
2019-02-01 05:50:06 +03:00
|
|
|
|
2019-03-25 04:06:57 +03:00
|
|
|
void initialize()
|
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 timer_reload;
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
IO::out8(PIT_CTL, TIMER0_SELECT | WRITE_WORD | MODE_SQUARE_WAVE);
|
2018-10-16 12:01:38 +03:00
|
|
|
|
|
|
|
timer_reload = (BASE_FREQUENCY / TICKS_PER_SECOND);
|
|
|
|
|
2019-02-06 13:32:23 +03:00
|
|
|
kprintf("PIT: %u Hz, square wave (%x)\n", TICKS_PER_SECOND, timer_reload);
|
2018-10-16 12:01:38 +03:00
|
|
|
|
|
|
|
IO::out8(TIMER0_CTL, LSB(timer_reload));
|
|
|
|
IO::out8(TIMER0_CTL, MSB(timer_reload));
|
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
register_interrupt_handler(IRQ_VECTOR_BASE + IRQ_TIMER, timer_interrupt_entry);
|
2018-10-16 12:01:38 +03:00
|
|
|
|
|
|
|
PIC::enable(IRQ_TIMER);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|