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 static int irqchip_init_pending(struct per_cpu *cpu_data)
39 struct pending_irq *pend_array;
41 if (cpu_data->pending_irqs == NULL) {
42 cpu_data->pending_irqs = pend_array = page_alloc(&mem_pool, 1);
43 if (pend_array == NULL)
46 pend_array = cpu_data->pending_irqs;
49 memset(pend_array, 0, PAGE_SIZE);
51 cpu_data->pending_irqs = pend_array;
52 cpu_data->first_pending = NULL;
58 * Find the first available pending struct for insertion. The `prev' pointer is
59 * set to the previous pending interrupt, if any, to help inserting the new one
61 * Returns NULL when no slot is available
63 static struct pending_irq* get_pending_slot(struct per_cpu *cpu_data,
64 struct pending_irq **prev)
67 struct pending_irq *pending = cpu_data->first_pending;
71 for (i = 0; i < MAX_PENDING_IRQS; i++) {
72 pending_idx = pending - cpu_data->pending_irqs;
73 if (pending == NULL || i < pending_idx)
74 return cpu_data->pending_irqs + i;
77 pending = pending->next;
83 int irqchip_insert_pending(struct per_cpu *cpu_data, struct pending_irq *irq)
85 struct pending_irq *prev = NULL;
86 struct pending_irq *slot;
88 spin_lock(&cpu_data->gic_lock);
90 slot = get_pending_slot(cpu_data, &prev);
92 spin_unlock(&cpu_data->gic_lock);
97 * Don't override the pointers yet, they may be read by the injection
98 * loop. Odds are astronomically low, but hey.
100 memcpy(slot, irq, sizeof(struct pending_irq) - 2 * sizeof(void *));
103 slot->next = prev->next;
106 slot->next = cpu_data->first_pending;
107 cpu_data->first_pending = slot;
110 slot->next->prev = slot;
112 spin_unlock(&cpu_data->gic_lock);
117 int irqchip_set_pending(struct per_cpu *cpu_data, u32 irq_id, bool try_inject)
119 struct pending_irq pending;
121 pending.virt_id = irq_id;
122 /* Priority must be less than ICC_PMR */
123 pending.priority = 0;
125 if (is_sgi(irq_id)) {
127 pending.type.sgi.maintenance = 0;
128 pending.type.sgi.cpuid = 0;
131 pending.type.irq = irq_id;
134 if (try_inject && irqchip.inject_irq(cpu_data, &pending) == 0)
137 return irqchip_insert_pending(cpu_data, &pending);
141 * Only executed by `irqchip_inject_pending' on a CPU to inject its own stuff.
143 int irqchip_remove_pending(struct per_cpu *cpu_data, struct pending_irq *irq)
145 spin_lock(&cpu_data->gic_lock);
147 if (cpu_data->first_pending == irq)
148 cpu_data->first_pending = irq->next;
150 irq->prev->next = irq->next;
152 irq->next->prev = irq->prev;
154 spin_unlock(&cpu_data->gic_lock);
159 int irqchip_inject_pending(struct per_cpu *cpu_data)
162 struct pending_irq *pending = cpu_data->first_pending;
164 while (pending != NULL) {
165 err = irqchip.inject_irq(cpu_data, pending);
167 /* The list registers are full. */
171 * Removal only changes the pointers, but does not
172 * deallocate anything.
173 * Concurrent accesses are avoided with the spinlock,
174 * but the `next' pointer of the current pending object
175 * may be rewritten by an external insert before or
176 * after this removal, which isn't an issue.
178 irqchip_remove_pending(cpu_data, pending);
180 pending = pending->next;
186 void irqchip_handle_irq(struct per_cpu *cpu_data)
188 irqchip.handle_irq(cpu_data);
191 void irqchip_eoi_irq(u32 irqn, bool deactivate)
193 irqchip.eoi_irq(irqn, deactivate);
196 int irqchip_send_sgi(struct sgi *sgi)
198 return irqchip.send_sgi(sgi);
201 int irqchip_cpu_init(struct per_cpu *cpu_data)
205 err = irqchip_init_pending(cpu_data);
209 if (irqchip.cpu_init)
210 return irqchip.cpu_init(cpu_data);
215 int irqchip_cpu_reset(struct per_cpu *cpu_data)
219 err = irqchip_init_pending(cpu_data);
223 if (irqchip.cpu_reset)
224 return irqchip.cpu_reset(cpu_data);
229 /* Only the GIC is implemented */
230 extern struct irqchip_ops gic_irqchip;
232 int irqchip_init(void)
238 /* Only executed on master CPU */
242 /* FIXME: parse device tree */
243 gicd_base = GICD_BASE;
244 gicd_size = GICD_SIZE;
246 if ((err = arch_map_device(gicd_base, gicd_base, gicd_size)) != 0)
249 for (i = 3; i >= 0; i--) {
250 cidr = mmio_read32(gicd_base + GICD_CIDR0 + i * 4);
251 dev_id |= cidr << i * 8;
253 if (dev_id != AMBA_DEVICE)
254 goto err_no_distributor;
256 /* Probe the GIC version */
257 pidr2 = mmio_read32(gicd_base + GICD_PIDR2);
258 switch (GICD_PIDR2_ARCH(pidr2)) {
263 memcpy(&irqchip, &gic_irqchip, sizeof(struct irqchip_ops));
268 err = irqchip.init();
269 irqchip_is_init = true;
275 printk("GIC: no distributor found\n");
276 arch_unmap_device(gicd_base, gicd_size);