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