1b0104773SPascal Brand /* 2b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 3b0104773SPascal Brand * All rights reserved. 4b0104773SPascal Brand * 5b0104773SPascal Brand * Redistribution and use in source and binary forms, with or without 6b0104773SPascal Brand * modification, are permitted provided that the following conditions are met: 7b0104773SPascal Brand * 8b0104773SPascal Brand * 1. Redistributions of source code must retain the above copyright notice, 9b0104773SPascal Brand * this list of conditions and the following disclaimer. 10b0104773SPascal Brand * 11b0104773SPascal Brand * 2. Redistributions in binary form must reproduce the above copyright notice, 12b0104773SPascal Brand * this list of conditions and the following disclaimer in the documentation 13b0104773SPascal Brand * and/or other materials provided with the distribution. 14b0104773SPascal Brand * 15b0104773SPascal Brand * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16b0104773SPascal Brand * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17b0104773SPascal Brand * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18b0104773SPascal Brand * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19b0104773SPascal Brand * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20b0104773SPascal Brand * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21b0104773SPascal Brand * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22b0104773SPascal Brand * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23b0104773SPascal Brand * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24b0104773SPascal Brand * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25b0104773SPascal Brand * POSSIBILITY OF SUCH DAMAGE. 26b0104773SPascal Brand */ 27b0104773SPascal Brand 28b0104773SPascal Brand #include <drivers/gic.h> 29b0104773SPascal Brand #include <io.h> 30b0104773SPascal Brand #include <kernel/tee_core_trace.h> 31b0104773SPascal Brand 32b0104773SPascal Brand #include <assert.h> 33b0104773SPascal Brand 34b0104773SPascal Brand /* Offsets from gic.gicc_base */ 35b0104773SPascal Brand #define GICC_CTLR (0x000) 36b0104773SPascal Brand #define GICC_IAR (0x00C) 37b0104773SPascal Brand #define GICC_EOIR (0x010) 38b0104773SPascal Brand 39b0104773SPascal Brand #define GICC_CTLR_ENABLEGRP0 (1 << 0) 40b0104773SPascal Brand #define GICC_CTLR_ENABLEGRP1 (1 << 1) 41b0104773SPascal Brand #define GICC_CTLR_FIQEN (1 << 3) 42b0104773SPascal Brand 43b0104773SPascal Brand /* Offsets from gic.gicd_base */ 44b0104773SPascal Brand #define GICD_CTLR (0x000) 45b0104773SPascal Brand #define GICD_TYPER (0x004) 46b0104773SPascal Brand #define GICD_IGROUPR(n) (0x080 + (n) * 4) 47b0104773SPascal Brand #define GICD_ISENABLER(n) (0x100 + (n) * 4) 48b0104773SPascal Brand #define GICD_ICENABLER(n) (0x180 + (n) * 4) 49b0104773SPascal Brand #define GICD_ICPENDR(n) (0x280 + (n) * 4) 50b0104773SPascal Brand #define GICD_IPRIORITYR(n) (0x400 + (n) * 4) 51b0104773SPascal Brand #define GICD_ITARGETSR(n) (0x800 + (n) * 4) 52b0104773SPascal Brand 53b0104773SPascal Brand #define GICD_CTLR_ENABLEGRP0 (1 << 0) 54b0104773SPascal Brand #define GICD_CTLR_ENABLEGRP1 (1 << 1) 55b0104773SPascal Brand 56*53bd332aSSY Chiu /* Number of Private Peripheral Interrupt */ 57*53bd332aSSY Chiu #define NUM_PPI 32 58*53bd332aSSY Chiu 59*53bd332aSSY Chiu /* Number of interrupts in one register */ 60*53bd332aSSY Chiu #define NUM_INTS_PER_REG 32 61*53bd332aSSY Chiu 62*53bd332aSSY Chiu /* Number of targets in one register */ 63*53bd332aSSY Chiu #define NUM_TARGETS_PER_REG 4 64*53bd332aSSY Chiu 65*53bd332aSSY Chiu /* Accessors to access ITARGETSRn */ 66*53bd332aSSY Chiu #define ITARGETSR_FIELD_BITS 8 67*53bd332aSSY Chiu #define ITARGETSR_FIELD_MASK 0xff 68*53bd332aSSY Chiu 69b0104773SPascal Brand /* Maximum number of interrups a GIC can support */ 70b0104773SPascal Brand #define GIC_MAX_INTS 1020 71b0104773SPascal Brand 72b0104773SPascal Brand 73b0104773SPascal Brand static struct { 74b0104773SPascal Brand vaddr_t gicc_base; 75b0104773SPascal Brand vaddr_t gicd_base; 76b0104773SPascal Brand size_t max_it; 77b0104773SPascal Brand } gic; 78b0104773SPascal Brand 79b0104773SPascal Brand static size_t probe_max_it(void) 80b0104773SPascal Brand { 81b0104773SPascal Brand int i; 82b0104773SPascal Brand uint32_t old_ctlr; 83b0104773SPascal Brand size_t ret = 0; 84b0104773SPascal Brand 85b0104773SPascal Brand /* 86b0104773SPascal Brand * Probe which interrupt number is the largest. 87b0104773SPascal Brand */ 88b0104773SPascal Brand old_ctlr = read32(gic.gicc_base + GICC_CTLR); 89b0104773SPascal Brand write32(0, gic.gicc_base + GICC_CTLR); 90*53bd332aSSY Chiu for (i = GIC_MAX_INTS / NUM_INTS_PER_REG; i > 0; i--) { 91b0104773SPascal Brand uint32_t old_reg; 92b0104773SPascal Brand uint32_t reg; 93b0104773SPascal Brand int b; 94b0104773SPascal Brand 95b0104773SPascal Brand old_reg = read32(gic.gicd_base + GICD_ISENABLER(i)); 96b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ISENABLER(i)); 97b0104773SPascal Brand reg = read32(gic.gicd_base + GICD_ISENABLER(i)); 98b0104773SPascal Brand write32(old_reg, gic.gicd_base + GICD_ICENABLER(i)); 99*53bd332aSSY Chiu for (b = NUM_INTS_PER_REG - 1; b > 0; b--) { 100b0104773SPascal Brand if ((1 << b) & reg) { 101*53bd332aSSY Chiu ret = i * NUM_INTS_PER_REG + b; 102b0104773SPascal Brand goto out; 103b0104773SPascal Brand } 104b0104773SPascal Brand } 105b0104773SPascal Brand } 106b0104773SPascal Brand out: 107b0104773SPascal Brand write32(old_ctlr, gic.gicc_base + GICC_CTLR); 108b0104773SPascal Brand return ret; 109b0104773SPascal Brand } 110b0104773SPascal Brand 111b0104773SPascal Brand void gic_init(vaddr_t gicc_base, vaddr_t gicd_base) 112b0104773SPascal Brand { 113b0104773SPascal Brand size_t n; 114b0104773SPascal Brand 115b0104773SPascal Brand gic.gicc_base = gicc_base; 116b0104773SPascal Brand gic.gicd_base = gicd_base; 117b0104773SPascal Brand gic.max_it = probe_max_it(); 118b0104773SPascal Brand 119*53bd332aSSY Chiu for (n = 0; n <= gic.max_it / NUM_INTS_PER_REG; n++) { 120b0104773SPascal Brand /* Disable interrupts */ 121b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ICENABLER(n)); 122b0104773SPascal Brand 123b0104773SPascal Brand /* Make interrupts non-pending */ 124b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ICPENDR(n)); 125b0104773SPascal Brand 126b0104773SPascal Brand /* Mark interrupts non-secure */ 127b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_IGROUPR(n)); 128b0104773SPascal Brand } 129b0104773SPascal Brand 130b0104773SPascal Brand /* Enable GIC */ 131b0104773SPascal Brand write32(GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1 | GICC_CTLR_FIQEN, 132b0104773SPascal Brand gic.gicc_base + GICC_CTLR); 133b0104773SPascal Brand write32(GICD_CTLR_ENABLEGRP0 | GICD_CTLR_ENABLEGRP1, 134b0104773SPascal Brand gic.gicd_base + GICD_CTLR); 135b0104773SPascal Brand } 136b0104773SPascal Brand 137*53bd332aSSY Chiu void gic_init_base_addr(vaddr_t gicc_base, vaddr_t gicd_base) 138*53bd332aSSY Chiu { 139*53bd332aSSY Chiu gic.gicc_base = gicc_base; 140*53bd332aSSY Chiu gic.gicd_base = gicd_base; 141*53bd332aSSY Chiu gic.max_it = probe_max_it(); 142*53bd332aSSY Chiu } 143*53bd332aSSY Chiu 144b0104773SPascal Brand void gic_it_add(size_t it) 145b0104773SPascal Brand { 146*53bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 147*53bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 148b0104773SPascal Brand 149b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 150b0104773SPascal Brand 151b0104773SPascal Brand /* Disable the interrupt */ 152b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICENABLER(idx)); 153b0104773SPascal Brand /* Make it non-pending */ 154b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICPENDR(idx)); 155b0104773SPascal Brand /* Assign it to group0 */ 156b0104773SPascal Brand write32(read32(gic.gicd_base + GICD_IGROUPR(idx)) & ~mask, 157b0104773SPascal Brand gic.gicd_base + GICD_IGROUPR(idx)); 158b0104773SPascal Brand } 159b0104773SPascal Brand 160b0104773SPascal Brand void gic_it_set_cpu_mask(size_t it, uint8_t cpu_mask) 161b0104773SPascal Brand { 162*53bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 163*53bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 164*53bd332aSSY Chiu uint32_t target, target_shift; 165b0104773SPascal Brand 166b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 167b0104773SPascal Brand /* Assigned to group0 */ 168b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 169b0104773SPascal Brand 170b0104773SPascal Brand /* Route it to selected CPUs */ 171*53bd332aSSY Chiu target = read32(gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 172*53bd332aSSY Chiu target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS; 173*53bd332aSSY Chiu target &= ~(ITARGETSR_FIELD_MASK << target_shift); 174*53bd332aSSY Chiu target |= cpu_mask << target_shift; 175b0104773SPascal Brand DMSG("cpu_mask: writing 0x%x to 0x%x\n", 176*53bd332aSSY Chiu target, gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 177*53bd332aSSY Chiu write32(target, gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 178b0104773SPascal Brand DMSG("cpu_mask: 0x%x\n", 179*53bd332aSSY Chiu read32(gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG))); 180b0104773SPascal Brand } 181b0104773SPascal Brand 182b0104773SPascal Brand void gic_it_set_prio(size_t it, uint8_t prio) 183b0104773SPascal Brand { 184*53bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 185*53bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 186b0104773SPascal Brand 187b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 188b0104773SPascal Brand /* Assigned to group0 */ 189b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 190b0104773SPascal Brand 191b0104773SPascal Brand /* Set prio it to selected CPUs */ 192b0104773SPascal Brand DMSG("prio: writing 0x%x to 0x%x\n", 193b0104773SPascal Brand prio, gic.gicd_base + GICD_IPRIORITYR(0) + it); 194b0104773SPascal Brand write8(prio, gic.gicd_base + GICD_IPRIORITYR(0) + it); 195b0104773SPascal Brand } 196b0104773SPascal Brand 197b0104773SPascal Brand void gic_it_enable(size_t it) 198b0104773SPascal Brand { 199*53bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 200*53bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 201b0104773SPascal Brand 202b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 203b0104773SPascal Brand /* Assigned to group0 */ 204b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 205b0104773SPascal Brand /* Not enabled yet */ 206b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_ISENABLER(idx)) & mask)); 207b0104773SPascal Brand 208b0104773SPascal Brand /* Enable the interrupt */ 209b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ISENABLER(idx)); 210b0104773SPascal Brand } 211b0104773SPascal Brand 212b0104773SPascal Brand void gic_it_disable(size_t it) 213b0104773SPascal Brand { 214*53bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 215*53bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 216b0104773SPascal Brand 217b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 218b0104773SPascal Brand /* Assigned to group0 */ 219b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 220b0104773SPascal Brand 221b0104773SPascal Brand /* Disable the interrupt */ 222b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICENABLER(idx)); 223b0104773SPascal Brand } 224b0104773SPascal Brand 225b0104773SPascal Brand uint32_t gic_read_iar(void) 226b0104773SPascal Brand { 227b0104773SPascal Brand return read32(gic.gicc_base + GICC_IAR); 228b0104773SPascal Brand } 229b0104773SPascal Brand 230b0104773SPascal Brand void gic_write_eoir(uint32_t eoir) 231b0104773SPascal Brand { 232b0104773SPascal Brand write32(eoir, gic.gicc_base + GICC_EOIR); 233b0104773SPascal Brand } 234b0104773SPascal Brand 235*53bd332aSSY Chiu bool gic_it_is_enabled(size_t it) { 236*53bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 237*53bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 238*53bd332aSSY Chiu return !!(read32(gic.gicd_base + GICD_ISENABLER(idx)) & mask); 239*53bd332aSSY Chiu } 240*53bd332aSSY Chiu 241*53bd332aSSY Chiu bool gic_it_get_group(size_t it) { 242*53bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 243*53bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 244*53bd332aSSY Chiu return !!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask); 245*53bd332aSSY Chiu } 246*53bd332aSSY Chiu 247*53bd332aSSY Chiu uint32_t gic_it_get_target(size_t it) { 248*53bd332aSSY Chiu size_t reg_idx = it / NUM_TARGETS_PER_REG; 249*53bd332aSSY Chiu uint32_t target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS; 250*53bd332aSSY Chiu uint32_t target_mask = ITARGETSR_FIELD_MASK << target_shift; 251*53bd332aSSY Chiu uint32_t target = 252*53bd332aSSY Chiu read32(gic.gicd_base + GICD_ITARGETSR(reg_idx)) & target_mask; 253*53bd332aSSY Chiu target = target >> target_shift; 254*53bd332aSSY Chiu return target; 255*53bd332aSSY Chiu } 256*53bd332aSSY Chiu 257*53bd332aSSY Chiu void gic_dump_state(void) 258*53bd332aSSY Chiu { 259*53bd332aSSY Chiu int i; 260*53bd332aSSY Chiu DMSG("GICC_CTLR: 0x%x", read32(gic.gicc_base + GICC_CTLR)); 261*53bd332aSSY Chiu DMSG("GICD_CTLR: 0x%x", read32(gic.gicd_base + GICD_CTLR)); 262*53bd332aSSY Chiu 263*53bd332aSSY Chiu for (i = 0; i < NUM_PPI; i++) { 264*53bd332aSSY Chiu if (gic_it_is_enabled(i)) { 265*53bd332aSSY Chiu DMSG("irq%d: enabled, group:%d, target:%x", i, 266*53bd332aSSY Chiu gic_it_get_group(i), gic_it_get_target(i)); 267*53bd332aSSY Chiu } 268*53bd332aSSY Chiu } 269*53bd332aSSY Chiu } 270