162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  Definitions for use by exception code on Book3-E
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (C) 2008 Ben. Herrenschmidt (benh@kernel.crashing.org), IBM Corp.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci#ifndef _ASM_POWERPC_EXCEPTION_64E_H
862306a36Sopenharmony_ci#define _ASM_POWERPC_EXCEPTION_64E_H
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/*
1162306a36Sopenharmony_ci * SPRGs usage an other considerations...
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Since TLB miss and other standard exceptions can be interrupted by
1462306a36Sopenharmony_ci * critical exceptions which can themselves be interrupted by machine
1562306a36Sopenharmony_ci * checks, and since the two later can themselves cause a TLB miss when
1662306a36Sopenharmony_ci * hitting the linear mapping for the kernel stacks, we need to be a bit
1762306a36Sopenharmony_ci * creative on how we use SPRGs.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * The base idea is that we have one SRPG reserved for critical and one
2062306a36Sopenharmony_ci * for machine check interrupts. Those are used to save a GPR that can
2162306a36Sopenharmony_ci * then be used to get the PACA, and store as much context as we need
2262306a36Sopenharmony_ci * to save in there. That includes saving the SPRGs used by the TLB miss
2362306a36Sopenharmony_ci * handler for linear mapping misses and the associated SRR0/1 due to
2462306a36Sopenharmony_ci * the above re-entrancy issue.
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * So here's the current usage pattern. It's done regardless of which
2762306a36Sopenharmony_ci * SPRGs are user-readable though, thus we might have to change some of
2862306a36Sopenharmony_ci * this later. In order to do that more easily, we use special constants
2962306a36Sopenharmony_ci * for naming them
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * WARNING: Some of these SPRGs are user readable. We need to do something
3262306a36Sopenharmony_ci * about it as some point by making sure they can't be used to leak kernel
3362306a36Sopenharmony_ci * critical data
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#define PACA_EXGDBELL PACA_EXGEN
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* We are out of SPRGs so we save some things in the PACA. The normal
3962306a36Sopenharmony_ci * exception frame is smaller than the CRIT or MC one though
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_ci#define EX_R1		(0 * 8)
4262306a36Sopenharmony_ci#define EX_CR		(1 * 8)
4362306a36Sopenharmony_ci#define EX_R10		(2 * 8)
4462306a36Sopenharmony_ci#define EX_R11		(3 * 8)
4562306a36Sopenharmony_ci#define EX_R14		(4 * 8)
4662306a36Sopenharmony_ci#define EX_R15		(5 * 8)
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/*
4962306a36Sopenharmony_ci * The TLB miss exception uses different slots.
5062306a36Sopenharmony_ci *
5162306a36Sopenharmony_ci * The bolted variant uses only the first six fields,
5262306a36Sopenharmony_ci * which in combination with pgd and kernel_pgd fits in
5362306a36Sopenharmony_ci * one 64-byte cache line.
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#define EX_TLB_R10	( 0 * 8)
5762306a36Sopenharmony_ci#define EX_TLB_R11	( 1 * 8)
5862306a36Sopenharmony_ci#define EX_TLB_R14	( 2 * 8)
5962306a36Sopenharmony_ci#define EX_TLB_R15	( 3 * 8)
6062306a36Sopenharmony_ci#define EX_TLB_R16	( 4 * 8)
6162306a36Sopenharmony_ci#define EX_TLB_CR	( 5 * 8)
6262306a36Sopenharmony_ci#define EX_TLB_R12	( 6 * 8)
6362306a36Sopenharmony_ci#define EX_TLB_R13	( 7 * 8)
6462306a36Sopenharmony_ci#define EX_TLB_DEAR	( 8 * 8) /* Level 0 and 2 only */
6562306a36Sopenharmony_ci#define EX_TLB_ESR	( 9 * 8) /* Level 0 and 2 only */
6662306a36Sopenharmony_ci#define EX_TLB_SRR0	(10 * 8)
6762306a36Sopenharmony_ci#define EX_TLB_SRR1	(11 * 8)
6862306a36Sopenharmony_ci#define EX_TLB_R7	(12 * 8)
6962306a36Sopenharmony_ci#define EX_TLB_SIZE	(13 * 8)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#define	START_EXCEPTION(label)						\
7262306a36Sopenharmony_ci	.globl exc_##label##_book3e;					\
7362306a36Sopenharmony_ciexc_##label##_book3e:
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/* TLB miss exception prolog
7662306a36Sopenharmony_ci *
7762306a36Sopenharmony_ci * This prolog handles re-entrancy (up to 3 levels supported in the PACA
7862306a36Sopenharmony_ci * though we currently don't test for overflow). It provides you with a
7962306a36Sopenharmony_ci * re-entrancy safe working space of r10...r16 and CR with r12 being used
8062306a36Sopenharmony_ci * as the exception area pointer in the PACA for that level of re-entrancy
8162306a36Sopenharmony_ci * and r13 containing the PACA pointer.
8262306a36Sopenharmony_ci *
8362306a36Sopenharmony_ci * SRR0 and SRR1 are saved, but DEAR and ESR are not, since they don't apply
8462306a36Sopenharmony_ci * as-is for instruction exceptions. It's up to the actual exception code
8562306a36Sopenharmony_ci * to save them as well if required.
8662306a36Sopenharmony_ci */
8762306a36Sopenharmony_ci#define TLB_MISS_PROLOG							    \
8862306a36Sopenharmony_ci	mtspr	SPRN_SPRG_TLB_SCRATCH,r12;				    \
8962306a36Sopenharmony_ci	mfspr	r12,SPRN_SPRG_TLB_EXFRAME;				    \
9062306a36Sopenharmony_ci	std	r10,EX_TLB_R10(r12);					    \
9162306a36Sopenharmony_ci	mfcr	r10;							    \
9262306a36Sopenharmony_ci	std	r11,EX_TLB_R11(r12);					    \
9362306a36Sopenharmony_ci	mfspr	r11,SPRN_SPRG_TLB_SCRATCH;				    \
9462306a36Sopenharmony_ci	std	r13,EX_TLB_R13(r12);					    \
9562306a36Sopenharmony_ci	mfspr	r13,SPRN_SPRG_PACA;					    \
9662306a36Sopenharmony_ci	std	r14,EX_TLB_R14(r12);					    \
9762306a36Sopenharmony_ci	addi	r14,r12,EX_TLB_SIZE;					    \
9862306a36Sopenharmony_ci	std	r15,EX_TLB_R15(r12);					    \
9962306a36Sopenharmony_ci	mfspr	r15,SPRN_SRR1;						    \
10062306a36Sopenharmony_ci	std	r16,EX_TLB_R16(r12);					    \
10162306a36Sopenharmony_ci	mfspr	r16,SPRN_SRR0;						    \
10262306a36Sopenharmony_ci	std	r10,EX_TLB_CR(r12);					    \
10362306a36Sopenharmony_ci	std	r11,EX_TLB_R12(r12);					    \
10462306a36Sopenharmony_ci	mtspr	SPRN_SPRG_TLB_EXFRAME,r14;				    \
10562306a36Sopenharmony_ci	std	r15,EX_TLB_SRR1(r12);					    \
10662306a36Sopenharmony_ci	std	r16,EX_TLB_SRR0(r12);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/* And these are the matching epilogs that restores things
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * There are 3 epilogs:
11162306a36Sopenharmony_ci *
11262306a36Sopenharmony_ci * - SUCCESS       : Unwinds one level
11362306a36Sopenharmony_ci * - ERROR         : restore from level 0 and reset
11462306a36Sopenharmony_ci * - ERROR_SPECIAL : restore from current level and reset
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci * Normal errors use ERROR, that is, they restore the initial fault context
11762306a36Sopenharmony_ci * and trigger a fault. However, there is a special case for linear mapping
11862306a36Sopenharmony_ci * errors. Those should basically never happen, but if they do happen, we
11962306a36Sopenharmony_ci * want the error to point out the context that did that linear mapping
12062306a36Sopenharmony_ci * fault, not the initial level 0 (basically, we got a bogus PGF or something
12162306a36Sopenharmony_ci * like that). For userland errors on the linear mapping, there is no
12262306a36Sopenharmony_ci * difference since those are always level 0 anyway
12362306a36Sopenharmony_ci */
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci#define TLB_MISS_RESTORE(freg)						    \
12662306a36Sopenharmony_ci	ld	r14,EX_TLB_CR(r12);					    \
12762306a36Sopenharmony_ci	ld	r10,EX_TLB_R10(r12);					    \
12862306a36Sopenharmony_ci	ld	r15,EX_TLB_SRR0(r12);					    \
12962306a36Sopenharmony_ci	ld	r16,EX_TLB_SRR1(r12);					    \
13062306a36Sopenharmony_ci	mtspr	SPRN_SPRG_TLB_EXFRAME,freg;				    \
13162306a36Sopenharmony_ci	ld	r11,EX_TLB_R11(r12);					    \
13262306a36Sopenharmony_ci	mtcr	r14;							    \
13362306a36Sopenharmony_ci	ld	r13,EX_TLB_R13(r12);					    \
13462306a36Sopenharmony_ci	ld	r14,EX_TLB_R14(r12);					    \
13562306a36Sopenharmony_ci	mtspr	SPRN_SRR0,r15;						    \
13662306a36Sopenharmony_ci	ld	r15,EX_TLB_R15(r12);					    \
13762306a36Sopenharmony_ci	mtspr	SPRN_SRR1,r16;						    \
13862306a36Sopenharmony_ci	ld	r16,EX_TLB_R16(r12);					    \
13962306a36Sopenharmony_ci	ld	r12,EX_TLB_R12(r12);					    \
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci#define TLB_MISS_EPILOG_SUCCESS						    \
14262306a36Sopenharmony_ci	TLB_MISS_RESTORE(r12)
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci#define TLB_MISS_EPILOG_ERROR						    \
14562306a36Sopenharmony_ci	addi	r12,r13,PACA_EXTLB;					    \
14662306a36Sopenharmony_ci	TLB_MISS_RESTORE(r12)
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci#define TLB_MISS_EPILOG_ERROR_SPECIAL					    \
14962306a36Sopenharmony_ci	addi	r11,r13,PACA_EXTLB;					    \
15062306a36Sopenharmony_ci	TLB_MISS_RESTORE(r11)
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci#ifndef __ASSEMBLY__
15362306a36Sopenharmony_ciextern unsigned int interrupt_base_book3e;
15462306a36Sopenharmony_ci#endif
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci#define SET_IVOR(vector_number, vector_offset)	\
15762306a36Sopenharmony_ci	LOAD_REG_ADDR(r3,interrupt_base_book3e);\
15862306a36Sopenharmony_ci	ori	r3,r3,vector_offset@l;		\
15962306a36Sopenharmony_ci	mtspr	SPRN_IVOR##vector_number,r3;
16062306a36Sopenharmony_ci/*
16162306a36Sopenharmony_ci * powerpc relies on return from interrupt/syscall being context synchronising
16262306a36Sopenharmony_ci * (which rfi is) to support ARCH_HAS_MEMBARRIER_SYNC_CORE without additional
16362306a36Sopenharmony_ci * synchronisation instructions.
16462306a36Sopenharmony_ci */
16562306a36Sopenharmony_ci#define RFI_TO_KERNEL							\
16662306a36Sopenharmony_ci	rfi
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci#define RFI_TO_USER							\
16962306a36Sopenharmony_ci	rfi
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci#endif /* _ASM_POWERPC_EXCEPTION_64E_H */
17262306a36Sopenharmony_ci
173