2 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
4 * SPDX-License-Identifier: BSD-3-Clause
7 #include <platform_def.h>
9 #include <common/debug.h>
10 #include <drivers/cfi/v2m_flash.h>
11 #include <lib/psci/psci.h>
13 #include <lib/utils.h>
14 #include <plat/arm/common/plat_arm.h>
17 * DRAM1 is used also to load the NS boot loader. For this reason we
18 * cannot clear the full DRAM1, because in that case we would clear
19 * the NS images (especially for RESET_TO_BL31 and RESET_TO_SPMIN cases).
20 * For this reason we reserve 64 MB for the NS images and protect the RAM
21 * until the end of DRAM1.
22 * We limit the size of DRAM2 to 1 GB to avoid big delays while booting
24 #define DRAM1_NS_IMAGE_LIMIT (PLAT_ARM_NS_IMAGE_BASE + (32 << TWO_MB_SHIFT))
25 #define DRAM1_PROTECTED_SIZE (ARM_NS_DRAM1_END+1u - DRAM1_NS_IMAGE_LIMIT)
27 static mem_region_t arm_ram_ranges
[] = {
28 {DRAM1_NS_IMAGE_LIMIT
, DRAM1_PROTECTED_SIZE
},
30 {ARM_DRAM2_BASE
, 1u << ONE_GB_SHIFT
},
34 /*******************************************************************************
35 * Function that reads the content of the memory protect variable that
36 * enables clearing of non secure memory when system boots. This variable
37 * should be stored in a secure NVRAM.
38 ******************************************************************************/
39 int arm_psci_read_mem_protect(int *enabled
)
43 tmp
= *(int *) PLAT_ARM_MEM_PROT_ADDR
;
44 *enabled
= (tmp
== 1) ? 1 : 0;
48 /*******************************************************************************
49 * Function that writes the content of the memory protect variable that
50 * enables overwritten of non secure memory when system boots.
51 ******************************************************************************/
52 int arm_nor_psci_write_mem_protect(int val
)
54 unsigned long enable
= (val
!= 0) ? 1UL : 0UL;
56 if (nor_unlock(PLAT_ARM_MEM_PROT_ADDR
) != 0) {
57 ERROR("unlocking memory protect variable\n");
63 * If we want to write a value different than 0
64 * then we have to erase the full block because
65 * otherwise we cannot ensure that the value programmed
66 * into the flash is going to be the same than the value
67 * requested by the caller
69 if (nor_erase(PLAT_ARM_MEM_PROT_ADDR
) != 0) {
70 ERROR("erasing block containing memory protect variable\n");
75 if (nor_word_program(PLAT_ARM_MEM_PROT_ADDR
, enable
) != 0) {
76 ERROR("programming memory protection variable\n");
82 /*******************************************************************************
83 * Function used for required psci operations performed when
85 ******************************************************************************/
87 * PLAT_MEM_PROTECT_VA_FRAME is a address specifically
88 * selected in a way that is not needed an additional
89 * translation table for memprotect. It happens because
90 * we use a chunk of size 2MB and it means that it can
91 * be mapped in a level 2 table and the level 2 table
92 * for 0xc0000000 is already used and the entry for
93 * 0xc0000000 is not used.
95 #if defined(PLAT_XLAT_TABLES_DYNAMIC)
96 void arm_nor_psci_do_dyn_mem_protect(void)
100 arm_psci_read_mem_protect(&enable
);
104 INFO("PSCI: Overwriting non secure memory\n");
105 clear_map_dyn_mem_regions(arm_ram_ranges
,
106 ARRAY_SIZE(arm_ram_ranges
),
107 PLAT_ARM_MEM_PROTEC_VA_FRAME
,
112 /*******************************************************************************
113 * Function used for required psci operations performed when
114 * system boots and dynamic memory is not used.
115 ******************************************************************************/
116 void arm_nor_psci_do_static_mem_protect(void)
120 (void) arm_psci_read_mem_protect(&enable
);
124 INFO("PSCI: Overwriting non secure memory\n");
125 clear_mem_regions(arm_ram_ranges
,
126 ARRAY_SIZE(arm_ram_ranges
));
127 (void) arm_nor_psci_write_mem_protect(0);
130 /*******************************************************************************
131 * Function that checks if a region is protected by the memory protect
133 ******************************************************************************/
134 int arm_psci_mem_protect_chk(uintptr_t base
, u_register_t length
)
136 return mem_region_in_array_chk(arm_ram_ranges
,
137 ARRAY_SIZE(arm_ram_ranges
),