Path: blob/main/sys/contrib/ck/include/gcc/arm/ck_pr.h
48525 views
/*1* Copyright 2009-2015 Samy Al Bahra.2* Copyright 2013-2015 Olivier Houchard.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#ifndef CK_PR_ARM_H28#define CK_PR_ARM_H2930#ifndef CK_PR_H31#error Do not include this file directly, use ck_pr.h32#endif3334#include <ck_cc.h>35#include <ck_md.h>3637/*38* The following represent supported atomic operations.39* These operations may be emulated.40*/41#include "ck_f_pr.h"4243/*44* Minimum interface requirement met.45*/46#define CK_F_PR4748CK_CC_INLINE static void49ck_pr_stall(void)50{5152__asm__ __volatile__("" ::: "memory");53return;54}5556#if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__)57#define CK_ISB __asm __volatile("isb" : : "r" (0) : "memory")58#define CK_DMB __asm __volatile("dmb" : : "r" (0) : "memory")59#define CK_DSB __asm __volatile("dsb" : : "r" (0) : "memory")60/* FreeBSD's toolchain doesn't accept dmb st, so use the opcode instead */61#ifdef __FreeBSD__62#define CK_DMB_ST __asm __volatile(".word 0xf57ff05e" : : "r" (0) : "memory")63#else64#define CK_DMB_ST __asm __volatile("dmb st" : : "r" (0) : "memory")65#endif /* __FreeBSD__ */66#else67/* armv6 doesn't have dsb/dmb/isb, and no way to wait only for stores */68#define CK_ISB \69__asm __volatile("mcr p15, 0, %0, c7, c5, 4" : : "r" (0) : "memory")70#define CK_DSB \71__asm __volatile("mcr p15, 0, %0, c7, c10, 4" : : "r" (0) : "memory")72#define CK_DMB \73__asm __volatile("mcr p15, 0, %0, c7, c10, 5" : : "r" (0) : "memory")74#define CK_DMB_ST CK_DMB75#endif7677#define CK_PR_FENCE(T, I) \78CK_CC_INLINE static void \79ck_pr_fence_strict_##T(void) \80{ \81I; \82}8384CK_PR_FENCE(atomic, CK_DMB_ST)85CK_PR_FENCE(atomic_store, CK_DMB_ST)86CK_PR_FENCE(atomic_load, CK_DMB_ST)87CK_PR_FENCE(store_atomic, CK_DMB_ST)88CK_PR_FENCE(load_atomic, CK_DMB)89CK_PR_FENCE(store, CK_DMB_ST)90CK_PR_FENCE(store_load, CK_DMB)91CK_PR_FENCE(load, CK_DMB)92CK_PR_FENCE(load_store, CK_DMB)93CK_PR_FENCE(memory, CK_DMB)94CK_PR_FENCE(acquire, CK_DMB)95CK_PR_FENCE(release, CK_DMB)96CK_PR_FENCE(acqrel, CK_DMB)97CK_PR_FENCE(lock, CK_DMB)98CK_PR_FENCE(unlock, CK_DMB)99100#undef CK_PR_FENCE101102#undef CK_ISB103#undef CK_DSB104#undef CK_DMB105#undef CK_DMB_ST106107#define CK_PR_LOAD(S, M, T, C, I) \108CK_CC_INLINE static T \109ck_pr_md_load_##S(const M *target) \110{ \111long r = 0; \112__asm__ __volatile__(I " %0, [%1];" \113: "=r" (r) \114: "r" (target) \115: "memory"); \116return ((T)r); \117}118119CK_PR_LOAD(ptr, void, void *, uint32_t, "ldr")120121#define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I)122123CK_PR_LOAD_S(32, uint32_t, "ldr")124CK_PR_LOAD_S(16, uint16_t, "ldrh")125CK_PR_LOAD_S(8, uint8_t, "ldrb")126CK_PR_LOAD_S(uint, unsigned int, "ldr")127CK_PR_LOAD_S(int, int, "ldr")128CK_PR_LOAD_S(short, short, "ldrh")129CK_PR_LOAD_S(char, char, "ldrb")130131#undef CK_PR_LOAD_S132#undef CK_PR_LOAD133134#if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__)135136#define CK_PR_DOUBLE_LOAD(T, N) \137CK_CC_INLINE static T \138ck_pr_md_load_##N(const T *target) \139{ \140register T ret; \141\142__asm __volatile("ldrexd %0, [%1]" \143: "=&r" (ret) \144: "r" (target) \145: "memory", "cc"); \146return (ret); \147}148149CK_PR_DOUBLE_LOAD(uint64_t, 64)150#ifndef CK_PR_DISABLE_DOUBLE151CK_PR_DOUBLE_LOAD(double, double)152#endif153#undef CK_PR_DOUBLE_LOAD154#endif155156#define CK_PR_STORE(S, M, T, C, I) \157CK_CC_INLINE static void \158ck_pr_md_store_##S(M *target, T v) \159{ \160__asm__ __volatile__(I " %1, [%0]" \161: \162: "r" (target), \163"r" (v) \164: "memory"); \165return; \166}167168CK_PR_STORE(ptr, void, const void *, uint32_t, "str")169170#define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I)171172CK_PR_STORE_S(32, uint32_t, "str")173CK_PR_STORE_S(16, uint16_t, "strh")174CK_PR_STORE_S(8, uint8_t, "strb")175CK_PR_STORE_S(uint, unsigned int, "str")176CK_PR_STORE_S(int, int, "str")177CK_PR_STORE_S(short, short, "strh")178CK_PR_STORE_S(char, char, "strb")179180#undef CK_PR_STORE_S181#undef CK_PR_STORE182183#if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__)184185#define CK_PR_DOUBLE_STORE(T, N) \186CK_CC_INLINE static void \187ck_pr_md_store_##N(const T *target, T value) \188{ \189T tmp; \190uint32_t flag; \191__asm __volatile("1: \n" \192"ldrexd %0, [%2]\n" \193"strexd %1, %3, [%2]\n" \194"teq %1, #0\n" \195"it ne \n" \196"bne 1b\n" \197: "=&r" (tmp), "=&r" (flag) \198: "r" (target), "r" (value) \199: "memory", "cc"); \200}201202CK_PR_DOUBLE_STORE(uint64_t, 64)203#ifndef CK_PR_DISABLE_DOUBLE204CK_PR_DOUBLE_STORE(double, double)205#endif206207#undef CK_PR_DOUBLE_STORE208209#define CK_PR_DOUBLE_CAS_VALUE(T, N) \210CK_CC_INLINE static bool \211ck_pr_cas_##N##_value(T *target, T compare, T set, T *value) \212{ \213T previous; \214int tmp; \215\216__asm__ __volatile__("1:" \217"ldrexd %0, [%4];" \218"cmp %Q0, %Q2;" \219"ittt eq;" \220"cmpeq %R0, %R2;" \221"strexdeq %1, %3, [%4];" \222"cmpeq %1, #1;" \223"beq 1b;" \224:"=&r" (previous), "=&r" (tmp) \225: "r" (compare), "r" (set) , \226"r"(target) \227: "memory", "cc"); \228*value = previous; \229return (*value == compare); \230}231232CK_PR_DOUBLE_CAS_VALUE(uint64_t, 64)233#ifndef CK_PR_DISABLE_DOUBLE234CK_PR_DOUBLE_CAS_VALUE(double, double)235#endif236237#undef CK_PR_DOUBLE_CAS_VALUE238239CK_CC_INLINE static bool240ck_pr_cas_ptr_2_value(void *target, void *compare, void *set, void *value)241{242uint32_t *_compare = CK_CPP_CAST(uint32_t *, compare);243uint32_t *_set = CK_CPP_CAST(uint32_t *, set);244uint64_t __compare = ((uint64_t)_compare[0]) | ((uint64_t)_compare[1] << 32);245uint64_t __set = ((uint64_t)_set[0]) | ((uint64_t)_set[1] << 32);246247return (ck_pr_cas_64_value(CK_CPP_CAST(uint64_t *, target),248__compare,249__set,250CK_CPP_CAST(uint64_t *, value)));251}252253#define CK_PR_DOUBLE_CAS(T, N) \254CK_CC_INLINE static bool \255ck_pr_cas_##N(T *target, T compare, T set) \256{ \257int ret; \258T tmp; \259\260__asm__ __volatile__("1:" \261"mov %0, #0;" \262"ldrexd %1, [%4];" \263"cmp %Q1, %Q2;" \264"itttt eq;" \265"cmpeq %R1, %R2;" \266"strexdeq %1, %3, [%4];" \267"moveq %0, #1;" \268"cmpeq %1, #1;" \269"beq 1b;" \270: "=&r" (ret), "=&r" (tmp) \271: "r" (compare), "r" (set) , \272"r"(target) \273: "memory", "cc"); \274\275return (ret); \276}277278CK_PR_DOUBLE_CAS(uint64_t, 64)279#ifndef CK_PR_DISABLE_DOUBLE280CK_PR_DOUBLE_CAS(double, double)281#endif282283CK_CC_INLINE static bool284ck_pr_cas_ptr_2(void *target, void *compare, void *set)285{286uint32_t *_compare = CK_CPP_CAST(uint32_t *, compare);287uint32_t *_set = CK_CPP_CAST(uint32_t *, set);288uint64_t __compare = ((uint64_t)_compare[0]) | ((uint64_t)_compare[1] << 32);289uint64_t __set = ((uint64_t)_set[0]) | ((uint64_t)_set[1] << 32);290return (ck_pr_cas_64(CK_CPP_CAST(uint64_t *, target),291__compare,292__set));293}294295#endif296297CK_CC_INLINE static bool298ck_pr_cas_ptr_value(void *target, void *compare, void *set, void *value)299{300void *previous, *tmp;301__asm__ __volatile__("1:"302"ldrex %0, [%2];"303"cmp %0, %4;"304"itt eq;"305"strexeq %1, %3, [%2];"306"cmpeq %1, #1;"307"beq 1b;"308: "=&r" (previous),309"=&r" (tmp)310: "r" (target),311"r" (set),312"r" (compare)313: "memory", "cc");314*(void **)value = previous;315return (previous == compare);316}317318CK_CC_INLINE static bool319ck_pr_cas_ptr(void *target, void *compare, void *set)320{321void *previous, *tmp;322__asm__ __volatile__("1:"323"ldrex %0, [%2];"324"cmp %0, %4;"325"itt eq;"326"strexeq %1, %3, [%2];"327"cmpeq %1, #1;"328"beq 1b;"329: "=&r" (previous),330"=&r" (tmp)331: "r" (target),332"r" (set),333"r" (compare)334: "memory", "cc");335return (previous == compare);336}337338#define CK_PR_CAS(N, T, W) \339CK_CC_INLINE static bool \340ck_pr_cas_##N##_value(T *target, T compare, T set, T *value) \341{ \342T previous = 0, tmp = 0; \343__asm__ __volatile__("1:" \344"ldrex" W " %0, [%2];" \345"cmp %0, %4;" \346"itt eq;" \347"strex" W "eq %1, %3, [%2];" \348"cmpeq %1, #1;" \349"beq 1b;" \350/* \351* Using "+&" instead of "=&" to avoid bogus \352* clang warnings. \353*/ \354: "+&r" (previous), \355"+&r" (tmp) \356: "r" (target), \357"r" (set), \358"r" (compare) \359: "memory", "cc"); \360*value = previous; \361return (previous == compare); \362} \363CK_CC_INLINE static bool \364ck_pr_cas_##N(T *target, T compare, T set) \365{ \366T previous = 0, tmp = 0; \367__asm__ __volatile__("1:" \368"ldrex" W " %0, [%2];" \369"cmp %0, %4;" \370"itt eq;" \371"strex" W "eq %1, %3, [%2];" \372"cmpeq %1, #1;" \373"beq 1b;" \374: "+&r" (previous), \375"+&r" (tmp) \376: "r" (target), \377"r" (set), \378"r" (compare) \379: "memory", "cc"); \380return (previous == compare); \381}382383CK_PR_CAS(32, uint32_t, "")384CK_PR_CAS(uint, unsigned int, "")385CK_PR_CAS(int, int, "")386CK_PR_CAS(16, uint16_t, "h")387CK_PR_CAS(8, uint8_t, "b")388CK_PR_CAS(short, short, "h")389CK_PR_CAS(char, char, "b")390391392#undef CK_PR_CAS393394#define CK_PR_FAS(N, M, T, W) \395CK_CC_INLINE static T \396ck_pr_fas_##N(M *target, T v) \397{ \398T previous = 0; \399T tmp = 0; \400__asm__ __volatile__("1:" \401"ldrex" W " %0, [%2];" \402"strex" W " %1, %3, [%2];" \403"cmp %1, #0;" \404"bne 1b;" \405: "+&r" (previous), \406"+&r" (tmp) \407: "r" (target), \408"r" (v) \409: "memory", "cc"); \410return (previous); \411}412413CK_PR_FAS(32, uint32_t, uint32_t, "")414CK_PR_FAS(ptr, void, void *, "")415CK_PR_FAS(int, int, int, "")416CK_PR_FAS(uint, unsigned int, unsigned int, "")417CK_PR_FAS(16, uint16_t, uint16_t, "h")418CK_PR_FAS(8, uint8_t, uint8_t, "b")419CK_PR_FAS(short, short, short, "h")420CK_PR_FAS(char, char, char, "b")421422423#undef CK_PR_FAS424425#define CK_PR_UNARY(O, N, M, T, I, W) \426CK_CC_INLINE static void \427ck_pr_##O##_##N(M *target) \428{ \429T previous = 0; \430T tmp = 0; \431__asm__ __volatile__("1:" \432"ldrex" W " %0, [%2];" \433I ";" \434"strex" W " %1, %0, [%2];" \435"cmp %1, #0;" \436"bne 1b;" \437: "+&r" (previous), \438"+&r" (tmp) \439: "r" (target) \440: "memory", "cc"); \441return; \442}443444CK_PR_UNARY(inc, ptr, void, void *, "add %0, %0, #1", "")445CK_PR_UNARY(dec, ptr, void, void *, "sub %0, %0, #1", "")446CK_PR_UNARY(not, ptr, void, void *, "mvn %0, %0", "")447CK_PR_UNARY(neg, ptr, void, void *, "neg %0, %0", "")448449#define CK_PR_UNARY_S(S, T, W) \450CK_PR_UNARY(inc, S, T, T, "add %0, %0, #1", W) \451CK_PR_UNARY(dec, S, T, T, "sub %0, %0, #1", W) \452CK_PR_UNARY(not, S, T, T, "mvn %0, %0", W) \453CK_PR_UNARY(neg, S, T, T, "neg %0, %0", W) \454455CK_PR_UNARY_S(32, uint32_t, "")456CK_PR_UNARY_S(uint, unsigned int, "")457CK_PR_UNARY_S(int, int, "")458CK_PR_UNARY_S(16, uint16_t, "h")459CK_PR_UNARY_S(8, uint8_t, "b")460CK_PR_UNARY_S(short, short, "h")461CK_PR_UNARY_S(char, char, "b")462463#undef CK_PR_UNARY_S464#undef CK_PR_UNARY465466#define CK_PR_BINARY(O, N, M, T, I, W) \467CK_CC_INLINE static void \468ck_pr_##O##_##N(M *target, T delta) \469{ \470T previous = 0; \471T tmp = 0; \472__asm__ __volatile__("1:" \473"ldrex" W " %0, [%2];" \474I " %0, %0, %3;" \475"strex" W " %1, %0, [%2];" \476"cmp %1, #0;" \477"bne 1b;" \478: "+&r" (previous), \479"+&r" (tmp) \480: "r" (target), \481"r" (delta) \482: "memory", "cc"); \483return; \484}485486CK_PR_BINARY(and, ptr, void, uintptr_t, "and", "")487CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "")488CK_PR_BINARY(or, ptr, void, uintptr_t, "orr", "")489CK_PR_BINARY(sub, ptr, void, uintptr_t, "sub", "")490CK_PR_BINARY(xor, ptr, void, uintptr_t, "eor", "")491492#define CK_PR_BINARY_S(S, T, W) \493CK_PR_BINARY(and, S, T, T, "and", W) \494CK_PR_BINARY(add, S, T, T, "add", W) \495CK_PR_BINARY(or, S, T, T, "orr", W) \496CK_PR_BINARY(sub, S, T, T, "sub", W) \497CK_PR_BINARY(xor, S, T, T, "eor", W)498499CK_PR_BINARY_S(32, uint32_t, "")500CK_PR_BINARY_S(uint, unsigned int, "")501CK_PR_BINARY_S(int, int, "")502CK_PR_BINARY_S(16, uint16_t, "h")503CK_PR_BINARY_S(8, uint8_t, "b")504CK_PR_BINARY_S(short, short, "h")505CK_PR_BINARY_S(char, char, "b")506507#undef CK_PR_BINARY_S508#undef CK_PR_BINARY509510CK_CC_INLINE static void *511ck_pr_faa_ptr(void *target, uintptr_t delta)512{513uintptr_t previous, r, tmp;514515__asm__ __volatile__("1:"516"ldrex %0, [%3];"517"add %1, %4, %0;"518"strex %2, %1, [%3];"519"cmp %2, #0;"520"bne 1b;"521: "=&r" (previous),522"=&r" (r),523"=&r" (tmp)524: "r" (target),525"r" (delta)526: "memory", "cc");527528return (void *)(previous);529}530531#define CK_PR_FAA(S, T, W) \532CK_CC_INLINE static T \533ck_pr_faa_##S(T *target, T delta) \534{ \535T previous = 0, r = 0, tmp = 0; \536__asm__ __volatile__("1:" \537"ldrex" W " %0, [%3];" \538"add %1, %4, %0;" \539"strex" W " %2, %1, [%3];" \540"cmp %2, #0;" \541"bne 1b;" \542: "+&r" (previous), \543"+&r" (r), \544"+&r" (tmp) \545: "r" (target), \546"r" (delta) \547: "memory", "cc"); \548return (previous); \549}550551CK_PR_FAA(32, uint32_t, "")552CK_PR_FAA(uint, unsigned int, "")553CK_PR_FAA(int, int, "")554CK_PR_FAA(16, uint16_t, "h")555CK_PR_FAA(8, uint8_t, "b")556CK_PR_FAA(short, short, "h")557CK_PR_FAA(char, char, "b")558559#undef CK_PR_FAA560561#endif /* CK_PR_ARM_H */562563564565