2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) ARM Limited, 2014
7 * Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include <jailhouse/entry.h>
14 #include <jailhouse/mmio.h>
15 #include <jailhouse/paging.h>
16 #include <jailhouse/printk.h>
17 #include <jailhouse/string.h>
18 #include <asm/gic_common.h>
19 #include <asm/irqchip.h>
20 #include <asm/platform.h>
21 #include <asm/setup.h>
22 #include <asm/sysregs.h>
25 #define AMBA_DEVICE 0xb105f00d
28 unsigned long gicd_size;
31 * The init function must be called after the MMU setup, and whilst in the
32 * per-cpu setup, which means that a bool must be set by the master CPU
34 static bool irqchip_is_init;
35 static struct irqchip_ops irqchip;
37 bool spi_in_cell(struct cell *cell, unsigned int spi)
39 /* FIXME: Change the configuration to a bitmask range */
45 spi_mask = cell->arch.spis >> 32;
47 spi_mask = cell->arch.spis;
49 return spi_mask & (1 << (spi & 31));
52 void irqchip_set_pending(struct per_cpu *cpu_data, u16 irq_id, bool try_inject)
54 unsigned int new_tail;
56 if (try_inject && irqchip.inject_irq(cpu_data, irq_id) != -EBUSY)
59 spin_lock(&cpu_data->pending_irqs_lock);
61 new_tail = (cpu_data->pending_irqs_tail + 1) % MAX_PENDING_IRQS;
63 /* Queue space available? */
64 if (new_tail != cpu_data->pending_irqs_head) {
65 cpu_data->pending_irqs[cpu_data->pending_irqs_tail] = irq_id;
66 cpu_data->pending_irqs_tail = new_tail;
68 * Make the change to pending_irqs_tail visible before the
69 * caller sends SGI_INJECT.
74 spin_unlock(&cpu_data->pending_irqs_lock);
77 void irqchip_inject_pending(struct per_cpu *cpu_data)
81 while (cpu_data->pending_irqs_head != cpu_data->pending_irqs_tail) {
82 irq_id = cpu_data->pending_irqs[cpu_data->pending_irqs_head];
84 if (irqchip.inject_irq(cpu_data, irq_id) == -EBUSY) {
86 * The list registers are full, trigger maintenance
87 * interrupt and leave.
89 irqchip.enable_maint_irq(true);
93 cpu_data->pending_irqs_head =
94 (cpu_data->pending_irqs_head + 1) % MAX_PENDING_IRQS;
98 * The software interrupt queue is empty - turn off the maintenance
101 irqchip.enable_maint_irq(false);
104 void irqchip_handle_irq(struct per_cpu *cpu_data)
106 irqchip.handle_irq(cpu_data);
109 void irqchip_eoi_irq(u32 irqn, bool deactivate)
111 irqchip.eoi_irq(irqn, deactivate);
114 int irqchip_send_sgi(struct sgi *sgi)
116 return irqchip.send_sgi(sgi);
119 int irqchip_cpu_init(struct per_cpu *cpu_data)
121 if (irqchip.cpu_init)
122 return irqchip.cpu_init(cpu_data);
127 int irqchip_cpu_reset(struct per_cpu *cpu_data)
129 cpu_data->pending_irqs_head = cpu_data->pending_irqs_tail = 0;
131 if (irqchip.cpu_reset)
132 return irqchip.cpu_reset(cpu_data, false);
137 void irqchip_cpu_shutdown(struct per_cpu *cpu_data)
140 * The GIC backend must take care of only resetting the hyp interface if
141 * it has been initialised: this function may be executed during the
144 if (irqchip.cpu_reset)
145 irqchip.cpu_reset(cpu_data, true);
148 static const struct jailhouse_irqchip *
149 irqchip_find_config(struct jailhouse_cell_desc *config)
151 const struct jailhouse_irqchip *irq_config =
152 jailhouse_cell_irqchips(config);
154 if (config->num_irqchips)
160 int irqchip_cell_init(struct cell *cell)
162 const struct jailhouse_irqchip *pins = irqchip_find_config(cell->config);
164 cell->arch.spis = (pins ? pins->pin_bitmap : 0);
166 return irqchip.cell_init(cell);
169 void irqchip_cell_exit(struct cell *cell)
171 const struct jailhouse_irqchip *root_pins =
172 irqchip_find_config(root_cell.config);
174 /* might be called by arch_shutdown while rolling back
176 if (!irqchip_is_init)
180 root_cell.arch.spis |= cell->arch.spis & root_pins->pin_bitmap;
182 irqchip.cell_exit(cell);
185 void irqchip_root_cell_shrink(struct cell *cell)
187 root_cell.arch.spis &= ~(cell->arch.spis);
190 /* Only the GIC is implemented */
191 extern struct irqchip_ops gic_irqchip;
193 int irqchip_init(void)
199 /* Only executed on master CPU */
203 /* FIXME: parse device tree */
204 gicd_base = GICD_BASE;
205 gicd_size = GICD_SIZE;
207 if ((err = arch_map_device(gicd_base, gicd_base, gicd_size)) != 0)
210 for (i = 3; i >= 0; i--) {
211 cidr = mmio_read32(gicd_base + GICD_CIDR0 + i * 4);
212 dev_id |= cidr << i * 8;
214 if (dev_id != AMBA_DEVICE)
215 goto err_no_distributor;
217 /* Probe the GIC version */
218 pidr2 = mmio_read32(gicd_base + GICD_PIDR2);
219 switch (GICD_PIDR2_ARCH(pidr2)) {
223 memcpy(&irqchip, &gic_irqchip, sizeof(struct irqchip_ops));
228 err = irqchip.init();
229 irqchip_is_init = true;
235 printk("GIC: no distributor found\n");
236 arch_unmap_device(gicd_base, gicd_size);