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);
27e3028437SMichael Ellerman 
28e3028437SMichael Ellerman int read_auxv(char *buf, ssize_t buf_size);
29e3028437SMichael Ellerman void *find_auxv_entry(int type, char *auxv);
30e3028437SMichael Ellerman void *get_auxv_entry(int type);
31e3028437SMichael Ellerman 
3215ec3997SSimon Guo int pick_online_cpu(void);
3315ec3997SSimon Guo 
3415ec3997SSimon Guo static inline bool have_hwcap(unsigned long ftr)
3515ec3997SSimon Guo {
3615ec3997SSimon Guo 	return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
3715ec3997SSimon Guo }
3815ec3997SSimon Guo 
3915ec3997SSimon Guo #ifdef AT_HWCAP2
4015ec3997SSimon Guo static inline bool have_hwcap2(unsigned long ftr2)
4115ec3997SSimon Guo {
4215ec3997SSimon Guo 	return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
4315ec3997SSimon Guo }
4415ec3997SSimon Guo #else
4515ec3997SSimon Guo static inline bool have_hwcap2(unsigned long ftr2)
4615ec3997SSimon Guo {
4715ec3997SSimon Guo 	return false;
4815ec3997SSimon Guo }
4915ec3997SSimon Guo #endif
5015ec3997SSimon Guo 
5115ec3997SSimon Guo /* Yes, this is evil */
5215ec3997SSimon Guo #define FAIL_IF(x)						\
5315ec3997SSimon Guo do {								\
5415ec3997SSimon Guo 	if ((x)) {						\
5515ec3997SSimon Guo 		fprintf(stderr,					\
5615ec3997SSimon Guo 		"[FAIL] Test FAILED on line %d\n", __LINE__);	\
5715ec3997SSimon Guo 		return 1;					\
5815ec3997SSimon Guo 	}							\
5915ec3997SSimon Guo } while (0)
6015ec3997SSimon Guo 
6115ec3997SSimon Guo /* The test harness uses this, yes it's gross */
6215ec3997SSimon Guo #define MAGIC_SKIP_RETURN_VALUE	99
6315ec3997SSimon Guo 
6415ec3997SSimon Guo #define SKIP_IF(x)						\
6515ec3997SSimon Guo do {								\
6615ec3997SSimon Guo 	if ((x)) {						\
6715ec3997SSimon Guo 		fprintf(stderr,					\
6815ec3997SSimon Guo 		"[SKIP] Test skipped on line %d\n", __LINE__);	\
6915ec3997SSimon Guo 		return MAGIC_SKIP_RETURN_VALUE;			\
7015ec3997SSimon Guo 	}							\
7115ec3997SSimon Guo } while (0)
7215ec3997SSimon Guo 
7315ec3997SSimon Guo #define _str(s) #s
7415ec3997SSimon Guo #define str(s) _str(s)
7515ec3997SSimon Guo 
7615ec3997SSimon Guo /* POWER9 feature */
7715ec3997SSimon Guo #ifndef PPC_FEATURE2_ARCH_3_00
7815ec3997SSimon Guo #define PPC_FEATURE2_ARCH_3_00 0x00800000
7915ec3997SSimon Guo #endif
8015ec3997SSimon Guo 
8115ec3997SSimon Guo #endif /* _SELFTESTS_POWERPC_UTILS_H */
82