162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * ARM Generic Interrupt Controller (GIC) v3 host support
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/kvm.h>
762306a36Sopenharmony_ci#include <linux/sizes.h>
862306a36Sopenharmony_ci#include <asm/kvm_para.h>
962306a36Sopenharmony_ci#include <asm/kvm.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "kvm_util.h"
1262306a36Sopenharmony_ci#include "vgic.h"
1362306a36Sopenharmony_ci#include "gic.h"
1462306a36Sopenharmony_ci#include "gic_v3.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * vGIC-v3 default host setup
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * Input args:
2062306a36Sopenharmony_ci *	vm - KVM VM
2162306a36Sopenharmony_ci *	nr_vcpus - Number of vCPUs supported by this VM
2262306a36Sopenharmony_ci *	gicd_base_gpa - Guest Physical Address of the Distributor region
2362306a36Sopenharmony_ci *	gicr_base_gpa - Guest Physical Address of the Redistributor region
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * Output args: None
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * Return: GIC file-descriptor or negative error code upon failure
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * The function creates a vGIC-v3 device and maps the distributor and
3062306a36Sopenharmony_ci * redistributor regions of the guest. Since it depends on the number of
3162306a36Sopenharmony_ci * vCPUs for the VM, it must be called after all the vCPUs have been created.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ciint vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus, uint32_t nr_irqs,
3462306a36Sopenharmony_ci		uint64_t gicd_base_gpa, uint64_t gicr_base_gpa)
3562306a36Sopenharmony_ci{
3662306a36Sopenharmony_ci	int gic_fd;
3762306a36Sopenharmony_ci	uint64_t redist_attr;
3862306a36Sopenharmony_ci	struct list_head *iter;
3962306a36Sopenharmony_ci	unsigned int nr_gic_pages, nr_vcpus_created = 0;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	TEST_ASSERT(nr_vcpus, "Number of vCPUs cannot be empty\n");
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	/*
4462306a36Sopenharmony_ci	 * Make sure that the caller is infact calling this
4562306a36Sopenharmony_ci	 * function after all the vCPUs are added.
4662306a36Sopenharmony_ci	 */
4762306a36Sopenharmony_ci	list_for_each(iter, &vm->vcpus)
4862306a36Sopenharmony_ci		nr_vcpus_created++;
4962306a36Sopenharmony_ci	TEST_ASSERT(nr_vcpus == nr_vcpus_created,
5062306a36Sopenharmony_ci			"Number of vCPUs requested (%u) doesn't match with the ones created for the VM (%u)\n",
5162306a36Sopenharmony_ci			nr_vcpus, nr_vcpus_created);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	/* Distributor setup */
5462306a36Sopenharmony_ci	gic_fd = __kvm_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_V3);
5562306a36Sopenharmony_ci	if (gic_fd < 0)
5662306a36Sopenharmony_ci		return gic_fd;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0, &nr_irqs);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
6162306a36Sopenharmony_ci			    KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
6462306a36Sopenharmony_ci			    KVM_VGIC_V3_ADDR_TYPE_DIST, &gicd_base_gpa);
6562306a36Sopenharmony_ci	nr_gic_pages = vm_calc_num_guest_pages(vm->mode, KVM_VGIC_V3_DIST_SIZE);
6662306a36Sopenharmony_ci	virt_map(vm, gicd_base_gpa, gicd_base_gpa,  nr_gic_pages);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	/* Redistributor setup */
6962306a36Sopenharmony_ci	redist_attr = REDIST_REGION_ATTR_ADDR(nr_vcpus, gicr_base_gpa, 0, 0);
7062306a36Sopenharmony_ci	kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
7162306a36Sopenharmony_ci			    KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &redist_attr);
7262306a36Sopenharmony_ci	nr_gic_pages = vm_calc_num_guest_pages(vm->mode,
7362306a36Sopenharmony_ci						KVM_VGIC_V3_REDIST_SIZE * nr_vcpus);
7462306a36Sopenharmony_ci	virt_map(vm, gicr_base_gpa, gicr_base_gpa,  nr_gic_pages);
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
7762306a36Sopenharmony_ci			    KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	return gic_fd;
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci/* should only work for level sensitive interrupts */
8362306a36Sopenharmony_ciint _kvm_irq_set_level_info(int gic_fd, uint32_t intid, int level)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	uint64_t attr = 32 * (intid / 32);
8662306a36Sopenharmony_ci	uint64_t index = intid % 32;
8762306a36Sopenharmony_ci	uint64_t val;
8862306a36Sopenharmony_ci	int ret;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	ret = __kvm_device_attr_get(gic_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO,
9162306a36Sopenharmony_ci				    attr, &val);
9262306a36Sopenharmony_ci	if (ret != 0)
9362306a36Sopenharmony_ci		return ret;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	val |= 1U << index;
9662306a36Sopenharmony_ci	ret = __kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO,
9762306a36Sopenharmony_ci				    attr, &val);
9862306a36Sopenharmony_ci	return ret;
9962306a36Sopenharmony_ci}
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_civoid kvm_irq_set_level_info(int gic_fd, uint32_t intid, int level)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	int ret = _kvm_irq_set_level_info(gic_fd, intid, level);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO, ret));
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ciint _kvm_arm_irq_line(struct kvm_vm *vm, uint32_t intid, int level)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	uint32_t irq = intid & KVM_ARM_IRQ_NUM_MASK;
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	TEST_ASSERT(!INTID_IS_SGI(intid), "KVM_IRQ_LINE's interface itself "
11362306a36Sopenharmony_ci		"doesn't allow injecting SGIs. There's no mask for it.");
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	if (INTID_IS_PPI(intid))
11662306a36Sopenharmony_ci		irq |= KVM_ARM_IRQ_TYPE_PPI << KVM_ARM_IRQ_TYPE_SHIFT;
11762306a36Sopenharmony_ci	else
11862306a36Sopenharmony_ci		irq |= KVM_ARM_IRQ_TYPE_SPI << KVM_ARM_IRQ_TYPE_SHIFT;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	return _kvm_irq_line(vm, irq, level);
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_civoid kvm_arm_irq_line(struct kvm_vm *vm, uint32_t intid, int level)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	int ret = _kvm_arm_irq_line(vm, intid, level);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_IRQ_LINE, ret));
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic void vgic_poke_irq(int gic_fd, uint32_t intid, struct kvm_vcpu *vcpu,
13162306a36Sopenharmony_ci			  uint64_t reg_off)
13262306a36Sopenharmony_ci{
13362306a36Sopenharmony_ci	uint64_t reg = intid / 32;
13462306a36Sopenharmony_ci	uint64_t index = intid % 32;
13562306a36Sopenharmony_ci	uint64_t attr = reg_off + reg * 4;
13662306a36Sopenharmony_ci	uint64_t val;
13762306a36Sopenharmony_ci	bool intid_is_private = INTID_IS_SGI(intid) || INTID_IS_PPI(intid);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	uint32_t group = intid_is_private ? KVM_DEV_ARM_VGIC_GRP_REDIST_REGS
14062306a36Sopenharmony_ci					  : KVM_DEV_ARM_VGIC_GRP_DIST_REGS;
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	if (intid_is_private) {
14362306a36Sopenharmony_ci		/* TODO: only vcpu 0 implemented for now. */
14462306a36Sopenharmony_ci		assert(vcpu->id == 0);
14562306a36Sopenharmony_ci		attr += SZ_64K;
14662306a36Sopenharmony_ci	}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	/* Check that the addr part of the attr is within 32 bits. */
14962306a36Sopenharmony_ci	assert((attr & ~KVM_DEV_ARM_VGIC_OFFSET_MASK) == 0);
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	/*
15262306a36Sopenharmony_ci	 * All calls will succeed, even with invalid intid's, as long as the
15362306a36Sopenharmony_ci	 * addr part of the attr is within 32 bits (checked above). An invalid
15462306a36Sopenharmony_ci	 * intid will just make the read/writes point to above the intended
15562306a36Sopenharmony_ci	 * register space (i.e., ICPENDR after ISPENDR).
15662306a36Sopenharmony_ci	 */
15762306a36Sopenharmony_ci	kvm_device_attr_get(gic_fd, group, attr, &val);
15862306a36Sopenharmony_ci	val |= 1ULL << index;
15962306a36Sopenharmony_ci	kvm_device_attr_set(gic_fd, group, attr, &val);
16062306a36Sopenharmony_ci}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_civoid kvm_irq_write_ispendr(int gic_fd, uint32_t intid, struct kvm_vcpu *vcpu)
16362306a36Sopenharmony_ci{
16462306a36Sopenharmony_ci	vgic_poke_irq(gic_fd, intid, vcpu, GICD_ISPENDR);
16562306a36Sopenharmony_ci}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_civoid kvm_irq_write_isactiver(int gic_fd, uint32_t intid, struct kvm_vcpu *vcpu)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	vgic_poke_irq(gic_fd, intid, vcpu, GICD_ISACTIVER);
17062306a36Sopenharmony_ci}
171