| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * LoongArch IPI interrupt KVM 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/intc/loongarch_ipi.h" |
| 11 | #include "system/kvm.h" |
| 12 | #include "target/loongarch/cpu.h" |
| 13 | |
| 14 | static void kvm_ipi_access_reg(int fd, uint64_t addr, uint32_t *val, bool write) |
| 15 | { |
| 16 | kvm_device_access(fd, KVM_DEV_LOONGARCH_IPI_GRP_REGS, |
| 17 | addr, val, write, &error_abort); |
| 18 | } |
| 19 | |
| 20 | static void kvm_ipi_access_regs(void *opaque, bool write) |
| 21 | { |
| 22 | LoongsonIPICommonState *ipi = (LoongsonIPICommonState *)opaque; |
| 23 | LoongarchIPIState *lis = LOONGARCH_IPI(opaque); |
| 24 | IPICore *core; |
| 25 | uint64_t attr; |
| 26 | int i, cpu_index, fd = lis->dev_fd; |
| 27 | |
| 28 | if (fd == 0) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | for (i = 0; i < ipi->num_cpu; i++) { |
| 33 | core = &ipi->cpu[i]; |
| 34 | if (core->cpu == NULL) { |
| 35 | continue; |
| 36 | } |
| 37 | cpu_index = i; |
| 38 | |
| 39 | attr = (cpu_index << 16) | CORE_STATUS_OFF; |
| 40 | kvm_ipi_access_reg(fd, attr, &core->status, write); |
| 41 | |
| 42 | attr = (cpu_index << 16) | CORE_EN_OFF; |
| 43 | kvm_ipi_access_reg(fd, attr, &core->en, write); |
| 44 | |
| 45 | attr = (cpu_index << 16) | CORE_SET_OFF; |
| 46 | kvm_ipi_access_reg(fd, attr, &core->set, write); |
| 47 | |
| 48 | attr = (cpu_index << 16) | CORE_CLEAR_OFF; |
| 49 | kvm_ipi_access_reg(fd, attr, &core->clear, write); |
| 50 | |
| 51 | attr = (cpu_index << 16) | CORE_BUF_20; |
| 52 | kvm_ipi_access_reg(fd, attr, &core->buf[0], write); |
| 53 | |
| 54 | attr = (cpu_index << 16) | CORE_BUF_28; |
| 55 | kvm_ipi_access_reg(fd, attr, &core->buf[2], write); |
| 56 | |
| 57 | attr = (cpu_index << 16) | CORE_BUF_30; |
| 58 | kvm_ipi_access_reg(fd, attr, &core->buf[4], write); |
| 59 | |
| 60 | attr = (cpu_index << 16) | CORE_BUF_38; |
| 61 | kvm_ipi_access_reg(fd, attr, &core->buf[6], write); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | int kvm_ipi_get(void *opaque) |
| 66 | { |
| 67 | kvm_ipi_access_regs(opaque, false); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int kvm_ipi_put(void *opaque, int version_id) |
| 72 | { |
| 73 | kvm_ipi_access_regs(opaque, true); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | void kvm_ipi_realize(DeviceState *dev, Error **errp) |
| 78 | { |
| 79 | LoongarchIPIState *lis = LOONGARCH_IPI(dev); |
| 80 | int ret; |
| 81 | |
| 82 | ret = kvm_create_device(kvm_state, KVM_DEV_TYPE_LOONGARCH_IPI, false); |
| 83 | if (ret < 0) { |
| 84 | fprintf(stderr, "IPI KVM_CREATE_DEVICE failed: %s\n", |
| 85 | strerror(-ret)); |
| 86 | abort(); |
| 87 | } |
| 88 | |
| 89 | lis->dev_fd = ret; |
| 90 | } |