1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park *
4*54fd6939SJiyong Park * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park */
6*54fd6939SJiyong Park
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park
9*54fd6939SJiyong Park #include <platform_def.h>
10*54fd6939SJiyong Park
11*54fd6939SJiyong Park #include <arch.h>
12*54fd6939SJiyong Park #include <arch_helpers.h>
13*54fd6939SJiyong Park #include <common/debug.h>
14*54fd6939SJiyong Park #include <lib/mmio.h>
15*54fd6939SJiyong Park #include <lib/xlat_tables/xlat_tables_v2.h>
16*54fd6939SJiyong Park
17*54fd6939SJiyong Park const mmap_region_t *plat_ls_get_mmap(void);
18*54fd6939SJiyong Park
19*54fd6939SJiyong Park /*
20*54fd6939SJiyong Park * Table of memory regions for various BL stages to map using the MMU.
21*54fd6939SJiyong Park * This doesn't include Trusted SRAM as ls_setup_page_tables() already
22*54fd6939SJiyong Park * takes care of mapping it.
23*54fd6939SJiyong Park *
24*54fd6939SJiyong Park * The flash needs to be mapped as writable in order to erase the FIP's Table of
25*54fd6939SJiyong Park * Contents in case of unrecoverable error (see plat_error_handler()).
26*54fd6939SJiyong Park */
27*54fd6939SJiyong Park #ifdef IMAGE_BL1
28*54fd6939SJiyong Park const mmap_region_t plat_ls_mmap[] = {
29*54fd6939SJiyong Park LS_MAP_FLASH0_RW,
30*54fd6939SJiyong Park LS_MAP_NS_DRAM,
31*54fd6939SJiyong Park LS_MAP_CCSR,
32*54fd6939SJiyong Park {0}
33*54fd6939SJiyong Park };
34*54fd6939SJiyong Park #endif
35*54fd6939SJiyong Park #ifdef IMAGE_BL2
36*54fd6939SJiyong Park const mmap_region_t plat_ls_mmap[] = {
37*54fd6939SJiyong Park LS_MAP_FLASH0_RW,
38*54fd6939SJiyong Park LS_MAP_CCSR,
39*54fd6939SJiyong Park LS_MAP_NS_DRAM,
40*54fd6939SJiyong Park LS_MAP_TSP_SEC_MEM,
41*54fd6939SJiyong Park {0}
42*54fd6939SJiyong Park };
43*54fd6939SJiyong Park #endif
44*54fd6939SJiyong Park #ifdef IMAGE_BL31
45*54fd6939SJiyong Park const mmap_region_t plat_ls_mmap[] = {
46*54fd6939SJiyong Park LS_MAP_CCSR,
47*54fd6939SJiyong Park LS_MAP_FLASH0_RW,
48*54fd6939SJiyong Park LS_MAP_NS_DRAM,
49*54fd6939SJiyong Park LS_MAP_TSP_SEC_MEM,
50*54fd6939SJiyong Park {0}
51*54fd6939SJiyong Park };
52*54fd6939SJiyong Park #endif
53*54fd6939SJiyong Park #ifdef IMAGE_BL32
54*54fd6939SJiyong Park const mmap_region_t plat_ls_mmap[] = {
55*54fd6939SJiyong Park LS_MAP_CCSR,
56*54fd6939SJiyong Park LS_MAP_FLASH0_RW,
57*54fd6939SJiyong Park LS_MAP_TSP_SEC_MEM,
58*54fd6939SJiyong Park {0}
59*54fd6939SJiyong Park };
60*54fd6939SJiyong Park #endif
61*54fd6939SJiyong Park /*
62*54fd6939SJiyong Park * Set up the page tables for the generic and platform-specific memory regions.
63*54fd6939SJiyong Park * The extents of the generic memory regions are specified by the function
64*54fd6939SJiyong Park * arguments and consist of:
65*54fd6939SJiyong Park * - Trusted SRAM seen by the BL image;
66*54fd6939SJiyong Park * - Code section;
67*54fd6939SJiyong Park * - Read-only data section;
68*54fd6939SJiyong Park * - Coherent memory region, if applicable.
69*54fd6939SJiyong Park */
ls_setup_page_tables(uintptr_t total_base,size_t total_size,uintptr_t code_start,uintptr_t code_limit,uintptr_t rodata_start,uintptr_t rodata_limit,uintptr_t coh_start,uintptr_t coh_limit)70*54fd6939SJiyong Park void ls_setup_page_tables(uintptr_t total_base,
71*54fd6939SJiyong Park size_t total_size,
72*54fd6939SJiyong Park uintptr_t code_start,
73*54fd6939SJiyong Park uintptr_t code_limit,
74*54fd6939SJiyong Park uintptr_t rodata_start,
75*54fd6939SJiyong Park uintptr_t rodata_limit
76*54fd6939SJiyong Park #if USE_COHERENT_MEM
77*54fd6939SJiyong Park ,
78*54fd6939SJiyong Park uintptr_t coh_start,
79*54fd6939SJiyong Park uintptr_t coh_limit
80*54fd6939SJiyong Park #endif
81*54fd6939SJiyong Park )
82*54fd6939SJiyong Park {
83*54fd6939SJiyong Park /* Now (re-)map the platform-specific memory regions */
84*54fd6939SJiyong Park mmap_add(plat_ls_get_mmap());
85*54fd6939SJiyong Park /*
86*54fd6939SJiyong Park * Map the Trusted SRAM with appropriate memory attributes.
87*54fd6939SJiyong Park * Subsequent mappings will adjust the attributes for specific regions.
88*54fd6939SJiyong Park */
89*54fd6939SJiyong Park VERBOSE("Trusted SRAM seen by this BL image: %p - %p\n",
90*54fd6939SJiyong Park (void *) total_base, (void *) (total_base + total_size));
91*54fd6939SJiyong Park mmap_add_region(total_base, total_base,
92*54fd6939SJiyong Park total_size,
93*54fd6939SJiyong Park MT_MEMORY | MT_RW | MT_SECURE);
94*54fd6939SJiyong Park
95*54fd6939SJiyong Park /* Re-map the code section */
96*54fd6939SJiyong Park VERBOSE("Code region: %p - %p\n",
97*54fd6939SJiyong Park (void *) code_start, (void *) code_limit);
98*54fd6939SJiyong Park mmap_add_region(code_start, code_start,
99*54fd6939SJiyong Park code_limit - code_start,
100*54fd6939SJiyong Park MT_CODE | MT_SECURE);
101*54fd6939SJiyong Park
102*54fd6939SJiyong Park /* Re-map the read-only data section */
103*54fd6939SJiyong Park VERBOSE("Read-only data region: %p - %p\n",
104*54fd6939SJiyong Park (void *) rodata_start, (void *) rodata_limit);
105*54fd6939SJiyong Park mmap_add_region(rodata_start, rodata_start,
106*54fd6939SJiyong Park rodata_limit - rodata_start,
107*54fd6939SJiyong Park MT_RO_DATA | MT_SECURE);
108*54fd6939SJiyong Park
109*54fd6939SJiyong Park #if USE_COHERENT_MEM
110*54fd6939SJiyong Park /* Re-map the coherent memory region */
111*54fd6939SJiyong Park VERBOSE("Coherent region: %p - %p\n",
112*54fd6939SJiyong Park (void *) coh_start, (void *) coh_limit);
113*54fd6939SJiyong Park mmap_add_region(coh_start, coh_start,
114*54fd6939SJiyong Park coh_limit - coh_start,
115*54fd6939SJiyong Park MT_DEVICE | MT_RW | MT_SECURE);
116*54fd6939SJiyong Park #endif
117*54fd6939SJiyong Park
118*54fd6939SJiyong Park /* Create the page tables to reflect the above mappings */
119*54fd6939SJiyong Park init_xlat_tables();
120*54fd6939SJiyong Park }
121*54fd6939SJiyong Park
plat_get_ns_image_entrypoint(void)122*54fd6939SJiyong Park uintptr_t plat_get_ns_image_entrypoint(void)
123*54fd6939SJiyong Park {
124*54fd6939SJiyong Park #ifdef PRELOADED_BL33_BASE
125*54fd6939SJiyong Park return PRELOADED_BL33_BASE;
126*54fd6939SJiyong Park #else
127*54fd6939SJiyong Park return LS_NS_DRAM_BASE;
128*54fd6939SJiyong Park #endif
129*54fd6939SJiyong Park }
130*54fd6939SJiyong Park
131*54fd6939SJiyong Park /*******************************************************************************
132*54fd6939SJiyong Park * Gets SPSR for BL32 entry
133*54fd6939SJiyong Park ******************************************************************************/
ls_get_spsr_for_bl32_entry(void)134*54fd6939SJiyong Park uint32_t ls_get_spsr_for_bl32_entry(void)
135*54fd6939SJiyong Park {
136*54fd6939SJiyong Park /*
137*54fd6939SJiyong Park * The Secure Payload Dispatcher service is responsible for
138*54fd6939SJiyong Park * setting the SPSR prior to entry into the BL32 image.
139*54fd6939SJiyong Park */
140*54fd6939SJiyong Park return 0;
141*54fd6939SJiyong Park }
142*54fd6939SJiyong Park
143*54fd6939SJiyong Park /*******************************************************************************
144*54fd6939SJiyong Park * Gets SPSR for BL33 entry
145*54fd6939SJiyong Park ******************************************************************************/
146*54fd6939SJiyong Park #ifdef __aarch64__
ls_get_spsr_for_bl33_entry(void)147*54fd6939SJiyong Park uint32_t ls_get_spsr_for_bl33_entry(void)
148*54fd6939SJiyong Park {
149*54fd6939SJiyong Park unsigned int mode;
150*54fd6939SJiyong Park uint32_t spsr;
151*54fd6939SJiyong Park
152*54fd6939SJiyong Park /* Figure out what mode we enter the non-secure world in */
153*54fd6939SJiyong Park mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1;
154*54fd6939SJiyong Park
155*54fd6939SJiyong Park /*
156*54fd6939SJiyong Park * TODO: Consider the possibility of specifying the SPSR in
157*54fd6939SJiyong Park * the FIP ToC and allowing the platform to have a say as
158*54fd6939SJiyong Park * well.
159*54fd6939SJiyong Park */
160*54fd6939SJiyong Park spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
161*54fd6939SJiyong Park return spsr;
162*54fd6939SJiyong Park }
163*54fd6939SJiyong Park #else
164*54fd6939SJiyong Park /*******************************************************************************
165*54fd6939SJiyong Park * Gets SPSR for BL33 entry
166*54fd6939SJiyong Park ******************************************************************************/
ls_get_spsr_for_bl33_entry(void)167*54fd6939SJiyong Park uint32_t ls_get_spsr_for_bl33_entry(void)
168*54fd6939SJiyong Park {
169*54fd6939SJiyong Park unsigned int hyp_status, mode, spsr;
170*54fd6939SJiyong Park
171*54fd6939SJiyong Park hyp_status = GET_VIRT_EXT(read_id_pfr1());
172*54fd6939SJiyong Park
173*54fd6939SJiyong Park mode = (hyp_status) ? MODE32_hyp : MODE32_svc;
174*54fd6939SJiyong Park
175*54fd6939SJiyong Park /*
176*54fd6939SJiyong Park * TODO: Consider the possibility of specifying the SPSR in
177*54fd6939SJiyong Park * the FIP ToC and allowing the platform to have a say as
178*54fd6939SJiyong Park * well.
179*54fd6939SJiyong Park */
180*54fd6939SJiyong Park spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1,
181*54fd6939SJiyong Park SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
182*54fd6939SJiyong Park return spsr;
183*54fd6939SJiyong Park }
184*54fd6939SJiyong Park #endif /* __aarch64__ */
185*54fd6939SJiyong Park
186*54fd6939SJiyong Park /*******************************************************************************
187*54fd6939SJiyong Park * Returns Layerscape platform specific memory map regions.
188*54fd6939SJiyong Park ******************************************************************************/
plat_ls_get_mmap(void)189*54fd6939SJiyong Park const mmap_region_t *plat_ls_get_mmap(void)
190*54fd6939SJiyong Park {
191*54fd6939SJiyong Park return plat_ls_mmap;
192*54fd6939SJiyong Park }
193*54fd6939SJiyong Park
194*54fd6939SJiyong Park
plat_get_syscnt_freq2(void)195*54fd6939SJiyong Park unsigned int plat_get_syscnt_freq2(void)
196*54fd6939SJiyong Park {
197*54fd6939SJiyong Park unsigned int counter_base_frequency;
198*54fd6939SJiyong Park
199*54fd6939SJiyong Park counter_base_frequency = COUNTER_FREQUENCY;
200*54fd6939SJiyong Park
201*54fd6939SJiyong Park return counter_base_frequency;
202*54fd6939SJiyong Park }
203