1 /*-
2 * Copyright (c) 2018 Joyent, Inc.
3 * Copyright (c) 2014 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
4 * Copyright (c) 2011 NetApp, Inc.
5 * All rights reserved.
6 * Copyright (c) 2018 Joyent, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/queue.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
41
42 #include <machine/vmm.h>
43
44 #include "vmm_ktr.h"
45 #include "vatpic.h"
46 #include "vioapic.h"
47 #include "vatpit.h"
48
49 static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)");
50
51 #define VATPIT_LOCK(vatpit) mtx_lock_spin(&((vatpit)->mtx))
52 #define VATPIT_UNLOCK(vatpit) mtx_unlock_spin(&((vatpit)->mtx))
53 #define VATPIT_LOCKED(vatpit) mtx_owned(&((vatpit)->mtx))
54
55 #define TIMER_SEL_MASK 0xc0
56 #define TIMER_RW_MASK 0x30
57 #define TIMER_MODE_MASK 0x0f
58 #define TIMER_SEL_READBACK 0xc0
59
60 #define TIMER_STS_OUT 0x80
61 #define TIMER_STS_NULLCNT 0x40
62
63 #define TIMER_RB_LCTR 0x20
64 #define TIMER_RB_LSTATUS 0x10
65 #define TIMER_RB_CTR_2 0x08
66 #define TIMER_RB_CTR_1 0x04
67 #define TIMER_RB_CTR_0 0x02
68
69 #define TMR2_OUT_STS 0x20
70
71 #define PIT_8254_FREQ 1193182
72 #define TIMER_DIV(freq, hz) (((freq) + (hz) / 2) / (hz))
73
74 struct vatpit_callout_arg {
75 struct vatpit *vatpit;
76 int channel_num;
77 };
78
79 struct channel {
80 int mode;
81 uint16_t initial; /* initial counter value */
82 struct bintime now_bt; /* uptime when counter was loaded */
83 uint8_t cr[2];
84 uint8_t ol[2];
85 bool slatched; /* status latched */
86 uint8_t status;
87 int crbyte;
88 int olbyte;
89 int frbyte;
90 struct callout callout;
91 struct bintime callout_bt; /* target time */
92 struct vatpit_callout_arg callout_arg;
93 };
94
95 struct vatpit {
96 struct vm *vm;
97 struct mtx mtx;
98
99 struct bintime freq_bt;
100
101 struct channel channel[3];
102 };
103
104 static void pit_timer_start_cntr0(struct vatpit *vatpit);
105
106 static uint64_t
vatpit_delta_ticks(struct vatpit * vatpit,struct channel * c)107 vatpit_delta_ticks(struct vatpit *vatpit, struct channel *c)
108 {
109 struct bintime delta;
110 uint64_t result;
111
112 binuptime(&delta);
113 bintime_sub(&delta, &c->now_bt);
114
115 result = delta.sec * PIT_8254_FREQ;
116 result += delta.frac / vatpit->freq_bt.frac;
117
118 return (result);
119 }
120
121 static int
vatpit_get_out(struct vatpit * vatpit,int channel)122 vatpit_get_out(struct vatpit *vatpit, int channel)
123 {
124 struct channel *c;
125 uint64_t delta_ticks;
126 int out;
127
128 c = &vatpit->channel[channel];
129
130 switch (c->mode) {
131 case TIMER_INTTC:
132 delta_ticks = vatpit_delta_ticks(vatpit, c);
133 out = (delta_ticks >= c->initial);
134 break;
135 default:
136 out = 0;
137 break;
138 }
139
140 return (out);
141 }
142
143 static void
vatpit_callout_handler(void * a)144 vatpit_callout_handler(void *a)
145 {
146 struct vatpit_callout_arg *arg = a;
147 struct vatpit *vatpit;
148 struct callout *callout;
149 struct channel *c;
150
151 vatpit = arg->vatpit;
152 c = &vatpit->channel[arg->channel_num];
153 callout = &c->callout;
154
155 VM_CTR1(vatpit->vm, "atpit t%d fired", arg->channel_num);
156
157 VATPIT_LOCK(vatpit);
158
159 if (callout_pending(callout)) /* callout was reset */
160 goto done;
161
162 if (!callout_active(callout)) /* callout was stopped */
163 goto done;
164
165 callout_deactivate(callout);
166
167 if (c->mode == TIMER_RATEGEN || c->mode == TIMER_SQWAVE) {
168 pit_timer_start_cntr0(vatpit);
169 }
170
171 vatpic_pulse_irq(vatpit->vm, 0);
172 vioapic_pulse_irq(vatpit->vm, 2);
173
174 done:
175 VATPIT_UNLOCK(vatpit);
176 }
177
178 static void
pit_timer_start_cntr0(struct vatpit * vatpit)179 pit_timer_start_cntr0(struct vatpit *vatpit)
180 {
181 struct channel *c;
182 struct bintime now, delta;
183 sbintime_t precision;
184
185 c = &vatpit->channel[0];
186 if (c->initial != 0) {
187 delta.sec = 0;
188 delta.frac = vatpit->freq_bt.frac * c->initial;
189 bintime_add(&c->callout_bt, &delta);
190 precision = bttosbt(delta) >> tc_precexp;
191
192 /*
193 * Reset 'callout_bt' if the time that the callout
194 * was supposed to fire is more than 'c->initial'
195 * ticks in the past.
196 */
197 binuptime(&now);
198 if (BINTIME_CMP(&c->callout_bt, <, &now)) {
199 c->callout_bt = now;
200 bintime_add(&c->callout_bt, &delta);
201 }
202
203 callout_reset_sbt(&c->callout, bttosbt(c->callout_bt),
204 precision, vatpit_callout_handler, &c->callout_arg,
205 C_ABSOLUTE);
206 }
207 }
208
209 static uint16_t
pit_update_counter(struct vatpit * vatpit,struct channel * c,bool latch)210 pit_update_counter(struct vatpit *vatpit, struct channel *c, bool latch)
211 {
212 uint16_t lval;
213 uint64_t delta_ticks;
214
215 /* cannot latch a new value until the old one has been consumed */
216 if (latch && c->olbyte != 0)
217 return (0);
218
219 if (c->initial == 0) {
220 /*
221 * This is possibly an o/s bug - reading the value of
222 * the timer without having set up the initial value.
223 *
224 * The original user-space version of this code set
225 * the timer to 100hz in this condition; do the same
226 * here.
227 */
228 c->initial = TIMER_DIV(PIT_8254_FREQ, 100);
229 binuptime(&c->now_bt);
230 c->status &= ~TIMER_STS_NULLCNT;
231 }
232
233 delta_ticks = vatpit_delta_ticks(vatpit, c);
234 lval = c->initial - delta_ticks % c->initial;
235
236 if (latch) {
237 c->olbyte = 2;
238 c->ol[1] = lval; /* LSB */
239 c->ol[0] = lval >> 8; /* MSB */
240 }
241
242 return (lval);
243 }
244
245 static int
pit_readback1(struct vatpit * vatpit,int channel,uint8_t cmd)246 pit_readback1(struct vatpit *vatpit, int channel, uint8_t cmd)
247 {
248 struct channel *c;
249
250 c = &vatpit->channel[channel];
251
252 /*
253 * Latch the count/status of the timer if not already latched.
254 * N.B. that the count/status latch-select bits are active-low.
255 */
256 if (!(cmd & TIMER_RB_LCTR) && !c->olbyte) {
257 (void) pit_update_counter(vatpit, c, true);
258 }
259
260 if (!(cmd & TIMER_RB_LSTATUS) && !c->slatched) {
261 c->slatched = true;
262 /*
263 * For mode 0, see if the elapsed time is greater
264 * than the initial value - this results in the
265 * output pin being set to 1 in the status byte.
266 */
267 if (c->mode == TIMER_INTTC && vatpit_get_out(vatpit, channel))
268 c->status |= TIMER_STS_OUT;
269 else
270 c->status &= ~TIMER_STS_OUT;
271 }
272
273 return (0);
274 }
275
276 static int
pit_readback(struct vatpit * vatpit,uint8_t cmd)277 pit_readback(struct vatpit *vatpit, uint8_t cmd)
278 {
279 int error;
280
281 /*
282 * The readback command can apply to all timers.
283 */
284 error = 0;
285 if (cmd & TIMER_RB_CTR_0)
286 error = pit_readback1(vatpit, 0, cmd);
287 if (!error && cmd & TIMER_RB_CTR_1)
288 error = pit_readback1(vatpit, 1, cmd);
289 if (!error && cmd & TIMER_RB_CTR_2)
290 error = pit_readback1(vatpit, 2, cmd);
291
292 return (error);
293 }
294
295 static int
vatpit_update_mode(struct vatpit * vatpit,uint8_t val)296 vatpit_update_mode(struct vatpit *vatpit, uint8_t val)
297 {
298 struct channel *c;
299 int sel, rw, mode;
300
301 sel = val & TIMER_SEL_MASK;
302 rw = val & TIMER_RW_MASK;
303 mode = val & TIMER_MODE_MASK;
304
305 if (sel == TIMER_SEL_READBACK)
306 return (pit_readback(vatpit, val));
307
308 if (rw != TIMER_LATCH && rw != TIMER_16BIT)
309 return (-1);
310
311 if (rw != TIMER_LATCH) {
312 /*
313 * Counter mode is not affected when issuing a
314 * latch command.
315 */
316 if (mode != TIMER_INTTC &&
317 mode != TIMER_RATEGEN &&
318 mode != TIMER_SQWAVE &&
319 mode != TIMER_SWSTROBE)
320 return (-1);
321 }
322
323 c = &vatpit->channel[sel >> 6];
324 if (rw == TIMER_LATCH)
325 pit_update_counter(vatpit, c, true);
326 else {
327 c->mode = mode;
328 c->olbyte = 0; /* reset latch after reprogramming */
329 c->status |= TIMER_STS_NULLCNT;
330 }
331
332 return (0);
333 }
334
335 int
vatpit_handler(void * arg,bool in,uint16_t port,uint8_t bytes,uint32_t * eax)336 vatpit_handler(void *arg, bool in, uint16_t port, uint8_t bytes, uint32_t *eax)
337 {
338 struct vatpit *vatpit = arg;
339 struct channel *c;
340 uint8_t val;
341 int error;
342
343 if (bytes != 1)
344 return (-1);
345
346 val = *eax;
347
348 if (port == TIMER_MODE) {
349 if (in) {
350 VM_CTR0(vatpit->vm, "vatpit attempt to read mode");
351 return (-1);
352 }
353
354 VATPIT_LOCK(vatpit);
355 error = vatpit_update_mode(vatpit, val);
356 VATPIT_UNLOCK(vatpit);
357
358 return (error);
359 }
360
361 /* counter ports */
362 KASSERT(port >= TIMER_CNTR0 && port <= TIMER_CNTR2,
363 ("invalid port 0x%x", port));
364 c = &vatpit->channel[port - TIMER_CNTR0];
365
366 VATPIT_LOCK(vatpit);
367 if (in && c->slatched) {
368 /*
369 * Return the status byte if latched
370 */
371 *eax = c->status;
372 c->slatched = false;
373 c->status = 0;
374 } else if (in) {
375 /*
376 * The spec says that once the output latch is completely
377 * read it should revert to "following" the counter. Use
378 * the free running counter for this case (i.e. Linux
379 * TSC calibration). Assuming the access mode is 16-bit,
380 * toggle the MSB/LSB bit on each read.
381 */
382 if (c->olbyte == 0) {
383 uint16_t tmp;
384
385 tmp = pit_update_counter(vatpit, c, false);
386 if (c->frbyte)
387 tmp >>= 8;
388 tmp &= 0xff;
389 *eax = tmp;
390 c->frbyte ^= 1;
391 } else {
392 *eax = c->ol[--c->olbyte];
393 }
394 } else {
395 c->cr[c->crbyte++] = *eax;
396 if (c->crbyte == 2) {
397 c->status &= ~TIMER_STS_NULLCNT;
398 c->frbyte = 0;
399 c->crbyte = 0;
400 c->initial = c->cr[0] | (uint16_t)c->cr[1] << 8;
401 binuptime(&c->now_bt);
402 /* Start an interval timer for channel 0 */
403 if (port == TIMER_CNTR0) {
404 c->callout_bt = c->now_bt;
405 pit_timer_start_cntr0(vatpit);
406 }
407 if (c->initial == 0)
408 c->initial = 0xffff;
409 }
410 }
411 VATPIT_UNLOCK(vatpit);
412
413 return (0);
414 }
415
416 int
vatpit_nmisc_handler(void * arg,bool in,uint16_t port,uint8_t bytes,uint32_t * eax)417 vatpit_nmisc_handler(void *arg, bool in, uint16_t port, uint8_t bytes,
418 uint32_t *eax)
419 {
420 struct vatpit *vatpit = arg;
421
422 if (in) {
423 VATPIT_LOCK(vatpit);
424 if (vatpit_get_out(vatpit, 2))
425 *eax = TMR2_OUT_STS;
426 else
427 *eax = 0;
428
429 VATPIT_UNLOCK(vatpit);
430 }
431
432 return (0);
433 }
434
435 struct vatpit *
vatpit_init(struct vm * vm)436 vatpit_init(struct vm *vm)
437 {
438 struct vatpit *vatpit;
439 struct vatpit_callout_arg *arg;
440 int i;
441
442 vatpit = malloc(sizeof (struct vatpit), M_VATPIT, M_WAITOK | M_ZERO);
443 vatpit->vm = vm;
444
445 mtx_init(&vatpit->mtx, "vatpit lock", NULL, MTX_SPIN);
446
447 FREQ2BT(PIT_8254_FREQ, &vatpit->freq_bt);
448
449 for (i = 0; i < 3; i++) {
450 callout_init(&vatpit->channel[i].callout, 1);
451 arg = &vatpit->channel[i].callout_arg;
452 arg->vatpit = vatpit;
453 arg->channel_num = i;
454 }
455
456 return (vatpit);
457 }
458
459 void
vatpit_cleanup(struct vatpit * vatpit)460 vatpit_cleanup(struct vatpit *vatpit)
461 {
462 int i;
463
464 for (i = 0; i < 3; i++)
465 callout_drain(&vatpit->channel[i].callout);
466
467 free(vatpit, M_VATPIT);
468 }
469
470 void
vatpit_localize_resources(struct vatpit * vatpit)471 vatpit_localize_resources(struct vatpit *vatpit)
472 {
473 for (uint_t i = 0; i < 3; i++) {
474 /* Only localize channels which might be running */
475 if (vatpit->channel[i].mode != 0) {
476 vmm_glue_callout_localize(&vatpit->channel[i].callout);
477 }
478 }
479 }
480