xref: /optee_os/core/drivers/gic.c (revision b01047730e77127c23a36591643eeb8bb0487d68)
1*b0104773SPascal Brand /*
2*b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
3*b0104773SPascal Brand  * All rights reserved.
4*b0104773SPascal Brand  *
5*b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
6*b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
7*b0104773SPascal Brand  *
8*b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
9*b0104773SPascal Brand  * this list of conditions and the following disclaimer.
10*b0104773SPascal Brand  *
11*b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
12*b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
13*b0104773SPascal Brand  * and/or other materials provided with the distribution.
14*b0104773SPascal Brand  *
15*b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16*b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19*b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
26*b0104773SPascal Brand  */
27*b0104773SPascal Brand 
28*b0104773SPascal Brand #include <drivers/gic.h>
29*b0104773SPascal Brand #include <io.h>
30*b0104773SPascal Brand #include <kernel/tee_core_trace.h>
31*b0104773SPascal Brand 
32*b0104773SPascal Brand #include <assert.h>
33*b0104773SPascal Brand 
34*b0104773SPascal Brand /* Offsets from gic.gicc_base */
35*b0104773SPascal Brand #define GICC_CTLR		(0x000)
36*b0104773SPascal Brand #define GICC_IAR		(0x00C)
37*b0104773SPascal Brand #define GICC_EOIR		(0x010)
38*b0104773SPascal Brand 
39*b0104773SPascal Brand #define GICC_CTLR_ENABLEGRP0	(1 << 0)
40*b0104773SPascal Brand #define GICC_CTLR_ENABLEGRP1	(1 << 1)
41*b0104773SPascal Brand #define GICC_CTLR_FIQEN		(1 << 3)
42*b0104773SPascal Brand 
43*b0104773SPascal Brand /* Offsets from gic.gicd_base */
44*b0104773SPascal Brand #define GICD_CTLR		(0x000)
45*b0104773SPascal Brand #define GICD_TYPER		(0x004)
46*b0104773SPascal Brand #define GICD_IGROUPR(n)		(0x080 + (n) * 4)
47*b0104773SPascal Brand #define GICD_ISENABLER(n)	(0x100 + (n) * 4)
48*b0104773SPascal Brand #define GICD_ICENABLER(n)	(0x180 + (n) * 4)
49*b0104773SPascal Brand #define GICD_ICPENDR(n)		(0x280 + (n) * 4)
50*b0104773SPascal Brand #define GICD_IPRIORITYR(n)	(0x400 + (n) * 4)
51*b0104773SPascal Brand #define GICD_ITARGETSR(n)	(0x800 + (n) * 4)
52*b0104773SPascal Brand 
53*b0104773SPascal Brand #define GICD_CTLR_ENABLEGRP0	(1 << 0)
54*b0104773SPascal Brand #define GICD_CTLR_ENABLEGRP1	(1 << 1)
55*b0104773SPascal Brand 
56*b0104773SPascal Brand /* Maximum number of interrups a GIC can support */
57*b0104773SPascal Brand #define GIC_MAX_INTS		1020
58*b0104773SPascal Brand 
59*b0104773SPascal Brand 
60*b0104773SPascal Brand static struct {
61*b0104773SPascal Brand 	vaddr_t gicc_base;
62*b0104773SPascal Brand 	vaddr_t gicd_base;
63*b0104773SPascal Brand 	size_t max_it;
64*b0104773SPascal Brand } gic;
65*b0104773SPascal Brand 
66*b0104773SPascal Brand static size_t probe_max_it(void)
67*b0104773SPascal Brand {
68*b0104773SPascal Brand 	int i;
69*b0104773SPascal Brand 	uint32_t old_ctlr;
70*b0104773SPascal Brand 	size_t ret = 0;
71*b0104773SPascal Brand 
72*b0104773SPascal Brand 	/*
73*b0104773SPascal Brand 	 * Probe which interrupt number is the largest.
74*b0104773SPascal Brand 	 */
75*b0104773SPascal Brand 	old_ctlr = read32(gic.gicc_base + GICC_CTLR);
76*b0104773SPascal Brand 	write32(0, gic.gicc_base + GICC_CTLR);
77*b0104773SPascal Brand 	for (i = GIC_MAX_INTS / 32; i > 0; i--) {
78*b0104773SPascal Brand 		uint32_t old_reg;
79*b0104773SPascal Brand 		uint32_t reg;
80*b0104773SPascal Brand 		int b;
81*b0104773SPascal Brand 
82*b0104773SPascal Brand 		old_reg = read32(gic.gicd_base + GICD_ISENABLER(i));
83*b0104773SPascal Brand 		write32(0xffffffff, gic.gicd_base + GICD_ISENABLER(i));
84*b0104773SPascal Brand 		reg = read32(gic.gicd_base + GICD_ISENABLER(i));
85*b0104773SPascal Brand 		write32(old_reg, gic.gicd_base + GICD_ICENABLER(i));
86*b0104773SPascal Brand 		for (b = 31; b > 0; b--) {
87*b0104773SPascal Brand 			if ((1 << b) & reg) {
88*b0104773SPascal Brand 				ret = i * 32 + b;
89*b0104773SPascal Brand 				goto out;
90*b0104773SPascal Brand 			}
91*b0104773SPascal Brand 		}
92*b0104773SPascal Brand 	}
93*b0104773SPascal Brand out:
94*b0104773SPascal Brand 	write32(old_ctlr, gic.gicc_base + GICC_CTLR);
95*b0104773SPascal Brand 	return ret;
96*b0104773SPascal Brand }
97*b0104773SPascal Brand 
98*b0104773SPascal Brand void gic_init(vaddr_t gicc_base, vaddr_t gicd_base)
99*b0104773SPascal Brand {
100*b0104773SPascal Brand 	size_t n;
101*b0104773SPascal Brand 
102*b0104773SPascal Brand 	gic.gicc_base = gicc_base;
103*b0104773SPascal Brand 	gic.gicd_base = gicd_base;
104*b0104773SPascal Brand 	gic.max_it = probe_max_it();
105*b0104773SPascal Brand 
106*b0104773SPascal Brand 	for (n = 0; n <= gic.max_it / 32; n++) {
107*b0104773SPascal Brand 		/* Disable interrupts */
108*b0104773SPascal Brand 		write32(0xffffffff, gic.gicd_base + GICD_ICENABLER(n));
109*b0104773SPascal Brand 
110*b0104773SPascal Brand 		/* Make interrupts non-pending */
111*b0104773SPascal Brand 		write32(0xffffffff, gic.gicd_base + GICD_ICPENDR(n));
112*b0104773SPascal Brand 
113*b0104773SPascal Brand 		/* Mark interrupts non-secure */
114*b0104773SPascal Brand 		write32(0xffffffff, gic.gicd_base + GICD_IGROUPR(n));
115*b0104773SPascal Brand 	}
116*b0104773SPascal Brand 
117*b0104773SPascal Brand 	/* Enable GIC */
118*b0104773SPascal Brand 	write32(GICC_CTLR_ENABLEGRP0 | GICC_CTLR_ENABLEGRP1 | GICC_CTLR_FIQEN,
119*b0104773SPascal Brand 		gic.gicc_base + GICC_CTLR);
120*b0104773SPascal Brand 	write32(GICD_CTLR_ENABLEGRP0 | GICD_CTLR_ENABLEGRP1,
121*b0104773SPascal Brand 		gic.gicd_base + GICD_CTLR);
122*b0104773SPascal Brand }
123*b0104773SPascal Brand 
124*b0104773SPascal Brand void gic_it_add(size_t it)
125*b0104773SPascal Brand {
126*b0104773SPascal Brand 	size_t idx = it / 32;
127*b0104773SPascal Brand 	uint32_t mask = 1 << (it % 32);
128*b0104773SPascal Brand 
129*b0104773SPascal Brand 	assert(it <= gic.max_it); /* Not too large */
130*b0104773SPascal Brand 
131*b0104773SPascal Brand 	/* Disable the interrupt */
132*b0104773SPascal Brand 	write32(mask, gic.gicd_base + GICD_ICENABLER(idx));
133*b0104773SPascal Brand 	/* Make it non-pending */
134*b0104773SPascal Brand 	write32(mask, gic.gicd_base + GICD_ICPENDR(idx));
135*b0104773SPascal Brand 	/* Assign it to group0 */
136*b0104773SPascal Brand 	write32(read32(gic.gicd_base + GICD_IGROUPR(idx)) & ~mask,
137*b0104773SPascal Brand 			gic.gicd_base + GICD_IGROUPR(idx));
138*b0104773SPascal Brand }
139*b0104773SPascal Brand 
140*b0104773SPascal Brand void gic_it_set_cpu_mask(size_t it, uint8_t cpu_mask)
141*b0104773SPascal Brand {
142*b0104773SPascal Brand 	size_t idx = it / 32;
143*b0104773SPascal Brand 	uint32_t mask = 1 << (it % 32);
144*b0104773SPascal Brand 	uint32_t target;
145*b0104773SPascal Brand 
146*b0104773SPascal Brand 	assert(it <= gic.max_it); /* Not too large */
147*b0104773SPascal Brand 	/* Assigned to group0 */
148*b0104773SPascal Brand 	assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask));
149*b0104773SPascal Brand 
150*b0104773SPascal Brand 	/* Route it to selected CPUs */
151*b0104773SPascal Brand 	target = read32(gic.gicd_base + GICD_ITARGETSR(it / 4));
152*b0104773SPascal Brand 	target &= ~(0xff << ((it % 4) * 8));
153*b0104773SPascal Brand 	target |= cpu_mask << ((it % 4) * 8);
154*b0104773SPascal Brand 	DMSG("cpu_mask: writing 0x%x to 0x%x\n",
155*b0104773SPascal Brand 		target, gic.gicd_base + GICD_ITARGETSR(it / 4));
156*b0104773SPascal Brand 	write32(target, gic.gicd_base + GICD_ITARGETSR(it / 4));
157*b0104773SPascal Brand 	DMSG("cpu_mask: 0x%x\n",
158*b0104773SPascal Brand 		read32(gic.gicd_base + GICD_ITARGETSR(it / 4)));
159*b0104773SPascal Brand }
160*b0104773SPascal Brand 
161*b0104773SPascal Brand void gic_it_set_prio(size_t it, uint8_t prio)
162*b0104773SPascal Brand {
163*b0104773SPascal Brand 	size_t idx = it / 32;
164*b0104773SPascal Brand 	uint32_t mask = 1 << (it % 32);
165*b0104773SPascal Brand 
166*b0104773SPascal Brand 	assert(it <= gic.max_it); /* Not too large */
167*b0104773SPascal Brand 	/* Assigned to group0 */
168*b0104773SPascal Brand 	assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask));
169*b0104773SPascal Brand 
170*b0104773SPascal Brand 	/* Set prio it to selected CPUs */
171*b0104773SPascal Brand 	DMSG("prio: writing 0x%x to 0x%x\n",
172*b0104773SPascal Brand 		prio, gic.gicd_base + GICD_IPRIORITYR(0) + it);
173*b0104773SPascal Brand 	write8(prio, gic.gicd_base + GICD_IPRIORITYR(0) + it);
174*b0104773SPascal Brand }
175*b0104773SPascal Brand 
176*b0104773SPascal Brand void gic_it_enable(size_t it)
177*b0104773SPascal Brand {
178*b0104773SPascal Brand 	size_t idx = it / 32;
179*b0104773SPascal Brand 	uint32_t mask = 1 << (it % 32);
180*b0104773SPascal Brand 
181*b0104773SPascal Brand 	assert(it <= gic.max_it); /* Not too large */
182*b0104773SPascal Brand 	/* Assigned to group0 */
183*b0104773SPascal Brand 	assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask));
184*b0104773SPascal Brand 	/* Not enabled yet */
185*b0104773SPascal Brand 	assert(!(read32(gic.gicd_base + GICD_ISENABLER(idx)) & mask));
186*b0104773SPascal Brand 
187*b0104773SPascal Brand 	/* Enable the interrupt */
188*b0104773SPascal Brand 	write32(mask, gic.gicd_base + GICD_ISENABLER(idx));
189*b0104773SPascal Brand }
190*b0104773SPascal Brand 
191*b0104773SPascal Brand void gic_it_disable(size_t it)
192*b0104773SPascal Brand {
193*b0104773SPascal Brand 	size_t idx = it / 32;
194*b0104773SPascal Brand 	uint32_t mask = 1 << (it % 32);
195*b0104773SPascal Brand 
196*b0104773SPascal Brand 	assert(it <= gic.max_it); /* Not too large */
197*b0104773SPascal Brand 	/* Assigned to group0 */
198*b0104773SPascal Brand 	assert(!(read32(gic.gicd_base + GICD_IGROUPR(idx)) & mask));
199*b0104773SPascal Brand 
200*b0104773SPascal Brand 	/* Disable the interrupt */
201*b0104773SPascal Brand 	write32(mask, gic.gicd_base + GICD_ICENABLER(idx));
202*b0104773SPascal Brand }
203*b0104773SPascal Brand 
204*b0104773SPascal Brand uint32_t gic_read_iar(void)
205*b0104773SPascal Brand {
206*b0104773SPascal Brand 	return read32(gic.gicc_base + GICC_IAR);
207*b0104773SPascal Brand }
208*b0104773SPascal Brand 
209*b0104773SPascal Brand void gic_write_eoir(uint32_t eoir)
210*b0104773SPascal Brand {
211*b0104773SPascal Brand 	write32(eoir, gic.gicc_base + GICC_EOIR);
212*b0104773SPascal Brand }
213*b0104773SPascal Brand 
214