11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 2b0104773SPascal Brand /* 318901324SDavid Wang * Copyright (c) 2016-2017, Linaro Limited 4b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 5b0104773SPascal Brand */ 6b0104773SPascal Brand 718901324SDavid Wang #include <arm.h> 88ddf5a4eSEtienne Carriere #include <assert.h> 967729d8dSLudovic Barre #include <config.h> 10b0104773SPascal Brand #include <drivers/gic.h> 110f93de74SEtienne Carriere #include <keep.h> 1267729d8dSLudovic Barre #include <kernel/dt.h> 137315b7b4SJens Wiklander #include <kernel/interrupt.h> 14d13278b8SEtienne Carriere #include <kernel/panic.h> 1567729d8dSLudovic Barre #include <libfdt.h> 167315b7b4SJens Wiklander #include <util.h> 17b0104773SPascal Brand #include <io.h> 184de4bebcSJens Wiklander #include <trace.h> 19b0104773SPascal Brand 20b0104773SPascal Brand /* Offsets from gic.gicc_base */ 21b0104773SPascal Brand #define GICC_CTLR (0x000) 2230a673e3SPeter Maydell #define GICC_PMR (0x004) 23b0104773SPascal Brand #define GICC_IAR (0x00C) 24b0104773SPascal Brand #define GICC_EOIR (0x010) 25b0104773SPascal Brand 26b0104773SPascal Brand #define GICC_CTLR_ENABLEGRP0 (1 << 0) 27b0104773SPascal Brand #define GICC_CTLR_ENABLEGRP1 (1 << 1) 281fcac774SSandeep Tripathy #define GICD_CTLR_ENABLEGRP1S (1 << 2) 29b0104773SPascal Brand #define GICC_CTLR_FIQEN (1 << 3) 30b0104773SPascal Brand 31b0104773SPascal Brand /* Offsets from gic.gicd_base */ 32b0104773SPascal Brand #define GICD_CTLR (0x000) 33b0104773SPascal Brand #define GICD_TYPER (0x004) 34b0104773SPascal Brand #define GICD_IGROUPR(n) (0x080 + (n) * 4) 35b0104773SPascal Brand #define GICD_ISENABLER(n) (0x100 + (n) * 4) 36b0104773SPascal Brand #define GICD_ICENABLER(n) (0x180 + (n) * 4) 3726ed70ecSGuanchao Liang #define GICD_ISPENDR(n) (0x200 + (n) * 4) 38b0104773SPascal Brand #define GICD_ICPENDR(n) (0x280 + (n) * 4) 39b0104773SPascal Brand #define GICD_IPRIORITYR(n) (0x400 + (n) * 4) 40b0104773SPascal Brand #define GICD_ITARGETSR(n) (0x800 + (n) * 4) 411fcac774SSandeep Tripathy #define GICD_IGROUPMODR(n) (0xd00 + (n) * 4) 4226ed70ecSGuanchao Liang #define GICD_SGIR (0xF00) 43b0104773SPascal Brand 44b0104773SPascal Brand #define GICD_CTLR_ENABLEGRP0 (1 << 0) 45b0104773SPascal Brand #define GICD_CTLR_ENABLEGRP1 (1 << 1) 46b0104773SPascal Brand 4753bd332aSSY Chiu /* Number of Private Peripheral Interrupt */ 4853bd332aSSY Chiu #define NUM_PPI 32 4953bd332aSSY Chiu 5026ed70ecSGuanchao Liang /* Number of Software Generated Interrupt */ 5126ed70ecSGuanchao Liang #define NUM_SGI 16 5226ed70ecSGuanchao Liang 5326ed70ecSGuanchao Liang /* Number of Non-secure Software Generated Interrupt */ 5426ed70ecSGuanchao Liang #define NUM_NS_SGI 8 5526ed70ecSGuanchao Liang 5653bd332aSSY Chiu /* Number of interrupts in one register */ 5753bd332aSSY Chiu #define NUM_INTS_PER_REG 32 5853bd332aSSY Chiu 5953bd332aSSY Chiu /* Number of targets in one register */ 6053bd332aSSY Chiu #define NUM_TARGETS_PER_REG 4 6153bd332aSSY Chiu 6253bd332aSSY Chiu /* Accessors to access ITARGETSRn */ 6353bd332aSSY Chiu #define ITARGETSR_FIELD_BITS 8 6453bd332aSSY Chiu #define ITARGETSR_FIELD_MASK 0xff 6553bd332aSSY Chiu 66b0104773SPascal Brand /* Maximum number of interrups a GIC can support */ 67b0104773SPascal Brand #define GIC_MAX_INTS 1020 68b0104773SPascal Brand 697315b7b4SJens Wiklander #define GICC_IAR_IT_ID_MASK 0x3ff 707315b7b4SJens Wiklander #define GICC_IAR_CPU_ID_MASK 0x7 717315b7b4SJens Wiklander #define GICC_IAR_CPU_ID_SHIFT 10 72b0104773SPascal Brand 73*702fe5a7SClément Léger static void gic_op_add(struct itr_chip *chip, size_t it, uint32_t type, 74*702fe5a7SClément Léger uint32_t prio); 757315b7b4SJens Wiklander static void gic_op_enable(struct itr_chip *chip, size_t it); 767315b7b4SJens Wiklander static void gic_op_disable(struct itr_chip *chip, size_t it); 7726ed70ecSGuanchao Liang static void gic_op_raise_pi(struct itr_chip *chip, size_t it); 7826ed70ecSGuanchao Liang static void gic_op_raise_sgi(struct itr_chip *chip, size_t it, 7926ed70ecSGuanchao Liang uint8_t cpu_mask); 8026ed70ecSGuanchao Liang static void gic_op_set_affinity(struct itr_chip *chip, size_t it, 8126ed70ecSGuanchao Liang uint8_t cpu_mask); 827315b7b4SJens Wiklander 837315b7b4SJens Wiklander static const struct itr_ops gic_ops = { 847315b7b4SJens Wiklander .add = gic_op_add, 857315b7b4SJens Wiklander .enable = gic_op_enable, 867315b7b4SJens Wiklander .disable = gic_op_disable, 8726ed70ecSGuanchao Liang .raise_pi = gic_op_raise_pi, 8826ed70ecSGuanchao Liang .raise_sgi = gic_op_raise_sgi, 8926ed70ecSGuanchao Liang .set_affinity = gic_op_set_affinity, 907315b7b4SJens Wiklander }; 913639b55fSJerome Forissier DECLARE_KEEP_PAGER(gic_ops); 927315b7b4SJens Wiklander 9318901324SDavid Wang static size_t probe_max_it(vaddr_t gicc_base __maybe_unused, vaddr_t gicd_base) 94b0104773SPascal Brand { 95b0104773SPascal Brand int i; 96b0104773SPascal Brand uint32_t old_ctlr; 97b0104773SPascal Brand size_t ret = 0; 9879f008d3SJens Wiklander const size_t max_regs = ((GIC_MAX_INTS + NUM_INTS_PER_REG - 1) / 9979f008d3SJens Wiklander NUM_INTS_PER_REG) - 1; 100b0104773SPascal Brand 101b0104773SPascal Brand /* 102b0104773SPascal Brand * Probe which interrupt number is the largest. 103b0104773SPascal Brand */ 10418901324SDavid Wang #if defined(CFG_ARM_GICV3) 10518901324SDavid Wang old_ctlr = read_icc_ctlr(); 10618901324SDavid Wang write_icc_ctlr(0); 10718901324SDavid Wang #else 108918bb3a5SEtienne Carriere old_ctlr = io_read32(gicc_base + GICC_CTLR); 109918bb3a5SEtienne Carriere io_write32(gicc_base + GICC_CTLR, 0); 11018901324SDavid Wang #endif 11179f008d3SJens Wiklander for (i = max_regs; i >= 0; i--) { 112b0104773SPascal Brand uint32_t old_reg; 113b0104773SPascal Brand uint32_t reg; 114b0104773SPascal Brand int b; 115b0104773SPascal Brand 116918bb3a5SEtienne Carriere old_reg = io_read32(gicd_base + GICD_ISENABLER(i)); 117918bb3a5SEtienne Carriere io_write32(gicd_base + GICD_ISENABLER(i), 0xffffffff); 118918bb3a5SEtienne Carriere reg = io_read32(gicd_base + GICD_ISENABLER(i)); 119918bb3a5SEtienne Carriere io_write32(gicd_base + GICD_ICENABLER(i), ~old_reg); 12079f008d3SJens Wiklander for (b = NUM_INTS_PER_REG - 1; b >= 0; b--) { 121007a97a2SJens Wiklander if (BIT32(b) & reg) { 12253bd332aSSY Chiu ret = i * NUM_INTS_PER_REG + b; 123b0104773SPascal Brand goto out; 124b0104773SPascal Brand } 125b0104773SPascal Brand } 126b0104773SPascal Brand } 127b0104773SPascal Brand out: 12818901324SDavid Wang #if defined(CFG_ARM_GICV3) 12918901324SDavid Wang write_icc_ctlr(old_ctlr); 13018901324SDavid Wang #else 131918bb3a5SEtienne Carriere io_write32(gicc_base + GICC_CTLR, old_ctlr); 13218901324SDavid Wang #endif 133b0104773SPascal Brand return ret; 134b0104773SPascal Brand } 135b0104773SPascal Brand 1367315b7b4SJens Wiklander void gic_cpu_init(struct gic_data *gd) 137bedc2b9fSsunny { 13818901324SDavid Wang #if defined(CFG_ARM_GICV3) 13918901324SDavid Wang assert(gd->gicd_base); 14018901324SDavid Wang #else 14105efe1e1SEtienne Carriere assert(gd->gicd_base && gd->gicc_base); 14218901324SDavid Wang #endif 14305efe1e1SEtienne Carriere 144e06e6e74SPeter Maydell /* per-CPU interrupts 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 */ 149918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_IGROUPR(0), 0xffff00ff); 150bedc2b9fSsunny 15130a673e3SPeter Maydell /* Set the priority mask to permit Non-secure interrupts, and to 15230a673e3SPeter Maydell * allow the Non-secure world to adjust the priority mask itself 15330a673e3SPeter Maydell */ 15418901324SDavid Wang #if defined(CFG_ARM_GICV3) 15518901324SDavid Wang write_icc_pmr(0x80); 1561fcac774SSandeep Tripathy write_icc_igrpen1(1); 15718901324SDavid Wang #else 158918bb3a5SEtienne Carriere io_write32(gd->gicc_base + GICC_PMR, 0x80); 15930a673e3SPeter Maydell 160bedc2b9fSsunny /* Enable GIC */ 161918bb3a5SEtienne Carriere io_write32(gd->gicc_base + GICC_CTLR, 162918bb3a5SEtienne Carriere GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1 | 163918bb3a5SEtienne Carriere GICC_CTLR_FIQEN); 16418901324SDavid Wang #endif 165bedc2b9fSsunny } 166bedc2b9fSsunny 16718901324SDavid Wang void gic_init(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, 16818901324SDavid Wang vaddr_t gicd_base) 169b0104773SPascal Brand { 170b0104773SPascal Brand size_t n; 171b0104773SPascal Brand 1727315b7b4SJens Wiklander gic_init_base_addr(gd, gicc_base, gicd_base); 173b0104773SPascal Brand 1747315b7b4SJens Wiklander for (n = 0; n <= gd->max_it / NUM_INTS_PER_REG; n++) { 175b0104773SPascal Brand /* Disable interrupts */ 176918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_ICENABLER(n), 0xffffffff); 177b0104773SPascal Brand 178b0104773SPascal Brand /* Make interrupts non-pending */ 179918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_ICPENDR(n), 0xffffffff); 180b0104773SPascal Brand 181b0104773SPascal Brand /* Mark interrupts non-secure */ 182bedc2b9fSsunny if (n == 0) { 183bedc2b9fSsunny /* per-CPU inerrupts config: 184bedc2b9fSsunny * ID0-ID7(SGI) for Non-secure interrupts 185bedc2b9fSsunny * ID8-ID15(SGI) for Secure interrupts. 186bedc2b9fSsunny * All PPI config as Non-secure interrupts. 187bedc2b9fSsunny */ 188918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_IGROUPR(n), 0xffff00ff); 189bedc2b9fSsunny } else { 190918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_IGROUPR(n), 0xffffffff); 191b0104773SPascal Brand } 192bedc2b9fSsunny } 193b0104773SPascal Brand 19430a673e3SPeter Maydell /* Set the priority mask to permit Non-secure interrupts, and to 19530a673e3SPeter Maydell * allow the Non-secure world to adjust the priority mask itself 19630a673e3SPeter Maydell */ 19718901324SDavid Wang #if defined(CFG_ARM_GICV3) 19818901324SDavid Wang write_icc_pmr(0x80); 1991fcac774SSandeep Tripathy write_icc_igrpen1(1); 2001fcac774SSandeep Tripathy io_setbits32(gd->gicd_base + GICD_CTLR, GICD_CTLR_ENABLEGRP1S); 20118901324SDavid Wang #else 202918bb3a5SEtienne Carriere io_write32(gd->gicc_base + GICC_PMR, 0x80); 20330a673e3SPeter Maydell 204b0104773SPascal Brand /* Enable GIC */ 205918bb3a5SEtienne Carriere io_write32(gd->gicc_base + GICC_CTLR, GICC_CTLR_FIQEN | 206918bb3a5SEtienne Carriere GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1); 207918bb3a5SEtienne Carriere io_setbits32(gd->gicd_base + GICD_CTLR, 208918bb3a5SEtienne Carriere GICD_CTLR_ENABLEGRP0 | GICD_CTLR_ENABLEGRP1); 2091fcac774SSandeep Tripathy #endif 210b0104773SPascal Brand } 211b0104773SPascal Brand 212*702fe5a7SClément Léger static int gic_dt_get_irq(const uint32_t *properties, int count, uint32_t *type, 213*702fe5a7SClément Léger uint32_t *prio) 21467729d8dSLudovic Barre { 215ed74d1c4SLudovic Barre int it_num = DT_INFO_INVALID_INTERRUPT; 216ed74d1c4SLudovic Barre 217*702fe5a7SClément Léger if (type) 218*702fe5a7SClément Léger *type = IRQ_TYPE_NONE; 219*702fe5a7SClément Léger 220*702fe5a7SClément Léger if (prio) 221*702fe5a7SClément Léger *prio = 0; 222*702fe5a7SClément Léger 223888bb63dSClément Léger if (!properties || count < 2) 22467729d8dSLudovic Barre return DT_INFO_INVALID_INTERRUPT; 22567729d8dSLudovic Barre 226ed74d1c4SLudovic Barre it_num = fdt32_to_cpu(properties[1]); 227ed74d1c4SLudovic Barre 228ed74d1c4SLudovic Barre switch (fdt32_to_cpu(properties[0])) { 229ed74d1c4SLudovic Barre case 1: 230ed74d1c4SLudovic Barre it_num += 16; 231ed74d1c4SLudovic Barre break; 232ed74d1c4SLudovic Barre case 0: 233ed74d1c4SLudovic Barre it_num += 32; 234ed74d1c4SLudovic Barre break; 235ed74d1c4SLudovic Barre default: 236ed74d1c4SLudovic Barre it_num = DT_INFO_INVALID_INTERRUPT; 237ed74d1c4SLudovic Barre } 238ed74d1c4SLudovic Barre 239ed74d1c4SLudovic Barre return it_num; 24067729d8dSLudovic Barre } 24167729d8dSLudovic Barre 24218901324SDavid Wang void gic_init_base_addr(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, 2437315b7b4SJens Wiklander vaddr_t gicd_base) 24453bd332aSSY Chiu { 2457315b7b4SJens Wiklander gd->gicc_base = gicc_base; 2467315b7b4SJens Wiklander gd->gicd_base = gicd_base; 2477315b7b4SJens Wiklander gd->max_it = probe_max_it(gicc_base, gicd_base); 2487315b7b4SJens Wiklander gd->chip.ops = &gic_ops; 24967729d8dSLudovic Barre 25067729d8dSLudovic Barre if (IS_ENABLED(CFG_DT)) 25167729d8dSLudovic Barre gd->chip.dt_get_irq = gic_dt_get_irq; 25253bd332aSSY Chiu } 25353bd332aSSY Chiu 2547315b7b4SJens Wiklander static void gic_it_add(struct gic_data *gd, size_t it) 255b0104773SPascal Brand { 25653bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 25753bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 258b0104773SPascal Brand 259b0104773SPascal Brand /* Disable the interrupt */ 260918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_ICENABLER(idx), mask); 261b0104773SPascal Brand /* Make it non-pending */ 262918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_ICPENDR(idx), mask); 263b0104773SPascal Brand /* Assign it to group0 */ 264918bb3a5SEtienne Carriere io_clrbits32(gd->gicd_base + GICD_IGROUPR(idx), mask); 2651fcac774SSandeep Tripathy #if defined(CFG_ARM_GICV3) 2661fcac774SSandeep Tripathy /* Assign it to group1S */ 2671fcac774SSandeep Tripathy io_setbits32(gd->gicd_base + GICD_IGROUPMODR(idx), mask); 2681fcac774SSandeep Tripathy #endif 269b0104773SPascal Brand } 270b0104773SPascal Brand 2717315b7b4SJens Wiklander static void gic_it_set_cpu_mask(struct gic_data *gd, size_t it, 2727315b7b4SJens Wiklander uint8_t cpu_mask) 273b0104773SPascal Brand { 2748ddf5a4eSEtienne Carriere size_t idx __maybe_unused = it / NUM_INTS_PER_REG; 2758ddf5a4eSEtienne Carriere uint32_t mask __maybe_unused = 1 << (it % NUM_INTS_PER_REG); 27653bd332aSSY Chiu uint32_t target, target_shift; 277918bb3a5SEtienne Carriere vaddr_t itargetsr = gd->gicd_base + 278918bb3a5SEtienne Carriere GICD_ITARGETSR(it / NUM_TARGETS_PER_REG); 279b0104773SPascal Brand 280b0104773SPascal Brand /* Assigned to group0 */ 281918bb3a5SEtienne Carriere assert(!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask)); 282b0104773SPascal Brand 283b0104773SPascal Brand /* Route it to selected CPUs */ 284918bb3a5SEtienne Carriere target = io_read32(itargetsr); 28553bd332aSSY Chiu target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS; 28653bd332aSSY Chiu target &= ~(ITARGETSR_FIELD_MASK << target_shift); 28753bd332aSSY Chiu target |= cpu_mask << target_shift; 288918bb3a5SEtienne Carriere DMSG("cpu_mask: writing 0x%x to 0x%" PRIxVA, target, itargetsr); 289918bb3a5SEtienne Carriere io_write32(itargetsr, target); 290918bb3a5SEtienne Carriere DMSG("cpu_mask: 0x%x", io_read32(itargetsr)); 291b0104773SPascal Brand } 292b0104773SPascal Brand 2937315b7b4SJens Wiklander static void gic_it_set_prio(struct gic_data *gd, size_t it, uint8_t prio) 294b0104773SPascal Brand { 2958ddf5a4eSEtienne Carriere size_t idx __maybe_unused = it / NUM_INTS_PER_REG; 2968ddf5a4eSEtienne Carriere uint32_t mask __maybe_unused = 1 << (it % NUM_INTS_PER_REG); 297b0104773SPascal Brand 298b0104773SPascal Brand /* Assigned to group0 */ 299918bb3a5SEtienne Carriere assert(!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask)); 300b0104773SPascal Brand 301b0104773SPascal Brand /* Set prio it to selected CPUs */ 3021f60363aSJens Wiklander DMSG("prio: writing 0x%x to 0x%" PRIxVA, 3037315b7b4SJens Wiklander prio, gd->gicd_base + GICD_IPRIORITYR(0) + it); 304918bb3a5SEtienne Carriere io_write8(gd->gicd_base + GICD_IPRIORITYR(0) + it, prio); 305b0104773SPascal Brand } 306b0104773SPascal Brand 3077315b7b4SJens Wiklander static void gic_it_enable(struct gic_data *gd, size_t it) 308b0104773SPascal Brand { 30953bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 31053bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 311918bb3a5SEtienne Carriere vaddr_t base = gd->gicd_base; 312b0104773SPascal Brand 313b0104773SPascal Brand /* Assigned to group0 */ 314918bb3a5SEtienne Carriere assert(!(io_read32(base + GICD_IGROUPR(idx)) & mask)); 315b0104773SPascal Brand 316b0104773SPascal Brand /* Enable the interrupt */ 317918bb3a5SEtienne Carriere io_write32(base + GICD_ISENABLER(idx), mask); 318b0104773SPascal Brand } 319b0104773SPascal Brand 3207315b7b4SJens Wiklander static void gic_it_disable(struct gic_data *gd, size_t it) 321b0104773SPascal Brand { 32253bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 32353bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 324b0104773SPascal Brand 325b0104773SPascal Brand /* Assigned to group0 */ 326918bb3a5SEtienne Carriere assert(!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask)); 327b0104773SPascal Brand 328b0104773SPascal Brand /* Disable the interrupt */ 329918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_ICENABLER(idx), mask); 330b0104773SPascal Brand } 331b0104773SPascal Brand 33226ed70ecSGuanchao Liang static void gic_it_set_pending(struct gic_data *gd, size_t it) 33326ed70ecSGuanchao Liang { 33426ed70ecSGuanchao Liang size_t idx = it / NUM_INTS_PER_REG; 33526ed70ecSGuanchao Liang uint32_t mask = BIT32(it % NUM_INTS_PER_REG); 33626ed70ecSGuanchao Liang 33726ed70ecSGuanchao Liang /* Should be Peripheral Interrupt */ 33826ed70ecSGuanchao Liang assert(it >= NUM_SGI); 33926ed70ecSGuanchao Liang 34026ed70ecSGuanchao Liang /* Raise the interrupt */ 341918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_ISPENDR(idx), mask); 34226ed70ecSGuanchao Liang } 34326ed70ecSGuanchao Liang 34426ed70ecSGuanchao Liang static void gic_it_raise_sgi(struct gic_data *gd, size_t it, 34526ed70ecSGuanchao Liang uint8_t cpu_mask, uint8_t group) 34626ed70ecSGuanchao Liang { 34726ed70ecSGuanchao Liang uint32_t mask_id = it & 0xf; 34826ed70ecSGuanchao Liang uint32_t mask_group = group & 0x1; 34926ed70ecSGuanchao Liang uint32_t mask_cpu = cpu_mask & 0xff; 35026ed70ecSGuanchao Liang uint32_t mask = (mask_id | SHIFT_U32(mask_group, 15) | 35126ed70ecSGuanchao Liang SHIFT_U32(mask_cpu, 16)); 35226ed70ecSGuanchao Liang 35326ed70ecSGuanchao Liang /* Should be Software Generated Interrupt */ 35426ed70ecSGuanchao Liang assert(it < NUM_SGI); 35526ed70ecSGuanchao Liang 35626ed70ecSGuanchao Liang /* Raise the interrupt */ 357918bb3a5SEtienne Carriere io_write32(gd->gicd_base + GICD_SGIR, mask); 35826ed70ecSGuanchao Liang } 35926ed70ecSGuanchao Liang 36018901324SDavid Wang static uint32_t gic_read_iar(struct gic_data *gd __maybe_unused) 361b0104773SPascal Brand { 36218901324SDavid Wang #if defined(CFG_ARM_GICV3) 3631de462e1SSumit Garg return read_icc_iar1(); 36418901324SDavid Wang #else 365918bb3a5SEtienne Carriere return io_read32(gd->gicc_base + GICC_IAR); 36618901324SDavid Wang #endif 367b0104773SPascal Brand } 368b0104773SPascal Brand 36918901324SDavid Wang static void gic_write_eoir(struct gic_data *gd __maybe_unused, uint32_t eoir) 370b0104773SPascal Brand { 37118901324SDavid Wang #if defined(CFG_ARM_GICV3) 3721de462e1SSumit Garg write_icc_eoir1(eoir); 37318901324SDavid Wang #else 374918bb3a5SEtienne Carriere io_write32(gd->gicc_base + GICC_EOIR, eoir); 37518901324SDavid Wang #endif 376b0104773SPascal Brand } 377b0104773SPascal Brand 3787315b7b4SJens Wiklander static bool gic_it_is_enabled(struct gic_data *gd, size_t it) 3797315b7b4SJens Wiklander { 38053bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 38153bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 382918bb3a5SEtienne Carriere return !!(io_read32(gd->gicd_base + GICD_ISENABLER(idx)) & mask); 38353bd332aSSY Chiu } 38453bd332aSSY Chiu 3857315b7b4SJens Wiklander static bool __maybe_unused gic_it_get_group(struct gic_data *gd, size_t it) 3867315b7b4SJens Wiklander { 38753bd332aSSY Chiu size_t idx = it / NUM_INTS_PER_REG; 38853bd332aSSY Chiu uint32_t mask = 1 << (it % NUM_INTS_PER_REG); 389918bb3a5SEtienne Carriere return !!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask); 39053bd332aSSY Chiu } 39153bd332aSSY Chiu 3927315b7b4SJens Wiklander static uint32_t __maybe_unused gic_it_get_target(struct gic_data *gd, size_t it) 3937315b7b4SJens Wiklander { 39453bd332aSSY Chiu size_t reg_idx = it / NUM_TARGETS_PER_REG; 3957315b7b4SJens Wiklander uint32_t target_shift = (it % NUM_TARGETS_PER_REG) * 3967315b7b4SJens Wiklander ITARGETSR_FIELD_BITS; 39753bd332aSSY Chiu uint32_t target_mask = ITARGETSR_FIELD_MASK << target_shift; 398918bb3a5SEtienne Carriere uint32_t target = io_read32(gd->gicd_base + GICD_ITARGETSR(reg_idx)); 3997315b7b4SJens Wiklander 400918bb3a5SEtienne Carriere return (target & target_mask) >> target_shift; 40153bd332aSSY Chiu } 40253bd332aSSY Chiu 4037315b7b4SJens Wiklander void gic_dump_state(struct gic_data *gd) 40453bd332aSSY Chiu { 40553bd332aSSY Chiu int i; 40653bd332aSSY Chiu 40718901324SDavid Wang #if defined(CFG_ARM_GICV3) 40818901324SDavid Wang DMSG("GICC_CTLR: 0x%x", read_icc_ctlr()); 40918901324SDavid Wang #else 410918bb3a5SEtienne Carriere DMSG("GICC_CTLR: 0x%x", io_read32(gd->gicc_base + GICC_CTLR)); 41118901324SDavid Wang #endif 412918bb3a5SEtienne Carriere DMSG("GICD_CTLR: 0x%x", io_read32(gd->gicd_base + GICD_CTLR)); 4137315b7b4SJens Wiklander 4144a9ea08cSFangsuo Wu for (i = 0; i <= (int)gd->max_it; i++) { 4157315b7b4SJens Wiklander if (gic_it_is_enabled(gd, i)) { 41653bd332aSSY Chiu DMSG("irq%d: enabled, group:%d, target:%x", i, 4177315b7b4SJens Wiklander gic_it_get_group(gd, i), gic_it_get_target(gd, i)); 41853bd332aSSY Chiu } 41953bd332aSSY Chiu } 42053bd332aSSY Chiu } 4217315b7b4SJens Wiklander 4227315b7b4SJens Wiklander void gic_it_handle(struct gic_data *gd) 4237315b7b4SJens Wiklander { 4247315b7b4SJens Wiklander uint32_t iar; 4257315b7b4SJens Wiklander uint32_t id; 4267315b7b4SJens Wiklander 4277315b7b4SJens Wiklander iar = gic_read_iar(gd); 4287315b7b4SJens Wiklander id = iar & GICC_IAR_IT_ID_MASK; 4297315b7b4SJens Wiklander 4304a9ea08cSFangsuo Wu if (id <= gd->max_it) 4317315b7b4SJens Wiklander itr_handle(id); 4323b3a4611SMathieu Briand else 4333b3a4611SMathieu Briand DMSG("ignoring interrupt %" PRIu32, id); 4347315b7b4SJens Wiklander 4357315b7b4SJens Wiklander gic_write_eoir(gd, iar); 4367315b7b4SJens Wiklander } 4377315b7b4SJens Wiklander 4387315b7b4SJens Wiklander static void gic_op_add(struct itr_chip *chip, size_t it, 439*702fe5a7SClément Léger uint32_t type __unused, 440*702fe5a7SClément Léger uint32_t prio __unused) 4417315b7b4SJens Wiklander { 4427315b7b4SJens Wiklander struct gic_data *gd = container_of(chip, struct gic_data, chip); 4437315b7b4SJens Wiklander 4444a9ea08cSFangsuo Wu if (it > gd->max_it) 445d13278b8SEtienne Carriere panic(); 446d13278b8SEtienne Carriere 4477315b7b4SJens Wiklander gic_it_add(gd, it); 4487315b7b4SJens Wiklander /* Set the CPU mask to deliver interrupts to any online core */ 4497315b7b4SJens Wiklander gic_it_set_cpu_mask(gd, it, 0xff); 4507315b7b4SJens Wiklander gic_it_set_prio(gd, it, 0x1); 4517315b7b4SJens Wiklander } 4527315b7b4SJens Wiklander 4537315b7b4SJens Wiklander static void gic_op_enable(struct itr_chip *chip, size_t it) 4547315b7b4SJens Wiklander { 4557315b7b4SJens Wiklander struct gic_data *gd = container_of(chip, struct gic_data, chip); 4567315b7b4SJens Wiklander 4574a9ea08cSFangsuo Wu if (it > gd->max_it) 458d13278b8SEtienne Carriere panic(); 459d13278b8SEtienne Carriere 4607315b7b4SJens Wiklander gic_it_enable(gd, it); 4617315b7b4SJens Wiklander } 4627315b7b4SJens Wiklander 4637315b7b4SJens Wiklander static void gic_op_disable(struct itr_chip *chip, size_t it) 4647315b7b4SJens Wiklander { 4657315b7b4SJens Wiklander struct gic_data *gd = container_of(chip, struct gic_data, chip); 4667315b7b4SJens Wiklander 4674a9ea08cSFangsuo Wu if (it > gd->max_it) 468d13278b8SEtienne Carriere panic(); 469d13278b8SEtienne Carriere 4707315b7b4SJens Wiklander gic_it_disable(gd, it); 4717315b7b4SJens Wiklander } 47226ed70ecSGuanchao Liang 47326ed70ecSGuanchao Liang static void gic_op_raise_pi(struct itr_chip *chip, size_t it) 47426ed70ecSGuanchao Liang { 47526ed70ecSGuanchao Liang struct gic_data *gd = container_of(chip, struct gic_data, chip); 47626ed70ecSGuanchao Liang 4774a9ea08cSFangsuo Wu if (it > gd->max_it) 47826ed70ecSGuanchao Liang panic(); 47926ed70ecSGuanchao Liang 48026ed70ecSGuanchao Liang gic_it_set_pending(gd, it); 48126ed70ecSGuanchao Liang } 48226ed70ecSGuanchao Liang 48326ed70ecSGuanchao Liang static void gic_op_raise_sgi(struct itr_chip *chip, size_t it, 48426ed70ecSGuanchao Liang uint8_t cpu_mask) 48526ed70ecSGuanchao Liang { 48626ed70ecSGuanchao Liang struct gic_data *gd = container_of(chip, struct gic_data, chip); 48726ed70ecSGuanchao Liang 4884a9ea08cSFangsuo Wu if (it > gd->max_it) 48926ed70ecSGuanchao Liang panic(); 49026ed70ecSGuanchao Liang 49126ed70ecSGuanchao Liang if (it < NUM_NS_SGI) 49226ed70ecSGuanchao Liang gic_it_raise_sgi(gd, it, cpu_mask, 1); 49326ed70ecSGuanchao Liang else 49426ed70ecSGuanchao Liang gic_it_raise_sgi(gd, it, cpu_mask, 0); 49526ed70ecSGuanchao Liang } 49626ed70ecSGuanchao Liang static void gic_op_set_affinity(struct itr_chip *chip, size_t it, 49726ed70ecSGuanchao Liang uint8_t cpu_mask) 49826ed70ecSGuanchao Liang { 49926ed70ecSGuanchao Liang struct gic_data *gd = container_of(chip, struct gic_data, chip); 50026ed70ecSGuanchao Liang 5014a9ea08cSFangsuo Wu if (it > gd->max_it) 50226ed70ecSGuanchao Liang panic(); 50326ed70ecSGuanchao Liang 50426ed70ecSGuanchao Liang gic_it_set_cpu_mask(gd, it, cpu_mask); 50526ed70ecSGuanchao Liang } 506