Path: blob/main/sys/contrib/ck/include/gcc/ppc64/ck_pr.h
48420 views
/*1* Copyright 2009-2015 Samy Al Bahra.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#ifndef CK_PR_PPC64_H27#define CK_PR_PPC64_H2829#ifndef CK_PR_H30#error Do not include this file directly, use ck_pr.h31#endif3233#include <ck_cc.h>34#include <ck_md.h>3536/*37* The following represent supported atomic operations.38* These operations may be emulated.39*/40#include "ck_f_pr.h"4142/*43* Minimum interface requirement met.44*/45#define CK_F_PR4647/*48* This bounces the hardware thread from low to medium49* priority. I am unsure of the benefits of this approach50* but it is used by the Linux kernel.51*/52CK_CC_INLINE static void53ck_pr_stall(void)54{5556__asm__ __volatile__("or 1, 1, 1;"57"or 2, 2, 2;" ::: "memory");58return;59}6061#define CK_PR_FENCE(T, I) \62CK_CC_INLINE static void \63ck_pr_fence_strict_##T(void) \64{ \65__asm__ __volatile__(I ::: "memory"); \66}6768/*69* These are derived from:70* http://www.ibm.com/developerworks/systems/articles/powerpc.html71*/72CK_PR_FENCE(atomic, "lwsync")73CK_PR_FENCE(atomic_store, "lwsync")74CK_PR_FENCE(atomic_load, "sync")75CK_PR_FENCE(store_atomic, "lwsync")76CK_PR_FENCE(load_atomic, "lwsync")77CK_PR_FENCE(store, "lwsync")78CK_PR_FENCE(store_load, "sync")79CK_PR_FENCE(load, "lwsync")80CK_PR_FENCE(load_store, "lwsync")81CK_PR_FENCE(memory, "sync")82CK_PR_FENCE(acquire, "lwsync")83CK_PR_FENCE(release, "lwsync")84CK_PR_FENCE(acqrel, "lwsync")85CK_PR_FENCE(lock, "lwsync")86CK_PR_FENCE(unlock, "lwsync")8788#undef CK_PR_FENCE8990#define CK_PR_LOAD(S, M, T, C, I) \91CK_CC_INLINE static T \92ck_pr_md_load_##S(const M *target) \93{ \94T r; \95__asm__ __volatile__(I "%U1%X1 %0, %1" \96: "=r" (r) \97: "m" (*(const C *)target) \98: "memory"); \99return (r); \100}101102CK_PR_LOAD(ptr, void, void *, uint64_t, "ld")103104#define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I)105106CK_PR_LOAD_S(64, uint64_t, "ld")107CK_PR_LOAD_S(32, uint32_t, "lwz")108CK_PR_LOAD_S(16, uint16_t, "lhz")109CK_PR_LOAD_S(8, uint8_t, "lbz")110CK_PR_LOAD_S(uint, unsigned int, "lwz")111CK_PR_LOAD_S(int, int, "lwz")112CK_PR_LOAD_S(short, short, "lhz")113CK_PR_LOAD_S(char, char, "lbz")114#ifndef CK_PR_DISABLE_DOUBLE115CK_PR_LOAD_S(double, double, "ld")116#endif117118#undef CK_PR_LOAD_S119#undef CK_PR_LOAD120121#define CK_PR_STORE(S, M, T, C, I) \122CK_CC_INLINE static void \123ck_pr_md_store_##S(M *target, T v) \124{ \125__asm__ __volatile__(I "%U0%X0 %1, %0" \126: "=m" (*(C *)target) \127: "r" (v) \128: "memory"); \129return; \130}131132CK_PR_STORE(ptr, void, const void *, uint64_t, "std")133134#define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I)135136CK_PR_STORE_S(64, uint64_t, "std")137CK_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")144#ifndef CK_PR_DISABLE_DOUBLE145CK_PR_STORE_S(double, double, "std")146#endif147148#undef CK_PR_STORE_S149#undef CK_PR_STORE150151CK_CC_INLINE static bool152ck_pr_cas_64_value(uint64_t *target, uint64_t compare, uint64_t set, uint64_t *value)153{154uint64_t previous;155156__asm__ __volatile__("1:"157"ldarx %0, 0, %1;"158"cmpd 0, %0, %3;"159"bne- 2f;"160"stdcx. %2, 0, %1;"161"bne- 1b;"162"2:"163: "=&r" (previous)164: "r" (target),165"r" (set),166"r" (compare)167: "memory", "cc");168169*value = previous;170return (previous == compare);171}172173CK_CC_INLINE static bool174ck_pr_cas_ptr_value(void *target, void *compare, void *set, void *value)175{176void *previous;177178__asm__ __volatile__("1:"179"ldarx %0, 0, %1;"180"cmpd 0, %0, %3;"181"bne- 2f;"182"stdcx. %2, 0, %1;"183"bne- 1b;"184"2:"185: "=&r" (previous)186: "r" (target),187"r" (set),188"r" (compare)189: "memory", "cc");190191ck_pr_md_store_ptr(value, previous);192return (previous == compare);193}194195CK_CC_INLINE static bool196ck_pr_cas_64(uint64_t *target, uint64_t compare, uint64_t set)197{198uint64_t previous;199200__asm__ __volatile__("1:"201"ldarx %0, 0, %1;"202"cmpd 0, %0, %3;"203"bne- 2f;"204"stdcx. %2, 0, %1;"205"bne- 1b;"206"2:"207: "=&r" (previous)208: "r" (target),209"r" (set),210"r" (compare)211: "memory", "cc");212213return (previous == compare);214}215216CK_CC_INLINE static bool217ck_pr_cas_ptr(void *target, void *compare, void *set)218{219void *previous;220221__asm__ __volatile__("1:"222"ldarx %0, 0, %1;"223"cmpd 0, %0, %3;"224"bne- 2f;"225"stdcx. %2, 0, %1;"226"bne- 1b;"227"2:"228: "=&r" (previous)229: "r" (target),230"r" (set),231"r" (compare)232: "memory", "cc");233234return (previous == compare);235}236237#define CK_PR_CAS(N, T) \238CK_CC_INLINE static bool \239ck_pr_cas_##N##_value(T *target, T compare, T set, T *value) \240{ \241T previous; \242__asm__ __volatile__("1:" \243"lwarx %0, 0, %1;" \244"cmpw 0, %0, %3;" \245"bne- 2f;" \246"stwcx. %2, 0, %1;" \247"bne- 1b;" \248"2:" \249: "=&r" (previous) \250: "r" (target), \251"r" (set), \252"r" (compare) \253: "memory", "cc"); \254*value = previous; \255return (previous == compare); \256} \257CK_CC_INLINE static bool \258ck_pr_cas_##N(T *target, T compare, T set) \259{ \260T previous; \261__asm__ __volatile__("1:" \262"lwarx %0, 0, %1;" \263"cmpw 0, %0, %3;" \264"bne- 2f;" \265"stwcx. %2, 0, %1;" \266"bne- 1b;" \267"2:" \268: "=&r" (previous) \269: "r" (target), \270"r" (set), \271"r" (compare) \272: "memory", "cc"); \273return (previous == compare); \274}275276CK_PR_CAS(32, uint32_t)277CK_PR_CAS(uint, unsigned int)278CK_PR_CAS(int, int)279280#undef CK_PR_CAS281282#define CK_PR_FAS(N, M, T, W) \283CK_CC_INLINE static T \284ck_pr_fas_##N(M *target, T v) \285{ \286T previous; \287__asm__ __volatile__("1:" \288"l" W "arx %0, 0, %1;" \289"st" W "cx. %2, 0, %1;" \290"bne- 1b;" \291: "=&r" (previous) \292: "r" (target), \293"r" (v) \294: "memory", "cc"); \295return (previous); \296}297298CK_PR_FAS(64, uint64_t, uint64_t, "d")299CK_PR_FAS(32, uint32_t, uint32_t, "w")300#ifndef CK_PR_DISABLE_DOUBLE301CK_PR_FAS(double, double, double, "d")302#endif303CK_PR_FAS(ptr, void, void *, "d")304CK_PR_FAS(int, int, int, "w")305CK_PR_FAS(uint, unsigned int, unsigned int, "w")306307#undef CK_PR_FAS308309#define CK_PR_UNARY(O, N, M, T, I, W) \310CK_CC_INLINE static void \311ck_pr_##O##_##N(M *target) \312{ \313T previous; \314__asm__ __volatile__("1:" \315"l" W "arx %0, 0, %1;" \316I ";" \317"st" W "cx. %0, 0, %1;" \318"bne- 1b;" \319: "=&r" (previous) \320: "r" (target) \321: "memory", "cc"); \322return; \323}324325CK_PR_UNARY(inc, ptr, void, void *, "addic %0, %0, 1", "d")326CK_PR_UNARY(dec, ptr, void, void *, "addic %0, %0, -1", "d")327CK_PR_UNARY(not, ptr, void, void *, "not %0, %0", "d")328CK_PR_UNARY(neg, ptr, void, void *, "neg %0, %0", "d")329330#define CK_PR_UNARY_S(S, T, W) \331CK_PR_UNARY(inc, S, T, T, "addic %0, %0, 1", W) \332CK_PR_UNARY(dec, S, T, T, "addic %0, %0, -1", W) \333CK_PR_UNARY(not, S, T, T, "not %0, %0", W) \334CK_PR_UNARY(neg, S, T, T, "neg %0, %0", W)335336CK_PR_UNARY_S(64, uint64_t, "d")337CK_PR_UNARY_S(32, uint32_t, "w")338CK_PR_UNARY_S(uint, unsigned int, "w")339CK_PR_UNARY_S(int, int, "w")340341#undef CK_PR_UNARY_S342#undef CK_PR_UNARY343344#define CK_PR_BINARY(O, N, M, T, I, W) \345CK_CC_INLINE static void \346ck_pr_##O##_##N(M *target, T delta) \347{ \348T previous; \349__asm__ __volatile__("1:" \350"l" W "arx %0, 0, %1;" \351I " %0, %2, %0;" \352"st" W "cx. %0, 0, %1;" \353"bne- 1b;" \354: "=&r" (previous) \355: "r" (target), \356"r" (delta) \357: "memory", "cc"); \358return; \359}360361CK_PR_BINARY(and, ptr, void, uintptr_t, "and", "d")362CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "d")363CK_PR_BINARY(or, ptr, void, uintptr_t, "or", "d")364CK_PR_BINARY(sub, ptr, void, uintptr_t, "sub", "d")365CK_PR_BINARY(xor, ptr, void, uintptr_t, "xor", "d")366367#define CK_PR_BINARY_S(S, T, W) \368CK_PR_BINARY(and, S, T, T, "and", W) \369CK_PR_BINARY(add, S, T, T, "add", W) \370CK_PR_BINARY(or, S, T, T, "or", W) \371CK_PR_BINARY(sub, S, T, T, "subf", W) \372CK_PR_BINARY(xor, S, T, T, "xor", W)373374CK_PR_BINARY_S(64, uint64_t, "d")375CK_PR_BINARY_S(32, uint32_t, "w")376CK_PR_BINARY_S(uint, unsigned int, "w")377CK_PR_BINARY_S(int, int, "w")378379#undef CK_PR_BINARY_S380#undef CK_PR_BINARY381382CK_CC_INLINE static void *383ck_pr_faa_ptr(void *target, uintptr_t delta)384{385uintptr_t previous, r;386387__asm__ __volatile__("1:"388"ldarx %0, 0, %2;"389"add %1, %3, %0;"390"stdcx. %1, 0, %2;"391"bne- 1b;"392: "=&r" (previous),393"=&r" (r)394: "r" (target),395"r" (delta)396: "memory", "cc");397398return (void *)(previous);399}400401#define CK_PR_FAA(S, T, W) \402CK_CC_INLINE static T \403ck_pr_faa_##S(T *target, T delta) \404{ \405T previous, r; \406__asm__ __volatile__("1:" \407"l" W "arx %0, 0, %2;" \408"add %1, %3, %0;" \409"st" W "cx. %1, 0, %2;" \410"bne- 1b;" \411: "=&r" (previous), \412"=&r" (r) \413: "r" (target), \414"r" (delta) \415: "memory", "cc"); \416return (previous); \417}418419CK_PR_FAA(64, uint64_t, "d")420CK_PR_FAA(32, uint32_t, "w")421CK_PR_FAA(uint, unsigned int, "w")422CK_PR_FAA(int, int, "w")423424#undef CK_PR_FAA425426#endif /* CK_PR_PPC64_H */427428429