Thomas Gleixner | f50a7f3 | 2019-05-28 09:57:18 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2017, Nicholas Piggin, IBM Corporation |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #define pr_fmt(fmt) "dt-cpu-ftrs: " fmt |
| 7 | |
| 8 | #include <linux/export.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/jump_label.h> |
Nicholas Piggin | a2b05b7 | 2017-05-11 21:24:41 +1000 | [diff] [blame] | 11 | #include <linux/libfdt.h> |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 12 | #include <linux/memblock.h> |
| 13 | #include <linux/printk.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/threads.h> |
| 17 | |
| 18 | #include <asm/cputable.h> |
| 19 | #include <asm/dt_cpu_ftrs.h> |
| 20 | #include <asm/mmu.h> |
| 21 | #include <asm/oprofile_impl.h> |
| 22 | #include <asm/prom.h> |
| 23 | #include <asm/setup.h> |
| 24 | |
| 25 | |
| 26 | /* Device-tree visible constants follow */ |
| 27 | #define ISA_V2_07B 2070 |
| 28 | #define ISA_V3_0B 3000 |
Alistair Popple | 43d0d37 | 2020-05-21 11:43:37 +1000 | [diff] [blame] | 29 | #define ISA_V3_1 3100 |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 30 | |
| 31 | #define USABLE_PR (1U << 0) |
| 32 | #define USABLE_OS (1U << 1) |
| 33 | #define USABLE_HV (1U << 2) |
| 34 | |
| 35 | #define HV_SUPPORT_HFSCR (1U << 0) |
| 36 | #define OS_SUPPORT_FSCR (1U << 0) |
| 37 | |
| 38 | /* For parsing, we define all bits set as "NONE" case */ |
| 39 | #define HV_SUPPORT_NONE 0xffffffffU |
| 40 | #define OS_SUPPORT_NONE 0xffffffffU |
| 41 | |
| 42 | struct dt_cpu_feature { |
| 43 | const char *name; |
| 44 | uint32_t isa; |
| 45 | uint32_t usable_privilege; |
| 46 | uint32_t hv_support; |
| 47 | uint32_t os_support; |
| 48 | uint32_t hfscr_bit_nr; |
| 49 | uint32_t fscr_bit_nr; |
| 50 | uint32_t hwcap_bit_nr; |
| 51 | /* fdt parsing */ |
| 52 | unsigned long node; |
| 53 | int enabled; |
| 54 | int disabled; |
| 55 | }; |
| 56 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 57 | #define MMU_FTRS_HASH_BASE (MMU_FTRS_POWER8) |
| 58 | |
| 59 | #define COMMON_USER_BASE (PPC_FEATURE_32 | PPC_FEATURE_64 | \ |
| 60 | PPC_FEATURE_ARCH_2_06 |\ |
| 61 | PPC_FEATURE_ICACHE_SNOOP) |
| 62 | #define COMMON_USER2_BASE (PPC_FEATURE2_ARCH_2_07 | \ |
| 63 | PPC_FEATURE2_ISEL) |
| 64 | /* |
| 65 | * Set up the base CPU |
| 66 | */ |
| 67 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 68 | extern long __machine_check_early_realmode_p8(struct pt_regs *regs); |
| 69 | extern long __machine_check_early_realmode_p9(struct pt_regs *regs); |
| 70 | |
| 71 | static int hv_mode; |
| 72 | |
| 73 | static struct { |
| 74 | u64 lpcr; |
Nicholas Piggin | a57ac41 | 2018-04-05 15:50:49 +1000 | [diff] [blame] | 75 | u64 lpcr_clear; |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 76 | u64 hfscr; |
| 77 | u64 fscr; |
| 78 | } system_registers; |
| 79 | |
| 80 | static void (*init_pmu_registers)(void); |
| 81 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 82 | static void __restore_cpu_cpufeatures(void) |
| 83 | { |
Nicholas Piggin | a57ac41 | 2018-04-05 15:50:49 +1000 | [diff] [blame] | 84 | u64 lpcr; |
| 85 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 86 | /* |
| 87 | * LPCR is restored by the power on engine already. It can be changed |
| 88 | * after early init e.g., by radix enable, and we have no unified API |
| 89 | * for saving and restoring such SPRs. |
| 90 | * |
| 91 | * This ->restore hook should really be removed from idle and register |
| 92 | * restore moved directly into the idle restore code, because this code |
| 93 | * doesn't know how idle is implemented or what it needs restored here. |
| 94 | * |
| 95 | * The best we can do to accommodate secondary boot and idle restore |
| 96 | * for now is "or" LPCR with existing. |
| 97 | */ |
Nicholas Piggin | a57ac41 | 2018-04-05 15:50:49 +1000 | [diff] [blame] | 98 | lpcr = mfspr(SPRN_LPCR); |
| 99 | lpcr |= system_registers.lpcr; |
| 100 | lpcr &= ~system_registers.lpcr_clear; |
| 101 | mtspr(SPRN_LPCR, lpcr); |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 102 | if (hv_mode) { |
| 103 | mtspr(SPRN_LPID, 0); |
| 104 | mtspr(SPRN_HFSCR, system_registers.hfscr); |
Jordan Niethe | 13c7bb3 | 2019-09-17 10:46:05 +1000 | [diff] [blame] | 105 | mtspr(SPRN_PCR, PCR_MASK); |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 106 | } |
| 107 | mtspr(SPRN_FSCR, system_registers.fscr); |
| 108 | |
| 109 | if (init_pmu_registers) |
| 110 | init_pmu_registers(); |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static char dt_cpu_name[64]; |
| 114 | |
| 115 | static struct cpu_spec __initdata base_cpu_spec = { |
| 116 | .cpu_name = NULL, |
Michael Ellerman | 81b654c | 2018-04-12 22:24:45 +1000 | [diff] [blame] | 117 | .cpu_features = CPU_FTRS_DT_CPU_BASE, |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 118 | .cpu_user_features = COMMON_USER_BASE, |
| 119 | .cpu_user_features2 = COMMON_USER2_BASE, |
| 120 | .mmu_features = 0, |
| 121 | .icache_bsize = 32, /* minimum block size, fixed by */ |
| 122 | .dcache_bsize = 32, /* cache info init. */ |
| 123 | .num_pmcs = 0, |
| 124 | .pmc_type = PPC_PMC_DEFAULT, |
| 125 | .oprofile_cpu_type = NULL, |
| 126 | .oprofile_type = PPC_OPROFILE_INVALID, |
| 127 | .cpu_setup = NULL, |
| 128 | .cpu_restore = __restore_cpu_cpufeatures, |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 129 | .machine_check_early = NULL, |
| 130 | .platform = NULL, |
| 131 | }; |
| 132 | |
| 133 | static void __init cpufeatures_setup_cpu(void) |
| 134 | { |
| 135 | set_cur_cpu_spec(&base_cpu_spec); |
| 136 | |
| 137 | cur_cpu_spec->pvr_mask = -1; |
| 138 | cur_cpu_spec->pvr_value = mfspr(SPRN_PVR); |
| 139 | |
| 140 | /* Initialize the base environment -- clear FSCR/HFSCR. */ |
| 141 | hv_mode = !!(mfmsr() & MSR_HV); |
| 142 | if (hv_mode) { |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 143 | cur_cpu_spec->cpu_features |= CPU_FTR_HVMODE; |
| 144 | mtspr(SPRN_HFSCR, 0); |
| 145 | } |
| 146 | mtspr(SPRN_FSCR, 0); |
Jordan Niethe | 13c7bb3 | 2019-09-17 10:46:05 +1000 | [diff] [blame] | 147 | mtspr(SPRN_PCR, PCR_MASK); |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * LPCR does not get cleared, to match behaviour with secondaries |
| 151 | * in __restore_cpu_cpufeatures. Once the idle code is fixed, this |
| 152 | * could clear LPCR too. |
| 153 | */ |
| 154 | } |
| 155 | |
| 156 | static int __init feat_try_enable_unknown(struct dt_cpu_feature *f) |
| 157 | { |
| 158 | if (f->hv_support == HV_SUPPORT_NONE) { |
| 159 | } else if (f->hv_support & HV_SUPPORT_HFSCR) { |
| 160 | u64 hfscr = mfspr(SPRN_HFSCR); |
| 161 | hfscr |= 1UL << f->hfscr_bit_nr; |
| 162 | mtspr(SPRN_HFSCR, hfscr); |
| 163 | } else { |
| 164 | /* Does not have a known recipe */ |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | if (f->os_support == OS_SUPPORT_NONE) { |
| 169 | } else if (f->os_support & OS_SUPPORT_FSCR) { |
| 170 | u64 fscr = mfspr(SPRN_FSCR); |
| 171 | fscr |= 1UL << f->fscr_bit_nr; |
| 172 | mtspr(SPRN_FSCR, fscr); |
| 173 | } else { |
| 174 | /* Does not have a known recipe */ |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | if ((f->usable_privilege & USABLE_PR) && (f->hwcap_bit_nr != -1)) { |
| 179 | uint32_t word = f->hwcap_bit_nr / 32; |
| 180 | uint32_t bit = f->hwcap_bit_nr % 32; |
| 181 | |
| 182 | if (word == 0) |
| 183 | cur_cpu_spec->cpu_user_features |= 1U << bit; |
| 184 | else if (word == 1) |
| 185 | cur_cpu_spec->cpu_user_features2 |= 1U << bit; |
| 186 | else |
| 187 | pr_err("%s could not advertise to user (no hwcap bits)\n", f->name); |
| 188 | } |
| 189 | |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | static int __init feat_enable(struct dt_cpu_feature *f) |
| 194 | { |
| 195 | if (f->hv_support != HV_SUPPORT_NONE) { |
| 196 | if (f->hfscr_bit_nr != -1) { |
| 197 | u64 hfscr = mfspr(SPRN_HFSCR); |
| 198 | hfscr |= 1UL << f->hfscr_bit_nr; |
| 199 | mtspr(SPRN_HFSCR, hfscr); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (f->os_support != OS_SUPPORT_NONE) { |
| 204 | if (f->fscr_bit_nr != -1) { |
| 205 | u64 fscr = mfspr(SPRN_FSCR); |
| 206 | fscr |= 1UL << f->fscr_bit_nr; |
| 207 | mtspr(SPRN_FSCR, fscr); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if ((f->usable_privilege & USABLE_PR) && (f->hwcap_bit_nr != -1)) { |
| 212 | uint32_t word = f->hwcap_bit_nr / 32; |
| 213 | uint32_t bit = f->hwcap_bit_nr % 32; |
| 214 | |
| 215 | if (word == 0) |
| 216 | cur_cpu_spec->cpu_user_features |= 1U << bit; |
| 217 | else if (word == 1) |
| 218 | cur_cpu_spec->cpu_user_features2 |= 1U << bit; |
| 219 | else |
| 220 | pr_err("CPU feature: %s could not advertise to user (no hwcap bits)\n", f->name); |
| 221 | } |
| 222 | |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | static int __init feat_disable(struct dt_cpu_feature *f) |
| 227 | { |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int __init feat_enable_hv(struct dt_cpu_feature *f) |
| 232 | { |
| 233 | u64 lpcr; |
| 234 | |
| 235 | if (!hv_mode) { |
| 236 | pr_err("CPU feature hypervisor present in device tree but HV mode not enabled in the CPU. Ignoring.\n"); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | mtspr(SPRN_LPID, 0); |
| 241 | |
| 242 | lpcr = mfspr(SPRN_LPCR); |
| 243 | lpcr &= ~LPCR_LPES0; /* HV external interrupts */ |
| 244 | mtspr(SPRN_LPCR, lpcr); |
| 245 | |
| 246 | cur_cpu_spec->cpu_features |= CPU_FTR_HVMODE; |
| 247 | |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | static int __init feat_enable_le(struct dt_cpu_feature *f) |
| 252 | { |
| 253 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_TRUE_LE; |
| 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | static int __init feat_enable_smt(struct dt_cpu_feature *f) |
| 258 | { |
| 259 | cur_cpu_spec->cpu_features |= CPU_FTR_SMT; |
| 260 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_SMT; |
| 261 | return 1; |
| 262 | } |
| 263 | |
| 264 | static int __init feat_enable_idle_nap(struct dt_cpu_feature *f) |
| 265 | { |
| 266 | u64 lpcr; |
| 267 | |
| 268 | /* Set PECE wakeup modes for ISA 207 */ |
| 269 | lpcr = mfspr(SPRN_LPCR); |
| 270 | lpcr |= LPCR_PECE0; |
| 271 | lpcr |= LPCR_PECE1; |
| 272 | lpcr |= LPCR_PECE2; |
| 273 | mtspr(SPRN_LPCR, lpcr); |
| 274 | |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | static int __init feat_enable_align_dsisr(struct dt_cpu_feature *f) |
| 279 | { |
| 280 | cur_cpu_spec->cpu_features &= ~CPU_FTR_NODSISRALIGN; |
| 281 | |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | static int __init feat_enable_idle_stop(struct dt_cpu_feature *f) |
| 286 | { |
| 287 | u64 lpcr; |
| 288 | |
| 289 | /* Set PECE wakeup modes for ISAv3.0B */ |
| 290 | lpcr = mfspr(SPRN_LPCR); |
| 291 | lpcr |= LPCR_PECE0; |
| 292 | lpcr |= LPCR_PECE1; |
| 293 | lpcr |= LPCR_PECE2; |
| 294 | mtspr(SPRN_LPCR, lpcr); |
| 295 | |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | static int __init feat_enable_mmu_hash(struct dt_cpu_feature *f) |
| 300 | { |
| 301 | u64 lpcr; |
| 302 | |
| 303 | lpcr = mfspr(SPRN_LPCR); |
| 304 | lpcr &= ~LPCR_ISL; |
| 305 | |
| 306 | /* VRMASD */ |
| 307 | lpcr |= LPCR_VPM0; |
| 308 | lpcr &= ~LPCR_VPM1; |
| 309 | lpcr |= 0x10UL << LPCR_VRMASD_SH; /* L=1 LP=00 */ |
| 310 | mtspr(SPRN_LPCR, lpcr); |
| 311 | |
| 312 | cur_cpu_spec->mmu_features |= MMU_FTRS_HASH_BASE; |
| 313 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_MMU; |
| 314 | |
| 315 | return 1; |
| 316 | } |
| 317 | |
| 318 | static int __init feat_enable_mmu_hash_v3(struct dt_cpu_feature *f) |
| 319 | { |
| 320 | u64 lpcr; |
| 321 | |
Nicholas Piggin | a57ac41 | 2018-04-05 15:50:49 +1000 | [diff] [blame] | 322 | system_registers.lpcr_clear |= (LPCR_ISL | LPCR_UPRT | LPCR_HR); |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 323 | lpcr = mfspr(SPRN_LPCR); |
Nicholas Piggin | a57ac41 | 2018-04-05 15:50:49 +1000 | [diff] [blame] | 324 | lpcr &= ~(LPCR_ISL | LPCR_UPRT | LPCR_HR); |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 325 | mtspr(SPRN_LPCR, lpcr); |
| 326 | |
| 327 | cur_cpu_spec->mmu_features |= MMU_FTRS_HASH_BASE; |
| 328 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_MMU; |
| 329 | |
| 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | static int __init feat_enable_mmu_radix(struct dt_cpu_feature *f) |
| 335 | { |
| 336 | #ifdef CONFIG_PPC_RADIX_MMU |
| 337 | cur_cpu_spec->mmu_features |= MMU_FTR_TYPE_RADIX; |
| 338 | cur_cpu_spec->mmu_features |= MMU_FTRS_HASH_BASE; |
| 339 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_MMU; |
| 340 | |
| 341 | return 1; |
| 342 | #endif |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static int __init feat_enable_dscr(struct dt_cpu_feature *f) |
| 347 | { |
| 348 | u64 lpcr; |
| 349 | |
Michael Ellerman | 993e3d9 | 2020-05-28 00:58:41 +1000 | [diff] [blame] | 350 | /* |
| 351 | * Linux relies on FSCR[DSCR] being clear, so that we can take the |
| 352 | * facility unavailable interrupt and track the task's usage of DSCR. |
| 353 | * See facility_unavailable_exception(). |
| 354 | * Clear the bit here so that feat_enable() doesn't set it. |
| 355 | */ |
| 356 | f->fscr_bit_nr = -1; |
| 357 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 358 | feat_enable(f); |
| 359 | |
| 360 | lpcr = mfspr(SPRN_LPCR); |
| 361 | lpcr &= ~LPCR_DPFD; |
| 362 | lpcr |= (4UL << LPCR_DPFD_SH); |
| 363 | mtspr(SPRN_LPCR, lpcr); |
| 364 | |
| 365 | return 1; |
| 366 | } |
| 367 | |
| 368 | static void hfscr_pmu_enable(void) |
| 369 | { |
| 370 | u64 hfscr = mfspr(SPRN_HFSCR); |
| 371 | hfscr |= PPC_BIT(60); |
| 372 | mtspr(SPRN_HFSCR, hfscr); |
| 373 | } |
| 374 | |
| 375 | static void init_pmu_power8(void) |
| 376 | { |
| 377 | if (hv_mode) { |
| 378 | mtspr(SPRN_MMCRC, 0); |
| 379 | mtspr(SPRN_MMCRH, 0); |
| 380 | } |
| 381 | |
| 382 | mtspr(SPRN_MMCRA, 0); |
| 383 | mtspr(SPRN_MMCR0, 0); |
| 384 | mtspr(SPRN_MMCR1, 0); |
| 385 | mtspr(SPRN_MMCR2, 0); |
| 386 | mtspr(SPRN_MMCRS, 0); |
| 387 | } |
| 388 | |
| 389 | static int __init feat_enable_mce_power8(struct dt_cpu_feature *f) |
| 390 | { |
| 391 | cur_cpu_spec->platform = "power8"; |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 392 | cur_cpu_spec->machine_check_early = __machine_check_early_realmode_p8; |
| 393 | |
| 394 | return 1; |
| 395 | } |
| 396 | |
| 397 | static int __init feat_enable_pmu_power8(struct dt_cpu_feature *f) |
| 398 | { |
| 399 | hfscr_pmu_enable(); |
| 400 | |
| 401 | init_pmu_power8(); |
| 402 | init_pmu_registers = init_pmu_power8; |
| 403 | |
| 404 | cur_cpu_spec->cpu_features |= CPU_FTR_MMCRA; |
| 405 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_PSERIES_PERFMON_COMPAT; |
| 406 | if (pvr_version_is(PVR_POWER8E)) |
| 407 | cur_cpu_spec->cpu_features |= CPU_FTR_PMAO_BUG; |
| 408 | |
| 409 | cur_cpu_spec->num_pmcs = 6; |
| 410 | cur_cpu_spec->pmc_type = PPC_PMC_IBM; |
| 411 | cur_cpu_spec->oprofile_cpu_type = "ppc64/power8"; |
| 412 | |
| 413 | return 1; |
| 414 | } |
| 415 | |
| 416 | static void init_pmu_power9(void) |
| 417 | { |
| 418 | if (hv_mode) |
| 419 | mtspr(SPRN_MMCRC, 0); |
| 420 | |
| 421 | mtspr(SPRN_MMCRA, 0); |
| 422 | mtspr(SPRN_MMCR0, 0); |
| 423 | mtspr(SPRN_MMCR1, 0); |
| 424 | mtspr(SPRN_MMCR2, 0); |
| 425 | } |
| 426 | |
| 427 | static int __init feat_enable_mce_power9(struct dt_cpu_feature *f) |
| 428 | { |
| 429 | cur_cpu_spec->platform = "power9"; |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 430 | cur_cpu_spec->machine_check_early = __machine_check_early_realmode_p9; |
| 431 | |
| 432 | return 1; |
| 433 | } |
| 434 | |
| 435 | static int __init feat_enable_pmu_power9(struct dt_cpu_feature *f) |
| 436 | { |
| 437 | hfscr_pmu_enable(); |
| 438 | |
| 439 | init_pmu_power9(); |
| 440 | init_pmu_registers = init_pmu_power9; |
| 441 | |
| 442 | cur_cpu_spec->cpu_features |= CPU_FTR_MMCRA; |
| 443 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_PSERIES_PERFMON_COMPAT; |
| 444 | |
| 445 | cur_cpu_spec->num_pmcs = 6; |
| 446 | cur_cpu_spec->pmc_type = PPC_PMC_IBM; |
| 447 | cur_cpu_spec->oprofile_cpu_type = "ppc64/power9"; |
| 448 | |
| 449 | return 1; |
| 450 | } |
| 451 | |
| 452 | static int __init feat_enable_tm(struct dt_cpu_feature *f) |
| 453 | { |
| 454 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 455 | feat_enable(f); |
| 456 | cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_HTM_NOSC; |
| 457 | return 1; |
| 458 | #endif |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int __init feat_enable_fp(struct dt_cpu_feature *f) |
| 463 | { |
| 464 | feat_enable(f); |
| 465 | cur_cpu_spec->cpu_features &= ~CPU_FTR_FPU_UNAVAILABLE; |
| 466 | |
| 467 | return 1; |
| 468 | } |
| 469 | |
| 470 | static int __init feat_enable_vector(struct dt_cpu_feature *f) |
| 471 | { |
| 472 | #ifdef CONFIG_ALTIVEC |
| 473 | feat_enable(f); |
| 474 | cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC; |
| 475 | cur_cpu_spec->cpu_features |= CPU_FTR_VMX_COPY; |
| 476 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC; |
| 477 | |
| 478 | return 1; |
| 479 | #endif |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static int __init feat_enable_vsx(struct dt_cpu_feature *f) |
| 484 | { |
| 485 | #ifdef CONFIG_VSX |
| 486 | feat_enable(f); |
| 487 | cur_cpu_spec->cpu_features |= CPU_FTR_VSX; |
| 488 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_VSX; |
| 489 | |
| 490 | return 1; |
| 491 | #endif |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int __init feat_enable_purr(struct dt_cpu_feature *f) |
| 496 | { |
| 497 | cur_cpu_spec->cpu_features |= CPU_FTR_PURR | CPU_FTR_SPURR; |
| 498 | |
| 499 | return 1; |
| 500 | } |
| 501 | |
| 502 | static int __init feat_enable_ebb(struct dt_cpu_feature *f) |
| 503 | { |
| 504 | /* |
| 505 | * PPC_FEATURE2_EBB is enabled in PMU init code because it has |
| 506 | * historically been related to the PMU facility. This may have |
| 507 | * to be decoupled if EBB becomes more generic. For now, follow |
| 508 | * existing convention. |
| 509 | */ |
| 510 | f->hwcap_bit_nr = -1; |
| 511 | feat_enable(f); |
| 512 | |
| 513 | return 1; |
| 514 | } |
| 515 | |
| 516 | static int __init feat_enable_dbell(struct dt_cpu_feature *f) |
| 517 | { |
| 518 | u64 lpcr; |
| 519 | |
| 520 | /* P9 has an HFSCR for privileged state */ |
| 521 | feat_enable(f); |
| 522 | |
| 523 | cur_cpu_spec->cpu_features |= CPU_FTR_DBELL; |
| 524 | |
| 525 | lpcr = mfspr(SPRN_LPCR); |
| 526 | lpcr |= LPCR_PECEDH; /* hyp doorbell wakeup */ |
| 527 | mtspr(SPRN_LPCR, lpcr); |
| 528 | |
| 529 | return 1; |
| 530 | } |
| 531 | |
| 532 | static int __init feat_enable_hvi(struct dt_cpu_feature *f) |
| 533 | { |
| 534 | u64 lpcr; |
| 535 | |
| 536 | /* |
| 537 | * POWER9 XIVE interrupts including in OPAL XICS compatibility |
| 538 | * are always delivered as hypervisor virtualization interrupts (HVI) |
| 539 | * rather than EE. |
| 540 | * |
| 541 | * However LPES0 is not set here, in the chance that an EE does get |
| 542 | * delivered to the host somehow, the EE handler would not expect it |
| 543 | * to be delivered in LPES0 mode (e.g., using SRR[01]). This could |
| 544 | * happen if there is a bug in interrupt controller code, or IC is |
| 545 | * misconfigured in systemsim. |
| 546 | */ |
| 547 | |
| 548 | lpcr = mfspr(SPRN_LPCR); |
| 549 | lpcr |= LPCR_HVICE; /* enable hvi interrupts */ |
| 550 | lpcr |= LPCR_HEIC; /* disable ee interrupts when MSR_HV */ |
| 551 | lpcr |= LPCR_PECE_HVEE; /* hvi can wake from stop */ |
| 552 | mtspr(SPRN_LPCR, lpcr); |
| 553 | |
| 554 | return 1; |
| 555 | } |
| 556 | |
| 557 | static int __init feat_enable_large_ci(struct dt_cpu_feature *f) |
| 558 | { |
| 559 | cur_cpu_spec->mmu_features |= MMU_FTR_CI_LARGE_PAGE; |
| 560 | |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | struct dt_cpu_feature_match { |
| 565 | const char *name; |
| 566 | int (*enable)(struct dt_cpu_feature *f); |
| 567 | u64 cpu_ftr_bit_mask; |
| 568 | }; |
| 569 | |
| 570 | static struct dt_cpu_feature_match __initdata |
| 571 | dt_cpu_feature_match_table[] = { |
| 572 | {"hypervisor", feat_enable_hv, 0}, |
| 573 | {"big-endian", feat_enable, 0}, |
| 574 | {"little-endian", feat_enable_le, CPU_FTR_REAL_LE}, |
| 575 | {"smt", feat_enable_smt, 0}, |
| 576 | {"interrupt-facilities", feat_enable, 0}, |
| 577 | {"timer-facilities", feat_enable, 0}, |
| 578 | {"timer-facilities-v3", feat_enable, 0}, |
| 579 | {"debug-facilities", feat_enable, 0}, |
| 580 | {"come-from-address-register", feat_enable, CPU_FTR_CFAR}, |
| 581 | {"branch-tracing", feat_enable, 0}, |
| 582 | {"floating-point", feat_enable_fp, 0}, |
| 583 | {"vector", feat_enable_vector, 0}, |
| 584 | {"vector-scalar", feat_enable_vsx, 0}, |
| 585 | {"vector-scalar-v3", feat_enable, 0}, |
| 586 | {"decimal-floating-point", feat_enable, 0}, |
| 587 | {"decimal-integer", feat_enable, 0}, |
| 588 | {"quadword-load-store", feat_enable, 0}, |
| 589 | {"vector-crypto", feat_enable, 0}, |
| 590 | {"mmu-hash", feat_enable_mmu_hash, 0}, |
| 591 | {"mmu-radix", feat_enable_mmu_radix, 0}, |
| 592 | {"mmu-hash-v3", feat_enable_mmu_hash_v3, 0}, |
| 593 | {"virtual-page-class-key-protection", feat_enable, 0}, |
| 594 | {"transactional-memory", feat_enable_tm, CPU_FTR_TM}, |
| 595 | {"transactional-memory-v3", feat_enable_tm, 0}, |
Paul Mackerras | b5af4f2 | 2018-03-21 21:31:59 +1100 | [diff] [blame] | 596 | {"tm-suspend-hypervisor-assist", feat_enable, CPU_FTR_P9_TM_HV_ASSIST}, |
| 597 | {"tm-suspend-xer-so-bug", feat_enable, CPU_FTR_P9_TM_XER_SO_BUG}, |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 598 | {"idle-nap", feat_enable_idle_nap, 0}, |
| 599 | {"alignment-interrupt-dsisr", feat_enable_align_dsisr, 0}, |
| 600 | {"idle-stop", feat_enable_idle_stop, 0}, |
| 601 | {"machine-check-power8", feat_enable_mce_power8, 0}, |
| 602 | {"performance-monitor-power8", feat_enable_pmu_power8, 0}, |
| 603 | {"data-stream-control-register", feat_enable_dscr, CPU_FTR_DSCR}, |
| 604 | {"event-based-branch", feat_enable_ebb, 0}, |
| 605 | {"target-address-register", feat_enable, 0}, |
| 606 | {"branch-history-rolling-buffer", feat_enable, 0}, |
| 607 | {"control-register", feat_enable, CPU_FTR_CTRL}, |
| 608 | {"processor-control-facility", feat_enable_dbell, CPU_FTR_DBELL}, |
| 609 | {"processor-control-facility-v3", feat_enable_dbell, CPU_FTR_DBELL}, |
| 610 | {"processor-utilization-of-resources-register", feat_enable_purr, 0}, |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 611 | {"no-execute", feat_enable, 0}, |
| 612 | {"strong-access-ordering", feat_enable, CPU_FTR_SAO}, |
| 613 | {"cache-inhibited-large-page", feat_enable_large_ci, 0}, |
Michael Ellerman | c1807e3 | 2017-10-19 15:08:19 +1100 | [diff] [blame] | 614 | {"coprocessor-icswx", feat_enable, 0}, |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 615 | {"hypervisor-virtualization-interrupt", feat_enable_hvi, 0}, |
| 616 | {"program-priority-register", feat_enable, CPU_FTR_HAS_PPR}, |
| 617 | {"wait", feat_enable, 0}, |
| 618 | {"atomic-memory-operations", feat_enable, 0}, |
| 619 | {"branch-v3", feat_enable, 0}, |
| 620 | {"copy-paste", feat_enable, 0}, |
| 621 | {"decimal-floating-point-v3", feat_enable, 0}, |
| 622 | {"decimal-integer-v3", feat_enable, 0}, |
| 623 | {"fixed-point-v3", feat_enable, 0}, |
| 624 | {"floating-point-v3", feat_enable, 0}, |
| 625 | {"group-start-register", feat_enable, 0}, |
| 626 | {"pc-relative-addressing", feat_enable, 0}, |
| 627 | {"machine-check-power9", feat_enable_mce_power9, 0}, |
| 628 | {"performance-monitor-power9", feat_enable_pmu_power9, 0}, |
| 629 | {"event-based-branch-v3", feat_enable, 0}, |
| 630 | {"random-number-generator", feat_enable, 0}, |
| 631 | {"system-call-vectored", feat_disable, 0}, |
| 632 | {"trace-interrupt-v3", feat_enable, 0}, |
| 633 | {"vector-v3", feat_enable, 0}, |
| 634 | {"vector-binary128", feat_enable, 0}, |
| 635 | {"vector-binary16", feat_enable, 0}, |
| 636 | {"wait-v3", feat_enable, 0}, |
Alistair Popple | c63d688 | 2020-05-21 11:43:39 +1000 | [diff] [blame^] | 637 | {"prefix-instructions", feat_enable, 0}, |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 638 | }; |
| 639 | |
Nicholas Piggin | a2b05b7 | 2017-05-11 21:24:41 +1000 | [diff] [blame] | 640 | static bool __initdata using_dt_cpu_ftrs; |
| 641 | static bool __initdata enable_unknown = true; |
| 642 | |
| 643 | static int __init dt_cpu_ftrs_parse(char *str) |
| 644 | { |
| 645 | if (!str) |
| 646 | return 0; |
| 647 | |
| 648 | if (!strcmp(str, "off")) |
| 649 | using_dt_cpu_ftrs = false; |
| 650 | else if (!strcmp(str, "known")) |
| 651 | enable_unknown = false; |
| 652 | else |
| 653 | return 1; |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | early_param("dt_cpu_ftrs", dt_cpu_ftrs_parse); |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 658 | |
| 659 | static void __init cpufeatures_setup_start(u32 isa) |
| 660 | { |
| 661 | pr_info("setup for ISA %d\n", isa); |
| 662 | |
| 663 | if (isa >= 3000) { |
| 664 | cur_cpu_spec->cpu_features |= CPU_FTR_ARCH_300; |
| 665 | cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_ARCH_3_00; |
| 666 | } |
Alistair Popple | 43d0d37 | 2020-05-21 11:43:37 +1000 | [diff] [blame] | 667 | |
| 668 | if (isa >= 3100) { |
| 669 | cur_cpu_spec->cpu_features |= CPU_FTR_ARCH_31; |
| 670 | cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_ARCH_3_1; |
| 671 | } |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static bool __init cpufeatures_process_feature(struct dt_cpu_feature *f) |
| 675 | { |
| 676 | const struct dt_cpu_feature_match *m; |
| 677 | bool known = false; |
| 678 | int i; |
| 679 | |
| 680 | for (i = 0; i < ARRAY_SIZE(dt_cpu_feature_match_table); i++) { |
| 681 | m = &dt_cpu_feature_match_table[i]; |
| 682 | if (!strcmp(f->name, m->name)) { |
| 683 | known = true; |
Michael Ellerman | 8cfaf10 | 2019-02-11 11:20:01 +1100 | [diff] [blame] | 684 | if (m->enable(f)) { |
| 685 | cur_cpu_spec->cpu_features |= m->cpu_ftr_bit_mask; |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 686 | break; |
Michael Ellerman | 8cfaf10 | 2019-02-11 11:20:01 +1100 | [diff] [blame] | 687 | } |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 688 | |
| 689 | pr_info("not enabling: %s (disabled or unsupported by kernel)\n", |
| 690 | f->name); |
| 691 | return false; |
| 692 | } |
| 693 | } |
| 694 | |
Michael Ellerman | 8cfaf10 | 2019-02-11 11:20:01 +1100 | [diff] [blame] | 695 | if (!known && (!enable_unknown || !feat_try_enable_unknown(f))) { |
| 696 | pr_info("not enabling: %s (unknown and unsupported by kernel)\n", |
| 697 | f->name); |
| 698 | return false; |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 699 | } |
| 700 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 701 | if (known) |
| 702 | pr_debug("enabling: %s\n", f->name); |
| 703 | else |
| 704 | pr_debug("enabling: %s (unknown)\n", f->name); |
| 705 | |
| 706 | return true; |
| 707 | } |
| 708 | |
Aneesh Kumar K.V | 677733e | 2019-09-24 09:22:51 +0530 | [diff] [blame] | 709 | /* |
| 710 | * Handle POWER9 broadcast tlbie invalidation issue using |
| 711 | * cpu feature flag. |
| 712 | */ |
| 713 | static __init void update_tlbie_feature_flag(unsigned long pvr) |
| 714 | { |
| 715 | if (PVR_VER(pvr) == PVR_POWER9) { |
| 716 | /* |
| 717 | * Set the tlbie feature flag for anything below |
| 718 | * Nimbus DD 2.3 and Cumulus DD 1.3 |
| 719 | */ |
| 720 | if ((pvr & 0xe000) == 0) { |
| 721 | /* Nimbus */ |
| 722 | if ((pvr & 0xfff) < 0x203) |
Aneesh Kumar K.V | 09ce98c | 2019-09-24 09:22:52 +0530 | [diff] [blame] | 723 | cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_STQ_BUG; |
Aneesh Kumar K.V | 677733e | 2019-09-24 09:22:51 +0530 | [diff] [blame] | 724 | } else if ((pvr & 0xc000) == 0) { |
| 725 | /* Cumulus */ |
| 726 | if ((pvr & 0xfff) < 0x103) |
Aneesh Kumar K.V | 09ce98c | 2019-09-24 09:22:52 +0530 | [diff] [blame] | 727 | cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_STQ_BUG; |
Aneesh Kumar K.V | 677733e | 2019-09-24 09:22:51 +0530 | [diff] [blame] | 728 | } else { |
| 729 | WARN_ONCE(1, "Unknown PVR"); |
Aneesh Kumar K.V | 09ce98c | 2019-09-24 09:22:52 +0530 | [diff] [blame] | 730 | cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_STQ_BUG; |
Aneesh Kumar K.V | 677733e | 2019-09-24 09:22:51 +0530 | [diff] [blame] | 731 | } |
Aneesh Kumar K.V | 047e657 | 2019-09-24 09:22:53 +0530 | [diff] [blame] | 732 | |
| 733 | cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_ERAT_BUG; |
Aneesh Kumar K.V | 677733e | 2019-09-24 09:22:51 +0530 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 737 | static __init void cpufeatures_cpu_quirks(void) |
| 738 | { |
Aneesh Kumar K.V | 677733e | 2019-09-24 09:22:51 +0530 | [diff] [blame] | 739 | unsigned long version = mfspr(SPRN_PVR); |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 740 | |
| 741 | /* |
| 742 | * Not all quirks can be derived from the cpufeatures device tree. |
| 743 | */ |
Jordan Niethe | 736bcdd | 2019-12-06 14:17:22 +1100 | [diff] [blame] | 744 | if ((version & 0xffffefff) == 0x004e0200) { |
| 745 | /* DD2.0 has no feature flag */ |
| 746 | cur_cpu_spec->cpu_features |= CPU_FTR_P9_RADIX_PREFETCH_BUG; |
| 747 | } else if ((version & 0xffffefff) == 0x004e0201) { |
Michael Ellerman | 4d6c51b | 2017-11-22 23:17:01 +1100 | [diff] [blame] | 748 | cur_cpu_spec->cpu_features |= CPU_FTR_POWER9_DD2_1; |
Jordan Niethe | 736bcdd | 2019-12-06 14:17:22 +1100 | [diff] [blame] | 749 | cur_cpu_spec->cpu_features |= CPU_FTR_P9_RADIX_PREFETCH_BUG; |
| 750 | } else if ((version & 0xffffefff) == 0x004e0202) { |
Nicholas Piggin | 9e9626e | 2018-02-21 05:08:27 +1000 | [diff] [blame] | 751 | cur_cpu_spec->cpu_features |= CPU_FTR_P9_TM_HV_ASSIST; |
| 752 | cur_cpu_spec->cpu_features |= CPU_FTR_P9_TM_XER_SO_BUG; |
| 753 | cur_cpu_spec->cpu_features |= CPU_FTR_POWER9_DD2_1; |
Jordan Niethe | 736bcdd | 2019-12-06 14:17:22 +1100 | [diff] [blame] | 754 | } else if ((version & 0xffff0000) == 0x004e0000) { |
Michael Ellerman | 749a027 | 2018-06-13 23:23:56 +1000 | [diff] [blame] | 755 | /* DD2.1 and up have DD2_1 */ |
Nicholas Piggin | 9e9626e | 2018-02-21 05:08:27 +1000 | [diff] [blame] | 756 | cur_cpu_spec->cpu_features |= CPU_FTR_POWER9_DD2_1; |
Jordan Niethe | 736bcdd | 2019-12-06 14:17:22 +1100 | [diff] [blame] | 757 | } |
Michael Neuling | 622aa35 | 2018-03-27 15:37:23 +1100 | [diff] [blame] | 758 | |
Michael Ellerman | 95dff48 | 2018-03-28 22:59:50 +1100 | [diff] [blame] | 759 | if ((version & 0xffff0000) == 0x004e0000) { |
Michael Neuling | 622aa35 | 2018-03-27 15:37:23 +1100 | [diff] [blame] | 760 | cur_cpu_spec->cpu_features &= ~(CPU_FTR_DAWR); |
Alastair D'Silva | 8198442 | 2018-05-11 16:12:57 +1000 | [diff] [blame] | 761 | cur_cpu_spec->cpu_features |= CPU_FTR_P9_TIDR; |
Michael Ellerman | 95dff48 | 2018-03-28 22:59:50 +1100 | [diff] [blame] | 762 | } |
Nicholas Piggin | c130153 | 2018-04-05 15:57:54 +1000 | [diff] [blame] | 763 | |
Aneesh Kumar K.V | 677733e | 2019-09-24 09:22:51 +0530 | [diff] [blame] | 764 | update_tlbie_feature_flag(version); |
Nicholas Piggin | c130153 | 2018-04-05 15:57:54 +1000 | [diff] [blame] | 765 | /* |
| 766 | * PKEY was not in the initial base or feature node |
| 767 | * specification, but it should become optional in the next |
| 768 | * cpu feature version sequence. |
| 769 | */ |
| 770 | cur_cpu_spec->cpu_features |= CPU_FTR_PKEY; |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static void __init cpufeatures_setup_finished(void) |
| 774 | { |
| 775 | cpufeatures_cpu_quirks(); |
| 776 | |
| 777 | if (hv_mode && !(cur_cpu_spec->cpu_features & CPU_FTR_HVMODE)) { |
| 778 | pr_err("hypervisor not present in device tree but HV mode is enabled in the CPU. Enabling.\n"); |
| 779 | cur_cpu_spec->cpu_features |= CPU_FTR_HVMODE; |
| 780 | } |
| 781 | |
Michael Ellerman | e4b7990 | 2018-03-13 15:58:11 +1100 | [diff] [blame] | 782 | /* Make sure powerpc_base_platform is non-NULL */ |
| 783 | powerpc_base_platform = cur_cpu_spec->platform; |
| 784 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 785 | system_registers.lpcr = mfspr(SPRN_LPCR); |
| 786 | system_registers.hfscr = mfspr(SPRN_HFSCR); |
| 787 | system_registers.fscr = mfspr(SPRN_FSCR); |
| 788 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 789 | pr_info("final cpu/mmu features = 0x%016lx 0x%08x\n", |
| 790 | cur_cpu_spec->cpu_features, cur_cpu_spec->mmu_features); |
| 791 | } |
| 792 | |
Nicholas Piggin | a2b05b7 | 2017-05-11 21:24:41 +1000 | [diff] [blame] | 793 | static int __init disabled_on_cmdline(void) |
| 794 | { |
| 795 | unsigned long root, chosen; |
| 796 | const char *p; |
| 797 | |
| 798 | root = of_get_flat_dt_root(); |
| 799 | chosen = of_get_flat_dt_subnode_by_name(root, "chosen"); |
| 800 | if (chosen == -FDT_ERR_NOTFOUND) |
| 801 | return false; |
| 802 | |
| 803 | p = of_get_flat_dt_prop(chosen, "bootargs", NULL); |
| 804 | if (!p) |
| 805 | return false; |
| 806 | |
| 807 | if (strstr(p, "dt_cpu_ftrs=off")) |
| 808 | return true; |
| 809 | |
| 810 | return false; |
| 811 | } |
| 812 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 813 | static int __init fdt_find_cpu_features(unsigned long node, const char *uname, |
| 814 | int depth, void *data) |
| 815 | { |
| 816 | if (of_flat_dt_is_compatible(node, "ibm,powerpc-cpu-features") |
| 817 | && of_get_flat_dt_prop(node, "isa", NULL)) |
| 818 | return 1; |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 823 | bool __init dt_cpu_ftrs_in_use(void) |
| 824 | { |
| 825 | return using_dt_cpu_ftrs; |
| 826 | } |
| 827 | |
| 828 | bool __init dt_cpu_ftrs_init(void *fdt) |
| 829 | { |
Nicholas Piggin | a2b05b7 | 2017-05-11 21:24:41 +1000 | [diff] [blame] | 830 | using_dt_cpu_ftrs = false; |
| 831 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 832 | /* Setup and verify the FDT, if it fails we just bail */ |
| 833 | if (!early_init_dt_verify(fdt)) |
| 834 | return false; |
| 835 | |
| 836 | if (!of_scan_flat_dt(fdt_find_cpu_features, NULL)) |
| 837 | return false; |
| 838 | |
Nicholas Piggin | a2b05b7 | 2017-05-11 21:24:41 +1000 | [diff] [blame] | 839 | if (disabled_on_cmdline()) |
| 840 | return false; |
| 841 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 842 | cpufeatures_setup_cpu(); |
| 843 | |
| 844 | using_dt_cpu_ftrs = true; |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | static int nr_dt_cpu_features; |
| 849 | static struct dt_cpu_feature *dt_cpu_features; |
| 850 | |
| 851 | static int __init process_cpufeatures_node(unsigned long node, |
| 852 | const char *uname, int i) |
| 853 | { |
| 854 | const __be32 *prop; |
| 855 | struct dt_cpu_feature *f; |
| 856 | int len; |
| 857 | |
| 858 | f = &dt_cpu_features[i]; |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 859 | |
| 860 | f->node = node; |
| 861 | |
| 862 | f->name = uname; |
| 863 | |
| 864 | prop = of_get_flat_dt_prop(node, "isa", &len); |
| 865 | if (!prop) { |
| 866 | pr_warn("%s: missing isa property\n", uname); |
| 867 | return 0; |
| 868 | } |
| 869 | f->isa = be32_to_cpup(prop); |
| 870 | |
| 871 | prop = of_get_flat_dt_prop(node, "usable-privilege", &len); |
| 872 | if (!prop) { |
| 873 | pr_warn("%s: missing usable-privilege property", uname); |
| 874 | return 0; |
| 875 | } |
| 876 | f->usable_privilege = be32_to_cpup(prop); |
| 877 | |
| 878 | prop = of_get_flat_dt_prop(node, "hv-support", &len); |
| 879 | if (prop) |
| 880 | f->hv_support = be32_to_cpup(prop); |
| 881 | else |
| 882 | f->hv_support = HV_SUPPORT_NONE; |
| 883 | |
| 884 | prop = of_get_flat_dt_prop(node, "os-support", &len); |
| 885 | if (prop) |
| 886 | f->os_support = be32_to_cpup(prop); |
| 887 | else |
| 888 | f->os_support = OS_SUPPORT_NONE; |
| 889 | |
| 890 | prop = of_get_flat_dt_prop(node, "hfscr-bit-nr", &len); |
| 891 | if (prop) |
| 892 | f->hfscr_bit_nr = be32_to_cpup(prop); |
| 893 | else |
| 894 | f->hfscr_bit_nr = -1; |
| 895 | prop = of_get_flat_dt_prop(node, "fscr-bit-nr", &len); |
| 896 | if (prop) |
| 897 | f->fscr_bit_nr = be32_to_cpup(prop); |
| 898 | else |
| 899 | f->fscr_bit_nr = -1; |
| 900 | prop = of_get_flat_dt_prop(node, "hwcap-bit-nr", &len); |
| 901 | if (prop) |
| 902 | f->hwcap_bit_nr = be32_to_cpup(prop); |
| 903 | else |
| 904 | f->hwcap_bit_nr = -1; |
| 905 | |
| 906 | if (f->usable_privilege & USABLE_HV) { |
| 907 | if (!(mfmsr() & MSR_HV)) { |
| 908 | pr_warn("%s: HV feature passed to guest\n", uname); |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | if (f->hv_support == HV_SUPPORT_NONE && f->hfscr_bit_nr != -1) { |
| 913 | pr_warn("%s: unwanted hfscr_bit_nr\n", uname); |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | if (f->hv_support == HV_SUPPORT_HFSCR) { |
| 918 | if (f->hfscr_bit_nr == -1) { |
| 919 | pr_warn("%s: missing hfscr_bit_nr\n", uname); |
| 920 | return 0; |
| 921 | } |
| 922 | } |
| 923 | } else { |
| 924 | if (f->hv_support != HV_SUPPORT_NONE || f->hfscr_bit_nr != -1) { |
| 925 | pr_warn("%s: unwanted hv_support/hfscr_bit_nr\n", uname); |
| 926 | return 0; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | if (f->usable_privilege & USABLE_OS) { |
| 931 | if (f->os_support == OS_SUPPORT_NONE && f->fscr_bit_nr != -1) { |
| 932 | pr_warn("%s: unwanted fscr_bit_nr\n", uname); |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | if (f->os_support == OS_SUPPORT_FSCR) { |
| 937 | if (f->fscr_bit_nr == -1) { |
| 938 | pr_warn("%s: missing fscr_bit_nr\n", uname); |
| 939 | return 0; |
| 940 | } |
| 941 | } |
| 942 | } else { |
| 943 | if (f->os_support != OS_SUPPORT_NONE || f->fscr_bit_nr != -1) { |
| 944 | pr_warn("%s: unwanted os_support/fscr_bit_nr\n", uname); |
| 945 | return 0; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | if (!(f->usable_privilege & USABLE_PR)) { |
| 950 | if (f->hwcap_bit_nr != -1) { |
| 951 | pr_warn("%s: unwanted hwcap_bit_nr\n", uname); |
| 952 | return 0; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | /* Do all the independent features in the first pass */ |
| 957 | if (!of_get_flat_dt_prop(node, "dependencies", &len)) { |
| 958 | if (cpufeatures_process_feature(f)) |
| 959 | f->enabled = 1; |
| 960 | else |
| 961 | f->disabled = 1; |
| 962 | } |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static void __init cpufeatures_deps_enable(struct dt_cpu_feature *f) |
| 968 | { |
| 969 | const __be32 *prop; |
| 970 | int len; |
| 971 | int nr_deps; |
| 972 | int i; |
| 973 | |
| 974 | if (f->enabled || f->disabled) |
| 975 | return; |
| 976 | |
| 977 | prop = of_get_flat_dt_prop(f->node, "dependencies", &len); |
| 978 | if (!prop) { |
| 979 | pr_warn("%s: missing dependencies property", f->name); |
| 980 | return; |
| 981 | } |
| 982 | |
| 983 | nr_deps = len / sizeof(int); |
| 984 | |
| 985 | for (i = 0; i < nr_deps; i++) { |
| 986 | unsigned long phandle = be32_to_cpu(prop[i]); |
| 987 | int j; |
| 988 | |
| 989 | for (j = 0; j < nr_dt_cpu_features; j++) { |
| 990 | struct dt_cpu_feature *d = &dt_cpu_features[j]; |
| 991 | |
| 992 | if (of_get_flat_dt_phandle(d->node) == phandle) { |
| 993 | cpufeatures_deps_enable(d); |
| 994 | if (d->disabled) { |
| 995 | f->disabled = 1; |
| 996 | return; |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | if (cpufeatures_process_feature(f)) |
| 1003 | f->enabled = 1; |
| 1004 | else |
| 1005 | f->disabled = 1; |
| 1006 | } |
| 1007 | |
| 1008 | static int __init scan_cpufeatures_subnodes(unsigned long node, |
| 1009 | const char *uname, |
| 1010 | void *data) |
| 1011 | { |
| 1012 | int *count = data; |
| 1013 | |
| 1014 | process_cpufeatures_node(node, uname, *count); |
| 1015 | |
| 1016 | (*count)++; |
| 1017 | |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | static int __init count_cpufeatures_subnodes(unsigned long node, |
| 1022 | const char *uname, |
| 1023 | void *data) |
| 1024 | { |
| 1025 | int *count = data; |
| 1026 | |
| 1027 | (*count)++; |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char |
| 1033 | *uname, int depth, void *data) |
| 1034 | { |
| 1035 | const __be32 *prop; |
| 1036 | int count, i; |
| 1037 | u32 isa; |
| 1038 | |
| 1039 | /* We are scanning "ibm,powerpc-cpu-features" nodes only */ |
| 1040 | if (!of_flat_dt_is_compatible(node, "ibm,powerpc-cpu-features")) |
| 1041 | return 0; |
| 1042 | |
| 1043 | prop = of_get_flat_dt_prop(node, "isa", NULL); |
| 1044 | if (!prop) |
| 1045 | /* We checked before, "can't happen" */ |
| 1046 | return 0; |
| 1047 | |
| 1048 | isa = be32_to_cpup(prop); |
| 1049 | |
| 1050 | /* Count and allocate space for cpu features */ |
| 1051 | of_scan_flat_dt_subnodes(node, count_cpufeatures_subnodes, |
| 1052 | &nr_dt_cpu_features); |
Christophe Leroy | 1269f7b | 2019-03-11 23:29:00 -0700 | [diff] [blame] | 1053 | dt_cpu_features = memblock_alloc(sizeof(struct dt_cpu_feature) * nr_dt_cpu_features, PAGE_SIZE); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 1054 | if (!dt_cpu_features) |
| 1055 | panic("%s: Failed to allocate %zu bytes align=0x%lx\n", |
| 1056 | __func__, |
| 1057 | sizeof(struct dt_cpu_feature) * nr_dt_cpu_features, |
| 1058 | PAGE_SIZE); |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 1059 | |
| 1060 | cpufeatures_setup_start(isa); |
| 1061 | |
| 1062 | /* Scan nodes into dt_cpu_features and enable those without deps */ |
| 1063 | count = 0; |
| 1064 | of_scan_flat_dt_subnodes(node, scan_cpufeatures_subnodes, &count); |
| 1065 | |
| 1066 | /* Recursive enable remaining features with dependencies */ |
| 1067 | for (i = 0; i < nr_dt_cpu_features; i++) { |
| 1068 | struct dt_cpu_feature *f = &dt_cpu_features[i]; |
| 1069 | |
| 1070 | cpufeatures_deps_enable(f); |
| 1071 | } |
| 1072 | |
| 1073 | prop = of_get_flat_dt_prop(node, "display-name", NULL); |
| 1074 | if (prop && strlen((char *)prop) != 0) { |
| 1075 | strlcpy(dt_cpu_name, (char *)prop, sizeof(dt_cpu_name)); |
| 1076 | cur_cpu_spec->cpu_name = dt_cpu_name; |
| 1077 | } |
| 1078 | |
| 1079 | cpufeatures_setup_finished(); |
| 1080 | |
| 1081 | memblock_free(__pa(dt_cpu_features), |
| 1082 | sizeof(struct dt_cpu_feature)*nr_dt_cpu_features); |
| 1083 | |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | void __init dt_cpu_ftrs_scan(void) |
| 1088 | { |
Nicholas Piggin | a2b05b7 | 2017-05-11 21:24:41 +1000 | [diff] [blame] | 1089 | if (!using_dt_cpu_ftrs) |
| 1090 | return; |
| 1091 | |
Nicholas Piggin | 5a61ef7 | 2017-05-09 13:16:52 +1000 | [diff] [blame] | 1092 | of_scan_flat_dt(dt_cpu_ftrs_scan_callback, NULL); |
| 1093 | } |