xref: /rk3399_ARM-atf/plat/arm/common/arm_common.c (revision bc149bfcaeecb947a8a631715c66c712b2cb9436)
1*bc149bfcSSoby Mathew /*
2*bc149bfcSSoby Mathew  * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
3*bc149bfcSSoby Mathew  *
4*bc149bfcSSoby Mathew  * Redistribution and use in source and binary forms, with or without
5*bc149bfcSSoby Mathew  * modification, are permitted provided that the following conditions are met:
6*bc149bfcSSoby Mathew  *
7*bc149bfcSSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
8*bc149bfcSSoby Mathew  * list of conditions and the following disclaimer.
9*bc149bfcSSoby Mathew  *
10*bc149bfcSSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
11*bc149bfcSSoby Mathew  * this list of conditions and the following disclaimer in the documentation
12*bc149bfcSSoby Mathew  * and/or other materials provided with the distribution.
13*bc149bfcSSoby Mathew  *
14*bc149bfcSSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
15*bc149bfcSSoby Mathew  * to endorse or promote products derived from this software without specific
16*bc149bfcSSoby Mathew  * prior written permission.
17*bc149bfcSSoby Mathew  *
18*bc149bfcSSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*bc149bfcSSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*bc149bfcSSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*bc149bfcSSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*bc149bfcSSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*bc149bfcSSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*bc149bfcSSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*bc149bfcSSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*bc149bfcSSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*bc149bfcSSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*bc149bfcSSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
29*bc149bfcSSoby Mathew  */
30*bc149bfcSSoby Mathew #include <arch.h>
31*bc149bfcSSoby Mathew #include <arch_helpers.h>
32*bc149bfcSSoby Mathew #include <assert.h>
33*bc149bfcSSoby Mathew #include <debug.h>
34*bc149bfcSSoby Mathew #include <mmio.h>
35*bc149bfcSSoby Mathew #include <plat_arm.h>
36*bc149bfcSSoby Mathew #include <platform_def.h>
37*bc149bfcSSoby Mathew #include <xlat_tables.h>
38*bc149bfcSSoby Mathew 
39*bc149bfcSSoby Mathew extern const mmap_region_t plat_arm_mmap[];
40*bc149bfcSSoby Mathew 
41*bc149bfcSSoby Mathew /* Weak definitions may be overridden in specific ARM standard platform */
42*bc149bfcSSoby Mathew #pragma weak plat_get_ns_image_entrypoint
43*bc149bfcSSoby Mathew #pragma weak plat_arm_get_mmap
44*bc149bfcSSoby Mathew 
45*bc149bfcSSoby Mathew /* Conditionally provide a weak definition of plat_get_syscnt_freq2 to avoid
46*bc149bfcSSoby Mathew  * conflicts with the definition in plat/common. */
47*bc149bfcSSoby Mathew #if ERROR_DEPRECATED
48*bc149bfcSSoby Mathew #pragma weak plat_get_syscnt_freq2
49*bc149bfcSSoby Mathew #else
50*bc149bfcSSoby Mathew #pragma weak plat_get_syscnt_freq
51*bc149bfcSSoby Mathew #endif
52*bc149bfcSSoby Mathew 
53*bc149bfcSSoby Mathew /*
54*bc149bfcSSoby Mathew  * Set up the page tables for the generic and platform-specific memory regions.
55*bc149bfcSSoby Mathew  * The extents of the generic memory regions are specified by the function
56*bc149bfcSSoby Mathew  * arguments and consist of:
57*bc149bfcSSoby Mathew  * - Trusted SRAM seen by the BL image;
58*bc149bfcSSoby Mathew  * - Code section;
59*bc149bfcSSoby Mathew  * - Read-only data section;
60*bc149bfcSSoby Mathew  * - Coherent memory region, if applicable.
61*bc149bfcSSoby Mathew  */
62*bc149bfcSSoby Mathew void arm_setup_page_tables(uintptr_t total_base,
63*bc149bfcSSoby Mathew 			   size_t total_size,
64*bc149bfcSSoby Mathew 			   uintptr_t code_start,
65*bc149bfcSSoby Mathew 			   uintptr_t code_limit,
66*bc149bfcSSoby Mathew 			   uintptr_t rodata_start,
67*bc149bfcSSoby Mathew 			   uintptr_t rodata_limit
68*bc149bfcSSoby Mathew #if USE_COHERENT_MEM
69*bc149bfcSSoby Mathew 			   ,
70*bc149bfcSSoby Mathew 			   uintptr_t coh_start,
71*bc149bfcSSoby Mathew 			   uintptr_t coh_limit
72*bc149bfcSSoby Mathew #endif
73*bc149bfcSSoby Mathew 			   )
74*bc149bfcSSoby Mathew {
75*bc149bfcSSoby Mathew 	/*
76*bc149bfcSSoby Mathew 	 * Map the Trusted SRAM with appropriate memory attributes.
77*bc149bfcSSoby Mathew 	 * Subsequent mappings will adjust the attributes for specific regions.
78*bc149bfcSSoby Mathew 	 */
79*bc149bfcSSoby Mathew 	VERBOSE("Trusted SRAM seen by this BL image: %p - %p\n",
80*bc149bfcSSoby Mathew 		(void *) total_base, (void *) (total_base + total_size));
81*bc149bfcSSoby Mathew 	mmap_add_region(total_base, total_base,
82*bc149bfcSSoby Mathew 			total_size,
83*bc149bfcSSoby Mathew 			MT_MEMORY | MT_RW | MT_SECURE);
84*bc149bfcSSoby Mathew 
85*bc149bfcSSoby Mathew 	/* Re-map the code section */
86*bc149bfcSSoby Mathew 	VERBOSE("Code region: %p - %p\n",
87*bc149bfcSSoby Mathew 		(void *) code_start, (void *) code_limit);
88*bc149bfcSSoby Mathew 	mmap_add_region(code_start, code_start,
89*bc149bfcSSoby Mathew 			code_limit - code_start,
90*bc149bfcSSoby Mathew 			MT_CODE | MT_SECURE);
91*bc149bfcSSoby Mathew 
92*bc149bfcSSoby Mathew 	/* Re-map the read-only data section */
93*bc149bfcSSoby Mathew 	VERBOSE("Read-only data region: %p - %p\n",
94*bc149bfcSSoby Mathew 		(void *) rodata_start, (void *) rodata_limit);
95*bc149bfcSSoby Mathew 	mmap_add_region(rodata_start, rodata_start,
96*bc149bfcSSoby Mathew 			rodata_limit - rodata_start,
97*bc149bfcSSoby Mathew 			MT_RO_DATA | MT_SECURE);
98*bc149bfcSSoby Mathew 
99*bc149bfcSSoby Mathew #if USE_COHERENT_MEM
100*bc149bfcSSoby Mathew 	/* Re-map the coherent memory region */
101*bc149bfcSSoby Mathew 	VERBOSE("Coherent region: %p - %p\n",
102*bc149bfcSSoby Mathew 		(void *) coh_start, (void *) coh_limit);
103*bc149bfcSSoby Mathew 	mmap_add_region(coh_start, coh_start,
104*bc149bfcSSoby Mathew 			coh_limit - coh_start,
105*bc149bfcSSoby Mathew 			MT_DEVICE | MT_RW | MT_SECURE);
106*bc149bfcSSoby Mathew #endif
107*bc149bfcSSoby Mathew 
108*bc149bfcSSoby Mathew 	/* Now (re-)map the platform-specific memory regions */
109*bc149bfcSSoby Mathew 	mmap_add(plat_arm_get_mmap());
110*bc149bfcSSoby Mathew 
111*bc149bfcSSoby Mathew 	/* Create the page tables to reflect the above mappings */
112*bc149bfcSSoby Mathew 	init_xlat_tables();
113*bc149bfcSSoby Mathew }
114*bc149bfcSSoby Mathew 
115*bc149bfcSSoby Mathew uintptr_t plat_get_ns_image_entrypoint(void)
116*bc149bfcSSoby Mathew {
117*bc149bfcSSoby Mathew 	return PLAT_ARM_NS_IMAGE_OFFSET;
118*bc149bfcSSoby Mathew }
119*bc149bfcSSoby Mathew 
120*bc149bfcSSoby Mathew /*******************************************************************************
121*bc149bfcSSoby Mathew  * Gets SPSR for BL32 entry
122*bc149bfcSSoby Mathew  ******************************************************************************/
123*bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl32_entry(void)
124*bc149bfcSSoby Mathew {
125*bc149bfcSSoby Mathew 	/*
126*bc149bfcSSoby Mathew 	 * The Secure Payload Dispatcher service is responsible for
127*bc149bfcSSoby Mathew 	 * setting the SPSR prior to entry into the BL32 image.
128*bc149bfcSSoby Mathew 	 */
129*bc149bfcSSoby Mathew 	return 0;
130*bc149bfcSSoby Mathew }
131*bc149bfcSSoby Mathew 
132*bc149bfcSSoby Mathew /*******************************************************************************
133*bc149bfcSSoby Mathew  * Gets SPSR for BL33 entry
134*bc149bfcSSoby Mathew  ******************************************************************************/
135*bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl33_entry(void)
136*bc149bfcSSoby Mathew {
137*bc149bfcSSoby Mathew 	unsigned long el_status;
138*bc149bfcSSoby Mathew 	unsigned int mode;
139*bc149bfcSSoby Mathew 	uint32_t spsr;
140*bc149bfcSSoby Mathew 
141*bc149bfcSSoby Mathew 	/* Figure out what mode we enter the non-secure world in */
142*bc149bfcSSoby Mathew 	el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
143*bc149bfcSSoby Mathew 	el_status &= ID_AA64PFR0_ELX_MASK;
144*bc149bfcSSoby Mathew 
145*bc149bfcSSoby Mathew 	mode = (el_status) ? MODE_EL2 : MODE_EL1;
146*bc149bfcSSoby Mathew 
147*bc149bfcSSoby Mathew 	/*
148*bc149bfcSSoby Mathew 	 * TODO: Consider the possibility of specifying the SPSR in
149*bc149bfcSSoby Mathew 	 * the FIP ToC and allowing the platform to have a say as
150*bc149bfcSSoby Mathew 	 * well.
151*bc149bfcSSoby Mathew 	 */
152*bc149bfcSSoby Mathew 	spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
153*bc149bfcSSoby Mathew 	return spsr;
154*bc149bfcSSoby Mathew }
155*bc149bfcSSoby Mathew 
156*bc149bfcSSoby Mathew /*******************************************************************************
157*bc149bfcSSoby Mathew  * Configures access to the system counter timer module.
158*bc149bfcSSoby Mathew  ******************************************************************************/
159*bc149bfcSSoby Mathew #ifdef ARM_SYS_TIMCTL_BASE
160*bc149bfcSSoby Mathew void arm_configure_sys_timer(void)
161*bc149bfcSSoby Mathew {
162*bc149bfcSSoby Mathew 	unsigned int reg_val;
163*bc149bfcSSoby Mathew 
164*bc149bfcSSoby Mathew #if ARM_CONFIG_CNTACR
165*bc149bfcSSoby Mathew 	reg_val = (1 << CNTACR_RPCT_SHIFT) | (1 << CNTACR_RVCT_SHIFT);
166*bc149bfcSSoby Mathew 	reg_val |= (1 << CNTACR_RFRQ_SHIFT) | (1 << CNTACR_RVOFF_SHIFT);
167*bc149bfcSSoby Mathew 	reg_val |= (1 << CNTACR_RWVT_SHIFT) | (1 << CNTACR_RWPT_SHIFT);
168*bc149bfcSSoby Mathew 	mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTACR_BASE(PLAT_ARM_NSTIMER_FRAME_ID), reg_val);
169*bc149bfcSSoby Mathew #endif /* ARM_CONFIG_CNTACR */
170*bc149bfcSSoby Mathew 
171*bc149bfcSSoby Mathew 	reg_val = (1 << CNTNSAR_NS_SHIFT(PLAT_ARM_NSTIMER_FRAME_ID));
172*bc149bfcSSoby Mathew 	mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTNSAR, reg_val);
173*bc149bfcSSoby Mathew }
174*bc149bfcSSoby Mathew #endif /* ARM_SYS_TIMCTL_BASE */
175*bc149bfcSSoby Mathew 
176*bc149bfcSSoby Mathew /*******************************************************************************
177*bc149bfcSSoby Mathew  * Returns ARM platform specific memory map regions.
178*bc149bfcSSoby Mathew  ******************************************************************************/
179*bc149bfcSSoby Mathew const mmap_region_t *plat_arm_get_mmap(void)
180*bc149bfcSSoby Mathew {
181*bc149bfcSSoby Mathew 	return plat_arm_mmap;
182*bc149bfcSSoby Mathew }
183*bc149bfcSSoby Mathew 
184*bc149bfcSSoby Mathew #ifdef ARM_SYS_CNTCTL_BASE
185*bc149bfcSSoby Mathew 
186*bc149bfcSSoby Mathew #if ERROR_DEPRECATED
187*bc149bfcSSoby Mathew unsigned int plat_get_syscnt_freq2(void)
188*bc149bfcSSoby Mathew {
189*bc149bfcSSoby Mathew 	unsigned int counter_base_frequency;
190*bc149bfcSSoby Mathew #else
191*bc149bfcSSoby Mathew unsigned long long plat_get_syscnt_freq(void)
192*bc149bfcSSoby Mathew {
193*bc149bfcSSoby Mathew 	unsigned long long counter_base_frequency;
194*bc149bfcSSoby Mathew #endif /* ERROR_DEPRECATED */
195*bc149bfcSSoby Mathew 
196*bc149bfcSSoby Mathew 	/* Read the frequency from Frequency modes table */
197*bc149bfcSSoby Mathew 	counter_base_frequency = mmio_read_32(ARM_SYS_CNTCTL_BASE + CNTFID_OFF);
198*bc149bfcSSoby Mathew 
199*bc149bfcSSoby Mathew 	/* The first entry of the frequency modes table must not be 0 */
200*bc149bfcSSoby Mathew 	if (counter_base_frequency == 0)
201*bc149bfcSSoby Mathew 		panic();
202*bc149bfcSSoby Mathew 
203*bc149bfcSSoby Mathew 	return counter_base_frequency;
204*bc149bfcSSoby Mathew }
205*bc149bfcSSoby Mathew 
206*bc149bfcSSoby Mathew #endif /* ARM_SYS_CNTCTL_BASE */
207