vgic-init.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright (C) 2015, 2016 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/uaccess.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/cpu.h>
  19. #include <linux/kvm_host.h>
  20. #include <kvm/arm_vgic.h>
  21. #include <asm/kvm_emulate.h>
  22. #include <asm/kvm_mmu.h>
  23. #include "vgic.h"
  24. /*
  25. * Initialization rules: there are multiple stages to the vgic
  26. * initialization, both for the distributor and the CPU interfaces. The basic
  27. * idea is that even though the VGIC is not functional or not requested from
  28. * user space, the critical path of the run loop can still call VGIC functions
  29. * that just won't do anything, without them having to check additional
  30. * initialization flags to ensure they don't look at uninitialized data
  31. * structures.
  32. *
  33. * Distributor:
  34. *
  35. * - kvm_vgic_early_init(): initialization of static data that doesn't
  36. * depend on any sizing information or emulation type. No allocation
  37. * is allowed there.
  38. *
  39. * - vgic_init(): allocation and initialization of the generic data
  40. * structures that depend on sizing information (number of CPUs,
  41. * number of interrupts). Also initializes the vcpu specific data
  42. * structures. Can be executed lazily for GICv2.
  43. *
  44. * CPU Interface:
  45. *
  46. * - kvm_vgic_vcpu_init(): initialization of static data that
  47. * doesn't depend on any sizing information or emulation type. No
  48. * allocation is allowed there.
  49. */
  50. /* EARLY INIT */
  51. /**
  52. * kvm_vgic_early_init() - Initialize static VGIC VCPU data structures
  53. * @kvm: The VM whose VGIC districutor should be initialized
  54. *
  55. * Only do initialization of static structures that don't require any
  56. * allocation or sizing information from userspace. vgic_init() called
  57. * kvm_vgic_dist_init() which takes care of the rest.
  58. */
  59. void kvm_vgic_early_init(struct kvm *kvm)
  60. {
  61. struct vgic_dist *dist = &kvm->arch.vgic;
  62. INIT_LIST_HEAD(&dist->lpi_list_head);
  63. raw_spin_lock_init(&dist->lpi_list_lock);
  64. }
  65. /* CREATION */
  66. /**
  67. * kvm_vgic_create: triggered by the instantiation of the VGIC device by
  68. * user space, either through the legacy KVM_CREATE_IRQCHIP ioctl (v2 only)
  69. * or through the generic KVM_CREATE_DEVICE API ioctl.
  70. * irqchip_in_kernel() tells you if this function succeeded or not.
  71. * @kvm: kvm struct pointer
  72. * @type: KVM_DEV_TYPE_ARM_VGIC_V[23]
  73. */
  74. int kvm_vgic_create(struct kvm *kvm, u32 type)
  75. {
  76. int i, vcpu_lock_idx = -1, ret;
  77. struct kvm_vcpu *vcpu;
  78. if (irqchip_in_kernel(kvm))
  79. return -EEXIST;
  80. /*
  81. * This function is also called by the KVM_CREATE_IRQCHIP handler,
  82. * which had no chance yet to check the availability of the GICv2
  83. * emulation. So check this here again. KVM_CREATE_DEVICE does
  84. * the proper checks already.
  85. */
  86. if (type == KVM_DEV_TYPE_ARM_VGIC_V2 &&
  87. !kvm_vgic_global_state.can_emulate_gicv2)
  88. return -ENODEV;
  89. /*
  90. * Any time a vcpu is run, vcpu_load is called which tries to grab the
  91. * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
  92. * that no other VCPUs are run while we create the vgic.
  93. */
  94. ret = -EBUSY;
  95. kvm_for_each_vcpu(i, vcpu, kvm) {
  96. if (!mutex_trylock(&vcpu->mutex))
  97. goto out_unlock;
  98. vcpu_lock_idx = i;
  99. }
  100. kvm_for_each_vcpu(i, vcpu, kvm) {
  101. if (vcpu->arch.has_run_once)
  102. goto out_unlock;
  103. }
  104. ret = 0;
  105. if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
  106. kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS;
  107. else
  108. kvm->arch.max_vcpus = VGIC_V3_MAX_CPUS;
  109. if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) {
  110. ret = -E2BIG;
  111. goto out_unlock;
  112. }
  113. kvm->arch.vgic.in_kernel = true;
  114. kvm->arch.vgic.vgic_model = type;
  115. kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
  116. if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
  117. kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
  118. else
  119. INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
  120. out_unlock:
  121. for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
  122. vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
  123. mutex_unlock(&vcpu->mutex);
  124. }
  125. return ret;
  126. }
  127. /* INIT/DESTROY */
  128. /**
  129. * kvm_vgic_dist_init: initialize the dist data structures
  130. * @kvm: kvm struct pointer
  131. * @nr_spis: number of spis, frozen by caller
  132. */
  133. static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis)
  134. {
  135. struct vgic_dist *dist = &kvm->arch.vgic;
  136. struct kvm_vcpu *vcpu0 = kvm_get_vcpu(kvm, 0);
  137. int i;
  138. dist->spis = kcalloc(nr_spis, sizeof(struct vgic_irq), GFP_KERNEL);
  139. if (!dist->spis)
  140. return -ENOMEM;
  141. /*
  142. * In the following code we do not take the irq struct lock since
  143. * no other action on irq structs can happen while the VGIC is
  144. * not initialized yet:
  145. * If someone wants to inject an interrupt or does a MMIO access, we
  146. * require prior initialization in case of a virtual GICv3 or trigger
  147. * initialization when using a virtual GICv2.
  148. */
  149. for (i = 0; i < nr_spis; i++) {
  150. struct vgic_irq *irq = &dist->spis[i];
  151. irq->intid = i + VGIC_NR_PRIVATE_IRQS;
  152. INIT_LIST_HEAD(&irq->ap_list);
  153. spin_lock_init(&irq->irq_lock);
  154. irq->vcpu = NULL;
  155. irq->target_vcpu = vcpu0;
  156. kref_init(&irq->refcount);
  157. switch (dist->vgic_model) {
  158. case KVM_DEV_TYPE_ARM_VGIC_V2:
  159. irq->targets = 0;
  160. irq->group = 0;
  161. break;
  162. case KVM_DEV_TYPE_ARM_VGIC_V3:
  163. irq->mpidr = 0;
  164. irq->group = 1;
  165. break;
  166. default:
  167. kfree(dist->spis);
  168. dist->spis = NULL;
  169. return -EINVAL;
  170. }
  171. }
  172. return 0;
  173. }
  174. /**
  175. * kvm_vgic_vcpu_init() - Initialize static VGIC VCPU data
  176. * structures and register VCPU-specific KVM iodevs
  177. *
  178. * @vcpu: pointer to the VCPU being created and initialized
  179. *
  180. * Only do initialization, but do not actually enable the
  181. * VGIC CPU interface
  182. */
  183. int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  184. {
  185. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  186. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  187. int ret = 0;
  188. int i;
  189. vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF;
  190. vgic_cpu->sgi_iodev.base_addr = VGIC_ADDR_UNDEF;
  191. INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
  192. spin_lock_init(&vgic_cpu->ap_list_lock);
  193. /*
  194. * Enable and configure all SGIs to be edge-triggered and
  195. * configure all PPIs as level-triggered.
  196. */
  197. for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
  198. struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
  199. INIT_LIST_HEAD(&irq->ap_list);
  200. spin_lock_init(&irq->irq_lock);
  201. irq->intid = i;
  202. irq->vcpu = NULL;
  203. irq->target_vcpu = vcpu;
  204. kref_init(&irq->refcount);
  205. if (vgic_irq_is_sgi(i)) {
  206. /* SGIs */
  207. irq->enabled = 1;
  208. irq->config = VGIC_CONFIG_EDGE;
  209. } else {
  210. /* PPIs */
  211. irq->config = VGIC_CONFIG_LEVEL;
  212. }
  213. }
  214. if (!irqchip_in_kernel(vcpu->kvm))
  215. return 0;
  216. /*
  217. * If we are creating a VCPU with a GICv3 we must also register the
  218. * KVM io device for the redistributor that belongs to this VCPU.
  219. */
  220. if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  221. mutex_lock(&vcpu->kvm->lock);
  222. ret = vgic_register_redist_iodev(vcpu);
  223. mutex_unlock(&vcpu->kvm->lock);
  224. }
  225. return ret;
  226. }
  227. static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu)
  228. {
  229. if (kvm_vgic_global_state.type == VGIC_V2)
  230. vgic_v2_enable(vcpu);
  231. else
  232. vgic_v3_enable(vcpu);
  233. }
  234. /*
  235. * vgic_init: allocates and initializes dist and vcpu data structures
  236. * depending on two dimensioning parameters:
  237. * - the number of spis
  238. * - the number of vcpus
  239. * The function is generally called when nr_spis has been explicitly set
  240. * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
  241. * vgic_initialized() returns true when this function has succeeded.
  242. * Must be called with kvm->lock held!
  243. */
  244. int vgic_init(struct kvm *kvm)
  245. {
  246. struct vgic_dist *dist = &kvm->arch.vgic;
  247. struct kvm_vcpu *vcpu;
  248. int ret = 0, i, idx;
  249. if (vgic_initialized(kvm))
  250. return 0;
  251. /* Are we also in the middle of creating a VCPU? */
  252. if (kvm->created_vcpus != atomic_read(&kvm->online_vcpus))
  253. return -EBUSY;
  254. /* freeze the number of spis */
  255. if (!dist->nr_spis)
  256. dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
  257. ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
  258. if (ret)
  259. goto out;
  260. /* Initialize groups on CPUs created before the VGIC type was known */
  261. kvm_for_each_vcpu(idx, vcpu, kvm) {
  262. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  263. for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
  264. struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
  265. switch (dist->vgic_model) {
  266. case KVM_DEV_TYPE_ARM_VGIC_V3:
  267. irq->group = 1;
  268. irq->mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
  269. break;
  270. case KVM_DEV_TYPE_ARM_VGIC_V2:
  271. irq->group = 0;
  272. irq->targets = 1U << idx;
  273. break;
  274. default:
  275. ret = -EINVAL;
  276. goto out;
  277. }
  278. }
  279. }
  280. if (vgic_has_its(kvm)) {
  281. ret = vgic_v4_init(kvm);
  282. if (ret)
  283. goto out;
  284. }
  285. kvm_for_each_vcpu(i, vcpu, kvm)
  286. kvm_vgic_vcpu_enable(vcpu);
  287. ret = kvm_vgic_setup_default_irq_routing(kvm);
  288. if (ret)
  289. goto out;
  290. vgic_debug_init(kvm);
  291. dist->implementation_rev = 2;
  292. dist->initialized = true;
  293. out:
  294. return ret;
  295. }
  296. static void kvm_vgic_dist_destroy(struct kvm *kvm)
  297. {
  298. struct vgic_dist *dist = &kvm->arch.vgic;
  299. struct vgic_redist_region *rdreg, *next;
  300. dist->ready = false;
  301. dist->initialized = false;
  302. kfree(dist->spis);
  303. dist->spis = NULL;
  304. dist->nr_spis = 0;
  305. if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  306. list_for_each_entry_safe(rdreg, next, &dist->rd_regions, list) {
  307. list_del(&rdreg->list);
  308. kfree(rdreg);
  309. }
  310. INIT_LIST_HEAD(&dist->rd_regions);
  311. }
  312. if (vgic_supports_direct_msis(kvm))
  313. vgic_v4_teardown(kvm);
  314. }
  315. void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
  316. {
  317. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  318. INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
  319. }
  320. /* To be called with kvm->lock held */
  321. static void __kvm_vgic_destroy(struct kvm *kvm)
  322. {
  323. struct kvm_vcpu *vcpu;
  324. int i;
  325. vgic_debug_destroy(kvm);
  326. kvm_vgic_dist_destroy(kvm);
  327. kvm_for_each_vcpu(i, vcpu, kvm)
  328. kvm_vgic_vcpu_destroy(vcpu);
  329. }
  330. void kvm_vgic_destroy(struct kvm *kvm)
  331. {
  332. mutex_lock(&kvm->lock);
  333. __kvm_vgic_destroy(kvm);
  334. mutex_unlock(&kvm->lock);
  335. }
  336. /**
  337. * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
  338. * is a GICv2. A GICv3 must be explicitly initialized by the guest using the
  339. * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
  340. * @kvm: kvm struct pointer
  341. */
  342. int vgic_lazy_init(struct kvm *kvm)
  343. {
  344. int ret = 0;
  345. if (unlikely(!vgic_initialized(kvm))) {
  346. /*
  347. * We only provide the automatic initialization of the VGIC
  348. * for the legacy case of a GICv2. Any other type must
  349. * be explicitly initialized once setup with the respective
  350. * KVM device call.
  351. */
  352. if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
  353. return -EBUSY;
  354. mutex_lock(&kvm->lock);
  355. ret = vgic_init(kvm);
  356. mutex_unlock(&kvm->lock);
  357. }
  358. return ret;
  359. }
  360. /* RESOURCE MAPPING */
  361. /**
  362. * Map the MMIO regions depending on the VGIC model exposed to the guest
  363. * called on the first VCPU run.
  364. * Also map the virtual CPU interface into the VM.
  365. * v2/v3 derivatives call vgic_init if not already done.
  366. * vgic_ready() returns true if this function has succeeded.
  367. * @kvm: kvm struct pointer
  368. */
  369. int kvm_vgic_map_resources(struct kvm *kvm)
  370. {
  371. struct vgic_dist *dist = &kvm->arch.vgic;
  372. int ret = 0;
  373. mutex_lock(&kvm->lock);
  374. if (!irqchip_in_kernel(kvm))
  375. goto out;
  376. if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
  377. ret = vgic_v2_map_resources(kvm);
  378. else
  379. ret = vgic_v3_map_resources(kvm);
  380. if (ret)
  381. __kvm_vgic_destroy(kvm);
  382. out:
  383. mutex_unlock(&kvm->lock);
  384. return ret;
  385. }
  386. /* GENERIC PROBE */
  387. static int vgic_init_cpu_starting(unsigned int cpu)
  388. {
  389. enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
  390. return 0;
  391. }
  392. static int vgic_init_cpu_dying(unsigned int cpu)
  393. {
  394. disable_percpu_irq(kvm_vgic_global_state.maint_irq);
  395. return 0;
  396. }
  397. static irqreturn_t vgic_maintenance_handler(int irq, void *data)
  398. {
  399. /*
  400. * We cannot rely on the vgic maintenance interrupt to be
  401. * delivered synchronously. This means we can only use it to
  402. * exit the VM, and we perform the handling of EOIed
  403. * interrupts on the exit path (see vgic_fold_lr_state).
  404. */
  405. return IRQ_HANDLED;
  406. }
  407. /**
  408. * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
  409. *
  410. * For a specific CPU, initialize the GIC VE hardware.
  411. */
  412. void kvm_vgic_init_cpu_hardware(void)
  413. {
  414. BUG_ON(preemptible());
  415. /*
  416. * We want to make sure the list registers start out clear so that we
  417. * only have the program the used registers.
  418. */
  419. if (kvm_vgic_global_state.type == VGIC_V2)
  420. vgic_v2_init_lrs();
  421. else
  422. kvm_call_hyp(__vgic_v3_init_lrs);
  423. }
  424. /**
  425. * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
  426. * according to the host GIC model. Accordingly calls either
  427. * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
  428. * instantiated by a guest later on .
  429. */
  430. int kvm_vgic_hyp_init(void)
  431. {
  432. const struct gic_kvm_info *gic_kvm_info;
  433. int ret;
  434. gic_kvm_info = gic_get_kvm_info();
  435. if (!gic_kvm_info)
  436. return -ENODEV;
  437. if (!gic_kvm_info->maint_irq) {
  438. kvm_err("No vgic maintenance irq\n");
  439. return -ENXIO;
  440. }
  441. switch (gic_kvm_info->type) {
  442. case GIC_V2:
  443. ret = vgic_v2_probe(gic_kvm_info);
  444. break;
  445. case GIC_V3:
  446. ret = vgic_v3_probe(gic_kvm_info);
  447. if (!ret) {
  448. static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
  449. kvm_info("GIC system register CPU interface enabled\n");
  450. }
  451. break;
  452. default:
  453. ret = -ENODEV;
  454. };
  455. if (ret)
  456. return ret;
  457. kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
  458. ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
  459. vgic_maintenance_handler,
  460. "vgic", kvm_get_running_vcpus());
  461. if (ret) {
  462. kvm_err("Cannot register interrupt %d\n",
  463. kvm_vgic_global_state.maint_irq);
  464. return ret;
  465. }
  466. ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
  467. "kvm/arm/vgic:starting",
  468. vgic_init_cpu_starting, vgic_init_cpu_dying);
  469. if (ret) {
  470. kvm_err("Cannot register vgic CPU notifier\n");
  471. goto out_free_irq;
  472. }
  473. kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
  474. return 0;
  475. out_free_irq:
  476. free_percpu_irq(kvm_vgic_global_state.maint_irq,
  477. kvm_get_running_vcpus());
  478. return ret;
  479. }