]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/etimer.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / i386 / etimer.c
CommitLineData
2d21ac55 1/*
060df5ea 2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
2d21ac55
A
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
c910b4d9 42#include <kern/timer_queue.h>
2d21ac55
A
43#include <kern/clock.h>
44#include <kern/thread.h>
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>
55#include <i386/cpu_data.h>
56#include <i386/cpu_topology.h>
57#include <i386/cpu_threads.h>
58
2d21ac55
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 */
67void
060df5ea
A
68etimer_intr(int user_mode,
69 uint64_t rip)
2d21ac55
A
70{
71 uint64_t abstime;
72 rtclock_timer_t *mytimer;
73 cpu_data_t *pp;
060df5ea
A
74 int32_t latency;
75 uint64_t pmdeadline;
2d21ac55
A
76
77 pp = current_cpu_datap();
2d21ac55 78
060df5ea 79 abstime = mach_absolute_time(); /* Get the time now */
2d21ac55
A
80
81 /* has a pending clock timer expired? */
060df5ea
A
82 mytimer = &pp->rtclock_timer;
83 if (mytimer->deadline <= abstime) {
84 /*
85 * Log interrupt service latency (-ve value expected by tool)
86 * a non-PM event is expected next.
87 */
88 latency = (int32_t) (abstime - mytimer->deadline);
89 KERNEL_DEBUG_CONSTANT(
90 MACHDBG_CODE(DBG_MACH_EXCP_DECI, 0) | DBG_FUNC_NONE,
91 -latency,
92 (uint32_t)rip, user_mode, 0, 0);
93
94 mytimer->has_expired = TRUE; /* Remember that we popped */
c910b4d9 95 mytimer->deadline = timer_queue_expire(&mytimer->queue, abstime);
2d21ac55 96 mytimer->has_expired = FALSE;
060df5ea
A
97
98 /* Get the time again since we ran for a bit */
99 abstime = mach_absolute_time();
100 }
101
102 /* is it time for power management state change? */
103 if ((pmdeadline = pmCPUGetDeadline(pp)) && (pmdeadline <= abstime)) {
104 KERNEL_DEBUG_CONSTANT(
105 MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_START,
106 0, 0, 0, 0, 0);
107 pmCPUDeadline(pp);
108 KERNEL_DEBUG_CONSTANT(
109 MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_END,
110 0, 0, 0, 0, 0);
2d21ac55
A
111 }
112
2d21ac55
A
113 etimer_resync_deadlines();
114}
115
116/*
c910b4d9 117 * Set the clock deadline.
2d21ac55
A
118 */
119void etimer_set_deadline(uint64_t deadline)
120{
121 rtclock_timer_t *mytimer;
122 spl_t s;
123 cpu_data_t *pp;
124
060df5ea 125 s = splclock(); /* no interruptions */
2d21ac55
A
126 pp = current_cpu_datap();
127
060df5ea
A
128 mytimer = &pp->rtclock_timer; /* Point to the timer itself */
129 mytimer->deadline = deadline; /* Set the new expiration time */
2d21ac55
A
130
131 etimer_resync_deadlines();
132
133 splx(s);
134}
135
136/*
137 * Re-evaluate the outstanding deadlines and select the most proximate.
138 *
139 * Should be called at splclock.
140 */
141void
142etimer_resync_deadlines(void)
143{
144 uint64_t deadline;
145 uint64_t pmdeadline;
146 rtclock_timer_t *mytimer;
147 spl_t s = splclock();
148 cpu_data_t *pp;
060df5ea 149 uint32_t decr;
2d21ac55
A
150
151 pp = current_cpu_datap();
060df5ea 152 deadline = EndOfAllTime;
2d21ac55
A
153
154 /*
060df5ea 155 * If we have a clock timer set, pick that.
2d21ac55
A
156 */
157 mytimer = &pp->rtclock_timer;
060df5ea
A
158 if (!mytimer->has_expired &&
159 0 < mytimer->deadline && mytimer->deadline < EndOfAllTime)
2d21ac55
A
160 deadline = mytimer->deadline;
161
162 /*
163 * If we have a power management deadline, see if that's earlier.
164 */
165 pmdeadline = pmCPUGetDeadline(pp);
060df5ea 166 if (0 < pmdeadline && pmdeadline < deadline)
2d21ac55
A
167 deadline = pmdeadline;
168
169 /*
170 * Go and set the "pop" event.
171 */
060df5ea
A
172 decr = (uint32_t) setPop(deadline);
173
174 /* Record non-PM deadline for latency tool */
175 if (deadline != pmdeadline) {
176 KERNEL_DEBUG_CONSTANT(
177 MACHDBG_CODE(DBG_MACH_EXCP_DECI, 1) | DBG_FUNC_NONE,
178 decr, 2,
179 deadline, (uint32_t)(deadline >> 32), 0);
2d21ac55
A
180 }
181 splx(s);
182}
c910b4d9
A
183
184void etimer_timer_expire(void *arg);
185
186void
187etimer_timer_expire(
188__unused void *arg)
189{
190 rtclock_timer_t *mytimer;
191 uint64_t abstime;
192 cpu_data_t *pp;
c910b4d9
A
193
194 pp = current_cpu_datap();
c910b4d9
A
195
196 mytimer = &pp->rtclock_timer;
197 abstime = mach_absolute_time();
198
199 mytimer->has_expired = TRUE;
200 mytimer->deadline = timer_queue_expire(&mytimer->queue, abstime);
201 mytimer->has_expired = FALSE;
202
c910b4d9
A
203 etimer_resync_deadlines();
204}
205
206queue_t
207timer_queue_assign(
208 uint64_t deadline)
209{
210 cpu_data_t *cdp = current_cpu_datap();
211 rtclock_timer_t *timer;
212
213 if (cdp->cpu_running) {
214 timer = &cdp->rtclock_timer;
215
216 if (deadline < timer->deadline)
217 etimer_set_deadline(deadline);
218 }
219 else
220 timer = &cpu_datap(master_cpu)->rtclock_timer;
221
222 return (&timer->queue);
223}
224
225void
226timer_queue_cancel(
227 queue_t queue,
228 uint64_t deadline,
229 uint64_t new_deadline)
230{
231 if (queue == &current_cpu_datap()->rtclock_timer.queue) {
232 if (deadline < new_deadline)
233 etimer_set_deadline(new_deadline);
234 }
235}