2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * @APPLE_FREE_COPYRIGHT@
38 * Purpose: Routines for handling the machine independent
42 #include <mach/mach_types.h>
44 #include <kern/clock.h>
45 #include <kern/thread.h>
46 #include <kern/processor.h>
47 #include <kern/macro_help.h>
49 #include <kern/etimer.h>
52 #include <machine/commpage.h>
53 #include <machine/machine_routines.h>
55 #include <sys/kdebug.h>
58 #include <ppc/exception.h>
60 #include <i386/cpu_data.h>
63 #include <sys/kdebug.h>
66 /* XXX from <arch>/rtclock.c */
67 uint32_t rtclock_tick_interval
;
68 clock_timer_func_t rtclock_timer_expire
;
71 # define PER_PROC_INFO struct per_proc_info
72 # define GET_PER_PROC_INFO() getPerProc()
74 # define PER_PROC_INFO cpu_data_t
75 # define GET_PER_PROC_INFO() current_cpu_datap()
79 * Event timer interrupt.
81 * XXX a drawback of this implementation is that events serviced earlier must not set deadlines
82 * that occur before the entire chain completes.
84 * XXX a better implementation would use a set of generic callouts and iterate over them
86 void etimer_intr(int inuser
, uint64_t iaddr
) {
89 rtclock_timer_t
*mytimer
;
92 pp
= GET_PER_PROC_INFO();
94 mytimer
= &pp
->rtclock_timer
; /* Point to the event timer */
96 abstime
= mach_absolute_time(); /* Get the time now */
98 /* is it time for power management state change? */
99 if (pp
->pms
.pmsPop
<= abstime
) {
101 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 3) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
102 pmsStep(1); /* Yes, advance step */
103 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 3) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
105 abstime
= mach_absolute_time(); /* Get the time again since we ran a bit */
108 /* have we passed the rtclock pop time? */
109 if (pp
->rtclock_intr_deadline
<= abstime
) {
111 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 4) | DBG_FUNC_START
, (int)rtclock_tick_interval
, 0, 0, 0, 0);
113 clock_deadline_for_periodic_event(rtclock_tick_interval
,
115 &pp
->rtclock_intr_deadline
);
117 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 4) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
119 hertz_tick(NSEC_PER_HZ
, inuser
, iaddr
); /* Accumulate hertz */
121 hertz_tick(inuser
, iaddr
); /* Accumulate hertz */
124 abstime
= mach_absolute_time(); /* Refresh the current time since we went away */
127 /* has a pending clock timer expired? */
128 if (mytimer
->deadline
<= abstime
) { /* Have we expired the deadline? */
129 mytimer
->has_expired
= TRUE
; /* Remember that we popped */
130 mytimer
->deadline
= EndOfAllTime
; /* Set timer request to the end of all time in case we have no more events */
131 (*rtclock_timer_expire
)(abstime
); /* Process pop */
132 mytimer
->has_expired
= FALSE
;
135 /* schedule our next deadline */
136 pp
->rtcPop
= EndOfAllTime
; /* any real deadline will be earlier */
137 etimer_resync_deadlines();
141 * Set the clock deadline; called by the thread scheduler.
143 void etimer_set_deadline(uint64_t deadline
)
145 rtclock_timer_t
*mytimer
;
149 s
= splclock(); /* no interruptions */
150 pp
= GET_PER_PROC_INFO();
152 mytimer
= &pp
->rtclock_timer
; /* Point to the timer itself */
153 mytimer
->deadline
= deadline
; /* Set the new expiration time */
155 etimer_resync_deadlines();
161 * Re-evaluate the outstanding deadlines and select the most proximate.
163 * Should be called at splclock.
166 etimer_resync_deadlines(void)
169 rtclock_timer_t
*mytimer
;
170 spl_t s
= splclock(); /* No interruptions please */
173 pp
= GET_PER_PROC_INFO();
177 /* next rtclock interrupt? */
178 if (pp
->rtclock_intr_deadline
> 0)
179 deadline
= pp
->rtclock_intr_deadline
;
181 /* if we have a clock timer set sooner, pop on that */
182 mytimer
= &pp
->rtclock_timer
; /* Point to the timer itself */
183 if ((!mytimer
->has_expired
) && (mytimer
->deadline
> 0) && (mytimer
->deadline
< deadline
))
184 deadline
= mytimer
->deadline
;
186 /* if we have a power management event coming up, how about that? */
187 if ((pp
->pms
.pmsPop
> 0) && (pp
->pms
.pmsPop
< deadline
))
188 deadline
= pp
->pms
.pmsPop
;
193 if ((deadline
> 0) && (deadline
< pp
->rtcPop
)) {
196 pp
->rtcPop
= deadline
;
197 decr
= setPop(deadline
);
199 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 1) | DBG_FUNC_NONE
, decr
, 2, 0, 0, 0);