115ec3997SSimon Guo /*
215ec3997SSimon Guo  * Copyright 2013, Michael Ellerman, IBM Corp.
315ec3997SSimon Guo  * Licensed under GPLv2.
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>
1215ec3997SSimon Guo #include <stdbool.h>
1315ec3997SSimon Guo #include <linux/auxvec.h>
14d2bf7932SNaveen N. Rao #include <linux/perf_event.h>
1515ec3997SSimon Guo #include "reg.h"
1615ec3997SSimon Guo 
1715ec3997SSimon Guo /* Avoid headaches with PRI?64 - just use %ll? always */
1815ec3997SSimon Guo typedef unsigned long long u64;
1915ec3997SSimon Guo typedef   signed long long s64;
2015ec3997SSimon Guo 
2115ec3997SSimon Guo /* Just for familiarity */
2215ec3997SSimon Guo typedef uint32_t u32;
2315ec3997SSimon Guo typedef uint16_t u16;
2415ec3997SSimon Guo typedef uint8_t u8;
2515ec3997SSimon Guo 
2615ec3997SSimon Guo void test_harness_set_timeout(uint64_t time);
2715ec3997SSimon Guo int test_harness(int (test_function)(void), char *name);
28e3028437SMichael Ellerman 
29e3028437SMichael Ellerman int read_auxv(char *buf, ssize_t buf_size);
30e3028437SMichael Ellerman void *find_auxv_entry(int type, char *auxv);
31e3028437SMichael Ellerman void *get_auxv_entry(int type);
32e3028437SMichael Ellerman 
3315ec3997SSimon Guo int pick_online_cpu(void);
3415ec3997SSimon Guo 
35d2bf7932SNaveen N. Rao int read_debugfs_file(char *debugfs_file, int *result);
36d2bf7932SNaveen N. Rao int write_debugfs_file(char *debugfs_file, int result);
37d2bf7932SNaveen N. Rao void set_dscr(unsigned long val);
38d2bf7932SNaveen N. Rao int perf_event_open_counter(unsigned int type,
39d2bf7932SNaveen N. Rao 			    unsigned long config, int group_fd);
40d2bf7932SNaveen N. Rao int perf_event_enable(int fd);
41d2bf7932SNaveen N. Rao int perf_event_disable(int fd);
42d2bf7932SNaveen N. Rao int perf_event_reset(int fd);
43d2bf7932SNaveen N. Rao 
4415ec3997SSimon Guo static inline bool have_hwcap(unsigned long ftr)
4515ec3997SSimon Guo {
4615ec3997SSimon Guo 	return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
4715ec3997SSimon Guo }
4815ec3997SSimon Guo 
4915ec3997SSimon Guo #ifdef AT_HWCAP2
5015ec3997SSimon Guo static inline bool have_hwcap2(unsigned long ftr2)
5115ec3997SSimon Guo {
5215ec3997SSimon Guo 	return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
5315ec3997SSimon Guo }
5415ec3997SSimon Guo #else
5515ec3997SSimon Guo static inline bool have_hwcap2(unsigned long ftr2)
5615ec3997SSimon Guo {
5715ec3997SSimon Guo 	return false;
5815ec3997SSimon Guo }
5915ec3997SSimon Guo #endif
6015ec3997SSimon Guo 
6195f9b3afSMichael Ellerman bool is_ppc64le(void);
6295f9b3afSMichael Ellerman 
6315ec3997SSimon Guo /* Yes, this is evil */
6415ec3997SSimon Guo #define FAIL_IF(x)						\
6515ec3997SSimon Guo do {								\
6615ec3997SSimon Guo 	if ((x)) {						\
6715ec3997SSimon Guo 		fprintf(stderr,					\
6815ec3997SSimon Guo 		"[FAIL] Test FAILED on line %d\n", __LINE__);	\
6915ec3997SSimon Guo 		return 1;					\
7015ec3997SSimon Guo 	}							\
7115ec3997SSimon Guo } while (0)
7215ec3997SSimon Guo 
7315ec3997SSimon Guo /* The test harness uses this, yes it's gross */
7415ec3997SSimon Guo #define MAGIC_SKIP_RETURN_VALUE	99
7515ec3997SSimon Guo 
7615ec3997SSimon Guo #define SKIP_IF(x)						\
7715ec3997SSimon Guo do {								\
7815ec3997SSimon Guo 	if ((x)) {						\
7915ec3997SSimon Guo 		fprintf(stderr,					\
8015ec3997SSimon Guo 		"[SKIP] Test skipped on line %d\n", __LINE__);	\
8115ec3997SSimon Guo 		return MAGIC_SKIP_RETURN_VALUE;			\
8215ec3997SSimon Guo 	}							\
8315ec3997SSimon Guo } while (0)
8415ec3997SSimon Guo 
8515ec3997SSimon Guo #define _str(s) #s
8615ec3997SSimon Guo #define str(s) _str(s)
8715ec3997SSimon Guo 
8815ec3997SSimon Guo /* POWER9 feature */
8915ec3997SSimon Guo #ifndef PPC_FEATURE2_ARCH_3_00
9015ec3997SSimon Guo #define PPC_FEATURE2_ARCH_3_00 0x00800000
9115ec3997SSimon Guo #endif
9215ec3997SSimon Guo 
93db384851SNaveen N. Rao #if defined(__powerpc64__)
94db384851SNaveen N. Rao #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.gp_regs[PT_NIP]
95db384851SNaveen N. Rao #elif defined(__powerpc__)
96db384851SNaveen N. Rao #define UCONTEXT_NIA(UC)	(UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
97db384851SNaveen N. Rao #else
98db384851SNaveen N. Rao #error implement UCONTEXT_NIA
99db384851SNaveen N. Rao #endif
100db384851SNaveen N. Rao 
10115ec3997SSimon Guo #endif /* _SELFTESTS_POWERPC_UTILS_H */
102