xref: /freebsd/sys/amd64/vmm/io/vatpit.c (revision 3ccb02334bf5aee49a66dcff4b9229220bd0184b)
1e883c9bbSTycho Nightingale /*-
2e883c9bbSTycho Nightingale  * Copyright (c) 2014 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
3e883c9bbSTycho Nightingale  * Copyright (c) 2011 NetApp, Inc.
4e883c9bbSTycho Nightingale  * All rights reserved.
587c39157SJohn Baldwin  * Copyright (c) 2018 Joyent, Inc.
6e883c9bbSTycho Nightingale  *
7e883c9bbSTycho Nightingale  * Redistribution and use in source and binary forms, with or without
8e883c9bbSTycho Nightingale  * modification, are permitted provided that the following conditions
9e883c9bbSTycho Nightingale  * are met:
10e883c9bbSTycho Nightingale  * 1. Redistributions of source code must retain the above copyright
11e883c9bbSTycho Nightingale  *    notice, this list of conditions and the following disclaimer.
12e883c9bbSTycho Nightingale  * 2. Redistributions in binary form must reproduce the above copyright
13e883c9bbSTycho Nightingale  *    notice, this list of conditions and the following disclaimer in the
14e883c9bbSTycho Nightingale  *    documentation and/or other materials provided with the distribution.
15e883c9bbSTycho Nightingale  *
16e883c9bbSTycho Nightingale  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17e883c9bbSTycho Nightingale  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18e883c9bbSTycho Nightingale  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19e883c9bbSTycho Nightingale  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20e883c9bbSTycho Nightingale  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21e883c9bbSTycho Nightingale  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22e883c9bbSTycho Nightingale  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23e883c9bbSTycho Nightingale  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24e883c9bbSTycho Nightingale  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25e883c9bbSTycho Nightingale  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26e883c9bbSTycho Nightingale  * SUCH DAMAGE.
27e883c9bbSTycho Nightingale  */
28e883c9bbSTycho Nightingale 
29e883c9bbSTycho Nightingale #include <sys/cdefs.h>
30483d953aSJohn Baldwin #include "opt_bhyve_snapshot.h"
31483d953aSJohn Baldwin 
32e883c9bbSTycho Nightingale #include <sys/param.h>
33e883c9bbSTycho Nightingale #include <sys/types.h>
34e883c9bbSTycho Nightingale #include <sys/queue.h>
35e883c9bbSTycho Nightingale #include <sys/kernel.h>
36e883c9bbSTycho Nightingale #include <sys/lock.h>
37e883c9bbSTycho Nightingale #include <sys/malloc.h>
38e883c9bbSTycho Nightingale #include <sys/mutex.h>
39e883c9bbSTycho Nightingale #include <sys/systm.h>
40e883c9bbSTycho Nightingale 
41e883c9bbSTycho Nightingale #include <machine/vmm.h>
42483d953aSJohn Baldwin #include <machine/vmm_snapshot.h>
43e883c9bbSTycho Nightingale 
44*3ccb0233SMark Johnston #include <dev/vmm/vmm_ktr.h>
45*3ccb0233SMark Johnston 
46e883c9bbSTycho Nightingale #include "vatpic.h"
47e883c9bbSTycho Nightingale #include "vioapic.h"
48e883c9bbSTycho Nightingale #include "vatpit.h"
49e883c9bbSTycho Nightingale 
50e883c9bbSTycho Nightingale static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)");
51e883c9bbSTycho Nightingale 
52e883c9bbSTycho Nightingale #define	VATPIT_LOCK(vatpit)		mtx_lock_spin(&((vatpit)->mtx))
53e883c9bbSTycho Nightingale #define	VATPIT_UNLOCK(vatpit)		mtx_unlock_spin(&((vatpit)->mtx))
54e883c9bbSTycho Nightingale #define	VATPIT_LOCKED(vatpit)		mtx_owned(&((vatpit)->mtx))
55e883c9bbSTycho Nightingale 
56e883c9bbSTycho Nightingale #define	TIMER_SEL_MASK		0xc0
57e883c9bbSTycho Nightingale #define	TIMER_RW_MASK		0x30
58e883c9bbSTycho Nightingale #define	TIMER_MODE_MASK		0x0f
59e883c9bbSTycho Nightingale #define	TIMER_SEL_READBACK	0xc0
60e883c9bbSTycho Nightingale 
61c46ff7faSTycho Nightingale #define	TIMER_STS_OUT		0x80
62c46ff7faSTycho Nightingale #define	TIMER_STS_NULLCNT	0x40
63c46ff7faSTycho Nightingale 
64c46ff7faSTycho Nightingale #define	TIMER_RB_LCTR		0x20
65c46ff7faSTycho Nightingale #define	TIMER_RB_LSTATUS	0x10
66c46ff7faSTycho Nightingale #define	TIMER_RB_CTR_2		0x08
67c46ff7faSTycho Nightingale #define	TIMER_RB_CTR_1		0x04
68c46ff7faSTycho Nightingale #define	TIMER_RB_CTR_0		0x02
69c46ff7faSTycho Nightingale 
7079d6ca33STycho Nightingale #define	TMR2_OUT_STS		0x20
7179d6ca33STycho Nightingale 
72e883c9bbSTycho Nightingale #define	PIT_8254_FREQ		1193182
73e883c9bbSTycho Nightingale #define	TIMER_DIV(freq, hz)	(((freq) + (hz) / 2) / (hz))
74e883c9bbSTycho Nightingale 
75e883c9bbSTycho Nightingale struct vatpit_callout_arg {
76e883c9bbSTycho Nightingale 	struct vatpit	*vatpit;
77e883c9bbSTycho Nightingale 	int		channel_num;
78e883c9bbSTycho Nightingale };
79e883c9bbSTycho Nightingale 
80e883c9bbSTycho Nightingale struct channel {
81e883c9bbSTycho Nightingale 	int		mode;
82e883c9bbSTycho Nightingale 	uint16_t	initial;	/* initial counter value */
8387c39157SJohn Baldwin 	struct bintime	now_bt;		/* uptime when counter was loaded */
84e883c9bbSTycho Nightingale 	uint8_t		cr[2];
85e883c9bbSTycho Nightingale 	uint8_t		ol[2];
86c46ff7faSTycho Nightingale 	bool		slatched;	/* status latched */
87c46ff7faSTycho Nightingale 	uint8_t		status;
88e883c9bbSTycho Nightingale 	int		crbyte;
89e883c9bbSTycho Nightingale 	int		olbyte;
90e883c9bbSTycho Nightingale 	int		frbyte;
91e883c9bbSTycho Nightingale 	struct callout	callout;
9287c39157SJohn Baldwin 	struct bintime	callout_bt;	/* target time */
93e883c9bbSTycho Nightingale 	struct vatpit_callout_arg callout_arg;
94e883c9bbSTycho Nightingale };
95e883c9bbSTycho Nightingale 
96e883c9bbSTycho Nightingale struct vatpit {
97e883c9bbSTycho Nightingale 	struct vm	*vm;
98e883c9bbSTycho Nightingale 	struct mtx	mtx;
99e883c9bbSTycho Nightingale 
10087c39157SJohn Baldwin 	struct bintime	freq_bt;
101e883c9bbSTycho Nightingale 
102e883c9bbSTycho Nightingale 	struct channel	channel[3];
103e883c9bbSTycho Nightingale };
104e883c9bbSTycho Nightingale 
105e883c9bbSTycho Nightingale static void pit_timer_start_cntr0(struct vatpit *vatpit);
106e883c9bbSTycho Nightingale 
10787c39157SJohn Baldwin static uint64_t
vatpit_delta_ticks(struct vatpit * vatpit,struct channel * c)10887c39157SJohn Baldwin vatpit_delta_ticks(struct vatpit *vatpit, struct channel *c)
10987c39157SJohn Baldwin {
11087c39157SJohn Baldwin 	struct bintime delta;
11187c39157SJohn Baldwin 	uint64_t result;
11287c39157SJohn Baldwin 
11387c39157SJohn Baldwin 	binuptime(&delta);
11487c39157SJohn Baldwin 	bintime_sub(&delta, &c->now_bt);
11587c39157SJohn Baldwin 
11687c39157SJohn Baldwin 	result = delta.sec * PIT_8254_FREQ;
11787c39157SJohn Baldwin 	result += delta.frac / vatpit->freq_bt.frac;
11887c39157SJohn Baldwin 
11987c39157SJohn Baldwin 	return (result);
12087c39157SJohn Baldwin }
12187c39157SJohn Baldwin 
12279d6ca33STycho Nightingale static int
vatpit_get_out(struct vatpit * vatpit,int channel)12379d6ca33STycho Nightingale vatpit_get_out(struct vatpit *vatpit, int channel)
12479d6ca33STycho Nightingale {
12579d6ca33STycho Nightingale 	struct channel *c;
12687c39157SJohn Baldwin 	uint64_t delta_ticks;
12779d6ca33STycho Nightingale 	int out;
12879d6ca33STycho Nightingale 
12979d6ca33STycho Nightingale 	c = &vatpit->channel[channel];
13079d6ca33STycho Nightingale 
13179d6ca33STycho Nightingale 	switch (c->mode) {
13279d6ca33STycho Nightingale 	case TIMER_INTTC:
13387c39157SJohn Baldwin 		delta_ticks = vatpit_delta_ticks(vatpit, c);
13487c39157SJohn Baldwin 		out = (delta_ticks >= c->initial);
13579d6ca33STycho Nightingale 		break;
13679d6ca33STycho Nightingale 	default:
13779d6ca33STycho Nightingale 		out = 0;
13879d6ca33STycho Nightingale 		break;
13979d6ca33STycho Nightingale 	}
14079d6ca33STycho Nightingale 
14179d6ca33STycho Nightingale 	return (out);
14279d6ca33STycho Nightingale }
14379d6ca33STycho Nightingale 
144e883c9bbSTycho Nightingale static void
vatpit_callout_handler(void * a)145e883c9bbSTycho Nightingale vatpit_callout_handler(void *a)
146e883c9bbSTycho Nightingale {
147e883c9bbSTycho Nightingale 	struct vatpit_callout_arg *arg = a;
148e883c9bbSTycho Nightingale 	struct vatpit *vatpit;
149e883c9bbSTycho Nightingale 	struct callout *callout;
150e883c9bbSTycho Nightingale 	struct channel *c;
151e883c9bbSTycho Nightingale 
152e883c9bbSTycho Nightingale 	vatpit = arg->vatpit;
153e883c9bbSTycho Nightingale 	c = &vatpit->channel[arg->channel_num];
154e883c9bbSTycho Nightingale 	callout = &c->callout;
155e883c9bbSTycho Nightingale 
15679d6ca33STycho Nightingale 	VM_CTR1(vatpit->vm, "atpit t%d fired", arg->channel_num);
157e883c9bbSTycho Nightingale 
158e883c9bbSTycho Nightingale 	VATPIT_LOCK(vatpit);
159e883c9bbSTycho Nightingale 
160e883c9bbSTycho Nightingale 	if (callout_pending(callout))		/* callout was reset */
161e883c9bbSTycho Nightingale 		goto done;
162e883c9bbSTycho Nightingale 
163e883c9bbSTycho Nightingale 	if (!callout_active(callout))		/* callout was stopped */
164e883c9bbSTycho Nightingale 		goto done;
165e883c9bbSTycho Nightingale 
166e883c9bbSTycho Nightingale 	callout_deactivate(callout);
167e883c9bbSTycho Nightingale 
168e883c9bbSTycho Nightingale 	if (c->mode == TIMER_RATEGEN) {
169e883c9bbSTycho Nightingale 		pit_timer_start_cntr0(vatpit);
170e883c9bbSTycho Nightingale 	}
171e883c9bbSTycho Nightingale 
172e883c9bbSTycho Nightingale 	vatpic_pulse_irq(vatpit->vm, 0);
173e883c9bbSTycho Nightingale 	vioapic_pulse_irq(vatpit->vm, 2);
174e883c9bbSTycho Nightingale 
175e883c9bbSTycho Nightingale done:
176e883c9bbSTycho Nightingale 	VATPIT_UNLOCK(vatpit);
177e883c9bbSTycho Nightingale 	return;
178e883c9bbSTycho Nightingale }
179e883c9bbSTycho Nightingale 
180e883c9bbSTycho Nightingale static void
pit_timer_start_cntr0(struct vatpit * vatpit)181e883c9bbSTycho Nightingale pit_timer_start_cntr0(struct vatpit *vatpit)
182e883c9bbSTycho Nightingale {
183e883c9bbSTycho Nightingale 	struct channel *c;
18487c39157SJohn Baldwin 	struct bintime now, delta;
18587c39157SJohn Baldwin 	sbintime_t precision;
186e883c9bbSTycho Nightingale 
187e883c9bbSTycho Nightingale 	c = &vatpit->channel[0];
188e883c9bbSTycho Nightingale 	if (c->initial != 0) {
18987c39157SJohn Baldwin 		delta.sec = 0;
19087c39157SJohn Baldwin 		delta.frac = vatpit->freq_bt.frac * c->initial;
19187c39157SJohn Baldwin 		bintime_add(&c->callout_bt, &delta);
19287c39157SJohn Baldwin 		precision = bttosbt(delta) >> tc_precexp;
193e883c9bbSTycho Nightingale 
19479d6ca33STycho Nightingale 		/*
19587c39157SJohn Baldwin 		 * Reset 'callout_bt' if the time that the callout
19679d6ca33STycho Nightingale 		 * was supposed to fire is more than 'c->initial'
19779d6ca33STycho Nightingale 		 * ticks in the past.
19879d6ca33STycho Nightingale 		 */
19987c39157SJohn Baldwin 		binuptime(&now);
20087c39157SJohn Baldwin 		if (bintime_cmp(&c->callout_bt, &now, <)) {
20187c39157SJohn Baldwin 			c->callout_bt = now;
20287c39157SJohn Baldwin 			bintime_add(&c->callout_bt, &delta);
20387c39157SJohn Baldwin 		}
20479d6ca33STycho Nightingale 
20587c39157SJohn Baldwin 		callout_reset_sbt(&c->callout, bttosbt(c->callout_bt),
206e883c9bbSTycho Nightingale 		    precision, vatpit_callout_handler, &c->callout_arg,
207e883c9bbSTycho Nightingale 		    C_ABSOLUTE);
208e883c9bbSTycho Nightingale 	}
209e883c9bbSTycho Nightingale }
210e883c9bbSTycho Nightingale 
211e883c9bbSTycho Nightingale static uint16_t
pit_update_counter(struct vatpit * vatpit,struct channel * c,bool latch)212e883c9bbSTycho Nightingale pit_update_counter(struct vatpit *vatpit, struct channel *c, bool latch)
213e883c9bbSTycho Nightingale {
214e883c9bbSTycho Nightingale 	uint16_t lval;
21587c39157SJohn Baldwin 	uint64_t delta_ticks;
216e883c9bbSTycho Nightingale 
217e883c9bbSTycho Nightingale 	/* cannot latch a new value until the old one has been consumed */
218e883c9bbSTycho Nightingale 	if (latch && c->olbyte != 0)
219e883c9bbSTycho Nightingale 		return (0);
220e883c9bbSTycho Nightingale 
221e883c9bbSTycho Nightingale 	if (c->initial == 0) {
222e883c9bbSTycho Nightingale 		/*
223e883c9bbSTycho Nightingale 		 * This is possibly an o/s bug - reading the value of
224e883c9bbSTycho Nightingale 		 * the timer without having set up the initial value.
225e883c9bbSTycho Nightingale 		 *
226e883c9bbSTycho Nightingale 		 * The original user-space version of this code set
227e883c9bbSTycho Nightingale 		 * the timer to 100hz in this condition; do the same
228e883c9bbSTycho Nightingale 		 * here.
229e883c9bbSTycho Nightingale 		 */
230e883c9bbSTycho Nightingale 		c->initial = TIMER_DIV(PIT_8254_FREQ, 100);
23187c39157SJohn Baldwin 		binuptime(&c->now_bt);
232c46ff7faSTycho Nightingale 		c->status &= ~TIMER_STS_NULLCNT;
233e883c9bbSTycho Nightingale 	}
234e883c9bbSTycho Nightingale 
23587c39157SJohn Baldwin 	delta_ticks = vatpit_delta_ticks(vatpit, c);
236e883c9bbSTycho Nightingale 	lval = c->initial - delta_ticks % c->initial;
237e883c9bbSTycho Nightingale 
238e883c9bbSTycho Nightingale 	if (latch) {
239e883c9bbSTycho Nightingale 		c->olbyte = 2;
240e883c9bbSTycho Nightingale 		c->ol[1] = lval;		/* LSB */
241e883c9bbSTycho Nightingale 		c->ol[0] = lval >> 8;		/* MSB */
242e883c9bbSTycho Nightingale 	}
243e883c9bbSTycho Nightingale 
244e883c9bbSTycho Nightingale 	return (lval);
245e883c9bbSTycho Nightingale }
246e883c9bbSTycho Nightingale 
247e883c9bbSTycho Nightingale static int
pit_readback1(struct vatpit * vatpit,int channel,uint8_t cmd)248c46ff7faSTycho Nightingale pit_readback1(struct vatpit *vatpit, int channel, uint8_t cmd)
249c46ff7faSTycho Nightingale {
250c46ff7faSTycho Nightingale 	struct channel *c;
251c46ff7faSTycho Nightingale 
252c46ff7faSTycho Nightingale 	c = &vatpit->channel[channel];
253c46ff7faSTycho Nightingale 
254c46ff7faSTycho Nightingale 	/*
255c46ff7faSTycho Nightingale 	 * Latch the count/status of the timer if not already latched.
256c46ff7faSTycho Nightingale 	 * N.B. that the count/status latch-select bits are active-low.
257c46ff7faSTycho Nightingale 	 */
258c46ff7faSTycho Nightingale 	if (!(cmd & TIMER_RB_LCTR) && !c->olbyte) {
259c46ff7faSTycho Nightingale 		(void) pit_update_counter(vatpit, c, true);
260c46ff7faSTycho Nightingale 	}
261c46ff7faSTycho Nightingale 
262c46ff7faSTycho Nightingale 	if (!(cmd & TIMER_RB_LSTATUS) && !c->slatched) {
263c46ff7faSTycho Nightingale 		c->slatched = true;
264c46ff7faSTycho Nightingale 		/*
265c46ff7faSTycho Nightingale 		 * For mode 0, see if the elapsed time is greater
266c46ff7faSTycho Nightingale 		 * than the initial value - this results in the
267c46ff7faSTycho Nightingale 		 * output pin being set to 1 in the status byte.
268c46ff7faSTycho Nightingale 		 */
269c46ff7faSTycho Nightingale 		if (c->mode == TIMER_INTTC && vatpit_get_out(vatpit, channel))
270c46ff7faSTycho Nightingale 			c->status |= TIMER_STS_OUT;
271c46ff7faSTycho Nightingale 		else
272c46ff7faSTycho Nightingale 			c->status &= ~TIMER_STS_OUT;
273c46ff7faSTycho Nightingale 	}
274c46ff7faSTycho Nightingale 
275c46ff7faSTycho Nightingale 	return (0);
276c46ff7faSTycho Nightingale }
277c46ff7faSTycho Nightingale 
278c46ff7faSTycho Nightingale static int
pit_readback(struct vatpit * vatpit,uint8_t cmd)279c46ff7faSTycho Nightingale pit_readback(struct vatpit *vatpit, uint8_t cmd)
280c46ff7faSTycho Nightingale {
281c46ff7faSTycho Nightingale 	int error;
282c46ff7faSTycho Nightingale 
283c46ff7faSTycho Nightingale 	/*
284c46ff7faSTycho Nightingale 	 * The readback command can apply to all timers.
285c46ff7faSTycho Nightingale 	 */
286c46ff7faSTycho Nightingale 	error = 0;
287c46ff7faSTycho Nightingale 	if (cmd & TIMER_RB_CTR_0)
288c46ff7faSTycho Nightingale 		error = pit_readback1(vatpit, 0, cmd);
289c46ff7faSTycho Nightingale 	if (!error && cmd & TIMER_RB_CTR_1)
290c46ff7faSTycho Nightingale 		error = pit_readback1(vatpit, 1, cmd);
291c46ff7faSTycho Nightingale 	if (!error && cmd & TIMER_RB_CTR_2)
292c46ff7faSTycho Nightingale 		error = pit_readback1(vatpit, 2, cmd);
293c46ff7faSTycho Nightingale 
294c46ff7faSTycho Nightingale 	return (error);
295c46ff7faSTycho Nightingale }
296c46ff7faSTycho Nightingale 
297c46ff7faSTycho Nightingale static int
vatpit_update_mode(struct vatpit * vatpit,uint8_t val)298e883c9bbSTycho Nightingale vatpit_update_mode(struct vatpit *vatpit, uint8_t val)
299e883c9bbSTycho Nightingale {
300e883c9bbSTycho Nightingale 	struct channel *c;
301e883c9bbSTycho Nightingale 	int sel, rw, mode;
302e883c9bbSTycho Nightingale 
303e883c9bbSTycho Nightingale 	sel = val & TIMER_SEL_MASK;
304e883c9bbSTycho Nightingale 	rw = val & TIMER_RW_MASK;
305e883c9bbSTycho Nightingale 	mode = val & TIMER_MODE_MASK;
306e883c9bbSTycho Nightingale 
307e883c9bbSTycho Nightingale 	if (sel == TIMER_SEL_READBACK)
308c46ff7faSTycho Nightingale 		return (pit_readback(vatpit, val));
309e883c9bbSTycho Nightingale 
310e883c9bbSTycho Nightingale 	if (rw != TIMER_LATCH && rw != TIMER_16BIT)
311e883c9bbSTycho Nightingale 		return (-1);
312e883c9bbSTycho Nightingale 
313e883c9bbSTycho Nightingale 	if (rw != TIMER_LATCH) {
314e883c9bbSTycho Nightingale 		/*
315e883c9bbSTycho Nightingale 		 * Counter mode is not affected when issuing a
316e883c9bbSTycho Nightingale 		 * latch command.
317e883c9bbSTycho Nightingale 		 */
318e883c9bbSTycho Nightingale 		if (mode != TIMER_INTTC &&
319e883c9bbSTycho Nightingale 		    mode != TIMER_RATEGEN &&
320e883c9bbSTycho Nightingale 		    mode != TIMER_SQWAVE &&
321e883c9bbSTycho Nightingale 		    mode != TIMER_SWSTROBE)
322e883c9bbSTycho Nightingale 			return (-1);
323e883c9bbSTycho Nightingale 	}
324e883c9bbSTycho Nightingale 
325e883c9bbSTycho Nightingale 	c = &vatpit->channel[sel >> 6];
326e883c9bbSTycho Nightingale 	if (rw == TIMER_LATCH)
327e883c9bbSTycho Nightingale 		pit_update_counter(vatpit, c, true);
328e883c9bbSTycho Nightingale 	else {
329e883c9bbSTycho Nightingale 		c->mode = mode;
330e883c9bbSTycho Nightingale 		c->olbyte = 0;	/* reset latch after reprogramming */
331c46ff7faSTycho Nightingale 		c->status |= TIMER_STS_NULLCNT;
332e883c9bbSTycho Nightingale 	}
333e883c9bbSTycho Nightingale 
334e883c9bbSTycho Nightingale 	return (0);
335e883c9bbSTycho Nightingale }
336e883c9bbSTycho Nightingale 
337e883c9bbSTycho Nightingale int
vatpit_handler(struct vm * vm,bool in,int port,int bytes,uint32_t * eax)3389388bc1eSJohn Baldwin vatpit_handler(struct vm *vm, bool in, int port, int bytes, uint32_t *eax)
339e883c9bbSTycho Nightingale {
340e883c9bbSTycho Nightingale 	struct vatpit *vatpit;
341e883c9bbSTycho Nightingale 	struct channel *c;
342e883c9bbSTycho Nightingale 	uint8_t val;
343e883c9bbSTycho Nightingale 	int error;
344e883c9bbSTycho Nightingale 
345e883c9bbSTycho Nightingale 	vatpit = vm_atpit(vm);
346e883c9bbSTycho Nightingale 
347d6aa08c3STycho Nightingale 	if (bytes != 1)
348e883c9bbSTycho Nightingale 		return (-1);
349e883c9bbSTycho Nightingale 
350d6aa08c3STycho Nightingale 	val = *eax;
351e883c9bbSTycho Nightingale 
352e883c9bbSTycho Nightingale 	if (port == TIMER_MODE) {
353d6aa08c3STycho Nightingale 		if (in) {
35479d6ca33STycho Nightingale 			VM_CTR0(vatpit->vm, "vatpit attempt to read mode");
355e883c9bbSTycho Nightingale 			return (-1);
356e883c9bbSTycho Nightingale 		}
357e883c9bbSTycho Nightingale 
358e883c9bbSTycho Nightingale 		VATPIT_LOCK(vatpit);
359e883c9bbSTycho Nightingale 		error = vatpit_update_mode(vatpit, val);
360e883c9bbSTycho Nightingale 		VATPIT_UNLOCK(vatpit);
361e883c9bbSTycho Nightingale 
362e883c9bbSTycho Nightingale 		return (error);
363e883c9bbSTycho Nightingale 	}
364e883c9bbSTycho Nightingale 
365e883c9bbSTycho Nightingale 	/* counter ports */
366d6aa08c3STycho Nightingale 	KASSERT(port >= TIMER_CNTR0 && port <= TIMER_CNTR2,
367e883c9bbSTycho Nightingale 	    ("invalid port 0x%x", port));
368e883c9bbSTycho Nightingale 	c = &vatpit->channel[port - TIMER_CNTR0];
369e883c9bbSTycho Nightingale 
370e883c9bbSTycho Nightingale 	VATPIT_LOCK(vatpit);
371c46ff7faSTycho Nightingale 	if (in && c->slatched) {
372c46ff7faSTycho Nightingale 		/*
373c46ff7faSTycho Nightingale 		 * Return the status byte if latched
374c46ff7faSTycho Nightingale 		 */
375c46ff7faSTycho Nightingale 		*eax = c->status;
376c46ff7faSTycho Nightingale 		c->slatched = false;
377c46ff7faSTycho Nightingale 		c->status = 0;
378c46ff7faSTycho Nightingale 	} else if (in) {
379e883c9bbSTycho Nightingale 		/*
380e883c9bbSTycho Nightingale 		 * The spec says that once the output latch is completely
381e883c9bbSTycho Nightingale 		 * read it should revert to "following" the counter. Use
382e883c9bbSTycho Nightingale 		 * the free running counter for this case (i.e. Linux
383e883c9bbSTycho Nightingale 		 * TSC calibration). Assuming the access mode is 16-bit,
384e883c9bbSTycho Nightingale 		 * toggle the MSB/LSB bit on each read.
385e883c9bbSTycho Nightingale 		 */
386e883c9bbSTycho Nightingale 		if (c->olbyte == 0) {
387e883c9bbSTycho Nightingale 			uint16_t tmp;
388e883c9bbSTycho Nightingale 
389e883c9bbSTycho Nightingale 			tmp = pit_update_counter(vatpit, c, false);
390e883c9bbSTycho Nightingale 			if (c->frbyte)
391e883c9bbSTycho Nightingale 				tmp >>= 8;
392e883c9bbSTycho Nightingale 			tmp &= 0xff;
393d6aa08c3STycho Nightingale 			*eax = tmp;
394e883c9bbSTycho Nightingale 			c->frbyte ^= 1;
395e883c9bbSTycho Nightingale 		}  else
396d6aa08c3STycho Nightingale 			*eax = c->ol[--c->olbyte];
397e883c9bbSTycho Nightingale 	} else {
398d6aa08c3STycho Nightingale 		c->cr[c->crbyte++] = *eax;
399e883c9bbSTycho Nightingale 		if (c->crbyte == 2) {
400c46ff7faSTycho Nightingale 			c->status &= ~TIMER_STS_NULLCNT;
401e883c9bbSTycho Nightingale 			c->frbyte = 0;
402e883c9bbSTycho Nightingale 			c->crbyte = 0;
403e883c9bbSTycho Nightingale 			c->initial = c->cr[0] | (uint16_t)c->cr[1] << 8;
40487c39157SJohn Baldwin 			binuptime(&c->now_bt);
405e883c9bbSTycho Nightingale 			/* Start an interval timer for channel 0 */
406e883c9bbSTycho Nightingale 			if (port == TIMER_CNTR0) {
40787c39157SJohn Baldwin 				c->callout_bt = c->now_bt;
408e883c9bbSTycho Nightingale 				pit_timer_start_cntr0(vatpit);
409e883c9bbSTycho Nightingale 			}
410e883c9bbSTycho Nightingale 			if (c->initial == 0)
411e883c9bbSTycho Nightingale 				c->initial = 0xffff;
412e883c9bbSTycho Nightingale 		}
413e883c9bbSTycho Nightingale 	}
414e883c9bbSTycho Nightingale 	VATPIT_UNLOCK(vatpit);
415e883c9bbSTycho Nightingale 
416e883c9bbSTycho Nightingale 	return (0);
417e883c9bbSTycho Nightingale }
418e883c9bbSTycho Nightingale 
41979d6ca33STycho Nightingale int
vatpit_nmisc_handler(struct vm * vm,bool in,int port,int bytes,uint32_t * eax)4209388bc1eSJohn Baldwin vatpit_nmisc_handler(struct vm *vm, bool in, int port, int bytes,
421d6aa08c3STycho Nightingale     uint32_t *eax)
42279d6ca33STycho Nightingale {
42379d6ca33STycho Nightingale 	struct vatpit *vatpit;
42479d6ca33STycho Nightingale 
42579d6ca33STycho Nightingale 	vatpit = vm_atpit(vm);
42679d6ca33STycho Nightingale 
427d6aa08c3STycho Nightingale 	if (in) {
42879d6ca33STycho Nightingale 			VATPIT_LOCK(vatpit);
42979d6ca33STycho Nightingale 			if (vatpit_get_out(vatpit, 2))
430d6aa08c3STycho Nightingale 				*eax = TMR2_OUT_STS;
43179d6ca33STycho Nightingale 			else
432d6aa08c3STycho Nightingale 				*eax = 0;
43379d6ca33STycho Nightingale 
43479d6ca33STycho Nightingale 			VATPIT_UNLOCK(vatpit);
43579d6ca33STycho Nightingale 	}
43679d6ca33STycho Nightingale 
43779d6ca33STycho Nightingale 	return (0);
43879d6ca33STycho Nightingale }
43979d6ca33STycho Nightingale 
440e883c9bbSTycho Nightingale struct vatpit *
vatpit_init(struct vm * vm)441e883c9bbSTycho Nightingale vatpit_init(struct vm *vm)
442e883c9bbSTycho Nightingale {
443e883c9bbSTycho Nightingale 	struct vatpit *vatpit;
444e883c9bbSTycho Nightingale 	struct vatpit_callout_arg *arg;
445e883c9bbSTycho Nightingale 	int i;
446e883c9bbSTycho Nightingale 
447e883c9bbSTycho Nightingale 	vatpit = malloc(sizeof(struct vatpit), M_VATPIT, M_WAITOK | M_ZERO);
448e883c9bbSTycho Nightingale 	vatpit->vm = vm;
449e883c9bbSTycho Nightingale 
450e883c9bbSTycho Nightingale 	mtx_init(&vatpit->mtx, "vatpit lock", NULL, MTX_SPIN);
451e883c9bbSTycho Nightingale 
45287c39157SJohn Baldwin 	FREQ2BT(PIT_8254_FREQ, &vatpit->freq_bt);
453e883c9bbSTycho Nightingale 
454e883c9bbSTycho Nightingale 	for (i = 0; i < 3; i++) {
455fd90e2edSJung-uk Kim 		callout_init(&vatpit->channel[i].callout, 1);
456e883c9bbSTycho Nightingale 		arg = &vatpit->channel[i].callout_arg;
457e883c9bbSTycho Nightingale 		arg->vatpit = vatpit;
458e883c9bbSTycho Nightingale 		arg->channel_num = i;
459e883c9bbSTycho Nightingale 	}
460e883c9bbSTycho Nightingale 
461e883c9bbSTycho Nightingale 	return (vatpit);
462e883c9bbSTycho Nightingale }
463e883c9bbSTycho Nightingale 
464e883c9bbSTycho Nightingale void
vatpit_cleanup(struct vatpit * vatpit)465e883c9bbSTycho Nightingale vatpit_cleanup(struct vatpit *vatpit)
466e883c9bbSTycho Nightingale {
467e883c9bbSTycho Nightingale 	int i;
468e883c9bbSTycho Nightingale 
469e883c9bbSTycho Nightingale 	for (i = 0; i < 3; i++)
470e883c9bbSTycho Nightingale 		callout_drain(&vatpit->channel[i].callout);
471e883c9bbSTycho Nightingale 
47208ebb360SJohn Baldwin 	mtx_destroy(&vatpit->mtx);
473e883c9bbSTycho Nightingale 	free(vatpit, M_VATPIT);
474e883c9bbSTycho Nightingale }
475483d953aSJohn Baldwin 
476483d953aSJohn Baldwin #ifdef BHYVE_SNAPSHOT
477483d953aSJohn Baldwin int
vatpit_snapshot(struct vatpit * vatpit,struct vm_snapshot_meta * meta)478483d953aSJohn Baldwin vatpit_snapshot(struct vatpit *vatpit, struct vm_snapshot_meta *meta)
479483d953aSJohn Baldwin {
480483d953aSJohn Baldwin 	int ret;
481483d953aSJohn Baldwin 	int i;
482483d953aSJohn Baldwin 	struct channel *channel;
483483d953aSJohn Baldwin 
484483d953aSJohn Baldwin 	SNAPSHOT_VAR_OR_LEAVE(vatpit->freq_bt.sec, meta, ret, done);
485483d953aSJohn Baldwin 	SNAPSHOT_VAR_OR_LEAVE(vatpit->freq_bt.frac, meta, ret, done);
486483d953aSJohn Baldwin 
487483d953aSJohn Baldwin 	/* properly restore timers; they will NOT work currently */
488483d953aSJohn Baldwin 	printf("%s: snapshot restore does not reset timers!\r\n", __func__);
489483d953aSJohn Baldwin 
490483d953aSJohn Baldwin 	for (i = 0; i < nitems(vatpit->channel); i++) {
491483d953aSJohn Baldwin 		channel = &vatpit->channel[i];
492483d953aSJohn Baldwin 
493483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->mode, meta, ret, done);
494483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->initial, meta, ret, done);
495483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->now_bt.sec, meta, ret, done);
496483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->now_bt.frac, meta, ret, done);
497483d953aSJohn Baldwin 		SNAPSHOT_BUF_OR_LEAVE(channel->cr, sizeof(channel->cr),
498483d953aSJohn Baldwin 			meta, ret, done);
499483d953aSJohn Baldwin 		SNAPSHOT_BUF_OR_LEAVE(channel->ol, sizeof(channel->ol),
500483d953aSJohn Baldwin 			meta, ret, done);
501483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->slatched, meta, ret, done);
502483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->status, meta, ret, done);
503483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->crbyte, meta, ret, done);
504483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->frbyte, meta, ret, done);
505483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->callout_bt.sec, meta, ret, done);
506483d953aSJohn Baldwin 		SNAPSHOT_VAR_OR_LEAVE(channel->callout_bt.frac, meta, ret,
507483d953aSJohn Baldwin 			done);
508483d953aSJohn Baldwin 	}
509483d953aSJohn Baldwin 
510483d953aSJohn Baldwin done:
511483d953aSJohn Baldwin 	return (ret);
512483d953aSJohn Baldwin }
513483d953aSJohn Baldwin #endif
514