1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Loongson 3A5000 ext interrupt controller emulation
5 * Copyright (C) 2021 Loongson Technology Corporation Limited
8 #include "qemu/osdep.h"
9 #include "qemu/module.h"
12 #include "hw/sysbus.h"
13 #include "hw/loongarch/virt.h"
14 #include "hw/qdev-properties.h"
15 #include "exec/address-spaces.h"
16 #include "hw/intc/loongarch_extioi.h"
17 #include "migration/vmstate.h"
21 static void extioi_update_irq(LoongArchExtIOI
*s
, int irq
, int level
)
23 int ipnum
, cpu
, found
, irq_index
, irq_mask
;
25 ipnum
= s
->sw_ipmap
[irq
/ 32];
26 cpu
= s
->sw_coremap
[irq
];
28 irq_mask
= 1 << (irq
& 0x1f);
31 /* if not enable return false */
32 if (((s
->enable
[irq_index
]) & irq_mask
) == 0) {
35 s
->coreisr
[cpu
][irq_index
] |= irq_mask
;
36 found
= find_first_bit(s
->sw_isr
[cpu
][ipnum
], EXTIOI_IRQS
);
37 set_bit(irq
, s
->sw_isr
[cpu
][ipnum
]);
38 if (found
< EXTIOI_IRQS
) {
39 /* other irq is handling, need not update parent irq level */
43 s
->coreisr
[cpu
][irq_index
] &= ~irq_mask
;
44 clear_bit(irq
, s
->sw_isr
[cpu
][ipnum
]);
45 found
= find_first_bit(s
->sw_isr
[cpu
][ipnum
], EXTIOI_IRQS
);
46 if (found
< EXTIOI_IRQS
) {
47 /* other irq is handling, need not update parent irq level */
51 qemu_set_irq(s
->parent_irq
[cpu
][ipnum
], level
);
54 static void extioi_setirq(void *opaque
, int irq
, int level
)
56 LoongArchExtIOI
*s
= LOONGARCH_EXTIOI(opaque
);
57 trace_loongarch_extioi_setirq(irq
, level
);
60 * s->isr should be used in vmstate structure,
61 * but it not support 'unsigned long',
62 * so we have to switch it.
64 set_bit(irq
, (unsigned long *)s
->isr
);
66 clear_bit(irq
, (unsigned long *)s
->isr
);
68 extioi_update_irq(s
, irq
, level
);
71 static MemTxResult
extioi_readw(void *opaque
, hwaddr addr
, uint64_t *data
,
72 unsigned size
, MemTxAttrs attrs
)
74 LoongArchExtIOI
*s
= LOONGARCH_EXTIOI(opaque
);
75 unsigned long offset
= addr
& 0xffff;
79 case EXTIOI_NODETYPE_START
... EXTIOI_NODETYPE_END
- 1:
80 index
= (offset
- EXTIOI_NODETYPE_START
) >> 2;
81 *data
= s
->nodetype
[index
];
83 case EXTIOI_IPMAP_START
... EXTIOI_IPMAP_END
- 1:
84 index
= (offset
- EXTIOI_IPMAP_START
) >> 2;
85 *data
= s
->ipmap
[index
];
87 case EXTIOI_ENABLE_START
... EXTIOI_ENABLE_END
- 1:
88 index
= (offset
- EXTIOI_ENABLE_START
) >> 2;
89 *data
= s
->enable
[index
];
91 case EXTIOI_BOUNCE_START
... EXTIOI_BOUNCE_END
- 1:
92 index
= (offset
- EXTIOI_BOUNCE_START
) >> 2;
93 *data
= s
->bounce
[index
];
95 case EXTIOI_COREISR_START
... EXTIOI_COREISR_END
- 1:
96 index
= (offset
- EXTIOI_COREISR_START
) >> 2;
97 /* using attrs to get current cpu index */
98 cpu
= attrs
.requester_id
;
99 *data
= s
->coreisr
[cpu
][index
];
101 case EXTIOI_COREMAP_START
... EXTIOI_COREMAP_END
- 1:
102 index
= (offset
- EXTIOI_COREMAP_START
) >> 2;
103 *data
= s
->coremap
[index
];
109 trace_loongarch_extioi_readw(addr
, *data
);
113 static inline void extioi_enable_irq(LoongArchExtIOI
*s
, int index
,\
114 uint32_t mask
, int level
)
119 val
= mask
& s
->isr
[index
];
123 * enable bit change from 0 to 1,
124 * need to update irq by pending bits
126 extioi_update_irq(s
, irq
+ index
* 32, level
);
132 static MemTxResult
extioi_writew(void *opaque
, hwaddr addr
,
133 uint64_t val
, unsigned size
,
136 LoongArchExtIOI
*s
= LOONGARCH_EXTIOI(opaque
);
137 int i
, cpu
, index
, old_data
, irq
;
140 trace_loongarch_extioi_writew(addr
, val
);
141 offset
= addr
& 0xffff;
144 case EXTIOI_NODETYPE_START
... EXTIOI_NODETYPE_END
- 1:
145 index
= (offset
- EXTIOI_NODETYPE_START
) >> 2;
146 s
->nodetype
[index
] = val
;
148 case EXTIOI_IPMAP_START
... EXTIOI_IPMAP_END
- 1:
150 * ipmap cannot be set at runtime, can be set only at the beginning
151 * of intr driver, need not update upper irq level
153 index
= (offset
- EXTIOI_IPMAP_START
) >> 2;
154 s
->ipmap
[index
] = val
;
156 * loongarch only support little endian,
157 * so we paresd the value with little endian.
159 val
= cpu_to_le64(val
);
160 for (i
= 0; i
< 4; i
++) {
163 ipnum
= ctz32(ipnum
);
164 ipnum
= (ipnum
>= 4) ? 0 : ipnum
;
165 s
->sw_ipmap
[index
* 4 + i
] = ipnum
;
170 case EXTIOI_ENABLE_START
... EXTIOI_ENABLE_END
- 1:
171 index
= (offset
- EXTIOI_ENABLE_START
) >> 2;
172 old_data
= s
->enable
[index
];
173 s
->enable
[index
] = val
;
176 val
= s
->enable
[index
] & ~old_data
;
177 extioi_enable_irq(s
, index
, val
, 1);
180 val
= ~s
->enable
[index
] & old_data
;
181 extioi_enable_irq(s
, index
, val
, 0);
183 case EXTIOI_BOUNCE_START
... EXTIOI_BOUNCE_END
- 1:
184 /* do not emulate hw bounced irq routing */
185 index
= (offset
- EXTIOI_BOUNCE_START
) >> 2;
186 s
->bounce
[index
] = val
;
188 case EXTIOI_COREISR_START
... EXTIOI_COREISR_END
- 1:
189 index
= (offset
- EXTIOI_COREISR_START
) >> 2;
190 /* using attrs to get current cpu index */
191 cpu
= attrs
.requester_id
;
192 old_data
= s
->coreisr
[cpu
][index
];
193 s
->coreisr
[cpu
][index
] = old_data
& ~val
;
194 /* write 1 to clear interrrupt */
196 irq
= ctz32(old_data
);
198 extioi_update_irq(s
, irq
+ index
* 32, 0);
199 old_data
&= ~(1 << irq
);
200 irq
= ctz32(old_data
);
203 case EXTIOI_COREMAP_START
... EXTIOI_COREMAP_END
- 1:
204 irq
= offset
- EXTIOI_COREMAP_START
;
206 s
->coremap
[index
] = val
;
208 * loongarch only support little endian,
209 * so we paresd the value with little endian.
211 val
= cpu_to_le64(val
);
213 for (i
= 0; i
< 4; i
++) {
216 cpu
= (cpu
>= 4) ? 0 : cpu
;
219 if (s
->sw_coremap
[irq
+ i
] == cpu
) {
223 if (test_bit(irq
, (unsigned long *)s
->isr
)) {
225 * lower irq at old cpu and raise irq at new cpu
227 extioi_update_irq(s
, irq
+ i
, 0);
228 s
->sw_coremap
[irq
+ i
] = cpu
;
229 extioi_update_irq(s
, irq
+ i
, 1);
231 s
->sw_coremap
[irq
+ i
] = cpu
;
241 static const MemoryRegionOps extioi_ops
= {
242 .read_with_attrs
= extioi_readw
,
243 .write_with_attrs
= extioi_writew
,
244 .impl
.min_access_size
= 4,
245 .impl
.max_access_size
= 4,
246 .valid
.min_access_size
= 4,
247 .valid
.max_access_size
= 8,
248 .endianness
= DEVICE_LITTLE_ENDIAN
,
251 static const VMStateDescription vmstate_loongarch_extioi
= {
252 .name
= TYPE_LOONGARCH_EXTIOI
,
254 .minimum_version_id
= 1,
255 .fields
= (VMStateField
[]) {
256 VMSTATE_UINT32_ARRAY(bounce
, LoongArchExtIOI
, EXTIOI_IRQS_GROUP_COUNT
),
257 VMSTATE_UINT32_2DARRAY(coreisr
, LoongArchExtIOI
, EXTIOI_CPUS
,
258 EXTIOI_IRQS_GROUP_COUNT
),
259 VMSTATE_UINT32_ARRAY(nodetype
, LoongArchExtIOI
,
260 EXTIOI_IRQS_NODETYPE_COUNT
/ 2),
261 VMSTATE_UINT32_ARRAY(enable
, LoongArchExtIOI
, EXTIOI_IRQS
/ 32),
262 VMSTATE_UINT32_ARRAY(isr
, LoongArchExtIOI
, EXTIOI_IRQS
/ 32),
263 VMSTATE_UINT32_ARRAY(ipmap
, LoongArchExtIOI
, EXTIOI_IRQS_IPMAP_SIZE
/ 4),
264 VMSTATE_UINT32_ARRAY(coremap
, LoongArchExtIOI
, EXTIOI_IRQS
/ 4),
265 VMSTATE_UINT8_ARRAY(sw_ipmap
, LoongArchExtIOI
, EXTIOI_IRQS_IPMAP_SIZE
),
266 VMSTATE_UINT8_ARRAY(sw_coremap
, LoongArchExtIOI
, EXTIOI_IRQS
),
268 VMSTATE_END_OF_LIST()
272 static void loongarch_extioi_instance_init(Object
*obj
)
274 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
275 LoongArchExtIOI
*s
= LOONGARCH_EXTIOI(obj
);
278 for (i
= 0; i
< EXTIOI_IRQS
; i
++) {
279 sysbus_init_irq(dev
, &s
->irq
[i
]);
282 qdev_init_gpio_in(DEVICE(obj
), extioi_setirq
, EXTIOI_IRQS
);
284 for (cpu
= 0; cpu
< EXTIOI_CPUS
; cpu
++) {
285 memory_region_init_io(&s
->extioi_iocsr_mem
[cpu
], OBJECT(s
), &extioi_ops
,
286 s
, "extioi_iocsr", 0x900);
287 sysbus_init_mmio(dev
, &s
->extioi_iocsr_mem
[cpu
]);
288 for (pin
= 0; pin
< LS3A_INTC_IP
; pin
++) {
289 qdev_init_gpio_out(DEVICE(obj
), &s
->parent_irq
[cpu
][pin
], 1);
292 memory_region_init_io(&s
->extioi_system_mem
, OBJECT(s
), &extioi_ops
,
293 s
, "extioi_system_mem", 0x900);
294 sysbus_init_mmio(dev
, &s
->extioi_system_mem
);
297 static void loongarch_extioi_class_init(ObjectClass
*klass
, void *data
)
299 DeviceClass
*dc
= DEVICE_CLASS(klass
);
301 dc
->vmsd
= &vmstate_loongarch_extioi
;
304 static const TypeInfo loongarch_extioi_info
= {
305 .name
= TYPE_LOONGARCH_EXTIOI
,
306 .parent
= TYPE_SYS_BUS_DEVICE
,
307 .instance_init
= loongarch_extioi_instance_init
,
308 .instance_size
= sizeof(struct LoongArchExtIOI
),
309 .class_init
= loongarch_extioi_class_init
,
312 static void loongarch_extioi_register_types(void)
314 type_register_static(&loongarch_extioi_info
);
317 type_init(loongarch_extioi_register_types
)