vgic.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ARM Generic Interrupt Controller (GIC) v3 host support
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/kvm.h>
  7. #include <linux/sizes.h>
  8. #include <asm/cputype.h>
  9. #include <asm/kvm_para.h>
  10. #include <asm/kvm.h>
  11. #include "kvm_util.h"
  12. #include "vgic.h"
  13. #include "gic.h"
  14. #include "gic_v3.h"
  15. /*
  16. * vGIC-v3 default host setup
  17. *
  18. * Input args:
  19. * vm - KVM VM
  20. * nr_vcpus - Number of vCPUs supported by this VM
  21. *
  22. * Output args: None
  23. *
  24. * Return: GIC file-descriptor or negative error code upon failure
  25. *
  26. * The function creates a vGIC-v3 device and maps the distributor and
  27. * redistributor regions of the guest. Since it depends on the number of
  28. * vCPUs for the VM, it must be called after all the vCPUs have been created.
  29. */
  30. int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus, uint32_t nr_irqs)
  31. {
  32. int gic_fd;
  33. uint64_t attr;
  34. struct list_head *iter;
  35. unsigned int nr_gic_pages, nr_vcpus_created = 0;
  36. TEST_ASSERT(nr_vcpus, "Number of vCPUs cannot be empty");
  37. /*
  38. * Make sure that the caller is infact calling this
  39. * function after all the vCPUs are added.
  40. */
  41. list_for_each(iter, &vm->vcpus)
  42. nr_vcpus_created++;
  43. TEST_ASSERT(nr_vcpus == nr_vcpus_created,
  44. "Number of vCPUs requested (%u) doesn't match with the ones created for the VM (%u)",
  45. nr_vcpus, nr_vcpus_created);
  46. /* Distributor setup */
  47. gic_fd = __kvm_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_V3);
  48. if (gic_fd < 0)
  49. return gic_fd;
  50. kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0, &nr_irqs);
  51. kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  52. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
  53. attr = GICD_BASE_GPA;
  54. kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  55. KVM_VGIC_V3_ADDR_TYPE_DIST, &attr);
  56. nr_gic_pages = vm_calc_num_guest_pages(vm->mode, KVM_VGIC_V3_DIST_SIZE);
  57. virt_map(vm, GICD_BASE_GPA, GICD_BASE_GPA, nr_gic_pages);
  58. /* Redistributor setup */
  59. attr = REDIST_REGION_ATTR_ADDR(nr_vcpus, GICR_BASE_GPA, 0, 0);
  60. kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  61. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &attr);
  62. nr_gic_pages = vm_calc_num_guest_pages(vm->mode,
  63. KVM_VGIC_V3_REDIST_SIZE * nr_vcpus);
  64. virt_map(vm, GICR_BASE_GPA, GICR_BASE_GPA, nr_gic_pages);
  65. kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  66. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
  67. return gic_fd;
  68. }
  69. /* should only work for level sensitive interrupts */
  70. int _kvm_irq_set_level_info(int gic_fd, uint32_t intid, int level)
  71. {
  72. uint64_t attr = 32 * (intid / 32);
  73. uint64_t index = intid % 32;
  74. uint64_t val;
  75. int ret;
  76. ret = __kvm_device_attr_get(gic_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO,
  77. attr, &val);
  78. if (ret != 0)
  79. return ret;
  80. val |= 1U << index;
  81. ret = __kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO,
  82. attr, &val);
  83. return ret;
  84. }
  85. void kvm_irq_set_level_info(int gic_fd, uint32_t intid, int level)
  86. {
  87. int ret = _kvm_irq_set_level_info(gic_fd, intid, level);
  88. TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO, ret));
  89. }
  90. int _kvm_arm_irq_line(struct kvm_vm *vm, uint32_t intid, int level)
  91. {
  92. uint32_t irq = intid & KVM_ARM_IRQ_NUM_MASK;
  93. TEST_ASSERT(!INTID_IS_SGI(intid), "KVM_IRQ_LINE's interface itself "
  94. "doesn't allow injecting SGIs. There's no mask for it.");
  95. if (INTID_IS_PPI(intid))
  96. irq |= KVM_ARM_IRQ_TYPE_PPI << KVM_ARM_IRQ_TYPE_SHIFT;
  97. else
  98. irq |= KVM_ARM_IRQ_TYPE_SPI << KVM_ARM_IRQ_TYPE_SHIFT;
  99. return _kvm_irq_line(vm, irq, level);
  100. }
  101. void kvm_arm_irq_line(struct kvm_vm *vm, uint32_t intid, int level)
  102. {
  103. int ret = _kvm_arm_irq_line(vm, intid, level);
  104. TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_IRQ_LINE, ret));
  105. }
  106. static void vgic_poke_irq(int gic_fd, uint32_t intid, struct kvm_vcpu *vcpu,
  107. uint64_t reg_off)
  108. {
  109. uint64_t reg = intid / 32;
  110. uint64_t index = intid % 32;
  111. uint64_t attr = reg_off + reg * 4;
  112. uint64_t val;
  113. bool intid_is_private = INTID_IS_SGI(intid) || INTID_IS_PPI(intid);
  114. uint32_t group = intid_is_private ? KVM_DEV_ARM_VGIC_GRP_REDIST_REGS
  115. : KVM_DEV_ARM_VGIC_GRP_DIST_REGS;
  116. if (intid_is_private) {
  117. /* TODO: only vcpu 0 implemented for now. */
  118. assert(vcpu->id == 0);
  119. attr += SZ_64K;
  120. }
  121. /* Check that the addr part of the attr is within 32 bits. */
  122. assert((attr & ~KVM_DEV_ARM_VGIC_OFFSET_MASK) == 0);
  123. /*
  124. * All calls will succeed, even with invalid intid's, as long as the
  125. * addr part of the attr is within 32 bits (checked above). An invalid
  126. * intid will just make the read/writes point to above the intended
  127. * register space (i.e., ICPENDR after ISPENDR).
  128. */
  129. kvm_device_attr_get(gic_fd, group, attr, &val);
  130. val |= 1ULL << index;
  131. kvm_device_attr_set(gic_fd, group, attr, &val);
  132. }
  133. void kvm_irq_write_ispendr(int gic_fd, uint32_t intid, struct kvm_vcpu *vcpu)
  134. {
  135. vgic_poke_irq(gic_fd, intid, vcpu, GICD_ISPENDR);
  136. }
  137. void kvm_irq_write_isactiver(int gic_fd, uint32_t intid, struct kvm_vcpu *vcpu)
  138. {
  139. vgic_poke_irq(gic_fd, intid, vcpu, GICD_ISACTIVER);
  140. }
  141. int vgic_its_setup(struct kvm_vm *vm)
  142. {
  143. int its_fd = kvm_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_ITS);
  144. u64 attr;
  145. attr = GITS_BASE_GPA;
  146. kvm_device_attr_set(its_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  147. KVM_VGIC_ITS_ADDR_TYPE, &attr);
  148. kvm_device_attr_set(its_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  149. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
  150. virt_map(vm, GITS_BASE_GPA, GITS_BASE_GPA,
  151. vm_calc_num_guest_pages(vm->mode, KVM_VGIC_V3_ITS_SIZE));
  152. return its_fd;
  153. }