| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * LoongArch kvm pch pic interrupt support |
| 4 | * |
| 5 | * Copyright (C) 2025 Loongson Technology Corporation Limited |
| 6 | */ |
| 7 | |
| 8 | #include "qemu/osdep.h" |
| 9 | #include "qapi/error.h" |
| 10 | #include "hw/core/boards.h" |
| 11 | #include "hw/intc/loongarch_pch_pic.h" |
| 12 | #include "hw/loongarch/virt.h" |
| 13 | #include "system/kvm.h" |
| 14 | |
| 15 | static void kvm_pch_pic_access_reg(int fd, uint64_t addr, void *val, bool write) |
| 16 | { |
| 17 | kvm_device_access(fd, KVM_DEV_LOONGARCH_PCH_PIC_GRP_REGS, |
| 18 | addr, val, write, &error_abort); |
| 19 | } |
| 20 | |
| 21 | static void kvm_pch_pic_access(void *opaque, bool write) |
| 22 | { |
| 23 | LoongArchPICCommonState *s = LOONGARCH_PIC_COMMON(opaque); |
| 24 | LoongarchPICState *lps = LOONGARCH_PIC(opaque); |
| 25 | int fd = lps->dev_fd; |
| 26 | int addr, offset; |
| 27 | |
| 28 | if (fd == 0) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | kvm_pch_pic_access_reg(fd, PCH_PIC_INT_MASK, &s->int_mask, write); |
| 33 | kvm_pch_pic_access_reg(fd, PCH_PIC_HTMSI_EN, &s->htmsi_en, write); |
| 34 | kvm_pch_pic_access_reg(fd, PCH_PIC_INT_EDGE, &s->intedge, write); |
| 35 | kvm_pch_pic_access_reg(fd, PCH_PIC_AUTO_CTRL0, &s->auto_crtl0, write); |
| 36 | kvm_pch_pic_access_reg(fd, PCH_PIC_AUTO_CTRL1, &s->auto_crtl1, write); |
| 37 | |
| 38 | for (addr = PCH_PIC_ROUTE_ENTRY; |
| 39 | addr < PCH_PIC_ROUTE_ENTRY_END; addr++) { |
| 40 | offset = addr - PCH_PIC_ROUTE_ENTRY; |
| 41 | kvm_pch_pic_access_reg(fd, addr, &s->route_entry[offset], write); |
| 42 | } |
| 43 | |
| 44 | for (addr = PCH_PIC_HTMSI_VEC; addr < PCH_PIC_HTMSI_VEC_END; addr++) { |
| 45 | offset = addr - PCH_PIC_HTMSI_VEC; |
| 46 | kvm_pch_pic_access_reg(fd, addr, &s->htmsi_vector[offset], write); |
| 47 | } |
| 48 | |
| 49 | kvm_pch_pic_access_reg(fd, PCH_PIC_INT_REQUEST, &s->intirr, write); |
| 50 | kvm_pch_pic_access_reg(fd, PCH_PIC_INT_STATUS, &s->intisr, write); |
| 51 | kvm_pch_pic_access_reg(fd, PCH_PIC_INT_POL, &s->int_polarity, write); |
| 52 | } |
| 53 | |
| 54 | int kvm_pic_get(void *opaque) |
| 55 | { |
| 56 | kvm_pch_pic_access(opaque, false); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int kvm_pic_put(void *opaque, int version_id) |
| 61 | { |
| 62 | kvm_pch_pic_access(opaque, true); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | void kvm_pic_realize(DeviceState *dev, Error **errp) |
| 67 | { |
| 68 | LoongarchPICState *lps = LOONGARCH_PIC(dev); |
| 69 | uint64_t pch_pic_base = VIRT_PCH_REG_BASE; |
| 70 | int ret; |
| 71 | |
| 72 | ret = kvm_create_device(kvm_state, KVM_DEV_TYPE_LOONGARCH_PCHPIC, false); |
| 73 | if (ret < 0) { |
| 74 | fprintf(stderr, "Create KVM_LOONGARCH_PCHPIC failed: %s\n", |
| 75 | strerror(-ret)); |
| 76 | abort(); |
| 77 | } |
| 78 | |
| 79 | lps->dev_fd = ret; |
| 80 | ret = kvm_device_access(lps->dev_fd, KVM_DEV_LOONGARCH_PCH_PIC_GRP_CTRL, |
| 81 | KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIT, |
| 82 | &pch_pic_base, true, NULL); |
| 83 | if (ret < 0) { |
| 84 | fprintf(stderr, "KVM_LOONGARCH_PCH_PIC_INIT failed: %s\n", |
| 85 | strerror(-ret)); |
| 86 | abort(); |
| 87 | } |
| 88 | } |