]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/arm/irqchip.c
arm: store the pending virtual interrupts
[jailhouse.git] / hypervisor / arch / arm / irqchip.c
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) ARM Limited, 2014
5  *
6  * Authors:
7  *  Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12
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>
23
24 /* AMBA's biosfood */
25 #define AMBA_DEVICE     0xb105f00d
26
27 void *gicd_base;
28 unsigned long gicd_size;
29
30 /*
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
33  */
34 static bool irqchip_is_init;
35 static struct irqchip_ops irqchip;
36
37 static int irqchip_init_pending(struct per_cpu *cpu_data)
38 {
39         struct pending_irq *pend_array = page_alloc(&mem_pool, 1);
40
41         if (pend_array == NULL)
42                 return -ENOMEM;
43         memset(pend_array, 0, PAGE_SIZE);
44
45         cpu_data->pending_irqs = pend_array;
46         cpu_data->first_pending = NULL;
47
48         return 0;
49 }
50
51 /*
52  * Find the first available pending struct for insertion. The `prev' pointer is
53  * set to the previous pending interrupt, if any, to help inserting the new one
54  * into the list.
55  * Returns NULL when no slot is available
56  */
57 static struct pending_irq* get_pending_slot(struct per_cpu *cpu_data,
58                                             struct pending_irq **prev)
59 {
60         u32 i, pending_idx;
61         struct pending_irq *pending = cpu_data->first_pending;
62
63         *prev = NULL;
64
65         for (i = 0; i < MAX_PENDING_IRQS; i++) {
66                 pending_idx = pending - cpu_data->pending_irqs;
67                 if (pending == NULL || i < pending_idx)
68                         return cpu_data->pending_irqs + i;
69
70                 *prev = pending;
71                 pending = pending->next;
72         }
73
74         return NULL;
75 }
76
77 int irqchip_insert_pending(struct per_cpu *cpu_data, struct pending_irq *irq)
78 {
79         struct pending_irq *prev = NULL;
80         struct pending_irq *slot;
81
82         spin_lock(&cpu_data->gic_lock);
83
84         slot = get_pending_slot(cpu_data, &prev);
85         if (slot == NULL) {
86                 spin_unlock(&cpu_data->gic_lock);
87                 return -ENOMEM;
88         }
89
90         /*
91          * Don't override the pointers yet, they may be read by the injection
92          * loop. Odds are astronomically low, but hey.
93          */
94         memcpy(slot, irq, sizeof(struct pending_irq) - 2 * sizeof(void *));
95         slot->prev = prev;
96         if (prev) {
97                 slot->next = prev->next;
98                 prev->next = slot;
99         } else {
100                 slot->next = cpu_data->first_pending;
101                 cpu_data->first_pending = slot;
102         }
103         if (slot->next)
104                 slot->next->prev = slot;
105
106         spin_unlock(&cpu_data->gic_lock);
107
108         return 0;
109 }
110
111 /*
112  * Only executed by `irqchip_inject_pending' on a CPU to inject its own stuff.
113  */
114 int irqchip_remove_pending(struct per_cpu *cpu_data, struct pending_irq *irq)
115 {
116         spin_lock(&cpu_data->gic_lock);
117
118         if (cpu_data->first_pending == irq)
119                 cpu_data->first_pending = irq->next;
120         if (irq->prev)
121                 irq->prev->next = irq->next;
122         if (irq->next)
123                 irq->next->prev = irq->prev;
124
125         spin_unlock(&cpu_data->gic_lock);
126
127         return 0;
128 }
129
130 int irqchip_inject_pending(struct per_cpu *cpu_data)
131 {
132         int err;
133         struct pending_irq *pending = cpu_data->first_pending;
134
135         while (pending != NULL) {
136                 err = irqchip.inject_irq(cpu_data, pending);
137                 if (err == -EBUSY)
138                         /* The list registers are full. */
139                         break;
140                 else
141                         /*
142                          * Removal only changes the pointers, but does not
143                          * deallocate anything.
144                          * Concurrent accesses are avoided with the spinlock,
145                          * but the `next' pointer of the current pending object
146                          * may be rewritten by an external insert before or
147                          * after this removal, which isn't an issue.
148                          */
149                         irqchip_remove_pending(cpu_data, pending);
150
151                 pending = pending->next;
152         }
153
154         return 0;
155 }
156
157 void irqchip_handle_irq(struct per_cpu *cpu_data)
158 {
159         irqchip.handle_irq(cpu_data);
160 }
161
162 int irqchip_send_sgi(struct sgi *sgi)
163 {
164         return irqchip.send_sgi(sgi);
165 }
166
167 int irqchip_cpu_init(struct per_cpu *cpu_data)
168 {
169         int err;
170
171         err = irqchip_init_pending(cpu_data);
172         if (err)
173                 return err;
174
175         if (irqchip.cpu_init)
176                 return irqchip.cpu_init(cpu_data);
177
178         return 0;
179 }
180
181 /* Only the GIC is implemented */
182 extern struct irqchip_ops gic_irqchip;
183
184 int irqchip_init(void)
185 {
186         int i, err;
187         u32 pidr2, cidr;
188         u32 dev_id = 0;
189
190         /* Only executed on master CPU */
191         if (irqchip_is_init)
192                 return 0;
193
194         /* FIXME: parse device tree */
195         gicd_base = GICD_BASE;
196         gicd_size = GICD_SIZE;
197
198         if ((err = arch_map_device(gicd_base, gicd_base, gicd_size)) != 0)
199                 return err;
200
201         for (i = 3; i >= 0; i--) {
202                 cidr = mmio_read32(gicd_base + GICD_CIDR0 + i * 4);
203                 dev_id |= cidr << i * 8;
204         }
205         if (dev_id != AMBA_DEVICE)
206                 goto err_no_distributor;
207
208         /* Probe the GIC version */
209         pidr2 = mmio_read32(gicd_base + GICD_PIDR2);
210         switch (GICD_PIDR2_ARCH(pidr2)) {
211         case 0x2:
212                 break;
213         case 0x3:
214         case 0x4:
215                 memcpy(&irqchip, &gic_irqchip, sizeof(struct irqchip_ops));
216                 break;
217         }
218
219         if (irqchip.init) {
220                 err = irqchip.init();
221                 irqchip_is_init = true;
222
223                 return err;
224         }
225
226 err_no_distributor:
227         printk("GIC: no distributor found\n");
228         arch_unmap_device(gicd_base, gicd_size);
229
230         return -ENODEV;
231 }