1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * William Jolitz and Don Ahn. 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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from: @(#)clock.c 7.2 (Berkeley) 5/12/91 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 /* 39 * Routines to handle clock hardware. 40 */ 41 42 #ifndef __amd64__ 43 #include "opt_apic.h" 44 #endif 45 #include "opt_clock.h" 46 #include "opt_kdtrace.h" 47 #include "opt_isa.h" 48 #include "opt_mca.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/bus.h> 53 #include <sys/lock.h> 54 #include <sys/kdb.h> 55 #include <sys/mutex.h> 56 #include <sys/proc.h> 57 #include <sys/timetc.h> 58 #include <sys/kernel.h> 59 #include <sys/module.h> 60 #include <sys/sched.h> 61 #include <sys/smp.h> 62 #include <sys/sysctl.h> 63 64 #include <machine/clock.h> 65 #include <machine/cpu.h> 66 #include <machine/intr_machdep.h> 67 #include <machine/md_var.h> 68 #include <machine/apicvar.h> 69 #include <machine/ppireg.h> 70 #include <machine/timerreg.h> 71 #include <machine/smp.h> 72 73 #include <isa/rtc.h> 74 #ifdef DEV_ISA 75 #include <isa/isareg.h> 76 #include <isa/isavar.h> 77 #endif 78 79 #ifdef DEV_MCA 80 #include <i386/bios/mca_machdep.h> 81 #endif 82 83 #ifdef KDTRACE_HOOKS 84 #include <sys/dtrace_bsd.h> 85 #endif 86 87 #define TIMER_DIV(x) ((i8254_freq + (x) / 2) / (x)) 88 89 int clkintr_pending; 90 static int pscnt = 1; 91 static int psdiv = 1; 92 #ifndef TIMER_FREQ 93 #define TIMER_FREQ 1193182 94 #endif 95 u_int i8254_freq = TIMER_FREQ; 96 TUNABLE_INT("hw.i8254.freq", &i8254_freq); 97 int i8254_max_count; 98 static int i8254_real_max_count; 99 100 struct mtx clock_lock; 101 static struct intsrc *i8254_intsrc; 102 static u_int32_t i8254_lastcount; 103 static u_int32_t i8254_offset; 104 static int (*i8254_pending)(struct intsrc *); 105 static int i8254_ticked; 106 static int using_atrtc_timer; 107 static enum lapic_clock using_lapic_timer = LAPIC_CLOCK_NONE; 108 109 /* Values for timerX_state: */ 110 #define RELEASED 0 111 #define RELEASE_PENDING 1 112 #define ACQUIRED 2 113 #define ACQUIRE_PENDING 3 114 115 static u_char timer2_state; 116 117 static unsigned i8254_get_timecount(struct timecounter *tc); 118 static unsigned i8254_simple_get_timecount(struct timecounter *tc); 119 static void set_i8254_freq(u_int freq, int intr_freq); 120 121 static struct timecounter i8254_timecounter = { 122 i8254_get_timecount, /* get_timecount */ 123 0, /* no poll_pps */ 124 ~0u, /* counter_mask */ 125 0, /* frequency */ 126 "i8254", /* name */ 127 0 /* quality */ 128 }; 129 130 int 131 hardclockintr(struct trapframe *frame) 132 { 133 134 if (PCPU_GET(cpuid) == 0) 135 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame)); 136 else 137 hardclock_cpu(TRAPF_USERMODE(frame)); 138 return (FILTER_HANDLED); 139 } 140 141 int 142 statclockintr(struct trapframe *frame) 143 { 144 145 profclockintr(frame); 146 statclock(TRAPF_USERMODE(frame)); 147 return (FILTER_HANDLED); 148 } 149 150 int 151 profclockintr(struct trapframe *frame) 152 { 153 154 if (!using_atrtc_timer) 155 hardclockintr(frame); 156 if (profprocs != 0) 157 profclock(TRAPF_USERMODE(frame), TRAPF_PC(frame)); 158 return (FILTER_HANDLED); 159 } 160 161 static int 162 clkintr(struct trapframe *frame) 163 { 164 165 if (timecounter->tc_get_timecount == i8254_get_timecount) { 166 mtx_lock_spin(&clock_lock); 167 if (i8254_ticked) 168 i8254_ticked = 0; 169 else { 170 i8254_offset += i8254_max_count; 171 i8254_lastcount = 0; 172 } 173 clkintr_pending = 0; 174 mtx_unlock_spin(&clock_lock); 175 } 176 KASSERT(using_lapic_timer == LAPIC_CLOCK_NONE, 177 ("clk interrupt enabled with lapic timer")); 178 179 #ifdef KDTRACE_HOOKS 180 /* 181 * If the DTrace hooks are configured and a callback function 182 * has been registered, then call it to process the high speed 183 * timers. 184 */ 185 int cpu = PCPU_GET(cpuid); 186 if (lapic_cyclic_clock_func[cpu] != NULL) 187 (*lapic_cyclic_clock_func[cpu])(frame); 188 #endif 189 190 if (using_atrtc_timer) { 191 #ifdef SMP 192 if (smp_started) 193 ipi_all_but_self(IPI_HARDCLOCK); 194 #endif 195 hardclockintr(frame); 196 } else { 197 if (--pscnt <= 0) { 198 pscnt = psratio; 199 #ifdef SMP 200 if (smp_started) 201 ipi_all_but_self(IPI_STATCLOCK); 202 #endif 203 statclockintr(frame); 204 } else { 205 #ifdef SMP 206 if (smp_started) 207 ipi_all_but_self(IPI_PROFCLOCK); 208 #endif 209 profclockintr(frame); 210 } 211 } 212 213 #ifdef DEV_MCA 214 /* Reset clock interrupt by asserting bit 7 of port 0x61 */ 215 if (MCA_system) 216 outb(0x61, inb(0x61) | 0x80); 217 #endif 218 return (FILTER_HANDLED); 219 } 220 221 int 222 timer_spkr_acquire(void) 223 { 224 int mode; 225 226 mode = TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT; 227 228 if (timer2_state != RELEASED) 229 return (-1); 230 timer2_state = ACQUIRED; 231 232 /* 233 * This access to the timer registers is as atomic as possible 234 * because it is a single instruction. We could do better if we 235 * knew the rate. Use of splclock() limits glitches to 10-100us, 236 * and this is probably good enough for timer2, so we aren't as 237 * careful with it as with timer0. 238 */ 239 outb(TIMER_MODE, TIMER_SEL2 | (mode & 0x3f)); 240 ppi_spkr_on(); /* enable counter2 output to speaker */ 241 return (0); 242 } 243 244 int 245 timer_spkr_release(void) 246 { 247 248 if (timer2_state != ACQUIRED) 249 return (-1); 250 timer2_state = RELEASED; 251 outb(TIMER_MODE, TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT); 252 ppi_spkr_off(); /* disable counter2 output to speaker */ 253 return (0); 254 } 255 256 void 257 timer_spkr_setfreq(int freq) 258 { 259 260 freq = i8254_freq / freq; 261 mtx_lock_spin(&clock_lock); 262 outb(TIMER_CNTR2, freq & 0xff); 263 outb(TIMER_CNTR2, freq >> 8); 264 mtx_unlock_spin(&clock_lock); 265 } 266 267 /* 268 * This routine receives statistical clock interrupts from the RTC. 269 * As explained above, these occur at 128 interrupts per second. 270 * When profiling, we receive interrupts at a rate of 1024 Hz. 271 * 272 * This does not actually add as much overhead as it sounds, because 273 * when the statistical clock is active, the hardclock driver no longer 274 * needs to keep (inaccurate) statistics on its own. This decouples 275 * statistics gathering from scheduling interrupts. 276 * 277 * The RTC chip requires that we read status register C (RTC_INTR) 278 * to acknowledge an interrupt, before it will generate the next one. 279 * Under high interrupt load, rtcintr() can be indefinitely delayed and 280 * the clock can tick immediately after the read from RTC_INTR. In this 281 * case, the mc146818A interrupt signal will not drop for long enough 282 * to register with the 8259 PIC. If an interrupt is missed, the stat 283 * clock will halt, considerably degrading system performance. This is 284 * why we use 'while' rather than a more straightforward 'if' below. 285 * Stat clock ticks can still be lost, causing minor loss of accuracy 286 * in the statistics, but the stat clock will no longer stop. 287 */ 288 static int 289 rtcintr(struct trapframe *frame) 290 { 291 int flag = 0; 292 293 while (rtcin(RTC_INTR) & RTCIR_PERIOD) { 294 flag = 1; 295 if (--pscnt <= 0) { 296 pscnt = psdiv; 297 #ifdef SMP 298 if (smp_started) 299 ipi_all_but_self(IPI_STATCLOCK); 300 #endif 301 statclockintr(frame); 302 } else { 303 #ifdef SMP 304 if (smp_started) 305 ipi_all_but_self(IPI_PROFCLOCK); 306 #endif 307 profclockintr(frame); 308 } 309 } 310 return(flag ? FILTER_HANDLED : FILTER_STRAY); 311 } 312 313 static int 314 getit(void) 315 { 316 int high, low; 317 318 mtx_lock_spin(&clock_lock); 319 320 /* Select timer0 and latch counter value. */ 321 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); 322 323 low = inb(TIMER_CNTR0); 324 high = inb(TIMER_CNTR0); 325 326 mtx_unlock_spin(&clock_lock); 327 return ((high << 8) | low); 328 } 329 330 /* 331 * Wait "n" microseconds. 332 * Relies on timer 1 counting down from (i8254_freq / hz) 333 * Note: timer had better have been programmed before this is first used! 334 */ 335 void 336 DELAY(int n) 337 { 338 int delta, prev_tick, tick, ticks_left; 339 340 #ifdef DELAYDEBUG 341 int getit_calls = 1; 342 int n1; 343 static int state = 0; 344 #endif 345 346 if (tsc_freq != 0 && !tsc_is_broken) { 347 uint64_t start, end, now; 348 349 sched_pin(); 350 start = rdtsc(); 351 end = start + (tsc_freq * n) / 1000000; 352 do { 353 cpu_spinwait(); 354 now = rdtsc(); 355 } while (now < end || (now > start && end < start)); 356 sched_unpin(); 357 return; 358 } 359 #ifdef DELAYDEBUG 360 if (state == 0) { 361 state = 1; 362 for (n1 = 1; n1 <= 10000000; n1 *= 10) 363 DELAY(n1); 364 state = 2; 365 } 366 if (state == 1) 367 printf("DELAY(%d)...", n); 368 #endif 369 /* 370 * Read the counter first, so that the rest of the setup overhead is 371 * counted. Guess the initial overhead is 20 usec (on most systems it 372 * takes about 1.5 usec for each of the i/o's in getit(). The loop 373 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The 374 * multiplications and divisions to scale the count take a while). 375 * 376 * However, if ddb is active then use a fake counter since reading 377 * the i8254 counter involves acquiring a lock. ddb must not do 378 * locking for many reasons, but it calls here for at least atkbd 379 * input. 380 */ 381 #ifdef KDB 382 if (kdb_active) 383 prev_tick = 1; 384 else 385 #endif 386 prev_tick = getit(); 387 n -= 0; /* XXX actually guess no initial overhead */ 388 /* 389 * Calculate (n * (i8254_freq / 1e6)) without using floating point 390 * and without any avoidable overflows. 391 */ 392 if (n <= 0) 393 ticks_left = 0; 394 else if (n < 256) 395 /* 396 * Use fixed point to avoid a slow division by 1000000. 397 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest. 398 * 2^15 is the first power of 2 that gives exact results 399 * for n between 0 and 256. 400 */ 401 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15; 402 else 403 /* 404 * Don't bother using fixed point, although gcc-2.7.2 405 * generates particularly poor code for the long long 406 * division, since even the slow way will complete long 407 * before the delay is up (unless we're interrupted). 408 */ 409 ticks_left = ((u_int)n * (long long)i8254_freq + 999999) 410 / 1000000; 411 412 while (ticks_left > 0) { 413 #ifdef KDB 414 if (kdb_active) { 415 inb(0x84); 416 tick = prev_tick - 1; 417 if (tick <= 0) 418 tick = i8254_max_count; 419 } else 420 #endif 421 tick = getit(); 422 #ifdef DELAYDEBUG 423 ++getit_calls; 424 #endif 425 delta = prev_tick - tick; 426 prev_tick = tick; 427 if (delta < 0) { 428 delta += i8254_max_count; 429 /* 430 * Guard against i8254_max_count being wrong. 431 * This shouldn't happen in normal operation, 432 * but it may happen if set_i8254_freq() is 433 * traced. 434 */ 435 if (delta < 0) 436 delta = 0; 437 } 438 ticks_left -= delta; 439 } 440 #ifdef DELAYDEBUG 441 if (state == 1) 442 printf(" %d calls to getit() at %d usec each\n", 443 getit_calls, (n + 5) / getit_calls); 444 #endif 445 } 446 447 static void 448 set_i8254_freq(u_int freq, int intr_freq) 449 { 450 int new_i8254_real_max_count; 451 452 i8254_timecounter.tc_frequency = freq; 453 mtx_lock_spin(&clock_lock); 454 i8254_freq = freq; 455 if (using_lapic_timer != LAPIC_CLOCK_NONE) 456 new_i8254_real_max_count = 0x10000; 457 else 458 new_i8254_real_max_count = TIMER_DIV(intr_freq); 459 if (new_i8254_real_max_count != i8254_real_max_count) { 460 i8254_real_max_count = new_i8254_real_max_count; 461 if (i8254_real_max_count == 0x10000) 462 i8254_max_count = 0xffff; 463 else 464 i8254_max_count = i8254_real_max_count; 465 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 466 outb(TIMER_CNTR0, i8254_real_max_count & 0xff); 467 outb(TIMER_CNTR0, i8254_real_max_count >> 8); 468 } 469 mtx_unlock_spin(&clock_lock); 470 } 471 472 static void 473 i8254_restore(void) 474 { 475 476 mtx_lock_spin(&clock_lock); 477 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); 478 outb(TIMER_CNTR0, i8254_real_max_count & 0xff); 479 outb(TIMER_CNTR0, i8254_real_max_count >> 8); 480 mtx_unlock_spin(&clock_lock); 481 } 482 483 #ifndef __amd64__ 484 /* 485 * Restore all the timers non-atomically (XXX: should be atomically). 486 * 487 * This function is called from pmtimer_resume() to restore all the timers. 488 * This should not be necessary, but there are broken laptops that do not 489 * restore all the timers on resume. 490 * As long as pmtimer is not part of amd64 suport, skip this for the amd64 491 * case. 492 */ 493 void 494 timer_restore(void) 495 { 496 497 i8254_restore(); /* restore i8254_freq and hz */ 498 atrtc_restore(); /* reenable RTC interrupts */ 499 } 500 #endif 501 502 /* This is separate from startrtclock() so that it can be called early. */ 503 void 504 i8254_init(void) 505 { 506 507 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE); 508 set_i8254_freq(i8254_freq, hz); 509 } 510 511 void 512 startrtclock() 513 { 514 515 atrtc_start(); 516 517 set_i8254_freq(i8254_freq, hz); 518 tc_init(&i8254_timecounter); 519 520 init_TSC(); 521 } 522 523 /* 524 * Start both clocks running. 525 */ 526 void 527 cpu_initclocks() 528 { 529 530 #if defined(__amd64__) || defined(DEV_APIC) 531 using_lapic_timer = lapic_setup_clock(); 532 #endif 533 /* 534 * If we aren't using the local APIC timer to drive the kernel 535 * clocks, setup the interrupt handler for the 8254 timer 0 so 536 * that it can drive hardclock(). Otherwise, change the 8254 537 * timecounter to user a simpler algorithm. 538 */ 539 if (using_lapic_timer == LAPIC_CLOCK_NONE) { 540 intr_add_handler("clk", 0, (driver_filter_t *)clkintr, NULL, 541 NULL, INTR_TYPE_CLK, NULL); 542 i8254_intsrc = intr_lookup_source(0); 543 if (i8254_intsrc != NULL) 544 i8254_pending = 545 i8254_intsrc->is_pic->pic_source_pending; 546 } else { 547 i8254_timecounter.tc_get_timecount = 548 i8254_simple_get_timecount; 549 i8254_timecounter.tc_counter_mask = 0xffff; 550 set_i8254_freq(i8254_freq, hz); 551 } 552 553 /* Initialize RTC. */ 554 atrtc_start(); 555 556 /* 557 * If the separate statistics clock hasn't been explicility disabled 558 * and we aren't already using the local APIC timer to drive the 559 * kernel clocks, then setup the RTC to periodically interrupt to 560 * drive statclock() and profclock(). 561 */ 562 if (using_lapic_timer != LAPIC_CLOCK_ALL) { 563 using_atrtc_timer = atrtc_setup_clock(); 564 if (using_atrtc_timer) { 565 /* Enable periodic interrupts from the RTC. */ 566 intr_add_handler("rtc", 8, 567 (driver_filter_t *)rtcintr, NULL, NULL, 568 INTR_TYPE_CLK, NULL); 569 atrtc_enable_intr(); 570 } else { 571 profhz = hz; 572 if (hz < 128) 573 stathz = hz; 574 else 575 stathz = hz / (hz / 128); 576 } 577 } 578 579 init_TSC_tc(); 580 } 581 582 void 583 cpu_startprofclock(void) 584 { 585 586 if (using_lapic_timer == LAPIC_CLOCK_ALL || !using_atrtc_timer) 587 return; 588 atrtc_rate(RTCSA_PROF); 589 psdiv = pscnt = psratio; 590 } 591 592 void 593 cpu_stopprofclock(void) 594 { 595 596 if (using_lapic_timer == LAPIC_CLOCK_ALL || !using_atrtc_timer) 597 return; 598 atrtc_rate(RTCSA_NOPROF); 599 psdiv = pscnt = 1; 600 } 601 602 static int 603 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS) 604 { 605 int error; 606 u_int freq; 607 608 /* 609 * Use `i8254' instead of `timer' in external names because `timer' 610 * is is too generic. Should use it everywhere. 611 */ 612 freq = i8254_freq; 613 error = sysctl_handle_int(oidp, &freq, 0, req); 614 if (error == 0 && req->newptr != NULL) 615 set_i8254_freq(freq, hz); 616 return (error); 617 } 618 619 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW, 620 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", ""); 621 622 static unsigned 623 i8254_simple_get_timecount(struct timecounter *tc) 624 { 625 626 return (i8254_max_count - getit()); 627 } 628 629 static unsigned 630 i8254_get_timecount(struct timecounter *tc) 631 { 632 register_t flags; 633 u_int count; 634 u_int high, low; 635 636 #ifdef __amd64__ 637 flags = read_rflags(); 638 #else 639 flags = read_eflags(); 640 #endif 641 mtx_lock_spin(&clock_lock); 642 643 /* Select timer0 and latch counter value. */ 644 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); 645 646 low = inb(TIMER_CNTR0); 647 high = inb(TIMER_CNTR0); 648 count = i8254_max_count - ((high << 8) | low); 649 if (count < i8254_lastcount || 650 (!i8254_ticked && (clkintr_pending || 651 ((count < 20 || (!(flags & PSL_I) && 652 count < i8254_max_count / 2u)) && 653 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) { 654 i8254_ticked = 1; 655 i8254_offset += i8254_max_count; 656 } 657 i8254_lastcount = count; 658 count += i8254_offset; 659 mtx_unlock_spin(&clock_lock); 660 return (count); 661 } 662 663 #ifdef DEV_ISA 664 /* 665 * Attach to the ISA PnP descriptors for the timer 666 */ 667 static struct isa_pnp_id attimer_ids[] = { 668 { 0x0001d041 /* PNP0100 */, "AT timer" }, 669 { 0 } 670 }; 671 672 static int 673 attimer_probe(device_t dev) 674 { 675 int result; 676 677 result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids); 678 if (result <= 0) 679 device_quiet(dev); 680 return(result); 681 } 682 683 static int 684 attimer_attach(device_t dev) 685 { 686 return(0); 687 } 688 689 static int 690 attimer_resume(device_t dev) 691 { 692 693 i8254_restore(); 694 return (0); 695 } 696 697 static device_method_t attimer_methods[] = { 698 /* Device interface */ 699 DEVMETHOD(device_probe, attimer_probe), 700 DEVMETHOD(device_attach, attimer_attach), 701 DEVMETHOD(device_detach, bus_generic_detach), 702 DEVMETHOD(device_shutdown, bus_generic_shutdown), 703 DEVMETHOD(device_suspend, bus_generic_suspend), 704 DEVMETHOD(device_resume, attimer_resume), 705 { 0, 0 } 706 }; 707 708 static driver_t attimer_driver = { 709 "attimer", 710 attimer_methods, 711 1, /* no softc */ 712 }; 713 714 static devclass_t attimer_devclass; 715 716 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0); 717 DRIVER_MODULE(attimer, acpi, attimer_driver, attimer_devclass, 0, 0); 718 719 #endif /* DEV_ISA */ 720