2 * Copyright (c) 2004-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * CPU-specific power management support.
32 * Implements the "wrappers" to the KEXT.
34 #include <kern/machine.h>
35 #include <i386/machine_routines.h>
36 #include <i386/machine_cpu.h>
37 #include <i386/misc_protos.h>
38 #include <i386/pmap.h>
41 #include <i386/proc_reg.h>
43 #include <kern/processor.h>
44 #include <i386/cpu_threads.h>
45 #include <i386/pmCPU.h>
46 #include <i386/cpuid.h>
47 #include <i386/rtclock.h>
48 #include <kern/sched_prim.h>
51 * Kernel parameter determining whether threads are halted unconditionally
52 * in the idle state. This is the default behavior.
53 * See machine_idle() for use.
57 extern int disableConsoleOutput
;
59 decl_simple_lock_data(,pm_init_lock
);
62 * The following is set when the KEXT loads and initializes.
64 pmDispatch_t
*pmDispatch
= NULL
;
66 static uint32_t pmInitDone
= 0;
70 * Initialize the Cstate change code.
73 power_management_init(void)
75 static boolean_t initialized
= FALSE
;
78 * Initialize the lock for the KEXT initialization.
81 simple_lock_init(&pm_init_lock
, 0);
85 if (pmDispatch
!= NULL
&& pmDispatch
->cstateInit
!= NULL
)
86 (*pmDispatch
->cstateInit
)();
90 * Called when the CPU is idle. It calls into the power management kext
91 * to determine the best way to idle the CPU.
96 cpu_data_t
*my_cpu
= current_cpu_datap();
102 * If idlehalt isn't set, then don't do any power management related
108 my_cpu
->lcpu
.state
= LCPU_IDLE
;
109 DBGLOG(cpu_handle
, cpu_number(), MP_IDLE
);
110 MARK_CPU_IDLE(cpu_number());
113 && pmDispatch
!= NULL
114 && pmDispatch
->cstateMachineIdle
!= NULL
)
115 (*pmDispatch
->cstateMachineIdle
)(0x7FFFFFFFFFFFFFFFULL
);
118 * If no power management, re-enable interrupts and halt.
119 * This will keep the CPU from spinning through the scheduler
120 * and will allow at least some minimal power savings (but it
121 * cause problems in some MP configurations w.r.t. the APIC
122 * stopping during a GV3 transition).
124 __asm__
volatile ("sti; hlt");
128 * Mark the CPU as running again.
130 MARK_CPU_ACTIVE(cpu_number());
131 DBGLOG(cpu_handle
, cpu_number(), MP_UNIDLE
);
132 my_cpu
->lcpu
.state
= LCPU_RUN
;
135 * Re-enable interrupts.
138 __asm__
volatile("sti");
142 * Called when the CPU is to be halted. It will choose the best C-State
146 pmCPUHalt(uint32_t reason
)
148 cpu_data_t
*cpup
= current_cpu_datap();
152 cpup
->lcpu
.state
= LCPU_PAUSE
;
153 __asm__
volatile ("wbinvd; hlt");
157 cpup
->lcpu
.state
= LCPU_PAUSE
;
158 __asm__
volatile ("cli; wbinvd; hlt");
163 __asm__
volatile ("cli");
166 && pmDispatch
!= NULL
167 && pmDispatch
->pmCPUHalt
!= NULL
) {
169 * Halt the CPU (and put it in a low power state.
171 (*pmDispatch
->pmCPUHalt
)();
174 * We've exited halt, so get the the CPU schedulable again.
176 i386_init_slave_fast();
178 panic("init_slave_fast returned");
181 * If no power managment and a processor is taken off-line,
182 * then invalidate the cache and halt it (it will not be able
183 * to be brought back on-line without resetting the CPU).
185 __asm__
volatile ("wbinvd");
186 cpup
->lcpu
.state
= LCPU_HALT
;
187 __asm__
volatile ( "wbinvd; hlt" );
189 panic("back from Halt");
196 pmMarkAllCPUsOff(void)
199 && pmDispatch
!= NULL
200 && pmDispatch
->markAllCPUsOff
!= NULL
)
201 (*pmDispatch
->markAllCPUsOff
)();
211 pmGetLogicalCPU(int cpu
)
213 return(cpu_to_lcpu(cpu
));
217 pmGetMyLogicalCPU(void)
219 cpu_data_t
*cpup
= current_cpu_datap();
227 return(cpu_to_core(cpu
));
233 cpu_data_t
*cpup
= current_cpu_datap();
235 return(cpup
->lcpu
.core
);
241 return(cpu_to_die(cpu
));
247 cpu_data_t
*cpup
= current_cpu_datap();
249 return(cpup
->lcpu
.die
);
253 pmGetPackage(int cpu
)
255 return(cpu_to_package(cpu
));
261 cpu_data_t
*cpup
= current_cpu_datap();
263 return(cpup
->lcpu
.package
);
267 pmLockCPUTopology(int lock
)
270 simple_lock(&x86_topo_lock
);
272 simple_unlock(&x86_topo_lock
);
277 * Called to get the next deadline that has been set by the
278 * power management code.
281 pmCPUGetDeadline(cpu_data_t
*cpu
)
283 uint64_t deadline
= EndOfAllTime
;
286 && pmDispatch
!= NULL
287 && pmDispatch
->GetDeadline
!= NULL
)
288 deadline
= (*pmDispatch
->GetDeadline
)(&cpu
->lcpu
);
294 * Called to determine if the supplied deadline or the power management
295 * deadline is sooner. Returns which ever one is first.
298 pmCPUSetDeadline(cpu_data_t
*cpu
, uint64_t deadline
)
301 && pmDispatch
!= NULL
302 && pmDispatch
->SetDeadline
!= NULL
)
303 deadline
= (*pmDispatch
->SetDeadline
)(&cpu
->lcpu
, deadline
);
309 * Called when a power management deadline expires.
312 pmCPUDeadline(cpu_data_t
*cpu
)
315 && pmDispatch
!= NULL
316 && pmDispatch
->Deadline
!= NULL
)
317 (*pmDispatch
->Deadline
)(&cpu
->lcpu
);
321 * Called to get a CPU out of idle.
324 pmCPUExitIdle(cpu_data_t
*cpu
)
329 && pmDispatch
!= NULL
330 && pmDispatch
->exitIdle
!= NULL
)
331 do_ipi
= (*pmDispatch
->exitIdle
)(&cpu
->lcpu
);
339 pmCPUExitHalt(int cpu
)
341 kern_return_t rc
= KERN_INVALID_ARGUMENT
;
344 && pmDispatch
!= NULL
345 && pmDispatch
->exitHalt
!= NULL
)
346 rc
= pmDispatch
->exitHalt(cpu_to_lcpu(cpu
));
352 * Called to initialize the power management structures for the CPUs.
357 if (pmDispatch
!= NULL
&& pmDispatch
->pmCPUStateInit
!= NULL
)
358 (*pmDispatch
->pmCPUStateInit
)();
362 * Called when a CPU is being restarted after being powered off (as in S3).
365 pmCPUMarkRunning(cpu_data_t
*cpu
)
367 cpu_data_t
*cpup
= current_cpu_datap();
370 && pmDispatch
!= NULL
371 && pmDispatch
->markCPURunning
!= NULL
)
372 (*pmDispatch
->markCPURunning
)(&cpu
->lcpu
);
374 cpup
->lcpu
.state
= LCPU_RUN
;
378 * Called to get/set CPU power management state.
381 pmCPUControl(uint32_t cmd
, void *datap
)
385 if (pmDispatch
!= NULL
386 && pmDispatch
->pmCPUControl
!= NULL
)
387 rc
= (*pmDispatch
->pmCPUControl
)(cmd
, datap
);
393 * Called to save the timer state used by power management prior
399 if (pmDispatch
!= NULL
400 && pmDispatch
->pmTimerStateSave
!= NULL
)
401 (*pmDispatch
->pmTimerStateSave
)();
405 * Called to restore the timer state used by power management after
406 * waking from "sleep".
411 if (pmDispatch
!= NULL
412 && pmDispatch
->pmTimerStateRestore
!= NULL
)
413 (*pmDispatch
->pmTimerStateRestore
)();
417 * Set the worst-case time for the C4 to C2 transition.
418 * No longer does anything.
421 ml_set_maxsnoop(__unused
uint32_t maxdelay
)
427 * Get the worst-case time for the C4 to C2 transition. Returns nanoseconds.
430 ml_get_maxsnoop(void)
432 uint64_t max_snoop
= 0;
434 if (pmDispatch
!= NULL
435 && pmDispatch
->getMaxSnoop
!= NULL
)
436 max_snoop
= pmDispatch
->getMaxSnoop();
438 return((unsigned)(max_snoop
& 0xffffffff));
443 ml_get_maxbusdelay(void)
445 uint64_t max_delay
= 0;
447 if (pmDispatch
!= NULL
448 && pmDispatch
->getMaxBusDelay
!= NULL
)
449 max_delay
= pmDispatch
->getMaxBusDelay();
451 return((uint32_t)(max_delay
& 0xffffffff));
455 * Set the maximum delay time allowed for snoop on the bus.
457 * Note that this value will be compared to the amount of time that it takes
458 * to transition from a non-snooping power state (C4) to a snooping state (C2).
459 * If maxBusDelay is less than C4C2SnoopDelay,
460 * we will not enter the lowest power state.
463 ml_set_maxbusdelay(uint32_t mdelay
)
465 uint64_t maxdelay
= mdelay
;
467 if (pmDispatch
!= NULL
468 && pmDispatch
->setMaxBusDelay
!= NULL
)
469 pmDispatch
->setMaxBusDelay(maxdelay
);
473 ml_get_maxintdelay(void)
475 uint64_t max_delay
= 0;
477 if (pmDispatch
!= NULL
478 && pmDispatch
->getMaxIntDelay
!= NULL
)
479 max_delay
= pmDispatch
->getMaxIntDelay();
485 * Set the maximum delay allowed for an interrupt.
488 ml_set_maxintdelay(uint64_t mdelay
)
490 if (pmDispatch
!= NULL
491 && pmDispatch
->setMaxIntDelay
!= NULL
)
492 pmDispatch
->setMaxIntDelay(mdelay
);
496 * Put a CPU into "safe" mode with respect to power.
498 * Some systems cannot operate at a continuous "normal" speed without
499 * exceeding the thermal design. This is called per-CPU to place the
500 * CPUs into a "safe" operating mode.
503 pmSafeMode(x86_lcpu_t
*lcpu
, uint32_t flags
)
505 if (pmDispatch
!= NULL
506 && pmDispatch
->pmCPUSafeMode
!= NULL
)
507 pmDispatch
->pmCPUSafeMode(lcpu
, flags
);
510 * Do something reasonable if the KEXT isn't present.
512 * We only look at the PAUSE and RESUME flags. The other flag(s)
513 * will not make any sense without the KEXT, so just ignore them.
515 * We set the CPU's state to indicate that it's halted. If this
516 * is the CPU we're currently running on, then spin until the
517 * state becomes non-halted.
519 if (flags
& PM_SAFE_FL_PAUSE
) {
520 lcpu
->state
= LCPU_PAUSE
;
521 if (lcpu
== x86_lcpu()) {
522 while (lcpu
->state
== LCPU_PAUSE
)
528 * Clear the halted flag for the specified CPU, that will
529 * get it out of it's spin loop.
531 if (flags
& PM_SAFE_FL_RESUME
) {
532 lcpu
->state
= LCPU_RUN
;
538 * Returns the root of the package tree.
547 pmCPUGetHibernate(int cpu
)
549 return(cpu_datap(cpu
)->cpu_hibernate
);
553 pmLCPUtoProcessor(int lcpu
)
555 return(cpu_datap(lcpu
)->cpu_processor
);
559 * Called by the power management kext to register itself and to get the
560 * callbacks it might need into other kernel functions. This interface
561 * is versioned to allow for slight mis-matches between the kext and the
565 pmKextRegister(uint32_t version
, pmDispatch_t
*cpuFuncs
,
566 pmCallBacks_t
*callbacks
)
568 if (callbacks
!= NULL
&& version
== PM_DISPATCH_VERSION
) {
569 callbacks
->setRTCPop
= setPop
;
570 callbacks
->resyncDeadlines
= etimer_resync_deadlines
;
571 callbacks
->initComplete
= pmInitComplete
;
572 callbacks
->GetLCPU
= pmGetLogicalCPU
;
573 callbacks
->GetCore
= pmGetCore
;
574 callbacks
->GetDie
= pmGetDie
;
575 callbacks
->GetPackage
= pmGetPackage
;
576 callbacks
->GetMyLCPU
= pmGetMyLogicalCPU
;
577 callbacks
->GetMyCore
= pmGetMyCore
;
578 callbacks
->GetMyDie
= pmGetMyDie
;
579 callbacks
->GetMyPackage
= pmGetMyPackage
;
580 callbacks
->GetPkgRoot
= pmGetPkgRoot
;
581 callbacks
->LockCPUTopology
= pmLockCPUTopology
;
582 callbacks
->GetHibernate
= pmCPUGetHibernate
;
583 callbacks
->LCPUtoProcessor
= pmLCPUtoProcessor
;
584 callbacks
->ThreadBind
= thread_bind
;
585 callbacks
->topoParms
= &topoParms
;
588 if (cpuFuncs
!= NULL
) {
589 pmDispatch
= cpuFuncs
;
594 * Unregisters the power management functions from the kext.
597 pmUnRegister(pmDispatch_t
*cpuFuncs
)
599 if (cpuFuncs
!= NULL
&& pmDispatch
== cpuFuncs
) {
604 /******************************************************************************
606 * All of the following are deprecated interfaces and no longer used.
608 ******************************************************************************/
610 pmsControl(__unused
uint32_t request
, __unused user_addr_t reqaddr
,
611 __unused
uint32_t reqsize
)
613 return(KERN_SUCCESS
);
632 pmsRun(__unused
uint32_t nstep
)
637 pmsBuild(__unused pmsDef
*pd
, __unused
uint32_t pdsize
,
638 __unused pmsSetFunc_t
*functab
,
639 __unused
uint32_t platformData
, __unused pmsQueryFunc_t queryFunc
)
641 return(KERN_SUCCESS
);