xref: /freebsd/sys/amd64/vmm/io/vatpit.c (revision e17f5b1d)
1 /*-
2  * Copyright (c) 2014 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
3  * Copyright (c) 2011 NetApp, Inc.
4  * All rights reserved.
5  * Copyright (c) 2018 Joyent, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_bhyve_snapshot.h"
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/queue.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/systm.h>
42 
43 #include <machine/vmm.h>
44 #include <machine/vmm_snapshot.h>
45 
46 #include "vmm_ktr.h"
47 #include "vatpic.h"
48 #include "vioapic.h"
49 #include "vatpit.h"
50 
51 static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)");
52 
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))
56 
57 #define	TIMER_SEL_MASK		0xc0
58 #define	TIMER_RW_MASK		0x30
59 #define	TIMER_MODE_MASK		0x0f
60 #define	TIMER_SEL_READBACK	0xc0
61 
62 #define	TIMER_STS_OUT		0x80
63 #define	TIMER_STS_NULLCNT	0x40
64 
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
70 
71 #define	TMR2_OUT_STS		0x20
72 
73 #define	PIT_8254_FREQ		1193182
74 #define	TIMER_DIV(freq, hz)	(((freq) + (hz) / 2) / (hz))
75 
76 struct vatpit_callout_arg {
77 	struct vatpit	*vatpit;
78 	int		channel_num;
79 };
80 
81 
82 struct channel {
83 	int		mode;
84 	uint16_t	initial;	/* initial counter value */
85 	struct bintime	now_bt;		/* uptime when counter was loaded */
86 	uint8_t		cr[2];
87 	uint8_t		ol[2];
88 	bool		slatched;	/* status latched */
89 	uint8_t		status;
90 	int		crbyte;
91 	int		olbyte;
92 	int		frbyte;
93 	struct callout	callout;
94 	struct bintime	callout_bt;	/* target time */
95 	struct vatpit_callout_arg callout_arg;
96 };
97 
98 struct vatpit {
99 	struct vm	*vm;
100 	struct mtx	mtx;
101 
102 	struct bintime	freq_bt;
103 
104 	struct channel	channel[3];
105 };
106 
107 static void pit_timer_start_cntr0(struct vatpit *vatpit);
108 
109 static uint64_t
110 vatpit_delta_ticks(struct vatpit *vatpit, struct channel *c)
111 {
112 	struct bintime delta;
113 	uint64_t result;
114 
115 	binuptime(&delta);
116 	bintime_sub(&delta, &c->now_bt);
117 
118 	result = delta.sec * PIT_8254_FREQ;
119 	result += delta.frac / vatpit->freq_bt.frac;
120 
121 	return (result);
122 }
123 
124 static int
125 vatpit_get_out(struct vatpit *vatpit, int channel)
126 {
127 	struct channel *c;
128 	uint64_t delta_ticks;
129 	int out;
130 
131 	c = &vatpit->channel[channel];
132 
133 	switch (c->mode) {
134 	case TIMER_INTTC:
135 		delta_ticks = vatpit_delta_ticks(vatpit, c);
136 		out = (delta_ticks >= c->initial);
137 		break;
138 	default:
139 		out = 0;
140 		break;
141 	}
142 
143 	return (out);
144 }
145 
146 static void
147 vatpit_callout_handler(void *a)
148 {
149 	struct vatpit_callout_arg *arg = a;
150 	struct vatpit *vatpit;
151 	struct callout *callout;
152 	struct channel *c;
153 
154 	vatpit = arg->vatpit;
155 	c = &vatpit->channel[arg->channel_num];
156 	callout = &c->callout;
157 
158 	VM_CTR1(vatpit->vm, "atpit t%d fired", arg->channel_num);
159 
160 	VATPIT_LOCK(vatpit);
161 
162 	if (callout_pending(callout))		/* callout was reset */
163 		goto done;
164 
165 	if (!callout_active(callout))		/* callout was stopped */
166 		goto done;
167 
168 	callout_deactivate(callout);
169 
170 	if (c->mode == TIMER_RATEGEN) {
171 		pit_timer_start_cntr0(vatpit);
172 	}
173 
174 	vatpic_pulse_irq(vatpit->vm, 0);
175 	vioapic_pulse_irq(vatpit->vm, 2);
176 
177 done:
178 	VATPIT_UNLOCK(vatpit);
179 	return;
180 }
181 
182 static void
183 pit_timer_start_cntr0(struct vatpit *vatpit)
184 {
185 	struct channel *c;
186 	struct bintime now, delta;
187 	sbintime_t precision;
188 
189 	c = &vatpit->channel[0];
190 	if (c->initial != 0) {
191 		delta.sec = 0;
192 		delta.frac = vatpit->freq_bt.frac * c->initial;
193 		bintime_add(&c->callout_bt, &delta);
194 		precision = bttosbt(delta) >> tc_precexp;
195 
196 		/*
197 		 * Reset 'callout_bt' if the time that the callout
198 		 * was supposed to fire is more than 'c->initial'
199 		 * ticks in the past.
200 		 */
201 		binuptime(&now);
202 		if (bintime_cmp(&c->callout_bt, &now, <)) {
203 			c->callout_bt = now;
204 			bintime_add(&c->callout_bt, &delta);
205 		}
206 
207 		callout_reset_sbt(&c->callout, bttosbt(c->callout_bt),
208 		    precision, vatpit_callout_handler, &c->callout_arg,
209 		    C_ABSOLUTE);
210 	}
211 }
212 
213 static uint16_t
214 pit_update_counter(struct vatpit *vatpit, struct channel *c, bool latch)
215 {
216 	uint16_t lval;
217 	uint64_t delta_ticks;
218 
219 	/* cannot latch a new value until the old one has been consumed */
220 	if (latch && c->olbyte != 0)
221 		return (0);
222 
223 	if (c->initial == 0) {
224 		/*
225 		 * This is possibly an o/s bug - reading the value of
226 		 * the timer without having set up the initial value.
227 		 *
228 		 * The original user-space version of this code set
229 		 * the timer to 100hz in this condition; do the same
230 		 * here.
231 		 */
232 		c->initial = TIMER_DIV(PIT_8254_FREQ, 100);
233 		binuptime(&c->now_bt);
234 		c->status &= ~TIMER_STS_NULLCNT;
235 	}
236 
237 	delta_ticks = vatpit_delta_ticks(vatpit, c);
238 	lval = c->initial - delta_ticks % c->initial;
239 
240 	if (latch) {
241 		c->olbyte = 2;
242 		c->ol[1] = lval;		/* LSB */
243 		c->ol[0] = lval >> 8;		/* MSB */
244 	}
245 
246 	return (lval);
247 }
248 
249 static int
250 pit_readback1(struct vatpit *vatpit, int channel, uint8_t cmd)
251 {
252 	struct channel *c;
253 
254 	c = &vatpit->channel[channel];
255 
256 	/*
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.
259 	 */
260 	if (!(cmd & TIMER_RB_LCTR) && !c->olbyte) {
261 		(void) pit_update_counter(vatpit, c, true);
262 	}
263 
264 	if (!(cmd & TIMER_RB_LSTATUS) && !c->slatched) {
265 		c->slatched = true;
266 		/*
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.
270 		 */
271 		if (c->mode == TIMER_INTTC && vatpit_get_out(vatpit, channel))
272 			c->status |= TIMER_STS_OUT;
273 		else
274 			c->status &= ~TIMER_STS_OUT;
275 	}
276 
277 	return (0);
278 }
279 
280 static int
281 pit_readback(struct vatpit *vatpit, uint8_t cmd)
282 {
283 	int error;
284 
285 	/*
286 	 * The readback command can apply to all timers.
287 	 */
288 	error = 0;
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);
295 
296 	return (error);
297 }
298 
299 
300 static int
301 vatpit_update_mode(struct vatpit *vatpit, uint8_t val)
302 {
303 	struct channel *c;
304 	int sel, rw, mode;
305 
306 	sel = val & TIMER_SEL_MASK;
307 	rw = val & TIMER_RW_MASK;
308 	mode = val & TIMER_MODE_MASK;
309 
310 	if (sel == TIMER_SEL_READBACK)
311 		return (pit_readback(vatpit, val));
312 
313 	if (rw != TIMER_LATCH && rw != TIMER_16BIT)
314 		return (-1);
315 
316 	if (rw != TIMER_LATCH) {
317 		/*
318 		 * Counter mode is not affected when issuing a
319 		 * latch command.
320 		 */
321 		if (mode != TIMER_INTTC &&
322 		    mode != TIMER_RATEGEN &&
323 		    mode != TIMER_SQWAVE &&
324 		    mode != TIMER_SWSTROBE)
325 			return (-1);
326 	}
327 
328 	c = &vatpit->channel[sel >> 6];
329 	if (rw == TIMER_LATCH)
330 		pit_update_counter(vatpit, c, true);
331 	else {
332 		c->mode = mode;
333 		c->olbyte = 0;	/* reset latch after reprogramming */
334 		c->status |= TIMER_STS_NULLCNT;
335 	}
336 
337 	return (0);
338 }
339 
340 int
341 vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
342     uint32_t *eax)
343 {
344 	struct vatpit *vatpit;
345 	struct channel *c;
346 	uint8_t val;
347 	int error;
348 
349 	vatpit = vm_atpit(vm);
350 
351 	if (bytes != 1)
352 		return (-1);
353 
354 	val = *eax;
355 
356 	if (port == TIMER_MODE) {
357 		if (in) {
358 			VM_CTR0(vatpit->vm, "vatpit attempt to read mode");
359 			return (-1);
360 		}
361 
362 		VATPIT_LOCK(vatpit);
363 		error = vatpit_update_mode(vatpit, val);
364 		VATPIT_UNLOCK(vatpit);
365 
366 		return (error);
367 	}
368 
369 	/* counter ports */
370 	KASSERT(port >= TIMER_CNTR0 && port <= TIMER_CNTR2,
371 	    ("invalid port 0x%x", port));
372 	c = &vatpit->channel[port - TIMER_CNTR0];
373 
374 	VATPIT_LOCK(vatpit);
375 	if (in && c->slatched) {
376 		/*
377 		 * Return the status byte if latched
378 		 */
379 		*eax = c->status;
380 		c->slatched = false;
381 		c->status = 0;
382 	} else if (in) {
383 		/*
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.
389 		 */
390 		if (c->olbyte == 0) {
391 			uint16_t tmp;
392 
393 			tmp = pit_update_counter(vatpit, c, false);
394 			if (c->frbyte)
395 				tmp >>= 8;
396 			tmp &= 0xff;
397 			*eax = tmp;
398 			c->frbyte ^= 1;
399 		}  else
400 			*eax = c->ol[--c->olbyte];
401 	} else {
402 		c->cr[c->crbyte++] = *eax;
403 		if (c->crbyte == 2) {
404 			c->status &= ~TIMER_STS_NULLCNT;
405 			c->frbyte = 0;
406 			c->crbyte = 0;
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);
413 			}
414 			if (c->initial == 0)
415 				c->initial = 0xffff;
416 		}
417 	}
418 	VATPIT_UNLOCK(vatpit);
419 
420 	return (0);
421 }
422 
423 int
424 vatpit_nmisc_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
425     uint32_t *eax)
426 {
427 	struct vatpit *vatpit;
428 
429 	vatpit = vm_atpit(vm);
430 
431 	if (in) {
432 			VATPIT_LOCK(vatpit);
433 			if (vatpit_get_out(vatpit, 2))
434 				*eax = TMR2_OUT_STS;
435 			else
436 				*eax = 0;
437 
438 			VATPIT_UNLOCK(vatpit);
439 	}
440 
441 	return (0);
442 }
443 
444 struct vatpit *
445 vatpit_init(struct vm *vm)
446 {
447 	struct vatpit *vatpit;
448 	struct vatpit_callout_arg *arg;
449 	int i;
450 
451 	vatpit = malloc(sizeof(struct vatpit), M_VATPIT, M_WAITOK | M_ZERO);
452 	vatpit->vm = vm;
453 
454 	mtx_init(&vatpit->mtx, "vatpit lock", NULL, MTX_SPIN);
455 
456 	FREQ2BT(PIT_8254_FREQ, &vatpit->freq_bt);
457 
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;
463 	}
464 
465 	return (vatpit);
466 }
467 
468 void
469 vatpit_cleanup(struct vatpit *vatpit)
470 {
471 	int i;
472 
473 	for (i = 0; i < 3; i++)
474 		callout_drain(&vatpit->channel[i].callout);
475 
476 	free(vatpit, M_VATPIT);
477 }
478 
479 #ifdef BHYVE_SNAPSHOT
480 int
481 vatpit_snapshot(struct vatpit *vatpit, struct vm_snapshot_meta *meta)
482 {
483 	int ret;
484 	int i;
485 	struct channel *channel;
486 
487 	SNAPSHOT_VAR_OR_LEAVE(vatpit->freq_bt.sec, meta, ret, done);
488 	SNAPSHOT_VAR_OR_LEAVE(vatpit->freq_bt.frac, meta, ret, done);
489 
490 	/* properly restore timers; they will NOT work currently */
491 	printf("%s: snapshot restore does not reset timers!\r\n", __func__);
492 
493 	for (i = 0; i < nitems(vatpit->channel); i++) {
494 		channel = &vatpit->channel[i];
495 
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),
501 			meta, ret, done);
502 		SNAPSHOT_BUF_OR_LEAVE(channel->ol, sizeof(channel->ol),
503 			meta, ret, done);
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,
510 			done);
511 	}
512 
513 done:
514 	return (ret);
515 }
516 #endif
517