Skip to content

Commit 3eb06f6

Browse files
committed
arm64: cpufeature: Introduce MATCH_ALL_EARLY_CPUS capability type
For system-wide capabilities, the kernel has the SCOPE_SYSTEM type. Such capabilities are checked once the SMP boot has completed using the sanitised ID registers. However, there is a need for a new capability type similar in scope to the system one but with checking performed locally on each CPU during boot (e.g. based on MIDR_EL1 which is not a sanitised register). Introduce ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS which, together with ARM64_CPUCAP_SCOPE_LOCAL_CPU, ensures that such capability is enabled only if all early CPUs have it. For ease of use, define ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE which combines SCOPE_LOCAL_CPU, PERMITTED_FOR_LATE_CPUS and MATCH_ALL_EARLY_CPUS. Signed-off-by: Mikołaj Lenczewski <[email protected]> Reviewed-by: Suzuki K Poulose <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 19272b3 commit 3eb06f6

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

arch/arm64/include/asm/cpufeature.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ extern struct arm64_ftr_reg arm64_ftr_reg_ctrel0;
275275
#define ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU ((u16)BIT(5))
276276
/* Panic when a conflict is detected */
277277
#define ARM64_CPUCAP_PANIC_ON_CONFLICT ((u16)BIT(6))
278+
/*
279+
* When paired with SCOPE_LOCAL_CPU, all early CPUs must satisfy the
280+
* condition. This is different from SCOPE_SYSTEM where the check is performed
281+
* only once at the end of the SMP boot on the sanitised ID registers.
282+
* SCOPE_SYSTEM is not suitable for cases where the capability depends on
283+
* properties local to a CPU like MIDR_EL1.
284+
*/
285+
#define ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS ((u16)BIT(7))
278286

279287
/*
280288
* CPU errata workarounds that need to be enabled at boot time if one or
@@ -304,6 +312,16 @@ extern struct arm64_ftr_reg arm64_ftr_reg_ctrel0;
304312
(ARM64_CPUCAP_SCOPE_LOCAL_CPU | \
305313
ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU | \
306314
ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
315+
/*
316+
* CPU feature detected at boot time and present on all early CPUs. Late CPUs
317+
* are permitted to have the feature even if it hasn't been enabled, although
318+
* the feature will not be used by Linux in this case. If all early CPUs have
319+
* the feature, then every late CPU must have it.
320+
*/
321+
#define ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE \
322+
(ARM64_CPUCAP_SCOPE_LOCAL_CPU | \
323+
ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU | \
324+
ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS)
307325

308326
/*
309327
* CPU feature detected at boot time, on one or more CPUs. A late CPU
@@ -391,6 +409,11 @@ static inline int cpucap_default_scope(const struct arm64_cpu_capabilities *cap)
391409
return cap->type & ARM64_CPUCAP_SCOPE_MASK;
392410
}
393411

412+
static inline bool cpucap_match_all_early_cpus(const struct arm64_cpu_capabilities *cap)
413+
{
414+
return cap->type & ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS;
415+
}
416+
394417
/*
395418
* Generic helper for handling capabilities with multiple (match,enable) pairs
396419
* of call backs, sharing the same capability bit.

arch/arm64/kernel/cpufeature.c

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3370,18 +3370,49 @@ static void update_cpu_capabilities(u16 scope_mask)
33703370

33713371
scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
33723372
for (i = 0; i < ARM64_NCAPS; i++) {
3373+
bool match_all = false;
3374+
bool caps_set = false;
3375+
bool boot_cpu = false;
3376+
33733377
caps = cpucap_ptrs[i];
3374-
if (!caps || !(caps->type & scope_mask) ||
3375-
cpus_have_cap(caps->capability) ||
3376-
!caps->matches(caps, cpucap_default_scope(caps)))
3378+
if (!caps || !(caps->type & scope_mask))
33773379
continue;
33783380

3379-
if (caps->desc && !caps->cpus)
3381+
match_all = cpucap_match_all_early_cpus(caps);
3382+
caps_set = cpus_have_cap(caps->capability);
3383+
boot_cpu = scope_mask & SCOPE_BOOT_CPU;
3384+
3385+
/*
3386+
* Unless it's a match-all CPUs feature, avoid probing if
3387+
* already detected.
3388+
*/
3389+
if (!match_all && caps_set)
3390+
continue;
3391+
3392+
/*
3393+
* A match-all CPUs capability is only set when probing the
3394+
* boot CPU. It may be cleared subsequently if not detected on
3395+
* secondary ones.
3396+
*/
3397+
if (match_all && !caps_set && !boot_cpu)
3398+
continue;
3399+
3400+
if (!caps->matches(caps, cpucap_default_scope(caps))) {
3401+
if (match_all)
3402+
__clear_bit(caps->capability, system_cpucaps);
3403+
continue;
3404+
}
3405+
3406+
/*
3407+
* Match-all CPUs capabilities are logged later when the
3408+
* system capabilities are finalised.
3409+
*/
3410+
if (!match_all && caps->desc && !caps->cpus)
33803411
pr_info("detected: %s\n", caps->desc);
33813412

33823413
__set_bit(caps->capability, system_cpucaps);
33833414

3384-
if ((scope_mask & SCOPE_BOOT_CPU) && (caps->type & SCOPE_BOOT_CPU))
3415+
if (boot_cpu && (caps->type & SCOPE_BOOT_CPU))
33853416
set_bit(caps->capability, boot_cpucaps);
33863417
}
33873418
}
@@ -3782,17 +3813,24 @@ static void __init setup_system_capabilities(void)
37823813
enable_cpu_capabilities(SCOPE_ALL & ~SCOPE_BOOT_CPU);
37833814
apply_alternatives_all();
37843815

3785-
/*
3786-
* Log any cpucaps with a cpumask as these aren't logged by
3787-
* update_cpu_capabilities().
3788-
*/
37893816
for (int i = 0; i < ARM64_NCAPS; i++) {
37903817
const struct arm64_cpu_capabilities *caps = cpucap_ptrs[i];
37913818

3792-
if (caps && caps->cpus && caps->desc &&
3793-
cpumask_any(caps->cpus) < nr_cpu_ids)
3819+
if (!caps || !caps->desc)
3820+
continue;
3821+
3822+
/*
3823+
* Log any cpucaps with a cpumask as these aren't logged by
3824+
* update_cpu_capabilities().
3825+
*/
3826+
if (caps->cpus && cpumask_any(caps->cpus) < nr_cpu_ids)
37943827
pr_info("detected: %s on CPU%*pbl\n",
37953828
caps->desc, cpumask_pr_args(caps->cpus));
3829+
3830+
/* Log match-all CPUs capabilities */
3831+
if (cpucap_match_all_early_cpus(caps) &&
3832+
cpus_have_cap(caps->capability))
3833+
pr_info("detected: %s\n", caps->desc);
37963834
}
37973835

37983836
/*

0 commit comments

Comments
 (0)