2 * Copyright (c) 2014 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
3 * Copyright (c) 2011 NetApp, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/queue.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
40 #include <machine/vmm.h>
47 static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)");
49 #define VATPIT_LOCK(vatpit) mtx_lock_spin(&((vatpit)->mtx))
50 #define VATPIT_UNLOCK(vatpit) mtx_unlock_spin(&((vatpit)->mtx))
51 #define VATPIT_LOCKED(vatpit) mtx_owned(&((vatpit)->mtx))
53 #define TIMER_SEL_MASK 0xc0
54 #define TIMER_RW_MASK 0x30
55 #define TIMER_MODE_MASK 0x0f
56 #define TIMER_SEL_READBACK 0xc0
58 #define TIMER_STS_OUT 0x80
59 #define TIMER_STS_NULLCNT 0x40
61 #define TIMER_RB_LCTR 0x20
62 #define TIMER_RB_LSTATUS 0x10
63 #define TIMER_RB_CTR_2 0x08
64 #define TIMER_RB_CTR_1 0x04
65 #define TIMER_RB_CTR_0 0x02
67 #define TMR2_OUT_STS 0x20
69 #define PIT_8254_FREQ 1193182
70 #define TIMER_DIV(freq, hz) (((freq) + (hz) / 2) / (hz))
72 struct vatpit_callout_arg {
73 struct vatpit *vatpit;
80 uint16_t initial; /* initial counter value */
81 sbintime_t now_sbt; /* uptime when counter was loaded */
84 bool slatched; /* status latched */
89 struct callout callout;
90 sbintime_t callout_sbt; /* target time */
91 struct vatpit_callout_arg callout_arg;
100 struct channel channel[3];
103 static void pit_timer_start_cntr0(struct vatpit *vatpit);
106 vatpit_get_out(struct vatpit *vatpit, int channel)
109 sbintime_t delta_ticks;
112 c = &vatpit->channel[channel];
116 delta_ticks = (sbinuptime() - c->now_sbt) / vatpit->freq_sbt;
117 out = ((c->initial - delta_ticks) <= 0);
128 vatpit_callout_handler(void *a)
130 struct vatpit_callout_arg *arg = a;
131 struct vatpit *vatpit;
132 struct callout *callout;
135 vatpit = arg->vatpit;
136 c = &vatpit->channel[arg->channel_num];
137 callout = &c->callout;
139 VM_CTR1(vatpit->vm, "atpit t%d fired", arg->channel_num);
143 if (callout_pending(callout)) /* callout was reset */
146 if (!callout_active(callout)) /* callout was stopped */
149 callout_deactivate(callout);
151 if (c->mode == TIMER_RATEGEN) {
152 pit_timer_start_cntr0(vatpit);
155 vatpic_pulse_irq(vatpit->vm, 0);
156 vioapic_pulse_irq(vatpit->vm, 2);
159 VATPIT_UNLOCK(vatpit);
164 pit_timer_start_cntr0(struct vatpit *vatpit)
167 sbintime_t now, delta, precision;
169 c = &vatpit->channel[0];
170 if (c->initial != 0) {
171 delta = c->initial * vatpit->freq_sbt;
172 precision = delta >> tc_precexp;
173 c->callout_sbt = c->callout_sbt + delta;
176 * Reset 'callout_sbt' if the time that the callout
177 * was supposed to fire is more than 'c->initial'
181 if (c->callout_sbt < now)
182 c->callout_sbt = now + delta;
184 callout_reset_sbt(&c->callout, c->callout_sbt,
185 precision, vatpit_callout_handler, &c->callout_arg,
191 pit_update_counter(struct vatpit *vatpit, struct channel *c, bool latch)
194 sbintime_t delta_ticks;
196 /* cannot latch a new value until the old one has been consumed */
197 if (latch && c->olbyte != 0)
200 if (c->initial == 0) {
202 * This is possibly an o/s bug - reading the value of
203 * the timer without having set up the initial value.
205 * The original user-space version of this code set
206 * the timer to 100hz in this condition; do the same
209 c->initial = TIMER_DIV(PIT_8254_FREQ, 100);
210 c->now_sbt = sbinuptime();
211 c->status &= ~TIMER_STS_NULLCNT;
214 delta_ticks = (sbinuptime() - c->now_sbt) / vatpit->freq_sbt;
216 lval = c->initial - delta_ticks % c->initial;
220 c->ol[1] = lval; /* LSB */
221 c->ol[0] = lval >> 8; /* MSB */
228 pit_readback1(struct vatpit *vatpit, int channel, uint8_t cmd)
232 c = &vatpit->channel[channel];
235 * Latch the count/status of the timer if not already latched.
236 * N.B. that the count/status latch-select bits are active-low.
238 if (!(cmd & TIMER_RB_LCTR) && !c->olbyte) {
239 (void) pit_update_counter(vatpit, c, true);
242 if (!(cmd & TIMER_RB_LSTATUS) && !c->slatched) {
245 * For mode 0, see if the elapsed time is greater
246 * than the initial value - this results in the
247 * output pin being set to 1 in the status byte.
249 if (c->mode == TIMER_INTTC && vatpit_get_out(vatpit, channel))
250 c->status |= TIMER_STS_OUT;
252 c->status &= ~TIMER_STS_OUT;
259 pit_readback(struct vatpit *vatpit, uint8_t cmd)
264 * The readback command can apply to all timers.
267 if (cmd & TIMER_RB_CTR_0)
268 error = pit_readback1(vatpit, 0, cmd);
269 if (!error && cmd & TIMER_RB_CTR_1)
270 error = pit_readback1(vatpit, 1, cmd);
271 if (!error && cmd & TIMER_RB_CTR_2)
272 error = pit_readback1(vatpit, 2, cmd);
279 vatpit_update_mode(struct vatpit *vatpit, uint8_t val)
284 sel = val & TIMER_SEL_MASK;
285 rw = val & TIMER_RW_MASK;
286 mode = val & TIMER_MODE_MASK;
288 if (sel == TIMER_SEL_READBACK)
289 return (pit_readback(vatpit, val));
291 if (rw != TIMER_LATCH && rw != TIMER_16BIT)
294 if (rw != TIMER_LATCH) {
296 * Counter mode is not affected when issuing a
299 if (mode != TIMER_INTTC &&
300 mode != TIMER_RATEGEN &&
301 mode != TIMER_SQWAVE &&
302 mode != TIMER_SWSTROBE)
306 c = &vatpit->channel[sel >> 6];
307 if (rw == TIMER_LATCH)
308 pit_update_counter(vatpit, c, true);
311 c->olbyte = 0; /* reset latch after reprogramming */
312 c->status |= TIMER_STS_NULLCNT;
319 vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
322 struct vatpit *vatpit;
327 vatpit = vm_atpit(vm);
334 if (port == TIMER_MODE) {
336 VM_CTR0(vatpit->vm, "vatpit attempt to read mode");
341 error = vatpit_update_mode(vatpit, val);
342 VATPIT_UNLOCK(vatpit);
348 KASSERT(port >= TIMER_CNTR0 && port <= TIMER_CNTR2,
349 ("invalid port 0x%x", port));
350 c = &vatpit->channel[port - TIMER_CNTR0];
353 if (in && c->slatched) {
355 * Return the status byte if latched
362 * The spec says that once the output latch is completely
363 * read it should revert to "following" the counter. Use
364 * the free running counter for this case (i.e. Linux
365 * TSC calibration). Assuming the access mode is 16-bit,
366 * toggle the MSB/LSB bit on each read.
368 if (c->olbyte == 0) {
371 tmp = pit_update_counter(vatpit, c, false);
378 *eax = c->ol[--c->olbyte];
380 c->cr[c->crbyte++] = *eax;
381 if (c->crbyte == 2) {
382 c->status &= ~TIMER_STS_NULLCNT;
385 c->initial = c->cr[0] | (uint16_t)c->cr[1] << 8;
386 c->now_sbt = sbinuptime();
387 /* Start an interval timer for channel 0 */
388 if (port == TIMER_CNTR0) {
389 c->callout_sbt = c->now_sbt;
390 pit_timer_start_cntr0(vatpit);
396 VATPIT_UNLOCK(vatpit);
402 vatpit_nmisc_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
405 struct vatpit *vatpit;
407 vatpit = vm_atpit(vm);
411 if (vatpit_get_out(vatpit, 2))
416 VATPIT_UNLOCK(vatpit);
423 vatpit_init(struct vm *vm)
425 struct vatpit *vatpit;
427 struct vatpit_callout_arg *arg;
430 vatpit = malloc(sizeof(struct vatpit), M_VATPIT, M_WAITOK | M_ZERO);
433 mtx_init(&vatpit->mtx, "vatpit lock", NULL, MTX_SPIN);
435 FREQ2BT(PIT_8254_FREQ, &bt);
436 vatpit->freq_sbt = bttosbt(bt);
438 for (i = 0; i < 3; i++) {
439 callout_init(&vatpit->channel[i].callout, 1);
440 arg = &vatpit->channel[i].callout_arg;
441 arg->vatpit = vatpit;
442 arg->channel_num = i;
449 vatpit_cleanup(struct vatpit *vatpit)
453 for (i = 0; i < 3; i++)
454 callout_drain(&vatpit->channel[i].callout);
456 free(vatpit, M_VATPIT);