2 * Copyright (c) 2004-2009 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.
35 #include <i386/machine_cpu.h>
37 #include <i386/machine_routines.h>
38 #include <i386/proc_reg.h>
39 #include <i386/pmap.h>
40 #include <i386/misc_protos.h>
41 #include <kern/machine.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>
49 #include <i386/lapic.h>
52 * Kernel parameter determining whether threads are halted unconditionally
53 * in the idle state. This is the default behavior.
54 * See machine_idle() for use.
58 extern int disableConsoleOutput
;
60 decl_simple_lock_data(,pm_init_lock
);
63 * The following is set when the KEXT loads and initializes.
65 pmDispatch_t
*pmDispatch
= NULL
;
67 static uint32_t pmInitDone
= 0;
71 * Initialize the Cstate change code.
74 power_management_init(void)
76 static boolean_t initialized
= FALSE
;
79 * Initialize the lock for the KEXT initialization.
82 simple_lock_init(&pm_init_lock
, 0);
86 if (pmDispatch
!= NULL
&& pmDispatch
->cstateInit
!= NULL
)
87 (*pmDispatch
->cstateInit
)();
91 * Called when the CPU is idle. It calls into the power management kext
92 * to determine the best way to idle the CPU.
97 cpu_data_t
*my_cpu
= current_cpu_datap();
103 * If idlehalt isn't set, then don't do any power management related
109 my_cpu
->lcpu
.state
= LCPU_IDLE
;
110 DBGLOG(cpu_handle
, cpu_number(), MP_IDLE
);
111 MARK_CPU_IDLE(cpu_number());
114 && pmDispatch
!= NULL
115 && pmDispatch
->cstateMachineIdle
!= NULL
)
116 (*pmDispatch
->cstateMachineIdle
)(0x7FFFFFFFFFFFFFFFULL
);
119 * If no power management, re-enable interrupts and halt.
120 * This will keep the CPU from spinning through the scheduler
121 * and will allow at least some minimal power savings (but it
122 * cause problems in some MP configurations w.r.t. the APIC
123 * stopping during a GV3 transition).
125 __asm__
volatile ("sti; hlt");
129 * Mark the CPU as running again.
131 MARK_CPU_ACTIVE(cpu_number());
132 DBGLOG(cpu_handle
, cpu_number(), MP_UNIDLE
);
133 my_cpu
->lcpu
.state
= LCPU_RUN
;
136 * Re-enable interrupts.
139 __asm__
volatile("sti");
143 * Called when the CPU is to be halted. It will choose the best C-State
147 pmCPUHalt(uint32_t reason
)
149 cpu_data_t
*cpup
= current_cpu_datap();
153 cpup
->lcpu
.state
= LCPU_PAUSE
;
154 __asm__
volatile ("wbinvd; hlt");
158 cpup
->lcpu
.state
= LCPU_PAUSE
;
159 __asm__
volatile ("cli; wbinvd; hlt");
164 __asm__
volatile ("cli");
167 && pmDispatch
!= NULL
168 && pmDispatch
->pmCPUHalt
!= NULL
) {
170 * Halt the CPU (and put it in a low power state.
172 (*pmDispatch
->pmCPUHalt
)();
175 * We've exited halt, so get the the CPU schedulable again.
177 i386_init_slave_fast();
179 panic("init_slave_fast returned");
182 * If no power managment and a processor is taken off-line,
183 * then invalidate the cache and halt it (it will not be able
184 * to be brought back on-line without resetting the CPU).
186 __asm__
volatile ("wbinvd");
187 cpup
->lcpu
.state
= LCPU_HALT
;
188 __asm__
volatile ( "wbinvd; hlt" );
190 panic("back from Halt");
197 pmMarkAllCPUsOff(void)
200 && pmDispatch
!= NULL
201 && pmDispatch
->markAllCPUsOff
!= NULL
)
202 (*pmDispatch
->markAllCPUsOff
)();
212 pmGetLogicalCPU(int cpu
)
214 return(cpu_to_lcpu(cpu
));
218 pmGetMyLogicalCPU(void)
220 cpu_data_t
*cpup
= current_cpu_datap();
228 return(cpu_to_core(cpu
));
234 cpu_data_t
*cpup
= current_cpu_datap();
236 return(cpup
->lcpu
.core
);
242 return(cpu_to_die(cpu
));
248 cpu_data_t
*cpup
= current_cpu_datap();
250 return(cpup
->lcpu
.die
);
254 pmGetPackage(int cpu
)
256 return(cpu_to_package(cpu
));
262 cpu_data_t
*cpup
= current_cpu_datap();
264 return(cpup
->lcpu
.package
);
268 pmLockCPUTopology(int lock
)
271 simple_lock(&x86_topo_lock
);
273 simple_unlock(&x86_topo_lock
);
278 * Called to get the next deadline that has been set by the
279 * power management code.
282 pmCPUGetDeadline(cpu_data_t
*cpu
)
284 uint64_t deadline
= EndOfAllTime
;
287 && pmDispatch
!= NULL
288 && pmDispatch
->GetDeadline
!= NULL
)
289 deadline
= (*pmDispatch
->GetDeadline
)(&cpu
->lcpu
);
295 * Called to determine if the supplied deadline or the power management
296 * deadline is sooner. Returns which ever one is first.
299 pmCPUSetDeadline(cpu_data_t
*cpu
, uint64_t deadline
)
302 && pmDispatch
!= NULL
303 && pmDispatch
->SetDeadline
!= NULL
)
304 deadline
= (*pmDispatch
->SetDeadline
)(&cpu
->lcpu
, deadline
);
310 * Called when a power management deadline expires.
313 pmCPUDeadline(cpu_data_t
*cpu
)
316 && pmDispatch
!= NULL
317 && pmDispatch
->Deadline
!= NULL
)
318 (*pmDispatch
->Deadline
)(&cpu
->lcpu
);
322 * Called to get a CPU out of idle.
325 pmCPUExitIdle(cpu_data_t
*cpu
)
330 && pmDispatch
!= NULL
331 && pmDispatch
->exitIdle
!= NULL
)
332 do_ipi
= (*pmDispatch
->exitIdle
)(&cpu
->lcpu
);
340 pmCPUExitHalt(int cpu
)
342 kern_return_t rc
= KERN_INVALID_ARGUMENT
;
345 && pmDispatch
!= NULL
346 && pmDispatch
->exitHalt
!= NULL
)
347 rc
= pmDispatch
->exitHalt(cpu_to_lcpu(cpu
));
353 pmCPUExitHaltToOff(int cpu
)
355 kern_return_t rc
= KERN_INVALID_ARGUMENT
;
358 && pmDispatch
!= NULL
359 && pmDispatch
->exitHaltToOff
!= NULL
)
360 rc
= pmDispatch
->exitHaltToOff(cpu_to_lcpu(cpu
));
366 * Called to initialize the power management structures for the CPUs.
371 if (pmDispatch
!= NULL
&& pmDispatch
->pmCPUStateInit
!= NULL
)
372 (*pmDispatch
->pmCPUStateInit
)();
376 * Called when a CPU is being restarted after being powered off (as in S3).
379 pmCPUMarkRunning(cpu_data_t
*cpu
)
381 cpu_data_t
*cpup
= current_cpu_datap();
384 && pmDispatch
!= NULL
385 && pmDispatch
->markCPURunning
!= NULL
)
386 (*pmDispatch
->markCPURunning
)(&cpu
->lcpu
);
388 cpup
->lcpu
.state
= LCPU_RUN
;
392 * Called to get/set CPU power management state.
395 pmCPUControl(uint32_t cmd
, void *datap
)
399 if (pmDispatch
!= NULL
400 && pmDispatch
->pmCPUControl
!= NULL
)
401 rc
= (*pmDispatch
->pmCPUControl
)(cmd
, datap
);
407 * Called to save the timer state used by power management prior
413 if (pmDispatch
!= NULL
414 && pmDispatch
->pmTimerStateSave
!= NULL
)
415 (*pmDispatch
->pmTimerStateSave
)();
419 * Called to restore the timer state used by power management after
420 * waking from "sleep".
425 if (pmDispatch
!= NULL
426 && pmDispatch
->pmTimerStateRestore
!= NULL
)
427 (*pmDispatch
->pmTimerStateRestore
)();
431 * Set the worst-case time for the C4 to C2 transition.
432 * No longer does anything.
435 ml_set_maxsnoop(__unused
uint32_t maxdelay
)
441 * Get the worst-case time for the C4 to C2 transition. Returns nanoseconds.
444 ml_get_maxsnoop(void)
446 uint64_t max_snoop
= 0;
448 if (pmDispatch
!= NULL
449 && pmDispatch
->getMaxSnoop
!= NULL
)
450 max_snoop
= pmDispatch
->getMaxSnoop();
452 return((unsigned)(max_snoop
& 0xffffffff));
457 ml_get_maxbusdelay(void)
459 uint64_t max_delay
= 0;
461 if (pmDispatch
!= NULL
462 && pmDispatch
->getMaxBusDelay
!= NULL
)
463 max_delay
= pmDispatch
->getMaxBusDelay();
465 return((uint32_t)(max_delay
& 0xffffffff));
469 * Set the maximum delay time allowed for snoop on the bus.
471 * Note that this value will be compared to the amount of time that it takes
472 * to transition from a non-snooping power state (C4) to a snooping state (C2).
473 * If maxBusDelay is less than C4C2SnoopDelay,
474 * we will not enter the lowest power state.
477 ml_set_maxbusdelay(uint32_t mdelay
)
479 uint64_t maxdelay
= mdelay
;
481 if (pmDispatch
!= NULL
482 && pmDispatch
->setMaxBusDelay
!= NULL
)
483 pmDispatch
->setMaxBusDelay(maxdelay
);
487 ml_get_maxintdelay(void)
489 uint64_t max_delay
= 0;
491 if (pmDispatch
!= NULL
492 && pmDispatch
->getMaxIntDelay
!= NULL
)
493 max_delay
= pmDispatch
->getMaxIntDelay();
499 * Set the maximum delay allowed for an interrupt.
502 ml_set_maxintdelay(uint64_t mdelay
)
504 if (pmDispatch
!= NULL
505 && pmDispatch
->setMaxIntDelay
!= NULL
)
506 pmDispatch
->setMaxIntDelay(mdelay
);
510 * Put a CPU into "safe" mode with respect to power.
512 * Some systems cannot operate at a continuous "normal" speed without
513 * exceeding the thermal design. This is called per-CPU to place the
514 * CPUs into a "safe" operating mode.
517 pmSafeMode(x86_lcpu_t
*lcpu
, uint32_t flags
)
519 if (pmDispatch
!= NULL
520 && pmDispatch
->pmCPUSafeMode
!= NULL
)
521 pmDispatch
->pmCPUSafeMode(lcpu
, flags
);
524 * Do something reasonable if the KEXT isn't present.
526 * We only look at the PAUSE and RESUME flags. The other flag(s)
527 * will not make any sense without the KEXT, so just ignore them.
529 * We set the CPU's state to indicate that it's halted. If this
530 * is the CPU we're currently running on, then spin until the
531 * state becomes non-halted.
533 if (flags
& PM_SAFE_FL_PAUSE
) {
534 lcpu
->state
= LCPU_PAUSE
;
535 if (lcpu
== x86_lcpu()) {
536 while (lcpu
->state
== LCPU_PAUSE
)
542 * Clear the halted flag for the specified CPU, that will
543 * get it out of it's spin loop.
545 if (flags
& PM_SAFE_FL_RESUME
) {
546 lcpu
->state
= LCPU_RUN
;
551 static uint32_t saved_run_count
= 0;
554 machine_run_count(uint32_t count
)
556 if (pmDispatch
!= NULL
557 && pmDispatch
->pmSetRunCount
!= NULL
)
558 pmDispatch
->pmSetRunCount(count
);
560 saved_run_count
= count
;
564 machine_cpu_is_inactive(int cpu
)
566 if (pmDispatch
!= NULL
567 && pmDispatch
->pmIsCPUUnAvailable
!= NULL
)
568 return(pmDispatch
->pmIsCPUUnAvailable(cpu_to_lcpu(cpu
)));
574 pmGetSavedRunCount(void)
576 return(saved_run_count
);
580 * Returns the root of the package tree.
589 pmCPUGetHibernate(int cpu
)
591 return(cpu_datap(cpu
)->cpu_hibernate
);
595 pmLCPUtoProcessor(int lcpu
)
597 return(cpu_datap(lcpu
)->cpu_processor
);
601 pmReSyncDeadlines(int cpu
)
603 static boolean_t registered
= FALSE
;
606 PM_interrupt_register(&etimer_resync_deadlines
);
610 if ((uint32_t)cpu
== current_cpu_datap()->lcpu
.cpu_num
)
611 etimer_resync_deadlines();
613 cpu_PM_interrupt(cpu
);
619 lapic_send_ipi(cpu
, LAPIC_PM_INTERRUPT
);
623 * Called by the power management kext to register itself and to get the
624 * callbacks it might need into other kernel functions. This interface
625 * is versioned to allow for slight mis-matches between the kext and the
629 pmKextRegister(uint32_t version
, pmDispatch_t
*cpuFuncs
,
630 pmCallBacks_t
*callbacks
)
632 if (callbacks
!= NULL
&& version
== PM_DISPATCH_VERSION
) {
633 callbacks
->setRTCPop
= setPop
;
634 callbacks
->resyncDeadlines
= pmReSyncDeadlines
;
635 callbacks
->initComplete
= pmInitComplete
;
636 callbacks
->GetLCPU
= pmGetLogicalCPU
;
637 callbacks
->GetCore
= pmGetCore
;
638 callbacks
->GetDie
= pmGetDie
;
639 callbacks
->GetPackage
= pmGetPackage
;
640 callbacks
->GetMyLCPU
= pmGetMyLogicalCPU
;
641 callbacks
->GetMyCore
= pmGetMyCore
;
642 callbacks
->GetMyDie
= pmGetMyDie
;
643 callbacks
->GetMyPackage
= pmGetMyPackage
;
644 callbacks
->GetPkgRoot
= pmGetPkgRoot
;
645 callbacks
->LockCPUTopology
= pmLockCPUTopology
;
646 callbacks
->GetHibernate
= pmCPUGetHibernate
;
647 callbacks
->LCPUtoProcessor
= pmLCPUtoProcessor
;
648 callbacks
->ThreadBind
= thread_bind
;
649 callbacks
->GetSavedRunCount
= pmGetSavedRunCount
;
650 callbacks
->pmSendIPI
= pmSendIPI
;
651 callbacks
->topoParms
= &topoParms
;
653 panic("Version mis-match between Kernel and CPU PM");
656 if (cpuFuncs
!= NULL
) {
657 pmDispatch
= cpuFuncs
;
659 if (pmDispatch
->pmIPIHandler
!= NULL
) {
660 lapic_set_pm_func((i386_intr_func_t
)pmDispatch
->pmIPIHandler
);
666 * Unregisters the power management functions from the kext.
669 pmUnRegister(pmDispatch_t
*cpuFuncs
)
671 if (cpuFuncs
!= NULL
&& pmDispatch
== cpuFuncs
) {
676 /******************************************************************************
678 * All of the following are deprecated interfaces and no longer used.
680 ******************************************************************************/
682 pmsControl(__unused
uint32_t request
, __unused user_addr_t reqaddr
,
683 __unused
uint32_t reqsize
)
685 return(KERN_SUCCESS
);
704 pmsRun(__unused
uint32_t nstep
)
709 pmsBuild(__unused pmsDef
*pd
, __unused
uint32_t pdsize
,
710 __unused pmsSetFunc_t
*functab
,
711 __unused
uint32_t platformData
, __unused pmsQueryFunc_t queryFunc
)
713 return(KERN_SUCCESS
);