2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
27 * File: i386/rtclock.c
28 * Purpose: Routines for handling the machine dependent
29 * real-time clock. This clock is generated by
30 * the Intel 8254 Programmable Interval Timer.
34 #include <platforms.h>
37 #include <mach/mach_types.h>
39 #include <kern/cpu_number.h>
40 #include <kern/cpu_data.h>
41 #include <kern/clock.h>
42 #include <kern/host_notify.h>
43 #include <kern/macro_help.h>
44 #include <kern/misc_protos.h>
46 #include <machine/mach_param.h> /* HZ */
47 #include <mach/vm_prot.h>
49 #include <vm/vm_kern.h> /* for kernel_map */
53 #include <i386/misc_protos.h>
54 #include <i386/rtclock_entries.h>
55 #include <i386/hardclock_entries.h>
56 #include <i386/proc_reg.h>
57 #include <i386/machine_cpu.h>
58 #include <pexpert/pexpert.h>
60 #define DISPLAYENTER(x) printf("[RTCLOCK] entering " #x "\n");
61 #define DISPLAYEXIT(x) printf("[RTCLOCK] leaving " #x "\n");
62 #define DISPLAYVALUE(x,y) printf("[RTCLOCK] " #x ":" #y " = 0x%08x \n",y);
64 int sysclk_config(void);
66 int sysclk_init(void);
68 kern_return_t
sysclk_gettime(
69 mach_timespec_t
*cur_time
);
71 kern_return_t
sysclk_getattr(
72 clock_flavor_t flavor
,
74 mach_msg_type_number_t
*count
);
76 kern_return_t
sysclk_setattr(
77 clock_flavor_t flavor
,
79 mach_msg_type_number_t count
);
82 mach_timespec_t
*alarm_time
);
84 extern void (*IOKitRegisterInterruptHook
)(void *, int irq
, int isclock
);
87 * Lists of clock routines.
89 struct clock_ops sysclk_ops
= {
90 sysclk_config
, sysclk_init
,
92 sysclk_getattr
, sysclk_setattr
,
96 int calend_config(void);
98 int calend_init(void);
100 kern_return_t
calend_gettime(
101 mach_timespec_t
*cur_time
);
103 kern_return_t
calend_getattr(
104 clock_flavor_t flavor
,
106 mach_msg_type_number_t
*count
);
108 struct clock_ops calend_ops
= {
109 calend_config
, calend_init
,
115 /* local data declarations */
116 mach_timespec_t
*RtcTime
= (mach_timespec_t
*)0;
117 mach_timespec_t
*RtcAlrm
;
120 /* global data declarations */
124 mach_timespec_t time
;
125 mach_timespec_t alarm_time
; /* time of next alarm */
127 mach_timespec_t calend_offset
;
128 boolean_t calend_is_set
;
130 int64_t calend_adjtotal
;
131 int32_t calend_adjdelta
;
133 uint64_t timer_deadline
;
134 boolean_t timer_is_set
;
135 clock_timer_func_t timer_expire
;
137 clock_res_t new_ires
; /* pending new resolution (nano ) */
138 clock_res_t intr_nsec
; /* interrupt resolution (nano) */
139 mach_timebase_info_data_t timebase_const
;
141 decl_simple_lock_data(,lock
) /* real-time clock device lock */
144 unsigned int clknum
; /* clks per second */
145 unsigned int new_clknum
; /* pending clknum */
146 unsigned int time_per_clk
; /* time per clk in ZHZ */
147 unsigned int clks_per_int
; /* clks per interrupt */
148 unsigned int clks_per_int_99
;
149 int rtc_intr_count
; /* interrupt counter */
150 int rtc_intr_hertz
; /* interrupts per HZ */
151 int rtc_intr_freq
; /* interrupt frequency */
152 int rtc_print_lost_tick
; /* print lost tick */
154 uint32_t rtc_cyc_per_sec
; /* processor cycles per seconds */
155 uint32_t rtc_quant_scale
; /* used internally to convert clocks to nanos */
158 * Macros to lock/unlock real-time clock device.
160 #define LOCK_RTC(s) \
163 simple_lock(&rtclock.lock); \
166 #define UNLOCK_RTC(s) \
168 simple_unlock(&rtclock.lock); \
173 * i8254 control. ** MONUMENT **
175 * The i8254 is a traditional PC device with some arbitrary characteristics.
176 * Basically, it is a register that counts at a fixed rate and can be
177 * programmed to generate an interrupt every N counts. The count rate is
178 * clknum counts per second (see pit.h), historically 1193167 we believe.
179 * Various constants are computed based on this value, and we calculate
180 * them at init time for execution efficiency. To obtain sufficient
181 * accuracy, some of the calculation are most easily done in floating
182 * point and then converted to int.
184 * We want an interrupt every 10 milliseconds, approximately. The count
185 * which will do that is clks_per_int. However, that many counts is not
186 * *exactly* 10 milliseconds; it is a bit more or less depending on
187 * roundoff. The actual time per tick is calculated and saved in
188 * rtclock.intr_nsec, and it is that value which is added to the time
189 * register on each tick.
191 * The i8254 counter can be read between interrupts in order to determine
192 * the time more accurately. The counter counts down from the preset value
193 * toward 0, and we have to handle the case where the counter has been
194 * reset just before being read and before the interrupt has been serviced.
195 * Given a count since the last interrupt, the time since then is given
196 * by (count * time_per_clk). In order to minimize integer truncation,
197 * we perform this calculation in an arbitrary unit of time which maintains
198 * the maximum precision, i.e. such that one tick is 1.0e9 of these units,
199 * or close to the precision of a 32-bit int. We then divide by this unit
200 * (which doesn't lose precision) to get nanoseconds. For notation
201 * purposes, this unit is defined as ZHZ = zanoseconds per nanosecond.
203 * This sequence to do all this is in sysclk_gettime. For efficiency, this
204 * sequence also needs the value that the counter will have if it has just
205 * overflowed, so we precompute that also.
207 * The fix for certain really old certain platforms has been removed
208 * (specifically the DEC XL5100) have been observed to have problem
209 * with latching the counter, and they occasionally (say, one out of
210 * 100,000 times) return a bogus value. Hence, the present code reads
211 * the counter twice and checks for a consistent pair of values.
216 * } while ( val2 > val || val2 < val - 10 );
219 * Some attributes of the rt clock can be changed, including the
220 * interrupt resolution. We default to the minimum resolution (10 ms),
221 * but allow a finer resolution to be requested. The assumed frequency
222 * of the clock can also be set since it appears that the actual
223 * frequency of real-world hardware can vary from the nominal by
224 * 200 ppm or more. When the frequency is set, the values above are
225 * recomputed and we continue without resetting or changing anything else.
227 #define RTC_MINRES (NSEC_PER_SEC / HZ) /* nsec per tick */
228 #define RTC_MAXRES (RTC_MINRES / 20) /* nsec per tick */
229 #define ZANO (1000000000)
230 #define ZHZ (ZANO / (NSEC_PER_SEC / HZ))
231 #define READ_8254(val) { \
232 outb(PITCTL_PORT, PIT_C0); \
233 (val) = inb(PITCTR0_PORT); \
234 (val) |= inb(PITCTR0_PORT) << 8 ; }
236 #define UI_CPUFREQ_ROUNDING_FACTOR 10000000
243 void rtc_setvals( unsigned int, clock_res_t
);
245 static void rtc_set_cyc_per_sec();
247 /* define assembly routines */
251 * Inlines to get timestamp counter value.
254 inline static uint64_t
258 asm volatile("rdtsc": "=A" (result
));
262 // create_mul_quant_GHZ create a constant that can be used to multiply
263 // the TSC by to create nanoseconds. This is a 32 bit number
264 // and the TSC *MUST* have a frequency higher than 1000Mhz for this routine to work
266 // The theory here is that we know how many TSCs-per-sec the processor runs at. Normally to convert this
267 // to nanoseconds you would multiply the current time stamp by 1000000000 (a billion) then divide
268 // by TSCs-per-sec to get nanoseconds. Unfortunatly the TSC is 64 bits which would leave us with
269 // 96 bit intermediate results from the dultiply that must be divided by.
271 // uint96 = tsc * numer
272 // nanos = uint96 / denom
273 // Instead, we create this quant constant and it becomes the numerator, the denominator
274 // can then be 0x100000000 which makes our division as simple as forgetting the lower 32 bits
275 // of the result. We can also pass this number to user space as the numer and pass 0xFFFFFFFF
276 // as the denom to converting raw counts to nanos. the difference is so small as to be undetectable
278 // unfortunatly we can not do this for sub GHZ processors. In that case, all we do is pass the CPU
279 // speed in raw as the denom and we pass in 1000000000 as the numerator. No short cuts allowed
281 inline static uint32_t
282 create_mul_quant_GHZ(uint32_t quant
)
284 return (uint32_t)((50000000ULL << 32) / quant
);
287 // this routine takes a value of raw TSC ticks and applies the passed mul_quant
288 // generated by create_mul_quant() This is our internal routine for creating
290 // since we don't really have uint96_t this routine basically does this....
291 // uint96_t intermediate = (*value) * scale
292 // return (intermediate >> 32)
293 inline static uint64_t
294 fast_get_nano_from_abs(uint64_t value
, int scale
)
296 asm (" movl %%edx,%%esi \n\t"
298 " movl %%edx,%%edi \n\t"
299 " movl %%esi,%%eax \n\t"
301 " xorl %%ecx,%%ecx \n\t"
302 " addl %%edi,%%eax \n\t"
311 * this routine basically does this...
312 * ts.tv_sec = nanos / 1000000000; create seconds
313 * ts.tv_nsec = nanos % 1000000000; create remainder nanos
315 inline static mach_timespec_t
316 nanos_to_timespec(uint64_t nanos
)
323 asm volatile("divl %1" : "+A" (ret
.u64
) : "r" (NSEC_PER_SEC
));
327 // the following two routine perform the 96 bit arithmetic we need to
328 // convert generic absolute<->nanoseconds
329 // the multiply routine takes a uint64_t and a uint32_t and returns the result in a
330 // uint32_t[3] array. the dicide routine takes this uint32_t[3] array and
331 // divides it by a uint32_t returning a uint64_t
333 longmul(uint64_t *abstime
, uint32_t multiplicand
, uint32_t *result
)
337 " movl %%eax,%%ebx \n\t"
338 " movl (%%eax),%%eax \n\t"
340 " xchg %%eax,%%ebx \n\t"
342 " movl 4(%%eax),%%eax \n\t"
344 " movl %2,%%ecx \n\t"
345 " movl %%ebx,(%%ecx) \n\t"
347 " addl %%ebx,%%eax \n\t"
349 " movl %%eax,4(%%ecx) \n\t"
350 " adcl $0,%%edx \n\t"
351 " movl %%edx,8(%%ecx) // and save it"
352 : : "a"(abstime
), "c"(multiplicand
), "m"(result
));
356 inline static uint64_t
357 longdiv(uint32_t *numer
, uint32_t denom
)
362 " movl %%eax,%%ebx \n\t"
363 " movl 8(%%eax),%%edx \n\t"
364 " movl 4(%%eax),%%eax \n\t"
366 " xchg %%ebx,%%eax \n\t"
367 " movl (%%eax),%%eax \n\t"
369 " xchg %%ebx,%%edx \n\t"
371 : "=A"(result
) : "a"(numer
),"c"(denom
));
375 #define PIT_Mode4 0x08 /* turn on mode 4 one shot software trigger */
377 // Enable or disable timer 2.
383 " and $253,%%al \n\t"
385 " outb %%al,$97 \n\t"
394 " and $253,%%al \n\t"
395 " outb %%al,$97 \n\t"
399 // ctimeRDTSC() routine sets up counter 2 to count down 1/20 of a second
400 // it pauses until the value is latched in the counter
401 // and then reads the time stamp counter to return to the caller
403 // Code to calculate how many processor cycles are in a second...
407 // first, tell the clock we are going to write 16 bytes to the counter and enable one-shot mode
408 // then write the two bytes into the clock register.
409 // loop until the value is "realized" in the clock, this happens on the next tick
412 " movb $184,%%al \n\t"
413 " outb %%al,$67 \n\t"
414 " movb %%dl,%%al \n\t"
415 " outb %%al,$66 \n\t"
416 " movb %%dh,%%al \n\t"
418 "1: inb $66,%%al \n\t"
420 " cmp %%al,%%dh \n\t"
422 : : "d"(value
) : "%al");
425 inline static uint64_t
426 get_PIT2(unsigned int *value
)
428 // this routine first latches the time, then gets the time stamp so we know
429 // how long the read will take later. Reads
430 register uint64_t result
;
432 " xorl %%ecx,%%ecx \n\t"
433 " movb $128,%%al \n\t"
434 " outb %%al,$67 \n\t"
438 " movb %%al,%%cl \n\t"
440 " movb %%al,%%ch \n\t"
442 : "=A"(result
), "=c"(*value
));
450 uint64_t saveTime
,intermediate
;
451 unsigned int timerValue
,x
;
452 boolean_t int_enabled
;
453 uint64_t fact
[6] = { 2000011734ll,
460 int_enabled
= ml_set_interrupts_enabled(FALSE
);
462 enable_PIT2(); // turn on PIT2
463 set_PIT2(0); // reset timer 2 to be zero
464 latchTime
= rdtsc_64(); // get the time stamp to time
465 latchTime
= get_PIT2(&timerValue
) - latchTime
; // time how long this takes
466 set_PIT2(59658); // set up the timer to count 1/20th a second
467 saveTime
= rdtsc_64(); // now time how ling a 20th a second is...
469 do { get_PIT2(&timerValue
); x
= timerValue
;} while (timerValue
> x
);
471 intermediate
= get_PIT2(&timerValue
);
472 if (timerValue
>x
) printf("Hey we are going backwards! %d, %d\n",timerValue
,x
);
474 } while ((timerValue
!= 0) && (timerValue
>5));
475 printf("Timer value:%d\n",timerValue
);
476 printf("intermediate 0x%08x:0x%08x\n",intermediate
);
477 printf("saveTime 0x%08x:0x%08x\n",saveTime
);
479 intermediate
= intermediate
- saveTime
; // raw # of tsc's it takes for about 1/20 second
480 intermediate
= intermediate
* fact
[timerValue
]; // actual time spent
481 intermediate
= intermediate
/ 2000000000ll; // rescale so its exactly 1/20 a second
482 intermediate
= intermediate
+ latchTime
; // add on our save fudge
483 set_PIT2(0); // reset timer 2 to be zero
484 disable_PIT2(0); // turn off PIT 2
485 ml_set_interrupts_enabled(int_enabled
);
490 rdtsctime_to_nanoseconds( void )
496 uint32_t intermediate
[3];
498 numer
= rtclock
.timebase_const
.numer
;
499 denom
= rtclock
.timebase_const
.denom
;
500 abstime
= rdtsc_64();
501 if (denom
== 0xFFFFFFFF) {
502 abstime
= fast_get_nano_from_abs(abstime
, numer
);
504 longmul(&abstime
, numer
, intermediate
);
505 abstime
= longdiv(intermediate
, denom
);
510 inline static mach_timespec_t
511 rdtsc_to_timespec(void)
514 currNanos
= rdtsctime_to_nanoseconds();
515 return nanos_to_timespec(currNanos
);
519 * Initialize non-zero clock structure values.
523 unsigned int new_clknum
,
527 unsigned int timeperclk
;
533 rtc_intr_freq
= (NSEC_PER_SEC
/ new_ires
);
534 rtc_intr_hertz
= rtc_intr_freq
/ HZ
;
535 clks_per_int
= (clknum
+ (rtc_intr_freq
/ 2)) / rtc_intr_freq
;
536 clks_per_int_99
= clks_per_int
- clks_per_int
/100;
539 * The following calculations are done with scaling integer operations
540 * in order that the integer results are accurate to the lsb.
542 timeperclk
= div_scale(ZANO
, clknum
, &scale0
); /* 838.105647 nsec */
544 time_per_clk
= mul_scale(ZHZ
, timeperclk
, &scale1
); /* 83810 */
546 time_per_clk
>>= (scale0
- scale1
);
547 else if (scale0
< scale1
)
548 panic("rtc_clock: time_per_clk overflow\n");
551 * Notice that rtclock.intr_nsec is signed ==> use unsigned int res
553 res
= mul_scale(clks_per_int
, timeperclk
, &scale1
); /* 10000276 */
555 rtclock
.intr_nsec
= res
>> (scale0
- scale1
);
557 panic("rtc_clock: rtclock.intr_nsec overflow\n");
560 RtcDelt
= rtclock
.intr_nsec
/2;
564 * Configure the real-time clock device. Return success (1)
575 mp_disable_preemption();
576 if (cpu_number() != master_cpu
) {
577 mp_enable_preemption();
580 mp_enable_preemption();
585 pic
= 0; /* FIXME .. interrupt registration moved to AppleIntelClock */
589 * We should attempt to test the real-time clock
590 * device here. If it were to fail, we should panic
593 RtcFlag
= /* test device */1;
594 printf("realtime clock configured\n");
596 simple_lock_init(&rtclock
.lock
, ETAP_NO_TRACE
);
601 * Initialize the real-time clock device. Return success (1)
602 * or failure (0). Since the real-time clock is required to
603 * provide canonical mapped time, we allocate a page to keep
604 * the clock time value. In addition, various variables used
605 * to support the clock are initialized. Note: the clock is
606 * not started until rtclock_reset is called.
613 mp_disable_preemption();
614 if (cpu_number() != master_cpu
) {
615 mp_enable_preemption();
618 mp_enable_preemption();
621 RtcTime
= &rtclock
.time
;
622 rtc_setvals( CLKNUM
, RTC_MINRES
); /* compute constants */
623 rtc_set_cyc_per_sec(); /* compute number of tsc beats per second */
624 clock_timebase_init();
628 static volatile unsigned int last_ival
= 0;
631 * Get the clock device time. This routine is responsible
632 * for converting the device's machine dependent time value
633 * into a canonical mach_timespec_t value.
637 mach_timespec_t
*cur_time
) /* OUT */
641 cur_time
->tv_nsec
= 0;
642 cur_time
->tv_sec
= 0;
643 return (KERN_SUCCESS
);
646 *cur_time
= rdtsc_to_timespec();
647 return (KERN_SUCCESS
);
651 sysclk_gettime_internal(
652 mach_timespec_t
*cur_time
) /* OUT */
656 cur_time
->tv_nsec
= 0;
657 cur_time
->tv_sec
= 0;
658 return (KERN_SUCCESS
);
660 *cur_time
= rdtsc_to_timespec();
661 return (KERN_SUCCESS
);
665 * Get the clock device time when ALL interrupts are already disabled.
666 * Same as above except for turning interrupts off and on.
667 * This routine is responsible for converting the device's machine dependent
668 * time value into a canonical mach_timespec_t value.
671 sysclk_gettime_interrupts_disabled(
672 mach_timespec_t
*cur_time
) /* OUT */
676 cur_time
->tv_nsec
= 0;
677 cur_time
->tv_sec
= 0;
680 *cur_time
= rdtsc_to_timespec();
684 // Code to calculate how many processor cycles are in a second...
687 rtc_set_cyc_per_sec()
690 uint32_t twen_cycles
;
693 twen_cycles
= timeRDTSC();
694 if (twen_cycles
> (1000000000/20)) {
695 // we create this value so that you can use just a "fast" multiply to get nanos
696 rtc_quant_scale
= create_mul_quant_GHZ(twen_cycles
);
697 rtclock
.timebase_const
.numer
= rtc_quant_scale
; // because ctimeRDTSC gives us 1/20 a seconds worth
698 rtclock
.timebase_const
.denom
= 0xffffffff; // so that nanoseconds = (TSC * numer) / denom
701 rtclock
.timebase_const
.numer
= 1000000000/20; // because ctimeRDTSC gives us 1/20 a seconds worth
702 rtclock
.timebase_const
.denom
= twen_cycles
; // so that nanoseconds = (TSC * numer) / denom
704 cycles
= twen_cycles
; // number of cycles in 1/20th a second
705 rtc_cyc_per_sec
= cycles
*20; // multiply it by 20 and we are done.. BUT we also want to calculate...
707 cycles
= ((rtc_cyc_per_sec
+ UI_CPUFREQ_ROUNDING_FACTOR
- 1) / UI_CPUFREQ_ROUNDING_FACTOR
) * UI_CPUFREQ_ROUNDING_FACTOR
;
708 gPEClockFrequencyInfo
.cpu_clock_rate_hz
= cycles
;
709 DISPLAYVALUE(rtc_set_cyc_per_sec
,rtc_cyc_per_sec
);
710 DISPLAYEXIT(rtc_set_cyc_per_sec
);
714 clock_get_system_microtime(
720 sysclk_gettime(&now
);
723 *microsecs
= now
.tv_nsec
/ NSEC_PER_USEC
;
727 clock_get_system_nanotime(
733 sysclk_gettime(&now
);
736 *nanosecs
= now
.tv_nsec
;
740 * Get clock device attributes.
744 clock_flavor_t flavor
,
745 clock_attr_t attr
, /* OUT */
746 mach_msg_type_number_t
*count
) /* IN/OUT */
751 return (KERN_FAILURE
);
754 case CLOCK_GET_TIME_RES
: /* >0 res */
757 *(clock_res_t
*) attr
= 1000;
760 #endif /* (NCPUS == 1) */
761 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
763 *(clock_res_t
*) attr
= rtclock
.intr_nsec
;
767 case CLOCK_ALARM_MAXRES
:
768 *(clock_res_t
*) attr
= RTC_MAXRES
;
771 case CLOCK_ALARM_MINRES
:
772 *(clock_res_t
*) attr
= RTC_MINRES
;
776 return (KERN_INVALID_VALUE
);
778 return (KERN_SUCCESS
);
782 * Set clock device attributes.
786 clock_flavor_t flavor
,
787 clock_attr_t attr
, /* IN */
788 mach_msg_type_number_t count
) /* IN */
793 clock_res_t new_ires
;
796 return (KERN_FAILURE
);
799 case CLOCK_GET_TIME_RES
:
800 case CLOCK_ALARM_MAXRES
:
801 case CLOCK_ALARM_MINRES
:
802 return (KERN_FAILURE
);
804 case CLOCK_ALARM_CURRES
:
805 new_ires
= *(clock_res_t
*) attr
;
808 * The new resolution must be within the predetermined
809 * range. If the desired resolution cannot be achieved
810 * to within 0.1%, an error is returned.
812 if (new_ires
< RTC_MAXRES
|| new_ires
> RTC_MINRES
)
813 return (KERN_INVALID_VALUE
);
814 freq
= (NSEC_PER_SEC
/ new_ires
);
815 adj
= (((clknum
% freq
) * new_ires
) / clknum
);
816 if (adj
> (new_ires
/ 1000))
817 return (KERN_INVALID_VALUE
);
819 * Record the new alarm resolution which will take effect
820 * on the next HZ aligned clock tick.
823 if ( freq
!= rtc_intr_freq
) {
824 rtclock
.new_ires
= new_ires
;
828 return (KERN_SUCCESS
);
831 return (KERN_INVALID_VALUE
);
836 * Set next alarm time for the clock device. This call
837 * always resets the time to deliver an alarm for the
842 mach_timespec_t
*alarm_time
)
847 rtclock
.alarm_time
= *alarm_time
;
848 RtcAlrm
= &rtclock
.alarm_time
;
853 * Configure the calendar clock.
862 * Initialize calendar clock.
871 * Get the current clock time.
875 mach_timespec_t
*cur_time
) /* OUT */
880 if (!rtclock
.calend_is_set
) {
882 return (KERN_FAILURE
);
885 (void) sysclk_gettime_internal(cur_time
);
886 ADD_MACH_TIMESPEC(cur_time
, &rtclock
.calend_offset
);
889 return (KERN_SUCCESS
);
893 clock_get_calendar_microtime(
899 calend_gettime(&now
);
902 *microsecs
= now
.tv_nsec
/ NSEC_PER_USEC
;
906 clock_get_calendar_nanotime(
912 calend_gettime(&now
);
915 *nanosecs
= now
.tv_nsec
;
919 clock_set_calendar_microtime(
923 mach_timespec_t new_time
, curr_time
;
927 (void) sysclk_gettime_internal(&curr_time
);
928 rtclock
.calend_offset
.tv_sec
= new_time
.tv_sec
= secs
;
929 rtclock
.calend_offset
.tv_nsec
= new_time
.tv_nsec
= microsecs
* NSEC_PER_USEC
;
930 SUB_MACH_TIMESPEC(&rtclock
.calend_offset
, &curr_time
);
931 rtclock
.calend_is_set
= TRUE
;
934 (void) bbc_settime(&new_time
);
936 host_notify_calendar_change();
940 * Get clock device attributes.
944 clock_flavor_t flavor
,
945 clock_attr_t attr
, /* OUT */
946 mach_msg_type_number_t
*count
) /* IN/OUT */
951 return (KERN_FAILURE
);
954 case CLOCK_GET_TIME_RES
: /* >0 res */
957 *(clock_res_t
*) attr
= 1000;
960 #else /* (NCPUS == 1) */
962 *(clock_res_t
*) attr
= rtclock
.intr_nsec
;
965 #endif /* (NCPUS == 1) */
967 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
968 case CLOCK_ALARM_MINRES
:
969 case CLOCK_ALARM_MAXRES
:
970 *(clock_res_t
*) attr
= 0;
974 return (KERN_INVALID_VALUE
);
976 return (KERN_SUCCESS
);
979 #define tickadj (40*NSEC_PER_USEC) /* "standard" skew, ns / tick */
980 #define bigadj (NSEC_PER_SEC) /* use 10x skew above bigadj ns */
983 clock_set_calendar_adjtime(
987 int64_t total
, ototal
;
988 uint32_t interval
= 0;
991 total
= (int64_t)*secs
* NSEC_PER_SEC
+ *microsecs
* NSEC_PER_USEC
;
994 ototal
= rtclock
.calend_adjtotal
;
997 int32_t delta
= tickadj
;
1006 if (total
< -bigadj
)
1013 rtclock
.calend_adjtotal
= total
;
1014 rtclock
.calend_adjdelta
= delta
;
1016 interval
= (NSEC_PER_SEC
/ HZ
);
1019 rtclock
.calend_adjdelta
= rtclock
.calend_adjtotal
= 0;
1024 *secs
= *microsecs
= 0;
1026 *secs
= ototal
/ NSEC_PER_SEC
;
1027 *microsecs
= ototal
% NSEC_PER_SEC
;
1034 clock_adjust_calendar(void)
1036 uint32_t interval
= 0;
1041 delta
= rtclock
.calend_adjdelta
;
1042 ADD_MACH_TIMESPEC_NSEC(&rtclock
.calend_offset
, delta
);
1044 rtclock
.calend_adjtotal
-= delta
;
1047 if (delta
> rtclock
.calend_adjtotal
)
1048 rtclock
.calend_adjdelta
= rtclock
.calend_adjtotal
;
1052 if (delta
< rtclock
.calend_adjtotal
)
1053 rtclock
.calend_adjdelta
= rtclock
.calend_adjtotal
;
1056 if (rtclock
.calend_adjdelta
!= 0)
1057 interval
= (NSEC_PER_SEC
/ HZ
);
1065 clock_initialize_calendar(void)
1067 mach_timespec_t bbc_time
, curr_time
;
1070 if (bbc_gettime(&bbc_time
) != KERN_SUCCESS
)
1074 if (!rtclock
.calend_is_set
) {
1075 (void) sysclk_gettime_internal(&curr_time
);
1076 rtclock
.calend_offset
= bbc_time
;
1077 SUB_MACH_TIMESPEC(&rtclock
.calend_offset
, &curr_time
);
1078 rtclock
.calend_is_set
= TRUE
;
1082 host_notify_calendar_change();
1086 clock_timebase_info(
1087 mach_timebase_info_t info
)
1092 if (rtclock
.timebase_const
.denom
== 0xFFFFFFFF) {
1093 info
->numer
= info
->denom
= rtc_quant_scale
;
1095 info
->numer
= info
->denom
= 1;
1101 clock_set_timer_deadline(
1107 rtclock
.timer_deadline
= deadline
;
1108 rtclock
.timer_is_set
= TRUE
;
1113 clock_set_timer_func(
1114 clock_timer_func_t func
)
1119 if (rtclock
.timer_expire
== NULL
)
1120 rtclock
.timer_expire
= func
;
1127 * Load the count register and start the clock.
1129 #define RTCLOCK_RESET() { \
1130 outb(PITCTL_PORT, PIT_C0|PIT_NDIVMODE|PIT_READMODE); \
1131 outb(PITCTR0_PORT, (clks_per_int & 0xff)); \
1132 outb(PITCTR0_PORT, (clks_per_int >> 8)); \
1136 * Reset the clock device. This causes the realtime clock
1137 * device to reload its mode and count value (frequency).
1138 * Note: the CPU should be calibrated
1139 * before starting the clock for the first time.
1148 mp_disable_preemption();
1149 if (cpu_number() != master_cpu
) {
1150 mp_enable_preemption();
1153 mp_enable_preemption();
1154 #endif /* NCPUS > 1 */
1161 * Real-time clock device interrupt. Called only on the
1162 * master processor. Updates the clock time and upcalls
1163 * into the higher level clock code to deliver alarms.
1166 rtclock_intr(struct i386_interrupt_state
*regs
)
1169 mach_timespec_t clock_time
;
1175 * Update clock time. Do the update so that the macro
1176 * MTS_TO_TS() for reading the mapped time works (e.g.
1177 * update in order: mtv_csec, mtv_time.tv_nsec, mtv_time.tv_sec).
1180 abstime
= rdtsctime_to_nanoseconds(); // get the time as of the TSC
1181 clock_time
= nanos_to_timespec(abstime
); // turn it into a timespec
1182 rtclock
.time
.tv_nsec
= clock_time
.tv_nsec
;
1183 rtclock
.time
.tv_sec
= clock_time
.tv_sec
;
1184 rtclock
.abstime
= abstime
;
1186 /* note time now up to date */
1190 * On a HZ-tick boundary: return 0 and adjust the clock
1191 * alarm resolution (if requested). Otherwise return a
1194 if ((i
= --rtc_intr_count
) == 0) {
1195 if (rtclock
.new_ires
) {
1196 rtc_setvals(new_clknum
, rtclock
.new_ires
);
1197 RTCLOCK_RESET(); /* lock clock register */
1198 rtclock
.new_ires
= 0;
1200 rtc_intr_count
= rtc_intr_hertz
;
1202 usermode
= (regs
->efl
& EFL_VM
) || ((regs
->cs
& 0x03) != 0);
1203 hertz_tick(usermode
, regs
->eip
);
1207 if ( rtclock
.timer_is_set
&&
1208 rtclock
.timer_deadline
<= abstime
) {
1209 rtclock
.timer_is_set
= FALSE
;
1212 (*rtclock
.timer_expire
)(abstime
);
1218 * Perform alarm clock processing if needed. The time
1219 * passed up is incremented by a half-interrupt tick
1220 * to trigger alarms closest to their desired times.
1221 * The clock_alarm_intr() routine calls sysclk_setalrm()
1222 * before returning if later alarms are pending.
1225 if (RtcAlrm
&& (RtcAlrm
->tv_sec
< RtcTime
->tv_sec
||
1226 (RtcAlrm
->tv_sec
== RtcTime
->tv_sec
&&
1227 RtcDelt
>= RtcAlrm
->tv_nsec
- RtcTime
->tv_nsec
))) {
1228 clock_time
.tv_sec
= 0;
1229 clock_time
.tv_nsec
= RtcDelt
;
1230 ADD_MACH_TIMESPEC (&clock_time
, RtcTime
);
1234 * Call clock_alarm_intr() without RTC-lock.
1235 * The lock ordering is always CLOCK-lock
1238 clock_alarm_intr(SYSTEM_CLOCK
, &clock_time
);
1250 *result
= rdtsctime_to_nanoseconds();
1254 mach_absolute_time(void)
1256 return rdtsctime_to_nanoseconds();
1260 clock_interval_to_deadline(
1262 uint32_t scale_factor
,
1267 clock_get_uptime(result
);
1269 clock_interval_to_absolutetime_interval(interval
, scale_factor
, &abstime
);
1275 clock_interval_to_absolutetime_interval(
1277 uint32_t scale_factor
,
1280 *result
= (uint64_t)interval
* scale_factor
;
1284 clock_absolutetime_interval_to_deadline(
1288 clock_get_uptime(result
);
1294 absolutetime_to_nanoseconds(
1302 nanoseconds_to_absolutetime(
1303 uint64_t nanoseconds
,
1306 *result
= nanoseconds
;
1310 * Spin-loop delay primitives.
1315 uint32_t scale_factor
)
1319 clock_interval_to_deadline(interval
, scale_factor
, &end
);
1323 now
= mach_absolute_time();
1324 } while (now
< end
);
1335 now
= mach_absolute_time();
1336 } while (now
< deadline
);
1343 delay_for_interval((usec
< 0)? -usec
: usec
, NSEC_PER_USEC
);