2 * Copyright (c) 2007-2016 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@
31 * cpu specific routines
34 #include <pexpert/arm64/board_config.h>
35 #include <kern/kalloc.h>
36 #include <kern/machine.h>
37 #include <kern/cpu_number.h>
38 #include <kern/thread.h>
39 #include <kern/timer_queue.h>
40 #include <arm/cpu_data.h>
41 #include <arm/cpuid.h>
42 #include <arm/caches_internal.h>
43 #include <arm/cpu_data_internal.h>
44 #include <arm/cpu_internal.h>
45 #include <arm/misc_protos.h>
46 #include <arm/machine_cpu.h>
47 #include <arm/rtclock.h>
48 #include <arm64/proc_reg.h>
49 #include <mach/processor_info.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_map.h>
53 #include <pexpert/arm/protos.h>
54 #include <pexpert/device_tree.h>
55 #include <sys/kdebug.h>
56 #include <arm/machine_routines.h>
58 #include <machine/atomic.h>
60 #include <san/kasan.h>
67 #include <kern/monotonic.h>
68 #endif /* MONOTONIC */
70 extern boolean_t idle_enable
;
71 extern uint64_t wake_abstime
;
74 void sleep_token_buffer_init(void);
78 extern uintptr_t resume_idle_cpu
;
79 extern uintptr_t start_cpu
;
81 #if __ARM_KERNEL_PROTECT__
82 extern void exc_vectors_table
;
83 #endif /* __ARM_KERNEL_PROTECT__ */
85 extern void __attribute__((noreturn
)) arm64_prepare_for_sleep(void);
86 extern void arm64_force_wfi_clock_gate(void);
87 #if (defined(APPLECYCLONE) || defined(APPLETYPHOON))
88 // <rdar://problem/15827409> CPU1 Stuck in WFIWT Because of MMU Prefetch
89 extern void cyclone_typhoon_prepare_for_wfi(void);
90 extern void cyclone_typhoon_return_from_wfi(void);
94 vm_address_t start_cpu_paddr
;
96 sysreg_restore_t sysreg_restore
__attribute__((section("__DATA, __const"))) = {
97 .tcr_el1
= TCR_EL1_BOOT
,
104 // 2 : overhead simulation (delay & flags)
107 #if DEVELOPMENT || DEBUG
110 // 1 << 0 : flush L1s
111 // 1 << 1 : flush TLBs
112 static int wfi_flags
= 0;
114 // wfi_delay - delay ticks after wfi exit
115 static uint64_t wfi_delay
= 0;
117 #endif /* DEVELOPMENT || DEBUG */
119 #if __ARM_GLOBAL_SLEEP_BIT__
120 volatile boolean_t arm64_stall_sleep
= TRUE
;
125 * These must be aligned to avoid issues with calling bcopy_phys on them before
126 * we are done with pmap initialization.
128 static const uint8_t __attribute__ ((aligned(8))) suspend_signature
[] = {'X', 'S', 'O', 'M', 'P', 'S', 'U', 'S'};
129 static const uint8_t __attribute__ ((aligned(8))) running_signature
[] = {'X', 'S', 'O', 'M', 'N', 'N', 'U', 'R'};
133 static vm_offset_t sleepTokenBuffer
= (vm_offset_t
)NULL
;
135 static boolean_t coresight_debug_enabled
= FALSE
;
139 configure_coresight_registers(cpu_data_t
*cdp
)
147 * ARMv8 coresight registers are optional. If the device tree did not
148 * provide cpu_regmap_paddr, assume that coresight registers are not
151 if (cdp
->cpu_regmap_paddr
) {
152 for (i
= 0; i
< CORESIGHT_REGIONS
; ++i
) {
153 /* Skip CTI; these registers are debug-only (they are
154 * not present on production hardware), and there is
155 * at least one known Cyclone errata involving CTI
156 * (rdar://12802966). We have no known clients that
157 * need the kernel to unlock CTI, so it is safer
158 * to avoid doing the access.
160 if (i
== CORESIGHT_CTI
)
162 /* Skip debug-only registers on production chips */
163 if (((i
== CORESIGHT_ED
) || (i
== CORESIGHT_UTT
)) && !coresight_debug_enabled
)
166 if (!cdp
->coresight_base
[i
]) {
167 addr
= cdp
->cpu_regmap_paddr
+ CORESIGHT_OFFSET(i
);
168 cdp
->coresight_base
[i
] = (vm_offset_t
)ml_io_map(addr
, CORESIGHT_SIZE
);
171 * At this point, failing to io map the
172 * registers is considered as an error.
174 if (!cdp
->coresight_base
[i
]) {
175 panic("unable to ml_io_map coresight regions");
178 /* Unlock EDLAR, CTILAR, PMLAR */
179 if (i
!= CORESIGHT_UTT
)
180 *(volatile uint32_t *)(cdp
->coresight_base
[i
] + ARM_DEBUG_OFFSET_DBGLAR
) = ARM_DBG_LOCK_ACCESS_KEY
;
187 * Routine: cpu_bootstrap
202 cpu_data_t
*cpu_data_ptr
= getCpuDatap();
204 pmap_switch_user_ttb(kernel_pmap
);
205 cpu_data_ptr
->cpu_active_thread
= current_thread();
206 cpu_data_ptr
->cpu_reset_handler
= (uintptr_t) start_cpu_paddr
;
207 cpu_data_ptr
->cpu_flags
|= SleepState
;
208 cpu_data_ptr
->cpu_user_debug
= NULL
;
213 mt_cpu_down(cpu_data_ptr
);
214 #endif /* MONOTONIC */
218 PE_cpu_machine_quiesce(cpu_data_ptr
->cpu_id
);
226 void __attribute__((noreturn
))
229 cpu_data_t
*cpu_data_ptr
= getCpuDatap();
230 uint64_t new_idle_timeout_ticks
= 0x0ULL
, lastPop
;
232 if ((!idle_enable
) || (cpu_data_ptr
->cpu_signal
& SIGPdisabled
))
236 lastPop
= cpu_data_ptr
->rtcPop
;
238 pmap_switch_user_ttb(kernel_pmap
);
239 cpu_data_ptr
->cpu_active_thread
= current_thread();
240 if (cpu_data_ptr
->cpu_user_debug
)
242 cpu_data_ptr
->cpu_user_debug
= NULL
;
244 if (cpu_data_ptr
->cpu_idle_notify
)
245 ((processor_idle_t
) cpu_data_ptr
->cpu_idle_notify
) (cpu_data_ptr
->cpu_id
, TRUE
, &new_idle_timeout_ticks
);
247 if (cpu_data_ptr
->idle_timer_notify
!= 0) {
248 if (new_idle_timeout_ticks
== 0x0ULL
) {
249 /* turn off the idle timer */
250 cpu_data_ptr
->idle_timer_deadline
= 0x0ULL
;
252 /* set the new idle timeout */
253 clock_absolutetime_interval_to_deadline(new_idle_timeout_ticks
, &cpu_data_ptr
->idle_timer_deadline
);
255 timer_resync_deadlines();
256 if (cpu_data_ptr
->rtcPop
!= lastPop
)
264 mt_cpu_idle(cpu_data_ptr
);
265 #endif /* MONOTONIC */
268 platform_cache_idle_enter();
270 #if DEVELOPMENT || DEBUG
271 // When simulating wfi overhead,
272 // force wfi to clock gating only
274 arm64_force_wfi_clock_gate();
276 #endif /* DEVELOPMENT || DEBUG */
278 #if defined(APPLECYCLONE) || defined(APPLETYPHOON)
279 // <rdar://problem/15827409> CPU1 Stuck in WFIWT Because of MMU Prefetch
280 cyclone_typhoon_prepare_for_wfi();
282 __builtin_arm_dsb(DSB_SY
);
285 #if defined(APPLECYCLONE) || defined(APPLETYPHOON)
286 // <rdar://problem/15827409> CPU1 Stuck in WFIWT Because of MMU Prefetch
287 cyclone_typhoon_return_from_wfi();
290 #if DEVELOPMENT || DEBUG
291 // Handle wfi overhead simulation
295 // Calculate wfi delay deadline
296 clock_absolutetime_interval_to_deadline(wfi_delay
, &deadline
);
299 if ((wfi_flags
& 1) != 0) {
300 InvalidatePoU_Icache();
305 if ((wfi_flags
& 2) != 0) {
309 // Wait for the ballance of the wfi delay
310 clock_delay_until(deadline
);
312 #endif /* DEVELOPMENT || DEBUG */
314 platform_cache_idle_exit();
323 * Routine: cpu_idle_exit
329 uint64_t new_idle_timeout_ticks
= 0x0ULL
;
330 cpu_data_t
*cpu_data_ptr
= getCpuDatap();
332 assert(exception_stack_pointer() != 0);
334 /* Back from WFI, unlock OSLAR and EDLAR. */
335 configure_coresight_registers(cpu_data_ptr
);
342 mt_cpu_run(cpu_data_ptr
);
343 #endif /* MONOTONIC */
345 pmap_switch_user_ttb(cpu_data_ptr
->cpu_active_thread
->map
->pmap
);
347 if (cpu_data_ptr
->cpu_idle_notify
)
348 ((processor_idle_t
) cpu_data_ptr
->cpu_idle_notify
) (cpu_data_ptr
->cpu_id
, FALSE
, &new_idle_timeout_ticks
);
350 if (cpu_data_ptr
->idle_timer_notify
!= 0) {
351 if (new_idle_timeout_ticks
== 0x0ULL
) {
352 /* turn off the idle timer */
353 cpu_data_ptr
->idle_timer_deadline
= 0x0ULL
;
355 /* set the new idle timeout */
356 clock_absolutetime_interval_to_deadline(new_idle_timeout_ticks
, &cpu_data_ptr
->idle_timer_deadline
);
358 timer_resync_deadlines();
367 cpu_data_t
*cdp
= getCpuDatap();
368 arm_cpu_info_t
*cpu_info_p
;
370 assert(exception_stack_pointer() != 0);
372 if (cdp
->cpu_type
!= CPU_TYPE_ARM64
) {
374 cdp
->cpu_type
= CPU_TYPE_ARM64
;
376 timer_call_queue_init(&cdp
->rtclock_timer
.queue
);
377 cdp
->rtclock_timer
.deadline
= EndOfAllTime
;
379 if (cdp
== &BootCpuData
) {
385 * We initialize non-boot CPUs here; the boot CPU is
386 * dealt with as part of pmap_bootstrap.
388 pmap_cpu_data_init();
390 /* ARM_SMP: Assuming identical cpu */
393 cpu_info_p
= cpuid_info();
395 /* switch based on CPU's reported architecture */
396 switch (cpu_info_p
->arm_info
.arm_arch
) {
398 cdp
->cpu_subtype
= CPU_SUBTYPE_ARM64_V8
;
401 //cdp->cpu_subtype = CPU_SUBTYPE_ARM64_ALL;
402 /* this panic doesn't work this early in startup */
403 panic("Unknown CPU subtype...");
407 cdp
->cpu_threadtype
= CPU_THREADTYPE_NONE
;
409 cdp
->cpu_stat
.irq_ex_cnt_wake
= 0;
410 cdp
->cpu_stat
.ipi_cnt_wake
= 0;
411 cdp
->cpu_stat
.timer_cnt_wake
= 0;
412 cdp
->cpu_running
= TRUE
;
413 cdp
->cpu_sleep_token_last
= cdp
->cpu_sleep_token
;
414 cdp
->cpu_sleep_token
= 0x0UL
;
420 #endif /* MONOTONIC */
424 cpu_data_alloc(boolean_t is_boot_cpu
)
426 cpu_data_t
*cpu_data_ptr
= NULL
;
429 cpu_data_ptr
= &BootCpuData
;
431 void *irq_stack
= NULL
;
432 void *exc_stack
= NULL
;
433 void *fiq_stack
= NULL
;
435 if ((kmem_alloc(kernel_map
, (vm_offset_t
*)&cpu_data_ptr
, sizeof(cpu_data_t
), VM_KERN_MEMORY_CPU
)) != KERN_SUCCESS
)
436 goto cpu_data_alloc_error
;
438 bzero((void *)cpu_data_ptr
, sizeof(cpu_data_t
));
440 if ((irq_stack
= kalloc(INTSTACK_SIZE
)) == 0)
441 goto cpu_data_alloc_error
;
442 cpu_data_ptr
->intstack_top
= (vm_offset_t
)irq_stack
+ INTSTACK_SIZE
;
443 cpu_data_ptr
->istackptr
= cpu_data_ptr
->intstack_top
;
445 if ((exc_stack
= kalloc(PAGE_SIZE
)) == 0)
446 goto cpu_data_alloc_error
;
447 cpu_data_ptr
->excepstack_top
= (vm_offset_t
)exc_stack
+ PAGE_SIZE
;
448 cpu_data_ptr
->excepstackptr
= cpu_data_ptr
->excepstack_top
;
450 if ((fiq_stack
= kalloc(PAGE_SIZE
)) == 0)
451 goto cpu_data_alloc_error
;
452 cpu_data_ptr
->fiqstack_top
= (vm_offset_t
)fiq_stack
+ PAGE_SIZE
;
453 cpu_data_ptr
->fiqstackptr
= cpu_data_ptr
->fiqstack_top
;
456 cpu_data_ptr
->cpu_processor
= cpu_processor_alloc(is_boot_cpu
);
457 if (cpu_data_ptr
->cpu_processor
== (struct processor
*)NULL
)
458 goto cpu_data_alloc_error
;
462 cpu_data_alloc_error
:
463 panic("cpu_data_alloc() failed\n");
464 return (cpu_data_t
*)NULL
;
469 cpu_data_free(cpu_data_t
*cpu_data_ptr
)
471 if (cpu_data_ptr
== &BootCpuData
)
474 cpu_processor_free( cpu_data_ptr
->cpu_processor
);
475 kfree( (void *)(cpu_data_ptr
->intstack_top
- INTSTACK_SIZE
), INTSTACK_SIZE
);
476 kfree( (void *)(cpu_data_ptr
->fiqstack_top
- PAGE_SIZE
), PAGE_SIZE
);
477 kmem_free(kernel_map
, (vm_offset_t
)cpu_data_ptr
, sizeof(cpu_data_t
));
481 cpu_data_init(cpu_data_t
*cpu_data_ptr
)
485 cpu_data_ptr
->cpu_flags
= 0;
486 cpu_data_ptr
->interrupts_enabled
= 0;
487 cpu_data_ptr
->cpu_int_state
= 0;
488 cpu_data_ptr
->cpu_pending_ast
= AST_NONE
;
489 cpu_data_ptr
->cpu_cache_dispatch
= (void *) 0;
490 cpu_data_ptr
->rtcPop
= EndOfAllTime
;
491 cpu_data_ptr
->rtclock_datap
= &RTClockData
;
492 cpu_data_ptr
->cpu_user_debug
= NULL
;
495 cpu_data_ptr
->cpu_base_timebase
= 0;
496 cpu_data_ptr
->cpu_idle_notify
= (void *) 0;
497 cpu_data_ptr
->cpu_idle_latency
= 0x0ULL
;
498 cpu_data_ptr
->cpu_idle_pop
= 0x0ULL
;
499 cpu_data_ptr
->cpu_reset_type
= 0x0UL
;
500 cpu_data_ptr
->cpu_reset_handler
= 0x0UL
;
501 cpu_data_ptr
->cpu_reset_assist
= 0x0UL
;
502 cpu_data_ptr
->cpu_regmap_paddr
= 0x0ULL
;
503 cpu_data_ptr
->cpu_phys_id
= 0x0UL
;
504 cpu_data_ptr
->cpu_l2_access_penalty
= 0;
505 cpu_data_ptr
->cpu_cluster_type
= CLUSTER_TYPE_SMP
;
506 cpu_data_ptr
->cpu_cluster_id
= 0;
507 cpu_data_ptr
->cpu_l2_id
= 0;
508 cpu_data_ptr
->cpu_l2_size
= 0;
509 cpu_data_ptr
->cpu_l3_id
= 0;
510 cpu_data_ptr
->cpu_l3_size
= 0;
512 cpu_data_ptr
->cpu_signal
= SIGPdisabled
;
514 #if DEBUG || DEVELOPMENT
515 cpu_data_ptr
->failed_xcall
= NULL
;
516 cpu_data_ptr
->failed_signal
= 0;
517 cpu_data_ptr
->failed_signal_count
= 0;
520 cpu_data_ptr
->cpu_get_fiq_handler
= NULL
;
521 cpu_data_ptr
->cpu_tbd_hardware_addr
= NULL
;
522 cpu_data_ptr
->cpu_tbd_hardware_val
= NULL
;
523 cpu_data_ptr
->cpu_get_decrementer_func
= NULL
;
524 cpu_data_ptr
->cpu_set_decrementer_func
= NULL
;
525 cpu_data_ptr
->cpu_sleep_token
= ARM_CPU_ON_SLEEP_PATH
;
526 cpu_data_ptr
->cpu_sleep_token_last
= 0x00000000UL
;
527 cpu_data_ptr
->cpu_xcall_p0
= NULL
;
528 cpu_data_ptr
->cpu_xcall_p1
= NULL
;
530 for (i
= 0; i
< CORESIGHT_REGIONS
; ++i
) {
531 cpu_data_ptr
->coresight_base
[i
] = 0;
534 pmap_cpu_data_t
* pmap_cpu_data_ptr
= &cpu_data_ptr
->cpu_pmap_cpu_data
;
536 pmap_cpu_data_ptr
->cpu_user_pmap
= (struct pmap
*) NULL
;
537 pmap_cpu_data_ptr
->cpu_user_pmap_stamp
= 0;
538 pmap_cpu_data_ptr
->cpu_number
= PMAP_INVALID_CPU_NUM
;
540 for (i
= 0; i
< (sizeof(pmap_cpu_data_ptr
->cpu_asid_high_bits
) / sizeof(*pmap_cpu_data_ptr
->cpu_asid_high_bits
)); i
++) {
541 pmap_cpu_data_ptr
->cpu_asid_high_bits
[i
] = 0;
543 cpu_data_ptr
->halt_status
= CPU_NOT_HALTED
;
544 #if __ARM_KERNEL_PROTECT__
545 cpu_data_ptr
->cpu_exc_vectors
= (vm_offset_t
)&exc_vectors_table
;
546 #endif /* __ARM_KERNEL_PROTECT__ */
550 cpu_data_register(cpu_data_t
*cpu_data_ptr
)
552 int cpu
= cpu_data_ptr
->cpu_number
;
555 for (int i
= 0; i
< CPUWINDOWS_MAX
; i
++) {
556 kasan_notify_address_nopoison(pmap_cpu_windows_copy_addr(cpu
, i
), PAGE_SIZE
);
560 CpuDataEntries
[cpu
].cpu_data_vaddr
= cpu_data_ptr
;
561 CpuDataEntries
[cpu
].cpu_data_paddr
= (void *)ml_vtophys( (vm_offset_t
)cpu_data_ptr
);
569 cpu_data_t
*cpu_data_ptr
= CpuDataEntries
[cpu
].cpu_data_vaddr
;
571 kprintf("cpu_start() cpu: %d\n", cpu
);
573 if (cpu
== cpu_number()) {
575 configure_coresight_registers(cpu_data_ptr
);
577 thread_t first_thread
;
579 cpu_data_ptr
->cpu_reset_handler
= (vm_offset_t
) start_cpu_paddr
;
581 cpu_data_ptr
->cpu_pmap_cpu_data
.cpu_user_pmap
= NULL
;
583 if (cpu_data_ptr
->cpu_processor
->next_thread
!= THREAD_NULL
)
584 first_thread
= cpu_data_ptr
->cpu_processor
->next_thread
;
586 first_thread
= cpu_data_ptr
->cpu_processor
->idle_thread
;
587 cpu_data_ptr
->cpu_active_thread
= first_thread
;
588 first_thread
->machine
.CpuDatap
= cpu_data_ptr
;
590 configure_coresight_registers(cpu_data_ptr
);
592 flush_dcache((vm_offset_t
)&CpuDataEntries
[cpu
], sizeof(cpu_data_entry_t
), FALSE
);
593 flush_dcache((vm_offset_t
)cpu_data_ptr
, sizeof(cpu_data_t
), FALSE
);
594 (void) PE_cpu_start(cpu_data_ptr
->cpu_id
, (vm_offset_t
)NULL
, (vm_offset_t
)NULL
);
602 cpu_timebase_init(boolean_t from_boot
)
604 cpu_data_t
*cdp
= getCpuDatap();
606 if (cdp
->cpu_get_fiq_handler
== NULL
) {
607 cdp
->cpu_get_fiq_handler
= rtclock_timebase_func
.tbd_fiq_handler
;
608 cdp
->cpu_get_decrementer_func
= rtclock_timebase_func
.tbd_get_decrementer
;
609 cdp
->cpu_set_decrementer_func
= rtclock_timebase_func
.tbd_set_decrementer
;
610 cdp
->cpu_tbd_hardware_addr
= (void *)rtclock_timebase_addr
;
611 cdp
->cpu_tbd_hardware_val
= (void *)rtclock_timebase_val
;
614 if (!from_boot
&& (cdp
== &BootCpuData
)) {
616 * When we wake from sleep, we have no guarantee about the state
617 * of the hardware timebase. It may have kept ticking across sleep, or
620 * To deal with this, we calculate an offset to the clock that will
621 * produce a timebase value wake_abstime at the point the boot
622 * CPU calls cpu_timebase_init on wake.
624 * This ensures that mach_absolute_time() stops ticking across sleep.
626 rtclock_base_abstime
= wake_abstime
- ml_get_hwclock();
629 cdp
->cpu_decrementer
= 0x7FFFFFFFUL
;
630 cdp
->cpu_timebase
= 0x0UL
;
631 cdp
->cpu_base_timebase
= rtclock_base_abstime
;
637 return (getCpuDatap()->cpu_cluster_id
);
640 __attribute__((noreturn
))
644 cpu_data_t
*cpu_data_ptr
= getCpuDatap();
646 if (cpu_data_ptr
== &BootCpuData
) {
647 cpu_data_t
*target_cdp
;
651 max_cpu
= ml_get_max_cpu_number();
652 for (cpu
=0; cpu
<= max_cpu
; cpu
++) {
653 target_cdp
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
655 if ((target_cdp
== NULL
) || (target_cdp
== cpu_data_ptr
))
658 while (target_cdp
->cpu_sleep_token
!= ARM_CPU_ON_SLEEP_PATH
);
662 * Now that the other cores have entered the sleep path, set
663 * the abstime value we'll use when we resume.
665 wake_abstime
= ml_get_timebase();
670 cpu_data_ptr
->cpu_sleep_token
= ARM_CPU_ON_SLEEP_PATH
;
672 if (cpu_data_ptr
== &BootCpuData
) {
674 // Classic suspend to RAM writes the suspend signature into the
675 // sleep token buffer so that iBoot knows that it's on the warm
676 // boot (wake) path (as opposed to the cold boot path). Newer SoC
677 // do not go through SecureROM/iBoot on the warm boot path. The
678 // reconfig engine script brings the CPU out of reset at the kernel's
679 // reset vector which points to the warm boot initialization code.
680 if(sleepTokenBuffer
!= (vm_offset_t
) NULL
) {
681 platform_cache_shutdown();
682 bcopy((const void *)suspend_signature
, (void *)sleepTokenBuffer
, sizeof(SleepToken
));
685 panic("No sleep token buffer");
689 #if __ARM_GLOBAL_SLEEP_BIT__
690 /* Allow other CPUs to go to sleep. */
691 arm64_stall_sleep
= FALSE
;
692 __builtin_arm_dmb(DMB_ISH
);
695 /* Architectural debug state: <rdar://problem/12390433>:
696 * Grab debug lock EDLAR and clear bit 0 in EDPRCR,
697 * tell debugger to not prevent power gating .
699 if (cpu_data_ptr
->coresight_base
[CORESIGHT_ED
]) {
700 *(volatile uint32_t *)(cpu_data_ptr
->coresight_base
[CORESIGHT_ED
] + ARM_DEBUG_OFFSET_DBGLAR
) = ARM_DBG_LOCK_ACCESS_KEY
;
701 *(volatile uint32_t *)(cpu_data_ptr
->coresight_base
[CORESIGHT_ED
] + ARM_DEBUG_OFFSET_DBGPRCR
) = 0;
706 #endif /* MONOTONIC */
707 /* ARM64-specific preparation */
708 arm64_prepare_for_sleep();
710 #if __ARM_GLOBAL_SLEEP_BIT__
712 * With the exception of the CPU revisions listed above, our ARM64 CPUs have a
713 * global register to manage entering deep sleep, as opposed to a per-CPU
714 * register. We cannot update this register until all CPUs are ready to enter
715 * deep sleep, because if a CPU executes WFI outside of the deep sleep context
716 * (by idling), it will hang (due to the side effects of enabling deep sleep),
717 * which can hang the sleep process or cause memory corruption on wake.
719 * To avoid these issues, we'll stall on this global value, which CPU0 will
722 while (arm64_stall_sleep
) {
726 CleanPoU_DcacheRegion((vm_offset_t
) cpu_data_ptr
, sizeof(cpu_data_t
));
728 /* Architectural debug state: <rdar://problem/12390433>:
729 * Grab debug lock EDLAR and clear bit 0 in EDPRCR,
730 * tell debugger to not prevent power gating .
732 if (cpu_data_ptr
->coresight_base
[CORESIGHT_ED
]) {
733 *(volatile uint32_t *)(cpu_data_ptr
->coresight_base
[CORESIGHT_ED
] + ARM_DEBUG_OFFSET_DBGLAR
) = ARM_DBG_LOCK_ACCESS_KEY
;
734 *(volatile uint32_t *)(cpu_data_ptr
->coresight_base
[CORESIGHT_ED
] + ARM_DEBUG_OFFSET_DBGPRCR
) = 0;
737 /* ARM64-specific preparation */
738 arm64_prepare_for_sleep();
743 cpu_machine_idle_init(boolean_t from_boot
)
745 static vm_address_t resume_idle_cpu_paddr
= (vm_address_t
)NULL
;
746 cpu_data_t
*cpu_data_ptr
= getCpuDatap();
749 unsigned long jtag
= 0;
751 uint32_t production
= 1;
754 if (PE_parse_boot_argn("jtag", &jtag
, sizeof (jtag
))) {
762 PE_parse_boot_argn("wfi", &wfi_tmp
, sizeof (wfi_tmp
));
764 // bits 7..0 give the wfi type
765 switch (wfi_tmp
& 0xff) {
771 #if DEVELOPMENT || DEBUG
773 // wfi overhead simulation
774 // 31..16 - wfi delay is us
778 wfi_flags
= (wfi_tmp
>> 8) & 0xFF;
779 nanoseconds_to_absolutetime(((wfi_tmp
>> 16) & 0xFFFF) * NSEC_PER_MSEC
, &wfi_delay
);
781 #endif /* DEVELOPMENT || DEBUG */
789 ResetHandlerData
.assist_reset_handler
= 0;
790 ResetHandlerData
.cpu_data_entries
= ml_static_vtop((vm_offset_t
)CpuDataEntries
);
793 monitor_call(MONITOR_SET_ENTRY
, (uintptr_t)ml_static_vtop((vm_offset_t
)&LowResetVectorBase
), 0, 0);
794 #elif !defined(NO_MONITOR)
795 #error MONITOR undefined, WFI power gating may not operate correctly
798 // Determine if we are on production or debug chip
799 if (kSuccess
== DTLookupEntry(NULL
, "/chosen", &entry
)) {
803 if (kSuccess
== DTGetProperty(entry
, "effective-production-status-ap", &prop
, &size
))
805 bcopy(prop
, &production
, size
);
808 #if defined(APPLE_ARM64_ARCH_FAMILY)
809 // Enable coresight debug registers on debug-fused chips
810 coresight_debug_enabled
= TRUE
;
814 start_cpu_paddr
= ml_static_vtop((vm_offset_t
)&start_cpu
);
815 resume_idle_cpu_paddr
= ml_static_vtop((vm_offset_t
)&resume_idle_cpu
);
819 if (cpu_data_ptr
== &BootCpuData
) {
820 static addr64_t SleepToken_low_paddr
= (addr64_t
)NULL
;
821 if (sleepTokenBuffer
!= (vm_offset_t
) NULL
) {
822 SleepToken_low_paddr
= ml_vtophys(sleepTokenBuffer
);
825 panic("No sleep token buffer");
828 bcopy_phys((addr64_t
)ml_static_vtop((vm_offset_t
)running_signature
),
829 SleepToken_low_paddr
, sizeof(SleepToken
));
830 flush_dcache((vm_offset_t
)SleepToken
, sizeof(SleepToken
), TRUE
);
834 cpu_data_ptr
->cpu_reset_handler
= resume_idle_cpu_paddr
;
835 clean_dcache((vm_offset_t
)cpu_data_ptr
, sizeof(cpu_data_t
), FALSE
);
838 _Atomic
uint32_t cpu_idle_count
= 0;
841 machine_track_platform_idle(boolean_t entry
)
844 (void)__c11_atomic_fetch_add(&cpu_idle_count
, 1, __ATOMIC_RELAXED
);
846 (void)__c11_atomic_fetch_sub(&cpu_idle_count
, 1, __ATOMIC_RELAXED
);
851 sleep_token_buffer_init(void)
853 cpu_data_t
*cpu_data_ptr
= getCpuDatap();
858 if ((cpu_data_ptr
== &BootCpuData
) && (sleepTokenBuffer
== (vm_offset_t
) NULL
)) {
859 /* Find the stpage node in the device tree */
860 if (kSuccess
!= DTLookupEntry(0, "stram", &entry
))
863 if (kSuccess
!= DTGetProperty(entry
, "reg", (void **)&prop
, (unsigned int *)&size
))
866 /* Map the page into the kernel space */
867 sleepTokenBuffer
= ml_io_map(((vm_offset_t
*)prop
)[0], ((vm_size_t
*)prop
)[1]);