Skip to content

Commit fa9bb93

Browse files
marcanjannau
authored andcommitted
arm64: Implement Apple IMPDEF TSO memory model control
Apple CPUs may implement the TSO memory model as an optional configurable mode. This allows x86 emulators to simplify their load/store handling, greatly increasing performance. Expose this via the prctl PR_SET_MEM_MODEL_TSO mechanism. We use the Apple IMPDEF AIDR_EL1 register to check for the availability of TSO mode, and enable this codepath on all CPUs with an Apple implementer. This relies on the ACTLR_EL1 thread state scaffolding introduced earlier. Signed-off-by: Hector Martin <[email protected]> Reviewed-by: Neal Gompa <[email protected]>
1 parent cc2de8c commit fa9bb93

6 files changed

Lines changed: 65 additions & 1 deletion

File tree

arch/arm64/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,8 @@ endif # ARM64_PSEUDO_NMI
22552255

22562256
config ARM64_MEMORY_MODEL_CONTROL
22572257
bool "Runtime memory model control"
2258+
default ARCH_APPLE
2259+
select ARM64_ACTLR_STATE
22582260
help
22592261
Some ARM64 CPUs support runtime switching of the CPU memory
22602262
model, which can be useful to emulate other CPU architectures
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#ifndef __ASM_APPLE_CPUFEATURES_H
4+
#define __ASM_APPLE_CPUFEATURES_H
5+
6+
#include <linux/bits.h>
7+
#include <asm/sysreg.h>
8+
9+
#define AIDR_APPLE_TSO_SHIFT 9
10+
#define AIDR_APPLE_TSO BIT(9)
11+
12+
#define ACTLR_APPLE_TSO_SHIFT 1
13+
#define ACTLR_APPLE_TSO BIT(1)
14+
15+
#endif

arch/arm64/include/asm/cpufeature.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,8 @@ static inline unsigned int get_vmid_bits(u64 mmfr1)
917917

918918
static __always_inline bool system_has_actlr_state(void)
919919
{
920-
return false;
920+
return IS_ENABLED(CONFIG_ARM64_ACTLR_STATE) &&
921+
alternative_has_cap_unlikely(ARM64_HAS_TSO_APPLE);
921922
}
922923

923924
s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);

arch/arm64/kernel/cpufeature_impdef.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@
44
*/
55

66
#include <asm/cpufeature.h>
7+
#include <asm/apple_cpufeature.h>
78

89
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
10+
static bool has_apple_feature(const struct arm64_cpu_capabilities *entry, int scope)
11+
{
12+
u64 val;
13+
WARN_ON(scope != SCOPE_SYSTEM);
14+
15+
if (read_cpuid_implementor() != ARM_CPU_IMP_APPLE)
16+
return false;
17+
18+
val = read_sysreg(aidr_el1);
19+
return cpufeature_matches(val, entry);
20+
}
21+
922
static bool has_tso_fixed(const struct arm64_cpu_capabilities *entry, int scope)
1023
{
1124
/* List of CPUs that always use the TSO memory model */
@@ -22,6 +35,16 @@ static bool has_tso_fixed(const struct arm64_cpu_capabilities *entry, int scope)
2235

2336
static const struct arm64_cpu_capabilities arm64_impdef_features[] = {
2437
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
38+
{
39+
.desc = "TSO memory model (Apple)",
40+
.capability = ARM64_HAS_TSO_APPLE,
41+
.type = ARM64_CPUCAP_SYSTEM_FEATURE,
42+
.matches = has_apple_feature,
43+
.field_pos = AIDR_APPLE_TSO_SHIFT,
44+
.field_width = 1,
45+
.sign = FTR_UNSIGNED,
46+
.min_field_value = 1,
47+
},
2548
{
2649
.desc = "TSO memory model (Fixed)",
2750
.capability = ARM64_HAS_TSO_FIXED,

arch/arm64/kernel/process.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <linux/memory_ordering_model.h>
4545

4646
#include <asm/alternative.h>
47+
#include <asm/apple_cpufeature.h>
4748
#include <asm/arch_timer.h>
4849
#include <asm/compat.h>
4950
#include <asm/cpufeature.h>
@@ -574,6 +575,10 @@ void update_sctlr_el1(u64 sctlr)
574575
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
575576
int arch_prctl_mem_model_get(struct task_struct *t)
576577
{
578+
if (alternative_has_cap_unlikely(ARM64_HAS_TSO_APPLE) &&
579+
t->thread.actlr & ACTLR_APPLE_TSO)
580+
return PR_SET_MEM_MODEL_TSO;
581+
577582
return PR_SET_MEM_MODEL_DEFAULT;
578583
}
579584

@@ -583,6 +588,23 @@ int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val)
583588
val == PR_SET_MEM_MODEL_TSO)
584589
return 0;
585590

591+
if (alternative_has_cap_unlikely(ARM64_HAS_TSO_APPLE)) {
592+
WARN_ON(!system_has_actlr_state());
593+
594+
switch (val) {
595+
case PR_SET_MEM_MODEL_TSO:
596+
t->thread.actlr |= ACTLR_APPLE_TSO;
597+
break;
598+
case PR_SET_MEM_MODEL_DEFAULT:
599+
t->thread.actlr &= ~ACTLR_APPLE_TSO;
600+
break;
601+
default:
602+
return -EINVAL;
603+
}
604+
write_sysreg(t->thread.actlr, actlr_el1);
605+
return 0;
606+
}
607+
586608
if (val == PR_SET_MEM_MODEL_DEFAULT)
587609
return 0;
588610

arch/arm64/tools/cpucaps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ HAS_STAGE2_FWB
5353
HAS_TCR2
5454
HAS_TIDCP1
5555
HAS_TLB_RANGE
56+
HAS_TSO_APPLE
5657
HAS_TSO_FIXED
5758
HAS_VA52
5859
HAS_VIRT_HOST_EXTN

0 commit comments

Comments
 (0)