git: b3e05bc282b6 - stable/13 - Store mpidr as a 64-bit value on arm64

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Wed, 21 Sep 2022 09:46:44 UTC
The branch stable/13 has been updated by andrew:

URL: https://cgit.FreeBSD.org/src/commit/?id=b3e05bc282b65e08ad34d734cee8f1af087f38d8

commit b3e05bc282b65e08ad34d734cee8f1af087f38d8
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2022-08-25 08:28:28 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2022-09-21 09:45:52 +0000

    Store mpidr as a 64-bit value on arm64
    
    The mpidr register is 64 bit on arm64 and 32 bit on arm. Fix this by
    extending the arm64 definition to include the top 32 bits.
    
    To preserve KBI when MFCing split the value into two 32 bit values.
    This will be cleaned up later only on main.
    
    Reviewed by:    bz
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D36346
    
    (cherry picked from commit 544f047f894046a68c373f55ddd072e91bcfbf38)
---
 sys/arm/arm/pmu_fdt.c        |  2 +-
 sys/arm/include/pcpu.h       |  2 ++
 sys/arm64/arm64/machdep.c    |  3 ++-
 sys/arm64/arm64/mp_machdep.c | 17 +++++++++++------
 sys/arm64/include/pcpu.h     |  9 +++++++--
 5 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/sys/arm/arm/pmu_fdt.c b/sys/arm/arm/pmu_fdt.c
