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
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.
13 * Copyright 2015, Joyent, Inc.
17 * The microfind() routine is used to calibrate the delay provided by
18 * tenmicrosec(). Early in boot gethrtime() is not yet configured and
19 * available for accurate delays, but some drivers still need to be able to
20 * pause execution for rough increments of ten microseconds. To that end,
21 * microfind() will measure the wall time elapsed during a simple delay loop
22 * using the Intel 8254 Programmable Interval Timer (PIT), and attempt to find
23 * a loop count that approximates a ten microsecond delay.
25 * This mechanism is accurate enough when running unvirtualised on real CPUs,
26 * but is somewhat less efficacious in a virtual machine. In a virtualised
27 * guest the relationship between instruction completion and elapsed wall time
28 * is, at best, variable; on such machines the calibration is merely a rough
32 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/inline.h>
37 #include <sys/machlock.h>
38 #include <sys/avintr.h>
39 #include <sys/smp_impldefs.h>
40 #include <sys/archsystm.h>
41 #include <sys/systm.h>
42 #include <sys/machsystm.h>
45 * Loop count for 10 microsecond wait. MUST be initialized for those who
46 * insist on calling "tenmicrosec" before the clock has been initialized.
48 unsigned int microdata
= 50;
51 * These values, used later in microfind(), are stored in globals to allow them
52 * to be adjusted more easily via kmdb.
54 unsigned int microdata_trial_count
= 7;
55 unsigned int microdata_allowed_failures
= 3;
59 microfind_pit_reprogram_for_bios(void)
62 * Restore PIT counter 0 for BIOS use in mode 3 -- "Square Wave
65 outb(PITCTL_PORT
, PIT_C0
| PIT_LOADMODE
| PIT_SQUAREMODE
);
68 * Load an initial counter value of zero.
70 outb(PITCTR0_PORT
, 0);
71 outb(PITCTR0_PORT
, 0);
75 * Measure the run time of tenmicrosec() using the Intel 8254 Programmable
76 * Interval Timer. The timer operates at 1.193182 Mhz, so each timer tick
77 * represents 0.8381 microseconds of wall time. This function returns the
78 * number of such ticks that passed while tenmicrosec() was running, or
79 * -1 if the delay was too long to measure with the PIT.
82 microfind_pit_delta(void)
88 * Configure PIT counter 0 in mode 0 -- "Interrupt On Terminal Count".
89 * In this mode, the PIT will count down from the loaded value and
90 * set its output bit high once it reaches zero. The PIT will pause
91 * until we write the low byte and then the high byte to the counter
94 outb(PITCTL_PORT
, PIT_LOADMODE
);
97 * Load the maximum counter value, 0xffff, into the counter port.
99 outb(PITCTR0_PORT
, 0xff);
100 outb(PITCTR0_PORT
, 0xff);
103 * Run the delay function.
108 * Latch the counter value and status for counter 0 with the read
111 outb(PITCTL_PORT
, PIT_READBACK
| PIT_READBACKC0
);
114 * In read back mode, three values are read from the counter port
115 * in order: the status byte, followed by the low byte and high
116 * byte of the counter value.
118 status
= inb(PITCTR0_PORT
);
119 count
= inb(PITCTR0_PORT
);
120 count
|= inb(PITCTR0_PORT
) << 8;
123 * Verify that the counter started counting down. The null count
124 * flag in the status byte is set when we load a value, and cleared
125 * when counting operation begins.
127 if (status
& (1 << PITSTAT_NULLCNT
)) {
129 * The counter did not begin. This means the loop count
130 * used by tenmicrosec is too small for this CPU. We return
131 * a zero count to represent that the delay was too small
138 * Verify that the counter did not wrap around. The output pin is
139 * reset when we load a new counter value, and set once the counter
142 if (status
& (1 << PITSTAT_OUTPUT
)) {
144 * The counter reached zero before we were able to read the
145 * value. This means the loop count used by tenmicrosec is too
146 * large for this CPU.
152 * The PIT counts from our initial load value of 0xffff down to zero.
153 * Return the number of timer ticks that passed while tenmicrosec was
156 VERIFY(count
<= 0xffff);
157 return (0xffff - count
);
161 microfind_pit_delta_avg(int trials
, int allowed_failures
)
165 long long int total
= 0;
167 while (tc
< trials
) {
170 if ((d
= microfind_pit_delta()) < 0) {
172 * If the counter wrapped, we cannot use this
173 * data point in the average. Record the failure
176 if (++failures
> allowed_failures
) {
199 * Disable interrupts while we measure the speed of the CPU.
201 s
= clear_int_flag();
204 * Start at the smallest loop count, i.e. 1, and keep doubling
205 * until a delay of ~10ms can be measured.
209 int ticksprev
= ticks
;
212 * We use a trial count of 7 to attempt to smooth out jitter
213 * caused by the scheduling of virtual machines. We only allow
214 * three failures, as each failure represents a wrapped counter
215 * and an expired wall time of at least ~55ms.
217 if ((ticks
= microfind_pit_delta_avg(microdata_trial_count
,
218 microdata_allowed_failures
)) < 0) {
220 * The counter wrapped. Halve the counter, restore the
221 * previous ticks count and break out of the loop.
223 if (microdata
<= 1) {
225 * If the counter wrapped on the first try,
226 * then we have some serious problems.
228 panic("microfind: pit counter always wrapped");
230 microdata
= microdata
>> 1;
235 if (ticks
> 0x3000) {
237 * The loop ran for at least ~10ms worth of 0.8381us
241 } else if (microdata
> (UINT_MAX
>> 1)) {
243 * Doubling the loop count again would cause an
244 * overflow. Use what we have.
249 * Double and try again.
251 microdata
= microdata
<< 1;
257 * If we were unable to measure a positive PIT tick count, then
258 * we will be unable to scale the value of "microdata"
261 panic("microfind: could not calibrate delay loop");
265 * Calculate the loop count based on the final PIT tick count and the
266 * loop count. Each PIT tick represents a duration of ~0.8381us, so we
267 * want to adjust microdata to represent a duration of 12 ticks, or
270 microdata
= (long long)microdata
* 12LL / (long long)ticks
;
273 * Try and leave things as we found them.
275 microfind_pit_reprogram_for_bios();
278 * Restore previous interrupt state.