xref: /illumos-gate/usr/src/test/bhyve-tests/tests/kdev/payload_vhpet_freq.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2022 Oxide Computer Company
14  */
15 
16 #include "payload_common.h"
17 #include "payload_utils.h"
18 #include "test_defs.h"
19 
20 #define	HPET_OFF_CONFIG			0x10
21 #define	HPET_OFF_MAIN_COUNT_LOW		0xf0
22 
23 #define	HPET_CONFIG_ENABLE	1
24 
25 
26 static void
27 write_hpet(uint_t reg, uint32_t value)
28 {
29 	volatile uint32_t *ptr = (uint32_t *)(MMIO_HPET_BASE + reg);
30 	*ptr = value;
31 }
32 
33 static uint32_t
34 read_hpet_main_low(void)
35 {
36 	volatile uint32_t *ptr =
37 	    (uint32_t *)(MMIO_HPET_BASE + HPET_OFF_MAIN_COUNT_LOW);
38 	return (*ptr);
39 }
40 
41 
42 void
43 start(void)
44 {
45 	write_hpet(HPET_OFF_CONFIG, HPET_CONFIG_ENABLE);
46 
47 	/* loop for as long as the host wants */
48 	for (;;) {
49 		uint32_t start, end;
50 
51 		start = read_hpet_main_low();
52 		outl(IOP_TEST_VALUE, start);
53 
54 		do {
55 			end = read_hpet_main_low();
56 			/* wait for enough ticks to pass */
57 		} while (end < (start + HPET_TARGET_TICKS));
58 		outl(IOP_TEST_VALUE, end);
59 	}
60 }
61