blob: 4a84eec3984137c89178dab2d56eb75d8b04d35f [file] [log] [blame]
Varun Wadekarb316e242015-05-19 16:48:04 +05301/*
2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekarb316e242015-05-19 16:48:04 +05305 */
6
7#include <arch_helpers.h>
8#include <assert.h>
Varun Wadekard3a41502015-06-16 11:23:00 +05309#include <arm_gic.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053010#include <bl_common.h>
11#include <debug.h>
12#include <gic_v2.h>
13#include <interrupt_mgmt.h>
14#include <platform.h>
15#include <stdint.h>
16#include <tegra_private.h>
17#include <tegra_def.h>
18
Varun Wadekard3a41502015-06-16 11:23:00 +053019/* Value used to initialize Non-Secure IRQ priorities four at a time */
20#define GICD_IPRIORITYR_DEF_VAL \
21 (GIC_HIGHEST_NS_PRIORITY | \
22 (GIC_HIGHEST_NS_PRIORITY << 8) | \
23 (GIC_HIGHEST_NS_PRIORITY << 16) | \
24 (GIC_HIGHEST_NS_PRIORITY << 24))
25
Varun Wadekarc6c386d2016-05-20 16:21:22 -070026static const irq_sec_cfg_t *g_irq_sec_ptr;
Varun Wadekarb7b45752015-12-28 14:55:41 -080027static unsigned int g_num_irqs;
28
Varun Wadekarb316e242015-05-19 16:48:04 +053029/*******************************************************************************
30 * Place the cpu interface in a state where it can never make a cpu exit wfi as
31 * as result of an asserted interrupt. This is critical for powering down a cpu
32 ******************************************************************************/
33void tegra_gic_cpuif_deactivate(void)
34{
35 unsigned int val;
36
37 /* Disable secure, non-secure interrupts and disable their bypass */
38 val = gicc_read_ctlr(TEGRA_GICC_BASE);
39 val &= ~(ENABLE_GRP0 | ENABLE_GRP1);
40 val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
41 val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
42 gicc_write_ctlr(TEGRA_GICC_BASE, val);
43}
44
45/*******************************************************************************
46 * Enable secure interrupts and set the priority mask register to allow all
47 * interrupts to trickle in.
48 ******************************************************************************/
49static void tegra_gic_cpuif_setup(unsigned int gicc_base)
50{
Varun Wadekard3a41502015-06-16 11:23:00 +053051 unsigned int val;
52
53 val = ENABLE_GRP0 | ENABLE_GRP1 | FIQ_EN | FIQ_BYP_DIS_GRP0;
54 val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
55
56 gicc_write_ctlr(gicc_base, val);
Varun Wadekarb316e242015-05-19 16:48:04 +053057 gicc_write_pmr(gicc_base, GIC_PRI_MASK);
58}
59
60/*******************************************************************************
Varun Wadekard3a41502015-06-16 11:23:00 +053061 * Per cpu gic distributor setup which will be done by all cpus after a cold
62 * boot/hotplug. This marks out the secure interrupts & enables them.
63 ******************************************************************************/
64static void tegra_gic_pcpu_distif_setup(unsigned int gicd_base)
65{
66 unsigned int index, sec_ppi_sgi_mask = 0;
67
68 assert(gicd_base);
69
70 /* Setup PPI priorities doing four at a time */
71 for (index = 0; index < 32; index += 4) {
72 gicd_write_ipriorityr(gicd_base, index,
73 GICD_IPRIORITYR_DEF_VAL);
74 }
75
76 /*
77 * Invert the bitmask to create a mask for non-secure PPIs and
78 * SGIs. Program the GICD_IGROUPR0 with this bit mask. This write will
79 * update the GICR_IGROUPR0 as well in case we are running on a GICv3
80 * system. This is critical if GICD_CTLR.ARE_NS=1.
81 */
82 gicd_write_igroupr(gicd_base, 0, ~sec_ppi_sgi_mask);
83}
84
85/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +053086 * Global gic distributor setup which will be done by the primary cpu after a
87 * cold boot. It marks out the non secure SPIs, PPIs & SGIs and enables them.
88 * It then enables the secure GIC distributor interface.
89 ******************************************************************************/
90static void tegra_gic_distif_setup(unsigned int gicd_base)
91{
Varun Wadekarb7b45752015-12-28 14:55:41 -080092 unsigned int index, num_ints, irq_num;
93 uint8_t target_cpus;
94 uint32_t val;
Varun Wadekarb316e242015-05-19 16:48:04 +053095
96 /*
97 * Mark out non-secure interrupts. Calculate number of
98 * IGROUPR registers to consider. Will be equal to the
99 * number of IT_LINES
100 */
101 num_ints = gicd_read_typer(gicd_base) & IT_LINES_NO_MASK;
Varun Wadekard3a41502015-06-16 11:23:00 +0530102 num_ints = (num_ints + 1) << 5;
103 for (index = MIN_SPI_ID; index < num_ints; index += 32)
104 gicd_write_igroupr(gicd_base, index, ~0);
105
106 /* Setup SPI priorities doing four at a time */
107 for (index = MIN_SPI_ID; index < num_ints; index += 4) {
108 gicd_write_ipriorityr(gicd_base, index,
109 GICD_IPRIORITYR_DEF_VAL);
110 }
111
Varun Wadekarb7b45752015-12-28 14:55:41 -0800112 /* Configure SPI secure interrupts now */
113 if (g_irq_sec_ptr) {
114
Varun Wadekarb7b45752015-12-28 14:55:41 -0800115 for (index = 0; index < g_num_irqs; index++) {
Varun Wadekarc6c386d2016-05-20 16:21:22 -0700116 irq_num = (g_irq_sec_ptr + index)->irq;
117 target_cpus = (g_irq_sec_ptr + index)->target_cpus;
Varun Wadekarb7b45752015-12-28 14:55:41 -0800118
119 if (irq_num >= MIN_SPI_ID) {
120
121 /* Configure as a secure interrupt */
122 gicd_clr_igroupr(gicd_base, irq_num);
123
124 /* Configure SPI priority */
125 mmio_write_8(gicd_base + GICD_IPRIORITYR +
126 irq_num,
127 GIC_HIGHEST_SEC_PRIORITY &
128 GIC_PRI_MASK);
129
130 /* Configure as level triggered */
131 val = gicd_read_icfgr(gicd_base, irq_num);
132 val |= (3 << ((irq_num & 0xF) << 1));
133 gicd_write_icfgr(gicd_base, irq_num, val);
134
135 /* Route SPI to the target CPUs */
136 gicd_set_itargetsr(gicd_base, irq_num,
137 target_cpus);
138
139 /* Enable this interrupt */
140 gicd_set_isenabler(gicd_base, irq_num);
141 }
142 }
143 }
144
Varun Wadekard3a41502015-06-16 11:23:00 +0530145 /*
146 * Configure the SGI and PPI. This is done in a separated function
147 * because each CPU is responsible for initializing its own private
148 * interrupts.
149 */
150 tegra_gic_pcpu_distif_setup(gicd_base);
Varun Wadekarb316e242015-05-19 16:48:04 +0530151
152 /* enable distributor */
153 gicd_write_ctlr(gicd_base, ENABLE_GRP0 | ENABLE_GRP1);
154}
155
Varun Wadekarc6c386d2016-05-20 16:21:22 -0700156void tegra_gic_setup(const irq_sec_cfg_t *irq_sec_ptr, unsigned int num_irqs)
Varun Wadekarb316e242015-05-19 16:48:04 +0530157{
Varun Wadekarb7b45752015-12-28 14:55:41 -0800158 g_irq_sec_ptr = irq_sec_ptr;
159 g_num_irqs = num_irqs;
160
Varun Wadekarb316e242015-05-19 16:48:04 +0530161 tegra_gic_cpuif_setup(TEGRA_GICC_BASE);
162 tegra_gic_distif_setup(TEGRA_GICD_BASE);
163}
Varun Wadekard3a41502015-06-16 11:23:00 +0530164
165/*******************************************************************************
166 * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins.
167 * The interrupt controller knows which pin/line it uses to signal a type of
168 * interrupt. This function provides a common implementation of
169 * plat_interrupt_type_to_line() in an ARM GIC environment for optional re-use
170 * across platforms. It lets the interrupt management framework determine
171 * for a type of interrupt and security state, which line should be used in the
172 * SCR_EL3 to control its routing to EL3. The interrupt line is represented as
173 * the bit position of the IRQ or FIQ bit in the SCR_EL3.
174 ******************************************************************************/
175uint32_t tegra_gic_interrupt_type_to_line(uint32_t type,
176 uint32_t security_state)
177{
178 assert(type == INTR_TYPE_S_EL1 ||
179 type == INTR_TYPE_EL3 ||
180 type == INTR_TYPE_NS);
181
182 assert(sec_state_is_valid(security_state));
183
184 /*
185 * We ignore the security state parameter under the assumption that
186 * both normal and secure worlds are using ARM GICv2. This parameter
187 * will be used when the secure world starts using GICv3.
188 */
189#if ARM_GIC_ARCH == 2
190 return gicv2_interrupt_type_to_line(TEGRA_GICC_BASE, type);
191#else
192#error "Invalid ARM GIC architecture version specified for platform port"
193#endif /* ARM_GIC_ARCH */
194}
195
196#if ARM_GIC_ARCH == 2
197/*******************************************************************************
198 * This function returns the type of the highest priority pending interrupt at
199 * the GIC cpu interface. INTR_TYPE_INVAL is returned when there is no
200 * interrupt pending.
201 ******************************************************************************/
202uint32_t tegra_gic_get_pending_interrupt_type(void)
203{
204 uint32_t id;
Varun Wadekarc6c386d2016-05-20 16:21:22 -0700205 unsigned int index;
Varun Wadekard3a41502015-06-16 11:23:00 +0530206
207 id = gicc_read_hppir(TEGRA_GICC_BASE) & INT_ID_MASK;
208
Varun Wadekarc6c386d2016-05-20 16:21:22 -0700209 /* get the interrupt type */
210 if (id < 1022) {
211 for (index = 0; index < g_num_irqs; index++) {
212 if (id == (g_irq_sec_ptr + index)->irq)
213 return (g_irq_sec_ptr + index)->type;
214 }
215 }
Varun Wadekard3a41502015-06-16 11:23:00 +0530216
217 if (id == GIC_SPURIOUS_INTERRUPT)
218 return INTR_TYPE_INVAL;
219
220 return INTR_TYPE_NS;
221}
222
223/*******************************************************************************
224 * This function returns the id of the highest priority pending interrupt at
225 * the GIC cpu interface. INTR_ID_UNAVAILABLE is returned when there is no
226 * interrupt pending.
227 ******************************************************************************/
228uint32_t tegra_gic_get_pending_interrupt_id(void)
229{
230 uint32_t id;
231
232 id = gicc_read_hppir(TEGRA_GICC_BASE) & INT_ID_MASK;
233
234 if (id < 1022)
235 return id;
236
237 if (id == 1023)
238 return INTR_ID_UNAVAILABLE;
239
240 /*
241 * Find out which non-secure interrupt it is under the assumption that
242 * the GICC_CTLR.AckCtl bit is 0.
243 */
244 return gicc_read_ahppir(TEGRA_GICC_BASE) & INT_ID_MASK;
245}
246
247/*******************************************************************************
248 * This functions reads the GIC cpu interface Interrupt Acknowledge register
249 * to start handling the pending interrupt. It returns the contents of the IAR.
250 ******************************************************************************/
251uint32_t tegra_gic_acknowledge_interrupt(void)
252{
253 return gicc_read_IAR(TEGRA_GICC_BASE);
254}
255
256/*******************************************************************************
257 * This functions writes the GIC cpu interface End Of Interrupt register with
258 * the passed value to finish handling the active interrupt
259 ******************************************************************************/
260void tegra_gic_end_of_interrupt(uint32_t id)
261{
262 gicc_write_EOIR(TEGRA_GICC_BASE, id);
263}
264
265/*******************************************************************************
266 * This function returns the type of the interrupt id depending upon the group
267 * this interrupt has been configured under by the interrupt controller i.e.
268 * group0 or group1.
269 ******************************************************************************/
270uint32_t tegra_gic_get_interrupt_type(uint32_t id)
271{
272 uint32_t group;
Varun Wadekarc6c386d2016-05-20 16:21:22 -0700273 unsigned int index;
Varun Wadekard3a41502015-06-16 11:23:00 +0530274
275 group = gicd_get_igroupr(TEGRA_GICD_BASE, id);
276
Varun Wadekarc6c386d2016-05-20 16:21:22 -0700277 /* get the interrupt type */
278 if (group == GRP0) {
279 for (index = 0; index < g_num_irqs; index++) {
280 if (id == (g_irq_sec_ptr + index)->irq)
281 return (g_irq_sec_ptr + index)->type;
282 }
283 }
284
285 return INTR_TYPE_NS;
Varun Wadekard3a41502015-06-16 11:23:00 +0530286}
287
288#else
289#error "Invalid ARM GIC architecture version specified for platform port"
290#endif /* ARM_GIC_ARCH */
291
292uint32_t plat_ic_get_pending_interrupt_id(void)
293{
294 return tegra_gic_get_pending_interrupt_id();
295}
296
297uint32_t plat_ic_get_pending_interrupt_type(void)
298{
299 return tegra_gic_get_pending_interrupt_type();
300}
301
302uint32_t plat_ic_acknowledge_interrupt(void)
303{
304 return tegra_gic_acknowledge_interrupt();
305}
306
307uint32_t plat_ic_get_interrupt_type(uint32_t id)
308{
309 return tegra_gic_get_interrupt_type(id);
310}
311
312void plat_ic_end_of_interrupt(uint32_t id)
313{
314 tegra_gic_end_of_interrupt(id);
315}
316
317uint32_t plat_interrupt_type_to_line(uint32_t type,
318 uint32_t security_state)
319{
320 return tegra_gic_interrupt_type_to_line(type, security_state);
321}