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 <sys/param.h> 33 #include <sys/types.h> 34 #include <sys/queue.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/mutex.h> 39 #include <sys/systm.h> 40 41 #include <machine/vmm.h> 42 43 #include "vmm_ktr.h" 44 #include "vatpic.h" 45 #include "vioapic.h" 46 #include "vatpit.h" 47 48 static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)"); 49 50 #define VATPIT_LOCK(vatpit) mtx_lock_spin(&((vatpit)->mtx)) 51 #define VATPIT_UNLOCK(vatpit) mtx_unlock_spin(&((vatpit)->mtx)) 52 #define VATPIT_LOCKED(vatpit) mtx_owned(&((vatpit)->mtx)) 53 54 #define TIMER_SEL_MASK 0xc0 55 #define TIMER_RW_MASK 0x30 56 #define TIMER_MODE_MASK 0x0f 57 #define TIMER_SEL_READBACK 0xc0 58 59 #define TIMER_STS_OUT 0x80 60 #define TIMER_STS_NULLCNT 0x40 61 62 #define TIMER_RB_LCTR 0x20 63 #define TIMER_RB_LSTATUS 0x10 64 #define TIMER_RB_CTR_2 0x08 65 #define TIMER_RB_CTR_1 0x04 66 #define TIMER_RB_CTR_0 0x02 67 68 #define TMR2_OUT_STS 0x20 69 70 #define PIT_8254_FREQ 1193182 71 #define TIMER_DIV(freq, hz) (((freq) + (hz) / 2) / (hz)) 72 73 struct vatpit_callout_arg { 74 struct vatpit *vatpit; 75 int channel_num; 76 }; 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 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 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 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) { 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 return; 177 } 178 179 static void 180 pit_timer_start_cntr0(struct vatpit *vatpit) 181 { 182 struct channel *c; 183 struct bintime now, delta; 184 sbintime_t precision; 185 186 c = &vatpit->channel[0]; 187 if (c->initial != 0) { 188 delta.sec = 0; 189 delta.frac = vatpit->freq_bt.frac * c->initial; 190 bintime_add(&c->callout_bt, &delta); 191 precision = bttosbt(delta) >> tc_precexp; 192 193 /* 194 * Reset 'callout_bt' if the time that the callout 195 * was supposed to fire is more than 'c->initial' 196 * ticks in the past. 197 */ 198 binuptime(&now); 199 if (bintime_cmp(&c->callout_bt, &now, <)) { 200 c->callout_bt = now; 201 bintime_add(&c->callout_bt, &delta); 202 } 203 204 callout_reset_sbt(&c->callout, bttosbt(c->callout_bt), 205 precision, vatpit_callout_handler, &c->callout_arg, 206 C_ABSOLUTE); 207 } 208 } 209 210 static uint16_t 211 pit_update_counter(struct vatpit *vatpit, struct channel *c, bool latch) 212 { 213 uint16_t lval; 214 uint64_t delta_ticks; 215 216 /* cannot latch a new value until the old one has been consumed */ 217 if (latch && c->olbyte != 0) 218 return (0); 219 220 if (c->initial == 0) { 221 /* 222 * This is possibly an o/s bug - reading the value of 223 * the timer without having set up the initial value. 224 * 225 * The original user-space version of this code set 226 * the timer to 100hz in this condition; do the same 227 * here. 228 */ 229 c->initial = TIMER_DIV(PIT_8254_FREQ, 100); 230 binuptime(&c->now_bt); 231 c->status &= ~TIMER_STS_NULLCNT; 232 } 233 234 delta_ticks = vatpit_delta_ticks(vatpit, c); 235 lval = c->initial - delta_ticks % c->initial; 236 237 if (latch) { 238 c->olbyte = 2; 239 c->ol[1] = lval; /* LSB */ 240 c->ol[0] = lval >> 8; /* MSB */ 241 } 242 243 return (lval); 244 } 245 246 static int 247 pit_readback1(struct vatpit *vatpit, int channel, uint8_t cmd) 248 { 249 struct channel *c; 250 251 c = &vatpit->channel[channel]; 252 253 /* 254 * Latch the count/status of the timer if not already latched. 255 * N.B. that the count/status latch-select bits are active-low. 256 */ 257 if (!(cmd & TIMER_RB_LCTR) && !c->olbyte) { 258 (void) pit_update_counter(vatpit, c, true); 259 } 260 261 if (!(cmd & TIMER_RB_LSTATUS) && !c->slatched) { 262 c->slatched = true; 263 /* 264 * For mode 0, see if the elapsed time is greater 265 * than the initial value - this results in the 266 * output pin being set to 1 in the status byte. 267 */ 268 if (c->mode == TIMER_INTTC && vatpit_get_out(vatpit, channel)) 269 c->status |= TIMER_STS_OUT; 270 else 271 c->status &= ~TIMER_STS_OUT; 272 } 273 274 return (0); 275 } 276 277 static int 278 pit_readback(struct vatpit *vatpit, uint8_t cmd) 279 { 280 int error; 281 282 /* 283 * The readback command can apply to all timers. 284 */ 285 error = 0; 286 if (cmd & TIMER_RB_CTR_0) 287 error = pit_readback1(vatpit, 0, cmd); 288 if (!error && cmd & TIMER_RB_CTR_1) 289 error = pit_readback1(vatpit, 1, cmd); 290 if (!error && cmd & TIMER_RB_CTR_2) 291 error = pit_readback1(vatpit, 2, cmd); 292 293 return (error); 294 } 295 296 297 static int 298 vatpit_update_mode(struct vatpit *vatpit, uint8_t val) 299 { 300 struct channel *c; 301 int sel, rw, mode; 302 303 sel = val & TIMER_SEL_MASK; 304 rw = val & TIMER_RW_MASK; 305 mode = val & TIMER_MODE_MASK; 306 307 if (sel == TIMER_SEL_READBACK) 308 return (pit_readback(vatpit, val)); 309 310 if (rw != TIMER_LATCH && rw != TIMER_16BIT) 311 return (-1); 312 313 if (rw != TIMER_LATCH) { 314 /* 315 * Counter mode is not affected when issuing a 316 * latch command. 317 */ 318 if (mode != TIMER_INTTC && 319 mode != TIMER_RATEGEN && 320 mode != TIMER_SQWAVE && 321 mode != TIMER_SWSTROBE) 322 return (-1); 323 } 324 325 c = &vatpit->channel[sel >> 6]; 326 if (rw == TIMER_LATCH) 327 pit_update_counter(vatpit, c, true); 328 else { 329 c->mode = mode; 330 c->olbyte = 0; /* reset latch after reprogramming */ 331 c->status |= TIMER_STS_NULLCNT; 332 } 333 334 return (0); 335 } 336 337 int 338 vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, 339 uint32_t *eax) 340 { 341 struct vatpit *vatpit; 342 struct channel *c; 343 uint8_t val; 344 int error; 345 346 vatpit = vm_atpit(vm); 347 348 if (bytes != 1) 349 return (-1); 350 351 val = *eax; 352 353 if (port == TIMER_MODE) { 354 if (in) { 355 VM_CTR0(vatpit->vm, "vatpit attempt to read mode"); 356 return (-1); 357 } 358 359 VATPIT_LOCK(vatpit); 360 error = vatpit_update_mode(vatpit, val); 361 VATPIT_UNLOCK(vatpit); 362 363 return (error); 364 } 365 366 /* counter ports */ 367 KASSERT(port >= TIMER_CNTR0 && port <= TIMER_CNTR2, 368 ("invalid port 0x%x", port)); 369 c = &vatpit->channel[port - TIMER_CNTR0]; 370 371 VATPIT_LOCK(vatpit); 372 if (in && c->slatched) { 373 /* 374 * Return the status byte if latched 375 */ 376 *eax = c->status; 377 c->slatched = false; 378 c->status = 0; 379 } else if (in) { 380 /* 381 * The spec says that once the output latch is completely 382 * read it should revert to "following" the counter. Use 383 * the free running counter for this case (i.e. Linux 384 * TSC calibration). Assuming the access mode is 16-bit, 385 * toggle the MSB/LSB bit on each read. 386 */ 387 if (c->olbyte == 0) { 388 uint16_t tmp; 389 390 tmp = pit_update_counter(vatpit, c, false); 391 if (c->frbyte) 392 tmp >>= 8; 393 tmp &= 0xff; 394 *eax = tmp; 395 c->frbyte ^= 1; 396 } else 397 *eax = c->ol[--c->olbyte]; 398 } else { 399 c->cr[c->crbyte++] = *eax; 400 if (c->crbyte == 2) { 401 c->status &= ~TIMER_STS_NULLCNT; 402 c->frbyte = 0; 403 c->crbyte = 0; 404 c->initial = c->cr[0] | (uint16_t)c->cr[1] << 8; 405 binuptime(&c->now_bt); 406 /* Start an interval timer for channel 0 */ 407 if (port == TIMER_CNTR0) { 408 c->callout_bt = c->now_bt; 409 pit_timer_start_cntr0(vatpit); 410 } 411 if (c->initial == 0) 412 c->initial = 0xffff; 413 } 414 } 415 VATPIT_UNLOCK(vatpit); 416 417 return (0); 418 } 419 420 int 421 vatpit_nmisc_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, 422 uint32_t *eax) 423 { 424 struct vatpit *vatpit; 425 426 vatpit = vm_atpit(vm); 427 428 if (in) { 429 VATPIT_LOCK(vatpit); 430 if (vatpit_get_out(vatpit, 2)) 431 *eax = TMR2_OUT_STS; 432 else 433 *eax = 0; 434 435 VATPIT_UNLOCK(vatpit); 436 } 437 438 return (0); 439 } 440 441 struct vatpit * 442 vatpit_init(struct vm *vm) 443 { 444 struct vatpit *vatpit; 445 struct vatpit_callout_arg *arg; 446 int i; 447 448 vatpit = malloc(sizeof(struct vatpit), M_VATPIT, M_WAITOK | M_ZERO); 449 vatpit->vm = vm; 450 451 mtx_init(&vatpit->mtx, "vatpit lock", NULL, MTX_SPIN); 452 453 FREQ2BT(PIT_8254_FREQ, &vatpit->freq_bt); 454 455 for (i = 0; i < 3; i++) { 456 callout_init(&vatpit->channel[i].callout, 1); 457 arg = &vatpit->channel[i].callout_arg; 458 arg->vatpit = vatpit; 459 arg->channel_num = i; 460 } 461 462 return (vatpit); 463 } 464 465 void 466 vatpit_cleanup(struct vatpit *vatpit) 467 { 468 int i; 469 470 for (i = 0; i < 3; i++) 471 callout_drain(&vatpit->channel[i].callout); 472 473 free(vatpit, M_VATPIT); 474 } 475