2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * Refactoring for Pi2 Copyright (c) 2015, Microsoft. Written by Andrew Baumann.
4 * This code is licensed under the GNU GPLv2 and later.
5 * Heavily based on pl190.c, copyright terms below:
7 * Arm PrimeCell PL190 Vector Interrupt Controller
9 * Copyright (c) 2006 CodeSourcery.
10 * Written by Paul Brook
12 * This code is licensed under the GPL.
15 #include "qemu/osdep.h"
16 #include "hw/intc/bcm2835_ic.h"
22 #define IRQ_PENDING_BASIC 0x00 /* IRQ basic pending */
23 #define IRQ_PENDING_1 0x04 /* IRQ pending 1 */
24 #define IRQ_PENDING_2 0x08 /* IRQ pending 2 */
25 #define FIQ_CONTROL 0x0C /* FIQ register */
26 #define IRQ_ENABLE_1 0x10 /* Interrupt enable register 1 */
27 #define IRQ_ENABLE_2 0x14 /* Interrupt enable register 2 */
28 #define IRQ_ENABLE_BASIC 0x18 /* Base interrupt enable register */
29 #define IRQ_DISABLE_1 0x1C /* Interrupt disable register 1 */
30 #define IRQ_DISABLE_2 0x20 /* Interrupt disable register 2 */
31 #define IRQ_DISABLE_BASIC 0x24 /* Base interrupt disable register */
33 /* Update interrupts. */
34 static void bcm2835_ic_update(BCM2835ICState
*s
)
39 if (s
->fiq_select
>= GPU_IRQS
) {
41 set
= extract32(s
->arm_irq_level
, s
->fiq_select
- GPU_IRQS
, 1);
43 set
= extract64(s
->gpu_irq_level
, s
->fiq_select
, 1);
46 qemu_set_irq(s
->fiq
, set
);
48 set
= (s
->gpu_irq_level
& s
->gpu_irq_enable
)
49 || (s
->arm_irq_level
& s
->arm_irq_enable
);
50 qemu_set_irq(s
->irq
, set
);
54 static void bcm2835_ic_set_gpu_irq(void *opaque
, int irq
, int level
)
56 BCM2835ICState
*s
= opaque
;
58 assert(irq
>= 0 && irq
< 64);
59 s
->gpu_irq_level
= deposit64(s
->gpu_irq_level
, irq
, 1, level
!= 0);
63 static void bcm2835_ic_set_arm_irq(void *opaque
, int irq
, int level
)
65 BCM2835ICState
*s
= opaque
;
67 assert(irq
>= 0 && irq
< 8);
68 s
->arm_irq_level
= deposit32(s
->arm_irq_level
, irq
, 1, level
!= 0);
72 static const int irq_dups
[] = { 7, 9, 10, 18, 19, 53, 54, 55, 56, 57, 62 };
74 static uint64_t bcm2835_ic_read(void *opaque
, hwaddr offset
, unsigned size
)
76 BCM2835ICState
*s
= opaque
;
78 uint64_t gpu_pending
= s
->gpu_irq_level
& s
->gpu_irq_enable
;
82 case IRQ_PENDING_BASIC
:
83 /* bits 0-7: ARM irqs */
84 res
= s
->arm_irq_level
& s
->arm_irq_enable
;
86 /* bits 8 & 9: pending registers 1 & 2 */
87 res
|= (((uint32_t)gpu_pending
) != 0) << 8;
88 res
|= ((gpu_pending
>> 32) != 0) << 9;
90 /* bits 10-20: selected GPU IRQs */
91 for (i
= 0; i
< ARRAY_SIZE(irq_dups
); i
++) {
92 res
|= extract64(gpu_pending
, irq_dups
[i
], 1) << (i
+ 10);
99 res
= gpu_pending
>> 32;
102 res
= (s
->fiq_enable
<< 7) | s
->fiq_select
;
105 res
= s
->gpu_irq_enable
;
108 res
= s
->gpu_irq_enable
>> 32;
110 case IRQ_ENABLE_BASIC
:
111 res
= s
->arm_irq_enable
;
114 res
= ~s
->gpu_irq_enable
;
117 res
= ~s
->gpu_irq_enable
>> 32;
119 case IRQ_DISABLE_BASIC
:
120 res
= ~s
->arm_irq_enable
;
123 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
131 static void bcm2835_ic_write(void *opaque
, hwaddr offset
, uint64_t val
,
134 BCM2835ICState
*s
= opaque
;
138 s
->fiq_select
= extract32(val
, 0, 7);
139 s
->fiq_enable
= extract32(val
, 7, 1);
142 s
->gpu_irq_enable
|= val
;
145 s
->gpu_irq_enable
|= val
<< 32;
147 case IRQ_ENABLE_BASIC
:
148 s
->arm_irq_enable
|= val
& 0xff;
151 s
->gpu_irq_enable
&= ~val
;
154 s
->gpu_irq_enable
&= ~(val
<< 32);
156 case IRQ_DISABLE_BASIC
:
157 s
->arm_irq_enable
&= ~val
& 0xff;
160 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
164 bcm2835_ic_update(s
);
167 static const MemoryRegionOps bcm2835_ic_ops
= {
168 .read
= bcm2835_ic_read
,
169 .write
= bcm2835_ic_write
,
170 .endianness
= DEVICE_NATIVE_ENDIAN
,
171 .valid
.min_access_size
= 4,
172 .valid
.max_access_size
= 4,
175 static void bcm2835_ic_reset(DeviceState
*d
)
177 BCM2835ICState
*s
= BCM2835_IC(d
);
179 s
->gpu_irq_enable
= 0;
180 s
->arm_irq_enable
= 0;
181 s
->fiq_enable
= false;
185 static void bcm2835_ic_init(Object
*obj
)
187 BCM2835ICState
*s
= BCM2835_IC(obj
);
189 memory_region_init_io(&s
->iomem
, obj
, &bcm2835_ic_ops
, s
, TYPE_BCM2835_IC
,
191 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
193 qdev_init_gpio_in_named(DEVICE(s
), bcm2835_ic_set_gpu_irq
,
194 BCM2835_IC_GPU_IRQ
, GPU_IRQS
);
195 qdev_init_gpio_in_named(DEVICE(s
), bcm2835_ic_set_arm_irq
,
196 BCM2835_IC_ARM_IRQ
, ARM_IRQS
);
198 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->irq
);
199 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->fiq
);
202 static const VMStateDescription vmstate_bcm2835_ic
= {
203 .name
= TYPE_BCM2835_IC
,
205 .minimum_version_id
= 1,
206 .fields
= (VMStateField
[]) {
207 VMSTATE_UINT64(gpu_irq_level
, BCM2835ICState
),
208 VMSTATE_UINT64(gpu_irq_enable
, BCM2835ICState
),
209 VMSTATE_UINT8(arm_irq_level
, BCM2835ICState
),
210 VMSTATE_UINT8(arm_irq_enable
, BCM2835ICState
),
211 VMSTATE_BOOL(fiq_enable
, BCM2835ICState
),
212 VMSTATE_UINT8(fiq_select
, BCM2835ICState
),
213 VMSTATE_END_OF_LIST()
217 static void bcm2835_ic_class_init(ObjectClass
*klass
, void *data
)
219 DeviceClass
*dc
= DEVICE_CLASS(klass
);
221 dc
->reset
= bcm2835_ic_reset
;
222 dc
->vmsd
= &vmstate_bcm2835_ic
;
225 static TypeInfo bcm2835_ic_info
= {
226 .name
= TYPE_BCM2835_IC
,
227 .parent
= TYPE_SYS_BUS_DEVICE
,
228 .instance_size
= sizeof(BCM2835ICState
),
229 .class_init
= bcm2835_ic_class_init
,
230 .instance_init
= bcm2835_ic_init
,
233 static void bcm2835_ic_register_types(void)
235 type_register_static(&bcm2835_ic_info
);
238 type_init(bcm2835_ic_register_types
)