blob: 9d6e833da2bdcbba8b6d86d9ad1bf926adbc2320 [file] [log] [blame]
Thomas Gleixnerf50a7f32019-05-28 09:57:18 -07001// SPDX-License-Identifier: GPL-2.0-only
Nicholas Piggin5a61ef72017-05-09 13:16:52 +10002/*
3 * Copyright 2017, Nicholas Piggin, IBM Corporation
Nicholas Piggin5a61ef72017-05-09 13:16:52 +10004 */
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 Piggina2b05b72017-05-11 21:24:41 +100011#include <linux/libfdt.h>
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100012#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 */
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100027#define ISA_V3_0B 3000
Alistair Popple43d0d372020-05-21 11:43:37 +100028#define ISA_V3_1 3100
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100029
30#define USABLE_PR (1U << 0)
31#define USABLE_OS (1U << 1)
32#define USABLE_HV (1U << 2)
33
34#define HV_SUPPORT_HFSCR (1U << 0)
35#define OS_SUPPORT_FSCR (1U << 0)
36
37/* For parsing, we define all bits set as "NONE" case */
38#define HV_SUPPORT_NONE 0xffffffffU
39#define OS_SUPPORT_NONE 0xffffffffU
40
41struct dt_cpu_feature {
42 const char *name;
43 uint32_t isa;
44 uint32_t usable_privilege;
45 uint32_t hv_support;
46 uint32_t os_support;
47 uint32_t hfscr_bit_nr;
48 uint32_t fscr_bit_nr;
49 uint32_t hwcap_bit_nr;
50 /* fdt parsing */
51 unsigned long node;
52 int enabled;
53 int disabled;
54};
55
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100056#define MMU_FTRS_HASH_BASE (MMU_FTRS_POWER8)
57
58#define COMMON_USER_BASE (PPC_FEATURE_32 | PPC_FEATURE_64 | \
59 PPC_FEATURE_ARCH_2_06 |\
60 PPC_FEATURE_ICACHE_SNOOP)
61#define COMMON_USER2_BASE (PPC_FEATURE2_ARCH_2_07 | \
62 PPC_FEATURE2_ISEL)
63/*
64 * Set up the base CPU
65 */
66
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100067extern long __machine_check_early_realmode_p8(struct pt_regs *regs);
68extern long __machine_check_early_realmode_p9(struct pt_regs *regs);
69
70static int hv_mode;
71
72static struct {
73 u64 lpcr;
Nicholas Piggina57ac412018-04-05 15:50:49 +100074 u64 lpcr_clear;
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100075 u64 hfscr;
76 u64 fscr;
Alistair Popple87939d52020-05-21 11:43:40 +100077 u64 pcr;
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100078} system_registers;
79
80static void (*init_pmu_registers)(void);
81
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100082static void __restore_cpu_cpufeatures(void)
83{
Nicholas Piggina57ac412018-04-05 15:50:49 +100084 u64 lpcr;
85
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100086 /*
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 Piggina57ac412018-04-05 15:50:49 +100098 lpcr = mfspr(SPRN_LPCR);
99 lpcr |= system_registers.lpcr;
100 lpcr &= ~system_registers.lpcr_clear;
101 mtspr(SPRN_LPCR, lpcr);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000102 if (hv_mode) {
103 mtspr(SPRN_LPID, 0);
104 mtspr(SPRN_HFSCR, system_registers.hfscr);
Alistair Popple87939d52020-05-21 11:43:40 +1000105 mtspr(SPRN_PCR, system_registers.pcr);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000106 }
107 mtspr(SPRN_FSCR, system_registers.fscr);
108
109 if (init_pmu_registers)
110 init_pmu_registers();
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000111}
112
113static char dt_cpu_name[64];
114
115static struct cpu_spec __initdata base_cpu_spec = {
116 .cpu_name = NULL,
Michael Ellerman81b654c2018-04-12 22:24:45 +1000117 .cpu_features = CPU_FTRS_DT_CPU_BASE,
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000118 .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 Piggin5a61ef72017-05-09 13:16:52 +1000129 .machine_check_early = NULL,
130 .platform = NULL,
131};
132
133static 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 Piggin5a61ef72017-05-09 13:16:52 +1000143 cur_cpu_spec->cpu_features |= CPU_FTR_HVMODE;
144 mtspr(SPRN_HFSCR, 0);
145 }
146 mtspr(SPRN_FSCR, 0);
Jordan Niethe13c7bb32019-09-17 10:46:05 +1000147 mtspr(SPRN_PCR, PCR_MASK);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000148
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
156static 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
193static 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
226static int __init feat_disable(struct dt_cpu_feature *f)
227{
228 return 0;
229}
230
231static 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
251static 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
257static 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
264static 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
278static 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
285static 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
299static 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
318static int __init feat_enable_mmu_hash_v3(struct dt_cpu_feature *f)
319{
320 u64 lpcr;
321
Nicholas Piggina57ac412018-04-05 15:50:49 +1000322 system_registers.lpcr_clear |= (LPCR_ISL | LPCR_UPRT | LPCR_HR);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000323 lpcr = mfspr(SPRN_LPCR);
Nicholas Piggina57ac412018-04-05 15:50:49 +1000324 lpcr &= ~(LPCR_ISL | LPCR_UPRT | LPCR_HR);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000325 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
334static 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
346static int __init feat_enable_dscr(struct dt_cpu_feature *f)
347{
348 u64 lpcr;
349
Michael Ellerman993e3d92020-05-28 00:58:41 +1000350 /*
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 Piggin5a61ef72017-05-09 13:16:52 +1000358 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
368static void hfscr_pmu_enable(void)
369{
370 u64 hfscr = mfspr(SPRN_HFSCR);
371 hfscr |= PPC_BIT(60);
372 mtspr(SPRN_HFSCR, hfscr);
373}
374
375static 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
389static int __init feat_enable_mce_power8(struct dt_cpu_feature *f)
390{
391 cur_cpu_spec->platform = "power8";
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000392 cur_cpu_spec->machine_check_early = __machine_check_early_realmode_p8;
393
394 return 1;
395}
396
397static 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
416static 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
427static int __init feat_enable_mce_power9(struct dt_cpu_feature *f)
428{
429 cur_cpu_spec->platform = "power9";
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000430 cur_cpu_spec->machine_check_early = __machine_check_early_realmode_p9;
431
432 return 1;
433}
434
435static 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
452static 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
462static 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
470static 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
483static 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
495static 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
502static 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
516static 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
532static 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
557static 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
Alistair Popple87939d52020-05-21 11:43:40 +1000564static int __init feat_enable_mma(struct dt_cpu_feature *f)
565{
566 u64 pcr;
567
568 feat_enable(f);
569 pcr = mfspr(SPRN_PCR);
570 pcr &= ~PCR_MMA_DIS;
571 mtspr(SPRN_PCR, pcr);
572
573 return 1;
574}
575
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000576struct dt_cpu_feature_match {
577 const char *name;
578 int (*enable)(struct dt_cpu_feature *f);
579 u64 cpu_ftr_bit_mask;
580};
581
582static struct dt_cpu_feature_match __initdata
583 dt_cpu_feature_match_table[] = {
584 {"hypervisor", feat_enable_hv, 0},
585 {"big-endian", feat_enable, 0},
586 {"little-endian", feat_enable_le, CPU_FTR_REAL_LE},
587 {"smt", feat_enable_smt, 0},
588 {"interrupt-facilities", feat_enable, 0},
589 {"timer-facilities", feat_enable, 0},
590 {"timer-facilities-v3", feat_enable, 0},
591 {"debug-facilities", feat_enable, 0},
592 {"come-from-address-register", feat_enable, CPU_FTR_CFAR},
593 {"branch-tracing", feat_enable, 0},
594 {"floating-point", feat_enable_fp, 0},
595 {"vector", feat_enable_vector, 0},
596 {"vector-scalar", feat_enable_vsx, 0},
597 {"vector-scalar-v3", feat_enable, 0},
598 {"decimal-floating-point", feat_enable, 0},
599 {"decimal-integer", feat_enable, 0},
600 {"quadword-load-store", feat_enable, 0},
601 {"vector-crypto", feat_enable, 0},
602 {"mmu-hash", feat_enable_mmu_hash, 0},
603 {"mmu-radix", feat_enable_mmu_radix, 0},
604 {"mmu-hash-v3", feat_enable_mmu_hash_v3, 0},
605 {"virtual-page-class-key-protection", feat_enable, 0},
606 {"transactional-memory", feat_enable_tm, CPU_FTR_TM},
607 {"transactional-memory-v3", feat_enable_tm, 0},
Paul Mackerrasb5af4f22018-03-21 21:31:59 +1100608 {"tm-suspend-hypervisor-assist", feat_enable, CPU_FTR_P9_TM_HV_ASSIST},
609 {"tm-suspend-xer-so-bug", feat_enable, CPU_FTR_P9_TM_XER_SO_BUG},
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000610 {"idle-nap", feat_enable_idle_nap, 0},
611 {"alignment-interrupt-dsisr", feat_enable_align_dsisr, 0},
612 {"idle-stop", feat_enable_idle_stop, 0},
613 {"machine-check-power8", feat_enable_mce_power8, 0},
614 {"performance-monitor-power8", feat_enable_pmu_power8, 0},
615 {"data-stream-control-register", feat_enable_dscr, CPU_FTR_DSCR},
616 {"event-based-branch", feat_enable_ebb, 0},
617 {"target-address-register", feat_enable, 0},
618 {"branch-history-rolling-buffer", feat_enable, 0},
619 {"control-register", feat_enable, CPU_FTR_CTRL},
620 {"processor-control-facility", feat_enable_dbell, CPU_FTR_DBELL},
621 {"processor-control-facility-v3", feat_enable_dbell, CPU_FTR_DBELL},
622 {"processor-utilization-of-resources-register", feat_enable_purr, 0},
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000623 {"no-execute", feat_enable, 0},
624 {"strong-access-ordering", feat_enable, CPU_FTR_SAO},
625 {"cache-inhibited-large-page", feat_enable_large_ci, 0},
Michael Ellermanc1807e32017-10-19 15:08:19 +1100626 {"coprocessor-icswx", feat_enable, 0},
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000627 {"hypervisor-virtualization-interrupt", feat_enable_hvi, 0},
628 {"program-priority-register", feat_enable, CPU_FTR_HAS_PPR},
629 {"wait", feat_enable, 0},
630 {"atomic-memory-operations", feat_enable, 0},
631 {"branch-v3", feat_enable, 0},
632 {"copy-paste", feat_enable, 0},
633 {"decimal-floating-point-v3", feat_enable, 0},
634 {"decimal-integer-v3", feat_enable, 0},
635 {"fixed-point-v3", feat_enable, 0},
636 {"floating-point-v3", feat_enable, 0},
637 {"group-start-register", feat_enable, 0},
638 {"pc-relative-addressing", feat_enable, 0},
639 {"machine-check-power9", feat_enable_mce_power9, 0},
640 {"performance-monitor-power9", feat_enable_pmu_power9, 0},
641 {"event-based-branch-v3", feat_enable, 0},
642 {"random-number-generator", feat_enable, 0},
643 {"system-call-vectored", feat_disable, 0},
644 {"trace-interrupt-v3", feat_enable, 0},
645 {"vector-v3", feat_enable, 0},
646 {"vector-binary128", feat_enable, 0},
647 {"vector-binary16", feat_enable, 0},
648 {"wait-v3", feat_enable, 0},
Alistair Popplec63d6882020-05-21 11:43:39 +1000649 {"prefix-instructions", feat_enable, 0},
Alistair Popple87939d52020-05-21 11:43:40 +1000650 {"matrix-multiply-assist", feat_enable_mma, 0},
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000651};
652
Nicholas Piggina2b05b72017-05-11 21:24:41 +1000653static bool __initdata using_dt_cpu_ftrs;
654static bool __initdata enable_unknown = true;
655
656static int __init dt_cpu_ftrs_parse(char *str)
657{
658 if (!str)
659 return 0;
660
661 if (!strcmp(str, "off"))
662 using_dt_cpu_ftrs = false;
663 else if (!strcmp(str, "known"))
664 enable_unknown = false;
665 else
666 return 1;
667
668 return 0;
669}
670early_param("dt_cpu_ftrs", dt_cpu_ftrs_parse);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000671
672static void __init cpufeatures_setup_start(u32 isa)
673{
674 pr_info("setup for ISA %d\n", isa);
675
Murilo Opsfelder Araujoe781f122020-06-10 18:51:13 -0300676 if (isa >= ISA_V3_0B) {
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000677 cur_cpu_spec->cpu_features |= CPU_FTR_ARCH_300;
678 cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_ARCH_3_00;
679 }
Alistair Popple43d0d372020-05-21 11:43:37 +1000680
681 if (isa >= 3100) {
682 cur_cpu_spec->cpu_features |= CPU_FTR_ARCH_31;
683 cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_ARCH_3_1;
684 }
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000685}
686
687static bool __init cpufeatures_process_feature(struct dt_cpu_feature *f)
688{
689 const struct dt_cpu_feature_match *m;
690 bool known = false;
691 int i;
692
693 for (i = 0; i < ARRAY_SIZE(dt_cpu_feature_match_table); i++) {
694 m = &dt_cpu_feature_match_table[i];
695 if (!strcmp(f->name, m->name)) {
696 known = true;
Michael Ellerman8cfaf102019-02-11 11:20:01 +1100697 if (m->enable(f)) {
698 cur_cpu_spec->cpu_features |= m->cpu_ftr_bit_mask;
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000699 break;
Michael Ellerman8cfaf102019-02-11 11:20:01 +1100700 }
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000701
702 pr_info("not enabling: %s (disabled or unsupported by kernel)\n",
703 f->name);
704 return false;
705 }
706 }
707
Michael Ellerman8cfaf102019-02-11 11:20:01 +1100708 if (!known && (!enable_unknown || !feat_try_enable_unknown(f))) {
709 pr_info("not enabling: %s (unknown and unsupported by kernel)\n",
710 f->name);
711 return false;
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000712 }
713
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000714 if (known)
715 pr_debug("enabling: %s\n", f->name);
716 else
717 pr_debug("enabling: %s (unknown)\n", f->name);
718
719 return true;
720}
721
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530722/*
723 * Handle POWER9 broadcast tlbie invalidation issue using
724 * cpu feature flag.
725 */
726static __init void update_tlbie_feature_flag(unsigned long pvr)
727{
728 if (PVR_VER(pvr) == PVR_POWER9) {
729 /*
730 * Set the tlbie feature flag for anything below
731 * Nimbus DD 2.3 and Cumulus DD 1.3
732 */
733 if ((pvr & 0xe000) == 0) {
734 /* Nimbus */
735 if ((pvr & 0xfff) < 0x203)
Aneesh Kumar K.V09ce98c2019-09-24 09:22:52 +0530736 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_STQ_BUG;
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530737 } else if ((pvr & 0xc000) == 0) {
738 /* Cumulus */
739 if ((pvr & 0xfff) < 0x103)
Aneesh Kumar K.V09ce98c2019-09-24 09:22:52 +0530740 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_STQ_BUG;
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530741 } else {
742 WARN_ONCE(1, "Unknown PVR");
Aneesh Kumar K.V09ce98c2019-09-24 09:22:52 +0530743 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_STQ_BUG;
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530744 }
Aneesh Kumar K.V047e6572019-09-24 09:22:53 +0530745
746 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_ERAT_BUG;
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530747 }
748}
749
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000750static __init void cpufeatures_cpu_quirks(void)
751{
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530752 unsigned long version = mfspr(SPRN_PVR);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000753
754 /*
755 * Not all quirks can be derived from the cpufeatures device tree.
756 */
Jordan Niethe736bcdd2019-12-06 14:17:22 +1100757 if ((version & 0xffffefff) == 0x004e0200) {
758 /* DD2.0 has no feature flag */
759 cur_cpu_spec->cpu_features |= CPU_FTR_P9_RADIX_PREFETCH_BUG;
760 } else if ((version & 0xffffefff) == 0x004e0201) {
Michael Ellerman4d6c51b2017-11-22 23:17:01 +1100761 cur_cpu_spec->cpu_features |= CPU_FTR_POWER9_DD2_1;
Jordan Niethe736bcdd2019-12-06 14:17:22 +1100762 cur_cpu_spec->cpu_features |= CPU_FTR_P9_RADIX_PREFETCH_BUG;
763 } else if ((version & 0xffffefff) == 0x004e0202) {
Nicholas Piggin9e9626e2018-02-21 05:08:27 +1000764 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TM_HV_ASSIST;
765 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TM_XER_SO_BUG;
766 cur_cpu_spec->cpu_features |= CPU_FTR_POWER9_DD2_1;
Jordan Niethe736bcdd2019-12-06 14:17:22 +1100767 } else if ((version & 0xffff0000) == 0x004e0000) {
Michael Ellerman749a0272018-06-13 23:23:56 +1000768 /* DD2.1 and up have DD2_1 */
Nicholas Piggin9e9626e2018-02-21 05:08:27 +1000769 cur_cpu_spec->cpu_features |= CPU_FTR_POWER9_DD2_1;
Jordan Niethe736bcdd2019-12-06 14:17:22 +1100770 }
Michael Neuling622aa352018-03-27 15:37:23 +1100771
Michael Ellerman95dff482018-03-28 22:59:50 +1100772 if ((version & 0xffff0000) == 0x004e0000) {
Michael Neuling622aa352018-03-27 15:37:23 +1100773 cur_cpu_spec->cpu_features &= ~(CPU_FTR_DAWR);
Alastair D'Silva81984422018-05-11 16:12:57 +1000774 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TIDR;
Michael Ellerman95dff482018-03-28 22:59:50 +1100775 }
Nicholas Pigginc1301532018-04-05 15:57:54 +1000776
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530777 update_tlbie_feature_flag(version);
Nicholas Pigginc1301532018-04-05 15:57:54 +1000778 /*
779 * PKEY was not in the initial base or feature node
780 * specification, but it should become optional in the next
781 * cpu feature version sequence.
782 */
783 cur_cpu_spec->cpu_features |= CPU_FTR_PKEY;
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000784}
785
786static void __init cpufeatures_setup_finished(void)
787{
788 cpufeatures_cpu_quirks();
789
790 if (hv_mode && !(cur_cpu_spec->cpu_features & CPU_FTR_HVMODE)) {
791 pr_err("hypervisor not present in device tree but HV mode is enabled in the CPU. Enabling.\n");
792 cur_cpu_spec->cpu_features |= CPU_FTR_HVMODE;
793 }
794
Michael Ellermane4b79902018-03-13 15:58:11 +1100795 /* Make sure powerpc_base_platform is non-NULL */
796 powerpc_base_platform = cur_cpu_spec->platform;
797
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000798 system_registers.lpcr = mfspr(SPRN_LPCR);
799 system_registers.hfscr = mfspr(SPRN_HFSCR);
800 system_registers.fscr = mfspr(SPRN_FSCR);
Alistair Popple87939d52020-05-21 11:43:40 +1000801 system_registers.pcr = mfspr(SPRN_PCR);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000802
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000803 pr_info("final cpu/mmu features = 0x%016lx 0x%08x\n",
804 cur_cpu_spec->cpu_features, cur_cpu_spec->mmu_features);
805}
806
Nicholas Piggina2b05b72017-05-11 21:24:41 +1000807static int __init disabled_on_cmdline(void)
808{
809 unsigned long root, chosen;
810 const char *p;
811
812 root = of_get_flat_dt_root();
813 chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
814 if (chosen == -FDT_ERR_NOTFOUND)
815 return false;
816
817 p = of_get_flat_dt_prop(chosen, "bootargs", NULL);
818 if (!p)
819 return false;
820
821 if (strstr(p, "dt_cpu_ftrs=off"))
822 return true;
823
824 return false;
825}
826
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000827static int __init fdt_find_cpu_features(unsigned long node, const char *uname,
828 int depth, void *data)
829{
830 if (of_flat_dt_is_compatible(node, "ibm,powerpc-cpu-features")
831 && of_get_flat_dt_prop(node, "isa", NULL))
832 return 1;
833
834 return 0;
835}
836
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000837bool __init dt_cpu_ftrs_in_use(void)
838{
839 return using_dt_cpu_ftrs;
840}
841
842bool __init dt_cpu_ftrs_init(void *fdt)
843{
Nicholas Piggina2b05b72017-05-11 21:24:41 +1000844 using_dt_cpu_ftrs = false;
845
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000846 /* Setup and verify the FDT, if it fails we just bail */
847 if (!early_init_dt_verify(fdt))
848 return false;
849
850 if (!of_scan_flat_dt(fdt_find_cpu_features, NULL))
851 return false;
852
Nicholas Piggina2b05b72017-05-11 21:24:41 +1000853 if (disabled_on_cmdline())
854 return false;
855
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000856 cpufeatures_setup_cpu();
857
858 using_dt_cpu_ftrs = true;
859 return true;
860}
861
862static int nr_dt_cpu_features;
863static struct dt_cpu_feature *dt_cpu_features;
864
865static int __init process_cpufeatures_node(unsigned long node,
866 const char *uname, int i)
867{
868 const __be32 *prop;
869 struct dt_cpu_feature *f;
870 int len;
871
872 f = &dt_cpu_features[i];
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000873
874 f->node = node;
875
876 f->name = uname;
877
878 prop = of_get_flat_dt_prop(node, "isa", &len);
879 if (!prop) {
880 pr_warn("%s: missing isa property\n", uname);
881 return 0;
882 }
883 f->isa = be32_to_cpup(prop);
884
885 prop = of_get_flat_dt_prop(node, "usable-privilege", &len);
886 if (!prop) {
887 pr_warn("%s: missing usable-privilege property", uname);
888 return 0;
889 }
890 f->usable_privilege = be32_to_cpup(prop);
891
892 prop = of_get_flat_dt_prop(node, "hv-support", &len);
893 if (prop)
894 f->hv_support = be32_to_cpup(prop);
895 else
896 f->hv_support = HV_SUPPORT_NONE;
897
898 prop = of_get_flat_dt_prop(node, "os-support", &len);
899 if (prop)
900 f->os_support = be32_to_cpup(prop);
901 else
902 f->os_support = OS_SUPPORT_NONE;
903
904 prop = of_get_flat_dt_prop(node, "hfscr-bit-nr", &len);
905 if (prop)
906 f->hfscr_bit_nr = be32_to_cpup(prop);
907 else
908 f->hfscr_bit_nr = -1;
909 prop = of_get_flat_dt_prop(node, "fscr-bit-nr", &len);
910 if (prop)
911 f->fscr_bit_nr = be32_to_cpup(prop);
912 else
913 f->fscr_bit_nr = -1;
914 prop = of_get_flat_dt_prop(node, "hwcap-bit-nr", &len);
915 if (prop)
916 f->hwcap_bit_nr = be32_to_cpup(prop);
917 else
918 f->hwcap_bit_nr = -1;
919
920 if (f->usable_privilege & USABLE_HV) {
921 if (!(mfmsr() & MSR_HV)) {
922 pr_warn("%s: HV feature passed to guest\n", uname);
923 return 0;
924 }
925
926 if (f->hv_support == HV_SUPPORT_NONE && f->hfscr_bit_nr != -1) {
927 pr_warn("%s: unwanted hfscr_bit_nr\n", uname);
928 return 0;
929 }
930
931 if (f->hv_support == HV_SUPPORT_HFSCR) {
932 if (f->hfscr_bit_nr == -1) {
933 pr_warn("%s: missing hfscr_bit_nr\n", uname);
934 return 0;
935 }
936 }
937 } else {
938 if (f->hv_support != HV_SUPPORT_NONE || f->hfscr_bit_nr != -1) {
939 pr_warn("%s: unwanted hv_support/hfscr_bit_nr\n", uname);
940 return 0;
941 }
942 }
943
944 if (f->usable_privilege & USABLE_OS) {
945 if (f->os_support == OS_SUPPORT_NONE && f->fscr_bit_nr != -1) {
946 pr_warn("%s: unwanted fscr_bit_nr\n", uname);
947 return 0;
948 }
949
950 if (f->os_support == OS_SUPPORT_FSCR) {
951 if (f->fscr_bit_nr == -1) {
952 pr_warn("%s: missing fscr_bit_nr\n", uname);
953 return 0;
954 }
955 }
956 } else {
957 if (f->os_support != OS_SUPPORT_NONE || f->fscr_bit_nr != -1) {
958 pr_warn("%s: unwanted os_support/fscr_bit_nr\n", uname);
959 return 0;
960 }
961 }
962
963 if (!(f->usable_privilege & USABLE_PR)) {
964 if (f->hwcap_bit_nr != -1) {
965 pr_warn("%s: unwanted hwcap_bit_nr\n", uname);
966 return 0;
967 }
968 }
969
970 /* Do all the independent features in the first pass */
971 if (!of_get_flat_dt_prop(node, "dependencies", &len)) {
972 if (cpufeatures_process_feature(f))
973 f->enabled = 1;
974 else
975 f->disabled = 1;
976 }
977
978 return 0;
979}
980
981static void __init cpufeatures_deps_enable(struct dt_cpu_feature *f)
982{
983 const __be32 *prop;
984 int len;
985 int nr_deps;
986 int i;
987
988 if (f->enabled || f->disabled)
989 return;
990
991 prop = of_get_flat_dt_prop(f->node, "dependencies", &len);
992 if (!prop) {
993 pr_warn("%s: missing dependencies property", f->name);
994 return;
995 }
996
997 nr_deps = len / sizeof(int);
998
999 for (i = 0; i < nr_deps; i++) {
1000 unsigned long phandle = be32_to_cpu(prop[i]);
1001 int j;
1002
1003 for (j = 0; j < nr_dt_cpu_features; j++) {
1004 struct dt_cpu_feature *d = &dt_cpu_features[j];
1005
1006 if (of_get_flat_dt_phandle(d->node) == phandle) {
1007 cpufeatures_deps_enable(d);
1008 if (d->disabled) {
1009 f->disabled = 1;
1010 return;
1011 }
1012 }
1013 }
1014 }
1015
1016 if (cpufeatures_process_feature(f))
1017 f->enabled = 1;
1018 else
1019 f->disabled = 1;
1020}
1021
1022static int __init scan_cpufeatures_subnodes(unsigned long node,
1023 const char *uname,
1024 void *data)
1025{
1026 int *count = data;
1027
1028 process_cpufeatures_node(node, uname, *count);
1029
1030 (*count)++;
1031
1032 return 0;
1033}
1034
1035static int __init count_cpufeatures_subnodes(unsigned long node,
1036 const char *uname,
1037 void *data)
1038{
1039 int *count = data;
1040
1041 (*count)++;
1042
1043 return 0;
1044}
1045
1046static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
1047 *uname, int depth, void *data)
1048{
1049 const __be32 *prop;
1050 int count, i;
1051 u32 isa;
1052
1053 /* We are scanning "ibm,powerpc-cpu-features" nodes only */
1054 if (!of_flat_dt_is_compatible(node, "ibm,powerpc-cpu-features"))
1055 return 0;
1056
1057 prop = of_get_flat_dt_prop(node, "isa", NULL);
1058 if (!prop)
1059 /* We checked before, "can't happen" */
1060 return 0;
1061
1062 isa = be32_to_cpup(prop);
1063
1064 /* Count and allocate space for cpu features */
1065 of_scan_flat_dt_subnodes(node, count_cpufeatures_subnodes,
1066 &nr_dt_cpu_features);
Christophe Leroy1269f7b2019-03-11 23:29:00 -07001067 dt_cpu_features = memblock_alloc(sizeof(struct dt_cpu_feature) * nr_dt_cpu_features, PAGE_SIZE);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -07001068 if (!dt_cpu_features)
1069 panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
1070 __func__,
1071 sizeof(struct dt_cpu_feature) * nr_dt_cpu_features,
1072 PAGE_SIZE);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +10001073
1074 cpufeatures_setup_start(isa);
1075
1076 /* Scan nodes into dt_cpu_features and enable those without deps */
1077 count = 0;
1078 of_scan_flat_dt_subnodes(node, scan_cpufeatures_subnodes, &count);
1079
1080 /* Recursive enable remaining features with dependencies */
1081 for (i = 0; i < nr_dt_cpu_features; i++) {
1082 struct dt_cpu_feature *f = &dt_cpu_features[i];
1083
1084 cpufeatures_deps_enable(f);
1085 }
1086
1087 prop = of_get_flat_dt_prop(node, "display-name", NULL);
1088 if (prop && strlen((char *)prop) != 0) {
1089 strlcpy(dt_cpu_name, (char *)prop, sizeof(dt_cpu_name));
1090 cur_cpu_spec->cpu_name = dt_cpu_name;
1091 }
1092
1093 cpufeatures_setup_finished();
1094
1095 memblock_free(__pa(dt_cpu_features),
1096 sizeof(struct dt_cpu_feature)*nr_dt_cpu_features);
1097
1098 return 0;
1099}
1100
1101void __init dt_cpu_ftrs_scan(void)
1102{
Nicholas Piggina2b05b72017-05-11 21:24:41 +10001103 if (!using_dt_cpu_ftrs)
1104 return;
1105
Nicholas Piggin5a61ef72017-05-09 13:16:52 +10001106 of_scan_flat_dt(dt_cpu_ftrs_scan_callback, NULL);
1107}