2 * Copyright (c) 2017-2019 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@
29 * File: arm/cpu_common.c
31 * cpu routines common to all supported arm variants
34 #include <kern/machine.h>
35 #include <kern/cpu_number.h>
36 #include <kern/thread.h>
37 #include <kern/percpu.h>
38 #include <kern/timer_queue.h>
39 #include <kern/locks.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 <mach/processor_info.h>
49 #include <machine/atomic.h>
50 #include <machine/config.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>
57 #include <arm/proc_reg.h>
58 #include <libkern/OSAtomic.h>
60 SECURITY_READ_ONLY_LATE(struct percpu_base
) percpu_base
;
61 vm_address_t percpu_base_cur
;
62 cpu_data_t
PERCPU_DATA(cpu_data
);
63 cpu_data_entry_t CpuDataEntries
[MAX_CPUS
];
65 static lck_grp_t cpu_lck_grp
;
66 static lck_rw_t cpu_state_lock
;
68 unsigned int real_ncpus
= 1;
69 boolean_t idle_enable
= FALSE
;
70 uint64_t wake_abstime
= 0x0ULL
;
73 extern unsigned int gFastIPI
;
74 #endif /* defined(HAS_IPI) */
79 assert(cpu
<= ml_get_max_cpu_number());
80 return CpuDataEntries
[cpu
].cpu_data_vaddr
;
84 cpu_control(int slot_num
,
85 processor_info_t info
,
88 printf("cpu_control(%d,%p,%d) not implemented\n",
89 slot_num
, info
, count
);
94 cpu_info_count(processor_flavor_t flavor
,
98 case PROCESSOR_CPU_STAT
:
99 *count
= PROCESSOR_CPU_STAT_COUNT
;
102 case PROCESSOR_CPU_STAT64
:
103 *count
= PROCESSOR_CPU_STAT64_COUNT
;
113 cpu_info(processor_flavor_t flavor
, int slot_num
, processor_info_t info
,
116 cpu_data_t
*cpu_data_ptr
= CpuDataEntries
[slot_num
].cpu_data_vaddr
;
119 case PROCESSOR_CPU_STAT
:
121 if (*count
< PROCESSOR_CPU_STAT_COUNT
) {
125 processor_cpu_stat_t cpu_stat
= (processor_cpu_stat_t
)info
;
126 cpu_stat
->irq_ex_cnt
= (uint32_t)cpu_data_ptr
->cpu_stat
.irq_ex_cnt
;
127 cpu_stat
->ipi_cnt
= (uint32_t)cpu_data_ptr
->cpu_stat
.ipi_cnt
;
128 cpu_stat
->timer_cnt
= (uint32_t)cpu_data_ptr
->cpu_stat
.timer_cnt
;
129 cpu_stat
->undef_ex_cnt
= (uint32_t)cpu_data_ptr
->cpu_stat
.undef_ex_cnt
;
130 cpu_stat
->unaligned_cnt
= (uint32_t)cpu_data_ptr
->cpu_stat
.unaligned_cnt
;
131 cpu_stat
->vfp_cnt
= (uint32_t)cpu_data_ptr
->cpu_stat
.vfp_cnt
;
132 cpu_stat
->vfp_shortv_cnt
= 0;
133 cpu_stat
->data_ex_cnt
= (uint32_t)cpu_data_ptr
->cpu_stat
.data_ex_cnt
;
134 cpu_stat
->instr_ex_cnt
= (uint32_t)cpu_data_ptr
->cpu_stat
.instr_ex_cnt
;
136 *count
= PROCESSOR_CPU_STAT_COUNT
;
141 case PROCESSOR_CPU_STAT64
:
143 if (*count
< PROCESSOR_CPU_STAT64_COUNT
) {
147 processor_cpu_stat64_t cpu_stat
= (processor_cpu_stat64_t
)info
;
148 cpu_stat
->irq_ex_cnt
= cpu_data_ptr
->cpu_stat
.irq_ex_cnt
;
149 cpu_stat
->ipi_cnt
= cpu_data_ptr
->cpu_stat
.ipi_cnt
;
150 cpu_stat
->timer_cnt
= cpu_data_ptr
->cpu_stat
.timer_cnt
;
151 cpu_stat
->undef_ex_cnt
= cpu_data_ptr
->cpu_stat
.undef_ex_cnt
;
152 cpu_stat
->unaligned_cnt
= cpu_data_ptr
->cpu_stat
.unaligned_cnt
;
153 cpu_stat
->vfp_cnt
= cpu_data_ptr
->cpu_stat
.vfp_cnt
;
154 cpu_stat
->vfp_shortv_cnt
= 0;
155 cpu_stat
->data_ex_cnt
= cpu_data_ptr
->cpu_stat
.data_ex_cnt
;
156 cpu_stat
->instr_ex_cnt
= cpu_data_ptr
->cpu_stat
.instr_ex_cnt
;
158 cpu_stat
->pmi_cnt
= cpu_data_ptr
->cpu_monotonic
.mtc_npmis
;
159 #endif /* MONOTONIC */
161 *count
= PROCESSOR_CPU_STAT64_COUNT
;
172 * Routine: cpu_doshutdown
176 cpu_doshutdown(void (*doshutdown
)(processor_t
),
177 processor_t processor
)
179 doshutdown(processor
);
183 * Routine: cpu_idle_tickle
187 cpu_idle_tickle(void)
190 cpu_data_t
*cpu_data_ptr
;
191 uint64_t new_idle_timeout_ticks
= 0x0ULL
;
193 intr
= ml_set_interrupts_enabled(FALSE
);
194 cpu_data_ptr
= getCpuDatap();
196 if (cpu_data_ptr
->idle_timer_notify
!= NULL
) {
197 cpu_data_ptr
->idle_timer_notify(cpu_data_ptr
->idle_timer_refcon
, &new_idle_timeout_ticks
);
198 if (new_idle_timeout_ticks
!= 0x0ULL
) {
199 /* if a new idle timeout was requested set the new idle timer deadline */
200 clock_absolutetime_interval_to_deadline(new_idle_timeout_ticks
, &cpu_data_ptr
->idle_timer_deadline
);
202 /* turn off the idle timer */
203 cpu_data_ptr
->idle_timer_deadline
= 0x0ULL
;
205 timer_resync_deadlines();
207 (void) ml_set_interrupts_enabled(intr
);
211 cpu_handle_xcall(cpu_data_t
*cpu_data_ptr
)
216 os_atomic_thread_fence(acquire
);
217 /* Come back around if cpu_signal_internal is running on another CPU and has just
218 * added SIGPxcall to the pending mask, but hasn't yet assigned the call params.*/
219 if (cpu_data_ptr
->cpu_xcall_p0
!= NULL
&& cpu_data_ptr
->cpu_xcall_p1
!= NULL
) {
220 xfunc
= cpu_data_ptr
->cpu_xcall_p0
;
221 INTERRUPT_MASKED_DEBUG_START(xfunc
, DBG_INTR_TYPE_IPI
);
222 xparam
= cpu_data_ptr
->cpu_xcall_p1
;
223 cpu_data_ptr
->cpu_xcall_p0
= NULL
;
224 cpu_data_ptr
->cpu_xcall_p1
= NULL
;
225 os_atomic_thread_fence(acq_rel
);
226 os_atomic_andnot(&cpu_data_ptr
->cpu_signal
, SIGPxcall
, relaxed
);
228 INTERRUPT_MASKED_DEBUG_END();
230 if (cpu_data_ptr
->cpu_imm_xcall_p0
!= NULL
&& cpu_data_ptr
->cpu_imm_xcall_p1
!= NULL
) {
231 xfunc
= cpu_data_ptr
->cpu_imm_xcall_p0
;
232 INTERRUPT_MASKED_DEBUG_START(xfunc
, DBG_INTR_TYPE_IPI
);
233 xparam
= cpu_data_ptr
->cpu_imm_xcall_p1
;
234 cpu_data_ptr
->cpu_imm_xcall_p0
= NULL
;
235 cpu_data_ptr
->cpu_imm_xcall_p1
= NULL
;
236 os_atomic_thread_fence(acq_rel
);
237 os_atomic_andnot(&cpu_data_ptr
->cpu_signal
, SIGPxcallImm
, relaxed
);
239 INTERRUPT_MASKED_DEBUG_END();
244 cpu_broadcast_xcall_internal(unsigned int signal
,
246 boolean_t self_xcall
,
251 cpu_data_t
*cpu_data_ptr
;
252 cpu_data_t
*target_cpu_datap
;
253 unsigned int failsig
;
255 int max_cpu
= ml_get_max_cpu_number() + 1;
257 //yes, param ALSO cannot be NULL
261 intr
= ml_set_interrupts_enabled(FALSE
);
262 cpu_data_ptr
= getCpuDatap();
268 assert_wait((event_t
)synch
, THREAD_UNINT
);
271 for (cpu
= 0; cpu
< max_cpu
; cpu
++) {
272 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
274 if (target_cpu_datap
== cpu_data_ptr
) {
278 if ((target_cpu_datap
== NULL
) ||
279 KERN_SUCCESS
!= cpu_signal(target_cpu_datap
, signal
, (void *)func
, parm
)) {
289 (void) ml_set_interrupts_enabled(intr
);
292 if (os_atomic_sub(synch
, (!self_xcall
) ? failsig
+ 1 : failsig
, relaxed
) == 0) {
293 clear_wait(current_thread(), THREAD_AWAKENED
);
295 thread_block(THREAD_CONTINUE_NULL
);
300 return max_cpu
- failsig
- 1;
302 return max_cpu
- failsig
;
307 cpu_broadcast_xcall(uint32_t *synch
,
308 boolean_t self_xcall
,
312 return cpu_broadcast_xcall_internal(SIGPxcall
, synch
, self_xcall
, func
, parm
);
315 struct cpu_broadcast_xcall_simple_data
{
322 cpu_broadcast_xcall_simple_cbk(void *parm
)
324 struct cpu_broadcast_xcall_simple_data
*data
= (struct cpu_broadcast_xcall_simple_data
*)parm
;
326 data
->func(data
->parm
);
328 if (os_atomic_dec(&data
->sync
, relaxed
) == 0) {
329 thread_wakeup((event_t
)&data
->sync
);
334 cpu_xcall_simple(boolean_t self_xcall
,
339 struct cpu_broadcast_xcall_simple_data data
= {};
344 return cpu_broadcast_xcall_internal(immediate
? SIGPxcallImm
: SIGPxcall
, &data
.sync
, self_xcall
, cpu_broadcast_xcall_simple_cbk
, &data
);
348 cpu_broadcast_immediate_xcall(uint32_t *synch
,
349 boolean_t self_xcall
,
353 return cpu_broadcast_xcall_internal(SIGPxcallImm
, synch
, self_xcall
, func
, parm
);
357 cpu_broadcast_xcall_simple(boolean_t self_xcall
,
361 return cpu_xcall_simple(self_xcall
, func
, parm
, false);
365 cpu_broadcast_immediate_xcall_simple(boolean_t self_xcall
,
369 return cpu_xcall_simple(self_xcall
, func
, parm
, true);
373 cpu_xcall_internal(unsigned int signal
, int cpu_number
, broadcastFunc func
, void *param
)
375 cpu_data_t
*target_cpu_datap
;
377 if ((cpu_number
< 0) || (cpu_number
> ml_get_max_cpu_number())) {
378 return KERN_INVALID_ARGUMENT
;
381 if (func
== NULL
|| param
== NULL
) {
382 return KERN_INVALID_ARGUMENT
;
385 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu_number
].cpu_data_vaddr
;
386 if (target_cpu_datap
== NULL
) {
387 return KERN_INVALID_ARGUMENT
;
390 return cpu_signal(target_cpu_datap
, signal
, (void*)func
, param
);
394 cpu_xcall(int cpu_number
, broadcastFunc func
, void *param
)
396 return cpu_xcall_internal(SIGPxcall
, cpu_number
, func
, param
);
400 cpu_immediate_xcall(int cpu_number
, broadcastFunc func
, void *param
)
402 return cpu_xcall_internal(SIGPxcallImm
, cpu_number
, func
, param
);
406 cpu_signal_internal(cpu_data_t
*target_proc
,
412 unsigned int Check_SIGPdisabled
;
414 Boolean swap_success
;
415 boolean_t interruptible
= ml_set_interrupts_enabled(FALSE
);
416 cpu_data_t
*current_proc
= getCpuDatap();
418 /* We'll mandate that only IPIs meant to kick a core out of idle may ever be deferred. */
420 assert(signal
== SIGPnop
);
423 if (current_proc
!= target_proc
) {
424 Check_SIGPdisabled
= SIGPdisabled
;
426 Check_SIGPdisabled
= 0;
429 if ((signal
== SIGPxcall
) || (signal
== SIGPxcallImm
)) {
431 current_signals
= target_proc
->cpu_signal
;
432 if ((current_signals
& SIGPdisabled
) == SIGPdisabled
) {
433 ml_set_interrupts_enabled(interruptible
);
436 swap_success
= OSCompareAndSwap(current_signals
& (~signal
), current_signals
| signal
,
437 &target_proc
->cpu_signal
);
439 if (!swap_success
&& (signal
== SIGPxcallImm
) && (target_proc
->cpu_signal
& SIGPxcallImm
)) {
440 ml_set_interrupts_enabled(interruptible
);
441 return KERN_ALREADY_WAITING
;
444 /* Drain pending xcalls on this cpu; the CPU we're trying to xcall may in turn
445 * be trying to xcall us. Since we have interrupts disabled that can deadlock,
446 * so break the deadlock by draining pending xcalls. */
447 if (!swap_success
&& (current_proc
->cpu_signal
& signal
)) {
448 cpu_handle_xcall(current_proc
);
450 } while (!swap_success
);
452 if (signal
== SIGPxcallImm
) {
453 target_proc
->cpu_imm_xcall_p0
= p0
;
454 target_proc
->cpu_imm_xcall_p1
= p1
;
456 target_proc
->cpu_xcall_p0
= p0
;
457 target_proc
->cpu_xcall_p1
= p1
;
461 current_signals
= target_proc
->cpu_signal
;
462 if ((Check_SIGPdisabled
!= 0) && (current_signals
& Check_SIGPdisabled
) == SIGPdisabled
) {
463 ml_set_interrupts_enabled(interruptible
);
467 swap_success
= OSCompareAndSwap(current_signals
, current_signals
| signal
,
468 &target_proc
->cpu_signal
);
469 } while (!swap_success
);
473 * Issue DSB here to guarantee: 1) prior stores to pending signal mask and xcall params
474 * will be visible to other cores when the IPI is dispatched, and 2) subsequent
475 * instructions to signal the other cores will not execute until after the barrier.
476 * DMB would be sufficient to guarantee 1) but not 2).
478 __builtin_arm_dsb(DSB_ISH
);
480 if (!(target_proc
->cpu_signal
& SIGPdisabled
)) {
484 ml_cpu_signal_deferred(target_proc
->cpu_phys_id
);
486 PE_cpu_signal_deferred(getCpuDatap()->cpu_id
, target_proc
->cpu_id
);
489 PE_cpu_signal_deferred(getCpuDatap()->cpu_id
, target_proc
->cpu_id
);
490 #endif /* defined(HAS_IPI) */
494 ml_cpu_signal(target_proc
->cpu_phys_id
);
496 PE_cpu_signal(getCpuDatap()->cpu_id
, target_proc
->cpu_id
);
499 PE_cpu_signal(getCpuDatap()->cpu_id
, target_proc
->cpu_id
);
500 #endif /* defined(HAS_IPI) */
504 ml_set_interrupts_enabled(interruptible
);
509 cpu_signal(cpu_data_t
*target_proc
,
514 return cpu_signal_internal(target_proc
, signal
, p0
, p1
, FALSE
);
518 cpu_signal_deferred(cpu_data_t
*target_proc
)
520 return cpu_signal_internal(target_proc
, SIGPnop
, NULL
, NULL
, TRUE
);
524 cpu_signal_cancel(cpu_data_t
*target_proc
)
526 /* TODO: Should we care about the state of a core as far as squashing deferred IPIs goes? */
527 if (!(target_proc
->cpu_signal
& SIGPdisabled
)) {
530 ml_cpu_signal_retract(target_proc
->cpu_phys_id
);
532 PE_cpu_signal_cancel(getCpuDatap()->cpu_id
, target_proc
->cpu_id
);
535 PE_cpu_signal_cancel(getCpuDatap()->cpu_id
, target_proc
->cpu_id
);
536 #endif /* defined(HAS_IPI) */
541 cpu_signal_handler(void)
543 cpu_signal_handler_internal(FALSE
);
547 cpu_signal_handler_internal(boolean_t disable_signal
)
549 cpu_data_t
*cpu_data_ptr
= getCpuDatap();
550 unsigned int cpu_signal
;
552 cpu_data_ptr
->cpu_stat
.ipi_cnt
++;
553 cpu_data_ptr
->cpu_stat
.ipi_cnt_wake
++;
554 SCHED_STATS_INC(ipi_count
);
556 cpu_signal
= os_atomic_or(&cpu_data_ptr
->cpu_signal
, 0, relaxed
);
558 if ((!(cpu_signal
& SIGPdisabled
)) && (disable_signal
== TRUE
)) {
559 os_atomic_or(&cpu_data_ptr
->cpu_signal
, SIGPdisabled
, relaxed
);
560 } else if ((cpu_signal
& SIGPdisabled
) && (disable_signal
== FALSE
)) {
561 os_atomic_andnot(&cpu_data_ptr
->cpu_signal
, SIGPdisabled
, relaxed
);
564 while (cpu_signal
& ~SIGPdisabled
) {
565 if (cpu_signal
& SIGPdec
) {
566 os_atomic_andnot(&cpu_data_ptr
->cpu_signal
, SIGPdec
, relaxed
);
567 INTERRUPT_MASKED_DEBUG_START(rtclock_intr
, DBG_INTR_TYPE_IPI
);
569 INTERRUPT_MASKED_DEBUG_END();
572 if (cpu_signal
& SIGPkppet
) {
573 os_atomic_andnot(&cpu_data_ptr
->cpu_signal
, SIGPkppet
, relaxed
);
574 extern void kperf_signal_handler(void);
575 INTERRUPT_MASKED_DEBUG_START(kperf_signal_handler
, DBG_INTR_TYPE_IPI
);
576 kperf_signal_handler();
577 INTERRUPT_MASKED_DEBUG_END();
580 if (cpu_signal
& (SIGPxcall
| SIGPxcallImm
)) {
581 cpu_handle_xcall(cpu_data_ptr
);
583 if (cpu_signal
& SIGPast
) {
584 os_atomic_andnot(&cpu_data_ptr
->cpu_signal
, SIGPast
, relaxed
);
585 INTERRUPT_MASKED_DEBUG_START(ast_check
, DBG_INTR_TYPE_IPI
);
586 ast_check(current_processor());
587 INTERRUPT_MASKED_DEBUG_END();
589 if (cpu_signal
& SIGPdebug
) {
590 os_atomic_andnot(&cpu_data_ptr
->cpu_signal
, SIGPdebug
, relaxed
);
591 INTERRUPT_MASKED_DEBUG_START(DebuggerXCall
, DBG_INTR_TYPE_IPI
);
592 DebuggerXCall(cpu_data_ptr
->cpu_int_state
);
593 INTERRUPT_MASKED_DEBUG_END();
596 if (cpu_signal
& SIGPLWFlush
) {
597 os_atomic_andnot(&cpu_data_ptr
->cpu_signal
, SIGPLWFlush
, relaxed
);
598 INTERRUPT_MASKED_DEBUG_START(cache_xcall_handler
, DBG_INTR_TYPE_IPI
);
599 cache_xcall_handler(LWFlush
);
600 INTERRUPT_MASKED_DEBUG_END();
602 if (cpu_signal
& SIGPLWClean
) {
603 os_atomic_andnot(&cpu_data_ptr
->cpu_signal
, SIGPLWClean
, relaxed
);
604 INTERRUPT_MASKED_DEBUG_START(cache_xcall_handler
, DBG_INTR_TYPE_IPI
);
605 cache_xcall_handler(LWClean
);
606 INTERRUPT_MASKED_DEBUG_END();
610 cpu_signal
= os_atomic_or(&cpu_data_ptr
->cpu_signal
, 0, relaxed
);
615 cpu_exit_wait(int cpu_id
)
618 if (!ml_is_quiescing()) {
619 // For runtime disable (non S2R) the CPU will shut down immediately.
620 ml_topology_cpu_t
*cpu
= &ml_get_topology_info()->cpus
[cpu_id
];
621 assert(cpu
&& cpu
->cpu_IMPL_regs
);
622 volatile uint64_t *cpu_sts
= (void *)(cpu
->cpu_IMPL_regs
+ CPU_PIO_CPU_STS_OFFSET
);
624 // Poll the "CPU running state" field until it is 0 (off)
625 while ((*cpu_sts
& CPU_PIO_CPU_STS_cpuRunSt_mask
) != 0x00) {
626 __builtin_arm_dsb(DSB_ISH
);
630 #endif /* USE_APPLEARMSMP */
632 if (cpu_id
!= master_cpu
) {
633 // For S2R, ml_arm_sleep() will do some extra polling after setting ARM_CPU_ON_SLEEP_PATH.
634 cpu_data_t
*cpu_data_ptr
;
636 cpu_data_ptr
= CpuDataEntries
[cpu_id
].cpu_data_vaddr
;
637 while (!((*(volatile unsigned int*)&cpu_data_ptr
->cpu_sleep_token
) == ARM_CPU_ON_SLEEP_PATH
)) {
644 cpu_can_exit(__unused
int cpu
)
650 cpu_machine_init(void)
652 static boolean_t started
= FALSE
;
653 cpu_data_t
*cpu_data_ptr
;
655 cpu_data_ptr
= getCpuDatap();
656 started
= ((cpu_data_ptr
->cpu_flags
& StartedState
) == StartedState
);
657 if (cpu_data_ptr
->cpu_cache_dispatch
!= NULL
) {
658 platform_cache_init();
661 /* Note: this calls IOCPURunPlatformActiveActions when resuming on boot cpu */
662 PE_cpu_machine_init(cpu_data_ptr
->cpu_id
, !started
);
664 cpu_data_ptr
->cpu_flags
|= StartedState
;
669 current_processor(void)
671 return PERCPU_GET(processor
);
675 cpu_to_processor(int cpu
)
677 cpu_data_t
*cpu_data
= cpu_datap(cpu
);
678 if (cpu_data
!= NULL
) {
679 return PERCPU_GET_RELATIVE(processor
, cpu_data
, cpu_data
);
686 processor_to_cpu_datap(processor_t processor
)
688 assert(processor
->cpu_id
<= ml_get_max_cpu_number());
689 assert(CpuDataEntries
[processor
->cpu_id
].cpu_data_vaddr
!= NULL
);
691 return PERCPU_GET_RELATIVE(cpu_data
, processor
, processor
);
696 cpu_data_startup_init(void)
698 vm_size_t size
= percpu_section_size() * (ml_get_cpu_count() - 1);
700 percpu_base
.size
= percpu_section_size();
701 if (ml_get_cpu_count() == 1) {
702 percpu_base
.start
= VM_MAX_KERNEL_ADDRESS
;
707 * The memory needs to be physically contiguous because it contains
708 * cpu_data_t structures sometimes accessed during reset
711 * kmem_alloc_contig() can't be used early, at the time STARTUP_SUB_PERCPU
712 * normally runs, so we instead steal the memory for the PERCPU subsystem
715 percpu_base
.start
= (vm_offset_t
)pmap_steal_memory(round_page(size
));
716 bzero((void *)percpu_base
.start
, round_page(size
));
718 percpu_base
.start
-= percpu_section_start();
719 percpu_base
.end
= percpu_base
.start
+ size
- 1;
720 percpu_base_cur
= percpu_base
.start
;
722 STARTUP(PMAP_STEAL
, STARTUP_RANK_FIRST
, cpu_data_startup_init
);
725 cpu_data_alloc(boolean_t is_boot_cpu
)
727 cpu_data_t
*cpu_data_ptr
= NULL
;
731 cpu_data_ptr
= PERCPU_GET_MASTER(cpu_data
);
733 base
= os_atomic_add_orig(&percpu_base_cur
,
734 percpu_section_size(), relaxed
);
736 cpu_data_ptr
= PERCPU_GET_WITH_BASE(base
, cpu_data
);
737 cpu_stack_alloc(cpu_data_ptr
);
746 return &getCpuDatap()->cpu_pending_ast
;
750 slot_type(int slot_num
)
752 return cpu_datap(slot_num
)->cpu_type
;
756 slot_subtype(int slot_num
)
758 return cpu_datap(slot_num
)->cpu_subtype
;
762 slot_threadtype(int slot_num
)
764 return cpu_datap(slot_num
)->cpu_threadtype
;
770 return getCpuDatap()->cpu_type
;
776 return getCpuDatap()->cpu_subtype
;
782 return getCpuDatap()->cpu_threadtype
;
788 return getCpuDatap()->cpu_number
;
792 current_percpu_base(void)
794 return current_thread()->machine
.pcpu_data_base
;
798 ml_get_wake_timebase(void)
804 ml_cpu_signal_is_enabled(void)
806 return !(getCpuDatap()->cpu_signal
& SIGPdisabled
);
810 ml_cpu_can_exit(__unused
int cpu_id
)
812 /* processor_exit() is always allowed on the S2R path */
813 if (ml_is_quiescing()) {
816 #if HAS_CLUSTER && USE_APPLEARMSMP
818 * Cyprus and newer chips can disable individual non-boot CPUs. The
819 * implementation polls cpuX_IMPL_CPU_STS, which differs on older chips.
821 if (CpuDataEntries
[cpu_id
].cpu_data_vaddr
!= &BootCpuData
) {
829 ml_cpu_init_state(void)
831 lck_grp_init(&cpu_lck_grp
, "cpu_lck_grp", LCK_GRP_ATTR_NULL
);
832 lck_rw_init(&cpu_state_lock
, &cpu_lck_grp
, LCK_ATTR_NULL
);
835 #ifdef USE_APPLEARMSMP
838 ml_cpu_begin_state_transition(int cpu_id
)
840 lck_rw_lock_exclusive(&cpu_state_lock
);
841 CpuDataEntries
[cpu_id
].cpu_data_vaddr
->in_state_transition
= true;
842 lck_rw_unlock_exclusive(&cpu_state_lock
);
846 ml_cpu_end_state_transition(int cpu_id
)
848 lck_rw_lock_exclusive(&cpu_state_lock
);
849 CpuDataEntries
[cpu_id
].cpu_data_vaddr
->in_state_transition
= false;
850 lck_rw_unlock_exclusive(&cpu_state_lock
);
854 ml_cpu_begin_loop(void)
856 lck_rw_lock_shared(&cpu_state_lock
);
860 ml_cpu_end_loop(void)
862 lck_rw_unlock_shared(&cpu_state_lock
);
865 #else /* USE_APPLEARMSMP */
868 ml_cpu_begin_state_transition(__unused
int cpu_id
)
873 ml_cpu_end_state_transition(__unused
int cpu_id
)
878 ml_cpu_begin_loop(void)
883 ml_cpu_end_loop(void)
887 #endif /* USE_APPLEARMSMP */