1 /*
2  * Copyright 2013, Michael Ellerman, IBM Corp.
3  * Licensed under GPLv2.
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 void set_dscr(unsigned long val);
38 int perf_event_open_counter(unsigned int type,
39 			    unsigned long config, int group_fd);
40 int perf_event_enable(int fd);
41 int perf_event_disable(int fd);
42 int perf_event_reset(int fd);
43 
44 static inline bool have_hwcap(unsigned long ftr)
45 {
46 	return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
47 }
48 
49 #ifdef AT_HWCAP2
50 static inline bool have_hwcap2(unsigned long ftr2)
51 {
52 	return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
53 }
54 #else
55 static inline bool have_hwcap2(unsigned long ftr2)
56 {
57 	return false;
58 }
59 #endif
60 
61 bool is_ppc64le(void);
62 
63 /* Yes, this is evil */
64 #define FAIL_IF(x)						\
65 do {								\
66 	if ((x)) {						\
67 		fprintf(stderr,					\
68 		"[FAIL] Test FAILED on line %d\n", __LINE__);	\
69 		return 1;					\
70 	}							\
71 } while (0)
72 
73 /* The test harness uses this, yes it's gross */
74 #define MAGIC_SKIP_RETURN_VALUE	99
75 
76 #define SKIP_IF(x)						\
77 do {								\
78 	if ((x)) {						\
79 		fprintf(stderr,					\
80 		"[SKIP] Test skipped on line %d\n", __LINE__);	\
81 		return MAGIC_SKIP_RETURN_VALUE;			\
82 	}							\
83 } while (0)
84 
85 #define SKIP_IF_MSG(x, msg)					\
86 do {								\
87 	if ((x)) {						\
88 		fprintf(stderr,					\
89 		"[SKIP] Test skipped on line %d: %s\n",		\
90 		 __LINE__, msg);				\
91 		return MAGIC_SKIP_RETURN_VALUE;			\
92 	}							\
93 } while (0)
94 
95 #define _str(s) #s
96 #define str(s) _str(s)
97 
98 /* POWER9 feature */
99 #ifndef PPC_FEATURE2_ARCH_3_00
100 #define PPC_FEATURE2_ARCH_3_00 0x00800000
101 #endif
102 
103 #if defined(__powerpc64__)
104 #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.gp_regs[PT_NIP]
105 #define UCONTEXT_MSR(UC)	(UC)->uc_mcontext.gp_regs[PT_MSR]
106 #elif defined(__powerpc__)
107 #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
108 #define UCONTEXT_MSR(UC)	(UC)->uc_mcontext.uc_regs->gregs[PT_MSR]
109 #else
110 #error implement UCONTEXT_NIA
111 #endif
112 
113 #endif /* _SELFTESTS_POWERPC_UTILS_H */
114