1/*
2 * Copyright (c) 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/timer_queue.h>
48#include <kern/timer_call.h>
49
50#include <machine/commpage.h>
51#include <machine/machine_routines.h>
52
53#include <sys/kdebug.h>
54#include <arm/cpu_data.h>
55#include <arm/cpu_data_internal.h>
56#include <arm/cpu_internal.h>
57
58/*
59 * Event timer interrupt.
60 *
61 * XXX a drawback of this implementation is that events serviced earlier must not set deadlines
62 * that occur before the entire chain completes.
63 *
64 * XXX a better implementation would use a set of generic callouts and iterate over them
65 */
66void
67timer_intr(__unused int inuser, __unused uint64_t iaddr)
68{
69 uint64_t abstime, new_idle_timeout_ticks;
70 rtclock_timer_t *mytimer;
71 cpu_data_t *cpu_data_ptr;
72 processor_t processor;
73
74 cpu_data_ptr = getCpuDatap();
75 mytimer = &cpu_data_ptr->rtclock_timer; /* Point to the event timer */
76 abstime = mach_absolute_time(); /* Get the time now */
77
78 /* is it time for an idle timer event? */
79 if ((cpu_data_ptr->idle_timer_deadline > 0) && (cpu_data_ptr->idle_timer_deadline <= abstime)) {
80 cpu_data_ptr->idle_timer_deadline = 0x0ULL;
81 new_idle_timeout_ticks = 0x0ULL;
82
83 KDBG_RELEASE(DECR_PM_DEADLINE | DBG_FUNC_START);
84 cpu_data_ptr->idle_timer_notify(cpu_data_ptr->idle_timer_refcon, &new_idle_timeout_ticks);
85 KDBG_RELEASE(DECR_PM_DEADLINE | DBG_FUNC_END);
86
87 /* if a new idle timeout was requested set the new idle timer deadline */
88 if (new_idle_timeout_ticks != 0x0ULL) {
89 clock_absolutetime_interval_to_deadline(abstime: new_idle_timeout_ticks, result: &cpu_data_ptr->idle_timer_deadline);
90 }
91
92 abstime = mach_absolute_time(); /* Get the time again since we ran a bit */
93 }
94
95 /* has a pending clock timer expired? */
96 if (mytimer->deadline <= abstime) { /* Have we expired the
97 * deadline? */
98 mytimer->has_expired = TRUE; /* Remember that we popped */
99 mytimer->deadline = EndOfAllTime; /* Set timer request to
100 * the end of all time
101 * in case we have no
102 * more events */
103 mytimer->deadline = timer_queue_expire(queue: &mytimer->queue, deadline: abstime);
104 mytimer->has_expired = FALSE;
105 abstime = mach_absolute_time(); /* Get the time again since we ran a bit */
106 }
107
108 processor = PERCPU_GET_RELATIVE(processor, cpu_data, cpu_data_ptr);
109 (void)running_timers_expire(processor, now: abstime);
110 /*
111 * No need to update abstime.
112 */
113
114 /* Force reload our next deadline */
115 cpu_data_ptr->rtcPop = EndOfAllTime;
116 /* schedule our next deadline */
117 timer_resync_deadlines();
118}
119
120/*
121 * Set the clock deadline
122 */
123void
124timer_set_deadline(uint64_t deadline)
125{
126 rtclock_timer_t *mytimer;
127 spl_t s;
128 cpu_data_t *cpu_data_ptr;
129
130 s = splclock(); /* no interruptions */
131 cpu_data_ptr = getCpuDatap();
132
133 mytimer = &cpu_data_ptr->rtclock_timer; /* Point to the timer itself */
134 mytimer->deadline = deadline; /* Set the new expiration time */
135
136 timer_resync_deadlines();
137
138 splx(s);
139}
140
141/*
142 * Re-evaluate the outstanding deadlines and select the most proximate.
143 *
144 * Should be called at splclock.
145 */
146void
147timer_resync_deadlines(void)
148{
149 uint64_t deadline;
150 rtclock_timer_t *mytimer;
151 spl_t s = splclock(); /* No interruptions please */
152 cpu_data_t *cpu_data_ptr;
153
154 cpu_data_ptr = getCpuDatap();
155
156 deadline = 0;
157
158 /* if we have a clock timer set sooner, pop on that */
159 mytimer = &cpu_data_ptr->rtclock_timer; /* Point to the timer itself */
160 if ((!mytimer->has_expired) && (mytimer->deadline > 0)) {
161 deadline = mytimer->deadline;
162 }
163
164 /* if we have a idle timer event coming up, how about that? */
165 if ((cpu_data_ptr->idle_timer_deadline > 0)
166 && (cpu_data_ptr->idle_timer_deadline < deadline)) {
167 deadline = cpu_data_ptr->idle_timer_deadline;
168 }
169
170 uint64_t run_deadline = running_timers_deadline(
171 PERCPU_GET_RELATIVE(processor, cpu_data, cpu_data_ptr));
172 if (run_deadline < deadline) {
173 deadline = run_deadline;
174 }
175
176 if ((deadline == EndOfAllTime)
177 || ((deadline > 0) && (cpu_data_ptr->rtcPop != deadline))) {
178 int decr;
179
180 decr = setPop(deadline);
181
182 KDBG_RELEASE(DECR_SET_DEADLINE | DBG_FUNC_NONE, decr, 2);
183 }
184 splx(s);
185}
186
187void
188timer_queue_expire_local(
189 __unused void *arg)
190{
191 rtclock_timer_t *mytimer = &getCpuDatap()->rtclock_timer;
192 uint64_t abstime;
193
194 abstime = mach_absolute_time();
195 mytimer->has_expired = TRUE;
196 mytimer->deadline = timer_queue_expire(queue: &mytimer->queue, deadline: abstime);
197 mytimer->has_expired = FALSE;
198
199 timer_resync_deadlines();
200}
201
202boolean_t
203timer_resort_threshold(__unused uint64_t skew)
204{
205 return FALSE;
206}
207
208mpqueue_head_t *
209timer_queue_assign(
210 uint64_t deadline)
211{
212 cpu_data_t *cpu_data_ptr = getCpuDatap();
213 mpqueue_head_t *queue;
214
215 if (cpu_data_ptr->cpu_running) {
216 queue = &cpu_data_ptr->rtclock_timer.queue;
217
218 if (deadline < cpu_data_ptr->rtclock_timer.deadline) {
219 timer_set_deadline(deadline);
220 }
221 } else {
222 queue = &cpu_datap(cpu: master_cpu)->rtclock_timer.queue;
223 }
224
225 return queue;
226}
227
228void
229timer_queue_cancel(
230 mpqueue_head_t *queue,
231 uint64_t deadline,
232 uint64_t new_deadline)
233{
234 if (queue == &getCpuDatap()->rtclock_timer.queue) {
235 if (deadline < new_deadline) {
236 timer_set_deadline(deadline: new_deadline);
237 }
238 }
239}
240
241mpqueue_head_t *
242timer_queue_cpu(int cpu)
243{
244 return &cpu_datap(cpu)->rtclock_timer.queue;
245}
246
247void
248timer_call_cpu(int cpu, void (*fn)(void *), void *arg)
249{
250 cpu_signal(target: cpu_datap(cpu), SIGPxcall, p0: (void *) fn, p1: arg);
251}
252
253void
254timer_call_nosync_cpu(int cpu, void (*fn)(void *), void *arg)
255{
256 /* XXX Needs error checking and retry */
257 cpu_signal(target: cpu_datap(cpu), SIGPxcall, p0: (void *) fn, p1: arg);
258}
259
260
261static timer_coalescing_priority_params_ns_t tcoal_prio_params_init =
262{
263 .idle_entry_timer_processing_hdeadline_threshold_ns = 5000ULL * NSEC_PER_USEC,
264 .interrupt_timer_coalescing_ilat_threshold_ns = 30ULL * NSEC_PER_USEC,
265 .timer_resort_threshold_ns = 50 * NSEC_PER_MSEC,
266 .timer_coalesce_rt_shift = 0,
267 .timer_coalesce_bg_shift = -5,
268 .timer_coalesce_kt_shift = 3,
269 .timer_coalesce_fp_shift = 3,
270 .timer_coalesce_ts_shift = 3,
271 .timer_coalesce_rt_ns_max = 0ULL,
272 .timer_coalesce_bg_ns_max = 100 * NSEC_PER_MSEC,
273 .timer_coalesce_kt_ns_max = 1 * NSEC_PER_MSEC,
274 .timer_coalesce_fp_ns_max = 1 * NSEC_PER_MSEC,
275 .timer_coalesce_ts_ns_max = 1 * NSEC_PER_MSEC,
276#if XNU_TARGET_OS_OSX
277 .latency_qos_scale = {3, 2, 1, -2, 3, 3},
278 .latency_qos_ns_max = {1 * NSEC_PER_MSEC, 5 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC,
279 75 * NSEC_PER_MSEC, 1 * NSEC_PER_MSEC, 1 * NSEC_PER_MSEC},
280 .latency_tier_rate_limited = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE},
281#else /* XNU_TARGET_OS_OSX */
282 .latency_qos_scale = {3, 2, 1, -2, -15, -15},
283 .latency_qos_ns_max = {1 * NSEC_PER_MSEC, 5 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC,
284 75 * NSEC_PER_MSEC, 10000 * NSEC_PER_MSEC, 10000 * NSEC_PER_MSEC},
285 .latency_tier_rate_limited = {FALSE, FALSE, FALSE, FALSE, TRUE, TRUE},
286#endif /* XNU_TARGET_OS_OSX */
287};
288timer_coalescing_priority_params_ns_t *
289timer_call_get_priority_params(void)
290{
291 return &tcoal_prio_params_init;
292}
293