Path: blob/main/sys/contrib/ck/include/gcc/ppc/ck_pr.h
48525 views
/*1* Copyright 2009-2015 Samy Al Bahra.2* Copyright 2012 João Fernandes.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_PPC_H28#define CK_PR_PPC_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_PR4748/*49* This bounces the hardware thread from low to medium50* priority. I am unsure of the benefits of this approach51* but it is used by the Linux kernel.52*/53CK_CC_INLINE static void54ck_pr_stall(void)55{5657__asm__ __volatile__("or 1, 1, 1;"58"or 2, 2, 2;" ::: "memory");59return;60}6162#define CK_PR_FENCE(T, I) \63CK_CC_INLINE static void \64ck_pr_fence_strict_##T(void) \65{ \66__asm__ __volatile__(I ::: "memory"); \67}6869#ifdef CK_MD_PPC32_LWSYNC70#define CK_PR_LWSYNCOP "lwsync"71#else /* CK_MD_PPC32_LWSYNC_DISABLE */72#define CK_PR_LWSYNCOP "sync"73#endif7475CK_PR_FENCE(atomic, CK_PR_LWSYNCOP)76CK_PR_FENCE(atomic_store, CK_PR_LWSYNCOP)77CK_PR_FENCE(atomic_load, "sync")78CK_PR_FENCE(store_atomic, CK_PR_LWSYNCOP)79CK_PR_FENCE(load_atomic, CK_PR_LWSYNCOP)80CK_PR_FENCE(store, CK_PR_LWSYNCOP)81CK_PR_FENCE(store_load, "sync")82CK_PR_FENCE(load, CK_PR_LWSYNCOP)83CK_PR_FENCE(load_store, CK_PR_LWSYNCOP)84CK_PR_FENCE(memory, "sync")85CK_PR_FENCE(acquire, CK_PR_LWSYNCOP)86CK_PR_FENCE(release, CK_PR_LWSYNCOP)87CK_PR_FENCE(acqrel, CK_PR_LWSYNCOP)88CK_PR_FENCE(lock, CK_PR_LWSYNCOP)89CK_PR_FENCE(unlock, CK_PR_LWSYNCOP)9091#undef CK_PR_LWSYNCOP9293#undef CK_PR_FENCE9495#define CK_PR_LOAD(S, M, T, C, I) \96CK_CC_INLINE static T \97ck_pr_md_load_##S(const M *target) \98{ \99T r; \100__asm__ __volatile__(I "%U1%X1 %0, %1" \101: "=r" (r) \102: "m" (*(const C *)target) \103: "memory"); \104return (r); \105}106107CK_PR_LOAD(ptr, void, void *, uint32_t, "lwz")108109#define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I)110111CK_PR_LOAD_S(32, uint32_t, "lwz")112CK_PR_LOAD_S(16, uint16_t, "lhz")113CK_PR_LOAD_S(8, uint8_t, "lbz")114CK_PR_LOAD_S(uint, unsigned int, "lwz")115CK_PR_LOAD_S(int, int, "lwz")116CK_PR_LOAD_S(short, short, "lhz")117CK_PR_LOAD_S(char, char, "lbz")118119#undef CK_PR_LOAD_S120#undef CK_PR_LOAD121122#define CK_PR_STORE(S, M, T, C, I) \123CK_CC_INLINE static void \124ck_pr_md_store_##S(M *target, T v) \125{ \126__asm__ __volatile__(I "%U0%X0 %1, %0" \127: "=m" (*(C *)target) \128: "r" (v) \129: "memory"); \130return; \131}132133CK_PR_STORE(ptr, void, const void *, uint32_t, "stw")134135#define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I)136137CK_PR_STORE_S(32, uint32_t, "stw")138CK_PR_STORE_S(16, uint16_t, "sth")139CK_PR_STORE_S(8, uint8_t, "stb")140CK_PR_STORE_S(uint, unsigned int, "stw")141CK_PR_STORE_S(int, int, "stw")142CK_PR_STORE_S(short, short, "sth")143CK_PR_STORE_S(char, char, "stb")144145#undef CK_PR_STORE_S146#undef CK_PR_STORE147148#define CK_PR_CAS(N, T, M) \149CK_CC_INLINE static bool \150ck_pr_cas_##N##_value(M *target, T compare, T set, M *value) \151{ \152T previous; \153__asm__ __volatile__("1:" \154"lwarx %0, 0, %1;" \155"cmpw 0, %0, %3;" \156"bne- 2f;" \157"stwcx. %2, 0, %1;" \158"bne- 1b;" \159"2:" \160: "=&r" (previous) \161: "r" (target), \162"r" (set), \163"r" (compare) \164: "memory", "cc"); \165*(T *)value = previous; \166return (previous == compare); \167} \168CK_CC_INLINE static bool \169ck_pr_cas_##N(M *target, T compare, T set) \170{ \171T previous; \172__asm__ __volatile__("1:" \173"lwarx %0, 0, %1;" \174"cmpw 0, %0, %3;" \175"bne- 2f;" \176"stwcx. %2, 0, %1;" \177"bne- 1b;" \178"2:" \179: "=&r" (previous) \180: "r" (target), \181"r" (set), \182"r" (compare) \183: "memory", "cc"); \184return (previous == compare); \185}186187CK_PR_CAS(ptr, void *, void)188#define CK_PR_CAS_S(a, b) CK_PR_CAS(a, b, b)189CK_PR_CAS_S(32, uint32_t)190CK_PR_CAS_S(uint, unsigned int)191CK_PR_CAS_S(int, int)192193#undef CK_PR_CAS_S194#undef CK_PR_CAS195196#define CK_PR_FAS(N, M, T, W) \197CK_CC_INLINE static T \198ck_pr_fas_##N(M *target, T v) \199{ \200T previous; \201__asm__ __volatile__("1:" \202"l" W "arx %0, 0, %1;" \203"st" W "cx. %2, 0, %1;" \204"bne- 1b;" \205: "=&r" (previous) \206: "r" (target), \207"r" (v) \208: "memory", "cc"); \209return (previous); \210}211212CK_PR_FAS(32, uint32_t, uint32_t, "w")213CK_PR_FAS(ptr, void, void *, "w")214CK_PR_FAS(int, int, int, "w")215CK_PR_FAS(uint, unsigned int, unsigned int, "w")216217#undef CK_PR_FAS218219#define CK_PR_UNARY(O, N, M, T, I, W) \220CK_CC_INLINE static void \221ck_pr_##O##_##N(M *target) \222{ \223T previous; \224__asm__ __volatile__("1:" \225"l" W "arx %0, 0, %1;" \226I ";" \227"st" W "cx. %0, 0, %1;" \228"bne- 1b;" \229: "=&r" (previous) \230: "r" (target) \231: "memory", "cc"); \232return; \233}234235CK_PR_UNARY(inc, ptr, void, void *, "addic %0, %0, 1", "w")236CK_PR_UNARY(dec, ptr, void, void *, "addic %0, %0, -1", "w")237CK_PR_UNARY(not, ptr, void, void *, "not %0, %0", "w")238CK_PR_UNARY(neg, ptr, void, void *, "neg %0, %0", "w")239240#define CK_PR_UNARY_S(S, T, W) \241CK_PR_UNARY(inc, S, T, T, "addic %0, %0, 1", W) \242CK_PR_UNARY(dec, S, T, T, "addic %0, %0, -1", W) \243CK_PR_UNARY(not, S, T, T, "not %0, %0", W) \244CK_PR_UNARY(neg, S, T, T, "neg %0, %0", W)245246CK_PR_UNARY_S(32, uint32_t, "w")247CK_PR_UNARY_S(uint, unsigned int, "w")248CK_PR_UNARY_S(int, int, "w")249250#undef CK_PR_UNARY_S251#undef CK_PR_UNARY252253#define CK_PR_BINARY(O, N, M, T, I, W) \254CK_CC_INLINE static void \255ck_pr_##O##_##N(M *target, T delta) \256{ \257T previous; \258__asm__ __volatile__("1:" \259"l" W "arx %0, 0, %1;" \260I " %0, %2, %0;" \261"st" W "cx. %0, 0, %1;" \262"bne- 1b;" \263: "=&r" (previous) \264: "r" (target), \265"r" (delta) \266: "memory", "cc"); \267return; \268}269270CK_PR_BINARY(and, ptr, void, uintptr_t, "and", "w")271CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "w")272CK_PR_BINARY(or, ptr, void, uintptr_t, "or", "w")273CK_PR_BINARY(sub, ptr, void, uintptr_t, "sub", "w")274CK_PR_BINARY(xor, ptr, void, uintptr_t, "xor", "w")275276#define CK_PR_BINARY_S(S, T, W) \277CK_PR_BINARY(and, S, T, T, "and", W) \278CK_PR_BINARY(add, S, T, T, "add", W) \279CK_PR_BINARY(or, S, T, T, "or", W) \280CK_PR_BINARY(sub, S, T, T, "subf", W) \281CK_PR_BINARY(xor, S, T, T, "xor", W)282283CK_PR_BINARY_S(32, uint32_t, "w")284CK_PR_BINARY_S(uint, unsigned int, "w")285CK_PR_BINARY_S(int, int, "w")286287#undef CK_PR_BINARY_S288#undef CK_PR_BINARY289290CK_CC_INLINE static void *291ck_pr_faa_ptr(void *target, uintptr_t delta)292{293uintptr_t previous, r;294295__asm__ __volatile__("1:"296"lwarx %0, 0, %2;"297"add %1, %3, %0;"298"stwcx. %1, 0, %2;"299"bne- 1b;"300: "=&r" (previous),301"=&r" (r)302: "r" (target),303"r" (delta)304: "memory", "cc");305306return (void *)(previous);307}308309#define CK_PR_FAA(S, T, W) \310CK_CC_INLINE static T \311ck_pr_faa_##S(T *target, T delta) \312{ \313T previous, r; \314__asm__ __volatile__("1:" \315"l" W "arx %0, 0, %2;" \316"add %1, %3, %0;" \317"st" W "cx. %1, 0, %2;" \318"bne- 1b;" \319: "=&r" (previous), \320"=&r" (r) \321: "r" (target), \322"r" (delta) \323: "memory", "cc"); \324return (previous); \325}326327CK_PR_FAA(32, uint32_t, "w")328CK_PR_FAA(uint, unsigned int, "w")329CK_PR_FAA(int, int, "w")330331#undef CK_PR_FAA332333#endif /* CK_PR_PPC_H */334335336337