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
29 #include "qemu/osdep.h"
31 #include "hw/sysbus.h"
33 #include "hw/qdev-properties.h"
34 #include "hw/intc/grlib_irqmp.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
61 OBJECT_DECLARE_SIMPLE_TYPE(IRQMP
, GRLIB_IRQMP
)
63 typedef struct IRQMPState IRQMPState
;
66 SysBusDevice parent_obj
;
72 qemu_irq start_signal
[IRQMP_MAX_CPU
];
73 qemu_irq irq
[IRQMP_MAX_CPU
];
83 uint32_t mask
[IRQMP_MAX_CPU
];
84 uint32_t force
[IRQMP_MAX_CPU
];
85 uint32_t extended
[IRQMP_MAX_CPU
];
90 static void grlib_irqmp_check_irqs(IRQMPState
*state
)
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
,
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
);
126 state
= irqmp
->state
;
127 assert(state
!= NULL
);
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
);
145 assert(s
->parent
!= NULL
);
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
;
157 s
->pending
|= 1 << irq
;
159 grlib_irqmp_check_irqs(s
);
163 static uint64_t grlib_irqmp_read(void *opaque
, hwaddr addr
,
166 IRQMP
*irqmp
= opaque
;
169 assert(irqmp
!= NULL
);
170 state
= irqmp
->state
;
171 assert(state
!= NULL
);
175 /* global registers */
181 return state
->pending
;
184 /* This register is an "alias" for the force register of CPU 0 */
185 return state
->force
[0];
188 /* Always read as 0 */
191 case MP_STATUS_OFFSET
:
192 return state
->mpstatus
;
194 case BROADCAST_OFFSET
:
195 return state
->broadcast
;
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
);
229 static void grlib_irqmp_write(void *opaque
, hwaddr addr
,
230 uint64_t value
, unsigned size
)
232 IRQMP
*irqmp
= opaque
;
236 assert(irqmp
!= NULL
);
237 state
= irqmp
->state
;
238 assert(state
!= NULL
);
242 /* global registers */
245 value
&= 0xFFFF << 1; /* clean up the value */
246 state
->level
= value
;
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
);
262 value
&= ~1; /* clean up the value */
263 for (i
= 0; i
< irqmp
->ncpus
; i
++) {
264 grlib_irqmp_ack_mask(state
, i
, value
);
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
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
);
283 case BROADCAST_OFFSET
:
284 value
&= 0xFFFE; /* clean up the value */
285 state
->broadcast
= value
;
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
);
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
);
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
;
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
,
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
,
362 qdev_init_gpio_in(dev
, grlib_irqmp_set_irq
, MAX_PILS
);
365 * Transitionning from 0 to 1 starts the CPUs. The opposite can't
368 qdev_init_gpio_out_named(dev
, irqmp
->start_signal
, "grlib-start-cpu",
370 qdev_init_gpio_out_named(dev
, irqmp
->irq
, "grlib-irq", irqmp
->ncpus
);
371 memory_region_init_io(&irqmp
->iomem
, OBJECT(dev
), &grlib_irqmp_ops
, irqmp
,
372 "irqmp", IRQMP_REG_SIZE
);
374 irqmp
->state
= g_malloc0(sizeof *irqmp
->state
);
376 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &irqmp
->iomem
);
379 static Property grlib_irqmp_properties
[] = {
380 DEFINE_PROP_UINT32("ncpus", IRQMP
, ncpus
, 1),
381 DEFINE_PROP_END_OF_LIST(),
384 static void grlib_irqmp_class_init(ObjectClass
*klass
, void *data
)
386 DeviceClass
*dc
= DEVICE_CLASS(klass
);
388 dc
->realize
= grlib_irqmp_realize
;
389 device_class_set_legacy_reset(dc
, grlib_irqmp_reset
);
390 device_class_set_props(dc
, grlib_irqmp_properties
);
393 static const TypeInfo grlib_irqmp_info
= {
394 .name
= TYPE_GRLIB_IRQMP
,
395 .parent
= TYPE_SYS_BUS_DEVICE
,
396 .instance_size
= sizeof(IRQMP
),
397 .class_init
= grlib_irqmp_class_init
,
400 static void grlib_irqmp_register_types(void)
402 type_register_static(&grlib_irqmp_info
);
405 type_init(grlib_irqmp_register_types
)