1 /*
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <pexpert/pexpert.h>
30 #if __arm64__
31 #include <pexpert/arm64/board_config.h>
32 #include <arm64/hv_hvc.h>
33 #endif /* __arm64__ */
34
35 #include <arm/cpuid_internal.h>
36 #include <arm/cpu_capabilities_public.h>
37 #include <arm/pmap.h>
38 #include <arm64/proc_reg.h>
39 #include <machine/machine_cpuid.h>
40 #include <machine/machine_routines.h>
41 #include <vm/vm_protos.h>
42
43
44 #if __arm64__
45
46 void configure_misc_apple_boot_args(void);
47 void configure_misc_apple_regs(bool is_boot_cpu);
48 void configure_timer_apple_regs(void);
49 void configure_late_apple_regs(bool cold_boot);
50
51 void
configure_misc_apple_boot_args(void)52 configure_misc_apple_boot_args(void)
53 {
54 }
55
56
57 void
configure_misc_apple_regs(bool is_boot_cpu)58 configure_misc_apple_regs(bool is_boot_cpu)
59 {
60 #pragma unused(is_boot_cpu)
61
62 }
63
64 // machine_routines_apple.c gets built on non-Apple platforms but it won't
65 // #include apple_arm64_regs.h so some of the constants referenced below
66 // won't exist in those builds
67 #if APPLE_ARM64_ARCH_FAMILY
68
69 static bool
cpu_needs_throttle_tunable(uint32_t midr_pnum)70 cpu_needs_throttle_tunable(uint32_t midr_pnum)
71 {
72 switch (midr_pnum) {
73 #if defined(APPLEAVALANCHE)
74 /* ACCP only */
75 case MIDR_RHODES_DIE_AVALANCHE:
76 return true;
77 #endif /* APPLEAVALANCHE */
78
79 #if defined(APPLEEVEREST)
80 case MIDR_IBIZA_ACCE:
81 case MIDR_IBIZA_ACCP:
82 return true;
83 case MIDR_LOBOS_ACCE:
84 case MIDR_LOBOS_ACCP:
85 return true;
86 case MIDR_PALMA_ACCE:
87 case MIDR_PALMA_ACCP:
88 return true;
89 case MIDR_COLL_ACCE:
90 case MIDR_COLL_ACCP:
91 return true;
92 #endif /* APPLEEVEREST */
93 default:
94 return false;
95 }
96 }
97
98 /*
99 * configure_late_apple_regs()
100 *
101 * Normal tunables (HID bits) are applied early on, in the APPLY_TUNABLES
102 * asm macro. This C function is intended to handle special cases where that
103 * isn't possible, e.g.
104 * - Tunables that require PIO mappings
105 * - Tunables that need access to the parsed CPU topology info
106 *
107 * Unlike configure_misc_apple_regs(), it is guaranteed to execute after
108 * ml_parse_cpu_topology() / ml_map_cpu_pio() are done,
109 * and after cpu_number() is valid.
110 */
111 void
configure_late_apple_regs(bool cold_boot)112 configure_late_apple_regs(bool cold_boot)
113 {
114 const ml_topology_info_t *tinfo = ml_get_topology_info();
115 uint32_t midr_pnum = machine_read_midr() & MIDR_EL1_PNUM_MASK;
116 uint64_t reg_val;
117
118 bool apply_late_pio_regs = cold_boot;
119 #ifdef APPLEEVEREST
120 /*
121 * On H15 CPUs PIO locks are applied early in the non-cold boot
122 * path.
123 */
124 apply_late_pio_regs = 0;
125 #endif
126 if (apply_late_pio_regs) {
127 if (cpu_needs_throttle_tunable(midr_pnum)) {
128 vm_offset_t cpu_impl = tinfo->cpus[cpu_number()].cpu_IMPL_regs;
129 const uint64_t c1pptThrtlRate = 0xb2;
130 reg_val = ml_io_read64(cpu_impl + CORE_THRTL_CFG2_OFFSET);
131 reg_val &= ~CORE_THRTL_CFG2_c1pptThrtlRate_mask;
132 reg_val |= c1pptThrtlRate << CORE_THRTL_CFG2_c1pptThrtlRate_shift;
133 ml_io_write64(cpu_impl + CORE_THRTL_CFG2_OFFSET, reg_val);
134 }
135 }
136
137 #if defined(APPLEAVALANCHE)
138 if (tinfo->max_die_id > 0) {
139 if (midr_pnum == MIDR_RHODES_DIE_AVALANCHE || midr_pnum == MIDR_RHODES_DIE_BLIZZARD) {
140 // rdar://93675127 (Rhodes address match granularity for BIU)
141 reg_val = __builtin_arm_rsr64("HID5");
142 reg_val &= ~ARM64_REG_HID5_BiuBchMatchGran_mask;
143 reg_val |= ARM64_REG_HID5_BiuBchMatchGran_VALUE(0);
144 __builtin_arm_wsr64("HID5", reg_val);
145 }
146 }
147 #endif /* APPLEAVALANCHE */
148
149 #if defined(APPLEEVEREST)
150 #endif /* APPLEEVEREST */
151 }
152 #endif /* APPLE_ARM64_ARCH_FAMILY */
153
154 void
configure_timer_apple_regs(void)155 configure_timer_apple_regs(void)
156 {
157 }
158
159 #endif /* __arm64__ */
160
161 #if HAS_APPLE_PAC
162
163 #if HAS_PARAVIRTUALIZED_PAC
164 static uint64_t vmapple_default_rop_pid;
165 static uint64_t vmapple_default_jop_pid;
166
167 static inline void
vmapple_pac_get_default_keys()168 vmapple_pac_get_default_keys()
169 {
170 static bool initialized = false;
171 if (os_atomic_xchg(&initialized, true, relaxed)) {
172 return;
173 }
174
175 const uint64_t fn = VMAPPLE_PAC_GET_DEFAULT_KEYS;
176 asm volatile (
177 "mov x0, %[fn]" "\n"
178 "hvc #0" "\n"
179 "cbnz x0, ." "\n"
180 "str x2, %[b_key]" "\n"
181 "str x3, %[el0_key]" "\n"
182 : [b_key] "=m"(vmapple_default_rop_pid),
183 [el0_key] "=m"(vmapple_default_jop_pid)
184 : [fn] "r"(fn)
185 : "x0", "x1", "x2", "x3", "x4"
186 );
187 }
188
189 #endif /* HAS_PARAVIRTUALIZED_PAC */
190
191 /**
192 * Returns the default ROP key.
193 */
194 uint64_t
ml_default_rop_pid(void)195 ml_default_rop_pid(void)
196 {
197 #if HAS_PARAVIRTUALIZED_PAC
198 vmapple_pac_get_default_keys();
199 return vmapple_default_rop_pid;
200 #else
201 return 0;
202 #endif /* HAS_PARAVIRTUALIZED_PAC */
203 }
204
205 /**
206 * Returns the default JOP key.
207 */
208 uint64_t
ml_default_jop_pid(void)209 ml_default_jop_pid(void)
210 {
211 #if HAS_PARAVIRTUALIZED_PAC
212 vmapple_pac_get_default_keys();
213 return vmapple_default_jop_pid;
214 #else
215 return 0;
216 #endif /* HAS_PARAVIRTUALIZED_PAC */
217 }
218
219 /**
220 * Returns an appropriate JOP key for non-arm64e userspace processes. The
221 * return value may vary from call to call.
222 */
223 uint64_t
ml_non_arm64e_user_jop_pid(void)224 ml_non_arm64e_user_jop_pid(void)
225 {
226 #if HAS_PARAVIRTUALIZED_PAC
227 return generate_jop_key();
228 #else
229 return 0;
230 #endif /* HAS_PARAVIRTUALIZED_PAC */
231 }
232 #endif /* HAS_APPLE_PAC */
233