162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _SELFTESTS_POWERPC_BASIC_ASM_H
362306a36Sopenharmony_ci#define _SELFTESTS_POWERPC_BASIC_ASM_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <ppc-asm.h>
662306a36Sopenharmony_ci#include <asm/unistd.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifdef __powerpc64__
962306a36Sopenharmony_ci#define PPC_LL		ld
1062306a36Sopenharmony_ci#define PPC_STL		std
1162306a36Sopenharmony_ci#define PPC_STLU	stdu
1262306a36Sopenharmony_ci#else
1362306a36Sopenharmony_ci#define PPC_LL		lwz
1462306a36Sopenharmony_ci#define PPC_STL		stw
1562306a36Sopenharmony_ci#define PPC_STLU	stwu
1662306a36Sopenharmony_ci#endif
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define LOAD_REG_IMMEDIATE(reg, expr) \
1962306a36Sopenharmony_ci	lis	reg, (expr)@highest;	\
2062306a36Sopenharmony_ci	ori	reg, reg, (expr)@higher;	\
2162306a36Sopenharmony_ci	rldicr	reg, reg, 32, 31;	\
2262306a36Sopenharmony_ci	oris	reg, reg, (expr)@high;	\
2362306a36Sopenharmony_ci	ori	reg, reg, (expr)@l;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * Note: These macros assume that variables being stored on the stack are
2762306a36Sopenharmony_ci * sizeof(long), while this is usually the case it may not always be the
2862306a36Sopenharmony_ci * case for each use case.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci#ifdef  __powerpc64__
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci// ABIv2
3362306a36Sopenharmony_ci#if defined(_CALL_ELF) && _CALL_ELF == 2
3462306a36Sopenharmony_ci#define STACK_FRAME_MIN_SIZE 32
3562306a36Sopenharmony_ci#define STACK_FRAME_TOC_POS  24
3662306a36Sopenharmony_ci#define __STACK_FRAME_PARAM(_param)  (32 + ((_param)*8))
3762306a36Sopenharmony_ci#define __STACK_FRAME_LOCAL(_num_params, _var_num)  \
3862306a36Sopenharmony_ci	((STACK_FRAME_PARAM(_num_params)) + ((_var_num)*8))
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#else // ABIv1 below
4162306a36Sopenharmony_ci#define STACK_FRAME_MIN_SIZE 112
4262306a36Sopenharmony_ci#define STACK_FRAME_TOC_POS  40
4362306a36Sopenharmony_ci#define __STACK_FRAME_PARAM(i)  (48 + ((i)*8))
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/*
4662306a36Sopenharmony_ci * Caveat: if a function passed more than 8 doublewords, the caller will have
4762306a36Sopenharmony_ci * made more space... which would render the 112 incorrect.
4862306a36Sopenharmony_ci */
4962306a36Sopenharmony_ci#define __STACK_FRAME_LOCAL(_num_params, _var_num)  \
5062306a36Sopenharmony_ci	(112 + ((_var_num)*8))
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#endif // ABIv2
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci// Common 64-bit
5662306a36Sopenharmony_ci#define STACK_FRAME_LR_POS   16
5762306a36Sopenharmony_ci#define STACK_FRAME_CR_POS   8
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci#else // 32-bit below
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define STACK_FRAME_MIN_SIZE 16
6262306a36Sopenharmony_ci#define STACK_FRAME_LR_POS   4
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#define __STACK_FRAME_PARAM(_param)  (STACK_FRAME_MIN_SIZE + ((_param)*4))
6562306a36Sopenharmony_ci#define __STACK_FRAME_LOCAL(_num_params, _var_num)  \
6662306a36Sopenharmony_ci	((STACK_FRAME_PARAM(_num_params)) + ((_var_num)*4))
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#endif // __powerpc64__
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/* Parameter x saved to the stack */
7162306a36Sopenharmony_ci#define STACK_FRAME_PARAM(var)    __STACK_FRAME_PARAM(var)
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci/* Local variable x saved to the stack after x parameters */
7462306a36Sopenharmony_ci#define STACK_FRAME_LOCAL(num_params, var)    \
7562306a36Sopenharmony_ci	__STACK_FRAME_LOCAL(num_params, var)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/*
7862306a36Sopenharmony_ci * It is very important to note here that _extra is the extra amount of
7962306a36Sopenharmony_ci * stack space needed. This space can be accessed using STACK_FRAME_PARAM()
8062306a36Sopenharmony_ci * or STACK_FRAME_LOCAL() macros.
8162306a36Sopenharmony_ci *
8262306a36Sopenharmony_ci * r1 and r2 are not defined in ppc-asm.h (instead they are defined as sp
8362306a36Sopenharmony_ci * and toc). Kernel programmers tend to prefer rX even for r1 and r2, hence
8462306a36Sopenharmony_ci * %1 and %r2. r0 is defined in ppc-asm.h and therefore %r0 gets
8562306a36Sopenharmony_ci * preprocessed incorrectly, hence r0.
8662306a36Sopenharmony_ci */
8762306a36Sopenharmony_ci#define PUSH_BASIC_STACK(_extra) \
8862306a36Sopenharmony_ci	mflr	 r0; \
8962306a36Sopenharmony_ci	PPC_STL	 r0, STACK_FRAME_LR_POS(%r1); \
9062306a36Sopenharmony_ci	PPC_STLU %r1, -(((_extra + 15) & ~15) + STACK_FRAME_MIN_SIZE)(%r1);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define POP_BASIC_STACK(_extra) \
9362306a36Sopenharmony_ci	addi	%r1, %r1, (((_extra + 15) & ~15) + STACK_FRAME_MIN_SIZE); \
9462306a36Sopenharmony_ci	PPC_LL	r0, STACK_FRAME_LR_POS(%r1); \
9562306a36Sopenharmony_ci	mtlr	r0;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci.macro OP_REGS op, reg_width, start_reg, end_reg, base_reg, base_reg_offset=0, skip=0
9862306a36Sopenharmony_ci	.set i, \start_reg
9962306a36Sopenharmony_ci	.rept (\end_reg - \start_reg + 1)
10062306a36Sopenharmony_ci	\op	i, (\reg_width * (i - \skip) + \base_reg_offset)(\base_reg)
10162306a36Sopenharmony_ci	.set i, i + 1
10262306a36Sopenharmony_ci	.endr
10362306a36Sopenharmony_ci.endm
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#endif /* _SELFTESTS_POWERPC_BASIC_ASM_H */
106