.gitlab-ci.d/windows.yml: Drop msys2-32bit job
[qemu/ar7.git] / hw / intc / grlib_irqmp.c
blob144b121d48ad085ca0204402148b0db210292272
1 /*
2 * QEMU GRLIB IRQMP Emulator
4 * (Extended interrupt not supported)
6 * SPDX-License-Identifier: MIT
8 * Copyright (c) 2010-2024 AdaCore
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
29 #include "qemu/osdep.h"
30 #include "hw/irq.h"
31 #include "hw/sysbus.h"
33 #include "hw/qdev-properties.h"
34 #include "hw/intc/grlib_irqmp.h"
36 #include "trace.h"
37 #include "qapi/error.h"
38 #include "qemu/module.h"
39 #include "qom/object.h"
41 #define IRQMP_MAX_CPU 16
42 #define IRQMP_REG_SIZE 256 /* Size of memory mapped registers */
44 /* Memory mapped register offsets */
45 #define LEVEL_OFFSET 0x00
46 #define PENDING_OFFSET 0x04
47 #define FORCE0_OFFSET 0x08
48 #define CLEAR_OFFSET 0x0C
49 #define MP_STATUS_OFFSET 0x10
50 #define BROADCAST_OFFSET 0x14
51 #define MASK_OFFSET 0x40
52 #define FORCE_OFFSET 0x80
53 #define EXTENDED_OFFSET 0xC0
55 /* Multiprocessor Status Register */
56 #define MP_STATUS_CPU_STATUS_MASK ((1 << IRQMP_MAX_CPU)-2)
57 #define MP_STATUS_NCPU_SHIFT 28
59 #define MAX_PILS 16
61 OBJECT_DECLARE_SIMPLE_TYPE(IRQMP, GRLIB_IRQMP)
63 typedef struct IRQMPState IRQMPState;
65 struct IRQMP {
66 SysBusDevice parent_obj;
68 MemoryRegion iomem;
70 unsigned int ncpus;
71 IRQMPState *state;
72 qemu_irq start_signal[IRQMP_MAX_CPU];
73 qemu_irq irq[IRQMP_MAX_CPU];
76 struct IRQMPState {
77 uint32_t level;
78 uint32_t pending;
79 uint32_t clear;
80 uint32_t mpstatus;
81 uint32_t broadcast;
83 uint32_t mask[IRQMP_MAX_CPU];
84 uint32_t force[IRQMP_MAX_CPU];
85 uint32_t extended[IRQMP_MAX_CPU];
87 IRQMP *parent;
90 static void grlib_irqmp_check_irqs(IRQMPState *state)
92 int i;
94 assert(state != NULL);
95 assert(state->parent != NULL);
97 for (i = 0; i < state->parent->ncpus; i++) {
98 uint32_t pend = (state->pending | state->force[i]) & state->mask[i];
99 uint32_t level0 = pend & ~state->level;
100 uint32_t level1 = pend & state->level;
102 trace_grlib_irqmp_check_irqs(state->pending, state->force[i],
103 state->mask[i], level1, level0);
105 /* Trigger level1 interrupt first and level0 if there is no level1 */
106 qemu_set_irq(state->parent->irq[i], level1 ?: level0);
110 static void grlib_irqmp_ack_mask(IRQMPState *state, unsigned int cpu,
111 uint32_t mask)
113 /* Clear registers */
114 state->pending &= ~mask;
115 state->force[cpu] &= ~mask;
117 grlib_irqmp_check_irqs(state);
120 void grlib_irqmp_ack(DeviceState *dev, unsigned int cpu, int intno)
122 IRQMP *irqmp = GRLIB_IRQMP(dev);
123 IRQMPState *state;
124 uint32_t mask;
126 state = irqmp->state;
127 assert(state != NULL);
129 intno &= 15;
130 mask = 1 << intno;
132 trace_grlib_irqmp_ack(intno);
134 grlib_irqmp_ack_mask(state, cpu, mask);
137 static void grlib_irqmp_set_irq(void *opaque, int irq, int level)
139 IRQMP *irqmp = GRLIB_IRQMP(opaque);
140 IRQMPState *s;
141 int i = 0;
143 s = irqmp->state;
144 assert(s != NULL);
145 assert(s->parent != NULL);
148 if (level) {
149 trace_grlib_irqmp_set_irq(irq);
151 if (s->broadcast & 1 << irq) {
152 /* Broadcasted IRQ */
153 for (i = 0; i < IRQMP_MAX_CPU; i++) {
154 s->force[i] |= 1 << irq;
156 } else {
157 s->pending |= 1 << irq;
159 grlib_irqmp_check_irqs(s);
163 static uint64_t grlib_irqmp_read(void *opaque, hwaddr addr,
164 unsigned size)
166 IRQMP *irqmp = opaque;
167 IRQMPState *state;
169 assert(irqmp != NULL);
170 state = irqmp->state;
171 assert(state != NULL);
173 addr &= 0xff;
175 /* global registers */
176 switch (addr) {
177 case LEVEL_OFFSET:
178 return state->level;
180 case PENDING_OFFSET:
181 return state->pending;
183 case FORCE0_OFFSET:
184 /* This register is an "alias" for the force register of CPU 0 */
185 return state->force[0];
187 case CLEAR_OFFSET:
188 /* Always read as 0 */
189 return 0;
191 case MP_STATUS_OFFSET:
192 return state->mpstatus;
194 case BROADCAST_OFFSET:
195 return state->broadcast;
197 default:
198 break;
201 /* mask registers */
202 if (addr >= MASK_OFFSET && addr < FORCE_OFFSET) {
203 int cpu = (addr - MASK_OFFSET) / 4;
204 assert(cpu >= 0 && cpu < IRQMP_MAX_CPU);
206 return state->mask[cpu];
209 /* force registers */
210 if (addr >= FORCE_OFFSET && addr < EXTENDED_OFFSET) {
211 int cpu = (addr - FORCE_OFFSET) / 4;
212 assert(cpu >= 0 && cpu < IRQMP_MAX_CPU);
214 return state->force[cpu];
217 /* extended (not supported) */
218 if (addr >= EXTENDED_OFFSET && addr < IRQMP_REG_SIZE) {
219 int cpu = (addr - EXTENDED_OFFSET) / 4;
220 assert(cpu >= 0 && cpu < IRQMP_MAX_CPU);
222 return state->extended[cpu];
225 trace_grlib_irqmp_readl_unknown(addr);
226 return 0;
229 static void grlib_irqmp_write(void *opaque, hwaddr addr,
230 uint64_t value, unsigned size)
232 IRQMP *irqmp = opaque;
233 IRQMPState *state;
234 int i;
236 assert(irqmp != NULL);
237 state = irqmp->state;
238 assert(state != NULL);
240 addr &= 0xff;
242 /* global registers */
243 switch (addr) {
244 case LEVEL_OFFSET:
245 value &= 0xFFFF << 1; /* clean up the value */
246 state->level = value;
247 return;
249 case PENDING_OFFSET:
250 /* Read Only */
251 return;
253 case FORCE0_OFFSET:
254 /* This register is an "alias" for the force register of CPU 0 */
256 value &= 0xFFFE; /* clean up the value */
257 state->force[0] = value;
258 grlib_irqmp_check_irqs(irqmp->state);
259 return;
261 case CLEAR_OFFSET:
262 value &= ~1; /* clean up the value */
263 for (i = 0; i < irqmp->ncpus; i++) {
264 grlib_irqmp_ack_mask(state, i, value);
266 return;
268 case MP_STATUS_OFFSET:
270 * Writing and reading operations are reversed for the CPU status.
271 * Writing "1" will start the CPU, but reading "1" means that the CPU
272 * is power-down.
274 value &= MP_STATUS_CPU_STATUS_MASK;
275 for (i = 0; i < irqmp->ncpus; i++) {
276 if ((value >> i) & 1) {
277 qemu_set_irq(irqmp->start_signal[i], 1);
278 state->mpstatus &= ~(1 << i);
281 return;
283 case BROADCAST_OFFSET:
284 value &= 0xFFFE; /* clean up the value */
285 state->broadcast = value;
286 return;
288 default:
289 break;
292 /* mask registers */
293 if (addr >= MASK_OFFSET && addr < FORCE_OFFSET) {
294 int cpu = (addr - MASK_OFFSET) / 4;
295 assert(cpu >= 0 && cpu < IRQMP_MAX_CPU);
297 value &= ~1; /* clean up the value */
298 state->mask[cpu] = value;
299 grlib_irqmp_check_irqs(irqmp->state);
300 return;
303 /* force registers */
304 if (addr >= FORCE_OFFSET && addr < EXTENDED_OFFSET) {
305 int cpu = (addr - FORCE_OFFSET) / 4;
306 assert(cpu >= 0 && cpu < IRQMP_MAX_CPU);
308 uint32_t force = value & 0xFFFE;
309 uint32_t clear = (value >> 16) & 0xFFFE;
310 uint32_t old = state->force[cpu];
312 state->force[cpu] = (old | force) & ~clear;
313 grlib_irqmp_check_irqs(irqmp->state);
314 return;
317 /* extended (not supported) */
318 if (addr >= EXTENDED_OFFSET && addr < IRQMP_REG_SIZE) {
319 int cpu = (addr - EXTENDED_OFFSET) / 4;
320 assert(cpu >= 0 && cpu < IRQMP_MAX_CPU);
322 value &= 0xF; /* clean up the value */
323 state->extended[cpu] = value;
324 return;
327 trace_grlib_irqmp_writel_unknown(addr, value);
330 static const MemoryRegionOps grlib_irqmp_ops = {
331 .read = grlib_irqmp_read,
332 .write = grlib_irqmp_write,
333 .endianness = DEVICE_NATIVE_ENDIAN,
334 .valid = {
335 .min_access_size = 4,
336 .max_access_size = 4,
340 static void grlib_irqmp_reset(DeviceState *d)
342 IRQMP *irqmp = GRLIB_IRQMP(d);
343 assert(irqmp->state != NULL);
345 memset(irqmp->state, 0, sizeof *irqmp->state);
346 irqmp->state->parent = irqmp;
347 irqmp->state->mpstatus = ((irqmp->ncpus - 1) << MP_STATUS_NCPU_SHIFT) |
348 ((1 << irqmp->ncpus) - 2);
351 static void grlib_irqmp_realize(DeviceState *dev, Error **errp)
353 IRQMP *irqmp = GRLIB_IRQMP(dev);
355 if ((!irqmp->ncpus) || (irqmp->ncpus > IRQMP_MAX_CPU)) {
356 error_setg(errp, "Invalid ncpus properties: "
357 "%u, must be 0 < ncpus =< %u.", irqmp->ncpus,
358 IRQMP_MAX_CPU);
361 qdev_init_gpio_in(dev, grlib_irqmp_set_irq, MAX_PILS);
364 * Transitionning from 0 to 1 starts the CPUs. The opposite can't
365 * happen.
367 qdev_init_gpio_out_named(dev, irqmp->start_signal, "grlib-start-cpu",
368 IRQMP_MAX_CPU);
369 qdev_init_gpio_out_named(dev, irqmp->irq, "grlib-irq", irqmp->ncpus);
370 memory_region_init_io(&irqmp->iomem, OBJECT(dev), &grlib_irqmp_ops, irqmp,
371 "irqmp", IRQMP_REG_SIZE);
373 irqmp->state = g_malloc0(sizeof *irqmp->state);
375 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &irqmp->iomem);
378 static Property grlib_irqmp_properties[] = {
379 DEFINE_PROP_UINT32("ncpus", IRQMP, ncpus, 1),
380 DEFINE_PROP_END_OF_LIST(),
383 static void grlib_irqmp_class_init(ObjectClass *klass, void *data)
385 DeviceClass *dc = DEVICE_CLASS(klass);
387 dc->realize = grlib_irqmp_realize;
388 dc->reset = grlib_irqmp_reset;
389 device_class_set_props(dc, grlib_irqmp_properties);
392 static const TypeInfo grlib_irqmp_info = {
393 .name = TYPE_GRLIB_IRQMP,
394 .parent = TYPE_SYS_BUS_DEVICE,
395 .instance_size = sizeof(IRQMP),
396 .class_init = grlib_irqmp_class_init,
399 static void grlib_irqmp_register_types(void)
401 type_register_static(&grlib_irqmp_info);
404 type_init(grlib_irqmp_register_types)