source: trunk/hal/x86_64/core/hal_apic.c @ 117

Last change on this file since 117 was 117, checked in by max@…, 7 years ago

Calibrate the LAPIC timer with the PIT, with one interrupt per
second for now (will be revisited).

File size: 7.4 KB
Line 
1/*
2 * hal_apic.c - Advanced Programmable Interrupt Controller
3 *
4 * Copyright (c) 2017 Maxime Villard
5 *
6 * This file is part of ALMOS-MKH.
7 *
8 * ALMOS-MKH is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2.0 of the License.
11 *
12 * ALMOS-MKH is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <hal_types.h>
23#include <hal_boot.h>
24#include <hal_register.h>
25#include <hal_segmentation.h>
26#include <hal_apic.h>
27#include <hal_internal.h>
28
29#include <memcpy.h>
30#include <thread.h>
31#include <string.h>
32#include <process.h>
33#include <printk.h>
34#include <vmm.h>
35#include <core.h>
36#include <cluster.h>
37
38/* -------------------------------------------------------------------------- */
39
40#define PIC1_CMD        0x0020
41#define PIC1_DATA       0x0021
42#define PIC2_CMD        0x00a0
43#define PIC2_DATA       0x00a1
44
45static void hal_pic_init()
46{
47        /*
48         * Disable the PIC (8259A). We are going to use IOAPIC instead.
49         */
50        out8(PIC1_DATA, 0xff);
51        out8(PIC2_DATA, 0xff);
52}
53
54/* -------------------------------------------------------------------------- */
55
56uint64_t pit_ticks_base __in_kdata = 0;
57
58#define PIT_FREQUENCY   1193182
59#define HZ              100 /* 1/HZ = 10ms */
60
61#define PIT_TIMER0      0x40
62
63#define PIT_CMD         0x43
64#       define CMD_BINARY       0x00 /* Use Binary counter values */
65#       define CMD_BCD          0x01 /* Use Binary Coded Decimal counter values */
66#       define CMD_MODE0        0x00 /* Interrupt on Terminal Count */
67#       define CMD_MODE1        0x02 /* Hardware Retriggerable One-Shot */
68#       define CMD_MODE2        0x04 /* Rate Generator */
69#       define CMD_MODE3        0x06 /* Square Wave */
70#       define CMD_MODE4        0x08 /* Software Trigerred Strobe */
71#       define CMD_MODE5        0x0a /* Hardware Trigerred Strobe */
72#       define CMD_LATCH        0x00 /* latch counter for reading */
73#       define CMD_LSB          0x10 /* LSB, 8 bits */
74#       define CMD_MSB          0x20 /* MSB, 8 bits */
75#       define CMD_16BIT        0x30 /* LSB and MSB, 16 bits */
76#       define CMD_COUNTER0     0x00
77#       define CMD_COUNTER1     0x40
78#       define CMD_COUNTER2     0x80
79#       define CMD_READBACK     0xc0
80
81void hal_pit_init()
82{
83        /* Initialize PIT clock 0 to the maximum counter value, 65535. */
84        out8(PIT_CMD, CMD_COUNTER0|CMD_MODE2|CMD_16BIT);
85        out8(PIT_TIMER0, 0xFF);
86        out8(PIT_TIMER0, 0xFF);
87}
88
89uint64_t
90hal_pit_timer_read()
91{
92        static uint16_t last;
93
94        uint8_t lo, hi;
95        uint16_t ctr;
96        uint64_t ticks;
97
98        /* Read the current timer counter. */
99        out8(PIT_CMD, CMD_COUNTER0|CMD_LATCH);
100        lo = in8(PIT_TIMER0);
101        hi = in8(PIT_TIMER0);
102        ctr = (hi << 8) | lo;
103
104        /* If the counter has wrapped, assume we're into the next tick. */
105        if (ctr > last)
106                pit_ticks_base += 0xFFFF;
107        last = ctr;
108
109        ticks = pit_ticks_base + (0xFFFF - ctr);
110
111        return ticks;
112}
113
114/* -------------------------------------------------------------------------- */
115
116paddr_t ioapic_pa __in_kdata = 0;
117vaddr_t ioapic_va __in_kdata = 0;
118
119#define IRQ_TIMER       0x00
120#define IRQ_KEYBOARD    0x01
121#define IRQ_COM2        0x03
122#define IRQ_COM1        0x04
123#define IRQ_FLOPPY      0x06
124#define IRQ_ATA0        0x0e
125#define IRQ_ATA1        0x0f
126
127#define IOREGSEL        0x00
128#define IOWIN   0x10
129
130#define IOAPICID        0x00
131#define IOAPICVER       0x01
132#define IOAPICARB       0x02
133#define IOREDTBL        0x10
134
135#define IOENTRY_DISABLE 0x10000
136
137void hal_ioapic_write(uint8_t reg, uint32_t val)
138{
139        *((volatile uint32_t *)((uint8_t *)ioapic_va + IOREGSEL)) = reg;
140        *((volatile uint32_t *)((uint8_t *)ioapic_va + IOWIN)) = val;
141}
142
143uint32_t hal_ioapic_read(uint8_t reg)
144{
145        *((volatile uint32_t *)((uint8_t *)ioapic_va + IOREGSEL)) = reg;
146        return *((volatile uint32_t *)((uint8_t *)ioapic_va + IOWIN));
147}
148
149void hal_ioapic_set_entry(uint8_t index, uint64_t data)
150{
151        hal_ioapic_write(IOREDTBL + index * 2, (uint32_t)(data & 0xFFFFFFFF));
152        hal_ioapic_write(IOREDTBL + index * 2 + 1, (uint32_t)(data >> 32));
153}
154
155static void hal_ioapic_init()
156{
157        size_t i, pins;
158        uint32_t ver;
159
160        ioapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared
161
162        hal_gpt_enter(ioapic_va, ioapic_pa, PG_V|PG_KW|PG_NX|PG_N);
163
164        ver = hal_ioapic_read(IOAPICVER);
165        pins = ((ver >> 16) & 0xFF) + 1;
166
167        /* Explicitly disable (mask) each vector */
168        for (i = 0; i < pins; i++) {
169                hal_ioapic_set_entry(i, IOENTRY_DISABLE);
170        }
171
172        /* Now, enable the keyboard */
173        hal_ioapic_set_entry(IRQ_KEYBOARD, IOAPIC_KEYBOARD_VECTOR);
174}
175
176/* -------------------------------------------------------------------------- */
177
178paddr_t lapic_pa __in_kdata = 0;
179vaddr_t lapic_va __in_kdata = 0;
180
181void hal_lapic_write(uint32_t reg, uint32_t val)
182{
183        *((volatile uint32_t *)((uint8_t *)lapic_va + reg)) = val;
184}
185
186uint32_t hal_lapic_read(uint32_t reg)
187{
188        return *((volatile uint32_t *)((uint8_t *)lapic_va + reg));
189}
190
191uint32_t hal_lapic_gid()
192{
193        return hal_lapic_read(LAPIC_ID) >> LAPIC_ID_SHIFT;
194}
195
196/*
197 * Use the PIT, which has a standard clock frequency, to determine the CPU's
198 * exact bus frequency.
199 */
200static void hal_lapic_calibrate()
201{
202        uint64_t pittick, lapictick0, lapictick1;
203        uint32_t lapicticks, lapicstart;
204
205        /* Initialize the LAPIC timer to the maximum value */
206        hal_lapic_write(LAPIC_ICR_TIMER, 0xFFFFFFFF); 
207
208        pittick = hal_pit_timer_read() + 1;
209        while (hal_pit_timer_read() < pittick) {
210                /* Wait until start of a PIT tick */
211        }
212
213        /* Read base count from LAPIC */
214        lapictick0 = hal_lapic_read(LAPIC_CCR_TIMER);
215
216        while (hal_pit_timer_read() < pittick + (PIT_FREQUENCY + HZ/2) / HZ) {
217                /* Wait 1/HZ sec = 10ms */
218        }
219
220        /* Read final count from LAPIC */
221        lapictick1 = hal_lapic_read(LAPIC_CCR_TIMER);
222
223        /* Total number of LAPIC ticks per 1/HZ tick */
224        lapicticks = (lapictick1 - lapictick0);
225
226        /* Finally, calibrate the timer, an interrupt each 1s. */
227        lapicstart = - (lapicticks * 100);
228        hal_lapic_write(LAPIC_ICR_TIMER, lapicstart);
229}
230
231/*
232 * We have 8 interrupt sources:
233 *  - Spurious
234 *  - APIC Timer (TMR)
235 *  - Local Interrupt 0 (LINT0)
236 *  - Local Interrupt 1 (LINT1)
237 *  - Performance Monitor Counters (PMC)
238 *  - Thermal Sensors (THM)
239 *  - APIC internal error (ERR)
240 *  - Extended (Implementation dependent)
241 */
242static void hal_lapic_init()
243{
244        lapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared
245
246        if ((rdmsr(MSR_APICBASE) & APICBASE_PHYSADDR) != lapic_pa) {
247                x86_panic("APICBASE and ACPI don't match!\n");
248        }
249        wrmsr(MSR_APICBASE, lapic_pa | APICBASE_EN);
250
251        hal_gpt_enter(lapic_va, lapic_pa, PG_V|PG_KW|PG_NX|PG_N);
252
253        hal_lapic_write(LAPIC_TPR, 0);
254        hal_lapic_write(LAPIC_EOI, 0);
255        hal_lapic_write(LAPIC_SVR, LAPIC_SVR_ENABLE|LAPIC_SPURIOUS_VECTOR);
256
257        /* Explicitly disable (mask) each vector */
258        hal_lapic_write(LAPIC_LVT_TMR, LAPIC_TMR_M);
259        hal_lapic_write(LAPIC_LVT_LINT0, LAPIC_LINT_M);
260        hal_lapic_write(LAPIC_LVT_LINT1, LAPIC_LINT_M);
261        hal_lapic_write(LAPIC_LVT_PMC, LAPIC_PMC_M);
262        hal_lapic_write(LAPIC_LVT_THM, LAPIC_THM_M);
263        hal_lapic_write(LAPIC_LVT_ERR, LAPIC_ERR_M);
264
265        /* Now, enable the timer in repeated mode. */
266        hal_lapic_write(LAPIC_LVT_TMR, LAPIC_TMR_TM|LAPIC_TMR_M);
267        hal_lapic_write(LAPIC_DCR_TIMER, LAPIC_DCRT_DIV1);
268        hal_lapic_calibrate();
269        hal_lapic_write(LAPIC_LVT_TMR, LAPIC_TMR_TM|LAPIC_TIMER_VECTOR);
270}
271
272/* -------------------------------------------------------------------------- */
273
274void hal_apic_init()
275{
276        /* Disable the PIC */
277        hal_pic_init();
278
279        /* Enable the LAPIC */
280        hal_lapic_init();
281
282        /* Enable the IOAPIC */
283        hal_ioapic_init();
284}
285
Note: See TracBrowser for help on using the repository browser.