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>
1415ec3997SSimon Guo #include "reg.h"
1515ec3997SSimon Guo 
1615ec3997SSimon Guo /* Avoid headaches with PRI?64 - just use %ll? always */
1715ec3997SSimon Guo typedef unsigned long long u64;
1815ec3997SSimon Guo typedef   signed long long s64;
1915ec3997SSimon Guo 
2015ec3997SSimon Guo /* Just for familiarity */
2115ec3997SSimon Guo typedef uint32_t u32;
2215ec3997SSimon Guo typedef uint16_t u16;
2315ec3997SSimon Guo typedef uint8_t u8;
2415ec3997SSimon Guo 
2515ec3997SSimon Guo void test_harness_set_timeout(uint64_t time);
2615ec3997SSimon Guo int test_harness(int (test_function)(void), char *name);
2715ec3997SSimon Guo extern void *get_auxv_entry(int type);
2815ec3997SSimon Guo int pick_online_cpu(void);
2915ec3997SSimon Guo 
3015ec3997SSimon Guo static inline bool have_hwcap(unsigned long ftr)
3115ec3997SSimon Guo {
3215ec3997SSimon Guo 	return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
3315ec3997SSimon Guo }
3415ec3997SSimon Guo 
3515ec3997SSimon Guo #ifdef AT_HWCAP2
3615ec3997SSimon Guo static inline bool have_hwcap2(unsigned long ftr2)
3715ec3997SSimon Guo {
3815ec3997SSimon Guo 	return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
3915ec3997SSimon Guo }
4015ec3997SSimon Guo #else
4115ec3997SSimon Guo static inline bool have_hwcap2(unsigned long ftr2)
4215ec3997SSimon Guo {
4315ec3997SSimon Guo 	return false;
4415ec3997SSimon Guo }
4515ec3997SSimon Guo #endif
4615ec3997SSimon Guo 
4715ec3997SSimon Guo /* Yes, this is evil */
4815ec3997SSimon Guo #define FAIL_IF(x)						\
4915ec3997SSimon Guo do {								\
5015ec3997SSimon Guo 	if ((x)) {						\
5115ec3997SSimon Guo 		fprintf(stderr,					\
5215ec3997SSimon Guo 		"[FAIL] Test FAILED on line %d\n", __LINE__);	\
5315ec3997SSimon Guo 		return 1;					\
5415ec3997SSimon Guo 	}							\
5515ec3997SSimon Guo } while (0)
5615ec3997SSimon Guo 
5715ec3997SSimon Guo /* The test harness uses this, yes it's gross */
5815ec3997SSimon Guo #define MAGIC_SKIP_RETURN_VALUE	99
5915ec3997SSimon Guo 
6015ec3997SSimon Guo #define SKIP_IF(x)						\
6115ec3997SSimon Guo do {								\
6215ec3997SSimon Guo 	if ((x)) {						\
6315ec3997SSimon Guo 		fprintf(stderr,					\
6415ec3997SSimon Guo 		"[SKIP] Test skipped on line %d\n", __LINE__);	\
6515ec3997SSimon Guo 		return MAGIC_SKIP_RETURN_VALUE;			\
6615ec3997SSimon Guo 	}							\
6715ec3997SSimon Guo } while (0)
6815ec3997SSimon Guo 
6915ec3997SSimon Guo #define _str(s) #s
7015ec3997SSimon Guo #define str(s) _str(s)
7115ec3997SSimon Guo 
7215ec3997SSimon Guo /* POWER9 feature */
7315ec3997SSimon Guo #ifndef PPC_FEATURE2_ARCH_3_00
7415ec3997SSimon Guo #define PPC_FEATURE2_ARCH_3_00 0x00800000
7515ec3997SSimon Guo #endif
7615ec3997SSimon Guo 
7715ec3997SSimon Guo #endif /* _SELFTESTS_POWERPC_UTILS_H */
78