2 * Copyright (c) 2000-2005 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@
26 * @APPLE_FREE_COPYRIGHT@
30 * Purpose: Routines for handling the machine independent
34 #include <mach/mach_types.h>
36 #include <kern/clock.h>
37 #include <kern/thread.h>
38 #include <kern/processor.h>
39 #include <kern/macro_help.h>
41 #include <kern/etimer.h>
44 #include <machine/commpage.h>
45 #include <machine/machine_routines.h>
47 #include <sys/kdebug.h>
50 #include <ppc/exception.h>
52 #include <i386/cpu_data.h>
55 #include <sys/kdebug.h>
58 /* XXX from <arch>/rtclock.c */
59 uint32_t rtclock_tick_interval
;
60 clock_timer_func_t rtclock_timer_expire
;
63 # define PER_PROC_INFO struct per_proc_info
64 # define GET_PER_PROC_INFO() getPerProc()
66 # define PER_PROC_INFO cpu_data_t
67 # define GET_PER_PROC_INFO() current_cpu_datap()
71 * Event timer interrupt.
73 * XXX a drawback of this implementation is that events serviced earlier must not set deadlines
74 * that occur before the entire chain completes.
76 * XXX a better implementation would use a set of generic callouts and iterate over them
78 void etimer_intr(int inuser
, uint64_t iaddr
) {
81 rtclock_timer_t
*mytimer
;
84 pp
= GET_PER_PROC_INFO();
86 mytimer
= &pp
->rtclock_timer
; /* Point to the event timer */
88 abstime
= mach_absolute_time(); /* Get the time now */
90 /* is it time for power management state change? */
91 if (pp
->pms
.pmsPop
<= abstime
) {
93 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 3) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
94 pmsStep(1); /* Yes, advance step */
95 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 3) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
97 abstime
= mach_absolute_time(); /* Get the time again since we ran a bit */
100 /* have we passed the rtclock pop time? */
101 if (pp
->rtclock_intr_deadline
<= abstime
) {
103 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 4) | DBG_FUNC_START
, (int)rtclock_tick_interval
, 0, 0, 0, 0);
105 clock_deadline_for_periodic_event(rtclock_tick_interval
,
107 &pp
->rtclock_intr_deadline
);
109 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 4) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
111 hertz_tick(NSEC_PER_HZ
, inuser
, iaddr
); /* Accumulate hertz */
113 hertz_tick(inuser
, iaddr
); /* Accumulate hertz */
116 abstime
= mach_absolute_time(); /* Refresh the current time since we went away */
119 /* has a pending clock timer expired? */
120 if (mytimer
->deadline
<= abstime
) { /* Have we expired the deadline? */
121 mytimer
->has_expired
= TRUE
; /* Remember that we popped */
122 mytimer
->deadline
= EndOfAllTime
; /* Set timer request to the end of all time in case we have no more events */
123 (*rtclock_timer_expire
)(abstime
); /* Process pop */
124 mytimer
->has_expired
= FALSE
;
127 /* schedule our next deadline */
128 pp
->rtcPop
= EndOfAllTime
; /* any real deadline will be earlier */
129 etimer_resync_deadlines();
133 * Set the clock deadline; called by the thread scheduler.
135 void etimer_set_deadline(uint64_t deadline
)
137 rtclock_timer_t
*mytimer
;
141 s
= splclock(); /* no interruptions */
142 pp
= GET_PER_PROC_INFO();
144 mytimer
= &pp
->rtclock_timer
; /* Point to the timer itself */
145 mytimer
->deadline
= deadline
; /* Set the new expiration time */
147 etimer_resync_deadlines();
153 * Re-evaluate the outstanding deadlines and select the most proximate.
155 * Should be called at splclock.
158 etimer_resync_deadlines(void)
161 rtclock_timer_t
*mytimer
;
162 spl_t s
= splclock(); /* No interruptions please */
165 pp
= GET_PER_PROC_INFO();
169 /* next rtclock interrupt? */
170 if (pp
->rtclock_intr_deadline
> 0)
171 deadline
= pp
->rtclock_intr_deadline
;
173 /* if we have a clock timer set sooner, pop on that */
174 mytimer
= &pp
->rtclock_timer
; /* Point to the timer itself */
175 if ((!mytimer
->has_expired
) && (mytimer
->deadline
> 0) && (mytimer
->deadline
< deadline
))
176 deadline
= mytimer
->deadline
;
178 /* if we have a power management event coming up, how about that? */
179 if ((pp
->pms
.pmsPop
> 0) && (pp
->pms
.pmsPop
< deadline
))
180 deadline
= pp
->pms
.pmsPop
;
185 if ((deadline
> 0) && (deadline
< pp
->rtcPop
)) {
188 pp
->rtcPop
= deadline
;
189 decr
= setPop(deadline
);
191 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI
, 1) | DBG_FUNC_NONE
, decr
, 2, 0, 0, 0);