stm32mp1_security.c 4.44 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2021, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdint.h>
8
9
10
11
12
13
14
15
16

#include <platform_def.h>

#include <common/debug.h>
#include <drivers/arm/tzc400.h>
#include <drivers/st/stm32mp1_clk.h>
#include <dt-bindings/clock/stm32mp1-clks.h>
#include <lib/mmio.h>

17
18
19
20
21
22
23
24
25
26
27
28
29
#define TZC_REGION_NSEC_ALL_ACCESS_RDWR \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_A7_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_GPU_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_LCD_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_MDMA_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_M4_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_DMA_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_USB_HOST_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_USB_OTG_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_SDMMC_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_ETH_ID) | \
	TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_DAP_ID)

30
31
static unsigned int region_nb;

32
static void init_tzc400_begin(unsigned int region0_attr)
33
34
35
36
{
	tzc400_init(STM32MP1_TZC_BASE);
	tzc400_disable_filters();

37
38
39
	/* Region 0 set to cover all DRAM at 0xC000_0000 */
	tzc400_configure_region0(region0_attr, 0);

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	region_nb = 1U;
}

static void init_tzc400_end(unsigned int action)
{
	tzc400_set_action(action);
	tzc400_enable_filters();
}

static void tzc400_add_region(unsigned long long region_base,
			      unsigned long long region_top, bool sec)
{
	unsigned int sec_attr;
	unsigned int nsaid_permissions;

	if (sec) {
		sec_attr = TZC_REGION_S_RDWR;
		nsaid_permissions = 0;
	} else {
		sec_attr = TZC_REGION_S_NONE;
		nsaid_permissions = TZC_REGION_NSEC_ALL_ACCESS_RDWR;
	}

	tzc400_configure_region(STM32MP1_FILTER_BIT_ALL, region_nb, region_base,
				region_top, sec_attr, nsaid_permissions);

	region_nb++;
}

69
70
71
72
73
74
75
/*******************************************************************************
 * Initialize the TrustZone Controller. Configure Region 0 with Secure RW access
 * and allow Non-Secure masters full access.
 ******************************************************************************/
static void init_tzc400(void)
{
	unsigned long long region_base, region_top;
76
	unsigned long long ddr_base = STM32MP_DDR_BASE;
77
78
79
	unsigned long long ddr_ns_size =
		(unsigned long long)stm32mp_get_ddr_ns_size();
	unsigned long long ddr_ns_top = ddr_base + (ddr_ns_size - 1U);
80
	unsigned long long ddr_top __unused;
81

82
	init_tzc400_begin(TZC_REGION_S_NONE);
83

Yann Gautier's avatar
Yann Gautier committed
84
85
86
87
88
	/*
	 * Region 1 set to cover all non-secure DRAM at 0xC000_0000. Apply the
	 * same configuration to all filters in the TZC.
	 */
	region_base = ddr_base;
89
	region_top = ddr_ns_top;
90
	tzc400_add_region(region_base, region_top, false);
Yann Gautier's avatar
Yann Gautier committed
91

92
#ifdef AARCH32_SP_OPTEE
Yann Gautier's avatar
Yann Gautier committed
93
94
	/* Region 2 set to cover all secure DRAM. */
	region_base = region_top + 1U;
95
	region_top += STM32MP_DDR_S_SIZE;
96
97
98
99
100
101
102
103
104
	tzc400_add_region(region_base, region_top, true);

	ddr_top = STM32MP_DDR_BASE + dt_get_ddr_size() - 1U;
	if (region_top < ddr_top) {
		/* Region 3 set to cover non-secure memory DRAM after BL32. */
		region_base = region_top + 1U;
		region_top = ddr_top;
		tzc400_add_region(region_base, region_top, false);
	}
Yann Gautier's avatar
Yann Gautier committed
105
#endif
106

107
108
109
110
111
	/*
	 * Raise an interrupt (secure FIQ) if a NS device tries to access
	 * secure memory
	 */
	init_tzc400_end(TZC_ACTION_INT);
112
113
}

114
115
116
117
118
119
120
/*******************************************************************************
 * Initialize the TrustZone Controller.
 * Early initialization create only one region with full access to secure.
 * This setting is used before and during DDR initialization.
 ******************************************************************************/
static void early_init_tzc400(void)
{
Yann Gautier's avatar
Yann Gautier committed
121
122
	stm32mp_clk_enable(TZC1);
	stm32mp_clk_enable(TZC2);
123

124
125
	/* Region 0 set to cover all DRAM secure at 0xC000_0000 */
	init_tzc400_begin(TZC_REGION_S_RDWR);
126
127

	/* Raise an exception if a NS device tries to access secure memory */
128
	init_tzc400_end(TZC_ACTION_ERR);
129
130
131
132
133
134
135
136
137
138
}

/*******************************************************************************
 * Initialize the secure environment. At this moment only the TrustZone
 * Controller is initialized.
 ******************************************************************************/
void stm32mp1_arch_security_setup(void)
{
	early_init_tzc400();
}
139
140
141
142
143
144
145
146
147

/*******************************************************************************
 * Initialize the secure environment. At this moment only the TrustZone
 * Controller is initialized.
 ******************************************************************************/
void stm32mp1_security_setup(void)
{
	init_tzc400();
}