]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/etimer.c
xnu-792.18.15.tar.gz
[apple/xnu.git] / osfmk / kern / etimer.c
CommitLineData
89b3af67
A
1/*
2 * Copyright (c) 2000-2005 Apple Computer, 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
55#ifdef __ppc__
56#include <ppc/exception.h>
57#else
58#include <i386/cpu_data.h>
59#endif
60
61#include <sys/kdebug.h>
62
63
64/* XXX from <arch>/rtclock.c */
65uint32_t rtclock_tick_interval;
66clock_timer_func_t rtclock_timer_expire;
67
68#ifdef __ppc__
69# define PER_PROC_INFO struct per_proc_info
70# define GET_PER_PROC_INFO() getPerProc()
71#else
72# define PER_PROC_INFO cpu_data_t
73# define GET_PER_PROC_INFO() current_cpu_datap()
74#endif
75
76/*
77 * Event timer interrupt.
78 *
79 * XXX a drawback of this implementation is that events serviced earlier must not set deadlines
80 * that occur before the entire chain completes.
81 *
82 * XXX a better implementation would use a set of generic callouts and iterate over them
83 */
84void etimer_intr(int inuser, uint64_t iaddr) {
85
86 uint64_t abstime;
87 rtclock_timer_t *mytimer;
88 PER_PROC_INFO *pp;
89
90 pp = GET_PER_PROC_INFO();
91
92 mytimer = &pp->rtclock_timer; /* Point to the event timer */
93
94 abstime = mach_absolute_time(); /* Get the time now */
95
96 /* is it time for power management state change? */
97 if (pp->pms.pmsPop <= abstime) {
98
99 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_START, 0, 0, 0, 0, 0);
100 pmsStep(1); /* Yes, advance step */
101 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 3) | DBG_FUNC_END, 0, 0, 0, 0, 0);
102
103 abstime = mach_absolute_time(); /* Get the time again since we ran a bit */
104 }
105
106 /* have we passed the rtclock pop time? */
107 if (pp->rtclock_intr_deadline <= abstime) {
108
109 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 4) | DBG_FUNC_START, (int)rtclock_tick_interval, 0, 0, 0, 0);
110
111 clock_deadline_for_periodic_event(rtclock_tick_interval,
112 abstime,
113 &pp->rtclock_intr_deadline);
114
115 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 4) | DBG_FUNC_END, 0, 0, 0, 0, 0);
116#if STAT_TIME
117 hertz_tick(NSEC_PER_HZ, inuser, iaddr); /* Accumulate hertz */
118#else
119 hertz_tick(inuser, iaddr); /* Accumulate hertz */
120#endif
121
122 abstime = mach_absolute_time(); /* Refresh the current time since we went away */
123 }
124
125 /* has a pending clock timer expired? */
126 if (mytimer->deadline <= abstime) { /* Have we expired the deadline? */
127 mytimer->has_expired = TRUE; /* Remember that we popped */
128 mytimer->deadline = EndOfAllTime; /* Set timer request to the end of all time in case we have no more events */
129 (*rtclock_timer_expire)(abstime); /* Process pop */
130 mytimer->has_expired = FALSE;
131 }
132
133 /* schedule our next deadline */
134 pp->rtcPop = EndOfAllTime; /* any real deadline will be earlier */
135 etimer_resync_deadlines();
136}
137
138/*
139 * Set the clock deadline; called by the thread scheduler.
140 */
141void etimer_set_deadline(uint64_t deadline)
142{
143 rtclock_timer_t *mytimer;
144 spl_t s;
145 PER_PROC_INFO *pp;
146
147 s = splclock(); /* no interruptions */
148 pp = GET_PER_PROC_INFO();
149
150 mytimer = &pp->rtclock_timer; /* Point to the timer itself */
151 mytimer->deadline = deadline; /* Set the new expiration time */
152
153 etimer_resync_deadlines();
154
155 splx(s);
156}
157
158/*
159 * Re-evaluate the outstanding deadlines and select the most proximate.
160 *
161 * Should be called at splclock.
162 */
163void
164etimer_resync_deadlines(void)
165{
166 uint64_t deadline;
167 rtclock_timer_t *mytimer;
168 spl_t s = splclock(); /* No interruptions please */
169 PER_PROC_INFO *pp;
170
171 pp = GET_PER_PROC_INFO();
172
173 deadline = 0;
174
175 /* next rtclock interrupt? */
176 if (pp->rtclock_intr_deadline > 0)
177 deadline = pp->rtclock_intr_deadline;
178
179 /* if we have a clock timer set sooner, pop on that */
180 mytimer = &pp->rtclock_timer; /* Point to the timer itself */
181 if ((!mytimer->has_expired) && (mytimer->deadline > 0) && (mytimer->deadline < deadline))
182 deadline = mytimer->deadline;
183
184 /* if we have a power management event coming up, how about that? */
185 if ((pp->pms.pmsPop > 0) && (pp->pms.pmsPop < deadline))
186 deadline = pp->pms.pmsPop;
187
188#ifdef __ppc__
189#endif
190
191 if ((deadline > 0) && (deadline < pp->rtcPop)) {
192 int decr;
193
194 pp->rtcPop = deadline;
195 decr = setPop(deadline);
196
197 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 1) | DBG_FUNC_NONE, decr, 2, 0, 0, 0);
198 }
199 splx(s);
200}