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 5653bd332aSSY Chiu /* Number of Private Peripheral Interrupt */ 5753bd332aSSY Chiu #define NUM_PPI 32 5853bd332aSSY Chiu 5953bd332aSSY Chiu /* Number of interrupts in one register */ 6053bd332aSSY Chiu #define NUM_INTS_PER_REG 32 6153bd332aSSY Chiu 6253bd332aSSY Chiu /* Number of targets in one register */ 6353bd332aSSY Chiu #define NUM_TARGETS_PER_REG 4 6453bd332aSSY Chiu 6553bd332aSSY Chiu /* Accessors to access ITARGETSRn */ 6653bd332aSSY Chiu #define ITARGETSR_FIELD_BITS 8 6753bd332aSSY Chiu #define ITARGETSR_FIELD_MASK 0xff 6853bd332aSSY 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; 84*79f008d3SJens Wiklander const size_t max_regs = ((GIC_MAX_INTS + NUM_INTS_PER_REG - 1) / 85*79f008d3SJens Wiklander NUM_INTS_PER_REG) - 1; 86b0104773SPascal Brand 87b0104773SPascal Brand /* 88b0104773SPascal Brand * Probe which interrupt number is the largest. 89b0104773SPascal Brand */ 90b0104773SPascal Brand old_ctlr = read32(gic.gicc_base + GICC_CTLR); 91b0104773SPascal Brand write32(0, gic.gicc_base + GICC_CTLR); 92*79f008d3SJens Wiklander for (i = max_regs; i >= 0; i--) { 93b0104773SPascal Brand uint32_t old_reg; 94b0104773SPascal Brand uint32_t reg; 95b0104773SPascal Brand int b; 96b0104773SPascal Brand 97b0104773SPascal Brand old_reg = read32(gic.gicd_base + GICD_ISENABLER(i)); 98b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ISENABLER(i)); 99b0104773SPascal Brand reg = read32(gic.gicd_base + GICD_ISENABLER(i)); 100b0104773SPascal Brand write32(old_reg, gic.gicd_base + GICD_ICENABLER(i)); 101*79f008d3SJens Wiklander for (b = NUM_INTS_PER_REG - 1; b >= 0; b--) { 102b0104773SPascal Brand if ((1 << b) & reg) { 10353bd332aSSY Chiu ret = i * NUM_INTS_PER_REG + b; 104b0104773SPascal Brand goto out; 105b0104773SPascal Brand } 106b0104773SPascal Brand } 107b0104773SPascal Brand } 108b0104773SPascal Brand out: 109b0104773SPascal Brand write32(old_ctlr, gic.gicc_base + GICC_CTLR); 110b0104773SPascal Brand return ret; 111b0104773SPascal Brand } 112b0104773SPascal Brand 113b0104773SPascal Brand void gic_init(vaddr_t gicc_base, vaddr_t gicd_base) 114b0104773SPascal Brand { 115b0104773SPascal Brand size_t n; 116b0104773SPascal Brand 117b0104773SPascal Brand gic.gicc_base = gicc_base; 118b0104773SPascal Brand gic.gicd_base = gicd_base; 119b0104773SPascal Brand gic.max_it = probe_max_it(); 120b0104773SPascal Brand 12153bd332aSSY Chiu for (n = 0; n <= gic.max_it / NUM_INTS_PER_REG; n++) { 122b0104773SPascal Brand /* Disable interrupts */ 123b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ICENABLER(n)); 124b0104773SPascal Brand 125b0104773SPascal Brand /* Make interrupts non-pending */ 126b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ICPENDR(n)); 127b0104773SPascal Brand 128b0104773SPascal Brand /* Mark interrupts non-secure */ 129b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_IGROUPR(n)); 130b0104773SPascal Brand } 131b0104773SPascal Brand 132b0104773SPascal Brand /* Enable GIC */ 133b0104773SPascal Brand write32(GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1 | GICC_CTLR_FIQEN, 134b0104773SPascal Brand gic.gicc_base + GICC_CTLR); 135b0104773SPascal Brand write32(GICD_CTLR_ENABLEGRP0 | GICD_CTLR_ENABLEGRP1, 136b0104773SPascal Brand gic.gicd_base + GICD_CTLR); 137b0104773SPascal Brand } 138b0104773SPascal Brand 13953bd332aSSY Chiu void gic_init_base_addr(vaddr_t gicc_base, vaddr_t gicd_base) 14053bd332aSSY Chiu { 14153bd332aSSY Chiu gic.gicc_base = gicc_base; 14253bd332aSSY Chiu gic.gicd_base = gicd_base; 14353bd332aSSY Chiu gic.max_it = probe_max_it(); 14453bd332aSSY Chiu } 14553bd332aSSY Chiu 146b0104773SPascal Brand void gic_it_add(size_t it) 147b0104773SPascal Brand { 14853bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 14953bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 150b0104773SPascal Brand 151b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 152b0104773SPascal Brand 153b0104773SPascal Brand /* Disable the interrupt */ 154b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICENABLER(idx)); 155b0104773SPascal Brand /* Make it non-pending */ 156b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICPENDR(idx)); 157b0104773SPascal Brand /* Assign it to group0 */ 158b0104773SPascal Brand write32(read32(gic.gicd_base + GICD_IGROUPR(idx)) & ~mask, 159b0104773SPascal Brand gic.gicd_base + GICD_IGROUPR(idx)); 160b0104773SPascal Brand } 161b0104773SPascal Brand 162b0104773SPascal Brand void gic_it_set_cpu_mask(size_t it, uint8_t cpu_mask) 163b0104773SPascal Brand { 16453bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 16553bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 16653bd332aSSY Chiu uint32_t target, target_shift; 167b0104773SPascal Brand 168b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 169b0104773SPascal Brand /* Assigned to group0 */ 170b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 171b0104773SPascal Brand 172b0104773SPascal Brand /* Route it to selected CPUs */ 17353bd332aSSY Chiu target = read32(gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 17453bd332aSSY Chiu target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS; 17553bd332aSSY Chiu target &= ~(ITARGETSR_FIELD_MASK << target_shift); 17653bd332aSSY Chiu target |= cpu_mask << target_shift; 177b0104773SPascal Brand DMSG("cpu_mask: writing 0x%x to 0x%x\n", 17853bd332aSSY Chiu target, gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 17953bd332aSSY Chiu write32(target, gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 180b0104773SPascal Brand DMSG("cpu_mask: 0x%x\n", 18153bd332aSSY Chiu read32(gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG))); 182b0104773SPascal Brand } 183b0104773SPascal Brand 184b0104773SPascal Brand void gic_it_set_prio(size_t it, uint8_t prio) 185b0104773SPascal Brand { 18653bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 18753bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 188b0104773SPascal Brand 189b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 190b0104773SPascal Brand /* Assigned to group0 */ 191b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 192b0104773SPascal Brand 193b0104773SPascal Brand /* Set prio it to selected CPUs */ 194b0104773SPascal Brand DMSG("prio: writing 0x%x to 0x%x\n", 195b0104773SPascal Brand prio, gic.gicd_base + GICD_IPRIORITYR(0) + it); 196b0104773SPascal Brand write8(prio, gic.gicd_base + GICD_IPRIORITYR(0) + it); 197b0104773SPascal Brand } 198b0104773SPascal Brand 199b0104773SPascal Brand void gic_it_enable(size_t it) 200b0104773SPascal Brand { 20153bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 20253bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 203b0104773SPascal Brand 204b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 205b0104773SPascal Brand /* Assigned to group0 */ 206b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 207b0104773SPascal Brand /* Not enabled yet */ 208b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_ISENABLER(idx)) & mask)); 209b0104773SPascal Brand 210b0104773SPascal Brand /* Enable the interrupt */ 211b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ISENABLER(idx)); 212b0104773SPascal Brand } 213b0104773SPascal Brand 214b0104773SPascal Brand void gic_it_disable(size_t it) 215b0104773SPascal Brand { 21653bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 21753bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 218b0104773SPascal Brand 219b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 220b0104773SPascal Brand /* Assigned to group0 */ 221b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 222b0104773SPascal Brand 223b0104773SPascal Brand /* Disable the interrupt */ 224b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICENABLER(idx)); 225b0104773SPascal Brand } 226b0104773SPascal Brand 227b0104773SPascal Brand uint32_t gic_read_iar(void) 228b0104773SPascal Brand { 229b0104773SPascal Brand return read32(gic.gicc_base + GICC_IAR); 230b0104773SPascal Brand } 231b0104773SPascal Brand 232b0104773SPascal Brand void gic_write_eoir(uint32_t eoir) 233b0104773SPascal Brand { 234b0104773SPascal Brand write32(eoir, gic.gicc_base + GICC_EOIR); 235b0104773SPascal Brand } 236b0104773SPascal Brand 23753bd332aSSY Chiu bool gic_it_is_enabled(size_t it) { 23853bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 23953bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 24053bd332aSSY Chiu return !!(read32(gic.gicd_base + GICD_ISENABLER(idx)) & mask); 24153bd332aSSY Chiu } 24253bd332aSSY Chiu 24353bd332aSSY Chiu bool gic_it_get_group(size_t it) { 24453bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 24553bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 24653bd332aSSY Chiu return !!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask); 24753bd332aSSY Chiu } 24853bd332aSSY Chiu 24953bd332aSSY Chiu uint32_t gic_it_get_target(size_t it) { 25053bd332aSSY Chiu size_t reg_idx = it / NUM_TARGETS_PER_REG; 25153bd332aSSY Chiu uint32_t target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS; 25253bd332aSSY Chiu uint32_t target_mask = ITARGETSR_FIELD_MASK << target_shift; 25353bd332aSSY Chiu uint32_t target = 25453bd332aSSY Chiu read32(gic.gicd_base + GICD_ITARGETSR(reg_idx)) & target_mask; 25553bd332aSSY Chiu target = target >> target_shift; 25653bd332aSSY Chiu return target; 25753bd332aSSY Chiu } 25853bd332aSSY Chiu 25953bd332aSSY Chiu void gic_dump_state(void) 26053bd332aSSY Chiu { 26153bd332aSSY Chiu int i; 26253bd332aSSY Chiu DMSG("GICC_CTLR: 0x%x", read32(gic.gicc_base + GICC_CTLR)); 26353bd332aSSY Chiu DMSG("GICD_CTLR: 0x%x", read32(gic.gicd_base + GICD_CTLR)); 26453bd332aSSY Chiu 26553bd332aSSY Chiu for (i = 0; i < NUM_PPI; i++) { 26653bd332aSSY Chiu if (gic_it_is_enabled(i)) { 26753bd332aSSY Chiu DMSG("irq%d: enabled, group:%d, target:%x", i, 26853bd332aSSY Chiu gic_it_get_group(i), gic_it_get_target(i)); 26953bd332aSSY Chiu } 27053bd332aSSY Chiu } 27153bd332aSSY Chiu } 272