index 2e03fc98bfe0..6590e0659d82 100644
--- a/sys/arm/arm/pmu_fdt.c
+++ b/sys/arm/arm/pmu_fdt.c
@@ -105,7 +105,7 @@ pmu_parse_affinity(device_t dev, struct pmu_softc *sc, struct pmu_intr *irq,
 
 	for (i = 0; i < MAXCPU; i++) {
 		pcpu = pcpu_find(i);
-		if (pcpu != NULL && pcpu->pc_mpidr == mpidr) {
+		if (pcpu != NULL && PCPU_GET_MPIDR(pcpu) == mpidr) {
 			irq->cpuid = i;
 			return (0);
 		}
diff --git a/sys/arm/include/pcpu.h b/sys/arm/include/pcpu.h
index 4d609b10bf73..a3a6900f692d 100644
--- a/sys/arm/include/pcpu.h
+++ b/sys/arm/include/pcpu.h
@@ -140,6 +140,8 @@ set_tls(void *tls)
 #define	PCPU_PTR(member)	(&get_pcpu()->pc_ ## member)
 #define	PCPU_SET(member,value)	(get_pcpu()->pc_ ## member = (value))
 
+#define	PCPU_GET_MPIDR(pc)	((pc)->pc_mpidr)
+
 void pcpu0_init(void);
 #endif	/* _KERNEL */
 
diff --git a/sys/arm64/arm64/machdep.c b/sys/arm64/arm64/machdep.c
index 14d0dcbf6c2f..6e2ec7d39ed2 100644
--- a/sys/arm64/arm64/machdep.c
+++ b/sys/arm64/arm64/machdep.c
@@ -300,7 +300,8 @@ cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
 {
 
 	pcpu->pc_acpi_id = 0xffffffff;
-	pcpu->pc_mpidr = 0xffffffff;
+	pcpu->pc_mpidr_low = 0xffffffff;
+	pcpu->pc_mpidr_high = 0xffffffff;
 }
 
 void
diff --git a/sys/arm64/arm64/mp_machdep.c b/sys/arm64/arm64/mp_machdep.c
index 8443c766923b..4d3f9f4963f5 100644
--- a/sys/arm64/arm64/mp_machdep.c
+++ b/sys/arm64/arm64/mp_machdep.c
@@ -154,7 +154,7 @@ static bool
 is_boot_cpu(uint64_t target_cpu)
 {
 
-	return (cpuid_to_pcpu[0]->pc_mpidr == (target_cpu & CPU_AFF_MASK));
+	return (PCPU_GET_MPIDR(cpuid_to_pcpu[0]) == (target_cpu & CPU_AFF_MASK));
 }
 
 static void
@@ -208,7 +208,7 @@ init_secondary(uint64_t cpu)
 {
 	struct pcpu *pcpup;
 	pmap_t pmap0;
-	u_int mpidr;
+	uint64_t mpidr;
 
 	/*
 	 * Verify that the value passed in 'cpu' argument (aka context_id) is
@@ -217,10 +217,10 @@ init_secondary(uint64_t cpu)
 	 */
 	mpidr = READ_SPECIALREG(mpidr_el1) & CPU_AFF_MASK;
 	if (cpu >= MAXCPU || cpuid_to_pcpu[cpu] == NULL ||
-	    cpuid_to_pcpu[cpu]->pc_mpidr != mpidr) {
+	    PCPU_GET_MPIDR(cpuid_to_pcpu[cpu]) != mpidr) {
 		for (cpu = 0; cpu < mp_maxid; cpu++)
 			if (cpuid_to_pcpu[cpu] != NULL &&
-			    cpuid_to_pcpu[cpu]->pc_mpidr == mpidr)
+			    PCPU_GET_MPIDR(cpuid_to_pcpu[cpu]) == mpidr)
 				break;
 		if ( cpu >= MAXCPU)
 			panic("MPIDR for this CPU is not in pcpu table");
@@ -517,7 +517,8 @@ start_cpu(u_int cpuid, uint64_t target_cpu, int domain)
 
 	pcpup = (struct pcpu *)pcpu_mem;
 	pcpu_init(pcpup, cpuid, sizeof(struct pcpu));
-	pcpup->pc_mpidr = target_cpu & CPU_AFF_MASK;
+	pcpup->pc_mpidr_low = target_cpu & CPU_AFF_MASK;
+	pcpup->pc_mpidr_high = (target_cpu & CPU_AFF_MASK) >> 32;
 
 	dpcpu[cpuid - 1] = (void *)(pcpup + 1);
 	dpcpu_init(dpcpu[cpuid - 1], cpuid);
@@ -688,11 +689,15 @@ cpu_init_fdt(void)
 void
 cpu_mp_start(void)
 {
+	uint64_t mpidr;
+
 	mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
 
 	/* CPU 0 is always boot CPU. */
 	CPU_SET(0, &all_cpus);
-	cpuid_to_pcpu[0]->pc_mpidr = READ_SPECIALREG(mpidr_el1) & CPU_AFF_MASK;
+	mpidr = READ_SPECIALREG(mpidr_el1) & CPU_AFF_MASK;
+	cpuid_to_pcpu[0]->pc_mpidr_low = mpidr;
+	cpuid_to_pcpu[0]->pc_mpidr_high = mpidr >> 32;
 
 	switch(arm64_bus_method) {
 #ifdef DEV_ACPI
diff --git a/sys/arm64/include/pcpu.h b/sys/arm64/include/pcpu.h
index d83c9a656634..8ec4a33b0d67 100644
--- a/sys/arm64/include/pcpu.h
+++ b/sys/arm64/include/pcpu.h
@@ -48,8 +48,10 @@ struct debug_monitor_state;
 	struct pmap *pc_curpmap;					\
 	struct pmap *pc_curvmpmap;					\
 	u_int	pc_bcast_tlbi_workaround;				\
-	u_int	pc_mpidr;	/* stored MPIDR value */		\
-	char __pad[201]
+	/* Store as two u_int values to preserve KBI */			\
+	u_int	pc_mpidr_low;	/* lower MPIDR 32 bits */		\
+	u_int	pc_mpidr_high;	/* upper MPIDR 32 bits */		\
+	char __pad[197]
 
 #ifdef _KERNEL
 
@@ -84,6 +86,9 @@ get_curthread(void)
 #define	PCPU_PTR(member)	(&pcpup->pc_ ## member)
 #define	PCPU_SET(member,value)	(pcpup->pc_ ## member = (value))
 
+#define	PCPU_GET_MPIDR(pc)	\
+    ((((uint64_t)((pc)->pc_mpidr_high)) << 32) | ((pc)->pc_mpidr_low))
+
 #endif	/* _KERNEL */
 
 #endif	/* !_MACHINE_PCPU_H_ */