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 "opt_bhyve_snapshot.h"
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/queue.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/systm.h>
43 #include <machine/vmm.h>
44 #include <machine/vmm_snapshot.h>
51 static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)");
53 #define VATPIT_LOCK(vatpit) mtx_lock_spin(&((vatpit)->mtx))
54 #define VATPIT_UNLOCK(vatpit) mtx_unlock_spin(&((vatpit)->mtx))
55 #define VATPIT_LOCKED(vatpit) mtx_owned(&((vatpit)->mtx))
57 #define TIMER_SEL_MASK 0xc0
58 #define TIMER_RW_MASK 0x30
59 #define TIMER_MODE_MASK 0x0f
60 #define TIMER_SEL_READBACK 0xc0
62 #define TIMER_STS_OUT 0x80
63 #define TIMER_STS_NULLCNT 0x40
65 #define TIMER_RB_LCTR 0x20
66 #define TIMER_RB_LSTATUS 0x10
67 #define TIMER_RB_CTR_2 0x08
68 #define TIMER_RB_CTR_1 0x04
69 #define TIMER_RB_CTR_0 0x02
71 #define TMR2_OUT_STS 0x20
73 #define PIT_8254_FREQ 1193182
74 #define TIMER_DIV(freq, hz) (((freq) + (hz) / 2) / (hz))
76 struct vatpit_callout_arg {
77 struct vatpit *vatpit;
84 uint16_t initial; /* initial counter value */
85 struct bintime now_bt; /* uptime when counter was loaded */
88 bool slatched; /* status latched */
93 struct callout callout;
94 struct bintime callout_bt; /* target time */
95 struct vatpit_callout_arg callout_arg;
102 struct bintime freq_bt;
104 struct channel channel[3];
107 static void pit_timer_start_cntr0(struct vatpit *vatpit);
110 vatpit_delta_ticks(struct vatpit *vatpit, struct channel *c)
112 struct bintime delta;
116 bintime_sub(&delta, &c->now_bt);
118 result = delta.sec * PIT_8254_FREQ;
119 result += delta.frac / vatpit->freq_bt.frac;
125 vatpit_get_out(struct vatpit *vatpit, int channel)
128 uint64_t delta_ticks;
131 c = &vatpit->channel[channel];
135 delta_ticks = vatpit_delta_ticks(vatpit, c);
136 out = (delta_ticks >= c->initial);
147 vatpit_callout_handler(void *a)
149 struct vatpit_callout_arg *arg = a;
150 struct vatpit *vatpit;
151 struct callout *callout;
154 vatpit = arg->vatpit;
155 c = &vatpit->channel[arg->channel_num];
156 callout = &c->callout;
158 VM_CTR1(vatpit->vm, "atpit t%d fired", arg->channel_num);
162 if (callout_pending(callout)) /* callout was reset */
165 if (!callout_active(callout)) /* callout was stopped */
168 callout_deactivate(callout);
170 if (c->mode == TIMER_RATEGEN) {
171 pit_timer_start_cntr0(vatpit);
174 vatpic_pulse_irq(vatpit->vm, 0);
175 vioapic_pulse_irq(vatpit->vm, 2);
178 VATPIT_UNLOCK(vatpit);
183 pit_timer_start_cntr0(struct vatpit *vatpit)
186 struct bintime now, delta;
187 sbintime_t precision;
189 c = &vatpit->channel[0];
190 if (c->initial != 0) {
192 delta.frac = vatpit->freq_bt.frac * c->initial;
193 bintime_add(&c->callout_bt, &delta);
194 precision = bttosbt(delta) >> tc_precexp;
197 * Reset 'callout_bt' if the time that the callout
198 * was supposed to fire is more than 'c->initial'
202 if (bintime_cmp(&c->callout_bt, &now, <)) {
204 bintime_add(&c->callout_bt, &delta);
207 callout_reset_sbt(&c->callout, bttosbt(c->callout_bt),
208 precision, vatpit_callout_handler, &c->callout_arg,
214 pit_update_counter(struct vatpit *vatpit, struct channel *c, bool latch)
217 uint64_t delta_ticks;
219 /* cannot latch a new value until the old one has been consumed */
220 if (latch && c->olbyte != 0)
223 if (c->initial == 0) {
225 * This is possibly an o/s bug - reading the value of
226 * the timer without having set up the initial value.
228 * The original user-space version of this code set
229 * the timer to 100hz in this condition; do the same
232 c->initial = TIMER_DIV(PIT_8254_FREQ, 100);
233 binuptime(&c->now_bt);
234 c->status &= ~TIMER_STS_NULLCNT;
237 delta_ticks = vatpit_delta_ticks(vatpit, c);
238 lval = c->initial - delta_ticks % c->initial;
242 c->ol[1] = lval; /* LSB */
243 c->ol[0] = lval >> 8; /* MSB */
250 pit_readback1(struct vatpit *vatpit, int channel, uint8_t cmd)
254 c = &vatpit->channel[channel];
257 * Latch the count/status of the timer if not already latched.
258 * N.B. that the count/status latch-select bits are active-low.
260 if (!(cmd & TIMER_RB_LCTR) && !c->olbyte) {
261 (void) pit_update_counter(vatpit, c, true);
264 if (!(cmd & TIMER_RB_LSTATUS) && !c->slatched) {
267 * For mode 0, see if the elapsed time is greater
268 * than the initial value - this results in the
269 * output pin being set to 1 in the status byte.
271 if (c->mode == TIMER_INTTC && vatpit_get_out(vatpit, channel))
272 c->status |= TIMER_STS_OUT;
274 c->status &= ~TIMER_STS_OUT;
281 pit_readback(struct vatpit *vatpit, uint8_t cmd)
286 * The readback command can apply to all timers.
289 if (cmd & TIMER_RB_CTR_0)
290 error = pit_readback1(vatpit, 0, cmd);
291 if (!error && cmd & TIMER_RB_CTR_1)
292 error = pit_readback1(vatpit, 1, cmd);
293 if (!error && cmd & TIMER_RB_CTR_2)
294 error = pit_readback1(vatpit, 2, cmd);
301 vatpit_update_mode(struct vatpit *vatpit, uint8_t val)
306 sel = val & TIMER_SEL_MASK;
307 rw = val & TIMER_RW_MASK;
308 mode = val & TIMER_MODE_MASK;
310 if (sel == TIMER_SEL_READBACK)
311 return (pit_readback(vatpit, val));
313 if (rw != TIMER_LATCH && rw != TIMER_16BIT)
316 if (rw != TIMER_LATCH) {
318 * Counter mode is not affected when issuing a
321 if (mode != TIMER_INTTC &&
322 mode != TIMER_RATEGEN &&
323 mode != TIMER_SQWAVE &&
324 mode != TIMER_SWSTROBE)
328 c = &vatpit->channel[sel >> 6];
329 if (rw == TIMER_LATCH)
330 pit_update_counter(vatpit, c, true);
333 c->olbyte = 0; /* reset latch after reprogramming */
334 c->status |= TIMER_STS_NULLCNT;
341 vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
344 struct vatpit *vatpit;
349 vatpit = vm_atpit(vm);
356 if (port == TIMER_MODE) {
358 VM_CTR0(vatpit->vm, "vatpit attempt to read mode");
363 error = vatpit_update_mode(vatpit, val);
364 VATPIT_UNLOCK(vatpit);
370 KASSERT(port >= TIMER_CNTR0 && port <= TIMER_CNTR2,
371 ("invalid port 0x%x", port));
372 c = &vatpit->channel[port - TIMER_CNTR0];
375 if (in && c->slatched) {
377 * Return the status byte if latched
384 * The spec says that once the output latch is completely
385 * read it should revert to "following" the counter. Use
386 * the free running counter for this case (i.e. Linux
387 * TSC calibration). Assuming the access mode is 16-bit,
388 * toggle the MSB/LSB bit on each read.
390 if (c->olbyte == 0) {
393 tmp = pit_update_counter(vatpit, c, false);
400 *eax = c->ol[--c->olbyte];
402 c->cr[c->crbyte++] = *eax;
403 if (c->crbyte == 2) {
404 c->status &= ~TIMER_STS_NULLCNT;
407 c->initial = c->cr[0] | (uint16_t)c->cr[1] << 8;
408 binuptime(&c->now_bt);
409 /* Start an interval timer for channel 0 */
410 if (port == TIMER_CNTR0) {
411 c->callout_bt = c->now_bt;
412 pit_timer_start_cntr0(vatpit);
418 VATPIT_UNLOCK(vatpit);
424 vatpit_nmisc_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
427 struct vatpit *vatpit;
429 vatpit = vm_atpit(vm);
433 if (vatpit_get_out(vatpit, 2))
438 VATPIT_UNLOCK(vatpit);
445 vatpit_init(struct vm *vm)
447 struct vatpit *vatpit;
448 struct vatpit_callout_arg *arg;
451 vatpit = malloc(sizeof(struct vatpit), M_VATPIT, M_WAITOK | M_ZERO);
454 mtx_init(&vatpit->mtx, "vatpit lock", NULL, MTX_SPIN);
456 FREQ2BT(PIT_8254_FREQ, &vatpit->freq_bt);
458 for (i = 0; i < 3; i++) {
459 callout_init(&vatpit->channel[i].callout, 1);
460 arg = &vatpit->channel[i].callout_arg;
461 arg->vatpit = vatpit;
462 arg->channel_num = i;
469 vatpit_cleanup(struct vatpit *vatpit)
473 for (i = 0; i < 3; i++)
474 callout_drain(&vatpit->channel[i].callout);
476 free(vatpit, M_VATPIT);
479 #ifdef BHYVE_SNAPSHOT
481 vatpit_snapshot(struct vatpit *vatpit, struct vm_snapshot_meta *meta)
485 struct channel *channel;
487 SNAPSHOT_VAR_OR_LEAVE(vatpit->freq_bt.sec, meta, ret, done);
488 SNAPSHOT_VAR_OR_LEAVE(vatpit->freq_bt.frac, meta, ret, done);
490 /* properly restore timers; they will NOT work currently */
491 printf("%s: snapshot restore does not reset timers!\r\n", __func__);
493 for (i = 0; i < nitems(vatpit->channel); i++) {
494 channel = &vatpit->channel[i];
496 SNAPSHOT_VAR_OR_LEAVE(channel->mode, meta, ret, done);
497 SNAPSHOT_VAR_OR_LEAVE(channel->initial, meta, ret, done);
498 SNAPSHOT_VAR_OR_LEAVE(channel->now_bt.sec, meta, ret, done);
499 SNAPSHOT_VAR_OR_LEAVE(channel->now_bt.frac, meta, ret, done);
500 SNAPSHOT_BUF_OR_LEAVE(channel->cr, sizeof(channel->cr),
502 SNAPSHOT_BUF_OR_LEAVE(channel->ol, sizeof(channel->ol),
504 SNAPSHOT_VAR_OR_LEAVE(channel->slatched, meta, ret, done);
505 SNAPSHOT_VAR_OR_LEAVE(channel->status, meta, ret, done);
506 SNAPSHOT_VAR_OR_LEAVE(channel->crbyte, meta, ret, done);
507 SNAPSHOT_VAR_OR_LEAVE(channel->frbyte, meta, ret, done);
508 SNAPSHOT_VAR_OR_LEAVE(channel->callout_bt.sec, meta, ret, done);
509 SNAPSHOT_VAR_OR_LEAVE(channel->callout_bt.frac, meta, ret,