blob: 87a5d47415eb363adf5d8d6a152482c0b0bc1a58 [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 */
27#define ISA_V2_07B 2070
28#define ISA_V3_0B 3000
Alistair Popple43d0d372020-05-21 11:43:37 +100029#define ISA_V3_1 3100
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100030
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
42struct 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 Piggin5a61ef72017-05-09 13:16:52 +100057#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 Piggin5a61ef72017-05-09 13:16:52 +100068extern long __machine_check_early_realmode_p8(struct pt_regs *regs);
69extern long __machine_check_early_realmode_p9(struct pt_regs *regs);
70
71static int hv_mode;
72
73static struct {
74 u64 lpcr;
Nicholas Piggina57ac412018-04-05 15:50:49 +100075 u64 lpcr_clear;
Nicholas Piggin5a61ef72017-05-09 13:16:52 +100076 u64 hfscr;
77 u64 fscr;
78} 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);
Jordan Niethe13c7bb32019-09-17 10:46:05 +1000105 mtspr(SPRN_PCR, PCR_MASK);
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
564struct dt_cpu_feature_match {
565 const char *name;
566 int (*enable)(struct dt_cpu_feature *f);
567 u64 cpu_ftr_bit_mask;
568};
569
570static 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 Mackerrasb5af4f22018-03-21 21:31:59 +1100596 {"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 Piggin5a61ef72017-05-09 13:16:52 +1000598 {"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 Piggin5a61ef72017-05-09 13:16:52 +1000611 {"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 Ellermanc1807e32017-10-19 15:08:19 +1100614 {"coprocessor-icswx", feat_enable, 0},
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000615 {"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 Popplec63d6882020-05-21 11:43:39 +1000637 {"prefix-instructions", feat_enable, 0},
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000638};
639
Nicholas Piggina2b05b72017-05-11 21:24:41 +1000640static bool __initdata using_dt_cpu_ftrs;
641static bool __initdata enable_unknown = true;
642
643static 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}
657early_param("dt_cpu_ftrs", dt_cpu_ftrs_parse);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000658
659static 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 Popple43d0d372020-05-21 11:43:37 +1000667
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 Piggin5a61ef72017-05-09 13:16:52 +1000672}
673
674static 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 Ellerman8cfaf102019-02-11 11:20:01 +1100684 if (m->enable(f)) {
685 cur_cpu_spec->cpu_features |= m->cpu_ftr_bit_mask;
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000686 break;
Michael Ellerman8cfaf102019-02-11 11:20:01 +1100687 }
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000688
689 pr_info("not enabling: %s (disabled or unsupported by kernel)\n",
690 f->name);
691 return false;
692 }
693 }
694
Michael Ellerman8cfaf102019-02-11 11:20:01 +1100695 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 Piggin5a61ef72017-05-09 13:16:52 +1000699 }
700
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000701 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.V677733e2019-09-24 09:22:51 +0530709/*
710 * Handle POWER9 broadcast tlbie invalidation issue using
711 * cpu feature flag.
712 */
713static __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.V09ce98c2019-09-24 09:22:52 +0530723 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_STQ_BUG;
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530724 } else if ((pvr & 0xc000) == 0) {
725 /* Cumulus */
726 if ((pvr & 0xfff) < 0x103)
Aneesh Kumar K.V09ce98c2019-09-24 09:22:52 +0530727 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_STQ_BUG;
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530728 } else {
729 WARN_ONCE(1, "Unknown PVR");
Aneesh Kumar K.V09ce98c2019-09-24 09:22:52 +0530730 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_STQ_BUG;
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530731 }
Aneesh Kumar K.V047e6572019-09-24 09:22:53 +0530732
733 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TLBIE_ERAT_BUG;
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530734 }
735}
736
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000737static __init void cpufeatures_cpu_quirks(void)
738{
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530739 unsigned long version = mfspr(SPRN_PVR);
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000740
741 /*
742 * Not all quirks can be derived from the cpufeatures device tree.
743 */
Jordan Niethe736bcdd2019-12-06 14:17:22 +1100744 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 Ellerman4d6c51b2017-11-22 23:17:01 +1100748 cur_cpu_spec->cpu_features |= CPU_FTR_POWER9_DD2_1;
Jordan Niethe736bcdd2019-12-06 14:17:22 +1100749 cur_cpu_spec->cpu_features |= CPU_FTR_P9_RADIX_PREFETCH_BUG;
750 } else if ((version & 0xffffefff) == 0x004e0202) {
Nicholas Piggin9e9626e2018-02-21 05:08:27 +1000751 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 Niethe736bcdd2019-12-06 14:17:22 +1100754 } else if ((version & 0xffff0000) == 0x004e0000) {
Michael Ellerman749a0272018-06-13 23:23:56 +1000755 /* DD2.1 and up have DD2_1 */
Nicholas Piggin9e9626e2018-02-21 05:08:27 +1000756 cur_cpu_spec->cpu_features |= CPU_FTR_POWER9_DD2_1;
Jordan Niethe736bcdd2019-12-06 14:17:22 +1100757 }
Michael Neuling622aa352018-03-27 15:37:23 +1100758
Michael Ellerman95dff482018-03-28 22:59:50 +1100759 if ((version & 0xffff0000) == 0x004e0000) {
Michael Neuling622aa352018-03-27 15:37:23 +1100760 cur_cpu_spec->cpu_features &= ~(CPU_FTR_DAWR);
Alastair D'Silva81984422018-05-11 16:12:57 +1000761 cur_cpu_spec->cpu_features |= CPU_FTR_P9_TIDR;
Michael Ellerman95dff482018-03-28 22:59:50 +1100762 }
Nicholas Pigginc1301532018-04-05 15:57:54 +1000763
Aneesh Kumar K.V677733e2019-09-24 09:22:51 +0530764 update_tlbie_feature_flag(version);
Nicholas Pigginc1301532018-04-05 15:57:54 +1000765 /*
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 Piggin5a61ef72017-05-09 13:16:52 +1000771}
772
773static 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 Ellermane4b79902018-03-13 15:58:11 +1100782 /* Make sure powerpc_base_platform is non-NULL */
783 powerpc_base_platform = cur_cpu_spec->platform;
784
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000785 system_registers.lpcr = mfspr(SPRN_LPCR);
786 system_registers.hfscr = mfspr(SPRN_HFSCR);
787 system_registers.fscr = mfspr(SPRN_FSCR);
788
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000789 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 Piggina2b05b72017-05-11 21:24:41 +1000793static 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 Piggin5a61ef72017-05-09 13:16:52 +1000813static 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 Piggin5a61ef72017-05-09 13:16:52 +1000823bool __init dt_cpu_ftrs_in_use(void)
824{
825 return using_dt_cpu_ftrs;
826}
827
828bool __init dt_cpu_ftrs_init(void *fdt)
829{
Nicholas Piggina2b05b72017-05-11 21:24:41 +1000830 using_dt_cpu_ftrs = false;
831
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000832 /* 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 Piggina2b05b72017-05-11 21:24:41 +1000839 if (disabled_on_cmdline())
840 return false;
841
Nicholas Piggin5a61ef72017-05-09 13:16:52 +1000842 cpufeatures_setup_cpu();
843
844 using_dt_cpu_ftrs = true;
845 return true;
846}
847
848static int nr_dt_cpu_features;
849static struct dt_cpu_feature *dt_cpu_features;
850
851static 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 Piggin5a61ef72017-05-09 13:16:52 +1000859
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
967static 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
1008static 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
1021static 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
1032static 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 Leroy1269f7b2019-03-11 23:29:00 -07001053 dt_cpu_features = memblock_alloc(sizeof(struct dt_cpu_feature) * nr_dt_cpu_features, PAGE_SIZE);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -07001054 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 Piggin5a61ef72017-05-09 13:16:52 +10001059
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
1087void __init dt_cpu_ftrs_scan(void)
1088{
Nicholas Piggina2b05b72017-05-11 21:24:41 +10001089 if (!using_dt_cpu_ftrs)
1090 return;
1091
Nicholas Piggin5a61ef72017-05-09 13:16:52 +10001092 of_scan_flat_dt(dt_cpu_ftrs_scan_callback, NULL);
1093}