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;
83 uint16_t initial; /* initial counter value */
84 struct bintime now_bt; /* uptime when counter was loaded */
87 bool slatched; /* status latched */
92 struct callout callout;
93 struct bintime callout_bt; /* target time */
94 struct vatpit_callout_arg callout_arg;
101 struct bintime freq_bt;
103 struct channel channel[3];
106 static void pit_timer_start_cntr0(struct vatpit *vatpit);
109 vatpit_delta_ticks(struct vatpit *vatpit, struct channel *c)
111 struct bintime delta;
115 bintime_sub(&delta, &c->now_bt);
117 result = delta.sec * PIT_8254_FREQ;
118 result += delta.frac / vatpit->freq_bt.frac;
124 vatpit_get_out(struct vatpit *vatpit, int channel)
127 uint64_t delta_ticks;
130 c = &vatpit->channel[channel];
134 delta_ticks = vatpit_delta_ticks(vatpit, c);
135 out = (delta_ticks >= c->initial);
146 vatpit_callout_handler(void *a)
148 struct vatpit_callout_arg *arg = a;
149 struct vatpit *vatpit;
150 struct callout *callout;
153 vatpit = arg->vatpit;
154 c = &vatpit->channel[arg->channel_num];
155 callout = &c->callout;
157 VM_CTR1(vatpit->vm, "atpit t%d fired", arg->channel_num);
161 if (callout_pending(callout)) /* callout was reset */
164 if (!callout_active(callout)) /* callout was stopped */
167 callout_deactivate(callout);
169 if (c->mode == TIMER_RATEGEN) {
170 pit_timer_start_cntr0(vatpit);
173 vatpic_pulse_irq(vatpit->vm, 0);
174 vioapic_pulse_irq(vatpit->vm, 2);
177 VATPIT_UNLOCK(vatpit);
182 pit_timer_start_cntr0(struct vatpit *vatpit)
185 struct bintime now, delta;
186 sbintime_t precision;
188 c = &vatpit->channel[0];
189 if (c->initial != 0) {
191 delta.frac = vatpit->freq_bt.frac * c->initial;
192 bintime_add(&c->callout_bt, &delta);
193 precision = bttosbt(delta) >> tc_precexp;
196 * Reset 'callout_bt' if the time that the callout
197 * was supposed to fire is more than 'c->initial'
201 if (bintime_cmp(&c->callout_bt, &now, <)) {
203 bintime_add(&c->callout_bt, &delta);
206 callout_reset_sbt(&c->callout, bttosbt(c->callout_bt),
207 precision, vatpit_callout_handler, &c->callout_arg,
213 pit_update_counter(struct vatpit *vatpit, struct channel *c, bool latch)
216 uint64_t delta_ticks;
218 /* cannot latch a new value until the old one has been consumed */
219 if (latch && c->olbyte != 0)
222 if (c->initial == 0) {
224 * This is possibly an o/s bug - reading the value of
225 * the timer without having set up the initial value.
227 * The original user-space version of this code set
228 * the timer to 100hz in this condition; do the same
231 c->initial = TIMER_DIV(PIT_8254_FREQ, 100);
232 binuptime(&c->now_bt);
233 c->status &= ~TIMER_STS_NULLCNT;
236 delta_ticks = vatpit_delta_ticks(vatpit, c);
237 lval = c->initial - delta_ticks % c->initial;
241 c->ol[1] = lval; /* LSB */
242 c->ol[0] = lval >> 8; /* MSB */
249 pit_readback1(struct vatpit *vatpit, int channel, uint8_t cmd)
253 c = &vatpit->channel[channel];
256 * Latch the count/status of the timer if not already latched.
257 * N.B. that the count/status latch-select bits are active-low.
259 if (!(cmd & TIMER_RB_LCTR) && !c->olbyte) {
260 (void) pit_update_counter(vatpit, c, true);
263 if (!(cmd & TIMER_RB_LSTATUS) && !c->slatched) {
266 * For mode 0, see if the elapsed time is greater
267 * than the initial value - this results in the
268 * output pin being set to 1 in the status byte.
270 if (c->mode == TIMER_INTTC && vatpit_get_out(vatpit, channel))
271 c->status |= TIMER_STS_OUT;
273 c->status &= ~TIMER_STS_OUT;
280 pit_readback(struct vatpit *vatpit, uint8_t cmd)
285 * The readback command can apply to all timers.
288 if (cmd & TIMER_RB_CTR_0)
289 error = pit_readback1(vatpit, 0, cmd);
290 if (!error && cmd & TIMER_RB_CTR_1)
291 error = pit_readback1(vatpit, 1, cmd);
292 if (!error && cmd & TIMER_RB_CTR_2)
293 error = pit_readback1(vatpit, 2, cmd);
299 vatpit_update_mode(struct vatpit *vatpit, uint8_t val)
304 sel = val & TIMER_SEL_MASK;
305 rw = val & TIMER_RW_MASK;
306 mode = val & TIMER_MODE_MASK;
308 if (sel == TIMER_SEL_READBACK)
309 return (pit_readback(vatpit, val));
311 if (rw != TIMER_LATCH && rw != TIMER_16BIT)
314 if (rw != TIMER_LATCH) {
316 * Counter mode is not affected when issuing a
319 if (mode != TIMER_INTTC &&
320 mode != TIMER_RATEGEN &&
321 mode != TIMER_SQWAVE &&
322 mode != TIMER_SWSTROBE)
326 c = &vatpit->channel[sel >> 6];
327 if (rw == TIMER_LATCH)
328 pit_update_counter(vatpit, c, true);
331 c->olbyte = 0; /* reset latch after reprogramming */
332 c->status |= TIMER_STS_NULLCNT;
339 vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
342 struct vatpit *vatpit;
347 vatpit = vm_atpit(vm);
354 if (port == TIMER_MODE) {
356 VM_CTR0(vatpit->vm, "vatpit attempt to read mode");
361 error = vatpit_update_mode(vatpit, val);
362 VATPIT_UNLOCK(vatpit);
368 KASSERT(port >= TIMER_CNTR0 && port <= TIMER_CNTR2,
369 ("invalid port 0x%x", port));
370 c = &vatpit->channel[port - TIMER_CNTR0];
373 if (in && c->slatched) {
375 * Return the status byte if latched
382 * The spec says that once the output latch is completely
383 * read it should revert to "following" the counter. Use
384 * the free running counter for this case (i.e. Linux
385 * TSC calibration). Assuming the access mode is 16-bit,
386 * toggle the MSB/LSB bit on each read.
388 if (c->olbyte == 0) {
391 tmp = pit_update_counter(vatpit, c, false);
398 *eax = c->ol[--c->olbyte];
400 c->cr[c->crbyte++] = *eax;
401 if (c->crbyte == 2) {
402 c->status &= ~TIMER_STS_NULLCNT;
405 c->initial = c->cr[0] | (uint16_t)c->cr[1] << 8;
406 binuptime(&c->now_bt);
407 /* Start an interval timer for channel 0 */
408 if (port == TIMER_CNTR0) {
409 c->callout_bt = c->now_bt;
410 pit_timer_start_cntr0(vatpit);
416 VATPIT_UNLOCK(vatpit);
422 vatpit_nmisc_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
425 struct vatpit *vatpit;
427 vatpit = vm_atpit(vm);
431 if (vatpit_get_out(vatpit, 2))
436 VATPIT_UNLOCK(vatpit);
443 vatpit_init(struct vm *vm)
445 struct vatpit *vatpit;
446 struct vatpit_callout_arg *arg;
449 vatpit = malloc(sizeof(struct vatpit), M_VATPIT, M_WAITOK | M_ZERO);
452 mtx_init(&vatpit->mtx, "vatpit lock", NULL, MTX_SPIN);
454 FREQ2BT(PIT_8254_FREQ, &vatpit->freq_bt);
456 for (i = 0; i < 3; i++) {
457 callout_init(&vatpit->channel[i].callout, 1);
458 arg = &vatpit->channel[i].callout_arg;
459 arg->vatpit = vatpit;
460 arg->channel_num = i;
467 vatpit_cleanup(struct vatpit *vatpit)
471 for (i = 0; i < 3; i++)
472 callout_drain(&vatpit->channel[i].callout);
474 free(vatpit, M_VATPIT);
477 #ifdef BHYVE_SNAPSHOT
479 vatpit_snapshot(struct vatpit *vatpit, struct vm_snapshot_meta *meta)
483 struct channel *channel;
485 SNAPSHOT_VAR_OR_LEAVE(vatpit->freq_bt.sec, meta, ret, done);
486 SNAPSHOT_VAR_OR_LEAVE(vatpit->freq_bt.frac, meta, ret, done);
488 /* properly restore timers; they will NOT work currently */
489 printf("%s: snapshot restore does not reset timers!\r\n", __func__);
491 for (i = 0; i < nitems(vatpit->channel); i++) {
492 channel = &vatpit->channel[i];
494 SNAPSHOT_VAR_OR_LEAVE(channel->mode, meta, ret, done);
495 SNAPSHOT_VAR_OR_LEAVE(channel->initial, meta, ret, done);
496 SNAPSHOT_VAR_OR_LEAVE(channel->now_bt.sec, meta, ret, done);
497 SNAPSHOT_VAR_OR_LEAVE(channel->now_bt.frac, meta, ret, done);
498 SNAPSHOT_BUF_OR_LEAVE(channel->cr, sizeof(channel->cr),
500 SNAPSHOT_BUF_OR_LEAVE(channel->ol, sizeof(channel->ol),
502 SNAPSHOT_VAR_OR_LEAVE(channel->slatched, meta, ret, done);
503 SNAPSHOT_VAR_OR_LEAVE(channel->status, meta, ret, done);
504 SNAPSHOT_VAR_OR_LEAVE(channel->crbyte, meta, ret, done);
505 SNAPSHOT_VAR_OR_LEAVE(channel->frbyte, meta, ret, done);
506 SNAPSHOT_VAR_OR_LEAVE(channel->callout_bt.sec, meta, ret, done);
507 SNAPSHOT_VAR_OR_LEAVE(channel->callout_bt.frac, meta, ret,