blob: c84bffcf9f83b7e868d60c6a13439dce9cec537f [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>
Yann Gautiera205a5c2021-08-30 15:06:54 +020013#include <drivers/clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <dt-bindings/clock/stm32mp1-clks.h>
Yann Gautier68e6d692021-07-05 14:07:29 +020016#include <dt-bindings/soc/stm32mp15-tzc400.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <lib/mmio.h>
18
Yann Gautierde00f3e2020-08-20 16:36:07 +020019static unsigned int region_nb;
20
Yann Gautierf3bd87e2020-09-04 15:55:53 +020021static void init_tzc400_begin(unsigned int region0_attr)
Yann Gautierde00f3e2020-08-20 16:36:07 +020022{
23 tzc400_init(STM32MP1_TZC_BASE);
24 tzc400_disable_filters();
25
Yann Gautierf3bd87e2020-09-04 15:55:53 +020026 /* Region 0 set to cover all DRAM at 0xC000_0000 */
27 tzc400_configure_region0(region0_attr, 0);
28
Yann Gautierde00f3e2020-08-20 16:36:07 +020029 region_nb = 1U;
30}
31
32static void init_tzc400_end(unsigned int action)
33{
34 tzc400_set_action(action);
35 tzc400_enable_filters();
36}
37
38static void tzc400_add_region(unsigned long long region_base,
39 unsigned long long region_top, bool sec)
40{
41 unsigned int sec_attr;
42 unsigned int nsaid_permissions;
43
44 if (sec) {
45 sec_attr = TZC_REGION_S_RDWR;
46 nsaid_permissions = 0;
47 } else {
48 sec_attr = TZC_REGION_S_NONE;
49 nsaid_permissions = TZC_REGION_NSEC_ALL_ACCESS_RDWR;
50 }
51
52 tzc400_configure_region(STM32MP1_FILTER_BIT_ALL, region_nb, region_base,
53 region_top, sec_attr, nsaid_permissions);
54
55 region_nb++;
56}
57
Yann Gautiercaf575b2018-07-24 17:18:19 +020058/*******************************************************************************
Yann Gautier9d135e42018-07-16 19:36:06 +020059 * Initialize the TrustZone Controller. Configure Region 0 with Secure RW access
60 * and allow Non-Secure masters full access.
61 ******************************************************************************/
62static void init_tzc400(void)
63{
64 unsigned long long region_base, region_top;
Yann Gautiera2e2a302019-02-14 11:13:39 +010065 unsigned long long ddr_base = STM32MP_DDR_BASE;
Yann Gautiercd40f322020-02-26 13:36:07 +010066 unsigned long long ddr_ns_size =
67 (unsigned long long)stm32mp_get_ddr_ns_size();
68 unsigned long long ddr_ns_top = ddr_base + (ddr_ns_size - 1U);
Yann Gautierde00f3e2020-08-20 16:36:07 +020069 unsigned long long ddr_top __unused;
Yann Gautier9d135e42018-07-16 19:36:06 +020070
Yann Gautierf3bd87e2020-09-04 15:55:53 +020071 init_tzc400_begin(TZC_REGION_S_NONE);
Yann Gautier9d135e42018-07-16 19:36:06 +020072
Yann Gautierb3386f72019-04-19 09:41:01 +020073 /*
74 * Region 1 set to cover all non-secure DRAM at 0xC000_0000. Apply the
75 * same configuration to all filters in the TZC.
76 */
77 region_base = ddr_base;
Yann Gautiercd40f322020-02-26 13:36:07 +010078 region_top = ddr_ns_top;
Yann Gautierde00f3e2020-08-20 16:36:07 +020079 tzc400_add_region(region_base, region_top, false);
Yann Gautierb3386f72019-04-19 09:41:01 +020080
Yann Gautiercd40f322020-02-26 13:36:07 +010081#ifdef AARCH32_SP_OPTEE
Yann Gautierb3386f72019-04-19 09:41:01 +020082 /* Region 2 set to cover all secure DRAM. */
83 region_base = region_top + 1U;
Yann Gautiercd40f322020-02-26 13:36:07 +010084 region_top += STM32MP_DDR_S_SIZE;
Yann Gautierde00f3e2020-08-20 16:36:07 +020085 tzc400_add_region(region_base, region_top, true);
Yann Gautierb3386f72019-04-19 09:41:01 +020086
Yann Gautierde00f3e2020-08-20 16:36:07 +020087 ddr_top = STM32MP_DDR_BASE + dt_get_ddr_size() - 1U;
88 if (region_top < ddr_top) {
89 /* Region 3 set to cover non-secure memory DRAM after BL32. */
90 region_base = region_top + 1U;
91 region_top = ddr_top;
92 tzc400_add_region(region_base, region_top, false);
93 }
Yann Gautierb3386f72019-04-19 09:41:01 +020094#endif
Yann Gautier9d135e42018-07-16 19:36:06 +020095
Yann Gautierde00f3e2020-08-20 16:36:07 +020096 /*
97 * Raise an interrupt (secure FIQ) if a NS device tries to access
98 * secure memory
99 */
100 init_tzc400_end(TZC_ACTION_INT);
Yann Gautier9d135e42018-07-16 19:36:06 +0200101}
102
103/*******************************************************************************
Yann Gautiercaf575b2018-07-24 17:18:19 +0200104 * Initialize the TrustZone Controller.
105 * Early initialization create only one region with full access to secure.
106 * This setting is used before and during DDR initialization.
107 ******************************************************************************/
108static void early_init_tzc400(void)
109{
Yann Gautiera205a5c2021-08-30 15:06:54 +0200110 clk_enable(TZC1);
111 clk_enable(TZC2);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200112
Yann Gautierf3bd87e2020-09-04 15:55:53 +0200113 /* Region 0 set to cover all DRAM secure at 0xC000_0000 */
114 init_tzc400_begin(TZC_REGION_S_RDWR);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200115
116 /* Raise an exception if a NS device tries to access secure memory */
Yann Gautierde00f3e2020-08-20 16:36:07 +0200117 init_tzc400_end(TZC_ACTION_ERR);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200118}
119
120/*******************************************************************************
121 * Initialize the secure environment. At this moment only the TrustZone
122 * Controller is initialized.
123 ******************************************************************************/
124void stm32mp1_arch_security_setup(void)
125{
126 early_init_tzc400();
127}
Yann Gautier9d135e42018-07-16 19:36:06 +0200128
129/*******************************************************************************
130 * Initialize the secure environment. At this moment only the TrustZone
131 * Controller is initialized.
132 ******************************************************************************/
133void stm32mp1_security_setup(void)
134{
135 init_tzc400();
136}