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> 304de4bebcSJens Wiklander #include <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; 8479f008d3SJens Wiklander const size_t max_regs = ((GIC_MAX_INTS + NUM_INTS_PER_REG - 1) / 8579f008d3SJens 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); 9279f008d3SJens 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)); 10179f008d3SJens 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 113bedc2b9fSsunny void gic_cpu_init(void) 114bedc2b9fSsunny { 115bedc2b9fSsunny /* per-CPU inerrupts config: 116bedc2b9fSsunny * ID0-ID7(SGI) for Non-secure interrupts 117bedc2b9fSsunny * ID8-ID15(SGI) for Secure interrupts. 118bedc2b9fSsunny * All PPI config as Non-secure interrupts. 119bedc2b9fSsunny */ 120bedc2b9fSsunny write32(0xffff00ff, gic.gicd_base + GICD_IGROUPR(0)); 121bedc2b9fSsunny 122bedc2b9fSsunny /* Enable GIC */ 123bedc2b9fSsunny write32(GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1 | GICC_CTLR_FIQEN, 124bedc2b9fSsunny gic.gicc_base + GICC_CTLR); 125bedc2b9fSsunny } 126bedc2b9fSsunny 127b0104773SPascal Brand void gic_init(vaddr_t gicc_base, vaddr_t gicd_base) 128b0104773SPascal Brand { 129b0104773SPascal Brand size_t n; 130b0104773SPascal Brand 131b0104773SPascal Brand gic.gicc_base = gicc_base; 132b0104773SPascal Brand gic.gicd_base = gicd_base; 133b0104773SPascal Brand gic.max_it = probe_max_it(); 134b0104773SPascal Brand 13553bd332aSSY Chiu for (n = 0; n <= gic.max_it / NUM_INTS_PER_REG; n++) { 136b0104773SPascal Brand /* Disable interrupts */ 137b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ICENABLER(n)); 138b0104773SPascal Brand 139b0104773SPascal Brand /* Make interrupts non-pending */ 140b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ICPENDR(n)); 141b0104773SPascal Brand 142b0104773SPascal Brand /* Mark interrupts non-secure */ 143bedc2b9fSsunny if (n == 0) { 144bedc2b9fSsunny /* per-CPU inerrupts config: 145bedc2b9fSsunny * ID0-ID7(SGI) for Non-secure interrupts 146bedc2b9fSsunny * ID8-ID15(SGI) for Secure interrupts. 147bedc2b9fSsunny * All PPI config as Non-secure interrupts. 148bedc2b9fSsunny */ 149bedc2b9fSsunny write32(0xffff00ff, gic.gicd_base + GICD_IGROUPR(n)); 150bedc2b9fSsunny } else { 151b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_IGROUPR(n)); 152b0104773SPascal Brand } 153bedc2b9fSsunny } 154b0104773SPascal Brand 155b0104773SPascal Brand /* Enable GIC */ 156b0104773SPascal Brand write32(GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1 | GICC_CTLR_FIQEN, 157b0104773SPascal Brand gic.gicc_base + GICC_CTLR); 158b0104773SPascal Brand write32(GICD_CTLR_ENABLEGRP0 | GICD_CTLR_ENABLEGRP1, 159b0104773SPascal Brand gic.gicd_base + GICD_CTLR); 160b0104773SPascal Brand } 161b0104773SPascal Brand 16253bd332aSSY Chiu void gic_init_base_addr(vaddr_t gicc_base, vaddr_t gicd_base) 16353bd332aSSY Chiu { 16453bd332aSSY Chiu gic.gicc_base = gicc_base; 16553bd332aSSY Chiu gic.gicd_base = gicd_base; 16653bd332aSSY Chiu gic.max_it = probe_max_it(); 16753bd332aSSY Chiu } 16853bd332aSSY Chiu 169b0104773SPascal Brand void gic_it_add(size_t it) 170b0104773SPascal Brand { 17153bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 17253bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 173b0104773SPascal Brand 174b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 175b0104773SPascal Brand 176b0104773SPascal Brand /* Disable the interrupt */ 177b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICENABLER(idx)); 178b0104773SPascal Brand /* Make it non-pending */ 179b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICPENDR(idx)); 180b0104773SPascal Brand /* Assign it to group0 */ 181b0104773SPascal Brand write32(read32(gic.gicd_base + GICD_IGROUPR(idx)) & ~mask, 182b0104773SPascal Brand gic.gicd_base + GICD_IGROUPR(idx)); 183b0104773SPascal Brand } 184b0104773SPascal Brand 185b0104773SPascal Brand void gic_it_set_cpu_mask(size_t it, uint8_t cpu_mask) 186b0104773SPascal Brand { 18753bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 18853bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 18953bd332aSSY Chiu uint32_t target, target_shift; 190b0104773SPascal Brand 191b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 192b0104773SPascal Brand /* Assigned to group0 */ 193b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 194b0104773SPascal Brand 195b0104773SPascal Brand /* Route it to selected CPUs */ 19653bd332aSSY Chiu target = read32(gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 19753bd332aSSY Chiu target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS; 19853bd332aSSY Chiu target &= ~(ITARGETSR_FIELD_MASK << target_shift); 19953bd332aSSY Chiu target |= cpu_mask << target_shift; 200*1f60363aSJens Wiklander DMSG("cpu_mask: writing 0x%x to 0x%" PRIxVA, 20153bd332aSSY Chiu target, gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 20253bd332aSSY Chiu write32(target, gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 203b0104773SPascal Brand DMSG("cpu_mask: 0x%x\n", 20453bd332aSSY Chiu read32(gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG))); 205b0104773SPascal Brand } 206b0104773SPascal Brand 207b0104773SPascal Brand void gic_it_set_prio(size_t it, uint8_t prio) 208b0104773SPascal Brand { 20953bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 21053bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 211b0104773SPascal Brand 212b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 213b0104773SPascal Brand /* Assigned to group0 */ 214b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 215b0104773SPascal Brand 216b0104773SPascal Brand /* Set prio it to selected CPUs */ 217*1f60363aSJens Wiklander DMSG("prio: writing 0x%x to 0x%" PRIxVA, 218b0104773SPascal Brand prio, gic.gicd_base + GICD_IPRIORITYR(0) + it); 219b0104773SPascal Brand write8(prio, gic.gicd_base + GICD_IPRIORITYR(0) + it); 220b0104773SPascal Brand } 221b0104773SPascal Brand 222b0104773SPascal Brand void gic_it_enable(size_t it) 223b0104773SPascal Brand { 22453bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 22553bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 226b0104773SPascal Brand 227b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 228b0104773SPascal Brand /* Assigned to group0 */ 229b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 230b0104773SPascal Brand /* Not enabled yet */ 231b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_ISENABLER(idx)) & mask)); 232b0104773SPascal Brand 233b0104773SPascal Brand /* Enable the interrupt */ 234b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ISENABLER(idx)); 235b0104773SPascal Brand } 236b0104773SPascal Brand 237b0104773SPascal Brand void gic_it_disable(size_t it) 238b0104773SPascal Brand { 23953bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 24053bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 241b0104773SPascal Brand 242b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 243b0104773SPascal Brand /* Assigned to group0 */ 244b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 245b0104773SPascal Brand 246b0104773SPascal Brand /* Disable the interrupt */ 247b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICENABLER(idx)); 248b0104773SPascal Brand } 249b0104773SPascal Brand 250b0104773SPascal Brand uint32_t gic_read_iar(void) 251b0104773SPascal Brand { 252b0104773SPascal Brand return read32(gic.gicc_base + GICC_IAR); 253b0104773SPascal Brand } 254b0104773SPascal Brand 255b0104773SPascal Brand void gic_write_eoir(uint32_t eoir) 256b0104773SPascal Brand { 257b0104773SPascal Brand write32(eoir, gic.gicc_base + GICC_EOIR); 258b0104773SPascal Brand } 259b0104773SPascal Brand 26053bd332aSSY Chiu bool gic_it_is_enabled(size_t it) { 26153bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 26253bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 26353bd332aSSY Chiu return !!(read32(gic.gicd_base + GICD_ISENABLER(idx)) & mask); 26453bd332aSSY Chiu } 26553bd332aSSY Chiu 26653bd332aSSY Chiu bool gic_it_get_group(size_t it) { 26753bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 26853bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 26953bd332aSSY Chiu return !!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask); 27053bd332aSSY Chiu } 27153bd332aSSY Chiu 27253bd332aSSY Chiu uint32_t gic_it_get_target(size_t it) { 27353bd332aSSY Chiu size_t reg_idx = it / NUM_TARGETS_PER_REG; 27453bd332aSSY Chiu uint32_t target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS; 27553bd332aSSY Chiu uint32_t target_mask = ITARGETSR_FIELD_MASK << target_shift; 27653bd332aSSY Chiu uint32_t target = 27753bd332aSSY Chiu read32(gic.gicd_base + GICD_ITARGETSR(reg_idx)) & target_mask; 27853bd332aSSY Chiu target = target >> target_shift; 27953bd332aSSY Chiu return target; 28053bd332aSSY Chiu } 28153bd332aSSY Chiu 28253bd332aSSY Chiu void gic_dump_state(void) 28353bd332aSSY Chiu { 28453bd332aSSY Chiu int i; 28553bd332aSSY Chiu DMSG("GICC_CTLR: 0x%x", read32(gic.gicc_base + GICC_CTLR)); 28653bd332aSSY Chiu DMSG("GICD_CTLR: 0x%x", read32(gic.gicd_base + GICD_CTLR)); 28753bd332aSSY Chiu 288ff97306fSJens Wiklander for (i = 0; i < (int)gic.max_it; i++) { 28953bd332aSSY Chiu if (gic_it_is_enabled(i)) { 29053bd332aSSY Chiu DMSG("irq%d: enabled, group:%d, target:%x", i, 29153bd332aSSY Chiu gic_it_get_group(i), gic_it_get_target(i)); 29253bd332aSSY Chiu } 29353bd332aSSY Chiu } 29453bd332aSSY Chiu } 295