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