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) 36*30a673e3SPeter Maydell #define GICC_PMR (0x004) 37b0104773SPascal Brand #define GICC_IAR (0x00C) 38b0104773SPascal Brand #define GICC_EOIR (0x010) 39b0104773SPascal Brand 40b0104773SPascal Brand #define GICC_CTLR_ENABLEGRP0 (1 << 0) 41b0104773SPascal Brand #define GICC_CTLR_ENABLEGRP1 (1 << 1) 42b0104773SPascal Brand #define GICC_CTLR_FIQEN (1 << 3) 43b0104773SPascal Brand 44b0104773SPascal Brand /* Offsets from gic.gicd_base */ 45b0104773SPascal Brand #define GICD_CTLR (0x000) 46b0104773SPascal Brand #define GICD_TYPER (0x004) 47b0104773SPascal Brand #define GICD_IGROUPR(n) (0x080 + (n) * 4) 48b0104773SPascal Brand #define GICD_ISENABLER(n) (0x100 + (n) * 4) 49b0104773SPascal Brand #define GICD_ICENABLER(n) (0x180 + (n) * 4) 50b0104773SPascal Brand #define GICD_ICPENDR(n) (0x280 + (n) * 4) 51b0104773SPascal Brand #define GICD_IPRIORITYR(n) (0x400 + (n) * 4) 52b0104773SPascal Brand #define GICD_ITARGETSR(n) (0x800 + (n) * 4) 53b0104773SPascal Brand 54b0104773SPascal Brand #define GICD_CTLR_ENABLEGRP0 (1 << 0) 55b0104773SPascal Brand #define GICD_CTLR_ENABLEGRP1 (1 << 1) 56b0104773SPascal Brand 5753bd332aSSY Chiu /* Number of Private Peripheral Interrupt */ 5853bd332aSSY Chiu #define NUM_PPI 32 5953bd332aSSY Chiu 6053bd332aSSY Chiu /* Number of interrupts in one register */ 6153bd332aSSY Chiu #define NUM_INTS_PER_REG 32 6253bd332aSSY Chiu 6353bd332aSSY Chiu /* Number of targets in one register */ 6453bd332aSSY Chiu #define NUM_TARGETS_PER_REG 4 6553bd332aSSY Chiu 6653bd332aSSY Chiu /* Accessors to access ITARGETSRn */ 6753bd332aSSY Chiu #define ITARGETSR_FIELD_BITS 8 6853bd332aSSY Chiu #define ITARGETSR_FIELD_MASK 0xff 6953bd332aSSY Chiu 70b0104773SPascal Brand /* Maximum number of interrups a GIC can support */ 71b0104773SPascal Brand #define GIC_MAX_INTS 1020 72b0104773SPascal Brand 73b0104773SPascal Brand 74b0104773SPascal Brand static struct { 75b0104773SPascal Brand vaddr_t gicc_base; 76b0104773SPascal Brand vaddr_t gicd_base; 77b0104773SPascal Brand size_t max_it; 78b0104773SPascal Brand } gic; 79b0104773SPascal Brand 80b0104773SPascal Brand static size_t probe_max_it(void) 81b0104773SPascal Brand { 82b0104773SPascal Brand int i; 83b0104773SPascal Brand uint32_t old_ctlr; 84b0104773SPascal Brand size_t ret = 0; 8579f008d3SJens Wiklander const size_t max_regs = ((GIC_MAX_INTS + NUM_INTS_PER_REG - 1) / 8679f008d3SJens Wiklander NUM_INTS_PER_REG) - 1; 87b0104773SPascal Brand 88b0104773SPascal Brand /* 89b0104773SPascal Brand * Probe which interrupt number is the largest. 90b0104773SPascal Brand */ 91b0104773SPascal Brand old_ctlr = read32(gic.gicc_base + GICC_CTLR); 92b0104773SPascal Brand write32(0, gic.gicc_base + GICC_CTLR); 9379f008d3SJens Wiklander for (i = max_regs; i >= 0; i--) { 94b0104773SPascal Brand uint32_t old_reg; 95b0104773SPascal Brand uint32_t reg; 96b0104773SPascal Brand int b; 97b0104773SPascal Brand 98b0104773SPascal Brand old_reg = read32(gic.gicd_base + GICD_ISENABLER(i)); 99b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ISENABLER(i)); 100b0104773SPascal Brand reg = read32(gic.gicd_base + GICD_ISENABLER(i)); 101b0104773SPascal Brand write32(old_reg, gic.gicd_base + GICD_ICENABLER(i)); 10279f008d3SJens Wiklander for (b = NUM_INTS_PER_REG - 1; b >= 0; b--) { 103b0104773SPascal Brand if ((1 << b) & reg) { 10453bd332aSSY Chiu ret = i * NUM_INTS_PER_REG + b; 105b0104773SPascal Brand goto out; 106b0104773SPascal Brand } 107b0104773SPascal Brand } 108b0104773SPascal Brand } 109b0104773SPascal Brand out: 110b0104773SPascal Brand write32(old_ctlr, gic.gicc_base + GICC_CTLR); 111b0104773SPascal Brand return ret; 112b0104773SPascal Brand } 113b0104773SPascal Brand 114bedc2b9fSsunny void gic_cpu_init(void) 115bedc2b9fSsunny { 116e06e6e74SPeter Maydell /* per-CPU interrupts config: 117bedc2b9fSsunny * ID0-ID7(SGI) for Non-secure interrupts 118bedc2b9fSsunny * ID8-ID15(SGI) for Secure interrupts. 119bedc2b9fSsunny * All PPI config as Non-secure interrupts. 120bedc2b9fSsunny */ 121bedc2b9fSsunny write32(0xffff00ff, gic.gicd_base + GICD_IGROUPR(0)); 122bedc2b9fSsunny 123*30a673e3SPeter Maydell /* Set the priority mask to permit Non-secure interrupts, and to 124*30a673e3SPeter Maydell * allow the Non-secure world to adjust the priority mask itself 125*30a673e3SPeter Maydell */ 126*30a673e3SPeter Maydell write32(0x80, gic.gicc_base + GICC_PMR); 127*30a673e3SPeter Maydell 128bedc2b9fSsunny /* Enable GIC */ 129bedc2b9fSsunny write32(GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1 | GICC_CTLR_FIQEN, 130bedc2b9fSsunny gic.gicc_base + GICC_CTLR); 131bedc2b9fSsunny } 132bedc2b9fSsunny 133b0104773SPascal Brand void gic_init(vaddr_t gicc_base, vaddr_t gicd_base) 134b0104773SPascal Brand { 135b0104773SPascal Brand size_t n; 136b0104773SPascal Brand 137b0104773SPascal Brand gic.gicc_base = gicc_base; 138b0104773SPascal Brand gic.gicd_base = gicd_base; 139b0104773SPascal Brand gic.max_it = probe_max_it(); 140b0104773SPascal Brand 14153bd332aSSY Chiu for (n = 0; n <= gic.max_it / NUM_INTS_PER_REG; n++) { 142b0104773SPascal Brand /* Disable interrupts */ 143b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ICENABLER(n)); 144b0104773SPascal Brand 145b0104773SPascal Brand /* Make interrupts non-pending */ 146b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_ICPENDR(n)); 147b0104773SPascal Brand 148b0104773SPascal Brand /* Mark interrupts non-secure */ 149bedc2b9fSsunny if (n == 0) { 150bedc2b9fSsunny /* per-CPU inerrupts config: 151bedc2b9fSsunny * ID0-ID7(SGI) for Non-secure interrupts 152bedc2b9fSsunny * ID8-ID15(SGI) for Secure interrupts. 153bedc2b9fSsunny * All PPI config as Non-secure interrupts. 154bedc2b9fSsunny */ 155bedc2b9fSsunny write32(0xffff00ff, gic.gicd_base + GICD_IGROUPR(n)); 156bedc2b9fSsunny } else { 157b0104773SPascal Brand write32(0xffffffff, gic.gicd_base + GICD_IGROUPR(n)); 158b0104773SPascal Brand } 159bedc2b9fSsunny } 160b0104773SPascal Brand 161*30a673e3SPeter Maydell /* Set the priority mask to permit Non-secure interrupts, and to 162*30a673e3SPeter Maydell * allow the Non-secure world to adjust the priority mask itself 163*30a673e3SPeter Maydell */ 164*30a673e3SPeter Maydell write32(0x80, gic.gicc_base + GICC_PMR); 165*30a673e3SPeter Maydell 166b0104773SPascal Brand /* Enable GIC */ 167b0104773SPascal Brand write32(GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1 | GICC_CTLR_FIQEN, 168b0104773SPascal Brand gic.gicc_base + GICC_CTLR); 169b0104773SPascal Brand write32(GICD_CTLR_ENABLEGRP0 | GICD_CTLR_ENABLEGRP1, 170b0104773SPascal Brand gic.gicd_base + GICD_CTLR); 171b0104773SPascal Brand } 172b0104773SPascal Brand 17353bd332aSSY Chiu void gic_init_base_addr(vaddr_t gicc_base, vaddr_t gicd_base) 17453bd332aSSY Chiu { 17553bd332aSSY Chiu gic.gicc_base = gicc_base; 17653bd332aSSY Chiu gic.gicd_base = gicd_base; 17753bd332aSSY Chiu gic.max_it = probe_max_it(); 17853bd332aSSY Chiu } 17953bd332aSSY Chiu 180b0104773SPascal Brand void gic_it_add(size_t it) 181b0104773SPascal Brand { 18253bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 18353bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 184b0104773SPascal Brand 185b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 186b0104773SPascal Brand 187b0104773SPascal Brand /* Disable the interrupt */ 188b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICENABLER(idx)); 189b0104773SPascal Brand /* Make it non-pending */ 190b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICPENDR(idx)); 191b0104773SPascal Brand /* Assign it to group0 */ 192b0104773SPascal Brand write32(read32(gic.gicd_base + GICD_IGROUPR(idx)) & ~mask, 193b0104773SPascal Brand gic.gicd_base + GICD_IGROUPR(idx)); 194b0104773SPascal Brand } 195b0104773SPascal Brand 196b0104773SPascal Brand void gic_it_set_cpu_mask(size_t it, uint8_t cpu_mask) 197b0104773SPascal Brand { 19853bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 19953bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 20053bd332aSSY Chiu uint32_t target, target_shift; 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 206b0104773SPascal Brand /* Route it to selected CPUs */ 20753bd332aSSY Chiu target = read32(gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 20853bd332aSSY Chiu target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS; 20953bd332aSSY Chiu target &= ~(ITARGETSR_FIELD_MASK << target_shift); 21053bd332aSSY Chiu target |= cpu_mask << target_shift; 2111f60363aSJens Wiklander DMSG("cpu_mask: writing 0x%x to 0x%" PRIxVA, 21253bd332aSSY Chiu target, gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 21353bd332aSSY Chiu write32(target, gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG)); 214b0104773SPascal Brand DMSG("cpu_mask: 0x%x\n", 21553bd332aSSY Chiu read32(gic.gicd_base + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG))); 216b0104773SPascal Brand } 217b0104773SPascal Brand 218b0104773SPascal Brand void gic_it_set_prio(size_t it, uint8_t prio) 219b0104773SPascal Brand { 22053bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 22153bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 222b0104773SPascal Brand 223b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 224b0104773SPascal Brand /* Assigned to group0 */ 225b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 226b0104773SPascal Brand 227b0104773SPascal Brand /* Set prio it to selected CPUs */ 2281f60363aSJens Wiklander DMSG("prio: writing 0x%x to 0x%" PRIxVA, 229b0104773SPascal Brand prio, gic.gicd_base + GICD_IPRIORITYR(0) + it); 230b0104773SPascal Brand write8(prio, gic.gicd_base + GICD_IPRIORITYR(0) + it); 231b0104773SPascal Brand } 232b0104773SPascal Brand 233b0104773SPascal Brand void gic_it_enable(size_t it) 234b0104773SPascal Brand { 23553bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 23653bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 237b0104773SPascal Brand 238b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 239b0104773SPascal Brand /* Assigned to group0 */ 240b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 241b0104773SPascal Brand /* Not enabled yet */ 242b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_ISENABLER(idx)) & mask)); 243b0104773SPascal Brand 244b0104773SPascal Brand /* Enable the interrupt */ 245b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ISENABLER(idx)); 246b0104773SPascal Brand } 247b0104773SPascal Brand 248b0104773SPascal Brand void gic_it_disable(size_t it) 249b0104773SPascal Brand { 25053bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 25153bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 252b0104773SPascal Brand 253b0104773SPascal Brand assert(it <= gic.max_it); /* Not too large */ 254b0104773SPascal Brand /* Assigned to group0 */ 255b0104773SPascal Brand assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask)); 256b0104773SPascal Brand 257b0104773SPascal Brand /* Disable the interrupt */ 258b0104773SPascal Brand write32(mask, gic.gicd_base + GICD_ICENABLER(idx)); 259b0104773SPascal Brand } 260b0104773SPascal Brand 261b0104773SPascal Brand uint32_t gic_read_iar(void) 262b0104773SPascal Brand { 263b0104773SPascal Brand return read32(gic.gicc_base + GICC_IAR); 264b0104773SPascal Brand } 265b0104773SPascal Brand 266b0104773SPascal Brand void gic_write_eoir(uint32_t eoir) 267b0104773SPascal Brand { 268b0104773SPascal Brand write32(eoir, gic.gicc_base + GICC_EOIR); 269b0104773SPascal Brand } 270b0104773SPascal Brand 27153bd332aSSY Chiu bool gic_it_is_enabled(size_t it) { 27253bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 27353bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 27453bd332aSSY Chiu return !!(read32(gic.gicd_base + GICD_ISENABLER(idx)) & mask); 27553bd332aSSY Chiu } 27653bd332aSSY Chiu 27753bd332aSSY Chiu bool gic_it_get_group(size_t it) { 27853bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 27953bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 28053bd332aSSY Chiu return !!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask); 28153bd332aSSY Chiu } 28253bd332aSSY Chiu 28353bd332aSSY Chiu uint32_t gic_it_get_target(size_t it) { 28453bd332aSSY Chiu size_t reg_idx = it / NUM_TARGETS_PER_REG; 28553bd332aSSY Chiu uint32_t target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS; 28653bd332aSSY Chiu uint32_t target_mask = ITARGETSR_FIELD_MASK << target_shift; 28753bd332aSSY Chiu uint32_t target = 28853bd332aSSY Chiu read32(gic.gicd_base + GICD_ITARGETSR(reg_idx)) & target_mask; 28953bd332aSSY Chiu target = target >> target_shift; 29053bd332aSSY Chiu return target; 29153bd332aSSY Chiu } 29253bd332aSSY Chiu 29353bd332aSSY Chiu void gic_dump_state(void) 29453bd332aSSY Chiu { 29553bd332aSSY Chiu int i; 29653bd332aSSY Chiu DMSG("GICC_CTLR: 0x%x", read32(gic.gicc_base + GICC_CTLR)); 29753bd332aSSY Chiu DMSG("GICD_CTLR: 0x%x", read32(gic.gicd_base + GICD_CTLR)); 29853bd332aSSY Chiu 299ff97306fSJens Wiklander for (i = 0; i < (int)gic.max_it; i++) { 30053bd332aSSY Chiu if (gic_it_is_enabled(i)) { 30153bd332aSSY Chiu DMSG("irq%d: enabled, group:%d, target:%x", i, 30253bd332aSSY Chiu gic_it_get_group(i), gic_it_get_target(i)); 30353bd332aSSY Chiu } 30453bd332aSSY Chiu } 30553bd332aSSY Chiu } 306