2 * QEMU GRLIB IRQMP Emulator
4 * (Multiprocessor and extended interrupt not supported)
6 * Copyright (c) 2010-2011 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 "hw/sysbus.h"
30 #include "hw/sparc/grlib.h"
34 #define IRQMP_MAX_CPU 16
35 #define IRQMP_REG_SIZE 256 /* Size of memory mapped registers */
37 /* Memory mapped register offsets */
38 #define LEVEL_OFFSET 0x00
39 #define PENDING_OFFSET 0x04
40 #define FORCE0_OFFSET 0x08
41 #define CLEAR_OFFSET 0x0C
42 #define MP_STATUS_OFFSET 0x10
43 #define BROADCAST_OFFSET 0x14
44 #define MASK_OFFSET 0x40
45 #define FORCE_OFFSET 0x80
46 #define EXTENDED_OFFSET 0xC0
48 typedef struct IRQMPState IRQMPState
;
50 typedef struct IRQMP
{
55 void *set_pil_in_opaque
;
66 uint32_t mask
[IRQMP_MAX_CPU
];
67 uint32_t force
[IRQMP_MAX_CPU
];
68 uint32_t extended
[IRQMP_MAX_CPU
];
73 static void grlib_irqmp_check_irqs(IRQMPState
*state
)
78 set_pil_in_fn set_pil_in
;
80 assert(state
!= NULL
);
81 assert(state
->parent
!= NULL
);
83 /* IRQ for CPU 0 (no SMP support) */
84 pend
= (state
->pending
| state
->force
[0])
87 level0
= pend
& ~state
->level
;
88 level1
= pend
& state
->level
;
90 trace_grlib_irqmp_check_irqs(state
->pending
, state
->force
[0],
91 state
->mask
[0], level1
, level0
);
93 set_pil_in
= (set_pil_in_fn
)state
->parent
->set_pil_in
;
95 /* Trigger level1 interrupt first and level0 if there is no level1 */
97 set_pil_in(state
->parent
->set_pil_in_opaque
, level1
);
99 set_pil_in(state
->parent
->set_pil_in_opaque
, level0
);
103 void grlib_irqmp_ack(DeviceState
*dev
, int intno
)
112 sdev
= SYS_BUS_DEVICE(dev
);
113 assert(sdev
!= NULL
);
115 irqmp
= FROM_SYSBUS(typeof(*irqmp
), sdev
);
116 assert(irqmp
!= NULL
);
118 state
= irqmp
->state
;
119 assert(state
!= NULL
);
124 trace_grlib_irqmp_ack(intno
);
126 /* Clear registers */
127 state
->pending
&= ~mask
;
128 state
->force
[0] &= ~mask
; /* Only CPU 0 (No SMP support) */
130 grlib_irqmp_check_irqs(state
);
133 void grlib_irqmp_set_irq(void *opaque
, int irq
, int level
)
139 assert(opaque
!= NULL
);
141 irqmp
= FROM_SYSBUS(typeof(*irqmp
), SYS_BUS_DEVICE(opaque
));
142 assert(irqmp
!= NULL
);
146 assert(s
->parent
!= NULL
);
150 trace_grlib_irqmp_set_irq(irq
);
152 if (s
->broadcast
& 1 << irq
) {
153 /* Broadcasted IRQ */
154 for (i
= 0; i
< IRQMP_MAX_CPU
; i
++) {
155 s
->force
[i
] |= 1 << irq
;
158 s
->pending
|= 1 << irq
;
160 grlib_irqmp_check_irqs(s
);
165 static uint64_t grlib_irqmp_read(void *opaque
, hwaddr addr
,
168 IRQMP
*irqmp
= opaque
;
171 assert(irqmp
!= NULL
);
172 state
= irqmp
->state
;
173 assert(state
!= NULL
);
177 /* global registers */
183 return state
->pending
;
186 /* This register is an "alias" for the force register of CPU 0 */
187 return state
->force
[0];
190 case MP_STATUS_OFFSET
:
191 /* Always read as 0 */
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
;
235 assert(irqmp
!= NULL
);
236 state
= irqmp
->state
;
237 assert(state
!= NULL
);
241 /* global registers */
244 value
&= 0xFFFF << 1; /* clean up the value */
245 state
->level
= value
;
253 /* This register is an "alias" for the force register of CPU 0 */
255 value
&= 0xFFFE; /* clean up the value */
256 state
->force
[0] = value
;
257 grlib_irqmp_check_irqs(irqmp
->state
);
261 value
&= ~1; /* clean up the value */
262 state
->pending
&= ~value
;
265 case MP_STATUS_OFFSET
:
266 /* Read Only (no SMP support) */
269 case BROADCAST_OFFSET
:
270 value
&= 0xFFFE; /* clean up the value */
271 state
->broadcast
= value
;
279 if (addr
>= MASK_OFFSET
&& addr
< FORCE_OFFSET
) {
280 int cpu
= (addr
- MASK_OFFSET
) / 4;
281 assert(cpu
>= 0 && cpu
< IRQMP_MAX_CPU
);
283 value
&= ~1; /* clean up the value */
284 state
->mask
[cpu
] = value
;
285 grlib_irqmp_check_irqs(irqmp
->state
);
289 /* force registers */
290 if (addr
>= FORCE_OFFSET
&& addr
< EXTENDED_OFFSET
) {
291 int cpu
= (addr
- FORCE_OFFSET
) / 4;
292 assert(cpu
>= 0 && cpu
< IRQMP_MAX_CPU
);
294 uint32_t force
= value
& 0xFFFE;
295 uint32_t clear
= (value
>> 16) & 0xFFFE;
296 uint32_t old
= state
->force
[cpu
];
298 state
->force
[cpu
] = (old
| force
) & ~clear
;
299 grlib_irqmp_check_irqs(irqmp
->state
);
303 /* extended (not supported) */
304 if (addr
>= EXTENDED_OFFSET
&& addr
< IRQMP_REG_SIZE
) {
305 int cpu
= (addr
- EXTENDED_OFFSET
) / 4;
306 assert(cpu
>= 0 && cpu
< IRQMP_MAX_CPU
);
308 value
&= 0xF; /* clean up the value */
309 state
->extended
[cpu
] = value
;
313 trace_grlib_irqmp_writel_unknown(addr
, value
);
316 static const MemoryRegionOps grlib_irqmp_ops
= {
317 .read
= grlib_irqmp_read
,
318 .write
= grlib_irqmp_write
,
319 .endianness
= DEVICE_NATIVE_ENDIAN
,
321 .min_access_size
= 4,
322 .max_access_size
= 4,
326 static void grlib_irqmp_reset(DeviceState
*d
)
328 IRQMP
*irqmp
= container_of(d
, IRQMP
, busdev
.qdev
);
329 assert(irqmp
!= NULL
);
330 assert(irqmp
->state
!= NULL
);
332 memset(irqmp
->state
, 0, sizeof *irqmp
->state
);
333 irqmp
->state
->parent
= irqmp
;
336 static int grlib_irqmp_init(SysBusDevice
*dev
)
338 IRQMP
*irqmp
= FROM_SYSBUS(typeof(*irqmp
), dev
);
340 assert(irqmp
!= NULL
);
342 /* Check parameters */
343 if (irqmp
->set_pil_in
== NULL
) {
347 memory_region_init_io(&irqmp
->iomem
, &grlib_irqmp_ops
, irqmp
,
348 "irqmp", IRQMP_REG_SIZE
);
350 irqmp
->state
= g_malloc0(sizeof *irqmp
->state
);
352 sysbus_init_mmio(dev
, &irqmp
->iomem
);
357 static Property grlib_irqmp_properties
[] = {
358 DEFINE_PROP_PTR("set_pil_in", IRQMP
, set_pil_in
),
359 DEFINE_PROP_PTR("set_pil_in_opaque", IRQMP
, set_pil_in_opaque
),
360 DEFINE_PROP_END_OF_LIST(),
363 static void grlib_irqmp_class_init(ObjectClass
*klass
, void *data
)
365 DeviceClass
*dc
= DEVICE_CLASS(klass
);
366 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
368 k
->init
= grlib_irqmp_init
;
369 dc
->reset
= grlib_irqmp_reset
;
370 dc
->props
= grlib_irqmp_properties
;
373 static const TypeInfo grlib_irqmp_info
= {
374 .name
= "grlib,irqmp",
375 .parent
= TYPE_SYS_BUS_DEVICE
,
376 .instance_size
= sizeof(IRQMP
),
377 .class_init
= grlib_irqmp_class_init
,
380 static void grlib_irqmp_register_types(void)
382 type_register_static(&grlib_irqmp_info
);
385 type_init(grlib_irqmp_register_types
)