1f50a7f3dSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
215ec3997SSimon Guo /*
315ec3997SSimon Guo  * Copyright 2013, Michael Ellerman, IBM Corp.
415ec3997SSimon Guo  */
515ec3997SSimon Guo 
615ec3997SSimon Guo #ifndef _SELFTESTS_POWERPC_UTILS_H
715ec3997SSimon Guo #define _SELFTESTS_POWERPC_UTILS_H
815ec3997SSimon Guo 
915ec3997SSimon Guo #define __cacheline_aligned __attribute__((aligned(128)))
1015ec3997SSimon Guo 
1115ec3997SSimon Guo #include <stdint.h>
12*b9125c9aSBenjamin Gray #include <stdio.h>
1315ec3997SSimon Guo #include <stdbool.h>
1415ec3997SSimon Guo #include <linux/auxvec.h>
15d2bf7932SNaveen N. Rao #include <linux/perf_event.h>
16178282a0SMichael Ellerman #include <asm/cputable.h>
1715ec3997SSimon Guo #include "reg.h"
18*b9125c9aSBenjamin Gray #include <unistd.h>
19*b9125c9aSBenjamin Gray 
20*b9125c9aSBenjamin Gray #ifndef ARRAY_SIZE
21*b9125c9aSBenjamin Gray # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
22*b9125c9aSBenjamin Gray #endif
2315ec3997SSimon Guo 
2415ec3997SSimon Guo /* Avoid headaches with PRI?64 - just use %ll? always */
2515ec3997SSimon Guo typedef unsigned long long u64;
2615ec3997SSimon Guo typedef   signed long long s64;
2715ec3997SSimon Guo 
2815ec3997SSimon Guo /* Just for familiarity */
2915ec3997SSimon Guo typedef uint32_t u32;
3015ec3997SSimon Guo typedef uint16_t u16;
3115ec3997SSimon Guo typedef uint8_t u8;
3215ec3997SSimon Guo 
3315ec3997SSimon Guo void test_harness_set_timeout(uint64_t time);
3415ec3997SSimon Guo int test_harness(int (test_function)(void), char *name);
35e3028437SMichael Ellerman 
36e3028437SMichael Ellerman int read_auxv(char *buf, ssize_t buf_size);
37e3028437SMichael Ellerman void *find_auxv_entry(int type, char *auxv);
38e3028437SMichael Ellerman void *get_auxv_entry(int type);
39e3028437SMichael Ellerman 
406ff4dc25SBenjamin Gray #define BIND_CPU_ANY	(-1)
416ff4dc25SBenjamin Gray 
4215ec3997SSimon Guo int pick_online_cpu(void);
43c97b2fc6SBenjamin Gray int bind_to_cpu(int cpu);
4415ec3997SSimon Guo 
45d1bc05b7SBenjamin Gray int parse_intmax(const char *buffer, size_t count, intmax_t *result, int base);
46d1bc05b7SBenjamin Gray int parse_uintmax(const char *buffer, size_t count, uintmax_t *result, int base);
47d1bc05b7SBenjamin Gray int parse_int(const char *buffer, size_t count, int *result, int base);
48d1bc05b7SBenjamin Gray int parse_uint(const char *buffer, size_t count, unsigned int *result, int base);
49d1bc05b7SBenjamin Gray int parse_long(const char *buffer, size_t count, long *result, int base);
50d1bc05b7SBenjamin Gray int parse_ulong(const char *buffer, size_t count, unsigned long *result, int base);
51d1bc05b7SBenjamin Gray 
52a974f0c1SBenjamin Gray int read_file(const char *path, char *buf, size_t count, size_t *len);
53a974f0c1SBenjamin Gray int write_file(const char *path, const char *buf, size_t count);
548d7253dcSBenjamin Gray int read_file_alloc(const char *path, char **buf, size_t *len);
555c20de57SBenjamin Gray int read_long(const char *path, long *result, int base);
565c20de57SBenjamin Gray int write_long(const char *path, long result, int base);
575c20de57SBenjamin Gray int read_ulong(const char *path, unsigned long *result, int base);
585c20de57SBenjamin Gray int write_ulong(const char *path, unsigned long result, int base);
59121d340bSBenjamin Gray int read_debugfs_file(const char *debugfs_file, char *buf, size_t count);
60121d340bSBenjamin Gray int write_debugfs_file(const char *debugfs_file, const char *buf, size_t count);
61121d340bSBenjamin Gray int read_debugfs_int(const char *debugfs_file, int *result);
62121d340bSBenjamin Gray int write_debugfs_int(const char *debugfs_file, int result);
63c790c3d2SMichael Ellerman int read_sysfs_file(char *debugfs_file, char *result, size_t result_size);
64d2bf7932SNaveen N. Rao int perf_event_open_counter(unsigned int type,
65d2bf7932SNaveen N. Rao 			    unsigned long config, int group_fd);
66d2bf7932SNaveen N. Rao int perf_event_enable(int fd);
67d2bf7932SNaveen N. Rao int perf_event_disable(int fd);
68d2bf7932SNaveen N. Rao int perf_event_reset(int fd);
69d2bf7932SNaveen N. Rao 
700d239f3bSDaniel Axtens struct perf_event_read {
710d239f3bSDaniel Axtens 	__u64 nr;
720d239f3bSDaniel Axtens 	__u64 l1d_misses;
730d239f3bSDaniel Axtens };
740d239f3bSDaniel Axtens 
75743f3544SSandipan Das #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 30)
76743f3544SSandipan Das #include <sys/syscall.h>
77743f3544SSandipan Das 
78743f3544SSandipan Das static inline pid_t gettid(void)
79743f3544SSandipan Das {
80743f3544SSandipan Das 	return syscall(SYS_gettid);
81743f3544SSandipan Das }
82743f3544SSandipan Das #endif
83743f3544SSandipan Das 
8415ec3997SSimon Guo static inline bool have_hwcap(unsigned long ftr)
8515ec3997SSimon Guo {
8615ec3997SSimon Guo 	return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
8715ec3997SSimon Guo }
8815ec3997SSimon Guo 
8915ec3997SSimon Guo #ifdef AT_HWCAP2
9015ec3997SSimon Guo static inline bool have_hwcap2(unsigned long ftr2)
9115ec3997SSimon Guo {
9215ec3997SSimon Guo 	return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
9315ec3997SSimon Guo }
9415ec3997SSimon Guo #else
9515ec3997SSimon Guo static inline bool have_hwcap2(unsigned long ftr2)
9615ec3997SSimon Guo {
9715ec3997SSimon Guo 	return false;
9815ec3997SSimon Guo }
9915ec3997SSimon Guo #endif
10015ec3997SSimon Guo 
101a069b5f9SAthira Rajeev static inline char *auxv_base_platform(void)
102a069b5f9SAthira Rajeev {
103a069b5f9SAthira Rajeev 	return ((char *)get_auxv_entry(AT_BASE_PLATFORM));
104a069b5f9SAthira Rajeev }
105a069b5f9SAthira Rajeev 
106a069b5f9SAthira Rajeev static inline char *auxv_platform(void)
107a069b5f9SAthira Rajeev {
108a069b5f9SAthira Rajeev 	return ((char *)get_auxv_entry(AT_PLATFORM));
109a069b5f9SAthira Rajeev }
110a069b5f9SAthira Rajeev 
11195f9b3afSMichael Ellerman bool is_ppc64le(void);
112c405b738SSandipan Das int using_hash_mmu(bool *using_hash);
11395f9b3afSMichael Ellerman 
11415ec3997SSimon Guo /* Yes, this is evil */
11515ec3997SSimon Guo #define FAIL_IF(x)						\
11615ec3997SSimon Guo do {								\
11715ec3997SSimon Guo 	if ((x)) {						\
11815ec3997SSimon Guo 		fprintf(stderr,					\
11915ec3997SSimon Guo 		"[FAIL] Test FAILED on line %d\n", __LINE__);	\
12015ec3997SSimon Guo 		return 1;					\
12115ec3997SSimon Guo 	}							\
12215ec3997SSimon Guo } while (0)
12315ec3997SSimon Guo 
124*b9125c9aSBenjamin Gray #define FAIL_IF_MSG(x, msg)					\
125*b9125c9aSBenjamin Gray do {								\
126*b9125c9aSBenjamin Gray 	if ((x)) {						\
127*b9125c9aSBenjamin Gray 		fprintf(stderr,					\
128*b9125c9aSBenjamin Gray 		"[FAIL] Test FAILED on line %d: %s\n", 		\
129*b9125c9aSBenjamin Gray 		__LINE__, msg);					\
130*b9125c9aSBenjamin Gray 		return 1;					\
131*b9125c9aSBenjamin Gray 	}							\
132*b9125c9aSBenjamin Gray } while (0)
133*b9125c9aSBenjamin Gray 
134e3333c59SSandipan Das #define FAIL_IF_EXIT(x)						\
135e3333c59SSandipan Das do {								\
136e3333c59SSandipan Das 	if ((x)) {						\
137e3333c59SSandipan Das 		fprintf(stderr,					\
138e3333c59SSandipan Das 		"[FAIL] Test FAILED on line %d\n", __LINE__);	\
139e3333c59SSandipan Das 		_exit(1);					\
140e3333c59SSandipan Das 	}							\
141e3333c59SSandipan Das } while (0)
142e3333c59SSandipan Das 
143*b9125c9aSBenjamin Gray #define FAIL_IF_EXIT_MSG(x, msg)				\
144*b9125c9aSBenjamin Gray do {								\
145*b9125c9aSBenjamin Gray 	if ((x)) {						\
146*b9125c9aSBenjamin Gray 		fprintf(stderr,					\
147*b9125c9aSBenjamin Gray 		"[FAIL] Test FAILED on line %d: %s\n", 		\
148*b9125c9aSBenjamin Gray 		__LINE__, msg);					\
149*b9125c9aSBenjamin Gray 		_exit(1);					\
150*b9125c9aSBenjamin Gray 	}							\
151*b9125c9aSBenjamin Gray } while (0)
152*b9125c9aSBenjamin Gray 
15315ec3997SSimon Guo /* The test harness uses this, yes it's gross */
15415ec3997SSimon Guo #define MAGIC_SKIP_RETURN_VALUE	99
15515ec3997SSimon Guo 
15615ec3997SSimon Guo #define SKIP_IF(x)						\
15715ec3997SSimon Guo do {								\
15815ec3997SSimon Guo 	if ((x)) {						\
15915ec3997SSimon Guo 		fprintf(stderr,					\
16015ec3997SSimon Guo 		"[SKIP] Test skipped on line %d\n", __LINE__);	\
16115ec3997SSimon Guo 		return MAGIC_SKIP_RETURN_VALUE;			\
16215ec3997SSimon Guo 	}							\
16315ec3997SSimon Guo } while (0)
16415ec3997SSimon Guo 
165e653b656SBreno Leitao #define SKIP_IF_MSG(x, msg)					\
166e653b656SBreno Leitao do {								\
167e653b656SBreno Leitao 	if ((x)) {						\
168e653b656SBreno Leitao 		fprintf(stderr,					\
169e653b656SBreno Leitao 		"[SKIP] Test skipped on line %d: %s\n",		\
170e653b656SBreno Leitao 		 __LINE__, msg);				\
171e653b656SBreno Leitao 		return MAGIC_SKIP_RETURN_VALUE;			\
172e653b656SBreno Leitao 	}							\
173e653b656SBreno Leitao } while (0)
174e653b656SBreno Leitao 
17515ec3997SSimon Guo #define _str(s) #s
17615ec3997SSimon Guo #define str(s) _str(s)
17715ec3997SSimon Guo 
178128d3d02SSandipan Das #define sigsafe_err(msg)	({ \
179128d3d02SSandipan Das 		ssize_t nbytes __attribute__((unused)); \
180128d3d02SSandipan Das 		nbytes = write(STDERR_FILENO, msg, strlen(msg)); })
181128d3d02SSandipan Das 
18215ec3997SSimon Guo /* POWER9 feature */
18315ec3997SSimon Guo #ifndef PPC_FEATURE2_ARCH_3_00
18415ec3997SSimon Guo #define PPC_FEATURE2_ARCH_3_00 0x00800000
18515ec3997SSimon Guo #endif
18615ec3997SSimon Guo 
187620a6473SJordan Niethe /* POWER10 feature */
188620a6473SJordan Niethe #ifndef PPC_FEATURE2_ARCH_3_1
189620a6473SJordan Niethe #define PPC_FEATURE2_ARCH_3_1 0x00040000
190620a6473SJordan Niethe #endif
191620a6473SJordan Niethe 
1923527e1abSAlistair Popple /* POWER10 features */
1933527e1abSAlistair Popple #ifndef PPC_FEATURE2_MMA
1943527e1abSAlistair Popple #define PPC_FEATURE2_MMA 0x00020000
1953527e1abSAlistair Popple #endif
1963527e1abSAlistair Popple 
197db384851SNaveen N. Rao #if defined(__powerpc64__)
198db384851SNaveen N. Rao #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.gp_regs[PT_NIP]
199a65329aaSBreno Leitao #define UCONTEXT_MSR(UC)	(UC)->uc_mcontext.gp_regs[PT_MSR]
200db384851SNaveen N. Rao #elif defined(__powerpc__)
201db384851SNaveen N. Rao #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
202a65329aaSBreno Leitao #define UCONTEXT_MSR(UC)	(UC)->uc_mcontext.uc_regs->gregs[PT_MSR]
203db384851SNaveen N. Rao #else
204db384851SNaveen N. Rao #error implement UCONTEXT_NIA
205db384851SNaveen N. Rao #endif
206db384851SNaveen N. Rao 
20715ec3997SSimon Guo #endif /* _SELFTESTS_POWERPC_UTILS_H */
208