blob: 19ef4f0aee77adccc6f70efcaebad0bc003f1bb8 [file] [log] [blame]
Yann Gautiercaf575b2018-07-24 17:18:19 +02001/*
Yann Gautierd7820562019-04-25 13:29:12 +02002 * Copyright (c) 2015-2021, ARM Limited and Contributors. All rights reserved.
Yann Gautiercaf575b2018-07-24 17:18:19 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautiercaf575b2018-07-24 17:18:19 +02007#include <stdint.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
9#include <platform_def.h>
10
11#include <common/debug.h>
12#include <drivers/arm/tzc400.h>
13#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <dt-bindings/clock/stm32mp1-clks.h>
Yann Gautier68e6d692021-07-05 14:07:29 +020015#include <dt-bindings/soc/stm32mp15-tzc400.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <lib/mmio.h>
17
Yann Gautierde00f3e2020-08-20 16:36:07 +020018static unsigned int region_nb;
19
Yann Gautierf3bd87e2020-09-04 15:55:53 +020020static void init_tzc400_begin(unsigned int region0_attr)
Yann Gautierde00f3e2020-08-20 16:36:07 +020021{
22 tzc400_init(STM32MP1_TZC_BASE);
23 tzc400_disable_filters();
24
Yann Gautierf3bd87e2020-09-04 15:55:53 +020025 /* Region 0 set to cover all DRAM at 0xC000_0000 */
26 tzc400_configure_region0(region0_attr, 0);
27
Yann Gautierde00f3e2020-08-20 16:36:07 +020028 region_nb = 1U;
29}
30
31static void init_tzc400_end(unsigned int action)
32{
33 tzc400_set_action(action);
34 tzc400_enable_filters();
35}
36
37static void tzc400_add_region(unsigned long long region_base,
38 unsigned long long region_top, bool sec)
39{
40 unsigned int sec_attr;
41 unsigned int nsaid_permissions;
42
43 if (sec) {
44 sec_attr = TZC_REGION_S_RDWR;
45 nsaid_permissions = 0;
46 } else {
47 sec_attr = TZC_REGION_S_NONE;
48 nsaid_permissions = TZC_REGION_NSEC_ALL_ACCESS_RDWR;
49 }
50
51 tzc400_configure_region(STM32MP1_FILTER_BIT_ALL, region_nb, region_base,
52 region_top, sec_attr, nsaid_permissions);
53
54 region_nb++;
55}
56
Yann Gautiercaf575b2018-07-24 17:18:19 +020057/*******************************************************************************
Yann Gautier9d135e42018-07-16 19:36:06 +020058 * Initialize the TrustZone Controller. Configure Region 0 with Secure RW access
59 * and allow Non-Secure masters full access.
60 ******************************************************************************/
61static void init_tzc400(void)
62{
63 unsigned long long region_base, region_top;
Yann Gautiera2e2a302019-02-14 11:13:39 +010064 unsigned long long ddr_base = STM32MP_DDR_BASE;
Yann Gautiercd40f322020-02-26 13:36:07 +010065 unsigned long long ddr_ns_size =
66 (unsigned long long)stm32mp_get_ddr_ns_size();
67 unsigned long long ddr_ns_top = ddr_base + (ddr_ns_size - 1U);
Yann Gautierde00f3e2020-08-20 16:36:07 +020068 unsigned long long ddr_top __unused;
Yann Gautier9d135e42018-07-16 19:36:06 +020069
Yann Gautierf3bd87e2020-09-04 15:55:53 +020070 init_tzc400_begin(TZC_REGION_S_NONE);
Yann Gautier9d135e42018-07-16 19:36:06 +020071
Yann Gautierb3386f72019-04-19 09:41:01 +020072 /*
73 * Region 1 set to cover all non-secure DRAM at 0xC000_0000. Apply the
74 * same configuration to all filters in the TZC.
75 */
76 region_base = ddr_base;
Yann Gautiercd40f322020-02-26 13:36:07 +010077 region_top = ddr_ns_top;
Yann Gautierde00f3e2020-08-20 16:36:07 +020078 tzc400_add_region(region_base, region_top, false);
Yann Gautierb3386f72019-04-19 09:41:01 +020079
Yann Gautiercd40f322020-02-26 13:36:07 +010080#ifdef AARCH32_SP_OPTEE
Yann Gautierb3386f72019-04-19 09:41:01 +020081 /* Region 2 set to cover all secure DRAM. */
82 region_base = region_top + 1U;
Yann Gautiercd40f322020-02-26 13:36:07 +010083 region_top += STM32MP_DDR_S_SIZE;
Yann Gautierde00f3e2020-08-20 16:36:07 +020084 tzc400_add_region(region_base, region_top, true);
Yann Gautierb3386f72019-04-19 09:41:01 +020085
Yann Gautierde00f3e2020-08-20 16:36:07 +020086 ddr_top = STM32MP_DDR_BASE + dt_get_ddr_size() - 1U;
87 if (region_top < ddr_top) {
88 /* Region 3 set to cover non-secure memory DRAM after BL32. */
89 region_base = region_top + 1U;
90 region_top = ddr_top;
91 tzc400_add_region(region_base, region_top, false);
92 }
Yann Gautierb3386f72019-04-19 09:41:01 +020093#endif
Yann Gautier9d135e42018-07-16 19:36:06 +020094
Yann Gautierde00f3e2020-08-20 16:36:07 +020095 /*
96 * Raise an interrupt (secure FIQ) if a NS device tries to access
97 * secure memory
98 */
99 init_tzc400_end(TZC_ACTION_INT);
Yann Gautier9d135e42018-07-16 19:36:06 +0200100}
101
102/*******************************************************************************
Yann Gautiercaf575b2018-07-24 17:18:19 +0200103 * Initialize the TrustZone Controller.
104 * Early initialization create only one region with full access to secure.
105 * This setting is used before and during DDR initialization.
106 ******************************************************************************/
107static void early_init_tzc400(void)
108{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100109 stm32mp_clk_enable(TZC1);
110 stm32mp_clk_enable(TZC2);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200111
Yann Gautierf3bd87e2020-09-04 15:55:53 +0200112 /* Region 0 set to cover all DRAM secure at 0xC000_0000 */
113 init_tzc400_begin(TZC_REGION_S_RDWR);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200114
115 /* Raise an exception if a NS device tries to access secure memory */
Yann Gautierde00f3e2020-08-20 16:36:07 +0200116 init_tzc400_end(TZC_ACTION_ERR);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200117}
118
119/*******************************************************************************
120 * Initialize the secure environment. At this moment only the TrustZone
121 * Controller is initialized.
122 ******************************************************************************/
123void stm32mp1_arch_security_setup(void)
124{
125 early_init_tzc400();
126}
Yann Gautier9d135e42018-07-16 19:36:06 +0200127
128/*******************************************************************************
129 * Initialize the secure environment. At this moment only the TrustZone
130 * Controller is initialized.
131 ******************************************************************************/
132void stm32mp1_security_setup(void)
133{
134 init_tzc400();
135}