vgic-v3.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <linux/irqchip/arm-gic-v3.h>
  15. #include <linux/kvm.h>
  16. #include <linux/kvm_host.h>
  17. #include <kvm/arm_vgic.h>
  18. #include <asm/kvm_mmu.h>
  19. #include <asm/kvm_asm.h>
  20. #include "vgic.h"
  21. void vgic_v3_process_maintenance(struct kvm_vcpu *vcpu)
  22. {
  23. struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
  24. u32 model = vcpu->kvm->arch.vgic.vgic_model;
  25. if (cpuif->vgic_misr & ICH_MISR_EOI) {
  26. unsigned long eisr_bmap = cpuif->vgic_eisr;
  27. int lr;
  28. for_each_set_bit(lr, &eisr_bmap, kvm_vgic_global_state.nr_lr) {
  29. u32 intid;
  30. u64 val = cpuif->vgic_lr[lr];
  31. if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
  32. intid = val & ICH_LR_VIRTUAL_ID_MASK;
  33. else
  34. intid = val & GICH_LR_VIRTUALID;
  35. WARN_ON(cpuif->vgic_lr[lr] & ICH_LR_STATE);
  36. /* Only SPIs require notification */
  37. if (vgic_valid_spi(vcpu->kvm, intid))
  38. kvm_notify_acked_irq(vcpu->kvm, 0,
  39. intid - VGIC_NR_PRIVATE_IRQS);
  40. }
  41. /*
  42. * In the next iterations of the vcpu loop, if we sync
  43. * the vgic state after flushing it, but before
  44. * entering the guest (this happens for pending
  45. * signals and vmid rollovers), then make sure we
  46. * don't pick up any old maintenance interrupts here.
  47. */
  48. cpuif->vgic_eisr = 0;
  49. }
  50. cpuif->vgic_hcr &= ~ICH_HCR_UIE;
  51. }
  52. void vgic_v3_set_underflow(struct kvm_vcpu *vcpu)
  53. {
  54. struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
  55. cpuif->vgic_hcr |= ICH_HCR_UIE;
  56. }
  57. void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu)
  58. {
  59. struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
  60. u32 model = vcpu->kvm->arch.vgic.vgic_model;
  61. int lr;
  62. for (lr = 0; lr < vcpu->arch.vgic_cpu.used_lrs; lr++) {
  63. u64 val = cpuif->vgic_lr[lr];
  64. u32 intid;
  65. struct vgic_irq *irq;
  66. if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
  67. intid = val & ICH_LR_VIRTUAL_ID_MASK;
  68. else
  69. intid = val & GICH_LR_VIRTUALID;
  70. irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
  71. if (!irq) /* An LPI could have been unmapped. */
  72. continue;
  73. spin_lock(&irq->irq_lock);
  74. /* Always preserve the active bit */
  75. irq->active = !!(val & ICH_LR_ACTIVE_BIT);
  76. /* Edge is the only case where we preserve the pending bit */
  77. if (irq->config == VGIC_CONFIG_EDGE &&
  78. (val & ICH_LR_PENDING_BIT)) {
  79. irq->pending = true;
  80. if (vgic_irq_is_sgi(intid) &&
  81. model == KVM_DEV_TYPE_ARM_VGIC_V2) {
  82. u32 cpuid = val & GICH_LR_PHYSID_CPUID;
  83. cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
  84. irq->source |= (1 << cpuid);
  85. }
  86. }
  87. /*
  88. * Clear soft pending state when level irqs have been acked.
  89. * Always regenerate the pending state.
  90. */
  91. if (irq->config == VGIC_CONFIG_LEVEL) {
  92. if (!(val & ICH_LR_PENDING_BIT))
  93. irq->soft_pending = false;
  94. irq->pending = irq->line_level || irq->soft_pending;
  95. }
  96. spin_unlock(&irq->irq_lock);
  97. vgic_put_irq(vcpu->kvm, irq);
  98. }
  99. }
  100. /* Requires the irq to be locked already */
  101. void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
  102. {
  103. u32 model = vcpu->kvm->arch.vgic.vgic_model;
  104. u64 val = irq->intid;
  105. if (irq->pending) {
  106. val |= ICH_LR_PENDING_BIT;
  107. if (irq->config == VGIC_CONFIG_EDGE)
  108. irq->pending = false;
  109. if (vgic_irq_is_sgi(irq->intid) &&
  110. model == KVM_DEV_TYPE_ARM_VGIC_V2) {
  111. u32 src = ffs(irq->source);
  112. BUG_ON(!src);
  113. val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
  114. irq->source &= ~(1 << (src - 1));
  115. if (irq->source)
  116. irq->pending = true;
  117. }
  118. }
  119. if (irq->active)
  120. val |= ICH_LR_ACTIVE_BIT;
  121. if (irq->hw) {
  122. val |= ICH_LR_HW;
  123. val |= ((u64)irq->hwintid) << ICH_LR_PHYS_ID_SHIFT;
  124. /*
  125. * Never set pending+active on a HW interrupt, as the
  126. * pending state is kept at the physical distributor
  127. * level.
  128. */
  129. if (irq->active && irq->pending)
  130. val &= ~ICH_LR_PENDING_BIT;
  131. } else {
  132. if (irq->config == VGIC_CONFIG_LEVEL)
  133. val |= ICH_LR_EOI;
  134. }
  135. /*
  136. * We currently only support Group1 interrupts, which is a
  137. * known defect. This needs to be addressed at some point.
  138. */
  139. if (model == KVM_DEV_TYPE_ARM_VGIC_V3)
  140. val |= ICH_LR_GROUP;
  141. val |= (u64)irq->priority << ICH_LR_PRIORITY_SHIFT;
  142. vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = val;
  143. }
  144. void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr)
  145. {
  146. vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = 0;
  147. }
  148. void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
  149. {
  150. u32 vmcr;
  151. vmcr = (vmcrp->ctlr << ICH_VMCR_CTLR_SHIFT) & ICH_VMCR_CTLR_MASK;
  152. vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK;
  153. vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK;
  154. vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK;
  155. vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = vmcr;
  156. }
  157. void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
  158. {
  159. u32 vmcr = vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr;
  160. vmcrp->ctlr = (vmcr & ICH_VMCR_CTLR_MASK) >> ICH_VMCR_CTLR_SHIFT;
  161. vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
  162. vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
  163. vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
  164. }
  165. #define INITIAL_PENDBASER_VALUE \
  166. (GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWb) | \
  167. GIC_BASER_CACHEABILITY(GICR_PENDBASER, OUTER, SameAsInner) | \
  168. GIC_BASER_SHAREABILITY(GICR_PENDBASER, InnerShareable))
  169. void vgic_v3_enable(struct kvm_vcpu *vcpu)
  170. {
  171. struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3;
  172. /*
  173. * By forcing VMCR to zero, the GIC will restore the binary
  174. * points to their reset values. Anything else resets to zero
  175. * anyway.
  176. */
  177. vgic_v3->vgic_vmcr = 0;
  178. vgic_v3->vgic_elrsr = ~0;
  179. /*
  180. * If we are emulating a GICv3, we do it in an non-GICv2-compatible
  181. * way, so we force SRE to 1 to demonstrate this to the guest.
  182. * This goes with the spec allowing the value to be RAO/WI.
  183. */
  184. if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  185. vgic_v3->vgic_sre = ICC_SRE_EL1_SRE;
  186. vcpu->arch.vgic_cpu.pendbaser = INITIAL_PENDBASER_VALUE;
  187. } else {
  188. vgic_v3->vgic_sre = 0;
  189. }
  190. /* Get the show on the road... */
  191. vgic_v3->vgic_hcr = ICH_HCR_EN;
  192. }
  193. /* check for overlapping regions and for regions crossing the end of memory */
  194. static bool vgic_v3_check_base(struct kvm *kvm)
  195. {
  196. struct vgic_dist *d = &kvm->arch.vgic;
  197. gpa_t redist_size = KVM_VGIC_V3_REDIST_SIZE;
  198. redist_size *= atomic_read(&kvm->online_vcpus);
  199. if (d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE < d->vgic_dist_base)
  200. return false;
  201. if (d->vgic_redist_base + redist_size < d->vgic_redist_base)
  202. return false;
  203. if (d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE <= d->vgic_redist_base)
  204. return true;
  205. if (d->vgic_redist_base + redist_size <= d->vgic_dist_base)
  206. return true;
  207. return false;
  208. }
  209. int vgic_v3_map_resources(struct kvm *kvm)
  210. {
  211. int ret = 0;
  212. struct vgic_dist *dist = &kvm->arch.vgic;
  213. if (vgic_ready(kvm))
  214. goto out;
  215. if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base) ||
  216. IS_VGIC_ADDR_UNDEF(dist->vgic_redist_base)) {
  217. kvm_err("Need to set vgic distributor addresses first\n");
  218. ret = -ENXIO;
  219. goto out;
  220. }
  221. if (!vgic_v3_check_base(kvm)) {
  222. kvm_err("VGIC redist and dist frames overlap\n");
  223. ret = -EINVAL;
  224. goto out;
  225. }
  226. /*
  227. * For a VGICv3 we require the userland to explicitly initialize
  228. * the VGIC before we need to use it.
  229. */
  230. if (!vgic_initialized(kvm)) {
  231. ret = -EBUSY;
  232. goto out;
  233. }
  234. ret = vgic_register_dist_iodev(kvm, dist->vgic_dist_base, VGIC_V3);
  235. if (ret) {
  236. kvm_err("Unable to register VGICv3 dist MMIO regions\n");
  237. goto out;
  238. }
  239. ret = vgic_register_redist_iodevs(kvm, dist->vgic_redist_base);
  240. if (ret) {
  241. kvm_err("Unable to register VGICv3 redist MMIO regions\n");
  242. goto out;
  243. }
  244. if (vgic_has_its(kvm)) {
  245. ret = vgic_register_its_iodevs(kvm);
  246. if (ret) {
  247. kvm_err("Unable to register VGIC ITS MMIO regions\n");
  248. goto out;
  249. }
  250. }
  251. dist->ready = true;
  252. out:
  253. return ret;
  254. }
  255. /**
  256. * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
  257. * @node: pointer to the DT node
  258. *
  259. * Returns 0 if a GICv3 has been found, returns an error code otherwise
  260. */
  261. int vgic_v3_probe(const struct gic_kvm_info *info)
  262. {
  263. u32 ich_vtr_el2 = kvm_call_hyp(__vgic_v3_get_ich_vtr_el2);
  264. int ret;
  265. /*
  266. * The ListRegs field is 5 bits, but there is a architectural
  267. * maximum of 16 list registers. Just ignore bit 4...
  268. */
  269. kvm_vgic_global_state.nr_lr = (ich_vtr_el2 & 0xf) + 1;
  270. kvm_vgic_global_state.can_emulate_gicv2 = false;
  271. if (!info->vcpu.start) {
  272. kvm_info("GICv3: no GICV resource entry\n");
  273. kvm_vgic_global_state.vcpu_base = 0;
  274. } else if (!PAGE_ALIGNED(info->vcpu.start)) {
  275. pr_warn("GICV physical address 0x%llx not page aligned\n",
  276. (unsigned long long)info->vcpu.start);
  277. kvm_vgic_global_state.vcpu_base = 0;
  278. } else if (!PAGE_ALIGNED(resource_size(&info->vcpu))) {
  279. pr_warn("GICV size 0x%llx not a multiple of page size 0x%lx\n",
  280. (unsigned long long)resource_size(&info->vcpu),
  281. PAGE_SIZE);
  282. kvm_vgic_global_state.vcpu_base = 0;
  283. } else {
  284. kvm_vgic_global_state.vcpu_base = info->vcpu.start;
  285. kvm_vgic_global_state.can_emulate_gicv2 = true;
  286. ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2);
  287. if (ret) {
  288. kvm_err("Cannot register GICv2 KVM device.\n");
  289. return ret;
  290. }
  291. kvm_info("vgic-v2@%llx\n", info->vcpu.start);
  292. }
  293. ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V3);
  294. if (ret) {
  295. kvm_err("Cannot register GICv3 KVM device.\n");
  296. kvm_unregister_device_ops(KVM_DEV_TYPE_ARM_VGIC_V2);
  297. return ret;
  298. }
  299. if (kvm_vgic_global_state.vcpu_base == 0)
  300. kvm_info("disabling GICv2 emulation\n");
  301. kvm_vgic_global_state.vctrl_base = NULL;
  302. kvm_vgic_global_state.type = VGIC_V3;
  303. kvm_vgic_global_state.max_gic_vcpus = VGIC_V3_MAX_CPUS;
  304. return 0;
  305. }