blob: 7aa1182ddecc2cc718ec13b8d49ab2d91d19af46 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
2 * Copyright (c) 2013, ARM Limited. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <string.h>
32#include <assert.h>
33#include <arch_helpers.h>
34#include <platform.h>
35#include <bl31.h>
36#include <bl_common.h>
37#include <pl011.h>
38#include <bakery_lock.h>
39#include <cci400.h>
40#include <gic.h>
41#include <fvp_pwrc.h>
42
43/*******************************************************************************
44 * Declarations of linker defined symbols which will help us find the layout
45 * of trusted SRAM
46 ******************************************************************************/
47#if defined (__GNUC__)
48extern unsigned long __BL31_RO_BASE__;
49extern unsigned long __BL31_STACKS_BASE__;
50extern unsigned long __BL31_COHERENT_RAM_BASE__;
51extern unsigned long __BL31_RW_BASE__;
52
53#define BL31_RO_BASE __BL31_RO_BASE__
54#define BL31_STACKS_BASE __BL31_STACKS_BASE__
55#define BL31_COHERENT_RAM_BASE __BL31_COHERENT_RAM_BASE__
56#define BL31_RW_BASE __BL31_RW_BASE__
57
58#else
59 #error "Unknown compiler."
60#endif
61
62/*******************************************************************************
63 * This data structures holds information copied by BL31 from BL2 to pass
64 * control to the non-trusted software images. A per-cpu entry was created to
65 * use the same structure in the warm boot path but that's not the case right
66 * now. Persisting with this approach for the time being. TODO: Can this be
67 * moved out of device memory.
68 ******************************************************************************/
69el_change_info ns_entry_info[PLATFORM_CORE_COUNT]
70__attribute__ ((aligned(PLATFORM_CACHE_LINE_SIZE),
71 section("tzfw_coherent_mem"))) = {0};
72
73/* Data structure which holds the extents of the trusted SRAM for BL31 */
74static meminfo bl31_tzram_layout
75__attribute__ ((aligned(PLATFORM_CACHE_LINE_SIZE),
76 section("tzfw_coherent_mem"))) = {0};
77
78meminfo bl31_get_sec_mem_layout(void)
79{
80 return bl31_tzram_layout;
81}
82
83/*******************************************************************************
84 * Return information about passing control to the non-trusted software images
85 * to common code.TODO: In the initial architecture, the image after BL31 will
86 * always run in the non-secure state. In the final architecture there
87 * will be a series of images. This function will need enhancement then
88 ******************************************************************************/
89el_change_info *bl31_get_next_image_info(unsigned long mpidr)
90{
91 return &ns_entry_info[platform_get_core_pos(mpidr)];
92}
93
94/*******************************************************************************
95 * Perform any BL31 specific platform actions. Here we copy parameters passed
96 * by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they are lost
97 * (potentially). This is done before the MMU is initialized so that the memory
98 * layout can be used while creating page tables.
99 ******************************************************************************/
100void bl31_early_platform_setup(meminfo *mem_layout,
101 void *data,
102 unsigned long mpidr)
103{
104 el_change_info *image_info = (el_change_info *) data;
105 unsigned int lin_index = platform_get_core_pos(mpidr);
106
107 /* Setup the BL31 memory layout */
108 bl31_tzram_layout.total_base = mem_layout->total_base;
109 bl31_tzram_layout.total_size = mem_layout->total_size;
110 bl31_tzram_layout.free_base = mem_layout->free_base;
111 bl31_tzram_layout.free_size = mem_layout->free_size;
112 bl31_tzram_layout.attr = mem_layout->attr;
113 bl31_tzram_layout.next = 0;
114
115 /* Save information about jumping into the NS world */
116 ns_entry_info[lin_index].entrypoint = image_info->entrypoint;
117 ns_entry_info[lin_index].spsr = image_info->spsr;
118 ns_entry_info[lin_index].args = image_info->args;
119 ns_entry_info[lin_index].security_state = image_info->security_state;
120 ns_entry_info[lin_index].next = image_info->next;
121
122 /* Initialize the platform config for future decision making */
123 platform_config_setup();
124}
125
126/*******************************************************************************
127 * Initialize the gic, configure the CLCD and zero out variables needed by the
128 * secondaries to boot up correctly.
129 ******************************************************************************/
130void bl31_platform_setup()
131{
132 unsigned int reg_val;
133
134 /* Initialize the gic cpu and distributor interfaces */
135 gic_setup();
136
137 /*
138 * TODO: Configure the CLCD before handing control to
139 * linux. Need to see if a separate driver is needed
140 * instead.
141 */
142 mmio_write_32(VE_SYSREGS_BASE + V2M_SYS_CFGDATA, 0);
143 mmio_write_32(VE_SYSREGS_BASE + V2M_SYS_CFGCTRL,
144 (1ull << 31) | (1 << 30) | (7 << 20) | (0 << 16));
145
146 /* Allow access to the System counter timer module */
147 reg_val = (1 << CNTACR_RPCT_SHIFT) | (1 << CNTACR_RVCT_SHIFT);
148 reg_val |= (1 << CNTACR_RFRQ_SHIFT) | (1 << CNTACR_RVOFF_SHIFT);
149 reg_val |= (1 << CNTACR_RWVT_SHIFT) | (1 << CNTACR_RWPT_SHIFT);
150 mmio_write_32(SYS_TIMCTL_BASE + CNTACR_BASE(0), reg_val);
151 mmio_write_32(SYS_TIMCTL_BASE + CNTACR_BASE(1), reg_val);
152
153 reg_val = (1 << CNTNSAR_NS_SHIFT(0)) | (1 << CNTNSAR_NS_SHIFT(1));
154 mmio_write_32(SYS_TIMCTL_BASE + CNTNSAR, reg_val);
155
156 /* Intialize the power controller */
157 fvp_pwrc_setup();
158
159 /* Topologies are best known to the platform. */
160 plat_setup_topology();
161}
162
163/*******************************************************************************
164 * Perform the very early platform specific architectural setup here. At the
165 * moment this is only intializes the mmu in a quick and dirty way.
166 ******************************************************************************/
167void bl31_plat_arch_setup()
168{
Achin Gupta4f6ad662013-10-25 09:08:21 +0100169 configure_mmu(&bl31_tzram_layout,
170 (unsigned long) &BL31_RO_BASE,
171 (unsigned long) &BL31_STACKS_BASE,
172 (unsigned long) &BL31_COHERENT_RAM_BASE,
173 (unsigned long) &BL31_RW_BASE);
174}
175
176/*******************************************************************************
177 * TODO: Move GIC setup to a separate file in case it is needed by other BL
178 * stages or ELs
179 * TODO: Revisit if priorities are being set such that no non-secure interrupt
180 * can have a higher priority than a secure one as recommended in the GICv2 spec
181 *******************************************************************************/
182
183/*******************************************************************************
184 * This function does some minimal GICv3 configuration. The Firmware itself does
185 * not fully support GICv3 at this time and relies on GICv2 emulation as
186 * provided by GICv3. This function allows software (like Linux) in later stages
187 * to use full GICv3 features.
188 *******************************************************************************/
189void gicv3_cpuif_setup(void)
190{
191 unsigned int scr_val, val, base;
192
193 /*
194 * When CPUs come out of reset they have their GICR_WAKER.ProcessorSleep
195 * bit set. In order to allow interrupts to get routed to the CPU we
196 * need to clear this bit if set and wait for GICR_WAKER.ChildrenAsleep
197 * to clear (GICv3 Architecture specification 5.4.23).
198 * GICR_WAKER is NOT banked per CPU, compute the correct base address
199 * per CPU.
200 *
201 * TODO:
202 * For GICv4 we also need to adjust the Base address based on
203 * GICR_TYPER.VLPIS
204 */
205 base = BASE_GICR_BASE +
206 (platform_get_core_pos(read_mpidr()) << GICR_PCPUBASE_SHIFT);
207 val = gicr_read_waker(base);
208
209 val &= ~WAKER_PS;
210 gicr_write_waker(base, val);
211 dsb();
212
213 /* We need to wait for ChildrenAsleep to clear. */
214 val = gicr_read_waker(base);
215 while (val & WAKER_CA) {
216 val = gicr_read_waker(base);
217 }
218
219 /*
220 * We need to set SCR_EL3.NS in order to see GICv3 non-secure state.
221 * Restore SCR_EL3.NS again before exit.
222 */
223 scr_val = read_scr();
224 write_scr(scr_val | SCR_NS_BIT);
225
226 /*
227 * By default EL2 and NS-EL1 software should be able to enable GICv3
228 * System register access without any configuration at EL3. But it turns
229 * out that GICC PMR as set in GICv2 mode does not affect GICv3 mode. So
230 * we need to set it here again. In order to do that we need to enable
231 * register access. We leave it enabled as it should be fine and might
232 * prevent problems with later software trying to access GIC System
233 * Registers.
234 */
235 val = read_icc_sre_el3();
236 write_icc_sre_el3(val | ICC_SRE_EN | ICC_SRE_SRE);
237
238 val = read_icc_sre_el2();
239 write_icc_sre_el2(val | ICC_SRE_EN | ICC_SRE_SRE);
240
241 write_icc_pmr_el1(MAX_PRI_VAL);
242
243 /* Restore SCR_EL3 */
244 write_scr(scr_val);
245}
246
247/*******************************************************************************
248 * This function does some minimal GICv3 configuration when cores go
249 * down.
250 *******************************************************************************/
251void gicv3_cpuif_deactivate(void)
252{
253 unsigned int val, base;
254
255 /*
256 * When taking CPUs down we need to set GICR_WAKER.ProcessorSleep and
257 * wait for GICR_WAKER.ChildrenAsleep to get set.
258 * (GICv3 Architecture specification 5.4.23).
259 * GICR_WAKER is NOT banked per CPU, compute the correct base address
260 * per CPU.
261 *
262 * TODO:
263 * For GICv4 we also need to adjust the Base address based on
264 * GICR_TYPER.VLPIS
265 */
266 base = BASE_GICR_BASE +
267 (platform_get_core_pos(read_mpidr()) << GICR_PCPUBASE_SHIFT);
268 val = gicr_read_waker(base);
269 val |= WAKER_PS;
270 gicr_write_waker(base, val);
271 dsb();
272
273 /* We need to wait for ChildrenAsleep to set. */
274 val = gicr_read_waker(base);
275 while ((val & WAKER_CA) == 0) {
276 val = gicr_read_waker(base);
277 }
278}
279
280
281/*******************************************************************************
282 * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
283 * and set the priority mask register to allow all interrupts to trickle in.
284 ******************************************************************************/
285void gic_cpuif_setup(unsigned int gicc_base)
286{
287 unsigned int val;
288
289 val = gicc_read_iidr(gicc_base);
290
291 /*
292 * If GICv3 we need to do a bit of additional setup. We want to
293 * allow default GICv2 behaviour but allow the next stage to
294 * enable full gicv3 features.
295 */
296 if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) {
297 gicv3_cpuif_setup();
298 }
299
300 val = ENABLE_GRP0 | FIQ_EN | FIQ_BYP_DIS_GRP0;
301 val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
302
303 gicc_write_pmr(gicc_base, MAX_PRI_VAL);
304 gicc_write_ctlr(gicc_base, val);
305}
306
307/*******************************************************************************
308 * Place the cpu interface in a state where it can never make a cpu exit wfi as
309 * as result of an asserted interrupt. This is critical for powering down a cpu
310 ******************************************************************************/
311void gic_cpuif_deactivate(unsigned int gicc_base)
312{
313 unsigned int val;
314
315 /* Disable secure, non-secure interrupts and disable their bypass */
316 val = gicc_read_ctlr(gicc_base);
317 val &= ~(ENABLE_GRP0 | ENABLE_GRP1);
318 val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
319 val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
320 gicc_write_ctlr(gicc_base, val);
321
322 val = gicc_read_iidr(gicc_base);
323
324 /*
325 * If GICv3 we need to do a bit of additional setup. Make sure the
326 * RDIST is put to sleep.
327 */
328 if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) {
329 gicv3_cpuif_deactivate();
330 }
331}
332
333/*******************************************************************************
334 * Per cpu gic distributor setup which will be done by all cpus after a cold
335 * boot/hotplug. This marks out the secure interrupts & enables them.
336 ******************************************************************************/
337void gic_pcpu_distif_setup(unsigned int gicd_base)
338{
339 gicd_write_igroupr(gicd_base, 0, ~0);
340
341 gicd_clr_igroupr(gicd_base, IRQ_SEC_PHY_TIMER);
342 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_0);
343 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_1);
344 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_2);
345 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_3);
346 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_4);
347 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_5);
348 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_6);
349 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_7);
350
351 gicd_set_ipriorityr(gicd_base, IRQ_SEC_PHY_TIMER, MAX_PRI_VAL);
352 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_0, MAX_PRI_VAL);
353 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_1, MAX_PRI_VAL);
354 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_2, MAX_PRI_VAL);
355 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_3, MAX_PRI_VAL);
356 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_4, MAX_PRI_VAL);
357 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_5, MAX_PRI_VAL);
358 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_6, MAX_PRI_VAL);
359 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_7, MAX_PRI_VAL);
360
361 gicd_set_isenabler(gicd_base, IRQ_SEC_PHY_TIMER);
362 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_0);
363 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_1);
364 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_2);
365 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_3);
366 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_4);
367 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_5);
368 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_6);
369 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_7);
370}
371
372/*******************************************************************************
373 * Global gic distributor setup which will be done by the primary cpu after a
374 * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
375 * then enables the secure GIC distributor interface.
376 ******************************************************************************/
377void gic_distif_setup(unsigned int gicd_base)
378{
379 unsigned int ctr, num_ints, ctlr;
380
381 /* Disable the distributor before going further */
382 ctlr = gicd_read_ctlr(gicd_base);
383 ctlr &= ~(ENABLE_GRP0 | ENABLE_GRP1);
384 gicd_write_ctlr(gicd_base, ctlr);
385
386 /*
387 * Mark out non-secure interrupts. Calculate number of
388 * IGROUPR registers to consider. Will be equal to the
389 * number of IT_LINES
390 */
391 num_ints = gicd_read_typer(gicd_base) & IT_LINES_NO_MASK;
392 num_ints++;
393 for (ctr = 0; ctr < num_ints; ctr++)
394 gicd_write_igroupr(gicd_base, ctr << IGROUPR_SHIFT, ~0);
395
396 /* Configure secure interrupts now */
397 gicd_clr_igroupr(gicd_base, IRQ_TZ_WDOG);
398 gicd_set_ipriorityr(gicd_base, IRQ_TZ_WDOG, MAX_PRI_VAL);
399 gicd_set_itargetsr(gicd_base, IRQ_TZ_WDOG,
400 platform_get_core_pos(read_mpidr()));
401 gicd_set_isenabler(gicd_base, IRQ_TZ_WDOG);
402 gic_pcpu_distif_setup(gicd_base);
403
404 gicd_write_ctlr(gicd_base, ctlr | ENABLE_GRP0);
405}
406
407void gic_setup(void)
408{
409 unsigned int gicd_base, gicc_base;
410
411 gicd_base = platform_get_cfgvar(CONFIG_GICD_ADDR);
412 gicc_base = platform_get_cfgvar(CONFIG_GICC_ADDR);
413
414 gic_cpuif_setup(gicc_base);
415 gic_distif_setup(gicd_base);
416}