xref: /optee_os/core/drivers/gic.c (revision ed74d1c4e2fb36a55697761a0e1c6cf47884e144)
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 
737315b7b4SJens Wiklander static void gic_op_add(struct itr_chip *chip, size_t it, uint32_t flags);
747315b7b4SJens Wiklander static void gic_op_enable(struct itr_chip *chip, size_t it);
757315b7b4SJens Wiklander static void gic_op_disable(struct itr_chip *chip, size_t it);
7626ed70ecSGuanchao Liang static void gic_op_raise_pi(struct itr_chip *chip, size_t it);
7726ed70ecSGuanchao Liang static void gic_op_raise_sgi(struct itr_chip *chip, size_t it,
7826ed70ecSGuanchao Liang 			uint8_t cpu_mask);
7926ed70ecSGuanchao Liang static void gic_op_set_affinity(struct itr_chip *chip, size_t it,
8026ed70ecSGuanchao Liang 			uint8_t cpu_mask);
817315b7b4SJens Wiklander 
827315b7b4SJens Wiklander static const struct itr_ops gic_ops = {
837315b7b4SJens Wiklander 	.add = gic_op_add,
847315b7b4SJens Wiklander 	.enable = gic_op_enable,
857315b7b4SJens Wiklander 	.disable = gic_op_disable,
8626ed70ecSGuanchao Liang 	.raise_pi = gic_op_raise_pi,
8726ed70ecSGuanchao Liang 	.raise_sgi = gic_op_raise_sgi,
8826ed70ecSGuanchao Liang 	.set_affinity = gic_op_set_affinity,
897315b7b4SJens Wiklander };
903639b55fSJerome Forissier DECLARE_KEEP_PAGER(gic_ops);
917315b7b4SJens Wiklander 
9218901324SDavid Wang static size_t probe_max_it(vaddr_t gicc_base __maybe_unused, vaddr_t gicd_base)
93b0104773SPascal Brand {
94b0104773SPascal Brand 	int i;
95b0104773SPascal Brand 	uint32_t old_ctlr;
96b0104773SPascal Brand 	size_t ret = 0;
9779f008d3SJens Wiklander 	const size_t max_regs = ((GIC_MAX_INTS + NUM_INTS_PER_REG - 1) /
9879f008d3SJens Wiklander 					NUM_INTS_PER_REG) - 1;
99b0104773SPascal Brand 
100b0104773SPascal Brand 	/*
101b0104773SPascal Brand 	 * Probe which interrupt number is the largest.
102b0104773SPascal Brand 	 */
10318901324SDavid Wang #if defined(CFG_ARM_GICV3)
10418901324SDavid Wang 	old_ctlr = read_icc_ctlr();
10518901324SDavid Wang 	write_icc_ctlr(0);
10618901324SDavid Wang #else
107918bb3a5SEtienne Carriere 	old_ctlr = io_read32(gicc_base + GICC_CTLR);
108918bb3a5SEtienne Carriere 	io_write32(gicc_base + GICC_CTLR, 0);
10918901324SDavid Wang #endif
11079f008d3SJens Wiklander 	for (i = max_regs; i >= 0; i--) {
111b0104773SPascal Brand 		uint32_t old_reg;
112b0104773SPascal Brand 		uint32_t reg;
113b0104773SPascal Brand 		int b;
114b0104773SPascal Brand 
115918bb3a5SEtienne Carriere 		old_reg = io_read32(gicd_base + GICD_ISENABLER(i));
116918bb3a5SEtienne Carriere 		io_write32(gicd_base + GICD_ISENABLER(i), 0xffffffff);
117918bb3a5SEtienne Carriere 		reg = io_read32(gicd_base + GICD_ISENABLER(i));
118918bb3a5SEtienne Carriere 		io_write32(gicd_base + GICD_ICENABLER(i), ~old_reg);
11979f008d3SJens Wiklander 		for (b = NUM_INTS_PER_REG - 1; b >= 0; b--) {
120007a97a2SJens Wiklander 			if (BIT32(b) & reg) {
12153bd332aSSY Chiu 				ret = i * NUM_INTS_PER_REG + b;
122b0104773SPascal Brand 				goto out;
123b0104773SPascal Brand 			}
124b0104773SPascal Brand 		}
125b0104773SPascal Brand 	}
126b0104773SPascal Brand out:
12718901324SDavid Wang #if defined(CFG_ARM_GICV3)
12818901324SDavid Wang 	write_icc_ctlr(old_ctlr);
12918901324SDavid Wang #else
130918bb3a5SEtienne Carriere 	io_write32(gicc_base + GICC_CTLR, old_ctlr);
13118901324SDavid Wang #endif
132b0104773SPascal Brand 	return ret;
133b0104773SPascal Brand }
134b0104773SPascal Brand 
1357315b7b4SJens Wiklander void gic_cpu_init(struct gic_data *gd)
136bedc2b9fSsunny {
13718901324SDavid Wang #if defined(CFG_ARM_GICV3)
13818901324SDavid Wang 	assert(gd->gicd_base);
13918901324SDavid Wang #else
14005efe1e1SEtienne Carriere 	assert(gd->gicd_base && gd->gicc_base);
14118901324SDavid Wang #endif
14205efe1e1SEtienne Carriere 
143e06e6e74SPeter Maydell 	/* per-CPU interrupts config:
144bedc2b9fSsunny 	 * ID0-ID7(SGI)   for Non-secure interrupts
145bedc2b9fSsunny 	 * ID8-ID15(SGI)  for Secure interrupts.
146bedc2b9fSsunny 	 * All PPI config as Non-secure interrupts.
147bedc2b9fSsunny 	 */
148918bb3a5SEtienne Carriere 	io_write32(gd->gicd_base + GICD_IGROUPR(0), 0xffff00ff);
149bedc2b9fSsunny 
15030a673e3SPeter Maydell 	/* Set the priority mask to permit Non-secure interrupts, and to
15130a673e3SPeter Maydell 	 * allow the Non-secure world to adjust the priority mask itself
15230a673e3SPeter Maydell 	 */
15318901324SDavid Wang #if defined(CFG_ARM_GICV3)
15418901324SDavid Wang 	write_icc_pmr(0x80);
1551fcac774SSandeep Tripathy 	write_icc_igrpen1(1);
15618901324SDavid Wang #else
157918bb3a5SEtienne Carriere 	io_write32(gd->gicc_base + GICC_PMR, 0x80);
15830a673e3SPeter Maydell 
159bedc2b9fSsunny 	/* Enable GIC */
160918bb3a5SEtienne Carriere 	io_write32(gd->gicc_base + GICC_CTLR,
161918bb3a5SEtienne Carriere 		   GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1 |
162918bb3a5SEtienne Carriere 		   GICC_CTLR_FIQEN);
16318901324SDavid Wang #endif
164bedc2b9fSsunny }
165bedc2b9fSsunny 
16618901324SDavid Wang void gic_init(struct gic_data *gd, vaddr_t gicc_base __maybe_unused,
16718901324SDavid Wang 	      vaddr_t gicd_base)
168b0104773SPascal Brand {
169b0104773SPascal Brand 	size_t n;
170b0104773SPascal Brand 
1717315b7b4SJens Wiklander 	gic_init_base_addr(gd, gicc_base, gicd_base);
172b0104773SPascal Brand 
1737315b7b4SJens Wiklander 	for (n = 0; n <= gd->max_it / NUM_INTS_PER_REG; n++) {
174b0104773SPascal Brand 		/* Disable interrupts */
175918bb3a5SEtienne Carriere 		io_write32(gd->gicd_base + GICD_ICENABLER(n), 0xffffffff);
176b0104773SPascal Brand 
177b0104773SPascal Brand 		/* Make interrupts non-pending */
178918bb3a5SEtienne Carriere 		io_write32(gd->gicd_base + GICD_ICPENDR(n), 0xffffffff);
179b0104773SPascal Brand 
180b0104773SPascal Brand 		/* Mark interrupts non-secure */
181bedc2b9fSsunny 		if (n == 0) {
182bedc2b9fSsunny 			/* per-CPU inerrupts config:
183bedc2b9fSsunny                          * ID0-ID7(SGI)   for Non-secure interrupts
184bedc2b9fSsunny                          * ID8-ID15(SGI)  for Secure interrupts.
185bedc2b9fSsunny                          * All PPI config as Non-secure interrupts.
186bedc2b9fSsunny 			 */
187918bb3a5SEtienne Carriere 			io_write32(gd->gicd_base + GICD_IGROUPR(n), 0xffff00ff);
188bedc2b9fSsunny 		} else {
189918bb3a5SEtienne Carriere 			io_write32(gd->gicd_base + GICD_IGROUPR(n), 0xffffffff);
190b0104773SPascal Brand 		}
191bedc2b9fSsunny 	}
192b0104773SPascal Brand 
19330a673e3SPeter Maydell 	/* Set the priority mask to permit Non-secure interrupts, and to
19430a673e3SPeter Maydell 	 * allow the Non-secure world to adjust the priority mask itself
19530a673e3SPeter Maydell 	 */
19618901324SDavid Wang #if defined(CFG_ARM_GICV3)
19718901324SDavid Wang 	write_icc_pmr(0x80);
1981fcac774SSandeep Tripathy 	write_icc_igrpen1(1);
1991fcac774SSandeep Tripathy 	io_setbits32(gd->gicd_base + GICD_CTLR, GICD_CTLR_ENABLEGRP1S);
20018901324SDavid Wang #else
201918bb3a5SEtienne Carriere 	io_write32(gd->gicc_base + GICC_PMR, 0x80);
20230a673e3SPeter Maydell 
203b0104773SPascal Brand 	/* Enable GIC */
204918bb3a5SEtienne Carriere 	io_write32(gd->gicc_base + GICC_CTLR, GICC_CTLR_FIQEN |
205918bb3a5SEtienne Carriere 		   GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1);
206918bb3a5SEtienne Carriere 	io_setbits32(gd->gicd_base + GICD_CTLR,
207918bb3a5SEtienne Carriere 		     GICD_CTLR_ENABLEGRP0 | GICD_CTLR_ENABLEGRP1);
2081fcac774SSandeep Tripathy #endif
209b0104773SPascal Brand }
210b0104773SPascal Brand 
21167729d8dSLudovic Barre static int gic_dt_get_irq(const uint32_t *properties, int len)
21267729d8dSLudovic Barre {
213*ed74d1c4SLudovic Barre 	int it_num = DT_INFO_INVALID_INTERRUPT;
214*ed74d1c4SLudovic Barre 
21567729d8dSLudovic Barre 	if (!properties || len < 2)
21667729d8dSLudovic Barre 		return DT_INFO_INVALID_INTERRUPT;
21767729d8dSLudovic Barre 
218*ed74d1c4SLudovic Barre 	it_num = fdt32_to_cpu(properties[1]);
219*ed74d1c4SLudovic Barre 
220*ed74d1c4SLudovic Barre 	switch (fdt32_to_cpu(properties[0])) {
221*ed74d1c4SLudovic Barre 	case 1:
222*ed74d1c4SLudovic Barre 		it_num += 16;
223*ed74d1c4SLudovic Barre 		break;
224*ed74d1c4SLudovic Barre 	case 0:
225*ed74d1c4SLudovic Barre 		it_num += 32;
226*ed74d1c4SLudovic Barre 		break;
227*ed74d1c4SLudovic Barre 	default:
228*ed74d1c4SLudovic Barre 		it_num = DT_INFO_INVALID_INTERRUPT;
229*ed74d1c4SLudovic Barre 	}
230*ed74d1c4SLudovic Barre 
231*ed74d1c4SLudovic Barre 	return it_num;
23267729d8dSLudovic Barre }
23367729d8dSLudovic Barre 
23418901324SDavid Wang void gic_init_base_addr(struct gic_data *gd, vaddr_t gicc_base __maybe_unused,
2357315b7b4SJens Wiklander 			vaddr_t gicd_base)
23653bd332aSSY Chiu {
2377315b7b4SJens Wiklander 	gd->gicc_base = gicc_base;
2387315b7b4SJens Wiklander 	gd->gicd_base = gicd_base;
2397315b7b4SJens Wiklander 	gd->max_it = probe_max_it(gicc_base, gicd_base);
2407315b7b4SJens Wiklander 	gd->chip.ops = &gic_ops;
24167729d8dSLudovic Barre 
24267729d8dSLudovic Barre 	if (IS_ENABLED(CFG_DT))
24367729d8dSLudovic Barre 		gd->chip.dt_get_irq = gic_dt_get_irq;
24453bd332aSSY Chiu }
24553bd332aSSY Chiu 
2467315b7b4SJens Wiklander static void gic_it_add(struct gic_data *gd, size_t it)
247b0104773SPascal Brand {
24853bd332aSSY Chiu 	size_t idx = it / NUM_INTS_PER_REG;
24953bd332aSSY Chiu 	uint32_t mask = 1 << (it % NUM_INTS_PER_REG);
250b0104773SPascal Brand 
251b0104773SPascal Brand 	/* Disable the interrupt */
252918bb3a5SEtienne Carriere 	io_write32(gd->gicd_base + GICD_ICENABLER(idx), mask);
253b0104773SPascal Brand 	/* Make it non-pending */
254918bb3a5SEtienne Carriere 	io_write32(gd->gicd_base + GICD_ICPENDR(idx), mask);
255b0104773SPascal Brand 	/* Assign it to group0 */
256918bb3a5SEtienne Carriere 	io_clrbits32(gd->gicd_base + GICD_IGROUPR(idx), mask);
2571fcac774SSandeep Tripathy #if defined(CFG_ARM_GICV3)
2581fcac774SSandeep Tripathy 	/* Assign it to group1S */
2591fcac774SSandeep Tripathy 	io_setbits32(gd->gicd_base + GICD_IGROUPMODR(idx), mask);
2601fcac774SSandeep Tripathy #endif
261b0104773SPascal Brand }
262b0104773SPascal Brand 
2637315b7b4SJens Wiklander static void gic_it_set_cpu_mask(struct gic_data *gd, size_t it,
2647315b7b4SJens Wiklander 				uint8_t cpu_mask)
265b0104773SPascal Brand {
2668ddf5a4eSEtienne Carriere 	size_t idx __maybe_unused = it / NUM_INTS_PER_REG;
2678ddf5a4eSEtienne Carriere 	uint32_t mask __maybe_unused = 1 << (it % NUM_INTS_PER_REG);
26853bd332aSSY Chiu 	uint32_t target, target_shift;
269918bb3a5SEtienne Carriere 	vaddr_t itargetsr = gd->gicd_base +
270918bb3a5SEtienne Carriere 			    GICD_ITARGETSR(it / NUM_TARGETS_PER_REG);
271b0104773SPascal Brand 
272b0104773SPascal Brand 	/* Assigned to group0 */
273918bb3a5SEtienne Carriere 	assert(!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask));
274b0104773SPascal Brand 
275b0104773SPascal Brand 	/* Route it to selected CPUs */
276918bb3a5SEtienne Carriere 	target = io_read32(itargetsr);
27753bd332aSSY Chiu 	target_shift = (it % NUM_TARGETS_PER_REG) * ITARGETSR_FIELD_BITS;
27853bd332aSSY Chiu 	target &= ~(ITARGETSR_FIELD_MASK << target_shift);
27953bd332aSSY Chiu 	target |= cpu_mask << target_shift;
280918bb3a5SEtienne Carriere 	DMSG("cpu_mask: writing 0x%x to 0x%" PRIxVA, target, itargetsr);
281918bb3a5SEtienne Carriere 	io_write32(itargetsr, target);
282918bb3a5SEtienne Carriere 	DMSG("cpu_mask: 0x%x", io_read32(itargetsr));
283b0104773SPascal Brand }
284b0104773SPascal Brand 
2857315b7b4SJens Wiklander static void gic_it_set_prio(struct gic_data *gd, size_t it, uint8_t prio)
286b0104773SPascal Brand {
2878ddf5a4eSEtienne Carriere 	size_t idx __maybe_unused = it / NUM_INTS_PER_REG;
2888ddf5a4eSEtienne Carriere 	uint32_t mask __maybe_unused = 1 << (it % NUM_INTS_PER_REG);
289b0104773SPascal Brand 
290b0104773SPascal Brand 	/* Assigned to group0 */
291918bb3a5SEtienne Carriere 	assert(!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask));
292b0104773SPascal Brand 
293b0104773SPascal Brand 	/* Set prio it to selected CPUs */
2941f60363aSJens Wiklander 	DMSG("prio: writing 0x%x to 0x%" PRIxVA,
2957315b7b4SJens Wiklander 		prio, gd->gicd_base + GICD_IPRIORITYR(0) + it);
296918bb3a5SEtienne Carriere 	io_write8(gd->gicd_base + GICD_IPRIORITYR(0) + it, prio);
297b0104773SPascal Brand }
298b0104773SPascal Brand 
2997315b7b4SJens Wiklander static void gic_it_enable(struct gic_data *gd, size_t it)
300b0104773SPascal Brand {
30153bd332aSSY Chiu 	size_t idx = it / NUM_INTS_PER_REG;
30253bd332aSSY Chiu 	uint32_t mask = 1 << (it % NUM_INTS_PER_REG);
303918bb3a5SEtienne Carriere 	vaddr_t base = gd->gicd_base;
304b0104773SPascal Brand 
305b0104773SPascal Brand 	/* Assigned to group0 */
306918bb3a5SEtienne Carriere 	assert(!(io_read32(base + GICD_IGROUPR(idx)) & mask));
30726ed70ecSGuanchao Liang 	if (it >= NUM_SGI) {
30826ed70ecSGuanchao Liang 		/*
30926ed70ecSGuanchao Liang 		 * Not enabled yet, except Software Generated Interrupt
31026ed70ecSGuanchao Liang 		 * which is implementation defined
31126ed70ecSGuanchao Liang 		 */
312918bb3a5SEtienne Carriere 		assert(!(io_read32(base + GICD_ISENABLER(idx)) & mask));
31326ed70ecSGuanchao Liang 	}
314b0104773SPascal Brand 
315b0104773SPascal Brand 	/* Enable the interrupt */
316918bb3a5SEtienne Carriere 	io_write32(base + GICD_ISENABLER(idx), mask);
317b0104773SPascal Brand }
318b0104773SPascal Brand 
3197315b7b4SJens Wiklander static void gic_it_disable(struct gic_data *gd, size_t it)
320b0104773SPascal Brand {
32153bd332aSSY Chiu 	size_t idx = it / NUM_INTS_PER_REG;
32253bd332aSSY Chiu 	uint32_t mask = 1 << (it % NUM_INTS_PER_REG);
323b0104773SPascal Brand 
324b0104773SPascal Brand 	/* Assigned to group0 */
325918bb3a5SEtienne Carriere 	assert(!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask));
326b0104773SPascal Brand 
327b0104773SPascal Brand 	/* Disable the interrupt */
328918bb3a5SEtienne Carriere 	io_write32(gd->gicd_base + GICD_ICENABLER(idx), mask);
329b0104773SPascal Brand }
330b0104773SPascal Brand 
33126ed70ecSGuanchao Liang static void gic_it_set_pending(struct gic_data *gd, size_t it)
33226ed70ecSGuanchao Liang {
33326ed70ecSGuanchao Liang 	size_t idx = it / NUM_INTS_PER_REG;
33426ed70ecSGuanchao Liang 	uint32_t mask = BIT32(it % NUM_INTS_PER_REG);
33526ed70ecSGuanchao Liang 
33626ed70ecSGuanchao Liang 	/* Should be Peripheral Interrupt */
33726ed70ecSGuanchao Liang 	assert(it >= NUM_SGI);
33826ed70ecSGuanchao Liang 
33926ed70ecSGuanchao Liang 	/* Raise the interrupt */
340918bb3a5SEtienne Carriere 	io_write32(gd->gicd_base + GICD_ISPENDR(idx), mask);
34126ed70ecSGuanchao Liang }
34226ed70ecSGuanchao Liang 
34326ed70ecSGuanchao Liang static void gic_it_raise_sgi(struct gic_data *gd, size_t it,
34426ed70ecSGuanchao Liang 		uint8_t cpu_mask, uint8_t group)
34526ed70ecSGuanchao Liang {
34626ed70ecSGuanchao Liang 	uint32_t mask_id = it & 0xf;
34726ed70ecSGuanchao Liang 	uint32_t mask_group = group & 0x1;
34826ed70ecSGuanchao Liang 	uint32_t mask_cpu = cpu_mask & 0xff;
34926ed70ecSGuanchao Liang 	uint32_t mask = (mask_id | SHIFT_U32(mask_group, 15) |
35026ed70ecSGuanchao Liang 		SHIFT_U32(mask_cpu, 16));
35126ed70ecSGuanchao Liang 
35226ed70ecSGuanchao Liang 	/* Should be Software Generated Interrupt */
35326ed70ecSGuanchao Liang 	assert(it < NUM_SGI);
35426ed70ecSGuanchao Liang 
35526ed70ecSGuanchao Liang 	/* Raise the interrupt */
356918bb3a5SEtienne Carriere 	io_write32(gd->gicd_base + GICD_SGIR, mask);
35726ed70ecSGuanchao Liang }
35826ed70ecSGuanchao Liang 
35918901324SDavid Wang static uint32_t gic_read_iar(struct gic_data *gd __maybe_unused)
360b0104773SPascal Brand {
36118901324SDavid Wang #if defined(CFG_ARM_GICV3)
3621de462e1SSumit Garg 	return read_icc_iar1();
36318901324SDavid Wang #else
364918bb3a5SEtienne Carriere 	return io_read32(gd->gicc_base + GICC_IAR);
36518901324SDavid Wang #endif
366b0104773SPascal Brand }
367b0104773SPascal Brand 
36818901324SDavid Wang static void gic_write_eoir(struct gic_data *gd __maybe_unused, uint32_t eoir)
369b0104773SPascal Brand {
37018901324SDavid Wang #if defined(CFG_ARM_GICV3)
3711de462e1SSumit Garg 	write_icc_eoir1(eoir);
37218901324SDavid Wang #else
373918bb3a5SEtienne Carriere 	io_write32(gd->gicc_base + GICC_EOIR, eoir);
37418901324SDavid Wang #endif
375b0104773SPascal Brand }
376b0104773SPascal Brand 
3777315b7b4SJens Wiklander static bool gic_it_is_enabled(struct gic_data *gd, size_t it)
3787315b7b4SJens Wiklander {
37953bd332aSSY Chiu 	size_t idx = it / NUM_INTS_PER_REG;
38053bd332aSSY Chiu 	uint32_t mask = 1 << (it % NUM_INTS_PER_REG);
381918bb3a5SEtienne Carriere 	return !!(io_read32(gd->gicd_base + GICD_ISENABLER(idx)) & mask);
38253bd332aSSY Chiu }
38353bd332aSSY Chiu 
3847315b7b4SJens Wiklander static bool __maybe_unused gic_it_get_group(struct gic_data *gd, size_t it)
3857315b7b4SJens Wiklander {
38653bd332aSSY Chiu 	size_t idx = it / NUM_INTS_PER_REG;
38753bd332aSSY Chiu 	uint32_t mask = 1 << (it % NUM_INTS_PER_REG);
388918bb3a5SEtienne Carriere 	return !!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask);
38953bd332aSSY Chiu }
39053bd332aSSY Chiu 
3917315b7b4SJens Wiklander static uint32_t __maybe_unused gic_it_get_target(struct gic_data *gd, size_t it)
3927315b7b4SJens Wiklander {
39353bd332aSSY Chiu 	size_t reg_idx = it / NUM_TARGETS_PER_REG;
3947315b7b4SJens Wiklander 	uint32_t target_shift = (it % NUM_TARGETS_PER_REG) *
3957315b7b4SJens Wiklander 				ITARGETSR_FIELD_BITS;
39653bd332aSSY Chiu 	uint32_t target_mask = ITARGETSR_FIELD_MASK << target_shift;
397918bb3a5SEtienne Carriere 	uint32_t target = io_read32(gd->gicd_base + GICD_ITARGETSR(reg_idx));
3987315b7b4SJens Wiklander 
399918bb3a5SEtienne Carriere 	return (target & target_mask) >> target_shift;
40053bd332aSSY Chiu }
40153bd332aSSY Chiu 
4027315b7b4SJens Wiklander void gic_dump_state(struct gic_data *gd)
40353bd332aSSY Chiu {
40453bd332aSSY Chiu 	int i;
40553bd332aSSY Chiu 
40618901324SDavid Wang #if defined(CFG_ARM_GICV3)
40718901324SDavid Wang 	DMSG("GICC_CTLR: 0x%x", read_icc_ctlr());
40818901324SDavid Wang #else
409918bb3a5SEtienne Carriere 	DMSG("GICC_CTLR: 0x%x", io_read32(gd->gicc_base + GICC_CTLR));
41018901324SDavid Wang #endif
411918bb3a5SEtienne Carriere 	DMSG("GICD_CTLR: 0x%x", io_read32(gd->gicd_base + GICD_CTLR));
4127315b7b4SJens Wiklander 
4134a9ea08cSFangsuo Wu 	for (i = 0; i <= (int)gd->max_it; i++) {
4147315b7b4SJens Wiklander 		if (gic_it_is_enabled(gd, i)) {
41553bd332aSSY Chiu 			DMSG("irq%d: enabled, group:%d, target:%x", i,
4167315b7b4SJens Wiklander 			     gic_it_get_group(gd, i), gic_it_get_target(gd, i));
41753bd332aSSY Chiu 		}
41853bd332aSSY Chiu 	}
41953bd332aSSY Chiu }
4207315b7b4SJens Wiklander 
4217315b7b4SJens Wiklander void gic_it_handle(struct gic_data *gd)
4227315b7b4SJens Wiklander {
4237315b7b4SJens Wiklander 	uint32_t iar;
4247315b7b4SJens Wiklander 	uint32_t id;
4257315b7b4SJens Wiklander 
4267315b7b4SJens Wiklander 	iar = gic_read_iar(gd);
4277315b7b4SJens Wiklander 	id = iar & GICC_IAR_IT_ID_MASK;
4287315b7b4SJens Wiklander 
4294a9ea08cSFangsuo Wu 	if (id <= gd->max_it)
4307315b7b4SJens Wiklander 		itr_handle(id);
4313b3a4611SMathieu Briand 	else
4323b3a4611SMathieu Briand 		DMSG("ignoring interrupt %" PRIu32, id);
4337315b7b4SJens Wiklander 
4347315b7b4SJens Wiklander 	gic_write_eoir(gd, iar);
4357315b7b4SJens Wiklander }
4367315b7b4SJens Wiklander 
4377315b7b4SJens Wiklander static void gic_op_add(struct itr_chip *chip, size_t it,
4387315b7b4SJens Wiklander 		       uint32_t flags __unused)
4397315b7b4SJens Wiklander {
4407315b7b4SJens Wiklander 	struct gic_data *gd = container_of(chip, struct gic_data, chip);
4417315b7b4SJens Wiklander 
4424a9ea08cSFangsuo Wu 	if (it > gd->max_it)
443d13278b8SEtienne Carriere 		panic();
444d13278b8SEtienne Carriere 
4457315b7b4SJens Wiklander 	gic_it_add(gd, it);
4467315b7b4SJens Wiklander 	/* Set the CPU mask to deliver interrupts to any online core */
4477315b7b4SJens Wiklander 	gic_it_set_cpu_mask(gd, it, 0xff);
4487315b7b4SJens Wiklander 	gic_it_set_prio(gd, it, 0x1);
4497315b7b4SJens Wiklander }
4507315b7b4SJens Wiklander 
4517315b7b4SJens Wiklander static void gic_op_enable(struct itr_chip *chip, size_t it)
4527315b7b4SJens Wiklander {
4537315b7b4SJens Wiklander 	struct gic_data *gd = container_of(chip, struct gic_data, chip);
4547315b7b4SJens Wiklander 
4554a9ea08cSFangsuo Wu 	if (it > gd->max_it)
456d13278b8SEtienne Carriere 		panic();
457d13278b8SEtienne Carriere 
4587315b7b4SJens Wiklander 	gic_it_enable(gd, it);
4597315b7b4SJens Wiklander }
4607315b7b4SJens Wiklander 
4617315b7b4SJens Wiklander static void gic_op_disable(struct itr_chip *chip, size_t it)
4627315b7b4SJens Wiklander {
4637315b7b4SJens Wiklander 	struct gic_data *gd = container_of(chip, struct gic_data, chip);
4647315b7b4SJens Wiklander 
4654a9ea08cSFangsuo Wu 	if (it > gd->max_it)
466d13278b8SEtienne Carriere 		panic();
467d13278b8SEtienne Carriere 
4687315b7b4SJens Wiklander 	gic_it_disable(gd, it);
4697315b7b4SJens Wiklander }
47026ed70ecSGuanchao Liang 
47126ed70ecSGuanchao Liang static void gic_op_raise_pi(struct itr_chip *chip, size_t it)
47226ed70ecSGuanchao Liang {
47326ed70ecSGuanchao Liang 	struct gic_data *gd = container_of(chip, struct gic_data, chip);
47426ed70ecSGuanchao Liang 
4754a9ea08cSFangsuo Wu 	if (it > gd->max_it)
47626ed70ecSGuanchao Liang 		panic();
47726ed70ecSGuanchao Liang 
47826ed70ecSGuanchao Liang 	gic_it_set_pending(gd, it);
47926ed70ecSGuanchao Liang }
48026ed70ecSGuanchao Liang 
48126ed70ecSGuanchao Liang static void gic_op_raise_sgi(struct itr_chip *chip, size_t it,
48226ed70ecSGuanchao Liang 			uint8_t cpu_mask)
48326ed70ecSGuanchao Liang {
48426ed70ecSGuanchao Liang 	struct gic_data *gd = container_of(chip, struct gic_data, chip);
48526ed70ecSGuanchao Liang 
4864a9ea08cSFangsuo Wu 	if (it > gd->max_it)
48726ed70ecSGuanchao Liang 		panic();
48826ed70ecSGuanchao Liang 
48926ed70ecSGuanchao Liang 	if (it < NUM_NS_SGI)
49026ed70ecSGuanchao Liang 		gic_it_raise_sgi(gd, it, cpu_mask, 1);
49126ed70ecSGuanchao Liang 	else
49226ed70ecSGuanchao Liang 		gic_it_raise_sgi(gd, it, cpu_mask, 0);
49326ed70ecSGuanchao Liang }
49426ed70ecSGuanchao Liang static void gic_op_set_affinity(struct itr_chip *chip, size_t it,
49526ed70ecSGuanchao Liang 			uint8_t cpu_mask)
49626ed70ecSGuanchao Liang {
49726ed70ecSGuanchao Liang 	struct gic_data *gd = container_of(chip, struct gic_data, chip);
49826ed70ecSGuanchao Liang 
4994a9ea08cSFangsuo Wu 	if (it > gd->max_it)
50026ed70ecSGuanchao Liang 		panic();
50126ed70ecSGuanchao Liang 
50226ed70ecSGuanchao Liang 	gic_it_set_cpu_mask(gd, it, cpu_mask);
50326ed70ecSGuanchao Liang }
504