]>
Commit | Line | Data |
---|---|---|
0c530ab8 | 1 | /* |
c910b4d9 | 2 | * Copyright (c) 2000-2008 Apple Inc. All rights reserved. |
0c530ab8 | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0c530ab8 | 5 | * |
2d21ac55 A |
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. | |
0c530ab8 | 14 | * |
2d21ac55 A |
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 | |
0c530ab8 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0c530ab8 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
0c530ab8 A |
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> | |
c910b4d9 | 44 | #include <kern/timer_queue.h> |
0c530ab8 A |
45 | #include <kern/processor.h> |
46 | #include <kern/macro_help.h> | |
47 | #include <kern/spl.h> | |
48 | #include <kern/etimer.h> | |
49 | #include <kern/pms.h> | |
50 | ||
51 | #include <machine/commpage.h> | |
52 | #include <machine/machine_routines.h> | |
53 | ||
54 | #include <sys/kdebug.h> | |
0c530ab8 | 55 | #include <ppc/exception.h> |
0c530ab8 | 56 | |
0c530ab8 A |
57 | /* |
58 | * Event timer interrupt. | |
59 | * | |
60 | * XXX a drawback of this implementation is that events serviced earlier must not set deadlines | |
61 | * that occur before the entire chain completes. | |
62 | * | |
63 | * XXX a better implementation would use a set of generic callouts and iterate over them | |
64 | */ | |
2d21ac55 A |
65 | void |
66 | etimer_intr( | |
67 | __unused int inuser, | |
68 | __unused uint64_t iaddr) | |
69 | { | |
0c530ab8 A |
70 | uint64_t abstime; |
71 | rtclock_timer_t *mytimer; | |
2d21ac55 | 72 | struct per_proc_info *pp; |
0c530ab8 | 73 | |
2d21ac55 | 74 | pp = getPerProc(); |
0c530ab8 A |
75 | |
76 | mytimer = &pp->rtclock_timer; /* Point to the event timer */ | |
77 | ||
78 | abstime = mach_absolute_time(); /* Get the time now */ | |
79 | ||
80 | /* is it time for power management state change? */ | |
81 | if (pp->pms.pmsPop <= abstime) { | |
0c530ab8 A |
82 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_START, 0, 0, 0, 0, 0); |
83 | pmsStep(1); /* Yes, advance step */ | |
84 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_END, 0, 0, 0, 0, 0); | |
85 | ||
86 | abstime = mach_absolute_time(); /* Get the time again since we ran a bit */ | |
87 | } | |
88 | ||
0c530ab8 A |
89 | /* has a pending clock timer expired? */ |
90 | if (mytimer->deadline <= abstime) { /* Have we expired the deadline? */ | |
91 | mytimer->has_expired = TRUE; /* Remember that we popped */ | |
c910b4d9 | 92 | mytimer->deadline = timer_queue_expire(&mytimer->queue, abstime); |
0c530ab8 A |
93 | mytimer->has_expired = FALSE; |
94 | } | |
95 | ||
96 | /* schedule our next deadline */ | |
97 | pp->rtcPop = EndOfAllTime; /* any real deadline will be earlier */ | |
98 | etimer_resync_deadlines(); | |
99 | } | |
100 | ||
101 | /* | |
c910b4d9 | 102 | * Set the clock deadline. |
0c530ab8 A |
103 | */ |
104 | void etimer_set_deadline(uint64_t deadline) | |
105 | { | |
106 | rtclock_timer_t *mytimer; | |
107 | spl_t s; | |
2d21ac55 | 108 | struct per_proc_info *pp; |
0c530ab8 A |
109 | |
110 | s = splclock(); /* no interruptions */ | |
2d21ac55 | 111 | pp = getPerProc(); |
0c530ab8 A |
112 | |
113 | mytimer = &pp->rtclock_timer; /* Point to the timer itself */ | |
114 | mytimer->deadline = deadline; /* Set the new expiration time */ | |
115 | ||
116 | etimer_resync_deadlines(); | |
117 | ||
118 | splx(s); | |
119 | } | |
120 | ||
2d21ac55 | 121 | |
0c530ab8 A |
122 | /* |
123 | * Re-evaluate the outstanding deadlines and select the most proximate. | |
124 | * | |
125 | * Should be called at splclock. | |
126 | */ | |
127 | void | |
128 | etimer_resync_deadlines(void) | |
129 | { | |
130 | uint64_t deadline; | |
131 | rtclock_timer_t *mytimer; | |
132 | spl_t s = splclock(); /* No interruptions please */ | |
2d21ac55 | 133 | struct per_proc_info *pp; |
0c530ab8 | 134 | |
2d21ac55 | 135 | pp = getPerProc(); |
0c530ab8 | 136 | |
2d21ac55 | 137 | deadline = ~0ULL; |
0c530ab8 A |
138 | |
139 | /* if we have a clock timer set sooner, pop on that */ | |
140 | mytimer = &pp->rtclock_timer; /* Point to the timer itself */ | |
2d21ac55 | 141 | if (!mytimer->has_expired && mytimer->deadline > 0) |
0c530ab8 A |
142 | deadline = mytimer->deadline; |
143 | ||
144 | /* if we have a power management event coming up, how about that? */ | |
2d21ac55 | 145 | if (pp->pms.pmsPop > 0 && pp->pms.pmsPop < deadline) |
0c530ab8 A |
146 | deadline = pp->pms.pmsPop; |
147 | ||
0c530ab8 | 148 | |
2d21ac55 | 149 | if (deadline > 0 && deadline <= pp->rtcPop) { |
0c530ab8 | 150 | int decr; |
2d21ac55 | 151 | uint64_t now; |
0c530ab8 | 152 | |
2d21ac55 | 153 | now = mach_absolute_time(); |
0c530ab8 A |
154 | decr = setPop(deadline); |
155 | ||
2d21ac55 A |
156 | if (deadline < now) |
157 | pp->rtcPop = now + decr; | |
158 | else | |
159 | pp->rtcPop = deadline; | |
160 | ||
0c530ab8 A |
161 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 1) | DBG_FUNC_NONE, decr, 2, 0, 0, 0); |
162 | } | |
163 | splx(s); | |
164 | } | |
c910b4d9 A |
165 | |
166 | queue_t | |
167 | timer_queue_assign( | |
168 | uint64_t deadline) | |
169 | { | |
170 | struct per_proc_info *pp = getPerProc(); | |
171 | rtclock_timer_t *timer; | |
172 | ||
173 | if (pp->running) { | |
174 | timer = &pp->rtclock_timer; | |
175 | ||
176 | if (deadline < timer->deadline) | |
177 | etimer_set_deadline(deadline); | |
178 | } | |
179 | else | |
180 | timer = &PerProcTable[master_cpu].ppe_vaddr->rtclock_timer; | |
181 | ||
182 | return (&timer->queue); | |
183 | } | |
184 | ||
185 | void | |
186 | timer_queue_cancel( | |
187 | queue_t queue, | |
188 | uint64_t deadline, | |
189 | uint64_t new_deadline) | |
190 | { | |
191 | if (queue == &getPerProc()->rtclock_timer.queue) { | |
192 | if (deadline < new_deadline) | |
193 | etimer_set_deadline(new_deadline); | |
194 | } | |
195 | } |