xref: /rk3399_ARM-atf/include/lib/xlat_tables/aarch64/xlat_tables_aarch64.h (revision 72e8f2456af54b75a0a1d92aadfce0b4bcde6ba1)
18933c34bSSandrine Bailleux /*
2*4c700c15SGovindraj Raja  * Copyright (c) 2017-2018, Arm Limited and Contributors. All rights reserved.
38933c34bSSandrine Bailleux  *
48933c34bSSandrine Bailleux  * SPDX-License-Identifier: BSD-3-Clause
58933c34bSSandrine Bailleux  */
68933c34bSSandrine Bailleux 
7e7b9886cSAntonio Nino Diaz #ifndef XLAT_TABLES_AARCH64_H
8e7b9886cSAntonio Nino Diaz #define XLAT_TABLES_AARCH64_H
98933c34bSSandrine Bailleux 
108933c34bSSandrine Bailleux #include <arch.h>
1109d40e0eSAntonio Nino Diaz #include <lib/utils_def.h>
1209d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_defs.h>
138933c34bSSandrine Bailleux 
148933c34bSSandrine Bailleux #if !defined(PAGE_SIZE)
158933c34bSSandrine Bailleux #error "PAGE_SIZE is not defined."
168933c34bSSandrine Bailleux #endif
178933c34bSSandrine Bailleux 
188933c34bSSandrine Bailleux /*
19ad02a759SAntonio Nino Diaz  * Encode a Physical Address Space size for its use in TCR_ELx.
20ad02a759SAntonio Nino Diaz  */
21ad02a759SAntonio Nino Diaz unsigned long long tcr_physical_addr_size_bits(unsigned long long max_addr);
22ad02a759SAntonio Nino Diaz 
23ad02a759SAntonio Nino Diaz /*
248933c34bSSandrine Bailleux  * In AArch64 state, the MMU may support 4 KB, 16 KB and 64 KB page
258933c34bSSandrine Bailleux  * granularity. For 4KB granularity, a level 0 table descriptor doesn't support
268933c34bSSandrine Bailleux  * block translation. For 16KB, the same thing happens to levels 0 and 1. For
278933c34bSSandrine Bailleux  * 64KB, same for level 1. See section D4.3.1 of the ARMv8-A Architecture
288933c34bSSandrine Bailleux  * Reference Manual (DDI 0487A.k) for more information.
298933c34bSSandrine Bailleux  *
308933c34bSSandrine Bailleux  * The define below specifies the first table level that allows block
318933c34bSSandrine Bailleux  * descriptors.
328933c34bSSandrine Bailleux  */
33e7b9886cSAntonio Nino Diaz #if PAGE_SIZE == PAGE_SIZE_4KB
340044231dSSandrine Bailleux # define MIN_LVL_BLOCK_DESC	U(1)
35e7b9886cSAntonio Nino Diaz #elif (PAGE_SIZE == PAGE_SIZE_16KB) || (PAGE_SIZE == PAGE_SIZE_64KB)
360044231dSSandrine Bailleux # define MIN_LVL_BLOCK_DESC	U(2)
378933c34bSSandrine Bailleux #endif
388933c34bSSandrine Bailleux 
398933c34bSSandrine Bailleux #define XLAT_TABLE_LEVEL_MIN	U(0)
408933c34bSSandrine Bailleux 
418933c34bSSandrine Bailleux /*
428933c34bSSandrine Bailleux  * Define the architectural limits of the virtual address space in AArch64
438933c34bSSandrine Bailleux  * state.
448933c34bSSandrine Bailleux  *
458933c34bSSandrine Bailleux  * TCR.TxSZ is calculated as 64 minus the width of said address space.
46cedfa04bSSathees Balya  * The value of TCR.TxSZ must be in the range 16 to 39 [1] or 48 [2],
47cedfa04bSSathees Balya  * depending on Small Translation Table Support which means that
48cedfa04bSSathees Balya  * the virtual address space width must be in the range 48 to 25 or 16 bits.
498933c34bSSandrine Bailleux  *
508933c34bSSandrine Bailleux  * [1] See the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more
518933c34bSSandrine Bailleux  * information:
528933c34bSSandrine Bailleux  * Page 1730: 'Input address size', 'For all translation stages'.
53cedfa04bSSathees Balya  * [2] See section 12.2.55 in the ARMv8-A Architecture Reference Manual
54cedfa04bSSathees Balya  * (DDI 0487D.a)
558933c34bSSandrine Bailleux  */
56cedfa04bSSathees Balya /* Maximum value of TCR_ELx.T(0,1)SZ is 39 */
57e7b9886cSAntonio Nino Diaz #define MIN_VIRT_ADDR_SPACE_SIZE	(ULL(1) << (U(64) - TCR_TxSZ_MAX))
58cedfa04bSSathees Balya 
59cedfa04bSSathees Balya /* Maximum value of TCR_ELx.T(0,1)SZ is 48 */
60cedfa04bSSathees Balya #define MIN_VIRT_ADDR_SPACE_SIZE_TTST	\
61cedfa04bSSathees Balya 				(ULL(1) << (U(64) - TCR_TxSZ_MAX_TTST))
62e7b9886cSAntonio Nino Diaz #define MAX_VIRT_ADDR_SPACE_SIZE	(ULL(1) << (U(64) - TCR_TxSZ_MIN))
638933c34bSSandrine Bailleux 
648933c34bSSandrine Bailleux /*
658933c34bSSandrine Bailleux  * Here we calculate the initial lookup level from the value of the given
668933c34bSSandrine Bailleux  * virtual address space size. For a 4 KB page size,
678933c34bSSandrine Bailleux  * - level 0 supports virtual address spaces of widths 48 to 40 bits;
688933c34bSSandrine Bailleux  * - level 1 from 39 to 31;
69cedfa04bSSathees Balya  * - level 2 from 30 to 22.
70cedfa04bSSathees Balya  * - level 3 from 21 to 16.
718933c34bSSandrine Bailleux  *
72cedfa04bSSathees Balya  * Small Translation Table (Armv8.4-TTST) support allows the starting level
73cedfa04bSSathees Balya  * of the translation table from 3 for 4KB granularity. See section 12.2.55 in
74cedfa04bSSathees Balya  * the ARMv8-A Architecture Reference Manual (DDI 0487D.a). In Armv8.3 and below
75cedfa04bSSathees Balya  * wider or narrower address spaces are not supported. As a result, level 3
768933c34bSSandrine Bailleux  * cannot be used as initial lookup level with 4 KB granularity. See section
778933c34bSSandrine Bailleux  * D4.2.5 in the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more
788933c34bSSandrine Bailleux  * information.
798933c34bSSandrine Bailleux  *
808933c34bSSandrine Bailleux  * For example, for a 35-bit address space (i.e. virt_addr_space_size ==
818933c34bSSandrine Bailleux  * 1 << 35), TCR.TxSZ will be programmed to (64 - 35) = 29. According to Table
828933c34bSSandrine Bailleux  * D4-11 in the ARM ARM, the initial lookup level for an address space like that
838933c34bSSandrine Bailleux  * is 1.
848933c34bSSandrine Bailleux  *
858933c34bSSandrine Bailleux  * Note that this macro assumes that the given virtual address space size is
86cedfa04bSSathees Balya  * valid.
878933c34bSSandrine Bailleux  */
88e7b9886cSAntonio Nino Diaz #define GET_XLAT_TABLE_LEVEL_BASE(_virt_addr_space_sz)		\
89e7b9886cSAntonio Nino Diaz 	(((_virt_addr_space_sz) > (ULL(1) << L0_XLAT_ADDRESS_SHIFT))	\
90e7b9886cSAntonio Nino Diaz 	? 0U								\
91e7b9886cSAntonio Nino Diaz 	: (((_virt_addr_space_sz) > (ULL(1) << L1_XLAT_ADDRESS_SHIFT))	\
92cedfa04bSSathees Balya 	? 1U								\
93cedfa04bSSathees Balya 	: (((_virt_addr_space_sz) > (ULL(1) << L2_XLAT_ADDRESS_SHIFT))	\
94cedfa04bSSathees Balya 	? 2U : 3U)))
958933c34bSSandrine Bailleux 
96e7b9886cSAntonio Nino Diaz #endif /* XLAT_TABLES_AARCH64_H */
97