blob: b86402179a2ec12979b43af1d40db18ebf39993d [file] [log] [blame]
Sumit Garg82d45c12018-06-15 13:41:59 +05301/*
Antonio Nino Diazcbccdbf2019-01-21 11:53:29 +00002 * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
Sumit Garg82d45c12018-06-15 13:41:59 +05303 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <assert.h>
8
9#include <platform_def.h>
10
Sumit Garg82d45c12018-06-15 13:41:59 +053011#include <arch.h>
12#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <common/bl_common.h>
14#include <common/debug.h>
15#include <drivers/arm/pl011.h>
16#include <lib/mmio.h>
Sumit Gargbda9d3c2018-06-15 14:50:19 +053017#include <sq_common.h>
Sumit Garg82d45c12018-06-15 13:41:59 +053018
Sumit Garg84711f92018-06-15 14:34:42 +053019static console_pl011_t console;
Sumit Garge8c5e872018-06-15 14:38:50 +053020static entry_point_info_t bl32_image_ep_info;
21static entry_point_info_t bl33_image_ep_info;
22
Ard Biesheuvelc0415c62018-12-29 19:44:35 +010023IMPORT_SYM(uintptr_t, __SPM_SHIM_EXCEPTIONS_START__, SPM_SHIM_EXCEPTIONS_START);
24IMPORT_SYM(uintptr_t, __SPM_SHIM_EXCEPTIONS_END__, SPM_SHIM_EXCEPTIONS_END);
25IMPORT_SYM(uintptr_t, __SPM_SHIM_EXCEPTIONS_LMA__, SPM_SHIM_EXCEPTIONS_LMA);
26
Sumit Garge8c5e872018-06-15 14:38:50 +053027entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
28{
29 assert(sec_state_is_valid(type));
30 return type == NON_SECURE ? &bl33_image_ep_info : &bl32_image_ep_info;
31}
32
33/*******************************************************************************
34 * Gets SPSR for BL32 entry
35 ******************************************************************************/
36uint32_t sq_get_spsr_for_bl32_entry(void)
37{
38 /*
39 * The Secure Payload Dispatcher service is responsible for
40 * setting the SPSR prior to entry into the BL32 image.
41 */
42 return 0;
43}
44
45/*******************************************************************************
46 * Gets SPSR for BL33 entry
47 ******************************************************************************/
48uint32_t sq_get_spsr_for_bl33_entry(void)
49{
50 unsigned long el_status;
51 unsigned int mode;
52 uint32_t spsr;
53
54 /* Figure out what mode we enter the non-secure world in */
55 el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
56 el_status &= ID_AA64PFR0_ELX_MASK;
57
58 mode = (el_status) ? MODE_EL2 : MODE_EL1;
59
60 spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
61 return spsr;
62}
Sumit Garg84711f92018-06-15 14:34:42 +053063
Antonio Nino Diaz1d1b4f62018-09-24 17:16:30 +010064void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
65 u_register_t arg2, u_register_t arg3)
Sumit Garg82d45c12018-06-15 13:41:59 +053066{
Sumit Garg84711f92018-06-15 14:34:42 +053067 /* Initialize the console to provide early debug support */
68 (void)console_pl011_register(PLAT_SQ_BOOT_UART_BASE,
69 PLAT_SQ_BOOT_UART_CLK_IN_HZ,
70 SQ_CONSOLE_BAUDRATE, &console);
71
72 console_set_scope(&console.console, CONSOLE_FLAG_BOOT |
73 CONSOLE_FLAG_RUNTIME);
74
Sumit Garg82d45c12018-06-15 13:41:59 +053075 /* There are no parameters from BL2 if BL31 is a reset vector */
Antonio Nino Diaz1d1b4f62018-09-24 17:16:30 +010076 assert(arg0 == 0U);
77 assert(arg1 == 0U);
Sumit Garge8c5e872018-06-15 14:38:50 +053078
Sumit Garge11f87b2018-07-19 18:05:50 +053079 /* Initialize power controller before setting up topology */
80 plat_sq_pwrc_setup();
81
Ard Biesheuvel18498352018-12-29 19:40:31 +010082#ifdef SPD_opteed
Sumit Garge11f87b2018-07-19 18:05:50 +053083 struct draminfo di = {0};
84
Masahisa Kojimaebfd8eb2019-03-07 10:41:54 +090085 sq_scp_get_draminfo(&di);
Sumit Garge11f87b2018-07-19 18:05:50 +053086
87 /*
88 * Check if OP-TEE has been loaded in Secure RAM allocated
89 * from DRAM1 region
90 */
91 if ((di.base1 + di.size1) <= BL32_BASE) {
92 NOTICE("OP-TEE has been loaded by SCP firmware\n");
93 /* Populate entry point information for BL32 */
94 SET_PARAM_HEAD(&bl32_image_ep_info,
95 PARAM_EP,
96 VERSION_1,
97 0);
98 SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
99 bl32_image_ep_info.pc = BL32_BASE;
100 bl32_image_ep_info.spsr = sq_get_spsr_for_bl32_entry();
101 } else {
102 NOTICE("OP-TEE has not been loaded by SCP firmware\n");
103 }
Ard Biesheuvel18498352018-12-29 19:40:31 +0100104#endif /* SPD_opteed */
Sumit Garge8c5e872018-06-15 14:38:50 +0530105
106 /* Populate entry point information for BL33 */
107 SET_PARAM_HEAD(&bl33_image_ep_info,
108 PARAM_EP,
109 VERSION_1,
110 0);
111 /*
112 * Tell BL31 where the non-trusted software image
113 * is located and the entry state information
114 */
115 bl33_image_ep_info.pc = PRELOADED_BL33_BASE;
116 bl33_image_ep_info.spsr = sq_get_spsr_for_bl33_entry();
117 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
Sumit Garg82d45c12018-06-15 13:41:59 +0530118}
119
Sumit Garg58ed23d2018-06-15 15:02:31 +0530120static void sq_configure_sys_timer(void)
121{
122 unsigned int reg_val;
123
124 reg_val = (1 << CNTACR_RPCT_SHIFT) | (1 << CNTACR_RVCT_SHIFT);
125 reg_val |= (1 << CNTACR_RFRQ_SHIFT) | (1 << CNTACR_RVOFF_SHIFT);
126 reg_val |= (1 << CNTACR_RWVT_SHIFT) | (1 << CNTACR_RWPT_SHIFT);
127 mmio_write_32(SQ_SYS_TIMCTL_BASE +
128 CNTACR_BASE(PLAT_SQ_NSTIMER_FRAME_ID), reg_val);
129
130 reg_val = (1 << CNTNSAR_NS_SHIFT(PLAT_SQ_NSTIMER_FRAME_ID));
131 mmio_write_32(SQ_SYS_TIMCTL_BASE + CNTNSAR, reg_val);
132}
133
Sumit Garg82d45c12018-06-15 13:41:59 +0530134void bl31_platform_setup(void)
135{
Sumit Gargbda9d3c2018-06-15 14:50:19 +0530136 /* Initialize the CCN interconnect */
137 plat_sq_interconnect_init();
138 plat_sq_interconnect_enter_coherency();
Sumit Gargc412c2c2018-06-15 14:58:25 +0530139
140 /* Initialize the GIC driver, cpu and distributor interfaces */
141 sq_gic_driver_init();
142 sq_gic_init();
Sumit Garg58ed23d2018-06-15 15:02:31 +0530143
144 /* Enable and initialize the System level generic timer */
145 mmio_write_32(SQ_SYS_CNTCTL_BASE + CNTCR_OFF,
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100146 CNTCR_FCREQ(0U) | CNTCR_EN);
Sumit Garg58ed23d2018-06-15 15:02:31 +0530147
148 /* Allow access to the System counter timer module */
149 sq_configure_sys_timer();
Sumit Garg82d45c12018-06-15 13:41:59 +0530150}
151
152void bl31_plat_runtime_setup(void)
153{
Ard Biesheuvel6fc122f2018-06-15 15:25:42 +0530154 struct draminfo *di = (struct draminfo *)(unsigned long)DRAMINFO_BASE;
155
Masahisa Kojimaebfd8eb2019-03-07 10:41:54 +0900156 sq_scp_get_draminfo(di);
Sumit Garg82d45c12018-06-15 13:41:59 +0530157}
158
159void bl31_plat_arch_setup(void)
160{
Ard Biesheuvelc0415c62018-12-29 19:44:35 +0100161 static const mmap_region_t secure_partition_mmap[] = {
Paul Beesleyfe975b42019-09-16 11:29:03 +0000162#if SPM_MM
Ard Biesheuvelc0415c62018-12-29 19:44:35 +0100163 MAP_REGION_FLAT(PLAT_SPM_BUF_BASE,
164 PLAT_SPM_BUF_SIZE,
165 MT_RW_DATA | MT_SECURE),
166 MAP_REGION_FLAT(PLAT_SQ_SP_PRIV_BASE,
167 PLAT_SQ_SP_PRIV_SIZE,
168 MT_RW_DATA | MT_SECURE),
169#endif
170 {0},
171 };
172
173 sq_mmap_setup(BL31_BASE, BL31_SIZE, secure_partition_mmap);
Sumit Garg470255b2018-06-15 15:10:16 +0530174 enable_mmu_el3(XLAT_TABLE_NC);
Ard Biesheuvelc0415c62018-12-29 19:44:35 +0100175
Paul Beesleyfe975b42019-09-16 11:29:03 +0000176#if SPM_MM
Ard Biesheuvelc0415c62018-12-29 19:44:35 +0100177 memcpy((void *)SPM_SHIM_EXCEPTIONS_START,
178 (void *)SPM_SHIM_EXCEPTIONS_LMA,
179 (uintptr_t)SPM_SHIM_EXCEPTIONS_END -
180 (uintptr_t)SPM_SHIM_EXCEPTIONS_START);
181#endif
Sumit Garg470255b2018-06-15 15:10:16 +0530182}
183
184void bl31_plat_enable_mmu(uint32_t flags)
185{
186 enable_mmu_el3(flags | XLAT_TABLE_NC);
Sumit Garg82d45c12018-06-15 13:41:59 +0530187}
Sumit Garg58ed23d2018-06-15 15:02:31 +0530188
189unsigned int plat_get_syscnt_freq2(void)
190{
191 unsigned int counter_base_frequency;
192
193 /* Read the frequency from Frequency modes table */
194 counter_base_frequency = mmio_read_32(SQ_SYS_CNTCTL_BASE + CNTFID_OFF);
195
196 /* The first entry of the frequency modes table must not be 0 */
197 if (counter_base_frequency == 0)
198 panic();
199
200 return counter_base_frequency;
201}