1/* 2 * Copyright (c) 2000-2008 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/timer_queue.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 <ppc/exception.h> 56 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 */ 65void 66etimer_intr( 67__unused int inuser, 68__unused uint64_t iaddr) 69{ 70 uint64_t abstime; 71 rtclock_timer_t *mytimer; 72 struct per_proc_info *pp; 73 74 pp = getPerProc(); 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) { 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 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 */ 92 mytimer->deadline = timer_queue_expire(&mytimer->queue, abstime); 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/* 102 * Set the clock deadline. 103 */ 104void etimer_set_deadline(uint64_t deadline) 105{ 106 rtclock_timer_t *mytimer; 107 spl_t s; 108 struct per_proc_info *pp; 109 110 s = splclock(); /* no interruptions */ 111 pp = getPerProc(); 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 121 122/* 123 * Re-evaluate the outstanding deadlines and select the most proximate. 124 * 125 * Should be called at splclock. 126 */ 127void 128etimer_resync_deadlines(void) 129{ 130 uint64_t deadline; 131 rtclock_timer_t *mytimer; 132 spl_t s = splclock(); /* No interruptions please */ 133 struct per_proc_info *pp; 134 135 pp = getPerProc(); 136 137 deadline = ~0ULL; 138 139 /* if we have a clock timer set sooner, pop on that */ 140 mytimer = &pp->rtclock_timer; /* Point to the timer itself */ 141 if (!mytimer->has_expired && mytimer->deadline > 0) 142 deadline = mytimer->deadline; 143 144 /* if we have a power management event coming up, how about that? */ 145 if (pp->pms.pmsPop > 0 && pp->pms.pmsPop < deadline) 146 deadline = pp->pms.pmsPop; 147 148 149 if (deadline > 0 && deadline <= pp->rtcPop) { 150 int decr; 151 uint64_t now; 152 153 now = mach_absolute_time(); 154 decr = setPop(deadline); 155 156 if (deadline < now) 157 pp->rtcPop = now + decr; 158 else 159 pp->rtcPop = deadline; 160 161 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI, 1) | DBG_FUNC_NONE, decr, 2, 0, 0, 0); 162 } 163 splx(s); 164} 165 166queue_t 167timer_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 185void 186timer_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} 196