1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright 2013, Michael Ellerman, IBM Corp.
4  */
5 
6 #ifndef _SELFTESTS_POWERPC_UTILS_H
7 #define _SELFTESTS_POWERPC_UTILS_H
8 
9 #define __cacheline_aligned __attribute__((aligned(128)))
10 
11 #include <stdint.h>
12 #include <stdbool.h>
13 #include <linux/auxvec.h>
14 #include <linux/perf_event.h>
15 #include <asm/cputable.h>
16 #include "reg.h"
17 
18 /* Avoid headaches with PRI?64 - just use %ll? always */
19 typedef unsigned long long u64;
20 typedef   signed long long s64;
21 
22 /* Just for familiarity */
23 typedef uint32_t u32;
24 typedef uint16_t u16;
25 typedef uint8_t u8;
26 
27 void test_harness_set_timeout(uint64_t time);
28 int test_harness(int (test_function)(void), char *name);
29 
30 int read_auxv(char *buf, ssize_t buf_size);
31 void *find_auxv_entry(int type, char *auxv);
32 void *get_auxv_entry(int type);
33 
34 int pick_online_cpu(void);
35 
36 int read_debugfs_file(char *debugfs_file, int *result);
37 int write_debugfs_file(char *debugfs_file, int result);
38 int read_sysfs_file(char *debugfs_file, char *result, size_t result_size);
39 int perf_event_open_counter(unsigned int type,
40 			    unsigned long config, int group_fd);
41 int perf_event_enable(int fd);
42 int perf_event_disable(int fd);
43 int perf_event_reset(int fd);
44 
45 struct perf_event_read {
46 	__u64 nr;
47 	__u64 l1d_misses;
48 };
49 
50 #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 30)
51 #include <unistd.h>
52 #include <sys/syscall.h>
53 
54 static inline pid_t gettid(void)
55 {
56 	return syscall(SYS_gettid);
57 }
58 #endif
59 
60 static inline bool have_hwcap(unsigned long ftr)
61 {
62 	return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
63 }
64 
65 #ifdef AT_HWCAP2
66 static inline bool have_hwcap2(unsigned long ftr2)
67 {
68 	return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
69 }
70 #else
71 static inline bool have_hwcap2(unsigned long ftr2)
72 {
73 	return false;
74 }
75 #endif
76 
77 static inline char *auxv_base_platform(void)
78 {
79 	return ((char *)get_auxv_entry(AT_BASE_PLATFORM));
80 }
81 
82 static inline char *auxv_platform(void)
83 {
84 	return ((char *)get_auxv_entry(AT_PLATFORM));
85 }
86 
87 bool is_ppc64le(void);
88 int using_hash_mmu(bool *using_hash);
89 
90 /* Yes, this is evil */
91 #define FAIL_IF(x)						\
92 do {								\
93 	if ((x)) {						\
94 		fprintf(stderr,					\
95 		"[FAIL] Test FAILED on line %d\n", __LINE__);	\
96 		return 1;					\
97 	}							\
98 } while (0)
99 
100 #define FAIL_IF_EXIT(x)						\
101 do {								\
102 	if ((x)) {						\
103 		fprintf(stderr,					\
104 		"[FAIL] Test FAILED on line %d\n", __LINE__);	\
105 		_exit(1);					\
106 	}							\
107 } while (0)
108 
109 /* The test harness uses this, yes it's gross */
110 #define MAGIC_SKIP_RETURN_VALUE	99
111 
112 #define SKIP_IF(x)						\
113 do {								\
114 	if ((x)) {						\
115 		fprintf(stderr,					\
116 		"[SKIP] Test skipped on line %d\n", __LINE__);	\
117 		return MAGIC_SKIP_RETURN_VALUE;			\
118 	}							\
119 } while (0)
120 
121 #define SKIP_IF_MSG(x, msg)					\
122 do {								\
123 	if ((x)) {						\
124 		fprintf(stderr,					\
125 		"[SKIP] Test skipped on line %d: %s\n",		\
126 		 __LINE__, msg);				\
127 		return MAGIC_SKIP_RETURN_VALUE;			\
128 	}							\
129 } while (0)
130 
131 #define _str(s) #s
132 #define str(s) _str(s)
133 
134 #define sigsafe_err(msg)	({ \
135 		ssize_t nbytes __attribute__((unused)); \
136 		nbytes = write(STDERR_FILENO, msg, strlen(msg)); })
137 
138 /* POWER9 feature */
139 #ifndef PPC_FEATURE2_ARCH_3_00
140 #define PPC_FEATURE2_ARCH_3_00 0x00800000
141 #endif
142 
143 /* POWER10 feature */
144 #ifndef PPC_FEATURE2_ARCH_3_1
145 #define PPC_FEATURE2_ARCH_3_1 0x00040000
146 #endif
147 
148 /* POWER10 features */
149 #ifndef PPC_FEATURE2_MMA
150 #define PPC_FEATURE2_MMA 0x00020000
151 #endif
152 
153 #if defined(__powerpc64__)
154 #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.gp_regs[PT_NIP]
155 #define UCONTEXT_MSR(UC)	(UC)->uc_mcontext.gp_regs[PT_MSR]
156 #elif defined(__powerpc__)
157 #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
158 #define UCONTEXT_MSR(UC)	(UC)->uc_mcontext.uc_regs->gregs[PT_MSR]
159 #else
160 #error implement UCONTEXT_NIA
161 #endif
162 
163 #endif /* _SELFTESTS_POWERPC_UTILS_H */
164