1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright 2015, Michael Ellerman, IBM Corp.
4  */
5 
6 #ifndef _SELFTESTS_POWERPC_TM_TM_H
7 #define _SELFTESTS_POWERPC_TM_TM_H
8 
9 #include <stdbool.h>
10 #include <asm/tm.h>
11 
12 #include "utils.h"
13 
14 static inline bool have_htm(void)
15 {
16 #ifdef PPC_FEATURE2_HTM
17 	return have_hwcap2(PPC_FEATURE2_HTM);
18 #else
19 	printf("PPC_FEATURE2_HTM not defined, can't check AT_HWCAP2\n");
20 	return false;
21 #endif
22 }
23 
24 static inline bool have_htm_nosc(void)
25 {
26 #ifdef PPC_FEATURE2_HTM_NOSC
27 	return have_hwcap2(PPC_FEATURE2_HTM_NOSC);
28 #else
29 	printf("PPC_FEATURE2_HTM_NOSC not defined, can't check AT_HWCAP2\n");
30 	return false;
31 #endif
32 }
33 
34 static inline long failure_code(void)
35 {
36 	return __builtin_get_texasru() >> 24;
37 }
38 
39 static inline bool failure_is_persistent(void)
40 {
41 	return (failure_code() & TM_CAUSE_PERSISTENT) == TM_CAUSE_PERSISTENT;
42 }
43 
44 static inline bool failure_is_syscall(void)
45 {
46 	return (failure_code() & TM_CAUSE_SYSCALL) == TM_CAUSE_SYSCALL;
47 }
48 
49 static inline bool failure_is_unavailable(void)
50 {
51 	return (failure_code() & TM_CAUSE_FAC_UNAV) == TM_CAUSE_FAC_UNAV;
52 }
53 
54 static inline bool failure_is_reschedule(void)
55 {
56 	if ((failure_code() & TM_CAUSE_RESCHED) == TM_CAUSE_RESCHED ||
57 	    (failure_code() & TM_CAUSE_KVM_RESCHED) == TM_CAUSE_KVM_RESCHED ||
58 	    (failure_code() & TM_CAUSE_KVM_FAC_UNAV) == TM_CAUSE_KVM_FAC_UNAV)
59 		return true;
60 
61 	return false;
62 }
63 
64 static inline bool failure_is_nesting(void)
65 {
66 	return (__builtin_get_texasru() & 0x400000);
67 }
68 
69 static inline int tcheck(void)
70 {
71 	long cr;
72 	asm volatile ("tcheck 0" : "=r"(cr) : : "cr0");
73 	return (cr >> 28) & 4;
74 }
75 
76 static inline bool tcheck_doomed(void)
77 {
78 	return tcheck() & 8;
79 }
80 
81 static inline bool tcheck_active(void)
82 {
83 	return tcheck() & 4;
84 }
85 
86 static inline bool tcheck_suspended(void)
87 {
88 	return tcheck() & 2;
89 }
90 
91 static inline bool tcheck_transactional(void)
92 {
93 	return tcheck() & 6;
94 }
95 
96 #endif /* _SELFTESTS_POWERPC_TM_TM_H */
97