]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/etimer.c
25b913c9ca2d56298001ed3937cfe03093cd8035
[apple/xnu.git] / osfmk / kern / etimer.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * @APPLE_FREE_COPYRIGHT@
35 */
36 /*
37 * File: etimer.c
38 * Purpose: Routines for handling the machine independent
39 * event timer.
40 */
41
42 #include <mach/mach_types.h>
43
44 #include <kern/clock.h>
45 #include <kern/thread.h>
46 #include <kern/processor.h>
47 #include <kern/macro_help.h>
48 #include <kern/spl.h>
49 #include <kern/etimer.h>
50 #include <kern/pms.h>
51
52 #include <machine/commpage.h>
53 #include <machine/machine_routines.h>
54
55 #include <sys/kdebug.h>
56
57 #ifdef __ppc__
58 #include <ppc/exception.h>
59 #else
60 #include <i386/cpu_data.h>
61 #endif
62
63 #include <sys/kdebug.h>
64
65
66 /* XXX from <arch>/rtclock.c */
67 uint32_t rtclock_tick_interval;
68 clock_timer_func_t rtclock_timer_expire;
69
70 #ifdef __ppc__
71 # define PER_PROC_INFO struct per_proc_info
72 # define GET_PER_PROC_INFO() getPerProc()
73 #else
74 # define PER_PROC_INFO cpu_data_t
75 # define GET_PER_PROC_INFO() current_cpu_datap()
76 #endif
77
78 /*
79 * Event timer interrupt.
80 *
81 * XXX a drawback of this implementation is that events serviced earlier must not set deadlines
82 * that occur before the entire chain completes.
83 *
84 * XXX a better implementation would use a set of generic callouts and iterate over them
85 */
86 void etimer_intr(int inuser, uint64_t iaddr) {
87
88 uint64_t abstime;
89 rtclock_timer_t *mytimer;
90 PER_PROC_INFO *pp;
91
92 pp = GET_PER_PROC_INFO();
93
94 mytimer = &pp->rtclock_timer; /* Point to the event timer */
95
96 abstime = mach_absolute_time(); /* Get the time now */
97
98 /* is it time for power management state change? */
99 if (pp->pms.pmsPop <= abstime) {
100
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);
104
105 abstime = mach_absolute_time(); /* Get the time again since we ran a bit */
106 }
107
108 /* have we passed the rtclock pop time? */
109 if (pp->rtclock_intr_deadline <= abstime) {
110
111 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 4) | DBG_FUNC_START, (int)rtclock_tick_interval, 0, 0, 0, 0);
112
113 clock_deadline_for_periodic_event(rtclock_tick_interval,
114 abstime,
115 &pp->rtclock_intr_deadline);
116
117 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 4) | DBG_FUNC_END, 0, 0, 0, 0, 0);
118 #if STAT_TIME
119 hertz_tick(NSEC_PER_HZ, inuser, iaddr); /* Accumulate hertz */
120 #else
121 hertz_tick(inuser, iaddr); /* Accumulate hertz */
122 #endif
123
124 abstime = mach_absolute_time(); /* Refresh the current time since we went away */
125 }
126
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;
133 }
134
135 /* schedule our next deadline */
136 pp->rtcPop = EndOfAllTime; /* any real deadline will be earlier */
137 etimer_resync_deadlines();
138 }
139
140 /*
141 * Set the clock deadline; called by the thread scheduler.
142 */
143 void etimer_set_deadline(uint64_t deadline)
144 {
145 rtclock_timer_t *mytimer;
146 spl_t s;
147 PER_PROC_INFO *pp;
148
149 s = splclock(); /* no interruptions */
150 pp = GET_PER_PROC_INFO();
151
152 mytimer = &pp->rtclock_timer; /* Point to the timer itself */
153 mytimer->deadline = deadline; /* Set the new expiration time */
154
155 etimer_resync_deadlines();
156
157 splx(s);
158 }
159
160 /*
161 * Re-evaluate the outstanding deadlines and select the most proximate.
162 *
163 * Should be called at splclock.
164 */
165 void
166 etimer_resync_deadlines(void)
167 {
168 uint64_t deadline;
169 rtclock_timer_t *mytimer;
170 spl_t s = splclock(); /* No interruptions please */
171 PER_PROC_INFO *pp;
172
173 pp = GET_PER_PROC_INFO();
174
175 deadline = 0;
176
177 /* next rtclock interrupt? */
178 if (pp->rtclock_intr_deadline > 0)
179 deadline = pp->rtclock_intr_deadline;
180
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;
185
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;
189
190 #ifdef __ppc__
191 #endif
192
193 if ((deadline > 0) && (deadline < pp->rtcPop)) {
194 int decr;
195
196 pp->rtcPop = deadline;
197 decr = setPop(deadline);
198
199 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 1) | DBG_FUNC_NONE, decr, 2, 0, 0, 0);
200 }
201 splx(s);
202 }