Further NOP/sysreg cleanups
Subsystems (5)
cover Further NOP/sysreg cleanups
There's clearly something going round our office, as both Rutland and I ended up doing some mundane NOP and sysreg cleanups for the arm64 headers.
I plan to queue the first three patches here, but the GIC stuff can wait, as it depends on Vladimir's patch (included here for completeness) and this is hardly urgent.
Will
--->8
Vladimir Murzin (1):
arm64: KVM: Move GIC accessors to arch_gicv3.h
Will Deacon (5):
arm64: sysreg: replace open-coded mrs_s/msr_s with
{read,write}_sysreg_s
arm64: barriers: introduce nops and __nops macros for NOP sequences
arm64: lse: convert lse alternatives NOP padding to use __nops irqchip/gic-v3: Convert arm64 GIC accessors to {read,write}_sysreg_s irqchip/gic-v3: Use nops macro for Cavium ThunderX erratum 23154
arch/arm64/include/asm/arch_gicv3.h | 33 +++++++++---------- arch/arm64/include/asm/assembler.h | 9 ++++++ arch/arm64/include/asm/atomic_lse.h | 64 ++++++++++++++++--------------------- arch/arm64/include/asm/barrier.h | 3 ++ arch/arm64/include/asm/cmpxchg.h | 4 +-- arch/arm64/include/asm/cputype.h | 6 +--- arch/arm64/include/asm/spinlock.h | 27 +++++++--------- arch/arm64/include/asm/sysreg.h | 16 ++++++++++ arch/arm64/kvm/hyp/vgic-v3-sr.c | 13 -------- 9 files changed, 83 insertions(+), 92 deletions(-)
Signature · 1 line
2.1.4
PATCH 1/6 arm64: sysreg: replace open-coded mrs_s/msr_s with {read, write}_sysreg_s
Similar to our {read,write}_sysreg accessors for architected, named system registers, this patch introduces {read,write}_sysreg_s variants that can take arbitrary sys_reg output and therefore access IMPDEF registers or registers that unsupported by binutils.
--- arch/arm64/include/asm/cputype.h | 6 +----- arch/arm64/include/asm/sysreg.h | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-)
arch/arm64/include/asm/cputype.h +1 -5
@@ -93,11 +93,7 @@#include <asm/sysreg.h> -#define read_cpuid (reg) ({ \ - u64 __val; \ - asm("mrs_s %0, " __stringify(SYS_ ## reg) : "=r" (__val)); \ - __val; \ -}) +#define read_cpuid (reg) read_sysreg_s(SYS_ ## reg)/* * The CPU ID never changes at run time, so we might as well tell the
arch/arm64/include/asm/sysreg.h +16 -0
@@ -279,6 +279,22 @@ static inline void config_sctlr_el1(u32 clear, u32 set) : : "r" (__val)); \ } while (0) +/* +* For registers without architectural names, or simply unsupported by +* GAS. + */ +#define read_sysreg_s (r) ({ \ + u64 __val; \ + asm volatile("mrs_s %0, " __stringify(r) : "=r" (__val)); \ + __val; \ +}) + +#define write_sysreg_s (v, r) do { \ + u64 __val = (u64)v; \ + asm volatile("msr_s " __stringify(r) ", %0" \ + : : "r" (__val)); \ +} while (0) +#endif #endif /* __ASM_SYSREG_H */
Signature · 1 line
2.1.4
Mark Rutland
reviewerPATCH 1/6 arm64: sysreg: replace open-coded mrs_s/msr_s with {read,write}_sysreg_s
Quoted 11 lines · Will Deacon
Similar to our {read,write}_sysreg accessors for architected, named system registers, this patch introduces {read,write}_sysreg_s variants that can take arbitrary
sys_regoutput and therefore access IMPDEF registers or registers that unsupported by binutils.Signed-off-by Will Deacon <[email protected]>--- arch/arm64/include/asm/cputype.h | 6 +----- arch/arm64/include/asm/sysreg.h | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-)
arch/arm64/include/asm/sysreg.h static inline void config_sctlr_el1(u32 clear, u32 set) · line 296
Quoted 5 lines
arch/arm64/include/asm/sysreg.h +5
-0+#define write_sysreg_s (v, r) do { \ + u64 __val = (u64)v; \ + asm volatile("msr_s " __stringify(r) ", %0" \ + : : "r" (__val)); \ +} while (0)
As with my patches, can we please make write_sysreg_s allow the zero register? i.e. use the "%x0" tempate and "rZ" constraint. The mrs_s asm template should handle that correctly.
With that:
Thanks,
Mark.
PATCH 2/6 arm64: barriers: introduce nops and __nops macros for NOP sequences
In reply to Will DeaconNOP sequences tend to get used for padding out alternative sections and uarch-specific pipeline flushes in errata workarounds.
This patch adds macros for generating these sequences as both inline asm blocks, but also as strings suitable for embedding in other asm blocks directly.
--- arch/arm64/include/asm/assembler.h | 9 +++++++++ arch/arm64/include/asm/barrier.h | 3 +++ 2 files changed, 12 insertions(+)
arch/arm64/include/asm/assembler.h +9 -0
@@ -87,6 +87,15 @@ .endm /* + * NOP sequence + */ + .macro nops, num + .rept \num + nop + .endr + .endm + +/* * Emit an entry into the exception table */ .macro _asm_extable, from, to
arch/arm64/include/asm/barrier.h +3 -0
@@ -20,6 +20,9 @@#ifndef __ASSEMBLY__ +#define __nops (n) ".rept " #n "\nnop\n.endr\n" +#define nops (n) asm volatile(__nops(n)) +#define sev () asm volatile("sev" : : : "memory")#define wfe () asm volatile("wfe" : : : "memory")#define wfi () asm volatile("wfi" : : : "memory")
Signature · 1 line
2.1.4
PATCH 3/6 arm64: lse: convert lse alternatives NOP padding to use __nops
In reply to Will DeaconThe LSE atomics are implemented using alternative code sequences of different lengths, and explicit NOP padding is used to ensure the patching works correctly.
This patch converts the bulk of the LSE code over to using the __nops macro, which makes it slightly clearer as to what is going on and also consolidates all of the padding at the end of the various sequences.
--- arch/arm64/include/asm/atomic_lse.h | 64 ++++++++++++++++--------------------- arch/arm64/include/asm/cmpxchg.h | 4 +-- arch/arm64/include/asm/spinlock.h | 27 +++++++--------- 3 files changed, 39 insertions(+), 56 deletions(-)
arch/arm64/include/asm/atomic_lse.h +27 -37
@@ -86,8 +86,8 @@ static inline int atomic_add_return##name(int i, atomic_t *v) \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ -" nop\n" \ - __LL_SC_ATOMIC(add_return##name), \ + __LL_SC_ATOMIC(add_return##name) \ + __nops(1), \ /* LSE atomics */ \ " ldadd" #mb " %w[i], w30, %[v]\n" \ " add %w[i], %w[i], w30") \
@@ -112,8 +112,8 @@ static inline void atomic_and(int i, atomic_t *v) asm volatile(ARM64_LSE_ATOMIC_INSN(/* LL/SC */ -" nop\n" - __LL_SC_ATOMIC(and), + __LL_SC_ATOMIC(and) + __nops(1),/* LSE atomics */ " mvn %w[i], %w[i]\n" " stclr %w[i], %[v]" )
@@ -130,8 +130,8 @@ static inline int atomic_fetch_and##name(int i, atomic_t *v) \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ -" nop\n" \ - __LL_SC_ATOMIC(fetch_and##name), \ + __LL_SC_ATOMIC(fetch_and##name) \ + __nops(1), \ /* LSE atomics */ \" mvn %w[i], %w[i]\n" \ " ldclr" #mb " %w[i], %w[i], %[v]") \
@@ -156,8 +156,8 @@ static inline void atomic_sub(int i, atomic_t *v) asm volatile(ARM64_LSE_ATOMIC_INSN(/* LL/SC */ -" nop\n" - __LL_SC_ATOMIC(sub), + __LL_SC_ATOMIC(sub) + __nops(1),/* LSE atomics */ " neg %w[i], %w[i]\n" " stadd %w[i], %[v]" )
@@ -174,9 +174,8 @@ static inline int atomic_sub_return##name(int i, atomic_t *v) \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ - " nop\n" \ __LL_SC_ATOMIC(sub_return##name) \ - " nop", \ + __nops(2), \ /* LSE atomics */ \" neg %w[i], %w[i]\n" \ " ldadd" #mb " %w[i], w30, %[v]\n" \
@@ -203,8 +202,8 @@ static inline int atomic_fetch_sub##name(int i, atomic_t *v) \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ -" nop\n" \ - __LL_SC_ATOMIC(fetch_sub##name), \ + __LL_SC_ATOMIC(fetch_sub##name) \ + __nops(1), \ /* LSE atomics */ \" neg %w[i], %w[i]\n" \ " ldadd" #mb " %w[i], %w[i], %[v]") \
@@ -284,8 +283,8 @@ static inline long atomic64_add_return##name(long i, atomic64_t *v) \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ -" nop\n" \ - __LL_SC_ATOMIC64(add_return##name), \ + __LL_SC_ATOMIC64(add_return##name) \ + __nops(1), \ /* LSE atomics */ \ " ldadd" #mb " %[i], x30, %[v]\n" \ " add %[i], %[i], x30") \
@@ -310,8 +309,8 @@ static inline void atomic64_and(long i, atomic64_t *v) asm volatile(ARM64_LSE_ATOMIC_INSN(/* LL/SC */ -" nop\n" - __LL_SC_ATOMIC64(and), + __LL_SC_ATOMIC64(and) + __nops(1),/* LSE atomics */ " mvn %[i], %[i]\n" " stclr %[i], %[v]" )
@@ -328,8 +327,8 @@ static inline long atomic64_fetch_and##name(long i, atomic64_t *v) \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ -" nop\n" \ - __LL_SC_ATOMIC64(fetch_and##name), \ + __LL_SC_ATOMIC64(fetch_and##name) \ + __nops(1), \ /* LSE atomics */ \" mvn %[i], %[i]\n" \ " ldclr" #mb " %[i], %[i], %[v]") \
@@ -354,8 +353,8 @@ static inline void atomic64_sub(long i, atomic64_t *v) asm volatile(ARM64_LSE_ATOMIC_INSN(/* LL/SC */ -" nop\n" - __LL_SC_ATOMIC64(sub), + __LL_SC_ATOMIC64(sub) + __nops(1),/* LSE atomics */ " neg %[i], %[i]\n" " stadd %[i], %[v]" )
@@ -372,9 +371,8 @@ static inline long atomic64_sub_return##name(long i, atomic64_t *v) \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ - " nop\n" \ __LL_SC_ATOMIC64(sub_return##name) \ - " nop", \ + __nops(2), \ /* LSE atomics */ \" neg %[i], %[i]\n" \ " ldadd" #mb " %[i], x30, %[v]\n" \
@@ -401,8 +399,8 @@ static inline long atomic64_fetch_sub##name(long i, atomic64_t *v) \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ -" nop\n" \ - __LL_SC_ATOMIC64(fetch_sub##name), \ + __LL_SC_ATOMIC64(fetch_sub##name) \ + __nops(1), \ /* LSE atomics */ \" neg %[i], %[i]\n" \ " ldadd" #mb " %[i], %[i], %[v]") \
@@ -426,13 +424,8 @@ static inline long atomic64_dec_if_positive(atomic64_t *v) asm volatile(ARM64_LSE_ATOMIC_INSN(/* LL/SC */ -" nop\n" __LL_SC_ATOMIC64(dec_if_positive) -" nop\n" -" nop\n" -" nop\n" -" nop\n" -" nop" , + __nops(6),/* LSE atomics */ "1: ldr x30, %[v]\n" " subs %[ret], x30, #1\n"
@@ -464,9 +457,8 @@ static inline unsigned long __cmpxchg_case_##name(volatile void *ptr, \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ -" nop\n" \ - __LL_SC_CMPXCHG(name) \ -" nop" , \ + __LL_SC_CMPXCHG(name) \ + __nops(2), \ /* LSE atomics */ \ " mov " #w "30, %" #w "[old]\n" \ " cas" #mb #sz "\t" #w "30, %" #w "[new], %[v]\n" \
@@ -517,10 +509,8 @@ static inline long __cmpxchg_double##name(unsigned long old1, \ \ asm volatile(ARM64_LSE_ATOMIC_INSN( \ /* LL/SC */ \ -" nop\n" \ -" nop\n" \ -" nop\n" \ - __LL_SC_CMPXCHG_DBL(name), \ + __LL_SC_CMPXCHG_DBL(name) \ + __nops(3), \ /* LSE atomics */ \ " casp" #mb "\t%[old1], %[old2], %[new1], %[new2], %[v]\n"\ " eor %[old1], %[old1], %[oldval1]\n" \
arch/arm64/include/asm/cmpxchg.h +1 -3
@@ -43,10 +43,8 @@ static inline unsigned long __xchg_case_##name(unsigned long x, \" cbnz %w1, 1b\n" \ " " #mb, \/* LSE atomics */ \ -" nop\n" \ -" nop\n" \ " swp" #acq_lse #rel #sz "\t%" #w "3, %" #w "0, %2\n" \ - " nop\n" \ + __nops(3) \ " " #nop_lse) \ : "=&r" (ret), "=&r" (tmp), "+Q" (*(u8 *)ptr) \ : "r" (x) \
arch/arm64/include/asm/spinlock.h +11 -16
@@ -66,8 +66,7 @@ static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)ARM64_LSE_ATOMIC_INSN (/* LL/SC */ " stxr %w1, %w0, %2\n" -" nop\n" -" nop\n" , + __nops(2 ),/* LSE atomics */ " mov %w1, %w0\n" " cas %w0, %w0, %2\n"
@@ -99,9 +98,7 @@ static inline void arch_spin_lock(arch_spinlock_t *lock)/* LSE atomics */ " mov %w2, %w5\n" " ldadda %w2, %w0, %3\n" -" nop\n" -" nop\n" -" nop\n" + __nops(3 ) )/* Did we get the lock? */
@@ -165,8 +162,8 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock) " stlrh %w1, %0",/* LSE atomics */ " mov %w1, #1\n" - " nop\n" - " staddlh %w1, %0") +" staddlh %w1, %0\n" + __nops(1)) : "=Q" (lock->owner), "=&r" (tmp) : : "memory");
@@ -212,7 +209,7 @@ static inline void arch_write_lock(arch_rwlock_t *rw)" cbnz %w0, 1b\n" " stxr %w0, %w2, %1\n" " cbnz %w0, 2b\n" -" nop" , + __nops(1 ),/* LSE atomics */ "1: mov %w0, wzr\n" "2: casa %w0, %w2, %1\n"
@@ -241,8 +238,7 @@ static inline int arch_write_trylock(arch_rwlock_t *rw)/* LSE atomics */ " mov %w0, wzr\n" " casa %w0, %w2, %1\n" -" nop\n" - " nop") + __nops(2)) : "=&r" (tmp), "+Q" (rw->lock) : "r" (0x80000000) : "memory");
@@ -290,8 +286,8 @@ static inline void arch_read_lock(arch_rwlock_t *rw)" add %w0, %w0, #1\n" " tbnz %w0, #31, 1b\n" " stxr %w1, %w0, %2\n" -" nop\n" -" cbnz %w1, 2b" , +" cbnz %w1, 2b\n" + __nops(1 ),/* LSE atomics */ "1: wfe\n" "2: ldxr %w0, %2\n"
@@ -317,9 +313,8 @@ static inline void arch_read_unlock(arch_rwlock_t *rw) " cbnz %w1, 1b",/* LSE atomics */ " movn %w0, #0\n" -" nop\n" -" nop\n" - " staddl %w0, %2") +" staddl %w0, %2\n" + __nops(2)) : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock) : : "memory");
@@ -344,7 +339,7 @@ static inline int arch_read_trylock(arch_rwlock_t *rw)" tbnz %w1, #31, 1f\n" " casa %w0, %w1, %2\n" " sbc %w1, %w1, %w0\n" - " nop\n" + __nops(1) "1:") : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock) :
Signature · 1 line
2.1.4
PATCH 4/6 arm64: KVM: Move GIC accessors to arch_gicv3.h
In reply to Will DeaconFrom: Vladimir Murzin <[email protected]>
Since we are going to share vgic-v3 save/restore code with ARM keep arch specific accessors separately.
--- arch/arm64/include/asm/arch_gicv3.h | 13 +++++++++++++ arch/arm64/kvm/hyp/vgic-v3-sr.c | 13 ------------- 2 files changed, 13 insertions(+), 13 deletions(-)
arch/arm64/include/asm/arch_gicv3.h +13 -0
@@ -79,6 +79,19 @@#include <linux/stringify.h> #include <asm/barrier.h> +#define read_gicreg (r) \ + ({ \ + u64 reg; \ + asm volatile("mrs_s %0, " __stringify(r) : "=r" (reg)); \ + reg; \ + }) + +#define write_gicreg (v,r) \ + do { \ + u64 __val = (v); \ + asm volatile("msr_s " __stringify(r) ", %0" : : "r" (__val));\ + } while (0) +/* * Low-level accessors *
arch/arm64/kvm/hyp/vgic-v3-sr.c +0 -13
@@ -24,19 +24,6 @@#define vtr_to_max_lr_idx (v) ((v) & 0xf)#define vtr_to_nr_pri_bits (v) (((u32)(v) >> 29) + 1) -#define read_gicreg (r) \ - ({ \ - u64 reg; \ - asm volatile("mrs_s %0, " __stringify(r) : "=r" (reg)); \ - reg; \ - }) - -#define write_gicreg (v,r) \ - do { \ - u64 __val = (v); \ - asm volatile("msr_s " __stringify(r) ", %0" : : "r" (__val));\ - } while (0) - static u64 __hyp_text __gic_v3_get_lr(unsigned int lr) {switch (lr &0xf ) {
Signature · 1 line
2.1.4
PATCH 5/6 irqchip/gic-v3: Convert arm64 GIC accessors to {read, write}_sysreg_s
In reply to Will DeaconThe GIC system registers are accessed using open-coded wrappers around the mrs_s/msr_s asm macros.
This patch moves the code over to the {read,wrote}_sysreg_s accessors instead, reducing the amount of explicit asm blocks in the arch headers.
--- arch/arm64/include/asm/arch_gicv3.h | 45 ++++++++++++++----------------------- 1 file changed, 17 insertions(+), 28 deletions(-)
arch/arm64/include/asm/arch_gicv3.h +17 -28
@@ -79,18 +79,8 @@#include <linux/stringify.h> #include <asm/barrier.h> -#define read_gicreg (r) \ - ({ \ - u64 reg; \ - asm volatile("mrs_s %0, " __stringify(r) : "=r" (reg)); \ - reg; \ - }) - -#define write_gicreg (v,r) \ - do { \ - u64 __val = (v); \ - asm volatile("msr_s " __stringify(r) ", %0" : : "r" (__val));\ - } while (0) +#define read_gicreg read_sysreg_s +#define write_gicreg write_sysreg_s/* * Low-level accessors
@@ -101,13 +91,13 @@static inline void gic_write_eoir (u32 irq) { - asm volatile("msr_s " __stringify(ICC_EOIR1_EL1) ", %0" : : "r" ((u64)irq)); +write_sysreg_s (irq,ICC_EOIR1_EL1 );isb (); }static inline void gic_write_dir (u32 irq) { - asm volatile("msr_s " __stringify(ICC_DIR_EL1) ", %0" : : "r" ((u64)irq)); +write_sysreg_s (irq,ICC_DIR_EL1 );isb (); }
@@ -115,7 +105,7 @@ static inline u64 gic_read_iar_common(void) {u64 irqstat; - asm volatile("mrs_s %0, " __stringify(ICC_IAR1_EL1) : "=r" (irqstat)); + irqstat =read_sysreg_s (ICC_IAR1_EL1 );dsb (sy);return irqstat; }
@@ -133,10 +123,12 @@ static inline u64 gic_read_iar_cavium_thunderx(void) asmvolatile ("nop;nop;nop;nop\n\t" -"nop;nop;nop;nop\n\t" - "mrs_s %0, " __stringify(ICC_IAR1_EL1) "\n\t" - "nop;nop;nop;nop" - : "=r" (irqstat)); +"nop;nop;nop;nop" ); + + irqstat =read_sysreg_s (ICC_IAR1_EL1 ); + + asmvolatile ( +"nop;nop;nop;nop" );mb ();return irqstat;
@@ -144,37 +136,34 @@ static inline u64 gic_read_iar_cavium_thunderx(void)static inline void gic_write_pmr (u32 val) { - asm volatile("msr_s " __stringify(ICC_PMR_EL1) ", %0" : : "r" ((u64)val)); +write_sysreg_s (val,ICC_PMR_EL1 ); }static inline void gic_write_ctlr (u32 val) { - asm volatile("msr_s " __stringify(ICC_CTLR_EL1) ", %0" : : "r" ((u64)val)); +write_sysreg_s (val,ICC_CTLR_EL1 );isb (); }static inline void gic_write_grpen1 (u32 val) { - asm volatile("msr_s " __stringify(ICC_GRPEN1_EL1) ", %0" : : "r" ((u64)val)); +write_sysreg_s (val,ICC_GRPEN1_EL1 );isb (); }static inline void gic_write_sgi1r (u64 val) { - asm volatile("msr_s " __stringify(ICC_SGI1R_EL1) ", %0" : : "r" (val)); +write_sysreg_s (val,ICC_SGI1R_EL1 ); }static inline u32 gic_read_sre (void ) { -u64 val; - - asm volatile("mrs_s %0, " __stringify(ICC_SRE_EL1) : "=r" (val)); -return val; +return read_sysreg_s (ICC_SRE_EL1 ); }static inline void gic_write_sre (u32 val) { - asm volatile("msr_s " __stringify(ICC_SRE_EL1) ", %0" : : "r" ((u64)val)); +write_sysreg_s (val,ICC_SRE_EL1 );isb (); }
Signature · 1 line
2.1.4
PATCH 6/6 irqchip/gic-v3: Use nops macro for Cavium ThunderX erratum 23154
In reply to Will DeaconThe workaround for Cavium ThunderX erratum 23154 has a homebrew pipeflush built out of NOP sequences around the read of the IAR.
This patch converts the code to use the new nops macro, which makes it a little easier to read.
--- arch/arm64/include/asm/arch_gicv3.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
arch/arm64/include/asm/arch_gicv3.h +2 -7
@@ -121,14 +121,9 @@ static inline u64 gic_read_iar_cavium_thunderx(void) {u64 irqstat; - asmvolatile ( -"nop;nop;nop;nop\n\t" -"nop;nop;nop;nop" ); - +nops (8 ); irqstat =read_sysreg_s (ICC_IAR1_EL1 ); - - asmvolatile ( -"nop;nop;nop;nop" ); +nops (4 );mb ();return irqstat;
Signature · 1 line
2.1.4