xref: /arm-trusted-firmware/include/lib/xlat_tables/aarch64/xlat_tables_aarch64.h (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2017-2018, Arm Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #ifndef XLAT_TABLES_AARCH64_H
8*91f16700Schasinglulu #define XLAT_TABLES_AARCH64_H
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <arch.h>
11*91f16700Schasinglulu #include <lib/utils_def.h>
12*91f16700Schasinglulu #include <lib/xlat_tables/xlat_tables_defs.h>
13*91f16700Schasinglulu 
14*91f16700Schasinglulu #if !defined(PAGE_SIZE)
15*91f16700Schasinglulu #error "PAGE_SIZE is not defined."
16*91f16700Schasinglulu #endif
17*91f16700Schasinglulu 
18*91f16700Schasinglulu /*
19*91f16700Schasinglulu  * Encode a Physical Address Space size for its use in TCR_ELx.
20*91f16700Schasinglulu  */
21*91f16700Schasinglulu unsigned long long tcr_physical_addr_size_bits(unsigned long long max_addr);
22*91f16700Schasinglulu 
23*91f16700Schasinglulu /*
24*91f16700Schasinglulu  * In AArch64 state, the MMU may support 4 KB, 16 KB and 64 KB page
25*91f16700Schasinglulu  * granularity. For 4KB granularity, a level 0 table descriptor doesn't support
26*91f16700Schasinglulu  * block translation. For 16KB, the same thing happens to levels 0 and 1. For
27*91f16700Schasinglulu  * 64KB, same for level 1. See section D4.3.1 of the ARMv8-A Architecture
28*91f16700Schasinglulu  * Reference Manual (DDI 0487A.k) for more information.
29*91f16700Schasinglulu  *
30*91f16700Schasinglulu  * The define below specifies the first table level that allows block
31*91f16700Schasinglulu  * descriptors.
32*91f16700Schasinglulu  */
33*91f16700Schasinglulu #if PAGE_SIZE == PAGE_SIZE_4KB
34*91f16700Schasinglulu # define MIN_LVL_BLOCK_DESC	U(1)
35*91f16700Schasinglulu #elif (PAGE_SIZE == PAGE_SIZE_16KB) || (PAGE_SIZE == PAGE_SIZE_64KB)
36*91f16700Schasinglulu # define MIN_LVL_BLOCK_DESC	U(2)
37*91f16700Schasinglulu #endif
38*91f16700Schasinglulu 
39*91f16700Schasinglulu #define XLAT_TABLE_LEVEL_MIN	U(0)
40*91f16700Schasinglulu 
41*91f16700Schasinglulu /*
42*91f16700Schasinglulu  * Define the architectural limits of the virtual address space in AArch64
43*91f16700Schasinglulu  * state.
44*91f16700Schasinglulu  *
45*91f16700Schasinglulu  * TCR.TxSZ is calculated as 64 minus the width of said address space.
46*91f16700Schasinglulu  * The value of TCR.TxSZ must be in the range 16 to 39 [1] or 48 [2],
47*91f16700Schasinglulu  * depending on Small Translation Table Support which means that
48*91f16700Schasinglulu  * the virtual address space width must be in the range 48 to 25 or 16 bits.
49*91f16700Schasinglulu  *
50*91f16700Schasinglulu  * [1] See the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more
51*91f16700Schasinglulu  * information:
52*91f16700Schasinglulu  * Page 1730: 'Input address size', 'For all translation stages'.
53*91f16700Schasinglulu  * [2] See section 12.2.55 in the ARMv8-A Architecture Reference Manual
54*91f16700Schasinglulu  * (DDI 0487D.a)
55*91f16700Schasinglulu  */
56*91f16700Schasinglulu /* Maximum value of TCR_ELx.T(0,1)SZ is 39 */
57*91f16700Schasinglulu #define MIN_VIRT_ADDR_SPACE_SIZE	(ULL(1) << (U(64) - TCR_TxSZ_MAX))
58*91f16700Schasinglulu 
59*91f16700Schasinglulu /* Maximum value of TCR_ELx.T(0,1)SZ is 48 */
60*91f16700Schasinglulu #define MIN_VIRT_ADDR_SPACE_SIZE_TTST	\
61*91f16700Schasinglulu 				(ULL(1) << (U(64) - TCR_TxSZ_MAX_TTST))
62*91f16700Schasinglulu #define MAX_VIRT_ADDR_SPACE_SIZE	(ULL(1) << (U(64) - TCR_TxSZ_MIN))
63*91f16700Schasinglulu 
64*91f16700Schasinglulu /*
65*91f16700Schasinglulu  * Here we calculate the initial lookup level from the value of the given
66*91f16700Schasinglulu  * virtual address space size. For a 4 KB page size,
67*91f16700Schasinglulu  * - level 0 supports virtual address spaces of widths 48 to 40 bits;
68*91f16700Schasinglulu  * - level 1 from 39 to 31;
69*91f16700Schasinglulu  * - level 2 from 30 to 22.
70*91f16700Schasinglulu  * - level 3 from 21 to 16.
71*91f16700Schasinglulu  *
72*91f16700Schasinglulu  * Small Translation Table (Armv8.4-TTST) support allows the starting level
73*91f16700Schasinglulu  * of the translation table from 3 for 4KB granularity. See section 12.2.55 in
74*91f16700Schasinglulu  * the ARMv8-A Architecture Reference Manual (DDI 0487D.a). In Armv8.3 and below
75*91f16700Schasinglulu  * wider or narrower address spaces are not supported. As a result, level 3
76*91f16700Schasinglulu  * cannot be used as initial lookup level with 4 KB granularity. See section
77*91f16700Schasinglulu  * D4.2.5 in the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more
78*91f16700Schasinglulu  * information.
79*91f16700Schasinglulu  *
80*91f16700Schasinglulu  * For example, for a 35-bit address space (i.e. virt_addr_space_size ==
81*91f16700Schasinglulu  * 1 << 35), TCR.TxSZ will be programmed to (64 - 35) = 29. According to Table
82*91f16700Schasinglulu  * D4-11 in the ARM ARM, the initial lookup level for an address space like that
83*91f16700Schasinglulu  * is 1.
84*91f16700Schasinglulu  *
85*91f16700Schasinglulu  * Note that this macro assumes that the given virtual address space size is
86*91f16700Schasinglulu  * valid.
87*91f16700Schasinglulu  */
88*91f16700Schasinglulu #define GET_XLAT_TABLE_LEVEL_BASE(_virt_addr_space_sz)		\
89*91f16700Schasinglulu 	(((_virt_addr_space_sz) > (ULL(1) << L0_XLAT_ADDRESS_SHIFT))	\
90*91f16700Schasinglulu 	? 0U								\
91*91f16700Schasinglulu 	: (((_virt_addr_space_sz) > (ULL(1) << L1_XLAT_ADDRESS_SHIFT))	\
92*91f16700Schasinglulu 	? 1U								\
93*91f16700Schasinglulu 	: (((_virt_addr_space_sz) > (ULL(1) << L2_XLAT_ADDRESS_SHIFT))	\
94*91f16700Schasinglulu 	? 2U : 3U)))
95*91f16700Schasinglulu 
96*91f16700Schasinglulu #endif /* XLAT_TABLES_AARCH64_H */
97