1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use crate::frame_allocator::PhysicalMemRegion;
use crate::mem::PhysicalAddress;
use crate::paging;
use crate::paging::MappingAccessRights;
use crate::paging::PAGE_SIZE;
use static_assertions::assert_eq_size;
use sunrise_libutils::io::{Io, Mmio};
use core::fmt;
use core::fmt::Debug;
use core::fmt::Formatter;
use crate::timer;
bitfield! {
#[derive(Clone, Copy, Debug)]
pub struct HpetIdRegister(u32);
pub revision_id, _ : 7, 0;
pub timer_count_minus_one, _ : 12, 8;
pub counter_size_capability, _: 13;
pub legacy_rt_capability, _: 15;
pub vendor_id, _: 31, 16;
}
bitfield! {
#[derive(Clone, Copy, Debug)]
pub struct HpetGeneralConfigurationRegister(u32);
pub enable_config, set_enable_config: 0;
pub legacy_rt_config, set_legacy_rt_config: 1;
}
bitfield! {
#[derive(Clone, Copy, Debug)]
pub struct HpetTimerConfigurationRegister(u32);
pub interrupt_type, set_interrupt_type: 1;
pub interrupt_enable, set_interrupt_enable: 2;
pub timer_type, set_timer_type: 3;
pub periodic_interrupt_capability, _: 4;
pub size_capability, _: 5;
pub accumulator_config, set_accumulator_config: 6;
pub is_32bit_mode, set_32bit_mode: 8;
pub interrupt_route, set_interrupt_route: 13, 9;
pub fsb_interrupt, set_fsb_interrupt: 14;
pub fsb_interrupt_capability, _: 15;
}
#[allow(clippy::missing_docs_in_private_items)]
#[repr(packed)]
pub struct HpetRegister {
pub identifier: Mmio<HpetIdRegister>,
pub period: Mmio<u32>,
_reserved0: u64,
pub general_configuration: Mmio<HpetGeneralConfigurationRegister>,
_reserved1: u32,
_reserved2: u64,
pub general_interrupt_status: Mmio<u32>,
_reserved3: [u8; 0xCC],
pub main_counter_value: Mmio<u64>,
_reserved4: u64,
}
impl Debug for HpetRegister {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
f.debug_struct("HpetRegister")
.field("identifier", &self.identifier)
.field("period", &self.period)
.field("general_configuration", &self.general_configuration)
.field("general_interrupt_status", &self.general_interrupt_status)
.field("main_counter_value", &self.main_counter_value)
.finish()
}
}
#[derive(Debug)]
pub struct HpetTimerRegister {
pub config: Mmio<HpetTimerConfigurationRegister>,
pub interrupt_route_capability: Mmio<u32>,
pub comparator_value_low: Mmio<u32>,
pub comparator_value_high: Mmio<u32>,
pub fsb_value: Mmio<u32>,
pub fsb_address: Mmio<u32>,
}
#[derive(Debug)]
pub struct Hpet {
pub inner: *mut HpetRegister,
period: u32,
timer_count: u32,
}
#[derive(Debug)]
pub struct HpetTimer {
inner: *mut HpetTimerRegister,
support_64bit: bool,
support_periodic_interrupt: bool,
support_fsb_interrupt: bool,
interrupt_route_capability: u32,
}
impl HpetTimer {
const MAX_IRQ: u32 = 0x1F;
fn new(inner: *mut HpetTimerRegister) -> Self {
let mut res = HpetTimer {
inner,
support_64bit: false,
support_periodic_interrupt: false,
support_fsb_interrupt: false,
interrupt_route_capability: 0,
};
let config = unsafe { (*inner).config.read() };
res.support_64bit = config.size_capability();
res.support_periodic_interrupt = config.periodic_interrupt_capability();
res.support_fsb_interrupt = config.fsb_interrupt_capability();
res.interrupt_route_capability = unsafe { (*inner).interrupt_route_capability.read() };
res
}
pub fn support_64bit(&self) -> bool {
self.support_64bit
}
pub fn support_periodic_interrupt(&self) -> bool {
self.support_periodic_interrupt
}
pub fn support_fsb_interrupt(&self) -> bool {
self.support_fsb_interrupt
}
pub fn support_interrupt_routing(&self, index: u32) -> bool {
if index > Self::MAX_IRQ {
return false;
}
let irq_mask = 1 << index;
(self.interrupt_route_capability & irq_mask) == irq_mask
}
pub fn set_interrupt_route(&self, index: u32) {
assert!(self.support_interrupt_routing(index), "Illegal interrupt route (as claimed). Supported routes: {}.", self.interrupt_route_capability);
let mut config = unsafe { (*self.inner).config.read() };
config.set_interrupt_route(index);
unsafe { (*self.inner).config.write(config); }
let config = unsafe { (*self.inner).config.read() };
assert!(config.interrupt_route() == index, "Illegal interrupt route (as tested). Supported routes: {}.", self.interrupt_route_capability);
}
pub fn set_comparator_value(&self, value: u64) {
unsafe {
(*self.inner)
.comparator_value_low
.write((value & 0xFFFF_FFFF) as u32)
};
unsafe {
(*self.inner)
.comparator_value_high
.write((value >> 32) as u32)
};
}
pub fn set_accumulator_value(&self, value: u64) {
let mut config = unsafe { (*self.inner).config.read() };
config.set_accumulator_config(true);
unsafe { (*self.inner).config.write(config) };
unsafe {
(*self.inner)
.comparator_value_low
.write((value & 0xFFFF_FFFF) as u32)
};
let mut config = unsafe { (*self.inner).config.read() };
config.set_accumulator_config(true);
unsafe { (*self.inner).config.write(config) };
unsafe {
(*self.inner)
.comparator_value_high
.write((value >> 32) as u32)
};
}
pub fn set_edge_trigger(&self) {
let mut config = unsafe { (*self.inner).config.read() };
config.set_interrupt_type(false);
unsafe { (*self.inner).config.write(config) };
}
pub fn set_level_trigger(&self) {
let mut config = unsafe { (*self.inner).config.read() };
config.set_interrupt_type(true);
unsafe { (*self.inner).config.write(config) };
}
pub fn set_one_shot_mode(&self) {
let mut config = unsafe { (*self.inner).config.read() };
config.set_timer_type(false);
unsafe { (*self.inner).config.write(config) };
}
pub fn set_periodic_mode(&self) {
let mut config = unsafe { (*self.inner).config.read() };
config.set_timer_type(true);
unsafe { (*self.inner).config.write(config) };
}
pub fn enable_interrupt(&self) {
let mut config = unsafe { (*self.inner).config.read() };
config.set_interrupt_enable(true);
unsafe { (*self.inner).config.write(config) };
}
pub fn disable_interrupt(&self) {
let mut config = unsafe { (*self.inner).config.read() };
config.set_interrupt_enable(false);
unsafe { (*self.inner).config.write(config) };
}
pub fn has_interrupt_enabled(&self) -> bool {
unsafe { (*self.inner).config.read().interrupt_enable() }
}
}
impl Hpet {
fn new(inner: *mut HpetRegister) -> Self {
debug!("Creating new HPET with registers at {:p}", inner);
let mut res = Hpet {
inner,
timer_count: 1,
period: 0,
};
res.timer_count = unsafe { (*res.inner).identifier.read().timer_count_minus_one() } + 1;
res.period = unsafe { (*res.inner).period.read() };
res
}
pub fn has_legacy_mapping(&self) -> bool {
unsafe { (*self.inner).identifier.read().legacy_rt_capability() }
}
pub fn get_period(&self) -> u32 {
self.period
}
pub fn get_frequency(&self) -> u64 {
1000000000000000 / u64::from(self.get_period())
}
pub fn enable_legacy_mapping(&self) {
let mut general_configuration = unsafe { (*self.inner).general_configuration.read() };
general_configuration.set_legacy_rt_config(true);
unsafe {
(*self.inner)
.general_configuration
.write(general_configuration)
}
}
pub fn disable_legacy_mapping(&self) {
let mut general_configuration = unsafe { (*self.inner).general_configuration.read() };
general_configuration.set_legacy_rt_config(false);
unsafe {
(*self.inner)
.general_configuration
.write(general_configuration)
}
}
pub fn is_legacy_mapping_enabled(&self) -> bool {
let general_configuration = unsafe { (*self.inner).general_configuration.read() };
general_configuration.legacy_rt_config()
}
pub fn enable(&self) {
let mut general_configuration = unsafe { (*self.inner).general_configuration.read() };
general_configuration.set_enable_config(true);
unsafe {
(*self.inner)
.general_configuration
.write(general_configuration)
}
}
pub fn set_main_counter_value(&self, value: u64) {
unsafe {
(*self.inner)
.main_counter_value
.write(value)
}
}
pub fn get_main_counter_value(&self) -> u64 {
unsafe { (*self.inner).main_counter_value.read() }
}
pub fn disable(&self) {
let mut general_configuration = unsafe { (*self.inner).general_configuration.read() };
general_configuration.set_enable_config(false);
unsafe {
(*self.inner)
.general_configuration
.write(general_configuration)
}
}
pub fn is_enabled(&self) -> bool {
let general_configuration = unsafe { (*self.inner).general_configuration.read() };
general_configuration.enable_config()
}
pub fn get_timer(&self, index: u32) -> Option<HpetTimer> {
if index >= self.timer_count {
return None;
}
let mmio_base_address = self.inner as usize;
let timer_address = mmio_base_address + 0x100 + (0x20 * index) as usize;
Some(HpetTimer::new(timer_address as *mut HpetTimerRegister))
}
}
assert_eq_size!(HpetRegister, [u8; 0x100]);
static mut HPET_INSTANCE: Option<Hpet> = None;
pub unsafe fn init(hpet: &acpi::Hpet) -> bool {
let physical_mem = PhysicalMemRegion::on_fixed_mmio(
PhysicalAddress(hpet.base_address.address as usize),
PAGE_SIZE,
)
.unwrap();
let virtual_address = paging::kernel_memory::get_kernel_memory().map_phys_region(
physical_mem,
MappingAccessRights::READABLE | MappingAccessRights::WRITABLE,
);
let hpet_mmio = virtual_address.addr() as *mut HpetRegister;
let hpet_instance = Hpet::new(hpet_mmio);
if hpet_instance.is_enabled() {
hpet_instance.disable();
hpet_instance.set_main_counter_value(0);
}
if !hpet_instance.has_legacy_mapping() {
paging::kernel_memory::get_kernel_memory().unmap(virtual_address, PAGE_SIZE);
return false;
}
let main_timer_opt = hpet_instance.get_timer(0);
if main_timer_opt.is_none() {
paging::kernel_memory::get_kernel_memory().unmap(virtual_address, PAGE_SIZE);
return false;
}
let main_timer = main_timer_opt.unwrap();
if !main_timer.support_periodic_interrupt() {
paging::kernel_memory::get_kernel_memory().unmap(virtual_address, PAGE_SIZE);
return false;
}
let irq_period_ns = 1 * 1_000_000;
let irq_period_fs = irq_period_ns * 1_000_000;
info!("HPET frequency: {} Hz", hpet_instance.get_frequency());
info!("HPET IRQ period: {} fs", irq_period_fs);
let irq_period_tick = irq_period_fs / u64::from(hpet_instance.get_period());
main_timer.set_edge_trigger();
main_timer.set_periodic_mode();
main_timer.enable_interrupt();
main_timer.set_accumulator_value(irq_period_tick);
main_timer.set_comparator_value(irq_period_tick);
main_timer.set_interrupt_route(16);
hpet_instance.enable();
timer::set_kernel_timer_info(16, hpet_instance.get_frequency(), irq_period_ns);
HPET_INSTANCE = Some(hpet_instance);
true
}