vgic-init.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015, 2016 ARM Ltd.
  4. */
  5. #include <linux/uaccess.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/cpu.h>
  8. #include <linux/kvm_host.h>
  9. #include <kvm/arm_vgic.h>
  10. #include <asm/kvm_emulate.h>
  11. #include <asm/kvm_mmu.h>
  12. #include "vgic.h"
  13. /*
  14. * Initialization rules: there are multiple stages to the vgic
  15. * initialization, both for the distributor and the CPU interfaces. The basic
  16. * idea is that even though the VGIC is not functional or not requested from
  17. * user space, the critical path of the run loop can still call VGIC functions
  18. * that just won't do anything, without them having to check additional
  19. * initialization flags to ensure they don't look at uninitialized data
  20. * structures.
  21. *
  22. * Distributor:
  23. *
  24. * - kvm_vgic_early_init(): initialization of static data that doesn't
  25. * depend on any sizing information or emulation type. No allocation
  26. * is allowed there.
  27. *
  28. * - vgic_init(): allocation and initialization of the generic data
  29. * structures that depend on sizing information (number of CPUs,
  30. * number of interrupts). Also initializes the vcpu specific data
  31. * structures. Can be executed lazily for GICv2.
  32. *
  33. * CPU Interface:
  34. *
  35. * - kvm_vgic_vcpu_init(): initialization of static data that
  36. * doesn't depend on any sizing information or emulation type. No
  37. * allocation is allowed there.
  38. */
  39. /* EARLY INIT */
  40. /**
  41. * kvm_vgic_early_init() - Initialize static VGIC VCPU data structures
  42. * @kvm: The VM whose VGIC districutor should be initialized
  43. *
  44. * Only do initialization of static structures that don't require any
  45. * allocation or sizing information from userspace. vgic_init() called
  46. * kvm_vgic_dist_init() which takes care of the rest.
  47. */
  48. void kvm_vgic_early_init(struct kvm *kvm)
  49. {
  50. struct vgic_dist *dist = &kvm->arch.vgic;
  51. xa_init_flags(&dist->lpi_xa, XA_FLAGS_LOCK_IRQ);
  52. }
  53. /* CREATION */
  54. /**
  55. * kvm_vgic_create: triggered by the instantiation of the VGIC device by
  56. * user space, either through the legacy KVM_CREATE_IRQCHIP ioctl (v2 only)
  57. * or through the generic KVM_CREATE_DEVICE API ioctl.
  58. * irqchip_in_kernel() tells you if this function succeeded or not.
  59. * @kvm: kvm struct pointer
  60. * @type: KVM_DEV_TYPE_ARM_VGIC_V[23]
  61. */
  62. int kvm_vgic_create(struct kvm *kvm, u32 type)
  63. {
  64. struct kvm_vcpu *vcpu;
  65. unsigned long i;
  66. int ret;
  67. /*
  68. * This function is also called by the KVM_CREATE_IRQCHIP handler,
  69. * which had no chance yet to check the availability of the GICv2
  70. * emulation. So check this here again. KVM_CREATE_DEVICE does
  71. * the proper checks already.
  72. */
  73. if (type == KVM_DEV_TYPE_ARM_VGIC_V2 &&
  74. !kvm_vgic_global_state.can_emulate_gicv2)
  75. return -ENODEV;
  76. /* Must be held to avoid race with vCPU creation */
  77. lockdep_assert_held(&kvm->lock);
  78. ret = -EBUSY;
  79. if (!lock_all_vcpus(kvm))
  80. return ret;
  81. mutex_lock(&kvm->arch.config_lock);
  82. if (irqchip_in_kernel(kvm)) {
  83. ret = -EEXIST;
  84. goto out_unlock;
  85. }
  86. kvm_for_each_vcpu(i, vcpu, kvm) {
  87. if (vcpu_has_run_once(vcpu))
  88. goto out_unlock;
  89. }
  90. ret = 0;
  91. if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
  92. kvm->max_vcpus = VGIC_V2_MAX_CPUS;
  93. else
  94. kvm->max_vcpus = VGIC_V3_MAX_CPUS;
  95. if (atomic_read(&kvm->online_vcpus) > kvm->max_vcpus) {
  96. ret = -E2BIG;
  97. goto out_unlock;
  98. }
  99. kvm->arch.vgic.in_kernel = true;
  100. kvm->arch.vgic.vgic_model = type;
  101. kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
  102. if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
  103. kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
  104. else
  105. INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
  106. out_unlock:
  107. mutex_unlock(&kvm->arch.config_lock);
  108. unlock_all_vcpus(kvm);
  109. return ret;
  110. }
  111. /* INIT/DESTROY */
  112. /**
  113. * kvm_vgic_dist_init: initialize the dist data structures
  114. * @kvm: kvm struct pointer
  115. * @nr_spis: number of spis, frozen by caller
  116. */
  117. static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis)
  118. {
  119. struct vgic_dist *dist = &kvm->arch.vgic;
  120. struct kvm_vcpu *vcpu0 = kvm_get_vcpu(kvm, 0);
  121. int i;
  122. dist->spis = kcalloc(nr_spis, sizeof(struct vgic_irq), GFP_KERNEL_ACCOUNT);
  123. if (!dist->spis)
  124. return -ENOMEM;
  125. /*
  126. * In the following code we do not take the irq struct lock since
  127. * no other action on irq structs can happen while the VGIC is
  128. * not initialized yet:
  129. * If someone wants to inject an interrupt or does a MMIO access, we
  130. * require prior initialization in case of a virtual GICv3 or trigger
  131. * initialization when using a virtual GICv2.
  132. */
  133. for (i = 0; i < nr_spis; i++) {
  134. struct vgic_irq *irq = &dist->spis[i];
  135. irq->intid = i + VGIC_NR_PRIVATE_IRQS;
  136. INIT_LIST_HEAD(&irq->ap_list);
  137. raw_spin_lock_init(&irq->irq_lock);
  138. irq->vcpu = NULL;
  139. irq->target_vcpu = vcpu0;
  140. kref_init(&irq->refcount);
  141. switch (dist->vgic_model) {
  142. case KVM_DEV_TYPE_ARM_VGIC_V2:
  143. irq->targets = 0;
  144. irq->group = 0;
  145. break;
  146. case KVM_DEV_TYPE_ARM_VGIC_V3:
  147. irq->mpidr = 0;
  148. irq->group = 1;
  149. break;
  150. default:
  151. kfree(dist->spis);
  152. dist->spis = NULL;
  153. return -EINVAL;
  154. }
  155. }
  156. return 0;
  157. }
  158. static int vgic_allocate_private_irqs_locked(struct kvm_vcpu *vcpu)
  159. {
  160. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  161. int i;
  162. lockdep_assert_held(&vcpu->kvm->arch.config_lock);
  163. if (vgic_cpu->private_irqs)
  164. return 0;
  165. vgic_cpu->private_irqs = kcalloc(VGIC_NR_PRIVATE_IRQS,
  166. sizeof(struct vgic_irq),
  167. GFP_KERNEL_ACCOUNT);
  168. if (!vgic_cpu->private_irqs)
  169. return -ENOMEM;
  170. /*
  171. * Enable and configure all SGIs to be edge-triggered and
  172. * configure all PPIs as level-triggered.
  173. */
  174. for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
  175. struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
  176. INIT_LIST_HEAD(&irq->ap_list);
  177. raw_spin_lock_init(&irq->irq_lock);
  178. irq->intid = i;
  179. irq->vcpu = NULL;
  180. irq->target_vcpu = vcpu;
  181. kref_init(&irq->refcount);
  182. if (vgic_irq_is_sgi(i)) {
  183. /* SGIs */
  184. irq->enabled = 1;
  185. irq->config = VGIC_CONFIG_EDGE;
  186. } else {
  187. /* PPIs */
  188. irq->config = VGIC_CONFIG_LEVEL;
  189. }
  190. }
  191. return 0;
  192. }
  193. static int vgic_allocate_private_irqs(struct kvm_vcpu *vcpu)
  194. {
  195. int ret;
  196. mutex_lock(&vcpu->kvm->arch.config_lock);
  197. ret = vgic_allocate_private_irqs_locked(vcpu);
  198. mutex_unlock(&vcpu->kvm->arch.config_lock);
  199. return ret;
  200. }
  201. /**
  202. * kvm_vgic_vcpu_init() - Initialize static VGIC VCPU data
  203. * structures and register VCPU-specific KVM iodevs
  204. *
  205. * @vcpu: pointer to the VCPU being created and initialized
  206. *
  207. * Only do initialization, but do not actually enable the
  208. * VGIC CPU interface
  209. */
  210. int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  211. {
  212. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  213. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  214. int ret = 0;
  215. vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF;
  216. INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
  217. raw_spin_lock_init(&vgic_cpu->ap_list_lock);
  218. atomic_set(&vgic_cpu->vgic_v3.its_vpe.vlpi_count, 0);
  219. if (!irqchip_in_kernel(vcpu->kvm))
  220. return 0;
  221. ret = vgic_allocate_private_irqs(vcpu);
  222. if (ret)
  223. return ret;
  224. /*
  225. * If we are creating a VCPU with a GICv3 we must also register the
  226. * KVM io device for the redistributor that belongs to this VCPU.
  227. */
  228. if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  229. mutex_lock(&vcpu->kvm->slots_lock);
  230. ret = vgic_register_redist_iodev(vcpu);
  231. mutex_unlock(&vcpu->kvm->slots_lock);
  232. }
  233. return ret;
  234. }
  235. static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu)
  236. {
  237. if (kvm_vgic_global_state.type == VGIC_V2)
  238. vgic_v2_enable(vcpu);
  239. else
  240. vgic_v3_enable(vcpu);
  241. }
  242. /*
  243. * vgic_init: allocates and initializes dist and vcpu data structures
  244. * depending on two dimensioning parameters:
  245. * - the number of spis
  246. * - the number of vcpus
  247. * The function is generally called when nr_spis has been explicitly set
  248. * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
  249. * vgic_initialized() returns true when this function has succeeded.
  250. */
  251. int vgic_init(struct kvm *kvm)
  252. {
  253. struct vgic_dist *dist = &kvm->arch.vgic;
  254. struct kvm_vcpu *vcpu;
  255. int ret = 0, i;
  256. unsigned long idx;
  257. lockdep_assert_held(&kvm->arch.config_lock);
  258. if (vgic_initialized(kvm))
  259. return 0;
  260. /* Are we also in the middle of creating a VCPU? */
  261. if (kvm->created_vcpus != atomic_read(&kvm->online_vcpus))
  262. return -EBUSY;
  263. /* freeze the number of spis */
  264. if (!dist->nr_spis)
  265. dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
  266. ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
  267. if (ret)
  268. goto out;
  269. /* Initialize groups on CPUs created before the VGIC type was known */
  270. kvm_for_each_vcpu(idx, vcpu, kvm) {
  271. ret = vgic_allocate_private_irqs_locked(vcpu);
  272. if (ret)
  273. goto out;
  274. for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
  275. struct vgic_irq *irq = vgic_get_irq(kvm, vcpu, i);
  276. switch (dist->vgic_model) {
  277. case KVM_DEV_TYPE_ARM_VGIC_V3:
  278. irq->group = 1;
  279. irq->mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
  280. break;
  281. case KVM_DEV_TYPE_ARM_VGIC_V2:
  282. irq->group = 0;
  283. irq->targets = 1U << idx;
  284. break;
  285. default:
  286. ret = -EINVAL;
  287. }
  288. vgic_put_irq(kvm, irq);
  289. if (ret)
  290. goto out;
  291. }
  292. }
  293. /*
  294. * If we have GICv4.1 enabled, unconditionally request enable the
  295. * v4 support so that we get HW-accelerated vSGIs. Otherwise, only
  296. * enable it if we present a virtual ITS to the guest.
  297. */
  298. if (vgic_supports_direct_msis(kvm)) {
  299. ret = vgic_v4_init(kvm);
  300. if (ret)
  301. goto out;
  302. }
  303. kvm_for_each_vcpu(idx, vcpu, kvm)
  304. kvm_vgic_vcpu_enable(vcpu);
  305. ret = kvm_vgic_setup_default_irq_routing(kvm);
  306. if (ret)
  307. goto out;
  308. vgic_debug_init(kvm);
  309. /*
  310. * If userspace didn't set the GIC implementation revision,
  311. * default to the latest and greatest. You know want it.
  312. */
  313. if (!dist->implementation_rev)
  314. dist->implementation_rev = KVM_VGIC_IMP_REV_LATEST;
  315. dist->initialized = true;
  316. out:
  317. return ret;
  318. }
  319. static void kvm_vgic_dist_destroy(struct kvm *kvm)
  320. {
  321. struct vgic_dist *dist = &kvm->arch.vgic;
  322. struct vgic_redist_region *rdreg, *next;
  323. dist->ready = false;
  324. dist->initialized = false;
  325. kfree(dist->spis);
  326. dist->spis = NULL;
  327. dist->nr_spis = 0;
  328. dist->vgic_dist_base = VGIC_ADDR_UNDEF;
  329. if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  330. list_for_each_entry_safe(rdreg, next, &dist->rd_regions, list)
  331. vgic_v3_free_redist_region(kvm, rdreg);
  332. INIT_LIST_HEAD(&dist->rd_regions);
  333. } else {
  334. dist->vgic_cpu_base = VGIC_ADDR_UNDEF;
  335. }
  336. if (vgic_supports_direct_msis(kvm))
  337. vgic_v4_teardown(kvm);
  338. xa_destroy(&dist->lpi_xa);
  339. }
  340. static void __kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
  341. {
  342. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  343. /*
  344. * Retire all pending LPIs on this vcpu anyway as we're
  345. * going to destroy it.
  346. */
  347. vgic_flush_pending_lpis(vcpu);
  348. INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
  349. kfree(vgic_cpu->private_irqs);
  350. vgic_cpu->private_irqs = NULL;
  351. if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  352. /*
  353. * If this vCPU is being destroyed because of a failed creation
  354. * then unregister the redistributor to avoid leaving behind a
  355. * dangling pointer to the vCPU struct.
  356. *
  357. * vCPUs that have been successfully created (i.e. added to
  358. * kvm->vcpu_array) get unregistered in kvm_vgic_destroy(), as
  359. * this function gets called while holding kvm->arch.config_lock
  360. * in the VM teardown path and would otherwise introduce a lock
  361. * inversion w.r.t. kvm->srcu.
  362. *
  363. * vCPUs that failed creation are torn down outside of the
  364. * kvm->arch.config_lock and do not get unregistered in
  365. * kvm_vgic_destroy(), meaning it is both safe and necessary to
  366. * do so here.
  367. */
  368. if (kvm_get_vcpu_by_id(vcpu->kvm, vcpu->vcpu_id) != vcpu)
  369. vgic_unregister_redist_iodev(vcpu);
  370. vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF;
  371. }
  372. }
  373. void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
  374. {
  375. struct kvm *kvm = vcpu->kvm;
  376. mutex_lock(&kvm->slots_lock);
  377. __kvm_vgic_vcpu_destroy(vcpu);
  378. mutex_unlock(&kvm->slots_lock);
  379. }
  380. void kvm_vgic_destroy(struct kvm *kvm)
  381. {
  382. struct kvm_vcpu *vcpu;
  383. unsigned long i;
  384. mutex_lock(&kvm->slots_lock);
  385. mutex_lock(&kvm->arch.config_lock);
  386. vgic_debug_destroy(kvm);
  387. kvm_for_each_vcpu(i, vcpu, kvm)
  388. __kvm_vgic_vcpu_destroy(vcpu);
  389. kvm_vgic_dist_destroy(kvm);
  390. mutex_unlock(&kvm->arch.config_lock);
  391. if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3)
  392. kvm_for_each_vcpu(i, vcpu, kvm)
  393. vgic_unregister_redist_iodev(vcpu);
  394. mutex_unlock(&kvm->slots_lock);
  395. }
  396. /**
  397. * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
  398. * is a GICv2. A GICv3 must be explicitly initialized by userspace using the
  399. * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
  400. * @kvm: kvm struct pointer
  401. */
  402. int vgic_lazy_init(struct kvm *kvm)
  403. {
  404. int ret = 0;
  405. if (unlikely(!vgic_initialized(kvm))) {
  406. /*
  407. * We only provide the automatic initialization of the VGIC
  408. * for the legacy case of a GICv2. Any other type must
  409. * be explicitly initialized once setup with the respective
  410. * KVM device call.
  411. */
  412. if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
  413. return -EBUSY;
  414. mutex_lock(&kvm->arch.config_lock);
  415. ret = vgic_init(kvm);
  416. mutex_unlock(&kvm->arch.config_lock);
  417. }
  418. return ret;
  419. }
  420. /* RESOURCE MAPPING */
  421. /**
  422. * kvm_vgic_map_resources - map the MMIO regions
  423. * @kvm: kvm struct pointer
  424. *
  425. * Map the MMIO regions depending on the VGIC model exposed to the guest
  426. * called on the first VCPU run.
  427. * Also map the virtual CPU interface into the VM.
  428. * v2 calls vgic_init() if not already done.
  429. * v3 and derivatives return an error if the VGIC is not initialized.
  430. * vgic_ready() returns true if this function has succeeded.
  431. */
  432. int kvm_vgic_map_resources(struct kvm *kvm)
  433. {
  434. struct vgic_dist *dist = &kvm->arch.vgic;
  435. enum vgic_type type;
  436. gpa_t dist_base;
  437. int ret = 0;
  438. if (likely(vgic_ready(kvm)))
  439. return 0;
  440. mutex_lock(&kvm->slots_lock);
  441. mutex_lock(&kvm->arch.config_lock);
  442. if (vgic_ready(kvm))
  443. goto out;
  444. if (!irqchip_in_kernel(kvm))
  445. goto out;
  446. if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2) {
  447. ret = vgic_v2_map_resources(kvm);
  448. type = VGIC_V2;
  449. } else {
  450. ret = vgic_v3_map_resources(kvm);
  451. type = VGIC_V3;
  452. }
  453. if (ret)
  454. goto out;
  455. dist_base = dist->vgic_dist_base;
  456. mutex_unlock(&kvm->arch.config_lock);
  457. ret = vgic_register_dist_iodev(kvm, dist_base, type);
  458. if (ret) {
  459. kvm_err("Unable to register VGIC dist MMIO regions\n");
  460. goto out_slots;
  461. }
  462. /*
  463. * kvm_io_bus_register_dev() guarantees all readers see the new MMIO
  464. * registration before returning through synchronize_srcu(), which also
  465. * implies a full memory barrier. As such, marking the distributor as
  466. * 'ready' here is guaranteed to be ordered after all vCPUs having seen
  467. * a completely configured distributor.
  468. */
  469. dist->ready = true;
  470. goto out_slots;
  471. out:
  472. mutex_unlock(&kvm->arch.config_lock);
  473. out_slots:
  474. if (ret)
  475. kvm_vm_dead(kvm);
  476. mutex_unlock(&kvm->slots_lock);
  477. return ret;
  478. }
  479. /* GENERIC PROBE */
  480. void kvm_vgic_cpu_up(void)
  481. {
  482. enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
  483. }
  484. void kvm_vgic_cpu_down(void)
  485. {
  486. disable_percpu_irq(kvm_vgic_global_state.maint_irq);
  487. }
  488. static irqreturn_t vgic_maintenance_handler(int irq, void *data)
  489. {
  490. /*
  491. * We cannot rely on the vgic maintenance interrupt to be
  492. * delivered synchronously. This means we can only use it to
  493. * exit the VM, and we perform the handling of EOIed
  494. * interrupts on the exit path (see vgic_fold_lr_state).
  495. */
  496. return IRQ_HANDLED;
  497. }
  498. static struct gic_kvm_info *gic_kvm_info;
  499. void __init vgic_set_kvm_info(const struct gic_kvm_info *info)
  500. {
  501. BUG_ON(gic_kvm_info != NULL);
  502. gic_kvm_info = kmalloc(sizeof(*info), GFP_KERNEL);
  503. if (gic_kvm_info)
  504. *gic_kvm_info = *info;
  505. }
  506. /**
  507. * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
  508. *
  509. * For a specific CPU, initialize the GIC VE hardware.
  510. */
  511. void kvm_vgic_init_cpu_hardware(void)
  512. {
  513. BUG_ON(preemptible());
  514. /*
  515. * We want to make sure the list registers start out clear so that we
  516. * only have the program the used registers.
  517. */
  518. if (kvm_vgic_global_state.type == VGIC_V2)
  519. vgic_v2_init_lrs();
  520. else
  521. kvm_call_hyp(__vgic_v3_init_lrs);
  522. }
  523. /**
  524. * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
  525. * according to the host GIC model. Accordingly calls either
  526. * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
  527. * instantiated by a guest later on .
  528. */
  529. int kvm_vgic_hyp_init(void)
  530. {
  531. bool has_mask;
  532. int ret;
  533. if (!gic_kvm_info)
  534. return -ENODEV;
  535. has_mask = !gic_kvm_info->no_maint_irq_mask;
  536. if (has_mask && !gic_kvm_info->maint_irq) {
  537. kvm_err("No vgic maintenance irq\n");
  538. return -ENXIO;
  539. }
  540. /*
  541. * If we get one of these oddball non-GICs, taint the kernel,
  542. * as we have no idea of how they *really* behave.
  543. */
  544. if (gic_kvm_info->no_hw_deactivation) {
  545. kvm_info("Non-architectural vgic, tainting kernel\n");
  546. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  547. kvm_vgic_global_state.no_hw_deactivation = true;
  548. }
  549. switch (gic_kvm_info->type) {
  550. case GIC_V2:
  551. ret = vgic_v2_probe(gic_kvm_info);
  552. break;
  553. case GIC_V3:
  554. ret = vgic_v3_probe(gic_kvm_info);
  555. if (!ret) {
  556. static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
  557. kvm_info("GIC system register CPU interface enabled\n");
  558. }
  559. break;
  560. default:
  561. ret = -ENODEV;
  562. }
  563. kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
  564. kfree(gic_kvm_info);
  565. gic_kvm_info = NULL;
  566. if (ret)
  567. return ret;
  568. if (!has_mask && !kvm_vgic_global_state.maint_irq)
  569. return 0;
  570. ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
  571. vgic_maintenance_handler,
  572. "vgic", kvm_get_running_vcpus());
  573. if (ret) {
  574. kvm_err("Cannot register interrupt %d\n",
  575. kvm_vgic_global_state.maint_irq);
  576. return ret;
  577. }
  578. kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
  579. return 0;
  580. }