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_file(const char *path, char *buf, size_t count, size_t *len); 37 int write_file(const char *path, const char *buf, size_t count); 38 int read_debugfs_file(const char *debugfs_file, char *buf, size_t count); 39 int write_debugfs_file(const char *debugfs_file, const char *buf, size_t count); 40 int read_debugfs_int(const char *debugfs_file, int *result); 41 int write_debugfs_int(const char *debugfs_file, int result); 42 int read_sysfs_file(char *debugfs_file, char *result, size_t result_size); 43 int perf_event_open_counter(unsigned int type, 44 unsigned long config, int group_fd); 45 int perf_event_enable(int fd); 46 int perf_event_disable(int fd); 47 int perf_event_reset(int fd); 48 49 struct perf_event_read { 50 __u64 nr; 51 __u64 l1d_misses; 52 }; 53 54 #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 30) 55 #include <unistd.h> 56 #include <sys/syscall.h> 57 58 static inline pid_t gettid(void) 59 { 60 return syscall(SYS_gettid); 61 } 62 #endif 63 64 static inline bool have_hwcap(unsigned long ftr) 65 { 66 return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr; 67 } 68 69 #ifdef AT_HWCAP2 70 static inline bool have_hwcap2(unsigned long ftr2) 71 { 72 return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2; 73 } 74 #else 75 static inline bool have_hwcap2(unsigned long ftr2) 76 { 77 return false; 78 } 79 #endif 80 81 static inline char *auxv_base_platform(void) 82 { 83 return ((char *)get_auxv_entry(AT_BASE_PLATFORM)); 84 } 85 86 static inline char *auxv_platform(void) 87 { 88 return ((char *)get_auxv_entry(AT_PLATFORM)); 89 } 90 91 bool is_ppc64le(void); 92 int using_hash_mmu(bool *using_hash); 93 94 /* Yes, this is evil */ 95 #define FAIL_IF(x) \ 96 do { \ 97 if ((x)) { \ 98 fprintf(stderr, \ 99 "[FAIL] Test FAILED on line %d\n", __LINE__); \ 100 return 1; \ 101 } \ 102 } while (0) 103 104 #define FAIL_IF_EXIT(x) \ 105 do { \ 106 if ((x)) { \ 107 fprintf(stderr, \ 108 "[FAIL] Test FAILED on line %d\n", __LINE__); \ 109 _exit(1); \ 110 } \ 111 } while (0) 112 113 /* The test harness uses this, yes it's gross */ 114 #define MAGIC_SKIP_RETURN_VALUE 99 115 116 #define SKIP_IF(x) \ 117 do { \ 118 if ((x)) { \ 119 fprintf(stderr, \ 120 "[SKIP] Test skipped on line %d\n", __LINE__); \ 121 return MAGIC_SKIP_RETURN_VALUE; \ 122 } \ 123 } while (0) 124 125 #define SKIP_IF_MSG(x, msg) \ 126 do { \ 127 if ((x)) { \ 128 fprintf(stderr, \ 129 "[SKIP] Test skipped on line %d: %s\n", \ 130 __LINE__, msg); \ 131 return MAGIC_SKIP_RETURN_VALUE; \ 132 } \ 133 } while (0) 134 135 #define _str(s) #s 136 #define str(s) _str(s) 137 138 #define sigsafe_err(msg) ({ \ 139 ssize_t nbytes __attribute__((unused)); \ 140 nbytes = write(STDERR_FILENO, msg, strlen(msg)); }) 141 142 /* POWER9 feature */ 143 #ifndef PPC_FEATURE2_ARCH_3_00 144 #define PPC_FEATURE2_ARCH_3_00 0x00800000 145 #endif 146 147 /* POWER10 feature */ 148 #ifndef PPC_FEATURE2_ARCH_3_1 149 #define PPC_FEATURE2_ARCH_3_1 0x00040000 150 #endif 151 152 /* POWER10 features */ 153 #ifndef PPC_FEATURE2_MMA 154 #define PPC_FEATURE2_MMA 0x00020000 155 #endif 156 157 #if defined(__powerpc64__) 158 #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP] 159 #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.gp_regs[PT_MSR] 160 #elif defined(__powerpc__) 161 #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP] 162 #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_MSR] 163 #else 164 #error implement UCONTEXT_NIA 165 #endif 166 167 #endif /* _SELFTESTS_POWERPC_UTILS_H */ 168