]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/etimer.c
xnu-1228.5.18.tar.gz
[apple/xnu.git] / osfmk / i386 / etimer.c
1 /*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * @APPLE_FREE_COPYRIGHT@
33 */
34 /*
35 * File: etimer.c
36 * Purpose: Routines for handling the machine independent
37 * event timer.
38 */
39
40 #include <mach/mach_types.h>
41
42 #include <kern/clock.h>
43 #include <kern/thread.h>
44 #include <kern/processor.h>
45 #include <kern/macro_help.h>
46 #include <kern/spl.h>
47 #include <kern/etimer.h>
48 #include <kern/pms.h>
49
50 #include <machine/commpage.h>
51 #include <machine/machine_routines.h>
52
53 #include <sys/kdebug.h>
54 #include <i386/cpu_data.h>
55 #include <i386/cpu_topology.h>
56 #include <i386/cpu_threads.h>
57
58 /* XXX from <arch>/rtclock.c */
59 clock_timer_func_t rtclock_timer_expire;
60
61 /*
62 * Event timer interrupt.
63 *
64 * XXX a drawback of this implementation is that events serviced earlier must not set deadlines
65 * that occur before the entire chain completes.
66 *
67 * XXX a better implementation would use a set of generic callouts and iterate over them
68 */
69 void
70 etimer_intr(
71 __unused int inuser,
72 __unused uint64_t iaddr)
73 {
74 uint64_t abstime;
75 rtclock_timer_t *mytimer;
76 cpu_data_t *pp;
77 x86_lcpu_t *lcpu;
78
79 pp = current_cpu_datap();
80 lcpu = x86_lcpu();
81
82 mytimer = &pp->rtclock_timer; /* Point to the event timer */
83 abstime = mach_absolute_time(); /* Get the time now */
84
85 /* is it time for power management state change? */
86 if (pmCPUGetDeadline(pp) <= abstime) {
87 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_START, 0, 0, 0, 0, 0);
88 pmCPUDeadline(pp);
89 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_END, 0, 0, 0, 0, 0);
90
91 abstime = mach_absolute_time(); /* Get the time again since we ran a bit */
92 }
93
94 /* has a pending clock timer expired? */
95 if (mytimer->deadline <= abstime) { /* Have we expired the deadline? */
96 mytimer->has_expired = TRUE; /* Remember that we popped */
97 mytimer->deadline = EndOfAllTime; /* Set timer request to the end of all time in case we have no more events */
98 (*rtclock_timer_expire)(abstime); /* Process pop */
99 mytimer->has_expired = FALSE;
100 }
101
102 /* schedule our next deadline */
103 lcpu->rtcPop = EndOfAllTime; /* any real deadline will be earlier */
104 etimer_resync_deadlines();
105 }
106
107 /*
108 * Set the clock deadline; called by the thread scheduler.
109 */
110 void etimer_set_deadline(uint64_t deadline)
111 {
112 rtclock_timer_t *mytimer;
113 spl_t s;
114 cpu_data_t *pp;
115
116 s = splclock(); /* no interruptions */
117 pp = current_cpu_datap();
118
119 mytimer = &pp->rtclock_timer; /* Point to the timer itself */
120 mytimer->deadline = deadline; /* Set the new expiration time */
121
122 etimer_resync_deadlines();
123
124 splx(s);
125 }
126
127 /*
128 * Re-evaluate the outstanding deadlines and select the most proximate.
129 *
130 * Should be called at splclock.
131 */
132 void
133 etimer_resync_deadlines(void)
134 {
135 uint64_t deadline;
136 uint64_t pmdeadline;
137 rtclock_timer_t *mytimer;
138 spl_t s = splclock();
139 cpu_data_t *pp;
140 x86_lcpu_t *lcpu;
141
142 pp = current_cpu_datap();
143 lcpu = x86_lcpu();
144 deadline = ~0ULL;
145
146 /*
147 * If we have a clock timer set sooner, pop on that.
148 */
149 mytimer = &pp->rtclock_timer;
150 if (!mytimer->has_expired && mytimer->deadline > 0)
151 deadline = mytimer->deadline;
152
153 /*
154 * If we have a power management deadline, see if that's earlier.
155 */
156 pmdeadline = pmCPUGetDeadline(pp);
157 if (pmdeadline > 0 && pmdeadline < deadline)
158 deadline = pmdeadline;
159
160 /*
161 * Go and set the "pop" event.
162 */
163 if (deadline > 0) {
164 int decr;
165 uint64_t now;
166
167 now = mach_absolute_time();
168 decr = setPop(deadline);
169
170 if (deadline < now)
171 lcpu->rtcPop = now + decr;
172 else
173 lcpu->rtcPop = deadline;
174
175 lcpu->rtcDeadline = lcpu->rtcPop;
176
177 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 1) | DBG_FUNC_NONE, decr, 2, 0, 0, 0);
178 }
179 splx(s);
180 }