1 // Copyright 2016 The Fuchsia Authors
2 // Copyright (c) 2012-2015 Travis Geiselbrecht
3 //
4 // Use of this source code is governed by a MIT-style
5 // license that can be found in the LICENSE file or at
6 // https://opensource.org/licenses/MIT
7 
8 #include <arch/arm64/hypervisor/gic/gicv2.h>
9 #include <arch/arm64/periphmap.h>
10 #include <arch/ops.h>
11 #include <assert.h>
12 #include <bits.h>
13 #include <debug.h>
14 #include <dev/interrupt.h>
15 #include <dev/interrupt/arm_gic_common.h>
16 #include <dev/interrupt/arm_gicv2_regs.h>
17 #include <dev/interrupt/arm_gicv2m.h>
18 #include <dev/interrupt/arm_gicv2m_msi.h>
19 #include <err.h>
20 #include <inttypes.h>
21 #include <kernel/stats.h>
22 #include <kernel/thread.h>
23 #include <lib/ktrace.h>
24 #include <lk/init.h>
25 #include <pdev/driver.h>
26 #include <pdev/interrupt.h>
27 #include <reg.h>
28 #include <sys/types.h>
29 #include <trace.h>
30 #include <zircon/boot/driver-config.h>
31 #include <zircon/types.h>
32 
33 #define LOCAL_TRACE 0
34 
35 #include <arch/arm64.h>
36 #define iframe arm64_iframe_short
37 #define IFRAME_PC(frame) ((frame)->elr)
38 
39 static spin_lock_t gicd_lock;
40 #define GICD_LOCK_FLAGS SPIN_LOCK_FLAG_INTERRUPTS
41 
42 // values read from zbi
43 vaddr_t arm_gicv2_gic_base = 0;
44 uint64_t arm_gicv2_gicd_offset = 0;
45 uint64_t arm_gicv2_gicc_offset = 0;
46 uint64_t arm_gicv2_gich_offset = 0;
47 uint64_t arm_gicv2_gicv_offset = 0;
48 static uint32_t ipi_base = 0;
49 
50 static uint max_irqs = 0;
51 
52 static zx_status_t arm_gic_init();
53 
54 static zx_status_t gic_configure_interrupt(unsigned int vector,
55                                            enum interrupt_trigger_mode tm,
56                                            enum interrupt_polarity pol);
57 
suspend_resume_fiq(bool resume_gicc,bool resume_gicd)58 static void suspend_resume_fiq(bool resume_gicc, bool resume_gicd) {
59 }
60 
gic_is_valid_interrupt(uint vector,uint32_t flags)61 static bool gic_is_valid_interrupt(uint vector, uint32_t flags) {
62     return (vector < max_irqs);
63 }
64 
gic_get_base_vector()65 static uint32_t gic_get_base_vector() {
66     // ARM Generic Interrupt Controller v2 chapter 2.1
67     // INTIDs 0-15 are local CPU interrupts
68     return 16;
69 }
70 
gic_get_max_vector()71 static uint32_t gic_get_max_vector() {
72     return max_irqs;
73 }
74 
gic_set_enable(uint vector,bool enable)75 static void gic_set_enable(uint vector, bool enable) {
76     int reg = vector / 32;
77     uint32_t mask = (uint32_t)(1ULL << (vector % 32));
78 
79     if (enable) {
80         GICREG(0, GICD_ISENABLER(reg)) = mask;
81     } else {
82         GICREG(0, GICD_ICENABLER(reg)) = mask;
83     }
84 }
85 
gic_init_percpu_early()86 static void gic_init_percpu_early() {
87     GICREG(0, GICC_CTLR) = 0x201;   // EnableGrp1 and EOImodeNS
88     GICREG(0, GICC_PMR) = 0xff;     // unmask interrupts at all priority levels
89 }
90 
arm_gic_suspend_cpu(uint level)91 static void arm_gic_suspend_cpu(uint level) {
92     suspend_resume_fiq(false, false);
93 }
94 
arm_gic_resume_cpu(uint level)95 static void arm_gic_resume_cpu(uint level) {
96     spin_lock_saved_state_t state;
97     bool resume_gicd = false;
98 
99     spin_lock_save(&gicd_lock, &state, GICD_LOCK_FLAGS);
100     if (!(GICREG(0, GICD_CTLR) & 1)) {
101         dprintf(SPEW, "%s: distributor is off, calling arm_gic_init instead\n", __func__);
102         arm_gic_init();
103         resume_gicd = true;
104     } else {
105         gic_init_percpu_early();
106     }
107     spin_unlock_restore(&gicd_lock, state, GICD_LOCK_FLAGS);
108     suspend_resume_fiq(true, resume_gicd);
109 }
110 
111 // disable for now. we will need to add suspend/resume support to dev/pdev for this to work
112 #if 0
113 LK_INIT_HOOK_FLAGS(arm_gic_suspend_cpu, arm_gic_suspend_cpu,
114                    LK_INIT_LEVEL_PLATFORM, LK_INIT_FLAG_CPU_SUSPEND);
115 
116 LK_INIT_HOOK_FLAGS(arm_gic_resume_cpu, arm_gic_resume_cpu,
117                    LK_INIT_LEVEL_PLATFORM, LK_INIT_FLAG_CPU_RESUME);
118 #endif
119 
arm_gic_max_cpu()120 static int arm_gic_max_cpu() {
121     return (GICREG(0, GICD_TYPER) >> 5) & 0x7;
122 }
123 
arm_gic_init()124 static zx_status_t arm_gic_init() {
125     uint i;
126     // see if we're gic v2
127     uint rev = 0;
128     uint32_t pidr2 = GICREG(0, GICD_PIDR2);
129     if (pidr2 != 0) {
130         uint rev = BITS_SHIFT(pidr2, 7, 4);
131         if (rev != GICV2) {
132             return ZX_ERR_NOT_FOUND;
133         }
134     } else {
135         // some v2's return a null PIDR2
136         pidr2 = GICREG(0, GICD_V3_PIDR2);
137         rev = BITS_SHIFT(pidr2, 7, 4);
138         if (rev >= GICV3) {
139             // looks like a gic v3
140             return ZX_ERR_NOT_FOUND;
141         }
142         // HACK: if gicv2 and v3 pidr2 seems to be blank, assume we're v2 and continue
143     }
144 
145     uint32_t typer = GICREG(0, GICD_TYPER);
146     uint32_t it_lines_number = BITS_SHIFT(typer, 4, 0);
147     max_irqs = (it_lines_number + 1) * 32;
148     LTRACEF("arm_gic_init max_irqs: %u\n", max_irqs);
149     assert(max_irqs <= MAX_INT);
150 
151     for (i = 0; i < max_irqs; i += 32) {
152         GICREG(0, GICD_ICENABLER(i / 32)) = ~0;
153         GICREG(0, GICD_ICPENDR(i / 32)) = ~0;
154     }
155 
156     if (arm_gic_max_cpu() > 0) {
157         // Set external interrupts to target cpu 0
158         for (i = 32; i < max_irqs; i += 4) {
159             GICREG(0, GICD_ITARGETSR(i / 4)) = 0x01010101;
160         }
161     }
162     // Initialize all the SPIs to edge triggered
163     for (i = GIC_BASE_SPI; i < max_irqs; i++) {
164         gic_configure_interrupt(i, IRQ_TRIGGER_MODE_EDGE, IRQ_POLARITY_ACTIVE_HIGH);
165     }
166 
167     GICREG(0, GICD_CTLR) = 1; // enable GIC0
168 
169     gic_init_percpu_early();
170 
171     return ZX_OK;
172 }
173 
arm_gic_sgi(u_int irq,u_int flags,u_int cpu_mask)174 static zx_status_t arm_gic_sgi(u_int irq, u_int flags, u_int cpu_mask) {
175     u_int val =
176         ((flags & ARM_GIC_SGI_FLAG_TARGET_FILTER_MASK) << 24) |
177         ((cpu_mask & 0xff) << 16) |
178         ((flags & ARM_GIC_SGI_FLAG_NS) ? (1U << 15) : 0) |
179         (irq & 0xf);
180 
181     if (irq >= 16) {
182         return ZX_ERR_INVALID_ARGS;
183     }
184 
185     LTRACEF("GICD_SGIR: %x\n", val);
186 
187     GICREG(0, GICD_SGIR) = val;
188 
189     return ZX_OK;
190 }
191 
gic_mask_interrupt(unsigned int vector)192 static zx_status_t gic_mask_interrupt(unsigned int vector) {
193     if (vector >= max_irqs) {
194         return ZX_ERR_INVALID_ARGS;
195     }
196 
197     gic_set_enable(vector, false);
198 
199     return ZX_OK;
200 }
201 
gic_unmask_interrupt(unsigned int vector)202 static zx_status_t gic_unmask_interrupt(unsigned int vector) {
203     if (vector >= max_irqs) {
204         return ZX_ERR_INVALID_ARGS;
205     }
206 
207     gic_set_enable(vector, true);
208 
209     return ZX_OK;
210 }
211 
gic_configure_interrupt(unsigned int vector,enum interrupt_trigger_mode tm,enum interrupt_polarity pol)212 static zx_status_t gic_configure_interrupt(unsigned int vector,
213                                            enum interrupt_trigger_mode tm,
214                                            enum interrupt_polarity pol) {
215     // Only configurable for SPI interrupts
216     if ((vector >= max_irqs) || (vector < GIC_BASE_SPI)) {
217         return ZX_ERR_INVALID_ARGS;
218     }
219 
220     if (pol != IRQ_POLARITY_ACTIVE_HIGH) {
221         // TODO: polarity should actually be configure through a GPIO controller
222         return ZX_ERR_NOT_SUPPORTED;
223     }
224 
225     // type is encoded with two bits, MSB of the two determine type
226     // 16 irqs encoded per ICFGR register
227     uint32_t reg_ndx = vector >> 4;
228     uint32_t bit_shift = ((vector & 0xf) << 1) + 1;
229     uint32_t reg_val = GICREG(0, GICD_ICFGR(reg_ndx));
230     if (tm == IRQ_TRIGGER_MODE_EDGE) {
231         reg_val |= (1 << bit_shift);
232     } else {
233         reg_val &= ~(1 << bit_shift);
234     }
235     GICREG(0, GICD_ICFGR(reg_ndx)) = reg_val;
236 
237     return ZX_OK;
238 }
239 
gic_get_interrupt_config(unsigned int vector,enum interrupt_trigger_mode * tm,enum interrupt_polarity * pol)240 static zx_status_t gic_get_interrupt_config(unsigned int vector,
241                                             enum interrupt_trigger_mode* tm,
242                                             enum interrupt_polarity* pol) {
243     if (vector >= max_irqs) {
244         return ZX_ERR_INVALID_ARGS;
245     }
246 
247     if (tm) {
248         *tm = IRQ_TRIGGER_MODE_EDGE;
249     }
250     if (pol) {
251         *pol = IRQ_POLARITY_ACTIVE_HIGH;
252     }
253 
254     return ZX_OK;
255 }
256 
gic_remap_interrupt(unsigned int vector)257 static unsigned int gic_remap_interrupt(unsigned int vector) {
258     return vector;
259 }
260 
gic_handle_irq(struct iframe * frame)261 static void gic_handle_irq(struct iframe* frame) {
262     // get the current vector
263     uint32_t iar = GICREG(0, GICC_IAR);
264     unsigned int vector = iar & 0x3ff;
265 
266     if (vector >= 0x3fe) {
267         // spurious
268         return;
269     }
270 
271     // tracking external hardware irqs in this variable
272     if (vector >= 32)
273         CPU_STATS_INC(interrupts);
274 
275     uint cpu = arch_curr_cpu_num();
276 
277     ktrace_tiny(TAG_IRQ_ENTER, (vector << 8) | cpu);
278 
279     LTRACEF_LEVEL(2, "iar 0x%x cpu %u currthread %p vector %u pc %#" PRIxPTR "\n", iar, cpu,
280                   get_current_thread(), vector, (uintptr_t)IFRAME_PC(frame));
281 
282     // deliver the interrupt
283     struct int_handler_struct* handler = pdev_get_int_handler(vector);
284     interrupt_eoi eoi = IRQ_EOI_DEACTIVATE;
285     if (handler->handler) {
286         eoi = handler->handler(handler->arg);
287     }
288     GICREG(0, GICC_EOIR) = iar;
289     if (eoi == IRQ_EOI_DEACTIVATE) {
290         GICREG(0, GICC_DIR) = iar;
291     }
292 
293     LTRACEF_LEVEL(2, "cpu %u exit\n", cpu);
294 
295     ktrace_tiny(TAG_IRQ_EXIT, (vector << 8) | cpu);
296 }
297 
gic_handle_fiq(struct iframe * frame)298 static void gic_handle_fiq(struct iframe* frame) {
299     PANIC_UNIMPLEMENTED;
300 }
301 
gic_send_ipi(cpu_mask_t target,mp_ipi_t ipi)302 static zx_status_t gic_send_ipi(cpu_mask_t target, mp_ipi_t ipi) {
303     uint gic_ipi_num = ipi + ipi_base;
304 
305     // filter out targets outside of the range of cpus we care about
306     target &= ((1UL << SMP_MAX_CPUS) - 1);
307     if (target != 0) {
308         LTRACEF("target 0x%x, gic_ipi %u\n", target, gic_ipi_num);
309         arm_gic_sgi(gic_ipi_num, ARM_GIC_SGI_FLAG_NS, target);
310     }
311 
312     return ZX_OK;
313 }
314 
arm_ipi_halt_handler(void *)315 static interrupt_eoi arm_ipi_halt_handler(void*) {
316     LTRACEF("cpu %u\n", arch_curr_cpu_num());
317 
318     arch_disable_ints();
319     while (true) {
320     }
321 
322     return IRQ_EOI_DEACTIVATE;
323 }
324 
gic_init_percpu()325 static void gic_init_percpu() {
326     mp_set_curr_cpu_online(true);
327     unmask_interrupt(MP_IPI_GENERIC + ipi_base);
328     unmask_interrupt(MP_IPI_RESCHEDULE + ipi_base);
329     unmask_interrupt(MP_IPI_INTERRUPT + ipi_base);
330     unmask_interrupt(MP_IPI_HALT + ipi_base);
331 }
332 
gic_shutdown()333 static void gic_shutdown() {
334     // Turn off all GIC0 interrupts at the distributor.
335     GICREG(0, GICD_CTLR) = 0;
336 }
337 
338 // Returns true if any PPIs are enabled on the calling CPU.
is_ppi_enabled()339 static bool is_ppi_enabled() {
340     DEBUG_ASSERT(arch_ints_disabled());
341 
342     // PPIs are 16-31.
343     uint32_t ppi_mask = 0xffff0000;
344 
345     // GICD_ISENABLER0 is banked so it corresponds to *this* CPU's interface.
346     return (GICREG(0, GICD_ISENABLER(0)) & ppi_mask) != 0;
347 }
348 
349 // Returns true if any SPIs are enabled on the calling CPU.
is_spi_enabled()350 static bool is_spi_enabled() {
351     DEBUG_ASSERT(arch_ints_disabled());
352 
353     // We're going to check four interrupts at a time.  Build a repeated mask for the current CPU.
354     // Each byte in the mask is a CPU bit mask corresponding to CPU0..CPU7 (lsb..msb).
355     uint cpu_num = arch_curr_cpu_num();
356     DEBUG_ASSERT(cpu_num < 8);
357     uint32_t mask = 0x01010101U << cpu_num;
358 
359     for (unsigned int vector = GIC_BASE_SPI; vector < max_irqs; vector += 4) {
360         uint32_t reg = GICREG(0, GICD_ITARGETSR(vector / 4));
361         if (reg & mask) {
362             return true;
363         }
364     }
365 
366     return false;
367 }
368 
gic_shutdown_cpu()369 static void gic_shutdown_cpu() {
370     DEBUG_ASSERT(arch_ints_disabled());
371 
372     // Before we shutdown the GIC, make sure we've migrated/disabled any and all peripheral
373     // interrupts targeted at this CPU (PPIs and SPIs).
374     DEBUG_ASSERT(!is_ppi_enabled());
375     DEBUG_ASSERT(!is_spi_enabled());
376 
377     // Turn off interrupts at the CPU interface.
378     GICREG(0, GICC_CTLR) = 0;
379 }
380 
381 static const struct pdev_interrupt_ops gic_ops = {
382     .mask = gic_mask_interrupt,
383     .unmask = gic_unmask_interrupt,
384     .configure = gic_configure_interrupt,
385     .get_config = gic_get_interrupt_config,
386     .is_valid = gic_is_valid_interrupt,
387     .get_base_vector = gic_get_base_vector,
388     .get_max_vector = gic_get_max_vector,
389     .remap = gic_remap_interrupt,
390     .send_ipi = gic_send_ipi,
391     .init_percpu_early = gic_init_percpu_early,
392     .init_percpu = gic_init_percpu,
393     .handle_irq = gic_handle_irq,
394     .handle_fiq = gic_handle_fiq,
395     .shutdown = gic_shutdown,
396     .shutdown_cpu = gic_shutdown_cpu,
397     .msi_is_supported = arm_gicv2m_msi_is_supported,
398     .msi_supports_masking = arm_gicv2m_msi_supports_masking,
399     .msi_mask_unmask = arm_gicv2m_msi_mask_unmask,
400     .msi_alloc_block = arm_gicv2m_msi_alloc_block,
401     .msi_free_block = arm_gicv2m_msi_free_block,
402     .msi_register_handler = arm_gicv2m_msi_register_handler,
403 };
404 
arm_gic_v2_init(const void * driver_data,uint32_t length)405 static void arm_gic_v2_init(const void* driver_data, uint32_t length) {
406     ASSERT(length >= sizeof(dcfg_arm_gicv2_driver_t));
407     auto driver = static_cast<const dcfg_arm_gicv2_driver_t*>(driver_data);
408     ASSERT(driver->mmio_phys);
409 
410     arm_gicv2_gic_base = periph_paddr_to_vaddr(driver->mmio_phys);
411     ASSERT(arm_gicv2_gic_base);
412     arm_gicv2_gicd_offset = driver->gicd_offset;
413     arm_gicv2_gicc_offset = driver->gicc_offset;
414     arm_gicv2_gich_offset = driver->gich_offset;
415     arm_gicv2_gicv_offset = driver->gicv_offset;
416     ipi_base = driver->ipi_base;
417 
418     if (arm_gic_init() != ZX_OK) {
419         if (driver->optional) {
420             // failed to detect gic v2 but it's marked optional. continue
421             return;
422         }
423         printf("GICv2: failed to detect GICv2, interrupts will be broken\n");
424         return;
425     }
426 
427     dprintf(SPEW, "detected GICv2 (ID %#x)\n", GICREG(0, GICC_IIDR));
428 
429     // pass the list of physical and virtual addresses for the GICv2m register apertures
430     if (driver->msi_frame_phys) {
431         // the following arrays must be static because arm_gicv2m_init stashes the pointer
432         static paddr_t GICV2M_REG_FRAMES[] = {0};
433         static vaddr_t GICV2M_REG_FRAMES_VIRT[] = {0};
434 
435         GICV2M_REG_FRAMES[0] = driver->msi_frame_phys;
436         GICV2M_REG_FRAMES_VIRT[0] = periph_paddr_to_vaddr(driver->msi_frame_phys);
437         ASSERT(GICV2M_REG_FRAMES_VIRT[0]);
438         arm_gicv2m_init(GICV2M_REG_FRAMES, GICV2M_REG_FRAMES_VIRT, countof(GICV2M_REG_FRAMES));
439     }
440     pdev_register_interrupts(&gic_ops);
441 
442     zx_status_t status = gic_register_sgi_handler(MP_IPI_GENERIC + ipi_base, &mp_mbx_generic_irq);
443     DEBUG_ASSERT(status == ZX_OK);
444     status = gic_register_sgi_handler(MP_IPI_RESCHEDULE + ipi_base, &mp_mbx_reschedule_irq);
445     DEBUG_ASSERT(status == ZX_OK);
446     status = gic_register_sgi_handler(MP_IPI_INTERRUPT + ipi_base, &mp_mbx_interrupt_irq);
447     DEBUG_ASSERT(status == ZX_OK);
448     status = gic_register_sgi_handler(MP_IPI_HALT + ipi_base, &arm_ipi_halt_handler);
449     DEBUG_ASSERT(status == ZX_OK);
450 
451     gicv2_hw_interface_register();
452 }
453 
454 LK_PDEV_INIT(arm_gic_v2_init, KDRV_ARM_GIC_V2, arm_gic_v2_init, LK_INIT_LEVEL_PLATFORM_EARLY);
455