Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 1 | /* |
Zelalem | 87675d4 | 2020-02-03 14:56:42 -0600 | [diff] [blame] | 2 | * Copyright (c) 2015-2020, 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 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 9 | #include <arch.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <common/debug.h> |
| 11 | #include <common/interrupt_props.h> |
| 12 | #include <drivers/arm/gic_common.h> |
| 13 | #include <drivers/arm/gicv2.h> |
Nithin G | 74d33e4 | 2024-04-23 15:29:52 +0530 | [diff] [blame] | 14 | #include <lib/utils_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 15 | |
Soby Mathew | 50f6fe4 | 2016-02-01 17:59:22 +0000 | [diff] [blame] | 16 | #include "../common/gic_common_private.h" |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 17 | #include "gicv2_private.h" |
| 18 | |
| 19 | /* |
| 20 | * Accessor to read the GIC Distributor ITARGETSR corresponding to the |
| 21 | * interrupt `id`, 4 interrupt IDs at a time. |
| 22 | */ |
| 23 | unsigned int gicd_read_itargetsr(uintptr_t base, unsigned int id) |
| 24 | { |
| 25 | unsigned n = id >> ITARGETSR_SHIFT; |
| 26 | return mmio_read_32(base + GICD_ITARGETSR + (n << 2)); |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * Accessor to read the GIC Distributor CPENDSGIR corresponding to the |
| 31 | * interrupt `id`, 4 interrupt IDs at a time. |
| 32 | */ |
| 33 | unsigned int gicd_read_cpendsgir(uintptr_t base, unsigned int id) |
| 34 | { |
| 35 | unsigned n = id >> CPENDSGIR_SHIFT; |
| 36 | return mmio_read_32(base + GICD_CPENDSGIR + (n << 2)); |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Accessor to read the GIC Distributor SPENDSGIR corresponding to the |
| 41 | * interrupt `id`, 4 interrupt IDs at a time. |
| 42 | */ |
| 43 | unsigned int gicd_read_spendsgir(uintptr_t base, unsigned int id) |
| 44 | { |
| 45 | unsigned n = id >> SPENDSGIR_SHIFT; |
| 46 | return mmio_read_32(base + GICD_SPENDSGIR + (n << 2)); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Accessor to write the GIC Distributor ITARGETSR corresponding to the |
| 51 | * interrupt `id`, 4 interrupt IDs at a time. |
| 52 | */ |
| 53 | void gicd_write_itargetsr(uintptr_t base, unsigned int id, unsigned int val) |
| 54 | { |
| 55 | unsigned n = id >> ITARGETSR_SHIFT; |
| 56 | mmio_write_32(base + GICD_ITARGETSR + (n << 2), val); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Accessor to write the GIC Distributor CPENDSGIR corresponding to the |
| 61 | * interrupt `id`, 4 interrupt IDs at a time. |
| 62 | */ |
| 63 | void gicd_write_cpendsgir(uintptr_t base, unsigned int id, unsigned int val) |
| 64 | { |
| 65 | unsigned n = id >> CPENDSGIR_SHIFT; |
| 66 | mmio_write_32(base + GICD_CPENDSGIR + (n << 2), val); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Accessor to write the GIC Distributor SPENDSGIR corresponding to the |
| 71 | * interrupt `id`, 4 interrupt IDs at a time. |
| 72 | */ |
| 73 | void gicd_write_spendsgir(uintptr_t base, unsigned int id, unsigned int val) |
| 74 | { |
| 75 | unsigned n = id >> SPENDSGIR_SHIFT; |
| 76 | mmio_write_32(base + GICD_SPENDSGIR + (n << 2), val); |
| 77 | } |
| 78 | |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 79 | /******************************************************************************* |
| 80 | * Get the current CPU bit mask from GICD_ITARGETSR0 |
| 81 | ******************************************************************************/ |
| 82 | unsigned int gicv2_get_cpuif_id(uintptr_t base) |
| 83 | { |
| 84 | unsigned int val; |
| 85 | |
| 86 | val = gicd_read_itargetsr(base, 0); |
| 87 | return val & GIC_TARGET_CPU_MASK; |
| 88 | } |
| 89 | |
| 90 | /******************************************************************************* |
| 91 | * Helper function to configure the default attributes of SPIs. |
| 92 | ******************************************************************************/ |
| 93 | void gicv2_spis_configure_defaults(uintptr_t gicd_base) |
| 94 | { |
| 95 | unsigned int index, num_ints; |
| 96 | |
| 97 | num_ints = gicd_read_typer(gicd_base); |
| 98 | num_ints &= TYPER_IT_LINES_NO_MASK; |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 99 | num_ints = (num_ints + 1U) << 5; |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * Treat all SPIs as G1NS by default. The number of interrupts is |
| 103 | * calculated as 32 * (IT_LINES + 1). We do 32 at a time. |
| 104 | */ |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 105 | for (index = MIN_SPI_ID; index < num_ints; index += 32U) { |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 106 | gicd_write_igroupr(gicd_base, index, ~0U); |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 107 | } |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 108 | /* Setup the default SPI priorities doing four at a time */ |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 109 | for (index = MIN_SPI_ID; index < num_ints; index += 4U) { |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 110 | gicd_write_ipriorityr(gicd_base, |
| 111 | index, |
| 112 | GICD_IPRIORITYR_DEF_VAL); |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 113 | } |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 114 | /* Treat all SPIs as level triggered by default, 16 at a time */ |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 115 | for (index = MIN_SPI_ID; index < num_ints; index += 16U) { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 116 | gicd_write_icfgr(gicd_base, index, 0U); |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 117 | } |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Soby Mathew | e063d3c | 2015-10-07 09:45:27 +0100 | [diff] [blame] | 120 | /******************************************************************************* |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 121 | * Helper function to configure properties of secure G0 SPIs. |
| 122 | ******************************************************************************/ |
| 123 | void gicv2_secure_spis_configure_props(uintptr_t gicd_base, |
| 124 | const interrupt_prop_t *interrupt_props, |
| 125 | unsigned int interrupt_props_num) |
| 126 | { |
| 127 | unsigned int i; |
| 128 | const interrupt_prop_t *prop_desc; |
| 129 | |
| 130 | /* Make sure there's a valid property array */ |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 131 | if (interrupt_props_num != 0U) { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 132 | assert(interrupt_props != NULL); |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 133 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 134 | for (i = 0; i < interrupt_props_num; i++) { |
| 135 | prop_desc = &interrupt_props[i]; |
| 136 | |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 137 | if (prop_desc->intr_num < MIN_SPI_ID) { |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 138 | continue; |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 139 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 140 | /* Configure this interrupt as a secure interrupt */ |
| 141 | assert(prop_desc->intr_grp == GICV2_INTR_GROUP0); |
| 142 | gicd_clr_igroupr(gicd_base, prop_desc->intr_num); |
| 143 | |
| 144 | /* Set the priority of this interrupt */ |
| 145 | gicd_set_ipriorityr(gicd_base, prop_desc->intr_num, |
| 146 | prop_desc->intr_pri); |
| 147 | |
| 148 | /* Target the secure interrupts to primary CPU */ |
| 149 | gicd_set_itargetsr(gicd_base, prop_desc->intr_num, |
| 150 | gicv2_get_cpuif_id(gicd_base)); |
| 151 | |
| 152 | /* Set interrupt configuration */ |
| 153 | gicd_set_icfgr(gicd_base, prop_desc->intr_num, |
| 154 | prop_desc->intr_cfg); |
| 155 | |
| 156 | /* Enable this interrupt */ |
| 157 | gicd_set_isenabler(gicd_base, prop_desc->intr_num); |
| 158 | } |
| 159 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 160 | |
| 161 | /******************************************************************************* |
| 162 | * Helper function to configure properties of secure G0 SGIs and PPIs. |
| 163 | ******************************************************************************/ |
| 164 | void gicv2_secure_ppi_sgi_setup_props(uintptr_t gicd_base, |
| 165 | const interrupt_prop_t *interrupt_props, |
| 166 | unsigned int interrupt_props_num) |
| 167 | { |
| 168 | unsigned int i; |
| 169 | uint32_t sec_ppi_sgi_mask = 0; |
| 170 | const interrupt_prop_t *prop_desc; |
| 171 | |
| 172 | /* Make sure there's a valid property array */ |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 173 | if (interrupt_props_num != 0U) { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 174 | assert(interrupt_props != NULL); |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 175 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 176 | /* |
| 177 | * Disable all SGIs (imp. def.)/PPIs before configuring them. This is a |
| 178 | * more scalable approach as it avoids clearing the enable bits in the |
| 179 | * GICD_CTLR. |
| 180 | */ |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 181 | gicd_write_icenabler(gicd_base, 0U, ~0U); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 182 | |
| 183 | /* Setup the default PPI/SGI priorities doing four at a time */ |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 184 | for (i = 0U; i < MIN_SPI_ID; i += 4U) { |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 185 | gicd_write_ipriorityr(gicd_base, i, GICD_IPRIORITYR_DEF_VAL); |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 186 | } |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 187 | for (i = 0U; i < interrupt_props_num; i++) { |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 188 | prop_desc = &interrupt_props[i]; |
| 189 | |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 190 | if (prop_desc->intr_num >= MIN_SPI_ID) { |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 191 | continue; |
Maheedhar Bollapalli | a9f0a68 | 2024-04-25 10:57:33 +0530 | [diff] [blame] | 192 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 193 | /* Configure this interrupt as a secure interrupt */ |
| 194 | assert(prop_desc->intr_grp == GICV2_INTR_GROUP0); |
| 195 | |
| 196 | /* |
| 197 | * Set interrupt configuration for PPIs. Configuration for SGIs |
| 198 | * are ignored. |
| 199 | */ |
| 200 | if ((prop_desc->intr_num >= MIN_PPI_ID) && |
| 201 | (prop_desc->intr_num < MIN_SPI_ID)) { |
| 202 | gicd_set_icfgr(gicd_base, prop_desc->intr_num, |
| 203 | prop_desc->intr_cfg); |
| 204 | } |
| 205 | |
| 206 | /* We have an SGI or a PPI. They are Group0 at reset */ |
Nithin G | 74d33e4 | 2024-04-23 15:29:52 +0530 | [diff] [blame] | 207 | sec_ppi_sgi_mask |= BIT_32((uint32_t)prop_desc->intr_num); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 208 | |
| 209 | /* Set the priority of this interrupt */ |
| 210 | gicd_set_ipriorityr(gicd_base, prop_desc->intr_num, |
| 211 | prop_desc->intr_pri); |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Invert the bitmask to create a mask for non-secure PPIs and SGIs. |
| 216 | * Program the GICD_IGROUPR0 with this bit mask. |
| 217 | */ |
| 218 | gicd_write_igroupr(gicd_base, 0, ~sec_ppi_sgi_mask); |
| 219 | |
| 220 | /* Enable the Group 0 SGIs and PPIs */ |
| 221 | gicd_write_isenabler(gicd_base, 0, sec_ppi_sgi_mask); |
| 222 | } |