new file mode 100644
@@ -0,0 +1,62 @@
+/*
+ * QEMU LOONGARCH interrupt support
+ *
+ * Copyright (C) 2021 Loongson Technology Corporation Limited
+ *
+ * SPDX-License-Identifier: LGPL-2.1+
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/main-loop.h"
+#include "hw/irq.h"
+#include "hw/loongarch/loongarch.h"
+#include "cpu.h"
+
+static void cpu_loongarch_irq_request(void *opaque, int irq, int level)
+{
+ LoongArchCPU *cpu = opaque;
+ CPULoongArchState *env = &cpu->env;
+ CPUState *cs = CPU(cpu);
+ bool locked = false;
+
+ if (irq < 0 || irq > N_IRQS) {
+ return;
+ }
+
+ /* Make sure locking works even if BQL is already held by the caller */
+ if (!qemu_mutex_iothread_locked()) {
+ locked = true;
+ qemu_mutex_lock_iothread();
+ }
+
+ if (level) {
+ env->CSR_ESTAT |= 1 << irq;
+ } else {
+ env->CSR_ESTAT &= ~(1 << irq);
+ }
+
+ if (FIELD_EX64(env->CSR_ESTAT, CSR_ESTAT, IS)) {
+ cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+ } else {
+ cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
+ }
+
+ if (locked) {
+ qemu_mutex_unlock_iothread();
+ }
+}
+
+void cpu_loongarch_init_irq(LoongArchCPU *cpu)
+{
+ CPULoongArchState *env = &cpu->env;
+ qemu_irq *qi;
+ int i;
+
+ qi = qemu_allocate_irqs(cpu_loongarch_irq_request, cpu, N_IRQS);
+ for (i = 0; i < N_IRQS; i++) {
+ env->irq[i] = qi[i];
+ }
+ g_free(qi);
+}
+
+
@@ -145,6 +145,8 @@ static void ls3a5000_virt_init(MachineState *machine)
env = &cpu->env;
cpu_states[i] = env;
+ /* Init CPU internal devices */
+ cpu_loongarch_init_irq(cpu);
cpu_loongarch_clock_init(cpu);
qemu_register_reset(main_cpu_reset, cpu);
}
@@ -1,4 +1,5 @@
loongarch_ss = ss.source_set()
+loongarch_ss.add(files('loongarch_int.c'))
loongarch_ss.add(when: 'CONFIG_LOONGSON_3A5000', if_true: files('ls3a5000_virt.c'))
hw_arch += {'loongarch': loongarch_ss}
@@ -44,4 +44,6 @@ typedef struct LoongarchMachineState {
#define TYPE_LOONGARCH_MACHINE MACHINE_TYPE_NAME("loongson7a")
DECLARE_INSTANCE_CHECKER(LoongarchMachineState, LOONGARCH_MACHINE,
TYPE_LOONGARCH_MACHINE)
+
+void cpu_loongarch_init_irq(LoongArchCPU *cpu);
#endif