162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2020 Google LLC. 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci#ifndef __ASM_RWONCE_H 662306a36Sopenharmony_ci#define __ASM_RWONCE_H 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#if defined(CONFIG_LTO) && !defined(__ASSEMBLY__) 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <linux/compiler_types.h> 1162306a36Sopenharmony_ci#include <asm/alternative-macros.h> 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#ifndef BUILD_VDSO 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#ifdef CONFIG_AS_HAS_LDAPR 1662306a36Sopenharmony_ci#define __LOAD_RCPC(sfx, regs...) \ 1762306a36Sopenharmony_ci ALTERNATIVE( \ 1862306a36Sopenharmony_ci "ldar" #sfx "\t" #regs, \ 1962306a36Sopenharmony_ci ".arch_extension rcpc\n" \ 2062306a36Sopenharmony_ci "ldapr" #sfx "\t" #regs, \ 2162306a36Sopenharmony_ci ARM64_HAS_LDAPR) 2262306a36Sopenharmony_ci#else 2362306a36Sopenharmony_ci#define __LOAD_RCPC(sfx, regs...) "ldar" #sfx "\t" #regs 2462306a36Sopenharmony_ci#endif /* CONFIG_AS_HAS_LDAPR */ 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci/* 2762306a36Sopenharmony_ci * When building with LTO, there is an increased risk of the compiler 2862306a36Sopenharmony_ci * converting an address dependency headed by a READ_ONCE() invocation 2962306a36Sopenharmony_ci * into a control dependency and consequently allowing for harmful 3062306a36Sopenharmony_ci * reordering by the CPU. 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * Ensure that such transformations are harmless by overriding the generic 3362306a36Sopenharmony_ci * READ_ONCE() definition with one that provides RCpc acquire semantics 3462306a36Sopenharmony_ci * when building with LTO. 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_ci#define __READ_ONCE(x) \ 3762306a36Sopenharmony_ci({ \ 3862306a36Sopenharmony_ci typeof(&(x)) __x = &(x); \ 3962306a36Sopenharmony_ci int atomic = 1; \ 4062306a36Sopenharmony_ci union { __unqual_scalar_typeof(*__x) __val; char __c[1]; } __u; \ 4162306a36Sopenharmony_ci switch (sizeof(x)) { \ 4262306a36Sopenharmony_ci case 1: \ 4362306a36Sopenharmony_ci asm volatile(__LOAD_RCPC(b, %w0, %1) \ 4462306a36Sopenharmony_ci : "=r" (*(__u8 *)__u.__c) \ 4562306a36Sopenharmony_ci : "Q" (*__x) : "memory"); \ 4662306a36Sopenharmony_ci break; \ 4762306a36Sopenharmony_ci case 2: \ 4862306a36Sopenharmony_ci asm volatile(__LOAD_RCPC(h, %w0, %1) \ 4962306a36Sopenharmony_ci : "=r" (*(__u16 *)__u.__c) \ 5062306a36Sopenharmony_ci : "Q" (*__x) : "memory"); \ 5162306a36Sopenharmony_ci break; \ 5262306a36Sopenharmony_ci case 4: \ 5362306a36Sopenharmony_ci asm volatile(__LOAD_RCPC(, %w0, %1) \ 5462306a36Sopenharmony_ci : "=r" (*(__u32 *)__u.__c) \ 5562306a36Sopenharmony_ci : "Q" (*__x) : "memory"); \ 5662306a36Sopenharmony_ci break; \ 5762306a36Sopenharmony_ci case 8: \ 5862306a36Sopenharmony_ci asm volatile(__LOAD_RCPC(, %0, %1) \ 5962306a36Sopenharmony_ci : "=r" (*(__u64 *)__u.__c) \ 6062306a36Sopenharmony_ci : "Q" (*__x) : "memory"); \ 6162306a36Sopenharmony_ci break; \ 6262306a36Sopenharmony_ci default: \ 6362306a36Sopenharmony_ci atomic = 0; \ 6462306a36Sopenharmony_ci } \ 6562306a36Sopenharmony_ci atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(__x))__x);\ 6662306a36Sopenharmony_ci}) 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci#endif /* !BUILD_VDSO */ 6962306a36Sopenharmony_ci#endif /* CONFIG_LTO && !__ASSEMBLY__ */ 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci#include <asm-generic/rwonce.h> 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci#endif /* __ASM_RWONCE_H */ 74