2 * Copyright (c) 2014 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
3 * Copyright (c) 2011 NetApp, Inc.
5 * Copyright (c) 2018 Joyent, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/systm.h>
41 #include <machine/vmm.h>
48 static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)");
50 #define VATPIT_LOCK(vatpit) mtx_lock_spin(&((vatpit)->mtx))
51 #define VATPIT_UNLOCK(vatpit) mtx_unlock_spin(&((vatpit)->mtx))
52 #define VATPIT_LOCKED(vatpit) mtx_owned(&((vatpit)->mtx))
54 #define TIMER_SEL_MASK 0xc0
55 #define TIMER_RW_MASK 0x30
56 #define TIMER_MODE_MASK 0x0f
57 #define TIMER_SEL_READBACK 0xc0
59 #define TIMER_STS_OUT 0x80
60 #define TIMER_STS_NULLCNT 0x40
62 #define TIMER_RB_LCTR 0x20
63 #define TIMER_RB_LSTATUS 0x10
64 #define TIMER_RB_CTR_2 0x08
65 #define TIMER_RB_CTR_1 0x04
66 #define TIMER_RB_CTR_0 0x02
68 #define TMR2_OUT_STS 0x20
70 #define PIT_8254_FREQ 1193182
71 #define TIMER_DIV(freq, hz) (((freq) + (hz) / 2) / (hz))
73 struct vatpit_callout_arg {
74 struct vatpit *vatpit;
81 uint16_t initial; /* initial counter value */
82 struct bintime now_bt; /* uptime when counter was loaded */
85 bool slatched; /* status latched */
90 struct callout callout;
91 struct bintime callout_bt; /* target time */
92 struct vatpit_callout_arg callout_arg;
99 struct bintime freq_bt;
101 struct channel channel[3];
104 static void pit_timer_start_cntr0(struct vatpit *vatpit);
107 vatpit_delta_ticks(struct vatpit *vatpit, struct channel *c)
109 struct bintime delta;
113 bintime_sub(&delta, &c->now_bt);
115 result = delta.sec * PIT_8254_FREQ;
116 result += delta.frac / vatpit->freq_bt.frac;
122 vatpit_get_out(struct vatpit *vatpit, int channel)
125 uint64_t delta_ticks;
128 c = &vatpit->channel[channel];
132 delta_ticks = vatpit_delta_ticks(vatpit, c);
133 out = (delta_ticks >= c->initial);
144 vatpit_callout_handler(void *a)
146 struct vatpit_callout_arg *arg = a;
147 struct vatpit *vatpit;
148 struct callout *callout;
151 vatpit = arg->vatpit;
152 c = &vatpit->channel[arg->channel_num];
153 callout = &c->callout;
155 VM_CTR1(vatpit->vm, "atpit t%d fired", arg->channel_num);
159 if (callout_pending(callout)) /* callout was reset */
162 if (!callout_active(callout)) /* callout was stopped */
165 callout_deactivate(callout);
167 if (c->mode == TIMER_RATEGEN) {
168 pit_timer_start_cntr0(vatpit);
171 vatpic_pulse_irq(vatpit->vm, 0);
172 vioapic_pulse_irq(vatpit->vm, 2);
175 VATPIT_UNLOCK(vatpit);
180 pit_timer_start_cntr0(struct vatpit *vatpit)
183 struct bintime now, delta;
184 sbintime_t precision;
186 c = &vatpit->channel[0];
187 if (c->initial != 0) {
189 delta.frac = vatpit->freq_bt.frac * c->initial;
190 bintime_add(&c->callout_bt, &delta);
191 precision = bttosbt(delta) >> tc_precexp;
194 * Reset 'callout_bt' if the time that the callout
195 * was supposed to fire is more than 'c->initial'
199 if (bintime_cmp(&c->callout_bt, &now, <)) {
201 bintime_add(&c->callout_bt, &delta);
204 callout_reset_sbt(&c->callout, bttosbt(c->callout_bt),
205 precision, vatpit_callout_handler, &c->callout_arg,
211 pit_update_counter(struct vatpit *vatpit, struct channel *c, bool latch)
214 uint64_t delta_ticks;
216 /* cannot latch a new value until the old one has been consumed */
217 if (latch && c->olbyte != 0)
220 if (c->initial == 0) {
222 * This is possibly an o/s bug - reading the value of
223 * the timer without having set up the initial value.
225 * The original user-space version of this code set
226 * the timer to 100hz in this condition; do the same
229 c->initial = TIMER_DIV(PIT_8254_FREQ, 100);
230 binuptime(&c->now_bt);
231 c->status &= ~TIMER_STS_NULLCNT;
234 delta_ticks = vatpit_delta_ticks(vatpit, c);
235 lval = c->initial - delta_ticks % c->initial;
239 c->ol[1] = lval; /* LSB */
240 c->ol[0] = lval >> 8; /* MSB */
247 pit_readback1(struct vatpit *vatpit, int channel, uint8_t cmd)
251 c = &vatpit->channel[channel];
254 * Latch the count/status of the timer if not already latched.
255 * N.B. that the count/status latch-select bits are active-low.
257 if (!(cmd & TIMER_RB_LCTR) && !c->olbyte) {
258 (void) pit_update_counter(vatpit, c, true);
261 if (!(cmd & TIMER_RB_LSTATUS) && !c->slatched) {
264 * For mode 0, see if the elapsed time is greater
265 * than the initial value - this results in the
266 * output pin being set to 1 in the status byte.
268 if (c->mode == TIMER_INTTC && vatpit_get_out(vatpit, channel))
269 c->status |= TIMER_STS_OUT;
271 c->status &= ~TIMER_STS_OUT;
278 pit_readback(struct vatpit *vatpit, uint8_t cmd)
283 * The readback command can apply to all timers.
286 if (cmd & TIMER_RB_CTR_0)
287 error = pit_readback1(vatpit, 0, cmd);
288 if (!error && cmd & TIMER_RB_CTR_1)
289 error = pit_readback1(vatpit, 1, cmd);
290 if (!error && cmd & TIMER_RB_CTR_2)
291 error = pit_readback1(vatpit, 2, cmd);
298 vatpit_update_mode(struct vatpit *vatpit, uint8_t val)
303 sel = val & TIMER_SEL_MASK;
304 rw = val & TIMER_RW_MASK;
305 mode = val & TIMER_MODE_MASK;
307 if (sel == TIMER_SEL_READBACK)
308 return (pit_readback(vatpit, val));
310 if (rw != TIMER_LATCH && rw != TIMER_16BIT)
313 if (rw != TIMER_LATCH) {
315 * Counter mode is not affected when issuing a
318 if (mode != TIMER_INTTC &&
319 mode != TIMER_RATEGEN &&
320 mode != TIMER_SQWAVE &&
321 mode != TIMER_SWSTROBE)
325 c = &vatpit->channel[sel >> 6];
326 if (rw == TIMER_LATCH)
327 pit_update_counter(vatpit, c, true);
330 c->olbyte = 0; /* reset latch after reprogramming */
331 c->status |= TIMER_STS_NULLCNT;
338 vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
341 struct vatpit *vatpit;
346 vatpit = vm_atpit(vm);
353 if (port == TIMER_MODE) {
355 VM_CTR0(vatpit->vm, "vatpit attempt to read mode");
360 error = vatpit_update_mode(vatpit, val);
361 VATPIT_UNLOCK(vatpit);
367 KASSERT(port >= TIMER_CNTR0 && port <= TIMER_CNTR2,
368 ("invalid port 0x%x", port));
369 c = &vatpit->channel[port - TIMER_CNTR0];
372 if (in && c->slatched) {
374 * Return the status byte if latched
381 * The spec says that once the output latch is completely
382 * read it should revert to "following" the counter. Use
383 * the free running counter for this case (i.e. Linux
384 * TSC calibration). Assuming the access mode is 16-bit,
385 * toggle the MSB/LSB bit on each read.
387 if (c->olbyte == 0) {
390 tmp = pit_update_counter(vatpit, c, false);
397 *eax = c->ol[--c->olbyte];
399 c->cr[c->crbyte++] = *eax;
400 if (c->crbyte == 2) {
401 c->status &= ~TIMER_STS_NULLCNT;
404 c->initial = c->cr[0] | (uint16_t)c->cr[1] << 8;
405 binuptime(&c->now_bt);
406 /* Start an interval timer for channel 0 */
407 if (port == TIMER_CNTR0) {
408 c->callout_bt = c->now_bt;
409 pit_timer_start_cntr0(vatpit);
415 VATPIT_UNLOCK(vatpit);
421 vatpit_nmisc_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
424 struct vatpit *vatpit;
426 vatpit = vm_atpit(vm);
430 if (vatpit_get_out(vatpit, 2))
435 VATPIT_UNLOCK(vatpit);
442 vatpit_init(struct vm *vm)
444 struct vatpit *vatpit;
445 struct vatpit_callout_arg *arg;
448 vatpit = malloc(sizeof(struct vatpit), M_VATPIT, M_WAITOK | M_ZERO);
451 mtx_init(&vatpit->mtx, "vatpit lock", NULL, MTX_SPIN);
453 FREQ2BT(PIT_8254_FREQ, &vatpit->freq_bt);
455 for (i = 0; i < 3; i++) {
456 callout_init(&vatpit->channel[i].callout, 1);
457 arg = &vatpit->channel[i].callout_arg;
458 arg->vatpit = vatpit;
459 arg->channel_num = i;
466 vatpit_cleanup(struct vatpit *vatpit)
470 for (i = 0; i < 3; i++)
471 callout_drain(&vatpit->channel[i].callout);
473 free(vatpit, M_VATPIT);