xref: /rk3399_ARM-atf/plat/arm/common/arm_common.c (revision 1af540ef2a09797c3a22c40c340facd4b2f47c2f)
1bc149bfcSSoby Mathew /*
2*1af540efSRoberto Vargas  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3bc149bfcSSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5bc149bfcSSoby Mathew  */
6bc149bfcSSoby Mathew #include <arch.h>
7bc149bfcSSoby Mathew #include <arch_helpers.h>
83b211ff5SAntonio Nino Diaz #include <arm_xlat_tables.h>
9bc149bfcSSoby Mathew #include <assert.h>
10bc149bfcSSoby Mathew #include <debug.h>
11bc149bfcSSoby Mathew #include <mmio.h>
12bc149bfcSSoby Mathew #include <plat_arm.h>
13bc149bfcSSoby Mathew #include <platform_def.h>
14*1af540efSRoberto Vargas #include <platform.h>
15e29efeb1SAntonio Nino Diaz #include <secure_partition.h>
16bc149bfcSSoby Mathew 
17bc149bfcSSoby Mathew extern const mmap_region_t plat_arm_mmap[];
18bc149bfcSSoby Mathew 
19bc149bfcSSoby Mathew /* Weak definitions may be overridden in specific ARM standard platform */
20bc149bfcSSoby Mathew #pragma weak plat_get_ns_image_entrypoint
21bc149bfcSSoby Mathew #pragma weak plat_arm_get_mmap
22bc149bfcSSoby Mathew 
23bc149bfcSSoby Mathew /* Conditionally provide a weak definition of plat_get_syscnt_freq2 to avoid
24bc149bfcSSoby Mathew  * conflicts with the definition in plat/common. */
25bc149bfcSSoby Mathew #if ERROR_DEPRECATED
26bc149bfcSSoby Mathew #pragma weak plat_get_syscnt_freq2
27bc149bfcSSoby Mathew #endif
28bc149bfcSSoby Mathew 
29bc149bfcSSoby Mathew /*
30bc149bfcSSoby Mathew  * Set up the page tables for the generic and platform-specific memory regions.
31bc149bfcSSoby Mathew  * The extents of the generic memory regions are specified by the function
32bc149bfcSSoby Mathew  * arguments and consist of:
33bc149bfcSSoby Mathew  * - Trusted SRAM seen by the BL image;
34bc149bfcSSoby Mathew  * - Code section;
35bc149bfcSSoby Mathew  * - Read-only data section;
36bc149bfcSSoby Mathew  * - Coherent memory region, if applicable.
37bc149bfcSSoby Mathew  */
38bc149bfcSSoby Mathew void arm_setup_page_tables(uintptr_t total_base,
39bc149bfcSSoby Mathew 			   size_t total_size,
40bc149bfcSSoby Mathew 			   uintptr_t code_start,
41bc149bfcSSoby Mathew 			   uintptr_t code_limit,
42bc149bfcSSoby Mathew 			   uintptr_t rodata_start,
43bc149bfcSSoby Mathew 			   uintptr_t rodata_limit
44bc149bfcSSoby Mathew #if USE_COHERENT_MEM
45bc149bfcSSoby Mathew 			   ,
46bc149bfcSSoby Mathew 			   uintptr_t coh_start,
47bc149bfcSSoby Mathew 			   uintptr_t coh_limit
48bc149bfcSSoby Mathew #endif
49bc149bfcSSoby Mathew 			   )
50bc149bfcSSoby Mathew {
51bc149bfcSSoby Mathew 	/*
52bc149bfcSSoby Mathew 	 * Map the Trusted SRAM with appropriate memory attributes.
53bc149bfcSSoby Mathew 	 * Subsequent mappings will adjust the attributes for specific regions.
54bc149bfcSSoby Mathew 	 */
55bc149bfcSSoby Mathew 	VERBOSE("Trusted SRAM seen by this BL image: %p - %p\n",
56bc149bfcSSoby Mathew 		(void *) total_base, (void *) (total_base + total_size));
57bc149bfcSSoby Mathew 	mmap_add_region(total_base, total_base,
58bc149bfcSSoby Mathew 			total_size,
59bc149bfcSSoby Mathew 			MT_MEMORY | MT_RW | MT_SECURE);
60bc149bfcSSoby Mathew 
61bc149bfcSSoby Mathew 	/* Re-map the code section */
62bc149bfcSSoby Mathew 	VERBOSE("Code region: %p - %p\n",
63bc149bfcSSoby Mathew 		(void *) code_start, (void *) code_limit);
64bc149bfcSSoby Mathew 	mmap_add_region(code_start, code_start,
65bc149bfcSSoby Mathew 			code_limit - code_start,
66bc149bfcSSoby Mathew 			MT_CODE | MT_SECURE);
67bc149bfcSSoby Mathew 
68bc149bfcSSoby Mathew 	/* Re-map the read-only data section */
69bc149bfcSSoby Mathew 	VERBOSE("Read-only data region: %p - %p\n",
70bc149bfcSSoby Mathew 		(void *) rodata_start, (void *) rodata_limit);
71bc149bfcSSoby Mathew 	mmap_add_region(rodata_start, rodata_start,
72bc149bfcSSoby Mathew 			rodata_limit - rodata_start,
73bc149bfcSSoby Mathew 			MT_RO_DATA | MT_SECURE);
74bc149bfcSSoby Mathew 
75bc149bfcSSoby Mathew #if USE_COHERENT_MEM
76bc149bfcSSoby Mathew 	/* Re-map the coherent memory region */
77bc149bfcSSoby Mathew 	VERBOSE("Coherent region: %p - %p\n",
78bc149bfcSSoby Mathew 		(void *) coh_start, (void *) coh_limit);
79bc149bfcSSoby Mathew 	mmap_add_region(coh_start, coh_start,
80bc149bfcSSoby Mathew 			coh_limit - coh_start,
81bc149bfcSSoby Mathew 			MT_DEVICE | MT_RW | MT_SECURE);
82bc149bfcSSoby Mathew #endif
83bc149bfcSSoby Mathew 
84e29efeb1SAntonio Nino Diaz #if ENABLE_SPM && defined(IMAGE_BL31)
85e29efeb1SAntonio Nino Diaz 	/* The address of the following region is calculated by the linker. */
86e29efeb1SAntonio Nino Diaz 	mmap_add_region(SP_IMAGE_XLAT_TABLES_START,
87e29efeb1SAntonio Nino Diaz 			SP_IMAGE_XLAT_TABLES_START,
88e29efeb1SAntonio Nino Diaz 			SP_IMAGE_XLAT_TABLES_SIZE,
89e29efeb1SAntonio Nino Diaz 			MT_MEMORY | MT_RW | MT_SECURE);
90e29efeb1SAntonio Nino Diaz #endif
91e29efeb1SAntonio Nino Diaz 
92bc149bfcSSoby Mathew 	/* Now (re-)map the platform-specific memory regions */
93bc149bfcSSoby Mathew 	mmap_add(plat_arm_get_mmap());
94bc149bfcSSoby Mathew 
95bc149bfcSSoby Mathew 	/* Create the page tables to reflect the above mappings */
96bc149bfcSSoby Mathew 	init_xlat_tables();
97bc149bfcSSoby Mathew }
98bc149bfcSSoby Mathew 
99bc149bfcSSoby Mathew uintptr_t plat_get_ns_image_entrypoint(void)
100bc149bfcSSoby Mathew {
10148ac1df9SSoby Mathew #ifdef PRELOADED_BL33_BASE
10248ac1df9SSoby Mathew 	return PRELOADED_BL33_BASE;
10348ac1df9SSoby Mathew #else
104bc149bfcSSoby Mathew 	return PLAT_ARM_NS_IMAGE_OFFSET;
10548ac1df9SSoby Mathew #endif
106bc149bfcSSoby Mathew }
107bc149bfcSSoby Mathew 
108bc149bfcSSoby Mathew /*******************************************************************************
109bc149bfcSSoby Mathew  * Gets SPSR for BL32 entry
110bc149bfcSSoby Mathew  ******************************************************************************/
111bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl32_entry(void)
112bc149bfcSSoby Mathew {
113bc149bfcSSoby Mathew 	/*
114bc149bfcSSoby Mathew 	 * The Secure Payload Dispatcher service is responsible for
115bc149bfcSSoby Mathew 	 * setting the SPSR prior to entry into the BL32 image.
116bc149bfcSSoby Mathew 	 */
117bc149bfcSSoby Mathew 	return 0;
118bc149bfcSSoby Mathew }
119bc149bfcSSoby Mathew 
120bc149bfcSSoby Mathew /*******************************************************************************
121bc149bfcSSoby Mathew  * Gets SPSR for BL33 entry
122bc149bfcSSoby Mathew  ******************************************************************************/
123877cf3ffSSoby Mathew #ifndef AARCH32
124bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl33_entry(void)
125bc149bfcSSoby Mathew {
126bc149bfcSSoby Mathew 	unsigned int mode;
127bc149bfcSSoby Mathew 	uint32_t spsr;
128bc149bfcSSoby Mathew 
129bc149bfcSSoby Mathew 	/* Figure out what mode we enter the non-secure world in */
130f4c8aa90SJeenu Viswambharan 	mode = EL_IMPLEMENTED(2) ? MODE_EL2 : MODE_EL1;
131bc149bfcSSoby Mathew 
132bc149bfcSSoby Mathew 	/*
133bc149bfcSSoby Mathew 	 * TODO: Consider the possibility of specifying the SPSR in
134bc149bfcSSoby Mathew 	 * the FIP ToC and allowing the platform to have a say as
135bc149bfcSSoby Mathew 	 * well.
136bc149bfcSSoby Mathew 	 */
137bc149bfcSSoby Mathew 	spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
138bc149bfcSSoby Mathew 	return spsr;
139bc149bfcSSoby Mathew }
140877cf3ffSSoby Mathew #else
141877cf3ffSSoby Mathew /*******************************************************************************
142877cf3ffSSoby Mathew  * Gets SPSR for BL33 entry
143877cf3ffSSoby Mathew  ******************************************************************************/
144877cf3ffSSoby Mathew uint32_t arm_get_spsr_for_bl33_entry(void)
145877cf3ffSSoby Mathew {
146877cf3ffSSoby Mathew 	unsigned int hyp_status, mode, spsr;
147877cf3ffSSoby Mathew 
148877cf3ffSSoby Mathew 	hyp_status = GET_VIRT_EXT(read_id_pfr1());
149877cf3ffSSoby Mathew 
150877cf3ffSSoby Mathew 	mode = (hyp_status) ? MODE32_hyp : MODE32_svc;
151877cf3ffSSoby Mathew 
152877cf3ffSSoby Mathew 	/*
153877cf3ffSSoby Mathew 	 * TODO: Consider the possibility of specifying the SPSR in
154877cf3ffSSoby Mathew 	 * the FIP ToC and allowing the platform to have a say as
155877cf3ffSSoby Mathew 	 * well.
156877cf3ffSSoby Mathew 	 */
157877cf3ffSSoby Mathew 	spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1,
158877cf3ffSSoby Mathew 			SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
159877cf3ffSSoby Mathew 	return spsr;
160877cf3ffSSoby Mathew }
161877cf3ffSSoby Mathew #endif /* AARCH32 */
162bc149bfcSSoby Mathew 
163bc149bfcSSoby Mathew /*******************************************************************************
164bc149bfcSSoby Mathew  * Configures access to the system counter timer module.
165bc149bfcSSoby Mathew  ******************************************************************************/
166bc149bfcSSoby Mathew #ifdef ARM_SYS_TIMCTL_BASE
167bc149bfcSSoby Mathew void arm_configure_sys_timer(void)
168bc149bfcSSoby Mathew {
169bc149bfcSSoby Mathew 	unsigned int reg_val;
170bc149bfcSSoby Mathew 
171bc149bfcSSoby Mathew #if ARM_CONFIG_CNTACR
172bc149bfcSSoby Mathew 	reg_val = (1 << CNTACR_RPCT_SHIFT) | (1 << CNTACR_RVCT_SHIFT);
173bc149bfcSSoby Mathew 	reg_val |= (1 << CNTACR_RFRQ_SHIFT) | (1 << CNTACR_RVOFF_SHIFT);
174bc149bfcSSoby Mathew 	reg_val |= (1 << CNTACR_RWVT_SHIFT) | (1 << CNTACR_RWPT_SHIFT);
175bc149bfcSSoby Mathew 	mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTACR_BASE(PLAT_ARM_NSTIMER_FRAME_ID), reg_val);
176bc149bfcSSoby Mathew #endif /* ARM_CONFIG_CNTACR */
177bc149bfcSSoby Mathew 
178bc149bfcSSoby Mathew 	reg_val = (1 << CNTNSAR_NS_SHIFT(PLAT_ARM_NSTIMER_FRAME_ID));
179bc149bfcSSoby Mathew 	mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTNSAR, reg_val);
180bc149bfcSSoby Mathew }
181bc149bfcSSoby Mathew #endif /* ARM_SYS_TIMCTL_BASE */
182bc149bfcSSoby Mathew 
183bc149bfcSSoby Mathew /*******************************************************************************
184bc149bfcSSoby Mathew  * Returns ARM platform specific memory map regions.
185bc149bfcSSoby Mathew  ******************************************************************************/
186bc149bfcSSoby Mathew const mmap_region_t *plat_arm_get_mmap(void)
187bc149bfcSSoby Mathew {
188bc149bfcSSoby Mathew 	return plat_arm_mmap;
189bc149bfcSSoby Mathew }
190bc149bfcSSoby Mathew 
191bc149bfcSSoby Mathew #ifdef ARM_SYS_CNTCTL_BASE
192bc149bfcSSoby Mathew 
193bc149bfcSSoby Mathew unsigned int plat_get_syscnt_freq2(void)
194bc149bfcSSoby Mathew {
195bc149bfcSSoby Mathew 	unsigned int counter_base_frequency;
196bc149bfcSSoby Mathew 
197bc149bfcSSoby Mathew 	/* Read the frequency from Frequency modes table */
198bc149bfcSSoby Mathew 	counter_base_frequency = mmio_read_32(ARM_SYS_CNTCTL_BASE + CNTFID_OFF);
199bc149bfcSSoby Mathew 
200bc149bfcSSoby Mathew 	/* The first entry of the frequency modes table must not be 0 */
201bc149bfcSSoby Mathew 	if (counter_base_frequency == 0)
202bc149bfcSSoby Mathew 		panic();
203bc149bfcSSoby Mathew 
204bc149bfcSSoby Mathew 	return counter_base_frequency;
205bc149bfcSSoby Mathew }
206bc149bfcSSoby Mathew 
207bc149bfcSSoby Mathew #endif /* ARM_SYS_CNTCTL_BASE */
208781f4aacSJeenu Viswambharan 
209781f4aacSJeenu Viswambharan #if SDEI_SUPPORT
210781f4aacSJeenu Viswambharan /*
211781f4aacSJeenu Viswambharan  * Translate SDEI entry point to PA, and perform standard ARM entry point
212781f4aacSJeenu Viswambharan  * validation on it.
213781f4aacSJeenu Viswambharan  */
214781f4aacSJeenu Viswambharan int plat_sdei_validate_entry_point(uintptr_t ep, unsigned int client_mode)
215781f4aacSJeenu Viswambharan {
216781f4aacSJeenu Viswambharan 	uint64_t par, pa;
217781f4aacSJeenu Viswambharan 	uint32_t scr_el3;
218781f4aacSJeenu Viswambharan 
219781f4aacSJeenu Viswambharan 	/* Doing Non-secure address translation requires SCR_EL3.NS set */
220781f4aacSJeenu Viswambharan 	scr_el3 = read_scr_el3();
221781f4aacSJeenu Viswambharan 	write_scr_el3(scr_el3 | SCR_NS_BIT);
222781f4aacSJeenu Viswambharan 	isb();
223781f4aacSJeenu Viswambharan 
224781f4aacSJeenu Viswambharan 	assert((client_mode == MODE_EL2) || (client_mode == MODE_EL1));
225781f4aacSJeenu Viswambharan 	if (client_mode == MODE_EL2) {
226781f4aacSJeenu Viswambharan 		/*
227781f4aacSJeenu Viswambharan 		 * Translate entry point to Physical Address using the EL2
228781f4aacSJeenu Viswambharan 		 * translation regime.
229781f4aacSJeenu Viswambharan 		 */
230781f4aacSJeenu Viswambharan 		ats1e2r(ep);
231781f4aacSJeenu Viswambharan 	} else {
232781f4aacSJeenu Viswambharan 		/*
233781f4aacSJeenu Viswambharan 		 * Translate entry point to Physical Address using the EL1&0
234781f4aacSJeenu Viswambharan 		 * translation regime, including stage 2.
235781f4aacSJeenu Viswambharan 		 */
236781f4aacSJeenu Viswambharan 		ats12e1r(ep);
237781f4aacSJeenu Viswambharan 	}
238781f4aacSJeenu Viswambharan 	isb();
239781f4aacSJeenu Viswambharan 	par = read_par_el1();
240781f4aacSJeenu Viswambharan 
241781f4aacSJeenu Viswambharan 	/* Restore original SCRL_EL3 */
242781f4aacSJeenu Viswambharan 	write_scr_el3(scr_el3);
243781f4aacSJeenu Viswambharan 	isb();
244781f4aacSJeenu Viswambharan 
245781f4aacSJeenu Viswambharan 	/* If the translation resulted in fault, return failure */
246781f4aacSJeenu Viswambharan 	if ((par & PAR_F_MASK) != 0)
247781f4aacSJeenu Viswambharan 		return -1;
248781f4aacSJeenu Viswambharan 
249781f4aacSJeenu Viswambharan 	/* Extract Physical Address from PAR */
250781f4aacSJeenu Viswambharan 	pa = (par & (PAR_ADDR_MASK << PAR_ADDR_SHIFT));
251781f4aacSJeenu Viswambharan 
252781f4aacSJeenu Viswambharan 	/* Perform NS entry point validation on the physical address */
253781f4aacSJeenu Viswambharan 	return arm_validate_ns_entrypoint(pa);
254781f4aacSJeenu Viswambharan }
255781f4aacSJeenu Viswambharan #endif
256