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 <kern/etimer.h>
45 #include <i386/cpu_threads.h>
46 #include <i386/pmCPU.h>
47 #include <i386/cpuid.h>
48 #include <i386/rtclock.h>
49 #include <kern/sched_prim.h>
50 #include <i386/lapic.h>
53 * Kernel parameter determining whether threads are halted unconditionally
54 * in the idle state. This is the default behavior.
55 * See machine_idle() for use.
59 extern int disableConsoleOutput
;
61 decl_simple_lock_data(,pm_init_lock
);
64 * The following is set when the KEXT loads and initializes.
66 pmDispatch_t
*pmDispatch
= NULL
;
68 static uint32_t pmInitDone
= 0;
72 * Initialize the Cstate change code.
75 power_management_init(void)
77 static boolean_t initialized
= FALSE
;
80 * Initialize the lock for the KEXT initialization.
83 simple_lock_init(&pm_init_lock
, 0);
87 if (pmDispatch
!= NULL
&& pmDispatch
->cstateInit
!= NULL
)
88 (*pmDispatch
->cstateInit
)();
92 * Called when the CPU is idle. It calls into the power management kext
93 * to determine the best way to idle the CPU.
98 cpu_data_t
*my_cpu
= current_cpu_datap();
104 * If idlehalt isn't set, then don't do any power management related
110 my_cpu
->lcpu
.state
= LCPU_IDLE
;
111 DBGLOG(cpu_handle
, cpu_number(), MP_IDLE
);
112 MARK_CPU_IDLE(cpu_number());
115 && pmDispatch
!= NULL
116 && pmDispatch
->cstateMachineIdle
!= NULL
)
117 (*pmDispatch
->cstateMachineIdle
)(0x7FFFFFFFFFFFFFFFULL
);
120 * If no power management, re-enable interrupts and halt.
121 * This will keep the CPU from spinning through the scheduler
122 * and will allow at least some minimal power savings (but it
123 * cause problems in some MP configurations w.r.t. the APIC
124 * stopping during a GV3 transition).
126 __asm__
volatile ("sti; hlt");
130 * Mark the CPU as running again.
132 MARK_CPU_ACTIVE(cpu_number());
133 DBGLOG(cpu_handle
, cpu_number(), MP_UNIDLE
);
134 my_cpu
->lcpu
.state
= LCPU_RUN
;
137 * Re-enable interrupts.
140 __asm__
volatile("sti");
144 * Called when the CPU is to be halted. It will choose the best C-State
148 pmCPUHalt(uint32_t reason
)
150 cpu_data_t
*cpup
= current_cpu_datap();
154 cpup
->lcpu
.state
= LCPU_PAUSE
;
155 __asm__
volatile ("wbinvd; hlt");
159 cpup
->lcpu
.state
= LCPU_PAUSE
;
160 __asm__
volatile ("cli; wbinvd; hlt");
165 __asm__
volatile ("cli");
168 && pmDispatch
!= NULL
169 && pmDispatch
->pmCPUHalt
!= NULL
) {
171 * Halt the CPU (and put it in a low power state.
173 (*pmDispatch
->pmCPUHalt
)();
176 * We've exited halt, so get the the CPU schedulable again.
178 i386_init_slave_fast();
180 panic("init_slave_fast returned");
183 * If no power managment and a processor is taken off-line,
184 * then invalidate the cache and halt it (it will not be able
185 * to be brought back on-line without resetting the CPU).
187 __asm__
volatile ("wbinvd");
188 cpup
->lcpu
.state
= LCPU_HALT
;
189 __asm__
volatile ( "wbinvd; hlt" );
191 panic("back from Halt");
198 pmMarkAllCPUsOff(void)
201 && pmDispatch
!= NULL
202 && pmDispatch
->markAllCPUsOff
!= NULL
)
203 (*pmDispatch
->markAllCPUsOff
)();
213 pmGetLogicalCPU(int cpu
)
215 return(cpu_to_lcpu(cpu
));
219 pmGetMyLogicalCPU(void)
221 cpu_data_t
*cpup
= current_cpu_datap();
229 return(cpu_to_core(cpu
));
235 cpu_data_t
*cpup
= current_cpu_datap();
237 return(cpup
->lcpu
.core
);
243 return(cpu_to_die(cpu
));
249 cpu_data_t
*cpup
= current_cpu_datap();
251 return(cpup
->lcpu
.die
);
255 pmGetPackage(int cpu
)
257 return(cpu_to_package(cpu
));
263 cpu_data_t
*cpup
= current_cpu_datap();
265 return(cpup
->lcpu
.package
);
269 pmLockCPUTopology(int lock
)
272 simple_lock(&x86_topo_lock
);
274 simple_unlock(&x86_topo_lock
);
279 * Called to get the next deadline that has been set by the
280 * power management code.
283 pmCPUGetDeadline(cpu_data_t
*cpu
)
285 uint64_t deadline
= EndOfAllTime
;
288 && pmDispatch
!= NULL
289 && pmDispatch
->GetDeadline
!= NULL
)
290 deadline
= (*pmDispatch
->GetDeadline
)(&cpu
->lcpu
);
296 * Called to determine if the supplied deadline or the power management
297 * deadline is sooner. Returns which ever one is first.
300 pmCPUSetDeadline(cpu_data_t
*cpu
, uint64_t deadline
)
303 && pmDispatch
!= NULL
304 && pmDispatch
->SetDeadline
!= NULL
)
305 deadline
= (*pmDispatch
->SetDeadline
)(&cpu
->lcpu
, deadline
);
311 * Called when a power management deadline expires.
314 pmCPUDeadline(cpu_data_t
*cpu
)
317 && pmDispatch
!= NULL
318 && pmDispatch
->Deadline
!= NULL
)
319 (*pmDispatch
->Deadline
)(&cpu
->lcpu
);
323 * Called to get a CPU out of idle.
326 pmCPUExitIdle(cpu_data_t
*cpu
)
331 && pmDispatch
!= NULL
332 && pmDispatch
->exitIdle
!= NULL
)
333 do_ipi
= (*pmDispatch
->exitIdle
)(&cpu
->lcpu
);
341 pmCPUExitHalt(int cpu
)
343 kern_return_t rc
= KERN_INVALID_ARGUMENT
;
346 && pmDispatch
!= NULL
347 && pmDispatch
->exitHalt
!= NULL
)
348 rc
= pmDispatch
->exitHalt(cpu_to_lcpu(cpu
));
354 pmCPUExitHaltToOff(int cpu
)
356 kern_return_t rc
= KERN_INVALID_ARGUMENT
;
359 && pmDispatch
!= NULL
360 && pmDispatch
->exitHaltToOff
!= NULL
)
361 rc
= pmDispatch
->exitHaltToOff(cpu_to_lcpu(cpu
));
367 * Called to initialize the power management structures for the CPUs.
372 if (pmDispatch
!= NULL
&& pmDispatch
->pmCPUStateInit
!= NULL
)
373 (*pmDispatch
->pmCPUStateInit
)();
377 * Called when a CPU is being restarted after being powered off (as in S3).
380 pmCPUMarkRunning(cpu_data_t
*cpu
)
382 cpu_data_t
*cpup
= current_cpu_datap();
385 && pmDispatch
!= NULL
386 && pmDispatch
->markCPURunning
!= NULL
)
387 (*pmDispatch
->markCPURunning
)(&cpu
->lcpu
);
389 cpup
->lcpu
.state
= LCPU_RUN
;
393 * Called to get/set CPU power management state.
396 pmCPUControl(uint32_t cmd
, void *datap
)
400 if (pmDispatch
!= NULL
401 && pmDispatch
->pmCPUControl
!= NULL
)
402 rc
= (*pmDispatch
->pmCPUControl
)(cmd
, datap
);
408 * Called to save the timer state used by power management prior
414 if (pmDispatch
!= NULL
415 && pmDispatch
->pmTimerStateSave
!= NULL
)
416 (*pmDispatch
->pmTimerStateSave
)();
420 * Called to restore the timer state used by power management after
421 * waking from "sleep".
426 if (pmDispatch
!= NULL
427 && pmDispatch
->pmTimerStateRestore
!= NULL
)
428 (*pmDispatch
->pmTimerStateRestore
)();
432 * Set the worst-case time for the C4 to C2 transition.
433 * No longer does anything.
436 ml_set_maxsnoop(__unused
uint32_t maxdelay
)
442 * Get the worst-case time for the C4 to C2 transition. Returns nanoseconds.
445 ml_get_maxsnoop(void)
447 uint64_t max_snoop
= 0;
449 if (pmDispatch
!= NULL
450 && pmDispatch
->getMaxSnoop
!= NULL
)
451 max_snoop
= pmDispatch
->getMaxSnoop();
453 return((unsigned)(max_snoop
& 0xffffffff));
458 ml_get_maxbusdelay(void)
460 uint64_t max_delay
= 0;
462 if (pmDispatch
!= NULL
463 && pmDispatch
->getMaxBusDelay
!= NULL
)
464 max_delay
= pmDispatch
->getMaxBusDelay();
466 return((uint32_t)(max_delay
& 0xffffffff));
470 * Set the maximum delay time allowed for snoop on the bus.
472 * Note that this value will be compared to the amount of time that it takes
473 * to transition from a non-snooping power state (C4) to a snooping state (C2).
474 * If maxBusDelay is less than C4C2SnoopDelay,
475 * we will not enter the lowest power state.
478 ml_set_maxbusdelay(uint32_t mdelay
)
480 uint64_t maxdelay
= mdelay
;
482 if (pmDispatch
!= NULL
483 && pmDispatch
->setMaxBusDelay
!= NULL
)
484 pmDispatch
->setMaxBusDelay(maxdelay
);
488 ml_get_maxintdelay(void)
490 uint64_t max_delay
= 0;
492 if (pmDispatch
!= NULL
493 && pmDispatch
->getMaxIntDelay
!= NULL
)
494 max_delay
= pmDispatch
->getMaxIntDelay();
500 * Set the maximum delay allowed for an interrupt.
503 ml_set_maxintdelay(uint64_t mdelay
)
505 if (pmDispatch
!= NULL
506 && pmDispatch
->setMaxIntDelay
!= NULL
)
507 pmDispatch
->setMaxIntDelay(mdelay
);
511 * Put a CPU into "safe" mode with respect to power.
513 * Some systems cannot operate at a continuous "normal" speed without
514 * exceeding the thermal design. This is called per-CPU to place the
515 * CPUs into a "safe" operating mode.
518 pmSafeMode(x86_lcpu_t
*lcpu
, uint32_t flags
)
520 if (pmDispatch
!= NULL
521 && pmDispatch
->pmCPUSafeMode
!= NULL
)
522 pmDispatch
->pmCPUSafeMode(lcpu
, flags
);
525 * Do something reasonable if the KEXT isn't present.
527 * We only look at the PAUSE and RESUME flags. The other flag(s)
528 * will not make any sense without the KEXT, so just ignore them.
530 * We set the CPU's state to indicate that it's halted. If this
531 * is the CPU we're currently running on, then spin until the
532 * state becomes non-halted.
534 if (flags
& PM_SAFE_FL_PAUSE
) {
535 lcpu
->state
= LCPU_PAUSE
;
536 if (lcpu
== x86_lcpu()) {
537 while (lcpu
->state
== LCPU_PAUSE
)
543 * Clear the halted flag for the specified CPU, that will
544 * get it out of it's spin loop.
546 if (flags
& PM_SAFE_FL_RESUME
) {
547 lcpu
->state
= LCPU_RUN
;
552 static uint32_t saved_run_count
= 0;
555 machine_run_count(uint32_t count
)
557 if (pmDispatch
!= NULL
558 && pmDispatch
->pmSetRunCount
!= NULL
)
559 pmDispatch
->pmSetRunCount(count
);
561 saved_run_count
= count
;
565 machine_cpu_is_inactive(int cpu
)
567 if (pmDispatch
!= NULL
568 && pmDispatch
->pmIsCPUUnAvailable
!= NULL
)
569 return(pmDispatch
->pmIsCPUUnAvailable(cpu_to_lcpu(cpu
)));
575 pmGetSavedRunCount(void)
577 return(saved_run_count
);
581 * Returns the root of the package tree.
590 pmCPUGetHibernate(int cpu
)
592 return(cpu_datap(cpu
)->cpu_hibernate
);
596 pmLCPUtoProcessor(int lcpu
)
598 return(cpu_datap(lcpu
)->cpu_processor
);
602 pmReSyncDeadlines(int cpu
)
604 static boolean_t registered
= FALSE
;
607 PM_interrupt_register(&etimer_resync_deadlines
);
611 if ((uint32_t)cpu
== current_cpu_datap()->lcpu
.cpu_num
)
612 etimer_resync_deadlines();
614 cpu_PM_interrupt(cpu
);
620 lapic_send_ipi(cpu
, LAPIC_PM_INTERRUPT
);
623 static rtc_nanotime_t
*
624 pmGetNanotimeInfo(void)
626 return(&rtc_nanotime_info
);
630 * Called by the power management kext to register itself and to get the
631 * callbacks it might need into other kernel functions. This interface
632 * is versioned to allow for slight mis-matches between the kext and the
636 pmKextRegister(uint32_t version
, pmDispatch_t
*cpuFuncs
,
637 pmCallBacks_t
*callbacks
)
639 if (callbacks
!= NULL
&& version
== PM_DISPATCH_VERSION
) {
640 callbacks
->setRTCPop
= setPop
;
641 callbacks
->resyncDeadlines
= pmReSyncDeadlines
;
642 callbacks
->initComplete
= pmInitComplete
;
643 callbacks
->GetLCPU
= pmGetLogicalCPU
;
644 callbacks
->GetCore
= pmGetCore
;
645 callbacks
->GetDie
= pmGetDie
;
646 callbacks
->GetPackage
= pmGetPackage
;
647 callbacks
->GetMyLCPU
= pmGetMyLogicalCPU
;
648 callbacks
->GetMyCore
= pmGetMyCore
;
649 callbacks
->GetMyDie
= pmGetMyDie
;
650 callbacks
->GetMyPackage
= pmGetMyPackage
;
651 callbacks
->GetPkgRoot
= pmGetPkgRoot
;
652 callbacks
->LockCPUTopology
= pmLockCPUTopology
;
653 callbacks
->GetHibernate
= pmCPUGetHibernate
;
654 callbacks
->LCPUtoProcessor
= pmLCPUtoProcessor
;
655 callbacks
->ThreadBind
= thread_bind
;
656 callbacks
->GetSavedRunCount
= pmGetSavedRunCount
;
657 callbacks
->pmSendIPI
= pmSendIPI
;
658 callbacks
->GetNanotimeInfo
= pmGetNanotimeInfo
;
659 callbacks
->topoParms
= &topoParms
;
661 panic("Version mis-match between Kernel and CPU PM");
664 if (cpuFuncs
!= NULL
) {
665 pmDispatch
= cpuFuncs
;
667 if (pmDispatch
->pmIPIHandler
!= NULL
) {
668 lapic_set_pm_func((i386_intr_func_t
)pmDispatch
->pmIPIHandler
);
674 * Unregisters the power management functions from the kext.
677 pmUnRegister(pmDispatch_t
*cpuFuncs
)
679 if (cpuFuncs
!= NULL
&& pmDispatch
== cpuFuncs
) {
684 /******************************************************************************
686 * All of the following are deprecated interfaces and no longer used.
688 ******************************************************************************/
690 pmsControl(__unused
uint32_t request
, __unused user_addr_t reqaddr
,
691 __unused
uint32_t reqsize
)
693 return(KERN_SUCCESS
);
712 pmsRun(__unused
uint32_t nstep
)
717 pmsBuild(__unused pmsDef
*pd
, __unused
uint32_t pdsize
,
718 __unused pmsSetFunc_t
*functab
,
719 __unused
uint32_t platformData
, __unused pmsQueryFunc_t queryFunc
)
721 return(KERN_SUCCESS
);