Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 1 | /* |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <arch_helpers.h> |
| 9 | #include <assert.h> |
| 10 | #include <debug.h> |
| 11 | #include <gic_common.h> |
| 12 | #include <gicv2.h> |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 13 | #include <interrupt_props.h> |
Jeenu Viswambharan | c06f05c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 14 | #include <spinlock.h> |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 15 | #include <stdbool.h> |
| 16 | |
Soby Mathew | 50f6fe4 | 2016-02-01 17:59:22 +0000 | [diff] [blame] | 17 | #include "../common/gic_common_private.h" |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 18 | #include "gicv2_private.h" |
| 19 | |
| 20 | static const gicv2_driver_data_t *driver_data; |
| 21 | |
Jeenu Viswambharan | c06f05c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 22 | /* |
| 23 | * Spinlock to guard registers needing read-modify-write. APIs protected by this |
| 24 | * spinlock are used either at boot time (when only a single CPU is active), or |
| 25 | * when the system is fully coherent. |
| 26 | */ |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 27 | static spinlock_t gic_lock; |
Jeenu Viswambharan | c06f05c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 28 | |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 29 | /******************************************************************************* |
| 30 | * Enable secure interrupts and use FIQs to route them. Disable legacy bypass |
| 31 | * and set the priority mask register to allow all interrupts to trickle in. |
| 32 | ******************************************************************************/ |
| 33 | void gicv2_cpuif_enable(void) |
| 34 | { |
| 35 | unsigned int val; |
| 36 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 37 | assert(driver_data != NULL); |
| 38 | assert(driver_data->gicc_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Enable the Group 0 interrupts, FIQEn and disable Group 0/1 |
| 42 | * bypass. |
| 43 | */ |
| 44 | val = CTLR_ENABLE_G0_BIT | FIQ_EN_BIT | FIQ_BYP_DIS_GRP0; |
| 45 | val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1; |
| 46 | |
| 47 | /* Program the idle priority in the PMR */ |
| 48 | gicc_write_pmr(driver_data->gicc_base, GIC_PRI_MASK); |
| 49 | gicc_write_ctlr(driver_data->gicc_base, val); |
| 50 | } |
| 51 | |
| 52 | /******************************************************************************* |
| 53 | * Place the cpu interface in a state where it can never make a cpu exit wfi as |
| 54 | * as result of an asserted interrupt. This is critical for powering down a cpu |
| 55 | ******************************************************************************/ |
| 56 | void gicv2_cpuif_disable(void) |
| 57 | { |
| 58 | unsigned int val; |
| 59 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 60 | assert(driver_data != NULL); |
| 61 | assert(driver_data->gicc_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 62 | |
| 63 | /* Disable secure, non-secure interrupts and disable their bypass */ |
| 64 | val = gicc_read_ctlr(driver_data->gicc_base); |
| 65 | val &= ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT); |
| 66 | val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0; |
| 67 | val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1; |
| 68 | gicc_write_ctlr(driver_data->gicc_base, val); |
| 69 | } |
| 70 | |
| 71 | /******************************************************************************* |
| 72 | * Per cpu gic distributor setup which will be done by all cpus after a cold |
| 73 | * boot/hotplug. This marks out the secure SPIs and PPIs & enables them. |
| 74 | ******************************************************************************/ |
| 75 | void gicv2_pcpu_distif_init(void) |
| 76 | { |
Jeenu Viswambharan | 88d8f45 | 2017-11-07 08:38:23 +0000 | [diff] [blame] | 77 | unsigned int ctlr; |
| 78 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 79 | assert(driver_data != NULL); |
| 80 | assert(driver_data->gicd_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 81 | |
Antonio Nino Diaz | 29b9f5b | 2018-09-24 17:23:24 +0100 | [diff] [blame^] | 82 | gicv2_secure_ppi_sgi_setup_props(driver_data->gicd_base, |
| 83 | driver_data->interrupt_props, |
| 84 | driver_data->interrupt_props_num); |
Jeenu Viswambharan | 88d8f45 | 2017-11-07 08:38:23 +0000 | [diff] [blame] | 85 | |
| 86 | /* Enable G0 interrupts if not already */ |
| 87 | ctlr = gicd_read_ctlr(driver_data->gicd_base); |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 88 | if ((ctlr & CTLR_ENABLE_G0_BIT) == 0U) { |
Jeenu Viswambharan | 88d8f45 | 2017-11-07 08:38:23 +0000 | [diff] [blame] | 89 | gicd_write_ctlr(driver_data->gicd_base, |
| 90 | ctlr | CTLR_ENABLE_G0_BIT); |
| 91 | } |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /******************************************************************************* |
| 95 | * Global gic distributor init which will be done by the primary cpu after a |
| 96 | * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It |
| 97 | * then enables the secure GIC distributor interface. |
| 98 | ******************************************************************************/ |
| 99 | void gicv2_distif_init(void) |
| 100 | { |
| 101 | unsigned int ctlr; |
| 102 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 103 | assert(driver_data != NULL); |
| 104 | assert(driver_data->gicd_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 105 | |
| 106 | /* Disable the distributor before going further */ |
| 107 | ctlr = gicd_read_ctlr(driver_data->gicd_base); |
| 108 | gicd_write_ctlr(driver_data->gicd_base, |
| 109 | ctlr & ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT)); |
| 110 | |
| 111 | /* Set the default attribute of all SPIs */ |
| 112 | gicv2_spis_configure_defaults(driver_data->gicd_base); |
| 113 | |
Antonio Nino Diaz | 29b9f5b | 2018-09-24 17:23:24 +0100 | [diff] [blame^] | 114 | gicv2_secure_spis_configure_props(driver_data->gicd_base, |
| 115 | driver_data->interrupt_props, |
| 116 | driver_data->interrupt_props_num); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 117 | |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 118 | |
| 119 | /* Re-enable the secure SPIs now that they have been configured */ |
| 120 | gicd_write_ctlr(driver_data->gicd_base, ctlr | CTLR_ENABLE_G0_BIT); |
| 121 | } |
| 122 | |
| 123 | /******************************************************************************* |
| 124 | * Initialize the ARM GICv2 driver with the provided platform inputs |
| 125 | ******************************************************************************/ |
| 126 | void gicv2_driver_init(const gicv2_driver_data_t *plat_driver_data) |
| 127 | { |
| 128 | unsigned int gic_version; |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 129 | |
| 130 | assert(plat_driver_data != NULL); |
| 131 | assert(plat_driver_data->gicd_base != 0U); |
| 132 | assert(plat_driver_data->gicc_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 133 | |
Samuel Holland | 694f81f | 2017-11-09 12:07:53 -0600 | [diff] [blame] | 134 | assert(plat_driver_data->interrupt_props_num > 0 ? |
| 135 | plat_driver_data->interrupt_props != NULL : 1); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 136 | |
| 137 | /* Ensure that this is a GICv2 system */ |
| 138 | gic_version = gicd_read_pidr2(plat_driver_data->gicd_base); |
| 139 | gic_version = (gic_version >> PIDR2_ARCH_REV_SHIFT) |
| 140 | & PIDR2_ARCH_REV_MASK; |
Etienne Carriere | 0a8c353 | 2017-11-05 22:57:38 +0100 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * GICv1 with security extension complies with trusted firmware |
| 144 | * GICv2 driver as far as virtualization and few tricky power |
| 145 | * features are not used. GICv2 features that are not supported |
| 146 | * by GICv1 with Security Extensions are: |
| 147 | * - virtual interrupt support. |
| 148 | * - wake up events. |
| 149 | * - writeable GIC state register (for power sequences) |
| 150 | * - interrupt priority drop. |
| 151 | * - interrupt signal bypass. |
| 152 | */ |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 153 | assert((gic_version == ARCH_REV_GICV2) || |
| 154 | (gic_version == ARCH_REV_GICV1)); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 155 | |
| 156 | driver_data = plat_driver_data; |
| 157 | |
Soby Mathew | 7264513 | 2017-02-14 10:11:52 +0000 | [diff] [blame] | 158 | /* |
| 159 | * The GIC driver data is initialized by the primary CPU with caches |
| 160 | * enabled. When the secondary CPU boots up, it initializes the |
| 161 | * GICC/GICR interface with the caches disabled. Hence flush the |
| 162 | * driver_data to ensure coherency. This is not required if the |
Andrew F. Davis | 4d23a64 | 2018-07-26 13:50:14 -0500 | [diff] [blame] | 163 | * platform has HW_ASSISTED_COHERENCY or WARMBOOT_ENABLE_DCACHE_EARLY |
| 164 | * enabled. |
Soby Mathew | 7264513 | 2017-02-14 10:11:52 +0000 | [diff] [blame] | 165 | */ |
Andrew F. Davis | 4d23a64 | 2018-07-26 13:50:14 -0500 | [diff] [blame] | 166 | #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) |
Soby Mathew | 7264513 | 2017-02-14 10:11:52 +0000 | [diff] [blame] | 167 | flush_dcache_range((uintptr_t) &driver_data, sizeof(driver_data)); |
| 168 | flush_dcache_range((uintptr_t) driver_data, sizeof(*driver_data)); |
| 169 | #endif |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 170 | INFO("ARM GICv2 driver initialized\n"); |
| 171 | } |
| 172 | |
| 173 | /****************************************************************************** |
| 174 | * This function returns whether FIQ is enabled in the GIC CPU interface. |
| 175 | *****************************************************************************/ |
| 176 | unsigned int gicv2_is_fiq_enabled(void) |
| 177 | { |
| 178 | unsigned int gicc_ctlr; |
| 179 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 180 | assert(driver_data != NULL); |
| 181 | assert(driver_data->gicc_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 182 | |
| 183 | gicc_ctlr = gicc_read_ctlr(driver_data->gicc_base); |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 184 | return (gicc_ctlr >> FIQ_EN_SHIFT) & 0x1U; |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /******************************************************************************* |
| 188 | * This function returns the type of the highest priority pending interrupt at |
| 189 | * the GIC cpu interface. The return values can be one of the following : |
| 190 | * PENDING_G1_INTID : The interrupt type is non secure Group 1. |
| 191 | * 0 - 1019 : The interrupt type is secure Group 0. |
| 192 | * GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with |
| 193 | * sufficient priority to be signaled |
| 194 | ******************************************************************************/ |
| 195 | unsigned int gicv2_get_pending_interrupt_type(void) |
| 196 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 197 | assert(driver_data != NULL); |
| 198 | assert(driver_data->gicc_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 199 | |
| 200 | return gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK; |
| 201 | } |
| 202 | |
| 203 | /******************************************************************************* |
| 204 | * This function returns the id of the highest priority pending interrupt at |
| 205 | * the GIC cpu interface. GIC_SPURIOUS_INTERRUPT is returned when there is no |
| 206 | * interrupt pending. |
| 207 | ******************************************************************************/ |
| 208 | unsigned int gicv2_get_pending_interrupt_id(void) |
| 209 | { |
| 210 | unsigned int id; |
| 211 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 212 | assert(driver_data != NULL); |
| 213 | assert(driver_data->gicc_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 214 | |
| 215 | id = gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK; |
| 216 | |
| 217 | /* |
| 218 | * Find out which non-secure interrupt it is under the assumption that |
| 219 | * the GICC_CTLR.AckCtl bit is 0. |
| 220 | */ |
| 221 | if (id == PENDING_G1_INTID) |
| 222 | id = gicc_read_ahppir(driver_data->gicc_base) & INT_ID_MASK; |
| 223 | |
| 224 | return id; |
| 225 | } |
| 226 | |
| 227 | /******************************************************************************* |
| 228 | * This functions reads the GIC cpu interface Interrupt Acknowledge register |
| 229 | * to start handling the pending secure 0 interrupt. It returns the |
| 230 | * contents of the IAR. |
| 231 | ******************************************************************************/ |
| 232 | unsigned int gicv2_acknowledge_interrupt(void) |
| 233 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 234 | assert(driver_data != NULL); |
| 235 | assert(driver_data->gicc_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 236 | |
| 237 | return gicc_read_IAR(driver_data->gicc_base); |
| 238 | } |
| 239 | |
| 240 | /******************************************************************************* |
| 241 | * This functions writes the GIC cpu interface End Of Interrupt register with |
| 242 | * the passed value to finish handling the active secure group 0 interrupt. |
| 243 | ******************************************************************************/ |
| 244 | void gicv2_end_of_interrupt(unsigned int id) |
| 245 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 246 | assert(driver_data != NULL); |
| 247 | assert(driver_data->gicc_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 248 | |
| 249 | gicc_write_EOIR(driver_data->gicc_base, id); |
| 250 | } |
| 251 | |
| 252 | /******************************************************************************* |
| 253 | * This function returns the type of the interrupt id depending upon the group |
| 254 | * this interrupt has been configured under by the interrupt controller i.e. |
| 255 | * group0 secure or group1 non secure. It returns zero for Group 0 secure and |
| 256 | * one for Group 1 non secure interrupt. |
| 257 | ******************************************************************************/ |
| 258 | unsigned int gicv2_get_interrupt_group(unsigned int id) |
| 259 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 260 | assert(driver_data != NULL); |
| 261 | assert(driver_data->gicd_base != 0U); |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 262 | |
| 263 | return gicd_get_igroupr(driver_data->gicd_base, id); |
| 264 | } |
Jeenu Viswambharan | b1e957e | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 265 | |
| 266 | /******************************************************************************* |
| 267 | * This function returns the priority of the interrupt the processor is |
| 268 | * currently servicing. |
| 269 | ******************************************************************************/ |
| 270 | unsigned int gicv2_get_running_priority(void) |
| 271 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 272 | assert(driver_data != NULL); |
| 273 | assert(driver_data->gicc_base != 0U); |
Jeenu Viswambharan | b1e957e | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 274 | |
| 275 | return gicc_read_rpr(driver_data->gicc_base); |
| 276 | } |
Jeenu Viswambharan | 393fdd9 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 277 | |
| 278 | /******************************************************************************* |
| 279 | * This function sets the GICv2 target mask pattern for the current PE. The PE |
| 280 | * target mask is used to translate linear PE index (returned by platform core |
| 281 | * position) to a bit mask used when targeting interrupts to a PE, viz. when |
| 282 | * raising SGIs and routing SPIs. |
| 283 | ******************************************************************************/ |
| 284 | void gicv2_set_pe_target_mask(unsigned int proc_num) |
| 285 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 286 | assert(driver_data != NULL); |
| 287 | assert(driver_data->gicd_base != 0U); |
| 288 | assert(driver_data->target_masks != NULL); |
| 289 | assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE); |
| 290 | assert((unsigned int)proc_num < driver_data->target_masks_num); |
Jeenu Viswambharan | 393fdd9 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 291 | |
| 292 | /* Return if the target mask is already populated */ |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 293 | if (driver_data->target_masks[proc_num] != 0U) |
Jeenu Viswambharan | 393fdd9 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 294 | return; |
| 295 | |
Jeenu Viswambharan | fbf5bda | 2017-11-07 16:10:19 +0000 | [diff] [blame] | 296 | /* |
| 297 | * Update target register corresponding to this CPU and flush for it to |
| 298 | * be visible to other CPUs. |
| 299 | */ |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 300 | if (driver_data->target_masks[proc_num] == 0U) { |
Jeenu Viswambharan | fbf5bda | 2017-11-07 16:10:19 +0000 | [diff] [blame] | 301 | driver_data->target_masks[proc_num] = |
| 302 | gicv2_get_cpuif_id(driver_data->gicd_base); |
Andrew F. Davis | 4d23a64 | 2018-07-26 13:50:14 -0500 | [diff] [blame] | 303 | #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) |
Jeenu Viswambharan | fbf5bda | 2017-11-07 16:10:19 +0000 | [diff] [blame] | 304 | /* |
| 305 | * PEs only update their own masks. Primary updates it with |
| 306 | * caches on. But because secondaries does it with caches off, |
| 307 | * all updates go to memory directly, and there's no danger of |
| 308 | * secondaries overwriting each others' mask, despite |
| 309 | * target_masks[] not being cache line aligned. |
| 310 | */ |
| 311 | flush_dcache_range((uintptr_t) |
| 312 | &driver_data->target_masks[proc_num], |
| 313 | sizeof(driver_data->target_masks[proc_num])); |
| 314 | #endif |
| 315 | } |
Jeenu Viswambharan | 393fdd9 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 316 | } |
Jeenu Viswambharan | 24e7029 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 317 | |
| 318 | /******************************************************************************* |
| 319 | * This function returns the active status of the interrupt (either because the |
| 320 | * state is active, or active and pending). |
| 321 | ******************************************************************************/ |
| 322 | unsigned int gicv2_get_interrupt_active(unsigned int id) |
| 323 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 324 | assert(driver_data != NULL); |
| 325 | assert(driver_data->gicd_base != 0U); |
Jeenu Viswambharan | 24e7029 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 326 | assert(id <= MAX_SPI_ID); |
| 327 | |
| 328 | return gicd_get_isactiver(driver_data->gicd_base, id); |
| 329 | } |
Jeenu Viswambharan | 0fcdfff | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 330 | |
| 331 | /******************************************************************************* |
| 332 | * This function enables the interrupt identified by id. |
| 333 | ******************************************************************************/ |
| 334 | void gicv2_enable_interrupt(unsigned int id) |
| 335 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 336 | assert(driver_data != NULL); |
| 337 | assert(driver_data->gicd_base != 0U); |
Jeenu Viswambharan | 0fcdfff | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 338 | assert(id <= MAX_SPI_ID); |
| 339 | |
| 340 | /* |
| 341 | * Ensure that any shared variable updates depending on out of band |
| 342 | * interrupt trigger are observed before enabling interrupt. |
| 343 | */ |
| 344 | dsbishst(); |
| 345 | gicd_set_isenabler(driver_data->gicd_base, id); |
| 346 | } |
| 347 | |
| 348 | /******************************************************************************* |
| 349 | * This function disables the interrupt identified by id. |
| 350 | ******************************************************************************/ |
| 351 | void gicv2_disable_interrupt(unsigned int id) |
| 352 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 353 | assert(driver_data != NULL); |
| 354 | assert(driver_data->gicd_base != 0U); |
Jeenu Viswambharan | 0fcdfff | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 355 | assert(id <= MAX_SPI_ID); |
| 356 | |
| 357 | /* |
| 358 | * Disable interrupt, and ensure that any shared variable updates |
| 359 | * depending on out of band interrupt trigger are observed afterwards. |
| 360 | */ |
| 361 | gicd_set_icenabler(driver_data->gicd_base, id); |
| 362 | dsbishst(); |
| 363 | } |
Jeenu Viswambharan | 447b89d | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 364 | |
| 365 | /******************************************************************************* |
| 366 | * This function sets the interrupt priority as supplied for the given interrupt |
| 367 | * id. |
| 368 | ******************************************************************************/ |
| 369 | void gicv2_set_interrupt_priority(unsigned int id, unsigned int priority) |
| 370 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 371 | assert(driver_data != NULL); |
| 372 | assert(driver_data->gicd_base != 0U); |
Jeenu Viswambharan | 447b89d | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 373 | assert(id <= MAX_SPI_ID); |
| 374 | |
| 375 | gicd_set_ipriorityr(driver_data->gicd_base, id, priority); |
| 376 | } |
Jeenu Viswambharan | c06f05c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 377 | |
| 378 | /******************************************************************************* |
| 379 | * This function assigns group for the interrupt identified by id. The group can |
| 380 | * be any of GICV2_INTR_GROUP* |
| 381 | ******************************************************************************/ |
| 382 | void gicv2_set_interrupt_type(unsigned int id, unsigned int type) |
| 383 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 384 | assert(driver_data != NULL); |
| 385 | assert(driver_data->gicd_base != 0U); |
Jeenu Viswambharan | c06f05c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 386 | assert(id <= MAX_SPI_ID); |
| 387 | |
| 388 | /* Serialize read-modify-write to Distributor registers */ |
| 389 | spin_lock(&gic_lock); |
| 390 | switch (type) { |
| 391 | case GICV2_INTR_GROUP1: |
| 392 | gicd_set_igroupr(driver_data->gicd_base, id); |
| 393 | break; |
| 394 | case GICV2_INTR_GROUP0: |
| 395 | gicd_clr_igroupr(driver_data->gicd_base, id); |
| 396 | break; |
| 397 | default: |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 398 | assert(false); |
Jonathan Wright | 39b4221 | 2018-03-13 15:24:29 +0000 | [diff] [blame] | 399 | break; |
Jeenu Viswambharan | c06f05c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 400 | } |
| 401 | spin_unlock(&gic_lock); |
| 402 | } |
Jeenu Viswambharan | ab14e9b | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 403 | |
| 404 | /******************************************************************************* |
| 405 | * This function raises the specified SGI to requested targets. |
| 406 | * |
| 407 | * The proc_num parameter must be the linear index of the target PE in the |
| 408 | * system. |
| 409 | ******************************************************************************/ |
| 410 | void gicv2_raise_sgi(int sgi_num, int proc_num) |
| 411 | { |
| 412 | unsigned int sgir_val, target; |
| 413 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 414 | assert(driver_data != NULL); |
| 415 | assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE); |
| 416 | assert(driver_data->gicd_base != 0U); |
Jeenu Viswambharan | ab14e9b | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * Target masks array must have been supplied, and the core position |
| 420 | * should be valid. |
| 421 | */ |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 422 | assert(driver_data->target_masks != NULL); |
| 423 | assert((unsigned int)proc_num < driver_data->target_masks_num); |
Jeenu Viswambharan | ab14e9b | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 424 | |
| 425 | /* Don't raise SGI if the mask hasn't been populated */ |
| 426 | target = driver_data->target_masks[proc_num]; |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 427 | assert(target != 0U); |
Jeenu Viswambharan | ab14e9b | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 428 | |
| 429 | sgir_val = GICV2_SGIR_VALUE(SGIR_TGT_SPECIFIC, target, sgi_num); |
| 430 | |
| 431 | /* |
| 432 | * Ensure that any shared variable updates depending on out of band |
| 433 | * interrupt trigger are observed before raising SGI. |
| 434 | */ |
| 435 | dsbishst(); |
| 436 | gicd_write_sgir(driver_data->gicd_base, sgir_val); |
| 437 | } |
Jeenu Viswambharan | dce70b3 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 438 | |
| 439 | /******************************************************************************* |
| 440 | * This function sets the interrupt routing for the given SPI interrupt id. |
| 441 | * The interrupt routing is specified in routing mode. The proc_num parameter is |
| 442 | * linear index of the PE to target SPI. When proc_num < 0, the SPI may target |
| 443 | * all PEs. |
| 444 | ******************************************************************************/ |
| 445 | void gicv2_set_spi_routing(unsigned int id, int proc_num) |
| 446 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 447 | unsigned int target; |
Jeenu Viswambharan | dce70b3 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 448 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 449 | assert(driver_data != NULL); |
| 450 | assert(driver_data->gicd_base != 0U); |
Jeenu Viswambharan | dce70b3 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 451 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 452 | assert((id >= MIN_SPI_ID) && (id <= MAX_SPI_ID)); |
Jeenu Viswambharan | dce70b3 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | * Target masks array must have been supplied, and the core position |
| 456 | * should be valid. |
| 457 | */ |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 458 | assert(driver_data->target_masks != NULL); |
| 459 | assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE); |
| 460 | assert((unsigned int)proc_num < driver_data->target_masks_num); |
Jeenu Viswambharan | dce70b3 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 461 | |
| 462 | if (proc_num < 0) { |
| 463 | /* Target all PEs */ |
| 464 | target = GIC_TARGET_CPU_MASK; |
| 465 | } else { |
| 466 | /* Don't route interrupt if the mask hasn't been populated */ |
| 467 | target = driver_data->target_masks[proc_num]; |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 468 | assert(target != 0U); |
Jeenu Viswambharan | dce70b3 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | gicd_set_itargetsr(driver_data->gicd_base, id, target); |
| 472 | } |
Jeenu Viswambharan | eb1c12c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 473 | |
| 474 | /******************************************************************************* |
| 475 | * This function clears the pending status of an interrupt identified by id. |
| 476 | ******************************************************************************/ |
| 477 | void gicv2_clear_interrupt_pending(unsigned int id) |
| 478 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 479 | assert(driver_data != NULL); |
| 480 | assert(driver_data->gicd_base != 0U); |
Jeenu Viswambharan | eb1c12c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 481 | |
| 482 | /* SGIs can't be cleared pending */ |
| 483 | assert(id >= MIN_PPI_ID); |
| 484 | |
| 485 | /* |
| 486 | * Clear pending interrupt, and ensure that any shared variable updates |
| 487 | * depending on out of band interrupt trigger are observed afterwards. |
| 488 | */ |
| 489 | gicd_set_icpendr(driver_data->gicd_base, id); |
| 490 | dsbishst(); |
| 491 | } |
| 492 | |
| 493 | /******************************************************************************* |
| 494 | * This function sets the pending status of an interrupt identified by id. |
| 495 | ******************************************************************************/ |
| 496 | void gicv2_set_interrupt_pending(unsigned int id) |
| 497 | { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 498 | assert(driver_data != NULL); |
| 499 | assert(driver_data->gicd_base != 0U); |
Jeenu Viswambharan | eb1c12c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 500 | |
| 501 | /* SGIs can't be cleared pending */ |
| 502 | assert(id >= MIN_PPI_ID); |
| 503 | |
| 504 | /* |
| 505 | * Ensure that any shared variable updates depending on out of band |
| 506 | * interrupt trigger are observed before setting interrupt pending. |
| 507 | */ |
| 508 | dsbishst(); |
| 509 | gicd_set_ispendr(driver_data->gicd_base, id); |
| 510 | } |
Jeenu Viswambharan | 6250507 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 511 | |
| 512 | /******************************************************************************* |
| 513 | * This function sets the PMR register with the supplied value. Returns the |
| 514 | * original PMR. |
| 515 | ******************************************************************************/ |
| 516 | unsigned int gicv2_set_pmr(unsigned int mask) |
| 517 | { |
| 518 | unsigned int old_mask; |
| 519 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 520 | assert(driver_data != NULL); |
| 521 | assert(driver_data->gicc_base != 0U); |
Jeenu Viswambharan | 6250507 | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 522 | |
| 523 | old_mask = gicc_read_pmr(driver_data->gicc_base); |
| 524 | |
| 525 | /* |
| 526 | * Order memory updates w.r.t. PMR write, and ensure they're visible |
| 527 | * before potential out of band interrupt trigger because of PMR update. |
| 528 | */ |
| 529 | dmbishst(); |
| 530 | gicc_write_pmr(driver_data->gicc_base, mask); |
| 531 | dsbishst(); |
| 532 | |
| 533 | return old_mask; |
| 534 | } |
Marcin Wojtas | dd568dd | 2018-03-21 09:55:47 +0100 | [diff] [blame] | 535 | |
| 536 | /******************************************************************************* |
| 537 | * This function updates single interrupt configuration to be level/edge |
| 538 | * triggered |
| 539 | ******************************************************************************/ |
| 540 | void gicv2_interrupt_set_cfg(unsigned int id, unsigned int cfg) |
| 541 | { |
| 542 | gicd_set_icfgr(driver_data->gicd_base, id, cfg); |
| 543 | } |