blob: ee12975748c9e36cee8fc70bee49a40f35fda574 [file] [log] [blame]
Varun Wadekarb316e242015-05-19 16:48:04 +05301/*
2 * Copyright (c) 2015, ARM Limited and Contributors. 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 <arch_helpers.h>
32#include <assert.h>
Varun Wadekard3a41502015-06-16 11:23:00 +053033#include <arm_gic.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053034#include <bl_common.h>
35#include <debug.h>
36#include <gic_v2.h>
37#include <interrupt_mgmt.h>
38#include <platform.h>
39#include <stdint.h>
40#include <tegra_private.h>
41#include <tegra_def.h>
42
Varun Wadekard3a41502015-06-16 11:23:00 +053043/* Value used to initialize Non-Secure IRQ priorities four at a time */
44#define GICD_IPRIORITYR_DEF_VAL \
45 (GIC_HIGHEST_NS_PRIORITY | \
46 (GIC_HIGHEST_NS_PRIORITY << 8) | \
47 (GIC_HIGHEST_NS_PRIORITY << 16) | \
48 (GIC_HIGHEST_NS_PRIORITY << 24))
49
Varun Wadekarb316e242015-05-19 16:48:04 +053050/*******************************************************************************
51 * Place the cpu interface in a state where it can never make a cpu exit wfi as
52 * as result of an asserted interrupt. This is critical for powering down a cpu
53 ******************************************************************************/
54void tegra_gic_cpuif_deactivate(void)
55{
56 unsigned int val;
57
58 /* Disable secure, non-secure interrupts and disable their bypass */
59 val = gicc_read_ctlr(TEGRA_GICC_BASE);
60 val &= ~(ENABLE_GRP0 | ENABLE_GRP1);
61 val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
62 val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
63 gicc_write_ctlr(TEGRA_GICC_BASE, val);
64}
65
66/*******************************************************************************
67 * Enable secure interrupts and set the priority mask register to allow all
68 * interrupts to trickle in.
69 ******************************************************************************/
70static void tegra_gic_cpuif_setup(unsigned int gicc_base)
71{
Varun Wadekard3a41502015-06-16 11:23:00 +053072 unsigned int val;
73
74 val = ENABLE_GRP0 | ENABLE_GRP1 | FIQ_EN | FIQ_BYP_DIS_GRP0;
75 val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
76
77 gicc_write_ctlr(gicc_base, val);
Varun Wadekarb316e242015-05-19 16:48:04 +053078 gicc_write_pmr(gicc_base, GIC_PRI_MASK);
79}
80
81/*******************************************************************************
Varun Wadekard3a41502015-06-16 11:23:00 +053082 * Per cpu gic distributor setup which will be done by all cpus after a cold
83 * boot/hotplug. This marks out the secure interrupts & enables them.
84 ******************************************************************************/
85static void tegra_gic_pcpu_distif_setup(unsigned int gicd_base)
86{
87 unsigned int index, sec_ppi_sgi_mask = 0;
88
89 assert(gicd_base);
90
91 /* Setup PPI priorities doing four at a time */
92 for (index = 0; index < 32; index += 4) {
93 gicd_write_ipriorityr(gicd_base, index,
94 GICD_IPRIORITYR_DEF_VAL);
95 }
96
97 /*
98 * Invert the bitmask to create a mask for non-secure PPIs and
99 * SGIs. Program the GICD_IGROUPR0 with this bit mask. This write will
100 * update the GICR_IGROUPR0 as well in case we are running on a GICv3
101 * system. This is critical if GICD_CTLR.ARE_NS=1.
102 */
103 gicd_write_igroupr(gicd_base, 0, ~sec_ppi_sgi_mask);
104}
105
106/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530107 * Global gic distributor setup which will be done by the primary cpu after a
108 * cold boot. It marks out the non secure SPIs, PPIs & SGIs and enables them.
109 * It then enables the secure GIC distributor interface.
110 ******************************************************************************/
111static void tegra_gic_distif_setup(unsigned int gicd_base)
112{
Varun Wadekard3a41502015-06-16 11:23:00 +0530113 unsigned int index, num_ints;
Varun Wadekarb316e242015-05-19 16:48:04 +0530114
115 /*
116 * Mark out non-secure interrupts. Calculate number of
117 * IGROUPR registers to consider. Will be equal to the
118 * number of IT_LINES
119 */
120 num_ints = gicd_read_typer(gicd_base) & IT_LINES_NO_MASK;
Varun Wadekard3a41502015-06-16 11:23:00 +0530121 num_ints = (num_ints + 1) << 5;
122 for (index = MIN_SPI_ID; index < num_ints; index += 32)
123 gicd_write_igroupr(gicd_base, index, ~0);
124
125 /* Setup SPI priorities doing four at a time */
126 for (index = MIN_SPI_ID; index < num_ints; index += 4) {
127 gicd_write_ipriorityr(gicd_base, index,
128 GICD_IPRIORITYR_DEF_VAL);
129 }
130
131 /*
132 * Configure the SGI and PPI. This is done in a separated function
133 * because each CPU is responsible for initializing its own private
134 * interrupts.
135 */
136 tegra_gic_pcpu_distif_setup(gicd_base);
Varun Wadekarb316e242015-05-19 16:48:04 +0530137
138 /* enable distributor */
139 gicd_write_ctlr(gicd_base, ENABLE_GRP0 | ENABLE_GRP1);
140}
141
142void tegra_gic_setup(void)
143{
144 tegra_gic_cpuif_setup(TEGRA_GICC_BASE);
145 tegra_gic_distif_setup(TEGRA_GICD_BASE);
146}
Varun Wadekard3a41502015-06-16 11:23:00 +0530147
148/*******************************************************************************
149 * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins.
150 * The interrupt controller knows which pin/line it uses to signal a type of
151 * interrupt. This function provides a common implementation of
152 * plat_interrupt_type_to_line() in an ARM GIC environment for optional re-use
153 * across platforms. It lets the interrupt management framework determine
154 * for a type of interrupt and security state, which line should be used in the
155 * SCR_EL3 to control its routing to EL3. The interrupt line is represented as
156 * the bit position of the IRQ or FIQ bit in the SCR_EL3.
157 ******************************************************************************/
158uint32_t tegra_gic_interrupt_type_to_line(uint32_t type,
159 uint32_t security_state)
160{
161 assert(type == INTR_TYPE_S_EL1 ||
162 type == INTR_TYPE_EL3 ||
163 type == INTR_TYPE_NS);
164
165 assert(sec_state_is_valid(security_state));
166
167 /*
168 * We ignore the security state parameter under the assumption that
169 * both normal and secure worlds are using ARM GICv2. This parameter
170 * will be used when the secure world starts using GICv3.
171 */
172#if ARM_GIC_ARCH == 2
173 return gicv2_interrupt_type_to_line(TEGRA_GICC_BASE, type);
174#else
175#error "Invalid ARM GIC architecture version specified for platform port"
176#endif /* ARM_GIC_ARCH */
177}
178
179#if ARM_GIC_ARCH == 2
180/*******************************************************************************
181 * This function returns the type of the highest priority pending interrupt at
182 * the GIC cpu interface. INTR_TYPE_INVAL is returned when there is no
183 * interrupt pending.
184 ******************************************************************************/
185uint32_t tegra_gic_get_pending_interrupt_type(void)
186{
187 uint32_t id;
188
189 id = gicc_read_hppir(TEGRA_GICC_BASE) & INT_ID_MASK;
190
191 /* Assume that all secure interrupts are S-EL1 interrupts */
192 if (id < 1022)
193 return INTR_TYPE_S_EL1;
194
195 if (id == GIC_SPURIOUS_INTERRUPT)
196 return INTR_TYPE_INVAL;
197
198 return INTR_TYPE_NS;
199}
200
201/*******************************************************************************
202 * This function returns the id of the highest priority pending interrupt at
203 * the GIC cpu interface. INTR_ID_UNAVAILABLE is returned when there is no
204 * interrupt pending.
205 ******************************************************************************/
206uint32_t tegra_gic_get_pending_interrupt_id(void)
207{
208 uint32_t id;
209
210 id = gicc_read_hppir(TEGRA_GICC_BASE) & INT_ID_MASK;
211
212 if (id < 1022)
213 return id;
214
215 if (id == 1023)
216 return INTR_ID_UNAVAILABLE;
217
218 /*
219 * Find out which non-secure interrupt it is under the assumption that
220 * the GICC_CTLR.AckCtl bit is 0.
221 */
222 return gicc_read_ahppir(TEGRA_GICC_BASE) & INT_ID_MASK;
223}
224
225/*******************************************************************************
226 * This functions reads the GIC cpu interface Interrupt Acknowledge register
227 * to start handling the pending interrupt. It returns the contents of the IAR.
228 ******************************************************************************/
229uint32_t tegra_gic_acknowledge_interrupt(void)
230{
231 return gicc_read_IAR(TEGRA_GICC_BASE);
232}
233
234/*******************************************************************************
235 * This functions writes the GIC cpu interface End Of Interrupt register with
236 * the passed value to finish handling the active interrupt
237 ******************************************************************************/
238void tegra_gic_end_of_interrupt(uint32_t id)
239{
240 gicc_write_EOIR(TEGRA_GICC_BASE, id);
241}
242
243/*******************************************************************************
244 * This function returns the type of the interrupt id depending upon the group
245 * this interrupt has been configured under by the interrupt controller i.e.
246 * group0 or group1.
247 ******************************************************************************/
248uint32_t tegra_gic_get_interrupt_type(uint32_t id)
249{
250 uint32_t group;
251
252 group = gicd_get_igroupr(TEGRA_GICD_BASE, id);
253
254 /* Assume that all secure interrupts are S-EL1 interrupts */
255 if (group == GRP0)
256 return INTR_TYPE_S_EL1;
257 else
258 return INTR_TYPE_NS;
259}
260
261#else
262#error "Invalid ARM GIC architecture version specified for platform port"
263#endif /* ARM_GIC_ARCH */
264
265uint32_t plat_ic_get_pending_interrupt_id(void)
266{
267 return tegra_gic_get_pending_interrupt_id();
268}
269
270uint32_t plat_ic_get_pending_interrupt_type(void)
271{
272 return tegra_gic_get_pending_interrupt_type();
273}
274
275uint32_t plat_ic_acknowledge_interrupt(void)
276{
277 return tegra_gic_acknowledge_interrupt();
278}
279
280uint32_t plat_ic_get_interrupt_type(uint32_t id)
281{
282 return tegra_gic_get_interrupt_type(id);
283}
284
285void plat_ic_end_of_interrupt(uint32_t id)
286{
287 tegra_gic_end_of_interrupt(id);
288}
289
290uint32_t plat_interrupt_type_to_line(uint32_t type,
291 uint32_t security_state)
292{
293 return tegra_gic_interrupt_type_to_line(type, security_state);
294}