2 * QEMU GRLIB IRQMP Emulator
4 * (Multiprocessor and extended interrupt not supported)
6 * Copyright (c) 2010-2019 AdaCore
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu/osdep.h"
29 #include "hw/sysbus.h"
31 #include "hw/qdev-properties.h"
32 #include "hw/sparc/grlib.h"
35 #include "qapi/error.h"
36 #include "qemu/module.h"
37 #include "qom/object.h"
39 #define IRQMP_MAX_CPU 16
40 #define IRQMP_REG_SIZE 256 /* Size of memory mapped registers */
42 /* Memory mapped register offsets */
43 #define LEVEL_OFFSET 0x00
44 #define PENDING_OFFSET 0x04
45 #define FORCE0_OFFSET 0x08
46 #define CLEAR_OFFSET 0x0C
47 #define MP_STATUS_OFFSET 0x10
48 #define BROADCAST_OFFSET 0x14
49 #define MASK_OFFSET 0x40
50 #define FORCE_OFFSET 0x80
51 #define EXTENDED_OFFSET 0xC0
55 OBJECT_DECLARE_SIMPLE_TYPE(IRQMP
, GRLIB_IRQMP
)
57 typedef struct IRQMPState IRQMPState
;
60 SysBusDevice parent_obj
;
74 uint32_t mask
[IRQMP_MAX_CPU
];
75 uint32_t force
[IRQMP_MAX_CPU
];
76 uint32_t extended
[IRQMP_MAX_CPU
];
81 static void grlib_irqmp_check_irqs(IRQMPState
*state
)
87 assert(state
!= NULL
);
88 assert(state
->parent
!= NULL
);
90 /* IRQ for CPU 0 (no SMP support) */
91 pend
= (state
->pending
| state
->force
[0])
94 level0
= pend
& ~state
->level
;
95 level1
= pend
& state
->level
;
97 trace_grlib_irqmp_check_irqs(state
->pending
, state
->force
[0],
98 state
->mask
[0], level1
, level0
);
100 /* Trigger level1 interrupt first and level0 if there is no level1 */
101 qemu_set_irq(state
->parent
->irq
, level1
?: level0
);
104 static void grlib_irqmp_ack_mask(IRQMPState
*state
, uint32_t mask
)
106 /* Clear registers */
107 state
->pending
&= ~mask
;
108 state
->force
[0] &= ~mask
; /* Only CPU 0 (No SMP support) */
110 grlib_irqmp_check_irqs(state
);
113 void grlib_irqmp_ack(DeviceState
*dev
, int intno
)
115 IRQMP
*irqmp
= GRLIB_IRQMP(dev
);
119 state
= irqmp
->state
;
120 assert(state
!= NULL
);
125 trace_grlib_irqmp_ack(intno
);
127 grlib_irqmp_ack_mask(state
, mask
);
130 static void grlib_irqmp_set_irq(void *opaque
, int irq
, int level
)
132 IRQMP
*irqmp
= GRLIB_IRQMP(opaque
);
138 assert(s
->parent
!= NULL
);
142 trace_grlib_irqmp_set_irq(irq
);
144 if (s
->broadcast
& 1 << irq
) {
145 /* Broadcasted IRQ */
146 for (i
= 0; i
< IRQMP_MAX_CPU
; i
++) {
147 s
->force
[i
] |= 1 << irq
;
150 s
->pending
|= 1 << irq
;
152 grlib_irqmp_check_irqs(s
);
157 static uint64_t grlib_irqmp_read(void *opaque
, hwaddr addr
,
160 IRQMP
*irqmp
= opaque
;
163 assert(irqmp
!= NULL
);
164 state
= irqmp
->state
;
165 assert(state
!= NULL
);
169 /* global registers */
175 return state
->pending
;
178 /* This register is an "alias" for the force register of CPU 0 */
179 return state
->force
[0];
182 case MP_STATUS_OFFSET
:
183 /* Always read as 0 */
186 case BROADCAST_OFFSET
:
187 return state
->broadcast
;
194 if (addr
>= MASK_OFFSET
&& addr
< FORCE_OFFSET
) {
195 int cpu
= (addr
- MASK_OFFSET
) / 4;
196 assert(cpu
>= 0 && cpu
< IRQMP_MAX_CPU
);
198 return state
->mask
[cpu
];
201 /* force registers */
202 if (addr
>= FORCE_OFFSET
&& addr
< EXTENDED_OFFSET
) {
203 int cpu
= (addr
- FORCE_OFFSET
) / 4;
204 assert(cpu
>= 0 && cpu
< IRQMP_MAX_CPU
);
206 return state
->force
[cpu
];
209 /* extended (not supported) */
210 if (addr
>= EXTENDED_OFFSET
&& addr
< IRQMP_REG_SIZE
) {
211 int cpu
= (addr
- EXTENDED_OFFSET
) / 4;
212 assert(cpu
>= 0 && cpu
< IRQMP_MAX_CPU
);
214 return state
->extended
[cpu
];
217 trace_grlib_irqmp_readl_unknown(addr
);
221 static void grlib_irqmp_write(void *opaque
, hwaddr addr
,
222 uint64_t value
, unsigned size
)
224 IRQMP
*irqmp
= opaque
;
227 assert(irqmp
!= NULL
);
228 state
= irqmp
->state
;
229 assert(state
!= NULL
);
233 /* global registers */
236 value
&= 0xFFFF << 1; /* clean up the value */
237 state
->level
= value
;
245 /* This register is an "alias" for the force register of CPU 0 */
247 value
&= 0xFFFE; /* clean up the value */
248 state
->force
[0] = value
;
249 grlib_irqmp_check_irqs(irqmp
->state
);
253 value
&= ~1; /* clean up the value */
254 grlib_irqmp_ack_mask(state
, value
);
257 case MP_STATUS_OFFSET
:
258 /* Read Only (no SMP support) */
261 case BROADCAST_OFFSET
:
262 value
&= 0xFFFE; /* clean up the value */
263 state
->broadcast
= value
;
271 if (addr
>= MASK_OFFSET
&& addr
< FORCE_OFFSET
) {
272 int cpu
= (addr
- MASK_OFFSET
) / 4;
273 assert(cpu
>= 0 && cpu
< IRQMP_MAX_CPU
);
275 value
&= ~1; /* clean up the value */
276 state
->mask
[cpu
] = value
;
277 grlib_irqmp_check_irqs(irqmp
->state
);
281 /* force registers */
282 if (addr
>= FORCE_OFFSET
&& addr
< EXTENDED_OFFSET
) {
283 int cpu
= (addr
- FORCE_OFFSET
) / 4;
284 assert(cpu
>= 0 && cpu
< IRQMP_MAX_CPU
);
286 uint32_t force
= value
& 0xFFFE;
287 uint32_t clear
= (value
>> 16) & 0xFFFE;
288 uint32_t old
= state
->force
[cpu
];
290 state
->force
[cpu
] = (old
| force
) & ~clear
;
291 grlib_irqmp_check_irqs(irqmp
->state
);
295 /* extended (not supported) */
296 if (addr
>= EXTENDED_OFFSET
&& addr
< IRQMP_REG_SIZE
) {
297 int cpu
= (addr
- EXTENDED_OFFSET
) / 4;
298 assert(cpu
>= 0 && cpu
< IRQMP_MAX_CPU
);
300 value
&= 0xF; /* clean up the value */
301 state
->extended
[cpu
] = value
;
305 trace_grlib_irqmp_writel_unknown(addr
, value
);
308 static const MemoryRegionOps grlib_irqmp_ops
= {
309 .read
= grlib_irqmp_read
,
310 .write
= grlib_irqmp_write
,
311 .endianness
= DEVICE_NATIVE_ENDIAN
,
313 .min_access_size
= 4,
314 .max_access_size
= 4,
318 static void grlib_irqmp_reset(DeviceState
*d
)
320 IRQMP
*irqmp
= GRLIB_IRQMP(d
);
321 assert(irqmp
->state
!= NULL
);
323 memset(irqmp
->state
, 0, sizeof *irqmp
->state
);
324 irqmp
->state
->parent
= irqmp
;
327 static void grlib_irqmp_init(Object
*obj
)
329 IRQMP
*irqmp
= GRLIB_IRQMP(obj
);
330 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
332 qdev_init_gpio_in(DEVICE(obj
), grlib_irqmp_set_irq
, MAX_PILS
);
333 qdev_init_gpio_out_named(DEVICE(obj
), &irqmp
->irq
, "grlib-irq", 1);
334 memory_region_init_io(&irqmp
->iomem
, obj
, &grlib_irqmp_ops
, irqmp
,
335 "irqmp", IRQMP_REG_SIZE
);
337 irqmp
->state
= g_malloc0(sizeof *irqmp
->state
);
339 sysbus_init_mmio(dev
, &irqmp
->iomem
);
342 static void grlib_irqmp_class_init(ObjectClass
*klass
, void *data
)
344 DeviceClass
*dc
= DEVICE_CLASS(klass
);
346 dc
->reset
= grlib_irqmp_reset
;
349 static const TypeInfo grlib_irqmp_info
= {
350 .name
= TYPE_GRLIB_IRQMP
,
351 .parent
= TYPE_SYS_BUS_DEVICE
,
352 .instance_size
= sizeof(IRQMP
),
353 .instance_init
= grlib_irqmp_init
,
354 .class_init
= grlib_irqmp_class_init
,
357 static void grlib_irqmp_register_types(void)
359 type_register_static(&grlib_irqmp_info
);
362 type_init(grlib_irqmp_register_types
)