]>
Commit | Line | Data |
---|---|---|
0c530ab8 | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 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> | |
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> | |
0c530ab8 | 54 | #include <ppc/exception.h> |
0c530ab8 A |
55 | |
56 | /* XXX from <arch>/rtclock.c */ | |
0c530ab8 A |
57 | clock_timer_func_t rtclock_timer_expire; |
58 | ||
0c530ab8 A |
59 | /* |
60 | * Event timer interrupt. | |
61 | * | |
62 | * XXX a drawback of this implementation is that events serviced earlier must not set deadlines | |
63 | * that occur before the entire chain completes. | |
64 | * | |
65 | * XXX a better implementation would use a set of generic callouts and iterate over them | |
66 | */ | |
2d21ac55 A |
67 | void |
68 | etimer_intr( | |
69 | __unused int inuser, | |
70 | __unused uint64_t iaddr) | |
71 | { | |
0c530ab8 A |
72 | uint64_t abstime; |
73 | rtclock_timer_t *mytimer; | |
2d21ac55 | 74 | struct per_proc_info *pp; |
0c530ab8 | 75 | |
2d21ac55 | 76 | pp = getPerProc(); |
0c530ab8 A |
77 | |
78 | mytimer = &pp->rtclock_timer; /* Point to the event timer */ | |
79 | ||
80 | abstime = mach_absolute_time(); /* Get the time now */ | |
81 | ||
82 | /* is it time for power management state change? */ | |
83 | if (pp->pms.pmsPop <= abstime) { | |
0c530ab8 A |
84 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_START, 0, 0, 0, 0, 0); |
85 | pmsStep(1); /* Yes, advance step */ | |
86 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_END, 0, 0, 0, 0, 0); | |
87 | ||
88 | abstime = mach_absolute_time(); /* Get the time again since we ran a bit */ | |
89 | } | |
90 | ||
0c530ab8 A |
91 | /* has a pending clock timer expired? */ |
92 | if (mytimer->deadline <= abstime) { /* Have we expired the deadline? */ | |
93 | mytimer->has_expired = TRUE; /* Remember that we popped */ | |
94 | mytimer->deadline = EndOfAllTime; /* Set timer request to the end of all time in case we have no more events */ | |
95 | (*rtclock_timer_expire)(abstime); /* Process pop */ | |
96 | mytimer->has_expired = FALSE; | |
97 | } | |
98 | ||
99 | /* schedule our next deadline */ | |
100 | pp->rtcPop = EndOfAllTime; /* any real deadline will be earlier */ | |
101 | etimer_resync_deadlines(); | |
102 | } | |
103 | ||
104 | /* | |
105 | * Set the clock deadline; called by the thread scheduler. | |
106 | */ | |
107 | void etimer_set_deadline(uint64_t deadline) | |
108 | { | |
109 | rtclock_timer_t *mytimer; | |
110 | spl_t s; | |
2d21ac55 | 111 | struct per_proc_info *pp; |
0c530ab8 A |
112 | |
113 | s = splclock(); /* no interruptions */ | |
2d21ac55 | 114 | pp = getPerProc(); |
0c530ab8 A |
115 | |
116 | mytimer = &pp->rtclock_timer; /* Point to the timer itself */ | |
117 | mytimer->deadline = deadline; /* Set the new expiration time */ | |
118 | ||
119 | etimer_resync_deadlines(); | |
120 | ||
121 | splx(s); | |
122 | } | |
123 | ||
2d21ac55 | 124 | |
0c530ab8 A |
125 | /* |
126 | * Re-evaluate the outstanding deadlines and select the most proximate. | |
127 | * | |
128 | * Should be called at splclock. | |
129 | */ | |
130 | void | |
131 | etimer_resync_deadlines(void) | |
132 | { | |
133 | uint64_t deadline; | |
134 | rtclock_timer_t *mytimer; | |
135 | spl_t s = splclock(); /* No interruptions please */ | |
2d21ac55 | 136 | struct per_proc_info *pp; |
0c530ab8 | 137 | |
2d21ac55 | 138 | pp = getPerProc(); |
0c530ab8 | 139 | |
2d21ac55 | 140 | deadline = ~0ULL; |
0c530ab8 A |
141 | |
142 | /* if we have a clock timer set sooner, pop on that */ | |
143 | mytimer = &pp->rtclock_timer; /* Point to the timer itself */ | |
2d21ac55 | 144 | if (!mytimer->has_expired && mytimer->deadline > 0) |
0c530ab8 A |
145 | deadline = mytimer->deadline; |
146 | ||
147 | /* if we have a power management event coming up, how about that? */ | |
2d21ac55 | 148 | if (pp->pms.pmsPop > 0 && pp->pms.pmsPop < deadline) |
0c530ab8 A |
149 | deadline = pp->pms.pmsPop; |
150 | ||
0c530ab8 | 151 | |
2d21ac55 | 152 | if (deadline > 0 && deadline <= pp->rtcPop) { |
0c530ab8 | 153 | int decr; |
2d21ac55 | 154 | uint64_t now; |
0c530ab8 | 155 | |
2d21ac55 | 156 | now = mach_absolute_time(); |
0c530ab8 A |
157 | decr = setPop(deadline); |
158 | ||
2d21ac55 A |
159 | if (deadline < now) |
160 | pp->rtcPop = now + decr; | |
161 | else | |
162 | pp->rtcPop = deadline; | |
163 | ||
0c530ab8 A |
164 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 1) | DBG_FUNC_NONE, decr, 2, 0, 0, 0); |
165 | } | |
166 | splx(s); | |
167 | } |