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 "reg.h"
16 
17 /* Avoid headaches with PRI?64 - just use %ll? always */
18 typedef unsigned long long u64;
19 typedef   signed long long s64;
20 
21 /* Just for familiarity */
22 typedef uint32_t u32;
23 typedef uint16_t u16;
24 typedef uint8_t u8;
25 
26 void test_harness_set_timeout(uint64_t time);
27 int test_harness(int (test_function)(void), char *name);
28 
29 int read_auxv(char *buf, ssize_t buf_size);
30 void *find_auxv_entry(int type, char *auxv);
31 void *get_auxv_entry(int type);
32 
33 int pick_online_cpu(void);
34 
35 int read_debugfs_file(char *debugfs_file, int *result);
36 int write_debugfs_file(char *debugfs_file, int result);
37 int read_sysfs_file(char *debugfs_file, char *result, size_t result_size);
38 void set_dscr(unsigned long val);
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 static inline bool have_hwcap(unsigned long ftr)
46 {
47 	return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
48 }
49 
50 #ifdef AT_HWCAP2
51 static inline bool have_hwcap2(unsigned long ftr2)
52 {
53 	return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
54 }
55 #else
56 static inline bool have_hwcap2(unsigned long ftr2)
57 {
58 	return false;
59 }
60 #endif
61 
62 bool is_ppc64le(void);
63 
64 /* Yes, this is evil */
65 #define FAIL_IF(x)						\
66 do {								\
67 	if ((x)) {						\
68 		fprintf(stderr,					\
69 		"[FAIL] Test FAILED on line %d\n", __LINE__);	\
70 		return 1;					\
71 	}							\
72 } while (0)
73 
74 /* The test harness uses this, yes it's gross */
75 #define MAGIC_SKIP_RETURN_VALUE	99
76 
77 #define SKIP_IF(x)						\
78 do {								\
79 	if ((x)) {						\
80 		fprintf(stderr,					\
81 		"[SKIP] Test skipped on line %d\n", __LINE__);	\
82 		return MAGIC_SKIP_RETURN_VALUE;			\
83 	}							\
84 } while (0)
85 
86 #define SKIP_IF_MSG(x, msg)					\
87 do {								\
88 	if ((x)) {						\
89 		fprintf(stderr,					\
90 		"[SKIP] Test skipped on line %d: %s\n",		\
91 		 __LINE__, msg);				\
92 		return MAGIC_SKIP_RETURN_VALUE;			\
93 	}							\
94 } while (0)
95 
96 #define _str(s) #s
97 #define str(s) _str(s)
98 
99 /* POWER9 feature */
100 #ifndef PPC_FEATURE2_ARCH_3_00
101 #define PPC_FEATURE2_ARCH_3_00 0x00800000
102 #endif
103 
104 #if defined(__powerpc64__)
105 #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.gp_regs[PT_NIP]
106 #define UCONTEXT_MSR(UC)	(UC)->uc_mcontext.gp_regs[PT_MSR]
107 #elif defined(__powerpc__)
108 #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
109 #define UCONTEXT_MSR(UC)	(UC)->uc_mcontext.uc_regs->gregs[PT_MSR]
110 #else
111 #error implement UCONTEXT_NIA
112 #endif
113 
114 #endif /* _SELFTESTS_POWERPC_UTILS_H */
115